zsmalloc: add Kconfig for enabling page table method
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / staging / zsmalloc / zsmalloc-main.c
CommitLineData
61989a80
NG
1/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
5 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the license that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 */
12
2db51dae
NG
13
14/*
15 * This allocator is designed for use with zcache and zram. Thus, the
16 * allocator is supposed to work well under low memory conditions. In
17 * particular, it never attempts higher order page allocation which is
18 * very likely to fail under memory pressure. On the other hand, if we
19 * just use single (0-order) pages, it would suffer from very high
20 * fragmentation -- any object of size PAGE_SIZE/2 or larger would occupy
21 * an entire page. This was one of the major issues with its predecessor
22 * (xvmalloc).
23 *
24 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
25 * and links them together using various 'struct page' fields. These linked
26 * pages act as a single higher-order page i.e. an object can span 0-order
27 * page boundaries. The code refers to these linked pages as a single entity
28 * called zspage.
29 *
30 * Following is how we use various fields and flags of underlying
31 * struct page(s) to form a zspage.
32 *
33 * Usage of struct page fields:
34 * page->first_page: points to the first component (0-order) page
35 * page->index (union with page->freelist): offset of the first object
36 * starting in this page. For the first page, this is
37 * always 0, so we use this field (aka freelist) to point
38 * to the first free object in zspage.
39 * page->lru: links together all component pages (except the first page)
40 * of a zspage
41 *
42 * For _first_ page only:
43 *
44 * page->private (union with page->first_page): refers to the
45 * component page after the first page
46 * page->freelist: points to the first free object in zspage.
47 * Free objects are linked together using in-place
48 * metadata.
49 * page->objects: maximum number of objects we can store in this
50 * zspage (class->zspage_order * PAGE_SIZE / class->size)
51 * page->lru: links together first pages of various zspages.
52 * Basically forming list of zspages in a fullness group.
53 * page->mapping: class index and fullness group of the zspage
54 *
55 * Usage of struct page flags:
56 * PG_private: identifies the first component page
57 * PG_private2: identifies the last component page
58 *
59 */
60
61989a80
NG
61#ifdef CONFIG_ZSMALLOC_DEBUG
62#define DEBUG
63#endif
64
65#include <linux/module.h>
66#include <linux/kernel.h>
67#include <linux/bitops.h>
68#include <linux/errno.h>
69#include <linux/highmem.h>
70#include <linux/init.h>
71#include <linux/string.h>
72#include <linux/slab.h>
73#include <asm/tlbflush.h>
74#include <asm/pgtable.h>
75#include <linux/cpumask.h>
76#include <linux/cpu.h>
0cbb613f 77#include <linux/vmalloc.h>
c60369f0 78#include <linux/hardirq.h>
0959c63f
SJ
79#include <linux/spinlock.h>
80#include <linux/types.h>
61989a80
NG
81
82#include "zsmalloc.h"
0959c63f
SJ
83
84/*
85 * This must be power of 2 and greater than of equal to sizeof(link_free).
86 * These two conditions ensure that any 'struct link_free' itself doesn't
87 * span more than 1 page which avoids complex case of mapping 2 pages simply
88 * to restore link_free pointer values.
89 */
90#define ZS_ALIGN 8
91
92/*
93 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
94 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
95 */
96#define ZS_MAX_ZSPAGE_ORDER 2
97#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
98
99/*
100 * Object location (<PFN>, <obj_idx>) is encoded as
101 * as single (void *) handle value.
102 *
103 * Note that object index <obj_idx> is relative to system
104 * page <PFN> it is stored in, so for each sub-page belonging
105 * to a zspage, obj_idx starts with 0.
106 *
107 * This is made more complicated by various memory models and PAE.
108 */
109
110#ifndef MAX_PHYSMEM_BITS
111#ifdef CONFIG_HIGHMEM64G
112#define MAX_PHYSMEM_BITS 36
113#else /* !CONFIG_HIGHMEM64G */
114/*
115 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
116 * be PAGE_SHIFT
117 */
118#define MAX_PHYSMEM_BITS BITS_PER_LONG
119#endif
120#endif
121#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
122#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
123#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
124
125#define MAX(a, b) ((a) >= (b) ? (a) : (b))
126/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
127#define ZS_MIN_ALLOC_SIZE \
128 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
129#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
130
131/*
132 * On systems with 4K page size, this gives 254 size classes! There is a
133 * trader-off here:
134 * - Large number of size classes is potentially wasteful as free page are
135 * spread across these classes
136 * - Small number of size classes causes large internal fragmentation
137 * - Probably its better to use specific size classes (empirically
138 * determined). NOTE: all those class sizes must be set as multiple of
139 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
140 *
141 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
142 * (reason above)
143 */
d662b8eb 144#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
0959c63f
SJ
145#define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
146 ZS_SIZE_CLASS_DELTA + 1)
147
148/*
149 * We do not maintain any list for completely empty or full pages
150 */
151enum fullness_group {
152 ZS_ALMOST_FULL,
153 ZS_ALMOST_EMPTY,
154 _ZS_NR_FULLNESS_GROUPS,
155
156 ZS_EMPTY,
157 ZS_FULL
158};
159
160/*
161 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
162 * n <= N / f, where
163 * n = number of allocated objects
164 * N = total number of objects zspage can store
165 * f = 1/fullness_threshold_frac
166 *
167 * Similarly, we assign zspage to:
168 * ZS_ALMOST_FULL when n > N / f
169 * ZS_EMPTY when n == 0
170 * ZS_FULL when n == N
171 *
172 * (see: fix_fullness_group())
173 */
174static const int fullness_threshold_frac = 4;
175
176struct size_class {
177 /*
178 * Size of objects stored in this class. Must be multiple
179 * of ZS_ALIGN.
180 */
181 int size;
182 unsigned int index;
183
184 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
185 int pages_per_zspage;
186
187 spinlock_t lock;
188
189 /* stats */
190 u64 pages_allocated;
191
192 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
193};
194
195/*
196 * Placed within free objects to form a singly linked list.
197 * For every zspage, first_page->freelist gives head of this list.
198 *
199 * This must be power of 2 and less than or equal to ZS_ALIGN
200 */
201struct link_free {
202 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
203 void *next;
204};
205
206struct zs_pool {
207 struct size_class size_class[ZS_SIZE_CLASSES];
208
209 gfp_t flags; /* allocation flags used when growing pool */
0959c63f 210};
61989a80
NG
211
212/*
213 * A zspage's class index and fullness group
214 * are encoded in its (first)page->mapping
215 */
216#define CLASS_IDX_BITS 28
217#define FULLNESS_BITS 4
218#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
219#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
220
f553646a 221struct mapping_area {
1b945aee 222#ifdef CONFIG_PGTABLE_MAPPING
f553646a
SJ
223 struct vm_struct *vm; /* vm area for mapping object that span pages */
224#else
225 char *vm_buf; /* copy buffer for objects that span pages */
226#endif
227 char *vm_addr; /* address of kmap_atomic()'ed pages */
228 enum zs_mapmode vm_mm; /* mapping mode */
229};
230
231
61989a80
NG
232/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
233static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
234
235static int is_first_page(struct page *page)
236{
a27545bf 237 return PagePrivate(page);
61989a80
NG
238}
239
240static int is_last_page(struct page *page)
241{
a27545bf 242 return PagePrivate2(page);
61989a80
NG
243}
244
245static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
246 enum fullness_group *fullness)
247{
248 unsigned long m;
249 BUG_ON(!is_first_page(page));
250
251 m = (unsigned long)page->mapping;
252 *fullness = m & FULLNESS_MASK;
253 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
254}
255
256static void set_zspage_mapping(struct page *page, unsigned int class_idx,
257 enum fullness_group fullness)
258{
259 unsigned long m;
260 BUG_ON(!is_first_page(page));
261
262 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
263 (fullness & FULLNESS_MASK);
264 page->mapping = (struct address_space *)m;
265}
266
267static int get_size_class_index(int size)
268{
269 int idx = 0;
270
271 if (likely(size > ZS_MIN_ALLOC_SIZE))
272 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
273 ZS_SIZE_CLASS_DELTA);
274
275 return idx;
276}
277
278static enum fullness_group get_fullness_group(struct page *page)
279{
280 int inuse, max_objects;
281 enum fullness_group fg;
282 BUG_ON(!is_first_page(page));
283
284 inuse = page->inuse;
285 max_objects = page->objects;
286
287 if (inuse == 0)
288 fg = ZS_EMPTY;
289 else if (inuse == max_objects)
290 fg = ZS_FULL;
291 else if (inuse <= max_objects / fullness_threshold_frac)
292 fg = ZS_ALMOST_EMPTY;
293 else
294 fg = ZS_ALMOST_FULL;
295
296 return fg;
297}
298
299static void insert_zspage(struct page *page, struct size_class *class,
300 enum fullness_group fullness)
301{
302 struct page **head;
303
304 BUG_ON(!is_first_page(page));
305
306 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
307 return;
308
309 head = &class->fullness_list[fullness];
310 if (*head)
311 list_add_tail(&page->lru, &(*head)->lru);
312
313 *head = page;
314}
315
316static void remove_zspage(struct page *page, struct size_class *class,
317 enum fullness_group fullness)
318{
319 struct page **head;
320
321 BUG_ON(!is_first_page(page));
322
323 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
324 return;
325
326 head = &class->fullness_list[fullness];
327 BUG_ON(!*head);
328 if (list_empty(&(*head)->lru))
329 *head = NULL;
330 else if (*head == page)
331 *head = (struct page *)list_entry((*head)->lru.next,
332 struct page, lru);
333
334 list_del_init(&page->lru);
335}
336
337static enum fullness_group fix_fullness_group(struct zs_pool *pool,
338 struct page *page)
339{
340 int class_idx;
341 struct size_class *class;
342 enum fullness_group currfg, newfg;
343
344 BUG_ON(!is_first_page(page));
345
346 get_zspage_mapping(page, &class_idx, &currfg);
347 newfg = get_fullness_group(page);
348 if (newfg == currfg)
349 goto out;
350
351 class = &pool->size_class[class_idx];
352 remove_zspage(page, class, currfg);
353 insert_zspage(page, class, newfg);
354 set_zspage_mapping(page, class_idx, newfg);
355
356out:
357 return newfg;
358}
359
360/*
361 * We have to decide on how many pages to link together
362 * to form a zspage for each size class. This is important
363 * to reduce wastage due to unusable space left at end of
364 * each zspage which is given as:
365 * wastage = Zp - Zp % size_class
366 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
367 *
368 * For example, for size class of 3/8 * PAGE_SIZE, we should
369 * link together 3 PAGE_SIZE sized pages to form a zspage
370 * since then we can perfectly fit in 8 such objects.
371 */
2e3b6154 372static int get_pages_per_zspage(int class_size)
61989a80
NG
373{
374 int i, max_usedpc = 0;
375 /* zspage order which gives maximum used size per KB */
376 int max_usedpc_order = 1;
377
84d4faab 378 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
61989a80
NG
379 int zspage_size;
380 int waste, usedpc;
381
382 zspage_size = i * PAGE_SIZE;
383 waste = zspage_size % class_size;
384 usedpc = (zspage_size - waste) * 100 / zspage_size;
385
386 if (usedpc > max_usedpc) {
387 max_usedpc = usedpc;
388 max_usedpc_order = i;
389 }
390 }
391
392 return max_usedpc_order;
393}
394
395/*
396 * A single 'zspage' is composed of many system pages which are
397 * linked together using fields in struct page. This function finds
398 * the first/head page, given any component page of a zspage.
399 */
400static struct page *get_first_page(struct page *page)
401{
402 if (is_first_page(page))
403 return page;
404 else
405 return page->first_page;
406}
407
408static struct page *get_next_page(struct page *page)
409{
410 struct page *next;
411
412 if (is_last_page(page))
413 next = NULL;
414 else if (is_first_page(page))
e842b976 415 next = (struct page *)page_private(page);
61989a80
NG
416 else
417 next = list_entry(page->lru.next, struct page, lru);
418
419 return next;
420}
421
67296874
OH
422/*
423 * Encode <page, obj_idx> as a single handle value.
424 * On hardware platforms with physical memory starting at 0x0 the pfn
425 * could be 0 so we ensure that the handle will never be 0 by adjusting the
426 * encoded obj_idx value before encoding.
427 */
61989a80
NG
428static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
429{
430 unsigned long handle;
431
432 if (!page) {
433 BUG_ON(obj_idx);
434 return NULL;
435 }
436
437 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
67296874 438 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
61989a80
NG
439
440 return (void *)handle;
441}
442
67296874
OH
443/*
444 * Decode <page, obj_idx> pair from the given object handle. We adjust the
445 * decoded obj_idx back to its original value since it was adjusted in
446 * obj_location_to_handle().
447 */
c2344348 448static void obj_handle_to_location(unsigned long handle, struct page **page,
61989a80
NG
449 unsigned long *obj_idx)
450{
c2344348 451 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
67296874 452 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
61989a80
NG
453}
454
455static unsigned long obj_idx_to_offset(struct page *page,
456 unsigned long obj_idx, int class_size)
457{
458 unsigned long off = 0;
459
460 if (!is_first_page(page))
461 off = page->index;
462
463 return off + obj_idx * class_size;
464}
465
f4477e90
NG
466static void reset_page(struct page *page)
467{
468 clear_bit(PG_private, &page->flags);
469 clear_bit(PG_private_2, &page->flags);
470 set_page_private(page, 0);
471 page->mapping = NULL;
472 page->freelist = NULL;
22b751c3 473 page_mapcount_reset(page);
f4477e90
NG
474}
475
61989a80
NG
476static void free_zspage(struct page *first_page)
477{
f4477e90 478 struct page *nextp, *tmp, *head_extra;
61989a80
NG
479
480 BUG_ON(!is_first_page(first_page));
481 BUG_ON(first_page->inuse);
482
f4477e90 483 head_extra = (struct page *)page_private(first_page);
61989a80 484
f4477e90 485 reset_page(first_page);
61989a80
NG
486 __free_page(first_page);
487
488 /* zspage with only 1 system page */
f4477e90 489 if (!head_extra)
61989a80
NG
490 return;
491
f4477e90 492 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
61989a80 493 list_del(&nextp->lru);
f4477e90 494 reset_page(nextp);
61989a80
NG
495 __free_page(nextp);
496 }
f4477e90
NG
497 reset_page(head_extra);
498 __free_page(head_extra);
61989a80
NG
499}
500
501/* Initialize a newly allocated zspage */
502static void init_zspage(struct page *first_page, struct size_class *class)
503{
504 unsigned long off = 0;
505 struct page *page = first_page;
506
507 BUG_ON(!is_first_page(first_page));
508 while (page) {
509 struct page *next_page;
510 struct link_free *link;
511 unsigned int i, objs_on_page;
512
513 /*
514 * page->index stores offset of first object starting
515 * in the page. For the first page, this is always 0,
516 * so we use first_page->index (aka ->freelist) to store
517 * head of corresponding zspage's freelist.
518 */
519 if (page != first_page)
520 page->index = off;
521
522 link = (struct link_free *)kmap_atomic(page) +
523 off / sizeof(*link);
524 objs_on_page = (PAGE_SIZE - off) / class->size;
525
526 for (i = 1; i <= objs_on_page; i++) {
527 off += class->size;
528 if (off < PAGE_SIZE) {
529 link->next = obj_location_to_handle(page, i);
530 link += class->size / sizeof(*link);
531 }
532 }
533
534 /*
535 * We now come to the last (full or partial) object on this
536 * page, which must point to the first object on the next
537 * page (if present)
538 */
539 next_page = get_next_page(page);
540 link->next = obj_location_to_handle(next_page, 0);
541 kunmap_atomic(link);
542 page = next_page;
543 off = (off + class->size) % PAGE_SIZE;
544 }
545}
546
547/*
548 * Allocate a zspage for the given size class
549 */
550static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
551{
552 int i, error;
b4b700c5 553 struct page *first_page = NULL, *uninitialized_var(prev_page);
61989a80
NG
554
555 /*
556 * Allocate individual pages and link them together as:
557 * 1. first page->private = first sub-page
558 * 2. all sub-pages are linked together using page->lru
559 * 3. each sub-page is linked to the first page using page->first_page
560 *
561 * For each size class, First/Head pages are linked together using
562 * page->lru. Also, we set PG_private to identify the first page
563 * (i.e. no other sub-page has this flag set) and PG_private_2 to
564 * identify the last page.
565 */
566 error = -ENOMEM;
2e3b6154 567 for (i = 0; i < class->pages_per_zspage; i++) {
b4b700c5 568 struct page *page;
61989a80
NG
569
570 page = alloc_page(flags);
571 if (!page)
572 goto cleanup;
573
574 INIT_LIST_HEAD(&page->lru);
575 if (i == 0) { /* first page */
a27545bf 576 SetPagePrivate(page);
61989a80
NG
577 set_page_private(page, 0);
578 first_page = page;
579 first_page->inuse = 0;
580 }
581 if (i == 1)
e842b976 582 set_page_private(first_page, (unsigned long)page);
61989a80
NG
583 if (i >= 1)
584 page->first_page = first_page;
585 if (i >= 2)
586 list_add(&page->lru, &prev_page->lru);
2e3b6154 587 if (i == class->pages_per_zspage - 1) /* last page */
a27545bf 588 SetPagePrivate2(page);
61989a80
NG
589 prev_page = page;
590 }
591
592 init_zspage(first_page, class);
593
594 first_page->freelist = obj_location_to_handle(first_page, 0);
595 /* Maximum number of objects we can store in this zspage */
2e3b6154 596 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
61989a80
NG
597
598 error = 0; /* Success */
599
600cleanup:
601 if (unlikely(error) && first_page) {
602 free_zspage(first_page);
603 first_page = NULL;
604 }
605
606 return first_page;
607}
608
609static struct page *find_get_zspage(struct size_class *class)
610{
611 int i;
612 struct page *page;
613
614 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
615 page = class->fullness_list[i];
616 if (page)
617 break;
618 }
619
620 return page;
621}
622
1b945aee 623#ifdef CONFIG_PGTABLE_MAPPING
f553646a
SJ
624static inline int __zs_cpu_up(struct mapping_area *area)
625{
626 /*
627 * Make sure we don't leak memory if a cpu UP notification
628 * and zs_init() race and both call zs_cpu_up() on the same cpu
629 */
630 if (area->vm)
631 return 0;
632 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
633 if (!area->vm)
634 return -ENOMEM;
635 return 0;
636}
637
638static inline void __zs_cpu_down(struct mapping_area *area)
639{
640 if (area->vm)
641 free_vm_area(area->vm);
642 area->vm = NULL;
643}
644
645static inline void *__zs_map_object(struct mapping_area *area,
646 struct page *pages[2], int off, int size)
647{
648 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
649 area->vm_addr = area->vm->addr;
650 return area->vm_addr + off;
651}
652
653static inline void __zs_unmap_object(struct mapping_area *area,
654 struct page *pages[2], int off, int size)
655{
656 unsigned long addr = (unsigned long)area->vm_addr;
f553646a 657
d95abbbb 658 unmap_kernel_range(addr, PAGE_SIZE * 2);
f553646a
SJ
659}
660
1b945aee 661#else /* CONFIG_PGTABLE_MAPPING */
f553646a
SJ
662
663static inline int __zs_cpu_up(struct mapping_area *area)
664{
665 /*
666 * Make sure we don't leak memory if a cpu UP notification
667 * and zs_init() race and both call zs_cpu_up() on the same cpu
668 */
669 if (area->vm_buf)
670 return 0;
671 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
672 if (!area->vm_buf)
673 return -ENOMEM;
674 return 0;
675}
676
677static inline void __zs_cpu_down(struct mapping_area *area)
678{
679 if (area->vm_buf)
680 free_page((unsigned long)area->vm_buf);
681 area->vm_buf = NULL;
682}
683
684static void *__zs_map_object(struct mapping_area *area,
685 struct page *pages[2], int off, int size)
5f601902 686{
5f601902
SJ
687 int sizes[2];
688 void *addr;
f553646a 689 char *buf = area->vm_buf;
5f601902 690
f553646a
SJ
691 /* disable page faults to match kmap_atomic() return conditions */
692 pagefault_disable();
693
694 /* no read fastpath */
695 if (area->vm_mm == ZS_MM_WO)
696 goto out;
5f601902
SJ
697
698 sizes[0] = PAGE_SIZE - off;
699 sizes[1] = size - sizes[0];
700
5f601902
SJ
701 /* copy object to per-cpu buffer */
702 addr = kmap_atomic(pages[0]);
703 memcpy(buf, addr + off, sizes[0]);
704 kunmap_atomic(addr);
705 addr = kmap_atomic(pages[1]);
706 memcpy(buf + sizes[0], addr, sizes[1]);
707 kunmap_atomic(addr);
f553646a
SJ
708out:
709 return area->vm_buf;
5f601902
SJ
710}
711
f553646a
SJ
712static void __zs_unmap_object(struct mapping_area *area,
713 struct page *pages[2], int off, int size)
5f601902 714{
5f601902
SJ
715 int sizes[2];
716 void *addr;
f553646a 717 char *buf = area->vm_buf;
5f601902 718
f553646a
SJ
719 /* no write fastpath */
720 if (area->vm_mm == ZS_MM_RO)
721 goto out;
5f601902
SJ
722
723 sizes[0] = PAGE_SIZE - off;
724 sizes[1] = size - sizes[0];
725
726 /* copy per-cpu buffer to object */
727 addr = kmap_atomic(pages[0]);
728 memcpy(addr + off, buf, sizes[0]);
729 kunmap_atomic(addr);
730 addr = kmap_atomic(pages[1]);
731 memcpy(addr, buf + sizes[0], sizes[1]);
732 kunmap_atomic(addr);
f553646a
SJ
733
734out:
735 /* enable page faults to match kunmap_atomic() return conditions */
736 pagefault_enable();
5f601902 737}
61989a80 738
1b945aee 739#endif /* CONFIG_PGTABLE_MAPPING */
f553646a 740
61989a80
NG
741static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
742 void *pcpu)
743{
f553646a 744 int ret, cpu = (long)pcpu;
61989a80
NG
745 struct mapping_area *area;
746
747 switch (action) {
748 case CPU_UP_PREPARE:
749 area = &per_cpu(zs_map_area, cpu);
f553646a
SJ
750 ret = __zs_cpu_up(area);
751 if (ret)
752 return notifier_from_errno(ret);
61989a80
NG
753 break;
754 case CPU_DEAD:
755 case CPU_UP_CANCELED:
756 area = &per_cpu(zs_map_area, cpu);
f553646a 757 __zs_cpu_down(area);
61989a80
NG
758 break;
759 }
760
761 return NOTIFY_OK;
762}
763
764static struct notifier_block zs_cpu_nb = {
765 .notifier_call = zs_cpu_notifier
766};
767
768static void zs_exit(void)
769{
770 int cpu;
771
772 for_each_online_cpu(cpu)
773 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
774 unregister_cpu_notifier(&zs_cpu_nb);
775}
776
777static int zs_init(void)
778{
779 int cpu, ret;
780
781 register_cpu_notifier(&zs_cpu_nb);
782 for_each_online_cpu(cpu) {
783 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
784 if (notifier_to_errno(ret))
785 goto fail;
786 }
787 return 0;
788fail:
789 zs_exit();
790 return notifier_to_errno(ret);
791}
792
4bbc0bc0
DB
793/**
794 * zs_create_pool - Creates an allocation pool to work from.
0d145a50 795 * @flags: allocation flags used to allocate pool metadata
4bbc0bc0
DB
796 *
797 * This function must be called before anything when using
798 * the zsmalloc allocator.
799 *
800 * On success, a pointer to the newly created pool is returned,
801 * otherwise NULL.
802 */
0d145a50 803struct zs_pool *zs_create_pool(gfp_t flags)
61989a80 804{
069f101f 805 int i, ovhd_size;
61989a80
NG
806 struct zs_pool *pool;
807
61989a80
NG
808 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
809 pool = kzalloc(ovhd_size, GFP_KERNEL);
810 if (!pool)
811 return NULL;
812
813 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
814 int size;
815 struct size_class *class;
816
817 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
818 if (size > ZS_MAX_ALLOC_SIZE)
819 size = ZS_MAX_ALLOC_SIZE;
820
821 class = &pool->size_class[i];
822 class->size = size;
823 class->index = i;
824 spin_lock_init(&class->lock);
2e3b6154 825 class->pages_per_zspage = get_pages_per_zspage(size);
61989a80
NG
826
827 }
828
61989a80 829 pool->flags = flags;
61989a80 830
61989a80
NG
831 return pool;
832}
833EXPORT_SYMBOL_GPL(zs_create_pool);
834
835void zs_destroy_pool(struct zs_pool *pool)
836{
837 int i;
838
839 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
840 int fg;
841 struct size_class *class = &pool->size_class[i];
842
843 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
844 if (class->fullness_list[fg]) {
93ad5ab5 845 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
61989a80
NG
846 class->size, fg);
847 }
848 }
849 }
850 kfree(pool);
851}
852EXPORT_SYMBOL_GPL(zs_destroy_pool);
853
854/**
855 * zs_malloc - Allocate block of given size from pool.
856 * @pool: pool to allocate from
857 * @size: size of block to allocate
61989a80 858 *
00a61d86 859 * On success, handle to the allocated object is returned,
c2344348 860 * otherwise 0.
61989a80
NG
861 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
862 */
c2344348 863unsigned long zs_malloc(struct zs_pool *pool, size_t size)
61989a80 864{
c2344348 865 unsigned long obj;
61989a80
NG
866 struct link_free *link;
867 int class_idx;
868 struct size_class *class;
869
870 struct page *first_page, *m_page;
871 unsigned long m_objidx, m_offset;
872
873 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
c2344348 874 return 0;
61989a80
NG
875
876 class_idx = get_size_class_index(size);
877 class = &pool->size_class[class_idx];
878 BUG_ON(class_idx != class->index);
879
880 spin_lock(&class->lock);
881 first_page = find_get_zspage(class);
882
883 if (!first_page) {
884 spin_unlock(&class->lock);
885 first_page = alloc_zspage(class, pool->flags);
886 if (unlikely(!first_page))
c2344348 887 return 0;
61989a80
NG
888
889 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
890 spin_lock(&class->lock);
2e3b6154 891 class->pages_allocated += class->pages_per_zspage;
61989a80
NG
892 }
893
c2344348 894 obj = (unsigned long)first_page->freelist;
61989a80
NG
895 obj_handle_to_location(obj, &m_page, &m_objidx);
896 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
897
898 link = (struct link_free *)kmap_atomic(m_page) +
899 m_offset / sizeof(*link);
900 first_page->freelist = link->next;
901 memset(link, POISON_INUSE, sizeof(*link));
902 kunmap_atomic(link);
903
904 first_page->inuse++;
905 /* Now move the zspage to another fullness group, if required */
906 fix_fullness_group(pool, first_page);
907 spin_unlock(&class->lock);
908
909 return obj;
910}
911EXPORT_SYMBOL_GPL(zs_malloc);
912
c2344348 913void zs_free(struct zs_pool *pool, unsigned long obj)
61989a80
NG
914{
915 struct link_free *link;
916 struct page *first_page, *f_page;
917 unsigned long f_objidx, f_offset;
918
919 int class_idx;
920 struct size_class *class;
921 enum fullness_group fullness;
922
923 if (unlikely(!obj))
924 return;
925
926 obj_handle_to_location(obj, &f_page, &f_objidx);
927 first_page = get_first_page(f_page);
928
929 get_zspage_mapping(first_page, &class_idx, &fullness);
930 class = &pool->size_class[class_idx];
931 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
932
933 spin_lock(&class->lock);
934
935 /* Insert this object in containing zspage's freelist */
936 link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
937 + f_offset);
938 link->next = first_page->freelist;
939 kunmap_atomic(link);
c2344348 940 first_page->freelist = (void *)obj;
61989a80
NG
941
942 first_page->inuse--;
943 fullness = fix_fullness_group(pool, first_page);
944
945 if (fullness == ZS_EMPTY)
2e3b6154 946 class->pages_allocated -= class->pages_per_zspage;
61989a80
NG
947
948 spin_unlock(&class->lock);
949
950 if (fullness == ZS_EMPTY)
951 free_zspage(first_page);
952}
953EXPORT_SYMBOL_GPL(zs_free);
954
00a61d86
MK
955/**
956 * zs_map_object - get address of allocated object from handle.
957 * @pool: pool from which the object was allocated
958 * @handle: handle returned from zs_malloc
959 *
960 * Before using an object allocated from zs_malloc, it must be mapped using
961 * this function. When done with the object, it must be unmapped using
166cfda7
SJ
962 * zs_unmap_object.
963 *
964 * Only one object can be mapped per cpu at a time. There is no protection
965 * against nested mappings.
966 *
967 * This function returns with preemption and page faults disabled.
396b7fd6 968 */
b7418510
SJ
969void *zs_map_object(struct zs_pool *pool, unsigned long handle,
970 enum zs_mapmode mm)
61989a80
NG
971{
972 struct page *page;
973 unsigned long obj_idx, off;
974
975 unsigned int class_idx;
976 enum fullness_group fg;
977 struct size_class *class;
978 struct mapping_area *area;
f553646a 979 struct page *pages[2];
61989a80
NG
980
981 BUG_ON(!handle);
982
c60369f0
SJ
983 /*
984 * Because we use per-cpu mapping areas shared among the
985 * pools/users, we can't allow mapping in interrupt context
986 * because it can corrupt another users mappings.
987 */
988 BUG_ON(in_interrupt());
989
61989a80
NG
990 obj_handle_to_location(handle, &page, &obj_idx);
991 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
992 class = &pool->size_class[class_idx];
993 off = obj_idx_to_offset(page, obj_idx, class->size);
994
995 area = &get_cpu_var(zs_map_area);
f553646a 996 area->vm_mm = mm;
61989a80
NG
997 if (off + class->size <= PAGE_SIZE) {
998 /* this object is contained entirely within a page */
999 area->vm_addr = kmap_atomic(page);
5f601902 1000 return area->vm_addr + off;
61989a80
NG
1001 }
1002
f553646a
SJ
1003 /* this object spans two pages */
1004 pages[0] = page;
1005 pages[1] = get_next_page(page);
1006 BUG_ON(!pages[1]);
b7418510 1007
f553646a 1008 return __zs_map_object(area, pages, off, class->size);
61989a80
NG
1009}
1010EXPORT_SYMBOL_GPL(zs_map_object);
1011
c2344348 1012void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
61989a80
NG
1013{
1014 struct page *page;
1015 unsigned long obj_idx, off;
1016
1017 unsigned int class_idx;
1018 enum fullness_group fg;
1019 struct size_class *class;
1020 struct mapping_area *area;
1021
1022 BUG_ON(!handle);
1023
1024 obj_handle_to_location(handle, &page, &obj_idx);
1025 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1026 class = &pool->size_class[class_idx];
1027 off = obj_idx_to_offset(page, obj_idx, class->size);
1028
f553646a
SJ
1029 area = &__get_cpu_var(zs_map_area);
1030 if (off + class->size <= PAGE_SIZE)
1031 kunmap_atomic(area->vm_addr);
1032 else {
1033 struct page *pages[2];
1034
1035 pages[0] = page;
1036 pages[1] = get_next_page(page);
1037 BUG_ON(!pages[1]);
b7418510 1038
f553646a
SJ
1039 __zs_unmap_object(area, pages, off, class->size);
1040 }
61989a80
NG
1041 put_cpu_var(zs_map_area);
1042}
1043EXPORT_SYMBOL_GPL(zs_unmap_object);
1044
1045u64 zs_get_total_size_bytes(struct zs_pool *pool)
1046{
1047 int i;
1048 u64 npages = 0;
1049
1050 for (i = 0; i < ZS_SIZE_CLASSES; i++)
1051 npages += pool->size_class[i].pages_allocated;
1052
1053 return npages << PAGE_SHIFT;
1054}
1055EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
069f101f
BH
1056
1057module_init(zs_init);
1058module_exit(zs_exit);
1059
1060MODULE_LICENSE("Dual BSD/GPL");
1061MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");