* [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache
@ 2026-08-06 19:06 Youngjun Park
2026-08-06 19:06 ` [PATCH 1/4] mm, swap: don't free a hibernation slot that is in " Youngjun Park
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Youngjun Park @ 2026-08-06 19:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Jianyue Wu, Youngjun Park, her0gyugyu, linux-mm,
linux-kernel
Cluster readahead walks a raw page_cluster sized window of offsets around
the faulting entry. A hibernation slot looks like an ordinary swapped out
slot, so __swap_cache_add_check() lets it in. Readahead reads the offset
off the device into a folio and puts that folio in the swap table where the
hibernation entry was. This has been possible for a long time. It only
wasted a folio and a read.
That changed with commit 0d6af9bcf383 ("mm, swap: use the swap table to
track the swap count"). A slot with a folio in the swap cache should only
be freed when the folio leaves the cache. swap_put_entries_cluster() still
does that, but the conversion left swap_free_hibernation_slot() freeing the
slot either way. Nothing points at the folio after that, and when reclaim
drops it later, it writes to the table entry at the old offset, which
someone else may own by then.
Patch 1 is the fix. It puts the missing check back, so both free paths
behave the same again.
The rest removes the cause. Readahead should not touch these slots at all,
so patch 2 gives hibernation slots their own swap table entry type, patch 3
lets only swapped out slots into the swap cache, and patch 4 drops the
check and the reclaim, since no such folio can exist any more.
For any of this a task has to hold hibernation slots while the system is
still running. The in kernel path does not, it allocates, writes and frees
the slots with everything frozen. Userspace hibernation is different. The
process writing the image is not frozen, and SNAPSHOT_ALLOC_SWAP_PAGE does
not check that anything is frozen. The swap device must also not be
SWP_SYNCHRONOUS_IO, or swapin takes the direct path and never reaches
cluster readahead.
Tested with a debug patch generated by AI that counts hibernation slots through the swap
cache paths. virtio-blk swap, page-cluster 3, SNAPSHOT_ALLOC_SWAP_PAGE
interleaved with MADV_PAGEOUT of a shmem region so the hibernation slots
land in the readahead windows.
unpatched +patch 1 patches 1-4
hibernation slots allocated 2732 2732 2732
readahead landed on the slot 2731 2731 2731
admitted to the swap cache 2731 2731 0
reclaim found the folio 0 2731 -
slot left unfreed 0 0 0
VM_WARN in the free path 2731 0 0
The VM_WARN is the existing assertion in __swap_cluster_free_entries(),
not something the debug patch adds.
Youngjun Park (4):
mm, swap: don't free a hibernation slot that is in the swap cache
mm, swap: give hibernation swap slots their own swap table entry type
mm, swap: only allow swapped-out slots into the swap cache
mm, swap: drop the swap cache guard and reclaim in
swap_free_hibernation_slot()
mm/swap_state.c | 9 +++++++--
mm/swap_table.h | 12 ++++++++++++
mm/swapfile.c | 16 +++++++---------
3 files changed, 26 insertions(+), 11 deletions(-)
base-commit: 0b53bff4fa05ff0d3ffbd3d3bb10fae69dfab498
--
2.48.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/4] mm, swap: don't free a hibernation slot that is in the swap cache
2026-08-06 19:06 [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache Youngjun Park
@ 2026-08-06 19:06 ` Youngjun Park
2026-08-08 12:09 ` Kairui Song
2026-08-06 19:06 ` [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type Youngjun Park
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Youngjun Park @ 2026-08-06 19:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Jianyue Wu, Youngjun Park, her0gyugyu, linux-mm,
linux-kernel
A slot with a folio in the swap cache is freed when the folio leaves the
cache, not when its count drops. swap_put_entries_cluster() follows that
rule. swap_free_hibernation_slot() does not, it calls
__swap_cluster_free_entries() whether or not a folio sits on the slot.
Cluster readahead can put one there. It walks a raw page_cluster sized
window of offsets around the faulting entry, and a hibernation slot passes
__swap_cache_add_check() because it is not a folio and its count is not
zero. Freeing the slot then clears the entry under that folio.
The folio is now unreachable from the swap table, and the offset goes back
to the allocator. The folio is still on the LRU though, so reclaim can
pick it up later. It then takes the old offset out of folio->swap and
overwrites the table entry there, which by then may belong to someone else.
Check for a cached folio before freeing. The slot is then left in the
ordinary state where only the swap cache holds it, and it is freed when the
folio leaves the cache, either through the reclaim below or through normal
reclaim later.
Fixes: 0d6af9bcf383 ("mm, swap: use the swap table to track the swap count")
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index dea2d3b36e06..f5dfc7e59191 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2202,7 +2202,14 @@ void swap_free_hibernation_slot(swp_entry_t entry)
ci = swap_cluster_lock(si, offset);
__swap_cluster_put_entry(ci, offset % SWAPFILE_CLUSTER);
- __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
+ /*
+ * A slot with a folio in the swap cache is freed when the folio
+ * leaves the cache, the same rule swap_put_entries_cluster() follows.
+ * Readahead can put a folio here, and freeing the slot now would
+ * leave that folio with no entry behind it.
+ */
+ if (!swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER)))
+ __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
swap_cluster_unlock(ci);
/* In theory readahead might add it to the swap cache by accident */
--
2.48.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type
2026-08-06 19:06 [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache Youngjun Park
2026-08-06 19:06 ` [PATCH 1/4] mm, swap: don't free a hibernation slot that is in " Youngjun Park
@ 2026-08-06 19:06 ` Youngjun Park
2026-08-08 13:25 ` Kairui Song
2026-08-06 19:06 ` [PATCH 3/4] mm, swap: only allow swapped-out slots into the swap cache Youngjun Park
2026-08-06 19:06 ` [PATCH 4/4] mm, swap: drop the swap cache guard and reclaim in swap_free_hibernation_slot() Youngjun Park
3 siblings, 1 reply; 8+ messages in thread
From: Youngjun Park @ 2026-08-06 19:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Jianyue Wu, Youngjun Park, her0gyugyu, linux-mm,
linux-kernel
swap_alloc_hibernation_slot() stores a fake shadow in the slot it hands
out. An anon slot swapped out with no workingset shadow looks exactly the
same, so nothing in mm can tell the two apart.
Give hibernation slots their own type. Bit 4 and every bit above it are
set, the same shape as SWP_TB_BAD. Bits 0 to 3 are taken by the shadow,
PFN, pointer and bad marks, so bit 4 is the first free one. Neither type
holds data, so the value alone says what it is.
The entry has no swap count. Hibernation only allocates and frees a slot,
so a count would never change. swap_free_hibernation_slot() frees the slot
directly, there is no count to put first.
The next patch needs these slots to stop looking like shadows.
Suggested-by: Kairui Song <kasong@tencent.com>
Link: https://lore.kernel.org/linux-mm/abp7aDgYLrxF3Me8@KASONG-MC4/
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swap_table.h | 12 ++++++++++++
mm/swapfile.c | 13 +++++++------
2 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/mm/swap_table.h b/mm/swap_table.h
index e6613e62f8d0..c1c516bcc17e 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -30,6 +30,7 @@ struct swap_memcg_table {
* PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot
* Pointer: |----------- Pointer ----------|100| - (Unused)
* Bad: |------------- 1 -------------|1000| - Bad slot
+ * Hibern: |------------ 1 -------------|10000| - Hibernation slot
*
* COUNT is `SWP_TB_COUNT_BITS` long, Z is the `SWP_TB_ZERO_FLAG` bit,
* and together they form the `SWP_TB_FLAGS_BITS` wide flags field.
@@ -54,6 +55,9 @@ struct swap_memcg_table {
* aligned pointers.
*
* - Bad: Swap slot is reserved, protects swap header or holes on swap devices.
+ *
+ * - Hibern: Swap slot is reserved by hibernation for the suspend image, and
+ * must never enter the swap cache.
*/
/* NULL Entry, all 0 */
@@ -81,6 +85,9 @@ struct swap_memcg_table {
/* Bad slot: ends with 0b1000 and rests of bits are all 1 */
#define SWP_TB_BAD ((~0UL) << 3)
+/* Hibernation slot: ends with 0b10000 and rests of bits are all 1 */
+#define SWP_TB_HIB ((~0UL) << 4)
+
/* Macro for shadow offset calculation */
#define SWAP_COUNT_SHIFT SWP_TB_FLAGS_BITS
@@ -166,6 +173,11 @@ static inline bool swp_tb_is_bad(unsigned long swp_tb)
return swp_tb == SWP_TB_BAD;
}
+static inline bool swp_tb_is_hibernation(unsigned long swp_tb)
+{
+ return swp_tb == SWP_TB_HIB;
+}
+
static inline bool swp_tb_is_countable(unsigned long swp_tb)
{
return (swp_tb_is_shadow(swp_tb) || swp_tb_is_folio(swp_tb) ||
diff --git a/mm/swapfile.c b/mm/swapfile.c
index f5dfc7e59191..a337387f7431 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -928,7 +928,7 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
* upon folio unmap.
*
* Else, it's a exclusive order 0 allocation for hibernation.
- * The slot starts with count == 1 and never increases.
+ * The slot carries no swap count and is freed by offset.
*/
if (likely(folio)) {
order = folio_order(folio);
@@ -940,8 +940,8 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
order = 0;
nr_pages = 1;
swap_cluster_assert_empty(ci, ci_off, 1, false);
- /* Fake shadow placeholder with no flag, hibernation does not use the zeromap */
- __swap_table_set(ci, ci_off, __swp_tb_mk_count(shadow_to_swp_tb(NULL, 0), 1));
+ /* Exclusively owned by hibernation, must never enter the swap cache */
+ __swap_table_set(ci, ci_off, SWP_TB_HIB);
} else {
/* Allocation without folio is only possible with hibernation */
WARN_ON_ONCE(1);
@@ -1929,9 +1929,11 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
old_tb = __swap_table_get(ci, ci_off);
/*
* Freeing is done after release of the last swap count
- * ref, or after swap cache is dropped
+ * ref, or after swap cache is dropped. A hibernation slot
+ * has no count and is freed directly by its owner.
*/
- VM_WARN_ON(!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1);
+ VM_WARN_ON(!swp_tb_is_hibernation(old_tb) &&
+ (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
/* Resetting the slot to NULL also clears the inline flags. */
__swap_table_set(ci, ci_off, null_to_swp_tb());
@@ -2201,7 +2203,6 @@ void swap_free_hibernation_slot(swp_entry_t entry)
pgoff_t offset = swp_offset(entry);
ci = swap_cluster_lock(si, offset);
- __swap_cluster_put_entry(ci, offset % SWAPFILE_CLUSTER);
/*
* A slot with a folio in the swap cache is freed when the folio
* leaves the cache, the same rule swap_put_entries_cluster() follows.
--
2.48.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/4] mm, swap: only allow swapped-out slots into the swap cache
2026-08-06 19:06 [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache Youngjun Park
2026-08-06 19:06 ` [PATCH 1/4] mm, swap: don't free a hibernation slot that is in " Youngjun Park
2026-08-06 19:06 ` [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type Youngjun Park
@ 2026-08-06 19:06 ` Youngjun Park
2026-08-06 19:06 ` [PATCH 4/4] mm, swap: drop the swap cache guard and reclaim in swap_free_hibernation_slot() Youngjun Park
3 siblings, 0 replies; 8+ messages in thread
From: Youngjun Park @ 2026-08-06 19:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Jianyue Wu, Youngjun Park, her0gyugyu, linux-mm,
linux-kernel
__swap_cache_add_check() turns away folio entries and slots with no count
and lets everything else in. That is safe only when the caller owns the
slot. Cluster readahead owns nothing, it walks a raw page_cluster sized
window of offsets around the faulting entry.
A hibernation slot is not a folio, and the count test does not stop it
either, because the type the previous patch added has the count bits set.
So readahead allocates a folio and reads the offset off the device into
it, for a slot that nothing will ever swap in. A bad slot listed in the
swap header gets in the same way, its count bits are set too, and the folio
entry that replaces it drops the bad marker. The first patch keeps that
folio from doing harm, but the folio and the read still happen.
Require a shadow entry instead. A slot dropped from the swap cache always
gets one, empty if there is no workingset value. The check runs before the
folio allocation in __swap_cache_alloc(), so readahead now skips the offset
without allocating or reading.
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swap_state.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 5be825911e64..9f2cc5918713 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -180,9 +180,14 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
old_tb = __swap_table_get(ci, ci_off);
if (swp_tb_is_folio(old_tb))
return -EEXIST;
- if (!__swp_tb_get_count(old_tb))
+ /*
+ * Only a swapped-out slot may be brought into the swap cache.
+ * Cluster readahead walks raw offset ranges, so it can land on
+ * slots that are free, bad, or owned by hibernation.
+ */
+ if (!swp_tb_is_shadow(old_tb) || !__swp_tb_get_count(old_tb))
return -ENOENT;
- if (shadowp && swp_tb_is_shadow(old_tb))
+ if (shadowp)
*shadowp = swp_tb_to_shadow(old_tb);
if (memcg_id)
*memcg_id = __swap_cgroup_get(ci, ci_off);
--
2.48.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 4/4] mm, swap: drop the swap cache guard and reclaim in swap_free_hibernation_slot()
2026-08-06 19:06 [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache Youngjun Park
` (2 preceding siblings ...)
2026-08-06 19:06 ` [PATCH 3/4] mm, swap: only allow swapped-out slots into the swap cache Youngjun Park
@ 2026-08-06 19:06 ` Youngjun Park
3 siblings, 0 replies; 8+ messages in thread
From: Youngjun Park @ 2026-08-06 19:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Jianyue Wu, Youngjun Park, her0gyugyu, linux-mm,
linux-kernel
Both are there for a folio that readahead might have put on the slot. The
previous patch turns these slots away at the swap cache boundary, so no
such folio can exist.
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index a337387f7431..4fba1770ce61 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2203,18 +2203,8 @@ void swap_free_hibernation_slot(swp_entry_t entry)
pgoff_t offset = swp_offset(entry);
ci = swap_cluster_lock(si, offset);
- /*
- * A slot with a folio in the swap cache is freed when the folio
- * leaves the cache, the same rule swap_put_entries_cluster() follows.
- * Readahead can put a folio here, and freeing the slot now would
- * leave that folio with no entry behind it.
- */
- if (!swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER)))
- __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
+ __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
swap_cluster_unlock(ci);
-
- /* In theory readahead might add it to the swap cache by accident */
- __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
}
static int __find_hibernation_swap_type(dev_t device, sector_t offset)
--
2.48.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 1/4] mm, swap: don't free a hibernation slot that is in the swap cache
2026-08-06 19:06 ` [PATCH 1/4] mm, swap: don't free a hibernation slot that is in " Youngjun Park
@ 2026-08-08 12:09 ` Kairui Song
2026-08-08 13:26 ` Kairui Song
0 siblings, 1 reply; 8+ messages in thread
From: Kairui Song @ 2026-08-08 12:09 UTC (permalink / raw)
To: Youngjun Park
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Jianyue Wu, her0gyugyu, linux-mm,
linux-kernel
On Fri, Aug 07, 2026 at 04:06:33AM +0800, Youngjun Park wrote:
> A slot with a folio in the swap cache is freed when the folio leaves the
> cache, not when its count drops. swap_put_entries_cluster() follows that
> rule. swap_free_hibernation_slot() does not, it calls
> __swap_cluster_free_entries() whether or not a folio sits on the slot.
>
> Cluster readahead can put one there. It walks a raw page_cluster sized
> window of offsets around the faulting entry, and a hibernation slot passes
> __swap_cache_add_check() because it is not a folio and its count is not
> zero. Freeing the slot then clears the entry under that folio.
>
> The folio is now unreachable from the swap table, and the offset goes back
> to the allocator. The folio is still on the LRU though, so reclaim can
> pick it up later. It then takes the old offset out of folio->swap and
> overwrites the table entry there, which by then may belong to someone else.
>
> Check for a cached folio before freeing. The slot is then left in the
> ordinary state where only the swap cache holds it, and it is freed when the
> folio leaves the cache, either through the reclaim below or through normal
> reclaim later.
>
> Fixes: 0d6af9bcf383 ("mm, swap: use the swap table to track the swap count")
> Signed-off-by: Youngjun Park <youngjun.park@lge.com>
> ---
> mm/swapfile.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
Hi Youngjun,
Thanks a lot for looking into the hibernation issue, I've been thinking
about using a exclusive type for it, glad to see actual code for it :)
And this patch looks good to me, nice catch!
Acked-by: Kairui Song <kasong@tencent.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type
2026-08-06 19:06 ` [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type Youngjun Park
@ 2026-08-08 13:25 ` Kairui Song
0 siblings, 0 replies; 8+ messages in thread
From: Kairui Song @ 2026-08-08 13:25 UTC (permalink / raw)
To: Youngjun Park
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Jianyue Wu, her0gyugyu, linux-mm,
linux-kernel
On Fri, Aug 07, 2026 at 04:06:34AM +0800, Youngjun Park wrote:
> swap_alloc_hibernation_slot() stores a fake shadow in the slot it hands
> out. An anon slot swapped out with no workingset shadow looks exactly the
> same, so nothing in mm can tell the two apart.
>
> Give hibernation slots their own type. Bit 4 and every bit above it are
> set, the same shape as SWP_TB_BAD. Bits 0 to 3 are taken by the shadow,
> PFN, pointer and bad marks, so bit 4 is the first free one. Neither type
> holds data, so the value alone says what it is.
>
> The entry has no swap count. Hibernation only allocates and frees a slot,
> so a count would never change. swap_free_hibernation_slot() frees the slot
> directly, there is no count to put first.
>
> The next patch needs these slots to stop looking like shadows.
>
> Suggested-by: Kairui Song <kasong@tencent.com>
> Link: https://lore.kernel.org/linux-mm/abp7aDgYLrxF3Me8@KASONG-MC4/
> Signed-off-by: Youngjun Park <youngjun.park@lge.com>
> ---
> mm/swap_table.h | 12 ++++++++++++
> mm/swapfile.c | 13 +++++++------
> 2 files changed, 19 insertions(+), 6 deletions(-)
>
> diff --git a/mm/swap_table.h b/mm/swap_table.h
> index e6613e62f8d0..c1c516bcc17e 100644
> --- a/mm/swap_table.h
> +++ b/mm/swap_table.h
> @@ -30,6 +30,7 @@ struct swap_memcg_table {
> * PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot
> * Pointer: |----------- Pointer ----------|100| - (Unused)
> * Bad: |------------- 1 -------------|1000| - Bad slot
> + * Hibern: |------------ 1 -------------|10000| - Hibernation slot
Nice!
Just one idea, would it be nicer if we have:
* Hibern: | 0 |------- 1 -------------|10000| - Hibernation slot
Or:
* Hibern: |0..001|------- 1 -------------|10000| - Hibernation slot
That way if we accidentally used __swp_tb_get_count, it return a actual
meaningful value instead of MAX. Either 0 - the slot is not used as
a countable ordinary slot, or 1 - the slot has one user: hibernation.
Maybe 0 is better at least for the intermediate commit, see below.
>
> +static inline bool swp_tb_is_hibernation(unsigned long swp_tb)
> +{
> + return swp_tb == SWP_TB_HIB;
> +}
> +
> static inline bool swp_tb_is_countable(unsigned long swp_tb)
> {
> return (swp_tb_is_shadow(swp_tb) || swp_tb_is_folio(swp_tb) ||
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index f5dfc7e59191..a337387f7431 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -928,7 +928,7 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
> * upon folio unmap.
> *
> * Else, it's a exclusive order 0 allocation for hibernation.
> - * The slot starts with count == 1 and never increases.
> + * The slot carries no swap count and is freed by offset.
> */
> if (likely(folio)) {
> order = folio_order(folio);
> @@ -940,8 +940,8 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
> order = 0;
> nr_pages = 1;
> swap_cluster_assert_empty(ci, ci_off, 1, false);
> - /* Fake shadow placeholder with no flag, hibernation does not use the zeromap */
> - __swap_table_set(ci, ci_off, __swp_tb_mk_count(shadow_to_swp_tb(NULL, 0), 1));
> + /* Exclusively owned by hibernation, must never enter the swap cache */
> + __swap_table_set(ci, ci_off, SWP_TB_HIB);
> } else {
> /* Allocation without folio is only possible with hibernation */
> WARN_ON_ONCE(1);
> @@ -1929,9 +1929,11 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
> old_tb = __swap_table_get(ci, ci_off);
> /*
> * Freeing is done after release of the last swap count
> - * ref, or after swap cache is dropped
> + * ref, or after swap cache is dropped. A hibernation slot
> + * has no count and is freed directly by its owner.
> */
> - VM_WARN_ON(!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1);
> + VM_WARN_ON(!swp_tb_is_hibernation(old_tb) &&
> + (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
>
> /* Resetting the slot to NULL also clears the inline flags. */
> __swap_table_set(ci, ci_off, null_to_swp_tb());
> @@ -2201,7 +2203,6 @@ void swap_free_hibernation_slot(swp_entry_t entry)
> pgoff_t offset = swp_offset(entry);
>
> ci = swap_cluster_lock(si, offset);
> - __swap_cluster_put_entry(ci, offset % SWAPFILE_CLUSTER);
> /*
> * A slot with a folio in the swap cache is freed when the folio
> * leaves the cache, the same rule swap_put_entries_cluster() follows.
This idea is right, but is the patch in the right order? If readahead
tried to add a folio to a hibernate slot by accident, seems nothing
blocks that in the current patch, and that PFN slot will have a (MAX)
count value, and considered countable? If the that folio is somehow
reclaimed, we got a corrupted shadow (hib type is gone)?
If we have the count part of a hibernation slot be 0,
__swap_cache_add_check will fail natively, seems there will be no
such risk. A few existing helpers can also help catch potential
wrong freeing of hibernation slot. (underflow check).
The layout can be changed again afterwards.
How do you think?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/4] mm, swap: don't free a hibernation slot that is in the swap cache
2026-08-08 12:09 ` Kairui Song
@ 2026-08-08 13:26 ` Kairui Song
0 siblings, 0 replies; 8+ messages in thread
From: Kairui Song @ 2026-08-08 13:26 UTC (permalink / raw)
To: Youngjun Park
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Jianyue Wu, her0gyugyu, linux-mm,
linux-kernel
On Sat, Aug 08, 2026 at 08:09:44PM +0800, Kairui Song wrote:
> On Fri, Aug 07, 2026 at 04:06:33AM +0800, Youngjun Park wrote:
> > A slot with a folio in the swap cache is freed when the folio leaves the
> > cache, not when its count drops. swap_put_entries_cluster() follows that
> > rule. swap_free_hibernation_slot() does not, it calls
> > __swap_cluster_free_entries() whether or not a folio sits on the slot.
> >
> > Cluster readahead can put one there. It walks a raw page_cluster sized
> > window of offsets around the faulting entry, and a hibernation slot passes
> > __swap_cache_add_check() because it is not a folio and its count is not
> > zero. Freeing the slot then clears the entry under that folio.
> >
> > The folio is now unreachable from the swap table, and the offset goes back
> > to the allocator. The folio is still on the LRU though, so reclaim can
> > pick it up later. It then takes the old offset out of folio->swap and
> > overwrites the table entry there, which by then may belong to someone else.
> >
> > Check for a cached folio before freeing. The slot is then left in the
> > ordinary state where only the swap cache holds it, and it is freed when the
> > folio leaves the cache, either through the reclaim below or through normal
> > reclaim later.
> >
> > Fixes: 0d6af9bcf383 ("mm, swap: use the swap table to track the swap count")
> > Signed-off-by: Youngjun Park <youngjun.park@lge.com>
> > ---
> > mm/swapfile.c | 9 ++++++++-
> > 1 file changed, 8 insertions(+), 1 deletion(-)
>
> Hi Youngjun,
>
> Thanks a lot for looking into the hibernation issue, I've been thinking
> about using a exclusive type for it, glad to see actual code for it :)
>
> And this patch looks good to me, nice catch!
>
> Acked-by: Kairui Song <kasong@tencent.com>
Oh and I think we need to Cc stable?
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-08-08 13:26 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-06 19:06 [PATCH 0/4] mm, swap: keep hibernation swap slots out of the swap cache Youngjun Park
2026-08-06 19:06 ` [PATCH 1/4] mm, swap: don't free a hibernation slot that is in " Youngjun Park
2026-08-08 12:09 ` Kairui Song
2026-08-08 13:26 ` Kairui Song
2026-08-06 19:06 ` [PATCH 2/4] mm, swap: give hibernation swap slots their own swap table entry type Youngjun Park
2026-08-08 13:25 ` Kairui Song
2026-08-06 19:06 ` [PATCH 3/4] mm, swap: only allow swapped-out slots into the swap cache Youngjun Park
2026-08-06 19:06 ` [PATCH 4/4] mm, swap: drop the swap cache guard and reclaim in swap_free_hibernation_slot() Youngjun Park
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).