mm/zswap: split zswap rb-tree
authorChengming Zhou <zhouchengming@bytedance.com>
Fri, 19 Jan 2024 11:22:23 +0000 (11:22 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 22 Feb 2024 18:24:39 +0000 (10:24 -0800)
Each swapfile has one rb-tree to search the mapping of swp_entry_t to
zswap_entry, that use a spinlock to protect, which can cause heavy lock
contention if multiple tasks zswap_store/load concurrently.

Optimize the scalability problem by splitting the zswap rb-tree into
multiple rb-trees, each corresponds to SWAP_ADDRESS_SPACE_PAGES (64M),
just like we did in the swap cache address_space splitting.

Although this method can't solve the spinlock contention completely, it
can mitigate much of that contention.  Below is the results of kernel
build in tmpfs with zswap shrinker enabled:

     linux-next  zswap-lock-optimize
real 1m9.181s    1m3.820s
user 17m44.036s  17m40.100s
sys  7m37.297s   4m54.622s

So there are clearly improvements.

Link: https://lkml.kernel.org/r/20240117-b4-zswap-lock-optimize-v2-2-b5cc55479090@bytedance.com
Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Nhat Pham <nphamcs@gmail.com>
Acked-by: Yosry Ahmed <yosryahmed@google.com>
Cc: Chris Li <chriscli@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/zswap.h
mm/swapfile.c
mm/zswap.c

index eca3882..91895ce 100644 (file)
@@ -30,7 +30,7 @@ struct zswap_lruvec_state {
 bool zswap_store(struct folio *folio);
 bool zswap_load(struct folio *folio);
 void zswap_invalidate(int type, pgoff_t offset);
-int zswap_swapon(int type);
+int zswap_swapon(int type, unsigned long nr_pages);
 void zswap_swapoff(int type);
 void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg);
 void zswap_lruvec_state_init(struct lruvec *lruvec);
@@ -51,7 +51,7 @@ static inline bool zswap_load(struct folio *folio)
 }
 
 static inline void zswap_invalidate(int type, pgoff_t offset) {}
-static inline int zswap_swapon(int type)
+static inline int zswap_swapon(int type, unsigned long nr_pages)
 {
        return 0;
 }
index b3a83c5..0c6dde8 100644 (file)
@@ -3165,7 +3165,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        if (error)
                goto bad_swap_unlock_inode;
 
-       error = zswap_swapon(p->type);
+       error = zswap_swapon(p->type, maxpages);
        if (error)
                goto free_swap_address_space;
 
index 5a40a7b..464179d 100644 (file)
@@ -239,6 +239,7 @@ struct zswap_tree {
 };
 
 static struct zswap_tree *zswap_trees[MAX_SWAPFILES];
+static unsigned int nr_zswap_trees[MAX_SWAPFILES];
 
 /* RCU-protected iteration */
 static LIST_HEAD(zswap_pools);
@@ -265,6 +266,12 @@ static bool zswap_has_pool;
 * helpers and fwd declarations
 **********************************/
 
+static inline struct zswap_tree *swap_zswap_tree(swp_entry_t swp)
+{
+       return &zswap_trees[swp_type(swp)][swp_offset(swp)
+               >> SWAP_ADDRESS_SPACE_SHIFT];
+}
+
 #define zswap_pool_debug(msg, p)                               \
        pr_debug("%s pool %s/%s\n", msg, (p)->tfm_name,         \
                 zpool_get_type((p)->zpools[0]))
@@ -864,7 +871,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
         * until the entry is verified to still be alive in the tree.
         */
        swpoffset = swp_offset(entry->swpentry);
