staging: btmtk_usb: remove driver
[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
SJ
221/*
222 * By default, zsmalloc uses a copy-based object mapping method to access
223 * allocations that span two pages. However, if a particular architecture
99155188
MK
224 * performs VM mapping faster than copying, then it should be added here
225 * so that USE_PGTABLE_MAPPING is defined. This causes zsmalloc to use
226 * page table mapping rather than copying for object mapping.
396b7fd6 227 */
796ce5a7 228#if defined(CONFIG_ARM) && !defined(MODULE)
f553646a
SJ
229#define USE_PGTABLE_MAPPING
230#endif
231
232struct mapping_area {
233#ifdef USE_PGTABLE_MAPPING
234 struct vm_struct *vm; /* vm area for mapping object that span pages */
235#else
236 char *vm_buf; /* copy buffer for objects that span pages */
237#endif
238 char *vm_addr; /* address of kmap_atomic()'ed pages */
239 enum zs_mapmode vm_mm; /* mapping mode */
240};
241
242
61989a80
NG
243/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
244static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
245
246static int is_first_page(struct page *page)
247{
a27545bf 248 return PagePrivate(page);
61989a80
NG
249}
250
251static int is_last_page(struct page *page)
252{
a27545bf 253 return PagePrivate2(page);
61989a80
NG
254}
255
256static void get_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 = (unsigned long)page->mapping;
263 *fullness = m & FULLNESS_MASK;
264 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
265}
266
267static void set_zspage_mapping(struct page *page, unsigned int class_idx,
268 enum fullness_group fullness)
269{
270 unsigned long m;
271 BUG_ON(!is_first_page(page));
272
273 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
274 (fullness & FULLNESS_MASK);
275 page->mapping = (struct address_space *)m;
276}
277
278static int get_size_class_index(int size)
279{
280 int idx = 0;
281
282 if (likely(size > ZS_MIN_ALLOC_SIZE))
283 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
284 ZS_SIZE_CLASS_DELTA);
285
286 return idx;
287}
288
289static enum fullness_group get_fullness_group(struct page *page)
290{
291 int inuse, max_objects;
292 enum fullness_group fg;
293 BUG_ON(!is_first_page(page));
294
295 inuse = page->inuse;
296 max_objects = page->objects;
297
298 if (inuse == 0)
299 fg = ZS_EMPTY;
300 else if (inuse == max_objects)
301 fg = ZS_FULL;
302 else if (inuse <= max_objects / fullness_threshold_frac)
303 fg = ZS_ALMOST_EMPTY;
304 else
305 fg = ZS_ALMOST_FULL;
306
307 return fg;
308}
309
310static void insert_zspage(struct page *page, struct size_class *class,
311 enum fullness_group fullness)
312{
313 struct page **head;
314
315 BUG_ON(!is_first_page(page));
316
317 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
318 return;
319
320 head = &class->fullness_list[fullness];
321 if (*head)
322 list_add_tail(&page->lru, &(*head)->lru);
323
324 *head = page;
325}
326
327static void remove_zspage(struct page *page, struct size_class *class,
328 enum fullness_group fullness)
329{
330 struct page **head;
331
332 BUG_ON(!is_first_page(page));
333
334 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
335 return;
336
337 head = &class->fullness_list[fullness];
338 BUG_ON(!*head);
339 if (list_empty(&(*head)->lru))
340 *head = NULL;
341 else if (*head == page)
342 *head = (struct page *)list_entry((*head)->lru.next,
343 struct page, lru);
344
345 list_del_init(&page->lru);
346}
347
348static enum fullness_group fix_fullness_group(struct zs_pool *pool,
349 struct page *page)
350{
351 int class_idx;
352 struct size_class *class;
353 enum fullness_group currfg, newfg;
354
355 BUG_ON(!is_first_page(page));
356
357 get_zspage_mapping(page, &class_idx, &currfg);
358 newfg = get_fullness_group(page);
359 if (newfg == currfg)
360 goto out;
361
362 class = &pool->size_class[class_idx];
363 remove_zspage(page, class, currfg);
364 insert_zspage(page, class, newfg);
365 set_zspage_mapping(page, class_idx, newfg);
366
367out:
368 return newfg;
369}
370
371/*
372 * We have to decide on how many pages to link together
373 * to form a zspage for each size class. This is important
374 * to reduce wastage due to unusable space left at end of
375 * each zspage which is given as:
376 * wastage = Zp - Zp % size_class
377 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
378 *
379 * For example, for size class of 3/8 * PAGE_SIZE, we should
380 * link together 3 PAGE_SIZE sized pages to form a zspage
381 * since then we can perfectly fit in 8 such objects.
382 */
2e3b6154 383static int get_pages_per_zspage(int class_size)
61989a80
NG
384{
385 int i, max_usedpc = 0;
386 /* zspage order which gives maximum used size per KB */
387 int max_usedpc_order = 1;
388
84d4faab 389 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
61989a80
NG
390 int zspage_size;
391 int waste, usedpc;
392
393 zspage_size = i * PAGE_SIZE;
394 waste = zspage_size % class_size;
395 usedpc = (zspage_size - waste) * 100 / zspage_size;
396
397 if (usedpc > max_usedpc) {
398 max_usedpc = usedpc;
399 max_usedpc_order = i;
400 }
401 }
402
403 return max_usedpc_order;
404}
405
406/*
407 * A single 'zspage' is composed of many system pages which are
408 * linked together using fields in struct page. This function finds
409 * the first/head page, given any component page of a zspage.
410 */
411static struct page *get_first_page(struct page *page)
412{
413 if (is_first_page(page))
414 return page;
415 else
416 return page->first_page;
417}
418
419static struct page *get_next_page(struct page *page)
420{
421 struct page *next;
422
423 if (is_last_page(page))
424 next = NULL;
425 else if (is_first_page(page))
e842b976 426 next = (struct page *)page_private(page);
61989a80
NG
427 else
428 next = list_entry(page->lru.next, struct page, lru);
429
430 return next;
431}
432
67296874
OH
433/*
434 * Encode <page, obj_idx> as a single handle value.
435 * On hardware platforms with physical memory starting at 0x0 the pfn
436 * could be 0 so we ensure that the handle will never be 0 by adjusting the
437 * encoded obj_idx value before encoding.
438 */
61989a80
NG
439static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
440{
441 unsigned long handle;
442
443 if (!page) {
444 BUG_ON(obj_idx);
445 return NULL;
446 }
447
448 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
67296874 449 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
61989a80
NG
450
451 return (void *)handle;
452}
453
67296874
OH
454/*
455 * Decode <page, obj_idx> pair from the given object handle. We adjust the
456 * decoded obj_idx back to its original value since it was adjusted in
457 * obj_location_to_handle().
458 */
c2344348 459static void obj_handle_to_location(unsigned long handle, struct page **page,
61989a80
NG
460 unsigned long *obj_idx)
461{
c2344348 462 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
67296874 463 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
61989a80
NG
464}
465
466static unsigned long obj_idx_to_offset(struct page *page,
467 unsigned long obj_idx, int class_size)
468{
469 unsigned long off = 0;
470
471 if (!is_first_page(page))
472 off = page->index;
473
474 return off + obj_idx * class_size;
475}
476
f4477e90
NG
477static void reset_page(struct page *page)
478{
479 clear_bit(PG_private, &page->flags);
480 clear_bit(PG_private_2, &page->flags);
481 set_page_private(page, 0);
482 page->mapping = NULL;
483 page->freelist = NULL;
22b751c3 484 page_mapcount_reset(page);
f4477e90
NG
485}
486
61989a80
NG
487static void free_zspage(struct page *first_page)
488{
f4477e90 489 struct page *nextp, *tmp, *head_extra;
61989a80
NG
490
491 BUG_ON(!is_first_page(first_page));
492 BUG_ON(first_page->inuse);
493
f4477e90 494 head_extra = (struct page *)page_private(first_page);
61989a80 495
f4477e90 496 reset_page(first_page);
61989a80
NG
497 __free_page(first_page);
498
499 /* zspage with only 1 system page */
f4477e90 500 if (!head_extra)
61989a80
NG
501 return;
502
f4477e90 503 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
61989a80 504 list_del(&nextp->lru);
f4477e90 505 reset_page(nextp);
61989a80
NG
506 __free_page(nextp);
507 }
f4477e90
NG
508 reset_page(head_extra);
509 __free_page(head_extra);
61989a80
NG
510}
511
512/* Initialize a newly allocated zspage */
513static void init_zspage(struct page *first_page, struct size_class *class)
514{
515 unsigned long off = 0;
516 struct page *page = first_page;
517
518 BUG_ON(!is_first_page(first_page));
519 while (page) {
520 struct page *next_page;
521 struct link_free *link;
522 unsigned int i, objs_on_page;
523
524 /*
525 * page->index stores offset of first object starting
526 * in the page. For the first page, this is always 0,
527 * so we use first_page->index (aka ->freelist) to store
528 * head of corresponding zspage's freelist.
529 */
530 if (page != first_page)
531 page->index = off;
532
533 link = (struct link_free *)kmap_atomic(page) +
534 off / sizeof(*link);
535 objs_on_page = (PAGE_SIZE - off) / class->size;
536
537 for (i = 1; i <= objs_on_page; i++) {
538 off += class->size;
539 if (off < PAGE_SIZE) {
540 link->next = obj_location_to_handle(page, i);
541 link += class->size / sizeof(*link);
542 }
543 }
544
545 /*
546 * We now come to the last (full or partial) object on this
547 * page, which must point to the first object on the next
548 * page (if present)
549 */
550 next_page = get_next_page(page);
551 link->next = obj_location_to_handle(next_page, 0);
552 kunmap_atomic(link);
553 page = next_page;
554 off = (off + class->size) % PAGE_SIZE;
555 }
556}
557
558/*
559 * Allocate a zspage for the given size class
560 */
561static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
562{
563 int i, error;
b4b700c5 564 struct page *first_page = NULL, *uninitialized_var(prev_page);
61989a80
NG
565
566 /*
567 * Allocate individual pages and link them together as:
568 * 1. first page->private = first sub-page
569 * 2. all sub-pages are linked together using page->lru
570 * 3. each sub-page is linked to the first page using page->first_page
571 *
572 * For each size class, First/Head pages are linked together using
573 * page->lru. Also, we set PG_private to identify the first page
574 * (i.e. no other sub-page has this flag set) and PG_private_2 to
575 * identify the last page.
576 */
577 error = -ENOMEM;
2e3b6154 578 for (i = 0; i < class->pages_per_zspage; i++) {
b4b700c5 579 struct page *page;
61989a80
NG
580
581 page = alloc_page(flags);
582 if (!page)
583 goto cleanup;
584
585 INIT_LIST_HEAD(&page->lru);
586 if (i == 0) { /* first page */
a27545bf 587 SetPagePrivate(page);
61989a80
NG
588 set_page_private(page, 0);
589 first_page = page;
590 first_page->inuse = 0;
591 }
592 if (i == 1)
e842b976 593 set_page_private(first_page, (unsigned long)page);
61989a80
NG
594 if (i >= 1)
595 page->first_page = first_page;
596 if (i >= 2)
597 list_add(&page->lru, &prev_page->lru);
2e3b6154 598 if (i == class->pages_per_zspage - 1) /* last page */
a27545bf 599 SetPagePrivate2(page);
61989a80
NG
600 prev_page = page;
601 }
602
603 init_zspage(first_page, class);
604
605 first_page->freelist = obj_location_to_handle(first_page, 0);
606 /* Maximum number of objects we can store in this zspage */
2e3b6154 607 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
61989a80
NG
608
609 error = 0; /* Success */
610
611cleanup:
612 if (unlikely(error) && first_page) {
613 free_zspage(first_page);
614 first_page = NULL;
615 }
616
617 return first_page;
618}
619
620static struct page *find_get_zspage(struct size_class *class)
621{
622 int i;
623 struct page *page;
624
625 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
626 page = class->fullness_list[i];
627 if (page)
628 break;
629 }
630
631 return page;
632}
633
f553646a
SJ
634#ifdef USE_PGTABLE_MAPPING
635static inline int __zs_cpu_up(struct mapping_area *area)
636{
637 /*
638 * Make sure we don't leak memory if a cpu UP notification
639 * and zs_init() race and both call zs_cpu_up() on the same cpu
640 */
641 if (area->vm)
642 return 0;
643 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
644 if (!area->vm)
645 return -ENOMEM;
646 return 0;
647}
648
649static inline void __zs_cpu_down(struct mapping_area *area)
650{
651 if (area->vm)
652 free_vm_area(area->vm);
653 area->vm = NULL;
654}
655
656static inline void *__zs_map_object(struct mapping_area *area,
657 struct page *pages[2], int off, int size)
658{
659 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
660 area->vm_addr = area->vm->addr;
661 return area->vm_addr + off;
662}
663
664static inline void __zs_unmap_object(struct mapping_area *area,
665 struct page *pages[2], int off, int size)
666{
667 unsigned long addr = (unsigned long)area->vm_addr;
f553646a 668
d95abbbb 669 unmap_kernel_range(addr, PAGE_SIZE * 2);
f553646a
SJ
670}
671
672#else /* USE_PGTABLE_MAPPING */
673
674static inline int __zs_cpu_up(struct mapping_area *area)
675{
676 /*
677 * Make sure we don't leak memory if a cpu UP notification
678 * and zs_init() race and both call zs_cpu_up() on the same cpu
679 */
680 if (area->vm_buf)
681 return 0;
682 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
683 if (!area->vm_buf)
684 return -ENOMEM;
685 return 0;
686}
687
688static inline void __zs_cpu_down(struct mapping_area *area)
689{
690 if (area->vm_buf)
691 free_page((unsigned long)area->vm_buf);
692 area->vm_buf = NULL;
693}
694
695static void *__zs_map_object(struct mapping_area *area,
696 struct page *pages[2], int off, int size)
5f601902 697{
5f601902
SJ
698 int sizes[2];
699 void *addr;
f553646a 700 char *buf = area->vm_buf;
5f601902 701
f553646a
SJ
702 /* disable page faults to match kmap_atomic() return conditions */
703 pagefault_disable();
704
705 /* no read fastpath */
706 if (area->vm_mm == ZS_MM_WO)
707 goto out;
5f601902
SJ
708
709 sizes[0] = PAGE_SIZE - off;
710 sizes[1] = size - sizes[0];
711
5f601902
SJ
712 /* copy object to per-cpu buffer */
713 addr = kmap_atomic(pages[0]);
714 memcpy(buf, addr + off, sizes[0]);
715 kunmap_atomic(addr);
716 addr = kmap_atomic(pages[1]);
717 memcpy(buf + sizes[0], addr, sizes[1]);
718 kunmap_atomic(addr);
f553646a
SJ
719out:
720 return area->vm_buf;
5f601902
SJ
721}
722
f553646a
SJ
723static void __zs_unmap_object(struct mapping_area *area,
724 struct page *pages[2], int off, int size)
5f601902 725{
5f601902
SJ
726 int sizes[2];
727 void *addr;
f553646a 728 char *buf = area->vm_buf;
5f601902 729
f553646a
SJ
730 /* no write fastpath */
731 if (area->vm_mm == ZS_MM_RO)
732 goto out;
5f601902
SJ
733
734 sizes[0] = PAGE_SIZE - off;
735 sizes[1] = size - sizes[0];
736
737 /* copy per-cpu buffer to object */
738 addr = kmap_atomic(pages[0]);
739 memcpy(addr + off, buf, sizes[0]);
740 kunmap_atomic(addr);
741 addr = kmap_atomic(pages[1]);
742 memcpy(addr, buf + sizes[0], sizes[1]);
743 kunmap_atomic(addr);
f553646a
SJ
744
745out:
746 /* enable page faults to match kunmap_atomic() return conditions */
747 pagefault_enable();
5f601902 748}
61989a80 749
f553646a
SJ
750#endif /* USE_PGTABLE_MAPPING */
751
61989a80
NG
752static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
753 void *pcpu)
754{
f553646a 755 int ret, cpu = (long)pcpu;
61989a80
NG
756 struct mapping_area *area;
757
758 switch (action) {
759 case CPU_UP_PREPARE:
760 area = &per_cpu(zs_map_area, cpu);
f553646a
SJ
761 ret = __zs_cpu_up(area);
762 if (ret)
763 return notifier_from_errno(ret);
61989a80
NG
764 break;
765 case CPU_DEAD:
766 case CPU_UP_CANCELED:
767 area = &per_cpu(zs_map_area, cpu);
f553646a 768 __zs_cpu_down(area);
61989a80
NG
769 break;
770 }
771
772 return NOTIFY_OK;
773}
774
775static struct notifier_block zs_cpu_nb = {
776 .notifier_call = zs_cpu_notifier
777};
778
779static void zs_exit(void)
780{
781 int cpu;
782
783 for_each_online_cpu(cpu)
784 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
785 unregister_cpu_notifier(&zs_cpu_nb);
786}
787
788static int zs_init(void)
789{
790 int cpu, ret;
791
792 register_cpu_notifier(&zs_cpu_nb);
793 for_each_online_cpu(cpu) {
794 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
795 if (notifier_to_errno(ret))
796 goto fail;
797 }
798 return 0;
799fail:
800 zs_exit();
801 return notifier_to_errno(ret);
802}
803
4bbc0bc0
DB
804/**
805 * zs_create_pool - Creates an allocation pool to work from.
0d145a50 806 * @flags: allocation flags used to allocate pool metadata
4bbc0bc0
DB
807 *
808 * This function must be called before anything when using
809 * the zsmalloc allocator.
810 *
811 * On success, a pointer to the newly created pool is returned,
812 * otherwise NULL.
813 */
0d145a50 814struct zs_pool *zs_create_pool(gfp_t flags)
61989a80 815{
069f101f 816 int i, ovhd_size;
61989a80
NG
817 struct zs_pool *pool;
818
61989a80
NG
819 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
820 pool = kzalloc(ovhd_size, GFP_KERNEL);
821 if (!pool)
822 return NULL;
823
824 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
825 int size;
826 struct size_class *class;
827
828 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
829 if (size > ZS_MAX_ALLOC_SIZE)
830 size = ZS_MAX_ALLOC_SIZE;
831
832 class = &pool->size_class[i];
833 class->size = size;
834 class->index = i;
835 spin_lock_init(&class->lock);
2e3b6154 836 class->pages_per_zspage = get_pages_per_zspage(size);
61989a80
NG
837
838 }
839
61989a80 840 pool->flags = flags;
61989a80 841
61989a80
NG
842 return pool;
843}
844EXPORT_SYMBOL_GPL(zs_create_pool);
845
846void zs_destroy_pool(struct zs_pool *pool)
847{
848 int i;
849
850 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
851 int fg;
852 struct size_class *class = &pool->size_class[i];
853
854 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
855 if (class->fullness_list[fg]) {
93ad5ab5 856 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
61989a80
NG
857 class->size, fg);
858 }
859 }
860 }
861 kfree(pool);
862}
863EXPORT_SYMBOL_GPL(zs_destroy_pool);
864
865/**
866 * zs_malloc - Allocate block of given size from pool.
867 * @pool: pool to allocate from
868 * @size: size of block to allocate
61989a80 869 *
00a61d86 870 * On success, handle to the allocated object is returned,
c2344348 871 * otherwise 0.
61989a80
NG
872 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
873 */
c2344348 874unsigned long zs_malloc(struct zs_pool *pool, size_t size)
61989a80 875{
c2344348 876 unsigned long obj;
61989a80
NG
877 struct link_free *link;
878 int class_idx;
879 struct size_class *class;
880
881 struct page *first_page, *m_page;
882 unsigned long m_objidx, m_offset;
883
884 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
c2344348 885 return 0;
61989a80
NG
886
887 class_idx = get_size_class_index(size);
888 class = &pool->size_class[class_idx];
889 BUG_ON(class_idx != class->index);
890
891 spin_lock(&class->lock);
892 first_page = find_get_zspage(class);
893
894 if (!first_page) {
895 spin_unlock(&class->lock);
896 first_page = alloc_zspage(class, pool->flags);
897 if (unlikely(!first_page))
c2344348 898 return 0;
61989a80
NG
899
900 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
901 spin_lock(&class->lock);
2e3b6154 902 class->pages_allocated += class->pages_per_zspage;
61989a80
NG
903 }
904
c2344348 905 obj = (unsigned long)first_page->freelist;
61989a80
NG
906 obj_handle_to_location(obj, &m_page, &m_objidx);
907 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
908
909 link = (struct link_free *)kmap_atomic(m_page) +
910 m_offset / sizeof(*link);
911 first_page->freelist = link->next;
912 memset(link, POISON_INUSE, sizeof(*link));
913 kunmap_atomic(link);
914
915 first_page->inuse++;
916 /* Now move the zspage to another fullness group, if required */
917 fix_fullness_group(pool, first_page);
918 spin_unlock(&class->lock);
919
920 return obj;
921}
922EXPORT_SYMBOL_GPL(zs_malloc);
923
c2344348 924void zs_free(struct zs_pool *pool, unsigned long obj)
61989a80
NG
925{
926 struct link_free *link;
927 struct page *first_page, *f_page;
928 unsigned long f_objidx, f_offset;
929
930 int class_idx;
931 struct size_class *class;
932 enum fullness_group fullness;
933
934 if (unlikely(!obj))
935 return;
936
937 obj_handle_to_location(obj, &f_page, &f_objidx);
938 first_page = get_first_page(f_page);
939
940 get_zspage_mapping(first_page, &class_idx, &fullness);
941 class = &pool->size_class[class_idx];
942 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
943
944 spin_lock(&class->lock);
945
946 /* Insert this object in containing zspage's freelist */
947 link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
948 + f_offset);
949 link->next = first_page->freelist;
950 kunmap_atomic(link);
c2344348 951 first_page->freelist = (void *)obj;
61989a80
NG
952
953 first_page->inuse--;
954 fullness = fix_fullness_group(pool, first_page);
955
956 if (fullness == ZS_EMPTY)
2e3b6154 957 class->pages_allocated -= class->pages_per_zspage;
61989a80
NG
958
959 spin_unlock(&class->lock);
960
961 if (fullness == ZS_EMPTY)
962 free_zspage(first_page);
963}
964EXPORT_SYMBOL_GPL(zs_free);
965
00a61d86
MK
966/**
967 * zs_map_object - get address of allocated object from handle.
968 * @pool: pool from which the object was allocated
969 * @handle: handle returned from zs_malloc
970 *
971 * Before using an object allocated from zs_malloc, it must be mapped using
972 * this function. When done with the object, it must be unmapped using
166cfda7
SJ
973 * zs_unmap_object.
974 *
975 * Only one object can be mapped per cpu at a time. There is no protection
976 * against nested mappings.
977 *
978 * This function returns with preemption and page faults disabled.
396b7fd6 979 */
b7418510
SJ
980void *zs_map_object(struct zs_pool *pool, unsigned long handle,
981 enum zs_mapmode mm)
61989a80
NG
982{
983 struct page *page;
984 unsigned long obj_idx, off;
985
986 unsigned int class_idx;
987 enum fullness_group fg;
988 struct size_class *class;
989 struct mapping_area *area;
f553646a 990 struct page *pages[2];
61989a80
NG
991
992 BUG_ON(!handle);
993
c60369f0
SJ
994 /*
995 * Because we use per-cpu mapping areas shared among the
996 * pools/users, we can't allow mapping in interrupt context
997 * because it can corrupt another users mappings.
998 */
999 BUG_ON(in_interrupt());
1000
61989a80
NG
1001 obj_handle_to_location(handle, &page, &obj_idx);
1002 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1003 class = &pool->size_class[class_idx];
1004 off = obj_idx_to_offset(page, obj_idx, class->size);
1005
1006 area = &get_cpu_var(zs_map_area);
f553646a 1007 area->vm_mm = mm;
61989a80
NG
1008 if (off + class->size <= PAGE_SIZE) {
1009 /* this object is contained entirely within a page */
1010 area->vm_addr = kmap_atomic(page);
5f601902 1011 return area->vm_addr + off;
61989a80
NG
1012 }
1013
f553646a
SJ
1014 /* this object spans two pages */
1015 pages[0] = page;
1016 pages[1] = get_next_page(page);
1017 BUG_ON(!pages[1]);
b7418510 1018
f553646a 1019 return __zs_map_object(area, pages, off, class->size);
61989a80
NG
1020}
1021EXPORT_SYMBOL_GPL(zs_map_object);
1022
c2344348 1023void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
61989a80
NG
1024{
1025 struct page *page;
1026 unsigned long obj_idx, off;
1027
1028 unsigned int class_idx;
1029 enum fullness_group fg;
1030 struct size_class *class;
1031 struct mapping_area *area;
1032
1033 BUG_ON(!handle);
1034
1035 obj_handle_to_location(handle, &page, &obj_idx);
1036 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1037 class = &pool->size_class[class_idx];
1038 off = obj_idx_to_offset(page, obj_idx, class->size);
1039
f553646a
SJ
1040 area = &__get_cpu_var(zs_map_area);
1041 if (off + class->size <= PAGE_SIZE)
1042 kunmap_atomic(area->vm_addr);
1043 else {
1044 struct page *pages[2];
1045
1046 pages[0] = page;
1047 pages[1] = get_next_page(page);
1048 BUG_ON(!pages[1]);
b7418510 1049
f553646a
SJ
1050 __zs_unmap_object(area, pages, off, class->size);
1051 }
61989a80
NG
1052 put_cpu_var(zs_map_area);
1053}
1054EXPORT_SYMBOL_GPL(zs_unmap_object);
1055
1056u64 zs_get_total_size_bytes(struct zs_pool *pool)
1057{
1058 int i;
1059 u64 npages = 0;
1060
1061 for (i = 0; i < ZS_SIZE_CLASSES; i++)
1062 npages += pool->size_class[i].pages_allocated;
1063
1064 return npages << PAGE_SHIFT;
1065}
1066EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
069f101f
BH
1067
1068module_init(zs_init);
1069module_exit(zs_exit);
1070
1071MODULE_LICENSE("Dual BSD/GPL");
1072MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");