From: sashiko-bot@kernel.org
To: "Nhat Pham" <nphamcs@gmail.com>, linux-mm@kvack.org
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Chris Li" <chrisl@kernel.org>,
"Kairui Song" <kasong@tencent.com>,
"Kemeng Shi" <shikemeng@huaweicloud.com>,
"Baoquan He" <baoquan.he@linux.dev>,
"Barry Song" <baohua@kernel.org>,
"Youngjun Park" <youngjun.park@lge.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Yosry Ahmed" <yosry@kernel.org>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Qi Zheng" <qi.zheng@linux.dev>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Axel Rasmussen" <axelrasmussen@google.com>,
"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Michal Hocko" <mhocko@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Hugh Dickins" <hughd@google.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Muchun Song" <muchun.song@linux.dev>,
"Arnd Bergmann" <arnd@arndb.de>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
"Marco Elver" <elver@google.com>,
"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
"Borislav Petkov (AMD)" <bp@alien8.de>,
"Jeff Layton" <jlayton@kernel.org>, "Tejun Heo" <tj@kernel.org>,
"Michal Koutný" <mkoutny@suse.com>
Subject: Re: [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap
Date: Fri, 04 Dec 2026 22:09:09 +0000 [thread overview]
Message-ID: <sashiko-outbox-250719@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-2-nphamcs@gmail.com>
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 <nphamcs@gmail.com>
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;
> };
>
> +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));
> }
>
> +/**
> + * __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 the
> + * 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 = offset / SWAPFILE_CLUSTER;
> +
> VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
> VM_WARN_ON_ONCE(offset >= 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 = 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 = __swap_entry_to_cluster(entry);
> - unsigned int ci_off = swp_cluster_offset(entry);
> unsigned long swp_tb;
> struct folio *folio;
>
> for (;;) {
> - swp_tb = swap_table_get(ci, ci_off);
> + swp_tb = swap_table_lookup(entry);
> if (!swp_tb_is_folio(swp_tb))
> return NULL;
> folio = 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 = swap_table_get(ci, ci_off);
> + swp_tb = swap_table_lookup(entry);
> if (likely(swp_tb_is_folio(swp_tb) &&
> swp_tb_to_folio(swp_tb) == 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 swap_info_struct *si,
> return found;
> }
>
> +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 = 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 offset. */
> + ci = &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 = 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_struct *si, unsigned long offset,
>
> 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+3tO91BmRwaLqf3Xia32GCemt3W8tayErG81BYLgrzA@mail.gmail.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=1
next prev parent reply other threads:[~2026-12-04 22:09 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
2026-12-04 22:09 ` sashiko-bot [this message]
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
2026-12-03 23:00 ` Nhat Pham
2026-12-03 23:05 ` Yosry Ahmed
2026-12-03 23:14 ` Nhat Pham
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=sashiko-outbox-250719@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bp@alien8.de \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=dapeng1.mi@linux.intel.com \
--cc=david@kernel.org \
--cc=elver@google.com \
--cc=enelsonmoore@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jlayton@kernel.org \
--cc=kasong@tencent.com \
--cc=kmehltretter@gmail.com \
--cc=liam@infradead.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=qi.zheng@linux.dev \
--cc=rdunlap@infradead.org \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=skhan@linuxfoundation.org \
--cc=tj@kernel.org \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
--cc=yuanchu@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.