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: 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) --- 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