1fbf59bc9STejun Heo /* 288999a89STejun Heo * mm/percpu.c - percpu memory allocator 3fbf59bc9STejun Heo * 4fbf59bc9STejun Heo * Copyright (C) 2009 SUSE Linux Products GmbH 5fbf59bc9STejun Heo * Copyright (C) 2009 Tejun Heo <tj@kernel.org> 6fbf59bc9STejun Heo * 7fbf59bc9STejun Heo * This file is released under the GPLv2. 8fbf59bc9STejun Heo * 9fbf59bc9STejun Heo * This is percpu allocator which can handle both static and dynamic 1088999a89STejun Heo * areas. Percpu areas are allocated in chunks. Each chunk is 1188999a89STejun Heo * consisted of boot-time determined number of units and the first 1288999a89STejun Heo * chunk is used for static percpu variables in the kernel image 132f39e637STejun Heo * (special boot time alloc/init handling necessary as these areas 142f39e637STejun Heo * need to be brought up before allocation services are running). 152f39e637STejun Heo * Unit grows as necessary and all units grow or shrink in unison. 1688999a89STejun Heo * When a chunk is filled up, another chunk is allocated. 17fbf59bc9STejun Heo * 18fbf59bc9STejun Heo * c0 c1 c2 19fbf59bc9STejun Heo * ------------------- ------------------- ------------ 20fbf59bc9STejun Heo * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u 21fbf59bc9STejun Heo * ------------------- ...... ------------------- .... ------------ 22fbf59bc9STejun Heo * 23fbf59bc9STejun Heo * Allocation is done in offset-size areas of single unit space. Ie, 24fbf59bc9STejun Heo * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0, 252f39e637STejun Heo * c1:u1, c1:u2 and c1:u3. On UMA, units corresponds directly to 262f39e637STejun Heo * cpus. On NUMA, the mapping can be non-linear and even sparse. 272f39e637STejun Heo * Percpu access can be done by configuring percpu base registers 282f39e637STejun Heo * according to cpu to unit mapping and pcpu_unit_size. 29fbf59bc9STejun Heo * 302f39e637STejun Heo * There are usually many small percpu allocations many of them being 312f39e637STejun Heo * as small as 4 bytes. The allocator organizes chunks into lists 32fbf59bc9STejun Heo * according to free size and tries to allocate from the fullest one. 33fbf59bc9STejun Heo * Each chunk keeps the maximum contiguous area size hint which is 344785879eSNamhyung Kim * guaranteed to be equal to or larger than the maximum contiguous 35fbf59bc9STejun Heo * area in the chunk. This helps the allocator not to iterate the 36fbf59bc9STejun Heo * chunk maps unnecessarily. 37fbf59bc9STejun Heo * 38fbf59bc9STejun Heo * Allocation state in each chunk is kept using an array of integers 39fbf59bc9STejun Heo * on chunk->map. A positive value in the map represents a free 40fbf59bc9STejun Heo * region and negative allocated. Allocation inside a chunk is done 41fbf59bc9STejun Heo * by scanning this map sequentially and serving the first matching 42fbf59bc9STejun Heo * entry. This is mostly copied from the percpu_modalloc() allocator. 43e1b9aa3fSChristoph Lameter * Chunks can be determined from the address using the index field 44e1b9aa3fSChristoph Lameter * in the page struct. The index field contains a pointer to the chunk. 45fbf59bc9STejun Heo * 46fbf59bc9STejun Heo * To use this allocator, arch code should do the followings. 47fbf59bc9STejun Heo * 48fbf59bc9STejun Heo * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate 49e0100983STejun Heo * regular address to percpu pointer and back if they need to be 50e0100983STejun Heo * different from the default 51fbf59bc9STejun Heo * 528d408b4bSTejun Heo * - use pcpu_setup_first_chunk() during percpu area initialization to 538d408b4bSTejun Heo * setup the first chunk containing the kernel static percpu area 54fbf59bc9STejun Heo */ 55fbf59bc9STejun Heo 56fbf59bc9STejun Heo #include <linux/bitmap.h> 57fbf59bc9STejun Heo #include <linux/bootmem.h> 58fd1e8a1fSTejun Heo #include <linux/err.h> 59fbf59bc9STejun Heo #include <linux/list.h> 60a530b795STejun Heo #include <linux/log2.h> 61fbf59bc9STejun Heo #include <linux/mm.h> 62fbf59bc9STejun Heo #include <linux/module.h> 63fbf59bc9STejun Heo #include <linux/mutex.h> 64fbf59bc9STejun Heo #include <linux/percpu.h> 65fbf59bc9STejun Heo #include <linux/pfn.h> 66fbf59bc9STejun Heo #include <linux/slab.h> 67ccea34b5STejun Heo #include <linux/spinlock.h> 68fbf59bc9STejun Heo #include <linux/vmalloc.h> 69a56dbddfSTejun Heo #include <linux/workqueue.h> 70fbf59bc9STejun Heo 71fbf59bc9STejun Heo #include <asm/cacheflush.h> 72e0100983STejun Heo #include <asm/sections.h> 73fbf59bc9STejun Heo #include <asm/tlbflush.h> 743b034b0dSVivek Goyal #include <asm/io.h> 75fbf59bc9STejun Heo 76fbf59bc9STejun Heo #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */ 77fbf59bc9STejun Heo #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */ 78fbf59bc9STejun Heo 79bbddff05STejun Heo #ifdef CONFIG_SMP 80e0100983STejun Heo /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */ 81e0100983STejun Heo #ifndef __addr_to_pcpu_ptr 82e0100983STejun Heo #define __addr_to_pcpu_ptr(addr) \ 8343cf38ebSTejun Heo (void __percpu *)((unsigned long)(addr) - \ 8443cf38ebSTejun Heo (unsigned long)pcpu_base_addr + \ 8543cf38ebSTejun Heo (unsigned long)__per_cpu_start) 86e0100983STejun Heo #endif 87e0100983STejun Heo #ifndef __pcpu_ptr_to_addr 88e0100983STejun Heo #define __pcpu_ptr_to_addr(ptr) \ 8943cf38ebSTejun Heo (void __force *)((unsigned long)(ptr) + \ 9043cf38ebSTejun Heo (unsigned long)pcpu_base_addr - \ 9143cf38ebSTejun Heo (unsigned long)__per_cpu_start) 92e0100983STejun Heo #endif 93bbddff05STejun Heo #else /* CONFIG_SMP */ 94bbddff05STejun Heo /* on UP, it's always identity mapped */ 95bbddff05STejun Heo #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr) 96bbddff05STejun Heo #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr) 97bbddff05STejun Heo #endif /* CONFIG_SMP */ 98e0100983STejun Heo 99fbf59bc9STejun Heo struct pcpu_chunk { 100fbf59bc9STejun Heo struct list_head list; /* linked to pcpu_slot lists */ 101fbf59bc9STejun Heo int free_size; /* free bytes in the chunk */ 102fbf59bc9STejun Heo int contig_hint; /* max contiguous size hint */ 103bba174f5STejun Heo void *base_addr; /* base address of this chunk */ 104fbf59bc9STejun Heo int map_used; /* # of map entries used */ 105fbf59bc9STejun Heo int map_alloc; /* # of map entries allocated */ 106fbf59bc9STejun Heo int *map; /* allocation map */ 10788999a89STejun Heo void *data; /* chunk data */ 1088d408b4bSTejun Heo bool immutable; /* no [de]population allowed */ 109ce3141a2STejun Heo unsigned long populated[]; /* populated bitmap */ 110fbf59bc9STejun Heo }; 111fbf59bc9STejun Heo 11240150d37STejun Heo static int pcpu_unit_pages __read_mostly; 11340150d37STejun Heo static int pcpu_unit_size __read_mostly; 1142f39e637STejun Heo static int pcpu_nr_units __read_mostly; 1156563297cSTejun Heo static int pcpu_atom_size __read_mostly; 11640150d37STejun Heo static int pcpu_nr_slots __read_mostly; 11740150d37STejun Heo static size_t pcpu_chunk_struct_size __read_mostly; 118fbf59bc9STejun Heo 1192f39e637STejun Heo /* cpus with the lowest and highest unit numbers */ 1202f39e637STejun Heo static unsigned int pcpu_first_unit_cpu __read_mostly; 1212f39e637STejun Heo static unsigned int pcpu_last_unit_cpu __read_mostly; 1222f39e637STejun Heo 123fbf59bc9STejun Heo /* the address of the first chunk which starts with the kernel static area */ 12440150d37STejun Heo void *pcpu_base_addr __read_mostly; 125fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(pcpu_base_addr); 126fbf59bc9STejun Heo 127fb435d52STejun Heo static const int *pcpu_unit_map __read_mostly; /* cpu -> unit */ 128fb435d52STejun Heo const unsigned long *pcpu_unit_offsets __read_mostly; /* cpu -> unit offset */ 1292f39e637STejun Heo 1306563297cSTejun Heo /* group information, used for vm allocation */ 1316563297cSTejun Heo static int pcpu_nr_groups __read_mostly; 1326563297cSTejun Heo static const unsigned long *pcpu_group_offsets __read_mostly; 1336563297cSTejun Heo static const size_t *pcpu_group_sizes __read_mostly; 1346563297cSTejun Heo 135ae9e6bc9STejun Heo /* 136ae9e6bc9STejun Heo * The first chunk which always exists. Note that unlike other 137ae9e6bc9STejun Heo * chunks, this one can be allocated and mapped in several different 138ae9e6bc9STejun Heo * ways and thus often doesn't live in the vmalloc area. 139ae9e6bc9STejun Heo */ 140ae9e6bc9STejun Heo static struct pcpu_chunk *pcpu_first_chunk; 141ae9e6bc9STejun Heo 142ae9e6bc9STejun Heo /* 143ae9e6bc9STejun Heo * Optional reserved chunk. This chunk reserves part of the first 144ae9e6bc9STejun Heo * chunk and serves it for reserved allocations. The amount of 145ae9e6bc9STejun Heo * reserved offset is in pcpu_reserved_chunk_limit. When reserved 146ae9e6bc9STejun Heo * area doesn't exist, the following variables contain NULL and 0 147ae9e6bc9STejun Heo * respectively. 148ae9e6bc9STejun Heo */ 149edcb4639STejun Heo static struct pcpu_chunk *pcpu_reserved_chunk; 150edcb4639STejun Heo static int pcpu_reserved_chunk_limit; 151edcb4639STejun Heo 152fbf59bc9STejun Heo /* 153ccea34b5STejun Heo * Synchronization rules. 154fbf59bc9STejun Heo * 155ccea34b5STejun Heo * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former 156ce3141a2STejun Heo * protects allocation/reclaim paths, chunks, populated bitmap and 157ce3141a2STejun Heo * vmalloc mapping. The latter is a spinlock and protects the index 158ce3141a2STejun Heo * data structures - chunk slots, chunks and area maps in chunks. 159fbf59bc9STejun Heo * 160ccea34b5STejun Heo * During allocation, pcpu_alloc_mutex is kept locked all the time and 161ccea34b5STejun Heo * pcpu_lock is grabbed and released as necessary. All actual memory 162403a91b1SJiri Kosina * allocations are done using GFP_KERNEL with pcpu_lock released. In 163403a91b1SJiri Kosina * general, percpu memory can't be allocated with irq off but 164403a91b1SJiri Kosina * irqsave/restore are still used in alloc path so that it can be used 165403a91b1SJiri Kosina * from early init path - sched_init() specifically. 166ccea34b5STejun Heo * 167ccea34b5STejun Heo * Free path accesses and alters only the index data structures, so it 168ccea34b5STejun Heo * can be safely called from atomic context. When memory needs to be 169ccea34b5STejun Heo * returned to the system, free path schedules reclaim_work which 170ccea34b5STejun Heo * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be 171ccea34b5STejun Heo * reclaimed, release both locks and frees the chunks. Note that it's 172ccea34b5STejun Heo * necessary to grab both locks to remove a chunk from circulation as 173ccea34b5STejun Heo * allocation path might be referencing the chunk with only 174ccea34b5STejun Heo * pcpu_alloc_mutex locked. 175fbf59bc9STejun Heo */ 176ccea34b5STejun Heo static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */ 177ccea34b5STejun Heo static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */ 178fbf59bc9STejun Heo 17940150d37STejun Heo static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */ 180fbf59bc9STejun Heo 181a56dbddfSTejun Heo /* reclaim work to release fully free chunks, scheduled from free path */ 182a56dbddfSTejun Heo static void pcpu_reclaim(struct work_struct *work); 183a56dbddfSTejun Heo static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim); 184a56dbddfSTejun Heo 185020ec653STejun Heo static bool pcpu_addr_in_first_chunk(void *addr) 186020ec653STejun Heo { 187020ec653STejun Heo void *first_start = pcpu_first_chunk->base_addr; 188020ec653STejun Heo 189020ec653STejun Heo return addr >= first_start && addr < first_start + pcpu_unit_size; 190020ec653STejun Heo } 191020ec653STejun Heo 192020ec653STejun Heo static bool pcpu_addr_in_reserved_chunk(void *addr) 193020ec653STejun Heo { 194020ec653STejun Heo void *first_start = pcpu_first_chunk->base_addr; 195020ec653STejun Heo 196020ec653STejun Heo return addr >= first_start && 197020ec653STejun Heo addr < first_start + pcpu_reserved_chunk_limit; 198020ec653STejun Heo } 199020ec653STejun Heo 200d9b55eebSTejun Heo static int __pcpu_size_to_slot(int size) 201fbf59bc9STejun Heo { 202cae3aeb8STejun Heo int highbit = fls(size); /* size is in bytes */ 203fbf59bc9STejun Heo return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1); 204fbf59bc9STejun Heo } 205fbf59bc9STejun Heo 206d9b55eebSTejun Heo static int pcpu_size_to_slot(int size) 207d9b55eebSTejun Heo { 208d9b55eebSTejun Heo if (size == pcpu_unit_size) 209d9b55eebSTejun Heo return pcpu_nr_slots - 1; 210d9b55eebSTejun Heo return __pcpu_size_to_slot(size); 211d9b55eebSTejun Heo } 212d9b55eebSTejun Heo 213fbf59bc9STejun Heo static int pcpu_chunk_slot(const struct pcpu_chunk *chunk) 214fbf59bc9STejun Heo { 215fbf59bc9STejun Heo if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int)) 216fbf59bc9STejun Heo return 0; 217fbf59bc9STejun Heo 218fbf59bc9STejun Heo return pcpu_size_to_slot(chunk->free_size); 219fbf59bc9STejun Heo } 220fbf59bc9STejun Heo 22188999a89STejun Heo /* set the pointer to a chunk in a page struct */ 22288999a89STejun Heo static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu) 22388999a89STejun Heo { 22488999a89STejun Heo page->index = (unsigned long)pcpu; 22588999a89STejun Heo } 22688999a89STejun Heo 22788999a89STejun Heo /* obtain pointer to a chunk from a page struct */ 22888999a89STejun Heo static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page) 22988999a89STejun Heo { 23088999a89STejun Heo return (struct pcpu_chunk *)page->index; 23188999a89STejun Heo } 23288999a89STejun Heo 23388999a89STejun Heo static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx) 234fbf59bc9STejun Heo { 2352f39e637STejun Heo return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx; 236fbf59bc9STejun Heo } 237fbf59bc9STejun Heo 2389983b6f0STejun Heo static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk, 239fbf59bc9STejun Heo unsigned int cpu, int page_idx) 240fbf59bc9STejun Heo { 241bba174f5STejun Heo return (unsigned long)chunk->base_addr + pcpu_unit_offsets[cpu] + 242fb435d52STejun Heo (page_idx << PAGE_SHIFT); 243fbf59bc9STejun Heo } 244fbf59bc9STejun Heo 24588999a89STejun Heo static void __maybe_unused pcpu_next_unpop(struct pcpu_chunk *chunk, 24688999a89STejun Heo int *rs, int *re, int end) 247ce3141a2STejun Heo { 248ce3141a2STejun Heo *rs = find_next_zero_bit(chunk->populated, end, *rs); 249ce3141a2STejun Heo *re = find_next_bit(chunk->populated, end, *rs + 1); 250ce3141a2STejun Heo } 251ce3141a2STejun Heo 25288999a89STejun Heo static void __maybe_unused pcpu_next_pop(struct pcpu_chunk *chunk, 25388999a89STejun Heo int *rs, int *re, int end) 254ce3141a2STejun Heo { 255ce3141a2STejun Heo *rs = find_next_bit(chunk->populated, end, *rs); 256ce3141a2STejun Heo *re = find_next_zero_bit(chunk->populated, end, *rs + 1); 257ce3141a2STejun Heo } 258ce3141a2STejun Heo 259ce3141a2STejun Heo /* 260ce3141a2STejun Heo * (Un)populated page region iterators. Iterate over (un)populated 261ce3141a2STejun Heo * page regions betwen @start and @end in @chunk. @rs and @re should 262ce3141a2STejun Heo * be integer variables and will be set to start and end page index of 263ce3141a2STejun Heo * the current region. 264ce3141a2STejun Heo */ 265ce3141a2STejun Heo #define pcpu_for_each_unpop_region(chunk, rs, re, start, end) \ 266ce3141a2STejun Heo for ((rs) = (start), pcpu_next_unpop((chunk), &(rs), &(re), (end)); \ 267ce3141a2STejun Heo (rs) < (re); \ 268ce3141a2STejun Heo (rs) = (re) + 1, pcpu_next_unpop((chunk), &(rs), &(re), (end))) 269ce3141a2STejun Heo 270ce3141a2STejun Heo #define pcpu_for_each_pop_region(chunk, rs, re, start, end) \ 271ce3141a2STejun Heo for ((rs) = (start), pcpu_next_pop((chunk), &(rs), &(re), (end)); \ 272ce3141a2STejun Heo (rs) < (re); \ 273ce3141a2STejun Heo (rs) = (re) + 1, pcpu_next_pop((chunk), &(rs), &(re), (end))) 274ce3141a2STejun Heo 275fbf59bc9STejun Heo /** 2761880d93bSTejun Heo * pcpu_mem_alloc - allocate memory 2771880d93bSTejun Heo * @size: bytes to allocate 278fbf59bc9STejun Heo * 2791880d93bSTejun Heo * Allocate @size bytes. If @size is smaller than PAGE_SIZE, 2801880d93bSTejun Heo * kzalloc() is used; otherwise, vmalloc() is used. The returned 2811880d93bSTejun Heo * memory is always zeroed. 282fbf59bc9STejun Heo * 283ccea34b5STejun Heo * CONTEXT: 284ccea34b5STejun Heo * Does GFP_KERNEL allocation. 285ccea34b5STejun Heo * 286fbf59bc9STejun Heo * RETURNS: 2871880d93bSTejun Heo * Pointer to the allocated area on success, NULL on failure. 288fbf59bc9STejun Heo */ 2891880d93bSTejun Heo static void *pcpu_mem_alloc(size_t size) 290fbf59bc9STejun Heo { 291099a19d9STejun Heo if (WARN_ON_ONCE(!slab_is_available())) 292099a19d9STejun Heo return NULL; 293099a19d9STejun Heo 294fbf59bc9STejun Heo if (size <= PAGE_SIZE) 2951880d93bSTejun Heo return kzalloc(size, GFP_KERNEL); 2961880d93bSTejun Heo else { 2971880d93bSTejun Heo void *ptr = vmalloc(size); 2981880d93bSTejun Heo if (ptr) 2991880d93bSTejun Heo memset(ptr, 0, size); 3001880d93bSTejun Heo return ptr; 3011880d93bSTejun Heo } 3021880d93bSTejun Heo } 303fbf59bc9STejun Heo 3041880d93bSTejun Heo /** 3051880d93bSTejun Heo * pcpu_mem_free - free memory 3061880d93bSTejun Heo * @ptr: memory to free 3071880d93bSTejun Heo * @size: size of the area 3081880d93bSTejun Heo * 3091880d93bSTejun Heo * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc(). 3101880d93bSTejun Heo */ 3111880d93bSTejun Heo static void pcpu_mem_free(void *ptr, size_t size) 3121880d93bSTejun Heo { 3131880d93bSTejun Heo if (size <= PAGE_SIZE) 3141880d93bSTejun Heo kfree(ptr); 3151880d93bSTejun Heo else 3161880d93bSTejun Heo vfree(ptr); 317fbf59bc9STejun Heo } 318fbf59bc9STejun Heo 319fbf59bc9STejun Heo /** 320fbf59bc9STejun Heo * pcpu_chunk_relocate - put chunk in the appropriate chunk slot 321fbf59bc9STejun Heo * @chunk: chunk of interest 322fbf59bc9STejun Heo * @oslot: the previous slot it was on 323fbf59bc9STejun Heo * 324fbf59bc9STejun Heo * This function is called after an allocation or free changed @chunk. 325fbf59bc9STejun Heo * New slot according to the changed state is determined and @chunk is 326edcb4639STejun Heo * moved to the slot. Note that the reserved chunk is never put on 327edcb4639STejun Heo * chunk slots. 328ccea34b5STejun Heo * 329ccea34b5STejun Heo * CONTEXT: 330ccea34b5STejun Heo * pcpu_lock. 331fbf59bc9STejun Heo */ 332fbf59bc9STejun Heo static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) 333fbf59bc9STejun Heo { 334fbf59bc9STejun Heo int nslot = pcpu_chunk_slot(chunk); 335fbf59bc9STejun Heo 336edcb4639STejun Heo if (chunk != pcpu_reserved_chunk && oslot != nslot) { 337fbf59bc9STejun Heo if (oslot < nslot) 338fbf59bc9STejun Heo list_move(&chunk->list, &pcpu_slot[nslot]); 339fbf59bc9STejun Heo else 340fbf59bc9STejun Heo list_move_tail(&chunk->list, &pcpu_slot[nslot]); 341fbf59bc9STejun Heo } 342fbf59bc9STejun Heo } 343fbf59bc9STejun Heo 344fbf59bc9STejun Heo /** 345833af842STejun Heo * pcpu_need_to_extend - determine whether chunk area map needs to be extended 346833af842STejun Heo * @chunk: chunk of interest 3479f7dcf22STejun Heo * 348833af842STejun Heo * Determine whether area map of @chunk needs to be extended to 349833af842STejun Heo * accomodate a new allocation. 3509f7dcf22STejun Heo * 351ccea34b5STejun Heo * CONTEXT: 352833af842STejun Heo * pcpu_lock. 353ccea34b5STejun Heo * 3549f7dcf22STejun Heo * RETURNS: 355833af842STejun Heo * New target map allocation length if extension is necessary, 0 356833af842STejun Heo * otherwise. 3579f7dcf22STejun Heo */ 358833af842STejun Heo static int pcpu_need_to_extend(struct pcpu_chunk *chunk) 3599f7dcf22STejun Heo { 3609f7dcf22STejun Heo int new_alloc; 3619f7dcf22STejun Heo 3629f7dcf22STejun Heo if (chunk->map_alloc >= chunk->map_used + 2) 3639f7dcf22STejun Heo return 0; 3649f7dcf22STejun Heo 3659f7dcf22STejun Heo new_alloc = PCPU_DFL_MAP_ALLOC; 3669f7dcf22STejun Heo while (new_alloc < chunk->map_used + 2) 3679f7dcf22STejun Heo new_alloc *= 2; 3689f7dcf22STejun Heo 369833af842STejun Heo return new_alloc; 370ccea34b5STejun Heo } 371ccea34b5STejun Heo 372833af842STejun Heo /** 373833af842STejun Heo * pcpu_extend_area_map - extend area map of a chunk 374833af842STejun Heo * @chunk: chunk of interest 375833af842STejun Heo * @new_alloc: new target allocation length of the area map 376833af842STejun Heo * 377833af842STejun Heo * Extend area map of @chunk to have @new_alloc entries. 378833af842STejun Heo * 379833af842STejun Heo * CONTEXT: 380833af842STejun Heo * Does GFP_KERNEL allocation. Grabs and releases pcpu_lock. 381833af842STejun Heo * 382833af842STejun Heo * RETURNS: 383833af842STejun Heo * 0 on success, -errno on failure. 384ccea34b5STejun Heo */ 385833af842STejun Heo static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc) 386833af842STejun Heo { 387833af842STejun Heo int *old = NULL, *new = NULL; 388833af842STejun Heo size_t old_size = 0, new_size = new_alloc * sizeof(new[0]); 389833af842STejun Heo unsigned long flags; 3909f7dcf22STejun Heo 391833af842STejun Heo new = pcpu_mem_alloc(new_size); 392833af842STejun Heo if (!new) 393833af842STejun Heo return -ENOMEM; 394833af842STejun Heo 395833af842STejun Heo /* acquire pcpu_lock and switch to new area map */ 396833af842STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 397833af842STejun Heo 398833af842STejun Heo if (new_alloc <= chunk->map_alloc) 399833af842STejun Heo goto out_unlock; 400833af842STejun Heo 401833af842STejun Heo old_size = chunk->map_alloc * sizeof(chunk->map[0]); 402a002d148SHuang Shijie old = chunk->map; 403a002d148SHuang Shijie 404a002d148SHuang Shijie memcpy(new, old, old_size); 4059f7dcf22STejun Heo 4069f7dcf22STejun Heo chunk->map_alloc = new_alloc; 4079f7dcf22STejun Heo chunk->map = new; 408833af842STejun Heo new = NULL; 409833af842STejun Heo 410833af842STejun Heo out_unlock: 411833af842STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 412833af842STejun Heo 413833af842STejun Heo /* 414833af842STejun Heo * pcpu_mem_free() might end up calling vfree() which uses 415833af842STejun Heo * IRQ-unsafe lock and thus can't be called under pcpu_lock. 416833af842STejun Heo */ 417833af842STejun Heo pcpu_mem_free(old, old_size); 418833af842STejun Heo pcpu_mem_free(new, new_size); 419833af842STejun Heo 4209f7dcf22STejun Heo return 0; 4219f7dcf22STejun Heo } 4229f7dcf22STejun Heo 4239f7dcf22STejun Heo /** 424fbf59bc9STejun Heo * pcpu_split_block - split a map block 425fbf59bc9STejun Heo * @chunk: chunk of interest 426fbf59bc9STejun Heo * @i: index of map block to split 427cae3aeb8STejun Heo * @head: head size in bytes (can be 0) 428cae3aeb8STejun Heo * @tail: tail size in bytes (can be 0) 429fbf59bc9STejun Heo * 430fbf59bc9STejun Heo * Split the @i'th map block into two or three blocks. If @head is 431fbf59bc9STejun Heo * non-zero, @head bytes block is inserted before block @i moving it 432fbf59bc9STejun Heo * to @i+1 and reducing its size by @head bytes. 433fbf59bc9STejun Heo * 434fbf59bc9STejun Heo * If @tail is non-zero, the target block, which can be @i or @i+1 435fbf59bc9STejun Heo * depending on @head, is reduced by @tail bytes and @tail byte block 436fbf59bc9STejun Heo * is inserted after the target block. 437fbf59bc9STejun Heo * 4389f7dcf22STejun Heo * @chunk->map must have enough free slots to accomodate the split. 439ccea34b5STejun Heo * 440ccea34b5STejun Heo * CONTEXT: 441ccea34b5STejun Heo * pcpu_lock. 442fbf59bc9STejun Heo */ 4439f7dcf22STejun Heo static void pcpu_split_block(struct pcpu_chunk *chunk, int i, 4449f7dcf22STejun Heo int head, int tail) 445fbf59bc9STejun Heo { 446fbf59bc9STejun Heo int nr_extra = !!head + !!tail; 447fbf59bc9STejun Heo 4489f7dcf22STejun Heo BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra); 449fbf59bc9STejun Heo 4509f7dcf22STejun Heo /* insert new subblocks */ 451fbf59bc9STejun Heo memmove(&chunk->map[i + nr_extra], &chunk->map[i], 452fbf59bc9STejun Heo sizeof(chunk->map[0]) * (chunk->map_used - i)); 453fbf59bc9STejun Heo chunk->map_used += nr_extra; 454fbf59bc9STejun Heo 455fbf59bc9STejun Heo if (head) { 456fbf59bc9STejun Heo chunk->map[i + 1] = chunk->map[i] - head; 457fbf59bc9STejun Heo chunk->map[i++] = head; 458fbf59bc9STejun Heo } 459fbf59bc9STejun Heo if (tail) { 460fbf59bc9STejun Heo chunk->map[i++] -= tail; 461fbf59bc9STejun Heo chunk->map[i] = tail; 462fbf59bc9STejun Heo } 463fbf59bc9STejun Heo } 464fbf59bc9STejun Heo 465fbf59bc9STejun Heo /** 466fbf59bc9STejun Heo * pcpu_alloc_area - allocate area from a pcpu_chunk 467fbf59bc9STejun Heo * @chunk: chunk of interest 468cae3aeb8STejun Heo * @size: wanted size in bytes 469fbf59bc9STejun Heo * @align: wanted align 470fbf59bc9STejun Heo * 471fbf59bc9STejun Heo * Try to allocate @size bytes area aligned at @align from @chunk. 472fbf59bc9STejun Heo * Note that this function only allocates the offset. It doesn't 473fbf59bc9STejun Heo * populate or map the area. 474fbf59bc9STejun Heo * 4759f7dcf22STejun Heo * @chunk->map must have at least two free slots. 4769f7dcf22STejun Heo * 477ccea34b5STejun Heo * CONTEXT: 478ccea34b5STejun Heo * pcpu_lock. 479ccea34b5STejun Heo * 480fbf59bc9STejun Heo * RETURNS: 4819f7dcf22STejun Heo * Allocated offset in @chunk on success, -1 if no matching area is 4829f7dcf22STejun Heo * found. 483fbf59bc9STejun Heo */ 484fbf59bc9STejun Heo static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align) 485fbf59bc9STejun Heo { 486fbf59bc9STejun Heo int oslot = pcpu_chunk_slot(chunk); 487fbf59bc9STejun Heo int max_contig = 0; 488fbf59bc9STejun Heo int i, off; 489fbf59bc9STejun Heo 490fbf59bc9STejun Heo for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) { 491fbf59bc9STejun Heo bool is_last = i + 1 == chunk->map_used; 492fbf59bc9STejun Heo int head, tail; 493fbf59bc9STejun Heo 494fbf59bc9STejun Heo /* extra for alignment requirement */ 495fbf59bc9STejun Heo head = ALIGN(off, align) - off; 496fbf59bc9STejun Heo BUG_ON(i == 0 && head != 0); 497fbf59bc9STejun Heo 498fbf59bc9STejun Heo if (chunk->map[i] < 0) 499fbf59bc9STejun Heo continue; 500fbf59bc9STejun Heo if (chunk->map[i] < head + size) { 501fbf59bc9STejun Heo max_contig = max(chunk->map[i], max_contig); 502fbf59bc9STejun Heo continue; 503fbf59bc9STejun Heo } 504fbf59bc9STejun Heo 505fbf59bc9STejun Heo /* 506fbf59bc9STejun Heo * If head is small or the previous block is free, 507fbf59bc9STejun Heo * merge'em. Note that 'small' is defined as smaller 508fbf59bc9STejun Heo * than sizeof(int), which is very small but isn't too 509fbf59bc9STejun Heo * uncommon for percpu allocations. 510fbf59bc9STejun Heo */ 511fbf59bc9STejun Heo if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) { 512fbf59bc9STejun Heo if (chunk->map[i - 1] > 0) 513fbf59bc9STejun Heo chunk->map[i - 1] += head; 514fbf59bc9STejun Heo else { 515fbf59bc9STejun Heo chunk->map[i - 1] -= head; 516fbf59bc9STejun Heo chunk->free_size -= head; 517fbf59bc9STejun Heo } 518fbf59bc9STejun Heo chunk->map[i] -= head; 519fbf59bc9STejun Heo off += head; 520fbf59bc9STejun Heo head = 0; 521fbf59bc9STejun Heo } 522fbf59bc9STejun Heo 523fbf59bc9STejun Heo /* if tail is small, just keep it around */ 524fbf59bc9STejun Heo tail = chunk->map[i] - head - size; 525fbf59bc9STejun Heo if (tail < sizeof(int)) 526fbf59bc9STejun Heo tail = 0; 527fbf59bc9STejun Heo 528fbf59bc9STejun Heo /* split if warranted */ 529fbf59bc9STejun Heo if (head || tail) { 5309f7dcf22STejun Heo pcpu_split_block(chunk, i, head, tail); 531fbf59bc9STejun Heo if (head) { 532fbf59bc9STejun Heo i++; 533fbf59bc9STejun Heo off += head; 534fbf59bc9STejun Heo max_contig = max(chunk->map[i - 1], max_contig); 535fbf59bc9STejun Heo } 536fbf59bc9STejun Heo if (tail) 537fbf59bc9STejun Heo max_contig = max(chunk->map[i + 1], max_contig); 538fbf59bc9STejun Heo } 539fbf59bc9STejun Heo 540fbf59bc9STejun Heo /* update hint and mark allocated */ 541fbf59bc9STejun Heo if (is_last) 542fbf59bc9STejun Heo chunk->contig_hint = max_contig; /* fully scanned */ 543fbf59bc9STejun Heo else 544fbf59bc9STejun Heo chunk->contig_hint = max(chunk->contig_hint, 545fbf59bc9STejun Heo max_contig); 546fbf59bc9STejun Heo 547fbf59bc9STejun Heo chunk->free_size -= chunk->map[i]; 548fbf59bc9STejun Heo chunk->map[i] = -chunk->map[i]; 549fbf59bc9STejun Heo 550fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 551fbf59bc9STejun Heo return off; 552fbf59bc9STejun Heo } 553fbf59bc9STejun Heo 554fbf59bc9STejun Heo chunk->contig_hint = max_contig; /* fully scanned */ 555fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 556fbf59bc9STejun Heo 5579f7dcf22STejun Heo /* tell the upper layer that this chunk has no matching area */ 5589f7dcf22STejun Heo return -1; 559fbf59bc9STejun Heo } 560fbf59bc9STejun Heo 561fbf59bc9STejun Heo /** 562fbf59bc9STejun Heo * pcpu_free_area - free area to a pcpu_chunk 563fbf59bc9STejun Heo * @chunk: chunk of interest 564fbf59bc9STejun Heo * @freeme: offset of area to free 565fbf59bc9STejun Heo * 566fbf59bc9STejun Heo * Free area starting from @freeme to @chunk. Note that this function 567fbf59bc9STejun Heo * only modifies the allocation map. It doesn't depopulate or unmap 568fbf59bc9STejun Heo * the area. 569ccea34b5STejun Heo * 570ccea34b5STejun Heo * CONTEXT: 571ccea34b5STejun Heo * pcpu_lock. 572fbf59bc9STejun Heo */ 573fbf59bc9STejun Heo static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme) 574fbf59bc9STejun Heo { 575fbf59bc9STejun Heo int oslot = pcpu_chunk_slot(chunk); 576fbf59bc9STejun Heo int i, off; 577fbf59bc9STejun Heo 578fbf59bc9STejun Heo for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) 579fbf59bc9STejun Heo if (off == freeme) 580fbf59bc9STejun Heo break; 581fbf59bc9STejun Heo BUG_ON(off != freeme); 582fbf59bc9STejun Heo BUG_ON(chunk->map[i] > 0); 583fbf59bc9STejun Heo 584fbf59bc9STejun Heo chunk->map[i] = -chunk->map[i]; 585fbf59bc9STejun Heo chunk->free_size += chunk->map[i]; 586fbf59bc9STejun Heo 587fbf59bc9STejun Heo /* merge with previous? */ 588fbf59bc9STejun Heo if (i > 0 && chunk->map[i - 1] >= 0) { 589fbf59bc9STejun Heo chunk->map[i - 1] += chunk->map[i]; 590fbf59bc9STejun Heo chunk->map_used--; 591fbf59bc9STejun Heo memmove(&chunk->map[i], &chunk->map[i + 1], 592fbf59bc9STejun Heo (chunk->map_used - i) * sizeof(chunk->map[0])); 593fbf59bc9STejun Heo i--; 594fbf59bc9STejun Heo } 595fbf59bc9STejun Heo /* merge with next? */ 596fbf59bc9STejun Heo if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) { 597fbf59bc9STejun Heo chunk->map[i] += chunk->map[i + 1]; 598fbf59bc9STejun Heo chunk->map_used--; 599fbf59bc9STejun Heo memmove(&chunk->map[i + 1], &chunk->map[i + 2], 600fbf59bc9STejun Heo (chunk->map_used - (i + 1)) * sizeof(chunk->map[0])); 601fbf59bc9STejun Heo } 602fbf59bc9STejun Heo 603fbf59bc9STejun Heo chunk->contig_hint = max(chunk->map[i], chunk->contig_hint); 604fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 605fbf59bc9STejun Heo } 606fbf59bc9STejun Heo 6076081089fSTejun Heo static struct pcpu_chunk *pcpu_alloc_chunk(void) 6086081089fSTejun Heo { 6096081089fSTejun Heo struct pcpu_chunk *chunk; 6106081089fSTejun Heo 611099a19d9STejun Heo chunk = pcpu_mem_alloc(pcpu_chunk_struct_size); 6126081089fSTejun Heo if (!chunk) 6136081089fSTejun Heo return NULL; 6146081089fSTejun Heo 6156081089fSTejun Heo chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0])); 6166081089fSTejun Heo if (!chunk->map) { 6176081089fSTejun Heo kfree(chunk); 6186081089fSTejun Heo return NULL; 6196081089fSTejun Heo } 6206081089fSTejun Heo 6216081089fSTejun Heo chunk->map_alloc = PCPU_DFL_MAP_ALLOC; 6226081089fSTejun Heo chunk->map[chunk->map_used++] = pcpu_unit_size; 6236081089fSTejun Heo 6246081089fSTejun Heo INIT_LIST_HEAD(&chunk->list); 6256081089fSTejun Heo chunk->free_size = pcpu_unit_size; 6266081089fSTejun Heo chunk->contig_hint = pcpu_unit_size; 6276081089fSTejun Heo 6286081089fSTejun Heo return chunk; 6296081089fSTejun Heo } 6306081089fSTejun Heo 6316081089fSTejun Heo static void pcpu_free_chunk(struct pcpu_chunk *chunk) 6326081089fSTejun Heo { 6336081089fSTejun Heo if (!chunk) 6346081089fSTejun Heo return; 6356081089fSTejun Heo pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0])); 6366081089fSTejun Heo kfree(chunk); 6376081089fSTejun Heo } 6386081089fSTejun Heo 639fbf59bc9STejun Heo /* 6409f645532STejun Heo * Chunk management implementation. 641fbf59bc9STejun Heo * 6429f645532STejun Heo * To allow different implementations, chunk alloc/free and 6439f645532STejun Heo * [de]population are implemented in a separate file which is pulled 6449f645532STejun Heo * into this file and compiled together. The following functions 6459f645532STejun Heo * should be implemented. 646ccea34b5STejun Heo * 6479f645532STejun Heo * pcpu_populate_chunk - populate the specified range of a chunk 6489f645532STejun Heo * pcpu_depopulate_chunk - depopulate the specified range of a chunk 6499f645532STejun Heo * pcpu_create_chunk - create a new chunk 6509f645532STejun Heo * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop 6519f645532STejun Heo * pcpu_addr_to_page - translate address to physical address 6529f645532STejun Heo * pcpu_verify_alloc_info - check alloc_info is acceptable during init 653fbf59bc9STejun Heo */ 6549f645532STejun Heo static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size); 6559f645532STejun Heo static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size); 6569f645532STejun Heo static struct pcpu_chunk *pcpu_create_chunk(void); 6579f645532STejun Heo static void pcpu_destroy_chunk(struct pcpu_chunk *chunk); 6589f645532STejun Heo static struct page *pcpu_addr_to_page(void *addr); 6599f645532STejun Heo static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai); 660fbf59bc9STejun Heo 661b0c9778bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_KM 662b0c9778bSTejun Heo #include "percpu-km.c" 663b0c9778bSTejun Heo #else 6649f645532STejun Heo #include "percpu-vm.c" 665b0c9778bSTejun Heo #endif 666fbf59bc9STejun Heo 667fbf59bc9STejun Heo /** 66888999a89STejun Heo * pcpu_chunk_addr_search - determine chunk containing specified address 66988999a89STejun Heo * @addr: address for which the chunk needs to be determined. 67088999a89STejun Heo * 67188999a89STejun Heo * RETURNS: 67288999a89STejun Heo * The address of the found chunk. 67388999a89STejun Heo */ 67488999a89STejun Heo static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr) 67588999a89STejun Heo { 67688999a89STejun Heo /* is it in the first chunk? */ 67788999a89STejun Heo if (pcpu_addr_in_first_chunk(addr)) { 67888999a89STejun Heo /* is it in the reserved area? */ 67988999a89STejun Heo if (pcpu_addr_in_reserved_chunk(addr)) 68088999a89STejun Heo return pcpu_reserved_chunk; 68188999a89STejun Heo return pcpu_first_chunk; 68288999a89STejun Heo } 68388999a89STejun Heo 68488999a89STejun Heo /* 68588999a89STejun Heo * The address is relative to unit0 which might be unused and 68688999a89STejun Heo * thus unmapped. Offset the address to the unit space of the 68788999a89STejun Heo * current processor before looking it up in the vmalloc 68888999a89STejun Heo * space. Note that any possible cpu id can be used here, so 68988999a89STejun Heo * there's no need to worry about preemption or cpu hotplug. 69088999a89STejun Heo */ 69188999a89STejun Heo addr += pcpu_unit_offsets[raw_smp_processor_id()]; 6929f645532STejun Heo return pcpu_get_page_chunk(pcpu_addr_to_page(addr)); 69388999a89STejun Heo } 69488999a89STejun Heo 69588999a89STejun Heo /** 696edcb4639STejun Heo * pcpu_alloc - the percpu allocator 697cae3aeb8STejun Heo * @size: size of area to allocate in bytes 698fbf59bc9STejun Heo * @align: alignment of area (max PAGE_SIZE) 699edcb4639STejun Heo * @reserved: allocate from the reserved chunk if available 700fbf59bc9STejun Heo * 701ccea34b5STejun Heo * Allocate percpu area of @size bytes aligned at @align. 702ccea34b5STejun Heo * 703ccea34b5STejun Heo * CONTEXT: 704ccea34b5STejun Heo * Does GFP_KERNEL allocation. 705fbf59bc9STejun Heo * 706fbf59bc9STejun Heo * RETURNS: 707fbf59bc9STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 708fbf59bc9STejun Heo */ 70943cf38ebSTejun Heo static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved) 710fbf59bc9STejun Heo { 711f2badb0cSTejun Heo static int warn_limit = 10; 712fbf59bc9STejun Heo struct pcpu_chunk *chunk; 713f2badb0cSTejun Heo const char *err; 714833af842STejun Heo int slot, off, new_alloc; 715403a91b1SJiri Kosina unsigned long flags; 716fbf59bc9STejun Heo 7178d408b4bSTejun Heo if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) { 718fbf59bc9STejun Heo WARN(true, "illegal size (%zu) or align (%zu) for " 719fbf59bc9STejun Heo "percpu allocation\n", size, align); 720fbf59bc9STejun Heo return NULL; 721fbf59bc9STejun Heo } 722fbf59bc9STejun Heo 723ccea34b5STejun Heo mutex_lock(&pcpu_alloc_mutex); 724403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 725fbf59bc9STejun Heo 726edcb4639STejun Heo /* serve reserved allocations from the reserved chunk if available */ 727edcb4639STejun Heo if (reserved && pcpu_reserved_chunk) { 728edcb4639STejun Heo chunk = pcpu_reserved_chunk; 729833af842STejun Heo 730833af842STejun Heo if (size > chunk->contig_hint) { 731833af842STejun Heo err = "alloc from reserved chunk failed"; 732ccea34b5STejun Heo goto fail_unlock; 733f2badb0cSTejun Heo } 734833af842STejun Heo 735833af842STejun Heo while ((new_alloc = pcpu_need_to_extend(chunk))) { 736833af842STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 737833af842STejun Heo if (pcpu_extend_area_map(chunk, new_alloc) < 0) { 738833af842STejun Heo err = "failed to extend area map of reserved chunk"; 739833af842STejun Heo goto fail_unlock_mutex; 740833af842STejun Heo } 741833af842STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 742833af842STejun Heo } 743833af842STejun Heo 744edcb4639STejun Heo off = pcpu_alloc_area(chunk, size, align); 745edcb4639STejun Heo if (off >= 0) 746edcb4639STejun Heo goto area_found; 747833af842STejun Heo 748f2badb0cSTejun Heo err = "alloc from reserved chunk failed"; 749ccea34b5STejun Heo goto fail_unlock; 750edcb4639STejun Heo } 751edcb4639STejun Heo 752ccea34b5STejun Heo restart: 753edcb4639STejun Heo /* search through normal chunks */ 754fbf59bc9STejun Heo for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) { 755fbf59bc9STejun Heo list_for_each_entry(chunk, &pcpu_slot[slot], list) { 756fbf59bc9STejun Heo if (size > chunk->contig_hint) 757fbf59bc9STejun Heo continue; 758ccea34b5STejun Heo 759833af842STejun Heo new_alloc = pcpu_need_to_extend(chunk); 760833af842STejun Heo if (new_alloc) { 761833af842STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 762833af842STejun Heo if (pcpu_extend_area_map(chunk, 763833af842STejun Heo new_alloc) < 0) { 764f2badb0cSTejun Heo err = "failed to extend area map"; 765833af842STejun Heo goto fail_unlock_mutex; 766833af842STejun Heo } 767833af842STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 768833af842STejun Heo /* 769833af842STejun Heo * pcpu_lock has been dropped, need to 770833af842STejun Heo * restart cpu_slot list walking. 771833af842STejun Heo */ 772833af842STejun Heo goto restart; 773ccea34b5STejun Heo } 774ccea34b5STejun Heo 775fbf59bc9STejun Heo off = pcpu_alloc_area(chunk, size, align); 776fbf59bc9STejun Heo if (off >= 0) 777fbf59bc9STejun Heo goto area_found; 778fbf59bc9STejun Heo } 779fbf59bc9STejun Heo } 780fbf59bc9STejun Heo 781fbf59bc9STejun Heo /* hmmm... no space left, create a new chunk */ 782403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 783ccea34b5STejun Heo 7846081089fSTejun Heo chunk = pcpu_create_chunk(); 785f2badb0cSTejun Heo if (!chunk) { 786f2badb0cSTejun Heo err = "failed to allocate new chunk"; 787ccea34b5STejun Heo goto fail_unlock_mutex; 788f2badb0cSTejun Heo } 789ccea34b5STejun Heo 790403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 791fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, -1); 792ccea34b5STejun Heo goto restart; 793fbf59bc9STejun Heo 794fbf59bc9STejun Heo area_found: 795403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 796ccea34b5STejun Heo 797fbf59bc9STejun Heo /* populate, map and clear the area */ 798fbf59bc9STejun Heo if (pcpu_populate_chunk(chunk, off, size)) { 799403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 800fbf59bc9STejun Heo pcpu_free_area(chunk, off); 801f2badb0cSTejun Heo err = "failed to populate"; 802ccea34b5STejun Heo goto fail_unlock; 803fbf59bc9STejun Heo } 804fbf59bc9STejun Heo 805ccea34b5STejun Heo mutex_unlock(&pcpu_alloc_mutex); 806ccea34b5STejun Heo 807bba174f5STejun Heo /* return address relative to base address */ 808bba174f5STejun Heo return __addr_to_pcpu_ptr(chunk->base_addr + off); 809ccea34b5STejun Heo 810ccea34b5STejun Heo fail_unlock: 811403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 812ccea34b5STejun Heo fail_unlock_mutex: 813ccea34b5STejun Heo mutex_unlock(&pcpu_alloc_mutex); 814f2badb0cSTejun Heo if (warn_limit) { 815f2badb0cSTejun Heo pr_warning("PERCPU: allocation failed, size=%zu align=%zu, " 816f2badb0cSTejun Heo "%s\n", size, align, err); 817f2badb0cSTejun Heo dump_stack(); 818f2badb0cSTejun Heo if (!--warn_limit) 819f2badb0cSTejun Heo pr_info("PERCPU: limit reached, disable warning\n"); 820f2badb0cSTejun Heo } 821ccea34b5STejun Heo return NULL; 822fbf59bc9STejun Heo } 823edcb4639STejun Heo 824edcb4639STejun Heo /** 825edcb4639STejun Heo * __alloc_percpu - allocate dynamic percpu area 826edcb4639STejun Heo * @size: size of area to allocate in bytes 827edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 828edcb4639STejun Heo * 8299329ba97STejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align. 8309329ba97STejun Heo * Might sleep. Might trigger writeouts. 831edcb4639STejun Heo * 832ccea34b5STejun Heo * CONTEXT: 833ccea34b5STejun Heo * Does GFP_KERNEL allocation. 834ccea34b5STejun Heo * 835edcb4639STejun Heo * RETURNS: 836edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 837edcb4639STejun Heo */ 83843cf38ebSTejun Heo void __percpu *__alloc_percpu(size_t size, size_t align) 839edcb4639STejun Heo { 840edcb4639STejun Heo return pcpu_alloc(size, align, false); 841edcb4639STejun Heo } 842fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu); 843fbf59bc9STejun Heo 844edcb4639STejun Heo /** 845edcb4639STejun Heo * __alloc_reserved_percpu - allocate reserved percpu area 846edcb4639STejun Heo * @size: size of area to allocate in bytes 847edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 848edcb4639STejun Heo * 8499329ba97STejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align 8509329ba97STejun Heo * from reserved percpu area if arch has set it up; otherwise, 8519329ba97STejun Heo * allocation is served from the same dynamic area. Might sleep. 8529329ba97STejun Heo * Might trigger writeouts. 853edcb4639STejun Heo * 854ccea34b5STejun Heo * CONTEXT: 855ccea34b5STejun Heo * Does GFP_KERNEL allocation. 856ccea34b5STejun Heo * 857edcb4639STejun Heo * RETURNS: 858edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 859edcb4639STejun Heo */ 86043cf38ebSTejun Heo void __percpu *__alloc_reserved_percpu(size_t size, size_t align) 861edcb4639STejun Heo { 862edcb4639STejun Heo return pcpu_alloc(size, align, true); 863edcb4639STejun Heo } 864edcb4639STejun Heo 865a56dbddfSTejun Heo /** 866a56dbddfSTejun Heo * pcpu_reclaim - reclaim fully free chunks, workqueue function 867a56dbddfSTejun Heo * @work: unused 868a56dbddfSTejun Heo * 869a56dbddfSTejun Heo * Reclaim all fully free chunks except for the first one. 870ccea34b5STejun Heo * 871ccea34b5STejun Heo * CONTEXT: 872ccea34b5STejun Heo * workqueue context. 873a56dbddfSTejun Heo */ 874a56dbddfSTejun Heo static void pcpu_reclaim(struct work_struct *work) 875fbf59bc9STejun Heo { 876a56dbddfSTejun Heo LIST_HEAD(todo); 877a56dbddfSTejun Heo struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1]; 878a56dbddfSTejun Heo struct pcpu_chunk *chunk, *next; 879a56dbddfSTejun Heo 880ccea34b5STejun Heo mutex_lock(&pcpu_alloc_mutex); 881ccea34b5STejun Heo spin_lock_irq(&pcpu_lock); 882a56dbddfSTejun Heo 883a56dbddfSTejun Heo list_for_each_entry_safe(chunk, next, head, list) { 8848d408b4bSTejun Heo WARN_ON(chunk->immutable); 885a56dbddfSTejun Heo 886a56dbddfSTejun Heo /* spare the first one */ 887a56dbddfSTejun Heo if (chunk == list_first_entry(head, struct pcpu_chunk, list)) 888a56dbddfSTejun Heo continue; 889a56dbddfSTejun Heo 890a56dbddfSTejun Heo list_move(&chunk->list, &todo); 891a56dbddfSTejun Heo } 892a56dbddfSTejun Heo 893ccea34b5STejun Heo spin_unlock_irq(&pcpu_lock); 894a56dbddfSTejun Heo 895a56dbddfSTejun Heo list_for_each_entry_safe(chunk, next, &todo, list) { 896ce3141a2STejun Heo pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size); 8976081089fSTejun Heo pcpu_destroy_chunk(chunk); 898fbf59bc9STejun Heo } 899971f3918STejun Heo 900971f3918STejun Heo mutex_unlock(&pcpu_alloc_mutex); 901a56dbddfSTejun Heo } 902fbf59bc9STejun Heo 903fbf59bc9STejun Heo /** 904fbf59bc9STejun Heo * free_percpu - free percpu area 905fbf59bc9STejun Heo * @ptr: pointer to area to free 906fbf59bc9STejun Heo * 907ccea34b5STejun Heo * Free percpu area @ptr. 908ccea34b5STejun Heo * 909ccea34b5STejun Heo * CONTEXT: 910ccea34b5STejun Heo * Can be called from atomic context. 911fbf59bc9STejun Heo */ 91243cf38ebSTejun Heo void free_percpu(void __percpu *ptr) 913fbf59bc9STejun Heo { 914129182e5SAndrew Morton void *addr; 915fbf59bc9STejun Heo struct pcpu_chunk *chunk; 916ccea34b5STejun Heo unsigned long flags; 917fbf59bc9STejun Heo int off; 918fbf59bc9STejun Heo 919fbf59bc9STejun Heo if (!ptr) 920fbf59bc9STejun Heo return; 921fbf59bc9STejun Heo 922129182e5SAndrew Morton addr = __pcpu_ptr_to_addr(ptr); 923129182e5SAndrew Morton 924ccea34b5STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 925fbf59bc9STejun Heo 926fbf59bc9STejun Heo chunk = pcpu_chunk_addr_search(addr); 927bba174f5STejun Heo off = addr - chunk->base_addr; 928fbf59bc9STejun Heo 929fbf59bc9STejun Heo pcpu_free_area(chunk, off); 930fbf59bc9STejun Heo 931a56dbddfSTejun Heo /* if there are more than one fully free chunks, wake up grim reaper */ 932fbf59bc9STejun Heo if (chunk->free_size == pcpu_unit_size) { 933fbf59bc9STejun Heo struct pcpu_chunk *pos; 934fbf59bc9STejun Heo 935a56dbddfSTejun Heo list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list) 936fbf59bc9STejun Heo if (pos != chunk) { 937a56dbddfSTejun Heo schedule_work(&pcpu_reclaim_work); 938fbf59bc9STejun Heo break; 939fbf59bc9STejun Heo } 940fbf59bc9STejun Heo } 941fbf59bc9STejun Heo 942ccea34b5STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 943fbf59bc9STejun Heo } 944fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(free_percpu); 945fbf59bc9STejun Heo 9463b034b0dSVivek Goyal /** 94710fad5e4STejun Heo * is_kernel_percpu_address - test whether address is from static percpu area 94810fad5e4STejun Heo * @addr: address to test 94910fad5e4STejun Heo * 95010fad5e4STejun Heo * Test whether @addr belongs to in-kernel static percpu area. Module 95110fad5e4STejun Heo * static percpu areas are not considered. For those, use 95210fad5e4STejun Heo * is_module_percpu_address(). 95310fad5e4STejun Heo * 95410fad5e4STejun Heo * RETURNS: 95510fad5e4STejun Heo * %true if @addr is from in-kernel static percpu area, %false otherwise. 95610fad5e4STejun Heo */ 95710fad5e4STejun Heo bool is_kernel_percpu_address(unsigned long addr) 95810fad5e4STejun Heo { 959bbddff05STejun Heo #ifdef CONFIG_SMP 96010fad5e4STejun Heo const size_t static_size = __per_cpu_end - __per_cpu_start; 96110fad5e4STejun Heo void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 96210fad5e4STejun Heo unsigned int cpu; 96310fad5e4STejun Heo 96410fad5e4STejun Heo for_each_possible_cpu(cpu) { 96510fad5e4STejun Heo void *start = per_cpu_ptr(base, cpu); 96610fad5e4STejun Heo 96710fad5e4STejun Heo if ((void *)addr >= start && (void *)addr < start + static_size) 96810fad5e4STejun Heo return true; 96910fad5e4STejun Heo } 970bbddff05STejun Heo #endif 971bbddff05STejun Heo /* on UP, can't distinguish from other static vars, always false */ 97210fad5e4STejun Heo return false; 97310fad5e4STejun Heo } 97410fad5e4STejun Heo 97510fad5e4STejun Heo /** 9763b034b0dSVivek Goyal * per_cpu_ptr_to_phys - convert translated percpu address to physical address 9773b034b0dSVivek Goyal * @addr: the address to be converted to physical address 9783b034b0dSVivek Goyal * 9793b034b0dSVivek Goyal * Given @addr which is dereferenceable address obtained via one of 9803b034b0dSVivek Goyal * percpu access macros, this function translates it into its physical 9813b034b0dSVivek Goyal * address. The caller is responsible for ensuring @addr stays valid 9823b034b0dSVivek Goyal * until this function finishes. 9833b034b0dSVivek Goyal * 9843b034b0dSVivek Goyal * RETURNS: 9853b034b0dSVivek Goyal * The physical address for @addr. 9863b034b0dSVivek Goyal */ 9873b034b0dSVivek Goyal phys_addr_t per_cpu_ptr_to_phys(void *addr) 9883b034b0dSVivek Goyal { 9899983b6f0STejun Heo void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 9909983b6f0STejun Heo bool in_first_chunk = false; 9919983b6f0STejun Heo unsigned long first_start, first_end; 9929983b6f0STejun Heo unsigned int cpu; 9939983b6f0STejun Heo 9949983b6f0STejun Heo /* 9959983b6f0STejun Heo * The following test on first_start/end isn't strictly 9969983b6f0STejun Heo * necessary but will speed up lookups of addresses which 9979983b6f0STejun Heo * aren't in the first chunk. 9989983b6f0STejun Heo */ 9999983b6f0STejun Heo first_start = pcpu_chunk_addr(pcpu_first_chunk, pcpu_first_unit_cpu, 0); 10009983b6f0STejun Heo first_end = pcpu_chunk_addr(pcpu_first_chunk, pcpu_last_unit_cpu, 10019983b6f0STejun Heo pcpu_unit_pages); 10029983b6f0STejun Heo if ((unsigned long)addr >= first_start && 10039983b6f0STejun Heo (unsigned long)addr < first_end) { 10049983b6f0STejun Heo for_each_possible_cpu(cpu) { 10059983b6f0STejun Heo void *start = per_cpu_ptr(base, cpu); 10069983b6f0STejun Heo 10079983b6f0STejun Heo if (addr >= start && addr < start + pcpu_unit_size) { 10089983b6f0STejun Heo in_first_chunk = true; 10099983b6f0STejun Heo break; 10109983b6f0STejun Heo } 10119983b6f0STejun Heo } 10129983b6f0STejun Heo } 10139983b6f0STejun Heo 10149983b6f0STejun Heo if (in_first_chunk) { 10153b034b0dSVivek Goyal if ((unsigned long)addr < VMALLOC_START || 10163b034b0dSVivek Goyal (unsigned long)addr >= VMALLOC_END) 10173b034b0dSVivek Goyal return __pa(addr); 10183b034b0dSVivek Goyal else 10193b034b0dSVivek Goyal return page_to_phys(vmalloc_to_page(addr)); 1020020ec653STejun Heo } else 10219f645532STejun Heo return page_to_phys(pcpu_addr_to_page(addr)); 10223b034b0dSVivek Goyal } 10233b034b0dSVivek Goyal 1024fbf59bc9STejun Heo /** 1025fd1e8a1fSTejun Heo * pcpu_alloc_alloc_info - allocate percpu allocation info 1026fd1e8a1fSTejun Heo * @nr_groups: the number of groups 1027fd1e8a1fSTejun Heo * @nr_units: the number of units 1028033e48fbSTejun Heo * 1029fd1e8a1fSTejun Heo * Allocate ai which is large enough for @nr_groups groups containing 1030fd1e8a1fSTejun Heo * @nr_units units. The returned ai's groups[0].cpu_map points to the 1031fd1e8a1fSTejun Heo * cpu_map array which is long enough for @nr_units and filled with 1032fd1e8a1fSTejun Heo * NR_CPUS. It's the caller's responsibility to initialize cpu_map 1033fd1e8a1fSTejun Heo * pointer of other groups. 1034033e48fbSTejun Heo * 1035033e48fbSTejun Heo * RETURNS: 1036fd1e8a1fSTejun Heo * Pointer to the allocated pcpu_alloc_info on success, NULL on 1037fd1e8a1fSTejun Heo * failure. 1038033e48fbSTejun Heo */ 1039fd1e8a1fSTejun Heo struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups, 1040fd1e8a1fSTejun Heo int nr_units) 1041fd1e8a1fSTejun Heo { 1042fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 1043fd1e8a1fSTejun Heo size_t base_size, ai_size; 1044fd1e8a1fSTejun Heo void *ptr; 1045fd1e8a1fSTejun Heo int unit; 1046fd1e8a1fSTejun Heo 1047fd1e8a1fSTejun Heo base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]), 1048fd1e8a1fSTejun Heo __alignof__(ai->groups[0].cpu_map[0])); 1049fd1e8a1fSTejun Heo ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]); 1050fd1e8a1fSTejun Heo 1051fd1e8a1fSTejun Heo ptr = alloc_bootmem_nopanic(PFN_ALIGN(ai_size)); 1052fd1e8a1fSTejun Heo if (!ptr) 1053fd1e8a1fSTejun Heo return NULL; 1054fd1e8a1fSTejun Heo ai = ptr; 1055fd1e8a1fSTejun Heo ptr += base_size; 1056fd1e8a1fSTejun Heo 1057fd1e8a1fSTejun Heo ai->groups[0].cpu_map = ptr; 1058fd1e8a1fSTejun Heo 1059fd1e8a1fSTejun Heo for (unit = 0; unit < nr_units; unit++) 1060fd1e8a1fSTejun Heo ai->groups[0].cpu_map[unit] = NR_CPUS; 1061fd1e8a1fSTejun Heo 1062fd1e8a1fSTejun Heo ai->nr_groups = nr_groups; 1063fd1e8a1fSTejun Heo ai->__ai_size = PFN_ALIGN(ai_size); 1064fd1e8a1fSTejun Heo 1065fd1e8a1fSTejun Heo return ai; 1066fd1e8a1fSTejun Heo } 1067fd1e8a1fSTejun Heo 1068fd1e8a1fSTejun Heo /** 1069fd1e8a1fSTejun Heo * pcpu_free_alloc_info - free percpu allocation info 1070fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info to free 1071fd1e8a1fSTejun Heo * 1072fd1e8a1fSTejun Heo * Free @ai which was allocated by pcpu_alloc_alloc_info(). 1073fd1e8a1fSTejun Heo */ 1074fd1e8a1fSTejun Heo void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai) 1075fd1e8a1fSTejun Heo { 1076fd1e8a1fSTejun Heo free_bootmem(__pa(ai), ai->__ai_size); 1077fd1e8a1fSTejun Heo } 1078fd1e8a1fSTejun Heo 1079fd1e8a1fSTejun Heo /** 1080fd1e8a1fSTejun Heo * pcpu_dump_alloc_info - print out information about pcpu_alloc_info 1081fd1e8a1fSTejun Heo * @lvl: loglevel 1082fd1e8a1fSTejun Heo * @ai: allocation info to dump 1083fd1e8a1fSTejun Heo * 1084fd1e8a1fSTejun Heo * Print out information about @ai using loglevel @lvl. 1085fd1e8a1fSTejun Heo */ 1086fd1e8a1fSTejun Heo static void pcpu_dump_alloc_info(const char *lvl, 1087fd1e8a1fSTejun Heo const struct pcpu_alloc_info *ai) 1088033e48fbSTejun Heo { 1089fd1e8a1fSTejun Heo int group_width = 1, cpu_width = 1, width; 1090033e48fbSTejun Heo char empty_str[] = "--------"; 1091fd1e8a1fSTejun Heo int alloc = 0, alloc_end = 0; 1092fd1e8a1fSTejun Heo int group, v; 1093fd1e8a1fSTejun Heo int upa, apl; /* units per alloc, allocs per line */ 1094033e48fbSTejun Heo 1095fd1e8a1fSTejun Heo v = ai->nr_groups; 1096033e48fbSTejun Heo while (v /= 10) 1097fd1e8a1fSTejun Heo group_width++; 1098033e48fbSTejun Heo 1099fd1e8a1fSTejun Heo v = num_possible_cpus(); 1100fd1e8a1fSTejun Heo while (v /= 10) 1101fd1e8a1fSTejun Heo cpu_width++; 1102fd1e8a1fSTejun Heo empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0'; 1103033e48fbSTejun Heo 1104fd1e8a1fSTejun Heo upa = ai->alloc_size / ai->unit_size; 1105fd1e8a1fSTejun Heo width = upa * (cpu_width + 1) + group_width + 3; 1106fd1e8a1fSTejun Heo apl = rounddown_pow_of_two(max(60 / width, 1)); 1107033e48fbSTejun Heo 1108fd1e8a1fSTejun Heo printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu", 1109fd1e8a1fSTejun Heo lvl, ai->static_size, ai->reserved_size, ai->dyn_size, 1110fd1e8a1fSTejun Heo ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size); 1111fd1e8a1fSTejun Heo 1112fd1e8a1fSTejun Heo for (group = 0; group < ai->nr_groups; group++) { 1113fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 1114fd1e8a1fSTejun Heo int unit = 0, unit_end = 0; 1115fd1e8a1fSTejun Heo 1116fd1e8a1fSTejun Heo BUG_ON(gi->nr_units % upa); 1117fd1e8a1fSTejun Heo for (alloc_end += gi->nr_units / upa; 1118fd1e8a1fSTejun Heo alloc < alloc_end; alloc++) { 1119fd1e8a1fSTejun Heo if (!(alloc % apl)) { 1120033e48fbSTejun Heo printk("\n"); 1121fd1e8a1fSTejun Heo printk("%spcpu-alloc: ", lvl); 1122033e48fbSTejun Heo } 1123fd1e8a1fSTejun Heo printk("[%0*d] ", group_width, group); 1124fd1e8a1fSTejun Heo 1125fd1e8a1fSTejun Heo for (unit_end += upa; unit < unit_end; unit++) 1126fd1e8a1fSTejun Heo if (gi->cpu_map[unit] != NR_CPUS) 1127fd1e8a1fSTejun Heo printk("%0*d ", cpu_width, 1128fd1e8a1fSTejun Heo gi->cpu_map[unit]); 1129033e48fbSTejun Heo else 1130033e48fbSTejun Heo printk("%s ", empty_str); 1131033e48fbSTejun Heo } 1132fd1e8a1fSTejun Heo } 1133033e48fbSTejun Heo printk("\n"); 1134033e48fbSTejun Heo } 1135033e48fbSTejun Heo 1136fbf59bc9STejun Heo /** 11378d408b4bSTejun Heo * pcpu_setup_first_chunk - initialize the first percpu chunk 1138fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info describing how to percpu area is shaped 113938a6be52STejun Heo * @base_addr: mapped address 1140fbf59bc9STejun Heo * 11418d408b4bSTejun Heo * Initialize the first percpu chunk which contains the kernel static 11428d408b4bSTejun Heo * perpcu area. This function is to be called from arch percpu area 114338a6be52STejun Heo * setup path. 11448d408b4bSTejun Heo * 1145fd1e8a1fSTejun Heo * @ai contains all information necessary to initialize the first 1146fd1e8a1fSTejun Heo * chunk and prime the dynamic percpu allocator. 11478d408b4bSTejun Heo * 1148fd1e8a1fSTejun Heo * @ai->static_size is the size of static percpu area. 1149fd1e8a1fSTejun Heo * 1150fd1e8a1fSTejun Heo * @ai->reserved_size, if non-zero, specifies the amount of bytes to 1151edcb4639STejun Heo * reserve after the static area in the first chunk. This reserves 1152edcb4639STejun Heo * the first chunk such that it's available only through reserved 1153edcb4639STejun Heo * percpu allocation. This is primarily used to serve module percpu 1154edcb4639STejun Heo * static areas on architectures where the addressing model has 1155edcb4639STejun Heo * limited offset range for symbol relocations to guarantee module 1156edcb4639STejun Heo * percpu symbols fall inside the relocatable range. 1157edcb4639STejun Heo * 1158fd1e8a1fSTejun Heo * @ai->dyn_size determines the number of bytes available for dynamic 1159fd1e8a1fSTejun Heo * allocation in the first chunk. The area between @ai->static_size + 1160fd1e8a1fSTejun Heo * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused. 11616074d5b0STejun Heo * 1162fd1e8a1fSTejun Heo * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE 1163fd1e8a1fSTejun Heo * and equal to or larger than @ai->static_size + @ai->reserved_size + 1164fd1e8a1fSTejun Heo * @ai->dyn_size. 11658d408b4bSTejun Heo * 1166fd1e8a1fSTejun Heo * @ai->atom_size is the allocation atom size and used as alignment 1167fd1e8a1fSTejun Heo * for vm areas. 11688d408b4bSTejun Heo * 1169fd1e8a1fSTejun Heo * @ai->alloc_size is the allocation size and always multiple of 1170fd1e8a1fSTejun Heo * @ai->atom_size. This is larger than @ai->atom_size if 1171fd1e8a1fSTejun Heo * @ai->unit_size is larger than @ai->atom_size. 1172fd1e8a1fSTejun Heo * 1173fd1e8a1fSTejun Heo * @ai->nr_groups and @ai->groups describe virtual memory layout of 1174fd1e8a1fSTejun Heo * percpu areas. Units which should be colocated are put into the 1175fd1e8a1fSTejun Heo * same group. Dynamic VM areas will be allocated according to these 1176fd1e8a1fSTejun Heo * groupings. If @ai->nr_groups is zero, a single group containing 1177fd1e8a1fSTejun Heo * all units is assumed. 11788d408b4bSTejun Heo * 117938a6be52STejun Heo * The caller should have mapped the first chunk at @base_addr and 118038a6be52STejun Heo * copied static data to each unit. 1181fbf59bc9STejun Heo * 1182edcb4639STejun Heo * If the first chunk ends up with both reserved and dynamic areas, it 1183edcb4639STejun Heo * is served by two chunks - one to serve the core static and reserved 1184edcb4639STejun Heo * areas and the other for the dynamic area. They share the same vm 1185edcb4639STejun Heo * and page map but uses different area allocation map to stay away 1186edcb4639STejun Heo * from each other. The latter chunk is circulated in the chunk slots 1187edcb4639STejun Heo * and available for dynamic allocation like any other chunks. 1188edcb4639STejun Heo * 1189fbf59bc9STejun Heo * RETURNS: 1190fb435d52STejun Heo * 0 on success, -errno on failure. 1191fbf59bc9STejun Heo */ 1192fb435d52STejun Heo int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai, 1193fd1e8a1fSTejun Heo void *base_addr) 1194fbf59bc9STejun Heo { 1195635b75fcSTejun Heo static char cpus_buf[4096] __initdata; 1196099a19d9STejun Heo static int smap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata; 1197099a19d9STejun Heo static int dmap[PERCPU_DYNAMIC_EARLY_SLOTS] __initdata; 1198fd1e8a1fSTejun Heo size_t dyn_size = ai->dyn_size; 1199fd1e8a1fSTejun Heo size_t size_sum = ai->static_size + ai->reserved_size + dyn_size; 1200edcb4639STejun Heo struct pcpu_chunk *schunk, *dchunk = NULL; 12016563297cSTejun Heo unsigned long *group_offsets; 12026563297cSTejun Heo size_t *group_sizes; 1203fb435d52STejun Heo unsigned long *unit_off; 1204fbf59bc9STejun Heo unsigned int cpu; 1205fd1e8a1fSTejun Heo int *unit_map; 1206fd1e8a1fSTejun Heo int group, unit, i; 1207fbf59bc9STejun Heo 1208635b75fcSTejun Heo cpumask_scnprintf(cpus_buf, sizeof(cpus_buf), cpu_possible_mask); 1209635b75fcSTejun Heo 1210635b75fcSTejun Heo #define PCPU_SETUP_BUG_ON(cond) do { \ 1211635b75fcSTejun Heo if (unlikely(cond)) { \ 1212635b75fcSTejun Heo pr_emerg("PERCPU: failed to initialize, %s", #cond); \ 1213635b75fcSTejun Heo pr_emerg("PERCPU: cpu_possible_mask=%s\n", cpus_buf); \ 1214635b75fcSTejun Heo pcpu_dump_alloc_info(KERN_EMERG, ai); \ 1215635b75fcSTejun Heo BUG(); \ 1216635b75fcSTejun Heo } \ 1217635b75fcSTejun Heo } while (0) 1218635b75fcSTejun Heo 12192f39e637STejun Heo /* sanity checks */ 1220635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->nr_groups <= 0); 1221bbddff05STejun Heo #ifdef CONFIG_SMP 1222635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!ai->static_size); 1223bbddff05STejun Heo #endif 1224635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!base_addr); 1225635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < size_sum); 1226635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size & ~PAGE_MASK); 1227635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE); 1228099a19d9STejun Heo PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE); 12299f645532STejun Heo PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0); 12308d408b4bSTejun Heo 12316563297cSTejun Heo /* process group information and build config tables accordingly */ 12326563297cSTejun Heo group_offsets = alloc_bootmem(ai->nr_groups * sizeof(group_offsets[0])); 12336563297cSTejun Heo group_sizes = alloc_bootmem(ai->nr_groups * sizeof(group_sizes[0])); 1234fd1e8a1fSTejun Heo unit_map = alloc_bootmem(nr_cpu_ids * sizeof(unit_map[0])); 1235fb435d52STejun Heo unit_off = alloc_bootmem(nr_cpu_ids * sizeof(unit_off[0])); 12362f39e637STejun Heo 1237fd1e8a1fSTejun Heo for (cpu = 0; cpu < nr_cpu_ids; cpu++) 1238ffe0d5a5STejun Heo unit_map[cpu] = UINT_MAX; 1239fd1e8a1fSTejun Heo pcpu_first_unit_cpu = NR_CPUS; 12402f39e637STejun Heo 1241fd1e8a1fSTejun Heo for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) { 1242fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 12432f39e637STejun Heo 12446563297cSTejun Heo group_offsets[group] = gi->base_offset; 12456563297cSTejun Heo group_sizes[group] = gi->nr_units * ai->unit_size; 12466563297cSTejun Heo 1247fd1e8a1fSTejun Heo for (i = 0; i < gi->nr_units; i++) { 1248fd1e8a1fSTejun Heo cpu = gi->cpu_map[i]; 1249fd1e8a1fSTejun Heo if (cpu == NR_CPUS) 1250fd1e8a1fSTejun Heo continue; 1251fd1e8a1fSTejun Heo 1252635b75fcSTejun Heo PCPU_SETUP_BUG_ON(cpu > nr_cpu_ids); 1253635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!cpu_possible(cpu)); 1254635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX); 1255fd1e8a1fSTejun Heo 1256fd1e8a1fSTejun Heo unit_map[cpu] = unit + i; 1257fb435d52STejun Heo unit_off[cpu] = gi->base_offset + i * ai->unit_size; 1258fb435d52STejun Heo 1259fd1e8a1fSTejun Heo if (pcpu_first_unit_cpu == NR_CPUS) 12602f39e637STejun Heo pcpu_first_unit_cpu = cpu; 12612f39e637STejun Heo pcpu_last_unit_cpu = cpu; 12620fc0531eSLinus Torvalds } 12630fc0531eSLinus Torvalds } 1264fd1e8a1fSTejun Heo pcpu_nr_units = unit; 12652f39e637STejun Heo 12662f39e637STejun Heo for_each_possible_cpu(cpu) 1267635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX); 1268635b75fcSTejun Heo 1269635b75fcSTejun Heo /* we're done parsing the input, undefine BUG macro and dump config */ 1270635b75fcSTejun Heo #undef PCPU_SETUP_BUG_ON 1271*bcbea798STejun Heo pcpu_dump_alloc_info(KERN_DEBUG, ai); 12722f39e637STejun Heo 12736563297cSTejun Heo pcpu_nr_groups = ai->nr_groups; 12746563297cSTejun Heo pcpu_group_offsets = group_offsets; 12756563297cSTejun Heo pcpu_group_sizes = group_sizes; 1276fd1e8a1fSTejun Heo pcpu_unit_map = unit_map; 1277fb435d52STejun Heo pcpu_unit_offsets = unit_off; 12782f39e637STejun Heo 12792f39e637STejun Heo /* determine basic parameters */ 1280fd1e8a1fSTejun Heo pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT; 1281d9b55eebSTejun Heo pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; 12826563297cSTejun Heo pcpu_atom_size = ai->atom_size; 1283ce3141a2STejun Heo pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) + 1284ce3141a2STejun Heo BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long); 1285cafe8816STejun Heo 1286d9b55eebSTejun Heo /* 1287d9b55eebSTejun Heo * Allocate chunk slots. The additional last slot is for 1288d9b55eebSTejun Heo * empty chunks. 1289d9b55eebSTejun Heo */ 1290d9b55eebSTejun Heo pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2; 1291fbf59bc9STejun Heo pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0])); 1292fbf59bc9STejun Heo for (i = 0; i < pcpu_nr_slots; i++) 1293fbf59bc9STejun Heo INIT_LIST_HEAD(&pcpu_slot[i]); 1294fbf59bc9STejun Heo 1295edcb4639STejun Heo /* 1296edcb4639STejun Heo * Initialize static chunk. If reserved_size is zero, the 1297edcb4639STejun Heo * static chunk covers static area + dynamic allocation area 1298edcb4639STejun Heo * in the first chunk. If reserved_size is not zero, it 1299edcb4639STejun Heo * covers static area + reserved area (mostly used for module 1300edcb4639STejun Heo * static percpu allocation). 1301edcb4639STejun Heo */ 13022441d15cSTejun Heo schunk = alloc_bootmem(pcpu_chunk_struct_size); 13032441d15cSTejun Heo INIT_LIST_HEAD(&schunk->list); 1304bba174f5STejun Heo schunk->base_addr = base_addr; 130561ace7faSTejun Heo schunk->map = smap; 130661ace7faSTejun Heo schunk->map_alloc = ARRAY_SIZE(smap); 130738a6be52STejun Heo schunk->immutable = true; 1308ce3141a2STejun Heo bitmap_fill(schunk->populated, pcpu_unit_pages); 1309edcb4639STejun Heo 1310fd1e8a1fSTejun Heo if (ai->reserved_size) { 1311fd1e8a1fSTejun Heo schunk->free_size = ai->reserved_size; 1312ae9e6bc9STejun Heo pcpu_reserved_chunk = schunk; 1313fd1e8a1fSTejun Heo pcpu_reserved_chunk_limit = ai->static_size + ai->reserved_size; 1314edcb4639STejun Heo } else { 13152441d15cSTejun Heo schunk->free_size = dyn_size; 1316edcb4639STejun Heo dyn_size = 0; /* dynamic area covered */ 1317edcb4639STejun Heo } 13182441d15cSTejun Heo schunk->contig_hint = schunk->free_size; 1319fbf59bc9STejun Heo 1320fd1e8a1fSTejun Heo schunk->map[schunk->map_used++] = -ai->static_size; 132161ace7faSTejun Heo if (schunk->free_size) 132261ace7faSTejun Heo schunk->map[schunk->map_used++] = schunk->free_size; 132361ace7faSTejun Heo 1324edcb4639STejun Heo /* init dynamic chunk if necessary */ 1325edcb4639STejun Heo if (dyn_size) { 1326ce3141a2STejun Heo dchunk = alloc_bootmem(pcpu_chunk_struct_size); 1327edcb4639STejun Heo INIT_LIST_HEAD(&dchunk->list); 1328bba174f5STejun Heo dchunk->base_addr = base_addr; 1329edcb4639STejun Heo dchunk->map = dmap; 1330edcb4639STejun Heo dchunk->map_alloc = ARRAY_SIZE(dmap); 133138a6be52STejun Heo dchunk->immutable = true; 1332ce3141a2STejun Heo bitmap_fill(dchunk->populated, pcpu_unit_pages); 1333edcb4639STejun Heo 1334edcb4639STejun Heo dchunk->contig_hint = dchunk->free_size = dyn_size; 1335edcb4639STejun Heo dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit; 1336edcb4639STejun Heo dchunk->map[dchunk->map_used++] = dchunk->free_size; 1337edcb4639STejun Heo } 1338edcb4639STejun Heo 13392441d15cSTejun Heo /* link the first chunk in */ 1340ae9e6bc9STejun Heo pcpu_first_chunk = dchunk ?: schunk; 1341ae9e6bc9STejun Heo pcpu_chunk_relocate(pcpu_first_chunk, -1); 1342fbf59bc9STejun Heo 1343fbf59bc9STejun Heo /* we're done */ 1344bba174f5STejun Heo pcpu_base_addr = base_addr; 1345fb435d52STejun Heo return 0; 1346fbf59bc9STejun Heo } 134766c3a757STejun Heo 1348bbddff05STejun Heo #ifdef CONFIG_SMP 1349bbddff05STejun Heo 1350f58dc01bSTejun Heo const char *pcpu_fc_names[PCPU_FC_NR] __initdata = { 1351f58dc01bSTejun Heo [PCPU_FC_AUTO] = "auto", 1352f58dc01bSTejun Heo [PCPU_FC_EMBED] = "embed", 1353f58dc01bSTejun Heo [PCPU_FC_PAGE] = "page", 1354f58dc01bSTejun Heo }; 135566c3a757STejun Heo 1356f58dc01bSTejun Heo enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO; 1357f58dc01bSTejun Heo 1358f58dc01bSTejun Heo static int __init percpu_alloc_setup(char *str) 135966c3a757STejun Heo { 1360f58dc01bSTejun Heo if (0) 1361f58dc01bSTejun Heo /* nada */; 1362f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK 1363f58dc01bSTejun Heo else if (!strcmp(str, "embed")) 1364f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_EMBED; 1365f58dc01bSTejun Heo #endif 1366f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 1367f58dc01bSTejun Heo else if (!strcmp(str, "page")) 1368f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 1369f58dc01bSTejun Heo #endif 1370f58dc01bSTejun Heo else 1371f58dc01bSTejun Heo pr_warning("PERCPU: unknown allocator %s specified\n", str); 137266c3a757STejun Heo 1373f58dc01bSTejun Heo return 0; 137466c3a757STejun Heo } 1375f58dc01bSTejun Heo early_param("percpu_alloc", percpu_alloc_setup); 137666c3a757STejun Heo 13773c9a024fSTejun Heo /* 13783c9a024fSTejun Heo * pcpu_embed_first_chunk() is used by the generic percpu setup. 13793c9a024fSTejun Heo * Build it if needed by the arch config or the generic setup is going 13803c9a024fSTejun Heo * to be used. 13813c9a024fSTejun Heo */ 138208fc4580STejun Heo #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \ 138308fc4580STejun Heo !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) 13843c9a024fSTejun Heo #define BUILD_EMBED_FIRST_CHUNK 13853c9a024fSTejun Heo #endif 13863c9a024fSTejun Heo 13873c9a024fSTejun Heo /* build pcpu_page_first_chunk() iff needed by the arch config */ 13883c9a024fSTejun Heo #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK) 13893c9a024fSTejun Heo #define BUILD_PAGE_FIRST_CHUNK 13903c9a024fSTejun Heo #endif 13913c9a024fSTejun Heo 13923c9a024fSTejun Heo /* pcpu_build_alloc_info() is used by both embed and page first chunk */ 13933c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK) 13943c9a024fSTejun Heo /** 1395fbf59bc9STejun Heo * pcpu_build_alloc_info - build alloc_info considering distances between CPUs 1396fbf59bc9STejun Heo * @reserved_size: the size of reserved percpu area in bytes 1397fbf59bc9STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 1398fbf59bc9STejun Heo * @atom_size: allocation atom size 1399fbf59bc9STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 1400fbf59bc9STejun Heo * 1401fbf59bc9STejun Heo * This function determines grouping of units, their mappings to cpus 1402fbf59bc9STejun Heo * and other parameters considering needed percpu size, allocation 1403fbf59bc9STejun Heo * atom size and distances between CPUs. 1404fbf59bc9STejun Heo * 1405fbf59bc9STejun Heo * Groups are always mutliples of atom size and CPUs which are of 1406fbf59bc9STejun Heo * LOCAL_DISTANCE both ways are grouped together and share space for 1407fbf59bc9STejun Heo * units in the same group. The returned configuration is guaranteed 1408fbf59bc9STejun Heo * to have CPUs on different nodes on different groups and >=75% usage 1409fbf59bc9STejun Heo * of allocated virtual address space. 1410fbf59bc9STejun Heo * 1411fbf59bc9STejun Heo * RETURNS: 1412fbf59bc9STejun Heo * On success, pointer to the new allocation_info is returned. On 1413fbf59bc9STejun Heo * failure, ERR_PTR value is returned. 1414fbf59bc9STejun Heo */ 1415fbf59bc9STejun Heo static struct pcpu_alloc_info * __init pcpu_build_alloc_info( 1416fbf59bc9STejun Heo size_t reserved_size, size_t dyn_size, 1417fbf59bc9STejun Heo size_t atom_size, 1418fbf59bc9STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn) 1419fbf59bc9STejun Heo { 1420fbf59bc9STejun Heo static int group_map[NR_CPUS] __initdata; 1421fbf59bc9STejun Heo static int group_cnt[NR_CPUS] __initdata; 1422fbf59bc9STejun Heo const size_t static_size = __per_cpu_end - __per_cpu_start; 1423fbf59bc9STejun Heo int nr_groups = 1, nr_units = 0; 1424fbf59bc9STejun Heo size_t size_sum, min_unit_size, alloc_size; 1425fbf59bc9STejun Heo int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */ 1426fbf59bc9STejun Heo int last_allocs, group, unit; 1427fbf59bc9STejun Heo unsigned int cpu, tcpu; 1428fbf59bc9STejun Heo struct pcpu_alloc_info *ai; 1429fbf59bc9STejun Heo unsigned int *cpu_map; 1430fbf59bc9STejun Heo 1431fbf59bc9STejun Heo /* this function may be called multiple times */ 1432fbf59bc9STejun Heo memset(group_map, 0, sizeof(group_map)); 1433fbf59bc9STejun Heo memset(group_cnt, 0, sizeof(group_cnt)); 1434fbf59bc9STejun Heo 1435fbf59bc9STejun Heo /* calculate size_sum and ensure dyn_size is enough for early alloc */ 1436fbf59bc9STejun Heo size_sum = PFN_ALIGN(static_size + reserved_size + 1437fbf59bc9STejun Heo max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE)); 1438fbf59bc9STejun Heo dyn_size = size_sum - static_size - reserved_size; 1439fbf59bc9STejun Heo 1440fbf59bc9STejun Heo /* 1441fbf59bc9STejun Heo * Determine min_unit_size, alloc_size and max_upa such that 1442fbf59bc9STejun Heo * alloc_size is multiple of atom_size and is the smallest 1443fbf59bc9STejun Heo * which can accomodate 4k aligned segments which are equal to 1444fbf59bc9STejun Heo * or larger than min_unit_size. 1445fbf59bc9STejun Heo */ 1446fbf59bc9STejun Heo min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); 1447fbf59bc9STejun Heo 1448fbf59bc9STejun Heo alloc_size = roundup(min_unit_size, atom_size); 1449fbf59bc9STejun Heo upa = alloc_size / min_unit_size; 1450fbf59bc9STejun Heo while (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK)) 1451fbf59bc9STejun Heo upa--; 1452fbf59bc9STejun Heo max_upa = upa; 1453fbf59bc9STejun Heo 1454fbf59bc9STejun Heo /* group cpus according to their proximity */ 1455fbf59bc9STejun Heo for_each_possible_cpu(cpu) { 1456fbf59bc9STejun Heo group = 0; 1457fbf59bc9STejun Heo next_group: 1458fbf59bc9STejun Heo for_each_possible_cpu(tcpu) { 1459fbf59bc9STejun Heo if (cpu == tcpu) 1460fbf59bc9STejun Heo break; 1461fbf59bc9STejun Heo if (group_map[tcpu] == group && cpu_distance_fn && 1462fbf59bc9STejun Heo (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE || 1463fbf59bc9STejun Heo cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) { 1464fbf59bc9STejun Heo group++; 1465fbf59bc9STejun Heo nr_groups = max(nr_groups, group + 1); 1466fbf59bc9STejun Heo goto next_group; 1467fbf59bc9STejun Heo } 1468fbf59bc9STejun Heo } 1469fbf59bc9STejun Heo group_map[cpu] = group; 1470fbf59bc9STejun Heo group_cnt[group]++; 1471fbf59bc9STejun Heo } 1472fbf59bc9STejun Heo 1473fbf59bc9STejun Heo /* 1474fbf59bc9STejun Heo * Expand unit size until address space usage goes over 75% 1475fbf59bc9STejun Heo * and then as much as possible without using more address 1476fbf59bc9STejun Heo * space. 1477fbf59bc9STejun Heo */ 1478fbf59bc9STejun Heo last_allocs = INT_MAX; 1479fbf59bc9STejun Heo for (upa = max_upa; upa; upa--) { 1480fbf59bc9STejun Heo int allocs = 0, wasted = 0; 1481fbf59bc9STejun Heo 1482fbf59bc9STejun Heo if (alloc_size % upa || ((alloc_size / upa) & ~PAGE_MASK)) 1483fbf59bc9STejun Heo continue; 1484fbf59bc9STejun Heo 1485fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 1486fbf59bc9STejun Heo int this_allocs = DIV_ROUND_UP(group_cnt[group], upa); 1487fbf59bc9STejun Heo allocs += this_allocs; 1488fbf59bc9STejun Heo wasted += this_allocs * upa - group_cnt[group]; 1489fbf59bc9STejun Heo } 1490fbf59bc9STejun Heo 1491fbf59bc9STejun Heo /* 1492fbf59bc9STejun Heo * Don't accept if wastage is over 1/3. The 1493fbf59bc9STejun Heo * greater-than comparison ensures upa==1 always 1494fbf59bc9STejun Heo * passes the following check. 1495fbf59bc9STejun Heo */ 1496fbf59bc9STejun Heo if (wasted > num_possible_cpus() / 3) 1497fbf59bc9STejun Heo continue; 1498fbf59bc9STejun Heo 1499fbf59bc9STejun Heo /* and then don't consume more memory */ 1500fbf59bc9STejun Heo if (allocs > last_allocs) 1501fbf59bc9STejun Heo break; 1502fbf59bc9STejun Heo last_allocs = allocs; 1503fbf59bc9STejun Heo best_upa = upa; 1504fbf59bc9STejun Heo } 1505fbf59bc9STejun Heo upa = best_upa; 1506fbf59bc9STejun Heo 1507fbf59bc9STejun Heo /* allocate and fill alloc_info */ 1508fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) 1509fbf59bc9STejun Heo nr_units += roundup(group_cnt[group], upa); 1510fbf59bc9STejun Heo 1511fbf59bc9STejun Heo ai = pcpu_alloc_alloc_info(nr_groups, nr_units); 1512fbf59bc9STejun Heo if (!ai) 1513fbf59bc9STejun Heo return ERR_PTR(-ENOMEM); 1514fbf59bc9STejun Heo cpu_map = ai->groups[0].cpu_map; 1515fbf59bc9STejun Heo 1516fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 1517fbf59bc9STejun Heo ai->groups[group].cpu_map = cpu_map; 1518fbf59bc9STejun Heo cpu_map += roundup(group_cnt[group], upa); 1519fbf59bc9STejun Heo } 1520fbf59bc9STejun Heo 1521fbf59bc9STejun Heo ai->static_size = static_size; 1522fbf59bc9STejun Heo ai->reserved_size = reserved_size; 1523fbf59bc9STejun Heo ai->dyn_size = dyn_size; 1524fbf59bc9STejun Heo ai->unit_size = alloc_size / upa; 1525fbf59bc9STejun Heo ai->atom_size = atom_size; 1526fbf59bc9STejun Heo ai->alloc_size = alloc_size; 1527fbf59bc9STejun Heo 1528fbf59bc9STejun Heo for (group = 0, unit = 0; group_cnt[group]; group++) { 1529fbf59bc9STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 1530fbf59bc9STejun Heo 1531fbf59bc9STejun Heo /* 1532fbf59bc9STejun Heo * Initialize base_offset as if all groups are located 1533fbf59bc9STejun Heo * back-to-back. The caller should update this to 1534fbf59bc9STejun Heo * reflect actual allocation. 1535fbf59bc9STejun Heo */ 1536fbf59bc9STejun Heo gi->base_offset = unit * ai->unit_size; 1537fbf59bc9STejun Heo 1538fbf59bc9STejun Heo for_each_possible_cpu(cpu) 1539fbf59bc9STejun Heo if (group_map[cpu] == group) 1540fbf59bc9STejun Heo gi->cpu_map[gi->nr_units++] = cpu; 1541fbf59bc9STejun Heo gi->nr_units = roundup(gi->nr_units, upa); 1542fbf59bc9STejun Heo unit += gi->nr_units; 1543fbf59bc9STejun Heo } 1544fbf59bc9STejun Heo BUG_ON(unit != nr_units); 1545fbf59bc9STejun Heo 1546fbf59bc9STejun Heo return ai; 1547fbf59bc9STejun Heo } 15483c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */ 1549fbf59bc9STejun Heo 15503c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) 155166c3a757STejun Heo /** 155266c3a757STejun Heo * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem 155366c3a757STejun Heo * @reserved_size: the size of reserved percpu area in bytes 15544ba6ce25STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 1555c8826dd5STejun Heo * @atom_size: allocation atom size 1556c8826dd5STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 1557c8826dd5STejun Heo * @alloc_fn: function to allocate percpu page 1558c8826dd5STejun Heo * @free_fn: funtion to free percpu page 155966c3a757STejun Heo * 156066c3a757STejun Heo * This is a helper to ease setting up embedded first percpu chunk and 156166c3a757STejun Heo * can be called where pcpu_setup_first_chunk() is expected. 156266c3a757STejun Heo * 156366c3a757STejun Heo * If this function is used to setup the first chunk, it is allocated 1564c8826dd5STejun Heo * by calling @alloc_fn and used as-is without being mapped into 1565c8826dd5STejun Heo * vmalloc area. Allocations are always whole multiples of @atom_size 1566c8826dd5STejun Heo * aligned to @atom_size. 1567c8826dd5STejun Heo * 1568c8826dd5STejun Heo * This enables the first chunk to piggy back on the linear physical 1569c8826dd5STejun Heo * mapping which often uses larger page size. Please note that this 1570c8826dd5STejun Heo * can result in very sparse cpu->unit mapping on NUMA machines thus 1571c8826dd5STejun Heo * requiring large vmalloc address space. Don't use this allocator if 1572c8826dd5STejun Heo * vmalloc space is not orders of magnitude larger than distances 1573c8826dd5STejun Heo * between node memory addresses (ie. 32bit NUMA machines). 157466c3a757STejun Heo * 15754ba6ce25STejun Heo * @dyn_size specifies the minimum dynamic area size. 157666c3a757STejun Heo * 157766c3a757STejun Heo * If the needed size is smaller than the minimum or specified unit 1578c8826dd5STejun Heo * size, the leftover is returned using @free_fn. 157966c3a757STejun Heo * 158066c3a757STejun Heo * RETURNS: 1581fb435d52STejun Heo * 0 on success, -errno on failure. 158266c3a757STejun Heo */ 15834ba6ce25STejun Heo int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size, 1584c8826dd5STejun Heo size_t atom_size, 1585c8826dd5STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn, 1586c8826dd5STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 1587c8826dd5STejun Heo pcpu_fc_free_fn_t free_fn) 158866c3a757STejun Heo { 1589c8826dd5STejun Heo void *base = (void *)ULONG_MAX; 1590c8826dd5STejun Heo void **areas = NULL; 1591fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 15926ea529a2STejun Heo size_t size_sum, areas_size, max_distance; 1593c8826dd5STejun Heo int group, i, rc; 159466c3a757STejun Heo 1595c8826dd5STejun Heo ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size, 1596c8826dd5STejun Heo cpu_distance_fn); 1597fd1e8a1fSTejun Heo if (IS_ERR(ai)) 1598fd1e8a1fSTejun Heo return PTR_ERR(ai); 159966c3a757STejun Heo 1600fd1e8a1fSTejun Heo size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 1601c8826dd5STejun Heo areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *)); 160266c3a757STejun Heo 1603c8826dd5STejun Heo areas = alloc_bootmem_nopanic(areas_size); 1604c8826dd5STejun Heo if (!areas) { 1605fb435d52STejun Heo rc = -ENOMEM; 1606c8826dd5STejun Heo goto out_free; 1607fa8a7094STejun Heo } 160866c3a757STejun Heo 1609c8826dd5STejun Heo /* allocate, copy and determine base address */ 1610c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) { 1611c8826dd5STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 1612c8826dd5STejun Heo unsigned int cpu = NR_CPUS; 1613c8826dd5STejun Heo void *ptr; 161466c3a757STejun Heo 1615c8826dd5STejun Heo for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++) 1616c8826dd5STejun Heo cpu = gi->cpu_map[i]; 1617c8826dd5STejun Heo BUG_ON(cpu == NR_CPUS); 1618c8826dd5STejun Heo 1619c8826dd5STejun Heo /* allocate space for the whole group */ 1620c8826dd5STejun Heo ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size); 1621c8826dd5STejun Heo if (!ptr) { 1622c8826dd5STejun Heo rc = -ENOMEM; 1623c8826dd5STejun Heo goto out_free_areas; 1624c8826dd5STejun Heo } 1625c8826dd5STejun Heo areas[group] = ptr; 1626c8826dd5STejun Heo 1627c8826dd5STejun Heo base = min(ptr, base); 1628c8826dd5STejun Heo 1629c8826dd5STejun Heo for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) { 1630c8826dd5STejun Heo if (gi->cpu_map[i] == NR_CPUS) { 1631c8826dd5STejun Heo /* unused unit, free whole */ 1632c8826dd5STejun Heo free_fn(ptr, ai->unit_size); 1633c8826dd5STejun Heo continue; 1634c8826dd5STejun Heo } 1635c8826dd5STejun Heo /* copy and return the unused part */ 1636fd1e8a1fSTejun Heo memcpy(ptr, __per_cpu_load, ai->static_size); 1637c8826dd5STejun Heo free_fn(ptr + size_sum, ai->unit_size - size_sum); 1638c8826dd5STejun Heo } 163966c3a757STejun Heo } 164066c3a757STejun Heo 1641c8826dd5STejun Heo /* base address is now known, determine group base offsets */ 16426ea529a2STejun Heo max_distance = 0; 16436ea529a2STejun Heo for (group = 0; group < ai->nr_groups; group++) { 1644c8826dd5STejun Heo ai->groups[group].base_offset = areas[group] - base; 16451a0c3298STejun Heo max_distance = max_t(size_t, max_distance, 16461a0c3298STejun Heo ai->groups[group].base_offset); 16476ea529a2STejun Heo } 16486ea529a2STejun Heo max_distance += ai->unit_size; 16496ea529a2STejun Heo 16506ea529a2STejun Heo /* warn if maximum distance is further than 75% of vmalloc space */ 16516ea529a2STejun Heo if (max_distance > (VMALLOC_END - VMALLOC_START) * 3 / 4) { 16521a0c3298STejun Heo pr_warning("PERCPU: max_distance=0x%zx too large for vmalloc " 16536ea529a2STejun Heo "space 0x%lx\n", 16546ea529a2STejun Heo max_distance, VMALLOC_END - VMALLOC_START); 16556ea529a2STejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 16566ea529a2STejun Heo /* and fail if we have fallback */ 16576ea529a2STejun Heo rc = -EINVAL; 16586ea529a2STejun Heo goto out_free; 16596ea529a2STejun Heo #endif 16606ea529a2STejun Heo } 1661c8826dd5STejun Heo 1662004018e2STejun Heo pr_info("PERCPU: Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n", 1663fd1e8a1fSTejun Heo PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size, 1664fd1e8a1fSTejun Heo ai->dyn_size, ai->unit_size); 166566c3a757STejun Heo 1666fb435d52STejun Heo rc = pcpu_setup_first_chunk(ai, base); 1667c8826dd5STejun Heo goto out_free; 1668c8826dd5STejun Heo 1669c8826dd5STejun Heo out_free_areas: 1670c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) 1671c8826dd5STejun Heo free_fn(areas[group], 1672c8826dd5STejun Heo ai->groups[group].nr_units * ai->unit_size); 1673c8826dd5STejun Heo out_free: 1674fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 1675c8826dd5STejun Heo if (areas) 1676c8826dd5STejun Heo free_bootmem(__pa(areas), areas_size); 1677fb435d52STejun Heo return rc; 1678d4b95f80STejun Heo } 16793c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK */ 1680d4b95f80STejun Heo 16813c9a024fSTejun Heo #ifdef BUILD_PAGE_FIRST_CHUNK 1682d4b95f80STejun Heo /** 168300ae4064STejun Heo * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages 1684d4b95f80STejun Heo * @reserved_size: the size of reserved percpu area in bytes 1685d4b95f80STejun Heo * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE 1686d4b95f80STejun Heo * @free_fn: funtion to free percpu page, always called with PAGE_SIZE 1687d4b95f80STejun Heo * @populate_pte_fn: function to populate pte 1688d4b95f80STejun Heo * 168900ae4064STejun Heo * This is a helper to ease setting up page-remapped first percpu 169000ae4064STejun Heo * chunk and can be called where pcpu_setup_first_chunk() is expected. 1691d4b95f80STejun Heo * 1692d4b95f80STejun Heo * This is the basic allocator. Static percpu area is allocated 1693d4b95f80STejun Heo * page-by-page into vmalloc area. 1694d4b95f80STejun Heo * 1695d4b95f80STejun Heo * RETURNS: 1696fb435d52STejun Heo * 0 on success, -errno on failure. 1697d4b95f80STejun Heo */ 1698fb435d52STejun Heo int __init pcpu_page_first_chunk(size_t reserved_size, 1699d4b95f80STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 1700d4b95f80STejun Heo pcpu_fc_free_fn_t free_fn, 1701d4b95f80STejun Heo pcpu_fc_populate_pte_fn_t populate_pte_fn) 1702d4b95f80STejun Heo { 17038f05a6a6STejun Heo static struct vm_struct vm; 1704fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 170500ae4064STejun Heo char psize_str[16]; 1706ce3141a2STejun Heo int unit_pages; 1707d4b95f80STejun Heo size_t pages_size; 1708ce3141a2STejun Heo struct page **pages; 1709fb435d52STejun Heo int unit, i, j, rc; 1710d4b95f80STejun Heo 171100ae4064STejun Heo snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10); 171200ae4064STejun Heo 17134ba6ce25STejun Heo ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL); 1714fd1e8a1fSTejun Heo if (IS_ERR(ai)) 1715fd1e8a1fSTejun Heo return PTR_ERR(ai); 1716fd1e8a1fSTejun Heo BUG_ON(ai->nr_groups != 1); 1717fd1e8a1fSTejun Heo BUG_ON(ai->groups[0].nr_units != num_possible_cpus()); 1718fd1e8a1fSTejun Heo 1719fd1e8a1fSTejun Heo unit_pages = ai->unit_size >> PAGE_SHIFT; 1720d4b95f80STejun Heo 1721d4b95f80STejun Heo /* unaligned allocations can't be freed, round up to page size */ 1722fd1e8a1fSTejun Heo pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() * 1723fd1e8a1fSTejun Heo sizeof(pages[0])); 1724ce3141a2STejun Heo pages = alloc_bootmem(pages_size); 1725d4b95f80STejun Heo 17268f05a6a6STejun Heo /* allocate pages */ 1727d4b95f80STejun Heo j = 0; 1728fd1e8a1fSTejun Heo for (unit = 0; unit < num_possible_cpus(); unit++) 1729ce3141a2STejun Heo for (i = 0; i < unit_pages; i++) { 1730fd1e8a1fSTejun Heo unsigned int cpu = ai->groups[0].cpu_map[unit]; 1731d4b95f80STejun Heo void *ptr; 1732d4b95f80STejun Heo 17333cbc8565STejun Heo ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE); 1734d4b95f80STejun Heo if (!ptr) { 173500ae4064STejun Heo pr_warning("PERCPU: failed to allocate %s page " 173600ae4064STejun Heo "for cpu%u\n", psize_str, cpu); 1737d4b95f80STejun Heo goto enomem; 1738d4b95f80STejun Heo } 1739ce3141a2STejun Heo pages[j++] = virt_to_page(ptr); 1740d4b95f80STejun Heo } 1741d4b95f80STejun Heo 17428f05a6a6STejun Heo /* allocate vm area, map the pages and copy static data */ 17438f05a6a6STejun Heo vm.flags = VM_ALLOC; 1744fd1e8a1fSTejun Heo vm.size = num_possible_cpus() * ai->unit_size; 17458f05a6a6STejun Heo vm_area_register_early(&vm, PAGE_SIZE); 17468f05a6a6STejun Heo 1747fd1e8a1fSTejun Heo for (unit = 0; unit < num_possible_cpus(); unit++) { 17481d9d3257STejun Heo unsigned long unit_addr = 1749fd1e8a1fSTejun Heo (unsigned long)vm.addr + unit * ai->unit_size; 17508f05a6a6STejun Heo 1751ce3141a2STejun Heo for (i = 0; i < unit_pages; i++) 17528f05a6a6STejun Heo populate_pte_fn(unit_addr + (i << PAGE_SHIFT)); 17538f05a6a6STejun Heo 17548f05a6a6STejun Heo /* pte already populated, the following shouldn't fail */ 1755fb435d52STejun Heo rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages], 1756ce3141a2STejun Heo unit_pages); 1757fb435d52STejun Heo if (rc < 0) 1758fb435d52STejun Heo panic("failed to map percpu area, err=%d\n", rc); 17598f05a6a6STejun Heo 17608f05a6a6STejun Heo /* 17618f05a6a6STejun Heo * FIXME: Archs with virtual cache should flush local 17628f05a6a6STejun Heo * cache for the linear mapping here - something 17638f05a6a6STejun Heo * equivalent to flush_cache_vmap() on the local cpu. 17648f05a6a6STejun Heo * flush_cache_vmap() can't be used as most supporting 17658f05a6a6STejun Heo * data structures are not set up yet. 17668f05a6a6STejun Heo */ 17678f05a6a6STejun Heo 17688f05a6a6STejun Heo /* copy static data */ 1769fd1e8a1fSTejun Heo memcpy((void *)unit_addr, __per_cpu_load, ai->static_size); 177066c3a757STejun Heo } 177166c3a757STejun Heo 177266c3a757STejun Heo /* we're ready, commit */ 17731d9d3257STejun Heo pr_info("PERCPU: %d %s pages/cpu @%p s%zu r%zu d%zu\n", 1774fd1e8a1fSTejun Heo unit_pages, psize_str, vm.addr, ai->static_size, 1775fd1e8a1fSTejun Heo ai->reserved_size, ai->dyn_size); 177666c3a757STejun Heo 1777fb435d52STejun Heo rc = pcpu_setup_first_chunk(ai, vm.addr); 1778d4b95f80STejun Heo goto out_free_ar; 1779d4b95f80STejun Heo 1780d4b95f80STejun Heo enomem: 1781d4b95f80STejun Heo while (--j >= 0) 1782ce3141a2STejun Heo free_fn(page_address(pages[j]), PAGE_SIZE); 1783fb435d52STejun Heo rc = -ENOMEM; 1784d4b95f80STejun Heo out_free_ar: 1785ce3141a2STejun Heo free_bootmem(__pa(pages), pages_size); 1786fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 1787fb435d52STejun Heo return rc; 178866c3a757STejun Heo } 17893c9a024fSTejun Heo #endif /* BUILD_PAGE_FIRST_CHUNK */ 1790d4b95f80STejun Heo 1791bbddff05STejun Heo #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA 17928c4bfc6eSTejun Heo /* 1793bbddff05STejun Heo * Generic SMP percpu area setup. 1794e74e3962STejun Heo * 1795e74e3962STejun Heo * The embedding helper is used because its behavior closely resembles 1796e74e3962STejun Heo * the original non-dynamic generic percpu area setup. This is 1797e74e3962STejun Heo * important because many archs have addressing restrictions and might 1798e74e3962STejun Heo * fail if the percpu area is located far away from the previous 1799e74e3962STejun Heo * location. As an added bonus, in non-NUMA cases, embedding is 1800e74e3962STejun Heo * generally a good idea TLB-wise because percpu area can piggy back 1801e74e3962STejun Heo * on the physical linear memory mapping which uses large page 1802e74e3962STejun Heo * mappings on applicable archs. 1803e74e3962STejun Heo */ 1804e74e3962STejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; 1805e74e3962STejun Heo EXPORT_SYMBOL(__per_cpu_offset); 1806e74e3962STejun Heo 1807c8826dd5STejun Heo static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size, 1808c8826dd5STejun Heo size_t align) 1809c8826dd5STejun Heo { 1810c8826dd5STejun Heo return __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS)); 1811c8826dd5STejun Heo } 1812c8826dd5STejun Heo 1813c8826dd5STejun Heo static void __init pcpu_dfl_fc_free(void *ptr, size_t size) 1814c8826dd5STejun Heo { 1815c8826dd5STejun Heo free_bootmem(__pa(ptr), size); 1816c8826dd5STejun Heo } 1817c8826dd5STejun Heo 1818e74e3962STejun Heo void __init setup_per_cpu_areas(void) 1819e74e3962STejun Heo { 1820e74e3962STejun Heo unsigned long delta; 1821e74e3962STejun Heo unsigned int cpu; 1822fb435d52STejun Heo int rc; 1823e74e3962STejun Heo 1824e74e3962STejun Heo /* 1825e74e3962STejun Heo * Always reserve area for module percpu variables. That's 1826e74e3962STejun Heo * what the legacy allocator did. 1827e74e3962STejun Heo */ 1828fb435d52STejun Heo rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, 1829c8826dd5STejun Heo PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL, 1830c8826dd5STejun Heo pcpu_dfl_fc_alloc, pcpu_dfl_fc_free); 1831fb435d52STejun Heo if (rc < 0) 1832bbddff05STejun Heo panic("Failed to initialize percpu areas."); 1833e74e3962STejun Heo 1834e74e3962STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 1835e74e3962STejun Heo for_each_possible_cpu(cpu) 1836fb435d52STejun Heo __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; 1837e74e3962STejun Heo } 1838e74e3962STejun Heo #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */ 1839099a19d9STejun Heo 1840bbddff05STejun Heo #else /* CONFIG_SMP */ 1841bbddff05STejun Heo 1842bbddff05STejun Heo /* 1843bbddff05STejun Heo * UP percpu area setup. 1844bbddff05STejun Heo * 1845bbddff05STejun Heo * UP always uses km-based percpu allocator with identity mapping. 1846bbddff05STejun Heo * Static percpu variables are indistinguishable from the usual static 1847bbddff05STejun Heo * variables and don't require any special preparation. 1848bbddff05STejun Heo */ 1849bbddff05STejun Heo void __init setup_per_cpu_areas(void) 1850bbddff05STejun Heo { 1851bbddff05STejun Heo const size_t unit_size = 1852bbddff05STejun Heo roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE, 1853bbddff05STejun Heo PERCPU_DYNAMIC_RESERVE)); 1854bbddff05STejun Heo struct pcpu_alloc_info *ai; 1855bbddff05STejun Heo void *fc; 1856bbddff05STejun Heo 1857bbddff05STejun Heo ai = pcpu_alloc_alloc_info(1, 1); 1858bbddff05STejun Heo fc = __alloc_bootmem(unit_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS)); 1859bbddff05STejun Heo if (!ai || !fc) 1860bbddff05STejun Heo panic("Failed to allocate memory for percpu areas."); 1861bbddff05STejun Heo 1862bbddff05STejun Heo ai->dyn_size = unit_size; 1863bbddff05STejun Heo ai->unit_size = unit_size; 1864bbddff05STejun Heo ai->atom_size = unit_size; 1865bbddff05STejun Heo ai->alloc_size = unit_size; 1866bbddff05STejun Heo ai->groups[0].nr_units = 1; 1867bbddff05STejun Heo ai->groups[0].cpu_map[0] = 0; 1868bbddff05STejun Heo 1869bbddff05STejun Heo if (pcpu_setup_first_chunk(ai, fc) < 0) 1870bbddff05STejun Heo panic("Failed to initialize percpu areas."); 1871bbddff05STejun Heo } 1872bbddff05STejun Heo 1873bbddff05STejun Heo #endif /* CONFIG_SMP */ 1874bbddff05STejun Heo 1875099a19d9STejun Heo /* 1876099a19d9STejun Heo * First and reserved chunks are initialized with temporary allocation 1877099a19d9STejun Heo * map in initdata so that they can be used before slab is online. 1878099a19d9STejun Heo * This function is called after slab is brought up and replaces those 1879099a19d9STejun Heo * with properly allocated maps. 1880099a19d9STejun Heo */ 1881099a19d9STejun Heo void __init percpu_init_late(void) 1882099a19d9STejun Heo { 1883099a19d9STejun Heo struct pcpu_chunk *target_chunks[] = 1884099a19d9STejun Heo { pcpu_first_chunk, pcpu_reserved_chunk, NULL }; 1885099a19d9STejun Heo struct pcpu_chunk *chunk; 1886099a19d9STejun Heo unsigned long flags; 1887099a19d9STejun Heo int i; 1888099a19d9STejun Heo 1889099a19d9STejun Heo for (i = 0; (chunk = target_chunks[i]); i++) { 1890099a19d9STejun Heo int *map; 1891099a19d9STejun Heo const size_t size = PERCPU_DYNAMIC_EARLY_SLOTS * sizeof(map[0]); 1892099a19d9STejun Heo 1893099a19d9STejun Heo BUILD_BUG_ON(size > PAGE_SIZE); 1894099a19d9STejun Heo 1895099a19d9STejun Heo map = pcpu_mem_alloc(size); 1896099a19d9STejun Heo BUG_ON(!map); 1897099a19d9STejun Heo 1898099a19d9STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1899099a19d9STejun Heo memcpy(map, chunk->map, size); 1900099a19d9STejun Heo chunk->map = map; 1901099a19d9STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1902099a19d9STejun Heo } 1903099a19d9STejun Heo } 1904