* [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events
@ 2026-11-29 16:25 Mike Rapoport
2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Mike Rapoport @ 2026-11-29 16:25 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Chris Mason, Mike Rapoport, Peter Xu, linux-mm
These are two small fixes for userfaultfd events.
The first patch charges the memory allocated for unmap events to a memory
cgroup, so that these events can't be abused to bypass memcg memory limits.
The second patch fixes a purely theoretical deadlock in the handling of
fork events. So many unlikely things have to happen at the same time that
it's hard to imagine anybody hitting it in real life, but it's easy to fix.
---
Chris Mason (2):
mm/userfaultfd: account memory allocated for unmap events
mm/userfaultfd: drop fork context reference outside event_wqh.lock
mm/userfaultfd.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261129-fixes-uffd-fork-acct-6b94acb0bc25
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events 2026-11-29 16:25 [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Mike Rapoport @ 2026-11-29 16:25 ` Mike Rapoport 2026-11-30 10:45 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 2026-11-29 16:25 ` [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock Mike Rapoport 2026-11-29 17:58 ` [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Jose A. Perez de Azpillaga 2 siblings, 2 replies; 8+ messages in thread From: Mike Rapoport @ 2026-11-29 16:25 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Chris Mason, Mike Rapoport, Peter Xu, linux-mm From: Chris Mason <mason@kernel.org> When memory registered with UFFD_FEATURE_EVENT_UNMAP is unmapped, userfaultfd_unmap_prep() allocates a small structure for each userfaultfd context that should be notified. These structures are usually freed only after the monitor reads the events, so userspace controls how many of them are allocated and how long they stay around. They are allocated with GFP_KERNEL, so this memory is not charged to any memory cgroup and memory cgroup limits don't apply to it. Use GFP_KERNEL_ACCOUNT to charge this memory to the memory cgroup of the task that does the unmap. When the memory cgroup reaches its limit, the allocation can now trigger reclaim or the OOM killer there. If the charge fails, it is handled in the same way as any other failure of this allocation. Fixes: 897ab3e0c49e ("userfaultfd: non-cooperative: add event for memory unmaps") Signed-off-by: Chris Mason <mason@kernel.org> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> [rppt: rewrote the changelog] Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 15ad69ec2fc0..2ed520c36f71 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -3264,7 +3264,7 @@ int userfaultfd_unmap_prep(struct vm_area_struct *vma, unsigned long start, has_unmap_ctx(ctx, unmaps, start, end)) return 0; - unmap_ctx = kzalloc_obj(*unmap_ctx); + unmap_ctx = kzalloc_obj(*unmap_ctx, GFP_KERNEL_ACCOUNT); if (!unmap_ctx) return -ENOMEM; -- 2.53.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events 2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport @ 2026-11-30 10:45 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 1 sibling, 0 replies; 8+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-30 10:45 UTC (permalink / raw) To: Mike Rapoport, Andrew Morton; +Cc: Chris Mason, Peter Xu, linux-mm On 11/29/26 17:25, Mike Rapoport wrote: > From: Chris Mason <mason@kernel.org> > > When memory registered with UFFD_FEATURE_EVENT_UNMAP is unmapped, > userfaultfd_unmap_prep() allocates a small structure for each userfaultfd > context that should be notified. These structures are usually freed only > after the monitor reads the events, so userspace controls how many of them > are allocated and how long they stay around. > > They are allocated with GFP_KERNEL, so this memory is not charged to any > memory cgroup and memory cgroup limits don't apply to it. > > Use GFP_KERNEL_ACCOUNT to charge this memory to the memory cgroup of the > task that does the unmap. > > When the memory cgroup reaches its limit, the allocation can now trigger > reclaim or the OOM killer there. If the charge fails, it is handled in the > same way as any other failure of this allocation. > > Fixes: 897ab3e0c49e ("userfaultfd: non-cooperative: add event for memory unmaps") > Signed-off-by: Chris Mason <mason@kernel.org> > Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> > [rppt: rewrote the changelog] > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> > --- Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events 2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport 2026-11-30 10:45 ` David Hildenbrand (Arm) @ 2026-12-02 23:16 ` Chris Down 1 sibling, 0 replies; 8+ messages in thread From: Chris Down @ 2026-12-02 23:16 UTC (permalink / raw) To: Mike Rapoport Cc: Andrew Morton, David Hildenbrand, Chris Mason, Peter Xu, linux-mm Hi Mike, Thanks, I've confirmed that with munmap()s blocked on unread unmap events, the unmap contexts were not charged to the memcg before this patch and are charged with it. Reviewed-by: Chris Down <chris@chrisdown.name> Tested-by: Chris Down <chris@chrisdown.name> Chris ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock 2026-11-29 16:25 [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Mike Rapoport 2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport @ 2026-11-29 16:25 ` Mike Rapoport 2026-11-30 10:46 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 2026-11-29 17:58 ` [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Jose A. Perez de Azpillaga 2 siblings, 2 replies; 8+ messages in thread From: Mike Rapoport @ 2026-11-29 16:25 UTC (permalink / raw) To: Andrew Morton, David Hildenbrand Cc: Chris Mason, Mike Rapoport, Peter Xu, linux-mm From: Chris Mason <mason@kernel.org> When userfaultfd_ctx_read() handles UFFD_EVENT_FORK, it takes a temporary reference to the userfaultfd context of the child and drops it with event_wqh.lock held and interrupts disabled. If the forking task is killed and userfaultfd_ctx_read() fails to create a file descriptor for the new context, this may be the last reference to that context. The context, in turn, may hold the last reference to the mm_struct of the child. Dropping it also frees the pgd, and x86 takes pgd_lock, which is not IRQ-safe, to remove the pgd from pgd_list. This can deadlock with __split_large_page(), which calls flush_tlb_all() with pgd_lock held. Unless the CPU supports INVLPGB, flush_tlb_all() sends IPIs and waits for all CPUs, but the CPU running userfaultfd_ctx_read() spins on pgd_lock with interrupts disabled. There is also a possible deadlock with aio. When free_ioctx_users() cancels poll requests, it can take fd_wqh.lock in softirq context. event_wqh.lock nests inside fd_wqh.lock, so all the locks taken under event_wqh.lock must be IRQ-safe. The stars have to align for any of this to happen, but it's easy to avoid. Drop the reference after releasing event_wqh.lock, just like userfaultfd_event_wait_completion() does. Fixes: bfe4037e722e ("aio: implement IOCB_CMD_POLL") Signed-off-by: Chris Mason <mason@kernel.org> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> [rppt: rewrote the changelog] Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> --- mm/userfaultfd.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 2ed520c36f71..25c24424a5e0 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -3547,12 +3547,6 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, ret = resolve_userfault_fork(fork_nctx, inode, msg); spin_lock_irq(&ctx->event_wqh.lock); if (!list_empty(&fork_event)) { - /* - * The fork thread didn't abort, so we can - * drop the temporary refcount. - */ - userfaultfd_ctx_put(fork_nctx); - uwq = list_first_entry(&fork_event, typeof(*uwq), wq.entry); @@ -3588,10 +3582,17 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, * it succeeded we'll hold it because the new * uffd references it. */ - if (ret) - userfaultfd_ctx_put(fork_nctx); + if (!ret) + fork_nctx = NULL; } spin_unlock_irq(&ctx->event_wqh.lock); + + /* + * Drop the temporary refcount unlocked, it can be the + * last one and free the child mm. + */ + if (fork_nctx) + userfaultfd_ctx_put(fork_nctx); } return ret; -- 2.53.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock 2026-11-29 16:25 ` [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock Mike Rapoport @ 2026-11-30 10:46 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 1 sibling, 0 replies; 8+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-30 10:46 UTC (permalink / raw) To: Mike Rapoport, Andrew Morton; +Cc: Chris Mason, Peter Xu, linux-mm On 11/29/26 17:25, Mike Rapoport wrote: > From: Chris Mason <mason@kernel.org> > > When userfaultfd_ctx_read() handles UFFD_EVENT_FORK, it takes a temporary > reference to the userfaultfd context of the child and drops it with > event_wqh.lock held and interrupts disabled. > > If the forking task is killed and userfaultfd_ctx_read() fails to create a > file descriptor for the new context, this may be the last reference to that > context. The context, in turn, may hold the last reference to the mm_struct > of the child. Dropping it also frees the pgd, and x86 takes pgd_lock, which > is not IRQ-safe, to remove the pgd from pgd_list. > > This can deadlock with __split_large_page(), which calls flush_tlb_all() > with pgd_lock held. Unless the CPU supports INVLPGB, flush_tlb_all() sends > IPIs and waits for all CPUs, but the CPU running userfaultfd_ctx_read() > spins on pgd_lock with interrupts disabled. > > There is also a possible deadlock with aio. When free_ioctx_users() cancels > poll requests, it can take fd_wqh.lock in softirq context. event_wqh.lock > nests inside fd_wqh.lock, so all the locks taken under event_wqh.lock must > be IRQ-safe. > > The stars have to align for any of this to happen, but it's easy to avoid. > > Drop the reference after releasing event_wqh.lock, just like > userfaultfd_event_wait_completion() does. > > Fixes: bfe4037e722e ("aio: implement IOCB_CMD_POLL") > Signed-off-by: Chris Mason <mason@kernel.org> > Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> > [rppt: rewrote the changelog] > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> > --- Reviewed-by: David Hildenbrand (Arm) <david@kernel.org> -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock 2026-11-29 16:25 ` [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock Mike Rapoport 2026-11-30 10:46 ` David Hildenbrand (Arm) @ 2026-12-02 23:16 ` Chris Down 1 sibling, 0 replies; 8+ messages in thread From: Chris Down @ 2026-12-02 23:16 UTC (permalink / raw) To: Mike Rapoport Cc: Andrew Morton, David Hildenbrand, Chris Mason, Peter Xu, linux-mm Hi Mike, Thanks, I've reproduced this on the old code, with an msleep() added before resolve_userfault_fork() to widen the window. Reviewed-by: Chris Down <chris@chrisdown.name> Tested-by: Chris Down <chris@chrisdown.name> Chris ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events 2026-11-29 16:25 [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Mike Rapoport 2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport 2026-11-29 16:25 ` [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock Mike Rapoport @ 2026-11-29 17:58 ` Jose A. Perez de Azpillaga 2 siblings, 0 replies; 8+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-11-29 17:58 UTC (permalink / raw) To: Mike Rapoport, Andrew Morton, David Hildenbrand Cc: Chris Mason, Peter Xu, linux-mm On 11/29/26 5:25 PM, Mike Rapoport wrote: > These are two small fixes for userfaultfd events. > > The first patch charges the memory allocated for unmap events to a memory > cgroup, so that these events can't be abused to bypass memcg memory limits. > > The second patch fixes a purely theoretical deadlock in the handling of > fork events. So many unlikely things have to happen at the same time that > it's hard to imagine anybody hitting it in real life, but it's easy to fix. > all seems good to me so: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-02 23:16 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-29 16:25 [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Mike Rapoport 2026-11-29 16:25 ` [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events Mike Rapoport 2026-11-30 10:45 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 2026-11-29 16:25 ` [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock Mike Rapoport 2026-11-30 10:46 ` David Hildenbrand (Arm) 2026-12-02 23:16 ` Chris Down 2026-11-29 17:58 ` [PATCH 0/2] mm/userfaultfd: fixes for unmap and fork events Jose A. Perez de Azpillaga
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox