From: Avi Weiss <thnkslprpt@gmail.com>
To: david@kernel.org
Cc: akpm@linux-foundation.org, ljs@kernel.org, rppt@kernel.org,
peterx@redhat.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Avi Weiss <thnkslprpt@gmail.com>
Subject: [PATCH] mm: skip temporary folio allocation for anonymous UFFD missing faults
Date: Thu, 19 Nov 2026 20:00:15 +0200 [thread overview]
Message-ID: <20261119180015.933433-1-thnkslprpt@gmail.com> (raw)
An anonymous write fault on a VMA registered for missing userfaultfd
faults currently allocates, zeroes and memcg-charges a folio before
checking the PTE and handing the fault to userspace. The folio is then
immediately discarded without ever being mapped.
Check the PTE and address-space stability under the PTE lock before
allocating a folio when missing faults are registered. Deliver the fault
to userfaultfd without allocating and discarding a temporary folio.
In a four-boot native B/P/B/P comparison, the mean UFFD fault median
fell from 13,997.75 ns to 13,093.55 ns (-904.2 ns, -6.46%). A guest
allocation trace saw 256 fewer allocations in the faulting task for
256 registered missing faults.
Note: this changes OOM ordering. In a fixed 18 MiB memcg test, the
baseline OOM-killed only the faulting client before delivering a UFFD
event. With this change, the event reached an unprotected shared handler,
and its UFFDIO_COPY allocation could trigger an additional OOM kill of the
handler. A separate surviving client consequently lost its handler and
failed a page-content check after the UFFD registration was released.
The same handler-death mechanism was reproducible on the unmodified
(mainline) kernel when memory pressure was imposed after event delivery
but before UFFDIO_COPY, although the fixed-limit outcomes differed.
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---
mm/memory.c | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 0fb676d..63b39fa 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5547,6 +5547,22 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
ret = vmf_anon_prepare(vmf);
if (ret)
return ret;
+
+ if (unlikely(userfaultfd_missing(vma))) {
+ vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
+ vmf->address, &vmf->ptl);
+ if (!vmf->pte)
+ goto unlock;
+ if (vmf_pte_changed(vmf)) {
+ update_mmu_tlb(vma, vmf->address, vmf->pte);
+ goto unlock;
+ }
+ ret = check_stable_address_space(vma->vm_mm);
+ if (ret)
+ goto unlock;
+ pte_unmap_unlock(vmf->pte, vmf->ptl);
+ return handle_userfault(vmf, VM_UFFD_MISSING);
+ }
/* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */
folio = alloc_anon_folio(vmf);
if (IS_ERR(folio))
@@ -5579,12 +5595,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
if (ret)
goto release;
- /* Deliver the page fault to userland, check inside PT lock */
- if (userfaultfd_missing(vma)) {
- pte_unmap_unlock(vmf->pte, vmf->ptl);
- folio_put(folio);
- return handle_userfault(vmf, VM_UFFD_MISSING);
- }
map_anon_folio_pte_pf(folio, vmf->pte, vma, addr,
vmf_orig_pte_uffd_wp(vmf));
unlock:
--
2.43.0
next reply other threads:[~2026-11-19 18:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 18:00 Avi Weiss [this message]
2026-11-19 18:22 ` Lorenzo Stoakes (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=20261119180015.933433-1-thnkslprpt@gmail.com \
--to=thnkslprpt@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=peterx@redhat.com \
--cc=rppt@kernel.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®