From patchwork Tue Oct 11 02:21:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13003559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC1EAC433F5 for ; Tue, 11 Oct 2022 02:22:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 608468E0001; Mon, 10 Oct 2022 22:22:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 568E56B0073; Mon, 10 Oct 2022 22:22:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3941F8E0001; Mon, 10 Oct 2022 22:22:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 20F536B0072 for ; Mon, 10 Oct 2022 22:22:05 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EAD6A805A4 for ; Tue, 11 Oct 2022 02:22:04 +0000 (UTC) X-FDA: 80007068568.10.68FE865 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf23.hostedemail.com (Postfix) with ESMTP id 6A3AB14002B for ; Tue, 11 Oct 2022 02:22:04 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id c20so2345295plc.5 for ; Mon, 10 Oct 2022 19:22:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jsbh45Fv58HEehSJMVvF1reAzOnYH1CPjnbjNiOuTbI=; b=lgOVYuFLApBB99lxB0wJ+xs6iHGx1lEt2NfNrp2YK62xBG7cGjYeVl9kKELJ/qMHMn Zyd1vrv+Pck4AGaEvT974cU36a/3WWkDBHnuCTXvw1bc04EmlKd0Em5eipe+qGAEyUaU LF2Fn5nE/nb7YoHhIKt0V+cWuj0RHTf4QLYA0QG0VPhZnW8IKCAS8UCah3FqahzOxcG7 Ul5miU01nXsNlaIoyvq9ItU767ag1L+LNKPvPXixm6iPHOOSv5fl7pBcXW3nZUysG+IZ v5tV7aCmWU34KlM0txj6ud6e/MUyrd0QHdt4fi49yFsR/gC0GEzRYKXyVR47/GJsAc+p cLEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jsbh45Fv58HEehSJMVvF1reAzOnYH1CPjnbjNiOuTbI=; b=0HgeelnKhZmktAFC2hWurjN993EgBSlB7Fit4XU5rkX2eggCzcY2Rr8DG6sO/+y8ml XJ+rvdZM2XV0oIWN1+dGjaLNxujtlCRQZ/eAAyo9o370/thfFW8ehXmMFdPrAJsCvdQU L7EhOt48oaF8K/OeKFTckORA0O4oJHnGI4CyO53eUTQCyHpRkEbExKiVTGcnGcMbtMqR vIs+FGHkPEg5Nil7bzvmGgh30bI4OxB8cNCJdPZCsialYZchRalUJupfcU2dyY3ft8GT 4t3KWrRyi76YnxD3nPlmpOHRTpFXcFr7dPU1poIdFJe3vZwj0GFhv0e26mgtj/GPqE2S 3u2g== X-Gm-Message-State: ACrzQf208uHOnG4QkymuGwVDZtuEtu07k5aiJuuMlW55WV3ARUYir6fo 1B4vtG0rr9CklbqARbr7hpA= X-Google-Smtp-Source: AMsMyM7JW36aWaS0bLlTDqSb2ZhzhL4vqsQD6p3KYfBeOqc7tFwXhZjgSDiBMKko8MCBq+bCt5qKKA== X-Received: by 2002:a17:902:f0d4:b0:17f:72bd:74f3 with SMTP id v20-20020a170902f0d400b0017f72bd74f3mr22495074pla.109.1665454923480; Mon, 10 Oct 2022 19:22:03 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id l24-20020a17090aaa9800b0020d51aefb82sm94655pjq.19.2022.10.10.19.22.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 19:22:03 -0700 (PDT) From: xu.xin.sc@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, xu xin , Claudio Imbrenda , David Hildenbrand , Xuexin Jiang , Xiaokai Ran , Yang Yang Subject: [PATCH v3 1/5] ksm: abstract the function try_to_get_old_rmap_item Date: Tue, 11 Oct 2022 02:21:50 +0000 Message-Id: <20221011022150.322212-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221011022006.322158-1-xu.xin16@zte.com.cn> References: <20221011022006.322158-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665454924; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jsbh45Fv58HEehSJMVvF1reAzOnYH1CPjnbjNiOuTbI=; b=0uVTS1/TNlm20bGeYRtMlXWD9rNQavmMoenh3zGLBGRzPuheFI4E4/wvmX1VnAVFKIMZ3R 1PPKqAK0ACf3bhKJ254ufG1nfgETojwodF4LGp9yk3GGMWKnhjrJwilqb7lbtzUa5bZiZu CVM2lER3EoXXkojAASjppuqdLQq1z1g= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lgOVYuFL; spf=pass (imf23.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.196 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665454924; a=rsa-sha256; cv=none; b=Si39g2oV4JQTw/qjlkvJDHoP7X/kda7M5vQY7JAkxuVKN+GtwPOAu1R272xolQacrR7gcn C2Qz/VsiCXv0NwO0PH30Oan1h9dIVzs4ZBOOSawGafG4yNd15fzltrCLESHO3/gZuwcRnU DRwD2k+xxWAAoOfRB+tvi6PLlsCpxjo= Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lgOVYuFL; spf=pass (imf23.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.196 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: u3318pmb69oacg84cid8wp9zmfupedcb X-Rspamd-Queue-Id: 6A3AB14002B X-HE-Tag: 1665454924-632331 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: xu xin A new function try_to_get_old_rmap_item is abstracted from get_next_rmap_item. This function will be reused by the subsequent patches about counting ksm_zero_pages. The patch improves the readability and reusability of KSM code. Signed-off-by: xu xin Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Cc: Xiaokai Ran Cc: Yang Yang --- mm/ksm.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index c19fcca9bc03..13c60f1071d8 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2187,23 +2187,36 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite } } -static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, - struct ksm_rmap_item **rmap_list, - unsigned long addr) +static struct ksm_rmap_item *try_to_get_old_rmap_item(unsigned long addr, + struct ksm_rmap_item **rmap_list) { - struct ksm_rmap_item *rmap_item; - while (*rmap_list) { - rmap_item = *rmap_list; + struct ksm_rmap_item *rmap_item = *rmap_list; if ((rmap_item->address & PAGE_MASK) == addr) return rmap_item; if (rmap_item->address > addr) break; *rmap_list = rmap_item->rmap_list; + /* Running here indicates it's vma has been UNMERGEABLE */ remove_rmap_item_from_tree(rmap_item); free_rmap_item(rmap_item); } + return NULL; +} + +static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, + struct ksm_rmap_item **rmap_list, + unsigned long addr) +{ + struct ksm_rmap_item *rmap_item; + + /* lookup if we have a old rmap_item matching the addr*/ + rmap_item = try_to_get_old_rmap_item(addr, rmap_list); + if (rmap_item) + return rmap_item; + + /* Need to allocate a new rmap_item */ rmap_item = alloc_rmap_item(); if (rmap_item) { /* It has already been zeroed */