Skip to content

mm/swap, PM: hibernate: fix swapoff race in uswsusp by pinning swap device

Youngjun Park linux-mm linux-pm 6 versions – Applied

Status from this message: [merged mm-stable] mm-swap-remove-redundant-swap-device-reference-in-alloc-free.patch removed from -mm tree

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest2 patches 6 repliesApplied 2/2
  2. v72 patches 4 repliesSuperseded
  3. v63 patches 6 repliesSuperseded
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v53 patches4 repliesSuperseded
v43 patches6 repliesSuperseded
v3 RFC2 patches2 repliesSuperseded

Changes in v8 from the cover letter

- Rebased onto mm-new per Andrew Morton's suggestion.
- Clarified function comments (SWP_HIBERNATION pinning).
- Re-tested and AI-reviewed on mm-new; no new issues found.

Patches in v8 All 2 applied

#Subject and filesSizeRepliesApplied
1/2mm/swap, PM: hibernate: fix swapoff race in uswsusp by pinning swap deviceinclude/linux/swap.h kernel/power/swap.c kernel/power/user.c +1 more+136 -212 repliesApplied
2/2mm/swap: remove redundant swap device reference in alloc/freemm/swapfile.c+36 -321 replyApplied

Reviews trailers per patch

All 2 patches have a Reviewed-by or Acked-by.

PatchKairui Song
1/2R-b
2/2R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v8 thread

Cover letter [PATCH v8 0/2] mm/swap, PM: hibernate: fix swapoff race in uswsusp by pinning swap device

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 pinning the swap device from the point it is looked up until the session completes.
- Patch 2: Removes the overhead of per-slot reference counting in alloc/free paths and cleans up the redundant SWP_WRITEOK check.

Rebased onto mm-new per Andrew's suggestion [1]. The si->flags race flagged by AI review in v7 (between SWP_HIBERNATION and cont_lock in add_swap_count_continuation) and the proposed fixes discussed there (atomic ops for si->flags, or serializing with swap_lock) are all moot on mm-new since Kairui's series removed that code path entirely. kernel/power/ changes are small, so Andrew proposed carrying everything through mm-new.

Rafael, could you ack the PM-side changes?

Re-tested on mm-new (c51ea78c5466) with hibernate/resume cycles and uswsusp paths. Also ran an additional round of AI review against the rebased version, no new issues found.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Links:
RFC v1: https://lore.kernel.org/linux-mm/[email protected]/T/#m3693d45180f14f441b6951984f4b4bfd90ec0c9d
RFC v2: https://lore.kernel.org/linux-mm/[email protected]/
RFC v3: https://lore.kernel.org/linux-mm/[email protected]/
v4: https://lore.kernel.org/linux-mm/abv+rjgyArqZ2uym@yjaykim-PowerEdge-T330/T/#m924fa3e58d0f0da488300653163ee8db7e870e4a
v5: https://lore.kernel.org/linux-mm/ab0YEn+Fd41q6LM7@yjaykim-PowerEdge-T330/T/#m8409d470c68cb152b0849940759bff7d7806f397
v6: https://lore.kernel.org/linux-mm/[email protected]/T/#m10ee3346cd8dcd052749105d9a8e2052dbf3bc80
v7: https://lore.kernel.org/linux-mm/ab/[email protected]/

Testing:
- Hibernate/resume via sysfs
(echo reboot > /sys/power/disk && echo disk > /sys/power/state)
- Hibernate with suspend via sysfs
(echo suspend > /sys/power/disk && echo disk > /sys/power/state)
- Hibernate/resume via uswsusp (suspend-utils s2disk/resume on QEMU)
- Verified swap I/O works correctly after resume.
- Verified swapoff succeeds after snapshot resume completes.
- swapoff during active uswsusp session:
- Verified swapoff returns -EBUSY while swap device is pinned (Patch 1).
- Verified swapoff succeeds after uswsusp process terminates.

Changelog:
v7 -> v8:
- Rebased onto mm-new per Andrew Morton's suggestion.
- Clarified function comments (SWP_HIBERNATION pinning).
- Re-tested and AI-reviewed on mm-new; no new issues found.

v6 -> v7:
- Dropped Patch 3 (pm_restore_gfp_mask fix) from series as it has no dependency on Patches 1-2. Will be sent separately. (Rafael J. Wysocki feedback)

v5 -> v6:
- Replaced get/put reference approach with SWP_HIBERNATION pinning to prevent swapoff, per Kairui's feedback. Renamed helpers from get/find/put_hibernation_swap_type() to pin/find/unpin_hibernation_swap_type().
- Renamed swap_type_of() to __find_hibernation_swap_type() since it is now an internal helper with no external callers.
- Removed swapoff waiting on hibernation reference. swapoff now returns -EBUSY immediately when the swap device is pinned.
- Updated function comments per Kairui's review.

v4 -> v5:
- Rebased onto v7.0-rc4 (Rafael J. Wysocki comment)
- No functional changes.

rfc v3 -> v4:
- Introduced get/find/put_hibernation_swap_type() helpers per Kairui's feedback.
- Switched to swap_type_to_info() and added type < 0 check.
- Fixed get_hibernation_swap_type() return when ref == false.
- Made swapoff wait interruptible to prevent hang when uswsusp holds a swap reference.

rfc v2 -> rfc v3:
- Split into 2 patches per Chris Li's feedback.
- Simplified by not holding reference in normal hibernation path.
- Removed redundant SWP_WRITEOK check.

rfc v1 -> rfc v2:
- Squashed into single patch per Usama Arif's feedback.

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

 include/linux/swap.h |   5 +-
 kernel/power/swap.c  |   2 +-
 kernel/power/user.c  |  15 +++-
 mm/swapfile.c        | 203 +++++++++++++++++++++++++++++++++----------
 4 files changed, 172 insertions(+), 53 deletions(-)

base-commit: c51ea78c5466be89914cbfbe2618dea67026c2b1

Signature · 1 line
2.34.1