From: Chris Mason swapoff kills si->users and waits for the last put_swap_device() with wait_for_completion(). do_swap_page() currently holds its reference to the swap device until it returns to the caller. If it ends up calling into do_wp_page() and triggers uffd-wp, we will sleep in handle_userfault() until the user space handler has processed the event and woken up the thread: do_swap_page() swapoff ============== ======= get_swap_device() set_ptes() folio_unlock() do_wp_page() handle_userfault() schedule() try_to_unuse() returns 0 percpu_ref_kill(&p->users) wait_for_completion(&p->comp) put_swap_device() Consequently, swapoff hangs until the user space handler resolves the fault. Let's fix it by dropping the swap device reference before calling do_wp_page(). This has medium urgency, as an unprivileged process can make swapoff hang for as long as it likes, using a UFFD_USER_MODE_ONLY userfaultfd. Fixes: 2799e77529c2 ("swap: fix do_swap_page() race with swapoff") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: minor code cleanups, adjust comment and description ] Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 60e07ab3306bc..ec98704eb2202 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5396,10 +5396,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * drop the marker for a non-exclusive page. */ if ((vmf->flags & FAULT_FLAG_WRITE) && !pte_write(pte) && !rwp_restore) { + /* do_wp_page() may wait for uffd, clean up first. */ + put_swap_device(si); ret |= do_wp_page(vmf); if (ret & VM_FAULT_ERROR) ret &= VM_FAULT_ERROR; - goto out; + return ret; } /* No need to invalidate - it was non-present before */ -- 2.56.1