mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
@ 2026-12-04  2:44 albin_yang
  2026-12-04  4:52 ` Youngjun Park
  0 siblings, 1 reply; 3+ messages in thread
From: albin_yang @ 2026-12-04  2:44 UTC (permalink / raw)
  To: akpm, chrisl, kasong
  Cc: nphamcs, baoquan.he, baohua, youngjun.park, linux-mm,
	linux-kernel, albin_yang, albinwyang, stable

From: Wei Yang <albinwyang@tencent.com>

swap_sync_discard() walks swap_active_head and, for every device, drops
and re-takes the global swap_lock and does a get/put_swap_device()
round trip.  A device without SWP_PAGE_DISCARD can never make this
function return true, because swap_do_scheduled_discard() is only
reached under that flag.  For such devices all of that work is pure
overhead.

Skip them without dropping swap_lock.  A skipped device cannot affect
ret or any global state, so folio_alloc_swap() behaves identically.
Holding the lock keeps 'next' on the list (plist_del() takes the same
lock), so the plist_node_empty() re-check below is unnecessary here;
visited devices still unlock/relock and re-validate as before.
SWP_PAGE_DISCARD is only set or cleared at swapon/swapoff time.

We backported the upstream swap sync-discard changes into our 6.6-based
kernel.  LTP/stress-ng swap stress testing then drove swap 100% full
and triggered a hung_task panic: the vmcore showed 17 of 256 CPUs
spinning on swap_lock here, with 25 active devices and none carrying
SWP_PAGE_DISCARD, while the mmap_read_lock holder starved on block
tags.

With swap full, each failed folio_alloc_swap() on an order-0,
non-swapcache folio walks all N devices, costing 1+N+N+1 global
swap_lock operations plus N get/put round trips.  The walk is over
swap_active_head, so an empty avail list no longer lets it exit early.
The same code is present in current mainline.

Measured on v7.4-rc4, strictly paired on the same machine (26 x 256MB
swap devices, 16 concurrent workers, 512MB MADV_PAGEOUT chunks, swap
100% full), unpatched vs patched:

  folio_alloc_swap avg      32.745 us  ->  1.505 us   (21.8x)
  allocations in 30s         916,734   ->  6,216,609  (6.8x)
  per-page                   30.2 us   ->  4.0 us     (7.6x)
  spinlock slowpath/alloc     27.1     ->  1.87       (14.5x lower)

The per-allocation cost used to grow linearly with the device count
and is now independent of it.

Semantics are unchanged: swap stayed 100% full in both runs, so
allocation outcomes were identical, and swap_do_scheduled_discard()
had 0 hits.  A separate test with 25 no-flag devices plus one enabled
with --discard=pages confirmed that flag-bearing devices still take
the original path: swap_do_scheduled_discard() was called once per
folio_alloc_swap() call, with no hung tasks or regression.

Fixes: f9e82f99b377 ("mm/swapfile: fix list iteration when next node is removed during discard")
Cc: stable@vger.kernel.org
Signed-off-by: Wei Yang <albinwyang@tencent.com>
---
Changes in v2:
  - Point Fixes at f9e82f99b377 (Kairui Song)
  - Add Cc: stable (Kairui Song)

v1: https://lore.kernel.org/r/20261128134810.3203203-1-albin_yang@163.com

 mm/swapfile.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 226230dda0aa..e553bd2c20a7 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1509,6 +1509,8 @@ static bool swap_sync_discard(void)
 	spin_lock(&swap_lock);
 start_over:
 	plist_for_each_entry_safe(si, next, &swap_active_head, list) {
+		if (!(si->flags & SWP_PAGE_DISCARD))
+			continue;
 		spin_unlock(&swap_lock);
 		if (get_swap_device_info(si)) {
 			if (si->flags & SWP_PAGE_DISCARD)
-- 
2.43.7


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
  2026-12-04  2:44 [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
@ 2026-12-04  4:52 ` Youngjun Park
  2026-12-05  2:55   ` albin_yang
  0 siblings, 1 reply; 3+ messages in thread
From: Youngjun Park @ 2026-12-04  4:52 UTC (permalink / raw)
  To: albin_yang
  Cc: akpm, chrisl, kasong, nphamcs, baoquan.he, baohua, linux-mm,
	linux-kernel, albinwyang, stable

On Fri, Dec 04, 2026 at 10:44:46AM +0800, albin_yang@163.com wrote:
> From: Wei Yang <albinwyang@tencent.com>

Hi Wei

> Measured on v7.4-rc4, strictly paired on the same machine (26 x 256MB
> swap devices, 16 concurrent workers, 512MB MADV_PAGEOUT chunks, swap
> 100% full), unpatched vs patched:
> 
>   folio_alloc_swap avg      32.745 us  ->  1.505 us   (21.8x)
>   allocations in 30s         916,734   ->  6,216,609  (6.8x)
>   per-page                   30.2 us   ->  4.0 us     (7.6x)
>   spinlock slowpath/alloc     27.1     ->  1.87       (14.5x lower)
> 
> The per-allocation cost used to grow linearly with the device count
> and is now independent of it.
> 
> Semantics are unchanged: swap stayed 100% full in both runs, so
> allocation outcomes were identical, and swap_do_scheduled_discard()
> had 0 hits.  A separate test with 25 no-flag devices plus one enabled
> with --discard=pages confirmed that flag-bearing devices still take
> the original path: swap_do_scheduled_discard() was called once per
> folio_alloc_swap() call, with no hung tasks or regression.

Looking at your use case, there seems to be no discard device at all.

What about counting SWP_PAGE_DISCARD devices like nr_rotate_swap, so we
can skip the walk (and swap_lock) entirely when it's zero? Might be a
bit local, but swap on discard devices isn't that common.
It is just opinion for curios about your thought. no serious consideration plz :)

> Fixes: f9e82f99b377 ("mm/swapfile: fix list iteration when next node is removed during discard")
> Cc: stable@vger.kernel.org
> Signed-off-by: Wei Yang <albinwyang@tencent.com>
> ---
> Changes in v2:
>   - Point Fixes at f9e82f99b377 (Kairui Song)
>   - Add Cc: stable (Kairui Song)
> 
> v1: https://lore.kernel.org/r/20261128134810.3203203-1-albin_yang@163.com
> 
>  mm/swapfile.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 226230dda0aa..e553bd2c20a7 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1509,6 +1509,8 @@ static bool swap_sync_discard(void)
>  	spin_lock(&swap_lock);
>  start_over:
>  	plist_for_each_entry_safe(si, next, &swap_active_head, list) {
> +		if (!(si->flags & SWP_PAGE_DISCARD))
> +			continue;
>  		spin_unlock(&swap_lock);
>  		if (get_swap_device_info(si)) {
>  			if (si->flags & SWP_PAGE_DISCARD)

This inner check should be redundant now, right? Can we drop it?

Thanks,
Youngjun

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
  2026-12-04  4:52 ` Youngjun Park
