Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 16/20] mm/swap: make vswap mandatory
Date: Fri, 04 Dec 2026 22:09:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-250734@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-17-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] mm/swap: missing SWP_RMAP_CACHE_ONLY on pre-reserved physical
  swap slots
- [Medium] mm/swap: zero-filled and zswap shmem entries lose synchronous
  swapin fast path
- [Medium] mm/swap: mem_cgroup_can_swap() and folio_alloc_swap() ignore
  vswap exhaustion
- [Medium] mm/swap: mandatory vswap relies on GFP_ATOMIC-only cluster
  allocations
- [Medium] mm/swap: swap reporting and OOM accounting ignore mandatory
  vswap capacity cap
- [Medium] mm/swap: cacheline false sharing and barrier in
  vswap_capacity_in_pages()
- [Medium] mm/swap: redundant vswap cluster locking and barriers on
  swap-in faults
- [Medium] mm/swap: missing NULL check or acquire ordering in
  vswap_capacity_in_pages()
- [Medium] mm/swap: redundant cluster locks and zero-page slot churn on
  disk swapout
--

commit 4d9278f9d6b32ab62d490caafb93e1e6851a00d3
Author: Nhat Pham <nphamcs@gmail.com>

mm/swap: make vswap mandatory

This commit initializes the virtual swap device unconditionally when
CONFIG_SWAP is enabled, removes the vswap Kconfig and boot controls, and
routes all ordinary swapout through virtual swap while reserving physical
backing upfront for folios ineligible for zswap.  It also caps overcommit
and swapoff swap capacity checks at vswap_capacity_in_pages() and uses
swap_entry_backend_has_flag() for shmem synchronous swap-in checks.

> diff --git a/mm/shmem.c b/mm/shmem.c
> index 5d3695886ecd1..a9000a4436746 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c

[ ... ]

> @@ -2609,10 +2608,12 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>  	/* Look it up and read it in.. */
>  	folio = swap_cache_get_folio(swap);
>  	if (!folio) {
> -		if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
> -			/* Direct swapin skipping swap cache & readahead */
> +		if (vswap_unuse ||
> +		    swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) {
>  			folio = shmem_swap_alloc_folio(inode, vmf, index,
> -						       swap, vswap_unuse ? 0 : order, gfp);
> +						       swap,
> +						       vswap_unuse ? 0 : order,
> +						       gfp);
>  		} else {
>  			/* Cached swapin only supports order 0 folio */
>  			folio = shmem_swapin_cluster(swap, gfp, info, index);

[Severity: Medium]
Does this demote zero-filled and zswap-backed shmem entries to order-0
readahead swap-in?

For a zero-filled folio, swap_zeromap_folio_set() releases any physical
backing via __vswap_release_backing(), and zswap-backed entries have no
physical slot either.  In both cases vswap_to_phys() returns 0, so
swap_entry_backend_has_flag() returns false and shmem_swapin_folio() falls
back to shmem_swapin_cluster(), forcing shmem_split_large_entry() on large
entries and triggering speculative readahead of neighboring slots.

Zero-filled anonymous entries hit the same regression in do_swap_page(),
where both swap_entry_synchronous() and swap_entry_backend_has_flag()
return false and send the fault to order-0 swapin_readahead() instead of
swapin_sync().

[Severity: Medium]
Can mandatory vswap amplify cluster spinlock and barrier overhead on the
swap-in fault path?

Replacing the unlocked si->flags check here with
swap_entry_backend_has_flag() adds an XArray lookup, a ci->lock round-trip,
and a get_swap_device()/smp_rmb()/put_swap_device() pair on every shmem
swap-in miss.

On anonymous swap-in faults, do_swap_page() and should_try_to_free_swap()
now perform up to five separate vswap_lock_cluster() round-trips and two
get_swap_device()/smp_rmb() pairs for a single fault.  In particular,
zswap_is_present() checks is_vswap_entry() before zswap_never_enabled(), so
swap_entry_synchronous() locks the vswap cluster on every fault even when
zswap has never been enabled.

Checking zswap_never_enabled() first in zswap_is_present() and resolving
the backing type and flags once across do_swap_page() and
should_try_to_free_swap() would avoid these redundant lock cycles.

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index b0865d617512a..72652a2099e9d 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -2062,25 +2062,37 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
>  	return err;
>  }
>  
> -bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
> +/**
> + * mem_cgroup_can_swap - check backing headroom for ordinary swapout
> + * @memcg: the cgroup to query, or NULL for global reclaim
> + * @nr_pages: the number of pages the caller wants to swap out
> + *
> + * Return: true if compression or physical capacity and quota permit swapout.
> + */
> +bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
>  {
> -	return vswap_is_enabled() && zswap_is_enabled() &&
> -	       (!memcg || mem_cgroup_may_zswap(memcg, false));
> +	long nr_swap_pages;
> +
> +	if (!vswap_is_enabled())
> +		return false;
> +
> +	if (zswap_is_enabled() &&
> +	    (!memcg || mem_cgroup_may_zswap(memcg, false)))
> +		return true;
> +
> +	nr_swap_pages = memcg ? mem_cgroup_get_nr_swap_pages(memcg) :
> +			       get_nr_swap_pages();
> +	return nr_swap_pages >= nr_pages;
>  }

