aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-03-10 05:16:17 -0400
committerIngo Molnar <mingo@elte.hu>2009-03-10 05:17:48 -0400
commit8293dd6f86e759068ce918aa10ca9c5d6d711cd0 (patch)
treede366d69078bf2b98c6765fa4ec1cc652f3d3173 /mm
parent631595fbf4aeac260e664a8a002897e4db6a50dd (diff)
parent467c88fee51e2ae862e9485245687da0730e29aa (diff)
Merge branch 'x86/core' into tracing/ftrace
Semantic merge: kernel/trace/trace_functions_graph.c Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'mm')
-rw-r--r--mm/percpu.c593
1 files changed, 420 insertions, 173 deletions
diff --git a/mm/percpu.c b/mm/percpu.c
index 3d0f5456827c..bfe6a3afaf45 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -62,7 +62,9 @@
62#include <linux/pfn.h> 62#include <linux/pfn.h>
63#include <linux/rbtree.h> 63#include <linux/rbtree.h>
64#include <linux/slab.h> 64#include <linux/slab.h>
65#include <linux/spinlock.h>
65#include <linux/vmalloc.h> 66#include <linux/vmalloc.h>
67#include <linux/workqueue.h>
66 68
67#include <asm/cacheflush.h> 69#include <asm/cacheflush.h>
68#include <asm/tlbflush.h> 70#include <asm/tlbflush.h>
@@ -80,7 +82,8 @@ struct pcpu_chunk {
80 int map_alloc; /* # of map entries allocated */ 82 int map_alloc; /* # of map entries allocated */
81 int *map; /* allocation map */ 83 int *map; /* allocation map */
82 bool immutable; /* no [de]population allowed */ 84 bool immutable; /* no [de]population allowed */
83 struct page *page[]; /* #cpus * UNIT_PAGES */ 85 struct page **page; /* points to page array */
86 struct page *page_ar[]; /* #cpus * UNIT_PAGES */
84}; 87};
85 88
86static int pcpu_unit_pages __read_mostly; 89static int pcpu_unit_pages __read_mostly;
@@ -93,28 +96,42 @@ static size_t pcpu_chunk_struct_size __read_mostly;
93void *pcpu_base_addr __read_mostly; 96void *pcpu_base_addr __read_mostly;
94EXPORT_SYMBOL_GPL(pcpu_base_addr); 97EXPORT_SYMBOL_GPL(pcpu_base_addr);
95 98
96/* the size of kernel static area */ 99/* optional reserved chunk, only accessible for reserved allocations */
97static int pcpu_static_size __read_mostly; 100static struct pcpu_chunk *pcpu_reserved_chunk;
101/* offset limit of the reserved chunk */
102static int pcpu_reserved_chunk_limit;
98 103
99/* 104/*
100 * One mutex to rule them all. 105 * Synchronization rules.
101 * 106 *
102 * The following mutex is grabbed in the outermost public alloc/free 107 * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former
103 * interface functions and released only when the operation is 108 * protects allocation/reclaim paths, chunks and chunk->page arrays.
104 * complete. As such, every function in this file other than the 109 * The latter is a spinlock and protects the index data structures -
105 * outermost functions are called under pcpu_mutex. 110 * chunk slots, rbtree, chunks and area maps in chunks.
106 * 111 *
107 * It can easily be switched to use spinlock such that only the area 112 * During allocation, pcpu_alloc_mutex is kept locked all the time and
108 * allocation and page population commit are protected with it doing 113 * pcpu_lock is grabbed and released as necessary. All actual memory
109 * actual [de]allocation without holding any lock. However, given 114 * allocations are done using GFP_KERNEL with pcpu_lock released.
110 * what this allocator does, I think it's better to let them run 115 *
111 * sequentially. 116 * Free path accesses and alters only the index data structures, so it
117 * can be safely called from atomic context. When memory needs to be
118 * returned to the system, free path schedules reclaim_work which
119 * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
120 * reclaimed, release both locks and frees the chunks. Note that it's
121 * necessary to grab both locks to remove a chunk from circulation as
122 * allocation path might be referencing the chunk with only
123 * pcpu_alloc_mutex locked.
112 */ 124 */
113static DEFINE_MUTEX(pcpu_mutex); 125static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */
126static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */
114 127
115static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */ 128static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
116static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */ 129static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
117 130
131/* reclaim work to release fully free chunks, scheduled from free path */
132static void pcpu_reclaim(struct work_struct *work);
133static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
134
118static int __pcpu_size_to_slot(int size) 135static int __pcpu_size_to_slot(int size)
119{ 136{
120 int highbit = fls(size); /* size is in bytes */ 137 int highbit = fls(size); /* size is in bytes */
@@ -161,39 +178,44 @@ static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
161} 178}
162 179
163/** 180/**
164 * pcpu_realloc - versatile realloc 181 * pcpu_mem_alloc - allocate memory
165 * @p: the current pointer (can be NULL for new allocations) 182 * @size: bytes to allocate
166 * @size: the current size in bytes (can be 0 for new allocations) 183 *
167 * @new_size: the wanted new size in bytes (can be 0 for free) 184 * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
185 * kzalloc() is used; otherwise, vmalloc() is used. The returned
186 * memory is always zeroed.
168 * 187 *
169 * More robust realloc which can be used to allocate, resize or free a 188 * CONTEXT:
170 * memory area of arbitrary size. If the needed size goes over 189 * Does GFP_KERNEL allocation.
171 * PAGE_SIZE, kernel VM is used.
172 * 190 *
173 * RETURNS: 191 * RETURNS:
174 * The new pointer on success, NULL on failure. 192 * Pointer to the allocated area on success, NULL on failure.
175 */ 193 */
176static void *pcpu_realloc(void *p, size_t size, size_t new_size) 194static void *pcpu_mem_alloc(size_t size)
177{ 195{
178 void *new; 196 if (size <= PAGE_SIZE)
179 197 return kzalloc(size, GFP_KERNEL);
180 if (new_size <= PAGE_SIZE) 198 else {
181 new = kmalloc(new_size, GFP_KERNEL); 199 void *ptr = vmalloc(size);
182 else 200 if (ptr)
183 new = vmalloc(new_size); 201 memset(ptr, 0, size);
184 if (new_size && !new) 202 return ptr;
185 return NULL; 203 }
186 204}
187 memcpy(new, p, min(size, new_size));
188 if (new_size > size)
189 memset(new + size, 0, new_size - size);
190 205
206/**
207 * pcpu_mem_free - free memory
208 * @ptr: memory to free
209 * @size: size of the area
210 *
211 * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
212 */
213static void pcpu_mem_free(void *ptr, size_t size)
214{
191 if (size <= PAGE_SIZE) 215 if (size <= PAGE_SIZE)
192 kfree(p); 216 kfree(ptr);
193 else 217 else
194 vfree(p); 218 vfree(ptr);
195
196 return new;
197} 219}
198 220
199/** 221/**
@@ -203,13 +225,17 @@ static void *pcpu_realloc(void *p, size_t size, size_t new_size)
203 * 225 *
204 * This function is called after an allocation or free changed @chunk. 226 * This function is called after an allocation or free changed @chunk.
205 * New slot according to the changed state is determined and @chunk is 227 * New slot according to the changed state is determined and @chunk is
206 * moved to the slot. 228 * moved to the slot. Note that the reserved chunk is never put on
229 * chunk slots.
230 *
231 * CONTEXT:
232 * pcpu_lock.
207 */ 233 */
208static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) 234static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
209{ 235{
210 int nslot = pcpu_chunk_slot(chunk); 236 int nslot = pcpu_chunk_slot(chunk);
211 237
212 if (oslot != nslot) { 238 if (chunk != pcpu_reserved_chunk && oslot != nslot) {
213 if (oslot < nslot) 239 if (oslot < nslot)
214 list_move(&chunk->list, &pcpu_slot[nslot]); 240 list_move(&chunk->list, &pcpu_slot[nslot]);
215 else 241 else
@@ -249,6 +275,9 @@ static struct rb_node **pcpu_chunk_rb_search(void *addr,
249 * searchs for the chunk with the highest start address which isn't 275 * searchs for the chunk with the highest start address which isn't
250 * beyond @addr. 276 * beyond @addr.
251 * 277 *
278 * CONTEXT:
279 * pcpu_lock.
280 *
252 * RETURNS: 281 * RETURNS:
253 * The address of the found chunk. 282 * The address of the found chunk.
254 */ 283 */
@@ -257,6 +286,15 @@ static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
257 struct rb_node *n, *parent; 286 struct rb_node *n, *parent;
258 struct pcpu_chunk *chunk; 287 struct pcpu_chunk *chunk;
259 288
289 /* is it in the reserved chunk? */
290 if (pcpu_reserved_chunk) {
291 void *start = pcpu_reserved_chunk->vm->addr;
292
293 if (addr >= start && addr < start + pcpu_reserved_chunk_limit)
294 return pcpu_reserved_chunk;
295 }
296
297 /* nah... search the regular ones */
260 n = *pcpu_chunk_rb_search(addr, &parent); 298 n = *pcpu_chunk_rb_search(addr, &parent);
261 if (!n) { 299 if (!n) {
262 /* no exactly matching chunk, the parent is the closest */ 300 /* no exactly matching chunk, the parent is the closest */
@@ -280,6 +318,9 @@ static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
280 * @new: chunk to insert 318 * @new: chunk to insert
281 * 319 *
282 * Insert @new into address rb tree. 320 * Insert @new into address rb tree.
321 *
322 * CONTEXT:
323 * pcpu_lock.
283 */ 324 */
284static void pcpu_chunk_addr_insert(struct pcpu_chunk *new) 325static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
285{ 326{
@@ -292,6 +333,66 @@ static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
292} 333}
293 334
294/** 335/**
336 * pcpu_extend_area_map - extend area map for allocation
337 * @chunk: target chunk
338 *
339 * Extend area map of @chunk so that it can accomodate an allocation.
340 * A single allocation can split an area into three areas, so this
341 * function makes sure that @chunk->map has at least two extra slots.
342 *
343 * CONTEXT:
344 * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
345 * if area map is extended.
346 *
347 * RETURNS:
348 * 0 if noop, 1 if successfully extended, -errno on failure.
349 */
350static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
351{
352 int new_alloc;
353 int *new;
354 size_t size;
355
356 /* has enough? */
357 if (chunk->map_alloc >= chunk->map_used + 2)
358 return 0;
359
360 spin_unlock_irq(&pcpu_lock);
361
362 new_alloc = PCPU_DFL_MAP_ALLOC;
363 while (new_alloc < chunk->map_used + 2)
364 new_alloc *= 2;
365
366 new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
367 if (!new) {
368 spin_lock_irq(&pcpu_lock);
369 return -ENOMEM;
370 }
371
372 /*
373 * Acquire pcpu_lock and switch to new area map. Only free
374 * could have happened inbetween, so map_used couldn't have
375 * grown.
376 */
377 spin_lock_irq(&pcpu_lock);
378 BUG_ON(new_alloc < chunk->map_used + 2);
379
380 size = chunk->map_alloc * sizeof(chunk->map[0]);
381 memcpy(new, chunk->map, size);
382
383 /*
384 * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
385 * one of the first chunks and still using static map.
386 */
387 if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
388 pcpu_mem_free(chunk->map, size);
389
390 chunk->map_alloc = new_alloc;
391 chunk->map = new;
392 return 0;
393}
394
395/**
295 * pcpu_split_block - split a map block 396 * pcpu_split_block - split a map block
296 * @chunk: chunk of interest 397 * @chunk: chunk of interest
297 * @i: index of map block to split 398 * @i: index of map block to split
@@ -306,33 +407,19 @@ static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
306 * depending on @head, is reduced by @tail bytes and @tail byte block 407 * depending on @head, is reduced by @tail bytes and @tail byte block
307 * is inserted after the target block. 408 * is inserted after the target block.
308 * 409 *
309 * RETURNS: 410 * @chunk->map must have enough free slots to accomodate the split.
310 * 0 on success, -errno on failure. 411 *
412 * CONTEXT:
413 * pcpu_lock.
311 */ 414 */
312static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail) 415static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
416 int head, int tail)
313{ 417{
314 int nr_extra = !!head + !!tail; 418 int nr_extra = !!head + !!tail;
315 int target = chunk->map_used + nr_extra;
316
317 /* reallocation required? */
318 if (chunk->map_alloc < target) {
319 int new_alloc = chunk->map_alloc;
320 int *new;
321 419
322 while (new_alloc < target) 420 BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
323 new_alloc *= 2;
324 421
325 new = pcpu_realloc(chunk->map, 422 /* insert new subblocks */
326 chunk->map_alloc * sizeof(new[0]),
327 new_alloc * sizeof(new[0]));
328 if (!new)
329 return -ENOMEM;
330
331 chunk->map_alloc = new_alloc;
332 chunk->map = new;
333 }
334
335 /* insert a new subblock */
336 memmove(&chunk->map[i + nr_extra], &chunk->map[i], 423 memmove(&chunk->map[i + nr_extra], &chunk->map[i],
337 sizeof(chunk->map[0]) * (chunk->map_used - i)); 424 sizeof(chunk->map[0]) * (chunk->map_used - i));
338 chunk->map_used += nr_extra; 425 chunk->map_used += nr_extra;
@@ -345,7 +432,6 @@ static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail)
345 chunk->map[i++] -= tail; 432 chunk->map[i++] -= tail;
346 chunk->map[i] = tail; 433 chunk->map[i] = tail;
347 } 434 }
348 return 0;
349} 435}
350 436
351/** 437/**
@@ -358,8 +444,14 @@ static int pcpu_split_block(struct pcpu_chunk *chunk, int i, int head, int tail)
358 * Note that this function only allocates the offset. It doesn't 444 * Note that this function only allocates the offset. It doesn't
359 * populate or map the area. 445 * populate or map the area.
360 * 446 *
447 * @chunk->map must have at least two free slots.
448 *
449 * CONTEXT:
450 * pcpu_lock.
451 *
361 * RETURNS: 452 * RETURNS:
362 * Allocated offset in @chunk on success, -errno on failure. 453 * Allocated offset in @chunk on success, -1 if no matching area is
454 * found.
363 */ 455 */
364static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align) 456static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
365{ 457{
@@ -367,22 +459,6 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
367 int max_contig = 0; 459 int max_contig = 0;
368 int i, off; 460 int i, off;
369 461
370 /*
371 * The static chunk initially doesn't have map attached
372 * because kmalloc wasn't available during init. Give it one.
373 */
374 if (unlikely(!chunk->map)) {
375 chunk->map = pcpu_realloc(NULL, 0,
376 PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
377 if (!chunk->map)
378 return -ENOMEM;
379
380 chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
381 chunk->map[chunk->map_used++] = -pcpu_static_size;
382 if (chunk->free_size)
383 chunk->map[chunk->map_used++] = chunk->free_size;
384 }
385
386 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) { 462 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
387 bool is_last = i + 1 == chunk->map_used; 463 bool is_last = i + 1 == chunk->map_used;
388 int head, tail; 464 int head, tail;
@@ -423,8 +499,7 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
423 499
424 /* split if warranted */ 500 /* split if warranted */
425 if (head || tail) { 501 if (head || tail) {
426 if (pcpu_split_block(chunk, i, head, tail)) 502 pcpu_split_block(chunk, i, head, tail);
427 return -ENOMEM;
428 if (head) { 503 if (head) {
429 i++; 504 i++;
430 off += head; 505 off += head;
@@ -451,14 +526,8 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
451 chunk->contig_hint = max_contig; /* fully scanned */ 526 chunk->contig_hint = max_contig; /* fully scanned */
452 pcpu_chunk_relocate(chunk, oslot); 527 pcpu_chunk_relocate(chunk, oslot);
453 528
454 /* 529 /* tell the upper layer that this chunk has no matching area */
455 * Tell the upper layer that this chunk has no area left. 530 return -1;
456 * Note that this is not an error condition but a notification
457 * to upper layer that it needs to look at other chunks.
458 * -ENOSPC is chosen as it isn't used in memory subsystem and
459 * matches the meaning in a way.
460 */
461 return -ENOSPC;
462} 531}
463 532
464/** 533/**
@@ -469,6 +538,9 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
469 * Free area starting from @freeme to @chunk. Note that this function 538 * Free area starting from @freeme to @chunk. Note that this function
470 * only modifies the allocation map. It doesn't depopulate or unmap 539 * only modifies the allocation map. It doesn't depopulate or unmap
471 * the area. 540 * the area.
541 *
542 * CONTEXT:
543 * pcpu_lock.
472 */ 544 */
473static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme) 545static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
474{ 546{
@@ -554,6 +626,9 @@ static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
554 * For each cpu, depopulate and unmap pages [@page_start,@page_end) 626 * For each cpu, depopulate and unmap pages [@page_start,@page_end)
555 * from @chunk. If @flush is true, vcache is flushed before unmapping 627 * from @chunk. If @flush is true, vcache is flushed before unmapping
556 * and tlb after. 628 * and tlb after.
629 *
630 * CONTEXT:
631 * pcpu_alloc_mutex.
557 */ 632 */
558static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size, 633static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
559 bool flush) 634 bool flush)
@@ -632,6 +707,9 @@ static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
632 * 707 *
633 * For each cpu, populate and map pages [@page_start,@page_end) into 708 * For each cpu, populate and map pages [@page_start,@page_end) into
634 * @chunk. The area is cleared on return. 709 * @chunk. The area is cleared on return.
710 *
711 * CONTEXT:
712 * pcpu_alloc_mutex, does GFP_KERNEL allocation.
635 */ 713 */
636static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size) 714static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
637{ 715{
@@ -686,7 +764,7 @@ static void free_pcpu_chunk(struct pcpu_chunk *chunk)
686 return; 764 return;
687 if (chunk->vm) 765 if (chunk->vm)
688 free_vm_area(chunk->vm); 766 free_vm_area(chunk->vm);
689 pcpu_realloc(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]), 0); 767 pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
690 kfree(chunk); 768 kfree(chunk);
691} 769}
692 770
@@ -698,10 +776,10 @@ static struct pcpu_chunk *alloc_pcpu_chunk(void)
698 if (!chunk) 776 if (!chunk)
699 return NULL; 777 return NULL;
700 778
701 chunk->map = pcpu_realloc(NULL, 0, 779 chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
702 PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
703 chunk->map_alloc = PCPU_DFL_MAP_ALLOC; 780 chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
704 chunk->map[chunk->map_used++] = pcpu_unit_size; 781 chunk->map[chunk->map_used++] = pcpu_unit_size;
782 chunk->page = chunk->page_ar;
705 783
706 chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL); 784 chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
707 if (!chunk->vm) { 785 if (!chunk->vm) {
@@ -717,19 +795,21 @@ static struct pcpu_chunk *alloc_pcpu_chunk(void)
717} 795}
718 796
719/** 797/**
720 * __alloc_percpu - allocate percpu area 798 * pcpu_alloc - the percpu allocator
721 * @size: size of area to allocate in bytes 799 * @size: size of area to allocate in bytes
722 * @align: alignment of area (max PAGE_SIZE) 800 * @align: alignment of area (max PAGE_SIZE)
801 * @reserved: allocate from the reserved chunk if available
723 * 802 *
724 * Allocate percpu area of @size bytes aligned at @align. Might 803 * Allocate percpu area of @size bytes aligned at @align.
725 * sleep. Might trigger writeouts. 804 *
805 * CONTEXT:
806 * Does GFP_KERNEL allocation.
726 * 807 *
727 * RETURNS: 808 * RETURNS:
728 * Percpu pointer to the allocated area on success, NULL on failure. 809 * Percpu pointer to the allocated area on success, NULL on failure.
729 */ 810 */
730void *__alloc_percpu(size_t size, size_t align) 811static void *pcpu_alloc(size_t size, size_t align, bool reserved)
731{ 812{
732 void *ptr = NULL;
733 struct pcpu_chunk *chunk; 813 struct pcpu_chunk *chunk;
734 int slot, off; 814 int slot, off;
735 815
@@ -739,90 +819,192 @@ void *__alloc_percpu(size_t size, size_t align)
739 return NULL; 819 return NULL;
740 } 820 }
741 821
742 mutex_lock(&pcpu_mutex); 822 mutex_lock(&pcpu_alloc_mutex);
823 spin_lock_irq(&pcpu_lock);
824
825 /* serve reserved allocations from the reserved chunk if available */
826 if (reserved && pcpu_reserved_chunk) {
827 chunk = pcpu_reserved_chunk;
828 if (size > chunk->contig_hint ||
829 pcpu_extend_area_map(chunk) < 0)
830 goto fail_unlock;
831 off = pcpu_alloc_area(chunk, size, align);
832 if (off >= 0)
833 goto area_found;
834 goto fail_unlock;
835 }
743 836
744 /* allocate area */ 837restart:
838 /* search through normal chunks */
745 for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) { 839 for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
746 list_for_each_entry(chunk, &pcpu_slot[slot], list) { 840 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
747 if (size > chunk->contig_hint) 841 if (size > chunk->contig_hint)
748 continue; 842 continue;
843
844 switch (pcpu_extend_area_map(chunk)) {
845 case 0:
846 break;
847 case 1:
848 goto restart; /* pcpu_lock dropped, restart */
849 default:
850 goto fail_unlock;
851 }
852
749 off = pcpu_alloc_area(chunk, size, align); 853 off = pcpu_alloc_area(chunk, size, align);
750 if (off >= 0) 854 if (off >= 0)
751 goto area_found; 855 goto area_found;
752 if (off != -ENOSPC)
753 goto out_unlock;
754 } 856 }
755 } 857 }
756 858
757 /* hmmm... no space left, create a new chunk */ 859 /* hmmm... no space left, create a new chunk */
860 spin_unlock_irq(&pcpu_lock);
861
758 chunk = alloc_pcpu_chunk(); 862 chunk = alloc_pcpu_chunk();
759 if (!chunk) 863 if (!chunk)
760 goto out_unlock; 864 goto fail_unlock_mutex;
865
866 spin_lock_irq(&pcpu_lock);
761 pcpu_chunk_relocate(chunk, -1); 867 pcpu_chunk_relocate(chunk, -1);
762 pcpu_chunk_addr_insert(chunk); 868 pcpu_chunk_addr_insert(chunk);
763 869 goto restart;
764 off = pcpu_alloc_area(chunk, size, align);
765 if (off < 0)
766 goto out_unlock;
767 870
768area_found: 871area_found:
872 spin_unlock_irq(&pcpu_lock);
873
769 /* populate, map and clear the area */ 874 /* populate, map and clear the area */
770 if (pcpu_populate_chunk(chunk, off, size)) { 875 if (pcpu_populate_chunk(chunk, off, size)) {
876 spin_lock_irq(&pcpu_lock);
771 pcpu_free_area(chunk, off); 877 pcpu_free_area(chunk, off);
772 goto out_unlock; 878 goto fail_unlock;
773 } 879 }
774 880
775 ptr = __addr_to_pcpu_ptr(chunk->vm->addr + off); 881 mutex_unlock(&pcpu_alloc_mutex);
776out_unlock: 882
777 mutex_unlock(&pcpu_mutex); 883 return __addr_to_pcpu_ptr(chunk->vm->addr + off);
778 return ptr; 884
885fail_unlock:
886 spin_unlock_irq(&pcpu_lock);
887fail_unlock_mutex:
888 mutex_unlock(&pcpu_alloc_mutex);
889 return NULL;
890}
891
892/**
893 * __alloc_percpu - allocate dynamic percpu area
894 * @size: size of area to allocate in bytes
895 * @align: alignment of area (max PAGE_SIZE)
896 *
897 * Allocate percpu area of @size bytes aligned at @align. Might
898 * sleep. Might trigger writeouts.
899 *
900 * CONTEXT:
901 * Does GFP_KERNEL allocation.
902 *
903 * RETURNS:
904 * Percpu pointer to the allocated area on success, NULL on failure.
905 */
906void *__alloc_percpu(size_t size, size_t align)
907{
908 return pcpu_alloc(size, align, false);
779} 909}
780EXPORT_SYMBOL_GPL(__alloc_percpu); 910EXPORT_SYMBOL_GPL(__alloc_percpu);
781 911
782static void pcpu_kill_chunk(struct pcpu_chunk *chunk) 912/**
913 * __alloc_reserved_percpu - allocate reserved percpu area
914 * @size: size of area to allocate in bytes
915 * @align: alignment of area (max PAGE_SIZE)
916 *
917 * Allocate percpu area of @size bytes aligned at @align from reserved
918 * percpu area if arch has set it up; otherwise, allocation is served
919 * from the same dynamic area. Might sleep. Might trigger writeouts.
920 *
921 * CONTEXT:
922 * Does GFP_KERNEL allocation.
923 *
924 * RETURNS:
925 * Percpu pointer to the allocated area on success, NULL on failure.
926 */
927void *__alloc_reserved_percpu(size_t size, size_t align)
928{
929 return pcpu_alloc(size, align, true);
930}
931
932/**
933 * pcpu_reclaim - reclaim fully free chunks, workqueue function
934 * @work: unused
935 *
936 * Reclaim all fully free chunks except for the first one.
937 *
938 * CONTEXT:
939 * workqueue context.
940 */
941static void pcpu_reclaim(struct work_struct *work)
783{ 942{
784 WARN_ON(chunk->immutable); 943 LIST_HEAD(todo);
785 pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false); 944 struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
786 list_del(&chunk->list); 945 struct pcpu_chunk *chunk, *next;
787 rb_erase(&chunk->rb_node, &pcpu_addr_root); 946
788 free_pcpu_chunk(chunk); 947 mutex_lock(&pcpu_alloc_mutex);
948 spin_lock_irq(&pcpu_lock);
949
950 list_for_each_entry_safe(chunk, next, head, list) {
951 WARN_ON(chunk->immutable);
952
953 /* spare the first one */
954 if (chunk == list_first_entry(head, struct pcpu_chunk, list))
955 continue;
956
957 rb_erase(&chunk->rb_node, &pcpu_addr_root);
958 list_move(&chunk->list, &todo);
959 }
960
961 spin_unlock_irq(&pcpu_lock);
962 mutex_unlock(&pcpu_alloc_mutex);
963
964 list_for_each_entry_safe(chunk, next, &todo, list) {
965 pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
966 free_pcpu_chunk(chunk);
967 }
789} 968}
790 969
791/** 970/**
792 * free_percpu - free percpu area 971 * free_percpu - free percpu area
793 * @ptr: pointer to area to free 972 * @ptr: pointer to area to free
794 * 973 *
795 * Free percpu area @ptr. Might sleep. 974 * Free percpu area @ptr.
975 *
976 * CONTEXT:
977 * Can be called from atomic context.
796 */ 978 */
797void free_percpu(void *ptr) 979void free_percpu(void *ptr)
798{ 980{
799 void *addr = __pcpu_ptr_to_addr(ptr); 981 void *addr = __pcpu_ptr_to_addr(ptr);
800 struct pcpu_chunk *chunk; 982 struct pcpu_chunk *chunk;
983 unsigned long flags;
801 int off; 984 int off;
802 985
803 if (!ptr) 986 if (!ptr)
804 return; 987 return;
805 988
806 mutex_lock(&pcpu_mutex); 989 spin_lock_irqsave(&pcpu_lock, flags);
807 990
808 chunk = pcpu_chunk_addr_search(addr); 991 chunk = pcpu_chunk_addr_search(addr);
809 off = addr - chunk->vm->addr; 992 off = addr - chunk->vm->addr;
810 993
811 pcpu_free_area(chunk, off); 994 pcpu_free_area(chunk, off);
812 995
813 /* the chunk became fully free, kill one if there are other free ones */ 996 /* if there are more than one fully free chunks, wake up grim reaper */
814 if (chunk->free_size == pcpu_unit_size) { 997 if (chunk->free_size == pcpu_unit_size) {
815 struct pcpu_chunk *pos; 998 struct pcpu_chunk *pos;
816 999
817 list_for_each_entry(pos, 1000 list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
818 &pcpu_slot[pcpu_chunk_slot(chunk)], list)
819 if (pos != chunk) { 1001 if (pos != chunk) {
820 pcpu_kill_chunk(pos); 1002 schedule_work(&pcpu_reclaim_work);
821 break; 1003 break;
822 } 1004 }
823 } 1005 }
824 1006
825 mutex_unlock(&pcpu_mutex); 1007 spin_unlock_irqrestore(&pcpu_lock, flags);
826} 1008}
827EXPORT_SYMBOL_GPL(free_percpu); 1009EXPORT_SYMBOL_GPL(free_percpu);
828 1010
@@ -830,8 +1012,9 @@ EXPORT_SYMBOL_GPL(free_percpu);
830 * pcpu_setup_first_chunk - initialize the first percpu chunk 1012 * pcpu_setup_first_chunk - initialize the first percpu chunk
831 * @get_page_fn: callback to fetch page pointer 1013 * @get_page_fn: callback to fetch page pointer
832 * @static_size: the size of static percpu area in bytes 1014 * @static_size: the size of static percpu area in bytes
833 * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, 0 for auto 1015 * @reserved_size: the size of reserved percpu area in bytes
834 * @free_size: free size in bytes, 0 for auto 1016 * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
1017 * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
835 * @base_addr: mapped address, NULL for auto 1018 * @base_addr: mapped address, NULL for auto
836 * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary 1019 * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
837 * 1020 *
@@ -848,13 +1031,22 @@ EXPORT_SYMBOL_GPL(free_percpu);
848 * indicates end of pages for the cpu. Note that @get_page_fn() must 1031 * indicates end of pages for the cpu. Note that @get_page_fn() must
849 * return the same number of pages for all cpus. 1032 * return the same number of pages for all cpus.
850 * 1033 *
851 * @unit_size, if non-zero, determines unit size and must be aligned 1034 * @reserved_size, if non-zero, specifies the amount of bytes to
852 * to PAGE_SIZE and equal to or larger than @static_size + @free_size. 1035 * reserve after the static area in the first chunk. This reserves
1036 * the first chunk such that it's available only through reserved
1037 * percpu allocation. This is primarily used to serve module percpu
1038 * static areas on architectures where the addressing model has
1039 * limited offset range for symbol relocations to guarantee module
1040 * percpu symbols fall inside the relocatable range.
853 * 1041 *
854 * @free_size determines the number of free bytes after the static 1042 * @unit_size, if non-negative, specifies unit size and must be
855 * area in the first chunk. If zero, whatever left is available. 1043 * aligned to PAGE_SIZE and equal to or larger than @static_size +
856 * Specifying non-zero value make percpu leave the area after 1044 * @reserved_size + @dyn_size.
857 * @static_size + @free_size alone. 1045 *
1046 * @dyn_size, if non-negative, limits the number of bytes available
1047 * for dynamic allocation in the first chunk. Specifying non-negative
1048 * value make percpu leave alone the area beyond @static_size +
1049 * @reserved_size + @dyn_size.
858 * 1050 *
859 * Non-null @base_addr means that the caller already allocated virtual 1051 * Non-null @base_addr means that the caller already allocated virtual
860 * region for the first chunk and mapped it. percpu must not mess 1052 * region for the first chunk and mapped it. percpu must not mess
@@ -864,41 +1056,58 @@ EXPORT_SYMBOL_GPL(free_percpu);
864 * @populate_pte_fn is used to populate the pagetable. NULL means the 1056 * @populate_pte_fn is used to populate the pagetable. NULL means the
865 * caller already populated the pagetable. 1057 * caller already populated the pagetable.
866 * 1058 *
1059 * If the first chunk ends up with both reserved and dynamic areas, it
1060 * is served by two chunks - one to serve the core static and reserved
1061 * areas and the other for the dynamic area. They share the same vm
1062 * and page map but uses different area allocation map to stay away
1063 * from each other. The latter chunk is circulated in the chunk slots
1064 * and available for dynamic allocation like any other chunks.
1065 *
867 * RETURNS: 1066 * RETURNS:
868 * The determined pcpu_unit_size which can be used to initialize 1067 * The determined pcpu_unit_size which can be used to initialize
869 * percpu access. 1068 * percpu access.
870 */ 1069 */
871size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn, 1070size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
872 size_t static_size, size_t unit_size, 1071 size_t static_size, size_t reserved_size,
873 size_t free_size, void *base_addr, 1072 ssize_t unit_size, ssize_t dyn_size,
1073 void *base_addr,
874 pcpu_populate_pte_fn_t populate_pte_fn) 1074 pcpu_populate_pte_fn_t populate_pte_fn)
875{ 1075{
876 static struct vm_struct static_vm; 1076 static struct vm_struct first_vm;
877 struct pcpu_chunk *static_chunk; 1077 static int smap[2], dmap[2];
1078 struct pcpu_chunk *schunk, *dchunk = NULL;
878 unsigned int cpu; 1079 unsigned int cpu;
879 int nr_pages; 1080 int nr_pages;
880 int err, i; 1081 int err, i;
881 1082
882 /* santiy checks */ 1083 /* santiy checks */
1084 BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
1085 ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
883 BUG_ON(!static_size); 1086 BUG_ON(!static_size);
884 BUG_ON(!unit_size && free_size); 1087 if (unit_size >= 0) {
885 BUG_ON(unit_size && unit_size < static_size + free_size); 1088 BUG_ON(unit_size < static_size + reserved_size +
886 BUG_ON(unit_size & ~PAGE_MASK); 1089 (dyn_size >= 0 ? dyn_size : 0));
887 BUG_ON(base_addr && !unit_size); 1090 BUG_ON(unit_size & ~PAGE_MASK);
1091 } else {
1092 BUG_ON(dyn_size >= 0);
1093 BUG_ON(base_addr);
1094 }
888 BUG_ON(base_addr && populate_pte_fn); 1095 BUG_ON(base_addr && populate_pte_fn);
889 1096
890 if (unit_size) 1097 if (unit_size >= 0)
891 pcpu_unit_pages = unit_size >> PAGE_SHIFT; 1098 pcpu_unit_pages = unit_size >> PAGE_SHIFT;
892 else 1099 else
893 pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT, 1100 pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
894 PFN_UP(static_size)); 1101 PFN_UP(static_size + reserved_size));
895 1102
896 pcpu_static_size = static_size;
897 pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; 1103 pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
898 pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size; 1104 pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
899 pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) 1105 pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
900 + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *); 1106 + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
901 1107
1108 if (dyn_size < 0)
1109 dyn_size = pcpu_unit_size - static_size - reserved_size;
1110
902 /* 1111 /*
903 * Allocate chunk slots. The additional last slot is for 1112 * Allocate chunk slots. The additional last slot is for
904 * empty chunks. 1113 * empty chunks.
@@ -908,33 +1117,66 @@ size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
908 for (i = 0; i < pcpu_nr_slots; i++) 1117 for (i = 0; i < pcpu_nr_slots; i++)
909 INIT_LIST_HEAD(&pcpu_slot[i]); 1118 INIT_LIST_HEAD(&pcpu_slot[i]);
910 1119
911 /* init static_chunk */ 1120 /*
912 static_chunk = alloc_bootmem(pcpu_chunk_struct_size); 1121 * Initialize static chunk. If reserved_size is zero, the
913 INIT_LIST_HEAD(&static_chunk->list); 1122 * static chunk covers static area + dynamic allocation area
914 static_chunk->vm = &static_vm; 1123 * in the first chunk. If reserved_size is not zero, it
915 1124 * covers static area + reserved area (mostly used for module
916 if (free_size) 1125 * static percpu allocation).
917 static_chunk->free_size = free_size; 1126 */
918 else 1127 schunk = alloc_bootmem(pcpu_chunk_struct_size);
919 static_chunk->free_size = pcpu_unit_size - pcpu_static_size; 1128 INIT_LIST_HEAD(&schunk->list);
920 1129 schunk->vm = &first_vm;
921 static_chunk->contig_hint = static_chunk->free_size; 1130 schunk->map = smap;
1131 schunk->map_alloc = ARRAY_SIZE(smap);
1132 schunk->page = schunk->page_ar;
1133
1134 if (reserved_size) {
1135 schunk->free_size = reserved_size;
1136 pcpu_reserved_chunk = schunk; /* not for dynamic alloc */
1137 } else {
1138 schunk->free_size = dyn_size;
1139 dyn_size = 0; /* dynamic area covered */
1140 }
1141 schunk->contig_hint = schunk->free_size;
1142
1143 schunk->map[schunk->map_used++] = -static_size;
1144 if (schunk->free_size)
1145 schunk->map[schunk->map_used++] = schunk->free_size;
1146
1147 pcpu_reserved_chunk_limit = static_size + schunk->free_size;
1148
1149 /* init dynamic chunk if necessary */
1150 if (dyn_size) {
1151 dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
1152 INIT_LIST_HEAD(&dchunk->list);
1153 dchunk->vm = &first_vm;
1154 dchunk->map = dmap;
1155 dchunk->map_alloc = ARRAY_SIZE(dmap);
1156 dchunk->page = schunk->page_ar; /* share page map with schunk */
1157
1158 dchunk->contig_hint = dchunk->free_size = dyn_size;
1159 dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
1160 dchunk->map[dchunk->map_used++] = dchunk->free_size;
1161 }
922 1162
923 /* allocate vm address */ 1163 /* allocate vm address */
924 static_vm.flags = VM_ALLOC; 1164 first_vm.flags = VM_ALLOC;
925 static_vm.size = pcpu_chunk_size; 1165 first_vm.size = pcpu_chunk_size;
926 1166
927 if (!base_addr) 1167 if (!base_addr)
928 vm_area_register_early(&static_vm, PAGE_SIZE); 1168 vm_area_register_early(&first_vm, PAGE_SIZE);
929 else { 1169 else {
930 /* 1170 /*
931 * Pages already mapped. No need to remap into 1171 * Pages already mapped. No need to remap into
932 * vmalloc area. In this case the static chunk can't 1172 * vmalloc area. In this case the first chunks can't
933 * be mapped or unmapped by percpu and is marked 1173 * be mapped or unmapped by percpu and are marked
934 * immutable. 1174 * immutable.
935 */ 1175 */
936 static_vm.addr = base_addr; 1176 first_vm.addr = base_addr;
937 static_chunk->immutable = true; 1177 schunk->immutable = true;
1178 if (dchunk)
1179 dchunk->immutable = true;
938 } 1180 }
939 1181
940 /* assign pages */ 1182 /* assign pages */
@@ -945,10 +1187,10 @@ size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
945 1187
946 if (!page) 1188 if (!page)
947 break; 1189 break;
948 *pcpu_chunk_pagep(static_chunk, cpu, i) = page; 1190 *pcpu_chunk_pagep(schunk, cpu, i) = page;
949 } 1191 }
950 1192
951 BUG_ON(i < PFN_UP(pcpu_static_size)); 1193 BUG_ON(i < PFN_UP(static_size));
952 1194
953 if (nr_pages < 0) 1195 if (nr_pages < 0)
954 nr_pages = i; 1196 nr_pages = i;
@@ -960,20 +1202,25 @@ size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
960 if (populate_pte_fn) { 1202 if (populate_pte_fn) {
961 for_each_possible_cpu(cpu) 1203 for_each_possible_cpu(cpu)
962 for (i = 0; i < nr_pages; i++) 1204 for (i = 0; i < nr_pages; i++)
963 populate_pte_fn(pcpu_chunk_addr(static_chunk, 1205 populate_pte_fn(pcpu_chunk_addr(schunk,
964 cpu, i)); 1206 cpu, i));
965 1207
966 err = pcpu_map(static_chunk, 0, nr_pages); 1208 err = pcpu_map(schunk, 0, nr_pages);
967 if (err) 1209 if (err)
968 panic("failed to setup static percpu area, err=%d\n", 1210 panic("failed to setup static percpu area, err=%d\n",
969 err); 1211 err);
970 } 1212 }
971 1213
972 /* link static_chunk in */ 1214 /* link the first chunk in */
973 pcpu_chunk_relocate(static_chunk, -1); 1215 if (!dchunk) {
974 pcpu_chunk_addr_insert(static_chunk); 1216 pcpu_chunk_relocate(schunk, -1);
1217 pcpu_chunk_addr_insert(schunk);
1218 } else {
1219 pcpu_chunk_relocate(dchunk, -1);
1220 pcpu_chunk_addr_insert(dchunk);
1221 }
975 1222
976 /* we're done */ 1223 /* we're done */
977 pcpu_base_addr = (void *)pcpu_chunk_addr(static_chunk, 0, 0); 1224 pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
978 return pcpu_unit_size; 1225 return pcpu_unit_size;
979} 1226}