Linux 6.9-rc1
[linux-2.6-microblaze.git] / mm / zsmalloc.c
index 34f784a..7d7cb3e 100644 (file)
@@ -33,8 +33,7 @@
 /*
  * lock ordering:
  *     page_lock
- *     pool->migrate_lock
- *     class->lock
+ *     pool->lock
  *     zspage->lock
  */
 
  */
 #define ZS_ALIGN               8
 
-/*
- * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
- * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
- */
-#define ZS_MAX_ZSPAGE_ORDER 2
-#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
-
 #define ZS_HANDLE_SIZE (sizeof(unsigned long))
 
 /*
  * have room for two bit at least.
  */
 #define OBJ_ALLOCATED_TAG 1
-#define OBJ_TAG_BITS 1
-#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
+
+#define OBJ_TAG_BITS   1
+#define OBJ_TAG_MASK   OBJ_ALLOCATED_TAG
+
+#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
 
 #define HUGE_BITS      1
-#define FULLNESS_BITS  2
+#define FULLNESS_BITS  4
 #define CLASS_BITS     8
-#define ISOLATED_BITS  3
 #define MAGIC_VAL_BITS 8
 
 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
+
+#define ZS_MAX_PAGES_PER_ZSPAGE        (_AC(CONFIG_ZSMALLOC_CHAIN_SIZE, UL))
+
 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
 #define ZS_MIN_ALLOC_SIZE \
        MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
 #define ZS_SIZE_CLASSES        (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
                                      ZS_SIZE_CLASS_DELTA) + 1)
 
+/*
+ * Pages are distinguished by the ratio of used memory (that is the ratio
+ * of ->inuse objects to all objects that page can store). For example,
+ * INUSE_RATIO_10 means that the ratio of used objects is > 0% and <= 10%.
+ *
+ * The number of fullness groups is not random. It allows us to keep
+ * difference between the least busy page in the group (minimum permitted
+ * number of ->inuse objects) and the most busy page (maximum permitted
+ * number of ->inuse objects) at a reasonable value.
+ */
 enum fullness_group {
-       ZS_EMPTY,
-       ZS_ALMOST_EMPTY,
-       ZS_ALMOST_FULL,
-       ZS_FULL,
-       NR_ZS_FULLNESS,
+       ZS_INUSE_RATIO_0,
+       ZS_INUSE_RATIO_10,
+       /* NOTE: 8 more fullness groups here */
+       ZS_INUSE_RATIO_99       = 10,
+       ZS_INUSE_RATIO_100,
+       NR_FULLNESS_GROUPS,
 };
 
 enum class_stat_type {
-       CLASS_EMPTY,
-       CLASS_ALMOST_EMPTY,
-       CLASS_ALMOST_FULL,
-       CLASS_FULL,
-       OBJ_ALLOCATED,
-       OBJ_USED,
-       NR_ZS_STAT_TYPE,
+       /* NOTE: stats for 12 fullness groups here: from inuse 0 to 100 */
+       ZS_OBJS_ALLOCATED       = NR_FULLNESS_GROUPS,
+       ZS_OBJS_INUSE,
+       NR_CLASS_STAT_TYPES,
 };
 
 struct zs_size_stat {
-       unsigned long objs[NR_ZS_STAT_TYPE];
+       unsigned long objs[NR_CLASS_STAT_TYPES];
 };
 
 #ifdef CONFIG_ZSMALLOC_STAT
 static struct dentry *zs_stat_root;
 #endif
 
-/*
- * We assign a page to ZS_ALMOST_EMPTY fullness group when:
- *     n <= N / f, where
- * n = number of allocated objects
- * N = total number of objects zspage can store
- * f = fullness_threshold_frac
- *
- * Similarly, we assign zspage to:
- *     ZS_ALMOST_FULL  when n > N / f
- *     ZS_EMPTY        when n == 0
- *     ZS_FULL         when n == N
- *
- * (see: fix_fullness_group())
- */
-static const int fullness_threshold_frac = 4;
 static size_t huge_class_size;
 
 struct size_class {
-       spinlock_t lock;
-       struct list_head fullness_list[NR_ZS_FULLNESS];
+       struct list_head fullness_list[NR_FULLNESS_GROUPS];
        /*
         * Size of objects stored in this class. Must be multiple
         * of ZS_ALIGN.
@@ -239,7 +228,7 @@ struct zs_pool {
        struct zs_pool_stats stats;
 
        /* Compact classes */
-       struct shrinker shrinker;
+       struct shrinker *shrinker;
 
 #ifdef CONFIG_ZSMALLOC_STAT
        struct dentry *stat_dentry;
@@ -247,8 +236,8 @@ struct zs_pool {
 #ifdef CONFIG_COMPACTION
        struct work_struct free_work;
 #endif
-       /* protect page/zspage migration */
-       rwlock_t migrate_lock;
+       spinlock_t lock;
+       atomic_t compaction_in_progress;
 };
 
 struct zspage {
@@ -256,7 +245,6 @@ struct zspage {
                unsigned int huge:HUGE_BITS;
                unsigned int fullness:FULLNESS_BITS;
                unsigned int class:CLASS_BITS + 1;
-               unsigned int isolated:ISOLATED_BITS;
                unsigned int magic:MAGIC_VAL_BITS;
        };
        unsigned int inuse;
@@ -264,9 +252,7 @@ struct zspage {
        struct page *first_page;
        struct list_head list; /* fullness list */
        struct zs_pool *pool;
-#ifdef CONFIG_COMPACTION
        rwlock_t lock;
-#endif
 };
 
 struct mapping_area {
@@ -287,23 +273,17 @@ static bool ZsHugePage(struct zspage *zspage)
        return zspage->huge;
 }
 
-#ifdef CONFIG_COMPACTION
 static void migrate_lock_init(struct zspage *zspage);
 static void migrate_read_lock(struct zspage *zspage);
 static void migrate_read_unlock(struct zspage *zspage);
 static void migrate_write_lock(struct zspage *zspage);
-static void migrate_write_lock_nested(struct zspage *zspage);
 static void migrate_write_unlock(struct zspage *zspage);
+
+#ifdef CONFIG_COMPACTION
 static void kick_deferred_free(struct zs_pool *pool);
 static void init_deferred_free(struct zs_pool *pool);
 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
 #else
-static void migrate_lock_init(struct zspage *zspage) {}
-static void migrate_read_lock(struct zspage *zspage) {}
-static void migrate_read_unlock(struct zspage *zspage) {}
-static void migrate_write_lock(struct zspage *zspage) {}
-static void migrate_write_lock_nested(struct zspage *zspage) {}
-static void migrate_write_unlock(struct zspage *zspage) {}
 static void kick_deferred_free(struct zs_pool *pool) {}
 static void init_deferred_free(struct zs_pool *pool) {}
 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
@@ -355,7 +335,7 @@ static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
        kmem_cache_free(pool->zspage_cachep, zspage);
 }
 
-/* class->lock(which owns the handle) synchronizes races */
+/* pool->lock(which owns the handle) synchronizes races */
 static void record_obj(unsigned long handle, unsigned long obj)
 {
        *(unsigned long *)handle = obj;
@@ -365,9 +345,7 @@ static void record_obj(unsigned long handle, unsigned long obj)
 
 #ifdef CONFIG_ZPOOL
 
-static void *zs_zpool_create(const char *name, gfp_t gfp,
-                            const struct zpool_ops *zpool_ops,
-                            struct zpool *zpool)
+static void *zs_zpool_create(const char *name, gfp_t gfp)
 {
        /*
         * Ignore global gfp flags: zs_malloc() may be invoked from
@@ -387,7 +365,7 @@ static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
 {
        *handle = zs_malloc(pool, size, gfp);
 
-       if (IS_ERR((void *)(*handle)))
+       if (IS_ERR_VALUE(*handle))
                return PTR_ERR((void *)*handle);
        return 0;
 }
@@ -452,7 +430,7 @@ static __maybe_unused int is_first_page(struct page *page)
        return PagePrivate(page);
 }
 
-/* Protected by class->lock */
+/* Protected by pool->lock */
 static inline int get_zspage_inuse(struct zspage *zspage)
 {
        return zspage->inuse;
@@ -472,12 +450,12 @@ static inline struct page *get_first_page(struct zspage *zspage)
        return first_page;
 }
 
-static inline int get_first_obj_offset(struct page *page)
+static inline unsigned int get_first_obj_offset(struct page *page)
 {
        return page->page_type;
 }
 
-static inline void set_first_obj_offset(struct page *page, int offset)
+static inline void set_first_obj_offset(struct page *page, unsigned int offset)
 {
        page->page_type = offset;
 }
@@ -492,30 +470,12 @@ static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
        zspage->freeobj = obj;
 }
 
-static void get_zspage_mapping(struct zspage *zspage,
-                               unsigned int *class_idx,
-                               enum fullness_group *fullness)
-{
-       BUG_ON(zspage->magic != ZSPAGE_MAGIC);
-
-       *fullness = zspage->fullness;
-       *class_idx = zspage->class;
-}
-
 static struct size_class *zspage_class(struct zs_pool *pool,
-                                            struct zspage *zspage)
+                                      struct zspage *zspage)
 {
        return pool->size_class[zspage->class];
 }
 
-static void set_zspage_mapping(struct zspage *zspage,
-                               unsigned int class_idx,
-                               enum fullness_group fullness)
-{
-       zspage->class = class_idx;
-       zspage->fullness = fullness;
-}
-
 /*
  * zsmalloc divides the pool into various size classes where each
  * class maintains a list of zspages where each zspage is divided
@@ -534,23 +494,19 @@ static int get_size_class_index(int size)
        return min_t(int, ZS_SIZE_CLASSES - 1, idx);
 }
 
-/* type can be of enum type class_stat_type or fullness_group */
 static inline void class_stat_inc(struct size_class *class,
                                int type, unsigned long cnt)
 {
        class->stats.objs[type] += cnt;
 }
 
-/* type can be of enum type class_stat_type or fullness_group */
 static inline void class_stat_dec(struct size_class *class,
                                int type, unsigned long cnt)
 {
        class->stats.objs[type] -= cnt;
 }
 
-/* type can be of enum type class_stat_type or fullness_group */
-static inline unsigned long zs_stat_get(struct size_class *class,
-                               int type)
+static inline unsigned long zs_stat_get(struct size_class *class, int type)
 {
        return class->stats.objs[type];
 }
@@ -576,47 +532,49 @@ static unsigned long zs_can_compact(struct size_class *class);
 
 static int zs_stats_size_show(struct seq_file *s, void *v)
 {
-       int i;
+       int i, fg;
        struct zs_pool *pool = s->private;
        struct size_class *class;
        int objs_per_zspage;
-       unsigned long class_almost_full, class_almost_empty;
        unsigned long obj_allocated, obj_used, pages_used, freeable;
-       unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
        unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
        unsigned long total_freeable = 0;
+       unsigned long inuse_totals[NR_FULLNESS_GROUPS] = {0, };
 
-       seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
-                       "class", "size", "almost_full", "almost_empty",
+       seq_printf(s, " %5s %5s %9s %9s %9s %9s %9s %9s %9s %9s %9s %9s %9s %13s %10s %10s %16s %8s\n",
+                       "class", "size", "10%", "20%", "30%", "40%",
+                       "50%", "60%", "70%", "80%", "90%", "99%", "100%",
                        "obj_allocated", "obj_used", "pages_used",
                        "pages_per_zspage", "freeable");
 
        for (i = 0; i < ZS_SIZE_CLASSES; i++) {
+
                class = pool->size_class[i];
 
                if (class->index != i)
                        continue;
 
-               spin_lock(&class->lock);
-               class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
-               class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
-               obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
-               obj_used = zs_stat_get(class, OBJ_USED);
+               spin_lock(&pool->lock);
+
+               seq_printf(s, " %5u %5u ", i, class->size);
+               for (fg = ZS_INUSE_RATIO_10; fg < NR_FULLNESS_GROUPS; fg++) {
+                       inuse_totals[fg] += zs_stat_get(class, fg);
+                       seq_printf(s, "%9lu ", zs_stat_get(class, fg));
+               }
+
+               obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED);
+               obj_used = zs_stat_get(class, ZS_OBJS_INUSE);
                freeable = zs_can_compact(class);
-               spin_unlock(&class->lock);
+               spin_unlock(&pool->lock);
 
                objs_per_zspage = class->objs_per_zspage;
                pages_used = obj_allocated / objs_per_zspage *
                                class->pages_per_zspage;
 
-               seq_printf(s, " %5u %5u %11lu %12lu %13lu"
-                               " %10lu %10lu %16d %8lu\n",
-                       i, class->size, class_almost_full, class_almost_empty,
-                       obj_allocated, obj_used, pages_used,
-                       class->pages_per_zspage, freeable);
+               seq_printf(s, "%13lu %10lu %10lu %16d %8lu\n",
+                          obj_allocated, obj_used, pages_used,
+                          class->pages_per_zspage, freeable);
 
-               total_class_almost_full += class_almost_full;
-               total_class_almost_empty += class_almost_empty;
                total_objs += obj_allocated;
                total_used_objs += obj_used;
                total_pages += pages_used;
@@ -624,10 +582,14 @@ static int zs_stats_size_show(struct seq_file *s, void *v)
        }
 
        seq_puts(s, "\n");
-       seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
-                       "Total", "", total_class_almost_full,
-                       total_class_almost_empty, total_objs,
-                       total_used_objs, total_pages, "", total_freeable);
+       seq_printf(s, " %5s %5s ", "Total", "");
+
+       for (fg = ZS_INUSE_RATIO_10; fg < NR_FULLNESS_GROUPS; fg++)
+               seq_printf(s, "%9lu ", inuse_totals[fg]);
+
+       seq_printf(s, "%13lu %10lu %10lu %16s %8lu\n",
+                  total_objs, total_used_objs, total_pages, "",
+                  total_freeable);
 
        return 0;
 }
