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