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 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries
Date: Fri, 04 Dec 2026 22:09:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-250729@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-12-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm: folio_realloc_swap() omits SWP_RMAP_CACHE_ONLY on
  zero-count slots
- [Medium] mm, swap: try_to_reclaim_vswap_backing() overshoots physical
  run when i > 0
- [Medium] mm: unconditional cross-cluster atomic RMWs in swap put/dup
  paths
- [Medium] mm: try_to_reclaim_vswap_backing() returns 0 on
  folio_trylock() failure
--

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

mm, swap: reclaim physical slots backing cache-only vswap entries

This commit adds a SWP_RMAP_CACHE_ONLY flag on physical Vswap-tagged swap
table entries to mark cache-only vswap slots, and extends
swap_reclaim_full_clusters(), cluster_scan_range(), and
cluster_reclaim_range() to reclaim their physical backing when swap is more
than half full.

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

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

[ ... ]

> @@ -936,6 +941,55 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
>  	return ret;
>  }
>  
> +/*
> + * Try to reclaim a Vswap-tagged physical slot backing a vswap entry.
> + * The physical cluster lock must NOT be held. Returns the backing folio's
> + * page count, negated if the slots could not be reclaimed, or 0 if the
> + * folio could not be shown to own @offset (i.e. there is a race).
> + */
> +static int try_to_reclaim_vswap_backing(struct swap_info_struct *si,
> +					unsigned long offset,
> +					swp_entry_t vswap_entry)
> +{
> +	swp_entry_t phys_base;
> +	struct folio *folio;
> +	unsigned int i;
> +	int ret;
> +
> +	folio = swap_cache_get_folio(vswap_entry);
> +	if (!folio)
> +		return 0;
> +
> +	if (!folio_trylock(folio)) {
> +		/* Unvalidated: the folio may not even own @offset. */
> +		folio_put(folio);
> +		return 0;
> +	}

[Severity: Medium]
Should this return a negative value (such as -1) instead of 0 when
folio_trylock() fails?

Returning 0 conflates a contended locked folio with an already-freed slot.
In cluster_reclaim_range(), the loop only breaks on < 0, so a locked folio
at the start of an order > 0 range does not abort the scan; it retries every
remaining subpage and calls folio_free_swap() on unrelated later folios
(dirtying and evicting them from swap cache) even though the locked slot
causes the final null-check loop to fail.

In swap_reclaim_full_clusters(), returning 0 falls through to offset++,
repeating the unlock, XArray lookup in swap_cache_get_folio(), failed
folio_trylock(), and relock up to SWAPFILE_CLUSTER times on a locked large
folio while holding local_lock(&percpu_swap_cluster.lock).

> +	if (!folio_matches_swap_entry(folio, vswap_entry)) {
> +		folio_unlock(folio);
> +		folio_put(folio);
> +		return 0;
> +	}
> +
> +	i = vswap_entry.val - folio->swap.val;
> +	phys_base = vswap_to_phys(folio->swap);
> +	if (!phys_base.val || swp_type(phys_base) != si->type ||
> +	    swp_offset(phys_base) + i != offset) {
> +		folio_unlock(folio);
> +		folio_put(folio);
> +		return 0;
> +	}
> +
> +	/* The run is ours: skip it all, whether or not the free succeeds. */
> +	ret = folio_nr_pages(folio);
> +	if (!folio_free_swap(folio))
> +		ret = -ret;
> +	folio_unlock(folio);
> +	folio_put(folio);
> +	return ret;
> +}

[Severity: Medium]
Can returning folio_nr_pages(folio) here cause swap_reclaim_full_clusters()
to overshoot past the end of the folio when i > 0?

swap_reclaim_full_clusters() advances by offset += abs(nr_reclaim), which
jumps to swp_offset(phys_base) + i + folio_nr_pages(folio) instead of
swp_offset(phys_base) + folio_nr_pages(folio) and skips i unrelated slots
after the folio.  This happens on a partially swapped large vswap folio
where subpages [0, i) still have swap_count > 0 and only subpage i has
SWP_RMAP_CACHE_ONLY set.

Returning folio_nr_pages(folio) - i (negated when folio_free_swap() fails)
would advance to the exact end of the folio's physical run.

[ ... ]

