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 08/20] mm/swap: support physical swap as a vswap backend
Date: Fri, 04 Dec 2026 22:09:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-250726@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-9-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] mm: large vswap folios declined by zswap cannot split on phys
  fallback
- [Medium] mm: vswap reclaim predicates ignore global zswap pool
  fullness
- [Medium] mm: duplicate forced rstat flush per swapped folio in
  folio_may_vswap()
- [Medium] mm/swap: vswap_init() bypasses swapon/swapoff with empty
  vswap_ops
- [Low] mm/swap: unbatched per-page physical backing free in
  zswap_store()
--

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

mm/swap: support physical swap as a vswap backend

This commit adds physical swapfile backing for virtual swap entries when
zswap declines a folio, records physical-to-virtual reverse maps, and
registers the virtual swap device at boot behind the vswap= parameter and
CONFIG_VSWAP_DEFAULT_ON.

Link: https://lore.kernel.org/all/sashiko-outbox-181078@kernel.org/
Link: https://lore.kernel.org/all/sashiko-outbox-181079@kernel.org/

[ ... ]

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index df02308bb80ab..a3d065c594bc9 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c

[ ... ]

> @@ -6324,36 +6342,38 @@ static struct cftype swap_files[] = {
>   * spending cycles on compression when there is already no room left
>   * or zswap is disabled altogether somewhere in the hierarchy.
>   */
> -bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
> +bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
>  {

[ ... ]

>  		/* Force flush to get accurate stats for charging */
> -		__mem_cgroup_flush_stats(memcg, true);
> +		if (may_flush)
> +			__mem_cgroup_flush_stats(memcg, true);
>  		pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
> -		if (pages < max)
> -			continue;
> -		ret = false;
> -		break;
> +		if (pages >= max)
> +			return false;
>  	}
> -	mem_cgroup_put(original_memcg);
> +	return true;
> +}
> +
> +bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
> +{
> +	struct mem_cgroup *memcg;
> +	bool ret;
> +
> +	memcg = get_mem_cgroup_from_objcg(objcg);
> +	ret = mem_cgroup_may_zswap(memcg, true);
> +	mem_cgroup_put(memcg);
>  	return ret;
>  }

[Severity: Medium]
With vswap enabled by this patch, does folio_may_vswap() calling
obj_cgroup_may_zswap() double the forced rstat flushes on every swapped
folio?

For cgroups with a finite non-zero memory.zswap.max, folio_alloc_swap()
now invokes obj_cgroup_may_zswap() -> mem_cgroup_may_zswap(memcg, true),
which unconditionally runs css_rstat_flush() across all possible CPUs
under the global rstat lock, and zswap_store() then repeats the same
forced flush on the same folio during swap_writeout().  Folios that never
reach zswap_store() (such as zero-filled or unmap-failed folios) also pay
this cost.

Since zswap_store() already performs the authoritative flushing check and
mem_cgroup_may_zswap(memcg, false) still catches memory.zswap.max == 0,
could folio_may_vswap() use mem_cgroup_may_zswap(memcg, false) instead?

> diff --git a/mm/page_io.c b/mm/page_io.c
> index 956c3ffc10397..854b0b71581d6 100644
> --- a/mm/page_io.c
> +++ b/mm/page_io.c

[ ... ]

