Skip to contentSkip to conversation

arm64, mm: avoid always making PTE dirty in pte_mkwrite()

PATCH Applied Huang Ying 4 participants 7 messages Oct 15, 2025 – Oct 21, 2025

Subsystems (2)
lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 1/1 arm64, mm: avoid always making PTE dirty in pte_mkwrite()

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
 arch/arm64/include/asm/pgtable.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +2 -1
index aa89c2e67ebc..0944e296dd4a 100644
@@ -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;
 }
 
Signature · 1 line
2.39.5
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 48 lines · Huang Ying

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
 arch/arm64/include/asm/pgtable.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +0 -0
index aa89c2e67ebc..0944e296dd4a 100644

arch/arm64/include/asm/pgtable.h static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) · line 299

Quoted 8 lines
arch/arm64/include/asm/pgtable.h +2 -1
@@ -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:

I wonder whether we should also add (as a separate patch):

mm/debug_vm_pgtable.c +1 -0
index 830107b6dd08..df1c552ef11c 100644
@@ -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))));

I cc'ed linux-mm in case we missed anything. If nothing raised, I'll queue it next week.

Thanks.

Signature · 1 line
Catalin
#2 lore raw
Quoted 82 lines · Catalin Marinas
Quoted 48 lines · Huang Ying

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
 arch/arm64/include/asm/pgtable.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +0 -0
index aa89c2e67ebc..0944e296dd4a 100644

arch/arm64/include/asm/pgtable.h static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) · line 299

Quoted 8 lines
arch/arm64/include/asm/pgtable.h +2 -1
@@ -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:

I wonder whether we should also add (as a separate patch):

mm/debug_vm_pgtable.c +1 -0
index 830107b6dd08..df1c552ef11c 100644
@@ -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):

Quoted 1 line
	WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte))));

Adding similar tests to pte_wrprotect().

mm/debug_vm_pgtable.c +8 -0
index 830107b6dd08..573632ebf304 100644
@@ -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().
Quoted 6 lines

I cc'ed linux-mm in case we missed anything. If nothing raised, I'll queue it next week.

Thanks.

#3 lore raw

Hi, Anshuman,

Quoted 104 lines · Anshuman Khandual
Quoted 82 lines · Catalin Marinas
Quoted 48 lines · Huang Ying

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
 arch/arm64/include/asm/pgtable.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +0 -0
index aa89c2e67ebc..0944e296dd4a 100644

arch/arm64/include/asm/pgtable.h static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) · line 299

Quoted 8 lines
arch/arm64/include/asm/pgtable.h +2 -1
@@ -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:

I wonder whether we should also add (as a separate patch):

mm/debug_vm_pgtable.c +1 -0
index 830107b6dd08..df1c552ef11c 100644
@@ -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):

Quoted 1 line
	WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte))));

Adding similar tests to pte_wrprotect().

mm/debug_vm_pgtable.c +5 -0
index 830107b6dd08..573632ebf304 100644
@@ -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)

mm/debug_vm_pgtable.c static void __init pmd_basic_tests(struct pgtable_debug_args *args, int idx) · line 208

Quoted 9 lines
mm/debug_vm_pgtable.c +3 -0
@@ -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?

Quoted 3 lines
Quoted 3 lines

I cc'ed linux-mm in case we missed anything. If nothing raised, I'll queue it next week.

---
Best Regards,
Huang, Ying

#4 lore raw
In reply to Anshuman Khandual
Quoted 116 lines · Anshuman Khandual
Quoted 82 lines · Catalin Marinas
Quoted 48 lines · Huang Ying

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
  arch/arm64/include/asm/pgtable.h | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +0 -0
index aa89c2e67ebc..0944e296dd4a 100644

arch/arm64/include/asm/pgtable.h static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) · line 299

Quoted 8 lines
arch/arm64/include/asm/pgtable.h +2 -1
@@ -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:

I wonder whether we should also add (as a separate patch):

mm/debug_vm_pgtable.c +1 -0
index 830107b6dd08..df1c552ef11c 100644
@@ -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):

Quoted 1 line
	WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte))));

Adding similar tests to pte_wrprotect().

mm/debug_vm_pgtable.c +8 -0
index 830107b6dd08..573632ebf304 100644
@@ -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().
Quoted 1 line
Quoted 2 lines
Quoted 2 lines

I cc'ed linux-mm in case we missed anything. If nothing raised, I'll queue it next week.

Can we please send patches touching mm/debug_vm_pgtable.c properly to linux-mm?

I wrote tools/testing/selftests/mm/mkdirty.c a while ago. I wonder whether that could also be expressed in these tests here.

But my memory comes back: ARCH_HAS_DEBUG_VM_PGTABLE is not set by all architectures (and in particluar not sparc64 which I fixed back then)

Signature · 3 lines
Cheers

David / dhildenb
#5 lore raw
In reply to Catalin Marinas

Hi, Catalin,

Quoted 53 lines · Catalin Marinas
Quoted 52 lines · Huang Ying

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()")
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Gavin Shan <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Yicong Yang <[email protected]>
---
 arch/arm64/include/asm/pgtable.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/pgtable.h +0 -1
index aa89c2e67ebc..0944e296dd4a 100644
@@ -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));
Quoted 8 lines

arch/arm64/include/asm/pgtable.h static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) · line 299

Quoted 4 lines
arch/arm64/include/asm/pgtable.h +2 -0
+	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.

The do_swap_page() change was introduced in June 2024, quite recently.

Quoted 3 lines

For this patch:

Thanks!

Quoted 16 lines

I wonder whether we should also add (as a separate patch):

mm/debug_vm_pgtable.c +1 -0
index 830107b6dd08..df1c552ef11c 100644
@@ -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):

Quoted 1 line
	WARN_ON(!pte_write(pte_mkdirty(pte_mkwrite_novma(pte))));

Sure. Will add another patch for this.

Quoted 2 lines

I cc'ed linux-mm in case we missed anything. If nothing raised, I'll queue it next week.

---
Best Regards,
Huang, Ying

maintainer Applied
#6 lore raw
In reply to Huang Ying
Quoted 9 lines · Huang Ying

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.

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64, mm: avoid always making PTE dirty in pte_mkwrite()

      https://git.kernel.org/arm64/c/143937ca51cc
Signature · 1 line
Catalin