blob: 1ce774503fa191445e289e70f269137b6144a858 [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.
Nitin Gupta2db51da2012-06-09 17:41:14 -070023 *
24 * Usage of struct page flags:
25 * PG_private: identifies the first component page
26 * PG_private2: identifies the last component page
Minchan Kim48b48002016-07-26 15:23:31 -070027 * PG_owner_priv_1: indentifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Nitin Gupta61989a82012-01-09 16:51:56 -060033#include <linux/module.h>
34#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070035#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060036#include <linux/bitops.h>
37#include <linux/errno.h>
38#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060039#include <linux/string.h>
40#include <linux/slab.h>
41#include <asm/tlbflush.h>
42#include <asm/pgtable.h>
43#include <linux/cpumask.h>
44#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060045#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080046#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090047#include <linux/spinlock.h>
48#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080049#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080050#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070051#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070052#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070053#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070054#include <linux/pagemap.h>
55
56#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090057
58/*
59 * This must be power of 2 and greater than of equal to sizeof(link_free).
60 * These two conditions ensure that any 'struct link_free' itself doesn't
61 * span more than 1 page which avoids complex case of mapping 2 pages simply
62 * to restore link_free pointer values.
63 */
64#define ZS_ALIGN 8
65
66/*
67 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
68 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
69 */
70#define ZS_MAX_ZSPAGE_ORDER 2
71#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
72
Minchan Kim2e40e162015-04-15 16:15:23 -070073#define ZS_HANDLE_SIZE (sizeof(unsigned long))
74
Seth Jennings0959c632012-08-08 15:12:17 +090075/*
76 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090077 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090078 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070079 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090080 *
81 * This is made more complicated by various memory models and PAE.
82 */
83
84#ifndef MAX_PHYSMEM_BITS
85#ifdef CONFIG_HIGHMEM64G
86#define MAX_PHYSMEM_BITS 36
87#else /* !CONFIG_HIGHMEM64G */
88/*
89 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
90 * be PAGE_SHIFT
91 */
92#define MAX_PHYSMEM_BITS BITS_PER_LONG
93#endif
94#endif
95#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070096
97/*
98 * Memory for allocating for handle keeps object position by
99 * encoding <page, obj_idx> and the encoded value has a room
100 * in least bit(ie, look at obj_to_location).
101 * We use the bit to synchronize between object access by
102 * user and migration.
103 */
104#define HANDLE_PIN_BIT 0
105
106/*
107 * Head in allocated object should have OBJ_ALLOCATED_TAG
108 * to identify the object was allocated or not.
109 * It's okay to add the status bit in the least bit because
110 * header keeps handle which is 4byte-aligned address so we
111 * have room for two bit at least.
112 */
113#define OBJ_ALLOCATED_TAG 1
114#define OBJ_TAG_BITS 1
115#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900116#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
117
118#define MAX(a, b) ((a) >= (b) ? (a) : (b))
119/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
120#define ZS_MIN_ALLOC_SIZE \
121 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700122/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700123#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900124
125/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700126 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900127 * trader-off here:
128 * - Large number of size classes is potentially wasteful as free page are
129 * spread across these classes
130 * - Small number of size classes causes large internal fragmentation
131 * - Probably its better to use specific size classes (empirically
132 * determined). NOTE: all those class sizes must be set as multiple of
133 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
134 *
135 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
136 * (reason above)
137 */
Minchan Kim37836892016-07-26 15:23:23 -0700138#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900139
140/*
141 * We do not maintain any list for completely empty or full pages
142 */
143enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900144 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700145 ZS_ALMOST_EMPTY,
146 ZS_ALMOST_FULL,
147 ZS_FULL,
148 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900149};
150
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800151enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700152 CLASS_EMPTY,
153 CLASS_ALMOST_EMPTY,
154 CLASS_ALMOST_FULL,
155 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800156 OBJ_ALLOCATED,
157 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700158 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800159};
160
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800161struct zs_size_stat {
162 unsigned long objs[NR_ZS_STAT_TYPE];
163};
164
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700165#ifdef CONFIG_ZSMALLOC_STAT
166static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800167#endif
168
Minchan Kim48b48002016-07-26 15:23:31 -0700169#ifdef CONFIG_COMPACTION
170static struct vfsmount *zsmalloc_mnt;
171#endif
172
Seth Jennings0959c632012-08-08 15:12:17 +0900173/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800174 * number of size_classes
175 */
176static int zs_size_classes;
177
178/*
Seth Jennings0959c632012-08-08 15:12:17 +0900179 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
180 * n <= N / f, where
181 * n = number of allocated objects
182 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700183 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900184 *
185 * Similarly, we assign zspage to:
186 * ZS_ALMOST_FULL when n > N / f
187 * ZS_EMPTY when n == 0
188 * ZS_FULL when n == N
189 *
190 * (see: fix_fullness_group())
191 */
192static const int fullness_threshold_frac = 4;
193
194struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700195 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700196 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900197 /*
198 * Size of objects stored in this class. Must be multiple
199 * of ZS_ALIGN.
200 */
201 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700202 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800203 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
204 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700205
206 unsigned int index;
207 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900208};
209
Minchan Kim48b48002016-07-26 15:23:31 -0700210/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
211static void SetPageHugeObject(struct page *page)
212{
213 SetPageOwnerPriv1(page);
214}
215
216static void ClearPageHugeObject(struct page *page)
217{
218 ClearPageOwnerPriv1(page);
219}
220
221static int PageHugeObject(struct page *page)
222{
223 return PageOwnerPriv1(page);
224}
225
Seth Jennings0959c632012-08-08 15:12:17 +0900226/*
227 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700228 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900229 *
230 * This must be power of 2 and less than or equal to ZS_ALIGN
231 */
232struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700233 union {
234 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700235 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700236 * It's valid for non-allocated object
237 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700238 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700239 /*
240 * Handle of allocated object.
241 */
242 unsigned long handle;
243 };
Seth Jennings0959c632012-08-08 15:12:17 +0900244};
245
246struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800247 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800248
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800249 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700250 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700251 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900252
Minchan Kim13de8932014-10-09 15:29:48 -0700253 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800254
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700255 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700256
257 /* Compact classes */
258 struct shrinker shrinker;
259 /*
260 * To signify that register_shrinker() was successful
261 * and unregister_shrinker() will not Oops.
262 */
263 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800264#ifdef CONFIG_ZSMALLOC_STAT
265 struct dentry *stat_dentry;
266#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700267#ifdef CONFIG_COMPACTION
268 struct inode *inode;
269 struct work_struct free_work;
270#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900271};
Nitin Gupta61989a82012-01-09 16:51:56 -0600272
273/*
274 * A zspage's class index and fullness group
275 * are encoded in its (first)page->mapping
276 */
Minchan Kim37836892016-07-26 15:23:23 -0700277#define FULLNESS_BITS 2
278#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700279#define ISOLATED_BITS 3
280#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700281
Minchan Kim37836892016-07-26 15:23:23 -0700282struct zspage {
283 struct {
284 unsigned int fullness:FULLNESS_BITS;
285 unsigned int class:CLASS_BITS;
Minchan Kim48b48002016-07-26 15:23:31 -0700286 unsigned int isolated:ISOLATED_BITS;
287 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700288 };
289 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700290 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700291 struct page *first_page;
292 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700293#ifdef CONFIG_COMPACTION
294 rwlock_t lock;
295#endif
Minchan Kim37836892016-07-26 15:23:23 -0700296};
Nitin Gupta61989a82012-01-09 16:51:56 -0600297
Seth Jenningsf5536462012-07-18 11:55:56 -0500298struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900299#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500300 struct vm_struct *vm; /* vm area for mapping object that span pages */
301#else
302 char *vm_buf; /* copy buffer for objects that span pages */
303#endif
304 char *vm_addr; /* address of kmap_atomic()'ed pages */
305 enum zs_mapmode vm_mm; /* mapping mode */
306};
307
Minchan Kim48b48002016-07-26 15:23:31 -0700308#ifdef CONFIG_COMPACTION
309static int zs_register_migration(struct zs_pool *pool);
310static void zs_unregister_migration(struct zs_pool *pool);
311static void migrate_lock_init(struct zspage *zspage);
312static void migrate_read_lock(struct zspage *zspage);
313static void migrate_read_unlock(struct zspage *zspage);
314static void kick_deferred_free(struct zs_pool *pool);
315static void init_deferred_free(struct zs_pool *pool);
316static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
317#else
318static int zsmalloc_mount(void) { return 0; }
319static void zsmalloc_unmount(void) {}
320static int zs_register_migration(struct zs_pool *pool) { return 0; }
321static void zs_unregister_migration(struct zs_pool *pool) {}
322static void migrate_lock_init(struct zspage *zspage) {}
323static void migrate_read_lock(struct zspage *zspage) {}
324static void migrate_read_unlock(struct zspage *zspage) {}
325static void kick_deferred_free(struct zs_pool *pool) {}
326static void init_deferred_free(struct zs_pool *pool) {}
327static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
328#endif
329
Minchan Kim37836892016-07-26 15:23:23 -0700330static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700331{
332 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
333 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700334 if (!pool->handle_cachep)
335 return 1;
336
337 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
338 0, 0, NULL);
339 if (!pool->zspage_cachep) {
340 kmem_cache_destroy(pool->handle_cachep);
341 pool->handle_cachep = NULL;
342 return 1;
343 }
344
345 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700346}
347
Minchan Kim37836892016-07-26 15:23:23 -0700348static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700349{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700350 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700351 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700352}
353
Minchan Kim37836892016-07-26 15:23:23 -0700354static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700355{
356 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700357 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700358}
359
Minchan Kim37836892016-07-26 15:23:23 -0700360static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700361{
362 kmem_cache_free(pool->handle_cachep, (void *)handle);
363}
364
Minchan Kim37836892016-07-26 15:23:23 -0700365static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
366{
Minchan Kim48b48002016-07-26 15:23:31 -0700367 return kmem_cache_alloc(pool->zspage_cachep,
368 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim37836892016-07-26 15:23:23 -0700369};
370
371static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
372{
373 kmem_cache_free(pool->zspage_cachep, zspage);
374}
375
Minchan Kim2e40e162015-04-15 16:15:23 -0700376static void record_obj(unsigned long handle, unsigned long obj)
377{
Junil Leec102f072016-01-20 14:58:18 -0800378 /*
379 * lsb of @obj represents handle lock while other bits
380 * represent object value the handle is pointing so
381 * updating shouldn't do store tearing.
382 */
383 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700384}
385
Dan Streetmanc7957792014-08-06 16:08:38 -0700386/* zpool driver */
387
388#ifdef CONFIG_ZPOOL
389
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800390static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700391 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700392 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700393{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700394 /*
395 * Ignore global gfp flags: zs_malloc() may be invoked from
396 * different contexts and its caller must provide a valid
397 * gfp mask.
398 */
399 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700400}
401
402static void zs_zpool_destroy(void *pool)
403{
404 zs_destroy_pool(pool);
405}
406
407static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
408 unsigned long *handle)
409{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700410 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700411 return *handle ? 0 : -1;
412}
413static void zs_zpool_free(void *pool, unsigned long handle)
414{
415 zs_free(pool, handle);
416}
417
418static int zs_zpool_shrink(void *pool, unsigned int pages,
419 unsigned int *reclaimed)
420{
421 return -EINVAL;
422}
423
424static void *zs_zpool_map(void *pool, unsigned long handle,
425 enum zpool_mapmode mm)
426{
427 enum zs_mapmode zs_mm;
428
429 switch (mm) {
430 case ZPOOL_MM_RO:
431 zs_mm = ZS_MM_RO;
432 break;
433 case ZPOOL_MM_WO:
434 zs_mm = ZS_MM_WO;
435 break;
436 case ZPOOL_MM_RW: /* fallthru */
437 default:
438 zs_mm = ZS_MM_RW;
439 break;
440 }
441
442 return zs_map_object(pool, handle, zs_mm);
443}
444static void zs_zpool_unmap(void *pool, unsigned long handle)
445{
446 zs_unmap_object(pool, handle);
447}
448
449static u64 zs_zpool_total_size(void *pool)
450{
Minchan Kim722cdc12014-10-09 15:29:50 -0700451 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700452}
453
454static struct zpool_driver zs_zpool_driver = {
455 .type = "zsmalloc",
456 .owner = THIS_MODULE,
457 .create = zs_zpool_create,
458 .destroy = zs_zpool_destroy,
459 .malloc = zs_zpool_malloc,
460 .free = zs_zpool_free,
461 .shrink = zs_zpool_shrink,
462 .map = zs_zpool_map,
463 .unmap = zs_zpool_unmap,
464 .total_size = zs_zpool_total_size,
465};
466
Kees Cook137f8cf2014-08-29 15:18:40 -0700467MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700468#endif /* CONFIG_ZPOOL */
469
Nitin Gupta61989a82012-01-09 16:51:56 -0600470/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
471static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
472
Minchan Kim48b48002016-07-26 15:23:31 -0700473static bool is_zspage_isolated(struct zspage *zspage)
474{
475 return zspage->isolated;
476}
477
Nitin Gupta61989a82012-01-09 16:51:56 -0600478static int is_first_page(struct page *page)
479{
Minchan Kima27545bf2012-04-25 15:23:09 +0900480 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600481}
482
Minchan Kim48b48002016-07-26 15:23:31 -0700483/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700484static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600485{
Minchan Kim37836892016-07-26 15:23:23 -0700486 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600487}
488
Minchan Kim37836892016-07-26 15:23:23 -0700489static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700490{
Minchan Kim37836892016-07-26 15:23:23 -0700491 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700492}
493
Minchan Kim37836892016-07-26 15:23:23 -0700494static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700495{
Minchan Kim37836892016-07-26 15:23:23 -0700496 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700497}
498
Minchan Kim48b48002016-07-26 15:23:31 -0700499static inline struct page *get_first_page(struct zspage *zspage)
500{
501 struct page *first_page = zspage->first_page;
502
503 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
504 return first_page;
505}
506
Minchan Kim4f420472016-07-26 15:23:17 -0700507static inline int get_first_obj_offset(struct page *page)
508{
Minchan Kim48b48002016-07-26 15:23:31 -0700509 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700510}
511
512static inline void set_first_obj_offset(struct page *page, int offset)
513{
Minchan Kim48b48002016-07-26 15:23:31 -0700514 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700515}
516
Minchan Kimbfd093f2016-07-26 15:23:28 -0700517static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700518{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700519 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700520}
521
Minchan Kimbfd093f2016-07-26 15:23:28 -0700522static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700523{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700524 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700525}
526
Minchan Kim37836892016-07-26 15:23:23 -0700527static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700528 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600529 enum fullness_group *fullness)
530{
Minchan Kim48b48002016-07-26 15:23:31 -0700531 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
532
Minchan Kim37836892016-07-26 15:23:23 -0700533 *fullness = zspage->fullness;
534 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600535}
536
Minchan Kim37836892016-07-26 15:23:23 -0700537static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700538 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600539 enum fullness_group fullness)
540{
Minchan Kim37836892016-07-26 15:23:23 -0700541 zspage->class = class_idx;
542 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600543}
544
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900545/*
546 * zsmalloc divides the pool into various size classes where each
547 * class maintains a list of zspages where each zspage is divided
548 * into equal sized chunks. Each allocation falls into one of these
549 * classes depending on its size. This function returns index of the
550 * size class which has chunk size big enough to hold the give size.
551 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600552static int get_size_class_index(int size)
553{
554 int idx = 0;
555
556 if (likely(size > ZS_MIN_ALLOC_SIZE))
557 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
558 ZS_SIZE_CLASS_DELTA);
559
Minchan Kim7b60a682015-04-15 16:15:39 -0700560 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600561}
562
Minchan Kim248ca1b2015-04-15 16:15:42 -0700563static inline void zs_stat_inc(struct size_class *class,
564 enum zs_stat_type type, unsigned long cnt)
565{
Minchan Kim48b48002016-07-26 15:23:31 -0700566 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700567}
568
569static inline void zs_stat_dec(struct size_class *class,
570 enum zs_stat_type type, unsigned long cnt)
571{
Minchan Kim48b48002016-07-26 15:23:31 -0700572 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700573}
574
575static inline unsigned long zs_stat_get(struct size_class *class,
576 enum zs_stat_type type)
577{
Minchan Kim48b48002016-07-26 15:23:31 -0700578 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700579}
580
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700581#ifdef CONFIG_ZSMALLOC_STAT
582
Dan Streetman4abaac92016-05-26 15:16:27 -0700583static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700584{
Dan Streetman4abaac92016-05-26 15:16:27 -0700585 if (!debugfs_initialized()) {
586 pr_warn("debugfs not available, stat dir not created\n");
587 return;
588 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700589
590 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
591 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700592 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700593}
594
595static void __exit zs_stat_exit(void)
596{
597 debugfs_remove_recursive(zs_stat_root);
598}
599
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700600static unsigned long zs_can_compact(struct size_class *class);
601
Minchan Kim248ca1b2015-04-15 16:15:42 -0700602static int zs_stats_size_show(struct seq_file *s, void *v)
603{
604 int i;
605 struct zs_pool *pool = s->private;
606 struct size_class *class;
607 int objs_per_zspage;
608 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700609 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700610 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
611 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700612 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700613
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700614 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700615 "class", "size", "almost_full", "almost_empty",
616 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700617 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700618
619 for (i = 0; i < zs_size_classes; i++) {
620 class = pool->size_class[i];
621
622 if (class->index != i)
623 continue;
624
625 spin_lock(&class->lock);
626 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
627 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
628 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
629 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700630 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700631 spin_unlock(&class->lock);
632
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700633 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700634 pages_used = obj_allocated / objs_per_zspage *
635 class->pages_per_zspage;
636
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700637 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
638 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700639 i, class->size, class_almost_full, class_almost_empty,
640 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700641 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700642
643 total_class_almost_full += class_almost_full;
644 total_class_almost_empty += class_almost_empty;
645 total_objs += obj_allocated;
646 total_used_objs += obj_used;
647 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700648 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700649 }
650
651 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700652 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700653 "Total", "", total_class_almost_full,
654 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700655 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700656
657 return 0;
658}
659
660static int zs_stats_size_open(struct inode *inode, struct file *file)
661{
662 return single_open(file, zs_stats_size_show, inode->i_private);
663}
664
665static const struct file_operations zs_stat_size_ops = {
666 .open = zs_stats_size_open,
667 .read = seq_read,
668 .llseek = seq_lseek,
669 .release = single_release,
670};
671
Dan Streetmand34f6152016-05-20 16:59:56 -0700672static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700673{
674 struct dentry *entry;
675
Dan Streetman4abaac92016-05-26 15:16:27 -0700676 if (!zs_stat_root) {
677 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700678 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700679 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700680
681 entry = debugfs_create_dir(name, zs_stat_root);
682 if (!entry) {
683 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700684 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700685 }
686 pool->stat_dentry = entry;
687
688 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
689 pool->stat_dentry, pool, &zs_stat_size_ops);
690 if (!entry) {
691 pr_warn("%s: debugfs file entry <%s> creation failed\n",
692 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700693 debugfs_remove_recursive(pool->stat_dentry);
694 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700695 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700696}
697
698static void zs_pool_stat_destroy(struct zs_pool *pool)
699{
700 debugfs_remove_recursive(pool->stat_dentry);
701}
702
703#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700704static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700705{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700706}
707
708static void __exit zs_stat_exit(void)
709{
710}
711
Dan Streetmand34f6152016-05-20 16:59:56 -0700712static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700713{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700714}
715
716static inline void zs_pool_stat_destroy(struct zs_pool *pool)
717{
718}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700719#endif
720
Minchan Kim48b48002016-07-26 15:23:31 -0700721
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900722/*
723 * For each size class, zspages are divided into different groups
724 * depending on how "full" they are. This was done so that we could
725 * easily find empty or nearly empty zspages when we try to shrink
726 * the pool (not yet implemented). This function returns fullness
727 * status of the given page.
728 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700729static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700730 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600731{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700732 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600733 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700734
Minchan Kim37836892016-07-26 15:23:23 -0700735 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700736 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600737
738 if (inuse == 0)
739 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700740 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600741 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700742 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600743 fg = ZS_ALMOST_EMPTY;
744 else
745 fg = ZS_ALMOST_FULL;
746
747 return fg;
748}
749
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900750/*
751 * Each size class maintains various freelists and zspages are assigned
752 * to one of these freelists based on the number of live objects they
753 * have. This functions inserts the given zspage into the freelist
754 * identified by <class, fullness_group>.
755 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700756static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700757 struct zspage *zspage,
758 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600759{
Minchan Kim37836892016-07-26 15:23:23 -0700760 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600761
Minchan Kim48b48002016-07-26 15:23:31 -0700762 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700763 head = list_first_entry_or_null(&class->fullness_list[fullness],
764 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700765 /*
Minchan Kim37836892016-07-26 15:23:23 -0700766 * We want to see more ZS_FULL pages and less almost empty/full.
767 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700768 */
Minchan Kim37836892016-07-26 15:23:23 -0700769 if (head) {
770 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
771 list_add(&zspage->list, &head->list);
772 return;
773 }
774 }
775 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600776}
777
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900778/*
779 * This function removes the given zspage from the freelist identified
780 * by <class, fullness_group>.
781 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700782static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700783 struct zspage *zspage,
784 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600785{
Minchan Kim37836892016-07-26 15:23:23 -0700786 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700787 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600788
Minchan Kim37836892016-07-26 15:23:23 -0700789 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700790 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600791}
792
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900793/*
794 * Each size class maintains zspages in different fullness groups depending
795 * on the number of live objects they contain. When allocating or freeing
796 * objects, the fullness status of the page can change, say, from ALMOST_FULL
797 * to ALMOST_EMPTY when freeing an object. This function checks if such
798 * a status change has occurred for the given page and accordingly moves the
799 * page from the freelist of the old fullness group to that of the new
800 * fullness group.
801 */
Minchan Kimc7806262015-04-15 16:15:26 -0700802static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700803 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600804{
805 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600806 enum fullness_group currfg, newfg;
807
Minchan Kim37836892016-07-26 15:23:23 -0700808 get_zspage_mapping(zspage, &class_idx, &currfg);
809 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600810 if (newfg == currfg)
811 goto out;
812
Minchan Kim48b48002016-07-26 15:23:31 -0700813 if (!is_zspage_isolated(zspage)) {
814 remove_zspage(class, zspage, currfg);
815 insert_zspage(class, zspage, newfg);
816 }
817
Minchan Kim37836892016-07-26 15:23:23 -0700818 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600819
820out:
821 return newfg;
822}
823
824/*
825 * We have to decide on how many pages to link together
826 * to form a zspage for each size class. This is important
827 * to reduce wastage due to unusable space left at end of
828 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700829 * wastage = Zp % class_size
830 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600831 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
832 *
833 * For example, for size class of 3/8 * PAGE_SIZE, we should
834 * link together 3 PAGE_SIZE sized pages to form a zspage
835 * since then we can perfectly fit in 8 such objects.
836 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900837static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600838{
839 int i, max_usedpc = 0;
840 /* zspage order which gives maximum used size per KB */
841 int max_usedpc_order = 1;
842
Seth Jennings84d4faa2012-03-05 11:33:21 -0600843 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600844 int zspage_size;
845 int waste, usedpc;
846
847 zspage_size = i * PAGE_SIZE;
848 waste = zspage_size % class_size;
849 usedpc = (zspage_size - waste) * 100 / zspage_size;
850
851 if (usedpc > max_usedpc) {
852 max_usedpc = usedpc;
853 max_usedpc_order = i;
854 }
855 }
856
857 return max_usedpc_order;
858}
859
Minchan Kim37836892016-07-26 15:23:23 -0700860static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600861{
Minchan Kim48b48002016-07-26 15:23:31 -0700862 struct zspage *zspage = (struct zspage *)page->private;
863
864 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
865 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600866}
867
868static struct page *get_next_page(struct page *page)
869{
Minchan Kim48b48002016-07-26 15:23:31 -0700870 if (unlikely(PageHugeObject(page)))
871 return NULL;
872
873 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600874}
875
Minchan Kimbfd093f2016-07-26 15:23:28 -0700876/**
877 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
878 * @page: page object resides in zspage
879 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800880 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700881static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700882 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600883{
Minchan Kim312fcae2015-04-15 16:15:30 -0700884 obj >>= OBJ_TAG_BITS;
885 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
886 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600887}
888
Minchan Kimbfd093f2016-07-26 15:23:28 -0700889/**
890 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
891 * @page: page object resides in zspage
892 * @obj_idx: object index
893 */
894static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
895{
896 unsigned long obj;
897
898 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
899 obj |= obj_idx & OBJ_INDEX_MASK;
900 obj <<= OBJ_TAG_BITS;
901
902 return obj;
903}
904
Minchan Kim2e40e162015-04-15 16:15:23 -0700905static unsigned long handle_to_obj(unsigned long handle)
906{
907 return *(unsigned long *)handle;
908}
909
Minchan Kim48b48002016-07-26 15:23:31 -0700910static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700911{
Minchan Kim48b48002016-07-26 15:23:31 -0700912 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700913 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700914 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700915 } else
916 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700917}
918
Minchan Kim48b48002016-07-26 15:23:31 -0700919static inline int testpin_tag(unsigned long handle)
920{
921 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
922}
923
Minchan Kim312fcae2015-04-15 16:15:30 -0700924static inline int trypin_tag(unsigned long handle)
925{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700926 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700927}
928
929static void pin_tag(unsigned long handle)
930{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700931 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700932}
933
934static void unpin_tag(unsigned long handle)
935{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700936 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700937}
938
Nitin Guptaf4477e92012-04-02 09:13:56 -0500939static void reset_page(struct page *page)
940{
Minchan Kim48b48002016-07-26 15:23:31 -0700941 __ClearPageMovable(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500942 clear_bit(PG_private, &page->flags);
943 clear_bit(PG_private_2, &page->flags);
944 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700945 page_mapcount_reset(page);
946 ClearPageHugeObject(page);
947 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500948}
949
Minchan Kim48b48002016-07-26 15:23:31 -0700950/*
951 * To prevent zspage destroy during migration, zspage freeing should
952 * hold locks of all pages in the zspage.
953 */
954void lock_zspage(struct zspage *zspage)
955{
956 struct page *page = get_first_page(zspage);
957
958 do {
959 lock_page(page);
960 } while ((page = get_next_page(page)) != NULL);
961}
962
963int trylock_zspage(struct zspage *zspage)
964{
965 struct page *cursor, *fail;
966
967 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
968 get_next_page(cursor)) {
969 if (!trylock_page(cursor)) {
970 fail = cursor;
971 goto unlock;
972 }
973 }
974
975 return 1;
976unlock:
977 for (cursor = get_first_page(zspage); cursor != fail; cursor =
978 get_next_page(cursor))
979 unlock_page(cursor);
980
981 return 0;
982}
983
984static void __free_zspage(struct zs_pool *pool, struct size_class *class,
985 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600986{
Minchan Kim37836892016-07-26 15:23:23 -0700987 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700988 enum fullness_group fg;
989 unsigned int class_idx;
990
991 get_zspage_mapping(zspage, &class_idx, &fg);
992
993 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600994
Minchan Kim37836892016-07-26 15:23:23 -0700995 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700996 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600997
Minchan Kim48b48002016-07-26 15:23:31 -0700998 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700999 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001000 VM_BUG_ON_PAGE(!PageLocked(page), page);
1001 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001002 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001003 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001004 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001005 put_page(page);
1006 page = next;
1007 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001008
Minchan Kim37836892016-07-26 15:23:23 -07001009 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001010
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001011 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001012 atomic_long_sub(class->pages_per_zspage,
1013 &pool->pages_allocated);
1014}
1015
1016static void free_zspage(struct zs_pool *pool, struct size_class *class,
1017 struct zspage *zspage)
1018{
1019 VM_BUG_ON(get_zspage_inuse(zspage));
1020 VM_BUG_ON(list_empty(&zspage->list));
1021
1022 if (!trylock_zspage(zspage)) {
1023 kick_deferred_free(pool);
1024 return;
1025 }
1026
1027 remove_zspage(class, zspage, ZS_EMPTY);
1028 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001029}
1030
1031/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001032static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001033{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001034 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001035 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001036 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001037
Nitin Gupta61989a82012-01-09 16:51:56 -06001038 while (page) {
1039 struct page *next_page;
1040 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001041 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001042
Minchan Kim37836892016-07-26 15:23:23 -07001043 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001044
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001045 vaddr = kmap_atomic(page);
1046 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001047
Dan Streetman5538c562014-10-09 15:30:01 -07001048 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001049 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001050 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001051 }
1052
1053 /*
1054 * We now come to the last (full or partial) object on this
1055 * page, which must point to the first object on the next
1056 * page (if present)
1057 */
1058 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001059 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001060 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001061 } else {
1062 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001063 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001064 * whether it's allocated object or not.
1065 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001066 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001067 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001068 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001069 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001070 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001071 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001072
Minchan Kimbfd093f2016-07-26 15:23:28 -07001073 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001074}
1075
Minchan Kim48b48002016-07-26 15:23:31 -07001076static void create_page_chain(struct size_class *class, struct zspage *zspage,
1077 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001078{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001079 int i;
1080 struct page *page;
1081 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001082 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001083
1084 /*
1085 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001086 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001087 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001088 *
Minchan Kim37836892016-07-26 15:23:23 -07001089 * we set PG_private to identify the first page (i.e. no other sub-page
1090 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001091 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001092 for (i = 0; i < nr_pages; i++) {
1093 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001094 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001095 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001096 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001097 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001098 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001099 if (unlikely(class->objs_per_zspage == 1 &&
1100 class->pages_per_zspage == 1))
1101 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001102 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001103 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001104 }
Minchan Kim48b48002016-07-26 15:23:31 -07001105 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001106 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001107 prev_page = page;
1108 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001109}
Nitin Gupta61989a82012-01-09 16:51:56 -06001110
Minchan Kimbdb0af72016-07-26 15:23:20 -07001111/*
1112 * Allocate a zspage for the given size class
1113 */
Minchan Kim37836892016-07-26 15:23:23 -07001114static struct zspage *alloc_zspage(struct zs_pool *pool,
1115 struct size_class *class,
1116 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001117{
1118 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001119 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001120 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1121
1122 if (!zspage)
1123 return NULL;
1124
1125 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001126 zspage->magic = ZSPAGE_MAGIC;
1127 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001128
Minchan Kimbdb0af72016-07-26 15:23:20 -07001129 for (i = 0; i < class->pages_per_zspage; i++) {
1130 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001131
Minchan Kim37836892016-07-26 15:23:23 -07001132 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001133 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001134 while (--i >= 0) {
1135 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001136 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001137 }
Minchan Kim37836892016-07-26 15:23:23 -07001138 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001139 return NULL;
1140 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001141
1142 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001143 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001144 }
1145
Minchan Kim48b48002016-07-26 15:23:31 -07001146 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001147 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001148
Minchan Kim37836892016-07-26 15:23:23 -07001149 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001150}
1151
Minchan Kim37836892016-07-26 15:23:23 -07001152static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001153{
1154 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001155 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001156
Minchan Kim48b48002016-07-26 15:23:31 -07001157 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001158 zspage = list_first_entry_or_null(&class->fullness_list[i],
1159 struct zspage, list);
1160 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001161 break;
1162 }
1163
Minchan Kim37836892016-07-26 15:23:23 -07001164 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001165}
1166
Minchan Kim1b945ae2013-12-11 11:04:36 +09001167#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001168static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001169{
Seth Jenningsf5536462012-07-18 11:55:56 -05001170 /*
1171 * Make sure we don't leak memory if a cpu UP notification
1172 * and zs_init() race and both call zs_cpu_up() on the same cpu
1173 */
1174 if (area->vm)
1175 return 0;
1176 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1177 if (!area->vm)
1178 return -ENOMEM;
1179 return 0;
1180}
1181
1182static inline void __zs_cpu_down(struct mapping_area *area)
1183{
1184 if (area->vm)
1185 free_vm_area(area->vm);
1186 area->vm = NULL;
1187}
1188
1189static inline void *__zs_map_object(struct mapping_area *area,
1190 struct page *pages[2], int off, int size)
1191{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001192 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001193 area->vm_addr = area->vm->addr;
1194 return area->vm_addr + off;
1195}
1196
1197static inline void __zs_unmap_object(struct mapping_area *area,
1198 struct page *pages[2], int off, int size)
1199{
1200 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001201
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001202 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001203}
1204
Minchan Kim1b945ae2013-12-11 11:04:36 +09001205#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001206
1207static inline int __zs_cpu_up(struct mapping_area *area)
1208{
1209 /*
1210 * Make sure we don't leak memory if a cpu UP notification
1211 * and zs_init() race and both call zs_cpu_up() on the same cpu
1212 */
1213 if (area->vm_buf)
1214 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001215 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001216 if (!area->vm_buf)
1217 return -ENOMEM;
1218 return 0;
1219}
1220
1221static inline void __zs_cpu_down(struct mapping_area *area)
1222{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001223 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001224 area->vm_buf = NULL;
1225}
1226
1227static void *__zs_map_object(struct mapping_area *area,
1228 struct page *pages[2], int off, int size)
1229{
Seth Jennings5f601902012-07-02 16:15:49 -05001230 int sizes[2];
1231 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001232 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001233
Seth Jenningsf5536462012-07-18 11:55:56 -05001234 /* disable page faults to match kmap_atomic() return conditions */
1235 pagefault_disable();
1236
1237 /* no read fastpath */
1238 if (area->vm_mm == ZS_MM_WO)
1239 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001240
1241 sizes[0] = PAGE_SIZE - off;
1242 sizes[1] = size - sizes[0];
1243
Seth Jennings5f601902012-07-02 16:15:49 -05001244 /* copy object to per-cpu buffer */
1245 addr = kmap_atomic(pages[0]);
1246 memcpy(buf, addr + off, sizes[0]);
1247 kunmap_atomic(addr);
1248 addr = kmap_atomic(pages[1]);
1249 memcpy(buf + sizes[0], addr, sizes[1]);
1250 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001251out:
1252 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001253}
1254
Seth Jenningsf5536462012-07-18 11:55:56 -05001255static void __zs_unmap_object(struct mapping_area *area,
1256 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001257{
Seth Jennings5f601902012-07-02 16:15:49 -05001258 int sizes[2];
1259 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001260 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001261
Seth Jenningsf5536462012-07-18 11:55:56 -05001262 /* no write fastpath */
1263 if (area->vm_mm == ZS_MM_RO)
1264 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001265
Minchan Kim7b60a682015-04-15 16:15:39 -07001266 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001267 buf = buf + ZS_HANDLE_SIZE;
1268 size -= ZS_HANDLE_SIZE;
1269 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001270
Seth Jennings5f601902012-07-02 16:15:49 -05001271 sizes[0] = PAGE_SIZE - off;
1272 sizes[1] = size - sizes[0];
1273
1274 /* copy per-cpu buffer to object */
1275 addr = kmap_atomic(pages[0]);
1276 memcpy(addr + off, buf, sizes[0]);
1277 kunmap_atomic(addr);
1278 addr = kmap_atomic(pages[1]);
1279 memcpy(addr, buf + sizes[0], sizes[1]);
1280 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001281
1282out:
1283 /* enable page faults to match kunmap_atomic() return conditions */
1284 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001285}
Nitin Gupta61989a82012-01-09 16:51:56 -06001286
Minchan Kim1b945ae2013-12-11 11:04:36 +09001287#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001288
Nitin Gupta61989a82012-01-09 16:51:56 -06001289static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1290 void *pcpu)
1291{
Seth Jenningsf5536462012-07-18 11:55:56 -05001292 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001293 struct mapping_area *area;
1294
1295 switch (action) {
1296 case CPU_UP_PREPARE:
1297 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001298 ret = __zs_cpu_up(area);
1299 if (ret)
1300 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001301 break;
1302 case CPU_DEAD:
1303 case CPU_UP_CANCELED:
1304 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001305 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001306 break;
1307 }
1308
1309 return NOTIFY_OK;
1310}
1311
1312static struct notifier_block zs_cpu_nb = {
1313 .notifier_call = zs_cpu_notifier
1314};
1315
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001316static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001317{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001318 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001319
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301320 cpu_notifier_register_begin();
1321
1322 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001323 for_each_online_cpu(cpu) {
1324 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001325 if (notifier_to_errno(ret))
1326 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001327 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301328
1329 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001330 return notifier_to_errno(ret);
1331}
1332
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001333static void zs_unregister_cpu_notifier(void)
1334{
1335 int cpu;
1336
1337 cpu_notifier_register_begin();
1338
1339 for_each_online_cpu(cpu)
1340 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1341 __unregister_cpu_notifier(&zs_cpu_nb);
1342
1343 cpu_notifier_register_done();
1344}
1345
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001346static void init_zs_size_classes(void)
1347{
1348 int nr;
1349
1350 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1351 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1352 nr += 1;
1353
1354 zs_size_classes = nr;
1355}
1356
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001357static bool can_merge(struct size_class *prev, int pages_per_zspage,
1358 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001359{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001360 if (prev->pages_per_zspage == pages_per_zspage &&
1361 prev->objs_per_zspage == objs_per_zspage)
1362 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001363
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001364 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001365}
1366
Minchan Kim37836892016-07-26 15:23:23 -07001367static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001368{
Minchan Kim37836892016-07-26 15:23:23 -07001369 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001370}
1371
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001372unsigned long zs_get_total_pages(struct zs_pool *pool)
1373{
1374 return atomic_long_read(&pool->pages_allocated);
1375}
1376EXPORT_SYMBOL_GPL(zs_get_total_pages);
1377
1378/**
1379 * zs_map_object - get address of allocated object from handle.
1380 * @pool: pool from which the object was allocated
1381 * @handle: handle returned from zs_malloc
1382 *
1383 * Before using an object allocated from zs_malloc, it must be mapped using
1384 * this function. When done with the object, it must be unmapped using
1385 * zs_unmap_object.
1386 *
1387 * Only one object can be mapped per cpu at a time. There is no protection
1388 * against nested mappings.
1389 *
1390 * This function returns with preemption and page faults disabled.
1391 */
1392void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1393 enum zs_mapmode mm)
1394{
Minchan Kim37836892016-07-26 15:23:23 -07001395 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001396 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001397 unsigned long obj, off;
1398 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001399
1400 unsigned int class_idx;
1401 enum fullness_group fg;
1402 struct size_class *class;
1403 struct mapping_area *area;
1404 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001405 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001406
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001407 /*
1408 * Because we use per-cpu mapping areas shared among the
1409 * pools/users, we can't allow mapping in interrupt context
1410 * because it can corrupt another users mappings.
1411 */
Minchan Kim830e4bc2016-05-20 16:59:39 -07001412 WARN_ON_ONCE(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001413
Minchan Kim312fcae2015-04-15 16:15:30 -07001414 /* From now on, migration cannot move the object */
1415 pin_tag(handle);
1416
Minchan Kim2e40e162015-04-15 16:15:23 -07001417 obj = handle_to_obj(handle);
1418 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001419 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001420
1421 /* migration cannot move any subpage in this zspage */
1422 migrate_read_lock(zspage);
1423
Minchan Kim37836892016-07-26 15:23:23 -07001424 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001425 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001426 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001427
1428 area = &get_cpu_var(zs_map_area);
1429 area->vm_mm = mm;
1430 if (off + class->size <= PAGE_SIZE) {
1431 /* this object is contained entirely within a page */
1432 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001433 ret = area->vm_addr + off;
1434 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001435 }
1436
1437 /* this object spans two pages */
1438 pages[0] = page;
1439 pages[1] = get_next_page(page);
1440 BUG_ON(!pages[1]);
1441
Minchan Kim2e40e162015-04-15 16:15:23 -07001442 ret = __zs_map_object(area, pages, off, class->size);
1443out:
Minchan Kim48b48002016-07-26 15:23:31 -07001444 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001445 ret += ZS_HANDLE_SIZE;
1446
1447 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001448}
1449EXPORT_SYMBOL_GPL(zs_map_object);
1450
1451void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1452{
Minchan Kim37836892016-07-26 15:23:23 -07001453 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001454 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001455 unsigned long obj, off;
1456 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001457
1458 unsigned int class_idx;
1459 enum fullness_group fg;
1460 struct size_class *class;
1461 struct mapping_area *area;
1462
Minchan Kim2e40e162015-04-15 16:15:23 -07001463 obj = handle_to_obj(handle);
1464 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001465 zspage = get_zspage(page);
1466 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001467 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001468 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001469
1470 area = this_cpu_ptr(&zs_map_area);
1471 if (off + class->size <= PAGE_SIZE)
1472 kunmap_atomic(area->vm_addr);
1473 else {
1474 struct page *pages[2];
1475
1476 pages[0] = page;
1477 pages[1] = get_next_page(page);
1478 BUG_ON(!pages[1]);
1479
1480 __zs_unmap_object(area, pages, off, class->size);
1481 }
1482 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001483
1484 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001485 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001486}
1487EXPORT_SYMBOL_GPL(zs_unmap_object);
1488
Minchan Kim251cbb92016-05-20 16:59:42 -07001489static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001490 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001491{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001492 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001493 unsigned long obj;
1494 struct link_free *link;
1495
1496 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001497 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001498 void *vaddr;
1499
Minchan Kim312fcae2015-04-15 16:15:30 -07001500 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001501 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001502
1503 offset = obj * class->size;
1504 nr_page = offset >> PAGE_SHIFT;
1505 m_offset = offset & ~PAGE_MASK;
1506 m_page = get_first_page(zspage);
1507
1508 for (i = 0; i < nr_page; i++)
1509 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001510
1511 vaddr = kmap_atomic(m_page);
1512 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001513 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001514 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001515 /* record handle in the header of allocated chunk */
1516 link->handle = handle;
1517 else
Minchan Kim37836892016-07-26 15:23:23 -07001518 /* record handle to page->index */
1519 zspage->first_page->index = handle;
1520
Minchan Kimc7806262015-04-15 16:15:26 -07001521 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001522 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001523 zs_stat_inc(class, OBJ_USED, 1);
1524
Minchan Kimbfd093f2016-07-26 15:23:28 -07001525 obj = location_to_obj(m_page, obj);
1526
Minchan Kimc7806262015-04-15 16:15:26 -07001527 return obj;
1528}
1529
1530
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001531/**
1532 * zs_malloc - Allocate block of given size from pool.
1533 * @pool: pool to allocate from
1534 * @size: size of block to allocate
1535 *
1536 * On success, handle to the allocated object is returned,
1537 * otherwise 0.
1538 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1539 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001540unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001541{
Minchan Kim2e40e162015-04-15 16:15:23 -07001542 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001543 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001544 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001545 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001546
Minchan Kim7b60a682015-04-15 16:15:39 -07001547 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548 return 0;
1549
Minchan Kim37836892016-07-26 15:23:23 -07001550 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001551 if (!handle)
1552 return 0;
1553
1554 /* extra space in chunk to keep the handle */
1555 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001556 class = pool->size_class[get_size_class_index(size)];
1557
1558 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001559 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001560 if (likely(zspage)) {
1561 obj = obj_malloc(class, zspage, handle);
1562 /* Now move the zspage to another fullness group, if required */
1563 fix_fullness_group(class, zspage);
1564 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001565 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001566
Minchan Kim48b48002016-07-26 15:23:31 -07001567 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001568 }
1569
Minchan Kim48b48002016-07-26 15:23:31 -07001570 spin_unlock(&class->lock);
1571
1572 zspage = alloc_zspage(pool, class, gfp);
1573 if (!zspage) {
1574 cache_free_handle(pool, handle);
1575 return 0;
1576 }
1577
1578 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001579 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001580 newfg = get_fullness_group(class, zspage);
1581 insert_zspage(class, zspage, newfg);
1582 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001583 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001584 atomic_long_add(class->pages_per_zspage,
1585 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001586 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001587
1588 /* We completely set up zspage so mark them as movable */
1589 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001590 spin_unlock(&class->lock);
1591
Minchan Kim2e40e162015-04-15 16:15:23 -07001592 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001593}
1594EXPORT_SYMBOL_GPL(zs_malloc);
1595
Minchan Kim1ee47162016-05-20 16:59:45 -07001596static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001597{
1598 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001599 struct zspage *zspage;
1600 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001601 unsigned long f_offset;
1602 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001603 void *vaddr;
1604
Minchan Kim312fcae2015-04-15 16:15:30 -07001605 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001606 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001607 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001608 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001609
Minchan Kimc7806262015-04-15 16:15:26 -07001610 vaddr = kmap_atomic(f_page);
1611
1612 /* Insert this object in containing zspage's freelist */
1613 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001614 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001615 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001616 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001617 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001618 zs_stat_dec(class, OBJ_USED, 1);
1619}
1620
1621void zs_free(struct zs_pool *pool, unsigned long handle)
1622{
Minchan Kim37836892016-07-26 15:23:23 -07001623 struct zspage *zspage;
1624 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001625 unsigned long obj;
1626 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001627 int class_idx;
1628 struct size_class *class;
1629 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001630 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001631
Minchan Kim2e40e162015-04-15 16:15:23 -07001632 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001633 return;
1634
Minchan Kim312fcae2015-04-15 16:15:30 -07001635 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001636 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001637 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001638 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001639
Minchan Kim48b48002016-07-26 15:23:31 -07001640 migrate_read_lock(zspage);
1641
Minchan Kim37836892016-07-26 15:23:23 -07001642 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001643 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001644
1645 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001646 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001647 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001648 if (fullness != ZS_EMPTY) {
1649 migrate_read_unlock(zspage);
1650 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001651 }
Minchan Kim48b48002016-07-26 15:23:31 -07001652
1653 isolated = is_zspage_isolated(zspage);
1654 migrate_read_unlock(zspage);
1655 /* If zspage is isolated, zs_page_putback will free the zspage */
1656 if (likely(!isolated))
1657 free_zspage(pool, class, zspage);
1658out:
1659
Minchan Kim312fcae2015-04-15 16:15:30 -07001660 spin_unlock(&class->lock);
1661 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001662 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001663}
1664EXPORT_SYMBOL_GPL(zs_free);
1665
Minchan Kim251cbb92016-05-20 16:59:42 -07001666static void zs_object_copy(struct size_class *class, unsigned long dst,
1667 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001668{
1669 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001670 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001671 unsigned long s_off, d_off;
1672 void *s_addr, *d_addr;
1673 int s_size, d_size, size;
1674 int written = 0;
1675
1676 s_size = d_size = class->size;
1677
1678 obj_to_location(src, &s_page, &s_objidx);
1679 obj_to_location(dst, &d_page, &d_objidx);
1680
Minchan Kimbfd093f2016-07-26 15:23:28 -07001681 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1682 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001683
1684 if (s_off + class->size > PAGE_SIZE)
1685 s_size = PAGE_SIZE - s_off;
1686
1687 if (d_off + class->size > PAGE_SIZE)
1688 d_size = PAGE_SIZE - d_off;
1689
1690 s_addr = kmap_atomic(s_page);
1691 d_addr = kmap_atomic(d_page);
1692
1693 while (1) {
1694 size = min(s_size, d_size);
1695 memcpy(d_addr + d_off, s_addr + s_off, size);
1696 written += size;
1697
1698 if (written == class->size)
1699 break;
1700
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001701 s_off += size;
1702 s_size -= size;
1703 d_off += size;
1704 d_size -= size;
1705
1706 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001707 kunmap_atomic(d_addr);
1708 kunmap_atomic(s_addr);
1709 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001710 s_addr = kmap_atomic(s_page);
1711 d_addr = kmap_atomic(d_page);
1712 s_size = class->size - written;
1713 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001714 }
1715
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001716 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001717 kunmap_atomic(d_addr);
1718 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001719 d_addr = kmap_atomic(d_page);
1720 d_size = class->size - written;
1721 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001722 }
1723 }
1724
1725 kunmap_atomic(d_addr);
1726 kunmap_atomic(s_addr);
1727}
1728
1729/*
1730 * Find alloced object in zspage from index object and
1731 * return handle.
1732 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001733static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001734 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001735{
1736 unsigned long head;
1737 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001738 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001739 unsigned long handle = 0;
1740 void *addr = kmap_atomic(page);
1741
Minchan Kim37836892016-07-26 15:23:23 -07001742 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001743 offset += class->size * index;
1744
1745 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001746 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001747 if (head & OBJ_ALLOCATED_TAG) {
1748 handle = head & ~OBJ_ALLOCATED_TAG;
1749 if (trypin_tag(handle))
1750 break;
1751 handle = 0;
1752 }
1753
1754 offset += class->size;
1755 index++;
1756 }
1757
1758 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001759
1760 *obj_idx = index;
1761
Minchan Kim312fcae2015-04-15 16:15:30 -07001762 return handle;
1763}
1764
1765struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001766 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001767 struct page *s_page;
1768 /* Destination page for migration which should be a first page
1769 * of zspage. */
1770 struct page *d_page;
1771 /* Starting object index within @s_page which used for live object
1772 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001773 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001774};
1775
1776static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1777 struct zs_compact_control *cc)
1778{
1779 unsigned long used_obj, free_obj;
1780 unsigned long handle;
1781 struct page *s_page = cc->s_page;
1782 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001783 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001784 int ret = 0;
1785
1786 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001787 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001788 if (!handle) {
1789 s_page = get_next_page(s_page);
1790 if (!s_page)
1791 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001792 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001793 continue;
1794 }
1795
1796 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001797 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001798 unpin_tag(handle);
1799 ret = -ENOMEM;
1800 break;
1801 }
1802
1803 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001804 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001805 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001806 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001807 /*
1808 * record_obj updates handle's value to free_obj and it will
1809 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1810 * breaks synchronization using pin_tag(e,g, zs_free) so
1811 * let's keep the lock bit.
1812 */
1813 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001814 record_obj(handle, free_obj);
1815 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001816 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001817 }
1818
1819 /* Remember last position in this iteration */
1820 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001821 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001822
1823 return ret;
1824}
1825
Minchan Kim37836892016-07-26 15:23:23 -07001826static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001827{
1828 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001829 struct zspage *zspage;
1830 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001831
Minchan Kim37836892016-07-26 15:23:23 -07001832 if (!source) {
1833 fg[0] = ZS_ALMOST_FULL;
1834 fg[1] = ZS_ALMOST_EMPTY;
1835 }
1836
1837 for (i = 0; i < 2; i++) {
1838 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1839 struct zspage, list);
1840 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001841 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001842 remove_zspage(class, zspage, fg[i]);
1843 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001844 }
1845 }
1846
Minchan Kim37836892016-07-26 15:23:23 -07001847 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001848}
1849
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001850/*
Minchan Kim37836892016-07-26 15:23:23 -07001851 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001852 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001853 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001854 *
Minchan Kim37836892016-07-26 15:23:23 -07001855 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001856 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001857static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001858 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001859{
Minchan Kim312fcae2015-04-15 16:15:30 -07001860 enum fullness_group fullness;
1861
Minchan Kim48b48002016-07-26 15:23:31 -07001862 VM_BUG_ON(is_zspage_isolated(zspage));
1863
Minchan Kim37836892016-07-26 15:23:23 -07001864 fullness = get_fullness_group(class, zspage);
1865 insert_zspage(class, zspage, fullness);
1866 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001867
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001868 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001869}
1870
Minchan Kim48b48002016-07-26 15:23:31 -07001871#ifdef CONFIG_COMPACTION
1872static struct dentry *zs_mount(struct file_system_type *fs_type,
1873 int flags, const char *dev_name, void *data)
1874{
1875 static const struct dentry_operations ops = {
1876 .d_dname = simple_dname,
1877 };
1878
1879 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1880}
1881
1882static struct file_system_type zsmalloc_fs = {
1883 .name = "zsmalloc",
1884 .mount = zs_mount,
1885 .kill_sb = kill_anon_super,
1886};
1887
1888static int zsmalloc_mount(void)
1889{
1890 int ret = 0;
1891
1892 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1893 if (IS_ERR(zsmalloc_mnt))
1894 ret = PTR_ERR(zsmalloc_mnt);
1895
1896 return ret;
1897}
1898
1899static void zsmalloc_unmount(void)
1900{
1901 kern_unmount(zsmalloc_mnt);
1902}
1903
1904static void migrate_lock_init(struct zspage *zspage)
1905{
1906 rwlock_init(&zspage->lock);
1907}
1908
1909static void migrate_read_lock(struct zspage *zspage)
1910{
1911 read_lock(&zspage->lock);
1912}
1913
1914static void migrate_read_unlock(struct zspage *zspage)
1915{
1916 read_unlock(&zspage->lock);
1917}
1918
1919static void migrate_write_lock(struct zspage *zspage)
1920{
1921 write_lock(&zspage->lock);
1922}
1923
1924static void migrate_write_unlock(struct zspage *zspage)
1925{
1926 write_unlock(&zspage->lock);
1927}
1928
1929/* Number of isolated subpage for *page migration* in this zspage */
1930static void inc_zspage_isolation(struct zspage *zspage)
1931{
1932 zspage->isolated++;
1933}
1934
1935static void dec_zspage_isolation(struct zspage *zspage)
1936{
1937 zspage->isolated--;
1938}
1939
1940static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1941 struct page *newpage, struct page *oldpage)
1942{
1943 struct page *page;
1944 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1945 int idx = 0;
1946
1947 page = get_first_page(zspage);
1948 do {
1949 if (page == oldpage)
1950 pages[idx] = newpage;
1951 else
1952 pages[idx] = page;
1953 idx++;
1954 } while ((page = get_next_page(page)) != NULL);
1955
1956 create_page_chain(class, zspage, pages);
1957 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1958 if (unlikely(PageHugeObject(oldpage)))
1959 newpage->index = oldpage->index;
1960 __SetPageMovable(newpage, page_mapping(oldpage));
1961}
1962
1963bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1964{
1965 struct zs_pool *pool;
1966 struct size_class *class;
1967 int class_idx;
1968 enum fullness_group fullness;
1969 struct zspage *zspage;
1970 struct address_space *mapping;
1971
1972 /*
1973 * Page is locked so zspage couldn't be destroyed. For detail, look at
1974 * lock_zspage in free_zspage.
1975 */
1976 VM_BUG_ON_PAGE(!PageMovable(page), page);
1977 VM_BUG_ON_PAGE(PageIsolated(page), page);
1978
1979 zspage = get_zspage(page);
1980
1981 /*
1982 * Without class lock, fullness could be stale while class_idx is okay
1983 * because class_idx is constant unless page is freed so we should get
1984 * fullness again under class lock.
1985 */
1986 get_zspage_mapping(zspage, &class_idx, &fullness);
1987 mapping = page_mapping(page);
1988 pool = mapping->private_data;
1989 class = pool->size_class[class_idx];
1990
1991 spin_lock(&class->lock);
1992 if (get_zspage_inuse(zspage) == 0) {
1993 spin_unlock(&class->lock);
1994 return false;
1995 }
1996
1997 /* zspage is isolated for object migration */
1998 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1999 spin_unlock(&class->lock);
2000 return false;
2001 }
2002
2003 /*
2004 * If this is first time isolation for the zspage, isolate zspage from
2005 * size_class to prevent further object allocation from the zspage.
2006 */
2007 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2008 get_zspage_mapping(zspage, &class_idx, &fullness);
2009 remove_zspage(class, zspage, fullness);
2010 }
2011
2012 inc_zspage_isolation(zspage);
2013 spin_unlock(&class->lock);
2014
2015 return true;
2016}
2017
2018int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2019 struct page *page, enum migrate_mode mode)
2020{
2021 struct zs_pool *pool;
2022 struct size_class *class;
2023 int class_idx;
2024 enum fullness_group fullness;
2025 struct zspage *zspage;
2026 struct page *dummy;
2027 void *s_addr, *d_addr, *addr;
2028 int offset, pos;
2029 unsigned long handle, head;
2030 unsigned long old_obj, new_obj;
2031 unsigned int obj_idx;
2032 int ret = -EAGAIN;
2033
2034 VM_BUG_ON_PAGE(!PageMovable(page), page);
2035 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2036
2037 zspage = get_zspage(page);
2038
2039 /* Concurrent compactor cannot migrate any subpage in zspage */
2040 migrate_write_lock(zspage);
2041 get_zspage_mapping(zspage, &class_idx, &fullness);
2042 pool = mapping->private_data;
2043 class = pool->size_class[class_idx];
2044 offset = get_first_obj_offset(page);
2045
2046 spin_lock(&class->lock);
2047 if (!get_zspage_inuse(zspage)) {
2048 ret = -EBUSY;
2049 goto unlock_class;
2050 }
2051
2052 pos = offset;
2053 s_addr = kmap_atomic(page);
2054 while (pos < PAGE_SIZE) {
2055 head = obj_to_head(page, s_addr + pos);
2056 if (head & OBJ_ALLOCATED_TAG) {
2057 handle = head & ~OBJ_ALLOCATED_TAG;
2058 if (!trypin_tag(handle))
2059 goto unpin_objects;
2060 }
2061 pos += class->size;
2062 }
2063
2064 /*
2065 * Here, any user cannot access all objects in the zspage so let's move.
2066 */
2067 d_addr = kmap_atomic(newpage);
2068 memcpy(d_addr, s_addr, PAGE_SIZE);
2069 kunmap_atomic(d_addr);
2070
2071 for (addr = s_addr + offset; addr < s_addr + pos;
2072 addr += class->size) {
2073 head = obj_to_head(page, addr);
2074 if (head & OBJ_ALLOCATED_TAG) {
2075 handle = head & ~OBJ_ALLOCATED_TAG;
2076 if (!testpin_tag(handle))
2077 BUG();
2078
2079 old_obj = handle_to_obj(handle);
2080 obj_to_location(old_obj, &dummy, &obj_idx);
2081 new_obj = (unsigned long)location_to_obj(newpage,
2082 obj_idx);
2083 new_obj |= BIT(HANDLE_PIN_BIT);
2084 record_obj(handle, new_obj);
2085 }
2086 }
2087
2088 replace_sub_page(class, zspage, newpage, page);
2089 get_page(newpage);
2090
2091 dec_zspage_isolation(zspage);
2092
2093 /*
2094 * Page migration is done so let's putback isolated zspage to
2095 * the list if @page is final isolated subpage in the zspage.
2096 */
2097 if (!is_zspage_isolated(zspage))
2098 putback_zspage(class, zspage);
2099
2100 reset_page(page);
2101 put_page(page);
2102 page = newpage;
2103
Minchan Kimdd4123f2016-07-26 15:26:50 -07002104 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002105unpin_objects:
2106 for (addr = s_addr + offset; addr < s_addr + pos;
2107 addr += class->size) {
2108 head = obj_to_head(page, addr);
2109 if (head & OBJ_ALLOCATED_TAG) {
2110 handle = head & ~OBJ_ALLOCATED_TAG;
2111 if (!testpin_tag(handle))
2112 BUG();
2113 unpin_tag(handle);
2114 }
2115 }
2116 kunmap_atomic(s_addr);
2117unlock_class:
2118 spin_unlock(&class->lock);
2119 migrate_write_unlock(zspage);
2120
2121 return ret;
2122}
2123
2124void zs_page_putback(struct page *page)
2125{
2126 struct zs_pool *pool;
2127 struct size_class *class;
2128 int class_idx;
2129 enum fullness_group fg;
2130 struct address_space *mapping;
2131 struct zspage *zspage;
2132
2133 VM_BUG_ON_PAGE(!PageMovable(page), page);
2134 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2135
2136 zspage = get_zspage(page);
2137 get_zspage_mapping(zspage, &class_idx, &fg);
2138 mapping = page_mapping(page);
2139 pool = mapping->private_data;
2140 class = pool->size_class[class_idx];
2141
2142 spin_lock(&class->lock);
2143 dec_zspage_isolation(zspage);
2144 if (!is_zspage_isolated(zspage)) {
2145 fg = putback_zspage(class, zspage);
2146 /*
2147 * Due to page_lock, we cannot free zspage immediately
2148 * so let's defer.
2149 */
2150 if (fg == ZS_EMPTY)
2151 schedule_work(&pool->free_work);
2152 }
2153 spin_unlock(&class->lock);
2154}
2155
2156const struct address_space_operations zsmalloc_aops = {
2157 .isolate_page = zs_page_isolate,
2158 .migratepage = zs_page_migrate,
2159 .putback_page = zs_page_putback,
2160};
2161
2162static int zs_register_migration(struct zs_pool *pool)
2163{
2164 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2165 if (IS_ERR(pool->inode)) {
2166 pool->inode = NULL;
2167 return 1;
2168 }
2169
2170 pool->inode->i_mapping->private_data = pool;
2171 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2172 return 0;
2173}
2174
2175static void zs_unregister_migration(struct zs_pool *pool)
2176{
2177 flush_work(&pool->free_work);
2178 if (pool->inode)
2179 iput(pool->inode);
2180}
2181
2182/*
2183 * Caller should hold page_lock of all pages in the zspage
2184 * In here, we cannot use zspage meta data.
2185 */
2186static void async_free_zspage(struct work_struct *work)
2187{
2188 int i;
2189 struct size_class *class;
2190 unsigned int class_idx;
2191 enum fullness_group fullness;
2192 struct zspage *zspage, *tmp;
2193 LIST_HEAD(free_pages);
2194 struct zs_pool *pool = container_of(work, struct zs_pool,
2195 free_work);
2196
2197 for (i = 0; i < zs_size_classes; i++) {
2198 class = pool->size_class[i];
2199 if (class->index != i)
2200 continue;
2201
2202 spin_lock(&class->lock);
2203 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2204 spin_unlock(&class->lock);
2205 }
2206
2207
2208 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2209 list_del(&zspage->list);
2210 lock_zspage(zspage);
2211
2212 get_zspage_mapping(zspage, &class_idx, &fullness);
2213 VM_BUG_ON(fullness != ZS_EMPTY);
2214 class = pool->size_class[class_idx];
2215 spin_lock(&class->lock);
2216 __free_zspage(pool, pool->size_class[class_idx], zspage);
2217 spin_unlock(&class->lock);
2218 }
2219};
2220
2221static void kick_deferred_free(struct zs_pool *pool)
2222{
2223 schedule_work(&pool->free_work);
2224}
2225
2226static void init_deferred_free(struct zs_pool *pool)
2227{
2228 INIT_WORK(&pool->free_work, async_free_zspage);
2229}
2230
2231static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2232{
2233 struct page *page = get_first_page(zspage);
2234
2235 do {
2236 WARN_ON(!trylock_page(page));
2237 __SetPageMovable(page, pool->inode->i_mapping);
2238 unlock_page(page);
2239 } while ((page = get_next_page(page)) != NULL);
2240}
2241#endif
2242
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002243/*
2244 *
2245 * Based on the number of unused allocated objects calculate
2246 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002247 */
2248static unsigned long zs_can_compact(struct size_class *class)
2249{
2250 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002251 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2252 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002253
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002254 if (obj_allocated <= obj_used)
2255 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002256
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002257 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002258 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002259
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002260 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002261}
2262
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002263static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002264{
Minchan Kim312fcae2015-04-15 16:15:30 -07002265 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002266 struct zspage *src_zspage;
2267 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002268
Minchan Kim312fcae2015-04-15 16:15:30 -07002269 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002270 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002271
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002272 if (!zs_can_compact(class))
2273 break;
2274
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002275 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002276 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002277
Minchan Kim37836892016-07-26 15:23:23 -07002278 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002279 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002280 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002281 * If there is no more space in dst_page, resched
2282 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002283 */
2284 if (!migrate_zspage(pool, class, &cc))
2285 break;
2286
Minchan Kim4aa409c2016-07-26 15:23:26 -07002287 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002288 }
2289
2290 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002291 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002292 break;
2293
Minchan Kim4aa409c2016-07-26 15:23:26 -07002294 putback_zspage(class, dst_zspage);
2295 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002296 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002297 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002298 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002299 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002300 cond_resched();
2301 spin_lock(&class->lock);
2302 }
2303
Minchan Kim37836892016-07-26 15:23:23 -07002304 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002305 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002306
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002307 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002308}
2309
2310unsigned long zs_compact(struct zs_pool *pool)
2311{
2312 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002313 struct size_class *class;
2314
2315 for (i = zs_size_classes - 1; i >= 0; i--) {
2316 class = pool->size_class[i];
2317 if (!class)
2318 continue;
2319 if (class->index != i)
2320 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002321 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002322 }
2323
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002324 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002325}
2326EXPORT_SYMBOL_GPL(zs_compact);
2327
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002328void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2329{
2330 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2331}
2332EXPORT_SYMBOL_GPL(zs_pool_stats);
2333
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002334static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2335 struct shrink_control *sc)
2336{
2337 unsigned long pages_freed;
2338 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2339 shrinker);
2340
2341 pages_freed = pool->stats.pages_compacted;
2342 /*
2343 * Compact classes and calculate compaction delta.
2344 * Can run concurrently with a manually triggered
2345 * (by user) compaction.
2346 */
2347 pages_freed = zs_compact(pool) - pages_freed;
2348
2349 return pages_freed ? pages_freed : SHRINK_STOP;
2350}
2351
2352static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2353 struct shrink_control *sc)
2354{
2355 int i;
2356 struct size_class *class;
2357 unsigned long pages_to_free = 0;
2358 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2359 shrinker);
2360
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002361 for (i = zs_size_classes - 1; i >= 0; i--) {
2362 class = pool->size_class[i];
2363 if (!class)
2364 continue;
2365 if (class->index != i)
2366 continue;
2367
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002368 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002369 }
2370
2371 return pages_to_free;
2372}
2373
2374static void zs_unregister_shrinker(struct zs_pool *pool)
2375{
2376 if (pool->shrinker_enabled) {
2377 unregister_shrinker(&pool->shrinker);
2378 pool->shrinker_enabled = false;
2379 }
2380}
2381
2382static int zs_register_shrinker(struct zs_pool *pool)
2383{
2384 pool->shrinker.scan_objects = zs_shrinker_scan;
2385 pool->shrinker.count_objects = zs_shrinker_count;
2386 pool->shrinker.batch = 0;
2387 pool->shrinker.seeks = DEFAULT_SEEKS;
2388
2389 return register_shrinker(&pool->shrinker);
2390}
2391
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002392/**
2393 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -06002394 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002395 *
2396 * This function must be called before anything when using
2397 * the zsmalloc allocator.
2398 *
2399 * On success, a pointer to the newly created pool is returned,
2400 * otherwise NULL.
2401 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002402struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002403{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002404 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002405 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002406 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002407
Ganesh Mahendran18136652014-12-12 16:57:10 -08002408 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002409 if (!pool)
2410 return NULL;
2411
Minchan Kim48b48002016-07-26 15:23:31 -07002412 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002413 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2414 GFP_KERNEL);
2415 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002416 kfree(pool);
2417 return NULL;
2418 }
2419
Minchan Kim2e40e162015-04-15 16:15:23 -07002420 pool->name = kstrdup(name, GFP_KERNEL);
2421 if (!pool->name)
2422 goto err;
2423
Minchan Kim37836892016-07-26 15:23:23 -07002424 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002425 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002426
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002427 /*
2428 * Iterate reversly, because, size of size_class that we want to use
2429 * for merging should be larger or equal to current size.
2430 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002431 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002432 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002433 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002434 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002435 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002436 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002437
2438 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2439 if (size > ZS_MAX_ALLOC_SIZE)
2440 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002441 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002442 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002443
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002444 /*
2445 * size_class is used for normal zsmalloc operation such
2446 * as alloc/free for that size. Although it is natural that we
2447 * have one size_class for each size, there is a chance that we
2448 * can get more memory utilization if we use one size_class for
2449 * many different sizes whose size_class have same
2450 * characteristics. So, we makes size_class point to
2451 * previous size_class if possible.
2452 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002453 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002454 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002455 pool->size_class[i] = prev_class;
2456 continue;
2457 }
2458 }
2459
2460 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2461 if (!class)
2462 goto err;
2463
Nitin Gupta61989a82012-01-09 16:51:56 -06002464 class->size = size;
2465 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002466 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002467 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002468 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002469 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002470 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2471 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002472 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002473
2474 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002475 }
2476
Dan Streetmand34f6152016-05-20 16:59:56 -07002477 /* debug only, don't abort if it fails */
2478 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002479
Minchan Kim48b48002016-07-26 15:23:31 -07002480 if (zs_register_migration(pool))
2481 goto err;
2482
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002483 /*
2484 * Not critical, we still can use the pool
2485 * and user can trigger compaction manually.
2486 */
2487 if (zs_register_shrinker(pool) == 0)
2488 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002489 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002490
2491err:
2492 zs_destroy_pool(pool);
2493 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002494}
2495EXPORT_SYMBOL_GPL(zs_create_pool);
2496
2497void zs_destroy_pool(struct zs_pool *pool)
2498{
2499 int i;
2500
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002501 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002502 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002503 zs_pool_stat_destroy(pool);
2504
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002505 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002506 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002507 struct size_class *class = pool->size_class[i];
2508
2509 if (!class)
2510 continue;
2511
2512 if (class->index != i)
2513 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002514
Minchan Kim48b48002016-07-26 15:23:31 -07002515 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002516 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002517 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002518 class->size, fg);
2519 }
2520 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002521 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002522 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002523
Minchan Kim37836892016-07-26 15:23:23 -07002524 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002525 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002526 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002527 kfree(pool);
2528}
2529EXPORT_SYMBOL_GPL(zs_destroy_pool);
2530
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002531static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002532{
Minchan Kim48b48002016-07-26 15:23:31 -07002533 int ret;
2534
2535 ret = zsmalloc_mount();
2536 if (ret)
2537 goto out;
2538
2539 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002540
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002541 if (ret)
2542 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002543
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002544 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002545
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002546#ifdef CONFIG_ZPOOL
2547 zpool_register_driver(&zs_zpool_driver);
2548#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002549
Dan Streetman4abaac92016-05-26 15:16:27 -07002550 zs_stat_init();
2551
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002552 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002553
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002554notifier_fail:
2555 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002556 zsmalloc_unmount();
2557out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002558 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002559}
Nitin Gupta61989a82012-01-09 16:51:56 -06002560
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002561static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002562{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002563#ifdef CONFIG_ZPOOL
2564 zpool_unregister_driver(&zs_zpool_driver);
2565#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002566 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002567 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002568
2569 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002570}
Ben Hutchings069f1012012-06-20 02:31:11 +01002571
2572module_init(zs_init);
2573module_exit(zs_exit);
2574
2575MODULE_LICENSE("Dual BSD/GPL");
2576MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");