From patchwork Wed Jun 5 09:53:01 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: 13686522 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 409C7C25B76 for ; Wed, 5 Jun 2024 09:48:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2DF16B0096; Wed, 5 Jun 2024 05:48:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB7326B0098; Wed, 5 Jun 2024 05:48:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A573E6B0099; Wed, 5 Jun 2024 05:48:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8761A6B0096 for ; Wed, 5 Jun 2024 05:48:26 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0B4C11A11DC for ; Wed, 5 Jun 2024 09:48:26 +0000 (UTC) X-FDA: 82196359812.11.B3172A7 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf19.hostedemail.com (Postfix) with ESMTP id E3DD11A0005 for ; Wed, 5 Jun 2024 09:48:23 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hqyx4qbp; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@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:references:dkim-signature; bh=s/mRvI3HBUwnpb0GleD2gvnnQDUhydoIMr6R2Pogbqs=; b=Ol2FMHidqXbBLaEk33d2B6o569K6clQb4xLrz1+KTrD8rSIpOLY56eC3nHtiSLvsZ7VzUK fUJI1WlrNAc86uPxDsp8RVt6Wpz8wsoU2kER/OytlJzYS/91/PIUcvPXrkxMxK0dLDUogB z0z4o4MMXQjanARAiiPHyqMiVKC/bFI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hqyx4qbp; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717580904; a=rsa-sha256; cv=none; b=Fi7FOD1VdWU6qcvGw31RE9flYWH7x4NqO6v/Zt354uT2/zBcBPVGq4IRw6yiturhrdYRJG yDtIRx5KxoNzlsUKuD+7c83DfrdgCq4PRokuWRfhqIGOfbEmSKXvT48szpNMj0l9GBW6u7 IEufJ01EhmL2FDK5d+d9uvYppIr1Wn4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id E4E89CE169E; Wed, 5 Jun 2024 09:48:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAD07C32781; Wed, 5 Jun 2024 09:48:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717580898; bh=H5TT0W89gkFgRO4p1LpEpFm2+mHkOB1T7hF2dgtWVhc=; h=From:To:Cc:Subject:Date:From; b=hqyx4qbpaNNhWJYB6Mi3oYuOjsTV+U6K5ROXZ4OVTgHogSokk0qrl5OWGtoUvC+lQ AplQ+lWnwqaELPRSz2Uz0C1xMmE71OFjKbDcnPITfKGZ4nElXQAfFkgwogVxskQ+p4 YWGiafS+jMRO6JNulFol5+CQpFHwhaL+7O6b6ydTcN+Y69GOh3GWra/mszoNlrW71n u2dQ7s3PuEj1sGf4Vtj0dVPG5FLOWDISoIPi6WyInU2aFryQGmbM6LKI06WwlOVQE6 EIReqbiv9xzaHzQOSSTrOGmrAkKvLd0R7GoVZTHPXXCBkFjq5CEDp097feerGpXzm7 puVzKXd460daQ== 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 1/3] mm/ksm: add anonymous check in find_mergeable_vma Date: Wed, 5 Jun 2024 17:53:01 +0800 Message-ID: <20240605095304.66389-1-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E3DD11A0005 X-Stat-Signature: c6r54woeo1ex5yf4mzxe7rxt5f5o915z X-HE-Tag: 1717580903-952302 X-HE-Meta: U2FsdGVkX1/S+MkYeIG1Rs5TC+OliazkfHakGUmLhL6tABZG00Orx6J3SMT+jH3Agaetdo4NHLGRwObMzvV4N/MpaHsqYHDqOk4Se0vr+h1K/y4ziyuUISdpSLA3mhxi5717dSMej4du3ngb8wYCAQkpqsRjI7is8Ju6ZSLoj+sTxjOT1e3nvXAX2fE12a+v2uciom6iplRHi5uFeQ2RO1IPfYy+/4bHEjhDOkt5nz2RPvXGFWse+c5Zz8z6YBcB0qjPQ1sSWZue3t7rytiq8VKuSk1VhiHrh6Cm9ZVG2KwH8yOnyIXew9UfI9FH4/Ym88MmrO6sNOJ7gj2HlkCmFR5JSNtS2RR57UXV8WqlR2q3tI5pAtwCKVgA+1VUeOU5/zl3jLBEWr1NvWqa+eFFkV3/cTGIk+Z6eWVNjGQuuu9mbkgTAipNQpH3B5vJhVEyIXUYrKlPg7QFqZa76RaJ+2Cnfyu1mUEiAMIS8xHycLKFEOBqoggbSOksRLH3+7F7DuoLhn4aem+lVaiiQjCI3Q2NNb3dONAbpgdLHmRye1PJvP9edN81/yECr1tSLRRejkUeNpIalPN0kVPX5x2KUTDnOZaQ+xlWXq8dtbz2CybPhrTW7Fz9LLNYewOW2PW+7z+t/R1DZ+XACx8E0CD+hRwDLXNbybFbkFMKFXqU9TZQk5zcX6eqEILvwt4hjFWHWC/fGCuqbWXWAuZisazYOIIHCK43rAOThVAvHrGXYv6VdcRZwL1ypFMluXM3mC0zxC5PPpLdzTkQMiWpJemTKAkjY/0MXHgL3s4qFvqfMCN8qayUBcsJ1d7hVwyeRMJIyqUdgjEDTEyMnZ5id2srpmJ85gQbDlypY688+Cs9fGmMJrusrayNXIZLJq1f2vIYNbWJCI578wmQeKMjgFPyYE+PVsFZu9PD+Ii7wEcKMV03sPeRAPXdVytWvhuUspjtPBx9KhP8Ty53BO+5STZ p1VBUKPQ njKDNfFBF6X1uZ7G1SaYzaWoUB97Gn8ID6TimGKFfXOiAjzeIpODyRlb+P85hDZZo5QH4fjPELbWEU1X92/eDO3xOGjc/Y22LQIrcNVu/x3TZFGWxnFIH26wR8jesJ+VVTqp61XaN6QwEoMOCvpRxaQGRR0zV78L0E5B9nKKOQAuE2Ol9C72gmgJ5WkuVSRNTWQW3q7in5HCb7G336cwUfeOdS/hiQLztYImQPXpBi4NTtR7UnBs2Q+O7r1Q3JchLLkdKmqAEhV+vGH//eFmeGEdXUq/hVgQ4pQGpQnWoWQ7If6zHjQwPwvsDpQ== 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)" We do vma_set_anonyous in do_mmap(), and then vma_is_anonymous() checking workable, use it as a extra check since ksm only care anonymous pages. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index f5138f43f0d2..088bce39cd33 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -742,7 +742,8 @@ static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm, if (ksm_test_exit(mm)) return NULL; vma = vma_lookup(mm, addr); - if (!vma || !(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) + if (!vma || !(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma || + !vma_is_anonymous(vma)) return NULL; return vma; } 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) { From patchwork Wed Jun 5 09:53:03 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: 13686524 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 D7A33C27C52 for ; Wed, 5 Jun 2024 09:48:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C7CF6B009A; Wed, 5 Jun 2024 05:48:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 54F026B009B; Wed, 5 Jun 2024 05:48:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EFBD6B009C; Wed, 5 Jun 2024 05:48:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1DB9E6B009A for ; Wed, 5 Jun 2024 05:48:30 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B1DF4120427 for ; Wed, 5 Jun 2024 09:48:29 +0000 (UTC) X-FDA: 82196359938.14.DC46DB2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf22.hostedemail.com (Postfix) with ESMTP id 41A86C0010 for ; Wed, 5 Jun 2024 09:48:26 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BERZumqA; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717580908; 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=2OTsB9t/Dou0CMwaiAHJETWFDgAHSWlPwCHL5wg4K3Y=; b=O89LA7qKLVDhZrBfY+PCVRKzMDzMeRMaajrLX3rgoUtwkPZoBI8Fgf1cbJtWpOIwl7vKvO 2n5tot4PrrGa7VIVqjX+csOdRQZjeYJFjv13vcA023rXZgIPoU+agmIjezCACew+UnfVNB BcrLXtJPHPQrKFDDkwDkAJPbzpZ+pJc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BERZumqA; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717580908; a=rsa-sha256; cv=none; b=7Uyv7ikhdAwfn75D3axcAcPcnPDthyfGqle52Rgj/vo7ST/zGVR4S8k62gZCri6XGMG4Yq HtHe44q27GJO2gu7JRfq61dnr7LWehyVqmU6nqDyly6KbcETTdQAR857whvdrsaXP43ND2 I9jyN2TsCnNlFvEZJE9WH3saDEXpOMI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 62966CE165D; Wed, 5 Jun 2024 09:48:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA735C32781; Wed, 5 Jun 2024 09:48:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717580904; bh=l4kI/Eqa/s3yp9ntZCNg8OdmGCzV7tDDQHa1fg1fi84=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BERZumqA2vlBVurZbqf3ltU4dd++VsLve3ArCLFM5cVVJE9xR7Wf/usLMZOgMaoPH MkjMktd7aVDlimP8qcGYJzOZo4QPKr58biZZF3aZF3i4PBQ7smQ+uy7baIJZE5EIio +BpT2b5kvjzZFcE5C2U6WAUeYLVoOeXuVeOoguYB3YAOOU6HTdtq5Vi2HJ1LHUAsUD 6gJv+IbIiHtdqJhOhmck8hkjIi8BWxHkgB2MOHtLL/07/Ked6czniRUmR4EyI64gfr /NlMkMKCgEedzMMvz6IrPV+FTqewyJ1lwDcgcrXvUUqf/5pUuXFZ4nMnPs+WvRaPYb zLg/T9ccxC/AA== 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 3/3] mm/ksm: move flush_anon_page before checksum calculation Date: Wed, 5 Jun 2024 17:53:03 +0800 Message-ID: <20240605095304.66389-3-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-Rspamd-Queue-Id: 41A86C0010 X-Stat-Signature: t65h5oqonobdz6zy17jdfqgz9zsdun4x X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1717580906-780518 X-HE-Meta: U2FsdGVkX1/vGLNH2m4TAgx+MR4AtiL4GYpo211ylo14XS2+zz5bwQ/sudGlww/jCzdbcVSolp8AbHhT6vD3h5MXqHeZY/2N5US7DQypKSHiaz+ERcXaOGvCsRiobI4aFeITrkLQTjNIbl7zHTGssaDmeZzBO5vRAyg1fNmMhHaC7fCouWOIVLkSV2VHknDIf/ckM5TyH83Lg+KvCMG+fNzI/NF7+3Fp1Q+Cp/ZtNhQ9JeE9Ra+PSh/R9aODpe/XSCwYNbvjoo1K387f8S2jNBPD9XPfHOV5BkBRtA2CcKUoPpemO4l4uGr9YorFpyUKty9kdiKg5CSKYC9zgleYJOjjgKRfm5h2SdS+GhQDH1QMtiSbDZhUJaZN6B36OnSL/GfJngQpCINc5GpyXclxiL0+bRD7fuz0akuUMOVEOvK6SOxfkxFSizsLQLEevdN/dyS/HOxKH9Mzwo0viB2DSAqmQzbzwy9SZKPZkKW4vC0YtDPZsAoG07cvvOhljU+W3Z2EfgMJf3+4JNhYH7UkqZdIPsdJiPgdN0CWB8IdNjzt65m4xxkFKI+w9enetAWttSg4HIFf/SCvMglXt0TI5k0fYzNCYJ8ZJi1DxxZSLgC1p0/gs5PA02LChQsHQZFUEZsia431FS1b5/Y6R/ZFqWPShcFBKQ08yL4Q8pM5aiT+6DJOSOKZ1aHeMMpzdXW208QPxV1tERK/Bp57oMlPwcOsaRaIkP95VVySRhLQs/XGcOmTYXMToPYSb9iOJqO3hesKhzOTpN3GGNdJumWsdqWsq3XhlcYheSjjRXiNMA8SbSIbsXJJYtwt7V9l+BWTN6fuPOOiMLjFcmiUunrha4Ncw0c5vTUvz2iHaTi0bVB6WspxRtAyFjaZEvgFlwCzyUI2paen/SGGTszz3M0DLpHsiGqSN3clivAFTudTXIBEAFg/NiHSW+VRB5PLvKQJr8Mrh5U7OOVjHTKFHp9 q9qCrLxF Ih4PudsX+qEyo4/Jk3trUtj0U4mh1Hdj3pZJ2GbrPNMfcG+V0OKw9aPOolgpSQi82APtqsqNcIaar2GIWVYldBl9ja4JNyH9OPamplcA79MwBEZfvv0uz/nNATRrtLKjxT/YdqthWiUTxMwZjeOX2bqRhddPXgw7JBfd9lCKm1T+uRd8Q2jCZcWMRs3yHEOz0+Ew2iZ+xnEn3M0H8LfgjrVivkTNJ2VnIH7Cv7jnzMYWE+4LpYnlSus9TfhI1KF65/0EATZM+4A5SXDB65HmzO3KCEvl0jkAMgOis 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)" commit 6020dff09252 ("[ARM] Resolve fuse and direct-IO failures due to missing cache flushes") explain that the aim of flush_anon_page() is to keep the cache and memory content synced. Also as David Hildenbrand pointed, flush page without the page contents reading here is meaningless, so let's move the flush action just before page contents reading, like calc_checksum(), not just find a page, flush it, w/o clear purpose. This should save some flush actions why keep page content safely synced. BTW, write_protect_page() do another type flush actions before pages_identical(). Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index ef335ee508d3..77e8c1ded9bb 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -784,10 +784,7 @@ static struct page *get_mergeable_page(struct ksm_rmap_item *rmap_item) goto out; if (is_zone_device_page(page)) goto out_putpage; - if (PageAnon(page)) { - flush_anon_page(vma, page, addr); - flush_dcache_page(page); - } else { + if (!PageAnon(page)) { out_putpage: put_page(page); out: @@ -2378,7 +2375,12 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite mmap_read_unlock(mm); return; } + + /* flush page contents before calculate checksum */ + flush_anon_page(vma, page, rmap_item->address); + flush_dcache_page(page); checksum = calc_checksum(page); + if (rmap_item->oldchecksum != checksum) { rmap_item->oldchecksum = checksum; mmap_read_unlock(mm); @@ -2662,8 +2664,6 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) if (is_zone_device_page(*page)) goto next_page; if (PageAnon(*page)) { - flush_anon_page(vma, *page, ksm_scan.address); - flush_dcache_page(*page); rmap_item = get_next_rmap_item(mm_slot, ksm_scan.rmap_list, ksm_scan.address); if (rmap_item) {