From patchwork Sun Oct 9 02:19: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: 13001878 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 57965C433F5 for ; Sun, 9 Oct 2022 02:20:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC2E16B0071; Sat, 8 Oct 2022 22:19:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C72C16B0073; Sat, 8 Oct 2022 22:19:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B164A6B0074; Sat, 8 Oct 2022 22:19:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9E5DD6B0071 for ; Sat, 8 Oct 2022 22:19:59 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6B8ECABCA0 for ; Sun, 9 Oct 2022 02:19:59 +0000 (UTC) X-FDA: 79999805718.21.67E8F31 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by imf03.hostedemail.com (Postfix) with ESMTP id 0BB1520014 for ; Sun, 9 Oct 2022 02:19:57 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id d24so7688606pls.4 for ; Sat, 08 Oct 2022 19:19:57 -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=IY6o+u/pLMZi4wFmcw5uqJB+plYJlGXOYXMKUIx/I6RempySqHXX4p8H6FrdpKXqaY l+TJqDQ6k81QwdzrhfHZC1XS9uG/K+ZOBuByePD6fGnX0gesRFBP6ejOcXwvZMfSpvHv 4/ip8yokki7OdNv7mE7FVla8cC5EjnDnIFkn+4FjWsvVxUt/q7oeOmYxV9yK3HoEsrr2 QMNddGvJ41qITpkE4W4hGrma43Y50VfVHoGv/dJK6Fck/N9/V4EmxYTzPZY2iYbHT65H B+Jf4usU0Akcf4NJ90Hj7EDMltRqDJtYOfPwZB5g7Gs0BdCe2CbXcEAQK0VPN88B+5U5 fNug== 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=xNtAHVFyg3nhSVKGdOPZQLriMrMGi+f18twcwESm+zZKRKoeEUmfEsEVjKmuSD1+Kg cW5kS3fbmx1BxUcL/ryjB6AMLEJ4hLsu9XMfxERiQjR7Ge5yAPk2kaj5j2SxmBq/oxEp C3Q+lvZMBSc8lrEydqDhaJqg/FwMS4voCBH13cqJNZPq3TLCEoI9VC8kvlRTIi8MLr+L p219YwrLlT9MJfXF/iDJqCakXJIDlvL2eqZU3Z/R+VGFZd8YySH8v4vyIbE2uNJ/QZRs CsrdOT59OAG6zOxKkQGua66UQnnLLMQgqI/gvcA18aOiTrXFC67aCuAUkF1b35uaetr0 5gNQ== X-Gm-Message-State: ACrzQf3j2wQhX/v0CsL4LIC+Uf17R+6MbF/4W6bJnQ8tUjjMZboHNEKo VKw2lDuqAHseqMNrS+aEn2s= X-Google-Smtp-Source: AMsMyM4AdT30BWpujVN2SmQYYAaqiC5g0djHrVWOrn0yrmMCpf0aIs13MpEVvw54B513wvnFdGFNaw== X-Received: by 2002:a17:902:9048:b0:17f:93a5:4638 with SMTP id w8-20020a170902904800b0017f93a54638mr12217181plz.108.1665281997076; Sat, 08 Oct 2022 19:19:57 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id jb12-20020a170903258c00b0017e9b820a1asm3967799plb.100.2022.10.08.19.19.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Oct 2022 19:19:56 -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 v2 1/5] ksm: abstract the function try_to_get_old_rmap_item Date: Sun, 9 Oct 2022 02:19:50 +0000 Message-Id: <20221009021950.315263-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221009021816.315205-1-xu.xin16@zte.com.cn> References: <20221009021816.315205-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=1665281998; 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=aNdkLhb807Cz7B3QpMbZoT+8KjuHp6ZzihPEGzma5n2VI2YFukKVBmBJrhVKR9P4nRXdyr iuQEYjZabZn5U86zEvow628n/UMnxXWFw8R8RWxPV0leYJUkLJ6q2JcfZaVcUxtqRCw9Cz ZgZeIOYTfnUcRDlWCeG1o9HdAvB822Q= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="IY6o+u/p"; spf=pass (imf03.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.193 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=1665281998; a=rsa-sha256; cv=none; b=x1WoCmh0marHa8i/co4+cvboaXhACbA4nA5VZh7L1ZaZgJDlm7pjfJHBOq3gEyxmj4pS0T PO/wzML5U+FkaKfSdHrCOpn/hXmTwLZhJraTNYkMSiptizC96LKXy8ovlPXwMmAQPGgBdh xQckauz8F7s+QKen5lt31XAgj7GiPI8= X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 0BB1520014 X-Stat-Signature: 8itmyh7tt5p64ehsh48ctkw8byyyp3ek Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="IY6o+u/p"; spf=pass (imf03.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.193 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1665281997-620715 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 */