Re: [PATCH v4 0/3] mm/swap, PM: hibernate: fix swapoff race and optimize swap

YoungJun Park
2026-03-18T02:16:44Z

On Tue, Mar 17, 2026 at 12:16:28PM -0700, Andrew Morton wrote:
> On Wed, 18 Mar 2026 03:13:15 +0900 Youngjun Park <youngjun.park@lge.com> wrote:
>
> > Currently, in the uswsusp path, only the swap type value is retrieved at
> > lookup time without holding a reference. If swapoff races after the type
> > is acquired, subsequent slot allocations operate on a stale swap device.
>
> Has this race been experienced or at least demonstrated? Or is this
> patchset derived from reading the code?
>
Hello Andrew,

I found and reproduced this race condition
(using a custom test program for verifying reproducibility):

Pre-condition.
- swapon /dev/sdb (intended for hibernation)

Process 1 (test program) Process 2
------------------- ---------
ioctl(SNAPSHOT_SET_SWAP_AREA)
swapoff /dev/sdb
swapon /dev/sdc
ioctl(SNAPSHOT_ALLOC_SWAP_PAGE)

(The same race applies if the swap device is set up at `open` time
via the `resume` parameter.)

Process 1 operates on a different swap device (or fails if no new swap is
enabled), breaking hibernation. The practical impact is that the subsequent
resume operation will fail. While rare in practice, this can be easily
prevented as shown in the patch.

I initially sent this as an RFC because there were no real-world bug reports.
However, after review and testing, I am submitting it as a formal patch
because it has clear benefits: it prevents a potential bug by enforcing the
code's underlying assumption that `swapoff` must not occur after
`SNAPSHOT_SET_SWAP_AREA` (or after `open`), and it removes unnecessary
reference get/put operations.

I also considered, but rejected, these alternatives:
- Enforcing SNAPSHOT_FREEZE first: Breaks existing user-space behavior
(e.g., `s2disk` in `suspend-utils` calls `SNAPSHOT_SET_SWAP_AREA`
before freezing).
- Marking the swap device (e.g., SWP_HIBERNATION) to prevent swapoff:
Using the existing reference counting mechanism is a cleaner approach.

Best regards,
Youngjun Park