From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 56A38CCD199 for ; Mon, 20 Oct 2025 11:04:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: Message-ID:Date:References:In-Reply-To:Subject:Cc:To:From:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=b+3d8uC7KF4xfmtSqoZNFp1K71/AwpCYuc/qfKAkSII=; b=DwzrJhbctAj9GBfYJZ5UeLXP2u d/hlSc4d4GJ/2yNPbQTyQySX2gF1qlbxLWHa12CTk/DB9XKZ14zKdyeA5M1mOJ73a8CVy3Ft+8oFk JYOKqRaJWgBC1VuaEm+xBiLwK2kuDpoLTJGfdwQ6Gq/lkT+NtsQ3LNHZoCW9aVjcTqy20gHS7eN9c 53uYEEa6Wgl70URy+pDCZVXA6ZxRas9faaIAwoVSK5XVdPnlZo9We63jzXYGZQ0gD0jwVDqWv/9Zt MTCbqgn24xTeIdjcvFDajxPzVa6sUUOQShjnoMtWRjETNY+2y9GFQIwkzbAfjTDxBJ1OXGJWWzQrq lUQsY5fw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vAnge-0000000CzTU-0LWN; Mon, 20 Oct 2025 11:04:24 +0000 Received: from out30-101.freemail.mail.aliyun.com ([115.124.30.101]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vAngb-0000000CzRV-2FGQ for linux-arm-kernel@lists.infradead.org; Mon, 20 Oct 2025 11:04:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1760958259; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=b+3d8uC7KF4xfmtSqoZNFp1K71/AwpCYuc/qfKAkSII=; b=jfTjLy23NlpXCkYAluPoYpfpV0Sl5nvLCoJVGtpzXNl4d4etD4kxelTpp0i8HiJ3UIH1dumpqCn1IbPsUleNpr0mitTV8y26fkBvr2qbx8mMj9lMkwjJvskvC1oGw6Pp4nerOQmlLh1Txfh08v6UBETN+DyT7PtJ3hhVn+kveyM= Received: from DESKTOP-5N7EMDA(mailfrom:ying.huang@linux.alibaba.com fp:SMTPD_---0WqakbIX_1760958255 cluster:ay36) by smtp.aliyun-inc.com; Mon, 20 Oct 2025 19:04:16 +0800 From: "Huang, Ying" To: Anshuman Khandual Cc: Catalin Marinas , Will Deacon , Ryan Roberts , Gavin Shan , Ard Biesheuvel , "Matthew Wilcox (Oracle)" , Yicong Yang , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] arm64, mm: avoid always making PTE dirty in pte_mkwrite() In-Reply-To: <0e6d1f1f-a917-4e36-80de-03ba94c6d850@arm.com> (Anshuman Khandual's message of "Mon, 20 Oct 2025 07:39:51 +0530") References: <20251015023712.46598-1-ying.huang@linux.alibaba.com> <0e6d1f1f-a917-4e36-80de-03ba94c6d850@arm.com> Date: Mon, 20 Oct 2025 19:04:14 +0800 Message-ID: <87o6q1dec1.fsf@DESKTOP-5N7EMDA> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251020_040421_874748_D4AF429A X-CRM114-Status: GOOD ( 24.59 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi, Anshuman, Anshuman Khandual writes: > On 17/10/25 11:36 PM, Catalin Marinas wrote: >> On Wed, Oct 15, 2025 at 10:37:12AM +0800, Huang Ying wrote: >>> Current pte_mkwrite_novma() makes PTE dirty unconditionally. This may >>> mark some pages that are never written dirty wrongly. For example, >>> do_swap_page() may map the exclusive pages with writable and clean PTEs >>> if the VMA is writable and the page fault is for read access. >>> However, current pte_mkwrite_novma() implementation always dirties the >>> PTE. This may cause unnecessary disk writing if the pages are >>> never written before being reclaimed. >>> >>> So, change pte_mkwrite_novma() to clear the PTE_RDONLY bit only if the >>> PTE_DIRTY bit is set to make it possible to make the PTE writable and >>> clean. >>> >>> The current behavior was introduced in commit 73e86cb03cf2 ("arm64: >>> Move PTE_RDONLY bit handling out of set_pte_at()"). Before that, >>> pte_mkwrite() only sets the PTE_WRITE bit, while set_pte_at() only >>> clears the PTE_RDONLY bit if both the PTE_WRITE and the PTE_DIRTY bits >>> are set. >>> >>> To test the performance impact of the patch, on an arm64 server >>> machine, run 16 redis-server processes on socket 1 and 16 >>> memtier_benchmark processes on socket 0 with mostly get >>> transactions (that is, redis-server will mostly read memory only). >>> The memory footprint of redis-server is larger than the available >>> memory, so swap out/in will be triggered. Test results show that the >>> patch can avoid most swapping out because the pages are mostly clean. >>> And the benchmark throughput improves ~23.9% in the test. >>> >>> Fixes: 73e86cb03cf2 ("arm64: Move PTE_RDONLY bit handling out of set_pte_at()") >>> Signed-off-by: Huang Ying >>> Cc: Catalin Marinas >>> Cc: Will Deacon >>> Cc: Anshuman Khandual >>> Cc: Ryan Roberts >>> Cc: Gavin Shan >>> Cc: Ard Biesheuvel >>> Cc: "Matthew Wilcox (Oracle)" >>> Cc: Yicong Yang >>> Cc: linux-arm-kernel@lists.infradead.org >>> Cc: linux-kernel@vger.kernel.org >>> --- >>> arch/arm64/include/asm/pgtable.h | 3 ++- >>> 1 file changed, 2 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h >>> index aa89c2e67ebc..0944e296dd4a 100644 >>> --- a/arch/arm64/include/asm/pgtable.h >>> +++ b/arch/arm64/include/asm/pgtable.h >>> @@ -293,7 +293,8 @@ static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) >>> static inline pte_t pte_mkwrite_novma(pte_t pte) >>> { >>> pte = set_pte_bit(pte, __pgprot(PTE_WRITE)); >>> - pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); >>> + if (pte_sw_dirty(pte)) >>> + pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); >>> return pte; >>> } >> >> This seems to be the right thing. I recall years ago I grep'ed >> (obviously not hard enough) and most pte_mkwrite() places had a >> pte_mkdirty(). But I missed do_swap_page() and possibly others. >> >> For this patch: >> >> Reviewed-by: Catalin Marinas >> >> I wonder whether we should also add (as a separate patch): >> >> diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c >> index 830107b6dd08..df1c552ef11c 100644 >> --- a/mm/debug_vm_pgtable.c >> +++ b/mm/debug_vm_pgtable.c >> @@ -101,6 +101,7 @@ static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) >> WARN_ON(pte_dirty(pte_mkclean(pte_mkdirty(pte)))); >> WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma)))); >> WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); >> + WARN_ON(pte_dirty(pte_mkwrite_novma(pte_mkclean(pte)))); >> WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); >> } >> >> For completeness, also (and maybe other combinations): >> >> WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte)))); > > Adding similar tests to pte_wrprotect(). > > diff --git a/mm/debug_vm_pgtable.c b/mm/debug_vm_pgtable.c > index 830107b6dd08..573632ebf304 100644 > --- a/mm/debug_vm_pgtable.c > +++ b/mm/debug_vm_pgtable.c > @@ -102,6 +102,11 @@ static void __init pte_basic_tests(struct pgtable_debug_args *args, int idx) > WARN_ON(pte_write(pte_wrprotect(pte_mkwrite(pte, args->vma)))); > WARN_ON(pte_dirty(pte_wrprotect(pte_mkclean(pte)))); > WARN_ON(!pte_dirty(pte_wrprotect(pte_mkdirty(pte)))); > + > + WARN_ON(pte_dirty(pte_mkwrite_novma(pte_mkclean(pte)))); > + WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte)))); > + WARN_ON(!pte_write(pte_mkwrite_novma(pte_wrprotect(pte)))); > + WARN_ON(pte_write(pte_wrprotect(pte_mkwrite_novma(pte)))); > } > > static void __init pte_advanced_tests(struct pgtable_debug_args *args) > @@ -195,6 +200,9 @@ static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) > WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite(pmd, args->vma)))); > WARN_ON(pmd_dirty(pmd_wrprotect(pmd_mkclean(pmd)))); > WARN_ON(!pmd_dirty(pmd_wrprotect(pmd_mkdirty(pmd)))); > + > + WARN_ON(!pmd_write(pmd_mkwrite_novma(pmd_wrprotect(pmd)))); > + WARN_ON(pmd_write(pmd_wrprotect(pmd_mkwrite_novma(pmd)))); > /* > * A huge page does not point to next level page table > * entry. Hence this must qualify as pmd_bad(). Thanks! I can add a patch for these tests. Or, do you want to work on it? >> >> I cc'ed linux-mm in case we missed anything. If nothing raised, I'll >> queue it next week. --- Best Regards, Huang, Ying