Skip to contentSkip to conversation

mm: introduce pmd|pte_needs_soft_dirty_wp helpers and utilize them

PATCH In review Barry Song 2 participants 5 messages Jun 7, 2024 – Jun 10, 2024

Subsystems (5)
lore
Share & tools
lore

Barry Song

author
#0 lore raw

v2 cover mm: introduce pmd|pte_needs_soft_dirty_wp helpers and utilize them

From: Barry Song <[email protected]>

This patchset introduces the pte_need_soft_dirty_wp and pmd_need_soft_dirty_wp helpers to determine if write protection is required for softdirty tracking. These helpers enhance code readability and improve the overall appearance.

They are then utilized in gup, mprotect, swap, and other related functions.

-v2:
* rename "need" to "needs" per David;
* separate the change of do_swap_page() per david;

Thanks to David for his original suggestions on this[1].
[1] https://lore.kernel.org/linux-mm/[email protected]/

Barry Song (2):
mm: introduce pmd|pte_needs_soft_dirty_wp helpers for softdirty
write-protect
mm: set pte writable while pmd_soft_dirty() is true in do_swap_page()

 mm/gup.c         |  4 ++--
 mm/huge_memory.c |  2 +-
 mm/internal.h    | 10 ++++++++++
 mm/memory.c      |  2 +-
 mm/mprotect.c    |  2 +-
 5 files changed, 15 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 mm: introduce pmd|pte_needs_soft_dirty_wp helpers for softdirty write-protect

From: Barry Song <[email protected]>

This patch introduces the pte_needs_soft_dirty_wp and pmd_needs_soft_dirty_wp helpers to determine if write protection is required for softdirty tracking. This can enhance code readability and improve its overall appearance.
These new helpers are then utilized in gup, huge_memory, and mprotect.

---
 mm/gup.c         |  4 ++--
 mm/huge_memory.c |  2 +-
 mm/internal.h    | 10 ++++++++++
 mm/mprotect.c    |  2 +-
 4 files changed, 14 insertions(+), 4 deletions(-)
mm/gup.c +2 -2
index 83e279731d1b..f15756a3646a 100644
@@ -820,7 +820,7 @@ static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
 		return false;
 
 	/* ... and a write-fault isn't required for other reasons. */
-	if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
+	if (pmd_needs_soft_dirty_wp(vma, pmd))
 		return false;
 	return !userfaultfd_huge_pmd_wp(vma, pmd);
 }
@@ -941,7 +941,7 @@ static inline bool can_follow_write_pte(pte_t pte, struct page *page,
 		return false;
 
 	/* ... and a write-fault isn't required for other reasons. */
-	if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
+	if (pte_needs_soft_dirty_wp(vma, pte))
 		return false;
 	return !userfaultfd_pte_wp(vma, pte);
 }
mm/huge_memory.c +1 -1
index 3fbcd77f5957..b29daea9c317 100644
@@ -1625,7 +1625,7 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
 		return false;
 
 	/* Do we need write faults for softdirty tracking? */
-	if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
+	if (pmd_needs_soft_dirty_wp(vma, pmd))
 		return false;
 
 	/* Do we need write faults for uffd-wp tracking? */
mm/internal.h +10 -0
index 12e95fdf61e9..b7853fd57d9a 100644
@@ -1348,6 +1348,16 @@ static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)
 	return !(vma->vm_flags & VM_SOFTDIRTY);
 }
 
+static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd)
+{
+	return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd);
+}
+
+static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte)
+{
+	return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
+}
+
 static inline void vma_iter_config(struct vma_iterator *vmi,
 		unsigned long index, unsigned long last)
 {
mm/mprotect.c +1 -1
index 888ef66468db..222ab434da54 100644
@@ -53,7 +53,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 		return false;
 
 	/* Do we need write faults for softdirty tracking? */
-	if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
+	if (pte_needs_soft_dirty_wp(vma, pte))
 		return false;
 
 	/* Do we need write faults for uffd-wp tracking? */
Signature · 1 line
2.34.1
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] mm: introduce pmd|pte_needs_soft_dirty_wp helpers for softdirty write-protect

Quoted 11 lines · Barry Song

From: Barry Song <[email protected]>

This patch introduces the pte_needs_soft_dirty_wp and pmd_needs_soft_dirty_wp helpers to determine if write protection is required for softdirty tracking. This can enhance code readability and improve its overall appearance.
These new helpers are then utilized in gup, huge_memory, and mprotect.

---

Signature · 3 lines
Cheers,

David / dhildenb
author
#3 lore raw

PATCH v2 2/2 mm: set pte writable while pte_soft_dirty() is true in do_swap_page()

In reply to Barry Song

From: Barry Song <[email protected]>

This patch leverages the new pte_needs_soft_dirty_wp() helper to optimize a scenario where softdirty is enabled, but the softdirty flag has already been set in do_swap_page(). In this situation, we can use pte_mkwrite instead of applying write-protection since we don't depend on write faults.

---
 mm/memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memory.c +1 -1
index db9130488231..a063e489446d 100644
@@ -4322,7 +4322,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 	if (!folio_test_ksm(folio) &&
 	    (exclusive || folio_ref_count(folio) == 1)) {
 		if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
-		    !vma_soft_dirty_enabled(vma)) {
+		    !pte_needs_soft_dirty_wp(vma, pte)) {
 			pte = pte_mkwrite(pte, vma);
 			if (vmf->flags & FAULT_FLAG_WRITE) {
 				pte = pte_mkdirty(pte);
Signature · 1 line
2.34.1
Acked-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] mm: set pte writable while pte_soft_dirty() is true in do_swap_page()

Quoted 19 lines · Barry Song

From: Barry Song <[email protected]>

This patch leverages the new pte_needs_soft_dirty_wp() helper to optimize a scenario where softdirty is enabled, but the softdirty flag has already been set in do_swap_page(). In this situation, we can use pte_mkwrite instead of applying write-protection since we don't depend on write faults.

---
  mm/memory.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/memory.c +0 -0
index db9130488231..a063e489446d 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4328

Quoted 8 lines
mm/memory.c +1 -1
@@ -4322,7 +4322,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
  	if (!folio_test_ksm(folio) &&
  	    (exclusive || folio_ref_count(folio) == 1)) {
  		if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
-		    !vma_soft_dirty_enabled(vma)) {
+		    !pte_needs_soft_dirty_wp(vma, pte)) {
  			pte = pte_mkwrite(pte, vma);
  			if (vmf->flags & FAULT_FLAG_WRITE) {
  				pte = pte_mkdirty(pte);
Signature · 3 lines
Cheers,

David / dhildenb