Re: [PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page()
From: Jose A. Perez de Azpillaga
Date: Sun Nov 29 2026 - 15:50:28 EST
On 11/29/26 11:06 AM, David Hildenbrand (Arm) wrote:
> 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>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
--
cheers,
jose a. p-a