From patchwork Wed Jun 5 09:53:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexs@kernel.org X-Patchwork-Id: 13686523 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 5277FC27C53 for ; Wed, 5 Jun 2024 09:48:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32D0B6B0098; Wed, 5 Jun 2024 05:48:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B6266B0099; Wed, 5 Jun 2024 05:48:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 155C06B009A; Wed, 5 Jun 2024 05:48:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EF82C6B0098 for ; Wed, 5 Jun 2024 05:48:26 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 91A0E140FF1 for ; Wed, 5 Jun 2024 09:48:26 +0000 (UTC) X-FDA: 82196359812.02.2FAB71D Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf15.hostedemail.com (Postfix) with ESMTP id 3426EA0003 for ; Wed, 5 Jun 2024 09:48:23 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=l6yWcPbZ; spf=pass (imf15.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717580904; 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=18saRldxYFRi4jjq+Y562Qd2Ui/iTLlILqElqN8m+rA=; b=A2ak7dBMZ6RM+f0G7MIgi54NXd1HaBzI0wLnDRBPVimNwICEPIZ99JYWeJBOKgpWOz7S5e v2aDD6VEDg6M6aTIimaOBZAVb02O3BsBpfbmQZivi1REbEaqSB9VefuzHlbZkA7UFSKnIc omRDSP0vUrg1vd189rqmZfuTPb894zE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=l6yWcPbZ; spf=pass (imf15.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717580904; a=rsa-sha256; cv=none; b=MVH8Y2KWWvO7i+OHakAcAGlcbm8UXMUZT8r7JFUHtmqolV4SUkzerEkz8Upx0ECzph+uyH Bw1m5htQqKdeNr9yooucsmGUKdXcfgZc+jE8E/cPv+P1K8N4n7y2VdRPUjh4uxgq7tBYtT J9m+OCPM92Yei6aUH2igof4R98AhC50= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 88FC5CE16FB; Wed, 5 Jun 2024 09:48:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CAD8CC3277B; Wed, 5 Jun 2024 09:48:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717580901; bh=TkZmn0j6P9PP99V7b1wYr7sy8xz+wQfdtM61tMGn0RI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l6yWcPbZMQQ1acDAPHB8r8Cmps0DD+OQjsvrBnG0oE8R0y8IEJlBI1jMS1sENnl1P +0iJCsd9CJ7/BP2JJ5URZQJi315OeYQiKE6Jz83wZBfr/WSnpfeWxXa8ggtkyUw9Ht UeWwLYTufxMArxQ1sK5zNMJO9gkbXUOpSsYmLLTLVQ/TdcUlBwx70gHsrgd7c42eH1 7sjpuM6MkabNXGuN/4SIBSd+ghANbJ58oZK2LOyOAS8cuQX25OWrn8JYGaT4l44dnT qVN8bHcR8PVx8oRkhF1KxqSYcCJDtvFtOYHMH63XyodCb/DoKGMwKvZmV+4KZYqS3s KiouR0RMK7GZA== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [RFC 2/3] mm/ksm: jump out early if vma out of date in cmp_and_merge_page Date: Wed, 5 Jun 2024 17:53:02 +0800 Message-ID: <20240605095304.66389-2-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605095304.66389-1-alexs@kernel.org> References: <20240605095304.66389-1-alexs@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 1k5wedh93mqr4uobyw4f6eu4uxci61wr X-Rspamd-Queue-Id: 3426EA0003 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1717580903-58300 X-HE-Meta: U2FsdGVkX188MmEj5Fxtqe4OgTJ/6bd091d3dBMLP1Yfgl0SRdVkkHmmtMqkmtX5wJ0V8Edk5ptiUPBgdxGS5GPVqmEDMfPfASkRo9KCzTp+MCocpBThMAcKg58PXJJl4y4mB7EQU0M2h9f5EAAcP3OWfdguoodcBukdBeh4CZGPYTpaFHTEYoujpwv8rBECIQ4BUk+zeJp7yVkOy/6bjaia5Da0x+O7aAUtBDJPgSpLdW+Ome36Tu99kKVi9gkh/9tzW26jZQAHaFQcIlcIeICRfEZWLH3Kq7INa02c/EVjrC2cIfagZU023eIdNlUk6UVXcL7RESnHGKvK2+HuT8VusSQpEmsIw5RL+n7OTPcEiuHra6cGEmW21WhFpwAtoQ7chkl8eLIh2pxPNW1GeTA/Cf9I8DnjC1nIAmc3tt2vMSzoIdyIxJk/EhB9FC0kG1Ji0jkSRjmy9dr3qjA3Ylvx8QMTH94jskq8yknJF6U2HrecDL4rQb3LsVEYl8MZ/LLwLt2EzkQ6u9qCSgF7UtVWiGza3bm6mxrx7nVSeCHff1L9sRI+FBpVrYWNXjddxTbNugw85VhkGu3CSFDSTFSxWNTn1aOoW1aZ+1usLWiXwxio8ty2s7xFSsiZzOaadonhN9gf+DhH8zFKK+WlyM77qE6+smJ04kKIiCufSKnL1l3iEZZbow3FcIXT08qLjMfj7eDiYyhvrIaPtzS1TCtr44c/pw01uuhuqxCaO06nfKccoOa55RF6qMy9K7CJBt+pI4uPo26NC6nLskFTO72ov3OKSemY9Xpa2p6tLgfU30PYbP3fQA1FNCYjjoYDmO0cWw04MbQEWdB9Pggnn/HH2mvyPGCk+MsKYtgDkKJwRWjuaGd0G8KhOcuvHPwdZekgZqA1ChiCAymYP3hNCnmgj96ujs15OX+EL2G/fLLjqpRhX+uXbuzK2P4x9hmoT0WdqPzu/sBEq0jYuMx /bkaN/AF 1s0GYFys82Lpdml5cvKAgsHuvpbup+/doyjl2f0WSVVibZpIOI1Ierwxm4jFQ5m+8yG5FRF7i8l+C0hVH6SxXb176WbU4+9snG8sGHLUrDsiPEFlLH7djdomG+A7YoYfimWUJ8WZ2KBSziu/Ahhc4fIU4cAdDxKgupQvFCXoNk/ojnq2kvG/A8YP54bAwUrjoWAO8DCjB4dV6Y81ykkO1BpmYFyDVYikzVjBP0PqaOR1IBwUzDe/yFreNSyj4Los2etlYV9ARvq+kkYNjiS8Uyj2SFO5c6bNLi3fG 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: List-Subscribe: List-Unsubscribe: From: "Alex Shi (tencent)" If we get a page which in a disappearing vma, the page should be useless soon, so don't bother to add it into ksm. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 088bce39cd33..ef335ee508d3 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2315,6 +2315,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite unsigned int checksum; int err; bool max_page_sharing_bypass = false; + struct vm_area_struct *vma; stable_node = page_stable_node(page); if (stable_node) { @@ -2370,9 +2371,17 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite * don't want to insert it in the unstable tree, and we don't want * to waste our time searching for something identical to it there. */ + mmap_read_lock(mm); + vma = find_mergeable_vma(mm, rmap_item->address); + if (!vma) { + /* If the vma is out of date, we do not need to continue.*/ + mmap_read_unlock(mm); + return; + } checksum = calc_checksum(page); if (rmap_item->oldchecksum != checksum) { rmap_item->oldchecksum = checksum; + mmap_read_unlock(mm); return; } @@ -2381,31 +2390,20 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite * appropriate zero page if the user enabled this via sysfs. */ if (ksm_use_zero_pages && (checksum == zero_checksum)) { - struct vm_area_struct *vma; - - mmap_read_lock(mm); - vma = find_mergeable_vma(mm, rmap_item->address); - if (vma) { - err = try_to_merge_one_page(vma, page, - ZERO_PAGE(rmap_item->address)); - trace_ksm_merge_one_page( - page_to_pfn(ZERO_PAGE(rmap_item->address)), - rmap_item, mm, err); - } else { - /* - * If the vma is out of date, we do not need to - * continue. - */ - err = 0; - } - mmap_read_unlock(mm); + err = try_to_merge_one_page(vma, page, ZERO_PAGE(rmap_item->address)); + trace_ksm_merge_one_page(page_to_pfn(ZERO_PAGE(rmap_item->address)), + rmap_item, mm, err); /* * In case of failure, the page was not really empty, so we * need to continue. Otherwise we're done. */ - if (!err) + if (!err) { + mmap_read_unlock(mm); return; + } } + mmap_read_unlock(mm); + tree_rmap_item = unstable_tree_search_insert(rmap_item, page, &tree_page); if (tree_rmap_item) {