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 07/20] mm/swap: prepare physical swapoff for virtual backing
Date: Fri, 04 Dec 2026 22:09:19 +0000 [thread overview]
Message-ID: <sashiko-outbox-250725@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-8-nphamcs@gmail.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm/swap: spurious swapoff -ENOMEM when unuse_pte_range()
discards -ENOENT
- [Medium] mm/swap: swapoff loses readahead and I/O batching for vswap
entries
- [Medium] mm: shmem_find_swap_entries() takes xa_lock_irq and restarts
walk per entry
- [Medium] mm/shmem: shmem_find_swapoff_entry() relocks vswap cluster
per sub-slot
--
commit f05da385c356349b81af60a33b89b354ca7cc636
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: prepare physical swapoff for virtual backing
This commit updates physical swapoff in shmem and swapfile to match
virtual swap entries backed by the device being removed, resolve physical
reverse maps during the swap cache walk, and use order-0 allocations when
reading vswap entries during unuse.
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 344f7edf5e15a..5d68075184746 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
[ ... ]
> @@ -1710,6 +1713,24 @@ static void shmem_evict_inode(struct inode *inode)
> #endif
> }
>
> +static bool shmem_find_swapoff_entry(swp_entry_t entry, pgoff_t *index,
> + unsigned int order, unsigned int type)
> +{
> + unsigned long nr = 1UL << order;
> + unsigned long off = *index & (nr - 1);
> +
> + if (!softleaf_is_swap(entry))
> + return false;
> + entry.val += off;
> + for (; off < nr; off++, entry.val++) {
> + if (swap_entry_is_backed_by(entry, type)) {
> + *index = round_down(*index, nr) + off;
> + return true;
> + }
> + }
> + return false;
> +}
[Severity: Medium]
Can this loop avoid re-looking up and relocking the same vswap cluster
for every sub-slot of a large shmem swap entry?
For a large entry (up to 512 sub-slots for a 2 MiB THP on 4 KiB pages)
backed by zswap or another swap device, swap_entry_is_backed_by() ->
vswap_to_phys() walks vswap_si->cluster_info_pool and acquires/releases
ci->lock on each iteration, even though all sub-slots of a large entry
reside in a single vswap cluster. Because shmem_find_swap_entries()
calls this under rcu_read_lock() and continues past cond_resched_rcu() on
non-matching entries, scanning large non-matching shmem files also
extends the RCU read-side critical section across those lookups and lock
round-trips.
Locking the vswap cluster once and scanning the contiguous range in its
virtual_table would eliminate the redundant XArray walks and lock cycles.
[ ... ]
> @@ -1726,15 +1749,27 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping,
> if (!xa_is_value(folio))
> continue;
>
> + /* A concurrent collapse can change both the value and its order. */
> + xas_lock_irq(&xas);
> + xas_set(&xas, xas.xa_index);
> + folio = xas_load(&xas);
> + if (!xa_is_value(folio)) {
> + xas_unlock_irq(&xas);
> + continue;
> + }
> entry = radix_to_swp_entry(folio);
> + index = xas.xa_index;
> + order = xas_get_order(&xas);
> + xas_unlock_irq(&xas);
[Severity: Medium]
Does taking xas_lock_irq(&xas) and restarting the XArray walk on every
swap entry regress swapoff scanning on large swapped shmem files?
shmem_unuse() scans every inode on shmem_swaplist regardless of which
swap device or zswap backs it. xas_set() resets xas->xa_node to
XAS_RESTART, so xas_load() re-descends from the root on every entry while
repeatedly disabling IRQs and taking mapping->i_pages.xa_lock in
contention with active shmem faults and reads.
When CONFIG_THP_SWAP is disabled, shmem swap entries are always order 0
and need no lock; with CONFIG_THP_SWAP, batching entries under a single
xas_lock_irq() acquisition with xas_pause() before unlocking would
preserve sequential leaf scanning and reduce lock contention.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d9e8c9365c447..6933e069bd2ea 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -2974,8 +3011,15 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
> .pmd = pmd,
> };
>
> - folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
> - &vmf);
> + if (is_vswap_entry(entry)) {
> + folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
> + BIT(0), &vmf, NULL, 0);
> + if (IS_ERR(folio))
> + folio = NULL;
> + } else {
> + folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
> + &vmf);
> + }
[Severity: Medium]
Will switching vswap entries to swapin_sync() with BIT(0) here and in
shmem_swapin_folio() drop swapoff readahead and I/O batching?
Previously, unuse_pte_range() called swapin_readahead() and
shmem_swapin_folio() called shmem_swapin_cluster(), both of which plug
the block queue, merge contiguous reads, and pipeline I/O across a
readahead window. swapin_sync() submits a single un-plugged 4 KiB read
and returns directly to folio_lock(folio), serializing physical swapoff
to queue-depth-1 4 KiB synchronous reads.
In shmem_swapin_folio(), forcing order 0 also triggers
shmem_split_large_entry() on every large swap entry, and because
shmem_unuse_inode() advances start to the last batched index, split
siblings of earlier entries in a batch are deferred to subsequent
try_to_unuse() retry passes.
> }
> if (!folio) {
> swp_tb = swap_table_lookup(entry);
[Severity: Medium]
Could discarding ERR_PTR(-ENOENT) from swapin_sync() cause swapoff(2) to
fail spuriously with -ENOMEM?
Unlike a physical swap device being removed, which has SWP_WRITEOK
cleared before try_to_unuse(), vswap_si stays writable throughout
physical swapoff so a freed vswap slot can be reallocated immediately:
CPU0 (swapoff) CPU1
unuse_pte_range()
pte_unmap(pte)
do_swap_page() / zap_pte_range()
frees vswap slot (count -> 0)
swapin_sync(entry, ...)
__swap_cache_add_check()
returns ERR_PTR(-ENOENT)
folio = NULL
swapout on live vswap_si
reallocates same vswap slot
(count >= 1)
swap_table_lookup(entry)
swp_tb_get_count(swp_tb) > 0
return -ENOMEM <-- aborts swapoff(2)
The spurious -ENOMEM propagates out of try_to_unuse(), causing swapoff(2)
to fail and reinsert the swap device even though no allocation failed.
Checking PTR_ERR(folio) == -ENOENT and continuing directly avoids the
re-lookup race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=7
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
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 [this message]
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-250725@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox