Skip to contentSkip to conversation

New: Hibernate image write 10x slower since commit 0ff67f990bd4 ("mm, swap: remove swap slot cache")

Bug 221055 Andrew Morton 3 participants 7 messages Feb 6 – Feb 15 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]/

Andrew Morton

author maintainer
#1 lore raw
Quoted 1 line · On Fri, 06 Feb 2026 19:51:38 +0000 [email protected] wrote:

https://bugzilla.kernel.org/show_bug.cgi?id=221055

Dear Deity, I do wish kernel MM bugzilla would just go away. Sigh.

Canned response follows:

(switched to email. Please respond via emailed reply-to-all, not via the bugzilla web interface).

Quoted 15 lines
            Bug ID: 221055
           Summary: Hibernate image write 10x slower since commit
                    0ff67f990bd4 ("mm,  swap: remove swap slot cache")
           Product: Memory Management
           Version: 2.5
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Other
          Assignee: [email protected]
          Reporter: [email protected]
        Regression: No
Quoted 4 lines

=== Summary ===

Hibernate image write 10x slower since commit 0ff67f990bd4 ("mm,
swap: remove swap slot cache")

Thanks for all the work you put into this.

Quoted 73 lines

=== Description ===

Since kernel 6.15, writing the hibernate image to swap has become approximately 10x slower. The regression was introduced by commit 0ff67f990bd45726e0d9e91111d998e7a3595b32 ("mm, swap: remove swap slot cache") authored by Kairui Song and merged by Andrew Morton.

=== Performance Data ===

                 Before (6.14)   After (6.15+)
Hibernate        28-32 seconds   290-342 seconds
Effective rate   ~60 MB/s        ~4.5 MB/s

The swap partition's raw sequential write speed (measured with dd) is 255-450 MB/s, confirming the hardware is not the bottleneck.

=== Bisect Result ===

A full git bisect on the vanilla kernel repository (git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git) between v6.14 (good) and v6.15 (bad) with 14 iterations consistently identified this commit:

  # first bad commit: [0ff67f990bd45726e0d9e91111d998e7a3595b32]
  #   mm, swap: remove swap slot cache

The parent commit 1b7e90020eb7 ("mm, swap: use percpu cluster as allocation fast path") is fast (28s), while 0ff67f990bd4 itself is slow (307s).

=== Kernel Version Tests ===

  Kernel              Duration   Result
  6.12.1-arch1-1      32 sec     OK
  6.13.1-arch1-1      42 sec     OK
  6.14.1-arch1-1      32 sec     OK
  6.15.1-arch1-1     302 sec     SLOW
  6.18.1-arch1-1     290 sec     SLOW

=== Reproduction ===

1. System with swap partition (not swap file)
2. Boot kernel >= 6.15
3. Run: echo reboot > /sys/power/disk
4. Run: echo 1 > /sys/power/pm_print_times
5. Run: date; echo disk > /sys/power/state; date
6. Compare hibernate duration with kernel <= 6.14

For best visibility of the issue, use hibernate=nocompress as kernel parameter to disable image compression.

=== Hardware ===

- CPU: Intel Core (Ivy Bridge)
- RAM: 16 GB
- SSD: Samsung SSD 830 Series (SATA II, 3.0 Gbps)
- Swap: 20 GB partition (/dev/sda2)
- Arch Linux x86_64

=== Kernel Log (6.18, with hibernate=nocompress) ===

PM: hibernation: hibernation entry
PM: hibernation: Preallocating image memory
PM: hibernation: Allocated 2217260 kbytes in 0.39 seconds (5685.28 MB/s)
PM: hibernation: Creating image:
PM: hibernation: Need to copy 506945 pages
PM: hibernation: Wrote 1794356 kbytes in 397.97 seconds (4.51 MB/s)

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are the assignee for the bug.
#2 lore raw
Quoted 27 lines · Andrew Morton
Quoted 1 line · On Fri, 06 Feb 2026 19:51:38 +0000 [email protected] wrote:

https://bugzilla.kernel.org/show_bug.cgi?id=221055

Dear Deity, I do wish kernel MM bugzilla would just go away. Sigh.

Canned response follows:

(switched to email. Please respond via emailed reply-to-all, not via the bugzilla web interface).

Quoted 15 lines
            Bug ID: 221055
           Summary: Hibernate image write 10x slower since commit
                    0ff67f990bd4 ("mm,  swap: remove swap slot cache")
           Product: Memory Management
           Version: 2.5
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Other
          Assignee: [email protected]
          Reporter: [email protected]
        Regression: No
Quoted 6 lines
Quoted 4 lines

=== Summary ===

Hibernate image write 10x slower since commit 0ff67f990bd4 ("mm,
swap: remove swap slot cache")

Thanks for all the work you put into this.

Thanks for letting me know!

Quoted 19 lines
Quoted 19 lines

=== Description ===

Since kernel 6.15, writing the hibernate image to swap has become approximately 10x slower. The regression was introduced by commit 0ff67f990bd45726e0d9e91111d998e7a3595b32 ("mm, swap: remove swap slot cache") authored by Kairui Song and merged by Andrew Morton.

=== Performance Data ===

                 Before (6.14)   After (6.15+)
Hibernate        28-32 seconds   290-342 seconds
Effective rate   ~60 MB/s        ~4.5 MB/s

The swap partition's raw sequential write speed (measured with dd) is 255-450 MB/s, confirming the hardware is not the bottleneck.

=== Bisect Result ===

Quoted 7 lines
Quoted 7 lines

A full git bisect on the vanilla kernel repository (git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git) between v6.14 (good) and v6.15 (bad) with 14 iterations consistently identified this commit:

  # first bad commit: [0ff67f990bd45726e0d9e91111d998e7a3595b32]
  #   mm, swap: remove swap slot cache

Oh, I took a look and I think I know the problem now, the new added fast path in that series is only used by allocation from vmscan, for hibernation it always use the slow path, as I didn't expect the slow path is below hibernation's requirement.

Or maybe, it not the allocation path that is slow, but because cluster rotation in slow path splatted the allocation to every cluster makes the IO much slower.

Both are not too hard to fix. Is that's SSD only issue? And should we fix this in every LTS or just mainline? It could be a bit trouble some if every LTS needs a fix.

#3 lore raw
Quoted 20 lines · Kairui Song
Quoted 18 lines · Andrew Morton
Quoted 6 lines · On Fri, 06 Feb 2026 19:51:38 +0000 [email protected] wrote:

https://bugzilla.kernel.org/show_bug.cgi?id=221055

=== Summary ===

Hibernate image write 10x slower since commit 0ff67f990bd4 ("mm,
swap: remove swap slot cache")

Thanks for all the work you put into this.

Quoted 7 lines

=== Bisect Result ===

A full git bisect on the vanilla kernel repository (git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git) between v6.14 (good) and v6.15 (bad) with 14 iterations consistently identified this commit:

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
  # first bad commit: [0ff67f990bd45726e0d9e91111d998e7a3595b32]
  #   mm, swap: remove swap slot cache

Both are not too hard to fix. Is that's SSD only issue? And should we fix this in every LTS or just mainline? It could be a bit trouble some if every LTS needs a fix.

Hi All,

After many attempts, I haven't (fully) reproduced it yet. But I do have a patch, see attachment.

After this patch:
PM: hibernation: Wrote 1559088 kbytes in 0.55 seconds (2834.70 MB/s) - NVME
PM: hibernation: Wrote 2038748 kbytes in 4.04 seconds (504.64 MB/s) - SATA

After 0ff67f990bd4 (the regression commit):
PM: hibernation: Wrote 2224184 kbytes in 0.90 seconds (2471.31 MB/s) - NVME
PM: hibernation: Wrote 2215472 kbytes in 4.44 seconds (498.98 MB/s) - SATA

Before 0ff67f990bd4:
PM: hibernation: Wrote 2222772 kbytes in 0.84 seconds (2646.15 MB/s) - NVME
PM: hibernation: Wrote 2230700 kbytes in 4.47 seconds (499.03 MB/s) - SATA

It seems a bit faster / slower indeed, or maybe just noise. On my machines 0ff67f990bd4 is not causing an major issue like the one described in the bugzilla link. I'll try to see if there are more detailed issues with the test setup.

And can anyone help verify this fix? I'll also try some other SSD later maybe that's only reproducible with SSD that have very poor 4K performance.

HDD should not be affected because the allocator has a special way to do HDD allocation.

From f40aad0136c870d5b535fd37a2ef20153a7f7680 Mon Sep 17 00:00:00 2001
From: Kairui Song <[email protected]>
Date: Thu, 12 Feb 2026 17:05:47 +0800
Subject: [PATCH 1/2] mm, swap: speed up hibernation allocation

Since commit 0ff67f990bd4 ("mm, swap: remove swap slot cache"), hibernation has been using the swap slot slow allocation path for simplification, which turns out to be too slow for some devices.

Fast allocation is not complex, so implement hibernation support as well.

And reduce the indent of the code too while at it. It doesn't have to check the device flag as the allocator will also check the device flag and refuse to allocate if the device is not writable.

Fixes commit 0ff67f990bd4 ("mm, swap: remove swap slot cache")
---
 mm/swapfile.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
mm/swapfile.c +22 -12
index c6863ff7152c..bcac10d96fb5 100644
@@ -1926,8 +1926,9 @@ void swap_put_entries_direct(swp_entry_t entry, int nr)
 /* Allocate a slot for hibernation */
 swp_entry_t swap_alloc_hibernation_slot(int type)
 {
-	struct swap_info_struct *si = swap_type_to_info(type);
-	unsigned long offset;
+	struct swap_info_struct *pcp_si, *si = swap_type_to_info(type);
+	unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID;
+	struct swap_cluster_info *ci;
 	swp_entry_t entry = {0};
 
 	if (!si)
@@ -1935,17 +1936,26 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
 
 	/* This is called for allocating swap entry, not cache */
 	if (get_swap_device_info(si)) {
-		if (si->flags & SWP_WRITEOK) {
-			/*
-			 * Grab the local lock to be compliant
-			 * with swap table allocation.
-			 */
-			local_lock(&percpu_swap_cluster.lock);
-			offset = cluster_alloc_swap_entry(si, NULL);
-			local_unlock(&percpu_swap_cluster.lock);
-			if (offset)
-				entry = swp_entry(si->type, offset);
+		/*
+		 * Try the local cluster first if it matches the device. If
+		 * not, try grab a new cluster and override local cluster.
+		 */
+		local_lock(&percpu_swap_cluster.lock);
+		pcp_si = this_cpu_read(percpu_swap_cluster.si[0]);
+		pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]);
+		if (pcp_si == si && pcp_offset) {
+			ci = swap_cluster_lock(si, pcp_offset);
+			if (cluster_is_usable(ci, 0))
+				offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
+			else
+				swap_cluster_unlock(ci);
 		}
+		if (!offset)
+			offset = cluster_alloc_swap_entry(si, NULL);
+		if (offset)
+			entry = swp_entry(si->type, offset);
+		local_unlock(&percpu_swap_cluster.lock);
+
 		put_swap_device(si);
 	}
 fail:
Signature · 21 lines
2.52.0


From 44552d10f3f80747bd6b69588d96dcae4400f016 Mon Sep 17 00:00:00 2001
From: Kairui Song <[email protected]>
Date: Thu, 12 Feb 2026 17:48:06 +0800
Subject: [PATCH 2/2] mm, swap: merge common convention and simplify allocation
 scan helper

Almost all caller of the cluster scan helper requires the: lock -> check
usefulness / emptiness -> unlock routine. So merge them into the same
helper to simplify the code.

This should also improve the scan slightly, as a few callers didn't
check the emptiness, which might help reduce fragmentation in rare
cases.

Signed-off-by: Kairui Song <[email protected]>
---
 mm/swapfile.c | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)
mm/swapfile.c +8 -22
index bcac10d96fb5..03cc0ff4dc8c 100644
@@ -923,11 +923,14 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 	bool need_reclaim, ret, usable;
 
 	lockdep_assert_held(&ci->lock);
-	VM_WARN_ON(!cluster_is_usable(ci, order));
 
-	if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER)
+	if (!cluster_is_usable(ci, order) || end < nr_pages ||
+	    ci->count + nr_pages > SWAPFILE_CLUSTER)
 		goto out;
 
+	if (cluster_is_empty(ci))
+		offset = cluster_offset(si, ci);
+
 	for (end -= nr_pages; offset <= end; offset += nr_pages) {
 		need_reclaim = false;
 		if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim))