@@ -672,30 +634,28 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool)
 
 /*
  * For each size class, zspages are divided into different groups
- * depending on how "full" they are. This was done so that we could
- * easily find empty or nearly empty zspages when we try to shrink
- * the pool (not yet implemented). This function returns fullness
+ * depending on their usage ratio. This function returns fullness
  * status of the given page.
  */
-static enum fullness_group get_fullness_group(struct size_class *class,
-                                               struct zspage *zspage)
+static int get_fullness_group(struct size_class *class, struct zspage *zspage)
 {
-       int inuse, objs_per_zspage;
-       enum fullness_group fg;
+       int inuse, objs_per_zspage, ratio;
 
        inuse = get_zspage_inuse(zspage);
        objs_per_zspage = class->objs_per_zspage;
 
        if (inuse == 0)
-               fg = ZS_EMPTY;
-       else if (inuse == objs_per_zspage)
-               fg = ZS_FULL;
-       else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
-               fg = ZS_ALMOST_EMPTY;
-       else
-               fg = ZS_ALMOST_FULL;
+               return ZS_INUSE_RATIO_0;
+       if (inuse == objs_per_zspage)
+               return ZS_INUSE_RATIO_100;
 
-       return fg;
+       ratio = 100 * inuse / objs_per_zspage;
+       /*
+        * Take integer division into consideration: a page with one inuse
+        * object out of 127 possible, will end up having 0 usage ratio,
+        * which is wrong as it belongs in ZS_INUSE_RATIO_10 fullness group.
+        */
+       return ratio / 10 + 1;
 }
 
 /*
@@ -706,31 +666,21 @@ static enum fullness_group get_fullness_group(struct size_class *class,
  */
 static void insert_zspage(struct size_class *class,
                                struct zspage *zspage,
-                               enum fullness_group fullness)
+                               int fullness)
 {
-       struct zspage *head;
-
        class_stat_inc(class, fullness, 1);
-       head = list_first_entry_or_null(&class->fullness_list[fullness],
-                                       struct zspage, list);
-       /*
-        * We want to see more ZS_FULL pages and less almost empty/full.
-        * Put pages with higher ->inuse first.
-        */
-       if (head && get_zspage_inuse(zspage) < get_zspage_inuse(head))
-               list_add(&zspage->list, &head->list);
-       else
-               list_add(&zspage->list, &class->fullness_list[fullness]);
+       list_add(&zspage->list, &class->fullness_list[fullness]);
+       zspage->fullness = fullness;
 }
 
 /*
  * This function removes the given zspage from the freelist identified
  * by <class, fullness_group>.
  */
-static void remove_zspage(struct size_class *class,
-                               struct zspage *zspage,
-                               enum fullness_group fullness)
+static void remove_zspage(struct size_class *class, struct zspage *zspage)
 {
+       int fullness = zspage->fullness;
+
        VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
 
        list_del_init(&zspage->list);
@@ -740,66 +690,26 @@ static void remove_zspage(struct size_class *class,
 /*
  * Each size class maintains zspages in different fullness groups depending
  * on the number of live objects they contain. When allocating or freeing
- * objects, the fullness status of the page can change, say, from ALMOST_FULL
- * to ALMOST_EMPTY when freeing an object. This function checks if such
- * a status change has occurred for the given page and accordingly moves the
- * page from the freelist of the old fullness group to that of the new
- * fullness group.
+ * objects, the fullness status of the page can change, for instance, from
+ * INUSE_RATIO_80 to INUSE_RATIO_70 when freeing an object. This function
+ * checks if such a status change has occurred for the given page and
+ * accordingly moves the page from the list of the old fullness group to that
+ * of the new fullness group.
  */
-static enum fullness_group fix_fullness_group(struct size_class *class,
-                                               struct zspage *zspage)
+static int fix_fullness_group(struct size_class *class, struct zspage *zspage)
 {
-       int class_idx;
-       enum fullness_group currfg, newfg;
+       int newfg;
 
-       get_zspage_mapping(zspage, &class_idx, &currfg);
        newfg = get_fullness_group(class, zspage);
-       if (newfg == currfg)
+       if (newfg == zspage->fullness)
                goto out;
 
-       remove_zspage(class, zspage, currfg);
+       remove_zspage(class, zspage);
        insert_zspage(class, zspage, newfg);
-       set_zspage_mapping(zspage, class_idx, newfg);
 out:
        return newfg;
 }
 
-/*
- * We have to decide on how many pages to link together
- * to form a zspage for each size class. This is important
- * to reduce wastage due to unusable space left at end of
- * each zspage which is given as:
- *     wastage = Zp % class_size
- *     usage = Zp - wastage
- * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
- *
- * For example, for size class of 3/8 * PAGE_SIZE, we should
- * link together 3 PAGE_SIZE sized pages to form a zspage
- * since then we can perfectly fit in 8 such objects.
- */
-static int get_pages_per_zspage(int class_size)
-{
-       int i, max_usedpc = 0;
-       /* zspage order which gives maximum used size per KB */
-       int max_usedpc_order = 1;
-
-       for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
-               int zspage_size;
-               int waste, usedpc;
-
-               zspage_size = i * PAGE_SIZE;
-               waste = zspage_size % class_size;
-               usedpc = (zspage_size - waste) * 100 / zspage_size;
-
-               if (usedpc > max_usedpc) {
-                       max_usedpc = usedpc;
-                       max_usedpc_order = i;
-               }
-       }
-
-       return max_usedpc_order;
-}
-
 static struct zspage *get_zspage(struct page *page)
 {
        struct zspage *zspage = (struct zspage *)page_private(page);
@@ -827,14 +737,12 @@ static struct page *get_next_page(struct page *page)
 static void obj_to_location(unsigned long obj, struct page **page,
                                unsigned int *obj_idx)
 {
-       obj >>= OBJ_TAG_BITS;
        *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
        *obj_idx = (obj & OBJ_INDEX_MASK);
 }
 
 static void obj_to_page(unsigned long obj, struct page **page)
 {
-       obj >>= OBJ_TAG_BITS;
        *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
 }
 
@@ -849,7 +757,6 @@ static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
 
        obj = page_to_pfn(page) << OBJ_INDEX_BITS;
        obj |= obj_idx & OBJ_INDEX_MASK;
-       obj <<= OBJ_TAG_BITS;
 
        return obj;
 }
@@ -859,7 +766,8 @@ static unsigned long handle_to_obj(unsigned long handle)
        return *(unsigned long *)handle;
 }
 