@ 2026-12-05  2:55   ` albin_yang
  0 siblings, 0 replies; 3+ messages in thread
From: albin_yang @ 2026-12-05  2:55 UTC (permalink / raw)
  To: youngjun.park
  Cc: akpm, albin_yang, albinwyang, baohua, baoquan.he, chrisl, kasong,
	linux-kernel, linux-mm, nphamcs

Hi Youngjun,

On Fri, 4 Dec 2026 13:52:06 +0900, Youngjun Park wrote:
> Looking at your use case, there seems to be no discard device at all.
>
> What about counting SWP_PAGE_DISCARD devices like nr_rotate_swap, so we
> can skip the walk (and swap_lock) entirely when it's zero? Might be a
> bit local, but swap on discard devices isn't that common.
> It is just opinion for curios about your thought. no serious consideration plz :)

Interesting idea, but a counter would need to stay consistent with
swap_active_head across swapon/swapoff, and it only saves the last
lock/unlock pair now that the scan is down to 2 operations.  Better to
leave that to the structural rework and keep this fix minimal.

> This inner check should be redundant now, right? Can we drop it?

Yes.  All runtime writes to si->flags for an enabled device hold
swap_lock, so the loop-head check is stable.  After the unlock those
writes only touch SWP_WRITEOK/SWP_HIBERNATION; the only runtime place
clearing SWP_PAGE_DISCARD is swapoff's p->flags = 0, which comes after
percpu_ref_kill(), so a live ref from get_swap_device_info() cannot
reach it.  The inner check is therefore always true, and is itself a
read outside swap_lock.  I'll drop it in v3.

Thanks,
Wei Yang


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05  2:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  2:44 [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
2026-12-04  4:52 ` Youngjun Park
2026-12-05  2:55   ` albin_yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®