mm/swap, PM: hibernate: fix swapoff race in uswsusp by pinning swap device
Subsystems (4)
Youngjun Park
reviewerv8 cover 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
Youngjun Park
reviewerPATCH v8 1/2 mm/swap, PM: hibernate: fix swapoff race in uswsusp by pinning swap device
Hibernation via uswsusp (/dev/snapshot ioctls) has a race window:
after selecting the resume swap area but before user space is frozen, swapoff may run and invalidate the selected swap device.
Fix this by pinning the swap device with SWP_HIBERNATION while it is in use. The pin is exclusive, which is sufficient since hibernate_acquire() already prevents concurrent hibernation sessions.
The kernel swsusp path (sysfs-based hibernate/resume) uses find_hibernation_swap_type() which is not affected by the pin. It freezes user space before touching swap, so swapoff cannot race.
Introduce dedicated helpers:
- pin_hibernation_swap_type(): Look up and pin the swap device. Used by the uswsusp path.
- find_hibernation_swap_type(): Lookup without pinning. Used by the kernel swsusp path.
- unpin_hibernation_swap_type(): Clear the hibernation pin.
While a swap device is pinned, swapoff is prevented from proceeding.
--- include/linux/swap.h | 5 +- kernel/power/swap.c | 2 +- kernel/power/user.c | 15 ++++- mm/swapfile.c | 135 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 136 insertions(+), 21 deletions(-)
include/linux/swap.h +4 -1
@@ -213,6 +213,7 @@ enum {SWP_PAGE_DISCARD = (1 <<10 ),/* freed swap page-cluster discards */ SWP_STABLE_WRITES = (1 <<11 ),/* no overwrite PG_writeback pages */ SWP_SYNCHRONOUS_IO = (1 <<12 ),/* synchronous IO is efficient */ + SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */ /* add others here before... */ };
@@ -433,7 +434,9 @@ static inline long get_nr_swap_pages(void) }extern void si_swapinfo (struct sysinfo *); -int swap_type_of (dev_t device,sector_t offset); +extern int pin_hibernation_swap_type (dev_t device,sector_t offset); +extern void unpin_hibernation_swap_type (int type); +extern int find_hibernation_swap_type (dev_t device,sector_t offset);int find_first_swap (dev_t *device);extern unsigned int count_swap_pages (int ,int );extern sector_t swapdev_block (int ,pgoff_t );
kernel/power/swap.c +1 -1
@@ -341,7 +341,7 @@ static int swsusp_swap_check(void)* This is called before saving the image. */ if (swsusp_resume_device) - res =swap_type_of (swsusp_resume_device, swsusp_resume_block); + res =find_hibernation_swap_type (swsusp_resume_device, swsusp_resume_block);else res =find_first_swap (&swsusp_resume_device);if (res <0 )
kernel/power/user.c +12 -3
@@ -71,7 +71,7 @@ static int snapshot_open(struct inode *inode, struct file *filp)memset (&data->handle,0 ,sizeof (struct snapshot_handle ));if ((filp->f_flags &O_ACCMODE ) ==O_RDONLY ) {/* Hibernating. The image device should be accessible. */ - data->swap =swap_type_of (swsusp_resume_device,0 ); + data->swap =pin_hibernation_swap_type (swsusp_resume_device,0 ); data->mode =O_RDONLY ; data->free_bitmaps = false; error =pm_notifier_call_chain_robust (PM_HIBERNATION_PREPARE ,PM_POST_HIBERNATION );
@@ -90,8 +90,10 @@ static int snapshot_open(struct inode *inode, struct file *filp) data->free_bitmaps = !error; } } -if (error) +if (error) { +unpin_hibernation_swap_type (data->swap);hibernate_release (); + } data->frozen = false; data->ready = false;
@@ -115,6 +117,7 @@ static int snapshot_release(struct inode *inode, struct file *filp) data = filp->private_data; data->dev =0 ;free_all_swap_pages (data->swap); +unpin_hibernation_swap_type (data->swap);if (data->frozen) {pm_restore_gfp_mask ();free_basic_memory_bitmaps ();
@@ -235,11 +238,17 @@ static int snapshot_set_swap_area(struct snapshot_data *data, offset = swap_area.offset; } +/* +* Unpin the swap device if a swap area was already +* set by SNAPSHOT_SET_SWAP_AREA. +*/ +unpin_hibernation_swap_type (data->swap); +/* * User space encodes device types as two-byte values, * so we need to recode them */ - data->swap =swap_type_of (swdev, offset); + data->swap =pin_hibernation_swap_type (swdev, offset);if (data->swap <0 )return swdev ? -ENODEV : -EINVAL ; data->dev = swdev;
mm/swapfile.c +119 -16
170 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 12 lines · Youngjun Park
Hibernation via uswsusp (
/dev/snapshotioctls) has a race window:
after selecting the resume swap area but before user space is frozen, swapoff may run and invalidate the selected swap device.Fix this by pinning the swap device with
SWP_HIBERNATIONwhile it is in use. The pin is exclusive, which is sufficient sincehibernate_acquire()already prevents concurrent hibernation sessions.The kernel swsusp path (sysfs-based hibernate/resume) uses
find_hibernation_swap_type()which is not affected by the pin. It freezes user space before touching swap, so swapoff cannot race.
Quoted 6 lines
Introduce dedicated helpers:
-pin_hibernation_swap_type(): Look up and pin the swap device. Used by the uswsusp path.
-find_hibernation_swap_type(): Lookup without pinning. Used by the kernel swsusp path.
-unpin_hibernation_swap_type(): Clear the hibernation pin.
Looks good to me, thanks!
Just one trivial nit picks below.
Quoted 11 lines
mm/swapfile.c +11
-0+/** +* unpin_hibernation_swap_type - Unpin the swap device for hibernation +* @type: Swap type previously returned by pin_hibernation_swap_type() +* +* Clear the hibernation pin on the given swap device, allowing +* swapoff() to proceed normally. +* +* If @type does not refer to a valid swap device, this function +* does nothing. +*/ +void unpin_hibernation_swap_type (int type)
mm/swapfile.c int swap_type_of(dev_t device, sector_t offset) · line 2238
Quoted 10 lines
mm/swapfile.c +10
-0+{ +struct swap_info_struct *si; + +spin_lock (&swap_lock); + si =swap_type_to_info (type); +if (!si) { +spin_unlock (&swap_lock); +return ; + } + si->flags &= ~SWP_HIBERNATION ;
Will the code will be simpler if you just:
if (si)
si->flags &= ~SWP_HIBERNATION;
Just personal taste, free feel to ignore.
And as you mentioned this is on top of swap table p3 so you based it on mm-new - but isn't p3 already in mm-unstable? Maybe we can have it there? Not sure how much conflict are there with PM.
The code and design looks OK.
YoungJun Park
reviewerQuoted 20 lines · Kairui Song
Quoted 12 lines · Youngjun Park
Hibernation via uswsusp (
/dev/snapshotioctls) has a race window:
after selecting the resume swap area but before user space is frozen, swapoff may run and invalidate the selected swap device.Fix this by pinning the swap device with
SWP_HIBERNATIONwhile it is in use. The pin is exclusive, which is sufficient sincehibernate_acquire()already prevents concurrent hibernation sessions.The kernel swsusp path (sysfs-based hibernate/resume) uses
find_hibernation_swap_type()which is not affected by the pin. It freezes user space before touching swap, so swapoff cannot race.Quoted 6 lines
Introduce dedicated helpers:
-pin_hibernation_swap_type(): Look up and pin the swap device. Used by the uswsusp path.
-find_hibernation_swap_type(): Lookup without pinning. Used by the kernel swsusp path.
-unpin_hibernation_swap_type(): Clear the hibernation pin.
Quoted 3 lines
Looks good to me, thanks!
Reviewed-by Kairui Song <[email protected]>
Thanks for the review, Kairui, and for all your feedback throughout the revisions!
Quoted 26 lines
Just one trivial nit picks below.
Quoted 11 lines
mm/swapfile.c +11
-0+/** +* unpin_hibernation_swap_type - Unpin the swap device for hibernation +* @type: Swap type previously returned by pin_hibernation_swap_type() +* +* Clear the hibernation pin on the given swap device, allowing +* swapoff() to proceed normally. +* +* If @type does not refer to a valid swap device, this function +* does nothing. +*/ +void unpin_hibernation_swap_type (int type)mm/swapfile.c int swap_type_of(dev_t device, sector_t offset) · line 2238
Quoted 10 lines
mm/swapfile.c +10
-0+{ +struct swap_info_struct *si; + +spin_lock (&swap_lock); + si =swap_type_to_info (type); +if (!si) { +spin_unlock (&swap_lock); +return ; + } + si->flags &= ~SWP_HIBERNATION ;Will the code will be simpler if you just:
Quoted 4 lines
if (si)
si->flags &= ~SWP_HIBERNATION;Just personal taste, free feel to ignore.
Noted on the style preference. I'll keep it in mind. :D
Quoted 5 lines
And as you mentioned this is on top of swap table p3 so you based it on mm-new - but isn't p3 already in mm-unstable? Maybe we can have it there? Not sure how much conflict are there with PM.
The code and design looks OK.
Regarding the base branch. Andrew is already aware of the potential conflicts in linux-next, and we've discussed possibly parking this for the next cycle depending on Rafael's input. So I think we can keep it as-is for now and see how things go!
Best regards,
Youngjun Park
Youngjun Park
reviewerPATCH v8 2/2 mm/swap: remove redundant swap device reference in alloc/free
In reply to Youngjun ParkIn the previous commit, uswsusp was modified to pin the swap device when the swap type is determined, ensuring the device remains valid throughout the hibernation I/O path.
Therefore, it is no longer necessary to repeatedly get and put the swap device reference for each swap slot allocation and free operation.
For hibernation via the sysfs interface, user-space tasks are frozen before swap allocation begins, so swapoff cannot race with allocation. After resume, tasks remain frozen while swap slots are freed, so additional reference management is not required there either.
Remove the redundant swap device get/put operations from the hibernation swap allocation and free paths.
Also remove the SWP_WRITEOK check before allocation, as the cluster allocation logic already validates the swap device state.
Update function comments to document the caller's responsibility for ensuring swap device stability.
--- mm/swapfile.c | 68 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 32 deletions(-)
mm/swapfile.c +36 -32
@@ -2077,7 +2077,16 @@ void swap_put_entries_direct(swp_entry_t entry, int nr) }#ifdef CONFIG_HIBERNATION -/* Allocate a slot for hibernation */ +/** +* swap_alloc_hibernation_slot() - Allocate a swap slot for hibernation. +* @type: swap device type index to allocate from. +* +* The caller must ensure the swap device is stable, either by pinning +* it (SWP_HIBERNATION) or by freezing user-space. +* +* Return: a valid swp_entry_t on success, or an empty entry (val == 0) +* on failure. +*/ swp_entry_t swap_alloc_hibernation_slot (int type) {struct swap_info_struct *pcp_si, *si =swap_type_to_info (type);
@@ -2088,46 +2097,42 @@ swp_entry_t swap_alloc_hibernation_slot(int type)if (!si) goto fail; -/* This is called for allocating swap entry, not cache */ -if (get_swap_device_info (si)) { -if (si->flags &SWP_WRITEOK ) { -/* -* Try the local cluster first if it matches the device. If -* not, try grab a new cluster and override local cluster. -*/ -local_lock (&percpu_swap_cluster.lock); - pcp_si =this_cpu_read (percpu_swap_cluster.si[0 ]); - pcp_offset =this_cpu_read (percpu_swap_cluster.offset[0 ]); -if (pcp_si == si && pcp_offset) { - ci =swap_cluster_lock (si, pcp_offset); -if (cluster_is_usable (ci,0 )) - offset =alloc_swap_scan_cluster (si, ci,NULL , pcp_offset); -else -swap_cluster_unlock (ci); - } -if (!offset) - offset =cluster_alloc_swap_entry (si,NULL ); -local_unlock (&percpu_swap_cluster.lock); -if (offset) - entry =swp_entry (si->type, offset); - } -put_swap_device (si); +/* +* Try the local cluster first if it matches the device. If +* not, try grab a new cluster and override local cluster. +*/ +local_lock (&percpu_swap_cluster.lock); + pcp_si =this_cpu_read (percpu_swap_cluster.si[0 ]); + pcp_offset =this_cpu_read (percpu_swap_cluster.offset[0 ]); +if (pcp_si == si && pcp_offset) { + ci =swap_cluster_lock (si, pcp_offset); +if (cluster_is_usable (ci,0 )) + offset =alloc_swap_scan_cluster (si, ci,NULL , pcp_offset); +else +swap_cluster_unlock (ci); } +if (!offset) + offset =cluster_alloc_swap_entry (si,NULL ); +local_unlock (&percpu_swap_cluster.lock); +if (offset) + entry =swp_entry (si->type, offset); + fail:return entry; } -/* Free a slot allocated by swap_alloc_hibernation_slot */ +/** +* swap_free_hibernation_slot() - Free a swap slot allocated for hibernation. +* @entry: swap entry to free. +* +* The caller must ensure the swap device is stable. +*/ void swap_free_hibernation_slot (swp_entry_t entry) { -struct swap_info_struct *si; +struct swap_info_struct *si =__swap_entry_to_info (entry);struct swap_cluster_info *ci;pgoff_t offset =swp_offset (entry); - si =get_swap_device (entry); -if (WARN_ON (!si)) -return ; - ci =swap_cluster_lock (si, offset);__swap_cluster_put_entry (ci, offset %SWAPFILE_CLUSTER );__swap_cluster_free_entries (si, ci, offset %SWAPFILE_CLUSTER ,1 );
@@ -2135,7 +2140,6 @@ void swap_free_hibernation_slot(swp_entry_t entry)/* In theory readahead might add it to the swap cache by accident */ __try_to_reclaim_swap (si, offset,TTRS_ANYWAY ); -put_swap_device (si); }static int __find_hibernation_swap_type (dev_t device,sector_t offset)
Signature · 1 line
2.34.1
Re: [PATCH v8 2/2] mm/swap: remove redundant swap device reference in alloc/free
Quoted 18 lines · Youngjun Park
In the previous commit, uswsusp was modified to pin the swap device when the swap type is determined, ensuring the device remains valid throughout the hibernation I/O path.
Therefore, it is no longer necessary to repeatedly get and put the swap device reference for each swap slot allocation and free operation.
For hibernation via the sysfs interface, user-space tasks are frozen before swap allocation begins, so swapoff cannot race with allocation. After resume, tasks remain frozen while swap slots are freed, so additional reference management is not required there either.
Remove the redundant swap device get/put operations from the hibernation swap allocation and free paths.
Also remove the
SWP_WRITEOKcheck before allocation, as the cluster allocation logic already validates the swap device state.
Quoted 7 lines
Update function comments to document the caller's responsibility for ensuring swap device stability.
Signed-off-by Youngjun Park <[email protected]>--- mm/swapfile.c | 68 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 32 deletions(-)
Thanks!
Andrew Morton
maintainerQuoted 9 lines · On Tue, 24 Mar 2026 01:08:20 +0900 Youngjun Park <[email protected]> wrote:
Rebased onto mm-new per Andrew's suggestion [1]. The
si->flagsrace flagged by AI review in v7 (betweenSWP_HIBERNATIONandcont_lockinadd_swap_count_continuation) and the proposed fixes discussed there (atomic ops forsi->flags, or serializing withswap_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?
Please.
We'll hit a conflict in linux-next and Mark will tell us and we can flag that to Linus when merging into mainline, usual stuff.
Or we can park this until the next cycle, depends on how serious the bug is. How serious is the bug?
YoungJun Park
reviewerQuoted 12 lines · Andrew Morton
Quoted 9 lines · On Tue, 24 Mar 2026 01:08:20 +0900 Youngjun Park <[email protected]> wrote:
Rebased onto mm-new per Andrew's suggestion [1]. The
si->flagsrace flagged by AI review in v7 (betweenSWP_HIBERNATIONandcont_lockinadd_swap_count_continuation) and the proposed fixes discussed there (atomic ops forsi->flags, or serializing withswap_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?
Quoted 7 lines
Please.
We'll hit a conflict in linux-next and Mark will tell us and we can flag that to Linus when merging into mainline, usual stuff.
Or we can park this until the next cycle, depends on how serious the bug is. How serious is the bug?
Hi Andrew,
In my opinion, this bug is unlikely to occur and does not appear to be serious. It may be better to park this for the next cycle.
I verified the behavior using suspend-utils. Below is a recap of the reproduction scenarios I mentioned earlier in more detail.
Pre-condition
- swapon /dev/sdb (intended for hibernation)
Case 1
Process 1 (test program) Process 2
--------------------------- ----------------
ioctl(SNAPSHOT_SET_SWAP_AREA)
swapoff /dev/sdb
ioctl(SNAPSHOT_ALLOC_SWAP_PAGE)
- SNAPSHOT_ALLOC_SWAP_PAGE fails with -ENOSPC.
- The race window where swapoff /dev/sdb can occur is extremely small, and such an intentional sequence is unlikely in practice.
- If SNAPSHOT_ALLOC_SWAP_PAGE succeeds, swapoff does not occur.
Case 2
Process 1 (test program) Process 2
--------------------------- ----------------
ioctl(SNAPSHOT_SET_SWAP_AREA)
swapoff /dev/sdb swapon /dev/sdc
freeze processes ioctl(SNAPSHOT_ALLOC_SWAP_PAGE)
create snapshot image
- In testing, snapshot boot from /dev/sdb succeeds.
- The swap block offset may be taken from an unexpected device, but I/O to /dev/sdb itself succeeds.
- Since the actual allocated swap offset is not used for writing the snapshot image, there is a theoretical risk of corruption if I/O occurs at that offset on /dev/sdb during the window. However, Processes are frozen before writing the snapshot image to /dev/sdb. Therefore, while the issue is theoretically possible, the probability of it occurring in practice appears extremely low.
Best regards,
Youngjun Park
Andrew Morton
maintainerQuoted 10 lines · On Tue, 24 Mar 2026 11:51:12 +0900 YoungJun Park <[email protected]> wrote:
Quoted 5 lines
We'll hit a conflict in linux-next and Mark will tell us and we can flag that to Linus when merging into mainline, usual stuff.
Or we can park this until the next cycle, depends on how serious the bug is. How serious is the bug?
Hi Andrew,
In my opinion, this bug is unlikely to occur and does not appear to be serious. It may be better to park this for the next cycle.
Great, thanks. I'll await Rafael's input and let's see how much damage Mark encounters.