-static bool obj_allocated(struct page *page, void *obj, unsigned long *phandle)
+static inline bool obj_allocated(struct page *page, void *obj,
+                                unsigned long *phandle)
 {
        unsigned long handle;
        struct zspage *zspage = get_zspage(page);
@@ -873,7 +781,8 @@ static bool obj_allocated(struct page *page, void *obj, unsigned long *phandle)
        if (!(handle & OBJ_ALLOCATED_TAG))
                return false;
 
-       *phandle = handle & ~OBJ_ALLOCATED_TAG;
+       /* Clear all tags before returning the handle */
+       *phandle = handle & ~OBJ_TAG_MASK;
        return true;
 }
 
@@ -911,15 +820,11 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class,
                                struct zspage *zspage)
 {
        struct page *page, *next;
-       enum fullness_group fg;
-       unsigned int class_idx;
 
-       get_zspage_mapping(zspage, &class_idx, &fg);
-
-       assert_spin_locked(&class->lock);
+       assert_spin_locked(&pool->lock);
 
        VM_BUG_ON(get_zspage_inuse(zspage));
-       VM_BUG_ON(fg != ZS_EMPTY);
+       VM_BUG_ON(zspage->fullness != ZS_INUSE_RATIO_0);
 
        next = page = get_first_page(zspage);
        do {
@@ -934,9 +839,8 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class,
 
        cache_free_zspage(pool, zspage);
 
-       class_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
-       atomic_long_sub(class->pages_per_zspage,
-                                       &pool->pages_allocated);
+       class_stat_dec(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage);
+       atomic_long_sub(class->pages_per_zspage, &pool->pages_allocated);
 }
 
 static void free_zspage(struct zs_pool *pool, struct size_class *class,
@@ -955,7 +859,7 @@ static void free_zspage(struct zs_pool *pool, struct size_class *class,
                return;
        }
 
-       remove_zspage(class, zspage, ZS_EMPTY);
+       remove_zspage(class, zspage);
        __free_zspage(pool, class, zspage);
 }
 
@@ -1074,6 +978,7 @@ static struct zspage *alloc_zspage(struct zs_pool *pool,
        create_page_chain(class, zspage, pages);
        init_zspage(class, zspage);
        zspage->pool = pool;
+       zspage->class = class->index;
 
        return zspage;
 }
@@ -1083,9 +988,9 @@ static struct zspage *find_get_zspage(struct size_class *class)
        int i;
        struct zspage *zspage;
 
-       for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
+       for (i = ZS_INUSE_RATIO_99; i >= ZS_INUSE_RATIO_0; i--) {
                zspage = list_first_entry_or_null(&class->fullness_list[i],
-                               struct zspage, list);
+                                                 struct zspage, list);
                if (zspage)
                        break;
        }
@@ -1205,6 +1110,32 @@ static bool zspage_full(struct size_class *class, struct zspage *zspage)
        return get_zspage_inuse(zspage) == class->objs_per_zspage;
 }
 
