* [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect
@ 2024-06-06 3:40 Barry Song
2024-06-07 8:46 ` David Hildenbrand
0 siblings, 1 reply; 3+ messages in thread
From: Barry Song @ 2024-06-06 3:40 UTC (permalink / raw)
To: akpm, david, linux-mm
Cc: chrisl, kasong, linux-kernel, minchan, ryan.roberts, surenb,
v-songbaohua, willy
From: Barry Song <v-songbaohua@oppo.com>
This patch introduces the pte_need_soft_dirty_wp and
pmd_need_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 utilized in gup, huge_memory, and protect,
and are particularly applied in do_swap_page() to optimize a
softdirty scenario where mkwrite can still be performed.
Suggested-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Barry Song <v-songbaohua@oppo.com>
---
-v1:
this is suggested by David here:
https://lore.kernel.org/linux-mm/baf84b51-7e8a-4da8-9662-3f5cf14ad6f6@redhat.com/
thanks!
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(-)
diff --git a/mm/gup.c b/mm/gup.c
index 83e279731d1b..756d5416df9c 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -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_need_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_need_soft_dirty_wp(vma, pte))
return false;
return !userfaultfd_pte_wp(vma, pte);
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 3fbcd77f5957..8fbb62f6e491 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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_need_soft_dirty_wp(vma, pmd))
return false;
/* Do we need write faults for uffd-wp tracking? */
diff --git a/mm/internal.h b/mm/internal.h
index 12e95fdf61e9..51551626da68 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -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_need_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_need_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)
{
diff --git a/mm/memory.c b/mm/memory.c
index db9130488231..6307c43796aa 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -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_need_soft_dirty_wp(vma, pte)) {
pte = pte_mkwrite(pte, vma);
if (vmf->flags & FAULT_FLAG_WRITE) {
pte = pte_mkdirty(pte);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 888ef66468db..5aea9ad11ae1 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -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_need_soft_dirty_wp(vma, pte))
return false;
/* Do we need write faults for uffd-wp tracking? */
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect
2024-06-06 3:40 [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect Barry Song
@ 2024-06-07 8:46 ` David Hildenbrand
2024-06-07 9:00 ` Barry Song
0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand @ 2024-06-07 8:46 UTC (permalink / raw)
To: Barry Song, akpm, linux-mm
Cc: chrisl, kasong, linux-kernel, minchan, ryan.roberts, surenb,
v-songbaohua, willy
On 06.06.24 05:40, Barry Song wrote:
> From: Barry Song <v-songbaohua@oppo.com>
>
> This patch introduces the pte_need_soft_dirty_wp and
> pmd_need_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 utilized in gup, huge_memory, and protect,
> and are particularly applied in do_swap_page() to optimize a
> softdirty scenario where mkwrite can still be performed.
[...]
> +static inline bool pmd_need_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_need_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte)
> +{
> + return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
> +}
> +
Should these be "needs" ? I tend to like these names/semantics.
> static inline void vma_iter_config(struct vma_iterator *vmi,
> unsigned long index, unsigned long last)
> {
> diff --git a/mm/memory.c b/mm/memory.c
> index db9130488231..6307c43796aa 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -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_need_soft_dirty_wp(vma, pte)) {
> pte = pte_mkwrite(pte, vma);
I would move that into a separate patch, as it's not a simple conversion.
--
Cheers,
David / dhildenb
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect
2024-06-07 8:46 ` David Hildenbrand
@ 2024-06-07 9:00 ` Barry Song
0 siblings, 0 replies; 3+ messages in thread
From: Barry Song @ 2024-06-07 9:00 UTC (permalink / raw)
To: David Hildenbrand
Cc: akpm, linux-mm, chrisl, kasong, linux-kernel, minchan,
ryan.roberts, surenb, v-songbaohua, willy
On Fri, Jun 7, 2024 at 8:46 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 06.06.24 05:40, Barry Song wrote:
> > From: Barry Song <v-songbaohua@oppo.com>
> >
> > This patch introduces the pte_need_soft_dirty_wp and
> > pmd_need_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 utilized in gup, huge_memory, and protect,
> > and are particularly applied in do_swap_page() to optimize a
> > softdirty scenario where mkwrite can still be performed.
>
> [...]
>
> > +static inline bool pmd_need_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_need_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte)
> > +{
> > + return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
> > +}
> > +
>
> Should these be "needs" ? I tend to like these names/semantics.
yes. "needs" is better. Glad to know you have the common liking
for these names.
>
>
> > static inline void vma_iter_config(struct vma_iterator *vmi,
> > unsigned long index, unsigned long last)
> > {
> > diff --git a/mm/memory.c b/mm/memory.c
> > index db9130488231..6307c43796aa 100644
> > --- a/mm/memory.c
> > +++ b/mm/memory.c
> > @@ -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_need_soft_dirty_wp(vma, pte)) {
> > pte = pte_mkwrite(pte, vma);
>
> I would move that into a separate patch, as it's not a simple conversion.
>
cool. will separate it in v2.
> --
> Cheers,
>
> David / dhildenb
>
Thanks
Barry
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-06-07 9:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06 3:40 [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect Barry Song
2024-06-07 8:46 ` David Hildenbrand
2024-06-07 9:00 ` Barry Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox