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
24 * Usage of struct page flags:
25 * PG_private: identifies the first component page
26 * PG_private2: identifies the last component page
27 * PG_owner_priv_1: indentifies the huge component page
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/bitops.h>
35 #include <linux/errno.h>
36 #include <linux/highmem.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <asm/tlbflush.h>
40 #include <asm/pgtable.h>
41 #include <linux/cpumask.h>
42 #include <linux/cpu.h>
43 #include <linux/vmalloc.h>
44 #include <linux/preempt.h>
45 #include <linux/spinlock.h>
46 #include <linux/types.h>
47 #include <linux/debugfs.h>
48 #include <linux/zsmalloc.h>
49 #include <linux/zpool.h>
50 #include <linux/mount.h>
51 #include <linux/compaction.h>
52 #include <linux/pagemap.h>
53 #include <linux/swap.h>
54 #include <linux/jiffies.h>
56 #define ZSPAGE_MAGIC 0x58
59 * This must be power of 2 and greater than of equal to sizeof(link_free).
60 * These two conditions ensure that any 'struct link_free' itself doesn't
61 * span more than 1 page which avoids complex case of mapping 2 pages simply
62 * to restore link_free pointer values.
67 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
68 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
70 #define ZS_MAX_ZSPAGE_ORDER 2
71 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
73 #define ZS_HANDLE_SIZE (sizeof(unsigned long))
76 * Object location (<PFN>, <obj_idx>) is encoded as
77 * as single (unsigned long) handle value.
79 * Note that object index <obj_idx> starts from 0.
81 * This is made more complicated by various memory models and PAE.
84 #ifndef MAX_PHYSMEM_BITS
85 #ifdef CONFIG_HIGHMEM64G
86 #define MAX_PHYSMEM_BITS 36
87 #else /* !CONFIG_HIGHMEM64G */
89 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
92 #define MAX_PHYSMEM_BITS BITS_PER_LONG
95 #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
98 * Memory for allocating for handle keeps object position by
99 * encoding <page, obj_idx> and the encoded value has a room
100 * in least bit(ie, look at obj_to_location).
101 * We use the bit to synchronize between object access by
102 * user and migration.
104 #define HANDLE_PIN_BIT 0
107 * Head in allocated object should have OBJ_ALLOCATED_TAG
108 * to identify the object was allocated or not.
109 * It's okay to add the status bit in the least bit because
110 * header keeps handle which is 4byte-aligned address so we
111 * have room for two bit at least.
113 #define OBJ_ALLOCATED_TAG 1
114 #define OBJ_TAG_BITS 1
115 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
116 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
118 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
119 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
120 #define ZS_MIN_ALLOC_SIZE \
121 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
122 /* each chunk includes extra space to keep handle */
123 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
126 * On systems with 4K page size, this gives 255 size classes! There is a
128 * - Large number of size classes is potentially wasteful as free page are
129 * spread across these classes
130 * - Small number of size classes causes large internal fragmentation
131 * - Probably its better to use specific size classes (empirically
132 * determined). NOTE: all those class sizes must be set as multiple of
133 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
135 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
138 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
141 * We do not maintain any list for completely empty or full pages
143 enum fullness_group
{
161 struct zs_size_stat
{
162 unsigned long objs
[NR_ZS_STAT_TYPE
];
165 #ifdef CONFIG_ZSMALLOC_STAT
166 static struct dentry
*zs_stat_root
;
169 #ifdef CONFIG_COMPACTION
170 static struct vfsmount
*zsmalloc_mnt
;
173 #ifdef CONFIG_ZSWAP_MIGRATION_SUPPORT
174 static int zs_page_migration_enabled
= 1;
176 static int zs_page_migration_enabled
;
180 * number of size_classes
182 static int zs_size_classes
;
185 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
187 * n = number of allocated objects
188 * N = total number of objects zspage can store
189 * f = fullness_threshold_frac
191 * Similarly, we assign zspage to:
192 * ZS_ALMOST_FULL when n > N / f
193 * ZS_EMPTY when n == 0
194 * ZS_FULL when n == N
196 * (see: fix_fullness_group())
198 static const int fullness_threshold_frac
= 4;
199 static size_t huge_class_size
;
203 struct list_head fullness_list
[NR_ZS_FULLNESS
];
205 * Size of objects stored in this class. Must be multiple
210 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
211 int pages_per_zspage
;
214 struct zs_size_stat stats
;
217 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
218 static void SetPageHugeObject(struct page
*page
)
220 SetPageOwnerPriv1(page
);
223 static void ClearPageHugeObject(struct page
*page
)
225 ClearPageOwnerPriv1(page
);
228 static int PageHugeObject(struct page
*page
)
230 return PageOwnerPriv1(page
);
234 * Placed within free objects to form a singly linked list.
235 * For every zspage, zspage->freeobj gives head of this list.
237 * This must be power of 2 and less than or equal to ZS_ALIGN
243 * It's valid for non-allocated object
247 * Handle of allocated object.
249 unsigned long handle
;
256 struct size_class
**size_class
;
257 struct kmem_cache
*handle_cachep
;
258 struct kmem_cache
*zspage_cachep
;
260 atomic_long_t pages_allocated
;
262 struct zs_pool_stats stats
;
264 /* Compact classes */
265 struct shrinker shrinker
;
267 * To signify that register_shrinker() was successful
268 * and unregister_shrinker() will not Oops.
270 bool shrinker_enabled
;
271 #ifdef CONFIG_ZSMALLOC_STAT
272 struct dentry
*stat_dentry
;
274 #ifdef CONFIG_COMPACTION
276 struct work_struct free_work
;
281 * A zspage's class index and fullness group
282 * are encoded in its (first)page->mapping
284 #define FULLNESS_BITS 2
286 #define ISOLATED_BITS 3
287 #define MAGIC_VAL_BITS 8
291 unsigned int fullness
:FULLNESS_BITS
;
292 unsigned int class:CLASS_BITS
;
293 unsigned int isolated
:ISOLATED_BITS
;
294 unsigned int magic
:MAGIC_VAL_BITS
;
297 unsigned int freeobj
;
298 struct page
*first_page
;
299 struct list_head list
; /* fullness list */
300 #ifdef CONFIG_COMPACTION
305 struct mapping_area
{
306 #ifdef CONFIG_PGTABLE_MAPPING
307 struct vm_struct
*vm
; /* vm area for mapping object that span pages */
309 char *vm_buf
; /* copy buffer for objects that span pages */
311 char *vm_addr
; /* address of kmap_atomic()'ed pages */
312 enum zs_mapmode vm_mm
; /* mapping mode */
315 #ifdef CONFIG_COMPACTION
316 static int zs_register_migration(struct zs_pool
*pool
);
317 static void zs_unregister_migration(struct zs_pool
*pool
);
318 static void migrate_lock_init(struct zspage
*zspage
);
319 static void migrate_read_lock(struct zspage
*zspage
);
320 static void migrate_read_unlock(struct zspage
*zspage
);
321 static void kick_deferred_free(struct zs_pool
*pool
);
322 static void init_deferred_free(struct zs_pool
*pool
);
323 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
);
325 static int zsmalloc_mount(void) { return 0; }
326 static void zsmalloc_unmount(void) {}
327 static int zs_register_migration(struct zs_pool
*pool
) { return 0; }
328 static void zs_unregister_migration(struct zs_pool
*pool
) {}
329 static void migrate_lock_init(struct zspage
*zspage
) {}
330 static void migrate_read_lock(struct zspage
*zspage
) {}
331 static void migrate_read_unlock(struct zspage
*zspage
) {}
332 static void kick_deferred_free(struct zs_pool
*pool
) {}
333 static void init_deferred_free(struct zs_pool
*pool
) {}
334 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
) {}
337 static int create_cache(struct zs_pool
*pool
)
339 pool
->handle_cachep
= kmem_cache_create("zs_handle", ZS_HANDLE_SIZE
,
341 if (!pool
->handle_cachep
)
344 pool
->zspage_cachep
= kmem_cache_create("zspage", sizeof(struct zspage
),
346 if (!pool
->zspage_cachep
) {
347 kmem_cache_destroy(pool
->handle_cachep
);
348 pool
->handle_cachep
= NULL
;
355 static void destroy_cache(struct zs_pool
*pool
)
357 kmem_cache_destroy(pool
->zspage_cachep
);
360 static unsigned long cache_alloc_handle(struct zs_pool
*pool
, gfp_t gfp
)
362 return (unsigned long)kmem_cache_alloc(pool
->handle_cachep
,
363 gfp
& ~(__GFP_HIGHMEM
|__GFP_MOVABLE
));
366 static void cache_free_handle(struct zs_pool
*pool
, unsigned long handle
)
368 kmem_cache_free(pool
->handle_cachep
, (void *)handle
);
371 static struct zspage
*cache_alloc_zspage(struct zs_pool
*pool
, gfp_t flags
)
373 return kmem_cache_alloc(pool
->zspage_cachep
,
374 flags
& ~(__GFP_HIGHMEM
|__GFP_MOVABLE
));
377 static void cache_free_zspage(struct zs_pool
*pool
, struct zspage
*zspage
)
379 kmem_cache_free(pool
->zspage_cachep
, zspage
);
382 static void record_obj(unsigned long handle
, unsigned long obj
)
385 * lsb of @obj represents handle lock while other bits
386 * represent object value the handle is pointing so
387 * updating shouldn't do store tearing.
389 WRITE_ONCE(*(unsigned long *)handle
, obj
);
396 static void *zs_zpool_create(const char *name
, gfp_t gfp
,
397 const struct zpool_ops
*zpool_ops
,
401 * Ignore global gfp flags: zs_malloc() may be invoked from
402 * different contexts and its caller must provide a valid
405 return zs_create_pool(name
);
408 static void zs_zpool_destroy(void *pool
)
410 zs_destroy_pool(pool
);
413 static int zs_zpool_malloc(void *pool
, size_t size
, gfp_t gfp
,
414 unsigned long *handle
)
416 *handle
= zs_malloc(pool
, size
, gfp
);
417 return *handle
? 0 : -1;
419 static void zs_zpool_free(void *pool
, unsigned long handle
)
421 zs_free(pool
, handle
);
424 static int zs_zpool_shrink(void *pool
, unsigned int pages
,
425 unsigned int *reclaimed
)
430 static void *zs_zpool_map(void *pool
, unsigned long handle
,
431 enum zpool_mapmode mm
)
433 enum zs_mapmode zs_mm
;
439 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
440 case ZPOOL_MM_RO_NOWAIT
:
441 zs_mm
= ZS_MM_RO_NOWAIT
;
447 case ZPOOL_MM_RW
: /* fallthru */
453 return zs_map_object(pool
, handle
, zs_mm
);
455 static void zs_zpool_unmap(void *pool
, unsigned long handle
)
457 zs_unmap_object(pool
, handle
);
460 static u64
zs_zpool_total_size(void *pool
)
462 return zs_get_total_pages(pool
) << PAGE_SHIFT
;
465 static struct zpool_driver zs_zpool_driver
= {
467 .owner
= THIS_MODULE
,
468 .create
= zs_zpool_create
,
469 .destroy
= zs_zpool_destroy
,
470 .malloc
= zs_zpool_malloc
,
471 .free
= zs_zpool_free
,
472 .shrink
= zs_zpool_shrink
,
474 .unmap
= zs_zpool_unmap
,
475 .total_size
= zs_zpool_total_size
,
478 MODULE_ALIAS("zpool-zsmalloc");
479 #endif /* CONFIG_ZPOOL */
481 static unsigned int get_maxobj_per_zspage(int size
, int pages_per_zspage
)
483 return pages_per_zspage
* PAGE_SIZE
/ size
;
486 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
487 static DEFINE_PER_CPU(struct mapping_area
, zs_map_area
);
489 static bool is_zspage_isolated(struct zspage
*zspage
)
491 return zspage
->isolated
;
494 static __maybe_unused
int is_first_page(struct page
*page
)
496 return PagePrivate(page
);
499 /* Protected by class->lock */
500 static inline int get_zspage_inuse(struct zspage
*zspage
)
502 return zspage
->inuse
;
505 static inline void set_zspage_inuse(struct zspage
*zspage
, int val
)
510 static inline void mod_zspage_inuse(struct zspage
*zspage
, int val
)
512 zspage
->inuse
+= val
;
515 static inline struct page
*get_first_page(struct zspage
*zspage
)
517 struct page
*first_page
= zspage
->first_page
;
519 VM_BUG_ON_PAGE(!is_first_page(first_page
), first_page
);
523 static inline int get_first_obj_offset(struct page
*page
)
528 static inline void set_first_obj_offset(struct page
*page
, int offset
)
530 page
->units
= offset
;
533 static inline unsigned int get_freeobj(struct zspage
*zspage
)
535 return zspage
->freeobj
;
538 static inline void set_freeobj(struct zspage
*zspage
, unsigned int obj
)
540 zspage
->freeobj
= obj
;
543 static void get_zspage_mapping(struct zspage
*zspage
,
544 unsigned int *class_idx
,
545 enum fullness_group
*fullness
)
547 VM_BUG_ON(zspage
->magic
!= ZSPAGE_MAGIC
);
549 *fullness
= zspage
->fullness
;
550 *class_idx
= zspage
->class;
553 static void set_zspage_mapping(struct zspage
*zspage
,
554 unsigned int class_idx
,
555 enum fullness_group fullness
)
557 zspage
->class = class_idx
;
558 zspage
->fullness
= fullness
;
562 * zsmalloc divides the pool into various size classes where each
563 * class maintains a list of zspages where each zspage is divided
564 * into equal sized chunks. Each allocation falls into one of these
565 * classes depending on its size. This function returns index of the
566 * size class which has chunk size big enough to hold the give size.
568 static int get_size_class_index(int size
)
572 if (likely(size
> ZS_MIN_ALLOC_SIZE
))
573 idx
= DIV_ROUND_UP(size
- ZS_MIN_ALLOC_SIZE
,
574 ZS_SIZE_CLASS_DELTA
);
576 return min(zs_size_classes
- 1, idx
);
579 /* type can be of enum type zs_stat_type or fullness_group */
580 static inline void zs_stat_inc(struct size_class
*class,
581 int type
, unsigned long cnt
)
583 class->stats
.objs
[type
] += cnt
;
586 /* type can be of enum type zs_stat_type or fullness_group */
587 static inline void zs_stat_dec(struct size_class
*class,
588 int type
, unsigned long cnt
)
590 class->stats
.objs
[type
] -= cnt
;
593 /* type can be of enum type zs_stat_type or fullness_group */
594 static inline unsigned long zs_stat_get(struct size_class
*class,
597 return class->stats
.objs
[type
];
600 #ifdef CONFIG_ZSMALLOC_STAT
602 static int __init
zs_stat_init(void)
604 if (!debugfs_initialized())
607 zs_stat_root
= debugfs_create_dir("zsmalloc", NULL
);
614 static void __exit
zs_stat_exit(void)
616 debugfs_remove_recursive(zs_stat_root
);
619 static unsigned long zs_can_compact(struct size_class
*class);
621 static int zs_stats_size_show(struct seq_file
*s
, void *v
)
624 struct zs_pool
*pool
= s
->private;
625 struct size_class
*class;
627 unsigned long class_almost_full
, class_almost_empty
;
628 unsigned long obj_allocated
, obj_used
, pages_used
, freeable
;
629 unsigned long total_class_almost_full
= 0, total_class_almost_empty
= 0;
630 unsigned long total_objs
= 0, total_used_objs
= 0, total_pages
= 0;
631 unsigned long total_freeable
= 0;
633 seq_printf(s
, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
634 "class", "size", "almost_full", "almost_empty",
635 "obj_allocated", "obj_used", "pages_used",
636 "pages_per_zspage", "freeable");
638 for (i
= 0; i
< zs_size_classes
; i
++) {
639 class = pool
->size_class
[i
];
641 if (class->index
!= i
)
644 spin_lock(&class->lock
);
645 class_almost_full
= zs_stat_get(class, CLASS_ALMOST_FULL
);
646 class_almost_empty
= zs_stat_get(class, CLASS_ALMOST_EMPTY
);
647 obj_allocated
= zs_stat_get(class, OBJ_ALLOCATED
);
648 obj_used
= zs_stat_get(class, OBJ_USED
);
649 freeable
= zs_can_compact(class);
650 spin_unlock(&class->lock
);
652 objs_per_zspage
= get_maxobj_per_zspage(class->size
,
653 class->pages_per_zspage
);
654 pages_used
= obj_allocated
/ objs_per_zspage
*
655 class->pages_per_zspage
;
657 seq_printf(s
, " %5u %5u %11lu %12lu %13lu"
658 " %10lu %10lu %16d %8lu\n",
659 i
, class->size
, class_almost_full
, class_almost_empty
,
660 obj_allocated
, obj_used
, pages_used
,
661 class->pages_per_zspage
, freeable
);
663 total_class_almost_full
+= class_almost_full
;
664 total_class_almost_empty
+= class_almost_empty
;
665 total_objs
+= obj_allocated
;
666 total_used_objs
+= obj_used
;
667 total_pages
+= pages_used
;
668 total_freeable
+= freeable
;
672 seq_printf(s
, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
673 "Total", "", total_class_almost_full
,
674 total_class_almost_empty
, total_objs
,
675 total_used_objs
, total_pages
, "", total_freeable
);
680 static int zs_stats_size_open(struct inode
*inode
, struct file
*file
)
682 return single_open(file
, zs_stats_size_show
, inode
->i_private
);
685 static const struct file_operations zs_stat_size_ops
= {
686 .open
= zs_stats_size_open
,
689 .release
= single_release
,
692 static int zs_pool_stat_create(struct zs_pool
*pool
, const char *name
)
694 struct dentry
*entry
;
699 entry
= debugfs_create_dir(name
, zs_stat_root
);
701 pr_warn("debugfs dir <%s> creation failed\n", name
);
704 pool
->stat_dentry
= entry
;
706 entry
= debugfs_create_file("classes", S_IFREG
| S_IRUGO
,
707 pool
->stat_dentry
, pool
, &zs_stat_size_ops
);
709 pr_warn("%s: debugfs file entry <%s> creation failed\n",
717 static void zs_pool_stat_destroy(struct zs_pool
*pool
)
719 debugfs_remove_recursive(pool
->stat_dentry
);
722 #else /* CONFIG_ZSMALLOC_STAT */
723 static int __init
zs_stat_init(void)
728 static void __exit
zs_stat_exit(void)
732 static inline int zs_pool_stat_create(struct zs_pool
*pool
, const char *name
)
737 static inline void zs_pool_stat_destroy(struct zs_pool
*pool
)
745 * For each size class, zspages are divided into different groups
746 * depending on how "full" they are. This was done so that we could
747 * easily find empty or nearly empty zspages when we try to shrink
748 * the pool (not yet implemented). This function returns fullness
749 * status of the given page.
751 static enum fullness_group
get_fullness_group(struct size_class
*class,
752 struct zspage
*zspage
)
754 int inuse
, objs_per_zspage
;
755 enum fullness_group fg
;
757 inuse
= get_zspage_inuse(zspage
);
758 objs_per_zspage
= class->objs_per_zspage
;
762 else if (inuse
== objs_per_zspage
)
764 else if (inuse
<= 3 * objs_per_zspage
/ fullness_threshold_frac
)
765 fg
= ZS_ALMOST_EMPTY
;
773 * Each size class maintains various freelists and zspages are assigned
774 * to one of these freelists based on the number of live objects they
775 * have. This functions inserts the given zspage into the freelist
776 * identified by <class, fullness_group>.
778 static void insert_zspage(struct size_class
*class,
779 struct zspage
*zspage
,
780 enum fullness_group fullness
)
784 zs_stat_inc(class, fullness
, 1);
785 head
= list_first_entry_or_null(&class->fullness_list
[fullness
],
786 struct zspage
, list
);
788 * We want to see more ZS_FULL pages and less almost empty/full.
789 * Put pages with higher ->inuse first.
792 if (get_zspage_inuse(zspage
) < get_zspage_inuse(head
)) {
793 list_add(&zspage
->list
, &head
->list
);
797 list_add(&zspage
->list
, &class->fullness_list
[fullness
]);
801 * This function removes the given zspage from the freelist identified
802 * by <class, fullness_group>.
804 static void remove_zspage(struct size_class
*class,
805 struct zspage
*zspage
,
806 enum fullness_group fullness
)
808 VM_BUG_ON(list_empty(&class->fullness_list
[fullness
]));
809 VM_BUG_ON(is_zspage_isolated(zspage
));
811 list_del_init(&zspage
->list
);
812 zs_stat_dec(class, fullness
, 1);
816 * Each size class maintains zspages in different fullness groups depending
817 * on the number of live objects they contain. When allocating or freeing
818 * objects, the fullness status of the page can change, say, from ALMOST_FULL
819 * to ALMOST_EMPTY when freeing an object. This function checks if such
820 * a status change has occurred for the given page and accordingly moves the
821 * page from the freelist of the old fullness group to that of the new
824 static enum fullness_group
fix_fullness_group(struct size_class
*class,
825 struct zspage
*zspage
)
828 enum fullness_group currfg
, newfg
;
830 get_zspage_mapping(zspage
, &class_idx
, &currfg
);
831 newfg
= get_fullness_group(class, zspage
);
835 if (!is_zspage_isolated(zspage
)) {
836 remove_zspage(class, zspage
, currfg
);
837 insert_zspage(class, zspage
, newfg
);
840 set_zspage_mapping(zspage
, class_idx
, newfg
);
847 * We have to decide on how many pages to link together
848 * to form a zspage for each size class. This is important
849 * to reduce wastage due to unusable space left at end of
850 * each zspage which is given as:
851 * wastage = Zp % class_size
852 * usage = Zp - wastage
853 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
855 * For example, for size class of 3/8 * PAGE_SIZE, we should
856 * link together 3 PAGE_SIZE sized pages to form a zspage
857 * since then we can perfectly fit in 8 such objects.
859 static int get_pages_per_zspage(int class_size
)
861 int i
, max_usedpc
= 0;
862 /* zspage order which gives maximum used size per KB */
863 int max_usedpc_order
= 1;
865 for (i
= 1; i
<= ZS_MAX_PAGES_PER_ZSPAGE
; i
++) {
869 zspage_size
= i
* PAGE_SIZE
;
870 waste
= zspage_size
% class_size
;
871 usedpc
= (zspage_size
- waste
) * 100 / zspage_size
;
873 if (usedpc
> max_usedpc
) {
875 max_usedpc_order
= i
;
879 return max_usedpc_order
;
882 static struct zspage
*get_zspage(struct page
*page
)
884 struct zspage
*zspage
= (struct zspage
*)page
->private;
886 VM_BUG_ON(zspage
->magic
!= ZSPAGE_MAGIC
);
890 static struct page
*get_next_page(struct page
*page
)
892 if (unlikely(PageHugeObject(page
)))
895 return page
->freelist
;
899 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
900 * @page: page object resides in zspage
901 * @obj_idx: object index
903 static void obj_to_location(unsigned long obj
, struct page
**page
,
904 unsigned int *obj_idx
)
906 obj
>>= OBJ_TAG_BITS
;
907 *page
= pfn_to_page(obj
>> OBJ_INDEX_BITS
);
908 *obj_idx
= (obj
& OBJ_INDEX_MASK
);
912 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
913 * @page: page object resides in zspage
914 * @obj_idx: object index
916 static unsigned long location_to_obj(struct page
*page
, unsigned int obj_idx
)
920 obj
= page_to_pfn(page
) << OBJ_INDEX_BITS
;
921 obj
|= obj_idx
& OBJ_INDEX_MASK
;
922 obj
<<= OBJ_TAG_BITS
;
927 static unsigned long handle_to_obj(unsigned long handle
)
929 return *(unsigned long *)handle
;
932 static unsigned long obj_to_head(struct page
*page
, void *obj
)
934 if (unlikely(PageHugeObject(page
))) {
935 VM_BUG_ON_PAGE(!is_first_page(page
), page
);
938 return *(unsigned long *)obj
;
941 static inline int testpin_tag(unsigned long handle
)
943 return bit_spin_is_locked(HANDLE_PIN_BIT
, (unsigned long *)handle
);
946 static inline int trypin_tag(unsigned long handle
)
948 return bit_spin_trylock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
951 static void pin_tag(unsigned long handle
)
953 bit_spin_lock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
956 static void unpin_tag(unsigned long handle
)
958 bit_spin_unlock(HANDLE_PIN_BIT
, (unsigned long *)handle
);
961 static void reset_page(struct page
*page
)
963 __ClearPageMovable(page
);
964 clear_bit(PG_private
, &page
->flags
);
965 clear_bit(PG_private_2
, &page
->flags
);
966 set_page_private(page
, 0);
967 page_mapcount_reset(page
);
968 ClearPageHugeObject(page
);
969 page
->freelist
= NULL
;
973 * To prevent zspage destroy during migration, zspage freeing should
974 * hold locks of all pages in the zspage.
976 void lock_zspage(struct zspage
*zspage
)
978 struct page
*page
= get_first_page(zspage
);
982 } while ((page
= get_next_page(page
)) != NULL
);
985 int trylock_zspage(struct zspage
*zspage
)
987 struct page
*cursor
, *fail
;
989 for (cursor
= get_first_page(zspage
); cursor
!= NULL
; cursor
=
990 get_next_page(cursor
)) {
991 if (!trylock_page(cursor
)) {
999 for (cursor
= get_first_page(zspage
); cursor
!= fail
; cursor
=
1000 get_next_page(cursor
))
1001 unlock_page(cursor
);
1006 static void __free_zspage(struct zs_pool
*pool
, struct size_class
*class,
1007 struct zspage
*zspage
)
1009 struct page
*page
, *next
;
1010 enum fullness_group fg
;
1011 unsigned int class_idx
;
1013 get_zspage_mapping(zspage
, &class_idx
, &fg
);
1015 assert_spin_locked(&class->lock
);
1017 VM_BUG_ON(get_zspage_inuse(zspage
));
1018 VM_BUG_ON(fg
!= ZS_EMPTY
);
1020 next
= page
= get_first_page(zspage
);
1022 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
1023 next
= get_next_page(page
);
1028 } while (page
!= NULL
);
1030 cache_free_zspage(pool
, zspage
);
1032 zs_stat_dec(class, OBJ_ALLOCATED
, get_maxobj_per_zspage(
1033 class->size
, class->pages_per_zspage
));
1034 atomic_long_sub(class->pages_per_zspage
,
1035 &pool
->pages_allocated
);
1038 static void free_zspage(struct zs_pool
*pool
, struct size_class
*class,
1039 struct zspage
*zspage
)
1041 VM_BUG_ON(get_zspage_inuse(zspage
));
1042 VM_BUG_ON(list_empty(&zspage
->list
));
1044 if (!trylock_zspage(zspage
)) {
1045 kick_deferred_free(pool
);
1049 remove_zspage(class, zspage
, ZS_EMPTY
);
1050 __free_zspage(pool
, class, zspage
);
1053 /* Initialize a newly allocated zspage */
1054 static void init_zspage(struct size_class
*class, struct zspage
*zspage
)
1056 unsigned int freeobj
= 1;
1057 unsigned long off
= 0;
1058 struct page
*page
= get_first_page(zspage
);
1061 struct page
*next_page
;
1062 struct link_free
*link
;
1065 set_first_obj_offset(page
, off
);
1067 vaddr
= kmap_atomic(page
);
1068 link
= (struct link_free
*)vaddr
+ off
/ sizeof(*link
);
1070 while ((off
+= class->size
) < PAGE_SIZE
) {
1071 link
->next
= freeobj
++ << OBJ_ALLOCATED_TAG
;
1072 link
+= class->size
/ sizeof(*link
);
1076 * We now come to the last (full or partial) object on this
1077 * page, which must point to the first object on the next
1080 next_page
= get_next_page(page
);
1082 link
->next
= freeobj
++ << OBJ_ALLOCATED_TAG
;
1085 * Reset OBJ_ALLOCATED_TAG bit to last link to tell
1086 * whether it's allocated object or not.
1088 link
->next
= -1 << OBJ_ALLOCATED_TAG
;
1090 kunmap_atomic(vaddr
);
1095 set_freeobj(zspage
, 0);
1098 static void create_page_chain(struct size_class
*class, struct zspage
*zspage
,
1099 struct page
*pages
[])
1103 struct page
*prev_page
= NULL
;
1104 int nr_pages
= class->pages_per_zspage
;
1107 * Allocate individual pages and link them together as:
1108 * 1. all pages are linked together using page->freelist
1109 * 2. each sub-page point to zspage using page->private
1111 * we set PG_private to identify the first page (i.e. no other sub-page
1112 * has this flag set) and PG_private_2 to identify the last page.
1114 for (i
= 0; i
< nr_pages
; i
++) {
1116 set_page_private(page
, (unsigned long)zspage
);
1117 page
->freelist
= NULL
;
1119 zspage
->first_page
= page
;
1120 SetPagePrivate(page
);
1121 if (unlikely(class->objs_per_zspage
== 1 &&
1122 class->pages_per_zspage
== 1))
1123 SetPageHugeObject(page
);
1125 prev_page
->freelist
= page
;
1127 if (i
== nr_pages
- 1)
1128 SetPagePrivate2(page
);
1134 * Allocate a zspage for the given size class
1136 static struct zspage
*alloc_zspage(struct zs_pool
*pool
,
1137 struct size_class
*class,
1141 struct page
*pages
[ZS_MAX_PAGES_PER_ZSPAGE
];
1142 struct zspage
*zspage
= cache_alloc_zspage(pool
, gfp
);
1147 memset(zspage
, 0, sizeof(struct zspage
));
1148 zspage
->magic
= ZSPAGE_MAGIC
;
1149 migrate_lock_init(zspage
);
1151 for (i
= 0; i
< class->pages_per_zspage
; i
++) {
1154 page
= alloc_page(gfp
);
1157 __free_page(pages
[i
]);
1158 cache_free_zspage(pool
, zspage
);
1164 create_page_chain(class, zspage
, pages
);
1165 init_zspage(class, zspage
);
1170 static struct zspage
*find_get_zspage(struct size_class
*class)
1173 struct zspage
*zspage
;
1175 for (i
= ZS_ALMOST_FULL
; i
>= ZS_EMPTY
; i
--) {
1176 zspage
= list_first_entry_or_null(&class->fullness_list
[i
],
1177 struct zspage
, list
);
1185 #ifdef CONFIG_PGTABLE_MAPPING
1186 static inline int __zs_cpu_up(struct mapping_area
*area
)
1189 * Make sure we don't leak memory if a cpu UP notification
1190 * and zs_init() race and both call zs_cpu_up() on the same cpu
1194 area
->vm
= alloc_vm_area(PAGE_SIZE
* 2, NULL
);
1200 static inline void __zs_cpu_down(struct mapping_area
*area
)
1203 free_vm_area(area
->vm
);
1207 static inline void *__zs_map_object(struct mapping_area
*area
,
1208 struct page
*pages
[2], int off
, int size
)
1210 BUG_ON(map_vm_area(area
->vm
, PAGE_KERNEL
, pages
));
1211 area
->vm_addr
= area
->vm
->addr
;
1212 return area
->vm_addr
+ off
;
1215 static inline void __zs_unmap_object(struct mapping_area
*area
,
1216 struct page
*pages
[2], int off
, int size
)
1218 unsigned long addr
= (unsigned long)area
->vm_addr
;
1220 unmap_kernel_range(addr
, PAGE_SIZE
* 2);
1223 #else /* CONFIG_PGTABLE_MAPPING */
1225 static inline int __zs_cpu_up(struct mapping_area
*area
)
1228 * Make sure we don't leak memory if a cpu UP notification
1229 * and zs_init() race and both call zs_cpu_up() on the same cpu
1233 area
->vm_buf
= kmalloc(ZS_MAX_ALLOC_SIZE
, GFP_KERNEL
);
1239 static inline void __zs_cpu_down(struct mapping_area
*area
)
1241 kfree(area
->vm_buf
);
1242 area
->vm_buf
= NULL
;
1245 static void *__zs_map_object(struct mapping_area
*area
,
1246 struct page
*pages
[2], int off
, int size
)
1250 char *buf
= area
->vm_buf
;
1252 /* disable page faults to match kmap_atomic() return conditions */
1253 pagefault_disable();
1255 /* no read fastpath */
1256 if (area
->vm_mm
== ZS_MM_WO
)
1259 sizes
[0] = PAGE_SIZE
- off
;
1260 sizes
[1] = size
- sizes
[0];
1262 /* copy object to per-cpu buffer */
1263 addr
= kmap_atomic(pages
[0]);
1264 memcpy(buf
, addr
+ off
, sizes
[0]);
1265 kunmap_atomic(addr
);
1266 addr
= kmap_atomic(pages
[1]);
1267 memcpy(buf
+ sizes
[0], addr
, sizes
[1]);
1268 kunmap_atomic(addr
);
1270 return area
->vm_buf
;
1273 static void __zs_unmap_object(struct mapping_area
*area
,
1274 struct page
*pages
[2], int off
, int size
)
1280 /* no write fastpath */
1281 if (area
->vm_mm
== ZS_MM_RO
)
1285 buf
= buf
+ ZS_HANDLE_SIZE
;
1286 size
-= ZS_HANDLE_SIZE
;
1287 off
+= ZS_HANDLE_SIZE
;
1289 sizes
[0] = PAGE_SIZE
- off
;
1290 sizes
[1] = size
- sizes
[0];
1292 /* copy per-cpu buffer to object */
1293 addr
= kmap_atomic(pages
[0]);
1294 memcpy(addr
+ off
, buf
, sizes
[0]);
1295 kunmap_atomic(addr
);
1296 addr
= kmap_atomic(pages
[1]);
1297 memcpy(addr
, buf
+ sizes
[0], sizes
[1]);
1298 kunmap_atomic(addr
);
1301 /* enable page faults to match kunmap_atomic() return conditions */
1305 #endif /* CONFIG_PGTABLE_MAPPING */
1307 static int zs_cpu_notifier(struct notifier_block
*nb
, unsigned long action
,
1310 int ret
, cpu
= (long)pcpu
;
1311 struct mapping_area
*area
;
1314 case CPU_UP_PREPARE
:
1315 area
= &per_cpu(zs_map_area
, cpu
);
1316 ret
= __zs_cpu_up(area
);
1318 return notifier_from_errno(ret
);
1321 case CPU_UP_CANCELED
:
1322 area
= &per_cpu(zs_map_area
, cpu
);
1323 __zs_cpu_down(area
);
1330 static struct notifier_block zs_cpu_nb
= {
1331 .notifier_call
= zs_cpu_notifier
1334 static int zs_register_cpu_notifier(void)
1336 int cpu
, uninitialized_var(ret
);
1338 cpu_notifier_register_begin();
1340 __register_cpu_notifier(&zs_cpu_nb
);
1341 for_each_online_cpu(cpu
) {
1342 ret
= zs_cpu_notifier(NULL
, CPU_UP_PREPARE
, (void *)(long)cpu
);
1343 if (notifier_to_errno(ret
))
1347 cpu_notifier_register_done();
1348 return notifier_to_errno(ret
);
1351 static void zs_unregister_cpu_notifier(void)
1355 cpu_notifier_register_begin();
1357 for_each_online_cpu(cpu
)
1358 zs_cpu_notifier(NULL
, CPU_DEAD
, (void *)(long)cpu
);
1359 __unregister_cpu_notifier(&zs_cpu_nb
);
1361 cpu_notifier_register_done();
1364 static void init_zs_size_classes(void)
1368 nr
= (ZS_MAX_ALLOC_SIZE
- ZS_MIN_ALLOC_SIZE
) / ZS_SIZE_CLASS_DELTA
+ 1;
1369 if ((ZS_MAX_ALLOC_SIZE
- ZS_MIN_ALLOC_SIZE
) % ZS_SIZE_CLASS_DELTA
)
1372 zs_size_classes
= nr
;
1375 static bool can_merge(struct size_class
*prev
, int size
, int pages_per_zspage
)
1377 if (prev
->pages_per_zspage
!= pages_per_zspage
)
1380 if (get_maxobj_per_zspage(prev
->size
, prev
->pages_per_zspage
)
1381 != get_maxobj_per_zspage(size
, pages_per_zspage
))
1387 static bool zspage_full(struct size_class
*class, struct zspage
*zspage
)
1389 return get_zspage_inuse(zspage
) == class->objs_per_zspage
;
1392 unsigned long zs_get_total_pages(struct zs_pool
*pool
)
1394 return atomic_long_read(&pool
->pages_allocated
);
1396 EXPORT_SYMBOL_GPL(zs_get_total_pages
);
1399 * zs_map_object - get address of allocated object from handle.
1400 * @pool: pool from which the object was allocated
1401 * @handle: handle returned from zs_malloc
1403 * Before using an object allocated from zs_malloc, it must be mapped using
1404 * this function. When done with the object, it must be unmapped using
1407 * Only one object can be mapped per cpu at a time. There is no protection
1408 * against nested mappings.
1410 * This function returns with preemption and page faults disabled.
1412 void *zs_map_object(struct zs_pool
*pool
, unsigned long handle
,
1415 struct zspage
*zspage
;
1417 unsigned long obj
, off
;
1418 unsigned int obj_idx
;
1420 unsigned int class_idx
;
1421 enum fullness_group fg
;
1422 struct size_class
*class;
1423 struct mapping_area
*area
;
1424 struct page
*pages
[2];
1428 * Because we use per-cpu mapping areas shared among the
1429 * pools/users, we can't allow mapping in interrupt context
1430 * because it can corrupt another users mappings.
1432 WARN_ON_ONCE(in_interrupt());
1434 /* From now on, migration cannot move the object */
1435 #ifdef CONFIG_ZSWAP_SAME_PAGE_SHARING
1436 if (mm
== ZS_MM_RO_NOWAIT
) {
1437 if (!trypin_tag(handle
))
1445 obj
= handle_to_obj(handle
);
1446 obj_to_location(obj
, &page
, &obj_idx
);
1447 zspage
= get_zspage(page
);
1449 /* migration cannot move any subpage in this zspage */
1450 migrate_read_lock(zspage
);
1452 get_zspage_mapping(zspage
, &class_idx
, &fg
);
1453 class = pool
->size_class
[class_idx
];
1454 off
= (class->size
* obj_idx
) & ~PAGE_MASK
;
1456 area
= &get_cpu_var(zs_map_area
);
1458 if (off
+ class->size
<= PAGE_SIZE
) {
1459 /* this object is contained entirely within a page */
1460 area
->vm_addr
= kmap_atomic(page
);
1461 ret
= area
->vm_addr
+ off
;
1465 /* this object spans two pages */
1467 pages
[1] = get_next_page(page
);
1470 ret
= __zs_map_object(area
, pages
, off
, class->size
);
1472 if (likely(!PageHugeObject(page
)))
1473 ret
+= ZS_HANDLE_SIZE
;
1477 EXPORT_SYMBOL_GPL(zs_map_object
);
1479 void zs_unmap_object(struct zs_pool
*pool
, unsigned long handle
)
1481 struct zspage
*zspage
;
1483 unsigned long obj
, off
;
1484 unsigned int obj_idx
;
1486 unsigned int class_idx
;
1487 enum fullness_group fg
;
1488 struct size_class
*class;
1489 struct mapping_area
*area
;
1491 obj
= handle_to_obj(handle
);
1492 obj_to_location(obj
, &page
, &obj_idx
);
1493 zspage
= get_zspage(page
);
1494 get_zspage_mapping(zspage
, &class_idx
, &fg
);
1495 class = pool
->size_class
[class_idx
];
1496 off
= (class->size
* obj_idx
) & ~PAGE_MASK
;
1498 area
= this_cpu_ptr(&zs_map_area
);
1499 if (off
+ class->size
<= PAGE_SIZE
)
1500 kunmap_atomic(area
->vm_addr
);
1502 struct page
*pages
[2];
1505 pages
[1] = get_next_page(page
);
1508 __zs_unmap_object(area
, pages
, off
, class->size
);
1510 put_cpu_var(zs_map_area
);
1512 migrate_read_unlock(zspage
);
1515 EXPORT_SYMBOL_GPL(zs_unmap_object
);
1518 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1519 * zsmalloc &size_class.
1520 * @pool: zsmalloc pool to use
1522 * The function returns the size of the first huge class - any object of equal
1523 * or bigger size will be stored in zspage consisting of a single physical
1526 * Context: Any context.
1528 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1530 size_t zs_huge_class_size(struct zs_pool
*pool
)
1532 return huge_class_size
;
1534 EXPORT_SYMBOL_GPL(zs_huge_class_size
);
1536 static unsigned long obj_malloc(struct size_class
*class,
1537 struct zspage
*zspage
, unsigned long handle
)
1539 int i
, nr_page
, offset
;
1541 struct link_free
*link
;
1543 struct page
*m_page
;
1544 unsigned long m_offset
;
1547 handle
|= OBJ_ALLOCATED_TAG
;
1548 obj
= get_freeobj(zspage
);
1550 offset
= obj
* class->size
;
1551 nr_page
= offset
>> PAGE_SHIFT
;
1552 m_offset
= offset
& ~PAGE_MASK
;
1553 m_page
= get_first_page(zspage
);
1555 for (i
= 0; i
< nr_page
; i
++)
1556 m_page
= get_next_page(m_page
);
1558 vaddr
= kmap_atomic(m_page
);
1559 link
= (struct link_free
*)vaddr
+ m_offset
/ sizeof(*link
);
1560 set_freeobj(zspage
, link
->next
>> OBJ_ALLOCATED_TAG
);
1561 if (likely(!PageHugeObject(m_page
)))
1562 /* record handle in the header of allocated chunk */
1563 link
->handle
= handle
;
1565 /* record handle to page->index */
1566 zspage
->first_page
->index
= handle
;
1568 kunmap_atomic(vaddr
);
1569 mod_zspage_inuse(zspage
, 1);
1570 zs_stat_inc(class, OBJ_USED
, 1);
1572 obj
= location_to_obj(m_page
, obj
);
1579 * zs_malloc - Allocate block of given size from pool.
1580 * @pool: pool to allocate from
1581 * @size: size of block to allocate
1583 * On success, handle to the allocated object is returned,
1585 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1587 unsigned long zs_malloc(struct zs_pool
*pool
, size_t size
, gfp_t gfp
)
1589 unsigned long handle
, obj
;
1590 struct size_class
*class;
1591 enum fullness_group newfg
;
1592 struct zspage
*zspage
;
1594 if (unlikely(!size
|| size
> ZS_MAX_ALLOC_SIZE
))
1597 handle
= cache_alloc_handle(pool
, gfp
);
1601 /* extra space in chunk to keep the handle */
1602 size
+= ZS_HANDLE_SIZE
;
1603 class = pool
->size_class
[get_size_class_index(size
)];
1605 spin_lock(&class->lock
);
1606 zspage
= find_get_zspage(class);
1608 if (likely(zspage
)) {
1609 obj
= obj_malloc(class, zspage
, handle
);
1610 /* Now move the zspage to another fullness group, if required */
1611 fix_fullness_group(class, zspage
);
1612 record_obj(handle
, obj
);
1613 spin_unlock(&class->lock
);
1618 spin_unlock(&class->lock
);
1620 zspage
= alloc_zspage(pool
, class, gfp
);
1622 cache_free_handle(pool
, handle
);
1626 spin_lock(&class->lock
);
1627 obj
= obj_malloc(class, zspage
, handle
);
1628 newfg
= get_fullness_group(class, zspage
);
1629 insert_zspage(class, zspage
, newfg
);
1630 set_zspage_mapping(zspage
, class->index
, newfg
);
1631 record_obj(handle
, obj
);
1632 atomic_long_add(class->pages_per_zspage
,
1633 &pool
->pages_allocated
);
1634 zs_stat_inc(class, OBJ_ALLOCATED
, get_maxobj_per_zspage(
1635 class->size
, class->pages_per_zspage
));
1637 /* We completely set up zspage so mark them as movable */
1638 SetZsPageMovable(pool
, zspage
);
1639 spin_unlock(&class->lock
);
1643 EXPORT_SYMBOL_GPL(zs_malloc
);
1645 static void obj_free(struct size_class
*class, unsigned long obj
)
1647 struct link_free
*link
;
1648 struct zspage
*zspage
;
1649 struct page
*f_page
;
1650 unsigned long f_offset
;
1651 unsigned int f_objidx
;
1654 obj
&= ~OBJ_ALLOCATED_TAG
;
1655 obj_to_location(obj
, &f_page
, &f_objidx
);
1656 f_offset
= (class->size
* f_objidx
) & ~PAGE_MASK
;
1657 zspage
= get_zspage(f_page
);
1659 vaddr
= kmap_atomic(f_page
);
1661 /* Insert this object in containing zspage's freelist */
1662 link
= (struct link_free
*)(vaddr
+ f_offset
);
1663 link
->next
= get_freeobj(zspage
) << OBJ_ALLOCATED_TAG
;
1664 kunmap_atomic(vaddr
);
1665 set_freeobj(zspage
, f_objidx
);
1666 mod_zspage_inuse(zspage
, -1);
1667 zs_stat_dec(class, OBJ_USED
, 1);
1670 void zs_free(struct zs_pool
*pool
, unsigned long handle
)
1672 struct zspage
*zspage
;
1673 struct page
*f_page
;
1675 unsigned int f_objidx
;
1677 struct size_class
*class;
1678 enum fullness_group fullness
;
1681 if (unlikely(!handle
))
1685 obj
= handle_to_obj(handle
);
1686 obj_to_location(obj
, &f_page
, &f_objidx
);
1687 zspage
= get_zspage(f_page
);
1689 migrate_read_lock(zspage
);
1691 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
1692 class = pool
->size_class
[class_idx
];
1694 spin_lock(&class->lock
);
1695 obj_free(class, obj
);
1696 fullness
= fix_fullness_group(class, zspage
);
1697 if (fullness
!= ZS_EMPTY
) {
1698 migrate_read_unlock(zspage
);
1702 isolated
= is_zspage_isolated(zspage
);
1703 migrate_read_unlock(zspage
);
1704 /* If zspage is isolated, zs_page_putback will free the zspage */
1705 if (likely(!isolated
))
1706 free_zspage(pool
, class, zspage
);
1709 spin_unlock(&class->lock
);
1711 cache_free_handle(pool
, handle
);
1713 EXPORT_SYMBOL_GPL(zs_free
);
1715 static void zs_object_copy(struct size_class
*class, unsigned long dst
,
1718 struct page
*s_page
, *d_page
;
1719 unsigned int s_objidx
, d_objidx
;
1720 unsigned long s_off
, d_off
;
1721 void *s_addr
, *d_addr
;
1722 int s_size
, d_size
, size
;
1725 s_size
= d_size
= class->size
;
1727 obj_to_location(src
, &s_page
, &s_objidx
);
1728 obj_to_location(dst
, &d_page
, &d_objidx
);
1730 s_off
= (class->size
* s_objidx
) & ~PAGE_MASK
;
1731 d_off
= (class->size
* d_objidx
) & ~PAGE_MASK
;
1733 if (s_off
+ class->size
> PAGE_SIZE
)
1734 s_size
= PAGE_SIZE
- s_off
;
1736 if (d_off
+ class->size
> PAGE_SIZE
)
1737 d_size
= PAGE_SIZE
- d_off
;
1739 s_addr
= kmap_atomic(s_page
);
1740 d_addr
= kmap_atomic(d_page
);
1743 size
= min(s_size
, d_size
);
1744 memcpy(d_addr
+ d_off
, s_addr
+ s_off
, size
);
1747 if (written
== class->size
)
1755 if (s_off
>= PAGE_SIZE
) {
1756 kunmap_atomic(d_addr
);
1757 kunmap_atomic(s_addr
);
1758 s_page
= get_next_page(s_page
);
1759 s_addr
= kmap_atomic(s_page
);
1760 d_addr
= kmap_atomic(d_page
);
1761 s_size
= class->size
- written
;
1765 if (d_off
>= PAGE_SIZE
) {
1766 kunmap_atomic(d_addr
);
1767 d_page
= get_next_page(d_page
);
1768 d_addr
= kmap_atomic(d_page
);
1769 d_size
= class->size
- written
;
1774 kunmap_atomic(d_addr
);
1775 kunmap_atomic(s_addr
);
1779 * Find alloced object in zspage from index object and
1782 static unsigned long find_alloced_obj(struct size_class
*class,
1783 struct page
*page
, int index
)
1787 unsigned long handle
= 0;
1788 void *addr
= kmap_atomic(page
);
1790 offset
= get_first_obj_offset(page
);
1791 offset
+= class->size
* index
;
1793 while (offset
< PAGE_SIZE
) {
1794 head
= obj_to_head(page
, addr
+ offset
);
1795 if (head
& OBJ_ALLOCATED_TAG
) {
1796 handle
= head
& ~OBJ_ALLOCATED_TAG
;
1797 if (trypin_tag(handle
))
1802 offset
+= class->size
;
1806 kunmap_atomic(addr
);
1810 struct zs_compact_control
{
1811 /* Source spage for migration which could be a subpage of zspage */
1812 struct page
*s_page
;
1813 /* Destination page for migration which should be a first page
1815 struct page
*d_page
;
1816 /* Starting object index within @s_page which used for live object
1817 * in the subpage. */
1821 static int migrate_zspage(struct zs_pool
*pool
, struct size_class
*class,
1822 struct zs_compact_control
*cc
)
1824 unsigned long used_obj
, free_obj
;
1825 unsigned long handle
;
1826 struct page
*s_page
= cc
->s_page
;
1827 struct page
*d_page
= cc
->d_page
;
1828 unsigned long index
= cc
->index
;
1832 handle
= find_alloced_obj(class, s_page
, index
);
1834 s_page
= get_next_page(s_page
);
1841 /* Stop if there is no more space */
1842 if (zspage_full(class, get_zspage(d_page
))) {
1848 used_obj
= handle_to_obj(handle
);
1849 free_obj
= obj_malloc(class, get_zspage(d_page
), handle
);
1850 zs_object_copy(class, free_obj
, used_obj
);
1853 * record_obj updates handle's value to free_obj and it will
1854 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1855 * breaks synchronization using pin_tag(e,g, zs_free) so
1856 * let's keep the lock bit.
1858 free_obj
|= BIT(HANDLE_PIN_BIT
);
1859 record_obj(handle
, free_obj
);
1861 obj_free(class, used_obj
);
1864 /* Remember last position in this iteration */
1865 cc
->s_page
= s_page
;
1871 static struct zspage
*isolate_zspage(struct size_class
*class, bool source
)
1874 struct zspage
*zspage
;
1875 enum fullness_group fg
[2] = {ZS_ALMOST_EMPTY
, ZS_ALMOST_FULL
};
1878 fg
[0] = ZS_ALMOST_FULL
;
1879 fg
[1] = ZS_ALMOST_EMPTY
;
1882 for (i
= 0; i
< 2; i
++) {
1883 zspage
= list_first_entry_or_null(&class->fullness_list
[fg
[i
]],
1884 struct zspage
, list
);
1886 VM_BUG_ON(is_zspage_isolated(zspage
));
1887 remove_zspage(class, zspage
, fg
[i
]);
1896 * putback_zspage - add @zspage into right class's fullness list
1897 * @class: destination class
1898 * @zspage: target page
1900 * Return @zspage's fullness_group
1902 static enum fullness_group
putback_zspage(struct size_class
*class,
1903 struct zspage
*zspage
)
1905 enum fullness_group fullness
;
1907 VM_BUG_ON(is_zspage_isolated(zspage
));
1909 fullness
= get_fullness_group(class, zspage
);
1910 insert_zspage(class, zspage
, fullness
);
1911 set_zspage_mapping(zspage
, class->index
, fullness
);
1916 #ifdef CONFIG_COMPACTION
1917 static struct dentry
*zs_mount(struct file_system_type
*fs_type
,
1918 int flags
, const char *dev_name
, void *data
)
1920 static const struct dentry_operations ops
= {
1921 .d_dname
= simple_dname
,
1924 return mount_pseudo(fs_type
, "zsmalloc:", NULL
, &ops
, ZSMALLOC_MAGIC
);
1927 static struct file_system_type zsmalloc_fs
= {
1930 .kill_sb
= kill_anon_super
,
1933 static int zsmalloc_mount(void)
1937 zsmalloc_mnt
= kern_mount(&zsmalloc_fs
);
1938 if (IS_ERR(zsmalloc_mnt
))
1939 ret
= PTR_ERR(zsmalloc_mnt
);
1944 static void zsmalloc_unmount(void)
1946 kern_unmount(zsmalloc_mnt
);
1949 static void migrate_lock_init(struct zspage
*zspage
)
1951 rwlock_init(&zspage
->lock
);
1954 static void migrate_read_lock(struct zspage
*zspage
)
1956 read_lock(&zspage
->lock
);
1959 static void migrate_read_unlock(struct zspage
*zspage
)
1961 read_unlock(&zspage
->lock
);
1964 static void migrate_write_lock(struct zspage
*zspage
)
1966 write_lock(&zspage
->lock
);
1969 static void migrate_write_unlock(struct zspage
*zspage
)
1971 write_unlock(&zspage
->lock
);
1974 /* Number of isolated subpage for *page migration* in this zspage */
1975 static void inc_zspage_isolation(struct zspage
*zspage
)
1980 static void dec_zspage_isolation(struct zspage
*zspage
)
1985 static void replace_sub_page(struct size_class
*class, struct zspage
*zspage
,
1986 struct page
*newpage
, struct page
*oldpage
)
1989 struct page
*pages
[ZS_MAX_PAGES_PER_ZSPAGE
] = {NULL
, };
1992 page
= get_first_page(zspage
);
1994 if (page
== oldpage
)
1995 pages
[idx
] = newpage
;
1999 } while ((page
= get_next_page(page
)) != NULL
);
2001 create_page_chain(class, zspage
, pages
);
2002 set_first_obj_offset(newpage
, get_first_obj_offset(oldpage
));
2003 if (unlikely(PageHugeObject(oldpage
)))
2004 newpage
->index
= oldpage
->index
;
2005 __SetPageMovable(newpage
, page_mapping(oldpage
));
2008 bool zs_page_isolate(struct page
*page
, isolate_mode_t mode
)
2010 struct zs_pool
*pool
;
2011 struct size_class
*class;
2013 enum fullness_group fullness
;
2014 struct zspage
*zspage
;
2015 struct address_space
*mapping
;
2018 * Page is locked so zspage couldn't be destroyed. For detail, look at
2019 * lock_zspage in free_zspage.
2021 if (!zs_page_migration_enabled
)
2024 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
2025 VM_BUG_ON_PAGE(PageIsolated(page
), page
);
2027 zspage
= get_zspage(page
);
2030 * Without class lock, fullness could be stale while class_idx is okay
2031 * because class_idx is constant unless page is freed so we should get
2032 * fullness again under class lock.
2034 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2035 mapping
= page_mapping(page
);
2036 pool
= mapping
->private_data
;
2037 class = pool
->size_class
[class_idx
];
2039 spin_lock(&class->lock
);
2040 if (get_zspage_inuse(zspage
) == 0) {
2041 spin_unlock(&class->lock
);
2045 /* zspage is isolated for object migration */
2046 if (list_empty(&zspage
->list
) && !is_zspage_isolated(zspage
)) {
2047 spin_unlock(&class->lock
);
2052 * If this is first time isolation for the zspage, isolate zspage from
2053 * size_class to prevent further object allocation from the zspage.
2055 if (!list_empty(&zspage
->list
) && !is_zspage_isolated(zspage
)) {
2056 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2057 remove_zspage(class, zspage
, fullness
);
2060 inc_zspage_isolation(zspage
);
2061 spin_unlock(&class->lock
);
2066 int zs_page_migrate(struct address_space
*mapping
, struct page
*newpage
,
2067 struct page
*page
, enum migrate_mode mode
)
2069 struct zs_pool
*pool
;
2070 struct size_class
*class;
2072 enum fullness_group fullness
;
2073 struct zspage
*zspage
;
2075 void *s_addr
, *d_addr
, *addr
;
2077 unsigned long handle
, head
;
2078 unsigned long old_obj
, new_obj
;
2079 unsigned int obj_idx
;
2082 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
2083 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
2085 zspage
= get_zspage(page
);
2087 /* Concurrent compactor cannot migrate any subpage in zspage */
2088 migrate_write_lock(zspage
);
2089 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2090 pool
= mapping
->private_data
;
2091 class = pool
->size_class
[class_idx
];
2092 offset
= get_first_obj_offset(page
);
2094 spin_lock(&class->lock
);
2095 if (!get_zspage_inuse(zspage
)) {
2101 s_addr
= kmap_atomic(page
);
2102 while (pos
< PAGE_SIZE
) {
2103 head
= obj_to_head(page
, s_addr
+ pos
);
2104 if (head
& OBJ_ALLOCATED_TAG
) {
2105 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2106 if (!trypin_tag(handle
))
2113 * Here, any user cannot access all objects in the zspage so let's move.
2115 d_addr
= kmap_atomic(newpage
);
2116 memcpy(d_addr
, s_addr
, PAGE_SIZE
);
2117 kunmap_atomic(d_addr
);
2119 for (addr
= s_addr
+ offset
; addr
< s_addr
+ pos
;
2120 addr
+= class->size
) {
2121 head
= obj_to_head(page
, addr
);
2122 if (head
& OBJ_ALLOCATED_TAG
) {
2123 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2124 if (!testpin_tag(handle
))
2127 old_obj
= handle_to_obj(handle
);
2128 obj_to_location(old_obj
, &dummy
, &obj_idx
);
2129 new_obj
= (unsigned long)location_to_obj(newpage
,
2131 new_obj
|= BIT(HANDLE_PIN_BIT
);
2132 record_obj(handle
, new_obj
);
2136 replace_sub_page(class, zspage
, newpage
, page
);
2139 dec_zspage_isolation(zspage
);
2142 * Page migration is done so let's putback isolated zspage to
2143 * the list if @page is final isolated subpage in the zspage.
2145 if (!is_zspage_isolated(zspage
))
2146 putback_zspage(class, zspage
);
2154 for (addr
= s_addr
+ offset
; addr
< s_addr
+ pos
;
2155 addr
+= class->size
) {
2156 head
= obj_to_head(page
, addr
);
2157 if (head
& OBJ_ALLOCATED_TAG
) {
2158 handle
= head
& ~OBJ_ALLOCATED_TAG
;
2159 if (!testpin_tag(handle
))
2164 kunmap_atomic(s_addr
);
2166 spin_unlock(&class->lock
);
2167 migrate_write_unlock(zspage
);
2172 void zs_page_putback(struct page
*page
)
2174 struct zs_pool
*pool
;
2175 struct size_class
*class;
2177 enum fullness_group fg
;
2178 struct address_space
*mapping
;
2179 struct zspage
*zspage
;
2181 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
2182 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
2184 zspage
= get_zspage(page
);
2185 get_zspage_mapping(zspage
, &class_idx
, &fg
);
2186 mapping
= page_mapping(page
);
2187 pool
= mapping
->private_data
;
2188 class = pool
->size_class
[class_idx
];
2190 spin_lock(&class->lock
);
2191 dec_zspage_isolation(zspage
);
2192 if (!is_zspage_isolated(zspage
)) {
2193 fg
= putback_zspage(class, zspage
);
2195 * Due to page_lock, we cannot free zspage immediately
2199 schedule_work(&pool
->free_work
);
2201 spin_unlock(&class->lock
);
2204 const struct address_space_operations zsmalloc_aops
= {
2205 .isolate_page
= zs_page_isolate
,
2206 .migratepage
= zs_page_migrate
,
2207 .putback_page
= zs_page_putback
,
2210 static int zs_register_migration(struct zs_pool
*pool
)
2212 pool
->inode
= alloc_anon_inode(zsmalloc_mnt
->mnt_sb
);
2213 if (IS_ERR(pool
->inode
)) {
2218 pool
->inode
->i_mapping
->private_data
= pool
;
2219 pool
->inode
->i_mapping
->a_ops
= &zsmalloc_aops
;
2223 static void zs_unregister_migration(struct zs_pool
*pool
)
2225 flush_work(&pool
->free_work
);
2231 * Caller should hold page_lock of all pages in the zspage
2232 * In here, we cannot use zspage meta data.
2234 static void async_free_zspage(struct work_struct
*work
)
2237 struct size_class
*class;
2238 unsigned int class_idx
;
2239 enum fullness_group fullness
;
2240 struct zspage
*zspage
, *tmp
;
2241 LIST_HEAD(free_pages
);
2242 struct zs_pool
*pool
= container_of(work
, struct zs_pool
,
2245 for (i
= 0; i
< zs_size_classes
; i
++) {
2246 class = pool
->size_class
[i
];
2247 if (class->index
!= i
)
2250 spin_lock(&class->lock
);
2251 list_splice_init(&class->fullness_list
[ZS_EMPTY
], &free_pages
);
2252 spin_unlock(&class->lock
);
2256 list_for_each_entry_safe(zspage
, tmp
, &free_pages
, list
) {
2257 list_del(&zspage
->list
);
2258 lock_zspage(zspage
);
2260 get_zspage_mapping(zspage
, &class_idx
, &fullness
);
2261 VM_BUG_ON(fullness
!= ZS_EMPTY
);
2262 class = pool
->size_class
[class_idx
];
2263 spin_lock(&class->lock
);
2264 __free_zspage(pool
, pool
->size_class
[class_idx
], zspage
);
2265 spin_unlock(&class->lock
);
2269 static void kick_deferred_free(struct zs_pool
*pool
)
2271 schedule_work(&pool
->free_work
);
2274 static void init_deferred_free(struct zs_pool
*pool
)
2276 INIT_WORK(&pool
->free_work
, async_free_zspage
);
2279 static void SetZsPageMovable(struct zs_pool
*pool
, struct zspage
*zspage
)
2281 struct page
*page
= get_first_page(zspage
);
2284 WARN_ON(!trylock_page(page
));
2285 __SetPageMovable(page
, pool
->inode
->i_mapping
);
2287 } while ((page
= get_next_page(page
)) != NULL
);
2293 * Based on the number of unused allocated objects calculate
2294 * and return the number of pages that we can free.
2296 static unsigned long zs_can_compact(struct size_class
*class)
2298 unsigned long obj_wasted
;
2299 unsigned long obj_allocated
= zs_stat_get(class, OBJ_ALLOCATED
);
2300 unsigned long obj_used
= zs_stat_get(class, OBJ_USED
);
2302 if (obj_allocated
<= obj_used
)
2305 obj_wasted
= obj_allocated
- obj_used
;
2306 obj_wasted
/= get_maxobj_per_zspage(class->size
,
2307 class->pages_per_zspage
);
2309 return obj_wasted
* class->pages_per_zspage
;
2312 static void __zs_compact(struct zs_pool
*pool
, struct size_class
*class)
2314 struct zs_compact_control cc
;
2315 struct zspage
*src_zspage
;
2316 struct zspage
*dst_zspage
= NULL
;
2318 spin_lock(&class->lock
);
2319 while ((src_zspage
= isolate_zspage(class, true))) {
2321 if (!zs_can_compact(class))
2325 cc
.s_page
= get_first_page(src_zspage
);
2327 while ((dst_zspage
= isolate_zspage(class, false))) {
2328 cc
.d_page
= get_first_page(dst_zspage
);
2330 * If there is no more space in dst_page, resched
2331 * and see if anyone had allocated another zspage.
2333 if (!migrate_zspage(pool
, class, &cc
))
2336 putback_zspage(class, dst_zspage
);
2339 /* Stop if we couldn't find slot */
2340 if (dst_zspage
== NULL
)
2343 putback_zspage(class, dst_zspage
);
2344 if (putback_zspage(class, src_zspage
) == ZS_EMPTY
) {
2345 free_zspage(pool
, class, src_zspage
);
2346 pool
->stats
.pages_compacted
+= class->pages_per_zspage
;
2348 spin_unlock(&class->lock
);
2350 spin_lock(&class->lock
);
2354 putback_zspage(class, src_zspage
);
2356 spin_unlock(&class->lock
);
2359 unsigned long zs_compact(struct zs_pool
*pool
)
2362 struct size_class
*class;
2364 for (i
= zs_size_classes
- 1; i
>= 0; i
--) {
2365 class = pool
->size_class
[i
];
2368 if (class->index
!= i
)
2370 __zs_compact(pool
, class);
2373 return pool
->stats
.pages_compacted
;
2375 EXPORT_SYMBOL_GPL(zs_compact
);
2377 void zs_pool_stats(struct zs_pool
*pool
, struct zs_pool_stats
*stats
)
2379 memcpy(stats
, &pool
->stats
, sizeof(struct zs_pool_stats
));
2381 EXPORT_SYMBOL_GPL(zs_pool_stats
);
2383 static unsigned long zs_shrinker_scan(struct shrinker
*shrinker
,
2384 struct shrink_control
*sc
)
2386 unsigned long pages_freed
;
2387 struct zs_pool
*pool
= container_of(shrinker
, struct zs_pool
,
2390 pages_freed
= pool
->stats
.pages_compacted
;
2392 * Compact classes and calculate compaction delta.
2393 * Can run concurrently with a manually triggered
2394 * (by user) compaction.
2396 pages_freed
= zs_compact(pool
) - pages_freed
;
2398 return pages_freed
? pages_freed
: SHRINK_STOP
;
2401 #define ZS_SHRINKER_THRESHOLD 1024
2402 #define ZS_SHRINKER_INTERVAL 10
2404 static unsigned long zs_shrinker_count(struct shrinker
*shrinker
,
2405 struct shrink_control
*sc
)
2408 struct size_class
*class;
2409 unsigned long pages_to_free
= 0;
2410 struct zs_pool
*pool
= container_of(shrinker
, struct zs_pool
,
2412 static unsigned long time_stamp
;
2414 if (!current_is_kswapd() || time_is_after_jiffies(time_stamp
))
2417 for (i
= zs_size_classes
- 1; i
>= 0; i
--) {
2418 class = pool
->size_class
[i
];
2421 if (class->index
!= i
)
2424 pages_to_free
+= zs_can_compact(class);
2427 if (pages_to_free
> ZS_SHRINKER_THRESHOLD
)
2428 time_stamp
= jiffies
+ (ZS_SHRINKER_INTERVAL
* HZ
);
2432 return pages_to_free
;
2435 static void zs_unregister_shrinker(struct zs_pool
*pool
)
2437 if (pool
->shrinker_enabled
) {
2438 unregister_shrinker(&pool
->shrinker
);
2439 pool
->shrinker_enabled
= false;
2443 static int zs_register_shrinker(struct zs_pool
*pool
)
2445 pool
->shrinker
.scan_objects
= zs_shrinker_scan
;
2446 pool
->shrinker
.count_objects
= zs_shrinker_count
;
2447 pool
->shrinker
.batch
= 0;
2448 pool
->shrinker
.seeks
= DEFAULT_SEEKS
;
2450 return register_shrinker(&pool
->shrinker
);
2454 * zs_create_pool - Creates an allocation pool to work from.
2455 * @flags: allocation flags used to allocate pool metadata
2457 * This function must be called before anything when using
2458 * the zsmalloc allocator.
2460 * On success, a pointer to the newly created pool is returned,
2463 struct zs_pool
*zs_create_pool(const char *name
)
2466 struct zs_pool
*pool
;
2467 struct size_class
*prev_class
= NULL
;
2469 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
2473 init_deferred_free(pool
);
2474 pool
->size_class
= kcalloc(zs_size_classes
, sizeof(struct size_class
*),
2476 if (!pool
->size_class
) {
2481 pool
->name
= kstrdup(name
, GFP_KERNEL
);
2485 if (create_cache(pool
))
2489 * Iterate reversly, because, size of size_class that we want to use
2490 * for merging should be larger or equal to current size.
2492 for (i
= zs_size_classes
- 1; i
>= 0; i
--) {
2494 int pages_per_zspage
;
2495 int objs_per_zspage
;
2496 struct size_class
*class;
2499 size
= ZS_MIN_ALLOC_SIZE
+ i
* ZS_SIZE_CLASS_DELTA
;
2500 if (size
> ZS_MAX_ALLOC_SIZE
)
2501 size
= ZS_MAX_ALLOC_SIZE
;
2502 pages_per_zspage
= get_pages_per_zspage(size
);
2503 objs_per_zspage
= pages_per_zspage
* PAGE_SIZE
/ size
;
2506 * We iterate from biggest down to smallest classes,
2507 * so huge_class_size holds the size of the first huge
2508 * class. Any object bigger than or equal to that will
2509 * endup in the huge class.
2511 if (pages_per_zspage
!= 1 && objs_per_zspage
!= 1 &&
2513 huge_class_size
= size
;
2515 * The object uses ZS_HANDLE_SIZE bytes to store the
2516 * handle. We need to subtract it, because zs_malloc()
2517 * unconditionally adds handle size before it performs
2518 * size class search - so object may be smaller than
2519 * huge class size, yet it still can end up in the huge
2520 * class because it grows by ZS_HANDLE_SIZE extra bytes
2521 * right before class lookup.
2523 huge_class_size
-= (ZS_HANDLE_SIZE
- 1);
2527 * size_class is used for normal zsmalloc operation such
2528 * as alloc/free for that size. Although it is natural that we
2529 * have one size_class for each size, there is a chance that we
2530 * can get more memory utilization if we use one size_class for
2531 * many different sizes whose size_class have same
2532 * characteristics. So, we makes size_class point to
2533 * previous size_class if possible.
2536 if (can_merge(prev_class
, size
, pages_per_zspage
)) {
2537 pool
->size_class
[i
] = prev_class
;
2542 class = kzalloc(sizeof(struct size_class
), GFP_KERNEL
);
2548 class->pages_per_zspage
= pages_per_zspage
;
2549 class->objs_per_zspage
= class->pages_per_zspage
*
2550 PAGE_SIZE
/ class->size
;
2551 spin_lock_init(&class->lock
);
2552 pool
->size_class
[i
] = class;
2553 for (fullness
= ZS_EMPTY
; fullness
< NR_ZS_FULLNESS
;
2555 INIT_LIST_HEAD(&class->fullness_list
[fullness
]);
2560 if (zs_pool_stat_create(pool
, name
))
2563 if (zs_register_migration(pool
))
2567 * Not critical, we still can use the pool
2568 * and user can trigger compaction manually.
2570 if (zs_register_shrinker(pool
) == 0)
2571 pool
->shrinker_enabled
= true;
2575 zs_destroy_pool(pool
);
2578 EXPORT_SYMBOL_GPL(zs_create_pool
);
2580 void zs_destroy_pool(struct zs_pool
*pool
)
2584 zs_unregister_shrinker(pool
);
2585 zs_unregister_migration(pool
);
2586 zs_pool_stat_destroy(pool
);
2588 for (i
= 0; i
< zs_size_classes
; i
++) {
2590 struct size_class
*class = pool
->size_class
[i
];
2595 if (class->index
!= i
)
2598 for (fg
= ZS_EMPTY
; fg
< NR_ZS_FULLNESS
; fg
++) {
2599 if (!list_empty(&class->fullness_list
[fg
])) {
2600 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
2607 destroy_cache(pool
);
2608 kfree(pool
->size_class
);
2612 EXPORT_SYMBOL_GPL(zs_destroy_pool
);
2614 static int __init
zs_init(void)
2618 ret
= zsmalloc_mount();
2622 ret
= zs_register_cpu_notifier();
2627 init_zs_size_classes();
2630 zpool_register_driver(&zs_zpool_driver
);
2633 ret
= zs_stat_init();
2635 pr_err("zs stat initialization failed\n");
2642 zpool_unregister_driver(&zs_zpool_driver
);
2645 zs_unregister_cpu_notifier();
2651 static void __exit
zs_exit(void)
2654 zpool_unregister_driver(&zs_zpool_driver
);
2657 zs_unregister_cpu_notifier();
2662 module_init(zs_init
);
2663 module_exit(zs_exit
);
2665 MODULE_LICENSE("Dual BSD/GPL");
2666 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");