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 06/20] mm/swap: resolve physical backing for reads and device flags
Date: Fri, 04 Dec 2026 22:09:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-250724@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-7-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm/swap: redundant vswap cluster locks and XArray walks on
  swap-in hot paths
- [Low] mm: unlocked gap in swap_entry_backend_has_flag() races with
  swapoff+swapon
--

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

mm/swap: resolve physical backing for reads and device flags

This commit adds physical swap-entry encoding and lookup helpers to the
vswap vtable and updates swap-in reads, synchronous I/O, stable-write,
and filesystem-reclaim checks to resolve physical backing for virtual
swap entries.

> diff --git a/mm/memory.c b/mm/memory.c
> index 9ba6ebd581324..3eb1841530bb3 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4754,8 +4754,11 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
>  {
>  	if (!folio_test_swapcache(folio))
>  		return false;
> -	/* A vswap entry holds no physical slot, so keeping it saves no IO. */
> -	if (is_vswap_entry(folio->swap))
> +	/*
> +	 * Non-swapfile backends cannot be reused for future swapouts.
> +	 * Free the swap slot unless backed by contiguous physical swap.
> +	 */
> +	if (!folio_phys_swap_backed(folio))
>  		return true;
>  	/*
>  	 * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
> @@ -4763,7 +4766,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
>  	 * are fast, and meanwhile, swap cache pinning the slot deferring the
>  	 * release of metadata or fragmentation is a more critical issue.
>  	 */
> -	if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
> +	if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO))
>  		return true;

[Severity: Medium]
Can these swap-in hot paths avoid repeating the cluster_info_pool XArray
walk and ci->lock acquisition on the same vswap slot?

In should_try_to_free_swap(), which runs on every do_swap_page() fault
under vmf->ptl and folio_lock, folio_phys_swap_backed() locks the cluster
and discards the resolved physical entry, and the next line calls
swap_entry_backend_has_flag() to walk and lock the same cluster again
along with get_swap_device()/put_swap_device().

The same duplicate lookup happens in do_swap_page() on non-zswap major
faults, where swap_entry_synchronous() and swap_entry_backend_has_flag()
walk and lock the same cluster back-to-back, and in swap_read_folio(),
where vswap_to_phys() reads the vtable before swap_read_folio_zeromap()
and zswap_load() look up the same slot again.

Returning the resolved physical entry from folio_phys_swap_backed() and
dispatching once on the decoded vtable entry in swap_entry_synchronous()
and swap_read_folio() would avoid the extra spinlock contention and
vmf->ptl hold time.

[ ... ]

> diff --git a/mm/vswap.h b/mm/vswap.h
> index f64b1543e01fc..c4f0670c9b5c0 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h

[ ... ]

> +static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
> +					       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;
> +
> +	phys_si = get_swap_device(phys);
> +	if (IS_ERR_OR_NULL(phys_si))
> +		return false;
> +
> +	has_flag = data_race(phys_si->flags & flag);
> +	put_swap_device(phys_si);
> +	return has_flag;
> +}

[Severity: Low]
Can the unlocked gap between vswap_to_phys() and get_swap_device() race
with swapoff and swapon and trigger spurious "Bad swap offset entry"
errors?

In do_swap_page(), swap_entry_backend_has_flag() is called without
holding folio_lock, vmf->ptl, rcu_read_lock(), or a reference on the
physical swap device:

CPU0                              CPU1
do_swap_page()
  swap_entry_backend_has_flag()
    vswap_to_phys()
      vswap_lock_cluster()
      vt = __vtable_get()
      swap_cluster_unlock()
                                  swapoff()
                                    try_to_unuse()
                                      __vswap_release_backing()
                                    synchronize_rcu()
                                  swapon() /* smaller, same slot */
    get_swap_device(phys)
      offset >= si->max  <-- logs "Bad swap offset entry"

Reading data_race(__swap_entry_to_info(phys)->flags & flag) while still
holding ci_dyn->ci.lock avoids both this race and the
get_swap_device()/put_swap_device() round-trip, since try_to_unuse() must
take ci_dyn->ci.lock to release the backing and swap_info[] slots are
never freed.

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


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