* [PATCH v2 0/4] mm, swap: some random fixes and cleanups
@ 2026-08-20 11:55 Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Kemeng Shi @ 2026-08-20 11:55 UTC (permalink / raw)
To: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park
Cc: linux-mm, linux-kernel, Kemeng Shi
Hello all,
This series contains some random fixes and cleanups. More details
can be found in respective patches.
Thanks!
v1->v2:
- Collect RVB from Youngjun nad Youngjun
- Some minor improvements
Kemeng Shi (4):
mm, swap: Fix potential NULL dereference when trying a sleep table
allocation
mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE
initialization
mm, swap: return early from swap_extend_table_try_free() on first
non-zero entry
mm, swap: Remove unneeded swap_extend_table_try_free() in
swap_dup_entries_cluster()
mm/swapfile.c | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)
--
2.36.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation
2026-08-20 11:55 [PATCH v2 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
@ 2026-08-20 11:55 ` Kemeng Shi
2026-08-27 9:10 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Kemeng Shi
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Kemeng Shi @ 2026-08-20 11:55 UTC (permalink / raw)
To: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park
Cc: linux-mm, linux-kernel, Kemeng Shi
The root cause of this issue is because multi-tables are updated in non
atomic context. To be more specific, the issue could be triggerred as
following:
swap_alloc_fast swap_cluster_populate()
/* Try a sleep allocation */
spin_unlock(&ci->lock);
swap_cluster_alloc_table()
rcu_assign_pointer(ci->table, table);
ci = swap_cluster_lock(si, offset)
cluster_is_usable(ci, order)
if (!cluster_table_is_alloced(ci)) // ok
alloc_swap_scan_cluster()
cluster_scan_range()
__swap_table_get()
/* free table when more table allocation fails */
ci->memcg_table = kzalloc_obj(*ci->memcg_table,
gfp);
if (!ci->memcg_table)
swap_cluster_free_table()
rcu_assign_pointer(ci->table, NULL);
table = rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock));
atomic_long_read(&table[off]); // NULL dereference
Fix the issue by making tables visible at end of swap_cluster_populate().
Fixes: 2fe7a6f5024b8 ("mm/memcg, swap: store cgroup id in cluster table directly")
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
mm/swapfile.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 615d90867111..4561c864f806 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -466,8 +466,6 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
if (!table)
return -ENOMEM;
- rcu_assign_pointer(ci->table, table);
-
#ifdef CONFIG_MEMCG
if (!mem_cgroup_disabled()) {
VM_WARN_ON_ONCE(ci->memcg_table);
@@ -487,6 +485,12 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
return -ENOMEM;
}
#endif
+
+ /*
+ * Make tables visible to cluster_is_usable() after everything is
+ * ready.
+ */
+ rcu_assign_pointer(ci->table, table);
return 0;
}
--
2.36.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization
2026-08-20 11:55 [PATCH v2 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
@ 2026-08-20 11:55 ` Kemeng Shi
2026-08-27 9:43 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster() Kemeng Shi
3 siblings, 1 reply; 8+ messages in thread
From: Kemeng Shi @ 2026-08-20 11:55 UTC (permalink / raw)
To: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park
Cc: linux-mm, linux-kernel, Kemeng Shi, Luiz Capitulino
In setup_swap_clusters_info(), SWP_SOLIDSTATE is used to decide
global_cluster allocation. Move setup_swap_clusters_info() after
SWP_SOLIDSTATE initialization to avoid unneeded global_cluster
allocation.
Fixes: 451c6326105b2 ("mm, swap: clean up swapon process and locking")
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Luiz Capitulino <luizcap@redhat.com>
---
mm/swapfile.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 4561c864f806..e9554c28e6eb 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3592,11 +3592,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
maxpages = si->max;
- /* Set up the swap cluster info */
- error = setup_swap_clusters_info(si, swap_header, maxpages);
- if (error)
- goto bad_swap_unlock_inode;
-
if (si->bdev && bdev_stable_writes(si->bdev))
si->flags |= SWP_STABLE_WRITES;
@@ -3610,6 +3605,14 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
inced_nr_rotate_swap = true;
}
+ /*
+ * Set up the swap cluster info after SWP_ flags handling as
+ * setup_swap_clusters_info() checks SWP_SOLIDSTATE.
+ */
+ error = setup_swap_clusters_info(si, swap_header, maxpages);
+ if (error)
+ goto bad_swap_unlock_inode;
+
if ((swap_flags & SWAP_FLAG_DISCARD) &&
si->bdev && bdev_max_discard_sectors(si->bdev)) {
/*
--
2.36.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry
2026-08-20 11:55 [PATCH v2 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Kemeng Shi
@ 2026-08-20 11:55 ` Kemeng Shi
2026-08-27 13:54 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster() Kemeng Shi
3 siblings, 1 reply; 8+ messages in thread
From: Kemeng Shi @ 2026-08-20 11:55 UTC (permalink / raw)
To: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park
Cc: linux-mm, linux-kernel, Kemeng Shi
Return immediately when the first non-zero swap count is found as
any non-zero swap count prevents freeing extend_table and further
iteration is pointless.
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index e9554c28e6eb..9f43ab43b927 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1515,20 +1515,17 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
static void swap_extend_table_try_free(struct swap_cluster_info *ci)
{
unsigned long i;
- bool can_free = true;
if (!ci->extend_table)
return;
for (i = 0; i < SWAPFILE_CLUSTER; i++) {
if (ci->extend_table[i])
- can_free = false;
+ return;
}
- if (can_free) {
- kfree(ci->extend_table);
- ci->extend_table = NULL;
- }
+ kfree(ci->extend_table);
+ ci->extend_table = NULL;
}
/* Decrease the swap count of one slot, without freeing it */
--
2.36.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster()
2026-08-20 11:55 [PATCH v2 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
` (2 preceding siblings ...)
2026-08-20 11:55 ` [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Kemeng Shi
@ 2026-08-20 11:55 ` Kemeng Shi
3 siblings, 0 replies; 8+ messages in thread
From: Kemeng Shi @ 2026-08-20 11:55 UTC (permalink / raw)
To: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park
Cc: linux-mm, linux-kernel, Kemeng Shi
Since commit 0475fde0f68de ("mm, swap: avoid leaving unused extend table
after alloc race"), extend table is always allocated when any swap count
reach MAX - 1 and is always freed when swap count decrease to MAX - 1
or MAX - 2 with cluster lock held. So the extend table will always be
freed properly when decrease swap count in __swap_cluster_put_entry().
So swap_extend_table_try_free() outside of __swap_cluster_put_entry()
is unneeded and can be removed.
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 9f43ab43b927..9f829b008de1 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1717,7 +1717,6 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
failed:
while (ci_off-- > ci_start)
__swap_cluster_put_entry(ci, ci_off);
- swap_extend_table_try_free(ci);
swap_cluster_unlock(ci);
return err;
}
--
2.36.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation
2026-08-20 11:55 ` [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
@ 2026-08-27 9:10 ` Kairui Song
0 siblings, 0 replies; 8+ messages in thread
From: Kairui Song @ 2026-08-27 9:10 UTC (permalink / raw)
To: Kemeng Shi
Cc: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park,
linux-mm, linux-kernel
On Thu, Aug 20, 2026 at 07:55:02PM +0800, Kemeng Shi wrote:
> The root cause of this issue is because multi-tables are updated in non
> atomic context. To be more specific, the issue could be triggerred as
> following:
>
> swap_alloc_fast swap_cluster_populate()
> /* Try a sleep allocation */
> spin_unlock(&ci->lock);
> swap_cluster_alloc_table()
> rcu_assign_pointer(ci->table, table);
>
> ci = swap_cluster_lock(si, offset)
> cluster_is_usable(ci, order)
> if (!cluster_table_is_alloced(ci)) // ok
> alloc_swap_scan_cluster()
> cluster_scan_range()
> __swap_table_get()
>
> /* free table when more table allocation fails */
> ci->memcg_table = kzalloc_obj(*ci->memcg_table,
> gfp);
> if (!ci->memcg_table)
> swap_cluster_free_table()
> rcu_assign_pointer(ci->table, NULL);
>
> table = rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock));
> atomic_long_read(&table[off]); // NULL dereference
>
> Fix the issue by making tables visible at end of swap_cluster_populate().
I think you mean "end of swap_cluster_alloc_table".
>
> Fixes: 2fe7a6f5024b8 ("mm/memcg, swap: store cgroup id in cluster table directly")
Hmm, wrong commit id again, should be
Fixes: b197d41462c2 ("mm/memcg, swap: store cgroup id in cluster table directly")
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
> ---
> mm/swapfile.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 615d90867111..4561c864f806 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -466,8 +466,6 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
> if (!table)
> return -ENOMEM;
>
> - rcu_assign_pointer(ci->table, table);
> -
> #ifdef CONFIG_MEMCG
> if (!mem_cgroup_disabled()) {
> VM_WARN_ON_ONCE(ci->memcg_table);
> @@ -487,6 +485,12 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
> return -ENOMEM;
> }
> #endif
> +
> + /*
> + * Make tables visible to cluster_is_usable() after everything is
> + * ready.
> + */
> + rcu_assign_pointer(ci->table, table);
Hmm, but for the error paths above, they will leak the new allocated table?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization
2026-08-20 11:55 ` [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Kemeng Shi
@ 2026-08-27 9:43 ` Kairui Song
0 siblings, 0 replies; 8+ messages in thread
From: Kairui Song @ 2026-08-27 9:43 UTC (permalink / raw)
To: Kemeng Shi
Cc: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park,
linux-mm, linux-kernel, Luiz Capitulino
On Thu, Aug 20, 2026 at 07:55:03PM +0800, Kemeng Shi wrote:
> In setup_swap_clusters_info(), SWP_SOLIDSTATE is used to decide
> global_cluster allocation. Move setup_swap_clusters_info() after
> SWP_SOLIDSTATE initialization to avoid unneeded global_cluster
> allocation.
>
> Fixes: 451c6326105b2 ("mm, swap: clean up swapon process and locking")
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Thanks!
Acked-by: Kairui Song <kasong@tencent.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry
2026-08-20 11:55 ` [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Kemeng Shi
@ 2026-08-27 13:54 ` Kairui Song
0 siblings, 0 replies; 8+ messages in thread
From: Kairui Song @ 2026-08-27 13:54 UTC (permalink / raw)
To: Kemeng Shi
Cc: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park,
linux-mm, linux-kernel
On Thu, Aug 20, 2026 at 07:55:04PM +0800, Kemeng Shi wrote:
> Return immediately when the first non-zero swap count is found as
> any non-zero swap count prevents freeing extend_table and further
> iteration is pointless.
>
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
> Reviewed-by: Youngjun Park <youngjun.park@lge.com>
> ---
> mm/swapfile.c | 9 +++------
> 1 file changed, 3 insertions(+), 6 deletions(-)
Looks good, nice optimization.
Acked-by: Kairui Song <kasong@tencent.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-08-27 13:54 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-20 11:55 [PATCH v2 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
2026-08-20 11:55 ` [PATCH v2 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
2026-08-27 9:10 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Kemeng Shi
2026-08-27 9:43 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Kemeng Shi
2026-08-27 13:54 ` Kairui Song
2026-08-20 11:55 ` [PATCH v2 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster() Kemeng Shi
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).