Skip to contentSkip to conversation

mm/swap: skip empty clusters in the swapoff scan

PATCH Superseded Youngjun Park 4 participants 13 messages Aug 6 – Aug 8

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Youngjun Park

author reviewer
#0 lore raw

v3 cover mm/swap: skip empty clusters in the swapoff scan

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Patch 1 is an unrelated one line comment fix noticed on the way.

A debug test confirmed the skip path runs, and swapoff completed under load with no DEBUG_VM or lockdep splats.

Changes in v3:
- 2/2: clamp the scan end with min_t() so it stops at si->max, rather than running into the masked tail of the last cluster, which drops the need to explain why walking that tail was safe (Barry)
- 2/2: compute ci_off only where it is used
- 1/2, 2/2: pick up Barry's Reviewed-by
- Rebased on mm-new
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- 1/2: reword the comment to "array, one entry per cluster", dropping the redundant "on every device" (Barry)
- 1/2: pick up Kairui's Acked-by
- 2/2: drop the min(), the swap table is always SWAPFILE_CLUSTER entries and swapon() masks the tail past si->max as bad (Kairui)
- 2/2: mark the unlocked ci->count read with READ_ONCE() for KCSAN instead of cluster_is_empty(), whose other callers hold ci->lock (Kairui)
- 2/2: expand the commit message to cover both of the above
- Rebased on mm-new
- Link to v1: https://lore.kernel.org/r/[email protected]

Youngjun Park (2):
mm/swap: fix stale comment on swap_info_struct::cluster_info mm/swap: scan by cluster in find_next_to_unuse()

 include/linux/swap.h |  2 +-
 mm/swapfile.c        | 43 ++++++++++++++++++++++++++++++-------------
 2 files changed, 31 insertions(+), 14 deletions(-)

base-commit: 1fb556c523f6c18b43b1f52fb366f61c9963ce06

Signature · 1 line
2.48.1
author reviewer
#1 lore raw

PATCH v3 1/2 mm/swap: fix stale comment on swap_info_struct::cluster_info

setup_swap_clusters_info() allocates cluster_info for every swap area, not only for SSDs.

---
 include/linux/swap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +1 -1
index 2cb1d29307c5..2b14e2e9673b 100644
@@ -246,7 +246,7 @@ struct swap_info_struct {
 	struct plist_node list;		/* entry in swap_active_head */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* size of this swap device */
-	struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */
+	struct swap_cluster_info *cluster_info; /* array, one entry per cluster */
 	struct list_head free_clusters; /* free clusters list */
 	struct list_head full_clusters; /* full clusters list */
 	struct list_head nonfull_clusters[SWAP_NR_ORDERS];
Signature · 1 line
2.48.1
reviewer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] mm/swap: fix stale comment on swap_info_struct::cluster_info

Quoted 9 lines · Youngjun Park

setup_swap_clusters_info() allocates cluster_info for every swap area, not only for SSDs.

---
 include/linux/swap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
