blob: c6fb543cfb989078b25649f2f06cff4d9eb98afa [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
20 * page->index: offset of the first object starting in this page.
21 * For the first page, this is always 0, so we use this field
22 * to store handle for huge object.
23 * page->next: links together all component pages of a zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
27 * PG_private2: identifies the last component page
28 *
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>
Seth Jennings0959c632012-08-08 15:12:17 +090052
53/*
54 * This must be power of 2 and greater than of equal to sizeof(link_free).
55 * These two conditions ensure that any 'struct link_free' itself doesn't
56 * span more than 1 page which avoids complex case of mapping 2 pages simply
57 * to restore link_free pointer values.
58 */
59#define ZS_ALIGN 8
60
61/*
62 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
63 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
64 */
65#define ZS_MAX_ZSPAGE_ORDER 2
66#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
67
Minchan Kim2e40e162015-04-15 16:15:23 -070068#define ZS_HANDLE_SIZE (sizeof(unsigned long))
69
Seth Jennings0959c632012-08-08 15:12:17 +090070/*
71 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090072 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090073 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070074 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090075 *
76 * This is made more complicated by various memory models and PAE.
77 */
78
79#ifndef MAX_PHYSMEM_BITS
80#ifdef CONFIG_HIGHMEM64G
81#define MAX_PHYSMEM_BITS 36
82#else /* !CONFIG_HIGHMEM64G */
83/*
84 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
85 * be PAGE_SHIFT
86 */
87#define MAX_PHYSMEM_BITS BITS_PER_LONG
88#endif
89#endif
90#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -070091
92/*
93 * Memory for allocating for handle keeps object position by
94 * encoding <page, obj_idx> and the encoded value has a room
95 * in least bit(ie, look at obj_to_location).
96 * We use the bit to synchronize between object access by
97 * user and migration.
98 */
99#define HANDLE_PIN_BIT 0
100
101/*
102 * Head in allocated object should have OBJ_ALLOCATED_TAG
103 * to identify the object was allocated or not.
104 * It's okay to add the status bit in the least bit because
105 * header keeps handle which is 4byte-aligned address so we
106 * have room for two bit at least.
107 */
108#define OBJ_ALLOCATED_TAG 1
109#define OBJ_TAG_BITS 1
110#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900111#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
112
113#define MAX(a, b) ((a) >= (b) ? (a) : (b))
114/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
115#define ZS_MIN_ALLOC_SIZE \
116 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700117/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700118#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900119
120/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700121 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900122 * trader-off here:
123 * - Large number of size classes is potentially wasteful as free page are
124 * spread across these classes
125 * - Small number of size classes causes large internal fragmentation
126 * - Probably its better to use specific size classes (empirically
127 * determined). NOTE: all those class sizes must be set as multiple of
128 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
129 *
130 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
131 * (reason above)
132 */
Minchan Kim37836892016-07-26 15:23:23 -0700133#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900134
135/*
136 * We do not maintain any list for completely empty or full pages
137 */
138enum fullness_group {
139 ZS_ALMOST_FULL,
140 ZS_ALMOST_EMPTY,
Seth Jennings0959c632012-08-08 15:12:17 +0900141 ZS_EMPTY,
142 ZS_FULL
143};
144
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800145enum zs_stat_type {
146 OBJ_ALLOCATED,
147 OBJ_USED,
Minchan Kim248ca1b2015-04-15 16:15:42 -0700148 CLASS_ALMOST_FULL,
149 CLASS_ALMOST_EMPTY,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800150};
151
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800152#ifdef CONFIG_ZSMALLOC_STAT
153#define NR_ZS_STAT_TYPE (CLASS_ALMOST_EMPTY + 1)
154#else
155#define NR_ZS_STAT_TYPE (OBJ_USED + 1)
156#endif
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
Seth Jennings0959c632012-08-08 15:12:17 +0900166/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800167 * number of size_classes
168 */
169static int zs_size_classes;
170
171/*
Seth Jennings0959c632012-08-08 15:12:17 +0900172 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
173 * n <= N / f, where
174 * n = number of allocated objects
175 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700176 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900177 *
178 * Similarly, we assign zspage to:
179 * ZS_ALMOST_FULL when n > N / f
180 * ZS_EMPTY when n == 0
181 * ZS_FULL when n == N
182 *
183 * (see: fix_fullness_group())
184 */
185static const int fullness_threshold_frac = 4;
186
187struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700188 spinlock_t lock;
Minchan Kim37836892016-07-26 15:23:23 -0700189 struct list_head fullness_list[2];
Seth Jennings0959c632012-08-08 15:12:17 +0900190 /*
191 * Size of objects stored in this class. Must be multiple
192 * of ZS_ALIGN.
193 */
194 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700195 int objs_per_zspage;
Seth Jennings0959c632012-08-08 15:12:17 +0900196 unsigned int index;
197
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700198 struct zs_size_stat stats;
199
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 Kim7b60a682015-04-15 16:15:39 -0700202 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
203 bool huge;
Seth Jennings0959c632012-08-08 15:12:17 +0900204};
205
206/*
207 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700208 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900209 *
210 * This must be power of 2 and less than or equal to ZS_ALIGN
211 */
212struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700213 union {
214 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700215 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700216 * It's valid for non-allocated object
217 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700218 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700219 /*
220 * Handle of allocated object.
221 */
222 unsigned long handle;
223 };
Seth Jennings0959c632012-08-08 15:12:17 +0900224};
225
226struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800227 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800228
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800229 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700230 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700231 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900232
Minchan Kim13de8932014-10-09 15:29:48 -0700233 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800234
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700235 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700236
237 /* Compact classes */
238 struct shrinker shrinker;
239 /*
240 * To signify that register_shrinker() was successful
241 * and unregister_shrinker() will not Oops.
242 */
243 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800244#ifdef CONFIG_ZSMALLOC_STAT
245 struct dentry *stat_dentry;
246#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900247};
Nitin Gupta61989a82012-01-09 16:51:56 -0600248
249/*
250 * A zspage's class index and fullness group
251 * are encoded in its (first)page->mapping
252 */
Minchan Kim37836892016-07-26 15:23:23 -0700253#define FULLNESS_BITS 2
254#define CLASS_BITS 8
Minchan Kim4f420472016-07-26 15:23:17 -0700255
Minchan Kim37836892016-07-26 15:23:23 -0700256struct zspage {
257 struct {
258 unsigned int fullness:FULLNESS_BITS;
259 unsigned int class:CLASS_BITS;
260 };
261 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700262 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700263 struct page *first_page;
264 struct list_head list; /* fullness list */
265};
Nitin Gupta61989a82012-01-09 16:51:56 -0600266
Seth Jenningsf5536462012-07-18 11:55:56 -0500267struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900268#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500269 struct vm_struct *vm; /* vm area for mapping object that span pages */
270#else
271 char *vm_buf; /* copy buffer for objects that span pages */
272#endif
273 char *vm_addr; /* address of kmap_atomic()'ed pages */
274 enum zs_mapmode vm_mm; /* mapping mode */
275};
276
Minchan Kim37836892016-07-26 15:23:23 -0700277static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700278{
279 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
280 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700281 if (!pool->handle_cachep)
282 return 1;
283
284 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
285 0, 0, NULL);
286 if (!pool->zspage_cachep) {
287 kmem_cache_destroy(pool->handle_cachep);
288 pool->handle_cachep = NULL;
289 return 1;
290 }
291
292 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700293}
294
Minchan Kim37836892016-07-26 15:23:23 -0700295static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700296{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700297 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700298 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700299}
300
Minchan Kim37836892016-07-26 15:23:23 -0700301static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700302{
303 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700304 gfp & ~__GFP_HIGHMEM);
Minchan Kim2e40e162015-04-15 16:15:23 -0700305}
306
Minchan Kim37836892016-07-26 15:23:23 -0700307static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700308{
309 kmem_cache_free(pool->handle_cachep, (void *)handle);
310}
311
Minchan Kim37836892016-07-26 15:23:23 -0700312static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
313{
314 return kmem_cache_alloc(pool->zspage_cachep, flags & ~__GFP_HIGHMEM);
315};
316
317static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
318{
319 kmem_cache_free(pool->zspage_cachep, zspage);
320}
321
Minchan Kim2e40e162015-04-15 16:15:23 -0700322static void record_obj(unsigned long handle, unsigned long obj)
323{
Junil Leec102f072016-01-20 14:58:18 -0800324 /*
325 * lsb of @obj represents handle lock while other bits
326 * represent object value the handle is pointing so
327 * updating shouldn't do store tearing.
328 */
329 WRITE_ONCE(*(unsigned long *)handle, obj);
Minchan Kim2e40e162015-04-15 16:15:23 -0700330}
331
Dan Streetmanc7957792014-08-06 16:08:38 -0700332/* zpool driver */
333
334#ifdef CONFIG_ZPOOL
335
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800336static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700337 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700338 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700339{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700340 /*
341 * Ignore global gfp flags: zs_malloc() may be invoked from
342 * different contexts and its caller must provide a valid
343 * gfp mask.
344 */
345 return zs_create_pool(name);
Dan Streetmanc7957792014-08-06 16:08:38 -0700346}
347
348static void zs_zpool_destroy(void *pool)
349{
350 zs_destroy_pool(pool);
351}
352
353static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
354 unsigned long *handle)
355{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700356 *handle = zs_malloc(pool, size, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700357 return *handle ? 0 : -1;
358}
359static void zs_zpool_free(void *pool, unsigned long handle)
360{
361 zs_free(pool, handle);
362}
363
364static int zs_zpool_shrink(void *pool, unsigned int pages,
365 unsigned int *reclaimed)
366{
367 return -EINVAL;
368}
369
370static void *zs_zpool_map(void *pool, unsigned long handle,
371 enum zpool_mapmode mm)
372{
373 enum zs_mapmode zs_mm;
374
375 switch (mm) {
376 case ZPOOL_MM_RO:
377 zs_mm = ZS_MM_RO;
378 break;
379 case ZPOOL_MM_WO:
380 zs_mm = ZS_MM_WO;
381 break;
382 case ZPOOL_MM_RW: /* fallthru */
383 default:
384 zs_mm = ZS_MM_RW;
385 break;
386 }
387
388 return zs_map_object(pool, handle, zs_mm);
389}
390static void zs_zpool_unmap(void *pool, unsigned long handle)
391{
392 zs_unmap_object(pool, handle);
393}
394
395static u64 zs_zpool_total_size(void *pool)
396{
Minchan Kim722cdc12014-10-09 15:29:50 -0700397 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700398}
399
400static struct zpool_driver zs_zpool_driver = {
401 .type = "zsmalloc",
402 .owner = THIS_MODULE,
403 .create = zs_zpool_create,
404 .destroy = zs_zpool_destroy,
405 .malloc = zs_zpool_malloc,
406 .free = zs_zpool_free,
407 .shrink = zs_zpool_shrink,
408 .map = zs_zpool_map,
409 .unmap = zs_zpool_unmap,
410 .total_size = zs_zpool_total_size,
411};
412
Kees Cook137f8cf2014-08-29 15:18:40 -0700413MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700414#endif /* CONFIG_ZPOOL */
415
Minchan Kim248ca1b2015-04-15 16:15:42 -0700416static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
417{
418 return pages_per_zspage * PAGE_SIZE / size;
419}
420
Nitin Gupta61989a82012-01-09 16:51:56 -0600421/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
422static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
423
424static int is_first_page(struct page *page)
425{
Minchan Kima27545bf2012-04-25 15:23:09 +0900426 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600427}
428
Minchan Kim37836892016-07-26 15:23:23 -0700429static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600430{
Minchan Kim37836892016-07-26 15:23:23 -0700431 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600432}
433
Minchan Kim37836892016-07-26 15:23:23 -0700434static inline void set_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700435{
Minchan Kim37836892016-07-26 15:23:23 -0700436 zspage->inuse = val;
Minchan Kim4f420472016-07-26 15:23:17 -0700437}
438
Minchan Kim37836892016-07-26 15:23:23 -0700439static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700440{
Minchan Kim37836892016-07-26 15:23:23 -0700441 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700442}
443
444static inline int get_first_obj_offset(struct page *page)
445{
Minchan Kim37836892016-07-26 15:23:23 -0700446 if (is_first_page(page))
447 return 0;
448
Minchan Kim4f420472016-07-26 15:23:17 -0700449 return page->index;
450}
451
452static inline void set_first_obj_offset(struct page *page, int offset)
453{
Minchan Kim37836892016-07-26 15:23:23 -0700454 if (is_first_page(page))
455 return;
456
Minchan Kim4f420472016-07-26 15:23:17 -0700457 page->index = offset;
458}
459
Minchan Kimbfd093f2016-07-26 15:23:28 -0700460static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700461{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700462 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700463}
464
Minchan Kimbfd093f2016-07-26 15:23:28 -0700465static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700466{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700467 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700468}
469
Minchan Kim37836892016-07-26 15:23:23 -0700470static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700471 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600472 enum fullness_group *fullness)
473{
Minchan Kim37836892016-07-26 15:23:23 -0700474 *fullness = zspage->fullness;
475 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600476}
477
Minchan Kim37836892016-07-26 15:23:23 -0700478static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700479 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600480 enum fullness_group fullness)
481{
Minchan Kim37836892016-07-26 15:23:23 -0700482 zspage->class = class_idx;
483 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600484}
485
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900486/*
487 * zsmalloc divides the pool into various size classes where each
488 * class maintains a list of zspages where each zspage is divided
489 * into equal sized chunks. Each allocation falls into one of these
490 * classes depending on its size. This function returns index of the
491 * size class which has chunk size big enough to hold the give size.
492 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600493static int get_size_class_index(int size)
494{
495 int idx = 0;
496
497 if (likely(size > ZS_MIN_ALLOC_SIZE))
498 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
499 ZS_SIZE_CLASS_DELTA);
500
Minchan Kim7b60a682015-04-15 16:15:39 -0700501 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600502}
503
Minchan Kim248ca1b2015-04-15 16:15:42 -0700504static inline void zs_stat_inc(struct size_class *class,
505 enum zs_stat_type type, unsigned long cnt)
506{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800507 if (type < NR_ZS_STAT_TYPE)
508 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700509}
510
511static inline void zs_stat_dec(struct size_class *class,
512 enum zs_stat_type type, unsigned long cnt)
513{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800514 if (type < NR_ZS_STAT_TYPE)
515 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700516}
517
518static inline unsigned long zs_stat_get(struct size_class *class,
519 enum zs_stat_type type)
520{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800521 if (type < NR_ZS_STAT_TYPE)
522 return class->stats.objs[type];
523 return 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700524}
525
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700526#ifdef CONFIG_ZSMALLOC_STAT
527
Dan Streetman4abaac92016-05-26 15:16:27 -0700528static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700529{
Dan Streetman4abaac92016-05-26 15:16:27 -0700530 if (!debugfs_initialized()) {
531 pr_warn("debugfs not available, stat dir not created\n");
532 return;
533 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700534
535 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
536 if (!zs_stat_root)
Dan Streetman4abaac92016-05-26 15:16:27 -0700537 pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700538}
539
540static void __exit zs_stat_exit(void)
541{
542 debugfs_remove_recursive(zs_stat_root);
543}
544
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700545static unsigned long zs_can_compact(struct size_class *class);
546
Minchan Kim248ca1b2015-04-15 16:15:42 -0700547static int zs_stats_size_show(struct seq_file *s, void *v)
548{
549 int i;
550 struct zs_pool *pool = s->private;
551 struct size_class *class;
552 int objs_per_zspage;
553 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700554 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700555 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
556 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700557 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700558
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700559 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700560 "class", "size", "almost_full", "almost_empty",
561 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700562 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700563
564 for (i = 0; i < zs_size_classes; i++) {
565 class = pool->size_class[i];
566
567 if (class->index != i)
568 continue;
569
570 spin_lock(&class->lock);
571 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
572 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
573 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
574 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700575 freeable = zs_can_compact(class);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700576 spin_unlock(&class->lock);
577
578 objs_per_zspage = get_maxobj_per_zspage(class->size,
579 class->pages_per_zspage);
580 pages_used = obj_allocated / objs_per_zspage *
581 class->pages_per_zspage;
582
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700583 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
584 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700585 i, class->size, class_almost_full, class_almost_empty,
586 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700587 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700588
589 total_class_almost_full += class_almost_full;
590 total_class_almost_empty += class_almost_empty;
591 total_objs += obj_allocated;
592 total_used_objs += obj_used;
593 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700594 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700595 }
596
597 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700598 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700599 "Total", "", total_class_almost_full,
600 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700601 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700602
603 return 0;
604}
605
606static int zs_stats_size_open(struct inode *inode, struct file *file)
607{
608 return single_open(file, zs_stats_size_show, inode->i_private);
609}
610
611static const struct file_operations zs_stat_size_ops = {
612 .open = zs_stats_size_open,
613 .read = seq_read,
614 .llseek = seq_lseek,
615 .release = single_release,
616};
617
Dan Streetmand34f6152016-05-20 16:59:56 -0700618static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619{
620 struct dentry *entry;
621
Dan Streetman4abaac92016-05-26 15:16:27 -0700622 if (!zs_stat_root) {
623 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700624 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700625 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700626
627 entry = debugfs_create_dir(name, zs_stat_root);
628 if (!entry) {
629 pr_warn("debugfs dir <%s> creation failed\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700630 return;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700631 }
632 pool->stat_dentry = entry;
633
634 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
635 pool->stat_dentry, pool, &zs_stat_size_ops);
636 if (!entry) {
637 pr_warn("%s: debugfs file entry <%s> creation failed\n",
638 name, "classes");
Dan Streetman4abaac92016-05-26 15:16:27 -0700639 debugfs_remove_recursive(pool->stat_dentry);
640 pool->stat_dentry = NULL;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700641 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700642}
643
644static void zs_pool_stat_destroy(struct zs_pool *pool)
645{
646 debugfs_remove_recursive(pool->stat_dentry);
647}
648
649#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700650static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700651{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700652}
653
654static void __exit zs_stat_exit(void)
655{
656}
657
Dan Streetmand34f6152016-05-20 16:59:56 -0700658static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700659{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700660}
661
662static inline void zs_pool_stat_destroy(struct zs_pool *pool)
663{
664}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700665#endif
666
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900667/*
668 * For each size class, zspages are divided into different groups
669 * depending on how "full" they are. This was done so that we could
670 * easily find empty or nearly empty zspages when we try to shrink
671 * the pool (not yet implemented). This function returns fullness
672 * status of the given page.
673 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700674static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700675 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600676{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700677 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600678 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700679
Minchan Kim37836892016-07-26 15:23:23 -0700680 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700681 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600682
683 if (inuse == 0)
684 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700685 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600686 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700687 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600688 fg = ZS_ALMOST_EMPTY;
689 else
690 fg = ZS_ALMOST_FULL;
691
692 return fg;
693}
694
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900695/*
696 * Each size class maintains various freelists and zspages are assigned
697 * to one of these freelists based on the number of live objects they
698 * have. This functions inserts the given zspage into the freelist
699 * identified by <class, fullness_group>.
700 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700701static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700702 struct zspage *zspage,
703 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600704{
Minchan Kim37836892016-07-26 15:23:23 -0700705 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600706
Minchan Kim37836892016-07-26 15:23:23 -0700707 if (fullness >= ZS_EMPTY)
Nitin Gupta61989a82012-01-09 16:51:56 -0600708 return;
709
Minchan Kim37836892016-07-26 15:23:23 -0700710 head = list_first_entry_or_null(&class->fullness_list[fullness],
711 struct zspage, list);
712
Minchan Kim248ca1b2015-04-15 16:15:42 -0700713 zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
714 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700715
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700716 /*
Minchan Kim37836892016-07-26 15:23:23 -0700717 * We want to see more ZS_FULL pages and less almost empty/full.
718 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700719 */
Minchan Kim37836892016-07-26 15:23:23 -0700720 if (head) {
721 if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
722 list_add(&zspage->list, &head->list);
723 return;
724 }
725 }
726 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600727}
728
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900729/*
730 * This function removes the given zspage from the freelist identified
731 * by <class, fullness_group>.
732 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700733static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700734 struct zspage *zspage,
735 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600736{
Minchan Kim37836892016-07-26 15:23:23 -0700737 if (fullness >= ZS_EMPTY)
Nitin Gupta61989a82012-01-09 16:51:56 -0600738 return;
739
Minchan Kim37836892016-07-26 15:23:23 -0700740 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Nitin Gupta61989a82012-01-09 16:51:56 -0600741
Minchan Kim37836892016-07-26 15:23:23 -0700742 list_del_init(&zspage->list);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700743 zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
744 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600745}
746
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900747/*
748 * Each size class maintains zspages in different fullness groups depending
749 * on the number of live objects they contain. When allocating or freeing
750 * objects, the fullness status of the page can change, say, from ALMOST_FULL
751 * to ALMOST_EMPTY when freeing an object. This function checks if such
752 * a status change has occurred for the given page and accordingly moves the
753 * page from the freelist of the old fullness group to that of the new
754 * fullness group.
755 */
Minchan Kimc7806262015-04-15 16:15:26 -0700756static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700757 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600758{
759 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600760 enum fullness_group currfg, newfg;
761
Minchan Kim37836892016-07-26 15:23:23 -0700762 get_zspage_mapping(zspage, &class_idx, &currfg);
763 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600764 if (newfg == currfg)
765 goto out;
766
Minchan Kim37836892016-07-26 15:23:23 -0700767 remove_zspage(class, zspage, currfg);
768 insert_zspage(class, zspage, newfg);
769 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600770
771out:
772 return newfg;
773}
774
775/*
776 * We have to decide on how many pages to link together
777 * to form a zspage for each size class. This is important
778 * to reduce wastage due to unusable space left at end of
779 * each zspage which is given as:
Yinghao Xie888fa372015-04-15 16:15:49 -0700780 * wastage = Zp % class_size
781 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600782 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
783 *
784 * For example, for size class of 3/8 * PAGE_SIZE, we should
785 * link together 3 PAGE_SIZE sized pages to form a zspage
786 * since then we can perfectly fit in 8 such objects.
787 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900788static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600789{
790 int i, max_usedpc = 0;
791 /* zspage order which gives maximum used size per KB */
792 int max_usedpc_order = 1;
793
Seth Jennings84d4faa2012-03-05 11:33:21 -0600794 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600795 int zspage_size;
796 int waste, usedpc;
797
798 zspage_size = i * PAGE_SIZE;
799 waste = zspage_size % class_size;
800 usedpc = (zspage_size - waste) * 100 / zspage_size;
801
802 if (usedpc > max_usedpc) {
803 max_usedpc = usedpc;
804 max_usedpc_order = i;
805 }
806 }
807
808 return max_usedpc_order;
809}
810
Minchan Kimbfd093f2016-07-26 15:23:28 -0700811static struct page *get_first_page(struct zspage *zspage)
812{
813 return zspage->first_page;
814}
Minchan Kim37836892016-07-26 15:23:23 -0700815
816static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600817{
Minchan Kim37836892016-07-26 15:23:23 -0700818 return (struct zspage *)page->private;
Nitin Gupta61989a82012-01-09 16:51:56 -0600819}
820
821static struct page *get_next_page(struct page *page)
822{
Minchan Kim37836892016-07-26 15:23:23 -0700823 return page->next;
Nitin Gupta61989a82012-01-09 16:51:56 -0600824}
825
Minchan Kimbfd093f2016-07-26 15:23:28 -0700826/**
827 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
828 * @page: page object resides in zspage
829 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800830 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700831static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700832 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600833{
Minchan Kim312fcae2015-04-15 16:15:30 -0700834 obj >>= OBJ_TAG_BITS;
835 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
836 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600837}
838
Minchan Kimbfd093f2016-07-26 15:23:28 -0700839/**
840 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
841 * @page: page object resides in zspage
842 * @obj_idx: object index
843 */
844static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
845{
846 unsigned long obj;
847
848 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
849 obj |= obj_idx & OBJ_INDEX_MASK;
850 obj <<= OBJ_TAG_BITS;
851
852 return obj;
853}
854
Minchan Kim2e40e162015-04-15 16:15:23 -0700855static unsigned long handle_to_obj(unsigned long handle)
856{
857 return *(unsigned long *)handle;
858}
859
Minchan Kim7b60a682015-04-15 16:15:39 -0700860static unsigned long obj_to_head(struct size_class *class, struct page *page,
861 void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700862{
Minchan Kim7b60a682015-04-15 16:15:39 -0700863 if (class->huge) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700864 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim37836892016-07-26 15:23:23 -0700865 return page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700866 } else
867 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700868}
869
Minchan Kim312fcae2015-04-15 16:15:30 -0700870static inline int trypin_tag(unsigned long handle)
871{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700872 return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700873}
874
875static void pin_tag(unsigned long handle)
876{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700877 bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700878}
879
880static void unpin_tag(unsigned long handle)
881{
Minchan Kim1b8320b2016-07-26 15:23:14 -0700882 bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
Minchan Kim312fcae2015-04-15 16:15:30 -0700883}
884
Nitin Guptaf4477e92012-04-02 09:13:56 -0500885static void reset_page(struct page *page)
886{
887 clear_bit(PG_private, &page->flags);
888 clear_bit(PG_private_2, &page->flags);
889 set_page_private(page, 0);
Minchan Kim37836892016-07-26 15:23:23 -0700890 page->index = 0;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500891}
892
Minchan Kim37836892016-07-26 15:23:23 -0700893static void free_zspage(struct zs_pool *pool, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600894{
Minchan Kim37836892016-07-26 15:23:23 -0700895 struct page *page, *next;
Nitin Gupta61989a82012-01-09 16:51:56 -0600896
Minchan Kim37836892016-07-26 15:23:23 -0700897 VM_BUG_ON(get_zspage_inuse(zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -0600898
Minchan Kim37836892016-07-26 15:23:23 -0700899 next = page = zspage->first_page;
900 do {
901 next = page->next;
902 reset_page(page);
903 put_page(page);
904 page = next;
905 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -0600906
Minchan Kim37836892016-07-26 15:23:23 -0700907 cache_free_zspage(pool, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600908}
909
910/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -0700911static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600912{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700913 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600914 unsigned long off = 0;
Minchan Kim37836892016-07-26 15:23:23 -0700915 struct page *page = zspage->first_page;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700916
Nitin Gupta61989a82012-01-09 16:51:56 -0600917 while (page) {
918 struct page *next_page;
919 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800920 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600921
Minchan Kim37836892016-07-26 15:23:23 -0700922 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -0600923
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800924 vaddr = kmap_atomic(page);
925 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600926
Dan Streetman5538c562014-10-09 15:30:01 -0700927 while ((off += class->size) < PAGE_SIZE) {
Minchan Kimbfd093f2016-07-26 15:23:28 -0700928 link->next = freeobj++ << OBJ_ALLOCATED_TAG;
Dan Streetman5538c562014-10-09 15:30:01 -0700929 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600930 }
931
932 /*
933 * We now come to the last (full or partial) object on this
934 * page, which must point to the first object on the next
935 * page (if present)
936 */
937 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -0700938 if (next_page) {
939 link->next = freeobj++ << OBJ_ALLOCATED_TAG;
940 } else {
941 /*
942 * Reset OBJ_ALLOCATED_TAG bit to last link to tell
943 * whether it's allocated object or not.
944 */
945 link->next = -1 << OBJ_ALLOCATED_TAG;
946 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800947 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -0600948 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -0700949 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -0600950 }
Minchan Kimbdb0af72016-07-26 15:23:20 -0700951
Minchan Kimbfd093f2016-07-26 15:23:28 -0700952 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -0600953}
954
Minchan Kim37836892016-07-26 15:23:23 -0700955static void create_page_chain(struct zspage *zspage, struct page *pages[],
956 int nr_pages)
Nitin Gupta61989a82012-01-09 16:51:56 -0600957{
Minchan Kimbdb0af72016-07-26 15:23:20 -0700958 int i;
959 struct page *page;
960 struct page *prev_page = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -0600961
962 /*
963 * Allocate individual pages and link them together as:
Minchan Kim37836892016-07-26 15:23:23 -0700964 * 1. all pages are linked together using page->next
965 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -0600966 *
Minchan Kim37836892016-07-26 15:23:23 -0700967 * we set PG_private to identify the first page (i.e. no other sub-page
968 * has this flag set) and PG_private_2 to identify the last page.
Nitin Gupta61989a82012-01-09 16:51:56 -0600969 */
Minchan Kimbdb0af72016-07-26 15:23:20 -0700970 for (i = 0; i < nr_pages; i++) {
971 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -0700972 set_page_private(page, (unsigned long)zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -0700973 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -0700974 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +0900975 SetPagePrivate(page);
Minchan Kim37836892016-07-26 15:23:23 -0700976 } else {
977 prev_page->next = page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600978 }
Minchan Kim37836892016-07-26 15:23:23 -0700979 if (i == nr_pages - 1) {
Minchan Kima27545bf2012-04-25 15:23:09 +0900980 SetPagePrivate2(page);
Minchan Kim37836892016-07-26 15:23:23 -0700981 page->next = NULL;
982 }
Nitin Gupta61989a82012-01-09 16:51:56 -0600983 prev_page = page;
984 }
Minchan Kimbdb0af72016-07-26 15:23:20 -0700985}
Nitin Gupta61989a82012-01-09 16:51:56 -0600986
Minchan Kimbdb0af72016-07-26 15:23:20 -0700987/*
988 * Allocate a zspage for the given size class
989 */
Minchan Kim37836892016-07-26 15:23:23 -0700990static struct zspage *alloc_zspage(struct zs_pool *pool,
991 struct size_class *class,
992 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -0700993{
994 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -0700995 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -0700996 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
997
998 if (!zspage)
999 return NULL;
1000
1001 memset(zspage, 0, sizeof(struct zspage));
Nitin Gupta61989a82012-01-09 16:51:56 -06001002
Minchan Kimbdb0af72016-07-26 15:23:20 -07001003 for (i = 0; i < class->pages_per_zspage; i++) {
1004 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001005
Minchan Kim37836892016-07-26 15:23:23 -07001006 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001007 if (!page) {
1008 while (--i >= 0)
1009 __free_page(pages[i]);
Minchan Kim37836892016-07-26 15:23:23 -07001010 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001011 return NULL;
1012 }
1013 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001014 }
1015
Minchan Kim37836892016-07-26 15:23:23 -07001016 create_page_chain(zspage, pages, class->pages_per_zspage);
1017 init_zspage(class, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001018
Minchan Kim37836892016-07-26 15:23:23 -07001019 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001020}
1021
Minchan Kim37836892016-07-26 15:23:23 -07001022static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001023{
1024 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001025 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001026
Minchan Kim37836892016-07-26 15:23:23 -07001027 for (i = ZS_ALMOST_FULL; i <= ZS_ALMOST_EMPTY; i++) {
1028 zspage = list_first_entry_or_null(&class->fullness_list[i],
1029 struct zspage, list);
1030 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001031 break;
1032 }
1033
Minchan Kim37836892016-07-26 15:23:23 -07001034 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001035}
1036
Minchan Kim1b945ae2013-12-11 11:04:36 +09001037#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001038static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001039{
Seth Jenningsf5536462012-07-18 11:55:56 -05001040 /*
1041 * Make sure we don't leak memory if a cpu UP notification
1042 * and zs_init() race and both call zs_cpu_up() on the same cpu
1043 */
1044 if (area->vm)
1045 return 0;
1046 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1047 if (!area->vm)
1048 return -ENOMEM;
1049 return 0;
1050}
1051
1052static inline void __zs_cpu_down(struct mapping_area *area)
1053{
1054 if (area->vm)
1055 free_vm_area(area->vm);
1056 area->vm = NULL;
1057}
1058
1059static inline void *__zs_map_object(struct mapping_area *area,
1060 struct page *pages[2], int off, int size)
1061{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001062 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001063 area->vm_addr = area->vm->addr;
1064 return area->vm_addr + off;
1065}
1066
1067static inline void __zs_unmap_object(struct mapping_area *area,
1068 struct page *pages[2], int off, int size)
1069{
1070 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001071
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001072 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001073}
1074
Minchan Kim1b945ae2013-12-11 11:04:36 +09001075#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001076
1077static inline int __zs_cpu_up(struct mapping_area *area)
1078{
1079 /*
1080 * Make sure we don't leak memory if a cpu UP notification
1081 * and zs_init() race and both call zs_cpu_up() on the same cpu
1082 */
1083 if (area->vm_buf)
1084 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001085 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001086 if (!area->vm_buf)
1087 return -ENOMEM;
1088 return 0;
1089}
1090
1091static inline void __zs_cpu_down(struct mapping_area *area)
1092{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001093 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001094 area->vm_buf = NULL;
1095}
1096
1097static void *__zs_map_object(struct mapping_area *area,
1098 struct page *pages[2], int off, int size)
1099{
Seth Jennings5f601902012-07-02 16:15:49 -05001100 int sizes[2];
1101 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001102 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001103
Seth Jenningsf5536462012-07-18 11:55:56 -05001104 /* disable page faults to match kmap_atomic() return conditions */
1105 pagefault_disable();
1106
1107 /* no read fastpath */
1108 if (area->vm_mm == ZS_MM_WO)
1109 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001110
1111 sizes[0] = PAGE_SIZE - off;
1112 sizes[1] = size - sizes[0];
1113
Seth Jennings5f601902012-07-02 16:15:49 -05001114 /* copy object to per-cpu buffer */
1115 addr = kmap_atomic(pages[0]);
1116 memcpy(buf, addr + off, sizes[0]);
1117 kunmap_atomic(addr);
1118 addr = kmap_atomic(pages[1]);
1119 memcpy(buf + sizes[0], addr, sizes[1]);
1120 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001121out:
1122 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001123}
1124
Seth Jenningsf5536462012-07-18 11:55:56 -05001125static void __zs_unmap_object(struct mapping_area *area,
1126 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001127{
Seth Jennings5f601902012-07-02 16:15:49 -05001128 int sizes[2];
1129 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001130 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001131
Seth Jenningsf5536462012-07-18 11:55:56 -05001132 /* no write fastpath */
1133 if (area->vm_mm == ZS_MM_RO)
1134 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001135
Minchan Kim7b60a682015-04-15 16:15:39 -07001136 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001137 buf = buf + ZS_HANDLE_SIZE;
1138 size -= ZS_HANDLE_SIZE;
1139 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001140
Seth Jennings5f601902012-07-02 16:15:49 -05001141 sizes[0] = PAGE_SIZE - off;
1142 sizes[1] = size - sizes[0];
1143
1144 /* copy per-cpu buffer to object */
1145 addr = kmap_atomic(pages[0]);
1146 memcpy(addr + off, buf, sizes[0]);
1147 kunmap_atomic(addr);
1148 addr = kmap_atomic(pages[1]);
1149 memcpy(addr, buf + sizes[0], sizes[1]);
1150 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001151
1152out:
1153 /* enable page faults to match kunmap_atomic() return conditions */
1154 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001155}
Nitin Gupta61989a82012-01-09 16:51:56 -06001156
Minchan Kim1b945ae2013-12-11 11:04:36 +09001157#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001158
Nitin Gupta61989a82012-01-09 16:51:56 -06001159static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1160 void *pcpu)
1161{
Seth Jenningsf5536462012-07-18 11:55:56 -05001162 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001163 struct mapping_area *area;
1164
1165 switch (action) {
1166 case CPU_UP_PREPARE:
1167 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001168 ret = __zs_cpu_up(area);
1169 if (ret)
1170 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001171 break;
1172 case CPU_DEAD:
1173 case CPU_UP_CANCELED:
1174 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001175 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001176 break;
1177 }
1178
1179 return NOTIFY_OK;
1180}
1181
1182static struct notifier_block zs_cpu_nb = {
1183 .notifier_call = zs_cpu_notifier
1184};
1185
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001186static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001187{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001188 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001189
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301190 cpu_notifier_register_begin();
1191
1192 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001193 for_each_online_cpu(cpu) {
1194 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001195 if (notifier_to_errno(ret))
1196 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001197 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301198
1199 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001200 return notifier_to_errno(ret);
1201}
1202
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001203static void zs_unregister_cpu_notifier(void)
1204{
1205 int cpu;
1206
1207 cpu_notifier_register_begin();
1208
1209 for_each_online_cpu(cpu)
1210 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1211 __unregister_cpu_notifier(&zs_cpu_nb);
1212
1213 cpu_notifier_register_done();
1214}
1215
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001216static void init_zs_size_classes(void)
1217{
1218 int nr;
1219
1220 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1221 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1222 nr += 1;
1223
1224 zs_size_classes = nr;
1225}
1226
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001227static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
1228{
1229 if (prev->pages_per_zspage != pages_per_zspage)
1230 return false;
1231
1232 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
1233 != get_maxobj_per_zspage(size, pages_per_zspage))
1234 return false;
1235
1236 return true;
1237}
1238
Minchan Kim37836892016-07-26 15:23:23 -07001239static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001240{
Minchan Kim37836892016-07-26 15:23:23 -07001241 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001242}
1243
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001244unsigned long zs_get_total_pages(struct zs_pool *pool)
1245{
1246 return atomic_long_read(&pool->pages_allocated);
1247}
1248EXPORT_SYMBOL_GPL(zs_get_total_pages);
1249
1250/**
1251 * zs_map_object - get address of allocated object from handle.
1252 * @pool: pool from which the object was allocated
1253 * @handle: handle returned from zs_malloc
1254 *
1255 * Before using an object allocated from zs_malloc, it must be mapped using
1256 * this function. When done with the object, it must be unmapped using
1257 * zs_unmap_object.
1258 *
1259 * Only one object can be mapped per cpu at a time. There is no protection
1260 * against nested mappings.
1261 *
1262 * This function returns with preemption and page faults disabled.
1263 */
1264void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1265 enum zs_mapmode mm)
1266{
Minchan Kim37836892016-07-26 15:23:23 -07001267 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001268 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001269 unsigned long obj, off;
1270 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001271
1272 unsigned int class_idx;
1273 enum fullness_group fg;
1274 struct size_class *class;
1275 struct mapping_area *area;
1276 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001277 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001278
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001279 /*
1280 * Because we use per-cpu mapping areas shared among the
1281 * pools/users, we can't allow mapping in interrupt context
1282 * because it can corrupt another users mappings.
1283 */
Minchan Kim830e4bc2016-05-20 16:59:39 -07001284 WARN_ON_ONCE(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001285
Minchan Kim312fcae2015-04-15 16:15:30 -07001286 /* From now on, migration cannot move the object */
1287 pin_tag(handle);
1288
Minchan Kim2e40e162015-04-15 16:15:23 -07001289 obj = handle_to_obj(handle);
1290 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001291 zspage = get_zspage(page);
1292 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001293 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001294 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001295
1296 area = &get_cpu_var(zs_map_area);
1297 area->vm_mm = mm;
1298 if (off + class->size <= PAGE_SIZE) {
1299 /* this object is contained entirely within a page */
1300 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001301 ret = area->vm_addr + off;
1302 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001303 }
1304
1305 /* this object spans two pages */
1306 pages[0] = page;
1307 pages[1] = get_next_page(page);
1308 BUG_ON(!pages[1]);
1309
Minchan Kim2e40e162015-04-15 16:15:23 -07001310 ret = __zs_map_object(area, pages, off, class->size);
1311out:
Minchan Kim7b60a682015-04-15 16:15:39 -07001312 if (!class->huge)
1313 ret += ZS_HANDLE_SIZE;
1314
1315 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001316}
1317EXPORT_SYMBOL_GPL(zs_map_object);
1318
1319void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1320{
Minchan Kim37836892016-07-26 15:23:23 -07001321 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001322 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001323 unsigned long obj, off;
1324 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001325
1326 unsigned int class_idx;
1327 enum fullness_group fg;
1328 struct size_class *class;
1329 struct mapping_area *area;
1330
Minchan Kim2e40e162015-04-15 16:15:23 -07001331 obj = handle_to_obj(handle);
1332 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001333 zspage = get_zspage(page);
1334 get_zspage_mapping(zspage, &class_idx, &fg);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001335 class = pool->size_class[class_idx];
Minchan Kimbfd093f2016-07-26 15:23:28 -07001336 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001337
1338 area = this_cpu_ptr(&zs_map_area);
1339 if (off + class->size <= PAGE_SIZE)
1340 kunmap_atomic(area->vm_addr);
1341 else {
1342 struct page *pages[2];
1343
1344 pages[0] = page;
1345 pages[1] = get_next_page(page);
1346 BUG_ON(!pages[1]);
1347
1348 __zs_unmap_object(area, pages, off, class->size);
1349 }
1350 put_cpu_var(zs_map_area);
Minchan Kim312fcae2015-04-15 16:15:30 -07001351 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001352}
1353EXPORT_SYMBOL_GPL(zs_unmap_object);
1354
Minchan Kim251cbb92016-05-20 16:59:42 -07001355static unsigned long obj_malloc(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001356 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001357{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001358 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001359 unsigned long obj;
1360 struct link_free *link;
1361
1362 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001363 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001364 void *vaddr;
1365
Minchan Kim312fcae2015-04-15 16:15:30 -07001366 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001367 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001368
1369 offset = obj * class->size;
1370 nr_page = offset >> PAGE_SHIFT;
1371 m_offset = offset & ~PAGE_MASK;
1372 m_page = get_first_page(zspage);
1373
1374 for (i = 0; i < nr_page; i++)
1375 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001376
1377 vaddr = kmap_atomic(m_page);
1378 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001379 set_freeobj(zspage, link->next >> OBJ_ALLOCATED_TAG);
Minchan Kim7b60a682015-04-15 16:15:39 -07001380 if (!class->huge)
1381 /* record handle in the header of allocated chunk */
1382 link->handle = handle;
1383 else
Minchan Kim37836892016-07-26 15:23:23 -07001384 /* record handle to page->index */
1385 zspage->first_page->index = handle;
1386
Minchan Kimc7806262015-04-15 16:15:26 -07001387 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001388 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001389 zs_stat_inc(class, OBJ_USED, 1);
1390
Minchan Kimbfd093f2016-07-26 15:23:28 -07001391 obj = location_to_obj(m_page, obj);
1392
Minchan Kimc7806262015-04-15 16:15:26 -07001393 return obj;
1394}
1395
1396
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001397/**
1398 * zs_malloc - Allocate block of given size from pool.
1399 * @pool: pool to allocate from
1400 * @size: size of block to allocate
1401 *
1402 * On success, handle to the allocated object is returned,
1403 * otherwise 0.
1404 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1405 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001406unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001407{
Minchan Kim2e40e162015-04-15 16:15:23 -07001408 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001409 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07001410 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001411
Minchan Kim7b60a682015-04-15 16:15:39 -07001412 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001413 return 0;
1414
Minchan Kim37836892016-07-26 15:23:23 -07001415 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001416 if (!handle)
1417 return 0;
1418
1419 /* extra space in chunk to keep the handle */
1420 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001421 class = pool->size_class[get_size_class_index(size)];
1422
1423 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001424 zspage = find_get_zspage(class);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001425
Minchan Kim37836892016-07-26 15:23:23 -07001426 if (!zspage) {
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001427 spin_unlock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001428 zspage = alloc_zspage(pool, class, gfp);
1429 if (unlikely(!zspage)) {
1430 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001431 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -07001432 }
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001433
Minchan Kim37836892016-07-26 15:23:23 -07001434 set_zspage_mapping(zspage, class->index, ZS_EMPTY);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001435 atomic_long_add(class->pages_per_zspage,
1436 &pool->pages_allocated);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001437
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001438 spin_lock(&class->lock);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001439 zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1440 class->size, class->pages_per_zspage));
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001441 }
1442
Minchan Kim37836892016-07-26 15:23:23 -07001443 obj = obj_malloc(class, zspage, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001444 /* Now move the zspage to another fullness group, if required */
Minchan Kim37836892016-07-26 15:23:23 -07001445 fix_fullness_group(class, zspage);
Minchan Kim2e40e162015-04-15 16:15:23 -07001446 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001447 spin_unlock(&class->lock);
1448
Minchan Kim2e40e162015-04-15 16:15:23 -07001449 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001450}
1451EXPORT_SYMBOL_GPL(zs_malloc);
1452
Minchan Kim1ee47162016-05-20 16:59:45 -07001453static void obj_free(struct size_class *class, unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001454{
1455 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001456 struct zspage *zspage;
1457 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001458 unsigned long f_offset;
1459 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001460 void *vaddr;
1461
Minchan Kim312fcae2015-04-15 16:15:30 -07001462 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001463 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001464 f_offset = (class->size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001465 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001466
Minchan Kimc7806262015-04-15 16:15:26 -07001467 vaddr = kmap_atomic(f_page);
1468
1469 /* Insert this object in containing zspage's freelist */
1470 link = (struct link_free *)(vaddr + f_offset);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001471 link->next = get_freeobj(zspage) << OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001472 kunmap_atomic(vaddr);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001473 set_freeobj(zspage, f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001474 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001475 zs_stat_dec(class, OBJ_USED, 1);
1476}
1477
1478void zs_free(struct zs_pool *pool, unsigned long handle)
1479{
Minchan Kim37836892016-07-26 15:23:23 -07001480 struct zspage *zspage;
1481 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001482 unsigned long obj;
1483 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001484 int class_idx;
1485 struct size_class *class;
1486 enum fullness_group fullness;
1487
Minchan Kim2e40e162015-04-15 16:15:23 -07001488 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001489 return;
1490
Minchan Kim312fcae2015-04-15 16:15:30 -07001491 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001492 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001493 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim37836892016-07-26 15:23:23 -07001494 zspage = get_zspage(f_page);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001495
Minchan Kim37836892016-07-26 15:23:23 -07001496 get_zspage_mapping(zspage, &class_idx, &fullness);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001497 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001498
1499 spin_lock(&class->lock);
Minchan Kim1ee47162016-05-20 16:59:45 -07001500 obj_free(class, obj);
Minchan Kim37836892016-07-26 15:23:23 -07001501 fullness = fix_fullness_group(class, zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001502 if (fullness == ZS_EMPTY) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001503 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1504 class->size, class->pages_per_zspage));
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001505 atomic_long_sub(class->pages_per_zspage,
1506 &pool->pages_allocated);
Minchan Kim37836892016-07-26 15:23:23 -07001507 free_zspage(pool, zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001508 }
Minchan Kim312fcae2015-04-15 16:15:30 -07001509 spin_unlock(&class->lock);
1510 unpin_tag(handle);
1511
Minchan Kim37836892016-07-26 15:23:23 -07001512 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001513}
1514EXPORT_SYMBOL_GPL(zs_free);
1515
Minchan Kim251cbb92016-05-20 16:59:42 -07001516static void zs_object_copy(struct size_class *class, unsigned long dst,
1517 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001518{
1519 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001520 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001521 unsigned long s_off, d_off;
1522 void *s_addr, *d_addr;
1523 int s_size, d_size, size;
1524 int written = 0;
1525
1526 s_size = d_size = class->size;
1527
1528 obj_to_location(src, &s_page, &s_objidx);
1529 obj_to_location(dst, &d_page, &d_objidx);
1530
Minchan Kimbfd093f2016-07-26 15:23:28 -07001531 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1532 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001533
1534 if (s_off + class->size > PAGE_SIZE)
1535 s_size = PAGE_SIZE - s_off;
1536
1537 if (d_off + class->size > PAGE_SIZE)
1538 d_size = PAGE_SIZE - d_off;
1539
1540 s_addr = kmap_atomic(s_page);
1541 d_addr = kmap_atomic(d_page);
1542
1543 while (1) {
1544 size = min(s_size, d_size);
1545 memcpy(d_addr + d_off, s_addr + s_off, size);
1546 written += size;
1547
1548 if (written == class->size)
1549 break;
1550
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001551 s_off += size;
1552 s_size -= size;
1553 d_off += size;
1554 d_size -= size;
1555
1556 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001557 kunmap_atomic(d_addr);
1558 kunmap_atomic(s_addr);
1559 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001560 s_addr = kmap_atomic(s_page);
1561 d_addr = kmap_atomic(d_page);
1562 s_size = class->size - written;
1563 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001564 }
1565
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001566 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001567 kunmap_atomic(d_addr);
1568 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001569 d_addr = kmap_atomic(d_page);
1570 d_size = class->size - written;
1571 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001572 }
1573 }
1574
1575 kunmap_atomic(d_addr);
1576 kunmap_atomic(s_addr);
1577}
1578
1579/*
1580 * Find alloced object in zspage from index object and
1581 * return handle.
1582 */
Minchan Kim251cbb92016-05-20 16:59:42 -07001583static unsigned long find_alloced_obj(struct size_class *class,
1584 struct page *page, int index)
Minchan Kim312fcae2015-04-15 16:15:30 -07001585{
1586 unsigned long head;
1587 int offset = 0;
1588 unsigned long handle = 0;
1589 void *addr = kmap_atomic(page);
1590
Minchan Kim37836892016-07-26 15:23:23 -07001591 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001592 offset += class->size * index;
1593
1594 while (offset < PAGE_SIZE) {
Minchan Kim7b60a682015-04-15 16:15:39 -07001595 head = obj_to_head(class, page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001596 if (head & OBJ_ALLOCATED_TAG) {
1597 handle = head & ~OBJ_ALLOCATED_TAG;
1598 if (trypin_tag(handle))
1599 break;
1600 handle = 0;
1601 }
1602
1603 offset += class->size;
1604 index++;
1605 }
1606
1607 kunmap_atomic(addr);
1608 return handle;
1609}
1610
1611struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001612 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001613 struct page *s_page;
1614 /* Destination page for migration which should be a first page
1615 * of zspage. */
1616 struct page *d_page;
1617 /* Starting object index within @s_page which used for live object
1618 * in the subpage. */
1619 int index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001620};
1621
1622static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1623 struct zs_compact_control *cc)
1624{
1625 unsigned long used_obj, free_obj;
1626 unsigned long handle;
1627 struct page *s_page = cc->s_page;
1628 struct page *d_page = cc->d_page;
1629 unsigned long index = cc->index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001630 int ret = 0;
1631
1632 while (1) {
Minchan Kim251cbb92016-05-20 16:59:42 -07001633 handle = find_alloced_obj(class, s_page, index);
Minchan Kim312fcae2015-04-15 16:15:30 -07001634 if (!handle) {
1635 s_page = get_next_page(s_page);
1636 if (!s_page)
1637 break;
1638 index = 0;
1639 continue;
1640 }
1641
1642 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001643 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001644 unpin_tag(handle);
1645 ret = -ENOMEM;
1646 break;
1647 }
1648
1649 used_obj = handle_to_obj(handle);
Minchan Kim37836892016-07-26 15:23:23 -07001650 free_obj = obj_malloc(class, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001651 zs_object_copy(class, free_obj, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001652 index++;
Junil Leec102f072016-01-20 14:58:18 -08001653 /*
1654 * record_obj updates handle's value to free_obj and it will
1655 * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
1656 * breaks synchronization using pin_tag(e,g, zs_free) so
1657 * let's keep the lock bit.
1658 */
1659 free_obj |= BIT(HANDLE_PIN_BIT);
Minchan Kim312fcae2015-04-15 16:15:30 -07001660 record_obj(handle, free_obj);
1661 unpin_tag(handle);
Minchan Kim1ee47162016-05-20 16:59:45 -07001662 obj_free(class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001663 }
1664
1665 /* Remember last position in this iteration */
1666 cc->s_page = s_page;
1667 cc->index = index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001668
1669 return ret;
1670}
1671
Minchan Kim37836892016-07-26 15:23:23 -07001672static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001673{
1674 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001675 struct zspage *zspage;
1676 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001677
Minchan Kim37836892016-07-26 15:23:23 -07001678 if (!source) {
1679 fg[0] = ZS_ALMOST_FULL;
1680 fg[1] = ZS_ALMOST_EMPTY;
1681 }
1682
1683 for (i = 0; i < 2; i++) {
1684 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1685 struct zspage, list);
1686 if (zspage) {
1687 remove_zspage(class, zspage, fg[i]);
1688 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001689 }
1690 }
1691
Minchan Kim37836892016-07-26 15:23:23 -07001692 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001693}
1694
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001695/*
Minchan Kim37836892016-07-26 15:23:23 -07001696 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001697 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001698 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001699 *
Minchan Kim37836892016-07-26 15:23:23 -07001700 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001701 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001702static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001703 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001704{
Minchan Kim312fcae2015-04-15 16:15:30 -07001705 enum fullness_group fullness;
1706
Minchan Kim37836892016-07-26 15:23:23 -07001707 fullness = get_fullness_group(class, zspage);
1708 insert_zspage(class, zspage, fullness);
1709 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001710
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001711 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001712}
1713
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001714/*
1715 *
1716 * Based on the number of unused allocated objects calculate
1717 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001718 */
1719static unsigned long zs_can_compact(struct size_class *class)
1720{
1721 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07001722 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
1723 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001724
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07001725 if (obj_allocated <= obj_used)
1726 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001727
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07001728 obj_wasted = obj_allocated - obj_used;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001729 obj_wasted /= get_maxobj_per_zspage(class->size,
1730 class->pages_per_zspage);
1731
Minchan Kim6cbf16b2015-09-08 15:04:49 -07001732 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001733}
1734
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001735static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07001736{
Minchan Kim312fcae2015-04-15 16:15:30 -07001737 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07001738 struct zspage *src_zspage;
1739 struct zspage *dst_zspage = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07001740
Minchan Kim312fcae2015-04-15 16:15:30 -07001741 spin_lock(&class->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001742 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001743
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001744 if (!zs_can_compact(class))
1745 break;
1746
Minchan Kim312fcae2015-04-15 16:15:30 -07001747 cc.index = 0;
Minchan Kim37836892016-07-26 15:23:23 -07001748 cc.s_page = src_zspage->first_page;
Minchan Kim312fcae2015-04-15 16:15:30 -07001749
Minchan Kim37836892016-07-26 15:23:23 -07001750 while ((dst_zspage = isolate_zspage(class, false))) {
1751 cc.d_page = dst_zspage->first_page;
Minchan Kim312fcae2015-04-15 16:15:30 -07001752 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001753 * If there is no more space in dst_page, resched
1754 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07001755 */
1756 if (!migrate_zspage(pool, class, &cc))
1757 break;
1758
Minchan Kim4aa409c2016-07-26 15:23:26 -07001759 putback_zspage(class, dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001760 }
1761
1762 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07001763 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07001764 break;
1765
Minchan Kim4aa409c2016-07-26 15:23:26 -07001766 putback_zspage(class, dst_zspage);
1767 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
1768 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1769 class->size, class->pages_per_zspage));
1770 atomic_long_sub(class->pages_per_zspage,
1771 &pool->pages_allocated);
1772 free_zspage(pool, src_zspage);
Minchan Kim6cbf16b2015-09-08 15:04:49 -07001773 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim4aa409c2016-07-26 15:23:26 -07001774 }
Minchan Kim312fcae2015-04-15 16:15:30 -07001775 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07001776 cond_resched();
1777 spin_lock(&class->lock);
1778 }
1779
Minchan Kim37836892016-07-26 15:23:23 -07001780 if (src_zspage)
Minchan Kim4aa409c2016-07-26 15:23:26 -07001781 putback_zspage(class, src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07001782
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001783 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07001784}
1785
1786unsigned long zs_compact(struct zs_pool *pool)
1787{
1788 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07001789 struct size_class *class;
1790
1791 for (i = zs_size_classes - 1; i >= 0; i--) {
1792 class = pool->size_class[i];
1793 if (!class)
1794 continue;
1795 if (class->index != i)
1796 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001797 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07001798 }
1799
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001800 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07001801}
1802EXPORT_SYMBOL_GPL(zs_compact);
1803
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001804void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
1805{
1806 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
1807}
1808EXPORT_SYMBOL_GPL(zs_pool_stats);
1809
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001810static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
1811 struct shrink_control *sc)
1812{
1813 unsigned long pages_freed;
1814 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1815 shrinker);
1816
1817 pages_freed = pool->stats.pages_compacted;
1818 /*
1819 * Compact classes and calculate compaction delta.
1820 * Can run concurrently with a manually triggered
1821 * (by user) compaction.
1822 */
1823 pages_freed = zs_compact(pool) - pages_freed;
1824
1825 return pages_freed ? pages_freed : SHRINK_STOP;
1826}
1827
1828static unsigned long zs_shrinker_count(struct shrinker *shrinker,
1829 struct shrink_control *sc)
1830{
1831 int i;
1832 struct size_class *class;
1833 unsigned long pages_to_free = 0;
1834 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1835 shrinker);
1836
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001837 for (i = zs_size_classes - 1; i >= 0; i--) {
1838 class = pool->size_class[i];
1839 if (!class)
1840 continue;
1841 if (class->index != i)
1842 continue;
1843
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001844 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001845 }
1846
1847 return pages_to_free;
1848}
1849
1850static void zs_unregister_shrinker(struct zs_pool *pool)
1851{
1852 if (pool->shrinker_enabled) {
1853 unregister_shrinker(&pool->shrinker);
1854 pool->shrinker_enabled = false;
1855 }
1856}
1857
1858static int zs_register_shrinker(struct zs_pool *pool)
1859{
1860 pool->shrinker.scan_objects = zs_shrinker_scan;
1861 pool->shrinker.count_objects = zs_shrinker_count;
1862 pool->shrinker.batch = 0;
1863 pool->shrinker.seeks = DEFAULT_SEEKS;
1864
1865 return register_shrinker(&pool->shrinker);
1866}
1867
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001868/**
1869 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -06001870 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001871 *
1872 * This function must be called before anything when using
1873 * the zsmalloc allocator.
1874 *
1875 * On success, a pointer to the newly created pool is returned,
1876 * otherwise NULL.
1877 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001878struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06001879{
Ganesh Mahendran18136652014-12-12 16:57:10 -08001880 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06001881 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001882 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001883
Ganesh Mahendran18136652014-12-12 16:57:10 -08001884 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001885 if (!pool)
1886 return NULL;
1887
Minchan Kim2e40e162015-04-15 16:15:23 -07001888 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
1889 GFP_KERNEL);
1890 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001891 kfree(pool);
1892 return NULL;
1893 }
1894
Minchan Kim2e40e162015-04-15 16:15:23 -07001895 pool->name = kstrdup(name, GFP_KERNEL);
1896 if (!pool->name)
1897 goto err;
1898
Minchan Kim37836892016-07-26 15:23:23 -07001899 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07001900 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001901
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001902 /*
1903 * Iterate reversly, because, size of size_class that we want to use
1904 * for merging should be larger or equal to current size.
1905 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001906 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001907 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001908 int pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001909 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07001910 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06001911
1912 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
1913 if (size > ZS_MAX_ALLOC_SIZE)
1914 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001915 pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001916
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001917 /*
1918 * size_class is used for normal zsmalloc operation such
1919 * as alloc/free for that size. Although it is natural that we
1920 * have one size_class for each size, there is a chance that we
1921 * can get more memory utilization if we use one size_class for
1922 * many different sizes whose size_class have same
1923 * characteristics. So, we makes size_class point to
1924 * previous size_class if possible.
1925 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001926 if (prev_class) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001927 if (can_merge(prev_class, size, pages_per_zspage)) {
1928 pool->size_class[i] = prev_class;
1929 continue;
1930 }
1931 }
1932
1933 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
1934 if (!class)
1935 goto err;
1936
Nitin Gupta61989a82012-01-09 16:51:56 -06001937 class->size = size;
1938 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001939 class->pages_per_zspage = pages_per_zspage;
Minchan Kim1fc6e272016-07-26 15:23:11 -07001940 class->objs_per_zspage = class->pages_per_zspage *
1941 PAGE_SIZE / class->size;
1942 if (pages_per_zspage == 1 && class->objs_per_zspage == 1)
Minchan Kim7b60a682015-04-15 16:15:39 -07001943 class->huge = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06001944 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001945 pool->size_class[i] = class;
Minchan Kim37836892016-07-26 15:23:23 -07001946 for (fullness = ZS_ALMOST_FULL; fullness <= ZS_ALMOST_EMPTY;
1947 fullness++)
1948 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001949
1950 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06001951 }
1952
Dan Streetmand34f6152016-05-20 16:59:56 -07001953 /* debug only, don't abort if it fails */
1954 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001955
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001956 /*
1957 * Not critical, we still can use the pool
1958 * and user can trigger compaction manually.
1959 */
1960 if (zs_register_shrinker(pool) == 0)
1961 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06001962 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001963
1964err:
1965 zs_destroy_pool(pool);
1966 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001967}
1968EXPORT_SYMBOL_GPL(zs_create_pool);
1969
1970void zs_destroy_pool(struct zs_pool *pool)
1971{
1972 int i;
1973
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001974 zs_unregister_shrinker(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001975 zs_pool_stat_destroy(pool);
1976
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001977 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001978 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001979 struct size_class *class = pool->size_class[i];
1980
1981 if (!class)
1982 continue;
1983
1984 if (class->index != i)
1985 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06001986
Minchan Kim37836892016-07-26 15:23:23 -07001987 for (fg = ZS_ALMOST_FULL; fg <= ZS_ALMOST_EMPTY; fg++) {
1988 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04001989 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06001990 class->size, fg);
1991 }
1992 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001993 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06001994 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001995
Minchan Kim37836892016-07-26 15:23:23 -07001996 destroy_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001997 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001998 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06001999 kfree(pool);
2000}
2001EXPORT_SYMBOL_GPL(zs_destroy_pool);
2002
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002003static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002004{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002005 int ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06002006
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002007 if (ret)
2008 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002009
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002010 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002011
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002012#ifdef CONFIG_ZPOOL
2013 zpool_register_driver(&zs_zpool_driver);
2014#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002015
Dan Streetman4abaac92016-05-26 15:16:27 -07002016 zs_stat_init();
2017
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002018 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002019
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002020notifier_fail:
2021 zs_unregister_cpu_notifier();
2022
2023 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002024}
Nitin Gupta61989a82012-01-09 16:51:56 -06002025
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002026static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002027{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002028#ifdef CONFIG_ZPOOL
2029 zpool_unregister_driver(&zs_zpool_driver);
2030#endif
2031 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002032
2033 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002034}
Ben Hutchings069f1012012-06-20 02:31:11 +01002035
2036module_init(zs_init);
2037module_exit(zs_exit);
2038
2039MODULE_LICENSE("Dual BSD/GPL");
2040MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");