]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/swapfile.c: introduce function alloc_swap_scan_list()
authorChris Li <chrisl@kernel.org>
Tue, 12 Aug 2025 07:10:58 +0000 (00:10 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 12 Sep 2025 00:24:57 +0000 (17:24 -0700)
Patch series "mm/swapfile.c and swap.h cleanup", v3.

This patch series, which builds on Kairui's swap improve cluster scan series.
https://lore.kernel.org/linux-mm/20250806161748.76651-1-ryncsn@gmail.com/

It introduces a new function, alloc_swap_scan_list(), for swapfile.c.

It also cleans up swap.h by removing comments that reference fields that
have been deleted.

There are no functional changes in this two-patch series.

This patch (of 2):

alloc_swap_scan_list() will scan the whole list or the first cluster.

This reduces the repeat patterns of isolating a cluster then scanning that
cluster.  As a result, cluster_alloc_swap_entry() is shorter and
shallower.

No functional change.

Link: https://lkml.kernel.org/r/20250812-swap-scan-list-v3-0-6d73504d267b@kernel.org
Link: https://lkml.kernel.org/r/20250812-swap-scan-list-v3-1-6d73504d267b@kernel.org
Signed-off-by: Chris Li <chrisl@kernel.org>
Reviewed-by: Kairui Song <kasong@tencent.com>
Acked-by: Nhat Pham <nphamcs@gmail.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: "Huang, Ying" <ying.huang@linux.alibaba.com>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/swapfile.c

index 4a0cf4fb348df386928be3a9d57be66a85a76267..a7ffabbe65efd493fe6cb3fb3a964305cade4756 100644 (file)
@@ -820,6 +820,29 @@ out:
        return found;
 }
 
+static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
+                                        struct list_head *list,
+                                        unsigned int order,
+                                        unsigned char usage,
+                                        bool scan_all)
+{
+       unsigned int found = SWAP_ENTRY_INVALID;
+
+       do {
+               struct swap_cluster_info *ci = isolate_lock_cluster(si, list);
+               unsigned long offset;
+
+               if (!ci)
+                       break;
+               offset = cluster_offset(si, ci);
+               found = alloc_swap_scan_cluster(si, ci, offset, order, usage);
+               if (found)
+                       break;
+       } while (scan_all);
+
+       return found;
+}
+
 static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 {
        long to_scan = 1;
@@ -913,32 +936,24 @@ new_cluster:
         * to spread out the writes.
         */
        if (si->flags & SWP_PAGE_DISCARD) {
-               ci = isolate_lock_cluster(si, &si->free_clusters);
-               if (ci) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       order, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->free_clusters, order, usage,
+                                            false);
+               if (found)
+                       goto done;
        }
 
        if (order < PMD_ORDER) {
-               while ((ci = isolate_lock_cluster(si, &si->nonfull_clusters[order]))) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       order, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
+                                            order, usage, true);
+               if (found)
+                       goto done;
        }
 
        if (!(si->flags & SWP_PAGE_DISCARD)) {
-               ci = isolate_lock_cluster(si, &si->free_clusters);
-               if (ci) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       order, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->free_clusters, order, usage,
+                                            false);
+               if (found)
+                       goto done;
        }
 
        /* Try reclaim full clusters if free and nonfull lists are drained */
@@ -952,13 +967,10 @@ new_cluster:
                 * failure is not critical. Scanning one cluster still
                 * keeps the list rotated and reclaimed (for HAS_CACHE).
                 */
-               ci = isolate_lock_cluster(si, &si->frag_clusters[order]);
-               if (ci) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       order, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->frag_clusters[order], order,
+                                            usage, false);
+               if (found)
+                       goto done;
        }
 
        /*
@@ -977,19 +989,15 @@ new_cluster:
                 * Clusters here have at least one usable slots and can't fail order 0
                 * allocation, but reclaim may drop si->lock and race with another user.
                 */
-               while ((ci = isolate_lock_cluster(si, &si->frag_clusters[o]))) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       0, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->frag_clusters[o],
+                                            0, usage, true);
+               if (found)
+                       goto done;
 
-               while ((ci = isolate_lock_cluster(si, &si->nonfull_clusters[o]))) {
-                       found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci),
-                                                       0, usage);
-                       if (found)
-                               goto done;
-               }
+               found = alloc_swap_scan_list(si, &si->nonfull_clusters[o],
+                                            0, usage, true);
+               if (found)
+                       goto done;
        }
 done:
        if (!(si->flags & SWP_SOLIDSTATE))