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