From: Chris Mason 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 Signed-off-by: David Hildenbrand (Arm) [rppt: rewrote the changelog] Signed-off-by: Mike Rapoport (Microsoft) --- 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