percpu: move fully free chunk reclamation into a work
[safe/jmp/linux-2.6] / mm / percpu.c
1 /*
2  * linux/mm/percpu.c - percpu memory allocator
3  *
4  * Copyright (C) 2009           SUSE Linux Products GmbH
5  * Copyright (C) 2009           Tejun Heo <tj@kernel.org>
6  *
7  * This file is released under the GPLv2.
8  *
9  * This is percpu allocator which can handle both static and dynamic
10  * areas.  Percpu areas are allocated in chunks in vmalloc area.  Each
11  * chunk is consisted of num_possible_cpus() units and the first chunk
12  * is used for static percpu variables in the kernel image (special
13  * boot time alloc/init handling necessary as these areas need to be
14  * brought up before allocation services are running).  Unit grows as
15  * necessary and all units grow or shrink in unison.  When a chunk is
16  * filled up, another chunk is allocated.  ie. in vmalloc area
17  *
18  *  c0                           c1                         c2
19  *  -------------------          -------------------        ------------
20  * | u0 | u1 | u2 | u3 |        | u0 | u1 | u2 | u3 |      | u0 | u1 | u
21  *  -------------------  ......  -------------------  ....  ------------
22  *
23  * Allocation is done in offset-size areas of single unit space.  Ie,
24  * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0,
25  * c1:u1, c1:u2 and c1:u3.  Percpu access can be done by configuring
26  * percpu base registers UNIT_SIZE apart.
27  *
28  * There are usually many small percpu allocations many of them as
29  * small as 4 bytes.  The allocator organizes chunks into lists
30  * according to free size and tries to allocate from the fullest one.
31  * Each chunk keeps the maximum contiguous area size hint which is
32  * guaranteed to be eqaul to or larger than the maximum contiguous
33  * area in the chunk.  This helps the allocator not to iterate the
34  * chunk maps unnecessarily.
35  *
36  * Allocation state in each chunk is kept using an array of integers
37  * on chunk->map.  A positive value in the map represents a free
38  * region and negative allocated.  Allocation inside a chunk is done
39  * by scanning this map sequentially and serving the first matching
40  * entry.  This is mostly copied from the percpu_modalloc() allocator.
41  * Chunks are also linked into a rb tree to ease address to chunk
42  * mapping during free.
43  *
44  * To use this allocator, arch code should do the followings.
45  *
46  * - define CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
47  *
48  * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
49  *   regular address to percpu pointer and back
50  *
51  * - use pcpu_setup_first_chunk() during percpu area initialization to
52  *   setup the first chunk containing the kernel static percpu area
53  */
54
55 #include <linux/bitmap.h>
56 #include <linux/bootmem.h>
57 #include <linux/list.h>
58 #include <linux/mm.h>
59 #include <linux/module.h>
60 #include <linux/mutex.h>
61 #include <linux/percpu.h>
62 #include <linux/pfn.h>
63 #include <linux/rbtree.h>
64 #include <linux/slab.h>
65 #include <linux/vmalloc.h>
66 #include <linux/workqueue.h>
67
68 #include <asm/cacheflush.h>
69 #include <asm/tlbflush.h>
70
71 #define PCPU_SLOT_BASE_SHIFT            5       /* 1-31 shares the same slot */
72 #define PCPU_DFL_MAP_ALLOC              16      /* start a map with 16 ents */
73
74 struct pcpu_chunk {
75         struct list_head        list;           /* linked to pcpu_slot lists */
76         struct rb_node          rb_node;        /* key is chunk->vm->addr */
77         int                     free_size;      /* free bytes in the chunk */
78         int                     contig_hint;    /* max contiguous size hint */
79         struct vm_struct        *vm;            /* mapped vmalloc region */
80         int                     map_used;       /* # of map entries used */
81         int                     map_alloc;      /* # of map entries allocated */
82         int                     *map;           /* allocation map */
83         bool                    immutable;      /* no [de]population allowed */
84         struct page             **page;         /* points to page array */
85         struct page             *page_ar[];     /* #cpus * UNIT_PAGES */
86 };
87
88 static int pcpu_unit_pages __read_mostly;
89 static int pcpu_unit_size __read_mostly;
90 static int pcpu_chunk_size __read_mostly;
91 static int pcpu_nr_slots __read_mostly;
92 static size_t pcpu_chunk_struct_size __read_mostly;
93
94 /* the address of the first chunk which starts with the kernel static area */
95 void *pcpu_base_addr __read_mostly;
96 EXPORT_SYMBOL_GPL(pcpu_base_addr);
97
98 /* optional reserved chunk, only accessible for reserved allocations */
99 static struct pcpu_chunk *pcpu_reserved_chunk;
100 /* offset limit of the reserved chunk */
101 static int pcpu_reserved_chunk_limit;
102
103 /*
104  * One mutex to rule them all.
105  *
106  * The following mutex is grabbed in the outermost public alloc/free
107  * interface functions and released only when the operation is
108  * complete.  As such, every function in this file other than the
109  * outermost functions are called under pcpu_mutex.
110  *
111  * It can easily be switched to use spinlock such that only the area
112  * allocation and page population commit are protected with it doing
113  * actual [de]allocation without holding any lock.  However, given
114  * what this allocator does, I think it's better to let them run
115  * sequentially.
116  */
117 static DEFINE_MUTEX(pcpu_mutex);
118
119 static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
120 static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
121
122 /* reclaim work to release fully free chunks, scheduled from free path */
123 static void pcpu_reclaim(struct work_struct *work);
124 static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
125
126 static int __pcpu_size_to_slot(int size)
127 {
128         int highbit = fls(size);        /* size is in bytes */
129         return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
130 }
131
132 static int pcpu_size_to_slot(int size)
133 {
134         if (size == pcpu_unit_size)
135                 return pcpu_nr_slots - 1;
136         return __pcpu_size_to_slot(size);
137 }
138
139 static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
140 {
141         if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
142                 return 0;
143
144         return pcpu_size_to_slot(chunk->free_size);
145 }
146
147 static int pcpu_page_idx(unsigned int cpu, int page_idx)
148 {
149         return cpu * pcpu_unit_pages + page_idx;
150 }
151
152 static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
153                                       unsigned int cpu, int page_idx)
154 {
155         return &chunk->page[pcpu_page_idx(cpu, page_idx)];
156 }
157
158 static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
159                                      unsigned int cpu, int page_idx)
160 {
161         return (unsigned long)chunk->vm->addr +
162                 (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
163 }
164
165 static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
166                                      int page_idx)
167 {
168         return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
169 }
170
171 /**
172  * pcpu_mem_alloc - allocate memory
173  * @size: bytes to allocate
174  *
175  * Allocate @size bytes.  If @size is smaller than PAGE_SIZE,
176  * kzalloc() is used; otherwise, vmalloc() is used.  The returned
177  * memory is always zeroed.
178  *
179  * RETURNS:
180  * Pointer to the allocated area on success, NULL on failure.
181  */
182 static void *pcpu_mem_alloc(size_t size)
183 {
184         if (size <= PAGE_SIZE)
185                 return kzalloc(size, GFP_KERNEL);
186         else {
187                 void *ptr = vmalloc(size);
188                 if (ptr)
189                         memset(ptr, 0, size);
190                 return ptr;
191         }
192 }
193
194 /**
195  * pcpu_mem_free - free memory
196  * @ptr: memory to free
197  * @size: size of the area
198  *
199  * Free @ptr.  @ptr should have been allocated using pcpu_mem_alloc().
200  */
201 static void pcpu_mem_free(void *ptr, size_t size)
202 {
203         if (size <= PAGE_SIZE)
204                 kfree(ptr);
205         else
206                 vfree(ptr);
207 }
208
209 /**
210  * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
211  * @chunk: chunk of interest
212  * @oslot: the previous slot it was on
213  *
214  * This function is called after an allocation or free changed @chunk.
215  * New slot according to the changed state is determined and @chunk is
216  * moved to the slot.  Note that the reserved chunk is never put on
217  * chunk slots.
218  */
219 static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
220 {
221         int nslot = pcpu_chunk_slot(chunk);
222
223         if (chunk != pcpu_reserved_chunk && oslot != nslot) {
224                 if (oslot < nslot)
225                         list_move(&chunk->list, &pcpu_slot[nslot]);
226                 else
227                         list_move_tail(&chunk->list, &pcpu_slot[nslot]);
228         }
229 }
230
231 static struct rb_node **pcpu_chunk_rb_search(void *addr,
232                                              struct rb_node **parentp)
233 {
234         struct rb_node **p = &pcpu_addr_root.rb_node;
235         struct rb_node *parent = NULL;
236         struct pcpu_chunk *chunk;
237
238         while (*p) {
239                 parent = *p;
240                 chunk = rb_entry(parent, struct pcpu_chunk, rb_node);
241
242                 if (addr < chunk->vm->addr)
243                         p = &(*p)->rb_left;
244                 else if (addr > chunk->vm->addr)
245                         p = &(*p)->rb_right;
246                 else
247                         break;
248         }
249
250         if (parentp)
251                 *parentp = parent;
252         return p;
253 }
254
255 /**
256  * pcpu_chunk_addr_search - search for chunk containing specified address
257  * @addr: address to search for
258  *
259  * Look for chunk which might contain @addr.  More specifically, it
260  * searchs for the chunk with the highest start address which isn't
261  * beyond @addr.
262  *
263  * RETURNS:
264  * The address of the found chunk.
265  */
266 static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
267 {
268         struct rb_node *n, *parent;
269         struct pcpu_chunk *chunk;
270
271         /* is it in the reserved chunk? */
272         if (pcpu_reserved_chunk) {
273                 void *start = pcpu_reserved_chunk->vm->addr;
274
275                 if (addr >= start && addr < start + pcpu_reserved_chunk_limit)
276                         return pcpu_reserved_chunk;
277         }
278
279         /* nah... search the regular ones */
280         n = *pcpu_chunk_rb_search(addr, &parent);
281         if (!n) {
282                 /* no exactly matching chunk, the parent is the closest */
283                 n = parent;
284                 BUG_ON(!n);
285         }
286         chunk = rb_entry(n, struct pcpu_chunk, rb_node);
287
288         if (addr < chunk->vm->addr) {
289                 /* the parent was the next one, look for the previous one */
290                 n = rb_prev(n);
291                 BUG_ON(!n);
292                 chunk = rb_entry(n, struct pcpu_chunk, rb_node);
293         }
294
295         return chunk;
296 }
297
298 /**
299  * pcpu_chunk_addr_insert - insert chunk into address rb tree
300  * @new: chunk to insert
301  *
302  * Insert @new into address rb tree.
303  */
304 static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
305 {
306         struct rb_node **p, *parent;
307
308         p = pcpu_chunk_rb_search(new->vm->addr, &parent);
309         BUG_ON(*p);
310         rb_link_node(&new->rb_node, parent, p);
311         rb_insert_color(&new->rb_node, &pcpu_addr_root);
312 }
313
314 /**
315  * pcpu_extend_area_map - extend area map for allocation
316  * @chunk: target chunk
317  *
318  * Extend area map of @chunk so that it can accomodate an allocation.
319  * A single allocation can split an area into three areas, so this
320  * function makes sure that @chunk->map has at least two extra slots.
321  *
322  * RETURNS:
323  * 0 if noop, 1 if successfully extended, -errno on failure.
324  */
325 static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
326 {
327         int new_alloc;
328         int *new;
329         size_t size;
330
331         /* has enough? */
332         if (chunk->map_alloc >= chunk->map_used + 2)
333                 return 0;
334
335         new_alloc = PCPU_DFL_MAP_ALLOC;
336         while (new_alloc < chunk->map_used + 2)
337                 new_alloc *= 2;
338
339         new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
340         if (!new)
341                 return -ENOMEM;
342
343         size = chunk->map_alloc * sizeof(chunk->map[0]);
344         memcpy(new, chunk->map, size);
345
346         /*
347          * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
348          * one of the first chunks and still using static map.
349          */
350         if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
351                 pcpu_mem_free(chunk->map, size);
352
353         chunk->map_alloc = new_alloc;
354         chunk->map = new;
355         return 0;
356 }
357
358 /**
359  * pcpu_split_block - split a map block
360  * @chunk: chunk of interest
361  * @i: index of map block to split
362  * @head: head size in bytes (can be 0)
363  * @tail: tail size in bytes (can be 0)
364  *
365  * Split the @i'th map block into two or three blocks.  If @head is
366  * non-zero, @head bytes block is inserted before block @i moving it
367  * to @i+1 and reducing its size by @head bytes.
368  *
369  * If @tail is non-zero, the target block, which can be @i or @i+1
370  * depending on @head, is reduced by @tail bytes and @tail byte block
371  * is inserted after the target block.
372  *
373  * @chunk->map must have enough free slots to accomodate the split.
374  */
375 static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
376                              int head, int tail)
377 {
378         int nr_extra = !!head + !!tail;
379
380         BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
381
382         /* insert new subblocks */
383         memmove(&chunk->map[i + nr_extra], &chunk->map[i],
384                 sizeof(chunk->map[0]) * (chunk->map_used - i));
385         chunk->map_used += nr_extra;
386
387         if (head) {
388                 chunk->map[i + 1] = chunk->map[i] - head;
389                 chunk->map[i++] = head;
390         }
391         if (tail) {
392                 chunk->map[i++] -= tail;
393                 chunk->map[i] = tail;
394         }
395 }
396
397 /**
398  * pcpu_alloc_area - allocate area from a pcpu_chunk
399  * @chunk: chunk of interest
400  * @size: wanted size in bytes
401  * @align: wanted align
402  *
403  * Try to allocate @size bytes area aligned at @align from @chunk.
404  * Note that this function only allocates the offset.  It doesn't
405  * populate or map the area.
406  *
407  * @chunk->map must have at least two free slots.
408  *
409  * RETURNS:
410  * Allocated offset in @chunk on success, -1 if no matching area is
411  * found.
412  */
413 static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
414 {
415         int oslot = pcpu_chunk_slot(chunk);
416         int max_contig = 0;
417         int i, off;
418
419         for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
420                 bool is_last = i + 1 == chunk->map_used;
421                 int head, tail;
422
423                 /* extra for alignment requirement */
424                 head = ALIGN(off, align) - off;
425                 BUG_ON(i == 0 && head != 0);
426
427                 if (chunk->map[i] < 0)
428                         continue;
429                 if (chunk->map[i] < head + size) {
430                         max_contig = max(chunk->map[i], max_contig);
431                         continue;
432                 }
433
434                 /*
435                  * If head is small or the previous block is free,
436                  * merge'em.  Note that 'small' is defined as smaller
437                  * than sizeof(int), which is very small but isn't too
438                  * uncommon for percpu allocations.
439                  */
440                 if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
441                         if (chunk->map[i - 1] > 0)
442                                 chunk->map[i - 1] += head;
443                         else {
444                                 chunk->map[i - 1] -= head;
445                                 chunk->free_size -= head;
446                         }
447                         chunk->map[i] -= head;
448                         off += head;
449                         head = 0;
450                 }
451
452                 /* if tail is small, just keep it around */
453                 tail = chunk->map[i] - head - size;
454                 if (tail < sizeof(int))
455                         tail = 0;
456
457                 /* split if warranted */
458                 if (head || tail) {
459                         pcpu_split_block(chunk, i, head, tail);
460                         if (head) {
461                                 i++;
462                                 off += head;
463                                 max_contig = max(chunk->map[i - 1], max_contig);
464                         }
465                         if (tail)
466                                 max_contig = max(chunk->map[i + 1], max_contig);
467                 }
468
469                 /* update hint and mark allocated */
470                 if (is_last)
471                         chunk->contig_hint = max_contig; /* fully scanned */
472                 else
473                         chunk->contig_hint = max(chunk->contig_hint,
474                                                  max_contig);
475
476                 chunk->free_size -= chunk->map[i];
477                 chunk->map[i] = -chunk->map[i];
478
479                 pcpu_chunk_relocate(chunk, oslot);
480                 return off;
481         }
482
483         chunk->contig_hint = max_contig;        /* fully scanned */
484         pcpu_chunk_relocate(chunk, oslot);
485
486         /* tell the upper layer that this chunk has no matching area */
487         return -1;
488 }
489
490 /**
491  * pcpu_free_area - free area to a pcpu_chunk
492  * @chunk: chunk of interest
493  * @freeme: offset of area to free
494  *
495  * Free area starting from @freeme to @chunk.  Note that this function
496  * only modifies the allocation map.  It doesn't depopulate or unmap
497  * the area.
498  */
499 static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
500 {
501         int oslot = pcpu_chunk_slot(chunk);
502         int i, off;
503
504         for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
505                 if (off == freeme)
506                         break;
507         BUG_ON(off != freeme);
508         BUG_ON(chunk->map[i] > 0);
509
510         chunk->map[i] = -chunk->map[i];
511         chunk->free_size += chunk->map[i];
512
513         /* merge with previous? */
514         if (i > 0 && chunk->map[i - 1] >= 0) {
515                 chunk->map[i - 1] += chunk->map[i];
516                 chunk->map_used--;
517                 memmove(&chunk->map[i], &chunk->map[i + 1],
518                         (chunk->map_used - i) * sizeof(chunk->map[0]));
519                 i--;
520         }
521         /* merge with next? */
522         if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
523                 chunk->map[i] += chunk->map[i + 1];
524                 chunk->map_used--;
525                 memmove(&chunk->map[i + 1], &chunk->map[i + 2],
526                         (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
527         }
528
529         chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
530         pcpu_chunk_relocate(chunk, oslot);
531 }
532
533 /**
534  * pcpu_unmap - unmap pages out of a pcpu_chunk
535  * @chunk: chunk of interest
536  * @page_start: page index of the first page to unmap
537  * @page_end: page index of the last page to unmap + 1
538  * @flush: whether to flush cache and tlb or not
539  *
540  * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
541  * If @flush is true, vcache is flushed before unmapping and tlb
542  * after.
543  */
544 static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
545                        bool flush)
546 {
547         unsigned int last = num_possible_cpus() - 1;
548         unsigned int cpu;
549
550         /* unmap must not be done on immutable chunk */
551         WARN_ON(chunk->immutable);
552
553         /*
554          * Each flushing trial can be very expensive, issue flush on
555          * the whole region at once rather than doing it for each cpu.
556          * This could be an overkill but is more scalable.
557          */
558         if (flush)
559                 flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
560                                    pcpu_chunk_addr(chunk, last, page_end));
561
562         for_each_possible_cpu(cpu)
563                 unmap_kernel_range_noflush(
564                                 pcpu_chunk_addr(chunk, cpu, page_start),
565                                 (page_end - page_start) << PAGE_SHIFT);
566
567         /* ditto as flush_cache_vunmap() */
568         if (flush)
569                 flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
570                                        pcpu_chunk_addr(chunk, last, page_end));
571 }
572
573 /**
574  * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
575  * @chunk: chunk to depopulate
576  * @off: offset to the area to depopulate
577  * @size: size of the area to depopulate in bytes
578  * @flush: whether to flush cache and tlb or not
579  *
580  * For each cpu, depopulate and unmap pages [@page_start,@page_end)
581  * from @chunk.  If @flush is true, vcache is flushed before unmapping
582  * and tlb after.
583  */
584 static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
585                                   bool flush)
586 {
587         int page_start = PFN_DOWN(off);
588         int page_end = PFN_UP(off + size);
589         int unmap_start = -1;
590         int uninitialized_var(unmap_end);
591         unsigned int cpu;
592         int i;
593
594         for (i = page_start; i < page_end; i++) {
595                 for_each_possible_cpu(cpu) {
596                         struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
597
598                         if (!*pagep)
599                                 continue;
600
601                         __free_page(*pagep);
602
603                         /*
604                          * If it's partial depopulation, it might get
605                          * populated or depopulated again.  Mark the
606                          * page gone.
607                          */
608                         *pagep = NULL;
609
610                         unmap_start = unmap_start < 0 ? i : unmap_start;
611                         unmap_end = i + 1;
612                 }
613         }
614
615         if (unmap_start >= 0)
616                 pcpu_unmap(chunk, unmap_start, unmap_end, flush);
617 }
618
619 /**
620  * pcpu_map - map pages into a pcpu_chunk
621  * @chunk: chunk of interest
622  * @page_start: page index of the first page to map
623  * @page_end: page index of the last page to map + 1
624  *
625  * For each cpu, map pages [@page_start,@page_end) into @chunk.
626  * vcache is flushed afterwards.
627  */
628 static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
629 {
630         unsigned int last = num_possible_cpus() - 1;
631         unsigned int cpu;
632         int err;
633
634         /* map must not be done on immutable chunk */
635         WARN_ON(chunk->immutable);
636
637         for_each_possible_cpu(cpu) {
638                 err = map_kernel_range_noflush(
639                                 pcpu_chunk_addr(chunk, cpu, page_start),
640                                 (page_end - page_start) << PAGE_SHIFT,
641                                 PAGE_KERNEL,
642                                 pcpu_chunk_pagep(chunk, cpu, page_start));
643                 if (err < 0)
644                         return err;
645         }
646
647         /* flush at once, please read comments in pcpu_unmap() */
648         flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
649                          pcpu_chunk_addr(chunk, last, page_end));
650         return 0;
651 }
652
653 /**
654  * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
655  * @chunk: chunk of interest
656  * @off: offset to the area to populate
657  * @size: size of the area to populate in bytes
658  *
659  * For each cpu, populate and map pages [@page_start,@page_end) into
660  * @chunk.  The area is cleared on return.
661  */
662 static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
663 {
664         const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
665         int page_start = PFN_DOWN(off);
666         int page_end = PFN_UP(off + size);
667         int map_start = -1;
668         int uninitialized_var(map_end);
669         unsigned int cpu;
670         int i;
671
672         for (i = page_start; i < page_end; i++) {
673                 if (pcpu_chunk_page_occupied(chunk, i)) {
674                         if (map_start >= 0) {
675                                 if (pcpu_map(chunk, map_start, map_end))
676                                         goto err;
677                                 map_start = -1;
678                         }
679                         continue;
680                 }
681
682                 map_start = map_start < 0 ? i : map_start;
683                 map_end = i + 1;
684
685                 for_each_possible_cpu(cpu) {
686                         struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
687
688                         *pagep = alloc_pages_node(cpu_to_node(cpu),
689                                                   alloc_mask, 0);
690                         if (!*pagep)
691                                 goto err;
692                 }
693         }
694
695         if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
696                 goto err;
697
698         for_each_possible_cpu(cpu)
699                 memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
700                        size);
701
702         return 0;
703 err:
704         /* likely under heavy memory pressure, give memory back */
705         pcpu_depopulate_chunk(chunk, off, size, true);
706         return -ENOMEM;
707 }
708
709 static void free_pcpu_chunk(struct pcpu_chunk *chunk)
710 {
711         if (!chunk)
712                 return;
713         if (chunk->vm)
714                 free_vm_area(chunk->vm);
715         pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
716         kfree(chunk);
717 }
718
719 static struct pcpu_chunk *alloc_pcpu_chunk(void)
720 {
721         struct pcpu_chunk *chunk;
722
723         chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
724         if (!chunk)
725                 return NULL;
726
727         chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
728         chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
729         chunk->map[chunk->map_used++] = pcpu_unit_size;
730         chunk->page = chunk->page_ar;
731
732         chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
733         if (!chunk->vm) {
734                 free_pcpu_chunk(chunk);
735                 return NULL;
736         }
737
738         INIT_LIST_HEAD(&chunk->list);
739         chunk->free_size = pcpu_unit_size;
740         chunk->contig_hint = pcpu_unit_size;
741
742         return chunk;
743 }
744
745 /**
746  * pcpu_alloc - the percpu allocator
747  * @size: size of area to allocate in bytes
748  * @align: alignment of area (max PAGE_SIZE)
749  * @reserved: allocate from the reserved chunk if available
750  *
751  * Allocate percpu area of @size bytes aligned at @align.  Might
752  * sleep.  Might trigger writeouts.
753  *
754  * RETURNS:
755  * Percpu pointer to the allocated area on success, NULL on failure.
756  */
757 static void *pcpu_alloc(size_t size, size_t align, bool reserved)
758 {
759         void *ptr = NULL;
760         struct pcpu_chunk *chunk;
761         int slot, off;
762
763         if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
764                 WARN(true, "illegal size (%zu) or align (%zu) for "
765                      "percpu allocation\n", size, align);
766                 return NULL;
767         }
768
769         mutex_lock(&pcpu_mutex);
770
771         /* serve reserved allocations from the reserved chunk if available */
772         if (reserved && pcpu_reserved_chunk) {
773                 chunk = pcpu_reserved_chunk;
774                 if (size > chunk->contig_hint ||
775                     pcpu_extend_area_map(chunk) < 0)
776                         goto out_unlock;
777                 off = pcpu_alloc_area(chunk, size, align);
778                 if (off >= 0)
779                         goto area_found;
780                 goto out_unlock;
781         }
782
783         /* search through normal chunks */
784         for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
785                 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
786                         if (size > chunk->contig_hint)
787                                 continue;
788                         if (pcpu_extend_area_map(chunk) < 0)
789                                 goto out_unlock;
790                         off = pcpu_alloc_area(chunk, size, align);
791                         if (off >= 0)
792                                 goto area_found;
793                 }
794         }
795
796         /* hmmm... no space left, create a new chunk */
797         chunk = alloc_pcpu_chunk();
798         if (!chunk)
799                 goto out_unlock;
800         pcpu_chunk_relocate(chunk, -1);
801         pcpu_chunk_addr_insert(chunk);
802
803         off = pcpu_alloc_area(chunk, size, align);
804         if (off < 0)
805                 goto out_unlock;
806
807 area_found:
808         /* populate, map and clear the area */
809         if (pcpu_populate_chunk(chunk, off, size)) {
810                 pcpu_free_area(chunk, off);
811                 goto out_unlock;
812         }
813
814         ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off);
815 out_unlock:
816         mutex_unlock(&pcpu_mutex);
817         return ptr;
818 }
819
820 /**
821  * __alloc_percpu - allocate dynamic percpu area
822  * @size: size of area to allocate in bytes
823  * @align: alignment of area (max PAGE_SIZE)
824  *
825  * Allocate percpu area of @size bytes aligned at @align.  Might
826  * sleep.  Might trigger writeouts.
827  *
828  * RETURNS:
829  * Percpu pointer to the allocated area on success, NULL on failure.
830  */
831 void *__alloc_percpu(size_t size, size_t align)
832 {
833         return pcpu_alloc(size, align, false);
834 }
835 EXPORT_SYMBOL_GPL(__alloc_percpu);
836
837 /**
838  * __alloc_reserved_percpu - allocate reserved percpu area
839  * @size: size of area to allocate in bytes
840  * @align: alignment of area (max PAGE_SIZE)
841  *
842  * Allocate percpu area of @size bytes aligned at @align from reserved
843  * percpu area if arch has set it up; otherwise, allocation is served
844  * from the same dynamic area.  Might sleep.  Might trigger writeouts.
845  *
846  * RETURNS:
847  * Percpu pointer to the allocated area on success, NULL on failure.
848  */
849 void *__alloc_reserved_percpu(size_t size, size_t align)
850 {
851         return pcpu_alloc(size, align, true);
852 }
853
854 /**
855  * pcpu_reclaim - reclaim fully free chunks, workqueue function
856  * @work: unused
857  *
858  * Reclaim all fully free chunks except for the first one.
859  */
860 static void pcpu_reclaim(struct work_struct *work)
861 {
862         LIST_HEAD(todo);
863         struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
864         struct pcpu_chunk *chunk, *next;
865
866         mutex_lock(&pcpu_mutex);
867
868         list_for_each_entry_safe(chunk, next, head, list) {
869                 WARN_ON(chunk->immutable);
870
871                 /* spare the first one */
872                 if (chunk == list_first_entry(head, struct pcpu_chunk, list))
873                         continue;
874
875                 rb_erase(&chunk->rb_node, &pcpu_addr_root);
876                 list_move(&chunk->list, &todo);
877         }
878
879         mutex_unlock(&pcpu_mutex);
880
881         list_for_each_entry_safe(chunk, next, &todo, list) {
882                 pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
883                 free_pcpu_chunk(chunk);
884         }
885 }
886
887 /**
888  * free_percpu - free percpu area
889  * @ptr: pointer to area to free
890  *
891  * Free percpu area @ptr.  Might sleep.
892  */
893 void free_percpu(void *ptr)
894 {
895         void *addr = __pcpu_ptr_to_addr(ptr);
896         struct pcpu_chunk *chunk;
897         int off;
898
899         if (!ptr)
900                 return;
901
902         mutex_lock(&pcpu_mutex);
903
904         chunk = pcpu_chunk_addr_search(addr);
905         off = addr - chunk->vm->addr;
906
907         pcpu_free_area(chunk, off);
908
909         /* if there are more than one fully free chunks, wake up grim reaper */
910         if (chunk->free_size == pcpu_unit_size) {
911                 struct pcpu_chunk *pos;
912
913                 list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
914                         if (pos != chunk) {
915                                 schedule_work(&pcpu_reclaim_work);
916                                 break;
917                         }
918         }
919
920         mutex_unlock(&pcpu_mutex);
921 }
922 EXPORT_SYMBOL_GPL(free_percpu);
923
924 /**
925  * pcpu_setup_first_chunk - initialize the first percpu chunk
926  * @get_page_fn: callback to fetch page pointer
927  * @static_size: the size of static percpu area in bytes
928  * @reserved_size: the size of reserved percpu area in bytes
929  * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
930  * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
931  * @base_addr: mapped address, NULL for auto
932  * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
933  *
934  * Initialize the first percpu chunk which contains the kernel static
935  * perpcu area.  This function is to be called from arch percpu area
936  * setup path.  The first two parameters are mandatory.  The rest are
937  * optional.
938  *
939  * @get_page_fn() should return pointer to percpu page given cpu
940  * number and page number.  It should at least return enough pages to
941  * cover the static area.  The returned pages for static area should
942  * have been initialized with valid data.  If @unit_size is specified,
943  * it can also return pages after the static area.  NULL return
944  * indicates end of pages for the cpu.  Note that @get_page_fn() must
945  * return the same number of pages for all cpus.
946  *
947  * @reserved_size, if non-zero, specifies the amount of bytes to
948  * reserve after the static area in the first chunk.  This reserves
949  * the first chunk such that it's available only through reserved
950  * percpu allocation.  This is primarily used to serve module percpu
951  * static areas on architectures where the addressing model has
952  * limited offset range for symbol relocations to guarantee module
953  * percpu symbols fall inside the relocatable range.
954  *
955  * @unit_size, if non-negative, specifies unit size and must be
956  * aligned to PAGE_SIZE and equal to or larger than @static_size +
957  * @reserved_size + @dyn_size.
958  *
959  * @dyn_size, if non-negative, limits the number of bytes available
960  * for dynamic allocation in the first chunk.  Specifying non-negative
961  * value make percpu leave alone the area beyond @static_size +
962  * @reserved_size + @dyn_size.
963  *
964  * Non-null @base_addr means that the caller already allocated virtual
965  * region for the first chunk and mapped it.  percpu must not mess
966  * with the chunk.  Note that @base_addr with 0 @unit_size or non-NULL
967  * @populate_pte_fn doesn't make any sense.
968  *
969  * @populate_pte_fn is used to populate the pagetable.  NULL means the
970  * caller already populated the pagetable.
971  *
972  * If the first chunk ends up with both reserved and dynamic areas, it
973  * is served by two chunks - one to serve the core static and reserved
974  * areas and the other for the dynamic area.  They share the same vm
975  * and page map but uses different area allocation map to stay away
976  * from each other.  The latter chunk is circulated in the chunk slots
977  * and available for dynamic allocation like any other chunks.
978  *
979  * RETURNS:
980  * The determined pcpu_unit_size which can be used to initialize
981  * percpu access.
982  */
983 size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
984                                      size_t static_size, size_t reserved_size,
985                                      ssize_t unit_size, ssize_t dyn_size,
986                                      void *base_addr,
987                                      pcpu_populate_pte_fn_t populate_pte_fn)
988 {
989         static struct vm_struct first_vm;
990         static int smap[2], dmap[2];
991         struct pcpu_chunk *schunk, *dchunk = NULL;
992         unsigned int cpu;
993         int nr_pages;
994         int err, i;
995
996         /* santiy checks */
997         BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
998                      ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
999         BUG_ON(!static_size);
1000         if (unit_size >= 0) {
1001                 BUG_ON(unit_size < static_size + reserved_size +
1002                                    (dyn_size >= 0 ? dyn_size : 0));
1003                 BUG_ON(unit_size & ~PAGE_MASK);
1004         } else {
1005                 BUG_ON(dyn_size >= 0);
1006                 BUG_ON(base_addr);
1007         }
1008         BUG_ON(base_addr && populate_pte_fn);
1009
1010         if (unit_size >= 0)
1011                 pcpu_unit_pages = unit_size >> PAGE_SHIFT;
1012         else
1013                 pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
1014                                         PFN_UP(static_size + reserved_size));
1015
1016         pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
1017         pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
1018         pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
1019                 + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
1020
1021         if (dyn_size < 0)
1022                 dyn_size = pcpu_unit_size - static_size - reserved_size;
1023
1024         /*
1025          * Allocate chunk slots.  The additional last slot is for
1026          * empty chunks.
1027          */
1028         pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
1029         pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
1030         for (i = 0; i < pcpu_nr_slots; i++)
1031                 INIT_LIST_HEAD(&pcpu_slot[i]);
1032
1033         /*
1034          * Initialize static chunk.  If reserved_size is zero, the
1035          * static chunk covers static area + dynamic allocation area
1036          * in the first chunk.  If reserved_size is not zero, it
1037          * covers static area + reserved area (mostly used for module
1038          * static percpu allocation).
1039          */
1040         schunk = alloc_bootmem(pcpu_chunk_struct_size);
1041         INIT_LIST_HEAD(&schunk->list);
1042         schunk->vm = &first_vm;
1043         schunk->map = smap;
1044         schunk->map_alloc = ARRAY_SIZE(smap);
1045         schunk->page = schunk->page_ar;
1046
1047         if (reserved_size) {
1048                 schunk->free_size = reserved_size;
1049                 pcpu_reserved_chunk = schunk;   /* not for dynamic alloc */
1050         } else {
1051                 schunk->free_size = dyn_size;
1052                 dyn_size = 0;                   /* dynamic area covered */
1053         }
1054         schunk->contig_hint = schunk->free_size;
1055
1056         schunk->map[schunk->map_used++] = -static_size;
1057         if (schunk->free_size)
1058                 schunk->map[schunk->map_used++] = schunk->free_size;
1059
1060         pcpu_reserved_chunk_limit = static_size + schunk->free_size;
1061
1062         /* init dynamic chunk if necessary */
1063         if (dyn_size) {
1064                 dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
1065                 INIT_LIST_HEAD(&dchunk->list);
1066                 dchunk->vm = &first_vm;
1067                 dchunk->map = dmap;
1068                 dchunk->map_alloc = ARRAY_SIZE(dmap);
1069                 dchunk->page = schunk->page_ar; /* share page map with schunk */
1070
1071                 dchunk->contig_hint = dchunk->free_size = dyn_size;
1072                 dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
1073                 dchunk->map[dchunk->map_used++] = dchunk->free_size;
1074         }
1075
1076         /* allocate vm address */
1077         first_vm.flags = VM_ALLOC;
1078         first_vm.size = pcpu_chunk_size;
1079
1080         if (!base_addr)
1081                 vm_area_register_early(&first_vm, PAGE_SIZE);
1082         else {
1083                 /*
1084                  * Pages already mapped.  No need to remap into
1085                  * vmalloc area.  In this case the first chunks can't
1086                  * be mapped or unmapped by percpu and are marked
1087                  * immutable.
1088                  */
1089                 first_vm.addr = base_addr;
1090                 schunk->immutable = true;
1091                 if (dchunk)
1092                         dchunk->immutable = true;
1093         }
1094
1095         /* assign pages */
1096         nr_pages = -1;
1097         for_each_possible_cpu(cpu) {
1098                 for (i = 0; i < pcpu_unit_pages; i++) {
1099                         struct page *page = get_page_fn(cpu, i);
1100
1101                         if (!page)
1102                                 break;
1103                         *pcpu_chunk_pagep(schunk, cpu, i) = page;
1104                 }
1105
1106                 BUG_ON(i < PFN_UP(static_size));
1107
1108                 if (nr_pages < 0)
1109                         nr_pages = i;
1110                 else
1111                         BUG_ON(nr_pages != i);
1112         }
1113
1114         /* map them */
1115         if (populate_pte_fn) {
1116                 for_each_possible_cpu(cpu)
1117                         for (i = 0; i < nr_pages; i++)
1118                                 populate_pte_fn(pcpu_chunk_addr(schunk,
1119                                                                 cpu, i));
1120
1121                 err = pcpu_map(schunk, 0, nr_pages);
1122                 if (err)
1123                         panic("failed to setup static percpu area, err=%d\n",
1124                               err);
1125         }
1126
1127         /* link the first chunk in */
1128         if (!dchunk) {
1129                 pcpu_chunk_relocate(schunk, -1);
1130                 pcpu_chunk_addr_insert(schunk);
1131         } else {
1132                 pcpu_chunk_relocate(dchunk, -1);
1133                 pcpu_chunk_addr_insert(dchunk);
1134         }
1135
1136         /* we're done */
1137         pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
1138         return pcpu_unit_size;
1139 }