Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm, swap: prefer nonfull over free clusters

We prefer a free cluster over a nonfull cluster whenever a CPU local
cluster is drained to respect the SSD discard behavior [1]. It's not a
best practice for non-discarding devices. And this is causing a higher
fragmentation rate.

So for a non-discarding device, prefer nonfull over free clusters. This
reduces the fragmentation issue by a lot.

Testing with make -j96, defconfig, using 64k mTHP, 8G ZRAM:

Before: sys time: 6176.34s 64kB/swpout: 1659757 64kB/swpout_fallback: 139503
After: sys time: 6194.11s 64kB/swpout: 1689470 64kB/swpout_fallback: 56147

Testing with make -j96, defconfig, using 64k mTHP, 10G ZRAM:

After: sys time: 5531.49s 64kB/swpout: 1791142 64kB/swpout_fallback: 17676
After: sys time: 5587.53s 64kB/swpout: 1811598 64kB/swpout_fallback: 0

Performance is basically unchanged, and the large allocation failure rate
is lower. Enabling all mTHP sizes showed a more significant result.

Using the same test setup with 10G ZRAM and enabling all mTHP sizes:

128kB swap failure rate:
Before: swpout:451599 swpout_fallback:54525
After: swpout:502710 swpout_fallback:870

256kB swap failure rate:
Before: swpout:63652 swpout_fallback:2708
After: swpout:65913 swpout_fallback:20

512kB swap failure rate:
Before: swpout:11663 swpout_fallback:1767
After: swpout:14480 swpout_fallback:6

2M swap failure rate:
Before: swpout:24 swpout_fallback:1442
After: swpout:1329 swpout_fallback:7

The success rate of large allocations is much higher.

Link: https://lore.kernel.org/linux-mm/87v8242vng.fsf@yhuang6-desk2.ccr.corp.intel.com/ [1]
Link: https://lkml.kernel.org/r/20250806161748.76651-4-ryncsn@gmail.com
Signed-off-by: Kairui Song <kasong@tencent.com>
Acked-by: Chris Li <chrisl@kernel.org>
Reviewed-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>

authored by

Kairui Song and committed by
Andrew Morton
9a42aed4 913fff31

+28 -10
+28 -10
mm/swapfile.c
··· 908 908 } 909 909 910 910 new_cluster: 911 - ci = isolate_lock_cluster(si, &si->free_clusters); 912 - if (ci) { 913 - found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), 914 - order, usage); 915 - if (found) 916 - goto done; 911 + /* 912 + * If the device need discard, prefer new cluster over nonfull 913 + * to spread out the writes. 914 + */ 915 + if (si->flags & SWP_PAGE_DISCARD) { 916 + ci = isolate_lock_cluster(si, &si->free_clusters); 917 + if (ci) { 918 + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), 919 + order, usage); 920 + if (found) 921 + goto done; 922 + } 917 923 } 918 - 919 - /* Try reclaim from full clusters if free clusters list is drained */ 920 - if (vm_swap_full()) 921 - swap_reclaim_full_clusters(si, false); 922 924 923 925 if (order < PMD_ORDER) { 924 926 while ((ci = isolate_lock_cluster(si, &si->nonfull_clusters[order]))) { ··· 929 927 if (found) 930 928 goto done; 931 929 } 930 + } 932 931 932 + if (!(si->flags & SWP_PAGE_DISCARD)) { 933 + ci = isolate_lock_cluster(si, &si->free_clusters); 934 + if (ci) { 935 + found = alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), 936 + order, usage); 937 + if (found) 938 + goto done; 939 + } 940 + } 941 + 942 + /* Try reclaim full clusters if free and nonfull lists are drained */ 943 + if (vm_swap_full()) 944 + swap_reclaim_full_clusters(si, false); 945 + 946 + if (order < PMD_ORDER) { 933 947 /* 934 948 * Scan only one fragment cluster is good enough. Order 0 935 949 * allocation will surely success, and large allocation