From nobody Fri Sep 25 23:54:11 2026 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 216BB448391 for ; Mon, 7 Sep 2026 09:14:07 +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=1788772451; cv=none; b=Of2jSYK1J/P1o7Mw0nePUfygYA/ycgzjIzU6A3PoW6bmuXVhK6u4VnasyablE4zT13DQPL4CjOaPStyoEKM9bVjpzuCxJ/BD3YmIqbrSbbH6lUjFq7gWU+HN4QejiOEG4AaU5dj15yB0C/TIg99MvjjcqQ3kJwIn3aPK8yXiMUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772451; c=relaxed/simple; bh=EuwCH11R92TYqx8qAsMflYyIBm9cROqmFL3OH0Qnt50=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=OaJyKc1JMYgWANrPZa8FjVifZLsZqDDX5NcUegI0Oc4IQjwMjdTMCUhuTLCkYr3StEn0FIyrFGhsU59HHAxImVW2vFqXma/1N1Ls7rqPPuRHwRXL8uiX4e8TF3ZJueoU7a6nuZUPDbrXHscbItRnwOGCCSV08Fjp5/tv8GeZi4o= 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.198]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4hdhF32yfPzYQwPy for ; Mon, 7 Sep 2026 17:13:11 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id E711140577 for ; Mon, 7 Sep 2026 17:14:00 +0800 (CST) Received: from huaweicloud.com (unknown [10.166.163.161]) by APP3 (Coremail) with UTF8SMTPA id _Ch0CgCnZbNWgJ5qMElGBA--.7816S3; Mon, 07 Sep 2026 17:14:00 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org, chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kemeng Shi Subject: [PATCH v3 1/4] mm, swap: Fix potential NULL dereference when trying a sleep table allocation Date: Mon, 7 Sep 2026 17:13:53 +0800 Message-Id: <20260907091356.53026-2-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20260907091356.53026-1-shikemeng@huaweicloud.com> References: <20260907091356.53026-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: quoted-printable X-CM-TRANSID: _Ch0CgCnZbNWgJ5qMElGBA--.7816S3 X-Coremail-Antispam: 1UD129KBjvJXoWxGF4fXFW7WF1fCFWfJF1fJFb_yoWrWFWDpF nYgF98Kr4rXr9xWrsxAw1Dt34rWw4ru34DGayxGw1293ZxXrWkWFWxAr9xZF98CF97Jryq gr1jgw17uF4vvwUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBl14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1I6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r1q6r43Mx AIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_ Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwI xGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWx JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcV C2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbn2-7UUUUU== 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 =3D swap_cluster_lock(si, offset) cluster_is_usable(ci, order=EF=BC=89 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 =3D kzalloc_obj(*ci->memcg_table, gfp); if (!ci->memcg_table) swap_cluster_free_table() rcu_assign_pointer(ci->table, NULL); table =3D rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock)); atomic_long_read(&table[off]); // NULL dereference Since memory order guarantee between ci->table, as well as between ci->table and ci->zero_bitmap, fix the issue by making tables visible at the end of swap_cluster_populate(). Current memory order guarantee is as following: On write side: rcu_assign_pointer(ci->table, table) will offer release to ensure zero_bitmap and memcg_table visible before ci->table. On read side: folio_alloc_swap swap_alloc_fast/swap_alloc_slow /* ci->table: protected by cluster lock */ swap_cluster_lock cluster_is_usable ... __swap_table_set ... swap_cluster_unlock mem_cgroup_try_charge_swap ... /* memcg_table: protected by cluster lock */ swap_cluster_get_and_lock __swap_cgroup_set swap_cluster_unlock swap_writeout swap_zeromap_folio_set /* zero_bitmap: protected by cluster lock */ swap_cluster_get_and_lock __swap_table_set_zero swap_cluster_unlock Fixes: b197d41462c2 ("mm/memcg, swap: store cgroup id in cluster table dire= ctly") Signed-off-by: Kemeng Shi --- mm/swapfile.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 53bf01d5f7f1..45b6154f85ad 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -418,6 +418,17 @@ static void swap_cluster_free_table_folio_rcu_cb(struc= t rcu_head *head) folio_put(folio); } =20 +static void swap_cluster_free_count_table(struct swap_table *table) +{ + if (!SWP_TABLE_USE_PAGE) { + kmem_cache_free(swap_table_cachep, table); + return; + } + + call_rcu(&(folio_page(virt_to_folio(table), 0)->rcu_head), + swap_cluster_free_table_folio_rcu_cb); +} + static void swap_cluster_free_table(struct swap_cluster_info *ci) { struct swap_table *table; @@ -437,13 +448,7 @@ static void swap_cluster_free_table(struct swap_cluste= r_info *ci) return; =20 rcu_assign_pointer(ci->table, NULL); - if (!SWP_TABLE_USE_PAGE) { - kmem_cache_free(swap_table_cachep, table); - return; - } - - call_rcu(&(folio_page(virt_to_folio(table), 0)->rcu_head), - swap_cluster_free_table_folio_rcu_cb); + swap_cluster_free_count_table(table); } =20 static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gf= p) @@ -466,14 +471,12 @@ static int swap_cluster_alloc_table(struct swap_clust= er_info *ci, gfp_t gfp) if (!table) return -ENOMEM; =20 - rcu_assign_pointer(ci->table, table); - #ifdef CONFIG_MEMCG if (!mem_cgroup_disabled()) { VM_WARN_ON_ONCE(ci->memcg_table); ci->memcg_table =3D kzalloc_obj(*ci->memcg_table, gfp); if (!ci->memcg_table) { - swap_cluster_free_table(ci); + swap_cluster_free_count_table(table); return -ENOMEM; } } @@ -484,9 +487,16 @@ static int swap_cluster_alloc_table(struct swap_cluste= r_info *ci, gfp_t gfp) ci->zero_bitmap =3D bitmap_zalloc(SWAPFILE_CLUSTER, gfp); if (!ci->zero_bitmap) { swap_cluster_free_table(ci); + swap_cluster_free_count_table(table); return -ENOMEM; } #endif + + /* + * Make tables visible to cluster_is_usable() after everything is + * ready. + */ + rcu_assign_pointer(ci->table, table); return 0; } =20 --=20 2.36.1 From nobody Fri Sep 25 23:54:11 2026 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (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 A856A44C51A for ; Mon, 7 Sep 2026 09:14:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772453; cv=none; b=Nrf2KVT7J095OmVobsDv1hFK+2LmsV6IC6NGnSYhuUnQ96T8Wv4uBOD0xFl8YjMqbonrzKeRTOzs3LoXsq7hHjiG+YAnZCsDos5BNq2QYwjw57HZJPPsDs4FBa1H9maa/qvbhZGVZzTBrI4wFoSTzzg5UjTzu8FwFvwSJW0ocsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772453; c=relaxed/simple; bh=kMlxtO0vxz7iyrenf5B99YNHUpSGDHuLzd6/P+E+sIA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AePiAUJ8434voDR039bpBrLXmWO9VSp2hF8dPFpOL7pOhpoRgytr8aoHKxy1Al6ANMf6AkfDtrtnR33+8SWZf7Dx4x2MmWJiVdPX3vdxyS7zxVA/NmovlqMx3Ku4DIgCU41okYpadmh0nDOh2rDBOkSvci14XCwnpTp972X3sic= 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.56 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 dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4hdhDs40pfzKHPRM for ; Mon, 7 Sep 2026 17:13:01 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 865E94056D for ; Mon, 7 Sep 2026 17:14:01 +0800 (CST) Received: from huaweicloud.com (unknown [10.166.163.161]) by APP3 (Coremail) with UTF8SMTPA id _Ch0CgCnZbNWgJ5qMElGBA--.7816S4; Mon, 07 Sep 2026 17:14:01 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org, chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kemeng Shi , Luiz Capitulino , Kairui Song Subject: [PATCH v3 2/4] mm, swap: Move setup_swap_clusters_info() after SWP_SOLIDSTATE initialization Date: Mon, 7 Sep 2026 17:13:54 +0800 Message-Id: <20260907091356.53026-3-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20260907091356.53026-1-shikemeng@huaweicloud.com> References: <20260907091356.53026-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-Transfer-Encoding: quoted-printable X-CM-TRANSID: _Ch0CgCnZbNWgJ5qMElGBA--.7816S4 X-Coremail-Antispam: 1UD129KBjvJXoW7urWrJFy7Zr4xur1kKw1ftFb_yoW8XFy3p3 Z5Gas8Cw48JF1fWa18Zw1UW34rWwn5KF4UJF4avr43uwn7Jr10gryrt3Z8Ar1DGrn5AFWD Ww1I934xuF1Y9wUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmj14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUj5Ef7UUUUU== X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Luiz Capitulino Acked-by: Kairui Song --- mm/swapfile.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 45b6154f85ad..767d83d89d0b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3731,11 +3731,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) =20 maxpages =3D si->max; =20 - /* Set up the swap cluster info */ - error =3D 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 |=3D SWP_STABLE_WRITES; =20 @@ -3749,6 +3744,14 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) inced_nr_rotate_swap =3D true; } =20 + /* + * Set up the swap cluster info after SWP_ flags handling as + * setup_swap_clusters_info() checks SWP_SOLIDSTATE. + */ + error =3D 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)) { /* --=20 2.36.1 From nobody Fri Sep 25 23:54:11 2026 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (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 8F8C2448D01 for ; Mon, 7 Sep 2026 09:14:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772454; cv=none; b=rk6w/Lajd3cRmnkw/EeaJ2oxZ+gNvSXIWvFJ2ot/NOjv8wbEMFQKVbivt1X9IRjzNkkD2oOmFXK1BOmSQKJPjX9RnVy6Zz0+Tqkby6ccungxmDXa+1jNSTAuszQMxZoZIaTahazKaQicSouW1TWYqK3R56gRcDTo2LI5z56hczs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772454; c=relaxed/simple; bh=2jkM/aXnfe6UELFef2CwP2z67BUn60uRKDwPOtEudKs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mgOXRW3bEJ0TVSQ4GJ0juUL2rHr5/UKXYLfizJPMVsX8BRF2JQri6LiQNIGPD5w5bWYUv6gTrJ9DIt+wvsFomv3Ly0+YCOCknFJ/OZ1rtrKi8t/1qFDBR5xeHncincQ/FHRu86DGE+zAiQHj0+wj1+lpy2atqSO9Z2J0OBteiLs= 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.56 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.170]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4hdhDt0JmfzKHPbW for ; Mon, 7 Sep 2026 17:13:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 06BB44056D for ; Mon, 7 Sep 2026 17:14:02 +0800 (CST) Received: from huaweicloud.com (unknown [10.166.163.161]) by APP3 (Coremail) with UTF8SMTPA id _Ch0CgCnZbNWgJ5qMElGBA--.7816S5; Mon, 07 Sep 2026 17:14:01 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org, chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kemeng Shi , Kairui Song Subject: [PATCH v3 3/4] mm, swap: return early from swap_extend_table_try_free() on first non-zero entry Date: Mon, 7 Sep 2026 17:13:55 +0800 Message-Id: <20260907091356.53026-4-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20260907091356.53026-1-shikemeng@huaweicloud.com> References: <20260907091356.53026-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-Transfer-Encoding: quoted-printable X-CM-TRANSID: _Ch0CgCnZbNWgJ5qMElGBA--.7816S5 X-Coremail-Antispam: 1UD129KBjvdXoWrtw1Dur1rAF1rKF15Ar1xGrg_yoWDKwb_Ca 4Ivws3JFW3trn7X397tasrXr1fGr4fK3Z5uF97tFyayry5X3y5uF9rXF4DZw48Ka1IyF9a y3Wvvr17Kw42yjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbkxFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k26cxKx2IYs7xG 6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWwA2048vs2IY02 0Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUCVW8JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1l84 ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUbpwZ7UUUUU== X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Youngjun Park Acked-by: Kairui Song --- mm/swapfile.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 767d83d89d0b..b73f34ff44f8 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1527,20 +1527,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 =3D true; =20 if (!ci->extend_table) return; =20 for (i =3D 0; i < SWAPFILE_CLUSTER; i++) { if (ci->extend_table[i]) - can_free =3D false; + return; } =20 - if (can_free) { - kfree(ci->extend_table); - ci->extend_table =3D NULL; - } + kfree(ci->extend_table); + ci->extend_table =3D NULL; } =20 /* Decrease the swap count of one slot, without freeing it */ --=20 2.36.1 From nobody Fri Sep 25 23:54:11 2026 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 DACDC448393 for ; Mon, 7 Sep 2026 09:14:06 +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=1788772449; cv=none; b=WKSxC6TSSa+Vh71r74rUW8Mdyhav0hVHQsfiRSEW9QWzQuK6VlEoRWR1VpNK/486iQmAPrbIOYiAFiplJ9t0esvOqtDu7XQRqJEBzd4auDWSaURciPKgEok4rA7rvBt7CdrgTXz1t4vdS3g2sy3PJnERydE4pa8mLtMs+knLNOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788772449; c=relaxed/simple; bh=PM6zuGMhk0g9CuM/kw01q+sl3bgp4A53kP4BilzK/Ew=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UCHH0LwAZNbHrcDnJ2AlM8HfTRf0CFJy//go+7cBlwdN6OfRpJ/0f7ivZRY/FaU0BXK/n033bTyT7+P+GrKybQmg5T3Y9Z6MzbK6OXSpX7Qohl1ys+aztnuP0qHLPKfiIK5foNvWKqWzQVZ1S5ybGld8aIanC7sdHCAWqkyQodI= 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.198]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4hdhF46kvFzYQwrg for ; Mon, 7 Sep 2026 17:13:12 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 728D240576 for ; Mon, 7 Sep 2026 17:14:02 +0800 (CST) Received: from huaweicloud.com (unknown [10.166.163.161]) by APP3 (Coremail) with UTF8SMTPA id _Ch0CgCnZbNWgJ5qMElGBA--.7816S6; Mon, 07 Sep 2026 17:14:02 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org, chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kemeng Shi Subject: [PATCH v3 4/4] mm, swap: Remove unneeded swap_extend_table_try_free() in swap_dup_entries_cluster() Date: Mon, 7 Sep 2026 17:13:56 +0800 Message-Id: <20260907091356.53026-5-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20260907091356.53026-1-shikemeng@huaweicloud.com> References: <20260907091356.53026-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-Transfer-Encoding: quoted-printable X-CM-TRANSID: _Ch0CgCnZbNWgJ5qMElGBA--.7816S6 X-Coremail-Antispam: 1UD129KBjvdXoWruw1rGrW7uF17KFyDAFWrZrb_yoWDZrb_ua 95A34kJr45Ja1DG3s3t3W2qrWvgrZYgr1Ykr1rtFnaya4UXFWrWF17W3srA3s2q390v3s5 Ca1vvF1Sy39rGjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUb6AFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k26cxKx2IYs7xG 6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2 IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28E F7xvwVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr 1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0D M2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjx v20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1l F7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r1q6r 43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_ Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x 0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8 JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIx AIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbPC7UUUUUU= = X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Youngjun Park --- mm/swapfile.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index b73f34ff44f8..449890074c28 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1729,7 +1729,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; } --=20 2.36.1