From: Nhat Pham <nphamcs@gmail.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Chris Li" <chrisl@kernel.org>,
"Kairui Song" <kasong@tencent.com>,
"Kemeng Shi" <shikemeng@huaweicloud.com>,
"Baoquan He" <baoquan.he@linux.dev>,
"Barry Song" <baohua@kernel.org>,
"Youngjun Park" <youngjun.park@lge.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Yosry Ahmed" <yosry@kernel.org>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Qi Zheng" <qi.zheng@linux.dev>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Axel Rasmussen" <axelrasmussen@google.com>,
"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Michal Hocko" <mhocko@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Hugh Dickins" <hughd@google.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Muchun Song" <muchun.song@linux.dev>,
"Arnd Bergmann" <arnd@arndb.de>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
"Marco Elver" <elver@google.com>,
"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
"Borislav Petkov (AMD)" <bp@alien8.de>,
"Jeff Layton" <jlayton@kernel.org>,
linux-doc@vger.kernel.org, cgroups@vger.kernel.org,
"Tejun Heo" <tj@kernel.org>, "Michal Koutný" <mkoutny@suse.com>
Subject: [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters
Date: Thu, 3 Dec 2026 14:00:10 -0800 [thread overview]
Message-ID: <20261203220010.1302207-21-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>
Physical swap tables hold reverse mappings and marks for bad and raw
hibernation slots. Ordinary swap uses the virtual slot for its cgroup
owner, swap count and zero state. Hibernation accounts its slots to the
root cgroup directly.
Move the cgroup table, overflow count table and zero bitmap pointers
into the vswap cluster descriptor. This saves 16 bytes per physical
cluster on x86-64 with CONFIG_MEMCG without growing the virtual
descriptor. Where the zero flag does not fit in the swap table, allocate
the separate bitmap only for virtual clusters.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/memcontrol-v1.c | 4 +-
mm/memcontrol.c | 6 +-
mm/swap.h | 14 ++--
mm/swap_state.c | 6 +-
mm/swap_table.h | 51 +++++++-----
mm/swapfile.c | 194 ++++++++++++++++++++++++++-------------------
mm/vswap.h | 6 +-
7 files changed, 161 insertions(+), 120 deletions(-)
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 570bb5006328..cd365873861f 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -313,7 +313,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
if (nr_swapfile)
mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
- __swap_cgroup_set(ci, start, nr_entries, private_id);
+ __swap_cgroup_set(ci_dyn, start, nr_entries, private_id);
folio_unqueue_deferred_split(folio);
folio->memcg_data = 0;
@@ -383,8 +383,8 @@ void memcg1_swapin(struct folio *folio)
nr_pages = folio_nr_pages(folio);
start = swp_cluster_offset(folio->swap);
ci = swap_cluster_get_and_lock(folio);
- id = __swap_cgroup_clear(ci, start, nr_pages);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ id = __swap_cgroup_clear(ci_dyn, start, nr_pages);
for (i = start; i < start + nr_pages; i++)
if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
nr_swapfile++;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 03f78a11138f..58478af5696b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6093,7 +6093,7 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
for (i = start; i < end; i++)
if (__swp_tb_get_count(__swap_table_get(ci, i)))
- __swap_cgroup_set(ci, i, 1, id);
+ __swap_cgroup_set(ci_dyn, i, 1, id);
}
#endif
@@ -6185,10 +6185,12 @@ int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages)
*/
void __mem_cgroup_swap_record(struct folio *folio, unsigned short id)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
ci = swap_cluster_get_and_lock(folio);
- __swap_cgroup_set(ci, swp_cluster_offset(folio->swap),
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ __swap_cgroup_set(ci_dyn, swp_cluster_offset(folio->swap),
folio_nr_pages(folio), id);
swap_cluster_unlock(ci);
}
diff --git a/mm/swap.h b/mm/swap.h
index 0d87de6eaddf..1bf4821cdfef 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -57,13 +57,6 @@ struct swap_cluster_info {
u8 flags;
u8 order;
atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */
- unsigned int *extend_table; /* For large swap count, protected by ci->lock */
-#ifdef CONFIG_MEMCG
- struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
-#endif
-#if !SWAP_TABLE_HAS_ZEROFLAG
- unsigned long *zero_bitmap;
-#endif
struct list_head list;
};
@@ -72,6 +65,13 @@ struct swap_cluster_info_dynamic {
unsigned int index; /* for cluster_index() */
struct rcu_head rcu;
atomic_long_t *virtual_table; /* Backing pointers for vswap slots */
+ unsigned int *extend_table; /* For large swap count, protected by ci->lock */
+#ifdef CONFIG_MEMCG
+ struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
+#endif
+#if !SWAP_TABLE_HAS_ZEROFLAG
+ unsigned long *zero_bitmap;
+#endif
};
/* All on-list cluster must have a non-zero flag. */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index b03871d63408..003147bd7ac4 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -201,10 +201,11 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
*/
if (!swp_tb_is_shadow(old_tb) || !__swp_tb_get_count(old_tb))
return -ENOENT;
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
if (shadowp)
*shadowp = swp_tb_to_shadow(old_tb);
if (memcg_id)
- *memcg_id = __swap_cgroup_get(ci, ci_off);
+ *memcg_id = __swap_cgroup_get(ci_dyn, ci_off);
if (nr == 1)
return 0;
@@ -214,7 +215,6 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
* (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in
* __swap_cache_alloc_folio will retry with a smaller order on -EBUSY.
*/
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr), nr, &type);
if (ret != nr || type == VSWAP_ZSWAP)
return -EBUSY;
@@ -240,7 +240,7 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
!swp_tb_is_shadow(old_tb) ||
!__swp_tb_get_count(old_tb) ||
is_zero != __swap_table_test_zero(ci, ci_off) ||
- (memcg_id && *memcg_id != __swap_cgroup_get(ci, ci_off))))
+ (memcg_id && *memcg_id != __swap_cgroup_get(ci_dyn, ci_off))))
return -EBUSY;
} while (++ci_off < ci_end);
diff --git a/mm/swap_table.h b/mm/swap_table.h
index f723ce4f2fca..bd1d9909113c 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -316,8 +316,11 @@ static inline void __swap_table_set_zero(struct swap_cluster_info *ci,
swp_tb |= SWP_TB_ZERO_FLAG;
__swap_table_set(ci, ci_off, swp_tb);
#else
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
lockdep_assert_held(&ci->lock);
- __set_bit(ci_off, ci->zero_bitmap);
+ __set_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
@@ -330,7 +333,10 @@ static inline bool __swap_table_test_zero(struct swap_cluster_info *ci,
VM_WARN_ON(!swp_tb_is_countable(swp_tb));
return !!(swp_tb & SWP_TB_ZERO_FLAG);
#else
- return test_bit(ci_off, ci->zero_bitmap);
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ return test_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
@@ -344,62 +350,67 @@ static inline void __swap_table_clear_zero(struct swap_cluster_info *ci,
swp_tb &= ~SWP_TB_ZERO_FLAG;
__swap_table_set(ci, ci_off, swp_tb);
#else
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
lockdep_assert_held(&ci->lock);
- __clear_bit(ci_off, ci->zero_bitmap);
+ __clear_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
#ifdef CONFIG_MEMCG
-static inline void __swap_cgroup_set(struct swap_cluster_info *ci,
- unsigned int ci_off, unsigned long nr, unsigned short id)
+static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off, unsigned long nr,
+ unsigned short id)
{
- lockdep_assert_held(&ci->lock);
+ lockdep_assert_held(&ci_dyn->ci.lock);
VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER);
- if (WARN_ON_ONCE(!ci->memcg_table))
+ if (WARN_ON_ONCE(!ci_dyn->memcg_table))
return;
do {
- ci->memcg_table->id[ci_off++] = id;
+ ci_dyn->memcg_table->id[ci_off++] = id;
} while (--nr);
}
-static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
- lockdep_assert_held(&ci->lock);
+ lockdep_assert_held(&ci_dyn->ci.lock);
VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER);
- if (unlikely(!ci->memcg_table))
+ if (unlikely(!ci_dyn->memcg_table))
return 0;
- return ci->memcg_table->id[ci_off];
+ return ci_dyn->memcg_table->id[ci_off];
}
-static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off,
unsigned long nr)
{
- unsigned short old = __swap_cgroup_get(ci, ci_off);
+ unsigned short old = __swap_cgroup_get(ci_dyn, ci_off);
if (!old)
return 0;
do {
- VM_WARN_ON_ONCE(ci->memcg_table->id[ci_off] != old);
- ci->memcg_table->id[ci_off++] = 0;
+ VM_WARN_ON_ONCE(ci_dyn->memcg_table->id[ci_off] != old);
+ ci_dyn->memcg_table->id[ci_off++] = 0;
} while (--nr);
return old;
}
#else
-static inline void __swap_cgroup_set(struct swap_cluster_info *ci,
- unsigned int ci_off, unsigned long nr, unsigned short id)
+static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off, unsigned long nr,
+ unsigned short id)
{
}
-static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
return 0;
}
-static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off,
unsigned long nr)
{
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 3f6c56c51c2c..a9ee5bd1f86b 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -443,19 +443,21 @@ static void swap_cluster_free_count_table(struct swap_table *table)
swap_cluster_free_table_folio_rcu_cb);
}
-static void swap_cluster_free_table(struct swap_cluster_info *ci)
+static void swap_cluster_free_table(struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn)
{
struct swap_table *table;
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- kfree(ci->memcg_table);
- ci->memcg_table = NULL;
+ kfree(ci_dyn->memcg_table);
+ ci_dyn->memcg_table = NULL;
#endif
-
#if !SWAP_TABLE_HAS_ZEROFLAG
- kfree(ci->zero_bitmap);
- ci->zero_bitmap = NULL;
+ bitmap_free(ci_dyn->zero_bitmap);
+ ci_dyn->zero_bitmap = NULL;
#endif
+ }
table = (struct swap_table *)rcu_access_pointer(ci->table);
if (!table)
@@ -471,12 +473,12 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
*/
static int swap_cluster_alloc_table(struct swap_info_struct *si,
struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
gfp_t gfp, bool locked)
{
struct swap_table *table = NULL;
#ifdef CONFIG_MEMCG
struct swap_memcg_table *memcg_table = NULL;
- bool vswap = si && swap_is_vswap(si);
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
unsigned long *zero_bitmap = NULL;
@@ -494,19 +496,20 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
if (!table)
goto lock;
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- if (vswap && !mem_cgroup_disabled()) {
- memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
- if (!memcg_table)
- goto lock;
- }
+ if (!mem_cgroup_disabled()) {
+ memcg_table = kzalloc_obj(*memcg_table, gfp);
+ if (!memcg_table)
+ goto lock;
+ }
#endif
-
#if !SWAP_TABLE_HAS_ZEROFLAG
- zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
- if (!zero_bitmap)
- goto lock;
+ zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
+ if (!zero_bitmap)
+ goto lock;
#endif
+ }
ret = 0;
lock:
@@ -528,14 +531,16 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
/* The cluster must be empty and not on any list during allocation. */
VM_WARN_ON_ONCE(!cluster_is_empty(ci));
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- VM_WARN_ON_ONCE(ci->memcg_table);
- ci->memcg_table = memcg_table;
+ VM_WARN_ON_ONCE(ci_dyn->memcg_table);
+ ci_dyn->memcg_table = memcg_table;
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
- VM_WARN_ON_ONCE(ci->zero_bitmap);
- ci->zero_bitmap = zero_bitmap;
+ VM_WARN_ON_ONCE(ci_dyn->zero_bitmap);
+ ci_dyn->zero_bitmap = zero_bitmap;
#endif
+ }
/*
* Make tables visible to cluster_is_usable() after everything is
@@ -562,6 +567,7 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
* bad slots for swapoff, and non-swapoff path must never free bad slots.
*/
static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off, unsigned int nr,
bool swapoff)
{
@@ -578,11 +584,12 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
bad_slots++;
else
WARN_ON_ONCE(!swp_tb_is_null(swp_tb));
- WARN_ON_ONCE(__swap_cgroup_get(ci, ci_off));
+ if (ci_dyn)
+ WARN_ON_ONCE(__swap_cgroup_get(ci_dyn, ci_off));
} while (++ci_off < ci_end);
WARN_ON_ONCE(bad_slots != (swapoff ? ci->count : 0));
- WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci->extend_table);
+ WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci_dyn && ci_dyn->extend_table);
}
/*
@@ -606,7 +613,7 @@ swap_cluster_populate(struct swap_info_struct *si,
lockdep_assert_held(&si->global_cluster_lock);
lockdep_assert_held(&ci->lock);
- if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ if (!swap_cluster_alloc_table(si, ci, NULL, __GFP_HIGH | __GFP_NOMEMALLOC |
__GFP_NOWARN, true))
return ci;
@@ -620,7 +627,7 @@ swap_cluster_populate(struct swap_info_struct *si,
spin_unlock(&si->global_cluster_lock);
local_unlock(&percpu_swap_cluster.lock);
- ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ ret = swap_cluster_alloc_table(si, ci, NULL, __GFP_HIGH | __GFP_NOMEMALLOC |
GFP_KERNEL, false);
/*
@@ -674,14 +681,15 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si,
static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci)
{
- swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false);
- swap_cluster_free_table(ci);
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
- if (swap_is_vswap(si)) {
- struct swap_cluster_info_dynamic *ci_dyn;
+ if (swap_is_vswap(si))
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ swap_cluster_assert_empty(ci, ci_dyn, 0, SWAPFILE_CLUSTER, false);
+ swap_cluster_free_table(ci, ci_dyn);
+ if (ci_dyn) {
/* vswap clusters are destroyed, not returned to free_clusters. */
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
xa_erase(&si->cluster_info_pool, ci_dyn->index);
move_cluster(si, ci, NULL, CLUSTER_FLAG_DEAD);
vswap_cluster_free_vtable(ci);
@@ -910,7 +918,11 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
need_table = !ci->count;
/* Need to allocate swap table first for initial bad slot marking. */
if (need_table) {
- ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
+
+ if (alloc_si)
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = swap_cluster_alloc_table(alloc_si, ci, ci_dyn, GFP_KERNEL, false);
if (ret)
goto out;
} else {
@@ -1088,10 +1100,14 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
* The slot carries no swap count and is freed by offset.
*/
if (likely(folio)) {
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
+
order = folio_order(folio);
nr_pages = 1 << order;
- swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
- if (!swap_is_vswap(si)) {
+ if (swap_is_vswap(si))
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ swap_cluster_assert_empty(ci, ci_dyn, ci_off, nr_pages, false);
+ if (!ci_dyn) {
/*
* Folio already in the swap cache: we are allocating
* physical backing for its vswap entry. Point each
@@ -1115,7 +1131,7 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
} else if (IS_ENABLED(CONFIG_HIBERNATION)) {
order = 0;
nr_pages = 1;
- swap_cluster_assert_empty(ci, ci_off, 1, false);
+ swap_cluster_assert_empty(ci, NULL, ci_off, 1, false);
/* Exclusively owned by hibernation, must never enter the swap cache */
__swap_table_set(ci, ci_off, SWP_TB_HIB);
} else {
@@ -1249,7 +1265,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
- if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
+ if (swap_cluster_alloc_table(si, ci, ci_dyn, GFP_ATOMIC | __GFP_NOWARN, true)) {
spin_unlock(&ci->lock);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1257,7 +1273,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
if (vswap_cluster_alloc_vtable(ci_dyn, GFP_ATOMIC | __GFP_NOWARN)) {
spin_unlock(&ci->lock);
- swap_cluster_free_table(ci);
+ swap_cluster_free_table(ci, ci_dyn);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
}
@@ -1266,7 +1282,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
XA_LIMIT(1, DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER) - 1),
GFP_ATOMIC | __GFP_NOWARN)) {
spin_unlock(&ci->lock);
- swap_cluster_free_table(&ci_dyn->ci);
+ swap_cluster_free_table(ci, ci_dyn);
vswap_cluster_free_vtable(&ci_dyn->ci);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1718,14 +1734,14 @@ static bool swap_sync_discard(void)
return false;
}
-static int swap_extend_table_alloc(struct swap_info_struct *si,
- struct swap_cluster_info *ci,
+static int swap_extend_table_alloc(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off, gfp_t gfp)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
void *table;
- table = kzalloc(sizeof(ci->extend_table[0]) * SWAPFILE_CLUSTER, gfp);
+ table = kzalloc(sizeof(ci_dyn->extend_table[0]) * SWAPFILE_CLUSTER, gfp);
if (!table)
return -ENOMEM;
@@ -1741,10 +1757,10 @@ static int swap_extend_table_alloc(struct swap_info_struct *si,
count = swp_tb_get_count(__swap_table_get(ci, ci_off));
if (count < (SWP_TB_COUNT_MAX - 1))
goto out_free;
- if (ci->extend_table)
+ if (ci_dyn->extend_table)
goto out_free;
- ci->extend_table = table;
+ ci_dyn->extend_table = table;
spin_unlock(&ci->lock);
return 0;
@@ -1759,6 +1775,7 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
int ret;
struct swap_info_struct *si;
struct swap_cluster_info *ci;
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned long offset = swp_offset(entry);
si = get_swap_device(entry);
@@ -1767,32 +1784,34 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
/* The source PTE pins the entry, so its cluster is alive. */
ci = __swap_offset_to_cluster(si, offset);
- ret = swap_extend_table_alloc(si, ci, swp_cluster_offset(entry), gfp);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = swap_extend_table_alloc(ci_dyn, swp_cluster_offset(entry), gfp);
put_swap_device(si);
return ret;
}
-static void swap_extend_table_try_free(struct swap_cluster_info *ci)
+static void swap_extend_table_try_free(struct swap_cluster_info_dynamic *ci_dyn)
{
unsigned long i;
- if (!ci->extend_table)
+ if (!ci_dyn->extend_table)
return;
for (i = 0; i < SWAPFILE_CLUSTER; i++) {
- if (ci->extend_table[i])
+ if (ci_dyn->extend_table[i])
return;
}
- kfree(ci->extend_table);
- ci->extend_table = NULL;
+ kfree(ci_dyn->extend_table);
+ ci_dyn->extend_table = NULL;
}
/* Decrease the swap count of one slot, without freeing it */
-static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
- unsigned int ci_off)
+static void __swap_cluster_put_entry(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
unsigned long swp_tb;
@@ -1804,15 +1823,15 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
VM_WARN_ON_ONCE(count > SWP_TB_COUNT_MAX);
if (count == SWP_TB_COUNT_MAX) {
- count = ci->extend_table[ci_off];
+ count = ci_dyn->extend_table[ci_off];
/* Overflow starts with SWP_TB_COUNT_MAX */
VM_WARN_ON_ONCE(count < SWP_TB_COUNT_MAX);
count--;
if (count == (SWP_TB_COUNT_MAX - 1)) {
- ci->extend_table[ci_off] = 0;
+ ci_dyn->extend_table[ci_off] = 0;
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count));
} else {
- ci->extend_table[ci_off] = count;
+ ci_dyn->extend_table[ci_off] = count;
}
} else {
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, --count));
@@ -1825,7 +1844,7 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
* also possible that a pending dup just attached an extend table.
*/
if (unlikely(count == SWP_TB_COUNT_MAX - 2 || count == SWP_TB_COUNT_MAX - 1))
- swap_extend_table_try_free(ci);
+ swap_extend_table_try_free(ci_dyn);
}
/**
@@ -1845,6 +1864,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
pgoff_t offset, int nr,
bool reclaim_cache)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
unsigned int ci_off, ci_end;
pgoff_t end = offset + nr;
@@ -1854,6 +1874,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
int ci_batch = -1;
ci = swap_cluster_lock(si, offset);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
ci_off = offset % SWAPFILE_CLUSTER;
ci_end = ci_off + nr;
do {
@@ -1875,7 +1896,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
* slots will be freed when folio is removed from swap cache
* (__swap_cache_del_folio).
*/
- __swap_cluster_put_entry(ci, ci_off);
+ __swap_cluster_put_entry(ci_dyn, ci_off);
if (ci_batch != -1) {
__swap_cluster_free_entries(si, ci, ci_batch, ci_off - ci_batch);
ci_batch = -1;
@@ -1898,16 +1919,17 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
}
/* Increase the swap count of one slot. */
-static int __swap_cluster_dup_entry(struct swap_cluster_info *ci,
+static int __swap_cluster_dup_entry(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
unsigned long swp_tb;
lockdep_assert_held(&ci->lock);
swp_tb = __swap_table_get(ci, ci_off);
/* Bad or special slots can't be handled */
- if (WARN_ON_ONCE(swp_tb_is_bad(swp_tb)))
+ if (WARN_ON_ONCE(!swp_tb_is_countable(swp_tb)))
return -EINVAL;
count = __swp_tb_get_count(swp_tb);
/* Must be either cached or have a count already */
@@ -1916,19 +1938,19 @@ static int __swap_cluster_dup_entry(struct swap_cluster_info *ci,
if (likely(count < (SWP_TB_COUNT_MAX - 1))) {
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count + 1));
- VM_WARN_ON_ONCE(ci->extend_table && ci->extend_table[ci_off]);
+ VM_WARN_ON_ONCE(ci_dyn->extend_table && ci_dyn->extend_table[ci_off]);
} else if (count == (SWP_TB_COUNT_MAX - 1)) {
- if (ci->extend_table) {
- VM_WARN_ON_ONCE(ci->extend_table[ci_off]);
- ci->extend_table[ci_off] = SWP_TB_COUNT_MAX;
+ if (ci_dyn->extend_table) {
+ VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off]);
+ ci_dyn->extend_table[ci_off] = SWP_TB_COUNT_MAX;
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, SWP_TB_COUNT_MAX));
} else {
return -ENOMEM;
}
} else if (count == SWP_TB_COUNT_MAX) {
- VM_WARN_ON_ONCE(ci->extend_table[ci_off] >=
- type_max(typeof(ci->extend_table[0])));
- ++ci->extend_table[ci_off];
+ VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off] >=
+ type_max(typeof(ci_dyn->extend_table[0])));
+ ++ci_dyn->extend_table[ci_off];
} else {
/* Never happens unless counting went wrong */
WARN_ON_ONCE(1);
@@ -1952,6 +1974,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
pgoff_t offset, int nr)
{
int err;
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
unsigned int ci_start, ci_off, ci_end;
@@ -1959,13 +1982,14 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
ci_end = ci_start + nr;
ci_off = ci_start;
ci = swap_cluster_lock(si, offset);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
restart:
do {
- err = __swap_cluster_dup_entry(ci, ci_off);
+ err = __swap_cluster_dup_entry(ci_dyn, ci_off);
if (unlikely(err)) {
if (err == -ENOMEM) {
spin_unlock(&ci->lock);
- err = swap_extend_table_alloc(si, ci, ci_off, GFP_ATOMIC);
+ err = swap_extend_table_alloc(ci_dyn, ci_off, GFP_ATOMIC);
spin_lock(&ci->lock);
if (!err)
goto restart;
@@ -1978,7 +2002,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return 0;
failed:
while (ci_off-- > ci_start)
- __swap_cluster_put_entry(ci, ci_off);
+ __swap_cluster_put_entry(ci_dyn, ci_off);
swap_cluster_unlock(ci);
return err;
}
@@ -2216,11 +2240,11 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
lockdep_assert_held(&ci->lock);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
- batch_id = __swap_cgroup_get(ci, ci_start);
+ batch_id = __swap_cgroup_get(ci_dyn, ci_start);
for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
vt = __vtable_get(ci_dyn, ci_off);
- cur_id = __swap_cgroup_get(ci, ci_off);
+ cur_id = __swap_cgroup_get(ci_dyn, ci_off);
if (cur_id != batch_id) {
mem_cgroup_swap_uncharge(batch_id, batch_nr,
@@ -2391,7 +2415,7 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
spin_unlock(&ci->lock);
return vtable_to_phys(vt);
}
- memcg_id = __swap_cgroup_get(ci, voff);
+ memcg_id = __swap_cgroup_get(ci_dyn, voff);
spin_unlock(&ci->lock);
phys_entry = folio_alloc_phys_swap(folio);
@@ -2558,12 +2582,13 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
*/
static void __swap_cluster_finish_free(struct swap_info_struct *si,
struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_start,
unsigned int nr_pages)
{
lockdep_assert_held(&ci->lock);
swap_range_free(si, cluster_offset(si, ci) + ci_start, nr_pages);
- swap_cluster_assert_empty(ci, ci_start, nr_pages, false);
+ swap_cluster_assert_empty(ci, ci_dyn, ci_start, nr_pages, false);
if (!ci->count)
free_cluster(si, ci);
@@ -2582,12 +2607,9 @@ static void __swap_cluster_free_phys_slots(struct swap_info_struct *si,
lockdep_assert_held(&ci->lock);
VM_WARN_ON(ci->count < nr_pages);
ci->count -= nr_pages;
- for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) {
+ for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++)
__swap_table_set(ci, ci_off, null_to_swp_tb());
- if (!SWAP_TABLE_HAS_ZEROFLAG)
- __swap_table_clear_zero(ci, ci_off);
- }
- __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
+ __swap_cluster_finish_free(si, ci, NULL, ci_start, nr_pages);
}
/*
@@ -2611,6 +2633,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
struct swap_cluster_info *ci,
unsigned int ci_start, unsigned int nr_pages)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned long old_tb;
unsigned short batch_id = 0, id_cur;
unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
@@ -2618,6 +2641,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
VM_WARN_ON(ci->count < nr_pages);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
__vswap_release_backing(ci, ci_start, nr_pages);
ci->count -= nr_pages;
@@ -2640,7 +2664,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
* backing of vswap entries was already uncharged by
* __vswap_release_backing().
*/
- id_cur = __swap_cgroup_clear(ci, ci_off, 1);
+ id_cur = __swap_cgroup_clear(ci_dyn, ci_off, 1);
if (batch_id != id_cur) {
if (batch_id)
mem_cgroup_swap_put(batch_id,
@@ -2653,7 +2677,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
if (batch_id)
mem_cgroup_swap_put(batch_id, ci_off - batch_off);
- __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
+ __swap_cluster_finish_free(si, ci, ci_dyn, ci_start, nr_pages);
}
int __swap_count(swp_entry_t entry)
@@ -2700,8 +2724,12 @@ int swp_swapcount(swp_entry_t entry)
ci = swap_cluster_lock(si, swp_offset(entry));
swp_tb = __swap_table_get(ci, swp_cluster_offset(entry));
count = swp_tb_get_count(swp_tb);
- if (count == SWP_TB_COUNT_MAX)
- count = ci->extend_table[swp_cluster_offset(entry)];
+ if (count == SWP_TB_COUNT_MAX) {
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ count = ci_dyn->extend_table[swp_cluster_offset(entry)];
+ }
swap_cluster_unlock(ci);
put_swap_device(si);
@@ -3879,9 +3907,9 @@ static void free_swap_cluster_info(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
if (cluster_table_is_alloced(ci)) {
- swap_cluster_assert_empty(ci, 0,
+ swap_cluster_assert_empty(ci, ci_dyn, 0,
SWAPFILE_CLUSTER, true);
- swap_cluster_free_table(ci);
+ swap_cluster_free_table(ci, ci_dyn);
}
spin_unlock(&ci->lock);
vswap_cluster_free_vtable(ci);
@@ -3898,8 +3926,8 @@ static void free_swap_cluster_info(struct swap_info_struct *si,
/* Cluster with bad marks count will have a remaining table */
spin_lock(&ci->lock);
if (cluster_table_is_alloced(ci)) {
- swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true);
- swap_cluster_free_table(ci);
+ swap_cluster_assert_empty(ci, NULL, 0, SWAPFILE_CLUSTER, true);
+ swap_cluster_free_table(ci, NULL);
}
spin_unlock(&ci->lock);
}
diff --git a/mm/vswap.h b/mm/vswap.h
index f3a55414c6b7..3af7d73b7b1c 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -107,9 +107,9 @@ static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
* reads atomic; it does not permit backend changes. Revalidate speculative
* reads under the folio lock before using them.
*
- * Zero-backed slots use the swap_table per-slot zero flag (same as
- * direct-mapped physical swap), via __swap_table_test_zero() and friends,
- * which fall back to ci->zero_bitmap where the flag does not fit. Cached
+ * Zero-backed slots use the swap_table per-slot zero flag via
+ * __swap_table_test_zero() and friends, which fall back to
+ * ci_dyn->zero_bitmap where the flag does not fit. Cached
* folios are read out of the swap_table PFN entry; there is no separate FOLIO
* vtable type because the folio pointer would duplicate that PFN and
* would go stale on folio migration / split.
--
2.53.1-Meta
next prev parent reply other threads:[~2026-12-03 22:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
2026-12-03 21:59 ` [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
2026-12-03 22:00 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
2026-12-03 22:00 ` Nhat Pham [this message]
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
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=20261203220010.1302207-21-nphamcs@gmail.com \
--to=nphamcs@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bp@alien8.de \
--cc=cgroups@vger.kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=dapeng1.mi@linux.intel.com \
--cc=david@kernel.org \
--cc=elver@google.com \
--cc=enelsonmoore@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jlayton@kernel.org \
--cc=kasong@tencent.com \
--cc=kmehltretter@gmail.com \
--cc=liam@infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=qi.zheng@linux.dev \
--cc=rdunlap@infradead.org \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=skhan@linuxfoundation.org \
--cc=tj@kernel.org \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
--cc=yuanchu@google.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