blob: 3078eca4737da1fb754cf9b7e18d35741798a337 [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim00cfab32014-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
15/*
16 * This allocator is designed for use with zcache and zram. Thus, the
17 * allocator is supposed to work well under low memory conditions. In
18 * particular, it never attempts higher order page allocation which is
19 * very likely to fail under memory pressure. On the other hand, if we
20 * just use single (0-order) pages, it would suffer from very high
21 * fragmentation -- any object of size PAGE_SIZE/2 or larger would occupy
22 * an entire page. This was one of the major issues with its predecessor
23 * (xvmalloc).
24 *
25 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
26 * and links them together using various 'struct page' fields. These linked
27 * pages act as a single higher-order page i.e. an object can span 0-order
28 * page boundaries. The code refers to these linked pages as a single entity
29 * called zspage.
30 *
31 * Following is how we use various fields and flags of underlying
32 * struct page(s) to form a zspage.
33 *
34 * Usage of struct page fields:
35 * page->first_page: points to the first component (0-order) page
36 * page->index (union with page->freelist): offset of the first object
37 * starting in this page. For the first page, this is
38 * always 0, so we use this field (aka freelist) to point
39 * to the first free object in zspage.
40 * page->lru: links together all component pages (except the first page)
41 * of a zspage
42 *
43 * For _first_ page only:
44 *
45 * page->private (union with page->first_page): refers to the
46 * component page after the first page
47 * page->freelist: points to the first free object in zspage.
48 * Free objects are linked together using in-place
49 * metadata.
50 * page->objects: maximum number of objects we can store in this
51 * zspage (class->zspage_order * PAGE_SIZE / class->size)
52 * page->lru: links together first pages of various zspages.
53 * Basically forming list of zspages in a fullness group.
54 * page->mapping: class index and fullness group of the zspage
55 *
56 * Usage of struct page flags:
57 * PG_private: identifies the first component page
58 * PG_private2: identifies the last component page
59 *
60 */
61
Nitin Gupta61989a82012-01-09 16:51:56 -060062#ifdef CONFIG_ZSMALLOC_DEBUG
63#define DEBUG
64#endif
65
66#include <linux/module.h>
67#include <linux/kernel.h>
68#include <linux/bitops.h>
69#include <linux/errno.h>
70#include <linux/highmem.h>
71#include <linux/init.h>
72#include <linux/string.h>
73#include <linux/slab.h>
74#include <asm/tlbflush.h>
75#include <asm/pgtable.h>
76#include <linux/cpumask.h>
77#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060078#include <linux/vmalloc.h>
Seth Jenningsc60369f2012-07-18 11:55:55 -050079#include <linux/hardirq.h>
Seth Jennings0959c632012-08-08 15:12:17 +090080#include <linux/spinlock.h>
81#include <linux/types.h>
Minchan Kim03dc2ac2014-01-30 15:45:50 -080082#include <linux/zsmalloc.h>
Seth Jennings0959c632012-08-08 15:12:17 +090083
84/*
85 * This must be power of 2 and greater than of equal to sizeof(link_free).
86 * These two conditions ensure that any 'struct link_free' itself doesn't
87 * span more than 1 page which avoids complex case of mapping 2 pages simply
88 * to restore link_free pointer values.
89 */
90#define ZS_ALIGN 8
91
92/*
93 * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
94 * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
95 */
96#define ZS_MAX_ZSPAGE_ORDER 2
97#define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
98
99/*
100 * Object location (<PFN>, <obj_idx>) is encoded as
101 * as single (void *) handle value.
102 *
103 * Note that object index <obj_idx> is relative to system
104 * page <PFN> it is stored in, so for each sub-page belonging
105 * to a zspage, obj_idx starts with 0.
106 *
107 * This is made more complicated by various memory models and PAE.
108 */
109
110#ifndef MAX_PHYSMEM_BITS
111#ifdef CONFIG_HIGHMEM64G
112#define MAX_PHYSMEM_BITS 36
113#else /* !CONFIG_HIGHMEM64G */
114/*
115 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
116 * be PAGE_SHIFT
117 */
118#define MAX_PHYSMEM_BITS BITS_PER_LONG
119#endif
120#endif
121#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
122#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
123#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
124
125#define MAX(a, b) ((a) >= (b) ? (a) : (b))
126/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
127#define ZS_MIN_ALLOC_SIZE \
128 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
129#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
130
131/*
Weijie Yangc398e6a2014-06-04 16:11:08 -0700132 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900133 * trader-off here:
134 * - Large number of size classes is potentially wasteful as free page are
135 * spread across these classes
136 * - Small number of size classes causes large internal fragmentation
137 * - Probably its better to use specific size classes (empirically
138 * determined). NOTE: all those class sizes must be set as multiple of
139 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
140 *
141 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
142 * (reason above)
143 */
Seth Jenningsd662b8e2013-01-25 11:46:18 -0600144#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
Seth Jennings0959c632012-08-08 15:12:17 +0900145#define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
146 ZS_SIZE_CLASS_DELTA + 1)
147
148/*
149 * We do not maintain any list for completely empty or full pages
150 */
151enum fullness_group {
152 ZS_ALMOST_FULL,
153 ZS_ALMOST_EMPTY,
154 _ZS_NR_FULLNESS_GROUPS,
155
156 ZS_EMPTY,
157 ZS_FULL
158};
159
160/*
161 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
162 * n <= N / f, where
163 * n = number of allocated objects
164 * N = total number of objects zspage can store
165 * f = 1/fullness_threshold_frac
166 *
167 * Similarly, we assign zspage to:
168 * ZS_ALMOST_FULL when n > N / f
169 * ZS_EMPTY when n == 0
170 * ZS_FULL when n == N
171 *
172 * (see: fix_fullness_group())
173 */
174static const int fullness_threshold_frac = 4;
175
176struct size_class {
177 /*
178 * Size of objects stored in this class. Must be multiple
179 * of ZS_ALIGN.
180 */
181 int size;
182 unsigned int index;
183
184 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
185 int pages_per_zspage;
186
187 spinlock_t lock;
188
189 /* stats */
190 u64 pages_allocated;
191
192 struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
193};
194
195/*
196 * Placed within free objects to form a singly linked list.
197 * For every zspage, first_page->freelist gives head of this list.
198 *
199 * This must be power of 2 and less than or equal to ZS_ALIGN
200 */
201struct link_free {
202 /* Handle of next free chunk (encodes <PFN, obj_idx>) */
203 void *next;
204};
205
206struct zs_pool {
207 struct size_class size_class[ZS_SIZE_CLASSES];
208
209 gfp_t flags; /* allocation flags used when growing pool */
Seth Jennings0959c632012-08-08 15:12:17 +0900210};
Nitin Gupta61989a82012-01-09 16:51:56 -0600211
212/*
213 * A zspage's class index and fullness group
214 * are encoded in its (first)page->mapping
215 */
216#define CLASS_IDX_BITS 28
217#define FULLNESS_BITS 4
218#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
219#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
220
Seth Jenningsf5536462012-07-18 11:55:56 -0500221/*
222 * By default, zsmalloc uses a copy-based object mapping method to access
223 * allocations that span two pages. However, if a particular architecture
Minchan Kim99155182013-01-28 10:00:08 +0900224 * performs VM mapping faster than copying, then it should be added here
225 * so that USE_PGTABLE_MAPPING is defined. This causes zsmalloc to use
226 * page table mapping rather than copying for object mapping.
Seth Jenningsf5536462012-07-18 11:55:56 -0500227*/
Arnd Bergmann796ce5a2013-04-23 18:30:48 +0200228#if defined(CONFIG_ARM) && !defined(MODULE)
Seth Jenningsf5536462012-07-18 11:55:56 -0500229#define USE_PGTABLE_MAPPING
230#endif
231
232struct mapping_area {
233#ifdef USE_PGTABLE_MAPPING
234 struct vm_struct *vm; /* vm area for mapping object that span pages */
235#else
236 char *vm_buf; /* copy buffer for objects that span pages */
237#endif
238 char *vm_addr; /* address of kmap_atomic()'ed pages */
239 enum zs_mapmode vm_mm; /* mapping mode */
240};
241
Nitin Gupta61989a82012-01-09 16:51:56 -0600242/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
243static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
244
245static int is_first_page(struct page *page)
246{
Minchan Kima27545bf2012-04-25 15:23:09 +0900247 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600248}
249
250static int is_last_page(struct page *page)
251{
Minchan Kima27545bf2012-04-25 15:23:09 +0900252 return PagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600253}
254
255static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
256 enum fullness_group *fullness)
257{
258 unsigned long m;
259 BUG_ON(!is_first_page(page));
260
261 m = (unsigned long)page->mapping;
262 *fullness = m & FULLNESS_MASK;
263 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
264}
265
266static void set_zspage_mapping(struct page *page, unsigned int class_idx,
267 enum fullness_group fullness)
268{
269 unsigned long m;
270 BUG_ON(!is_first_page(page));
271
272 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
273 (fullness & FULLNESS_MASK);
274 page->mapping = (struct address_space *)m;
275}
276
277static int get_size_class_index(int size)
278{
279 int idx = 0;
280
281 if (likely(size > ZS_MIN_ALLOC_SIZE))
282 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
283 ZS_SIZE_CLASS_DELTA);
284
285 return idx;
286}
287
288static enum fullness_group get_fullness_group(struct page *page)
289{
290 int inuse, max_objects;
291 enum fullness_group fg;
292 BUG_ON(!is_first_page(page));
293
294 inuse = page->inuse;
295 max_objects = page->objects;
296
297 if (inuse == 0)
298 fg = ZS_EMPTY;
299 else if (inuse == max_objects)
300 fg = ZS_FULL;
301 else if (inuse <= max_objects / fullness_threshold_frac)
302 fg = ZS_ALMOST_EMPTY;
303 else
304 fg = ZS_ALMOST_FULL;
305
306 return fg;
307}
308
309static void insert_zspage(struct page *page, struct size_class *class,
310 enum fullness_group fullness)
311{
312 struct page **head;
313
314 BUG_ON(!is_first_page(page));
315
316 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
317 return;
318
319 head = &class->fullness_list[fullness];
320 if (*head)
321 list_add_tail(&page->lru, &(*head)->lru);
322
323 *head = page;
324}
325
326static void remove_zspage(struct page *page, struct size_class *class,
327 enum fullness_group fullness)
328{
329 struct page **head;
330
331 BUG_ON(!is_first_page(page));
332
333 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
334 return;
335
336 head = &class->fullness_list[fullness];
337 BUG_ON(!*head);
338 if (list_empty(&(*head)->lru))
339 *head = NULL;
340 else if (*head == page)
341 *head = (struct page *)list_entry((*head)->lru.next,
342 struct page, lru);
343
344 list_del_init(&page->lru);
345}
346
347static enum fullness_group fix_fullness_group(struct zs_pool *pool,
348 struct page *page)
349{
350 int class_idx;
351 struct size_class *class;
352 enum fullness_group currfg, newfg;
353
354 BUG_ON(!is_first_page(page));
355
356 get_zspage_mapping(page, &class_idx, &currfg);
357 newfg = get_fullness_group(page);
358 if (newfg == currfg)
359 goto out;
360
361 class = &pool->size_class[class_idx];
362 remove_zspage(page, class, currfg);
363 insert_zspage(page, class, newfg);
364 set_zspage_mapping(page, class_idx, newfg);
365
366out:
367 return newfg;
368}
369
370/*
371 * We have to decide on how many pages to link together
372 * to form a zspage for each size class. This is important
373 * to reduce wastage due to unusable space left at end of
374 * each zspage which is given as:
375 * wastage = Zp - Zp % size_class
376 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
377 *
378 * For example, for size class of 3/8 * PAGE_SIZE, we should
379 * link together 3 PAGE_SIZE sized pages to form a zspage
380 * since then we can perfectly fit in 8 such objects.
381 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900382static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600383{
384 int i, max_usedpc = 0;
385 /* zspage order which gives maximum used size per KB */
386 int max_usedpc_order = 1;
387
Seth Jennings84d4faa2012-03-05 11:33:21 -0600388 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600389 int zspage_size;
390 int waste, usedpc;
391
392 zspage_size = i * PAGE_SIZE;
393 waste = zspage_size % class_size;
394 usedpc = (zspage_size - waste) * 100 / zspage_size;
395
396 if (usedpc > max_usedpc) {
397 max_usedpc = usedpc;
398 max_usedpc_order = i;
399 }
400 }
401
402 return max_usedpc_order;
403}
404
405/*
406 * A single 'zspage' is composed of many system pages which are
407 * linked together using fields in struct page. This function finds
408 * the first/head page, given any component page of a zspage.
409 */
410static struct page *get_first_page(struct page *page)
411{
412 if (is_first_page(page))
413 return page;
414 else
415 return page->first_page;
416}
417
418static struct page *get_next_page(struct page *page)
419{
420 struct page *next;
421
422 if (is_last_page(page))
423 next = NULL;
424 else if (is_first_page(page))
425 next = (struct page *)page->private;
426 else
427 next = list_entry(page->lru.next, struct page, lru);
428
429 return next;
430}
431
Olav Haugan6871b982013-11-22 09:30:41 -0800432/*
433 * Encode <page, obj_idx> as a single handle value.
434 * On hardware platforms with physical memory starting at 0x0 the pfn
435 * could be 0 so we ensure that the handle will never be 0 by adjusting the
436 * encoded obj_idx value before encoding.
437 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600438static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
439{
440 unsigned long handle;
441
442 if (!page) {
443 BUG_ON(obj_idx);
444 return NULL;
445 }
446
447 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
Olav Haugan6871b982013-11-22 09:30:41 -0800448 handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600449
450 return (void *)handle;
451}
452
Olav Haugan6871b982013-11-22 09:30:41 -0800453/*
454 * Decode <page, obj_idx> pair from the given object handle. We adjust the
455 * decoded obj_idx back to its original value since it was adjusted in
456 * obj_location_to_handle().
457 */
Minchan Kimc2344342012-06-08 15:39:25 +0900458static void obj_handle_to_location(unsigned long handle, struct page **page,
Nitin Gupta61989a82012-01-09 16:51:56 -0600459 unsigned long *obj_idx)
460{
Minchan Kimc2344342012-06-08 15:39:25 +0900461 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
Olav Haugan6871b982013-11-22 09:30:41 -0800462 *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
Nitin Gupta61989a82012-01-09 16:51:56 -0600463}
464
465static unsigned long obj_idx_to_offset(struct page *page,
466 unsigned long obj_idx, int class_size)
467{
468 unsigned long off = 0;
469
470 if (!is_first_page(page))
471 off = page->index;
472
473 return off + obj_idx * class_size;
474}
475
Nitin Guptaf4477e92012-04-02 09:13:56 -0500476static void reset_page(struct page *page)
477{
478 clear_bit(PG_private, &page->flags);
479 clear_bit(PG_private_2, &page->flags);
480 set_page_private(page, 0);
481 page->mapping = NULL;
482 page->freelist = NULL;
Mel Gorman22b751c2013-02-22 16:34:59 -0800483 page_mapcount_reset(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500484}
485
Nitin Gupta61989a82012-01-09 16:51:56 -0600486static void free_zspage(struct page *first_page)
487{
Nitin Guptaf4477e92012-04-02 09:13:56 -0500488 struct page *nextp, *tmp, *head_extra;
Nitin Gupta61989a82012-01-09 16:51:56 -0600489
490 BUG_ON(!is_first_page(first_page));
491 BUG_ON(first_page->inuse);
492
Nitin Guptaf4477e92012-04-02 09:13:56 -0500493 head_extra = (struct page *)page_private(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600494
Nitin Guptaf4477e92012-04-02 09:13:56 -0500495 reset_page(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600496 __free_page(first_page);
497
498 /* zspage with only 1 system page */
Nitin Guptaf4477e92012-04-02 09:13:56 -0500499 if (!head_extra)
Nitin Gupta61989a82012-01-09 16:51:56 -0600500 return;
501
Nitin Guptaf4477e92012-04-02 09:13:56 -0500502 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600503 list_del(&nextp->lru);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500504 reset_page(nextp);
Nitin Gupta61989a82012-01-09 16:51:56 -0600505 __free_page(nextp);
506 }
Nitin Guptaf4477e92012-04-02 09:13:56 -0500507 reset_page(head_extra);
508 __free_page(head_extra);
Nitin Gupta61989a82012-01-09 16:51:56 -0600509}
510
511/* Initialize a newly allocated zspage */
512static void init_zspage(struct page *first_page, struct size_class *class)
513{
514 unsigned long off = 0;
515 struct page *page = first_page;
516
517 BUG_ON(!is_first_page(first_page));
518 while (page) {
519 struct page *next_page;
520 struct link_free *link;
521 unsigned int i, objs_on_page;
522
523 /*
524 * page->index stores offset of first object starting
525 * in the page. For the first page, this is always 0,
526 * so we use first_page->index (aka ->freelist) to store
527 * head of corresponding zspage's freelist.
528 */
529 if (page != first_page)
530 page->index = off;
531
532 link = (struct link_free *)kmap_atomic(page) +
533 off / sizeof(*link);
534 objs_on_page = (PAGE_SIZE - off) / class->size;
535
536 for (i = 1; i <= objs_on_page; i++) {
537 off += class->size;
538 if (off < PAGE_SIZE) {
539 link->next = obj_location_to_handle(page, i);
540 link += class->size / sizeof(*link);
541 }
542 }
543
544 /*
545 * We now come to the last (full or partial) object on this
546 * page, which must point to the first object on the next
547 * page (if present)
548 */
549 next_page = get_next_page(page);
550 link->next = obj_location_to_handle(next_page, 0);
551 kunmap_atomic(link);
552 page = next_page;
553 off = (off + class->size) % PAGE_SIZE;
554 }
555}
556
557/*
558 * Allocate a zspage for the given size class
559 */
560static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
561{
562 int i, error;
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500563 struct page *first_page = NULL, *uninitialized_var(prev_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600564
565 /*
566 * Allocate individual pages and link them together as:
567 * 1. first page->private = first sub-page
568 * 2. all sub-pages are linked together using page->lru
569 * 3. each sub-page is linked to the first page using page->first_page
570 *
571 * For each size class, First/Head pages are linked together using
572 * page->lru. Also, we set PG_private to identify the first page
573 * (i.e. no other sub-page has this flag set) and PG_private_2 to
574 * identify the last page.
575 */
576 error = -ENOMEM;
Minchan Kim2e3b61542012-05-03 15:40:39 +0900577 for (i = 0; i < class->pages_per_zspage; i++) {
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500578 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600579
580 page = alloc_page(flags);
581 if (!page)
582 goto cleanup;
583
584 INIT_LIST_HEAD(&page->lru);
585 if (i == 0) { /* first page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900586 SetPagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600587 set_page_private(page, 0);
588 first_page = page;
589 first_page->inuse = 0;
590 }
591 if (i == 1)
592 first_page->private = (unsigned long)page;
593 if (i >= 1)
594 page->first_page = first_page;
595 if (i >= 2)
596 list_add(&page->lru, &prev_page->lru);
Minchan Kim2e3b61542012-05-03 15:40:39 +0900597 if (i == class->pages_per_zspage - 1) /* last page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900598 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600599 prev_page = page;
600 }
601
602 init_zspage(first_page, class);
603
604 first_page->freelist = obj_location_to_handle(first_page, 0);
605 /* Maximum number of objects we can store in this zspage */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900606 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600607
608 error = 0; /* Success */
609
610cleanup:
611 if (unlikely(error) && first_page) {
612 free_zspage(first_page);
613 first_page = NULL;
614 }
615
616 return first_page;
617}
618
619static struct page *find_get_zspage(struct size_class *class)
620{
621 int i;
622 struct page *page;
623
624 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
625 page = class->fullness_list[i];
626 if (page)
627 break;
628 }
629
630 return page;
631}
632
Seth Jenningsf5536462012-07-18 11:55:56 -0500633#ifdef USE_PGTABLE_MAPPING
634static inline int __zs_cpu_up(struct mapping_area *area)
Seth Jennings5f601902012-07-02 16:15:49 -0500635{
Seth Jenningsf5536462012-07-18 11:55:56 -0500636 /*
637 * Make sure we don't leak memory if a cpu UP notification
638 * and zs_init() race and both call zs_cpu_up() on the same cpu
639 */
640 if (area->vm)
641 return 0;
642 area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
643 if (!area->vm)
644 return -ENOMEM;
645 return 0;
646}
647
648static inline void __zs_cpu_down(struct mapping_area *area)
649{
650 if (area->vm)
651 free_vm_area(area->vm);
652 area->vm = NULL;
653}
654
655static inline void *__zs_map_object(struct mapping_area *area,
656 struct page *pages[2], int off, int size)
657{
658 BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
659 area->vm_addr = area->vm->addr;
660 return area->vm_addr + off;
661}
662
663static inline void __zs_unmap_object(struct mapping_area *area,
664 struct page *pages[2], int off, int size)
665{
666 unsigned long addr = (unsigned long)area->vm_addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500667
Joerg Roedeld95abbb2013-03-27 01:43:14 +0100668 unmap_kernel_range(addr, PAGE_SIZE * 2);
Seth Jenningsf5536462012-07-18 11:55:56 -0500669}
670
671#else /* USE_PGTABLE_MAPPING */
672
673static inline int __zs_cpu_up(struct mapping_area *area)
674{
675 /*
676 * Make sure we don't leak memory if a cpu UP notification
677 * and zs_init() race and both call zs_cpu_up() on the same cpu
678 */
679 if (area->vm_buf)
680 return 0;
681 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
682 if (!area->vm_buf)
683 return -ENOMEM;
684 return 0;
685}
686
687static inline void __zs_cpu_down(struct mapping_area *area)
688{
689 if (area->vm_buf)
690 free_page((unsigned long)area->vm_buf);
691 area->vm_buf = NULL;
692}
693
694static void *__zs_map_object(struct mapping_area *area,
695 struct page *pages[2], int off, int size)
696{
Seth Jennings5f601902012-07-02 16:15:49 -0500697 int sizes[2];
698 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500699 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500700
Seth Jenningsf5536462012-07-18 11:55:56 -0500701 /* disable page faults to match kmap_atomic() return conditions */
702 pagefault_disable();
703
704 /* no read fastpath */
705 if (area->vm_mm == ZS_MM_WO)
706 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -0500707
708 sizes[0] = PAGE_SIZE - off;
709 sizes[1] = size - sizes[0];
710
Seth Jennings5f601902012-07-02 16:15:49 -0500711 /* copy object to per-cpu buffer */
712 addr = kmap_atomic(pages[0]);
713 memcpy(buf, addr + off, sizes[0]);
714 kunmap_atomic(addr);
715 addr = kmap_atomic(pages[1]);
716 memcpy(buf + sizes[0], addr, sizes[1]);
717 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500718out:
719 return area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500720}
721
Seth Jenningsf5536462012-07-18 11:55:56 -0500722static void __zs_unmap_object(struct mapping_area *area,
723 struct page *pages[2], int off, int size)
Seth Jennings5f601902012-07-02 16:15:49 -0500724{
Seth Jennings5f601902012-07-02 16:15:49 -0500725 int sizes[2];
726 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -0500727 char *buf = area->vm_buf;
Seth Jennings5f601902012-07-02 16:15:49 -0500728
Seth Jenningsf5536462012-07-18 11:55:56 -0500729 /* no write fastpath */
730 if (area->vm_mm == ZS_MM_RO)
731 goto out;
Seth Jennings5f601902012-07-02 16:15:49 -0500732
733 sizes[0] = PAGE_SIZE - off;
734 sizes[1] = size - sizes[0];
735
736 /* copy per-cpu buffer to object */
737 addr = kmap_atomic(pages[0]);
738 memcpy(addr + off, buf, sizes[0]);
739 kunmap_atomic(addr);
740 addr = kmap_atomic(pages[1]);
741 memcpy(addr, buf + sizes[0], sizes[1]);
742 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -0500743
744out:
745 /* enable page faults to match kunmap_atomic() return conditions */
746 pagefault_enable();
Seth Jennings5f601902012-07-02 16:15:49 -0500747}
Nitin Gupta61989a82012-01-09 16:51:56 -0600748
Seth Jenningsf5536462012-07-18 11:55:56 -0500749#endif /* USE_PGTABLE_MAPPING */
750
Nitin Gupta61989a82012-01-09 16:51:56 -0600751static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
752 void *pcpu)
753{
Seth Jenningsf5536462012-07-18 11:55:56 -0500754 int ret, cpu = (long)pcpu;
Nitin Gupta61989a82012-01-09 16:51:56 -0600755 struct mapping_area *area;
756
757 switch (action) {
758 case CPU_UP_PREPARE:
759 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500760 ret = __zs_cpu_up(area);
761 if (ret)
762 return notifier_from_errno(ret);
Nitin Gupta61989a82012-01-09 16:51:56 -0600763 break;
764 case CPU_DEAD:
765 case CPU_UP_CANCELED:
766 area = &per_cpu(zs_map_area, cpu);
Seth Jenningsf5536462012-07-18 11:55:56 -0500767 __zs_cpu_down(area);
Nitin Gupta61989a82012-01-09 16:51:56 -0600768 break;
769 }
770
771 return NOTIFY_OK;
772}
773
774static struct notifier_block zs_cpu_nb = {
775 .notifier_call = zs_cpu_notifier
776};
777
778static void zs_exit(void)
779{
780 int cpu;
781
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530782 cpu_notifier_register_begin();
783
Nitin Gupta61989a82012-01-09 16:51:56 -0600784 for_each_online_cpu(cpu)
785 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530786 __unregister_cpu_notifier(&zs_cpu_nb);
787
788 cpu_notifier_register_done();
Nitin Gupta61989a82012-01-09 16:51:56 -0600789}
790
791static int zs_init(void)
792{
793 int cpu, ret;
794
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530795 cpu_notifier_register_begin();
796
797 __register_cpu_notifier(&zs_cpu_nb);
Nitin Gupta61989a82012-01-09 16:51:56 -0600798 for_each_online_cpu(cpu) {
799 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530800 if (notifier_to_errno(ret)) {
801 cpu_notifier_register_done();
Nitin Gupta61989a82012-01-09 16:51:56 -0600802 goto fail;
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530803 }
Nitin Gupta61989a82012-01-09 16:51:56 -0600804 }
Srivatsa S. Bhatcda631a2014-03-11 02:09:59 +0530805
806 cpu_notifier_register_done();
807
Nitin Gupta61989a82012-01-09 16:51:56 -0600808 return 0;
809fail:
810 zs_exit();
811 return notifier_to_errno(ret);
812}
813
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -0800814/**
815 * zs_create_pool - Creates an allocation pool to work from.
Seth Jennings0d145a52013-01-30 09:36:52 -0600816 * @flags: allocation flags used to allocate pool metadata
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -0800817 *
818 * This function must be called before anything when using
819 * the zsmalloc allocator.
820 *
821 * On success, a pointer to the newly created pool is returned,
822 * otherwise NULL.
823 */
Seth Jennings0d145a52013-01-30 09:36:52 -0600824struct zs_pool *zs_create_pool(gfp_t flags)
Nitin Gupta61989a82012-01-09 16:51:56 -0600825{
Ben Hutchings069f1012012-06-20 02:31:11 +0100826 int i, ovhd_size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600827 struct zs_pool *pool;
828
Nitin Gupta61989a82012-01-09 16:51:56 -0600829 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
830 pool = kzalloc(ovhd_size, GFP_KERNEL);
831 if (!pool)
832 return NULL;
833
834 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
835 int size;
836 struct size_class *class;
837
838 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
839 if (size > ZS_MAX_ALLOC_SIZE)
840 size = ZS_MAX_ALLOC_SIZE;
841
842 class = &pool->size_class[i];
843 class->size = size;
844 class->index = i;
845 spin_lock_init(&class->lock);
Minchan Kim2e3b61542012-05-03 15:40:39 +0900846 class->pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -0600847
848 }
849
Nitin Gupta61989a82012-01-09 16:51:56 -0600850 pool->flags = flags;
Nitin Gupta61989a82012-01-09 16:51:56 -0600851
Nitin Gupta61989a82012-01-09 16:51:56 -0600852 return pool;
853}
854EXPORT_SYMBOL_GPL(zs_create_pool);
855
856void zs_destroy_pool(struct zs_pool *pool)
857{
858 int i;
859
860 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
861 int fg;
862 struct size_class *class = &pool->size_class[i];
863
864 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
865 if (class->fullness_list[fg]) {
866 pr_info("Freeing non-empty class with size "
867 "%db, fullness group %d\n",
868 class->size, fg);
869 }
870 }
871 }
872 kfree(pool);
873}
874EXPORT_SYMBOL_GPL(zs_destroy_pool);
875
876/**
877 * zs_malloc - Allocate block of given size from pool.
878 * @pool: pool to allocate from
879 * @size: size of block to allocate
Nitin Gupta61989a82012-01-09 16:51:56 -0600880 *
Minchan Kim00a61d82012-05-03 15:40:40 +0900881 * On success, handle to the allocated object is returned,
Minchan Kimc2344342012-06-08 15:39:25 +0900882 * otherwise 0.
Nitin Gupta61989a82012-01-09 16:51:56 -0600883 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
884 */
Minchan Kimc2344342012-06-08 15:39:25 +0900885unsigned long zs_malloc(struct zs_pool *pool, size_t size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600886{
Minchan Kimc2344342012-06-08 15:39:25 +0900887 unsigned long obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600888 struct link_free *link;
889 int class_idx;
890 struct size_class *class;
891
892 struct page *first_page, *m_page;
893 unsigned long m_objidx, m_offset;
894
895 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Minchan Kimc2344342012-06-08 15:39:25 +0900896 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600897
898 class_idx = get_size_class_index(size);
899 class = &pool->size_class[class_idx];
900 BUG_ON(class_idx != class->index);
901
902 spin_lock(&class->lock);
903 first_page = find_get_zspage(class);
904
905 if (!first_page) {
906 spin_unlock(&class->lock);
907 first_page = alloc_zspage(class, pool->flags);
908 if (unlikely(!first_page))
Minchan Kimc2344342012-06-08 15:39:25 +0900909 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600910
911 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
912 spin_lock(&class->lock);
Minchan Kim2e3b61542012-05-03 15:40:39 +0900913 class->pages_allocated += class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600914 }
915
Minchan Kimc2344342012-06-08 15:39:25 +0900916 obj = (unsigned long)first_page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600917 obj_handle_to_location(obj, &m_page, &m_objidx);
918 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
919
920 link = (struct link_free *)kmap_atomic(m_page) +
921 m_offset / sizeof(*link);
922 first_page->freelist = link->next;
923 memset(link, POISON_INUSE, sizeof(*link));
924 kunmap_atomic(link);
925
926 first_page->inuse++;
927 /* Now move the zspage to another fullness group, if required */
928 fix_fullness_group(pool, first_page);
929 spin_unlock(&class->lock);
930
931 return obj;
932}
933EXPORT_SYMBOL_GPL(zs_malloc);
934
Minchan Kimc2344342012-06-08 15:39:25 +0900935void zs_free(struct zs_pool *pool, unsigned long obj)
Nitin Gupta61989a82012-01-09 16:51:56 -0600936{
937 struct link_free *link;
938 struct page *first_page, *f_page;
939 unsigned long f_objidx, f_offset;
940
941 int class_idx;
942 struct size_class *class;
943 enum fullness_group fullness;
944
945 if (unlikely(!obj))
946 return;
947
948 obj_handle_to_location(obj, &f_page, &f_objidx);
949 first_page = get_first_page(f_page);
950
951 get_zspage_mapping(first_page, &class_idx, &fullness);
952 class = &pool->size_class[class_idx];
953 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
954
955 spin_lock(&class->lock);
956
957 /* Insert this object in containing zspage's freelist */
958 link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
959 + f_offset);
960 link->next = first_page->freelist;
961 kunmap_atomic(link);
Minchan Kimc2344342012-06-08 15:39:25 +0900962 first_page->freelist = (void *)obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600963
964 first_page->inuse--;
965 fullness = fix_fullness_group(pool, first_page);
966
967 if (fullness == ZS_EMPTY)
Minchan Kim2e3b61542012-05-03 15:40:39 +0900968 class->pages_allocated -= class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600969
970 spin_unlock(&class->lock);
971
972 if (fullness == ZS_EMPTY)
973 free_zspage(first_page);
974}
975EXPORT_SYMBOL_GPL(zs_free);
976
Minchan Kim00a61d82012-05-03 15:40:40 +0900977/**
978 * zs_map_object - get address of allocated object from handle.
979 * @pool: pool from which the object was allocated
980 * @handle: handle returned from zs_malloc
981 *
982 * Before using an object allocated from zs_malloc, it must be mapped using
983 * this function. When done with the object, it must be unmapped using
Seth Jennings166cfda2012-07-02 16:15:51 -0500984 * zs_unmap_object.
985 *
986 * Only one object can be mapped per cpu at a time. There is no protection
987 * against nested mappings.
988 *
989 * This function returns with preemption and page faults disabled.
Minchan Kim00a61d82012-05-03 15:40:40 +0900990*/
Seth Jenningsb7418512012-07-02 16:15:52 -0500991void *zs_map_object(struct zs_pool *pool, unsigned long handle,
992 enum zs_mapmode mm)
Nitin Gupta61989a82012-01-09 16:51:56 -0600993{
994 struct page *page;
995 unsigned long obj_idx, off;
996
997 unsigned int class_idx;
998 enum fullness_group fg;
999 struct size_class *class;
1000 struct mapping_area *area;
Seth Jenningsf5536462012-07-18 11:55:56 -05001001 struct page *pages[2];
Nitin Gupta61989a82012-01-09 16:51:56 -06001002
1003 BUG_ON(!handle);
1004
Seth Jenningsc60369f2012-07-18 11:55:55 -05001005 /*
1006 * Because we use per-cpu mapping areas shared among the
1007 * pools/users, we can't allow mapping in interrupt context
1008 * because it can corrupt another users mappings.
1009 */
1010 BUG_ON(in_interrupt());
1011
Nitin Gupta61989a82012-01-09 16:51:56 -06001012 obj_handle_to_location(handle, &page, &obj_idx);
1013 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1014 class = &pool->size_class[class_idx];
1015 off = obj_idx_to_offset(page, obj_idx, class->size);
1016
1017 area = &get_cpu_var(zs_map_area);
Seth Jenningsf5536462012-07-18 11:55:56 -05001018 area->vm_mm = mm;
Nitin Gupta61989a82012-01-09 16:51:56 -06001019 if (off + class->size <= PAGE_SIZE) {
1020 /* this object is contained entirely within a page */
1021 area->vm_addr = kmap_atomic(page);
Seth Jennings5f601902012-07-02 16:15:49 -05001022 return area->vm_addr + off;
Nitin Gupta61989a82012-01-09 16:51:56 -06001023 }
1024
Seth Jenningsf5536462012-07-18 11:55:56 -05001025 /* this object spans two pages */
1026 pages[0] = page;
1027 pages[1] = get_next_page(page);
1028 BUG_ON(!pages[1]);
Seth Jenningsb7418512012-07-02 16:15:52 -05001029
Seth Jenningsf5536462012-07-18 11:55:56 -05001030 return __zs_map_object(area, pages, off, class->size);
Nitin Gupta61989a82012-01-09 16:51:56 -06001031}
1032EXPORT_SYMBOL_GPL(zs_map_object);
1033
Minchan Kimc2344342012-06-08 15:39:25 +09001034void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
Nitin Gupta61989a82012-01-09 16:51:56 -06001035{
1036 struct page *page;
1037 unsigned long obj_idx, off;
1038
1039 unsigned int class_idx;
1040 enum fullness_group fg;
1041 struct size_class *class;
1042 struct mapping_area *area;
1043
1044 BUG_ON(!handle);
1045
1046 obj_handle_to_location(handle, &page, &obj_idx);
1047 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
1048 class = &pool->size_class[class_idx];
1049 off = obj_idx_to_offset(page, obj_idx, class->size);
1050
Seth Jenningsf5536462012-07-18 11:55:56 -05001051 area = &__get_cpu_var(zs_map_area);
1052 if (off + class->size <= PAGE_SIZE)
1053 kunmap_atomic(area->vm_addr);
1054 else {
1055 struct page *pages[2];
Seth Jenningsb7418512012-07-02 16:15:52 -05001056
Seth Jenningsf5536462012-07-18 11:55:56 -05001057 pages[0] = page;
1058 pages[1] = get_next_page(page);
1059 BUG_ON(!pages[1]);
1060
1061 __zs_unmap_object(area, pages, off, class->size);
1062 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001063 put_cpu_var(zs_map_area);
1064}
1065EXPORT_SYMBOL_GPL(zs_unmap_object);
1066
1067u64 zs_get_total_size_bytes(struct zs_pool *pool)
1068{
1069 int i;
1070 u64 npages = 0;
1071
1072 for (i = 0; i < ZS_SIZE_CLASSES; i++)
1073 npages += pool->size_class[i].pages_allocated;
1074
1075 return npages << PAGE_SHIFT;
1076}
1077EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
Ben Hutchings069f1012012-06-20 02:31:11 +01001078
1079module_init(zs_init);
1080module_exit(zs_exit);
1081
1082MODULE_LICENSE("Dual BSD/GPL");
1083MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");