blob: 910d0407f8f10b8c29f518cb160efe9356f203aa [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);
579 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700580 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700581}
582
583static void __exit zs_stat_exit(void)
584{
585 debugfs_remove_recursive(zs_stat_root);
586}
587
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700588static unsigned long zs_can_compact(struct size_class *class);
589
Minchan Kim248ca1b2015-04-15 16:15:42 -0700590static int zs_stats_size_show(struct seq_file *s, void *v)
591{
592 int i;
593 struct zs_pool *pool = s->private;
594 struct size_class *class;
595 int objs_per_zspage;
596 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700597 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700598 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
599 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700600 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700601
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700602 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700603 "class", "size", "almost_full", "almost_empty",
604 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700605 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700606
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -0700607 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim248ca1b2015-04-15 16:15:42 -0700608 class = pool->size_class[i];
609
610 if (class->index != i)
611 continue;
612
613 spin_lock(&class->lock);
614 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
615 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
616 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
617 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700618 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619 spin_unlock(&class->lock);
620
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700621 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700622 pages_used = obj_allocated / objs_per_zspage *
623 class->pages_per_zspage;
624
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700625 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
626 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700627 i, class->size, class_almost_full, class_almost_empty,
628 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700629 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700630
631 total_class_almost_full += class_almost_full;
632 total_class_almost_empty += class_almost_empty;
633 total_objs += obj_allocated;
634 total_used_objs += obj_used;
635 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700636 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700637 }
638
639 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700640 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700641 "Total", "", total_class_almost_full,
642 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700643 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700644
645 return 0;
646}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700647DEFINE_SHOW_ATTRIBUTE(zs_stats_size);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648
Dan Streetmand34f6152016-05-20 16:59:56 -0700649static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700650{
651 struct dentry *entry;
652
Dan Streetman4abaac92016-05-26 15:16:27 -0700653 if (!zs_stat_root) {
654 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700655 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700656 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700657
658 entry = debugfs_create_dir(name, zs_stat_root);
659 if (!entry) {
660 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700661 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700662 }
663 pool->stat_dentry = entry;
664
Joe Perches0825a6f2018-06-14 15:27:58 -0700665 entry = debugfs_create_file("classes", S_IFREG | 0444,
666 pool->stat_dentry, pool,
667 &zs_stats_size_fops);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700668 if (!entry) {
669 pr_warn("%s: debugfs file entry <%s> creation failed\n",
670 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700671 debugfs_remove_recursive(pool->stat_dentry);
672 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700673 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700674}
675
676static void zs_pool_stat_destroy(struct zs_pool *pool)
677{
678 debugfs_remove_recursive(pool->stat_dentry);
679}
680
681#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700682static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700683{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700684}
685
686static void __exit zs_stat_exit(void)
687{
688}
689
Dan Streetmand34f6152016-05-20 16:59:56 -0700690static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700691{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700692}
693
694static inline void zs_pool_stat_destroy(struct zs_pool *pool)
695{
696}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700697#endif
698
Minchan Kim48b48002016-07-26 15:23:31 -0700699
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900700/*
701 * For each size class, zspages are divided into different groups
702 * depending on how "full" they are. This was done so that we could
703 * easily find empty or nearly empty zspages when we try to shrink
704 * the pool (not yet implemented). This function returns fullness
705 * status of the given page.
706 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700707static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700708 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600709{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700710 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600711 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700712
Minchan Kim37836892016-07-26 15:23:23 -0700713 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700714 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600715
716 if (inuse == 0)
717 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700718 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600719 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700720 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600721 fg = ZS_ALMOST_EMPTY;
722 else
723 fg = ZS_ALMOST_FULL;
724
725 return fg;
726}
727
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900728/*
729 * Each size class maintains various freelists and zspages are assigned
730 * to one of these freelists based on the number of live objects they
731 * have. This functions inserts the given zspage into the freelist
732 * identified by <class, fullness_group>.
733 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700734static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700735 struct zspage *zspage,
736 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600737{
Minchan Kim37836892016-07-26 15:23:23 -0700738 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600739
Minchan Kim48b48002016-07-26 15:23:31 -0700740 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700741 head = list_first_entry_or_null(&class->fullness_list[fullness],
742 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700743 /*
Minchan Kim37836892016-07-26 15:23:23 -0700744 * We want to see more ZS_FULL pages and less almost empty/full.
745 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700746 */
Minchan Kim37836892016-07-26 15:23:23 -0700747 if (head) {
748 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
749 list_add(&zspage->list, &head->list);
750 return;
751 }
752 }
753 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600754}
755
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900756/*
757 * This function removes the given zspage from the freelist identified
758 * by <class, fullness_group>.
759 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700760static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700761 struct zspage *zspage,
762 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600763{
Minchan Kim37836892016-07-26 15:23:23 -0700764 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700765 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600766
Minchan Kim37836892016-07-26 15:23:23 -0700767 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700768 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600769}
770
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900771/*
772 * Each size class maintains zspages in different fullness groups depending
773 * on the number of live objects they contain. When allocating or freeing
774 * objects, the fullness status of the page can change, say, from ALMOST_FULL
775 * to ALMOST_EMPTY when freeing an object. This function checks if such
776 * a status change has occurred for the given page and accordingly moves the
777 * page from the freelist of the old fullness group to that of the new
778 * fullness group.
779 */
Minchan Kimc7806262015-04-15 16:15:26 -0700780static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700781 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600782{
783 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600784 enum fullness_group currfg, newfg;
785
Minchan Kim37836892016-07-26 15:23:23 -0700786 get_zspage_mapping(zspage, &class_idx, &currfg);
787 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600788 if (newfg == currfg)
789 goto out;
790
Minchan Kim48b48002016-07-26 15:23:31 -0700791 if (!is_zspage_isolated(zspage)) {
792 remove_zspage(class, zspage, currfg);
793 insert_zspage(class, zspage, newfg);
794 }
795
Minchan Kim37836892016-07-26 15:23:23 -0700796 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600797
798out:
799 return newfg;
800}
801
802/*
803 * We have to decide on how many pages to link together
804 * to form a zspage for each size class. This is important
805 * to reduce wastage due to unusable space left at end of
806 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700807 * wastage = Zp % class_size
808 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600809 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
810 *
811 * For example, for size class of 3/8 * PAGE_SIZE, we should
812 * link together 3 PAGE_SIZE sized pages to form a zspage
813 * since then we can perfectly fit in 8 such objects.
814 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900815static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600816{
817 int i, max_usedpc = 0;
818 /* zspage order which gives maximum used size per KB */
819 int max_usedpc_order = 1;
820
Seth Jennings84d4faa2012-03-05 11:33:21 -0600821 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600822 int zspage_size;
823 int waste, usedpc;
824
825 zspage_size = i * PAGE_SIZE;
826 waste = zspage_size % class_size;
827 usedpc = (zspage_size - waste) * 100 / zspage_size;
828
829 if (usedpc > max_usedpc) {
830 max_usedpc = usedpc;
831 max_usedpc_order = i;
832 }
833 }
834
835 return max_usedpc_order;
836}
837
Minchan Kim37836892016-07-26 15:23:23 -0700838static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600839{
Minchan Kim48b48002016-07-26 15:23:31 -0700840 struct zspage *zspage = (struct zspage *)page->private;
841
842 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
843 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600844}
845
846static struct page *get_next_page(struct page *page)
847{
Minchan Kim48b48002016-07-26 15:23:31 -0700848 if (unlikely(PageHugeObject(page)))
849 return NULL;
850
851 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600852}
853
Minchan Kimbfd093f2016-07-26 15:23:28 -0700854/**
855 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700856 * @obj: the encoded object value
Minchan Kimbfd093f2016-07-26 15:23:28 -0700857 * @page: page object resides in zspage
858 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800859 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700860static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700861 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600862{
Minchan Kim312fcae2015-04-15 16:15:30 -0700863 obj >>= OBJ_TAG_BITS;
864 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
865 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600866}
867
Minchan Kimbfd093f2016-07-26 15:23:28 -0700868/**
869 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
870 * @page: page object resides in zspage
871 * @obj_idx: object index
872 */
873static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
874{
875 unsigned long obj;
876
877 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
878 obj |= obj_idx & OBJ_INDEX_MASK;
879 obj <<= OBJ_TAG_BITS;
880
881 return obj;
882}
883
Minchan Kim2e40e162015-04-15 16:15:23 -0700884static unsigned long handle_to_obj(unsigned long handle)
885{
886 return *(unsigned long *)handle;
887}
888
Minchan Kim48b48002016-07-26 15:23:31 -0700889static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700890{
Minchan Kim48b48002016-07-26 15:23:31 -0700891 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700892 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700893 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700894 } else
895 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700896}
897
Minchan Kim48b48002016-07-26 15:23:31 -0700898static inline int testpin_tag(unsigned long handle)
899{
900 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
901}
902
Minchan Kim312fcae2015-04-15 16:15:30 -0700903static inline int trypin_tag(unsigned long handle)
904{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700905 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700906}
907
908static void pin_tag(unsigned long handle)
909{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700910 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700911}
912
913static void unpin_tag(unsigned long handle)
914{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700915 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700916}
917
Nitin Guptaf4477e92012-04-02 09:13:56 -0500918static void reset_page(struct page *page)
919{
Minchan Kim48b48002016-07-26 15:23:31 -0700920 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700921 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500922 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700923 page_mapcount_reset(page);
924 ClearPageHugeObject(page);
925 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500926}
927
Colin Ian King4d0a5402018-08-17 15:46:50 -0700928static int trylock_zspage(struct zspage *zspage)
Minchan Kim48b48002016-07-26 15:23:31 -0700929{
930 struct page *cursor, *fail;
931
932 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
933 get_next_page(cursor)) {
934 if (!trylock_page(cursor)) {
935 fail = cursor;
936 goto unlock;
937 }
938 }
939
940 return 1;
941unlock:
942 for (cursor = get_first_page(zspage); cursor != fail; cursor =
943 get_next_page(cursor))
944 unlock_page(cursor);
945
946 return 0;
947}
948
949static void __free_zspage(struct zs_pool *pool, struct size_class *class,
950 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600951{
Minchan Kim37836892016-07-26 15:23:23 -0700952 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700953 enum fullness_group fg;
954 unsigned int class_idx;
955
956 get_zspage_mapping(zspage, &class_idx, &fg);
957
958 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600959
Minchan Kim37836892016-07-26 15:23:23 -0700960 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700961 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600962
Minchan Kim48b48002016-07-26 15:23:31 -0700963 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700964 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700965 VM_BUG_ON_PAGE(!PageLocked(page), page);
966 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700967 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700968 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -0700969 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -0700970 put_page(page);
971 page = next;
972 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -0600973
Minchan Kim37836892016-07-26 15:23:23 -0700974 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700975
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700976 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700977 atomic_long_sub(class->pages_per_zspage,
978 &pool->pages_allocated);
979}
980
981static void free_zspage(struct zs_pool *pool, struct size_class *class,
982 struct zspage *zspage)
983{
984 VM_BUG_ON(get_zspage_inuse(zspage));
985 VM_BUG_ON(list_empty(&zspage->list));
986
987 if (!trylock_zspage(zspage)) {
988 kick_deferred_free(pool);
989 return;
990 }
991
992 remove_zspage(class, zspage, ZS_EMPTY);
993 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600994}
995
996/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -0700997static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600998{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700999 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001000 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001001 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001002
Nitin Gupta61989a82012-01-09 16:51:56 -06001003 while (page) {
1004 struct page *next_page;
1005 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001006 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001007
Minchan Kim37836892016-07-26 15:23:23 -07001008 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001009
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001010 vaddr = kmap_atomic(page);
1011 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001012
Dan Streetman5538c562014-10-09 15:30:01 -07001013 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001014 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001015 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001016 }
1017
1018 /*
1019 * We now come to the last (full or partial) object on this
1020 * page, which must point to the first object on the next
1021 * page (if present)
1022 */
1023 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001024 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001025 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001026 } else {
1027 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001028 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001029 * whether it's allocated object or not.
1030 */
Nick Desaulniers01a6ad92018-01-31 16:20:15 -08001031 link->next = -1UL << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001032 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001033 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001034 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001035 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001036 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001037
Minchan Kimbfd093f2016-07-26 15:23:28 -07001038 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001039}
1040
Minchan Kim48b48002016-07-26 15:23:31 -07001041static void create_page_chain(struct size_class *class, struct zspage *zspage,
1042 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001043{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001044 int i;
1045 struct page *page;
1046 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001047 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001048
1049 /*
1050 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001051 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001052 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001053 *
Minchan Kim37836892016-07-26 15:23:23 -07001054 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001055 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001056 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001057 for (i = 0; i < nr_pages; i++) {
1058 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001059 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001060 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001061 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001062 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001063 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001064 if (unlikely(class->objs_per_zspage == 1 &&
1065 class->pages_per_zspage == 1))
1066 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001067 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001068 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001069 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001070 prev_page = page;
1071 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001072}
Nitin Gupta61989a82012-01-09 16:51:56 -06001073
Minchan Kimbdb0af72016-07-26 15:23:20 -07001074/*
1075 * Allocate a zspage for the given size class
1076 */
Minchan Kim37836892016-07-26 15:23:23 -07001077static struct zspage *alloc_zspage(struct zs_pool *pool,
1078 struct size_class *class,
1079 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001080{
1081 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001082 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001083 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1084
1085 if (!zspage)
1086 return NULL;
1087
1088 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001089 zspage->magic = ZSPAGE_MAGIC;
1090 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001091
Minchan Kimbdb0af72016-07-26 15:23:20 -07001092 for (i = 0; i < class->pages_per_zspage; i++) {
1093 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001094
Minchan Kim37836892016-07-26 15:23:23 -07001095 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001096 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001097 while (--i >= 0) {
1098 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001099 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001100 }
Minchan Kim37836892016-07-26 15:23:23 -07001101 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001102 return NULL;
1103 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001104
1105 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001106 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001107 }
1108
Minchan Kim48b48002016-07-26 15:23:31 -07001109 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001110 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001111
Minchan Kim37836892016-07-26 15:23:23 -07001112 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001113}
1114
Minchan Kim37836892016-07-26 15:23:23 -07001115static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001116{
1117 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001118 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001119
Minchan Kim48b48002016-07-26 15:23:31 -07001120 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001121 zspage = list_first_entry_or_null(&class->fullness_list[i],
1122 struct zspage, list);
1123 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001124 break;
1125 }
1126
Minchan Kim37836892016-07-26 15:23:23 -07001127 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001128}
1129
Minchan Kim1b945ae2013-12-11 11:04:36 +09001130#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001131static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -05001132{
Seth Jenningsf5536462012-07-18 11:55:56 -05001133 /*
1134 * Make sure we don't leak memory if a cpu UP notification
1135 * and zs_init() race and both call zs_cpu_up() on the same cpu
1136 */
1137 if (area->vm)
1138 return 0;
1139 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1140 if (!area->vm)
1141 return -ENOMEM;
1142 return 0;
1143}
1144
1145static inline void __zs_cpu_down(struct mapping_area *area)
1146{
1147 if (area->vm)
1148 free_vm_area(area->vm);
1149 area->vm = NULL;
1150}
1151
1152static inline void *__zs_map_object(struct mapping_area *area,
1153 struct page *pages[2], int off, int size)
1154{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001155 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001156 area->vm_addr = area->vm->addr;
1157 return area->vm_addr + off;
1158}
1159
1160static inline void __zs_unmap_object(struct mapping_area *area,
1161 struct page *pages[2], int off, int size)
1162{
1163 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001164
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001165 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001166}
1167
Minchan Kim1b945ae2013-12-11 11:04:36 +09001168#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001169
1170static inline int __zs_cpu_up(struct mapping_area *area)
1171{
1172 /*
1173 * Make sure we don't leak memory if a cpu UP notification
1174 * and zs_init() race and both call zs_cpu_up() on the same cpu
1175 */
1176 if (area->vm_buf)
1177 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001178 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001179 if (!area->vm_buf)
1180 return -ENOMEM;
1181 return 0;
1182}
1183
1184static inline void __zs_cpu_down(struct mapping_area *area)
1185{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001186 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001187 area->vm_buf = NULL;
1188}
1189
1190static void *__zs_map_object(struct mapping_area *area,
1191 struct page *pages[2], int off, int size)
1192{
Seth Jennings5f6019022012-07-02 16:15:49 -05001193 int sizes[2];
1194 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001195 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001196
Seth Jenningsf5536462012-07-18 11:55:56 -05001197 /* disable page faults to match kmap_atomic() return conditions */
1198 pagefault_disable();
1199
1200 /* no read fastpath */
1201 if (area->vm_mm == ZS_MM_WO)
1202 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001203
1204 sizes[0] = PAGE_SIZE - off;
1205 sizes[1] = size - sizes[0];
1206
Seth Jennings5f6019022012-07-02 16:15:49 -05001207 /* copy object to per-cpu buffer */
1208 addr = kmap_atomic(pages[0]);
1209 memcpy(buf, addr + off, sizes[0]);
1210 kunmap_atomic(addr);
1211 addr = kmap_atomic(pages[1]);
1212 memcpy(buf + sizes[0], addr, sizes[1]);
1213 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001214out:
1215 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001216}
1217
Seth Jenningsf5536462012-07-18 11:55:56 -05001218static void __zs_unmap_object(struct mapping_area *area,
1219 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001220{
Seth Jennings5f6019022012-07-02 16:15:49 -05001221 int sizes[2];
1222 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001223 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001224
Seth Jenningsf5536462012-07-18 11:55:56 -05001225 /* no write fastpath */
1226 if (area->vm_mm == ZS_MM_RO)
1227 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001228
Minchan Kim7b60a682015-04-15 16:15:39 -07001229 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001230 buf = buf + ZS_HANDLE_SIZE;
1231 size -= ZS_HANDLE_SIZE;
1232 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001233
Seth Jennings5f6019022012-07-02 16:15:49 -05001234 sizes[0] = PAGE_SIZE - off;
1235 sizes[1] = size - sizes[0];
1236
1237 /* copy per-cpu buffer to object */
1238 addr = kmap_atomic(pages[0]);
1239 memcpy(addr + off, buf, sizes[0]);
1240 kunmap_atomic(addr);
1241 addr = kmap_atomic(pages[1]);
1242 memcpy(addr, buf + sizes[0], sizes[1]);
1243 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001244
1245out:
1246 /* enable page faults to match kunmap_atomic() return conditions */
1247 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001248}
Nitin Gupta61989a82012-01-09 16:51:56 -06001249
Minchan Kim1b945ae2013-12-11 11:04:36 +09001250#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001251
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001252static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001253{
Nitin Gupta61989a82012-01-09 16:51:56 -06001254 struct mapping_area *area;
1255
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001256 area = &per_cpu(zs_map_area, cpu);
1257 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001258}
1259
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001260static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001261{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001262 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001263
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001264 area = &per_cpu(zs_map_area, cpu);
1265 __zs_cpu_down(area);
1266 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001267}
1268
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001269static bool can_merge(struct size_class *prev, int pages_per_zspage,
1270 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001271{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001272 if (prev->pages_per_zspage == pages_per_zspage &&
1273 prev->objs_per_zspage == objs_per_zspage)
1274 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001275
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001276 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001277}
1278
Minchan Kim37836892016-07-26 15:23:23 -07001279static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001280{
Minchan Kim37836892016-07-26 15:23:23 -07001281 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001282}
1283
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001284unsigned long zs_get_total_pages(struct zs_pool *pool)
1285{
1286 return atomic_long_read(&pool->pages_allocated);
1287}
1288EXPORT_SYMBOL_GPL(zs_get_total_pages);
1289
1290/**
1291 * zs_map_object - get address of allocated object from handle.
1292 * @pool: pool from which the object was allocated
1293 * @handle: handle returned from zs_malloc
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001294 * @mm: maping mode to use
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001295 *
1296 * Before using an object allocated from zs_malloc, it must be mapped using
1297 * this function. When done with the object, it must be unmapped using
1298 * zs_unmap_object.
1299 *
1300 * Only one object can be mapped per cpu at a time. There is no protection
1301 * against nested mappings.
1302 *
1303 * This function returns with preemption and page faults disabled.
1304 */
1305void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1306 enum zs_mapmode mm)
1307{
Minchan Kim37836892016-07-26 15:23:23 -07001308 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001309 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001310 unsigned long obj, off;
1311 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001312
1313 unsigned int class_idx;
1314 enum fullness_group fg;
1315 struct size_class *class;
1316 struct mapping_area *area;
1317 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001318 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001319
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001320 /*
1321 * Because we use per-cpu mapping areas shared among the
1322 * pools/users, we can't allow mapping in interrupt context
1323 * because it can corrupt another users mappings.
1324 */
Sergey Senozhatsky1aedcaf2017-11-15 17:34:03 -08001325 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001326
Minchan Kim312fcae2015-04-15 16:15:30 -07001327 /* From now on, migration cannot move the object */
1328 pin_tag(handle);
1329
Minchan Kim2e40e162015-04-15 16:15:23 -07001330 obj = handle_to_obj(handle);
1331 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001332 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001333
1334 /* migration cannot move any subpage in this zspage */
1335 migrate_read_lock(zspage);
1336
Minchan Kim37836892016-07-26 15:23:23 -07001337 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001338 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001339 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001340
1341 area = &get_cpu_var(zs_map_area);
1342 area->vm_mm = mm;
1343 if (off + class->size <= PAGE_SIZE) {
1344 /* this object is contained entirely within a page */
1345 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001346 ret = area->vm_addr + off;
1347 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001348 }
1349
1350 /* this object spans two pages */
1351 pages[0] = page;
1352 pages[1] = get_next_page(page);
1353 BUG_ON(!pages[1]);
1354
Minchan Kim2e40e162015-04-15 16:15:23 -07001355 ret = __zs_map_object(area, pages, off, class->size);
1356out:
Minchan Kim48b48002016-07-26 15:23:31 -07001357 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001358 ret += ZS_HANDLE_SIZE;
1359
1360 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001361}
1362EXPORT_SYMBOL_GPL(zs_map_object);
1363
1364void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1365{
Minchan Kim37836892016-07-26 15:23:23 -07001366 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001367 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001368 unsigned long obj, off;
1369 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001370
1371 unsigned int class_idx;
1372 enum fullness_group fg;
1373 struct size_class *class;
1374 struct mapping_area *area;
1375
Minchan Kim2e40e162015-04-15 16:15:23 -07001376 obj = handle_to_obj(handle);
1377 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001378 zspage = get_zspage(page);
1379 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001380 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001381 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001382
1383 area = this_cpu_ptr(&zs_map_area);
1384 if (off + class->size <= PAGE_SIZE)
1385 kunmap_atomic(area->vm_addr);
1386 else {
1387 struct page *pages[2];
1388
1389 pages[0] = page;
1390 pages[1] = get_next_page(page);
1391 BUG_ON(!pages[1]);
1392
1393 __zs_unmap_object(area, pages, off, class->size);
1394 }
1395 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001396
1397 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001398 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001399}
1400EXPORT_SYMBOL_GPL(zs_unmap_object);
1401
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07001402/**
1403 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1404 * zsmalloc &size_class.
1405 * @pool: zsmalloc pool to use
1406 *
1407 * The function returns the size of the first huge class - any object of equal
1408 * or bigger size will be stored in zspage consisting of a single physical
1409 * page.
1410 *
1411 * Context: Any context.
1412 *
1413 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1414 */
1415size_t zs_huge_class_size(struct zs_pool *pool)
1416{
1417 return huge_class_size;
1418}
1419EXPORT_SYMBOL_GPL(zs_huge_class_size);
1420
Minchan Kim251cbb92016-05-20 16:59:42 -07001421static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001422 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001423{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001424 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001425 unsigned long obj;
1426 struct link_free *link;
1427
1428 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001429 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001430 void *vaddr;
1431
Minchan Kim312fcae2015-04-15 16:15:30 -07001432 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001433 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001434
1435 offset = obj * class->size;
1436 nr_page = offset >> PAGE_SHIFT;
1437 m_offset = offset & ~PAGE_MASK;
1438 m_page = get_first_page(zspage);
1439
1440 for (i = 0; i < nr_page; i++)
1441 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001442
1443 vaddr = kmap_atomic(m_page);
1444 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001445 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001446 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001447 /* record handle in the header of allocated chunk */
1448 link->handle = handle;
1449 else
Minchan Kim37836892016-07-26 15:23:23 -07001450 /* record handle to page->index */
1451 zspage->first_page->index = handle;
1452
Minchan Kimc7806262015-04-15 16:15:26 -07001453 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001454 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001455 zs_stat_inc(class, OBJ_USED, 1);
1456
Minchan Kimbfd093f2016-07-26 15:23:28 -07001457 obj = location_to_obj(m_page, obj);
1458
Minchan Kimc7806262015-04-15 16:15:26 -07001459 return obj;
1460}
1461
1462
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001463/**
1464 * zs_malloc - Allocate block of given size from pool.
1465 * @pool: pool to allocate from
1466 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001467 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001468 *
1469 * On success, handle to the allocated object is returned,
1470 * otherwise 0.
1471 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1472 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001473unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001474{
Minchan Kim2e40e162015-04-15 16:15:23 -07001475 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001476 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001477 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001478 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001479
Minchan Kim7b60a682015-04-15 16:15:39 -07001480 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001481 return 0;
1482
Minchan Kim37836892016-07-26 15:23:23 -07001483 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001484 if (!handle)
1485 return 0;
1486
1487 /* extra space in chunk to keep the handle */
1488 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001489 class = pool->size_class[get_size_class_index(size)];
1490
1491 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001492 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001493 if (likely(zspage)) {
1494 obj = obj_malloc(class, zspage, handle);
1495 /* Now move the zspage to another fullness group, if required */
1496 fix_fullness_group(class, zspage);
1497 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001498 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001499
Minchan Kim48b48002016-07-26 15:23:31 -07001500 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001501 }
1502
Minchan Kim48b48002016-07-26 15:23:31 -07001503 spin_unlock(&class->lock);
1504
1505 zspage = alloc_zspage(pool, class, gfp);
1506 if (!zspage) {
1507 cache_free_handle(pool, handle);
1508 return 0;
1509 }
1510
1511 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001512 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001513 newfg = get_fullness_group(class, zspage);
1514 insert_zspage(class, zspage, newfg);
1515 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001516 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001517 atomic_long_add(class->pages_per_zspage,
1518 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001519 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001520
1521 /* We completely set up zspage so mark them as movable */
1522 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001523 spin_unlock(&class->lock);
1524
Minchan Kim2e40e162015-04-15 16:15:23 -07001525 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001526}
1527EXPORT_SYMBOL_GPL(zs_malloc);
1528
Minchan Kim1ee47162016-05-20 16:59:45 -07001529static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001530{
1531 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001532 struct zspage *zspage;
1533 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001534 unsigned long f_offset;
1535 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001536 void *vaddr;
1537
Minchan Kim312fcae2015-04-15 16:15:30 -07001538 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001539 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001540 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001541 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001542
Minchan Kimc7806262015-04-15 16:15:26 -07001543 vaddr = kmap_atomic(f_page);
1544
1545 /* Insert this object in containing zspage's freelist */
1546 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001547 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001548 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001549 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001550 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001551 zs_stat_dec(class, OBJ_USED, 1);
1552}
1553
1554void zs_free(struct zs_pool *pool, unsigned long handle)
1555{
Minchan Kim37836892016-07-26 15:23:23 -07001556 struct zspage *zspage;
1557 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001558 unsigned long obj;
1559 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001560 int class_idx;
1561 struct size_class *class;
1562 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001563 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001564
Minchan Kim2e40e162015-04-15 16:15:23 -07001565 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001566 return;
1567
Minchan Kim312fcae2015-04-15 16:15:30 -07001568 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001569 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001570 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001571 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001572
Minchan Kim48b48002016-07-26 15:23:31 -07001573 migrate_read_lock(zspage);
1574
Minchan Kim37836892016-07-26 15:23:23 -07001575 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001576 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001577
1578 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001579 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001580 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001581 if (fullness != ZS_EMPTY) {
1582 migrate_read_unlock(zspage);
1583 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001584 }
Minchan Kim48b48002016-07-26 15:23:31 -07001585
1586 isolated = is_zspage_isolated(zspage);
1587 migrate_read_unlock(zspage);
1588 /* If zspage is isolated, zs_page_putback will free the zspage */
1589 if (likely(!isolated))
1590 free_zspage(pool, class, zspage);
1591out:
1592
Minchan Kim312fcae2015-04-15 16:15:30 -07001593 spin_unlock(&class->lock);
1594 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001595 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001596}
1597EXPORT_SYMBOL_GPL(zs_free);
1598
Minchan Kim251cbb92016-05-20 16:59:42 -07001599static void zs_object_copy(struct size_class *class, unsigned long dst,
1600 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001601{
1602 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001603 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001604 unsigned long s_off, d_off;
1605 void *s_addr, *d_addr;
1606 int s_size, d_size, size;
1607 int written = 0;
1608
1609 s_size = d_size = class->size;
1610
1611 obj_to_location(src, &s_page, &s_objidx);
1612 obj_to_location(dst, &d_page, &d_objidx);
1613
Minchan Kimbfd093f2016-07-26 15:23:28 -07001614 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1615 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001616
1617 if (s_off + class->size > PAGE_SIZE)
1618 s_size = PAGE_SIZE - s_off;
1619
1620 if (d_off + class->size > PAGE_SIZE)
1621 d_size = PAGE_SIZE - d_off;
1622
1623 s_addr = kmap_atomic(s_page);
1624 d_addr = kmap_atomic(d_page);
1625
1626 while (1) {
1627 size = min(s_size, d_size);
1628 memcpy(d_addr + d_off, s_addr + s_off, size);
1629 written += size;
1630
1631 if (written == class->size)
1632 break;
1633
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001634 s_off += size;
1635 s_size -= size;
1636 d_off += size;
1637 d_size -= size;
1638
1639 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001640 kunmap_atomic(d_addr);
1641 kunmap_atomic(s_addr);
1642 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001643 s_addr = kmap_atomic(s_page);
1644 d_addr = kmap_atomic(d_page);
1645 s_size = class->size - written;
1646 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001647 }
1648
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001649 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001650 kunmap_atomic(d_addr);
1651 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001652 d_addr = kmap_atomic(d_page);
1653 d_size = class->size - written;
1654 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001655 }
1656 }
1657
1658 kunmap_atomic(d_addr);
1659 kunmap_atomic(s_addr);
1660}
1661
1662/*
1663 * Find alloced object in zspage from index object and
1664 * return handle.
1665 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001666static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001667 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001668{
1669 unsigned long head;
1670 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001671 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001672 unsigned long handle = 0;
1673 void *addr = kmap_atomic(page);
1674
Minchan Kim37836892016-07-26 15:23:23 -07001675 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001676 offset += class->size * index;
1677
1678 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001679 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001680 if (head & OBJ_ALLOCATED_TAG) {
1681 handle = head & ~OBJ_ALLOCATED_TAG;
1682 if (trypin_tag(handle))
1683 break;
1684 handle = 0;
1685 }
1686
1687 offset += class->size;
1688 index++;
1689 }
1690
1691 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001692
1693 *obj_idx = index;
1694
Minchan Kim312fcae2015-04-15 16:15:30 -07001695 return handle;
1696}
1697
1698struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001699 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001700 struct page *s_page;
1701 /* Destination page for migration which should be a first page
1702 * of zspage. */
1703 struct page *d_page;
1704 /* Starting object index within @s_page which used for live object
1705 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001706 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001707};
1708
1709static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1710 struct zs_compact_control *cc)
1711{
1712 unsigned long used_obj, free_obj;
1713 unsigned long handle;
1714 struct page *s_page = cc->s_page;
1715 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001716 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001717 int ret = 0;
1718
1719 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001720 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001721 if (!handle) {
1722 s_page = get_next_page(s_page);
1723 if (!s_page)
1724 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001725 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001726 continue;
1727 }
1728
1729 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001730 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001731 unpin_tag(handle);
1732 ret = -ENOMEM;
1733 break;
1734 }
1735
1736 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001737 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001738 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001739 obj_idx++;
Junil Leec102f07c2016-01-20 14:58:18 -08001740 /*
1741 * record_obj updates handle's value to free_obj and it will
1742 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1743 * breaks synchronization using pin_tag(e,g, zs_free) so
1744 * let's keep the lock bit.
1745 */
1746 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001747 record_obj(handle, free_obj);
1748 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001749 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001750 }
1751
1752 /* Remember last position in this iteration */
1753 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001754 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001755
1756 return ret;
1757}
1758
Minchan Kim37836892016-07-26 15:23:23 -07001759static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001760{
1761 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001762 struct zspage *zspage;
1763 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001764
Minchan Kim37836892016-07-26 15:23:23 -07001765 if (!source) {
1766 fg[0] = ZS_ALMOST_FULL;
1767 fg[1] = ZS_ALMOST_EMPTY;
1768 }
1769
1770 for (i = 0; i < 2; i++) {
1771 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1772 struct zspage, list);
1773 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001774 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001775 remove_zspage(class, zspage, fg[i]);
1776 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001777 }
1778 }
1779
Minchan Kim37836892016-07-26 15:23:23 -07001780 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001781}
1782
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001783/*
Minchan Kim37836892016-07-26 15:23:23 -07001784 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001785 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001786 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001787 *
Minchan Kim37836892016-07-26 15:23:23 -07001788 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001789 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001790static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001791 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001792{
Minchan Kim312fcae2015-04-15 16:15:30 -07001793 enum fullness_group fullness;
1794
Minchan Kim48b48002016-07-26 15:23:31 -07001795 VM_BUG_ON(is_zspage_isolated(zspage));
1796
Minchan Kim37836892016-07-26 15:23:23 -07001797 fullness = get_fullness_group(class, zspage);
1798 insert_zspage(class, zspage, fullness);
1799 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001800
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001801 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001802}
1803
Minchan Kim48b48002016-07-26 15:23:31 -07001804#ifdef CONFIG_COMPACTION
Colin Ian King4d0a5402018-08-17 15:46:50 -07001805/*
1806 * To prevent zspage destroy during migration, zspage freeing should
1807 * hold locks of all pages in the zspage.
1808 */
1809static void lock_zspage(struct zspage *zspage)
1810{
1811 struct page *page = get_first_page(zspage);
1812
1813 do {
1814 lock_page(page);
1815 } while ((page = get_next_page(page)) != NULL);
1816}
1817
David Howells8e9231f2019-03-25 16:38:23 +00001818static int zs_init_fs_context(struct fs_context *fc)
Minchan Kim48b48002016-07-26 15:23:31 -07001819{
David Howells8e9231f2019-03-25 16:38:23 +00001820 return init_pseudo(fc, ZSMALLOC_MAGIC) ? 0 : -ENOMEM;
Minchan Kim48b48002016-07-26 15:23:31 -07001821}
1822
1823static struct file_system_type zsmalloc_fs = {
1824 .name = "zsmalloc",
David Howells8e9231f2019-03-25 16:38:23 +00001825 .init_fs_context = zs_init_fs_context,
Minchan Kim48b48002016-07-26 15:23:31 -07001826 .kill_sb = kill_anon_super,
1827};
1828
1829static int zsmalloc_mount(void)
1830{
1831 int ret = 0;
1832
1833 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1834 if (IS_ERR(zsmalloc_mnt))
1835 ret = PTR_ERR(zsmalloc_mnt);
1836
1837 return ret;
1838}
1839
1840static void zsmalloc_unmount(void)
1841{
1842 kern_unmount(zsmalloc_mnt);
1843}
1844
1845static void migrate_lock_init(struct zspage *zspage)
1846{
1847 rwlock_init(&zspage->lock);
1848}
1849
1850static void migrate_read_lock(struct zspage *zspage)
1851{
1852 read_lock(&zspage->lock);
1853}
1854
1855static void migrate_read_unlock(struct zspage *zspage)
1856{
1857 read_unlock(&zspage->lock);
1858}
1859
1860static void migrate_write_lock(struct zspage *zspage)
1861{
1862 write_lock(&zspage->lock);
1863}
1864
1865static void migrate_write_unlock(struct zspage *zspage)
1866{
1867 write_unlock(&zspage->lock);
1868}
1869
1870/* Number of isolated subpage for *page migration* in this zspage */
1871static void inc_zspage_isolation(struct zspage *zspage)
1872{
1873 zspage->isolated++;
1874}
1875
1876static void dec_zspage_isolation(struct zspage *zspage)
1877{
1878 zspage->isolated--;
1879}
1880
1881static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1882 struct page *newpage, struct page *oldpage)
1883{
1884 struct page *page;
1885 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1886 int idx = 0;
1887
1888 page = get_first_page(zspage);
1889 do {
1890 if (page == oldpage)
1891 pages[idx] = newpage;
1892 else
1893 pages[idx] = page;
1894 idx++;
1895 } while ((page = get_next_page(page)) != NULL);
1896
1897 create_page_chain(class, zspage, pages);
1898 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1899 if (unlikely(PageHugeObject(oldpage)))
1900 newpage->index = oldpage->index;
1901 __SetPageMovable(newpage, page_mapping(oldpage));
1902}
1903
Colin Ian King4d0a5402018-08-17 15:46:50 -07001904static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
Minchan Kim48b48002016-07-26 15:23:31 -07001905{
1906 struct zs_pool *pool;
1907 struct size_class *class;
1908 int class_idx;
1909 enum fullness_group fullness;
1910 struct zspage *zspage;
1911 struct address_space *mapping;
1912
1913 /*
1914 * Page is locked so zspage couldn't be destroyed. For detail, look at
1915 * lock_zspage in free_zspage.
1916 */
1917 VM_BUG_ON_PAGE(!PageMovable(page), page);
1918 VM_BUG_ON_PAGE(PageIsolated(page), page);
1919
1920 zspage = get_zspage(page);
1921
1922 /*
1923 * Without class lock, fullness could be stale while class_idx is okay
1924 * because class_idx is constant unless page is freed so we should get
1925 * fullness again under class lock.
1926 */
1927 get_zspage_mapping(zspage, &class_idx, &fullness);
1928 mapping = page_mapping(page);
1929 pool = mapping->private_data;
1930 class = pool->size_class[class_idx];
1931
1932 spin_lock(&class->lock);
1933 if (get_zspage_inuse(zspage) == 0) {
1934 spin_unlock(&class->lock);
1935 return false;
1936 }
1937
1938 /* zspage is isolated for object migration */
1939 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1940 spin_unlock(&class->lock);
1941 return false;
1942 }
1943
1944 /*
1945 * If this is first time isolation for the zspage, isolate zspage from
1946 * size_class to prevent further object allocation from the zspage.
1947 */
1948 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1949 get_zspage_mapping(zspage, &class_idx, &fullness);
1950 remove_zspage(class, zspage, fullness);
1951 }
1952
1953 inc_zspage_isolation(zspage);
1954 spin_unlock(&class->lock);
1955
1956 return true;
1957}
1958
Colin Ian King4d0a5402018-08-17 15:46:50 -07001959static int zs_page_migrate(struct address_space *mapping, struct page *newpage,
Minchan Kim48b48002016-07-26 15:23:31 -07001960 struct page *page, enum migrate_mode mode)
1961{
1962 struct zs_pool *pool;
1963 struct size_class *class;
1964 int class_idx;
1965 enum fullness_group fullness;
1966 struct zspage *zspage;
1967 struct page *dummy;
1968 void *s_addr, *d_addr, *addr;
1969 int offset, pos;
1970 unsigned long handle, head;
1971 unsigned long old_obj, new_obj;
1972 unsigned int obj_idx;
1973 int ret = -EAGAIN;
1974
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07001975 /*
1976 * We cannot support the _NO_COPY case here, because copy needs to
1977 * happen under the zs lock, which does not work with
1978 * MIGRATE_SYNC_NO_COPY workflow.
1979 */
1980 if (mode == MIGRATE_SYNC_NO_COPY)
1981 return -EINVAL;
1982
Minchan Kim48b48002016-07-26 15:23:31 -07001983 VM_BUG_ON_PAGE(!PageMovable(page), page);
1984 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1985
1986 zspage = get_zspage(page);
1987
1988 /* Concurrent compactor cannot migrate any subpage in zspage */
1989 migrate_write_lock(zspage);
1990 get_zspage_mapping(zspage, &class_idx, &fullness);
1991 pool = mapping->private_data;
1992 class = pool->size_class[class_idx];
1993 offset = get_first_obj_offset(page);
1994
1995 spin_lock(&class->lock);
1996 if (!get_zspage_inuse(zspage)) {
Hui Zhu77ff4652017-09-06 16:21:08 -07001997 /*
1998 * Set "offset" to end of the page so that every loops
1999 * skips unnecessary object scanning.
2000 */
2001 offset = PAGE_SIZE;
Minchan Kim48b48002016-07-26 15:23:31 -07002002 }
2003
2004 pos = offset;
2005 s_addr = kmap_atomic(page);
2006 while (pos < PAGE_SIZE) {
2007 head = obj_to_head(page, s_addr + pos);
2008 if (head & OBJ_ALLOCATED_TAG) {
2009 handle = head & ~OBJ_ALLOCATED_TAG;
2010 if (!trypin_tag(handle))
2011 goto unpin_objects;
2012 }
2013 pos += class->size;
2014 }
2015
2016 /*
2017 * Here, any user cannot access all objects in the zspage so let's move.
2018 */
2019 d_addr = kmap_atomic(newpage);
2020 memcpy(d_addr, s_addr, PAGE_SIZE);
2021 kunmap_atomic(d_addr);
2022
2023 for (addr = s_addr + offset; addr < s_addr + pos;
2024 addr += class->size) {
2025 head = obj_to_head(page, addr);
2026 if (head & OBJ_ALLOCATED_TAG) {
2027 handle = head & ~OBJ_ALLOCATED_TAG;
2028 if (!testpin_tag(handle))
2029 BUG();
2030
2031 old_obj = handle_to_obj(handle);
2032 obj_to_location(old_obj, &dummy, &obj_idx);
2033 new_obj = (unsigned long)location_to_obj(newpage,
2034 obj_idx);
2035 new_obj |= BIT(HANDLE_PIN_BIT);
2036 record_obj(handle, new_obj);
2037 }
2038 }
2039
2040 replace_sub_page(class, zspage, newpage, page);
2041 get_page(newpage);
2042
2043 dec_zspage_isolation(zspage);
2044
2045 /*
2046 * Page migration is done so let's putback isolated zspage to
2047 * the list if @page is final isolated subpage in the zspage.
2048 */
2049 if (!is_zspage_isolated(zspage))
2050 putback_zspage(class, zspage);
2051
2052 reset_page(page);
2053 put_page(page);
2054 page = newpage;
2055
Minchan Kimdd4123f2016-07-26 15:26:50 -07002056 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002057unpin_objects:
2058 for (addr = s_addr + offset; addr < s_addr + pos;
2059 addr += class->size) {
2060 head = obj_to_head(page, addr);
2061 if (head & OBJ_ALLOCATED_TAG) {
2062 handle = head & ~OBJ_ALLOCATED_TAG;
2063 if (!testpin_tag(handle))
2064 BUG();
2065 unpin_tag(handle);
2066 }
2067 }
2068 kunmap_atomic(s_addr);
Minchan Kim48b48002016-07-26 15:23:31 -07002069 spin_unlock(&class->lock);
2070 migrate_write_unlock(zspage);
2071
2072 return ret;
2073}
2074
Colin Ian King4d0a5402018-08-17 15:46:50 -07002075static void zs_page_putback(struct page *page)
Minchan Kim48b48002016-07-26 15:23:31 -07002076{
2077 struct zs_pool *pool;
2078 struct size_class *class;
2079 int class_idx;
2080 enum fullness_group fg;
2081 struct address_space *mapping;
2082 struct zspage *zspage;
2083
2084 VM_BUG_ON_PAGE(!PageMovable(page), page);
2085 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2086
2087 zspage = get_zspage(page);
2088 get_zspage_mapping(zspage, &class_idx, &fg);
2089 mapping = page_mapping(page);
2090 pool = mapping->private_data;
2091 class = pool->size_class[class_idx];
2092
2093 spin_lock(&class->lock);
2094 dec_zspage_isolation(zspage);
2095 if (!is_zspage_isolated(zspage)) {
2096 fg = putback_zspage(class, zspage);
2097 /*
2098 * Due to page_lock, we cannot free zspage immediately
2099 * so let's defer.
2100 */
2101 if (fg == ZS_EMPTY)
2102 schedule_work(&pool->free_work);
2103 }
2104 spin_unlock(&class->lock);
2105}
2106
Colin Ian King4d0a5402018-08-17 15:46:50 -07002107static const struct address_space_operations zsmalloc_aops = {
Minchan Kim48b48002016-07-26 15:23:31 -07002108 .isolate_page = zs_page_isolate,
2109 .migratepage = zs_page_migrate,
2110 .putback_page = zs_page_putback,
2111};
2112
2113static int zs_register_migration(struct zs_pool *pool)
2114{
2115 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2116 if (IS_ERR(pool->inode)) {
2117 pool->inode = NULL;
2118 return 1;
2119 }
2120
2121 pool->inode->i_mapping->private_data = pool;
2122 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2123 return 0;
2124}
2125
2126static void zs_unregister_migration(struct zs_pool *pool)
2127{
2128 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002129 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002130}
2131
2132/*
2133 * Caller should hold page_lock of all pages in the zspage
2134 * In here, we cannot use zspage meta data.
2135 */
2136static void async_free_zspage(struct work_struct *work)
2137{
2138 int i;
2139 struct size_class *class;
2140 unsigned int class_idx;
2141 enum fullness_group fullness;
2142 struct zspage *zspage, *tmp;
2143 LIST_HEAD(free_pages);
2144 struct zs_pool *pool = container_of(work, struct zs_pool,
2145 free_work);
2146
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002147 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim48b48002016-07-26 15:23:31 -07002148 class = pool->size_class[i];
2149 if (class->index != i)
2150 continue;
2151
2152 spin_lock(&class->lock);
2153 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2154 spin_unlock(&class->lock);
2155 }
2156
2157
2158 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2159 list_del(&zspage->list);
2160 lock_zspage(zspage);
2161
2162 get_zspage_mapping(zspage, &class_idx, &fullness);
2163 VM_BUG_ON(fullness != ZS_EMPTY);
2164 class = pool->size_class[class_idx];
2165 spin_lock(&class->lock);
2166 __free_zspage(pool, pool->size_class[class_idx], zspage);
2167 spin_unlock(&class->lock);
2168 }
2169};
2170
2171static void kick_deferred_free(struct zs_pool *pool)
2172{
2173 schedule_work(&pool->free_work);
2174}
2175
2176static void init_deferred_free(struct zs_pool *pool)
2177{
2178 INIT_WORK(&pool->free_work, async_free_zspage);
2179}
2180
2181static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2182{
2183 struct page *page = get_first_page(zspage);
2184
2185 do {
2186 WARN_ON(!trylock_page(page));
2187 __SetPageMovable(page, pool->inode->i_mapping);
2188 unlock_page(page);
2189 } while ((page = get_next_page(page)) != NULL);
2190}
2191#endif
2192
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002193/*
2194 *
2195 * Based on the number of unused allocated objects calculate
2196 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002197 */
2198static unsigned long zs_can_compact(struct size_class *class)
2199{
2200 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002201 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2202 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002203
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002204 if (obj_allocated <= obj_used)
2205 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002206
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002207 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002208 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002209
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002210 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002211}
2212
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002213static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002214{
Minchan Kim312fcae2015-04-15 16:15:30 -07002215 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002216 struct zspage *src_zspage;
2217 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002218
Minchan Kim312fcae2015-04-15 16:15:30 -07002219 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002220 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002221
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002222 if (!zs_can_compact(class))
2223 break;
2224
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002225 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002226 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002227
Minchan Kim37836892016-07-26 15:23:23 -07002228 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002229 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002230 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002231 * If there is no more space in dst_page, resched
2232 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002233 */
2234 if (!migrate_zspage(pool, class, &cc))
2235 break;
2236
Minchan Kim4aa409c2016-07-26 15:23:26 -07002237 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002238 }
2239
2240 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002241 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002242 break;
2243
Minchan Kim4aa409c2016-07-26 15:23:26 -07002244 putback_zspage(class, dst_zspage);
2245 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002246 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002247 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002248 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002249 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002250 cond_resched();
2251 spin_lock(&class->lock);
2252 }
2253
Minchan Kim37836892016-07-26 15:23:23 -07002254 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002255 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002256
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002257 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002258}
2259
2260unsigned long zs_compact(struct zs_pool *pool)
2261{
2262 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002263 struct size_class *class;
2264
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002265 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002266 class = pool->size_class[i];
2267 if (!class)
2268 continue;
2269 if (class->index != i)
2270 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002271 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002272 }
2273
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002274 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002275}
2276EXPORT_SYMBOL_GPL(zs_compact);
2277
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002278void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2279{
2280 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2281}
2282EXPORT_SYMBOL_GPL(zs_pool_stats);
2283
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002284static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2285 struct shrink_control *sc)
2286{
2287 unsigned long pages_freed;
2288 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2289 shrinker);
2290
2291 pages_freed = pool->stats.pages_compacted;
2292 /*
2293 * Compact classes and calculate compaction delta.
2294 * Can run concurrently with a manually triggered
2295 * (by user) compaction.
2296 */
2297 pages_freed = zs_compact(pool) - pages_freed;
2298
2299 return pages_freed ? pages_freed : SHRINK_STOP;
2300}
2301
2302static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2303 struct shrink_control *sc)
2304{
2305 int i;
2306 struct size_class *class;
2307 unsigned long pages_to_free = 0;
2308 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2309 shrinker);
2310
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002311 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002312 class = pool->size_class[i];
2313 if (!class)
2314 continue;
2315 if (class->index != i)
2316 continue;
2317
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002318 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002319 }
2320
2321 return pages_to_free;
2322}
2323
2324static void zs_unregister_shrinker(struct zs_pool *pool)
2325{
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002326 unregister_shrinker(&pool->shrinker);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002327}
2328
2329static int zs_register_shrinker(struct zs_pool *pool)
2330{
2331 pool->shrinker.scan_objects = zs_shrinker_scan;
2332 pool->shrinker.count_objects = zs_shrinker_count;
2333 pool->shrinker.batch = 0;
2334 pool->shrinker.seeks = DEFAULT_SEEKS;
2335
2336 return register_shrinker(&pool->shrinker);
2337}
2338
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002339/**
2340 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002341 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002342 *
2343 * This function must be called before anything when using
2344 * the zsmalloc allocator.
2345 *
2346 * On success, a pointer to the newly created pool is returned,
2347 * otherwise NULL.
2348 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002349struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002350{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002351 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002352 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002353 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002354
Ganesh Mahendran18136652014-12-12 16:57:10 -08002355 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002356 if (!pool)
2357 return NULL;
2358
Minchan Kim48b48002016-07-26 15:23:31 -07002359 init_deferred_free(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002360
Minchan Kim2e40e162015-04-15 16:15:23 -07002361 pool->name = kstrdup(name, GFP_KERNEL);
2362 if (!pool->name)
2363 goto err;
2364
Minchan Kim37836892016-07-26 15:23:23 -07002365 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002366 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002367
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002368 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002369 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002370 * for merging should be larger or equal to current size.
2371 */
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002372 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002373 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002374 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002375 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002376 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002377 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002378
2379 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2380 if (size > ZS_MAX_ALLOC_SIZE)
2381 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002382 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002383 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002384
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002385 /*
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07002386 * We iterate from biggest down to smallest classes,
2387 * so huge_class_size holds the size of the first huge
2388 * class. Any object bigger than or equal to that will
2389 * endup in the huge class.
2390 */
2391 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2392 !huge_class_size) {
2393 huge_class_size = size;
2394 /*
2395 * The object uses ZS_HANDLE_SIZE bytes to store the
2396 * handle. We need to subtract it, because zs_malloc()
2397 * unconditionally adds handle size before it performs
2398 * size class search - so object may be smaller than
2399 * huge class size, yet it still can end up in the huge
2400 * class because it grows by ZS_HANDLE_SIZE extra bytes
2401 * right before class lookup.
2402 */
2403 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2404 }
2405
2406 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002407 * size_class is used for normal zsmalloc operation such
2408 * as alloc/free for that size. Although it is natural that we
2409 * have one size_class for each size, there is a chance that we
2410 * can get more memory utilization if we use one size_class for
2411 * many different sizes whose size_class have same
2412 * characteristics. So, we makes size_class point to
2413 * previous size_class if possible.
2414 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002415 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002416 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002417 pool->size_class[i] = prev_class;
2418 continue;
2419 }
2420 }
2421
2422 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2423 if (!class)
2424 goto err;
2425
Nitin Gupta61989a82012-01-09 16:51:56 -06002426 class->size = size;
2427 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002428 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002429 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002430 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002431 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002432 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2433 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002434 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002435
2436 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002437 }
2438
Dan Streetmand34f6152016-05-20 16:59:56 -07002439 /* debug only, don't abort if it fails */
2440 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002441
Minchan Kim48b48002016-07-26 15:23:31 -07002442 if (zs_register_migration(pool))
2443 goto err;
2444
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002445 /*
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002446 * Not critical since shrinker is only used to trigger internal
2447 * defragmentation of the pool which is pretty optional thing. If
2448 * registration fails we still can use the pool normally and user can
2449 * trigger compaction manually. Thus, ignore return code.
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002450 */
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002451 zs_register_shrinker(pool);
2452
Nitin Gupta61989a82012-01-09 16:51:56 -06002453 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002454
2455err:
2456 zs_destroy_pool(pool);
2457 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002458}
2459EXPORT_SYMBOL_GPL(zs_create_pool);
2460
2461void zs_destroy_pool(struct zs_pool *pool)
2462{
2463 int i;
2464
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002465 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002466 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002467 zs_pool_stat_destroy(pool);
2468
Jerome Marchandcf8e0fe2017-07-10 15:50:18 -07002469 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002470 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002471 struct size_class *class = pool->size_class[i];
2472
2473 if (!class)
2474 continue;
2475
2476 if (class->index != i)
2477 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002478
Minchan Kim48b48002016-07-26 15:23:31 -07002479 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002480 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002481 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002482 class->size, fg);
2483 }
2484 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002485 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002486 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002487
Minchan Kim37836892016-07-26 15:23:23 -07002488 destroy_cache(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002489 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002490 kfree(pool);
2491}
2492EXPORT_SYMBOL_GPL(zs_destroy_pool);
2493
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002494static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002495{
Minchan Kim48b48002016-07-26 15:23:31 -07002496 int ret;
2497
2498 ret = zsmalloc_mount();
2499 if (ret)
2500 goto out;
2501
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002502 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2503 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002504 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002505 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002506
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002507#ifdef CONFIG_ZPOOL
2508 zpool_register_driver(&zs_zpool_driver);
2509#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002510
Dan Streetman4abaac92016-05-26 15:16:27 -07002511 zs_stat_init();
2512
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002513 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002514
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002515hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002516 zsmalloc_unmount();
2517out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002518 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002519}
Nitin Gupta61989a82012-01-09 16:51:56 -06002520
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002521static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002522{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002523#ifdef CONFIG_ZPOOL
2524 zpool_unregister_driver(&zs_zpool_driver);
2525#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002526 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002527 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002528
2529 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002530}
Ben Hutchings069f1012012-06-20 02:31:11 +01002531
2532module_init(zs_init);
2533module_exit(zs_exit);
2534
2535MODULE_LICENSE("Dual BSD/GPL");
2536MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");