kernel/power: fix swap device reference handling in hibernation swap path
Subsystems (4)
Youngjun Park
reviewercover kernel/power: fix swap device reference handling in hibernation swap path
This series addresses two issues in the hibernation swap path.
First, grabbing and releasing the swap device reference on every slot allocation is inefficient across the entire hibernation swap path.
Second, 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.
The fix is to hold the swap device reference from the point the swap device is looked up, and release it once at each exit path.
Patch 1: Release the reference immediately after each slot allocation
as a preparatory step.
Patch 2: Lift the reference acquisition to the lookup site and place
put_swap_device_by_type() at all relevant cleanup paths in swap.c and user.c.
This series is based on mm-new.
I'm sending this as RFC because my familiarity with the kernel/power and snapshot paths is limited. I believe the approach is reasonable, but I'd appreciate any feedback before moving forward with proper testing and a formal submission.
Thanks,
Youngjun Park
Youngjun Park (2):
mm/swap: release swap reference on each hibernation slot allocation kernel/power: hold swap device reference across hibernation swap
operation
include/linux/swap.h | 1 + kernel/power/swap.c | 12 +++++++--- kernel/power/user.c | 9 +++++++- mm/swapfile.c | 55 ++++++++++++++++++++++---------------------- 4 files changed, 45 insertions(+), 32 deletions(-)
Signature · 1 line
2.34.1
Youngjun Park
reviewerRFC PATCH 1/2 mm/swap: release swap reference on each hibernation slot allocation
Currently, only the swap type value is retrieved at lookup time without holding a reference. If swapoff races after the type is acquired, the type value becomes invalid and subsequent slot allocations operate on a stale swap device.
Additionally, grabbing and releasing the reference on every slot allocation is inefficient. The proper approach is to hold the reference from the swap device lookup and release it once when it is no longer needed.
This is a preparatory change. A subsequent commit will lift the reference acquisition to the lookup site and replace the per-slot acquire/release with a single reference held across the entire hibernation swap operation.
--- include/linux/swap.h | 1 + mm/swapfile.c | 55 ++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 28 deletions(-)
include/linux/swap.h +1 -0
@@ -442,6 +442,7 @@ extern bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry);extern int swp_swapcount (swp_entry_t entry);struct backing_dev_info ;extern struct swap_info_struct *get_swap_device (swp_entry_t entry); +extern void put_swap_device_by_type (int type);sector_t swap_folio_sector (struct folio *folio);/*
mm/swapfile.c +27 -28
96 lines not on this page · raw message
Signature · 1 line
2.34.1
Youngjun Park
reviewerRFC PATCH 2/2 kernel/power: hold swap device reference across hibernation swap operation
In reply to Youngjun ParkAcquire the swap device reference at the point the swap device is looked up and release it at each exit path, rather than grabbing and dropping it on every slot allocation.
This also fixes a race where only the swap type value was retrieved at lookup time without holding a reference. If swapoff raced after the type was acquired, subsequent operations would reference a stale swap device.
put_swap_device_by_type() is now placed at all relevant cleanup paths in both swap.c and user.c to ensure the reference is properly released when the hibernation swap operation completes or fails.
--- kernel/power/swap.c | 12 +++++++++--- kernel/power/user.c | 9 ++++++++- 2 files changed, 17 insertions(+), 4 deletions(-)
kernel/power/swap.c +9 -3
@@ -350,9 +350,10 @@ static int swsusp_swap_check(void) hib_resume_bdev_file =bdev_file_open_by_dev (swsusp_resume_device,BLK_OPEN_WRITE ,NULL ,NULL ); -if (IS_ERR (hib_resume_bdev_file)) +if (IS_ERR (hib_resume_bdev_file)) { +put_swap_device_by_type (root_swap);return PTR_ERR (hib_resume_bdev_file); - + }return 0 ; }
@@ -418,6 +419,7 @@ static int get_swap_writer(struct swap_map_handle *handle) err_rel:release_swap_writer (handle); err_close: +put_swap_device_by_type (root_swap);swsusp_close ();return ret; }
@@ -480,8 +482,11 @@ static int swap_writer_finish(struct swap_map_handle *handle,flush_swap_writer (handle); } -if (error) +if (error) {free_all_swap_pages (root_swap); +put_swap_device_by_type (root_swap); + } +release_swap_writer (handle);swsusp_close ();
@@ -1647,6 +1652,7 @@ int swsusp_unmark(void)* We just returned from suspend, we don't need the image any more. */ free_all_swap_pages (root_swap); +put_swap_device_by_type (root_swap);return error; }
kernel/power/user.c +8 -1
@@ -90,8 +90,11 @@ static int snapshot_open(struct inode *inode, struct file *filp) data->free_bitmaps = !error; } } -if (error) +if (error) {hibernate_release (); +if (data->swap >=0 ) +put_swap_device_by_type (data->swap); + } data->frozen = false; data->ready = false;
@@ -115,6 +118,8 @@ static int snapshot_release(struct inode *inode, struct file *filp) data = filp->private_data; data->dev =0 ;free_all_swap_pages (data->swap); +if (data->swap >=0 ) +put_swap_device_by_type (data->swap);if (data->frozen) {pm_restore_gfp_mask ();free_basic_memory_bitmaps ();
@@ -235,6 +240,8 @@ static int snapshot_set_swap_area(struct snapshot_data *data, offset = swap_area.offset; } +if (data->swap >=0 ) +put_swap_device_by_type (data->swap);/* * User space encodes device types as two-byte values, * so we need to recode them
Signature · 1 line
2.34.1
Quoted 10 lines · On Tue, 3 Mar 2026 01:53:32 +0900 Youngjun Park <[email protected]> wrote:
This series addresses two issues in the hibernation swap path.
First, grabbing and releasing the swap device reference on every slot allocation is inefficient across the entire hibernation swap path.
Second, 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.
Quoted 8 lines
The fix is to hold the swap device reference from the point the swap device is looked up, and release it once at each exit path.
Patch 1: Release the reference immediately after each slot allocation
as a preparatory step.
Patch 2: Lift the reference acquisition to the lookup site and place
put_swap_device_by_type()at all relevant cleanup paths inswap.canduser.c.
Hello!
I cant comment on the feasibility of the approach, but for proper series, you would need to squash the 2 commits, otherwise it would break git bisectability.
Thanks
YoungJun Park
reviewerQuoted 21 lines · Usama Arif
Quoted 10 lines · On Tue, 3 Mar 2026 01:53:32 +0900 Youngjun Park <[email protected]> wrote:
This series addresses two issues in the hibernation swap path.
First, grabbing and releasing the swap device reference on every slot allocation is inefficient across the entire hibernation swap path.
Second, 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.
Quoted 8 lines
The fix is to hold the swap device reference from the point the swap device is looked up, and release it once at each exit path.
Patch 1: Release the reference immediately after each slot allocation
as a preparatory step.
Patch 2: Lift the reference acquisition to the lookup site and place
put_swap_device_by_type()at all relevant cleanup paths inswap.canduser.c.
Quoted 7 lines
Hello!
I cant comment on the feasibility of the approach, but for proper series, you would need to squash the 2 commits, otherwise it would break git bisectability.
Thanks
Thanks for the feedback. :D
I have squashed the commits as suggested and sent the v2 patch.
Best regards,
Youngjun Park