]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/zsmalloc.c
zsmalloc: use freeobj for index
[mirror_ubuntu-zesty-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
MK
19 * page->private: points to zspage
20 * page->index: offset of the first object starting in this page.
21 * For the first page, this is always 0, so we use this field
22 * to store handle for huge object.
23 * page->next: links together all component pages of a zspage
2db51dae
NG
24 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
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>
61989a80
NG
36#include <linux/bitops.h>
37#include <linux/errno.h>
38#include <linux/highmem.h>
61989a80
NG
39#include <linux/string.h>
40#include <linux/slab.h>
41#include <asm/tlbflush.h>
42#include <asm/pgtable.h>
43#include <linux/cpumask.h>
44#include <linux/cpu.h>
0cbb613f 45#include <linux/vmalloc.h>
759b26b2 46#include <linux/preempt.h>
0959c63f
SJ
47#include <linux/spinlock.h>
48#include <linux/types.h>
0f050d99 49#include <linux/debugfs.h>
bcf1647d 50#include <linux/zsmalloc.h>
c795779d 51#include <linux/zpool.h>
0959c63f
SJ
52
53/*
54 * This must be power of 2 and greater than of equal to sizeof(link_free).
55 * These two conditions ensure that any 'struct link_free' itself doesn't
56 * span more than 1 page which avoids complex case of mapping 2 pages simply
57 * to restore link_free pointer values.
58 */
59#define ZS_ALIGN 8
60
61/*
62 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
63 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
64 */
65#define ZS_MAX_ZSPAGE_ORDER 2
66#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
67
2e40e163
MK
68#define ZS_HANDLE_SIZE (sizeof(unsigned long))
69
0959c63f
SJ
70/*
71 * Object location (<PFN>, <obj_idx>) is encoded as
c3e3e88a 72 * as single (unsigned long) handle value.
0959c63f 73 *
bfd093f5 74 * Note that object index <obj_idx> starts from 0.
0959c63f
SJ
75 *
76 * This is made more complicated by various memory models and PAE.
77 */
78
79#ifndef MAX_PHYSMEM_BITS
80#ifdef CONFIG_HIGHMEM64G
81#define MAX_PHYSMEM_BITS 36
82#else /* !CONFIG_HIGHMEM64G */
83/*
84 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
85 * be PAGE_SHIFT
86 */
87#define MAX_PHYSMEM_BITS BITS_PER_LONG
88#endif
89#endif
90#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
312fcae2
MK
91
92/*
93 * Memory for allocating for handle keeps object position by
94 * encoding <page, obj_idx> and the encoded value has a room
95 * in least bit(ie, look at obj_to_location).
96 * We use the bit to synchronize between object access by
97 * user and migration.
98 */
99#define HANDLE_PIN_BIT 0
100
101/*
102 * Head in allocated object should have OBJ_ALLOCATED_TAG
103 * to identify the object was allocated or not.
104 * It's okay to add the status bit in the least bit because
105 * header keeps handle which is 4byte-aligned address so we
106 * have room for two bit at least.
107 */
108#define OBJ_ALLOCATED_TAG 1
109#define OBJ_TAG_BITS 1
110#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
0959c63f
SJ
111#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
112
113#define MAX(a, b) ((a) >= (b) ? (a) : (b))
114/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
115#define ZS_MIN_ALLOC_SIZE \
116 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
2e40e163 117/* each chunk includes extra space to keep handle */
7b60a685 118#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
0959c63f
SJ
119
120/*
7eb52512 121 * On systems with 4K page size, this gives 255 size classes! There is a
0959c63f
SJ
122 * trader-off here:
123 * - Large number of size classes is potentially wasteful as free page are
124 * spread across these classes
125 * - Small number of size classes causes large internal fragmentation
126 * - Probably its better to use specific size classes (empirically
127 * determined). NOTE: all those class sizes must be set as multiple of
128 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
129 *
130 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
131 * (reason above)
132 */
3783689a 133#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
0959c63f
SJ
134
135/*
136 * We do not maintain any list for completely empty or full pages
137 */
138enum fullness_group {
139 ZS_ALMOST_FULL,
140 ZS_ALMOST_EMPTY,
0959c63f
SJ
141 ZS_EMPTY,
142 ZS_FULL
143};
144
0f050d99
GM
145enum zs_stat_type {
146 OBJ_ALLOCATED,
147 OBJ_USED,
248ca1b0
MK
148 CLASS_ALMOST_FULL,
149 CLASS_ALMOST_EMPTY,
0f050d99
GM
150};
151
6fe5186f
SS
152#ifdef CONFIG_ZSMALLOC_STAT
153#define NR_ZS_STAT_TYPE (CLASS_ALMOST_EMPTY + 1)
154#else
155#define NR_ZS_STAT_TYPE (OBJ_USED + 1)
156#endif
157
0f050d99
GM
158struct zs_size_stat {
159 unsigned long objs[NR_ZS_STAT_TYPE];
160};
161
57244594
SS
162#ifdef CONFIG_ZSMALLOC_STAT
163static struct dentry *zs_stat_root;
0f050d99
GM
164#endif
165
40f9fb8c
MG
166/*
167 * number of size_classes
168 */
169static int zs_size_classes;
170
0959c63f
SJ
171/*
172 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
173 * n <= N / f, where
174 * n = number of allocated objects
175 * N = total number of objects zspage can store
6dd9737e 176 * f = fullness_threshold_frac
0959c63f
SJ
177 *
178 * Similarly, we assign zspage to:
179 * ZS_ALMOST_FULL when n > N / f
180 * ZS_EMPTY when n == 0
181 * ZS_FULL when n == N
182 *
183 * (see: fix_fullness_group())
184 */
185static const int fullness_threshold_frac = 4;
186
187struct size_class {
57244594 188 spinlock_t lock;
3783689a 189 struct list_head fullness_list[2];
0959c63f
SJ
190 /*
191 * Size of objects stored in this class. Must be multiple
192 * of ZS_ALIGN.
193 */
194 int size;
1fc6e27d 195 int objs_per_zspage;
0959c63f
SJ
196 unsigned int index;
197
0f050d99 198 struct zs_size_stat stats;
0959c63f 199
7dfa4612
WY
200 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
201 int pages_per_zspage;
57244594
SS
202 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
203 bool huge;
0959c63f
SJ
204};
205
206/*
207 * Placed within free objects to form a singly linked list.
3783689a 208 * For every zspage, zspage->freeobj gives head of this list.
0959c63f
SJ
209 *
210 * This must be power of 2 and less than or equal to ZS_ALIGN
211 */
212struct link_free {
2e40e163
MK
213 union {
214 /*
bfd093f5 215 * Free object index;
2e40e163
MK
216 * It's valid for non-allocated object
217 */
bfd093f5 218 unsigned long next;
2e40e163
MK
219 /*
220 * Handle of allocated object.
221 */
222 unsigned long handle;
223 };
0959c63f
SJ
224};
225
226struct zs_pool {
6f3526d6 227 const char *name;
0f050d99 228
40f9fb8c 229 struct size_class **size_class;
2e40e163 230 struct kmem_cache *handle_cachep;
3783689a 231 struct kmem_cache *zspage_cachep;
0959c63f 232
13de8933 233 atomic_long_t pages_allocated;
0f050d99 234
7d3f3938 235 struct zs_pool_stats stats;
ab9d306d
SS
236
237 /* Compact classes */
238 struct shrinker shrinker;
239 /*
240 * To signify that register_shrinker() was successful
241 * and unregister_shrinker() will not Oops.
242 */
243 bool shrinker_enabled;
0f050d99
GM
244#ifdef CONFIG_ZSMALLOC_STAT
245 struct dentry *stat_dentry;
246#endif
0959c63f 247};
61989a80
NG
248
249/*
250 * A zspage's class index and fullness group
251 * are encoded in its (first)page->mapping
252 */
3783689a
MK
253#define FULLNESS_BITS 2
254#define CLASS_BITS 8
4f42047b 255
3783689a
MK
256struct zspage {
257 struct {
258 unsigned int fullness:FULLNESS_BITS;
259 unsigned int class:CLASS_BITS;
260 };
261 unsigned int inuse;
bfd093f5 262 unsigned int freeobj;
3783689a
MK
263 struct page *first_page;
264 struct list_head list; /* fullness list */
265};
61989a80 266
f553646a 267struct mapping_area {
1b945aee 268#ifdef CONFIG_PGTABLE_MAPPING
f553646a
SJ
269 struct vm_struct *vm; /* vm area for mapping object that span pages */
270#else
271 char *vm_buf; /* copy buffer for objects that span pages */
272#endif
273 char *vm_addr; /* address of kmap_atomic()'ed pages */
274 enum zs_mapmode vm_mm; /* mapping mode */
275};
276
3783689a 277static int create_cache(struct zs_pool *pool)
2e40e163
MK
278{
279 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
280 0, 0, NULL);
3783689a
MK
281 if (!pool->handle_cachep)
282 return 1;
283
284 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
285 0, 0, NULL);
286 if (!pool->zspage_cachep) {
287 kmem_cache_destroy(pool->handle_cachep);
288 pool->handle_cachep = NULL;
289 return 1;
290 }
291
292 return 0;
2e40e163
MK
293}
294
3783689a 295static void destroy_cache(struct zs_pool *pool)
2e40e163 296{
cd10add0 297 kmem_cache_destroy(pool->handle_cachep);
3783689a 298 kmem_cache_destroy(pool->zspage_cachep);
2e40e163
MK
299}
300
3783689a 301static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
2e40e163
MK
302{
303 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
d0d8da2d 304 gfp & ~__GFP_HIGHMEM);
2e40e163
MK
305}
306
3783689a 307static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
2e40e163
MK
308{
309 kmem_cache_free(pool->handle_cachep, (void *)handle);
310}
311
3783689a
MK
312static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
313{
314 return kmem_cache_alloc(pool->zspage_cachep, flags & ~__GFP_HIGHMEM);
315};
316
317static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
318{
319 kmem_cache_free(pool->zspage_cachep, zspage);
320}
321
2e40e163
MK
322static void record_obj(unsigned long handle, unsigned long obj)
323{
c102f07c
JL
324 /*
325 * lsb of @obj represents handle lock while other bits
326 * represent object value the handle is pointing so
327 * updating shouldn't do store tearing.
328 */
329 WRITE_ONCE(*(unsigned long *)handle, obj);
2e40e163
MK
330}
331
c795779d
DS
332/* zpool driver */
333
334#ifdef CONFIG_ZPOOL
335
6f3526d6 336static void *zs_zpool_create(const char *name, gfp_t gfp,
78672779 337 const struct zpool_ops *zpool_ops,
479305fd 338 struct zpool *zpool)
c795779d 339{
d0d8da2d
SS
340 /*
341 * Ignore global gfp flags: zs_malloc() may be invoked from
342 * different contexts and its caller must provide a valid
343 * gfp mask.
344 */
345 return zs_create_pool(name);
c795779d
DS
346}
347
348static void zs_zpool_destroy(void *pool)
349{
350 zs_destroy_pool(pool);
351}
352
353static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
354 unsigned long *handle)
355{
d0d8da2d 356 *handle = zs_malloc(pool, size, gfp);
c795779d
DS
357 return *handle ? 0 : -1;
358}
359static void zs_zpool_free(void *pool, unsigned long handle)
360{
361 zs_free(pool, handle);
362}
363
364static int zs_zpool_shrink(void *pool, unsigned int pages,
365 unsigned int *reclaimed)
366{
367 return -EINVAL;
368}
369
370static void *zs_zpool_map(void *pool, unsigned long handle,
371 enum zpool_mapmode mm)
372{
373 enum zs_mapmode zs_mm;
374
375 switch (mm) {
376 case ZPOOL_MM_RO:
377 zs_mm = ZS_MM_RO;
378 break;
379 case ZPOOL_MM_WO:
380 zs_mm = ZS_MM_WO;
381 break;
382 case ZPOOL_MM_RW: /* fallthru */
383 default:
384 zs_mm = ZS_MM_RW;
385 break;
386 }
387
388 return zs_map_object(pool, handle, zs_mm);
389}
390static void zs_zpool_unmap(void *pool, unsigned long handle)
391{
392 zs_unmap_object(pool, handle);
393}
394
395static u64 zs_zpool_total_size(void *pool)
396{
722cdc17 397 return zs_get_total_pages(pool) << PAGE_SHIFT;
c795779d
DS
398}
399
400static struct zpool_driver zs_zpool_driver = {
401 .type = "zsmalloc",
402 .owner = THIS_MODULE,
403 .create = zs_zpool_create,
404 .destroy = zs_zpool_destroy,
405 .malloc = zs_zpool_malloc,
406 .free = zs_zpool_free,
407 .shrink = zs_zpool_shrink,
408 .map = zs_zpool_map,
409 .unmap = zs_zpool_unmap,
410 .total_size = zs_zpool_total_size,
411};
412
137f8cff 413MODULE_ALIAS("zpool-zsmalloc");
c795779d
DS
414#endif /* CONFIG_ZPOOL */
415
248ca1b0
MK
416static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
417{
418 return pages_per_zspage * PAGE_SIZE / size;
419}
420
61989a80
NG
421/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
422static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
423
424static int is_first_page(struct page *page)
425{
a27545bf 426 return PagePrivate(page);
61989a80
NG
427}
428
3783689a 429static inline int get_zspage_inuse(struct zspage *zspage)
4f42047b 430{
3783689a 431 return zspage->inuse;
4f42047b
MK
432}
433
3783689a 434static inline void set_zspage_inuse(struct zspage *zspage, int val)
4f42047b 435{
3783689a 436 zspage->inuse = val;
4f42047b
MK
437}
438
3783689a 439static inline void mod_zspage_inuse(struct zspage *zspage, int val)
4f42047b 440{
3783689a 441 zspage->inuse += val;
4f42047b
MK
442}
443
444static inline int get_first_obj_offset(struct page *page)
445{
3783689a
MK
446 if (is_first_page(page))
447 return 0;
448
4f42047b
MK
449 return page->index;
450}
451
452static inline void set_first_obj_offset(struct page *page, int offset)
453{
3783689a
MK
454 if (is_first_page(page))
455 return;
456
4f42047b
MK
457 page->index = offset;
458}
459
bfd093f5 460static inline unsigned int get_freeobj(struct zspage *zspage)
4f42047b 461{
bfd093f5 462 return zspage->freeobj;
4f42047b
MK
463}
464
bfd093f5 465static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
4f42047b 466{
bfd093f5 467 zspage->freeobj = obj;
4f42047b
MK
468}
469
3783689a 470static void get_zspage_mapping(struct zspage *zspage,
a4209467 471 unsigned int *class_idx,
61989a80
NG
472 enum fullness_group *fullness)
473{
3783689a
MK
474 *fullness = zspage->fullness;
475 *class_idx = zspage->class;
61989a80
NG
476}
477
3783689a 478static void set_zspage_mapping(struct zspage *zspage,
a4209467 479 unsigned int class_idx,
61989a80
NG
480 enum fullness_group fullness)
481{
3783689a
MK
482 zspage->class = class_idx;
483 zspage->fullness = fullness;
61989a80
NG
484}
485
c3e3e88a
NC
486/*
487 * zsmalloc divides the pool into various size classes where each
488 * class maintains a list of zspages where each zspage is divided
489 * into equal sized chunks. Each allocation falls into one of these
490 * classes depending on its size. This function returns index of the
491 * size class which has chunk size big enough to hold the give size.
492 */
61989a80
NG
493static int get_size_class_index(int size)
494{
495 int idx = 0;
496
497 if (likely(size > ZS_MIN_ALLOC_SIZE))
498 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
499 ZS_SIZE_CLASS_DELTA);
500
7b60a685 501 return min(zs_size_classes - 1, idx);
61989a80
NG
502}
503
248ca1b0
MK
504static inline void zs_stat_inc(struct size_class *class,
505 enum zs_stat_type type, unsigned long cnt)
506{
6fe5186f
SS
507 if (type < NR_ZS_STAT_TYPE)
508 class->stats.objs[type] += cnt;
248ca1b0
MK
509}
510
511static inline void zs_stat_dec(struct size_class *class,
512 enum zs_stat_type type, unsigned long cnt)
513{
6fe5186f
SS
514 if (type < NR_ZS_STAT_TYPE)
515 class->stats.objs[type] -= cnt;
248ca1b0
MK
516}
517
518static inline unsigned long zs_stat_get(struct size_class *class,
519 enum zs_stat_type type)
520{
6fe5186f
SS
521 if (type < NR_ZS_STAT_TYPE)
522 return class->stats.objs[type];
523 return 0;
248ca1b0
MK
524}
525
57244594
SS
526#ifdef CONFIG_ZSMALLOC_STAT
527
4abaac9b 528static void __init zs_stat_init(void)
248ca1b0 529{
4abaac9b
DS
530 if (!debugfs_initialized()) {
531 pr_warn("debugfs not available, stat dir not created\n");
532 return;
533 }
248ca1b0
MK
534
535 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
536 if (!zs_stat_root)
4abaac9b 537 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
248ca1b0
MK
538}
539
540static void __exit zs_stat_exit(void)
541{
542 debugfs_remove_recursive(zs_stat_root);
543}
544
1120ed54
SS
545static unsigned long zs_can_compact(struct size_class *class);
546
248ca1b0
MK
547static int zs_stats_size_show(struct seq_file *s, void *v)
548{
549 int i;
550 struct zs_pool *pool = s->private;
551 struct size_class *class;
552 int objs_per_zspage;
553 unsigned long class_almost_full, class_almost_empty;
1120ed54 554 unsigned long obj_allocated, obj_used, pages_used, freeable;
248ca1b0
MK
555 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
556 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
1120ed54 557 unsigned long total_freeable = 0;
248ca1b0 558
1120ed54 559 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
248ca1b0
MK
560 "class", "size", "almost_full", "almost_empty",
561 "obj_allocated", "obj_used", "pages_used",
1120ed54 562 "pages_per_zspage", "freeable");
248ca1b0
MK
563
564 for (i = 0; i < zs_size_classes; i++) {
565 class = pool->size_class[i];
566
567 if (class->index != i)
568 continue;
569
570 spin_lock(&class->lock);
571 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
572 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
573 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
574 obj_used = zs_stat_get(class, OBJ_USED);
1120ed54 575 freeable = zs_can_compact(class);
248ca1b0
MK
576 spin_unlock(&class->lock);
577
578 objs_per_zspage = get_maxobj_per_zspage(class->size,
579 class->pages_per_zspage);
580 pages_used = obj_allocated / objs_per_zspage *
581 class->pages_per_zspage;
582
1120ed54
SS
583 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
584 " %10lu %10lu %16d %8lu\n",
248ca1b0
MK
585 i, class->size, class_almost_full, class_almost_empty,
586 obj_allocated, obj_used, pages_used,
1120ed54 587 class->pages_per_zspage, freeable);
248ca1b0
MK
588
589 total_class_almost_full += class_almost_full;
590 total_class_almost_empty += class_almost_empty;
591 total_objs += obj_allocated;
592 total_used_objs += obj_used;
593 total_pages += pages_used;
1120ed54 594 total_freeable += freeable;
248ca1b0
MK
595 }
596
597 seq_puts(s, "\n");
1120ed54 598 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
248ca1b0
MK
599 "Total", "", total_class_almost_full,
600 total_class_almost_empty, total_objs,
1120ed54 601 total_used_objs, total_pages, "", total_freeable);
248ca1b0
MK
602
603 return 0;
604}
605
606static int zs_stats_size_open(struct inode *inode, struct file *file)
607{
608 return single_open(file, zs_stats_size_show, inode->i_private);
609}
610
611static const struct file_operations zs_stat_size_ops = {
612 .open = zs_stats_size_open,
613 .read = seq_read,
614 .llseek = seq_lseek,
615 .release = single_release,
616};
617
d34f6157 618static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
248ca1b0
MK
619{
620 struct dentry *entry;
621
4abaac9b
DS
622 if (!zs_stat_root) {
623 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
d34f6157 624 return;
4abaac9b 625 }
248ca1b0
MK
626
627 entry = debugfs_create_dir(name, zs_stat_root);
628 if (!entry) {
629 pr_warn("debugfs dir <%s> creation failed\n", name);
d34f6157 630 return;
248ca1b0
MK
631 }
632 pool->stat_dentry = entry;
633
634 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
635 pool->stat_dentry, pool, &zs_stat_size_ops);
636 if (!entry) {
637 pr_warn("%s: debugfs file entry <%s> creation failed\n",
638 name, "classes");
4abaac9b
DS
639 debugfs_remove_recursive(pool->stat_dentry);
640 pool->stat_dentry = NULL;
248ca1b0 641 }
248ca1b0
MK
642}
643
644static void zs_pool_stat_destroy(struct zs_pool *pool)
645{
646 debugfs_remove_recursive(pool->stat_dentry);
647}
648
649#else /* CONFIG_ZSMALLOC_STAT */
4abaac9b 650static void __init zs_stat_init(void)
248ca1b0 651{
248ca1b0
MK
652}
653
654static void __exit zs_stat_exit(void)
655{
656}
657
d34f6157 658static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
248ca1b0 659{
248ca1b0
MK
660}
661
662static inline void zs_pool_stat_destroy(struct zs_pool *pool)
663{
664}
248ca1b0
MK
665#endif
666
c3e3e88a
NC
667/*
668 * For each size class, zspages are divided into different groups
669 * depending on how "full" they are. This was done so that we could
670 * easily find empty or nearly empty zspages when we try to shrink
671 * the pool (not yet implemented). This function returns fullness
672 * status of the given page.
673 */
1fc6e27d 674static enum fullness_group get_fullness_group(struct size_class *class,
3783689a 675 struct zspage *zspage)
61989a80 676{
1fc6e27d 677 int inuse, objs_per_zspage;
61989a80 678 enum fullness_group fg;
830e4bc5 679
3783689a 680 inuse = get_zspage_inuse(zspage);
1fc6e27d 681 objs_per_zspage = class->objs_per_zspage;
61989a80
NG
682
683 if (inuse == 0)
684 fg = ZS_EMPTY;
1fc6e27d 685 else if (inuse == objs_per_zspage)
61989a80 686 fg = ZS_FULL;
1fc6e27d 687 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
61989a80
NG
688 fg = ZS_ALMOST_EMPTY;
689 else
690 fg = ZS_ALMOST_FULL;
691
692 return fg;
693}
694
c3e3e88a
NC
695/*
696 * Each size class maintains various freelists and zspages are assigned
697 * to one of these freelists based on the number of live objects they
698 * have. This functions inserts the given zspage into the freelist
699 * identified by <class, fullness_group>.
700 */
251cbb95 701static void insert_zspage(struct size_class *class,
3783689a
MK
702 struct zspage *zspage,
703 enum fullness_group fullness)
61989a80 704{
3783689a 705 struct zspage *head;
61989a80 706
3783689a 707 if (fullness >= ZS_EMPTY)
61989a80
NG
708 return;
709
3783689a
MK
710 head = list_first_entry_or_null(&class->fullness_list[fullness],
711 struct zspage, list);
712
248ca1b0
MK
713 zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
714 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
58f17117 715
58f17117 716 /*
3783689a
MK
717 * We want to see more ZS_FULL pages and less almost empty/full.
718 * Put pages with higher ->inuse first.
58f17117 719 */
3783689a
MK
720 if (head) {
721 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
722 list_add(&zspage->list, &head->list);
723 return;
724 }
725 }
726 list_add(&zspage->list, &class->fullness_list[fullness]);
61989a80
NG
727}
728
c3e3e88a
NC
729/*
730 * This function removes the given zspage from the freelist identified
731 * by <class, fullness_group>.
732 */
251cbb95 733static void remove_zspage(struct size_class *class,
3783689a
MK
734 struct zspage *zspage,
735 enum fullness_group fullness)
61989a80 736{
3783689a 737 if (fullness >= ZS_EMPTY)
61989a80
NG
738 return;
739
3783689a 740 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
61989a80 741
3783689a 742 list_del_init(&zspage->list);
248ca1b0
MK
743 zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
744 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
61989a80
NG
745}
746
c3e3e88a
NC
747/*
748 * Each size class maintains zspages in different fullness groups depending
749 * on the number of live objects they contain. When allocating or freeing
750 * objects, the fullness status of the page can change, say, from ALMOST_FULL
751 * to ALMOST_EMPTY when freeing an object. This function checks if such
752 * a status change has occurred for the given page and accordingly moves the
753 * page from the freelist of the old fullness group to that of the new
754 * fullness group.
755 */
c7806261 756static enum fullness_group fix_fullness_group(struct size_class *class,
3783689a 757 struct zspage *zspage)
61989a80
NG
758{
759 int class_idx;
61989a80
NG
760 enum fullness_group currfg, newfg;
761
3783689a
MK
762 get_zspage_mapping(zspage, &class_idx, &currfg);
763 newfg = get_fullness_group(class, zspage);
61989a80
NG
764 if (newfg == currfg)
765 goto out;
766
3783689a
MK
767 remove_zspage(class, zspage, currfg);
768 insert_zspage(class, zspage, newfg);
769 set_zspage_mapping(zspage, class_idx, newfg);
61989a80
NG
770
771out:
772 return newfg;
773}
774
775/*
776 * We have to decide on how many pages to link together
777 * to form a zspage for each size class. This is important
778 * to reduce wastage due to unusable space left at end of
779 * each zspage which is given as:
888fa374
YX
780 * wastage = Zp % class_size
781 * usage = Zp - wastage
61989a80
NG
782 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
783 *
784 * For example, for size class of 3/8 * PAGE_SIZE, we should
785 * link together 3 PAGE_SIZE sized pages to form a zspage
786 * since then we can perfectly fit in 8 such objects.
787 */
2e3b6154 788static int get_pages_per_zspage(int class_size)
61989a80
NG
789{
790 int i, max_usedpc = 0;
791 /* zspage order which gives maximum used size per KB */
792 int max_usedpc_order = 1;
793
84d4faab 794 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
61989a80
NG
795 int zspage_size;
796 int waste, usedpc;
797
798 zspage_size = i * PAGE_SIZE;
799 waste = zspage_size % class_size;
800 usedpc = (zspage_size - waste) * 100 / zspage_size;
801
802 if (usedpc > max_usedpc) {
803 max_usedpc = usedpc;
804 max_usedpc_order = i;
805 }
806 }
807
808 return max_usedpc_order;
809}
810
bfd093f5
MK
811static struct page *get_first_page(struct zspage *zspage)
812{
813 return zspage->first_page;
814}
3783689a
MK
815
816static struct zspage *get_zspage(struct page *page)
61989a80 817{
3783689a 818 return (struct zspage *)page->private;
61989a80
NG
819}
820
821static struct page *get_next_page(struct page *page)
822{
3783689a 823 return page->next;
61989a80
NG
824}
825
bfd093f5
MK
826/**
827 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
828 * @page: page object resides in zspage
829 * @obj_idx: object index
67296874 830 */
bfd093f5
MK
831static void obj_to_location(unsigned long obj, struct page **page,
832 unsigned int *obj_idx)
61989a80 833{
bfd093f5
MK
834 obj >>= OBJ_TAG_BITS;
835 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
836 *obj_idx = (obj & OBJ_INDEX_MASK);
837}
61989a80 838
bfd093f5
MK
839/**
840 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
841 * @page: page object resides in zspage
842 * @obj_idx: object index
843 */
844static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
845{
846 unsigned long obj;
61989a80 847
312fcae2 848 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
bfd093f5 849 obj |= obj_idx & OBJ_INDEX_MASK;
312fcae2 850 obj <<= OBJ_TAG_BITS;
61989a80 851
bfd093f5 852 return obj;
61989a80
NG
853}
854
2e40e163
MK
855static unsigned long handle_to_obj(unsigned long handle)
856{
857 return *(unsigned long *)handle;
858}
859
7b60a685
MK
860static unsigned long obj_to_head(struct size_class *class, struct page *page,
861 void *obj)
312fcae2 862{
7b60a685 863 if (class->huge) {
830e4bc5 864 VM_BUG_ON_PAGE(!is_first_page(page), page);
3783689a 865 return page->index;
7b60a685
MK
866 } else
867 return *(unsigned long *)obj;
312fcae2
MK
868}
869
312fcae2
MK
870static inline int trypin_tag(unsigned long handle)
871{
1b8320b6 872 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
312fcae2
MK
873}
874
875static void pin_tag(unsigned long handle)
876{
1b8320b6 877 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
312fcae2
MK
878}
879
880static void unpin_tag(unsigned long handle)
881{
1b8320b6 882 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
312fcae2
MK
883}
884
f4477e90
NG
885static void reset_page(struct page *page)
886{
887 clear_bit(PG_private, &page->flags);
888 clear_bit(PG_private_2, &page->flags);
889 set_page_private(page, 0);
3783689a 890 page->index = 0;
f4477e90
NG
891}
892
3783689a 893static void free_zspage(struct zs_pool *pool, struct zspage *zspage)
61989a80 894{
3783689a 895 struct page *page, *next;
61989a80 896
3783689a 897 VM_BUG_ON(get_zspage_inuse(zspage));
61989a80 898
3783689a
MK
899 next = page = zspage->first_page;
900 do {
901 next = page->next;
902 reset_page(page);
903 put_page(page);
904 page = next;
905 } while (page != NULL);
61989a80 906
3783689a 907 cache_free_zspage(pool, zspage);
61989a80
NG
908}
909
910/* Initialize a newly allocated zspage */
3783689a 911static void init_zspage(struct size_class *class, struct zspage *zspage)
61989a80 912{
bfd093f5 913 unsigned int freeobj = 1;
61989a80 914 unsigned long off = 0;
3783689a 915 struct page *page = zspage->first_page;
830e4bc5 916
61989a80
NG
917 while (page) {
918 struct page *next_page;
919 struct link_free *link;
af4ee5e9 920 void *vaddr;
61989a80 921
3783689a 922 set_first_obj_offset(page, off);
61989a80 923
af4ee5e9
MK
924 vaddr = kmap_atomic(page);
925 link = (struct link_free *)vaddr + off / sizeof(*link);
5538c562
DS
926
927 while ((off += class->size) < PAGE_SIZE) {
bfd093f5 928 link->next = freeobj++ << OBJ_ALLOCATED_TAG;
5538c562 929 link += class->size / sizeof(*link);
61989a80
NG
930 }
931
932 /*
933 * We now come to the last (full or partial) object on this
934 * page, which must point to the first object on the next
935 * page (if present)
936 */
937 next_page = get_next_page(page);
bfd093f5
MK
938 if (next_page) {
939 link->next = freeobj++ << OBJ_ALLOCATED_TAG;
940 } else {
941 /*
942 * Reset OBJ_ALLOCATED_TAG bit to last link to tell
943 * whether it's allocated object or not.
944 */
945 link->next = -1 << OBJ_ALLOCATED_TAG;
946 }
af4ee5e9 947 kunmap_atomic(vaddr);
61989a80 948 page = next_page;
5538c562 949 off %= PAGE_SIZE;
61989a80 950 }
bdb0af7c 951
bfd093f5 952 set_freeobj(zspage, 0);
61989a80
NG
953}
954
3783689a
MK
955static void create_page_chain(struct zspage *zspage, struct page *pages[],
956 int nr_pages)
61989a80 957{
bdb0af7c
MK
958 int i;
959 struct page *page;
960 struct page *prev_page = NULL;
61989a80
NG
961
962 /*
963 * Allocate individual pages and link them together as:
3783689a
MK
964 * 1. all pages are linked together using page->next
965 * 2. each sub-page point to zspage using page->private
61989a80 966 *
3783689a
MK
967 * we set PG_private to identify the first page (i.e. no other sub-page
968 * has this flag set) and PG_private_2 to identify the last page.
61989a80 969 */
bdb0af7c
MK
970 for (i = 0; i < nr_pages; i++) {
971 page = pages[i];
3783689a 972 set_page_private(page, (unsigned long)zspage);
bdb0af7c 973 if (i == 0) {
3783689a 974 zspage->first_page = page;
a27545bf 975 SetPagePrivate(page);
3783689a
MK
976 } else {
977 prev_page->next = page;
61989a80 978 }
3783689a 979 if (i == nr_pages - 1) {
a27545bf 980 SetPagePrivate2(page);
3783689a
MK
981 page->next = NULL;
982 }
61989a80
NG
983 prev_page = page;
984 }
bdb0af7c 985}
61989a80 986
bdb0af7c
MK
987/*
988 * Allocate a zspage for the given size class
989 */
3783689a
MK
990static struct zspage *alloc_zspage(struct zs_pool *pool,
991 struct size_class *class,
992 gfp_t gfp)
bdb0af7c
MK
993{
994 int i;
bdb0af7c 995 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
3783689a
MK
996 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
997
998 if (!zspage)
999 return NULL;
1000
1001 memset(zspage, 0, sizeof(struct zspage));
61989a80 1002
bdb0af7c
MK
1003 for (i = 0; i < class->pages_per_zspage; i++) {
1004 struct page *page;
61989a80 1005
3783689a 1006 page = alloc_page(gfp);
bdb0af7c
MK
1007 if (!page) {
1008 while (--i >= 0)
1009 __free_page(pages[i]);
3783689a 1010 cache_free_zspage(pool, zspage);
bdb0af7c
MK
1011 return NULL;
1012 }
1013 pages[i] = page;
61989a80
NG
1014 }
1015
3783689a
MK
1016 create_page_chain(zspage, pages, class->pages_per_zspage);
1017 init_zspage(class, zspage);
bdb0af7c 1018
3783689a 1019 return zspage;
61989a80
NG
1020}
1021
3783689a 1022static struct zspage *find_get_zspage(struct size_class *class)
61989a80
NG
1023{
1024 int i;
3783689a 1025 struct zspage *zspage;
61989a80 1026
3783689a
MK
1027 for (i = ZS_ALMOST_FULL; i <= ZS_ALMOST_EMPTY; i++) {
1028 zspage = list_first_entry_or_null(&class->fullness_list[i],
1029 struct zspage, list);
1030 if (zspage)
61989a80
NG
1031 break;
1032 }
1033
3783689a 1034 return zspage;
61989a80
NG
1035}
1036
1b945aee 1037#ifdef CONFIG_PGTABLE_MAPPING
f553646a
SJ
1038static inline int __zs_cpu_up(struct mapping_area *area)
1039{
1040 /*
1041 * Make sure we don't leak memory if a cpu UP notification
1042 * and zs_init() race and both call zs_cpu_up() on the same cpu
1043 */
1044 if (area->vm)
1045 return 0;
1046 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1047 if (!area->vm)
1048 return -ENOMEM;
1049 return 0;
1050}
1051
1052static inline void __zs_cpu_down(struct mapping_area *area)
1053{
1054 if (area->vm)
1055 free_vm_area(area->vm);
1056 area->vm = NULL;
1057}
1058
1059static inline void *__zs_map_object(struct mapping_area *area,
1060 struct page *pages[2], int off, int size)
1061{
f6f8ed47 1062 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
f553646a
SJ
1063 area->vm_addr = area->vm->addr;
1064 return area->vm_addr + off;
1065}
1066
1067static inline void __zs_unmap_object(struct mapping_area *area,
1068 struct page *pages[2], int off, int size)
1069{
1070 unsigned long addr = (unsigned long)area->vm_addr;
f553646a 1071
d95abbbb 1072 unmap_kernel_range(addr, PAGE_SIZE * 2);
f553646a
SJ
1073}
1074
1b945aee 1075#else /* CONFIG_PGTABLE_MAPPING */
f553646a
SJ
1076
1077static inline int __zs_cpu_up(struct mapping_area *area)
1078{
1079 /*
1080 * Make sure we don't leak memory if a cpu UP notification
1081 * and zs_init() race and both call zs_cpu_up() on the same cpu
1082 */
1083 if (area->vm_buf)
1084 return 0;
40f9fb8c 1085 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
f553646a
SJ
1086 if (!area->vm_buf)
1087 return -ENOMEM;
1088 return 0;
1089}
1090
1091static inline void __zs_cpu_down(struct mapping_area *area)
1092{
40f9fb8c 1093 kfree(area->vm_buf);
f553646a
SJ
1094 area->vm_buf = NULL;
1095}
1096
1097static void *__zs_map_object(struct mapping_area *area,
1098 struct page *pages[2], int off, int size)
5f601902 1099{
5f601902
SJ
1100 int sizes[2];
1101 void *addr;
f553646a 1102 char *buf = area->vm_buf;
5f601902 1103
f553646a
SJ
1104 /* disable page faults to match kmap_atomic() return conditions */
1105 pagefault_disable();
1106
1107 /* no read fastpath */
1108 if (area->vm_mm == ZS_MM_WO)
1109 goto out;
5f601902
SJ
1110
1111 sizes[0] = PAGE_SIZE - off;
1112 sizes[1] = size - sizes[0];
1113
5f601902
SJ
1114 /* copy object to per-cpu buffer */
1115 addr = kmap_atomic(pages[0]);
1116 memcpy(buf, addr + off, sizes[0]);
1117 kunmap_atomic(addr);
1118 addr = kmap_atomic(pages[1]);
1119 memcpy(buf + sizes[0], addr, sizes[1]);
1120 kunmap_atomic(addr);
f553646a
SJ
1121out:
1122 return area->vm_buf;
5f601902
SJ
1123}
1124
f553646a
SJ
1125static void __zs_unmap_object(struct mapping_area *area,
1126 struct page *pages[2], int off, int size)
5f601902 1127{
5f601902
SJ
1128 int sizes[2];
1129 void *addr;
2e40e163 1130 char *buf;
5f601902 1131
f553646a
SJ
1132 /* no write fastpath */
1133 if (area->vm_mm == ZS_MM_RO)
1134 goto out;
5f601902 1135
7b60a685 1136 buf = area->vm_buf;
a82cbf07
YX
1137 buf = buf + ZS_HANDLE_SIZE;
1138 size -= ZS_HANDLE_SIZE;
1139 off += ZS_HANDLE_SIZE;
2e40e163 1140
5f601902
SJ
1141 sizes[0] = PAGE_SIZE - off;
1142 sizes[1] = size - sizes[0];
1143
1144 /* copy per-cpu buffer to object */
1145 addr = kmap_atomic(pages[0]);
1146 memcpy(addr + off, buf, sizes[0]);
1147 kunmap_atomic(addr);
1148 addr = kmap_atomic(pages[1]);
1149 memcpy(addr, buf + sizes[0], sizes[1]);
1150 kunmap_atomic(addr);
f553646a
SJ
1151
1152out:
1153 /* enable page faults to match kunmap_atomic() return conditions */
1154 pagefault_enable();
5f601902 1155}
61989a80 1156
1b945aee 1157#endif /* CONFIG_PGTABLE_MAPPING */
f553646a 1158
61989a80
NG
1159static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1160 void *pcpu)
1161{
f553646a 1162 int ret, cpu = (long)pcpu;
61989a80
NG
1163 struct mapping_area *area;
1164
1165 switch (action) {
1166 case CPU_UP_PREPARE:
1167 area = &per_cpu(zs_map_area, cpu);
f553646a
SJ
1168 ret = __zs_cpu_up(area);
1169 if (ret)
1170 return notifier_from_errno(ret);
61989a80
NG
1171 break;
1172 case CPU_DEAD:
1173 case CPU_UP_CANCELED:
1174 area = &per_cpu(zs_map_area, cpu);
f553646a 1175 __zs_cpu_down(area);
61989a80
NG
1176 break;
1177 }
1178
1179 return NOTIFY_OK;
1180}
1181
1182static struct notifier_block zs_cpu_nb = {
1183 .notifier_call = zs_cpu_notifier
1184};
1185
b1b00a5b 1186static int zs_register_cpu_notifier(void)
61989a80 1187{
b1b00a5b 1188 int cpu, uninitialized_var(ret);
61989a80 1189
f0e71fcd
SB
1190 cpu_notifier_register_begin();
1191
1192 __register_cpu_notifier(&zs_cpu_nb);
61989a80
NG
1193 for_each_online_cpu(cpu) {
1194 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
b1b00a5b
SS
1195 if (notifier_to_errno(ret))
1196 break;
61989a80 1197 }
f0e71fcd
SB
1198
1199 cpu_notifier_register_done();
b1b00a5b
SS
1200 return notifier_to_errno(ret);
1201}
f0e71fcd 1202
66cdef66 1203static void zs_unregister_cpu_notifier(void)
40f9fb8c 1204{
66cdef66 1205 int cpu;
40f9fb8c 1206
66cdef66 1207 cpu_notifier_register_begin();
40f9fb8c 1208
66cdef66
GM
1209 for_each_online_cpu(cpu)
1210 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1211 __unregister_cpu_notifier(&zs_cpu_nb);
40f9fb8c 1212
66cdef66 1213 cpu_notifier_register_done();
b1b00a5b
SS
1214}
1215
66cdef66 1216static void init_zs_size_classes(void)
b1b00a5b 1217{
66cdef66 1218 int nr;
c795779d 1219
66cdef66
GM
1220 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1221 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1222 nr += 1;
40f9fb8c 1223
66cdef66 1224 zs_size_classes = nr;
61989a80
NG
1225}
1226
9eec4cd5
JK
1227static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
1228{
1229 if (prev->pages_per_zspage != pages_per_zspage)
1230 return false;
1231
1232 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
1233 != get_maxobj_per_zspage(size, pages_per_zspage))
1234 return false;
1235
1236 return true;
1237}
1238
3783689a 1239static bool zspage_full(struct size_class *class, struct zspage *zspage)
312fcae2 1240{
3783689a 1241 return get_zspage_inuse(zspage) == class->objs_per_zspage;
312fcae2
MK
1242}
1243
66cdef66
GM
1244unsigned long zs_get_total_pages(struct zs_pool *pool)
1245{
1246 return atomic_long_read(&pool->pages_allocated);
1247}
1248EXPORT_SYMBOL_GPL(zs_get_total_pages);
1249
4bbc0bc0 1250/**
66cdef66
GM
1251 * zs_map_object - get address of allocated object from handle.
1252 * @pool: pool from which the object was allocated
1253 * @handle: handle returned from zs_malloc
4bbc0bc0 1254 *
66cdef66
GM
1255 * Before using an object allocated from zs_malloc, it must be mapped using
1256 * this function. When done with the object, it must be unmapped using
1257 * zs_unmap_object.
4bbc0bc0 1258 *
66cdef66
GM
1259 * Only one object can be mapped per cpu at a time. There is no protection
1260 * against nested mappings.
1261 *
1262 * This function returns with preemption and page faults disabled.
4bbc0bc0 1263 */
66cdef66
GM
1264void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1265 enum zs_mapmode mm)
61989a80 1266{
3783689a 1267 struct zspage *zspage;
66cdef66 1268 struct page *page;
bfd093f5
MK
1269 unsigned long obj, off;
1270 unsigned int obj_idx;
61989a80 1271
66cdef66
GM
1272 unsigned int class_idx;
1273 enum fullness_group fg;
1274 struct size_class *class;
1275 struct mapping_area *area;
1276 struct page *pages[2];
2e40e163 1277 void *ret;
61989a80 1278
9eec4cd5 1279 /*
66cdef66
GM
1280 * Because we use per-cpu mapping areas shared among the
1281 * pools/users, we can't allow mapping in interrupt context
1282 * because it can corrupt another users mappings.
9eec4cd5 1283 */
830e4bc5 1284 WARN_ON_ONCE(in_interrupt());
61989a80 1285
312fcae2
MK
1286 /* From now on, migration cannot move the object */
1287 pin_tag(handle);
1288
2e40e163
MK
1289 obj = handle_to_obj(handle);
1290 obj_to_location(obj, &page, &obj_idx);
3783689a
MK
1291 zspage = get_zspage(page);
1292 get_zspage_mapping(zspage, &class_idx, &fg);
66cdef66 1293 class = pool->size_class[class_idx];
bfd093f5 1294 off = (class->size * obj_idx) & ~PAGE_MASK;
df8b5bb9 1295
66cdef66
GM
1296 area = &get_cpu_var(zs_map_area);
1297 area->vm_mm = mm;
1298 if (off + class->size <= PAGE_SIZE) {
1299 /* this object is contained entirely within a page */
1300 area->vm_addr = kmap_atomic(page);
2e40e163
MK
1301 ret = area->vm_addr + off;
1302 goto out;
61989a80
NG
1303 }
1304
66cdef66
GM
1305 /* this object spans two pages */
1306 pages[0] = page;
1307 pages[1] = get_next_page(page);
1308 BUG_ON(!pages[1]);
9eec4cd5 1309
2e40e163
MK
1310 ret = __zs_map_object(area, pages, off, class->size);
1311out:
7b60a685
MK
1312 if (!class->huge)
1313 ret += ZS_HANDLE_SIZE;
1314
1315 return ret;
61989a80 1316}
66cdef66 1317EXPORT_SYMBOL_GPL(zs_map_object);
61989a80 1318
66cdef66 1319void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
61989a80 1320{
3783689a 1321 struct zspage *zspage;
66cdef66 1322 struct page *page;
bfd093f5
MK
1323 unsigned long obj, off;
1324 unsigned int obj_idx;
61989a80 1325
66cdef66
GM
1326 unsigned int class_idx;
1327 enum fullness_group fg;
1328 struct size_class *class;
1329 struct mapping_area *area;
9eec4cd5 1330
2e40e163
MK
1331 obj = handle_to_obj(handle);
1332 obj_to_location(obj, &page, &obj_idx);
3783689a
MK
1333 zspage = get_zspage(page);
1334 get_zspage_mapping(zspage, &class_idx, &fg);
66cdef66 1335 class = pool->size_class[class_idx];
bfd093f5 1336 off = (class->size * obj_idx) & ~PAGE_MASK;
61989a80 1337
66cdef66
GM
1338 area = this_cpu_ptr(&zs_map_area);
1339 if (off + class->size <= PAGE_SIZE)
1340 kunmap_atomic(area->vm_addr);
1341 else {
1342 struct page *pages[2];
40f9fb8c 1343
66cdef66
GM
1344 pages[0] = page;
1345 pages[1] = get_next_page(page);
1346 BUG_ON(!pages[1]);
1347
1348 __zs_unmap_object(area, pages, off, class->size);
1349 }
1350 put_cpu_var(zs_map_area);
312fcae2 1351 unpin_tag(handle);
61989a80 1352}
66cdef66 1353EXPORT_SYMBOL_GPL(zs_unmap_object);
61989a80 1354
251cbb95 1355static unsigned long obj_malloc(struct size_class *class,
3783689a 1356 struct zspage *zspage, unsigned long handle)
c7806261 1357{
bfd093f5 1358 int i, nr_page, offset;
c7806261
MK
1359 unsigned long obj;
1360 struct link_free *link;
1361
1362 struct page *m_page;
bfd093f5 1363 unsigned long m_offset;
c7806261
MK
1364 void *vaddr;
1365
312fcae2 1366 handle |= OBJ_ALLOCATED_TAG;
3783689a 1367 obj = get_freeobj(zspage);
bfd093f5
MK
1368
1369 offset = obj * class->size;
1370 nr_page = offset >> PAGE_SHIFT;
1371 m_offset = offset & ~PAGE_MASK;
1372 m_page = get_first_page(zspage);
1373
1374 for (i = 0; i < nr_page; i++)
1375 m_page = get_next_page(m_page);
c7806261
MK
1376
1377 vaddr = kmap_atomic(m_page);
1378 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
bfd093f5 1379 set_freeobj(zspage, link->next >> OBJ_ALLOCATED_TAG);
7b60a685
MK
1380 if (!class->huge)
1381 /* record handle in the header of allocated chunk */
1382 link->handle = handle;
1383 else
3783689a
MK
1384 /* record handle to page->index */
1385 zspage->first_page->index = handle;
1386
c7806261 1387 kunmap_atomic(vaddr);
3783689a 1388 mod_zspage_inuse(zspage, 1);
c7806261
MK
1389 zs_stat_inc(class, OBJ_USED, 1);
1390
bfd093f5
MK
1391 obj = location_to_obj(m_page, obj);
1392
c7806261
MK
1393 return obj;
1394}
1395
1396
61989a80
NG
1397/**
1398 * zs_malloc - Allocate block of given size from pool.
1399 * @pool: pool to allocate from
1400 * @size: size of block to allocate
61989a80 1401 *
00a61d86 1402 * On success, handle to the allocated object is returned,
c2344348 1403 * otherwise 0.
61989a80
NG
1404 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1405 */
d0d8da2d 1406unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
61989a80 1407{
2e40e163 1408 unsigned long handle, obj;
61989a80 1409 struct size_class *class;
3783689a 1410 struct zspage *zspage;
61989a80 1411
7b60a685 1412 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
2e40e163
MK
1413 return 0;
1414
3783689a 1415 handle = cache_alloc_handle(pool, gfp);
2e40e163 1416 if (!handle)
c2344348 1417 return 0;
61989a80 1418
2e40e163
MK
1419 /* extra space in chunk to keep the handle */
1420 size += ZS_HANDLE_SIZE;
9eec4cd5 1421 class = pool->size_class[get_size_class_index(size)];
61989a80
NG
1422
1423 spin_lock(&class->lock);
3783689a 1424 zspage = find_get_zspage(class);
61989a80 1425
3783689a 1426 if (!zspage) {
61989a80 1427 spin_unlock(&class->lock);
3783689a
MK
1428 zspage = alloc_zspage(pool, class, gfp);
1429 if (unlikely(!zspage)) {
1430 cache_free_handle(pool, handle);
c2344348 1431 return 0;
2e40e163 1432 }
61989a80 1433
3783689a 1434 set_zspage_mapping(zspage, class->index, ZS_EMPTY);
13de8933
MK
1435 atomic_long_add(class->pages_per_zspage,
1436 &pool->pages_allocated);
0f050d99 1437
61989a80 1438 spin_lock(&class->lock);
0f050d99
GM
1439 zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1440 class->size, class->pages_per_zspage));
61989a80
NG
1441 }
1442
3783689a 1443 obj = obj_malloc(class, zspage, handle);
61989a80 1444 /* Now move the zspage to another fullness group, if required */
3783689a 1445 fix_fullness_group(class, zspage);
2e40e163 1446 record_obj(handle, obj);
61989a80
NG
1447 spin_unlock(&class->lock);
1448
2e40e163 1449 return handle;
61989a80
NG
1450}
1451EXPORT_SYMBOL_GPL(zs_malloc);
1452
1ee47165 1453static void obj_free(struct size_class *class, unsigned long obj)
61989a80
NG
1454{
1455 struct link_free *link;
3783689a
MK
1456 struct zspage *zspage;
1457 struct page *f_page;
bfd093f5
MK
1458 unsigned long f_offset;
1459 unsigned int f_objidx;
af4ee5e9 1460 void *vaddr;
61989a80 1461
312fcae2 1462 obj &= ~OBJ_ALLOCATED_TAG;
2e40e163 1463 obj_to_location(obj, &f_page, &f_objidx);
bfd093f5 1464 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
3783689a 1465 zspage = get_zspage(f_page);
61989a80 1466
c7806261 1467 vaddr = kmap_atomic(f_page);
61989a80
NG
1468
1469 /* Insert this object in containing zspage's freelist */
af4ee5e9 1470 link = (struct link_free *)(vaddr + f_offset);
bfd093f5 1471 link->next = get_freeobj(zspage) << OBJ_ALLOCATED_TAG;
af4ee5e9 1472 kunmap_atomic(vaddr);
bfd093f5 1473 set_freeobj(zspage, f_objidx);
3783689a 1474 mod_zspage_inuse(zspage, -1);
0f050d99 1475 zs_stat_dec(class, OBJ_USED, 1);
c7806261
MK
1476}
1477
1478void zs_free(struct zs_pool *pool, unsigned long handle)
1479{
3783689a
MK
1480 struct zspage *zspage;
1481 struct page *f_page;
bfd093f5
MK
1482 unsigned long obj;
1483 unsigned int f_objidx;
c7806261
MK
1484 int class_idx;
1485 struct size_class *class;
1486 enum fullness_group fullness;
1487
1488 if (unlikely(!handle))
1489 return;
1490
312fcae2 1491 pin_tag(handle);
c7806261 1492 obj = handle_to_obj(handle);
c7806261 1493 obj_to_location(obj, &f_page, &f_objidx);
3783689a 1494 zspage = get_zspage(f_page);
c7806261 1495
3783689a 1496 get_zspage_mapping(zspage, &class_idx, &fullness);
c7806261
MK
1497 class = pool->size_class[class_idx];
1498
1499 spin_lock(&class->lock);
1ee47165 1500 obj_free(class, obj);
3783689a 1501 fullness = fix_fullness_group(class, zspage);
312fcae2 1502 if (fullness == ZS_EMPTY) {
0f050d99
GM
1503 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1504 class->size, class->pages_per_zspage));
312fcae2
MK
1505 atomic_long_sub(class->pages_per_zspage,
1506 &pool->pages_allocated);
3783689a 1507 free_zspage(pool, zspage);
312fcae2 1508 }
61989a80 1509 spin_unlock(&class->lock);
312fcae2 1510 unpin_tag(handle);
61989a80 1511
3783689a 1512 cache_free_handle(pool, handle);
312fcae2
MK
1513}
1514EXPORT_SYMBOL_GPL(zs_free);
1515
251cbb95
MK
1516static void zs_object_copy(struct size_class *class, unsigned long dst,
1517 unsigned long src)
312fcae2
MK
1518{
1519 struct page *s_page, *d_page;
bfd093f5 1520 unsigned int s_objidx, d_objidx;
312fcae2
MK
1521 unsigned long s_off, d_off;
1522 void *s_addr, *d_addr;
1523 int s_size, d_size, size;
1524 int written = 0;
1525
1526 s_size = d_size = class->size;
1527
1528 obj_to_location(src, &s_page, &s_objidx);
1529 obj_to_location(dst, &d_page, &d_objidx);
1530
bfd093f5
MK
1531 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1532 d_off = (class->size * d_objidx) & ~PAGE_MASK;
312fcae2
MK
1533
1534 if (s_off + class->size > PAGE_SIZE)
1535 s_size = PAGE_SIZE - s_off;
1536
1537 if (d_off + class->size > PAGE_SIZE)
1538 d_size = PAGE_SIZE - d_off;
1539
1540 s_addr = kmap_atomic(s_page);
1541 d_addr = kmap_atomic(d_page);
1542
1543 while (1) {
1544 size = min(s_size, d_size);
1545 memcpy(d_addr + d_off, s_addr + s_off, size);
1546 written += size;
1547
1548 if (written == class->size)
1549 break;
1550
495819ea
SS
1551 s_off += size;
1552 s_size -= size;
1553 d_off += size;
1554 d_size -= size;
1555
1556 if (s_off >= PAGE_SIZE) {
312fcae2
MK
1557 kunmap_atomic(d_addr);
1558 kunmap_atomic(s_addr);
1559 s_page = get_next_page(s_page);
312fcae2
MK
1560 s_addr = kmap_atomic(s_page);
1561 d_addr = kmap_atomic(d_page);
1562 s_size = class->size - written;
1563 s_off = 0;
312fcae2
MK
1564 }
1565
495819ea 1566 if (d_off >= PAGE_SIZE) {
312fcae2
MK
1567 kunmap_atomic(d_addr);
1568 d_page = get_next_page(d_page);
312fcae2
MK
1569 d_addr = kmap_atomic(d_page);
1570 d_size = class->size - written;
1571 d_off = 0;
312fcae2
MK
1572 }
1573 }
1574
1575 kunmap_atomic(d_addr);
1576 kunmap_atomic(s_addr);
1577}
1578
1579/*
1580 * Find alloced object in zspage from index object and
1581 * return handle.
1582 */
251cbb95
MK
1583static unsigned long find_alloced_obj(struct size_class *class,
1584 struct page *page, int index)
312fcae2
MK
1585{
1586 unsigned long head;
1587 int offset = 0;
1588 unsigned long handle = 0;
1589 void *addr = kmap_atomic(page);
1590
3783689a 1591 offset = get_first_obj_offset(page);
312fcae2
MK
1592 offset += class->size * index;
1593
1594 while (offset < PAGE_SIZE) {
7b60a685 1595 head = obj_to_head(class, page, addr + offset);
312fcae2
MK
1596 if (head & OBJ_ALLOCATED_TAG) {
1597 handle = head & ~OBJ_ALLOCATED_TAG;
1598 if (trypin_tag(handle))
1599 break;
1600 handle = 0;
1601 }
1602
1603 offset += class->size;
1604 index++;
1605 }
1606
1607 kunmap_atomic(addr);
1608 return handle;
1609}
1610
1611struct zs_compact_control {
3783689a 1612 /* Source spage for migration which could be a subpage of zspage */
312fcae2
MK
1613 struct page *s_page;
1614 /* Destination page for migration which should be a first page
1615 * of zspage. */
1616 struct page *d_page;
1617 /* Starting object index within @s_page which used for live object
1618 * in the subpage. */
1619 int index;
312fcae2
MK
1620};
1621
1622static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1623 struct zs_compact_control *cc)
1624{
1625 unsigned long used_obj, free_obj;
1626 unsigned long handle;
1627 struct page *s_page = cc->s_page;
1628 struct page *d_page = cc->d_page;
1629 unsigned long index = cc->index;
312fcae2
MK
1630 int ret = 0;
1631
1632 while (1) {
251cbb95 1633 handle = find_alloced_obj(class, s_page, index);
312fcae2
MK
1634 if (!handle) {
1635 s_page = get_next_page(s_page);
1636 if (!s_page)
1637 break;
1638 index = 0;
1639 continue;
1640 }
1641
1642 /* Stop if there is no more space */
3783689a 1643 if (zspage_full(class, get_zspage(d_page))) {
312fcae2
MK
1644 unpin_tag(handle);
1645 ret = -ENOMEM;
1646 break;
1647 }
1648
1649 used_obj = handle_to_obj(handle);
3783689a 1650 free_obj = obj_malloc(class, get_zspage(d_page), handle);
251cbb95 1651 zs_object_copy(class, free_obj, used_obj);
312fcae2 1652 index++;
c102f07c
JL
1653 /*
1654 * record_obj updates handle's value to free_obj and it will
1655 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1656 * breaks synchronization using pin_tag(e,g, zs_free) so
1657 * let's keep the lock bit.
1658 */
1659 free_obj |= BIT(HANDLE_PIN_BIT);
312fcae2
MK
1660 record_obj(handle, free_obj);
1661 unpin_tag(handle);
1ee47165 1662 obj_free(class, used_obj);
312fcae2
MK
1663 }
1664
1665 /* Remember last position in this iteration */
1666 cc->s_page = s_page;
1667 cc->index = index;
312fcae2
MK
1668
1669 return ret;
1670}
1671
3783689a 1672static struct zspage *isolate_zspage(struct size_class *class, bool source)
312fcae2
MK
1673{
1674 int i;
3783689a
MK
1675 struct zspage *zspage;
1676 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
312fcae2 1677
3783689a
MK
1678 if (!source) {
1679 fg[0] = ZS_ALMOST_FULL;
1680 fg[1] = ZS_ALMOST_EMPTY;
1681 }
1682
1683 for (i = 0; i < 2; i++) {
1684 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1685 struct zspage, list);
1686 if (zspage) {
1687 remove_zspage(class, zspage, fg[i]);
1688 return zspage;
312fcae2
MK
1689 }
1690 }
1691
3783689a 1692 return zspage;
312fcae2
MK
1693}
1694
860c707d 1695/*
3783689a 1696 * putback_zspage - add @zspage into right class's fullness list
860c707d 1697 * @class: destination class
3783689a 1698 * @zspage: target page
860c707d 1699 *
3783689a 1700 * Return @zspage's fullness_group
860c707d 1701 */
4aa409ca 1702static enum fullness_group putback_zspage(struct size_class *class,
3783689a 1703 struct zspage *zspage)
312fcae2 1704{
312fcae2
MK
1705 enum fullness_group fullness;
1706
3783689a
MK
1707 fullness = get_fullness_group(class, zspage);
1708 insert_zspage(class, zspage, fullness);
1709 set_zspage_mapping(zspage, class->index, fullness);
839373e6 1710
860c707d 1711 return fullness;
61989a80 1712}
312fcae2 1713
04f05909
SS
1714/*
1715 *
1716 * Based on the number of unused allocated objects calculate
1717 * and return the number of pages that we can free.
04f05909
SS
1718 */
1719static unsigned long zs_can_compact(struct size_class *class)
1720{
1721 unsigned long obj_wasted;
44f43e99
SS
1722 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
1723 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
04f05909 1724
44f43e99
SS
1725 if (obj_allocated <= obj_used)
1726 return 0;
04f05909 1727
44f43e99 1728 obj_wasted = obj_allocated - obj_used;
04f05909
SS
1729 obj_wasted /= get_maxobj_per_zspage(class->size,
1730 class->pages_per_zspage);
1731
6cbf16b3 1732 return obj_wasted * class->pages_per_zspage;
04f05909
SS
1733}
1734
7d3f3938 1735static void __zs_compact(struct zs_pool *pool, struct size_class *class)
312fcae2 1736{
312fcae2 1737 struct zs_compact_control cc;
3783689a
MK
1738 struct zspage *src_zspage;
1739 struct zspage *dst_zspage = NULL;
312fcae2 1740
312fcae2 1741 spin_lock(&class->lock);
3783689a 1742 while ((src_zspage = isolate_zspage(class, true))) {
312fcae2 1743
04f05909
SS
1744 if (!zs_can_compact(class))
1745 break;
1746
312fcae2 1747 cc.index = 0;
3783689a 1748 cc.s_page = src_zspage->first_page;
312fcae2 1749
3783689a
MK
1750 while ((dst_zspage = isolate_zspage(class, false))) {
1751 cc.d_page = dst_zspage->first_page;
312fcae2 1752 /*
0dc63d48
SS
1753 * If there is no more space in dst_page, resched
1754 * and see if anyone had allocated another zspage.
312fcae2
MK
1755 */
1756 if (!migrate_zspage(pool, class, &cc))
1757 break;
1758
4aa409ca 1759 putback_zspage(class, dst_zspage);
312fcae2
MK
1760 }
1761
1762 /* Stop if we couldn't find slot */
3783689a 1763 if (dst_zspage == NULL)
312fcae2
MK
1764 break;
1765
4aa409ca
MK
1766 putback_zspage(class, dst_zspage);
1767 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
1768 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1769 class->size, class->pages_per_zspage));
1770 atomic_long_sub(class->pages_per_zspage,
1771 &pool->pages_allocated);
1772 free_zspage(pool, src_zspage);
6cbf16b3 1773 pool->stats.pages_compacted += class->pages_per_zspage;
4aa409ca 1774 }
312fcae2 1775 spin_unlock(&class->lock);
312fcae2
MK
1776 cond_resched();
1777 spin_lock(&class->lock);
1778 }
1779
3783689a 1780 if (src_zspage)
4aa409ca 1781 putback_zspage(class, src_zspage);
312fcae2 1782
7d3f3938 1783 spin_unlock(&class->lock);
312fcae2
MK
1784}
1785
1786unsigned long zs_compact(struct zs_pool *pool)
1787{
1788 int i;
312fcae2
MK
1789 struct size_class *class;
1790
1791 for (i = zs_size_classes - 1; i >= 0; i--) {
1792 class = pool->size_class[i];
1793 if (!class)
1794 continue;
1795 if (class->index != i)
1796 continue;
7d3f3938 1797 __zs_compact(pool, class);
312fcae2
MK
1798 }
1799
860c707d 1800 return pool->stats.pages_compacted;
312fcae2
MK
1801}
1802EXPORT_SYMBOL_GPL(zs_compact);
61989a80 1803
7d3f3938
SS
1804void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
1805{
1806 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
1807}
1808EXPORT_SYMBOL_GPL(zs_pool_stats);
1809
ab9d306d
SS
1810static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
1811 struct shrink_control *sc)
1812{
1813 unsigned long pages_freed;
1814 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1815 shrinker);
1816
1817 pages_freed = pool->stats.pages_compacted;
1818 /*
1819 * Compact classes and calculate compaction delta.
1820 * Can run concurrently with a manually triggered
1821 * (by user) compaction.
1822 */
1823 pages_freed = zs_compact(pool) - pages_freed;
1824
1825 return pages_freed ? pages_freed : SHRINK_STOP;
1826}
1827
1828static unsigned long zs_shrinker_count(struct shrinker *shrinker,
1829 struct shrink_control *sc)
1830{
1831 int i;
1832 struct size_class *class;
1833 unsigned long pages_to_free = 0;
1834 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1835 shrinker);
1836
ab9d306d
SS
1837 for (i = zs_size_classes - 1; i >= 0; i--) {
1838 class = pool->size_class[i];
1839 if (!class)
1840 continue;
1841 if (class->index != i)
1842 continue;
1843
ab9d306d 1844 pages_to_free += zs_can_compact(class);
ab9d306d
SS
1845 }
1846
1847 return pages_to_free;
1848}
1849
1850static void zs_unregister_shrinker(struct zs_pool *pool)
1851{
1852 if (pool->shrinker_enabled) {
1853 unregister_shrinker(&pool->shrinker);
1854 pool->shrinker_enabled = false;
1855 }
1856}
1857
1858static int zs_register_shrinker(struct zs_pool *pool)
1859{
1860 pool->shrinker.scan_objects = zs_shrinker_scan;
1861 pool->shrinker.count_objects = zs_shrinker_count;
1862 pool->shrinker.batch = 0;
1863 pool->shrinker.seeks = DEFAULT_SEEKS;
1864
1865 return register_shrinker(&pool->shrinker);
1866}
1867
00a61d86 1868/**
66cdef66
GM
1869 * zs_create_pool - Creates an allocation pool to work from.
1870 * @flags: allocation flags used to allocate pool metadata
166cfda7 1871 *
66cdef66
GM
1872 * This function must be called before anything when using
1873 * the zsmalloc allocator.
166cfda7 1874 *
66cdef66
GM
1875 * On success, a pointer to the newly created pool is returned,
1876 * otherwise NULL.
396b7fd6 1877 */
d0d8da2d 1878struct zs_pool *zs_create_pool(const char *name)
61989a80 1879{
66cdef66
GM
1880 int i;
1881 struct zs_pool *pool;
1882 struct size_class *prev_class = NULL;
61989a80 1883
66cdef66
GM
1884 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
1885 if (!pool)
1886 return NULL;
61989a80 1887
66cdef66
GM
1888 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
1889 GFP_KERNEL);
1890 if (!pool->size_class) {
1891 kfree(pool);
1892 return NULL;
1893 }
61989a80 1894
2e40e163
MK
1895 pool->name = kstrdup(name, GFP_KERNEL);
1896 if (!pool->name)
1897 goto err;
1898
3783689a 1899 if (create_cache(pool))
2e40e163
MK
1900 goto err;
1901
c60369f0 1902 /*
66cdef66
GM
1903 * Iterate reversly, because, size of size_class that we want to use
1904 * for merging should be larger or equal to current size.
c60369f0 1905 */
66cdef66
GM
1906 for (i = zs_size_classes - 1; i >= 0; i--) {
1907 int size;
1908 int pages_per_zspage;
1909 struct size_class *class;
3783689a 1910 int fullness = 0;
c60369f0 1911
66cdef66
GM
1912 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
1913 if (size > ZS_MAX_ALLOC_SIZE)
1914 size = ZS_MAX_ALLOC_SIZE;
1915 pages_per_zspage = get_pages_per_zspage(size);
61989a80 1916
66cdef66
GM
1917 /*
1918 * size_class is used for normal zsmalloc operation such
1919 * as alloc/free for that size. Although it is natural that we
1920 * have one size_class for each size, there is a chance that we
1921 * can get more memory utilization if we use one size_class for
1922 * many different sizes whose size_class have same
1923 * characteristics. So, we makes size_class point to
1924 * previous size_class if possible.
1925 */
1926 if (prev_class) {
1927 if (can_merge(prev_class, size, pages_per_zspage)) {
1928 pool->size_class[i] = prev_class;
1929 continue;
1930 }
1931 }
1932
1933 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
1934 if (!class)
1935 goto err;
1936
1937 class->size = size;
1938 class->index = i;
1939 class->pages_per_zspage = pages_per_zspage;
1fc6e27d
MK
1940 class->objs_per_zspage = class->pages_per_zspage *
1941 PAGE_SIZE / class->size;
1942 if (pages_per_zspage == 1 && class->objs_per_zspage == 1)
7b60a685 1943 class->huge = true;
66cdef66
GM
1944 spin_lock_init(&class->lock);
1945 pool->size_class[i] = class;
3783689a
MK
1946 for (fullness = ZS_ALMOST_FULL; fullness <= ZS_ALMOST_EMPTY;
1947 fullness++)
1948 INIT_LIST_HEAD(&class->fullness_list[fullness]);
66cdef66
GM
1949
1950 prev_class = class;
61989a80
NG
1951 }
1952
d34f6157
DS
1953 /* debug only, don't abort if it fails */
1954 zs_pool_stat_create(pool, name);
0f050d99 1955
ab9d306d
SS
1956 /*
1957 * Not critical, we still can use the pool
1958 * and user can trigger compaction manually.
1959 */
1960 if (zs_register_shrinker(pool) == 0)
1961 pool->shrinker_enabled = true;
66cdef66
GM
1962 return pool;
1963
1964err:
1965 zs_destroy_pool(pool);
1966 return NULL;
61989a80 1967}
66cdef66 1968EXPORT_SYMBOL_GPL(zs_create_pool);
61989a80 1969
66cdef66 1970void zs_destroy_pool(struct zs_pool *pool)
61989a80 1971{
66cdef66 1972 int i;
61989a80 1973
ab9d306d 1974 zs_unregister_shrinker(pool);
0f050d99
GM
1975 zs_pool_stat_destroy(pool);
1976
66cdef66
GM
1977 for (i = 0; i < zs_size_classes; i++) {
1978 int fg;
1979 struct size_class *class = pool->size_class[i];
61989a80 1980
66cdef66
GM
1981 if (!class)
1982 continue;
61989a80 1983
66cdef66
GM
1984 if (class->index != i)
1985 continue;
61989a80 1986
3783689a
MK
1987 for (fg = ZS_ALMOST_FULL; fg <= ZS_ALMOST_EMPTY; fg++) {
1988 if (!list_empty(&class->fullness_list[fg])) {
66cdef66
GM
1989 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
1990 class->size, fg);
1991 }
1992 }
1993 kfree(class);
1994 }
f553646a 1995
3783689a 1996 destroy_cache(pool);
66cdef66 1997 kfree(pool->size_class);
0f050d99 1998 kfree(pool->name);
66cdef66
GM
1999 kfree(pool);
2000}
2001EXPORT_SYMBOL_GPL(zs_destroy_pool);
b7418510 2002
66cdef66
GM
2003static int __init zs_init(void)
2004{
2005 int ret = zs_register_cpu_notifier();
2006
0f050d99
GM
2007 if (ret)
2008 goto notifier_fail;
66cdef66
GM
2009
2010 init_zs_size_classes();
2011
2012#ifdef CONFIG_ZPOOL
2013 zpool_register_driver(&zs_zpool_driver);
2014#endif
0f050d99 2015
4abaac9b
DS
2016 zs_stat_init();
2017
66cdef66 2018 return 0;
0f050d99 2019
0f050d99
GM
2020notifier_fail:
2021 zs_unregister_cpu_notifier();
2022
2023 return ret;
61989a80 2024}
61989a80 2025
66cdef66 2026static void __exit zs_exit(void)
61989a80 2027{
66cdef66
GM
2028#ifdef CONFIG_ZPOOL
2029 zpool_unregister_driver(&zs_zpool_driver);
2030#endif
2031 zs_unregister_cpu_notifier();
0f050d99
GM
2032
2033 zs_stat_exit();
61989a80 2034}
069f101f
BH
2035
2036module_init(zs_init);
2037module_exit(zs_exit);
2038
2039MODULE_LICENSE("Dual BSD/GPL");
2040MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");