blob: 61e180931ca8f2dd6710b5788a12392a77a99f43 [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>
Minchan Kimbcf16472014-01-30 15:45:50 -080094#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070095#include <linux/zpool.h>
Seth Jennings0959c632012-08-08 15:12:17 +090096
97/*
98 * This must be power of 2 and greater than of equal to sizeof(link_free).
99 * These two conditions ensure that any 'struct link_free' itself doesn't
100 * span more than 1 page which avoids complex case of mapping 2 pages simply
101 * to restore link_free pointer values.
102 */
103#define ZS_ALIGN 8
104
105/*
106 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
107 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
108 */
109#define ZS_MAX_ZSPAGE_ORDER 2
110#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
111
112/*
113 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900114 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +0900115 *
116 * Note that object index <obj_idx> is relative to system
117 * page <PFN> it is stored in, so for each sub-page belonging
118 * to a zspage, obj_idx starts with 0.
119 *
120 * This is made more complicated by various memory models and PAE.
121 */
122
123#ifndef MAX_PHYSMEM_BITS
124#ifdef CONFIG_HIGHMEM64G
125#define MAX_PHYSMEM_BITS 36
126#else /* !CONFIG_HIGHMEM64G */
127/*
128 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
129 * be PAGE_SHIFT
130 */
131#define MAX_PHYSMEM_BITS BITS_PER_LONG
132#endif
133#endif
134#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
135#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
136#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
137
138#define MAX(a, b) ((a) >= (b) ? (a) : (b))
139/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
140#define ZS_MIN_ALLOC_SIZE \
141 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
142#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
143
144/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700145 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900146 * trader-off here:
147 * - Large number of size classes is potentially wasteful as free page are
148 * spread across these classes
149 * - Small number of size classes causes large internal fragmentation
150 * - Probably its better to use specific size classes (empirically
151 * determined). NOTE: all those class sizes must be set as multiple of
152 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
153 *
154 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
155 * (reason above)
156 */
Seth Jenningsd662b8e2013-01-25 11:46:18 -0600157#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
Seth Jennings0959c632012-08-08 15:12:17 +0900158#define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
159 ZS_SIZE_CLASS_DELTA + 1)
160
161/*
162 * We do not maintain any list for completely empty or full pages
163 */
164enum fullness_group {
165 ZS_ALMOST_FULL,
166 ZS_ALMOST_EMPTY,
167 _ZS_NR_FULLNESS_GROUPS,
168
169 ZS_EMPTY,
170 ZS_FULL
171};
172
173/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800174 * number of size_classes
175 */
176static int zs_size_classes;
177
178/*
Seth Jennings0959c632012-08-08 15:12:17 +0900179 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
180 * n <= N / f, where
181 * n = number of allocated objects
182 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700183 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900184 *
185 * Similarly, we assign zspage to:
186 * ZS_ALMOST_FULL when n > N / f
187 * ZS_EMPTY when n == 0
188 * ZS_FULL when n == N
189 *
190 * (see: fix_fullness_group())
191 */
192static const int fullness_threshold_frac = 4;
193
194struct size_class {
195 /*
196 * Size of objects stored in this class. Must be multiple
197 * of ZS_ALIGN.
198 */
199 int size;
200 unsigned int index;
201
202 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
203 int pages_per_zspage;
204
205 spinlock_t lock;
206
Seth Jennings0959c632012-08-08 15:12:17 +0900207 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
208};
209
210/*
211 * Placed within free objects to form a singly linked list.
212 * For every zspage, first_page->freelist gives head of this list.
213 *
214 * This must be power of 2 and less than or equal to ZS_ALIGN
215 */
216struct link_free {
217 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
218 void *next;
219};
220
221struct zs_pool {
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800222 struct size_class **size_class;
Seth Jennings0959c632012-08-08 15:12:17 +0900223
224 gfp_t flags; /* allocation flags used when growing pool */
Minchan Kim13de8932014-10-09 15:29:48 -0700225 atomic_long_t pages_allocated;
Seth Jennings0959c632012-08-08 15:12:17 +0900226};
Nitin Gupta61989a82012-01-09 16:51:56 -0600227
228/*
229 * A zspage's class index and fullness group
230 * are encoded in its (first)page->mapping
231 */
232#define CLASS_IDX_BITS 28
233#define FULLNESS_BITS 4
234#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
235#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
236
Seth Jenningsf5536462012-07-18 11:55:56 -0500237struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900238#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500239 struct vm_struct *vm; /* vm area for mapping object that span pages */
240#else
241 char *vm_buf; /* copy buffer for objects that span pages */
242#endif
243 char *vm_addr; /* address of kmap_atomic()'ed pages */
244 enum zs_mapmode vm_mm; /* mapping mode */
245};
246
Dan Streetmanc7957792014-08-06 16:08:38 -0700247/* zpool driver */
248
249#ifdef CONFIG_ZPOOL
250
251static void *zs_zpool_create(gfp_t gfp, struct zpool_ops *zpool_ops)
252{
253 return zs_create_pool(gfp);
254}
255
256static void zs_zpool_destroy(void *pool)
257{
258 zs_destroy_pool(pool);
259}
260
261static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
262 unsigned long *handle)
263{
264 *handle = zs_malloc(pool, size);
265 return *handle ? 0 : -1;
266}
267static void zs_zpool_free(void *pool, unsigned long handle)
268{
269 zs_free(pool, handle);
270}
271
272static int zs_zpool_shrink(void *pool, unsigned int pages,
273 unsigned int *reclaimed)
274{
275 return -EINVAL;
276}
277
278static void *zs_zpool_map(void *pool, unsigned long handle,
279 enum zpool_mapmode mm)
280{
281 enum zs_mapmode zs_mm;
282
283 switch (mm) {
284 case ZPOOL_MM_RO:
285 zs_mm = ZS_MM_RO;
286 break;
287 case ZPOOL_MM_WO:
288 zs_mm = ZS_MM_WO;
289 break;
290 case ZPOOL_MM_RW: /* fallthru */
291 default:
292 zs_mm = ZS_MM_RW;
293 break;
294 }
295
296 return zs_map_object(pool, handle, zs_mm);
297}
298static void zs_zpool_unmap(void *pool, unsigned long handle)
299{
300 zs_unmap_object(pool, handle);
301}
302
303static u64 zs_zpool_total_size(void *pool)
304{
Minchan Kim722cdc12014-10-09 15:29:50 -0700305 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700306}
307
308static struct zpool_driver zs_zpool_driver = {
309 .type = "zsmalloc",
310 .owner = THIS_MODULE,
311 .create = zs_zpool_create,
312 .destroy = zs_zpool_destroy,
313 .malloc = zs_zpool_malloc,
314 .free = zs_zpool_free,
315 .shrink = zs_zpool_shrink,
316 .map = zs_zpool_map,
317 .unmap = zs_zpool_unmap,
318 .total_size = zs_zpool_total_size,
319};
320
Kees Cook137f8cf2014-08-29 15:18:40 -0700321MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700322#endif /* CONFIG_ZPOOL */
323
Nitin Gupta61989a82012-01-09 16:51:56 -0600324/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
325static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
326
327static int is_first_page(struct page *page)
328{
Minchan Kima27545bf2012-04-25 15:23:09 +0900329 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600330}
331
332static int is_last_page(struct page *page)
333{
Minchan Kima27545bf2012-04-25 15:23:09 +0900334 return PagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600335}
336
337static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
338 enum fullness_group *fullness)
339{
340 unsigned long m;
341 BUG_ON(!is_first_page(page));
342
343 m = (unsigned long)page->mapping;
344 *fullness = m & FULLNESS_MASK;
345 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
346}
347
348static void set_zspage_mapping(struct page *page, unsigned int class_idx,
349 enum fullness_group fullness)
350{
351 unsigned long m;
352 BUG_ON(!is_first_page(page));
353
354 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
355 (fullness & FULLNESS_MASK);
356 page->mapping = (struct address_space *)m;
357}
358
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900359/*
360 * zsmalloc divides the pool into various size classes where each
361 * class maintains a list of zspages where each zspage is divided
362 * into equal sized chunks. Each allocation falls into one of these
363 * classes depending on its size. This function returns index of the
364 * size class which has chunk size big enough to hold the give size.
365 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600366static int get_size_class_index(int size)
367{
368 int idx = 0;
369
370 if (likely(size > ZS_MIN_ALLOC_SIZE))
371 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
372 ZS_SIZE_CLASS_DELTA);
373
374 return idx;
375}
376
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900377/*
378 * For each size class, zspages are divided into different groups
379 * depending on how "full" they are. This was done so that we could
380 * easily find empty or nearly empty zspages when we try to shrink
381 * the pool (not yet implemented). This function returns fullness
382 * status of the given page.
383 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600384static enum fullness_group get_fullness_group(struct page *page)
385{
386 int inuse, max_objects;
387 enum fullness_group fg;
388 BUG_ON(!is_first_page(page));
389
390 inuse = page->inuse;
391 max_objects = page->objects;
392
393 if (inuse == 0)
394 fg = ZS_EMPTY;
395 else if (inuse == max_objects)
396 fg = ZS_FULL;
397 else if (inuse <= max_objects / fullness_threshold_frac)
398 fg = ZS_ALMOST_EMPTY;
399 else
400 fg = ZS_ALMOST_FULL;
401
402 return fg;
403}
404
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900405/*
406 * Each size class maintains various freelists and zspages are assigned
407 * to one of these freelists based on the number of live objects they
408 * have. This functions inserts the given zspage into the freelist
409 * identified by <class, fullness_group>.
410 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600411static void insert_zspage(struct page *page, struct size_class *class,
412 enum fullness_group fullness)
413{
414 struct page **head;
415
416 BUG_ON(!is_first_page(page));
417
418 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
419 return;
420
421 head = &class->fullness_list[fullness];
422 if (*head)
423 list_add_tail(&page->lru, &(*head)->lru);
424
425 *head = page;
426}
427
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900428/*
429 * This function removes the given zspage from the freelist identified
430 * by <class, fullness_group>.
431 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600432static void remove_zspage(struct page *page, struct size_class *class,
433 enum fullness_group fullness)
434{
435 struct page **head;
436
437 BUG_ON(!is_first_page(page));
438
439 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
440 return;
441
442 head = &class->fullness_list[fullness];
443 BUG_ON(!*head);
444 if (list_empty(&(*head)->lru))
445 *head = NULL;
446 else if (*head == page)
447 *head = (struct page *)list_entry((*head)->lru.next,
448 struct page, lru);
449
450 list_del_init(&page->lru);
451}
452
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900453/*
454 * Each size class maintains zspages in different fullness groups depending
455 * on the number of live objects they contain. When allocating or freeing
456 * objects, the fullness status of the page can change, say, from ALMOST_FULL
457 * to ALMOST_EMPTY when freeing an object. This function checks if such
458 * a status change has occurred for the given page and accordingly moves the
459 * page from the freelist of the old fullness group to that of the new
460 * fullness group.
461 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600462static enum fullness_group fix_fullness_group(struct zs_pool *pool,
463 struct page *page)
464{
465 int class_idx;
466 struct size_class *class;
467 enum fullness_group currfg, newfg;
468
469 BUG_ON(!is_first_page(page));
470
471 get_zspage_mapping(page, &class_idx, &currfg);
472 newfg = get_fullness_group(page);
473 if (newfg == currfg)
474 goto out;
475
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -0800476 class = pool->size_class[class_idx];
Nitin Gupta61989a82012-01-09 16:51:56 -0600477 remove_zspage(page, class, currfg);
478 insert_zspage(page, class, newfg);
479 set_zspage_mapping(page, class_idx, newfg);
480
481out:
482 return newfg;
483}
484
485/*
486 * We have to decide on how many pages to link together
487 * to form a zspage for each size class. This is important
488 * to reduce wastage due to unusable space left at end of
489 * each zspage which is given as:
490 * wastage = Zp - Zp % size_class
491 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
492 *
493 * For example, for size class of 3/8 * PAGE_SIZE, we should
494 * link together 3 PAGE_SIZE sized pages to form a zspage
495 * since then we can perfectly fit in 8 such objects.
496 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900497static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600498{
499 int i, max_usedpc = 0;
500 /* zspage order which gives maximum used size per KB */
501 int max_usedpc_order = 1;
502
Seth Jennings84d4faa2012-03-05 11:33:21 -0600503 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600504 int zspage_size;
505 int waste, usedpc;
506
507 zspage_size = i * PAGE_SIZE;
508 waste = zspage_size % class_size;
509 usedpc = (zspage_size - waste) * 100 / zspage_size;
510
511 if (usedpc > max_usedpc) {
512 max_usedpc = usedpc;
513 max_usedpc_order = i;
514 }
515 }
516
517 return max_usedpc_order;
518}
519
520/*
521 * A single 'zspage' is composed of many system pages which are
522 * linked together using fields in struct page. This function finds
523 * the first/head page, given any component page of a zspage.
524 */
525static struct page *get_first_page(struct page *page)
526{
527 if (is_first_page(page))
528 return page;
529 else
530 return page->first_page;
531}
532
533static struct page *get_next_page(struct page *page)
534{
535 struct page *next;
536
537 if (is_last_page(page))
538 next = NULL;
539 else if (is_first_page(page))
Sunghan Suhe842b972013-07-12 16:08:13 +0900540 next = (struct page *)page_private(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600541 else
542 next = list_entry(page->lru.next, struct page, lru);
543
544 return next;
545}
546
Olav Haugan67296872013-11-22 09:30:41 -0800547/*
548 * Encode <page, obj_idx> as a single handle value.
549 * On hardware platforms with physical memory starting at 0x0 the pfn
550 * could be 0 so we ensure that the handle will never be 0 by adjusting the
551 * encoded obj_idx value before encoding.
552 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600553static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
554{
555 unsigned long handle;
556
557 if (!page) {
558 BUG_ON(obj_idx);
559 return NULL;
560 }
561
562 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
Olav Haugan67296872013-11-22 09:30:41 -0800563 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600564
565 return (void *)handle;
566}
567
Olav Haugan67296872013-11-22 09:30:41 -0800568/*
569 * Decode <page, obj_idx> pair from the given object handle. We adjust the
570 * decoded obj_idx back to its original value since it was adjusted in
571 * obj_location_to_handle().
572 */
Minchan Kimc2344342012-06-08 15:39:25 +0900573static void obj_handle_to_location(unsigned long handle, struct page **page,
Nitin Gupta61989a82012-01-09 16:51:56 -0600574 unsigned long *obj_idx)
575{
Minchan Kimc2344342012-06-08 15:39:25 +0900576 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
Olav Haugan67296872013-11-22 09:30:41 -0800577 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600578}
579
580static unsigned long obj_idx_to_offset(struct page *page,
581 unsigned long obj_idx, int class_size)
582{
583 unsigned long off = 0;
584
585 if (!is_first_page(page))
586 off = page->index;
587
588 return off + obj_idx * class_size;
589}
590
Nitin Guptaf4477e92012-04-02 09:13:56 -0500591static void reset_page(struct page *page)
592{
593 clear_bit(PG_private, &page->flags);
594 clear_bit(PG_private_2, &page->flags);
595 set_page_private(page, 0);
596 page->mapping = NULL;
597 page->freelist = NULL;
Mel Gorman22b751c2013-02-22 16:34:59 -0800598 page_mapcount_reset(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500599}
600
Nitin Gupta61989a82012-01-09 16:51:56 -0600601static void free_zspage(struct page *first_page)
602{
Nitin Guptaf4477e92012-04-02 09:13:56 -0500603 struct page *nextp, *tmp, *head_extra;
Nitin Gupta61989a82012-01-09 16:51:56 -0600604
605 BUG_ON(!is_first_page(first_page));
606 BUG_ON(first_page->inuse);
607
Nitin Guptaf4477e92012-04-02 09:13:56 -0500608 head_extra = (struct page *)page_private(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600609
Nitin Guptaf4477e92012-04-02 09:13:56 -0500610 reset_page(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600611 __free_page(first_page);
612
613 /* zspage with only 1 system page */
Nitin Guptaf4477e92012-04-02 09:13:56 -0500614 if (!head_extra)
Nitin Gupta61989a82012-01-09 16:51:56 -0600615 return;
616
Nitin Guptaf4477e92012-04-02 09:13:56 -0500617 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600618 list_del(&nextp->lru);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500619 reset_page(nextp);
Nitin Gupta61989a82012-01-09 16:51:56 -0600620 __free_page(nextp);
621 }
Nitin Guptaf4477e92012-04-02 09:13:56 -0500622 reset_page(head_extra);
623 __free_page(head_extra);
Nitin Gupta61989a82012-01-09 16:51:56 -0600624}
625
626/* Initialize a newly allocated zspage */
627static void init_zspage(struct page *first_page, struct size_class *class)
628{
629 unsigned long off = 0;
630 struct page *page = first_page;
631
632 BUG_ON(!is_first_page(first_page));
633 while (page) {
634 struct page *next_page;
635 struct link_free *link;
Dan Streetman5538c562014-10-09 15:30:01 -0700636 unsigned int i = 1;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800637 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600638
639 /*
640 * page->index stores offset of first object starting
641 * in the page. For the first page, this is always 0,
642 * so we use first_page->index (aka ->freelist) to store
643 * head of corresponding zspage's freelist.
644 */
645 if (page != first_page)
646 page->index = off;
647
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800648 vaddr = kmap_atomic(page);
649 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600650
Dan Streetman5538c562014-10-09 15:30:01 -0700651 while ((off += class->size) < PAGE_SIZE) {
652 link->next = obj_location_to_handle(page, i++);
653 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600654 }
655
656 /*
657 * We now come to the last (full or partial) object on this
658 * page, which must point to the first object on the next
659 * page (if present)
660 */
661 next_page = get_next_page(page);
662 link->next = obj_location_to_handle(next_page, 0);
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800663 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -0600664 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -0700665 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -0600666 }
667}
668
669/*
670 * Allocate a zspage for the given size class
671 */
672static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
673{
674 int i, error;
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500675 struct page *first_page = NULL, *uninitialized_var(prev_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600676
677 /*
678 * Allocate individual pages and link them together as:
679 * 1. first page->private = first sub-page
680 * 2. all sub-pages are linked together using page->lru
681 * 3. each sub-page is linked to the first page using page->first_page
682 *
683 * For each size class, First/Head pages are linked together using
684 * page->lru. Also, we set PG_private to identify the first page
685 * (i.e. no other sub-page has this flag set) and PG_private_2 to
686 * identify the last page.
687 */
688 error = -ENOMEM;
Minchan Kim2e3b6152012-05-03 15:40:39 +0900689 for (i = 0; i < class->pages_per_zspage; i++) {
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500690 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600691
692 page = alloc_page(flags);
693 if (!page)
694 goto cleanup;
695
696 INIT_LIST_HEAD(&page->lru);
697 if (i == 0) { /* first page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900698 SetPagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600699 set_page_private(page, 0);
700 first_page = page;
701 first_page->inuse = 0;
702 }
703 if (i == 1)
Sunghan Suhe842b972013-07-12 16:08:13 +0900704 set_page_private(first_page, (unsigned long)page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600705 if (i >= 1)
706 page->first_page = first_page;
707 if (i >= 2)
708 list_add(&page->lru, &prev_page->lru);
Minchan Kim2e3b6152012-05-03 15:40:39 +0900709 if (i == class->pages_per_zspage - 1) /* last page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900710 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600711 prev_page = page;
712 }
713
714 init_zspage(first_page, class);
715
716 first_page->freelist = obj_location_to_handle(first_page, 0);
717 /* Maximum number of objects we can store in this zspage */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900718 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600719
720 error = 0; /* Success */
721
722cleanup:
723 if (unlikely(error) && first_page) {
724 free_zspage(first_page);
725 first_page = NULL;
726 }
727
728 return first_page;
729}
730
731static struct page *find_get_zspage(struct size_class *class)
732{
733 int i;
734 struct page *page;
735
736 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
737 page = class->fullness_list[i];
738 if (page)
739 break;
740 }
741
742 return page;
743}
744
Minchan Kim1b945ae2013-12-11 11:04:36 +0900745#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500746static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -0500747{
Seth Jenningsf5536462012-07-18 11:55:56 -0500748 /*
749 * Make sure we don't leak memory if a cpu UP notification
750 * and zs_init() race and both call zs_cpu_up() on the same cpu
751 */
752 if (area->vm)
753 return 0;
754 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
755 if (!area->vm)
756 return -ENOMEM;
757 return 0;
758}
759
760static inline void __zs_cpu_down(struct mapping_area *area)
761{
762 if (area->vm)
763 free_vm_area(area->vm);
764 area->vm = NULL;
765}
766
767static inline void *__zs_map_object(struct mapping_area *area,
768 struct page *pages[2], int off, int size)
769{
WANG Chaof6f8ed42014-08-06 16:06:58 -0700770 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -0500771 area->vm_addr = area->vm->addr;
772 return area->vm_addr + off;
773}
774
775static inline void __zs_unmap_object(struct mapping_area *area,
776 struct page *pages[2], int off, int size)
777{
778 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500779
Joerg Roedeld95abbb2013-03-27 01:43:14 +0100780 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -0500781}
782
Minchan Kim1b945ae2013-12-11 11:04:36 +0900783#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -0500784
785static inline int __zs_cpu_up(struct mapping_area *area)
786{
787 /*
788 * Make sure we don't leak memory if a cpu UP notification
789 * and zs_init() race and both call zs_cpu_up() on the same cpu
790 */
791 if (area->vm_buf)
792 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800793 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -0500794 if (!area->vm_buf)
795 return -ENOMEM;
796 return 0;
797}
798
799static inline void __zs_cpu_down(struct mapping_area *area)
800{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800801 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -0500802 area->vm_buf = NULL;
803}
804
805static void *__zs_map_object(struct mapping_area *area,
806 struct page *pages[2], int off, int size)
807{
Seth Jennings5f601902012-07-02 16:15:49 -0500808 int sizes[2];
809 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500810 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500811
Seth Jenningsf5536462012-07-18 11:55:56 -0500812 /* disable page faults to match kmap_atomic() return conditions */
813 pagefault_disable();
814
815 /* no read fastpath */
816 if (area->vm_mm == ZS_MM_WO)
817 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -0500818
819 sizes[0] = PAGE_SIZE - off;
820 sizes[1] = size - sizes[0];
821
Seth Jennings5f601902012-07-02 16:15:49 -0500822 /* copy object to per-cpu buffer */
823 addr = kmap_atomic(pages[0]);
824 memcpy(buf, addr + off, sizes[0]);
825 kunmap_atomic(addr);
826 addr = kmap_atomic(pages[1]);
827 memcpy(buf + sizes[0], addr, sizes[1]);
828 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500829out:
830 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500831}
832
Seth Jenningsf5536462012-07-18 11:55:56 -0500833static void __zs_unmap_object(struct mapping_area *area,
834 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -0500835{
Seth Jennings5f601902012-07-02 16:15:49 -0500836 int sizes[2];
837 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500838 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500839
Seth Jenningsf5536462012-07-18 11:55:56 -0500840 /* no write fastpath */
841 if (area->vm_mm == ZS_MM_RO)
842 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -0500843
844 sizes[0] = PAGE_SIZE - off;
845 sizes[1] = size - sizes[0];
846
847 /* copy per-cpu buffer to object */
848 addr = kmap_atomic(pages[0]);
849 memcpy(addr + off, buf, sizes[0]);
850 kunmap_atomic(addr);
851 addr = kmap_atomic(pages[1]);
852 memcpy(addr, buf + sizes[0], sizes[1]);
853 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500854
855out:
856 /* enable page faults to match kunmap_atomic() return conditions */
857 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -0500858}
Nitin Gupta61989a82012-01-09 16:51:56 -0600859
Minchan Kim1b945ae2013-12-11 11:04:36 +0900860#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -0500861
Nitin Gupta61989a82012-01-09 16:51:56 -0600862static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
863 void *pcpu)
864{
Seth Jenningsf5536462012-07-18 11:55:56 -0500865 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -0600866 struct mapping_area *area;
867
868 switch (action) {
869 case CPU_UP_PREPARE:
870 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500871 ret = __zs_cpu_up(area);
872 if (ret)
873 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -0600874 break;
875 case CPU_DEAD:
876 case CPU_UP_CANCELED:
877 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500878 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -0600879 break;
880 }
881
882 return NOTIFY_OK;
883}
884
885static struct notifier_block zs_cpu_nb = {
886 .notifier_call = zs_cpu_notifier
887};
888
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800889static void zs_unregister_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -0600890{
891 int cpu;
892
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530893 cpu_notifier_register_begin();
894
Nitin Gupta61989a82012-01-09 16:51:56 -0600895 for_each_online_cpu(cpu)
896 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530897 __unregister_cpu_notifier(&zs_cpu_nb);
898
899 cpu_notifier_register_done();
Nitin Gupta61989a82012-01-09 16:51:56 -0600900}
901
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800902static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -0600903{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800904 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -0600905
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530906 cpu_notifier_register_begin();
907
908 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -0600909 for_each_online_cpu(cpu) {
910 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800911 if (notifier_to_errno(ret))
912 break;
Nitin Gupta61989a82012-01-09 16:51:56 -0600913 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530914
915 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800916 return notifier_to_errno(ret);
917}
918
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800919static void init_zs_size_classes(void)
920{
921 int nr;
922
923 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
924 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
925 nr += 1;
926
927 zs_size_classes = nr;
928}
929
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -0800930static void __exit zs_exit(void)
931{
932#ifdef CONFIG_ZPOOL
933 zpool_unregister_driver(&zs_zpool_driver);
934#endif
935 zs_unregister_cpu_notifier();
936}
937
938static int __init zs_init(void)
939{
940 int ret = zs_register_cpu_notifier();
941
942 if (ret) {
943 zs_unregister_cpu_notifier();
944 return ret;
945 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +0530946
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800947 init_zs_size_classes();
948
Dan Streetmanc7957792014-08-06 16:08:38 -0700949#ifdef CONFIG_ZPOOL
950 zpool_register_driver(&zs_zpool_driver);
951#endif
Nitin Gupta61989a82012-01-09 16:51:56 -0600952 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600953}
954
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -0800955static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
956{
957 return pages_per_zspage * PAGE_SIZE / size;
958}
959
960static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
961{
962 if (prev->pages_per_zspage != pages_per_zspage)
963 return false;
964
965 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
966 != get_maxobj_per_zspage(size, pages_per_zspage))
967 return false;
968
969 return true;
970}
971
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -0800972/**
973 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -0600974 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -0800975 *
976 * This function must be called before anything when using
977 * the zsmalloc allocator.
978 *
979 * On success, a pointer to the newly created pool is returned,
980 * otherwise NULL.
981 */
Seth Jennings0d145a52013-01-30 09:36:52 -0600982struct zs_pool *zs_create_pool(gfp_t flags)
Nitin Gupta61989a82012-01-09 16:51:56 -0600983{
Ben Hutchings069f1012012-06-20 02:31:11 +0100984 int i, ovhd_size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600985 struct zs_pool *pool;
986
Nitin Gupta61989a82012-01-09 16:51:56 -0600987 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
988 pool = kzalloc(ovhd_size, GFP_KERNEL);
989 if (!pool)
990 return NULL;
991
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800992 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
993 GFP_KERNEL);
994 if (!pool->size_class) {
995 kfree(pool);
996 return NULL;
997 }
998
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -0800999 /*
1000 * Iterate reversly, because, size of size_class that we want to use
1001 * for merging should be larger or equal to current size.
1002 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001003 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001004 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001005 int pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001006 struct size_class *class;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001007 struct size_class *prev_class;
Nitin Gupta61989a82012-01-09 16:51:56 -06001008
1009 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
1010 if (size > ZS_MAX_ALLOC_SIZE)
1011 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001012 pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001013
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001014 /*
1015 * size_class is used for normal zsmalloc operation such
1016 * as alloc/free for that size. Although it is natural that we
1017 * have one size_class for each size, there is a chance that we
1018 * can get more memory utilization if we use one size_class for
1019 * many different sizes whose size_class have same
1020 * characteristics. So, we makes size_class point to
1021 * previous size_class if possible.
1022 */
1023 if (i < ZS_SIZE_CLASSES - 1) {
1024 prev_class = pool->size_class[i + 1];
1025 if (can_merge(prev_class, size, pages_per_zspage)) {
1026 pool->size_class[i] = prev_class;
1027 continue;
1028 }
1029 }
1030
1031 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
1032 if (!class)
1033 goto err;
1034
Nitin Gupta61989a82012-01-09 16:51:56 -06001035 class->size = size;
1036 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001037 class->pages_per_zspage = pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001038 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001039 pool->size_class[i] = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06001040 }
1041
Nitin Gupta61989a82012-01-09 16:51:56 -06001042 pool->flags = flags;
Nitin Gupta61989a82012-01-09 16:51:56 -06001043
Nitin Gupta61989a82012-01-09 16:51:56 -06001044 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001045
1046err:
1047 zs_destroy_pool(pool);
1048 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001049}
1050EXPORT_SYMBOL_GPL(zs_create_pool);
1051
1052void zs_destroy_pool(struct zs_pool *pool)
1053{
1054 int i;
1055
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001056 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001057 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001058 struct size_class *class = pool->size_class[i];
1059
1060 if (!class)
1061 continue;
1062
1063 if (class->index != i)
1064 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06001065
1066 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
1067 if (class->fullness_list[fg]) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04001068 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06001069 class->size, fg);
1070 }
1071 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001072 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06001073 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001074
1075 kfree(pool->size_class);
Nitin Gupta61989a82012-01-09 16:51:56 -06001076 kfree(pool);
1077}
1078EXPORT_SYMBOL_GPL(zs_destroy_pool);
1079
1080/**
1081 * zs_malloc - Allocate block of given size from pool.
1082 * @pool: pool to allocate from
1083 * @size: size of block to allocate
Nitin Gupta61989a82012-01-09 16:51:56 -06001084 *
Minchan Kim00a61d82012-05-03 15:40:40 +09001085 * On success, handle to the allocated object is returned,
Minchan Kimc2344342012-06-08 15:39:25 +09001086 * otherwise 0.
Nitin Gupta61989a82012-01-09 16:51:56 -06001087 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1088 */
Minchan Kimc2344342012-06-08 15:39:25 +09001089unsigned long zs_malloc(struct zs_pool *pool, size_t size)
Nitin Gupta61989a82012-01-09 16:51:56 -06001090{
Minchan Kimc2344342012-06-08 15:39:25 +09001091 unsigned long obj;
Nitin Gupta61989a82012-01-09 16:51:56 -06001092 struct link_free *link;
Nitin Gupta61989a82012-01-09 16:51:56 -06001093 struct size_class *class;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001094 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001095
1096 struct page *first_page, *m_page;
1097 unsigned long m_objidx, m_offset;
1098
1099 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Minchan Kimc2344342012-06-08 15:39:25 +09001100 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06001101
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001102 class = pool->size_class[get_size_class_index(size)];
Nitin Gupta61989a82012-01-09 16:51:56 -06001103
1104 spin_lock(&class->lock);
1105 first_page = find_get_zspage(class);
1106
1107 if (!first_page) {
1108 spin_unlock(&class->lock);
1109 first_page = alloc_zspage(class, pool->flags);
1110 if (unlikely(!first_page))
Minchan Kimc2344342012-06-08 15:39:25 +09001111 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06001112
1113 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
Minchan Kim13de8932014-10-09 15:29:48 -07001114 atomic_long_add(class->pages_per_zspage,
1115 &pool->pages_allocated);
Nitin Gupta61989a82012-01-09 16:51:56 -06001116 spin_lock(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -06001117 }
1118
Minchan Kimc2344342012-06-08 15:39:25 +09001119 obj = (unsigned long)first_page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -06001120 obj_handle_to_location(obj, &m_page, &m_objidx);
1121 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
1122
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001123 vaddr = kmap_atomic(m_page);
1124 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001125 first_page->freelist = link->next;
1126 memset(link, POISON_INUSE, sizeof(*link));
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001127 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001128
1129 first_page->inuse++;
1130 /* Now move the zspage to another fullness group, if required */
1131 fix_fullness_group(pool, first_page);
1132 spin_unlock(&class->lock);
1133
1134 return obj;
1135}
1136EXPORT_SYMBOL_GPL(zs_malloc);
1137
Minchan Kimc2344342012-06-08 15:39:25 +09001138void zs_free(struct zs_pool *pool, unsigned long obj)
Nitin Gupta61989a82012-01-09 16:51:56 -06001139{
1140 struct link_free *link;
1141 struct page *first_page, *f_page;
1142 unsigned long f_objidx, f_offset;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001143 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001144
1145 int class_idx;
1146 struct size_class *class;
1147 enum fullness_group fullness;
1148
1149 if (unlikely(!obj))
1150 return;
1151
1152 obj_handle_to_location(obj, &f_page, &f_objidx);
1153 first_page = get_first_page(f_page);
1154
1155 get_zspage_mapping(first_page, &class_idx, &fullness);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001156 class = pool->size_class[class_idx];
Nitin Gupta61989a82012-01-09 16:51:56 -06001157 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
1158
1159 spin_lock(&class->lock);
1160
1161 /* Insert this object in containing zspage's freelist */
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001162 vaddr = kmap_atomic(f_page);
1163 link = (struct link_free *)(vaddr + f_offset);
Nitin Gupta61989a82012-01-09 16:51:56 -06001164 link->next = first_page->freelist;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001165 kunmap_atomic(vaddr);
Minchan Kimc2344342012-06-08 15:39:25 +09001166 first_page->freelist = (void *)obj;
Nitin Gupta61989a82012-01-09 16:51:56 -06001167
1168 first_page->inuse--;
1169 fullness = fix_fullness_group(pool, first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001170 spin_unlock(&class->lock);
1171
Minchan Kim13de8932014-10-09 15:29:48 -07001172 if (fullness == ZS_EMPTY) {
1173 atomic_long_sub(class->pages_per_zspage,
1174 &pool->pages_allocated);
Nitin Gupta61989a82012-01-09 16:51:56 -06001175 free_zspage(first_page);
Minchan Kim13de8932014-10-09 15:29:48 -07001176 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001177}
1178EXPORT_SYMBOL_GPL(zs_free);
1179
Minchan Kim00a61d82012-05-03 15:40:40 +09001180/**
1181 * zs_map_object - get address of allocated object from handle.
1182 * @pool: pool from which the object was allocated
1183 * @handle: handle returned from zs_malloc
1184 *
1185 * Before using an object allocated from zs_malloc, it must be mapped using
1186 * this function. When done with the object, it must be unmapped using
Seth Jennings166cfda2012-07-02 16:15:51 -05001187 * zs_unmap_object.
1188 *
1189 * Only one object can be mapped per cpu at a time. There is no protection
1190 * against nested mappings.
1191 *
1192 * This function returns with preemption and page faults disabled.
Sara Bird396b7fd2013-05-20 15:18:14 -04001193 */
Seth Jenningsb7418512012-07-02 16:15:52 -05001194void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1195 enum zs_mapmode mm)
Nitin Gupta61989a82012-01-09 16:51:56 -06001196{
1197 struct page *page;
1198 unsigned long obj_idx, off;
1199
1200 unsigned int class_idx;
1201 enum fullness_group fg;
1202 struct size_class *class;
1203 struct mapping_area *area;
Seth Jenningsf5536462012-07-18 11:55:56 -05001204 struct page *pages[2];
Nitin Gupta61989a82012-01-09 16:51:56 -06001205
1206 BUG_ON(!handle);
1207
Seth Jenningsc60369f2012-07-18 11:55:55 -05001208 /*
1209 * Because we use per-cpu mapping areas shared among the
1210 * pools/users, we can't allow mapping in interrupt context
1211 * because it can corrupt another users mappings.
1212 */
1213 BUG_ON(in_interrupt());
1214
Nitin Gupta61989a82012-01-09 16:51:56 -06001215 obj_handle_to_location(handle, &page, &obj_idx);
1216 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001217 class = pool->size_class[class_idx];
Nitin Gupta61989a82012-01-09 16:51:56 -06001218 off = obj_idx_to_offset(page, obj_idx, class->size);
1219
1220 area = &get_cpu_var(zs_map_area);
Seth Jenningsf5536462012-07-18 11:55:56 -05001221 area->vm_mm = mm;
Nitin Gupta61989a82012-01-09 16:51:56 -06001222 if (off + class->size <= PAGE_SIZE) {
1223 /* this object is contained entirely within a page */
1224 area->vm_addr = kmap_atomic(page);
Seth Jennings5f601902012-07-02 16:15:49 -05001225 return area->vm_addr + off;
Nitin Gupta61989a82012-01-09 16:51:56 -06001226 }
1227
Seth Jenningsf5536462012-07-18 11:55:56 -05001228 /* this object spans two pages */
1229 pages[0] = page;
1230 pages[1] = get_next_page(page);
1231 BUG_ON(!pages[1]);
Seth Jenningsb7418512012-07-02 16:15:52 -05001232
Seth Jenningsf5536462012-07-18 11:55:56 -05001233 return __zs_map_object(area, pages, off, class->size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001234}
1235EXPORT_SYMBOL_GPL(zs_map_object);
1236
Minchan Kimc2344342012-06-08 15:39:25 +09001237void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
Nitin Gupta61989a82012-01-09 16:51:56 -06001238{
1239 struct page *page;
1240 unsigned long obj_idx, off;
1241
1242 unsigned int class_idx;
1243 enum fullness_group fg;
1244 struct size_class *class;
1245 struct mapping_area *area;
1246
1247 BUG_ON(!handle);
1248
1249 obj_handle_to_location(handle, &page, &obj_idx);
1250 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001251 class = pool->size_class[class_idx];
Nitin Gupta61989a82012-01-09 16:51:56 -06001252 off = obj_idx_to_offset(page, obj_idx, class->size);
1253
Christoph Lameter7c8e0182014-06-04 16:07:56 -07001254 area = this_cpu_ptr(&zs_map_area);
Seth Jenningsf5536462012-07-18 11:55:56 -05001255 if (off + class->size <= PAGE_SIZE)
1256 kunmap_atomic(area->vm_addr);
1257 else {
1258 struct page *pages[2];
Seth Jenningsb7418512012-07-02 16:15:52 -05001259
Seth Jenningsf5536462012-07-18 11:55:56 -05001260 pages[0] = page;
1261 pages[1] = get_next_page(page);
1262 BUG_ON(!pages[1]);
1263
1264 __zs_unmap_object(area, pages, off, class->size);
1265 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001266 put_cpu_var(zs_map_area);
1267}
1268EXPORT_SYMBOL_GPL(zs_unmap_object);
1269
Minchan Kim722cdc12014-10-09 15:29:50 -07001270unsigned long zs_get_total_pages(struct zs_pool *pool)
Nitin Gupta61989a82012-01-09 16:51:56 -06001271{
Minchan Kim722cdc12014-10-09 15:29:50 -07001272 return atomic_long_read(&pool->pages_allocated);
Nitin Gupta61989a82012-01-09 16:51:56 -06001273}
Minchan Kim722cdc12014-10-09 15:29:50 -07001274EXPORT_SYMBOL_GPL(zs_get_total_pages);
Ben Hutchings069f1012012-06-20 02:31:11 +01001275
1276module_init(zs_init);
1277module_exit(zs_exit);
1278
1279MODULE_LICENSE("Dual BSD/GPL");
1280MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");