Skip to contentSkip to conversation

mm/userfaultfd: fixes for unmap and fork events

PATCH In review Mike Rapoport 4 participants 8 messages Nov 29 – Dec 1

Subsystems (2)
lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

cover mm/userfaultfd: fixes for unmap and fork events

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.

author maintainer
#1 lore raw

PATCH 1/2 mm/userfaultfd: account memory allocated for unmap events

From: Chris Mason <[email protected]>

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")
[rppt: rewrote the changelog]
---
 mm/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/userfaultfd.c +1 -1
index 15ad69ec2fc0..2ed520c36f71 100644
@@ -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;
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm/userfaultfd: account memory allocated for unmap events

Quoted 18 lines · Mike Rapoport

From: Chris Mason <[email protected]>

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.

Quoted 6 lines
Fixes 897ab3e0c49e ("userfaultfd: non-cooperative: add event for memory unmaps")
[rppt: rewrote the changelog]

---

Signature · 3 lines
Cheers,

David
maintainer tree notice
#3 lore raw

+ mm-userfaultfd-account-memory-allocated-for-unmap-events.patch added to mm-new branch

In reply to Mike Rapoport
The patch titled Subject: mm/userfaultfd: account memory allocated for unmap events has been added to the -mm mm-new branch. Its filename is…

The patch titled
Subject: mm/userfaultfd: account memory allocated for unmap events
has been added to the -mm mm-new branch. Its filename is
mm-userfaultfd-account-memory-allocated-for-unmap-events.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-userfaultfd-account-memory-allocated-for-unmap-events.patch

This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Chris Mason <[email protected]>
Subject: mm/userfaultfd: account memory allocated for unmap events
Date: Sun, 29 Nov 2026 18:25:07 +0200

Patch series "mm/userfaultfd: fixes for unmap and fork events".

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.

This patch (of 2):

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.

 mm/userfaultfd.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/userfaultfd.c renamed from mm/userfaultfd.c~mm-userfaultfd-account-memory-allocated-for-unmap-events +1 -1
@@ -3267,7 +3267,7 @@ int userfaultfd_unmap_prep(struct vm_are
 	    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;
 

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch

author maintainer
#4 lore raw

PATCH 2/2 mm/userfaultfd: drop fork context reference outside event_wqh.lock

In reply to Mike Rapoport

From: Chris Mason <[email protected]>

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")
[rppt: rewrote the changelog]
---
 mm/userfaultfd.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
mm/userfaultfd.c +9 -8
index 2ed520c36f71..25c24424a5e0 100644
@@ -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;
Signature · 1 line
2.53.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] mm/userfaultfd: drop fork context reference outside event_wqh.lock

Quoted 27 lines · Mike Rapoport

From: Chris Mason <[email protected]>

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.

Quoted 6 lines
Fixes bfe4037e722e ("aio: implement IOCB_CMD_POLL")
[rppt: rewrote the changelog]

---

Signature · 3 lines
Cheers,

David
maintainer tree notice
#6 lore raw

+ mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch added to mm-new branch

In reply to Mike Rapoport
The patch titled Subject: mm/userfaultfd: drop fork context reference outside event_wqh.lock has been added to the -mm mm-new branch. Its filename is…

The patch titled
Subject: mm/userfaultfd: drop fork context reference outside event_wqh.lock
has been added to the -mm mm-new branch. Its filename is
mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch

This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Chris Mason <[email protected]>
Subject: mm/userfaultfd: drop fork context reference outside event_wqh.lock
Date: Sun, 29 Nov 2026 18:25:08 +0200

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.

 mm/userfaultfd.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
mm/userfaultfd.c renamed from mm/userfaultfd.c~mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock +9 -8
@@ -3547,12 +3547,6 @@ static ssize_t userfaultfd_ctx_read(stru
 		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(stru
 			 * 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;

_

Patches currently in -mm which might be from [email protected] are

mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch

Reviewed-by on the v1 series
#7 lore raw
In reply to Mike Rapoport
Quoted 9 lines · Mike Rapoport

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:

Signature · 2 lines
cheers,
jose a. p-a