include/linux/swap.h +1 -1
index 2cb1d29307c5..2b14e2e9673b 100644
@@ -246,7 +246,7 @@ struct swap_info_struct {
 	struct plist_node list;		/* entry in swap_active_head */
 	signed char	type;		/* strange name for an index */
 	unsigned int	max;		/* size of this swap device */
-	struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */
+	struct swap_cluster_info *cluster_info; /* array, one entry per cluster */
 	struct list_head free_clusters; /* free clusters list */
 	struct list_head full_clusters; /* full clusters list */
 	struct list_head nonfull_clusters[SWAP_NR_ORDERS];
Signature · 1 line
2.48.1
author reviewer
#3 lore raw

PATCH v3 2/2 mm/swap: scan by cluster in find_next_to_unuse()

In reply to Youngjun Park

find_next_to_unuse() walks every offset from 0 to si->max, and swapoff restarts that walk on each retry, so the cost scales with the size of the device rather than with the few slots the shmem and mmlist passes could not free. It has caused stalls before.

The flat walk predates the swap table. Slot state now lives in a per cluster table, and wait_for_allocation() stops all allocation before try_to_unuse() runs, so a cluster that holds no slot in use stays that way. Skip such a cluster instead of reading all of its entries.

Commit dc644a073769 ("mm: add three more cond_resched() in swapoff") answered those stalls with a cond_resched() every 256 offsets. A walk bounded by one cluster no longer needs that counter. The loop now runs at most SWAPFILE_CLUSTER times before it returns or reschedules, the same bound swap_reclaim_full_clusters() already scans between cond_resched() calls.

The scan end is clamped to si->max, so the walk stops there rather than running into the masked tail of the last cluster.

ci->count is read without ci->lock, so READ_ONCE() marks the read for KCSAN. Allocation is already stopped, so the count can only drop, and a slot stops being counted only after its folio has left the swap cache. An empty cluster therefore holds nothing for try_to_unuse() to act on.

---
 mm/swapfile.c | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)
mm/swapfile.c +30 -13
index dea2d3b36e06..0d24efd32eb0 100644
@@ -370,8 +370,6 @@ static void discard_swap_cluster(struct swap_info_struct *si,
 	}
 }
 
-#define LATENCY_LIMIT		256
-
 static inline bool cluster_is_empty(struct swap_cluster_info *info)
 {
 	return info->count == 0;
@@ -2763,7 +2761,9 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type)
 static unsigned int find_next_to_unuse(struct swap_info_struct *si,
 					unsigned int prev)
 {
-	unsigned int i;
+	struct swap_cluster_info *ci;
+	unsigned long i, end;
+	unsigned int ci_off;
 	unsigned long swp_tb;
 
 	/*
@@ -2772,19 +2772,36 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
 	 * hits are okay, and sys_swapoff() has already prevented new
 	 * allocations from this area (while holding swap_lock).
 	 */
-	for (i = prev + 1; i < si->max; i++) {
-		swp_tb = swap_table_get(__swap_offset_to_cluster(si, i),
-					i % SWAPFILE_CLUSTER);
-		if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
-			break;
-		if ((i % LATENCY_LIMIT) == 0)
+	i = prev + 1;
+	while (i < si->max) {
+		ci = __swap_offset_to_cluster(si, i);
+		end = min_t(unsigned long,
+			    ALIGN_DOWN(i, SWAPFILE_CLUSTER) + SWAPFILE_CLUSTER,
+			    si->max);
+
+		/*
+		 * An empty cluster has no slot in use, so skip it whole.
+		 * A slot is uncounted only after its folio left the swap
+		 * cache, so there is nothing here for try_to_unuse() to act on.
+		 * Count only drops here, so a READ_ONCE() without ci->lock is
+		 * enough, unlike in every other cluster_is_empty() caller.
+		 */
+		if (!READ_ONCE(ci->count)) {
+			i = end;
 			cond_resched();
-	}
+			continue;
+		}
 
-	if (i == si->max)
-		i = 0;
+		ci_off = i % SWAPFILE_CLUSTER;
+		for (; i < end; ci_off++, i++) {
+			swp_tb = swap_table_get(ci, ci_off);
+			if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
+				return i;
+		}
+		cond_resched();
+	}
 
-	return i;
+	return 0;
 }
 
 static int try_to_unuse(unsigned int type)
Signature · 1 line
2.48.1
Acked-by on v3 2/2
#4 lore raw

Re: [PATCH v3 2/2] mm/swap: scan by cluster in find_next_to_unuse()

Quoted 26 lines · Youngjun Park

find_next_to_unuse() walks every offset from 0 to si->max, and swapoff restarts that walk on each retry, so the cost scales with the size of the device rather than with the few slots the shmem and mmlist passes could not free. It has caused stalls before.