@@ -1060,14 +1063,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 			goto new_cluster;
 
 		ci = swap_cluster_lock(si, offset);
-		/* Cluster could have been used by another order */
-		if (cluster_is_usable(ci, order)) {
-			if (cluster_is_empty(ci))
-				offset = cluster_offset(si, ci);
-			found = alloc_swap_scan_cluster(si, ci, folio, offset);
-		} else {
-			swap_cluster_unlock(ci);
-		}
+		found = alloc_swap_scan_cluster(si, ci, folio, offset);
 		if (found)
 			goto done;
 	}
@@ -1332,14 +1328,7 @@ static bool swap_alloc_fast(struct folio *folio)
 		return false;
 
 	ci = swap_cluster_lock(si, offset);
-	if (cluster_is_usable(ci, order)) {
-		if (cluster_is_empty(ci))
-			offset = cluster_offset(si, ci);
-		alloc_swap_scan_cluster(si, ci, folio, offset);
-	} else {
-		swap_cluster_unlock(ci);
-	}
-
+	alloc_swap_scan_cluster(si, ci, folio, offset);
 	put_swap_device(si);
 	return folio_test_swapcache(folio);
 }
@@ -1945,10 +1934,7 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
 		pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]);
 		if (pcp_si == si && pcp_offset) {
 			ci = swap_cluster_lock(si, pcp_offset);
-			if (cluster_is_usable(ci, 0))
-				offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
-			else
-				swap_cluster_unlock(ci);
+			offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
 		}
 		if (!offset)
 			offset = cluster_alloc_swap_entry(si, NULL);
Signature · 1 line
2.52.0
#4 lore raw

12.02.2026 20:01:48 Kairui Song <[email protected]>:

Quoted 2 lines

And can anyone help verify this fix? I'll also try some other SSD later maybe that's only reproducible with SSD that have very poor 4K performance.

I'll try out your patch over the weekend.

Quoted 2 lines

HDD should not be affected because the allocator has a special way to do HDD allocation.

I can connect an older hard drive to my PC and test this as well. Should I do that? If it's not necessary, then I can save myself the effort.

#5 lore raw
Quoted 7 lines · Carsten Grohmann <[email protected]> 于 2026年2月13日周五 04:37写道:

12.02.2026 20:01:48 Kairui Song <[email protected]>:

Quoted 3 lines

And can anyone help verify this fix? I'll also try some other SSD later maybe that's only reproducible with SSD that have very poor 4K performance.

I'll try out your patch over the weekend.

Thanks a lot!

Quoted 5 lines
Quoted 3 lines

HDD should not be affected because the allocator has a special way to do HDD allocation.

I can connect an older hard drive to my PC and test this as well. Should I do that? If it's not necessary, then I can save myself the effort.

That's not necessary, let's just verify the issue you reported. I have been running swap tests on HDD for a while and hibernate uses the same allocation method, the performance is good so far, I can double check that.

#6 lore raw
In reply to Carsten Grohmann

12.02.2026 21:37:04 Carsten Grohmann <[email protected]>:

Quoted 5 lines

12.02.2026 20:01:48 Kairui Song <[email protected]>:

Quoted 2 lines

And can anyone help verify this fix? I'll also try some other SSD later maybe that's only reproducible with SSD that have very poor 4K performance.

I'll try out your patch over the weekend.

I tested your patch with the current HEAD from git.kernel.org. The first run is with your patch, the second without:

Date:     2026-02-15 08:29:33
Commit:   770aaedb461a (Merge tag 'bootconfig-v7.0' of git://git.kernel.or)
Kernel:   6.19.0-20260214-153617-09922-g770aaedb461a-dirty
Duration: 35 Sekunden
Result:   good
----------------------------------------
Date:     2026-02-15 09:01:25
Commit:   770aaedb461a (Merge tag 'bootconfig-v7.0' of git://git.kernel.or)
Kernel:   6.19.0-20260215-083120-09922-g770aaedb461a
Duration: 324 Sekunden
Result:   bad

With your patch, hibernation is fast again. It solves the problem I reported.

Thank you very much for taking care of my annoying problem.

Regards,
Carsten

#7 lore raw
Quoted 28 lines · Carsten Grohmann

12.02.2026 21:37:04 Carsten Grohmann <[email protected]>:

Quoted 7 lines

12.02.2026 20:01:48 Kairui Song <[email protected]>:

Quoted 4 lines

And can anyone help verify this fix? I'll also try some other SSD later
maybe that's only reproducible with SSD that have very poor 4K performance.

I'll try out your patch over the weekend.

I tested your patch with the current HEAD from git.kernel.org. The first run is with your patch, the second without:

Date:     2026-02-15 08:29:33
Commit:   770aaedb461a (Merge tag 'bootconfig-v7.0' of
git://git.kernel.or)
Kernel:   6.19.0-20260214-153617-09922-g770aaedb461a-dirty
Duration: 35 Sekunden
Result:   good
----------------------------------------
Date:     2026-02-15 09:01:25
Commit:   770aaedb461a (Merge tag 'bootconfig-v7.0' of
git://git.kernel.or)
Kernel:   6.19.0-20260215-083120-09922-g770aaedb461a
Duration: 324 Sekunden
Result:   bad
Quoted 4 lines

With your patch, hibernation is fast again. It solves the problem I reported.

Thank you very much for taking care of my annoying problem.

Thank you very much for the testing! I'll post the fix with your tested-by.