2 * zsmalloc memory allocator
4 * Copyright (C) 2011 Nitin Gupta
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the license that better fits your requirements.
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
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
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
30 * Following is how we use various fields and flags of underlying
31 * struct page(s) to form a zspage.
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)
42 * For _first_ page only:
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
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
55 * Usage of struct page flags:
56 * PG_private: identifies the first component page
57 * PG_private2: identifies the last component page
61 #ifdef CONFIG_ZSMALLOC_DEBUG
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>
77 #include <linux/vmalloc.h>
78 #include <linux/hardirq.h>
79 #include <linux/spinlock.h>
80 #include <linux/types.h>
81 #include <linux/zsmalloc.h>
84 * This must be power of 2 and greater than of equal to sizeof(link_free).
85 * These two conditions ensure that any 'struct link_free' itself doesn't
86 * span more than 1 page which avoids complex case of mapping 2 pages simply
87 * to restore link_free pointer values.
92 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
93 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
95 #define ZS_MAX_ZSPAGE_ORDER 2
96 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
99 * Object location (<PFN>, <obj_idx>) is encoded as
100 * as single (void *) handle value.
102 * Note that object index <obj_idx> is relative to system
103 * page <PFN> it is stored in, so for each sub-page belonging
104 * to a zspage, obj_idx starts with 0.
106 * This is made more complicated by various memory models and PAE.
109 #ifndef MAX_PHYSMEM_BITS
110 #ifdef CONFIG_HIGHMEM64G
111 #define MAX_PHYSMEM_BITS 36
112 #else /* !CONFIG_HIGHMEM64G */
114 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
117 #define MAX_PHYSMEM_BITS BITS_PER_LONG
120 #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
121 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
122 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
124 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
125 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
126 #define ZS_MIN_ALLOC_SIZE \
127 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
128 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
131 * On systems with 4K page size, this gives 254 size classes! There is a
133 * - Large number of size classes is potentially wasteful as free page are
134 * spread across these classes
135 * - Small number of size classes causes large internal fragmentation
136 * - Probably its better to use specific size classes (empirically
137 * determined). NOTE: all those class sizes must be set as multiple of
138 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
140 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
143 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
144 #define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
145 ZS_SIZE_CLASS_DELTA + 1)
148 * We do not maintain any list for completely empty or full pages
150 enum fullness_group {
153 _ZS_NR_FULLNESS_GROUPS,
160 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
162 * n = number of allocated objects
163 * N = total number of objects zspage can store
164 * f = 1/fullness_threshold_frac
166 * Similarly, we assign zspage to:
167 * ZS_ALMOST_FULL when n > N / f
168 * ZS_EMPTY when n == 0
169 * ZS_FULL when n == N
171 * (see: fix_fullness_group())
173 static const int fullness_threshold_frac = 4;
177 * Size of objects stored in this class. Must be multiple
183 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
184 int pages_per_zspage;
191 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
195 * Placed within free objects to form a singly linked list.
196 * For every zspage, first_page->freelist gives head of this list.
198 * This must be power of 2 and less than or equal to ZS_ALIGN
201 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
206 struct size_class size_class[ZS_SIZE_CLASSES];
208 gfp_t flags; /* allocation flags used when growing pool */
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 */
242 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
243 static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
245 static int is_first_page(struct page *page)
247 return PagePrivate(page);
250 static int is_last_page(struct page *page)
252 return PagePrivate2(page);
255 static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
256 enum fullness_group *fullness)
259 BUG_ON(!is_first_page(page));
261 m = (unsigned long)page->mapping;
262 *fullness = m & FULLNESS_MASK;
263 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
266 static void set_zspage_mapping(struct page *page, unsigned int class_idx,
267 enum fullness_group fullness)
270 BUG_ON(!is_first_page(page));
272 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
273 (fullness & FULLNESS_MASK);
274 page->mapping = (struct address_space *)m;
277 static int get_size_class_index(int size)
281 if (likely(size > ZS_MIN_ALLOC_SIZE))
282 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
283 ZS_SIZE_CLASS_DELTA);
288 static enum fullness_group get_fullness_group(struct page *page)
290 int inuse, max_objects;
291 enum fullness_group fg;
292 BUG_ON(!is_first_page(page));
295 max_objects = page->objects;
299 else if (inuse == max_objects)
301 else if (inuse <= max_objects / fullness_threshold_frac)
302 fg = ZS_ALMOST_EMPTY;
309 static void insert_zspage(struct page *page, struct size_class *class,
310 enum fullness_group fullness)
314 BUG_ON(!is_first_page(page));
316 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
319 head = &class->fullness_list[fullness];
321 list_add_tail(&page->lru, &(*head)->lru);
326 static void remove_zspage(struct page *page, struct size_class *class,
327 enum fullness_group fullness)
331 BUG_ON(!is_first_page(page));
333 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
336 head = &class->fullness_list[fullness];
338 if (list_empty(&(*head)->lru))
340 else if (*head == page)
341 *head = (struct page *)list_entry((*head)->lru.next,
344 list_del_init(&page->lru);
347 static enum fullness_group fix_fullness_group(struct zs_pool *pool,
351 struct size_class *class;
352 enum fullness_group currfg, newfg;
354 BUG_ON(!is_first_page(page));
356 get_zspage_mapping(page, &class_idx, &currfg);
357 newfg = get_fullness_group(page);
361 class = &pool->size_class[class_idx];
362 remove_zspage(page, class, currfg);
363 insert_zspage(page, class, newfg);
364 set_zspage_mapping(page, class_idx, newfg);
371 * We have to decide on how many pages to link together
372 * to form a zspage for each size class. This is important
373 * to reduce wastage due to unusable space left at end of
374 * each zspage which is given as:
375 * wastage = Zp - Zp % size_class
376 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
378 * For example, for size class of 3/8 * PAGE_SIZE, we should
379 * link together 3 PAGE_SIZE sized pages to form a zspage
380 * since then we can perfectly fit in 8 such objects.
382 static int get_pages_per_zspage(int class_size)
384 int i, max_usedpc = 0;
385 /* zspage order which gives maximum used size per KB */
386 int max_usedpc_order = 1;
388 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
392 zspage_size = i * PAGE_SIZE;
393 waste = zspage_size % class_size;
394 usedpc = (zspage_size - waste) * 100 / zspage_size;
396 if (usedpc > max_usedpc) {
398 max_usedpc_order = i;
402 return max_usedpc_order;
406 * A single 'zspage' is composed of many system pages which are
407 * linked together using fields in struct page. This function finds
408 * the first/head page, given any component page of a zspage.
410 static struct page *get_first_page(struct page *page)
412 if (is_first_page(page))
415 return page->first_page;
418 static struct page *get_next_page(struct page *page)
422 if (is_last_page(page))
424 else if (is_first_page(page))
425 next = (struct page *)page->private;
427 next = list_entry(page->lru.next, struct page, lru);
433 * Encode <page, obj_idx> as a single handle value.
434 * On hardware platforms with physical memory starting at 0x0 the pfn
435 * could be 0 so we ensure that the handle will never be 0 by adjusting the
436 * encoded obj_idx value before encoding.
438 static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
440 unsigned long handle;
447 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
448 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
450 return (void *)handle;
454 * Decode <page, obj_idx> pair from the given object handle. We adjust the
455 * decoded obj_idx back to its original value since it was adjusted in
456 * obj_location_to_handle().
458 static void obj_handle_to_location(unsigned long handle, struct page **page,
459 unsigned long *obj_idx)
461 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
462 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
465 static unsigned long obj_idx_to_offset(struct page *page,
466 unsigned long obj_idx, int class_size)
468 unsigned long off = 0;
470 if (!is_first_page(page))
473 return off + obj_idx * class_size;
476 static void reset_page(struct page *page)
478 clear_bit(PG_private, &page->flags);
479 clear_bit(PG_private_2, &page->flags);
480 set_page_private(page, 0);
481 page->mapping = NULL;
482 page->freelist = NULL;
483 page_mapcount_reset(page);
486 static void free_zspage(struct page *first_page)
488 struct page *nextp, *tmp, *head_extra;
490 BUG_ON(!is_first_page(first_page));
491 BUG_ON(first_page->inuse);
493 head_extra = (struct page *)page_private(first_page);
495 reset_page(first_page);
496 __free_page(first_page);
498 /* zspage with only 1 system page */
502 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
503 list_del(&nextp->lru);
507 reset_page(head_extra);
508 __free_page(head_extra);
511 /* Initialize a newly allocated zspage */
512 static void init_zspage(struct page *first_page, struct size_class *class)
514 unsigned long off = 0;
515 struct page *page = first_page;
517 BUG_ON(!is_first_page(first_page));
519 struct page *next_page;
520 struct link_free *link;
521 unsigned int i, objs_on_page;
524 * page->index stores offset of first object starting
525 * in the page. For the first page, this is always 0,
526 * so we use first_page->index (aka ->freelist) to store
527 * head of corresponding zspage's freelist.
529 if (page != first_page)
532 link = (struct link_free *)kmap_atomic(page) +
534 objs_on_page = (PAGE_SIZE - off) / class->size;
536 for (i = 1; i <= objs_on_page; i++) {
538 if (off < PAGE_SIZE) {
539 link->next = obj_location_to_handle(page, i);
540 link += class->size / sizeof(*link);
545 * We now come to the last (full or partial) object on this
546 * page, which must point to the first object on the next
549 next_page = get_next_page(page);
550 link->next = obj_location_to_handle(next_page, 0);
553 off = (off + class->size) % PAGE_SIZE;
558 * Allocate a zspage for the given size class
560 static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
563 struct page *first_page = NULL, *uninitialized_var(prev_page);
566 * Allocate individual pages and link them together as:
567 * 1. first page->private = first sub-page
568 * 2. all sub-pages are linked together using page->lru
569 * 3. each sub-page is linked to the first page using page->first_page
571 * For each size class, First/Head pages are linked together using
572 * page->lru. Also, we set PG_private to identify the first page
573 * (i.e. no other sub-page has this flag set) and PG_private_2 to
574 * identify the last page.
577 for (i = 0; i < class->pages_per_zspage; i++) {
580 page = alloc_page(flags);
584 INIT_LIST_HEAD(&page->lru);
585 if (i == 0) { /* first page */
586 SetPagePrivate(page);
587 set_page_private(page, 0);
589 first_page->inuse = 0;
592 first_page->private = (unsigned long)page;
594 page->first_page = first_page;
596 list_add(&page->lru, &prev_page->lru);
597 if (i == class->pages_per_zspage - 1) /* last page */
598 SetPagePrivate2(page);
602 init_zspage(first_page, class);
604 first_page->freelist = obj_location_to_handle(first_page, 0);
605 /* Maximum number of objects we can store in this zspage */
606 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
608 error = 0; /* Success */
611 if (unlikely(error) && first_page) {
612 free_zspage(first_page);
619 static struct page *find_get_zspage(struct size_class *class)
624 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
625 page = class->fullness_list[i];
633 #ifdef USE_PGTABLE_MAPPING
634 static inline int __zs_cpu_up(struct mapping_area *area)
637 * Make sure we don't leak memory if a cpu UP notification
638 * and zs_init() race and both call zs_cpu_up() on the same cpu
642 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
648 static inline void __zs_cpu_down(struct mapping_area *area)
651 free_vm_area(area->vm);
655 static inline void *__zs_map_object(struct mapping_area *area,
656 struct page *pages[2], int off, int size)
658 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
659 area->vm_addr = area->vm->addr;
660 return area->vm_addr + off;
663 static inline void __zs_unmap_object(struct mapping_area *area,
664 struct page *pages[2], int off, int size)
666 unsigned long addr = (unsigned long)area->vm_addr;
668 unmap_kernel_range(addr, PAGE_SIZE * 2);
671 #else /* USE_PGTABLE_MAPPING */
673 static inline int __zs_cpu_up(struct mapping_area *area)
676 * Make sure we don't leak memory if a cpu UP notification
677 * and zs_init() race and both call zs_cpu_up() on the same cpu
681 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
687 static inline void __zs_cpu_down(struct mapping_area *area)
690 free_page((unsigned long)area->vm_buf);
694 static void *__zs_map_object(struct mapping_area *area,
695 struct page *pages[2], int off, int size)
699 char *buf = area->vm_buf;
701 /* disable page faults to match kmap_atomic() return conditions */
704 /* no read fastpath */
705 if (area->vm_mm == ZS_MM_WO)
708 sizes[0] = PAGE_SIZE - off;
709 sizes[1] = size - sizes[0];
711 /* copy object to per-cpu buffer */
712 addr = kmap_atomic(pages[0]);
713 memcpy(buf, addr + off, sizes[0]);
715 addr = kmap_atomic(pages[1]);
716 memcpy(buf + sizes[0], addr, sizes[1]);
722 static void __zs_unmap_object(struct mapping_area *area,
723 struct page *pages[2], int off, int size)
727 char *buf = area->vm_buf;
729 /* no write fastpath */
730 if (area->vm_mm == ZS_MM_RO)
733 sizes[0] = PAGE_SIZE - off;
734 sizes[1] = size - sizes[0];
736 /* copy per-cpu buffer to object */
737 addr = kmap_atomic(pages[0]);
738 memcpy(addr + off, buf, sizes[0]);
740 addr = kmap_atomic(pages[1]);
741 memcpy(addr, buf + sizes[0], sizes[1]);
745 /* enable page faults to match kunmap_atomic() return conditions */
749 #endif /* USE_PGTABLE_MAPPING */
751 static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
754 int ret, cpu = (long)pcpu;
755 struct mapping_area *area;
759 area = &per_cpu(zs_map_area, cpu);
760 ret = __zs_cpu_up(area);
762 return notifier_from_errno(ret);
765 case CPU_UP_CANCELED:
766 area = &per_cpu(zs_map_area, cpu);
774 static struct notifier_block zs_cpu_nb = {
775 .notifier_call = zs_cpu_notifier
778 static void zs_exit(void)
782 for_each_online_cpu(cpu)
783 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
784 unregister_cpu_notifier(&zs_cpu_nb);
787 static int zs_init(void)
791 register_cpu_notifier(&zs_cpu_nb);
792 for_each_online_cpu(cpu) {
793 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
794 if (notifier_to_errno(ret))
800 return notifier_to_errno(ret);
804 * zs_create_pool - Creates an allocation pool to work from.
805 * @flags: allocation flags used to allocate pool metadata
807 * This function must be called before anything when using
808 * the zsmalloc allocator.
810 * On success, a pointer to the newly created pool is returned,
813 struct zs_pool *zs_create_pool(gfp_t flags)
816 struct zs_pool *pool;
818 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
819 pool = kzalloc(ovhd_size, GFP_KERNEL);
823 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
825 struct size_class *class;
827 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
828 if (size > ZS_MAX_ALLOC_SIZE)
829 size = ZS_MAX_ALLOC_SIZE;
831 class = &pool->size_class[i];
834 spin_lock_init(&class->lock);
835 class->pages_per_zspage = get_pages_per_zspage(size);
843 EXPORT_SYMBOL_GPL(zs_create_pool);
845 void zs_destroy_pool(struct zs_pool *pool)
849 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
851 struct size_class *class = &pool->size_class[i];
853 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
854 if (class->fullness_list[fg]) {
855 pr_info("Freeing non-empty class with size "
856 "%db, fullness group %d\n",
863 EXPORT_SYMBOL_GPL(zs_destroy_pool);
866 * zs_malloc - Allocate block of given size from pool.
867 * @pool: pool to allocate from
868 * @size: size of block to allocate
870 * On success, handle to the allocated object is returned,
872 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
874 unsigned long zs_malloc(struct zs_pool *pool, size_t size)
877 struct link_free *link;
879 struct size_class *class;
881 struct page *first_page, *m_page;
882 unsigned long m_objidx, m_offset;
884 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
887 class_idx = get_size_class_index(size);
888 class = &pool->size_class[class_idx];
889 BUG_ON(class_idx != class->index);
891 spin_lock(&class->lock);
892 first_page = find_get_zspage(class);
895 spin_unlock(&class->lock);
896 first_page = alloc_zspage(class, pool->flags);
897 if (unlikely(!first_page))
900 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
901 spin_lock(&class->lock);
902 class->pages_allocated += class->pages_per_zspage;
905 obj = (unsigned long)first_page->freelist;
906 obj_handle_to_location(obj, &m_page, &m_objidx);
907 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
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));
916 /* Now move the zspage to another fullness group, if required */
917 fix_fullness_group(pool, first_page);
918 spin_unlock(&class->lock);
922 EXPORT_SYMBOL_GPL(zs_malloc);
924 void zs_free(struct zs_pool *pool, unsigned long obj)
926 struct link_free *link;
927 struct page *first_page, *f_page;
928 unsigned long f_objidx, f_offset;
931 struct size_class *class;
932 enum fullness_group fullness;
937 obj_handle_to_location(obj, &f_page, &f_objidx);
938 first_page = get_first_page(f_page);
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);
944 spin_lock(&class->lock);
946 /* Insert this object in containing zspage's freelist */
947 link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
949 link->next = first_page->freelist;
951 first_page->freelist = (void *)obj;
954 fullness = fix_fullness_group(pool, first_page);
956 if (fullness == ZS_EMPTY)
957 class->pages_allocated -= class->pages_per_zspage;
959 spin_unlock(&class->lock);
961 if (fullness == ZS_EMPTY)
962 free_zspage(first_page);
964 EXPORT_SYMBOL_GPL(zs_free);
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
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
975 * Only one object can be mapped per cpu at a time. There is no protection
976 * against nested mappings.
978 * This function returns with preemption and page faults disabled.
980 void *zs_map_object(struct zs_pool *pool, unsigned long handle,
984 unsigned long obj_idx, off;
986 unsigned int class_idx;
987 enum fullness_group fg;
988 struct size_class *class;
989 struct mapping_area *area;
990 struct page *pages[2];
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.
999 BUG_ON(in_interrupt());
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);
1006 area = &get_cpu_var(zs_map_area);
1008 if (off + class->size <= PAGE_SIZE) {
1009 /* this object is contained entirely within a page */
1010 area->vm_addr = kmap_atomic(page);
1011 return area->vm_addr + off;
1014 /* this object spans two pages */
1016 pages[1] = get_next_page(page);
1019 return __zs_map_object(area, pages, off, class->size);
1021 EXPORT_SYMBOL_GPL(zs_map_object);
1023 void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1026 unsigned long obj_idx, off;
1028 unsigned int class_idx;
1029 enum fullness_group fg;
1030 struct size_class *class;
1031 struct mapping_area *area;
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);
1040 area = &__get_cpu_var(zs_map_area);
1041 if (off + class->size <= PAGE_SIZE)
1042 kunmap_atomic(area->vm_addr);
1044 struct page *pages[2];
1047 pages[1] = get_next_page(page);
1050 __zs_unmap_object(area, pages, off, class->size);
1052 put_cpu_var(zs_map_area);
1054 EXPORT_SYMBOL_GPL(zs_unmap_object);
1056 u64 zs_get_total_size_bytes(struct zs_pool *pool)
1061 for (i = 0; i < ZS_SIZE_CLASSES; i++)
1062 npages += pool->size_class[i].pages_allocated;
1064 return npages << PAGE_SHIFT;
1066 EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
1068 module_init(zs_init);
1069 module_exit(zs_exit);
1071 MODULE_LICENSE("Dual BSD/GPL");
1072 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");