The flat walk predates the swap table. Slot state now lives in a per cluster table, and wait_for_allocation() stops all allocation before try_to_unuse() runs, so a cluster that holds no slot in use stays that way. Skip such a cluster instead of reading all of its entries.

Commit dc644a073769 ("mm: add three more cond_resched() in swapoff") answered those stalls with a cond_resched() every 256 offsets. A walk bounded by one cluster no longer needs that counter. The loop now runs at most SWAPFILE_CLUSTER times before it returns or reschedules, the same bound swap_reclaim_full_clusters() already scans between cond_resched() calls.

The scan end is clamped to si->max, so the walk stops there rather than running into the masked tail of the last cluster.

ci->count is read without ci->lock, so READ_ONCE() marks the read for KCSAN. Allocation is already stopped, so the count can only drop, and a slot stops being counted only after its folio has left the swap cache. An empty cluster therefore holds nothing for try_to_unuse() to act on.

Quoted 6 lines
---
 mm/swapfile.c | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

Looks good.

reviewer Reviewed-by on v3 2/2
#5 lore raw

Re: [PATCH v3 2/2] mm/swap: scan by cluster in find_next_to_unuse()

In reply to Youngjun Park
Quoted 83 lines · Youngjun Park

find_next_to_unuse() walks every offset from 0 to si->max, and swapoff restarts that walk on each retry, so the cost scales with the size of the device rather than with the few slots the shmem and mmlist passes could not free. It has caused stalls before.

The flat walk predates the swap table. Slot state now lives in a per cluster table, and wait_for_allocation() stops all allocation before try_to_unuse() runs, so a cluster that holds no slot in use stays that way. Skip such a cluster instead of reading all of its entries.

Commit dc644a073769 ("mm: add three more cond_resched() in swapoff") answered those stalls with a cond_resched() every 256 offsets. A walk bounded by one cluster no longer needs that counter. The loop now runs at most SWAPFILE_CLUSTER times before it returns or reschedules, the same bound swap_reclaim_full_clusters() already scans between cond_resched() calls.

The scan end is clamped to si->max, so the walk stops there rather than running into the masked tail of the last cluster.

ci->count is read without ci->lock, so READ_ONCE() marks the read for KCSAN. Allocation is already stopped, so the count can only drop, and a slot stops being counted only after its folio has left the swap cache. An empty cluster therefore holds nothing for try_to_unuse() to act on.

---
 mm/swapfile.c | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)
mm/swapfile.c +19 -10
index dea2d3b36e06..0d24efd32eb0 100644
@@ -370,8 +370,6 @@ static void discard_swap_cluster(struct swap_info_struct *si,
 	}
 }
 
