Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
@ 2026-12-05  3:14 albin_yang
  2026-12-05  4:39 ` Barry Song
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: albin_yang @ 2026-12-05  3:14 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.
The inner check after spin_unlock() is redundant, so drop it as well.

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 v3:
  - Drop the now-redundant inner SWP_PAGE_DISCARD check (Youngjun Park)

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

 mm/swapfile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 226230dda0aa..89abc9e09b27 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1509,10 +1509,11 @@ 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)
-				ret = swap_do_scheduled_discard(si);
+			ret = swap_do_scheduled_discard(si);
 			put_swap_device(si);
 		}
 		if (ret)
-- 
2.43.7


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

* Re: [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
  2026-12-05  3:14 [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
@ 2026-12-05  4:39 ` Barry Song
  2026-12-05  7:01 ` + mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch added to mm-new branch Andrew Morton
  2026-12-05  7:05 ` [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() Kairui Song
  2 siblings, 0 replies; 4+ messages in thread
From: Barry Song @ 2026-12-05  4:39 UTC (permalink / raw)
  To: albin_yang
  Cc: akpm, chrisl, kasong, nphamcs, baoquan.he, youngjun.park,
	linux-mm, linux-kernel, albinwyang, stable

On Sat, Dec 5, 2026 at 11:15 AM <albin_yang@163.com> wrote:
>
> 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.
> The inner check after spin_unlock() is redundant, so drop it as well.
>
> 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 v3:
>   - Drop the now-redundant inner SWP_PAGE_DISCARD check (Youngjun Park)
>
> v2: https://lore.kernel.org/r/20261204024446.3612318-1-albin_yang@163.com
> v1: https://lore.kernel.org/r/20261128134810.3203203-1-albin_yang@163.com
>
>  mm/swapfile.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 226230dda0aa..89abc9e09b27 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1509,10 +1509,11 @@ 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)
> -                               ret = swap_do_scheduled_discard(si);

We drop swap_lock here, so in theory, a concurrent swapoff could
clear si->flags:

SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
{
        ...
        spin_lock(&swap_lock);
        p->flags = 0;
        spin_unlock(&swap_lock);
        ...
}

A subsequent swapon could then reuse the same si through
alloc_swap_info(), with a different SWP_PAGE_DISCARD setting.

However, this should still be safe. Even if we end up making an
unnecessary call to swap_do_scheduled_discard(), the discard list
should be empty, and list_empty(&si->discard_clusters) should
prevent any actual discard operation.

Overall, the patch looks good to me.

Reviewed-by: Barry Song <baohua@kernel.org>

> +                       ret = swap_do_scheduled_discard(si);
>                         put_swap_device(si);
>                 }
>                 if (ret)
> --
> 2.43.7
>

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

* + mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch added to mm-new branch
  2026-12-05  3:14 [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
  2026-12-05  4:39 ` Barry Song
@ 2026-12-05  7:01 ` Andrew Morton
  2026-12-05  7:05 ` [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() Kairui Song
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-05  7:01 UTC (permalink / raw)
  To: mm-commits, youngjun.park, stable, nphamcs, kasong, chrisl,
	baoquan.he, baohua, albinwyang, akpm


The patch titled
     Subject: mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
has been added to the -mm mm-new branch.  Its filename is
     mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Wei Yang <albinwyang@tencent.com>
Subject: mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
Date: Sat,  5 Dec 2026 11:14:49 +0800

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.  The inner check after
spin_unlock() is redundant, so drop it as well.

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.

Link: https://patch.msgid.link/20261205031449.319131-1-albin_yang@163.com
Fixes: f9e82f99b377 ("mm/swapfile: fix list iteration when next node is removed during discard")
Signed-off-by: Wei Yang <albinwyang@tencent.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Barry Song <baohua@kernel.org>
Cc: Christopher Li <chrisl@kernel.org>
Cc: Kairui Song <kasong@tencent.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Youngjun Park <youngjun.park@lge.com>
Cc: <stable@vger.kernel.org>

 mm/swapfile.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

--- a/mm/swapfile.c~mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard
+++ a/mm/swapfile.c
@@ -1509,10 +1509,11 @@ 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)
-				ret = swap_do_scheduled_discard(si);
+			ret = swap_do_scheduled_discard(si);
 			put_swap_device(si);
 		}
 		if (ret)
_

Patches currently in -mm which might be from albinwyang@tencent.com are

mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch


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

* Re: [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()
  2026-12-05  3:14 [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
  2026-12-05  4:39 ` Barry Song
  2026-12-05  7:01 ` + mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch added to mm-new branch Andrew Morton
@ 2026-12-05  7:05 ` Kairui Song
  2 siblings, 0 replies; 4+ messages in thread
From: Kairui Song @ 2026-12-05  7:05 UTC (permalink / raw)
  To: albin_yang
  Cc: akpm, chrisl, nphamcs, baoquan.he, baohua, youngjun.park,
	linux-mm, linux-kernel, albinwyang, stable

On Sat, Dec 5, 2026 at 11:15 AM <albin_yang@163.com> wrote:
>
> 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.
> The inner check after spin_unlock() is redundant, so drop it as well.
>
> 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 v3:
>   - Drop the now-redundant inner SWP_PAGE_DISCARD check (Youngjun Park)
>
> v2: https://lore.kernel.org/r/20261204024446.3612318-1-albin_yang@163.com
> v1: https://lore.kernel.org/r/20261128134810.3203203-1-albin_yang@163.com
>
>  mm/swapfile.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 226230dda0aa..89abc9e09b27 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1509,10 +1509,11 @@ 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)
> -                               ret = swap_do_scheduled_discard(si);
> +                       ret = swap_do_scheduled_discard(si);
>                         put_swap_device(si);
>                 }
>                 if (ret)
> --
> 2.43.7

Looks good to me now, thanks for the update!

Acked-by: Kairui Song <kasong@tencent.com>

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  3:14 [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() albin_yang
2026-12-05  4:39 ` Barry Song
2026-12-05  7:01 ` + mm-swap-skip-devices-without-swp_page_discard-in-swap_sync_discard.patch added to mm-new branch Andrew Morton
2026-12-05  7:05 ` [PATCH v3] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() Kairui Song

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