From patchwork Fri Feb 10 01:16:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 55178 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp686083wrn; Thu, 9 Feb 2023 17:35:18 -0800 (PST) X-Google-Smtp-Source: AK7set914wJCsSHebUaaxeKSFWAOo+ODza4JhqjJ4/J2rHbciNi+Dz/v6xsEe1kszT3zyWu69y/o X-Received: by 2002:a05:6a20:69a6:b0:bf:85bc:ef33 with SMTP id t38-20020a056a2069a600b000bf85bcef33mr13663509pzk.42.1675992917724; Thu, 09 Feb 2023 17:35:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675992917; cv=none; d=google.com; s=arc-20160816; b=nHgB4eSnOa8R03FHdSsRpHelLlYUjk7FEY+/g9o/8HDS4vFEk31ZS88KdtgJtD3gU4 8JYIpijflx2lgXqJi1I5PjRMcA9bh3QZ0G8PVViaKqaNKvcvzwuVH6MDIkaQiTH1rNJC bUoiD0202ZODD7B46cIlvaSCNp3xlftfQhbwrou5i9Lk2EYoVn7lpzVmxyumR8nvdgSC RJYII2+SC9PjQsdbsZP6id0cTKtmVYFGzBmzmPim54t1PwQIzAM/cqXs0kEhHL2pFirT ezU8+jHSdM1Ra9v4o6nOftl5bymtgh117bpwI4pL78BlBCQX3X/5XVhMAXNxqw+8A5ND z3UA== 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=Go5knzcgdTW3rle4PYI2BccmNgMMlWTFK6bplH5FMRI=; b=wQNZYFuy9VfniX9016I+uRrb0y6JjjAbpzJ9uJClK5MxTSZJ8UrbKlSkTeYcwatuJG 1Lzl6A9FeIwphlYi0dm4z+fyDGoBU0b+8rVZAKAXD5Uv4RIqpRN3W0BwOy81ZacczH/E 7kzbTS5yIfTdgibCO0bSTAqApIv2wviQPfKwLxP50PgwaPqBSPmd/64AOgMxygAOLHjl 3KwfDB0U90yRVtivkWknlfBQAjqLPccH+CO8ZZqHLI1K4HVheyvuTPYugI/lj+rWv1Pv FZHmwto3G3ldFOLXCGjQgWAsZpF8gOXYvDNLcLa8w05GsJbhzUJhHG1vAiiFzbiIvcIw FgPA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z5-20020a626505000000b0058bcb6283edsi2885706pfb.288.2023.02.09.17.35.04; Thu, 09 Feb 2023 17:35:17 -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 S229483AbjBJBQx (ORCPT + 99 others); Thu, 9 Feb 2023 20:16:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbjBJBQv (ORCPT ); Thu, 9 Feb 2023 20:16:51 -0500 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37D2335B8 for ; Thu, 9 Feb 2023 17:16:47 -0800 (PST) Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (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 4PCbRd2v10z8R044; Fri, 10 Feb 2023 09:16:45 +0800 (CST) Received: from szxlzmapp05.zte.com.cn ([10.5.230.85]) by mse-fl1.zte.com.cn with SMTP id 31A1GfQW007483; Fri, 10 Feb 2023 09:16:41 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 10 Feb 2023 09:16:42 +0800 (CST) Date: Fri, 10 Feb 2023 09:16:42 +0800 (CST) X-Zmail-TransId: 2b0363e59afa1ea3fe53 X-Mailer: Zmail v1.0 Message-ID: <202302100916423431376@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v6_1/6=5D_ksm=3A_abstract_the_function_try=5Fto?= =?utf-8?q?=5Fget=5Fold=5Frmap=5Fitem?= X-MAIL: mse-fl1.zte.com.cn 31A1GfQW007483 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63E59AFD.000 by FangMail milter! X-FangMail-Envelope: 1675991805/4PCbRd2v10z8R044/63E59AFD.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63E59AFD.000/4PCbRd2v10z8R044 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757405949720729870?= X-GMAIL-MSGID: =?utf-8?q?1757405949720729870?= 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 v5->v6: Modify some comments according to David's suggestions. --- mm/ksm.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 83e2f74ae7da..905a79d213da 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2214,23 +2214,38 @@ 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; + /* + * If we end up here, the VMA is MADV_UNMERGEABLE or its page + * is ineligible or discarded, e.g. MADV_DONTNEED. + */ 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; + + rmap_item = try_to_get_old_rmap_item(addr, rmap_list); + if (rmap_item) + return rmap_item; + rmap_item = alloc_rmap_item(); if (rmap_item) { /* It has already been zeroed */