Merge branch 'mm-hotfixes-stable' into mm-stable
[platform/kernel/linux-starfive.git] / mm / zsmalloc.c
1 /*
2  * zsmalloc memory allocator
3  *
4  * Copyright (C) 2011  Nitin Gupta
5  * Copyright (C) 2012, 2013 Minchan Kim
6  *
7  * This code is released using a dual license strategy: BSD/GPL
8  * You can choose the license that better fits your requirements.
9  *
10  * Released under the terms of 3-clause BSD License
11  * Released under the terms of GNU General Public License Version 2.0
12  */
13
14 /*
15  * Following is how we use various fields and flags of underlying
16  * struct page(s) to form a zspage.
17  *
18  * Usage of struct page fields:
19  *      page->private: points to zspage
20  *      page->index: links together all component pages of a zspage
21  *              For the huge page, this is always 0, so we use this field
22  *              to store handle.
23  *      page->page_type: first object offset in a subpage of zspage
24  *
25  * Usage of struct page flags:
26  *      PG_private: identifies the first component page
27  *      PG_owner_priv_1: identifies the huge component page
28  *
29  */
30
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 /*
34  * lock ordering:
35  *      page_lock
36  *      pool->migrate_lock
37  *      class->lock
38  *      zspage->lock
39  */
40
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/bitops.h>
45 #include <linux/errno.h>
46 #include <linux/highmem.h>
47 #include <linux/string.h>
48 #include <linux/slab.h>
49 #include <linux/pgtable.h>
50 #include <asm/tlbflush.h>
51 #include <linux/cpumask.h>
52 #include <linux/cpu.h>
53 #include <linux/vmalloc.h>
54 #include <linux/preempt.h>
55 #include <linux/spinlock.h>
56 #include <linux/shrinker.h>
57 #include <linux/types.h>
58 #include <linux/debugfs.h>
59 #include <linux/zsmalloc.h>
60 #include <linux/zpool.h>
61 #include <linux/migrate.h>
62 #include <linux/wait.h>
63 #include <linux/pagemap.h>
64 #include <linux/fs.h>
65 #include <linux/local_lock.h>
66
67 #define ZSPAGE_MAGIC    0x58
68
69 /*
70  * This must be power of 2 and greater than or equal to sizeof(link_free).
71  * These two conditions ensure that any 'struct link_free' itself doesn't
72  * span more than 1 page which avoids complex case of mapping 2 pages simply
73  * to restore link_free pointer values.
74  */
75 #define ZS_ALIGN                8
76
77 /*
78  * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
79  * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
80  */
81 #define ZS_MAX_ZSPAGE_ORDER 2
82 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
83
84 #define ZS_HANDLE_SIZE (sizeof(unsigned long))
85
86 /*
87  * Object location (<PFN>, <obj_idx>) is encoded as
88  * a single (unsigned long) handle value.
89  *
90  * Note that object index <obj_idx> starts from 0.
91  *
92  * This is made more complicated by various memory models and PAE.
93  */
94
95 #ifndef MAX_POSSIBLE_PHYSMEM_BITS
96 #ifdef MAX_PHYSMEM_BITS
97 #define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS
98 #else
99 /*
100  * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
101  * be PAGE_SHIFT
102  */
103 #define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
104 #endif
105 #endif
106
107 #define _PFN_BITS               (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
108
109 /*
110  * Head in allocated object should have OBJ_ALLOCATED_TAG
111  * to identify the object was allocated or not.
112  * It's okay to add the status bit in the least bit because
113  * header keeps handle which is 4byte-aligned address so we
114  * have room for two bit at least.
115  */
116 #define OBJ_ALLOCATED_TAG 1
117 #define OBJ_TAG_BITS 1
118 #define OBJ_INDEX_BITS  (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
119 #define OBJ_INDEX_MASK  ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
120
121 #define HUGE_BITS       1
122 #define FULLNESS_BITS   2
123 #define CLASS_BITS      8
124 #define ISOLATED_BITS   3
125 #define MAGIC_VAL_BITS  8
126
127 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
128 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
129 #define ZS_MIN_ALLOC_SIZE \
130         MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
131 /* each chunk includes extra space to keep handle */
132 #define ZS_MAX_ALLOC_SIZE       PAGE_SIZE
133
134 /*
135  * On systems with 4K page size, this gives 255 size classes! There is a
136  * trader-off here:
137  *  - Large number of size classes is potentially wasteful as free page are
138  *    spread across these classes
139  *  - Small number of size classes causes large internal fragmentation
140  *  - Probably its better to use specific size classes (empirically
141  *    determined). NOTE: all those class sizes must be set as multiple of
142  *    ZS_ALIGN to make sure link_free itself never has to span 2 pages.
143  *
144  *  ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
145  *  (reason above)
146  */
147 #define ZS_SIZE_CLASS_DELTA     (PAGE_SIZE >> CLASS_BITS)
148 #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
149                                       ZS_SIZE_CLASS_DELTA) + 1)
150
151 enum fullness_group {
152         ZS_EMPTY,
153         ZS_ALMOST_EMPTY,
154         ZS_ALMOST_FULL,
155         ZS_FULL,
156         NR_ZS_FULLNESS,
157 };
158
159 enum class_stat_type {
160         CLASS_EMPTY,
161         CLASS_ALMOST_EMPTY,
162         CLASS_ALMOST_FULL,
163         CLASS_FULL,
164         OBJ_ALLOCATED,
165         OBJ_USED,
166         NR_ZS_STAT_TYPE,
167 };
168
169 struct zs_size_stat {
170         unsigned long objs[NR_ZS_STAT_TYPE];
171 };
172
173 #ifdef CONFIG_ZSMALLOC_STAT
174 static struct dentry *zs_stat_root;
175 #endif
176
177 /*
178  * We assign a page to ZS_ALMOST_EMPTY fullness group when:
179  *      n <= N / f, where
180  * n = number of allocated objects
181  * N = total number of objects zspage can store
182  * f = fullness_threshold_frac
183  *
184  * Similarly, we assign zspage to:
185  *      ZS_ALMOST_FULL  when n > N / f
186  *      ZS_EMPTY        when n == 0
187  *      ZS_FULL         when n == N
188  *
189  * (see: fix_fullness_group())
190  */
191 static const int fullness_threshold_frac = 4;
192 static size_t huge_class_size;
193
194 struct size_class {
195         spinlock_t lock;
196         struct list_head fullness_list[NR_ZS_FULLNESS];
197         /*
198          * Size of objects stored in this class. Must be multiple
199          * of ZS_ALIGN.
200          */
201         int size;
202         int objs_per_zspage;
203         /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
204         int pages_per_zspage;
205
206         unsigned int index;
207         struct zs_size_stat stats;
208 };
209
210 /*
211  * Placed within free objects to form a singly linked list.
212  * For every zspage, zspage->freeobj gives head of this list.
213  *
214  * This must be power of 2 and less than or equal to ZS_ALIGN
215  */
216 struct link_free {
217         union {
218                 /*
219                  * Free object index;
220                  * It's valid for non-allocated object
221                  */
222                 unsigned long next;
223                 /*
224                  * Handle of allocated object.
225                  */
226                 unsigned long handle;
227         };
228 };
229
230 struct zs_pool {
231         const char *name;
232
233         struct size_class *size_class[ZS_SIZE_CLASSES];
234         struct kmem_cache *handle_cachep;
235         struct kmem_cache *zspage_cachep;
236
237         atomic_long_t pages_allocated;
238
239         struct zs_pool_stats stats;
240
241         /* Compact classes */
242         struct shrinker shrinker;
243
244 #ifdef CONFIG_ZSMALLOC_STAT
245         struct dentry *stat_dentry;
246 #endif
247 #ifdef CONFIG_COMPACTION
248         struct work_struct free_work;
249 #endif
250         /* protect page/zspage migration */
251         rwlock_t migrate_lock;
252 };
253
254 struct zspage {
255         struct {
256                 unsigned int huge:HUGE_BITS;
257                 unsigned int fullness:FULLNESS_BITS;
258                 unsigned int class:CLASS_BITS + 1;
259                 unsigned int isolated:ISOLATED_BITS;
260                 unsigned int magic:MAGIC_VAL_BITS;
261         };
262         unsigned int inuse;
263         unsigned int freeobj;
264         struct page *first_page;
265         struct list_head list; /* fullness list */
266         struct zs_pool *pool;
267 #ifdef CONFIG_COMPACTION
268         rwlock_t lock;
269 #endif
270 };
271
272 struct mapping_area {
273         local_lock_t lock;
274         char *vm_buf; /* copy buffer for objects that span pages */
275         char *vm_addr; /* address of kmap_atomic()'ed pages */
276         enum zs_mapmode vm_mm; /* mapping mode */
277 };
278
279 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
280 static void SetZsHugePage(struct zspage *zspage)
281 {
282         zspage->huge = 1;
283 }
284
285 static bool ZsHugePage(struct zspage *zspage)
286 {
287         return zspage->huge;
288 }
289
290 #ifdef CONFIG_COMPACTION
291 static void migrate_lock_init(struct zspage *zspage);
292 static void migrate_read_lock(struct zspage *zspage);
293 static void migrate_read_unlock(struct zspage *zspage);
294 static void migrate_write_lock(struct zspage *zspage);
295 static void migrate_write_lock_nested(struct zspage *zspage);
296 static void migrate_write_unlock(struct zspage *zspage);
297 static void kick_deferred_free(struct zs_pool *pool);
298 static void init_deferred_free(struct zs_pool *pool);
299 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
300 #else
301 static void migrate_lock_init(struct zspage *zspage) {}
302 static void migrate_read_lock(struct zspage *zspage) {}
303 static void migrate_read_unlock(struct zspage *zspage) {}
304 static void migrate_write_lock(struct zspage *zspage) {}
305 static void migrate_write_lock_nested(struct zspage *zspage) {}
306 static void migrate_write_unlock(struct zspage *zspage) {}
307 static void kick_deferred_free(struct zs_pool *pool) {}
308 static void init_deferred_free(struct zs_pool *pool) {}
309 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
310 #endif
311
312 static int create_cache(struct zs_pool *pool)
313 {
314         pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
315                                         0, 0, NULL);
316         if (!pool->handle_cachep)
317                 return 1;
318
319         pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
320                                         0, 0, NULL);
321         if (!pool->zspage_cachep) {
322                 kmem_cache_destroy(pool->handle_cachep);
323                 pool->handle_cachep = NULL;
324                 return 1;
325         }
326
327         return 0;
328 }
329
330 static void destroy_cache(struct zs_pool *pool)
331 {
332         kmem_cache_destroy(pool->handle_cachep);
333         kmem_cache_destroy(pool->zspage_cachep);
334 }
335
336 static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
337 {
338         return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
339                         gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
340 }
341
342 static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
343 {
344         kmem_cache_free(pool->handle_cachep, (void *)handle);
345 }
346
347 static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
348 {
349         return kmem_cache_zalloc(pool->zspage_cachep,
350                         flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
351 }
352
353 static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
354 {
355         kmem_cache_free(pool->zspage_cachep, zspage);
356 }
357
358 /* class->lock(which owns the handle) synchronizes races */
359 static void record_obj(unsigned long handle, unsigned long obj)
360 {
361         *(unsigned long *)handle = obj;
362 }
363
364 /* zpool driver */
365
366 #ifdef CONFIG_ZPOOL
367
368 static void *zs_zpool_create(const char *name, gfp_t gfp,
369                              const struct zpool_ops *zpool_ops,
370                              struct zpool *zpool)
371 {
372         /*
373          * Ignore global gfp flags: zs_malloc() may be invoked from
374          * different contexts and its caller must provide a valid
375          * gfp mask.
376          */
377         return zs_create_pool(name);
378 }
379
380 static void zs_zpool_destroy(void *pool)
381 {
382         zs_destroy_pool(pool);
383 }
384
385 static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
386                         unsigned long *handle)
387 {
388         *handle = zs_malloc(pool, size, gfp);
389
390         if (IS_ERR_VALUE(*handle))
391                 return PTR_ERR((void *)*handle);
392         return 0;
393 }
394 static void zs_zpool_free(void *pool, unsigned long handle)
395 {
396         zs_free(pool, handle);
397 }
398
399 static void *zs_zpool_map(void *pool, unsigned long handle,
400                         enum zpool_mapmode mm)
401 {
402         enum zs_mapmode zs_mm;
403
404         switch (mm) {
405         case ZPOOL_MM_RO:
406                 zs_mm = ZS_MM_RO;
407                 break;
408         case ZPOOL_MM_WO:
409                 zs_mm = ZS_MM_WO;
410                 break;
411         case ZPOOL_MM_RW:
412         default:
413                 zs_mm = ZS_MM_RW;
414                 break;
415         }
416
417         return zs_map_object(pool, handle, zs_mm);
418 }
419 static void zs_zpool_unmap(void *pool, unsigned long handle)
420 {
421         zs_unmap_object(pool, handle);
422 }
423
424 static u64 zs_zpool_total_size(void *pool)
425 {
426         return zs_get_total_pages(pool) << PAGE_SHIFT;
427 }
428
429 static struct zpool_driver zs_zpool_driver = {
430         .type =                   "zsmalloc",
431         .owner =                  THIS_MODULE,
432         .create =                 zs_zpool_create,
433         .destroy =                zs_zpool_destroy,
434         .malloc_support_movable = true,
435         .malloc =                 zs_zpool_malloc,
436         .free =                   zs_zpool_free,
437         .map =                    zs_zpool_map,
438         .unmap =                  zs_zpool_unmap,
439         .total_size =             zs_zpool_total_size,
440 };
441
442 MODULE_ALIAS("zpool-zsmalloc");
443 #endif /* CONFIG_ZPOOL */
444
445 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
446 static DEFINE_PER_CPU(struct mapping_area, zs_map_area) = {
447         .lock   = INIT_LOCAL_LOCK(lock),
448 };
449
450 static __maybe_unused int is_first_page(struct page *page)
451 {
452         return PagePrivate(page);
453 }
454
455 /* Protected by class->lock */
456 static inline int get_zspage_inuse(struct zspage *zspage)
457 {
458         return zspage->inuse;
459 }
460
461
462 static inline void mod_zspage_inuse(struct zspage *zspage, int val)
463 {
464         zspage->inuse += val;
465 }
466
467 static inline struct page *get_first_page(struct zspage *zspage)
468 {
469         struct page *first_page = zspage->first_page;
470
471         VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
472         return first_page;
473 }
474
475 static inline unsigned int get_first_obj_offset(struct page *page)
476 {
477         return page->page_type;
478 }
479
480 static inline void set_first_obj_offset(struct page *page, unsigned int offset)
481 {
482         page->page_type = offset;
483 }
484
485 static inline unsigned int get_freeobj(struct zspage *zspage)
486 {
487         return zspage->freeobj;
488 }
489
490 static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
491 {
492         zspage->freeobj = obj;
493 }
494
495 static void get_zspage_mapping(struct zspage *zspage,
496                                 unsigned int *class_idx,
497                                 enum fullness_group *fullness)
498 {
499         BUG_ON(zspage->magic != ZSPAGE_MAGIC);
500
501         *fullness = zspage->fullness;
502         *class_idx = zspage->class;
503 }
504
505 static struct size_class *zspage_class(struct zs_pool *pool,
506                                              struct zspage *zspage)
507 {
508         return pool->size_class[zspage->class];
509 }
510
511 static void set_zspage_mapping(struct zspage *zspage,
512                                 unsigned int class_idx,
513                                 enum fullness_group fullness)
514 {
515         zspage->class = class_idx;
516         zspage->fullness = fullness;
517 }
518
519 /*
520  * zsmalloc divides the pool into various size classes where each
521  * class maintains a list of zspages where each zspage is divided
522  * into equal sized chunks. Each allocation falls into one of these
523  * classes depending on its size. This function returns index of the
524  * size class which has chunk size big enough to hold the given size.
525  */
526 static int get_size_class_index(int size)
527 {
528         int idx = 0;
529
530         if (likely(size > ZS_MIN_ALLOC_SIZE))
531                 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
532                                 ZS_SIZE_CLASS_DELTA);
533
534         return min_t(int, ZS_SIZE_CLASSES - 1, idx);
535 }
536
537 /* type can be of enum type class_stat_type or fullness_group */
538 static inline void class_stat_inc(struct size_class *class,
539                                 int type, unsigned long cnt)
540 {
541         class->stats.objs[type] += cnt;
542 }
543
544 /* type can be of enum type class_stat_type or fullness_group */
545 static inline void class_stat_dec(struct size_class *class,
546                                 int type, unsigned long cnt)
547 {
548         class->stats.objs[type] -= cnt;
549 }
550
551 /* type can be of enum type class_stat_type or fullness_group */
552 static inline unsigned long zs_stat_get(struct size_class *class,
553                                 int type)
554 {
555         return class->stats.objs[type];
556 }
557
558 #ifdef CONFIG_ZSMALLOC_STAT
559
560 static void __init zs_stat_init(void)
561 {
562         if (!debugfs_initialized()) {
563                 pr_warn("debugfs not available, stat dir not created\n");
564                 return;
565         }
566
567         zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
568 }
569
570 static void __exit zs_stat_exit(void)
571 {
572         debugfs_remove_recursive(zs_stat_root);
573 }
574
575 static unsigned long zs_can_compact(struct size_class *class);
576
577 static int zs_stats_size_show(struct seq_file *s, void *v)
578 {
579         int i;
580         struct zs_pool *pool = s->private;
581         struct size_class *class;
582         int objs_per_zspage;
583         unsigned long class_almost_full, class_almost_empty;
584         unsigned long obj_allocated, obj_used, pages_used, freeable;
585         unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
586         unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
587         unsigned long total_freeable = 0;
588
589         seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
590                         "class", "size", "almost_full", "almost_empty",
591                         "obj_allocated", "obj_used", "pages_used",
592                         "pages_per_zspage", "freeable");
593
594         for (i = 0; i < ZS_SIZE_CLASSES; i++) {
595                 class = pool->size_class[i];
596
597                 if (class->index != i)
598                         continue;
599
600                 spin_lock(&class->lock);
601                 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
602                 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
603                 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
604                 obj_used = zs_stat_get(class, OBJ_USED);
605                 freeable = zs_can_compact(class);
606                 spin_unlock(&class->lock);
607
608                 objs_per_zspage = class->objs_per_zspage;
609                 pages_used = obj_allocated / objs_per_zspage *
610                                 class->pages_per_zspage;
611
612                 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
613                                 " %10lu %10lu %16d %8lu\n",
614                         i, class->size, class_almost_full, class_almost_empty,
615                         obj_allocated, obj_used, pages_used,
616                         class->pages_per_zspage, freeable);
617
618                 total_class_almost_full += class_almost_full;
619                 total_class_almost_empty += class_almost_empty;
620                 total_objs += obj_allocated;
621                 total_used_objs += obj_used;
622                 total_pages += pages_used;
623                 total_freeable += freeable;
624         }
625
626         seq_puts(s, "\n");
627         seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
628                         "Total", "", total_class_almost_full,
629                         total_class_almost_empty, total_objs,
630                         total_used_objs, total_pages, "", total_freeable);
631
632         return 0;
633 }
634 DEFINE_SHOW_ATTRIBUTE(zs_stats_size);
635
636 static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
637 {
638         if (!zs_stat_root) {
639                 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
640                 return;
641         }
642
643         pool->stat_dentry = debugfs_create_dir(name, zs_stat_root);
644
645         debugfs_create_file("classes", S_IFREG | 0444, pool->stat_dentry, pool,
646                             &zs_stats_size_fops);
647 }
648
649 static void zs_pool_stat_destroy(struct zs_pool *pool)
650 {
651         debugfs_remove_recursive(pool->stat_dentry);
652 }
653
654 #else /* CONFIG_ZSMALLOC_STAT */
655 static void __init zs_stat_init(void)
656 {
657 }
658
659 static void __exit zs_stat_exit(void)
660 {
661 }
662
663 static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
664 {
665 }
666
667 static inline void zs_pool_stat_destroy(struct zs_pool *pool)
668 {
669 }
670 #endif
671
672
673 /*
674  * For each size class, zspages are divided into different groups
675  * depending on how "full" they are. This was done so that we could
676  * easily find empty or nearly empty zspages when we try to shrink
677  * the pool (not yet implemented). This function returns fullness
678  * status of the given page.
679  */
680 static enum fullness_group get_fullness_group(struct size_class *class,
681                                                 struct zspage *zspage)
682 {
683         int inuse, objs_per_zspage;
684         enum fullness_group fg;
685
686         inuse = get_zspage_inuse(zspage);
687         objs_per_zspage = class->objs_per_zspage;
688
689         if (inuse == 0)
690                 fg = ZS_EMPTY;
691         else if (inuse == objs_per_zspage)
692                 fg = ZS_FULL;
693         else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
694                 fg = ZS_ALMOST_EMPTY;
695         else
696                 fg = ZS_ALMOST_FULL;
697
698         return fg;
699 }
700
701 /*
702  * Each size class maintains various freelists and zspages are assigned
703  * to one of these freelists based on the number of live objects they
704  * have. This functions inserts the given zspage into the freelist
705  * identified by <class, fullness_group>.
706  */
707 static void insert_zspage(struct size_class *class,
708                                 struct zspage *zspage,
709                                 enum fullness_group fullness)
710 {
711         struct zspage *head;
712
713         class_stat_inc(class, fullness, 1);
714         head = list_first_entry_or_null(&class->fullness_list[fullness],
715                                         struct zspage, list);
716         /*
717          * We want to see more ZS_FULL pages and less almost empty/full.
718          * Put pages with higher ->inuse first.
719          */
720         if (head && get_zspage_inuse(zspage) < get_zspage_inuse(head))
721                 list_add(&zspage->list, &head->list);
722         else
723                 list_add(&zspage->list, &class->fullness_list[fullness]);
724 }
725
726 /*
727  * This function removes the given zspage from the freelist identified
728  * by <class, fullness_group>.
729  */
730 static void remove_zspage(struct size_class *class,
731                                 struct zspage *zspage,
732                                 enum fullness_group fullness)
733 {
734         VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
735
736         list_del_init(&zspage->list);
737         class_stat_dec(class, fullness, 1);
738 }
739
740 /*
741  * Each size class maintains zspages in different fullness groups depending
742  * on the number of live objects they contain. When allocating or freeing
743  * objects, the fullness status of the page can change, say, from ALMOST_FULL
744  * to ALMOST_EMPTY when freeing an object. This function checks if such
745  * a status change has occurred for the given page and accordingly moves the
746  * page from the freelist of the old fullness group to that of the new
747  * fullness group.
748  */
749 static enum fullness_group fix_fullness_group(struct size_class *class,
750                                                 struct zspage *zspage)
751 {
752         int class_idx;
753         enum fullness_group currfg, newfg;
754
755         get_zspage_mapping(zspage, &class_idx, &currfg);
756         newfg = get_fullness_group(class, zspage);
757         if (newfg == currfg)
758                 goto out;
759
760         remove_zspage(class, zspage, currfg);
761         insert_zspage(class, zspage, newfg);
762         set_zspage_mapping(zspage, class_idx, newfg);
763 out:
764         return newfg;
765 }
766
767 /*
768  * We have to decide on how many pages to link together
769  * to form a zspage for each size class. This is important
770  * to reduce wastage due to unusable space left at end of
771  * each zspage which is given as:
772  *     wastage = Zp % class_size
773  *     usage = Zp - wastage
774  * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
775  *
776  * For example, for size class of 3/8 * PAGE_SIZE, we should
777  * link together 3 PAGE_SIZE sized pages to form a zspage
778  * since then we can perfectly fit in 8 such objects.
779  */
780 static int get_pages_per_zspage(int class_size)
781 {
782         int i, max_usedpc = 0;
783         /* zspage order which gives maximum used size per KB */
784         int max_usedpc_order = 1;
785
786         for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
787                 int zspage_size;
788                 int waste, usedpc;
789
790                 zspage_size = i * PAGE_SIZE;
791                 waste = zspage_size % class_size;
792                 usedpc = (zspage_size - waste) * 100 / zspage_size;
793
794                 if (usedpc > max_usedpc) {
795                         max_usedpc = usedpc;
796                         max_usedpc_order = i;
797                 }
798         }
799
800         return max_usedpc_order;
801 }
802
803 static struct zspage *get_zspage(struct page *page)
804 {
805         struct zspage *zspage = (struct zspage *)page_private(page);
806
807         BUG_ON(zspage->magic != ZSPAGE_MAGIC);
808         return zspage;
809 }
810
811 static struct page *get_next_page(struct page *page)
812 {
813         struct zspage *zspage = get_zspage(page);
814
815         if (unlikely(ZsHugePage(zspage)))
816                 return NULL;
817
818         return (struct page *)page->index;
819 }
820
821 /**
822  * obj_to_location - get (<page>, <obj_idx>) from encoded object value
823  * @obj: the encoded object value
824  * @page: page object resides in zspage
825  * @obj_idx: object index
826  */
827 static void obj_to_location(unsigned long obj, struct page **page,
828                                 unsigned int *obj_idx)
829 {
830         obj >>= OBJ_TAG_BITS;
831         *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
832         *obj_idx = (obj & OBJ_INDEX_MASK);
833 }
834
835 static void obj_to_page(unsigned long obj, struct page **page)
836 {
837         obj >>= OBJ_TAG_BITS;
838         *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
839 }
840
841 /**
842  * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
843  * @page: page object resides in zspage
844  * @obj_idx: object index
845  */
846 static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
847 {
848         unsigned long obj;
849
850         obj = page_to_pfn(page) << OBJ_INDEX_BITS;
851         obj |= obj_idx & OBJ_INDEX_MASK;
852         obj <<= OBJ_TAG_BITS;
853
854         return obj;
855 }
856
857 static unsigned long handle_to_obj(unsigned long handle)
858 {
859         return *(unsigned long *)handle;
860 }
861
862 static bool obj_allocated(struct page *page, void *obj, unsigned long *phandle)
863 {
864         unsigned long handle;
865         struct zspage *zspage = get_zspage(page);
866
867         if (unlikely(ZsHugePage(zspage))) {
868                 VM_BUG_ON_PAGE(!is_first_page(page), page);
869                 handle = page->index;
870         } else
871                 handle = *(unsigned long *)obj;
872
873         if (!(handle & OBJ_ALLOCATED_TAG))
874                 return false;
875
876         *phandle = handle & ~OBJ_ALLOCATED_TAG;
877         return true;
878 }
879
880 static void reset_page(struct page *page)
881 {
882         __ClearPageMovable(page);
883         ClearPagePrivate(page);
884         set_page_private(page, 0);
885         page_mapcount_reset(page);
886         page->index = 0;
887 }
888
889 static int trylock_zspage(struct zspage *zspage)
890 {
891         struct page *cursor, *fail;
892
893         for (cursor = get_first_page(zspage); cursor != NULL; cursor =
894                                         get_next_page(cursor)) {
895                 if (!trylock_page(cursor)) {
896                         fail = cursor;
897                         goto unlock;
898                 }
899         }
900
901         return 1;
902 unlock:
903         for (cursor = get_first_page(zspage); cursor != fail; cursor =
904                                         get_next_page(cursor))
905                 unlock_page(cursor);
906
907         return 0;
908 }
909
910 static void __free_zspage(struct zs_pool *pool, struct size_class *class,
911                                 struct zspage *zspage)
912 {
913         struct page *page, *next;
914         enum fullness_group fg;
915         unsigned int class_idx;
916
917         get_zspage_mapping(zspage, &class_idx, &fg);
918
919         assert_spin_locked(&class->lock);
920
921         VM_BUG_ON(get_zspage_inuse(zspage));
922         VM_BUG_ON(fg != ZS_EMPTY);
923
924         next = page = get_first_page(zspage);
925         do {
926                 VM_BUG_ON_PAGE(!PageLocked(page), page);
927                 next = get_next_page(page);
928                 reset_page(page);
929                 unlock_page(page);
930                 dec_zone_page_state(page, NR_ZSPAGES);
931                 put_page(page);
932                 page = next;
933         } while (page != NULL);
934
935         cache_free_zspage(pool, zspage);
936
937         class_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
938         atomic_long_sub(class->pages_per_zspage,
939                                         &pool->pages_allocated);
940 }
941
942 static void free_zspage(struct zs_pool *pool, struct size_class *class,
943                                 struct zspage *zspage)
944 {
945         VM_BUG_ON(get_zspage_inuse(zspage));
946         VM_BUG_ON(list_empty(&zspage->list));
947
948         /*
949          * Since zs_free couldn't be sleepable, this function cannot call
950          * lock_page. The page locks trylock_zspage got will be released
951          * by __free_zspage.
952          */
953         if (!trylock_zspage(zspage)) {
954                 kick_deferred_free(pool);
955                 return;
956         }
957
958         remove_zspage(class, zspage, ZS_EMPTY);
959         __free_zspage(pool, class, zspage);
960 }
961
962 /* Initialize a newly allocated zspage */
963 static void init_zspage(struct size_class *class, struct zspage *zspage)
964 {
965         unsigned int freeobj = 1;
966         unsigned long off = 0;
967         struct page *page = get_first_page(zspage);
968
969         while (page) {
970                 struct page *next_page;
971                 struct link_free *link;
972                 void *vaddr;
973
974                 set_first_obj_offset(page, off);
975
976                 vaddr = kmap_atomic(page);
977                 link = (struct link_free *)vaddr + off / sizeof(*link);
978
979                 while ((off += class->size) < PAGE_SIZE) {
980                         link->next = freeobj++ << OBJ_TAG_BITS;
981                         link += class->size / sizeof(*link);
982                 }
983
984                 /*
985                  * We now come to the last (full or partial) object on this
986                  * page, which must point to the first object on the next
987                  * page (if present)
988                  */
989                 next_page = get_next_page(page);
990                 if (next_page) {
991                         link->next = freeobj++ << OBJ_TAG_BITS;
992                 } else {
993                         /*
994                          * Reset OBJ_TAG_BITS bit to last link to tell
995                          * whether it's allocated object or not.
996                          */
997                         link->next = -1UL << OBJ_TAG_BITS;
998                 }
999                 kunmap_atomic(vaddr);
1000                 page = next_page;
1001                 off %= PAGE_SIZE;
1002         }
1003
1004         set_freeobj(zspage, 0);
1005 }
1006
1007 static void create_page_chain(struct size_class *class, struct zspage *zspage,
1008                                 struct page *pages[])
1009 {
1010         int i;
1011         struct page *page;
1012         struct page *prev_page = NULL;
1013         int nr_pages = class->pages_per_zspage;
1014
1015         /*
1016          * Allocate individual pages and link them together as:
1017          * 1. all pages are linked together using page->index
1018          * 2. each sub-page point to zspage using page->private
1019          *
1020          * we set PG_private to identify the first page (i.e. no other sub-page
1021          * has this flag set).
1022          */
1023         for (i = 0; i < nr_pages; i++) {
1024                 page = pages[i];
1025                 set_page_private(page, (unsigned long)zspage);
1026                 page->index = 0;
1027                 if (i == 0) {
1028                         zspage->first_page = page;
1029                         SetPagePrivate(page);
1030                         if (unlikely(class->objs_per_zspage == 1 &&
1031                                         class->pages_per_zspage == 1))
1032                                 SetZsHugePage(zspage);
1033                 } else {
1034                         prev_page->index = (unsigned long)page;
1035                 }
1036                 prev_page = page;
1037         }
1038 }
1039
1040 /*
1041  * Allocate a zspage for the given size class
1042  */
1043 static struct zspage *alloc_zspage(struct zs_pool *pool,
1044                                         struct size_class *class,
1045                                         gfp_t gfp)
1046 {
1047         int i;
1048         struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
1049         struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1050
1051         if (!zspage)
1052                 return NULL;
1053
1054         zspage->magic = ZSPAGE_MAGIC;
1055         migrate_lock_init(zspage);
1056
1057         for (i = 0; i < class->pages_per_zspage; i++) {
1058                 struct page *page;
1059
1060                 page = alloc_page(gfp);
1061                 if (!page) {
1062                         while (--i >= 0) {
1063                                 dec_zone_page_state(pages[i], NR_ZSPAGES);
1064                                 __free_page(pages[i]);
1065                         }
1066                         cache_free_zspage(pool, zspage);
1067                         return NULL;
1068                 }
1069
1070                 inc_zone_page_state(page, NR_ZSPAGES);
1071                 pages[i] = page;
1072         }
1073
1074         create_page_chain(class, zspage, pages);
1075         init_zspage(class, zspage);
1076         zspage->pool = pool;
1077
1078         return zspage;
1079 }
1080
1081 static struct zspage *find_get_zspage(struct size_class *class)
1082 {
1083         int i;
1084         struct zspage *zspage;
1085
1086         for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
1087                 zspage = list_first_entry_or_null(&class->fullness_list[i],
1088                                 struct zspage, list);
1089                 if (zspage)
1090                         break;
1091         }
1092
1093         return zspage;
1094 }
1095
1096 static inline int __zs_cpu_up(struct mapping_area *area)
1097 {
1098         /*
1099          * Make sure we don't leak memory if a cpu UP notification
1100          * and zs_init() race and both call zs_cpu_up() on the same cpu
1101          */
1102         if (area->vm_buf)
1103                 return 0;
1104         area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
1105         if (!area->vm_buf)
1106                 return -ENOMEM;
1107         return 0;
1108 }
1109
1110 static inline void __zs_cpu_down(struct mapping_area *area)
1111 {
1112         kfree(area->vm_buf);
1113         area->vm_buf = NULL;
1114 }
1115
1116 static void *__zs_map_object(struct mapping_area *area,
1117                         struct page *pages[2], int off, int size)
1118 {
1119         int sizes[2];
1120         void *addr;
1121         char *buf = area->vm_buf;
1122
1123         /* disable page faults to match kmap_atomic() return conditions */
1124         pagefault_disable();
1125
1126         /* no read fastpath */
1127         if (area->vm_mm == ZS_MM_WO)
1128                 goto out;
1129
1130         sizes[0] = PAGE_SIZE - off;
1131         sizes[1] = size - sizes[0];
1132
1133         /* copy object to per-cpu buffer */
1134         addr = kmap_atomic(pages[0]);
1135         memcpy(buf, addr + off, sizes[0]);
1136         kunmap_atomic(addr);
1137         addr = kmap_atomic(pages[1]);
1138         memcpy(buf + sizes[0], addr, sizes[1]);
1139         kunmap_atomic(addr);
1140 out:
1141         return area->vm_buf;
1142 }
1143
1144 static void __zs_unmap_object(struct mapping_area *area,
1145                         struct page *pages[2], int off, int size)
1146 {
1147         int sizes[2];
1148         void *addr;
1149         char *buf;
1150
1151         /* no write fastpath */
1152         if (area->vm_mm == ZS_MM_RO)
1153                 goto out;
1154
1155         buf = area->vm_buf;
1156         buf = buf + ZS_HANDLE_SIZE;
1157         size -= ZS_HANDLE_SIZE;
1158         off += ZS_HANDLE_SIZE;
1159
1160         sizes[0] = PAGE_SIZE - off;
1161         sizes[1] = size - sizes[0];
1162
1163         /* copy per-cpu buffer to object */
1164         addr = kmap_atomic(pages[0]);
1165         memcpy(addr + off, buf, sizes[0]);
1166         kunmap_atomic(addr);
1167         addr = kmap_atomic(pages[1]);
1168         memcpy(addr, buf + sizes[0], sizes[1]);
1169         kunmap_atomic(addr);
1170
1171 out:
1172         /* enable page faults to match kunmap_atomic() return conditions */
1173         pagefault_enable();
1174 }
1175
1176 static int zs_cpu_prepare(unsigned int cpu)
1177 {
1178         struct mapping_area *area;
1179
1180         area = &per_cpu(zs_map_area, cpu);
1181         return __zs_cpu_up(area);
1182 }
1183
1184 static int zs_cpu_dead(unsigned int cpu)
1185 {
1186         struct mapping_area *area;
1187
1188         area = &per_cpu(zs_map_area, cpu);
1189         __zs_cpu_down(area);
1190         return 0;
1191 }
1192
1193 static bool can_merge(struct size_class *prev, int pages_per_zspage,
1194                                         int objs_per_zspage)
1195 {
1196         if (prev->pages_per_zspage == pages_per_zspage &&
1197                 prev->objs_per_zspage == objs_per_zspage)
1198                 return true;
1199
1200         return false;
1201 }
1202
1203 static bool zspage_full(struct size_class *class, struct zspage *zspage)
1204 {
1205         return get_zspage_inuse(zspage) == class->objs_per_zspage;
1206 }
1207
1208 /**
1209  * zs_lookup_class_index() - Returns index of the zsmalloc &size_class
1210  * that hold objects of the provided size.
1211  * @pool: zsmalloc pool to use
1212  * @size: object size
1213  *
1214  * Context: Any context.
1215  *
1216  * Return: the index of the zsmalloc &size_class that hold objects of the
1217  * provided size.
1218  */
1219 unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
1220 {
1221         struct size_class *class;
1222
1223         class = pool->size_class[get_size_class_index(size)];
1224
1225         return class->index;
1226 }
1227 EXPORT_SYMBOL_GPL(zs_lookup_class_index);
1228
1229 unsigned long zs_get_total_pages(struct zs_pool *pool)
1230 {
1231         return atomic_long_read(&pool->pages_allocated);
1232 }
1233 EXPORT_SYMBOL_GPL(zs_get_total_pages);
1234
1235 /**
1236  * zs_map_object - get address of allocated object from handle.
1237  * @pool: pool from which the object was allocated
1238  * @handle: handle returned from zs_malloc
1239  * @mm: mapping mode to use
1240  *
1241  * Before using an object allocated from zs_malloc, it must be mapped using
1242  * this function. When done with the object, it must be unmapped using
1243  * zs_unmap_object.
1244  *
1245  * Only one object can be mapped per cpu at a time. There is no protection
1246  * against nested mappings.
1247  *
1248  * This function returns with preemption and page faults disabled.
1249  */
1250 void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1251                         enum zs_mapmode mm)
1252 {
1253         struct zspage *zspage;
1254         struct page *page;
1255         unsigned long obj, off;
1256         unsigned int obj_idx;
1257
1258         struct size_class *class;
1259         struct mapping_area *area;
1260         struct page *pages[2];
1261         void *ret;
1262
1263         /*
1264          * Because we use per-cpu mapping areas shared among the
1265          * pools/users, we can't allow mapping in interrupt context
1266          * because it can corrupt another users mappings.
1267          */
1268         BUG_ON(in_interrupt());
1269
1270         /* It guarantees it can get zspage from handle safely */
1271         read_lock(&pool->migrate_lock);
1272         obj = handle_to_obj(handle);
1273         obj_to_location(obj, &page, &obj_idx);
1274         zspage = get_zspage(page);
1275
1276         /*
1277          * migration cannot move any zpages in this zspage. Here, class->lock
1278          * is too heavy since callers would take some time until they calls
1279          * zs_unmap_object API so delegate the locking from class to zspage
1280          * which is smaller granularity.
1281          */
1282         migrate_read_lock(zspage);
1283         read_unlock(&pool->migrate_lock);
1284
1285         class = zspage_class(pool, zspage);
1286         off = (class->size * obj_idx) & ~PAGE_MASK;
1287
1288         local_lock(&zs_map_area.lock);
1289         area = this_cpu_ptr(&zs_map_area);
1290         area->vm_mm = mm;
1291         if (off + class->size <= PAGE_SIZE) {
1292                 /* this object is contained entirely within a page */
1293                 area->vm_addr = kmap_atomic(page);
1294                 ret = area->vm_addr + off;
1295                 goto out;
1296         }
1297
1298         /* this object spans two pages */
1299         pages[0] = page;
1300         pages[1] = get_next_page(page);
1301         BUG_ON(!pages[1]);
1302
1303         ret = __zs_map_object(area, pages, off, class->size);
1304 out:
1305         if (likely(!ZsHugePage(zspage)))
1306                 ret += ZS_HANDLE_SIZE;
1307
1308         return ret;
1309 }
1310 EXPORT_SYMBOL_GPL(zs_map_object);
1311
1312 void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1313 {
1314         struct zspage *zspage;
1315         struct page *page;
1316         unsigned long obj, off;
1317         unsigned int obj_idx;
1318
1319         struct size_class *class;
1320         struct mapping_area *area;
1321
1322         obj = handle_to_obj(handle);
1323         obj_to_location(obj, &page, &obj_idx);
1324         zspage = get_zspage(page);
1325         class = zspage_class(pool, zspage);
1326         off = (class->size * obj_idx) & ~PAGE_MASK;
1327
1328         area = this_cpu_ptr(&zs_map_area);
1329         if (off + class->size <= PAGE_SIZE)
1330                 kunmap_atomic(area->vm_addr);
1331         else {
1332                 struct page *pages[2];
1333
1334                 pages[0] = page;
1335                 pages[1] = get_next_page(page);
1336                 BUG_ON(!pages[1]);
1337
1338                 __zs_unmap_object(area, pages, off, class->size);
1339         }
1340         local_unlock(&zs_map_area.lock);
1341
1342         migrate_read_unlock(zspage);
1343 }
1344 EXPORT_SYMBOL_GPL(zs_unmap_object);
1345
1346 /**
1347  * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1348  *                        zsmalloc &size_class.
1349  * @pool: zsmalloc pool to use
1350  *
1351  * The function returns the size of the first huge class - any object of equal
1352  * or bigger size will be stored in zspage consisting of a single physical
1353  * page.
1354  *
1355  * Context: Any context.
1356  *
1357  * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1358  */
1359 size_t zs_huge_class_size(struct zs_pool *pool)
1360 {
1361         return huge_class_size;
1362 }
1363 EXPORT_SYMBOL_GPL(zs_huge_class_size);
1364
1365 static unsigned long obj_malloc(struct zs_pool *pool,
1366                                 struct zspage *zspage, unsigned long handle)
1367 {
1368         int i, nr_page, offset;
1369         unsigned long obj;
1370         struct link_free *link;
1371         struct size_class *class;
1372
1373         struct page *m_page;
1374         unsigned long m_offset;
1375         void *vaddr;
1376
1377         class = pool->size_class[zspage->class];
1378         handle |= OBJ_ALLOCATED_TAG;
1379         obj = get_freeobj(zspage);
1380
1381         offset = obj * class->size;
1382         nr_page = offset >> PAGE_SHIFT;
1383         m_offset = offset & ~PAGE_MASK;
1384         m_page = get_first_page(zspage);
1385
1386         for (i = 0; i < nr_page; i++)
1387                 m_page = get_next_page(m_page);
1388
1389         vaddr = kmap_atomic(m_page);
1390         link = (struct link_free *)vaddr + m_offset / sizeof(*link);
1391         set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
1392         if (likely(!ZsHugePage(zspage)))
1393                 /* record handle in the header of allocated chunk */
1394                 link->handle = handle;
1395         else
1396                 /* record handle to page->index */
1397                 zspage->first_page->index = handle;
1398
1399         kunmap_atomic(vaddr);
1400         mod_zspage_inuse(zspage, 1);
1401
1402         obj = location_to_obj(m_page, obj);
1403
1404         return obj;
1405 }
1406
1407
1408 /**
1409  * zs_malloc - Allocate block of given size from pool.
1410  * @pool: pool to allocate from
1411  * @size: size of block to allocate
1412  * @gfp: gfp flags when allocating object
1413  *
1414  * On success, handle to the allocated object is returned,
1415  * otherwise an ERR_PTR().
1416  * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1417  */
1418 unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
1419 {
1420         unsigned long handle, obj;
1421         struct size_class *class;
1422         enum fullness_group newfg;
1423         struct zspage *zspage;
1424
1425         if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
1426                 return (unsigned long)ERR_PTR(-EINVAL);
1427
1428         handle = cache_alloc_handle(pool, gfp);
1429         if (!handle)
1430                 return (unsigned long)ERR_PTR(-ENOMEM);
1431
1432         /* extra space in chunk to keep the handle */
1433         size += ZS_HANDLE_SIZE;
1434         class = pool->size_class[get_size_class_index(size)];
1435
1436         /* class->lock effectively protects the zpage migration */
1437         spin_lock(&class->lock);
1438         zspage = find_get_zspage(class);
1439         if (likely(zspage)) {
1440                 obj = obj_malloc(pool, zspage, handle);
1441                 /* Now move the zspage to another fullness group, if required */
1442                 fix_fullness_group(class, zspage);
1443                 record_obj(handle, obj);
1444                 class_stat_inc(class, OBJ_USED, 1);
1445                 spin_unlock(&class->lock);
1446
1447                 return handle;
1448         }
1449
1450         spin_unlock(&class->lock);
1451
1452         zspage = alloc_zspage(pool, class, gfp);
1453         if (!zspage) {
1454                 cache_free_handle(pool, handle);
1455                 return (unsigned long)ERR_PTR(-ENOMEM);
1456         }
1457
1458         spin_lock(&class->lock);
1459         obj = obj_malloc(pool, zspage, handle);
1460         newfg = get_fullness_group(class, zspage);
1461         insert_zspage(class, zspage, newfg);
1462         set_zspage_mapping(zspage, class->index, newfg);
1463         record_obj(handle, obj);
1464         atomic_long_add(class->pages_per_zspage,
1465                                 &pool->pages_allocated);
1466         class_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
1467         class_stat_inc(class, OBJ_USED, 1);
1468
1469         /* We completely set up zspage so mark them as movable */
1470         SetZsPageMovable(pool, zspage);
1471         spin_unlock(&class->lock);
1472
1473         return handle;
1474 }
1475 EXPORT_SYMBOL_GPL(zs_malloc);
1476
1477 static void obj_free(int class_size, unsigned long obj)
1478 {
1479         struct link_free *link;
1480         struct zspage *zspage;
1481         struct page *f_page;
1482         unsigned long f_offset;
1483         unsigned int f_objidx;
1484         void *vaddr;
1485
1486         obj_to_location(obj, &f_page, &f_objidx);
1487         f_offset = (class_size * f_objidx) & ~PAGE_MASK;
1488         zspage = get_zspage(f_page);
1489
1490         vaddr = kmap_atomic(f_page);
1491
1492         /* Insert this object in containing zspage's freelist */
1493         link = (struct link_free *)(vaddr + f_offset);
1494         if (likely(!ZsHugePage(zspage)))
1495                 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
1496         else
1497                 f_page->index = 0;
1498         kunmap_atomic(vaddr);
1499         set_freeobj(zspage, f_objidx);
1500         mod_zspage_inuse(zspage, -1);
1501 }
1502
1503 void zs_free(struct zs_pool *pool, unsigned long handle)
1504 {
1505         struct zspage *zspage;
1506         struct page *f_page;
1507         unsigned long obj;
1508         struct size_class *class;
1509         enum fullness_group fullness;
1510
1511         if (IS_ERR_OR_NULL((void *)handle))
1512                 return;
1513
1514         /*
1515          * The pool->migrate_lock protects the race with zpage's migration
1516          * so it's safe to get the page from handle.
1517          */
1518         read_lock(&pool->migrate_lock);
1519         obj = handle_to_obj(handle);
1520         obj_to_page(obj, &f_page);
1521         zspage = get_zspage(f_page);
1522         class = zspage_class(pool, zspage);
1523         spin_lock(&class->lock);
1524         read_unlock(&pool->migrate_lock);
1525
1526         obj_free(class->size, obj);
1527         class_stat_dec(class, OBJ_USED, 1);
1528         fullness = fix_fullness_group(class, zspage);
1529         if (fullness != ZS_EMPTY)
1530                 goto out;
1531
1532         free_zspage(pool, class, zspage);
1533 out:
1534         spin_unlock(&class->lock);
1535         cache_free_handle(pool, handle);
1536 }
1537 EXPORT_SYMBOL_GPL(zs_free);
1538
1539 static void zs_object_copy(struct size_class *class, unsigned long dst,
1540                                 unsigned long src)
1541 {
1542         struct page *s_page, *d_page;
1543         unsigned int s_objidx, d_objidx;
1544         unsigned long s_off, d_off;
1545         void *s_addr, *d_addr;
1546         int s_size, d_size, size;
1547         int written = 0;
1548
1549         s_size = d_size = class->size;
1550
1551         obj_to_location(src, &s_page, &s_objidx);
1552         obj_to_location(dst, &d_page, &d_objidx);
1553
1554         s_off = (class->size * s_objidx) & ~PAGE_MASK;
1555         d_off = (class->size * d_objidx) & ~PAGE_MASK;
1556
1557         if (s_off + class->size > PAGE_SIZE)
1558                 s_size = PAGE_SIZE - s_off;
1559
1560         if (d_off + class->size > PAGE_SIZE)
1561                 d_size = PAGE_SIZE - d_off;
1562
1563         s_addr = kmap_atomic(s_page);
1564         d_addr = kmap_atomic(d_page);
1565
1566         while (1) {
1567                 size = min(s_size, d_size);
1568                 memcpy(d_addr + d_off, s_addr + s_off, size);
1569                 written += size;
1570
1571                 if (written == class->size)
1572                         break;
1573
1574                 s_off += size;
1575                 s_size -= size;
1576                 d_off += size;
1577                 d_size -= size;
1578
1579                 /*
1580                  * Calling kunmap_atomic(d_addr) is necessary. kunmap_atomic()
1581                  * calls must occurs in reverse order of calls to kmap_atomic().
1582                  * So, to call kunmap_atomic(s_addr) we should first call
1583                  * kunmap_atomic(d_addr). For more details see
1584                  * Documentation/mm/highmem.rst.
1585                  */
1586                 if (s_off >= PAGE_SIZE) {
1587                         kunmap_atomic(d_addr);
1588                         kunmap_atomic(s_addr);
1589                         s_page = get_next_page(s_page);
1590                         s_addr = kmap_atomic(s_page);
1591                         d_addr = kmap_atomic(d_page);
1592                         s_size = class->size - written;
1593                         s_off = 0;
1594                 }
1595
1596                 if (d_off >= PAGE_SIZE) {
1597                         kunmap_atomic(d_addr);
1598                         d_page = get_next_page(d_page);
1599                         d_addr = kmap_atomic(d_page);
1600                         d_size = class->size - written;
1601                         d_off = 0;
1602                 }
1603         }
1604
1605         kunmap_atomic(d_addr);
1606         kunmap_atomic(s_addr);
1607 }
1608
1609 /*
1610  * Find alloced object in zspage from index object and
1611  * return handle.
1612  */
1613 static unsigned long find_alloced_obj(struct size_class *class,
1614                                         struct page *page, int *obj_idx)
1615 {
1616         unsigned int offset;
1617         int index = *obj_idx;
1618         unsigned long handle = 0;
1619         void *addr = kmap_atomic(page);
1620
1621         offset = get_first_obj_offset(page);
1622         offset += class->size * index;
1623
1624         while (offset < PAGE_SIZE) {
1625                 if (obj_allocated(page, addr + offset, &handle))
1626                         break;
1627
1628                 offset += class->size;
1629                 index++;
1630         }
1631
1632         kunmap_atomic(addr);
1633
1634         *obj_idx = index;
1635
1636         return handle;
1637 }
1638
1639 struct zs_compact_control {
1640         /* Source spage for migration which could be a subpage of zspage */
1641         struct page *s_page;
1642         /* Destination page for migration which should be a first page
1643          * of zspage. */
1644         struct page *d_page;
1645          /* Starting object index within @s_page which used for live object
1646           * in the subpage. */
1647         int obj_idx;
1648 };
1649
1650 static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1651                                 struct zs_compact_control *cc)
1652 {
1653         unsigned long used_obj, free_obj;
1654         unsigned long handle;
1655         struct page *s_page = cc->s_page;
1656         struct page *d_page = cc->d_page;
1657         int obj_idx = cc->obj_idx;
1658         int ret = 0;
1659
1660         while (1) {
1661                 handle = find_alloced_obj(class, s_page, &obj_idx);
1662                 if (!handle) {
1663                         s_page = get_next_page(s_page);
1664                         if (!s_page)
1665                                 break;
1666                         obj_idx = 0;
1667                         continue;
1668                 }
1669
1670                 /* Stop if there is no more space */
1671                 if (zspage_full(class, get_zspage(d_page))) {
1672                         ret = -ENOMEM;
1673                         break;
1674                 }
1675
1676                 used_obj = handle_to_obj(handle);
1677                 free_obj = obj_malloc(pool, get_zspage(d_page), handle);
1678                 zs_object_copy(class, free_obj, used_obj);
1679                 obj_idx++;
1680                 record_obj(handle, free_obj);
1681                 obj_free(class->size, used_obj);
1682         }
1683
1684         /* Remember last position in this iteration */
1685         cc->s_page = s_page;
1686         cc->obj_idx = obj_idx;
1687
1688         return ret;
1689 }
1690
1691 static struct zspage *isolate_zspage(struct size_class *class, bool source)
1692 {
1693         int i;
1694         struct zspage *zspage;
1695         enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
1696
1697         if (!source) {
1698                 fg[0] = ZS_ALMOST_FULL;
1699                 fg[1] = ZS_ALMOST_EMPTY;
1700         }
1701
1702         for (i = 0; i < 2; i++) {
1703                 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1704                                                         struct zspage, list);
1705                 if (zspage) {
1706                         remove_zspage(class, zspage, fg[i]);
1707                         return zspage;
1708                 }
1709         }
1710
1711         return zspage;
1712 }
1713
1714 /*
1715  * putback_zspage - add @zspage into right class's fullness list
1716  * @class: destination class
1717  * @zspage: target page
1718  *
1719  * Return @zspage's fullness_group
1720  */
1721 static enum fullness_group putback_zspage(struct size_class *class,
1722                         struct zspage *zspage)
1723 {
1724         enum fullness_group fullness;
1725
1726         fullness = get_fullness_group(class, zspage);
1727         insert_zspage(class, zspage, fullness);
1728         set_zspage_mapping(zspage, class->index, fullness);
1729
1730         return fullness;
1731 }
1732
1733 #ifdef CONFIG_COMPACTION
1734 /*
1735  * To prevent zspage destroy during migration, zspage freeing should
1736  * hold locks of all pages in the zspage.
1737  */
1738 static void lock_zspage(struct zspage *zspage)
1739 {
1740         struct page *curr_page, *page;
1741
1742         /*
1743          * Pages we haven't locked yet can be migrated off the list while we're
1744          * trying to lock them, so we need to be careful and only attempt to
1745          * lock each page under migrate_read_lock(). Otherwise, the page we lock
1746          * may no longer belong to the zspage. This means that we may wait for
1747          * the wrong page to unlock, so we must take a reference to the page
1748          * prior to waiting for it to unlock outside migrate_read_lock().
1749          */
1750         while (1) {
1751                 migrate_read_lock(zspage);
1752                 page = get_first_page(zspage);
1753                 if (trylock_page(page))
1754                         break;
1755                 get_page(page);
1756                 migrate_read_unlock(zspage);
1757                 wait_on_page_locked(page);
1758                 put_page(page);
1759         }
1760
1761         curr_page = page;
1762         while ((page = get_next_page(curr_page))) {
1763                 if (trylock_page(page)) {
1764                         curr_page = page;
1765                 } else {
1766                         get_page(page);
1767                         migrate_read_unlock(zspage);
1768                         wait_on_page_locked(page);
1769                         put_page(page);
1770                         migrate_read_lock(zspage);
1771                 }
1772         }
1773         migrate_read_unlock(zspage);
1774 }
1775
1776 static void migrate_lock_init(struct zspage *zspage)
1777 {
1778         rwlock_init(&zspage->lock);
1779 }
1780
1781 static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock)
1782 {
1783         read_lock(&zspage->lock);
1784 }
1785
1786 static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock)
1787 {
1788         read_unlock(&zspage->lock);
1789 }
1790
1791 static void migrate_write_lock(struct zspage *zspage)
1792 {
1793         write_lock(&zspage->lock);
1794 }
1795
1796 static void migrate_write_lock_nested(struct zspage *zspage)
1797 {
1798         write_lock_nested(&zspage->lock, SINGLE_DEPTH_NESTING);
1799 }
1800
1801 static void migrate_write_unlock(struct zspage *zspage)
1802 {
1803         write_unlock(&zspage->lock);
1804 }
1805
1806 /* Number of isolated subpage for *page migration* in this zspage */
1807 static void inc_zspage_isolation(struct zspage *zspage)
1808 {
1809         zspage->isolated++;
1810 }
1811
1812 static void dec_zspage_isolation(struct zspage *zspage)
1813 {
1814         VM_BUG_ON(zspage->isolated == 0);
1815         zspage->isolated--;
1816 }
1817
1818 static const struct movable_operations zsmalloc_mops;
1819
1820 static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1821                                 struct page *newpage, struct page *oldpage)
1822 {
1823         struct page *page;
1824         struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1825         int idx = 0;
1826
1827         page = get_first_page(zspage);
1828         do {
1829                 if (page == oldpage)
1830                         pages[idx] = newpage;
1831                 else
1832                         pages[idx] = page;
1833                 idx++;
1834         } while ((page = get_next_page(page)) != NULL);
1835
1836         create_page_chain(class, zspage, pages);
1837         set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1838         if (unlikely(ZsHugePage(zspage)))
1839                 newpage->index = oldpage->index;
1840         __SetPageMovable(newpage, &zsmalloc_mops);
1841 }
1842
1843 static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1844 {
1845         struct zspage *zspage;
1846
1847         /*
1848          * Page is locked so zspage couldn't be destroyed. For detail, look at
1849          * lock_zspage in free_zspage.
1850          */
1851         VM_BUG_ON_PAGE(!PageMovable(page), page);
1852         VM_BUG_ON_PAGE(PageIsolated(page), page);
1853
1854         zspage = get_zspage(page);
1855         migrate_write_lock(zspage);
1856         inc_zspage_isolation(zspage);
1857         migrate_write_unlock(zspage);
1858
1859         return true;
1860 }
1861
1862 static int zs_page_migrate(struct page *newpage, struct page *page,
1863                 enum migrate_mode mode)
1864 {
1865         struct zs_pool *pool;
1866         struct size_class *class;
1867         struct zspage *zspage;
1868         struct page *dummy;
1869         void *s_addr, *d_addr, *addr;
1870         unsigned int offset;
1871         unsigned long handle;
1872         unsigned long old_obj, new_obj;
1873         unsigned int obj_idx;
1874
1875         /*
1876          * We cannot support the _NO_COPY case here, because copy needs to
1877          * happen under the zs lock, which does not work with
1878          * MIGRATE_SYNC_NO_COPY workflow.
1879          */
1880         if (mode == MIGRATE_SYNC_NO_COPY)
1881                 return -EINVAL;
1882
1883         VM_BUG_ON_PAGE(!PageMovable(page), page);
1884         VM_BUG_ON_PAGE(!PageIsolated(page), page);
1885
1886         /* The page is locked, so this pointer must remain valid */
1887         zspage = get_zspage(page);
1888         pool = zspage->pool;
1889
1890         /*
1891          * The pool migrate_lock protects the race between zpage migration
1892          * and zs_free.
1893          */
1894         write_lock(&pool->migrate_lock);
1895         class = zspage_class(pool, zspage);
1896
1897         /*
1898          * the class lock protects zpage alloc/free in the zspage.
1899          */
1900         spin_lock(&class->lock);
1901         /* the migrate_write_lock protects zpage access via zs_map_object */
1902         migrate_write_lock(zspage);
1903
1904         offset = get_first_obj_offset(page);
1905         s_addr = kmap_atomic(page);
1906
1907         /*
1908          * Here, any user cannot access all objects in the zspage so let's move.
1909          */
1910         d_addr = kmap_atomic(newpage);
1911         memcpy(d_addr, s_addr, PAGE_SIZE);
1912         kunmap_atomic(d_addr);
1913
1914         for (addr = s_addr + offset; addr < s_addr + PAGE_SIZE;
1915                                         addr += class->size) {
1916                 if (obj_allocated(page, addr, &handle)) {
1917
1918                         old_obj = handle_to_obj(handle);
1919                         obj_to_location(old_obj, &dummy, &obj_idx);
1920                         new_obj = (unsigned long)location_to_obj(newpage,
1921                                                                 obj_idx);
1922                         record_obj(handle, new_obj);
1923                 }
1924         }
1925         kunmap_atomic(s_addr);
1926
1927         replace_sub_page(class, zspage, newpage, page);
1928         /*
1929          * Since we complete the data copy and set up new zspage structure,
1930          * it's okay to release migration_lock.
1931          */
1932         write_unlock(&pool->migrate_lock);
1933         spin_unlock(&class->lock);
1934         dec_zspage_isolation(zspage);
1935         migrate_write_unlock(zspage);
1936
1937         get_page(newpage);
1938         if (page_zone(newpage) != page_zone(page)) {
1939                 dec_zone_page_state(page, NR_ZSPAGES);
1940                 inc_zone_page_state(newpage, NR_ZSPAGES);
1941         }
1942
1943         reset_page(page);
1944         put_page(page);
1945
1946         return MIGRATEPAGE_SUCCESS;
1947 }
1948
1949 static void zs_page_putback(struct page *page)
1950 {
1951         struct zspage *zspage;
1952
1953         VM_BUG_ON_PAGE(!PageMovable(page), page);
1954         VM_BUG_ON_PAGE(!PageIsolated(page), page);
1955
1956         zspage = get_zspage(page);
1957         migrate_write_lock(zspage);
1958         dec_zspage_isolation(zspage);
1959         migrate_write_unlock(zspage);
1960 }
1961
1962 static const struct movable_operations zsmalloc_mops = {
1963         .isolate_page = zs_page_isolate,
1964         .migrate_page = zs_page_migrate,
1965         .putback_page = zs_page_putback,
1966 };
1967
1968 /*
1969  * Caller should hold page_lock of all pages in the zspage
1970  * In here, we cannot use zspage meta data.
1971  */
1972 static void async_free_zspage(struct work_struct *work)
1973 {
1974         int i;
1975         struct size_class *class;
1976         unsigned int class_idx;
1977         enum fullness_group fullness;
1978         struct zspage *zspage, *tmp;
1979         LIST_HEAD(free_pages);
1980         struct zs_pool *pool = container_of(work, struct zs_pool,
1981                                         free_work);
1982
1983         for (i = 0; i < ZS_SIZE_CLASSES; i++) {
1984                 class = pool->size_class[i];
1985                 if (class->index != i)
1986                         continue;
1987
1988                 spin_lock(&class->lock);
1989                 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
1990                 spin_unlock(&class->lock);
1991         }
1992
1993         list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
1994                 list_del(&zspage->list);
1995                 lock_zspage(zspage);
1996
1997                 get_zspage_mapping(zspage, &class_idx, &fullness);
1998                 VM_BUG_ON(fullness != ZS_EMPTY);
1999                 class = pool->size_class[class_idx];
2000                 spin_lock(&class->lock);
2001                 __free_zspage(pool, class, zspage);
2002                 spin_unlock(&class->lock);
2003         }
2004 };
2005
2006 static void kick_deferred_free(struct zs_pool *pool)
2007 {
2008         schedule_work(&pool->free_work);
2009 }
2010
2011 static void zs_flush_migration(struct zs_pool *pool)
2012 {
2013         flush_work(&pool->free_work);
2014 }
2015
2016 static void init_deferred_free(struct zs_pool *pool)
2017 {
2018         INIT_WORK(&pool->free_work, async_free_zspage);
2019 }
2020
2021 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2022 {
2023         struct page *page = get_first_page(zspage);
2024
2025         do {
2026                 WARN_ON(!trylock_page(page));
2027                 __SetPageMovable(page, &zsmalloc_mops);
2028                 unlock_page(page);
2029         } while ((page = get_next_page(page)) != NULL);
2030 }
2031 #else
2032 static inline void zs_flush_migration(struct zs_pool *pool) { }
2033 #endif
2034
2035 /*
2036  *
2037  * Based on the number of unused allocated objects calculate
2038  * and return the number of pages that we can free.
2039  */
2040 static unsigned long zs_can_compact(struct size_class *class)
2041 {
2042         unsigned long obj_wasted;
2043         unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2044         unsigned long obj_used = zs_stat_get(class, OBJ_USED);
2045
2046         if (obj_allocated <= obj_used)
2047                 return 0;
2048
2049         obj_wasted = obj_allocated - obj_used;
2050         obj_wasted /= class->objs_per_zspage;
2051
2052         return obj_wasted * class->pages_per_zspage;
2053 }
2054
2055 static unsigned long __zs_compact(struct zs_pool *pool,
2056                                   struct size_class *class)
2057 {
2058         struct zs_compact_control cc;
2059         struct zspage *src_zspage;
2060         struct zspage *dst_zspage = NULL;
2061         unsigned long pages_freed = 0;
2062
2063         /* protect the race between zpage migration and zs_free */
2064         write_lock(&pool->migrate_lock);
2065         /* protect zpage allocation/free */
2066         spin_lock(&class->lock);
2067         while ((src_zspage = isolate_zspage(class, true))) {
2068                 /* protect someone accessing the zspage(i.e., zs_map_object) */
2069                 migrate_write_lock(src_zspage);
2070
2071                 if (!zs_can_compact(class))
2072                         break;
2073
2074                 cc.obj_idx = 0;
2075                 cc.s_page = get_first_page(src_zspage);
2076
2077                 while ((dst_zspage = isolate_zspage(class, false))) {
2078                         migrate_write_lock_nested(dst_zspage);
2079
2080                         cc.d_page = get_first_page(dst_zspage);
2081                         /*
2082                          * If there is no more space in dst_page, resched
2083                          * and see if anyone had allocated another zspage.
2084                          */
2085                         if (!migrate_zspage(pool, class, &cc))
2086                                 break;
2087
2088                         putback_zspage(class, dst_zspage);
2089                         migrate_write_unlock(dst_zspage);
2090                         dst_zspage = NULL;
2091                         if (rwlock_is_contended(&pool->migrate_lock))
2092                                 break;
2093                 }
2094
2095                 /* Stop if we couldn't find slot */
2096                 if (dst_zspage == NULL)
2097                         break;
2098
2099                 putback_zspage(class, dst_zspage);
2100                 migrate_write_unlock(dst_zspage);
2101
2102                 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
2103                         migrate_write_unlock(src_zspage);
2104                         free_zspage(pool, class, src_zspage);
2105                         pages_freed += class->pages_per_zspage;
2106                 } else
2107                         migrate_write_unlock(src_zspage);
2108                 spin_unlock(&class->lock);
2109                 write_unlock(&pool->migrate_lock);
2110                 cond_resched();
2111                 write_lock(&pool->migrate_lock);
2112                 spin_lock(&class->lock);
2113         }
2114
2115         if (src_zspage) {
2116                 putback_zspage(class, src_zspage);
2117                 migrate_write_unlock(src_zspage);
2118         }
2119
2120         spin_unlock(&class->lock);
2121         write_unlock(&pool->migrate_lock);
2122
2123         return pages_freed;
2124 }
2125
2126 unsigned long zs_compact(struct zs_pool *pool)
2127 {
2128         int i;
2129         struct size_class *class;
2130         unsigned long pages_freed = 0;
2131
2132         for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
2133                 class = pool->size_class[i];
2134                 if (class->index != i)
2135                         continue;
2136                 pages_freed += __zs_compact(pool, class);
2137         }
2138         atomic_long_add(pages_freed, &pool->stats.pages_compacted);
2139
2140         return pages_freed;
2141 }
2142 EXPORT_SYMBOL_GPL(zs_compact);
2143
2144 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2145 {
2146         memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2147 }
2148 EXPORT_SYMBOL_GPL(zs_pool_stats);
2149
2150 static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2151                 struct shrink_control *sc)
2152 {
2153         unsigned long pages_freed;
2154         struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2155                         shrinker);
2156
2157         /*
2158          * Compact classes and calculate compaction delta.
2159          * Can run concurrently with a manually triggered
2160          * (by user) compaction.
2161          */
2162         pages_freed = zs_compact(pool);
2163
2164         return pages_freed ? pages_freed : SHRINK_STOP;
2165 }
2166
2167 static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2168                 struct shrink_control *sc)
2169 {
2170         int i;
2171         struct size_class *class;
2172         unsigned long pages_to_free = 0;
2173         struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2174                         shrinker);
2175
2176         for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
2177                 class = pool->size_class[i];
2178                 if (class->index != i)
2179                         continue;
2180
2181                 pages_to_free += zs_can_compact(class);
2182         }
2183
2184         return pages_to_free;
2185 }
2186
2187 static void zs_unregister_shrinker(struct zs_pool *pool)
2188 {
2189         unregister_shrinker(&pool->shrinker);
2190 }
2191
2192 static int zs_register_shrinker(struct zs_pool *pool)
2193 {
2194         pool->shrinker.scan_objects = zs_shrinker_scan;
2195         pool->shrinker.count_objects = zs_shrinker_count;
2196         pool->shrinker.batch = 0;
2197         pool->shrinker.seeks = DEFAULT_SEEKS;
2198
2199         return register_shrinker(&pool->shrinker, "mm-zspool:%s",
2200                                  pool->name);
2201 }
2202
2203 /**
2204  * zs_create_pool - Creates an allocation pool to work from.
2205  * @name: pool name to be created
2206  *
2207  * This function must be called before anything when using
2208  * the zsmalloc allocator.
2209  *
2210  * On success, a pointer to the newly created pool is returned,
2211  * otherwise NULL.
2212  */
2213 struct zs_pool *zs_create_pool(const char *name)
2214 {
2215         int i;
2216         struct zs_pool *pool;
2217         struct size_class *prev_class = NULL;
2218
2219         pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2220         if (!pool)
2221                 return NULL;
2222
2223         init_deferred_free(pool);
2224         rwlock_init(&pool->migrate_lock);
2225
2226         pool->name = kstrdup(name, GFP_KERNEL);
2227         if (!pool->name)
2228                 goto err;
2229
2230         if (create_cache(pool))
2231                 goto err;
2232
2233         /*
2234          * Iterate reversely, because, size of size_class that we want to use
2235          * for merging should be larger or equal to current size.
2236          */
2237         for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
2238                 int size;
2239                 int pages_per_zspage;
2240                 int objs_per_zspage;
2241                 struct size_class *class;
2242                 int fullness = 0;
2243
2244                 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2245                 if (size > ZS_MAX_ALLOC_SIZE)
2246                         size = ZS_MAX_ALLOC_SIZE;
2247                 pages_per_zspage = get_pages_per_zspage(size);
2248                 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
2249
2250                 /*
2251                  * We iterate from biggest down to smallest classes,
2252                  * so huge_class_size holds the size of the first huge
2253                  * class. Any object bigger than or equal to that will
2254                  * endup in the huge class.
2255                  */
2256                 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2257                                 !huge_class_size) {
2258                         huge_class_size = size;
2259                         /*
2260                          * The object uses ZS_HANDLE_SIZE bytes to store the
2261                          * handle. We need to subtract it, because zs_malloc()
2262                          * unconditionally adds handle size before it performs
2263                          * size class search - so object may be smaller than
2264                          * huge class size, yet it still can end up in the huge
2265                          * class because it grows by ZS_HANDLE_SIZE extra bytes
2266                          * right before class lookup.
2267                          */
2268                         huge_class_size -= (ZS_HANDLE_SIZE - 1);
2269                 }
2270
2271                 /*
2272                  * size_class is used for normal zsmalloc operation such
2273                  * as alloc/free for that size. Although it is natural that we
2274                  * have one size_class for each size, there is a chance that we
2275                  * can get more memory utilization if we use one size_class for
2276                  * many different sizes whose size_class have same
2277                  * characteristics. So, we makes size_class point to
2278                  * previous size_class if possible.
2279                  */
2280                 if (prev_class) {
2281                         if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
2282                                 pool->size_class[i] = prev_class;
2283                                 continue;
2284                         }
2285                 }
2286
2287                 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2288                 if (!class)
2289                         goto err;
2290
2291                 class->size = size;
2292                 class->index = i;
2293                 class->pages_per_zspage = pages_per_zspage;
2294                 class->objs_per_zspage = objs_per_zspage;
2295                 spin_lock_init(&class->lock);
2296                 pool->size_class[i] = class;
2297                 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2298                                                         fullness++)
2299                         INIT_LIST_HEAD(&class->fullness_list[fullness]);
2300
2301                 prev_class = class;
2302         }
2303
2304         /* debug only, don't abort if it fails */
2305         zs_pool_stat_create(pool, name);
2306
2307         /*
2308          * Not critical since shrinker is only used to trigger internal
2309          * defragmentation of the pool which is pretty optional thing.  If
2310          * registration fails we still can use the pool normally and user can
2311          * trigger compaction manually. Thus, ignore return code.
2312          */
2313         zs_register_shrinker(pool);
2314
2315         return pool;
2316
2317 err:
2318         zs_destroy_pool(pool);
2319         return NULL;
2320 }
2321 EXPORT_SYMBOL_GPL(zs_create_pool);
2322
2323 void zs_destroy_pool(struct zs_pool *pool)
2324 {
2325         int i;
2326
2327         zs_unregister_shrinker(pool);
2328         zs_flush_migration(pool);
2329         zs_pool_stat_destroy(pool);
2330
2331         for (i = 0; i < ZS_SIZE_CLASSES; i++) {
2332                 int fg;
2333                 struct size_class *class = pool->size_class[i];
2334
2335                 if (!class)
2336                         continue;
2337
2338                 if (class->index != i)
2339                         continue;
2340
2341                 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
2342                         if (!list_empty(&class->fullness_list[fg])) {
2343                                 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
2344                                         class->size, fg);
2345                         }
2346                 }
2347                 kfree(class);
2348         }
2349
2350         destroy_cache(pool);
2351         kfree(pool->name);
2352         kfree(pool);
2353 }
2354 EXPORT_SYMBOL_GPL(zs_destroy_pool);
2355
2356 static int __init zs_init(void)
2357 {
2358         int ret;
2359
2360         ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2361                                 zs_cpu_prepare, zs_cpu_dead);
2362         if (ret)
2363                 goto out;
2364
2365 #ifdef CONFIG_ZPOOL
2366         zpool_register_driver(&zs_zpool_driver);
2367 #endif
2368
2369         zs_stat_init();
2370
2371         return 0;
2372
2373 out:
2374         return ret;
2375 }
2376
2377 static void __exit zs_exit(void)
2378 {
2379 #ifdef CONFIG_ZPOOL
2380         zpool_unregister_driver(&zs_zpool_driver);
2381 #endif
2382         cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
2383
2384         zs_stat_exit();
2385 }
2386
2387 module_init(zs_init);
2388 module_exit(zs_exit);
2389
2390 MODULE_LICENSE("Dual BSD/GPL");
2391 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");