From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BCBDA606A322 for ; Fri, 4 Dec 2026 22:09:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YvA7DrFc Received: by kanga.kvack.org (Postfix) id DB67A6B05B6; Fri, 4 Dec 2026 17:09:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D671E6B05B7; Fri, 4 Dec 2026 17:09:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C56126B05B8; Fri, 4 Dec 2026 17:09:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 518EB6B05B6 for ; Fri, 4 Dec 2026 17:09:14 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C701CC062F for ; Fri, 4 Dec 2026 22:09:13 +0000 (UTC) X-FDA: 85507692186.12.5B9481C Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf17.hostedemail.com (Postfix) with ESMTP id 2399C4000F for ; Fri, 4 Dec 2026 22:09:11 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=YvA7DrFc; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796422152; b=Oxa0/el8m/u8Yi5Od9qMbd1SbSSzpTaZUlWvIab6GG7nlQlJfa3ux2sYm3IEm7GYWbP3Tw w7fDLEzdu1bwb/Z+IZJ69QNLYaTm68IJddDfXzO0qUWXTvvBSrv5ZrXeZ0OycTIwcHTeOS /BYDGUUe4vrZ0MoCNf91u2cmno7DAew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796422152; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NnaPjKKmiDYr+inVsBzBsjSDOR6BAsv/5oWo+NqOMj8=; b=j42Na0HsGutHxSnx1S3Pb7DeUs2Ay/5PD11XBu4Jr8ABOlULlizNoN0xdo4YpNCJoGPW/w Y2LRg+Q4nuPoeE0zJiGDJZ1X9TLO10qYTtZlhup1nwwpiDBrxTfOYS5NY+O34HOGRlzDMB 4XqomWpYHgX61S4NPEFnhaX80DTG7E0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=YvA7DrFc; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 5A47D60CFA; Fri, 4 Dec 2026 22:09:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8ED81F00936; Fri, 4 Dec 2026 22:09:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796422151; bh=NnaPjKKmiDYr+inVsBzBsjSDOR6BAsv/5oWo+NqOMj8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=YvA7DrFceLJxza6diW4SpA78vcYbBhN1zq8HRqhM604Dve5nNVgvmQlf+9OXtfkk2 clKQ4M6ck1/XFxqvvuuXKwXIBCquxI/Q+Lt5WZIeuB2NLxlKfjb/5/TpSLaXZm92EA uiMOGEXhN3LEXxvT/qxPEtcHzsLyXDeP3npA3p+YjYaRc7cK80AXnpBsRSUaaocBhA 53YG42Sfi0uIqkkniwYGCpl8A2tKhksMxI0M7ipAeXiHbzwQOmBrmnSmOQcGvaBb5W ftW9j7s4ZCOXy1hoXaS+bIzlqdfKzObKNdXdgBOXLKSUJFlZLUY1i8U5/W8XKNk2Kh 2k0tHQ4bxd1iA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Reply-To: sashiko-reviews@lists.linux.dev To: "Nhat Pham" , linux-mm@kvack.org Cc: "Andrew Morton" , "Chris Li" , "Kairui Song" , "Kemeng Shi" , "Baoquan He" , "Barry Song" , "Youngjun Park" , "Johannes Weiner" , "Yosry Ahmed" , "Chengming Zhou" , "Qi Zheng" , "Shakeel Butt" , "Axel Rasmussen" , "Yuanchu Xie" , "Wei Xu" , "Baolin Wang" , "Michal Hocko" , "David Hildenbrand" , "Liam R. Howlett" , "Vlastimil Babka" , "Mike Rapoport" , "Hugh Dickins" , "Jonathan Corbet" , "Shuah Khan" , "Randy Dunlap" , "Roman Gushchin" , "Muchun Song" , "Arnd Bergmann" , "Karl Mehltretter" , "Dapeng Mi" , "Marco Elver" , "Ethan Nelson-Moore" , =?utf-8?b?Qm9yaXNsYXYgUGV0a292IChBTUQp?= , "Jeff Layton" , "Tejun Heo" , =?utf-8?b?TWljaGFsIEtvdXRuw70=?= In-Reply-To: <20261203220010.1302207-2-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> <20261203220010.1302207-2-nphamcs@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 22:09:09 +0000 X-Rspamd-Server: rspam04 X-Rspam-User: X-Rspamd-Queue-Id: 2399C4000F X-Stat-Signature: oe5zfr6onmroco31gbahk1tp1odjss5g X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796422151-750202 X-HE-Meta: U2FsdGVkX1/FsFlWsXdKvfmIgC0dz0wRz9po5KTs6UNOJyI07SPx23Xy3XeQt4VZlV248wRWs7GStfq83aud288yJkoCZ0y1ZbFdxpjzqjLlpeyNFNKCPt3wCqHKrrr9rNF4GB1oOZtPFDx0/E/t8gpjTk7xqzClCn9qV8SvuZICsuC+jN+prGcFZ6bzJ8gvttTsMjZbdvb8sxbqixeF/+xfY4/MPjd0BkXFV19NFOwpHeVM8waCenWn2rAUMc7FUVRhwDWrNOJRbUkS5EK53TtkQJJBV2Jo4/d1Hjmvcpk+IdhL8U2DNwJzxdn4m9+usp7dFxXN16N14jDhsSgsjsa7ngZ3idRL7jaQ1vfBOSEOvx29ks94SRss1jQPZFuifv985z/hF3XNvjWeYcAuibhkATJQjKwbfOJkOu6IuSDLruQ4yhN0skeIF+OYEurq3GMZm2jhKuJDJjtioh5sAtAMvD+Vv7gUsJ5Q3W5FiGnm59R25lCM6WIGLKWwj7l5Dof+MCok3bbJoD10MQ1I9JnrE5zQi8BpwRW9rCi8IbWMFsjt8aIVtt9bCsIg2vbGvUhl+khNJLO7peFhiY0/KgI8yVgwC+NPuBLfMXs13pu2aRdT+sBLmzZNQd9pUYNAXdaZrvpBvGJY3CIzz8uYjTtI7xkihi6WtgLnpfP2Rz4L+4VvjU1rBWdSCWXyUn/EYLzUi0zhMQSYJuMVA/I/YLsN2xYYK7Pqzch0C1fzMKOALopulcwDlMdEm2wM4BQ+GnJS6gqwFGv8cNDJHV9RhJhGnLFevzc3rX4VTcC3Mvf3fSfr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List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] mm/swap: duplicate swap_table_lookup() in swap_cache_get_folio() - [Medium] mm/swap: dual-personality swap_is_vswap() branching bypasses swap_ops - [Medium] mm/swap: vswap_alloc_cluster() omits __GFP_NOMEMALLOC and sleep fallback - [Medium] mm/swap: vswap bypasses swapon/swapoff lifecycle and si->users refcounting -- commit 4227eddefaec09d67150d2d6a27d66cb6f9fe4cd Author: Nhat Pham mm/swap: prepare dynamic clusters for vswap This commit adds an XArray-backed dynamic cluster pool for virtual swap devices alongside the static cluster array used by physical devices, and updates cluster lookup, locking, allocation, and teardown paths to handle both layouts. > diff --git a/mm/swap.h b/mm/swap.h > index d488a69c28091..0eaedbedc3d6a 100644 > --- a/mm/swap.h > +++ b/mm/swap.h > @@ -67,6 +67,12 @@ struct swap_cluster_info { > struct list_head list; > }; > =20 > +struct swap_cluster_info_dynamic { > + struct swap_cluster_info ci; > + unsigned int index; /* for cluster_index() */ > + struct rcu_head rcu; > +}; [ ... ] > @@ -120,12 +127,33 @@ static inline struct swap_info_struct *__swap_entry= _to_info(swp_entry_t entry) > return __swap_type_to_info(swp_type(entry)); > } > =20 > +/** > + * __swap_offset_to_cluster - look up the cluster holding a swap offset > + * @si: the swap device > + * @offset: the swap entry offset > + * > + * Context: A vswap cluster is freed by kfree_rcu(). Callers must hold t= he > + * RCU read lock, or know the cluster is pinned by an in-use entry. > + * > + * Return: the cluster, or NULL if @si is a vswap device with no cluster > + * allocated at @offset. > + */ > static inline struct swap_cluster_info *__swap_offset_to_cluster( > struct swap_info_struct *si, pgoff_t offset) > { > + unsigned int cluster_idx =3D offset / SWAPFILE_CLUSTER; > + > VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ > VM_WARN_ON_ONCE(offset >=3D roundup(si->max, SWAPFILE_CLUSTER)); > - return &si->cluster_info[offset / SWAPFILE_CLUSTER]; > + > + if (swap_is_vswap(si)) { > + struct swap_cluster_info_dynamic *ci_dyn; > + > + ci_dyn =3D xa_load(&si->cluster_info_pool, cluster_idx); > + return ci_dyn ? &ci_dyn->ci : NULL; > + } > + > + return &si->cluster_info[cluster_idx]; > } [Severity: Medium] As reported by Chris Li on v3 [1], splitting cluster storage, lookup, and locking into dual physical and virtual personalities via swap_cluster_info_dynamic and swap_is_vswap() checks fragments core swap paths instead of encapsulating virtual indirection behind struct swap_ops. This patch adds over twenty swap_is_vswap() branches across core swap helpers (including nullable locking in __vswap_cluster_lock() and special-cased teardown in __free_cluster()), and by the end of the series vswap_ops is still empty. Can this indirection live behind swap_ops? [ ... ] > diff --git a/mm/swap_state.c b/mm/swap_state.c > index da65d75ceead1..436f82319425f 100644 > --- a/mm/swap_state.c > +++ b/mm/swap_state.c > @@ -92,13 +92,11 @@ void show_swap_cache_info(void) > */ > struct folio *swap_cache_get_folio(swp_entry_t entry) > { > - struct swap_cluster_info *ci =3D __swap_entry_to_cluster(entry); > - unsigned int ci_off =3D swp_cluster_offset(entry); > unsigned long swp_tb; > struct folio *folio; > =20 > for (;;) { > - swp_tb =3D swap_table_get(ci, ci_off); > + swp_tb =3D swap_table_lookup(entry); > if (!swp_tb_is_folio(swp_tb)) > return NULL; > folio =3D swp_tb_to_folio(swp_tb); > @@ -108,7 +106,7 @@ struct folio *swap_cache_get_folio(swp_entry_t entry) > * The folio may have been freed and its memory reused before > * we got the reference. Make sure it is still in the slot. > */ > - swp_tb =3D swap_table_get(ci, ci_off); > + swp_tb =3D swap_table_lookup(entry); > if (likely(swp_tb_is_folio(swp_tb) && > swp_tb_to_folio(swp_tb) =3D=3D folio)) > return folio; [Severity: Medium] Does calling swap_table_lookup(entry) twice here double the XArray walk on every swap-cache hit? Each swap_table_lookup() enters RCU, looks up swap_info[], checks swap_is_vswap(), and for vswap traverses si->cluster_info_pool via xa_load(). Once commit "mm/swap: make vswap mandatory" makes vswap mandatory, every swap-in cache hit pays two full XArray walks. Because folio_try_get() does not sleep, a single rcu_read_lock() across __swap_entry_to_cluster(), folio_try_get(), and re-reading rcu_dereference(ci->table) avoids the second lookup while still detecting concurrent cluster teardown (which clears ci->table before kfree_rcu()). [ ... ] > diff --git a/mm/swapfile.c b/mm/swapfile.c > index d59fa82208620..818189c82ea9f 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c [ ... ] > @@ -1107,6 +1135,45 @@ static unsigned int alloc_swap_scan_list(struct sw= ap_info_struct *si, > return found; > } > =20 > +static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, > + struct folio *folio) > +{ > + struct swap_cluster_info_dynamic *ci_dyn; > + struct swap_cluster_info *ci; > + unsigned long offset; > + > + VM_WARN_ON(!swap_is_vswap(si)); > + > + ci_dyn =3D kzalloc_obj(*ci_dyn, GFP_ATOMIC | __GFP_NOWARN); > + if (!ci_dyn) > + return SWAP_ENTRY_INVALID; > + > + spin_lock_init(&ci_dyn->ci.lock); > + INIT_LIST_HEAD(&ci_dyn->ci.list); > + > + /* Lock before publishing: xa_alloc makes the cluster findable by offse= t. */ > + ci =3D &ci_dyn->ci; > + spin_lock(&ci->lock); > + > + if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) { > + spin_unlock(&ci->lock); > + kfree(ci_dyn); > + return SWAP_ENTRY_INVALID; > + } > + > + if (xa_alloc(&si->cluster_info_pool, &ci_dyn->index, ci_dyn, > + XA_LIMIT(1, DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER) - 1), > + GFP_ATOMIC | __GFP_NOWARN)) { > + spin_unlock(&ci->lock); > + swap_cluster_free_table(&ci_dyn->ci); > + kfree(ci_dyn); > + return SWAP_ENTRY_INVALID; > + } > + > + offset =3D cluster_offset(si, ci); > + return alloc_swap_scan_cluster(si, ci, folio, offset); > +} [Severity: Medium] Can vswap_alloc_cluster() deplete emergency memory reserves during reclaim by omitting __GFP_NOMEMALLOC? Most folio_alloc_swap() callers (such as kswapd() and direct reclaim) run with PF_MEMALLOC set. Without __GFP_NOMEMALLOC, __gfp_pfmemalloc_flags() grants ALLOC_NO_WATERMARKS to the ci_dyn, swap table, and XArray node allocations here, bypassing zone watermarks and risking reclaim deadlocks. In addition, non-PF_MEMALLOC callers such as ttm_backup_backup_folio() -> shmem_write_folio() have no sleepable fallback and fail immediately when non-blocking watermarks are tight. Physical swap_cluster_populate() passes __GFP_NOMEMALLOC and drops locks to retry with GFP_KERNEL on failure. [ ... ] > @@ -1413,6 +1490,10 @@ static void swap_range_free(struct swap_info_struc= t *si, unsigned long offset, > =20 > static bool get_swap_device_info(struct swap_info_struct *si) > { > + /* The vswap device is always alive, so it needs no refcount. */ > + if (swap_is_vswap(si)) > + return true; > + > if (!percpu_ref_tryget_live(&si->users)) > return false; [Severity: Medium] As reported by Chris Li on v3 [1], bypassing si->users refcounting here (and in put_swap_device()) along with keeping vswap off swap_active_head in _enable_swap_info() removes vswap from the standard swapon/swapoff lifecycle and /etc/fstab control. This changes user-visible behavior by allowing swapout to zswap without an explicit swapon, and commit "mm/swap: make vswap mandatory" later makes vswap unconditional at boot. Should vswap instead go through normal swapon and swapoff lifecycle management? [1] https://lore.kernel.org/r/CACePvbX+3tO91BmRwaLqf3Xia32GCemt3W8tayErG81B= YLgrzA@mail.gmail.com --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203220010.1302= 207-1-nphamcs@gmail.com?part=3D1