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 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
18 * Usage of struct page fields:
19 * page->private: points to zspage
20 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
23 * page->units: first object offset in a subpage of zspage
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_owner_priv_1: identifies the huge component page
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/magic.h>
37 #include <linux/bitops.h>
38 #include <linux/errno.h>
39 #include <linux/highmem.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <asm/tlbflush.h>
43 #include <asm/pgtable.h>
44 #include <linux/cpumask.h>
45 #include <linux/cpu.h>
46 #include <linux/vmalloc.h>
47 #include <linux/preempt.h>
48 #include <linux/spinlock.h>
49 #include <linux/types.h>
50 #include <linux/debugfs.h>
51 #include <linux/zsmalloc.h>
52 #include <linux/zpool.h>
53 #include <linux/mount.h>
54 #include <linux/migrate.h>
55 #include <linux/wait.h>
56 #include <linux/pagemap.h>
59 #define ZSPAGE_MAGIC 0x58
62 * This must be power of 2 and greater than of equal to sizeof(link_free).
63 * These two conditions ensure that any 'struct link_free' itself doesn't
64 * span more than 1 page which avoids complex case of mapping 2 pages simply
65 * to restore link_free pointer values.
70 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
71 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
73 #define ZS_MAX_ZSPAGE_ORDER 2
74 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
76 #define ZS_HANDLE_SIZE (sizeof(unsigned long))
79 * Object location (<PFN>, <obj_idx>) is encoded as
80 * as single (unsigned long) handle value.
82 * Note that object index <obj_idx> starts from 0.
84 * This is made more complicated by various memory models and PAE.
87 #ifndef MAX_PHYSMEM_BITS
88 #ifdef CONFIG_HIGHMEM64G
89 #define MAX_PHYSMEM_BITS 36
90 #else /* !CONFIG_HIGHMEM64G */
92 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
95 #define MAX_PHYSMEM_BITS BITS_PER_LONG
98 #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
101 * Memory for allocating for handle keeps object position by
102 * encoding <page, obj_idx> and the encoded value has a room
103 * in least bit(ie, look at obj_to_location).
104 * We use the bit to synchronize between object access by
105 * user and migration.
107 #define HANDLE_PIN_BIT 0
110 * Head in allocated object should have OBJ_ALLOCATED_TAG
111 * to identify the object was allocated or not.
112 * It's okay to add the status bit in the least bit because
113 * header keeps handle which is 4byte-aligned address so we
114 * have room for two bit at least.
116 #define OBJ_ALLOCATED_TAG 1
117 #define OBJ_TAG_BITS 1
118 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
119 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
121 #define FULLNESS_BITS 2
123 #define ISOLATED_BITS 3
124 #define MAGIC_VAL_BITS 8
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 /* each chunk includes extra space to keep handle */
131 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
134 * On systems with 4K page size, this gives 255 size classes! There is a
136 * - Large number of size classes is potentially wasteful as free page are
137 * spread across these classes
138 * - Small number of size classes causes large internal fragmentation
139 * - Probably its better to use specific size classes (empirically
140 * determined). NOTE: all those class sizes must be set as multiple of
141 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
143 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
146 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
147 #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
148 ZS_SIZE_CLASS_DELTA) + 1)
150 enum fullness_group
{
168 struct zs_size_stat
{
169 unsigned long objs
[NR_ZS_STAT_TYPE
];
172 #ifdef CONFIG_ZSMALLOC_STAT
173 static struct dentry
*zs_stat_root
;
176 #ifdef CONFIG_COMPACTION
177 static struct vfsmount
*zsmalloc_mnt
;
181 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
183 * n = number of allocated objects
184 * N = total number of objects zspage can store
185 * f = fullness_threshold_frac
187 * Similarly, we assign zspage to:
188 * ZS_ALMOST_FULL when n > N / f
189 * ZS_EMPTY when n == 0
190 * ZS_FULL when n == N
192 * (see: fix_fullness_group())
194 static const int fullness_threshold_frac
= 4;
198 struct list_head fullness_list
[NR_ZS_FULLNESS
];
200 * Size of objects stored in this class. Must be multiple
205 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
206 int pages_per_zspage
;
209 struct zs_size_stat stats
;
212 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
213 static void SetPageHugeObject(struct page
*page
)
215 SetPageOwnerPriv1(page
);
218 static void ClearPageHugeObject(struct page
*page
)
220 ClearPageOwnerPriv1(page
);
223 static int PageHugeObject(struct page
*page
)
225 return PageOwnerPriv1(page
);
229 * Placed within free objects to form a singly linked list.
230 * For every zspage, zspage->freeobj gives head of this list.
232 * This must be power of 2 and less than or equal to ZS_ALIGN
238 * It's valid for non-allocated object
242 * Handle of allocated object.
244 unsigned long handle
;
251 struct size_class
*size_class
[ZS_SIZE_CLASSES
];
252 struct kmem_cache
*handle_cachep
;
253 struct kmem_cache
*zspage_cachep
;
255 atomic_long_t pages_allocated
;
257 struct zs_pool_stats stats
;
259 /* Compact classes */
260 struct shrinker shrinker
;
262 * To signify that register_shrinker() was successful
263 * and unregister_shrinker() will not Oops.
265 bool shrinker_enabled
;
266 #ifdef CONFIG_ZSMALLOC_STAT
267 struct dentry
*stat_dentry
;
269 #ifdef CONFIG_COMPACTION
271 struct work_struct free_work
;
272 /* A wait queue for when migration races with async_free_zspage() */
273 struct wait_queue_head migration_wait
;
274 atomic_long_t isolated_pages
;
281 unsigned int fullness
:FULLNESS_BITS
;
282 unsigned int class:CLASS_BITS
+ 1;
283 unsigned int isolated
:ISOLATED_BITS
;
284 unsigned int magic
:MAGIC_VAL_BITS
;
287 unsigned int freeobj
;
288 struct page
*first_page
;
289 struct list_head list
; /* fullness list */
290 #ifdef CONFIG_COMPACTION
295 struct mapping_area
{
296 #ifdef CONFIG_PGTABLE_MAPPING
297 struct vm_struct
*vm
; /* vm area for mapping object that span pages */
299 char *vm_buf
; /* copy buffer for objects that span pages */
301 char *vm_addr
; /* address of kmap_atomic()'ed pages */
302 enum zs_mapmode vm_mm
; /* mapping mode */
305 #ifdef CONFIG_COMPACTION
306 static int zs_register_migration(struct zs_pool
*pool
);
307 static void zs_unregister_migration(struct zs_pool
*pool
);
308 static void migrate_lock_init(struct zspage
*zspage
);
309 static void migrate_read_lock(struct zspage
*zspage
);
310 static void migrate_read_unlock(struct zspage
*zspage
);
311 static void kick_deferred_free(struct zs_pool
*pool
);
312 static void init_deferred_free(struct zs_pool
*pool
);
313 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
);
315 static int zsmalloc_mount(void) { return 0; }
316 static void zsmalloc_unmount(void) {}
317 static int zs_register_migration(struct zs_pool
*pool
) { return 0; }
318 static void zs_unregister_migration(struct zs_pool
*pool
) {}
319 static void migrate_lock_init(struct zspage
*zspage
) {}
320 static void migrate_read_lock(struct zspage
*zspage
) {}
321 static void migrate_read_unlock(struct zspage
*zspage
) {}
322 static void kick_deferred_free(struct zs_pool
*pool
) {}
323 static void init_deferred_free(struct zs_pool
*pool
) {}
324 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
) {}
327 static int create_cache(struct zs_pool
*pool
)
329 pool
->handle_cachep
= kmem_cache_create("zs_handle", ZS_HANDLE_SIZE
,
331 if (!pool
->handle_cachep
)
334 pool
->zspage_cachep
= kmem_cache_create("zspage", sizeof(struct zspage
),
336 if (!pool
->zspage_cachep
) {
337 kmem_cache_destroy(pool
->handle_cachep
);
338 pool
->handle_cachep
= NULL
;
345 static void destroy_cache(struct zs_pool
*pool
)
347 kmem_cache_destroy(pool
->handle_cachep
);
348 kmem_cache_destroy(pool
->zspage_cachep
);
351 static unsigned long cache_alloc_handle(struct zs_pool
*pool
, gfp_t gfp
)
353 return (unsigned long)kmem_cache_alloc(pool
->handle_cachep
,
354 gfp
& ~(__GFP_HIGHMEM
|__GFP_MOVABLE
));
357 static void cache_free_handle(struct zs_pool
*pool
, unsigned long handle
)
359 kmem_cache_free(pool
->handle_cachep
, (void *)handle
);
362 static struct zspage
*cache_alloc_zspage(struct zs_pool
*pool
, gfp_t flags
)
364 return kmem_cache_alloc(pool
->zspage_cachep
,
365 flags
& ~(__GFP_HIGHMEM
|__GFP_MOVABLE
));
368 static void cache_free_zspage(struct zs_pool
*pool
, struct zspage
*zspage
)
370 kmem_cache_free(pool
->zspage_cachep
, zspage
);
373 static void record_obj(unsigned long handle
, unsigned long obj
)
376 * lsb of @obj represents handle lock while other bits
377 * represent object value the handle is pointing so
378 * updating shouldn't do store tearing.
380 WRITE_ONCE(*(unsigned long *)handle
, obj
);
387 static void *zs_zpool_create(const char *name
, gfp_t gfp
,
388 const struct zpool_ops
*zpool_ops
,
392 * Ignore global gfp flags: zs_malloc() may be invoked from
393 * different contexts and its caller must provide a valid
396 return zs_create_pool(name
);
399 static void zs_zpool_destroy(void *pool
)
401 zs_destroy_pool(pool
);
404 static int zs_zpool_malloc(void *pool
, size_t size
, gfp_t gfp
,
405 unsigned long *handle
)
407 *handle
= zs_malloc(pool
, size
, gfp
);
408 return *handle
? 0 : -1;
410 static void zs_zpool_free(void *pool
, unsigned long handle
)
412 zs_free(pool
, handle
);
415 static int zs_zpool_shrink(void *pool
, unsigned int pages
,
416 unsigned int *reclaimed
)
421 static void *zs_zpool_map(void *pool
, unsigned long handle
,
422 enum zpool_mapmode mm
)
424 enum zs_mapmode zs_mm
;
433 case ZPOOL_MM_RW
: /* fallthru */
439 return zs_map_object(pool
, handle
, zs_mm
);
441 static void zs_zpool_unmap(void *pool
, unsigned long handle
)
443 zs_unmap_object(pool
, handle
);
446 static u64
zs_zpool_total_size(void *pool
)
448 return zs_get_total_pages(pool
) << PAGE_SHIFT
;
451 static struct zpool_driver zs_zpool_driver
= {
453 .owner
= THIS_MODULE
,
454 .create
= zs_zpool_create
,
455 .destroy
= zs_zpool_destroy
,
456 .malloc
= zs_zpool_malloc
,
457 .free
= zs_zpool_free
,
458 .shrink
= zs_zpool_shrink
,
460 .unmap
= zs_zpool_unmap
,
461 .total_size
= zs_zpool_total_size
,
464 MODULE_ALIAS("zpool-zsmalloc");
465 #endif /* CONFIG_ZPOOL */
467 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
468 static DEFINE_PER_CPU(struct mapping_area
, zs_map_area
);
470 static bool is_zspage_isolated(struct zspage
*zspage
)
472 return zspage
->isolated
;
475 static __maybe_unused
int is_first_page(struct page
*page
)
477 return PagePrivate(page
);
480 /* Protected by class->lock */
481 static inline int get_zspage_inuse(struct zspage
*zspage
)
483 return zspage
->inuse
;
486 static inline void set_zspage_inuse(struct zspage
*zspage
, int val
)
491 static inline void mod_zspage_inuse(struct zspage
*zspage
, int val
)
493 zspage
->inuse
+= val
;
496 static inline struct page
*get_first_page(struct zspage
*zspage
)
498 struct page
*first_page
= zspage
->first_page
;
500 VM_BUG_ON_PAGE(!is_first_page(first_page
), first_page
);
504 static inline int get_first_obj_offset(struct page
*page
)
509 static inline void set_first_obj_offset(struct page
*page
, int offset
)
511 page
->units
= offset
;
514 static inline unsigned int get_freeobj(struct zspage
*zspage
)
516 return zspage
->freeobj
;
519 static inline void set_freeobj(struct zspage
*zspage
, unsigned int obj
)
521 zspage
->freeobj
= obj
;
524 static void get_zspage_mapping(struct zspage
*zspage
,
525 unsigned int *class_idx
,
526 enum fullness_group
*fullness
)
528 BUG_ON(zspage
->magic
!= ZSPAGE_MAGIC
);
530 *fullness
= zspage
->fullness
;
531 *class_idx
= zspage
->class;
534 static void set_zspage_mapping(struct zspage
*zspage
,
535 unsigned int class_idx
,
536 enum fullness_group fullness
)
538 zspage
->class = class_idx
;
539 zspage
->fullness
= fullness
;
543 * zsmalloc divides the pool into various size classes where each
544 * class maintains a list of zspages where each zspage is divided
545 * into equal sized chunks. Each allocation falls into one of these
546 * classes depending on its size. This function returns index of the
547 * size class which has chunk size big enough to hold the give size.
549 static int get_size_class_index(int size
)
553 if (likely(size
> ZS_MIN_ALLOC_SIZE
))
554 idx
= DIV_ROUND_UP(size
- ZS_MIN_ALLOC_SIZE
,
555 ZS_SIZE_CLASS_DELTA
);
557 return min_t(int, ZS_SIZE_CLASSES
- 1, idx
);
560 /* type can be of enum type zs_stat_type or fullness_group */
561 static inline void zs_stat_inc(struct size_class
*class,
562 int type
, unsigned long cnt
)
564 class->stats
.objs
[type
] += cnt
;
567 /* type can be of enum type zs_stat_type or fullness_group */
568 static inline void zs_stat_dec(struct size_class
*class,
569 int type
, unsigned long cnt
)
571 class->stats
.objs
[type
] -= cnt
;
574 /* type can be of enum type zs_stat_type or fullness_group */
575 static inline unsigned long zs_stat_get(struct size_class
*class,
578 return class->stats
.objs
[type
];
581 #ifdef CONFIG_ZSMALLOC_STAT
583 static void __init
zs_stat_init(void)
585 if (!debugfs_initialized()) {
586 pr_warn("debugfs not available, stat dir not created\n");
590 zs_stat_root
= debugfs_create_dir("zsmalloc", NULL
);
592 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
595 static void __exit
zs_stat_exit(void)
597 debugfs_remove_recursive(zs_stat_root
);
600 static unsigned long zs_can_compact(struct size_class
*class);
602 static int zs_stats_size_show(struct seq_file
*s
, void *v
)
605 struct zs_pool
*pool
= s
->private;
606 struct size_class
*class;
608 unsigned long class_almost_full
, class_almost_empty
;
609 unsigned long obj_allocated
, obj_used
, pages_used
, freeable
;
610 unsigned long total_class_almost_full
= 0, total_class_almost_empty
= 0;
611 unsigned long total_objs
= 0, total_used_objs
= 0, total_pages
= 0;
612 unsigned long total_freeable
= 0;
614 seq_printf(s
, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
615 "class", "size", "almost_full", "almost_empty",
616 "obj_allocated", "obj_used", "pages_used",
617 "pages_per_zspage", "freeable");
619 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++) {
620 class = pool
->size_class
[i
];
622 if (class->index
!= i
)
625 spin_lock(&class->lock
);
626 class_almost_full
= zs_stat_get(class, CLASS_ALMOST_FULL
);
627 class_almost_empty
= zs_stat_get(class, CLASS_ALMOST_EMPTY
);
628 obj_allocated
= zs_stat_get(class, OBJ_ALLOCATED
);
629 obj_used
= zs_stat_get(class, OBJ_USED
);
630 freeable
= zs_can_compact(class);
631 spin_unlock(&class->lock
);
633 objs_per_zspage
= class->objs_per_zspage
;
634 pages_used
= obj_allocated
/ objs_per_zspage
*
635 class->pages_per_zspage
;
637 seq_printf(s
, " %5u %5u %11lu %12lu %13lu"
638 " %10lu %10lu %16d %8lu\n",
639 i
, class->size
, class_almost_full
, class_almost_empty
,
640 obj_allocated
, obj_used
, pages_used
,
641 class->pages_per_zspage
, freeable
);
643 total_class_almost_full
+= class_almost_full
;
644 total_class_almost_empty
+= class_almost_empty
;
645 total_objs
+= obj_allocated
;
646 total_used_objs
+= obj_used
;
647 total_pages
+= pages_used
;
648 total_freeable
+= freeable
;
652 seq_printf(s
, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
653 "Total", "", total_class_almost_full
,
654 total_class_almost_empty
, total_objs
,
655 total_used_objs
, total_pages
, "", total_freeable
);
660 static int zs_stats_size_open(struct inode
*inode
, struct file
*file
)
662 return single_open(file
, zs_stats_size_show
, inode
->i_private
);
665 static const struct file_operations zs_stat_size_ops
= {
666 .open
= zs_stats_size_open
,
669 .release
= single_release
,
672 static void zs_pool_stat_create(struct zs_pool
*pool
, const char *name
)
674 struct dentry
*entry
;
677 pr_warn("no root stat dir, not creating <%s> stat dir\n", name
);
681 entry
= debugfs_create_dir(name
, zs_stat_root
);
683 pr_warn("debugfs dir <%s> creation failed\n", name
);
686 pool
->stat_dentry
= entry
;
688 entry
= debugfs_create_file("classes", S_IFREG
| S_IRUGO
,
689 pool
->stat_dentry
, pool
, &zs_stat_size_ops
);
691 pr_warn("%s: debugfs file entry <%s> creation failed\n",
693 debugfs_remove_recursive(pool
->stat_dentry
);
694 pool
->stat_dentry
= NULL
;
698 static void zs_pool_stat_destroy(struct zs_pool
*pool
)
700 debugfs_remove_recursive(pool
->stat_dentry
);
703 #else /* CONFIG_ZSMALLOC_STAT */
704 static void __init
zs_stat_init(void)
708 static void __exit
zs_stat_exit(void)
712 static inline void zs_pool_stat_create(struct zs_pool
*pool
, const char *name
)
716 static inline void zs_pool_stat_destroy(struct zs_pool
*pool
)
723 * For each size class, zspages are divided into different groups
724 * depending on how "full" they are. This was done so that we could
725 * easily find empty or nearly empty zspages when we try to shrink
726 * the pool (not yet implemented). This function returns fullness
727 * status of the given page.
729 static enum fullness_group
get_fullness_group(struct size_class
*class,
730 struct zspage
*zspage
)
732 int inuse
, objs_per_zspage
;
733 enum fullness_group fg
;
735 inuse
= get_zspage_inuse(zspage
);
736 objs_per_zspage
= class->objs_per_zspage
;
740 else if (inuse
== objs_per_zspage
)
742 else if (inuse
<= 3 * objs_per_zspage
/ fullness_threshold_frac
)
743 fg
= ZS_ALMOST_EMPTY
;
751 * Each size class maintains various freelists and zspages are assigned
752 * to one of these freelists based on the number of live objects they
753 * have. This functions inserts the given zspage into the freelist
754 * identified by <class, fullness_group>.
756 static void insert_zspage(struct size_class
*class,
757 struct zspage
*zspage
,
758 enum fullness_group fullness
)
762 zs_stat_inc(class, fullness
, 1);
763 head
= list_first_entry_or_null(&class->fullness_list
[fullness
],
764 struct zspage
, list
);
766 * We want to see more ZS_FULL pages and less almost empty/full.
767 * Put pages with higher ->inuse first.
770 if (get_zspage_inuse(zspage
) < get_zspage_inuse(head
)) {
771 list_add(&zspage
->list
, &head
->list
);
775 list_add(&zspage
->list
, &class->fullness_list
[fullness
]);
779 * This function removes the given zspage from the freelist identified
780 * by <class, fullness_group>.
782 static void remove_zspage(struct size_class
*class,
783 struct zspage
*zspage
,
784 enum fullness_group fullness
)
786 VM_BUG_ON(list_empty(&class->fullness_list
[fullness
]));
787 VM_BUG_ON(is_zspage_isolated(zspage
));
789 list_del_init(&zspage
->list
);
790 zs_stat_dec(class, fullness
, 1);
794 * Each size class maintains zspages in different fullness groups depending
795 * on the number of live objects they contain. When allocating or freeing
796 * objects, the fullness status of the page can change, say, from ALMOST_FULL
797 * to ALMOST_EMPTY when freeing an object. This function checks if such
798 * a status change has occurred for the given page and accordingly moves the
799 * page from the freelist of the old fullness group to that of the new
802 static enum fullness_group
fix_fullness_group(struct size_class
*class,
803 struct zspage
*zspage
)
806 enum fullness_group currfg
, newfg
;
808 get_zspage_mapping(zspage
, &class_idx
, &currfg
);
809 newfg
= get_fullness_group(class, zspage
);
813 if (!is_zspage_isolated(zspage
)) {
814 remove_zspage(class, zspage
, currfg
);
815 insert_zspage(class, zspage
, newfg
);
818 set_zspage_mapping(zspage
, class_idx
, newfg
);
825 * We have to decide on how many pages to link together
826 * to form a zspage for each size class. This is important
827 * to reduce wastage due to unusable space left at end of
828 * each zspage which is given as:
829 * wastage = Zp % class_size
830 * usage = Zp - wastage
831 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
833 * For example, for size class of 3/8 * PAGE_SIZE, we should
834 * link together 3 PAGE_SIZE sized pages to form a zspage
835 * since then we can perfectly fit in 8 such objects.
837 static int get_pages_per_zspage(int class_size
)
839 int i
, max_usedpc
= 0;
840 /* zspage order which gives maximum used size per KB */
841 int max_usedpc_order
= 1;
843 for (i
= 1; i
<= ZS_MAX_PAGES_PER_ZSPAGE
; i
++) {
847 zspage_size
= i
* PAGE_SIZE
;
848 waste
= zspage_size
% class_size
;
849 usedpc
= (zspage_size
- waste
) * 100 / zspage_size
;
851 if (usedpc
> max_usedpc
) {
853 max_usedpc_order
= i
;
857 return max_usedpc_order
;
860 static struct zspage
*get_zspage(struct page
*page
)
862 struct zspage
*zspage
= (struct zspage
*)page
->private;
864 BUG_ON(zspage
->magic
!= ZSPAGE_MAGIC
);
868 static struct page
*get_next_page(struct page
*page
)
870 if (unlikely(PageHugeObject(page
)))
873 return page
->freelist
;
877 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
878 * @page: page object resides in zspage
879 * @obj_idx: object index
881 static void obj_to_location(unsigned long obj
, struct page
**page
,
882 unsigned int *obj_idx
)
884 obj
>>= OBJ_TAG_BITS
;
885 *page
= pfn_to_page(obj
>> OBJ_INDEX_BITS
);
886 *obj_idx
= (obj
& OBJ_INDEX_MASK
);
890 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
891 * @page: page object resides in zspage
892 * @obj_idx: object index
894 static unsigned long location_to_obj(struct page
*page
, unsigned int obj_idx
)
898 obj
= page_to_pfn(page
) << OBJ_INDEX_BITS
;
899 obj
|= obj_idx
& OBJ_INDEX_MASK
;
900 obj
<<= OBJ_TAG_BITS
;
905 static unsigned long handle_to_obj(unsigned long handle
)
907 return *(unsigned long *)handle
;
910 static unsigned long obj_to_head(struct page
*page
, void *obj
)
912 if (unlikely(PageHugeObject(page
))) {
913 VM_BUG_ON_PAGE(!is_first_page(page
), page
);
916 return *(unsigned long *)obj
;
919 static inline int testpin_tag(unsigned long handle
)
921 return bit_spin_is_locked(HANDLE_PIN_BIT
, (unsigned long *)handle
);
924 static inline int trypin_tag(unsigned long handle
)
926 return bit_spin_trylock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
929 static void pin_tag(unsigned long handle
)
931 bit_spin_lock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
934 static void unpin_tag(unsigned long handle
)
936 bit_spin_unlock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
939 static void reset_page(struct page
*page
)
941 __ClearPageMovable(page
);
942 ClearPagePrivate(page
);
943 set_page_private(page
, 0);
944 page_mapcount_reset(page
);
945 ClearPageHugeObject(page
);
946 page
->freelist
= NULL
;
950 * To prevent zspage destroy during migration, zspage freeing should
951 * hold locks of all pages in the zspage.
953 void lock_zspage(struct zspage
*zspage
)
955 struct page
*page
= get_first_page(zspage
);
959 } while ((page
= get_next_page(page
)) != NULL
);
962 int trylock_zspage(struct zspage
*zspage
)
964 struct page
*cursor
, *fail
;
966 for (cursor
= get_first_page(zspage
); cursor
!= NULL
; cursor
=
967 get_next_page(cursor
)) {
968 if (!trylock_page(cursor
)) {
976 for (cursor
= get_first_page(zspage
); cursor
!= fail
; cursor
=
977 get_next_page(cursor
))
983 static void __free_zspage(struct zs_pool
*pool
, struct size_class
*class,
984 struct zspage
*zspage
)
986 struct page
*page
, *next
;
987 enum fullness_group fg
;
988 unsigned int class_idx
;
990 get_zspage_mapping(zspage
, &class_idx
, &fg
);
992 assert_spin_locked(&class->lock
);
994 VM_BUG_ON(get_zspage_inuse(zspage
));
995 VM_BUG_ON(fg
!= ZS_EMPTY
);
997 next
= page
= get_first_page(zspage
);
999 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
1000 next
= get_next_page(page
);
1003 dec_zone_page_state(page
, NR_ZSPAGES
);
1006 } while (page
!= NULL
);
1008 cache_free_zspage(pool
, zspage
);
1010 zs_stat_dec(class, OBJ_ALLOCATED
, class->objs_per_zspage
);
1011 atomic_long_sub(class->pages_per_zspage
,
1012 &pool
->pages_allocated
);
1015 static void free_zspage(struct zs_pool
*pool
, struct size_class
*class,
1016 struct zspage
*zspage
)
1018 VM_BUG_ON(get_zspage_inuse(zspage
));
1019 VM_BUG_ON(list_empty(&zspage
->list
));
1021 if (!trylock_zspage(zspage
)) {
1022 kick_deferred_free(pool
);
1026 remove_zspage(class, zspage
, ZS_EMPTY
);
1027 __free_zspage(pool
, class, zspage
);
1030 /* Initialize a newly allocated zspage */
1031 static void init_zspage(struct size_class
*class, struct zspage
*zspage
)
1033 unsigned int freeobj
= 1;
1034 unsigned long off
= 0;
1035 struct page
*page
= get_first_page(zspage
);
1038 struct page
*next_page
;
1039 struct link_free
*link
;
1042 set_first_obj_offset(page
, off
);
1044 vaddr
= kmap_atomic(page
);
1045 link
= (struct link_free
*)vaddr
+ off
/ sizeof(*link
);
1047 while ((off
+= class->size
) < PAGE_SIZE
) {
1048 link
->next
= freeobj
++ << OBJ_TAG_BITS
;
1049 link
+= class->size
/ sizeof(*link
);
1053 * We now come to the last (full or partial) object on this
1054 * page, which must point to the first object on the next
1057 next_page
= get_next_page(page
);
1059 link
->next
= freeobj
++ << OBJ_TAG_BITS
;
1062 * Reset OBJ_TAG_BITS bit to last link to tell
1063 * whether it's allocated object or not.
1065 link
->next
= -1 << OBJ_TAG_BITS
;
1067 kunmap_atomic(vaddr
);
1072 set_freeobj(zspage
, 0);
1075 static void create_page_chain(struct size_class
*class, struct zspage
*zspage
,
1076 struct page
*pages
[])
1080 struct page
*prev_page
= NULL
;
1081 int nr_pages
= class->pages_per_zspage
;
1084 * Allocate individual pages and link them together as:
1085 * 1. all pages are linked together using page->freelist
1086 * 2. each sub-page point to zspage using page->private
1088 * we set PG_private to identify the first page (i.e. no other sub-page
1089 * has this flag set).
1091 for (i
= 0; i
< nr_pages
; i
++) {
1093 set_page_private(page
, (unsigned long)zspage
);
1094 page
->freelist
= NULL
;
1096 zspage
->first_page
= page
;
1097 SetPagePrivate(page
);
1098 if (unlikely(class->objs_per_zspage
== 1 &&
1099 class->pages_per_zspage
== 1))
1100 SetPageHugeObject(page
);
1102 prev_page
->freelist
= page
;
1109 * Allocate a zspage for the given size class
1111 static struct zspage
*alloc_zspage(struct zs_pool
*pool
,
1112 struct size_class
*class,
1116 struct page
*pages
[ZS_MAX_PAGES_PER_ZSPAGE
];
1117 struct zspage
*zspage
= cache_alloc_zspage(pool
, gfp
);
1122 memset(zspage
, 0, sizeof(struct zspage
));
1123 zspage
->magic
= ZSPAGE_MAGIC
;
1124 migrate_lock_init(zspage
);
1126 for (i
= 0; i
< class->pages_per_zspage
; i
++) {
1129 page
= alloc_page(gfp
);
1132 dec_zone_page_state(pages
[i
], NR_ZSPAGES
);
1133 __free_page(pages
[i
]);
1135 cache_free_zspage(pool
, zspage
);
1139 inc_zone_page_state(page
, NR_ZSPAGES
);
1143 create_page_chain(class, zspage
, pages
);
1144 init_zspage(class, zspage
);
1149 static struct zspage
*find_get_zspage(struct size_class
*class)
1152 struct zspage
*zspage
;
1154 for (i
= ZS_ALMOST_FULL
; i
>= ZS_EMPTY
; i
--) {
1155 zspage
= list_first_entry_or_null(&class->fullness_list
[i
],
1156 struct zspage
, list
);
1164 #ifdef CONFIG_PGTABLE_MAPPING
1165 static inline int __zs_cpu_up(struct mapping_area
*area
)
1168 * Make sure we don't leak memory if a cpu UP notification
1169 * and zs_init() race and both call zs_cpu_up() on the same cpu
1173 area
->vm
= alloc_vm_area(PAGE_SIZE
* 2, NULL
);
1179 static inline void __zs_cpu_down(struct mapping_area
*area
)
1182 free_vm_area(area
->vm
);
1186 static inline void *__zs_map_object(struct mapping_area
*area
,
1187 struct page
*pages
[2], int off
, int size
)
1189 BUG_ON(map_vm_area(area
->vm
, PAGE_KERNEL
, pages
));
1190 area
->vm_addr
= area
->vm
->addr
;
1191 return area
->vm_addr
+ off
;
1194 static inline void __zs_unmap_object(struct mapping_area
*area
,
1195 struct page
*pages
[2], int off
, int size
)
1197 unsigned long addr
= (unsigned long)area
->vm_addr
;
1199 unmap_kernel_range(addr
, PAGE_SIZE
* 2);
1202 #else /* CONFIG_PGTABLE_MAPPING */
1204 static inline int __zs_cpu_up(struct mapping_area
*area
)
1207 * Make sure we don't leak memory if a cpu UP notification
1208 * and zs_init() race and both call zs_cpu_up() on the same cpu
1212 area
->vm_buf
= kmalloc(ZS_MAX_ALLOC_SIZE
, GFP_KERNEL
);
1218 static inline void __zs_cpu_down(struct mapping_area
*area
)
1220 kfree(area
->vm_buf
);
1221 area
->vm_buf
= NULL
;
1224 static void *__zs_map_object(struct mapping_area
*area
,
1225 struct page
*pages
[2], int off
, int size
)
1229 char *buf
= area
->vm_buf
;
1231 /* disable page faults to match kmap_atomic() return conditions */
1232 pagefault_disable();
1234 /* no read fastpath */
1235 if (area
->vm_mm
== ZS_MM_WO
)
1238 sizes
[0] = PAGE_SIZE
- off
;
1239 sizes
[1] = size
- sizes
[0];
1241 /* copy object to per-cpu buffer */
1242 addr
= kmap_atomic(pages
[0]);
1243 memcpy(buf
, addr
+ off
, sizes
[0]);
1244 kunmap_atomic(addr
);
1245 addr
= kmap_atomic(pages
[1]);
1246 memcpy(buf
+ sizes
[0], addr
, sizes
[1]);
1247 kunmap_atomic(addr
);
1249 return area
->vm_buf
;
1252 static void __zs_unmap_object(struct mapping_area
*area
,
1253 struct page
*pages
[2], int off
, int size
)
1259 /* no write fastpath */
1260 if (area
->vm_mm
== ZS_MM_RO
)
1264 buf
= buf
+ ZS_HANDLE_SIZE
;
1265 size
-= ZS_HANDLE_SIZE
;
1266 off
+= ZS_HANDLE_SIZE
;
1268 sizes
[0] = PAGE_SIZE
- off
;
1269 sizes
[1] = size
- sizes
[0];
1271 /* copy per-cpu buffer to object */
1272 addr
= kmap_atomic(pages
[0]);
1273 memcpy(addr
+ off
, buf
, sizes
[0]);
1274 kunmap_atomic(addr
);
1275 addr
= kmap_atomic(pages
[1]);
1276 memcpy(addr
, buf
+ sizes
[0], sizes
[1]);
1277 kunmap_atomic(addr
);
1280 /* enable page faults to match kunmap_atomic() return conditions */
1284 #endif /* CONFIG_PGTABLE_MAPPING */
1286 static int zs_cpu_prepare(unsigned int cpu
)
1288 struct mapping_area
*area
;
1290 area
= &per_cpu(zs_map_area
, cpu
);
1291 return __zs_cpu_up(area
);
1294 static int zs_cpu_dead(unsigned int cpu
)
1296 struct mapping_area
*area
;
1298 area
= &per_cpu(zs_map_area
, cpu
);
1299 __zs_cpu_down(area
);
1303 static bool can_merge(struct size_class
*prev
, int pages_per_zspage
,
1304 int objs_per_zspage
)
1306 if (prev
->pages_per_zspage
== pages_per_zspage
&&
1307 prev
->objs_per_zspage
== objs_per_zspage
)
1313 static bool zspage_full(struct size_class
*class, struct zspage
*zspage
)
1315 return get_zspage_inuse(zspage
) == class->objs_per_zspage
;
1318 unsigned long zs_get_total_pages(struct zs_pool
*pool
)
1320 return atomic_long_read(&pool
->pages_allocated
);
1322 EXPORT_SYMBOL_GPL(zs_get_total_pages
);
1325 * zs_map_object - get address of allocated object from handle.
1326 * @pool: pool from which the object was allocated
1327 * @handle: handle returned from zs_malloc
1329 * Before using an object allocated from zs_malloc, it must be mapped using
1330 * this function. When done with the object, it must be unmapped using
1333 * Only one object can be mapped per cpu at a time. There is no protection
1334 * against nested mappings.
1336 * This function returns with preemption and page faults disabled.
1338 void *zs_map_object(struct zs_pool
*pool
, unsigned long handle
,
1341 struct zspage
*zspage
;
1343 unsigned long obj
, off
;
1344 unsigned int obj_idx
;
1346 unsigned int class_idx
;
1347 enum fullness_group fg
;
1348 struct size_class
*class;
1349 struct mapping_area
*area
;
1350 struct page
*pages
[2];
1354 * Because we use per-cpu mapping areas shared among the
1355 * pools/users, we can't allow mapping in interrupt context
1356 * because it can corrupt another users mappings.
1358 BUG_ON(in_interrupt());
1360 /* From now on, migration cannot move the object */
1363 obj
= handle_to_obj(handle
);
1364 obj_to_location(obj
, &page
, &obj_idx
);
1365 zspage
= get_zspage(page
);
1367 /* migration cannot move any subpage in this zspage */
1368 migrate_read_lock(zspage
);
1370 get_zspage_mapping(zspage
, &class_idx
, &fg
);
1371 class = pool
->size_class
[class_idx
];
1372 off
= (class->size
* obj_idx
) & ~PAGE_MASK
;
1374 area
= &get_cpu_var(zs_map_area
);
1376 if (off
+ class->size
<= PAGE_SIZE
) {
1377 /* this object is contained entirely within a page */
1378 area
->vm_addr
= kmap_atomic(page
);
1379 ret
= area
->vm_addr
+ off
;
1383 /* this object spans two pages */
1385 pages
[1] = get_next_page(page
);
1388 ret
= __zs_map_object(area
, pages
, off
, class->size
);
1390 if (likely(!PageHugeObject(page
)))
1391 ret
+= ZS_HANDLE_SIZE
;
1395 EXPORT_SYMBOL_GPL(zs_map_object
);
1397 void zs_unmap_object(struct zs_pool
*pool
, unsigned long handle
)
1399 struct zspage
*zspage
;
1401 unsigned long obj
, off
;
1402 unsigned int obj_idx
;
1404 unsigned int class_idx
;
1405 enum fullness_group fg
;
1406 struct size_class
*class;
1407 struct mapping_area
*area
;
1409 obj
= handle_to_obj(handle
);
1410 obj_to_location(obj
, &page
, &obj_idx
);
1411 zspage
= get_zspage(page
);
1412 get_zspage_mapping(zspage
, &class_idx
, &fg
);
1413 class = pool
->size_class
[class_idx
];
1414 off
= (class->size
* obj_idx
) & ~PAGE_MASK
;
1416 area
= this_cpu_ptr(&zs_map_area
);
1417 if (off
+ class->size
<= PAGE_SIZE
)
1418 kunmap_atomic(area
->vm_addr
);
1420 struct page
*pages
[2];
1423 pages
[1] = get_next_page(page
);
1426 __zs_unmap_object(area
, pages
, off
, class->size
);
1428 put_cpu_var(zs_map_area
);
1430 migrate_read_unlock(zspage
);
1433 EXPORT_SYMBOL_GPL(zs_unmap_object
);
1435 static unsigned long obj_malloc(struct size_class
*class,
1436 struct zspage
*zspage
, unsigned long handle
)
1438 int i
, nr_page
, offset
;
1440 struct link_free
*link
;
1442 struct page
*m_page
;
1443 unsigned long m_offset
;
1446 handle
|= OBJ_ALLOCATED_TAG
;
1447 obj
= get_freeobj(zspage
);
1449 offset
= obj
* class->size
;
1450 nr_page
= offset
>> PAGE_SHIFT
;
1451 m_offset
= offset
& ~PAGE_MASK
;
1452 m_page
= get_first_page(zspage
);
1454 for (i
= 0; i
< nr_page
; i
++)
1455 m_page
= get_next_page(m_page
);
1457 vaddr
= kmap_atomic(m_page
);
1458 link
= (struct link_free
*)vaddr
+ m_offset
/ sizeof(*link
);
1459 set_freeobj(zspage
, link
->next
>> OBJ_TAG_BITS
);
1460 if (likely(!PageHugeObject(m_page
)))
1461 /* record handle in the header of allocated chunk */
1462 link
->handle
= handle
;
1464 /* record handle to page->index */
1465 zspage
->first_page
->index
= handle
;
1467 kunmap_atomic(vaddr
);
1468 mod_zspage_inuse(zspage
, 1);
1469 zs_stat_inc(class, OBJ_USED
, 1);
1471 obj
= location_to_obj(m_page
, obj
);
1478 * zs_malloc - Allocate block of given size from pool.
1479 * @pool: pool to allocate from
1480 * @size: size of block to allocate
1481 * @gfp: gfp flags when allocating object
1483 * On success, handle to the allocated object is returned,
1485 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1487 unsigned long zs_malloc(struct zs_pool
*pool
, size_t size
, gfp_t gfp
)
1489 unsigned long handle
, obj
;
1490 struct size_class
*class;
1491 enum fullness_group newfg
;
1492 struct zspage
*zspage
;
1494 if (unlikely(!size
|| size
> ZS_MAX_ALLOC_SIZE
))
1497 handle
= cache_alloc_handle(pool
, gfp
);
1501 /* extra space in chunk to keep the handle */
1502 size
+= ZS_HANDLE_SIZE
;
1503 class = pool
->size_class
[get_size_class_index(size
)];
1505 spin_lock(&class->lock
);
1506 zspage
= find_get_zspage(class);
1507 if (likely(zspage
)) {
1508 obj
= obj_malloc(class, zspage
, handle
);
1509 /* Now move the zspage to another fullness group, if required */
1510 fix_fullness_group(class, zspage
);
1511 record_obj(handle
, obj
);
1512 spin_unlock(&class->lock
);
1517 spin_unlock(&class->lock
);
1519 zspage
= alloc_zspage(pool
, class, gfp
);
1521 cache_free_handle(pool
, handle
);
1525 spin_lock(&class->lock
);
1526 obj
= obj_malloc(class, zspage
, handle
);
1527 newfg
= get_fullness_group(class, zspage
);
1528 insert_zspage(class, zspage
, newfg
);
1529 set_zspage_mapping(zspage
, class->index
, newfg
);
1530 record_obj(handle
, obj
);
1531 atomic_long_add(class->pages_per_zspage
,
1532 &pool
->pages_allocated
);
1533 zs_stat_inc(class, OBJ_ALLOCATED
, class->objs_per_zspage
);
1535 /* We completely set up zspage so mark them as movable */
1536 SetZsPageMovable(pool
, zspage
);
1537 spin_unlock(&class->lock
);
1541 EXPORT_SYMBOL_GPL(zs_malloc
);
1543 static void obj_free(struct size_class
*class, unsigned long obj
)
1545 struct link_free
*link
;
1546 struct zspage
*zspage
;
1547 struct page
*f_page
;
1548 unsigned long f_offset
;
1549 unsigned int f_objidx
;
1552 obj
&= ~OBJ_ALLOCATED_TAG
;
1553 obj_to_location(obj
, &f_page
, &f_objidx
);
1554 f_offset
= (class->size
* f_objidx
) & ~PAGE_MASK
;
1555 zspage
= get_zspage(f_page
);
1557 vaddr
= kmap_atomic(f_page
);
1559 /* Insert this object in containing zspage's freelist */
1560 link
= (struct link_free
*)(vaddr
+ f_offset
);
1561 link
->next
= get_freeobj(zspage
) << OBJ_TAG_BITS
;
1562 kunmap_atomic(vaddr
);
1563 set_freeobj(zspage
, f_objidx
);
1564 mod_zspage_inuse(zspage
, -1);
1565 zs_stat_dec(class, OBJ_USED
, 1);
1568 void zs_free(struct zs_pool
*pool
, unsigned long handle
)
1570 struct zspage
*zspage
;
1571 struct page
*f_page
;
1573 unsigned int f_objidx
;
1575 struct size_class
*class;
1576 enum fullness_group fullness
;
1579 if (unlikely(!handle
))
1583 obj
= handle_to_obj(handle
);
1584 obj_to_location(obj
, &f_page
, &f_objidx
);
1585 zspage
= get_zspage(f_page
);
1587 migrate_read_lock(zspage
);
1589 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
1590 class = pool
->size_class
[class_idx
];
1592 spin_lock(&class->lock
);
1593 obj_free(class, obj
);
1594 fullness
= fix_fullness_group(class, zspage
);
1595 if (fullness
!= ZS_EMPTY
) {
1596 migrate_read_unlock(zspage
);
1600 isolated
= is_zspage_isolated(zspage
);
1601 migrate_read_unlock(zspage
);
1602 /* If zspage is isolated, zs_page_putback will free the zspage */
1603 if (likely(!isolated
))
1604 free_zspage(pool
, class, zspage
);
1607 spin_unlock(&class->lock
);
1609 cache_free_handle(pool
, handle
);
1611 EXPORT_SYMBOL_GPL(zs_free
);
1613 static void zs_object_copy(struct size_class
*class, unsigned long dst
,
1616 struct page
*s_page
, *d_page
;
1617 unsigned int s_objidx
, d_objidx
;
1618 unsigned long s_off
, d_off
;
1619 void *s_addr
, *d_addr
;
1620 int s_size
, d_size
, size
;
1623 s_size
= d_size
= class->size
;
1625 obj_to_location(src
, &s_page
, &s_objidx
);
1626 obj_to_location(dst
, &d_page
, &d_objidx
);
1628 s_off
= (class->size
* s_objidx
) & ~PAGE_MASK
;
1629 d_off
= (class->size
* d_objidx
) & ~PAGE_MASK
;
1631 if (s_off
+ class->size
> PAGE_SIZE
)
1632 s_size
= PAGE_SIZE
- s_off
;
1634 if (d_off
+ class->size
> PAGE_SIZE
)
1635 d_size
= PAGE_SIZE
- d_off
;
1637 s_addr
= kmap_atomic(s_page
);
1638 d_addr
= kmap_atomic(d_page
);
1641 size
= min(s_size
, d_size
);
1642 memcpy(d_addr
+ d_off
, s_addr
+ s_off
, size
);
1645 if (written
== class->size
)
1653 if (s_off
>= PAGE_SIZE
) {
1654 kunmap_atomic(d_addr
);
1655 kunmap_atomic(s_addr
);
1656 s_page
= get_next_page(s_page
);
1657 s_addr
= kmap_atomic(s_page
);
1658 d_addr
= kmap_atomic(d_page
);
1659 s_size
= class->size
- written
;
1663 if (d_off
>= PAGE_SIZE
) {
1664 kunmap_atomic(d_addr
);
1665 d_page
= get_next_page(d_page
);
1666 d_addr
= kmap_atomic(d_page
);
1667 d_size
= class->size
- written
;
1672 kunmap_atomic(d_addr
);
1673 kunmap_atomic(s_addr
);
1677 * Find alloced object in zspage from index object and
1680 static unsigned long find_alloced_obj(struct size_class
*class,
1681 struct page
*page
, int *obj_idx
)
1685 int index
= *obj_idx
;
1686 unsigned long handle
= 0;
1687 void *addr
= kmap_atomic(page
);
1689 offset
= get_first_obj_offset(page
);
1690 offset
+= class->size
* index
;
1692 while (offset
< PAGE_SIZE
) {
1693 head
= obj_to_head(page
, addr
+ offset
);
1694 if (head
& OBJ_ALLOCATED_TAG
) {
1695 handle
= head
& ~OBJ_ALLOCATED_TAG
;
1696 if (trypin_tag(handle
))
1701 offset
+= class->size
;
1705 kunmap_atomic(addr
);
1712 struct zs_compact_control
{
1713 /* Source spage for migration which could be a subpage of zspage */
1714 struct page
*s_page
;
1715 /* Destination page for migration which should be a first page
1717 struct page
*d_page
;
1718 /* Starting object index within @s_page which used for live object
1719 * in the subpage. */
1723 static int migrate_zspage(struct zs_pool
*pool
, struct size_class
*class,
1724 struct zs_compact_control
*cc
)
1726 unsigned long used_obj
, free_obj
;
1727 unsigned long handle
;
1728 struct page
*s_page
= cc
->s_page
;
1729 struct page
*d_page
= cc
->d_page
;
1730 int obj_idx
= cc
->obj_idx
;
1734 handle
= find_alloced_obj(class, s_page
, &obj_idx
);
1736 s_page
= get_next_page(s_page
);
1743 /* Stop if there is no more space */
1744 if (zspage_full(class, get_zspage(d_page
))) {
1750 used_obj
= handle_to_obj(handle
);
1751 free_obj
= obj_malloc(class, get_zspage(d_page
), handle
);
1752 zs_object_copy(class, free_obj
, used_obj
);
1755 * record_obj updates handle's value to free_obj and it will
1756 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1757 * breaks synchronization using pin_tag(e,g, zs_free) so
1758 * let's keep the lock bit.
1760 free_obj
|= BIT(HANDLE_PIN_BIT
);
1761 record_obj(handle
, free_obj
);
1763 obj_free(class, used_obj
);
1766 /* Remember last position in this iteration */
1767 cc
->s_page
= s_page
;
1768 cc
->obj_idx
= obj_idx
;
1773 static struct zspage
*isolate_zspage(struct size_class
*class, bool source
)
1776 struct zspage
*zspage
;
1777 enum fullness_group fg
[2] = {ZS_ALMOST_EMPTY
, ZS_ALMOST_FULL
};
1780 fg
[0] = ZS_ALMOST_FULL
;
1781 fg
[1] = ZS_ALMOST_EMPTY
;
1784 for (i
= 0; i
< 2; i
++) {
1785 zspage
= list_first_entry_or_null(&class->fullness_list
[fg
[i
]],
1786 struct zspage
, list
);
1788 VM_BUG_ON(is_zspage_isolated(zspage
));
1789 remove_zspage(class, zspage
, fg
[i
]);
1798 * putback_zspage - add @zspage into right class's fullness list
1799 * @class: destination class
1800 * @zspage: target page
1802 * Return @zspage's fullness_group
1804 static enum fullness_group
putback_zspage(struct size_class
*class,
1805 struct zspage
*zspage
)
1807 enum fullness_group fullness
;
1809 VM_BUG_ON(is_zspage_isolated(zspage
));
1811 fullness
= get_fullness_group(class, zspage
);
1812 insert_zspage(class, zspage
, fullness
);
1813 set_zspage_mapping(zspage
, class->index
, fullness
);
1818 #ifdef CONFIG_COMPACTION
1819 static struct dentry
*zs_mount(struct file_system_type
*fs_type
,
1820 int flags
, const char *dev_name
, void *data
)
1822 static const struct dentry_operations ops
= {
1823 .d_dname
= simple_dname
,
1826 return mount_pseudo(fs_type
, "zsmalloc:", NULL
, &ops
, ZSMALLOC_MAGIC
);
1829 static struct file_system_type zsmalloc_fs
= {
1832 .kill_sb
= kill_anon_super
,
1835 static int zsmalloc_mount(void)
1839 zsmalloc_mnt
= kern_mount(&zsmalloc_fs
);
1840 if (IS_ERR(zsmalloc_mnt
))
1841 ret
= PTR_ERR(zsmalloc_mnt
);
1846 static void zsmalloc_unmount(void)
1848 kern_unmount(zsmalloc_mnt
);
1851 static void migrate_lock_init(struct zspage
*zspage
)
1853 rwlock_init(&zspage
->lock
);
1856 static void migrate_read_lock(struct zspage
*zspage
)
1858 read_lock(&zspage
->lock
);
1861 static void migrate_read_unlock(struct zspage
*zspage
)
1863 read_unlock(&zspage
->lock
);
1866 static void migrate_write_lock(struct zspage
*zspage
)
1868 write_lock(&zspage
->lock
);
1871 static void migrate_write_unlock(struct zspage
*zspage
)
1873 write_unlock(&zspage
->lock
);
1876 /* Number of isolated subpage for *page migration* in this zspage */
1877 static void inc_zspage_isolation(struct zspage
*zspage
)
1882 static void dec_zspage_isolation(struct zspage
*zspage
)
1887 static void putback_zspage_deferred(struct zs_pool
*pool
,
1888 struct size_class
*class,
1889 struct zspage
*zspage
)
1891 enum fullness_group fg
;
1893 fg
= putback_zspage(class, zspage
);
1895 schedule_work(&pool
->free_work
);
1899 static inline void zs_pool_dec_isolated(struct zs_pool
*pool
)
1901 VM_BUG_ON(atomic_long_read(&pool
->isolated_pages
) <= 0);
1902 atomic_long_dec(&pool
->isolated_pages
);
1904 * There's no possibility of racing, since wait_for_isolated_drain()
1905 * checks the isolated count under &class->lock after enqueuing
1906 * on migration_wait.
1908 if (atomic_long_read(&pool
->isolated_pages
) == 0 && pool
->destroying
)
1909 wake_up_all(&pool
->migration_wait
);
1912 static void replace_sub_page(struct size_class
*class, struct zspage
*zspage
,
1913 struct page
*newpage
, struct page
*oldpage
)
1916 struct page
*pages
[ZS_MAX_PAGES_PER_ZSPAGE
] = {NULL
, };
1919 page
= get_first_page(zspage
);
1921 if (page
== oldpage
)
1922 pages
[idx
] = newpage
;
1926 } while ((page
= get_next_page(page
)) != NULL
);
1928 create_page_chain(class, zspage
, pages
);
1929 set_first_obj_offset(newpage
, get_first_obj_offset(oldpage
));
1930 if (unlikely(PageHugeObject(oldpage
)))
1931 newpage
->index
= oldpage
->index
;
1932 __SetPageMovable(newpage
, page_mapping(oldpage
));
1935 bool zs_page_isolate(struct page
*page
, isolate_mode_t mode
)
1937 struct zs_pool
*pool
;
1938 struct size_class
*class;
1940 enum fullness_group fullness
;
1941 struct zspage
*zspage
;
1942 struct address_space
*mapping
;
1945 * Page is locked so zspage couldn't be destroyed. For detail, look at
1946 * lock_zspage in free_zspage.
1948 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
1949 VM_BUG_ON_PAGE(PageIsolated(page
), page
);
1951 zspage
= get_zspage(page
);
1954 * Without class lock, fullness could be stale while class_idx is okay
1955 * because class_idx is constant unless page is freed so we should get
1956 * fullness again under class lock.
1958 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
1959 mapping
= page_mapping(page
);
1960 pool
= mapping
->private_data
;
1961 class = pool
->size_class
[class_idx
];
1963 spin_lock(&class->lock
);
1964 if (get_zspage_inuse(zspage
) == 0) {
1965 spin_unlock(&class->lock
);
1969 /* zspage is isolated for object migration */
1970 if (list_empty(&zspage
->list
) && !is_zspage_isolated(zspage
)) {
1971 spin_unlock(&class->lock
);
1976 * If this is first time isolation for the zspage, isolate zspage from
1977 * size_class to prevent further object allocation from the zspage.
1979 if (!list_empty(&zspage
->list
) && !is_zspage_isolated(zspage
)) {
1980 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
1981 atomic_long_inc(&pool
->isolated_pages
);
1982 remove_zspage(class, zspage
, fullness
);
1985 inc_zspage_isolation(zspage
);
1986 spin_unlock(&class->lock
);
1991 int zs_page_migrate(struct address_space
*mapping
, struct page
*newpage
,
1992 struct page
*page
, enum migrate_mode mode
)
1994 struct zs_pool
*pool
;
1995 struct size_class
*class;
1997 enum fullness_group fullness
;
1998 struct zspage
*zspage
;
2000 void *s_addr
, *d_addr
, *addr
;
2002 unsigned long handle
, head
;
2003 unsigned long old_obj
, new_obj
;
2004 unsigned int obj_idx
;
2008 * We cannot support the _NO_COPY case here, because copy needs to
2009 * happen under the zs lock, which does not work with
2010 * MIGRATE_SYNC_NO_COPY workflow.
2012 if (mode
== MIGRATE_SYNC_NO_COPY
)
2015 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
2016 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
2018 zspage
= get_zspage(page
);
2020 /* Concurrent compactor cannot migrate any subpage in zspage */
2021 migrate_write_lock(zspage
);
2022 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2023 pool
= mapping
->private_data
;
2024 class = pool
->size_class
[class_idx
];
2025 offset
= get_first_obj_offset(page
);
2027 spin_lock(&class->lock
);
2028 if (!get_zspage_inuse(zspage
)) {
2030 * Set "offset" to end of the page so that every loops
2031 * skips unnecessary object scanning.
2037 s_addr
= kmap_atomic(page
);
2038 while (pos
< PAGE_SIZE
) {
2039 head
= obj_to_head(page
, s_addr
+ pos
);
2040 if (head
& OBJ_ALLOCATED_TAG
) {
2041 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2042 if (!trypin_tag(handle
))
2049 * Here, any user cannot access all objects in the zspage so let's move.
2051 d_addr
= kmap_atomic(newpage
);
2052 memcpy(d_addr
, s_addr
, PAGE_SIZE
);
2053 kunmap_atomic(d_addr
);
2055 for (addr
= s_addr
+ offset
; addr
< s_addr
+ pos
;
2056 addr
+= class->size
) {
2057 head
= obj_to_head(page
, addr
);
2058 if (head
& OBJ_ALLOCATED_TAG
) {
2059 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2060 if (!testpin_tag(handle
))
2063 old_obj
= handle_to_obj(handle
);
2064 obj_to_location(old_obj
, &dummy
, &obj_idx
);
2065 new_obj
= (unsigned long)location_to_obj(newpage
,
2067 new_obj
|= BIT(HANDLE_PIN_BIT
);
2068 record_obj(handle
, new_obj
);
2072 replace_sub_page(class, zspage
, newpage
, page
);
2075 dec_zspage_isolation(zspage
);
2078 * Page migration is done so let's putback isolated zspage to
2079 * the list if @page is final isolated subpage in the zspage.
2081 if (!is_zspage_isolated(zspage
)) {
2083 * We cannot race with zs_destroy_pool() here because we wait
2084 * for isolation to hit zero before we start destroying.
2085 * Also, we ensure that everyone can see pool->destroying before
2088 putback_zspage_deferred(pool
, class, zspage
);
2089 zs_pool_dec_isolated(pool
);
2096 ret
= MIGRATEPAGE_SUCCESS
;
2098 for (addr
= s_addr
+ offset
; addr
< s_addr
+ pos
;
2099 addr
+= class->size
) {
2100 head
= obj_to_head(page
, addr
);
2101 if (head
& OBJ_ALLOCATED_TAG
) {
2102 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2103 if (!testpin_tag(handle
))
2108 kunmap_atomic(s_addr
);
2109 spin_unlock(&class->lock
);
2110 migrate_write_unlock(zspage
);
2115 void zs_page_putback(struct page
*page
)
2117 struct zs_pool
*pool
;
2118 struct size_class
*class;
2120 enum fullness_group fg
;
2121 struct address_space
*mapping
;
2122 struct zspage
*zspage
;
2124 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
2125 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
2127 zspage
= get_zspage(page
);
2128 get_zspage_mapping(zspage
, &class_idx
, &fg
);
2129 mapping
= page_mapping(page
);
2130 pool
= mapping
->private_data
;
2131 class = pool
->size_class
[class_idx
];
2133 spin_lock(&class->lock
);
2134 dec_zspage_isolation(zspage
);
2135 if (!is_zspage_isolated(zspage
)) {
2137 * Due to page_lock, we cannot free zspage immediately
2140 putback_zspage_deferred(pool
, class, zspage
);
2141 zs_pool_dec_isolated(pool
);
2143 spin_unlock(&class->lock
);
2146 const struct address_space_operations zsmalloc_aops
= {
2147 .isolate_page
= zs_page_isolate
,
2148 .migratepage
= zs_page_migrate
,
2149 .putback_page
= zs_page_putback
,
2152 static int zs_register_migration(struct zs_pool
*pool
)
2154 pool
->inode
= alloc_anon_inode(zsmalloc_mnt
->mnt_sb
);
2155 if (IS_ERR(pool
->inode
)) {
2160 pool
->inode
->i_mapping
->private_data
= pool
;
2161 pool
->inode
->i_mapping
->a_ops
= &zsmalloc_aops
;
2165 static bool pool_isolated_are_drained(struct zs_pool
*pool
)
2167 return atomic_long_read(&pool
->isolated_pages
) == 0;
2170 /* Function for resolving migration */
2171 static void wait_for_isolated_drain(struct zs_pool
*pool
)
2175 * We're in the process of destroying the pool, so there are no
2176 * active allocations. zs_page_isolate() fails for completely free
2177 * zspages, so we need only wait for the zs_pool's isolated
2178 * count to hit zero.
2180 wait_event(pool
->migration_wait
,
2181 pool_isolated_are_drained(pool
));
2184 static void zs_unregister_migration(struct zs_pool
*pool
)
2186 pool
->destroying
= true;
2188 * We need a memory barrier here to ensure global visibility of
2189 * pool->destroying. Thus pool->isolated pages will either be 0 in which
2190 * case we don't care, or it will be > 0 and pool->destroying will
2191 * ensure that we wake up once isolation hits 0.
2194 wait_for_isolated_drain(pool
); /* This can block */
2195 flush_work(&pool
->free_work
);
2200 * Caller should hold page_lock of all pages in the zspage
2201 * In here, we cannot use zspage meta data.
2203 static void async_free_zspage(struct work_struct
*work
)
2206 struct size_class
*class;
2207 unsigned int class_idx
;
2208 enum fullness_group fullness
;
2209 struct zspage
*zspage
, *tmp
;
2210 LIST_HEAD(free_pages
);
2211 struct zs_pool
*pool
= container_of(work
, struct zs_pool
,
2214 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++) {
2215 class = pool
->size_class
[i
];
2216 if (class->index
!= i
)
2219 spin_lock(&class->lock
);
2220 list_splice_init(&class->fullness_list
[ZS_EMPTY
], &free_pages
);
2221 spin_unlock(&class->lock
);
2225 list_for_each_entry_safe(zspage
, tmp
, &free_pages
, list
) {
2226 list_del(&zspage
->list
);
2227 lock_zspage(zspage
);
2229 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2230 VM_BUG_ON(fullness
!= ZS_EMPTY
);
2231 class = pool
->size_class
[class_idx
];
2232 spin_lock(&class->lock
);
2233 __free_zspage(pool
, pool
->size_class
[class_idx
], zspage
);
2234 spin_unlock(&class->lock
);
2238 static void kick_deferred_free(struct zs_pool
*pool
)
2240 schedule_work(&pool
->free_work
);
2243 static void init_deferred_free(struct zs_pool
*pool
)
2245 INIT_WORK(&pool
->free_work
, async_free_zspage
);
2248 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
)
2250 struct page
*page
= get_first_page(zspage
);
2253 WARN_ON(!trylock_page(page
));
2254 __SetPageMovable(page
, pool
->inode
->i_mapping
);
2256 } while ((page
= get_next_page(page
)) != NULL
);
2262 * Based on the number of unused allocated objects calculate
2263 * and return the number of pages that we can free.
2265 static unsigned long zs_can_compact(struct size_class
*class)
2267 unsigned long obj_wasted
;
2268 unsigned long obj_allocated
= zs_stat_get(class, OBJ_ALLOCATED
);
2269 unsigned long obj_used
= zs_stat_get(class, OBJ_USED
);
2271 if (obj_allocated
<= obj_used
)
2274 obj_wasted
= obj_allocated
- obj_used
;
2275 obj_wasted
/= class->objs_per_zspage
;
2277 return obj_wasted
* class->pages_per_zspage
;
2280 static void __zs_compact(struct zs_pool
*pool
, struct size_class
*class)
2282 struct zs_compact_control cc
;
2283 struct zspage
*src_zspage
;
2284 struct zspage
*dst_zspage
= NULL
;
2286 spin_lock(&class->lock
);
2287 while ((src_zspage
= isolate_zspage(class, true))) {
2289 if (!zs_can_compact(class))
2293 cc
.s_page
= get_first_page(src_zspage
);
2295 while ((dst_zspage
= isolate_zspage(class, false))) {
2296 cc
.d_page
= get_first_page(dst_zspage
);
2298 * If there is no more space in dst_page, resched
2299 * and see if anyone had allocated another zspage.
2301 if (!migrate_zspage(pool
, class, &cc
))
2304 putback_zspage(class, dst_zspage
);
2307 /* Stop if we couldn't find slot */
2308 if (dst_zspage
== NULL
)
2311 putback_zspage(class, dst_zspage
);
2312 if (putback_zspage(class, src_zspage
) == ZS_EMPTY
) {
2313 free_zspage(pool
, class, src_zspage
);
2314 pool
->stats
.pages_compacted
+= class->pages_per_zspage
;
2316 spin_unlock(&class->lock
);
2318 spin_lock(&class->lock
);
2322 putback_zspage(class, src_zspage
);
2324 spin_unlock(&class->lock
);
2327 unsigned long zs_compact(struct zs_pool
*pool
)
2330 struct size_class
*class;
2332 for (i
= ZS_SIZE_CLASSES
- 1; i
>= 0; i
--) {
2333 class = pool
->size_class
[i
];
2336 if (class->index
!= i
)
2338 __zs_compact(pool
, class);
2341 return pool
->stats
.pages_compacted
;
2343 EXPORT_SYMBOL_GPL(zs_compact
);
2345 void zs_pool_stats(struct zs_pool
*pool
, struct zs_pool_stats
*stats
)
2347 memcpy(stats
, &pool
->stats
, sizeof(struct zs_pool_stats
));
2349 EXPORT_SYMBOL_GPL(zs_pool_stats
);
2351 static unsigned long zs_shrinker_scan(struct shrinker
*shrinker
,
2352 struct shrink_control
*sc
)
2354 unsigned long pages_freed
;
2355 struct zs_pool
*pool
= container_of(shrinker
, struct zs_pool
,
2358 pages_freed
= pool
->stats
.pages_compacted
;
2360 * Compact classes and calculate compaction delta.
2361 * Can run concurrently with a manually triggered
2362 * (by user) compaction.
2364 pages_freed
= zs_compact(pool
) - pages_freed
;
2366 return pages_freed
? pages_freed
: SHRINK_STOP
;
2369 static unsigned long zs_shrinker_count(struct shrinker
*shrinker
,
2370 struct shrink_control
*sc
)
2373 struct size_class
*class;
2374 unsigned long pages_to_free
= 0;
2375 struct zs_pool
*pool
= container_of(shrinker
, struct zs_pool
,
2378 for (i
= ZS_SIZE_CLASSES
- 1; i
>= 0; i
--) {
2379 class = pool
->size_class
[i
];
2382 if (class->index
!= i
)
2385 pages_to_free
+= zs_can_compact(class);
2388 return pages_to_free
;
2391 static void zs_unregister_shrinker(struct zs_pool
*pool
)
2393 if (pool
->shrinker_enabled
) {
2394 unregister_shrinker(&pool
->shrinker
);
2395 pool
->shrinker_enabled
= false;
2399 static int zs_register_shrinker(struct zs_pool
*pool
)
2401 pool
->shrinker
.scan_objects
= zs_shrinker_scan
;
2402 pool
->shrinker
.count_objects
= zs_shrinker_count
;
2403 pool
->shrinker
.batch
= 0;
2404 pool
->shrinker
.seeks
= DEFAULT_SEEKS
;
2406 return register_shrinker(&pool
->shrinker
);
2410 * zs_create_pool - Creates an allocation pool to work from.
2411 * @name: pool name to be created
2413 * This function must be called before anything when using
2414 * the zsmalloc allocator.
2416 * On success, a pointer to the newly created pool is returned,
2419 struct zs_pool
*zs_create_pool(const char *name
)
2422 struct zs_pool
*pool
;
2423 struct size_class
*prev_class
= NULL
;
2425 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
2429 init_deferred_free(pool
);
2431 pool
->name
= kstrdup(name
, GFP_KERNEL
);
2435 #ifdef CONFIG_COMPACTION
2436 init_waitqueue_head(&pool
->migration_wait
);
2439 if (create_cache(pool
))
2443 * Iterate reversely, because, size of size_class that we want to use
2444 * for merging should be larger or equal to current size.
2446 for (i
= ZS_SIZE_CLASSES
- 1; i
>= 0; i
--) {
2448 int pages_per_zspage
;
2449 int objs_per_zspage
;
2450 struct size_class
*class;
2453 size
= ZS_MIN_ALLOC_SIZE
+ i
* ZS_SIZE_CLASS_DELTA
;
2454 if (size
> ZS_MAX_ALLOC_SIZE
)
2455 size
= ZS_MAX_ALLOC_SIZE
;
2456 pages_per_zspage
= get_pages_per_zspage(size
);
2457 objs_per_zspage
= pages_per_zspage
* PAGE_SIZE
/ size
;
2460 * size_class is used for normal zsmalloc operation such
2461 * as alloc/free for that size. Although it is natural that we
2462 * have one size_class for each size, there is a chance that we
2463 * can get more memory utilization if we use one size_class for
2464 * many different sizes whose size_class have same
2465 * characteristics. So, we makes size_class point to
2466 * previous size_class if possible.
2469 if (can_merge(prev_class
, pages_per_zspage
, objs_per_zspage
)) {
2470 pool
->size_class
[i
] = prev_class
;
2475 class = kzalloc(sizeof(struct size_class
), GFP_KERNEL
);
2481 class->pages_per_zspage
= pages_per_zspage
;
2482 class->objs_per_zspage
= objs_per_zspage
;
2483 spin_lock_init(&class->lock
);
2484 pool
->size_class
[i
] = class;
2485 for (fullness
= ZS_EMPTY
; fullness
< NR_ZS_FULLNESS
;
2487 INIT_LIST_HEAD(&class->fullness_list
[fullness
]);
2492 /* debug only, don't abort if it fails */
2493 zs_pool_stat_create(pool
, name
);
2495 if (zs_register_migration(pool
))
2499 * Not critical, we still can use the pool
2500 * and user can trigger compaction manually.
2502 if (zs_register_shrinker(pool
) == 0)
2503 pool
->shrinker_enabled
= true;
2507 zs_destroy_pool(pool
);
2510 EXPORT_SYMBOL_GPL(zs_create_pool
);
2512 void zs_destroy_pool(struct zs_pool
*pool
)
2516 zs_unregister_shrinker(pool
);
2517 zs_unregister_migration(pool
);
2518 zs_pool_stat_destroy(pool
);
2520 for (i
= 0; i
< ZS_SIZE_CLASSES
; i
++) {
2522 struct size_class
*class = pool
->size_class
[i
];
2527 if (class->index
!= i
)
2530 for (fg
= ZS_EMPTY
; fg
< NR_ZS_FULLNESS
; fg
++) {
2531 if (!list_empty(&class->fullness_list
[fg
])) {
2532 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
2539 destroy_cache(pool
);
2543 EXPORT_SYMBOL_GPL(zs_destroy_pool
);
2545 static int __init
zs_init(void)
2549 ret
= zsmalloc_mount();
2553 ret
= cpuhp_setup_state(CPUHP_MM_ZS_PREPARE
, "mm/zsmalloc:prepare",
2554 zs_cpu_prepare
, zs_cpu_dead
);
2559 zpool_register_driver(&zs_zpool_driver
);
2572 static void __exit
zs_exit(void)
2575 zpool_unregister_driver(&zs_zpool_driver
);
2578 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE
);
2583 module_init(zs_init
);
2584 module_exit(zs_exit
);
2586 MODULE_LICENSE("Dual BSD/GPL");
2587 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");