From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C929368D65 for ; Tue, 21 Jul 2026 01:49:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784598601; cv=none; b=M+DDEkS1cciWpW850HDCG/O/cD4wRZr/OGcaNLYJKP/s9dMjkuQQYz4ZB6bMJrhtHf0HcHF4PP1zh5Il/n0J2jXy+/1XdiGGIZ9JgEmZXx1gFGdi7WBERkB89fBE0BgLAi2K7QYhLf7uv9ciaIvggBjBtsDd558smUrqVKQqvEc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784598601; c=relaxed/simple; bh=WcotoasjFzEt8PmrQy9dygCNgOdn2iZTjT9aZx2h428=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=DWtVP6gQL3klxkO7siHiROk6xiusxy+k3bQsH858xPRQvb2C/pAJQh4d3yOb0m7/wtEuOPoBN/YiluoewlcnIbNzTN3IYPz4PqpSpEc2UzK9D9hkUEmaYGNXn+oRQTusb3O+typdPQJw/EPwxBhi5ScNeCf6AOSynGO6KRWYT4Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.198]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4h40gD6MrRzYQvLN for ; Tue, 21 Jul 2026 09:49:28 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 6D27840B06 for ; Tue, 21 Jul 2026 09:49:55 +0800 (CST) Received: from [10.166.163.161] (unknown [10.166.163.161]) by APP2 (Coremail) with UTF8SMTPA id Syh0CgBnTChB0F5qlpbtBw--.23847S2; Tue, 21 Jul 2026 09:49:55 +0800 (CST) Message-ID: <0884d36c-06b3-4df1-8fd2-8abc9e6c97ee@huaweicloud.com> Date: Tue, 21 Jul 2026 09:49:53 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation To: Youngjun Park Cc: chrisl@kernel.org, kasong@tencent.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260720071342.50742-1-shikemeng@huaweicloud.com> <20260720071342.50742-2-shikemeng@huaweicloud.com> From: Kemeng Shi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:Syh0CgBnTChB0F5qlpbtBw--.23847S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWFyUuFykCF1ruFW5KF1Utrb_yoWrtF4DpF ZYgas0kF4rXr9xGrsxA3WqgryrWw40gr9rJa1fKr1293ZxZr1fWFn2yrZ0vFyDCF1kJFn0 gr1Yq3sruanYka7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyEb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6x kF7I0E14v26F4UJVW0owAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAq x4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6x CaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI 7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxV Cjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY 6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6x AIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY 1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1veHDUUUUU== X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ 在 2026/7/20 17:11:54, Youngjun Park 写道: > On Mon, Jul 20, 2026 at 03:13:39PM +0800, Kemeng Shi wrote: > > Hello Kemeng Shi, > > Good catch! > > it indeed looks like a possible race condition > (though I haven't verified it at runtime either). > >> 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: > > Here the cluster is isolated (CLUSTER_FLAG_NONE). > >> swap_alloc_fast swap_cluster_populate() >> /* Try a sleep allocation */ >> spin_unlock(&ci->lock); > > And from here, the table becomes visible, > >> swap_cluster_alloc_table() >> rcu_assign_pointer(ci->table, table); > > All entry on this cluster is freed(e.g process dead) , > so this might be the free cluster. > >> ci = swap_cluster_lock(si, offset) >> cluster_is_usable(ci, order) > > Since it's a normal cluster (CLUSTER_FLAG_NONE) and the table exists, > it passes here... > >> 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() > > Nullified > >> rcu_assign_pointer(ci->table, NULL); > > Now it happens. > >> 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. >> >> Fixes: 2fe7a6f5024b8 ("mm/memcg, swap: store cgroup id in cluster table directly") >> Signed-off-by: Kemeng Shi >> --- >> 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) >> +{ >> + 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; > Hello, Youngjun Thanks for feedback.> IMHO, How about resolving everything inside swap_cluster_alloc_table() instead? > > We could consider the following two things. > > - Assign the table at the very end inside swap_cluster_alloc_table(). > - Handle the table freeing properly if subsequent allocations fail. > > Rather than allocating a temp_ci (which adds special handling > for this case), wouldn't it be better to maintain the original intention > of the swap_cluster_alloc_table() function? > > Although this is not a hot path and table allocation will usually succeed with > the ATOMIC allocator, this approach would also save the memcpy overhead > and stack memory usage. Yes, it will be better to handle the issue inside the swap_cluster_alloc_table() and I also consider this before. However, I didn't find a way to properly handle the lock as swap_cluster_alloc_table() will be used for both ATOMIC allocation and sleep allocation. So what about introduce a new helper like: swap_cluster_alloc_table_unlocked() { table = kmem_cache_zalloc() #ifdef CONFIG_MEMCG memcg_table = kzalloc_obj() #endif #if !SWAP_TABLE_HAS_ZEROFLAG zero_bitmap = bitmap_zalloc() #endif spin_lock(&ci->lock); /* update all tables in atomic */ spin_unlock(&ci->lock); } In this way, there are some repeated code, so it is also a little ugly... I will appreciate if you have any better idea! > > (Another option might be checking memcg_table and zero_bitmap inside > cluster_table_is_alloced(). However, I personally dislike this idea as > it might introduce side effects regarding its coverage.) Yes, I also think we should try not to introduce any cost in hot path.> > Your current approach is also a good direction, but please review this > idea as well :) > > Thanks, > Youngjun