FAILED: patch "[PATCH] userfaultfd: gate must_wait writability check on" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 8e80af52db652fbc41320eee45a4f73bc029faf2
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026070239-eject-banking-8632@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 8e80af52db652fbc41320eee45a4f73bc029faf2 Mon Sep 17 00:00:00 2001
From: "Kiryl Shutsemau (Meta)" <[email protected]>
Date: Fri, 29 May 2026 18:23:29 +0100
Subject: [PATCH] userfaultfd: gate must_wait writability check on
pte_present()
userfaultfd_must_wait() and userfaultfd_huge_must_wait() read the PTE without taking the page table lock and then apply pte_write() / huge_pte_write() to it. Those accessors decode bits from the present encoding only; on a swap or migration entry they read the offset bits that happen to share the same position and return an undefined result.
The intent of the check is "is this fault still WP-blocked?". A non-marker swap entry means the page is in transit -- the userfault context the original fault delivered against is no longer the same, and the swap-in or migration completion path will re-deliver a fresh fault if userspace still needs to handle it. Worst case under the current code the garbage write bit says "wait", and the thread stays asleep until a UFFDIO_WAKE that may never arrive.
Gate the writability check on pte_present() so the lockless re-check only inspects present-PTE bits when the entry is actually present. The non-present, non-marker case returns "don't wait" and lets the fault path retry.
mm/userfaultfd.c +20 -0
@@ -2542,6 +2542,15 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,/* UFFD PTE markers require userspace to resolve the fault. */ if (pte_is_uffd_marker (pte))return true; +/* +* Concurrent migration may have replaced the present PTE with a +* non-marker swap entry between fault delivery and this lockless +* re-check. huge_pte_write() on a swap entry decodes random offset +* bits, so gate it on pte_present(). The migration completion path +* will re-deliver the fault if it still needs userspace. +*/ +if (!pte_present (pte)) +return false;/* * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to * resolve the fault.
@@ -2628,6 +2637,17 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,/* UFFD PTE markers require userspace to resolve the fault. */ if (pte_is_uffd_marker (ptent)) goto out; +/* +* Concurrent swap-out / migration may have replaced the present PTE +* with a non-marker swap entry between fault delivery and this +* lockless re-check. pte_write() on a swap entry decodes random +* offset bits, so gate it on pte_present(). The page-in path will +* re-deliver the fault if it still needs userspace. +*/ +if (!pte_present (ptent)) { + ret = false; + goto out; + }/* * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to * resolve the fault.
PATCH 1/1 userfaultfd: gate must_wait writability check on pte_present()
userfaultfd_must_wait() and userfaultfd_huge_must_wait() read the PTE without taking the page table lock and then apply pte_write() / huge_pte_write() to it. Those accessors decode bits from the present encoding only; on a swap or migration entry they read the offset bits that happen to share the same position and return an undefined result.
The intent of the check is "is this fault still WP-blocked?". A non-marker swap entry means the page is in transit -- the userfault context the original fault delivered against is no longer the same, and the swap-in or migration completion path will re-deliver a fresh fault if userspace still needs to handle it. Worst case under the current code the garbage write bit says "wait", and the thread stays asleep until a UFFDIO_WAKE that may never arrive.
Gate the writability check on pte_present() so the lockless re-check only inspects present-PTE bits when the entry is actually present. The non-present, non-marker case returns "don't wait" and lets the fault path retry.
(cherry picked from commit 8e80af52db652fbc41320eee45a4f73bc029faf2) [ kas: apply to fs/userfaultfd.c and fold the pte_present()/
huge_pte_present() gate into the existing writability checks; this tree predates the marker/return-style refactor of these functions ]
--- fs/userfaultfd.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
fs/userfaultfd.c +12 -2
@@ -273,7 +273,12 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx,*/ if (huge_pte_none_mostly (pte)) ret = true; -if (!huge_pte_write (pte) && (reason &VM_UFFD_WP )) +/* +* Gate the writability check on pte_present(): huge_pte_write() on a +* non-present migration entry decodes random offset bits. The +* migration completion path re-delivers the fault if still needed. +*/ +if (pte_present (pte) && !huge_pte_write (pte) && (reason &VM_UFFD_WP )) ret = true; out:return ret;
@@ -355,7 +360,12 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx,*/ if (pte_none_mostly (*pte)) ret = true; -if (!pte_write (*pte) && (reason &VM_UFFD_WP )) +/* +* Gate the writability check on pte_present(): pte_write() on a +* non-present swap/migration entry decodes random offset bits. The +* page-in path re-delivers the fault if it still needs userspace. +*/ +if (pte_present (*pte) && !pte_write (*pte) && (reason &VM_UFFD_WP )) ret = true;pte_unmap (pte);
Signature · 1 line
2.54.0
Re: [PATCH 6.1.y] userfaultfd: gate must_wait writability check on pte_present()
Quoted 5 lines · Kiryl Shutsemau
userfaultfd_must_wait()anduserfaultfd_huge_must_wait()read the PTE without taking the page table lock and then applypte_write()/huge_pte_write()to it. Those accessors decode bits from the present encoding only; on a swap or migration entry they read the offset bits that happen to share the same position and return an undefined result.
Queued for 6.1.y, thanks!
Signature · 2 lines
Thanks, Sasha