2 * linux/mm/percpu.c - percpu memory allocator
4 * Copyright (C) 2009 SUSE Linux Products GmbH
5 * Copyright (C) 2009 Tejun Heo <tj@kernel.org>
7 * This file is released under the GPLv2.
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
19 * ------------------- ------------------- ------------
20 * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u
21 * ------------------- ...... ------------------- .... ------------
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.
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.
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.
44 * To use this allocator, arch code should do the followings.
46 * - define CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
48 * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
49 * regular address to percpu pointer and back
51 * - use pcpu_setup_first_chunk() during percpu area initialization to
52 * setup the first chunk containing the kernel static percpu area
55 #include <linux/bitmap.h>
56 #include <linux/bootmem.h>
57 #include <linux/list.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>
67 #include <asm/cacheflush.h>
68 #include <asm/tlbflush.h>
70 #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
71 #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
74 struct list_head list; /* linked to pcpu_slot lists */
75 struct rb_node rb_node; /* key is chunk->vm->addr */
76 int free_size; /* free bytes in the chunk */
77 int contig_hint; /* max contiguous size hint */
78 struct vm_struct *vm; /* mapped vmalloc region */
79 int map_used; /* # of map entries used */
80 int map_alloc; /* # of map entries allocated */
81 int *map; /* allocation map */
82 bool immutable; /* no [de]population allowed */
83 struct page **page; /* points to page array */
84 struct page *page_ar[]; /* #cpus * UNIT_PAGES */
87 static int pcpu_unit_pages __read_mostly;
88 static int pcpu_unit_size __read_mostly;
89 static int pcpu_chunk_size __read_mostly;
90 static int pcpu_nr_slots __read_mostly;
91 static size_t pcpu_chunk_struct_size __read_mostly;
93 /* the address of the first chunk which starts with the kernel static area */
94 void *pcpu_base_addr __read_mostly;
95 EXPORT_SYMBOL_GPL(pcpu_base_addr);
98 * One mutex to rule them all.
100 * The following mutex is grabbed in the outermost public alloc/free
101 * interface functions and released only when the operation is
102 * complete. As such, every function in this file other than the
103 * outermost functions are called under pcpu_mutex.
105 * It can easily be switched to use spinlock such that only the area
106 * allocation and page population commit are protected with it doing
107 * actual [de]allocation without holding any lock. However, given
108 * what this allocator does, I think it's better to let them run
111 static DEFINE_MUTEX(pcpu_mutex);
113 static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
114 static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
116 static int __pcpu_size_to_slot(int size)
118 int highbit = fls(size); /* size is in bytes */
119 return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
122 static int pcpu_size_to_slot(int size)
124 if (size == pcpu_unit_size)
125 return pcpu_nr_slots - 1;
126 return __pcpu_size_to_slot(size);
129 static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
131 if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
134 return pcpu_size_to_slot(chunk->free_size);
137 static int pcpu_page_idx(unsigned int cpu, int page_idx)
139 return cpu * pcpu_unit_pages + page_idx;
142 static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
143 unsigned int cpu, int page_idx)
145 return &chunk->page[pcpu_page_idx(cpu, page_idx)];
148 static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
149 unsigned int cpu, int page_idx)
151 return (unsigned long)chunk->vm->addr +
152 (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
155 static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
158 return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
162 * pcpu_realloc - versatile realloc
163 * @p: the current pointer (can be NULL for new allocations)
164 * @size: the current size in bytes (can be 0 for new allocations)
165 * @new_size: the wanted new size in bytes (can be 0 for free)
167 * More robust realloc which can be used to allocate, resize or free a
168 * memory area of arbitrary size. If the needed size goes over
169 * PAGE_SIZE, kernel VM is used.
172 * The new pointer on success, NULL on failure.
174 static void *pcpu_realloc(void *p, size_t size, size_t new_size)
178 if (new_size <= PAGE_SIZE)
179 new = kmalloc(new_size, GFP_KERNEL);
181 new = vmalloc(new_size);
182 if (new_size && !new)
185 memcpy(new, p, min(size, new_size));
187 memset(new + size, 0, new_size - size);
189 if (size <= PAGE_SIZE)
198 * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
199 * @chunk: chunk of interest
200 * @oslot: the previous slot it was on
202 * This function is called after an allocation or free changed @chunk.
203 * New slot according to the changed state is determined and @chunk is
206 static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
208 int nslot = pcpu_chunk_slot(chunk);
210 if (oslot != nslot) {
212 list_move(&chunk->list, &pcpu_slot[nslot]);
214 list_move_tail(&chunk->list, &pcpu_slot[nslot]);
218 static struct rb_node **pcpu_chunk_rb_search(void *addr,
219 struct rb_node **parentp)
221 struct rb_node **p = &pcpu_addr_root.rb_node;
222 struct rb_node *parent = NULL;
223 struct pcpu_chunk *chunk;
227 chunk = rb_entry(parent, struct pcpu_chunk, rb_node);
229 if (addr < chunk->vm->addr)
231 else if (addr > chunk->vm->addr)
243 * pcpu_chunk_addr_search - search for chunk containing specified address
244 * @addr: address to search for
246 * Look for chunk which might contain @addr. More specifically, it
247 * searchs for the chunk with the highest start address which isn't
251 * The address of the found chunk.
253 static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
255 struct rb_node *n, *parent;
256 struct pcpu_chunk *chunk;
258 n = *pcpu_chunk_rb_search(addr, &parent);
260 /* no exactly matching chunk, the parent is the closest */
264 chunk = rb_entry(n, struct pcpu_chunk, rb_node);
266 if (addr < chunk->vm->addr) {
267 /* the parent was the next one, look for the previous one */
270 chunk = rb_entry(n, struct pcpu_chunk, rb_node);
277 * pcpu_chunk_addr_insert - insert chunk into address rb tree
278 * @new: chunk to insert
280 * Insert @new into address rb tree.
282 static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
284 struct rb_node **p, *parent;
286 p = pcpu_chunk_rb_search(new->vm->addr, &parent);
288 rb_link_node(&new->rb_node, parent, p);
289 rb_insert_color(&new->rb_node, &pcpu_addr_root);
293 * pcpu_split_block - split a map block
294 * @chunk: chunk of interest
295 * @i: index of map block to split
296 * @head: head size in bytes (can be 0)
297 * @tail: tail size in bytes (can be 0)
299 * Split the @i'th map block into two or three blocks. If @head is
300 * non-zero, @head bytes block is inserted before block @i moving it
301 * to @i+1 and reducing its size by @head bytes.
303 * If @tail is non-zero, the target block, which can be @i or @i+1
304 * depending on @head, is reduced by @tail bytes and @tail byte block
305 * is inserted after the target block.
308 * 0 on success, -errno on failure.
310 static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail)
312 int nr_extra = !!head + !!tail;
313 int target = chunk->map_used + nr_extra;
315 /* reallocation required? */
316 if (chunk->map_alloc < target) {
320 new_alloc = PCPU_DFL_MAP_ALLOC;
321 while (new_alloc < target)
324 if (chunk->map_alloc < PCPU_DFL_MAP_ALLOC) {
326 * map_alloc smaller than the default size
327 * indicates that the chunk is one of the
328 * first chunks and still using static map.
329 * Allocate a dynamic one and copy.
331 new = pcpu_realloc(NULL, 0, new_alloc * sizeof(new[0]));
333 memcpy(new, chunk->map,
334 chunk->map_alloc * sizeof(new[0]));
336 new = pcpu_realloc(chunk->map,
337 chunk->map_alloc * sizeof(new[0]),
338 new_alloc * sizeof(new[0]));
342 chunk->map_alloc = new_alloc;
346 /* insert a new subblock */
347 memmove(&chunk->map[i + nr_extra], &chunk->map[i],
348 sizeof(chunk->map[0]) * (chunk->map_used - i));
349 chunk->map_used += nr_extra;
352 chunk->map[i + 1] = chunk->map[i] - head;
353 chunk->map[i++] = head;
356 chunk->map[i++] -= tail;
357 chunk->map[i] = tail;
363 * pcpu_alloc_area - allocate area from a pcpu_chunk
364 * @chunk: chunk of interest
365 * @size: wanted size in bytes
366 * @align: wanted align
368 * Try to allocate @size bytes area aligned at @align from @chunk.
369 * Note that this function only allocates the offset. It doesn't
370 * populate or map the area.
373 * Allocated offset in @chunk on success, -errno on failure.
375 static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
377 int oslot = pcpu_chunk_slot(chunk);
381 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
382 bool is_last = i + 1 == chunk->map_used;
385 /* extra for alignment requirement */
386 head = ALIGN(off, align) - off;
387 BUG_ON(i == 0 && head != 0);
389 if (chunk->map[i] < 0)
391 if (chunk->map[i] < head + size) {
392 max_contig = max(chunk->map[i], max_contig);
397 * If head is small or the previous block is free,
398 * merge'em. Note that 'small' is defined as smaller
399 * than sizeof(int), which is very small but isn't too
400 * uncommon for percpu allocations.
402 if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
403 if (chunk->map[i - 1] > 0)
404 chunk->map[i - 1] += head;
406 chunk->map[i - 1] -= head;
407 chunk->free_size -= head;
409 chunk->map[i] -= head;
414 /* if tail is small, just keep it around */
415 tail = chunk->map[i] - head - size;
416 if (tail < sizeof(int))
419 /* split if warranted */
421 if (pcpu_split_block(chunk, i, head, tail))
426 max_contig = max(chunk->map[i - 1], max_contig);
429 max_contig = max(chunk->map[i + 1], max_contig);
432 /* update hint and mark allocated */
434 chunk->contig_hint = max_contig; /* fully scanned */
436 chunk->contig_hint = max(chunk->contig_hint,
439 chunk->free_size -= chunk->map[i];
440 chunk->map[i] = -chunk->map[i];
442 pcpu_chunk_relocate(chunk, oslot);
446 chunk->contig_hint = max_contig; /* fully scanned */
447 pcpu_chunk_relocate(chunk, oslot);
450 * Tell the upper layer that this chunk has no area left.
451 * Note that this is not an error condition but a notification
452 * to upper layer that it needs to look at other chunks.
453 * -ENOSPC is chosen as it isn't used in memory subsystem and
454 * matches the meaning in a way.
460 * pcpu_free_area - free area to a pcpu_chunk
461 * @chunk: chunk of interest
462 * @freeme: offset of area to free
464 * Free area starting from @freeme to @chunk. Note that this function
465 * only modifies the allocation map. It doesn't depopulate or unmap
468 static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
470 int oslot = pcpu_chunk_slot(chunk);
473 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
476 BUG_ON(off != freeme);
477 BUG_ON(chunk->map[i] > 0);
479 chunk->map[i] = -chunk->map[i];
480 chunk->free_size += chunk->map[i];
482 /* merge with previous? */
483 if (i > 0 && chunk->map[i - 1] >= 0) {
484 chunk->map[i - 1] += chunk->map[i];
486 memmove(&chunk->map[i], &chunk->map[i + 1],
487 (chunk->map_used - i) * sizeof(chunk->map[0]));
490 /* merge with next? */
491 if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
492 chunk->map[i] += chunk->map[i + 1];
494 memmove(&chunk->map[i + 1], &chunk->map[i + 2],
495 (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
498 chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
499 pcpu_chunk_relocate(chunk, oslot);
503 * pcpu_unmap - unmap pages out of a pcpu_chunk
504 * @chunk: chunk of interest
505 * @page_start: page index of the first page to unmap
506 * @page_end: page index of the last page to unmap + 1
507 * @flush: whether to flush cache and tlb or not
509 * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
510 * If @flush is true, vcache is flushed before unmapping and tlb
513 static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
516 unsigned int last = num_possible_cpus() - 1;
519 /* unmap must not be done on immutable chunk */
520 WARN_ON(chunk->immutable);
523 * Each flushing trial can be very expensive, issue flush on
524 * the whole region at once rather than doing it for each cpu.
525 * This could be an overkill but is more scalable.
528 flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
529 pcpu_chunk_addr(chunk, last, page_end));
531 for_each_possible_cpu(cpu)
532 unmap_kernel_range_noflush(
533 pcpu_chunk_addr(chunk, cpu, page_start),
534 (page_end - page_start) << PAGE_SHIFT);
536 /* ditto as flush_cache_vunmap() */
538 flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
539 pcpu_chunk_addr(chunk, last, page_end));
543 * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
544 * @chunk: chunk to depopulate
545 * @off: offset to the area to depopulate
546 * @size: size of the area to depopulate in bytes
547 * @flush: whether to flush cache and tlb or not
549 * For each cpu, depopulate and unmap pages [@page_start,@page_end)
550 * from @chunk. If @flush is true, vcache is flushed before unmapping
553 static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
556 int page_start = PFN_DOWN(off);
557 int page_end = PFN_UP(off + size);
558 int unmap_start = -1;
559 int uninitialized_var(unmap_end);
563 for (i = page_start; i < page_end; i++) {
564 for_each_possible_cpu(cpu) {
565 struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
573 * If it's partial depopulation, it might get
574 * populated or depopulated again. Mark the
579 unmap_start = unmap_start < 0 ? i : unmap_start;
584 if (unmap_start >= 0)
585 pcpu_unmap(chunk, unmap_start, unmap_end, flush);
589 * pcpu_map - map pages into a pcpu_chunk
590 * @chunk: chunk of interest
591 * @page_start: page index of the first page to map
592 * @page_end: page index of the last page to map + 1
594 * For each cpu, map pages [@page_start,@page_end) into @chunk.
595 * vcache is flushed afterwards.
597 static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
599 unsigned int last = num_possible_cpus() - 1;
603 /* map must not be done on immutable chunk */
604 WARN_ON(chunk->immutable);
606 for_each_possible_cpu(cpu) {
607 err = map_kernel_range_noflush(
608 pcpu_chunk_addr(chunk, cpu, page_start),
609 (page_end - page_start) << PAGE_SHIFT,
611 pcpu_chunk_pagep(chunk, cpu, page_start));
616 /* flush at once, please read comments in pcpu_unmap() */
617 flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
618 pcpu_chunk_addr(chunk, last, page_end));
623 * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
624 * @chunk: chunk of interest
625 * @off: offset to the area to populate
626 * @size: size of the area to populate in bytes
628 * For each cpu, populate and map pages [@page_start,@page_end) into
629 * @chunk. The area is cleared on return.
631 static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
633 const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
634 int page_start = PFN_DOWN(off);
635 int page_end = PFN_UP(off + size);
637 int uninitialized_var(map_end);
641 for (i = page_start; i < page_end; i++) {
642 if (pcpu_chunk_page_occupied(chunk, i)) {
643 if (map_start >= 0) {
644 if (pcpu_map(chunk, map_start, map_end))
651 map_start = map_start < 0 ? i : map_start;
654 for_each_possible_cpu(cpu) {
655 struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
657 *pagep = alloc_pages_node(cpu_to_node(cpu),
664 if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
667 for_each_possible_cpu(cpu)
668 memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
673 /* likely under heavy memory pressure, give memory back */
674 pcpu_depopulate_chunk(chunk, off, size, true);
678 static void free_pcpu_chunk(struct pcpu_chunk *chunk)
683 free_vm_area(chunk->vm);
684 pcpu_realloc(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]), 0);
688 static struct pcpu_chunk *alloc_pcpu_chunk(void)
690 struct pcpu_chunk *chunk;
692 chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
696 chunk->map = pcpu_realloc(NULL, 0,
697 PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
698 chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
699 chunk->map[chunk->map_used++] = pcpu_unit_size;
700 chunk->page = chunk->page_ar;
702 chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
704 free_pcpu_chunk(chunk);
708 INIT_LIST_HEAD(&chunk->list);
709 chunk->free_size = pcpu_unit_size;
710 chunk->contig_hint = pcpu_unit_size;
716 * __alloc_percpu - allocate percpu area
717 * @size: size of area to allocate in bytes
718 * @align: alignment of area (max PAGE_SIZE)
720 * Allocate percpu area of @size bytes aligned at @align. Might
721 * sleep. Might trigger writeouts.
724 * Percpu pointer to the allocated area on success, NULL on failure.
726 void *__alloc_percpu(size_t size, size_t align)
729 struct pcpu_chunk *chunk;
732 if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
733 WARN(true, "illegal size (%zu) or align (%zu) for "
734 "percpu allocation\n", size, align);
738 mutex_lock(&pcpu_mutex);
741 for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
742 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
743 if (size > chunk->contig_hint)
745 off = pcpu_alloc_area(chunk, size, align);
753 /* hmmm... no space left, create a new chunk */
754 chunk = alloc_pcpu_chunk();
757 pcpu_chunk_relocate(chunk, -1);
758 pcpu_chunk_addr_insert(chunk);
760 off = pcpu_alloc_area(chunk, size, align);
765 /* populate, map and clear the area */
766 if (pcpu_populate_chunk(chunk, off, size)) {
767 pcpu_free_area(chunk, off);
771 ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off);
773 mutex_unlock(&pcpu_mutex);
776 EXPORT_SYMBOL_GPL(__alloc_percpu);
778 static void pcpu_kill_chunk(struct pcpu_chunk *chunk)
780 WARN_ON(chunk->immutable);
781 pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
782 list_del(&chunk->list);
783 rb_erase(&chunk->rb_node, &pcpu_addr_root);
784 free_pcpu_chunk(chunk);
788 * free_percpu - free percpu area
789 * @ptr: pointer to area to free
791 * Free percpu area @ptr. Might sleep.
793 void free_percpu(void *ptr)
795 void *addr = __pcpu_ptr_to_addr(ptr);
796 struct pcpu_chunk *chunk;
802 mutex_lock(&pcpu_mutex);
804 chunk = pcpu_chunk_addr_search(addr);
805 off = addr - chunk->vm->addr;
807 pcpu_free_area(chunk, off);
809 /* the chunk became fully free, kill one if there are other free ones */
810 if (chunk->free_size == pcpu_unit_size) {
811 struct pcpu_chunk *pos;
813 list_for_each_entry(pos,
814 &pcpu_slot[pcpu_chunk_slot(chunk)], list)
816 pcpu_kill_chunk(pos);
821 mutex_unlock(&pcpu_mutex);
823 EXPORT_SYMBOL_GPL(free_percpu);
826 * pcpu_setup_first_chunk - initialize the first percpu chunk
827 * @get_page_fn: callback to fetch page pointer
828 * @static_size: the size of static percpu area in bytes
829 * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
830 * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
831 * @base_addr: mapped address, NULL for auto
832 * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
834 * Initialize the first percpu chunk which contains the kernel static
835 * perpcu area. This function is to be called from arch percpu area
836 * setup path. The first two parameters are mandatory. The rest are
839 * @get_page_fn() should return pointer to percpu page given cpu
840 * number and page number. It should at least return enough pages to
841 * cover the static area. The returned pages for static area should
842 * have been initialized with valid data. If @unit_size is specified,
843 * it can also return pages after the static area. NULL return
844 * indicates end of pages for the cpu. Note that @get_page_fn() must
845 * return the same number of pages for all cpus.
847 * @unit_size, if non-negative, specifies unit size and must be
848 * aligned to PAGE_SIZE and equal to or larger than @static_size +
851 * @dyn_size, if non-negative, limits the number of bytes available
852 * for dynamic allocation in the first chunk. Specifying non-negative
853 * value make percpu leave alone the area beyond @static_size +
856 * Non-null @base_addr means that the caller already allocated virtual
857 * region for the first chunk and mapped it. percpu must not mess
858 * with the chunk. Note that @base_addr with 0 @unit_size or non-NULL
859 * @populate_pte_fn doesn't make any sense.
861 * @populate_pte_fn is used to populate the pagetable. NULL means the
862 * caller already populated the pagetable.
865 * The determined pcpu_unit_size which can be used to initialize
868 size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
870 ssize_t unit_size, ssize_t dyn_size,
872 pcpu_populate_pte_fn_t populate_pte_fn)
874 static struct vm_struct first_vm;
876 struct pcpu_chunk *schunk;
882 BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC);
883 BUG_ON(!static_size);
884 if (unit_size >= 0) {
885 BUG_ON(unit_size < static_size +
886 (dyn_size >= 0 ? dyn_size : 0));
887 BUG_ON(unit_size & ~PAGE_MASK);
889 BUG_ON(dyn_size >= 0);
892 BUG_ON(base_addr && populate_pte_fn);
895 pcpu_unit_pages = unit_size >> PAGE_SHIFT;
897 pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
898 PFN_UP(static_size));
900 pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
901 pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
902 pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
903 + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
906 dyn_size = pcpu_unit_size - static_size;
909 * Allocate chunk slots. The additional last slot is for
912 pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
913 pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
914 for (i = 0; i < pcpu_nr_slots; i++)
915 INIT_LIST_HEAD(&pcpu_slot[i]);
917 /* init static chunk */
918 schunk = alloc_bootmem(pcpu_chunk_struct_size);
919 INIT_LIST_HEAD(&schunk->list);
920 schunk->vm = &first_vm;
922 schunk->map_alloc = ARRAY_SIZE(smap);
923 schunk->page = schunk->page_ar;
924 schunk->free_size = dyn_size;
925 schunk->contig_hint = schunk->free_size;
927 schunk->map[schunk->map_used++] = -static_size;
928 if (schunk->free_size)
929 schunk->map[schunk->map_used++] = schunk->free_size;
931 /* allocate vm address */
932 first_vm.flags = VM_ALLOC;
933 first_vm.size = pcpu_chunk_size;
936 vm_area_register_early(&first_vm, PAGE_SIZE);
939 * Pages already mapped. No need to remap into
940 * vmalloc area. In this case the static chunk can't
941 * be mapped or unmapped by percpu and is marked
944 first_vm.addr = base_addr;
945 schunk->immutable = true;
950 for_each_possible_cpu(cpu) {
951 for (i = 0; i < pcpu_unit_pages; i++) {
952 struct page *page = get_page_fn(cpu, i);
956 *pcpu_chunk_pagep(schunk, cpu, i) = page;
959 BUG_ON(i < PFN_UP(static_size));
964 BUG_ON(nr_pages != i);
968 if (populate_pte_fn) {
969 for_each_possible_cpu(cpu)
970 for (i = 0; i < nr_pages; i++)
971 populate_pte_fn(pcpu_chunk_addr(schunk,
974 err = pcpu_map(schunk, 0, nr_pages);
976 panic("failed to setup static percpu area, err=%d\n",
980 /* link the first chunk in */
981 pcpu_chunk_relocate(schunk, -1);
982 pcpu_chunk_addr_insert(schunk);
985 pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
986 return pcpu_unit_size;