blob: 25d0fb6443d1bea33eb6c51188c9391f6b5a12a8 [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the license that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 */
13
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * Following is how we use various fields and flags of underlying
16 * struct page(s) to form a zspage.
17 *
18 * Usage of struct page fields:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Minchan Kim48b48002016-07-26 15:23:31 -070020 * page->freelist(index): links together all component pages of a zspage
21 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Ganesh Mahendranfd854462016-07-28 15:47:54 -070023 * page->units: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
Xishi Qiu399d8ee2017-02-22 15:45:01 -080027 * PG_owner_priv_1: identifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Nitin Gupta61989a82012-01-09 16:51:56 -060033#include <linux/module.h>
34#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070035#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060036#include <linux/bitops.h>
37#include <linux/errno.h>
38#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060039#include <linux/string.h>
40#include <linux/slab.h>
41#include <asm/tlbflush.h>
42#include <asm/pgtable.h>
43#include <linux/cpumask.h>
44#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060045#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080046#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090047#include <linux/spinlock.h>
48#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080049#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080050#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070051#include <linux/zpool.h>
Minchan Kim48b48002016-07-26 15:23:31 -070052#include <linux/mount.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070053#include <linux/migrate.h>
Minchan Kim48b48002016-07-26 15:23:31 -070054#include <linux/pagemap.h>
55
56#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090057
58/*
59 * This must be power of 2 and greater than of equal to sizeof(link_free).
60 * These two conditions ensure that any 'struct link_free' itself doesn't
61 * span more than 1 page which avoids complex case of mapping 2 pages simply
62 * to restore link_free pointer values.
63 */
64#define ZS_ALIGN 8
65
66/*
67 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
68 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
69 */
70#define ZS_MAX_ZSPAGE_ORDER 2
71#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
72
Minchan Kim2e40e162015-04-15 16:15:23 -070073#define ZS_HANDLE_SIZE (sizeof(unsigned long))
74
Seth Jennings0959c632012-08-08 15:12:17 +090075/*
76 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090077 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090078 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070079 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090080 *
81 * This is made more complicated by various memory models and PAE.
82 */
83
84#ifndef MAX_PHYSMEM_BITS
85#ifdef CONFIG_HIGHMEM64G
86#define MAX_PHYSMEM_BITS 36
87#else /* !CONFIG_HIGHMEM64G */
88/*
89 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
90 * be PAGE_SHIFT
91 */
92#define MAX_PHYSMEM_BITS BITS_PER_LONG
93#endif
94#endif
95#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070096
97/*
98 * Memory for allocating for handle keeps object position by
99 * encoding <page, obj_idx> and the encoded value has a room
100 * in least bit(ie, look at obj_to_location).
101 * We use the bit to synchronize between object access by
102 * user and migration.
103 */
104#define HANDLE_PIN_BIT 0
105
106/*
107 * Head in allocated object should have OBJ_ALLOCATED_TAG
108 * to identify the object was allocated or not.
109 * It's okay to add the status bit in the least bit because
110 * header keeps handle which is 4byte-aligned address so we
111 * have room for two bit at least.
112 */
113#define OBJ_ALLOCATED_TAG 1
114#define OBJ_TAG_BITS 1
115#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900116#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
117
118#define MAX(a, b) ((a) >= (b) ? (a) : (b))
119/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
120#define ZS_MIN_ALLOC_SIZE \
121 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700122/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700123#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900124
125/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700126 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900127 * trader-off here:
128 * - Large number of size classes is potentially wasteful as free page are
129 * spread across these classes
130 * - Small number of size classes causes large internal fragmentation
131 * - Probably its better to use specific size classes (empirically
132 * determined). NOTE: all those class sizes must be set as multiple of
133 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
134 *
135 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
136 * (reason above)
137 */
Minchan Kim37836892016-07-26 15:23:23 -0700138#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900139
Seth Jennings0959c632012-08-08 15:12:17 +0900140enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900141 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700142 ZS_ALMOST_EMPTY,
143 ZS_ALMOST_FULL,
144 ZS_FULL,
145 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900146};
147
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800148enum zs_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700149 CLASS_EMPTY,
150 CLASS_ALMOST_EMPTY,
151 CLASS_ALMOST_FULL,
152 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800153 OBJ_ALLOCATED,
154 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700155 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800156};
157
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800158struct zs_size_stat {
159 unsigned long objs[NR_ZS_STAT_TYPE];
160};
161
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700162#ifdef CONFIG_ZSMALLOC_STAT
163static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800164#endif
165
Minchan Kim48b48002016-07-26 15:23:31 -0700166#ifdef CONFIG_COMPACTION
167static struct vfsmount *zsmalloc_mnt;
168#endif
169
Seth Jennings0959c632012-08-08 15:12:17 +0900170/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800171 * number of size_classes
172 */
173static int zs_size_classes;
174
175/*
Seth Jennings0959c632012-08-08 15:12:17 +0900176 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
177 * n <= N / f, where
178 * n = number of allocated objects
179 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700180 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900181 *
182 * Similarly, we assign zspage to:
183 * ZS_ALMOST_FULL when n > N / f
184 * ZS_EMPTY when n == 0
185 * ZS_FULL when n == N
186 *
187 * (see: fix_fullness_group())
188 */
189static const int fullness_threshold_frac = 4;
190
191struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700192 spinlock_t lock;
Minchan Kim48b48002016-07-26 15:23:31 -0700193 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900194 /*
195 * Size of objects stored in this class. Must be multiple
196 * of ZS_ALIGN.
197 */
198 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700199 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800200 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
201 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700202
203 unsigned int index;
204 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900205};
206
Minchan Kim48b48002016-07-26 15:23:31 -0700207/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
208static void SetPageHugeObject(struct page *page)
209{
210 SetPageOwnerPriv1(page);
211}
212
213static void ClearPageHugeObject(struct page *page)
214{
215 ClearPageOwnerPriv1(page);
216}
217
218static int PageHugeObject(struct page *page)
219{
220 return PageOwnerPriv1(page);
221}
222
Seth Jennings0959c632012-08-08 15:12:17 +0900223/*
224 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700225 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900226 *
227 * This must be power of 2 and less than or equal to ZS_ALIGN
228 */
229struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700230 union {
231 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700232 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700233 * It's valid for non-allocated object
234 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700235 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700236 /*
237 * Handle of allocated object.
238 */
239 unsigned long handle;
240 };
Seth Jennings0959c632012-08-08 15:12:17 +0900241};
242
243struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800244 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800245
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800246 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700247 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700248 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900249
Minchan Kim13de8932014-10-09 15:29:48 -0700250 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800251
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700252 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700253
254 /* Compact classes */
255 struct shrinker shrinker;
256 /*
257 * To signify that register_shrinker() was successful
258 * and unregister_shrinker() will not Oops.
259 */
260 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800261#ifdef CONFIG_ZSMALLOC_STAT
262 struct dentry *stat_dentry;
263#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700264#ifdef CONFIG_COMPACTION
265 struct inode *inode;
266 struct work_struct free_work;
267#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900268};
Nitin Gupta61989a82012-01-09 16:51:56 -0600269
270/*
271 * A zspage's class index and fullness group
272 * are encoded in its (first)page->mapping
273 */
Minchan Kim37836892016-07-26 15:23:23 -0700274#define FULLNESS_BITS 2
275#define CLASS_BITS 8
Minchan Kim48b48002016-07-26 15:23:31 -0700276#define ISOLATED_BITS 3
277#define MAGIC_VAL_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700278
Minchan Kim37836892016-07-26 15:23:23 -0700279struct zspage {
280 struct {
281 unsigned int fullness:FULLNESS_BITS;
282 unsigned int class:CLASS_BITS;
Minchan Kim48b48002016-07-26 15:23:31 -0700283 unsigned int isolated:ISOLATED_BITS;
284 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700285 };
286 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700287 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700288 struct page *first_page;
289 struct list_head list; /* fullness list */
Minchan Kim48b48002016-07-26 15:23:31 -0700290#ifdef CONFIG_COMPACTION
291 rwlock_t lock;
292#endif
Minchan Kim37836892016-07-26 15:23:23 -0700293};
Nitin Gupta61989a82012-01-09 16:51:56 -0600294
Seth Jenningsf5536462012-07-18 11:55:56 -0500295struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900296#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500297 struct vm_struct *vm; /* vm area for mapping object that span pages */
298#else
299 char *vm_buf; /* copy buffer for objects that span pages */
300#endif
301 char *vm_addr; /* address of kmap_atomic()'ed pages */
302 enum zs_mapmode vm_mm; /* mapping mode */
303};
304
Minchan Kim48b48002016-07-26 15:23:31 -0700305#ifdef CONFIG_COMPACTION
306static int zs_register_migration(struct zs_pool *pool);
307static void zs_unregister_migration(struct zs_pool *pool);
308static void migrate_lock_init(struct zspage *zspage);
309static void migrate_read_lock(struct zspage *zspage);
310static void migrate_read_unlock(struct zspage *zspage);
311static void kick_deferred_free(struct zs_pool *pool);
312static void init_deferred_free(struct zs_pool *pool);
313static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
314#else
315static int zsmalloc_mount(void) { return 0; }
316static void zsmalloc_unmount(void) {}
317static int zs_register_migration(struct zs_pool *pool) { return 0; }
318static void zs_unregister_migration(struct zs_pool *pool) {}
319static void migrate_lock_init(struct zspage *zspage) {}
320static void migrate_read_lock(struct zspage *zspage) {}
321static void migrate_read_unlock(struct zspage *zspage) {}
322static void kick_deferred_free(struct zs_pool *pool) {}
323static void init_deferred_free(struct zs_pool *pool) {}
324static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
325#endif
326
Minchan Kim37836892016-07-26 15:23:23 -0700327static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700328{
329 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
330 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700331 if (!pool->handle_cachep)
332 return 1;
333
334 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
335 0, 0, NULL);
336 if (!pool->zspage_cachep) {
337 kmem_cache_destroy(pool->handle_cachep);
338 pool->handle_cachep = NULL;
339 return 1;
340 }
341
342 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700343}
344
Minchan Kim37836892016-07-26 15:23:23 -0700345static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700346{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700347 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700348 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700349}
350
Minchan Kim37836892016-07-26 15:23:23 -0700351static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700352{
353 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700354 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700355}
356
Minchan Kim37836892016-07-26 15:23:23 -0700357static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700358{
359 kmem_cache_free(pool->handle_cachep, (void *)handle);
360}
361
Minchan Kim37836892016-07-26 15:23:23 -0700362static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
363{
Minchan Kim48b48002016-07-26 15:23:31 -0700364 return kmem_cache_alloc(pool->zspage_cachep,
365 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800366}
Minchan Kim37836892016-07-26 15:23:23 -0700367
368static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
369{
370 kmem_cache_free(pool->zspage_cachep, zspage);
371}
372
Minchan Kim2e40e162015-04-15 16:15:23 -0700373static void record_obj(unsigned long handle, unsigned long obj)
374{
Junil Leec102f07c2016-01-20 14:58:18 -0800375 /*
376 * lsb of @obj represents handle lock while other bits
377 * represent object value the handle is pointing so
378 * updating shouldn't do store tearing.
379 */
380 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700381}
382
Dan Streetmanc7957792014-08-06 16:08:38 -0700383/* zpool driver */
384
385#ifdef CONFIG_ZPOOL
386
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800387static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700388 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700389 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700390{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700391 /*
392 * Ignore global gfp flags: zs_malloc() may be invoked from
393 * different contexts and its caller must provide a valid
394 * gfp mask.
395 */
396 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700397}
398
399static void zs_zpool_destroy(void *pool)
400{
401 zs_destroy_pool(pool);
402}
403
404static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
405 unsigned long *handle)
406{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700407 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700408 return *handle ? 0 : -1;
409}
410static void zs_zpool_free(void *pool, unsigned long handle)
411{
412 zs_free(pool, handle);
413}
414
415static int zs_zpool_shrink(void *pool, unsigned int pages,
416 unsigned int *reclaimed)
417{
418 return -EINVAL;
419}
420
421static void *zs_zpool_map(void *pool, unsigned long handle,
422 enum zpool_mapmode mm)
423{
424 enum zs_mapmode zs_mm;
425
426 switch (mm) {
427 case ZPOOL_MM_RO:
428 zs_mm = ZS_MM_RO;
429 break;
430 case ZPOOL_MM_WO:
431 zs_mm = ZS_MM_WO;
432 break;
433 case ZPOOL_MM_RW: /* fallthru */
434 default:
435 zs_mm = ZS_MM_RW;
436 break;
437 }
438
439 return zs_map_object(pool, handle, zs_mm);
440}
441static void zs_zpool_unmap(void *pool, unsigned long handle)
442{
443 zs_unmap_object(pool, handle);
444}
445
446static u64 zs_zpool_total_size(void *pool)
447{
Minchan Kim722cdc12014-10-09 15:29:50 -0700448 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700449}
450
451static struct zpool_driver zs_zpool_driver = {
452 .type = "zsmalloc",
453 .owner = THIS_MODULE,
454 .create = zs_zpool_create,
455 .destroy = zs_zpool_destroy,
456 .malloc = zs_zpool_malloc,
457 .free = zs_zpool_free,
458 .shrink = zs_zpool_shrink,
459 .map = zs_zpool_map,
460 .unmap = zs_zpool_unmap,
461 .total_size = zs_zpool_total_size,
462};
463
Kees Cook137f8cf2014-08-29 15:18:40 -0700464MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700465#endif /* CONFIG_ZPOOL */
466
Nitin Gupta61989a82012-01-09 16:51:56 -0600467/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
468static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
469
Minchan Kim48b48002016-07-26 15:23:31 -0700470static bool is_zspage_isolated(struct zspage *zspage)
471{
472 return zspage->isolated;
473}
474
Nitin Gupta61989a82012-01-09 16:51:56 -0600475static int is_first_page(struct page *page)
476{
Minchan Kima27545bf2012-04-25 15:23:09 +0900477 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600478}
479
Minchan Kim48b48002016-07-26 15:23:31 -0700480/* Protected by class->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700481static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600482{
Minchan Kim37836892016-07-26 15:23:23 -0700483 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600484}
485
Minchan Kim37836892016-07-26 15:23:23 -0700486static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700487{
Minchan Kim37836892016-07-26 15:23:23 -0700488 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700489}
490
Minchan Kim37836892016-07-26 15:23:23 -0700491static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700492{
Minchan Kim37836892016-07-26 15:23:23 -0700493 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700494}
495
Minchan Kim48b48002016-07-26 15:23:31 -0700496static inline struct page *get_first_page(struct zspage *zspage)
497{
498 struct page *first_page = zspage->first_page;
499
500 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
501 return first_page;
502}
503
Minchan Kim4f420472016-07-26 15:23:17 -0700504static inline int get_first_obj_offset(struct page *page)
505{
Minchan Kim48b48002016-07-26 15:23:31 -0700506 return page->units;
Minchan Kim4f420472016-07-26 15:23:17 -0700507}
508
509static inline void set_first_obj_offset(struct page *page, int offset)
510{
Minchan Kim48b48002016-07-26 15:23:31 -0700511 page->units = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700512}
513
Minchan Kimbfd093f2016-07-26 15:23:28 -0700514static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700515{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700516 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700517}
518
Minchan Kimbfd093f2016-07-26 15:23:28 -0700519static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700520{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700521 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700522}
523
Minchan Kim37836892016-07-26 15:23:23 -0700524static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700525 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600526 enum fullness_group *fullness)
527{
Minchan Kim48b48002016-07-26 15:23:31 -0700528 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
529
Minchan Kim37836892016-07-26 15:23:23 -0700530 *fullness = zspage->fullness;
531 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600532}
533
Minchan Kim37836892016-07-26 15:23:23 -0700534static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700535 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600536 enum fullness_group fullness)
537{
Minchan Kim37836892016-07-26 15:23:23 -0700538 zspage->class = class_idx;
539 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600540}
541
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900542/*
543 * zsmalloc divides the pool into various size classes where each
544 * class maintains a list of zspages where each zspage is divided
545 * into equal sized chunks. Each allocation falls into one of these
546 * classes depending on its size. This function returns index of the
547 * size class which has chunk size big enough to hold the give size.
548 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600549static int get_size_class_index(int size)
550{
551 int idx = 0;
552
553 if (likely(size > ZS_MIN_ALLOC_SIZE))
554 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
555 ZS_SIZE_CLASS_DELTA);
556
Minchan Kim7b60a682015-04-15 16:15:39 -0700557 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600558}
559
Minchan Kim248ca1b2015-04-15 16:15:42 -0700560static inline void zs_stat_inc(struct size_class *class,
561 enum zs_stat_type type, unsigned long cnt)
562{
Minchan Kim48b48002016-07-26 15:23:31 -0700563 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700564}
565
566static inline void zs_stat_dec(struct size_class *class,
567 enum zs_stat_type type, unsigned long cnt)
568{
Minchan Kim48b48002016-07-26 15:23:31 -0700569 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700570}
571
572static inline unsigned long zs_stat_get(struct size_class *class,
573 enum zs_stat_type type)
574{
Minchan Kim48b48002016-07-26 15:23:31 -0700575 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700576}
577
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700578#ifdef CONFIG_ZSMALLOC_STAT
579
Dan Streetman4abaac92016-05-26 15:16:27 -0700580static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700581{
Dan Streetman4abaac92016-05-26 15:16:27 -0700582 if (!debugfs_initialized()) {
583 pr_warn("debugfs not available, stat dir not created\n");
584 return;
585 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700586
587 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
588 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700589 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700590}
591
592static void __exit zs_stat_exit(void)
593{
594 debugfs_remove_recursive(zs_stat_root);
595}
596
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700597static unsigned long zs_can_compact(struct size_class *class);
598
Minchan Kim248ca1b2015-04-15 16:15:42 -0700599static int zs_stats_size_show(struct seq_file *s, void *v)
600{
601 int i;
602 struct zs_pool *pool = s->private;
603 struct size_class *class;
604 int objs_per_zspage;
605 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700606 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700607 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
608 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700609 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700610
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700611 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700612 "class", "size", "almost_full", "almost_empty",
613 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700614 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700615
616 for (i = 0; i < zs_size_classes; i++) {
617 class = pool->size_class[i];
618
619 if (class->index != i)
620 continue;
621
622 spin_lock(&class->lock);
623 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
624 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
625 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
626 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700627 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700628 spin_unlock(&class->lock);
629
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700630 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700631 pages_used = obj_allocated / objs_per_zspage *
632 class->pages_per_zspage;
633
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700634 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
635 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700636 i, class->size, class_almost_full, class_almost_empty,
637 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700638 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700639
640 total_class_almost_full += class_almost_full;
641 total_class_almost_empty += class_almost_empty;
642 total_objs += obj_allocated;
643 total_used_objs += obj_used;
644 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700645 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700646 }
647
648 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700649 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700650 "Total", "", total_class_almost_full,
651 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700652 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700653
654 return 0;
655}
656
657static int zs_stats_size_open(struct inode *inode, struct file *file)
658{
659 return single_open(file, zs_stats_size_show, inode->i_private);
660}
661
662static const struct file_operations zs_stat_size_ops = {
663 .open = zs_stats_size_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
666 .release = single_release,
667};
668
Dan Streetmand34f6152016-05-20 16:59:56 -0700669static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700670{
671 struct dentry *entry;
672
Dan Streetman4abaac92016-05-26 15:16:27 -0700673 if (!zs_stat_root) {
674 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700675 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700676 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700677
678 entry = debugfs_create_dir(name, zs_stat_root);
679 if (!entry) {
680 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700681 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700682 }
683 pool->stat_dentry = entry;
684
685 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
686 pool->stat_dentry, pool, &zs_stat_size_ops);
687 if (!entry) {
688 pr_warn("%s: debugfs file entry <%s> creation failed\n",
689 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700690 debugfs_remove_recursive(pool->stat_dentry);
691 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700692 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700693}
694
695static void zs_pool_stat_destroy(struct zs_pool *pool)
696{
697 debugfs_remove_recursive(pool->stat_dentry);
698}
699
700#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700701static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700702{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700703}
704
705static void __exit zs_stat_exit(void)
706{
707}
708
Dan Streetmand34f6152016-05-20 16:59:56 -0700709static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700710{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700711}
712
713static inline void zs_pool_stat_destroy(struct zs_pool *pool)
714{
715}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700716#endif
717
Minchan Kim48b48002016-07-26 15:23:31 -0700718
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900719/*
720 * For each size class, zspages are divided into different groups
721 * depending on how "full" they are. This was done so that we could
722 * easily find empty or nearly empty zspages when we try to shrink
723 * the pool (not yet implemented). This function returns fullness
724 * status of the given page.
725 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700726static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700727 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600728{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700729 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600730 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700731
Minchan Kim37836892016-07-26 15:23:23 -0700732 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700733 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600734
735 if (inuse == 0)
736 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700737 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600738 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700739 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600740 fg = ZS_ALMOST_EMPTY;
741 else
742 fg = ZS_ALMOST_FULL;
743
744 return fg;
745}
746
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900747/*
748 * Each size class maintains various freelists and zspages are assigned
749 * to one of these freelists based on the number of live objects they
750 * have. This functions inserts the given zspage into the freelist
751 * identified by <class, fullness_group>.
752 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700753static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700754 struct zspage *zspage,
755 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600756{
Minchan Kim37836892016-07-26 15:23:23 -0700757 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600758
Minchan Kim48b48002016-07-26 15:23:31 -0700759 zs_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700760 head = list_first_entry_or_null(&class->fullness_list[fullness],
761 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700762 /*
Minchan Kim37836892016-07-26 15:23:23 -0700763 * We want to see more ZS_FULL pages and less almost empty/full.
764 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700765 */
Minchan Kim37836892016-07-26 15:23:23 -0700766 if (head) {
767 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
768 list_add(&zspage->list, &head->list);
769 return;
770 }
771 }
772 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600773}
774
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900775/*
776 * This function removes the given zspage from the freelist identified
777 * by <class, fullness_group>.
778 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700779static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700780 struct zspage *zspage,
781 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600782{
Minchan Kim37836892016-07-26 15:23:23 -0700783 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Minchan Kim48b48002016-07-26 15:23:31 -0700784 VM_BUG_ON(is_zspage_isolated(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600785
Minchan Kim37836892016-07-26 15:23:23 -0700786 list_del_init(&zspage->list);
Minchan Kim48b48002016-07-26 15:23:31 -0700787 zs_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600788}
789
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900790/*
791 * Each size class maintains zspages in different fullness groups depending
792 * on the number of live objects they contain. When allocating or freeing
793 * objects, the fullness status of the page can change, say, from ALMOST_FULL
794 * to ALMOST_EMPTY when freeing an object. This function checks if such
795 * a status change has occurred for the given page and accordingly moves the
796 * page from the freelist of the old fullness group to that of the new
797 * fullness group.
798 */
Minchan Kimc7806262015-04-15 16:15:26 -0700799static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700800 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600801{
802 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600803 enum fullness_group currfg, newfg;
804
Minchan Kim37836892016-07-26 15:23:23 -0700805 get_zspage_mapping(zspage, &class_idx, &currfg);
806 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600807 if (newfg == currfg)
808 goto out;
809
Minchan Kim48b48002016-07-26 15:23:31 -0700810 if (!is_zspage_isolated(zspage)) {
811 remove_zspage(class, zspage, currfg);
812 insert_zspage(class, zspage, newfg);
813 }
814
Minchan Kim37836892016-07-26 15:23:23 -0700815 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600816
817out:
818 return newfg;
819}
820
821/*
822 * We have to decide on how many pages to link together
823 * to form a zspage for each size class. This is important
824 * to reduce wastage due to unusable space left at end of
825 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700826 * wastage = Zp % class_size
827 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600828 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
829 *
830 * For example, for size class of 3/8 * PAGE_SIZE, we should
831 * link together 3 PAGE_SIZE sized pages to form a zspage
832 * since then we can perfectly fit in 8 such objects.
833 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900834static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600835{
836 int i, max_usedpc = 0;
837 /* zspage order which gives maximum used size per KB */
838 int max_usedpc_order = 1;
839
Seth Jennings84d4faa2012-03-05 11:33:21 -0600840 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600841 int zspage_size;
842 int waste, usedpc;
843
844 zspage_size = i * PAGE_SIZE;
845 waste = zspage_size % class_size;
846 usedpc = (zspage_size - waste) * 100 / zspage_size;
847
848 if (usedpc > max_usedpc) {
849 max_usedpc = usedpc;
850 max_usedpc_order = i;
851 }
852 }
853
854 return max_usedpc_order;
855}
856
Minchan Kim37836892016-07-26 15:23:23 -0700857static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600858{
Minchan Kim48b48002016-07-26 15:23:31 -0700859 struct zspage *zspage = (struct zspage *)page->private;
860
861 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
862 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600863}
864
865static struct page *get_next_page(struct page *page)
866{
Minchan Kim48b48002016-07-26 15:23:31 -0700867 if (unlikely(PageHugeObject(page)))
868 return NULL;
869
870 return page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600871}
872
Minchan Kimbfd093f2016-07-26 15:23:28 -0700873/**
874 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
875 * @page: page object resides in zspage
876 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800877 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700878static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700879 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600880{
Minchan Kim312fcae2015-04-15 16:15:30 -0700881 obj >>= OBJ_TAG_BITS;
882 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
883 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600884}
885
Minchan Kimbfd093f2016-07-26 15:23:28 -0700886/**
887 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
888 * @page: page object resides in zspage
889 * @obj_idx: object index
890 */
891static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
892{
893 unsigned long obj;
894
895 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
896 obj |= obj_idx & OBJ_INDEX_MASK;
897 obj <<= OBJ_TAG_BITS;
898
899 return obj;
900}
901
Minchan Kim2e40e162015-04-15 16:15:23 -0700902static unsigned long handle_to_obj(unsigned long handle)
903{
904 return *(unsigned long *)handle;
905}
906
Minchan Kim48b48002016-07-26 15:23:31 -0700907static unsigned long obj_to_head(struct page *page, void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700908{
Minchan Kim48b48002016-07-26 15:23:31 -0700909 if (unlikely(PageHugeObject(page))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700910 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700911 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700912 } else
913 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700914}
915
Minchan Kim48b48002016-07-26 15:23:31 -0700916static inline int testpin_tag(unsigned long handle)
917{
918 return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
919}
920
Minchan Kim312fcae2015-04-15 16:15:30 -0700921static inline int trypin_tag(unsigned long handle)
922{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700923 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700924}
925
926static void pin_tag(unsigned long handle)
927{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700928 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700929}
930
931static void unpin_tag(unsigned long handle)
932{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700933 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700934}
935
Nitin Guptaf4477e92012-04-02 09:13:56 -0500936static void reset_page(struct page *page)
937{
Minchan Kim48b48002016-07-26 15:23:31 -0700938 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700939 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500940 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700941 page_mapcount_reset(page);
942 ClearPageHugeObject(page);
943 page->freelist = NULL;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500944}
945
Minchan Kim48b48002016-07-26 15:23:31 -0700946/*
947 * To prevent zspage destroy during migration, zspage freeing should
948 * hold locks of all pages in the zspage.
949 */
950void lock_zspage(struct zspage *zspage)
951{
952 struct page *page = get_first_page(zspage);
953
954 do {
955 lock_page(page);
956 } while ((page = get_next_page(page)) != NULL);
957}
958
959int trylock_zspage(struct zspage *zspage)
960{
961 struct page *cursor, *fail;
962
963 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
964 get_next_page(cursor)) {
965 if (!trylock_page(cursor)) {
966 fail = cursor;
967 goto unlock;
968 }
969 }
970
971 return 1;
972unlock:
973 for (cursor = get_first_page(zspage); cursor != fail; cursor =
974 get_next_page(cursor))
975 unlock_page(cursor);
976
977 return 0;
978}
979
980static void __free_zspage(struct zs_pool *pool, struct size_class *class,
981 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600982{
Minchan Kim37836892016-07-26 15:23:23 -0700983 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -0700984 enum fullness_group fg;
985 unsigned int class_idx;
986
987 get_zspage_mapping(zspage, &class_idx, &fg);
988
989 assert_spin_locked(&class->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -0600990
Minchan Kim37836892016-07-26 15:23:23 -0700991 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -0700992 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -0600993
Minchan Kim48b48002016-07-26 15:23:31 -0700994 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -0700995 do {
Minchan Kim48b48002016-07-26 15:23:31 -0700996 VM_BUG_ON_PAGE(!PageLocked(page), page);
997 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -0700998 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700999 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001000 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001001 put_page(page);
1002 page = next;
1003 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001004
Minchan Kim37836892016-07-26 15:23:23 -07001005 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001006
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001007 zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001008 atomic_long_sub(class->pages_per_zspage,
1009 &pool->pages_allocated);
1010}
1011
1012static void free_zspage(struct zs_pool *pool, struct size_class *class,
1013 struct zspage *zspage)
1014{
1015 VM_BUG_ON(get_zspage_inuse(zspage));
1016 VM_BUG_ON(list_empty(&zspage->list));
1017
1018 if (!trylock_zspage(zspage)) {
1019 kick_deferred_free(pool);
1020 return;
1021 }
1022
1023 remove_zspage(class, zspage, ZS_EMPTY);
1024 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001025}
1026
1027/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001028static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001029{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001030 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001031 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001032 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001033
Nitin Gupta61989a82012-01-09 16:51:56 -06001034 while (page) {
1035 struct page *next_page;
1036 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001037 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001038
Minchan Kim37836892016-07-26 15:23:23 -07001039 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001040
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001041 vaddr = kmap_atomic(page);
1042 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001043
Dan Streetman5538c562014-10-09 15:30:01 -07001044 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001045 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001046 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001047 }
1048
1049 /*
1050 * We now come to the last (full or partial) object on this
1051 * page, which must point to the first object on the next
1052 * page (if present)
1053 */
1054 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001055 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001056 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001057 } else {
1058 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001059 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001060 * whether it's allocated object or not.
1061 */
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001062 link->next = -1 << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001063 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001064 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001065 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001066 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001067 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001068
Minchan Kimbfd093f2016-07-26 15:23:28 -07001069 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001070}
1071
Minchan Kim48b48002016-07-26 15:23:31 -07001072static void create_page_chain(struct size_class *class, struct zspage *zspage,
1073 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001074{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001075 int i;
1076 struct page *page;
1077 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001078 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001079
1080 /*
1081 * Allocate individual pages and link them together as:
Minchan Kim48b48002016-07-26 15:23:31 -07001082 * 1. all pages are linked together using page->freelist
Minchan Kim37836892016-07-26 15:23:23 -07001083 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001084 *
Minchan Kim37836892016-07-26 15:23:23 -07001085 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001086 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001087 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001088 for (i = 0; i < nr_pages; i++) {
1089 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001090 set_page_private(page, (unsigned long)zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001091 page->freelist = NULL;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001092 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001093 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001094 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001095 if (unlikely(class->objs_per_zspage == 1 &&
1096 class->pages_per_zspage == 1))
1097 SetPageHugeObject(page);
Minchan Kim37836892016-07-26 15:23:23 -07001098 } else {
Minchan Kim48b48002016-07-26 15:23:31 -07001099 prev_page->freelist = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001100 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001101 prev_page = page;
1102 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001103}
Nitin Gupta61989a82012-01-09 16:51:56 -06001104
Minchan Kimbdb0af72016-07-26 15:23:20 -07001105/*
1106 * Allocate a zspage for the given size class
1107 */
Minchan Kim37836892016-07-26 15:23:23 -07001108static struct zspage *alloc_zspage(struct zs_pool *pool,
1109 struct size_class *class,
1110 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001111{
1112 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001113 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001114 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1115
1116 if (!zspage)
1117 return NULL;
1118
1119 memset(zspage, 0, sizeof(struct zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001120 zspage->magic = ZSPAGE_MAGIC;
1121 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001122
Minchan Kimbdb0af72016-07-26 15:23:20 -07001123 for (i = 0; i < class->pages_per_zspage; i++) {
1124 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001125
Minchan Kim37836892016-07-26 15:23:23 -07001126 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001127 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001128 while (--i >= 0) {
1129 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001130 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001131 }
Minchan Kim37836892016-07-26 15:23:23 -07001132 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001133 return NULL;
1134 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001135
1136 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001137 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001138 }
1139
Minchan Kim48b48002016-07-26 15:23:31 -07001140 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001141 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001142
Minchan Kim37836892016-07-26 15:23:23 -07001143 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001144}
1145
Minchan Kim37836892016-07-26 15:23:23 -07001146static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001147{
1148 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001149 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001150
Minchan Kim48b48002016-07-26 15:23:31 -07001151 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001152 zspage = list_first_entry_or_null(&class->fullness_list[i],
1153 struct zspage, list);
1154 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001155 break;
1156 }
1157
Minchan Kim37836892016-07-26 15:23:23 -07001158 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001159}
1160
Minchan Kim1b945ae2013-12-11 11:04:36 +09001161#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001162static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f6019022012-07-02 16:15:49 -05001163{
Seth Jenningsf5536462012-07-18 11:55:56 -05001164 /*
1165 * Make sure we don't leak memory if a cpu UP notification
1166 * and zs_init() race and both call zs_cpu_up() on the same cpu
1167 */
1168 if (area->vm)
1169 return 0;
1170 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1171 if (!area->vm)
1172 return -ENOMEM;
1173 return 0;
1174}
1175
1176static inline void __zs_cpu_down(struct mapping_area *area)
1177{
1178 if (area->vm)
1179 free_vm_area(area->vm);
1180 area->vm = NULL;
1181}
1182
1183static inline void *__zs_map_object(struct mapping_area *area,
1184 struct page *pages[2], int off, int size)
1185{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001186 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001187 area->vm_addr = area->vm->addr;
1188 return area->vm_addr + off;
1189}
1190
1191static inline void __zs_unmap_object(struct mapping_area *area,
1192 struct page *pages[2], int off, int size)
1193{
1194 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001195
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001196 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001197}
1198
Minchan Kim1b945ae2013-12-11 11:04:36 +09001199#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001200
1201static inline int __zs_cpu_up(struct mapping_area *area)
1202{
1203 /*
1204 * Make sure we don't leak memory if a cpu UP notification
1205 * and zs_init() race and both call zs_cpu_up() on the same cpu
1206 */
1207 if (area->vm_buf)
1208 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001209 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001210 if (!area->vm_buf)
1211 return -ENOMEM;
1212 return 0;
1213}
1214
1215static inline void __zs_cpu_down(struct mapping_area *area)
1216{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001217 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001218 area->vm_buf = NULL;
1219}
1220
1221static void *__zs_map_object(struct mapping_area *area,
1222 struct page *pages[2], int off, int size)
1223{
Seth Jennings5f6019022012-07-02 16:15:49 -05001224 int sizes[2];
1225 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001226 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001227
Seth Jenningsf5536462012-07-18 11:55:56 -05001228 /* disable page faults to match kmap_atomic() return conditions */
1229 pagefault_disable();
1230
1231 /* no read fastpath */
1232 if (area->vm_mm == ZS_MM_WO)
1233 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001234
1235 sizes[0] = PAGE_SIZE - off;
1236 sizes[1] = size - sizes[0];
1237
Seth Jennings5f6019022012-07-02 16:15:49 -05001238 /* copy object to per-cpu buffer */
1239 addr = kmap_atomic(pages[0]);
1240 memcpy(buf, addr + off, sizes[0]);
1241 kunmap_atomic(addr);
1242 addr = kmap_atomic(pages[1]);
1243 memcpy(buf + sizes[0], addr, sizes[1]);
1244 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001245out:
1246 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001247}
1248
Seth Jenningsf5536462012-07-18 11:55:56 -05001249static void __zs_unmap_object(struct mapping_area *area,
1250 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001251{
Seth Jennings5f6019022012-07-02 16:15:49 -05001252 int sizes[2];
1253 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001254 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001255
Seth Jenningsf5536462012-07-18 11:55:56 -05001256 /* no write fastpath */
1257 if (area->vm_mm == ZS_MM_RO)
1258 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001259
Minchan Kim7b60a682015-04-15 16:15:39 -07001260 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001261 buf = buf + ZS_HANDLE_SIZE;
1262 size -= ZS_HANDLE_SIZE;
1263 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001264
Seth Jennings5f6019022012-07-02 16:15:49 -05001265 sizes[0] = PAGE_SIZE - off;
1266 sizes[1] = size - sizes[0];
1267
1268 /* copy per-cpu buffer to object */
1269 addr = kmap_atomic(pages[0]);
1270 memcpy(addr + off, buf, sizes[0]);
1271 kunmap_atomic(addr);
1272 addr = kmap_atomic(pages[1]);
1273 memcpy(addr, buf + sizes[0], sizes[1]);
1274 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001275
1276out:
1277 /* enable page faults to match kunmap_atomic() return conditions */
1278 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001279}
Nitin Gupta61989a82012-01-09 16:51:56 -06001280
Minchan Kim1b945ae2013-12-11 11:04:36 +09001281#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001282
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001283static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001284{
Nitin Gupta61989a82012-01-09 16:51:56 -06001285 struct mapping_area *area;
1286
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001287 area = &per_cpu(zs_map_area, cpu);
1288 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001289}
1290
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001291static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001292{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001293 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001294
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001295 area = &per_cpu(zs_map_area, cpu);
1296 __zs_cpu_down(area);
1297 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001298}
1299
Ganesh Mahendran35b34452016-07-28 15:47:57 -07001300static void __init init_zs_size_classes(void)
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001301{
1302 int nr;
1303
1304 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1305 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1306 nr += 1;
1307
1308 zs_size_classes = nr;
1309}
1310
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001311static bool can_merge(struct size_class *prev, int pages_per_zspage,
1312 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001313{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001314 if (prev->pages_per_zspage == pages_per_zspage &&
1315 prev->objs_per_zspage == objs_per_zspage)
1316 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001317
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001318 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001319}
1320
Minchan Kim37836892016-07-26 15:23:23 -07001321static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001322{
Minchan Kim37836892016-07-26 15:23:23 -07001323 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001324}
1325
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001326unsigned long zs_get_total_pages(struct zs_pool *pool)
1327{
1328 return atomic_long_read(&pool->pages_allocated);
1329}
1330EXPORT_SYMBOL_GPL(zs_get_total_pages);
1331
1332/**
1333 * zs_map_object - get address of allocated object from handle.
1334 * @pool: pool from which the object was allocated
1335 * @handle: handle returned from zs_malloc
1336 *
1337 * Before using an object allocated from zs_malloc, it must be mapped using
1338 * this function. When done with the object, it must be unmapped using
1339 * zs_unmap_object.
1340 *
1341 * Only one object can be mapped per cpu at a time. There is no protection
1342 * against nested mappings.
1343 *
1344 * This function returns with preemption and page faults disabled.
1345 */
1346void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1347 enum zs_mapmode mm)
1348{
Minchan Kim37836892016-07-26 15:23:23 -07001349 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001350 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001351 unsigned long obj, off;
1352 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001353
1354 unsigned int class_idx;
1355 enum fullness_group fg;
1356 struct size_class *class;
1357 struct mapping_area *area;
1358 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001359 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001360
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001361 /*
1362 * Because we use per-cpu mapping areas shared among the
1363 * pools/users, we can't allow mapping in interrupt context
1364 * because it can corrupt another users mappings.
1365 */
Minchan Kim830e4bc2016-05-20 16:59:39 -07001366 WARN_ON_ONCE(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001367
Minchan Kim312fcae2015-04-15 16:15:30 -07001368 /* From now on, migration cannot move the object */
1369 pin_tag(handle);
1370
Minchan Kim2e40e162015-04-15 16:15:23 -07001371 obj = handle_to_obj(handle);
1372 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001373 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001374
1375 /* migration cannot move any subpage in this zspage */
1376 migrate_read_lock(zspage);
1377
Minchan Kim37836892016-07-26 15:23:23 -07001378 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001379 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001380 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001381
1382 area = &get_cpu_var(zs_map_area);
1383 area->vm_mm = mm;
1384 if (off + class->size <= PAGE_SIZE) {
1385 /* this object is contained entirely within a page */
1386 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001387 ret = area->vm_addr + off;
1388 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001389 }
1390
1391 /* this object spans two pages */
1392 pages[0] = page;
1393 pages[1] = get_next_page(page);
1394 BUG_ON(!pages[1]);
1395
Minchan Kim2e40e162015-04-15 16:15:23 -07001396 ret = __zs_map_object(area, pages, off, class->size);
1397out:
Minchan Kim48b48002016-07-26 15:23:31 -07001398 if (likely(!PageHugeObject(page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001399 ret += ZS_HANDLE_SIZE;
1400
1401 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001402}
1403EXPORT_SYMBOL_GPL(zs_map_object);
1404
1405void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1406{
Minchan Kim37836892016-07-26 15:23:23 -07001407 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001408 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001409 unsigned long obj, off;
1410 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001411
1412 unsigned int class_idx;
1413 enum fullness_group fg;
1414 struct size_class *class;
1415 struct mapping_area *area;
1416
Minchan Kim2e40e162015-04-15 16:15:23 -07001417 obj = handle_to_obj(handle);
1418 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001419 zspage = get_zspage(page);
1420 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001421 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001422 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001423
1424 area = this_cpu_ptr(&zs_map_area);
1425 if (off + class->size <= PAGE_SIZE)
1426 kunmap_atomic(area->vm_addr);
1427 else {
1428 struct page *pages[2];
1429
1430 pages[0] = page;
1431 pages[1] = get_next_page(page);
1432 BUG_ON(!pages[1]);
1433
1434 __zs_unmap_object(area, pages, off, class->size);
1435 }
1436 put_cpu_var(zs_map_area);
Minchan Kim48b48002016-07-26 15:23:31 -07001437
1438 migrate_read_unlock(zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001439 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001440}
1441EXPORT_SYMBOL_GPL(zs_unmap_object);
1442
Minchan Kim251cbb92016-05-20 16:59:42 -07001443static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001444 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001445{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001446 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001447 unsigned long obj;
1448 struct link_free *link;
1449
1450 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001451 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001452 void *vaddr;
1453
Minchan Kim312fcae2015-04-15 16:15:30 -07001454 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001455 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001456
1457 offset = obj * class->size;
1458 nr_page = offset >> PAGE_SHIFT;
1459 m_offset = offset & ~PAGE_MASK;
1460 m_page = get_first_page(zspage);
1461
1462 for (i = 0; i < nr_page; i++)
1463 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001464
1465 vaddr = kmap_atomic(m_page);
1466 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001467 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kim48b48002016-07-26 15:23:31 -07001468 if (likely(!PageHugeObject(m_page)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001469 /* record handle in the header of allocated chunk */
1470 link->handle = handle;
1471 else
Minchan Kim37836892016-07-26 15:23:23 -07001472 /* record handle to page->index */
1473 zspage->first_page->index = handle;
1474
Minchan Kimc7806262015-04-15 16:15:26 -07001475 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001476 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001477 zs_stat_inc(class, OBJ_USED, 1);
1478
Minchan Kimbfd093f2016-07-26 15:23:28 -07001479 obj = location_to_obj(m_page, obj);
1480
Minchan Kimc7806262015-04-15 16:15:26 -07001481 return obj;
1482}
1483
1484
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001485/**
1486 * zs_malloc - Allocate block of given size from pool.
1487 * @pool: pool to allocate from
1488 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001489 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001490 *
1491 * On success, handle to the allocated object is returned,
1492 * otherwise 0.
1493 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1494 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001495unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001496{
Minchan Kim2e40e162015-04-15 16:15:23 -07001497 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001498 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001499 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001500 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001501
Minchan Kim7b60a682015-04-15 16:15:39 -07001502 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001503 return 0;
1504
Minchan Kim37836892016-07-26 15:23:23 -07001505 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001506 if (!handle)
1507 return 0;
1508
1509 /* extra space in chunk to keep the handle */
1510 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001511 class = pool->size_class[get_size_class_index(size)];
1512
1513 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001514 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001515 if (likely(zspage)) {
1516 obj = obj_malloc(class, zspage, handle);
1517 /* Now move the zspage to another fullness group, if required */
1518 fix_fullness_group(class, zspage);
1519 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001520 spin_unlock(&class->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001521
Minchan Kim48b48002016-07-26 15:23:31 -07001522 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001523 }
1524
Minchan Kim48b48002016-07-26 15:23:31 -07001525 spin_unlock(&class->lock);
1526
1527 zspage = alloc_zspage(pool, class, gfp);
1528 if (!zspage) {
1529 cache_free_handle(pool, handle);
1530 return 0;
1531 }
1532
1533 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001534 obj = obj_malloc(class, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001535 newfg = get_fullness_group(class, zspage);
1536 insert_zspage(class, zspage, newfg);
1537 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001538 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001539 atomic_long_add(class->pages_per_zspage,
1540 &pool->pages_allocated);
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07001541 zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001542
1543 /* We completely set up zspage so mark them as movable */
1544 SetZsPageMovable(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001545 spin_unlock(&class->lock);
1546
Minchan Kim2e40e162015-04-15 16:15:23 -07001547 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001548}
1549EXPORT_SYMBOL_GPL(zs_malloc);
1550
Minchan Kim1ee47162016-05-20 16:59:45 -07001551static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001552{
1553 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001554 struct zspage *zspage;
1555 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001556 unsigned long f_offset;
1557 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001558 void *vaddr;
1559
Minchan Kim312fcae2015-04-15 16:15:30 -07001560 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001561 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001562 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001563 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001564
Minchan Kimc7806262015-04-15 16:15:26 -07001565 vaddr = kmap_atomic(f_page);
1566
1567 /* Insert this object in containing zspage's freelist */
1568 link = (struct link_free *)(vaddr + f_offset);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001569 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
Minchan Kimc7806262015-04-15 16:15:26 -07001570 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001571 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001572 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001573 zs_stat_dec(class, OBJ_USED, 1);
1574}
1575
1576void zs_free(struct zs_pool *pool, unsigned long handle)
1577{
Minchan Kim37836892016-07-26 15:23:23 -07001578 struct zspage *zspage;
1579 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001580 unsigned long obj;
1581 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001582 int class_idx;
1583 struct size_class *class;
1584 enum fullness_group fullness;
Minchan Kim48b48002016-07-26 15:23:31 -07001585 bool isolated;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001586
Minchan Kim2e40e162015-04-15 16:15:23 -07001587 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001588 return;
1589
Minchan Kim312fcae2015-04-15 16:15:30 -07001590 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001591 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001592 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001593 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001594
Minchan Kim48b48002016-07-26 15:23:31 -07001595 migrate_read_lock(zspage);
1596
Minchan Kim37836892016-07-26 15:23:23 -07001597 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001598 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001599
1600 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001601 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001602 fullness = fix_fullness_group(class, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001603 if (fullness != ZS_EMPTY) {
1604 migrate_read_unlock(zspage);
1605 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001606 }
Minchan Kim48b48002016-07-26 15:23:31 -07001607
1608 isolated = is_zspage_isolated(zspage);
1609 migrate_read_unlock(zspage);
1610 /* If zspage is isolated, zs_page_putback will free the zspage */
1611 if (likely(!isolated))
1612 free_zspage(pool, class, zspage);
1613out:
1614
Minchan Kim312fcae2015-04-15 16:15:30 -07001615 spin_unlock(&class->lock);
1616 unpin_tag(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001617 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001618}
1619EXPORT_SYMBOL_GPL(zs_free);
1620
Minchan Kim251cbb92016-05-20 16:59:42 -07001621static void zs_object_copy(struct size_class *class, unsigned long dst,
1622 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001623{
1624 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001625 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001626 unsigned long s_off, d_off;
1627 void *s_addr, *d_addr;
1628 int s_size, d_size, size;
1629 int written = 0;
1630
1631 s_size = d_size = class->size;
1632
1633 obj_to_location(src, &s_page, &s_objidx);
1634 obj_to_location(dst, &d_page, &d_objidx);
1635
Minchan Kimbfd093f2016-07-26 15:23:28 -07001636 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1637 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001638
1639 if (s_off + class->size > PAGE_SIZE)
1640 s_size = PAGE_SIZE - s_off;
1641
1642 if (d_off + class->size > PAGE_SIZE)
1643 d_size = PAGE_SIZE - d_off;
1644
1645 s_addr = kmap_atomic(s_page);
1646 d_addr = kmap_atomic(d_page);
1647
1648 while (1) {
1649 size = min(s_size, d_size);
1650 memcpy(d_addr + d_off, s_addr + s_off, size);
1651 written += size;
1652
1653 if (written == class->size)
1654 break;
1655
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001656 s_off += size;
1657 s_size -= size;
1658 d_off += size;
1659 d_size -= size;
1660
1661 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001662 kunmap_atomic(d_addr);
1663 kunmap_atomic(s_addr);
1664 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001665 s_addr = kmap_atomic(s_page);
1666 d_addr = kmap_atomic(d_page);
1667 s_size = class->size - written;
1668 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001669 }
1670
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001671 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001672 kunmap_atomic(d_addr);
1673 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001674 d_addr = kmap_atomic(d_page);
1675 d_size = class->size - written;
1676 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001677 }
1678 }
1679
1680 kunmap_atomic(d_addr);
1681 kunmap_atomic(s_addr);
1682}
1683
1684/*
1685 * Find alloced object in zspage from index object and
1686 * return handle.
1687 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001688static unsigned long find_alloced_obj(struct size_class *class,
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001689 struct page *page, int *obj_idx)
Minchan Kim312fcae2015-04-15 16:15:30 -07001690{
1691 unsigned long head;
1692 int offset = 0;
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001693 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001694 unsigned long handle = 0;
1695 void *addr = kmap_atomic(page);
1696
Minchan Kim37836892016-07-26 15:23:23 -07001697 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001698 offset += class->size * index;
1699
1700 while (offset < PAGE_SIZE) {
Minchan Kim48b48002016-07-26 15:23:31 -07001701 head = obj_to_head(page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001702 if (head & OBJ_ALLOCATED_TAG) {
1703 handle = head & ~OBJ_ALLOCATED_TAG;
1704 if (trypin_tag(handle))
1705 break;
1706 handle = 0;
1707 }
1708
1709 offset += class->size;
1710 index++;
1711 }
1712
1713 kunmap_atomic(addr);
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001714
1715 *obj_idx = index;
1716
Minchan Kim312fcae2015-04-15 16:15:30 -07001717 return handle;
1718}
1719
1720struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001721 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001722 struct page *s_page;
1723 /* Destination page for migration which should be a first page
1724 * of zspage. */
1725 struct page *d_page;
1726 /* Starting object index within @s_page which used for live object
1727 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001728 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001729};
1730
1731static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1732 struct zs_compact_control *cc)
1733{
1734 unsigned long used_obj, free_obj;
1735 unsigned long handle;
1736 struct page *s_page = cc->s_page;
1737 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001738 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001739 int ret = 0;
1740
1741 while (1) {
Ganesh Mahendrancf675ac2016-07-28 15:47:46 -07001742 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001743 if (!handle) {
1744 s_page = get_next_page(s_page);
1745 if (!s_page)
1746 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001747 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001748 continue;
1749 }
1750
1751 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001752 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001753 unpin_tag(handle);
1754 ret = -ENOMEM;
1755 break;
1756 }
1757
1758 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001759 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001760 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001761 obj_idx++;
Junil Leec102f07c2016-01-20 14:58:18 -08001762 /*
1763 * record_obj updates handle's value to free_obj and it will
1764 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1765 * breaks synchronization using pin_tag(e,g, zs_free) so
1766 * let's keep the lock bit.
1767 */
1768 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001769 record_obj(handle, free_obj);
1770 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001771 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001772 }
1773
1774 /* Remember last position in this iteration */
1775 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001776 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001777
1778 return ret;
1779}
1780
Minchan Kim37836892016-07-26 15:23:23 -07001781static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001782{
1783 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001784 struct zspage *zspage;
1785 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001786
Minchan Kim37836892016-07-26 15:23:23 -07001787 if (!source) {
1788 fg[0] = ZS_ALMOST_FULL;
1789 fg[1] = ZS_ALMOST_EMPTY;
1790 }
1791
1792 for (i = 0; i < 2; i++) {
1793 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1794 struct zspage, list);
1795 if (zspage) {
Minchan Kim48b48002016-07-26 15:23:31 -07001796 VM_BUG_ON(is_zspage_isolated(zspage));
Minchan Kim37836892016-07-26 15:23:23 -07001797 remove_zspage(class, zspage, fg[i]);
1798 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001799 }
1800 }
1801
Minchan Kim37836892016-07-26 15:23:23 -07001802 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001803}
1804
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001805/*
Minchan Kim37836892016-07-26 15:23:23 -07001806 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001807 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001808 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001809 *
Minchan Kim37836892016-07-26 15:23:23 -07001810 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001811 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001812static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001813 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001814{
Minchan Kim312fcae2015-04-15 16:15:30 -07001815 enum fullness_group fullness;
1816
Minchan Kim48b48002016-07-26 15:23:31 -07001817 VM_BUG_ON(is_zspage_isolated(zspage));
1818
Minchan Kim37836892016-07-26 15:23:23 -07001819 fullness = get_fullness_group(class, zspage);
1820 insert_zspage(class, zspage, fullness);
1821 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001822
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001823 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001824}
1825
Minchan Kim48b48002016-07-26 15:23:31 -07001826#ifdef CONFIG_COMPACTION
1827static struct dentry *zs_mount(struct file_system_type *fs_type,
1828 int flags, const char *dev_name, void *data)
1829{
1830 static const struct dentry_operations ops = {
1831 .d_dname = simple_dname,
1832 };
1833
1834 return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
1835}
1836
1837static struct file_system_type zsmalloc_fs = {
1838 .name = "zsmalloc",
1839 .mount = zs_mount,
1840 .kill_sb = kill_anon_super,
1841};
1842
1843static int zsmalloc_mount(void)
1844{
1845 int ret = 0;
1846
1847 zsmalloc_mnt = kern_mount(&zsmalloc_fs);
1848 if (IS_ERR(zsmalloc_mnt))
1849 ret = PTR_ERR(zsmalloc_mnt);
1850
1851 return ret;
1852}
1853
1854static void zsmalloc_unmount(void)
1855{
1856 kern_unmount(zsmalloc_mnt);
1857}
1858
1859static void migrate_lock_init(struct zspage *zspage)
1860{
1861 rwlock_init(&zspage->lock);
1862}
1863
1864static void migrate_read_lock(struct zspage *zspage)
1865{
1866 read_lock(&zspage->lock);
1867}
1868
1869static void migrate_read_unlock(struct zspage *zspage)
1870{
1871 read_unlock(&zspage->lock);
1872}
1873
1874static void migrate_write_lock(struct zspage *zspage)
1875{
1876 write_lock(&zspage->lock);
1877}
1878
1879static void migrate_write_unlock(struct zspage *zspage)
1880{
1881 write_unlock(&zspage->lock);
1882}
1883
1884/* Number of isolated subpage for *page migration* in this zspage */
1885static void inc_zspage_isolation(struct zspage *zspage)
1886{
1887 zspage->isolated++;
1888}
1889
1890static void dec_zspage_isolation(struct zspage *zspage)
1891{
1892 zspage->isolated--;
1893}
1894
1895static void replace_sub_page(struct size_class *class, struct zspage *zspage,
1896 struct page *newpage, struct page *oldpage)
1897{
1898 struct page *page;
1899 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
1900 int idx = 0;
1901
1902 page = get_first_page(zspage);
1903 do {
1904 if (page == oldpage)
1905 pages[idx] = newpage;
1906 else
1907 pages[idx] = page;
1908 idx++;
1909 } while ((page = get_next_page(page)) != NULL);
1910
1911 create_page_chain(class, zspage, pages);
1912 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
1913 if (unlikely(PageHugeObject(oldpage)))
1914 newpage->index = oldpage->index;
1915 __SetPageMovable(newpage, page_mapping(oldpage));
1916}
1917
1918bool zs_page_isolate(struct page *page, isolate_mode_t mode)
1919{
1920 struct zs_pool *pool;
1921 struct size_class *class;
1922 int class_idx;
1923 enum fullness_group fullness;
1924 struct zspage *zspage;
1925 struct address_space *mapping;
1926
1927 /*
1928 * Page is locked so zspage couldn't be destroyed. For detail, look at
1929 * lock_zspage in free_zspage.
1930 */
1931 VM_BUG_ON_PAGE(!PageMovable(page), page);
1932 VM_BUG_ON_PAGE(PageIsolated(page), page);
1933
1934 zspage = get_zspage(page);
1935
1936 /*
1937 * Without class lock, fullness could be stale while class_idx is okay
1938 * because class_idx is constant unless page is freed so we should get
1939 * fullness again under class lock.
1940 */
1941 get_zspage_mapping(zspage, &class_idx, &fullness);
1942 mapping = page_mapping(page);
1943 pool = mapping->private_data;
1944 class = pool->size_class[class_idx];
1945
1946 spin_lock(&class->lock);
1947 if (get_zspage_inuse(zspage) == 0) {
1948 spin_unlock(&class->lock);
1949 return false;
1950 }
1951
1952 /* zspage is isolated for object migration */
1953 if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1954 spin_unlock(&class->lock);
1955 return false;
1956 }
1957
1958 /*
1959 * If this is first time isolation for the zspage, isolate zspage from
1960 * size_class to prevent further object allocation from the zspage.
1961 */
1962 if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
1963 get_zspage_mapping(zspage, &class_idx, &fullness);
1964 remove_zspage(class, zspage, fullness);
1965 }
1966
1967 inc_zspage_isolation(zspage);
1968 spin_unlock(&class->lock);
1969
1970 return true;
1971}
1972
1973int zs_page_migrate(struct address_space *mapping, struct page *newpage,
1974 struct page *page, enum migrate_mode mode)
1975{
1976 struct zs_pool *pool;
1977 struct size_class *class;
1978 int class_idx;
1979 enum fullness_group fullness;
1980 struct zspage *zspage;
1981 struct page *dummy;
1982 void *s_addr, *d_addr, *addr;
1983 int offset, pos;
1984 unsigned long handle, head;
1985 unsigned long old_obj, new_obj;
1986 unsigned int obj_idx;
1987 int ret = -EAGAIN;
1988
1989 VM_BUG_ON_PAGE(!PageMovable(page), page);
1990 VM_BUG_ON_PAGE(!PageIsolated(page), page);
1991
1992 zspage = get_zspage(page);
1993
1994 /* Concurrent compactor cannot migrate any subpage in zspage */
1995 migrate_write_lock(zspage);
1996 get_zspage_mapping(zspage, &class_idx, &fullness);
1997 pool = mapping->private_data;
1998 class = pool->size_class[class_idx];
1999 offset = get_first_obj_offset(page);
2000
2001 spin_lock(&class->lock);
2002 if (!get_zspage_inuse(zspage)) {
2003 ret = -EBUSY;
2004 goto unlock_class;
2005 }
2006
2007 pos = offset;
2008 s_addr = kmap_atomic(page);
2009 while (pos < PAGE_SIZE) {
2010 head = obj_to_head(page, s_addr + pos);
2011 if (head & OBJ_ALLOCATED_TAG) {
2012 handle = head & ~OBJ_ALLOCATED_TAG;
2013 if (!trypin_tag(handle))
2014 goto unpin_objects;
2015 }
2016 pos += class->size;
2017 }
2018
2019 /*
2020 * Here, any user cannot access all objects in the zspage so let's move.
2021 */
2022 d_addr = kmap_atomic(newpage);
2023 memcpy(d_addr, s_addr, PAGE_SIZE);
2024 kunmap_atomic(d_addr);
2025
2026 for (addr = s_addr + offset; addr < s_addr + pos;
2027 addr += class->size) {
2028 head = obj_to_head(page, addr);
2029 if (head & OBJ_ALLOCATED_TAG) {
2030 handle = head & ~OBJ_ALLOCATED_TAG;
2031 if (!testpin_tag(handle))
2032 BUG();
2033
2034 old_obj = handle_to_obj(handle);
2035 obj_to_location(old_obj, &dummy, &obj_idx);
2036 new_obj = (unsigned long)location_to_obj(newpage,
2037 obj_idx);
2038 new_obj |= BIT(HANDLE_PIN_BIT);
2039 record_obj(handle, new_obj);
2040 }
2041 }
2042
2043 replace_sub_page(class, zspage, newpage, page);
2044 get_page(newpage);
2045
2046 dec_zspage_isolation(zspage);
2047
2048 /*
2049 * Page migration is done so let's putback isolated zspage to
2050 * the list if @page is final isolated subpage in the zspage.
2051 */
2052 if (!is_zspage_isolated(zspage))
2053 putback_zspage(class, zspage);
2054
2055 reset_page(page);
2056 put_page(page);
2057 page = newpage;
2058
Minchan Kimdd4123f2016-07-26 15:26:50 -07002059 ret = MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002060unpin_objects:
2061 for (addr = s_addr + offset; addr < s_addr + pos;
2062 addr += class->size) {
2063 head = obj_to_head(page, addr);
2064 if (head & OBJ_ALLOCATED_TAG) {
2065 handle = head & ~OBJ_ALLOCATED_TAG;
2066 if (!testpin_tag(handle))
2067 BUG();
2068 unpin_tag(handle);
2069 }
2070 }
2071 kunmap_atomic(s_addr);
2072unlock_class:
2073 spin_unlock(&class->lock);
2074 migrate_write_unlock(zspage);
2075
2076 return ret;
2077}
2078
2079void zs_page_putback(struct page *page)
2080{
2081 struct zs_pool *pool;
2082 struct size_class *class;
2083 int class_idx;
2084 enum fullness_group fg;
2085 struct address_space *mapping;
2086 struct zspage *zspage;
2087
2088 VM_BUG_ON_PAGE(!PageMovable(page), page);
2089 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2090
2091 zspage = get_zspage(page);
2092 get_zspage_mapping(zspage, &class_idx, &fg);
2093 mapping = page_mapping(page);
2094 pool = mapping->private_data;
2095 class = pool->size_class[class_idx];
2096
2097 spin_lock(&class->lock);
2098 dec_zspage_isolation(zspage);
2099 if (!is_zspage_isolated(zspage)) {
2100 fg = putback_zspage(class, zspage);
2101 /*
2102 * Due to page_lock, we cannot free zspage immediately
2103 * so let's defer.
2104 */
2105 if (fg == ZS_EMPTY)
2106 schedule_work(&pool->free_work);
2107 }
2108 spin_unlock(&class->lock);
2109}
2110
2111const struct address_space_operations zsmalloc_aops = {
2112 .isolate_page = zs_page_isolate,
2113 .migratepage = zs_page_migrate,
2114 .putback_page = zs_page_putback,
2115};
2116
2117static int zs_register_migration(struct zs_pool *pool)
2118{
2119 pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
2120 if (IS_ERR(pool->inode)) {
2121 pool->inode = NULL;
2122 return 1;
2123 }
2124
2125 pool->inode->i_mapping->private_data = pool;
2126 pool->inode->i_mapping->a_ops = &zsmalloc_aops;
2127 return 0;
2128}
2129
2130static void zs_unregister_migration(struct zs_pool *pool)
2131{
2132 flush_work(&pool->free_work);
Markus Elfringc3491ec2016-07-28 15:48:59 -07002133 iput(pool->inode);
Minchan Kim48b48002016-07-26 15:23:31 -07002134}
2135
2136/*
2137 * Caller should hold page_lock of all pages in the zspage
2138 * In here, we cannot use zspage meta data.
2139 */
2140static void async_free_zspage(struct work_struct *work)
2141{
2142 int i;
2143 struct size_class *class;
2144 unsigned int class_idx;
2145 enum fullness_group fullness;
2146 struct zspage *zspage, *tmp;
2147 LIST_HEAD(free_pages);
2148 struct zs_pool *pool = container_of(work, struct zs_pool,
2149 free_work);
2150
2151 for (i = 0; i < zs_size_classes; i++) {
2152 class = pool->size_class[i];
2153 if (class->index != i)
2154 continue;
2155
2156 spin_lock(&class->lock);
2157 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
2158 spin_unlock(&class->lock);
2159 }
2160
2161
2162 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2163 list_del(&zspage->list);
2164 lock_zspage(zspage);
2165
2166 get_zspage_mapping(zspage, &class_idx, &fullness);
2167 VM_BUG_ON(fullness != ZS_EMPTY);
2168 class = pool->size_class[class_idx];
2169 spin_lock(&class->lock);
2170 __free_zspage(pool, pool->size_class[class_idx], zspage);
2171 spin_unlock(&class->lock);
2172 }
2173};
2174
2175static void kick_deferred_free(struct zs_pool *pool)
2176{
2177 schedule_work(&pool->free_work);
2178}
2179
2180static void init_deferred_free(struct zs_pool *pool)
2181{
2182 INIT_WORK(&pool->free_work, async_free_zspage);
2183}
2184
2185static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2186{
2187 struct page *page = get_first_page(zspage);
2188
2189 do {
2190 WARN_ON(!trylock_page(page));
2191 __SetPageMovable(page, pool->inode->i_mapping);
2192 unlock_page(page);
2193 } while ((page = get_next_page(page)) != NULL);
2194}
2195#endif
2196
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002197/*
2198 *
2199 * Based on the number of unused allocated objects calculate
2200 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002201 */
2202static unsigned long zs_can_compact(struct size_class *class)
2203{
2204 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002205 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2206 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002207
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002208 if (obj_allocated <= obj_used)
2209 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002210
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002211 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002212 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002213
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002214 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002215}
2216
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002217static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002218{
Minchan Kim312fcae2015-04-15 16:15:30 -07002219 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002220 struct zspage *src_zspage;
2221 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07002222
Minchan Kim312fcae2015-04-15 16:15:30 -07002223 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002224 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002225
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002226 if (!zs_can_compact(class))
2227 break;
2228
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002229 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002230 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002231
Minchan Kim37836892016-07-26 15:23:23 -07002232 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kim48b48002016-07-26 15:23:31 -07002233 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002234 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002235 * If there is no more space in dst_page, resched
2236 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002237 */
2238 if (!migrate_zspage(pool, class, &cc))
2239 break;
2240
Minchan Kim4aa409c2016-07-26 15:23:26 -07002241 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002242 }
2243
2244 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002245 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002246 break;
2247
Minchan Kim4aa409c2016-07-26 15:23:26 -07002248 putback_zspage(class, dst_zspage);
2249 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kim48b48002016-07-26 15:23:31 -07002250 free_zspage(pool, class, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002251 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07002252 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002253 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002254 cond_resched();
2255 spin_lock(&class->lock);
2256 }
2257
Minchan Kim37836892016-07-26 15:23:23 -07002258 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07002259 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002260
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002261 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002262}
2263
2264unsigned long zs_compact(struct zs_pool *pool)
2265{
2266 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002267 struct size_class *class;
2268
2269 for (i = zs_size_classes - 1; i >= 0; i--) {
2270 class = pool->size_class[i];
2271 if (!class)
2272 continue;
2273 if (class->index != i)
2274 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002275 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002276 }
2277
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07002278 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07002279}
2280EXPORT_SYMBOL_GPL(zs_compact);
2281
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002282void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2283{
2284 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2285}
2286EXPORT_SYMBOL_GPL(zs_pool_stats);
2287
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002288static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2289 struct shrink_control *sc)
2290{
2291 unsigned long pages_freed;
2292 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2293 shrinker);
2294
2295 pages_freed = pool->stats.pages_compacted;
2296 /*
2297 * Compact classes and calculate compaction delta.
2298 * Can run concurrently with a manually triggered
2299 * (by user) compaction.
2300 */
2301 pages_freed = zs_compact(pool) - pages_freed;
2302
2303 return pages_freed ? pages_freed : SHRINK_STOP;
2304}
2305
2306static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2307 struct shrink_control *sc)
2308{
2309 int i;
2310 struct size_class *class;
2311 unsigned long pages_to_free = 0;
2312 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2313 shrinker);
2314
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002315 for (i = zs_size_classes - 1; i >= 0; i--) {
2316 class = pool->size_class[i];
2317 if (!class)
2318 continue;
2319 if (class->index != i)
2320 continue;
2321
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002322 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002323 }
2324
2325 return pages_to_free;
2326}
2327
2328static void zs_unregister_shrinker(struct zs_pool *pool)
2329{
2330 if (pool->shrinker_enabled) {
2331 unregister_shrinker(&pool->shrinker);
2332 pool->shrinker_enabled = false;
2333 }
2334}
2335
2336static int zs_register_shrinker(struct zs_pool *pool)
2337{
2338 pool->shrinker.scan_objects = zs_shrinker_scan;
2339 pool->shrinker.count_objects = zs_shrinker_count;
2340 pool->shrinker.batch = 0;
2341 pool->shrinker.seeks = DEFAULT_SEEKS;
2342
2343 return register_shrinker(&pool->shrinker);
2344}
2345
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002346/**
2347 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002348 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002349 *
2350 * This function must be called before anything when using
2351 * the zsmalloc allocator.
2352 *
2353 * On success, a pointer to the newly created pool is returned,
2354 * otherwise NULL.
2355 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002356struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002357{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002358 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002359 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002360 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002361
Ganesh Mahendran18136652014-12-12 16:57:10 -08002362 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002363 if (!pool)
2364 return NULL;
2365
Minchan Kim48b48002016-07-26 15:23:31 -07002366 init_deferred_free(pool);
Minchan Kim2e40e162015-04-15 16:15:23 -07002367 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
2368 GFP_KERNEL);
2369 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002370 kfree(pool);
2371 return NULL;
2372 }
2373
Minchan Kim2e40e162015-04-15 16:15:23 -07002374 pool->name = kstrdup(name, GFP_KERNEL);
2375 if (!pool->name)
2376 goto err;
2377
Minchan Kim37836892016-07-26 15:23:23 -07002378 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002379 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002380
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002381 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002382 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002383 * for merging should be larger or equal to current size.
2384 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002385 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002386 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002387 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002388 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002389 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002390 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002391
2392 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2393 if (size > ZS_MAX_ALLOC_SIZE)
2394 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002395 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002396 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002397
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002398 /*
2399 * size_class is used for normal zsmalloc operation such
2400 * as alloc/free for that size. Although it is natural that we
2401 * have one size_class for each size, there is a chance that we
2402 * can get more memory utilization if we use one size_class for
2403 * many different sizes whose size_class have same
2404 * characteristics. So, we makes size_class point to
2405 * previous size_class if possible.
2406 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002407 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002408 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002409 pool->size_class[i] = prev_class;
2410 continue;
2411 }
2412 }
2413
2414 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2415 if (!class)
2416 goto err;
2417
Nitin Gupta61989a82012-01-09 16:51:56 -06002418 class->size = size;
2419 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002420 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002421 class->objs_per_zspage = objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002422 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002423 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002424 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2425 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002426 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002427
2428 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002429 }
2430
Dan Streetmand34f6152016-05-20 16:59:56 -07002431 /* debug only, don't abort if it fails */
2432 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002433
Minchan Kim48b48002016-07-26 15:23:31 -07002434 if (zs_register_migration(pool))
2435 goto err;
2436
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002437 /*
2438 * Not critical, we still can use the pool
2439 * and user can trigger compaction manually.
2440 */
2441 if (zs_register_shrinker(pool) == 0)
2442 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06002443 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002444
2445err:
2446 zs_destroy_pool(pool);
2447 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002448}
2449EXPORT_SYMBOL_GPL(zs_create_pool);
2450
2451void zs_destroy_pool(struct zs_pool *pool)
2452{
2453 int i;
2454
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002455 zs_unregister_shrinker(pool);
Minchan Kim48b48002016-07-26 15:23:31 -07002456 zs_unregister_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002457 zs_pool_stat_destroy(pool);
2458
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002459 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002460 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002461 struct size_class *class = pool->size_class[i];
2462
2463 if (!class)
2464 continue;
2465
2466 if (class->index != i)
2467 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002468
Minchan Kim48b48002016-07-26 15:23:31 -07002469 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002470 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002471 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002472 class->size, fg);
2473 }
2474 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002475 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002476 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002477
Minchan Kim37836892016-07-26 15:23:23 -07002478 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002479 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002480 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002481 kfree(pool);
2482}
2483EXPORT_SYMBOL_GPL(zs_destroy_pool);
2484
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002485static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002486{
Minchan Kim48b48002016-07-26 15:23:31 -07002487 int ret;
2488
2489 ret = zsmalloc_mount();
2490 if (ret)
2491 goto out;
2492
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002493 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2494 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002495 if (ret)
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002496 goto hp_setup_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002497
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002498 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002499
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002500#ifdef CONFIG_ZPOOL
2501 zpool_register_driver(&zs_zpool_driver);
2502#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002503
Dan Streetman4abaac92016-05-26 15:16:27 -07002504 zs_stat_init();
2505
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002506 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002507
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002508hp_setup_fail:
Minchan Kim48b48002016-07-26 15:23:31 -07002509 zsmalloc_unmount();
2510out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002511 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002512}
Nitin Gupta61989a82012-01-09 16:51:56 -06002513
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002514static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002515{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002516#ifdef CONFIG_ZPOOL
2517 zpool_unregister_driver(&zs_zpool_driver);
2518#endif
Minchan Kim48b48002016-07-26 15:23:31 -07002519 zsmalloc_unmount();
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002520 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002521
2522 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002523}
Ben Hutchings069f1012012-06-20 02:31:11 +01002524
2525module_init(zs_init);
2526module_exit(zs_exit);
2527
2528MODULE_LICENSE("Dual BSD/GPL");
2529MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");