* [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked()
@ 2026-11-17 15:58 Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 1/3] mm/memory: install new PTEs first " Mike Rapoport (Microsoft)
` (4 more replies)
0 siblings, 5 replies; 13+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 15:58 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Mike Rapoport, Muhammad Usama Anjum,
Peter Xu, Suren Baghdasaryan, Vlastimil Babka, linux-kernel,
linux-mm, nvdimm
Jingyi reported that a uffd-wp marker in a DAX mapping causes endless
refaults on reads and SIGBUS on writes [1].
The root cause is that insert_page_into_pte_locked() treats every PTE that
is not none as a present mapping. Besides uffd-wp markers, this also
affects guard markers that MADV_GUARD_INSTALL can put into a DAX mapping
concurrently with a write fault.
The first patch restructures insert_page_into_pte_locked() to make room
for the fixes, the second patch fixes the reported issue and the third
makes write faults back off on non-present PTEs.
Both issues were reproduced on ext4 with dax=always on emulated pmem, and
the fixes were verified with these reproducers.
selftests.
[1] https://lore.kernel.org/all/tencent_1874C71D4177A1624528043E008DB1A5EF05@qq.com
---
Mike Rapoport (Microsoft) (3):
mm/memory: install new PTEs first in insert_page_into_pte_locked()
mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
mm/memory.c | 68 +++++++++++++++++++++++++++++++------------------------------
1 file changed, 35 insertions(+), 33 deletions(-)
---
base-commit: 93d7a816a2df3e907e2719bd12b0073f1285fc5c
change-id: 20261117-uffd-dax-async-wp-fix-867acc8dc78f
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 1/3] mm/memory: install new PTEs first in insert_page_into_pte_locked()
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
@ 2026-11-17 15:58 ` Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 2/3] mm/memory: replace uffd-wp markers " Mike Rapoport (Microsoft)
` (3 subsequent siblings)
4 siblings, 0 replies; 13+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 15:58 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Mike Rapoport, Muhammad Usama Anjum,
Peter Xu, Suren Baghdasaryan, Vlastimil Babka, linux-kernel,
linux-mm, nvdimm
insert_page_into_pte_locked() first handles non-empty PTEs and only then
deals with installing a new PTE.
Upcoming fixes need to handle non-empty but non-present entries, like PTE
markers, differently from present mappings and this requires restructuring
of insert_page_into_pte_locked() to avoid extra branching and nesting.
Install a new PTE first and return early, and then handle the existing
mappings.
No functional change.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
mm/memory.c | 65 ++++++++++++++++++++++++++++++-------------------------------
1 file changed, 32 insertions(+), 33 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index fe72e66c9c2e..f927f2241e40 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2453,44 +2453,43 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
struct folio *folio = page_folio(page);
pte_t pteval = ptep_get(pte);
- if (!pte_none(pteval)) {
- if (!mkwrite)
- return -EBUSY;
-
- /*
- * For read faults on private mappings the PFN passed in may
- * not match the PFN we have mapped if the mapped PFN is a
- * writeable COW page. In the mkwrite case we are creating a
- * writable PTE for a shared mapping and we expect the PFNs to
- * match. If they don't match, we are likely racing with block
- * allocation and mapping invalidation.
- */
- if (pte_pfn(pteval) != page_to_pfn(page)) {
- WARN_ON_ONCE(!is_zero_pfn(pte_pfn(pteval)));
- return -EFAULT;
+ if (pte_none(pteval)) {
+ pteval = mk_pte(page, prot);
+ if (unlikely(is_zero_folio(folio))) {
+ pteval = pte_mkspecial(pteval);
+ } else {
+ folio_get(folio);
+ pteval = mk_pte(page, prot);
+ if (mkwrite) {
+ pteval = pte_mkyoung(pteval);
+ pteval = maybe_mkwrite(pte_mkdirty(pteval), vma);
+ }
+ inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
+ folio_add_file_rmap_pte(folio, page, vma);
}
- pteval = maybe_mkwrite(pteval, vma);
- pteval = pte_mkyoung(pteval);
- if (ptep_set_access_flags(vma, addr, pte, pteval, 1))
- update_mmu_cache(vma, addr, pte);
+ set_pte_at(vma->vm_mm, addr, pte, pteval);
return 0;
}
- /* Ok, finally just insert the thing.. */
- pteval = mk_pte(page, prot);
- if (unlikely(is_zero_folio(folio))) {
- pteval = pte_mkspecial(pteval);
- } else {
- folio_get(folio);
- pteval = mk_pte(page, prot);
- if (mkwrite) {
- pteval = pte_mkyoung(pteval);
- pteval = maybe_mkwrite(pte_mkdirty(pteval), vma);
- }
- inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
- folio_add_file_rmap_pte(folio, page, vma);
+ if (!mkwrite)
+ return -EBUSY;
+
+ /*
+ * For read faults on private mappings the PFN passed in may
+ * not match the PFN we have mapped if the mapped PFN is a
+ * writeable COW page. In the mkwrite case we are creating a
+ * writable PTE for a shared mapping and we expect the PFNs to
+ * match. If they don't match, we are likely racing with block
+ * allocation and mapping invalidation.
+ */
+ if (pte_pfn(pteval) != page_to_pfn(page)) {
+ WARN_ON_ONCE(!is_zero_pfn(pte_pfn(pteval)));
+ return -EFAULT;
}
- set_pte_at(vma->vm_mm, addr, pte, pteval);
+ pteval = maybe_mkwrite(pteval, vma);
+ pteval = pte_mkyoung(pteval);
+ if (ptep_set_access_flags(vma, addr, pte, pteval, 1))
+ update_mmu_cache(vma, addr, pte);
return 0;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 2/3] mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 1/3] mm/memory: install new PTEs first " Mike Rapoport (Microsoft)
@ 2026-11-17 15:58 ` Mike Rapoport (Microsoft)
2026-11-18 15:59 ` sashiko-bot
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
` (2 subsequent siblings)
4 siblings, 1 reply; 13+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 15:58 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Mike Rapoport, Muhammad Usama Anjum,
Peter Xu, Suren Baghdasaryan, Vlastimil Babka, linux-kernel,
linux-mm, nvdimm
With UFFD_FEATURE_WP_ASYNC userfaultfd write protection can be enabled
for DAX mappings. Write protecting an empty PTE in such a mapping installs
a uffd-wp PTE marker, and so does zapping a write-protected PTE.
A fault on such a PTE gets to the DAX fault handler via do_pte_missing(),
which eventually calls insert_page_into_pte_locked():
handle_pte_marker()
pte_marker_handle_uffd_wp()
do_pte_missing()
ext4_dax_huge_fault() -> dax_iomap_fault() -> dax_fault_iter()
vmf_insert_page_mkwrite()
insert_page_into_pte_locked()
But insert_page_into_pte_locked() treats the marker as an existing entry.
On a read fault it returns -EBUSY, the marker stays in place and the
access refaults forever.
On a write fault it extracts a PFN from the marker and fails the fault
with SIGBUS:
WARNING: mm/memory.c:2486 at insert_page_into_pte_locked+0x401/0x410, CPU#1: repro/151
RIP: 0010:insert_page_into_pte_locked+0x401/0x410
Call Trace:
<TASK>
insert_page+0x6f/0xc0
vmf_insert_page_mkwrite+0x31/0x60
dax_fault_iter+0x3be/0x5f0
dax_iomap_pte_fault+0x1b8/0x390
ext4_dax_huge_fault+0x1a4/0x2f0
__do_fault+0x34/0xc0
do_fault+0x179/0x610
__handle_mm_fault+0x943/0x1250
handle_mm_fault+0xb4/0x290
do_user_addr_fault+0x15f/0x730
exc_page_fault+0x83/0x190
asm_exc_page_fault+0x26/0x30
Replace uffd-wp markers with the new PTE and keep the page
write-protected by setting the uffd-wp bit in it, like set_pte_range()
does for other file mappings.
Fixes: d61ea1cb0095 ("userfaultfd: UFFD_FEATURE_WP_ASYNC")
Reported-by: Jingyi Sun <1045704958@qq.com>
Closes: https://lore.kernel.org/all/tencent_1874C71D4177A1624528043E008DB1A5EF05@qq.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
mm/memory.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/mm/memory.c b/mm/memory.c
index f927f2241e40..66ad58429967 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2452,8 +2452,9 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
{
struct folio *folio = page_folio(page);
pte_t pteval = ptep_get(pte);
+ bool uffd_wp_marker = pte_is_uffd_wp_marker(pteval);
- if (pte_none(pteval)) {
+ if (pte_none(pteval) || uffd_wp_marker) {
pteval = mk_pte(page, prot);
if (unlikely(is_zero_folio(folio))) {
pteval = pte_mkspecial(pteval);
@@ -2467,6 +2468,8 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
folio_add_file_rmap_pte(folio, page, vma);
}
+ if (uffd_wp_marker && userfaultfd_wp(vma))
+ pteval = pte_mkuffd(pteval);
set_pte_at(vma->vm_mm, addr, pte, pteval);
return 0;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 1/3] mm/memory: install new PTEs first " Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 2/3] mm/memory: replace uffd-wp markers " Mike Rapoport (Microsoft)
@ 2026-11-17 15:58 ` Mike Rapoport (Microsoft)
2026-11-18 10:05 ` David Hildenbrand (Arm)
` (3 more replies)
2026-11-17 16:22 ` [PATCH 0/3] mm/memory: fix handling of PTE markers " David Hildenbrand (Arm)
2026-11-18 14:56 ` (subset) " David Hildenbrand
4 siblings, 4 replies; 13+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-11-17 15:58 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Mike Rapoport, Muhammad Usama Anjum,
Peter Xu, Suren Baghdasaryan, Vlastimil Babka, linux-kernel,
linux-mm, nvdimm
On a write fault insert_page_into_pte_locked() assumes that a non-empty PTE
maps a page, compares its PFN with the page being inserted and upgrades
the PTE to writable.
Since guard regions are allowed in file-backed mappings,
MADV_GUARD_INSTALL can put a guard marker into a PTE of a DAX mapping
after the DAX fault handler found that PTE empty and before it inserts
the page.
vmf_insert_page_mkwrite() then extracts a PFN from the marker, hits the
WARN_ON_ONCE() and fails the fault with SIGBUS instead of the SIGSEGV
expected on access to a guard region:
WARNING: mm/memory.c:2489 at insert_page_into_pte_locked+0x47e/0x490, CPU#3: guard-race/157
RIP: 0010:insert_page_into_pte_locked+0x47e/0x490
Call Trace:
<TASK>
insert_page+0x6f/0xc0
vmf_insert_page_mkwrite+0x31/0x60
dax_fault_iter+0x3be/0x5f0
dax_iomap_pte_fault+0x1b8/0x390
ext4_dax_huge_fault+0x1a4/0x2f0
__do_fault+0x34/0xc0
do_fault+0x179/0x610
__handle_mm_fault+0x8ca/0x1250
handle_mm_fault+0xb4/0x290
do_user_addr_fault+0x15f/0x730
exc_page_fault+0x83/0x190
asm_exc_page_fault+0x26/0x30
Return -EBUSY for non-present PTEs on write faults like it's done for read
faults, so that the fault is retried and handle_pte_marker() deals with
the marker.
Fixes: f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
mm/memory.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/memory.c b/mm/memory.c
index 66ad58429967..a80925c61706 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2474,7 +2474,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
return 0;
}
- if (!mkwrite)
+ if (!mkwrite || !pte_present(pteval))
return -EBUSY;
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked()
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
` (2 preceding siblings ...)
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
@ 2026-11-17 16:22 ` David Hildenbrand (Arm)
2026-11-17 19:31 ` Mike Rapoport
2026-11-18 14:56 ` (subset) " David Hildenbrand
4 siblings, 1 reply; 13+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-17 16:22 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-kernel, linux-mm,
nvdimm
On 11/17/26 16:58, Mike Rapoport (Microsoft) wrote:
> Jingyi reported that a uffd-wp marker in a DAX mapping causes endless
> refaults on reads and SIGBUS on writes [1].
>
> The root cause is that insert_page_into_pte_locked() treats every PTE that
> is not none as a present mapping. Besides uffd-wp markers, this also
> affects guard markers that MADV_GUARD_INSTALL can put into a DAX mapping
> concurrently with a write fault.
>
> The first patch restructures insert_page_into_pte_locked() to make room
> for the fixes, the second patch fixes the reported issue and the third
> makes write faults back off on non-present PTEs.
>
> Both issues were reproduced on ext4 with dax=always on emulated pmem, and
> the fixes were verified with these reproducers.
> selftests.
>
> [1] https://lore.kernel.org/all/tencent_1874C71D4177A1624528043E008DB1A5EF05@qq.com
>
> ---
> Mike Rapoport (Microsoft) (3):
> mm/memory: install new PTEs first in insert_page_into_pte_locked()
> mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
> mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
>
> mm/memory.c | 68 +++++++++++++++++++++++++++++++------------------------------
> 1 file changed, 35 insertions(+), 33 deletions(-)
> ---
> base-commit: 93d7a816a2df3e907e2719bd12b0073f1285fc5c
> change-id: 20261117-uffd-dax-async-wp-fix-867acc8dc78f
>
> --
> Sincerely yours,
> Mike.
I have here a fix to disallow uffd on DAX entirely. It's completely broken.
I was staging it to send with others, but I can send it out separately.
--
Cheers,
David
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked()
2026-11-17 16:22 ` [PATCH 0/3] mm/memory: fix handling of PTE markers " David Hildenbrand (Arm)
@ 2026-11-17 19:31 ` Mike Rapoport
2026-11-17 20:19 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 13+ messages in thread
From: Mike Rapoport @ 2026-11-17 19:31 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Andrew Morton, Jan Kara, Jingyi Sun, Liam R. Howlett,
Lorenzo Stoakes, Matthew Wilcox, Michal Hocko,
Muhammad Usama Anjum, Peter Xu, Suren Baghdasaryan,
Vlastimil Babka, linux-kernel, linux-mm, nvdimm
On Tue, Nov 17, 2026 at 05:22:09PM +0100, David Hildenbrand (Arm) wrote:
> On 11/17/26 16:58, Mike Rapoport (Microsoft) wrote:
> > Jingyi reported that a uffd-wp marker in a DAX mapping causes endless
> > refaults on reads and SIGBUS on writes [1].
> >
> > The root cause is that insert_page_into_pte_locked() treats every PTE that
> > is not none as a present mapping. Besides uffd-wp markers, this also
> > affects guard markers that MADV_GUARD_INSTALL can put into a DAX mapping
> > concurrently with a write fault.
> >
> > The first patch restructures insert_page_into_pte_locked() to make room
> > for the fixes, the second patch fixes the reported issue and the third
> > makes write faults back off on non-present PTEs.
> >
> > Both issues were reproduced on ext4 with dax=always on emulated pmem, and
> > the fixes were verified with these reproducers.
> > selftests.
> >
> > [1] https://lore.kernel.org/all/tencent_1874C71D4177A1624528043E008DB1A5EF05@qq.com
> >
> > ---
> > Mike Rapoport (Microsoft) (3):
> > mm/memory: install new PTEs first in insert_page_into_pte_locked()
> > mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
> > mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
> >
> > mm/memory.c | 68 +++++++++++++++++++++++++++++++------------------------------
> > 1 file changed, 35 insertions(+), 33 deletions(-)
> > ---
> > base-commit: 93d7a816a2df3e907e2719bd12b0073f1285fc5c
> > change-id: 20261117-uffd-dax-async-wp-fix-867acc8dc78f
> >
> > --
> > Sincerely yours,
> > Mike.
>
>
> I have here a fix to disallow uffd on DAX entirely. It's completely broken.
>
> I was staging it to send with others, but I can send it out separately.
Nah, no need.
But patch 3/3 here is for MADV_GUARD_INSTALL on DAX unrelated to uffd.
> --
> Cheers,
>
> David
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked()
2026-11-17 19:31 ` Mike Rapoport
@ 2026-11-17 20:19 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 13+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-17 20:19 UTC (permalink / raw)
To: Mike Rapoport
Cc: Andrew Morton, Jan Kara, Jingyi Sun, Liam R. Howlett,
Lorenzo Stoakes, Matthew Wilcox, Michal Hocko,
Muhammad Usama Anjum, Peter Xu, Suren Baghdasaryan,
Vlastimil Babka, linux-kernel, linux-mm, nvdimm
On 11/17/26 20:31, Mike Rapoport wrote:
> On Tue, Nov 17, 2026 at 05:22:09PM +0100, David Hildenbrand (Arm) wrote:
>> On 11/17/26 16:58, Mike Rapoport (Microsoft) wrote:
>>> Jingyi reported that a uffd-wp marker in a DAX mapping causes endless
>>> refaults on reads and SIGBUS on writes [1].
>>>
>>> The root cause is that insert_page_into_pte_locked() treats every PTE that
>>> is not none as a present mapping. Besides uffd-wp markers, this also
>>> affects guard markers that MADV_GUARD_INSTALL can put into a DAX mapping
>>> concurrently with a write fault.
>>>
>>> The first patch restructures insert_page_into_pte_locked() to make room
>>> for the fixes, the second patch fixes the reported issue and the third
>>> makes write faults back off on non-present PTEs.
>>>
>>> Both issues were reproduced on ext4 with dax=always on emulated pmem, and
>>> the fixes were verified with these reproducers.
>>> selftests.
>>>
>>> [1] https://lore.kernel.org/all/tencent_1874C71D4177A1624528043E008DB1A5EF05@qq.com
>>>
>>> ---
>>> Mike Rapoport (Microsoft) (3):
>>> mm/memory: install new PTEs first in insert_page_into_pte_locked()
>>> mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
>>> mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
>>>
>>> mm/memory.c | 68 +++++++++++++++++++++++++++++++------------------------------
>>> 1 file changed, 35 insertions(+), 33 deletions(-)
>>> ---
>>> base-commit: 93d7a816a2df3e907e2719bd12b0073f1285fc5c
>>> change-id: 20261117-uffd-dax-async-wp-fix-867acc8dc78f
>>>
>>> --
>>> Sincerely yours,
>>> Mike.
>>
>>
>> I have here a fix to disallow uffd on DAX entirely. It's completely broken.
>>
>> I was staging it to send with others, but I can send it out separately.
>
> Nah, no need.
>
> But patch 3/3 here is for MADV_GUARD_INSTALL on DAX unrelated to uffd.
Let me take a look at that tomorrow. But for 1 and 2 we should just reject uffd
registration.
--
Cheers,
David
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
@ 2026-11-18 10:05 ` David Hildenbrand (Arm)
2026-11-18 14:27 ` Lorenzo Stoakes (ARM)
` (2 subsequent siblings)
3 siblings, 0 replies; 13+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 10:05 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-kernel, linux-mm,
nvdimm
On 11/17/26 16:58, Mike Rapoport (Microsoft) wrote:
> On a write fault insert_page_into_pte_locked() assumes that a non-empty PTE
> maps a page, compares its PFN with the page being inserted and upgrades
> the PTE to writable.
>
> Since guard regions are allowed in file-backed mappings,
> MADV_GUARD_INSTALL can put a guard marker into a PTE of a DAX mapping
> after the DAX fault handler found that PTE empty and before it inserts
> the page.
>
> vmf_insert_page_mkwrite() then extracts a PFN from the marker, hits the
> WARN_ON_ONCE() and fails the fault with SIGBUS instead of the SIGSEGV
> expected on access to a guard region:
>
> WARNING: mm/memory.c:2489 at insert_page_into_pte_locked+0x47e/0x490, CPU#3: guard-race/157
> RIP: 0010:insert_page_into_pte_locked+0x47e/0x490
> Call Trace:
> <TASK>
> insert_page+0x6f/0xc0
> vmf_insert_page_mkwrite+0x31/0x60
> dax_fault_iter+0x3be/0x5f0
> dax_iomap_pte_fault+0x1b8/0x390
> ext4_dax_huge_fault+0x1a4/0x2f0
> __do_fault+0x34/0xc0
> do_fault+0x179/0x610
> __handle_mm_fault+0x8ca/0x1250
> handle_mm_fault+0xb4/0x290
> do_user_addr_fault+0x15f/0x730
> exc_page_fault+0x83/0x190
> asm_exc_page_fault+0x26/0x30
>
> Return -EBUSY for non-present PTEs on write faults like it's done for read
> faults, so that the fault is retried and handle_pte_marker() deals with
> the marker.
>
> Fixes: f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 66ad58429967..a80925c61706 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2474,7 +2474,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> return 0;
> }
>
> - if (!mkwrite)
> + if (!mkwrite || !pte_present(pteval))
> return -EBUSY;
>
> /*
>
Should we CC stable?
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
I'll pick this up.
--
Cheers,
David
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
2026-11-18 10:05 ` David Hildenbrand (Arm)
@ 2026-11-18 14:27 ` Lorenzo Stoakes (ARM)
2026-11-18 14:47 ` David Hildenbrand (Arm)
2026-11-18 15:59 ` sashiko-bot
3 siblings, 0 replies; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-18 14:27 UTC (permalink / raw)
To: Mike Rapoport (Microsoft)
Cc: Andrew Morton, David Hildenbrand, Jan Kara, Jingyi Sun,
Liam R. Howlett, Matthew Wilcox, Michal Hocko,
Muhammad Usama Anjum, Peter Xu, Suren Baghdasaryan,
Vlastimil Babka, linux-kernel, linux-mm, nvdimm
On Tue, Nov 17, 2026 at 05:58:34PM +0200, Mike Rapoport (Microsoft) wrote:
> On a write fault insert_page_into_pte_locked() assumes that a non-empty PTE
> maps a page, compares its PFN with the page being inserted and upgrades
> the PTE to writable.
>
> Since guard regions are allowed in file-backed mappings,
> MADV_GUARD_INSTALL can put a guard marker into a PTE of a DAX mapping
> after the DAX fault handler found that PTE empty and before it inserts
> the page.
>
> vmf_insert_page_mkwrite() then extracts a PFN from the marker, hits the
> WARN_ON_ONCE() and fails the fault with SIGBUS instead of the SIGSEGV
> expected on access to a guard region:
>
> WARNING: mm/memory.c:2489 at insert_page_into_pte_locked+0x47e/0x490, CPU#3: guard-race/157
> RIP: 0010:insert_page_into_pte_locked+0x47e/0x490
> Call Trace:
> <TASK>
> insert_page+0x6f/0xc0
> vmf_insert_page_mkwrite+0x31/0x60
> dax_fault_iter+0x3be/0x5f0
> dax_iomap_pte_fault+0x1b8/0x390
> ext4_dax_huge_fault+0x1a4/0x2f0
> __do_fault+0x34/0xc0
> do_fault+0x179/0x610
> __handle_mm_fault+0x8ca/0x1250
> handle_mm_fault+0xb4/0x290
> do_user_addr_fault+0x15f/0x730
> exc_page_fault+0x83/0x190
> asm_exc_page_fault+0x26/0x30
>
> Return -EBUSY for non-present PTEs on write faults like it's done for read
> faults, so that the fault is retried and handle_pte_marker() deals with
> the marker.
>
> Fixes: f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Seems similar to some of my recent work :) what a hive of alien facehuggers
insert_page() and friends are...
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 66ad58429967..a80925c61706 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2474,7 +2474,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> return 0;
> }
>
> - if (!mkwrite)
> + if (!mkwrite || !pte_present(pteval))
> return -EBUSY;
>
> /*
>
> --
> 2.53.0
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
2026-11-18 10:05 ` David Hildenbrand (Arm)
2026-11-18 14:27 ` Lorenzo Stoakes (ARM)
@ 2026-11-18 14:47 ` David Hildenbrand (Arm)
2026-11-18 15:59 ` sashiko-bot
3 siblings, 0 replies; 13+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 14:47 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-kernel, linux-mm,
nvdimm
On 11/17/26 16:58, Mike Rapoport (Microsoft) wrote:
> On a write fault insert_page_into_pte_locked() assumes that a non-empty PTE
> maps a page, compares its PFN with the page being inserted and upgrades
> the PTE to writable.
>
> Since guard regions are allowed in file-backed mappings,
> MADV_GUARD_INSTALL can put a guard marker into a PTE of a DAX mapping
> after the DAX fault handler found that PTE empty and before it inserts
> the page.
>
> vmf_insert_page_mkwrite() then extracts a PFN from the marker, hits the
> WARN_ON_ONCE() and fails the fault with SIGBUS instead of the SIGSEGV
> expected on access to a guard region:
>
> WARNING: mm/memory.c:2489 at insert_page_into_pte_locked+0x47e/0x490, CPU#3: guard-race/157
> RIP: 0010:insert_page_into_pte_locked+0x47e/0x490
> Call Trace:
> <TASK>
> insert_page+0x6f/0xc0
> vmf_insert_page_mkwrite+0x31/0x60
> dax_fault_iter+0x3be/0x5f0
> dax_iomap_pte_fault+0x1b8/0x390
> ext4_dax_huge_fault+0x1a4/0x2f0
> __do_fault+0x34/0xc0
> do_fault+0x179/0x610
> __handle_mm_fault+0x8ca/0x1250
> handle_mm_fault+0xb4/0x290
> do_user_addr_fault+0x15f/0x730
> exc_page_fault+0x83/0x190
> asm_exc_page_fault+0x26/0x30
>
> Return -EBUSY for non-present PTEs on write faults like it's done for read
> faults, so that the fault is retried and handle_pte_marker() deals with
> the marker.
>
> Fixes: f807123d578d ("mm: allow guard regions in file-backed and read-only mappings")
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 66ad58429967..a80925c61706 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2474,7 +2474,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> return 0;
> }
>
> - if (!mkwrite)
> + if (!mkwrite || !pte_present(pteval))
> return -EBUSY;
>
> /*
>
Ugh, so this depends on some changes in Andrew's tree that should not be there ...
Let me figure this out.
--
Cheers,
David
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: (subset) [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked()
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
` (3 preceding siblings ...)
2026-11-17 16:22 ` [PATCH 0/3] mm/memory: fix handling of PTE markers " David Hildenbrand (Arm)
@ 2026-11-18 14:56 ` David Hildenbrand
4 siblings, 0 replies; 13+ messages in thread
From: David Hildenbrand @ 2026-11-18 14:56 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Mike Rapoport (Microsoft)
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-kernel, linux-mm,
nvdimm
On Tue, 17 Nov 2026 17:58:31 +0200, Mike Rapoport (Microsoft) wrote:
> Jingyi reported that a uffd-wp marker in a DAX mapping causes endless
> refaults on reads and SIGBUS on writes [1].
>
> The root cause is that insert_page_into_pte_locked() treats every PTE that
> is not none as a present mapping. Besides uffd-wp markers, this also
> affects guard markers that MADV_GUARD_INSTALL can put into a DAX mapping
> concurrently with a write fault.
>
> [...]
Applied, thanks!
tree: mm/core.git
branch: core-7.5.misc
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.
Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.
[3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
https://git.kernel.org/mm/core/c/3fa9917a
--
Cheers,
David
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/3] mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
2026-11-17 15:58 ` [PATCH 2/3] mm/memory: replace uffd-wp markers " Mike Rapoport (Microsoft)
@ 2026-11-18 15:59 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-18 15:59 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-mm, nvdimm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: uffd-wp bit not checked on mkwrite in
insert_page_into_pte_locked()
--
commit 6bd6838d9e7f98f448f46bdb05cc4400045f49f7
Author: Mike Rapoport (Microsoft) <rppt@kernel.org>
mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
This commit updates insert_page_into_pte_locked() to replace uffd-wp PTE
markers with a new PTE that preserves the uffd-wp bit when handling faults
on DAX mappings.
> diff --git a/mm/memory.c b/mm/memory.c
> index f927f2241e404..66ad58429967e 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2452,8 +2452,9 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> {
> struct folio *folio = page_folio(page);
> pte_t pteval = ptep_get(pte);
> + bool uffd_wp_marker = pte_is_uffd_wp_marker(pteval);
>
> - if (pte_none(pteval)) {
> + if (pte_none(pteval) || uffd_wp_marker) {
> pteval = mk_pte(page, prot);
> if (unlikely(is_zero_folio(folio))) {
> pteval = pte_mkspecial(pteval);
> @@ -2467,6 +2468,8 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
> folio_add_file_rmap_pte(folio, page, vma);
> }
> + if (uffd_wp_marker && userfaultfd_wp(vma))
> + pteval = pte_mkuffd(pteval);
> set_pte_at(vma->vm_mm, addr, pte, pteval);
> return 0;
> }
[Severity: Medium]
Can a concurrent write fault in insert_page_into_pte_locked() mark a
present uffd-wp PTE writable without clearing the uffd-wp bit?
DAX fault handlers serialize on the DAX mapping entry lock in
grab_mapping_entry() and only acquire the page table lock inside
vmf_insert_page_mkwrite() -> insert_page() ->
insert_page_into_pte_locked(). If a read fault and a write fault race on
the same uffd-wp PTE marker:
CPU0 (read fault) CPU1 (write fault, mkwrite = true)
dax_iomap_pte_fault() dax_iomap_pte_fault()
grab_mapping_entry()
dax_fault_iter()
vmf_insert_page()
insert_page_into_pte_locked()
/* replaces marker with
* present RO uffd-wp PTE
*/
pteval = pte_mkuffd(pteval)
set_pte_at(..., pteval)
dax_unlock_entry()
grab_mapping_entry()
dax_fault_iter()
vmf_insert_page_mkwrite()
insert_page_into_pte_locked()
/* pte_none() and uffd_wp_marker
* are both false
*/
pteval = maybe_mkwrite(pteval, vma)
ptep_set_access_flags(..., pteval, 1)
<-- pte_write() && pte_uffd()
In the existing-PTE path of insert_page_into_pte_locked(), maybe_mkwrite()
sets the writable bit without checking userfaultfd_pte_wp(vma, pteval):
mm/memory.c:insert_page_into_pte_locked() {
...
if (pte_pfn(pteval) != page_to_pfn(page)) {
WARN_ON_ONCE(!is_zero_pfn(pte_pfn(pteval)));
return -EFAULT;
}
pteval = maybe_mkwrite(pteval, vma);
pteval = pte_mkyoung(pteval);
if (ptep_set_access_flags(vma, addr, pte, pteval, 1))
update_mmu_cache(vma, addr, pte);
}
This leaves the present PTE with both pte_write() and pte_uffd() set,
which triggers WARN_ON_ONCE(pte_uffd(pte) && pte_write(pte)) in
page_table_check_pte_flags() when CONFIG_PAGE_TABLE_CHECK is enabled and
breaks UFFD_FEATURE_WP_ASYNC write tracking because hardware writes no
longer trap into do_wp_page() while pagemap_scan sees pte_uffd() still
set.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-uffd-dax-async-wp-fix-v1-0-c9a30f27f452@kernel.org?part=2
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 3/3] mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
` (2 preceding siblings ...)
2026-11-18 14:47 ` David Hildenbrand (Arm)
@ 2026-11-18 15:59 ` sashiko-bot
3 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-18 15:59 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Andrew Morton, David Hildenbrand
Cc: Jan Kara, Jingyi Sun, Liam R. Howlett, Lorenzo Stoakes,
Matthew Wilcox, Michal Hocko, Muhammad Usama Anjum, Peter Xu,
Suren Baghdasaryan, Vlastimil Babka, linux-mm, nvdimm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/memory: insert_page_into_pte_locked() upgrades uffd-wp
PTEs to writable
--
commit 62002bc33a7233bf10217f5cd96cf7d78e384960
Author: Mike Rapoport (Microsoft) <rppt@kernel.org>
mm/memory: don't upgrade non-present PTEs in insert_page_into_pte_locked()
This commit changes insert_page_into_pte_locked() to return -EBUSY when
encountering a non-present PTE on a write fault so that PTE markers are
retried through handle_pte_marker() instead of being upgraded in place.
> diff --git a/mm/memory.c b/mm/memory.c
> index 66ad58429967e..a80925c61706d 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -2474,7 +2474,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
> return 0;
> }
>
> - if (!mkwrite)
> + if (!mkwrite || !pte_present(pteval))
> return -EBUSY;
[Severity: Medium]
Can this still upgrade a present PTE that is write-protected by
userfaultfd-wp in place?
When two threads fault concurrently on a PTE_MARKER_UFFD_WP entry in a
shared DAX mapping, the first fault installs a present read-only PTE with
pte_mkuffd() set, and the second write fault then sees a present PTE and
upgrades it with maybe_mkwrite():
CPU0 (read fault) CPU1 (write fault, mkwrite = true)
dax_iomap_pte_fault() dax_iomap_pte_fault()
vmf_insert_page() vmf_insert_page_mkwrite()
insert_page_into_pte_locked()
/* uffd_wp_marker is true */
pteval = pte_mkuffd(pteval)
set_pte_at(..., pteval)
insert_page_into_pte_locked()
/* pte_present(pteval) is true */
pteval = maybe_mkwrite(pteval, vma)
ptep_set_access_flags(..., pteval, 1)
<-- pte_uffd() && pte_write()
Because maybe_mkwrite() sets the writable bit without clearing the uffd-wp
bit, this installs a present PTE with both pte_uffd() and pte_write() set.
That triggers the WARN_ON_ONCE(pte_uffd(pte) && pte_write(pte)) in
page_table_check_pte_flags() and allows subsequent writes to bypass
do_wp_page(), losing userfaultfd-wp write tracking.
Should this check also return -EBUSY when userfaultfd_pte_wp(vma, pteval)
is true so the fault retries through do_wp_page()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-uffd-dax-async-wp-fix-v1-0-c9a30f27f452@kernel.org?part=3
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2026-11-18 15:59 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE markers in insert_page_into_pte_locked() Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 1/3] mm/memory: install new PTEs first " Mike Rapoport (Microsoft)
2026-11-17 15:58 ` [PATCH 2/3] mm/memory: replace uffd-wp markers " Mike Rapoport (Microsoft)
2026-11-18 15:59 ` sashiko-bot
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
2026-11-18 10:05 ` David Hildenbrand (Arm)
2026-11-18 14:27 ` Lorenzo Stoakes (ARM)
2026-11-18 14:47 ` David Hildenbrand (Arm)
2026-11-18 15:59 ` sashiko-bot
2026-11-17 16:22 ` [PATCH 0/3] mm/memory: fix handling of PTE markers " David Hildenbrand (Arm)
2026-11-17 19:31 ` Mike Rapoport
2026-11-17 20:19 ` David Hildenbrand (Arm)
2026-11-18 14:56 ` (subset) " David Hildenbrand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox