blob: 57fbb7ced69f3a47111db0f665ec05b26e4186db [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
Xishi Qiu399d8ee2017-02-22 15:45:01 -080027 * PG_owner_priv_1: identifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Nitin Gupta61989a82012-01-09 16:51:56 -060033#include <linux/module.h>
34#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070035#include <linux/sched.h>
Ingo Molnar50d34392017-02-05 16:03:58 +010036#include <linux/magic.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060037#include <linux/bitops.h>
38#include <linux/errno.h>
39#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/string.h>
41#include <linux/slab.h>
42#include <asm/tlbflush.h>
43#include <asm/pgtable.h>
44#include <linux/cpumask.h>
45#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060046#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080047#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090048#include <linux/spinlock.h>
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -080049#include <linux/shrinker.h>
Seth Jennings0959c632012-08-08 15:12:17 +090050#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080051#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080052#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070053#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070054#include <linux/mount.h>
David Howells8e9231f2019-03-25 16:38:23 +000055#include <linux/pseudo_fs.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070056#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070057#include <linux/pagemap.h>
Sergey Senozhatskycdc346b2018-01-04 16:18:02 -080058#include <linux/fs.h>
Minchan Kim48b48002016-07-26 15:23:31 -070059
60#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090061
62/*
63 * This must be power of 2 and greater than of equal to sizeof(link_free).
64 * These two conditions ensure that any 'struct link_free' itself doesn't
65 * span more than 1 page which avoids complex case of mapping 2 pages simply
66 * to restore link_free pointer values.
67 */
68#define ZS_ALIGN 8
69
70/*
71 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
72 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
73 */
74#define ZS_MAX_ZSPAGE_ORDER 2
75#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
76
Minchan Kim2e40e162015-04-15 16:15:23 -070077#define ZS_HANDLE_SIZE (sizeof(unsigned long))
78
Seth Jennings0959c632012-08-08 15:12:17 +090079/*
80 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090081 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090082 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070083 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090084 *
85 * This is made more complicated by various memory models and PAE.
86 */
87
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030088#ifndef MAX_POSSIBLE_PHYSMEM_BITS
89#ifdef MAX_PHYSMEM_BITS
90#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS
91#else
Seth Jennings0959c632012-08-08 15:12:17 +090092/*
93 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
94 * be PAGE_SHIFT
95 */
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030096#define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
Seth Jennings0959c632012-08-08 15:12:17 +090097#endif
98#endif
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030099
100#define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -0700101
102/*
103 * Memory for allocating for handle keeps object position by
104 * encoding <page, obj_idx> and the encoded value has a room
105 * in least bit(ie, look at obj_to_location).
106 * We use the bit to synchronize between object access by
107 * user and migration.
108 */
109#define HANDLE_PIN_BIT 0
110
111/*
112 * Head in allocated object should have OBJ_ALLOCATED_TAG
113 * to identify the object was allocated or not.
114 * It's okay to add the status bit in the least bit because
115 * header keeps handle which is 4byte-aligned address so we
116 * have room for two bit at least.
117 */
118#define OBJ_ALLOCATED_TAG 1
119#define OBJ_TAG_BITS 1
120#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900121#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
122
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700123#define FULLNESS_BITS 2
124#define CLASS_BITS 8
125#define ISOLATED_BITS 3
126#define MAGIC_VAL_BITS 8
127
Seth Jennings0959c632012-08-08 15:12:17 +0900128#define MAX(a, b) ((a) >= (b) ? (a) : (b))
129/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
130#define ZS_MIN_ALLOC_SIZE \
131 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700132/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700133#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900134
135/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700136 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900137 * trader-off here:
138 * - Large number of size classes is potentially wasteful as free page are
139 * spread across these classes
140 * - Small number of size classes causes large internal fragmentation
141 * - Probably its better to use specific size classes (empirically
142 * determined). NOTE: all those class sizes must be set as multiple of
143 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
144 *
145 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
146 * (reason above)
147 */
Minchan Kim37836892016-07-26 15:23:23 -0700148#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700149#define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
150 ZS_SIZE_CLASS_DELTA) + 1)
Seth Jennings0959c632012-08-08 15:12:17 +0900151
Seth Jennings0959c632012-08-08 15:12:17 +0900152enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900153 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700154 ZS_ALMOST_EMPTY,
155 ZS_ALMOST_FULL,
156 ZS_FULL,
157 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900158};
159
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800160enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700161 CLASS_EMPTY,
162 CLASS_ALMOST_EMPTY,
163 CLASS_ALMOST_FULL,
164 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800165 OBJ_ALLOCATED,
166 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700167 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800168};
169
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800170struct zs_size_stat {
171 unsigned long objs[NR_ZS_STAT_TYPE];
172};
173
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700174#ifdef CONFIG_ZSMALLOC_STAT
175static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800176#endif
177
Minchan Kim48b48002016-07-26 15:23:31 -0700178#ifdef CONFIG_COMPACTION
179static struct vfsmount *zsmalloc_mnt;
180#endif
181
Seth Jennings0959c632012-08-08 15:12:17 +0900182/*
183 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
184 * n <= N / f, where
185 * n = number of allocated objects
186 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700187 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900188 *
189 * Similarly, we assign zspage to:
190 * ZS_ALMOST_FULL when n > N / f
191 * ZS_EMPTY when n == 0
192 * ZS_FULL when n == N
193 *
194 * (see: fix_fullness_group())
195 */
196static const int fullness_threshold_frac = 4;
Sergey Senozhatsky010b4952018-04-05 16:24:43 -0700197static size_t huge_class_size;
Seth Jennings0959c632012-08-08 15:12:17 +0900198
199struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700200 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700201 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900202 /*
203 * Size of objects stored in this class. Must be multiple
204 * of ZS_ALIGN.
205 */
206 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700207 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800208 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
209 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700210
211 unsigned int index;
212 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900213};
214
Minchan Kim48b48002016-07-26 15:23:31 -0700215/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
216static void SetPageHugeObject(struct page *page)
217{
218 SetPageOwnerPriv1(page);
219}
220
221static void ClearPageHugeObject(struct page *page)
222{
223 ClearPageOwnerPriv1(page);
224}
225
226static int PageHugeObject(struct page *page)
227{
228 return PageOwnerPriv1(page);
229}
230
Seth Jennings0959c632012-08-08 15:12:17 +0900231/*
232 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700233 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900234 *
235 * This must be power of 2 and less than or equal to ZS_ALIGN
236 */
237struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700238 union {
239 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700240 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700241 * It's valid for non-allocated object
242 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700243 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700244 /*
245 * Handle of allocated object.
246 */
247 unsigned long handle;
248 };
Seth Jennings0959c632012-08-08 15:12:17 +0900249};
250
251struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800252 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800253
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700254 struct size_class *size_class[ZS_SIZE_CLASSES];
Minchan Kim2e40e162015-04-15 16:15:23 -0700255 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700256 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900257
Minchan Kim13de8932014-10-09 15:29:48 -0700258 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800259
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700260 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700261
262 /* Compact classes */
263 struct shrinker shrinker;
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -0800264
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800265#ifdef CONFIG_ZSMALLOC_STAT
266 struct dentry *stat_dentry;
267#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700268#ifdef CONFIG_COMPACTION
269 struct inode *inode;
270 struct work_struct free_work;
271#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900272};
Nitin Gupta61989a82012-01-09 16:51:56 -0600273
Minchan Kim37836892016-07-26 15:23:23 -0700274struct zspage {
275 struct {
276 unsigned int fullness:FULLNESS_BITS;
Minchan Kim85d492f2017-04-13 14:56:40 -0700277 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700278 unsigned int isolated:ISOLATED_BITS;
279 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700280 };
281 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700282 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700283 struct page *first_page;
284 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700285#ifdef CONFIG_COMPACTION
286 rwlock_t lock;
287#endif
Minchan Kim37836892016-07-26 15:23:23 -0700288};
Nitin Gupta61989a82012-01-09 16:51:56 -0600289
Seth Jenningsf5536462012-07-18 11:55:56 -0500290struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900291#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500292 struct vm_struct *vm; /* vm area for mapping object that span pages */
293#else
294 char *vm_buf; /* copy buffer for objects that span pages */
295#endif
296 char *vm_addr; /* address of kmap_atomic()'ed pages */
297 enum zs_mapmode vm_mm; /* mapping mode */
298};
299
Minchan Kim48b48002016-07-26 15:23:31 -0700300#ifdef CONFIG_COMPACTION
301static int zs_register_migration(struct zs_pool *pool);
302static void zs_unregister_migration(struct zs_pool *pool);
303static void migrate_lock_init(struct zspage *zspage);
304static void migrate_read_lock(struct zspage *zspage);
305static void migrate_read_unlock(struct zspage *zspage);
306static void kick_deferred_free(struct zs_pool *pool);
307static void init_deferred_free(struct zs_pool *pool);
308static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
309#else
310static int zsmalloc_mount(void) { return 0; }
311static void zsmalloc_unmount(void) {}
312static int zs_register_migration(struct zs_pool *pool) { return 0; }
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#endif
321
Minchan Kim37836892016-07-26 15:23:23 -0700322static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700323{
324 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
325 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700326 if (!pool->handle_cachep)
327 return 1;
328
329 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
330 0, 0, NULL);
331 if (!pool->zspage_cachep) {
332 kmem_cache_destroy(pool->handle_cachep);
333 pool->handle_cachep = NULL;
334 return 1;
335 }
336
337 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700338}
339
Minchan Kim37836892016-07-26 15:23:23 -0700340static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700341{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700342 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700343 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700344}
345
Minchan Kim37836892016-07-26 15:23:23 -0700346static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700347{
348 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700349 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700350}
351
Minchan Kim37836892016-07-26 15:23:23 -0700352static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700353{
354 kmem_cache_free(pool->handle_cachep, (void *)handle);
355}
356
Minchan Kim37836892016-07-26 15:23:23 -0700357static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
358{
Minchan Kim48b48002016-07-26 15:23:31 -0700359 return kmem_cache_alloc(pool->zspage_cachep,
360 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800361}
Minchan Kim37836892016-07-26 15:23:23 -0700362
363static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
364{
365 kmem_cache_free(pool->zspage_cachep, zspage);
366}
367
Minchan Kim2e40e162015-04-15 16:15:23 -0700368static void record_obj(unsigned long handle, unsigned long obj)
369{
Junil Leec102f07c2016-01-20 14:58:18 -0800370 /*
371 * lsb of @obj represents handle lock while other bits
372 * represent object value the handle is pointing so
373 * updating shouldn't do store tearing.
374 */
375 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700376}
377
Dan Streetmanc7957792014-08-06 16:08:38 -0700378/* zpool driver */
379
380#ifdef CONFIG_ZPOOL
381
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800382static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700383 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700384 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700385{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700386 /*
387 * Ignore global gfp flags: zs_malloc() may be invoked from
388 * different contexts and its caller must provide a valid
389 * gfp mask.
390 */
391 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700392}
393
394static void zs_zpool_destroy(void *pool)
395{
396 zs_destroy_pool(pool);
397}
398
399static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
400 unsigned long *handle)
401{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700402 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700403 return *handle ? 0 : -1;
404}
405static void zs_zpool_free(void *pool, unsigned long handle)
406{
407 zs_free(pool, handle);
408}
409
Dan Streetmanc7957792014-08-06 16:08:38 -0700410static void *zs_zpool_map(void *pool, unsigned long handle,
411 enum zpool_mapmode mm)
412{
413 enum zs_mapmode zs_mm;
414
415 switch (mm) {
416 case ZPOOL_MM_RO:
417 zs_mm = ZS_MM_RO;
418 break;
419 case ZPOOL_MM_WO:
420 zs_mm = ZS_MM_WO;
421 break;
Gustavo A. R. Silva61855f02018-10-26 15:09:20 -0700422 case ZPOOL_MM_RW: /* fall through */
Dan Streetmanc7957792014-08-06 16:08:38 -0700423 default:
424 zs_mm = ZS_MM_RW;
425 break;
426 }
427
428 return zs_map_object(pool, handle, zs_mm);
429}
430static void zs_zpool_unmap(void *pool, unsigned long handle)
431{
432 zs_unmap_object(pool, handle);
433}
434
435static u64 zs_zpool_total_size(void *pool)
436{
Minchan Kim722cdc12014-10-09 15:29:50 -0700437 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700438}
439
440static struct zpool_driver zs_zpool_driver = {
441 .type = "zsmalloc",
442 .owner = THIS_MODULE,
443 .create = zs_zpool_create,
444 .destroy = zs_zpool_destroy,
445 .malloc = zs_zpool_malloc,
446 .free = zs_zpool_free,
Dan Streetmanc7957792014-08-06 16:08:38 -0700447 .map = zs_zpool_map,
448 .unmap = zs_zpool_unmap,
449 .total_size = zs_zpool_total_size,
450};
451
Kees Cook137f8cf2014-08-29 15:18:40 -0700452MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700453#endif /* CONFIG_ZPOOL */
454
Nitin Gupta61989a82012-01-09 16:51:56 -0600455/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
456static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
457
Minchan Kim48b48002016-07-26 15:23:31 -0700458static bool is_zspage_isolated(struct zspage *zspage)
459{
460 return zspage->isolated;
461}
462
Nick Desaulniers3457f412017-07-10 15:47:26 -0700463static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600464{
Minchan Kima27545bf2012-04-25 15:23:09 +0900465 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600466}
467
Minchan Kim48b48002016-07-26 15:23:31 -0700468/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700469static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600470{
Minchan Kim37836892016-07-26 15:23:23 -0700471 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600472}
473
Minchan Kim37836892016-07-26 15:23:23 -0700474static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700475{
Minchan Kim37836892016-07-26 15:23:23 -0700476 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700477}
478
Minchan Kim37836892016-07-26 15:23:23 -0700479static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700480{
Minchan Kim37836892016-07-26 15:23:23 -0700481 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700482}
483
Minchan Kim48b48002016-07-26 15:23:31 -0700484static inline struct page *get_first_page(struct zspage *zspage)
485{
486 struct page *first_page = zspage->first_page;
487
488 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
489 return first_page;
490}
491
Minchan Kim4f420472016-07-26 15:23:17 -0700492static inline int get_first_obj_offset(struct page *page)
493{
Minchan Kim48b48002016-07-26 15:23:31 -0700494 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700495}
496
497static inline void set_first_obj_offset(struct page *page, int offset)
498{
Minchan Kim48b48002016-07-26 15:23:31 -0700499 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700500}
501
Minchan Kimbfd093f2016-07-26 15:23:28 -0700502static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700503{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700504 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700505}
506
Minchan Kimbfd093f2016-07-26 15:23:28 -0700507static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700508{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700509 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700510}
511
Minchan Kim37836892016-07-26 15:23:23 -0700512static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700513 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600514 enum fullness_group *fullness)
515{
Minchan Kim48b48002016-07-26 15:23:31 -0700516 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
517
Minchan Kim37836892016-07-26 15:23:23 -0700518 *fullness = zspage->fullness;
519 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600520}
521
Minchan Kim37836892016-07-26 15:23:23 -0700522static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700523 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600524 enum fullness_group fullness)
525{
Minchan Kim37836892016-07-26 15:23:23 -0700526 zspage->class = class_idx;
527 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600528}
529
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900530/*
531 * zsmalloc divides the pool into various size classes where each
532 * class maintains a list of zspages where each zspage is divided
533 * into equal sized chunks. Each allocation falls into one of these
534 * classes depending on its size. This function returns index of the
535 * size class which has chunk size big enough to hold the give size.
536 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600537static int get_size_class_index(int size)
538{
539 int idx = 0;
540
541 if (likely(size > ZS_MIN_ALLOC_SIZE))
542 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
543 ZS_SIZE_CLASS_DELTA);
544
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700545 return min_t(int, ZS_SIZE_CLASSES - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600546}
547
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700548/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700549static inline void zs_stat_inc(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700550 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700551{
Minchan Kim48b48002016-07-26 15:23:31 -0700552 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700553}
554
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700555/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700556static inline void zs_stat_dec(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700557 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700558{
Minchan Kim48b48002016-07-26 15:23:31 -0700559 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700560}
561
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700562/* type can be of enum type zs_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700563static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700564 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700565{
Minchan Kim48b48002016-07-26 15:23:31 -0700566 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700567}
568
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700569#ifdef CONFIG_ZSMALLOC_STAT
570
Dan Streetman4abaac92016-05-26 15:16:27 -0700571static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700572{
Dan Streetman4abaac92016-05-26 15:16:27 -0700573 if (!debugfs_initialized()) {
574 pr_warn("debugfs not available, stat dir not created\n");
575 return;
576 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700577
578 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700579}
580
581static void __exit zs_stat_exit(void)
582{
583 debugfs_remove_recursive(zs_stat_root);
584}
585
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700586static unsigned long zs_can_compact(struct size_class *class);
587
Minchan Kim248ca1b2015-04-15 16:15:42 -0700588static int zs_stats_size_show(struct seq_file *s, void *v)
589{
590 int i;
591 struct zs_pool *pool = s->private;
592 struct size_class *class;
593 int objs_per_zspage;
594 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700595 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700596 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
597 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700598 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700599
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700600 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601 "class", "size", "almost_full", "almost_empty",
602 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700603 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700604
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700605 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim248ca1b2015-04-15 16:15:42 -0700606 class = pool->size_class[i];
607
608 if (class->index != i)
609 continue;
610
611 spin_lock(&class->lock);
612 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
613 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
614 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
615 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700616 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700617 spin_unlock(&class->lock);
618
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700619 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700620 pages_used = obj_allocated / objs_per_zspage *
621 class->pages_per_zspage;
622
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700623 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
624 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700625 i, class->size, class_almost_full, class_almost_empty,
626 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700627 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700628
629 total_class_almost_full += class_almost_full;
630 total_class_almost_empty += class_almost_empty;
631 total_objs += obj_allocated;
632 total_used_objs += obj_used;
633 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700634 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700635 }
636
637 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700638 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700639 "Total", "", total_class_almost_full,
640 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700641 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700642
643 return 0;
644}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700645DEFINE_SHOW_ATTRIBUTE(zs_stats_size);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700646
Dan Streetmand34f6152016-05-20 16:59:56 -0700647static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648{
Dan Streetman4abaac92016-05-26 15:16:27 -0700649 if (!zs_stat_root) {
650 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700651 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700652 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700653
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100654 pool->stat_dentry = debugfs_create_dir(name, zs_stat_root);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700655
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100656 debugfs_create_file("classes", S_IFREG | 0444, pool->stat_dentry, pool,
657 &zs_stats_size_fops);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700658}
659
660static void zs_pool_stat_destroy(struct zs_pool *pool)
661{
662 debugfs_remove_recursive(pool->stat_dentry);
663}
664
665#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700666static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700667{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700668}
669
670static void __exit zs_stat_exit(void)
671{
672}
673
Dan Streetmand34f6152016-05-20 16:59:56 -0700674static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700675{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700676}
677
678static inline void zs_pool_stat_destroy(struct zs_pool *pool)
679{
680}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700681#endif
682
Minchan Kim48b48002016-07-26 15:23:31 -0700683
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900684/*
685 * For each size class, zspages are divided into different groups
686 * depending on how "full" they are. This was done so that we could
687 * easily find empty or nearly empty zspages when we try to shrink
688 * the pool (not yet implemented). This function returns fullness
689 * status of the given page.
690 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700691static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700692 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600693{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700694 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600695 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700696
Minchan Kim37836892016-07-26 15:23:23 -0700697 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700698 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600699
700 if (inuse == 0)
701 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700702 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600703 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700704 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600705 fg = ZS_ALMOST_EMPTY;
706 else
707 fg = ZS_ALMOST_FULL;
708
709 return fg;
710}
711
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900712/*
713 * Each size class maintains various freelists and zspages are assigned
714 * to one of these freelists based on the number of live objects they
715 * have. This functions inserts the given zspage into the freelist
716 * identified by <class, fullness_group>.
717 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700718static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700719 struct zspage *zspage,
720 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600721{
Minchan Kim37836892016-07-26 15:23:23 -0700722 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600723
Minchan Kim48b48002016-07-26 15:23:31 -0700724 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700725 head = list_first_entry_or_null(&class->fullness_list[fullness],
726 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700727 /*
Minchan Kim37836892016-07-26 15:23:23 -0700728 * We want to see more ZS_FULL pages and less almost empty/full.
729 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700730 */
Minchan Kim37836892016-07-26 15:23:23 -0700731 if (head) {
732 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
733 list_add(&zspage->list, &head->list);
734 return;
735 }
736 }
737 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600738}
739
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900740/*
741 * This function removes the given zspage from the freelist identified
742 * by <class, fullness_group>.
743 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700744static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700745 struct zspage *zspage,
746 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600747{
Minchan Kim37836892016-07-26 15:23:23 -0700748 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700749 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600750
Minchan Kim37836892016-07-26 15:23:23 -0700751 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700752 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600753}
754
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900755/*
756 * Each size class maintains zspages in different fullness groups depending
757 * on the number of live objects they contain. When allocating or freeing
758 * objects, the fullness status of the page can change, say, from ALMOST_FULL
759 * to ALMOST_EMPTY when freeing an object. This function checks if such
760 * a status change has occurred for the given page and accordingly moves the
761 * page from the freelist of the old fullness group to that of the new
762 * fullness group.
763 */
Minchan Kimc7806262015-04-15 16:15:26 -0700764static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700765 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600766{
767 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600768 enum fullness_group currfg, newfg;
769
Minchan Kim37836892016-07-26 15:23:23 -0700770 get_zspage_mapping(zspage, &class_idx, &currfg);
771 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600772 if (newfg == currfg)
773 goto out;
774
Minchan Kim48b48002016-07-26 15:23:31 -0700775 if (!is_zspage_isolated(zspage)) {
776 remove_zspage(class, zspage, currfg);
777 insert_zspage(class, zspage, newfg);
778 }
779
Minchan Kim37836892016-07-26 15:23:23 -0700780 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600781
782out:
783 return newfg;
784}
785
786/*
787 * We have to decide on how many pages to link together
788 * to form a zspage for each size class. This is important
789 * to reduce wastage due to unusable space left at end of
790 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700791 * wastage = Zp % class_size
792 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600793 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
794 *
795 * For example, for size class of 3/8 * PAGE_SIZE, we should
796 * link together 3 PAGE_SIZE sized pages to form a zspage
797 * since then we can perfectly fit in 8 such objects.
798 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900799static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600800{
801 int i, max_usedpc = 0;
802 /* zspage order which gives maximum used size per KB */
803 int max_usedpc_order = 1;
804
Seth Jennings84d4faa2012-03-05 11:33:21 -0600805 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600806 int zspage_size;
807 int waste, usedpc;
808
809 zspage_size = i * PAGE_SIZE;
810 waste = zspage_size % class_size;
811 usedpc = (zspage_size - waste) * 100 / zspage_size;
812
813 if (usedpc > max_usedpc) {
814 max_usedpc = usedpc;
815 max_usedpc_order = i;
816 }
817 }
818
819 return max_usedpc_order;
820}
821
Minchan Kim37836892016-07-26 15:23:23 -0700822static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600823{
Minchan Kim48b48002016-07-26 15:23:31 -0700824 struct zspage *zspage = (struct zspage *)page->private;
825
826 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
827 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600828}
829
830static struct page *get_next_page(struct page *page)
831{
Minchan Kim48b48002016-07-26 15:23:31 -0700832 if (unlikely(PageHugeObject(page)))
833 return NULL;
834
835 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600836}
837
Minchan Kimbfd093f2016-07-26 15:23:28 -0700838/**
839 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700840 * @obj: the encoded object value
Minchan Kimbfd093f2016-07-26 15:23:28 -0700841 * @page: page object resides in zspage
842 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800843 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700844static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700845 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600846{
Minchan Kim312fcae2015-04-15 16:15:30 -0700847 obj >>= OBJ_TAG_BITS;
848 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
849 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600850}
851
Minchan Kimbfd093f2016-07-26 15:23:28 -0700852/**
853 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
854 * @page: page object resides in zspage
855 * @obj_idx: object index
856 */
857static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
858{
859 unsigned long obj;
860
861 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
862 obj |= obj_idx & OBJ_INDEX_MASK;
863 obj <<= OBJ_TAG_BITS;
864
865 return obj;
866}
867
Minchan Kim2e40e162015-04-15 16:15:23 -0700868static unsigned long handle_to_obj(unsigned long handle)
869{
870 return *(unsigned long *)handle;
871}
872
Minchan Kim48b48002016-07-26 15:23:31 -0700873static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700874{
Minchan Kim48b48002016-07-26 15:23:31 -0700875 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700876 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700877 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700878 } else
879 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700880}
881
Minchan Kim48b48002016-07-26 15:23:31 -0700882static inline int testpin_tag(unsigned long handle)
883{
884 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
885}
886
Minchan Kim312fcae2015-04-15 16:15:30 -0700887static inline int trypin_tag(unsigned long handle)
888{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700889 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700890}
891
892static void pin_tag(unsigned long handle)
893{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700894 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700895}
896
897static void unpin_tag(unsigned long handle)
898{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700899 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700900}
901
Nitin Guptaf4477e92012-04-02 09:13:56 -0500902static void reset_page(struct page *page)
903{
Minchan Kim48b48002016-07-26 15:23:31 -0700904 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700905 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500906 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700907 page_mapcount_reset(page);
908 ClearPageHugeObject(page);
909 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500910}
911
Colin Ian King4d0a5402018-08-17 15:46:50 -0700912static int trylock_zspage(struct zspage *zspage)
Minchan Kim48b48002016-07-26 15:23:31 -0700913{
914 struct page *cursor, *fail;
915
916 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
917 get_next_page(cursor)) {
918 if (!trylock_page(cursor)) {
919 fail = cursor;
920 goto unlock;
921 }
922 }
923
924 return 1;
925unlock:
926 for (cursor = get_first_page(zspage); cursor != fail; cursor =
927 get_next_page(cursor))
928 unlock_page(cursor);
929
930 return 0;
931}
932
933static void __free_zspage(struct zs_pool *pool, struct size_class *class,
934 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600935{
Minchan Kim37836892016-07-26 15:23:23 -0700936 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700937 enum fullness_group fg;
938 unsigned int class_idx;
939
940 get_zspage_mapping(zspage, &class_idx, &fg);
941
942 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600943
Minchan Kim37836892016-07-26 15:23:23 -0700944 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700945 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600946
Minchan Kim48b48002016-07-26 15:23:31 -0700947 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700948 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700949 VM_BUG_ON_PAGE(!PageLocked(page), page);
950 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700951 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700952 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -0700953 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -0700954 put_page(page);
955 page = next;
956 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -0600957
Minchan Kim37836892016-07-26 15:23:23 -0700958 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700959
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700960 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700961 atomic_long_sub(class->pages_per_zspage,
962 &pool->pages_allocated);
963}
964
965static void free_zspage(struct zs_pool *pool, struct size_class *class,
966 struct zspage *zspage)
967{
968 VM_BUG_ON(get_zspage_inuse(zspage));
969 VM_BUG_ON(list_empty(&zspage->list));
970
971 if (!trylock_zspage(zspage)) {
972 kick_deferred_free(pool);
973 return;
974 }
975
976 remove_zspage(class, zspage, ZS_EMPTY);
977 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600978}
979
980/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -0700981static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600982{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700983 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600984 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -0700985 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -0700986
Nitin Gupta61989a82012-01-09 16:51:56 -0600987 while (page) {
988 struct page *next_page;
989 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800990 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600991
Minchan Kim37836892016-07-26 15:23:23 -0700992 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -0600993
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800994 vaddr = kmap_atomic(page);
995 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600996
Dan Streetman5538c562014-10-09 15:30:01 -0700997 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -0700998 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -0700999 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001000 }
1001
1002 /*
1003 * We now come to the last (full or partial) object on this
1004 * page, which must point to the first object on the next
1005 * page (if present)
1006 */
1007 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001008 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001009 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001010 } else {
1011 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001012 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001013 * whether it's allocated object or not.
1014 */
Nick Desaulniers01a6ad92018-01-31 16:20:15 -08001015 link->next = -1UL << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001016 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001017 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001018 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001019 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001020 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001021
Minchan Kimbfd093f2016-07-26 15:23:28 -07001022 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001023}
1024
Minchan Kim48b48002016-07-26 15:23:31 -07001025static void create_page_chain(struct size_class *class, struct zspage *zspage,
1026 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001027{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001028 int i;
1029 struct page *page;
1030 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001031 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001032
1033 /*
1034 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001035 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001036 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001037 *
Minchan Kim37836892016-07-26 15:23:23 -07001038 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001039 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001040 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001041 for (i = 0; i < nr_pages; i++) {
1042 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001043 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001044 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001045 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001046 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001047 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001048 if (unlikely(class->objs_per_zspage == 1 &&
1049 class->pages_per_zspage == 1))
1050 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001051 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001052 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001053 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001054 prev_page = page;
1055 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001056}
Nitin Gupta61989a82012-01-09 16:51:56 -06001057
Minchan Kimbdb0af72016-07-26 15:23:20 -07001058/*
1059 * Allocate a zspage for the given size class
1060 */
Minchan Kim37836892016-07-26 15:23:23 -07001061static struct zspage *alloc_zspage(struct zs_pool *pool,
1062 struct size_class *class,
1063 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001064{
1065 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001066 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001067 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1068
1069 if (!zspage)
1070 return NULL;
1071
1072 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001073 zspage->magic = ZSPAGE_MAGIC;
1074 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001075
Minchan Kimbdb0af72016-07-26 15:23:20 -07001076 for (i = 0; i < class->pages_per_zspage; i++) {
1077 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001078
Minchan Kim37836892016-07-26 15:23:23 -07001079 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001080 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001081 while (--i >= 0) {
1082 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001083 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001084 }
Minchan Kim37836892016-07-26 15:23:23 -07001085 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001086 return NULL;
1087 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001088
1089 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001090 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001091 }
1092
Minchan Kim48b48002016-07-26 15:23:31 -07001093 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001094 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001095
Minchan Kim37836892016-07-26 15:23:23 -07001096 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001097}
1098
Minchan Kim37836892016-07-26 15:23:23 -07001099static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001100{
1101 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001102 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001103
Minchan Kim48b48002016-07-26 15:23:31 -07001104 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001105 zspage = list_first_entry_or_null(&class->fullness_list[i],
1106 struct zspage, list);
1107 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001108 break;
1109 }
1110
Minchan Kim37836892016-07-26 15:23:23 -07001111 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001112}
1113
Minchan Kim1b945ae2013-12-11 11:04:36 +09001114#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001115static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -05001116{
Seth Jenningsf5536462012-07-18 11:55:56 -05001117 /*
1118 * Make sure we don't leak memory if a cpu UP notification
1119 * and zs_init() race and both call zs_cpu_up() on the same cpu
1120 */
1121 if (area->vm)
1122 return 0;
1123 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1124 if (!area->vm)
1125 return -ENOMEM;
1126 return 0;
1127}
1128
1129static inline void __zs_cpu_down(struct mapping_area *area)
1130{
1131 if (area->vm)
1132 free_vm_area(area->vm);
1133 area->vm = NULL;
1134}
1135
1136static inline void *__zs_map_object(struct mapping_area *area,
1137 struct page *pages[2], int off, int size)
1138{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001139 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001140 area->vm_addr = area->vm->addr;
1141 return area->vm_addr + off;
1142}
1143
1144static inline void __zs_unmap_object(struct mapping_area *area,
1145 struct page *pages[2], int off, int size)
1146{
1147 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001148
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001149 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001150}
1151
Minchan Kim1b945ae2013-12-11 11:04:36 +09001152#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001153
1154static inline int __zs_cpu_up(struct mapping_area *area)
1155{
1156 /*
1157 * Make sure we don't leak memory if a cpu UP notification
1158 * and zs_init() race and both call zs_cpu_up() on the same cpu
1159 */
1160 if (area->vm_buf)
1161 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001162 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001163 if (!area->vm_buf)
1164 return -ENOMEM;
1165 return 0;
1166}
1167
1168static inline void __zs_cpu_down(struct mapping_area *area)
1169{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001170 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001171 area->vm_buf = NULL;
1172}
1173
1174static void *__zs_map_object(struct mapping_area *area,
1175 struct page *pages[2], int off, int size)
1176{
Seth Jennings5f6019022012-07-02 16:15:49 -05001177 int sizes[2];
1178 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001179 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001180
Seth Jenningsf5536462012-07-18 11:55:56 -05001181 /* disable page faults to match kmap_atomic() return conditions */
1182 pagefault_disable();
1183
1184 /* no read fastpath */
1185 if (area->vm_mm == ZS_MM_WO)
1186 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001187
1188 sizes[0] = PAGE_SIZE - off;
1189 sizes[1] = size - sizes[0];
1190
Seth Jennings5f6019022012-07-02 16:15:49 -05001191 /* copy object to per-cpu buffer */
1192 addr = kmap_atomic(pages[0]);
1193 memcpy(buf, addr + off, sizes[0]);
1194 kunmap_atomic(addr);
1195 addr = kmap_atomic(pages[1]);
1196 memcpy(buf + sizes[0], addr, sizes[1]);
1197 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001198out:
1199 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001200}
1201
Seth Jenningsf5536462012-07-18 11:55:56 -05001202static void __zs_unmap_object(struct mapping_area *area,
1203 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001204{
Seth Jennings5f6019022012-07-02 16:15:49 -05001205 int sizes[2];
1206 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001207 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001208
Seth Jenningsf5536462012-07-18 11:55:56 -05001209 /* no write fastpath */
1210 if (area->vm_mm == ZS_MM_RO)
1211 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001212
Minchan Kim7b60a682015-04-15 16:15:39 -07001213 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001214 buf = buf + ZS_HANDLE_SIZE;
1215 size -= ZS_HANDLE_SIZE;
1216 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001217
Seth Jennings5f6019022012-07-02 16:15:49 -05001218 sizes[0] = PAGE_SIZE - off;
1219 sizes[1] = size - sizes[0];
1220
1221 /* copy per-cpu buffer to object */
1222 addr = kmap_atomic(pages[0]);
1223 memcpy(addr + off, buf, sizes[0]);
1224 kunmap_atomic(addr);
1225 addr = kmap_atomic(pages[1]);
1226 memcpy(addr, buf + sizes[0], sizes[1]);
1227 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001228
1229out:
1230 /* enable page faults to match kunmap_atomic() return conditions */
1231 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001232}
Nitin Gupta61989a82012-01-09 16:51:56 -06001233
Minchan Kim1b945ae2013-12-11 11:04:36 +09001234#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001235
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001236static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001237{
Nitin Gupta61989a82012-01-09 16:51:56 -06001238 struct mapping_area *area;
1239
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001240 area = &per_cpu(zs_map_area, cpu);
1241 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001242}
1243
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001244static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001245{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001246 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001247
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001248 area = &per_cpu(zs_map_area, cpu);
1249 __zs_cpu_down(area);
1250 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001251}
1252
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001253static bool can_merge(struct size_class *prev, int pages_per_zspage,
1254 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001255{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001256 if (prev->pages_per_zspage == pages_per_zspage &&
1257 prev->objs_per_zspage == objs_per_zspage)
1258 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001259
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001260 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001261}
1262
Minchan Kim37836892016-07-26 15:23:23 -07001263static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001264{
Minchan Kim37836892016-07-26 15:23:23 -07001265 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001266}
1267
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001268unsigned long zs_get_total_pages(struct zs_pool *pool)
1269{
1270 return atomic_long_read(&pool->pages_allocated);
1271}
1272EXPORT_SYMBOL_GPL(zs_get_total_pages);
1273
1274/**
1275 * zs_map_object - get address of allocated object from handle.
1276 * @pool: pool from which the object was allocated
1277 * @handle: handle returned from zs_malloc
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001278 * @mm: maping mode to use
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001279 *
1280 * Before using an object allocated from zs_malloc, it must be mapped using
1281 * this function. When done with the object, it must be unmapped using
1282 * zs_unmap_object.
1283 *
1284 * Only one object can be mapped per cpu at a time. There is no protection
1285 * against nested mappings.
1286 *
1287 * This function returns with preemption and page faults disabled.
1288 */
1289void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1290 enum zs_mapmode mm)
1291{
Minchan Kim37836892016-07-26 15:23:23 -07001292 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001293 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001294 unsigned long obj, off;
1295 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001296
1297 unsigned int class_idx;
1298 enum fullness_group fg;
1299 struct size_class *class;
1300 struct mapping_area *area;
1301 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001302 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001303
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001304 /*
1305 * Because we use per-cpu mapping areas shared among the
1306 * pools/users, we can't allow mapping in interrupt context
1307 * because it can corrupt another users mappings.
1308 */
Sergey Senozhatsky1aedcaf2017-11-15 17:34:03 -08001309 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001310
Minchan Kim312fcae2015-04-15 16:15:30 -07001311 /* From now on, migration cannot move the object */
1312 pin_tag(handle);
1313
Minchan Kim2e40e162015-04-15 16:15:23 -07001314 obj = handle_to_obj(handle);
1315 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001316 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001317
1318 /* migration cannot move any subpage in this zspage */
1319 migrate_read_lock(zspage);
1320
Minchan Kim37836892016-07-26 15:23:23 -07001321 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001322 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001323 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001324
1325 area = &get_cpu_var(zs_map_area);
1326 area->vm_mm = mm;
1327 if (off + class->size <= PAGE_SIZE) {
1328 /* this object is contained entirely within a page */
1329 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001330 ret = area->vm_addr + off;
1331 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001332 }
1333
1334 /* this object spans two pages */
1335 pages[0] = page;
1336 pages[1] = get_next_page(page);
1337 BUG_ON(!pages[1]);
1338
Minchan Kim2e40e162015-04-15 16:15:23 -07001339 ret = __zs_map_object(area, pages, off, class->size);
1340out:
Minchan Kim48b48002016-07-26 15:23:31 -07001341 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001342 ret += ZS_HANDLE_SIZE;
1343
1344 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001345}
1346EXPORT_SYMBOL_GPL(zs_map_object);
1347
1348void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1349{
Minchan Kim37836892016-07-26 15:23:23 -07001350 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001351 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001352 unsigned long obj, off;
1353 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001354
1355 unsigned int class_idx;
1356 enum fullness_group fg;
1357 struct size_class *class;
1358 struct mapping_area *area;
1359
Minchan Kim2e40e162015-04-15 16:15:23 -07001360 obj = handle_to_obj(handle);
1361 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001362 zspage = get_zspage(page);
1363 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001364 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001365 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001366
1367 area = this_cpu_ptr(&zs_map_area);
1368 if (off + class->size <= PAGE_SIZE)
1369 kunmap_atomic(area->vm_addr);
1370 else {
1371 struct page *pages[2];
1372
1373 pages[0] = page;
1374 pages[1] = get_next_page(page);
1375 BUG_ON(!pages[1]);
1376
1377 __zs_unmap_object(area, pages, off, class->size);
1378 }
1379 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001380
1381 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001382 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001383}
1384EXPORT_SYMBOL_GPL(zs_unmap_object);
1385
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07001386/**
1387 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1388 * zsmalloc &size_class.
1389 * @pool: zsmalloc pool to use
1390 *
1391 * The function returns the size of the first huge class - any object of equal
1392 * or bigger size will be stored in zspage consisting of a single physical
1393 * page.
1394 *
1395 * Context: Any context.
1396 *
1397 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1398 */
1399size_t zs_huge_class_size(struct zs_pool *pool)
1400{
1401 return huge_class_size;
1402}
1403EXPORT_SYMBOL_GPL(zs_huge_class_size);
1404
Minchan Kim251cbb92016-05-20 16:59:42 -07001405static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001406 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001407{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001408 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001409 unsigned long obj;
1410 struct link_free *link;
1411
1412 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001413 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001414 void *vaddr;
1415
Minchan Kim312fcae2015-04-15 16:15:30 -07001416 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001417 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001418
1419 offset = obj * class->size;
1420 nr_page = offset >> PAGE_SHIFT;
1421 m_offset = offset & ~PAGE_MASK;
1422 m_page = get_first_page(zspage);
1423
1424 for (i = 0; i < nr_page; i++)
1425 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001426
1427 vaddr = kmap_atomic(m_page);
1428 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001429 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001430 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001431 /* record handle in the header of allocated chunk */
1432 link->handle = handle;
1433 else
Minchan Kim37836892016-07-26 15:23:23 -07001434 /* record handle to page->index */
1435 zspage->first_page->index = handle;
1436
Minchan Kimc7806262015-04-15 16:15:26 -07001437 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001438 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001439 zs_stat_inc(class, OBJ_USED, 1);
1440
Minchan Kimbfd093f2016-07-26 15:23:28 -07001441 obj = location_to_obj(m_page, obj);
1442
Minchan Kimc7806262015-04-15 16:15:26 -07001443 return obj;
1444}
1445
1446
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001447/**
1448 * zs_malloc - Allocate block of given size from pool.
1449 * @pool: pool to allocate from
1450 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001451 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001452 *
1453 * On success, handle to the allocated object is returned,
1454 * otherwise 0.
1455 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1456 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001457unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001458{
Minchan Kim2e40e162015-04-15 16:15:23 -07001459 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001460 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001461 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001462 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001463
Minchan Kim7b60a682015-04-15 16:15:39 -07001464 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001465 return 0;
1466
Minchan Kim37836892016-07-26 15:23:23 -07001467 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001468 if (!handle)
1469 return 0;
1470
1471 /* extra space in chunk to keep the handle */
1472 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001473 class = pool->size_class[get_size_class_index(size)];
1474
1475 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001476 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001477 if (likely(zspage)) {
1478 obj = obj_malloc(class, zspage, handle);
1479 /* Now move the zspage to another fullness group, if required */
1480 fix_fullness_group(class, zspage);
1481 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001482 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001483
Minchan Kim48b48002016-07-26 15:23:31 -07001484 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001485 }
1486
Minchan Kim48b48002016-07-26 15:23:31 -07001487 spin_unlock(&class->lock);
1488
1489 zspage = alloc_zspage(pool, class, gfp);
1490 if (!zspage) {
1491 cache_free_handle(pool, handle);
1492 return 0;
1493 }
1494
1495 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001496 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001497 newfg = get_fullness_group(class, zspage);
1498 insert_zspage(class, zspage, newfg);
1499 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001500 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001501 atomic_long_add(class->pages_per_zspage,
1502 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001503 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001504
1505 /* We completely set up zspage so mark them as movable */
1506 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001507 spin_unlock(&class->lock);
1508
Minchan Kim2e40e162015-04-15 16:15:23 -07001509 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001510}
1511EXPORT_SYMBOL_GPL(zs_malloc);
1512
Minchan Kim1ee47162016-05-20 16:59:45 -07001513static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001514{
1515 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001516 struct zspage *zspage;
1517 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001518 unsigned long f_offset;
1519 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001520 void *vaddr;
1521
Minchan Kim312fcae2015-04-15 16:15:30 -07001522 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001523 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001524 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001525 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001526
Minchan Kimc7806262015-04-15 16:15:26 -07001527 vaddr = kmap_atomic(f_page);
1528
1529 /* Insert this object in containing zspage's freelist */
1530 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001531 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001532 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001533 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001534 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001535 zs_stat_dec(class, OBJ_USED, 1);
1536}
1537
1538void zs_free(struct zs_pool *pool, unsigned long handle)
1539{
Minchan Kim37836892016-07-26 15:23:23 -07001540 struct zspage *zspage;
1541 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001542 unsigned long obj;
1543 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001544 int class_idx;
1545 struct size_class *class;
1546 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001547 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548
Minchan Kim2e40e162015-04-15 16:15:23 -07001549 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001550 return;
1551
Minchan Kim312fcae2015-04-15 16:15:30 -07001552 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001553 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001554 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001555 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001556
Minchan Kim48b48002016-07-26 15:23:31 -07001557 migrate_read_lock(zspage);
1558
Minchan Kim37836892016-07-26 15:23:23 -07001559 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001560 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001561
1562 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001563 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001564 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001565 if (fullness != ZS_EMPTY) {
1566 migrate_read_unlock(zspage);
1567 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001568 }
Minchan Kim48b48002016-07-26 15:23:31 -07001569
1570 isolated = is_zspage_isolated(zspage);
1571 migrate_read_unlock(zspage);
1572 /* If zspage is isolated, zs_page_putback will free the zspage */
1573 if (likely(!isolated))
1574 free_zspage(pool, class, zspage);
1575out:
1576
Minchan Kim312fcae2015-04-15 16:15:30 -07001577 spin_unlock(&class->lock);
1578 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001579 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001580}
1581EXPORT_SYMBOL_GPL(zs_free);
1582
Minchan Kim251cbb92016-05-20 16:59:42 -07001583static void zs_object_copy(struct size_class *class, unsigned long dst,
1584 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001585{
1586 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001587 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001588 unsigned long s_off, d_off;
1589 void *s_addr, *d_addr;
1590 int s_size, d_size, size;
1591 int written = 0;
1592
1593 s_size = d_size = class->size;
1594
1595 obj_to_location(src, &s_page, &s_objidx);
1596 obj_to_location(dst, &d_page, &d_objidx);
1597
Minchan Kimbfd093f2016-07-26 15:23:28 -07001598 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1599 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001600
1601 if (s_off + class->size > PAGE_SIZE)
1602 s_size = PAGE_SIZE - s_off;
1603
1604 if (d_off + class->size > PAGE_SIZE)
1605 d_size = PAGE_SIZE - d_off;
1606
1607 s_addr = kmap_atomic(s_page);
1608 d_addr = kmap_atomic(d_page);
1609
1610 while (1) {
1611 size = min(s_size, d_size);
1612 memcpy(d_addr + d_off, s_addr + s_off, size);
1613 written += size;
1614
1615 if (written == class->size)
1616 break;
1617
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001618 s_off += size;
1619 s_size -= size;
1620 d_off += size;
1621 d_size -= size;
1622
1623 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001624 kunmap_atomic(d_addr);
1625 kunmap_atomic(s_addr);
1626 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001627 s_addr = kmap_atomic(s_page);
1628 d_addr = kmap_atomic(d_page);
1629 s_size = class->size - written;
1630 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001631 }
1632
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001633 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001634 kunmap_atomic(d_addr);
1635 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001636 d_addr = kmap_atomic(d_page);
1637 d_size = class->size - written;
1638 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001639 }
1640 }
1641
1642 kunmap_atomic(d_addr);
1643 kunmap_atomic(s_addr);
1644}
1645
1646/*
1647 * Find alloced object in zspage from index object and
1648 * return handle.
1649 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001650static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001651 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001652{
1653 unsigned long head;
1654 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001655 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001656 unsigned long handle = 0;
1657 void *addr = kmap_atomic(page);
1658
Minchan Kim37836892016-07-26 15:23:23 -07001659 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001660 offset += class->size * index;
1661
1662 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001663 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001664 if (head & OBJ_ALLOCATED_TAG) {
1665 handle = head & ~OBJ_ALLOCATED_TAG;
1666 if (trypin_tag(handle))
1667 break;
1668 handle = 0;
1669 }
1670
1671 offset += class->size;
1672 index++;
1673 }
1674
1675 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001676
1677 *obj_idx = index;
1678
Minchan Kim312fcae2015-04-15 16:15:30 -07001679 return handle;
1680}
1681
1682struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001683 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001684 struct page *s_page;
1685 /* Destination page for migration which should be a first page
1686 * of zspage. */
1687 struct page *d_page;
1688 /* Starting object index within @s_page which used for live object
1689 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001690 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001691};
1692
1693static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1694 struct zs_compact_control *cc)
1695{
1696 unsigned long used_obj, free_obj;
1697 unsigned long handle;
1698 struct page *s_page = cc->s_page;
1699 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001700 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001701 int ret = 0;
1702
1703 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001704 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001705 if (!handle) {
1706 s_page = get_next_page(s_page);
1707 if (!s_page)
1708 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001709 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001710 continue;
1711 }
1712
1713 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001714 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001715 unpin_tag(handle);
1716 ret = -ENOMEM;
1717 break;
1718 }
1719
1720 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001721 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001722 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001723 obj_idx++;
Junil Leec102f07c2016-01-20 14:58:18 -08001724 /*
1725 * record_obj updates handle's value to free_obj and it will
1726 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1727 * breaks synchronization using pin_tag(e,g, zs_free) so
1728 * let's keep the lock bit.
1729 */
1730 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001731 record_obj(handle, free_obj);
1732 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001733 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001734 }
1735
1736 /* Remember last position in this iteration */
1737 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001738 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001739
1740 return ret;
1741}
1742
Minchan Kim37836892016-07-26 15:23:23 -07001743static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001744{
1745 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001746 struct zspage *zspage;
1747 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001748
Minchan Kim37836892016-07-26 15:23:23 -07001749 if (!source) {
1750 fg[0] = ZS_ALMOST_FULL;
1751 fg[1] = ZS_ALMOST_EMPTY;
1752 }
1753
1754 for (i = 0; i < 2; i++) {
1755 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1756 struct zspage, list);
1757 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001758 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001759 remove_zspage(class, zspage, fg[i]);
1760 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001761 }
1762 }
1763
Minchan Kim37836892016-07-26 15:23:23 -07001764 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001765}
1766
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001767/*
Minchan Kim37836892016-07-26 15:23:23 -07001768 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001769 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001770 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001771 *
Minchan Kim37836892016-07-26 15:23:23 -07001772 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001773 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001774static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001775 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001776{
Minchan Kim312fcae2015-04-15 16:15:30 -07001777 enum fullness_group fullness;
1778
Minchan Kim48b48002016-07-26 15:23:31 -07001779 VM_BUG_ON(is_zspage_isolated(zspage));
1780
Minchan Kim37836892016-07-26 15:23:23 -07001781 fullness = get_fullness_group(class, zspage);
1782 insert_zspage(class, zspage, fullness);
1783 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001784
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001785 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001786}
1787
Minchan Kim48b48002016-07-26 15:23:31 -07001788#ifdef CONFIG_COMPACTION
Colin Ian King4d0a5402018-08-17 15:46:50 -07001789/*
1790 * To prevent zspage destroy during migration, zspage freeing should
1791 * hold locks of all pages in the zspage.
1792 */
1793static void lock_zspage(struct zspage *zspage)
1794{
1795 struct page *page = get_first_page(zspage);
1796
1797 do {
1798 lock_page(page);
1799 } while ((page = get_next_page(page)) != NULL);
1800}
1801
David Howells8e9231f2019-03-25 16:38:23 +00001802static int zs_init_fs_context(struct fs_context *fc)
Minchan Kim48b48002016-07-26 15:23:31 -07001803{
David Howells8e9231f2019-03-25 16:38:23 +00001804 return init_pseudo(fc, ZSMALLOC_MAGIC) ? 0 : -ENOMEM;
Minchan Kim48b48002016-07-26 15:23:31 -07001805}
1806
1807static struct file_system_type zsmalloc_fs = {
1808 .name = "zsmalloc",
David Howells8e9231f2019-03-25 16:38:23 +00001809 .init_fs_context = zs_init_fs_context,
Minchan Kim48b48002016-07-26 15:23:31 -07001810 .kill_sb = kill_anon_super,
1811};
1812
1813static int zsmalloc_mount(void)
1814{
1815 int ret = 0;
1816
1817 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1818 if (IS_ERR(zsmalloc_mnt))
1819 ret = PTR_ERR(zsmalloc_mnt);
1820
1821 return ret;
1822}
1823
1824static void zsmalloc_unmount(void)
1825{
1826 kern_unmount(zsmalloc_mnt);
1827}
1828
1829static void migrate_lock_init(struct zspage *zspage)
1830{
1831 rwlock_init(&zspage->lock);
1832}
1833
1834static void migrate_read_lock(struct zspage *zspage)
1835{
1836 read_lock(&zspage->lock);
1837}
1838
1839static void migrate_read_unlock(struct zspage *zspage)
1840{
1841 read_unlock(&zspage->lock);
1842}
1843
1844static void migrate_write_lock(struct zspage *zspage)
1845{
1846 write_lock(&zspage->lock);
1847}
1848
1849static void migrate_write_unlock(struct zspage *zspage)
1850{
1851 write_unlock(&zspage->lock);
1852}
1853
1854/* Number of isolated subpage for *page migration* in this zspage */
1855static void inc_zspage_isolation(struct zspage *zspage)
1856{
1857 zspage->isolated++;
1858}
1859
1860static void dec_zspage_isolation(struct zspage *zspage)
1861{
1862 zspage->isolated--;
1863}
1864
1865static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1866 struct page *newpage, struct page *oldpage)
1867{
1868 struct page *page;
1869 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1870 int idx = 0;
1871
1872 page = get_first_page(zspage);
1873 do {
1874 if (page == oldpage)
1875 pages[idx] = newpage;
1876 else
1877 pages[idx] = page;
1878 idx++;
1879 } while ((page = get_next_page(page)) != NULL);
1880
1881 create_page_chain(class, zspage, pages);
1882 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1883 if (unlikely(PageHugeObject(oldpage)))
1884 newpage->index = oldpage->index;
1885 __SetPageMovable(newpage, page_mapping(oldpage));
1886}
1887
Colin Ian King4d0a5402018-08-17 15:46:50 -07001888static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
Minchan Kim48b48002016-07-26 15:23:31 -07001889{
1890 struct zs_pool *pool;
1891 struct size_class *class;
1892 int class_idx;
1893 enum fullness_group fullness;
1894 struct zspage *zspage;
1895 struct address_space *mapping;
1896
1897 /*
1898 * Page is locked so zspage couldn't be destroyed. For detail, look at
1899 * lock_zspage in free_zspage.
1900 */
1901 VM_BUG_ON_PAGE(!PageMovable(page), page);
1902 VM_BUG_ON_PAGE(PageIsolated(page), page);
1903
1904 zspage = get_zspage(page);
1905
1906 /*
1907 * Without class lock, fullness could be stale while class_idx is okay
1908 * because class_idx is constant unless page is freed so we should get
1909 * fullness again under class lock.
1910 */
1911 get_zspage_mapping(zspage, &class_idx, &fullness);
1912 mapping = page_mapping(page);
1913 pool = mapping->private_data;
1914 class = pool->size_class[class_idx];
1915
1916 spin_lock(&class->lock);
1917 if (get_zspage_inuse(zspage) == 0) {
1918 spin_unlock(&class->lock);
1919 return false;
1920 }
1921
1922 /* zspage is isolated for object migration */
1923 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1924 spin_unlock(&class->lock);
1925 return false;
1926 }
1927
1928 /*
1929 * If this is first time isolation for the zspage, isolate zspage from
1930 * size_class to prevent further object allocation from the zspage.
1931 */
1932 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1933 get_zspage_mapping(zspage, &class_idx, &fullness);
1934 remove_zspage(class, zspage, fullness);
1935 }
1936
1937 inc_zspage_isolation(zspage);
1938 spin_unlock(&class->lock);
1939
1940 return true;
1941}
1942
Colin Ian King4d0a5402018-08-17 15:46:50 -07001943static int zs_page_migrate(struct address_space *mapping, struct page *newpage,
Minchan Kim48b48002016-07-26 15:23:31 -07001944 struct page *page, enum migrate_mode mode)
1945{
1946 struct zs_pool *pool;
1947 struct size_class *class;
1948 int class_idx;
1949 enum fullness_group fullness;
1950 struct zspage *zspage;
1951 struct page *dummy;
1952 void *s_addr, *d_addr, *addr;
1953 int offset, pos;
1954 unsigned long handle, head;
1955 unsigned long old_obj, new_obj;
1956 unsigned int obj_idx;
1957 int ret = -EAGAIN;
1958
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001959 /*
1960 * We cannot support the _NO_COPY case here, because copy needs to
1961 * happen under the zs lock, which does not work with
1962 * MIGRATE_SYNC_NO_COPY workflow.
1963 */
1964 if (mode == MIGRATE_SYNC_NO_COPY)
1965 return -EINVAL;
1966
Minchan Kim48b48002016-07-26 15:23:31 -07001967 VM_BUG_ON_PAGE(!PageMovable(page), page);
1968 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1969
1970 zspage = get_zspage(page);
1971
1972 /* Concurrent compactor cannot migrate any subpage in zspage */
1973 migrate_write_lock(zspage);
1974 get_zspage_mapping(zspage, &class_idx, &fullness);
1975 pool = mapping->private_data;
1976 class = pool->size_class[class_idx];
1977 offset = get_first_obj_offset(page);
1978
1979 spin_lock(&class->lock);
1980 if (!get_zspage_inuse(zspage)) {
Hui Zhu77ff4652017-09-06 16:21:08 -07001981 /*
1982 * Set "offset" to end of the page so that every loops
1983 * skips unnecessary object scanning.
1984 */
1985 offset = PAGE_SIZE;
Minchan Kim48b48002016-07-26 15:23:31 -07001986 }
1987
1988 pos = offset;
1989 s_addr = kmap_atomic(page);
1990 while (pos < PAGE_SIZE) {
1991 head = obj_to_head(page, s_addr + pos);
1992 if (head & OBJ_ALLOCATED_TAG) {
1993 handle = head & ~OBJ_ALLOCATED_TAG;
1994 if (!trypin_tag(handle))
1995 goto unpin_objects;
1996 }
1997 pos += class->size;
1998 }
1999
2000 /*
2001 * Here, any user cannot access all objects in the zspage so let's move.
2002 */
2003 d_addr = kmap_atomic(newpage);
2004 memcpy(d_addr, s_addr, PAGE_SIZE);
2005 kunmap_atomic(d_addr);
2006
2007 for (addr = s_addr + offset; addr < s_addr + pos;
2008 addr += class->size) {
2009 head = obj_to_head(page, addr);
2010 if (head & OBJ_ALLOCATED_TAG) {
2011 handle = head & ~OBJ_ALLOCATED_TAG;
2012 if (!testpin_tag(handle))
2013 BUG();
2014
2015 old_obj = handle_to_obj(handle);
2016 obj_to_location(old_obj, &dummy, &obj_idx);
2017 new_obj = (unsigned long)location_to_obj(newpage,
2018 obj_idx);
2019 new_obj |= BIT(HANDLE_PIN_BIT);
2020 record_obj(handle, new_obj);
2021 }
2022 }
2023
2024 replace_sub_page(class, zspage, newpage, page);
2025 get_page(newpage);
2026
2027 dec_zspage_isolation(zspage);
2028
2029 /*
2030 * Page migration is done so let's putback isolated zspage to
2031 * the list if @page is final isolated subpage in the zspage.
2032 */
2033 if (!is_zspage_isolated(zspage))
2034 putback_zspage(class, zspage);
2035
2036 reset_page(page);
2037 put_page(page);
2038 page = newpage;
2039
Minchan Kimdd4123f2016-07-26 15:26:50 -07002040 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002041unpin_objects:
2042 for (addr = s_addr + offset; addr < s_addr + pos;
2043 addr += class->size) {
2044 head = obj_to_head(page, addr);
2045 if (head & OBJ_ALLOCATED_TAG) {
2046 handle = head & ~OBJ_ALLOCATED_TAG;
2047 if (!testpin_tag(handle))
2048 BUG();
2049 unpin_tag(handle);
2050 }
2051 }
2052 kunmap_atomic(s_addr);
Minchan Kim48b48002016-07-26 15:23:31 -07002053 spin_unlock(&class->lock);
2054 migrate_write_unlock(zspage);
2055
2056 return ret;
2057}
2058
Colin Ian King4d0a5402018-08-17 15:46:50 -07002059static void zs_page_putback(struct page *page)
Minchan Kim48b48002016-07-26 15:23:31 -07002060{
2061 struct zs_pool *pool;
2062 struct size_class *class;
2063 int class_idx;
2064 enum fullness_group fg;
2065 struct address_space *mapping;
2066 struct zspage *zspage;
2067
2068 VM_BUG_ON_PAGE(!PageMovable(page), page);
2069 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2070
2071 zspage = get_zspage(page);
2072 get_zspage_mapping(zspage, &class_idx, &fg);
2073 mapping = page_mapping(page);
2074 pool = mapping->private_data;
2075 class = pool->size_class[class_idx];
2076
2077 spin_lock(&class->lock);
2078 dec_zspage_isolation(zspage);
2079 if (!is_zspage_isolated(zspage)) {
2080 fg = putback_zspage(class, zspage);
2081 /*
2082 * Due to page_lock, we cannot free zspage immediately
2083 * so let's defer.
2084 */
2085 if (fg == ZS_EMPTY)
2086 schedule_work(&pool->free_work);
2087 }
2088 spin_unlock(&class->lock);
2089}
2090
Colin Ian King4d0a5402018-08-17 15:46:50 -07002091static const struct address_space_operations zsmalloc_aops = {
Minchan Kim48b48002016-07-26 15:23:31 -07002092 .isolate_page = zs_page_isolate,
2093 .migratepage = zs_page_migrate,
2094 .putback_page = zs_page_putback,
2095};
2096
2097static int zs_register_migration(struct zs_pool *pool)
2098{
2099 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2100 if (IS_ERR(pool->inode)) {
2101 pool->inode = NULL;
2102 return 1;
2103 }
2104
2105 pool->inode->i_mapping->private_data = pool;
2106 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2107 return 0;
2108}
2109
2110static void zs_unregister_migration(struct zs_pool *pool)
2111{
2112 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002113 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002114}
2115
2116/*
2117 * Caller should hold page_lock of all pages in the zspage
2118 * In here, we cannot use zspage meta data.
2119 */
2120static void async_free_zspage(struct work_struct *work)
2121{
2122 int i;
2123 struct size_class *class;
2124 unsigned int class_idx;
2125 enum fullness_group fullness;
2126 struct zspage *zspage, *tmp;
2127 LIST_HEAD(free_pages);
2128 struct zs_pool *pool = container_of(work, struct zs_pool,
2129 free_work);
2130
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002131 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim48b48002016-07-26 15:23:31 -07002132 class = pool->size_class[i];
2133 if (class->index != i)
2134 continue;
2135
2136 spin_lock(&class->lock);
2137 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2138 spin_unlock(&class->lock);
2139 }
2140
2141
2142 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2143 list_del(&zspage->list);
2144 lock_zspage(zspage);
2145
2146 get_zspage_mapping(zspage, &class_idx, &fullness);
2147 VM_BUG_ON(fullness != ZS_EMPTY);
2148 class = pool->size_class[class_idx];
2149 spin_lock(&class->lock);
2150 __free_zspage(pool, pool->size_class[class_idx], zspage);
2151 spin_unlock(&class->lock);
2152 }
2153};
2154
2155static void kick_deferred_free(struct zs_pool *pool)
2156{
2157 schedule_work(&pool->free_work);
2158}
2159
2160static void init_deferred_free(struct zs_pool *pool)
2161{
2162 INIT_WORK(&pool->free_work, async_free_zspage);
2163}
2164
2165static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2166{
2167 struct page *page = get_first_page(zspage);
2168
2169 do {
2170 WARN_ON(!trylock_page(page));
2171 __SetPageMovable(page, pool->inode->i_mapping);
2172 unlock_page(page);
2173 } while ((page = get_next_page(page)) != NULL);
2174}
2175#endif
2176
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002177/*
2178 *
2179 * Based on the number of unused allocated objects calculate
2180 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002181 */
2182static unsigned long zs_can_compact(struct size_class *class)
2183{
2184 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002185 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2186 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002187
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002188 if (obj_allocated <= obj_used)
2189 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002190
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002191 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002192 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002193
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002194 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002195}
2196
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002197static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002198{
Minchan Kim312fcae2015-04-15 16:15:30 -07002199 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002200 struct zspage *src_zspage;
2201 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002202
Minchan Kim312fcae2015-04-15 16:15:30 -07002203 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002204 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002205
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002206 if (!zs_can_compact(class))
2207 break;
2208
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002209 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002210 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002211
Minchan Kim37836892016-07-26 15:23:23 -07002212 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002213 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002214 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002215 * If there is no more space in dst_page, resched
2216 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002217 */
2218 if (!migrate_zspage(pool, class, &cc))
2219 break;
2220
Minchan Kim4aa409c2016-07-26 15:23:26 -07002221 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002222 }
2223
2224 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002225 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002226 break;
2227
Minchan Kim4aa409c2016-07-26 15:23:26 -07002228 putback_zspage(class, dst_zspage);
2229 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002230 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002231 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002232 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002233 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002234 cond_resched();
2235 spin_lock(&class->lock);
2236 }
2237
Minchan Kim37836892016-07-26 15:23:23 -07002238 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002239 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002240
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002241 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002242}
2243
2244unsigned long zs_compact(struct zs_pool *pool)
2245{
2246 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002247 struct size_class *class;
2248
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002249 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002250 class = pool->size_class[i];
2251 if (!class)
2252 continue;
2253 if (class->index != i)
2254 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002255 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002256 }
2257
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002258 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002259}
2260EXPORT_SYMBOL_GPL(zs_compact);
2261
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002262void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2263{
2264 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2265}
2266EXPORT_SYMBOL_GPL(zs_pool_stats);
2267
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002268static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2269 struct shrink_control *sc)
2270{
2271 unsigned long pages_freed;
2272 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2273 shrinker);
2274
2275 pages_freed = pool->stats.pages_compacted;
2276 /*
2277 * Compact classes and calculate compaction delta.
2278 * Can run concurrently with a manually triggered
2279 * (by user) compaction.
2280 */
2281 pages_freed = zs_compact(pool) - pages_freed;
2282
2283 return pages_freed ? pages_freed : SHRINK_STOP;
2284}
2285
2286static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2287 struct shrink_control *sc)
2288{
2289 int i;
2290 struct size_class *class;
2291 unsigned long pages_to_free = 0;
2292 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2293 shrinker);
2294
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002295 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002296 class = pool->size_class[i];
2297 if (!class)
2298 continue;
2299 if (class->index != i)
2300 continue;
2301
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002302 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002303 }
2304
2305 return pages_to_free;
2306}
2307
2308static void zs_unregister_shrinker(struct zs_pool *pool)
2309{
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002310 unregister_shrinker(&pool->shrinker);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002311}
2312
2313static int zs_register_shrinker(struct zs_pool *pool)
2314{
2315 pool->shrinker.scan_objects = zs_shrinker_scan;
2316 pool->shrinker.count_objects = zs_shrinker_count;
2317 pool->shrinker.batch = 0;
2318 pool->shrinker.seeks = DEFAULT_SEEKS;
2319
2320 return register_shrinker(&pool->shrinker);
2321}
2322
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002323/**
2324 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002325 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002326 *
2327 * This function must be called before anything when using
2328 * the zsmalloc allocator.
2329 *
2330 * On success, a pointer to the newly created pool is returned,
2331 * otherwise NULL.
2332 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002333struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002334{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002335 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002336 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002337 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002338
Ganesh Mahendran18136652014-12-12 16:57:10 -08002339 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002340 if (!pool)
2341 return NULL;
2342
Minchan Kim48b48002016-07-26 15:23:31 -07002343 init_deferred_free(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002344
Minchan Kim2e40e162015-04-15 16:15:23 -07002345 pool->name = kstrdup(name, GFP_KERNEL);
2346 if (!pool->name)
2347 goto err;
2348
Minchan Kim37836892016-07-26 15:23:23 -07002349 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002350 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002351
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002352 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002353 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002354 * for merging should be larger or equal to current size.
2355 */
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002356 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002357 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002358 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002359 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002360 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002361 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002362
2363 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2364 if (size > ZS_MAX_ALLOC_SIZE)
2365 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002366 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002367 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002368
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002369 /*
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07002370 * We iterate from biggest down to smallest classes,
2371 * so huge_class_size holds the size of the first huge
2372 * class. Any object bigger than or equal to that will
2373 * endup in the huge class.
2374 */
2375 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2376 !huge_class_size) {
2377 huge_class_size = size;
2378 /*
2379 * The object uses ZS_HANDLE_SIZE bytes to store the
2380 * handle. We need to subtract it, because zs_malloc()
2381 * unconditionally adds handle size before it performs
2382 * size class search - so object may be smaller than
2383 * huge class size, yet it still can end up in the huge
2384 * class because it grows by ZS_HANDLE_SIZE extra bytes
2385 * right before class lookup.
2386 */
2387 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2388 }
2389
2390 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002391 * size_class is used for normal zsmalloc operation such
2392 * as alloc/free for that size. Although it is natural that we
2393 * have one size_class for each size, there is a chance that we
2394 * can get more memory utilization if we use one size_class for
2395 * many different sizes whose size_class have same
2396 * characteristics. So, we makes size_class point to
2397 * previous size_class if possible.
2398 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002399 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002400 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002401 pool->size_class[i] = prev_class;
2402 continue;
2403 }
2404 }
2405
2406 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2407 if (!class)
2408 goto err;
2409
Nitin Gupta61989a82012-01-09 16:51:56 -06002410 class->size = size;
2411 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002412 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002413 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002414 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002415 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002416 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2417 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002418 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002419
2420 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002421 }
2422
Dan Streetmand34f6152016-05-20 16:59:56 -07002423 /* debug only, don't abort if it fails */
2424 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002425
Minchan Kim48b48002016-07-26 15:23:31 -07002426 if (zs_register_migration(pool))
2427 goto err;
2428
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002429 /*
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002430 * Not critical since shrinker is only used to trigger internal
2431 * defragmentation of the pool which is pretty optional thing. If
2432 * registration fails we still can use the pool normally and user can
2433 * trigger compaction manually. Thus, ignore return code.
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002434 */
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002435 zs_register_shrinker(pool);
2436
Nitin Gupta61989a82012-01-09 16:51:56 -06002437 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002438
2439err:
2440 zs_destroy_pool(pool);
2441 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002442}
2443EXPORT_SYMBOL_GPL(zs_create_pool);
2444
2445void zs_destroy_pool(struct zs_pool *pool)
2446{
2447 int i;
2448
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002449 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002450 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002451 zs_pool_stat_destroy(pool);
2452
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002453 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002454 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002455 struct size_class *class = pool->size_class[i];
2456
2457 if (!class)
2458 continue;
2459
2460 if (class->index != i)
2461 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002462
Minchan Kim48b48002016-07-26 15:23:31 -07002463 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002464 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002465 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002466 class->size, fg);
2467 }
2468 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002469 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002470 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002471
Minchan Kim37836892016-07-26 15:23:23 -07002472 destroy_cache(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002473 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002474 kfree(pool);
2475}
2476EXPORT_SYMBOL_GPL(zs_destroy_pool);
2477
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002478static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002479{
Minchan Kim48b48002016-07-26 15:23:31 -07002480 int ret;
2481
2482 ret = zsmalloc_mount();
2483 if (ret)
2484 goto out;
2485
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002486 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2487 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002488 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002489 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002490
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002491#ifdef CONFIG_ZPOOL
2492 zpool_register_driver(&zs_zpool_driver);
2493#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002494
Dan Streetman4abaac92016-05-26 15:16:27 -07002495 zs_stat_init();
2496
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002497 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002498
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002499hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002500 zsmalloc_unmount();
2501out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002502 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002503}
Nitin Gupta61989a82012-01-09 16:51:56 -06002504
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002505static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002506{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002507#ifdef CONFIG_ZPOOL
2508 zpool_unregister_driver(&zs_zpool_driver);
2509#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002510 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002511 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002512
2513 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002514}
Ben Hutchings069f1012012-06-20 02:31:11 +01002515
2516module_init(zs_init);
2517module_exit(zs_exit);
2518
2519MODULE_LICENSE("Dual BSD/GPL");
2520MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");