> @@ -2076,6 +2156,51 @@ int folio_alloc_swap(struct folio *folio)
>  	return order ? -E2BIG : -ENOMEM;
>  }
>  
> +static void vswap_mark_cache_only(struct swap_cluster_info *ci,
> +				  unsigned int ci_off)
> +{
> +	struct swap_cluster_info_dynamic *ci_dyn;
> +	struct swap_cluster_info *pci;
> +	swp_entry_t phys;
> +	unsigned long vt;
> +
> +	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> +	vt = __vtable_get(ci_dyn, ci_off);
> +
> +	if (vtable_type(vt) == VSWAP_SWAPFILE) {
> +		phys = vtable_to_phys(vt);
> +		pci = __swap_entry_to_cluster(phys);
> +		swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys));
> +	}
> +}

[Severity: Medium]
Does folio_realloc_swap() leave SWP_RMAP_CACHE_ONLY unset when attaching
physical backing to vswap slots that already have swap_count == 0?

__swap_cluster_alloc_entries() initializes new physical rmap entries with
SWP_RMAP_CACHE_ONLY cleared, and vswap_mark_cache_only() only sets it on
1-to-0 transitions when virtual_table already holds VSWAP_SWAPFILE.
folio_realloc_swap() installs VSWAP_SWAPFILE under ci->lock without
checking whether any slot is already at count 0.

This happens deterministically for a partially swapped large vswap folio
entering swap_writeout() with some subpages already at swap_count == 0,
and also on order-0 folios when a concurrent swap_put_entries_cluster()
drops swap_count from 1 to 0 before folio_realloc_swap() takes ci->lock:

CPU0                              CPU1
swap_writeout()
  folio_free_swap() -> false
  folio_realloc_swap()
    folio_alloc_phys_swap()
                                  swap_put_entries_cluster()
                                    lock(ci->lock)
                                    vswap_mark_cache_only()  /* VSWAP_NONE */
                                    count: 1 -> 0
                                    unlock(ci->lock)
    lock(ci->lock)
    __vtable_set(..., VSWAP_SWAPFILE)  <-- SWP_RMAP_CACHE_ONLY left clear
    unlock(ci->lock)

Because swap_count is already 0, swap_put_entries_cluster() will not run
on those slots again, leaving their physical backing invisible to the
reclaim scanner while the folio remains in swap cache.  Checking for
swap_count == 0 inside the ci->lock loop in folio_realloc_swap() and
calling swap_rmap_mark_cache_only() would close both paths.

[ ... ]

> diff --git a/mm/vswap.h b/mm/vswap.h
> index 080e4bec82802..72b43897faaef 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h
> @@ -43,6 +43,37 @@ static inline bool is_vswap_entry(swp_entry_t entry)
>  	return swap_is_vswap(__swap_entry_to_info(entry));
>  }
>  
> +/*
> + * Rmap cache-only helpers for physical cluster Vswap-tagged entries.
> + * SWP_RMAP_CACHE_ONLY records, inline on the physical swap_table entry,
> + * that the backing vswap entry has swap_count == 0 (swap-cache-only, so
> + * reclaimable). The physical reclaim scanner reads it directly instead of
> + * chasing the rmap into the vswap layer and paying the cluster-lookup
> + * indirection.
> + *
> + * Callers hold the vswap cluster lock, not the physical one. The rmap is
> + * only touched while the vtable holds the slot as SWAPFILE, and that
> + * window is opened and closed under the vswap cluster lock, so the
> + * allocator has finished writing the entry by then.
> + */
> +static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *ci,
> +					     unsigned int off)
> +{
> +	atomic_long_t *table;
> +
> +	table = rcu_dereference_protected(ci->table, true);
> +	atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]);
> +}
> +
> +static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
> +					      unsigned int off)
> +{
> +	atomic_long_t *table;
> +
> +	table = rcu_dereference_protected(ci->table, true);
> +	atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]);
> +}

[Severity: Medium]
Are unconditional locked atomic RMWs (atomic_long_or() and atomic_long_and())
on remote physical cluster tables needed here?

As noted in the comment above, all writes to a Vswap-tagged slot during its
VSWAP_SWAPFILE window are already serialized by the vswap cluster lock, so
full locked RMWs dirty remote cachelines and extend ci->lock hold times
unnecessarily compared to checking the bit and using plain stores.

On swap-in, shmem_swapin_folio() and do_swap_page() (when
should_try_to_free_swap() is true) call folio_put_swap() and immediately
delete the folio from swap cache, executing up to 512 remote atomic_long_or()
operations per 2MB THP only to overwrite those slots with NULL right after.

On swap-out, vswap_clear_cache_only() adds a second pass in
swap_dup_entries_cluster() and runs atomic_long_and() even when
SWP_RMAP_CACHE_ONLY is already clear.  Once commit "mm/swap: make vswap
mandatory" pre-allocates physical backing in folio_alloc_swap(), this hits
every initial swapout page on non-zswap systems.

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


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