From: Youngjun Park <youngjun.park@lge.com>
To: linux-pm@vger.kernel.org
Cc: linux-mm@kvack.org, rafael@kernel.org, lenb@kernel.org,
pavel@kernel.org, akpm@linux-foundation.org, chrisl@kernel.org,
kasong@tencent.com, shikemeng@huaweicloud.com, nphamcs@gmail.com,
bhe@redhat.com, baohua@kernel.org, youngjun.park@lge.com
Subject: [RFC PATCH 2/2] kernel/power: hold swap device reference across hibernation swap operation
Date: Tue, 3 Mar 2026 01:53:34 +0900 [thread overview]
Message-ID: <20260302165334.1278479-3-youngjun.park@lge.com> (raw)
In-Reply-To: <20260302165334.1278479-1-youngjun.park@lge.com>
Acquire 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.
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
kernel/power/swap.c | 12 +++++++++---
kernel/power/user.c | 9 ++++++++-
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index 2e64869bb5a0..c230b0fa5a5f 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -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;
}
diff --git a/kernel/power/user.c b/kernel/power/user.c
index 4401cfe26e5c..9cb6c24d49ea 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -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
--
2.34.1
prev parent reply other threads:[~2026-03-02 17:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-02 16:53 [RFC PATCH 0/2] kernel/power: fix swap device reference handling in hibernation swap path Youngjun Park
2026-03-02 16:53 ` [RFC PATCH 1/2] mm/swap: release swap reference on each hibernation slot allocation Youngjun Park
2026-03-02 16:53 ` Youngjun Park [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260302165334.1278479-3-youngjun.park@lge.com \
--to=youngjun.park@lge.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=bhe@redhat.com \
--cc=chrisl@kernel.org \
--cc=kasong@tencent.com \
--cc=lenb@kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-pm@vger.kernel.org \
--cc=nphamcs@gmail.com \
--cc=pavel@kernel.org \
--cc=rafael@kernel.org \
--cc=shikemeng@huaweicloud.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox