]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm, swap: speed up hibernation allocation and writeout
authorKairui Song <kasong@tencent.com>
Mon, 16 Feb 2026 14:58:02 +0000 (22:58 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 24 Mar 2026 21:38:25 +0000 (14:38 -0700)
Since commit 0ff67f990bd4 ("mm, swap: remove swap slot cache"),
hibernation has been using the swap slot slow allocation path for
simplification, which turns out might cause regression for some devices
because the allocator now rotates clusters too often, leading to slower
allocation and more random distribution of data.

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

Test result with Samsung SSD 830 Series (SATA II, 3.0 Gbps) shows the
performance is several times better [1]:
6.19:               324 seconds
After this series:  35 seconds

Link: https://lkml.kernel.org/r/20260216-hibernate-perf-v4-1-1ba9f0bf1ec9@tencent.com
Link: https://lore.kernel.org/linux-mm/8b4bdcfa-ce3f-4e23-839f-31367df7c18f@gmx.de/
Signed-off-by: Kairui Song <kasong@tencent.com>
Fixes: 0ff67f990bd4 ("mm, swap: remove swap slot cache")
Reported-by: Carsten Grohmann <mail@carstengrohmann.de>
Closes: https://lore.kernel.org/linux-mm/20260206121151.dea3633d1f0ded7bbf49c22e@linux-foundation.org/
Cc: Baoquan He <bhe@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Chris Li <chrisl@kernel.org>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/swapfile.c

index 94af29d1de8887135f8466c7c7b75121a45d273c..90132b74d6a0836d16f070c4cc7ae8d3a222fef1 100644 (file)
@@ -1926,8 +1926,9 @@ out:
 /* 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)
@@ -1937,11 +1938,21 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
        if (get_swap_device_info(si)) {
                if (si->flags & SWP_WRITEOK) {
                        /*
-                        * Grab the local lock to be compliant
-                        * with swap table allocation.
+                        * 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);
-                       offset = cluster_alloc_swap_entry(si, NULL);
+                       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);
                        local_unlock(&percpu_swap_cluster.lock);
                        if (offset)
                                entry = swp_entry(si->type, offset);