blob: 2858b20dce763741ff2e165fd4c09b98b5a12966 [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:
19 * page->first_page: points to the first component (0-order) page
20 * page->index (union with page->freelist): offset of the first object
21 * starting in this page. For the first page, this is
22 * always 0, so we use this field (aka freelist) to point
23 * to the first free object in zspage.
24 * page->lru: links together all component pages (except the first page)
25 * of a zspage
26 *
27 * For _first_ page only:
28 *
29 * page->private (union with page->first_page): refers to the
30 * component page after the first page
Minchan Kim7b60a682015-04-15 16:15:39 -070031 * If the page is first_page for huge object, it stores handle.
32 * Look at size_class->huge.
Nitin Gupta2db51da2012-06-09 17:41:14 -070033 * page->freelist: points to the first free object in zspage.
34 * Free objects are linked together using in-place
35 * metadata.
36 * page->objects: maximum number of objects we can store in this
37 * zspage (class->zspage_order * PAGE_SIZE / class->size)
38 * page->lru: links together first pages of various zspages.
39 * Basically forming list of zspages in a fullness group.
40 * page->mapping: class index and fullness group of the zspage
Hui Zhu8f958c92015-11-06 16:29:23 -080041 * page->inuse: the number of objects that are used in this zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070042 *
43 * Usage of struct page flags:
44 * PG_private: identifies the first component page
45 * PG_private2: identifies the last component page
46 *
47 */
48
Nitin Gupta61989a82012-01-09 16:51:56 -060049#include <linux/module.h>
50#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070051#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060052#include <linux/bitops.h>
53#include <linux/errno.h>
54#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060055#include <linux/string.h>
56#include <linux/slab.h>
57#include <asm/tlbflush.h>
58#include <asm/pgtable.h>
59#include <linux/cpumask.h>
60#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060061#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080062#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090063#include <linux/spinlock.h>
64#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080065#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080066#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070067#include <linux/zpool.h>
Seth Jennings0959c632012-08-08 15:12:17 +090068
69/*
70 * This must be power of 2 and greater than of equal to sizeof(link_free).
71 * These two conditions ensure that any 'struct link_free' itself doesn't
72 * span more than 1 page which avoids complex case of mapping 2 pages simply
73 * to restore link_free pointer values.
74 */
75#define ZS_ALIGN 8
76
77/*
78 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
79 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
80 */
81#define ZS_MAX_ZSPAGE_ORDER 2
82#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
83
Minchan Kim2e40e162015-04-15 16:15:23 -070084#define ZS_HANDLE_SIZE (sizeof(unsigned long))
85
Seth Jennings0959c632012-08-08 15:12:17 +090086/*
87 * Object location (<PFN>, <obj_idx>) is encoded as
Nitin Cuptac3e3e882013-12-11 11:04:37 +090088 * as single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090089 *
90 * Note that object index <obj_idx> is relative to system
91 * page <PFN> it is stored in, so for each sub-page belonging
92 * to a zspage, obj_idx starts with 0.
93 *
94 * This is made more complicated by various memory models and PAE.
95 */
96
97#ifndef MAX_PHYSMEM_BITS
98#ifdef CONFIG_HIGHMEM64G
99#define MAX_PHYSMEM_BITS 36
100#else /* !CONFIG_HIGHMEM64G */
101/*
102 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
103 * be PAGE_SHIFT
104 */
105#define MAX_PHYSMEM_BITS BITS_PER_LONG
106#endif
107#endif
108#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -0700109
110/*
111 * Memory for allocating for handle keeps object position by
112 * encoding <page, obj_idx> and the encoded value has a room
113 * in least bit(ie, look at obj_to_location).
114 * We use the bit to synchronize between object access by
115 * user and migration.
116 */
117#define HANDLE_PIN_BIT 0
118
119/*
120 * Head in allocated object should have OBJ_ALLOCATED_TAG
121 * to identify the object was allocated or not.
122 * It's okay to add the status bit in the least bit because
123 * header keeps handle which is 4byte-aligned address so we
124 * have room for two bit at least.
125 */
126#define OBJ_ALLOCATED_TAG 1
127#define OBJ_TAG_BITS 1
128#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900129#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
130
131#define MAX(a, b) ((a) >= (b) ? (a) : (b))
132/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
133#define ZS_MIN_ALLOC_SIZE \
134 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700135/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700136#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900137
138/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700139 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900140 * trader-off here:
141 * - Large number of size classes is potentially wasteful as free page are
142 * spread across these classes
143 * - Small number of size classes causes large internal fragmentation
144 * - Probably its better to use specific size classes (empirically
145 * determined). NOTE: all those class sizes must be set as multiple of
146 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
147 *
148 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
149 * (reason above)
150 */
Seth Jenningsd662b8e2013-01-25 11:46:18 -0600151#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
Seth Jennings0959c632012-08-08 15:12:17 +0900152
153/*
154 * We do not maintain any list for completely empty or full pages
155 */
156enum fullness_group {
157 ZS_ALMOST_FULL,
158 ZS_ALMOST_EMPTY,
159 _ZS_NR_FULLNESS_GROUPS,
160
161 ZS_EMPTY,
162 ZS_FULL
163};
164
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800165enum zs_stat_type {
166 OBJ_ALLOCATED,
167 OBJ_USED,
Minchan Kim248ca1b2015-04-15 16:15:42 -0700168 CLASS_ALMOST_FULL,
169 CLASS_ALMOST_EMPTY,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800170};
171
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800172#ifdef CONFIG_ZSMALLOC_STAT
173#define NR_ZS_STAT_TYPE (CLASS_ALMOST_EMPTY + 1)
174#else
175#define NR_ZS_STAT_TYPE (OBJ_USED + 1)
176#endif
177
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800178struct zs_size_stat {
179 unsigned long objs[NR_ZS_STAT_TYPE];
180};
181
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700182#ifdef CONFIG_ZSMALLOC_STAT
183static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800184#endif
185
Seth Jennings0959c632012-08-08 15:12:17 +0900186/*
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800187 * number of size_classes
188 */
189static int zs_size_classes;
190
191/*
Seth Jennings0959c632012-08-08 15:12:17 +0900192 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
193 * n <= N / f, where
194 * n = number of allocated objects
195 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700196 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900197 *
198 * Similarly, we assign zspage to:
199 * ZS_ALMOST_FULL when n > N / f
200 * ZS_EMPTY when n == 0
201 * ZS_FULL when n == N
202 *
203 * (see: fix_fullness_group())
204 */
205static const int fullness_threshold_frac = 4;
206
207struct size_class {
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700208 spinlock_t lock;
209 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
Seth Jennings0959c632012-08-08 15:12:17 +0900210 /*
211 * Size of objects stored in this class. Must be multiple
212 * of ZS_ALIGN.
213 */
214 int size;
215 unsigned int index;
216
217 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
218 int pages_per_zspage;
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700219 struct zs_size_stat stats;
220
Minchan Kim7b60a682015-04-15 16:15:39 -0700221 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
222 bool huge;
Seth Jennings0959c632012-08-08 15:12:17 +0900223};
224
225/*
226 * Placed within free objects to form a singly linked list.
227 * For every zspage, first_page->freelist gives head of this list.
228 *
229 * This must be power of 2 and less than or equal to ZS_ALIGN
230 */
231struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700232 union {
233 /*
234 * Position of next free chunk (encodes <PFN, obj_idx>)
235 * It's valid for non-allocated object
236 */
237 void *next;
238 /*
239 * Handle of allocated object.
240 */
241 unsigned long handle;
242 };
Seth Jennings0959c632012-08-08 15:12:17 +0900243};
244
245struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800246 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800247
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -0800248 struct size_class **size_class;
Minchan Kim2e40e162015-04-15 16:15:23 -0700249 struct kmem_cache *handle_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900250
251 gfp_t flags; /* allocation flags used when growing pool */
Minchan Kim13de8932014-10-09 15:29:48 -0700252 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800253
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700254 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700255
256 /* Compact classes */
257 struct shrinker shrinker;
258 /*
259 * To signify that register_shrinker() was successful
260 * and unregister_shrinker() will not Oops.
261 */
262 bool shrinker_enabled;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800263#ifdef CONFIG_ZSMALLOC_STAT
264 struct dentry *stat_dentry;
265#endif
Seth Jennings0959c632012-08-08 15:12:17 +0900266};
Nitin Gupta61989a82012-01-09 16:51:56 -0600267
268/*
269 * A zspage's class index and fullness group
270 * are encoded in its (first)page->mapping
271 */
272#define CLASS_IDX_BITS 28
273#define FULLNESS_BITS 4
274#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
275#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
276
Seth Jenningsf5536462012-07-18 11:55:56 -0500277struct mapping_area {
Minchan Kim1b945ae2013-12-11 11:04:36 +0900278#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -0500279 struct vm_struct *vm; /* vm area for mapping object that span pages */
280#else
281 char *vm_buf; /* copy buffer for objects that span pages */
282#endif
283 char *vm_addr; /* address of kmap_atomic()'ed pages */
284 enum zs_mapmode vm_mm; /* mapping mode */
Minchan Kim7b60a682015-04-15 16:15:39 -0700285 bool huge;
Seth Jenningsf5536462012-07-18 11:55:56 -0500286};
287
Minchan Kim2e40e162015-04-15 16:15:23 -0700288static int create_handle_cache(struct zs_pool *pool)
289{
290 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
291 0, 0, NULL);
292 return pool->handle_cachep ? 0 : 1;
293}
294
295static void destroy_handle_cache(struct zs_pool *pool)
296{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700297 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700298}
299
300static unsigned long alloc_handle(struct zs_pool *pool)
301{
302 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
303 pool->flags & ~__GFP_HIGHMEM);
304}
305
306static void free_handle(struct zs_pool *pool, unsigned long handle)
307{
308 kmem_cache_free(pool->handle_cachep, (void *)handle);
309}
310
311static void record_obj(unsigned long handle, unsigned long obj)
312{
313 *(unsigned long *)handle = obj;
314}
315
Dan Streetmanc7957792014-08-06 16:08:38 -0700316/* zpool driver */
317
318#ifdef CONFIG_ZPOOL
319
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800320static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700321 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700322 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700323{
Ganesh Mahendran3eba0c62015-02-12 15:00:51 -0800324 return zs_create_pool(name, gfp);
Dan Streetmanc7957792014-08-06 16:08:38 -0700325}
326
327static void zs_zpool_destroy(void *pool)
328{
329 zs_destroy_pool(pool);
330}
331
332static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
333 unsigned long *handle)
334{
335 *handle = zs_malloc(pool, size);
336 return *handle ? 0 : -1;
337}
338static void zs_zpool_free(void *pool, unsigned long handle)
339{
340 zs_free(pool, handle);
341}
342
343static int zs_zpool_shrink(void *pool, unsigned int pages,
344 unsigned int *reclaimed)
345{
346 return -EINVAL;
347}
348
349static void *zs_zpool_map(void *pool, unsigned long handle,
350 enum zpool_mapmode mm)
351{
352 enum zs_mapmode zs_mm;
353
354 switch (mm) {
355 case ZPOOL_MM_RO:
356 zs_mm = ZS_MM_RO;
357 break;
358 case ZPOOL_MM_WO:
359 zs_mm = ZS_MM_WO;
360 break;
361 case ZPOOL_MM_RW: /* fallthru */
362 default:
363 zs_mm = ZS_MM_RW;
364 break;
365 }
366
367 return zs_map_object(pool, handle, zs_mm);
368}
369static void zs_zpool_unmap(void *pool, unsigned long handle)
370{
371 zs_unmap_object(pool, handle);
372}
373
374static u64 zs_zpool_total_size(void *pool)
375{
Minchan Kim722cdc12014-10-09 15:29:50 -0700376 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700377}
378
379static struct zpool_driver zs_zpool_driver = {
380 .type = "zsmalloc",
381 .owner = THIS_MODULE,
382 .create = zs_zpool_create,
383 .destroy = zs_zpool_destroy,
384 .malloc = zs_zpool_malloc,
385 .free = zs_zpool_free,
386 .shrink = zs_zpool_shrink,
387 .map = zs_zpool_map,
388 .unmap = zs_zpool_unmap,
389 .total_size = zs_zpool_total_size,
390};
391
Kees Cook137f8cf2014-08-29 15:18:40 -0700392MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700393#endif /* CONFIG_ZPOOL */
394
Minchan Kim248ca1b2015-04-15 16:15:42 -0700395static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
396{
397 return pages_per_zspage * PAGE_SIZE / size;
398}
399
Nitin Gupta61989a82012-01-09 16:51:56 -0600400/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
401static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
402
403static int is_first_page(struct page *page)
404{
Minchan Kima27545bf2012-04-25 15:23:09 +0900405 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600406}
407
408static int is_last_page(struct page *page)
409{
Minchan Kima27545bf2012-04-25 15:23:09 +0900410 return PagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600411}
412
413static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
414 enum fullness_group *fullness)
415{
416 unsigned long m;
417 BUG_ON(!is_first_page(page));
418
419 m = (unsigned long)page->mapping;
420 *fullness = m & FULLNESS_MASK;
421 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
422}
423
424static void set_zspage_mapping(struct page *page, unsigned int class_idx,
425 enum fullness_group fullness)
426{
427 unsigned long m;
428 BUG_ON(!is_first_page(page));
429
430 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
431 (fullness & FULLNESS_MASK);
432 page->mapping = (struct address_space *)m;
433}
434
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900435/*
436 * zsmalloc divides the pool into various size classes where each
437 * class maintains a list of zspages where each zspage is divided
438 * into equal sized chunks. Each allocation falls into one of these
439 * classes depending on its size. This function returns index of the
440 * size class which has chunk size big enough to hold the give size.
441 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600442static int get_size_class_index(int size)
443{
444 int idx = 0;
445
446 if (likely(size > ZS_MIN_ALLOC_SIZE))
447 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
448 ZS_SIZE_CLASS_DELTA);
449
Minchan Kim7b60a682015-04-15 16:15:39 -0700450 return min(zs_size_classes - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600451}
452
Minchan Kim248ca1b2015-04-15 16:15:42 -0700453static inline void zs_stat_inc(struct size_class *class,
454 enum zs_stat_type type, unsigned long cnt)
455{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800456 if (type < NR_ZS_STAT_TYPE)
457 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700458}
459
460static inline void zs_stat_dec(struct size_class *class,
461 enum zs_stat_type type, unsigned long cnt)
462{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800463 if (type < NR_ZS_STAT_TYPE)
464 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700465}
466
467static inline unsigned long zs_stat_get(struct size_class *class,
468 enum zs_stat_type type)
469{
Sergey Senozhatsky6fe51862015-11-06 16:29:38 -0800470 if (type < NR_ZS_STAT_TYPE)
471 return class->stats.objs[type];
472 return 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700473}
474
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700475#ifdef CONFIG_ZSMALLOC_STAT
476
Minchan Kim248ca1b2015-04-15 16:15:42 -0700477static int __init zs_stat_init(void)
478{
479 if (!debugfs_initialized())
480 return -ENODEV;
481
482 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
483 if (!zs_stat_root)
484 return -ENOMEM;
485
486 return 0;
487}
488
489static void __exit zs_stat_exit(void)
490{
491 debugfs_remove_recursive(zs_stat_root);
492}
493
494static int zs_stats_size_show(struct seq_file *s, void *v)
495{
496 int i;
497 struct zs_pool *pool = s->private;
498 struct size_class *class;
499 int objs_per_zspage;
500 unsigned long class_almost_full, class_almost_empty;
501 unsigned long obj_allocated, obj_used, pages_used;
502 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
503 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
504
505 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s\n",
506 "class", "size", "almost_full", "almost_empty",
507 "obj_allocated", "obj_used", "pages_used",
508 "pages_per_zspage");
509
510 for (i = 0; i < zs_size_classes; i++) {
511 class = pool->size_class[i];
512
513 if (class->index != i)
514 continue;
515
516 spin_lock(&class->lock);
517 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
518 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
519 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
520 obj_used = zs_stat_get(class, OBJ_USED);
521 spin_unlock(&class->lock);
522
523 objs_per_zspage = get_maxobj_per_zspage(class->size,
524 class->pages_per_zspage);
525 pages_used = obj_allocated / objs_per_zspage *
526 class->pages_per_zspage;
527
528 seq_printf(s, " %5u %5u %11lu %12lu %13lu %10lu %10lu %16d\n",
529 i, class->size, class_almost_full, class_almost_empty,
530 obj_allocated, obj_used, pages_used,
531 class->pages_per_zspage);
532
533 total_class_almost_full += class_almost_full;
534 total_class_almost_empty += class_almost_empty;
535 total_objs += obj_allocated;
536 total_used_objs += obj_used;
537 total_pages += pages_used;
538 }
539
540 seq_puts(s, "\n");
541 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu\n",
542 "Total", "", total_class_almost_full,
543 total_class_almost_empty, total_objs,
544 total_used_objs, total_pages);
545
546 return 0;
547}
548
549static int zs_stats_size_open(struct inode *inode, struct file *file)
550{
551 return single_open(file, zs_stats_size_show, inode->i_private);
552}
553
554static const struct file_operations zs_stat_size_ops = {
555 .open = zs_stats_size_open,
556 .read = seq_read,
557 .llseek = seq_lseek,
558 .release = single_release,
559};
560
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800561static int zs_pool_stat_create(const char *name, struct zs_pool *pool)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700562{
563 struct dentry *entry;
564
565 if (!zs_stat_root)
566 return -ENODEV;
567
568 entry = debugfs_create_dir(name, zs_stat_root);
569 if (!entry) {
570 pr_warn("debugfs dir <%s> creation failed\n", name);
571 return -ENOMEM;
572 }
573 pool->stat_dentry = entry;
574
575 entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
576 pool->stat_dentry, pool, &zs_stat_size_ops);
577 if (!entry) {
578 pr_warn("%s: debugfs file entry <%s> creation failed\n",
579 name, "classes");
580 return -ENOMEM;
581 }
582
583 return 0;
584}
585
586static void zs_pool_stat_destroy(struct zs_pool *pool)
587{
588 debugfs_remove_recursive(pool->stat_dentry);
589}
590
591#else /* CONFIG_ZSMALLOC_STAT */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700592static int __init zs_stat_init(void)
593{
594 return 0;
595}
596
597static void __exit zs_stat_exit(void)
598{
599}
600
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800601static inline int zs_pool_stat_create(const char *name, struct zs_pool *pool)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700602{
603 return 0;
604}
605
606static inline void zs_pool_stat_destroy(struct zs_pool *pool)
607{
608}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700609#endif
610
611
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900612/*
613 * For each size class, zspages are divided into different groups
614 * depending on how "full" they are. This was done so that we could
615 * easily find empty or nearly empty zspages when we try to shrink
616 * the pool (not yet implemented). This function returns fullness
617 * status of the given page.
618 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600619static enum fullness_group get_fullness_group(struct page *page)
620{
621 int inuse, max_objects;
622 enum fullness_group fg;
623 BUG_ON(!is_first_page(page));
624
625 inuse = page->inuse;
626 max_objects = page->objects;
627
628 if (inuse == 0)
629 fg = ZS_EMPTY;
630 else if (inuse == max_objects)
631 fg = ZS_FULL;
Minchan Kimd3d07c92015-04-15 16:15:33 -0700632 else if (inuse <= 3 * max_objects / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600633 fg = ZS_ALMOST_EMPTY;
634 else
635 fg = ZS_ALMOST_FULL;
636
637 return fg;
638}
639
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900640/*
641 * Each size class maintains various freelists and zspages are assigned
642 * to one of these freelists based on the number of live objects they
643 * have. This functions inserts the given zspage into the freelist
644 * identified by <class, fullness_group>.
645 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600646static void insert_zspage(struct page *page, struct size_class *class,
647 enum fullness_group fullness)
648{
649 struct page **head;
650
651 BUG_ON(!is_first_page(page));
652
653 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
654 return;
655
Minchan Kim248ca1b2015-04-15 16:15:42 -0700656 zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
657 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700658
659 head = &class->fullness_list[fullness];
660 if (!*head) {
661 *head = page;
662 return;
663 }
664
665 /*
666 * We want to see more ZS_FULL pages and less almost
667 * empty/full. Put pages with higher ->inuse first.
668 */
669 list_add_tail(&page->lru, &(*head)->lru);
670 if (page->inuse >= (*head)->inuse)
671 *head = page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600672}
673
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900674/*
675 * This function removes the given zspage from the freelist identified
676 * by <class, fullness_group>.
677 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600678static void remove_zspage(struct page *page, struct size_class *class,
679 enum fullness_group fullness)
680{
681 struct page **head;
682
683 BUG_ON(!is_first_page(page));
684
685 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
686 return;
687
688 head = &class->fullness_list[fullness];
689 BUG_ON(!*head);
690 if (list_empty(&(*head)->lru))
691 *head = NULL;
692 else if (*head == page)
693 *head = (struct page *)list_entry((*head)->lru.next,
694 struct page, lru);
695
696 list_del_init(&page->lru);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700697 zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
698 CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600699}
700
Nitin Cuptac3e3e882013-12-11 11:04:37 +0900701/*
702 * Each size class maintains zspages in different fullness groups depending
703 * on the number of live objects they contain. When allocating or freeing
704 * objects, the fullness status of the page can change, say, from ALMOST_FULL
705 * to ALMOST_EMPTY when freeing an object. This function checks if such
706 * a status change has occurred for the given page and accordingly moves the
707 * page from the freelist of the old fullness group to that of the new
708 * fullness group.
709 */
Minchan Kimc7806262015-04-15 16:15:26 -0700710static enum fullness_group fix_fullness_group(struct size_class *class,
Nitin Gupta61989a82012-01-09 16:51:56 -0600711 struct page *page)
712{
713 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600714 enum fullness_group currfg, newfg;
715
716 BUG_ON(!is_first_page(page));
717
718 get_zspage_mapping(page, &class_idx, &currfg);
719 newfg = get_fullness_group(page);
720 if (newfg == currfg)
721 goto out;
722
Nitin Gupta61989a82012-01-09 16:51:56 -0600723 remove_zspage(page, class, currfg);
724 insert_zspage(page, class, newfg);
725 set_zspage_mapping(page, class_idx, newfg);
726
727out:
728 return newfg;
729}
730
731/*
732 * We have to decide on how many pages to link together
733 * to form a zspage for each size class. This is important
734 * to reduce wastage due to unusable space left at end of
735 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700736 * wastage = Zp % class_size
737 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600738 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
739 *
740 * For example, for size class of 3/8 * PAGE_SIZE, we should
741 * link together 3 PAGE_SIZE sized pages to form a zspage
742 * since then we can perfectly fit in 8 such objects.
743 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900744static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600745{
746 int i, max_usedpc = 0;
747 /* zspage order which gives maximum used size per KB */
748 int max_usedpc_order = 1;
749
Seth Jennings84d4faa2012-03-05 11:33:21 -0600750 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600751 int zspage_size;
752 int waste, usedpc;
753
754 zspage_size = i * PAGE_SIZE;
755 waste = zspage_size % class_size;
756 usedpc = (zspage_size - waste) * 100 / zspage_size;
757
758 if (usedpc > max_usedpc) {
759 max_usedpc = usedpc;
760 max_usedpc_order = i;
761 }
762 }
763
764 return max_usedpc_order;
765}
766
767/*
768 * A single 'zspage' is composed of many system pages which are
769 * linked together using fields in struct page. This function finds
770 * the first/head page, given any component page of a zspage.
771 */
772static struct page *get_first_page(struct page *page)
773{
774 if (is_first_page(page))
775 return page;
776 else
777 return page->first_page;
778}
779
780static struct page *get_next_page(struct page *page)
781{
782 struct page *next;
783
784 if (is_last_page(page))
785 next = NULL;
786 else if (is_first_page(page))
Sunghan Suhe842b972013-07-12 16:08:13 +0900787 next = (struct page *)page_private(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600788 else
789 next = list_entry(page->lru.next, struct page, lru);
790
791 return next;
792}
793
Olav Haugan67296872013-11-22 09:30:41 -0800794/*
795 * Encode <page, obj_idx> as a single handle value.
Minchan Kim312fcae2015-04-15 16:15:30 -0700796 * We use the least bit of handle for tagging.
Olav Haugan67296872013-11-22 09:30:41 -0800797 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700798static void *location_to_obj(struct page *page, unsigned long obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600799{
Minchan Kim312fcae2015-04-15 16:15:30 -0700800 unsigned long obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600801
802 if (!page) {
803 BUG_ON(obj_idx);
804 return NULL;
805 }
806
Minchan Kim312fcae2015-04-15 16:15:30 -0700807 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
808 obj |= ((obj_idx) & OBJ_INDEX_MASK);
809 obj <<= OBJ_TAG_BITS;
Nitin Gupta61989a82012-01-09 16:51:56 -0600810
Minchan Kim312fcae2015-04-15 16:15:30 -0700811 return (void *)obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600812}
813
Olav Haugan67296872013-11-22 09:30:41 -0800814/*
815 * Decode <page, obj_idx> pair from the given object handle. We adjust the
816 * decoded obj_idx back to its original value since it was adjusted in
Minchan Kim312fcae2015-04-15 16:15:30 -0700817 * location_to_obj().
Olav Haugan67296872013-11-22 09:30:41 -0800818 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700819static void obj_to_location(unsigned long obj, struct page **page,
Nitin Gupta61989a82012-01-09 16:51:56 -0600820 unsigned long *obj_idx)
821{
Minchan Kim312fcae2015-04-15 16:15:30 -0700822 obj >>= OBJ_TAG_BITS;
823 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
824 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600825}
826
Minchan Kim2e40e162015-04-15 16:15:23 -0700827static unsigned long handle_to_obj(unsigned long handle)
828{
829 return *(unsigned long *)handle;
830}
831
Minchan Kim7b60a682015-04-15 16:15:39 -0700832static unsigned long obj_to_head(struct size_class *class, struct page *page,
833 void *obj)
Minchan Kim312fcae2015-04-15 16:15:30 -0700834{
Minchan Kim7b60a682015-04-15 16:15:39 -0700835 if (class->huge) {
836 VM_BUG_ON(!is_first_page(page));
Hui Zhu12a7bfa2015-11-06 16:29:26 -0800837 return page_private(page);
Minchan Kim7b60a682015-04-15 16:15:39 -0700838 } else
839 return *(unsigned long *)obj;
Minchan Kim312fcae2015-04-15 16:15:30 -0700840}
841
Nitin Gupta61989a82012-01-09 16:51:56 -0600842static unsigned long obj_idx_to_offset(struct page *page,
843 unsigned long obj_idx, int class_size)
844{
845 unsigned long off = 0;
846
847 if (!is_first_page(page))
848 off = page->index;
849
850 return off + obj_idx * class_size;
851}
852
Minchan Kim312fcae2015-04-15 16:15:30 -0700853static inline int trypin_tag(unsigned long handle)
854{
855 unsigned long *ptr = (unsigned long *)handle;
856
857 return !test_and_set_bit_lock(HANDLE_PIN_BIT, ptr);
858}
859
860static void pin_tag(unsigned long handle)
861{
862 while (!trypin_tag(handle));
863}
864
865static void unpin_tag(unsigned long handle)
866{
867 unsigned long *ptr = (unsigned long *)handle;
868
869 clear_bit_unlock(HANDLE_PIN_BIT, ptr);
870}
871
Nitin Guptaf4477e92012-04-02 09:13:56 -0500872static void reset_page(struct page *page)
873{
874 clear_bit(PG_private, &page->flags);
875 clear_bit(PG_private_2, &page->flags);
876 set_page_private(page, 0);
877 page->mapping = NULL;
878 page->freelist = NULL;
Mel Gorman22b751c2013-02-22 16:34:59 -0800879 page_mapcount_reset(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500880}
881
Nitin Gupta61989a82012-01-09 16:51:56 -0600882static void free_zspage(struct page *first_page)
883{
Nitin Guptaf4477e92012-04-02 09:13:56 -0500884 struct page *nextp, *tmp, *head_extra;
Nitin Gupta61989a82012-01-09 16:51:56 -0600885
886 BUG_ON(!is_first_page(first_page));
887 BUG_ON(first_page->inuse);
888
Nitin Guptaf4477e92012-04-02 09:13:56 -0500889 head_extra = (struct page *)page_private(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600890
Nitin Guptaf4477e92012-04-02 09:13:56 -0500891 reset_page(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600892 __free_page(first_page);
893
894 /* zspage with only 1 system page */
Nitin Guptaf4477e92012-04-02 09:13:56 -0500895 if (!head_extra)
Nitin Gupta61989a82012-01-09 16:51:56 -0600896 return;
897
Nitin Guptaf4477e92012-04-02 09:13:56 -0500898 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600899 list_del(&nextp->lru);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500900 reset_page(nextp);
Nitin Gupta61989a82012-01-09 16:51:56 -0600901 __free_page(nextp);
902 }
Nitin Guptaf4477e92012-04-02 09:13:56 -0500903 reset_page(head_extra);
904 __free_page(head_extra);
Nitin Gupta61989a82012-01-09 16:51:56 -0600905}
906
907/* Initialize a newly allocated zspage */
908static void init_zspage(struct page *first_page, struct size_class *class)
909{
910 unsigned long off = 0;
911 struct page *page = first_page;
912
913 BUG_ON(!is_first_page(first_page));
914 while (page) {
915 struct page *next_page;
916 struct link_free *link;
Dan Streetman5538c562014-10-09 15:30:01 -0700917 unsigned int i = 1;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800918 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -0600919
920 /*
921 * page->index stores offset of first object starting
922 * in the page. For the first page, this is always 0,
923 * so we use first_page->index (aka ->freelist) to store
924 * head of corresponding zspage's freelist.
925 */
926 if (page != first_page)
927 page->index = off;
928
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800929 vaddr = kmap_atomic(page);
930 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600931
Dan Streetman5538c562014-10-09 15:30:01 -0700932 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -0700933 link->next = location_to_obj(page, i++);
Dan Streetman5538c562014-10-09 15:30:01 -0700934 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -0600935 }
936
937 /*
938 * We now come to the last (full or partial) object on this
939 * page, which must point to the first object on the next
940 * page (if present)
941 */
942 next_page = get_next_page(page);
Minchan Kim312fcae2015-04-15 16:15:30 -0700943 link->next = location_to_obj(next_page, 0);
Minchan Kimaf4ee5e2014-12-12 16:56:58 -0800944 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -0600945 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -0700946 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -0600947 }
948}
949
950/*
951 * Allocate a zspage for the given size class
952 */
953static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
954{
955 int i, error;
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500956 struct page *first_page = NULL, *uninitialized_var(prev_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600957
958 /*
959 * Allocate individual pages and link them together as:
960 * 1. first page->private = first sub-page
961 * 2. all sub-pages are linked together using page->lru
962 * 3. each sub-page is linked to the first page using page->first_page
963 *
964 * For each size class, First/Head pages are linked together using
965 * page->lru. Also, we set PG_private to identify the first page
966 * (i.e. no other sub-page has this flag set) and PG_private_2 to
967 * identify the last page.
968 */
969 error = -ENOMEM;
Minchan Kim2e3b6152012-05-03 15:40:39 +0900970 for (i = 0; i < class->pages_per_zspage; i++) {
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500971 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600972
973 page = alloc_page(flags);
974 if (!page)
975 goto cleanup;
976
977 INIT_LIST_HEAD(&page->lru);
978 if (i == 0) { /* first page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900979 SetPagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600980 set_page_private(page, 0);
981 first_page = page;
982 first_page->inuse = 0;
983 }
984 if (i == 1)
Sunghan Suhe842b972013-07-12 16:08:13 +0900985 set_page_private(first_page, (unsigned long)page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600986 if (i >= 1)
987 page->first_page = first_page;
988 if (i >= 2)
989 list_add(&page->lru, &prev_page->lru);
Minchan Kim2e3b6152012-05-03 15:40:39 +0900990 if (i == class->pages_per_zspage - 1) /* last page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900991 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600992 prev_page = page;
993 }
994
995 init_zspage(first_page, class);
996
Minchan Kim312fcae2015-04-15 16:15:30 -0700997 first_page->freelist = location_to_obj(first_page, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -0600998 /* Maximum number of objects we can store in this zspage */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900999 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
Nitin Gupta61989a82012-01-09 16:51:56 -06001000
1001 error = 0; /* Success */
1002
1003cleanup:
1004 if (unlikely(error) && first_page) {
1005 free_zspage(first_page);
1006 first_page = NULL;
1007 }
1008
1009 return first_page;
1010}
1011
1012static struct page *find_get_zspage(struct size_class *class)
1013{
1014 int i;
1015 struct page *page;
1016
1017 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
1018 page = class->fullness_list[i];
1019 if (page)
1020 break;
1021 }
1022
1023 return page;
1024}
1025
Minchan Kim1b945ae2013-12-11 11:04:36 +09001026#ifdef CONFIG_PGTABLE_MAPPING
Seth Jenningsf5536462012-07-18 11:55:56 -05001027static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -05001028{
Seth Jenningsf5536462012-07-18 11:55:56 -05001029 /*
1030 * Make sure we don't leak memory if a cpu UP notification
1031 * and zs_init() race and both call zs_cpu_up() on the same cpu
1032 */
1033 if (area->vm)
1034 return 0;
1035 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
1036 if (!area->vm)
1037 return -ENOMEM;
1038 return 0;
1039}
1040
1041static inline void __zs_cpu_down(struct mapping_area *area)
1042{
1043 if (area->vm)
1044 free_vm_area(area->vm);
1045 area->vm = NULL;
1046}
1047
1048static inline void *__zs_map_object(struct mapping_area *area,
1049 struct page *pages[2], int off, int size)
1050{
WANG Chaof6f8ed42014-08-06 16:06:58 -07001051 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
Seth Jenningsf5536462012-07-18 11:55:56 -05001052 area->vm_addr = area->vm->addr;
1053 return area->vm_addr + off;
1054}
1055
1056static inline void __zs_unmap_object(struct mapping_area *area,
1057 struct page *pages[2], int off, int size)
1058{
1059 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001060
Joerg Roedeld95abbb2013-03-27 01:43:14 +01001061 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -05001062}
1063
Minchan Kim1b945ae2013-12-11 11:04:36 +09001064#else /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001065
1066static inline int __zs_cpu_up(struct mapping_area *area)
1067{
1068 /*
1069 * Make sure we don't leak memory if a cpu UP notification
1070 * and zs_init() race and both call zs_cpu_up() on the same cpu
1071 */
1072 if (area->vm_buf)
1073 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001074 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001075 if (!area->vm_buf)
1076 return -ENOMEM;
1077 return 0;
1078}
1079
1080static inline void __zs_cpu_down(struct mapping_area *area)
1081{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001082 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001083 area->vm_buf = NULL;
1084}
1085
1086static void *__zs_map_object(struct mapping_area *area,
1087 struct page *pages[2], int off, int size)
1088{
Seth Jennings5f601902012-07-02 16:15:49 -05001089 int sizes[2];
1090 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001091 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001092
Seth Jenningsf5536462012-07-18 11:55:56 -05001093 /* disable page faults to match kmap_atomic() return conditions */
1094 pagefault_disable();
1095
1096 /* no read fastpath */
1097 if (area->vm_mm == ZS_MM_WO)
1098 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001099
1100 sizes[0] = PAGE_SIZE - off;
1101 sizes[1] = size - sizes[0];
1102
Seth Jennings5f601902012-07-02 16:15:49 -05001103 /* copy object to per-cpu buffer */
1104 addr = kmap_atomic(pages[0]);
1105 memcpy(buf, addr + off, sizes[0]);
1106 kunmap_atomic(addr);
1107 addr = kmap_atomic(pages[1]);
1108 memcpy(buf + sizes[0], addr, sizes[1]);
1109 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001110out:
1111 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001112}
1113
Seth Jenningsf5536462012-07-18 11:55:56 -05001114static void __zs_unmap_object(struct mapping_area *area,
1115 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -05001116{
Seth Jennings5f601902012-07-02 16:15:49 -05001117 int sizes[2];
1118 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001119 char *buf;
Seth Jennings5f601902012-07-02 16:15:49 -05001120
Seth Jenningsf5536462012-07-18 11:55:56 -05001121 /* no write fastpath */
1122 if (area->vm_mm == ZS_MM_RO)
1123 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -05001124
Minchan Kim7b60a682015-04-15 16:15:39 -07001125 buf = area->vm_buf;
1126 if (!area->huge) {
1127 buf = buf + ZS_HANDLE_SIZE;
1128 size -= ZS_HANDLE_SIZE;
1129 off += ZS_HANDLE_SIZE;
1130 }
Minchan Kim2e40e162015-04-15 16:15:23 -07001131
Seth Jennings5f601902012-07-02 16:15:49 -05001132 sizes[0] = PAGE_SIZE - off;
1133 sizes[1] = size - sizes[0];
1134
1135 /* copy per-cpu buffer to object */
1136 addr = kmap_atomic(pages[0]);
1137 memcpy(addr + off, buf, sizes[0]);
1138 kunmap_atomic(addr);
1139 addr = kmap_atomic(pages[1]);
1140 memcpy(addr, buf + sizes[0], sizes[1]);
1141 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001142
1143out:
1144 /* enable page faults to match kunmap_atomic() return conditions */
1145 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -05001146}
Nitin Gupta61989a82012-01-09 16:51:56 -06001147
Minchan Kim1b945ae2013-12-11 11:04:36 +09001148#endif /* CONFIG_PGTABLE_MAPPING */
Seth Jenningsf5536462012-07-18 11:55:56 -05001149
Nitin Gupta61989a82012-01-09 16:51:56 -06001150static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
1151 void *pcpu)
1152{
Seth Jenningsf5536462012-07-18 11:55:56 -05001153 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -06001154 struct mapping_area *area;
1155
1156 switch (action) {
1157 case CPU_UP_PREPARE:
1158 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001159 ret = __zs_cpu_up(area);
1160 if (ret)
1161 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001162 break;
1163 case CPU_DEAD:
1164 case CPU_UP_CANCELED:
1165 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -05001166 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001167 break;
1168 }
1169
1170 return NOTIFY_OK;
1171}
1172
1173static struct notifier_block zs_cpu_nb = {
1174 .notifier_call = zs_cpu_notifier
1175};
1176
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001177static int zs_register_cpu_notifier(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001178{
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001179 int cpu, uninitialized_var(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -06001180
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301181 cpu_notifier_register_begin();
1182
1183 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -06001184 for_each_online_cpu(cpu) {
1185 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001186 if (notifier_to_errno(ret))
1187 break;
Nitin Gupta61989a82012-01-09 16:51:56 -06001188 }
Srivatsa S. Bhatf0e71fc2014-03-11 02:09:59 +05301189
1190 cpu_notifier_register_done();
Sergey Senozhatskyb1b00a52014-12-12 16:56:56 -08001191 return notifier_to_errno(ret);
1192}
1193
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001194static void zs_unregister_cpu_notifier(void)
1195{
1196 int cpu;
1197
1198 cpu_notifier_register_begin();
1199
1200 for_each_online_cpu(cpu)
1201 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
1202 __unregister_cpu_notifier(&zs_cpu_nb);
1203
1204 cpu_notifier_register_done();
1205}
1206
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001207static void init_zs_size_classes(void)
1208{
1209 int nr;
1210
1211 nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
1212 if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
1213 nr += 1;
1214
1215 zs_size_classes = nr;
1216}
1217
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001218static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
1219{
1220 if (prev->pages_per_zspage != pages_per_zspage)
1221 return false;
1222
1223 if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
1224 != get_maxobj_per_zspage(size, pages_per_zspage))
1225 return false;
1226
1227 return true;
1228}
1229
Minchan Kim312fcae2015-04-15 16:15:30 -07001230static bool zspage_full(struct page *page)
1231{
1232 BUG_ON(!is_first_page(page));
1233
1234 return page->inuse == page->objects;
1235}
1236
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001237unsigned long zs_get_total_pages(struct zs_pool *pool)
1238{
1239 return atomic_long_read(&pool->pages_allocated);
1240}
1241EXPORT_SYMBOL_GPL(zs_get_total_pages);
1242
1243/**
1244 * zs_map_object - get address of allocated object from handle.
1245 * @pool: pool from which the object was allocated
1246 * @handle: handle returned from zs_malloc
1247 *
1248 * Before using an object allocated from zs_malloc, it must be mapped using
1249 * this function. When done with the object, it must be unmapped using
1250 * zs_unmap_object.
1251 *
1252 * Only one object can be mapped per cpu at a time. There is no protection
1253 * against nested mappings.
1254 *
1255 * This function returns with preemption and page faults disabled.
1256 */
1257void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1258 enum zs_mapmode mm)
1259{
1260 struct page *page;
Minchan Kim2e40e162015-04-15 16:15:23 -07001261 unsigned long obj, obj_idx, off;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001262
1263 unsigned int class_idx;
1264 enum fullness_group fg;
1265 struct size_class *class;
1266 struct mapping_area *area;
1267 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001268 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001269
1270 BUG_ON(!handle);
1271
1272 /*
1273 * Because we use per-cpu mapping areas shared among the
1274 * pools/users, we can't allow mapping in interrupt context
1275 * because it can corrupt another users mappings.
1276 */
1277 BUG_ON(in_interrupt());
1278
Minchan Kim312fcae2015-04-15 16:15:30 -07001279 /* From now on, migration cannot move the object */
1280 pin_tag(handle);
1281
Minchan Kim2e40e162015-04-15 16:15:23 -07001282 obj = handle_to_obj(handle);
1283 obj_to_location(obj, &page, &obj_idx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001284 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1285 class = pool->size_class[class_idx];
1286 off = obj_idx_to_offset(page, obj_idx, class->size);
1287
1288 area = &get_cpu_var(zs_map_area);
1289 area->vm_mm = mm;
1290 if (off + class->size <= PAGE_SIZE) {
1291 /* this object is contained entirely within a page */
1292 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001293 ret = area->vm_addr + off;
1294 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001295 }
1296
1297 /* this object spans two pages */
1298 pages[0] = page;
1299 pages[1] = get_next_page(page);
1300 BUG_ON(!pages[1]);
1301
Minchan Kim2e40e162015-04-15 16:15:23 -07001302 ret = __zs_map_object(area, pages, off, class->size);
1303out:
Minchan Kim7b60a682015-04-15 16:15:39 -07001304 if (!class->huge)
1305 ret += ZS_HANDLE_SIZE;
1306
1307 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001308}
1309EXPORT_SYMBOL_GPL(zs_map_object);
1310
1311void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1312{
1313 struct page *page;
Minchan Kim2e40e162015-04-15 16:15:23 -07001314 unsigned long obj, obj_idx, off;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001315
1316 unsigned int class_idx;
1317 enum fullness_group fg;
1318 struct size_class *class;
1319 struct mapping_area *area;
1320
1321 BUG_ON(!handle);
1322
Minchan Kim2e40e162015-04-15 16:15:23 -07001323 obj = handle_to_obj(handle);
1324 obj_to_location(obj, &page, &obj_idx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001325 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1326 class = pool->size_class[class_idx];
1327 off = obj_idx_to_offset(page, obj_idx, class->size);
1328
1329 area = this_cpu_ptr(&zs_map_area);
1330 if (off + class->size <= PAGE_SIZE)
1331 kunmap_atomic(area->vm_addr);
1332 else {
1333 struct page *pages[2];
1334
1335 pages[0] = page;
1336 pages[1] = get_next_page(page);
1337 BUG_ON(!pages[1]);
1338
1339 __zs_unmap_object(area, pages, off, class->size);
1340 }
1341 put_cpu_var(zs_map_area);
Minchan Kim312fcae2015-04-15 16:15:30 -07001342 unpin_tag(handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001343}
1344EXPORT_SYMBOL_GPL(zs_unmap_object);
1345
Minchan Kimc7806262015-04-15 16:15:26 -07001346static unsigned long obj_malloc(struct page *first_page,
1347 struct size_class *class, unsigned long handle)
1348{
1349 unsigned long obj;
1350 struct link_free *link;
1351
1352 struct page *m_page;
1353 unsigned long m_objidx, m_offset;
1354 void *vaddr;
1355
Minchan Kim312fcae2015-04-15 16:15:30 -07001356 handle |= OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001357 obj = (unsigned long)first_page->freelist;
1358 obj_to_location(obj, &m_page, &m_objidx);
1359 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
1360
1361 vaddr = kmap_atomic(m_page);
1362 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
1363 first_page->freelist = link->next;
Minchan Kim7b60a682015-04-15 16:15:39 -07001364 if (!class->huge)
1365 /* record handle in the header of allocated chunk */
1366 link->handle = handle;
1367 else
1368 /* record handle in first_page->private */
1369 set_page_private(first_page, handle);
Minchan Kimc7806262015-04-15 16:15:26 -07001370 kunmap_atomic(vaddr);
1371 first_page->inuse++;
1372 zs_stat_inc(class, OBJ_USED, 1);
1373
1374 return obj;
1375}
1376
1377
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001378/**
1379 * zs_malloc - Allocate block of given size from pool.
1380 * @pool: pool to allocate from
1381 * @size: size of block to allocate
1382 *
1383 * On success, handle to the allocated object is returned,
1384 * otherwise 0.
1385 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1386 */
1387unsigned long zs_malloc(struct zs_pool *pool, size_t size)
1388{
Minchan Kim2e40e162015-04-15 16:15:23 -07001389 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001390 struct size_class *class;
Minchan Kimc7806262015-04-15 16:15:26 -07001391 struct page *first_page;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001392
Minchan Kim7b60a682015-04-15 16:15:39 -07001393 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001394 return 0;
1395
Minchan Kim2e40e162015-04-15 16:15:23 -07001396 handle = alloc_handle(pool);
1397 if (!handle)
1398 return 0;
1399
1400 /* extra space in chunk to keep the handle */
1401 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001402 class = pool->size_class[get_size_class_index(size)];
1403
1404 spin_lock(&class->lock);
1405 first_page = find_get_zspage(class);
1406
1407 if (!first_page) {
1408 spin_unlock(&class->lock);
1409 first_page = alloc_zspage(class, pool->flags);
Minchan Kim2e40e162015-04-15 16:15:23 -07001410 if (unlikely(!first_page)) {
1411 free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001412 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -07001413 }
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001414
1415 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
1416 atomic_long_add(class->pages_per_zspage,
1417 &pool->pages_allocated);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001418
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001419 spin_lock(&class->lock);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001420 zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1421 class->size, class->pages_per_zspage));
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001422 }
1423
Minchan Kimc7806262015-04-15 16:15:26 -07001424 obj = obj_malloc(first_page, class, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001425 /* Now move the zspage to another fullness group, if required */
Minchan Kimc7806262015-04-15 16:15:26 -07001426 fix_fullness_group(class, first_page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001427 record_obj(handle, obj);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001428 spin_unlock(&class->lock);
1429
Minchan Kim2e40e162015-04-15 16:15:23 -07001430 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001431}
1432EXPORT_SYMBOL_GPL(zs_malloc);
1433
Minchan Kimc7806262015-04-15 16:15:26 -07001434static void obj_free(struct zs_pool *pool, struct size_class *class,
1435 unsigned long obj)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001436{
1437 struct link_free *link;
1438 struct page *first_page, *f_page;
Minchan Kimc7806262015-04-15 16:15:26 -07001439 unsigned long f_objidx, f_offset;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001440 void *vaddr;
1441
Minchan Kimc7806262015-04-15 16:15:26 -07001442 BUG_ON(!obj);
1443
Minchan Kim312fcae2015-04-15 16:15:30 -07001444 obj &= ~OBJ_ALLOCATED_TAG;
Minchan Kimc7806262015-04-15 16:15:26 -07001445 obj_to_location(obj, &f_page, &f_objidx);
1446 first_page = get_first_page(f_page);
1447
Minchan Kimc7806262015-04-15 16:15:26 -07001448 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
1449
1450 vaddr = kmap_atomic(f_page);
1451
1452 /* Insert this object in containing zspage's freelist */
1453 link = (struct link_free *)(vaddr + f_offset);
1454 link->next = first_page->freelist;
Minchan Kim7b60a682015-04-15 16:15:39 -07001455 if (class->huge)
1456 set_page_private(first_page, 0);
Minchan Kimc7806262015-04-15 16:15:26 -07001457 kunmap_atomic(vaddr);
1458 first_page->freelist = (void *)obj;
1459 first_page->inuse--;
1460 zs_stat_dec(class, OBJ_USED, 1);
1461}
1462
1463void zs_free(struct zs_pool *pool, unsigned long handle)
1464{
1465 struct page *first_page, *f_page;
1466 unsigned long obj, f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001467 int class_idx;
1468 struct size_class *class;
1469 enum fullness_group fullness;
1470
Minchan Kim2e40e162015-04-15 16:15:23 -07001471 if (unlikely(!handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001472 return;
1473
Minchan Kim312fcae2015-04-15 16:15:30 -07001474 pin_tag(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001475 obj = handle_to_obj(handle);
Minchan Kim2e40e162015-04-15 16:15:23 -07001476 obj_to_location(obj, &f_page, &f_objidx);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001477 first_page = get_first_page(f_page);
1478
1479 get_zspage_mapping(first_page, &class_idx, &fullness);
1480 class = pool->size_class[class_idx];
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001481
1482 spin_lock(&class->lock);
Minchan Kimc7806262015-04-15 16:15:26 -07001483 obj_free(pool, class, obj);
1484 fullness = fix_fullness_group(class, first_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001485 if (fullness == ZS_EMPTY) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001486 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1487 class->size, class->pages_per_zspage));
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001488 atomic_long_sub(class->pages_per_zspage,
1489 &pool->pages_allocated);
1490 free_zspage(first_page);
1491 }
Minchan Kim312fcae2015-04-15 16:15:30 -07001492 spin_unlock(&class->lock);
1493 unpin_tag(handle);
1494
1495 free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001496}
1497EXPORT_SYMBOL_GPL(zs_free);
1498
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001499static void zs_object_copy(unsigned long dst, unsigned long src,
Minchan Kim312fcae2015-04-15 16:15:30 -07001500 struct size_class *class)
1501{
1502 struct page *s_page, *d_page;
1503 unsigned long s_objidx, d_objidx;
1504 unsigned long s_off, d_off;
1505 void *s_addr, *d_addr;
1506 int s_size, d_size, size;
1507 int written = 0;
1508
1509 s_size = d_size = class->size;
1510
1511 obj_to_location(src, &s_page, &s_objidx);
1512 obj_to_location(dst, &d_page, &d_objidx);
1513
1514 s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
1515 d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
1516
1517 if (s_off + class->size > PAGE_SIZE)
1518 s_size = PAGE_SIZE - s_off;
1519
1520 if (d_off + class->size > PAGE_SIZE)
1521 d_size = PAGE_SIZE - d_off;
1522
1523 s_addr = kmap_atomic(s_page);
1524 d_addr = kmap_atomic(d_page);
1525
1526 while (1) {
1527 size = min(s_size, d_size);
1528 memcpy(d_addr + d_off, s_addr + s_off, size);
1529 written += size;
1530
1531 if (written == class->size)
1532 break;
1533
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001534 s_off += size;
1535 s_size -= size;
1536 d_off += size;
1537 d_size -= size;
1538
1539 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001540 kunmap_atomic(d_addr);
1541 kunmap_atomic(s_addr);
1542 s_page = get_next_page(s_page);
1543 BUG_ON(!s_page);
1544 s_addr = kmap_atomic(s_page);
1545 d_addr = kmap_atomic(d_page);
1546 s_size = class->size - written;
1547 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001548 }
1549
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001550 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001551 kunmap_atomic(d_addr);
1552 d_page = get_next_page(d_page);
1553 BUG_ON(!d_page);
1554 d_addr = kmap_atomic(d_page);
1555 d_size = class->size - written;
1556 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001557 }
1558 }
1559
1560 kunmap_atomic(d_addr);
1561 kunmap_atomic(s_addr);
1562}
1563
1564/*
1565 * Find alloced object in zspage from index object and
1566 * return handle.
1567 */
1568static unsigned long find_alloced_obj(struct page *page, int index,
1569 struct size_class *class)
1570{
1571 unsigned long head;
1572 int offset = 0;
1573 unsigned long handle = 0;
1574 void *addr = kmap_atomic(page);
1575
1576 if (!is_first_page(page))
1577 offset = page->index;
1578 offset += class->size * index;
1579
1580 while (offset < PAGE_SIZE) {
Minchan Kim7b60a682015-04-15 16:15:39 -07001581 head = obj_to_head(class, page, addr + offset);
Minchan Kim312fcae2015-04-15 16:15:30 -07001582 if (head & OBJ_ALLOCATED_TAG) {
1583 handle = head & ~OBJ_ALLOCATED_TAG;
1584 if (trypin_tag(handle))
1585 break;
1586 handle = 0;
1587 }
1588
1589 offset += class->size;
1590 index++;
1591 }
1592
1593 kunmap_atomic(addr);
1594 return handle;
1595}
1596
1597struct zs_compact_control {
1598 /* Source page for migration which could be a subpage of zspage. */
1599 struct page *s_page;
1600 /* Destination page for migration which should be a first page
1601 * of zspage. */
1602 struct page *d_page;
1603 /* Starting object index within @s_page which used for live object
1604 * in the subpage. */
1605 int index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001606};
1607
1608static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1609 struct zs_compact_control *cc)
1610{
1611 unsigned long used_obj, free_obj;
1612 unsigned long handle;
1613 struct page *s_page = cc->s_page;
1614 struct page *d_page = cc->d_page;
1615 unsigned long index = cc->index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001616 int ret = 0;
1617
1618 while (1) {
1619 handle = find_alloced_obj(s_page, index, class);
1620 if (!handle) {
1621 s_page = get_next_page(s_page);
1622 if (!s_page)
1623 break;
1624 index = 0;
1625 continue;
1626 }
1627
1628 /* Stop if there is no more space */
1629 if (zspage_full(d_page)) {
1630 unpin_tag(handle);
1631 ret = -ENOMEM;
1632 break;
1633 }
1634
1635 used_obj = handle_to_obj(handle);
1636 free_obj = obj_malloc(d_page, class, handle);
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001637 zs_object_copy(free_obj, used_obj, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07001638 index++;
1639 record_obj(handle, free_obj);
1640 unpin_tag(handle);
1641 obj_free(pool, class, used_obj);
Minchan Kim312fcae2015-04-15 16:15:30 -07001642 }
1643
1644 /* Remember last position in this iteration */
1645 cc->s_page = s_page;
1646 cc->index = index;
Minchan Kim312fcae2015-04-15 16:15:30 -07001647
1648 return ret;
1649}
1650
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001651static struct page *isolate_target_page(struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07001652{
1653 int i;
1654 struct page *page;
1655
1656 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
1657 page = class->fullness_list[i];
1658 if (page) {
1659 remove_zspage(page, class, i);
1660 break;
1661 }
1662 }
1663
1664 return page;
1665}
1666
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001667/*
1668 * putback_zspage - add @first_page into right class's fullness list
1669 * @pool: target pool
1670 * @class: destination class
1671 * @first_page: target page
1672 *
1673 * Return @fist_page's fullness_group
1674 */
1675static enum fullness_group putback_zspage(struct zs_pool *pool,
1676 struct size_class *class,
1677 struct page *first_page)
Minchan Kim312fcae2015-04-15 16:15:30 -07001678{
Minchan Kim312fcae2015-04-15 16:15:30 -07001679 enum fullness_group fullness;
1680
1681 BUG_ON(!is_first_page(first_page));
1682
Minchan Kim839373e2015-04-15 16:16:18 -07001683 fullness = get_fullness_group(first_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001684 insert_zspage(first_page, class, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001685 set_zspage_mapping(first_page, class->index, fullness);
1686
Minchan Kim312fcae2015-04-15 16:15:30 -07001687 if (fullness == ZS_EMPTY) {
1688 zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
1689 class->size, class->pages_per_zspage));
1690 atomic_long_sub(class->pages_per_zspage,
1691 &pool->pages_allocated);
1692
1693 free_zspage(first_page);
1694 }
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001695
1696 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001697}
1698
1699static struct page *isolate_source_page(struct size_class *class)
1700{
Minchan Kimad9d5e12015-09-08 15:04:47 -07001701 int i;
1702 struct page *page = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07001703
Minchan Kimad9d5e12015-09-08 15:04:47 -07001704 for (i = ZS_ALMOST_EMPTY; i >= ZS_ALMOST_FULL; i--) {
1705 page = class->fullness_list[i];
1706 if (!page)
1707 continue;
1708
1709 remove_zspage(page, class, i);
1710 break;
1711 }
Minchan Kim312fcae2015-04-15 16:15:30 -07001712
1713 return page;
1714}
1715
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001716/*
1717 *
1718 * Based on the number of unused allocated objects calculate
1719 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001720 */
1721static unsigned long zs_can_compact(struct size_class *class)
1722{
1723 unsigned long obj_wasted;
1724
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001725 obj_wasted = zs_stat_get(class, OBJ_ALLOCATED) -
1726 zs_stat_get(class, OBJ_USED);
1727
1728 obj_wasted /= get_maxobj_per_zspage(class->size,
1729 class->pages_per_zspage);
1730
Minchan Kim6cbf16b2015-09-08 15:04:49 -07001731 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001732}
1733
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001734static void __zs_compact(struct zs_pool *pool, struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07001735{
Minchan Kim312fcae2015-04-15 16:15:30 -07001736 struct zs_compact_control cc;
1737 struct page *src_page;
1738 struct page *dst_page = NULL;
Minchan Kim312fcae2015-04-15 16:15:30 -07001739
Minchan Kim312fcae2015-04-15 16:15:30 -07001740 spin_lock(&class->lock);
1741 while ((src_page = isolate_source_page(class))) {
1742
1743 BUG_ON(!is_first_page(src_page));
1744
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07001745 if (!zs_can_compact(class))
1746 break;
1747
Minchan Kim312fcae2015-04-15 16:15:30 -07001748 cc.index = 0;
1749 cc.s_page = src_page;
1750
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001751 while ((dst_page = isolate_target_page(class))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001752 cc.d_page = dst_page;
1753 /*
Sergey Senozhatsky0dc63d42015-09-08 15:04:33 -07001754 * If there is no more space in dst_page, resched
1755 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07001756 */
1757 if (!migrate_zspage(pool, class, &cc))
1758 break;
1759
1760 putback_zspage(pool, class, dst_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001761 }
1762
1763 /* Stop if we couldn't find slot */
1764 if (dst_page == NULL)
1765 break;
1766
1767 putback_zspage(pool, class, dst_page);
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001768 if (putback_zspage(pool, class, src_page) == ZS_EMPTY)
Minchan Kim6cbf16b2015-09-08 15:04:49 -07001769 pool->stats.pages_compacted += class->pages_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001770 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07001771 cond_resched();
1772 spin_lock(&class->lock);
1773 }
1774
1775 if (src_page)
1776 putback_zspage(pool, class, src_page);
1777
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001778 spin_unlock(&class->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07001779}
1780
1781unsigned long zs_compact(struct zs_pool *pool)
1782{
1783 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07001784 struct size_class *class;
1785
1786 for (i = zs_size_classes - 1; i >= 0; i--) {
1787 class = pool->size_class[i];
1788 if (!class)
1789 continue;
1790 if (class->index != i)
1791 continue;
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001792 __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07001793 }
1794
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001795 return pool->stats.pages_compacted;
Minchan Kim312fcae2015-04-15 16:15:30 -07001796}
1797EXPORT_SYMBOL_GPL(zs_compact);
1798
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07001799void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
1800{
1801 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
1802}
1803EXPORT_SYMBOL_GPL(zs_pool_stats);
1804
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001805static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
1806 struct shrink_control *sc)
1807{
1808 unsigned long pages_freed;
1809 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1810 shrinker);
1811
1812 pages_freed = pool->stats.pages_compacted;
1813 /*
1814 * Compact classes and calculate compaction delta.
1815 * Can run concurrently with a manually triggered
1816 * (by user) compaction.
1817 */
1818 pages_freed = zs_compact(pool) - pages_freed;
1819
1820 return pages_freed ? pages_freed : SHRINK_STOP;
1821}
1822
1823static unsigned long zs_shrinker_count(struct shrinker *shrinker,
1824 struct shrink_control *sc)
1825{
1826 int i;
1827 struct size_class *class;
1828 unsigned long pages_to_free = 0;
1829 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
1830 shrinker);
1831
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001832 for (i = zs_size_classes - 1; i >= 0; i--) {
1833 class = pool->size_class[i];
1834 if (!class)
1835 continue;
1836 if (class->index != i)
1837 continue;
1838
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001839 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001840 }
1841
1842 return pages_to_free;
1843}
1844
1845static void zs_unregister_shrinker(struct zs_pool *pool)
1846{
1847 if (pool->shrinker_enabled) {
1848 unregister_shrinker(&pool->shrinker);
1849 pool->shrinker_enabled = false;
1850 }
1851}
1852
1853static int zs_register_shrinker(struct zs_pool *pool)
1854{
1855 pool->shrinker.scan_objects = zs_shrinker_scan;
1856 pool->shrinker.count_objects = zs_shrinker_count;
1857 pool->shrinker.batch = 0;
1858 pool->shrinker.seeks = DEFAULT_SEEKS;
1859
1860 return register_shrinker(&pool->shrinker);
1861}
1862
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001863/**
1864 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -06001865 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08001866 *
1867 * This function must be called before anything when using
1868 * the zsmalloc allocator.
1869 *
1870 * On success, a pointer to the newly created pool is returned,
1871 * otherwise NULL.
1872 */
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -08001873struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
Nitin Gupta61989a82012-01-09 16:51:56 -06001874{
Ganesh Mahendran18136652014-12-12 16:57:10 -08001875 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06001876 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001877 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001878
Ganesh Mahendran18136652014-12-12 16:57:10 -08001879 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001880 if (!pool)
1881 return NULL;
1882
Minchan Kim2e40e162015-04-15 16:15:23 -07001883 pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
1884 GFP_KERNEL);
1885 if (!pool->size_class) {
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001886 kfree(pool);
1887 return NULL;
1888 }
1889
Minchan Kim2e40e162015-04-15 16:15:23 -07001890 pool->name = kstrdup(name, GFP_KERNEL);
1891 if (!pool->name)
1892 goto err;
1893
1894 if (create_handle_cache(pool))
1895 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001896
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001897 /*
1898 * Iterate reversly, because, size of size_class that we want to use
1899 * for merging should be larger or equal to current size.
1900 */
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001901 for (i = zs_size_classes - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001902 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001903 int pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001904 struct size_class *class;
1905
1906 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
1907 if (size > ZS_MAX_ALLOC_SIZE)
1908 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001909 pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001910
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001911 /*
1912 * size_class is used for normal zsmalloc operation such
1913 * as alloc/free for that size. Although it is natural that we
1914 * have one size_class for each size, there is a chance that we
1915 * can get more memory utilization if we use one size_class for
1916 * many different sizes whose size_class have same
1917 * characteristics. So, we makes size_class point to
1918 * previous size_class if possible.
1919 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001920 if (prev_class) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001921 if (can_merge(prev_class, size, pages_per_zspage)) {
1922 pool->size_class[i] = prev_class;
1923 continue;
1924 }
1925 }
1926
1927 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
1928 if (!class)
1929 goto err;
1930
Nitin Gupta61989a82012-01-09 16:51:56 -06001931 class->size = size;
1932 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001933 class->pages_per_zspage = pages_per_zspage;
Minchan Kim7b60a682015-04-15 16:15:39 -07001934 if (pages_per_zspage == 1 &&
1935 get_maxobj_per_zspage(size, pages_per_zspage) == 1)
1936 class->huge = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06001937 spin_lock_init(&class->lock);
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001938 pool->size_class[i] = class;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08001939
1940 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06001941 }
1942
Nitin Gupta61989a82012-01-09 16:51:56 -06001943 pool->flags = flags;
Nitin Gupta61989a82012-01-09 16:51:56 -06001944
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001945 if (zs_pool_stat_create(name, pool))
1946 goto err;
1947
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001948 /*
1949 * Not critical, we still can use the pool
1950 * and user can trigger compaction manually.
1951 */
1952 if (zs_register_shrinker(pool) == 0)
1953 pool->shrinker_enabled = true;
Nitin Gupta61989a82012-01-09 16:51:56 -06001954 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001955
1956err:
1957 zs_destroy_pool(pool);
1958 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06001959}
1960EXPORT_SYMBOL_GPL(zs_create_pool);
1961
1962void zs_destroy_pool(struct zs_pool *pool)
1963{
1964 int i;
1965
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07001966 zs_unregister_shrinker(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001967 zs_pool_stat_destroy(pool);
1968
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001969 for (i = 0; i < zs_size_classes; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06001970 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001971 struct size_class *class = pool->size_class[i];
1972
1973 if (!class)
1974 continue;
1975
1976 if (class->index != i)
1977 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06001978
1979 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
1980 if (class->fullness_list[fg]) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04001981 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06001982 class->size, fg);
1983 }
1984 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001985 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06001986 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001987
Minchan Kim2e40e162015-04-15 16:15:23 -07001988 destroy_handle_cache(pool);
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001989 kfree(pool->size_class);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001990 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06001991 kfree(pool);
1992}
1993EXPORT_SYMBOL_GPL(zs_destroy_pool);
1994
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001995static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06001996{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001997 int ret = zs_register_cpu_notifier();
Nitin Gupta61989a82012-01-09 16:51:56 -06001998
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08001999 if (ret)
2000 goto notifier_fail;
Nitin Gupta61989a82012-01-09 16:51:56 -06002001
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002002 init_zs_size_classes();
Nitin Gupta61989a82012-01-09 16:51:56 -06002003
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002004#ifdef CONFIG_ZPOOL
2005 zpool_register_driver(&zs_zpool_driver);
2006#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002007
2008 ret = zs_stat_init();
2009 if (ret) {
2010 pr_err("zs stat initialization failed\n");
2011 goto stat_fail;
2012 }
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002013 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002014
2015stat_fail:
2016#ifdef CONFIG_ZPOOL
2017 zpool_unregister_driver(&zs_zpool_driver);
2018#endif
2019notifier_fail:
2020 zs_unregister_cpu_notifier();
2021
2022 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002023}
Nitin Gupta61989a82012-01-09 16:51:56 -06002024
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002025static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002026{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002027#ifdef CONFIG_ZPOOL
2028 zpool_unregister_driver(&zs_zpool_driver);
2029#endif
2030 zs_unregister_cpu_notifier();
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002031
2032 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002033}
Ben Hutchings069f1012012-06-20 02:31:11 +01002034
2035module_init(zs_init);
2036module_exit(zs_exit);
2037
2038MODULE_LICENSE("Dual BSD/GPL");
2039MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");