-#define LATENCY_LIMIT		256
-
 static inline bool cluster_is_empty(struct swap_cluster_info *info)
 {
 	return info->count == 0;
@@ -2763,7 +2761,9 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type)
 static unsigned int find_next_to_unuse(struct swap_info_struct *si,
 					unsigned int prev)
 {
-	unsigned int i;
+	struct swap_cluster_info *ci;
+	unsigned long i, end;
+	unsigned int ci_off;
 	unsigned long swp_tb;
 
 	/*
@@ -2772,19 +2772,36 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
 	 * hits are okay, and sys_swapoff() has already prevented new
 	 * allocations from this area (while holding swap_lock).
 	 */
-	for (i = prev + 1; i < si->max; i++) {
-		swp_tb = swap_table_get(__swap_offset_to_cluster(si, i),
-					i % SWAPFILE_CLUSTER);
-		if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
-			break;
-		if ((i % LATENCY_LIMIT) == 0)
+	i = prev + 1;
+	while (i < si->max) {
+		ci = __swap_offset_to_cluster(si, i);
+		end = min_t(unsigned long,
+			    ALIGN_DOWN(i, SWAPFILE_CLUSTER) + SWAPFILE_CLUSTER,
+			    si->max);
+
+		/*
+		 * An empty cluster has no slot in use, so skip it whole.
+		 * A slot is uncounted only after its folio left the swap
+		 * cache, so there is nothing here for try_to_unuse() to act on.
+		 * Count only drops here, so a READ_ONCE() without ci->lock is
+		 * enough, unlike in every other cluster_is_empty() caller.
+		 */
+		if (!READ_ONCE(ci->count)) {
+			i = end;
 			cond_resched();
-	}

mm/swapfile.c static unsigned int find_next_to_unuse(struct swap_info_struct *si, · line 2799

Quoted 10 lines
mm/swapfile.c +7 -2
+			continue;
+		}
 
-	if (i == si->max)
-		i = 0;
+		ci_off = i % SWAPFILE_CLUSTER;
+		for (; i < end; ci_off++, i++) {
+			swp_tb = swap_table_get(ci, ci_off);
+			if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
+				return i;

I would remove ci_off to save one local variable, but it's only personal preference, not strong opinion.

	for (; i < end; i++) {
	    swp_tb = swap_table_get(ci, i % SWAPFILE_CLUSTER);
	    ...
	}

Other than the nitpick, this is a great optimization patch.

Quoted 13 lines
mm/swapfile.c +4 -1
+		}
+		cond_resched();
+	}
 
-	return i;
+	return 0;
 }
 
 static int try_to_unuse(unsigned int type)
Signature · 1 line
2.48.1
maintainer
#6 lore raw
In reply to Youngjun Park
Quoted 4 lines · On Fri, 7 Aug 2026 04:32:26 +0900 Youngjun Park <[email protected]> wrote:

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Thanks.

Can you help us understand how significant this change is for users? If "not very" then I'd prefer to defer consideraton of the series until after 7.3-rc1.

author reviewer
#7 lore raw
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Fri, 7 Aug 2026 04:32:26 +0900 Youngjun Park <[email protected]> wrote:

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Quoted 5 lines

Thanks.

Can you help us understand how significant this change is for users? If "not very" then I'd prefer to defer consideraton of the series until after 7.3-rc1.

Hello Andrew

"Not very" in the common case, though there is a case where the win is clear. No bug and no user report.

For now I would rather defer to after 7.3-rc1.

And for your reference, here is the details.

Every swapoff does a little less work now, because the scan steps over an unused area one cluster at a time.
But IMHO most of the swapoff time goes to unuse_mm() and to reading the pages back in.

The gain shows on a large swap device that is almost empty, when the last pages still in use are near the end of it. The scan has to walk up to them, and today it looks at every slot on the way. Now the empty clusters in between are skipped in one step.

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

- today: 256M table reads
- with the skip: 512K counter reads

That should be around half a second of scan saved.

What I have checked is that empty clusters are skipped as intended, so the scan does less work. That work is a small part of swapoff, so depending on the situation it may be too small to see in clock time.

Thanks,
Youngjun

author reviewer
#8 lore raw
Quoted 10 lines · Youngjun Park
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Fri, 7 Aug 2026 04:32:26 +0900 Youngjun Park <[email protected]> wrote:

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Thanks.

Quoted 8 lines
Quoted 3 lines

Can you help us understand how significant this change is for users? If "not very" then I'd prefer to defer consideraton of the series until after 7.3-rc1.

Hello Andrew

"Not very" in the common case, though there is a case where the win is clear. No bug and no user report.

Something I forgot to mention,

This only affects swapoff.
and few users run swapoff often, so the impact is limited either way.

Quoted 18 lines

For now I would rather defer to after 7.3-rc1.

And for your reference, here is the details.

Every swapoff does a little less work now, because the scan steps over an unused area one cluster at a time.
But IMHO most of the swapoff time goes to unuse_mm() and to reading the pages back in.

The gain shows on a large swap device that is almost empty, when the last pages still in use are near the end of it. The scan has to walk up to them, and today it looks at every slot on the way. Now the empty clusters in between are skipped in one step.

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

Quoted 4 lines

- today: 256M table reads
- with the skip: 512K counter reads

That should be around half a second of scan saved.

+ benefit.

Reclaim can put a cached folio back into a page table with the slot it already had, so try_to_unuse() retries and the scan starts over. The saving then applies once per pass.

reviewer
#9 lore raw
In reply to Youngjun Park
Quoted 32 lines · Youngjun Park
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Fri, 7 Aug 2026 04:32:26 +0900 Youngjun Park <[email protected]> wrote:

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Quoted 5 lines

Thanks.

Can you help us understand how significant this change is for users? If "not very" then I'd prefer to defer consideraton of the series until after 7.3-rc1.

Hello Andrew

"Not very" in the common case, though there is a case where the win is clear. No bug and no user report.

For now I would rather defer to after 7.3-rc1.

And for your reference, here is the details.

Every swapoff does a little less work now, because the scan steps over an unused area one cluster at a time.
But IMHO most of the swapoff time goes to unuse_mm() and to reading the pages back in.

The gain shows on a large swap device that is almost empty, when the last pages still in use are near the end of it. The scan has to walk up to them, and today it looks at every slot on the way. Now the empty clusters in between are skipped in one step.

Quoted 7 lines

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

- today: 256M table reads
- with the skip: 512K counter reads

Maybe just use time to measure swapoff time consuming, just like below as I did on a kvm guest, I guess a bare metal machine with larger system ram could be more obvious?

root@fedora:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       181Mi       3.6Gi       924Ki        72Mi       3.7Gi
Swap:          2.0Gi        18Mi       2.0Gi
root@fedora:~# time swapoff /dev/vdb

real 0m0.101s
user 0m0.001s
sys 0m0.017s

root@fedora:~# swapon /dev/vdb
root@fedora:~# time swapoff /dev/vdb

real 0m0.014s
user 0m0.003s
sys 0m0.001s

Not sure if Andrew is asking for this.

Quoted 2 lines

That should be around half a second of scan saved.

Yeah, a concrete number is shown.

author reviewer
#10 lore raw
Quoted 55 lines · Baoquan He
Quoted 32 lines · Youngjun Park
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Fri, 7 Aug 2026 04:32:26 +0900 Youngjun Park <[email protected]> wrote:

find_next_to_unuse() walks a swap device one offset at a time. Slot state now lives in a per cluster swap table, so patch 2 dismisses an empty cluster with one counter read instead of SWAPFILE_CLUSTER table reads.

Quoted 5 lines

Thanks.

Can you help us understand how significant this change is for users? If "not very" then I'd prefer to defer consideraton of the series until after 7.3-rc1.

Hello Andrew

"Not very" in the common case, though there is a case where the win is clear. No bug and no user report.

For now I would rather defer to after 7.3-rc1.

And for your reference, here is the details.

Every swapoff does a little less work now, because the scan steps over an unused area one cluster at a time.
But IMHO most of the swapoff time goes to unuse_mm() and to reading the pages back in.

The gain shows on a large swap device that is almost empty, when the last pages still in use are near the end of it. The scan has to walk up to them, and today it looks at every slot on the way. Now the empty clusters in between are skipped in one step.

Quoted 7 lines

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

- today: 256M table reads
- with the skip: 512K counter reads

Maybe just use time to measure swapoff time consuming, just like below as I did on a kvm guest, I guess a bare metal machine with larger system ram could be more obvious?

root@fedora:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       181Mi       3.6Gi       924Ki        72Mi       3.7Gi
Swap:          2.0Gi        18Mi       2.0Gi
root@fedora:~# time swapoff /dev/vdb

real 0m0.101s
user 0m0.001s
sys 0m0.017s

Quoted 8 lines
root@fedora:~# swapon /dev/vdb
root@fedora:~# time swapoff /dev/vdb

real 0m0.014s
user 0m0.003s
sys 0m0.001s

Not sure if Andrew is asking for this.

Thank you for looking into it.

Yes, it is possible to show this. However, it cannot provide the saturation time, as that depends on the swap device size and swap slot distribution.

Since a naive swapoff test cannot provide stable evidence, I addressed the approximate skip time (and also checked whether a skip occurred) using a logical time calculation as shown below.

I try to take some time to measure the swapoff time under experiment conditions. I will follow up this in the next patch iteration or sooner in this thread (depending on Andrew's decision).

Quoted 4 lines
Quoted 2 lines

That should be around half a second of scan saved.

Yeah, a concrete number is shown.

Right, based on time complexity arithmetic.

Youngjun

maintainer
#11 lore raw
In reply to Baoquan He
Quoted 22 lines · On Fri, 7 Aug 2026 16:42:22 +0800 Baoquan He <[email protected]> wrote:
Quoted 7 lines

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

- today: 256M table reads
- with the skip: 512K counter reads

Maybe just use time to measure swapoff time consuming, just like below as I did on a kvm guest, I guess a bare metal machine with larger system ram could be more obvious?

root@fedora:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       181Mi       3.6Gi       924Ki        72Mi       3.7Gi
Swap:          2.0Gi        18Mi       2.0Gi
root@fedora:~# time swapoff /dev/vdb

real 0m0.101s
user 0m0.001s
sys 0m0.017s

Quoted 8 lines
root@fedora:~# swapon /dev/vdb
root@fedora:~# time swapoff /dev/vdb

real 0m0.014s
user 0m0.003s
sys 0m0.001s

Not sure if Andrew is asking for this.

I think it's helpful to include such info. The audience for changelogs is more than swap developers! It's also an MM maintainer and -stable maintainers and other people who are all wondering "should I backport this for my users". Let's give them the means to determine that.

author reviewer
#12 lore raw
Quoted 31 lines · Andrew Morton
Quoted 29 lines · On Fri, 7 Aug 2026 16:42:22 +0800 Baoquan He <[email protected]> wrote:
Quoted 7 lines

I have no measured times yet, since that case has to be set up on purpose. What I did is the arithmetic for the case that skips best,
For example 1T of swap with 256M slots with SWAPFILE_CLUSTER = 512 and everything free but the far end:

- today: 256M table reads
- with the skip: 512K counter reads

Maybe just use time to measure swapoff time consuming, just like below as I did on a kvm guest, I guess a bare metal machine with larger system ram could be more obvious?

root@fedora:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       181Mi       3.6Gi       924Ki        72Mi       3.7Gi
Swap:          2.0Gi        18Mi       2.0Gi
root@fedora:~# time swapoff /dev/vdb

real 0m0.101s
user 0m0.001s
sys 0m0.017s

root@fedora:~# swapon /dev/vdb
root@fedora:~# time swapoff /dev/vdb

real 0m0.014s
user 0m0.003s
sys 0m0.001s

Quoted 6 lines
Quoted 1 line

Not sure if Andrew is asking for this.

I think it's helpful to include such info. The audience for changelogs is more than swap developers! It's also an MM maintainer and -stable maintainers and other people who are all wondering "should I backport this for my users". Let's give them the means to determine that.

Hello Andrew Here is the experiment.

7.2-rc5, fill a 1 TiB swap up to some amount, then swapoff.

What is left sits at the top of what was filled, so every slot below it is free. Those entries are in the swap cache and cheap to free, so the search is most of the time. In short, best scenario my patch shows improvement.

Medians over 11 pairs at 32 and 128 GiB, 3 pairs at 256 and 512.

      filled          swapoff 
                      old        new     
       32 GiB          92.4ms    66.3ms
      128 GiB         157.6ms    94.4ms
      256 GiB         209.7ms    63.8ms  
      512 GiB         391.8ms    73.4ms   

old grows with how much was filled, new does not.

In the ordinary case swap still holds real data and swapoff spends its time reading it back. I tested 4 GiB on an 8 GiB device, where the scan is 1.4% of try_to_unuse(), and there is no difference either way. meaning no regression

Thanks!
Youngjun