+static bool zspage_empty(struct zspage *zspage)
+{
+       return get_zspage_inuse(zspage) == 0;
+}
+
+/**
+ * zs_lookup_class_index() - Returns index of the zsmalloc &size_class
+ * that hold objects of the provided size.
+ * @pool: zsmalloc pool to use
+ * @size: object size
+ *
+ * Context: Any context.
+ *
+ * Return: the index of the zsmalloc &size_class that hold objects of the
+ * provided size.
+ */
+unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
+{
+       struct size_class *class;
+
+       class = pool->size_class[get_size_class_index(size)];
+
+       return class->index;
+}
+EXPORT_SYMBOL_GPL(zs_lookup_class_index);
+
 unsigned long zs_get_total_pages(struct zs_pool *pool)
 {
        return atomic_long_read(&pool->pages_allocated);
@@ -1247,22 +1178,22 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
        BUG_ON(in_interrupt());
 
        /* It guarantees it can get zspage from handle safely */
-       read_lock(&pool->migrate_lock);
+       spin_lock(&pool->lock);
        obj = handle_to_obj(handle);
        obj_to_location(obj, &page, &obj_idx);
        zspage = get_zspage(page);
 
        /*
-        * migration cannot move any zpages in this zspage. Here, class->lock
+        * migration cannot move any zpages in this zspage. Here, pool->lock
         * is too heavy since callers would take some time until they calls
         * zs_unmap_object API so delegate the locking from class to zspage
         * which is smaller granularity.
         */
        migrate_read_lock(zspage);
-       read_unlock(&pool->migrate_lock);
+       spin_unlock(&pool->lock);
 
        class = zspage_class(pool, zspage);
-       off = (class->size * obj_idx) & ~PAGE_MASK;
+       off = offset_in_page(class->size * obj_idx);
 
        local_lock(&zs_map_area.lock);
        area = this_cpu_ptr(&zs_map_area);
@@ -1302,7 +1233,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
        obj_to_location(obj, &page, &obj_idx);
        zspage = get_zspage(page);
        class = zspage_class(pool, zspage);
-       off = (class->size * obj_idx) & ~PAGE_MASK;
+       off = offset_in_page(class->size * obj_idx);
 
        area = this_cpu_ptr(&zs_map_area);
        if (off + class->size <= PAGE_SIZE)
@@ -1359,7 +1290,7 @@ static unsigned long obj_malloc(struct zs_pool *pool,
 
        offset = obj * class->size;
        nr_page = offset >> PAGE_SHIFT;
-       m_offset = offset & ~PAGE_MASK;
+       m_offset = offset_in_page(offset);
        m_page = get_first_page(zspage);
 
        for (i = 0; i < nr_page; i++)
@@ -1398,12 +1329,15 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
 {
        unsigned long handle, obj;
        struct size_class *class;
-       enum fullness_group newfg;
+       int newfg;
        struct zspage *zspage;
 
-       if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
+       if (unlikely(!size))
                return (unsigned long)ERR_PTR(-EINVAL);
 
+       if (unlikely(size > ZS_MAX_ALLOC_SIZE))
+               return (unsigned long)ERR_PTR(-ENOSPC);
+
        handle = cache_alloc_handle(pool, gfp);
        if (!handle)
                return (unsigned long)ERR_PTR(-ENOMEM);
@@ -1412,21 +1346,20 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
        size += ZS_HANDLE_SIZE;
        class = pool->size_class[get_size_class_index(size)];
 
-       /* class->lock effectively protects the zpage migration */
-       spin_lock(&class->lock);
+       /* pool->lock effectively protects the zpage migration */
+       spin_lock(&pool->lock);
        zspage = find_get_zspage(class);
        if (likely(zspage)) {
                obj = obj_malloc(pool, zspage, handle);
                /* Now move the zspage to another fullness group, if required */
                fix_fullness_group(class, zspage);
                record_obj(handle, obj);
-               class_stat_inc(class, OBJ_USED, 1);
-               spin_unlock(&class->lock);
+               class_stat_inc(class, ZS_OBJS_INUSE, 1);
 
-               return handle;
+               goto out;
        }
 
-       spin_unlock(&class->lock);
+       spin_unlock(&pool->lock);
 
        zspage = alloc_zspage(pool, class, gfp);
        if (!zspage) {
@@ -1434,20 +1367,19 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
                return (unsigned long)ERR_PTR(-ENOMEM);
        }
 
-       spin_lock(&class->lock);
+       spin_lock(&pool->lock);
        obj = obj_malloc(pool, zspage, handle);
        newfg = get_fullness_group(class, zspage);
        insert_zspage(class, zspage, newfg);
-       set_zspage_mapping(zspage, class->index, newfg);
        record_obj(handle, obj);
-       atomic_long_add(class->pages_per_zspage,
-                               &pool->pages_allocated);
-       class_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
-       class_stat_inc(class, OBJ_USED, 1);
+       atomic_long_add(class->pages_per_zspage, &pool->pages_allocated);
+       class_stat_inc(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage);
+       class_stat_inc(class, ZS_OBJS_INUSE, 1);
 
        /* We completely set up zspage so mark them as movable */
        SetZsPageMovable(pool, zspage);
-       spin_unlock(&class->lock);
+out:
+       spin_unlock(&pool->lock);
 
        return handle;
 }
@@ -1463,19 +1395,20 @@ static void obj_free(int class_size, unsigned long obj)
        void *vaddr;
 
        obj_to_location(obj, &f_page, &f_objidx);
-       f_offset = (class_size * f_objidx) & ~PAGE_MASK;
+       f_offset = offset_in_page(class_size * f_objidx);
        zspage = get_zspage(f_page);
 
        vaddr = kmap_atomic(f_page);
+       link = (struct link_free *)(vaddr + f_offset);
 
        /* Insert this object in containing zspage's freelist */
-       link = (struct link_free *)(vaddr + f_offset);
        if (likely(!ZsHugePage(zspage)))
                link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
        else
                f_page->index = 0;
-       kunmap_atomic(vaddr);
        set_freeobj(zspage, f_objidx);
+
+       kunmap_atomic(vaddr);
        mod_zspage_inuse(zspage, -1);
 }
 
@@ -1485,32 +1418,29 @@ void zs_free(struct zs_pool *pool, unsigned long handle)
        struct page *f_page;
        unsigned long obj;
        struct size_class *class;
-       enum fullness_group fullness;
+       int fullness;
 
-       if (unlikely(!handle))
+       if (IS_ERR_OR_NULL((void *)handle))
                return;
 
        /*
-        * The pool->migrate_lock protects the race with zpage's migration
+        * The pool->lock protects the race with zpage's migration
         * so it's safe to get the page from handle.
         */
-       read_lock(&pool->migrate_lock);
+       spin_lock(&pool->lock);
        obj = handle_to_obj(handle);
        obj_to_page(obj, &f_page);
        zspage = get_zspage(f_page);
        class = zspage_class(pool, zspage);
-       spin_lock(&class->lock);
-       read_unlock(&pool->migrate_lock);
 
+       class_stat_dec(class, ZS_OBJS_INUSE, 1);
        obj_free(class->size, obj);
-       class_stat_dec(class, OBJ_USED, 1);
+
        fullness = fix_fullness_group(class, zspage);
-       if (fullness != ZS_EMPTY)
-               goto out;
+       if (fullness == ZS_INUSE_RATIO_0)
+               free_zspage(pool, class, zspage);
 
-       free_zspage(pool, class, zspage);
-out:
-       spin_unlock(&class->lock);
+       spin_unlock(&pool->lock);
        cache_free_handle(pool, handle);
 }
 EXPORT_SYMBOL_GPL(zs_free);
@@ -1530,8 +1460,8 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
        obj_to_location(src, &s_page, &s_objidx);
        obj_to_location(dst, &d_page, &d_objidx);
 
-       s_off = (class->size * s_objidx) & ~PAGE_MASK;
-       d_off = (class->size * d_objidx) & ~PAGE_MASK;
+       s_off = offset_in_page(class->size * s_objidx);
+       d_off = offset_in_page(class->size * d_objidx);
 
        if (s_off + class->size > PAGE_SIZE)
                s_size = PAGE_SIZE - s_off;
@@ -1555,6 +1485,13 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
                d_off += size;
                d_size -= size;
 
