blob: b7b1fb6c8c21d4d4d1c6b7cc17f179e062dce94b [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>
Nitin Gupta61989a82012-01-09 16:51:56 -060036#include <linux/bitops.h>
37#include <linux/errno.h>
38#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060039#include <linux/string.h>
40#include <linux/slab.h>
41#include <asm/tlbflush.h>
42#include <asm/pgtable.h>
43#include <linux/cpumask.h>
44#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060045#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080046#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090047#include <linux/spinlock.h>
48#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080049#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080050#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070051#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070052#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070053#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070054#include <linux/pagemap.h>
55
56#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090057
58/*
59 * This must be power of 2 and greater than of equal to sizeof(link_free).
60 * These two conditions ensure that any 'struct link_free' itself doesn't
61 * span more than 1 page which avoids complex case of mapping 2 pages simply
62 * to restore link_free pointer values.
63 */
64#define ZS_ALIGN 8
65
66/*
67 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
68 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
69 */
70#define ZS_MAX_ZSPAGE_ORDER 2
71#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
72
Minchan Kim2e40e162015-04-15 16:15:23 -070073#define ZS_HANDLE_SIZE (sizeof(unsigned long))
74
Seth Jennings0959c632012-08-08 15:12:17 +090075/*
76 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090077 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090078 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070079 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090080 *
81 * This is made more complicated by various memory models and PAE.
82 */
83
84#ifndef MAX_PHYSMEM_BITS
85#ifdef CONFIG_HIGHMEM64G
86#define MAX_PHYSMEM_BITS 36
87#else /* !CONFIG_HIGHMEM64G */
88/*
89 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
90 * be PAGE_SHIFT
91 */
92#define MAX_PHYSMEM_BITS BITS_PER_LONG
93#endif
94#endif
95#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070096
97/*
98 * Memory for allocating for handle keeps object position by
99 * encoding <page, obj_idx> and the encoded value has a room
100 * in least bit(ie, look at obj_to_location).
101 * We use the bit to synchronize between object access by
102 * user and migration.
103 */
104#define HANDLE_PIN_BIT 0
105
106/*
107 * Head in allocated object should have OBJ_ALLOCATED_TAG
108 * to identify the object was allocated or not.
109 * It's okay to add the status bit in the least bit because
110 * header keeps handle which is 4byte-aligned address so we
111 * have room for two bit at least.
112 */
113#define OBJ_ALLOCATED_TAG 1
114#define OBJ_TAG_BITS 1
115#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900116#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
117
118#define MAX(a, b) ((a) >= (b) ? (a) : (b))
119/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
120#define ZS_MIN_ALLOC_SIZE \
121 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700122/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700123#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900124
125/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700126 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900127 * trader-off here:
128 * - Large number of size classes is potentially wasteful as free page are
129 * spread across these classes
130 * - Small number of size classes causes large internal fragmentation
131 * - Probably its better to use specific size classes (empirically
132 * determined). NOTE: all those class sizes must be set as multiple of
133 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
134 *
135 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
136 * (reason above)
137 */
Minchan Kim37836892016-07-26 15:23:23 -0700138#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900139
Seth Jennings0959c632012-08-08 15:12:17 +0900140enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900141 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700142 ZS_ALMOST_EMPTY,
143 ZS_ALMOST_FULL,
144 ZS_FULL,
145 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900146};
147
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800148enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700149 CLASS_EMPTY,
150 CLASS_ALMOST_EMPTY,
151 CLASS_ALMOST_FULL,
152 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800153 OBJ_ALLOCATED,
154 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700155 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800156};
157
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800158struct zs_size_stat {
159 unsigned long objs[NR_ZS_STAT_TYPE];
160};
161
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700162#ifdef CONFIG_ZSMALLOC_STAT
163static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800164#endif
165
Minchan Kim48b48002016-07-26 15:23:31 -0700166#ifdef CONFIG_COMPACTION
167static struct vfsmount *zsmalloc_mnt;
168#endif
169
Seth Jennings0959c632012-08-08 15:12:17 +0900170/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800171 * number of size_classes
172 */
173static int zs_size_classes;
174
175/*
Seth Jennings0959c632012-08-08 15:12:17 +0900176 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
177 * n <= N / f, where
178 * n = number of allocated objects
179 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700180 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900181 *
182 * Similarly, we assign zspage to:
183 * ZS_ALMOST_FULL when n > N / f
184 * ZS_EMPTY when n == 0
185 * ZS_FULL when n == N
186 *
187 * (see: fix_fullness_group())
188 */
189static const int fullness_threshold_frac = 4;
190
191struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700192 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700193 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900194 /*
195 * Size of objects stored in this class. Must be multiple
196 * of ZS_ALIGN.
197 */
198 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700199 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800200 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
201 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700202
203 unsigned int index;
204 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900205};
206
Minchan Kim48b48002016-07-26 15:23:31 -0700207/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
208static void SetPageHugeObject(struct page *page)
209{
210 SetPageOwnerPriv1(page);
211}
212
213static void ClearPageHugeObject(struct page *page)
214{
215 ClearPageOwnerPriv1(page);
216}
217
218static int PageHugeObject(struct page *page)
219{
220 return PageOwnerPriv1(page);
221}
222
Seth Jennings0959c632012-08-08 15:12:17 +0900223/*
224 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700225 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900226 *
227 * This must be power of 2 and less than or equal to ZS_ALIGN
228 */
229struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700230 union {
231 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700232 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700233 * It's valid for non-allocated object
234 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700235 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700236 /*
237 * Handle of allocated object.
238 */
239 unsigned long handle;
240 };
Seth Jennings0959c632012-08-08 15:12:17 +0900241};
242
243struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800244 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800245
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800246 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700247 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700248 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900249
Minchan Kim13de8932014-10-09 15:29:48 -0700250 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800251
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700252 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700253
254 /* Compact classes */
255 struct shrinker shrinker;
256 /*
257 * To signify that register_shrinker() was successful
258 * and unregister_shrinker() will not Oops.
259 */
260 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800261#ifdef CONFIG_ZSMALLOC_STAT
262 struct dentry *stat_dentry;
263#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700264#ifdef CONFIG_COMPACTION
265 struct inode *inode;
266 struct work_struct free_work;
267#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900268};
Nitin Gupta61989a82012-01-09 16:51:56 -0600269
Minchan Kim37836892016-07-26 15:23:23 -0700270#define FULLNESS_BITS 2
271#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700272#define ISOLATED_BITS 3
273#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700274
Minchan Kim37836892016-07-26 15:23:23 -0700275struct zspage {
276 struct {
277 unsigned int fullness:FULLNESS_BITS;
278 unsigned int class:CLASS_BITS;
Minchan Kim48b48002016-07-26 15:23:31 -0700279 unsigned int isolated:ISOLATED_BITS;
280 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700281 };
282 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700283 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700284 struct page *first_page;
285 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700286#ifdef CONFIG_COMPACTION
287 rwlock_t lock;
288#endif
Minchan Kim37836892016-07-26 15:23:23 -0700289};
Nitin Gupta61989a82012-01-09 16:51:56 -0600290
Seth Jenningsf5536462012-07-18 11:55:56 -0500291struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900292#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500293 struct vm_struct *vm; /* vm area for mapping object that span pages */
294#else
295 char *vm_buf; /* copy buffer for objects that span pages */
296#endif
297 char *vm_addr; /* address of kmap_atomic()'ed pages */
298 enum zs_mapmode vm_mm; /* mapping mode */
299};
300
Minchan Kim48b48002016-07-26 15:23:31 -0700301#ifdef CONFIG_COMPACTION
302static int zs_register_migration(struct zs_pool *pool);
303static void zs_unregister_migration(struct zs_pool *pool);
304static void migrate_lock_init(struct zspage *zspage);
305static void migrate_read_lock(struct zspage *zspage);
306static void migrate_read_unlock(struct zspage *zspage);
307static void kick_deferred_free(struct zs_pool *pool);
308static void init_deferred_free(struct zs_pool *pool);
309static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
310#else
311static int zsmalloc_mount(void) { return 0; }
312static void zsmalloc_unmount(void) {}
313static int zs_register_migration(struct zs_pool *pool) { return 0; }
314static void zs_unregister_migration(struct zs_pool *pool) {}
315static void migrate_lock_init(struct zspage *zspage) {}
316static void migrate_read_lock(struct zspage *zspage) {}
317static void migrate_read_unlock(struct zspage *zspage) {}
318static void kick_deferred_free(struct zs_pool *pool) {}
319static void init_deferred_free(struct zs_pool *pool) {}
320static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
321#endif
322
Minchan Kim37836892016-07-26 15:23:23 -0700323static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700324{
325 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
326 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700327 if (!pool->handle_cachep)
328 return 1;
329
330 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
331 0, 0, NULL);
332 if (!pool->zspage_cachep) {
333 kmem_cache_destroy(pool->handle_cachep);
334 pool->handle_cachep = NULL;
335 return 1;
336 }
337
338 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700339}
340
Minchan Kim37836892016-07-26 15:23:23 -0700341static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700342{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700343 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700344 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700345}
346
Minchan Kim37836892016-07-26 15:23:23 -0700347static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700348{
349 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700350 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700351}
352
Minchan Kim37836892016-07-26 15:23:23 -0700353static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700354{
355 kmem_cache_free(pool->handle_cachep, (void *)handle);
356}
357
Minchan Kim37836892016-07-26 15:23:23 -0700358static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
359{
Minchan Kim48b48002016-07-26 15:23:31 -0700360 return kmem_cache_alloc(pool->zspage_cachep,
361 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800362}
Minchan Kim37836892016-07-26 15:23:23 -0700363
364static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
365{
366 kmem_cache_free(pool->zspage_cachep, zspage);
367}
368
Minchan Kim2e40e162015-04-15 16:15:23 -0700369static void record_obj(unsigned long handle, unsigned long obj)
370{
Junil Leec102f07c2016-01-20 14:58:18 -0800371 /*
372 * lsb of @obj represents handle lock while other bits
373 * represent object value the handle is pointing so
374 * updating shouldn't do store tearing.
375 */
376 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700377}
378
Dan Streetmanc7957792014-08-06 16:08:38 -0700379/* zpool driver */
380
381#ifdef CONFIG_ZPOOL
382
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800383static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700384 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700385 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700386{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700387 /*
388 * Ignore global gfp flags: zs_malloc() may be invoked from
389 * different contexts and its caller must provide a valid
390 * gfp mask.
391 */
392 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700393}
394
395static void zs_zpool_destroy(void *pool)
396{
397 zs_destroy_pool(pool);
398}
399
400static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
401 unsigned long *handle)
402{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700403 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700404 return *handle ? 0 : -1;
405}
406static void zs_zpool_free(void *pool, unsigned long handle)
407{
408 zs_free(pool, handle);
409}
410
411static int zs_zpool_shrink(void *pool, unsigned int pages,
412 unsigned int *reclaimed)
413{
414 return -EINVAL;
415}
416
417static void *zs_zpool_map(void *pool, unsigned long handle,
418 enum zpool_mapmode mm)
419{
420 enum zs_mapmode zs_mm;
421
422 switch (mm) {
423 case ZPOOL_MM_RO:
424 zs_mm = ZS_MM_RO;
425 break;
426 case ZPOOL_MM_WO:
427 zs_mm = ZS_MM_WO;
428 break;
429 case ZPOOL_MM_RW: /* fallthru */
430 default:
431 zs_mm = ZS_MM_RW;
432 break;
433 }
434
435 return zs_map_object(pool, handle, zs_mm);
436}
437static void zs_zpool_unmap(void *pool, unsigned long handle)
438{
439 zs_unmap_object(pool, handle);
440}
441
442static u64 zs_zpool_total_size(void *pool)
443{
Minchan Kim722cdc12014-10-09 15:29:50 -0700444 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700445}
446
447static struct zpool_driver zs_zpool_driver = {
448 .type = "zsmalloc",
449 .owner = THIS_MODULE,
450 .create = zs_zpool_create,
451 .destroy = zs_zpool_destroy,
452 .malloc = zs_zpool_malloc,
453 .free = zs_zpool_free,
454 .shrink = zs_zpool_shrink,
455 .map = zs_zpool_map,
456 .unmap = zs_zpool_unmap,
457 .total_size = zs_zpool_total_size,
458};
459
Kees Cook137f8cf2014-08-29 15:18:40 -0700460MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700461#endif /* CONFIG_ZPOOL */
462
Nitin Gupta61989a82012-01-09 16:51:56 -0600463/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
464static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
465
Minchan Kim48b48002016-07-26 15:23:31 -0700466static bool is_zspage_isolated(struct zspage *zspage)
467{
468 return zspage->isolated;
469}
470
Nitin Gupta61989a82012-01-09 16:51:56 -0600471static int is_first_page(struct page *page)
472{
Minchan Kima27545bf2012-04-25 15:23:09 +0900473 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600474}
475
Minchan Kim48b48002016-07-26 15:23:31 -0700476/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700477static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600478{
Minchan Kim37836892016-07-26 15:23:23 -0700479 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600480}
481
Minchan Kim37836892016-07-26 15:23:23 -0700482static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700483{
Minchan Kim37836892016-07-26 15:23:23 -0700484 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700485}
486
Minchan Kim37836892016-07-26 15:23:23 -0700487static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700488{
Minchan Kim37836892016-07-26 15:23:23 -0700489 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700490}
491
Minchan Kim48b48002016-07-26 15:23:31 -0700492static inline struct page *get_first_page(struct zspage *zspage)
493{
494 struct page *first_page = zspage->first_page;
495
496 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
497 return first_page;
498}
499
Minchan Kim4f420472016-07-26 15:23:17 -0700500static inline int get_first_obj_offset(struct page *page)
501{
Minchan Kim48b48002016-07-26 15:23:31 -0700502 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700503}
504
505static inline void set_first_obj_offset(struct page *page, int offset)
506{
Minchan Kim48b48002016-07-26 15:23:31 -0700507 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700508}
509
Minchan Kimbfd093f2016-07-26 15:23:28 -0700510static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700511{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700512 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700513}
514
Minchan Kimbfd093f2016-07-26 15:23:28 -0700515static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700516{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700517 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700518}
519
Minchan Kim37836892016-07-26 15:23:23 -0700520static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700521 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600522 enum fullness_group *fullness)
523{
Minchan Kim48b48002016-07-26 15:23:31 -0700524 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
525
Minchan Kim37836892016-07-26 15:23:23 -0700526 *fullness = zspage->fullness;
527 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600528}
529
Minchan Kim37836892016-07-26 15:23:23 -0700530static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700531 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600532 enum fullness_group fullness)
533{
Minchan Kim37836892016-07-26 15:23:23 -0700534 zspage->class = class_idx;
535 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600536}
537
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900538/*
539 * zsmalloc divides the pool into various size classes where each
540 * class maintains a list of zspages where each zspage is divided
541 * into equal sized chunks. Each allocation falls into one of these
542 * classes depending on its size. This function returns index of the
543 * size class which has chunk size big enough to hold the give size.
544 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600545static int get_size_class_index(int size)
546{
547 int idx = 0;
548
549 if (likely(size > ZS_MIN_ALLOC_SIZE))
550 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
551 ZS_SIZE_CLASS_DELTA);
552
Minchan Kim7b60a682015-04-15 16:15:39 -0700553 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600554}
555
Minchan Kim248ca1b2015-04-15 16:15:42 -0700556static inline void zs_stat_inc(struct size_class *class,
557 enum zs_stat_type type, unsigned long cnt)
558{
Minchan Kim48b48002016-07-26 15:23:31 -0700559 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700560}
561
562static inline void zs_stat_dec(struct size_class *class,
563 enum zs_stat_type type, unsigned long cnt)
564{
Minchan Kim48b48002016-07-26 15:23:31 -0700565 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700566}
567
568static inline unsigned long zs_stat_get(struct size_class *class,
569 enum zs_stat_type type)
570{
Minchan Kim48b48002016-07-26 15:23:31 -0700571 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700572}
573
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700574#ifdef CONFIG_ZSMALLOC_STAT
575
Dan Streetman4abaac92016-05-26 15:16:27 -0700576static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700577{
Dan Streetman4abaac92016-05-26 15:16:27 -0700578 if (!debugfs_initialized()) {
579 pr_warn("debugfs not available, stat dir not created\n");
580 return;
581 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700582
583 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
584 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700585 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700586}
587
588static void __exit zs_stat_exit(void)
589{
590 debugfs_remove_recursive(zs_stat_root);
591}
592
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700593static unsigned long zs_can_compact(struct size_class *class);
594
Minchan Kim248ca1b2015-04-15 16:15:42 -0700595static int zs_stats_size_show(struct seq_file *s, void *v)
596{
597 int i;
598 struct zs_pool *pool = s->private;
599 struct size_class *class;
600 int objs_per_zspage;
601 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700602 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700603 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
604 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700605 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700606
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700607 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700608 "class", "size", "almost_full", "almost_empty",
609 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700610 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700611
612 for (i = 0; i < zs_size_classes; i++) {
613 class = pool->size_class[i];
614
615 if (class->index != i)
616 continue;
617
618 spin_lock(&class->lock);
619 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
620 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
621 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
622 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700623 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700624 spin_unlock(&class->lock);
625
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700626 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700627 pages_used = obj_allocated / objs_per_zspage *
628 class->pages_per_zspage;
629
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700630 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
631 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700632 i, class->size, class_almost_full, class_almost_empty,
633 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700634 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700635
636 total_class_almost_full += class_almost_full;
637 total_class_almost_empty += class_almost_empty;
638 total_objs += obj_allocated;
639 total_used_objs += obj_used;
640 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700641 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700642 }
643
644 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700645 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700646 "Total", "", total_class_almost_full,
647 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700648 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700649
650 return 0;
651}
652
653static int zs_stats_size_open(struct inode *inode, struct file *file)
654{
655 return single_open(file, zs_stats_size_show, inode->i_private);
656}
657
658static const struct file_operations zs_stat_size_ops = {
659 .open = zs_stats_size_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = single_release,
663};
664
Dan Streetmand34f6152016-05-20 16:59:56 -0700665static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700666{
667 struct dentry *entry;
668
Dan Streetman4abaac92016-05-26 15:16:27 -0700669 if (!zs_stat_root) {
670 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700671 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700672 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700673
674 entry = debugfs_create_dir(name, zs_stat_root);
675 if (!entry) {
676 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700677 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700678 }
679 pool->stat_dentry = entry;
680
681 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
682 pool->stat_dentry, pool, &zs_stat_size_ops);
683 if (!entry) {
684 pr_warn("%s: debugfs file entry <%s> creation failed\n",
685 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700686 debugfs_remove_recursive(pool->stat_dentry);
687 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700688 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700689}
690
691static void zs_pool_stat_destroy(struct zs_pool *pool)
692{
693 debugfs_remove_recursive(pool->stat_dentry);
694}
695
696#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700697static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700698{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700699}
700
701static void __exit zs_stat_exit(void)
702{
703}
704
Dan Streetmand34f6152016-05-20 16:59:56 -0700705static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700706{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700707}
708
709static inline void zs_pool_stat_destroy(struct zs_pool *pool)
710{
711}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700712#endif
713
Minchan Kim48b48002016-07-26 15:23:31 -0700714
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900715/*
716 * For each size class, zspages are divided into different groups
717 * depending on how "full" they are. This was done so that we could
718 * easily find empty or nearly empty zspages when we try to shrink
719 * the pool (not yet implemented). This function returns fullness
720 * status of the given page.
721 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700722static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700723 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600724{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700725 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600726 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700727
Minchan Kim37836892016-07-26 15:23:23 -0700728 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700729 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600730
731 if (inuse == 0)
732 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700733 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600734 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700735 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600736 fg = ZS_ALMOST_EMPTY;
737 else
738 fg = ZS_ALMOST_FULL;
739
740 return fg;
741}
742
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900743/*
744 * Each size class maintains various freelists and zspages are assigned
745 * to one of these freelists based on the number of live objects they
746 * have. This functions inserts the given zspage into the freelist
747 * identified by <class, fullness_group>.
748 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700749static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700750 struct zspage *zspage,
751 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600752{
Minchan Kim37836892016-07-26 15:23:23 -0700753 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600754
Minchan Kim48b48002016-07-26 15:23:31 -0700755 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700756 head = list_first_entry_or_null(&class->fullness_list[fullness],
757 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700758 /*
Minchan Kim37836892016-07-26 15:23:23 -0700759 * We want to see more ZS_FULL pages and less almost empty/full.
760 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700761 */
Minchan Kim37836892016-07-26 15:23:23 -0700762 if (head) {
763 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
764 list_add(&zspage->list, &head->list);
765 return;
766 }
767 }
768 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600769}
770
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900771/*
772 * This function removes the given zspage from the freelist identified
773 * by <class, fullness_group>.
774 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700775static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700776 struct zspage *zspage,
777 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600778{
Minchan Kim37836892016-07-26 15:23:23 -0700779 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700780 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600781
Minchan Kim37836892016-07-26 15:23:23 -0700782 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700783 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600784}
785
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900786/*
787 * Each size class maintains zspages in different fullness groups depending
788 * on the number of live objects they contain. When allocating or freeing
789 * objects, the fullness status of the page can change, say, from ALMOST_FULL
790 * to ALMOST_EMPTY when freeing an object. This function checks if such
791 * a status change has occurred for the given page and accordingly moves the
792 * page from the freelist of the old fullness group to that of the new
793 * fullness group.
794 */
Minchan Kimc7806262015-04-15 16:15:26 -0700795static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700796 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600797{
798 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600799 enum fullness_group currfg, newfg;
800
Minchan Kim37836892016-07-26 15:23:23 -0700801 get_zspage_mapping(zspage, &class_idx, &currfg);
802 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600803 if (newfg == currfg)
804 goto out;
805
Minchan Kim48b48002016-07-26 15:23:31 -0700806 if (!is_zspage_isolated(zspage)) {
807 remove_zspage(class, zspage, currfg);
808 insert_zspage(class, zspage, newfg);
809 }
810
Minchan Kim37836892016-07-26 15:23:23 -0700811 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600812
813out:
814 return newfg;
815}
816
817/*
818 * We have to decide on how many pages to link together
819 * to form a zspage for each size class. This is important
820 * to reduce wastage due to unusable space left at end of
821 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700822 * wastage = Zp % class_size
823 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600824 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
825 *
826 * For example, for size class of 3/8 * PAGE_SIZE, we should
827 * link together 3 PAGE_SIZE sized pages to form a zspage
828 * since then we can perfectly fit in 8 such objects.
829 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900830static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600831{
832 int i, max_usedpc = 0;
833 /* zspage order which gives maximum used size per KB */
834 int max_usedpc_order = 1;
835
Seth Jennings84d4faa2012-03-05 11:33:21 -0600836 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600837 int zspage_size;
838 int waste, usedpc;
839
840 zspage_size = i * PAGE_SIZE;
841 waste = zspage_size % class_size;
842 usedpc = (zspage_size - waste) * 100 / zspage_size;
843
844 if (usedpc > max_usedpc) {
845 max_usedpc = usedpc;
846 max_usedpc_order = i;
847 }
848 }
849
850 return max_usedpc_order;
851}
852
Minchan Kim37836892016-07-26 15:23:23 -0700853static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600854{
Minchan Kim48b48002016-07-26 15:23:31 -0700855 struct zspage *zspage = (struct zspage *)page->private;
856
857 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
858 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600859}
860
861static struct page *get_next_page(struct page *page)
862{
Minchan Kim48b48002016-07-26 15:23:31 -0700863 if (unlikely(PageHugeObject(page)))
864 return NULL;
865
866 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600867}
868
Minchan Kimbfd093f2016-07-26 15:23:28 -0700869/**
870 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
871 * @page: page object resides in zspage
872 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800873 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700874static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700875 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600876{
Minchan Kim312fcae2015-04-15 16:15:30 -0700877 obj >>= OBJ_TAG_BITS;
878 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
879 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600880}
881
Minchan Kimbfd093f2016-07-26 15:23:28 -0700882/**
883 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
884 * @page: page object resides in zspage
885 * @obj_idx: object index
886 */
887static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
888{
889 unsigned long obj;
890
891 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
892 obj |= obj_idx & OBJ_INDEX_MASK;
893 obj <<= OBJ_TAG_BITS;
894
895 return obj;
896}
897
Minchan Kim2e40e162015-04-15 16:15:23 -0700898static unsigned long handle_to_obj(unsigned long handle)
899{
900 return *(unsigned long *)handle;
901}
902
Minchan Kim48b48002016-07-26 15:23:31 -0700903static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700904{
Minchan Kim48b48002016-07-26 15:23:31 -0700905 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700906 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700907 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700908 } else
909 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700910}
911
Minchan Kim48b48002016-07-26 15:23:31 -0700912static inline int testpin_tag(unsigned long handle)
913{
914 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
915}
916
Minchan Kim312fcae2015-04-15 16:15:30 -0700917static inline int trypin_tag(unsigned long handle)
918{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700919 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700920}
921
922static void pin_tag(unsigned long handle)
923{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700924 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700925}
926
927static void unpin_tag(unsigned long handle)
928{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700929 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700930}
931
Nitin Guptaf4477e92012-04-02 09:13:56 -0500932static void reset_page(struct page *page)
933{
Minchan Kim48b48002016-07-26 15:23:31 -0700934 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700935 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500936 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700937 page_mapcount_reset(page);
938 ClearPageHugeObject(page);
939 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500940}
941
Minchan Kim48b48002016-07-26 15:23:31 -0700942/*
943 * To prevent zspage destroy during migration, zspage freeing should
944 * hold locks of all pages in the zspage.
945 */
946void lock_zspage(struct zspage *zspage)
947{
948 struct page *page = get_first_page(zspage);
949
950 do {
951 lock_page(page);
952 } while ((page = get_next_page(page)) != NULL);
953}
954
955int trylock_zspage(struct zspage *zspage)
956{
957 struct page *cursor, *fail;
958
959 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
960 get_next_page(cursor)) {
961 if (!trylock_page(cursor)) {
962 fail = cursor;
963 goto unlock;
964 }
965 }
966
967 return 1;
968unlock:
969 for (cursor = get_first_page(zspage); cursor != fail; cursor =
970 get_next_page(cursor))
971 unlock_page(cursor);
972
973 return 0;
974}
975
976static void __free_zspage(struct zs_pool *pool, struct size_class *class,
977 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600978{
Minchan Kim37836892016-07-26 15:23:23 -0700979 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700980 enum fullness_group fg;
981 unsigned int class_idx;
982
983 get_zspage_mapping(zspage, &class_idx, &fg);
984
985 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600986
Minchan Kim37836892016-07-26 15:23:23 -0700987 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700988 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600989
Minchan Kim48b48002016-07-26 15:23:31 -0700990 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700991 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700992 VM_BUG_ON_PAGE(!PageLocked(page), page);
993 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700994 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700995 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -0700996 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -0700997 put_page(page);
998 page = next;
999 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001000
Minchan Kim37836892016-07-26 15:23:23 -07001001 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001002
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001003 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001004 atomic_long_sub(class->pages_per_zspage,
1005 &pool->pages_allocated);
1006}
1007
1008static void free_zspage(struct zs_pool *pool, struct size_class *class,
1009 struct zspage *zspage)
1010{
1011 VM_BUG_ON(get_zspage_inuse(zspage));
1012 VM_BUG_ON(list_empty(&zspage->list));
1013
1014 if (!trylock_zspage(zspage)) {
1015 kick_deferred_free(pool);
1016 return;
1017 }
1018
1019 remove_zspage(class, zspage, ZS_EMPTY);
1020 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001021}
1022
1023/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001024static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001025{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001026 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001027 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001028 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001029
Nitin Gupta61989a82012-01-09 16:51:56 -06001030 while (page) {
1031 struct page *next_page;
1032 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001033 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001034
Minchan Kim37836892016-07-26 15:23:23 -07001035 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001036
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001037 vaddr = kmap_atomic(page);
1038 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001039
Dan Streetman5538c562014-10-09 15:30:01 -07001040 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001041 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001042 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001043 }
1044
1045 /*
1046 * We now come to the last (full or partial) object on this
1047 * page, which must point to the first object on the next
1048 * page (if present)
1049 */
1050 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001051 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001052 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001053 } else {
1054 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001055 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001056 * whether it's allocated object or not.
1057 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001058 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001059 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001060 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001061 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001062 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001063 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001064
Minchan Kimbfd093f2016-07-26 15:23:28 -07001065 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001066}
1067
Minchan Kim48b48002016-07-26 15:23:31 -07001068static void create_page_chain(struct size_class *class, struct zspage *zspage,
1069 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001070{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001071 int i;
1072 struct page *page;
1073 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001074 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001075
1076 /*
1077 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001078 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001079 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001080 *
Minchan Kim37836892016-07-26 15:23:23 -07001081 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001082 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001083 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001084 for (i = 0; i < nr_pages; i++) {
1085 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001086 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001087 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001088 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001089 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001090 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001091 if (unlikely(class->objs_per_zspage == 1 &&
1092 class->pages_per_zspage == 1))
1093 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001094 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001095 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001096 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001097 prev_page = page;
1098 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001099}
Nitin Gupta61989a82012-01-09 16:51:56 -06001100
Minchan Kimbdb0af72016-07-26 15:23:20 -07001101/*
1102 * Allocate a zspage for the given size class
1103 */
Minchan Kim37836892016-07-26 15:23:23 -07001104static struct zspage *alloc_zspage(struct zs_pool *pool,
1105 struct size_class *class,
1106 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001107{
1108 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001109 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001110 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1111
1112 if (!zspage)
1113 return NULL;
1114
1115 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001116 zspage->magic = ZSPAGE_MAGIC;
1117 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001118
Minchan Kimbdb0af72016-07-26 15:23:20 -07001119 for (i = 0; i < class->pages_per_zspage; i++) {
1120 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001121
Minchan Kim37836892016-07-26 15:23:23 -07001122 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001123 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001124 while (--i >= 0) {
1125 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001126 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001127 }
Minchan Kim37836892016-07-26 15:23:23 -07001128 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001129 return NULL;
1130 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001131
1132 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001133 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001134 }
1135
Minchan Kim48b48002016-07-26 15:23:31 -07001136 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001137 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001138
Minchan Kim37836892016-07-26 15:23:23 -07001139 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001140}
1141
Minchan Kim37836892016-07-26 15:23:23 -07001142static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001143{
1144 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001145 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001146
Minchan Kim48b48002016-07-26 15:23:31 -07001147 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001148 zspage = list_first_entry_or_null(&class->fullness_list[i],
1149 struct zspage, list);
1150 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001151 break;
1152 }
1153
Minchan Kim37836892016-07-26 15:23:23 -07001154 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001155}
1156
Minchan Kim1b945ae2013-12-11 11:04:36 +09001157#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001158static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -05001159{
Seth Jenningsf5536462012-07-18 11:55:56 -05001160 /*
1161 * Make sure we don't leak memory if a cpu UP notification
1162 * and zs_init() race and both call zs_cpu_up() on the same cpu
1163 */
1164 if (area->vm)
1165 return 0;
1166 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1167 if (!area->vm)
1168 return -ENOMEM;
1169 return 0;
1170}
1171
1172static inline void __zs_cpu_down(struct mapping_area *area)
1173{
1174 if (area->vm)
1175 free_vm_area(area->vm);
1176 area->vm = NULL;
1177}
1178
1179static inline void *__zs_map_object(struct mapping_area *area,
1180 struct page *pages[2], int off, int size)
1181{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001182 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001183 area->vm_addr = area->vm->addr;
1184 return area->vm_addr + off;
1185}
1186
1187static inline void __zs_unmap_object(struct mapping_area *area,
1188 struct page *pages[2], int off, int size)
1189{
1190 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001191
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001192 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001193}
1194
Minchan Kim1b945ae2013-12-11 11:04:36 +09001195#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001196
1197static inline int __zs_cpu_up(struct mapping_area *area)
1198{
1199 /*
1200 * Make sure we don't leak memory if a cpu UP notification
1201 * and zs_init() race and both call zs_cpu_up() on the same cpu
1202 */
1203 if (area->vm_buf)
1204 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001205 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001206 if (!area->vm_buf)
1207 return -ENOMEM;
1208 return 0;
1209}
1210
1211static inline void __zs_cpu_down(struct mapping_area *area)
1212{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001213 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001214 area->vm_buf = NULL;
1215}
1216
1217static void *__zs_map_object(struct mapping_area *area,
1218 struct page *pages[2], int off, int size)
1219{
Seth Jennings5f6019022012-07-02 16:15:49 -05001220 int sizes[2];
1221 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001222 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001223
Seth Jenningsf5536462012-07-18 11:55:56 -05001224 /* disable page faults to match kmap_atomic() return conditions */
1225 pagefault_disable();
1226
1227 /* no read fastpath */
1228 if (area->vm_mm == ZS_MM_WO)
1229 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001230
1231 sizes[0] = PAGE_SIZE - off;
1232 sizes[1] = size - sizes[0];
1233
Seth Jennings5f6019022012-07-02 16:15:49 -05001234 /* copy object to per-cpu buffer */
1235 addr = kmap_atomic(pages[0]);
1236 memcpy(buf, addr + off, sizes[0]);
1237 kunmap_atomic(addr);
1238 addr = kmap_atomic(pages[1]);
1239 memcpy(buf + sizes[0], addr, sizes[1]);
1240 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001241out:
1242 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001243}
1244
Seth Jenningsf5536462012-07-18 11:55:56 -05001245static void __zs_unmap_object(struct mapping_area *area,
1246 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001247{
Seth Jennings5f6019022012-07-02 16:15:49 -05001248 int sizes[2];
1249 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001250 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001251
Seth Jenningsf5536462012-07-18 11:55:56 -05001252 /* no write fastpath */
1253 if (area->vm_mm == ZS_MM_RO)
1254 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001255
Minchan Kim7b60a682015-04-15 16:15:39 -07001256 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001257 buf = buf + ZS_HANDLE_SIZE;
1258 size -= ZS_HANDLE_SIZE;
1259 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001260
Seth Jennings5f6019022012-07-02 16:15:49 -05001261 sizes[0] = PAGE_SIZE - off;
1262 sizes[1] = size - sizes[0];
1263
1264 /* copy per-cpu buffer to object */
1265 addr = kmap_atomic(pages[0]);
1266 memcpy(addr + off, buf, sizes[0]);
1267 kunmap_atomic(addr);
1268 addr = kmap_atomic(pages[1]);
1269 memcpy(addr, buf + sizes[0], sizes[1]);
1270 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001271
1272out:
1273 /* enable page faults to match kunmap_atomic() return conditions */
1274 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001275}
Nitin Gupta61989a82012-01-09 16:51:56 -06001276
Minchan Kim1b945ae2013-12-11 11:04:36 +09001277#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001278
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001279static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001280{
Nitin Gupta61989a82012-01-09 16:51:56 -06001281 struct mapping_area *area;
1282
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001283 area = &per_cpu(zs_map_area, cpu);
1284 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001285}
1286
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001287static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001288{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001289 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001290
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001291 area = &per_cpu(zs_map_area, cpu);
1292 __zs_cpu_down(area);
1293 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001294}
1295
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001296static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001297{
1298 int nr;
1299
1300 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1301 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1302 nr += 1;
1303
1304 zs_size_classes = nr;
1305}
1306
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001307static bool can_merge(struct size_class *prev, int pages_per_zspage,
1308 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001309{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001310 if (prev->pages_per_zspage == pages_per_zspage &&
1311 prev->objs_per_zspage == objs_per_zspage)
1312 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001313
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001314 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001315}
1316
Minchan Kim37836892016-07-26 15:23:23 -07001317static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001318{
Minchan Kim37836892016-07-26 15:23:23 -07001319 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001320}
1321
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001322unsigned long zs_get_total_pages(struct zs_pool *pool)
1323{
1324 return atomic_long_read(&pool->pages_allocated);
1325}
1326EXPORT_SYMBOL_GPL(zs_get_total_pages);
1327
1328/**
1329 * zs_map_object - get address of allocated object from handle.
1330 * @pool: pool from which the object was allocated
1331 * @handle: handle returned from zs_malloc
1332 *
1333 * Before using an object allocated from zs_malloc, it must be mapped using
1334 * this function. When done with the object, it must be unmapped using
1335 * zs_unmap_object.
1336 *
1337 * Only one object can be mapped per cpu at a time. There is no protection
1338 * against nested mappings.
1339 *
1340 * This function returns with preemption and page faults disabled.
1341 */
1342void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1343 enum zs_mapmode mm)
1344{
Minchan Kim37836892016-07-26 15:23:23 -07001345 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001346 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001347 unsigned long obj, off;
1348 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001349
1350 unsigned int class_idx;
1351 enum fullness_group fg;
1352 struct size_class *class;
1353 struct mapping_area *area;
1354 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001355 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001356
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001357 /*
1358 * Because we use per-cpu mapping areas shared among the
1359 * pools/users, we can't allow mapping in interrupt context
1360 * because it can corrupt another users mappings.
1361 */
Minchan Kim830e4bc2016-05-20 16:59:39 -07001362 WARN_ON_ONCE(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001363
Minchan Kim312fcae2015-04-15 16:15:30 -07001364 /* From now on, migration cannot move the object */
1365 pin_tag(handle);
1366
Minchan Kim2e40e162015-04-15 16:15:23 -07001367 obj = handle_to_obj(handle);
1368 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001369 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001370
1371 /* migration cannot move any subpage in this zspage */
1372 migrate_read_lock(zspage);
1373
Minchan Kim37836892016-07-26 15:23:23 -07001374 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001375 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001376 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001377
1378 area = &get_cpu_var(zs_map_area);
1379 area->vm_mm = mm;
1380 if (off + class->size <= PAGE_SIZE) {
1381 /* this object is contained entirely within a page */
1382 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001383 ret = area->vm_addr + off;
1384 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001385 }
1386
1387 /* this object spans two pages */
1388 pages[0] = page;
1389 pages[1] = get_next_page(page);
1390 BUG_ON(!pages[1]);
1391
Minchan Kim2e40e162015-04-15 16:15:23 -07001392 ret = __zs_map_object(area, pages, off, class->size);
1393out:
Minchan Kim48b48002016-07-26 15:23:31 -07001394 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001395 ret += ZS_HANDLE_SIZE;
1396
1397 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001398}
1399EXPORT_SYMBOL_GPL(zs_map_object);
1400
1401void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1402{
Minchan Kim37836892016-07-26 15:23:23 -07001403 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001404 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001405 unsigned long obj, off;
1406 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001407
1408 unsigned int class_idx;
1409 enum fullness_group fg;
1410 struct size_class *class;
1411 struct mapping_area *area;
1412
Minchan Kim2e40e162015-04-15 16:15:23 -07001413 obj = handle_to_obj(handle);
1414 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001415 zspage = get_zspage(page);
1416 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001417 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001418 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001419
1420 area = this_cpu_ptr(&zs_map_area);
1421 if (off + class->size <= PAGE_SIZE)
1422 kunmap_atomic(area->vm_addr);
1423 else {
1424 struct page *pages[2];
1425
1426 pages[0] = page;
1427 pages[1] = get_next_page(page);
1428 BUG_ON(!pages[1]);
1429
1430 __zs_unmap_object(area, pages, off, class->size);
1431 }
1432 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001433
1434 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001435 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001436}
1437EXPORT_SYMBOL_GPL(zs_unmap_object);
1438
Minchan Kim251cbb92016-05-20 16:59:42 -07001439static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001440 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001441{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001442 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001443 unsigned long obj;
1444 struct link_free *link;
1445
1446 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001447 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001448 void *vaddr;
1449
Minchan Kim312fcae2015-04-15 16:15:30 -07001450 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001451 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001452
1453 offset = obj * class->size;
1454 nr_page = offset >> PAGE_SHIFT;
1455 m_offset = offset & ~PAGE_MASK;
1456 m_page = get_first_page(zspage);
1457
1458 for (i = 0; i < nr_page; i++)
1459 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001460
1461 vaddr = kmap_atomic(m_page);
1462 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001463 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001464 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001465 /* record handle in the header of allocated chunk */
1466 link->handle = handle;
1467 else
Minchan Kim37836892016-07-26 15:23:23 -07001468 /* record handle to page->index */
1469 zspage->first_page->index = handle;
1470
Minchan Kimc7806262015-04-15 16:15:26 -07001471 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001472 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001473 zs_stat_inc(class, OBJ_USED, 1);
1474
Minchan Kimbfd093f2016-07-26 15:23:28 -07001475 obj = location_to_obj(m_page, obj);
1476
Minchan Kimc7806262015-04-15 16:15:26 -07001477 return obj;
1478}
1479
1480
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001481/**
1482 * zs_malloc - Allocate block of given size from pool.
1483 * @pool: pool to allocate from
1484 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001485 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001486 *
1487 * On success, handle to the allocated object is returned,
1488 * otherwise 0.
1489 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1490 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001491unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001492{
Minchan Kim2e40e162015-04-15 16:15:23 -07001493 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001494 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001495 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001496 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001497
Minchan Kim7b60a682015-04-15 16:15:39 -07001498 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001499 return 0;
1500
Minchan Kim37836892016-07-26 15:23:23 -07001501 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001502 if (!handle)
1503 return 0;
1504
1505 /* extra space in chunk to keep the handle */
1506 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001507 class = pool->size_class[get_size_class_index(size)];
1508
1509 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001510 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001511 if (likely(zspage)) {
1512 obj = obj_malloc(class, zspage, handle);
1513 /* Now move the zspage to another fullness group, if required */
1514 fix_fullness_group(class, zspage);
1515 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001516 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001517
Minchan Kim48b48002016-07-26 15:23:31 -07001518 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001519 }
1520
Minchan Kim48b48002016-07-26 15:23:31 -07001521 spin_unlock(&class->lock);
1522
1523 zspage = alloc_zspage(pool, class, gfp);
1524 if (!zspage) {
1525 cache_free_handle(pool, handle);
1526 return 0;
1527 }
1528
1529 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001530 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001531 newfg = get_fullness_group(class, zspage);
1532 insert_zspage(class, zspage, newfg);
1533 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001534 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001535 atomic_long_add(class->pages_per_zspage,
1536 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001537 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001538
1539 /* We completely set up zspage so mark them as movable */
1540 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001541 spin_unlock(&class->lock);
1542
Minchan Kim2e40e162015-04-15 16:15:23 -07001543 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001544}
1545EXPORT_SYMBOL_GPL(zs_malloc);
1546
Minchan Kim1ee47162016-05-20 16:59:45 -07001547static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548{
1549 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001550 struct zspage *zspage;
1551 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001552 unsigned long f_offset;
1553 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001554 void *vaddr;
1555
Minchan Kim312fcae2015-04-15 16:15:30 -07001556 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001557 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001558 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001559 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001560
Minchan Kimc7806262015-04-15 16:15:26 -07001561 vaddr = kmap_atomic(f_page);
1562
1563 /* Insert this object in containing zspage's freelist */
1564 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001565 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001566 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001567 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001568 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001569 zs_stat_dec(class, OBJ_USED, 1);
1570}
1571
1572void zs_free(struct zs_pool *pool, unsigned long handle)
1573{
Minchan Kim37836892016-07-26 15:23:23 -07001574 struct zspage *zspage;
1575 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001576 unsigned long obj;
1577 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001578 int class_idx;
1579 struct size_class *class;
1580 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001581 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001582
Minchan Kim2e40e162015-04-15 16:15:23 -07001583 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001584 return;
1585
Minchan Kim312fcae2015-04-15 16:15:30 -07001586 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001587 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001588 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001589 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001590
Minchan Kim48b48002016-07-26 15:23:31 -07001591 migrate_read_lock(zspage);
1592
Minchan Kim37836892016-07-26 15:23:23 -07001593 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001594 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001595
1596 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001597 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001598 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001599 if (fullness != ZS_EMPTY) {
1600 migrate_read_unlock(zspage);
1601 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001602 }
Minchan Kim48b48002016-07-26 15:23:31 -07001603
1604 isolated = is_zspage_isolated(zspage);
1605 migrate_read_unlock(zspage);
1606 /* If zspage is isolated, zs_page_putback will free the zspage */
1607 if (likely(!isolated))
1608 free_zspage(pool, class, zspage);
1609out:
1610
Minchan Kim312fcae2015-04-15 16:15:30 -07001611 spin_unlock(&class->lock);
1612 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001613 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001614}
1615EXPORT_SYMBOL_GPL(zs_free);
1616
Minchan Kim251cbb92016-05-20 16:59:42 -07001617static void zs_object_copy(struct size_class *class, unsigned long dst,
1618 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001619{
1620 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001621 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001622 unsigned long s_off, d_off;
1623 void *s_addr, *d_addr;
1624 int s_size, d_size, size;
1625 int written = 0;
1626
1627 s_size = d_size = class->size;
1628
1629 obj_to_location(src, &s_page, &s_objidx);
1630 obj_to_location(dst, &d_page, &d_objidx);
1631
Minchan Kimbfd093f2016-07-26 15:23:28 -07001632 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1633 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001634
1635 if (s_off + class->size > PAGE_SIZE)
1636 s_size = PAGE_SIZE - s_off;
1637
1638 if (d_off + class->size > PAGE_SIZE)
1639 d_size = PAGE_SIZE - d_off;
1640
1641 s_addr = kmap_atomic(s_page);
1642 d_addr = kmap_atomic(d_page);
1643
1644 while (1) {
1645 size = min(s_size, d_size);
1646 memcpy(d_addr + d_off, s_addr + s_off, size);
1647 written += size;
1648
1649 if (written == class->size)
1650 break;
1651
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001652 s_off += size;
1653 s_size -= size;
1654 d_off += size;
1655 d_size -= size;
1656
1657 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001658 kunmap_atomic(d_addr);
1659 kunmap_atomic(s_addr);
1660 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001661 s_addr = kmap_atomic(s_page);
1662 d_addr = kmap_atomic(d_page);
1663 s_size = class->size - written;
1664 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001665 }
1666
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001667 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001668 kunmap_atomic(d_addr);
1669 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001670 d_addr = kmap_atomic(d_page);
1671 d_size = class->size - written;
1672 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001673 }
1674 }
1675
1676 kunmap_atomic(d_addr);
1677 kunmap_atomic(s_addr);
1678}
1679
1680/*
1681 * Find alloced object in zspage from index object and
1682 * return handle.
1683 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001684static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001685 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001686{
1687 unsigned long head;
1688 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001689 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001690 unsigned long handle = 0;
1691 void *addr = kmap_atomic(page);
1692
Minchan Kim37836892016-07-26 15:23:23 -07001693 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001694 offset += class->size * index;
1695
1696 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001697 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001698 if (head & OBJ_ALLOCATED_TAG) {
1699 handle = head & ~OBJ_ALLOCATED_TAG;
1700 if (trypin_tag(handle))
1701 break;
1702 handle = 0;
1703 }
1704
1705 offset += class->size;
1706 index++;
1707 }
1708
1709 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001710
1711 *obj_idx = index;
1712
Minchan Kim312fcae2015-04-15 16:15:30 -07001713 return handle;
1714}
1715
1716struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001717 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001718 struct page *s_page;
1719 /* Destination page for migration which should be a first page
1720 * of zspage. */
1721 struct page *d_page;
1722 /* Starting object index within @s_page which used for live object
1723 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001724 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001725};
1726
1727static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1728 struct zs_compact_control *cc)
1729{
1730 unsigned long used_obj, free_obj;
1731 unsigned long handle;
1732 struct page *s_page = cc->s_page;
1733 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001734 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001735 int ret = 0;
1736
1737 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001738 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001739 if (!handle) {
1740 s_page = get_next_page(s_page);
1741 if (!s_page)
1742 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001743 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001744 continue;
1745 }
1746
1747 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001748 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001749 unpin_tag(handle);
1750 ret = -ENOMEM;
1751 break;
1752 }
1753
1754 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001755 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001756 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001757 obj_idx++;
Junil Leec102f07c2016-01-20 14:58:18 -08001758 /*
1759 * record_obj updates handle's value to free_obj and it will
1760 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1761 * breaks synchronization using pin_tag(e,g, zs_free) so
1762 * let's keep the lock bit.
1763 */
1764 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001765 record_obj(handle, free_obj);
1766 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001767 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001768 }
1769
1770 /* Remember last position in this iteration */
1771 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001772 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001773
1774 return ret;
1775}
1776
Minchan Kim37836892016-07-26 15:23:23 -07001777static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001778{
1779 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001780 struct zspage *zspage;
1781 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001782
Minchan Kim37836892016-07-26 15:23:23 -07001783 if (!source) {
1784 fg[0] = ZS_ALMOST_FULL;
1785 fg[1] = ZS_ALMOST_EMPTY;
1786 }
1787
1788 for (i = 0; i < 2; i++) {
1789 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1790 struct zspage, list);
1791 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001792 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001793 remove_zspage(class, zspage, fg[i]);
1794 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001795 }
1796 }
1797
Minchan Kim37836892016-07-26 15:23:23 -07001798 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001799}
1800
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001801/*
Minchan Kim37836892016-07-26 15:23:23 -07001802 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001803 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001804 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001805 *
Minchan Kim37836892016-07-26 15:23:23 -07001806 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001807 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001808static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001809 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001810{
Minchan Kim312fcae2015-04-15 16:15:30 -07001811 enum fullness_group fullness;
1812
Minchan Kim48b48002016-07-26 15:23:31 -07001813 VM_BUG_ON(is_zspage_isolated(zspage));
1814
Minchan Kim37836892016-07-26 15:23:23 -07001815 fullness = get_fullness_group(class, zspage);
1816 insert_zspage(class, zspage, fullness);
1817 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001818
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001819 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001820}
1821
Minchan Kim48b48002016-07-26 15:23:31 -07001822#ifdef CONFIG_COMPACTION
1823static struct dentry *zs_mount(struct file_system_type *fs_type,
1824 int flags, const char *dev_name, void *data)
1825{
1826 static const struct dentry_operations ops = {
1827 .d_dname = simple_dname,
1828 };
1829
1830 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1831}
1832
1833static struct file_system_type zsmalloc_fs = {
1834 .name = "zsmalloc",
1835 .mount = zs_mount,
1836 .kill_sb = kill_anon_super,
1837};
1838
1839static int zsmalloc_mount(void)
1840{
1841 int ret = 0;
1842
1843 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1844 if (IS_ERR(zsmalloc_mnt))
1845 ret = PTR_ERR(zsmalloc_mnt);
1846
1847 return ret;
1848}
1849
1850static void zsmalloc_unmount(void)
1851{
1852 kern_unmount(zsmalloc_mnt);
1853}
1854
1855static void migrate_lock_init(struct zspage *zspage)
1856{
1857 rwlock_init(&zspage->lock);
1858}
1859
1860static void migrate_read_lock(struct zspage *zspage)
1861{
1862 read_lock(&zspage->lock);
1863}
1864
1865static void migrate_read_unlock(struct zspage *zspage)
1866{
1867 read_unlock(&zspage->lock);
1868}
1869
1870static void migrate_write_lock(struct zspage *zspage)
1871{
1872 write_lock(&zspage->lock);
1873}
1874
1875static void migrate_write_unlock(struct zspage *zspage)
1876{
1877 write_unlock(&zspage->lock);
1878}
1879
1880/* Number of isolated subpage for *page migration* in this zspage */
1881static void inc_zspage_isolation(struct zspage *zspage)
1882{
1883 zspage->isolated++;
1884}
1885
1886static void dec_zspage_isolation(struct zspage *zspage)
1887{
1888 zspage->isolated--;
1889}
1890
1891static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1892 struct page *newpage, struct page *oldpage)
1893{
1894 struct page *page;
1895 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1896 int idx = 0;
1897
1898 page = get_first_page(zspage);
1899 do {
1900 if (page == oldpage)
1901 pages[idx] = newpage;
1902 else
1903 pages[idx] = page;
1904 idx++;
1905 } while ((page = get_next_page(page)) != NULL);
1906
1907 create_page_chain(class, zspage, pages);
1908 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1909 if (unlikely(PageHugeObject(oldpage)))
1910 newpage->index = oldpage->index;
1911 __SetPageMovable(newpage, page_mapping(oldpage));
1912}
1913
1914bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1915{
1916 struct zs_pool *pool;
1917 struct size_class *class;
1918 int class_idx;
1919 enum fullness_group fullness;
1920 struct zspage *zspage;
1921 struct address_space *mapping;
1922
1923 /*
1924 * Page is locked so zspage couldn't be destroyed. For detail, look at
1925 * lock_zspage in free_zspage.
1926 */
1927 VM_BUG_ON_PAGE(!PageMovable(page), page);
1928 VM_BUG_ON_PAGE(PageIsolated(page), page);
1929
1930 zspage = get_zspage(page);
1931
1932 /*
1933 * Without class lock, fullness could be stale while class_idx is okay
1934 * because class_idx is constant unless page is freed so we should get
1935 * fullness again under class lock.
1936 */
1937 get_zspage_mapping(zspage, &class_idx, &fullness);
1938 mapping = page_mapping(page);
1939 pool = mapping->private_data;
1940 class = pool->size_class[class_idx];
1941
1942 spin_lock(&class->lock);
1943 if (get_zspage_inuse(zspage) == 0) {
1944 spin_unlock(&class->lock);
1945 return false;
1946 }
1947
1948 /* zspage is isolated for object migration */
1949 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1950 spin_unlock(&class->lock);
1951 return false;
1952 }
1953
1954 /*
1955 * If this is first time isolation for the zspage, isolate zspage from
1956 * size_class to prevent further object allocation from the zspage.
1957 */
1958 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1959 get_zspage_mapping(zspage, &class_idx, &fullness);
1960 remove_zspage(class, zspage, fullness);
1961 }
1962
1963 inc_zspage_isolation(zspage);
1964 spin_unlock(&class->lock);
1965
1966 return true;
1967}
1968
1969int zs_page_migrate(struct address_space *mapping, struct page *newpage,
1970 struct page *page, enum migrate_mode mode)
1971{
1972 struct zs_pool *pool;
1973 struct size_class *class;
1974 int class_idx;
1975 enum fullness_group fullness;
1976 struct zspage *zspage;
1977 struct page *dummy;
1978 void *s_addr, *d_addr, *addr;
1979 int offset, pos;
1980 unsigned long handle, head;
1981 unsigned long old_obj, new_obj;
1982 unsigned int obj_idx;
1983 int ret = -EAGAIN;
1984
1985 VM_BUG_ON_PAGE(!PageMovable(page), page);
1986 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1987
1988 zspage = get_zspage(page);
1989
1990 /* Concurrent compactor cannot migrate any subpage in zspage */
1991 migrate_write_lock(zspage);
1992 get_zspage_mapping(zspage, &class_idx, &fullness);
1993 pool = mapping->private_data;
1994 class = pool->size_class[class_idx];
1995 offset = get_first_obj_offset(page);
1996
1997 spin_lock(&class->lock);
1998 if (!get_zspage_inuse(zspage)) {
1999 ret = -EBUSY;
2000 goto unlock_class;
2001 }
2002
2003 pos = offset;
2004 s_addr = kmap_atomic(page);
2005 while (pos < PAGE_SIZE) {
2006 head = obj_to_head(page, s_addr + pos);
2007 if (head & OBJ_ALLOCATED_TAG) {
2008 handle = head & ~OBJ_ALLOCATED_TAG;
2009 if (!trypin_tag(handle))
2010 goto unpin_objects;
2011 }
2012 pos += class->size;
2013 }
2014
2015 /*
2016 * Here, any user cannot access all objects in the zspage so let's move.
2017 */
2018 d_addr = kmap_atomic(newpage);
2019 memcpy(d_addr, s_addr, PAGE_SIZE);
2020 kunmap_atomic(d_addr);
2021
2022 for (addr = s_addr + offset; addr < s_addr + pos;
2023 addr += class->size) {
2024 head = obj_to_head(page, addr);
2025 if (head & OBJ_ALLOCATED_TAG) {
2026 handle = head & ~OBJ_ALLOCATED_TAG;
2027 if (!testpin_tag(handle))
2028 BUG();
2029
2030 old_obj = handle_to_obj(handle);
2031 obj_to_location(old_obj, &dummy, &obj_idx);
2032 new_obj = (unsigned long)location_to_obj(newpage,
2033 obj_idx);
2034 new_obj |= BIT(HANDLE_PIN_BIT);
2035 record_obj(handle, new_obj);
2036 }
2037 }
2038
2039 replace_sub_page(class, zspage, newpage, page);
2040 get_page(newpage);
2041
2042 dec_zspage_isolation(zspage);
2043
2044 /*
2045 * Page migration is done so let's putback isolated zspage to
2046 * the list if @page is final isolated subpage in the zspage.
2047 */
2048 if (!is_zspage_isolated(zspage))
2049 putback_zspage(class, zspage);
2050
2051 reset_page(page);
2052 put_page(page);
2053 page = newpage;
2054
Minchan Kimdd4123f2016-07-26 15:26:50 -07002055 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002056unpin_objects:
2057 for (addr = s_addr + offset; addr < s_addr + pos;
2058 addr += class->size) {
2059 head = obj_to_head(page, addr);
2060 if (head & OBJ_ALLOCATED_TAG) {
2061 handle = head & ~OBJ_ALLOCATED_TAG;
2062 if (!testpin_tag(handle))
2063 BUG();
2064 unpin_tag(handle);
2065 }
2066 }
2067 kunmap_atomic(s_addr);
2068unlock_class:
2069 spin_unlock(&class->lock);
2070 migrate_write_unlock(zspage);
2071
2072 return ret;
2073}
2074
2075void zs_page_putback(struct page *page)
2076{
2077 struct zs_pool *pool;
2078 struct size_class *class;
2079 int class_idx;
2080 enum fullness_group fg;
2081 struct address_space *mapping;
2082 struct zspage *zspage;
2083
2084 VM_BUG_ON_PAGE(!PageMovable(page), page);
2085 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2086
2087 zspage = get_zspage(page);
2088 get_zspage_mapping(zspage, &class_idx, &fg);
2089 mapping = page_mapping(page);
2090 pool = mapping->private_data;
2091 class = pool->size_class[class_idx];
2092
2093 spin_lock(&class->lock);
2094 dec_zspage_isolation(zspage);
2095 if (!is_zspage_isolated(zspage)) {
2096 fg = putback_zspage(class, zspage);
2097 /*
2098 * Due to page_lock, we cannot free zspage immediately
2099 * so let's defer.
2100 */
2101 if (fg == ZS_EMPTY)
2102 schedule_work(&pool->free_work);
2103 }
2104 spin_unlock(&class->lock);
2105}
2106
2107const struct address_space_operations zsmalloc_aops = {
2108 .isolate_page = zs_page_isolate,
2109 .migratepage = zs_page_migrate,
2110 .putback_page = zs_page_putback,
2111};
2112
2113static int zs_register_migration(struct zs_pool *pool)
2114{
2115 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2116 if (IS_ERR(pool->inode)) {
2117 pool->inode = NULL;
2118 return 1;
2119 }
2120
2121 pool->inode->i_mapping->private_data = pool;
2122 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2123 return 0;
2124}
2125
2126static void zs_unregister_migration(struct zs_pool *pool)
2127{
2128 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002129 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002130}
2131
2132/*
2133 * Caller should hold page_lock of all pages in the zspage
2134 * In here, we cannot use zspage meta data.
2135 */
2136static void async_free_zspage(struct work_struct *work)
2137{
2138 int i;
2139 struct size_class *class;
2140 unsigned int class_idx;
2141 enum fullness_group fullness;
2142 struct zspage *zspage, *tmp;
2143 LIST_HEAD(free_pages);
2144 struct zs_pool *pool = container_of(work, struct zs_pool,
2145 free_work);
2146
2147 for (i = 0; i < zs_size_classes; i++) {
2148 class = pool->size_class[i];
2149 if (class->index != i)
2150 continue;
2151
2152 spin_lock(&class->lock);
2153 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2154 spin_unlock(&class->lock);
2155 }
2156
2157
2158 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2159 list_del(&zspage->list);
2160 lock_zspage(zspage);
2161
2162 get_zspage_mapping(zspage, &class_idx, &fullness);
2163 VM_BUG_ON(fullness != ZS_EMPTY);
2164 class = pool->size_class[class_idx];
2165 spin_lock(&class->lock);
2166 __free_zspage(pool, pool->size_class[class_idx], zspage);
2167 spin_unlock(&class->lock);
2168 }
2169};
2170
2171static void kick_deferred_free(struct zs_pool *pool)
2172{
2173 schedule_work(&pool->free_work);
2174}
2175
2176static void init_deferred_free(struct zs_pool *pool)
2177{
2178 INIT_WORK(&pool->free_work, async_free_zspage);
2179}
2180
2181static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2182{
2183 struct page *page = get_first_page(zspage);
2184
2185 do {
2186 WARN_ON(!trylock_page(page));
2187 __SetPageMovable(page, pool->inode->i_mapping);
2188 unlock_page(page);
2189 } while ((page = get_next_page(page)) != NULL);
2190}
2191#endif
2192
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002193/*
2194 *
2195 * Based on the number of unused allocated objects calculate
2196 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002197 */
2198static unsigned long zs_can_compact(struct size_class *class)
2199{
2200 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002201 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2202 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002203
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002204 if (obj_allocated <= obj_used)
2205 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002206
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002207 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002208 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002209
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002210 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002211}
2212
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002213static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002214{
Minchan Kim312fcae2015-04-15 16:15:30 -07002215 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002216 struct zspage *src_zspage;
2217 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002218
Minchan Kim312fcae2015-04-15 16:15:30 -07002219 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002220 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002221
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002222 if (!zs_can_compact(class))
2223 break;
2224
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002225 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002226 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002227
Minchan Kim37836892016-07-26 15:23:23 -07002228 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002229 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002230 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002231 * If there is no more space in dst_page, resched
2232 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002233 */
2234 if (!migrate_zspage(pool, class, &cc))
2235 break;
2236
Minchan Kim4aa409c2016-07-26 15:23:26 -07002237 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002238 }
2239
2240 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002241 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002242 break;
2243
Minchan Kim4aa409c2016-07-26 15:23:26 -07002244 putback_zspage(class, dst_zspage);
2245 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002246 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002247 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002248 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002249 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002250 cond_resched();
2251 spin_lock(&class->lock);
2252 }
2253
Minchan Kim37836892016-07-26 15:23:23 -07002254 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002255 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002256
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002257 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002258}
2259
2260unsigned long zs_compact(struct zs_pool *pool)
2261{
2262 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002263 struct size_class *class;
2264
2265 for (i = zs_size_classes - 1; i >= 0; i--) {
2266 class = pool->size_class[i];
2267 if (!class)
2268 continue;
2269 if (class->index != i)
2270 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002271 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002272 }
2273
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002274 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002275}
2276EXPORT_SYMBOL_GPL(zs_compact);
2277
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002278void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2279{
2280 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2281}
2282EXPORT_SYMBOL_GPL(zs_pool_stats);
2283
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002284static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2285 struct shrink_control *sc)
2286{
2287 unsigned long pages_freed;
2288 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2289 shrinker);
2290
2291 pages_freed = pool->stats.pages_compacted;
2292 /*
2293 * Compact classes and calculate compaction delta.
2294 * Can run concurrently with a manually triggered
2295 * (by user) compaction.
2296 */
2297 pages_freed = zs_compact(pool) - pages_freed;
2298
2299 return pages_freed ? pages_freed : SHRINK_STOP;
2300}
2301
2302static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2303 struct shrink_control *sc)
2304{
2305 int i;
2306 struct size_class *class;
2307 unsigned long pages_to_free = 0;
2308 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2309 shrinker);
2310
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002311 for (i = zs_size_classes - 1; i >= 0; i--) {
2312 class = pool->size_class[i];
2313 if (!class)
2314 continue;
2315 if (class->index != i)
2316 continue;
2317
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002318 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002319 }
2320
2321 return pages_to_free;
2322}
2323
2324static void zs_unregister_shrinker(struct zs_pool *pool)
2325{
2326 if (pool->shrinker_enabled) {
2327 unregister_shrinker(&pool->shrinker);
2328 pool->shrinker_enabled = false;
2329 }
2330}
2331
2332static int zs_register_shrinker(struct zs_pool *pool)
2333{
2334 pool->shrinker.scan_objects = zs_shrinker_scan;
2335 pool->shrinker.count_objects = zs_shrinker_count;
2336 pool->shrinker.batch = 0;
2337 pool->shrinker.seeks = DEFAULT_SEEKS;
2338
2339 return register_shrinker(&pool->shrinker);
2340}
2341
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002342/**
2343 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002344 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002345 *
2346 * This function must be called before anything when using
2347 * the zsmalloc allocator.
2348 *
2349 * On success, a pointer to the newly created pool is returned,
2350 * otherwise NULL.
2351 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002352struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002353{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002354 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002355 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002356 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002357
Ganesh Mahendran18136652014-12-12 16:57:10 -08002358 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002359 if (!pool)
2360 return NULL;
2361
Minchan Kim48b48002016-07-26 15:23:31 -07002362 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002363 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2364 GFP_KERNEL);
2365 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002366 kfree(pool);
2367 return NULL;
2368 }
2369
Minchan Kim2e40e162015-04-15 16:15:23 -07002370 pool->name = kstrdup(name, GFP_KERNEL);
2371 if (!pool->name)
2372 goto err;
2373
Minchan Kim37836892016-07-26 15:23:23 -07002374 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002375 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002376
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002377 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002378 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002379 * for merging should be larger or equal to current size.
2380 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002381 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002382 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002383 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002384 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002385 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002386 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002387
2388 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2389 if (size > ZS_MAX_ALLOC_SIZE)
2390 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002391 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002392 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002393
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002394 /*
2395 * size_class is used for normal zsmalloc operation such
2396 * as alloc/free for that size. Although it is natural that we
2397 * have one size_class for each size, there is a chance that we
2398 * can get more memory utilization if we use one size_class for
2399 * many different sizes whose size_class have same
2400 * characteristics. So, we makes size_class point to
2401 * previous size_class if possible.
2402 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002403 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002404 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002405 pool->size_class[i] = prev_class;
2406 continue;
2407 }
2408 }
2409
2410 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2411 if (!class)
2412 goto err;
2413
Nitin Gupta61989a82012-01-09 16:51:56 -06002414 class->size = size;
2415 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002416 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002417 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002418 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002419 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002420 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2421 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002422 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002423
2424 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002425 }
2426
Dan Streetmand34f6152016-05-20 16:59:56 -07002427 /* debug only, don't abort if it fails */
2428 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002429
Minchan Kim48b48002016-07-26 15:23:31 -07002430 if (zs_register_migration(pool))
2431 goto err;
2432
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002433 /*
2434 * Not critical, we still can use the pool
2435 * and user can trigger compaction manually.
2436 */
2437 if (zs_register_shrinker(pool) == 0)
2438 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002439 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002440
2441err:
2442 zs_destroy_pool(pool);
2443 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002444}
2445EXPORT_SYMBOL_GPL(zs_create_pool);
2446
2447void zs_destroy_pool(struct zs_pool *pool)
2448{
2449 int i;
2450
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002451 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002452 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002453 zs_pool_stat_destroy(pool);
2454
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002455 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002456 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002457 struct size_class *class = pool->size_class[i];
2458
2459 if (!class)
2460 continue;
2461
2462 if (class->index != i)
2463 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002464
Minchan Kim48b48002016-07-26 15:23:31 -07002465 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002466 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002467 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002468 class->size, fg);
2469 }
2470 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002471 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002472 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002473
Minchan Kim37836892016-07-26 15:23:23 -07002474 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002475 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002476 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002477 kfree(pool);
2478}
2479EXPORT_SYMBOL_GPL(zs_destroy_pool);
2480
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002481static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002482{
Minchan Kim48b48002016-07-26 15:23:31 -07002483 int ret;
2484
2485 ret = zsmalloc_mount();
2486 if (ret)
2487 goto out;
2488
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002489 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2490 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002491 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002492 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002493
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002494 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002495
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002496#ifdef CONFIG_ZPOOL
2497 zpool_register_driver(&zs_zpool_driver);
2498#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002499
Dan Streetman4abaac92016-05-26 15:16:27 -07002500 zs_stat_init();
2501
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002502 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002503
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002504hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002505 zsmalloc_unmount();
2506out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002507 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002508}
Nitin Gupta61989a82012-01-09 16:51:56 -06002509
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002510static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002511{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002512#ifdef CONFIG_ZPOOL
2513 zpool_unregister_driver(&zs_zpool_driver);
2514#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002515 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002516 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002517
2518 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002519}
Ben Hutchings069f1012012-06-20 02:31:11 +01002520
2521module_init(zs_init);
2522module_exit(zs_exit);
2523
2524MODULE_LICENSE("Dual BSD/GPL");
2525MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");