From: Kairui Song <ryncsn@gmail.com>
To: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: chrisl@kernel.org, kasong@tencent.com, nphamcs@gmail.com,
baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation
Date: Mon, 20 Jul 2026 17:11:58 +0800 [thread overview]
Message-ID: <al3WqZ45Tapey-eB@KASONG-MC4> (raw)
In-Reply-To: <20260720071342.50742-2-shikemeng@huaweicloud.com>
On Mon, Jul 20, 2026 at 03:13:39PM +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 updating allocated tables in atomic context.
Thanks, that's a really tricky one, good catch.
>
> Fixes: 2fe7a6f5024b8 ("mm/memcg, swap: store cgroup id in cluster table directly")
This commit doesn't exist, you mean b197d41462c2 right?
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
> ---
> mm/swapfile.c | 21 ++++++++++++++++++++-
> 1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 615d90867111..d29062d9c3cd 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -490,6 +490,20 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
> return 0;
> }
>
> +static void swap_cluster_copy_table(struct swap_cluster_info *d_ci,
> + struct swap_cluster_info *s_ci)
Nit: The name is a bit misleading, it not copying the table content,
just filled the d_ci with s_ci. And do we really need a helper for this?
> +{
> + rcu_assign_pointer(d_ci->table, rcu_access_pointer(s_ci->table));
> +
> +#ifdef CONFIG_MEMCG
> + d_ci->memcg_table = s_ci->memcg_table;
> +#endif
> +
> +#if !SWAP_TABLE_HAS_ZEROFLAG
> + d_ci->zero_bitmap = s_ci->zero_bitmap;
> +#endif
> +}
> +
> /*
> * Sanity check to ensure nothing leaked, and the specified range is empty.
> * One special case is that bad slots can't be freed, so check the number of
> @@ -527,6 +541,7 @@ static struct swap_cluster_info *
> swap_cluster_populate(struct swap_info_struct *si,
> struct swap_cluster_info *ci)
> {
> + struct swap_cluster_info tmp_ci;
> int ret;
>
> /*
> @@ -552,7 +567,8 @@ 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(ci, __GFP_HIGH | __GFP_NOMEMALLOC |
> + tmp_ci = *ci;
> + ret = swap_cluster_alloc_table(&tmp_ci, __GFP_HIGH | __GFP_NOMEMALLOC |
> GFP_KERNEL);
This allocation could be largely simplified with mempool, the reason I didn't
use that is due to lack of RCU support, which can be done later to clean
this up.
Please fix the Fixes tags, other parts are mostly fine, thanks agian!
next prev parent reply other threads:[~2026-07-20 9:12 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 7:13 [PATCH 0/4] mm, swap: some random fixes and cleanups Kemeng Shi
2026-07-20 7:13 ` [PATCH 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Kemeng Shi
2026-07-20 9:11 ` Youngjun Park
2026-07-21 1:49 ` Kemeng Shi
2026-07-21 2:15 ` Youngjun Park
2026-07-21 6:12 ` Kemeng Shi
2026-07-20 9:11 ` Kairui Song [this message]
2026-07-21 2:21 ` Kemeng Shi
2026-07-20 7:13 ` [PATCH 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Kemeng Shi
2026-07-20 15:54 ` Luiz Capitulino
2026-07-21 2:19 ` Kemeng Shi
2026-07-20 7:13 ` [PATCH 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Kemeng Shi
2026-07-21 4:07 ` Youngjun Park
2026-07-20 7:13 ` [PATCH 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster() Kemeng Shi
2026-07-21 7:58 ` Youngjun Park
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=al3WqZ45Tapey-eB@KASONG-MC4 \
--to=ryncsn@gmail.com \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=youngjun.park@lge.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