blob: 308acb9d814b68d006775cb396479c2f328d9865 [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
Xishi Qiu399d8ee2017-02-22 15:45:01 -080027 * PG_owner_priv_1: identifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Nitin Gupta61989a82012-01-09 16:51:56 -060033#include <linux/module.h>
34#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070035#include <linux/sched.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010036#include <linux/magic.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/string.h>
41#include <linux/slab.h>
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
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700119#define FULLNESS_BITS 2
120#define CLASS_BITS 8
121#define ISOLATED_BITS 3
122#define MAGIC_VAL_BITS 8
123
Seth Jennings0959c632012-08-08 15:12:17 +0900124#define MAX(a, b) ((a) >= (b) ? (a) : (b))
125/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
126#define ZS_MIN_ALLOC_SIZE \
127 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700128/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700129#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900130
131/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700132 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900133 * trader-off here:
134 * - Large number of size classes is potentially wasteful as free page are
135 * spread across these classes
136 * - Small number of size classes causes large internal fragmentation
137 * - Probably its better to use specific size classes (empirically
138 * determined). NOTE: all those class sizes must be set as multiple of
139 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
140 *
141 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
142 * (reason above)
143 */
Minchan Kim37836892016-07-26 15:23:23 -0700144#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700145#define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
146 ZS_SIZE_CLASS_DELTA) + 1)
Seth Jennings0959c632012-08-08 15:12:17 +0900147
Seth Jennings0959c632012-08-08 15:12:17 +0900148enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900149 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700150 ZS_ALMOST_EMPTY,
151 ZS_ALMOST_FULL,
152 ZS_FULL,
153 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900154};
155
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800156enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700157 CLASS_EMPTY,
158 CLASS_ALMOST_EMPTY,
159 CLASS_ALMOST_FULL,
160 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800161 OBJ_ALLOCATED,
162 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700163 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800164};
165
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800166struct zs_size_stat {
167 unsigned long objs[NR_ZS_STAT_TYPE];
168};
169
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700170#ifdef CONFIG_ZSMALLOC_STAT
171static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800172#endif
173
Minchan Kim48b48002016-07-26 15:23:31 -0700174#ifdef CONFIG_COMPACTION
175static struct vfsmount *zsmalloc_mnt;
176#endif
177
Seth Jennings0959c632012-08-08 15:12:17 +0900178/*
179 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
180 * n <= N / f, where
181 * n = number of allocated objects
182 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700183 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900184 *
185 * Similarly, we assign zspage to:
186 * ZS_ALMOST_FULL when n > N / f
187 * ZS_EMPTY when n == 0
188 * ZS_FULL when n == N
189 *
190 * (see: fix_fullness_group())
191 */
192static const int fullness_threshold_frac = 4;
193
194struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700195 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700196 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900197 /*
198 * Size of objects stored in this class. Must be multiple
199 * of ZS_ALIGN.
200 */
201 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700202 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800203 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
204 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700205
206 unsigned int index;
207 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900208};
209
Minchan Kim48b48002016-07-26 15:23:31 -0700210/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
211static void SetPageHugeObject(struct page *page)
212{
213 SetPageOwnerPriv1(page);
214}
215
216static void ClearPageHugeObject(struct page *page)
217{
218 ClearPageOwnerPriv1(page);
219}
220
221static int PageHugeObject(struct page *page)
222{
223 return PageOwnerPriv1(page);
224}
225
Seth Jennings0959c632012-08-08 15:12:17 +0900226/*
227 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700228 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900229 *
230 * This must be power of 2 and less than or equal to ZS_ALIGN
231 */
232struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700233 union {
234 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700235 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700236 * It's valid for non-allocated object
237 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700238 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700239 /*
240 * Handle of allocated object.
241 */
242 unsigned long handle;
243 };
Seth Jennings0959c632012-08-08 15:12:17 +0900244};
245
246struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800247 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800248
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700249 struct size_class *size_class[ZS_SIZE_CLASSES];
Minchan Kim2e40e162015-04-15 16:15:23 -0700250 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700251 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900252
Minchan Kim13de8932014-10-09 15:29:48 -0700253 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800254
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700255 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700256
257 /* Compact classes */
258 struct shrinker shrinker;
259 /*
260 * To signify that register_shrinker() was successful
261 * and unregister_shrinker() will not Oops.
262 */
263 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800264#ifdef CONFIG_ZSMALLOC_STAT
265 struct dentry *stat_dentry;
266#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700267#ifdef CONFIG_COMPACTION
268 struct inode *inode;
269 struct work_struct free_work;
270#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900271};
Nitin Gupta61989a82012-01-09 16:51:56 -0600272
Minchan Kim37836892016-07-26 15:23:23 -0700273struct zspage {
274 struct {
275 unsigned int fullness:FULLNESS_BITS;
Minchan Kim85d492f2017-04-13 14:56:40 -0700276 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700277 unsigned int isolated:ISOLATED_BITS;
278 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700279 };
280 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700281 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700282 struct page *first_page;
283 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700284#ifdef CONFIG_COMPACTION
285 rwlock_t lock;
286#endif
Minchan Kim37836892016-07-26 15:23:23 -0700287};
Nitin Gupta61989a82012-01-09 16:51:56 -0600288
Seth Jenningsf5536462012-07-18 11:55:56 -0500289struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900290#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500291 struct vm_struct *vm; /* vm area for mapping object that span pages */
292#else
293 char *vm_buf; /* copy buffer for objects that span pages */
294#endif
295 char *vm_addr; /* address of kmap_atomic()'ed pages */
296 enum zs_mapmode vm_mm; /* mapping mode */
297};
298
Minchan Kim48b48002016-07-26 15:23:31 -0700299#ifdef CONFIG_COMPACTION
300static int zs_register_migration(struct zs_pool *pool);
301static void zs_unregister_migration(struct zs_pool *pool);
302static void migrate_lock_init(struct zspage *zspage);
303static void migrate_read_lock(struct zspage *zspage);
304static void migrate_read_unlock(struct zspage *zspage);
305static void kick_deferred_free(struct zs_pool *pool);
306static void init_deferred_free(struct zs_pool *pool);
307static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
308#else
309static int zsmalloc_mount(void) { return 0; }
310static void zsmalloc_unmount(void) {}
311static int zs_register_migration(struct zs_pool *pool) { return 0; }
312static void zs_unregister_migration(struct zs_pool *pool) {}
313static void migrate_lock_init(struct zspage *zspage) {}
314static void migrate_read_lock(struct zspage *zspage) {}
315static void migrate_read_unlock(struct zspage *zspage) {}
316static void kick_deferred_free(struct zs_pool *pool) {}
317static void init_deferred_free(struct zs_pool *pool) {}
318static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
319#endif
320
Minchan Kim37836892016-07-26 15:23:23 -0700321static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700322{
323 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
324 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700325 if (!pool->handle_cachep)
326 return 1;
327
328 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
329 0, 0, NULL);
330 if (!pool->zspage_cachep) {
331 kmem_cache_destroy(pool->handle_cachep);
332 pool->handle_cachep = NULL;
333 return 1;
334 }
335
336 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700337}
338
Minchan Kim37836892016-07-26 15:23:23 -0700339static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700340{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700341 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700342 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700343}
344
Minchan Kim37836892016-07-26 15:23:23 -0700345static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700346{
347 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700348 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700349}
350
Minchan Kim37836892016-07-26 15:23:23 -0700351static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700352{
353 kmem_cache_free(pool->handle_cachep, (void *)handle);
354}
355
Minchan Kim37836892016-07-26 15:23:23 -0700356static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
357{
Minchan Kim48b48002016-07-26 15:23:31 -0700358 return kmem_cache_alloc(pool->zspage_cachep,
359 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800360}
Minchan Kim37836892016-07-26 15:23:23 -0700361
362static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
363{
364 kmem_cache_free(pool->zspage_cachep, zspage);
365}
366
Minchan Kim2e40e162015-04-15 16:15:23 -0700367static void record_obj(unsigned long handle, unsigned long obj)
368{
Junil Leec102f072016-01-20 14:58:18 -0800369 /*
370 * lsb of @obj represents handle lock while other bits
371 * represent object value the handle is pointing so
372 * updating shouldn't do store tearing.
373 */
374 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700375}
376
Dan Streetmanc7957792014-08-06 16:08:38 -0700377/* zpool driver */
378
379#ifdef CONFIG_ZPOOL
380
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800381static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700382 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700383 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700384{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700385 /*
386 * Ignore global gfp flags: zs_malloc() may be invoked from
387 * different contexts and its caller must provide a valid
388 * gfp mask.
389 */
390 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700391}
392
393static void zs_zpool_destroy(void *pool)
394{
395 zs_destroy_pool(pool);
396}
397
398static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
399 unsigned long *handle)
400{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700401 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700402 return *handle ? 0 : -1;
403}
404static void zs_zpool_free(void *pool, unsigned long handle)
405{
406 zs_free(pool, handle);
407}
408
409static int zs_zpool_shrink(void *pool, unsigned int pages,
410 unsigned int *reclaimed)
411{
412 return -EINVAL;
413}
414
415static void *zs_zpool_map(void *pool, unsigned long handle,
416 enum zpool_mapmode mm)
417{
418 enum zs_mapmode zs_mm;
419
420 switch (mm) {
421 case ZPOOL_MM_RO:
422 zs_mm = ZS_MM_RO;
423 break;
424 case ZPOOL_MM_WO:
425 zs_mm = ZS_MM_WO;
426 break;
427 case ZPOOL_MM_RW: /* fallthru */
428 default:
429 zs_mm = ZS_MM_RW;
430 break;
431 }
432
433 return zs_map_object(pool, handle, zs_mm);
434}
435static void zs_zpool_unmap(void *pool, unsigned long handle)
436{
437 zs_unmap_object(pool, handle);
438}
439
440static u64 zs_zpool_total_size(void *pool)
441{
Minchan Kim722cdc12014-10-09 15:29:50 -0700442 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700443}
444
445static struct zpool_driver zs_zpool_driver = {
446 .type = "zsmalloc",
447 .owner = THIS_MODULE,
448 .create = zs_zpool_create,
449 .destroy = zs_zpool_destroy,
450 .malloc = zs_zpool_malloc,
451 .free = zs_zpool_free,
452 .shrink = zs_zpool_shrink,
453 .map = zs_zpool_map,
454 .unmap = zs_zpool_unmap,
455 .total_size = zs_zpool_total_size,
456};
457
Kees Cook137f8cf2014-08-29 15:18:40 -0700458MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700459#endif /* CONFIG_ZPOOL */
460
Nitin Gupta61989a82012-01-09 16:51:56 -0600461/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
462static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
463
Minchan Kim48b48002016-07-26 15:23:31 -0700464static bool is_zspage_isolated(struct zspage *zspage)
465{
466 return zspage->isolated;
467}
468
Nick Desaulniers3457f412017-07-10 15:47:26 -0700469static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600470{
Minchan Kima27545bf2012-04-25 15:23:09 +0900471 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600472}
473
Minchan Kim48b48002016-07-26 15:23:31 -0700474/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700475static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600476{
Minchan Kim37836892016-07-26 15:23:23 -0700477 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600478}
479
Minchan Kim37836892016-07-26 15:23:23 -0700480static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700481{
Minchan Kim37836892016-07-26 15:23:23 -0700482 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700483}
484
Minchan Kim37836892016-07-26 15:23:23 -0700485static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700486{
Minchan Kim37836892016-07-26 15:23:23 -0700487 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700488}
489
Minchan Kim48b48002016-07-26 15:23:31 -0700490static inline struct page *get_first_page(struct zspage *zspage)
491{
492 struct page *first_page = zspage->first_page;
493
494 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
495 return first_page;
496}
497
Minchan Kim4f420472016-07-26 15:23:17 -0700498static inline int get_first_obj_offset(struct page *page)
499{
Minchan Kim48b48002016-07-26 15:23:31 -0700500 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700501}
502
503static inline void set_first_obj_offset(struct page *page, int offset)
504{
Minchan Kim48b48002016-07-26 15:23:31 -0700505 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700506}
507
Minchan Kimbfd093f2016-07-26 15:23:28 -0700508static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700509{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700510 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700511}
512
Minchan Kimbfd093f2016-07-26 15:23:28 -0700513static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700514{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700515 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700516}
517
Minchan Kim37836892016-07-26 15:23:23 -0700518static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700519 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600520 enum fullness_group *fullness)
521{
Minchan Kim48b48002016-07-26 15:23:31 -0700522 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
523
Minchan Kim37836892016-07-26 15:23:23 -0700524 *fullness = zspage->fullness;
525 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600526}
527
Minchan Kim37836892016-07-26 15:23:23 -0700528static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700529 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600530 enum fullness_group fullness)
531{
Minchan Kim37836892016-07-26 15:23:23 -0700532 zspage->class = class_idx;
533 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600534}
535
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900536/*
537 * zsmalloc divides the pool into various size classes where each
538 * class maintains a list of zspages where each zspage is divided
539 * into equal sized chunks. Each allocation falls into one of these
540 * classes depending on its size. This function returns index of the
541 * size class which has chunk size big enough to hold the give size.
542 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600543static int get_size_class_index(int size)
544{
545 int idx = 0;
546
547 if (likely(size > ZS_MIN_ALLOC_SIZE))
548 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
549 ZS_SIZE_CLASS_DELTA);
550
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700551 return min_t(int, ZS_SIZE_CLASSES - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600552}
553
Minchan Kim248ca1b2015-04-15 16:15:42 -0700554static inline void zs_stat_inc(struct size_class *class,
555 enum zs_stat_type type, unsigned long cnt)
556{
Minchan Kim48b48002016-07-26 15:23:31 -0700557 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700558}
559
560static inline void zs_stat_dec(struct size_class *class,
561 enum zs_stat_type type, unsigned long cnt)
562{
Minchan Kim48b48002016-07-26 15:23:31 -0700563 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700564}
565
566static inline unsigned long zs_stat_get(struct size_class *class,
567 enum zs_stat_type type)
568{
Minchan Kim48b48002016-07-26 15:23:31 -0700569 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700570}
571
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700572#ifdef CONFIG_ZSMALLOC_STAT
573
Dan Streetman4abaac92016-05-26 15:16:27 -0700574static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700575{
Dan Streetman4abaac92016-05-26 15:16:27 -0700576 if (!debugfs_initialized()) {
577 pr_warn("debugfs not available, stat dir not created\n");
578 return;
579 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700580
581 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
582 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700583 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700584}
585
586static void __exit zs_stat_exit(void)
587{
588 debugfs_remove_recursive(zs_stat_root);
589}
590
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700591static unsigned long zs_can_compact(struct size_class *class);
592
Minchan Kim248ca1b2015-04-15 16:15:42 -0700593static int zs_stats_size_show(struct seq_file *s, void *v)
594{
595 int i;
596 struct zs_pool *pool = s->private;
597 struct size_class *class;
598 int objs_per_zspage;
599 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700600 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
602 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700603 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700604
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700605 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700606 "class", "size", "almost_full", "almost_empty",
607 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700608 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700609
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700610 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim248ca1b2015-04-15 16:15:42 -0700611 class = pool->size_class[i];
612
613 if (class->index != i)
614 continue;
615
616 spin_lock(&class->lock);
617 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
618 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
619 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
620 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700621 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700622 spin_unlock(&class->lock);
623
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700624 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700625 pages_used = obj_allocated / objs_per_zspage *
626 class->pages_per_zspage;
627
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700628 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
629 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700630 i, class->size, class_almost_full, class_almost_empty,
631 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700632 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700633
634 total_class_almost_full += class_almost_full;
635 total_class_almost_empty += class_almost_empty;
636 total_objs += obj_allocated;
637 total_used_objs += obj_used;
638 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700639 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700640 }
641
642 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700643 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700644 "Total", "", total_class_almost_full,
645 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700646 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700647
648 return 0;
649}
650
651static int zs_stats_size_open(struct inode *inode, struct file *file)
652{
653 return single_open(file, zs_stats_size_show, inode->i_private);
654}
655
656static const struct file_operations zs_stat_size_ops = {
657 .open = zs_stats_size_open,
658 .read = seq_read,
659 .llseek = seq_lseek,
660 .release = single_release,
661};
662
Dan Streetmand34f6152016-05-20 16:59:56 -0700663static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700664{
665 struct dentry *entry;
666
Dan Streetman4abaac92016-05-26 15:16:27 -0700667 if (!zs_stat_root) {
668 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700669 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700670 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700671
672 entry = debugfs_create_dir(name, zs_stat_root);
673 if (!entry) {
674 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700675 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700676 }
677 pool->stat_dentry = entry;
678
679 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
680 pool->stat_dentry, pool, &zs_stat_size_ops);
681 if (!entry) {
682 pr_warn("%s: debugfs file entry <%s> creation failed\n",
683 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700684 debugfs_remove_recursive(pool->stat_dentry);
685 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700686 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700687}
688
689static void zs_pool_stat_destroy(struct zs_pool *pool)
690{
691 debugfs_remove_recursive(pool->stat_dentry);
692}
693
694#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700695static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700696{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700697}
698
699static void __exit zs_stat_exit(void)
700{
701}
702
Dan Streetmand34f6152016-05-20 16:59:56 -0700703static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700704{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700705}
706
707static inline void zs_pool_stat_destroy(struct zs_pool *pool)
708{
709}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700710#endif
711
Minchan Kim48b48002016-07-26 15:23:31 -0700712
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900713/*
714 * For each size class, zspages are divided into different groups
715 * depending on how "full" they are. This was done so that we could
716 * easily find empty or nearly empty zspages when we try to shrink
717 * the pool (not yet implemented). This function returns fullness
718 * status of the given page.
719 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700720static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700721 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600722{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700723 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600724 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700725
Minchan Kim37836892016-07-26 15:23:23 -0700726 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700727 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600728
729 if (inuse == 0)
730 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700731 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600732 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700733 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600734 fg = ZS_ALMOST_EMPTY;
735 else
736 fg = ZS_ALMOST_FULL;
737
738 return fg;
739}
740
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900741/*
742 * Each size class maintains various freelists and zspages are assigned
743 * to one of these freelists based on the number of live objects they
744 * have. This functions inserts the given zspage into the freelist
745 * identified by <class, fullness_group>.
746 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700747static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700748 struct zspage *zspage,
749 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600750{
Minchan Kim37836892016-07-26 15:23:23 -0700751 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600752
Minchan Kim48b48002016-07-26 15:23:31 -0700753 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700754 head = list_first_entry_or_null(&class->fullness_list[fullness],
755 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700756 /*
Minchan Kim37836892016-07-26 15:23:23 -0700757 * We want to see more ZS_FULL pages and less almost empty/full.
758 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700759 */
Minchan Kim37836892016-07-26 15:23:23 -0700760 if (head) {
761 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
762 list_add(&zspage->list, &head->list);
763 return;
764 }
765 }
766 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600767}
768
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900769/*
770 * This function removes the given zspage from the freelist identified
771 * by <class, fullness_group>.
772 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700773static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700774 struct zspage *zspage,
775 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600776{
Minchan Kim37836892016-07-26 15:23:23 -0700777 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700778 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600779
Minchan Kim37836892016-07-26 15:23:23 -0700780 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700781 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600782}
783
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900784/*
785 * Each size class maintains zspages in different fullness groups depending
786 * on the number of live objects they contain. When allocating or freeing
787 * objects, the fullness status of the page can change, say, from ALMOST_FULL
788 * to ALMOST_EMPTY when freeing an object. This function checks if such
789 * a status change has occurred for the given page and accordingly moves the
790 * page from the freelist of the old fullness group to that of the new
791 * fullness group.
792 */
Minchan Kimc7806262015-04-15 16:15:26 -0700793static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700794 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600795{
796 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600797 enum fullness_group currfg, newfg;
798
Minchan Kim37836892016-07-26 15:23:23 -0700799 get_zspage_mapping(zspage, &class_idx, &currfg);
800 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600801 if (newfg == currfg)
802 goto out;
803
Minchan Kim48b48002016-07-26 15:23:31 -0700804 if (!is_zspage_isolated(zspage)) {
805 remove_zspage(class, zspage, currfg);
806 insert_zspage(class, zspage, newfg);
807 }
808
Minchan Kim37836892016-07-26 15:23:23 -0700809 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600810
811out:
812 return newfg;
813}
814
815/*
816 * We have to decide on how many pages to link together
817 * to form a zspage for each size class. This is important
818 * to reduce wastage due to unusable space left at end of
819 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700820 * wastage = Zp % class_size
821 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600822 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
823 *
824 * For example, for size class of 3/8 * PAGE_SIZE, we should
825 * link together 3 PAGE_SIZE sized pages to form a zspage
826 * since then we can perfectly fit in 8 such objects.
827 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900828static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600829{
830 int i, max_usedpc = 0;
831 /* zspage order which gives maximum used size per KB */
832 int max_usedpc_order = 1;
833
Seth Jennings84d4faa2012-03-05 11:33:21 -0600834 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600835 int zspage_size;
836 int waste, usedpc;
837
838 zspage_size = i * PAGE_SIZE;
839 waste = zspage_size % class_size;
840 usedpc = (zspage_size - waste) * 100 / zspage_size;
841
842 if (usedpc > max_usedpc) {
843 max_usedpc = usedpc;
844 max_usedpc_order = i;
845 }
846 }
847
848 return max_usedpc_order;
849}
850
Minchan Kim37836892016-07-26 15:23:23 -0700851static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600852{
Minchan Kim48b48002016-07-26 15:23:31 -0700853 struct zspage *zspage = (struct zspage *)page->private;
854
855 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
856 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600857}
858
859static struct page *get_next_page(struct page *page)
860{
Minchan Kim48b48002016-07-26 15:23:31 -0700861 if (unlikely(PageHugeObject(page)))
862 return NULL;
863
864 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600865}
866
Minchan Kimbfd093f2016-07-26 15:23:28 -0700867/**
868 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
869 * @page: page object resides in zspage
870 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800871 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700872static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700873 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600874{
Minchan Kim312fcae2015-04-15 16:15:30 -0700875 obj >>= OBJ_TAG_BITS;
876 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
877 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600878}
879
Minchan Kimbfd093f2016-07-26 15:23:28 -0700880/**
881 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
882 * @page: page object resides in zspage
883 * @obj_idx: object index
884 */
885static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
886{
887 unsigned long obj;
888
889 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
890 obj |= obj_idx & OBJ_INDEX_MASK;
891 obj <<= OBJ_TAG_BITS;
892
893 return obj;
894}
895
Minchan Kim2e40e162015-04-15 16:15:23 -0700896static unsigned long handle_to_obj(unsigned long handle)
897{
898 return *(unsigned long *)handle;
899}
900
Minchan Kim48b48002016-07-26 15:23:31 -0700901static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700902{
Minchan Kim48b48002016-07-26 15:23:31 -0700903 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700904 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700905 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700906 } else
907 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700908}
909
Minchan Kim48b48002016-07-26 15:23:31 -0700910static inline int testpin_tag(unsigned long handle)
911{
912 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
913}
914
Minchan Kim312fcae2015-04-15 16:15:30 -0700915static inline int trypin_tag(unsigned long handle)
916{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700917 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700918}
919
920static void pin_tag(unsigned long handle)
921{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700922 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700923}
924
925static void unpin_tag(unsigned long handle)
926{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700927 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700928}
929
Nitin Guptaf4477e92012-04-02 09:13:56 -0500930static void reset_page(struct page *page)
931{
Minchan Kim48b48002016-07-26 15:23:31 -0700932 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700933 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500934 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700935 page_mapcount_reset(page);
936 ClearPageHugeObject(page);
937 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500938}
939
Minchan Kim48b48002016-07-26 15:23:31 -0700940/*
941 * To prevent zspage destroy during migration, zspage freeing should
942 * hold locks of all pages in the zspage.
943 */
944void lock_zspage(struct zspage *zspage)
945{
946 struct page *page = get_first_page(zspage);
947
948 do {
949 lock_page(page);
950 } while ((page = get_next_page(page)) != NULL);
951}
952
953int trylock_zspage(struct zspage *zspage)
954{
955 struct page *cursor, *fail;
956
957 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
958 get_next_page(cursor)) {
959 if (!trylock_page(cursor)) {
960 fail = cursor;
961 goto unlock;
962 }
963 }
964
965 return 1;
966unlock:
967 for (cursor = get_first_page(zspage); cursor != fail; cursor =
968 get_next_page(cursor))
969 unlock_page(cursor);
970
971 return 0;
972}
973
974static void __free_zspage(struct zs_pool *pool, struct size_class *class,
975 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600976{
Minchan Kim37836892016-07-26 15:23:23 -0700977 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700978 enum fullness_group fg;
979 unsigned int class_idx;
980
981 get_zspage_mapping(zspage, &class_idx, &fg);
982
983 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600984
Minchan Kim37836892016-07-26 15:23:23 -0700985 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700986 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600987
Minchan Kim48b48002016-07-26 15:23:31 -0700988 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700989 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700990 VM_BUG_ON_PAGE(!PageLocked(page), page);
991 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700992 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700993 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -0700994 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -0700995 put_page(page);
996 page = next;
997 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -0600998
Minchan Kim37836892016-07-26 15:23:23 -0700999 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001000
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001001 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001002 atomic_long_sub(class->pages_per_zspage,
1003 &pool->pages_allocated);
1004}
1005
1006static void free_zspage(struct zs_pool *pool, struct size_class *class,
1007 struct zspage *zspage)
1008{
1009 VM_BUG_ON(get_zspage_inuse(zspage));
1010 VM_BUG_ON(list_empty(&zspage->list));
1011
1012 if (!trylock_zspage(zspage)) {
1013 kick_deferred_free(pool);
1014 return;
1015 }
1016
1017 remove_zspage(class, zspage, ZS_EMPTY);
1018 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001019}
1020
1021/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001022static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001023{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001024 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001025 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001026 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001027
Nitin Gupta61989a82012-01-09 16:51:56 -06001028 while (page) {
1029 struct page *next_page;
1030 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001031 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001032
Minchan Kim37836892016-07-26 15:23:23 -07001033 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001034
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001035 vaddr = kmap_atomic(page);
1036 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001037
Dan Streetman5538c562014-10-09 15:30:01 -07001038 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001039 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001040 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001041 }
1042
1043 /*
1044 * We now come to the last (full or partial) object on this
1045 * page, which must point to the first object on the next
1046 * page (if present)
1047 */
1048 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001049 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001050 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001051 } else {
1052 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001053 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001054 * whether it's allocated object or not.
1055 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001056 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001057 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001058 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001059 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001060 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001061 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001062
Minchan Kimbfd093f2016-07-26 15:23:28 -07001063 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001064}
1065
Minchan Kim48b48002016-07-26 15:23:31 -07001066static void create_page_chain(struct size_class *class, struct zspage *zspage,
1067 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001068{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001069 int i;
1070 struct page *page;
1071 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001072 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001073
1074 /*
1075 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001076 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001077 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001078 *
Minchan Kim37836892016-07-26 15:23:23 -07001079 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001080 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001081 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001082 for (i = 0; i < nr_pages; i++) {
1083 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001084 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001085 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001086 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001087 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001088 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001089 if (unlikely(class->objs_per_zspage == 1 &&
1090 class->pages_per_zspage == 1))
1091 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001092 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001093 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001094 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001095 prev_page = page;
1096 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001097}
Nitin Gupta61989a82012-01-09 16:51:56 -06001098
Minchan Kimbdb0af72016-07-26 15:23:20 -07001099/*
1100 * Allocate a zspage for the given size class
1101 */
Minchan Kim37836892016-07-26 15:23:23 -07001102static struct zspage *alloc_zspage(struct zs_pool *pool,
1103 struct size_class *class,
1104 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001105{
1106 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001107 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001108 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1109
1110 if (!zspage)
1111 return NULL;
1112
1113 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001114 zspage->magic = ZSPAGE_MAGIC;
1115 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001116
Minchan Kimbdb0af72016-07-26 15:23:20 -07001117 for (i = 0; i < class->pages_per_zspage; i++) {
1118 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001119
Minchan Kim37836892016-07-26 15:23:23 -07001120 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001121 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001122 while (--i >= 0) {
1123 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001124 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001125 }
Minchan Kim37836892016-07-26 15:23:23 -07001126 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001127 return NULL;
1128 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001129
1130 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001131 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001132 }
1133
Minchan Kim48b48002016-07-26 15:23:31 -07001134 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001135 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001136
Minchan Kim37836892016-07-26 15:23:23 -07001137 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001138}
1139
Minchan Kim37836892016-07-26 15:23:23 -07001140static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001141{
1142 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001143 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001144
Minchan Kim48b48002016-07-26 15:23:31 -07001145 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001146 zspage = list_first_entry_or_null(&class->fullness_list[i],
1147 struct zspage, list);
1148 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001149 break;
1150 }
1151
Minchan Kim37836892016-07-26 15:23:23 -07001152 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001153}
1154
Minchan Kim1b945ae2013-12-11 11:04:36 +09001155#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001156static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001157{
Seth Jenningsf5536462012-07-18 11:55:56 -05001158 /*
1159 * Make sure we don't leak memory if a cpu UP notification
1160 * and zs_init() race and both call zs_cpu_up() on the same cpu
1161 */
1162 if (area->vm)
1163 return 0;
1164 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1165 if (!area->vm)
1166 return -ENOMEM;
1167 return 0;
1168}
1169
1170static inline void __zs_cpu_down(struct mapping_area *area)
1171{
1172 if (area->vm)
1173 free_vm_area(area->vm);
1174 area->vm = NULL;
1175}
1176
1177static inline void *__zs_map_object(struct mapping_area *area,
1178 struct page *pages[2], int off, int size)
1179{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001180 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001181 area->vm_addr = area->vm->addr;
1182 return area->vm_addr + off;
1183}
1184
1185static inline void __zs_unmap_object(struct mapping_area *area,
1186 struct page *pages[2], int off, int size)
1187{
1188 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001189
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001190 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001191}
1192
Minchan Kim1b945ae2013-12-11 11:04:36 +09001193#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001194
1195static inline int __zs_cpu_up(struct mapping_area *area)
1196{
1197 /*
1198 * Make sure we don't leak memory if a cpu UP notification
1199 * and zs_init() race and both call zs_cpu_up() on the same cpu
1200 */
1201 if (area->vm_buf)
1202 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001203 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001204 if (!area->vm_buf)
1205 return -ENOMEM;
1206 return 0;
1207}
1208
1209static inline void __zs_cpu_down(struct mapping_area *area)
1210{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001211 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001212 area->vm_buf = NULL;
1213}
1214
1215static void *__zs_map_object(struct mapping_area *area,
1216 struct page *pages[2], int off, int size)
1217{
Seth Jennings5f601902012-07-02 16:15:49 -05001218 int sizes[2];
1219 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001220 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001221
Seth Jenningsf5536462012-07-18 11:55:56 -05001222 /* disable page faults to match kmap_atomic() return conditions */
1223 pagefault_disable();
1224
1225 /* no read fastpath */
1226 if (area->vm_mm == ZS_MM_WO)
1227 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001228
1229 sizes[0] = PAGE_SIZE - off;
1230 sizes[1] = size - sizes[0];
1231
Seth Jennings5f601902012-07-02 16:15:49 -05001232 /* copy object to per-cpu buffer */
1233 addr = kmap_atomic(pages[0]);
1234 memcpy(buf, addr + off, sizes[0]);
1235 kunmap_atomic(addr);
1236 addr = kmap_atomic(pages[1]);
1237 memcpy(buf + sizes[0], addr, sizes[1]);
1238 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001239out:
1240 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001241}
1242
Seth Jenningsf5536462012-07-18 11:55:56 -05001243static void __zs_unmap_object(struct mapping_area *area,
1244 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001245{
Seth Jennings5f601902012-07-02 16:15:49 -05001246 int sizes[2];
1247 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001248 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001249
Seth Jenningsf5536462012-07-18 11:55:56 -05001250 /* no write fastpath */
1251 if (area->vm_mm == ZS_MM_RO)
1252 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001253
Minchan Kim7b60a682015-04-15 16:15:39 -07001254 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001255 buf = buf + ZS_HANDLE_SIZE;
1256 size -= ZS_HANDLE_SIZE;
1257 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001258
Seth Jennings5f601902012-07-02 16:15:49 -05001259 sizes[0] = PAGE_SIZE - off;
1260 sizes[1] = size - sizes[0];
1261
1262 /* copy per-cpu buffer to object */
1263 addr = kmap_atomic(pages[0]);
1264 memcpy(addr + off, buf, sizes[0]);
1265 kunmap_atomic(addr);
1266 addr = kmap_atomic(pages[1]);
1267 memcpy(addr, buf + sizes[0], sizes[1]);
1268 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001269
1270out:
1271 /* enable page faults to match kunmap_atomic() return conditions */
1272 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001273}
Nitin Gupta61989a82012-01-09 16:51:56 -06001274
Minchan Kim1b945ae2013-12-11 11:04:36 +09001275#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001276
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001277static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001278{
Nitin Gupta61989a82012-01-09 16:51:56 -06001279 struct mapping_area *area;
1280
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001281 area = &per_cpu(zs_map_area, cpu);
1282 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001283}
1284
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001285static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001286{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001287 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001288
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001289 area = &per_cpu(zs_map_area, cpu);
1290 __zs_cpu_down(area);
1291 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001292}
1293
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001294static bool can_merge(struct size_class *prev, int pages_per_zspage,
1295 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001296{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001297 if (prev->pages_per_zspage == pages_per_zspage &&
1298 prev->objs_per_zspage == objs_per_zspage)
1299 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001300
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001301 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001302}
1303
Minchan Kim37836892016-07-26 15:23:23 -07001304static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001305{
Minchan Kim37836892016-07-26 15:23:23 -07001306 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001307}
1308
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001309unsigned long zs_get_total_pages(struct zs_pool *pool)
1310{
1311 return atomic_long_read(&pool->pages_allocated);
1312}
1313EXPORT_SYMBOL_GPL(zs_get_total_pages);
1314
1315/**
1316 * zs_map_object - get address of allocated object from handle.
1317 * @pool: pool from which the object was allocated
1318 * @handle: handle returned from zs_malloc
1319 *
1320 * Before using an object allocated from zs_malloc, it must be mapped using
1321 * this function. When done with the object, it must be unmapped using
1322 * zs_unmap_object.
1323 *
1324 * Only one object can be mapped per cpu at a time. There is no protection
1325 * against nested mappings.
1326 *
1327 * This function returns with preemption and page faults disabled.
1328 */
1329void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1330 enum zs_mapmode mm)
1331{
Minchan Kim37836892016-07-26 15:23:23 -07001332 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001333 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001334 unsigned long obj, off;
1335 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001336
1337 unsigned int class_idx;
1338 enum fullness_group fg;
1339 struct size_class *class;
1340 struct mapping_area *area;
1341 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001342 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001343
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001344 /*
1345 * Because we use per-cpu mapping areas shared among the
1346 * pools/users, we can't allow mapping in interrupt context
1347 * because it can corrupt another users mappings.
1348 */
Minchan Kim830e4bc2016-05-20 16:59:39 -07001349 WARN_ON_ONCE(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001350
Minchan Kim312fcae2015-04-15 16:15:30 -07001351 /* From now on, migration cannot move the object */
1352 pin_tag(handle);
1353
Minchan Kim2e40e162015-04-15 16:15:23 -07001354 obj = handle_to_obj(handle);
1355 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001356 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001357
1358 /* migration cannot move any subpage in this zspage */
1359 migrate_read_lock(zspage);
1360
Minchan Kim37836892016-07-26 15:23:23 -07001361 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001362 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001363 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001364
1365 area = &get_cpu_var(zs_map_area);
1366 area->vm_mm = mm;
1367 if (off + class->size <= PAGE_SIZE) {
1368 /* this object is contained entirely within a page */
1369 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001370 ret = area->vm_addr + off;
1371 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001372 }
1373
1374 /* this object spans two pages */
1375 pages[0] = page;
1376 pages[1] = get_next_page(page);
1377 BUG_ON(!pages[1]);
1378
Minchan Kim2e40e162015-04-15 16:15:23 -07001379 ret = __zs_map_object(area, pages, off, class->size);
1380out:
Minchan Kim48b48002016-07-26 15:23:31 -07001381 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001382 ret += ZS_HANDLE_SIZE;
1383
1384 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001385}
1386EXPORT_SYMBOL_GPL(zs_map_object);
1387
1388void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1389{
Minchan Kim37836892016-07-26 15:23:23 -07001390 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001391 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001392 unsigned long obj, off;
1393 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001394
1395 unsigned int class_idx;
1396 enum fullness_group fg;
1397 struct size_class *class;
1398 struct mapping_area *area;
1399
Minchan Kim2e40e162015-04-15 16:15:23 -07001400 obj = handle_to_obj(handle);
1401 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001402 zspage = get_zspage(page);
1403 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001404 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001405 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001406
1407 area = this_cpu_ptr(&zs_map_area);
1408 if (off + class->size <= PAGE_SIZE)
1409 kunmap_atomic(area->vm_addr);
1410 else {
1411 struct page *pages[2];
1412
1413 pages[0] = page;
1414 pages[1] = get_next_page(page);
1415 BUG_ON(!pages[1]);
1416
1417 __zs_unmap_object(area, pages, off, class->size);
1418 }
1419 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001420
1421 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001422 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001423}
1424EXPORT_SYMBOL_GPL(zs_unmap_object);
1425
Minchan Kim251cbb92016-05-20 16:59:42 -07001426static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001427 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001428{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001429 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001430 unsigned long obj;
1431 struct link_free *link;
1432
1433 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001434 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001435 void *vaddr;
1436
Minchan Kim312fcae2015-04-15 16:15:30 -07001437 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001438 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001439
1440 offset = obj * class->size;
1441 nr_page = offset >> PAGE_SHIFT;
1442 m_offset = offset & ~PAGE_MASK;
1443 m_page = get_first_page(zspage);
1444
1445 for (i = 0; i < nr_page; i++)
1446 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001447
1448 vaddr = kmap_atomic(m_page);
1449 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001450 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001451 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001452 /* record handle in the header of allocated chunk */
1453 link->handle = handle;
1454 else
Minchan Kim37836892016-07-26 15:23:23 -07001455 /* record handle to page->index */
1456 zspage->first_page->index = handle;
1457
Minchan Kimc7806262015-04-15 16:15:26 -07001458 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001459 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001460 zs_stat_inc(class, OBJ_USED, 1);
1461
Minchan Kimbfd093f2016-07-26 15:23:28 -07001462 obj = location_to_obj(m_page, obj);
1463
Minchan Kimc7806262015-04-15 16:15:26 -07001464 return obj;
1465}
1466
1467
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001468/**
1469 * zs_malloc - Allocate block of given size from pool.
1470 * @pool: pool to allocate from
1471 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001472 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001473 *
1474 * On success, handle to the allocated object is returned,
1475 * otherwise 0.
1476 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1477 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001478unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001479{
Minchan Kim2e40e162015-04-15 16:15:23 -07001480 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001481 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001482 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001483 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001484
Minchan Kim7b60a682015-04-15 16:15:39 -07001485 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001486 return 0;
1487
Minchan Kim37836892016-07-26 15:23:23 -07001488 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001489 if (!handle)
1490 return 0;
1491
1492 /* extra space in chunk to keep the handle */
1493 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001494 class = pool->size_class[get_size_class_index(size)];
1495
1496 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001497 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001498 if (likely(zspage)) {
1499 obj = obj_malloc(class, zspage, handle);
1500 /* Now move the zspage to another fullness group, if required */
1501 fix_fullness_group(class, zspage);
1502 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001503 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001504
Minchan Kim48b48002016-07-26 15:23:31 -07001505 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001506 }
1507
Minchan Kim48b48002016-07-26 15:23:31 -07001508 spin_unlock(&class->lock);
1509
1510 zspage = alloc_zspage(pool, class, gfp);
1511 if (!zspage) {
1512 cache_free_handle(pool, handle);
1513 return 0;
1514 }
1515
1516 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001517 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001518 newfg = get_fullness_group(class, zspage);
1519 insert_zspage(class, zspage, newfg);
1520 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001521 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001522 atomic_long_add(class->pages_per_zspage,
1523 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001524 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001525
1526 /* We completely set up zspage so mark them as movable */
1527 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001528 spin_unlock(&class->lock);
1529
Minchan Kim2e40e162015-04-15 16:15:23 -07001530 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001531}
1532EXPORT_SYMBOL_GPL(zs_malloc);
1533
Minchan Kim1ee47162016-05-20 16:59:45 -07001534static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001535{
1536 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001537 struct zspage *zspage;
1538 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001539 unsigned long f_offset;
1540 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001541 void *vaddr;
1542
Minchan Kim312fcae2015-04-15 16:15:30 -07001543 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001544 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001545 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001546 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001547
Minchan Kimc7806262015-04-15 16:15:26 -07001548 vaddr = kmap_atomic(f_page);
1549
1550 /* Insert this object in containing zspage's freelist */
1551 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001552 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001553 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001554 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001555 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001556 zs_stat_dec(class, OBJ_USED, 1);
1557}
1558
1559void zs_free(struct zs_pool *pool, unsigned long handle)
1560{
Minchan Kim37836892016-07-26 15:23:23 -07001561 struct zspage *zspage;
1562 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001563 unsigned long obj;
1564 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001565 int class_idx;
1566 struct size_class *class;
1567 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001568 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001569
Minchan Kim2e40e162015-04-15 16:15:23 -07001570 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001571 return;
1572
Minchan Kim312fcae2015-04-15 16:15:30 -07001573 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001574 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001575 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001576 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001577
Minchan Kim48b48002016-07-26 15:23:31 -07001578 migrate_read_lock(zspage);
1579
Minchan Kim37836892016-07-26 15:23:23 -07001580 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001581 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001582
1583 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001584 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001585 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001586 if (fullness != ZS_EMPTY) {
1587 migrate_read_unlock(zspage);
1588 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001589 }
Minchan Kim48b48002016-07-26 15:23:31 -07001590
1591 isolated = is_zspage_isolated(zspage);
1592 migrate_read_unlock(zspage);
1593 /* If zspage is isolated, zs_page_putback will free the zspage */
1594 if (likely(!isolated))
1595 free_zspage(pool, class, zspage);
1596out:
1597
Minchan Kim312fcae2015-04-15 16:15:30 -07001598 spin_unlock(&class->lock);
1599 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001600 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001601}
1602EXPORT_SYMBOL_GPL(zs_free);
1603
Minchan Kim251cbb92016-05-20 16:59:42 -07001604static void zs_object_copy(struct size_class *class, unsigned long dst,
1605 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001606{
1607 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001608 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001609 unsigned long s_off, d_off;
1610 void *s_addr, *d_addr;
1611 int s_size, d_size, size;
1612 int written = 0;
1613
1614 s_size = d_size = class->size;
1615
1616 obj_to_location(src, &s_page, &s_objidx);
1617 obj_to_location(dst, &d_page, &d_objidx);
1618
Minchan Kimbfd093f2016-07-26 15:23:28 -07001619 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1620 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001621
1622 if (s_off + class->size > PAGE_SIZE)
1623 s_size = PAGE_SIZE - s_off;
1624
1625 if (d_off + class->size > PAGE_SIZE)
1626 d_size = PAGE_SIZE - d_off;
1627
1628 s_addr = kmap_atomic(s_page);
1629 d_addr = kmap_atomic(d_page);
1630
1631 while (1) {
1632 size = min(s_size, d_size);
1633 memcpy(d_addr + d_off, s_addr + s_off, size);
1634 written += size;
1635
1636 if (written == class->size)
1637 break;
1638
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001639 s_off += size;
1640 s_size -= size;
1641 d_off += size;
1642 d_size -= size;
1643
1644 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001645 kunmap_atomic(d_addr);
1646 kunmap_atomic(s_addr);
1647 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001648 s_addr = kmap_atomic(s_page);
1649 d_addr = kmap_atomic(d_page);
1650 s_size = class->size - written;
1651 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001652 }
1653
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001654 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001655 kunmap_atomic(d_addr);
1656 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001657 d_addr = kmap_atomic(d_page);
1658 d_size = class->size - written;
1659 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001660 }
1661 }
1662
1663 kunmap_atomic(d_addr);
1664 kunmap_atomic(s_addr);
1665}
1666
1667/*
1668 * Find alloced object in zspage from index object and
1669 * return handle.
1670 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001671static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001672 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001673{
1674 unsigned long head;
1675 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001676 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001677 unsigned long handle = 0;
1678 void *addr = kmap_atomic(page);
1679
Minchan Kim37836892016-07-26 15:23:23 -07001680 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001681 offset += class->size * index;
1682
1683 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001684 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001685 if (head & OBJ_ALLOCATED_TAG) {
1686 handle = head & ~OBJ_ALLOCATED_TAG;
1687 if (trypin_tag(handle))
1688 break;
1689 handle = 0;
1690 }
1691
1692 offset += class->size;
1693 index++;
1694 }
1695
1696 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001697
1698 *obj_idx = index;
1699
Minchan Kim312fcae2015-04-15 16:15:30 -07001700 return handle;
1701}
1702
1703struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001704 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001705 struct page *s_page;
1706 /* Destination page for migration which should be a first page
1707 * of zspage. */
1708 struct page *d_page;
1709 /* Starting object index within @s_page which used for live object
1710 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001711 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001712};
1713
1714static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1715 struct zs_compact_control *cc)
1716{
1717 unsigned long used_obj, free_obj;
1718 unsigned long handle;
1719 struct page *s_page = cc->s_page;
1720 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001721 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001722 int ret = 0;
1723
1724 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001725 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001726 if (!handle) {
1727 s_page = get_next_page(s_page);
1728 if (!s_page)
1729 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001730 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001731 continue;
1732 }
1733
1734 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001735 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001736 unpin_tag(handle);
1737 ret = -ENOMEM;
1738 break;
1739 }
1740
1741 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001742 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001743 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001744 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001745 /*
1746 * record_obj updates handle's value to free_obj and it will
1747 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1748 * breaks synchronization using pin_tag(e,g, zs_free) so
1749 * let's keep the lock bit.
1750 */
1751 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001752 record_obj(handle, free_obj);
1753 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001754 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001755 }
1756
1757 /* Remember last position in this iteration */
1758 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001759 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001760
1761 return ret;
1762}
1763
Minchan Kim37836892016-07-26 15:23:23 -07001764static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001765{
1766 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001767 struct zspage *zspage;
1768 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001769
Minchan Kim37836892016-07-26 15:23:23 -07001770 if (!source) {
1771 fg[0] = ZS_ALMOST_FULL;
1772 fg[1] = ZS_ALMOST_EMPTY;
1773 }
1774
1775 for (i = 0; i < 2; i++) {
1776 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1777 struct zspage, list);
1778 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001779 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001780 remove_zspage(class, zspage, fg[i]);
1781 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001782 }
1783 }
1784
Minchan Kim37836892016-07-26 15:23:23 -07001785 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001786}
1787
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001788/*
Minchan Kim37836892016-07-26 15:23:23 -07001789 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001790 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001791 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001792 *
Minchan Kim37836892016-07-26 15:23:23 -07001793 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001794 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001795static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001796 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001797{
Minchan Kim312fcae2015-04-15 16:15:30 -07001798 enum fullness_group fullness;
1799
Minchan Kim48b48002016-07-26 15:23:31 -07001800 VM_BUG_ON(is_zspage_isolated(zspage));
1801
Minchan Kim37836892016-07-26 15:23:23 -07001802 fullness = get_fullness_group(class, zspage);
1803 insert_zspage(class, zspage, fullness);
1804 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001805
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001806 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001807}
1808
Minchan Kim48b48002016-07-26 15:23:31 -07001809#ifdef CONFIG_COMPACTION
1810static struct dentry *zs_mount(struct file_system_type *fs_type,
1811 int flags, const char *dev_name, void *data)
1812{
1813 static const struct dentry_operations ops = {
1814 .d_dname = simple_dname,
1815 };
1816
1817 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1818}
1819
1820static struct file_system_type zsmalloc_fs = {
1821 .name = "zsmalloc",
1822 .mount = zs_mount,
1823 .kill_sb = kill_anon_super,
1824};
1825
1826static int zsmalloc_mount(void)
1827{
1828 int ret = 0;
1829
1830 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1831 if (IS_ERR(zsmalloc_mnt))
1832 ret = PTR_ERR(zsmalloc_mnt);
1833
1834 return ret;
1835}
1836
1837static void zsmalloc_unmount(void)
1838{
1839 kern_unmount(zsmalloc_mnt);
1840}
1841
1842static void migrate_lock_init(struct zspage *zspage)
1843{
1844 rwlock_init(&zspage->lock);
1845}
1846
1847static void migrate_read_lock(struct zspage *zspage)
1848{
1849 read_lock(&zspage->lock);
1850}
1851
1852static void migrate_read_unlock(struct zspage *zspage)
1853{
1854 read_unlock(&zspage->lock);
1855}
1856
1857static void migrate_write_lock(struct zspage *zspage)
1858{
1859 write_lock(&zspage->lock);
1860}
1861
1862static void migrate_write_unlock(struct zspage *zspage)
1863{
1864 write_unlock(&zspage->lock);
1865}
1866
1867/* Number of isolated subpage for *page migration* in this zspage */
1868static void inc_zspage_isolation(struct zspage *zspage)
1869{
1870 zspage->isolated++;
1871}
1872
1873static void dec_zspage_isolation(struct zspage *zspage)
1874{
1875 zspage->isolated--;
1876}
1877
1878static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1879 struct page *newpage, struct page *oldpage)
1880{
1881 struct page *page;
1882 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1883 int idx = 0;
1884
1885 page = get_first_page(zspage);
1886 do {
1887 if (page == oldpage)
1888 pages[idx] = newpage;
1889 else
1890 pages[idx] = page;
1891 idx++;
1892 } while ((page = get_next_page(page)) != NULL);
1893
1894 create_page_chain(class, zspage, pages);
1895 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1896 if (unlikely(PageHugeObject(oldpage)))
1897 newpage->index = oldpage->index;
1898 __SetPageMovable(newpage, page_mapping(oldpage));
1899}
1900
1901bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1902{
1903 struct zs_pool *pool;
1904 struct size_class *class;
1905 int class_idx;
1906 enum fullness_group fullness;
1907 struct zspage *zspage;
1908 struct address_space *mapping;
1909
1910 /*
1911 * Page is locked so zspage couldn't be destroyed. For detail, look at
1912 * lock_zspage in free_zspage.
1913 */
1914 VM_BUG_ON_PAGE(!PageMovable(page), page);
1915 VM_BUG_ON_PAGE(PageIsolated(page), page);
1916
1917 zspage = get_zspage(page);
1918
1919 /*
1920 * Without class lock, fullness could be stale while class_idx is okay
1921 * because class_idx is constant unless page is freed so we should get
1922 * fullness again under class lock.
1923 */
1924 get_zspage_mapping(zspage, &class_idx, &fullness);
1925 mapping = page_mapping(page);
1926 pool = mapping->private_data;
1927 class = pool->size_class[class_idx];
1928
1929 spin_lock(&class->lock);
1930 if (get_zspage_inuse(zspage) == 0) {
1931 spin_unlock(&class->lock);
1932 return false;
1933 }
1934
1935 /* zspage is isolated for object migration */
1936 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1937 spin_unlock(&class->lock);
1938 return false;
1939 }
1940
1941 /*
1942 * If this is first time isolation for the zspage, isolate zspage from
1943 * size_class to prevent further object allocation from the zspage.
1944 */
1945 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1946 get_zspage_mapping(zspage, &class_idx, &fullness);
1947 remove_zspage(class, zspage, fullness);
1948 }
1949
1950 inc_zspage_isolation(zspage);
1951 spin_unlock(&class->lock);
1952
1953 return true;
1954}
1955
1956int zs_page_migrate(struct address_space *mapping, struct page *newpage,
1957 struct page *page, enum migrate_mode mode)
1958{
1959 struct zs_pool *pool;
1960 struct size_class *class;
1961 int class_idx;
1962 enum fullness_group fullness;
1963 struct zspage *zspage;
1964 struct page *dummy;
1965 void *s_addr, *d_addr, *addr;
1966 int offset, pos;
1967 unsigned long handle, head;
1968 unsigned long old_obj, new_obj;
1969 unsigned int obj_idx;
1970 int ret = -EAGAIN;
1971
1972 VM_BUG_ON_PAGE(!PageMovable(page), page);
1973 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1974
1975 zspage = get_zspage(page);
1976
1977 /* Concurrent compactor cannot migrate any subpage in zspage */
1978 migrate_write_lock(zspage);
1979 get_zspage_mapping(zspage, &class_idx, &fullness);
1980 pool = mapping->private_data;
1981 class = pool->size_class[class_idx];
1982 offset = get_first_obj_offset(page);
1983
1984 spin_lock(&class->lock);
1985 if (!get_zspage_inuse(zspage)) {
1986 ret = -EBUSY;
1987 goto unlock_class;
1988 }
1989
1990 pos = offset;
1991 s_addr = kmap_atomic(page);
1992 while (pos < PAGE_SIZE) {
1993 head = obj_to_head(page, s_addr + pos);
1994 if (head & OBJ_ALLOCATED_TAG) {
1995 handle = head & ~OBJ_ALLOCATED_TAG;
1996 if (!trypin_tag(handle))
1997 goto unpin_objects;
1998 }
1999 pos += class->size;
2000 }
2001
2002 /*
2003 * Here, any user cannot access all objects in the zspage so let's move.
2004 */
2005 d_addr = kmap_atomic(newpage);
2006 memcpy(d_addr, s_addr, PAGE_SIZE);
2007 kunmap_atomic(d_addr);
2008
2009 for (addr = s_addr + offset; addr < s_addr + pos;
2010 addr += class->size) {
2011 head = obj_to_head(page, addr);
2012 if (head & OBJ_ALLOCATED_TAG) {
2013 handle = head & ~OBJ_ALLOCATED_TAG;
2014 if (!testpin_tag(handle))
2015 BUG();
2016
2017 old_obj = handle_to_obj(handle);
2018 obj_to_location(old_obj, &dummy, &obj_idx);
2019 new_obj = (unsigned long)location_to_obj(newpage,
2020 obj_idx);
2021 new_obj |= BIT(HANDLE_PIN_BIT);
2022 record_obj(handle, new_obj);
2023 }
2024 }
2025
2026 replace_sub_page(class, zspage, newpage, page);
2027 get_page(newpage);
2028
2029 dec_zspage_isolation(zspage);
2030
2031 /*
2032 * Page migration is done so let's putback isolated zspage to
2033 * the list if @page is final isolated subpage in the zspage.
2034 */
2035 if (!is_zspage_isolated(zspage))
2036 putback_zspage(class, zspage);
2037
2038 reset_page(page);
2039 put_page(page);
2040 page = newpage;
2041
Minchan Kimdd4123f2016-07-26 15:26:50 -07002042 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002043unpin_objects:
2044 for (addr = s_addr + offset; addr < s_addr + pos;
2045 addr += class->size) {
2046 head = obj_to_head(page, addr);
2047 if (head & OBJ_ALLOCATED_TAG) {
2048 handle = head & ~OBJ_ALLOCATED_TAG;
2049 if (!testpin_tag(handle))
2050 BUG();
2051 unpin_tag(handle);
2052 }
2053 }
2054 kunmap_atomic(s_addr);
2055unlock_class:
2056 spin_unlock(&class->lock);
2057 migrate_write_unlock(zspage);
2058
2059 return ret;
2060}
2061
2062void zs_page_putback(struct page *page)
2063{
2064 struct zs_pool *pool;
2065 struct size_class *class;
2066 int class_idx;
2067 enum fullness_group fg;
2068 struct address_space *mapping;
2069 struct zspage *zspage;
2070
2071 VM_BUG_ON_PAGE(!PageMovable(page), page);
2072 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2073
2074 zspage = get_zspage(page);
2075 get_zspage_mapping(zspage, &class_idx, &fg);
2076 mapping = page_mapping(page);
2077 pool = mapping->private_data;
2078 class = pool->size_class[class_idx];
2079
2080 spin_lock(&class->lock);
2081 dec_zspage_isolation(zspage);
2082 if (!is_zspage_isolated(zspage)) {
2083 fg = putback_zspage(class, zspage);
2084 /*
2085 * Due to page_lock, we cannot free zspage immediately
2086 * so let's defer.
2087 */
2088 if (fg == ZS_EMPTY)
2089 schedule_work(&pool->free_work);
2090 }
2091 spin_unlock(&class->lock);
2092}
2093
2094const struct address_space_operations zsmalloc_aops = {
2095 .isolate_page = zs_page_isolate,
2096 .migratepage = zs_page_migrate,
2097 .putback_page = zs_page_putback,
2098};
2099
2100static int zs_register_migration(struct zs_pool *pool)
2101{
2102 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2103 if (IS_ERR(pool->inode)) {
2104 pool->inode = NULL;
2105 return 1;
2106 }
2107
2108 pool->inode->i_mapping->private_data = pool;
2109 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2110 return 0;
2111}
2112
2113static void zs_unregister_migration(struct zs_pool *pool)
2114{
2115 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002116 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002117}
2118
2119/*
2120 * Caller should hold page_lock of all pages in the zspage
2121 * In here, we cannot use zspage meta data.
2122 */
2123static void async_free_zspage(struct work_struct *work)
2124{
2125 int i;
2126 struct size_class *class;
2127 unsigned int class_idx;
2128 enum fullness_group fullness;
2129 struct zspage *zspage, *tmp;
2130 LIST_HEAD(free_pages);
2131 struct zs_pool *pool = container_of(work, struct zs_pool,
2132 free_work);
2133
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002134 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim48b48002016-07-26 15:23:31 -07002135 class = pool->size_class[i];
2136 if (class->index != i)
2137 continue;
2138
2139 spin_lock(&class->lock);
2140 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2141 spin_unlock(&class->lock);
2142 }
2143
2144
2145 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2146 list_del(&zspage->list);
2147 lock_zspage(zspage);
2148
2149 get_zspage_mapping(zspage, &class_idx, &fullness);
2150 VM_BUG_ON(fullness != ZS_EMPTY);
2151 class = pool->size_class[class_idx];
2152 spin_lock(&class->lock);
2153 __free_zspage(pool, pool->size_class[class_idx], zspage);
2154 spin_unlock(&class->lock);
2155 }
2156};
2157
2158static void kick_deferred_free(struct zs_pool *pool)
2159{
2160 schedule_work(&pool->free_work);
2161}
2162
2163static void init_deferred_free(struct zs_pool *pool)
2164{
2165 INIT_WORK(&pool->free_work, async_free_zspage);
2166}
2167
2168static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2169{
2170 struct page *page = get_first_page(zspage);
2171
2172 do {
2173 WARN_ON(!trylock_page(page));
2174 __SetPageMovable(page, pool->inode->i_mapping);
2175 unlock_page(page);
2176 } while ((page = get_next_page(page)) != NULL);
2177}
2178#endif
2179
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002180/*
2181 *
2182 * Based on the number of unused allocated objects calculate
2183 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002184 */
2185static unsigned long zs_can_compact(struct size_class *class)
2186{
2187 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002188 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2189 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002190
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002191 if (obj_allocated <= obj_used)
2192 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002193
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002194 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002195 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002196
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002197 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002198}
2199
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002200static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002201{
Minchan Kim312fcae2015-04-15 16:15:30 -07002202 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002203 struct zspage *src_zspage;
2204 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002205
Minchan Kim312fcae2015-04-15 16:15:30 -07002206 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002207 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002208
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002209 if (!zs_can_compact(class))
2210 break;
2211
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002212 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002213 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002214
Minchan Kim37836892016-07-26 15:23:23 -07002215 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002216 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002217 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002218 * If there is no more space in dst_page, resched
2219 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002220 */
2221 if (!migrate_zspage(pool, class, &cc))
2222 break;
2223
Minchan Kim4aa409c2016-07-26 15:23:26 -07002224 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002225 }
2226
2227 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002228 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002229 break;
2230
Minchan Kim4aa409c2016-07-26 15:23:26 -07002231 putback_zspage(class, dst_zspage);
2232 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002233 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002234 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002235 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002236 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002237 cond_resched();
2238 spin_lock(&class->lock);
2239 }
2240
Minchan Kim37836892016-07-26 15:23:23 -07002241 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002242 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002243
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002244 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002245}
2246
2247unsigned long zs_compact(struct zs_pool *pool)
2248{
2249 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002250 struct size_class *class;
2251
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002252 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002253 class = pool->size_class[i];
2254 if (!class)
2255 continue;
2256 if (class->index != i)
2257 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002258 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002259 }
2260
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002261 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002262}
2263EXPORT_SYMBOL_GPL(zs_compact);
2264
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002265void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2266{
2267 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2268}
2269EXPORT_SYMBOL_GPL(zs_pool_stats);
2270
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002271static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2272 struct shrink_control *sc)
2273{
2274 unsigned long pages_freed;
2275 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2276 shrinker);
2277
2278 pages_freed = pool->stats.pages_compacted;
2279 /*
2280 * Compact classes and calculate compaction delta.
2281 * Can run concurrently with a manually triggered
2282 * (by user) compaction.
2283 */
2284 pages_freed = zs_compact(pool) - pages_freed;
2285
2286 return pages_freed ? pages_freed : SHRINK_STOP;
2287}
2288
2289static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2290 struct shrink_control *sc)
2291{
2292 int i;
2293 struct size_class *class;
2294 unsigned long pages_to_free = 0;
2295 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2296 shrinker);
2297
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002298 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002299 class = pool->size_class[i];
2300 if (!class)
2301 continue;
2302 if (class->index != i)
2303 continue;
2304
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002305 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002306 }
2307
2308 return pages_to_free;
2309}
2310
2311static void zs_unregister_shrinker(struct zs_pool *pool)
2312{
2313 if (pool->shrinker_enabled) {
2314 unregister_shrinker(&pool->shrinker);
2315 pool->shrinker_enabled = false;
2316 }
2317}
2318
2319static int zs_register_shrinker(struct zs_pool *pool)
2320{
2321 pool->shrinker.scan_objects = zs_shrinker_scan;
2322 pool->shrinker.count_objects = zs_shrinker_count;
2323 pool->shrinker.batch = 0;
2324 pool->shrinker.seeks = DEFAULT_SEEKS;
2325
2326 return register_shrinker(&pool->shrinker);
2327}
2328
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002329/**
2330 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002331 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002332 *
2333 * This function must be called before anything when using
2334 * the zsmalloc allocator.
2335 *
2336 * On success, a pointer to the newly created pool is returned,
2337 * otherwise NULL.
2338 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002339struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002340{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002341 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002342 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002343 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002344
Ganesh Mahendran18136652014-12-12 16:57:10 -08002345 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002346 if (!pool)
2347 return NULL;
2348
Minchan Kim48b48002016-07-26 15:23:31 -07002349 init_deferred_free(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002350
Minchan Kim2e40e162015-04-15 16:15:23 -07002351 pool->name = kstrdup(name, GFP_KERNEL);
2352 if (!pool->name)
2353 goto err;
2354
Minchan Kim37836892016-07-26 15:23:23 -07002355 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002356 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002357
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002358 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002359 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002360 * for merging should be larger or equal to current size.
2361 */
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002362 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002363 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002364 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002365 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002366 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002367 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002368
2369 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2370 if (size > ZS_MAX_ALLOC_SIZE)
2371 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002372 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002373 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002374
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002375 /*
2376 * size_class is used for normal zsmalloc operation such
2377 * as alloc/free for that size. Although it is natural that we
2378 * have one size_class for each size, there is a chance that we
2379 * can get more memory utilization if we use one size_class for
2380 * many different sizes whose size_class have same
2381 * characteristics. So, we makes size_class point to
2382 * previous size_class if possible.
2383 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002384 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002385 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002386 pool->size_class[i] = prev_class;
2387 continue;
2388 }
2389 }
2390
2391 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2392 if (!class)
2393 goto err;
2394
Nitin Gupta61989a82012-01-09 16:51:56 -06002395 class->size = size;
2396 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002397 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002398 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002399 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002400 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002401 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2402 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002403 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002404
2405 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002406 }
2407
Dan Streetmand34f6152016-05-20 16:59:56 -07002408 /* debug only, don't abort if it fails */
2409 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002410
Minchan Kim48b48002016-07-26 15:23:31 -07002411 if (zs_register_migration(pool))
2412 goto err;
2413
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002414 /*
2415 * Not critical, we still can use the pool
2416 * and user can trigger compaction manually.
2417 */
2418 if (zs_register_shrinker(pool) == 0)
2419 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002420 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002421
2422err:
2423 zs_destroy_pool(pool);
2424 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002425}
2426EXPORT_SYMBOL_GPL(zs_create_pool);
2427
2428void zs_destroy_pool(struct zs_pool *pool)
2429{
2430 int i;
2431
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002432 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002433 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002434 zs_pool_stat_destroy(pool);
2435
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002436 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002437 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002438 struct size_class *class = pool->size_class[i];
2439
2440 if (!class)
2441 continue;
2442
2443 if (class->index != i)
2444 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002445
Minchan Kim48b48002016-07-26 15:23:31 -07002446 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002447 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002448 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002449 class->size, fg);
2450 }
2451 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002452 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002453 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002454
Minchan Kim37836892016-07-26 15:23:23 -07002455 destroy_cache(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002456 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002457 kfree(pool);
2458}
2459EXPORT_SYMBOL_GPL(zs_destroy_pool);
2460
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002461static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002462{
Minchan Kim48b48002016-07-26 15:23:31 -07002463 int ret;
2464
2465 ret = zsmalloc_mount();
2466 if (ret)
2467 goto out;
2468
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002469 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2470 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002471 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002472 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002473
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002474#ifdef CONFIG_ZPOOL
2475 zpool_register_driver(&zs_zpool_driver);
2476#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002477
Dan Streetman4abaac92016-05-26 15:16:27 -07002478 zs_stat_init();
2479
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002480 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002481
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002482hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002483 zsmalloc_unmount();
2484out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002485 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002486}
Nitin Gupta61989a82012-01-09 16:51:56 -06002487
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002488static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002489{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002490#ifdef CONFIG_ZPOOL
2491 zpool_unregister_driver(&zs_zpool_driver);
2492#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002493 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002494 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002495
2496 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002497}
Ben Hutchings069f1012012-06-20 02:31:11 +01002498
2499module_init(zs_init);
2500module_exit(zs_exit);
2501
2502MODULE_LICENSE("Dual BSD/GPL");
2503MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");