blob: a7a6f225bbffbb5d8700e33c50cdf23b150b192e [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
5 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the license that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 */
12
Nitin Gupta2db51da2012-06-09 17:41:14 -070013
14/*
15 * This allocator is designed for use with zcache and zram. Thus, the
16 * allocator is supposed to work well under low memory conditions. In
17 * particular, it never attempts higher order page allocation which is
18 * very likely to fail under memory pressure. On the other hand, if we
19 * just use single (0-order) pages, it would suffer from very high
20 * fragmentation -- any object of size PAGE_SIZE/2 or larger would occupy
21 * an entire page. This was one of the major issues with its predecessor
22 * (xvmalloc).
23 *
24 * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
25 * and links them together using various 'struct page' fields. These linked
26 * pages act as a single higher-order page i.e. an object can span 0-order
27 * page boundaries. The code refers to these linked pages as a single entity
28 * called zspage.
29 *
30 * Following is how we use various fields and flags of underlying
31 * struct page(s) to form a zspage.
32 *
33 * Usage of struct page fields:
34 * page->first_page: points to the first component (0-order) page
35 * page->index (union with page->freelist): offset of the first object
36 * starting in this page. For the first page, this is
37 * always 0, so we use this field (aka freelist) to point
38 * to the first free object in zspage.
39 * page->lru: links together all component pages (except the first page)
40 * of a zspage
41 *
42 * For _first_ page only:
43 *
44 * page->private (union with page->first_page): refers to the
45 * component page after the first page
46 * page->freelist: points to the first free object in zspage.
47 * Free objects are linked together using in-place
48 * metadata.
49 * page->objects: maximum number of objects we can store in this
50 * zspage (class->zspage_order * PAGE_SIZE / class->size)
51 * page->lru: links together first pages of various zspages.
52 * Basically forming list of zspages in a fullness group.
53 * page->mapping: class index and fullness group of the zspage
54 *
55 * Usage of struct page flags:
56 * PG_private: identifies the first component page
57 * PG_private2: identifies the last component page
58 *
59 */
60
Nitin Gupta61989a82012-01-09 16:51:56 -060061#ifdef CONFIG_ZSMALLOC_DEBUG
62#define DEBUG
63#endif
64
65#include <linux/module.h>
66#include <linux/kernel.h>
67#include <linux/bitops.h>
68#include <linux/errno.h>
69#include <linux/highmem.h>
70#include <linux/init.h>
71#include <linux/string.h>
72#include <linux/slab.h>
73#include <asm/tlbflush.h>
74#include <asm/pgtable.h>
75#include <linux/cpumask.h>
76#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060077#include <linux/vmalloc.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060078
79#include "zsmalloc.h"
80#include "zsmalloc_int.h"
81
82/*
83 * A zspage's class index and fullness group
84 * are encoded in its (first)page->mapping
85 */
86#define CLASS_IDX_BITS 28
87#define FULLNESS_BITS 4
88#define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
89#define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
90
Nitin Gupta61989a82012-01-09 16:51:56 -060091/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
92static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
93
94static int is_first_page(struct page *page)
95{
Minchan Kima27545bf2012-04-25 15:23:09 +090096 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -060097}
98
99static int is_last_page(struct page *page)
100{
Minchan Kima27545bf2012-04-25 15:23:09 +0900101 return PagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600102}
103
104static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
105 enum fullness_group *fullness)
106{
107 unsigned long m;
108 BUG_ON(!is_first_page(page));
109
110 m = (unsigned long)page->mapping;
111 *fullness = m & FULLNESS_MASK;
112 *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
113}
114
115static void set_zspage_mapping(struct page *page, unsigned int class_idx,
116 enum fullness_group fullness)
117{
118 unsigned long m;
119 BUG_ON(!is_first_page(page));
120
121 m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
122 (fullness & FULLNESS_MASK);
123 page->mapping = (struct address_space *)m;
124}
125
126static int get_size_class_index(int size)
127{
128 int idx = 0;
129
130 if (likely(size > ZS_MIN_ALLOC_SIZE))
131 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
132 ZS_SIZE_CLASS_DELTA);
133
134 return idx;
135}
136
137static enum fullness_group get_fullness_group(struct page *page)
138{
139 int inuse, max_objects;
140 enum fullness_group fg;
141 BUG_ON(!is_first_page(page));
142
143 inuse = page->inuse;
144 max_objects = page->objects;
145
146 if (inuse == 0)
147 fg = ZS_EMPTY;
148 else if (inuse == max_objects)
149 fg = ZS_FULL;
150 else if (inuse <= max_objects / fullness_threshold_frac)
151 fg = ZS_ALMOST_EMPTY;
152 else
153 fg = ZS_ALMOST_FULL;
154
155 return fg;
156}
157
158static void insert_zspage(struct page *page, struct size_class *class,
159 enum fullness_group fullness)
160{
161 struct page **head;
162
163 BUG_ON(!is_first_page(page));
164
165 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
166 return;
167
168 head = &class->fullness_list[fullness];
169 if (*head)
170 list_add_tail(&page->lru, &(*head)->lru);
171
172 *head = page;
173}
174
175static void remove_zspage(struct page *page, struct size_class *class,
176 enum fullness_group fullness)
177{
178 struct page **head;
179
180 BUG_ON(!is_first_page(page));
181
182 if (fullness >= _ZS_NR_FULLNESS_GROUPS)
183 return;
184
185 head = &class->fullness_list[fullness];
186 BUG_ON(!*head);
187 if (list_empty(&(*head)->lru))
188 *head = NULL;
189 else if (*head == page)
190 *head = (struct page *)list_entry((*head)->lru.next,
191 struct page, lru);
192
193 list_del_init(&page->lru);
194}
195
196static enum fullness_group fix_fullness_group(struct zs_pool *pool,
197 struct page *page)
198{
199 int class_idx;
200 struct size_class *class;
201 enum fullness_group currfg, newfg;
202
203 BUG_ON(!is_first_page(page));
204
205 get_zspage_mapping(page, &class_idx, &currfg);
206 newfg = get_fullness_group(page);
207 if (newfg == currfg)
208 goto out;
209
210 class = &pool->size_class[class_idx];
211 remove_zspage(page, class, currfg);
212 insert_zspage(page, class, newfg);
213 set_zspage_mapping(page, class_idx, newfg);
214
215out:
216 return newfg;
217}
218
219/*
220 * We have to decide on how many pages to link together
221 * to form a zspage for each size class. This is important
222 * to reduce wastage due to unusable space left at end of
223 * each zspage which is given as:
224 * wastage = Zp - Zp % size_class
225 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
226 *
227 * For example, for size class of 3/8 * PAGE_SIZE, we should
228 * link together 3 PAGE_SIZE sized pages to form a zspage
229 * since then we can perfectly fit in 8 such objects.
230 */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900231static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600232{
233 int i, max_usedpc = 0;
234 /* zspage order which gives maximum used size per KB */
235 int max_usedpc_order = 1;
236
Seth Jennings84d4faa2012-03-05 11:33:21 -0600237 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600238 int zspage_size;
239 int waste, usedpc;
240
241 zspage_size = i * PAGE_SIZE;
242 waste = zspage_size % class_size;
243 usedpc = (zspage_size - waste) * 100 / zspage_size;
244
245 if (usedpc > max_usedpc) {
246 max_usedpc = usedpc;
247 max_usedpc_order = i;
248 }
249 }
250
251 return max_usedpc_order;
252}
253
254/*
255 * A single 'zspage' is composed of many system pages which are
256 * linked together using fields in struct page. This function finds
257 * the first/head page, given any component page of a zspage.
258 */
259static struct page *get_first_page(struct page *page)
260{
261 if (is_first_page(page))
262 return page;
263 else
264 return page->first_page;
265}
266
267static struct page *get_next_page(struct page *page)
268{
269 struct page *next;
270
271 if (is_last_page(page))
272 next = NULL;
273 else if (is_first_page(page))
274 next = (struct page *)page->private;
275 else
276 next = list_entry(page->lru.next, struct page, lru);
277
278 return next;
279}
280
281/* Encode <page, obj_idx> as a single handle value */
282static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
283{
284 unsigned long handle;
285
286 if (!page) {
287 BUG_ON(obj_idx);
288 return NULL;
289 }
290
291 handle = page_to_pfn(page) << OBJ_INDEX_BITS;
292 handle |= (obj_idx & OBJ_INDEX_MASK);
293
294 return (void *)handle;
295}
296
297/* Decode <page, obj_idx> pair from the given object handle */
Minchan Kimc2344342012-06-08 15:39:25 +0900298static void obj_handle_to_location(unsigned long handle, struct page **page,
Nitin Gupta61989a82012-01-09 16:51:56 -0600299 unsigned long *obj_idx)
300{
Minchan Kimc2344342012-06-08 15:39:25 +0900301 *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
302 *obj_idx = handle & OBJ_INDEX_MASK;
Nitin Gupta61989a82012-01-09 16:51:56 -0600303}
304
305static unsigned long obj_idx_to_offset(struct page *page,
306 unsigned long obj_idx, int class_size)
307{
308 unsigned long off = 0;
309
310 if (!is_first_page(page))
311 off = page->index;
312
313 return off + obj_idx * class_size;
314}
315
Nitin Guptaf4477e92012-04-02 09:13:56 -0500316static void reset_page(struct page *page)
317{
318 clear_bit(PG_private, &page->flags);
319 clear_bit(PG_private_2, &page->flags);
320 set_page_private(page, 0);
321 page->mapping = NULL;
322 page->freelist = NULL;
323 reset_page_mapcount(page);
324}
325
Nitin Gupta61989a82012-01-09 16:51:56 -0600326static void free_zspage(struct page *first_page)
327{
Nitin Guptaf4477e92012-04-02 09:13:56 -0500328 struct page *nextp, *tmp, *head_extra;
Nitin Gupta61989a82012-01-09 16:51:56 -0600329
330 BUG_ON(!is_first_page(first_page));
331 BUG_ON(first_page->inuse);
332
Nitin Guptaf4477e92012-04-02 09:13:56 -0500333 head_extra = (struct page *)page_private(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600334
Nitin Guptaf4477e92012-04-02 09:13:56 -0500335 reset_page(first_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600336 __free_page(first_page);
337
338 /* zspage with only 1 system page */
Nitin Guptaf4477e92012-04-02 09:13:56 -0500339 if (!head_extra)
Nitin Gupta61989a82012-01-09 16:51:56 -0600340 return;
341
Nitin Guptaf4477e92012-04-02 09:13:56 -0500342 list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600343 list_del(&nextp->lru);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500344 reset_page(nextp);
Nitin Gupta61989a82012-01-09 16:51:56 -0600345 __free_page(nextp);
346 }
Nitin Guptaf4477e92012-04-02 09:13:56 -0500347 reset_page(head_extra);
348 __free_page(head_extra);
Nitin Gupta61989a82012-01-09 16:51:56 -0600349}
350
351/* Initialize a newly allocated zspage */
352static void init_zspage(struct page *first_page, struct size_class *class)
353{
354 unsigned long off = 0;
355 struct page *page = first_page;
356
357 BUG_ON(!is_first_page(first_page));
358 while (page) {
359 struct page *next_page;
360 struct link_free *link;
361 unsigned int i, objs_on_page;
362
363 /*
364 * page->index stores offset of first object starting
365 * in the page. For the first page, this is always 0,
366 * so we use first_page->index (aka ->freelist) to store
367 * head of corresponding zspage's freelist.
368 */
369 if (page != first_page)
370 page->index = off;
371
372 link = (struct link_free *)kmap_atomic(page) +
373 off / sizeof(*link);
374 objs_on_page = (PAGE_SIZE - off) / class->size;
375
376 for (i = 1; i <= objs_on_page; i++) {
377 off += class->size;
378 if (off < PAGE_SIZE) {
379 link->next = obj_location_to_handle(page, i);
380 link += class->size / sizeof(*link);
381 }
382 }
383
384 /*
385 * We now come to the last (full or partial) object on this
386 * page, which must point to the first object on the next
387 * page (if present)
388 */
389 next_page = get_next_page(page);
390 link->next = obj_location_to_handle(next_page, 0);
391 kunmap_atomic(link);
392 page = next_page;
393 off = (off + class->size) % PAGE_SIZE;
394 }
395}
396
397/*
398 * Allocate a zspage for the given size class
399 */
400static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
401{
402 int i, error;
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500403 struct page *first_page = NULL, *uninitialized_var(prev_page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600404
405 /*
406 * Allocate individual pages and link them together as:
407 * 1. first page->private = first sub-page
408 * 2. all sub-pages are linked together using page->lru
409 * 3. each sub-page is linked to the first page using page->first_page
410 *
411 * For each size class, First/Head pages are linked together using
412 * page->lru. Also, we set PG_private to identify the first page
413 * (i.e. no other sub-page has this flag set) and PG_private_2 to
414 * identify the last page.
415 */
416 error = -ENOMEM;
Minchan Kim2e3b6152012-05-03 15:40:39 +0900417 for (i = 0; i < class->pages_per_zspage; i++) {
Seth Jenningsb4b700c2012-06-13 16:03:42 -0500418 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -0600419
420 page = alloc_page(flags);
421 if (!page)
422 goto cleanup;
423
424 INIT_LIST_HEAD(&page->lru);
425 if (i == 0) { /* first page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900426 SetPagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600427 set_page_private(page, 0);
428 first_page = page;
429 first_page->inuse = 0;
430 }
431 if (i == 1)
432 first_page->private = (unsigned long)page;
433 if (i >= 1)
434 page->first_page = first_page;
435 if (i >= 2)
436 list_add(&page->lru, &prev_page->lru);
Minchan Kim2e3b6152012-05-03 15:40:39 +0900437 if (i == class->pages_per_zspage - 1) /* last page */
Minchan Kima27545bf2012-04-25 15:23:09 +0900438 SetPagePrivate2(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600439 prev_page = page;
440 }
441
442 init_zspage(first_page, class);
443
444 first_page->freelist = obj_location_to_handle(first_page, 0);
445 /* Maximum number of objects we can store in this zspage */
Minchan Kim2e3b6152012-05-03 15:40:39 +0900446 first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600447
448 error = 0; /* Success */
449
450cleanup:
451 if (unlikely(error) && first_page) {
452 free_zspage(first_page);
453 first_page = NULL;
454 }
455
456 return first_page;
457}
458
459static struct page *find_get_zspage(struct size_class *class)
460{
461 int i;
462 struct page *page;
463
464 for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
465 page = class->fullness_list[i];
466 if (page)
467 break;
468 }
469
470 return page;
471}
472
Seth Jennings5f601902012-07-02 16:15:49 -0500473static void zs_copy_map_object(char *buf, struct page *firstpage,
474 int off, int size)
475{
476 struct page *pages[2];
477 int sizes[2];
478 void *addr;
479
480 pages[0] = firstpage;
481 pages[1] = get_next_page(firstpage);
482 BUG_ON(!pages[1]);
483
484 sizes[0] = PAGE_SIZE - off;
485 sizes[1] = size - sizes[0];
486
487 /* disable page faults to match kmap_atomic() return conditions */
488 pagefault_disable();
489
490 /* copy object to per-cpu buffer */
491 addr = kmap_atomic(pages[0]);
492 memcpy(buf, addr + off, sizes[0]);
493 kunmap_atomic(addr);
494 addr = kmap_atomic(pages[1]);
495 memcpy(buf + sizes[0], addr, sizes[1]);
496 kunmap_atomic(addr);
497}
498
499static void zs_copy_unmap_object(char *buf, struct page *firstpage,
500 int off, int size)
501{
502 struct page *pages[2];
503 int sizes[2];
504 void *addr;
505
506 pages[0] = firstpage;
507 pages[1] = get_next_page(firstpage);
508 BUG_ON(!pages[1]);
509
510 sizes[0] = PAGE_SIZE - off;
511 sizes[1] = size - sizes[0];
512
513 /* copy per-cpu buffer to object */
514 addr = kmap_atomic(pages[0]);
515 memcpy(addr + off, buf, sizes[0]);
516 kunmap_atomic(addr);
517 addr = kmap_atomic(pages[1]);
518 memcpy(addr, buf + sizes[0], sizes[1]);
519 kunmap_atomic(addr);
520
521 /* enable page faults to match kunmap_atomic() return conditions */
522 pagefault_enable();
523}
Nitin Gupta61989a82012-01-09 16:51:56 -0600524
Nitin Gupta61989a82012-01-09 16:51:56 -0600525static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
526 void *pcpu)
527{
528 int cpu = (long)pcpu;
529 struct mapping_area *area;
530
531 switch (action) {
532 case CPU_UP_PREPARE:
533 area = &per_cpu(zs_map_area, cpu);
Seth Jennings5f601902012-07-02 16:15:49 -0500534 /*
535 * Make sure we don't leak memory if a cpu UP notification
536 * and zs_init() race and both call zs_cpu_up() on the same cpu
537 */
538 if (area->vm_buf)
539 return 0;
540 area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
541 if (!area->vm_buf)
542 return -ENOMEM;
543 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600544 break;
545 case CPU_DEAD:
546 case CPU_UP_CANCELED:
547 area = &per_cpu(zs_map_area, cpu);
Seth Jennings5f601902012-07-02 16:15:49 -0500548 if (area->vm_buf)
549 free_page((unsigned long)area->vm_buf);
550 area->vm_buf = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -0600551 break;
552 }
553
554 return NOTIFY_OK;
555}
556
557static struct notifier_block zs_cpu_nb = {
558 .notifier_call = zs_cpu_notifier
559};
560
561static void zs_exit(void)
562{
563 int cpu;
564
565 for_each_online_cpu(cpu)
566 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
567 unregister_cpu_notifier(&zs_cpu_nb);
568}
569
570static int zs_init(void)
571{
572 int cpu, ret;
573
574 register_cpu_notifier(&zs_cpu_nb);
575 for_each_online_cpu(cpu) {
576 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
577 if (notifier_to_errno(ret))
578 goto fail;
579 }
580 return 0;
581fail:
582 zs_exit();
583 return notifier_to_errno(ret);
584}
585
586struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
587{
Ben Hutchings069f1012012-06-20 02:31:11 +0100588 int i, ovhd_size;
Nitin Gupta61989a82012-01-09 16:51:56 -0600589 struct zs_pool *pool;
590
591 if (!name)
592 return NULL;
593
594 ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
595 pool = kzalloc(ovhd_size, GFP_KERNEL);
596 if (!pool)
597 return NULL;
598
599 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
600 int size;
601 struct size_class *class;
602
603 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
604 if (size > ZS_MAX_ALLOC_SIZE)
605 size = ZS_MAX_ALLOC_SIZE;
606
607 class = &pool->size_class[i];
608 class->size = size;
609 class->index = i;
610 spin_lock_init(&class->lock);
Minchan Kim2e3b6152012-05-03 15:40:39 +0900611 class->pages_per_zspage = get_pages_per_zspage(size);
Nitin Gupta61989a82012-01-09 16:51:56 -0600612
613 }
614
Nitin Gupta61989a82012-01-09 16:51:56 -0600615 pool->flags = flags;
616 pool->name = name;
617
Nitin Gupta61989a82012-01-09 16:51:56 -0600618 return pool;
619}
620EXPORT_SYMBOL_GPL(zs_create_pool);
621
622void zs_destroy_pool(struct zs_pool *pool)
623{
624 int i;
625
626 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
627 int fg;
628 struct size_class *class = &pool->size_class[i];
629
630 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
631 if (class->fullness_list[fg]) {
632 pr_info("Freeing non-empty class with size "
633 "%db, fullness group %d\n",
634 class->size, fg);
635 }
636 }
637 }
638 kfree(pool);
639}
640EXPORT_SYMBOL_GPL(zs_destroy_pool);
641
642/**
643 * zs_malloc - Allocate block of given size from pool.
644 * @pool: pool to allocate from
645 * @size: size of block to allocate
Nitin Gupta61989a82012-01-09 16:51:56 -0600646 *
Minchan Kim00a61d82012-05-03 15:40:40 +0900647 * On success, handle to the allocated object is returned,
Minchan Kimc2344342012-06-08 15:39:25 +0900648 * otherwise 0.
Nitin Gupta61989a82012-01-09 16:51:56 -0600649 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
650 */
Minchan Kimc2344342012-06-08 15:39:25 +0900651unsigned long zs_malloc(struct zs_pool *pool, size_t size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600652{
Minchan Kimc2344342012-06-08 15:39:25 +0900653 unsigned long obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600654 struct link_free *link;
655 int class_idx;
656 struct size_class *class;
657
658 struct page *first_page, *m_page;
659 unsigned long m_objidx, m_offset;
660
661 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Minchan Kimc2344342012-06-08 15:39:25 +0900662 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600663
664 class_idx = get_size_class_index(size);
665 class = &pool->size_class[class_idx];
666 BUG_ON(class_idx != class->index);
667
668 spin_lock(&class->lock);
669 first_page = find_get_zspage(class);
670
671 if (!first_page) {
672 spin_unlock(&class->lock);
673 first_page = alloc_zspage(class, pool->flags);
674 if (unlikely(!first_page))
Minchan Kimc2344342012-06-08 15:39:25 +0900675 return 0;
Nitin Gupta61989a82012-01-09 16:51:56 -0600676
677 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
678 spin_lock(&class->lock);
Minchan Kim2e3b6152012-05-03 15:40:39 +0900679 class->pages_allocated += class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600680 }
681
Minchan Kimc2344342012-06-08 15:39:25 +0900682 obj = (unsigned long)first_page->freelist;
Nitin Gupta61989a82012-01-09 16:51:56 -0600683 obj_handle_to_location(obj, &m_page, &m_objidx);
684 m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
685
686 link = (struct link_free *)kmap_atomic(m_page) +
687 m_offset / sizeof(*link);
688 first_page->freelist = link->next;
689 memset(link, POISON_INUSE, sizeof(*link));
690 kunmap_atomic(link);
691
692 first_page->inuse++;
693 /* Now move the zspage to another fullness group, if required */
694 fix_fullness_group(pool, first_page);
695 spin_unlock(&class->lock);
696
697 return obj;
698}
699EXPORT_SYMBOL_GPL(zs_malloc);
700
Minchan Kimc2344342012-06-08 15:39:25 +0900701void zs_free(struct zs_pool *pool, unsigned long obj)
Nitin Gupta61989a82012-01-09 16:51:56 -0600702{
703 struct link_free *link;
704 struct page *first_page, *f_page;
705 unsigned long f_objidx, f_offset;
706
707 int class_idx;
708 struct size_class *class;
709 enum fullness_group fullness;
710
711 if (unlikely(!obj))
712 return;
713
714 obj_handle_to_location(obj, &f_page, &f_objidx);
715 first_page = get_first_page(f_page);
716
717 get_zspage_mapping(first_page, &class_idx, &fullness);
718 class = &pool->size_class[class_idx];
719 f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
720
721 spin_lock(&class->lock);
722
723 /* Insert this object in containing zspage's freelist */
724 link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
725 + f_offset);
726 link->next = first_page->freelist;
727 kunmap_atomic(link);
Minchan Kimc2344342012-06-08 15:39:25 +0900728 first_page->freelist = (void *)obj;
Nitin Gupta61989a82012-01-09 16:51:56 -0600729
730 first_page->inuse--;
731 fullness = fix_fullness_group(pool, first_page);
732
733 if (fullness == ZS_EMPTY)
Minchan Kim2e3b6152012-05-03 15:40:39 +0900734 class->pages_allocated -= class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600735
736 spin_unlock(&class->lock);
737
738 if (fullness == ZS_EMPTY)
739 free_zspage(first_page);
740}
741EXPORT_SYMBOL_GPL(zs_free);
742
Minchan Kim00a61d82012-05-03 15:40:40 +0900743/**
744 * zs_map_object - get address of allocated object from handle.
745 * @pool: pool from which the object was allocated
746 * @handle: handle returned from zs_malloc
747 *
748 * Before using an object allocated from zs_malloc, it must be mapped using
749 * this function. When done with the object, it must be unmapped using
750 * zs_unmap_object
751*/
Minchan Kimc2344342012-06-08 15:39:25 +0900752void *zs_map_object(struct zs_pool *pool, unsigned long handle)
Nitin Gupta61989a82012-01-09 16:51:56 -0600753{
754 struct page *page;
755 unsigned long obj_idx, off;
756
757 unsigned int class_idx;
758 enum fullness_group fg;
759 struct size_class *class;
760 struct mapping_area *area;
761
762 BUG_ON(!handle);
763
764 obj_handle_to_location(handle, &page, &obj_idx);
765 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
766 class = &pool->size_class[class_idx];
767 off = obj_idx_to_offset(page, obj_idx, class->size);
768
769 area = &get_cpu_var(zs_map_area);
770 if (off + class->size <= PAGE_SIZE) {
771 /* this object is contained entirely within a page */
772 area->vm_addr = kmap_atomic(page);
Seth Jennings5f601902012-07-02 16:15:49 -0500773 return area->vm_addr + off;
Nitin Gupta61989a82012-01-09 16:51:56 -0600774 }
775
Seth Jennings5f601902012-07-02 16:15:49 -0500776 zs_copy_map_object(area->vm_buf, page, off, class->size);
777 return area->vm_buf;
Nitin Gupta61989a82012-01-09 16:51:56 -0600778}
779EXPORT_SYMBOL_GPL(zs_map_object);
780
Minchan Kimc2344342012-06-08 15:39:25 +0900781void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
Nitin Gupta61989a82012-01-09 16:51:56 -0600782{
783 struct page *page;
784 unsigned long obj_idx, off;
785
786 unsigned int class_idx;
787 enum fullness_group fg;
788 struct size_class *class;
789 struct mapping_area *area;
790
791 BUG_ON(!handle);
792
793 obj_handle_to_location(handle, &page, &obj_idx);
794 get_zspage_mapping(get_first_page(page), &class_idx, &fg);
795 class = &pool->size_class[class_idx];
796 off = obj_idx_to_offset(page, obj_idx, class->size);
797
798 area = &__get_cpu_var(zs_map_area);
Seth Jennings5f601902012-07-02 16:15:49 -0500799 if (off + class->size <= PAGE_SIZE)
Nitin Gupta61989a82012-01-09 16:51:56 -0600800 kunmap_atomic(area->vm_addr);
Seth Jennings5f601902012-07-02 16:15:49 -0500801 else
802 zs_copy_unmap_object(area->vm_buf, page, off, class->size);
Nitin Gupta61989a82012-01-09 16:51:56 -0600803 put_cpu_var(zs_map_area);
804}
805EXPORT_SYMBOL_GPL(zs_unmap_object);
806
807u64 zs_get_total_size_bytes(struct zs_pool *pool)
808{
809 int i;
810 u64 npages = 0;
811
812 for (i = 0; i < ZS_SIZE_CLASSES; i++)
813 npages += pool->size_class[i].pages_allocated;
814
815 return npages << PAGE_SHIFT;
816}
817EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
Ben Hutchings069f1012012-06-20 02:31:11 +0100818
819module_init(zs_init);
820module_exit(zs_exit);
821
822MODULE_LICENSE("Dual BSD/GPL");
823MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");