blob: ad8a34bd15ca739a05caafb4cf6903689bde6e9e [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
Minchan Kim48b48002016-07-26 15:23:31 -070028 * PG_owner_priv_1: indentifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070029 *
30 */
31
Dan Streetman4abaac92016-05-26 15:16:27 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Nitin Gupta61989a82012-01-09 16:51:56 -060034#include <linux/module.h>
35#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070036#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/string.h>
41#include <linux/slab.h>
42#include <asm/tlbflush.h>
43#include <asm/pgtable.h>
44#include <linux/cpumask.h>
45#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060046#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080047#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090048#include <linux/spinlock.h>
49#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080050#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080051#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070052#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070053#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070054#include <linux/migrate.h>
Henry Burns0e7e2732019-08-24 17:55:06 -070055#include <linux/wait.h>
Minchan Kim48b48002016-07-26 15:23:31 -070056#include <linux/pagemap.h>
57
58#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090059
60/*
61 * This must be power of 2 and greater than of equal to sizeof(link_free).
62 * These two conditions ensure that any 'struct link_free' itself doesn't
63 * span more than 1 page which avoids complex case of mapping 2 pages simply
64 * to restore link_free pointer values.
65 */
66#define ZS_ALIGN 8
67
68/*
69 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
70 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
71 */
72#define ZS_MAX_ZSPAGE_ORDER 2
73#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
74
Minchan Kim2e40e162015-04-15 16:15:23 -070075#define ZS_HANDLE_SIZE (sizeof(unsigned long))
76
Seth Jennings0959c632012-08-08 15:12:17 +090077/*
78 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090079 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090080 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070081 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090082 *
83 * This is made more complicated by various memory models and PAE.
84 */
85
86#ifndef MAX_PHYSMEM_BITS
87#ifdef CONFIG_HIGHMEM64G
88#define MAX_PHYSMEM_BITS 36
89#else /* !CONFIG_HIGHMEM64G */
90/*
91 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
92 * be PAGE_SHIFT
93 */
94#define MAX_PHYSMEM_BITS BITS_PER_LONG
95#endif
96#endif
97#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070098
99/*
100 * Memory for allocating for handle keeps object position by
101 * encoding <page, obj_idx> and the encoded value has a room
102 * in least bit(ie, look at obj_to_location).
103 * We use the bit to synchronize between object access by
104 * user and migration.
105 */
106#define HANDLE_PIN_BIT 0
107
108/*
109 * Head in allocated object should have OBJ_ALLOCATED_TAG
110 * to identify the object was allocated or not.
111 * It's okay to add the status bit in the least bit because
112 * header keeps handle which is 4byte-aligned address so we
113 * have room for two bit at least.
114 */
115#define OBJ_ALLOCATED_TAG 1
116#define OBJ_TAG_BITS 1
117#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900118#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
119
120#define MAX(a, b) ((a) >= (b) ? (a) : (b))
121/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
122#define ZS_MIN_ALLOC_SIZE \
123 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700124/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700125#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900126
127/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700128 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900129 * trader-off here:
130 * - Large number of size classes is potentially wasteful as free page are
131 * spread across these classes
132 * - Small number of size classes causes large internal fragmentation
133 * - Probably its better to use specific size classes (empirically
134 * determined). NOTE: all those class sizes must be set as multiple of
135 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
136 *
137 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
138 * (reason above)
139 */
Minchan Kim37836892016-07-26 15:23:23 -0700140#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900141
Seth Jennings0959c632012-08-08 15:12:17 +0900142enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900143 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700144 ZS_ALMOST_EMPTY,
145 ZS_ALMOST_FULL,
146 ZS_FULL,
147 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900148};
149
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800150enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700151 CLASS_EMPTY,
152 CLASS_ALMOST_EMPTY,
153 CLASS_ALMOST_FULL,
154 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800155 OBJ_ALLOCATED,
156 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700157 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800158};
159
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800160struct zs_size_stat {
161 unsigned long objs[NR_ZS_STAT_TYPE];
162};
163
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700164#ifdef CONFIG_ZSMALLOC_STAT
165static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800166#endif
167
Minchan Kim48b48002016-07-26 15:23:31 -0700168#ifdef CONFIG_COMPACTION
169static struct vfsmount *zsmalloc_mnt;
170#endif
171
Seth Jennings0959c632012-08-08 15:12:17 +0900172/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800173 * number of size_classes
174 */
175static int zs_size_classes;
176
177/*
Seth Jennings0959c632012-08-08 15:12:17 +0900178 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
179 * n <= N / f, where
180 * n = number of allocated objects
181 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700182 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900183 *
184 * Similarly, we assign zspage to:
185 * ZS_ALMOST_FULL when n > N / f
186 * ZS_EMPTY when n == 0
187 * ZS_FULL when n == N
188 *
189 * (see: fix_fullness_group())
190 */
191static const int fullness_threshold_frac = 4;
192
193struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700194 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700195 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900196 /*
197 * Size of objects stored in this class. Must be multiple
198 * of ZS_ALIGN.
199 */
200 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700201 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800202 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
203 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700204
205 unsigned int index;
206 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900207};
208
Minchan Kim48b48002016-07-26 15:23:31 -0700209/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
210static void SetPageHugeObject(struct page *page)
211{
212 SetPageOwnerPriv1(page);
213}
214
215static void ClearPageHugeObject(struct page *page)
216{
217 ClearPageOwnerPriv1(page);
218}
219
220static int PageHugeObject(struct page *page)
221{
222 return PageOwnerPriv1(page);
223}
224
Seth Jennings0959c632012-08-08 15:12:17 +0900225/*
226 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700227 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900228 *
229 * This must be power of 2 and less than or equal to ZS_ALIGN
230 */
231struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700232 union {
233 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700234 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700235 * It's valid for non-allocated object
236 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700237 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700238 /*
239 * Handle of allocated object.
240 */
241 unsigned long handle;
242 };
Seth Jennings0959c632012-08-08 15:12:17 +0900243};
244
245struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800246 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800247
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800248 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700249 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700250 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900251
Minchan Kim13de8932014-10-09 15:29:48 -0700252 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800253
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700254 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700255
256 /* Compact classes */
257 struct shrinker shrinker;
258 /*
259 * To signify that register_shrinker() was successful
260 * and unregister_shrinker() will not Oops.
261 */
262 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800263#ifdef CONFIG_ZSMALLOC_STAT
264 struct dentry *stat_dentry;
265#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700266#ifdef CONFIG_COMPACTION
267 struct inode *inode;
268 struct work_struct free_work;
Henry Burns0e7e2732019-08-24 17:55:06 -0700269 /* A wait queue for when migration races with async_free_zspage() */
270 wait_queue_head_t migration_wait;
271 atomic_long_t isolated_pages;
272 bool destroying;
Minchan Kim48b48002016-07-26 15:23:31 -0700273#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900274};
Nitin Gupta61989a82012-01-09 16:51:56 -0600275
276/*
277 * A zspage's class index and fullness group
278 * are encoded in its (first)page->mapping
279 */
Minchan Kim37836892016-07-26 15:23:23 -0700280#define FULLNESS_BITS 2
281#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700282#define ISOLATED_BITS 3
283#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700284
Minchan Kim37836892016-07-26 15:23:23 -0700285struct zspage {
286 struct {
287 unsigned int fullness:FULLNESS_BITS;
Minchan Kimd19f7452017-04-13 14:56:40 -0700288 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700289 unsigned int isolated:ISOLATED_BITS;
290 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700291 };
292 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700293 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700294 struct page *first_page;
295 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700296#ifdef CONFIG_COMPACTION
297 rwlock_t lock;
298#endif
Minchan Kim37836892016-07-26 15:23:23 -0700299};
Nitin Gupta61989a82012-01-09 16:51:56 -0600300
Seth Jenningsf5536462012-07-18 11:55:56 -0500301struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900302#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500303 struct vm_struct *vm; /* vm area for mapping object that span pages */
304#else
305 char *vm_buf; /* copy buffer for objects that span pages */
306#endif
307 char *vm_addr; /* address of kmap_atomic()'ed pages */
308 enum zs_mapmode vm_mm; /* mapping mode */
309};
310
Minchan Kim48b48002016-07-26 15:23:31 -0700311#ifdef CONFIG_COMPACTION
312static int zs_register_migration(struct zs_pool *pool);
313static void zs_unregister_migration(struct zs_pool *pool);
314static void migrate_lock_init(struct zspage *zspage);
315static void migrate_read_lock(struct zspage *zspage);
316static void migrate_read_unlock(struct zspage *zspage);
317static void kick_deferred_free(struct zs_pool *pool);
318static void init_deferred_free(struct zs_pool *pool);
319static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
320#else
321static int zsmalloc_mount(void) { return 0; }
322static void zsmalloc_unmount(void) {}
323static int zs_register_migration(struct zs_pool *pool) { return 0; }
324static void zs_unregister_migration(struct zs_pool *pool) {}
325static void migrate_lock_init(struct zspage *zspage) {}
326static void migrate_read_lock(struct zspage *zspage) {}
327static void migrate_read_unlock(struct zspage *zspage) {}
328static void kick_deferred_free(struct zs_pool *pool) {}
329static void init_deferred_free(struct zs_pool *pool) {}
330static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
331#endif
332
Minchan Kim37836892016-07-26 15:23:23 -0700333static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700334{
335 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
336 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700337 if (!pool->handle_cachep)
338 return 1;
339
340 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
341 0, 0, NULL);
342 if (!pool->zspage_cachep) {
343 kmem_cache_destroy(pool->handle_cachep);
344 pool->handle_cachep = NULL;
345 return 1;
346 }
347
348 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700349}
350
Minchan Kim37836892016-07-26 15:23:23 -0700351static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700352{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700353 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700354 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700355}
356
Minchan Kim37836892016-07-26 15:23:23 -0700357static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700358{
359 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700360 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700361}
362
Minchan Kim37836892016-07-26 15:23:23 -0700363static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700364{
365 kmem_cache_free(pool->handle_cachep, (void *)handle);
366}
367
Minchan Kim37836892016-07-26 15:23:23 -0700368static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
369{
Minchan Kim48b48002016-07-26 15:23:31 -0700370 return kmem_cache_alloc(pool->zspage_cachep,
371 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim37836892016-07-26 15:23:23 -0700372};
373
374static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
375{
376 kmem_cache_free(pool->zspage_cachep, zspage);
377}
378
Minchan Kim2e40e162015-04-15 16:15:23 -0700379static void record_obj(unsigned long handle, unsigned long obj)
380{
Junil Leec102f072016-01-20 14:58:18 -0800381 /*
382 * lsb of @obj represents handle lock while other bits
383 * represent object value the handle is pointing so
384 * updating shouldn't do store tearing.
385 */
386 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700387}
388
Dan Streetmanc7957792014-08-06 16:08:38 -0700389/* zpool driver */
390
391#ifdef CONFIG_ZPOOL
392
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800393static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700394 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700395 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700396{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700397 /*
398 * Ignore global gfp flags: zs_malloc() may be invoked from
399 * different contexts and its caller must provide a valid
400 * gfp mask.
401 */
402 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700403}
404
405static void zs_zpool_destroy(void *pool)
406{
407 zs_destroy_pool(pool);
408}
409
410static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
411 unsigned long *handle)
412{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700413 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700414 return *handle ? 0 : -1;
415}
416static void zs_zpool_free(void *pool, unsigned long handle)
417{
418 zs_free(pool, handle);
419}
420
421static int zs_zpool_shrink(void *pool, unsigned int pages,
422 unsigned int *reclaimed)
423{
424 return -EINVAL;
425}
426
427static void *zs_zpool_map(void *pool, unsigned long handle,
428 enum zpool_mapmode mm)
429{
430 enum zs_mapmode zs_mm;
431
432 switch (mm) {
433 case ZPOOL_MM_RO:
434 zs_mm = ZS_MM_RO;
435 break;
436 case ZPOOL_MM_WO:
437 zs_mm = ZS_MM_WO;
438 break;
439 case ZPOOL_MM_RW: /* fallthru */
440 default:
441 zs_mm = ZS_MM_RW;
442 break;
443 }
444
445 return zs_map_object(pool, handle, zs_mm);
446}
447static void zs_zpool_unmap(void *pool, unsigned long handle)
448{
449 zs_unmap_object(pool, handle);
450}
451
452static u64 zs_zpool_total_size(void *pool)
453{
Minchan Kim722cdc12014-10-09 15:29:50 -0700454 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700455}
456
457static struct zpool_driver zs_zpool_driver = {
458 .type = "zsmalloc",
459 .owner = THIS_MODULE,
460 .create = zs_zpool_create,
461 .destroy = zs_zpool_destroy,
462 .malloc = zs_zpool_malloc,
463 .free = zs_zpool_free,
464 .shrink = zs_zpool_shrink,
465 .map = zs_zpool_map,
466 .unmap = zs_zpool_unmap,
467 .total_size = zs_zpool_total_size,
468};
469
Kees Cook137f8cf2014-08-29 15:18:40 -0700470MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700471#endif /* CONFIG_ZPOOL */
472
Nitin Gupta61989a82012-01-09 16:51:56 -0600473/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
474static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
475
Minchan Kim48b48002016-07-26 15:23:31 -0700476static bool is_zspage_isolated(struct zspage *zspage)
477{
478 return zspage->isolated;
479}
480
Nick Desaulniers5ac69182017-07-10 15:47:26 -0700481static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600482{
Minchan Kima27545bf2012-04-25 15:23:09 +0900483 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600484}
485
Minchan Kim48b48002016-07-26 15:23:31 -0700486/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700487static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600488{
Minchan Kim37836892016-07-26 15:23:23 -0700489 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600490}
491
Minchan Kim37836892016-07-26 15:23:23 -0700492static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700493{
Minchan Kim37836892016-07-26 15:23:23 -0700494 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700495}
496
Minchan Kim37836892016-07-26 15:23:23 -0700497static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700498{
Minchan Kim37836892016-07-26 15:23:23 -0700499 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700500}
501
Minchan Kim48b48002016-07-26 15:23:31 -0700502static inline struct page *get_first_page(struct zspage *zspage)
503{
504 struct page *first_page = zspage->first_page;
505
506 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
507 return first_page;
508}
509
Minchan Kim4f420472016-07-26 15:23:17 -0700510static inline int get_first_obj_offset(struct page *page)
511{
Minchan Kim48b48002016-07-26 15:23:31 -0700512 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700513}
514
515static inline void set_first_obj_offset(struct page *page, int offset)
516{
Minchan Kim48b48002016-07-26 15:23:31 -0700517 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700518}
519
Minchan Kimbfd093f2016-07-26 15:23:28 -0700520static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700521{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700522 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700523}
524
Minchan Kimbfd093f2016-07-26 15:23:28 -0700525static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700526{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700527 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700528}
529
Minchan Kim37836892016-07-26 15:23:23 -0700530static void get_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 Kim48b48002016-07-26 15:23:31 -0700534 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
535
Minchan Kim37836892016-07-26 15:23:23 -0700536 *fullness = zspage->fullness;
537 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600538}
539
Minchan Kim37836892016-07-26 15:23:23 -0700540static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700541 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600542 enum fullness_group fullness)
543{
Minchan Kim37836892016-07-26 15:23:23 -0700544 zspage->class = class_idx;
545 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600546}
547
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900548/*
549 * zsmalloc divides the pool into various size classes where each
550 * class maintains a list of zspages where each zspage is divided
551 * into equal sized chunks. Each allocation falls into one of these
552 * classes depending on its size. This function returns index of the
553 * size class which has chunk size big enough to hold the give size.
554 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600555static int get_size_class_index(int size)
556{
557 int idx = 0;
558
559 if (likely(size > ZS_MIN_ALLOC_SIZE))
560 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
561 ZS_SIZE_CLASS_DELTA);
562
Minchan Kim7b60a682015-04-15 16:15:39 -0700563 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600564}
565
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700566/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700567static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700568 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700569{
Minchan Kim48b48002016-07-26 15:23:31 -0700570 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700571}
572
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700573/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700574static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700575 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700576{
Minchan Kim48b48002016-07-26 15:23:31 -0700577 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700578}
579
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700580/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700581static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcked069e8c2017-09-08 16:13:02 -0700582 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700583{
Minchan Kim48b48002016-07-26 15:23:31 -0700584 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700585}
586
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700587#ifdef CONFIG_ZSMALLOC_STAT
588
Dan Streetman4abaac92016-05-26 15:16:27 -0700589static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700590{
Dan Streetman4abaac92016-05-26 15:16:27 -0700591 if (!debugfs_initialized()) {
592 pr_warn("debugfs not available, stat dir not created\n");
593 return;
594 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700595
596 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
597 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700598 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700599}
600
601static void __exit zs_stat_exit(void)
602{
603 debugfs_remove_recursive(zs_stat_root);
604}
605
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700606static unsigned long zs_can_compact(struct size_class *class);
607
Minchan Kim248ca1b2015-04-15 16:15:42 -0700608static int zs_stats_size_show(struct seq_file *s, void *v)
609{
610 int i;
611 struct zs_pool *pool = s->private;
612 struct size_class *class;
613 int objs_per_zspage;
614 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700615 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700616 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
617 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700618 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700620 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700621 "class", "size", "almost_full", "almost_empty",
622 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700623 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700624
625 for (i = 0; i < zs_size_classes; i++) {
626 class = pool->size_class[i];
627
628 if (class->index != i)
629 continue;
630
631 spin_lock(&class->lock);
632 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
633 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
634 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
635 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700636 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700637 spin_unlock(&class->lock);
638
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700639 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700640 pages_used = obj_allocated / objs_per_zspage *
641 class->pages_per_zspage;
642
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700643 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
644 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700645 i, class->size, class_almost_full, class_almost_empty,
646 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700647 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648
649 total_class_almost_full += class_almost_full;
650 total_class_almost_empty += class_almost_empty;
651 total_objs += obj_allocated;
652 total_used_objs += obj_used;
653 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700654 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700655 }
656
657 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700658 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700659 "Total", "", total_class_almost_full,
660 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700661 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700662
663 return 0;
664}
665
666static int zs_stats_size_open(struct inode *inode, struct file *file)
667{
668 return single_open(file, zs_stats_size_show, inode->i_private);
669}
670
671static const struct file_operations zs_stat_size_ops = {
672 .open = zs_stats_size_open,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .release = single_release,
676};
677
Dan Streetmand34f6152016-05-20 16:59:56 -0700678static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700679{
680 struct dentry *entry;
681
Dan Streetman4abaac92016-05-26 15:16:27 -0700682 if (!zs_stat_root) {
683 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700684 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700685 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700686
687 entry = debugfs_create_dir(name, zs_stat_root);
688 if (!entry) {
689 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700690 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700691 }
692 pool->stat_dentry = entry;
693
694 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
695 pool->stat_dentry, pool, &zs_stat_size_ops);
696 if (!entry) {
697 pr_warn("%s: debugfs file entry <%s> creation failed\n",
698 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700699 debugfs_remove_recursive(pool->stat_dentry);
700 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700701 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700702}
703
704static void zs_pool_stat_destroy(struct zs_pool *pool)
705{
706 debugfs_remove_recursive(pool->stat_dentry);
707}
708
709#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700710static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700711{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700712}
713
714static void __exit zs_stat_exit(void)
715{
716}
717
Dan Streetmand34f6152016-05-20 16:59:56 -0700718static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700719{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700720}
721
722static inline void zs_pool_stat_destroy(struct zs_pool *pool)
723{
724}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700725#endif
726
Minchan Kim48b48002016-07-26 15:23:31 -0700727
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900728/*
729 * For each size class, zspages are divided into different groups
730 * depending on how "full" they are. This was done so that we could
731 * easily find empty or nearly empty zspages when we try to shrink
732 * the pool (not yet implemented). This function returns fullness
733 * status of the given page.
734 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700735static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700736 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600737{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700738 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600739 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700740
Minchan Kim37836892016-07-26 15:23:23 -0700741 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700742 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600743
744 if (inuse == 0)
745 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700746 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600747 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700748 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600749 fg = ZS_ALMOST_EMPTY;
750 else
751 fg = ZS_ALMOST_FULL;
752
753 return fg;
754}
755
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900756/*
757 * Each size class maintains various freelists and zspages are assigned
758 * to one of these freelists based on the number of live objects they
759 * have. This functions inserts the given zspage into the freelist
760 * identified by <class, fullness_group>.
761 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700762static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700763 struct zspage *zspage,
764 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600765{
Minchan Kim37836892016-07-26 15:23:23 -0700766 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600767
Minchan Kim48b48002016-07-26 15:23:31 -0700768 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700769 head = list_first_entry_or_null(&class->fullness_list[fullness],
770 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700771 /*
Minchan Kim37836892016-07-26 15:23:23 -0700772 * We want to see more ZS_FULL pages and less almost empty/full.
773 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700774 */
Minchan Kim37836892016-07-26 15:23:23 -0700775 if (head) {
776 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
777 list_add(&zspage->list, &head->list);
778 return;
779 }
780 }
781 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600782}
783
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900784/*
785 * This function removes the given zspage from the freelist identified
786 * by <class, fullness_group>.
787 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700788static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700789 struct zspage *zspage,
790 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600791{
Minchan Kim37836892016-07-26 15:23:23 -0700792 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700793 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600794
Minchan Kim37836892016-07-26 15:23:23 -0700795 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700796 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600797}
798
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900799/*
800 * Each size class maintains zspages in different fullness groups depending
801 * on the number of live objects they contain. When allocating or freeing
802 * objects, the fullness status of the page can change, say, from ALMOST_FULL
803 * to ALMOST_EMPTY when freeing an object. This function checks if such
804 * a status change has occurred for the given page and accordingly moves the
805 * page from the freelist of the old fullness group to that of the new
806 * fullness group.
807 */
Minchan Kimc7806262015-04-15 16:15:26 -0700808static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700809 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600810{
811 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600812 enum fullness_group currfg, newfg;
813
Minchan Kim37836892016-07-26 15:23:23 -0700814 get_zspage_mapping(zspage, &class_idx, &currfg);
815 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600816 if (newfg == currfg)
817 goto out;
818
Minchan Kim48b48002016-07-26 15:23:31 -0700819 if (!is_zspage_isolated(zspage)) {
820 remove_zspage(class, zspage, currfg);
821 insert_zspage(class, zspage, newfg);
822 }
823
Minchan Kim37836892016-07-26 15:23:23 -0700824 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600825
826out:
827 return newfg;
828}
829
830/*
831 * We have to decide on how many pages to link together
832 * to form a zspage for each size class. This is important
833 * to reduce wastage due to unusable space left at end of
834 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700835 * wastage = Zp % class_size
836 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600837 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
838 *
839 * For example, for size class of 3/8 * PAGE_SIZE, we should
840 * link together 3 PAGE_SIZE sized pages to form a zspage
841 * since then we can perfectly fit in 8 such objects.
842 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900843static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600844{
845 int i, max_usedpc = 0;
846 /* zspage order which gives maximum used size per KB */
847 int max_usedpc_order = 1;
848
Seth Jennings84d4faa2012-03-05 11:33:21 -0600849 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600850 int zspage_size;
851 int waste, usedpc;
852
853 zspage_size = i * PAGE_SIZE;
854 waste = zspage_size % class_size;
855 usedpc = (zspage_size - waste) * 100 / zspage_size;
856
857 if (usedpc > max_usedpc) {
858 max_usedpc = usedpc;
859 max_usedpc_order = i;
860 }
861 }
862
863 return max_usedpc_order;
864}
865
Minchan Kim37836892016-07-26 15:23:23 -0700866static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600867{
Minchan Kim48b48002016-07-26 15:23:31 -0700868 struct zspage *zspage = (struct zspage *)page->private;
869
870 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
871 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600872}
873
874static struct page *get_next_page(struct page *page)
875{
Minchan Kim48b48002016-07-26 15:23:31 -0700876 if (unlikely(PageHugeObject(page)))
877 return NULL;
878
879 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600880}
881
Minchan Kimbfd093f2016-07-26 15:23:28 -0700882/**
883 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
884 * @page: page object resides in zspage
885 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800886 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700887static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700888 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600889{
Minchan Kim312fcae2015-04-15 16:15:30 -0700890 obj >>= OBJ_TAG_BITS;
891 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
892 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600893}
894
Minchan Kimbfd093f2016-07-26 15:23:28 -0700895/**
896 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
897 * @page: page object resides in zspage
898 * @obj_idx: object index
899 */
900static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
901{
902 unsigned long obj;
903
904 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
905 obj |= obj_idx & OBJ_INDEX_MASK;
906 obj <<= OBJ_TAG_BITS;
907
908 return obj;
909}
910
Minchan Kim2e40e162015-04-15 16:15:23 -0700911static unsigned long handle_to_obj(unsigned long handle)
912{
913 return *(unsigned long *)handle;
914}
915
Minchan Kim48b48002016-07-26 15:23:31 -0700916static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700917{
Minchan Kim48b48002016-07-26 15:23:31 -0700918 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700919 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700920 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700921 } else
922 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700923}
924
Minchan Kim48b48002016-07-26 15:23:31 -0700925static inline int testpin_tag(unsigned long handle)
926{
927 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
928}
929
Minchan Kim312fcae2015-04-15 16:15:30 -0700930static inline int trypin_tag(unsigned long handle)
931{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700932 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700933}
934
935static void pin_tag(unsigned long handle)
936{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700937 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700938}
939
940static void unpin_tag(unsigned long handle)
941{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700942 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700943}
944
Nitin Guptaf4477e92012-04-02 09:13:56 -0500945static void reset_page(struct page *page)
946{
Minchan Kim48b48002016-07-26 15:23:31 -0700947 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700948 ClearPagePrivate(page);
949 ClearPagePrivate2(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500950 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700951 page_mapcount_reset(page);
952 ClearPageHugeObject(page);
953 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500954}
955
Minchan Kim48b48002016-07-26 15:23:31 -0700956/*
957 * To prevent zspage destroy during migration, zspage freeing should
958 * hold locks of all pages in the zspage.
959 */
960void lock_zspage(struct zspage *zspage)
961{
962 struct page *page = get_first_page(zspage);
963
964 do {
965 lock_page(page);
966 } while ((page = get_next_page(page)) != NULL);
967}
968
969int trylock_zspage(struct zspage *zspage)
970{
971 struct page *cursor, *fail;
972
973 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
974 get_next_page(cursor)) {
975 if (!trylock_page(cursor)) {
976 fail = cursor;
977 goto unlock;
978 }
979 }
980
981 return 1;
982unlock:
983 for (cursor = get_first_page(zspage); cursor != fail; cursor =
984 get_next_page(cursor))
985 unlock_page(cursor);
986
987 return 0;
988}
989
990static void __free_zspage(struct zs_pool *pool, struct size_class *class,
991 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600992{
Minchan Kim37836892016-07-26 15:23:23 -0700993 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700994 enum fullness_group fg;
995 unsigned int class_idx;
996
997 get_zspage_mapping(zspage, &class_idx, &fg);
998
999 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -06001000
Minchan Kim37836892016-07-26 15:23:23 -07001001 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001002 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -06001003
Minchan Kim48b48002016-07-26 15:23:31 -07001004 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001005 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001006 VM_BUG_ON_PAGE(!PageLocked(page), page);
1007 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001008 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001009 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001010 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001011 put_page(page);
1012 page = next;
1013 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001014
Minchan Kim37836892016-07-26 15:23:23 -07001015 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001016
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001017 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001018 atomic_long_sub(class->pages_per_zspage,
1019 &pool->pages_allocated);
1020}
1021
1022static void free_zspage(struct zs_pool *pool, struct size_class *class,
1023 struct zspage *zspage)
1024{
1025 VM_BUG_ON(get_zspage_inuse(zspage));
1026 VM_BUG_ON(list_empty(&zspage->list));
1027
1028 if (!trylock_zspage(zspage)) {
1029 kick_deferred_free(pool);
1030 return;
1031 }
1032
1033 remove_zspage(class, zspage, ZS_EMPTY);
1034 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001035}
1036
1037/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001038static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001039{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001040 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001041 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001042 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001043
Nitin Gupta61989a82012-01-09 16:51:56 -06001044 while (page) {
1045 struct page *next_page;
1046 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001047 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001048
Minchan Kim37836892016-07-26 15:23:23 -07001049 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001050
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001051 vaddr = kmap_atomic(page);
1052 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001053
Dan Streetman5538c562014-10-09 15:30:01 -07001054 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001055 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001056 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001057 }
1058
1059 /*
1060 * We now come to the last (full or partial) object on this
1061 * page, which must point to the first object on the next
1062 * page (if present)
1063 */
1064 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001065 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001066 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001067 } else {
1068 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001069 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001070 * whether it's allocated object or not.
1071 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001072 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001073 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001074 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001075 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001076 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001077 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001078
Minchan Kimbfd093f2016-07-26 15:23:28 -07001079 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001080}
1081
Minchan Kim48b48002016-07-26 15:23:31 -07001082static void create_page_chain(struct size_class *class, struct zspage *zspage,
1083 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001084{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001085 int i;
1086 struct page *page;
1087 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001088 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001089
1090 /*
1091 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001092 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001093 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001094 *
Minchan Kim37836892016-07-26 15:23:23 -07001095 * we set PG_private to identify the first page (i.e. no other sub-page
1096 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -06001097 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001098 for (i = 0; i < nr_pages; i++) {
1099 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001100 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001101 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001102 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001103 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001104 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001105 if (unlikely(class->objs_per_zspage == 1 &&
1106 class->pages_per_zspage == 1))
1107 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001108 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001109 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001110 }
Minchan Kim48b48002016-07-26 15:23:31 -07001111 if (i == nr_pages - 1)
Minchan Kima27545bf2012-04-25 15:23:09 +09001112 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -06001113 prev_page = page;
1114 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001115}
Nitin Gupta61989a82012-01-09 16:51:56 -06001116
Minchan Kimbdb0af72016-07-26 15:23:20 -07001117/*
1118 * Allocate a zspage for the given size class
1119 */
Minchan Kim37836892016-07-26 15:23:23 -07001120static struct zspage *alloc_zspage(struct zs_pool *pool,
1121 struct size_class *class,
1122 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001123{
1124 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001125 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001126 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1127
1128 if (!zspage)
1129 return NULL;
1130
1131 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001132 zspage->magic = ZSPAGE_MAGIC;
1133 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001134
Minchan Kimbdb0af72016-07-26 15:23:20 -07001135 for (i = 0; i < class->pages_per_zspage; i++) {
1136 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001137
Minchan Kim37836892016-07-26 15:23:23 -07001138 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001139 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001140 while (--i >= 0) {
1141 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001142 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001143 }
Minchan Kim37836892016-07-26 15:23:23 -07001144 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001145 return NULL;
1146 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001147
1148 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001149 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001150 }
1151
Minchan Kim48b48002016-07-26 15:23:31 -07001152 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001153 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001154
Minchan Kim37836892016-07-26 15:23:23 -07001155 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001156}
1157
Minchan Kim37836892016-07-26 15:23:23 -07001158static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001159{
1160 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001161 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001162
Minchan Kim48b48002016-07-26 15:23:31 -07001163 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001164 zspage = list_first_entry_or_null(&class->fullness_list[i],
1165 struct zspage, list);
1166 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001167 break;
1168 }
1169
Minchan Kim37836892016-07-26 15:23:23 -07001170 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001171}
1172
Minchan Kim1b945ae2013-12-11 11:04:36 +09001173#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001174static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001175{
Seth Jenningsf5536462012-07-18 11:55:56 -05001176 /*
1177 * Make sure we don't leak memory if a cpu UP notification
1178 * and zs_init() race and both call zs_cpu_up() on the same cpu
1179 */
1180 if (area->vm)
1181 return 0;
1182 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1183 if (!area->vm)
1184 return -ENOMEM;
1185 return 0;
1186}
1187
1188static inline void __zs_cpu_down(struct mapping_area *area)
1189{
1190 if (area->vm)
1191 free_vm_area(area->vm);
1192 area->vm = NULL;
1193}
1194
1195static inline void *__zs_map_object(struct mapping_area *area,
1196 struct page *pages[2], int off, int size)
1197{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001198 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001199 area->vm_addr = area->vm->addr;
1200 return area->vm_addr + off;
1201}
1202
1203static inline void __zs_unmap_object(struct mapping_area *area,
1204 struct page *pages[2], int off, int size)
1205{
1206 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001207
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001208 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001209}
1210
Minchan Kim1b945ae2013-12-11 11:04:36 +09001211#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001212
1213static inline int __zs_cpu_up(struct mapping_area *area)
1214{
1215 /*
1216 * Make sure we don't leak memory if a cpu UP notification
1217 * and zs_init() race and both call zs_cpu_up() on the same cpu
1218 */
1219 if (area->vm_buf)
1220 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001221 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001222 if (!area->vm_buf)
1223 return -ENOMEM;
1224 return 0;
1225}
1226
1227static inline void __zs_cpu_down(struct mapping_area *area)
1228{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001229 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001230 area->vm_buf = NULL;
1231}
1232
1233static void *__zs_map_object(struct mapping_area *area,
1234 struct page *pages[2], int off, int size)
1235{
Seth Jennings5f601902012-07-02 16:15:49 -05001236 int sizes[2];
1237 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001238 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001239
Seth Jenningsf5536462012-07-18 11:55:56 -05001240 /* disable page faults to match kmap_atomic() return conditions */
1241 pagefault_disable();
1242
1243 /* no read fastpath */
1244 if (area->vm_mm == ZS_MM_WO)
1245 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001246
1247 sizes[0] = PAGE_SIZE - off;
1248 sizes[1] = size - sizes[0];
1249
Seth Jennings5f601902012-07-02 16:15:49 -05001250 /* copy object to per-cpu buffer */
1251 addr = kmap_atomic(pages[0]);
1252 memcpy(buf, addr + off, sizes[0]);
1253 kunmap_atomic(addr);
1254 addr = kmap_atomic(pages[1]);
1255 memcpy(buf + sizes[0], addr, sizes[1]);
1256 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001257out:
1258 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001259}
1260
Seth Jenningsf5536462012-07-18 11:55:56 -05001261static void __zs_unmap_object(struct mapping_area *area,
1262 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001263{
Seth Jennings5f601902012-07-02 16:15:49 -05001264 int sizes[2];
1265 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001266 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001267
Seth Jenningsf5536462012-07-18 11:55:56 -05001268 /* no write fastpath */
1269 if (area->vm_mm == ZS_MM_RO)
1270 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001271
Minchan Kim7b60a682015-04-15 16:15:39 -07001272 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001273 buf = buf + ZS_HANDLE_SIZE;
1274 size -= ZS_HANDLE_SIZE;
1275 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001276
Seth Jennings5f601902012-07-02 16:15:49 -05001277 sizes[0] = PAGE_SIZE - off;
1278 sizes[1] = size - sizes[0];
1279
1280 /* copy per-cpu buffer to object */
1281 addr = kmap_atomic(pages[0]);
1282 memcpy(addr + off, buf, sizes[0]);
1283 kunmap_atomic(addr);
1284 addr = kmap_atomic(pages[1]);
1285 memcpy(addr, buf + sizes[0], sizes[1]);
1286 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001287
1288out:
1289 /* enable page faults to match kunmap_atomic() return conditions */
1290 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001291}
Nitin Gupta61989a82012-01-09 16:51:56 -06001292
Minchan Kim1b945ae2013-12-11 11:04:36 +09001293#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001294
Nitin Gupta61989a82012-01-09 16:51:56 -06001295static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1296 void *pcpu)
1297{
Seth Jenningsf5536462012-07-18 11:55:56 -05001298 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001299 struct mapping_area *area;
1300
1301 switch (action) {
1302 case CPU_UP_PREPARE:
1303 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001304 ret = __zs_cpu_up(area);
1305 if (ret)
1306 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001307 break;
1308 case CPU_DEAD:
1309 case CPU_UP_CANCELED:
1310 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001311 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001312 break;
1313 }
1314
1315 return NOTIFY_OK;
1316}
1317
1318static struct notifier_block zs_cpu_nb = {
1319 .notifier_call = zs_cpu_notifier
1320};
1321
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001322static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001323{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001324 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001325
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301326 cpu_notifier_register_begin();
1327
1328 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001329 for_each_online_cpu(cpu) {
1330 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001331 if (notifier_to_errno(ret))
1332 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001333 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301334
1335 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001336 return notifier_to_errno(ret);
1337}
1338
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001339static void zs_unregister_cpu_notifier(void)
1340{
1341 int cpu;
1342
1343 cpu_notifier_register_begin();
1344
1345 for_each_online_cpu(cpu)
1346 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1347 __unregister_cpu_notifier(&zs_cpu_nb);
1348
1349 cpu_notifier_register_done();
1350}
1351
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001352static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001353{
1354 int nr;
1355
1356 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1357 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1358 nr += 1;
1359
1360 zs_size_classes = nr;
1361}
1362
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001363static bool can_merge(struct size_class *prev, int pages_per_zspage,
1364 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001365{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001366 if (prev->pages_per_zspage == pages_per_zspage &&
1367 prev->objs_per_zspage == objs_per_zspage)
1368 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001369
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001370 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001371}
1372
Minchan Kim37836892016-07-26 15:23:23 -07001373static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001374{
Minchan Kim37836892016-07-26 15:23:23 -07001375 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001376}
1377
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001378unsigned long zs_get_total_pages(struct zs_pool *pool)
1379{
1380 return atomic_long_read(&pool->pages_allocated);
1381}
1382EXPORT_SYMBOL_GPL(zs_get_total_pages);
1383
1384/**
1385 * zs_map_object - get address of allocated object from handle.
1386 * @pool: pool from which the object was allocated
1387 * @handle: handle returned from zs_malloc
1388 *
1389 * Before using an object allocated from zs_malloc, it must be mapped using
1390 * this function. When done with the object, it must be unmapped using
1391 * zs_unmap_object.
1392 *
1393 * Only one object can be mapped per cpu at a time. There is no protection
1394 * against nested mappings.
1395 *
1396 * This function returns with preemption and page faults disabled.
1397 */
1398void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1399 enum zs_mapmode mm)
1400{
Minchan Kim37836892016-07-26 15:23:23 -07001401 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001402 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001403 unsigned long obj, off;
1404 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001405
1406 unsigned int class_idx;
1407 enum fullness_group fg;
1408 struct size_class *class;
1409 struct mapping_area *area;
1410 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001411 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001412
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001413 /*
1414 * Because we use per-cpu mapping areas shared among the
1415 * pools/users, we can't allow mapping in interrupt context
1416 * because it can corrupt another users mappings.
1417 */
Sergey Senozhatsky16184002017-11-15 17:34:03 -08001418 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001419
Minchan Kim312fcae2015-04-15 16:15:30 -07001420 /* From now on, migration cannot move the object */
1421 pin_tag(handle);
1422
Minchan Kim2e40e162015-04-15 16:15:23 -07001423 obj = handle_to_obj(handle);
1424 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001425 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001426
1427 /* migration cannot move any subpage in this zspage */
1428 migrate_read_lock(zspage);
1429
Minchan Kim37836892016-07-26 15:23:23 -07001430 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001431 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001432 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001433
1434 area = &get_cpu_var(zs_map_area);
1435 area->vm_mm = mm;
1436 if (off + class->size <= PAGE_SIZE) {
1437 /* this object is contained entirely within a page */
1438 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001439 ret = area->vm_addr + off;
1440 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001441 }
1442
1443 /* this object spans two pages */
1444 pages[0] = page;
1445 pages[1] = get_next_page(page);
1446 BUG_ON(!pages[1]);
1447
Minchan Kim2e40e162015-04-15 16:15:23 -07001448 ret = __zs_map_object(area, pages, off, class->size);
1449out:
Minchan Kim48b48002016-07-26 15:23:31 -07001450 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001451 ret += ZS_HANDLE_SIZE;
1452
1453 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001454}
1455EXPORT_SYMBOL_GPL(zs_map_object);
1456
1457void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1458{
Minchan Kim37836892016-07-26 15:23:23 -07001459 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001460 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001461 unsigned long obj, off;
1462 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001463
1464 unsigned int class_idx;
1465 enum fullness_group fg;
1466 struct size_class *class;
1467 struct mapping_area *area;
1468
Minchan Kim2e40e162015-04-15 16:15:23 -07001469 obj = handle_to_obj(handle);
1470 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001471 zspage = get_zspage(page);
1472 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001473 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001474 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001475
1476 area = this_cpu_ptr(&zs_map_area);
1477 if (off + class->size <= PAGE_SIZE)
1478 kunmap_atomic(area->vm_addr);
1479 else {
1480 struct page *pages[2];
1481
1482 pages[0] = page;
1483 pages[1] = get_next_page(page);
1484 BUG_ON(!pages[1]);
1485
1486 __zs_unmap_object(area, pages, off, class->size);
1487 }
1488 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001489
1490 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001491 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001492}
1493EXPORT_SYMBOL_GPL(zs_unmap_object);
1494
Minchan Kim251cbb92016-05-20 16:59:42 -07001495static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001496 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001497{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001498 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001499 unsigned long obj;
1500 struct link_free *link;
1501
1502 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001503 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001504 void *vaddr;
1505
Minchan Kim312fcae2015-04-15 16:15:30 -07001506 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001507 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001508
1509 offset = obj * class->size;
1510 nr_page = offset >> PAGE_SHIFT;
1511 m_offset = offset & ~PAGE_MASK;
1512 m_page = get_first_page(zspage);
1513
1514 for (i = 0; i < nr_page; i++)
1515 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001516
1517 vaddr = kmap_atomic(m_page);
1518 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001519 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001520 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001521 /* record handle in the header of allocated chunk */
1522 link->handle = handle;
1523 else
Minchan Kim37836892016-07-26 15:23:23 -07001524 /* record handle to page->index */
1525 zspage->first_page->index = handle;
1526
Minchan Kimc7806262015-04-15 16:15:26 -07001527 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001528 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001529 zs_stat_inc(class, OBJ_USED, 1);
1530
Minchan Kimbfd093f2016-07-26 15:23:28 -07001531 obj = location_to_obj(m_page, obj);
1532
Minchan Kimc7806262015-04-15 16:15:26 -07001533 return obj;
1534}
1535
1536
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001537/**
1538 * zs_malloc - Allocate block of given size from pool.
1539 * @pool: pool to allocate from
1540 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001541 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001542 *
1543 * On success, handle to the allocated object is returned,
1544 * otherwise 0.
1545 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1546 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001547unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548{
Minchan Kim2e40e162015-04-15 16:15:23 -07001549 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001550 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001551 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001552 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001553
Minchan Kim7b60a682015-04-15 16:15:39 -07001554 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001555 return 0;
1556
Minchan Kim37836892016-07-26 15:23:23 -07001557 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001558 if (!handle)
1559 return 0;
1560
1561 /* extra space in chunk to keep the handle */
1562 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001563 class = pool->size_class[get_size_class_index(size)];
1564
1565 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001566 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001567 if (likely(zspage)) {
1568 obj = obj_malloc(class, zspage, handle);
1569 /* Now move the zspage to another fullness group, if required */
1570 fix_fullness_group(class, zspage);
1571 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001572 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001573
Minchan Kim48b48002016-07-26 15:23:31 -07001574 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001575 }
1576
Minchan Kim48b48002016-07-26 15:23:31 -07001577 spin_unlock(&class->lock);
1578
1579 zspage = alloc_zspage(pool, class, gfp);
1580 if (!zspage) {
1581 cache_free_handle(pool, handle);
1582 return 0;
1583 }
1584
1585 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001586 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001587 newfg = get_fullness_group(class, zspage);
1588 insert_zspage(class, zspage, newfg);
1589 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001590 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001591 atomic_long_add(class->pages_per_zspage,
1592 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001593 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001594
1595 /* We completely set up zspage so mark them as movable */
1596 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001597 spin_unlock(&class->lock);
1598
Minchan Kim2e40e162015-04-15 16:15:23 -07001599 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001600}
1601EXPORT_SYMBOL_GPL(zs_malloc);
1602
Minchan Kim1ee47162016-05-20 16:59:45 -07001603static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001604{
1605 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001606 struct zspage *zspage;
1607 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001608 unsigned long f_offset;
1609 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001610 void *vaddr;
1611
Minchan Kim312fcae2015-04-15 16:15:30 -07001612 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001613 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001614 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001615 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001616
Minchan Kimc7806262015-04-15 16:15:26 -07001617 vaddr = kmap_atomic(f_page);
1618
1619 /* Insert this object in containing zspage's freelist */
1620 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001621 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001622 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001623 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001624 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001625 zs_stat_dec(class, OBJ_USED, 1);
1626}
1627
1628void zs_free(struct zs_pool *pool, unsigned long handle)
1629{
Minchan Kim37836892016-07-26 15:23:23 -07001630 struct zspage *zspage;
1631 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001632 unsigned long obj;
1633 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001634 int class_idx;
1635 struct size_class *class;
1636 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001637 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001638
Minchan Kim2e40e162015-04-15 16:15:23 -07001639 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001640 return;
1641
Minchan Kim312fcae2015-04-15 16:15:30 -07001642 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001643 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001644 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001645 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001646
Minchan Kim48b48002016-07-26 15:23:31 -07001647 migrate_read_lock(zspage);
1648
Minchan Kim37836892016-07-26 15:23:23 -07001649 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001650 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001651
1652 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001653 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001654 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001655 if (fullness != ZS_EMPTY) {
1656 migrate_read_unlock(zspage);
1657 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001658 }
Minchan Kim48b48002016-07-26 15:23:31 -07001659
1660 isolated = is_zspage_isolated(zspage);
1661 migrate_read_unlock(zspage);
1662 /* If zspage is isolated, zs_page_putback will free the zspage */
1663 if (likely(!isolated))
1664 free_zspage(pool, class, zspage);
1665out:
1666
Minchan Kim312fcae2015-04-15 16:15:30 -07001667 spin_unlock(&class->lock);
1668 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001669 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001670}
1671EXPORT_SYMBOL_GPL(zs_free);
1672
Minchan Kim251cbb92016-05-20 16:59:42 -07001673static void zs_object_copy(struct size_class *class, unsigned long dst,
1674 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001675{
1676 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001677 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001678 unsigned long s_off, d_off;
1679 void *s_addr, *d_addr;
1680 int s_size, d_size, size;
1681 int written = 0;
1682
1683 s_size = d_size = class->size;
1684
1685 obj_to_location(src, &s_page, &s_objidx);
1686 obj_to_location(dst, &d_page, &d_objidx);
1687
Minchan Kimbfd093f2016-07-26 15:23:28 -07001688 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1689 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001690
1691 if (s_off + class->size > PAGE_SIZE)
1692 s_size = PAGE_SIZE - s_off;
1693
1694 if (d_off + class->size > PAGE_SIZE)
1695 d_size = PAGE_SIZE - d_off;
1696
1697 s_addr = kmap_atomic(s_page);
1698 d_addr = kmap_atomic(d_page);
1699
1700 while (1) {
1701 size = min(s_size, d_size);
1702 memcpy(d_addr + d_off, s_addr + s_off, size);
1703 written += size;
1704
1705 if (written == class->size)
1706 break;
1707
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001708 s_off += size;
1709 s_size -= size;
1710 d_off += size;
1711 d_size -= size;
1712
1713 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001714 kunmap_atomic(d_addr);
1715 kunmap_atomic(s_addr);
1716 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001717 s_addr = kmap_atomic(s_page);
1718 d_addr = kmap_atomic(d_page);
1719 s_size = class->size - written;
1720 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001721 }
1722
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001723 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001724 kunmap_atomic(d_addr);
1725 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001726 d_addr = kmap_atomic(d_page);
1727 d_size = class->size - written;
1728 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001729 }
1730 }
1731
1732 kunmap_atomic(d_addr);
1733 kunmap_atomic(s_addr);
1734}
1735
1736/*
1737 * Find alloced object in zspage from index object and
1738 * return handle.
1739 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001740static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001741 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001742{
1743 unsigned long head;
1744 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001745 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001746 unsigned long handle = 0;
1747 void *addr = kmap_atomic(page);
1748
Minchan Kim37836892016-07-26 15:23:23 -07001749 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001750 offset += class->size * index;
1751
1752 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001753 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001754 if (head & OBJ_ALLOCATED_TAG) {
1755 handle = head & ~OBJ_ALLOCATED_TAG;
1756 if (trypin_tag(handle))
1757 break;
1758 handle = 0;
1759 }
1760
1761 offset += class->size;
1762 index++;
1763 }
1764
1765 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001766
1767 *obj_idx = index;
1768
Minchan Kim312fcae2015-04-15 16:15:30 -07001769 return handle;
1770}
1771
1772struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001773 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001774 struct page *s_page;
1775 /* Destination page for migration which should be a first page
1776 * of zspage. */
1777 struct page *d_page;
1778 /* Starting object index within @s_page which used for live object
1779 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001780 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001781};
1782
1783static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1784 struct zs_compact_control *cc)
1785{
1786 unsigned long used_obj, free_obj;
1787 unsigned long handle;
1788 struct page *s_page = cc->s_page;
1789 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001790 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001791 int ret = 0;
1792
1793 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001794 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001795 if (!handle) {
1796 s_page = get_next_page(s_page);
1797 if (!s_page)
1798 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001799 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001800 continue;
1801 }
1802
1803 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001804 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001805 unpin_tag(handle);
1806 ret = -ENOMEM;
1807 break;
1808 }
1809
1810 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001811 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001812 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001813 obj_idx++;
Junil Leec102f072016-01-20 14:58:18 -08001814 /*
1815 * record_obj updates handle's value to free_obj and it will
1816 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1817 * breaks synchronization using pin_tag(e,g, zs_free) so
1818 * let's keep the lock bit.
1819 */
1820 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001821 record_obj(handle, free_obj);
1822 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001823 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001824 }
1825
1826 /* Remember last position in this iteration */
1827 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001828 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001829
1830 return ret;
1831}
1832
Minchan Kim37836892016-07-26 15:23:23 -07001833static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001834{
1835 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001836 struct zspage *zspage;
1837 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001838
Minchan Kim37836892016-07-26 15:23:23 -07001839 if (!source) {
1840 fg[0] = ZS_ALMOST_FULL;
1841 fg[1] = ZS_ALMOST_EMPTY;
1842 }
1843
1844 for (i = 0; i < 2; i++) {
1845 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1846 struct zspage, list);
1847 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001848 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001849 remove_zspage(class, zspage, fg[i]);
1850 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001851 }
1852 }
1853
Minchan Kim37836892016-07-26 15:23:23 -07001854 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001855}
1856
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001857/*
Minchan Kim37836892016-07-26 15:23:23 -07001858 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001859 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001860 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001861 *
Minchan Kim37836892016-07-26 15:23:23 -07001862 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001863 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001864static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001865 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001866{
Minchan Kim312fcae2015-04-15 16:15:30 -07001867 enum fullness_group fullness;
1868
Minchan Kim48b48002016-07-26 15:23:31 -07001869 VM_BUG_ON(is_zspage_isolated(zspage));
1870
Minchan Kim37836892016-07-26 15:23:23 -07001871 fullness = get_fullness_group(class, zspage);
1872 insert_zspage(class, zspage, fullness);
1873 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001874
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001875 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001876}
1877
Minchan Kim48b48002016-07-26 15:23:31 -07001878#ifdef CONFIG_COMPACTION
1879static struct dentry *zs_mount(struct file_system_type *fs_type,
1880 int flags, const char *dev_name, void *data)
1881{
1882 static const struct dentry_operations ops = {
1883 .d_dname = simple_dname,
1884 };
1885
1886 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1887}
1888
1889static struct file_system_type zsmalloc_fs = {
1890 .name = "zsmalloc",
1891 .mount = zs_mount,
1892 .kill_sb = kill_anon_super,
1893};
1894
1895static int zsmalloc_mount(void)
1896{
1897 int ret = 0;
1898
1899 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1900 if (IS_ERR(zsmalloc_mnt))
1901 ret = PTR_ERR(zsmalloc_mnt);
1902
1903 return ret;
1904}
1905
1906static void zsmalloc_unmount(void)
1907{
1908 kern_unmount(zsmalloc_mnt);
1909}
1910
1911static void migrate_lock_init(struct zspage *zspage)
1912{
1913 rwlock_init(&zspage->lock);
1914}
1915
1916static void migrate_read_lock(struct zspage *zspage)
1917{
1918 read_lock(&zspage->lock);
1919}
1920
1921static void migrate_read_unlock(struct zspage *zspage)
1922{
1923 read_unlock(&zspage->lock);
1924}
1925
1926static void migrate_write_lock(struct zspage *zspage)
1927{
1928 write_lock(&zspage->lock);
1929}
1930
1931static void migrate_write_unlock(struct zspage *zspage)
1932{
1933 write_unlock(&zspage->lock);
1934}
1935
1936/* Number of isolated subpage for *page migration* in this zspage */
1937static void inc_zspage_isolation(struct zspage *zspage)
1938{
1939 zspage->isolated++;
1940}
1941
1942static void dec_zspage_isolation(struct zspage *zspage)
1943{
1944 zspage->isolated--;
1945}
1946
Henry Burns29295162019-08-24 17:55:03 -07001947static void putback_zspage_deferred(struct zs_pool *pool,
1948 struct size_class *class,
1949 struct zspage *zspage)
1950{
1951 enum fullness_group fg;
1952
1953 fg = putback_zspage(class, zspage);
1954 if (fg == ZS_EMPTY)
1955 schedule_work(&pool->free_work);
1956
1957}
1958
Henry Burns0e7e2732019-08-24 17:55:06 -07001959static inline void zs_pool_dec_isolated(struct zs_pool *pool)
1960{
1961 VM_BUG_ON(atomic_long_read(&pool->isolated_pages) <= 0);
1962 atomic_long_dec(&pool->isolated_pages);
1963 /*
1964 * There's no possibility of racing, since wait_for_isolated_drain()
1965 * checks the isolated count under &class->lock after enqueuing
1966 * on migration_wait.
1967 */
1968 if (atomic_long_read(&pool->isolated_pages) == 0 && pool->destroying)
1969 wake_up_all(&pool->migration_wait);
1970}
1971
Minchan Kim48b48002016-07-26 15:23:31 -07001972static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1973 struct page *newpage, struct page *oldpage)
1974{
1975 struct page *page;
1976 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1977 int idx = 0;
1978
1979 page = get_first_page(zspage);
1980 do {
1981 if (page == oldpage)
1982 pages[idx] = newpage;
1983 else
1984 pages[idx] = page;
1985 idx++;
1986 } while ((page = get_next_page(page)) != NULL);
1987
1988 create_page_chain(class, zspage, pages);
1989 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1990 if (unlikely(PageHugeObject(oldpage)))
1991 newpage->index = oldpage->index;
1992 __SetPageMovable(newpage, page_mapping(oldpage));
1993}
1994
1995bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1996{
1997 struct zs_pool *pool;
1998 struct size_class *class;
1999 int class_idx;
2000 enum fullness_group fullness;
2001 struct zspage *zspage;
2002 struct address_space *mapping;
2003
2004 /*
2005 * Page is locked so zspage couldn't be destroyed. For detail, look at
2006 * lock_zspage in free_zspage.
2007 */
2008 VM_BUG_ON_PAGE(!PageMovable(page), page);
2009 VM_BUG_ON_PAGE(PageIsolated(page), page);
2010
2011 zspage = get_zspage(page);
2012
2013 /*
2014 * Without class lock, fullness could be stale while class_idx is okay
2015 * because class_idx is constant unless page is freed so we should get
2016 * fullness again under class lock.
2017 */
2018 get_zspage_mapping(zspage, &class_idx, &fullness);
2019 mapping = page_mapping(page);
2020 pool = mapping->private_data;
2021 class = pool->size_class[class_idx];
2022
2023 spin_lock(&class->lock);
2024 if (get_zspage_inuse(zspage) == 0) {
2025 spin_unlock(&class->lock);
2026 return false;
2027 }
2028
2029 /* zspage is isolated for object migration */
2030 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2031 spin_unlock(&class->lock);
2032 return false;
2033 }
2034
2035 /*
2036 * If this is first time isolation for the zspage, isolate zspage from
2037 * size_class to prevent further object allocation from the zspage.
2038 */
2039 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
2040 get_zspage_mapping(zspage, &class_idx, &fullness);
Henry Burns0e7e2732019-08-24 17:55:06 -07002041 atomic_long_inc(&pool->isolated_pages);
Minchan Kim48b48002016-07-26 15:23:31 -07002042 remove_zspage(class, zspage, fullness);
2043 }
2044
2045 inc_zspage_isolation(zspage);
2046 spin_unlock(&class->lock);
2047
2048 return true;
2049}
2050
2051int zs_page_migrate(struct address_space *mapping, struct page *newpage,
2052 struct page *page, enum migrate_mode mode)
2053{
2054 struct zs_pool *pool;
2055 struct size_class *class;
2056 int class_idx;
2057 enum fullness_group fullness;
2058 struct zspage *zspage;
2059 struct page *dummy;
2060 void *s_addr, *d_addr, *addr;
2061 int offset, pos;
2062 unsigned long handle, head;
2063 unsigned long old_obj, new_obj;
2064 unsigned int obj_idx;
2065 int ret = -EAGAIN;
2066
2067 VM_BUG_ON_PAGE(!PageMovable(page), page);
2068 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2069
2070 zspage = get_zspage(page);
2071
2072 /* Concurrent compactor cannot migrate any subpage in zspage */
2073 migrate_write_lock(zspage);
2074 get_zspage_mapping(zspage, &class_idx, &fullness);
2075 pool = mapping->private_data;
2076 class = pool->size_class[class_idx];
2077 offset = get_first_obj_offset(page);
2078
2079 spin_lock(&class->lock);
2080 if (!get_zspage_inuse(zspage)) {
2081 ret = -EBUSY;
2082 goto unlock_class;
2083 }
2084
2085 pos = offset;
2086 s_addr = kmap_atomic(page);
2087 while (pos < PAGE_SIZE) {
2088 head = obj_to_head(page, s_addr + pos);
2089 if (head & OBJ_ALLOCATED_TAG) {
2090 handle = head & ~OBJ_ALLOCATED_TAG;
2091 if (!trypin_tag(handle))
2092 goto unpin_objects;
2093 }
2094 pos += class->size;
2095 }
2096
2097 /*
2098 * Here, any user cannot access all objects in the zspage so let's move.
2099 */
2100 d_addr = kmap_atomic(newpage);
2101 memcpy(d_addr, s_addr, PAGE_SIZE);
2102 kunmap_atomic(d_addr);
2103
2104 for (addr = s_addr + offset; addr < s_addr + pos;
2105 addr += class->size) {
2106 head = obj_to_head(page, addr);
2107 if (head & OBJ_ALLOCATED_TAG) {
2108 handle = head & ~OBJ_ALLOCATED_TAG;
2109 if (!testpin_tag(handle))
2110 BUG();
2111
2112 old_obj = handle_to_obj(handle);
2113 obj_to_location(old_obj, &dummy, &obj_idx);
2114 new_obj = (unsigned long)location_to_obj(newpage,
2115 obj_idx);
2116 new_obj |= BIT(HANDLE_PIN_BIT);
2117 record_obj(handle, new_obj);
2118 }
2119 }
2120
2121 replace_sub_page(class, zspage, newpage, page);
2122 get_page(newpage);
2123
2124 dec_zspage_isolation(zspage);
2125
2126 /*
2127 * Page migration is done so let's putback isolated zspage to
2128 * the list if @page is final isolated subpage in the zspage.
2129 */
Henry Burns0e7e2732019-08-24 17:55:06 -07002130 if (!is_zspage_isolated(zspage)) {
2131 /*
2132 * We cannot race with zs_destroy_pool() here because we wait
2133 * for isolation to hit zero before we start destroying.
2134 * Also, we ensure that everyone can see pool->destroying before
2135 * we start waiting.
2136 */
Henry Burns29295162019-08-24 17:55:03 -07002137 putback_zspage_deferred(pool, class, zspage);
Henry Burns0e7e2732019-08-24 17:55:06 -07002138 zs_pool_dec_isolated(pool);
2139 }
Minchan Kim48b48002016-07-26 15:23:31 -07002140
2141 reset_page(page);
2142 put_page(page);
2143 page = newpage;
2144
Minchan Kimdd4123f2016-07-26 15:26:50 -07002145 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002146unpin_objects:
2147 for (addr = s_addr + offset; addr < s_addr + pos;
2148 addr += class->size) {
2149 head = obj_to_head(page, addr);
2150 if (head & OBJ_ALLOCATED_TAG) {
2151 handle = head & ~OBJ_ALLOCATED_TAG;
2152 if (!testpin_tag(handle))
2153 BUG();
2154 unpin_tag(handle);
2155 }
2156 }
2157 kunmap_atomic(s_addr);
2158unlock_class:
2159 spin_unlock(&class->lock);
2160 migrate_write_unlock(zspage);
2161
2162 return ret;
2163}
2164
2165void zs_page_putback(struct page *page)
2166{
2167 struct zs_pool *pool;
2168 struct size_class *class;
2169 int class_idx;
2170 enum fullness_group fg;
2171 struct address_space *mapping;
2172 struct zspage *zspage;
2173
2174 VM_BUG_ON_PAGE(!PageMovable(page), page);
2175 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2176
2177 zspage = get_zspage(page);
2178 get_zspage_mapping(zspage, &class_idx, &fg);
2179 mapping = page_mapping(page);
2180 pool = mapping->private_data;
2181 class = pool->size_class[class_idx];
2182
2183 spin_lock(&class->lock);
2184 dec_zspage_isolation(zspage);
2185 if (!is_zspage_isolated(zspage)) {
Minchan Kim48b48002016-07-26 15:23:31 -07002186 /*
2187 * Due to page_lock, we cannot free zspage immediately
2188 * so let's defer.
2189 */
Henry Burns29295162019-08-24 17:55:03 -07002190 putback_zspage_deferred(pool, class, zspage);
Henry Burns0e7e2732019-08-24 17:55:06 -07002191 zs_pool_dec_isolated(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002192 }
2193 spin_unlock(&class->lock);
2194}
2195
2196const struct address_space_operations zsmalloc_aops = {
2197 .isolate_page = zs_page_isolate,
2198 .migratepage = zs_page_migrate,
2199 .putback_page = zs_page_putback,
2200};
2201
2202static int zs_register_migration(struct zs_pool *pool)
2203{
2204 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2205 if (IS_ERR(pool->inode)) {
2206 pool->inode = NULL;
2207 return 1;
2208 }
2209
2210 pool->inode->i_mapping->private_data = pool;
2211 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2212 return 0;
2213}
2214
Henry Burns0e7e2732019-08-24 17:55:06 -07002215static bool pool_isolated_are_drained(struct zs_pool *pool)
2216{
2217 return atomic_long_read(&pool->isolated_pages) == 0;
2218}
2219
2220/* Function for resolving migration */
2221static void wait_for_isolated_drain(struct zs_pool *pool)
2222{
2223
2224 /*
2225 * We're in the process of destroying the pool, so there are no
2226 * active allocations. zs_page_isolate() fails for completely free
2227 * zspages, so we need only wait for the zs_pool's isolated
2228 * count to hit zero.
2229 */
2230 wait_event(pool->migration_wait,
2231 pool_isolated_are_drained(pool));
2232}
2233
Minchan Kim48b48002016-07-26 15:23:31 -07002234static void zs_unregister_migration(struct zs_pool *pool)
2235{
Henry Burns0e7e2732019-08-24 17:55:06 -07002236 pool->destroying = true;
2237 /*
2238 * We need a memory barrier here to ensure global visibility of
2239 * pool->destroying. Thus pool->isolated pages will either be 0 in which
2240 * case we don't care, or it will be > 0 and pool->destroying will
2241 * ensure that we wake up once isolation hits 0.
2242 */
2243 smp_mb();
2244 wait_for_isolated_drain(pool); /* This can block */
Minchan Kim48b48002016-07-26 15:23:31 -07002245 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002246 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002247}
2248
2249/*
2250 * Caller should hold page_lock of all pages in the zspage
2251 * In here, we cannot use zspage meta data.
2252 */
2253static void async_free_zspage(struct work_struct *work)
2254{
2255 int i;
2256 struct size_class *class;
2257 unsigned int class_idx;
2258 enum fullness_group fullness;
2259 struct zspage *zspage, *tmp;
2260 LIST_HEAD(free_pages);
2261 struct zs_pool *pool = container_of(work, struct zs_pool,
2262 free_work);
2263
2264 for (i = 0; i < zs_size_classes; i++) {
2265 class = pool->size_class[i];
2266 if (class->index != i)
2267 continue;
2268
2269 spin_lock(&class->lock);
2270 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2271 spin_unlock(&class->lock);
2272 }
2273
2274
2275 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2276 list_del(&zspage->list);
2277 lock_zspage(zspage);
2278
2279 get_zspage_mapping(zspage, &class_idx, &fullness);
2280 VM_BUG_ON(fullness != ZS_EMPTY);
2281 class = pool->size_class[class_idx];
2282 spin_lock(&class->lock);
2283 __free_zspage(pool, pool->size_class[class_idx], zspage);
2284 spin_unlock(&class->lock);
2285 }
2286};
2287
2288static void kick_deferred_free(struct zs_pool *pool)
2289{
2290 schedule_work(&pool->free_work);
2291}
2292
2293static void init_deferred_free(struct zs_pool *pool)
2294{
2295 INIT_WORK(&pool->free_work, async_free_zspage);
2296}
2297
2298static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2299{
2300 struct page *page = get_first_page(zspage);
2301
2302 do {
2303 WARN_ON(!trylock_page(page));
2304 __SetPageMovable(page, pool->inode->i_mapping);
2305 unlock_page(page);
2306 } while ((page = get_next_page(page)) != NULL);
2307}
2308#endif
2309
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002310/*
2311 *
2312 * Based on the number of unused allocated objects calculate
2313 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002314 */
2315static unsigned long zs_can_compact(struct size_class *class)
2316{
2317 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002318 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2319 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002320
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002321 if (obj_allocated <= obj_used)
2322 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002323
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002324 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002325 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002326
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002327 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002328}
2329
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002330static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002331{
Minchan Kim312fcae2015-04-15 16:15:30 -07002332 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002333 struct zspage *src_zspage;
2334 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002335
Minchan Kim312fcae2015-04-15 16:15:30 -07002336 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002337 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002338
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002339 if (!zs_can_compact(class))
2340 break;
2341
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002342 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002343 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002344
Minchan Kim37836892016-07-26 15:23:23 -07002345 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002346 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002347 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07002348 * If there is no more space in dst_page, resched
2349 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002350 */
2351 if (!migrate_zspage(pool, class, &cc))
2352 break;
2353
Minchan Kim4aa409c2016-07-26 15:23:26 -07002354 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002355 }
2356
2357 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002358 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002359 break;
2360
Minchan Kim4aa409c2016-07-26 15:23:26 -07002361 putback_zspage(class, dst_zspage);
2362 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002363 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002364 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002365 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002366 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002367 cond_resched();
2368 spin_lock(&class->lock);
2369 }
2370
Minchan Kim37836892016-07-26 15:23:23 -07002371 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002372 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002373
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002374 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002375}
2376
2377unsigned long zs_compact(struct zs_pool *pool)
2378{
2379 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002380 struct size_class *class;
2381
2382 for (i = zs_size_classes - 1; i >= 0; i--) {
2383 class = pool->size_class[i];
2384 if (!class)
2385 continue;
2386 if (class->index != i)
2387 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002388 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002389 }
2390
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002391 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002392}
2393EXPORT_SYMBOL_GPL(zs_compact);
2394
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002395void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2396{
2397 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2398}
2399EXPORT_SYMBOL_GPL(zs_pool_stats);
2400
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002401static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2402 struct shrink_control *sc)
2403{
2404 unsigned long pages_freed;
2405 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2406 shrinker);
2407
2408 pages_freed = pool->stats.pages_compacted;
2409 /*
2410 * Compact classes and calculate compaction delta.
2411 * Can run concurrently with a manually triggered
2412 * (by user) compaction.
2413 */
2414 pages_freed = zs_compact(pool) - pages_freed;
2415
2416 return pages_freed ? pages_freed : SHRINK_STOP;
2417}
2418
2419static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2420 struct shrink_control *sc)
2421{
2422 int i;
2423 struct size_class *class;
2424 unsigned long pages_to_free = 0;
2425 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2426 shrinker);
2427
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002428 for (i = zs_size_classes - 1; i >= 0; i--) {
2429 class = pool->size_class[i];
2430 if (!class)
2431 continue;
2432 if (class->index != i)
2433 continue;
2434
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002435 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002436 }
2437
2438 return pages_to_free;
2439}
2440
2441static void zs_unregister_shrinker(struct zs_pool *pool)
2442{
2443 if (pool->shrinker_enabled) {
2444 unregister_shrinker(&pool->shrinker);
2445 pool->shrinker_enabled = false;
2446 }
2447}
2448
2449static int zs_register_shrinker(struct zs_pool *pool)
2450{
2451 pool->shrinker.scan_objects = zs_shrinker_scan;
2452 pool->shrinker.count_objects = zs_shrinker_count;
2453 pool->shrinker.batch = 0;
2454 pool->shrinker.seeks = DEFAULT_SEEKS;
2455
2456 return register_shrinker(&pool->shrinker);
2457}
2458
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002459/**
2460 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002461 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002462 *
2463 * This function must be called before anything when using
2464 * the zsmalloc allocator.
2465 *
2466 * On success, a pointer to the newly created pool is returned,
2467 * otherwise NULL.
2468 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002469struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002470{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002471 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002472 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002473 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002474
Ganesh Mahendran18136652014-12-12 16:57:10 -08002475 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002476 if (!pool)
2477 return NULL;
2478
Minchan Kim48b48002016-07-26 15:23:31 -07002479 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002480 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2481 GFP_KERNEL);
2482 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002483 kfree(pool);
2484 return NULL;
2485 }
2486
Minchan Kim2e40e162015-04-15 16:15:23 -07002487 pool->name = kstrdup(name, GFP_KERNEL);
2488 if (!pool->name)
2489 goto err;
2490
Henry Burns0e7e2732019-08-24 17:55:06 -07002491 init_waitqueue_head(&pool->migration_wait);
2492
Minchan Kim37836892016-07-26 15:23:23 -07002493 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002494 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002495
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002496 /*
2497 * Iterate reversly, because, size of size_class that we want to use
2498 * for merging should be larger or equal to current size.
2499 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002500 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002501 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002502 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002503 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002504 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002505 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002506
2507 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2508 if (size > ZS_MAX_ALLOC_SIZE)
2509 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002510 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002511 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002512
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002513 /*
2514 * size_class is used for normal zsmalloc operation such
2515 * as alloc/free for that size. Although it is natural that we
2516 * have one size_class for each size, there is a chance that we
2517 * can get more memory utilization if we use one size_class for
2518 * many different sizes whose size_class have same
2519 * characteristics. So, we makes size_class point to
2520 * previous size_class if possible.
2521 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002522 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002523 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002524 pool->size_class[i] = prev_class;
2525 continue;
2526 }
2527 }
2528
2529 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2530 if (!class)
2531 goto err;
2532
Nitin Gupta61989a82012-01-09 16:51:56 -06002533 class->size = size;
2534 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002535 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002536 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002537 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002538 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002539 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2540 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002541 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002542
2543 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002544 }
2545
Dan Streetmand34f6152016-05-20 16:59:56 -07002546 /* debug only, don't abort if it fails */
2547 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002548
Minchan Kim48b48002016-07-26 15:23:31 -07002549 if (zs_register_migration(pool))
2550 goto err;
2551
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002552 /*
2553 * Not critical, we still can use the pool
2554 * and user can trigger compaction manually.
2555 */
2556 if (zs_register_shrinker(pool) == 0)
2557 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002558 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002559
2560err:
2561 zs_destroy_pool(pool);
2562 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002563}
2564EXPORT_SYMBOL_GPL(zs_create_pool);
2565
2566void zs_destroy_pool(struct zs_pool *pool)
2567{
2568 int i;
2569
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002570 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002571 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002572 zs_pool_stat_destroy(pool);
2573
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002574 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002575 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002576 struct size_class *class = pool->size_class[i];
2577
2578 if (!class)
2579 continue;
2580
2581 if (class->index != i)
2582 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002583
Minchan Kim48b48002016-07-26 15:23:31 -07002584 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002585 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002586 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002587 class->size, fg);
2588 }
2589 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002590 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002591 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002592
Minchan Kim37836892016-07-26 15:23:23 -07002593 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002594 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002595 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002596 kfree(pool);
2597}
2598EXPORT_SYMBOL_GPL(zs_destroy_pool);
2599
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002600static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002601{
Minchan Kim48b48002016-07-26 15:23:31 -07002602 int ret;
2603
2604 ret = zsmalloc_mount();
2605 if (ret)
2606 goto out;
2607
2608 ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002609
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002610 if (ret)
2611 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002612
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002613 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002614
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002615#ifdef CONFIG_ZPOOL
2616 zpool_register_driver(&zs_zpool_driver);
2617#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002618
Dan Streetman4abaac92016-05-26 15:16:27 -07002619 zs_stat_init();
2620
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002621 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002622
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002623notifier_fail:
2624 zs_unregister_cpu_notifier();
Minchan Kim48b48002016-07-26 15:23:31 -07002625 zsmalloc_unmount();
2626out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002627 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002628}
Nitin Gupta61989a82012-01-09 16:51:56 -06002629
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002630static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002631{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002632#ifdef CONFIG_ZPOOL
2633 zpool_unregister_driver(&zs_zpool_driver);
2634#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002635 zsmalloc_unmount();
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002636 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002637
2638 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002639}
Ben Hutchings069f1012012-06-20 02:31:11 +01002640
2641module_init(zs_init);
2642module_exit(zs_exit);
2643
2644MODULE_LICENSE("Dual BSD/GPL");
2645MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");