-       tree = zswap_trees[swp_type(entry->swpentry)];
+       tree = swap_zswap_tree(entry->swpentry);
        list_lru_isolate(l, item);
        /*
         * It's safe to drop the lock here because we return either
@@ -1493,10 +1500,9 @@ static void zswap_fill_page(void *ptr, unsigned long value)
 bool zswap_store(struct folio *folio)
 {
        swp_entry_t swp = folio->swap;
-       int type = swp_type(swp);
        pgoff_t offset = swp_offset(swp);
        struct page *page = &folio->page;
-       struct zswap_tree *tree = zswap_trees[type];
+       struct zswap_tree *tree = swap_zswap_tree(swp);
        struct zswap_entry *entry, *dupentry;
        struct scatterlist input, output;
        struct crypto_acomp_ctx *acomp_ctx;
@@ -1569,7 +1575,7 @@ bool zswap_store(struct folio *folio)
                src = kmap_local_page(page);
                if (zswap_is_page_same_filled(src, &value)) {
                        kunmap_local(src);
-                       entry->swpentry = swp_entry(type, offset);
+                       entry->swpentry = swp;
                        entry->length = 0;
                        entry->value = value;
                        atomic_inc(&zswap_same_filled_pages);
@@ -1651,7 +1657,7 @@ bool zswap_store(struct folio *folio)
        mutex_unlock(&acomp_ctx->mutex);
 
        /* populate entry */
-       entry->swpentry = swp_entry(type, offset);
+       entry->swpentry = swp;
        entry->handle = handle;
        entry->length = dlen;
 
@@ -1711,10 +1717,9 @@ shrink:
 bool zswap_load(struct folio *folio)
 {
        swp_entry_t swp = folio->swap;
-       int type = swp_type(swp);
        pgoff_t offset = swp_offset(swp);
        struct page *page = &folio->page;
-       struct zswap_tree *tree = zswap_trees[type];
+       struct zswap_tree *tree = swap_zswap_tree(swp);
        struct zswap_entry *entry;
        u8 *dst;
 
@@ -1757,7 +1762,7 @@ bool zswap_load(struct folio *folio)
 
 void zswap_invalidate(int type, pgoff_t offset)
 {
-       struct zswap_tree *tree = zswap_trees[type];
+       struct zswap_tree *tree = swap_zswap_tree(swp_entry(type, offset));
        struct zswap_entry *entry;
 
        /* find */
@@ -1772,37 +1777,53 @@ void zswap_invalidate(int type, pgoff_t offset)
        spin_unlock(&tree->lock);
 }
 
-int zswap_swapon(int type)
+int zswap_swapon(int type, unsigned long nr_pages)
 {
-       struct zswap_tree *tree;
+       struct zswap_tree *trees, *tree;
+       unsigned int nr, i;
 
-       tree = kzalloc(sizeof(*tree), GFP_KERNEL);
-       if (!tree) {
+       nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
+       trees = kvcalloc(nr, sizeof(*tree), GFP_KERNEL);
+       if (!trees) {
                pr_err("alloc failed, zswap disabled for swap type %d\n", type);
                return -ENOMEM;
        }
 
-       tree->rbroot = RB_ROOT;
-       spin_lock_init(&tree->lock);
-       zswap_trees[type] = tree;
+       for (i = 0; i < nr; i++) {
+               tree = trees + i;
+               tree->rbroot = RB_ROOT;
+               spin_lock_init(&tree->lock);
+       }
+
+       nr_zswap_trees[type] = nr;
+       zswap_trees[type] = trees;
        return 0;
 }
 
 void zswap_swapoff(int type)
 {
-       struct zswap_tree *tree = zswap_trees[type];
-       struct zswap_entry *entry, *n;
+       struct zswap_tree *trees = zswap_trees[type];
+       unsigned int i;
 
-       if (!tree)
+       if (!trees)
                return;
 
-       /* walk the tree and free everything */
-       spin_lock(&tree->lock);
-       rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode)
-               zswap_free_entry(entry);
-       tree->rbroot = RB_ROOT;
-       spin_unlock(&tree->lock);
-       kfree(tree);
+       for (i = 0; i < nr_zswap_trees[type]; i++) {
+               struct zswap_tree *tree = trees + i;
+               struct zswap_entry *entry, *n;
+
+               /* walk the tree and free everything */
+               spin_lock(&tree->lock);
+               rbtree_postorder_for_each_entry_safe(entry, n,
+                                                    &tree->rbroot,
+                                                    rbnode)
+                       zswap_free_entry(entry);
+               tree->rbroot = RB_ROOT;
+               spin_unlock(&tree->lock);
+       }
+
+       kvfree(trees);
+       nr_zswap_trees[type] = 0;
        zswap_trees[type] = NULL;
 }