blob: 6f3cfbf5e237fb62b76c315a249ef75eb8b4d2cb [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
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
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Cuptac3e3e882013-12-11 11:04:37 +090015 * This allocator is designed for use with zram. Thus, the allocator is
16 * supposed to work well under low memory conditions. In particular, it
17 * never attempts higher order page allocation which is very likely to
18 * fail under memory pressure. On the other hand, if we just use single
19 * (0-order) pages, it would suffer from very high fragmentation --
20 * any object of size PAGE_SIZE/2 or larger would occupy an entire page.
21 * This was one of the major issues with its predecessor (xvmalloc).
Nitin Gupta2db51da2012-06-09 17:41:14 -070022 *
23 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
24 * and links them together using various 'struct page' fields. These linked
25 * pages act as a single higher-order page i.e. an object can span 0-order
26 * page boundaries. The code refers to these linked pages as a single entity
27 * called zspage.
28 *
Nitin Cuptac3e3e882013-12-11 11:04:37 +090029 * For simplicity, zsmalloc can only allocate objects of size up to PAGE_SIZE
30 * since this satisfies the requirements of all its current users (in the
31 * worst case, page is incompressible and is thus stored "as-is" i.e. in
32 * uncompressed form). For allocation requests larger than this size, failure
33 * is returned (see zs_malloc).
34 *
35 * Additionally, zs_malloc() does not return a dereferenceable pointer.
36 * Instead, it returns an opaque handle (unsigned long) which encodes actual
37 * location of the allocated object. The reason for this indirection is that
38 * zsmalloc does not keep zspages permanently mapped since that would cause
39 * issues on 32-bit systems where the VA region for kernel space mappings
40 * is very small. So, before using the allocating memory, the object has to
41 * be mapped using zs_map_object() to get a usable pointer and subsequently
42 * unmapped using zs_unmap_object().
43 *
Nitin Gupta2db51da2012-06-09 17:41:14 -070044 * Following is how we use various fields and flags of underlying
45 * struct page(s) to form a zspage.
46 *
47 * Usage of struct page fields:
48 * page->first_page: points to the first component (0-order) page
49 * page->index (union with page->freelist): offset of the first object
50 * starting in this page. For the first page, this is
51 * always 0, so we use this field (aka freelist) to point
52 * to the first free object in zspage.
53 * page->lru: links together all component pages (except the first page)
54 * of a zspage
55 *
56 * For _first_ page only:
57 *
58 * page->private (union with page->first_page): refers to the
59 * component page after the first page
60 * page->freelist: points to the first free object in zspage.
61 * Free objects are linked together using in-place
62 * metadata.
63 * page->objects: maximum number of objects we can store in this
64 * zspage (class->zspage_order * PAGE_SIZE / class->size)
65 * page->lru: links together first pages of various zspages.
66 * Basically forming list of zspages in a fullness group.
67 * page->mapping: class index and fullness group of the zspage
68 *
69 * Usage of struct page flags:
70 * PG_private: identifies the first component page
71 * PG_private2: identifies the last component page
72 *
73 */
74
Nitin Gupta61989a82012-01-09 16:51:56 -060075#ifdef CONFIG_ZSMALLOC_DEBUG
76#define DEBUG
77#endif
78
79#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/bitops.h>
82#include <linux/errno.h>
83#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060084#include <linux/string.h>
85#include <linux/slab.h>
86#include <asm/tlbflush.h>
87#include <asm/pgtable.h>
88#include <linux/cpumask.h>
89#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060090#include <linux/vmalloc.h>
Seth Jenningsc60369f2012-07-18 11:55:55 -050091#include <linux/hardirq.h>
Seth Jennings0959c632012-08-08 15:12:17 +090092#include <linux/spinlock.h>
93#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080094#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080095#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070096#include <linux/zpool.h>
Seth Jennings0959c632012-08-08 15:12:17 +090097
98/*
99 * This must be power of 2 and greater than of equal to sizeof(link_free).
100 * These two conditions ensure that any 'struct link_free' itself doesn't
101 * span more than 1 page which avoids complex case of mapping 2 pages simply
102 * to restore link_free pointer values.
103 */
104#define ZS_ALIGN 8
105
106/*
107 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
108 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
109 */
110#define ZS_MAX_ZSPAGE_ORDER 2
111#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
112
Minchan Kim2e40e162015-04-15 16:15:23 -0700113#define ZS_HANDLE_SIZE (sizeof(unsigned long))
114
Seth Jennings0959c632012-08-08 15:12:17 +0900115/*
116 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900117 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +0900118 *
119 * Note that object index <obj_idx> is relative to system
120 * page <PFN> it is stored in, so for each sub-page belonging
121 * to a zspage, obj_idx starts with 0.
122 *
123 * This is made more complicated by various memory models and PAE.
124 */
125
126#ifndef MAX_PHYSMEM_BITS
127#ifdef CONFIG_HIGHMEM64G
128#define MAX_PHYSMEM_BITS 36
129#else /* !CONFIG_HIGHMEM64G */
130/*
131 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
132 * be PAGE_SHIFT
133 */
134#define MAX_PHYSMEM_BITS BITS_PER_LONG
135#endif
136#endif
137#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
138#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
139#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
140
141#define MAX(a, b) ((a) >= (b) ? (a) : (b))
142/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
143#define ZS_MIN_ALLOC_SIZE \
144 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700145/* each chunk includes extra space to keep handle */
146#define ZS_MAX_ALLOC_SIZE (PAGE_SIZE + ZS_HANDLE_SIZE)
Seth Jennings0959c632012-08-08 15:12:17 +0900147
148/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700149 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900150 * trader-off here:
151 * - Large number of size classes is potentially wasteful as free page are
152 * spread across these classes
153 * - Small number of size classes causes large internal fragmentation
154 * - Probably its better to use specific size classes (empirically
155 * determined). NOTE: all those class sizes must be set as multiple of
156 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
157 *
158 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
159 * (reason above)
160 */
Seth Jenningsd662b8e2013-01-25 11:46:18 -0600161#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
Seth Jennings0959c632012-08-08 15:12:17 +0900162
163/*
164 * We do not maintain any list for completely empty or full pages
165 */
166enum fullness_group {
167 ZS_ALMOST_FULL,
168 ZS_ALMOST_EMPTY,
169 _ZS_NR_FULLNESS_GROUPS,
170
171 ZS_EMPTY,
172 ZS_FULL
173};
174
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800175enum zs_stat_type {
176 OBJ_ALLOCATED,
177 OBJ_USED,
178 NR_ZS_STAT_TYPE,
179};
180
181#ifdef CONFIG_ZSMALLOC_STAT
182
183static struct dentry *zs_stat_root;
184
185struct zs_size_stat {
186 unsigned long objs[NR_ZS_STAT_TYPE];
187};
188
189#endif
190
Seth Jennings0959c632012-08-08 15:12:17 +0900191/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800192 * number of size_classes
193 */
194static int zs_size_classes;
195
196/*
Seth Jennings0959c632012-08-08 15:12:17 +0900197 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
198 * n <= N / f, where
199 * n = number of allocated objects
200 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700201 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900202 *
203 * Similarly, we assign zspage to:
204 * ZS_ALMOST_FULL when n > N / f
205 * ZS_EMPTY when n == 0
206 * ZS_FULL when n == N
207 *
208 * (see: fix_fullness_group())
209 */
210static const int fullness_threshold_frac = 4;
211
212struct size_class {
213 /*
214 * Size of objects stored in this class. Must be multiple
215 * of ZS_ALIGN.
216 */
217 int size;
218 unsigned int index;
219
220 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
221 int pages_per_zspage;
222
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800223#ifdef CONFIG_ZSMALLOC_STAT
224 struct zs_size_stat stats;
225#endif
226
Seth Jennings0959c632012-08-08 15:12:17 +0900227 spinlock_t lock;
228
Seth Jennings0959c632012-08-08 15:12:17 +0900229 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
230};
231
232/*
233 * Placed within free objects to form a singly linked list.
234 * For every zspage, first_page->freelist gives head of this list.
235 *
236 * This must be power of 2 and less than or equal to ZS_ALIGN
237 */
238struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700239 union {
240 /*
241 * Position of next free chunk (encodes <PFN, obj_idx>)
242 * It's valid for non-allocated object
243 */
244 void *next;
245 /*
246 * Handle of allocated object.
247 */
248 unsigned long handle;
249 };
Seth Jennings0959c632012-08-08 15:12:17 +0900250};
251
252struct zs_pool {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800253 char *name;
254
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800255 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700256 struct kmem_cache *handle_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900257
258 gfp_t flags; /* allocation flags used when growing pool */
Minchan Kim13de8932014-10-09 15:29:48 -0700259 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800260
261#ifdef CONFIG_ZSMALLOC_STAT
262 struct dentry *stat_dentry;
263#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900264};
Nitin Gupta61989a82012-01-09 16:51:56 -0600265
266/*
267 * A zspage's class index and fullness group
268 * are encoded in its (first)page->mapping
269 */
270#define CLASS_IDX_BITS 28
271#define FULLNESS_BITS 4
272#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
273#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
274
Seth Jenningsf5536462012-07-18 11:55:56 -0500275struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900276#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500277 struct vm_struct *vm; /* vm area for mapping object that span pages */
278#else
279 char *vm_buf; /* copy buffer for objects that span pages */
280#endif
281 char *vm_addr; /* address of kmap_atomic()'ed pages */
282 enum zs_mapmode vm_mm; /* mapping mode */
283};
284
Minchan Kim2e40e162015-04-15 16:15:23 -0700285static int create_handle_cache(struct zs_pool *pool)
286{
287 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
288 0, 0, NULL);
289 return pool->handle_cachep ? 0 : 1;
290}
291
292static void destroy_handle_cache(struct zs_pool *pool)
293{
294 kmem_cache_destroy(pool->handle_cachep);
295}
296
297static unsigned long alloc_handle(struct zs_pool *pool)
298{
299 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
300 pool->flags & ~__GFP_HIGHMEM);
301}
302
303static void free_handle(struct zs_pool *pool, unsigned long handle)
304{
305 kmem_cache_free(pool->handle_cachep, (void *)handle);
306}
307
308static void record_obj(unsigned long handle, unsigned long obj)
309{
310 *(unsigned long *)handle = obj;
311}
312
Dan Streetmanc7957792014-08-06 16:08:38 -0700313/* zpool driver */
314
315#ifdef CONFIG_ZPOOL
316
Ganesh Mahendran3eba0c62015-02-12 15:00:51 -0800317static void *zs_zpool_create(char *name, gfp_t gfp, struct zpool_ops *zpool_ops)
Dan Streetmanc7957792014-08-06 16:08:38 -0700318{
Ganesh Mahendran3eba0c62015-02-12 15:00:51 -0800319 return zs_create_pool(name, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700320}
321
322static void zs_zpool_destroy(void *pool)
323{
324 zs_destroy_pool(pool);
325}
326
327static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
328 unsigned long *handle)
329{
330 *handle = zs_malloc(pool, size);
331 return *handle ? 0 : -1;
332}
333static void zs_zpool_free(void *pool, unsigned long handle)
334{
335 zs_free(pool, handle);
336}
337
338static int zs_zpool_shrink(void *pool, unsigned int pages,
339 unsigned int *reclaimed)
340{
341 return -EINVAL;
342}
343
344static void *zs_zpool_map(void *pool, unsigned long handle,
345 enum zpool_mapmode mm)
346{
347 enum zs_mapmode zs_mm;
348
349 switch (mm) {
350 case ZPOOL_MM_RO:
351 zs_mm = ZS_MM_RO;
352 break;
353 case ZPOOL_MM_WO:
354 zs_mm = ZS_MM_WO;
355 break;
356 case ZPOOL_MM_RW: /* fallthru */
357 default:
358 zs_mm = ZS_MM_RW;
359 break;
360 }
361
362 return zs_map_object(pool, handle, zs_mm);
363}
364static void zs_zpool_unmap(void *pool, unsigned long handle)
365{
366 zs_unmap_object(pool, handle);
367}
368
369static u64 zs_zpool_total_size(void *pool)
370{
Minchan Kim722cdc12014-10-09 15:29:50 -0700371 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700372}
373
374static struct zpool_driver zs_zpool_driver = {
375 .type = "zsmalloc",
376 .owner = THIS_MODULE,
377 .create = zs_zpool_create,
378 .destroy = zs_zpool_destroy,
379 .malloc = zs_zpool_malloc,
380 .free = zs_zpool_free,
381 .shrink = zs_zpool_shrink,
382 .map = zs_zpool_map,
383 .unmap = zs_zpool_unmap,
384 .total_size = zs_zpool_total_size,
385};
386
Kees Cook137f8cf2014-08-29 15:18:40 -0700387MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700388#endif /* CONFIG_ZPOOL */
389
Nitin Gupta61989a82012-01-09 16:51:56 -0600390/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
391static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
392
393static int is_first_page(struct page *page)
394{
Minchan Kima27545bf2012-04-25 15:23:09 +0900395 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600396}
397
398static int is_last_page(struct page *page)
399{
Minchan Kima27545bf2012-04-25 15:23:09 +0900400 return PagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600401}
402
403static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
404 enum fullness_group *fullness)
405{
406 unsigned long m;
407 BUG_ON(!is_first_page(page));
408
409 m = (unsigned long)page->mapping;
410 *fullness = m & FULLNESS_MASK;
411 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
412}
413
414static void set_zspage_mapping(struct page *page, unsigned int class_idx,
415 enum fullness_group fullness)
416{
417 unsigned long m;
418 BUG_ON(!is_first_page(page));
419
420 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
421 (fullness & FULLNESS_MASK);
422 page->mapping = (struct address_space *)m;
423}
424
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900425/*
426 * zsmalloc divides the pool into various size classes where each
427 * class maintains a list of zspages where each zspage is divided
428 * into equal sized chunks. Each allocation falls into one of these
429 * classes depending on its size. This function returns index of the
430 * size class which has chunk size big enough to hold the give size.
431 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600432static int get_size_class_index(int size)
433{
434 int idx = 0;
435
436 if (likely(size > ZS_MIN_ALLOC_SIZE))
437 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
438 ZS_SIZE_CLASS_DELTA);
439
440 return idx;
441}
442
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900443/*
444 * For each size class, zspages are divided into different groups
445 * depending on how "full" they are. This was done so that we could
446 * easily find empty or nearly empty zspages when we try to shrink
447 * the pool (not yet implemented). This function returns fullness
448 * status of the given page.
449 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600450static enum fullness_group get_fullness_group(struct page *page)
451{
452 int inuse, max_objects;
453 enum fullness_group fg;
454 BUG_ON(!is_first_page(page));
455
456 inuse = page->inuse;
457 max_objects = page->objects;
458
459 if (inuse == 0)
460 fg = ZS_EMPTY;
461 else if (inuse == max_objects)
462 fg = ZS_FULL;
463 else if (inuse <= max_objects / fullness_threshold_frac)
464 fg = ZS_ALMOST_EMPTY;
465 else
466 fg = ZS_ALMOST_FULL;
467
468 return fg;
469}
470
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900471/*
472 * Each size class maintains various freelists and zspages are assigned
473 * to one of these freelists based on the number of live objects they
474 * have. This functions inserts the given zspage into the freelist
475 * identified by <class, fullness_group>.
476 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600477static void insert_zspage(struct page *page, struct size_class *class,
478 enum fullness_group fullness)
479{
480 struct page **head;
481
482 BUG_ON(!is_first_page(page));
483
484 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
485 return;
486
487 head = &class->fullness_list[fullness];
488 if (*head)
489 list_add_tail(&page->lru, &(*head)->lru);
490
491 *head = page;
492}
493
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900494/*
495 * This function removes the given zspage from the freelist identified
496 * by <class, fullness_group>.
497 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600498static void remove_zspage(struct page *page, struct size_class *class,
499 enum fullness_group fullness)
500{
501 struct page **head;
502
503 BUG_ON(!is_first_page(page));
504
505 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
506 return;
507
508 head = &class->fullness_list[fullness];
509 BUG_ON(!*head);
510 if (list_empty(&(*head)->lru))
511 *head = NULL;
512 else if (*head == page)
513 *head = (struct page *)list_entry((*head)->lru.next,
514 struct page, lru);
515
516 list_del_init(&page->lru);
517}
518
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900519/*
520 * Each size class maintains zspages in different fullness groups depending
521 * on the number of live objects they contain. When allocating or freeing
522 * objects, the fullness status of the page can change, say, from ALMOST_FULL
523 * to ALMOST_EMPTY when freeing an object. This function checks if such
524 * a status change has occurred for the given page and accordingly moves the
525 * page from the freelist of the old fullness group to that of the new
526 * fullness group.
527 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600528static enum fullness_group fix_fullness_group(struct zs_pool *pool,
529 struct page *page)
530{
531 int class_idx;
532 struct size_class *class;
533 enum fullness_group currfg, newfg;
534
535 BUG_ON(!is_first_page(page));
536
537 get_zspage_mapping(page, &class_idx, &currfg);
538 newfg = get_fullness_group(page);
539 if (newfg == currfg)
540 goto out;
541
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -0800542 class = pool->size_class[class_idx];
Nitin Gupta61989a82012-01-09 16:51:56 -0600543 remove_zspage(page, class, currfg);
544 insert_zspage(page, class, newfg);
545 set_zspage_mapping(page, class_idx, newfg);
546
547out:
548 return newfg;
549}
550
551/*
552 * We have to decide on how many pages to link together
553 * to form a zspage for each size class. This is important
554 * to reduce wastage due to unusable space left at end of
555 * each zspage which is given as:
556 * wastage = Zp - Zp % size_class
557 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
558 *
559 * For example, for size class of 3/8 * PAGE_SIZE, we should
560 * link together 3 PAGE_SIZE sized pages to form a zspage
561 * since then we can perfectly fit in 8 such objects.
562 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900563static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600564{
565 int i, max_usedpc = 0;
566 /* zspage order which gives maximum used size per KB */
567 int max_usedpc_order = 1;
568
Seth Jennings84d4faa2012-03-05 11:33:21 -0600569 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600570 int zspage_size;
571 int waste, usedpc;
572
573 zspage_size = i * PAGE_SIZE;
574 waste = zspage_size % class_size;
575 usedpc = (zspage_size - waste) * 100 / zspage_size;
576
577 if (usedpc > max_usedpc) {
578 max_usedpc = usedpc;
579 max_usedpc_order = i;
580 }
581 }
582
583 return max_usedpc_order;
584}
585
586/*
587 * A single 'zspage' is composed of many system pages which are
588 * linked together using fields in struct page. This function finds
589 * the first/head page, given any component page of a zspage.
590 */
591static struct page *get_first_page(struct page *page)
592{
593 if (is_first_page(page))
594 return page;
595 else
596 return page->first_page;
597}
598
599static struct page *get_next_page(struct page *page)
600{
601 struct page *next;
602
603 if (is_last_page(page))
604 next = NULL;
605 else if (is_first_page(page))
Sunghan Suhe842b972013-07-12 16:08:13 +0900606 next = (struct page *)page_private(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600607 else
608 next = list_entry(page->lru.next, struct page, lru);
609
610 return next;
611}
612
Olav Haugan67296872013-11-22 09:30:41 -0800613/*
614 * Encode <page, obj_idx> as a single handle value.
615 * On hardware platforms with physical memory starting at 0x0 the pfn
616 * could be 0 so we ensure that the handle will never be 0 by adjusting the
617 * encoded obj_idx value before encoding.
618 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600619static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
620{
621 unsigned long handle;
622
623 if (!page) {
624 BUG_ON(obj_idx);
625 return NULL;
626 }
627
628 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
Olav Haugan67296872013-11-22 09:30:41 -0800629 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600630
631 return (void *)handle;
632}
633
Olav Haugan67296872013-11-22 09:30:41 -0800634/*
635 * Decode <page, obj_idx> pair from the given object handle. We adjust the
636 * decoded obj_idx back to its original value since it was adjusted in
637 * obj_location_to_handle().
638 */
Minchan Kim2e40e162015-04-15 16:15:23 -0700639static void obj_to_location(unsigned long handle, struct page **page,
Nitin Gupta61989a82012-01-09 16:51:56 -0600640 unsigned long *obj_idx)
641{
Minchan Kimc2344342012-06-08 15:39:25 +0900642 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
Olav Haugan67296872013-11-22 09:30:41 -0800643 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600644}
645
Minchan Kim2e40e162015-04-15 16:15:23 -0700646static unsigned long handle_to_obj(unsigned long handle)
647{
648 return *(unsigned long *)handle;
649}
650
Nitin Gupta61989a82012-01-09 16:51:56 -0600651static unsigned long obj_idx_to_offset(struct page *page,
652 unsigned long obj_idx, int class_size)
653{
654 unsigned long off = 0;
655
656 if (!is_first_page(page))
657 off = page->index;
658
659 return off + obj_idx * class_size;
660}
661
Nitin Guptaf4477e92012-04-02 09:13:56 -0500662static void reset_page(struct page *page)
663{
664 clear_bit(PG_private, &page->flags);
665 clear_bit(PG_private_2, &page->flags);
666 set_page_private(page, 0);
667 page->mapping = NULL;
668 page->freelist = NULL;
Mel Gorman22b751c2013-02-22 16:34:59 -0800669 page_mapcount_reset(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500670}
671
Nitin Gupta61989a82012-01-09 16:51:56 -0600672static void free_zspage(struct page *first_page)
673{
Nitin Guptaf4477e92012-04-02 09:13:56 -0500674 struct page *nextp, *tmp, *head_extra;
Nitin Gupta61989a82012-01-09 16:51:56 -0600675
676 BUG_ON(!is_first_page(first_page));
677 BUG_ON(first_page->inuse);
678
Nitin Guptaf4477e92012-04-02 09:13:56 -0500679 head_extra = (struct page *)page_private(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600680
Nitin Guptaf4477e92012-04-02 09:13:56 -0500681 reset_page(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600682 __free_page(first_page);
683
684 /* zspage with only 1 system page */
Nitin Guptaf4477e92012-04-02 09:13:56 -0500685 if (!head_extra)
Nitin Gupta61989a82012-01-09 16:51:56 -0600686 return;
687
Nitin Guptaf4477e92012-04-02 09:13:56 -0500688 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600689 list_del(&nextp->lru);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500690 reset_page(nextp);
Nitin Gupta61989a82012-01-09 16:51:56 -0600691 __free_page(nextp);
692 }
Nitin Guptaf4477e92012-04-02 09:13:56 -0500693 reset_page(head_extra);
694 __free_page(head_extra);
Nitin Gupta61989a82012-01-09 16:51:56 -0600695}
696
697/* Initialize a newly allocated zspage */
698static void init_zspage(struct page *first_page, struct size_class *class)
699{
700 unsigned long off = 0;
701 struct page *page = first_page;
702
703 BUG_ON(!is_first_page(first_page));
704 while (page) {
705 struct page *next_page;
706 struct link_free *link;
Dan Streetman5538c562014-10-09 15:30:01 -0700707 unsigned int i = 1;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800708 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600709
710 /*
711 * page->index stores offset of first object starting
712 * in the page. For the first page, this is always 0,
713 * so we use first_page->index (aka ->freelist) to store
714 * head of corresponding zspage's freelist.
715 */
716 if (page != first_page)
717 page->index = off;
718
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800719 vaddr = kmap_atomic(page);
720 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600721
Dan Streetman5538c562014-10-09 15:30:01 -0700722 while ((off += class->size) < PAGE_SIZE) {
723 link->next = obj_location_to_handle(page, i++);
724 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600725 }
726
727 /*
728 * We now come to the last (full or partial) object on this
729 * page, which must point to the first object on the next
730 * page (if present)
731 */
732 next_page = get_next_page(page);
733 link->next = obj_location_to_handle(next_page, 0);
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800734 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -0600735 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -0700736 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -0600737 }
738}
739
740/*
741 * Allocate a zspage for the given size class
742 */
743static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
744{
745 int i, error;
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500746 struct page *first_page = NULL, *uninitialized_var(prev_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600747
748 /*
749 * Allocate individual pages and link them together as:
750 * 1. first page->private = first sub-page
751 * 2. all sub-pages are linked together using page->lru
752 * 3. each sub-page is linked to the first page using page->first_page
753 *
754 * For each size class, First/Head pages are linked together using
755 * page->lru. Also, we set PG_private to identify the first page
756 * (i.e. no other sub-page has this flag set) and PG_private_2 to
757 * identify the last page.
758 */
759 error = -ENOMEM;
Minchan Kim2e3b61542012-05-03 15:40:39 +0900760 for (i = 0; i < class->pages_per_zspage; i++) {
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500761 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600762
763 page = alloc_page(flags);
764 if (!page)
765 goto cleanup;
766
767 INIT_LIST_HEAD(&page->lru);
768 if (i == 0) { /* first page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900769 SetPagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600770 set_page_private(page, 0);
771 first_page = page;
772 first_page->inuse = 0;
773 }
774 if (i == 1)
Sunghan Suhe842b972013-07-12 16:08:13 +0900775 set_page_private(first_page, (unsigned long)page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600776 if (i >= 1)
777 page->first_page = first_page;
778 if (i >= 2)
779 list_add(&page->lru, &prev_page->lru);
Minchan Kim2e3b61542012-05-03 15:40:39 +0900780 if (i == class->pages_per_zspage - 1) /* last page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900781 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600782 prev_page = page;
783 }
784
785 init_zspage(first_page, class);
786
787 first_page->freelist = obj_location_to_handle(first_page, 0);
788 /* Maximum number of objects we can store in this zspage */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900789 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600790
791 error = 0; /* Success */
792
793cleanup:
794 if (unlikely(error) && first_page) {
795 free_zspage(first_page);
796 first_page = NULL;
797 }
798
799 return first_page;
800}
801
802static struct page *find_get_zspage(struct size_class *class)
803{
804 int i;
805 struct page *page;
806
807 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
808 page = class->fullness_list[i];
809 if (page)
810 break;
811 }
812
813 return page;
814}
815
Minchan Kim1b945ae2013-12-11 11:04:36 +0900816#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500817static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -0500818{
Seth Jenningsf5536462012-07-18 11:55:56 -0500819 /*
820 * Make sure we don't leak memory if a cpu UP notification
821 * and zs_init() race and both call zs_cpu_up() on the same cpu
822 */
823 if (area->vm)
824 return 0;
825 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
826 if (!area->vm)
827 return -ENOMEM;
828 return 0;
829}
830
831static inline void __zs_cpu_down(struct mapping_area *area)
832{
833 if (area->vm)
834 free_vm_area(area->vm);
835 area->vm = NULL;
836}
837
838static inline void *__zs_map_object(struct mapping_area *area,
839 struct page *pages[2], int off, int size)
840{
WANG Chaof6f8ed42014-08-06 16:06:58 -0700841 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -0500842 area->vm_addr = area->vm->addr;
843 return area->vm_addr + off;
844}
845
846static inline void __zs_unmap_object(struct mapping_area *area,
847 struct page *pages[2], int off, int size)
848{
849 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500850
Joerg Roedeld95abbb2013-03-27 01:43:14 +0100851 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -0500852}
853
Minchan Kim1b945ae2013-12-11 11:04:36 +0900854#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -0500855
856static inline int __zs_cpu_up(struct mapping_area *area)
857{
858 /*
859 * Make sure we don't leak memory if a cpu UP notification
860 * and zs_init() race and both call zs_cpu_up() on the same cpu
861 */
862 if (area->vm_buf)
863 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800864 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -0500865 if (!area->vm_buf)
866 return -ENOMEM;
867 return 0;
868}
869
870static inline void __zs_cpu_down(struct mapping_area *area)
871{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800872 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -0500873 area->vm_buf = NULL;
874}
875
876static void *__zs_map_object(struct mapping_area *area,
877 struct page *pages[2], int off, int size)
878{
Seth Jennings5f6019022012-07-02 16:15:49 -0500879 int sizes[2];
880 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500881 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -0500882
Seth Jenningsf5536462012-07-18 11:55:56 -0500883 /* disable page faults to match kmap_atomic() return conditions */
884 pagefault_disable();
885
886 /* no read fastpath */
887 if (area->vm_mm == ZS_MM_WO)
888 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -0500889
890 sizes[0] = PAGE_SIZE - off;
891 sizes[1] = size - sizes[0];
892
Seth Jennings5f6019022012-07-02 16:15:49 -0500893 /* copy object to per-cpu buffer */
894 addr = kmap_atomic(pages[0]);
895 memcpy(buf, addr + off, sizes[0]);
896 kunmap_atomic(addr);
897 addr = kmap_atomic(pages[1]);
898 memcpy(buf + sizes[0], addr, sizes[1]);
899 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500900out:
901 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -0500902}
903
Seth Jenningsf5536462012-07-18 11:55:56 -0500904static void __zs_unmap_object(struct mapping_area *area,
905 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -0500906{
Seth Jennings5f6019022012-07-02 16:15:49 -0500907 int sizes[2];
908 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -0700909 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -0500910
Seth Jenningsf5536462012-07-18 11:55:56 -0500911 /* no write fastpath */
912 if (area->vm_mm == ZS_MM_RO)
913 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -0500914
Minchan Kim2e40e162015-04-15 16:15:23 -0700915 buf = area->vm_buf + ZS_HANDLE_SIZE;
916 size -= ZS_HANDLE_SIZE;
917 off += ZS_HANDLE_SIZE;
918
Seth Jennings5f6019022012-07-02 16:15:49 -0500919 sizes[0] = PAGE_SIZE - off;
920 sizes[1] = size - sizes[0];
921
922 /* copy per-cpu buffer to object */
923 addr = kmap_atomic(pages[0]);
924 memcpy(addr + off, buf, sizes[0]);
925 kunmap_atomic(addr);
926 addr = kmap_atomic(pages[1]);
927 memcpy(addr, buf + sizes[0], sizes[1]);
928 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500929
930out:
931 /* enable page faults to match kunmap_atomic() return conditions */
932 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -0500933}
Nitin Gupta61989a82012-01-09 16:51:56 -0600934
Minchan Kim1b945ae2013-12-11 11:04:36 +0900935#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -0500936
Nitin Gupta61989a82012-01-09 16:51:56 -0600937static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
938 void *pcpu)
939{
Seth Jenningsf5536462012-07-18 11:55:56 -0500940 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -0600941 struct mapping_area *area;
942
943 switch (action) {
944 case CPU_UP_PREPARE:
945 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500946 ret = __zs_cpu_up(area);
947 if (ret)
948 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -0600949 break;
950 case CPU_DEAD:
951 case CPU_UP_CANCELED:
952 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500953 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -0600954 break;
955 }
956
957 return NOTIFY_OK;
958}
959
960static struct notifier_block zs_cpu_nb = {
961 .notifier_call = zs_cpu_notifier
962};
963
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800964static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -0600965{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800966 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -0600967
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530968 cpu_notifier_register_begin();
969
970 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -0600971 for_each_online_cpu(cpu) {
972 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800973 if (notifier_to_errno(ret))
974 break;
Nitin Gupta61989a82012-01-09 16:51:56 -0600975 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530976
977 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800978 return notifier_to_errno(ret);
979}
980
Ganesh Mahendran66cdef62014-12-18 16:17:40 -0800981static void zs_unregister_cpu_notifier(void)
982{
983 int cpu;
984
985 cpu_notifier_register_begin();
986
987 for_each_online_cpu(cpu)
988 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
989 __unregister_cpu_notifier(&zs_cpu_nb);
990
991 cpu_notifier_register_done();
992}
993
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800994static void init_zs_size_classes(void)
995{
996 int nr;
997
998 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
999 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1000 nr += 1;
1001
1002 zs_size_classes = nr;
1003}
1004
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001005static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
1006{
1007 return pages_per_zspage * PAGE_SIZE / size;
1008}
1009
1010static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
1011{
1012 if (prev->pages_per_zspage != pages_per_zspage)
1013 return false;
1014
1015 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
1016 != get_maxobj_per_zspage(size, pages_per_zspage))
1017 return false;
1018
1019 return true;
1020}
1021
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001022#ifdef CONFIG_ZSMALLOC_STAT
1023
1024static inline void zs_stat_inc(struct size_class *class,
1025 enum zs_stat_type type, unsigned long cnt)
1026{
1027 class->stats.objs[type] += cnt;
1028}
1029
1030static inline void zs_stat_dec(struct size_class *class,
1031 enum zs_stat_type type, unsigned long cnt)
1032{
1033 class->stats.objs[type] -= cnt;
1034}
1035
1036static inline unsigned long zs_stat_get(struct size_class *class,
1037 enum zs_stat_type type)
1038{
1039 return class->stats.objs[type];
1040}
1041
1042static int __init zs_stat_init(void)
1043{
1044 if (!debugfs_initialized())
1045 return -ENODEV;
1046
1047 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
1048 if (!zs_stat_root)
1049 return -ENOMEM;
1050
1051 return 0;
1052}
1053
1054static void __exit zs_stat_exit(void)
1055{
1056 debugfs_remove_recursive(zs_stat_root);
1057}
1058
1059static int zs_stats_size_show(struct seq_file *s, void *v)
1060{
1061 int i;
1062 struct zs_pool *pool = s->private;
1063 struct size_class *class;
1064 int objs_per_zspage;
1065 unsigned long obj_allocated, obj_used, pages_used;
1066 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
1067
1068 seq_printf(s, " %5s %5s %13s %10s %10s\n", "class", "size",
1069 "obj_allocated", "obj_used", "pages_used");
1070
1071 for (i = 0; i < zs_size_classes; i++) {
1072 class = pool->size_class[i];
1073
1074 if (class->index != i)
1075 continue;
1076
1077 spin_lock(&class->lock);
1078 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
1079 obj_used = zs_stat_get(class, OBJ_USED);
1080 spin_unlock(&class->lock);
1081
1082 objs_per_zspage = get_maxobj_per_zspage(class->size,
1083 class->pages_per_zspage);
1084 pages_used = obj_allocated / objs_per_zspage *
1085 class->pages_per_zspage;
1086
1087 seq_printf(s, " %5u %5u %10lu %10lu %10lu\n", i,
1088 class->size, obj_allocated, obj_used, pages_used);
1089
1090 total_objs += obj_allocated;
1091 total_used_objs += obj_used;
1092 total_pages += pages_used;
1093 }
1094
1095 seq_puts(s, "\n");
1096 seq_printf(s, " %5s %5s %10lu %10lu %10lu\n", "Total", "",
1097 total_objs, total_used_objs, total_pages);
1098
1099 return 0;
1100}
1101
1102static int zs_stats_size_open(struct inode *inode, struct file *file)
1103{
1104 return single_open(file, zs_stats_size_show, inode->i_private);
1105}
1106
1107static const struct file_operations zs_stat_size_ops = {
1108 .open = zs_stats_size_open,
1109 .read = seq_read,
1110 .llseek = seq_lseek,
1111 .release = single_release,
1112};
1113
1114static int zs_pool_stat_create(char *name, struct zs_pool *pool)
1115{
1116 struct dentry *entry;
1117
1118 if (!zs_stat_root)
1119 return -ENODEV;
1120
1121 entry = debugfs_create_dir(name, zs_stat_root);
1122 if (!entry) {
1123 pr_warn("debugfs dir <%s> creation failed\n", name);
1124 return -ENOMEM;
1125 }
1126 pool->stat_dentry = entry;
1127
1128 entry = debugfs_create_file("obj_in_classes", S_IFREG | S_IRUGO,
1129 pool->stat_dentry, pool, &zs_stat_size_ops);
1130 if (!entry) {
1131 pr_warn("%s: debugfs file entry <%s> creation failed\n",
1132 name, "obj_in_classes");
1133 return -ENOMEM;
1134 }
1135
1136 return 0;
1137}
1138
1139static void zs_pool_stat_destroy(struct zs_pool *pool)
1140{
1141 debugfs_remove_recursive(pool->stat_dentry);
1142}
1143
1144#else /* CONFIG_ZSMALLOC_STAT */
1145
1146static inline void zs_stat_inc(struct size_class *class,
1147 enum zs_stat_type type, unsigned long cnt)
1148{
1149}
1150
1151static inline void zs_stat_dec(struct size_class *class,
1152 enum zs_stat_type type, unsigned long cnt)
1153{
1154}
1155
1156static inline unsigned long zs_stat_get(struct size_class *class,
1157 enum zs_stat_type type)
1158{
1159 return 0;
1160}
1161
1162static int __init zs_stat_init(void)
1163{
1164 return 0;
1165}
1166
1167static void __exit zs_stat_exit(void)
1168{
1169}
1170
1171static inline int zs_pool_stat_create(char *name, struct zs_pool *pool)
1172{
1173 return 0;
1174}
1175
1176static inline void zs_pool_stat_destroy(struct zs_pool *pool)
1177{
1178}
1179
1180#endif
1181
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001182unsigned long zs_get_total_pages(struct zs_pool *pool)
1183{
1184 return atomic_long_read(&pool->pages_allocated);
1185}
1186EXPORT_SYMBOL_GPL(zs_get_total_pages);
1187
1188/**
1189 * zs_map_object - get address of allocated object from handle.
1190 * @pool: pool from which the object was allocated
1191 * @handle: handle returned from zs_malloc
1192 *
1193 * Before using an object allocated from zs_malloc, it must be mapped using
1194 * this function. When done with the object, it must be unmapped using
1195 * zs_unmap_object.
1196 *
1197 * Only one object can be mapped per cpu at a time. There is no protection
1198 * against nested mappings.
1199 *
1200 * This function returns with preemption and page faults disabled.
1201 */
1202void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1203 enum zs_mapmode mm)
1204{
1205 struct page *page;
Minchan Kim2e40e162015-04-15 16:15:23 -07001206 unsigned long obj, obj_idx, off;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001207
1208 unsigned int class_idx;
1209 enum fullness_group fg;
1210 struct size_class *class;
1211 struct mapping_area *area;
1212 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001213 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001214
1215 BUG_ON(!handle);
1216
1217 /*
1218 * Because we use per-cpu mapping areas shared among the
1219 * pools/users, we can't allow mapping in interrupt context
1220 * because it can corrupt another users mappings.
1221 */
1222 BUG_ON(in_interrupt());
1223
Minchan Kim2e40e162015-04-15 16:15:23 -07001224 obj = handle_to_obj(handle);
1225 obj_to_location(obj, &page, &obj_idx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001226 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1227 class = pool->size_class[class_idx];
1228 off = obj_idx_to_offset(page, obj_idx, class->size);
1229
1230 area = &get_cpu_var(zs_map_area);
1231 area->vm_mm = mm;
1232 if (off + class->size <= PAGE_SIZE) {
1233 /* this object is contained entirely within a page */
1234 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001235 ret = area->vm_addr + off;
1236 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001237 }
1238
1239 /* this object spans two pages */
1240 pages[0] = page;
1241 pages[1] = get_next_page(page);
1242 BUG_ON(!pages[1]);
1243
Minchan Kim2e40e162015-04-15 16:15:23 -07001244 ret = __zs_map_object(area, pages, off, class->size);
1245out:
1246 return ret + ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001247}
1248EXPORT_SYMBOL_GPL(zs_map_object);
1249
1250void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1251{
1252 struct page *page;
Minchan Kim2e40e162015-04-15 16:15:23 -07001253 unsigned long obj, obj_idx, off;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001254
1255 unsigned int class_idx;
1256 enum fullness_group fg;
1257 struct size_class *class;
1258 struct mapping_area *area;
1259
1260 BUG_ON(!handle);
1261
Minchan Kim2e40e162015-04-15 16:15:23 -07001262 obj = handle_to_obj(handle);
1263 obj_to_location(obj, &page, &obj_idx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001264 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1265 class = pool->size_class[class_idx];
1266 off = obj_idx_to_offset(page, obj_idx, class->size);
1267
1268 area = this_cpu_ptr(&zs_map_area);
1269 if (off + class->size <= PAGE_SIZE)
1270 kunmap_atomic(area->vm_addr);
1271 else {
1272 struct page *pages[2];
1273
1274 pages[0] = page;
1275 pages[1] = get_next_page(page);
1276 BUG_ON(!pages[1]);
1277
1278 __zs_unmap_object(area, pages, off, class->size);
1279 }
1280 put_cpu_var(zs_map_area);
1281}
1282EXPORT_SYMBOL_GPL(zs_unmap_object);
1283
1284/**
1285 * zs_malloc - Allocate block of given size from pool.
1286 * @pool: pool to allocate from
1287 * @size: size of block to allocate
1288 *
1289 * On success, handle to the allocated object is returned,
1290 * otherwise 0.
1291 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1292 */
1293unsigned long zs_malloc(struct zs_pool *pool, size_t size)
1294{
Minchan Kim2e40e162015-04-15 16:15:23 -07001295 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001296 struct link_free *link;
1297 struct size_class *class;
1298 void *vaddr;
1299
1300 struct page *first_page, *m_page;
1301 unsigned long m_objidx, m_offset;
1302
Minchan Kim2e40e162015-04-15 16:15:23 -07001303 if (unlikely(!size || (size + ZS_HANDLE_SIZE) > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001304 return 0;
1305
Minchan Kim2e40e162015-04-15 16:15:23 -07001306 handle = alloc_handle(pool);
1307 if (!handle)
1308 return 0;
1309
1310 /* extra space in chunk to keep the handle */
1311 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001312 class = pool->size_class[get_size_class_index(size)];
1313
1314 spin_lock(&class->lock);
1315 first_page = find_get_zspage(class);
1316
1317 if (!first_page) {
1318 spin_unlock(&class->lock);
1319 first_page = alloc_zspage(class, pool->flags);
Minchan Kim2e40e162015-04-15 16:15:23 -07001320 if (unlikely(!first_page)) {
1321 free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001322 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -07001323 }
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001324
1325 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
1326 atomic_long_add(class->pages_per_zspage,
1327 &pool->pages_allocated);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001328
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001329 spin_lock(&class->lock);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001330 zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1331 class->size, class->pages_per_zspage));
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001332 }
1333
1334 obj = (unsigned long)first_page->freelist;
Minchan Kim2e40e162015-04-15 16:15:23 -07001335 obj_to_location(obj, &m_page, &m_objidx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001336 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
1337
1338 vaddr = kmap_atomic(m_page);
1339 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
1340 first_page->freelist = link->next;
Minchan Kim2e40e162015-04-15 16:15:23 -07001341
1342 /* record handle in the header of allocated chunk */
1343 link->handle = handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001344 kunmap_atomic(vaddr);
1345
1346 first_page->inuse++;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001347 zs_stat_inc(class, OBJ_USED, 1);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001348 /* Now move the zspage to another fullness group, if required */
1349 fix_fullness_group(pool, first_page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001350 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001351 spin_unlock(&class->lock);
1352
Minchan Kim2e40e162015-04-15 16:15:23 -07001353 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001354}
1355EXPORT_SYMBOL_GPL(zs_malloc);
1356
Minchan Kim2e40e162015-04-15 16:15:23 -07001357void zs_free(struct zs_pool *pool, unsigned long handle)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001358{
1359 struct link_free *link;
1360 struct page *first_page, *f_page;
Minchan Kim2e40e162015-04-15 16:15:23 -07001361 unsigned long obj, f_objidx, f_offset;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001362 void *vaddr;
1363
1364 int class_idx;
1365 struct size_class *class;
1366 enum fullness_group fullness;
1367
Minchan Kim2e40e162015-04-15 16:15:23 -07001368 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001369 return;
1370
Minchan Kim2e40e162015-04-15 16:15:23 -07001371 obj = handle_to_obj(handle);
1372 free_handle(pool, handle);
1373 obj_to_location(obj, &f_page, &f_objidx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001374 first_page = get_first_page(f_page);
1375
1376 get_zspage_mapping(first_page, &class_idx, &fullness);
1377 class = pool->size_class[class_idx];
1378 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
1379
1380 spin_lock(&class->lock);
1381
1382 /* Insert this object in containing zspage's freelist */
1383 vaddr = kmap_atomic(f_page);
1384 link = (struct link_free *)(vaddr + f_offset);
1385 link->next = first_page->freelist;
1386 kunmap_atomic(vaddr);
1387 first_page->freelist = (void *)obj;
1388
1389 first_page->inuse--;
1390 fullness = fix_fullness_group(pool, first_page);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001391
1392 zs_stat_dec(class, OBJ_USED, 1);
1393 if (fullness == ZS_EMPTY)
1394 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1395 class->size, class->pages_per_zspage));
1396
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001397 spin_unlock(&class->lock);
1398
1399 if (fullness == ZS_EMPTY) {
1400 atomic_long_sub(class->pages_per_zspage,
1401 &pool->pages_allocated);
1402 free_zspage(first_page);
1403 }
1404}
1405EXPORT_SYMBOL_GPL(zs_free);
1406
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001407/**
1408 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -06001409 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001410 *
1411 * This function must be called before anything when using
1412 * the zsmalloc allocator.
1413 *
1414 * On success, a pointer to the newly created pool is returned,
1415 * otherwise NULL.
1416 */
Ganesh Mahendran3eba0c62015-02-12 15:00:51 -08001417struct zs_pool *zs_create_pool(char *name, gfp_t flags)
Nitin Gupta61989a82012-01-09 16:51:56 -06001418{
Ganesh Mahendran18136652014-12-12 16:57:10 -08001419 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06001420 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001421 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001422
Ganesh Mahendran18136652014-12-12 16:57:10 -08001423 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001424 if (!pool)
1425 return NULL;
1426
Minchan Kim2e40e162015-04-15 16:15:23 -07001427 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
1428 GFP_KERNEL);
1429 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001430 kfree(pool);
1431 return NULL;
1432 }
1433
Minchan Kim2e40e162015-04-15 16:15:23 -07001434 pool->name = kstrdup(name, GFP_KERNEL);
1435 if (!pool->name)
1436 goto err;
1437
1438 if (create_handle_cache(pool))
1439 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001440
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001441 /*
1442 * Iterate reversly, because, size of size_class that we want to use
1443 * for merging should be larger or equal to current size.
1444 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001445 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001446 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001447 int pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001448 struct size_class *class;
1449
1450 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
1451 if (size > ZS_MAX_ALLOC_SIZE)
1452 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001453 pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001454
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001455 /*
1456 * size_class is used for normal zsmalloc operation such
1457 * as alloc/free for that size. Although it is natural that we
1458 * have one size_class for each size, there is a chance that we
1459 * can get more memory utilization if we use one size_class for
1460 * many different sizes whose size_class have same
1461 * characteristics. So, we makes size_class point to
1462 * previous size_class if possible.
1463 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001464 if (prev_class) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001465 if (can_merge(prev_class, size, pages_per_zspage)) {
1466 pool->size_class[i] = prev_class;
1467 continue;
1468 }
1469 }
1470
1471 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
1472 if (!class)
1473 goto err;
1474
Nitin Gupta61989a82012-01-09 16:51:56 -06001475 class->size = size;
1476 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001477 class->pages_per_zspage = pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001478 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001479 pool->size_class[i] = class;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001480
1481 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06001482 }
1483
Nitin Gupta61989a82012-01-09 16:51:56 -06001484 pool->flags = flags;
Nitin Gupta61989a82012-01-09 16:51:56 -06001485
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001486 if (zs_pool_stat_create(name, pool))
1487 goto err;
1488
Nitin Gupta61989a82012-01-09 16:51:56 -06001489 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001490
1491err:
1492 zs_destroy_pool(pool);
1493 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001494}
1495EXPORT_SYMBOL_GPL(zs_create_pool);
1496
1497void zs_destroy_pool(struct zs_pool *pool)
1498{
1499 int i;
1500
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001501 zs_pool_stat_destroy(pool);
1502
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001503 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001504 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001505 struct size_class *class = pool->size_class[i];
1506
1507 if (!class)
1508 continue;
1509
1510 if (class->index != i)
1511 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06001512
1513 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
1514 if (class->fullness_list[fg]) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04001515 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06001516 class->size, fg);
1517 }
1518 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001519 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06001520 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001521
Minchan Kim2e40e162015-04-15 16:15:23 -07001522 destroy_handle_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001523 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001524 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06001525 kfree(pool);
1526}
1527EXPORT_SYMBOL_GPL(zs_destroy_pool);
1528
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001529static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001530{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001531 int ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06001532
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001533 if (ret)
1534 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06001535
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001536 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06001537
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001538#ifdef CONFIG_ZPOOL
1539 zpool_register_driver(&zs_zpool_driver);
1540#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001541
1542 ret = zs_stat_init();
1543 if (ret) {
1544 pr_err("zs stat initialization failed\n");
1545 goto stat_fail;
1546 }
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001547 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001548
1549stat_fail:
1550#ifdef CONFIG_ZPOOL
1551 zpool_unregister_driver(&zs_zpool_driver);
1552#endif
1553notifier_fail:
1554 zs_unregister_cpu_notifier();
1555
1556 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06001557}
Nitin Gupta61989a82012-01-09 16:51:56 -06001558
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001559static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001560{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001561#ifdef CONFIG_ZPOOL
1562 zpool_unregister_driver(&zs_zpool_driver);
1563#endif
1564 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001565
1566 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06001567}
Ben Hutchings069f1012012-06-20 02:31:11 +01001568
1569module_init(zs_init);
1570module_exit(zs_exit);
1571
1572MODULE_LICENSE("Dual BSD/GPL");
1573MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");