mm/madvise: zap markers from guard regions
Subsystems (4)
Lorenzo Stoakes (ARM)
maintainerPATCH v2 1/1 mm/madvise: zap markers from guard regions
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the ZAP_FLAG_DROP_MARKER flag applied.
Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Since MADV_DONTNEED does something similar, abstract the zapping to zap_range() and call it from both places.
---
v2:
* Updated to just zap everything as per David + Tal.
v1:
https://lore.kernel.org/r/20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org
--- mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
mm/madvise.c +19 -9
@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags) +{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +} +/* * Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about
@@ -872,13 +885,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)static long madvise_dontneed_single_vma (struct madvise_behavior *madv_behavior) { -struct madvise_behavior_range *range = &madv_behavior->range; -struct zap_details details = { - .reclaim_pt = true, - }; - -zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, - range->start, range->end - range->start, &details); +zap_range (madv_behavior,0 );return 0 ; }
@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }/*
@@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :return true;default :return false;
---
base-commit: 4318887a3efa4c53d6f083ccfd7105b809a3ac65
change-id: 20261112-disallow-uffd-guard-install-a5294129ad7b
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
David Hildenbrand (Arm)
maintainerQuoted 75 lines · Lorenzo Stoakes (ARM)
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the
ZAP_FLAG_DROP_MARKERflag applied.Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Since
MADV_DONTNEEDdoes something similar, abstract the zapping tozap_range()and call it from both places.Fixes f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")Reported-by Tal Zussman <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
v2:
* Updated to just zap everything as per David + Tal.v1:
https://lore.kernel.org/r/20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org--- mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)mm/madvise.c +0
-0
mm/madvise.c static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) · line 854
Quoted 5 lines
mm/madvise.c +2
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags)
Two ... ;) But maybe it's consistent like that in the file, didn't check.
Quoted 44 lines
mm/madvise.c +16
-9+{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +} +/* * Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about @@ -872,13 +885,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)static long madvise_dontneed_single_vma (struct madvise_behavior *madv_behavior) { -struct madvise_behavior_range *range = &madv_behavior->range; -struct zap_details details = { - .reclaim_pt = true, - }; - -zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, - range->start, range->end - range->start, &details); +zap_range (madv_behavior,0 );return 0 ; }@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }/*
mm/madvise.c static bool madvise_batch_tlb_flush(int behavior) · line 1857
Quoted 4 lines
mm/madvise.c +1
-0@@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :
Huh why that change? Not spelled out above.
Apart from that LGTM.
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (ARM)
maintainerQuoted 79 lines · David Hildenbrand (Arm)
Quoted 78 lines · Lorenzo Stoakes (ARM)
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the
ZAP_FLAG_DROP_MARKERflag applied.Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Since
MADV_DONTNEEDdoes something similar, abstract the zapping tozap_range()and call it from both places.Fixes f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")Reported-by Tal Zussman <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
v2:
* Updated to just zap everything as per David + Tal.v1:
https://lore.kernel.org/r/20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org--- mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)mm/madvise.c +0
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; }
Quoted 4 lines
mm/madvise.c static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) · line 854
Quoted 2 lines
mm/madvise.c +2
-0+static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags)Two ... ;) But maybe it's consistent like that in the file, didn't check.
I'm a bit exhausted, can you or Andrew just fix up whitespace like this on application?
It'll be easier when I have the VMA tree.
Quoted 43 lines
Quoted 43 lines
mm/madvise.c +16
-9+{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +} +/* * Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about @@ -872,13 +885,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)static long madvise_dontneed_single_vma (struct madvise_behavior *madv_behavior) { -struct madvise_behavior_range *range = &madv_behavior->range; -struct zap_details details = { - .reclaim_pt = true, - }; - -zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, - range->start, range->end - range->start, &details); +zap_range (madv_behavior,0 );return 0 ; }@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }
Quoted 8 lines
mm/madvise.c static bool madvise_batch_tlb_flush(int behavior) · line 1857
Quoted 6 lines
mm/madvise.c +1
-0/* @@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :Huh why that change? Not spelled out above.
It's just the established way madvise interacts with TLB maintenance.
Anything that uses madv_behaviour->tlb has to return true here so madvise_init_tlb() and madvise_finish_tlb() do the right thing.
(I didn't really want to spell out something that's just a code convention in the commit message, esp. when I get feedback about the messages being too long...)
Quoted 7 lines
Apart from that LGTM.
--
Cheers,David
--
Cheers, Lorenzo
Quoted 16 lines
Quoted 11 lines
Quoted 11 lines
mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)mm/madvise.c +0
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; }Quoted 4 lines
mm/madvise.c static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) · line 854
Quoted 2 lines
mm/madvise.c +2
-0+static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags)Two ... ;) But maybe it's consistent like that in the file, didn't check.
Quoted 4 lines
I'm a bit exhausted, can you or Andrew just fix up whitespace like this on application?
It'll be easier when I have the VMA tree.
Yeah, absolutely. I'll let Andrew pick this one.
Quoted 53 lines
Quoted 44 lines
Quoted 44 lines
mm/madvise.c +16
-9+{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +} +/* * Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about @@ -872,13 +885,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)static long madvise_dontneed_single_vma (struct madvise_behavior *madv_behavior) { -struct madvise_behavior_range *range = &madv_behavior->range; -struct zap_details details = { - .reclaim_pt = true, - }; - -zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, - range->start, range->end - range->start, &details); +zap_range (madv_behavior,0 );return 0 ; }@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }Quoted 8 lines
mm/madvise.c static bool madvise_batch_tlb_flush(int behavior) · line 1857
Quoted 6 lines
mm/madvise.c +1
-0/* @@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :Huh why that change? Not spelled out above.
Quoted 4 lines
It's just the established way madvise interacts with TLB maintenance.
Anything that uses
madv_behaviour->tlbhas to return true here somadvise_init_tlb()andmadvise_finish_tlb()do the right thing.
Ohhh, I see now.
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (ARM)
maintainerQuoted 16 lines · David Hildenbrand (Arm)
Quoted 16 lines
Quoted 15 lines
mm/madvise.c static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) · line 854
Quoted 13 lines
mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)mm/madvise.c +2
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags)Two ... ;) But maybe it's consistent like that in the file, didn't check.
Quoted 6 lines
Quoted 4 lines
I'm a bit exhausted, can you or Andrew just fix up whitespace like this on application?
It'll be easier when I have the VMA tree.
Yeah, absolutely. I'll let Andrew pick this one.
Thanks! :)
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
mm/madvise.c static bool madvise_batch_tlb_flush(int behavior) · line 1857
Quoted 6 lines
mm/madvise.c +1
-0/* @@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :Huh why that change? Not spelled out above.
Quoted 8 lines
Quoted 4 lines
It's just the established way madvise interacts with TLB maintenance.
Anything that uses
madv_behaviour->tlbhas to return true here somadvise_init_tlb()andmadvise_finish_tlb()do the right thing.Ohhh, I see now.
Thanks :)
Quoted 5 lines
--
Cheers,David
--
Cheers, Lorenzo
Lorenzo Stoakes (ARM)
maintainerQuoted 2 lines · Lorenzo Stoakes (ARM)
v2:
* Updated to just zap everything as per David + Tal.
Sorry, forgot to mention that I had to drop the tags because of the new approach.
Would appreciate people looking at the v2 fresh :)
Thanks!
Quoted 3 lines
v1:
https://lore.kernel.org/r/20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org
--
Cheers, Lorenzo
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/madvise: zap_range() reclaims PTE tables needed by madvise_guard_install() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/madvise: zap_range() reclaims PTE tables needed by madvise_guard_install()
--
commit b93124c4df6f4f255fb9c4d2510c6ff4b474ec35
Author: Lorenzo Stoakes (ARM) <[email protected]>
mm/madvise: zap markers from guard regions
This commit introduces a zap_range() helper and uses it in madvise_guard_install() with ZAP_FLAG_DROP_MARKER so that existing PTE markers are dropped when zapping a range prior to installing guard markers.
Quoted 20 lines
mm/madvise.c +12
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags) +{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +}
[ ... ]
mm/madvise.c static long madvise_guard_install(struct madvise_behavior *madv_behavior) · line 1232
Quoted 12 lines
mm/madvise.c +4
-2@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }
[Severity: Medium]
Should zap_range() avoid setting details.reclaim_pt = true when called from madvise_guard_install()?
When a guard installation range spans at least a full PMD and contains any mapped page, swap entry, or non-guard marker, zap_range() with ZAP_FLAG_DROP_MARKER clears every PTE in the PMD and reclaims the empty PTE page table via pte_free_tlb(). On the very next iteration of the loop, madvise_guard_install() walks the same range to install PTE_MARKER_GUARD entries and walk_pmd_range() has to allocate a replacement PTE page table via __pte_alloc() while the old table is still queued in madv_behavior->tlb.
Besides the unnecessary page table teardown and reallocation, __pte_alloc() on the retry pass can fail with -ENOMEM under tight memory or memcg limits even when the PTE page tables were already allocated before the zap, leaving any pre-existing guard markers in the zapped range removed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261113-disallow-uffd-guard-install-v2-1-9d0063c70542@kernel.org?part=1
Lorenzo Stoakes (ARM)
maintainerQuoted 63 lines · On Sat, Nov 14, 2026 at 09:05:04PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/madvise:zap_range()reclaims PTE tables needed bymadvise_guard_install()
--commit
b93124c4df6f4f255fb9c4d2510c6ff4b474ec35
Author: Lorenzo Stoakes (ARM) <[email protected]>mm/madvise: zap markers from guard regions
This commit introduces a
zap_range()helper and uses it inmadvise_guard_install()withZAP_FLAG_DROP_MARKERso that existing PTE markers are dropped when zapping a range prior to installing guard markers.Quoted 20 lines
mm/madvise.c +12
-0@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags) +{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +}[ ... ]
mm/madvise.c static long madvise_guard_install(struct madvise_behavior *madv_behavior) · line 1232
Quoted 12 lines
mm/madvise.c +4
-2@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }[Severity: Medium]
Shouldzap_range()avoid setting details.reclaim_pt = true when called frommadvise_guard_install()?When a guard installation range spans at least a full PMD and contains any mapped page, swap entry, or non-guard marker,
zap_range()withZAP_FLAG_DROP_MARKERclears every PTE in the PMD and reclaims the empty PTE page table viapte_free_tlb(). On the very next iteration of the loop,madvise_guard_install()walks the same range to installPTE_MARKER_GUARDentries andwalk_pmd_range()has to allocate a replacement PTE page table via__pte_alloc()while the old table is still queued inmadv_behavior->tlb.
Quoted 4 lines
Besides the unnecessary page table teardown and reallocation,
__pte_alloc()on the retry pass can fail with-ENOMEMunder tight memory or memcg limits even when the PTE page tables were already allocated before the zap, leaving any pre-existing guard markers in the zapped range removed.
This is an edge case and doesn't matter. And fine, reallocate the PTE :) It allows sensible sharing with the MADV_DONTNEED bits.
The ENOMEM stuff is silly, if you're going to fail there you need unreal memory pressure + fatal signal, so failing early is right.
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261113-disallow-uffd-guard-install-v2-1-9d0063c70542@kernel.org?part=1
--
Cheers, Lorenzo
Quoted 53 lines · Lorenzo Stoakes (ARM)
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the
ZAP_FLAG_DROP_MARKERflag applied.Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Since
MADV_DONTNEEDdoes something similar, abstract the zapping tozap_range()and call it from both places.
Quoted 7 lines
Fixes f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")Reported-by Tal Zussman <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Quoted 80 lines
---
v2:
* Updated to just zap everything as per David + Tal.v1:
https://lore.kernel.org/r/20261112-disallow-uffd-guard-install-v1-1-bcf8b1aaa72e@kernel.org--- mm/madvise.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)mm/madvise.c +19
-10@@ -850,6 +850,19 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)return 0 ; } +static void zap_range (struct madvise_behavior *madv_behavior, +zap_flags_t flags) +{ +const struct madvise_behavior_range *range = &madv_behavior->range; +struct zap_details details = { + .reclaim_pt = true, + .zap_flags = flags, + }; + +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details); +} +/* * Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about @@ -872,13 +885,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)static long madvise_dontneed_single_vma (struct madvise_behavior *madv_behavior) { -struct madvise_behavior_range *range = &madv_behavior->range; -struct zap_details details = { - .reclaim_pt = true, - }; - -zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, - range->start, range->end - range->start, &details); +zap_range (madv_behavior,0 );return 0 ; }@@ -1217,9 +1224,11 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)/* * OK some of the range have non-guard pages mapped, zap -* them. This leaves existing guard pages in place. +* them. This also drops any PTE markers in the range, +* including existing guard markers, which we reinstall on +* the next attempt. */ -zap_vma_range (vma, range->start, range->end - range->start); +zap_range (madv_behavior,ZAP_FLAG_DROP_MARKER ); }/* @@ -1845,6 +1854,7 @@ static bool madvise_batch_tlb_flush(int behavior)case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE : +case MADV_GUARD_INSTALL :return true;default :return false; ---base-commit:
4318887a3efa4c53d6f083ccfd7105b809a3ac65
change-id: 20261112-disallow-uffd-guard-install-a5294129ad7bCheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 46 lines · Lorenzo Stoakes (ARM)
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the
ZAP_FLAG_DROP_MARKERflag applied.Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Quoted 7 lines
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Since
MADV_DONTNEEDdoes something similar, abstract the zapping tozap_range()and call it from both places.
Lorenzo Stoakes (ARM)
maintainerQuoted 51 lines · Tal Zussman
Quoted 50 lines · Lorenzo Stoakes (ARM)
In order to establish guard regions, guard markers are installed in the page tables of a mapped VMA.
However, problems arise when one tries to combine this with registering a VMA for userfaultfd, as such operations may themselves need to install page table markers such as uffd-wp.
When the installation is attempted the guard marker prevents these markers being applied, which can lead to unexpected results (especially if the guard regions are later removed.)
This is especially problematic because guard region installation causes the regions to be zapped first, meaning that the removal of userfaultfd-tracked ranges will not be logged in this case.
Additionally, the uffd-wp page table marker will be propagated regardless for file-backed mappings as the zap being performed does not clear these markers.
This is problematic, as it can cause repeated failure to install guard markers resulting in an infinite loop.
Therefore, update guard region installation to invoke the zap with the
ZAP_FLAG_DROP_MARKERflag applied.Both before and after this change, uffd remove events will not trigger on guard region installation, nor will write protect state be propagated after guard region removal.
This makes sense as conceptually, guard regions mark a part of a VMA as effectively unmapped at the page table level, and removing the markers is like mapping the VMA again, over which operation uffd markers would in any case be lost.
A survey of open source users indicates nobody is relying on installing guard regions in uffd-armed VMAs. Software including glibc, android, tcmalloc and CRIU were checked.
This change means that any already established guard regions are removed on zap. These will be overwritten with the newly installed guard markers on success.
However, if this fails for some reason (fatal signal likely the only reasonable cause), guard regions may remain removed, and concurrent faults may succeed in the interim in the range.
Since the installation failing guarantees nothing about the range being installed into, and since success overwrites with guard markers, this isn't an issue.
Quoted 5 lines
Quoted 3 lines
Since
MADV_DONTNEEDdoes something similar, abstract the zapping tozap_range()and call it from both places.Reviewed-by Tal Zussman <[email protected]>Tested-by Tal Zussman <[email protected]>
Thanks! :)
--
Cheers, Lorenzo