[RESEND RFC PATCH v3 0/2] mm/swap, PM: hibernate: fix swapoff race and optimize swap reference

Youngjun Park
2026-03-12T11:25:24Z

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.

Additionally, grabbing and releasing the swap device reference on every
slot allocation is inefficient across the entire hibernation swap path.

This patch series addresses these issues:
- Patch 1: Fixes the swapoff race in uswsusp by holding the swap device
reference from the point the swap device is looked up.
- Patch 2: Removes the overhead of per-slot reference counting in alloc/free
paths and cleans up the redundant SWP_WRITEOK check.

While this approach seems reasonable and has passed basic testing,
I am not entirely familiar with the snapshot codebase.
Therefore, I am submitting this as an RFC to gather your feedback.
I plan to update it to a formal patch series based on the comments received.

Changelog:
rfc v2 -> rfc v3:
- Split the single patch into 2 patches (Patch 1: race issue, Patch 2:
reference optimization) per Chris Li's feedback.
- Addressed Chris Li's concern regarding the complexity of releasing
references on error paths in normal hibernation. Since normal hibernation
freezes processes (making swapoff impossible), it no longer holds the
reference at all. Instead, uswsusp explicitly acquires the reference,
simplifying the overall fix.
- Removed the SWP_WRITEOK check before allocation, as it is already
handled during cluster allocation.
- Rebased the patch series onto f543926f9d0c3f6dfb354adfe7fbaeedd1277c6b.
- Change the RFC title and modify commit.

rfc v1 -> rfc v2:
- Squashed previous commits into a single patch per Usama Arif's feedback
regarding git bisectability.

Links:
RFC v1: https://lore.kernel.org/linux-mm/20260305202413.1888499-1-usama.arif@linux.dev/T/#m3693d45180f14f441b6951984f4b4bfd90ec0c9d
RFC v2: https://lore.kernel.org/linux-mm/20260306024608.1720991-1-youngjun.park@lge.com/

Thanks,
Youngjun Park

Youngjun Park (2):
mm/swap, PM: hibernate: fix swapoff race in uswsusp by getting swap
reference
mm/swap: remove redundant swap device reference in alloc/free

include/linux/swap.h | 3 +-
kernel/power/swap.c | 2 +-
kernel/power/user.c | 11 +++++--
mm/swapfile.c | 78 +++++++++++++++++++++++---------------------
4 files changed, 52 insertions(+), 42 deletions(-)

--
base-commit: f543926f9d0c3f6dfb354adfe7fbaeedd1277c6b (mm-new)

2.34.1