blob: f7adb5d9d4454dbb116dfb3e66aae6e845b1c2fe [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,
Vinayak Menonfda5cc72018-04-03 16:32:26 +0530356 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE|__GFP_CMA));
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,
Vinayak Menonfda5cc72018-04-03 16:32:26 +0530367 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE|__GFP_CMA));
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
Nick Desaulniersced00e32017-07-10 15:47:26 -0700477static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600478{
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
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700562/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700563static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700564 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700565{
Minchan Kim48b48002016-07-26 15:23:31 -0700566 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700567}
568
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700569/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700570static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700571 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700572{
Minchan Kim48b48002016-07-26 15:23:31 -0700573 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700574}
575
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700576/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700577static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcke0dbfde62017-09-08 16:13:02 -0700578 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700579{
Minchan Kim48b48002016-07-26 15:23:31 -0700580 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700581}
582
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700583#ifdef CONFIG_ZSMALLOC_STAT
584
Dan Streetman4abaac92016-05-26 15:16:27 -0700585static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700586{
Dan Streetman4abaac92016-05-26 15:16:27 -0700587 if (!debugfs_initialized()) {
588 pr_warn("debugfs not available, stat dir not created\n");
589 return;
590 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700591
592 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
593 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700594 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700595}
596
597static void __exit zs_stat_exit(void)
598{
599 debugfs_remove_recursive(zs_stat_root);
600}
601
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700602static unsigned long zs_can_compact(struct size_class *class);
603
Minchan Kim248ca1b2015-04-15 16:15:42 -0700604static int zs_stats_size_show(struct seq_file *s, void *v)
605{
606 int i;
607 struct zs_pool *pool = s->private;
608 struct size_class *class;
609 int objs_per_zspage;
610 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700611 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700612 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
613 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700614 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700615
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700616 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700617 "class", "size", "almost_full", "almost_empty",
618 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700619 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700620
621 for (i = 0; i < zs_size_classes; i++) {
622 class = pool->size_class[i];
623
624 if (class->index != i)
625 continue;
626
627 spin_lock(&class->lock);
628 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
629 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
630 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
631 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700632 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700633 spin_unlock(&class->lock);
634
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700635 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700636 pages_used = obj_allocated / objs_per_zspage *
637 class->pages_per_zspage;
638
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700639 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
640 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700641 i, class->size, class_almost_full, class_almost_empty,
642 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700643 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700644
645 total_class_almost_full += class_almost_full;
646 total_class_almost_empty += class_almost_empty;
647 total_objs += obj_allocated;
648 total_used_objs += obj_used;
649 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700650 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700651 }
652
653 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700654 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700655 "Total", "", total_class_almost_full,
656 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700657 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700658
659 return 0;
660}
661
662static int zs_stats_size_open(struct inode *inode, struct file *file)
663{
664 return single_open(file, zs_stats_size_show, inode->i_private);
665}
666
667static const struct file_operations zs_stat_size_ops = {
668 .open = zs_stats_size_open,
669 .read = seq_read,
670 .llseek = seq_lseek,
671 .release = single_release,
672};
673
Dan Streetmand34f6152016-05-20 16:59:56 -0700674static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700675{
676 struct dentry *entry;
677
Dan Streetman4abaac92016-05-26 15:16:27 -0700678 if (!zs_stat_root) {
679 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700680 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700681 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700682
683 entry = debugfs_create_dir(name, zs_stat_root);
684 if (!entry) {
685 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700686 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700687 }
688 pool->stat_dentry = entry;
689
690 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
691 pool->stat_dentry, pool, &zs_stat_size_ops);
692 if (!entry) {
693 pr_warn("%s: debugfs file entry <%s> creation failed\n",
694 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700695 debugfs_remove_recursive(pool->stat_dentry);
696 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700697 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700698}
699
700static void zs_pool_stat_destroy(struct zs_pool *pool)
701{
702 debugfs_remove_recursive(pool->stat_dentry);
703}
704
705#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700706static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700707{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700708}
709
710static void __exit zs_stat_exit(void)
711{
712}
713
Dan Streetmand34f6152016-05-20 16:59:56 -0700714static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700715{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700716}
717
718static inline void zs_pool_stat_destroy(struct zs_pool *pool)
719{
720}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700721#endif
722
Minchan Kim48b48002016-07-26 15:23:31 -0700723
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900724/*
725 * For each size class, zspages are divided into different groups
726 * depending on how "full" they are. This was done so that we could
727 * easily find empty or nearly empty zspages when we try to shrink
728 * the pool (not yet implemented). This function returns fullness
729 * status of the given page.
730 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700731static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700732 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600733{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700734 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600735 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700736
Minchan Kim37836892016-07-26 15:23:23 -0700737 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700738 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600739
740 if (inuse == 0)
741 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700742 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600743 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700744 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600745 fg = ZS_ALMOST_EMPTY;
746 else
747 fg = ZS_ALMOST_FULL;
748
749 return fg;
750}
751
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900752/*
753 * Each size class maintains various freelists and zspages are assigned
754 * to one of these freelists based on the number of live objects they
755 * have. This functions inserts the given zspage into the freelist
756 * identified by <class, fullness_group>.
757 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700758static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700759 struct zspage *zspage,
760 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600761{
Minchan Kim37836892016-07-26 15:23:23 -0700762 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600763
Minchan Kim48b48002016-07-26 15:23:31 -0700764 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700765 head = list_first_entry_or_null(&class->fullness_list[fullness],
766 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700767 /*
Minchan Kim37836892016-07-26 15:23:23 -0700768 * We want to see more ZS_FULL pages and less almost empty/full.
769 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700770 */
Minchan Kim37836892016-07-26 15:23:23 -0700771 if (head) {
772 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
773 list_add(&zspage->list, &head->list);
774 return;
775 }
776 }
777 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600778}
779
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900780/*
781 * This function removes the given zspage from the freelist identified
782 * by <class, fullness_group>.
783 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700784static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700785 struct zspage *zspage,
786 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600787{
Minchan Kim37836892016-07-26 15:23:23 -0700788 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700789 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600790
Minchan Kim37836892016-07-26 15:23:23 -0700791 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700792 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600793}
794
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900795/*
796 * Each size class maintains zspages in different fullness groups depending
797 * on the number of live objects they contain. When allocating or freeing
798 * objects, the fullness status of the page can change, say, from ALMOST_FULL
799 * to ALMOST_EMPTY when freeing an object. This function checks if such
800 * a status change has occurred for the given page and accordingly moves the
801 * page from the freelist of the old fullness group to that of the new
802 * fullness group.
803 */
Minchan Kimc7806262015-04-15 16:15:26 -0700804static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700805 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600806{
807 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600808 enum fullness_group currfg, newfg;
809
Minchan Kim37836892016-07-26 15:23:23 -0700810 get_zspage_mapping(zspage, &class_idx, &currfg);
811 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600812 if (newfg == currfg)
813 goto out;
814
Minchan Kim48b48002016-07-26 15:23:31 -0700815 if (!is_zspage_isolated(zspage)) {
816 remove_zspage(class, zspage, currfg);
817 insert_zspage(class, zspage, newfg);
818 }
819
Minchan Kim37836892016-07-26 15:23:23 -0700820 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600821
822out:
823 return newfg;
824}
825
826/*
827 * We have to decide on how many pages to link together
828 * to form a zspage for each size class. This is important
829 * to reduce wastage due to unusable space left at end of
830 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700831 * wastage = Zp % class_size
832 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600833 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
834 *
835 * For example, for size class of 3/8 * PAGE_SIZE, we should
836 * link together 3 PAGE_SIZE sized pages to form a zspage
837 * since then we can perfectly fit in 8 such objects.
838 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900839static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600840{
841 int i, max_usedpc = 0;
842 /* zspage order which gives maximum used size per KB */
843 int max_usedpc_order = 1;
844
Seth Jennings84d4faa2012-03-05 11:33:21 -0600845 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600846 int zspage_size;
847 int waste, usedpc;
848
849 zspage_size = i * PAGE_SIZE;
850 waste = zspage_size % class_size;
851 usedpc = (zspage_size - waste) * 100 / zspage_size;
852
853 if (usedpc > max_usedpc) {
854 max_usedpc = usedpc;
855 max_usedpc_order = i;
856 }
857 }
858
859 return max_usedpc_order;
860}
861
Minchan Kim37836892016-07-26 15:23:23 -0700862static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600863{
Minchan Kim48b48002016-07-26 15:23:31 -0700864 struct zspage *zspage = (struct zspage *)page->private;
865
866 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
867 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600868}
869
870static struct page *get_next_page(struct page *page)
871{
Minchan Kim48b48002016-07-26 15:23:31 -0700872 if (unlikely(PageHugeObject(page)))
873 return NULL;
874
875 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600876}
877
Minchan Kimbfd093f2016-07-26 15:23:28 -0700878/**
879 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
880 * @page: page object resides in zspage
881 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800882 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700883static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700884 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600885{
Minchan Kim312fcae2015-04-15 16:15:30 -0700886 obj >>= OBJ_TAG_BITS;
887 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
888 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600889}
890
Minchan Kimbfd093f2016-07-26 15:23:28 -0700891/**
892 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
893 * @page: page object resides in zspage
894 * @obj_idx: object index
895 */
896static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
897{
898 unsigned long obj;
899
900 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
901 obj |= obj_idx & OBJ_INDEX_MASK;
902 obj <<= OBJ_TAG_BITS;
903
904 return obj;
905}
906
Minchan Kim2e40e162015-04-15 16:15:23 -0700907static unsigned long handle_to_obj(unsigned long handle)
908{
909 return *(unsigned long *)handle;
910}
911
Minchan Kim48b48002016-07-26 15:23:31 -0700912static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700913{
Minchan Kim48b48002016-07-26 15:23:31 -0700914 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700915 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700916 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700917 } else
918 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700919}
920
Minchan Kim48b48002016-07-26 15:23:31 -0700921static inline int testpin_tag(unsigned long handle)
922{
923 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
924}
925
Minchan Kim312fcae2015-04-15 16:15:30 -0700926static inline int trypin_tag(unsigned long handle)
927{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700928 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700929}
930
931static void pin_tag(unsigned long handle)
932{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700933 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700934}
935
936static void unpin_tag(unsigned long handle)
937{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700938 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700939}
940
Nitin Guptaf4477e92012-04-02 09:13:56 -0500941static void reset_page(struct page *page)
942{
Minchan Kim48b48002016-07-26 15:23:31 -0700943 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700944 ClearPagePrivate(page);
945 ClearPagePrivate2(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500946 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700947 page_mapcount_reset(page);
948 ClearPageHugeObject(page);
949 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500950}
951
Minchan Kim48b48002016-07-26 15:23:31 -0700952/*
953 * To prevent zspage destroy during migration, zspage freeing should
954 * hold locks of all pages in the zspage.
955 */
956void lock_zspage(struct zspage *zspage)
957{
958 struct page *page = get_first_page(zspage);
959
960 do {
961 lock_page(page);
962 } while ((page = get_next_page(page)) != NULL);
963}
964
965int trylock_zspage(struct zspage *zspage)
966{
967 struct page *cursor, *fail;
968
969 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
970 get_next_page(cursor)) {
971 if (!trylock_page(cursor)) {
972 fail = cursor;
973 goto unlock;
974 }
975 }
976
977 return 1;
978unlock:
979 for (cursor = get_first_page(zspage); cursor != fail; cursor =
980 get_next_page(cursor))
981 unlock_page(cursor);
982
983 return 0;
984}
985
986static void __free_zspage(struct zs_pool *pool, struct size_class *class,
987 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600988{
Minchan Kim37836892016-07-26 15:23:23 -0700989 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700990 enum fullness_group fg;
991 unsigned int class_idx;
992
993 get_zspage_mapping(zspage, &class_idx, &fg);
994
995 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600996
Minchan Kim37836892016-07-26 15:23:23 -0700997 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700998 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600999
Minchan Kim48b48002016-07-26 15:23:31 -07001000 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001001 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001002 VM_BUG_ON_PAGE(!PageLocked(page), page);
1003 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001004 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001005 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001006 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001007 put_page(page);
1008 page = next;
1009 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001010
Minchan Kim37836892016-07-26 15:23:23 -07001011 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001012
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001013 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001014 atomic_long_sub(class->pages_per_zspage,
1015 &pool->pages_allocated);
1016}
1017
1018static void free_zspage(struct zs_pool *pool, struct size_class *class,
1019 struct zspage *zspage)
1020{
1021 VM_BUG_ON(get_zspage_inuse(zspage));
1022 VM_BUG_ON(list_empty(&zspage->list));
1023
1024 if (!trylock_zspage(zspage)) {
1025 kick_deferred_free(pool);
1026 return;
1027 }
1028
1029 remove_zspage(class, zspage, ZS_EMPTY);
1030 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001031}
1032
1033/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001034static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001035{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001036 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001037 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001038 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001039
Nitin Gupta61989a82012-01-09 16:51:56 -06001040 while (page) {
1041 struct page *next_page;
1042 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001043 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001044
Minchan Kim37836892016-07-26 15:23:23 -07001045 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001046
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001047 vaddr = kmap_atomic(page);
1048 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001049
Dan Streetman5538c562014-10-09 15:30:01 -07001050 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001051 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001052 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001053 }
1054
1055 /*
1056 * We now come to the last (full or partial) object on this
1057 * page, which must point to the first object on the next
1058 * page (if present)
1059 */
1060 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001061 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001062 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001063 } else {
1064 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001065 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001066 * whether it's allocated object or not.
1067 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001068 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001069 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001070 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001071 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001072 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001073 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001074
Minchan Kimbfd093f2016-07-26 15:23:28 -07001075 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001076}
1077
Minchan Kim48b48002016-07-26 15:23:31 -07001078static void create_page_chain(struct size_class *class, struct zspage *zspage,
1079 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001080{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001081 int i;
1082 struct page *page;
1083 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001084 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001085
1086 /*
1087 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001088 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001089 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001090 *
Minchan Kim37836892016-07-26 15:23:23 -07001091 * we set PG_private to identify the first page (i.e. no other sub-page
1092 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001093 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001094 for (i = 0; i < nr_pages; i++) {
1095 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001096 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001097 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001098 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001099 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001100 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001101 if (unlikely(class->objs_per_zspage == 1 &&
1102 class->pages_per_zspage == 1))
1103 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001104 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001105 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001106 }
Minchan Kim48b48002016-07-26 15:23:31 -07001107 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001108 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001109 prev_page = page;
1110 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001111}
Nitin Gupta61989a82012-01-09 16:51:56 -06001112
Minchan Kimbdb0af72016-07-26 15:23:20 -07001113/*
1114 * Allocate a zspage for the given size class
1115 */
Minchan Kim37836892016-07-26 15:23:23 -07001116static struct zspage *alloc_zspage(struct zs_pool *pool,
1117 struct size_class *class,
1118 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001119{
1120 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001121 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001122 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1123
1124 if (!zspage)
1125 return NULL;
1126
1127 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001128 zspage->magic = ZSPAGE_MAGIC;
1129 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001130
Minchan Kimbdb0af72016-07-26 15:23:20 -07001131 for (i = 0; i < class->pages_per_zspage; i++) {
1132 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001133
Minchan Kim37836892016-07-26 15:23:23 -07001134 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001135 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001136 while (--i >= 0) {
1137 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001138 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001139 }
Minchan Kim37836892016-07-26 15:23:23 -07001140 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001141 return NULL;
1142 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001143
1144 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001145 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001146 }
1147
Minchan Kim48b48002016-07-26 15:23:31 -07001148 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001149 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001150
Minchan Kim37836892016-07-26 15:23:23 -07001151 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001152}
1153
Minchan Kim37836892016-07-26 15:23:23 -07001154static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001155{
1156 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001157 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001158
Minchan Kim48b48002016-07-26 15:23:31 -07001159 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001160 zspage = list_first_entry_or_null(&class->fullness_list[i],
1161 struct zspage, list);
1162 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001163 break;
1164 }
1165
Minchan Kim37836892016-07-26 15:23:23 -07001166 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001167}
1168
Minchan Kim1b945ae2013-12-11 11:04:36 +09001169#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001170static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001171{
Seth Jenningsf5536462012-07-18 11:55:56 -05001172 /*
1173 * Make sure we don't leak memory if a cpu UP notification
1174 * and zs_init() race and both call zs_cpu_up() on the same cpu
1175 */
1176 if (area->vm)
1177 return 0;
1178 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1179 if (!area->vm)
1180 return -ENOMEM;
1181 return 0;
1182}
1183
1184static inline void __zs_cpu_down(struct mapping_area *area)
1185{
1186 if (area->vm)
1187 free_vm_area(area->vm);
1188 area->vm = NULL;
1189}
1190
1191static inline void *__zs_map_object(struct mapping_area *area,
1192 struct page *pages[2], int off, int size)
1193{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001194 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001195 area->vm_addr = area->vm->addr;
1196 return area->vm_addr + off;
1197}
1198
1199static inline void __zs_unmap_object(struct mapping_area *area,
1200 struct page *pages[2], int off, int size)
1201{
1202 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001203
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001204 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001205}
1206
Minchan Kim1b945ae2013-12-11 11:04:36 +09001207#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001208
1209static inline int __zs_cpu_up(struct mapping_area *area)
1210{
1211 /*
1212 * Make sure we don't leak memory if a cpu UP notification
1213 * and zs_init() race and both call zs_cpu_up() on the same cpu
1214 */
1215 if (area->vm_buf)
1216 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001217 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001218 if (!area->vm_buf)
1219 return -ENOMEM;
1220 return 0;
1221}
1222
1223static inline void __zs_cpu_down(struct mapping_area *area)
1224{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001225 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001226 area->vm_buf = NULL;
1227}
1228
1229static void *__zs_map_object(struct mapping_area *area,
1230 struct page *pages[2], int off, int size)
1231{
Seth Jennings5f601902012-07-02 16:15:49 -05001232 int sizes[2];
1233 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001234 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001235
Seth Jenningsf5536462012-07-18 11:55:56 -05001236 /* disable page faults to match kmap_atomic() return conditions */
1237 pagefault_disable();
1238
1239 /* no read fastpath */
1240 if (area->vm_mm == ZS_MM_WO)
1241 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001242
1243 sizes[0] = PAGE_SIZE - off;
1244 sizes[1] = size - sizes[0];
1245
Seth Jennings5f601902012-07-02 16:15:49 -05001246 /* copy object to per-cpu buffer */
1247 addr = kmap_atomic(pages[0]);
1248 memcpy(buf, addr + off, sizes[0]);
1249 kunmap_atomic(addr);
1250 addr = kmap_atomic(pages[1]);
1251 memcpy(buf + sizes[0], addr, sizes[1]);
1252 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001253out:
1254 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001255}
1256
Seth Jenningsf5536462012-07-18 11:55:56 -05001257static void __zs_unmap_object(struct mapping_area *area,
1258 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001259{
Seth Jennings5f601902012-07-02 16:15:49 -05001260 int sizes[2];
1261 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001262 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001263
Seth Jenningsf5536462012-07-18 11:55:56 -05001264 /* no write fastpath */
1265 if (area->vm_mm == ZS_MM_RO)
1266 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001267
Minchan Kim7b60a682015-04-15 16:15:39 -07001268 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001269 buf = buf + ZS_HANDLE_SIZE;
1270 size -= ZS_HANDLE_SIZE;
1271 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001272
Seth Jennings5f601902012-07-02 16:15:49 -05001273 sizes[0] = PAGE_SIZE - off;
1274 sizes[1] = size - sizes[0];
1275
1276 /* copy per-cpu buffer to object */
1277 addr = kmap_atomic(pages[0]);
1278 memcpy(addr + off, buf, sizes[0]);
1279 kunmap_atomic(addr);
1280 addr = kmap_atomic(pages[1]);
1281 memcpy(addr, buf + sizes[0], sizes[1]);
1282 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001283
1284out:
1285 /* enable page faults to match kunmap_atomic() return conditions */
1286 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001287}
Nitin Gupta61989a82012-01-09 16:51:56 -06001288
Minchan Kim1b945ae2013-12-11 11:04:36 +09001289#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001290
Nitin Gupta61989a82012-01-09 16:51:56 -06001291static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1292 void *pcpu)
1293{
Seth Jenningsf5536462012-07-18 11:55:56 -05001294 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001295 struct mapping_area *area;
1296
1297 switch (action) {
1298 case CPU_UP_PREPARE:
1299 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001300 ret = __zs_cpu_up(area);
1301 if (ret)
1302 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001303 break;
1304 case CPU_DEAD:
1305 case CPU_UP_CANCELED:
1306 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001307 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001308 break;
1309 }
1310
1311 return NOTIFY_OK;
1312}
1313
1314static struct notifier_block zs_cpu_nb = {
1315 .notifier_call = zs_cpu_notifier
1316};
1317
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001318static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001319{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001320 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001321
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301322 cpu_notifier_register_begin();
1323
1324 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001325 for_each_online_cpu(cpu) {
1326 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001327 if (notifier_to_errno(ret))
1328 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001329 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301330
1331 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001332 return notifier_to_errno(ret);
1333}
1334
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001335static void zs_unregister_cpu_notifier(void)
1336{
1337 int cpu;
1338
1339 cpu_notifier_register_begin();
1340
1341 for_each_online_cpu(cpu)
1342 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1343 __unregister_cpu_notifier(&zs_cpu_nb);
1344
1345 cpu_notifier_register_done();
1346}
1347
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001348static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001349{
1350 int nr;
1351
1352 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1353 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1354 nr += 1;
1355
1356 zs_size_classes = nr;
1357}
1358
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001359static bool can_merge(struct size_class *prev, int pages_per_zspage,
1360 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001361{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001362 if (prev->pages_per_zspage == pages_per_zspage &&
1363 prev->objs_per_zspage == objs_per_zspage)
1364 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001365
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001366 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001367}
1368
Minchan Kim37836892016-07-26 15:23:23 -07001369static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001370{
Minchan Kim37836892016-07-26 15:23:23 -07001371 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001372}
1373
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001374unsigned long zs_get_total_pages(struct zs_pool *pool)
1375{
1376 return atomic_long_read(&pool->pages_allocated);
1377}
1378EXPORT_SYMBOL_GPL(zs_get_total_pages);
1379
1380/**
1381 * zs_map_object - get address of allocated object from handle.
1382 * @pool: pool from which the object was allocated
1383 * @handle: handle returned from zs_malloc
1384 *
1385 * Before using an object allocated from zs_malloc, it must be mapped using
1386 * this function. When done with the object, it must be unmapped using
1387 * zs_unmap_object.
1388 *
1389 * Only one object can be mapped per cpu at a time. There is no protection
1390 * against nested mappings.
1391 *
1392 * This function returns with preemption and page faults disabled.
1393 */
1394void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1395 enum zs_mapmode mm)
1396{
Minchan Kim37836892016-07-26 15:23:23 -07001397 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001398 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001399 unsigned long obj, off;
1400 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001401
1402 unsigned int class_idx;
1403 enum fullness_group fg;
1404 struct size_class *class;
1405 struct mapping_area *area;
1406 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001407 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001408
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001409 /*
1410 * Because we use per-cpu mapping areas shared among the
1411 * pools/users, we can't allow mapping in interrupt context
1412 * because it can corrupt another users mappings.
1413 */
Sergey Senozhatsky16184002017-11-15 17:34:03 -08001414 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001415
Minchan Kim312fcae2015-04-15 16:15:30 -07001416 /* From now on, migration cannot move the object */
1417 pin_tag(handle);
1418
Minchan Kim2e40e162015-04-15 16:15:23 -07001419 obj = handle_to_obj(handle);
1420 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001421 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001422
1423 /* migration cannot move any subpage in this zspage */
1424 migrate_read_lock(zspage);
1425
Minchan Kim37836892016-07-26 15:23:23 -07001426 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001427 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001428 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001429
1430 area = &get_cpu_var(zs_map_area);
1431 area->vm_mm = mm;
1432 if (off + class->size <= PAGE_SIZE) {
1433 /* this object is contained entirely within a page */
1434 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001435 ret = area->vm_addr + off;
1436 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001437 }
1438
1439 /* this object spans two pages */
1440 pages[0] = page;
1441 pages[1] = get_next_page(page);
1442 BUG_ON(!pages[1]);
1443
Minchan Kim2e40e162015-04-15 16:15:23 -07001444 ret = __zs_map_object(area, pages, off, class->size);
1445out:
Minchan Kim48b48002016-07-26 15:23:31 -07001446 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001447 ret += ZS_HANDLE_SIZE;
1448
1449 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001450}
1451EXPORT_SYMBOL_GPL(zs_map_object);
1452
1453void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1454{
Minchan Kim37836892016-07-26 15:23:23 -07001455 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001456 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001457 unsigned long obj, off;
1458 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001459
1460 unsigned int class_idx;
1461 enum fullness_group fg;
1462 struct size_class *class;
1463 struct mapping_area *area;
1464
Minchan Kim2e40e162015-04-15 16:15:23 -07001465 obj = handle_to_obj(handle);
1466 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001467 zspage = get_zspage(page);
1468 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001469 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001470 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001471
1472 area = this_cpu_ptr(&zs_map_area);
1473 if (off + class->size <= PAGE_SIZE)
1474 kunmap_atomic(area->vm_addr);
1475 else {
1476 struct page *pages[2];
1477
1478 pages[0] = page;
1479 pages[1] = get_next_page(page);
1480 BUG_ON(!pages[1]);
1481
1482 __zs_unmap_object(area, pages, off, class->size);
1483 }
1484 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001485
1486 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001487 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001488}
1489EXPORT_SYMBOL_GPL(zs_unmap_object);
1490
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07001491/**
1492 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1493 * zsmalloc &size_class.
1494 * @pool: zsmalloc pool to use
1495 *
1496 * The function returns the size of the first huge class - any object of equal
1497 * or bigger size will be stored in zspage consisting of a single physical
1498 * page.
1499 *
1500 * Context: Any context.
1501 *
1502 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1503 */
1504size_t zs_huge_class_size(struct zs_pool *pool)
1505{
1506 return huge_class_size;
1507}
1508EXPORT_SYMBOL_GPL(zs_huge_class_size);
1509
Minchan Kim251cbb92016-05-20 16:59:42 -07001510static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001511 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001512{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001513 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001514 unsigned long obj;
1515 struct link_free *link;
1516
1517 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001518 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001519 void *vaddr;
1520
Minchan Kim312fcae2015-04-15 16:15:30 -07001521 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001522 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001523
1524 offset = obj * class->size;
1525 nr_page = offset >> PAGE_SHIFT;
1526 m_offset = offset & ~PAGE_MASK;
1527 m_page = get_first_page(zspage);
1528
1529 for (i = 0; i < nr_page; i++)
1530 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001531
1532 vaddr = kmap_atomic(m_page);
1533 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001534 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001535 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001536 /* record handle in the header of allocated chunk */
1537 link->handle = handle;
1538 else
Minchan Kim37836892016-07-26 15:23:23 -07001539 /* record handle to page->index */
1540 zspage->first_page->index = handle;
1541
Minchan Kimc7806262015-04-15 16:15:26 -07001542 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001543 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001544 zs_stat_inc(class, OBJ_USED, 1);
1545
Minchan Kimbfd093f2016-07-26 15:23:28 -07001546 obj = location_to_obj(m_page, obj);
1547
Minchan Kimc7806262015-04-15 16:15:26 -07001548 return obj;
1549}
1550
1551
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001552/**
1553 * zs_malloc - Allocate block of given size from pool.
1554 * @pool: pool to allocate from
1555 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001556 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001557 *
1558 * On success, handle to the allocated object is returned,
1559 * otherwise 0.
1560 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1561 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001562unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001563{
Minchan Kim2e40e162015-04-15 16:15:23 -07001564 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001565 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001566 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001567 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001568
Minchan Kim7b60a682015-04-15 16:15:39 -07001569 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001570 return 0;
1571
Minchan Kim37836892016-07-26 15:23:23 -07001572 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001573 if (!handle)
1574 return 0;
1575
1576 /* extra space in chunk to keep the handle */
1577 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001578 class = pool->size_class[get_size_class_index(size)];
1579
1580 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001581 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001582 if (likely(zspage)) {
1583 obj = obj_malloc(class, zspage, handle);
1584 /* Now move the zspage to another fullness group, if required */
1585 fix_fullness_group(class, zspage);
1586 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001587 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001588
Minchan Kim48b48002016-07-26 15:23:31 -07001589 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001590 }
1591
Minchan Kim48b48002016-07-26 15:23:31 -07001592 spin_unlock(&class->lock);
1593
1594 zspage = alloc_zspage(pool, class, gfp);
1595 if (!zspage) {
1596 cache_free_handle(pool, handle);
1597 return 0;
1598 }
1599
1600 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001601 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001602 newfg = get_fullness_group(class, zspage);
1603 insert_zspage(class, zspage, newfg);
1604 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001605 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001606 atomic_long_add(class->pages_per_zspage,
1607 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001608 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001609
1610 /* We completely set up zspage so mark them as movable */
1611 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001612 spin_unlock(&class->lock);
1613
Minchan Kim2e40e162015-04-15 16:15:23 -07001614 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001615}
1616EXPORT_SYMBOL_GPL(zs_malloc);
1617
Minchan Kim1ee47162016-05-20 16:59:45 -07001618static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001619{
1620 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001621 struct zspage *zspage;
1622 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001623 unsigned long f_offset;
1624 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001625 void *vaddr;
1626
Minchan Kim312fcae2015-04-15 16:15:30 -07001627 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001628 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001629 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001630 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001631
Minchan Kimc7806262015-04-15 16:15:26 -07001632 vaddr = kmap_atomic(f_page);
1633
1634 /* Insert this object in containing zspage's freelist */
1635 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001636 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001637 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001638 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001639 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001640 zs_stat_dec(class, OBJ_USED, 1);
1641}
1642
1643void zs_free(struct zs_pool *pool, unsigned long handle)
1644{
Minchan Kim37836892016-07-26 15:23:23 -07001645 struct zspage *zspage;
1646 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001647 unsigned long obj;
1648 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001649 int class_idx;
1650 struct size_class *class;
1651 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001652 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001653
Minchan Kim2e40e162015-04-15 16:15:23 -07001654 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001655 return;
1656
Minchan Kim312fcae2015-04-15 16:15:30 -07001657 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001658 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001659 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001660 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001661
Minchan Kim48b48002016-07-26 15:23:31 -07001662 migrate_read_lock(zspage);
1663
Minchan Kim37836892016-07-26 15:23:23 -07001664 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001665 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001666
1667 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001668 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001669 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001670 if (fullness != ZS_EMPTY) {
1671 migrate_read_unlock(zspage);
1672 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001673 }
Minchan Kim48b48002016-07-26 15:23:31 -07001674
1675 isolated = is_zspage_isolated(zspage);
1676 migrate_read_unlock(zspage);
1677 /* If zspage is isolated, zs_page_putback will free the zspage */
1678 if (likely(!isolated))
1679 free_zspage(pool, class, zspage);
1680out:
1681
Minchan Kim312fcae2015-04-15 16:15:30 -07001682 spin_unlock(&class->lock);
1683 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001684 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001685}
1686EXPORT_SYMBOL_GPL(zs_free);
1687
Minchan Kim251cbb92016-05-20 16:59:42 -07001688static void zs_object_copy(struct size_class *class, unsigned long dst,
1689 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001690{
1691 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001692 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001693 unsigned long s_off, d_off;
1694 void *s_addr, *d_addr;
1695 int s_size, d_size, size;
1696 int written = 0;
1697
1698 s_size = d_size = class->size;
1699
1700 obj_to_location(src, &s_page, &s_objidx);
1701 obj_to_location(dst, &d_page, &d_objidx);
1702
Minchan Kimbfd093f2016-07-26 15:23:28 -07001703 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1704 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001705
1706 if (s_off + class->size > PAGE_SIZE)
1707 s_size = PAGE_SIZE - s_off;
1708
1709 if (d_off + class->size > PAGE_SIZE)
1710 d_size = PAGE_SIZE - d_off;
1711
1712 s_addr = kmap_atomic(s_page);
1713 d_addr = kmap_atomic(d_page);
1714
1715 while (1) {
1716 size = min(s_size, d_size);
1717 memcpy(d_addr + d_off, s_addr + s_off, size);
1718 written += size;
1719
1720 if (written == class->size)
1721 break;
1722
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001723 s_off += size;
1724 s_size -= size;
1725 d_off += size;
1726 d_size -= size;
1727
1728 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001729 kunmap_atomic(d_addr);
1730 kunmap_atomic(s_addr);
1731 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001732 s_addr = kmap_atomic(s_page);
1733 d_addr = kmap_atomic(d_page);
1734 s_size = class->size - written;
1735 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001736 }
1737
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001738 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001739 kunmap_atomic(d_addr);
1740 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001741 d_addr = kmap_atomic(d_page);
1742 d_size = class->size - written;
1743 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001744 }
1745 }
1746
1747 kunmap_atomic(d_addr);
1748 kunmap_atomic(s_addr);
1749}
1750
1751/*
1752 * Find alloced object in zspage from index object and
1753 * return handle.
1754 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001755static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001756 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001757{
1758 unsigned long head;
1759 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001760 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001761 unsigned long handle = 0;
1762 void *addr = kmap_atomic(page);
1763
Minchan Kim37836892016-07-26 15:23:23 -07001764 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001765 offset += class->size * index;
1766
1767 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001768 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001769 if (head & OBJ_ALLOCATED_TAG) {
1770 handle = head & ~OBJ_ALLOCATED_TAG;
1771 if (trypin_tag(handle))
1772 break;
1773 handle = 0;
1774 }
1775
1776 offset += class->size;
1777 index++;
1778 }
1779
1780 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001781
1782 *obj_idx = index;
1783
Minchan Kim312fcae2015-04-15 16:15:30 -07001784 return handle;
1785}
1786
1787struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001788 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001789 struct page *s_page;
1790 /* Destination page for migration which should be a first page
1791 * of zspage. */
1792 struct page *d_page;
1793 /* Starting object index within @s_page which used for live object
1794 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001795 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001796};
1797
1798static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1799 struct zs_compact_control *cc)
1800{
1801 unsigned long used_obj, free_obj;
1802 unsigned long handle;
1803 struct page *s_page = cc->s_page;
1804 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001805 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001806 int ret = 0;
1807
1808 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001809 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001810 if (!handle) {
1811 s_page = get_next_page(s_page);
1812 if (!s_page)
1813 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001814 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001815 continue;
1816 }
1817
1818 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001819 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001820 unpin_tag(handle);
1821 ret = -ENOMEM;
1822 break;
1823 }
1824
1825 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001826 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001827 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001828 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001829 /*
1830 * record_obj updates handle's value to free_obj and it will
1831 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1832 * breaks synchronization using pin_tag(e,g, zs_free) so
1833 * let's keep the lock bit.
1834 */
1835 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001836 record_obj(handle, free_obj);
1837 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001838 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001839 }
1840
1841 /* Remember last position in this iteration */
1842 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001843 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001844
1845 return ret;
1846}
1847
Minchan Kim37836892016-07-26 15:23:23 -07001848static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001849{
1850 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001851 struct zspage *zspage;
1852 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001853
Minchan Kim37836892016-07-26 15:23:23 -07001854 if (!source) {
1855 fg[0] = ZS_ALMOST_FULL;
1856 fg[1] = ZS_ALMOST_EMPTY;
1857 }
1858
1859 for (i = 0; i < 2; i++) {
1860 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1861 struct zspage, list);
1862 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001863 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001864 remove_zspage(class, zspage, fg[i]);
1865 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001866 }
1867 }
1868
Minchan Kim37836892016-07-26 15:23:23 -07001869 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001870}
1871
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001872/*
Minchan Kim37836892016-07-26 15:23:23 -07001873 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001874 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001875 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001876 *
Minchan Kim37836892016-07-26 15:23:23 -07001877 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001878 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001879static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001880 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001881{
Minchan Kim312fcae2015-04-15 16:15:30 -07001882 enum fullness_group fullness;
1883
Minchan Kim48b48002016-07-26 15:23:31 -07001884 VM_BUG_ON(is_zspage_isolated(zspage));
1885
Minchan Kim37836892016-07-26 15:23:23 -07001886 fullness = get_fullness_group(class, zspage);
1887 insert_zspage(class, zspage, fullness);
1888 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001889
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001890 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001891}
1892
Minchan Kim48b48002016-07-26 15:23:31 -07001893#ifdef CONFIG_COMPACTION
1894static struct dentry *zs_mount(struct file_system_type *fs_type,
1895 int flags, const char *dev_name, void *data)
1896{
1897 static const struct dentry_operations ops = {
1898 .d_dname = simple_dname,
1899 };
1900
1901 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1902}
1903
1904static struct file_system_type zsmalloc_fs = {
1905 .name = "zsmalloc",
1906 .mount = zs_mount,
1907 .kill_sb = kill_anon_super,
1908};
1909
1910static int zsmalloc_mount(void)
1911{
1912 int ret = 0;
1913
1914 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1915 if (IS_ERR(zsmalloc_mnt))
1916 ret = PTR_ERR(zsmalloc_mnt);
1917
1918 return ret;
1919}
1920
1921static void zsmalloc_unmount(void)
1922{
1923 kern_unmount(zsmalloc_mnt);
1924}
1925
1926static void migrate_lock_init(struct zspage *zspage)
1927{
1928 rwlock_init(&zspage->lock);
1929}
1930
1931static void migrate_read_lock(struct zspage *zspage)
1932{
1933 read_lock(&zspage->lock);
1934}
1935
1936static void migrate_read_unlock(struct zspage *zspage)
1937{
1938 read_unlock(&zspage->lock);
1939}
1940
1941static void migrate_write_lock(struct zspage *zspage)
1942{
1943 write_lock(&zspage->lock);
1944}
1945
1946static void migrate_write_unlock(struct zspage *zspage)
1947{
1948 write_unlock(&zspage->lock);
1949}
1950
1951/* Number of isolated subpage for *page migration* in this zspage */
1952static void inc_zspage_isolation(struct zspage *zspage)
1953{
1954 zspage->isolated++;
1955}
1956
1957static void dec_zspage_isolation(struct zspage *zspage)
1958{
1959 zspage->isolated--;
1960}
1961
1962static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1963 struct page *newpage, struct page *oldpage)
1964{
1965 struct page *page;
1966 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1967 int idx = 0;
1968
1969 page = get_first_page(zspage);
1970 do {
1971 if (page == oldpage)
1972 pages[idx] = newpage;
1973 else
1974 pages[idx] = page;
1975 idx++;
1976 } while ((page = get_next_page(page)) != NULL);
1977
1978 create_page_chain(class, zspage, pages);
1979 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1980 if (unlikely(PageHugeObject(oldpage)))
1981 newpage->index = oldpage->index;
1982 __SetPageMovable(newpage, page_mapping(oldpage));
1983}
1984
1985bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1986{
1987 struct zs_pool *pool;
1988 struct size_class *class;
1989 int class_idx;
1990 enum fullness_group fullness;
1991 struct zspage *zspage;
1992 struct address_space *mapping;
1993
1994 /*
1995 * Page is locked so zspage couldn't be destroyed. For detail, look at
1996 * lock_zspage in free_zspage.
1997 */
1998 VM_BUG_ON_PAGE(!PageMovable(page), page);
1999 VM_BUG_ON_PAGE(PageIsolated(page), page);
2000
2001 zspage = get_zspage(page);
2002
2003 /*
2004 * Without class lock, fullness could be stale while class_idx is okay
2005 * because class_idx is constant unless page is freed so we should get
2006 * fullness again under class lock.
2007 */
2008 get_zspage_mapping(zspage, &class_idx, &fullness);
2009 mapping = page_mapping(page);
2010 pool = mapping->private_data;
2011 class = pool->size_class[class_idx];
2012
2013 spin_lock(&class->lock);
2014 if (get_zspage_inuse(zspage) == 0) {
2015 spin_unlock(&class->lock);
2016 return false;
2017 }
2018
2019 /* zspage is isolated for object migration */
2020 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2021 spin_unlock(&class->lock);
2022 return false;
2023 }
2024
2025 /*
2026 * If this is first time isolation for the zspage, isolate zspage from
2027 * size_class to prevent further object allocation from the zspage.
2028 */
2029 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2030 get_zspage_mapping(zspage, &class_idx, &fullness);
2031 remove_zspage(class, zspage, fullness);
2032 }
2033
2034 inc_zspage_isolation(zspage);
2035 spin_unlock(&class->lock);
2036
2037 return true;
2038}
2039
2040int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2041 struct page *page, enum migrate_mode mode)
2042{
2043 struct zs_pool *pool;
2044 struct size_class *class;
2045 int class_idx;
2046 enum fullness_group fullness;
2047 struct zspage *zspage;
2048 struct page *dummy;
2049 void *s_addr, *d_addr, *addr;
2050 int offset, pos;
2051 unsigned long handle, head;
2052 unsigned long old_obj, new_obj;
2053 unsigned int obj_idx;
2054 int ret = -EAGAIN;
2055
2056 VM_BUG_ON_PAGE(!PageMovable(page), page);
2057 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2058
2059 zspage = get_zspage(page);
2060
2061 /* Concurrent compactor cannot migrate any subpage in zspage */
2062 migrate_write_lock(zspage);
2063 get_zspage_mapping(zspage, &class_idx, &fullness);
2064 pool = mapping->private_data;
2065 class = pool->size_class[class_idx];
2066 offset = get_first_obj_offset(page);
2067
2068 spin_lock(&class->lock);
2069 if (!get_zspage_inuse(zspage)) {
2070 ret = -EBUSY;
2071 goto unlock_class;
2072 }
2073
2074 pos = offset;
2075 s_addr = kmap_atomic(page);
2076 while (pos < PAGE_SIZE) {
2077 head = obj_to_head(page, s_addr + pos);
2078 if (head & OBJ_ALLOCATED_TAG) {
2079 handle = head & ~OBJ_ALLOCATED_TAG;
2080 if (!trypin_tag(handle))
2081 goto unpin_objects;
2082 }
2083 pos += class->size;
2084 }
2085
2086 /*
2087 * Here, any user cannot access all objects in the zspage so let's move.
2088 */
2089 d_addr = kmap_atomic(newpage);
2090 memcpy(d_addr, s_addr, PAGE_SIZE);
2091 kunmap_atomic(d_addr);
2092
2093 for (addr = s_addr + offset; addr < s_addr + pos;
2094 addr += class->size) {
2095 head = obj_to_head(page, addr);
2096 if (head & OBJ_ALLOCATED_TAG) {
2097 handle = head & ~OBJ_ALLOCATED_TAG;
2098 if (!testpin_tag(handle))
2099 BUG();
2100
2101 old_obj = handle_to_obj(handle);
2102 obj_to_location(old_obj, &dummy, &obj_idx);
2103 new_obj = (unsigned long)location_to_obj(newpage,
2104 obj_idx);
2105 new_obj |= BIT(HANDLE_PIN_BIT);
2106 record_obj(handle, new_obj);
2107 }
2108 }
2109
2110 replace_sub_page(class, zspage, newpage, page);
2111 get_page(newpage);
2112
2113 dec_zspage_isolation(zspage);
2114
2115 /*
2116 * Page migration is done so let's putback isolated zspage to
2117 * the list if @page is final isolated subpage in the zspage.
2118 */
2119 if (!is_zspage_isolated(zspage))
2120 putback_zspage(class, zspage);
2121
2122 reset_page(page);
2123 put_page(page);
2124 page = newpage;
2125
Minchan Kimdd4123f2016-07-26 15:26:50 -07002126 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002127unpin_objects:
2128 for (addr = s_addr + offset; addr < s_addr + pos;
2129 addr += class->size) {
2130 head = obj_to_head(page, addr);
2131 if (head & OBJ_ALLOCATED_TAG) {
2132 handle = head & ~OBJ_ALLOCATED_TAG;
2133 if (!testpin_tag(handle))
2134 BUG();
2135 unpin_tag(handle);
2136 }
2137 }
2138 kunmap_atomic(s_addr);
2139unlock_class:
2140 spin_unlock(&class->lock);
2141 migrate_write_unlock(zspage);
2142
2143 return ret;
2144}
2145
2146void zs_page_putback(struct page *page)
2147{
2148 struct zs_pool *pool;
2149 struct size_class *class;
2150 int class_idx;
2151 enum fullness_group fg;
2152 struct address_space *mapping;
2153 struct zspage *zspage;
2154
2155 VM_BUG_ON_PAGE(!PageMovable(page), page);
2156 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2157
2158 zspage = get_zspage(page);
2159 get_zspage_mapping(zspage, &class_idx, &fg);
2160 mapping = page_mapping(page);
2161 pool = mapping->private_data;
2162 class = pool->size_class[class_idx];
2163
2164 spin_lock(&class->lock);
2165 dec_zspage_isolation(zspage);
2166 if (!is_zspage_isolated(zspage)) {
2167 fg = putback_zspage(class, zspage);
2168 /*
2169 * Due to page_lock, we cannot free zspage immediately
2170 * so let's defer.
2171 */
2172 if (fg == ZS_EMPTY)
2173 schedule_work(&pool->free_work);
2174 }
2175 spin_unlock(&class->lock);
2176}
2177
2178const struct address_space_operations zsmalloc_aops = {
2179 .isolate_page = zs_page_isolate,
2180 .migratepage = zs_page_migrate,
2181 .putback_page = zs_page_putback,
2182};
2183
2184static int zs_register_migration(struct zs_pool *pool)
2185{
2186 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2187 if (IS_ERR(pool->inode)) {
2188 pool->inode = NULL;
2189 return 1;
2190 }
2191
2192 pool->inode->i_mapping->private_data = pool;
2193 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2194 return 0;
2195}
2196
2197static void zs_unregister_migration(struct zs_pool *pool)
2198{
2199 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002200 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002201}
2202
2203/*
2204 * Caller should hold page_lock of all pages in the zspage
2205 * In here, we cannot use zspage meta data.
2206 */
2207static void async_free_zspage(struct work_struct *work)
2208{
2209 int i;
2210 struct size_class *class;
2211 unsigned int class_idx;
2212 enum fullness_group fullness;
2213 struct zspage *zspage, *tmp;
2214 LIST_HEAD(free_pages);
2215 struct zs_pool *pool = container_of(work, struct zs_pool,
2216 free_work);
2217
2218 for (i = 0; i < zs_size_classes; i++) {
2219 class = pool->size_class[i];
2220 if (class->index != i)
2221 continue;
2222
2223 spin_lock(&class->lock);
2224 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2225 spin_unlock(&class->lock);
2226 }
2227
2228
2229 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2230 list_del(&zspage->list);
2231 lock_zspage(zspage);
2232
2233 get_zspage_mapping(zspage, &class_idx, &fullness);
2234 VM_BUG_ON(fullness != ZS_EMPTY);
2235 class = pool->size_class[class_idx];
2236 spin_lock(&class->lock);
2237 __free_zspage(pool, pool->size_class[class_idx], zspage);
2238 spin_unlock(&class->lock);
2239 }
2240};
2241
2242static void kick_deferred_free(struct zs_pool *pool)
2243{
2244 schedule_work(&pool->free_work);
2245}
2246
2247static void init_deferred_free(struct zs_pool *pool)
2248{
2249 INIT_WORK(&pool->free_work, async_free_zspage);
2250}
2251
2252static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2253{
2254 struct page *page = get_first_page(zspage);
2255
2256 do {
2257 WARN_ON(!trylock_page(page));
2258 __SetPageMovable(page, pool->inode->i_mapping);
2259 unlock_page(page);
2260 } while ((page = get_next_page(page)) != NULL);
2261}
2262#endif
2263
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002264/*
2265 *
2266 * Based on the number of unused allocated objects calculate
2267 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002268 */
2269static unsigned long zs_can_compact(struct size_class *class)
2270{
2271 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002272 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2273 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002274
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002275 if (obj_allocated <= obj_used)
2276 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002277
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002278 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002279 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002280
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002281 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002282}
2283
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002284static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002285{
Minchan Kim312fcae2015-04-15 16:15:30 -07002286 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002287 struct zspage *src_zspage;
2288 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002289
Minchan Kim312fcae2015-04-15 16:15:30 -07002290 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002291 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002292
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002293 if (!zs_can_compact(class))
2294 break;
2295
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002296 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002297 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002298
Minchan Kim37836892016-07-26 15:23:23 -07002299 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002300 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002301 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002302 * If there is no more space in dst_page, resched
2303 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002304 */
2305 if (!migrate_zspage(pool, class, &cc))
2306 break;
2307
Minchan Kim4aa409c2016-07-26 15:23:26 -07002308 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002309 }
2310
2311 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002312 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002313 break;
2314
Minchan Kim4aa409c2016-07-26 15:23:26 -07002315 putback_zspage(class, dst_zspage);
2316 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002317 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002318 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002319 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002320 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002321 cond_resched();
2322 spin_lock(&class->lock);
2323 }
2324
Minchan Kim37836892016-07-26 15:23:23 -07002325 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002326 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002327
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002328 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002329}
2330
2331unsigned long zs_compact(struct zs_pool *pool)
2332{
2333 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002334 struct size_class *class;
2335
2336 for (i = zs_size_classes - 1; i >= 0; i--) {
2337 class = pool->size_class[i];
2338 if (!class)
2339 continue;
2340 if (class->index != i)
2341 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002342 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002343 }
2344
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002345 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002346}
2347EXPORT_SYMBOL_GPL(zs_compact);
2348
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002349void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2350{
2351 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2352}
2353EXPORT_SYMBOL_GPL(zs_pool_stats);
2354
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002355static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2356 struct shrink_control *sc)
2357{
2358 unsigned long pages_freed;
2359 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2360 shrinker);
2361
2362 pages_freed = pool->stats.pages_compacted;
2363 /*
2364 * Compact classes and calculate compaction delta.
2365 * Can run concurrently with a manually triggered
2366 * (by user) compaction.
2367 */
2368 pages_freed = zs_compact(pool) - pages_freed;
2369
2370 return pages_freed ? pages_freed : SHRINK_STOP;
2371}
2372
2373static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2374 struct shrink_control *sc)
2375{
2376 int i;
2377 struct size_class *class;
2378 unsigned long pages_to_free = 0;
2379 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2380 shrinker);
2381
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002382 for (i = zs_size_classes - 1; i >= 0; i--) {
2383 class = pool->size_class[i];
2384 if (!class)
2385 continue;
2386 if (class->index != i)
2387 continue;
2388
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002389 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002390 }
2391
2392 return pages_to_free;
2393}
2394
2395static void zs_unregister_shrinker(struct zs_pool *pool)
2396{
2397 if (pool->shrinker_enabled) {
2398 unregister_shrinker(&pool->shrinker);
2399 pool->shrinker_enabled = false;
2400 }
2401}
2402
2403static int zs_register_shrinker(struct zs_pool *pool)
2404{
2405 pool->shrinker.scan_objects = zs_shrinker_scan;
2406 pool->shrinker.count_objects = zs_shrinker_count;
2407 pool->shrinker.batch = 0;
2408 pool->shrinker.seeks = DEFAULT_SEEKS;
2409
2410 return register_shrinker(&pool->shrinker);
2411}
2412
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002413/**
2414 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002415 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002416 *
2417 * This function must be called before anything when using
2418 * the zsmalloc allocator.
2419 *
2420 * On success, a pointer to the newly created pool is returned,
2421 * otherwise NULL.
2422 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002423struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002424{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002425 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002426 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002427 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002428
Ganesh Mahendran18136652014-12-12 16:57:10 -08002429 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002430 if (!pool)
2431 return NULL;
2432
Minchan Kim48b48002016-07-26 15:23:31 -07002433 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002434 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2435 GFP_KERNEL);
2436 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002437 kfree(pool);
2438 return NULL;
2439 }
2440
Minchan Kim2e40e162015-04-15 16:15:23 -07002441 pool->name = kstrdup(name, GFP_KERNEL);
2442 if (!pool->name)
2443 goto err;
2444
Minchan Kim37836892016-07-26 15:23:23 -07002445 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002446 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002447
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002448 /*
2449 * Iterate reversly, because, size of size_class that we want to use
2450 * for merging should be larger or equal to current size.
2451 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002452 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002453 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002454 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002455 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002456 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002457 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002458
2459 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2460 if (size > ZS_MAX_ALLOC_SIZE)
2461 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002462 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002463 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002464
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002465 /*
Sergey Senozhatskyda33b2a2018-04-05 16:24:43 -07002466 * We iterate from biggest down to smallest classes,
2467 * so huge_class_size holds the size of the first huge
2468 * class. Any object bigger than or equal to that will
2469 * endup in the huge class.
2470 */
2471 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2472 !huge_class_size) {
2473 huge_class_size = size;
2474 /*
2475 * The object uses ZS_HANDLE_SIZE bytes to store the
2476 * handle. We need to subtract it, because zs_malloc()
2477 * unconditionally adds handle size before it performs
2478 * size class search - so object may be smaller than
2479 * huge class size, yet it still can end up in the huge
2480 * class because it grows by ZS_HANDLE_SIZE extra bytes
2481 * right before class lookup.
2482 */
2483 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2484 }
2485
2486 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002487 * size_class is used for normal zsmalloc operation such
2488 * as alloc/free for that size. Although it is natural that we
2489 * have one size_class for each size, there is a chance that we
2490 * can get more memory utilization if we use one size_class for
2491 * many different sizes whose size_class have same
2492 * characteristics. So, we makes size_class point to
2493 * previous size_class if possible.
2494 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002495 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002496 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002497 pool->size_class[i] = prev_class;
2498 continue;
2499 }
2500 }
2501
2502 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2503 if (!class)
2504 goto err;
2505
Nitin Gupta61989a82012-01-09 16:51:56 -06002506 class->size = size;
2507 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002508 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002509 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002510 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002511 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002512 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2513 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002514 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002515
2516 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002517 }
2518
Dan Streetmand34f6152016-05-20 16:59:56 -07002519 /* debug only, don't abort if it fails */
2520 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002521
Minchan Kim48b48002016-07-26 15:23:31 -07002522 if (zs_register_migration(pool))
2523 goto err;
2524
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002525 /*
2526 * Not critical, we still can use the pool
2527 * and user can trigger compaction manually.
2528 */
2529 if (zs_register_shrinker(pool) == 0)
2530 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002531 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002532
2533err:
2534 zs_destroy_pool(pool);
2535 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002536}
2537EXPORT_SYMBOL_GPL(zs_create_pool);
2538
2539void zs_destroy_pool(struct zs_pool *pool)
2540{
2541 int i;
2542
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002543 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002544 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002545 zs_pool_stat_destroy(pool);
2546
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002547 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002548 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002549 struct size_class *class = pool->size_class[i];
2550
2551 if (!class)
2552 continue;
2553
2554 if (class->index != i)
2555 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002556
Minchan Kim48b48002016-07-26 15:23:31 -07002557 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002558 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002559 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002560 class->size, fg);
2561 }
2562 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002563 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002564 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002565
Minchan Kim37836892016-07-26 15:23:23 -07002566 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002567 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002568 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002569 kfree(pool);
2570}
2571EXPORT_SYMBOL_GPL(zs_destroy_pool);
2572
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002573static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002574{
Minchan Kim48b48002016-07-26 15:23:31 -07002575 int ret;
2576
2577 ret = zsmalloc_mount();
2578 if (ret)
2579 goto out;
2580
2581 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002582
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002583 if (ret)
2584 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002585
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002586 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002587
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002588#ifdef CONFIG_ZPOOL
2589 zpool_register_driver(&zs_zpool_driver);
2590#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002591
Dan Streetman4abaac92016-05-26 15:16:27 -07002592 zs_stat_init();
2593
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002594 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002595
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002596notifier_fail:
2597 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002598 zsmalloc_unmount();
2599out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002600 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002601}
Nitin Gupta61989a82012-01-09 16:51:56 -06002602
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002603static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002604{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002605#ifdef CONFIG_ZPOOL
2606 zpool_unregister_driver(&zs_zpool_driver);
2607#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002608 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002609 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002610
2611 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002612}
Ben Hutchings069f1012012-06-20 02:31:11 +01002613
2614module_init(zs_init);
2615module_exit(zs_exit);
2616
2617MODULE_LICENSE("Dual BSD/GPL");
2618MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");