Patch 6 optimizes mprotect() by batch clearing the ptes, masking in the new
protections, and batch setting the ptes. Suppose that the first pte
of the batch is writable - with the current implementation of
folio_pte_batch(), it is not guaranteed that the other ptes in the batch
are already writable too, so we may incorrectly end up setting the
writable bit on all ptes via modify_prot_commit_ptes().
Therefore, introduce FPB_RESPECT_WRITE so that all ptes in the batch
are writable or not.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/internal.h | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 5b0f71e5434b..28d2d5b051df 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -208,17 +208,20 @@ typedef int __bitwise fpb_t;
/* Compare PTEs respecting the soft-dirty bit. */
#define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1))
+/* Compare PTEs respecting the writable bit. */
+#define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2))
+
/*
* Merge PTE write bits: if any PTE in the batch is writable, modify the
* PTE at @ptentp to be writable.
*/
-#define FPB_MERGE_WRITE ((__force fpb_t)BIT(2))
+#define FPB_MERGE_WRITE ((__force fpb_t)BIT(3))
/*
* Merge PTE young and dirty bits: if any PTE in the batch is young or dirty,
* modify the PTE at @ptentp to be young or dirty, respectively.
*/
-#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(3))
+#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4))
static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
{
@@ -226,7 +229,9 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
pte = pte_mkclean(pte);
if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY)))
pte = pte_clear_soft_dirty(pte);
- return pte_wrprotect(pte_mkold(pte));
+ if (likely(!(flags & FPB_RESPECT_WRITE)))
+ pte = pte_wrprotect(pte);
+ return pte_mkold(pte);
}
/**
--
2.30.2
On 18 Jul 2025, at 5:02, Dev Jain wrote: > Patch 6 optimizes mprotect() by batch clearing the ptes, masking in the new “Patch 6” might not make sense when reading it in the git log. Something like below might be better: mprotect() will be optimized by batch clearing the ptes, masking in the new protections, and batch setting the ptes in an upcoming commit. No need to repin for this one. > protections, and batch setting the ptes. Suppose that the first pte > of the batch is writable - with the current implementation of > folio_pte_batch(), it is not guaranteed that the other ptes in the batch > are already writable too, so we may incorrectly end up setting the > writable bit on all ptes via modify_prot_commit_ptes(). > > Therefore, introduce FPB_RESPECT_WRITE so that all ptes in the batch > are writable or not. > > Signed-off-by: Dev Jain <dev.jain@arm.com> > --- > mm/internal.h | 11 ++++++++--- > 1 file changed, 8 insertions(+), 3 deletions(-) > LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com> Best Regards, Yan, Zi
On 23/07/25 8:58 pm, Zi Yan wrote: > On 18 Jul 2025, at 5:02, Dev Jain wrote: > >> Patch 6 optimizes mprotect() by batch clearing the ptes, masking in the new > “Patch 6” might not make sense when reading it in the git log. Something like > below might be better: Andrew has fixed that for me :) > > mprotect() will be optimized by batch clearing the ptes, masking in the new > protections, and batch setting the ptes in an upcoming commit. > > No need to repin for this one. > >> protections, and batch setting the ptes. Suppose that the first pte >> of the batch is writable - with the current implementation of >> folio_pte_batch(), it is not guaranteed that the other ptes in the batch >> are already writable too, so we may incorrectly end up setting the >> writable bit on all ptes via modify_prot_commit_ptes(). >> >> Therefore, introduce FPB_RESPECT_WRITE so that all ptes in the batch >> are writable or not. >> >> Signed-off-by: Dev Jain <dev.jain@arm.com> >> --- >> mm/internal.h | 11 ++++++++--- >> 1 file changed, 8 insertions(+), 3 deletions(-) >> > LGTM. Reviewed-by: Zi Yan <ziy@nvidia.com> Thanks. > > Best Regards, > Yan, Zi
On 18/07/2025 10:02, Dev Jain wrote: > Patch 6 optimizes mprotect() by batch clearing the ptes, masking in the new > protections, and batch setting the ptes. Suppose that the first pte > of the batch is writable - with the current implementation of > folio_pte_batch(), it is not guaranteed that the other ptes in the batch > are already writable too, so we may incorrectly end up setting the > writable bit on all ptes via modify_prot_commit_ptes(). > > Therefore, introduce FPB_RESPECT_WRITE so that all ptes in the batch > are writable or not. > > Signed-off-by: Dev Jain <dev.jain@arm.com> Reviewed-by: Ryan Roberts <ryan.roberts@arm.com> > --- > mm/internal.h | 11 ++++++++--- > 1 file changed, 8 insertions(+), 3 deletions(-) > > diff --git a/mm/internal.h b/mm/internal.h > index 5b0f71e5434b..28d2d5b051df 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -208,17 +208,20 @@ typedef int __bitwise fpb_t; > /* Compare PTEs respecting the soft-dirty bit. */ > #define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1)) > > +/* Compare PTEs respecting the writable bit. */ > +#define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2)) > + > /* > * Merge PTE write bits: if any PTE in the batch is writable, modify the > * PTE at @ptentp to be writable. > */ > -#define FPB_MERGE_WRITE ((__force fpb_t)BIT(2)) > +#define FPB_MERGE_WRITE ((__force fpb_t)BIT(3)) > > /* > * Merge PTE young and dirty bits: if any PTE in the batch is young or dirty, > * modify the PTE at @ptentp to be young or dirty, respectively. > */ > -#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(3)) > +#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4)) > > static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) > { > @@ -226,7 +229,9 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) > pte = pte_mkclean(pte); > if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY))) > pte = pte_clear_soft_dirty(pte); > - return pte_wrprotect(pte_mkold(pte)); > + if (likely(!(flags & FPB_RESPECT_WRITE))) > + pte = pte_wrprotect(pte); > + return pte_mkold(pte); > } > > /**
On Fri, Jul 18, 2025 at 02:32:41PM +0530, Dev Jain wrote: > Patch 6 optimizes mprotect() by batch clearing the ptes, masking in the new > protections, and batch setting the ptes. Suppose that the first pte > of the batch is writable - with the current implementation of > folio_pte_batch(), it is not guaranteed that the other ptes in the batch > are already writable too, so we may incorrectly end up setting the > writable bit on all ptes via modify_prot_commit_ptes(). > > Therefore, introduce FPB_RESPECT_WRITE so that all ptes in the batch > are writable or not. > > Signed-off-by: Dev Jain <dev.jain@arm.com> LGTM, so: Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> > --- > mm/internal.h | 11 ++++++++--- > 1 file changed, 8 insertions(+), 3 deletions(-) > > diff --git a/mm/internal.h b/mm/internal.h > index 5b0f71e5434b..28d2d5b051df 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -208,17 +208,20 @@ typedef int __bitwise fpb_t; > /* Compare PTEs respecting the soft-dirty bit. */ > #define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1)) > > +/* Compare PTEs respecting the writable bit. */ > +#define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2)) > + > /* > * Merge PTE write bits: if any PTE in the batch is writable, modify the > * PTE at @ptentp to be writable. > */ > -#define FPB_MERGE_WRITE ((__force fpb_t)BIT(2)) > +#define FPB_MERGE_WRITE ((__force fpb_t)BIT(3)) > > /* > * Merge PTE young and dirty bits: if any PTE in the batch is young or dirty, > * modify the PTE at @ptentp to be young or dirty, respectively. > */ > -#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(3)) > +#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4)) > > static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) > { > @@ -226,7 +229,9 @@ static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags) > pte = pte_mkclean(pte); > if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY))) > pte = pte_clear_soft_dirty(pte); > - return pte_wrprotect(pte_mkold(pte)); > + if (likely(!(flags & FPB_RESPECT_WRITE))) > + pte = pte_wrprotect(pte); > + return pte_mkold(pte); > } > > /** > -- > 2.30.2 >
© 2016 - 2025 Red Hat, Inc.