+               /*
+                * Calling kunmap_atomic(d_addr) is necessary. kunmap_atomic()
+                * calls must occurs in reverse order of calls to kmap_atomic().
+                * So, to call kunmap_atomic(s_addr) we should first call
+                * kunmap_atomic(d_addr). For more details see
+                * Documentation/mm/highmem.rst.
+                */
                if (s_off >= PAGE_SIZE) {
                        kunmap_atomic(d_addr);
                        kunmap_atomic(s_addr);
@@ -1583,9 +1520,9 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
  * return handle.
  */
 static unsigned long find_alloced_obj(struct size_class *class,
-                                       struct page *page, int *obj_idx)
+                                     struct page *page, int *obj_idx)
 {
-       int offset = 0;
+       unsigned int offset;
        int index = *obj_idx;
        unsigned long handle = 0;
        void *addr = kmap_atomic(page);
@@ -1608,26 +1545,14 @@ static unsigned long find_alloced_obj(struct size_class *class,
        return handle;
 }
 
-struct zs_compact_control {
-       /* Source spage for migration which could be a subpage of zspage */
-       struct page *s_page;
-       /* Destination page for migration which should be a first page
-        * of zspage. */
-       struct page *d_page;
-        /* Starting object index within @s_page which used for live object
-         * in the subpage. */
-       int obj_idx;
-};
-
-static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
-                               struct zs_compact_control *cc)
+static void migrate_zspage(struct zs_pool *pool, struct zspage *src_zspage,
+                          struct zspage *dst_zspage)
 {
        unsigned long used_obj, free_obj;
        unsigned long handle;
-       struct page *s_page = cc->s_page;
-       struct page *d_page = cc->d_page;
-       int obj_idx = cc->obj_idx;
-       int ret = 0;
+       int obj_idx = 0;
+       struct page *s_page = get_first_page(src_zspage);
+       struct size_class *class = pool->size_class[src_zspage->class];
 
        while (1) {
                handle = find_alloced_obj(class, s_page, &obj_idx);
@@ -1639,43 +1564,50 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
                        continue;
                }
 
-               /* Stop if there is no more space */
-               if (zspage_full(class, get_zspage(d_page))) {
-                       ret = -ENOMEM;
-                       break;
-               }
-
                used_obj = handle_to_obj(handle);
-               free_obj = obj_malloc(pool, get_zspage(d_page), handle);
+               free_obj = obj_malloc(pool, dst_zspage, handle);
                zs_object_copy(class, free_obj, used_obj);
                obj_idx++;
                record_obj(handle, free_obj);
                obj_free(class->size, used_obj);
-       }
 
-       /* Remember last position in this iteration */
-       cc->s_page = s_page;
-       cc->obj_idx = obj_idx;
+               /* Stop if there is no more space */
+               if (zspage_full(class, dst_zspage))
+                       break;
 
-       return ret;
+               /* Stop if there are no more objects to migrate */
+               if (zspage_empty(src_zspage))
+                       break;
+       }
 }
 
-static struct zspage *isolate_zspage(struct size_class *class, bool source)
+static struct zspage *isolate_src_zspage(struct size_class *class)
 {
-       int i;
        struct zspage *zspage;
-       enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
+       int fg;
 
-       if (!source) {
-               fg[0] = ZS_ALMOST_FULL;
-               fg[1] = ZS_ALMOST_EMPTY;
+       for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_99; fg++) {
+               zspage = list_first_entry_or_null(&class->fullness_list[fg],
+                                                 struct zspage, list);
+               if (zspage) {
+                       remove_zspage(class, zspage);
+                       return zspage;
+               }
        }
 
-       for (i = 0; i < 2; i++) {
-               zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
-                                                       struct zspage, list);
+       return zspage;
+}
+
+static struct zspage *isolate_dst_zspage(struct size_class *class)
+{
+       struct zspage *zspage;
+       int fg;
+
+       for (fg = ZS_INUSE_RATIO_99; fg >= ZS_INUSE_RATIO_10; fg--) {
+               zspage = list_first_entry_or_null(&class->fullness_list[fg],
+                                                 struct zspage, list);
                if (zspage) {
-                       remove_zspage(class, zspage, fg[i]);
+                       remove_zspage(class, zspage);
                        return zspage;
                }
        }
@@ -1688,16 +1620,14 @@ static struct zspage *isolate_zspage(struct size_class *class, bool source)
  * @class: destination class
  * @zspage: target page
  *
- * Return @zspage's fullness_group
+ * Return @zspage's fullness status
  */
-static enum fullness_group putback_zspage(struct size_class *class,
-                       struct zspage *zspage)
+static int putback_zspage(struct size_class *class, struct zspage *zspage)
 {
-       enum fullness_group fullness;
+       int fullness;
 
        fullness = get_fullness_group(class, zspage);
        insert_zspage(class, zspage, fullness);
-       set_zspage_mapping(zspage, class->index, fullness);
 
        return fullness;
 }
@@ -1744,6 +1674,7 @@ static void lock_zspage(struct zspage *zspage)
        }
        migrate_read_unlock(zspage);
 }
+#endif /* CONFIG_COMPACTION */
 
 static void migrate_lock_init(struct zspage *zspage)
 {
@@ -1765,27 +1696,12 @@ static void migrate_write_lock(struct zspage *zspage)
        write_lock(&zspage->lock);
 }
 
-static void migrate_write_lock_nested(struct zspage *zspage)
-{
-       write_lock_nested(&zspage->lock, SINGLE_DEPTH_NESTING);
-}
-
 static void migrate_write_unlock(struct zspage *zspage)
 {
        write_unlock(&zspage->lock);
 }
 
-/* Number of isolated subpage for *page migration* in this zspage */
-static void inc_zspage_isolation(struct zspage *zspage)
-{
-       zspage->isolated++;
-}
-
-static void dec_zspage_isolation(struct zspage *zspage)
-{
-       VM_BUG_ON(zspage->isolated == 0);
-       zspage->isolated--;
-}
+#ifdef CONFIG_COMPACTION
 
 static const struct movable_operations zsmalloc_mops;
 
