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
15 * This allocator is designed for use with zram. Thus, the allocator is
16 * supposed to work well under low memory conditions. In particular, it
17 * never attempts higher order page allocation which is very likely to
18 * fail under memory pressure. On the other hand, if we just use single
19 * (0-order) pages, it would suffer from very high fragmentation --
20 * any object of size PAGE_SIZE/2 or larger would occupy an entire page.
21 * This was one of the major issues with its predecessor (xvmalloc).
23 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
24 * and links them together using various 'struct page' fields. These linked
25 * pages act as a single higher-order page i.e. an object can span 0-order
26 * page boundaries. The code refers to these linked pages as a single entity
29 * For simplicity, zsmalloc can only allocate objects of size up to PAGE_SIZE
30 * since this satisfies the requirements of all its current users (in the
31 * worst case, page is incompressible and is thus stored "as-is" i.e. in
32 * uncompressed form). For allocation requests larger than this size, failure
33 * is returned (see zs_malloc).
35 * Additionally, zs_malloc() does not return a dereferenceable pointer.
36 * Instead, it returns an opaque handle (unsigned long) which encodes actual
37 * location of the allocated object. The reason for this indirection is that
38 * zsmalloc does not keep zspages permanently mapped since that would cause
39 * issues on 32-bit systems where the VA region for kernel space mappings
40 * is very small. So, before using the allocating memory, the object has to
41 * be mapped using zs_map_object() to get a usable pointer and subsequently
42 * unmapped using zs_unmap_object().
44 * Following is how we use various fields and flags of underlying
45 * struct page(s) to form a zspage.
47 * Usage of struct page fields:
48 * page->first_page: points to the first component (0-order) page
49 * page->index (union with page->freelist): offset of the first object
50 * starting in this page. For the first page, this is
51 * always 0, so we use this field (aka freelist) to point
52 * to the first free object in zspage.
53 * page->lru: links together all component pages (except the first page)
56 * For _first_ page only:
58 * page->private (union with page->first_page): refers to the
59 * component page after the first page
60 * page->freelist: points to the first free object in zspage.
61 * Free objects are linked together using in-place
63 * page->objects: maximum number of objects we can store in this
64 * zspage (class->zspage_order * PAGE_SIZE / class->size)
65 * page->lru: links together first pages of various zspages.
66 * Basically forming list of zspages in a fullness group.
67 * page->mapping: class index and fullness group of the zspage
69 * Usage of struct page flags:
70 * PG_private: identifies the first component page
71 * PG_private2: identifies the last component page
75 #ifdef CONFIG_ZSMALLOC_DEBUG
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/bitops.h>
82 #include <linux/errno.h>
83 #include <linux/highmem.h>
84 #include <linux/string.h>
85 #include <linux/slab.h>
86 #include <asm/tlbflush.h>
87 #include <asm/pgtable.h>
88 #include <linux/cpumask.h>
89 #include <linux/cpu.h>
90 #include <linux/vmalloc.h>
91 #include <linux/hardirq.h>
92 #include <linux/spinlock.h>
93 #include <linux/types.h>
94 #include <linux/zsmalloc.h>
97 * This must be power of 2 and greater than of equal to sizeof(link_free).
98 * These two conditions ensure that any 'struct link_free' itself doesn't
99 * span more than 1 page which avoids complex case of mapping 2 pages simply
100 * to restore link_free pointer values.
105 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
106 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
108 #define ZS_MAX_ZSPAGE_ORDER 2
109 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
112 * Object location (<PFN>, <obj_idx>) is encoded as
113 * as single (unsigned long) handle value.
115 * Note that object index <obj_idx> is relative to system
116 * page <PFN> it is stored in, so for each sub-page belonging
117 * to a zspage, obj_idx starts with 0.
119 * This is made more complicated by various memory models and PAE.
122 #ifndef MAX_PHYSMEM_BITS
123 #ifdef CONFIG_HIGHMEM64G
124 #define MAX_PHYSMEM_BITS 36
125 #else /* !CONFIG_HIGHMEM64G */
127 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
130 #define MAX_PHYSMEM_BITS BITS_PER_LONG
133 #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
134 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
135 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
137 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
138 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
139 #define ZS_MIN_ALLOC_SIZE \
140 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
141 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
144 * On systems with 4K page size, this gives 254 size classes! There is a
146 * - Large number of size classes is potentially wasteful as free page are
147 * spread across these classes
148 * - Small number of size classes causes large internal fragmentation
149 * - Probably its better to use specific size classes (empirically
150 * determined). NOTE: all those class sizes must be set as multiple of
151 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
153 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
156 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
157 #define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
158 ZS_SIZE_CLASS_DELTA + 1)
161 * We do not maintain any list for completely empty or full pages
163 enum fullness_group
{
166 _ZS_NR_FULLNESS_GROUPS
,
173 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
175 * n = number of allocated objects
176 * N = total number of objects zspage can store
177 * f = 1/fullness_threshold_frac
179 * Similarly, we assign zspage to:
180 * ZS_ALMOST_FULL when n > N / f
181 * ZS_EMPTY when n == 0
182 * ZS_FULL when n == N
184 * (see: fix_fullness_group())
186 static const int fullness_threshold_frac
= 4;
190 * Size of objects stored in this class. Must be multiple
196 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
197 int pages_per_zspage
;
204 struct page
*fullness_list
[_ZS_NR_FULLNESS_GROUPS
];
208 * Placed within free objects to form a singly linked list.
209 * For every zspage, first_page->freelist gives head of this list.
211 * This must be power of 2 and less than or equal to ZS_ALIGN
214 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
219 struct size_class size_class
[ZS_SIZE_CLASSES
];
221 gfp_t flags
; /* allocation flags used when growing pool */
225 * A zspage's class index and fullness group
226 * are encoded in its (first)page->mapping
228 #define CLASS_IDX_BITS 28
229 #define FULLNESS_BITS 4
230 #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
231 #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
233 struct mapping_area
{
234 #ifdef CONFIG_PGTABLE_MAPPING
235 struct vm_struct
*vm
; /* vm area for mapping object that span pages */
237 char *vm_buf
; /* copy buffer for objects that span pages */
239 char *vm_addr
; /* address of kmap_atomic()'ed pages */
240 enum zs_mapmode vm_mm
; /* mapping mode */
244 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
245 static DEFINE_PER_CPU(struct mapping_area
, zs_map_area
);
247 static int is_first_page(struct page
*page
)
249 return PagePrivate(page
);
252 static int is_last_page(struct page
*page
)
254 return PagePrivate2(page
);
257 static void get_zspage_mapping(struct page
*page
, unsigned int *class_idx
,
258 enum fullness_group
*fullness
)
261 BUG_ON(!is_first_page(page
));
263 m
= (unsigned long)page
->mapping
;
264 *fullness
= m
& FULLNESS_MASK
;
265 *class_idx
= (m
>> FULLNESS_BITS
) & CLASS_IDX_MASK
;
268 static void set_zspage_mapping(struct page
*page
, unsigned int class_idx
,
269 enum fullness_group fullness
)
272 BUG_ON(!is_first_page(page
));
274 m
= ((class_idx
& CLASS_IDX_MASK
) << FULLNESS_BITS
) |
275 (fullness
& FULLNESS_MASK
);
276 page
->mapping
= (struct address_space
*)m
;
280 * zsmalloc divides the pool into various size classes where each
281 * class maintains a list of zspages where each zspage is divided
282 * into equal sized chunks. Each allocation falls into one of these
283 * classes depending on its size. This function returns index of the
284 * size class which has chunk size big enough to hold the give size.
286 static int get_size_class_index(int size
)
290 if (likely(size
> ZS_MIN_ALLOC_SIZE
))
291 idx
= DIV_ROUND_UP(size
- ZS_MIN_ALLOC_SIZE
,
292 ZS_SIZE_CLASS_DELTA
);
298 * For each size class, zspages are divided into different groups
299 * depending on how "full" they are. This was done so that we could
300 * easily find empty or nearly empty zspages when we try to shrink
301 * the pool (not yet implemented). This function returns fullness
302 * status of the given page.
304 static enum fullness_group
get_fullness_group(struct page
*page
)
306 int inuse
, max_objects
;
307 enum fullness_group fg
;
308 BUG_ON(!is_first_page(page
));
311 max_objects
= page
->objects
;
315 else if (inuse
== max_objects
)
317 else if (inuse
<= max_objects
/ fullness_threshold_frac
)
318 fg
= ZS_ALMOST_EMPTY
;
326 * Each size class maintains various freelists and zspages are assigned
327 * to one of these freelists based on the number of live objects they
328 * have. This functions inserts the given zspage into the freelist
329 * identified by <class, fullness_group>.
331 static void insert_zspage(struct page
*page
, struct size_class
*class,
332 enum fullness_group fullness
)
336 BUG_ON(!is_first_page(page
));
338 if (fullness
>= _ZS_NR_FULLNESS_GROUPS
)
341 head
= &class->fullness_list
[fullness
];
343 list_add_tail(&page
->lru
, &(*head
)->lru
);
349 * This function removes the given zspage from the freelist identified
350 * by <class, fullness_group>.
352 static void remove_zspage(struct page
*page
, struct size_class
*class,
353 enum fullness_group fullness
)
357 BUG_ON(!is_first_page(page
));
359 if (fullness
>= _ZS_NR_FULLNESS_GROUPS
)
362 head
= &class->fullness_list
[fullness
];
364 if (list_empty(&(*head
)->lru
))
366 else if (*head
== page
)
367 *head
= (struct page
*)list_entry((*head
)->lru
.next
,
370 list_del_init(&page
->lru
);
374 * Each size class maintains zspages in different fullness groups depending
375 * on the number of live objects they contain. When allocating or freeing
376 * objects, the fullness status of the page can change, say, from ALMOST_FULL
377 * to ALMOST_EMPTY when freeing an object. This function checks if such
378 * a status change has occurred for the given page and accordingly moves the
379 * page from the freelist of the old fullness group to that of the new
382 static enum fullness_group
fix_fullness_group(struct zs_pool
*pool
,
386 struct size_class
*class;
387 enum fullness_group currfg
, newfg
;
389 BUG_ON(!is_first_page(page
));
391 get_zspage_mapping(page
, &class_idx
, &currfg
);
392 newfg
= get_fullness_group(page
);
396 class = &pool
->size_class
[class_idx
];
397 remove_zspage(page
, class, currfg
);
398 insert_zspage(page
, class, newfg
);
399 set_zspage_mapping(page
, class_idx
, newfg
);
406 * We have to decide on how many pages to link together
407 * to form a zspage for each size class. This is important
408 * to reduce wastage due to unusable space left at end of
409 * each zspage which is given as:
410 * wastage = Zp - Zp % size_class
411 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
413 * For example, for size class of 3/8 * PAGE_SIZE, we should
414 * link together 3 PAGE_SIZE sized pages to form a zspage
415 * since then we can perfectly fit in 8 such objects.
417 static int get_pages_per_zspage(int class_size
)
419 int i
, max_usedpc
= 0;
420 /* zspage order which gives maximum used size per KB */
421 int max_usedpc_order
= 1;
423 for (i
= 1; i
<= ZS_MAX_PAGES_PER_ZSPAGE
; i
++) {
427 zspage_size
= i
* PAGE_SIZE
;
428 waste
= zspage_size
% class_size
;
429 usedpc
= (zspage_size
- waste
) * 100 / zspage_size
;
431 if (usedpc
> max_usedpc
) {
433 max_usedpc_order
= i
;
437 return max_usedpc_order
;
441 * A single 'zspage' is composed of many system pages which are
442 * linked together using fields in struct page. This function finds
443 * the first/head page, given any component page of a zspage.
445 static struct page
*get_first_page(struct page
*page
)
447 if (is_first_page(page
))
450 return page
->first_page
;
453 static struct page
*get_next_page(struct page
*page
)
457 if (is_last_page(page
))
459 else if (is_first_page(page
))
460 next
= (struct page
*)page_private(page
);
462 next
= list_entry(page
->lru
.next
, struct page
, lru
);
468 * Encode <page, obj_idx> as a single handle value.
469 * On hardware platforms with physical memory starting at 0x0 the pfn
470 * could be 0 so we ensure that the handle will never be 0 by adjusting the
471 * encoded obj_idx value before encoding.
473 static void *obj_location_to_handle(struct page
*page
, unsigned long obj_idx
)
475 unsigned long handle
;
482 handle
= page_to_pfn(page
) << OBJ_INDEX_BITS
;
483 handle
|= ((obj_idx
+ 1) & OBJ_INDEX_MASK
);
485 return (void *)handle
;
489 * Decode <page, obj_idx> pair from the given object handle. We adjust the
490 * decoded obj_idx back to its original value since it was adjusted in
491 * obj_location_to_handle().
493 static void obj_handle_to_location(unsigned long handle
, struct page
**page
,
494 unsigned long *obj_idx
)
496 *page
= pfn_to_page(handle
>> OBJ_INDEX_BITS
);
497 *obj_idx
= (handle
& OBJ_INDEX_MASK
) - 1;
500 static unsigned long obj_idx_to_offset(struct page
*page
,
501 unsigned long obj_idx
, int class_size
)
503 unsigned long off
= 0;
505 if (!is_first_page(page
))
508 return off
+ obj_idx
* class_size
;
511 static void reset_page(struct page
*page
)
513 clear_bit(PG_private
, &page
->flags
);
514 clear_bit(PG_private_2
, &page
->flags
);
515 set_page_private(page
, 0);
516 page
->mapping
= NULL
;
517 page
->freelist
= NULL
;
518 page_mapcount_reset(page
);
521 static void free_zspage(struct page
*first_page
)
523 struct page
*nextp
, *tmp
, *head_extra
;
525 BUG_ON(!is_first_page(first_page
));
526 BUG_ON(first_page
->inuse
);
528 head_extra
= (struct page
*)page_private(first_page
);
530 reset_page(first_page
);
531 __free_page(first_page
);
533 /* zspage with only 1 system page */
537 list_for_each_entry_safe(nextp
, tmp
, &head_extra
->lru
, lru
) {
538 list_del(&nextp
->lru
);
542 reset_page(head_extra
);
543 __free_page(head_extra
);
546 /* Initialize a newly allocated zspage */
547 static void init_zspage(struct page
*first_page
, struct size_class
*class)
549 unsigned long off
= 0;
550 struct page
*page
= first_page
;
552 BUG_ON(!is_first_page(first_page
));
554 struct page
*next_page
;
555 struct link_free
*link
;
556 unsigned int i
, objs_on_page
;
559 * page->index stores offset of first object starting
560 * in the page. For the first page, this is always 0,
561 * so we use first_page->index (aka ->freelist) to store
562 * head of corresponding zspage's freelist.
564 if (page
!= first_page
)
567 link
= (struct link_free
*)kmap_atomic(page
) +
569 objs_on_page
= (PAGE_SIZE
- off
) / class->size
;
571 for (i
= 1; i
<= objs_on_page
; i
++) {
573 if (off
< PAGE_SIZE
) {
574 link
->next
= obj_location_to_handle(page
, i
);
575 link
+= class->size
/ sizeof(*link
);
580 * We now come to the last (full or partial) object on this
581 * page, which must point to the first object on the next
584 next_page
= get_next_page(page
);
585 link
->next
= obj_location_to_handle(next_page
, 0);
588 off
= (off
+ class->size
) % PAGE_SIZE
;
593 * Allocate a zspage for the given size class
595 static struct page
*alloc_zspage(struct size_class
*class, gfp_t flags
)
598 struct page
*first_page
= NULL
, *uninitialized_var(prev_page
);
601 * Allocate individual pages and link them together as:
602 * 1. first page->private = first sub-page
603 * 2. all sub-pages are linked together using page->lru
604 * 3. each sub-page is linked to the first page using page->first_page
606 * For each size class, First/Head pages are linked together using
607 * page->lru. Also, we set PG_private to identify the first page
608 * (i.e. no other sub-page has this flag set) and PG_private_2 to
609 * identify the last page.
612 for (i
= 0; i
< class->pages_per_zspage
; i
++) {
615 page
= alloc_page(flags
);
619 INIT_LIST_HEAD(&page
->lru
);
620 if (i
== 0) { /* first page */
621 SetPagePrivate(page
);
622 set_page_private(page
, 0);
624 first_page
->inuse
= 0;
627 set_page_private(first_page
, (unsigned long)page
);
629 page
->first_page
= first_page
;
631 list_add(&page
->lru
, &prev_page
->lru
);
632 if (i
== class->pages_per_zspage
- 1) /* last page */
633 SetPagePrivate2(page
);
637 init_zspage(first_page
, class);
639 first_page
->freelist
= obj_location_to_handle(first_page
, 0);
640 /* Maximum number of objects we can store in this zspage */
641 first_page
->objects
= class->pages_per_zspage
* PAGE_SIZE
/ class->size
;
643 error
= 0; /* Success */
646 if (unlikely(error
) && first_page
) {
647 free_zspage(first_page
);
654 static struct page
*find_get_zspage(struct size_class
*class)
659 for (i
= 0; i
< _ZS_NR_FULLNESS_GROUPS
; i
++) {
660 page
= class->fullness_list
[i
];
668 #ifdef CONFIG_PGTABLE_MAPPING
669 static inline int __zs_cpu_up(struct mapping_area
*area
)
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
677 area
->vm
= alloc_vm_area(PAGE_SIZE
* 2, NULL
);
683 static inline void __zs_cpu_down(struct mapping_area
*area
)
686 free_vm_area(area
->vm
);
690 static inline void *__zs_map_object(struct mapping_area
*area
,
691 struct page
*pages
[2], int off
, int size
)
693 BUG_ON(map_vm_area(area
->vm
, PAGE_KERNEL
, &pages
));
694 area
->vm_addr
= area
->vm
->addr
;
695 return area
->vm_addr
+ off
;
698 static inline void __zs_unmap_object(struct mapping_area
*area
,
699 struct page
*pages
[2], int off
, int size
)
701 unsigned long addr
= (unsigned long)area
->vm_addr
;
703 unmap_kernel_range(addr
, PAGE_SIZE
* 2);
706 #else /* CONFIG_PGTABLE_MAPPING */
708 static inline int __zs_cpu_up(struct mapping_area
*area
)
711 * Make sure we don't leak memory if a cpu UP notification
712 * and zs_init() race and both call zs_cpu_up() on the same cpu
716 area
->vm_buf
= (char *)__get_free_page(GFP_KERNEL
);
722 static inline void __zs_cpu_down(struct mapping_area
*area
)
725 free_page((unsigned long)area
->vm_buf
);
729 static void *__zs_map_object(struct mapping_area
*area
,
730 struct page
*pages
[2], int off
, int size
)
734 char *buf
= area
->vm_buf
;
736 /* disable page faults to match kmap_atomic() return conditions */
739 /* no read fastpath */
740 if (area
->vm_mm
== ZS_MM_WO
)
743 sizes
[0] = PAGE_SIZE
- off
;
744 sizes
[1] = size
- sizes
[0];
746 /* copy object to per-cpu buffer */
747 addr
= kmap_atomic(pages
[0]);
748 memcpy(buf
, addr
+ off
, sizes
[0]);
750 addr
= kmap_atomic(pages
[1]);
751 memcpy(buf
+ sizes
[0], addr
, sizes
[1]);
757 static void __zs_unmap_object(struct mapping_area
*area
,
758 struct page
*pages
[2], int off
, int size
)
762 char *buf
= area
->vm_buf
;
764 /* no write fastpath */
765 if (area
->vm_mm
== ZS_MM_RO
)
768 sizes
[0] = PAGE_SIZE
- off
;
769 sizes
[1] = size
- sizes
[0];
771 /* copy per-cpu buffer to object */
772 addr
= kmap_atomic(pages
[0]);
773 memcpy(addr
+ off
, buf
, sizes
[0]);
775 addr
= kmap_atomic(pages
[1]);
776 memcpy(addr
, buf
+ sizes
[0], sizes
[1]);
780 /* enable page faults to match kunmap_atomic() return conditions */
784 #endif /* CONFIG_PGTABLE_MAPPING */
786 static int zs_cpu_notifier(struct notifier_block
*nb
, unsigned long action
,
789 int ret
, cpu
= (long)pcpu
;
790 struct mapping_area
*area
;
794 area
= &per_cpu(zs_map_area
, cpu
);
795 ret
= __zs_cpu_up(area
);
797 return notifier_from_errno(ret
);
800 case CPU_UP_CANCELED
:
801 area
= &per_cpu(zs_map_area
, cpu
);
809 static struct notifier_block zs_cpu_nb
= {
810 .notifier_call
= zs_cpu_notifier
813 static void zs_exit(void)
817 for_each_online_cpu(cpu
)
818 zs_cpu_notifier(NULL
, CPU_DEAD
, (void *)(long)cpu
);
819 unregister_cpu_notifier(&zs_cpu_nb
);
822 static int zs_init(void)
826 register_cpu_notifier(&zs_cpu_nb
);
827 for_each_online_cpu(cpu
) {
828 ret
= zs_cpu_notifier(NULL
, CPU_UP_PREPARE
, (void *)(long)cpu
);
829 if (notifier_to_errno(ret
))
835 return notifier_to_errno(ret
);
839 * zs_create_pool - Creates an allocation pool to work from.
840 * @flags: allocation flags used to allocate pool metadata
842 * This function must be called before anything when using
843 * the zsmalloc allocator.
845 * On success, a pointer to the newly created pool is returned,
848 struct zs_pool
*zs_create_pool(gfp_t flags
)
851 struct zs_pool
*pool
;
853 ovhd_size
= roundup(sizeof(*pool
), PAGE_SIZE
);
854 pool
= kzalloc(ovhd_size
, GFP_KERNEL
);
858 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++) {
860 struct size_class
*class;
862 size
= ZS_MIN_ALLOC_SIZE
+ i
* ZS_SIZE_CLASS_DELTA
;
863 if (size
> ZS_MAX_ALLOC_SIZE
)
864 size
= ZS_MAX_ALLOC_SIZE
;
866 class = &pool
->size_class
[i
];
869 spin_lock_init(&class->lock
);
870 class->pages_per_zspage
= get_pages_per_zspage(size
);
878 EXPORT_SYMBOL_GPL(zs_create_pool
);
880 void zs_destroy_pool(struct zs_pool
*pool
)
884 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++) {
886 struct size_class
*class = &pool
->size_class
[i
];
888 for (fg
= 0; fg
< _ZS_NR_FULLNESS_GROUPS
; fg
++) {
889 if (class->fullness_list
[fg
]) {
890 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
897 EXPORT_SYMBOL_GPL(zs_destroy_pool
);
900 * zs_malloc - Allocate block of given size from pool.
901 * @pool: pool to allocate from
902 * @size: size of block to allocate
904 * On success, handle to the allocated object is returned,
906 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
908 unsigned long zs_malloc(struct zs_pool
*pool
, size_t size
)
911 struct link_free
*link
;
913 struct size_class
*class;
915 struct page
*first_page
, *m_page
;
916 unsigned long m_objidx
, m_offset
;
918 if (unlikely(!size
|| size
> ZS_MAX_ALLOC_SIZE
))
921 class_idx
= get_size_class_index(size
);
922 class = &pool
->size_class
[class_idx
];
923 BUG_ON(class_idx
!= class->index
);
925 spin_lock(&class->lock
);
926 first_page
= find_get_zspage(class);
929 spin_unlock(&class->lock
);
930 first_page
= alloc_zspage(class, pool
->flags
);
931 if (unlikely(!first_page
))
934 set_zspage_mapping(first_page
, class->index
, ZS_EMPTY
);
935 spin_lock(&class->lock
);
936 class->pages_allocated
+= class->pages_per_zspage
;
939 obj
= (unsigned long)first_page
->freelist
;
940 obj_handle_to_location(obj
, &m_page
, &m_objidx
);
941 m_offset
= obj_idx_to_offset(m_page
, m_objidx
, class->size
);
943 link
= (struct link_free
*)kmap_atomic(m_page
) +
944 m_offset
/ sizeof(*link
);
945 first_page
->freelist
= link
->next
;
946 memset(link
, POISON_INUSE
, sizeof(*link
));
950 /* Now move the zspage to another fullness group, if required */
951 fix_fullness_group(pool
, first_page
);
952 spin_unlock(&class->lock
);
956 EXPORT_SYMBOL_GPL(zs_malloc
);
958 void zs_free(struct zs_pool
*pool
, unsigned long obj
)
960 struct link_free
*link
;
961 struct page
*first_page
, *f_page
;
962 unsigned long f_objidx
, f_offset
;
965 struct size_class
*class;
966 enum fullness_group fullness
;
971 obj_handle_to_location(obj
, &f_page
, &f_objidx
);
972 first_page
= get_first_page(f_page
);
974 get_zspage_mapping(first_page
, &class_idx
, &fullness
);
975 class = &pool
->size_class
[class_idx
];
976 f_offset
= obj_idx_to_offset(f_page
, f_objidx
, class->size
);
978 spin_lock(&class->lock
);
980 /* Insert this object in containing zspage's freelist */
981 link
= (struct link_free
*)((unsigned char *)kmap_atomic(f_page
)
983 link
->next
= first_page
->freelist
;
985 first_page
->freelist
= (void *)obj
;
988 fullness
= fix_fullness_group(pool
, first_page
);
990 if (fullness
== ZS_EMPTY
)
991 class->pages_allocated
-= class->pages_per_zspage
;
993 spin_unlock(&class->lock
);
995 if (fullness
== ZS_EMPTY
)
996 free_zspage(first_page
);
998 EXPORT_SYMBOL_GPL(zs_free
);
1001 * zs_map_object - get address of allocated object from handle.
1002 * @pool: pool from which the object was allocated
1003 * @handle: handle returned from zs_malloc
1005 * Before using an object allocated from zs_malloc, it must be mapped using
1006 * this function. When done with the object, it must be unmapped using
1009 * Only one object can be mapped per cpu at a time. There is no protection
1010 * against nested mappings.
1012 * This function returns with preemption and page faults disabled.
1014 void *zs_map_object(struct zs_pool
*pool
, unsigned long handle
,
1018 unsigned long obj_idx
, off
;
1020 unsigned int class_idx
;
1021 enum fullness_group fg
;
1022 struct size_class
*class;
1023 struct mapping_area
*area
;
1024 struct page
*pages
[2];
1029 * Because we use per-cpu mapping areas shared among the
1030 * pools/users, we can't allow mapping in interrupt context
1031 * because it can corrupt another users mappings.
1033 BUG_ON(in_interrupt());
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
);
1042 if (off
+ class->size
<= PAGE_SIZE
) {
1043 /* this object is contained entirely within a page */
1044 area
->vm_addr
= kmap_atomic(page
);
1045 return area
->vm_addr
+ off
;
1048 /* this object spans two pages */
1050 pages
[1] = get_next_page(page
);
1053 return __zs_map_object(area
, pages
, off
, class->size
);
1055 EXPORT_SYMBOL_GPL(zs_map_object
);
1057 void zs_unmap_object(struct zs_pool
*pool
, unsigned long handle
)
1060 unsigned long obj_idx
, off
;
1062 unsigned int class_idx
;
1063 enum fullness_group fg
;
1064 struct size_class
*class;
1065 struct mapping_area
*area
;
1069 obj_handle_to_location(handle
, &page
, &obj_idx
);
1070 get_zspage_mapping(get_first_page(page
), &class_idx
, &fg
);
1071 class = &pool
->size_class
[class_idx
];
1072 off
= obj_idx_to_offset(page
, obj_idx
, class->size
);
1074 area
= &__get_cpu_var(zs_map_area
);
1075 if (off
+ class->size
<= PAGE_SIZE
)
1076 kunmap_atomic(area
->vm_addr
);
1078 struct page
*pages
[2];
1081 pages
[1] = get_next_page(page
);
1084 __zs_unmap_object(area
, pages
, off
, class->size
);
1086 put_cpu_var(zs_map_area
);
1088 EXPORT_SYMBOL_GPL(zs_unmap_object
);
1090 u64
zs_get_total_size_bytes(struct zs_pool
*pool
)
1095 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++)
1096 npages
+= pool
->size_class
[i
].pages_allocated
;
1098 return npages
<< PAGE_SHIFT
;
1100 EXPORT_SYMBOL_GPL(zs_get_total_size_bytes
);
1102 module_init(zs_init
);
1103 module_exit(zs_exit
);
1105 MODULE_LICENSE("Dual BSD/GPL");
1106 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");