staging: fix powerpc linux-next break on zsmalloc
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / zsmalloc / zsmalloc-main.c
1 /*
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
13 #ifdef CONFIG_ZSMALLOC_DEBUG
14 #define DEBUG
15 #endif
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/highmem.h>
22 #include <linux/init.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <asm/tlbflush.h>
26 #include <asm/pgtable.h>
27 #include <linux/cpumask.h>
28 #include <linux/cpu.h>
29 #include <linux/vmalloc.h>
30
31 #include "zsmalloc.h"
32 #include "zsmalloc_int.h"
33
34 /*
35  * A zspage's class index and fullness group
36  * are encoded in its (first)page->mapping
37  */
38 #define CLASS_IDX_BITS  28
39 #define FULLNESS_BITS   4
40 #define CLASS_IDX_MASK  ((1 << CLASS_IDX_BITS) - 1)
41 #define FULLNESS_MASK   ((1 << FULLNESS_BITS) - 1)
42
43 /*
44  * Object location (<PFN>, <obj_idx>) is encoded as
45  * as single (void *) handle value.
46  *
47  * Note that object index <obj_idx> is relative to system
48  * page <PFN> it is stored in, so for each sub-page belonging
49  * to a zspage, obj_idx starts with 0.
50  */
51 #define _PFN_BITS               (MAX_PHYSMEM_BITS - PAGE_SHIFT)
52 #define OBJ_INDEX_BITS  (BITS_PER_LONG - _PFN_BITS)
53 #define OBJ_INDEX_MASK  ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
54
55 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
56 static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
57
58 static int is_first_page(struct page *page)
59 {
60         return test_bit(PG_private, &page->flags);
61 }
62
63 static int is_last_page(struct page *page)
64 {
65         return test_bit(PG_private_2, &page->flags);
66 }
67
68 static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
69                                 enum fullness_group *fullness)
70 {
71         unsigned long m;
72         BUG_ON(!is_first_page(page));
73
74         m = (unsigned long)page->mapping;
75         *fullness = m & FULLNESS_MASK;
76         *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
77 }
78
79 static void set_zspage_mapping(struct page *page, unsigned int class_idx,
80                                 enum fullness_group fullness)
81 {
82         unsigned long m;
83         BUG_ON(!is_first_page(page));
84
85         m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
86                         (fullness & FULLNESS_MASK);
87         page->mapping = (struct address_space *)m;
88 }
89
90 static int get_size_class_index(int size)
91 {
92         int idx = 0;
93
94         if (likely(size > ZS_MIN_ALLOC_SIZE))
95                 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
96                                 ZS_SIZE_CLASS_DELTA);
97
98         return idx;
99 }
100
101 static enum fullness_group get_fullness_group(struct page *page)
102 {
103         int inuse, max_objects;
104         enum fullness_group fg;
105         BUG_ON(!is_first_page(page));
106
107         inuse = page->inuse;
108         max_objects = page->objects;
109
110         if (inuse == 0)
111                 fg = ZS_EMPTY;
112         else if (inuse == max_objects)
113                 fg = ZS_FULL;
114         else if (inuse <= max_objects / fullness_threshold_frac)
115                 fg = ZS_ALMOST_EMPTY;
116         else
117                 fg = ZS_ALMOST_FULL;
118
119         return fg;
120 }
121
122 static void insert_zspage(struct page *page, struct size_class *class,
123                                 enum fullness_group fullness)
124 {
125         struct page **head;
126
127         BUG_ON(!is_first_page(page));
128
129         if (fullness >= _ZS_NR_FULLNESS_GROUPS)
130                 return;
131
132         head = &class->fullness_list[fullness];
133         if (*head)
134                 list_add_tail(&page->lru, &(*head)->lru);
135
136         *head = page;
137 }
138
139 static void remove_zspage(struct page *page, struct size_class *class,
140                                 enum fullness_group fullness)
141 {
142         struct page **head;
143
144         BUG_ON(!is_first_page(page));
145
146         if (fullness >= _ZS_NR_FULLNESS_GROUPS)
147                 return;
148
149         head = &class->fullness_list[fullness];
150         BUG_ON(!*head);
151         if (list_empty(&(*head)->lru))
152                 *head = NULL;
153         else if (*head == page)
154                 *head = (struct page *)list_entry((*head)->lru.next,
155                                         struct page, lru);
156
157         list_del_init(&page->lru);
158 }
159
160 static enum fullness_group fix_fullness_group(struct zs_pool *pool,
161                                                 struct page *page)
162 {
163         int class_idx;
164         struct size_class *class;
165         enum fullness_group currfg, newfg;
166
167         BUG_ON(!is_first_page(page));
168
169         get_zspage_mapping(page, &class_idx, &currfg);
170         newfg = get_fullness_group(page);
171         if (newfg == currfg)
172                 goto out;
173
174         class = &pool->size_class[class_idx];
175         remove_zspage(page, class, currfg);
176         insert_zspage(page, class, newfg);
177         set_zspage_mapping(page, class_idx, newfg);
178
179 out:
180         return newfg;
181 }
182
183 /*
184  * We have to decide on how many pages to link together
185  * to form a zspage for each size class. This is important
186  * to reduce wastage due to unusable space left at end of
187  * each zspage which is given as:
188  *      wastage = Zp - Zp % size_class
189  * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
190  *
191  * For example, for size class of 3/8 * PAGE_SIZE, we should
192  * link together 3 PAGE_SIZE sized pages to form a zspage
193  * since then we can perfectly fit in 8 such objects.
194  */
195 static int get_zspage_order(int class_size)
196 {
197         int i, max_usedpc = 0;
198         /* zspage order which gives maximum used size per KB */
199         int max_usedpc_order = 1;
200
201         for (i = 1; i <= max_zspage_order; i++) {
202                 int zspage_size;
203                 int waste, usedpc;
204
205                 zspage_size = i * PAGE_SIZE;
206                 waste = zspage_size % class_size;
207                 usedpc = (zspage_size - waste) * 100 / zspage_size;
208
209                 if (usedpc > max_usedpc) {
210                         max_usedpc = usedpc;
211                         max_usedpc_order = i;
212                 }
213         }
214
215         return max_usedpc_order;
216 }
217
218 /*
219  * A single 'zspage' is composed of many system pages which are
220  * linked together using fields in struct page. This function finds
221  * the first/head page, given any component page of a zspage.
222  */
223 static struct page *get_first_page(struct page *page)
224 {
225         if (is_first_page(page))
226                 return page;
227         else
228                 return page->first_page;
229 }
230
231 static struct page *get_next_page(struct page *page)
232 {
233         struct page *next;
234
235         if (is_last_page(page))
236                 next = NULL;
237         else if (is_first_page(page))
238                 next = (struct page *)page->private;
239         else
240                 next = list_entry(page->lru.next, struct page, lru);
241
242         return next;
243 }
244
245 /* Encode <page, obj_idx> as a single handle value */
246 static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
247 {
248         unsigned long handle;
249
250         if (!page) {
251                 BUG_ON(obj_idx);
252                 return NULL;
253         }
254
255         handle = page_to_pfn(page) << OBJ_INDEX_BITS;
256         handle |= (obj_idx & OBJ_INDEX_MASK);
257
258         return (void *)handle;
259 }
260
261 /* Decode <page, obj_idx> pair from the given object handle */
262 static void obj_handle_to_location(void *handle, struct page **page,
263                                 unsigned long *obj_idx)
264 {
265         unsigned long hval = (unsigned long)handle;
266
267         *page = pfn_to_page(hval >> OBJ_INDEX_BITS);
268         *obj_idx = hval & OBJ_INDEX_MASK;
269 }
270
271 static unsigned long obj_idx_to_offset(struct page *page,
272                                 unsigned long obj_idx, int class_size)
273 {
274         unsigned long off = 0;
275
276         if (!is_first_page(page))
277                 off = page->index;
278
279         return off + obj_idx * class_size;
280 }
281
282 static void free_zspage(struct page *first_page)
283 {
284         struct page *nextp, *tmp;
285
286         BUG_ON(!is_first_page(first_page));
287         BUG_ON(first_page->inuse);
288
289         nextp = (struct page *)page_private(first_page);
290
291         clear_bit(PG_private, &first_page->flags);
292         clear_bit(PG_private_2, &first_page->flags);
293         set_page_private(first_page, 0);
294         first_page->mapping = NULL;
295         first_page->freelist = NULL;
296         reset_page_mapcount(first_page);
297         __free_page(first_page);
298
299         /* zspage with only 1 system page */
300         if (!nextp)
301                 return;
302
303         list_for_each_entry_safe(nextp, tmp, &nextp->lru, lru) {
304                 list_del(&nextp->lru);
305                 clear_bit(PG_private_2, &nextp->flags);
306                 nextp->index = 0;
307                 __free_page(nextp);
308         }
309 }
310
311 /* Initialize a newly allocated zspage */
312 static void init_zspage(struct page *first_page, struct size_class *class)
313 {
314         unsigned long off = 0;
315         struct page *page = first_page;
316
317         BUG_ON(!is_first_page(first_page));
318         while (page) {
319                 struct page *next_page;
320                 struct link_free *link;
321                 unsigned int i, objs_on_page;
322
323                 /*
324                  * page->index stores offset of first object starting
325                  * in the page. For the first page, this is always 0,
326                  * so we use first_page->index (aka ->freelist) to store
327                  * head of corresponding zspage's freelist.
328                  */
329                 if (page != first_page)
330                         page->index = off;
331
332                 link = (struct link_free *)kmap_atomic(page) +
333                                                 off / sizeof(*link);
334                 objs_on_page = (PAGE_SIZE - off) / class->size;
335
336                 for (i = 1; i <= objs_on_page; i++) {
337                         off += class->size;
338                         if (off < PAGE_SIZE) {
339                                 link->next = obj_location_to_handle(page, i);
340                                 link += class->size / sizeof(*link);
341                         }
342                 }
343
344                 /*
345                  * We now come to the last (full or partial) object on this
346                  * page, which must point to the first object on the next
347                  * page (if present)
348                  */
349                 next_page = get_next_page(page);
350                 link->next = obj_location_to_handle(next_page, 0);
351                 kunmap_atomic(link);
352                 page = next_page;
353                 off = (off + class->size) % PAGE_SIZE;
354         }
355 }
356
357 /*
358  * Allocate a zspage for the given size class
359  */
360 static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
361 {
362         int i, error;
363         struct page *first_page = NULL;
364
365         /*
366          * Allocate individual pages and link them together as:
367          * 1. first page->private = first sub-page
368          * 2. all sub-pages are linked together using page->lru
369          * 3. each sub-page is linked to the first page using page->first_page
370          *
371          * For each size class, First/Head pages are linked together using
372          * page->lru. Also, we set PG_private to identify the first page
373          * (i.e. no other sub-page has this flag set) and PG_private_2 to
374          * identify the last page.
375          */
376         error = -ENOMEM;
377         for (i = 0; i < class->zspage_order; i++) {
378                 struct page *page, *prev_page;
379
380                 page = alloc_page(flags);
381                 if (!page)
382                         goto cleanup;
383
384                 INIT_LIST_HEAD(&page->lru);
385                 if (i == 0) {   /* first page */
386                         set_bit(PG_private, &page->flags);
387                         set_page_private(page, 0);
388                         first_page = page;
389                         first_page->inuse = 0;
390                 }
391                 if (i == 1)
392                         first_page->private = (unsigned long)page;
393                 if (i >= 1)
394                         page->first_page = first_page;
395                 if (i >= 2)
396                         list_add(&page->lru, &prev_page->lru);
397                 if (i == class->zspage_order - 1)       /* last page */
398                         set_bit(PG_private_2, &page->flags);
399
400                 prev_page = page;
401         }
402
403         init_zspage(first_page, class);
404
405         first_page->freelist = obj_location_to_handle(first_page, 0);
406         /* Maximum number of objects we can store in this zspage */
407         first_page->objects = class->zspage_order * PAGE_SIZE / class->size;
408
409         error = 0; /* Success */
410
411 cleanup:
412         if (unlikely(error) && first_page) {
413                 free_zspage(first_page);
414                 first_page = NULL;
415         }
416
417         return first_page;
418 }
419
420 static struct page *find_get_zspage(struct size_class *class)
421 {
422         int i;
423         struct page *page;
424
425         for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
426                 page = class->fullness_list[i];
427                 if (page)
428                         break;
429         }
430
431         return page;
432 }
433
434
435 /*
436  * If this becomes a separate module, register zs_init() with
437  * module_init(), zs_exit with module_exit(), and remove zs_initialized
438 */
439 static int zs_initialized;
440
441 static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
442                                 void *pcpu)
443 {
444         int cpu = (long)pcpu;
445         struct mapping_area *area;
446
447         switch (action) {
448         case CPU_UP_PREPARE:
449                 area = &per_cpu(zs_map_area, cpu);
450                 if (area->vm)
451                         break;
452                 area->vm = alloc_vm_area(2 * PAGE_SIZE, area->vm_ptes);
453                 if (!area->vm)
454                         return notifier_from_errno(-ENOMEM);
455                 break;
456         case CPU_DEAD:
457         case CPU_UP_CANCELED:
458                 area = &per_cpu(zs_map_area, cpu);
459                 if (area->vm)
460                         free_vm_area(area->vm);
461                 area->vm = NULL;
462                 break;
463         }
464
465         return NOTIFY_OK;
466 }
467
468 static struct notifier_block zs_cpu_nb = {
469         .notifier_call = zs_cpu_notifier
470 };
471
472 static void zs_exit(void)
473 {
474         int cpu;
475
476         for_each_online_cpu(cpu)
477                 zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
478         unregister_cpu_notifier(&zs_cpu_nb);
479 }
480
481 static int zs_init(void)
482 {
483         int cpu, ret;
484
485         register_cpu_notifier(&zs_cpu_nb);
486         for_each_online_cpu(cpu) {
487                 ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
488                 if (notifier_to_errno(ret))
489                         goto fail;
490         }
491         return 0;
492 fail:
493         zs_exit();
494         return notifier_to_errno(ret);
495 }
496
497 struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
498 {
499         int i, error, ovhd_size;
500         struct zs_pool *pool;
501
502         if (!name)
503                 return NULL;
504
505         ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
506         pool = kzalloc(ovhd_size, GFP_KERNEL);
507         if (!pool)
508                 return NULL;
509
510         for (i = 0; i < ZS_SIZE_CLASSES; i++) {
511                 int size;
512                 struct size_class *class;
513
514                 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
515                 if (size > ZS_MAX_ALLOC_SIZE)
516                         size = ZS_MAX_ALLOC_SIZE;
517
518                 class = &pool->size_class[i];
519                 class->size = size;
520                 class->index = i;
521                 spin_lock_init(&class->lock);
522                 class->zspage_order = get_zspage_order(size);
523
524         }
525
526         /*
527          * If this becomes a separate module, register zs_init with
528          * module_init, and remove this block
529         */
530         if (!zs_initialized) {
531                 error = zs_init();
532                 if (error)
533                         goto cleanup;
534                 zs_initialized = 1;
535         }
536
537         pool->flags = flags;
538         pool->name = name;
539
540         error = 0; /* Success */
541
542 cleanup:
543         if (error) {
544                 zs_destroy_pool(pool);
545                 pool = NULL;
546         }
547
548         return pool;
549 }
550 EXPORT_SYMBOL_GPL(zs_create_pool);
551
552 void zs_destroy_pool(struct zs_pool *pool)
553 {
554         int i;
555
556         for (i = 0; i < ZS_SIZE_CLASSES; i++) {
557                 int fg;
558                 struct size_class *class = &pool->size_class[i];
559
560                 for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
561                         if (class->fullness_list[fg]) {
562                                 pr_info("Freeing non-empty class with size "
563                                         "%db, fullness group %d\n",
564                                         class->size, fg);
565                         }
566                 }
567         }
568         kfree(pool);
569 }
570 EXPORT_SYMBOL_GPL(zs_destroy_pool);
571
572 /**
573  * zs_malloc - Allocate block of given size from pool.
574  * @pool: pool to allocate from
575  * @size: size of block to allocate
576  * @page: page no. that holds the object
577  * @offset: location of object within page
578  *
579  * On success, <page, offset> identifies block allocated
580  * and 0 is returned. On failure, <page, offset> is set to
581  * 0 and -ENOMEM is returned.
582  *
583  * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
584  */
585 void *zs_malloc(struct zs_pool *pool, size_t size)
586 {
587         void *obj;
588         struct link_free *link;
589         int class_idx;
590         struct size_class *class;
591
592         struct page *first_page, *m_page;
593         unsigned long m_objidx, m_offset;
594
595         if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
596                 return NULL;
597
598         class_idx = get_size_class_index(size);
599         class = &pool->size_class[class_idx];
600         BUG_ON(class_idx != class->index);
601
602         spin_lock(&class->lock);
603         first_page = find_get_zspage(class);
604
605         if (!first_page) {
606                 spin_unlock(&class->lock);
607                 first_page = alloc_zspage(class, pool->flags);
608                 if (unlikely(!first_page))
609                         return NULL;
610
611                 set_zspage_mapping(first_page, class->index, ZS_EMPTY);
612                 spin_lock(&class->lock);
613                 class->pages_allocated += class->zspage_order;
614         }
615
616         obj = first_page->freelist;
617         obj_handle_to_location(obj, &m_page, &m_objidx);
618         m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
619
620         link = (struct link_free *)kmap_atomic(m_page) +
621                                         m_offset / sizeof(*link);
622         first_page->freelist = link->next;
623         memset(link, POISON_INUSE, sizeof(*link));
624         kunmap_atomic(link);
625
626         first_page->inuse++;
627         /* Now move the zspage to another fullness group, if required */
628         fix_fullness_group(pool, first_page);
629         spin_unlock(&class->lock);
630
631         return obj;
632 }
633 EXPORT_SYMBOL_GPL(zs_malloc);
634
635 void zs_free(struct zs_pool *pool, void *obj)
636 {
637         struct link_free *link;
638         struct page *first_page, *f_page;
639         unsigned long f_objidx, f_offset;
640
641         int class_idx;
642         struct size_class *class;
643         enum fullness_group fullness;
644
645         if (unlikely(!obj))
646                 return;
647
648         obj_handle_to_location(obj, &f_page, &f_objidx);
649         first_page = get_first_page(f_page);
650
651         get_zspage_mapping(first_page, &class_idx, &fullness);
652         class = &pool->size_class[class_idx];
653         f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
654
655         spin_lock(&class->lock);
656
657         /* Insert this object in containing zspage's freelist */
658         link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
659                                                         + f_offset);
660         link->next = first_page->freelist;
661         kunmap_atomic(link);
662         first_page->freelist = obj;
663
664         first_page->inuse--;
665         fullness = fix_fullness_group(pool, first_page);
666
667         if (fullness == ZS_EMPTY)
668                 class->pages_allocated -= class->zspage_order;
669
670         spin_unlock(&class->lock);
671
672         if (fullness == ZS_EMPTY)
673                 free_zspage(first_page);
674 }
675 EXPORT_SYMBOL_GPL(zs_free);
676
677 void *zs_map_object(struct zs_pool *pool, void *handle)
678 {
679         struct page *page;
680         unsigned long obj_idx, off;
681
682         unsigned int class_idx;
683         enum fullness_group fg;
684         struct size_class *class;
685         struct mapping_area *area;
686
687         BUG_ON(!handle);
688
689         obj_handle_to_location(handle, &page, &obj_idx);
690         get_zspage_mapping(get_first_page(page), &class_idx, &fg);
691         class = &pool->size_class[class_idx];
692         off = obj_idx_to_offset(page, obj_idx, class->size);
693
694         area = &get_cpu_var(zs_map_area);
695         if (off + class->size <= PAGE_SIZE) {
696                 /* this object is contained entirely within a page */
697                 area->vm_addr = kmap_atomic(page);
698         } else {
699                 /* this object spans two pages */
700                 struct page *nextp;
701
702                 nextp = get_next_page(page);
703                 BUG_ON(!nextp);
704
705
706                 set_pte(area->vm_ptes[0], mk_pte(page, PAGE_KERNEL));
707                 set_pte(area->vm_ptes[1], mk_pte(nextp, PAGE_KERNEL));
708
709                 /* We pre-allocated VM area so mapping can never fail */
710                 area->vm_addr = area->vm->addr;
711         }
712
713         return area->vm_addr + off;
714 }
715 EXPORT_SYMBOL_GPL(zs_map_object);
716
717 void zs_unmap_object(struct zs_pool *pool, void *handle)
718 {
719         struct page *page;
720         unsigned long obj_idx, off;
721
722         unsigned int class_idx;
723         enum fullness_group fg;
724         struct size_class *class;
725         struct mapping_area *area;
726
727         BUG_ON(!handle);
728
729         obj_handle_to_location(handle, &page, &obj_idx);
730         get_zspage_mapping(get_first_page(page), &class_idx, &fg);
731         class = &pool->size_class[class_idx];
732         off = obj_idx_to_offset(page, obj_idx, class->size);
733
734         area = &__get_cpu_var(zs_map_area);
735         if (off + class->size <= PAGE_SIZE) {
736                 kunmap_atomic(area->vm_addr);
737         } else {
738                 set_pte(area->vm_ptes[0], __pte(0));
739                 set_pte(area->vm_ptes[1], __pte(0));
740                 __flush_tlb_one((unsigned long)area->vm_addr);
741                 __flush_tlb_one((unsigned long)area->vm_addr + PAGE_SIZE);
742         }
743         put_cpu_var(zs_map_area);
744 }
745 EXPORT_SYMBOL_GPL(zs_unmap_object);
746
747 u64 zs_get_total_size_bytes(struct zs_pool *pool)
748 {
749         int i;
750         u64 npages = 0;
751
752         for (i = 0; i < ZS_SIZE_CLASSES; i++)
753                 npages += pool->size_class[i].pages_allocated;
754
755         return npages << PAGE_SHIFT;
756 }
757 EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);