2 * zsmalloc memory allocator
4 * Copyright (C) 2011 Nitin Gupta
5 * Copyright (C) 2012, 2013 Minchan Kim
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
16 * This allocator is designed for use with zcache and zram. Thus, the
17 * allocator is supposed to work well under low memory conditions. In
18 * particular, it never attempts higher order page allocation which is
19 * very likely to fail under memory pressure. On the other hand, if we
20 * just use single (0-order) pages, it would suffer from very high
21 * fragmentation -- any object of size PAGE_SIZE/2 or larger would occupy
22 * an entire page. This was one of the major issues with its predecessor
25 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
26 * and links them together using various 'struct page' fields. These linked
27 * pages act as a single higher-order page i.e. an object can span 0-order
28 * page boundaries. The code refers to these linked pages as a single entity
31 * Following is how we use various fields and flags of underlying
32 * struct page(s) to form a zspage.
34 * Usage of struct page fields:
35 * page->first_page: points to the first component (0-order) page
36 * page->index (union with page->freelist): offset of the first object
37 * starting in this page. For the first page, this is
38 * always 0, so we use this field (aka freelist) to point
39 * to the first free object in zspage.
40 * page->lru: links together all component pages (except the first page)
43 * For _first_ page only:
45 * page->private (union with page->first_page): refers to the
46 * component page after the first page
47 * page->freelist: points to the first free object in zspage.
48 * Free objects are linked together using in-place
50 * page->objects: maximum number of objects we can store in this
51 * zspage (class->zspage_order * PAGE_SIZE / class->size)
52 * page->lru: links together first pages of various zspages.
53 * Basically forming list of zspages in a fullness group.
54 * page->mapping: class index and fullness group of the zspage
56 * Usage of struct page flags:
57 * PG_private: identifies the first component page
58 * PG_private2: identifies the last component page
62 #ifdef CONFIG_ZSMALLOC_DEBUG
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/bitops.h>
69 #include <linux/errno.h>
70 #include <linux/highmem.h>
71 #include <linux/init.h>
72 #include <linux/string.h>
73 #include <linux/slab.h>
74 #include <asm/tlbflush.h>
75 #include <asm/pgtable.h>
76 #include <linux/cpumask.h>
77 #include <linux/cpu.h>
78 #include <linux/vmalloc.h>
79 #include <linux/hardirq.h>
80 #include <linux/spinlock.h>
81 #include <linux/types.h>
82 #include <linux/zsmalloc.h>
83 #include <linux/zpool.h>
86 * This must be power of 2 and greater than of equal to sizeof(link_free).
87 * These two conditions ensure that any 'struct link_free' itself doesn't
88 * span more than 1 page which avoids complex case of mapping 2 pages simply
89 * to restore link_free pointer values.
94 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
95 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
97 #define ZS_MAX_ZSPAGE_ORDER 2
98 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
101 * Object location (<PFN>, <obj_idx>) is encoded as
102 * as single (void *) handle value.
104 * Note that object index <obj_idx> is relative to system
105 * page <PFN> it is stored in, so for each sub-page belonging
106 * to a zspage, obj_idx starts with 0.
108 * This is made more complicated by various memory models and PAE.
111 #ifndef MAX_PHYSMEM_BITS
112 #ifdef CONFIG_HIGHMEM64G
113 #define MAX_PHYSMEM_BITS 36
114 #else /* !CONFIG_HIGHMEM64G */
116 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
119 #define MAX_PHYSMEM_BITS BITS_PER_LONG
122 #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
123 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
124 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
126 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
127 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
128 #define ZS_MIN_ALLOC_SIZE \
129 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
130 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
133 * On systems with 4K page size, this gives 255 size classes! There is a
135 * - Large number of size classes is potentially wasteful as free page are
136 * spread across these classes
137 * - Small number of size classes causes large internal fragmentation
138 * - Probably its better to use specific size classes (empirically
139 * determined). NOTE: all those class sizes must be set as multiple of
140 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
142 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
145 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
146 #define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
147 ZS_SIZE_CLASS_DELTA + 1)
150 * We do not maintain any list for completely empty or full pages
152 enum fullness_group {
155 _ZS_NR_FULLNESS_GROUPS,
162 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
164 * n = number of allocated objects
165 * N = total number of objects zspage can store
166 * f = fullness_threshold_frac
168 * Similarly, we assign zspage to:
169 * ZS_ALMOST_FULL when n > N / f
170 * ZS_EMPTY when n == 0
171 * ZS_FULL when n == N
173 * (see: fix_fullness_group())
175 static const int fullness_threshold_frac = 4;
179 * Size of objects stored in this class. Must be multiple
185 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
186 int pages_per_zspage;
190 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
194 * Placed within free objects to form a singly linked list.
195 * For every zspage, first_page->freelist gives head of this list.
197 * This must be power of 2 and less than or equal to ZS_ALIGN
200 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
205 struct size_class *size_class[ZS_SIZE_CLASSES];
207 gfp_t flags; /* allocation flags used when growing pool */
208 atomic_long_t pages_allocated;
212 * A zspage's class index and fullness group
213 * are encoded in its (first)page->mapping
215 #define CLASS_IDX_BITS 28
216 #define FULLNESS_BITS 4
217 #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
218 #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
221 * By default, zsmalloc uses a copy-based object mapping method to access
222 * allocations that span two pages. However, if a particular architecture
223 * performs VM mapping faster than copying, then it should be added here
224 * so that USE_PGTABLE_MAPPING is defined. This causes zsmalloc to use
225 * page table mapping rather than copying for object mapping.
227 #if defined(CONFIG_ARM) && !defined(MODULE)
228 #define USE_PGTABLE_MAPPING
231 struct mapping_area {
232 #ifdef USE_PGTABLE_MAPPING
233 struct vm_struct *vm; /* vm area for mapping object that span pages */
235 char *vm_buf; /* copy buffer for objects that span pages */
237 char *vm_addr; /* address of kmap_atomic()'ed pages */
238 enum zs_mapmode vm_mm; /* mapping mode */
245 static void *zs_zpool_create(gfp_t gfp, struct zpool_ops *zpool_ops)
247 return zs_create_pool(gfp);
250 static void zs_zpool_destroy(void *pool)
252 zs_destroy_pool(pool);
255 static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
256 unsigned long *handle)
258 *handle = zs_malloc(pool, size);
259 return *handle ? 0 : -1;
261 static void zs_zpool_free(void *pool, unsigned long handle)
263 zs_free(pool, handle);
266 static int zs_zpool_shrink(void *pool, unsigned int pages,
267 unsigned int *reclaimed)
272 static void *zs_zpool_map(void *pool, unsigned long handle,
273 enum zpool_mapmode mm)
275 enum zs_mapmode zs_mm;
284 case ZPOOL_MM_RW: /* fallthru */
290 return zs_map_object(pool, handle, zs_mm);
292 static void zs_zpool_unmap(void *pool, unsigned long handle)
294 zs_unmap_object(pool, handle);
297 static u64 zs_zpool_total_size(void *pool)
299 return zs_get_total_pages(pool) << PAGE_SHIFT;
302 static struct zpool_driver zs_zpool_driver = {
304 .owner = THIS_MODULE,
305 .create = zs_zpool_create,
306 .destroy = zs_zpool_destroy,
307 .malloc = zs_zpool_malloc,
308 .free = zs_zpool_free,
309 .shrink = zs_zpool_shrink,
311 .unmap = zs_zpool_unmap,
312 .total_size = zs_zpool_total_size,
315 MODULE_ALIAS("zpool-zsmalloc");
316 #endif /* CONFIG_ZPOOL */
318 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
319 static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
321 static int is_first_page(struct page *page)
323 return PagePrivate(page);
326 static int is_last_page(struct page *page)
328 return PagePrivate2(page);
331 static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
332 enum fullness_group *fullness)
335 BUG_ON(!is_first_page(page));
337 m = (unsigned long)page->mapping;
338 *fullness = m & FULLNESS_MASK;
339 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
342 static void set_zspage_mapping(struct page *page, unsigned int class_idx,
343 enum fullness_group fullness)
346 BUG_ON(!is_first_page(page));
348 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
349 (fullness & FULLNESS_MASK);
350 page->mapping = (struct address_space *)m;
353 static int get_size_class_index(int size)
357 if (likely(size > ZS_MIN_ALLOC_SIZE))
358 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
359 ZS_SIZE_CLASS_DELTA);
364 static enum fullness_group get_fullness_group(struct page *page)
366 int inuse, max_objects;
367 enum fullness_group fg;
368 BUG_ON(!is_first_page(page));
371 max_objects = page->objects;
375 else if (inuse == max_objects)
377 else if (inuse <= max_objects / fullness_threshold_frac)
378 fg = ZS_ALMOST_EMPTY;
385 static void insert_zspage(struct page *page, struct size_class *class,
386 enum fullness_group fullness)
390 BUG_ON(!is_first_page(page));
392 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
395 head = &class->fullness_list[fullness];
397 list_add_tail(&page->lru, &(*head)->lru);
402 static void remove_zspage(struct page *page, struct size_class *class,
403 enum fullness_group fullness)
407 BUG_ON(!is_first_page(page));
409 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
412 head = &class->fullness_list[fullness];
414 if (list_empty(&(*head)->lru))
416 else if (*head == page)
417 *head = (struct page *)list_entry((*head)->lru.next,
420 list_del_init(&page->lru);
423 static enum fullness_group fix_fullness_group(struct zs_pool *pool,
427 struct size_class *class;
428 enum fullness_group currfg, newfg;
430 BUG_ON(!is_first_page(page));
432 get_zspage_mapping(page, &class_idx, &currfg);
433 newfg = get_fullness_group(page);
437 class = pool->size_class[class_idx];
438 remove_zspage(page, class, currfg);
439 insert_zspage(page, class, newfg);
440 set_zspage_mapping(page, class_idx, newfg);
447 * We have to decide on how many pages to link together
448 * to form a zspage for each size class. This is important
449 * to reduce wastage due to unusable space left at end of
450 * each zspage which is given as:
451 * wastage = Zp - Zp % size_class
452 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
454 * For example, for size class of 3/8 * PAGE_SIZE, we should
455 * link together 3 PAGE_SIZE sized pages to form a zspage
456 * since then we can perfectly fit in 8 such objects.
458 static int get_pages_per_zspage(int class_size)
460 int i, max_usedpc = 0;
461 /* zspage order which gives maximum used size per KB */
462 int max_usedpc_order = 1;
464 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
468 zspage_size = i * PAGE_SIZE;
469 waste = zspage_size % class_size;
470 usedpc = (zspage_size - waste) * 100 / zspage_size;
472 if (usedpc > max_usedpc) {
474 max_usedpc_order = i;
478 return max_usedpc_order;
482 * A single 'zspage' is composed of many system pages which are
483 * linked together using fields in struct page. This function finds
484 * the first/head page, given any component page of a zspage.
486 static struct page *get_first_page(struct page *page)
488 if (is_first_page(page))
491 return page->first_page;
494 static struct page *get_next_page(struct page *page)
498 if (is_last_page(page))
500 else if (is_first_page(page))
501 next = (struct page *)page->private;
503 next = list_entry(page->lru.next, struct page, lru);
509 * Encode <page, obj_idx> as a single handle value.
510 * On hardware platforms with physical memory starting at 0x0 the pfn
511 * could be 0 so we ensure that the handle will never be 0 by adjusting the
512 * encoded obj_idx value before encoding.
514 static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
516 unsigned long handle;
523 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
524 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
526 return (void *)handle;
530 * Decode <page, obj_idx> pair from the given object handle. We adjust the
531 * decoded obj_idx back to its original value since it was adjusted in
532 * obj_location_to_handle().
534 static void obj_handle_to_location(unsigned long handle, struct page **page,
535 unsigned long *obj_idx)
537 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
538 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
541 static unsigned long obj_idx_to_offset(struct page *page,
542 unsigned long obj_idx, int class_size)
544 unsigned long off = 0;
546 if (!is_first_page(page))
549 return off + obj_idx * class_size;
552 static void reset_page(struct page *page)
554 clear_bit(PG_private, &page->flags);
555 clear_bit(PG_private_2, &page->flags);
556 set_page_private(page, 0);
557 page->mapping = NULL;
558 page->freelist = NULL;
559 page_mapcount_reset(page);
562 static void free_zspage(struct page *first_page)
564 struct page *nextp, *tmp, *head_extra;
566 BUG_ON(!is_first_page(first_page));
567 BUG_ON(first_page->inuse);
569 head_extra = (struct page *)page_private(first_page);
571 reset_page(first_page);
572 __free_page(first_page);
574 /* zspage with only 1 system page */
578 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
579 list_del(&nextp->lru);
583 reset_page(head_extra);
584 __free_page(head_extra);
587 /* Initialize a newly allocated zspage */
588 static void init_zspage(struct page *first_page, struct size_class *class)
590 unsigned long off = 0;
591 struct page *page = first_page;
593 BUG_ON(!is_first_page(first_page));
595 struct page *next_page;
596 struct link_free *link;
601 * page->index stores offset of first object starting
602 * in the page. For the first page, this is always 0,
603 * so we use first_page->index (aka ->freelist) to store
604 * head of corresponding zspage's freelist.
606 if (page != first_page)
609 vaddr = kmap_atomic(page);
610 link = (struct link_free *)vaddr + off / sizeof(*link);
612 while ((off += class->size) < PAGE_SIZE) {
613 link->next = obj_location_to_handle(page, i++);
614 link += class->size / sizeof(*link);
618 * We now come to the last (full or partial) object on this
619 * page, which must point to the first object on the next
622 next_page = get_next_page(page);
623 link->next = obj_location_to_handle(next_page, 0);
624 kunmap_atomic(vaddr);
631 * Allocate a zspage for the given size class
633 static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
636 struct page *first_page = NULL, *uninitialized_var(prev_page);
639 * Allocate individual pages and link them together as:
640 * 1. first page->private = first sub-page
641 * 2. all sub-pages are linked together using page->lru
642 * 3. each sub-page is linked to the first page using page->first_page
644 * For each size class, First/Head pages are linked together using
645 * page->lru. Also, we set PG_private to identify the first page
646 * (i.e. no other sub-page has this flag set) and PG_private_2 to
647 * identify the last page.
650 for (i = 0; i < class->pages_per_zspage; i++) {
653 page = alloc_page(flags);
657 INIT_LIST_HEAD(&page->lru);
658 if (i == 0) { /* first page */
659 SetPagePrivate(page);
660 set_page_private(page, 0);
662 first_page->inuse = 0;
665 first_page->private = (unsigned long)page;
667 page->first_page = first_page;
669 list_add(&page->lru, &prev_page->lru);
670 if (i == class->pages_per_zspage - 1) /* last page */
671 SetPagePrivate2(page);
675 init_zspage(first_page, class);
677 first_page->freelist = obj_location_to_handle(first_page, 0);
678 /* Maximum number of objects we can store in this zspage */
679 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
681 error = 0; /* Success */
684 if (unlikely(error) && first_page) {
685 free_zspage(first_page);
692 static struct page *find_get_zspage(struct size_class *class)
697 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
698 page = class->fullness_list[i];
706 #ifdef USE_PGTABLE_MAPPING
707 static inline int __zs_cpu_up(struct mapping_area *area)
710 * Make sure we don't leak memory if a cpu UP notification
711 * and zs_init() race and both call zs_cpu_up() on the same cpu
715 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
721 static inline void __zs_cpu_down(struct mapping_area *area)
724 free_vm_area(area->vm);
728 static inline void *__zs_map_object(struct mapping_area *area,
729 struct page *pages[2], int off, int size)
731 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
732 area->vm_addr = area->vm->addr;
733 return area->vm_addr + off;
736 static inline void __zs_unmap_object(struct mapping_area *area,
737 struct page *pages[2], int off, int size)
739 unsigned long addr = (unsigned long)area->vm_addr;
741 unmap_kernel_range(addr, PAGE_SIZE * 2);
744 #else /* USE_PGTABLE_MAPPING */
746 static inline int __zs_cpu_up(struct mapping_area *area)
749 * Make sure we don't leak memory if a cpu UP notification
750 * and zs_init() race and both call zs_cpu_up() on the same cpu
754 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
760 static inline void __zs_cpu_down(struct mapping_area *area)
763 free_page((unsigned long)area->vm_buf);
767 static void *__zs_map_object(struct mapping_area *area,
768 struct page *pages[2], int off, int size)
772 char *buf = area->vm_buf;
774 /* disable page faults to match kmap_atomic() return conditions */
777 /* no read fastpath */
778 if (area->vm_mm == ZS_MM_WO)
781 sizes[0] = PAGE_SIZE - off;
782 sizes[1] = size - sizes[0];
784 /* copy object to per-cpu buffer */
785 addr = kmap_atomic(pages[0]);
786 memcpy(buf, addr + off, sizes[0]);
788 addr = kmap_atomic(pages[1]);
789 memcpy(buf + sizes[0], addr, sizes[1]);
795 static void __zs_unmap_object(struct mapping_area *area,
796 struct page *pages[2], int off, int size)
800 char *buf = area->vm_buf;
802 /* no write fastpath */
803 if (area->vm_mm == ZS_MM_RO)
806 sizes[0] = PAGE_SIZE - off;
807 sizes[1] = size - sizes[0];
809 /* copy per-cpu buffer to object */
810 addr = kmap_atomic(pages[0]);
811 memcpy(addr + off, buf, sizes[0]);
813 addr = kmap_atomic(pages[1]);
814 memcpy(addr, buf + sizes[0], sizes[1]);
818 /* enable page faults to match kunmap_atomic() return conditions */
822 #endif /* USE_PGTABLE_MAPPING */
824 static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
827 int ret, cpu = (long)pcpu;
828 struct mapping_area *area;
832 area = &per_cpu(zs_map_area, cpu);
833 ret = __zs_cpu_up(area);
835 return notifier_from_errno(ret);
838 case CPU_UP_CANCELED:
839 area = &per_cpu(zs_map_area, cpu);
847 static struct notifier_block zs_cpu_nb = {
848 .notifier_call = zs_cpu_notifier
851 static void zs_unregister_cpu_notifier(void)
855 cpu_notifier_register_begin();
857 for_each_online_cpu(cpu)
858 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
859 __unregister_cpu_notifier(&zs_cpu_nb);
861 cpu_notifier_register_done();
864 static int zs_register_cpu_notifier(void)
866 int cpu, uninitialized_var(ret);
868 cpu_notifier_register_begin();
870 __register_cpu_notifier(&zs_cpu_nb);
871 for_each_online_cpu(cpu) {
872 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
873 if (notifier_to_errno(ret))
877 cpu_notifier_register_done();
878 return notifier_to_errno(ret);
881 static void __exit zs_exit(void)
884 zpool_unregister_driver(&zs_zpool_driver);
886 zs_unregister_cpu_notifier();
889 static int __init zs_init(void)
891 int ret = zs_register_cpu_notifier();
894 zs_unregister_cpu_notifier();
899 zpool_register_driver(&zs_zpool_driver);
904 static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
906 return pages_per_zspage * PAGE_SIZE / size;
909 static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
911 if (prev->pages_per_zspage != pages_per_zspage)
914 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
915 != get_maxobj_per_zspage(size, pages_per_zspage))
922 * zs_create_pool - Creates an allocation pool to work from.
923 * @flags: allocation flags used to allocate pool metadata
925 * This function must be called before anything when using
926 * the zsmalloc allocator.
928 * On success, a pointer to the newly created pool is returned,
931 struct zs_pool *zs_create_pool(gfp_t flags)
934 struct zs_pool *pool;
936 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
937 pool = kzalloc(ovhd_size, GFP_KERNEL);
942 * Iterate reversly, because, size of size_class that we want to use
943 * for merging should be larger or equal to current size.
945 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
947 int pages_per_zspage;
948 struct size_class *class;
949 struct size_class *prev_class;
951 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
952 if (size > ZS_MAX_ALLOC_SIZE)
953 size = ZS_MAX_ALLOC_SIZE;
954 pages_per_zspage = get_pages_per_zspage(size);
957 * size_class is used for normal zsmalloc operation such
958 * as alloc/free for that size. Although it is natural that we
959 * have one size_class for each size, there is a chance that we
960 * can get more memory utilization if we use one size_class for
961 * many different sizes whose size_class have same
962 * characteristics. So, we makes size_class point to
963 * previous size_class if possible.
965 if (i < ZS_SIZE_CLASSES - 1) {
966 prev_class = pool->size_class[i + 1];
967 if (can_merge(prev_class, size, pages_per_zspage)) {
968 pool->size_class[i] = prev_class;
973 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
979 class->pages_per_zspage = pages_per_zspage;
980 spin_lock_init(&class->lock);
981 pool->size_class[i] = class;
989 zs_destroy_pool(pool);
992 EXPORT_SYMBOL_GPL(zs_create_pool);
994 void zs_destroy_pool(struct zs_pool *pool)
998 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
1000 struct size_class *class = pool->size_class[i];
1005 if (class->index != i)
1008 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
1009 if (class->fullness_list[fg]) {
1010 pr_info("Freeing non-empty class with size "
1011 "%db, fullness group %d\n",
1019 EXPORT_SYMBOL_GPL(zs_destroy_pool);
1022 * zs_malloc - Allocate block of given size from pool.
1023 * @pool: pool to allocate from
1024 * @size: size of block to allocate
1026 * On success, handle to the allocated object is returned,
1028 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1030 unsigned long zs_malloc(struct zs_pool *pool, size_t size)
1033 struct link_free *link;
1034 struct size_class *class;
1037 struct page *first_page, *m_page;
1038 unsigned long m_objidx, m_offset;
1040 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
1043 class = pool->size_class[get_size_class_index(size)];
1045 spin_lock(&class->lock);
1046 first_page = find_get_zspage(class);
1049 spin_unlock(&class->lock);
1050 first_page = alloc_zspage(class, pool->flags);
1051 if (unlikely(!first_page))
1054 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
1055 atomic_long_add(class->pages_per_zspage,
1056 &pool->pages_allocated);
1057 spin_lock(&class->lock);
1060 obj = (unsigned long)first_page->freelist;
1061 obj_handle_to_location(obj, &m_page, &m_objidx);
1062 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
1064 vaddr = kmap_atomic(m_page);
1065 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
1066 first_page->freelist = link->next;
1067 memset(link, POISON_INUSE, sizeof(*link));
1068 kunmap_atomic(vaddr);
1070 first_page->inuse++;
1071 /* Now move the zspage to another fullness group, if required */
1072 fix_fullness_group(pool, first_page);
1073 spin_unlock(&class->lock);
1077 EXPORT_SYMBOL_GPL(zs_malloc);
1079 void zs_free(struct zs_pool *pool, unsigned long obj)
1081 struct link_free *link;
1082 struct page *first_page, *f_page;
1083 unsigned long f_objidx, f_offset;
1087 struct size_class *class;
1088 enum fullness_group fullness;
1093 obj_handle_to_location(obj, &f_page, &f_objidx);
1094 first_page = get_first_page(f_page);
1096 get_zspage_mapping(first_page, &class_idx, &fullness);
1097 class = pool->size_class[class_idx];
1098 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
1100 spin_lock(&class->lock);
1102 /* Insert this object in containing zspage's freelist */
1103 vaddr = kmap_atomic(f_page);
1104 link = (struct link_free *)(vaddr + f_offset);
1105 link->next = first_page->freelist;
1106 kunmap_atomic(vaddr);
1107 first_page->freelist = (void *)obj;
1109 first_page->inuse--;
1110 fullness = fix_fullness_group(pool, first_page);
1111 spin_unlock(&class->lock);
1113 if (fullness == ZS_EMPTY) {
1114 atomic_long_sub(class->pages_per_zspage,
1115 &pool->pages_allocated);
1116 free_zspage(first_page);
1119 EXPORT_SYMBOL_GPL(zs_free);
1122 * zs_map_object - get address of allocated object from handle.
1123 * @pool: pool from which the object was allocated
1124 * @handle: handle returned from zs_malloc
1126 * Before using an object allocated from zs_malloc, it must be mapped using
1127 * this function. When done with the object, it must be unmapped using
1130 * Only one object can be mapped per cpu at a time. There is no protection
1131 * against nested mappings.
1133 * This function returns with preemption and page faults disabled.
1135 void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1139 unsigned long obj_idx, off;
1141 unsigned int class_idx;
1142 enum fullness_group fg;
1143 struct size_class *class;
1144 struct mapping_area *area;
1145 struct page *pages[2];
1150 * Because we use per-cpu mapping areas shared among the
1151 * pools/users, we can't allow mapping in interrupt context
1152 * because it can corrupt another users mappings.
1154 BUG_ON(in_interrupt());
1156 obj_handle_to_location(handle, &page, &obj_idx);
1157 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1158 class = pool->size_class[class_idx];
1159 off = obj_idx_to_offset(page, obj_idx, class->size);
1161 area = &get_cpu_var(zs_map_area);
1163 if (off + class->size <= PAGE_SIZE) {
1164 /* this object is contained entirely within a page */
1165 area->vm_addr = kmap_atomic(page);
1166 return area->vm_addr + off;
1169 /* this object spans two pages */
1171 pages[1] = get_next_page(page);
1174 return __zs_map_object(area, pages, off, class->size);
1176 EXPORT_SYMBOL_GPL(zs_map_object);
1178 void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1181 unsigned long obj_idx, off;
1183 unsigned int class_idx;
1184 enum fullness_group fg;
1185 struct size_class *class;
1186 struct mapping_area *area;
1190 obj_handle_to_location(handle, &page, &obj_idx);
1191 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1192 class = pool->size_class[class_idx];
1193 off = obj_idx_to_offset(page, obj_idx, class->size);
1195 area = &__get_cpu_var(zs_map_area);
1196 if (off + class->size <= PAGE_SIZE)
1197 kunmap_atomic(area->vm_addr);
1199 struct page *pages[2];
1202 pages[1] = get_next_page(page);
1205 __zs_unmap_object(area, pages, off, class->size);
1207 put_cpu_var(zs_map_area);
1209 EXPORT_SYMBOL_GPL(zs_unmap_object);
1211 unsigned long zs_get_total_pages(struct zs_pool *pool)
1213 return atomic_long_read(&pool->pages_allocated);
1215 EXPORT_SYMBOL_GPL(zs_get_total_pages);
1217 module_init(zs_init);
1218 module_exit(zs_exit);
1220 MODULE_LICENSE("Dual BSD/GPL");
1221 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");