Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp6643625rwl; Thu, 29 Dec 2022 17:19:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXuEYm00PTilCnMRXfi+ABa+vsCyPgXjku4Hmboii4s119zkkQ5T/X8J6IYZQdAppTv2/poz X-Received: by 2002:a05:6402:3784:b0:46b:eadf:8d34 with SMTP id et4-20020a056402378400b0046beadf8d34mr24407546edb.7.1672363159729; Thu, 29 Dec 2022 17:19:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672363159; cv=none; d=google.com; s=arc-20160816; b=EeL29T2ZOB7sMSpCxUNtBEHZnSsKz/dcEvk+eF/QxNwhlnJgwG3cozJVRRS0uIoaoG Fz7s4GO6QjC+c/o0xfS5X890Zzq2Y0JqPItiqdEdLwxTMLuS60siqwknPOmCmoRRDFdS FEyR7hBLv8Mer4MdVkrKpLbVoDDsFSc4CU7nIzOqBsC/spzYVDmGwXwXrmDeHneLeR+H ugaEfTEIL4SF/eWEgrqxQGg+uBcAG3i3iqogLJUiYB2+1v+Fgd1jkS8j1elWZTdmkiHZ uhiea9EA8G5Q0bH0glb+HIDvitHp/dztW+AUN2cOnvE3io8E50hnQvc7x9YjPT42WDar fefg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:mime-version:message-id:date; bh=Fn8CaapX2uBzNcHLvV2QH2LQpWpDs2Djiqkkl35bf/w=; b=BrN+yRRU59ZNgBe53wOo0x8Awfcf7rIxmEtq6poTTlQ2cqq7fkPsOxZvMW7Bhxxn4k DfMEF1wgzgtvjCIRGlgxKXC2UYuZ1FeKan47SnQitIc8btGlWYt2dt/yY3dxAHCw8EHp nneJSbDAGwWEEUIiW/6l1qrlatVMTq88/iEupa8uAPnLysMSUb2SJ9p5IiZSglffxcOq d6XTaXUFz7+v0fPNyTAC85jUcfcS/DDwTOfYVEAmNXgPgSbZaZXpqiNHoe2/cAMS4z0X W3l3LOy8MPADhZOncK6jQdAYOp4au7kcsy7c6dq78qHEvhAYwwT75g1nh9zV/xIF6HDX 1FfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a056402248600b004885f068e3bsi4813489eda.138.2022.12.29.17.19.03; Thu, 29 Dec 2022 17:19:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234024AbiL3BMy (ORCPT + 64 others); Thu, 29 Dec 2022 20:12:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbiL3BMw (ORCPT ); Thu, 29 Dec 2022 20:12:52 -0500 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5786DFD0B for ; Thu, 29 Dec 2022 17:12:51 -0800 (PST) Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4NjnLV0XKMz8R039; Fri, 30 Dec 2022 09:12:50 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl2.zte.com.cn with SMTP id 2BU1CiXg057579; Fri, 30 Dec 2022 09:12:44 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:12:44 +0800 (CST) Date: Fri, 30 Dec 2022 09:12:44 +0800 (CST) X-Zmail-TransId: 2b0363ae3b0c59fb2628 X-Mailer: Zmail v1.0 Message-ID: <202212300912449061763@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIHY1IDEvNl0ga3NtOiBhYnN0cmFjdCB0aGUgZnVuY3Rpb24gdHJ5X3RvX2dldF9vbGRfcm1hcF9pdGVt?= Content-Type: text/plain; charset="UTF-8" X-MAIL: mse-fl2.zte.com.cn 2BU1CiXg057579 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63AE3B12.000 by FangMail milter! X-FangMail-Envelope: 1672362770/4NjnLV0XKMz8R039/63AE3B12.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3B12.000/4NjnLV0XKMz8R039 X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, SORTED_RECIPS,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- mm/ksm.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 83e2f74ae7da..5b0a7343ff4a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2214,23 +2214,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 */ -- 2.15.2