[Severity: Medium]
Should mem_cgroup_can_swap() also check whether vswap_si has at least
nr_pages of free virtual slots left?

Once vswap_usage_in_pages() reaches vswap_capacity_in_pages(), this still
returns true whenever zswap is enabled or physical swap has free slots, so
reclaim keeps scanning and isolating anonymous folios.  In addition,
folio_alloc_swap()'s failed: path returns -E2BIG for large folios even when
vswap_si is exhausted, causing shrink_folio_list() and shmem_writeout() to
split THPs/mTHPs and retry order-0 allocation only to fail again.

[ ... ]

> @@ -2218,8 +2232,7 @@ int folio_alloc_swap(struct folio *folio)
>  		}
>  	}
>  
> -	if (!may_vswap || !vswap_alloc(folio))
> -		folio_alloc_phys_swap(folio);
> +	vswap_alloc(folio);

[Severity: Medium]
Can removing the physical swap fallback here cause swapout failures under
memory pressure because vswap_alloc_cluster() only uses GFP_ATOMIC?

Unlike swap_cluster_populate(), which drops its locks and retries with
__GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL, vswap_alloc_cluster() performs
several GFP_ATOMIC | __GFP_NOWARN allocations (ci_dyn, swap_table, vtable,
and xa_alloc()) under percpu_vswap_cluster.lock and ci->lock with no
sleeping fallback and without __GFP_NOMEMALLOC.  When atomic reserves are
depleted, vswap_alloc() fails even with abundant physical swap, triggering
futile large-folio splits on -E2BIG, reclaim stalls, or premature OOMs.

[ ... ]

>  	if (unlikely(!folio_test_swapcache(folio)))
>  		goto failed;
>  
> +	/* Reserve disk-only backing here so large allocation failures can split. */
> +	if (!may_zswap && !folio_realloc_swap(folio).val) {
> +		swap_cache_del_folio(folio);
> +		goto failed;
> +	}
> +
>  	return 0;

[Severity: High]
Does reserving physical backing via folio_realloc_swap() here leave
SWP_RMAP_CACHE_ONLY clear on physical swap slots whose vswap swap_count is
still 0?

vswap_alloc() enters the folio into swapcache with swap_count == 0, and
folio_realloc_swap() allocates physical slots via
__swap_cluster_alloc_entries(), which initializes the physical swap_table
rmap entries without SWP_RMAP_CACHE_ONLY.  folio_realloc_swap() then
installs vtable_mk_phys() under ci->lock without checking for
swap_count == 0, while vswap_mark_cache_only() only runs on a 1 -> 0
transition in swap_put_entries_cluster().

If try_to_unmap() fails in shrink_folio_list() (or the folio is deferred
while !mem_cgroup_swap_full() and !folio_test_mlocked()), folio_free_swap()
is skipped and the folio returns to the LRU in swapcache with
swap_count == 0 and physical backing lacking SWP_RMAP_CACHE_ONLY.  A
concurrent swap_put_entries_cluster() dropping swap_count from 1 to 0 while
folio_realloc_swap() has dropped ci->lock for folio_alloc_phys_swap() hits
the same state:

CPU0                              CPU1
folio_realloc_swap()
  spin_unlock(&ci->lock)
  folio_alloc_phys_swap()
                                  swap_put_entries_cluster()
                                    swap_count 1 -> 0
                                    vswap_mark_cache_only()
                                      (vtable still NONE/ZSWAP; no-op)
  spin_lock(&ci->lock)
  __vtable_set(..., vtable_mk_phys())
  spin_unlock(&ci->lock)          <-- SWP_RMAP_CACHE_ONLY never set

Once swap fills past 50%, cluster_scan_range(), cluster_reclaim_range(),
and swap_reclaim_full_clusters() skip Vswap-tagged physical slots without
SWP_RMAP_CACHE_ONLY, pinning physical swap space until the folio is freed
or swapoff runs and risking premature swap exhaustion.  Checking
swap_count == 0 under ci->lock in folio_realloc_swap() and calling
swap_rmap_mark_cache_only() fixes both paths.

[Severity: Medium]
Does calling folio_realloc_swap() eagerly for every !may_zswap folio add
avoidable cluster lock round-trips and zero-page slot churn on disk
swapout?

