From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: "Mike Rapoport (Microsoft)" <rppt@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>,
Tal Zussman <tz2294@columbia.edu>, Peter Xu <peterx@redhat.com>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.12.y] userfaultfd: clear uffd state in child PTEs if uffd is released during fork
Date: Thu, 3 Dec 2026 23:57:15 -0500 [thread overview]
Message-ID: <20261204045648.2693679-1-sashal@kernel.org> (raw)
In-Reply-To: <2026120249-emblaze-hatbox-baf0@gregkh>
From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
[ Upstream commit 111894c595a078d8481db34f3355e8531990668e ]
With UFFD_FEATURE_EVENT_FORK the userfaultfd context is duplicated for the
child process, the child VMAs are registered with the new context, and
uffd protection state from the parent PTEs is copied into the child.
If the parent's userfaultfd is released before the fork event is read,
userfaultfd_release_new() resets the VMA flags but keeps uffd state in the
child PTEs.
This leaves the child's VMAs in an inconsistent state: they are not
registered with userfaultfd but their mappings carry uffd protection
state.
If the child registers the range with userfaultfd again, the stale state
turns into protection the child never asked for.
When a write fault makes such a PTE writable without copying the page, the
uffd bit remains set and page_table_check_pte_flags() warns about it.
Split resolving of the uffd PTE state out of userfaultfd_clear_vma() and
call it from userfaultfd_release_new() before resetting the VMA.
Link: https://lore.kernel.org/20261031-uffd-prot-bits-fork-event-v2-1-c448e57df417@kernel.org
Fixes: f369b07c8614 ("mm/uffd: reset write protection when unregister with wp-mode")
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Tested-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Reviewed-by: Tal Zussman <tz2294@columbia.edu>
Tested-by: Tal Zussman <tz2294@columbia.edu>
Cc: Peter Xu <peterx@redhat.com>
Cc: <stable@vger.kernel.org>
[ sashal: Replaced the change_protection() based clearing, which also handles RWP, with a userfaultfd_clear_ptes() helper that calls uffd_wp_range() for WP-only VMAs, and the flags use vm_flags instead of new_vma_flags. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
mm/userfaultfd.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index f592f92c74079..f6be2a4134b96 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -1918,6 +1918,18 @@ void userfaultfd_reset_ctx(struct vm_area_struct *vma)
userfaultfd_set_ctx(vma, NULL, 0);
}
+/*
+ * Clear uffd state from a WP range. Call this before dropping the
+ * registration, as it needs the VMA flags to know what to resolve.
+ */
+static void userfaultfd_clear_ptes(struct vm_area_struct *vma,
+ unsigned long start, unsigned long end)
+{
+ /* Reset ptes for the whole vma range if wr-protected */
+ if (userfaultfd_wp(vma))
+ uffd_wp_range(vma, start, end - start, false);
+}
+
struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
struct vm_area_struct *prev,
struct vm_area_struct *vma,
@@ -1934,9 +1946,7 @@ struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
if (start == vma->vm_start && end == vma->vm_end)
give_up_on_oom = true;
- /* Reset ptes for the whole vma range if wr-protected */
- if (userfaultfd_wp(vma))
- uffd_wp_range(vma, start, end - start, false);
+ userfaultfd_clear_ptes(vma, start, end);
ret = vma_modify_flags_uffd(vmi, prev, vma, start, end,
vma->vm_flags & ~__VM_UFFD_FLAGS,
@@ -2035,8 +2045,10 @@ void userfaultfd_release_new(struct userfaultfd_ctx *ctx)
/* the various vma->vm_userfaultfd_ctx still points to it */
mmap_write_lock(mm);
for_each_vma(vmi, vma) {
- if (vma->vm_userfaultfd_ctx.ctx == ctx)
+ if (vma->vm_userfaultfd_ctx.ctx == ctx) {
+ userfaultfd_clear_ptes(vma, vma->vm_start, vma->vm_end);
userfaultfd_reset_ctx(vma);
+ }
}
mmap_write_unlock(mm);
}
--
2.53.0
prev parent reply other threads:[~2026-12-04 4:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 13:45 FAILED: patch "[PATCH] userfaultfd: clear uffd state in child PTEs if uffd is" failed to apply to 6.12-stable tree gregkh
2026-12-04 4:57 ` Sasha Levin [this message]
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=20261204045648.2693679-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=azpijr@gmail.com \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tz2294@columbia.edu \
/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