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 70CFC3A782B for ; Mon, 20 Jul 2026 07:13:46 +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=1784531630; cv=none; b=WCzZvJz4K/klRVF6ljJ6bwfJbfWX77o8Qz+cgmmhj1HR5JIdaCqPweDHQJ8BasbGEVd5rQ9STpdrD929z7Kgu3K4GLTgCQCuU/haXvF40JnPDD8cqCaPJEwXJBSr5Z9ULd/+j2QcFBaAJ/LUumylvL3swXlvxiaNDxT0JVKjqCo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784531630; c=relaxed/simple; bh=00GYCO6PUf6bLMEiTn+Snlq4YEX+lXXOA6mXHIG9Qm8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=txU6vt4TB1v6F1cROOBJcANbY13NBRrRu8avMr5M2X2pC/bbtXj4ghgnMxi3HbUoh8nrkUiaFZcvZgXCgvrBNkejnfrFFrNwepVBR6HMFZPF/XyIrFutpOZ6gHJqz4AslQR2fwKdEX+9xz7mppTRX6Zys9dyHoREWur/hucXXX4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass 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=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.170]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4h3WvM1l8CzYQttB for ; Mon, 20 Jul 2026 15:13:19 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 747F54056E for ; Mon, 20 Jul 2026 15:13:44 +0800 (CST) Received: from huaweicloud.com (unknown [10.166.163.161]) by APP3 (Coremail) with UTF8SMTPA id _Ch0CgAX4qKnyl1qax5+Bw--.27530S3; Mon, 20 Jul 2026 15:13:44 +0800 (CST) From: Kemeng Shi To: chrisl@kernel.org, kasong@tencent.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Date: Mon, 20 Jul 2026 15:13:39 +0800 Message-Id: <20260720071342.50742-2-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20260720071342.50742-1-shikemeng@huaweicloud.com> References: <20260720071342.50742-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:_Ch0CgAX4qKnyl1qax5+Bw--.27530S3 X-Coremail-Antispam: 1UD129KBjvJXoWxGF4fXFW7WF1fCFWfXF4kCrg_yoW5XF1kpF 9Yg3s8Kr45Jr9xGrsIy3Wqq34agw40gryDGw1xGr1293ZxXr4xXF1DCrWYvF95CF1kJr90 gr1Utw17ua1q9wUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBE14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1I6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0D M2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjx v20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1l F7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r1q6r 43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_ Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x 0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWx JVW8Jr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUHc_fUUUUU = X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ 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. 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; /* @@ -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); /* @@ -573,6 +589,9 @@ swap_cluster_populate(struct swap_info_struct *si, spin_unlock(&ci->lock); return NULL; } + + /* Update tables in atomic context */ + swap_cluster_copy_table(ci, &tmp_ci); return ci; } -- 2.36.1