[PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page()
From: David Hildenbrand (Arm)
Date: Sun Nov 29 2026 - 05:12:28 EST
From: Chris Mason <mason@xxxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
[ david: minor code cleanups, adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/memory.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/mm/memory.c b/mm/memory.c
index 1bc450cc359ca..7b0aeb102eff7 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5390,10 +5390,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