@@ -1814,20 +1730,12 @@ static void replace_sub_page(struct size_class *class, struct zspage *zspage,
 
 static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
 {
-       struct zspage *zspage;
-
        /*
         * Page is locked so zspage couldn't be destroyed. For detail, look at
         * lock_zspage in free_zspage.
         */
-       VM_BUG_ON_PAGE(!PageMovable(page), page);
        VM_BUG_ON_PAGE(PageIsolated(page), page);
 
-       zspage = get_zspage(page);
-       migrate_write_lock(zspage);
-       inc_zspage_isolation(zspage);
-       migrate_write_unlock(zspage);
-
        return true;
 }
 
@@ -1839,7 +1747,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
        struct zspage *zspage;
        struct page *dummy;
        void *s_addr, *d_addr, *addr;
-       int offset;
+       unsigned int offset;
        unsigned long handle;
        unsigned long old_obj, new_obj;
        unsigned int obj_idx;
@@ -1852,7 +1760,6 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
        if (mode == MIGRATE_SYNC_NO_COPY)
                return -EINVAL;
 
-       VM_BUG_ON_PAGE(!PageMovable(page), page);
        VM_BUG_ON_PAGE(!PageIsolated(page), page);
 
        /* The page is locked, so this pointer must remain valid */
@@ -1860,16 +1767,12 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
        pool = zspage->pool;
 
        /*
-        * The pool migrate_lock protects the race between zpage migration
+        * The pool's lock protects the race between zpage migration
         * and zs_free.
         */
-       write_lock(&pool->migrate_lock);
+       spin_lock(&pool->lock);
        class = zspage_class(pool, zspage);
 
-       /*
-        * the class lock protects zpage alloc/free in the zspage.
-        */
-       spin_lock(&class->lock);
        /* the migrate_write_lock protects zpage access via zs_map_object */
        migrate_write_lock(zspage);
 
@@ -1880,7 +1783,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
         * Here, any user cannot access all objects in the zspage so let's move.
         */
        d_addr = kmap_atomic(newpage);
-       memcpy(d_addr, s_addr, PAGE_SIZE);
+       copy_page(d_addr, s_addr);
        kunmap_atomic(d_addr);
 
        for (addr = s_addr + offset; addr < s_addr + PAGE_SIZE;
@@ -1899,11 +1802,9 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
        replace_sub_page(class, zspage, newpage, page);
        /*
         * Since we complete the data copy and set up new zspage structure,
-        * it's okay to release migration_lock.
+        * it's okay to release the pool's lock.
         */
-       write_unlock(&pool->migrate_lock);
-       spin_unlock(&class->lock);
-       dec_zspage_isolation(zspage);
+       spin_unlock(&pool->lock);
        migrate_write_unlock(zspage);
 
        get_page(newpage);
@@ -1920,15 +1821,7 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
 
 static void zs_page_putback(struct page *page)
 {
-       struct zspage *zspage;
-
-       VM_BUG_ON_PAGE(!PageMovable(page), page);
        VM_BUG_ON_PAGE(!PageIsolated(page), page);
-
-       zspage = get_zspage(page);
-       migrate_write_lock(zspage);
-       dec_zspage_isolation(zspage);
-       migrate_write_unlock(zspage);
 }
 
 static const struct movable_operations zsmalloc_mops = {
@@ -1945,8 +1838,6 @@ static void async_free_zspage(struct work_struct *work)
 {
        int i;
        struct size_class *class;
-       unsigned int class_idx;
-       enum fullness_group fullness;
        struct zspage *zspage, *tmp;
        LIST_HEAD(free_pages);
        struct zs_pool *pool = container_of(work, struct zs_pool,
@@ -1957,21 +1848,20 @@ static void async_free_zspage(struct work_struct *work)
                if (class->index != i)
                        continue;
 
-               spin_lock(&class->lock);
-               list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
-               spin_unlock(&class->lock);
+               spin_lock(&pool->lock);
+               list_splice_init(&class->fullness_list[ZS_INUSE_RATIO_0],
+                                &free_pages);
+               spin_unlock(&pool->lock);
        }
 
        list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
                list_del(&zspage->list);
                lock_zspage(zspage);
 
-               get_zspage_mapping(zspage, &class_idx, &fullness);
-               VM_BUG_ON(fullness != ZS_EMPTY);
-               class = pool->size_class[class_idx];
-               spin_lock(&class->lock);
+               spin_lock(&pool->lock);
+               class = zspage_class(pool, zspage);
                __free_zspage(pool, class, zspage);
-               spin_unlock(&class->lock);
+               spin_unlock(&pool->lock);
        }
 };
 
@@ -2012,8 +1902,8 @@ static inline void zs_flush_migration(struct zs_pool *pool) { }
 static unsigned long zs_can_compact(struct size_class *class)
 {
        unsigned long obj_wasted;
-       unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
-       unsigned long obj_used = zs_stat_get(class, OBJ_USED);
+       unsigned long obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED);
+       unsigned long obj_used = zs_stat_get(class, ZS_OBJS_INUSE);
 
        if (obj_allocated <= obj_used)
                return 0;
@@ -2027,70 +1917,57 @@ static unsigned long zs_can_compact(struct size_class *class)
 static unsigned long __zs_compact(struct zs_pool *pool,
                                  struct size_class *class)
 {
-       struct zs_compact_control cc;
-       struct zspage *src_zspage;
+       struct zspage *src_zspage = NULL;
        struct zspage *dst_zspage = NULL;
        unsigned long pages_freed = 0;
 
-       /* protect the race between zpage migration and zs_free */
-       write_lock(&pool->migrate_lock);
-       /* protect zpage allocation/free */
-       spin_lock(&class->lock);
-       while ((src_zspage = isolate_zspage(class, true))) {
-               /* protect someone accessing the zspage(i.e., zs_map_object) */
-               migrate_write_lock(src_zspage);
-
-               if (!zs_can_compact(class))
-                       break;
-
-               cc.obj_idx = 0;
-               cc.s_page = get_first_page(src_zspage);
-
-               while ((dst_zspage = isolate_zspage(class, false))) {
-                       migrate_write_lock_nested(dst_zspage);
-
-                       cc.d_page = get_first_page(dst_zspage);
-                       /*
-                        * If there is no more space in dst_page, resched
-                        * and see if anyone had allocated another zspage.
-                        */
-                       if (!migrate_zspage(pool, class, &cc))
-                               break;
+       /*
+        * protect the race between zpage migration and zs_free
+        * as well as zpage allocation/free
+        */
+       spin_lock(&pool->lock);
+       while (zs_can_compact(class)) {
+               int fg;
 
-                       putback_zspage(class, dst_zspage);
-                       migrate_write_unlock(dst_zspage);
-                       dst_zspage = NULL;
-                       if (rwlock_is_contended(&pool->migrate_lock))
+               if (!dst_zspage) {
+                       dst_zspage = isolate_dst_zspage(class);
+                       if (!dst_zspage)
                                break;
                }
 
-               /* Stop if we couldn't find slot */
-               if (dst_zspage == NULL)
+               src_zspage = isolate_src_zspage(class);
+               if (!src_zspage)
                        break;
 
-               putback_zspage(class, dst_zspage);
-               migrate_write_unlock(dst_zspage);
+               migrate_write_lock(src_zspage);
+               migrate_zspage(pool, src_zspage, dst_zspage);
+               migrate_write_unlock(src_zspage);
 
-               if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
-                       migrate_write_unlock(src_zspage);
+               fg = putback_zspage(class, src_zspage);
+               if (fg == ZS_INUSE_RATIO_0) {
                        free_zspage(pool, class, src_zspage);
                        pages_freed += class->pages_per_zspage;
-               } else
-                       migrate_write_unlock(src_zspage);
-               spin_unlock(&class->lock);
-               write_unlock(&pool->migrate_lock);
-               cond_resched();
-               write_lock(&pool->migrate_lock);
-               spin_lock(&class->lock);
+               }
+               src_zspage = NULL;
+
+               if (get_fullness_group(class, dst_zspage) == ZS_INUSE_RATIO_100
+                   || spin_is_contended(&pool->lock)) {
+                       putback_zspage(class, dst_zspage);
+                       dst_zspage = NULL;
+
+                       spin_unlock(&pool->lock);
+                       cond_resched();
+                       spin_lock(&pool->lock);
+               }
        }
 
-       if (src_zspage) {
+       if (src_zspage)
                putback_zspage(class, src_zspage);
-               migrate_write_unlock(src_zspage);
-       }
 
-       spin_unlock(&class->lock);
-       write_unlock(&pool->migrate_lock);
+       if (dst_zspage)
+               putback_zspage(class, dst_zspage);
+
+       spin_unlock(&pool->lock);
 
        return pages_freed;
 }
@@ -2101,15 +1978,23 @@ unsigned long zs_compact(struct zs_pool *pool)
        struct size_class *class;
        unsigned long pages_freed = 0;
 
+       /*
+        * Pool compaction is performed under pool->lock so it is basically
+        * single-threaded. Having more than one thread in __zs_compact()
+        * will increase pool->lock contention, which will impact other
+        * zsmalloc operations that need pool->lock.
+        */
+       if (atomic_xchg(&pool->compaction_in_progress, 1))
+               return 0;
+
        for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
                class = pool->size_class[i];
-               if (!class)
-                       continue;
                if (class->index != i)
                        continue;
                pages_freed += __zs_compact(pool, class);
        }
        atomic_long_add(pages_freed, &pool->stats.pages_compacted);
+       atomic_set(&pool->compaction_in_progress, 0);
 
        return pages_freed;
 }
@@ -2125,8 +2010,7 @@ static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
                struct shrink_control *sc)
 {
        unsigned long pages_freed;
-       struct zs_pool *pool = container_of(shrinker, struct zs_pool,
-                       shrinker);
+       struct zs_pool *pool = shrinker->private_data;
 
        /*
         * Compact classes and calculate compaction delta.
@@ -2144,13 +2028,10 @@ static unsigned long zs_shrinker_count(struct shrinker *shrinker,
        int i;
        struct size_class *class;
        unsigned long pages_to_free = 0;
-       struct zs_pool *pool = container_of(shrinker, struct zs_pool,
-                       shrinker);
+       struct zs_pool *pool = shrinker->private_data;
 
        for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
                class = pool->size_class[i];
-               if (!class)
-                       continue;
                if (class->index != i)
                        continue;
 
@@ -2162,18 +2043,44 @@ static unsigned long zs_shrinker_count(struct shrinker *shrinker,
 
 static void zs_unregister_shrinker(struct zs_pool *pool)
 {
-       unregister_shrinker(&pool->shrinker);
+       shrinker_free(pool->shrinker);
 }
 
 static int zs_register_shrinker(struct zs_pool *pool)
 {
-       pool->shrinker.scan_objects = zs_shrinker_scan;
-       pool->shrinker.count_objects = zs_shrinker_count;
-       pool->shrinker.batch = 0;
-       pool->shrinker.seeks = DEFAULT_SEEKS;
+       pool->shrinker = shrinker_alloc(0, "mm-zspool:%s", pool->name);
+       if (!pool->shrinker)
+               return -ENOMEM;
+
+       pool->shrinker->scan_objects = zs_shrinker_scan;
+       pool->shrinker->count_objects = zs_shrinker_count;
+       pool->shrinker->batch = 0;
+       pool->shrinker->private_data = pool;
 
-       return register_shrinker(&pool->shrinker, "mm-zspool:%s",
-                                pool->name);
+       shrinker_register(pool->shrinker);
+
+       return 0;
+}
+
+static int calculate_zspage_chain_size(int class_size)
+{
+       int i, min_waste = INT_MAX;
+       int chain_size = 1;
+
+       if (is_power_of_2(class_size))
+               return chain_size;
+
+       for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
+               int waste;
+
+               waste = (i * PAGE_SIZE) % class_size;
+               if (waste < min_waste) {
+                       min_waste = waste;
+                       chain_size = i;
+               }
+       }
+
+       return chain_size;
 }
 
 /**
@@ -2197,7 +2104,8 @@ struct zs_pool *zs_create_pool(const char *name)
                return NULL;
 
        init_deferred_free(pool);
-       rwlock_init(&pool->migrate_lock);
+       spin_lock_init(&pool->lock);
+       atomic_set(&pool->compaction_in_progress, 0);
 
        pool->name = kstrdup(name, GFP_KERNEL);
        if (!pool->name)
@@ -2215,12 +2123,12 @@ struct zs_pool *zs_create_pool(const char *name)
                int pages_per_zspage;
                int objs_per_zspage;
                struct size_class *class;
-               int fullness = 0;
+               int fullness;
 
                size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
                if (size > ZS_MAX_ALLOC_SIZE)
                        size = ZS_MAX_ALLOC_SIZE;
-               pages_per_zspage = get_pages_per_zspage(size);
+               pages_per_zspage = calculate_zspage_chain_size(size);
                objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
 
                /*
@@ -2268,11 +2176,13 @@ struct zs_pool *zs_create_pool(const char *name)
                class->index = i;
                class->pages_per_zspage = pages_per_zspage;
                class->objs_per_zspage = objs_per_zspage;
-               spin_lock_init(&class->lock);
                pool->size_class[i] = class;
-               for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
-                                                       fullness++)
+
+               fullness = ZS_INUSE_RATIO_0;
+               while (fullness < NR_FULLNESS_GROUPS) {
                        INIT_LIST_HEAD(&class->fullness_list[fullness]);
+                       fullness++;
+               }
 
                prev_class = class;
        }
@@ -2314,11 +2224,12 @@ void zs_destroy_pool(struct zs_pool *pool)
                if (class->index != i)
                        continue;
 
-               for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
-                       if (!list_empty(&class->fullness_list[fg])) {
-                               pr_info("Freeing non-empty class with size %db, fullness group %d\n",
-                                       class->size, fg);
-                       }
+               for (fg = ZS_INUSE_RATIO_0; fg < NR_FULLNESS_GROUPS; fg++) {
+                       if (list_empty(&class->fullness_list[fg]))
+                               continue;
+
+                       pr_err("Class-%d fullness group %d is not empty\n",
+                              class->size, fg);
                }
                kfree(class);
        }