Re: [PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page()
From: Gregory Price (Meta)
Date: Tue Dec 01 2026 - 08:53:07 EST
On 2026-11-29 11:06 +0100, 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>
Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
--
~Gregory