mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>,
	 David Hildenbrand <david@kernel.org>
Cc: Jan Kara <jack@suse.cz>, Jingyi Sun <1045704958@qq.com>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	 Matthew Wilcox <willy@infradead.org>,
	Michal Hocko <mhocko@suse.com>,  Mike Rapoport <rppt@kernel.org>,
	Muhammad Usama Anjum <usama.anjum@arm.com>,
	 Peter Xu <peterx@redhat.com>,
	Suren Baghdasaryan <surenb@google.com>,
	 Vlastimil Babka <vbabka@kernel.org>,
	linux-kernel@vger.kernel.org,  linux-mm@kvack.org,
	nvdimm@lists.linux.dev
Subject: [PATCH 2/3] mm/memory: replace uffd-wp markers in insert_page_into_pte_locked()
Date: Tue, 17 Nov 2026 17:58:33 +0200	[thread overview]
Message-ID: <20261117-uffd-dax-async-wp-fix-v1-2-c9a30f27f452@kernel.org> (raw)
In-Reply-To: <20261117-uffd-dax-async-wp-fix-v1-0-c9a30f27f452@kernel.org>

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


  parent reply	other threads:[~2026-11-17 15:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 15:58 [PATCH 0/3] mm/memory: fix handling of PTE " 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) [this message]
2026-11-17 15:58 ` [PATCH 3/3] mm/memory: don't upgrade non-present PTEs " Mike Rapoport (Microsoft)
2026-11-17 16:22 ` [PATCH 0/3] mm/memory: fix handling of PTE markers " David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117-uffd-dax-async-wp-fix-v1-2-c9a30f27f452@kernel.org \
    --to=rppt@kernel.org \
    --cc=1045704958@qq.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=jack@suse.cz \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=peterx@redhat.com \
    --cc=surenb@google.com \
    --cc=usama.anjum@arm.com \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®