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