blob: cf15851a7d2fb030c79328fd1ef8bf7c0c8a19ef [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
Minchan Kim48b48002016-07-26 15:23:31 -070028 * PG_owner_priv_1: indentifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070029 *
30 */
31
Dan Streetman4abaac92016-05-26 15:16:27 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Nitin Gupta61989a82012-01-09 16:51:56 -060034#include <linux/module.h>
35#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070036#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/string.h>
41#include <linux/slab.h>
42#include <asm/tlbflush.h>
43#include <asm/pgtable.h>
44#include <linux/cpumask.h>
45#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060046#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080047#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090048#include <linux/spinlock.h>
49#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080050#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080051#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070052#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070053#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070054#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070055#include <linux/pagemap.h>
56
57#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090058
59/*
60 * This must be power of 2 and greater than of equal to sizeof(link_free).
61 * These two conditions ensure that any 'struct link_free' itself doesn't
62 * span more than 1 page which avoids complex case of mapping 2 pages simply
63 * to restore link_free pointer values.
64 */
65#define ZS_ALIGN 8
66
67/*
68 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
69 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
70 */
71#define ZS_MAX_ZSPAGE_ORDER 2
72#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
73
Minchan Kim2e40e162015-04-15 16:15:23 -070074#define ZS_HANDLE_SIZE (sizeof(unsigned long))
75
Seth Jennings0959c632012-08-08 15:12:17 +090076/*
77 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090078 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090079 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070080 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090081 *
82 * This is made more complicated by various memory models and PAE.
83 */
84
85#ifndef MAX_PHYSMEM_BITS
86#ifdef CONFIG_HIGHMEM64G
87#define MAX_PHYSMEM_BITS 36
88#else /* !CONFIG_HIGHMEM64G */
89/*
90 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
91 * be PAGE_SHIFT
92 */
93#define MAX_PHYSMEM_BITS BITS_PER_LONG
94#endif
95#endif
96#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070097
98/*
99 * Memory for allocating for handle keeps object position by
100 * encoding <page, obj_idx> and the encoded value has a room
101 * in least bit(ie, look at obj_to_location).
102 * We use the bit to synchronize between object access by
103 * user and migration.
104 */
105#define HANDLE_PIN_BIT 0
106
107/*
108 * Head in allocated object should have OBJ_ALLOCATED_TAG
109 * to identify the object was allocated or not.
110 * It's okay to add the status bit in the least bit because
111 * header keeps handle which is 4byte-aligned address so we
112 * have room for two bit at least.
113 */
114#define OBJ_ALLOCATED_TAG 1
115#define OBJ_TAG_BITS 1
116#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900117#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
118
119#define MAX(a, b) ((a) >= (b) ? (a) : (b))
120/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
121#define ZS_MIN_ALLOC_SIZE \
122 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700123/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700124#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900125
126/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700127 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900128 * trader-off here:
129 * - Large number of size classes is potentially wasteful as free page are
130 * spread across these classes
131 * - Small number of size classes causes large internal fragmentation
132 * - Probably its better to use specific size classes (empirically
133 * determined). NOTE: all those class sizes must be set as multiple of
134 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
135 *
136 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
137 * (reason above)
138 */
Minchan Kim37836892016-07-26 15:23:23 -0700139#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900140
Seth Jennings0959c632012-08-08 15:12:17 +0900141enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900142 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700143 ZS_ALMOST_EMPTY,
144 ZS_ALMOST_FULL,
145 ZS_FULL,
146 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900147};
148
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800149enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700150 CLASS_EMPTY,
151 CLASS_ALMOST_EMPTY,
152 CLASS_ALMOST_FULL,
153 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800154 OBJ_ALLOCATED,
155 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700156 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800157};
158
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800159struct zs_size_stat {
160 unsigned long objs[NR_ZS_STAT_TYPE];
161};
162
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700163#ifdef CONFIG_ZSMALLOC_STAT
164static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800165#endif
166
Minchan Kim48b48002016-07-26 15:23:31 -0700167#ifdef CONFIG_COMPACTION
168static struct vfsmount *zsmalloc_mnt;
169#endif
170
Seth Jennings0959c632012-08-08 15:12:17 +0900171/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800172 * number of size_classes
173 */
174static int zs_size_classes;
175
176/*
Seth Jennings0959c632012-08-08 15:12:17 +0900177 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
178 * n <= N / f, where
179 * n = number of allocated objects
180 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700181 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900182 *
183 * Similarly, we assign zspage to:
184 * ZS_ALMOST_FULL when n > N / f
185 * ZS_EMPTY when n == 0
186 * ZS_FULL when n == N
187 *
188 * (see: fix_fullness_group())
189 */
190static const int fullness_threshold_frac = 4;
191
192struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700193 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700194 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900195 /*
196 * Size of objects stored in this class. Must be multiple
197 * of ZS_ALIGN.
198 */
199 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700200 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800201 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
202 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700203
204 unsigned int index;
205 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900206};
207
Minchan Kim48b48002016-07-26 15:23:31 -0700208/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
209static void SetPageHugeObject(struct page *page)
210{
211 SetPageOwnerPriv1(page);
212}
213
214static void ClearPageHugeObject(struct page *page)
215{
216 ClearPageOwnerPriv1(page);
217}
218
219static int PageHugeObject(struct page *page)
220{
221 return PageOwnerPriv1(page);
222}
223
Seth Jennings0959c632012-08-08 15:12:17 +0900224/*
225 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700226 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900227 *
228 * This must be power of 2 and less than or equal to ZS_ALIGN
229 */
230struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700231 union {
232 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700233 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700234 * It's valid for non-allocated object
235 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700236 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700237 /*
238 * Handle of allocated object.
239 */
240 unsigned long handle;
241 };
Seth Jennings0959c632012-08-08 15:12:17 +0900242};
243
244struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800245 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800246
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800247 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700248 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700249 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900250
Minchan Kim13de8932014-10-09 15:29:48 -0700251 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800252
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700253 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700254
255 /* Compact classes */
256 struct shrinker shrinker;
257 /*
258 * To signify that register_shrinker() was successful
259 * and unregister_shrinker() will not Oops.
260 */
261 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800262#ifdef CONFIG_ZSMALLOC_STAT
263 struct dentry *stat_dentry;
264#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700265#ifdef CONFIG_COMPACTION
266 struct inode *inode;
267 struct work_struct free_work;
268#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900269};
Nitin Gupta61989a82012-01-09 16:51:56 -0600270
271/*
272 * A zspage's class index and fullness group
273 * are encoded in its (first)page->mapping
274 */
Minchan Kim37836892016-07-26 15:23:23 -0700275#define FULLNESS_BITS 2
276#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700277#define ISOLATED_BITS 3
278#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700279
Minchan Kim37836892016-07-26 15:23:23 -0700280struct zspage {
281 struct {
282 unsigned int fullness:FULLNESS_BITS;
Minchan Kimd19f7452017-04-13 14:56:40 -0700283 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700284 unsigned int isolated:ISOLATED_BITS;
285 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700286 };
287 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700288 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700289 struct page *first_page;
290 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700291#ifdef CONFIG_COMPACTION
292 rwlock_t lock;
293#endif
Minchan Kim37836892016-07-26 15:23:23 -0700294};
Nitin Gupta61989a82012-01-09 16:51:56 -0600295
Seth Jenningsf5536462012-07-18 11:55:56 -0500296struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900297#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500298 struct vm_struct *vm; /* vm area for mapping object that span pages */
299#else
300 char *vm_buf; /* copy buffer for objects that span pages */
301#endif
302 char *vm_addr; /* address of kmap_atomic()'ed pages */
303 enum zs_mapmode vm_mm; /* mapping mode */
304};
305
Minchan Kim48b48002016-07-26 15:23:31 -0700306#ifdef CONFIG_COMPACTION
307static int zs_register_migration(struct zs_pool *pool);
308static void zs_unregister_migration(struct zs_pool *pool);
309static void migrate_lock_init(struct zspage *zspage);
310static void migrate_read_lock(struct zspage *zspage);
311static void migrate_read_unlock(struct zspage *zspage);
312static void kick_deferred_free(struct zs_pool *pool);
313static void init_deferred_free(struct zs_pool *pool);
314static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
315#else
316static int zsmalloc_mount(void) { return 0; }
317static void zsmalloc_unmount(void) {}
318static int zs_register_migration(struct zs_pool *pool) { return 0; }
319static void zs_unregister_migration(struct zs_pool *pool) {}
320static void migrate_lock_init(struct zspage *zspage) {}
321static void migrate_read_lock(struct zspage *zspage) {}
322static void migrate_read_unlock(struct zspage *zspage) {}
323static void kick_deferred_free(struct zs_pool *pool) {}
324static void init_deferred_free(struct zs_pool *pool) {}
325static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
326#endif
327
Minchan Kim37836892016-07-26 15:23:23 -0700328static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700329{
330 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
331 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700332 if (!pool->handle_cachep)
333 return 1;
334
335 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
336 0, 0, NULL);
337 if (!pool->zspage_cachep) {
338 kmem_cache_destroy(pool->handle_cachep);
339 pool->handle_cachep = NULL;
340 return 1;
341 }
342
343 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700344}
345
Minchan Kim37836892016-07-26 15:23:23 -0700346static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700347{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700348 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700349 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700350}
351
Minchan Kim37836892016-07-26 15:23:23 -0700352static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700353{
354 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700355 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700356}
357
Minchan Kim37836892016-07-26 15:23:23 -0700358static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700359{
360 kmem_cache_free(pool->handle_cachep, (void *)handle);
361}
362
Minchan Kim37836892016-07-26 15:23:23 -0700363static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
364{
Minchan Kim48b48002016-07-26 15:23:31 -0700365 return kmem_cache_alloc(pool->zspage_cachep,
366 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim37836892016-07-26 15:23:23 -0700367};
368
369static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
370{
371 kmem_cache_free(pool->zspage_cachep, zspage);
372}
373
Minchan Kim2e40e162015-04-15 16:15:23 -0700374static void record_obj(unsigned long handle, unsigned long obj)
375{
Junil Leec102f072016-01-20 14:58:18 -0800376 /*
377 * lsb of @obj represents handle lock while other bits
378 * represent object value the handle is pointing so
379 * updating shouldn't do store tearing.
380 */
381 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700382}
383
Dan Streetmanc7957792014-08-06 16:08:38 -0700384/* zpool driver */
385
386#ifdef CONFIG_ZPOOL
387
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800388static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700389 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700390 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700391{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700392 /*
393 * Ignore global gfp flags: zs_malloc() may be invoked from
394 * different contexts and its caller must provide a valid
395 * gfp mask.
396 */
397 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700398}
399
400static void zs_zpool_destroy(void *pool)
401{
402 zs_destroy_pool(pool);
403}
404
405static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
406 unsigned long *handle)
407{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700408 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700409 return *handle ? 0 : -1;
410}
411static void zs_zpool_free(void *pool, unsigned long handle)
412{
413 zs_free(pool, handle);
414}
415
416static int zs_zpool_shrink(void *pool, unsigned int pages,
417 unsigned int *reclaimed)
418{
419 return -EINVAL;
420}
421
422static void *zs_zpool_map(void *pool, unsigned long handle,
423 enum zpool_mapmode mm)
424{
425 enum zs_mapmode zs_mm;
426
427 switch (mm) {
428 case ZPOOL_MM_RO:
429 zs_mm = ZS_MM_RO;
430 break;
431 case ZPOOL_MM_WO:
432 zs_mm = ZS_MM_WO;
433 break;
434 case ZPOOL_MM_RW: /* fallthru */
435 default:
436 zs_mm = ZS_MM_RW;
437 break;
438 }
439
440 return zs_map_object(pool, handle, zs_mm);
441}
442static void zs_zpool_unmap(void *pool, unsigned long handle)
443{
444 zs_unmap_object(pool, handle);
445}
446
447static u64 zs_zpool_total_size(void *pool)
448{
Minchan Kim722cdc12014-10-09 15:29:50 -0700449 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700450}
451
452static struct zpool_driver zs_zpool_driver = {
453 .type = "zsmalloc",
454 .owner = THIS_MODULE,
455 .create = zs_zpool_create,
456 .destroy = zs_zpool_destroy,
457 .malloc = zs_zpool_malloc,
458 .free = zs_zpool_free,
459 .shrink = zs_zpool_shrink,
460 .map = zs_zpool_map,
461 .unmap = zs_zpool_unmap,
462 .total_size = zs_zpool_total_size,
463};
464
Kees Cook137f8cf2014-08-29 15:18:40 -0700465MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700466#endif /* CONFIG_ZPOOL */
467
Nitin Gupta61989a82012-01-09 16:51:56 -0600468/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
469static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
470
Minchan Kim48b48002016-07-26 15:23:31 -0700471static bool is_zspage_isolated(struct zspage *zspage)
472{
473 return zspage->isolated;
474}
475
Nick Desaulniers5ac69182017-07-10 15:47:26 -0700476static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600477{
Minchan Kima27545bf2012-04-25 15:23:09 +0900478 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600479}
480
Minchan Kim48b48002016-07-26 15:23:31 -0700481/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700482static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600483{
Minchan Kim37836892016-07-26 15:23:23 -0700484 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600485}
486
Minchan Kim37836892016-07-26 15:23:23 -0700487static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700488{
Minchan Kim37836892016-07-26 15:23:23 -0700489 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700490}
491
Minchan Kim37836892016-07-26 15:23:23 -0700492static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700493{
Minchan Kim37836892016-07-26 15:23:23 -0700494 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700495}
496
Minchan Kim48b48002016-07-26 15:23:31 -0700497static inline struct page *get_first_page(struct zspage *zspage)
498{
499 struct page *first_page = zspage->first_page;
500
501 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
502 return first_page;
503}
504
Minchan Kim4f420472016-07-26 15:23:17 -0700505static inline int get_first_obj_offset(struct page *page)
506{
Minchan Kim48b48002016-07-26 15:23:31 -0700507 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700508}
509
510static inline void set_first_obj_offset(struct page *page, int offset)
511{
Minchan Kim48b48002016-07-26 15:23:31 -0700512 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700513}
514
Minchan Kimbfd093f2016-07-26 15:23:28 -0700515static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700516{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700517 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700518}
519
Minchan Kimbfd093f2016-07-26 15:23:28 -0700520static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700521{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700522 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700523}
524
Minchan Kim37836892016-07-26 15:23:23 -0700525static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700526 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600527 enum fullness_group *fullness)
528{
Minchan Kim48b48002016-07-26 15:23:31 -0700529 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
530
Minchan Kim37836892016-07-26 15:23:23 -0700531 *fullness = zspage->fullness;
532 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600533}
534
Minchan Kim37836892016-07-26 15:23:23 -0700535static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700536 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600537 enum fullness_group fullness)
538{
Minchan Kim37836892016-07-26 15:23:23 -0700539 zspage->class = class_idx;
540 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600541}
542
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900543/*
544 * zsmalloc divides the pool into various size classes where each
545 * class maintains a list of zspages where each zspage is divided
546 * into equal sized chunks. Each allocation falls into one of these
547 * classes depending on its size. This function returns index of the
548 * size class which has chunk size big enough to hold the give size.
549 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600550static int get_size_class_index(int size)
551{
552 int idx = 0;
553
554 if (likely(size > ZS_MIN_ALLOC_SIZE))
555 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
556 ZS_SIZE_CLASS_DELTA);
557
Minchan Kim7b60a682015-04-15 16:15:39 -0700558 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600559}
560
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700561/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700562static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700563 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700564{
Minchan Kim48b48002016-07-26 15:23:31 -0700565 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700566}
567
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700568/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700569static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700570 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700571{
Minchan Kim48b48002016-07-26 15:23:31 -0700572 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700573}
574
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700575/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700576static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700577 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700578{
Minchan Kim48b48002016-07-26 15:23:31 -0700579 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700580}
581
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700582#ifdef CONFIG_ZSMALLOC_STAT
583
Dan Streetman4abaac92016-05-26 15:16:27 -0700584static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700585{
Dan Streetman4abaac92016-05-26 15:16:27 -0700586 if (!debugfs_initialized()) {
587 pr_warn("debugfs not available, stat dir not created\n");
588 return;
589 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700590
591 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
592 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700593 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700594}
595
596static void __exit zs_stat_exit(void)
597{
598 debugfs_remove_recursive(zs_stat_root);
599}
600
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700601static unsigned long zs_can_compact(struct size_class *class);
602
Minchan Kim248ca1b2015-04-15 16:15:42 -0700603static int zs_stats_size_show(struct seq_file *s, void *v)
604{
605 int i;
606 struct zs_pool *pool = s->private;
607 struct size_class *class;
608 int objs_per_zspage;
609 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700610 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700611 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
612 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700613 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700614
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700615 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700616 "class", "size", "almost_full", "almost_empty",
617 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700618 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619
620 for (i = 0; i < zs_size_classes; i++) {
621 class = pool->size_class[i];
622
623 if (class->index != i)
624 continue;
625
626 spin_lock(&class->lock);
627 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
628 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
629 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
630 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700631 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700632 spin_unlock(&class->lock);
633
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700634 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700635 pages_used = obj_allocated / objs_per_zspage *
636 class->pages_per_zspage;
637
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700638 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
639 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700640 i, class->size, class_almost_full, class_almost_empty,
641 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700642 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700643
644 total_class_almost_full += class_almost_full;
645 total_class_almost_empty += class_almost_empty;
646 total_objs += obj_allocated;
647 total_used_objs += obj_used;
648 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700649 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700650 }
651
652 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700653 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700654 "Total", "", total_class_almost_full,
655 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700656 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700657
658 return 0;
659}
660
661static int zs_stats_size_open(struct inode *inode, struct file *file)
662{
663 return single_open(file, zs_stats_size_show, inode->i_private);
664}
665
666static const struct file_operations zs_stat_size_ops = {
667 .open = zs_stats_size_open,
668 .read = seq_read,
669 .llseek = seq_lseek,
670 .release = single_release,
671};
672
Dan Streetmand34f6152016-05-20 16:59:56 -0700673static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700674{
675 struct dentry *entry;
676
Dan Streetman4abaac92016-05-26 15:16:27 -0700677 if (!zs_stat_root) {
678 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700679 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700680 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700681
682 entry = debugfs_create_dir(name, zs_stat_root);
683 if (!entry) {
684 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700685 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700686 }
687 pool->stat_dentry = entry;
688
689 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
690 pool->stat_dentry, pool, &zs_stat_size_ops);
691 if (!entry) {
692 pr_warn("%s: debugfs file entry <%s> creation failed\n",
693 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700694 debugfs_remove_recursive(pool->stat_dentry);
695 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700696 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700697}
698
699static void zs_pool_stat_destroy(struct zs_pool *pool)
700{
701 debugfs_remove_recursive(pool->stat_dentry);
702}
703
704#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700705static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700706{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700707}
708
709static void __exit zs_stat_exit(void)
710{
711}
712
Dan Streetmand34f6152016-05-20 16:59:56 -0700713static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700714{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700715}
716
717static inline void zs_pool_stat_destroy(struct zs_pool *pool)
718{
719}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700720#endif
721
Minchan Kim48b48002016-07-26 15:23:31 -0700722
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900723/*
724 * For each size class, zspages are divided into different groups
725 * depending on how "full" they are. This was done so that we could
726 * easily find empty or nearly empty zspages when we try to shrink
727 * the pool (not yet implemented). This function returns fullness
728 * status of the given page.
729 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700730static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700731 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600732{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700733 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600734 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700735
Minchan Kim37836892016-07-26 15:23:23 -0700736 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700737 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600738
739 if (inuse == 0)
740 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700741 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600742 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700743 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600744 fg = ZS_ALMOST_EMPTY;
745 else
746 fg = ZS_ALMOST_FULL;
747
748 return fg;
749}
750
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900751/*
752 * Each size class maintains various freelists and zspages are assigned
753 * to one of these freelists based on the number of live objects they
754 * have. This functions inserts the given zspage into the freelist
755 * identified by <class, fullness_group>.
756 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700757static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700758 struct zspage *zspage,
759 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600760{
Minchan Kim37836892016-07-26 15:23:23 -0700761 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600762
Minchan Kim48b48002016-07-26 15:23:31 -0700763 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700764 head = list_first_entry_or_null(&class->fullness_list[fullness],
765 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700766 /*
Minchan Kim37836892016-07-26 15:23:23 -0700767 * We want to see more ZS_FULL pages and less almost empty/full.
768 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700769 */
Minchan Kim37836892016-07-26 15:23:23 -0700770 if (head) {
771 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
772 list_add(&zspage->list, &head->list);
773 return;
774 }
775 }
776 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600777}
778
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900779/*
780 * This function removes the given zspage from the freelist identified
781 * by <class, fullness_group>.
782 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700783static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700784 struct zspage *zspage,
785 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600786{
Minchan Kim37836892016-07-26 15:23:23 -0700787 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700788 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600789
Minchan Kim37836892016-07-26 15:23:23 -0700790 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700791 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600792}
793
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900794/*
795 * Each size class maintains zspages in different fullness groups depending
796 * on the number of live objects they contain. When allocating or freeing
797 * objects, the fullness status of the page can change, say, from ALMOST_FULL
798 * to ALMOST_EMPTY when freeing an object. This function checks if such
799 * a status change has occurred for the given page and accordingly moves the
800 * page from the freelist of the old fullness group to that of the new
801 * fullness group.
802 */
Minchan Kimc7806262015-04-15 16:15:26 -0700803static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700804 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600805{
806 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600807 enum fullness_group currfg, newfg;
808
Minchan Kim37836892016-07-26 15:23:23 -0700809 get_zspage_mapping(zspage, &class_idx, &currfg);
810 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600811 if (newfg == currfg)
812 goto out;
813
Minchan Kim48b48002016-07-26 15:23:31 -0700814 if (!is_zspage_isolated(zspage)) {
815 remove_zspage(class, zspage, currfg);
816 insert_zspage(class, zspage, newfg);
817 }
818
Minchan Kim37836892016-07-26 15:23:23 -0700819 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600820
821out:
822 return newfg;
823}
824
825/*
826 * We have to decide on how many pages to link together
827 * to form a zspage for each size class. This is important
828 * to reduce wastage due to unusable space left at end of
829 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700830 * wastage = Zp % class_size
831 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600832 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
833 *
834 * For example, for size class of 3/8 * PAGE_SIZE, we should
835 * link together 3 PAGE_SIZE sized pages to form a zspage
836 * since then we can perfectly fit in 8 such objects.
837 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900838static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600839{
840 int i, max_usedpc = 0;
841 /* zspage order which gives maximum used size per KB */
842 int max_usedpc_order = 1;
843
Seth Jennings84d4faa2012-03-05 11:33:21 -0600844 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600845 int zspage_size;
846 int waste, usedpc;
847
848 zspage_size = i * PAGE_SIZE;
849 waste = zspage_size % class_size;
850 usedpc = (zspage_size - waste) * 100 / zspage_size;
851
852 if (usedpc > max_usedpc) {
853 max_usedpc = usedpc;
854 max_usedpc_order = i;
855 }
856 }
857
858 return max_usedpc_order;
859}
860
Minchan Kim37836892016-07-26 15:23:23 -0700861static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600862{
Minchan Kim48b48002016-07-26 15:23:31 -0700863 struct zspage *zspage = (struct zspage *)page->private;
864
865 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
866 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600867}
868
869static struct page *get_next_page(struct page *page)
870{
Minchan Kim48b48002016-07-26 15:23:31 -0700871 if (unlikely(PageHugeObject(page)))
872 return NULL;
873
874 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600875}
876
Minchan Kimbfd093f2016-07-26 15:23:28 -0700877/**
878 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
879 * @page: page object resides in zspage
880 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800881 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700882static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700883 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600884{
Minchan Kim312fcae2015-04-15 16:15:30 -0700885 obj >>= OBJ_TAG_BITS;
886 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
887 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600888}
889
Minchan Kimbfd093f2016-07-26 15:23:28 -0700890/**
891 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
892 * @page: page object resides in zspage
893 * @obj_idx: object index
894 */
895static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
896{
897 unsigned long obj;
898
899 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
900 obj |= obj_idx & OBJ_INDEX_MASK;
901 obj <<= OBJ_TAG_BITS;
902
903 return obj;
904}
905
Minchan Kim2e40e162015-04-15 16:15:23 -0700906static unsigned long handle_to_obj(unsigned long handle)
907{
908 return *(unsigned long *)handle;
909}
910
Minchan Kim48b48002016-07-26 15:23:31 -0700911static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700912{
Minchan Kim48b48002016-07-26 15:23:31 -0700913 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700914 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700915 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700916 } else
917 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700918}
919
Minchan Kim48b48002016-07-26 15:23:31 -0700920static inline int testpin_tag(unsigned long handle)
921{
922 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
923}
924
Minchan Kim312fcae2015-04-15 16:15:30 -0700925static inline int trypin_tag(unsigned long handle)
926{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700927 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700928}
929
930static void pin_tag(unsigned long handle)
931{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700932 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700933}
934
935static void unpin_tag(unsigned long handle)
936{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700937 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700938}
939
Nitin Guptaf4477e92012-04-02 09:13:56 -0500940static void reset_page(struct page *page)
941{
Minchan Kim48b48002016-07-26 15:23:31 -0700942 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700943 ClearPagePrivate(page);
944 ClearPagePrivate2(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500945 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700946 page_mapcount_reset(page);
947 ClearPageHugeObject(page);
948 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500949}
950
Minchan Kim48b48002016-07-26 15:23:31 -0700951/*
952 * To prevent zspage destroy during migration, zspage freeing should
953 * hold locks of all pages in the zspage.
954 */
955void lock_zspage(struct zspage *zspage)
956{
957 struct page *page = get_first_page(zspage);
958
959 do {
960 lock_page(page);
961 } while ((page = get_next_page(page)) != NULL);
962}
963
964int trylock_zspage(struct zspage *zspage)
965{
966 struct page *cursor, *fail;
967
968 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
969 get_next_page(cursor)) {
970 if (!trylock_page(cursor)) {
971 fail = cursor;
972 goto unlock;
973 }
974 }
975
976 return 1;
977unlock:
978 for (cursor = get_first_page(zspage); cursor != fail; cursor =
979 get_next_page(cursor))
980 unlock_page(cursor);
981
982 return 0;
983}
984
985static void __free_zspage(struct zs_pool *pool, struct size_class *class,
986 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600987{
Minchan Kim37836892016-07-26 15:23:23 -0700988 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700989 enum fullness_group fg;
990 unsigned int class_idx;
991
992 get_zspage_mapping(zspage, &class_idx, &fg);
993
994 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600995
Minchan Kim37836892016-07-26 15:23:23 -0700996 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700997 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600998
Minchan Kim48b48002016-07-26 15:23:31 -0700999 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001000 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001001 VM_BUG_ON_PAGE(!PageLocked(page), page);
1002 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001003 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001004 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001005 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001006 put_page(page);
1007 page = next;
1008 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001009
Minchan Kim37836892016-07-26 15:23:23 -07001010 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001011
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001012 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001013 atomic_long_sub(class->pages_per_zspage,
1014 &pool->pages_allocated);
1015}
1016
1017static void free_zspage(struct zs_pool *pool, struct size_class *class,
1018 struct zspage *zspage)
1019{
1020 VM_BUG_ON(get_zspage_inuse(zspage));
1021 VM_BUG_ON(list_empty(&zspage->list));
1022
1023 if (!trylock_zspage(zspage)) {
1024 kick_deferred_free(pool);
1025 return;
1026 }
1027
1028 remove_zspage(class, zspage, ZS_EMPTY);
1029 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001030}
1031
1032/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001033static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001034{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001035 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001036 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001037 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001038
Nitin Gupta61989a82012-01-09 16:51:56 -06001039 while (page) {
1040 struct page *next_page;
1041 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001042 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001043
Minchan Kim37836892016-07-26 15:23:23 -07001044 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001045
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001046 vaddr = kmap_atomic(page);
1047 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001048
Dan Streetman5538c562014-10-09 15:30:01 -07001049 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001050 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001051 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001052 }
1053
1054 /*
1055 * We now come to the last (full or partial) object on this
1056 * page, which must point to the first object on the next
1057 * page (if present)
1058 */
1059 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001060 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001061 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001062 } else {
1063 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001064 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001065 * whether it's allocated object or not.
1066 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001067 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001068 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001069 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001070 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001071 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001072 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001073
Minchan Kimbfd093f2016-07-26 15:23:28 -07001074 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001075}
1076
Minchan Kim48b48002016-07-26 15:23:31 -07001077static void create_page_chain(struct size_class *class, struct zspage *zspage,
1078 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001079{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001080 int i;
1081 struct page *page;
1082 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001083 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001084
1085 /*
1086 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001087 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001088 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001089 *
Minchan Kim37836892016-07-26 15:23:23 -07001090 * we set PG_private to identify the first page (i.e. no other sub-page
1091 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001092 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001093 for (i = 0; i < nr_pages; i++) {
1094 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001095 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001096 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001097 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001098 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001099 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001100 if (unlikely(class->objs_per_zspage == 1 &&
1101 class->pages_per_zspage == 1))
1102 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001103 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001104 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001105 }
Minchan Kim48b48002016-07-26 15:23:31 -07001106 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001107 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001108 prev_page = page;
1109 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001110}
Nitin Gupta61989a82012-01-09 16:51:56 -06001111
Minchan Kimbdb0af72016-07-26 15:23:20 -07001112/*
1113 * Allocate a zspage for the given size class
1114 */
Minchan Kim37836892016-07-26 15:23:23 -07001115static struct zspage *alloc_zspage(struct zs_pool *pool,
1116 struct size_class *class,
1117 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001118{
1119 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001120 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001121 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1122
1123 if (!zspage)
1124 return NULL;
1125
1126 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001127 zspage->magic = ZSPAGE_MAGIC;
1128 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001129
Minchan Kimbdb0af72016-07-26 15:23:20 -07001130 for (i = 0; i < class->pages_per_zspage; i++) {
1131 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001132
Minchan Kim37836892016-07-26 15:23:23 -07001133 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001134 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001135 while (--i >= 0) {
1136 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001137 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001138 }
Minchan Kim37836892016-07-26 15:23:23 -07001139 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001140 return NULL;
1141 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001142
1143 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001144 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001145 }
1146
Minchan Kim48b48002016-07-26 15:23:31 -07001147 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001148 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001149
Minchan Kim37836892016-07-26 15:23:23 -07001150 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001151}
1152
Minchan Kim37836892016-07-26 15:23:23 -07001153static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001154{
1155 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001156 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001157
Minchan Kim48b48002016-07-26 15:23:31 -07001158 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001159 zspage = list_first_entry_or_null(&class->fullness_list[i],
1160 struct zspage, list);
1161 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001162 break;
1163 }
1164
Minchan Kim37836892016-07-26 15:23:23 -07001165 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001166}
1167
Minchan Kim1b945ae2013-12-11 11:04:36 +09001168#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001169static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001170{
Seth Jenningsf5536462012-07-18 11:55:56 -05001171 /*
1172 * Make sure we don't leak memory if a cpu UP notification
1173 * and zs_init() race and both call zs_cpu_up() on the same cpu
1174 */
1175 if (area->vm)
1176 return 0;
1177 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1178 if (!area->vm)
1179 return -ENOMEM;
1180 return 0;
1181}
1182
1183static inline void __zs_cpu_down(struct mapping_area *area)
1184{
1185 if (area->vm)
1186 free_vm_area(area->vm);
1187 area->vm = NULL;
1188}
1189
1190static inline void *__zs_map_object(struct mapping_area *area,
1191 struct page *pages[2], int off, int size)
1192{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001193 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001194 area->vm_addr = area->vm->addr;
1195 return area->vm_addr + off;
1196}
1197
1198static inline void __zs_unmap_object(struct mapping_area *area,
1199 struct page *pages[2], int off, int size)
1200{
1201 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001202
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001203 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001204}
1205
Minchan Kim1b945ae2013-12-11 11:04:36 +09001206#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001207
1208static inline int __zs_cpu_up(struct mapping_area *area)
1209{
1210 /*
1211 * Make sure we don't leak memory if a cpu UP notification
1212 * and zs_init() race and both call zs_cpu_up() on the same cpu
1213 */
1214 if (area->vm_buf)
1215 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001216 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001217 if (!area->vm_buf)
1218 return -ENOMEM;
1219 return 0;
1220}
1221
1222static inline void __zs_cpu_down(struct mapping_area *area)
1223{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001224 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001225 area->vm_buf = NULL;
1226}
1227
1228static void *__zs_map_object(struct mapping_area *area,
1229 struct page *pages[2], int off, int size)
1230{
Seth Jennings5f601902012-07-02 16:15:49 -05001231 int sizes[2];
1232 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001233 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001234
Seth Jenningsf5536462012-07-18 11:55:56 -05001235 /* disable page faults to match kmap_atomic() return conditions */
1236 pagefault_disable();
1237
1238 /* no read fastpath */
1239 if (area->vm_mm == ZS_MM_WO)
1240 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001241
1242 sizes[0] = PAGE_SIZE - off;
1243 sizes[1] = size - sizes[0];
1244
Seth Jennings5f601902012-07-02 16:15:49 -05001245 /* copy object to per-cpu buffer */
1246 addr = kmap_atomic(pages[0]);
1247 memcpy(buf, addr + off, sizes[0]);
1248 kunmap_atomic(addr);
1249 addr = kmap_atomic(pages[1]);
1250 memcpy(buf + sizes[0], addr, sizes[1]);
1251 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001252out:
1253 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001254}
1255
Seth Jenningsf5536462012-07-18 11:55:56 -05001256static void __zs_unmap_object(struct mapping_area *area,
1257 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001258{
Seth Jennings5f601902012-07-02 16:15:49 -05001259 int sizes[2];
1260 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001261 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001262
Seth Jenningsf5536462012-07-18 11:55:56 -05001263 /* no write fastpath */
1264 if (area->vm_mm == ZS_MM_RO)
1265 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001266
Minchan Kim7b60a682015-04-15 16:15:39 -07001267 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001268 buf = buf + ZS_HANDLE_SIZE;
1269 size -= ZS_HANDLE_SIZE;
1270 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001271
Seth Jennings5f601902012-07-02 16:15:49 -05001272 sizes[0] = PAGE_SIZE - off;
1273 sizes[1] = size - sizes[0];
1274
1275 /* copy per-cpu buffer to object */
1276 addr = kmap_atomic(pages[0]);
1277 memcpy(addr + off, buf, sizes[0]);
1278 kunmap_atomic(addr);
1279 addr = kmap_atomic(pages[1]);
1280 memcpy(addr, buf + sizes[0], sizes[1]);
1281 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001282
1283out:
1284 /* enable page faults to match kunmap_atomic() return conditions */
1285 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001286}
Nitin Gupta61989a82012-01-09 16:51:56 -06001287
Minchan Kim1b945ae2013-12-11 11:04:36 +09001288#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001289
Nitin Gupta61989a82012-01-09 16:51:56 -06001290static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1291 void *pcpu)
1292{
Seth Jenningsf5536462012-07-18 11:55:56 -05001293 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001294 struct mapping_area *area;
1295
1296 switch (action) {
1297 case CPU_UP_PREPARE:
1298 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001299 ret = __zs_cpu_up(area);
1300 if (ret)
1301 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001302 break;
1303 case CPU_DEAD:
1304 case CPU_UP_CANCELED:
1305 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001306 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001307 break;
1308 }
1309
1310 return NOTIFY_OK;
1311}
1312
1313static struct notifier_block zs_cpu_nb = {
1314 .notifier_call = zs_cpu_notifier
1315};
1316
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001317static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001318{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001319 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001320
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301321 cpu_notifier_register_begin();
1322
1323 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001324 for_each_online_cpu(cpu) {
1325 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001326 if (notifier_to_errno(ret))
1327 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001328 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301329
1330 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001331 return notifier_to_errno(ret);
1332}
1333
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001334static void zs_unregister_cpu_notifier(void)
1335{
1336 int cpu;
1337
1338 cpu_notifier_register_begin();
1339
1340 for_each_online_cpu(cpu)
1341 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1342 __unregister_cpu_notifier(&zs_cpu_nb);
1343
1344 cpu_notifier_register_done();
1345}
1346
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001347static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001348{
1349 int nr;
1350
1351 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1352 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1353 nr += 1;
1354
1355 zs_size_classes = nr;
1356}
1357
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001358static bool can_merge(struct size_class *prev, int pages_per_zspage,
1359 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001360{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001361 if (prev->pages_per_zspage == pages_per_zspage &&
1362 prev->objs_per_zspage == objs_per_zspage)
1363 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001364
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001365 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001366}
1367
Minchan Kim37836892016-07-26 15:23:23 -07001368static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001369{
Minchan Kim37836892016-07-26 15:23:23 -07001370 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001371}
1372
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001373unsigned long zs_get_total_pages(struct zs_pool *pool)
1374{
1375 return atomic_long_read(&pool->pages_allocated);
1376}
1377EXPORT_SYMBOL_GPL(zs_get_total_pages);
1378
1379/**
1380 * zs_map_object - get address of allocated object from handle.
1381 * @pool: pool from which the object was allocated
1382 * @handle: handle returned from zs_malloc
1383 *
1384 * Before using an object allocated from zs_malloc, it must be mapped using
1385 * this function. When done with the object, it must be unmapped using
1386 * zs_unmap_object.
1387 *
1388 * Only one object can be mapped per cpu at a time. There is no protection
1389 * against nested mappings.
1390 *
1391 * This function returns with preemption and page faults disabled.
1392 */
1393void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1394 enum zs_mapmode mm)
1395{
Minchan Kim37836892016-07-26 15:23:23 -07001396 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001397 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001398 unsigned long obj, off;
1399 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001400
1401 unsigned int class_idx;
1402 enum fullness_group fg;
1403 struct size_class *class;
1404 struct mapping_area *area;
1405 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001406 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001407
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001408 /*
1409 * Because we use per-cpu mapping areas shared among the
1410 * pools/users, we can't allow mapping in interrupt context
1411 * because it can corrupt another users mappings.
1412 */
Sergey Senozhatsky16184002017-11-15 17:34:03 -08001413 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001414
Minchan Kim312fcae2015-04-15 16:15:30 -07001415 /* From now on, migration cannot move the object */
1416 pin_tag(handle);
1417
Minchan Kim2e40e162015-04-15 16:15:23 -07001418 obj = handle_to_obj(handle);
1419 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001420 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001421
1422 /* migration cannot move any subpage in this zspage */
1423 migrate_read_lock(zspage);
1424
Minchan Kim37836892016-07-26 15:23:23 -07001425 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001426 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001427 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001428
1429 area = &get_cpu_var(zs_map_area);
1430 area->vm_mm = mm;
1431 if (off + class->size <= PAGE_SIZE) {
1432 /* this object is contained entirely within a page */
1433 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001434 ret = area->vm_addr + off;
1435 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001436 }
1437
1438 /* this object spans two pages */
1439 pages[0] = page;
1440 pages[1] = get_next_page(page);
1441 BUG_ON(!pages[1]);
1442
Minchan Kim2e40e162015-04-15 16:15:23 -07001443 ret = __zs_map_object(area, pages, off, class->size);
1444out:
Minchan Kim48b48002016-07-26 15:23:31 -07001445 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001446 ret += ZS_HANDLE_SIZE;
1447
1448 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001449}
1450EXPORT_SYMBOL_GPL(zs_map_object);
1451
1452void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1453{
Minchan Kim37836892016-07-26 15:23:23 -07001454 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001455 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001456 unsigned long obj, off;
1457 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001458
1459 unsigned int class_idx;
1460 enum fullness_group fg;
1461 struct size_class *class;
1462 struct mapping_area *area;
1463
Minchan Kim2e40e162015-04-15 16:15:23 -07001464 obj = handle_to_obj(handle);
1465 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001466 zspage = get_zspage(page);
1467 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001468 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001469 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001470
1471 area = this_cpu_ptr(&zs_map_area);
1472 if (off + class->size <= PAGE_SIZE)
1473 kunmap_atomic(area->vm_addr);
1474 else {
1475 struct page *pages[2];
1476
1477 pages[0] = page;
1478 pages[1] = get_next_page(page);
1479 BUG_ON(!pages[1]);
1480
1481 __zs_unmap_object(area, pages, off, class->size);
1482 }
1483 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001484
1485 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001486 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001487}
1488EXPORT_SYMBOL_GPL(zs_unmap_object);
1489
Minchan Kim251cbb92016-05-20 16:59:42 -07001490static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001491 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001492{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001493 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001494 unsigned long obj;
1495 struct link_free *link;
1496
1497 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001498 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001499 void *vaddr;
1500
Minchan Kim312fcae2015-04-15 16:15:30 -07001501 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001502 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001503
1504 offset = obj * class->size;
1505 nr_page = offset >> PAGE_SHIFT;
1506 m_offset = offset & ~PAGE_MASK;
1507 m_page = get_first_page(zspage);
1508
1509 for (i = 0; i < nr_page; i++)
1510 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001511
1512 vaddr = kmap_atomic(m_page);
1513 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001514 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001515 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001516 /* record handle in the header of allocated chunk */
1517 link->handle = handle;
1518 else
Minchan Kim37836892016-07-26 15:23:23 -07001519 /* record handle to page->index */
1520 zspage->first_page->index = handle;
1521
Minchan Kimc7806262015-04-15 16:15:26 -07001522 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001523 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001524 zs_stat_inc(class, OBJ_USED, 1);
1525
Minchan Kimbfd093f2016-07-26 15:23:28 -07001526 obj = location_to_obj(m_page, obj);
1527
Minchan Kimc7806262015-04-15 16:15:26 -07001528 return obj;
1529}
1530
1531
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001532/**
1533 * zs_malloc - Allocate block of given size from pool.
1534 * @pool: pool to allocate from
1535 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001536 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001537 *
1538 * On success, handle to the allocated object is returned,
1539 * otherwise 0.
1540 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1541 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001542unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001543{
Minchan Kim2e40e162015-04-15 16:15:23 -07001544 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001545 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001546 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001547 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548
Minchan Kim7b60a682015-04-15 16:15:39 -07001549 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001550 return 0;
1551
Minchan Kim37836892016-07-26 15:23:23 -07001552 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001553 if (!handle)
1554 return 0;
1555
1556 /* extra space in chunk to keep the handle */
1557 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001558 class = pool->size_class[get_size_class_index(size)];
1559
1560 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001561 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001562 if (likely(zspage)) {
1563 obj = obj_malloc(class, zspage, handle);
1564 /* Now move the zspage to another fullness group, if required */
1565 fix_fullness_group(class, zspage);
1566 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001567 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001568
Minchan Kim48b48002016-07-26 15:23:31 -07001569 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001570 }
1571
Minchan Kim48b48002016-07-26 15:23:31 -07001572 spin_unlock(&class->lock);
1573
1574 zspage = alloc_zspage(pool, class, gfp);
1575 if (!zspage) {
1576 cache_free_handle(pool, handle);
1577 return 0;
1578 }
1579
1580 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001581 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001582 newfg = get_fullness_group(class, zspage);
1583 insert_zspage(class, zspage, newfg);
1584 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001585 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001586 atomic_long_add(class->pages_per_zspage,
1587 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001588 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001589
1590 /* We completely set up zspage so mark them as movable */
1591 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001592 spin_unlock(&class->lock);
1593
Minchan Kim2e40e162015-04-15 16:15:23 -07001594 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001595}
1596EXPORT_SYMBOL_GPL(zs_malloc);
1597
Minchan Kim1ee47162016-05-20 16:59:45 -07001598static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001599{
1600 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001601 struct zspage *zspage;
1602 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001603 unsigned long f_offset;
1604 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001605 void *vaddr;
1606
Minchan Kim312fcae2015-04-15 16:15:30 -07001607 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001608 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001609 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001610 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001611
Minchan Kimc7806262015-04-15 16:15:26 -07001612 vaddr = kmap_atomic(f_page);
1613
1614 /* Insert this object in containing zspage's freelist */
1615 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001616 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001617 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001618 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001619 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001620 zs_stat_dec(class, OBJ_USED, 1);
1621}
1622
1623void zs_free(struct zs_pool *pool, unsigned long handle)
1624{
Minchan Kim37836892016-07-26 15:23:23 -07001625 struct zspage *zspage;
1626 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001627 unsigned long obj;
1628 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001629 int class_idx;
1630 struct size_class *class;
1631 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001632 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001633
Minchan Kim2e40e162015-04-15 16:15:23 -07001634 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001635 return;
1636
Minchan Kim312fcae2015-04-15 16:15:30 -07001637 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001638 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001639 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001640 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001641
Minchan Kim48b48002016-07-26 15:23:31 -07001642 migrate_read_lock(zspage);
1643
Minchan Kim37836892016-07-26 15:23:23 -07001644 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001645 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001646
1647 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001648 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001649 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001650 if (fullness != ZS_EMPTY) {
1651 migrate_read_unlock(zspage);
1652 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001653 }
Minchan Kim48b48002016-07-26 15:23:31 -07001654
1655 isolated = is_zspage_isolated(zspage);
1656 migrate_read_unlock(zspage);
1657 /* If zspage is isolated, zs_page_putback will free the zspage */
1658 if (likely(!isolated))
1659 free_zspage(pool, class, zspage);
1660out:
1661
Minchan Kim312fcae2015-04-15 16:15:30 -07001662 spin_unlock(&class->lock);
1663 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001664 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001665}
1666EXPORT_SYMBOL_GPL(zs_free);
1667
Minchan Kim251cbb92016-05-20 16:59:42 -07001668static void zs_object_copy(struct size_class *class, unsigned long dst,
1669 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001670{
1671 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001672 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001673 unsigned long s_off, d_off;
1674 void *s_addr, *d_addr;
1675 int s_size, d_size, size;
1676 int written = 0;
1677
1678 s_size = d_size = class->size;
1679
1680 obj_to_location(src, &s_page, &s_objidx);
1681 obj_to_location(dst, &d_page, &d_objidx);
1682
Minchan Kimbfd093f2016-07-26 15:23:28 -07001683 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1684 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001685
1686 if (s_off + class->size > PAGE_SIZE)
1687 s_size = PAGE_SIZE - s_off;
1688
1689 if (d_off + class->size > PAGE_SIZE)
1690 d_size = PAGE_SIZE - d_off;
1691
1692 s_addr = kmap_atomic(s_page);
1693 d_addr = kmap_atomic(d_page);
1694
1695 while (1) {
1696 size = min(s_size, d_size);
1697 memcpy(d_addr + d_off, s_addr + s_off, size);
1698 written += size;
1699
1700 if (written == class->size)
1701 break;
1702
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001703 s_off += size;
1704 s_size -= size;
1705 d_off += size;
1706 d_size -= size;
1707
1708 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001709 kunmap_atomic(d_addr);
1710 kunmap_atomic(s_addr);
1711 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001712 s_addr = kmap_atomic(s_page);
1713 d_addr = kmap_atomic(d_page);
1714 s_size = class->size - written;
1715 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001716 }
1717
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001718 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001719 kunmap_atomic(d_addr);
1720 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001721 d_addr = kmap_atomic(d_page);
1722 d_size = class->size - written;
1723 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001724 }
1725 }
1726
1727 kunmap_atomic(d_addr);
1728 kunmap_atomic(s_addr);
1729}
1730
1731/*
1732 * Find alloced object in zspage from index object and
1733 * return handle.
1734 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001735static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001736 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001737{
1738 unsigned long head;
1739 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001740 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001741 unsigned long handle = 0;
1742 void *addr = kmap_atomic(page);
1743
Minchan Kim37836892016-07-26 15:23:23 -07001744 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001745 offset += class->size * index;
1746
1747 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001748 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001749 if (head & OBJ_ALLOCATED_TAG) {
1750 handle = head & ~OBJ_ALLOCATED_TAG;
1751 if (trypin_tag(handle))
1752 break;
1753 handle = 0;
1754 }
1755
1756 offset += class->size;
1757 index++;
1758 }
1759
1760 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001761
1762 *obj_idx = index;
1763
Minchan Kim312fcae2015-04-15 16:15:30 -07001764 return handle;
1765}
1766
1767struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001768 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001769 struct page *s_page;
1770 /* Destination page for migration which should be a first page
1771 * of zspage. */
1772 struct page *d_page;
1773 /* Starting object index within @s_page which used for live object
1774 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001775 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001776};
1777
1778static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1779 struct zs_compact_control *cc)
1780{
1781 unsigned long used_obj, free_obj;
1782 unsigned long handle;
1783 struct page *s_page = cc->s_page;
1784 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001785 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001786 int ret = 0;
1787
1788 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001789 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001790 if (!handle) {
1791 s_page = get_next_page(s_page);
1792 if (!s_page)
1793 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001794 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001795 continue;
1796 }
1797
1798 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001799 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001800 unpin_tag(handle);
1801 ret = -ENOMEM;
1802 break;
1803 }
1804
1805 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001806 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001807 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001808 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001809 /*
1810 * record_obj updates handle's value to free_obj and it will
1811 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1812 * breaks synchronization using pin_tag(e,g, zs_free) so
1813 * let's keep the lock bit.
1814 */
1815 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001816 record_obj(handle, free_obj);
1817 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001818 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001819 }
1820
1821 /* Remember last position in this iteration */
1822 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001823 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001824
1825 return ret;
1826}
1827
Minchan Kim37836892016-07-26 15:23:23 -07001828static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001829{
1830 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001831 struct zspage *zspage;
1832 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001833
Minchan Kim37836892016-07-26 15:23:23 -07001834 if (!source) {
1835 fg[0] = ZS_ALMOST_FULL;
1836 fg[1] = ZS_ALMOST_EMPTY;
1837 }
1838
1839 for (i = 0; i < 2; i++) {
1840 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1841 struct zspage, list);
1842 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001843 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001844 remove_zspage(class, zspage, fg[i]);
1845 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001846 }
1847 }
1848
Minchan Kim37836892016-07-26 15:23:23 -07001849 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001850}
1851
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001852/*
Minchan Kim37836892016-07-26 15:23:23 -07001853 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001854 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001855 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001856 *
Minchan Kim37836892016-07-26 15:23:23 -07001857 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001858 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001859static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001860 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001861{
Minchan Kim312fcae2015-04-15 16:15:30 -07001862 enum fullness_group fullness;
1863
Minchan Kim48b48002016-07-26 15:23:31 -07001864 VM_BUG_ON(is_zspage_isolated(zspage));
1865
Minchan Kim37836892016-07-26 15:23:23 -07001866 fullness = get_fullness_group(class, zspage);
1867 insert_zspage(class, zspage, fullness);
1868 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001869
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001870 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001871}
1872
Minchan Kim48b48002016-07-26 15:23:31 -07001873#ifdef CONFIG_COMPACTION
1874static struct dentry *zs_mount(struct file_system_type *fs_type,
1875 int flags, const char *dev_name, void *data)
1876{
1877 static const struct dentry_operations ops = {
1878 .d_dname = simple_dname,
1879 };
1880
1881 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1882}
1883
1884static struct file_system_type zsmalloc_fs = {
1885 .name = "zsmalloc",
1886 .mount = zs_mount,
1887 .kill_sb = kill_anon_super,
1888};
1889
1890static int zsmalloc_mount(void)
1891{
1892 int ret = 0;
1893
1894 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1895 if (IS_ERR(zsmalloc_mnt))
1896 ret = PTR_ERR(zsmalloc_mnt);
1897
1898 return ret;
1899}
1900
1901static void zsmalloc_unmount(void)
1902{
1903 kern_unmount(zsmalloc_mnt);
1904}
1905
1906static void migrate_lock_init(struct zspage *zspage)
1907{
1908 rwlock_init(&zspage->lock);
1909}
1910
1911static void migrate_read_lock(struct zspage *zspage)
1912{
1913 read_lock(&zspage->lock);
1914}
1915
1916static void migrate_read_unlock(struct zspage *zspage)
1917{
1918 read_unlock(&zspage->lock);
1919}
1920
1921static void migrate_write_lock(struct zspage *zspage)
1922{
1923 write_lock(&zspage->lock);
1924}
1925
1926static void migrate_write_unlock(struct zspage *zspage)
1927{
1928 write_unlock(&zspage->lock);
1929}
1930
1931/* Number of isolated subpage for *page migration* in this zspage */
1932static void inc_zspage_isolation(struct zspage *zspage)
1933{
1934 zspage->isolated++;
1935}
1936
1937static void dec_zspage_isolation(struct zspage *zspage)
1938{
1939 zspage->isolated--;
1940}
1941
1942static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1943 struct page *newpage, struct page *oldpage)
1944{
1945 struct page *page;
1946 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1947 int idx = 0;
1948
1949 page = get_first_page(zspage);
1950 do {
1951 if (page == oldpage)
1952 pages[idx] = newpage;
1953 else
1954 pages[idx] = page;
1955 idx++;
1956 } while ((page = get_next_page(page)) != NULL);
1957
1958 create_page_chain(class, zspage, pages);
1959 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1960 if (unlikely(PageHugeObject(oldpage)))
1961 newpage->index = oldpage->index;
1962 __SetPageMovable(newpage, page_mapping(oldpage));
1963}
1964
1965bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1966{
1967 struct zs_pool *pool;
1968 struct size_class *class;
1969 int class_idx;
1970 enum fullness_group fullness;
1971 struct zspage *zspage;
1972 struct address_space *mapping;
1973
1974 /*
1975 * Page is locked so zspage couldn't be destroyed. For detail, look at
1976 * lock_zspage in free_zspage.
1977 */
1978 VM_BUG_ON_PAGE(!PageMovable(page), page);
1979 VM_BUG_ON_PAGE(PageIsolated(page), page);
1980
1981 zspage = get_zspage(page);
1982
1983 /*
1984 * Without class lock, fullness could be stale while class_idx is okay
1985 * because class_idx is constant unless page is freed so we should get
1986 * fullness again under class lock.
1987 */
1988 get_zspage_mapping(zspage, &class_idx, &fullness);
1989 mapping = page_mapping(page);
1990 pool = mapping->private_data;
1991 class = pool->size_class[class_idx];
1992
1993 spin_lock(&class->lock);
1994 if (get_zspage_inuse(zspage) == 0) {
1995 spin_unlock(&class->lock);
1996 return false;
1997 }
1998
1999 /* zspage is isolated for object migration */
2000 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2001 spin_unlock(&class->lock);
2002 return false;
2003 }
2004
2005 /*
2006 * If this is first time isolation for the zspage, isolate zspage from
2007 * size_class to prevent further object allocation from the zspage.
2008 */
2009 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2010 get_zspage_mapping(zspage, &class_idx, &fullness);
2011 remove_zspage(class, zspage, fullness);
2012 }
2013
2014 inc_zspage_isolation(zspage);
2015 spin_unlock(&class->lock);
2016
2017 return true;
2018}
2019
2020int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2021 struct page *page, enum migrate_mode mode)
2022{
2023 struct zs_pool *pool;
2024 struct size_class *class;
2025 int class_idx;
2026 enum fullness_group fullness;
2027 struct zspage *zspage;
2028 struct page *dummy;
2029 void *s_addr, *d_addr, *addr;
2030 int offset, pos;
2031 unsigned long handle, head;
2032 unsigned long old_obj, new_obj;
2033 unsigned int obj_idx;
2034 int ret = -EAGAIN;
2035
2036 VM_BUG_ON_PAGE(!PageMovable(page), page);
2037 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2038
2039 zspage = get_zspage(page);
2040
2041 /* Concurrent compactor cannot migrate any subpage in zspage */
2042 migrate_write_lock(zspage);
2043 get_zspage_mapping(zspage, &class_idx, &fullness);
2044 pool = mapping->private_data;
2045 class = pool->size_class[class_idx];
2046 offset = get_first_obj_offset(page);
2047
2048 spin_lock(&class->lock);
2049 if (!get_zspage_inuse(zspage)) {
2050 ret = -EBUSY;
2051 goto unlock_class;
2052 }
2053
2054 pos = offset;
2055 s_addr = kmap_atomic(page);
2056 while (pos < PAGE_SIZE) {
2057 head = obj_to_head(page, s_addr + pos);
2058 if (head & OBJ_ALLOCATED_TAG) {
2059 handle = head & ~OBJ_ALLOCATED_TAG;
2060 if (!trypin_tag(handle))
2061 goto unpin_objects;
2062 }
2063 pos += class->size;
2064 }
2065
2066 /*
2067 * Here, any user cannot access all objects in the zspage so let's move.
2068 */
2069 d_addr = kmap_atomic(newpage);
2070 memcpy(d_addr, s_addr, PAGE_SIZE);
2071 kunmap_atomic(d_addr);
2072
2073 for (addr = s_addr + offset; addr < s_addr + pos;
2074 addr += class->size) {
2075 head = obj_to_head(page, addr);
2076 if (head & OBJ_ALLOCATED_TAG) {
2077 handle = head & ~OBJ_ALLOCATED_TAG;
2078 if (!testpin_tag(handle))
2079 BUG();
2080
2081 old_obj = handle_to_obj(handle);
2082 obj_to_location(old_obj, &dummy, &obj_idx);
2083 new_obj = (unsigned long)location_to_obj(newpage,
2084 obj_idx);
2085 new_obj |= BIT(HANDLE_PIN_BIT);
2086 record_obj(handle, new_obj);
2087 }
2088 }
2089
2090 replace_sub_page(class, zspage, newpage, page);
2091 get_page(newpage);
2092
2093 dec_zspage_isolation(zspage);
2094
2095 /*
2096 * Page migration is done so let's putback isolated zspage to
2097 * the list if @page is final isolated subpage in the zspage.
2098 */
2099 if (!is_zspage_isolated(zspage))
2100 putback_zspage(class, zspage);
2101
2102 reset_page(page);
2103 put_page(page);
2104 page = newpage;
2105
Minchan Kimdd4123f2016-07-26 15:26:50 -07002106 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002107unpin_objects:
2108 for (addr = s_addr + offset; addr < s_addr + pos;
2109 addr += class->size) {
2110 head = obj_to_head(page, addr);
2111 if (head & OBJ_ALLOCATED_TAG) {
2112 handle = head & ~OBJ_ALLOCATED_TAG;
2113 if (!testpin_tag(handle))
2114 BUG();
2115 unpin_tag(handle);
2116 }
2117 }
2118 kunmap_atomic(s_addr);
2119unlock_class:
2120 spin_unlock(&class->lock);
2121 migrate_write_unlock(zspage);
2122
2123 return ret;
2124}
2125
2126void zs_page_putback(struct page *page)
2127{
2128 struct zs_pool *pool;
2129 struct size_class *class;
2130 int class_idx;
2131 enum fullness_group fg;
2132 struct address_space *mapping;
2133 struct zspage *zspage;
2134
2135 VM_BUG_ON_PAGE(!PageMovable(page), page);
2136 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2137
2138 zspage = get_zspage(page);
2139 get_zspage_mapping(zspage, &class_idx, &fg);
2140 mapping = page_mapping(page);
2141 pool = mapping->private_data;
2142 class = pool->size_class[class_idx];
2143
2144 spin_lock(&class->lock);
2145 dec_zspage_isolation(zspage);
2146 if (!is_zspage_isolated(zspage)) {
2147 fg = putback_zspage(class, zspage);
2148 /*
2149 * Due to page_lock, we cannot free zspage immediately
2150 * so let's defer.
2151 */
2152 if (fg == ZS_EMPTY)
2153 schedule_work(&pool->free_work);
2154 }
2155 spin_unlock(&class->lock);
2156}
2157
2158const struct address_space_operations zsmalloc_aops = {
2159 .isolate_page = zs_page_isolate,
2160 .migratepage = zs_page_migrate,
2161 .putback_page = zs_page_putback,
2162};
2163
2164static int zs_register_migration(struct zs_pool *pool)
2165{
2166 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2167 if (IS_ERR(pool->inode)) {
2168 pool->inode = NULL;
2169 return 1;
2170 }
2171
2172 pool->inode->i_mapping->private_data = pool;
2173 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2174 return 0;
2175}
2176
2177static void zs_unregister_migration(struct zs_pool *pool)
2178{
2179 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002180 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002181}
2182
2183/*
2184 * Caller should hold page_lock of all pages in the zspage
2185 * In here, we cannot use zspage meta data.
2186 */
2187static void async_free_zspage(struct work_struct *work)
2188{
2189 int i;
2190 struct size_class *class;
2191 unsigned int class_idx;
2192 enum fullness_group fullness;
2193 struct zspage *zspage, *tmp;
2194 LIST_HEAD(free_pages);
2195 struct zs_pool *pool = container_of(work, struct zs_pool,
2196 free_work);
2197
2198 for (i = 0; i < zs_size_classes; i++) {
2199 class = pool->size_class[i];
2200 if (class->index != i)
2201 continue;
2202
2203 spin_lock(&class->lock);
2204 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2205 spin_unlock(&class->lock);
2206 }
2207
2208
2209 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2210 list_del(&zspage->list);
2211 lock_zspage(zspage);
2212
2213 get_zspage_mapping(zspage, &class_idx, &fullness);
2214 VM_BUG_ON(fullness != ZS_EMPTY);
2215 class = pool->size_class[class_idx];
2216 spin_lock(&class->lock);
2217 __free_zspage(pool, pool->size_class[class_idx], zspage);
2218 spin_unlock(&class->lock);
2219 }
2220};
2221
2222static void kick_deferred_free(struct zs_pool *pool)
2223{
2224 schedule_work(&pool->free_work);
2225}
2226
2227static void init_deferred_free(struct zs_pool *pool)
2228{
2229 INIT_WORK(&pool->free_work, async_free_zspage);
2230}
2231
2232static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2233{
2234 struct page *page = get_first_page(zspage);
2235
2236 do {
2237 WARN_ON(!trylock_page(page));
2238 __SetPageMovable(page, pool->inode->i_mapping);
2239 unlock_page(page);
2240 } while ((page = get_next_page(page)) != NULL);
2241}
2242#endif
2243
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002244/*
2245 *
2246 * Based on the number of unused allocated objects calculate
2247 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002248 */
2249static unsigned long zs_can_compact(struct size_class *class)
2250{
2251 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002252 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2253 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002254
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002255 if (obj_allocated <= obj_used)
2256 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002257
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002258 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002259 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002260
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002261 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002262}
2263
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002264static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002265{
Minchan Kim312fcae2015-04-15 16:15:30 -07002266 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002267 struct zspage *src_zspage;
2268 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002269
Minchan Kim312fcae2015-04-15 16:15:30 -07002270 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002271 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002272
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002273 if (!zs_can_compact(class))
2274 break;
2275
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002276 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002277 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002278
Minchan Kim37836892016-07-26 15:23:23 -07002279 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002280 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002281 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002282 * If there is no more space in dst_page, resched
2283 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002284 */
2285 if (!migrate_zspage(pool, class, &cc))
2286 break;
2287
Minchan Kim4aa409c2016-07-26 15:23:26 -07002288 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002289 }
2290
2291 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002292 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002293 break;
2294
Minchan Kim4aa409c2016-07-26 15:23:26 -07002295 putback_zspage(class, dst_zspage);
2296 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002297 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002298 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002299 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002300 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002301 cond_resched();
2302 spin_lock(&class->lock);
2303 }
2304
Minchan Kim37836892016-07-26 15:23:23 -07002305 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002306 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002307
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002308 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002309}
2310
2311unsigned long zs_compact(struct zs_pool *pool)
2312{
2313 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002314 struct size_class *class;
2315
2316 for (i = zs_size_classes - 1; i >= 0; i--) {
2317 class = pool->size_class[i];
2318 if (!class)
2319 continue;
2320 if (class->index != i)
2321 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002322 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002323 }
2324
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002325 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002326}
2327EXPORT_SYMBOL_GPL(zs_compact);
2328
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002329void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2330{
2331 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2332}
2333EXPORT_SYMBOL_GPL(zs_pool_stats);
2334
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002335static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2336 struct shrink_control *sc)
2337{
2338 unsigned long pages_freed;
2339 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2340 shrinker);
2341
2342 pages_freed = pool->stats.pages_compacted;
2343 /*
2344 * Compact classes and calculate compaction delta.
2345 * Can run concurrently with a manually triggered
2346 * (by user) compaction.
2347 */
2348 pages_freed = zs_compact(pool) - pages_freed;
2349
2350 return pages_freed ? pages_freed : SHRINK_STOP;
2351}
2352
2353static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2354 struct shrink_control *sc)
2355{
2356 int i;
2357 struct size_class *class;
2358 unsigned long pages_to_free = 0;
2359 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2360 shrinker);
2361
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002362 for (i = zs_size_classes - 1; i >= 0; i--) {
2363 class = pool->size_class[i];
2364 if (!class)
2365 continue;
2366 if (class->index != i)
2367 continue;
2368
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002369 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002370 }
2371
2372 return pages_to_free;
2373}
2374
2375static void zs_unregister_shrinker(struct zs_pool *pool)
2376{
2377 if (pool->shrinker_enabled) {
2378 unregister_shrinker(&pool->shrinker);
2379 pool->shrinker_enabled = false;
2380 }
2381}
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);
Minchan Kim2e40e162015-04-15 16:15:23 -07002414 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2415 GFP_KERNEL);
2416 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002417 kfree(pool);
2418 return NULL;
2419 }
2420
Minchan Kim2e40e162015-04-15 16:15:23 -07002421 pool->name = kstrdup(name, GFP_KERNEL);
2422 if (!pool->name)
2423 goto err;
2424
Minchan Kim37836892016-07-26 15:23:23 -07002425 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002426 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002427
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002428 /*
2429 * Iterate reversly, because, size of size_class that we want to use
2430 * for merging should be larger or equal to current size.
2431 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002432 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002433 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002434 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002435 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002436 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002437 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002438
2439 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2440 if (size > ZS_MAX_ALLOC_SIZE)
2441 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002442 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002443 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002444
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002445 /*
2446 * size_class is used for normal zsmalloc operation such
2447 * as alloc/free for that size. Although it is natural that we
2448 * have one size_class for each size, there is a chance that we
2449 * can get more memory utilization if we use one size_class for
2450 * many different sizes whose size_class have same
2451 * characteristics. So, we makes size_class point to
2452 * previous size_class if possible.
2453 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002454 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002455 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002456 pool->size_class[i] = prev_class;
2457 continue;
2458 }
2459 }
2460
2461 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2462 if (!class)
2463 goto err;
2464
Nitin Gupta61989a82012-01-09 16:51:56 -06002465 class->size = size;
2466 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002467 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002468 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002469 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002470 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002471 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2472 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002473 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002474
2475 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002476 }
2477
Dan Streetmand34f6152016-05-20 16:59:56 -07002478 /* debug only, don't abort if it fails */
2479 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002480
Minchan Kim48b48002016-07-26 15:23:31 -07002481 if (zs_register_migration(pool))
2482 goto err;
2483
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002484 /*
2485 * Not critical, we still can use the pool
2486 * and user can trigger compaction manually.
2487 */
2488 if (zs_register_shrinker(pool) == 0)
2489 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002490 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002491
2492err:
2493 zs_destroy_pool(pool);
2494 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002495}
2496EXPORT_SYMBOL_GPL(zs_create_pool);
2497
2498void zs_destroy_pool(struct zs_pool *pool)
2499{
2500 int i;
2501
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002502 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002503 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002504 zs_pool_stat_destroy(pool);
2505
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002506 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002507 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002508 struct size_class *class = pool->size_class[i];
2509
2510 if (!class)
2511 continue;
2512
2513 if (class->index != i)
2514 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002515
Minchan Kim48b48002016-07-26 15:23:31 -07002516 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002517 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002518 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002519 class->size, fg);
2520 }
2521 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002522 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002523 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002524
Minchan Kim37836892016-07-26 15:23:23 -07002525 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002526 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002527 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002528 kfree(pool);
2529}
2530EXPORT_SYMBOL_GPL(zs_destroy_pool);
2531
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002532static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002533{
Minchan Kim48b48002016-07-26 15:23:31 -07002534 int ret;
2535
2536 ret = zsmalloc_mount();
2537 if (ret)
2538 goto out;
2539
2540 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002541
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002542 if (ret)
2543 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002544
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002545 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002546
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002547#ifdef CONFIG_ZPOOL
2548 zpool_register_driver(&zs_zpool_driver);
2549#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002550
Dan Streetman4abaac92016-05-26 15:16:27 -07002551 zs_stat_init();
2552
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002553 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002554
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002555notifier_fail:
2556 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002557 zsmalloc_unmount();
2558out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002559 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002560}
Nitin Gupta61989a82012-01-09 16:51:56 -06002561
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002562static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002563{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002564#ifdef CONFIG_ZPOOL
2565 zpool_unregister_driver(&zs_zpool_driver);
2566#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002567 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002568 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002569
2570 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002571}
Ben Hutchings069f1012012-06-20 02:31:11 +01002572
2573module_init(zs_init);
2574module_exit(zs_exit);
2575
2576MODULE_LICENSE("Dual BSD/GPL");
2577MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");