blob: 6382bdae949d1dffb8307b871a4f995e2af6ff86 [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
Minchan Kim48b48002016-07-26 15:23:31 -070028 * PG_owner_priv_1: indentifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070029 *
30 */
31
Dan Streetman4abaac92016-05-26 15:16:27 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Nitin Gupta61989a82012-01-09 16:51:56 -060034#include <linux/module.h>
35#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070036#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/string.h>
41#include <linux/slab.h>
42#include <asm/tlbflush.h>
43#include <asm/pgtable.h>
44#include <linux/cpumask.h>
45#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060046#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080047#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090048#include <linux/spinlock.h>
49#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080050#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080051#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070052#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070053#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070054#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070055#include <linux/pagemap.h>
56
57#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090058
59/*
60 * This must be power of 2 and greater than of equal to sizeof(link_free).
61 * These two conditions ensure that any 'struct link_free' itself doesn't
62 * span more than 1 page which avoids complex case of mapping 2 pages simply
63 * to restore link_free pointer values.
64 */
65#define ZS_ALIGN 8
66
67/*
68 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
69 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
70 */
71#define ZS_MAX_ZSPAGE_ORDER 2
72#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
73
Minchan Kim2e40e162015-04-15 16:15:23 -070074#define ZS_HANDLE_SIZE (sizeof(unsigned long))
75
Seth Jennings0959c632012-08-08 15:12:17 +090076/*
77 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090078 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090079 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070080 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090081 *
82 * This is made more complicated by various memory models and PAE.
83 */
84
85#ifndef MAX_PHYSMEM_BITS
86#ifdef CONFIG_HIGHMEM64G
87#define MAX_PHYSMEM_BITS 36
88#else /* !CONFIG_HIGHMEM64G */
89/*
90 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
91 * be PAGE_SHIFT
92 */
93#define MAX_PHYSMEM_BITS BITS_PER_LONG
94#endif
95#endif
96#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070097
98/*
99 * Memory for allocating for handle keeps object position by
100 * encoding <page, obj_idx> and the encoded value has a room
101 * in least bit(ie, look at obj_to_location).
102 * We use the bit to synchronize between object access by
103 * user and migration.
104 */
105#define HANDLE_PIN_BIT 0
106
107/*
108 * Head in allocated object should have OBJ_ALLOCATED_TAG
109 * to identify the object was allocated or not.
110 * It's okay to add the status bit in the least bit because
111 * header keeps handle which is 4byte-aligned address so we
112 * have room for two bit at least.
113 */
114#define OBJ_ALLOCATED_TAG 1
115#define OBJ_TAG_BITS 1
116#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900117#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
118
119#define MAX(a, b) ((a) >= (b) ? (a) : (b))
120/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
121#define ZS_MIN_ALLOC_SIZE \
122 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700123/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700124#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900125
126/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700127 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900128 * trader-off here:
129 * - Large number of size classes is potentially wasteful as free page are
130 * spread across these classes
131 * - Small number of size classes causes large internal fragmentation
132 * - Probably its better to use specific size classes (empirically
133 * determined). NOTE: all those class sizes must be set as multiple of
134 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
135 *
136 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
137 * (reason above)
138 */
Minchan Kim37836892016-07-26 15:23:23 -0700139#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900140
Seth Jennings0959c632012-08-08 15:12:17 +0900141enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900142 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700143 ZS_ALMOST_EMPTY,
144 ZS_ALMOST_FULL,
145 ZS_FULL,
146 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900147};
148
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800149enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700150 CLASS_EMPTY,
151 CLASS_ALMOST_EMPTY,
152 CLASS_ALMOST_FULL,
153 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800154 OBJ_ALLOCATED,
155 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700156 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800157};
158
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800159struct zs_size_stat {
160 unsigned long objs[NR_ZS_STAT_TYPE];
161};
162
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700163#ifdef CONFIG_ZSMALLOC_STAT
164static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800165#endif
166
Minchan Kim48b48002016-07-26 15:23:31 -0700167#ifdef CONFIG_COMPACTION
168static struct vfsmount *zsmalloc_mnt;
169#endif
170
Seth Jennings0959c632012-08-08 15:12:17 +0900171/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800172 * number of size_classes
173 */
174static int zs_size_classes;
175
176/*
Seth Jennings0959c632012-08-08 15:12:17 +0900177 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
178 * n <= N / f, where
179 * n = number of allocated objects
180 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700181 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900182 *
183 * Similarly, we assign zspage to:
184 * ZS_ALMOST_FULL when n > N / f
185 * ZS_EMPTY when n == 0
186 * ZS_FULL when n == N
187 *
188 * (see: fix_fullness_group())
189 */
190static const int fullness_threshold_frac = 4;
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -0700191static size_t huge_class_size;
Seth Jennings0959c632012-08-08 15:12:17 +0900192
193struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700194 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700195 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900196 /*
197 * Size of objects stored in this class. Must be multiple
198 * of ZS_ALIGN.
199 */
200 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700201 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800202 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
203 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700204
205 unsigned int index;
206 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900207};
208
Minchan Kim48b48002016-07-26 15:23:31 -0700209/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
210static void SetPageHugeObject(struct page *page)
211{
212 SetPageOwnerPriv1(page);
213}
214
215static void ClearPageHugeObject(struct page *page)
216{
217 ClearPageOwnerPriv1(page);
218}
219
220static int PageHugeObject(struct page *page)
221{
222 return PageOwnerPriv1(page);
223}
224
Seth Jennings0959c632012-08-08 15:12:17 +0900225/*
226 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700227 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900228 *
229 * This must be power of 2 and less than or equal to ZS_ALIGN
230 */
231struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700232 union {
233 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700234 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700235 * It's valid for non-allocated object
236 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700237 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700238 /*
239 * Handle of allocated object.
240 */
241 unsigned long handle;
242 };
Seth Jennings0959c632012-08-08 15:12:17 +0900243};
244
245struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800246 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800247
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800248 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700249 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700250 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900251
Minchan Kim13de8932014-10-09 15:29:48 -0700252 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800253
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700254 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700255
256 /* Compact classes */
257 struct shrinker shrinker;
258 /*
259 * To signify that register_shrinker() was successful
260 * and unregister_shrinker() will not Oops.
261 */
262 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800263#ifdef CONFIG_ZSMALLOC_STAT
264 struct dentry *stat_dentry;
265#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700266#ifdef CONFIG_COMPACTION
267 struct inode *inode;
268 struct work_struct free_work;
269#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900270};
Nitin Gupta61989a82012-01-09 16:51:56 -0600271
272/*
273 * A zspage's class index and fullness group
274 * are encoded in its (first)page->mapping
275 */
Minchan Kim37836892016-07-26 15:23:23 -0700276#define FULLNESS_BITS 2
277#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700278#define ISOLATED_BITS 3
279#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700280
Minchan Kim37836892016-07-26 15:23:23 -0700281struct zspage {
282 struct {
283 unsigned int fullness:FULLNESS_BITS;
Minchan Kimd19f7452017-04-13 14:56:40 -0700284 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700285 unsigned int isolated:ISOLATED_BITS;
286 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700287 };
288 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700289 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700290 struct page *first_page;
291 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700292#ifdef CONFIG_COMPACTION
293 rwlock_t lock;
294#endif
Minchan Kim37836892016-07-26 15:23:23 -0700295};
Nitin Gupta61989a82012-01-09 16:51:56 -0600296
Seth Jenningsf5536462012-07-18 11:55:56 -0500297struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900298#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500299 struct vm_struct *vm; /* vm area for mapping object that span pages */
300#else
301 char *vm_buf; /* copy buffer for objects that span pages */
302#endif
303 char *vm_addr; /* address of kmap_atomic()'ed pages */
304 enum zs_mapmode vm_mm; /* mapping mode */
305};
306
Minchan Kim48b48002016-07-26 15:23:31 -0700307#ifdef CONFIG_COMPACTION
308static int zs_register_migration(struct zs_pool *pool);
309static void zs_unregister_migration(struct zs_pool *pool);
310static void migrate_lock_init(struct zspage *zspage);
311static void migrate_read_lock(struct zspage *zspage);
312static void migrate_read_unlock(struct zspage *zspage);
313static void kick_deferred_free(struct zs_pool *pool);
314static void init_deferred_free(struct zs_pool *pool);
315static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
316#else
317static int zsmalloc_mount(void) { return 0; }
318static void zsmalloc_unmount(void) {}
319static int zs_register_migration(struct zs_pool *pool) { return 0; }
320static void zs_unregister_migration(struct zs_pool *pool) {}
321static void migrate_lock_init(struct zspage *zspage) {}
322static void migrate_read_lock(struct zspage *zspage) {}
323static void migrate_read_unlock(struct zspage *zspage) {}
324static void kick_deferred_free(struct zs_pool *pool) {}
325static void init_deferred_free(struct zs_pool *pool) {}
326static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
327#endif
328
Minchan Kim37836892016-07-26 15:23:23 -0700329static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700330{
331 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
332 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700333 if (!pool->handle_cachep)
334 return 1;
335
336 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
337 0, 0, NULL);
338 if (!pool->zspage_cachep) {
339 kmem_cache_destroy(pool->handle_cachep);
340 pool->handle_cachep = NULL;
341 return 1;
342 }
343
344 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700345}
346
Minchan Kim37836892016-07-26 15:23:23 -0700347static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700348{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700349 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700350 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700351}
352
Minchan Kim37836892016-07-26 15:23:23 -0700353static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700354{
355 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700356 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700357}
358
Minchan Kim37836892016-07-26 15:23:23 -0700359static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700360{
361 kmem_cache_free(pool->handle_cachep, (void *)handle);
362}
363
Minchan Kim37836892016-07-26 15:23:23 -0700364static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
365{
Minchan Kim48b48002016-07-26 15:23:31 -0700366 return kmem_cache_alloc(pool->zspage_cachep,
367 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim37836892016-07-26 15:23:23 -0700368};
369
370static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
371{
372 kmem_cache_free(pool->zspage_cachep, zspage);
373}
374
Minchan Kim2e40e162015-04-15 16:15:23 -0700375static void record_obj(unsigned long handle, unsigned long obj)
376{
Junil Leec102f072016-01-20 14:58:18 -0800377 /*
378 * lsb of @obj represents handle lock while other bits
379 * represent object value the handle is pointing so
380 * updating shouldn't do store tearing.
381 */
382 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700383}
384
Dan Streetmanc7957792014-08-06 16:08:38 -0700385/* zpool driver */
386
387#ifdef CONFIG_ZPOOL
388
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800389static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700390 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700391 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700392{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700393 /*
394 * Ignore global gfp flags: zs_malloc() may be invoked from
395 * different contexts and its caller must provide a valid
396 * gfp mask.
397 */
398 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700399}
400
401static void zs_zpool_destroy(void *pool)
402{
403 zs_destroy_pool(pool);
404}
405
406static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
407 unsigned long *handle)
408{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700409 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700410 return *handle ? 0 : -1;
411}
412static void zs_zpool_free(void *pool, unsigned long handle)
413{
414 zs_free(pool, handle);
415}
416
417static int zs_zpool_shrink(void *pool, unsigned int pages,
418 unsigned int *reclaimed)
419{
420 return -EINVAL;
421}
422
423static void *zs_zpool_map(void *pool, unsigned long handle,
424 enum zpool_mapmode mm)
425{
426 enum zs_mapmode zs_mm;
427
428 switch (mm) {
429 case ZPOOL_MM_RO:
430 zs_mm = ZS_MM_RO;
431 break;
432 case ZPOOL_MM_WO:
433 zs_mm = ZS_MM_WO;
434 break;
435 case ZPOOL_MM_RW: /* fallthru */
436 default:
437 zs_mm = ZS_MM_RW;
438 break;
439 }
440
441 return zs_map_object(pool, handle, zs_mm);
442}
443static void zs_zpool_unmap(void *pool, unsigned long handle)
444{
445 zs_unmap_object(pool, handle);
446}
447
448static u64 zs_zpool_total_size(void *pool)
449{
Minchan Kim722cdc12014-10-09 15:29:50 -0700450 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700451}
452
453static struct zpool_driver zs_zpool_driver = {
454 .type = "zsmalloc",
455 .owner = THIS_MODULE,
456 .create = zs_zpool_create,
457 .destroy = zs_zpool_destroy,
458 .malloc = zs_zpool_malloc,
459 .free = zs_zpool_free,
460 .shrink = zs_zpool_shrink,
461 .map = zs_zpool_map,
462 .unmap = zs_zpool_unmap,
463 .total_size = zs_zpool_total_size,
464};
465
Kees Cook137f8cf2014-08-29 15:18:40 -0700466MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700467#endif /* CONFIG_ZPOOL */
468
Nitin Gupta61989a82012-01-09 16:51:56 -0600469/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
470static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
471
Minchan Kim48b48002016-07-26 15:23:31 -0700472static bool is_zspage_isolated(struct zspage *zspage)
473{
474 return zspage->isolated;
475}
476
Nitin Gupta61989a82012-01-09 16:51:56 -0600477static int is_first_page(struct page *page)
478{
Minchan Kima27545bf2012-04-25 15:23:09 +0900479 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600480}
481
Minchan Kim48b48002016-07-26 15:23:31 -0700482/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700483static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600484{
Minchan Kim37836892016-07-26 15:23:23 -0700485 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600486}
487
Minchan Kim37836892016-07-26 15:23:23 -0700488static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700489{
Minchan Kim37836892016-07-26 15:23:23 -0700490 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700491}
492
Minchan Kim37836892016-07-26 15:23:23 -0700493static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700494{
Minchan Kim37836892016-07-26 15:23:23 -0700495 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700496}
497
Minchan Kim48b48002016-07-26 15:23:31 -0700498static inline struct page *get_first_page(struct zspage *zspage)
499{
500 struct page *first_page = zspage->first_page;
501
502 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
503 return first_page;
504}
505
Minchan Kim4f420472016-07-26 15:23:17 -0700506static inline int get_first_obj_offset(struct page *page)
507{
Minchan Kim48b48002016-07-26 15:23:31 -0700508 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700509}
510
511static inline void set_first_obj_offset(struct page *page, int offset)
512{
Minchan Kim48b48002016-07-26 15:23:31 -0700513 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700514}
515
Minchan Kimbfd093f2016-07-26 15:23:28 -0700516static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700517{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700518 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700519}
520
Minchan Kimbfd093f2016-07-26 15:23:28 -0700521static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700522{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700523 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700524}
525
Minchan Kim37836892016-07-26 15:23:23 -0700526static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700527 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600528 enum fullness_group *fullness)
529{
Minchan Kim48b48002016-07-26 15:23:31 -0700530 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
531
Minchan Kim37836892016-07-26 15:23:23 -0700532 *fullness = zspage->fullness;
533 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600534}
535
Minchan Kim37836892016-07-26 15:23:23 -0700536static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700537 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600538 enum fullness_group fullness)
539{
Minchan Kim37836892016-07-26 15:23:23 -0700540 zspage->class = class_idx;
541 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600542}
543
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900544/*
545 * zsmalloc divides the pool into various size classes where each
546 * class maintains a list of zspages where each zspage is divided
547 * into equal sized chunks. Each allocation falls into one of these
548 * classes depending on its size. This function returns index of the
549 * size class which has chunk size big enough to hold the give size.
550 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600551static int get_size_class_index(int size)
552{
553 int idx = 0;
554
555 if (likely(size > ZS_MIN_ALLOC_SIZE))
556 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
557 ZS_SIZE_CLASS_DELTA);
558
Minchan Kim7b60a682015-04-15 16:15:39 -0700559 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600560}
561
Minchan Kim248ca1b2015-04-15 16:15:42 -0700562static inline void zs_stat_inc(struct size_class *class,
563 enum zs_stat_type type, unsigned long cnt)
564{
Minchan Kim48b48002016-07-26 15:23:31 -0700565 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700566}
567
568static inline void zs_stat_dec(struct size_class *class,
569 enum zs_stat_type type, unsigned long cnt)
570{
Minchan Kim48b48002016-07-26 15:23:31 -0700571 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700572}
573
574static inline unsigned long zs_stat_get(struct size_class *class,
575 enum zs_stat_type type)
576{
Minchan Kim48b48002016-07-26 15:23:31 -0700577 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700578}
579
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700580#ifdef CONFIG_ZSMALLOC_STAT
581
Dan Streetman4abaac92016-05-26 15:16:27 -0700582static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700583{
Dan Streetman4abaac92016-05-26 15:16:27 -0700584 if (!debugfs_initialized()) {
585 pr_warn("debugfs not available, stat dir not created\n");
586 return;
587 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700588
589 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
590 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700591 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700592}
593
594static void __exit zs_stat_exit(void)
595{
596 debugfs_remove_recursive(zs_stat_root);
597}
598
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700599static unsigned long zs_can_compact(struct size_class *class);
600
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601static int zs_stats_size_show(struct seq_file *s, void *v)
602{
603 int i;
604 struct zs_pool *pool = s->private;
605 struct size_class *class;
606 int objs_per_zspage;
607 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700608 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700609 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
610 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700611 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700612
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700613 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700614 "class", "size", "almost_full", "almost_empty",
615 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700616 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700617
618 for (i = 0; i < zs_size_classes; i++) {
619 class = pool->size_class[i];
620
621 if (class->index != i)
622 continue;
623
624 spin_lock(&class->lock);
625 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
626 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
627 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
628 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700629 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700630 spin_unlock(&class->lock);
631
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700632 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700633 pages_used = obj_allocated / objs_per_zspage *
634 class->pages_per_zspage;
635
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700636 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
637 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700638 i, class->size, class_almost_full, class_almost_empty,
639 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700640 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700641
642 total_class_almost_full += class_almost_full;
643 total_class_almost_empty += class_almost_empty;
644 total_objs += obj_allocated;
645 total_used_objs += obj_used;
646 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700647 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648 }
649
650 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700651 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700652 "Total", "", total_class_almost_full,
653 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700654 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700655
656 return 0;
657}
658
659static int zs_stats_size_open(struct inode *inode, struct file *file)
660{
661 return single_open(file, zs_stats_size_show, inode->i_private);
662}
663
664static const struct file_operations zs_stat_size_ops = {
665 .open = zs_stats_size_open,
666 .read = seq_read,
667 .llseek = seq_lseek,
668 .release = single_release,
669};
670
Dan Streetmand34f6152016-05-20 16:59:56 -0700671static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700672{
673 struct dentry *entry;
674
Dan Streetman4abaac92016-05-26 15:16:27 -0700675 if (!zs_stat_root) {
676 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700677 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700678 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700679
680 entry = debugfs_create_dir(name, zs_stat_root);
681 if (!entry) {
682 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700683 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700684 }
685 pool->stat_dentry = entry;
686
687 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
688 pool->stat_dentry, pool, &zs_stat_size_ops);
689 if (!entry) {
690 pr_warn("%s: debugfs file entry <%s> creation failed\n",
691 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700692 debugfs_remove_recursive(pool->stat_dentry);
693 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700694 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700695}
696
697static void zs_pool_stat_destroy(struct zs_pool *pool)
698{
699 debugfs_remove_recursive(pool->stat_dentry);
700}
701
702#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700703static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700704{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700705}
706
707static void __exit zs_stat_exit(void)
708{
709}
710
Dan Streetmand34f6152016-05-20 16:59:56 -0700711static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700712{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700713}
714
715static inline void zs_pool_stat_destroy(struct zs_pool *pool)
716{
717}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700718#endif
719
Minchan Kim48b48002016-07-26 15:23:31 -0700720
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900721/*
722 * For each size class, zspages are divided into different groups
723 * depending on how "full" they are. This was done so that we could
724 * easily find empty or nearly empty zspages when we try to shrink
725 * the pool (not yet implemented). This function returns fullness
726 * status of the given page.
727 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700728static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700729 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600730{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700731 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600732 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700733
Minchan Kim37836892016-07-26 15:23:23 -0700734 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700735 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600736
737 if (inuse == 0)
738 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700739 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600740 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700741 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600742 fg = ZS_ALMOST_EMPTY;
743 else
744 fg = ZS_ALMOST_FULL;
745
746 return fg;
747}
748
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900749/*
750 * Each size class maintains various freelists and zspages are assigned
751 * to one of these freelists based on the number of live objects they
752 * have. This functions inserts the given zspage into the freelist
753 * identified by <class, fullness_group>.
754 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700755static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700756 struct zspage *zspage,
757 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600758{
Minchan Kim37836892016-07-26 15:23:23 -0700759 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600760
Minchan Kim48b48002016-07-26 15:23:31 -0700761 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700762 head = list_first_entry_or_null(&class->fullness_list[fullness],
763 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700764 /*
Minchan Kim37836892016-07-26 15:23:23 -0700765 * We want to see more ZS_FULL pages and less almost empty/full.
766 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700767 */
Minchan Kim37836892016-07-26 15:23:23 -0700768 if (head) {
769 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
770 list_add(&zspage->list, &head->list);
771 return;
772 }
773 }
774 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600775}
776
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900777/*
778 * This function removes the given zspage from the freelist identified
779 * by <class, fullness_group>.
780 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700781static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700782 struct zspage *zspage,
783 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600784{
Minchan Kim37836892016-07-26 15:23:23 -0700785 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700786 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600787
Minchan Kim37836892016-07-26 15:23:23 -0700788 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700789 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600790}
791
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900792/*
793 * Each size class maintains zspages in different fullness groups depending
794 * on the number of live objects they contain. When allocating or freeing
795 * objects, the fullness status of the page can change, say, from ALMOST_FULL
796 * to ALMOST_EMPTY when freeing an object. This function checks if such
797 * a status change has occurred for the given page and accordingly moves the
798 * page from the freelist of the old fullness group to that of the new
799 * fullness group.
800 */
Minchan Kimc7806262015-04-15 16:15:26 -0700801static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700802 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600803{
804 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600805 enum fullness_group currfg, newfg;
806
Minchan Kim37836892016-07-26 15:23:23 -0700807 get_zspage_mapping(zspage, &class_idx, &currfg);
808 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600809 if (newfg == currfg)
810 goto out;
811
Minchan Kim48b48002016-07-26 15:23:31 -0700812 if (!is_zspage_isolated(zspage)) {
813 remove_zspage(class, zspage, currfg);
814 insert_zspage(class, zspage, newfg);
815 }
816
Minchan Kim37836892016-07-26 15:23:23 -0700817 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600818
819out:
820 return newfg;
821}
822
823/*
824 * We have to decide on how many pages to link together
825 * to form a zspage for each size class. This is important
826 * to reduce wastage due to unusable space left at end of
827 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700828 * wastage = Zp % class_size
829 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600830 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
831 *
832 * For example, for size class of 3/8 * PAGE_SIZE, we should
833 * link together 3 PAGE_SIZE sized pages to form a zspage
834 * since then we can perfectly fit in 8 such objects.
835 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900836static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600837{
838 int i, max_usedpc = 0;
839 /* zspage order which gives maximum used size per KB */
840 int max_usedpc_order = 1;
841
Seth Jennings84d4faa2012-03-05 11:33:21 -0600842 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600843 int zspage_size;
844 int waste, usedpc;
845
846 zspage_size = i * PAGE_SIZE;
847 waste = zspage_size % class_size;
848 usedpc = (zspage_size - waste) * 100 / zspage_size;
849
850 if (usedpc > max_usedpc) {
851 max_usedpc = usedpc;
852 max_usedpc_order = i;
853 }
854 }
855
856 return max_usedpc_order;
857}
858
Minchan Kim37836892016-07-26 15:23:23 -0700859static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600860{
Minchan Kim48b48002016-07-26 15:23:31 -0700861 struct zspage *zspage = (struct zspage *)page->private;
862
863 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
864 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600865}
866
867static struct page *get_next_page(struct page *page)
868{
Minchan Kim48b48002016-07-26 15:23:31 -0700869 if (unlikely(PageHugeObject(page)))
870 return NULL;
871
872 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600873}
874
Minchan Kimbfd093f2016-07-26 15:23:28 -0700875/**
876 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
877 * @page: page object resides in zspage
878 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800879 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700880static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700881 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600882{
Minchan Kim312fcae2015-04-15 16:15:30 -0700883 obj >>= OBJ_TAG_BITS;
884 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
885 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600886}
887
Minchan Kimbfd093f2016-07-26 15:23:28 -0700888/**
889 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
890 * @page: page object resides in zspage
891 * @obj_idx: object index
892 */
893static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
894{
895 unsigned long obj;
896
897 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
898 obj |= obj_idx & OBJ_INDEX_MASK;
899 obj <<= OBJ_TAG_BITS;
900
901 return obj;
902}
903
Minchan Kim2e40e162015-04-15 16:15:23 -0700904static unsigned long handle_to_obj(unsigned long handle)
905{
906 return *(unsigned long *)handle;
907}
908
Minchan Kim48b48002016-07-26 15:23:31 -0700909static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700910{
Minchan Kim48b48002016-07-26 15:23:31 -0700911 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700912 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700913 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700914 } else
915 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700916}
917
Minchan Kim48b48002016-07-26 15:23:31 -0700918static inline int testpin_tag(unsigned long handle)
919{
920 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
921}
922
Minchan Kim312fcae2015-04-15 16:15:30 -0700923static inline int trypin_tag(unsigned long handle)
924{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700925 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700926}
927
928static void pin_tag(unsigned long handle)
929{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700930 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700931}
932
933static void unpin_tag(unsigned long handle)
934{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700935 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700936}
937
Nitin Guptaf4477e92012-04-02 09:13:56 -0500938static void reset_page(struct page *page)
939{
Minchan Kim48b48002016-07-26 15:23:31 -0700940 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700941 ClearPagePrivate(page);
942 ClearPagePrivate2(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500943 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700944 page_mapcount_reset(page);
945 ClearPageHugeObject(page);
946 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500947}
948
Minchan Kim48b48002016-07-26 15:23:31 -0700949/*
950 * To prevent zspage destroy during migration, zspage freeing should
951 * hold locks of all pages in the zspage.
952 */
953void lock_zspage(struct zspage *zspage)
954{
955 struct page *page = get_first_page(zspage);
956
957 do {
958 lock_page(page);
959 } while ((page = get_next_page(page)) != NULL);
960}
961
962int trylock_zspage(struct zspage *zspage)
963{
964 struct page *cursor, *fail;
965
966 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
967 get_next_page(cursor)) {
968 if (!trylock_page(cursor)) {
969 fail = cursor;
970 goto unlock;
971 }
972 }
973
974 return 1;
975unlock:
976 for (cursor = get_first_page(zspage); cursor != fail; cursor =
977 get_next_page(cursor))
978 unlock_page(cursor);
979
980 return 0;
981}
982
983static void __free_zspage(struct zs_pool *pool, struct size_class *class,
984 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600985{
Minchan Kim37836892016-07-26 15:23:23 -0700986 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700987 enum fullness_group fg;
988 unsigned int class_idx;
989
990 get_zspage_mapping(zspage, &class_idx, &fg);
991
992 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600993
Minchan Kim37836892016-07-26 15:23:23 -0700994 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700995 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600996
Minchan Kim48b48002016-07-26 15:23:31 -0700997 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700998 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700999 VM_BUG_ON_PAGE(!PageLocked(page), page);
1000 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001001 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001002 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001003 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001004 put_page(page);
1005 page = next;
1006 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001007
Minchan Kim37836892016-07-26 15:23:23 -07001008 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001009
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001010 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001011 atomic_long_sub(class->pages_per_zspage,
1012 &pool->pages_allocated);
1013}
1014
1015static void free_zspage(struct zs_pool *pool, struct size_class *class,
1016 struct zspage *zspage)
1017{
1018 VM_BUG_ON(get_zspage_inuse(zspage));
1019 VM_BUG_ON(list_empty(&zspage->list));
1020
1021 if (!trylock_zspage(zspage)) {
1022 kick_deferred_free(pool);
1023 return;
1024 }
1025
1026 remove_zspage(class, zspage, ZS_EMPTY);
1027 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001028}
1029
1030/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001031static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001032{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001033 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001034 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001035 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001036
Nitin Gupta61989a82012-01-09 16:51:56 -06001037 while (page) {
1038 struct page *next_page;
1039 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001040 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001041
Minchan Kim37836892016-07-26 15:23:23 -07001042 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001043
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001044 vaddr = kmap_atomic(page);
1045 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001046
Dan Streetman5538c562014-10-09 15:30:01 -07001047 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001048 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001049 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001050 }
1051
1052 /*
1053 * We now come to the last (full or partial) object on this
1054 * page, which must point to the first object on the next
1055 * page (if present)
1056 */
1057 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001058 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001059 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001060 } else {
1061 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001062 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001063 * whether it's allocated object or not.
1064 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001065 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001066 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001067 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001068 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001069 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001070 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001071
Minchan Kimbfd093f2016-07-26 15:23:28 -07001072 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001073}
1074
Minchan Kim48b48002016-07-26 15:23:31 -07001075static void create_page_chain(struct size_class *class, struct zspage *zspage,
1076 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001077{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001078 int i;
1079 struct page *page;
1080 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001081 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001082
1083 /*
1084 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001085 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001086 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001087 *
Minchan Kim37836892016-07-26 15:23:23 -07001088 * we set PG_private to identify the first page (i.e. no other sub-page
1089 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001090 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001091 for (i = 0; i < nr_pages; i++) {
1092 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001093 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001094 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001095 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001096 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001097 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001098 if (unlikely(class->objs_per_zspage == 1 &&
1099 class->pages_per_zspage == 1))
1100 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001101 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001102 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001103 }
Minchan Kim48b48002016-07-26 15:23:31 -07001104 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001105 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001106 prev_page = page;
1107 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001108}
Nitin Gupta61989a82012-01-09 16:51:56 -06001109
Minchan Kimbdb0af72016-07-26 15:23:20 -07001110/*
1111 * Allocate a zspage for the given size class
1112 */
Minchan Kim37836892016-07-26 15:23:23 -07001113static struct zspage *alloc_zspage(struct zs_pool *pool,
1114 struct size_class *class,
1115 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001116{
1117 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001118 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001119 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1120
1121 if (!zspage)
1122 return NULL;
1123
1124 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001125 zspage->magic = ZSPAGE_MAGIC;
1126 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001127
Minchan Kimbdb0af72016-07-26 15:23:20 -07001128 for (i = 0; i < class->pages_per_zspage; i++) {
1129 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001130
Minchan Kim37836892016-07-26 15:23:23 -07001131 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001132 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001133 while (--i >= 0) {
1134 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001135 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001136 }
Minchan Kim37836892016-07-26 15:23:23 -07001137 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001138 return NULL;
1139 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001140
1141 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001142 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001143 }
1144
Minchan Kim48b48002016-07-26 15:23:31 -07001145 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001146 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001147
Minchan Kim37836892016-07-26 15:23:23 -07001148 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001149}
1150
Minchan Kim37836892016-07-26 15:23:23 -07001151static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001152{
1153 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001154 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001155
Minchan Kim48b48002016-07-26 15:23:31 -07001156 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001157 zspage = list_first_entry_or_null(&class->fullness_list[i],
1158 struct zspage, list);
1159 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001160 break;
1161 }
1162
Minchan Kim37836892016-07-26 15:23:23 -07001163 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001164}
1165
Minchan Kim1b945ae2013-12-11 11:04:36 +09001166#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001167static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001168{
Seth Jenningsf5536462012-07-18 11:55:56 -05001169 /*
1170 * Make sure we don't leak memory if a cpu UP notification
1171 * and zs_init() race and both call zs_cpu_up() on the same cpu
1172 */
1173 if (area->vm)
1174 return 0;
1175 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1176 if (!area->vm)
1177 return -ENOMEM;
1178 return 0;
1179}
1180
1181static inline void __zs_cpu_down(struct mapping_area *area)
1182{
1183 if (area->vm)
1184 free_vm_area(area->vm);
1185 area->vm = NULL;
1186}
1187
1188static inline void *__zs_map_object(struct mapping_area *area,
1189 struct page *pages[2], int off, int size)
1190{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001191 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001192 area->vm_addr = area->vm->addr;
1193 return area->vm_addr + off;
1194}
1195
1196static inline void __zs_unmap_object(struct mapping_area *area,
1197 struct page *pages[2], int off, int size)
1198{
1199 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001200
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001201 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001202}
1203
Minchan Kim1b945ae2013-12-11 11:04:36 +09001204#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001205
1206static inline int __zs_cpu_up(struct mapping_area *area)
1207{
1208 /*
1209 * Make sure we don't leak memory if a cpu UP notification
1210 * and zs_init() race and both call zs_cpu_up() on the same cpu
1211 */
1212 if (area->vm_buf)
1213 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001214 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001215 if (!area->vm_buf)
1216 return -ENOMEM;
1217 return 0;
1218}
1219
1220static inline void __zs_cpu_down(struct mapping_area *area)
1221{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001222 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001223 area->vm_buf = NULL;
1224}
1225
1226static void *__zs_map_object(struct mapping_area *area,
1227 struct page *pages[2], int off, int size)
1228{
Seth Jennings5f601902012-07-02 16:15:49 -05001229 int sizes[2];
1230 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001231 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001232
Seth Jenningsf5536462012-07-18 11:55:56 -05001233 /* disable page faults to match kmap_atomic() return conditions */
1234 pagefault_disable();
1235
1236 /* no read fastpath */
1237 if (area->vm_mm == ZS_MM_WO)
1238 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001239
1240 sizes[0] = PAGE_SIZE - off;
1241 sizes[1] = size - sizes[0];
1242
Seth Jennings5f601902012-07-02 16:15:49 -05001243 /* copy object to per-cpu buffer */
1244 addr = kmap_atomic(pages[0]);
1245 memcpy(buf, addr + off, sizes[0]);
1246 kunmap_atomic(addr);
1247 addr = kmap_atomic(pages[1]);
1248 memcpy(buf + sizes[0], addr, sizes[1]);
1249 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001250out:
1251 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001252}
1253
Seth Jenningsf5536462012-07-18 11:55:56 -05001254static void __zs_unmap_object(struct mapping_area *area,
1255 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001256{
Seth Jennings5f601902012-07-02 16:15:49 -05001257 int sizes[2];
1258 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001259 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001260
Seth Jenningsf5536462012-07-18 11:55:56 -05001261 /* no write fastpath */
1262 if (area->vm_mm == ZS_MM_RO)
1263 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001264
Minchan Kim7b60a682015-04-15 16:15:39 -07001265 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001266 buf = buf + ZS_HANDLE_SIZE;
1267 size -= ZS_HANDLE_SIZE;
1268 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001269
Seth Jennings5f601902012-07-02 16:15:49 -05001270 sizes[0] = PAGE_SIZE - off;
1271 sizes[1] = size - sizes[0];
1272
1273 /* copy per-cpu buffer to object */
1274 addr = kmap_atomic(pages[0]);
1275 memcpy(addr + off, buf, sizes[0]);
1276 kunmap_atomic(addr);
1277 addr = kmap_atomic(pages[1]);
1278 memcpy(addr, buf + sizes[0], sizes[1]);
1279 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001280
1281out:
1282 /* enable page faults to match kunmap_atomic() return conditions */
1283 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001284}
Nitin Gupta61989a82012-01-09 16:51:56 -06001285
Minchan Kim1b945ae2013-12-11 11:04:36 +09001286#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001287
Nitin Gupta61989a82012-01-09 16:51:56 -06001288static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1289 void *pcpu)
1290{
Seth Jenningsf5536462012-07-18 11:55:56 -05001291 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001292 struct mapping_area *area;
1293
1294 switch (action) {
1295 case CPU_UP_PREPARE:
1296 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001297 ret = __zs_cpu_up(area);
1298 if (ret)
1299 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001300 break;
1301 case CPU_DEAD:
1302 case CPU_UP_CANCELED:
1303 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001304 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001305 break;
1306 }
1307
1308 return NOTIFY_OK;
1309}
1310
1311static struct notifier_block zs_cpu_nb = {
1312 .notifier_call = zs_cpu_notifier
1313};
1314
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001315static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001316{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001317 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001318
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301319 cpu_notifier_register_begin();
1320
1321 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001322 for_each_online_cpu(cpu) {
1323 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001324 if (notifier_to_errno(ret))
1325 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001326 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301327
1328 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001329 return notifier_to_errno(ret);
1330}
1331
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001332static void zs_unregister_cpu_notifier(void)
1333{
1334 int cpu;
1335
1336 cpu_notifier_register_begin();
1337
1338 for_each_online_cpu(cpu)
1339 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1340 __unregister_cpu_notifier(&zs_cpu_nb);
1341
1342 cpu_notifier_register_done();
1343}
1344
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001345static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001346{
1347 int nr;
1348
1349 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1350 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1351 nr += 1;
1352
1353 zs_size_classes = nr;
1354}
1355
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001356static bool can_merge(struct size_class *prev, int pages_per_zspage,
1357 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001358{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001359 if (prev->pages_per_zspage == pages_per_zspage &&
1360 prev->objs_per_zspage == objs_per_zspage)
1361 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001362
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001363 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001364}
1365
Minchan Kim37836892016-07-26 15:23:23 -07001366static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001367{
Minchan Kim37836892016-07-26 15:23:23 -07001368 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001369}
1370
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001371unsigned long zs_get_total_pages(struct zs_pool *pool)
1372{
1373 return atomic_long_read(&pool->pages_allocated);
1374}
1375EXPORT_SYMBOL_GPL(zs_get_total_pages);
1376
1377/**
1378 * zs_map_object - get address of allocated object from handle.
1379 * @pool: pool from which the object was allocated
1380 * @handle: handle returned from zs_malloc
1381 *
1382 * Before using an object allocated from zs_malloc, it must be mapped using
1383 * this function. When done with the object, it must be unmapped using
1384 * zs_unmap_object.
1385 *
1386 * Only one object can be mapped per cpu at a time. There is no protection
1387 * against nested mappings.
1388 *
1389 * This function returns with preemption and page faults disabled.
1390 */
1391void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1392 enum zs_mapmode mm)
1393{
Minchan Kim37836892016-07-26 15:23:23 -07001394 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001395 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001396 unsigned long obj, off;
1397 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001398
1399 unsigned int class_idx;
1400 enum fullness_group fg;
1401 struct size_class *class;
1402 struct mapping_area *area;
1403 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001404 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001405
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001406 /*
1407 * Because we use per-cpu mapping areas shared among the
1408 * pools/users, we can't allow mapping in interrupt context
1409 * because it can corrupt another users mappings.
1410 */
Sergey Senozhatsky16184002017-11-15 17:34:03 -08001411 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001412
Minchan Kim312fcae2015-04-15 16:15:30 -07001413 /* From now on, migration cannot move the object */
1414 pin_tag(handle);
1415
Minchan Kim2e40e162015-04-15 16:15:23 -07001416 obj = handle_to_obj(handle);
1417 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001418 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001419
1420 /* migration cannot move any subpage in this zspage */
1421 migrate_read_lock(zspage);
1422
Minchan Kim37836892016-07-26 15:23:23 -07001423 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001424 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001425 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001426
1427 area = &get_cpu_var(zs_map_area);
1428 area->vm_mm = mm;
1429 if (off + class->size <= PAGE_SIZE) {
1430 /* this object is contained entirely within a page */
1431 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001432 ret = area->vm_addr + off;
1433 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001434 }
1435
1436 /* this object spans two pages */
1437 pages[0] = page;
1438 pages[1] = get_next_page(page);
1439 BUG_ON(!pages[1]);
1440
Minchan Kim2e40e162015-04-15 16:15:23 -07001441 ret = __zs_map_object(area, pages, off, class->size);
1442out:
Minchan Kim48b48002016-07-26 15:23:31 -07001443 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001444 ret += ZS_HANDLE_SIZE;
1445
1446 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001447}
1448EXPORT_SYMBOL_GPL(zs_map_object);
1449
1450void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1451{
Minchan Kim37836892016-07-26 15:23:23 -07001452 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001453 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001454 unsigned long obj, off;
1455 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001456
1457 unsigned int class_idx;
1458 enum fullness_group fg;
1459 struct size_class *class;
1460 struct mapping_area *area;
1461
Minchan Kim2e40e162015-04-15 16:15:23 -07001462 obj = handle_to_obj(handle);
1463 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001464 zspage = get_zspage(page);
1465 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001466 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001467 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001468
1469 area = this_cpu_ptr(&zs_map_area);
1470 if (off + class->size <= PAGE_SIZE)
1471 kunmap_atomic(area->vm_addr);
1472 else {
1473 struct page *pages[2];
1474
1475 pages[0] = page;
1476 pages[1] = get_next_page(page);
1477 BUG_ON(!pages[1]);
1478
1479 __zs_unmap_object(area, pages, off, class->size);
1480 }
1481 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001482
1483 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001484 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001485}
1486EXPORT_SYMBOL_GPL(zs_unmap_object);
1487
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07001488/**
1489 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1490 * zsmalloc &size_class.
1491 * @pool: zsmalloc pool to use
1492 *
1493 * The function returns the size of the first huge class - any object of equal
1494 * or bigger size will be stored in zspage consisting of a single physical
1495 * page.
1496 *
1497 * Context: Any context.
1498 *
1499 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1500 */
1501size_t zs_huge_class_size(struct zs_pool *pool)
1502{
1503 return huge_class_size;
1504}
1505EXPORT_SYMBOL_GPL(zs_huge_class_size);
1506
Minchan Kim251cbb92016-05-20 16:59:42 -07001507static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001508 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001509{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001510 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001511 unsigned long obj;
1512 struct link_free *link;
1513
1514 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001515 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001516 void *vaddr;
1517
Minchan Kim312fcae2015-04-15 16:15:30 -07001518 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001519 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001520
1521 offset = obj * class->size;
1522 nr_page = offset >> PAGE_SHIFT;
1523 m_offset = offset & ~PAGE_MASK;
1524 m_page = get_first_page(zspage);
1525
1526 for (i = 0; i < nr_page; i++)
1527 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001528
1529 vaddr = kmap_atomic(m_page);
1530 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001531 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001532 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001533 /* record handle in the header of allocated chunk */
1534 link->handle = handle;
1535 else
Minchan Kim37836892016-07-26 15:23:23 -07001536 /* record handle to page->index */
1537 zspage->first_page->index = handle;
1538
Minchan Kimc7806262015-04-15 16:15:26 -07001539 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001540 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001541 zs_stat_inc(class, OBJ_USED, 1);
1542
Minchan Kimbfd093f2016-07-26 15:23:28 -07001543 obj = location_to_obj(m_page, obj);
1544
Minchan Kimc7806262015-04-15 16:15:26 -07001545 return obj;
1546}
1547
1548
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001549/**
1550 * zs_malloc - Allocate block of given size from pool.
1551 * @pool: pool to allocate from
1552 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001553 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001554 *
1555 * On success, handle to the allocated object is returned,
1556 * otherwise 0.
1557 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1558 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001559unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001560{
Minchan Kim2e40e162015-04-15 16:15:23 -07001561 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001562 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001563 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001564 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001565
Minchan Kim7b60a682015-04-15 16:15:39 -07001566 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001567 return 0;
1568
Minchan Kim37836892016-07-26 15:23:23 -07001569 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001570 if (!handle)
1571 return 0;
1572
1573 /* extra space in chunk to keep the handle */
1574 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001575 class = pool->size_class[get_size_class_index(size)];
1576
1577 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001578 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001579 if (likely(zspage)) {
1580 obj = obj_malloc(class, zspage, handle);
1581 /* Now move the zspage to another fullness group, if required */
1582 fix_fullness_group(class, zspage);
1583 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001584 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001585
Minchan Kim48b48002016-07-26 15:23:31 -07001586 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001587 }
1588
Minchan Kim48b48002016-07-26 15:23:31 -07001589 spin_unlock(&class->lock);
1590
1591 zspage = alloc_zspage(pool, class, gfp);
1592 if (!zspage) {
1593 cache_free_handle(pool, handle);
1594 return 0;
1595 }
1596
1597 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001598 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001599 newfg = get_fullness_group(class, zspage);
1600 insert_zspage(class, zspage, newfg);
1601 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001602 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001603 atomic_long_add(class->pages_per_zspage,
1604 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001605 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001606
1607 /* We completely set up zspage so mark them as movable */
1608 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001609 spin_unlock(&class->lock);
1610
Minchan Kim2e40e162015-04-15 16:15:23 -07001611 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001612}
1613EXPORT_SYMBOL_GPL(zs_malloc);
1614
Minchan Kim1ee47162016-05-20 16:59:45 -07001615static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001616{
1617 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001618 struct zspage *zspage;
1619 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001620 unsigned long f_offset;
1621 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001622 void *vaddr;
1623
Minchan Kim312fcae2015-04-15 16:15:30 -07001624 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001625 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001626 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001627 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001628
Minchan Kimc7806262015-04-15 16:15:26 -07001629 vaddr = kmap_atomic(f_page);
1630
1631 /* Insert this object in containing zspage's freelist */
1632 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001633 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001634 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001635 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001636 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001637 zs_stat_dec(class, OBJ_USED, 1);
1638}
1639
1640void zs_free(struct zs_pool *pool, unsigned long handle)
1641{
Minchan Kim37836892016-07-26 15:23:23 -07001642 struct zspage *zspage;
1643 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001644 unsigned long obj;
1645 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001646 int class_idx;
1647 struct size_class *class;
1648 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001649 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001650
Minchan Kim2e40e162015-04-15 16:15:23 -07001651 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001652 return;
1653
Minchan Kim312fcae2015-04-15 16:15:30 -07001654 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001655 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001656 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001657 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001658
Minchan Kim48b48002016-07-26 15:23:31 -07001659 migrate_read_lock(zspage);
1660
Minchan Kim37836892016-07-26 15:23:23 -07001661 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001662 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001663
1664 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001665 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001666 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001667 if (fullness != ZS_EMPTY) {
1668 migrate_read_unlock(zspage);
1669 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001670 }
Minchan Kim48b48002016-07-26 15:23:31 -07001671
1672 isolated = is_zspage_isolated(zspage);
1673 migrate_read_unlock(zspage);
1674 /* If zspage is isolated, zs_page_putback will free the zspage */
1675 if (likely(!isolated))
1676 free_zspage(pool, class, zspage);
1677out:
1678
Minchan Kim312fcae2015-04-15 16:15:30 -07001679 spin_unlock(&class->lock);
1680 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001681 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001682}
1683EXPORT_SYMBOL_GPL(zs_free);
1684
Minchan Kim251cbb92016-05-20 16:59:42 -07001685static void zs_object_copy(struct size_class *class, unsigned long dst,
1686 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001687{
1688 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001689 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001690 unsigned long s_off, d_off;
1691 void *s_addr, *d_addr;
1692 int s_size, d_size, size;
1693 int written = 0;
1694
1695 s_size = d_size = class->size;
1696
1697 obj_to_location(src, &s_page, &s_objidx);
1698 obj_to_location(dst, &d_page, &d_objidx);
1699
Minchan Kimbfd093f2016-07-26 15:23:28 -07001700 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1701 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001702
1703 if (s_off + class->size > PAGE_SIZE)
1704 s_size = PAGE_SIZE - s_off;
1705
1706 if (d_off + class->size > PAGE_SIZE)
1707 d_size = PAGE_SIZE - d_off;
1708
1709 s_addr = kmap_atomic(s_page);
1710 d_addr = kmap_atomic(d_page);
1711
1712 while (1) {
1713 size = min(s_size, d_size);
1714 memcpy(d_addr + d_off, s_addr + s_off, size);
1715 written += size;
1716
1717 if (written == class->size)
1718 break;
1719
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001720 s_off += size;
1721 s_size -= size;
1722 d_off += size;
1723 d_size -= size;
1724
1725 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001726 kunmap_atomic(d_addr);
1727 kunmap_atomic(s_addr);
1728 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001729 s_addr = kmap_atomic(s_page);
1730 d_addr = kmap_atomic(d_page);
1731 s_size = class->size - written;
1732 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001733 }
1734
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001735 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001736 kunmap_atomic(d_addr);
1737 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001738 d_addr = kmap_atomic(d_page);
1739 d_size = class->size - written;
1740 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001741 }
1742 }
1743
1744 kunmap_atomic(d_addr);
1745 kunmap_atomic(s_addr);
1746}
1747
1748/*
1749 * Find alloced object in zspage from index object and
1750 * return handle.
1751 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001752static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001753 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001754{
1755 unsigned long head;
1756 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001757 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001758 unsigned long handle = 0;
1759 void *addr = kmap_atomic(page);
1760
Minchan Kim37836892016-07-26 15:23:23 -07001761 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001762 offset += class->size * index;
1763
1764 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001765 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001766 if (head & OBJ_ALLOCATED_TAG) {
1767 handle = head & ~OBJ_ALLOCATED_TAG;
1768 if (trypin_tag(handle))
1769 break;
1770 handle = 0;
1771 }
1772
1773 offset += class->size;
1774 index++;
1775 }
1776
1777 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001778
1779 *obj_idx = index;
1780
Minchan Kim312fcae2015-04-15 16:15:30 -07001781 return handle;
1782}
1783
1784struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001785 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001786 struct page *s_page;
1787 /* Destination page for migration which should be a first page
1788 * of zspage. */
1789 struct page *d_page;
1790 /* Starting object index within @s_page which used for live object
1791 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001792 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001793};
1794
1795static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1796 struct zs_compact_control *cc)
1797{
1798 unsigned long used_obj, free_obj;
1799 unsigned long handle;
1800 struct page *s_page = cc->s_page;
1801 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001802 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001803 int ret = 0;
1804
1805 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001806 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001807 if (!handle) {
1808 s_page = get_next_page(s_page);
1809 if (!s_page)
1810 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001811 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001812 continue;
1813 }
1814
1815 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001816 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001817 unpin_tag(handle);
1818 ret = -ENOMEM;
1819 break;
1820 }
1821
1822 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001823 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001824 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001825 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001826 /*
1827 * record_obj updates handle's value to free_obj and it will
1828 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1829 * breaks synchronization using pin_tag(e,g, zs_free) so
1830 * let's keep the lock bit.
1831 */
1832 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001833 record_obj(handle, free_obj);
1834 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001835 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001836 }
1837
1838 /* Remember last position in this iteration */
1839 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001840 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001841
1842 return ret;
1843}
1844
Minchan Kim37836892016-07-26 15:23:23 -07001845static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001846{
1847 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001848 struct zspage *zspage;
1849 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001850
Minchan Kim37836892016-07-26 15:23:23 -07001851 if (!source) {
1852 fg[0] = ZS_ALMOST_FULL;
1853 fg[1] = ZS_ALMOST_EMPTY;
1854 }
1855
1856 for (i = 0; i < 2; i++) {
1857 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1858 struct zspage, list);
1859 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001860 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001861 remove_zspage(class, zspage, fg[i]);
1862 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001863 }
1864 }
1865
Minchan Kim37836892016-07-26 15:23:23 -07001866 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001867}
1868
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001869/*
Minchan Kim37836892016-07-26 15:23:23 -07001870 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001871 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001872 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001873 *
Minchan Kim37836892016-07-26 15:23:23 -07001874 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001875 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001876static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001877 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001878{
Minchan Kim312fcae2015-04-15 16:15:30 -07001879 enum fullness_group fullness;
1880
Minchan Kim48b48002016-07-26 15:23:31 -07001881 VM_BUG_ON(is_zspage_isolated(zspage));
1882
Minchan Kim37836892016-07-26 15:23:23 -07001883 fullness = get_fullness_group(class, zspage);
1884 insert_zspage(class, zspage, fullness);
1885 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001886
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001887 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001888}
1889
Minchan Kim48b48002016-07-26 15:23:31 -07001890#ifdef CONFIG_COMPACTION
1891static struct dentry *zs_mount(struct file_system_type *fs_type,
1892 int flags, const char *dev_name, void *data)
1893{
1894 static const struct dentry_operations ops = {
1895 .d_dname = simple_dname,
1896 };
1897
1898 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1899}
1900
1901static struct file_system_type zsmalloc_fs = {
1902 .name = "zsmalloc",
1903 .mount = zs_mount,
1904 .kill_sb = kill_anon_super,
1905};
1906
1907static int zsmalloc_mount(void)
1908{
1909 int ret = 0;
1910
1911 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1912 if (IS_ERR(zsmalloc_mnt))
1913 ret = PTR_ERR(zsmalloc_mnt);
1914
1915 return ret;
1916}
1917
1918static void zsmalloc_unmount(void)
1919{
1920 kern_unmount(zsmalloc_mnt);
1921}
1922
1923static void migrate_lock_init(struct zspage *zspage)
1924{
1925 rwlock_init(&zspage->lock);
1926}
1927
1928static void migrate_read_lock(struct zspage *zspage)
1929{
1930 read_lock(&zspage->lock);
1931}
1932
1933static void migrate_read_unlock(struct zspage *zspage)
1934{
1935 read_unlock(&zspage->lock);
1936}
1937
1938static void migrate_write_lock(struct zspage *zspage)
1939{
1940 write_lock(&zspage->lock);
1941}
1942
1943static void migrate_write_unlock(struct zspage *zspage)
1944{
1945 write_unlock(&zspage->lock);
1946}
1947
1948/* Number of isolated subpage for *page migration* in this zspage */
1949static void inc_zspage_isolation(struct zspage *zspage)
1950{
1951 zspage->isolated++;
1952}
1953
1954static void dec_zspage_isolation(struct zspage *zspage)
1955{
1956 zspage->isolated--;
1957}
1958
1959static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1960 struct page *newpage, struct page *oldpage)
1961{
1962 struct page *page;
1963 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1964 int idx = 0;
1965
1966 page = get_first_page(zspage);
1967 do {
1968 if (page == oldpage)
1969 pages[idx] = newpage;
1970 else
1971 pages[idx] = page;
1972 idx++;
1973 } while ((page = get_next_page(page)) != NULL);
1974
1975 create_page_chain(class, zspage, pages);
1976 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1977 if (unlikely(PageHugeObject(oldpage)))
1978 newpage->index = oldpage->index;
1979 __SetPageMovable(newpage, page_mapping(oldpage));
1980}
1981
1982bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1983{
1984 struct zs_pool *pool;
1985 struct size_class *class;
1986 int class_idx;
1987 enum fullness_group fullness;
1988 struct zspage *zspage;
1989 struct address_space *mapping;
1990
1991 /*
1992 * Page is locked so zspage couldn't be destroyed. For detail, look at
1993 * lock_zspage in free_zspage.
1994 */
1995 VM_BUG_ON_PAGE(!PageMovable(page), page);
1996 VM_BUG_ON_PAGE(PageIsolated(page), page);
1997
1998 zspage = get_zspage(page);
1999
2000 /*
2001 * Without class lock, fullness could be stale while class_idx is okay
2002 * because class_idx is constant unless page is freed so we should get
2003 * fullness again under class lock.
2004 */
2005 get_zspage_mapping(zspage, &class_idx, &fullness);
2006 mapping = page_mapping(page);
2007 pool = mapping->private_data;
2008 class = pool->size_class[class_idx];
2009
2010 spin_lock(&class->lock);
2011 if (get_zspage_inuse(zspage) == 0) {
2012 spin_unlock(&class->lock);
2013 return false;
2014 }
2015
2016 /* zspage is isolated for object migration */
2017 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2018 spin_unlock(&class->lock);
2019 return false;
2020 }
2021
2022 /*
2023 * If this is first time isolation for the zspage, isolate zspage from
2024 * size_class to prevent further object allocation from the zspage.
2025 */
2026 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2027 get_zspage_mapping(zspage, &class_idx, &fullness);
2028 remove_zspage(class, zspage, fullness);
2029 }
2030
2031 inc_zspage_isolation(zspage);
2032 spin_unlock(&class->lock);
2033
2034 return true;
2035}
2036
2037int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2038 struct page *page, enum migrate_mode mode)
2039{
2040 struct zs_pool *pool;
2041 struct size_class *class;
2042 int class_idx;
2043 enum fullness_group fullness;
2044 struct zspage *zspage;
2045 struct page *dummy;
2046 void *s_addr, *d_addr, *addr;
2047 int offset, pos;
2048 unsigned long handle, head;
2049 unsigned long old_obj, new_obj;
2050 unsigned int obj_idx;
2051 int ret = -EAGAIN;
2052
2053 VM_BUG_ON_PAGE(!PageMovable(page), page);
2054 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2055
2056 zspage = get_zspage(page);
2057
2058 /* Concurrent compactor cannot migrate any subpage in zspage */
2059 migrate_write_lock(zspage);
2060 get_zspage_mapping(zspage, &class_idx, &fullness);
2061 pool = mapping->private_data;
2062 class = pool->size_class[class_idx];
2063 offset = get_first_obj_offset(page);
2064
2065 spin_lock(&class->lock);
2066 if (!get_zspage_inuse(zspage)) {
2067 ret = -EBUSY;
2068 goto unlock_class;
2069 }
2070
2071 pos = offset;
2072 s_addr = kmap_atomic(page);
2073 while (pos < PAGE_SIZE) {
2074 head = obj_to_head(page, s_addr + pos);
2075 if (head & OBJ_ALLOCATED_TAG) {
2076 handle = head & ~OBJ_ALLOCATED_TAG;
2077 if (!trypin_tag(handle))
2078 goto unpin_objects;
2079 }
2080 pos += class->size;
2081 }
2082
2083 /*
2084 * Here, any user cannot access all objects in the zspage so let's move.
2085 */
2086 d_addr = kmap_atomic(newpage);
2087 memcpy(d_addr, s_addr, PAGE_SIZE);
2088 kunmap_atomic(d_addr);
2089
2090 for (addr = s_addr + offset; addr < s_addr + pos;
2091 addr += class->size) {
2092 head = obj_to_head(page, addr);
2093 if (head & OBJ_ALLOCATED_TAG) {
2094 handle = head & ~OBJ_ALLOCATED_TAG;
2095 if (!testpin_tag(handle))
2096 BUG();
2097
2098 old_obj = handle_to_obj(handle);
2099 obj_to_location(old_obj, &dummy, &obj_idx);
2100 new_obj = (unsigned long)location_to_obj(newpage,
2101 obj_idx);
2102 new_obj |= BIT(HANDLE_PIN_BIT);
2103 record_obj(handle, new_obj);
2104 }
2105 }
2106
2107 replace_sub_page(class, zspage, newpage, page);
2108 get_page(newpage);
2109
2110 dec_zspage_isolation(zspage);
2111
2112 /*
2113 * Page migration is done so let's putback isolated zspage to
2114 * the list if @page is final isolated subpage in the zspage.
2115 */
2116 if (!is_zspage_isolated(zspage))
2117 putback_zspage(class, zspage);
2118
2119 reset_page(page);
2120 put_page(page);
2121 page = newpage;
2122
Minchan Kimdd4123f2016-07-26 15:26:50 -07002123 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002124unpin_objects:
2125 for (addr = s_addr + offset; addr < s_addr + pos;
2126 addr += class->size) {
2127 head = obj_to_head(page, addr);
2128 if (head & OBJ_ALLOCATED_TAG) {
2129 handle = head & ~OBJ_ALLOCATED_TAG;
2130 if (!testpin_tag(handle))
2131 BUG();
2132 unpin_tag(handle);
2133 }
2134 }
2135 kunmap_atomic(s_addr);
2136unlock_class:
2137 spin_unlock(&class->lock);
2138 migrate_write_unlock(zspage);
2139
2140 return ret;
2141}
2142
2143void zs_page_putback(struct page *page)
2144{
2145 struct zs_pool *pool;
2146 struct size_class *class;
2147 int class_idx;
2148 enum fullness_group fg;
2149 struct address_space *mapping;
2150 struct zspage *zspage;
2151
2152 VM_BUG_ON_PAGE(!PageMovable(page), page);
2153 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2154
2155 zspage = get_zspage(page);
2156 get_zspage_mapping(zspage, &class_idx, &fg);
2157 mapping = page_mapping(page);
2158 pool = mapping->private_data;
2159 class = pool->size_class[class_idx];
2160
2161 spin_lock(&class->lock);
2162 dec_zspage_isolation(zspage);
2163 if (!is_zspage_isolated(zspage)) {
2164 fg = putback_zspage(class, zspage);
2165 /*
2166 * Due to page_lock, we cannot free zspage immediately
2167 * so let's defer.
2168 */
2169 if (fg == ZS_EMPTY)
2170 schedule_work(&pool->free_work);
2171 }
2172 spin_unlock(&class->lock);
2173}
2174
2175const struct address_space_operations zsmalloc_aops = {
2176 .isolate_page = zs_page_isolate,
2177 .migratepage = zs_page_migrate,
2178 .putback_page = zs_page_putback,
2179};
2180
2181static int zs_register_migration(struct zs_pool *pool)
2182{
2183 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2184 if (IS_ERR(pool->inode)) {
2185 pool->inode = NULL;
2186 return 1;
2187 }
2188
2189 pool->inode->i_mapping->private_data = pool;
2190 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2191 return 0;
2192}
2193
2194static void zs_unregister_migration(struct zs_pool *pool)
2195{
2196 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002197 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002198}
2199
2200/*
2201 * Caller should hold page_lock of all pages in the zspage
2202 * In here, we cannot use zspage meta data.
2203 */
2204static void async_free_zspage(struct work_struct *work)
2205{
2206 int i;
2207 struct size_class *class;
2208 unsigned int class_idx;
2209 enum fullness_group fullness;
2210 struct zspage *zspage, *tmp;
2211 LIST_HEAD(free_pages);
2212 struct zs_pool *pool = container_of(work, struct zs_pool,
2213 free_work);
2214
2215 for (i = 0; i < zs_size_classes; i++) {
2216 class = pool->size_class[i];
2217 if (class->index != i)
2218 continue;
2219
2220 spin_lock(&class->lock);
2221 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2222 spin_unlock(&class->lock);
2223 }
2224
2225
2226 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2227 list_del(&zspage->list);
2228 lock_zspage(zspage);
2229
2230 get_zspage_mapping(zspage, &class_idx, &fullness);
2231 VM_BUG_ON(fullness != ZS_EMPTY);
2232 class = pool->size_class[class_idx];
2233 spin_lock(&class->lock);
2234 __free_zspage(pool, pool->size_class[class_idx], zspage);
2235 spin_unlock(&class->lock);
2236 }
2237};
2238
2239static void kick_deferred_free(struct zs_pool *pool)
2240{
2241 schedule_work(&pool->free_work);
2242}
2243
2244static void init_deferred_free(struct zs_pool *pool)
2245{
2246 INIT_WORK(&pool->free_work, async_free_zspage);
2247}
2248
2249static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2250{
2251 struct page *page = get_first_page(zspage);
2252
2253 do {
2254 WARN_ON(!trylock_page(page));
2255 __SetPageMovable(page, pool->inode->i_mapping);
2256 unlock_page(page);
2257 } while ((page = get_next_page(page)) != NULL);
2258}
2259#endif
2260
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002261/*
2262 *
2263 * Based on the number of unused allocated objects calculate
2264 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002265 */
2266static unsigned long zs_can_compact(struct size_class *class)
2267{
2268 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002269 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2270 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002271
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002272 if (obj_allocated <= obj_used)
2273 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002274
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002275 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002276 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002277
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002278 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002279}
2280
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002281static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002282{
Minchan Kim312fcae2015-04-15 16:15:30 -07002283 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002284 struct zspage *src_zspage;
2285 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002286
Minchan Kim312fcae2015-04-15 16:15:30 -07002287 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002288 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002289
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002290 if (!zs_can_compact(class))
2291 break;
2292
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002293 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002294 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002295
Minchan Kim37836892016-07-26 15:23:23 -07002296 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002297 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002298 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002299 * If there is no more space in dst_page, resched
2300 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002301 */
2302 if (!migrate_zspage(pool, class, &cc))
2303 break;
2304
Minchan Kim4aa409c2016-07-26 15:23:26 -07002305 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002306 }
2307
2308 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002309 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002310 break;
2311
Minchan Kim4aa409c2016-07-26 15:23:26 -07002312 putback_zspage(class, dst_zspage);
2313 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002314 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002315 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002316 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002317 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002318 cond_resched();
2319 spin_lock(&class->lock);
2320 }
2321
Minchan Kim37836892016-07-26 15:23:23 -07002322 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002323 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002324
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002325 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002326}
2327
2328unsigned long zs_compact(struct zs_pool *pool)
2329{
2330 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002331 struct size_class *class;
2332
2333 for (i = zs_size_classes - 1; i >= 0; i--) {
2334 class = pool->size_class[i];
2335 if (!class)
2336 continue;
2337 if (class->index != i)
2338 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002339 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002340 }
2341
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002342 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002343}
2344EXPORT_SYMBOL_GPL(zs_compact);
2345
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002346void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2347{
2348 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2349}
2350EXPORT_SYMBOL_GPL(zs_pool_stats);
2351
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002352static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2353 struct shrink_control *sc)
2354{
2355 unsigned long pages_freed;
2356 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2357 shrinker);
2358
2359 pages_freed = pool->stats.pages_compacted;
2360 /*
2361 * Compact classes and calculate compaction delta.
2362 * Can run concurrently with a manually triggered
2363 * (by user) compaction.
2364 */
2365 pages_freed = zs_compact(pool) - pages_freed;
2366
2367 return pages_freed ? pages_freed : SHRINK_STOP;
2368}
2369
2370static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2371 struct shrink_control *sc)
2372{
2373 int i;
2374 struct size_class *class;
2375 unsigned long pages_to_free = 0;
2376 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2377 shrinker);
2378
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002379 for (i = zs_size_classes - 1; i >= 0; i--) {
2380 class = pool->size_class[i];
2381 if (!class)
2382 continue;
2383 if (class->index != i)
2384 continue;
2385
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002386 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002387 }
2388
2389 return pages_to_free;
2390}
2391
2392static void zs_unregister_shrinker(struct zs_pool *pool)
2393{
2394 if (pool->shrinker_enabled) {
2395 unregister_shrinker(&pool->shrinker);
2396 pool->shrinker_enabled = false;
2397 }
2398}
2399
2400static int zs_register_shrinker(struct zs_pool *pool)
2401{
2402 pool->shrinker.scan_objects = zs_shrinker_scan;
2403 pool->shrinker.count_objects = zs_shrinker_count;
2404 pool->shrinker.batch = 0;
2405 pool->shrinker.seeks = DEFAULT_SEEKS;
2406
2407 return register_shrinker(&pool->shrinker);
2408}
2409
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002410/**
2411 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002412 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002413 *
2414 * This function must be called before anything when using
2415 * the zsmalloc allocator.
2416 *
2417 * On success, a pointer to the newly created pool is returned,
2418 * otherwise NULL.
2419 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002420struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002421{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002422 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002423 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002424 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002425
Ganesh Mahendran18136652014-12-12 16:57:10 -08002426 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002427 if (!pool)
2428 return NULL;
2429
Minchan Kim48b48002016-07-26 15:23:31 -07002430 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002431 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2432 GFP_KERNEL);
2433 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002434 kfree(pool);
2435 return NULL;
2436 }
2437
Minchan Kim2e40e162015-04-15 16:15:23 -07002438 pool->name = kstrdup(name, GFP_KERNEL);
2439 if (!pool->name)
2440 goto err;
2441
Minchan Kim37836892016-07-26 15:23:23 -07002442 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002443 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002444
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002445 /*
2446 * Iterate reversly, because, size of size_class that we want to use
2447 * for merging should be larger or equal to current size.
2448 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002449 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002450 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002451 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002452 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002453 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002454 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002455
2456 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2457 if (size > ZS_MAX_ALLOC_SIZE)
2458 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002459 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002460 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002461
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002462 /*
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07002463 * We iterate from biggest down to smallest classes,
2464 * so huge_class_size holds the size of the first huge
2465 * class. Any object bigger than or equal to that will
2466 * endup in the huge class.
2467 */
2468 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2469 !huge_class_size) {
2470 huge_class_size = size;
2471 /*
2472 * The object uses ZS_HANDLE_SIZE bytes to store the
2473 * handle. We need to subtract it, because zs_malloc()
2474 * unconditionally adds handle size before it performs
2475 * size class search - so object may be smaller than
2476 * huge class size, yet it still can end up in the huge
2477 * class because it grows by ZS_HANDLE_SIZE extra bytes
2478 * right before class lookup.
2479 */
2480 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2481 }
2482
2483 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002484 * size_class is used for normal zsmalloc operation such
2485 * as alloc/free for that size. Although it is natural that we
2486 * have one size_class for each size, there is a chance that we
2487 * can get more memory utilization if we use one size_class for
2488 * many different sizes whose size_class have same
2489 * characteristics. So, we makes size_class point to
2490 * previous size_class if possible.
2491 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002492 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002493 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002494 pool->size_class[i] = prev_class;
2495 continue;
2496 }
2497 }
2498
2499 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2500 if (!class)
2501 goto err;
2502
Nitin Gupta61989a82012-01-09 16:51:56 -06002503 class->size = size;
2504 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002505 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002506 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002507 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002508 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002509 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2510 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002511 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002512
2513 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002514 }
2515
Dan Streetmand34f6152016-05-20 16:59:56 -07002516 /* debug only, don't abort if it fails */
2517 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002518
Minchan Kim48b48002016-07-26 15:23:31 -07002519 if (zs_register_migration(pool))
2520 goto err;
2521
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002522 /*
2523 * Not critical, we still can use the pool
2524 * and user can trigger compaction manually.
2525 */
2526 if (zs_register_shrinker(pool) == 0)
2527 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002528 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002529
2530err:
2531 zs_destroy_pool(pool);
2532 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002533}
2534EXPORT_SYMBOL_GPL(zs_create_pool);
2535
2536void zs_destroy_pool(struct zs_pool *pool)
2537{
2538 int i;
2539
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002540 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002541 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002542 zs_pool_stat_destroy(pool);
2543
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002544 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002545 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002546 struct size_class *class = pool->size_class[i];
2547
2548 if (!class)
2549 continue;
2550
2551 if (class->index != i)
2552 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002553
Minchan Kim48b48002016-07-26 15:23:31 -07002554 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002555 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002556 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002557 class->size, fg);
2558 }
2559 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002560 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002561 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002562
Minchan Kim37836892016-07-26 15:23:23 -07002563 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002564 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002565 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002566 kfree(pool);
2567}
2568EXPORT_SYMBOL_GPL(zs_destroy_pool);
2569
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002570static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002571{
Minchan Kim48b48002016-07-26 15:23:31 -07002572 int ret;
2573
2574 ret = zsmalloc_mount();
2575 if (ret)
2576 goto out;
2577
2578 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002579
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002580 if (ret)
2581 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002582
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002583 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002584
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002585#ifdef CONFIG_ZPOOL
2586 zpool_register_driver(&zs_zpool_driver);
2587#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002588
Dan Streetman4abaac92016-05-26 15:16:27 -07002589 zs_stat_init();
2590
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002591 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002592
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002593notifier_fail:
2594 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002595 zsmalloc_unmount();
2596out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002597 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002598}
Nitin Gupta61989a82012-01-09 16:51:56 -06002599
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002600static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002601{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002602#ifdef CONFIG_ZPOOL
2603 zpool_unregister_driver(&zs_zpool_driver);
2604#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002605 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002606 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002607
2608 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002609}
Ben Hutchings069f1012012-06-20 02:31:11 +01002610
2611module_init(zs_init);
2612module_exit(zs_exit);
2613
2614MODULE_LICENSE("Dual BSD/GPL");
2615MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");