For a non-zero disk-backed folio, folio_alloc_swap() and swap_writeout()
now perform 8 cluster spinlock cycles and 7 XArray lookups (compared to 3
cluster locks and 0 XArray lookups before): folio_realloc_swap() locks
ci->lock just to re-check vtable_type and re-read memcg_id that
folio_alloc_swap() already holds, and swap_writeout() locks ci->lock three
more times for swap_zeromap_folio_clear(),
folio_release_non_phys_swap_backing(), and folio_realloc_swap() again.

For zero-filled !may_zswap folios, folio_alloc_swap() allocates physical
slots and charges memcg->swap only for swap_zeromap_folio_set() to
immediately free the unwritten slots via __vswap_release_backing(),
bouncing nr_swap_pages and psi->inuse_pages, uncharging memcg->swap, and
invoking swap_slot_free_notify() and potential cluster discards.

[ ... ]

> +long vswap_capacity_in_pages(void)
>  {
> -	return kstrtobool(buf, &vswap_enabled_early);
> -}
> -early_param("vswap", early_vswap);
> +	struct swap_info_struct *si;
>  
> -/* vswap does no IO on its own. */
> -static const struct swap_ops vswap_ops = { };
> +	if (!vswap_is_enabled())
> +		return 0;
> +	/* Pairs with publication in vswap_init(), before allocation is enabled. */
> +	si = smp_load_acquire(&vswap_si);
> +	return si->pages;
> +}

[Severity: Medium]
Can vswap_capacity_in_pages() dereference a NULL vswap_si during boot when
CONFIG_JUMP_LABEL=n?

Async initramfs unpacking (populate_rootfs() -> do_populate_rootfs() ->
shmem_acct_blocks() -> __vm_enough_memory()) runs concurrently with
late_initcall(vswap_init).  With CONFIG_JUMP_LABEL=n, static_key_enable()
uses a relaxed atomic_set() and vswap_is_enabled() uses a relaxed
raw_atomic_read().  The conditional branch after vswap_is_enabled() does
not order the subsequent smp_load_acquire(&vswap_si) after the load of
vswap_key.key.enabled on weakly-ordered CPUs:

CPU0 (vswap_init)                 CPU1 (do_populate_rootfs)
                                  vswap_capacity_in_pages()
                                    si = smp_load_acquire(&vswap_si)
                                      (reordered early; reads NULL)
smp_store_release(&vswap_si, si)
static_branch_enable(&vswap_key)
  atomic_set(&key->enabled, 1)
                                    vswap_is_enabled() -> true
                                    return si->pages;  <-- NULL deref

Checking if (!si) return 0; (or reading si = smp_load_acquire(&vswap_si)
without relying on the static key for ordering) prevents the NULL pointer
dereference.

[Severity: Medium]
Does calling vswap_capacity_in_pages() from __vm_enough_memory() introduce
cacheline false sharing and an acquire barrier on the mmap/brk hot path?

Under both OVERCOMMIT_GUESS (vm_overcommit_guess_limit()) and
OVERCOMMIT_NEVER (vm_commit_limit()), every __vm_enough_memory() call now
invokes vswap_capacity_in_pages(), executing smp_load_acquire(&vswap_si)
and reading si->pages.  In struct swap_info_struct, pages sits adjacent to
frag_clusters[] and atomic_long_t inuse_pages, which swap_usage_add() and
swap_usage_sub() modify globally on every swap allocation and free.

Since vswap_si->pages is a boot-time constant initialized before
static_branch_enable(&vswap_key), caching it in a __ro_after_init scalar
avoids both the false sharing and the acquire barrier.

> diff --git a/mm/util.c b/mm/util.c
> index 47c4cf7400cf7..d0dbe4de593fb 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -924,7 +924,7 @@ unsigned long vm_commit_limit(void)
>  	else
>  		allowed = ((totalram_pages() - hugetlb_total_pages())
>  			   * sysctl_overcommit_ratio / 100);
> -	allowed += total_swap_pages;
> +	allowed += min(total_swap_pages, vswap_capacity_in_pages());
>  
>  	return allowed;
>  }

[Severity: Medium]
Should swap reporting and OOM accounting also be capped at
vswap_capacity_in_pages() now that a single virtual swap device is
mandatory?

vswap_init() caps the sole virtual swap device at
min(swapfile_maximum_size, REFCOUNT_MAX) (~8 TiB on 64-bit 4 KiB systems,
or 512 MiB on 32-bit MIPS), and this commit caps vm_commit_limit(),
vm_overcommit_guess_limit(), and swapoff_check_commit() there.  However,
si_swapinfo() (used by /proc/meminfo and sysinfo(2)), constrained_alloc()
in mm/oom_kill.c, and mem_cgroup_get_max() in mm/memcontrol.c still read
uncapped total_swap_pages and nr_swap_pages, causing reported swap totals
and OOM badness calculations to disagree with actual usable swap capacity
when total physical swap exceeds vswap_capacity_in_pages() or vswap_init()
fails.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=16


  reply	other threads:[~2026-12-04 22:10 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
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 [this message]
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-250734@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