Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796087484; b=5czEsGENj9cnVDtM1KJfgVfzVKGZXvCZHSDGJx3UWTpgRVF//tCJbCkQMiXuzORQHJk6 GIiFKg0Ur9yitbt3eDhdR75CVRio5jfCJhJKoZpGqptMcTMu7GrcRiD516TbDzcM0cBqW C9Au81AqE8qKellb5olH3cT0nKljAIbvbBjbx0PIB50i8o5CzkmgVgqjgCuXFKR8tTa6W otaTSuJhRzHmCZp/f1UOsGmPrKGLvaBVd4zKfTVKLmLwnMnFEmVVOMkaJIiSjCyhvIONt Gz7NdUJbfNHbS3Ll1c8ndu7WR3Zl+U4+gwad752MCQ5G6E4rVeEWFaWBEGRNJxBDciA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796087484; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=2z2IzNEKLrktoqoPPx/YiOeAF1K4qWuVsePgwNXviDc=; b=cfer7ZUSjNeYhkbxlu/gJttjjrncO6+0iolARVcpf9O2BUONr5AC8J6P8xscTDaIFIuM UlQcNlwRCfgIySNu0z2+jIJEKEpQhPRCEUuDE9lYYLN99S/08P9/YeJc8ycvRIfPkiH4J fMUY8XW3oBaUR+sv6sQjAnM5TUFM8sVqD4n5x8af1NvL/3YgdicUvZSenQTZdZj0lHw+4 +JZVxRf1YdRg+lVhmGtFe4HT/43gdvPtxvV+mVVRhDUiqXJtCxouCJ4dKi9Fz0TVmbksa W6SlNLwIgeNqnQ3fH/1hhBxVjWwjja2gjWtACTB9gXnoQIc1HHDJweoPbdRp8CyCMaQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=JbWTmbOk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC5C5606A31A for ; Tue, 1 Dec 2026 01:11:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=JbWTmbOk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76EB91F00897; Tue, 1 Dec 2026 01:11:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796087483; bh=2z2IzNEKLrktoqoPPx/YiOeAF1K4qWuVsePgwNXviDc=; h=Date:To:From:In-Reply-To:References:Subject; b=JbWTmbOkeBSZInUoTKPClRwnohWJxUCwjB6PKJ+uKnEomD4Kb1x+dUU2obOlIybus qa9GbU2OTW8H7CRX0TLpxYahOIFI4C1M4ua1QwTGUx9s8fym6P7vaYKeSVIZPCKkZa l7jqleouqJuJvuqWfKOj7CkJRGyVR3kpStjRqKXY= Date: Mon, 30 Nov 2026 17:11:23 -0800 To: mm-commits@vger.kernel.org,rppt@kernel.org,peterx@redhat.com, david@kernel.org,azpijr@gmail.com,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261129-fixes-uffd-fork-acct-v1-2-f39cc449d92c@kernel.org> References: <20261129-fixes-uffd-fork-acct-v1-2-f39cc449d92c@kernel.org> Subject: + mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch added to mm-new branch Message-Id: <20261201011123.76EB91F00897@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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. [rppt@kernel.org: rewrote the changelog] Link: https://patch.msgid.link/20261129-fixes-uffd-fork-acct-v1-2-f39cc449d92c@kernel.org Fixes: bfe4037e722e ("aio: implement IOCB_CMD_POLL") Signed-off-by: Chris Mason Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Reviewed-by: David Hildenbrand (Arm) Reviewed-by: Jose A. Perez de Azpillaga Cc: Peter Xu mm/userfaultfd.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) --- a/mm/userfaultfd.c~mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock +++ a/mm/userfaultfd.c @@ -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 mason@kernel.org 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