blob: c36fdff9a37131fef3a773e25b1b56dbd644232e [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
Xishi Qiu399d8ee2017-02-22 15:45:01 -080027 * PG_owner_priv_1: identifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Nitin Gupta61989a82012-01-09 16:51:56 -060033#include <linux/module.h>
34#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070035#include <linux/sched.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010036#include <linux/magic.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>
Mike Rapoportca5999f2020-06-08 21:32:38 -070042#include <linux/pgtable.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070043#include <asm/tlbflush.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060044#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>
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -080049#include <linux/shrinker.h>
Seth Jennings0959c632012-08-08 15:12:17 +090050#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080051#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080052#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070053#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070054#include <linux/mount.h>
David Howells8e9231f2019-03-25 16:38:23 +000055#include <linux/pseudo_fs.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070056#include <linux/migrate.h>
Henry Burns701d6782019-08-24 17:55:06 -070057#include <linux/wait.h>
Minchan Kim48b48002016-07-26 15:23:31 -070058#include <linux/pagemap.h>
Sergey Senozhatskycdc346b2018-01-04 16:18:02 -080059#include <linux/fs.h>
Minchan Kim48b48002016-07-26 15:23:31 -070060
61#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090062
63/*
64 * This must be power of 2 and greater than of equal to sizeof(link_free).
65 * These two conditions ensure that any 'struct link_free' itself doesn't
66 * span more than 1 page which avoids complex case of mapping 2 pages simply
67 * to restore link_free pointer values.
68 */
69#define ZS_ALIGN 8
70
71/*
72 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
73 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
74 */
75#define ZS_MAX_ZSPAGE_ORDER 2
76#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
77
Minchan Kim2e40e162015-04-15 16:15:23 -070078#define ZS_HANDLE_SIZE (sizeof(unsigned long))
79
Seth Jennings0959c632012-08-08 15:12:17 +090080/*
81 * Object location (<PFN>, <obj_idx>) is encoded as
Randy Dunlapb956b5a2020-08-11 18:33:31 -070082 * a single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090083 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070084 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090085 *
86 * This is made more complicated by various memory models and PAE.
87 */
88
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030089#ifndef MAX_POSSIBLE_PHYSMEM_BITS
90#ifdef MAX_PHYSMEM_BITS
91#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS
92#else
Seth Jennings0959c632012-08-08 15:12:17 +090093/*
94 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
95 * be PAGE_SHIFT
96 */
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030097#define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
Seth Jennings0959c632012-08-08 15:12:17 +090098#endif
99#endif
Kirill A. Shutemov02390b82018-02-14 14:16:49 +0300100
101#define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -0700102
103/*
104 * Memory for allocating for handle keeps object position by
105 * encoding <page, obj_idx> and the encoded value has a room
106 * in least bit(ie, look at obj_to_location).
107 * We use the bit to synchronize between object access by
108 * user and migration.
109 */
110#define HANDLE_PIN_BIT 0
111
112/*
113 * Head in allocated object should have OBJ_ALLOCATED_TAG
114 * to identify the object was allocated or not.
115 * It's okay to add the status bit in the least bit because
116 * header keeps handle which is 4byte-aligned address so we
117 * have room for two bit at least.
118 */
119#define OBJ_ALLOCATED_TAG 1
120#define OBJ_TAG_BITS 1
121#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900122#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
123
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700124#define FULLNESS_BITS 2
125#define CLASS_BITS 8
126#define ISOLATED_BITS 3
127#define MAGIC_VAL_BITS 8
128
Seth Jennings0959c632012-08-08 15:12:17 +0900129#define MAX(a, b) ((a) >= (b) ? (a) : (b))
130/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
131#define ZS_MIN_ALLOC_SIZE \
132 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700133/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700134#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900135
136/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700137 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900138 * trader-off here:
139 * - Large number of size classes is potentially wasteful as free page are
140 * spread across these classes
141 * - Small number of size classes causes large internal fragmentation
142 * - Probably its better to use specific size classes (empirically
143 * determined). NOTE: all those class sizes must be set as multiple of
144 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
145 *
146 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
147 * (reason above)
148 */
Minchan Kim37836892016-07-26 15:23:23 -0700149#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700150#define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
151 ZS_SIZE_CLASS_DELTA) + 1)
Seth Jennings0959c632012-08-08 15:12:17 +0900152
Seth Jennings0959c632012-08-08 15:12:17 +0900153enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900154 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700155 ZS_ALMOST_EMPTY,
156 ZS_ALMOST_FULL,
157 ZS_FULL,
158 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900159};
160
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800161enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700162 CLASS_EMPTY,
163 CLASS_ALMOST_EMPTY,
164 CLASS_ALMOST_FULL,
165 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800166 OBJ_ALLOCATED,
167 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700168 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800169};
170
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800171struct zs_size_stat {
172 unsigned long objs[NR_ZS_STAT_TYPE];
173};
174
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700175#ifdef CONFIG_ZSMALLOC_STAT
176static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800177#endif
178
Minchan Kim48b48002016-07-26 15:23:31 -0700179#ifdef CONFIG_COMPACTION
180static struct vfsmount *zsmalloc_mnt;
181#endif
182
Seth Jennings0959c632012-08-08 15:12:17 +0900183/*
184 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
185 * n <= N / f, where
186 * n = number of allocated objects
187 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700188 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900189 *
190 * Similarly, we assign zspage to:
191 * ZS_ALMOST_FULL when n > N / f
192 * ZS_EMPTY when n == 0
193 * ZS_FULL when n == N
194 *
195 * (see: fix_fullness_group())
196 */
197static const int fullness_threshold_frac = 4;
Sergey Senozhatsky010b4952018-04-05 16:24:43 -0700198static size_t huge_class_size;
Seth Jennings0959c632012-08-08 15:12:17 +0900199
200struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700201 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700202 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900203 /*
204 * Size of objects stored in this class. Must be multiple
205 * of ZS_ALIGN.
206 */
207 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700208 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800209 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
210 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700211
212 unsigned int index;
213 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900214};
215
Minchan Kim48b48002016-07-26 15:23:31 -0700216/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
217static void SetPageHugeObject(struct page *page)
218{
219 SetPageOwnerPriv1(page);
220}
221
222static void ClearPageHugeObject(struct page *page)
223{
224 ClearPageOwnerPriv1(page);
225}
226
227static int PageHugeObject(struct page *page)
228{
229 return PageOwnerPriv1(page);
230}
231
Seth Jennings0959c632012-08-08 15:12:17 +0900232/*
233 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700234 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900235 *
236 * This must be power of 2 and less than or equal to ZS_ALIGN
237 */
238struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700239 union {
240 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700241 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700242 * It's valid for non-allocated object
243 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700244 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700245 /*
246 * Handle of allocated object.
247 */
248 unsigned long handle;
249 };
Seth Jennings0959c632012-08-08 15:12:17 +0900250};
251
252struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800253 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800254
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700255 struct size_class *size_class[ZS_SIZE_CLASSES];
Minchan Kim2e40e162015-04-15 16:15:23 -0700256 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700257 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900258
Minchan Kim13de8932014-10-09 15:29:48 -0700259 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800260
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700261 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700262
263 /* Compact classes */
264 struct shrinker shrinker;
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -0800265
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800266#ifdef CONFIG_ZSMALLOC_STAT
267 struct dentry *stat_dentry;
268#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700269#ifdef CONFIG_COMPACTION
270 struct inode *inode;
271 struct work_struct free_work;
Henry Burns701d6782019-08-24 17:55:06 -0700272 /* A wait queue for when migration races with async_free_zspage() */
273 struct wait_queue_head migration_wait;
274 atomic_long_t isolated_pages;
275 bool destroying;
Minchan Kim48b48002016-07-26 15:23:31 -0700276#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900277};
Nitin Gupta61989a82012-01-09 16:51:56 -0600278
Minchan Kim37836892016-07-26 15:23:23 -0700279struct zspage {
280 struct {
281 unsigned int fullness:FULLNESS_BITS;
Minchan Kim85d492f2017-04-13 14:56:40 -0700282 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700283 unsigned int isolated:ISOLATED_BITS;
284 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700285 };
286 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700287 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700288 struct page *first_page;
289 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700290#ifdef CONFIG_COMPACTION
291 rwlock_t lock;
292#endif
Minchan Kim37836892016-07-26 15:23:23 -0700293};
Nitin Gupta61989a82012-01-09 16:51:56 -0600294
Seth Jenningsf5536462012-07-18 11:55:56 -0500295struct mapping_area {
Christoph Hellwig8b136012020-06-01 21:50:53 -0700296#ifdef CONFIG_ZSMALLOC_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500297 struct vm_struct *vm; /* vm area for mapping object that span pages */
298#else
299 char *vm_buf; /* copy buffer for objects that span pages */
300#endif
301 char *vm_addr; /* address of kmap_atomic()'ed pages */
302 enum zs_mapmode vm_mm; /* mapping mode */
303};
304
Minchan Kim48b48002016-07-26 15:23:31 -0700305#ifdef CONFIG_COMPACTION
306static int zs_register_migration(struct zs_pool *pool);
307static void zs_unregister_migration(struct zs_pool *pool);
308static void migrate_lock_init(struct zspage *zspage);
309static void migrate_read_lock(struct zspage *zspage);
310static void migrate_read_unlock(struct zspage *zspage);
311static void kick_deferred_free(struct zs_pool *pool);
312static void init_deferred_free(struct zs_pool *pool);
313static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
314#else
315static int zsmalloc_mount(void) { return 0; }
316static void zsmalloc_unmount(void) {}
317static int zs_register_migration(struct zs_pool *pool) { return 0; }
318static void zs_unregister_migration(struct zs_pool *pool) {}
319static void migrate_lock_init(struct zspage *zspage) {}
320static void migrate_read_lock(struct zspage *zspage) {}
321static void migrate_read_unlock(struct zspage *zspage) {}
322static void kick_deferred_free(struct zs_pool *pool) {}
323static void init_deferred_free(struct zs_pool *pool) {}
324static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
325#endif
326
Minchan Kim37836892016-07-26 15:23:23 -0700327static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700328{
329 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
330 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700331 if (!pool->handle_cachep)
332 return 1;
333
334 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
335 0, 0, NULL);
336 if (!pool->zspage_cachep) {
337 kmem_cache_destroy(pool->handle_cachep);
338 pool->handle_cachep = NULL;
339 return 1;
340 }
341
342 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700343}
344
Minchan Kim37836892016-07-26 15:23:23 -0700345static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700346{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700347 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700348 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700349}
350
Minchan Kim37836892016-07-26 15:23:23 -0700351static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700352{
353 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700354 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700355}
356
Minchan Kim37836892016-07-26 15:23:23 -0700357static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700358{
359 kmem_cache_free(pool->handle_cachep, (void *)handle);
360}
361
Minchan Kim37836892016-07-26 15:23:23 -0700362static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
363{
Minchan Kim48b48002016-07-26 15:23:31 -0700364 return kmem_cache_alloc(pool->zspage_cachep,
365 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800366}
Minchan Kim37836892016-07-26 15:23:23 -0700367
368static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
369{
370 kmem_cache_free(pool->zspage_cachep, zspage);
371}
372
Minchan Kim2e40e162015-04-15 16:15:23 -0700373static void record_obj(unsigned long handle, unsigned long obj)
374{
Junil Leec102f07c2016-01-20 14:58:18 -0800375 /*
376 * lsb of @obj represents handle lock while other bits
377 * represent object value the handle is pointing so
378 * updating shouldn't do store tearing.
379 */
380 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700381}
382
Dan Streetmanc7957792014-08-06 16:08:38 -0700383/* zpool driver */
384
385#ifdef CONFIG_ZPOOL
386
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800387static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700388 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700389 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700390{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700391 /*
392 * Ignore global gfp flags: zs_malloc() may be invoked from
393 * different contexts and its caller must provide a valid
394 * gfp mask.
395 */
396 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700397}
398
399static void zs_zpool_destroy(void *pool)
400{
401 zs_destroy_pool(pool);
402}
403
404static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
405 unsigned long *handle)
406{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700407 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700408 return *handle ? 0 : -1;
409}
410static void zs_zpool_free(void *pool, unsigned long handle)
411{
412 zs_free(pool, handle);
413}
414
Dan Streetmanc7957792014-08-06 16:08:38 -0700415static void *zs_zpool_map(void *pool, unsigned long handle,
416 enum zpool_mapmode mm)
417{
418 enum zs_mapmode zs_mm;
419
420 switch (mm) {
421 case ZPOOL_MM_RO:
422 zs_mm = ZS_MM_RO;
423 break;
424 case ZPOOL_MM_WO:
425 zs_mm = ZS_MM_WO;
426 break;
Joe Perchese4a9bc52020-04-06 20:08:39 -0700427 case ZPOOL_MM_RW:
Dan Streetmanc7957792014-08-06 16:08:38 -0700428 default:
429 zs_mm = ZS_MM_RW;
430 break;
431 }
432
433 return zs_map_object(pool, handle, zs_mm);
434}
435static void zs_zpool_unmap(void *pool, unsigned long handle)
436{
437 zs_unmap_object(pool, handle);
438}
439
440static u64 zs_zpool_total_size(void *pool)
441{
Minchan Kim722cdc12014-10-09 15:29:50 -0700442 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700443}
444
445static struct zpool_driver zs_zpool_driver = {
Hui Zhuc165f252019-09-23 15:39:37 -0700446 .type = "zsmalloc",
447 .owner = THIS_MODULE,
448 .create = zs_zpool_create,
449 .destroy = zs_zpool_destroy,
450 .malloc_support_movable = true,
451 .malloc = zs_zpool_malloc,
452 .free = zs_zpool_free,
453 .map = zs_zpool_map,
454 .unmap = zs_zpool_unmap,
455 .total_size = zs_zpool_total_size,
Dan Streetmanc7957792014-08-06 16:08:38 -0700456};
457
Kees Cook137f8cf2014-08-29 15:18:40 -0700458MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700459#endif /* CONFIG_ZPOOL */
460
Nitin Gupta61989a82012-01-09 16:51:56 -0600461/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
462static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
463
Minchan Kim48b48002016-07-26 15:23:31 -0700464static bool is_zspage_isolated(struct zspage *zspage)
465{
466 return zspage->isolated;
467}
468
Nick Desaulniers3457f412017-07-10 15:47:26 -0700469static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600470{
Minchan Kima27545bf2012-04-25 15:23:09 +0900471 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600472}
473
Minchan Kim48b48002016-07-26 15:23:31 -0700474/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700475static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600476{
Minchan Kim37836892016-07-26 15:23:23 -0700477 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600478}
479
Minchan Kim4f420472016-07-26 15:23:17 -0700480
Minchan Kim37836892016-07-26 15:23:23 -0700481static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700482{
Minchan Kim37836892016-07-26 15:23:23 -0700483 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700484}
485
Minchan Kim48b48002016-07-26 15:23:31 -0700486static inline struct page *get_first_page(struct zspage *zspage)
487{
488 struct page *first_page = zspage->first_page;
489
490 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
491 return first_page;
492}
493
Minchan Kim4f420472016-07-26 15:23:17 -0700494static inline int get_first_obj_offset(struct page *page)
495{
Minchan Kim48b48002016-07-26 15:23:31 -0700496 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700497}
498
499static inline void set_first_obj_offset(struct page *page, int offset)
500{
Minchan Kim48b48002016-07-26 15:23:31 -0700501 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700502}
503
Minchan Kimbfd093f2016-07-26 15:23:28 -0700504static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700505{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700506 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700507}
508
Minchan Kimbfd093f2016-07-26 15:23:28 -0700509static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700510{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700511 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700512}
513
Minchan Kim37836892016-07-26 15:23:23 -0700514static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700515 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600516 enum fullness_group *fullness)
517{
Minchan Kim48b48002016-07-26 15:23:31 -0700518 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
519
Minchan Kim37836892016-07-26 15:23:23 -0700520 *fullness = zspage->fullness;
521 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600522}
523
Minchan Kim37836892016-07-26 15:23:23 -0700524static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700525 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600526 enum fullness_group fullness)
527{
Minchan Kim37836892016-07-26 15:23:23 -0700528 zspage->class = class_idx;
529 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600530}
531
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900532/*
533 * zsmalloc divides the pool into various size classes where each
534 * class maintains a list of zspages where each zspage is divided
535 * into equal sized chunks. Each allocation falls into one of these
536 * classes depending on its size. This function returns index of the
537 * size class which has chunk size big enough to hold the give size.
538 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600539static int get_size_class_index(int size)
540{
541 int idx = 0;
542
543 if (likely(size > ZS_MIN_ALLOC_SIZE))
544 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
545 ZS_SIZE_CLASS_DELTA);
546
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700547 return min_t(int, ZS_SIZE_CLASSES - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600548}
549
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700550/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700551static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700552 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700553{
Minchan Kim48b48002016-07-26 15:23:31 -0700554 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700555}
556
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700557/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700558static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700559 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700560{
Minchan Kim48b48002016-07-26 15:23:31 -0700561 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700562}
563
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700564/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700565static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700566 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700567{
Minchan Kim48b48002016-07-26 15:23:31 -0700568 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700569}
570
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700571#ifdef CONFIG_ZSMALLOC_STAT
572
Dan Streetman4abaac92016-05-26 15:16:27 -0700573static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700574{
Dan Streetman4abaac92016-05-26 15:16:27 -0700575 if (!debugfs_initialized()) {
576 pr_warn("debugfs not available, stat dir not created\n");
577 return;
578 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700579
580 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700581}
582
583static void __exit zs_stat_exit(void)
584{
585 debugfs_remove_recursive(zs_stat_root);
586}
587
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700588static unsigned long zs_can_compact(struct size_class *class);
589
Minchan Kim248ca1b2015-04-15 16:15:42 -0700590static int zs_stats_size_show(struct seq_file *s, void *v)
591{
592 int i;
593 struct zs_pool *pool = s->private;
594 struct size_class *class;
595 int objs_per_zspage;
596 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700597 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700598 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
599 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700600 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700602 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700603 "class", "size", "almost_full", "almost_empty",
604 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700605 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700606
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700607 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim248ca1b2015-04-15 16:15:42 -0700608 class = pool->size_class[i];
609
610 if (class->index != i)
611 continue;
612
613 spin_lock(&class->lock);
614 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
615 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
616 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
617 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700618 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619 spin_unlock(&class->lock);
620
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700621 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700622 pages_used = obj_allocated / objs_per_zspage *
623 class->pages_per_zspage;
624
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700625 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
626 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700627 i, class->size, class_almost_full, class_almost_empty,
628 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700629 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700630
631 total_class_almost_full += class_almost_full;
632 total_class_almost_empty += class_almost_empty;
633 total_objs += obj_allocated;
634 total_used_objs += obj_used;
635 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700636 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700637 }
638
639 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700640 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700641 "Total", "", total_class_almost_full,
642 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700643 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700644
645 return 0;
646}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700647DEFINE_SHOW_ATTRIBUTE(zs_stats_size);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648
Dan Streetmand34f6152016-05-20 16:59:56 -0700649static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700650{
Dan Streetman4abaac92016-05-26 15:16:27 -0700651 if (!zs_stat_root) {
652 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700653 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700654 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700655
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100656 pool->stat_dentry = debugfs_create_dir(name, zs_stat_root);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700657
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100658 debugfs_create_file("classes", S_IFREG | 0444, pool->stat_dentry, pool,
659 &zs_stats_size_fops);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700660}
661
662static void zs_pool_stat_destroy(struct zs_pool *pool)
663{
664 debugfs_remove_recursive(pool->stat_dentry);
665}
666
667#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700668static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700669{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700670}
671
672static void __exit zs_stat_exit(void)
673{
674}
675
Dan Streetmand34f6152016-05-20 16:59:56 -0700676static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700677{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700678}
679
680static inline void zs_pool_stat_destroy(struct zs_pool *pool)
681{
682}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700683#endif
684
Minchan Kim48b48002016-07-26 15:23:31 -0700685
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900686/*
687 * For each size class, zspages are divided into different groups
688 * depending on how "full" they are. This was done so that we could
689 * easily find empty or nearly empty zspages when we try to shrink
690 * the pool (not yet implemented). This function returns fullness
691 * status of the given page.
692 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700693static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700694 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600695{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700696 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600697 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700698
Minchan Kim37836892016-07-26 15:23:23 -0700699 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700700 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600701
702 if (inuse == 0)
703 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700704 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600705 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700706 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600707 fg = ZS_ALMOST_EMPTY;
708 else
709 fg = ZS_ALMOST_FULL;
710
711 return fg;
712}
713
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900714/*
715 * Each size class maintains various freelists and zspages are assigned
716 * to one of these freelists based on the number of live objects they
717 * have. This functions inserts the given zspage into the freelist
718 * identified by <class, fullness_group>.
719 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700720static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700721 struct zspage *zspage,
722 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600723{
Minchan Kim37836892016-07-26 15:23:23 -0700724 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600725
Minchan Kim48b48002016-07-26 15:23:31 -0700726 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700727 head = list_first_entry_or_null(&class->fullness_list[fullness],
728 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700729 /*
Minchan Kim37836892016-07-26 15:23:23 -0700730 * We want to see more ZS_FULL pages and less almost empty/full.
731 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700732 */
Minchan Kim37836892016-07-26 15:23:23 -0700733 if (head) {
734 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
735 list_add(&zspage->list, &head->list);
736 return;
737 }
738 }
739 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600740}
741
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900742/*
743 * This function removes the given zspage from the freelist identified
744 * by <class, fullness_group>.
745 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700746static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700747 struct zspage *zspage,
748 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600749{
Minchan Kim37836892016-07-26 15:23:23 -0700750 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700751 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600752
Minchan Kim37836892016-07-26 15:23:23 -0700753 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700754 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600755}
756
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900757/*
758 * Each size class maintains zspages in different fullness groups depending
759 * on the number of live objects they contain. When allocating or freeing
760 * objects, the fullness status of the page can change, say, from ALMOST_FULL
761 * to ALMOST_EMPTY when freeing an object. This function checks if such
762 * a status change has occurred for the given page and accordingly moves the
763 * page from the freelist of the old fullness group to that of the new
764 * fullness group.
765 */
Minchan Kimc7806262015-04-15 16:15:26 -0700766static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700767 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600768{
769 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600770 enum fullness_group currfg, newfg;
771
Minchan Kim37836892016-07-26 15:23:23 -0700772 get_zspage_mapping(zspage, &class_idx, &currfg);
773 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600774 if (newfg == currfg)
775 goto out;
776
Minchan Kim48b48002016-07-26 15:23:31 -0700777 if (!is_zspage_isolated(zspage)) {
778 remove_zspage(class, zspage, currfg);
779 insert_zspage(class, zspage, newfg);
780 }
781
Minchan Kim37836892016-07-26 15:23:23 -0700782 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600783
784out:
785 return newfg;
786}
787
788/*
789 * We have to decide on how many pages to link together
790 * to form a zspage for each size class. This is important
791 * to reduce wastage due to unusable space left at end of
792 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700793 * wastage = Zp % class_size
794 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600795 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
796 *
797 * For example, for size class of 3/8 * PAGE_SIZE, we should
798 * link together 3 PAGE_SIZE sized pages to form a zspage
799 * since then we can perfectly fit in 8 such objects.
800 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900801static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600802{
803 int i, max_usedpc = 0;
804 /* zspage order which gives maximum used size per KB */
805 int max_usedpc_order = 1;
806
Seth Jennings84d4faa2012-03-05 11:33:21 -0600807 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600808 int zspage_size;
809 int waste, usedpc;
810
811 zspage_size = i * PAGE_SIZE;
812 waste = zspage_size % class_size;
813 usedpc = (zspage_size - waste) * 100 / zspage_size;
814
815 if (usedpc > max_usedpc) {
816 max_usedpc = usedpc;
817 max_usedpc_order = i;
818 }
819 }
820
821 return max_usedpc_order;
822}
823
Minchan Kim37836892016-07-26 15:23:23 -0700824static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600825{
Minchan Kim48b48002016-07-26 15:23:31 -0700826 struct zspage *zspage = (struct zspage *)page->private;
827
828 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
829 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600830}
831
832static struct page *get_next_page(struct page *page)
833{
Minchan Kim48b48002016-07-26 15:23:31 -0700834 if (unlikely(PageHugeObject(page)))
835 return NULL;
836
837 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600838}
839
Minchan Kimbfd093f2016-07-26 15:23:28 -0700840/**
841 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700842 * @obj: the encoded object value
Minchan Kimbfd093f2016-07-26 15:23:28 -0700843 * @page: page object resides in zspage
844 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800845 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700846static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700847 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600848{
Minchan Kim312fcae2015-04-15 16:15:30 -0700849 obj >>= OBJ_TAG_BITS;
850 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
851 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600852}
853
Minchan Kimbfd093f2016-07-26 15:23:28 -0700854/**
855 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
856 * @page: page object resides in zspage
857 * @obj_idx: object index
858 */
859static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
860{
861 unsigned long obj;
862
863 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
864 obj |= obj_idx & OBJ_INDEX_MASK;
865 obj <<= OBJ_TAG_BITS;
866
867 return obj;
868}
869
Minchan Kim2e40e162015-04-15 16:15:23 -0700870static unsigned long handle_to_obj(unsigned long handle)
871{
872 return *(unsigned long *)handle;
873}
874
Minchan Kim48b48002016-07-26 15:23:31 -0700875static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700876{
Minchan Kim48b48002016-07-26 15:23:31 -0700877 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700878 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700879 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700880 } else
881 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700882}
883
Minchan Kim48b48002016-07-26 15:23:31 -0700884static inline int testpin_tag(unsigned long handle)
885{
886 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
887}
888
Minchan Kim312fcae2015-04-15 16:15:30 -0700889static inline int trypin_tag(unsigned long handle)
890{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700891 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700892}
893
Jules Irenge70c7ec92020-04-06 20:08:27 -0700894static void pin_tag(unsigned long handle) __acquires(bitlock)
Minchan Kim312fcae2015-04-15 16:15:30 -0700895{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700896 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700897}
898
Jules Irengebc22b182020-04-06 20:08:30 -0700899static void unpin_tag(unsigned long handle) __releases(bitlock)
Minchan Kim312fcae2015-04-15 16:15:30 -0700900{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700901 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700902}
903
Nitin Guptaf4477e92012-04-02 09:13:56 -0500904static void reset_page(struct page *page)
905{
Minchan Kim48b48002016-07-26 15:23:31 -0700906 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700907 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500908 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700909 page_mapcount_reset(page);
910 ClearPageHugeObject(page);
911 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500912}
913
Colin Ian King4d0a5402018-08-17 15:46:50 -0700914static int trylock_zspage(struct zspage *zspage)
Minchan Kim48b48002016-07-26 15:23:31 -0700915{
916 struct page *cursor, *fail;
917
918 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
919 get_next_page(cursor)) {
920 if (!trylock_page(cursor)) {
921 fail = cursor;
922 goto unlock;
923 }
924 }
925
926 return 1;
927unlock:
928 for (cursor = get_first_page(zspage); cursor != fail; cursor =
929 get_next_page(cursor))
930 unlock_page(cursor);
931
932 return 0;
933}
934
935static void __free_zspage(struct zs_pool *pool, struct size_class *class,
936 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600937{
Minchan Kim37836892016-07-26 15:23:23 -0700938 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700939 enum fullness_group fg;
940 unsigned int class_idx;
941
942 get_zspage_mapping(zspage, &class_idx, &fg);
943
944 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600945
Minchan Kim37836892016-07-26 15:23:23 -0700946 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700947 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600948
Minchan Kim48b48002016-07-26 15:23:31 -0700949 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700950 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700951 VM_BUG_ON_PAGE(!PageLocked(page), page);
952 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700953 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700954 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -0700955 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -0700956 put_page(page);
957 page = next;
958 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -0600959
Minchan Kim37836892016-07-26 15:23:23 -0700960 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700961
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700962 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700963 atomic_long_sub(class->pages_per_zspage,
964 &pool->pages_allocated);
965}
966
967static void free_zspage(struct zs_pool *pool, struct size_class *class,
968 struct zspage *zspage)
969{
970 VM_BUG_ON(get_zspage_inuse(zspage));
971 VM_BUG_ON(list_empty(&zspage->list));
972
973 if (!trylock_zspage(zspage)) {
974 kick_deferred_free(pool);
975 return;
976 }
977
978 remove_zspage(class, zspage, ZS_EMPTY);
979 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600980}
981
982/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -0700983static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600984{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700985 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600986 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -0700987 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -0700988
Nitin Gupta61989a82012-01-09 16:51:56 -0600989 while (page) {
990 struct page *next_page;
991 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800992 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600993
Minchan Kim37836892016-07-26 15:23:23 -0700994 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -0600995
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800996 vaddr = kmap_atomic(page);
997 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600998
Dan Streetman5538c562014-10-09 15:30:01 -0700999 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001000 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001001 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001002 }
1003
1004 /*
1005 * We now come to the last (full or partial) object on this
1006 * page, which must point to the first object on the next
1007 * page (if present)
1008 */
1009 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001010 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001011 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001012 } else {
1013 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001014 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001015 * whether it's allocated object or not.
1016 */
Nick Desaulniers01a6ad92018-01-31 16:20:15 -08001017 link->next = -1UL << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001018 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001019 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001020 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001021 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001022 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001023
Minchan Kimbfd093f2016-07-26 15:23:28 -07001024 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001025}
1026
Minchan Kim48b48002016-07-26 15:23:31 -07001027static void create_page_chain(struct size_class *class, struct zspage *zspage,
1028 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001029{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001030 int i;
1031 struct page *page;
1032 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001033 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001034
1035 /*
1036 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001037 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001038 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001039 *
Minchan Kim37836892016-07-26 15:23:23 -07001040 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001041 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001042 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001043 for (i = 0; i < nr_pages; i++) {
1044 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001045 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001046 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001047 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001048 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001049 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001050 if (unlikely(class->objs_per_zspage == 1 &&
1051 class->pages_per_zspage == 1))
1052 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001053 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001054 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001055 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001056 prev_page = page;
1057 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001058}
Nitin Gupta61989a82012-01-09 16:51:56 -06001059
Minchan Kimbdb0af72016-07-26 15:23:20 -07001060/*
1061 * Allocate a zspage for the given size class
1062 */
Minchan Kim37836892016-07-26 15:23:23 -07001063static struct zspage *alloc_zspage(struct zs_pool *pool,
1064 struct size_class *class,
1065 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001066{
1067 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001068 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001069 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1070
1071 if (!zspage)
1072 return NULL;
1073
1074 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001075 zspage->magic = ZSPAGE_MAGIC;
1076 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001077
Minchan Kimbdb0af72016-07-26 15:23:20 -07001078 for (i = 0; i < class->pages_per_zspage; i++) {
1079 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001080
Minchan Kim37836892016-07-26 15:23:23 -07001081 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001082 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001083 while (--i >= 0) {
1084 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001085 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001086 }
Minchan Kim37836892016-07-26 15:23:23 -07001087 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001088 return NULL;
1089 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001090
1091 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001092 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001093 }
1094
Minchan Kim48b48002016-07-26 15:23:31 -07001095 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001096 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001097
Minchan Kim37836892016-07-26 15:23:23 -07001098 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001099}
1100
Minchan Kim37836892016-07-26 15:23:23 -07001101static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001102{
1103 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001104 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001105
Minchan Kim48b48002016-07-26 15:23:31 -07001106 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001107 zspage = list_first_entry_or_null(&class->fullness_list[i],
1108 struct zspage, list);
1109 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001110 break;
1111 }
1112
Minchan Kim37836892016-07-26 15:23:23 -07001113 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001114}
1115
Christoph Hellwig8b136012020-06-01 21:50:53 -07001116#ifdef CONFIG_ZSMALLOC_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001117static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -05001118{
Seth Jenningsf5536462012-07-18 11:55:56 -05001119 /*
1120 * Make sure we don't leak memory if a cpu UP notification
1121 * and zs_init() race and both call zs_cpu_up() on the same cpu
1122 */
1123 if (area->vm)
1124 return 0;
1125 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1126 if (!area->vm)
1127 return -ENOMEM;
1128 return 0;
1129}
1130
1131static inline void __zs_cpu_down(struct mapping_area *area)
1132{
1133 if (area->vm)
1134 free_vm_area(area->vm);
1135 area->vm = NULL;
1136}
1137
1138static inline void *__zs_map_object(struct mapping_area *area,
1139 struct page *pages[2], int off, int size)
1140{
Christoph Hellwiged1f3242020-06-01 21:51:19 -07001141 unsigned long addr = (unsigned long)area->vm->addr;
1142
1143 BUG_ON(map_kernel_range(addr, PAGE_SIZE * 2, PAGE_KERNEL, pages) < 0);
Seth Jenningsf5536462012-07-18 11:55:56 -05001144 area->vm_addr = area->vm->addr;
1145 return area->vm_addr + off;
1146}
1147
1148static inline void __zs_unmap_object(struct mapping_area *area,
1149 struct page *pages[2], int off, int size)
1150{
1151 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001152
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001153 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001154}
1155
Christoph Hellwig8b136012020-06-01 21:50:53 -07001156#else /* CONFIG_ZSMALLOC_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001157
1158static inline int __zs_cpu_up(struct mapping_area *area)
1159{
1160 /*
1161 * Make sure we don't leak memory if a cpu UP notification
1162 * and zs_init() race and both call zs_cpu_up() on the same cpu
1163 */
1164 if (area->vm_buf)
1165 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001166 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001167 if (!area->vm_buf)
1168 return -ENOMEM;
1169 return 0;
1170}
1171
1172static inline void __zs_cpu_down(struct mapping_area *area)
1173{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001174 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001175 area->vm_buf = NULL;
1176}
1177
1178static void *__zs_map_object(struct mapping_area *area,
1179 struct page *pages[2], int off, int size)
1180{
Seth Jennings5f6019022012-07-02 16:15:49 -05001181 int sizes[2];
1182 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001183 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001184
Seth Jenningsf5536462012-07-18 11:55:56 -05001185 /* disable page faults to match kmap_atomic() return conditions */
1186 pagefault_disable();
1187
1188 /* no read fastpath */
1189 if (area->vm_mm == ZS_MM_WO)
1190 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001191
1192 sizes[0] = PAGE_SIZE - off;
1193 sizes[1] = size - sizes[0];
1194
Seth Jennings5f6019022012-07-02 16:15:49 -05001195 /* copy object to per-cpu buffer */
1196 addr = kmap_atomic(pages[0]);
1197 memcpy(buf, addr + off, sizes[0]);
1198 kunmap_atomic(addr);
1199 addr = kmap_atomic(pages[1]);
1200 memcpy(buf + sizes[0], addr, sizes[1]);
1201 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001202out:
1203 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001204}
1205
Seth Jenningsf5536462012-07-18 11:55:56 -05001206static void __zs_unmap_object(struct mapping_area *area,
1207 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001208{
Seth Jennings5f6019022012-07-02 16:15:49 -05001209 int sizes[2];
1210 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001211 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001212
Seth Jenningsf5536462012-07-18 11:55:56 -05001213 /* no write fastpath */
1214 if (area->vm_mm == ZS_MM_RO)
1215 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001216
Minchan Kim7b60a682015-04-15 16:15:39 -07001217 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001218 buf = buf + ZS_HANDLE_SIZE;
1219 size -= ZS_HANDLE_SIZE;
1220 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001221
Seth Jennings5f6019022012-07-02 16:15:49 -05001222 sizes[0] = PAGE_SIZE - off;
1223 sizes[1] = size - sizes[0];
1224
1225 /* copy per-cpu buffer to object */
1226 addr = kmap_atomic(pages[0]);
1227 memcpy(addr + off, buf, sizes[0]);
1228 kunmap_atomic(addr);
1229 addr = kmap_atomic(pages[1]);
1230 memcpy(addr, buf + sizes[0], sizes[1]);
1231 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001232
1233out:
1234 /* enable page faults to match kunmap_atomic() return conditions */
1235 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001236}
Nitin Gupta61989a82012-01-09 16:51:56 -06001237
Christoph Hellwig8b136012020-06-01 21:50:53 -07001238#endif /* CONFIG_ZSMALLOC_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001239
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001240static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001241{
Nitin Gupta61989a82012-01-09 16:51:56 -06001242 struct mapping_area *area;
1243
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001244 area = &per_cpu(zs_map_area, cpu);
1245 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001246}
1247
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001248static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001249{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001250 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001251
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001252 area = &per_cpu(zs_map_area, cpu);
1253 __zs_cpu_down(area);
1254 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001255}
1256
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001257static bool can_merge(struct size_class *prev, int pages_per_zspage,
1258 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001259{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001260 if (prev->pages_per_zspage == pages_per_zspage &&
1261 prev->objs_per_zspage == objs_per_zspage)
1262 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001263
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001264 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001265}
1266
Minchan Kim37836892016-07-26 15:23:23 -07001267static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001268{
Minchan Kim37836892016-07-26 15:23:23 -07001269 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001270}
1271
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001272unsigned long zs_get_total_pages(struct zs_pool *pool)
1273{
1274 return atomic_long_read(&pool->pages_allocated);
1275}
1276EXPORT_SYMBOL_GPL(zs_get_total_pages);
1277
1278/**
1279 * zs_map_object - get address of allocated object from handle.
1280 * @pool: pool from which the object was allocated
1281 * @handle: handle returned from zs_malloc
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001282 * @mm: maping mode to use
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001283 *
1284 * Before using an object allocated from zs_malloc, it must be mapped using
1285 * this function. When done with the object, it must be unmapped using
1286 * zs_unmap_object.
1287 *
1288 * Only one object can be mapped per cpu at a time. There is no protection
1289 * against nested mappings.
1290 *
1291 * This function returns with preemption and page faults disabled.
1292 */
1293void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1294 enum zs_mapmode mm)
1295{
Minchan Kim37836892016-07-26 15:23:23 -07001296 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001297 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001298 unsigned long obj, off;
1299 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001300
1301 unsigned int class_idx;
1302 enum fullness_group fg;
1303 struct size_class *class;
1304 struct mapping_area *area;
1305 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001306 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001307
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001308 /*
1309 * Because we use per-cpu mapping areas shared among the
1310 * pools/users, we can't allow mapping in interrupt context
1311 * because it can corrupt another users mappings.
1312 */
Sergey Senozhatsky1aedcaf2017-11-15 17:34:03 -08001313 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001314
Minchan Kim312fcae2015-04-15 16:15:30 -07001315 /* From now on, migration cannot move the object */
1316 pin_tag(handle);
1317
Minchan Kim2e40e162015-04-15 16:15:23 -07001318 obj = handle_to_obj(handle);
1319 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001320 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001321
1322 /* migration cannot move any subpage in this zspage */
1323 migrate_read_lock(zspage);
1324
Minchan Kim37836892016-07-26 15:23:23 -07001325 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001326 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001327 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001328
1329 area = &get_cpu_var(zs_map_area);
1330 area->vm_mm = mm;
1331 if (off + class->size <= PAGE_SIZE) {
1332 /* this object is contained entirely within a page */
1333 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001334 ret = area->vm_addr + off;
1335 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001336 }
1337
1338 /* this object spans two pages */
1339 pages[0] = page;
1340 pages[1] = get_next_page(page);
1341 BUG_ON(!pages[1]);
1342
Minchan Kim2e40e162015-04-15 16:15:23 -07001343 ret = __zs_map_object(area, pages, off, class->size);
1344out:
Minchan Kim48b48002016-07-26 15:23:31 -07001345 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001346 ret += ZS_HANDLE_SIZE;
1347
1348 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001349}
1350EXPORT_SYMBOL_GPL(zs_map_object);
1351
1352void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1353{
Minchan Kim37836892016-07-26 15:23:23 -07001354 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001355 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001356 unsigned long obj, off;
1357 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001358
1359 unsigned int class_idx;
1360 enum fullness_group fg;
1361 struct size_class *class;
1362 struct mapping_area *area;
1363
Minchan Kim2e40e162015-04-15 16:15:23 -07001364 obj = handle_to_obj(handle);
1365 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001366 zspage = get_zspage(page);
1367 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001368 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001369 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001370
1371 area = this_cpu_ptr(&zs_map_area);
1372 if (off + class->size <= PAGE_SIZE)
1373 kunmap_atomic(area->vm_addr);
1374 else {
1375 struct page *pages[2];
1376
1377 pages[0] = page;
1378 pages[1] = get_next_page(page);
1379 BUG_ON(!pages[1]);
1380
1381 __zs_unmap_object(area, pages, off, class->size);
1382 }
1383 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001384
1385 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001386 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001387}
1388EXPORT_SYMBOL_GPL(zs_unmap_object);
1389
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07001390/**
1391 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1392 * zsmalloc &size_class.
1393 * @pool: zsmalloc pool to use
1394 *
1395 * The function returns the size of the first huge class - any object of equal
1396 * or bigger size will be stored in zspage consisting of a single physical
1397 * page.
1398 *
1399 * Context: Any context.
1400 *
1401 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1402 */
1403size_t zs_huge_class_size(struct zs_pool *pool)
1404{
1405 return huge_class_size;
1406}
1407EXPORT_SYMBOL_GPL(zs_huge_class_size);
1408
Minchan Kim251cbb92016-05-20 16:59:42 -07001409static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001410 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001411{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001412 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001413 unsigned long obj;
1414 struct link_free *link;
1415
1416 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001417 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001418 void *vaddr;
1419
Minchan Kim312fcae2015-04-15 16:15:30 -07001420 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001421 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001422
1423 offset = obj * class->size;
1424 nr_page = offset >> PAGE_SHIFT;
1425 m_offset = offset & ~PAGE_MASK;
1426 m_page = get_first_page(zspage);
1427
1428 for (i = 0; i < nr_page; i++)
1429 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001430
1431 vaddr = kmap_atomic(m_page);
1432 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001433 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001434 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001435 /* record handle in the header of allocated chunk */
1436 link->handle = handle;
1437 else
Minchan Kim37836892016-07-26 15:23:23 -07001438 /* record handle to page->index */
1439 zspage->first_page->index = handle;
1440
Minchan Kimc7806262015-04-15 16:15:26 -07001441 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001442 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001443 zs_stat_inc(class, OBJ_USED, 1);
1444
Minchan Kimbfd093f2016-07-26 15:23:28 -07001445 obj = location_to_obj(m_page, obj);
1446
Minchan Kimc7806262015-04-15 16:15:26 -07001447 return obj;
1448}
1449
1450
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001451/**
1452 * zs_malloc - Allocate block of given size from pool.
1453 * @pool: pool to allocate from
1454 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001455 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001456 *
1457 * On success, handle to the allocated object is returned,
1458 * otherwise 0.
1459 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1460 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001461unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001462{
Minchan Kim2e40e162015-04-15 16:15:23 -07001463 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001464 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001465 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001466 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001467
Minchan Kim7b60a682015-04-15 16:15:39 -07001468 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001469 return 0;
1470
Minchan Kim37836892016-07-26 15:23:23 -07001471 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001472 if (!handle)
1473 return 0;
1474
1475 /* extra space in chunk to keep the handle */
1476 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001477 class = pool->size_class[get_size_class_index(size)];
1478
1479 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001480 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001481 if (likely(zspage)) {
1482 obj = obj_malloc(class, zspage, handle);
1483 /* Now move the zspage to another fullness group, if required */
1484 fix_fullness_group(class, zspage);
1485 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001486 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001487
Minchan Kim48b48002016-07-26 15:23:31 -07001488 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001489 }
1490
Minchan Kim48b48002016-07-26 15:23:31 -07001491 spin_unlock(&class->lock);
1492
1493 zspage = alloc_zspage(pool, class, gfp);
1494 if (!zspage) {
1495 cache_free_handle(pool, handle);
1496 return 0;
1497 }
1498
1499 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001500 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001501 newfg = get_fullness_group(class, zspage);
1502 insert_zspage(class, zspage, newfg);
1503 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001504 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001505 atomic_long_add(class->pages_per_zspage,
1506 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001507 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001508
1509 /* We completely set up zspage so mark them as movable */
1510 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001511 spin_unlock(&class->lock);
1512
Minchan Kim2e40e162015-04-15 16:15:23 -07001513 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001514}
1515EXPORT_SYMBOL_GPL(zs_malloc);
1516
Minchan Kim1ee47162016-05-20 16:59:45 -07001517static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001518{
1519 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001520 struct zspage *zspage;
1521 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001522 unsigned long f_offset;
1523 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001524 void *vaddr;
1525
Minchan Kim312fcae2015-04-15 16:15:30 -07001526 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001527 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001528 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001529 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001530
Minchan Kimc7806262015-04-15 16:15:26 -07001531 vaddr = kmap_atomic(f_page);
1532
1533 /* Insert this object in containing zspage's freelist */
1534 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001535 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001536 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001537 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001538 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001539 zs_stat_dec(class, OBJ_USED, 1);
1540}
1541
1542void zs_free(struct zs_pool *pool, unsigned long handle)
1543{
Minchan Kim37836892016-07-26 15:23:23 -07001544 struct zspage *zspage;
1545 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001546 unsigned long obj;
1547 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548 int class_idx;
1549 struct size_class *class;
1550 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001551 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001552
Minchan Kim2e40e162015-04-15 16:15:23 -07001553 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001554 return;
1555
Minchan Kim312fcae2015-04-15 16:15:30 -07001556 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001557 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001558 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001559 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001560
Minchan Kim48b48002016-07-26 15:23:31 -07001561 migrate_read_lock(zspage);
1562
Minchan Kim37836892016-07-26 15:23:23 -07001563 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001564 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001565
1566 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001567 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001568 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001569 if (fullness != ZS_EMPTY) {
1570 migrate_read_unlock(zspage);
1571 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001572 }
Minchan Kim48b48002016-07-26 15:23:31 -07001573
1574 isolated = is_zspage_isolated(zspage);
1575 migrate_read_unlock(zspage);
1576 /* If zspage is isolated, zs_page_putback will free the zspage */
1577 if (likely(!isolated))
1578 free_zspage(pool, class, zspage);
1579out:
1580
Minchan Kim312fcae2015-04-15 16:15:30 -07001581 spin_unlock(&class->lock);
1582 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001583 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001584}
1585EXPORT_SYMBOL_GPL(zs_free);
1586
Minchan Kim251cbb92016-05-20 16:59:42 -07001587static void zs_object_copy(struct size_class *class, unsigned long dst,
1588 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001589{
1590 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001591 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001592 unsigned long s_off, d_off;
1593 void *s_addr, *d_addr;
1594 int s_size, d_size, size;
1595 int written = 0;
1596
1597 s_size = d_size = class->size;
1598
1599 obj_to_location(src, &s_page, &s_objidx);
1600 obj_to_location(dst, &d_page, &d_objidx);
1601
Minchan Kimbfd093f2016-07-26 15:23:28 -07001602 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1603 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001604
1605 if (s_off + class->size > PAGE_SIZE)
1606 s_size = PAGE_SIZE - s_off;
1607
1608 if (d_off + class->size > PAGE_SIZE)
1609 d_size = PAGE_SIZE - d_off;
1610
1611 s_addr = kmap_atomic(s_page);
1612 d_addr = kmap_atomic(d_page);
1613
1614 while (1) {
1615 size = min(s_size, d_size);
1616 memcpy(d_addr + d_off, s_addr + s_off, size);
1617 written += size;
1618
1619 if (written == class->size)
1620 break;
1621
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001622 s_off += size;
1623 s_size -= size;
1624 d_off += size;
1625 d_size -= size;
1626
1627 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001628 kunmap_atomic(d_addr);
1629 kunmap_atomic(s_addr);
1630 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001631 s_addr = kmap_atomic(s_page);
1632 d_addr = kmap_atomic(d_page);
1633 s_size = class->size - written;
1634 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001635 }
1636
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001637 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001638 kunmap_atomic(d_addr);
1639 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001640 d_addr = kmap_atomic(d_page);
1641 d_size = class->size - written;
1642 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001643 }
1644 }
1645
1646 kunmap_atomic(d_addr);
1647 kunmap_atomic(s_addr);
1648}
1649
1650/*
1651 * Find alloced object in zspage from index object and
1652 * return handle.
1653 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001654static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001655 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001656{
1657 unsigned long head;
1658 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001659 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001660 unsigned long handle = 0;
1661 void *addr = kmap_atomic(page);
1662
Minchan Kim37836892016-07-26 15:23:23 -07001663 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001664 offset += class->size * index;
1665
1666 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001667 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001668 if (head & OBJ_ALLOCATED_TAG) {
1669 handle = head & ~OBJ_ALLOCATED_TAG;
1670 if (trypin_tag(handle))
1671 break;
1672 handle = 0;
1673 }
1674
1675 offset += class->size;
1676 index++;
1677 }
1678
1679 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001680
1681 *obj_idx = index;
1682
Minchan Kim312fcae2015-04-15 16:15:30 -07001683 return handle;
1684}
1685
1686struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001687 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001688 struct page *s_page;
1689 /* Destination page for migration which should be a first page
1690 * of zspage. */
1691 struct page *d_page;
1692 /* Starting object index within @s_page which used for live object
1693 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001694 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001695};
1696
1697static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1698 struct zs_compact_control *cc)
1699{
1700 unsigned long used_obj, free_obj;
1701 unsigned long handle;
1702 struct page *s_page = cc->s_page;
1703 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001704 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001705 int ret = 0;
1706
1707 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001708 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001709 if (!handle) {
1710 s_page = get_next_page(s_page);
1711 if (!s_page)
1712 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001713 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001714 continue;
1715 }
1716
1717 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001718 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001719 unpin_tag(handle);
1720 ret = -ENOMEM;
1721 break;
1722 }
1723
1724 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001725 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001726 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001727 obj_idx++;
Junil Leec102f07c2016-01-20 14:58:18 -08001728 /*
1729 * record_obj updates handle's value to free_obj and it will
1730 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1731 * breaks synchronization using pin_tag(e,g, zs_free) so
1732 * let's keep the lock bit.
1733 */
1734 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001735 record_obj(handle, free_obj);
1736 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001737 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001738 }
1739
1740 /* Remember last position in this iteration */
1741 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001742 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001743
1744 return ret;
1745}
1746
Minchan Kim37836892016-07-26 15:23:23 -07001747static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001748{
1749 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001750 struct zspage *zspage;
1751 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001752
Minchan Kim37836892016-07-26 15:23:23 -07001753 if (!source) {
1754 fg[0] = ZS_ALMOST_FULL;
1755 fg[1] = ZS_ALMOST_EMPTY;
1756 }
1757
1758 for (i = 0; i < 2; i++) {
1759 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1760 struct zspage, list);
1761 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001762 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001763 remove_zspage(class, zspage, fg[i]);
1764 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001765 }
1766 }
1767
Minchan Kim37836892016-07-26 15:23:23 -07001768 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001769}
1770
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001771/*
Minchan Kim37836892016-07-26 15:23:23 -07001772 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001773 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001774 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001775 *
Minchan Kim37836892016-07-26 15:23:23 -07001776 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001777 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001778static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001779 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001780{
Minchan Kim312fcae2015-04-15 16:15:30 -07001781 enum fullness_group fullness;
1782
Minchan Kim48b48002016-07-26 15:23:31 -07001783 VM_BUG_ON(is_zspage_isolated(zspage));
1784
Minchan Kim37836892016-07-26 15:23:23 -07001785 fullness = get_fullness_group(class, zspage);
1786 insert_zspage(class, zspage, fullness);
1787 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001788
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001789 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001790}
1791
Minchan Kim48b48002016-07-26 15:23:31 -07001792#ifdef CONFIG_COMPACTION
Colin Ian King4d0a5402018-08-17 15:46:50 -07001793/*
1794 * To prevent zspage destroy during migration, zspage freeing should
1795 * hold locks of all pages in the zspage.
1796 */
1797static void lock_zspage(struct zspage *zspage)
1798{
1799 struct page *page = get_first_page(zspage);
1800
1801 do {
1802 lock_page(page);
1803 } while ((page = get_next_page(page)) != NULL);
1804}
1805
David Howells8e9231f2019-03-25 16:38:23 +00001806static int zs_init_fs_context(struct fs_context *fc)
Minchan Kim48b48002016-07-26 15:23:31 -07001807{
David Howells8e9231f2019-03-25 16:38:23 +00001808 return init_pseudo(fc, ZSMALLOC_MAGIC) ? 0 : -ENOMEM;
Minchan Kim48b48002016-07-26 15:23:31 -07001809}
1810
1811static struct file_system_type zsmalloc_fs = {
1812 .name = "zsmalloc",
David Howells8e9231f2019-03-25 16:38:23 +00001813 .init_fs_context = zs_init_fs_context,
Minchan Kim48b48002016-07-26 15:23:31 -07001814 .kill_sb = kill_anon_super,
1815};
1816
1817static int zsmalloc_mount(void)
1818{
1819 int ret = 0;
1820
1821 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1822 if (IS_ERR(zsmalloc_mnt))
1823 ret = PTR_ERR(zsmalloc_mnt);
1824
1825 return ret;
1826}
1827
1828static void zsmalloc_unmount(void)
1829{
1830 kern_unmount(zsmalloc_mnt);
1831}
1832
1833static void migrate_lock_init(struct zspage *zspage)
1834{
1835 rwlock_init(&zspage->lock);
1836}
1837
Jules Irengecfc451cf2020-04-06 20:08:21 -07001838static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock)
Minchan Kim48b48002016-07-26 15:23:31 -07001839{
1840 read_lock(&zspage->lock);
1841}
1842
Jules Irenge8a374cc2020-04-06 20:08:24 -07001843static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock)
Minchan Kim48b48002016-07-26 15:23:31 -07001844{
1845 read_unlock(&zspage->lock);
1846}
1847
1848static void migrate_write_lock(struct zspage *zspage)
1849{
1850 write_lock(&zspage->lock);
1851}
1852
1853static void migrate_write_unlock(struct zspage *zspage)
1854{
1855 write_unlock(&zspage->lock);
1856}
1857
1858/* Number of isolated subpage for *page migration* in this zspage */
1859static void inc_zspage_isolation(struct zspage *zspage)
1860{
1861 zspage->isolated++;
1862}
1863
1864static void dec_zspage_isolation(struct zspage *zspage)
1865{
1866 zspage->isolated--;
1867}
1868
Henry Burns1a87aa02019-08-24 17:55:03 -07001869static void putback_zspage_deferred(struct zs_pool *pool,
1870 struct size_class *class,
1871 struct zspage *zspage)
1872{
1873 enum fullness_group fg;
1874
1875 fg = putback_zspage(class, zspage);
1876 if (fg == ZS_EMPTY)
1877 schedule_work(&pool->free_work);
1878
1879}
1880
Henry Burns701d6782019-08-24 17:55:06 -07001881static inline void zs_pool_dec_isolated(struct zs_pool *pool)
1882{
1883 VM_BUG_ON(atomic_long_read(&pool->isolated_pages) <= 0);
1884 atomic_long_dec(&pool->isolated_pages);
1885 /*
1886 * There's no possibility of racing, since wait_for_isolated_drain()
1887 * checks the isolated count under &class->lock after enqueuing
1888 * on migration_wait.
1889 */
1890 if (atomic_long_read(&pool->isolated_pages) == 0 && pool->destroying)
1891 wake_up_all(&pool->migration_wait);
1892}
1893
Minchan Kim48b48002016-07-26 15:23:31 -07001894static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1895 struct page *newpage, struct page *oldpage)
1896{
1897 struct page *page;
1898 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1899 int idx = 0;
1900
1901 page = get_first_page(zspage);
1902 do {
1903 if (page == oldpage)
1904 pages[idx] = newpage;
1905 else
1906 pages[idx] = page;
1907 idx++;
1908 } while ((page = get_next_page(page)) != NULL);
1909
1910 create_page_chain(class, zspage, pages);
1911 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1912 if (unlikely(PageHugeObject(oldpage)))
1913 newpage->index = oldpage->index;
1914 __SetPageMovable(newpage, page_mapping(oldpage));
1915}
1916
Colin Ian King4d0a5402018-08-17 15:46:50 -07001917static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
Minchan Kim48b48002016-07-26 15:23:31 -07001918{
1919 struct zs_pool *pool;
1920 struct size_class *class;
1921 int class_idx;
1922 enum fullness_group fullness;
1923 struct zspage *zspage;
1924 struct address_space *mapping;
1925
1926 /*
1927 * Page is locked so zspage couldn't be destroyed. For detail, look at
1928 * lock_zspage in free_zspage.
1929 */
1930 VM_BUG_ON_PAGE(!PageMovable(page), page);
1931 VM_BUG_ON_PAGE(PageIsolated(page), page);
1932
1933 zspage = get_zspage(page);
1934
1935 /*
1936 * Without class lock, fullness could be stale while class_idx is okay
1937 * because class_idx is constant unless page is freed so we should get
1938 * fullness again under class lock.
1939 */
1940 get_zspage_mapping(zspage, &class_idx, &fullness);
1941 mapping = page_mapping(page);
1942 pool = mapping->private_data;
1943 class = pool->size_class[class_idx];
1944
1945 spin_lock(&class->lock);
1946 if (get_zspage_inuse(zspage) == 0) {
1947 spin_unlock(&class->lock);
1948 return false;
1949 }
1950
1951 /* zspage is isolated for object migration */
1952 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1953 spin_unlock(&class->lock);
1954 return false;
1955 }
1956
1957 /*
1958 * If this is first time isolation for the zspage, isolate zspage from
1959 * size_class to prevent further object allocation from the zspage.
1960 */
1961 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1962 get_zspage_mapping(zspage, &class_idx, &fullness);
Henry Burns701d6782019-08-24 17:55:06 -07001963 atomic_long_inc(&pool->isolated_pages);
Minchan Kim48b48002016-07-26 15:23:31 -07001964 remove_zspage(class, zspage, fullness);
1965 }
1966
1967 inc_zspage_isolation(zspage);
1968 spin_unlock(&class->lock);
1969
1970 return true;
1971}
1972
Colin Ian King4d0a5402018-08-17 15:46:50 -07001973static int zs_page_migrate(struct address_space *mapping, struct page *newpage,
Minchan Kim48b48002016-07-26 15:23:31 -07001974 struct page *page, enum migrate_mode mode)
1975{
1976 struct zs_pool *pool;
1977 struct size_class *class;
1978 int class_idx;
1979 enum fullness_group fullness;
1980 struct zspage *zspage;
1981 struct page *dummy;
1982 void *s_addr, *d_addr, *addr;
1983 int offset, pos;
1984 unsigned long handle, head;
1985 unsigned long old_obj, new_obj;
1986 unsigned int obj_idx;
1987 int ret = -EAGAIN;
1988
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001989 /*
1990 * We cannot support the _NO_COPY case here, because copy needs to
1991 * happen under the zs lock, which does not work with
1992 * MIGRATE_SYNC_NO_COPY workflow.
1993 */
1994 if (mode == MIGRATE_SYNC_NO_COPY)
1995 return -EINVAL;
1996
Minchan Kim48b48002016-07-26 15:23:31 -07001997 VM_BUG_ON_PAGE(!PageMovable(page), page);
1998 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1999
2000 zspage = get_zspage(page);
2001
2002 /* Concurrent compactor cannot migrate any subpage in zspage */
2003 migrate_write_lock(zspage);
2004 get_zspage_mapping(zspage, &class_idx, &fullness);
2005 pool = mapping->private_data;
2006 class = pool->size_class[class_idx];
2007 offset = get_first_obj_offset(page);
2008
2009 spin_lock(&class->lock);
2010 if (!get_zspage_inuse(zspage)) {
Hui Zhu77ff4652017-09-06 16:21:08 -07002011 /*
2012 * Set "offset" to end of the page so that every loops
2013 * skips unnecessary object scanning.
2014 */
2015 offset = PAGE_SIZE;
Minchan Kim48b48002016-07-26 15:23:31 -07002016 }
2017
2018 pos = offset;
2019 s_addr = kmap_atomic(page);
2020 while (pos < PAGE_SIZE) {
2021 head = obj_to_head(page, s_addr + pos);
2022 if (head & OBJ_ALLOCATED_TAG) {
2023 handle = head & ~OBJ_ALLOCATED_TAG;
2024 if (!trypin_tag(handle))
2025 goto unpin_objects;
2026 }
2027 pos += class->size;
2028 }
2029
2030 /*
2031 * Here, any user cannot access all objects in the zspage so let's move.
2032 */
2033 d_addr = kmap_atomic(newpage);
2034 memcpy(d_addr, s_addr, PAGE_SIZE);
2035 kunmap_atomic(d_addr);
2036
2037 for (addr = s_addr + offset; addr < s_addr + pos;
2038 addr += class->size) {
2039 head = obj_to_head(page, addr);
2040 if (head & OBJ_ALLOCATED_TAG) {
2041 handle = head & ~OBJ_ALLOCATED_TAG;
2042 if (!testpin_tag(handle))
2043 BUG();
2044
2045 old_obj = handle_to_obj(handle);
2046 obj_to_location(old_obj, &dummy, &obj_idx);
2047 new_obj = (unsigned long)location_to_obj(newpage,
2048 obj_idx);
2049 new_obj |= BIT(HANDLE_PIN_BIT);
2050 record_obj(handle, new_obj);
2051 }
2052 }
2053
2054 replace_sub_page(class, zspage, newpage, page);
2055 get_page(newpage);
2056
2057 dec_zspage_isolation(zspage);
2058
2059 /*
2060 * Page migration is done so let's putback isolated zspage to
2061 * the list if @page is final isolated subpage in the zspage.
2062 */
Henry Burns701d6782019-08-24 17:55:06 -07002063 if (!is_zspage_isolated(zspage)) {
2064 /*
2065 * We cannot race with zs_destroy_pool() here because we wait
2066 * for isolation to hit zero before we start destroying.
2067 * Also, we ensure that everyone can see pool->destroying before
2068 * we start waiting.
2069 */
Henry Burns1a87aa02019-08-24 17:55:03 -07002070 putback_zspage_deferred(pool, class, zspage);
Henry Burns701d6782019-08-24 17:55:06 -07002071 zs_pool_dec_isolated(pool);
2072 }
Minchan Kim48b48002016-07-26 15:23:31 -07002073
Chanho Minac8f05d2020-01-04 12:59:36 -08002074 if (page_zone(newpage) != page_zone(page)) {
2075 dec_zone_page_state(page, NR_ZSPAGES);
2076 inc_zone_page_state(newpage, NR_ZSPAGES);
2077 }
2078
Minchan Kim48b48002016-07-26 15:23:31 -07002079 reset_page(page);
2080 put_page(page);
2081 page = newpage;
2082
Minchan Kimdd4123f2016-07-26 15:26:50 -07002083 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002084unpin_objects:
2085 for (addr = s_addr + offset; addr < s_addr + pos;
2086 addr += class->size) {
2087 head = obj_to_head(page, addr);
2088 if (head & OBJ_ALLOCATED_TAG) {
2089 handle = head & ~OBJ_ALLOCATED_TAG;
2090 if (!testpin_tag(handle))
2091 BUG();
2092 unpin_tag(handle);
2093 }
2094 }
2095 kunmap_atomic(s_addr);
Minchan Kim48b48002016-07-26 15:23:31 -07002096 spin_unlock(&class->lock);
2097 migrate_write_unlock(zspage);
2098
2099 return ret;
2100}
2101
Colin Ian King4d0a5402018-08-17 15:46:50 -07002102static void zs_page_putback(struct page *page)
Minchan Kim48b48002016-07-26 15:23:31 -07002103{
2104 struct zs_pool *pool;
2105 struct size_class *class;
2106 int class_idx;
2107 enum fullness_group fg;
2108 struct address_space *mapping;
2109 struct zspage *zspage;
2110
2111 VM_BUG_ON_PAGE(!PageMovable(page), page);
2112 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2113
2114 zspage = get_zspage(page);
2115 get_zspage_mapping(zspage, &class_idx, &fg);
2116 mapping = page_mapping(page);
2117 pool = mapping->private_data;
2118 class = pool->size_class[class_idx];
2119
2120 spin_lock(&class->lock);
2121 dec_zspage_isolation(zspage);
2122 if (!is_zspage_isolated(zspage)) {
Minchan Kim48b48002016-07-26 15:23:31 -07002123 /*
2124 * Due to page_lock, we cannot free zspage immediately
2125 * so let's defer.
2126 */
Henry Burns1a87aa02019-08-24 17:55:03 -07002127 putback_zspage_deferred(pool, class, zspage);
Henry Burns701d6782019-08-24 17:55:06 -07002128 zs_pool_dec_isolated(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002129 }
2130 spin_unlock(&class->lock);
2131}
2132
Colin Ian King4d0a5402018-08-17 15:46:50 -07002133static const struct address_space_operations zsmalloc_aops = {
Minchan Kim48b48002016-07-26 15:23:31 -07002134 .isolate_page = zs_page_isolate,
2135 .migratepage = zs_page_migrate,
2136 .putback_page = zs_page_putback,
2137};
2138
2139static int zs_register_migration(struct zs_pool *pool)
2140{
2141 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2142 if (IS_ERR(pool->inode)) {
2143 pool->inode = NULL;
2144 return 1;
2145 }
2146
2147 pool->inode->i_mapping->private_data = pool;
2148 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2149 return 0;
2150}
2151
Henry Burns701d6782019-08-24 17:55:06 -07002152static bool pool_isolated_are_drained(struct zs_pool *pool)
2153{
2154 return atomic_long_read(&pool->isolated_pages) == 0;
2155}
2156
2157/* Function for resolving migration */
2158static void wait_for_isolated_drain(struct zs_pool *pool)
2159{
2160
2161 /*
2162 * We're in the process of destroying the pool, so there are no
2163 * active allocations. zs_page_isolate() fails for completely free
2164 * zspages, so we need only wait for the zs_pool's isolated
2165 * count to hit zero.
2166 */
2167 wait_event(pool->migration_wait,
2168 pool_isolated_are_drained(pool));
2169}
2170
Minchan Kim48b48002016-07-26 15:23:31 -07002171static void zs_unregister_migration(struct zs_pool *pool)
2172{
Henry Burns701d6782019-08-24 17:55:06 -07002173 pool->destroying = true;
2174 /*
2175 * We need a memory barrier here to ensure global visibility of
2176 * pool->destroying. Thus pool->isolated pages will either be 0 in which
2177 * case we don't care, or it will be > 0 and pool->destroying will
2178 * ensure that we wake up once isolation hits 0.
2179 */
2180 smp_mb();
2181 wait_for_isolated_drain(pool); /* This can block */
Minchan Kim48b48002016-07-26 15:23:31 -07002182 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002183 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002184}
2185
2186/*
2187 * Caller should hold page_lock of all pages in the zspage
2188 * In here, we cannot use zspage meta data.
2189 */
2190static void async_free_zspage(struct work_struct *work)
2191{
2192 int i;
2193 struct size_class *class;
2194 unsigned int class_idx;
2195 enum fullness_group fullness;
2196 struct zspage *zspage, *tmp;
2197 LIST_HEAD(free_pages);
2198 struct zs_pool *pool = container_of(work, struct zs_pool,
2199 free_work);
2200
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002201 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim48b48002016-07-26 15:23:31 -07002202 class = pool->size_class[i];
2203 if (class->index != i)
2204 continue;
2205
2206 spin_lock(&class->lock);
2207 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2208 spin_unlock(&class->lock);
2209 }
2210
2211
2212 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2213 list_del(&zspage->list);
2214 lock_zspage(zspage);
2215
2216 get_zspage_mapping(zspage, &class_idx, &fullness);
2217 VM_BUG_ON(fullness != ZS_EMPTY);
2218 class = pool->size_class[class_idx];
2219 spin_lock(&class->lock);
2220 __free_zspage(pool, pool->size_class[class_idx], zspage);
2221 spin_unlock(&class->lock);
2222 }
2223};
2224
2225static void kick_deferred_free(struct zs_pool *pool)
2226{
2227 schedule_work(&pool->free_work);
2228}
2229
2230static void init_deferred_free(struct zs_pool *pool)
2231{
2232 INIT_WORK(&pool->free_work, async_free_zspage);
2233}
2234
2235static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2236{
2237 struct page *page = get_first_page(zspage);
2238
2239 do {
2240 WARN_ON(!trylock_page(page));
2241 __SetPageMovable(page, pool->inode->i_mapping);
2242 unlock_page(page);
2243 } while ((page = get_next_page(page)) != NULL);
2244}
2245#endif
2246
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002247/*
2248 *
2249 * Based on the number of unused allocated objects calculate
2250 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002251 */
2252static unsigned long zs_can_compact(struct size_class *class)
2253{
2254 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002255 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2256 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002257
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002258 if (obj_allocated <= obj_used)
2259 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002260
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002261 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002262 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002263
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002264 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002265}
2266
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002267static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002268{
Minchan Kim312fcae2015-04-15 16:15:30 -07002269 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002270 struct zspage *src_zspage;
2271 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002272
Minchan Kim312fcae2015-04-15 16:15:30 -07002273 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002274 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002275
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002276 if (!zs_can_compact(class))
2277 break;
2278
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002279 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002280 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002281
Minchan Kim37836892016-07-26 15:23:23 -07002282 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002283 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002284 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002285 * If there is no more space in dst_page, resched
2286 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002287 */
2288 if (!migrate_zspage(pool, class, &cc))
2289 break;
2290
Minchan Kim4aa409c2016-07-26 15:23:26 -07002291 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002292 }
2293
2294 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002295 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002296 break;
2297
Minchan Kim4aa409c2016-07-26 15:23:26 -07002298 putback_zspage(class, dst_zspage);
2299 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002300 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002301 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002302 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002303 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002304 cond_resched();
2305 spin_lock(&class->lock);
2306 }
2307
Minchan Kim37836892016-07-26 15:23:23 -07002308 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002309 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002310
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002311 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002312}
2313
2314unsigned long zs_compact(struct zs_pool *pool)
2315{
2316 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002317 struct size_class *class;
2318
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002319 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002320 class = pool->size_class[i];
2321 if (!class)
2322 continue;
2323 if (class->index != i)
2324 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002325 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002326 }
2327
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002328 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002329}
2330EXPORT_SYMBOL_GPL(zs_compact);
2331
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002332void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2333{
2334 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2335}
2336EXPORT_SYMBOL_GPL(zs_pool_stats);
2337
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002338static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2339 struct shrink_control *sc)
2340{
2341 unsigned long pages_freed;
2342 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2343 shrinker);
2344
2345 pages_freed = pool->stats.pages_compacted;
2346 /*
2347 * Compact classes and calculate compaction delta.
2348 * Can run concurrently with a manually triggered
2349 * (by user) compaction.
2350 */
2351 pages_freed = zs_compact(pool) - pages_freed;
2352
2353 return pages_freed ? pages_freed : SHRINK_STOP;
2354}
2355
2356static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2357 struct shrink_control *sc)
2358{
2359 int i;
2360 struct size_class *class;
2361 unsigned long pages_to_free = 0;
2362 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2363 shrinker);
2364
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002365 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002366 class = pool->size_class[i];
2367 if (!class)
2368 continue;
2369 if (class->index != i)
2370 continue;
2371
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002372 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002373 }
2374
2375 return pages_to_free;
2376}
2377
2378static void zs_unregister_shrinker(struct zs_pool *pool)
2379{
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002380 unregister_shrinker(&pool->shrinker);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002381}
2382
2383static int zs_register_shrinker(struct zs_pool *pool)
2384{
2385 pool->shrinker.scan_objects = zs_shrinker_scan;
2386 pool->shrinker.count_objects = zs_shrinker_count;
2387 pool->shrinker.batch = 0;
2388 pool->shrinker.seeks = DEFAULT_SEEKS;
2389
2390 return register_shrinker(&pool->shrinker);
2391}
2392
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002393/**
2394 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002395 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002396 *
2397 * This function must be called before anything when using
2398 * the zsmalloc allocator.
2399 *
2400 * On success, a pointer to the newly created pool is returned,
2401 * otherwise NULL.
2402 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002403struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002404{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002405 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002406 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002407 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002408
Ganesh Mahendran18136652014-12-12 16:57:10 -08002409 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002410 if (!pool)
2411 return NULL;
2412
Minchan Kim48b48002016-07-26 15:23:31 -07002413 init_deferred_free(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002414
Minchan Kim2e40e162015-04-15 16:15:23 -07002415 pool->name = kstrdup(name, GFP_KERNEL);
2416 if (!pool->name)
2417 goto err;
2418
Andrew Morton441e2542019-08-30 16:04:35 -07002419#ifdef CONFIG_COMPACTION
Henry Burns701d6782019-08-24 17:55:06 -07002420 init_waitqueue_head(&pool->migration_wait);
Andrew Morton441e2542019-08-30 16:04:35 -07002421#endif
Henry Burns701d6782019-08-24 17:55:06 -07002422
Minchan Kim37836892016-07-26 15:23:23 -07002423 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002424 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002425
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002426 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002427 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002428 * for merging should be larger or equal to current size.
2429 */
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002430 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002431 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002432 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002433 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002434 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002435 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002436
2437 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2438 if (size > ZS_MAX_ALLOC_SIZE)
2439 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002440 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002441 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002442
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002443 /*
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07002444 * We iterate from biggest down to smallest classes,
2445 * so huge_class_size holds the size of the first huge
2446 * class. Any object bigger than or equal to that will
2447 * endup in the huge class.
2448 */
2449 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2450 !huge_class_size) {
2451 huge_class_size = size;
2452 /*
2453 * The object uses ZS_HANDLE_SIZE bytes to store the
2454 * handle. We need to subtract it, because zs_malloc()
2455 * unconditionally adds handle size before it performs
2456 * size class search - so object may be smaller than
2457 * huge class size, yet it still can end up in the huge
2458 * class because it grows by ZS_HANDLE_SIZE extra bytes
2459 * right before class lookup.
2460 */
2461 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2462 }
2463
2464 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002465 * size_class is used for normal zsmalloc operation such
2466 * as alloc/free for that size. Although it is natural that we
2467 * have one size_class for each size, there is a chance that we
2468 * can get more memory utilization if we use one size_class for
2469 * many different sizes whose size_class have same
2470 * characteristics. So, we makes size_class point to
2471 * previous size_class if possible.
2472 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002473 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002474 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002475 pool->size_class[i] = prev_class;
2476 continue;
2477 }
2478 }
2479
2480 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2481 if (!class)
2482 goto err;
2483
Nitin Gupta61989a82012-01-09 16:51:56 -06002484 class->size = size;
2485 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002486 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002487 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002488 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002489 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002490 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2491 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002492 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002493
2494 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002495 }
2496
Dan Streetmand34f6152016-05-20 16:59:56 -07002497 /* debug only, don't abort if it fails */
2498 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002499
Minchan Kim48b48002016-07-26 15:23:31 -07002500 if (zs_register_migration(pool))
2501 goto err;
2502
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002503 /*
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002504 * Not critical since shrinker is only used to trigger internal
2505 * defragmentation of the pool which is pretty optional thing. If
2506 * registration fails we still can use the pool normally and user can
2507 * trigger compaction manually. Thus, ignore return code.
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002508 */
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002509 zs_register_shrinker(pool);
2510
Nitin Gupta61989a82012-01-09 16:51:56 -06002511 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002512
2513err:
2514 zs_destroy_pool(pool);
2515 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002516}
2517EXPORT_SYMBOL_GPL(zs_create_pool);
2518
2519void zs_destroy_pool(struct zs_pool *pool)
2520{
2521 int i;
2522
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002523 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002524 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002525 zs_pool_stat_destroy(pool);
2526
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002527 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002528 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002529 struct size_class *class = pool->size_class[i];
2530
2531 if (!class)
2532 continue;
2533
2534 if (class->index != i)
2535 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002536
Minchan Kim48b48002016-07-26 15:23:31 -07002537 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002538 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002539 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002540 class->size, fg);
2541 }
2542 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002543 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002544 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002545
Minchan Kim37836892016-07-26 15:23:23 -07002546 destroy_cache(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002547 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002548 kfree(pool);
2549}
2550EXPORT_SYMBOL_GPL(zs_destroy_pool);
2551
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002552static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002553{
Minchan Kim48b48002016-07-26 15:23:31 -07002554 int ret;
2555
2556 ret = zsmalloc_mount();
2557 if (ret)
2558 goto out;
2559
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002560 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2561 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002562 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002563 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002564
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002565#ifdef CONFIG_ZPOOL
2566 zpool_register_driver(&zs_zpool_driver);
2567#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002568
Dan Streetman4abaac92016-05-26 15:16:27 -07002569 zs_stat_init();
2570
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002571 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002572
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002573hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002574 zsmalloc_unmount();
2575out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002576 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002577}
Nitin Gupta61989a82012-01-09 16:51:56 -06002578
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002579static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002580{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002581#ifdef CONFIG_ZPOOL
2582 zpool_unregister_driver(&zs_zpool_driver);
2583#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002584 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002585 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002586
2587 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002588}
Ben Hutchings069f1012012-06-20 02:31:11 +01002589
2590module_init(zs_init);
2591module_exit(zs_exit);
2592
2593MODULE_LICENSE("Dual BSD/GPL");
2594MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");