Skip to contentSkip to conversation

mm, swap: avoid over reclaim of full clusters

PATCH Superseded Kairui Song 1 participant 1 message Sep 25, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, swap: avoid over reclaim of full clusters

From: Kairui Song <[email protected]>

When running low on usable slots, cluster allocator will try to reclaim the full clusters aggressively to reclaim HAS_CACHE slots. This guarantees that as long as there are any usable slots, HAS_CACHE or not, the swap device will be usable and workload won't go OOM.

Before the cluster allocator, swap allocator fails easily if device is filled up with reclaimable HAS_CACHE slots. Which can be easily simulated with following simple program:

    #include <stdio.h>
    #include <string.h>
    #include <linux/mman.h>
    #include <sys/mman.h>
    #define SIZE 8192UL * 1024UL * 1024UL
    int main(int argc, char **argv) {
        long tmp;
        char *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        memset(p, 0, SIZE);
        madvise(p, SIZE, MADV_PAGEOUT);
        for (unsigned long i = 0; i < SIZE; ++i)
            tmp += p[i];
        getchar(); /* Pause */
        return 0;
    }

Setup a 8G non ramdisk swap, the first run of the program can swapout 8G ram successfully. But run same program again without existing previous one, it can't swapout all 8G memory as nearly half of the swap device is occupied by HAS_CACHE. There was a random scan in the old allocator may reclaim part of the HAS_CACHE by luck, but it's unreliable.

The new allocator's added reclaim of full clusters, but it has a hidden pitfall. When multiple CPUs are seeing the device is low on usable slots, they ran into a thundering herd problem, causing performance drop and delayed OOM kill.

This is observable on large machine with mass parallel workload. Test using 128G ZRAM on a 96 CPU system, first fill the swap with 124G place holder data, then run build linux kernel with make -j96 in a 1G memory Cgroup. The left 4G swap space is far not enough for the workload, but the OOM won't kick in until the workload hung for about ~5min.

Full cluster reclaim is slower for large device, and every CPU will drop the si->lock for reclaim, allowed other CPUs to re-entry the reclaim path. As a result, all CPUs are busy doing the full reclaim. Besides, the current full reclaim trigger condition is too lenient (available slots < cluster size, which was suppose to ensure mTHP user won't fail due to HAS_CACHE), making things more risky.

So, to ensure only one aggressively full cluster reclaim is issued when device is near full, offload the aggressively reclaim to a kworker instead. This still ensures in worst case the device won't be unusable because of HAS_CACHE slots.

And to avoid allocation (especially higher order) suffer from HAS_CACHE filling up clusters and kworker not responsive enough, do one synchronous scan every time the free list is drained, and only scan one cluster. This keeps the full clusters rotated and reclaimed with a minimal latency, and provide a fair reclaim strategy for most workloads.

Fixes 2cacbdfdee65 ("mm: swap: add a adaptive full cluster cache reclaim")
---
 include/linux/swap.h |  1 +
 mm/swapfile.c        | 49 +++++++++++++++++++++++++++-----------------
 2 files changed, 31 insertions(+), 19 deletions(-)
include/linux/swap.h +1 -0
index ca533b478c21..f3e0ac20c2e8 100644
@@ -335,6 +335,7 @@ struct swap_info_struct {
 					 * list.
 					 */
 	struct work_struct discard_work; /* discard worker */
+	struct work_struct reclaim_work; /* reclaim worker */
 	struct list_head discard_clusters; /* discard clusters list */
 	struct plist_node avail_lists[]; /*
 					   * entries in swap_avail_heads, one
mm/swapfile.c +30 -19

110 lines not on this page · raw message

Signature · 1 line
2.46.1