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 1/3] mm/memory: install new PTEs first in insert_page_into_pte_locked()
Date: Tue, 17 Nov 2026 17:58:32 +0200	[thread overview]
Message-ID: <20261117-uffd-dax-async-wp-fix-v1-1-c9a30f27f452@kernel.org> (raw)
In-Reply-To: <20261117-uffd-dax-async-wp-fix-v1-0-c9a30f27f452@kernel.org>

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


  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 markers " Mike Rapoport (Microsoft)
2026-11-17 15:58 ` Mike Rapoport (Microsoft) [this message]
2026-11-17 15:58 ` [PATCH 2/3] mm/memory: replace uffd-wp " Mike Rapoport (Microsoft)
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-1-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®