> @@ -252,8 +253,14 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
>  	 */
>  	swap_zeromap_folio_clear(folio);
>  
> +	/*
> +	 * For vswap: release stale non-swapfile backings (e.g. ZSWAP from a
> +	 * previous swapout cycle) so zswap_store or folio_realloc_swap
> +	 * starts on clean slots. Contiguous PHYS backing is preserved for
> +	 * reuse by folio_realloc_swap.
> +	 */
>  	if (is_vswap_entry(folio->swap))
> -		folio_release_vswap_backing(folio);
> +		folio_release_non_phys_swap_backing(folio);
>  
>  	if (zswap_store(folio)) {

[Severity: Low]
When a physically backed large folio is redirtied in the swap cache and
then accepted by zswap_store(), does preserving the physical backing here
cause it to be freed one page at a time?

zswap_store_page() calls vswap_zswap_store() per subpage, which invokes
__vswap_release_backing(&ci_dyn->ci, voff, 1) and bypasses the contiguous
batching in __vswap_release_backing().  For a 2MB THP, that repeats
pci->lock, smp_wmb(), and global nr_swap_pages atomics up to 512 times,
and exposes the half-freed physical cluster on nonfull_clusters while the
remaining subpages are still compressing.

Batch-releasing the physical backing once in zswap_store() after admission
checks pass would keep physical slots intact on rejection while avoiding
per-page teardown on acceptance.

[ ... ]

> -	/* A vswap folio has no physical slot to write to. */
>  	if (is_vswap_entry(folio->swap)) {
> +		phys = folio_realloc_swap(folio);
> +		if (!phys.val) {
> +			folio_mark_dirty(folio);
> +			return AOP_WRITEPAGE_ACTIVATE;
> +		}
> +	}

[Severity: High]
Can a large vswap folio become permanently unreclaimable if zswap_store()
rejects it and folio_realloc_swap() cannot allocate a contiguous physical
entry?

Because cluster_alloc_swap_entry() exempts vswap_si from the SWP_BLKDEV
check, folio_alloc_swap() succeeds for large folios instead of returning
-E2BIG and triggering a split.  If zswap_store() later declines the folio
(for example, when the global zswap pool is full or GFP_NOWAIT compression
allocation fails), folio_realloc_swap() calls folio_alloc_phys_swap() on
the unsplit folio, which unconditionally fails on regular swapfiles and
fails on fragmented block swap devices.

Returning AOP_WRITEPAGE_ACTIVATE leaves an anonymous large folio in the
swap cache with vswap PTEs installed, so subsequent shrink_folio_list()
passes skip the !folio_test_swapcache(folio) block and can never reach
split_folio_to_list().  Large shmem folios hit a similar loop because
shmem_writeout() jumps to redirty rather than try_split on
AOP_WRITEPAGE_ACTIVATE, leading to reclaim thrashing and premature OOM
even when plenty of order-0 physical swap is free.

[ ... ]

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 6933e069bd2ea..3dca9bd557361 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -1911,6 +1935,12 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
>  	return err;
>  }
>  
> +bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
> +{
> +	return vswap_is_enabled() && zswap_is_enabled() &&
> +	       (!memcg || mem_cgroup_may_zswap(memcg, false));
> +}
> +
>  static bool folio_may_vswap(struct folio *folio)
>  {
>  	struct obj_cgroup *objcg;

[Severity: Medium]
Should mem_cgroup_can_vswap() and the other vswap reclaim predicates
(folio_may_vswap(), can_reclaim_anon_pages(), can_age_anon_pages(), and
shmem_writeout()) also check global zswap pool capacity?

Right now, zswap_pool_reached_full is only checked inside zswap_store().
When zswap is used without a physical swap device and the pool reaches
zswap_max_pool_percent, reclaim keeps isolating anonymous and shmem
folios, allocating vswap entries, unmapping PTEs with TLB shootdowns, and
failing at swap_writeout() with AOP_WRITEPAGE_ACTIVATE.  Checking global
zswap headroom before reclaim and swap allocation would avoid this
thrashing loop.

[ ... ]

> @@ -4515,3 +4710,76 @@ subsys_initcall(swapfile_init);
>  
>  struct swap_info_struct *vswap_si;
>  DEFINE_STATIC_KEY_FALSE(vswap_key);
> +
> +static bool vswap_enabled_early __initdata = IS_ENABLED(CONFIG_VSWAP_DEFAULT_ON);
> +
> +static int __init early_vswap(char *buf)
> +{
> +	return kstrtobool(buf, &vswap_enabled_early);
> +}
> +early_param("vswap", early_vswap);
> +
> +/* vswap does no IO on its own. */
> +static const struct swap_ops vswap_ops = { };
> +
> +static int __init vswap_init(void)

[Severity: Medium]
As reported by Chris Li on v5 [1], vswap_ops is left empty while core swap
paths accumulate scattered swap_is_vswap() special cases (including
bypassing si->users refcounting in get_swap_device_info() and
put_swap_device()), and vswap_init() auto-registers a swap device outside
the swapon/swapoff lifecycle.

Later commit "mm/swap: make vswap mandatory" removes the vswap= boot
parameter and CONFIG_VSWAP_DEFAULT_ON, making this auto-registration
unconditional on CONFIG_SWAP kernels while leaving vswap_ops empty.  Can
vswap-specific behavior be encapsulated behind swap_ops to avoid
fragmenting the core swap and locking paths?

[1] https://lore.kernel.org/r/CACePvbVaDNspMDSw_qAcapKAzXj3JGV-KMYmow7aePa0POHpSg@mail.gmail.com

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


  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
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 [this message]
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-250726@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