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