blob: aa7cfba9ea2cdebbf1d66133b052c7ea47a807ee [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * 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
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
Minchan Kim48b48002016-07-26 15:23:31 -070028 * PG_owner_priv_1: indentifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070029 *
30 */
31
Dan Streetman4abaac92016-05-26 15:16:27 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Nitin Gupta61989a82012-01-09 16:51:56 -060034#include <linux/module.h>
35#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070036#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#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>
Seth Jennings0cbb6132012-02-13 08:47:49 -060046#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080047#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090048#include <linux/spinlock.h>
49#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080050#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080051#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070052#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070053#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070054#include <linux/migrate.h>
Henry Burns0e7e2732019-08-24 17:55:06 -070055#include <linux/wait.h>
Minchan Kim48b48002016-07-26 15:23:31 -070056#include <linux/pagemap.h>
57
58#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090059
60/*
61 * This must be power of 2 and greater than of equal to sizeof(link_free).
62 * These two conditions ensure that any 'struct link_free' itself doesn't
63 * span more than 1 page which avoids complex case of mapping 2 pages simply
64 * to restore link_free pointer values.
65 */
66#define ZS_ALIGN 8
67
68/*
69 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
70 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
71 */
72#define ZS_MAX_ZSPAGE_ORDER 2
73#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
74
Minchan Kim2e40e162015-04-15 16:15:23 -070075#define ZS_HANDLE_SIZE (sizeof(unsigned long))
76
Seth Jennings0959c632012-08-08 15:12:17 +090077/*
78 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090079 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090080 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070081 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090082 *
83 * This is made more complicated by various memory models and PAE.
84 */
85
Kirill A. Shutemov215321a02021-11-03 13:57:13 -070086#ifndef MAX_POSSIBLE_PHYSMEM_BITS
87#ifdef MAX_PHYSMEM_BITS
88#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS
89#else
Seth Jennings0959c632012-08-08 15:12:17 +090090/*
91 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
92 * be PAGE_SHIFT
93 */
Kirill A. Shutemov215321a02021-11-03 13:57:13 -070094#define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
Seth Jennings0959c632012-08-08 15:12:17 +090095#endif
96#endif
Kirill A. Shutemov215321a02021-11-03 13:57:13 -070097
98#define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070099
100/*
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.
106 */
107#define HANDLE_PIN_BIT 0
108
109/*
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.
115 */
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)
Seth Jennings0959c632012-08-08 15:12:17 +0900119#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
120
121#define MAX(a, b) ((a) >= (b) ? (a) : (b))
122/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
123#define ZS_MIN_ALLOC_SIZE \
124 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700125/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700126#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900127
128/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700129 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900130 * trader-off here:
131 * - Large number of size classes is potentially wasteful as free page are
132 * spread across these classes
133 * - Small number of size classes causes large internal fragmentation
134 * - Probably its better to use specific size classes (empirically
135 * determined). NOTE: all those class sizes must be set as multiple of
136 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
137 *
138 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
139 * (reason above)
140 */
Minchan Kim37836892016-07-26 15:23:23 -0700141#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900142
Seth Jennings0959c632012-08-08 15:12:17 +0900143enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900144 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700145 ZS_ALMOST_EMPTY,
146 ZS_ALMOST_FULL,
147 ZS_FULL,
148 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900149};
150
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800151enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700152 CLASS_EMPTY,
153 CLASS_ALMOST_EMPTY,
154 CLASS_ALMOST_FULL,
155 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800156 OBJ_ALLOCATED,
157 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700158 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800159};
160
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800161struct zs_size_stat {
162 unsigned long objs[NR_ZS_STAT_TYPE];
163};
164
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700165#ifdef CONFIG_ZSMALLOC_STAT
166static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800167#endif
168
Minchan Kim48b48002016-07-26 15:23:31 -0700169#ifdef CONFIG_COMPACTION
170static struct vfsmount *zsmalloc_mnt;
171#endif
172
Seth Jennings0959c632012-08-08 15:12:17 +0900173/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800174 * number of size_classes
175 */
176static int zs_size_classes;
177
178/*
Seth Jennings0959c632012-08-08 15:12:17 +0900179 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
180 * n <= N / f, where
181 * n = number of allocated objects
182 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700183 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900184 *
185 * Similarly, we assign zspage to:
186 * ZS_ALMOST_FULL when n > N / f
187 * ZS_EMPTY when n == 0
188 * ZS_FULL when n == N
189 *
190 * (see: fix_fullness_group())
191 */
192static const int fullness_threshold_frac = 4;
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -0700193static size_t huge_class_size;
Seth Jennings0959c632012-08-08 15:12:17 +0900194
195struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700196 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700197 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900198 /*
199 * Size of objects stored in this class. Must be multiple
200 * of ZS_ALIGN.
201 */
202 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700203 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800204 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
205 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700206
207 unsigned int index;
208 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900209};
210
Minchan Kim48b48002016-07-26 15:23:31 -0700211/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
212static void SetPageHugeObject(struct page *page)
213{
214 SetPageOwnerPriv1(page);
215}
216
217static void ClearPageHugeObject(struct page *page)
218{
219 ClearPageOwnerPriv1(page);
220}
221
222static int PageHugeObject(struct page *page)
223{
224 return PageOwnerPriv1(page);
225}
226
Seth Jennings0959c632012-08-08 15:12:17 +0900227/*
228 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700229 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900230 *
231 * This must be power of 2 and less than or equal to ZS_ALIGN
232 */
233struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700234 union {
235 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700236 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700237 * It's valid for non-allocated object
238 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700239 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700240 /*
241 * Handle of allocated object.
242 */
243 unsigned long handle;
244 };
Seth Jennings0959c632012-08-08 15:12:17 +0900245};
246
247struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800248 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800249
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800250 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700251 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700252 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900253
Minchan Kim13de8932014-10-09 15:29:48 -0700254 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800255
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700256 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700257
258 /* Compact classes */
259 struct shrinker shrinker;
260 /*
261 * To signify that register_shrinker() was successful
262 * and unregister_shrinker() will not Oops.
263 */
264 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800265#ifdef CONFIG_ZSMALLOC_STAT
266 struct dentry *stat_dentry;
267#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700268#ifdef CONFIG_COMPACTION
269 struct inode *inode;
270 struct work_struct free_work;
Henry Burns0e7e2732019-08-24 17:55:06 -0700271 /* A wait queue for when migration races with async_free_zspage() */
272 wait_queue_head_t migration_wait;
273 atomic_long_t isolated_pages;
274 bool destroying;
Minchan Kim48b48002016-07-26 15:23:31 -0700275#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900276};
Nitin Gupta61989a82012-01-09 16:51:56 -0600277
278/*
279 * A zspage's class index and fullness group
280 * are encoded in its (first)page->mapping
281 */
Minchan Kim37836892016-07-26 15:23:23 -0700282#define FULLNESS_BITS 2
283#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700284#define ISOLATED_BITS 3
285#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700286
Minchan Kim37836892016-07-26 15:23:23 -0700287struct zspage {
288 struct {
289 unsigned int fullness:FULLNESS_BITS;
Minchan Kimd19f7452017-04-13 14:56:40 -0700290 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700291 unsigned int isolated:ISOLATED_BITS;
292 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700293 };
294 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700295 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700296 struct page *first_page;
297 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700298#ifdef CONFIG_COMPACTION
299 rwlock_t lock;
300#endif
Minchan Kim37836892016-07-26 15:23:23 -0700301};
Nitin Gupta61989a82012-01-09 16:51:56 -0600302
Seth Jenningsf5536462012-07-18 11:55:56 -0500303struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900304#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500305 struct vm_struct *vm; /* vm area for mapping object that span pages */
306#else
307 char *vm_buf; /* copy buffer for objects that span pages */
308#endif
309 char *vm_addr; /* address of kmap_atomic()'ed pages */
310 enum zs_mapmode vm_mm; /* mapping mode */
311};
312
Minchan Kim48b48002016-07-26 15:23:31 -0700313#ifdef CONFIG_COMPACTION
314static int zs_register_migration(struct zs_pool *pool);
315static void zs_unregister_migration(struct zs_pool *pool);
316static void migrate_lock_init(struct zspage *zspage);
317static void migrate_read_lock(struct zspage *zspage);
318static void migrate_read_unlock(struct zspage *zspage);
319static void kick_deferred_free(struct zs_pool *pool);
320static void init_deferred_free(struct zs_pool *pool);
321static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
322#else
323static int zsmalloc_mount(void) { return 0; }
324static void zsmalloc_unmount(void) {}
325static int zs_register_migration(struct zs_pool *pool) { return 0; }
326static void zs_unregister_migration(struct zs_pool *pool) {}
327static void migrate_lock_init(struct zspage *zspage) {}
328static void migrate_read_lock(struct zspage *zspage) {}
329static void migrate_read_unlock(struct zspage *zspage) {}
330static void kick_deferred_free(struct zs_pool *pool) {}
331static void init_deferred_free(struct zs_pool *pool) {}
332static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
333#endif
334
Minchan Kim37836892016-07-26 15:23:23 -0700335static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700336{
337 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
338 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700339 if (!pool->handle_cachep)
340 return 1;
341
342 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
343 0, 0, NULL);
344 if (!pool->zspage_cachep) {
345 kmem_cache_destroy(pool->handle_cachep);
346 pool->handle_cachep = NULL;
347 return 1;
348 }
349
350 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700351}
352
Minchan Kim37836892016-07-26 15:23:23 -0700353static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700354{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700355 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700356 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700357}
358
Minchan Kim37836892016-07-26 15:23:23 -0700359static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700360{
361 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700362 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700363}
364
Minchan Kim37836892016-07-26 15:23:23 -0700365static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700366{
367 kmem_cache_free(pool->handle_cachep, (void *)handle);
368}
369
Minchan Kim37836892016-07-26 15:23:23 -0700370static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
371{
Minchan Kim48b48002016-07-26 15:23:31 -0700372 return kmem_cache_alloc(pool->zspage_cachep,
373 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim37836892016-07-26 15:23:23 -0700374};
375
376static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
377{
378 kmem_cache_free(pool->zspage_cachep, zspage);
379}
380
Minchan Kim2e40e162015-04-15 16:15:23 -0700381static void record_obj(unsigned long handle, unsigned long obj)
382{
Junil Leec102f072016-01-20 14:58:18 -0800383 /*
384 * lsb of @obj represents handle lock while other bits
385 * represent object value the handle is pointing so
386 * updating shouldn't do store tearing.
387 */
388 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700389}
390
Dan Streetmanc7957792014-08-06 16:08:38 -0700391/* zpool driver */
392
393#ifdef CONFIG_ZPOOL
394
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800395static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700396 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700397 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700398{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700399 /*
400 * Ignore global gfp flags: zs_malloc() may be invoked from
401 * different contexts and its caller must provide a valid
402 * gfp mask.
403 */
404 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700405}
406
407static void zs_zpool_destroy(void *pool)
408{
409 zs_destroy_pool(pool);
410}
411
412static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
413 unsigned long *handle)
414{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700415 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700416 return *handle ? 0 : -1;
417}
418static void zs_zpool_free(void *pool, unsigned long handle)
419{
420 zs_free(pool, handle);
421}
422
423static int zs_zpool_shrink(void *pool, unsigned int pages,
424 unsigned int *reclaimed)
425{
426 return -EINVAL;
427}
428
429static void *zs_zpool_map(void *pool, unsigned long handle,
430 enum zpool_mapmode mm)
431{
432 enum zs_mapmode zs_mm;
433
434 switch (mm) {
435 case ZPOOL_MM_RO:
436 zs_mm = ZS_MM_RO;
437 break;
438 case ZPOOL_MM_WO:
439 zs_mm = ZS_MM_WO;
440 break;
441 case ZPOOL_MM_RW: /* fallthru */
442 default:
443 zs_mm = ZS_MM_RW;
444 break;
445 }
446
447 return zs_map_object(pool, handle, zs_mm);
448}
449static void zs_zpool_unmap(void *pool, unsigned long handle)
450{
451 zs_unmap_object(pool, handle);
452}
453
454static u64 zs_zpool_total_size(void *pool)
455{
Minchan Kim722cdc12014-10-09 15:29:50 -0700456 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700457}
458
459static struct zpool_driver zs_zpool_driver = {
460 .type = "zsmalloc",
461 .owner = THIS_MODULE,
462 .create = zs_zpool_create,
463 .destroy = zs_zpool_destroy,
464 .malloc = zs_zpool_malloc,
465 .free = zs_zpool_free,
466 .shrink = zs_zpool_shrink,
467 .map = zs_zpool_map,
468 .unmap = zs_zpool_unmap,
469 .total_size = zs_zpool_total_size,
470};
471
Kees Cook137f8cf2014-08-29 15:18:40 -0700472MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700473#endif /* CONFIG_ZPOOL */
474
Nitin Gupta61989a82012-01-09 16:51:56 -0600475/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
476static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
477
Minchan Kim48b48002016-07-26 15:23:31 -0700478static bool is_zspage_isolated(struct zspage *zspage)
479{
480 return zspage->isolated;
481}
482
Nick Desaulniers5ac69182017-07-10 15:47:26 -0700483static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600484{
Minchan Kima27545bf2012-04-25 15:23:09 +0900485 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600486}
487
Minchan Kim48b48002016-07-26 15:23:31 -0700488/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700489static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600490{
Minchan Kim37836892016-07-26 15:23:23 -0700491 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600492}
493
Minchan Kim37836892016-07-26 15:23:23 -0700494static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700495{
Minchan Kim37836892016-07-26 15:23:23 -0700496 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700497}
498
Minchan Kim37836892016-07-26 15:23:23 -0700499static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700500{
Minchan Kim37836892016-07-26 15:23:23 -0700501 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700502}
503
Minchan Kim48b48002016-07-26 15:23:31 -0700504static inline struct page *get_first_page(struct zspage *zspage)
505{
506 struct page *first_page = zspage->first_page;
507
508 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
509 return first_page;
510}
511
Minchan Kim4f420472016-07-26 15:23:17 -0700512static inline int get_first_obj_offset(struct page *page)
513{
Minchan Kim48b48002016-07-26 15:23:31 -0700514 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700515}
516
517static inline void set_first_obj_offset(struct page *page, int offset)
518{
Minchan Kim48b48002016-07-26 15:23:31 -0700519 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700520}
521
Minchan Kimbfd093f2016-07-26 15:23:28 -0700522static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700523{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700524 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700525}
526
Minchan Kimbfd093f2016-07-26 15:23:28 -0700527static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700528{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700529 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700530}
531
Minchan Kim37836892016-07-26 15:23:23 -0700532static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700533 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600534 enum fullness_group *fullness)
535{
Minchan Kim48b48002016-07-26 15:23:31 -0700536 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
537
Minchan Kim37836892016-07-26 15:23:23 -0700538 *fullness = zspage->fullness;
539 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600540}
541
Minchan Kim37836892016-07-26 15:23:23 -0700542static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700543 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600544 enum fullness_group fullness)
545{
Minchan Kim37836892016-07-26 15:23:23 -0700546 zspage->class = class_idx;
547 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600548}
549
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900550/*
551 * zsmalloc divides the pool into various size classes where each
552 * class maintains a list of zspages where each zspage is divided
553 * into equal sized chunks. Each allocation falls into one of these
554 * classes depending on its size. This function returns index of the
555 * size class which has chunk size big enough to hold the give size.
556 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600557static int get_size_class_index(int size)
558{
559 int idx = 0;
560
561 if (likely(size > ZS_MIN_ALLOC_SIZE))
562 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
563 ZS_SIZE_CLASS_DELTA);
564
Minchan Kim7b60a682015-04-15 16:15:39 -0700565 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600566}
567
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700568/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700569static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700570 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700571{
Minchan Kim48b48002016-07-26 15:23:31 -0700572 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700573}
574
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700575/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700576static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700577 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700578{
Minchan Kim48b48002016-07-26 15:23:31 -0700579 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700580}
581
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700582/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700583static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700584 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700585{
Minchan Kim48b48002016-07-26 15:23:31 -0700586 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700587}
588
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700589#ifdef CONFIG_ZSMALLOC_STAT
590
Dan Streetman4abaac92016-05-26 15:16:27 -0700591static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700592{
Dan Streetman4abaac92016-05-26 15:16:27 -0700593 if (!debugfs_initialized()) {
594 pr_warn("debugfs not available, stat dir not created\n");
595 return;
596 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700597
598 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
599 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700600 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601}
602
603static void __exit zs_stat_exit(void)
604{
605 debugfs_remove_recursive(zs_stat_root);
606}
607
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700608static unsigned long zs_can_compact(struct size_class *class);
609
Minchan Kim248ca1b2015-04-15 16:15:42 -0700610static int zs_stats_size_show(struct seq_file *s, void *v)
611{
612 int i;
613 struct zs_pool *pool = s->private;
614 struct size_class *class;
615 int objs_per_zspage;
616 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700617 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700618 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
619 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700620 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700621
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700622 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700623 "class", "size", "almost_full", "almost_empty",
624 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700625 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700626
627 for (i = 0; i < zs_size_classes; i++) {
628 class = pool->size_class[i];
629
630 if (class->index != i)
631 continue;
632
633 spin_lock(&class->lock);
634 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
635 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
636 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
637 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700638 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700639 spin_unlock(&class->lock);
640
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700641 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700642 pages_used = obj_allocated / objs_per_zspage *
643 class->pages_per_zspage;
644
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700645 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
646 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700647 i, class->size, class_almost_full, class_almost_empty,
648 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700649 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700650
651 total_class_almost_full += class_almost_full;
652 total_class_almost_empty += class_almost_empty;
653 total_objs += obj_allocated;
654 total_used_objs += obj_used;
655 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700656 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700657 }
658
659 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700660 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700661 "Total", "", total_class_almost_full,
662 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700663 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700664
665 return 0;
666}
667
668static int zs_stats_size_open(struct inode *inode, struct file *file)
669{
670 return single_open(file, zs_stats_size_show, inode->i_private);
671}
672
673static const struct file_operations zs_stat_size_ops = {
674 .open = zs_stats_size_open,
675 .read = seq_read,
676 .llseek = seq_lseek,
677 .release = single_release,
678};
679
Dan Streetmand34f6152016-05-20 16:59:56 -0700680static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700681{
682 struct dentry *entry;
683
Dan Streetman4abaac92016-05-26 15:16:27 -0700684 if (!zs_stat_root) {
685 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700686 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700687 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700688
689 entry = debugfs_create_dir(name, zs_stat_root);
690 if (!entry) {
691 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700692 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700693 }
694 pool->stat_dentry = entry;
695
696 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
697 pool->stat_dentry, pool, &zs_stat_size_ops);
698 if (!entry) {
699 pr_warn("%s: debugfs file entry <%s> creation failed\n",
700 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700701 debugfs_remove_recursive(pool->stat_dentry);
702 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700703 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700704}
705
706static void zs_pool_stat_destroy(struct zs_pool *pool)
707{
708 debugfs_remove_recursive(pool->stat_dentry);
709}
710
711#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700712static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700713{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700714}
715
716static void __exit zs_stat_exit(void)
717{
718}
719
Dan Streetmand34f6152016-05-20 16:59:56 -0700720static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700721{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700722}
723
724static inline void zs_pool_stat_destroy(struct zs_pool *pool)
725{
726}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700727#endif
728
Minchan Kim48b48002016-07-26 15:23:31 -0700729
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900730/*
731 * For each size class, zspages are divided into different groups
732 * depending on how "full" they are. This was done so that we could
733 * easily find empty or nearly empty zspages when we try to shrink
734 * the pool (not yet implemented). This function returns fullness
735 * status of the given page.
736 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700737static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700738 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600739{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700740 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600741 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700742
Minchan Kim37836892016-07-26 15:23:23 -0700743 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700744 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600745
746 if (inuse == 0)
747 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700748 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600749 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700750 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600751 fg = ZS_ALMOST_EMPTY;
752 else
753 fg = ZS_ALMOST_FULL;
754
755 return fg;
756}
757
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900758/*
759 * Each size class maintains various freelists and zspages are assigned
760 * to one of these freelists based on the number of live objects they
761 * have. This functions inserts the given zspage into the freelist
762 * identified by <class, fullness_group>.
763 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700764static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700765 struct zspage *zspage,
766 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600767{
Minchan Kim37836892016-07-26 15:23:23 -0700768 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600769
Minchan Kim48b48002016-07-26 15:23:31 -0700770 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700771 head = list_first_entry_or_null(&class->fullness_list[fullness],
772 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700773 /*
Minchan Kim37836892016-07-26 15:23:23 -0700774 * We want to see more ZS_FULL pages and less almost empty/full.
775 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700776 */
Minchan Kim37836892016-07-26 15:23:23 -0700777 if (head) {
778 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
779 list_add(&zspage->list, &head->list);
780 return;
781 }
782 }
783 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600784}
785
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900786/*
787 * This function removes the given zspage from the freelist identified
788 * by <class, fullness_group>.
789 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700790static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700791 struct zspage *zspage,
792 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600793{
Minchan Kim37836892016-07-26 15:23:23 -0700794 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700795 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600796
Minchan Kim37836892016-07-26 15:23:23 -0700797 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700798 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600799}
800
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900801/*
802 * Each size class maintains zspages in different fullness groups depending
803 * on the number of live objects they contain. When allocating or freeing
804 * objects, the fullness status of the page can change, say, from ALMOST_FULL
805 * to ALMOST_EMPTY when freeing an object. This function checks if such
806 * a status change has occurred for the given page and accordingly moves the
807 * page from the freelist of the old fullness group to that of the new
808 * fullness group.
809 */
Minchan Kimc7806262015-04-15 16:15:26 -0700810static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700811 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600812{
813 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600814 enum fullness_group currfg, newfg;
815
Minchan Kim37836892016-07-26 15:23:23 -0700816 get_zspage_mapping(zspage, &class_idx, &currfg);
817 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600818 if (newfg == currfg)
819 goto out;
820
Minchan Kim48b48002016-07-26 15:23:31 -0700821 if (!is_zspage_isolated(zspage)) {
822 remove_zspage(class, zspage, currfg);
823 insert_zspage(class, zspage, newfg);
824 }
825
Minchan Kim37836892016-07-26 15:23:23 -0700826 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600827
828out:
829 return newfg;
830}
831
832/*
833 * We have to decide on how many pages to link together
834 * to form a zspage for each size class. This is important
835 * to reduce wastage due to unusable space left at end of
836 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700837 * wastage = Zp % class_size
838 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600839 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
840 *
841 * For example, for size class of 3/8 * PAGE_SIZE, we should
842 * link together 3 PAGE_SIZE sized pages to form a zspage
843 * since then we can perfectly fit in 8 such objects.
844 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900845static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600846{
847 int i, max_usedpc = 0;
848 /* zspage order which gives maximum used size per KB */
849 int max_usedpc_order = 1;
850
Seth Jennings84d4faa2012-03-05 11:33:21 -0600851 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600852 int zspage_size;
853 int waste, usedpc;
854
855 zspage_size = i * PAGE_SIZE;
856 waste = zspage_size % class_size;
857 usedpc = (zspage_size - waste) * 100 / zspage_size;
858
859 if (usedpc > max_usedpc) {
860 max_usedpc = usedpc;
861 max_usedpc_order = i;
862 }
863 }
864
865 return max_usedpc_order;
866}
867
Minchan Kim37836892016-07-26 15:23:23 -0700868static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600869{
Minchan Kim48b48002016-07-26 15:23:31 -0700870 struct zspage *zspage = (struct zspage *)page->private;
871
872 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
873 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600874}
875
876static struct page *get_next_page(struct page *page)
877{
Minchan Kim48b48002016-07-26 15:23:31 -0700878 if (unlikely(PageHugeObject(page)))
879 return NULL;
880
881 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600882}
883
Minchan Kimbfd093f2016-07-26 15:23:28 -0700884/**
885 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
886 * @page: page object resides in zspage
887 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800888 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700889static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700890 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600891{
Minchan Kim312fcae2015-04-15 16:15:30 -0700892 obj >>= OBJ_TAG_BITS;
893 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
894 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600895}
896
Minchan Kimbfd093f2016-07-26 15:23:28 -0700897/**
898 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
899 * @page: page object resides in zspage
900 * @obj_idx: object index
901 */
902static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
903{
904 unsigned long obj;
905
906 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
907 obj |= obj_idx & OBJ_INDEX_MASK;
908 obj <<= OBJ_TAG_BITS;
909
910 return obj;
911}
912
Minchan Kim2e40e162015-04-15 16:15:23 -0700913static unsigned long handle_to_obj(unsigned long handle)
914{
915 return *(unsigned long *)handle;
916}
917
Minchan Kim48b48002016-07-26 15:23:31 -0700918static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700919{
Minchan Kim48b48002016-07-26 15:23:31 -0700920 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700921 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700922 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700923 } else
924 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700925}
926
Minchan Kim48b48002016-07-26 15:23:31 -0700927static inline int testpin_tag(unsigned long handle)
928{
929 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
930}
931
Minchan Kim312fcae2015-04-15 16:15:30 -0700932static inline int trypin_tag(unsigned long handle)
933{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700934 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700935}
936
937static void pin_tag(unsigned long handle)
938{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700939 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700940}
941
942static void unpin_tag(unsigned long handle)
943{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700944 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700945}
946
Nitin Guptaf4477e92012-04-02 09:13:56 -0500947static void reset_page(struct page *page)
948{
Minchan Kim48b48002016-07-26 15:23:31 -0700949 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700950 ClearPagePrivate(page);
951 ClearPagePrivate2(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500952 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700953 page_mapcount_reset(page);
954 ClearPageHugeObject(page);
955 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500956}
957
Minchan Kim48b48002016-07-26 15:23:31 -0700958/*
959 * To prevent zspage destroy during migration, zspage freeing should
960 * hold locks of all pages in the zspage.
961 */
962void lock_zspage(struct zspage *zspage)
963{
964 struct page *page = get_first_page(zspage);
965
966 do {
967 lock_page(page);
968 } while ((page = get_next_page(page)) != NULL);
969}
970
971int trylock_zspage(struct zspage *zspage)
972{
973 struct page *cursor, *fail;
974
975 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
976 get_next_page(cursor)) {
977 if (!trylock_page(cursor)) {
978 fail = cursor;
979 goto unlock;
980 }
981 }
982
983 return 1;
984unlock:
985 for (cursor = get_first_page(zspage); cursor != fail; cursor =
986 get_next_page(cursor))
987 unlock_page(cursor);
988
989 return 0;
990}
991
992static void __free_zspage(struct zs_pool *pool, struct size_class *class,
993 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600994{
Minchan Kim37836892016-07-26 15:23:23 -0700995 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700996 enum fullness_group fg;
997 unsigned int class_idx;
998
999 get_zspage_mapping(zspage, &class_idx, &fg);
1000
1001 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -06001002
Minchan Kim37836892016-07-26 15:23:23 -07001003 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001004 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -06001005
Minchan Kim48b48002016-07-26 15:23:31 -07001006 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001007 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001008 VM_BUG_ON_PAGE(!PageLocked(page), page);
1009 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001010 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001011 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001012 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001013 put_page(page);
1014 page = next;
1015 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001016
Minchan Kim37836892016-07-26 15:23:23 -07001017 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001018
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001019 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001020 atomic_long_sub(class->pages_per_zspage,
1021 &pool->pages_allocated);
1022}
1023
1024static void free_zspage(struct zs_pool *pool, struct size_class *class,
1025 struct zspage *zspage)
1026{
1027 VM_BUG_ON(get_zspage_inuse(zspage));
1028 VM_BUG_ON(list_empty(&zspage->list));
1029
1030 if (!trylock_zspage(zspage)) {
1031 kick_deferred_free(pool);
1032 return;
1033 }
1034
1035 remove_zspage(class, zspage, ZS_EMPTY);
1036 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001037}
1038
1039/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001040static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001041{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001042 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001043 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001044 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001045
Nitin Gupta61989a82012-01-09 16:51:56 -06001046 while (page) {
1047 struct page *next_page;
1048 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001049 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001050
Minchan Kim37836892016-07-26 15:23:23 -07001051 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001052
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001053 vaddr = kmap_atomic(page);
1054 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001055
Dan Streetman5538c562014-10-09 15:30:01 -07001056 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001057 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001058 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001059 }
1060
1061 /*
1062 * We now come to the last (full or partial) object on this
1063 * page, which must point to the first object on the next
1064 * page (if present)
1065 */
1066 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001067 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001068 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001069 } else {
1070 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001071 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001072 * whether it's allocated object or not.
1073 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001074 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001075 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001076 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001077 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001078 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001079 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001080
Minchan Kimbfd093f2016-07-26 15:23:28 -07001081 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001082}
1083
Minchan Kim48b48002016-07-26 15:23:31 -07001084static void create_page_chain(struct size_class *class, struct zspage *zspage,
1085 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001086{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001087 int i;
1088 struct page *page;
1089 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001090 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001091
1092 /*
1093 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001094 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001095 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001096 *
Minchan Kim37836892016-07-26 15:23:23 -07001097 * we set PG_private to identify the first page (i.e. no other sub-page
1098 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001099 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001100 for (i = 0; i < nr_pages; i++) {
1101 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001102 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001103 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001104 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001105 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001106 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001107 if (unlikely(class->objs_per_zspage == 1 &&
1108 class->pages_per_zspage == 1))
1109 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001110 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001111 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001112 }
Minchan Kim48b48002016-07-26 15:23:31 -07001113 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001114 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001115 prev_page = page;
1116 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001117}
Nitin Gupta61989a82012-01-09 16:51:56 -06001118
Minchan Kimbdb0af72016-07-26 15:23:20 -07001119/*
1120 * Allocate a zspage for the given size class
1121 */
Minchan Kim37836892016-07-26 15:23:23 -07001122static struct zspage *alloc_zspage(struct zs_pool *pool,
1123 struct size_class *class,
1124 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001125{
1126 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001127 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001128 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1129
1130 if (!zspage)
1131 return NULL;
1132
1133 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001134 zspage->magic = ZSPAGE_MAGIC;
1135 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001136
Minchan Kimbdb0af72016-07-26 15:23:20 -07001137 for (i = 0; i < class->pages_per_zspage; i++) {
1138 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001139
Minchan Kim37836892016-07-26 15:23:23 -07001140 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001141 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001142 while (--i >= 0) {
1143 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001144 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001145 }
Minchan Kim37836892016-07-26 15:23:23 -07001146 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001147 return NULL;
1148 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001149
1150 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001151 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001152 }
1153
Minchan Kim48b48002016-07-26 15:23:31 -07001154 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001155 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001156
Minchan Kim37836892016-07-26 15:23:23 -07001157 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001158}
1159
Minchan Kim37836892016-07-26 15:23:23 -07001160static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001161{
1162 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001163 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001164
Minchan Kim48b48002016-07-26 15:23:31 -07001165 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001166 zspage = list_first_entry_or_null(&class->fullness_list[i],
1167 struct zspage, list);
1168 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001169 break;
1170 }
1171
Minchan Kim37836892016-07-26 15:23:23 -07001172 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001173}
1174
Minchan Kim1b945ae2013-12-11 11:04:36 +09001175#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001176static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001177{
Seth Jenningsf5536462012-07-18 11:55:56 -05001178 /*
1179 * Make sure we don't leak memory if a cpu UP notification
1180 * and zs_init() race and both call zs_cpu_up() on the same cpu
1181 */
1182 if (area->vm)
1183 return 0;
1184 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1185 if (!area->vm)
1186 return -ENOMEM;
1187 return 0;
1188}
1189
1190static inline void __zs_cpu_down(struct mapping_area *area)
1191{
1192 if (area->vm)
1193 free_vm_area(area->vm);
1194 area->vm = NULL;
1195}
1196
1197static inline void *__zs_map_object(struct mapping_area *area,
1198 struct page *pages[2], int off, int size)
1199{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001200 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001201 area->vm_addr = area->vm->addr;
1202 return area->vm_addr + off;
1203}
1204
1205static inline void __zs_unmap_object(struct mapping_area *area,
1206 struct page *pages[2], int off, int size)
1207{
1208 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001209
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001210 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001211}
1212
Minchan Kim1b945ae2013-12-11 11:04:36 +09001213#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001214
1215static inline int __zs_cpu_up(struct mapping_area *area)
1216{
1217 /*
1218 * Make sure we don't leak memory if a cpu UP notification
1219 * and zs_init() race and both call zs_cpu_up() on the same cpu
1220 */
1221 if (area->vm_buf)
1222 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001223 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001224 if (!area->vm_buf)
1225 return -ENOMEM;
1226 return 0;
1227}
1228
1229static inline void __zs_cpu_down(struct mapping_area *area)
1230{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001231 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001232 area->vm_buf = NULL;
1233}
1234
1235static void *__zs_map_object(struct mapping_area *area,
1236 struct page *pages[2], int off, int size)
1237{
Seth Jennings5f601902012-07-02 16:15:49 -05001238 int sizes[2];
1239 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001240 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001241
Seth Jenningsf5536462012-07-18 11:55:56 -05001242 /* disable page faults to match kmap_atomic() return conditions */
1243 pagefault_disable();
1244
1245 /* no read fastpath */
1246 if (area->vm_mm == ZS_MM_WO)
1247 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001248
1249 sizes[0] = PAGE_SIZE - off;
1250 sizes[1] = size - sizes[0];
1251
Seth Jennings5f601902012-07-02 16:15:49 -05001252 /* copy object to per-cpu buffer */
1253 addr = kmap_atomic(pages[0]);
1254 memcpy(buf, addr + off, sizes[0]);
1255 kunmap_atomic(addr);
1256 addr = kmap_atomic(pages[1]);
1257 memcpy(buf + sizes[0], addr, sizes[1]);
1258 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001259out:
1260 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001261}
1262
Seth Jenningsf5536462012-07-18 11:55:56 -05001263static void __zs_unmap_object(struct mapping_area *area,
1264 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001265{
Seth Jennings5f601902012-07-02 16:15:49 -05001266 int sizes[2];
1267 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001268 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001269
Seth Jenningsf5536462012-07-18 11:55:56 -05001270 /* no write fastpath */
1271 if (area->vm_mm == ZS_MM_RO)
1272 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001273
Minchan Kim7b60a682015-04-15 16:15:39 -07001274 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001275 buf = buf + ZS_HANDLE_SIZE;
1276 size -= ZS_HANDLE_SIZE;
1277 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001278
Seth Jennings5f601902012-07-02 16:15:49 -05001279 sizes[0] = PAGE_SIZE - off;
1280 sizes[1] = size - sizes[0];
1281
1282 /* copy per-cpu buffer to object */
1283 addr = kmap_atomic(pages[0]);
1284 memcpy(addr + off, buf, sizes[0]);
1285 kunmap_atomic(addr);
1286 addr = kmap_atomic(pages[1]);
1287 memcpy(addr, buf + sizes[0], sizes[1]);
1288 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001289
1290out:
1291 /* enable page faults to match kunmap_atomic() return conditions */
1292 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001293}
Nitin Gupta61989a82012-01-09 16:51:56 -06001294
Minchan Kim1b945ae2013-12-11 11:04:36 +09001295#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001296
Nitin Gupta61989a82012-01-09 16:51:56 -06001297static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1298 void *pcpu)
1299{
Seth Jenningsf5536462012-07-18 11:55:56 -05001300 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001301 struct mapping_area *area;
1302
1303 switch (action) {
1304 case CPU_UP_PREPARE:
1305 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001306 ret = __zs_cpu_up(area);
1307 if (ret)
1308 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001309 break;
1310 case CPU_DEAD:
1311 case CPU_UP_CANCELED:
1312 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001313 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001314 break;
1315 }
1316
1317 return NOTIFY_OK;
1318}
1319
1320static struct notifier_block zs_cpu_nb = {
1321 .notifier_call = zs_cpu_notifier
1322};
1323
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001324static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001325{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001326 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001327
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301328 cpu_notifier_register_begin();
1329
1330 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001331 for_each_online_cpu(cpu) {
1332 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001333 if (notifier_to_errno(ret))
1334 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001335 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301336
1337 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001338 return notifier_to_errno(ret);
1339}
1340
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001341static void zs_unregister_cpu_notifier(void)
1342{
1343 int cpu;
1344
1345 cpu_notifier_register_begin();
1346
1347 for_each_online_cpu(cpu)
1348 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1349 __unregister_cpu_notifier(&zs_cpu_nb);
1350
1351 cpu_notifier_register_done();
1352}
1353
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001354static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001355{
1356 int nr;
1357
1358 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1359 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1360 nr += 1;
1361
1362 zs_size_classes = nr;
1363}
1364
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001365static bool can_merge(struct size_class *prev, int pages_per_zspage,
1366 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001367{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001368 if (prev->pages_per_zspage == pages_per_zspage &&
1369 prev->objs_per_zspage == objs_per_zspage)
1370 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001371
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001372 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001373}
1374
Minchan Kim37836892016-07-26 15:23:23 -07001375static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001376{
Minchan Kim37836892016-07-26 15:23:23 -07001377 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001378}
1379
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001380unsigned long zs_get_total_pages(struct zs_pool *pool)
1381{
1382 return atomic_long_read(&pool->pages_allocated);
1383}
1384EXPORT_SYMBOL_GPL(zs_get_total_pages);
1385
1386/**
1387 * zs_map_object - get address of allocated object from handle.
1388 * @pool: pool from which the object was allocated
1389 * @handle: handle returned from zs_malloc
1390 *
1391 * Before using an object allocated from zs_malloc, it must be mapped using
1392 * this function. When done with the object, it must be unmapped using
1393 * zs_unmap_object.
1394 *
1395 * Only one object can be mapped per cpu at a time. There is no protection
1396 * against nested mappings.
1397 *
1398 * This function returns with preemption and page faults disabled.
1399 */
1400void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1401 enum zs_mapmode mm)
1402{
Minchan Kim37836892016-07-26 15:23:23 -07001403 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001404 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001405 unsigned long obj, off;
1406 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001407
1408 unsigned int class_idx;
1409 enum fullness_group fg;
1410 struct size_class *class;
1411 struct mapping_area *area;
1412 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001413 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001414
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001415 /*
1416 * Because we use per-cpu mapping areas shared among the
1417 * pools/users, we can't allow mapping in interrupt context
1418 * because it can corrupt another users mappings.
1419 */
Sergey Senozhatsky16184002017-11-15 17:34:03 -08001420 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001421
Minchan Kim312fcae2015-04-15 16:15:30 -07001422 /* From now on, migration cannot move the object */
1423 pin_tag(handle);
1424
Minchan Kim2e40e162015-04-15 16:15:23 -07001425 obj = handle_to_obj(handle);
1426 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001427 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001428
1429 /* migration cannot move any subpage in this zspage */
1430 migrate_read_lock(zspage);
1431
Minchan Kim37836892016-07-26 15:23:23 -07001432 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001433 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001434 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001435
1436 area = &get_cpu_var(zs_map_area);
1437 area->vm_mm = mm;
1438 if (off + class->size <= PAGE_SIZE) {
1439 /* this object is contained entirely within a page */
1440 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001441 ret = area->vm_addr + off;
1442 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001443 }
1444
1445 /* this object spans two pages */
1446 pages[0] = page;
1447 pages[1] = get_next_page(page);
1448 BUG_ON(!pages[1]);
1449
Minchan Kim2e40e162015-04-15 16:15:23 -07001450 ret = __zs_map_object(area, pages, off, class->size);
1451out:
Minchan Kim48b48002016-07-26 15:23:31 -07001452 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001453 ret += ZS_HANDLE_SIZE;
1454
1455 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001456}
1457EXPORT_SYMBOL_GPL(zs_map_object);
1458
1459void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1460{
Minchan Kim37836892016-07-26 15:23:23 -07001461 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001462 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001463 unsigned long obj, off;
1464 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001465
1466 unsigned int class_idx;
1467 enum fullness_group fg;
1468 struct size_class *class;
1469 struct mapping_area *area;
1470
Minchan Kim2e40e162015-04-15 16:15:23 -07001471 obj = handle_to_obj(handle);
1472 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001473 zspage = get_zspage(page);
1474 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001475 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001476 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001477
1478 area = this_cpu_ptr(&zs_map_area);
1479 if (off + class->size <= PAGE_SIZE)
1480 kunmap_atomic(area->vm_addr);
1481 else {
1482 struct page *pages[2];
1483
1484 pages[0] = page;
1485 pages[1] = get_next_page(page);
1486 BUG_ON(!pages[1]);
1487
1488 __zs_unmap_object(area, pages, off, class->size);
1489 }
1490 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001491
1492 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001493 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001494}
1495EXPORT_SYMBOL_GPL(zs_unmap_object);
1496
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07001497/**
1498 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1499 * zsmalloc &size_class.
1500 * @pool: zsmalloc pool to use
1501 *
1502 * The function returns the size of the first huge class - any object of equal
1503 * or bigger size will be stored in zspage consisting of a single physical
1504 * page.
1505 *
1506 * Context: Any context.
1507 *
1508 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1509 */
1510size_t zs_huge_class_size(struct zs_pool *pool)
1511{
1512 return huge_class_size;
1513}
1514EXPORT_SYMBOL_GPL(zs_huge_class_size);
1515
Minchan Kim251cbb92016-05-20 16:59:42 -07001516static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001517 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001518{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001519 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001520 unsigned long obj;
1521 struct link_free *link;
1522
1523 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001524 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001525 void *vaddr;
1526
Minchan Kim312fcae2015-04-15 16:15:30 -07001527 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001528 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001529
1530 offset = obj * class->size;
1531 nr_page = offset >> PAGE_SHIFT;
1532 m_offset = offset & ~PAGE_MASK;
1533 m_page = get_first_page(zspage);
1534
1535 for (i = 0; i < nr_page; i++)
1536 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001537
1538 vaddr = kmap_atomic(m_page);
1539 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001540 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001541 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001542 /* record handle in the header of allocated chunk */
1543 link->handle = handle;
1544 else
Minchan Kim37836892016-07-26 15:23:23 -07001545 /* record handle to page->index */
1546 zspage->first_page->index = handle;
1547
Minchan Kimc7806262015-04-15 16:15:26 -07001548 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001549 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001550 zs_stat_inc(class, OBJ_USED, 1);
1551
Minchan Kimbfd093f2016-07-26 15:23:28 -07001552 obj = location_to_obj(m_page, obj);
1553
Minchan Kimc7806262015-04-15 16:15:26 -07001554 return obj;
1555}
1556
1557
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001558/**
1559 * zs_malloc - Allocate block of given size from pool.
1560 * @pool: pool to allocate from
1561 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001562 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001563 *
1564 * On success, handle to the allocated object is returned,
1565 * otherwise 0.
1566 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1567 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001568unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001569{
Minchan Kim2e40e162015-04-15 16:15:23 -07001570 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001571 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001572 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001573 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001574
Minchan Kim7b60a682015-04-15 16:15:39 -07001575 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001576 return 0;
1577
Minchan Kim37836892016-07-26 15:23:23 -07001578 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001579 if (!handle)
1580 return 0;
1581
1582 /* extra space in chunk to keep the handle */
1583 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001584 class = pool->size_class[get_size_class_index(size)];
1585
1586 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001587 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001588 if (likely(zspage)) {
1589 obj = obj_malloc(class, zspage, handle);
1590 /* Now move the zspage to another fullness group, if required */
1591 fix_fullness_group(class, zspage);
1592 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001593 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001594
Minchan Kim48b48002016-07-26 15:23:31 -07001595 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001596 }
1597
Minchan Kim48b48002016-07-26 15:23:31 -07001598 spin_unlock(&class->lock);
1599
1600 zspage = alloc_zspage(pool, class, gfp);
1601 if (!zspage) {
1602 cache_free_handle(pool, handle);
1603 return 0;
1604 }
1605
1606 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001607 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001608 newfg = get_fullness_group(class, zspage);
1609 insert_zspage(class, zspage, newfg);
1610 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001611 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001612 atomic_long_add(class->pages_per_zspage,
1613 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001614 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001615
1616 /* We completely set up zspage so mark them as movable */
1617 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001618 spin_unlock(&class->lock);
1619
Minchan Kim2e40e162015-04-15 16:15:23 -07001620 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001621}
1622EXPORT_SYMBOL_GPL(zs_malloc);
1623
Minchan Kim1ee47162016-05-20 16:59:45 -07001624static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001625{
1626 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001627 struct zspage *zspage;
1628 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001629 unsigned long f_offset;
1630 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001631 void *vaddr;
1632
Minchan Kim312fcae2015-04-15 16:15:30 -07001633 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001634 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001635 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001636 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001637
Minchan Kimc7806262015-04-15 16:15:26 -07001638 vaddr = kmap_atomic(f_page);
1639
1640 /* Insert this object in containing zspage's freelist */
1641 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001642 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001643 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001644 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001645 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001646 zs_stat_dec(class, OBJ_USED, 1);
1647}
1648
1649void zs_free(struct zs_pool *pool, unsigned long handle)
1650{
Minchan Kim37836892016-07-26 15:23:23 -07001651 struct zspage *zspage;
1652 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001653 unsigned long obj;
1654 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001655 int class_idx;
1656 struct size_class *class;
1657 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001658 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001659
Minchan Kim2e40e162015-04-15 16:15:23 -07001660 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001661 return;
1662
Minchan Kim312fcae2015-04-15 16:15:30 -07001663 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001664 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001665 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001666 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001667
Minchan Kim48b48002016-07-26 15:23:31 -07001668 migrate_read_lock(zspage);
1669
Minchan Kim37836892016-07-26 15:23:23 -07001670 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001671 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001672
1673 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001674 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001675 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001676 if (fullness != ZS_EMPTY) {
1677 migrate_read_unlock(zspage);
1678 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001679 }
Minchan Kim48b48002016-07-26 15:23:31 -07001680
1681 isolated = is_zspage_isolated(zspage);
1682 migrate_read_unlock(zspage);
1683 /* If zspage is isolated, zs_page_putback will free the zspage */
1684 if (likely(!isolated))
1685 free_zspage(pool, class, zspage);
1686out:
1687
Minchan Kim312fcae2015-04-15 16:15:30 -07001688 spin_unlock(&class->lock);
1689 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001690 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001691}
1692EXPORT_SYMBOL_GPL(zs_free);
1693
Minchan Kim251cbb92016-05-20 16:59:42 -07001694static void zs_object_copy(struct size_class *class, unsigned long dst,
1695 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001696{
1697 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001698 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001699 unsigned long s_off, d_off;
1700 void *s_addr, *d_addr;
1701 int s_size, d_size, size;
1702 int written = 0;
1703
1704 s_size = d_size = class->size;
1705
1706 obj_to_location(src, &s_page, &s_objidx);
1707 obj_to_location(dst, &d_page, &d_objidx);
1708
Minchan Kimbfd093f2016-07-26 15:23:28 -07001709 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1710 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001711
1712 if (s_off + class->size > PAGE_SIZE)
1713 s_size = PAGE_SIZE - s_off;
1714
1715 if (d_off + class->size > PAGE_SIZE)
1716 d_size = PAGE_SIZE - d_off;
1717
1718 s_addr = kmap_atomic(s_page);
1719 d_addr = kmap_atomic(d_page);
1720
1721 while (1) {
1722 size = min(s_size, d_size);
1723 memcpy(d_addr + d_off, s_addr + s_off, size);
1724 written += size;
1725
1726 if (written == class->size)
1727 break;
1728
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001729 s_off += size;
1730 s_size -= size;
1731 d_off += size;
1732 d_size -= size;
1733
1734 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001735 kunmap_atomic(d_addr);
1736 kunmap_atomic(s_addr);
1737 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001738 s_addr = kmap_atomic(s_page);
1739 d_addr = kmap_atomic(d_page);
1740 s_size = class->size - written;
1741 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001742 }
1743
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001744 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001745 kunmap_atomic(d_addr);
1746 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001747 d_addr = kmap_atomic(d_page);
1748 d_size = class->size - written;
1749 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001750 }
1751 }
1752
1753 kunmap_atomic(d_addr);
1754 kunmap_atomic(s_addr);
1755}
1756
1757/*
1758 * Find alloced object in zspage from index object and
1759 * return handle.
1760 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001761static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001762 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001763{
1764 unsigned long head;
1765 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001766 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001767 unsigned long handle = 0;
1768 void *addr = kmap_atomic(page);
1769
Minchan Kim37836892016-07-26 15:23:23 -07001770 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001771 offset += class->size * index;
1772
1773 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001774 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001775 if (head & OBJ_ALLOCATED_TAG) {
1776 handle = head & ~OBJ_ALLOCATED_TAG;
1777 if (trypin_tag(handle))
1778 break;
1779 handle = 0;
1780 }
1781
1782 offset += class->size;
1783 index++;
1784 }
1785
1786 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001787
1788 *obj_idx = index;
1789
Minchan Kim312fcae2015-04-15 16:15:30 -07001790 return handle;
1791}
1792
1793struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001794 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001795 struct page *s_page;
1796 /* Destination page for migration which should be a first page
1797 * of zspage. */
1798 struct page *d_page;
1799 /* Starting object index within @s_page which used for live object
1800 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001801 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001802};
1803
1804static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1805 struct zs_compact_control *cc)
1806{
1807 unsigned long used_obj, free_obj;
1808 unsigned long handle;
1809 struct page *s_page = cc->s_page;
1810 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001811 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001812 int ret = 0;
1813
1814 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001815 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001816 if (!handle) {
1817 s_page = get_next_page(s_page);
1818 if (!s_page)
1819 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001820 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001821 continue;
1822 }
1823
1824 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001825 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001826 unpin_tag(handle);
1827 ret = -ENOMEM;
1828 break;
1829 }
1830
1831 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001832 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001833 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001834 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001835 /*
1836 * record_obj updates handle's value to free_obj and it will
1837 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1838 * breaks synchronization using pin_tag(e,g, zs_free) so
1839 * let's keep the lock bit.
1840 */
1841 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001842 record_obj(handle, free_obj);
1843 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001844 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001845 }
1846
1847 /* Remember last position in this iteration */
1848 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001849 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001850
1851 return ret;
1852}
1853
Minchan Kim37836892016-07-26 15:23:23 -07001854static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001855{
1856 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001857 struct zspage *zspage;
1858 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001859
Minchan Kim37836892016-07-26 15:23:23 -07001860 if (!source) {
1861 fg[0] = ZS_ALMOST_FULL;
1862 fg[1] = ZS_ALMOST_EMPTY;
1863 }
1864
1865 for (i = 0; i < 2; i++) {
1866 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1867 struct zspage, list);
1868 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001869 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001870 remove_zspage(class, zspage, fg[i]);
1871 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001872 }
1873 }
1874
Minchan Kim37836892016-07-26 15:23:23 -07001875 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001876}
1877
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001878/*
Minchan Kim37836892016-07-26 15:23:23 -07001879 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001880 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001881 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001882 *
Minchan Kim37836892016-07-26 15:23:23 -07001883 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001884 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001885static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001886 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001887{
Minchan Kim312fcae2015-04-15 16:15:30 -07001888 enum fullness_group fullness;
1889
Minchan Kim48b48002016-07-26 15:23:31 -07001890 VM_BUG_ON(is_zspage_isolated(zspage));
1891
Minchan Kim37836892016-07-26 15:23:23 -07001892 fullness = get_fullness_group(class, zspage);
1893 insert_zspage(class, zspage, fullness);
1894 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001895
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001896 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001897}
1898
Minchan Kim48b48002016-07-26 15:23:31 -07001899#ifdef CONFIG_COMPACTION
1900static struct dentry *zs_mount(struct file_system_type *fs_type,
1901 int flags, const char *dev_name, void *data)
1902{
1903 static const struct dentry_operations ops = {
1904 .d_dname = simple_dname,
1905 };
1906
1907 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1908}
1909
1910static struct file_system_type zsmalloc_fs = {
1911 .name = "zsmalloc",
1912 .mount = zs_mount,
1913 .kill_sb = kill_anon_super,
1914};
1915
1916static int zsmalloc_mount(void)
1917{
1918 int ret = 0;
1919
1920 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1921 if (IS_ERR(zsmalloc_mnt))
1922 ret = PTR_ERR(zsmalloc_mnt);
1923
1924 return ret;
1925}
1926
1927static void zsmalloc_unmount(void)
1928{
1929 kern_unmount(zsmalloc_mnt);
1930}
1931
1932static void migrate_lock_init(struct zspage *zspage)
1933{
1934 rwlock_init(&zspage->lock);
1935}
1936
1937static void migrate_read_lock(struct zspage *zspage)
1938{
1939 read_lock(&zspage->lock);
1940}
1941
1942static void migrate_read_unlock(struct zspage *zspage)
1943{
1944 read_unlock(&zspage->lock);
1945}
1946
1947static void migrate_write_lock(struct zspage *zspage)
1948{
1949 write_lock(&zspage->lock);
1950}
1951
1952static void migrate_write_unlock(struct zspage *zspage)
1953{
1954 write_unlock(&zspage->lock);
1955}
1956
1957/* Number of isolated subpage for *page migration* in this zspage */
1958static void inc_zspage_isolation(struct zspage *zspage)
1959{
1960 zspage->isolated++;
1961}
1962
1963static void dec_zspage_isolation(struct zspage *zspage)
1964{
1965 zspage->isolated--;
1966}
1967
Henry Burns29295162019-08-24 17:55:03 -07001968static void putback_zspage_deferred(struct zs_pool *pool,
1969 struct size_class *class,
1970 struct zspage *zspage)
1971{
1972 enum fullness_group fg;
1973
1974 fg = putback_zspage(class, zspage);
1975 if (fg == ZS_EMPTY)
1976 schedule_work(&pool->free_work);
1977
1978}
1979
Henry Burns0e7e2732019-08-24 17:55:06 -07001980static inline void zs_pool_dec_isolated(struct zs_pool *pool)
1981{
1982 VM_BUG_ON(atomic_long_read(&pool->isolated_pages) <= 0);
1983 atomic_long_dec(&pool->isolated_pages);
1984 /*
Miaohe Linfdcd8b62021-11-05 13:45:03 -07001985 * Checking pool->destroying must happen after atomic_long_dec()
1986 * for pool->isolated_pages above. Paired with the smp_mb() in
1987 * zs_unregister_migration().
Henry Burns0e7e2732019-08-24 17:55:06 -07001988 */
Miaohe Linfdcd8b62021-11-05 13:45:03 -07001989 smp_mb__after_atomic();
Henry Burns0e7e2732019-08-24 17:55:06 -07001990 if (atomic_long_read(&pool->isolated_pages) == 0 && pool->destroying)
1991 wake_up_all(&pool->migration_wait);
1992}
1993
Minchan Kim48b48002016-07-26 15:23:31 -07001994static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1995 struct page *newpage, struct page *oldpage)
1996{
1997 struct page *page;
1998 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1999 int idx = 0;
2000
2001 page = get_first_page(zspage);
2002 do {
2003 if (page == oldpage)
2004 pages[idx] = newpage;
2005 else
2006 pages[idx] = page;
2007 idx++;
2008 } while ((page = get_next_page(page)) != NULL);
2009
2010 create_page_chain(class, zspage, pages);
2011 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
2012 if (unlikely(PageHugeObject(oldpage)))
2013 newpage->index = oldpage->index;
2014 __SetPageMovable(newpage, page_mapping(oldpage));
2015}
2016
2017bool zs_page_isolate(struct page *page, isolate_mode_t mode)
2018{
2019 struct zs_pool *pool;
2020 struct size_class *class;
2021 int class_idx;
2022 enum fullness_group fullness;
2023 struct zspage *zspage;
2024 struct address_space *mapping;
2025
2026 /*
2027 * Page is locked so zspage couldn't be destroyed. For detail, look at
2028 * lock_zspage in free_zspage.
2029 */
2030 VM_BUG_ON_PAGE(!PageMovable(page), page);
2031 VM_BUG_ON_PAGE(PageIsolated(page), page);
2032
2033 zspage = get_zspage(page);
2034
2035 /*
2036 * Without class lock, fullness could be stale while class_idx is okay
2037 * because class_idx is constant unless page is freed so we should get
2038 * fullness again under class lock.
2039 */
2040 get_zspage_mapping(zspage, &class_idx, &fullness);
2041 mapping = page_mapping(page);
2042 pool = mapping->private_data;
2043 class = pool->size_class[class_idx];
2044
2045 spin_lock(&class->lock);
2046 if (get_zspage_inuse(zspage) == 0) {
2047 spin_unlock(&class->lock);
2048 return false;
2049 }
2050
2051 /* zspage is isolated for object migration */
2052 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2053 spin_unlock(&class->lock);
2054 return false;
2055 }
2056
2057 /*
2058 * If this is first time isolation for the zspage, isolate zspage from
2059 * size_class to prevent further object allocation from the zspage.
2060 */
2061 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2062 get_zspage_mapping(zspage, &class_idx, &fullness);
Henry Burns0e7e2732019-08-24 17:55:06 -07002063 atomic_long_inc(&pool->isolated_pages);
Minchan Kim48b48002016-07-26 15:23:31 -07002064 remove_zspage(class, zspage, fullness);
2065 }
2066
2067 inc_zspage_isolation(zspage);
2068 spin_unlock(&class->lock);
2069
2070 return true;
2071}
2072
2073int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2074 struct page *page, enum migrate_mode mode)
2075{
2076 struct zs_pool *pool;
2077 struct size_class *class;
2078 int class_idx;
2079 enum fullness_group fullness;
2080 struct zspage *zspage;
2081 struct page *dummy;
2082 void *s_addr, *d_addr, *addr;
2083 int offset, pos;
2084 unsigned long handle, head;
2085 unsigned long old_obj, new_obj;
2086 unsigned int obj_idx;
2087 int ret = -EAGAIN;
2088
2089 VM_BUG_ON_PAGE(!PageMovable(page), page);
2090 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2091
2092 zspage = get_zspage(page);
2093
2094 /* Concurrent compactor cannot migrate any subpage in zspage */
2095 migrate_write_lock(zspage);
2096 get_zspage_mapping(zspage, &class_idx, &fullness);
2097 pool = mapping->private_data;
2098 class = pool->size_class[class_idx];
2099 offset = get_first_obj_offset(page);
2100
2101 spin_lock(&class->lock);
2102 if (!get_zspage_inuse(zspage)) {
2103 ret = -EBUSY;
2104 goto unlock_class;
2105 }
2106
2107 pos = offset;
2108 s_addr = kmap_atomic(page);
2109 while (pos < PAGE_SIZE) {
2110 head = obj_to_head(page, s_addr + pos);
2111 if (head & OBJ_ALLOCATED_TAG) {
2112 handle = head & ~OBJ_ALLOCATED_TAG;
2113 if (!trypin_tag(handle))
2114 goto unpin_objects;
2115 }
2116 pos += class->size;
2117 }
2118
2119 /*
2120 * Here, any user cannot access all objects in the zspage so let's move.
2121 */
2122 d_addr = kmap_atomic(newpage);
2123 memcpy(d_addr, s_addr, PAGE_SIZE);
2124 kunmap_atomic(d_addr);
2125
2126 for (addr = s_addr + offset; addr < s_addr + pos;
2127 addr += class->size) {
2128 head = obj_to_head(page, addr);
2129 if (head & OBJ_ALLOCATED_TAG) {
2130 handle = head & ~OBJ_ALLOCATED_TAG;
2131 if (!testpin_tag(handle))
2132 BUG();
2133
2134 old_obj = handle_to_obj(handle);
2135 obj_to_location(old_obj, &dummy, &obj_idx);
2136 new_obj = (unsigned long)location_to_obj(newpage,
2137 obj_idx);
2138 new_obj |= BIT(HANDLE_PIN_BIT);
2139 record_obj(handle, new_obj);
2140 }
2141 }
2142
2143 replace_sub_page(class, zspage, newpage, page);
2144 get_page(newpage);
2145
2146 dec_zspage_isolation(zspage);
2147
2148 /*
2149 * Page migration is done so let's putback isolated zspage to
2150 * the list if @page is final isolated subpage in the zspage.
2151 */
Henry Burns0e7e2732019-08-24 17:55:06 -07002152 if (!is_zspage_isolated(zspage)) {
2153 /*
2154 * We cannot race with zs_destroy_pool() here because we wait
2155 * for isolation to hit zero before we start destroying.
2156 * Also, we ensure that everyone can see pool->destroying before
2157 * we start waiting.
2158 */
Henry Burns29295162019-08-24 17:55:03 -07002159 putback_zspage_deferred(pool, class, zspage);
Henry Burns0e7e2732019-08-24 17:55:06 -07002160 zs_pool_dec_isolated(pool);
2161 }
Minchan Kim48b48002016-07-26 15:23:31 -07002162
Chanho Mindf5e2d32020-01-04 12:59:36 -08002163 if (page_zone(newpage) != page_zone(page)) {
2164 dec_zone_page_state(page, NR_ZSPAGES);
2165 inc_zone_page_state(newpage, NR_ZSPAGES);
2166 }
2167
Minchan Kim48b48002016-07-26 15:23:31 -07002168 reset_page(page);
2169 put_page(page);
2170 page = newpage;
2171
Minchan Kimdd4123f2016-07-26 15:26:50 -07002172 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002173unpin_objects:
2174 for (addr = s_addr + offset; addr < s_addr + pos;
2175 addr += class->size) {
2176 head = obj_to_head(page, addr);
2177 if (head & OBJ_ALLOCATED_TAG) {
2178 handle = head & ~OBJ_ALLOCATED_TAG;
2179 if (!testpin_tag(handle))
2180 BUG();
2181 unpin_tag(handle);
2182 }
2183 }
2184 kunmap_atomic(s_addr);
2185unlock_class:
2186 spin_unlock(&class->lock);
2187 migrate_write_unlock(zspage);
2188
2189 return ret;
2190}
2191
2192void zs_page_putback(struct page *page)
2193{
2194 struct zs_pool *pool;
2195 struct size_class *class;
2196 int class_idx;
2197 enum fullness_group fg;
2198 struct address_space *mapping;
2199 struct zspage *zspage;
2200
2201 VM_BUG_ON_PAGE(!PageMovable(page), page);
2202 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2203
2204 zspage = get_zspage(page);
2205 get_zspage_mapping(zspage, &class_idx, &fg);
2206 mapping = page_mapping(page);
2207 pool = mapping->private_data;
2208 class = pool->size_class[class_idx];
2209
2210 spin_lock(&class->lock);
2211 dec_zspage_isolation(zspage);
2212 if (!is_zspage_isolated(zspage)) {
Minchan Kim48b48002016-07-26 15:23:31 -07002213 /*
2214 * Due to page_lock, we cannot free zspage immediately
2215 * so let's defer.
2216 */
Henry Burns29295162019-08-24 17:55:03 -07002217 putback_zspage_deferred(pool, class, zspage);
Henry Burns0e7e2732019-08-24 17:55:06 -07002218 zs_pool_dec_isolated(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002219 }
2220 spin_unlock(&class->lock);
2221}
2222
2223const struct address_space_operations zsmalloc_aops = {
2224 .isolate_page = zs_page_isolate,
2225 .migratepage = zs_page_migrate,
2226 .putback_page = zs_page_putback,
2227};
2228
2229static int zs_register_migration(struct zs_pool *pool)
2230{
2231 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2232 if (IS_ERR(pool->inode)) {
2233 pool->inode = NULL;
2234 return 1;
2235 }
2236
2237 pool->inode->i_mapping->private_data = pool;
2238 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2239 return 0;
2240}
2241
Henry Burns0e7e2732019-08-24 17:55:06 -07002242static bool pool_isolated_are_drained(struct zs_pool *pool)
2243{
2244 return atomic_long_read(&pool->isolated_pages) == 0;
2245}
2246
2247/* Function for resolving migration */
2248static void wait_for_isolated_drain(struct zs_pool *pool)
2249{
2250
2251 /*
2252 * We're in the process of destroying the pool, so there are no
2253 * active allocations. zs_page_isolate() fails for completely free
2254 * zspages, so we need only wait for the zs_pool's isolated
2255 * count to hit zero.
2256 */
2257 wait_event(pool->migration_wait,
2258 pool_isolated_are_drained(pool));
2259}
2260
Minchan Kim48b48002016-07-26 15:23:31 -07002261static void zs_unregister_migration(struct zs_pool *pool)
2262{
Henry Burns0e7e2732019-08-24 17:55:06 -07002263 pool->destroying = true;
2264 /*
2265 * We need a memory barrier here to ensure global visibility of
2266 * pool->destroying. Thus pool->isolated pages will either be 0 in which
2267 * case we don't care, or it will be > 0 and pool->destroying will
2268 * ensure that we wake up once isolation hits 0.
2269 */
2270 smp_mb();
2271 wait_for_isolated_drain(pool); /* This can block */
Minchan Kim48b48002016-07-26 15:23:31 -07002272 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002273 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002274}
2275
2276/*
2277 * Caller should hold page_lock of all pages in the zspage
2278 * In here, we cannot use zspage meta data.
2279 */
2280static void async_free_zspage(struct work_struct *work)
2281{
2282 int i;
2283 struct size_class *class;
2284 unsigned int class_idx;
2285 enum fullness_group fullness;
2286 struct zspage *zspage, *tmp;
2287 LIST_HEAD(free_pages);
2288 struct zs_pool *pool = container_of(work, struct zs_pool,
2289 free_work);
2290
2291 for (i = 0; i < zs_size_classes; i++) {
2292 class = pool->size_class[i];
2293 if (class->index != i)
2294 continue;
2295
2296 spin_lock(&class->lock);
2297 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2298 spin_unlock(&class->lock);
2299 }
2300
2301
2302 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2303 list_del(&zspage->list);
2304 lock_zspage(zspage);
2305
2306 get_zspage_mapping(zspage, &class_idx, &fullness);
2307 VM_BUG_ON(fullness != ZS_EMPTY);
2308 class = pool->size_class[class_idx];
2309 spin_lock(&class->lock);
2310 __free_zspage(pool, pool->size_class[class_idx], zspage);
2311 spin_unlock(&class->lock);
2312 }
2313};
2314
2315static void kick_deferred_free(struct zs_pool *pool)
2316{
2317 schedule_work(&pool->free_work);
2318}
2319
2320static void init_deferred_free(struct zs_pool *pool)
2321{
2322 INIT_WORK(&pool->free_work, async_free_zspage);
2323}
2324
2325static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2326{
2327 struct page *page = get_first_page(zspage);
2328
2329 do {
2330 WARN_ON(!trylock_page(page));
2331 __SetPageMovable(page, pool->inode->i_mapping);
2332 unlock_page(page);
2333 } while ((page = get_next_page(page)) != NULL);
2334}
2335#endif
2336
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002337/*
2338 *
2339 * Based on the number of unused allocated objects calculate
2340 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002341 */
2342static unsigned long zs_can_compact(struct size_class *class)
2343{
2344 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002345 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2346 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002347
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002348 if (obj_allocated <= obj_used)
2349 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002350
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002351 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002352 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002353
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002354 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002355}
2356
Rokudo Yand72be3a2021-02-25 17:18:31 -08002357static unsigned long __zs_compact(struct zs_pool *pool,
2358 struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002359{
Minchan Kim312fcae2015-04-15 16:15:30 -07002360 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002361 struct zspage *src_zspage;
2362 struct zspage *dst_zspage = NULL;
Rokudo Yand72be3a2021-02-25 17:18:31 -08002363 unsigned long pages_freed = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07002364
Minchan Kim312fcae2015-04-15 16:15:30 -07002365 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002366 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002367
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002368 if (!zs_can_compact(class))
2369 break;
2370
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002371 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002372 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002373
Minchan Kim37836892016-07-26 15:23:23 -07002374 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002375 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002376 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002377 * If there is no more space in dst_page, resched
2378 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002379 */
2380 if (!migrate_zspage(pool, class, &cc))
2381 break;
2382
Minchan Kim4aa409c2016-07-26 15:23:26 -07002383 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002384 }
2385
2386 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002387 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002388 break;
2389
Minchan Kim4aa409c2016-07-26 15:23:26 -07002390 putback_zspage(class, dst_zspage);
2391 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002392 free_zspage(pool, class, src_zspage);
Rokudo Yand72be3a2021-02-25 17:18:31 -08002393 pages_freed += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002394 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002395 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002396 cond_resched();
2397 spin_lock(&class->lock);
2398 }
2399
Minchan Kim37836892016-07-26 15:23:23 -07002400 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002401 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002402
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002403 spin_unlock(&class->lock);
Rokudo Yand72be3a2021-02-25 17:18:31 -08002404
2405 return pages_freed;
Minchan Kim312fcae2015-04-15 16:15:30 -07002406}
2407
2408unsigned long zs_compact(struct zs_pool *pool)
2409{
2410 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002411 struct size_class *class;
Rokudo Yand72be3a2021-02-25 17:18:31 -08002412 unsigned long pages_freed = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07002413
2414 for (i = zs_size_classes - 1; i >= 0; i--) {
2415 class = pool->size_class[i];
2416 if (!class)
2417 continue;
2418 if (class->index != i)
2419 continue;
Rokudo Yand72be3a2021-02-25 17:18:31 -08002420 pages_freed += __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002421 }
Rokudo Yand72be3a2021-02-25 17:18:31 -08002422 atomic_long_add(pages_freed, &pool->stats.pages_compacted);
Minchan Kim312fcae2015-04-15 16:15:30 -07002423
Rokudo Yand72be3a2021-02-25 17:18:31 -08002424 return pages_freed;
Minchan Kim312fcae2015-04-15 16:15:30 -07002425}
2426EXPORT_SYMBOL_GPL(zs_compact);
2427
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002428void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2429{
2430 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2431}
2432EXPORT_SYMBOL_GPL(zs_pool_stats);
2433
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002434static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2435 struct shrink_control *sc)
2436{
2437 unsigned long pages_freed;
2438 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2439 shrinker);
2440
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002441 /*
2442 * Compact classes and calculate compaction delta.
2443 * Can run concurrently with a manually triggered
2444 * (by user) compaction.
2445 */
Rokudo Yand72be3a2021-02-25 17:18:31 -08002446 pages_freed = zs_compact(pool);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002447
2448 return pages_freed ? pages_freed : SHRINK_STOP;
2449}
2450
2451static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2452 struct shrink_control *sc)
2453{
2454 int i;
2455 struct size_class *class;
2456 unsigned long pages_to_free = 0;
2457 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2458 shrinker);
2459
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002460 for (i = zs_size_classes - 1; i >= 0; i--) {
2461 class = pool->size_class[i];
2462 if (!class)
2463 continue;
2464 if (class->index != i)
2465 continue;
2466
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002467 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002468 }
2469
2470 return pages_to_free;
2471}
2472
2473static void zs_unregister_shrinker(struct zs_pool *pool)
2474{
2475 if (pool->shrinker_enabled) {
2476 unregister_shrinker(&pool->shrinker);
2477 pool->shrinker_enabled = false;
2478 }
2479}
2480
2481static int zs_register_shrinker(struct zs_pool *pool)
2482{
2483 pool->shrinker.scan_objects = zs_shrinker_scan;
2484 pool->shrinker.count_objects = zs_shrinker_count;
2485 pool->shrinker.batch = 0;
2486 pool->shrinker.seeks = DEFAULT_SEEKS;
2487
2488 return register_shrinker(&pool->shrinker);
2489}
2490
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002491/**
2492 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002493 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002494 *
2495 * This function must be called before anything when using
2496 * the zsmalloc allocator.
2497 *
2498 * On success, a pointer to the newly created pool is returned,
2499 * otherwise NULL.
2500 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002501struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002502{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002503 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002504 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002505 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002506
Ganesh Mahendran18136652014-12-12 16:57:10 -08002507 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002508 if (!pool)
2509 return NULL;
2510
Minchan Kim48b48002016-07-26 15:23:31 -07002511 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002512 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2513 GFP_KERNEL);
2514 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002515 kfree(pool);
2516 return NULL;
2517 }
2518
Minchan Kim2e40e162015-04-15 16:15:23 -07002519 pool->name = kstrdup(name, GFP_KERNEL);
2520 if (!pool->name)
2521 goto err;
2522
Andrew Morton9ae46112019-08-30 16:04:35 -07002523#ifdef CONFIG_COMPACTION
Henry Burns0e7e2732019-08-24 17:55:06 -07002524 init_waitqueue_head(&pool->migration_wait);
Andrew Morton9ae46112019-08-30 16:04:35 -07002525#endif
Henry Burns0e7e2732019-08-24 17:55:06 -07002526
Minchan Kim37836892016-07-26 15:23:23 -07002527 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002528 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002529
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002530 /*
2531 * Iterate reversly, because, size of size_class that we want to use
2532 * for merging should be larger or equal to current size.
2533 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002534 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002535 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002536 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002537 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002538 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002539 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002540
2541 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2542 if (size > ZS_MAX_ALLOC_SIZE)
2543 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002544 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002545 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002546
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002547 /*
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07002548 * We iterate from biggest down to smallest classes,
2549 * so huge_class_size holds the size of the first huge
2550 * class. Any object bigger than or equal to that will
2551 * endup in the huge class.
2552 */
2553 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2554 !huge_class_size) {
2555 huge_class_size = size;
2556 /*
2557 * The object uses ZS_HANDLE_SIZE bytes to store the
2558 * handle. We need to subtract it, because zs_malloc()
2559 * unconditionally adds handle size before it performs
2560 * size class search - so object may be smaller than
2561 * huge class size, yet it still can end up in the huge
2562 * class because it grows by ZS_HANDLE_SIZE extra bytes
2563 * right before class lookup.
2564 */
2565 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2566 }
2567
2568 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002569 * size_class is used for normal zsmalloc operation such
2570 * as alloc/free for that size. Although it is natural that we
2571 * have one size_class for each size, there is a chance that we
2572 * can get more memory utilization if we use one size_class for
2573 * many different sizes whose size_class have same
2574 * characteristics. So, we makes size_class point to
2575 * previous size_class if possible.
2576 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002577 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002578 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002579 pool->size_class[i] = prev_class;
2580 continue;
2581 }
2582 }
2583
2584 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2585 if (!class)
2586 goto err;
2587
Nitin Gupta61989a82012-01-09 16:51:56 -06002588 class->size = size;
2589 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002590 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002591 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002592 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002593 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002594 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2595 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002596 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002597
2598 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002599 }
2600
Dan Streetmand34f6152016-05-20 16:59:56 -07002601 /* debug only, don't abort if it fails */
2602 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002603
Minchan Kim48b48002016-07-26 15:23:31 -07002604 if (zs_register_migration(pool))
2605 goto err;
2606
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002607 /*
2608 * Not critical, we still can use the pool
2609 * and user can trigger compaction manually.
2610 */
2611 if (zs_register_shrinker(pool) == 0)
2612 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002613 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002614
2615err:
2616 zs_destroy_pool(pool);
2617 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002618}
2619EXPORT_SYMBOL_GPL(zs_create_pool);
2620
2621void zs_destroy_pool(struct zs_pool *pool)
2622{
2623 int i;
2624
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002625 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002626 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002627 zs_pool_stat_destroy(pool);
2628
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002629 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002630 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002631 struct size_class *class = pool->size_class[i];
2632
2633 if (!class)
2634 continue;
2635
2636 if (class->index != i)
2637 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002638
Minchan Kim48b48002016-07-26 15:23:31 -07002639 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002640 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002641 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002642 class->size, fg);
2643 }
2644 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002645 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002646 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002647
Minchan Kim37836892016-07-26 15:23:23 -07002648 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002649 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002650 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002651 kfree(pool);
2652}
2653EXPORT_SYMBOL_GPL(zs_destroy_pool);
2654
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002655static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002656{
Minchan Kim48b48002016-07-26 15:23:31 -07002657 int ret;
2658
2659 ret = zsmalloc_mount();
2660 if (ret)
2661 goto out;
2662
2663 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002664
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002665 if (ret)
2666 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002667
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002668 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002669
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002670#ifdef CONFIG_ZPOOL
2671 zpool_register_driver(&zs_zpool_driver);
2672#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002673
Dan Streetman4abaac92016-05-26 15:16:27 -07002674 zs_stat_init();
2675
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002676 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002677
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002678notifier_fail:
2679 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002680 zsmalloc_unmount();
2681out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002682 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002683}
Nitin Gupta61989a82012-01-09 16:51:56 -06002684
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002685static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002686{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002687#ifdef CONFIG_ZPOOL
2688 zpool_unregister_driver(&zs_zpool_driver);
2689#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002690 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002691 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002692
2693 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002694}
Ben Hutchings069f1012012-06-20 02:31:11 +01002695
2696module_init(zs_init);
2697module_exit(zs_exit);
2698
2699MODULE_LICENSE("Dual BSD/GPL");
2700MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");