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 17/20] mm/swap: drop direct physical swapcache handling
Date: Fri, 04 Dec 2026 22:09:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-250735@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-18-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm/swap: repeated cluster spinlocks and XArray lookups per
  swap-in fault
- [Medium] mm: zswap swap-in faults run unnecessary mTHP PTE and cluster
  scans
- [Medium] mm/swap: swapoff disables readahead, plugging, and large
  shmem swap-in
--

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

mm/swap: drop direct physical swapcache handling

This commit removes the direct physical swapcache paths across swap-in,
writeout, swapoff, zswap, and memcg accounting so ordinary swapcache folios
and swap PTEs always go through virtual swap slots.  Physical slots are now
used only for reverse mappings and hibernation reservations.

[ ... ]

> diff --git a/mm/memory.c b/mm/memory.c
> index ae9d5e69622f9..ef443d569419b 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c

[ ... ]

> @@ -4919,25 +4917,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
>  
>  	entry = softleaf_from_pte(vmf->orig_pte);
>  
> -	/*
> -	 * A large swapped out folio could be partially or fully in zswap. We
> -	 * lack handling for such cases, so fallback to swapping in order-0
> -	 * folio.
> -	 *
> -	 * Vswap entries are checked later, under the cluster lock in
> -	 * __swap_cache_add_check().
> -	 */
> -	if (!is_vswap_entry(entry) && !zswap_never_enabled())
> -		return 0;
> -
> -	/*
> -	 * Neighbouring slots of a swap file can lie in two extents, and a folio
> -	 * is read with one bio. The flag is set in swapon() and stays, so the
> -	 * race is benign.
> -	 */
> -	if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
> -		return 0;
> -
>  	/*
>  	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
>  	 * and suitable for swapping THP.

[ ... ]

> @@ -5114,10 +5092,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>  	if (!folio) {
>  		/* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */
>  		if (swap_entry_synchronous(si, entry) ||
> -		    (is_vswap_entry(entry) &&
> -		     swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO)))
> +		    swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO))
>  			folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
> -					    thp_swapin_suitable_orders(si, vmf) | BIT(0),
> +					    thp_swapin_suitable_orders(vmf) | BIT(0),
>  					    vmf, NULL, 0);
>  		else
>  			folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf);

[Severity: Medium]
Does do_swap_page() now run mTHP PTE and cluster scans on every zswap swap-in
fault when mTHP orders are enabled?

With the !zswap_never_enabled() check removed from
thp_swapin_suitable_orders(), a fault where swap_entry_synchronous() matched
via zswap still takes the page table lock to scan neighboring PTEs and passes
a multi-order mask to swapin_sync().  __swap_cache_alloc_folio() then iterates
through each non-zero order, taking swap_cluster_lock() and scanning all
1 << order vtable slots in __vswap_check_backing() only to get -EBUSY for
VSWAP_ZSWAP and increment MTHP_STAT_SWPIN_FALLBACK before falling back to
order 0.

Passing BIT(0) directly when zswap matched in do_swap_page() would avoid the
extra lock contention and false fallback counts.

[ ... ]

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

[ ... ]

> @@ -3484,15 +3391,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
>  				.pmd = pmd,
>  			};
>  
> -			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);
> -			}
> +			folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, BIT(0),
> +					    &vmf, NULL, 0);
> +			if (IS_ERR(folio))
> +				folio = NULL;
>  		}

[Severity: Medium]
Does switching swapoff unconditionally to swapin_sync() serialize all swapoff
I/O to unplugged, queue-depth-1 4 KiB reads?

Previously unuse_pte_range() called swapin_readahead() with block plugging
and readahead, while swapin_sync() submits a single order-0 BIO immediately
and the caller waits on folio_lock() before moving to the next PTE.
shmem_swapin_folio() has the same regression when swapoff_type >= 0,
bypassing shmem_swapin_cluster() and forcing order 0 so large shmem swap
entries are also split via shmem_split_large_entry().

On asynchronous swap devices (HDDs, SSDs, NVMe), this can severely degrade
swapoff throughput and latency.

[ ... ]

> diff --git a/mm/vswap.h b/mm/vswap.h
> index c02aa8f6da579..f3a55414c6b71 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h
> @@ -341,8 +341,7 @@ static inline int vswap_check_backing(swp_entry_t entry, int nr,
>   *                          range of physical swap slots.
>   * @folio: a swap-cache resident folio
>   *
> - * Return: %true if @folio->swap is not a vswap entry, or if these vswap
> - * entries are backed by a contiguous range of physical slots.
> + * Return: %true if the folio has a contiguous range of physical slots.
>   */
>  static inline bool folio_phys_swap_backed(struct folio *folio)
>  {
> @@ -350,9 +349,8 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
>  	int nr = folio_nr_pages(folio);
>  	enum vswap_backing_type type;
>  
> -	return !is_vswap_entry(entry) ||
> -	       (vswap_check_backing(entry, nr, &type) == nr &&
> -		type == VSWAP_SWAPFILE);
> +	return vswap_check_backing(entry, nr, &type) == nr &&
> +	       type == VSWAP_SWAPFILE;
>  }
>  
>  static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
> @@ -403,22 +401,18 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
>  
>  /*
>   * Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...)
> - * for @entry. For a vswap entry the property belongs to the current
> + * for the virtual @entry. The property belongs to the current
>   * physical backing rather than vswap_si itself; resolve to the backing
>   * and test there. Returns false for zswap/zero/unbacked vswap entries
>   * as they don't have a backing bdev.
>   */
> -static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
> -					       swp_entry_t entry,
> +static inline bool swap_entry_backend_has_flag(swp_entry_t entry,
>  					       unsigned long flag)
>  {
>  	struct swap_info_struct *phys_si;
>  	swp_entry_t phys;
>  	bool has_flag;
>  
> -	if (!swap_is_vswap(si))
> -		return data_race(si->flags & flag);
> -
>  	phys = vswap_to_phys(entry);
>  	if (!phys.val)
>  		return false;

[Severity: High]
Can dropping the lockless fast paths here add several virtual cluster
spinlock roundtrips to every swap-in fault?

With all ordinary swap entries now virtual, a single swap-in miss in
do_swap_page() calls vswap_lock_cluster() (an XArray lookup plus ci->lock
acquire/release) repeatedly on the same slot:

- swap_entry_synchronous() -> zswap_is_present(), which checks
  is_vswap_entry() before zswap_never_enabled() and takes ci->lock even when
  zswap was never enabled;
- swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO) on the next line,
  plus get_swap_device()/put_swap_device() on the backing device;
- swap_read_folio() via vswap_to_phys(), even for zero-mapped or zswap
  folios;
- should_try_to_free_swap(), which runs on both hits and misses and locks the
  cluster twice back-to-back in folio_phys_swap_backed() and
  swap_entry_backend_has_flag(folio->swap, SWP_SYNCHRONOUS_IO) while holding
  the folio lock.

On fast NVMe or zram swap devices, this serializes concurrent faults within
the same swap cluster.  Coalescing the backing lookup in do_swap_page() and
should_try_to_free_swap(), and checking zswap_never_enabled() first in
zswap_is_present(), would eliminate most of these redundant lock roundtrips.

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


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