155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fbf59bc9STejun Heo /* 388999a89STejun Heo * mm/percpu.c - percpu memory allocator 4fbf59bc9STejun Heo * 5fbf59bc9STejun Heo * Copyright (C) 2009 SUSE Linux Products GmbH 6fbf59bc9STejun Heo * Copyright (C) 2009 Tejun Heo <tj@kernel.org> 7fbf59bc9STejun Heo * 85e81ee3eSDennis Zhou (Facebook) * Copyright (C) 2017 Facebook Inc. 95e81ee3eSDennis Zhou (Facebook) * Copyright (C) 2017 Dennis Zhou <dennisszhou@gmail.com> 105e81ee3eSDennis Zhou (Facebook) * 119c015162SDennis Zhou (Facebook) * The percpu allocator handles both static and dynamic areas. Percpu 129c015162SDennis Zhou (Facebook) * areas are allocated in chunks which are divided into units. There is 139c015162SDennis Zhou (Facebook) * a 1-to-1 mapping for units to possible cpus. These units are grouped 149c015162SDennis Zhou (Facebook) * based on NUMA properties of the machine. 15fbf59bc9STejun Heo * 16fbf59bc9STejun Heo * c0 c1 c2 17fbf59bc9STejun Heo * ------------------- ------------------- ------------ 18fbf59bc9STejun Heo * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u 19fbf59bc9STejun Heo * ------------------- ...... ------------------- .... ------------ 20fbf59bc9STejun Heo * 219c015162SDennis Zhou (Facebook) * Allocation is done by offsets into a unit's address space. Ie., an 229c015162SDennis Zhou (Facebook) * area of 512 bytes at 6k in c1 occupies 512 bytes at 6k in c1:u0, 239c015162SDennis Zhou (Facebook) * c1:u1, c1:u2, etc. On NUMA machines, the mapping may be non-linear 249c015162SDennis Zhou (Facebook) * and even sparse. Access is handled by configuring percpu base 259c015162SDennis Zhou (Facebook) * registers according to the cpu to unit mappings and offsetting the 269c015162SDennis Zhou (Facebook) * base address using pcpu_unit_size. 27fbf59bc9STejun Heo * 289c015162SDennis Zhou (Facebook) * There is special consideration for the first chunk which must handle 299c015162SDennis Zhou (Facebook) * the static percpu variables in the kernel image as allocation services 305e81ee3eSDennis Zhou (Facebook) * are not online yet. In short, the first chunk is structured like so: 319c015162SDennis Zhou (Facebook) * 329c015162SDennis Zhou (Facebook) * <Static | [Reserved] | Dynamic> 339c015162SDennis Zhou (Facebook) * 349c015162SDennis Zhou (Facebook) * The static data is copied from the original section managed by the 359c015162SDennis Zhou (Facebook) * linker. The reserved section, if non-zero, primarily manages static 369c015162SDennis Zhou (Facebook) * percpu variables from kernel modules. Finally, the dynamic section 379c015162SDennis Zhou (Facebook) * takes care of normal allocations. 38fbf59bc9STejun Heo * 395e81ee3eSDennis Zhou (Facebook) * The allocator organizes chunks into lists according to free size and 405e81ee3eSDennis Zhou (Facebook) * tries to allocate from the fullest chunk first. Each chunk is managed 415e81ee3eSDennis Zhou (Facebook) * by a bitmap with metadata blocks. The allocation map is updated on 425e81ee3eSDennis Zhou (Facebook) * every allocation and free to reflect the current state while the boundary 435e81ee3eSDennis Zhou (Facebook) * map is only updated on allocation. Each metadata block contains 445e81ee3eSDennis Zhou (Facebook) * information to help mitigate the need to iterate over large portions 455e81ee3eSDennis Zhou (Facebook) * of the bitmap. The reverse mapping from page to chunk is stored in 465e81ee3eSDennis Zhou (Facebook) * the page's index. Lastly, units are lazily backed and grow in unison. 47fbf59bc9STejun Heo * 485e81ee3eSDennis Zhou (Facebook) * There is a unique conversion that goes on here between bytes and bits. 495e81ee3eSDennis Zhou (Facebook) * Each bit represents a fragment of size PCPU_MIN_ALLOC_SIZE. The chunk 505e81ee3eSDennis Zhou (Facebook) * tracks the number of pages it is responsible for in nr_pages. Helper 515e81ee3eSDennis Zhou (Facebook) * functions are used to convert from between the bytes, bits, and blocks. 525e81ee3eSDennis Zhou (Facebook) * All hints are managed in bits unless explicitly stated. 539c015162SDennis Zhou (Facebook) * 544091fb95SMasahiro Yamada * To use this allocator, arch code should do the following: 55fbf59bc9STejun Heo * 56fbf59bc9STejun Heo * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate 57e0100983STejun Heo * regular address to percpu pointer and back if they need to be 58e0100983STejun Heo * different from the default 59fbf59bc9STejun Heo * 608d408b4bSTejun Heo * - use pcpu_setup_first_chunk() during percpu area initialization to 618d408b4bSTejun Heo * setup the first chunk containing the kernel static percpu area 62fbf59bc9STejun Heo */ 63fbf59bc9STejun Heo 64870d4b12SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 65870d4b12SJoe Perches 66fbf59bc9STejun Heo #include <linux/bitmap.h> 6757c8a661SMike Rapoport #include <linux/memblock.h> 68fd1e8a1fSTejun Heo #include <linux/err.h> 69ca460b3cSDennis Zhou (Facebook) #include <linux/lcm.h> 70fbf59bc9STejun Heo #include <linux/list.h> 71a530b795STejun Heo #include <linux/log2.h> 72fbf59bc9STejun Heo #include <linux/mm.h> 73fbf59bc9STejun Heo #include <linux/module.h> 74fbf59bc9STejun Heo #include <linux/mutex.h> 75fbf59bc9STejun Heo #include <linux/percpu.h> 76fbf59bc9STejun Heo #include <linux/pfn.h> 77fbf59bc9STejun Heo #include <linux/slab.h> 78ccea34b5STejun Heo #include <linux/spinlock.h> 79fbf59bc9STejun Heo #include <linux/vmalloc.h> 80a56dbddfSTejun Heo #include <linux/workqueue.h> 81f528f0b8SCatalin Marinas #include <linux/kmemleak.h> 8271546d10STejun Heo #include <linux/sched.h> 83fbf59bc9STejun Heo 84fbf59bc9STejun Heo #include <asm/cacheflush.h> 85e0100983STejun Heo #include <asm/sections.h> 86fbf59bc9STejun Heo #include <asm/tlbflush.h> 873b034b0dSVivek Goyal #include <asm/io.h> 88fbf59bc9STejun Heo 89df95e795SDennis Zhou #define CREATE_TRACE_POINTS 90df95e795SDennis Zhou #include <trace/events/percpu.h> 91df95e795SDennis Zhou 928fa3ed80SDennis Zhou #include "percpu-internal.h" 938fa3ed80SDennis Zhou 9440064aecSDennis Zhou (Facebook) /* the slots are sorted by free bytes left, 1-31 bytes share the same slot */ 9540064aecSDennis Zhou (Facebook) #define PCPU_SLOT_BASE_SHIFT 5 968744d859SDennis Zhou /* chunks in slots below this are subject to being sidelined on failed alloc */ 978744d859SDennis Zhou #define PCPU_SLOT_FAIL_THRESHOLD 3 9840064aecSDennis Zhou (Facebook) 991a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_LOW 2 1001a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_HIGH 4 101fbf59bc9STejun Heo 102bbddff05STejun Heo #ifdef CONFIG_SMP 103e0100983STejun Heo /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */ 104e0100983STejun Heo #ifndef __addr_to_pcpu_ptr 105e0100983STejun Heo #define __addr_to_pcpu_ptr(addr) \ 10643cf38ebSTejun Heo (void __percpu *)((unsigned long)(addr) - \ 10743cf38ebSTejun Heo (unsigned long)pcpu_base_addr + \ 10843cf38ebSTejun Heo (unsigned long)__per_cpu_start) 109e0100983STejun Heo #endif 110e0100983STejun Heo #ifndef __pcpu_ptr_to_addr 111e0100983STejun Heo #define __pcpu_ptr_to_addr(ptr) \ 11243cf38ebSTejun Heo (void __force *)((unsigned long)(ptr) + \ 11343cf38ebSTejun Heo (unsigned long)pcpu_base_addr - \ 11443cf38ebSTejun Heo (unsigned long)__per_cpu_start) 115e0100983STejun Heo #endif 116bbddff05STejun Heo #else /* CONFIG_SMP */ 117bbddff05STejun Heo /* on UP, it's always identity mapped */ 118bbddff05STejun Heo #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr) 119bbddff05STejun Heo #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr) 120bbddff05STejun Heo #endif /* CONFIG_SMP */ 121e0100983STejun Heo 1221328710bSDaniel Micay static int pcpu_unit_pages __ro_after_init; 1231328710bSDaniel Micay static int pcpu_unit_size __ro_after_init; 1241328710bSDaniel Micay static int pcpu_nr_units __ro_after_init; 1251328710bSDaniel Micay static int pcpu_atom_size __ro_after_init; 1268fa3ed80SDennis Zhou int pcpu_nr_slots __ro_after_init; 1271328710bSDaniel Micay static size_t pcpu_chunk_struct_size __ro_after_init; 128fbf59bc9STejun Heo 129a855b84cSTejun Heo /* cpus with the lowest and highest unit addresses */ 1301328710bSDaniel Micay static unsigned int pcpu_low_unit_cpu __ro_after_init; 1311328710bSDaniel Micay static unsigned int pcpu_high_unit_cpu __ro_after_init; 1322f39e637STejun Heo 133fbf59bc9STejun Heo /* the address of the first chunk which starts with the kernel static area */ 1341328710bSDaniel Micay void *pcpu_base_addr __ro_after_init; 135fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(pcpu_base_addr); 136fbf59bc9STejun Heo 1371328710bSDaniel Micay static const int *pcpu_unit_map __ro_after_init; /* cpu -> unit */ 1381328710bSDaniel Micay const unsigned long *pcpu_unit_offsets __ro_after_init; /* cpu -> unit offset */ 1392f39e637STejun Heo 1406563297cSTejun Heo /* group information, used for vm allocation */ 1411328710bSDaniel Micay static int pcpu_nr_groups __ro_after_init; 1421328710bSDaniel Micay static const unsigned long *pcpu_group_offsets __ro_after_init; 1431328710bSDaniel Micay static const size_t *pcpu_group_sizes __ro_after_init; 1446563297cSTejun Heo 145ae9e6bc9STejun Heo /* 146ae9e6bc9STejun Heo * The first chunk which always exists. Note that unlike other 147ae9e6bc9STejun Heo * chunks, this one can be allocated and mapped in several different 148ae9e6bc9STejun Heo * ways and thus often doesn't live in the vmalloc area. 149ae9e6bc9STejun Heo */ 1508fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_first_chunk __ro_after_init; 151ae9e6bc9STejun Heo 152ae9e6bc9STejun Heo /* 153ae9e6bc9STejun Heo * Optional reserved chunk. This chunk reserves part of the first 154e2266705SDennis Zhou (Facebook) * chunk and serves it for reserved allocations. When the reserved 155e2266705SDennis Zhou (Facebook) * region doesn't exist, the following variable is NULL. 156ae9e6bc9STejun Heo */ 1578fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_reserved_chunk __ro_after_init; 158edcb4639STejun Heo 1598fa3ed80SDennis Zhou DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */ 1606710e594STejun Heo static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */ 161fbf59bc9STejun Heo 1628fa3ed80SDennis Zhou struct list_head *pcpu_slot __ro_after_init; /* chunk list slots */ 163fbf59bc9STejun Heo 1644f996e23STejun Heo /* chunks which need their map areas extended, protected by pcpu_lock */ 1654f996e23STejun Heo static LIST_HEAD(pcpu_map_extend_chunks); 1664f996e23STejun Heo 167b539b87fSTejun Heo /* 168b539b87fSTejun Heo * The number of empty populated pages, protected by pcpu_lock. The 169b539b87fSTejun Heo * reserved chunk doesn't contribute to the count. 170b539b87fSTejun Heo */ 1716b9b6f39SDennis Zhou (Facebook) int pcpu_nr_empty_pop_pages; 172b539b87fSTejun Heo 1731a4d7607STejun Heo /* 1747e8a6304SDennis Zhou (Facebook) * The number of populated pages in use by the allocator, protected by 1757e8a6304SDennis Zhou (Facebook) * pcpu_lock. This number is kept per a unit per chunk (i.e. when a page gets 1767e8a6304SDennis Zhou (Facebook) * allocated/deallocated, it is allocated/deallocated in all units of a chunk 1777e8a6304SDennis Zhou (Facebook) * and increments/decrements this count by 1). 1787e8a6304SDennis Zhou (Facebook) */ 1797e8a6304SDennis Zhou (Facebook) static unsigned long pcpu_nr_populated; 1807e8a6304SDennis Zhou (Facebook) 1817e8a6304SDennis Zhou (Facebook) /* 1821a4d7607STejun Heo * Balance work is used to populate or destroy chunks asynchronously. We 1831a4d7607STejun Heo * try to keep the number of populated free pages between 1841a4d7607STejun Heo * PCPU_EMPTY_POP_PAGES_LOW and HIGH for atomic allocations and at most one 1851a4d7607STejun Heo * empty chunk. 1861a4d7607STejun Heo */ 187fe6bd8c3STejun Heo static void pcpu_balance_workfn(struct work_struct *work); 188fe6bd8c3STejun Heo static DECLARE_WORK(pcpu_balance_work, pcpu_balance_workfn); 1891a4d7607STejun Heo static bool pcpu_async_enabled __read_mostly; 1901a4d7607STejun Heo static bool pcpu_atomic_alloc_failed; 1911a4d7607STejun Heo 1921a4d7607STejun Heo static void pcpu_schedule_balance_work(void) 1931a4d7607STejun Heo { 1941a4d7607STejun Heo if (pcpu_async_enabled) 1951a4d7607STejun Heo schedule_work(&pcpu_balance_work); 1961a4d7607STejun Heo } 197a56dbddfSTejun Heo 198c0ebfdc3SDennis Zhou (Facebook) /** 199560f2c23SDennis Zhou (Facebook) * pcpu_addr_in_chunk - check if the address is served from this chunk 200560f2c23SDennis Zhou (Facebook) * @chunk: chunk of interest 201560f2c23SDennis Zhou (Facebook) * @addr: percpu address 202c0ebfdc3SDennis Zhou (Facebook) * 203c0ebfdc3SDennis Zhou (Facebook) * RETURNS: 204560f2c23SDennis Zhou (Facebook) * True if the address is served from this chunk. 205c0ebfdc3SDennis Zhou (Facebook) */ 206560f2c23SDennis Zhou (Facebook) static bool pcpu_addr_in_chunk(struct pcpu_chunk *chunk, void *addr) 207020ec653STejun Heo { 208c0ebfdc3SDennis Zhou (Facebook) void *start_addr, *end_addr; 209020ec653STejun Heo 210560f2c23SDennis Zhou (Facebook) if (!chunk) 211c0ebfdc3SDennis Zhou (Facebook) return false; 212c0ebfdc3SDennis Zhou (Facebook) 213560f2c23SDennis Zhou (Facebook) start_addr = chunk->base_addr + chunk->start_offset; 214560f2c23SDennis Zhou (Facebook) end_addr = chunk->base_addr + chunk->nr_pages * PAGE_SIZE - 215560f2c23SDennis Zhou (Facebook) chunk->end_offset; 216c0ebfdc3SDennis Zhou (Facebook) 217c0ebfdc3SDennis Zhou (Facebook) return addr >= start_addr && addr < end_addr; 218020ec653STejun Heo } 219020ec653STejun Heo 220d9b55eebSTejun Heo static int __pcpu_size_to_slot(int size) 221fbf59bc9STejun Heo { 222cae3aeb8STejun Heo int highbit = fls(size); /* size is in bytes */ 223fbf59bc9STejun Heo return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1); 224fbf59bc9STejun Heo } 225fbf59bc9STejun Heo 226d9b55eebSTejun Heo static int pcpu_size_to_slot(int size) 227d9b55eebSTejun Heo { 228d9b55eebSTejun Heo if (size == pcpu_unit_size) 229d9b55eebSTejun Heo return pcpu_nr_slots - 1; 230d9b55eebSTejun Heo return __pcpu_size_to_slot(size); 231d9b55eebSTejun Heo } 232d9b55eebSTejun Heo 233fbf59bc9STejun Heo static int pcpu_chunk_slot(const struct pcpu_chunk *chunk) 234fbf59bc9STejun Heo { 23592c14cabSDennis Zhou const struct pcpu_block_md *chunk_md = &chunk->chunk_md; 23692c14cabSDennis Zhou 23792c14cabSDennis Zhou if (chunk->free_bytes < PCPU_MIN_ALLOC_SIZE || 23892c14cabSDennis Zhou chunk_md->contig_hint == 0) 239fbf59bc9STejun Heo return 0; 240fbf59bc9STejun Heo 24192c14cabSDennis Zhou return pcpu_size_to_slot(chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE); 242fbf59bc9STejun Heo } 243fbf59bc9STejun Heo 24488999a89STejun Heo /* set the pointer to a chunk in a page struct */ 24588999a89STejun Heo static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu) 24688999a89STejun Heo { 24788999a89STejun Heo page->index = (unsigned long)pcpu; 24888999a89STejun Heo } 24988999a89STejun Heo 25088999a89STejun Heo /* obtain pointer to a chunk from a page struct */ 25188999a89STejun Heo static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page) 25288999a89STejun Heo { 25388999a89STejun Heo return (struct pcpu_chunk *)page->index; 25488999a89STejun Heo } 25588999a89STejun Heo 25688999a89STejun Heo static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx) 257fbf59bc9STejun Heo { 2582f39e637STejun Heo return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx; 259fbf59bc9STejun Heo } 260fbf59bc9STejun Heo 261c0ebfdc3SDennis Zhou (Facebook) static unsigned long pcpu_unit_page_offset(unsigned int cpu, int page_idx) 262c0ebfdc3SDennis Zhou (Facebook) { 263c0ebfdc3SDennis Zhou (Facebook) return pcpu_unit_offsets[cpu] + (page_idx << PAGE_SHIFT); 264c0ebfdc3SDennis Zhou (Facebook) } 265c0ebfdc3SDennis Zhou (Facebook) 2669983b6f0STejun Heo static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk, 267fbf59bc9STejun Heo unsigned int cpu, int page_idx) 268fbf59bc9STejun Heo { 269c0ebfdc3SDennis Zhou (Facebook) return (unsigned long)chunk->base_addr + 270c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(cpu, page_idx); 271fbf59bc9STejun Heo } 272fbf59bc9STejun Heo 27391e914c5SDennis Zhou (Facebook) static void pcpu_next_unpop(unsigned long *bitmap, int *rs, int *re, int end) 274ce3141a2STejun Heo { 27591e914c5SDennis Zhou (Facebook) *rs = find_next_zero_bit(bitmap, end, *rs); 27691e914c5SDennis Zhou (Facebook) *re = find_next_bit(bitmap, end, *rs + 1); 277ce3141a2STejun Heo } 278ce3141a2STejun Heo 27991e914c5SDennis Zhou (Facebook) static void pcpu_next_pop(unsigned long *bitmap, int *rs, int *re, int end) 280ce3141a2STejun Heo { 28191e914c5SDennis Zhou (Facebook) *rs = find_next_bit(bitmap, end, *rs); 28291e914c5SDennis Zhou (Facebook) *re = find_next_zero_bit(bitmap, end, *rs + 1); 283ce3141a2STejun Heo } 284ce3141a2STejun Heo 285ce3141a2STejun Heo /* 28691e914c5SDennis Zhou (Facebook) * Bitmap region iterators. Iterates over the bitmap between 28791e914c5SDennis Zhou (Facebook) * [@start, @end) in @chunk. @rs and @re should be integer variables 28891e914c5SDennis Zhou (Facebook) * and will be set to start and end index of the current free region. 289ce3141a2STejun Heo */ 29091e914c5SDennis Zhou (Facebook) #define pcpu_for_each_unpop_region(bitmap, rs, re, start, end) \ 29191e914c5SDennis Zhou (Facebook) for ((rs) = (start), pcpu_next_unpop((bitmap), &(rs), &(re), (end)); \ 292ce3141a2STejun Heo (rs) < (re); \ 29391e914c5SDennis Zhou (Facebook) (rs) = (re) + 1, pcpu_next_unpop((bitmap), &(rs), &(re), (end))) 294ce3141a2STejun Heo 29591e914c5SDennis Zhou (Facebook) #define pcpu_for_each_pop_region(bitmap, rs, re, start, end) \ 29691e914c5SDennis Zhou (Facebook) for ((rs) = (start), pcpu_next_pop((bitmap), &(rs), &(re), (end)); \ 297ce3141a2STejun Heo (rs) < (re); \ 29891e914c5SDennis Zhou (Facebook) (rs) = (re) + 1, pcpu_next_pop((bitmap), &(rs), &(re), (end))) 299ce3141a2STejun Heo 300ca460b3cSDennis Zhou (Facebook) /* 301ca460b3cSDennis Zhou (Facebook) * The following are helper functions to help access bitmaps and convert 302ca460b3cSDennis Zhou (Facebook) * between bitmap offsets to address offsets. 303ca460b3cSDennis Zhou (Facebook) */ 304ca460b3cSDennis Zhou (Facebook) static unsigned long *pcpu_index_alloc_map(struct pcpu_chunk *chunk, int index) 305ca460b3cSDennis Zhou (Facebook) { 306ca460b3cSDennis Zhou (Facebook) return chunk->alloc_map + 307ca460b3cSDennis Zhou (Facebook) (index * PCPU_BITMAP_BLOCK_BITS / BITS_PER_LONG); 308ca460b3cSDennis Zhou (Facebook) } 309ca460b3cSDennis Zhou (Facebook) 310ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_index(int off) 311ca460b3cSDennis Zhou (Facebook) { 312ca460b3cSDennis Zhou (Facebook) return off / PCPU_BITMAP_BLOCK_BITS; 313ca460b3cSDennis Zhou (Facebook) } 314ca460b3cSDennis Zhou (Facebook) 315ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_off(int off) 316ca460b3cSDennis Zhou (Facebook) { 317ca460b3cSDennis Zhou (Facebook) return off & (PCPU_BITMAP_BLOCK_BITS - 1); 318ca460b3cSDennis Zhou (Facebook) } 319ca460b3cSDennis Zhou (Facebook) 320b185cd0dSDennis Zhou (Facebook) static unsigned long pcpu_block_off_to_off(int index, int off) 321b185cd0dSDennis Zhou (Facebook) { 322b185cd0dSDennis Zhou (Facebook) return index * PCPU_BITMAP_BLOCK_BITS + off; 323b185cd0dSDennis Zhou (Facebook) } 324b185cd0dSDennis Zhou (Facebook) 325382b88e9SDennis Zhou /* 326382b88e9SDennis Zhou * pcpu_next_hint - determine which hint to use 327382b88e9SDennis Zhou * @block: block of interest 328382b88e9SDennis Zhou * @alloc_bits: size of allocation 329382b88e9SDennis Zhou * 330382b88e9SDennis Zhou * This determines if we should scan based on the scan_hint or first_free. 331382b88e9SDennis Zhou * In general, we want to scan from first_free to fulfill allocations by 332382b88e9SDennis Zhou * first fit. However, if we know a scan_hint at position scan_hint_start 333382b88e9SDennis Zhou * cannot fulfill an allocation, we can begin scanning from there knowing 334382b88e9SDennis Zhou * the contig_hint will be our fallback. 335382b88e9SDennis Zhou */ 336382b88e9SDennis Zhou static int pcpu_next_hint(struct pcpu_block_md *block, int alloc_bits) 337382b88e9SDennis Zhou { 338382b88e9SDennis Zhou /* 339382b88e9SDennis Zhou * The three conditions below determine if we can skip past the 340382b88e9SDennis Zhou * scan_hint. First, does the scan hint exist. Second, is the 341382b88e9SDennis Zhou * contig_hint after the scan_hint (possibly not true iff 342382b88e9SDennis Zhou * contig_hint == scan_hint). Third, is the allocation request 343382b88e9SDennis Zhou * larger than the scan_hint. 344382b88e9SDennis Zhou */ 345382b88e9SDennis Zhou if (block->scan_hint && 346382b88e9SDennis Zhou block->contig_hint_start > block->scan_hint_start && 347382b88e9SDennis Zhou alloc_bits > block->scan_hint) 348382b88e9SDennis Zhou return block->scan_hint_start + block->scan_hint; 349382b88e9SDennis Zhou 350382b88e9SDennis Zhou return block->first_free; 351382b88e9SDennis Zhou } 352382b88e9SDennis Zhou 353fbf59bc9STejun Heo /** 354525ca84dSDennis Zhou (Facebook) * pcpu_next_md_free_region - finds the next hint free area 355525ca84dSDennis Zhou (Facebook) * @chunk: chunk of interest 356525ca84dSDennis Zhou (Facebook) * @bit_off: chunk offset 357525ca84dSDennis Zhou (Facebook) * @bits: size of free area 358525ca84dSDennis Zhou (Facebook) * 359525ca84dSDennis Zhou (Facebook) * Helper function for pcpu_for_each_md_free_region. It checks 360525ca84dSDennis Zhou (Facebook) * block->contig_hint and performs aggregation across blocks to find the 361525ca84dSDennis Zhou (Facebook) * next hint. It modifies bit_off and bits in-place to be consumed in the 362525ca84dSDennis Zhou (Facebook) * loop. 363525ca84dSDennis Zhou (Facebook) */ 364525ca84dSDennis Zhou (Facebook) static void pcpu_next_md_free_region(struct pcpu_chunk *chunk, int *bit_off, 365525ca84dSDennis Zhou (Facebook) int *bits) 366525ca84dSDennis Zhou (Facebook) { 367525ca84dSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 368525ca84dSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 369525ca84dSDennis Zhou (Facebook) struct pcpu_block_md *block; 370525ca84dSDennis Zhou (Facebook) 371525ca84dSDennis Zhou (Facebook) *bits = 0; 372525ca84dSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 373525ca84dSDennis Zhou (Facebook) block++, i++) { 374525ca84dSDennis Zhou (Facebook) /* handles contig area across blocks */ 375525ca84dSDennis Zhou (Facebook) if (*bits) { 376525ca84dSDennis Zhou (Facebook) *bits += block->left_free; 377525ca84dSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 378525ca84dSDennis Zhou (Facebook) continue; 379525ca84dSDennis Zhou (Facebook) return; 380525ca84dSDennis Zhou (Facebook) } 381525ca84dSDennis Zhou (Facebook) 382525ca84dSDennis Zhou (Facebook) /* 383525ca84dSDennis Zhou (Facebook) * This checks three things. First is there a contig_hint to 384525ca84dSDennis Zhou (Facebook) * check. Second, have we checked this hint before by 385525ca84dSDennis Zhou (Facebook) * comparing the block_off. Third, is this the same as the 386525ca84dSDennis Zhou (Facebook) * right contig hint. In the last case, it spills over into 387525ca84dSDennis Zhou (Facebook) * the next block and should be handled by the contig area 388525ca84dSDennis Zhou (Facebook) * across blocks code. 389525ca84dSDennis Zhou (Facebook) */ 390525ca84dSDennis Zhou (Facebook) *bits = block->contig_hint; 391525ca84dSDennis Zhou (Facebook) if (*bits && block->contig_hint_start >= block_off && 392525ca84dSDennis Zhou (Facebook) *bits + block->contig_hint_start < PCPU_BITMAP_BLOCK_BITS) { 393525ca84dSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, 394525ca84dSDennis Zhou (Facebook) block->contig_hint_start); 395525ca84dSDennis Zhou (Facebook) return; 396525ca84dSDennis Zhou (Facebook) } 3971fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 3981fa4df3eSDennis Zhou block_off = 0; 399525ca84dSDennis Zhou (Facebook) 400525ca84dSDennis Zhou (Facebook) *bits = block->right_free; 401525ca84dSDennis Zhou (Facebook) *bit_off = (i + 1) * PCPU_BITMAP_BLOCK_BITS - block->right_free; 402525ca84dSDennis Zhou (Facebook) } 403525ca84dSDennis Zhou (Facebook) } 404525ca84dSDennis Zhou (Facebook) 405b4c2116cSDennis Zhou (Facebook) /** 406b4c2116cSDennis Zhou (Facebook) * pcpu_next_fit_region - finds fit areas for a given allocation request 407b4c2116cSDennis Zhou (Facebook) * @chunk: chunk of interest 408b4c2116cSDennis Zhou (Facebook) * @alloc_bits: size of allocation 409b4c2116cSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 410b4c2116cSDennis Zhou (Facebook) * @bit_off: chunk offset 411b4c2116cSDennis Zhou (Facebook) * @bits: size of free area 412b4c2116cSDennis Zhou (Facebook) * 413b4c2116cSDennis Zhou (Facebook) * Finds the next free region that is viable for use with a given size and 414b4c2116cSDennis Zhou (Facebook) * alignment. This only returns if there is a valid area to be used for this 415b4c2116cSDennis Zhou (Facebook) * allocation. block->first_free is returned if the allocation request fits 416b4c2116cSDennis Zhou (Facebook) * within the block to see if the request can be fulfilled prior to the contig 417b4c2116cSDennis Zhou (Facebook) * hint. 418b4c2116cSDennis Zhou (Facebook) */ 419b4c2116cSDennis Zhou (Facebook) static void pcpu_next_fit_region(struct pcpu_chunk *chunk, int alloc_bits, 420b4c2116cSDennis Zhou (Facebook) int align, int *bit_off, int *bits) 421b4c2116cSDennis Zhou (Facebook) { 422b4c2116cSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 423b4c2116cSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 424b4c2116cSDennis Zhou (Facebook) struct pcpu_block_md *block; 425b4c2116cSDennis Zhou (Facebook) 426b4c2116cSDennis Zhou (Facebook) *bits = 0; 427b4c2116cSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 428b4c2116cSDennis Zhou (Facebook) block++, i++) { 429b4c2116cSDennis Zhou (Facebook) /* handles contig area across blocks */ 430b4c2116cSDennis Zhou (Facebook) if (*bits) { 431b4c2116cSDennis Zhou (Facebook) *bits += block->left_free; 432b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 433b4c2116cSDennis Zhou (Facebook) return; 434b4c2116cSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 435b4c2116cSDennis Zhou (Facebook) continue; 436b4c2116cSDennis Zhou (Facebook) } 437b4c2116cSDennis Zhou (Facebook) 438b4c2116cSDennis Zhou (Facebook) /* check block->contig_hint */ 439b4c2116cSDennis Zhou (Facebook) *bits = ALIGN(block->contig_hint_start, align) - 440b4c2116cSDennis Zhou (Facebook) block->contig_hint_start; 441b4c2116cSDennis Zhou (Facebook) /* 442b4c2116cSDennis Zhou (Facebook) * This uses the block offset to determine if this has been 443b4c2116cSDennis Zhou (Facebook) * checked in the prior iteration. 444b4c2116cSDennis Zhou (Facebook) */ 445b4c2116cSDennis Zhou (Facebook) if (block->contig_hint && 446b4c2116cSDennis Zhou (Facebook) block->contig_hint_start >= block_off && 447b4c2116cSDennis Zhou (Facebook) block->contig_hint >= *bits + alloc_bits) { 448382b88e9SDennis Zhou int start = pcpu_next_hint(block, alloc_bits); 449382b88e9SDennis Zhou 450b4c2116cSDennis Zhou (Facebook) *bits += alloc_bits + block->contig_hint_start - 451382b88e9SDennis Zhou start; 452382b88e9SDennis Zhou *bit_off = pcpu_block_off_to_off(i, start); 453b4c2116cSDennis Zhou (Facebook) return; 454b4c2116cSDennis Zhou (Facebook) } 4551fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 4561fa4df3eSDennis Zhou block_off = 0; 457b4c2116cSDennis Zhou (Facebook) 458b4c2116cSDennis Zhou (Facebook) *bit_off = ALIGN(PCPU_BITMAP_BLOCK_BITS - block->right_free, 459b4c2116cSDennis Zhou (Facebook) align); 460b4c2116cSDennis Zhou (Facebook) *bits = PCPU_BITMAP_BLOCK_BITS - *bit_off; 461b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, *bit_off); 462b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 463b4c2116cSDennis Zhou (Facebook) return; 464b4c2116cSDennis Zhou (Facebook) } 465b4c2116cSDennis Zhou (Facebook) 466b4c2116cSDennis Zhou (Facebook) /* no valid offsets were found - fail condition */ 467b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_chunk_map_bits(chunk); 468b4c2116cSDennis Zhou (Facebook) } 469b4c2116cSDennis Zhou (Facebook) 470525ca84dSDennis Zhou (Facebook) /* 471525ca84dSDennis Zhou (Facebook) * Metadata free area iterators. These perform aggregation of free areas 472525ca84dSDennis Zhou (Facebook) * based on the metadata blocks and return the offset @bit_off and size in 473b4c2116cSDennis Zhou (Facebook) * bits of the free area @bits. pcpu_for_each_fit_region only returns when 474b4c2116cSDennis Zhou (Facebook) * a fit is found for the allocation request. 475525ca84dSDennis Zhou (Facebook) */ 476525ca84dSDennis Zhou (Facebook) #define pcpu_for_each_md_free_region(chunk, bit_off, bits) \ 477525ca84dSDennis Zhou (Facebook) for (pcpu_next_md_free_region((chunk), &(bit_off), &(bits)); \ 478525ca84dSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 479525ca84dSDennis Zhou (Facebook) (bit_off) += (bits) + 1, \ 480525ca84dSDennis Zhou (Facebook) pcpu_next_md_free_region((chunk), &(bit_off), &(bits))) 481525ca84dSDennis Zhou (Facebook) 482b4c2116cSDennis Zhou (Facebook) #define pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) \ 483b4c2116cSDennis Zhou (Facebook) for (pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 484b4c2116cSDennis Zhou (Facebook) &(bits)); \ 485b4c2116cSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 486b4c2116cSDennis Zhou (Facebook) (bit_off) += (bits), \ 487b4c2116cSDennis Zhou (Facebook) pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 488b4c2116cSDennis Zhou (Facebook) &(bits))) 489b4c2116cSDennis Zhou (Facebook) 490525ca84dSDennis Zhou (Facebook) /** 49190459ce0SBob Liu * pcpu_mem_zalloc - allocate memory 4921880d93bSTejun Heo * @size: bytes to allocate 49347504ee0SDennis Zhou * @gfp: allocation flags 494fbf59bc9STejun Heo * 4951880d93bSTejun Heo * Allocate @size bytes. If @size is smaller than PAGE_SIZE, 49647504ee0SDennis Zhou * kzalloc() is used; otherwise, the equivalent of vzalloc() is used. 49747504ee0SDennis Zhou * This is to facilitate passing through whitelisted flags. The 49847504ee0SDennis Zhou * returned memory is always zeroed. 499fbf59bc9STejun Heo * 500fbf59bc9STejun Heo * RETURNS: 5011880d93bSTejun Heo * Pointer to the allocated area on success, NULL on failure. 502fbf59bc9STejun Heo */ 50347504ee0SDennis Zhou static void *pcpu_mem_zalloc(size_t size, gfp_t gfp) 504fbf59bc9STejun Heo { 505099a19d9STejun Heo if (WARN_ON_ONCE(!slab_is_available())) 506099a19d9STejun Heo return NULL; 507099a19d9STejun Heo 508fbf59bc9STejun Heo if (size <= PAGE_SIZE) 509554fef1cSDennis Zhou return kzalloc(size, gfp); 5107af4c093SJesper Juhl else 511554fef1cSDennis Zhou return __vmalloc(size, gfp | __GFP_ZERO, PAGE_KERNEL); 5121880d93bSTejun Heo } 513fbf59bc9STejun Heo 5141880d93bSTejun Heo /** 5151880d93bSTejun Heo * pcpu_mem_free - free memory 5161880d93bSTejun Heo * @ptr: memory to free 5171880d93bSTejun Heo * 51890459ce0SBob Liu * Free @ptr. @ptr should have been allocated using pcpu_mem_zalloc(). 5191880d93bSTejun Heo */ 5201d5cfdb0STetsuo Handa static void pcpu_mem_free(void *ptr) 5211880d93bSTejun Heo { 5221d5cfdb0STetsuo Handa kvfree(ptr); 523fbf59bc9STejun Heo } 524fbf59bc9STejun Heo 5258744d859SDennis Zhou static void __pcpu_chunk_move(struct pcpu_chunk *chunk, int slot, 5268744d859SDennis Zhou bool move_front) 5278744d859SDennis Zhou { 5288744d859SDennis Zhou if (chunk != pcpu_reserved_chunk) { 5298744d859SDennis Zhou if (move_front) 5308744d859SDennis Zhou list_move(&chunk->list, &pcpu_slot[slot]); 5318744d859SDennis Zhou else 5328744d859SDennis Zhou list_move_tail(&chunk->list, &pcpu_slot[slot]); 5338744d859SDennis Zhou } 5348744d859SDennis Zhou } 5358744d859SDennis Zhou 5368744d859SDennis Zhou static void pcpu_chunk_move(struct pcpu_chunk *chunk, int slot) 5378744d859SDennis Zhou { 5388744d859SDennis Zhou __pcpu_chunk_move(chunk, slot, true); 5398744d859SDennis Zhou } 5408744d859SDennis Zhou 541fbf59bc9STejun Heo /** 542fbf59bc9STejun Heo * pcpu_chunk_relocate - put chunk in the appropriate chunk slot 543fbf59bc9STejun Heo * @chunk: chunk of interest 544fbf59bc9STejun Heo * @oslot: the previous slot it was on 545fbf59bc9STejun Heo * 546fbf59bc9STejun Heo * This function is called after an allocation or free changed @chunk. 547fbf59bc9STejun Heo * New slot according to the changed state is determined and @chunk is 548edcb4639STejun Heo * moved to the slot. Note that the reserved chunk is never put on 549edcb4639STejun Heo * chunk slots. 550ccea34b5STejun Heo * 551ccea34b5STejun Heo * CONTEXT: 552ccea34b5STejun Heo * pcpu_lock. 553fbf59bc9STejun Heo */ 554fbf59bc9STejun Heo static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) 555fbf59bc9STejun Heo { 556fbf59bc9STejun Heo int nslot = pcpu_chunk_slot(chunk); 557fbf59bc9STejun Heo 5588744d859SDennis Zhou if (oslot != nslot) 5598744d859SDennis Zhou __pcpu_chunk_move(chunk, nslot, oslot < nslot); 56040064aecSDennis Zhou (Facebook) } 56140064aecSDennis Zhou (Facebook) 56240064aecSDennis Zhou (Facebook) /* 563b239f7daSDennis Zhou * pcpu_update_empty_pages - update empty page counters 564b239f7daSDennis Zhou * @chunk: chunk of interest 565b239f7daSDennis Zhou * @nr: nr of empty pages 56640064aecSDennis Zhou (Facebook) * 567b239f7daSDennis Zhou * This is used to keep track of the empty pages now based on the premise 568b239f7daSDennis Zhou * a md_block covers a page. The hint update functions recognize if a block 569b239f7daSDennis Zhou * is made full or broken to calculate deltas for keeping track of free pages. 57040064aecSDennis Zhou (Facebook) */ 571b239f7daSDennis Zhou static inline void pcpu_update_empty_pages(struct pcpu_chunk *chunk, int nr) 572b239f7daSDennis Zhou { 573b239f7daSDennis Zhou chunk->nr_empty_pop_pages += nr; 57440064aecSDennis Zhou (Facebook) if (chunk != pcpu_reserved_chunk) 575b239f7daSDennis Zhou pcpu_nr_empty_pop_pages += nr; 57640064aecSDennis Zhou (Facebook) } 57740064aecSDennis Zhou (Facebook) 578d9f3a01eSDennis Zhou /* 579d9f3a01eSDennis Zhou * pcpu_region_overlap - determines if two regions overlap 580d9f3a01eSDennis Zhou * @a: start of first region, inclusive 581d9f3a01eSDennis Zhou * @b: end of first region, exclusive 582d9f3a01eSDennis Zhou * @x: start of second region, inclusive 583d9f3a01eSDennis Zhou * @y: end of second region, exclusive 584d9f3a01eSDennis Zhou * 585d9f3a01eSDennis Zhou * This is used to determine if the hint region [a, b) overlaps with the 586d9f3a01eSDennis Zhou * allocated region [x, y). 587d9f3a01eSDennis Zhou */ 588d9f3a01eSDennis Zhou static inline bool pcpu_region_overlap(int a, int b, int x, int y) 589d9f3a01eSDennis Zhou { 590d9f3a01eSDennis Zhou return (a < y) && (x < b); 59140064aecSDennis Zhou (Facebook) } 59240064aecSDennis Zhou (Facebook) 59340064aecSDennis Zhou (Facebook) /** 594ca460b3cSDennis Zhou (Facebook) * pcpu_block_update - updates a block given a free area 595ca460b3cSDennis Zhou (Facebook) * @block: block of interest 596ca460b3cSDennis Zhou (Facebook) * @start: start offset in block 597ca460b3cSDennis Zhou (Facebook) * @end: end offset in block 598ca460b3cSDennis Zhou (Facebook) * 599ca460b3cSDennis Zhou (Facebook) * Updates a block given a known free area. The region [start, end) is 600268625a6SDennis Zhou (Facebook) * expected to be the entirety of the free area within a block. Chooses 601268625a6SDennis Zhou (Facebook) * the best starting offset if the contig hints are equal. 602ca460b3cSDennis Zhou (Facebook) */ 603ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update(struct pcpu_block_md *block, int start, int end) 604ca460b3cSDennis Zhou (Facebook) { 605ca460b3cSDennis Zhou (Facebook) int contig = end - start; 606ca460b3cSDennis Zhou (Facebook) 607ca460b3cSDennis Zhou (Facebook) block->first_free = min(block->first_free, start); 608ca460b3cSDennis Zhou (Facebook) if (start == 0) 609ca460b3cSDennis Zhou (Facebook) block->left_free = contig; 610ca460b3cSDennis Zhou (Facebook) 611047924c9SDennis Zhou if (end == block->nr_bits) 612ca460b3cSDennis Zhou (Facebook) block->right_free = contig; 613ca460b3cSDennis Zhou (Facebook) 614ca460b3cSDennis Zhou (Facebook) if (contig > block->contig_hint) { 615382b88e9SDennis Zhou /* promote the old contig_hint to be the new scan_hint */ 616382b88e9SDennis Zhou if (start > block->contig_hint_start) { 617382b88e9SDennis Zhou if (block->contig_hint > block->scan_hint) { 618382b88e9SDennis Zhou block->scan_hint_start = 619382b88e9SDennis Zhou block->contig_hint_start; 620382b88e9SDennis Zhou block->scan_hint = block->contig_hint; 621382b88e9SDennis Zhou } else if (start < block->scan_hint_start) { 622382b88e9SDennis Zhou /* 623382b88e9SDennis Zhou * The old contig_hint == scan_hint. But, the 624382b88e9SDennis Zhou * new contig is larger so hold the invariant 625382b88e9SDennis Zhou * scan_hint_start < contig_hint_start. 626382b88e9SDennis Zhou */ 627382b88e9SDennis Zhou block->scan_hint = 0; 628382b88e9SDennis Zhou } 629382b88e9SDennis Zhou } else { 630382b88e9SDennis Zhou block->scan_hint = 0; 631382b88e9SDennis Zhou } 632ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = start; 633ca460b3cSDennis Zhou (Facebook) block->contig_hint = contig; 634382b88e9SDennis Zhou } else if (contig == block->contig_hint) { 635382b88e9SDennis Zhou if (block->contig_hint_start && 636382b88e9SDennis Zhou (!start || 637382b88e9SDennis Zhou __ffs(start) > __ffs(block->contig_hint_start))) { 638382b88e9SDennis Zhou /* start has a better alignment so use it */ 639268625a6SDennis Zhou (Facebook) block->contig_hint_start = start; 640382b88e9SDennis Zhou if (start < block->scan_hint_start && 641382b88e9SDennis Zhou block->contig_hint > block->scan_hint) 642382b88e9SDennis Zhou block->scan_hint = 0; 643382b88e9SDennis Zhou } else if (start > block->scan_hint_start || 644382b88e9SDennis Zhou block->contig_hint > block->scan_hint) { 645382b88e9SDennis Zhou /* 646382b88e9SDennis Zhou * Knowing contig == contig_hint, update the scan_hint 647382b88e9SDennis Zhou * if it is farther than or larger than the current 648382b88e9SDennis Zhou * scan_hint. 649382b88e9SDennis Zhou */ 650382b88e9SDennis Zhou block->scan_hint_start = start; 651382b88e9SDennis Zhou block->scan_hint = contig; 652382b88e9SDennis Zhou } 653382b88e9SDennis Zhou } else { 654382b88e9SDennis Zhou /* 655382b88e9SDennis Zhou * The region is smaller than the contig_hint. So only update 656382b88e9SDennis Zhou * the scan_hint if it is larger than or equal and farther than 657382b88e9SDennis Zhou * the current scan_hint. 658382b88e9SDennis Zhou */ 659382b88e9SDennis Zhou if ((start < block->contig_hint_start && 660382b88e9SDennis Zhou (contig > block->scan_hint || 661382b88e9SDennis Zhou (contig == block->scan_hint && 662382b88e9SDennis Zhou start > block->scan_hint_start)))) { 663382b88e9SDennis Zhou block->scan_hint_start = start; 664382b88e9SDennis Zhou block->scan_hint = contig; 665382b88e9SDennis Zhou } 666ca460b3cSDennis Zhou (Facebook) } 667ca460b3cSDennis Zhou (Facebook) } 668ca460b3cSDennis Zhou (Facebook) 669b89462a9SDennis Zhou /* 670b89462a9SDennis Zhou * pcpu_block_update_scan - update a block given a free area from a scan 671b89462a9SDennis Zhou * @chunk: chunk of interest 672b89462a9SDennis Zhou * @bit_off: chunk offset 673b89462a9SDennis Zhou * @bits: size of free area 674b89462a9SDennis Zhou * 675b89462a9SDennis Zhou * Finding the final allocation spot first goes through pcpu_find_block_fit() 676b89462a9SDennis Zhou * to find a block that can hold the allocation and then pcpu_alloc_area() 677b89462a9SDennis Zhou * where a scan is used. When allocations require specific alignments, 678b89462a9SDennis Zhou * we can inadvertently create holes which will not be seen in the alloc 679b89462a9SDennis Zhou * or free paths. 680b89462a9SDennis Zhou * 681b89462a9SDennis Zhou * This takes a given free area hole and updates a block as it may change the 682b89462a9SDennis Zhou * scan_hint. We need to scan backwards to ensure we don't miss free bits 683b89462a9SDennis Zhou * from alignment. 684b89462a9SDennis Zhou */ 685b89462a9SDennis Zhou static void pcpu_block_update_scan(struct pcpu_chunk *chunk, int bit_off, 686b89462a9SDennis Zhou int bits) 687b89462a9SDennis Zhou { 688b89462a9SDennis Zhou int s_off = pcpu_off_to_block_off(bit_off); 689b89462a9SDennis Zhou int e_off = s_off + bits; 690b89462a9SDennis Zhou int s_index, l_bit; 691b89462a9SDennis Zhou struct pcpu_block_md *block; 692b89462a9SDennis Zhou 693b89462a9SDennis Zhou if (e_off > PCPU_BITMAP_BLOCK_BITS) 694b89462a9SDennis Zhou return; 695b89462a9SDennis Zhou 696b89462a9SDennis Zhou s_index = pcpu_off_to_block_index(bit_off); 697b89462a9SDennis Zhou block = chunk->md_blocks + s_index; 698b89462a9SDennis Zhou 699b89462a9SDennis Zhou /* scan backwards in case of alignment skipping free bits */ 700b89462a9SDennis Zhou l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index), s_off); 701b89462a9SDennis Zhou s_off = (s_off == l_bit) ? 0 : l_bit + 1; 702b89462a9SDennis Zhou 703b89462a9SDennis Zhou pcpu_block_update(block, s_off, e_off); 704b89462a9SDennis Zhou } 705b89462a9SDennis Zhou 706ca460b3cSDennis Zhou (Facebook) /** 70792c14cabSDennis Zhou * pcpu_chunk_refresh_hint - updates metadata about a chunk 70892c14cabSDennis Zhou * @chunk: chunk of interest 709d33d9f3dSDennis Zhou * @full_scan: if we should scan from the beginning 71092c14cabSDennis Zhou * 71192c14cabSDennis Zhou * Iterates over the metadata blocks to find the largest contig area. 712d33d9f3dSDennis Zhou * A full scan can be avoided on the allocation path as this is triggered 713d33d9f3dSDennis Zhou * if we broke the contig_hint. In doing so, the scan_hint will be before 714d33d9f3dSDennis Zhou * the contig_hint or after if the scan_hint == contig_hint. This cannot 715d33d9f3dSDennis Zhou * be prevented on freeing as we want to find the largest area possibly 716d33d9f3dSDennis Zhou * spanning blocks. 71792c14cabSDennis Zhou */ 718d33d9f3dSDennis Zhou static void pcpu_chunk_refresh_hint(struct pcpu_chunk *chunk, bool full_scan) 71992c14cabSDennis Zhou { 72092c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 72192c14cabSDennis Zhou int bit_off, bits; 72292c14cabSDennis Zhou 723d33d9f3dSDennis Zhou /* promote scan_hint to contig_hint */ 724d33d9f3dSDennis Zhou if (!full_scan && chunk_md->scan_hint) { 725d33d9f3dSDennis Zhou bit_off = chunk_md->scan_hint_start + chunk_md->scan_hint; 726d33d9f3dSDennis Zhou chunk_md->contig_hint_start = chunk_md->scan_hint_start; 727d33d9f3dSDennis Zhou chunk_md->contig_hint = chunk_md->scan_hint; 728d33d9f3dSDennis Zhou chunk_md->scan_hint = 0; 729d33d9f3dSDennis Zhou } else { 73092c14cabSDennis Zhou bit_off = chunk_md->first_free; 731d33d9f3dSDennis Zhou chunk_md->contig_hint = 0; 732d33d9f3dSDennis Zhou } 733d33d9f3dSDennis Zhou 73492c14cabSDennis Zhou bits = 0; 73592c14cabSDennis Zhou pcpu_for_each_md_free_region(chunk, bit_off, bits) { 73692c14cabSDennis Zhou pcpu_block_update(chunk_md, bit_off, bit_off + bits); 737ca460b3cSDennis Zhou (Facebook) } 738ca460b3cSDennis Zhou (Facebook) } 739ca460b3cSDennis Zhou (Facebook) 740ca460b3cSDennis Zhou (Facebook) /** 741ca460b3cSDennis Zhou (Facebook) * pcpu_block_refresh_hint 742ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 743ca460b3cSDennis Zhou (Facebook) * @index: index of the metadata block 744ca460b3cSDennis Zhou (Facebook) * 745ca460b3cSDennis Zhou (Facebook) * Scans over the block beginning at first_free and updates the block 746ca460b3cSDennis Zhou (Facebook) * metadata accordingly. 747ca460b3cSDennis Zhou (Facebook) */ 748ca460b3cSDennis Zhou (Facebook) static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index) 749ca460b3cSDennis Zhou (Facebook) { 750ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *block = chunk->md_blocks + index; 751ca460b3cSDennis Zhou (Facebook) unsigned long *alloc_map = pcpu_index_alloc_map(chunk, index); 752da3afdd5SDennis Zhou int rs, re, start; /* region start, region end */ 753ca460b3cSDennis Zhou (Facebook) 754da3afdd5SDennis Zhou /* promote scan_hint to contig_hint */ 755da3afdd5SDennis Zhou if (block->scan_hint) { 756da3afdd5SDennis Zhou start = block->scan_hint_start + block->scan_hint; 757da3afdd5SDennis Zhou block->contig_hint_start = block->scan_hint_start; 758da3afdd5SDennis Zhou block->contig_hint = block->scan_hint; 759da3afdd5SDennis Zhou block->scan_hint = 0; 760da3afdd5SDennis Zhou } else { 761da3afdd5SDennis Zhou start = block->first_free; 762ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 763da3afdd5SDennis Zhou } 764da3afdd5SDennis Zhou 765da3afdd5SDennis Zhou block->right_free = 0; 766ca460b3cSDennis Zhou (Facebook) 767ca460b3cSDennis Zhou (Facebook) /* iterate over free areas and update the contig hints */ 768da3afdd5SDennis Zhou pcpu_for_each_unpop_region(alloc_map, rs, re, start, 769ca460b3cSDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS) { 770ca460b3cSDennis Zhou (Facebook) pcpu_block_update(block, rs, re); 771ca460b3cSDennis Zhou (Facebook) } 772ca460b3cSDennis Zhou (Facebook) } 773ca460b3cSDennis Zhou (Facebook) 774ca460b3cSDennis Zhou (Facebook) /** 775ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_alloc - update hint on allocation path 776ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 777ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 778ca460b3cSDennis Zhou (Facebook) * @bits: size of request 779fc304334SDennis Zhou (Facebook) * 780fc304334SDennis Zhou (Facebook) * Updates metadata for the allocation path. The metadata only has to be 781fc304334SDennis Zhou (Facebook) * refreshed by a full scan iff the chunk's contig hint is broken. Block level 782fc304334SDennis Zhou (Facebook) * scans are required if the block's contig hint is broken. 783ca460b3cSDennis Zhou (Facebook) */ 784ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_alloc(struct pcpu_chunk *chunk, int bit_off, 785ca460b3cSDennis Zhou (Facebook) int bits) 786ca460b3cSDennis Zhou (Facebook) { 78792c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 788b239f7daSDennis Zhou int nr_empty_pages = 0; 789ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 790ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 791ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 792ca460b3cSDennis Zhou (Facebook) 793ca460b3cSDennis Zhou (Facebook) /* 794ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 795ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 796ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 797ca460b3cSDennis Zhou (Facebook) * range. 798ca460b3cSDennis Zhou (Facebook) */ 799ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 800ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 801ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 802ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 803ca460b3cSDennis Zhou (Facebook) 804ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 805ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 806ca460b3cSDennis Zhou (Facebook) 807ca460b3cSDennis Zhou (Facebook) /* 808ca460b3cSDennis Zhou (Facebook) * Update s_block. 809fc304334SDennis Zhou (Facebook) * block->first_free must be updated if the allocation takes its place. 810fc304334SDennis Zhou (Facebook) * If the allocation breaks the contig_hint, a scan is required to 811fc304334SDennis Zhou (Facebook) * restore this hint. 812ca460b3cSDennis Zhou (Facebook) */ 813b239f7daSDennis Zhou if (s_block->contig_hint == PCPU_BITMAP_BLOCK_BITS) 814b239f7daSDennis Zhou nr_empty_pages++; 815b239f7daSDennis Zhou 816fc304334SDennis Zhou (Facebook) if (s_off == s_block->first_free) 817fc304334SDennis Zhou (Facebook) s_block->first_free = find_next_zero_bit( 818fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, s_index), 819fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, 820fc304334SDennis Zhou (Facebook) s_off + bits); 821fc304334SDennis Zhou (Facebook) 822382b88e9SDennis Zhou if (pcpu_region_overlap(s_block->scan_hint_start, 823382b88e9SDennis Zhou s_block->scan_hint_start + s_block->scan_hint, 824382b88e9SDennis Zhou s_off, 825382b88e9SDennis Zhou s_off + bits)) 826382b88e9SDennis Zhou s_block->scan_hint = 0; 827382b88e9SDennis Zhou 828d9f3a01eSDennis Zhou if (pcpu_region_overlap(s_block->contig_hint_start, 829d9f3a01eSDennis Zhou s_block->contig_hint_start + 830d9f3a01eSDennis Zhou s_block->contig_hint, 831d9f3a01eSDennis Zhou s_off, 832d9f3a01eSDennis Zhou s_off + bits)) { 833fc304334SDennis Zhou (Facebook) /* block contig hint is broken - scan to fix it */ 834da3afdd5SDennis Zhou if (!s_off) 835da3afdd5SDennis Zhou s_block->left_free = 0; 836ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, s_index); 837fc304334SDennis Zhou (Facebook) } else { 838fc304334SDennis Zhou (Facebook) /* update left and right contig manually */ 839fc304334SDennis Zhou (Facebook) s_block->left_free = min(s_block->left_free, s_off); 840fc304334SDennis Zhou (Facebook) if (s_index == e_index) 841fc304334SDennis Zhou (Facebook) s_block->right_free = min_t(int, s_block->right_free, 842fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 843fc304334SDennis Zhou (Facebook) else 844fc304334SDennis Zhou (Facebook) s_block->right_free = 0; 845fc304334SDennis Zhou (Facebook) } 846ca460b3cSDennis Zhou (Facebook) 847ca460b3cSDennis Zhou (Facebook) /* 848ca460b3cSDennis Zhou (Facebook) * Update e_block. 849ca460b3cSDennis Zhou (Facebook) */ 850ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 851b239f7daSDennis Zhou if (e_block->contig_hint == PCPU_BITMAP_BLOCK_BITS) 852b239f7daSDennis Zhou nr_empty_pages++; 853b239f7daSDennis Zhou 854fc304334SDennis Zhou (Facebook) /* 855fc304334SDennis Zhou (Facebook) * When the allocation is across blocks, the end is along 856fc304334SDennis Zhou (Facebook) * the left part of the e_block. 857fc304334SDennis Zhou (Facebook) */ 858fc304334SDennis Zhou (Facebook) e_block->first_free = find_next_zero_bit( 859fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, e_index), 860fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, e_off); 861fc304334SDennis Zhou (Facebook) 862fc304334SDennis Zhou (Facebook) if (e_off == PCPU_BITMAP_BLOCK_BITS) { 863fc304334SDennis Zhou (Facebook) /* reset the block */ 864fc304334SDennis Zhou (Facebook) e_block++; 865fc304334SDennis Zhou (Facebook) } else { 866382b88e9SDennis Zhou if (e_off > e_block->scan_hint_start) 867382b88e9SDennis Zhou e_block->scan_hint = 0; 868382b88e9SDennis Zhou 869da3afdd5SDennis Zhou e_block->left_free = 0; 870fc304334SDennis Zhou (Facebook) if (e_off > e_block->contig_hint_start) { 871fc304334SDennis Zhou (Facebook) /* contig hint is broken - scan to fix it */ 872ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, e_index); 873fc304334SDennis Zhou (Facebook) } else { 874fc304334SDennis Zhou (Facebook) e_block->right_free = 875fc304334SDennis Zhou (Facebook) min_t(int, e_block->right_free, 876fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 877fc304334SDennis Zhou (Facebook) } 878fc304334SDennis Zhou (Facebook) } 879ca460b3cSDennis Zhou (Facebook) 880ca460b3cSDennis Zhou (Facebook) /* update in-between md_blocks */ 881b239f7daSDennis Zhou nr_empty_pages += (e_index - s_index - 1); 882ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 883382b88e9SDennis Zhou block->scan_hint = 0; 884ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 885ca460b3cSDennis Zhou (Facebook) block->left_free = 0; 886ca460b3cSDennis Zhou (Facebook) block->right_free = 0; 887ca460b3cSDennis Zhou (Facebook) } 888ca460b3cSDennis Zhou (Facebook) } 889ca460b3cSDennis Zhou (Facebook) 890b239f7daSDennis Zhou if (nr_empty_pages) 891b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, -nr_empty_pages); 892b239f7daSDennis Zhou 893d33d9f3dSDennis Zhou if (pcpu_region_overlap(chunk_md->scan_hint_start, 894d33d9f3dSDennis Zhou chunk_md->scan_hint_start + 895d33d9f3dSDennis Zhou chunk_md->scan_hint, 896d33d9f3dSDennis Zhou bit_off, 897d33d9f3dSDennis Zhou bit_off + bits)) 898d33d9f3dSDennis Zhou chunk_md->scan_hint = 0; 899d33d9f3dSDennis Zhou 900fc304334SDennis Zhou (Facebook) /* 901fc304334SDennis Zhou (Facebook) * The only time a full chunk scan is required is if the chunk 902fc304334SDennis Zhou (Facebook) * contig hint is broken. Otherwise, it means a smaller space 903fc304334SDennis Zhou (Facebook) * was used and therefore the chunk contig hint is still correct. 904fc304334SDennis Zhou (Facebook) */ 90592c14cabSDennis Zhou if (pcpu_region_overlap(chunk_md->contig_hint_start, 90692c14cabSDennis Zhou chunk_md->contig_hint_start + 90792c14cabSDennis Zhou chunk_md->contig_hint, 908d9f3a01eSDennis Zhou bit_off, 909d9f3a01eSDennis Zhou bit_off + bits)) 910d33d9f3dSDennis Zhou pcpu_chunk_refresh_hint(chunk, false); 911ca460b3cSDennis Zhou (Facebook) } 912ca460b3cSDennis Zhou (Facebook) 913ca460b3cSDennis Zhou (Facebook) /** 914ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_free - updates the block hints on the free path 915ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 916ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 917ca460b3cSDennis Zhou (Facebook) * @bits: size of request 918b185cd0dSDennis Zhou (Facebook) * 919b185cd0dSDennis Zhou (Facebook) * Updates metadata for the allocation path. This avoids a blind block 920b185cd0dSDennis Zhou (Facebook) * refresh by making use of the block contig hints. If this fails, it scans 921b185cd0dSDennis Zhou (Facebook) * forward and backward to determine the extent of the free area. This is 922b185cd0dSDennis Zhou (Facebook) * capped at the boundary of blocks. 923b185cd0dSDennis Zhou (Facebook) * 924b185cd0dSDennis Zhou (Facebook) * A chunk update is triggered if a page becomes free, a block becomes free, 925b185cd0dSDennis Zhou (Facebook) * or the free spans across blocks. This tradeoff is to minimize iterating 92692c14cabSDennis Zhou * over the block metadata to update chunk_md->contig_hint. 92792c14cabSDennis Zhou * chunk_md->contig_hint may be off by up to a page, but it will never be more 92892c14cabSDennis Zhou * than the available space. If the contig hint is contained in one block, it 92992c14cabSDennis Zhou * will be accurate. 930ca460b3cSDennis Zhou (Facebook) */ 931ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_free(struct pcpu_chunk *chunk, int bit_off, 932ca460b3cSDennis Zhou (Facebook) int bits) 933ca460b3cSDennis Zhou (Facebook) { 934b239f7daSDennis Zhou int nr_empty_pages = 0; 935ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 936ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 937ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 938b185cd0dSDennis Zhou (Facebook) int start, end; /* start and end of the whole free area */ 939ca460b3cSDennis Zhou (Facebook) 940ca460b3cSDennis Zhou (Facebook) /* 941ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 942ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 943ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 944ca460b3cSDennis Zhou (Facebook) * range. 945ca460b3cSDennis Zhou (Facebook) */ 946ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 947ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 948ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 949ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 950ca460b3cSDennis Zhou (Facebook) 951ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 952ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 953ca460b3cSDennis Zhou (Facebook) 954b185cd0dSDennis Zhou (Facebook) /* 955b185cd0dSDennis Zhou (Facebook) * Check if the freed area aligns with the block->contig_hint. 956b185cd0dSDennis Zhou (Facebook) * If it does, then the scan to find the beginning/end of the 957b185cd0dSDennis Zhou (Facebook) * larger free area can be avoided. 958b185cd0dSDennis Zhou (Facebook) * 959b185cd0dSDennis Zhou (Facebook) * start and end refer to beginning and end of the free area 960b185cd0dSDennis Zhou (Facebook) * within each their respective blocks. This is not necessarily 961b185cd0dSDennis Zhou (Facebook) * the entire free area as it may span blocks past the beginning 962b185cd0dSDennis Zhou (Facebook) * or end of the block. 963b185cd0dSDennis Zhou (Facebook) */ 964b185cd0dSDennis Zhou (Facebook) start = s_off; 965b185cd0dSDennis Zhou (Facebook) if (s_off == s_block->contig_hint + s_block->contig_hint_start) { 966b185cd0dSDennis Zhou (Facebook) start = s_block->contig_hint_start; 967b185cd0dSDennis Zhou (Facebook) } else { 968b185cd0dSDennis Zhou (Facebook) /* 969b185cd0dSDennis Zhou (Facebook) * Scan backwards to find the extent of the free area. 970b185cd0dSDennis Zhou (Facebook) * find_last_bit returns the starting bit, so if the start bit 971b185cd0dSDennis Zhou (Facebook) * is returned, that means there was no last bit and the 972b185cd0dSDennis Zhou (Facebook) * remainder of the chunk is free. 973b185cd0dSDennis Zhou (Facebook) */ 974b185cd0dSDennis Zhou (Facebook) int l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index), 975b185cd0dSDennis Zhou (Facebook) start); 976b185cd0dSDennis Zhou (Facebook) start = (start == l_bit) ? 0 : l_bit + 1; 977b185cd0dSDennis Zhou (Facebook) } 978b185cd0dSDennis Zhou (Facebook) 979b185cd0dSDennis Zhou (Facebook) end = e_off; 980b185cd0dSDennis Zhou (Facebook) if (e_off == e_block->contig_hint_start) 981b185cd0dSDennis Zhou (Facebook) end = e_block->contig_hint_start + e_block->contig_hint; 982b185cd0dSDennis Zhou (Facebook) else 983b185cd0dSDennis Zhou (Facebook) end = find_next_bit(pcpu_index_alloc_map(chunk, e_index), 984b185cd0dSDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, end); 985b185cd0dSDennis Zhou (Facebook) 986ca460b3cSDennis Zhou (Facebook) /* update s_block */ 987b185cd0dSDennis Zhou (Facebook) e_off = (s_index == e_index) ? end : PCPU_BITMAP_BLOCK_BITS; 988b239f7daSDennis Zhou if (!start && e_off == PCPU_BITMAP_BLOCK_BITS) 989b239f7daSDennis Zhou nr_empty_pages++; 990b185cd0dSDennis Zhou (Facebook) pcpu_block_update(s_block, start, e_off); 991ca460b3cSDennis Zhou (Facebook) 992ca460b3cSDennis Zhou (Facebook) /* freeing in the same block */ 993ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 994ca460b3cSDennis Zhou (Facebook) /* update e_block */ 995b239f7daSDennis Zhou if (end == PCPU_BITMAP_BLOCK_BITS) 996b239f7daSDennis Zhou nr_empty_pages++; 997b185cd0dSDennis Zhou (Facebook) pcpu_block_update(e_block, 0, end); 998ca460b3cSDennis Zhou (Facebook) 999ca460b3cSDennis Zhou (Facebook) /* reset md_blocks in the middle */ 1000b239f7daSDennis Zhou nr_empty_pages += (e_index - s_index - 1); 1001ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 1002ca460b3cSDennis Zhou (Facebook) block->first_free = 0; 1003382b88e9SDennis Zhou block->scan_hint = 0; 1004ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = 0; 1005ca460b3cSDennis Zhou (Facebook) block->contig_hint = PCPU_BITMAP_BLOCK_BITS; 1006ca460b3cSDennis Zhou (Facebook) block->left_free = PCPU_BITMAP_BLOCK_BITS; 1007ca460b3cSDennis Zhou (Facebook) block->right_free = PCPU_BITMAP_BLOCK_BITS; 1008ca460b3cSDennis Zhou (Facebook) } 1009ca460b3cSDennis Zhou (Facebook) } 1010ca460b3cSDennis Zhou (Facebook) 1011b239f7daSDennis Zhou if (nr_empty_pages) 1012b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, nr_empty_pages); 1013b239f7daSDennis Zhou 1014b185cd0dSDennis Zhou (Facebook) /* 1015b239f7daSDennis Zhou * Refresh chunk metadata when the free makes a block free or spans 1016b239f7daSDennis Zhou * across blocks. The contig_hint may be off by up to a page, but if 1017b239f7daSDennis Zhou * the contig_hint is contained in a block, it will be accurate with 1018b239f7daSDennis Zhou * the else condition below. 1019b185cd0dSDennis Zhou (Facebook) */ 1020b239f7daSDennis Zhou if (((end - start) >= PCPU_BITMAP_BLOCK_BITS) || s_index != e_index) 1021d33d9f3dSDennis Zhou pcpu_chunk_refresh_hint(chunk, true); 1022b185cd0dSDennis Zhou (Facebook) else 102392c14cabSDennis Zhou pcpu_block_update(&chunk->chunk_md, 102492c14cabSDennis Zhou pcpu_block_off_to_off(s_index, start), 102592c14cabSDennis Zhou end); 1026ca460b3cSDennis Zhou (Facebook) } 1027ca460b3cSDennis Zhou (Facebook) 1028ca460b3cSDennis Zhou (Facebook) /** 102940064aecSDennis Zhou (Facebook) * pcpu_is_populated - determines if the region is populated 103040064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 103140064aecSDennis Zhou (Facebook) * @bit_off: chunk offset 103240064aecSDennis Zhou (Facebook) * @bits: size of area 103340064aecSDennis Zhou (Facebook) * @next_off: return value for the next offset to start searching 103440064aecSDennis Zhou (Facebook) * 103540064aecSDennis Zhou (Facebook) * For atomic allocations, check if the backing pages are populated. 103640064aecSDennis Zhou (Facebook) * 103740064aecSDennis Zhou (Facebook) * RETURNS: 103840064aecSDennis Zhou (Facebook) * Bool if the backing pages are populated. 103940064aecSDennis Zhou (Facebook) * next_index is to skip over unpopulated blocks in pcpu_find_block_fit. 104040064aecSDennis Zhou (Facebook) */ 104140064aecSDennis Zhou (Facebook) static bool pcpu_is_populated(struct pcpu_chunk *chunk, int bit_off, int bits, 104240064aecSDennis Zhou (Facebook) int *next_off) 104340064aecSDennis Zhou (Facebook) { 104440064aecSDennis Zhou (Facebook) int page_start, page_end, rs, re; 104540064aecSDennis Zhou (Facebook) 104640064aecSDennis Zhou (Facebook) page_start = PFN_DOWN(bit_off * PCPU_MIN_ALLOC_SIZE); 104740064aecSDennis Zhou (Facebook) page_end = PFN_UP((bit_off + bits) * PCPU_MIN_ALLOC_SIZE); 104840064aecSDennis Zhou (Facebook) 104940064aecSDennis Zhou (Facebook) rs = page_start; 105040064aecSDennis Zhou (Facebook) pcpu_next_unpop(chunk->populated, &rs, &re, page_end); 105140064aecSDennis Zhou (Facebook) if (rs >= page_end) 105240064aecSDennis Zhou (Facebook) return true; 105340064aecSDennis Zhou (Facebook) 105440064aecSDennis Zhou (Facebook) *next_off = re * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE; 105540064aecSDennis Zhou (Facebook) return false; 105640064aecSDennis Zhou (Facebook) } 105740064aecSDennis Zhou (Facebook) 105840064aecSDennis Zhou (Facebook) /** 105940064aecSDennis Zhou (Facebook) * pcpu_find_block_fit - finds the block index to start searching 106040064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 106140064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 106240064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE bytes) 106340064aecSDennis Zhou (Facebook) * @pop_only: use populated regions only 106440064aecSDennis Zhou (Facebook) * 1065b4c2116cSDennis Zhou (Facebook) * Given a chunk and an allocation spec, find the offset to begin searching 1066b4c2116cSDennis Zhou (Facebook) * for a free region. This iterates over the bitmap metadata blocks to 1067b4c2116cSDennis Zhou (Facebook) * find an offset that will be guaranteed to fit the requirements. It is 1068b4c2116cSDennis Zhou (Facebook) * not quite first fit as if the allocation does not fit in the contig hint 1069b4c2116cSDennis Zhou (Facebook) * of a block or chunk, it is skipped. This errs on the side of caution 1070b4c2116cSDennis Zhou (Facebook) * to prevent excess iteration. Poor alignment can cause the allocator to 1071b4c2116cSDennis Zhou (Facebook) * skip over blocks and chunks that have valid free areas. 1072b4c2116cSDennis Zhou (Facebook) * 107340064aecSDennis Zhou (Facebook) * RETURNS: 107440064aecSDennis Zhou (Facebook) * The offset in the bitmap to begin searching. 107540064aecSDennis Zhou (Facebook) * -1 if no offset is found. 107640064aecSDennis Zhou (Facebook) */ 107740064aecSDennis Zhou (Facebook) static int pcpu_find_block_fit(struct pcpu_chunk *chunk, int alloc_bits, 107840064aecSDennis Zhou (Facebook) size_t align, bool pop_only) 107940064aecSDennis Zhou (Facebook) { 108092c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 1081b4c2116cSDennis Zhou (Facebook) int bit_off, bits, next_off; 108240064aecSDennis Zhou (Facebook) 108313f96637SDennis Zhou (Facebook) /* 108413f96637SDennis Zhou (Facebook) * Check to see if the allocation can fit in the chunk's contig hint. 108513f96637SDennis Zhou (Facebook) * This is an optimization to prevent scanning by assuming if it 108613f96637SDennis Zhou (Facebook) * cannot fit in the global hint, there is memory pressure and creating 108713f96637SDennis Zhou (Facebook) * a new chunk would happen soon. 108813f96637SDennis Zhou (Facebook) */ 108992c14cabSDennis Zhou bit_off = ALIGN(chunk_md->contig_hint_start, align) - 109092c14cabSDennis Zhou chunk_md->contig_hint_start; 109192c14cabSDennis Zhou if (bit_off + alloc_bits > chunk_md->contig_hint) 109213f96637SDennis Zhou (Facebook) return -1; 109313f96637SDennis Zhou (Facebook) 1094d33d9f3dSDennis Zhou bit_off = pcpu_next_hint(chunk_md, alloc_bits); 1095b4c2116cSDennis Zhou (Facebook) bits = 0; 1096b4c2116cSDennis Zhou (Facebook) pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) { 109740064aecSDennis Zhou (Facebook) if (!pop_only || pcpu_is_populated(chunk, bit_off, bits, 1098b4c2116cSDennis Zhou (Facebook) &next_off)) 109940064aecSDennis Zhou (Facebook) break; 110040064aecSDennis Zhou (Facebook) 1101b4c2116cSDennis Zhou (Facebook) bit_off = next_off; 110240064aecSDennis Zhou (Facebook) bits = 0; 110340064aecSDennis Zhou (Facebook) } 110440064aecSDennis Zhou (Facebook) 110540064aecSDennis Zhou (Facebook) if (bit_off == pcpu_chunk_map_bits(chunk)) 110640064aecSDennis Zhou (Facebook) return -1; 110740064aecSDennis Zhou (Facebook) 110840064aecSDennis Zhou (Facebook) return bit_off; 110940064aecSDennis Zhou (Facebook) } 111040064aecSDennis Zhou (Facebook) 1111b89462a9SDennis Zhou /* 1112b89462a9SDennis Zhou * pcpu_find_zero_area - modified from bitmap_find_next_zero_area_off() 1113b89462a9SDennis Zhou * @map: the address to base the search on 1114b89462a9SDennis Zhou * @size: the bitmap size in bits 1115b89462a9SDennis Zhou * @start: the bitnumber to start searching at 1116b89462a9SDennis Zhou * @nr: the number of zeroed bits we're looking for 1117b89462a9SDennis Zhou * @align_mask: alignment mask for zero area 1118b89462a9SDennis Zhou * @largest_off: offset of the largest area skipped 1119b89462a9SDennis Zhou * @largest_bits: size of the largest area skipped 1120b89462a9SDennis Zhou * 1121b89462a9SDennis Zhou * The @align_mask should be one less than a power of 2. 1122b89462a9SDennis Zhou * 1123b89462a9SDennis Zhou * This is a modified version of bitmap_find_next_zero_area_off() to remember 1124b89462a9SDennis Zhou * the largest area that was skipped. This is imperfect, but in general is 1125b89462a9SDennis Zhou * good enough. The largest remembered region is the largest failed region 1126b89462a9SDennis Zhou * seen. This does not include anything we possibly skipped due to alignment. 1127b89462a9SDennis Zhou * pcpu_block_update_scan() does scan backwards to try and recover what was 1128b89462a9SDennis Zhou * lost to alignment. While this can cause scanning to miss earlier possible 1129b89462a9SDennis Zhou * free areas, smaller allocations will eventually fill those holes. 1130b89462a9SDennis Zhou */ 1131b89462a9SDennis Zhou static unsigned long pcpu_find_zero_area(unsigned long *map, 1132b89462a9SDennis Zhou unsigned long size, 1133b89462a9SDennis Zhou unsigned long start, 1134b89462a9SDennis Zhou unsigned long nr, 1135b89462a9SDennis Zhou unsigned long align_mask, 1136b89462a9SDennis Zhou unsigned long *largest_off, 1137b89462a9SDennis Zhou unsigned long *largest_bits) 1138b89462a9SDennis Zhou { 1139b89462a9SDennis Zhou unsigned long index, end, i, area_off, area_bits; 1140b89462a9SDennis Zhou again: 1141b89462a9SDennis Zhou index = find_next_zero_bit(map, size, start); 1142b89462a9SDennis Zhou 1143b89462a9SDennis Zhou /* Align allocation */ 1144b89462a9SDennis Zhou index = __ALIGN_MASK(index, align_mask); 1145b89462a9SDennis Zhou area_off = index; 1146b89462a9SDennis Zhou 1147b89462a9SDennis Zhou end = index + nr; 1148b89462a9SDennis Zhou if (end > size) 1149b89462a9SDennis Zhou return end; 1150b89462a9SDennis Zhou i = find_next_bit(map, end, index); 1151b89462a9SDennis Zhou if (i < end) { 1152b89462a9SDennis Zhou area_bits = i - area_off; 1153b89462a9SDennis Zhou /* remember largest unused area with best alignment */ 1154b89462a9SDennis Zhou if (area_bits > *largest_bits || 1155b89462a9SDennis Zhou (area_bits == *largest_bits && *largest_off && 1156b89462a9SDennis Zhou (!area_off || __ffs(area_off) > __ffs(*largest_off)))) { 1157b89462a9SDennis Zhou *largest_off = area_off; 1158b89462a9SDennis Zhou *largest_bits = area_bits; 1159b89462a9SDennis Zhou } 1160b89462a9SDennis Zhou 1161b89462a9SDennis Zhou start = i + 1; 1162b89462a9SDennis Zhou goto again; 1163b89462a9SDennis Zhou } 1164b89462a9SDennis Zhou return index; 1165b89462a9SDennis Zhou } 1166b89462a9SDennis Zhou 116740064aecSDennis Zhou (Facebook) /** 116840064aecSDennis Zhou (Facebook) * pcpu_alloc_area - allocates an area from a pcpu_chunk 116940064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 117040064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 117140064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 117240064aecSDennis Zhou (Facebook) * @start: bit_off to start searching 117340064aecSDennis Zhou (Facebook) * 117440064aecSDennis Zhou (Facebook) * This function takes in a @start offset to begin searching to fit an 1175b4c2116cSDennis Zhou (Facebook) * allocation of @alloc_bits with alignment @align. It needs to scan 1176b4c2116cSDennis Zhou (Facebook) * the allocation map because if it fits within the block's contig hint, 1177b4c2116cSDennis Zhou (Facebook) * @start will be block->first_free. This is an attempt to fill the 1178b4c2116cSDennis Zhou (Facebook) * allocation prior to breaking the contig hint. The allocation and 1179b4c2116cSDennis Zhou (Facebook) * boundary maps are updated accordingly if it confirms a valid 1180b4c2116cSDennis Zhou (Facebook) * free area. 118140064aecSDennis Zhou (Facebook) * 118240064aecSDennis Zhou (Facebook) * RETURNS: 118340064aecSDennis Zhou (Facebook) * Allocated addr offset in @chunk on success. 118440064aecSDennis Zhou (Facebook) * -1 if no matching area is found. 118540064aecSDennis Zhou (Facebook) */ 118640064aecSDennis Zhou (Facebook) static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits, 118740064aecSDennis Zhou (Facebook) size_t align, int start) 118840064aecSDennis Zhou (Facebook) { 118992c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 119040064aecSDennis Zhou (Facebook) size_t align_mask = (align) ? (align - 1) : 0; 1191b89462a9SDennis Zhou unsigned long area_off = 0, area_bits = 0; 119240064aecSDennis Zhou (Facebook) int bit_off, end, oslot; 11939f7dcf22STejun Heo 11944f996e23STejun Heo lockdep_assert_held(&pcpu_lock); 11954f996e23STejun Heo 119640064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 1197833af842STejun Heo 1198833af842STejun Heo /* 119940064aecSDennis Zhou (Facebook) * Search to find a fit. 1200833af842STejun Heo */ 12018c43004aSDennis Zhou end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS, 12028c43004aSDennis Zhou pcpu_chunk_map_bits(chunk)); 1203b89462a9SDennis Zhou bit_off = pcpu_find_zero_area(chunk->alloc_map, end, start, alloc_bits, 1204b89462a9SDennis Zhou align_mask, &area_off, &area_bits); 120540064aecSDennis Zhou (Facebook) if (bit_off >= end) 1206a16037c8STejun Heo return -1; 1207a16037c8STejun Heo 1208b89462a9SDennis Zhou if (area_bits) 1209b89462a9SDennis Zhou pcpu_block_update_scan(chunk, area_off, area_bits); 1210b89462a9SDennis Zhou 121140064aecSDennis Zhou (Facebook) /* update alloc map */ 121240064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, bit_off, alloc_bits); 1213a16037c8STejun Heo 121440064aecSDennis Zhou (Facebook) /* update boundary map */ 121540064aecSDennis Zhou (Facebook) set_bit(bit_off, chunk->bound_map); 121640064aecSDennis Zhou (Facebook) bitmap_clear(chunk->bound_map, bit_off + 1, alloc_bits - 1); 121740064aecSDennis Zhou (Facebook) set_bit(bit_off + alloc_bits, chunk->bound_map); 1218a16037c8STejun Heo 121940064aecSDennis Zhou (Facebook) chunk->free_bytes -= alloc_bits * PCPU_MIN_ALLOC_SIZE; 122040064aecSDennis Zhou (Facebook) 122186b442fbSDennis Zhou (Facebook) /* update first free bit */ 122292c14cabSDennis Zhou if (bit_off == chunk_md->first_free) 122392c14cabSDennis Zhou chunk_md->first_free = find_next_zero_bit( 122486b442fbSDennis Zhou (Facebook) chunk->alloc_map, 122586b442fbSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk), 122686b442fbSDennis Zhou (Facebook) bit_off + alloc_bits); 122786b442fbSDennis Zhou (Facebook) 1228ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, bit_off, alloc_bits); 122940064aecSDennis Zhou (Facebook) 123040064aecSDennis Zhou (Facebook) pcpu_chunk_relocate(chunk, oslot); 123140064aecSDennis Zhou (Facebook) 123240064aecSDennis Zhou (Facebook) return bit_off * PCPU_MIN_ALLOC_SIZE; 1233a16037c8STejun Heo } 1234a16037c8STejun Heo 1235a16037c8STejun Heo /** 123640064aecSDennis Zhou (Facebook) * pcpu_free_area - frees the corresponding offset 1237fbf59bc9STejun Heo * @chunk: chunk of interest 123840064aecSDennis Zhou (Facebook) * @off: addr offset into chunk 1239fbf59bc9STejun Heo * 124040064aecSDennis Zhou (Facebook) * This function determines the size of an allocation to free using 124140064aecSDennis Zhou (Facebook) * the boundary bitmap and clears the allocation map. 1242fbf59bc9STejun Heo */ 124340064aecSDennis Zhou (Facebook) static void pcpu_free_area(struct pcpu_chunk *chunk, int off) 1244fbf59bc9STejun Heo { 124592c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 124640064aecSDennis Zhou (Facebook) int bit_off, bits, end, oslot; 1247fbf59bc9STejun Heo 12485ccd30e4SDennis Zhou lockdep_assert_held(&pcpu_lock); 124930a5b536SDennis Zhou pcpu_stats_area_dealloc(chunk); 12505ccd30e4SDennis Zhou 125140064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 1252723ad1d9SAl Viro 125340064aecSDennis Zhou (Facebook) bit_off = off / PCPU_MIN_ALLOC_SIZE; 1254fbf59bc9STejun Heo 125540064aecSDennis Zhou (Facebook) /* find end index */ 125640064aecSDennis Zhou (Facebook) end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), 125740064aecSDennis Zhou (Facebook) bit_off + 1); 125840064aecSDennis Zhou (Facebook) bits = end - bit_off; 125940064aecSDennis Zhou (Facebook) bitmap_clear(chunk->alloc_map, bit_off, bits); 12603d331ad7SAl Viro 126140064aecSDennis Zhou (Facebook) /* update metadata */ 126240064aecSDennis Zhou (Facebook) chunk->free_bytes += bits * PCPU_MIN_ALLOC_SIZE; 1263fbf59bc9STejun Heo 126486b442fbSDennis Zhou (Facebook) /* update first free bit */ 126592c14cabSDennis Zhou chunk_md->first_free = min(chunk_md->first_free, bit_off); 126686b442fbSDennis Zhou (Facebook) 1267ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_free(chunk, bit_off, bits); 1268b539b87fSTejun Heo 1269fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 1270fbf59bc9STejun Heo } 1271fbf59bc9STejun Heo 1272047924c9SDennis Zhou static void pcpu_init_md_block(struct pcpu_block_md *block, int nr_bits) 1273047924c9SDennis Zhou { 1274047924c9SDennis Zhou block->scan_hint = 0; 1275047924c9SDennis Zhou block->contig_hint = nr_bits; 1276047924c9SDennis Zhou block->left_free = nr_bits; 1277047924c9SDennis Zhou block->right_free = nr_bits; 1278047924c9SDennis Zhou block->first_free = 0; 1279047924c9SDennis Zhou block->nr_bits = nr_bits; 1280047924c9SDennis Zhou } 1281047924c9SDennis Zhou 1282ca460b3cSDennis Zhou (Facebook) static void pcpu_init_md_blocks(struct pcpu_chunk *chunk) 1283ca460b3cSDennis Zhou (Facebook) { 1284ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *md_block; 1285ca460b3cSDennis Zhou (Facebook) 128692c14cabSDennis Zhou /* init the chunk's block */ 128792c14cabSDennis Zhou pcpu_init_md_block(&chunk->chunk_md, pcpu_chunk_map_bits(chunk)); 128892c14cabSDennis Zhou 1289ca460b3cSDennis Zhou (Facebook) for (md_block = chunk->md_blocks; 1290ca460b3cSDennis Zhou (Facebook) md_block != chunk->md_blocks + pcpu_chunk_nr_blocks(chunk); 1291047924c9SDennis Zhou md_block++) 1292047924c9SDennis Zhou pcpu_init_md_block(md_block, PCPU_BITMAP_BLOCK_BITS); 1293ca460b3cSDennis Zhou (Facebook) } 1294ca460b3cSDennis Zhou (Facebook) 129540064aecSDennis Zhou (Facebook) /** 129640064aecSDennis Zhou (Facebook) * pcpu_alloc_first_chunk - creates chunks that serve the first chunk 129740064aecSDennis Zhou (Facebook) * @tmp_addr: the start of the region served 129840064aecSDennis Zhou (Facebook) * @map_size: size of the region served 129940064aecSDennis Zhou (Facebook) * 130040064aecSDennis Zhou (Facebook) * This is responsible for creating the chunks that serve the first chunk. The 130140064aecSDennis Zhou (Facebook) * base_addr is page aligned down of @tmp_addr while the region end is page 130240064aecSDennis Zhou (Facebook) * aligned up. Offsets are kept track of to determine the region served. All 130340064aecSDennis Zhou (Facebook) * this is done to appease the bitmap allocator in avoiding partial blocks. 130440064aecSDennis Zhou (Facebook) * 130540064aecSDennis Zhou (Facebook) * RETURNS: 130640064aecSDennis Zhou (Facebook) * Chunk serving the region at @tmp_addr of @map_size. 130740064aecSDennis Zhou (Facebook) */ 1308c0ebfdc3SDennis Zhou (Facebook) static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr, 130940064aecSDennis Zhou (Facebook) int map_size) 131010edf5b0SDennis Zhou (Facebook) { 131110edf5b0SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 1312ca460b3cSDennis Zhou (Facebook) unsigned long aligned_addr, lcm_align; 131340064aecSDennis Zhou (Facebook) int start_offset, offset_bits, region_size, region_bits; 1314f655f405SMike Rapoport size_t alloc_size; 1315c0ebfdc3SDennis Zhou (Facebook) 1316c0ebfdc3SDennis Zhou (Facebook) /* region calculations */ 1317c0ebfdc3SDennis Zhou (Facebook) aligned_addr = tmp_addr & PAGE_MASK; 1318c0ebfdc3SDennis Zhou (Facebook) 1319c0ebfdc3SDennis Zhou (Facebook) start_offset = tmp_addr - aligned_addr; 13206b9d7c8eSDennis Zhou (Facebook) 1321ca460b3cSDennis Zhou (Facebook) /* 1322ca460b3cSDennis Zhou (Facebook) * Align the end of the region with the LCM of PAGE_SIZE and 1323ca460b3cSDennis Zhou (Facebook) * PCPU_BITMAP_BLOCK_SIZE. One of these constants is a multiple of 1324ca460b3cSDennis Zhou (Facebook) * the other. 1325ca460b3cSDennis Zhou (Facebook) */ 1326ca460b3cSDennis Zhou (Facebook) lcm_align = lcm(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE); 1327ca460b3cSDennis Zhou (Facebook) region_size = ALIGN(start_offset + map_size, lcm_align); 132810edf5b0SDennis Zhou (Facebook) 1329c0ebfdc3SDennis Zhou (Facebook) /* allocate chunk */ 1330f655f405SMike Rapoport alloc_size = sizeof(struct pcpu_chunk) + 1331f655f405SMike Rapoport BITS_TO_LONGS(region_size >> PAGE_SHIFT); 1332f655f405SMike Rapoport chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1333f655f405SMike Rapoport if (!chunk) 1334f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1335f655f405SMike Rapoport alloc_size); 1336c0ebfdc3SDennis Zhou (Facebook) 133710edf5b0SDennis Zhou (Facebook) INIT_LIST_HEAD(&chunk->list); 1338c0ebfdc3SDennis Zhou (Facebook) 1339c0ebfdc3SDennis Zhou (Facebook) chunk->base_addr = (void *)aligned_addr; 134010edf5b0SDennis Zhou (Facebook) chunk->start_offset = start_offset; 13416b9d7c8eSDennis Zhou (Facebook) chunk->end_offset = region_size - chunk->start_offset - map_size; 1342c0ebfdc3SDennis Zhou (Facebook) 13438ab16c43SDennis Zhou (Facebook) chunk->nr_pages = region_size >> PAGE_SHIFT; 134440064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 1345c0ebfdc3SDennis Zhou (Facebook) 1346f655f405SMike Rapoport alloc_size = BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]); 1347f655f405SMike Rapoport chunk->alloc_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1348f655f405SMike Rapoport if (!chunk->alloc_map) 1349f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1350f655f405SMike Rapoport alloc_size); 1351f655f405SMike Rapoport 1352f655f405SMike Rapoport alloc_size = 1353f655f405SMike Rapoport BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]); 1354f655f405SMike Rapoport chunk->bound_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1355f655f405SMike Rapoport if (!chunk->bound_map) 1356f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1357f655f405SMike Rapoport alloc_size); 1358f655f405SMike Rapoport 1359f655f405SMike Rapoport alloc_size = pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]); 1360f655f405SMike Rapoport chunk->md_blocks = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1361f655f405SMike Rapoport if (!chunk->md_blocks) 1362f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1363f655f405SMike Rapoport alloc_size); 1364f655f405SMike Rapoport 1365ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 136610edf5b0SDennis Zhou (Facebook) 136710edf5b0SDennis Zhou (Facebook) /* manage populated page bitmap */ 136810edf5b0SDennis Zhou (Facebook) chunk->immutable = true; 13698ab16c43SDennis Zhou (Facebook) bitmap_fill(chunk->populated, chunk->nr_pages); 13708ab16c43SDennis Zhou (Facebook) chunk->nr_populated = chunk->nr_pages; 1371b239f7daSDennis Zhou chunk->nr_empty_pop_pages = chunk->nr_pages; 137210edf5b0SDennis Zhou (Facebook) 137340064aecSDennis Zhou (Facebook) chunk->free_bytes = map_size; 1374c0ebfdc3SDennis Zhou (Facebook) 1375c0ebfdc3SDennis Zhou (Facebook) if (chunk->start_offset) { 1376c0ebfdc3SDennis Zhou (Facebook) /* hide the beginning of the bitmap */ 137740064aecSDennis Zhou (Facebook) offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE; 137840064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 0, offset_bits); 137940064aecSDennis Zhou (Facebook) set_bit(0, chunk->bound_map); 138040064aecSDennis Zhou (Facebook) set_bit(offset_bits, chunk->bound_map); 1381ca460b3cSDennis Zhou (Facebook) 138292c14cabSDennis Zhou chunk->chunk_md.first_free = offset_bits; 138386b442fbSDennis Zhou (Facebook) 1384ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, 0, offset_bits); 1385c0ebfdc3SDennis Zhou (Facebook) } 1386c0ebfdc3SDennis Zhou (Facebook) 13876b9d7c8eSDennis Zhou (Facebook) if (chunk->end_offset) { 13886b9d7c8eSDennis Zhou (Facebook) /* hide the end of the bitmap */ 138940064aecSDennis Zhou (Facebook) offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE; 139040064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 139140064aecSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk) - offset_bits, 139240064aecSDennis Zhou (Facebook) offset_bits); 139340064aecSDennis Zhou (Facebook) set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE, 139440064aecSDennis Zhou (Facebook) chunk->bound_map); 139540064aecSDennis Zhou (Facebook) set_bit(region_bits, chunk->bound_map); 13966b9d7c8eSDennis Zhou (Facebook) 1397ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk) 1398ca460b3cSDennis Zhou (Facebook) - offset_bits, offset_bits); 1399ca460b3cSDennis Zhou (Facebook) } 140040064aecSDennis Zhou (Facebook) 140110edf5b0SDennis Zhou (Facebook) return chunk; 140210edf5b0SDennis Zhou (Facebook) } 140310edf5b0SDennis Zhou (Facebook) 140447504ee0SDennis Zhou static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp) 14056081089fSTejun Heo { 14066081089fSTejun Heo struct pcpu_chunk *chunk; 140740064aecSDennis Zhou (Facebook) int region_bits; 14086081089fSTejun Heo 140947504ee0SDennis Zhou chunk = pcpu_mem_zalloc(pcpu_chunk_struct_size, gfp); 14106081089fSTejun Heo if (!chunk) 14116081089fSTejun Heo return NULL; 14126081089fSTejun Heo 14136081089fSTejun Heo INIT_LIST_HEAD(&chunk->list); 1414c0ebfdc3SDennis Zhou (Facebook) chunk->nr_pages = pcpu_unit_pages; 141540064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 141640064aecSDennis Zhou (Facebook) 141740064aecSDennis Zhou (Facebook) chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) * 141847504ee0SDennis Zhou sizeof(chunk->alloc_map[0]), gfp); 141940064aecSDennis Zhou (Facebook) if (!chunk->alloc_map) 142040064aecSDennis Zhou (Facebook) goto alloc_map_fail; 142140064aecSDennis Zhou (Facebook) 142240064aecSDennis Zhou (Facebook) chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) * 142347504ee0SDennis Zhou sizeof(chunk->bound_map[0]), gfp); 142440064aecSDennis Zhou (Facebook) if (!chunk->bound_map) 142540064aecSDennis Zhou (Facebook) goto bound_map_fail; 142640064aecSDennis Zhou (Facebook) 1427ca460b3cSDennis Zhou (Facebook) chunk->md_blocks = pcpu_mem_zalloc(pcpu_chunk_nr_blocks(chunk) * 142847504ee0SDennis Zhou sizeof(chunk->md_blocks[0]), gfp); 1429ca460b3cSDennis Zhou (Facebook) if (!chunk->md_blocks) 1430ca460b3cSDennis Zhou (Facebook) goto md_blocks_fail; 1431ca460b3cSDennis Zhou (Facebook) 1432ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 1433ca460b3cSDennis Zhou (Facebook) 143440064aecSDennis Zhou (Facebook) /* init metadata */ 143540064aecSDennis Zhou (Facebook) chunk->free_bytes = chunk->nr_pages * PAGE_SIZE; 1436c0ebfdc3SDennis Zhou (Facebook) 14376081089fSTejun Heo return chunk; 143840064aecSDennis Zhou (Facebook) 1439ca460b3cSDennis Zhou (Facebook) md_blocks_fail: 1440ca460b3cSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 144140064aecSDennis Zhou (Facebook) bound_map_fail: 144240064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 144340064aecSDennis Zhou (Facebook) alloc_map_fail: 144440064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk); 144540064aecSDennis Zhou (Facebook) 144640064aecSDennis Zhou (Facebook) return NULL; 14476081089fSTejun Heo } 14486081089fSTejun Heo 14496081089fSTejun Heo static void pcpu_free_chunk(struct pcpu_chunk *chunk) 14506081089fSTejun Heo { 14516081089fSTejun Heo if (!chunk) 14526081089fSTejun Heo return; 14536685b357SMike Rapoport pcpu_mem_free(chunk->md_blocks); 145440064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 145540064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 14561d5cfdb0STetsuo Handa pcpu_mem_free(chunk); 14576081089fSTejun Heo } 14586081089fSTejun Heo 1459b539b87fSTejun Heo /** 1460b539b87fSTejun Heo * pcpu_chunk_populated - post-population bookkeeping 1461b539b87fSTejun Heo * @chunk: pcpu_chunk which got populated 1462b539b87fSTejun Heo * @page_start: the start page 1463b539b87fSTejun Heo * @page_end: the end page 1464b539b87fSTejun Heo * 1465b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been populated to @chunk. Update 1466b539b87fSTejun Heo * the bookkeeping information accordingly. Must be called after each 1467b539b87fSTejun Heo * successful population. 146840064aecSDennis Zhou (Facebook) * 146940064aecSDennis Zhou (Facebook) * If this is @for_alloc, do not increment pcpu_nr_empty_pop_pages because it 147040064aecSDennis Zhou (Facebook) * is to serve an allocation in that area. 1471b539b87fSTejun Heo */ 147240064aecSDennis Zhou (Facebook) static void pcpu_chunk_populated(struct pcpu_chunk *chunk, int page_start, 1473b239f7daSDennis Zhou int page_end) 1474b539b87fSTejun Heo { 1475b539b87fSTejun Heo int nr = page_end - page_start; 1476b539b87fSTejun Heo 1477b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1478b539b87fSTejun Heo 1479b539b87fSTejun Heo bitmap_set(chunk->populated, page_start, nr); 1480b539b87fSTejun Heo chunk->nr_populated += nr; 14817e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += nr; 148240064aecSDennis Zhou (Facebook) 1483b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, nr); 148440064aecSDennis Zhou (Facebook) } 1485b539b87fSTejun Heo 1486b539b87fSTejun Heo /** 1487b539b87fSTejun Heo * pcpu_chunk_depopulated - post-depopulation bookkeeping 1488b539b87fSTejun Heo * @chunk: pcpu_chunk which got depopulated 1489b539b87fSTejun Heo * @page_start: the start page 1490b539b87fSTejun Heo * @page_end: the end page 1491b539b87fSTejun Heo * 1492b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been depopulated from @chunk. 1493b539b87fSTejun Heo * Update the bookkeeping information accordingly. Must be called after 1494b539b87fSTejun Heo * each successful depopulation. 1495b539b87fSTejun Heo */ 1496b539b87fSTejun Heo static void pcpu_chunk_depopulated(struct pcpu_chunk *chunk, 1497b539b87fSTejun Heo int page_start, int page_end) 1498b539b87fSTejun Heo { 1499b539b87fSTejun Heo int nr = page_end - page_start; 1500b539b87fSTejun Heo 1501b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1502b539b87fSTejun Heo 1503b539b87fSTejun Heo bitmap_clear(chunk->populated, page_start, nr); 1504b539b87fSTejun Heo chunk->nr_populated -= nr; 15057e8a6304SDennis Zhou (Facebook) pcpu_nr_populated -= nr; 1506b239f7daSDennis Zhou 1507b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, -nr); 1508b539b87fSTejun Heo } 1509b539b87fSTejun Heo 1510fbf59bc9STejun Heo /* 15119f645532STejun Heo * Chunk management implementation. 1512fbf59bc9STejun Heo * 15139f645532STejun Heo * To allow different implementations, chunk alloc/free and 15149f645532STejun Heo * [de]population are implemented in a separate file which is pulled 15159f645532STejun Heo * into this file and compiled together. The following functions 15169f645532STejun Heo * should be implemented. 1517ccea34b5STejun Heo * 15189f645532STejun Heo * pcpu_populate_chunk - populate the specified range of a chunk 15199f645532STejun Heo * pcpu_depopulate_chunk - depopulate the specified range of a chunk 15209f645532STejun Heo * pcpu_create_chunk - create a new chunk 15219f645532STejun Heo * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop 15229f645532STejun Heo * pcpu_addr_to_page - translate address to physical address 15239f645532STejun Heo * pcpu_verify_alloc_info - check alloc_info is acceptable during init 1524fbf59bc9STejun Heo */ 152515d9f3d1SDennis Zhou static int pcpu_populate_chunk(struct pcpu_chunk *chunk, 152647504ee0SDennis Zhou int page_start, int page_end, gfp_t gfp); 152715d9f3d1SDennis Zhou static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, 152815d9f3d1SDennis Zhou int page_start, int page_end); 152947504ee0SDennis Zhou static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp); 15309f645532STejun Heo static void pcpu_destroy_chunk(struct pcpu_chunk *chunk); 15319f645532STejun Heo static struct page *pcpu_addr_to_page(void *addr); 15329f645532STejun Heo static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai); 1533fbf59bc9STejun Heo 1534b0c9778bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_KM 1535b0c9778bSTejun Heo #include "percpu-km.c" 1536b0c9778bSTejun Heo #else 15379f645532STejun Heo #include "percpu-vm.c" 1538b0c9778bSTejun Heo #endif 1539fbf59bc9STejun Heo 1540fbf59bc9STejun Heo /** 154188999a89STejun Heo * pcpu_chunk_addr_search - determine chunk containing specified address 154288999a89STejun Heo * @addr: address for which the chunk needs to be determined. 154388999a89STejun Heo * 1544c0ebfdc3SDennis Zhou (Facebook) * This is an internal function that handles all but static allocations. 1545c0ebfdc3SDennis Zhou (Facebook) * Static percpu address values should never be passed into the allocator. 1546c0ebfdc3SDennis Zhou (Facebook) * 154788999a89STejun Heo * RETURNS: 154888999a89STejun Heo * The address of the found chunk. 154988999a89STejun Heo */ 155088999a89STejun Heo static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr) 155188999a89STejun Heo { 1552c0ebfdc3SDennis Zhou (Facebook) /* is it in the dynamic region (first chunk)? */ 1553560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_first_chunk, addr)) 1554c0ebfdc3SDennis Zhou (Facebook) return pcpu_first_chunk; 1555c0ebfdc3SDennis Zhou (Facebook) 1556c0ebfdc3SDennis Zhou (Facebook) /* is it in the reserved region? */ 1557560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_reserved_chunk, addr)) 155888999a89STejun Heo return pcpu_reserved_chunk; 155988999a89STejun Heo 156088999a89STejun Heo /* 156188999a89STejun Heo * The address is relative to unit0 which might be unused and 156288999a89STejun Heo * thus unmapped. Offset the address to the unit space of the 156388999a89STejun Heo * current processor before looking it up in the vmalloc 156488999a89STejun Heo * space. Note that any possible cpu id can be used here, so 156588999a89STejun Heo * there's no need to worry about preemption or cpu hotplug. 156688999a89STejun Heo */ 156788999a89STejun Heo addr += pcpu_unit_offsets[raw_smp_processor_id()]; 15689f645532STejun Heo return pcpu_get_page_chunk(pcpu_addr_to_page(addr)); 156988999a89STejun Heo } 157088999a89STejun Heo 157188999a89STejun Heo /** 1572edcb4639STejun Heo * pcpu_alloc - the percpu allocator 1573cae3aeb8STejun Heo * @size: size of area to allocate in bytes 1574fbf59bc9STejun Heo * @align: alignment of area (max PAGE_SIZE) 1575edcb4639STejun Heo * @reserved: allocate from the reserved chunk if available 15765835d96eSTejun Heo * @gfp: allocation flags 1577fbf59bc9STejun Heo * 15785835d96eSTejun Heo * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't 15790ea7eeecSDaniel Borkmann * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN 15800ea7eeecSDaniel Borkmann * then no warning will be triggered on invalid or failed allocation 15810ea7eeecSDaniel Borkmann * requests. 1582fbf59bc9STejun Heo * 1583fbf59bc9STejun Heo * RETURNS: 1584fbf59bc9STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1585fbf59bc9STejun Heo */ 15865835d96eSTejun Heo static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, 15875835d96eSTejun Heo gfp_t gfp) 1588fbf59bc9STejun Heo { 1589554fef1cSDennis Zhou /* whitelisted flags that can be passed to the backing allocators */ 1590554fef1cSDennis Zhou gfp_t pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 15910ea7eeecSDaniel Borkmann bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL; 15920ea7eeecSDaniel Borkmann bool do_warn = !(gfp & __GFP_NOWARN); 1593f2badb0cSTejun Heo static int warn_limit = 10; 15948744d859SDennis Zhou struct pcpu_chunk *chunk, *next; 1595f2badb0cSTejun Heo const char *err; 159640064aecSDennis Zhou (Facebook) int slot, off, cpu, ret; 1597403a91b1SJiri Kosina unsigned long flags; 1598f528f0b8SCatalin Marinas void __percpu *ptr; 159940064aecSDennis Zhou (Facebook) size_t bits, bit_align; 1600fbf59bc9STejun Heo 1601723ad1d9SAl Viro /* 160240064aecSDennis Zhou (Facebook) * There is now a minimum allocation size of PCPU_MIN_ALLOC_SIZE, 160340064aecSDennis Zhou (Facebook) * therefore alignment must be a minimum of that many bytes. 160440064aecSDennis Zhou (Facebook) * An allocation may have internal fragmentation from rounding up 160540064aecSDennis Zhou (Facebook) * of up to PCPU_MIN_ALLOC_SIZE - 1 bytes. 1606723ad1d9SAl Viro */ 1607d2f3c384SDennis Zhou (Facebook) if (unlikely(align < PCPU_MIN_ALLOC_SIZE)) 1608d2f3c384SDennis Zhou (Facebook) align = PCPU_MIN_ALLOC_SIZE; 1609723ad1d9SAl Viro 1610d2f3c384SDennis Zhou (Facebook) size = ALIGN(size, PCPU_MIN_ALLOC_SIZE); 161140064aecSDennis Zhou (Facebook) bits = size >> PCPU_MIN_ALLOC_SHIFT; 161240064aecSDennis Zhou (Facebook) bit_align = align >> PCPU_MIN_ALLOC_SHIFT; 16132f69fa82SViro 16143ca45a46Szijun_hu if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE || 16153ca45a46Szijun_hu !is_power_of_2(align))) { 16160ea7eeecSDaniel Borkmann WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n", 1617756a025fSJoe Perches size, align); 1618fbf59bc9STejun Heo return NULL; 1619fbf59bc9STejun Heo } 1620fbf59bc9STejun Heo 1621f52ba1feSKirill Tkhai if (!is_atomic) { 1622f52ba1feSKirill Tkhai /* 1623f52ba1feSKirill Tkhai * pcpu_balance_workfn() allocates memory under this mutex, 1624f52ba1feSKirill Tkhai * and it may wait for memory reclaim. Allow current task 1625f52ba1feSKirill Tkhai * to become OOM victim, in case of memory pressure. 1626f52ba1feSKirill Tkhai */ 1627f52ba1feSKirill Tkhai if (gfp & __GFP_NOFAIL) 16286710e594STejun Heo mutex_lock(&pcpu_alloc_mutex); 1629f52ba1feSKirill Tkhai else if (mutex_lock_killable(&pcpu_alloc_mutex)) 1630f52ba1feSKirill Tkhai return NULL; 1631f52ba1feSKirill Tkhai } 16326710e594STejun Heo 1633403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1634fbf59bc9STejun Heo 1635edcb4639STejun Heo /* serve reserved allocations from the reserved chunk if available */ 1636edcb4639STejun Heo if (reserved && pcpu_reserved_chunk) { 1637edcb4639STejun Heo chunk = pcpu_reserved_chunk; 1638833af842STejun Heo 163940064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, is_atomic); 164040064aecSDennis Zhou (Facebook) if (off < 0) { 1641833af842STejun Heo err = "alloc from reserved chunk failed"; 1642ccea34b5STejun Heo goto fail_unlock; 1643f2badb0cSTejun Heo } 1644833af842STejun Heo 164540064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1646edcb4639STejun Heo if (off >= 0) 1647edcb4639STejun Heo goto area_found; 1648833af842STejun Heo 1649f2badb0cSTejun Heo err = "alloc from reserved chunk failed"; 1650ccea34b5STejun Heo goto fail_unlock; 1651edcb4639STejun Heo } 1652edcb4639STejun Heo 1653ccea34b5STejun Heo restart: 1654edcb4639STejun Heo /* search through normal chunks */ 1655fbf59bc9STejun Heo for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) { 16568744d859SDennis Zhou list_for_each_entry_safe(chunk, next, &pcpu_slot[slot], list) { 165740064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, 165840064aecSDennis Zhou (Facebook) is_atomic); 16598744d859SDennis Zhou if (off < 0) { 16608744d859SDennis Zhou if (slot < PCPU_SLOT_FAIL_THRESHOLD) 16618744d859SDennis Zhou pcpu_chunk_move(chunk, 0); 1662fbf59bc9STejun Heo continue; 16638744d859SDennis Zhou } 1664ccea34b5STejun Heo 166540064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1666fbf59bc9STejun Heo if (off >= 0) 1667fbf59bc9STejun Heo goto area_found; 166840064aecSDennis Zhou (Facebook) 1669fbf59bc9STejun Heo } 1670fbf59bc9STejun Heo } 1671fbf59bc9STejun Heo 1672403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1673ccea34b5STejun Heo 1674b38d08f3STejun Heo /* 1675b38d08f3STejun Heo * No space left. Create a new chunk. We don't want multiple 1676b38d08f3STejun Heo * tasks to create chunks simultaneously. Serialize and create iff 1677b38d08f3STejun Heo * there's still no empty chunk after grabbing the mutex. 1678b38d08f3STejun Heo */ 167911df02bfSDennis Zhou if (is_atomic) { 168011df02bfSDennis Zhou err = "atomic alloc failed, no space left"; 16815835d96eSTejun Heo goto fail; 168211df02bfSDennis Zhou } 16835835d96eSTejun Heo 1684b38d08f3STejun Heo if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) { 1685554fef1cSDennis Zhou chunk = pcpu_create_chunk(pcpu_gfp); 1686f2badb0cSTejun Heo if (!chunk) { 1687f2badb0cSTejun Heo err = "failed to allocate new chunk"; 1688b38d08f3STejun Heo goto fail; 1689f2badb0cSTejun Heo } 1690ccea34b5STejun Heo 1691403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1692fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, -1); 1693b38d08f3STejun Heo } else { 1694b38d08f3STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1695b38d08f3STejun Heo } 1696b38d08f3STejun Heo 1697ccea34b5STejun Heo goto restart; 1698fbf59bc9STejun Heo 1699fbf59bc9STejun Heo area_found: 170030a5b536SDennis Zhou pcpu_stats_area_alloc(chunk, size); 1701403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1702ccea34b5STejun Heo 1703dca49645STejun Heo /* populate if not all pages are already there */ 17045835d96eSTejun Heo if (!is_atomic) { 1705e04d3208STejun Heo int page_start, page_end, rs, re; 1706e04d3208STejun Heo 1707dca49645STejun Heo page_start = PFN_DOWN(off); 1708dca49645STejun Heo page_end = PFN_UP(off + size); 1709dca49645STejun Heo 171091e914c5SDennis Zhou (Facebook) pcpu_for_each_unpop_region(chunk->populated, rs, re, 171191e914c5SDennis Zhou (Facebook) page_start, page_end) { 1712dca49645STejun Heo WARN_ON(chunk->immutable); 1713dca49645STejun Heo 1714554fef1cSDennis Zhou ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp); 1715b38d08f3STejun Heo 1716403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1717b38d08f3STejun Heo if (ret) { 171840064aecSDennis Zhou (Facebook) pcpu_free_area(chunk, off); 1719f2badb0cSTejun Heo err = "failed to populate"; 1720ccea34b5STejun Heo goto fail_unlock; 1721fbf59bc9STejun Heo } 1722b239f7daSDennis Zhou pcpu_chunk_populated(chunk, rs, re); 1723b38d08f3STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1724dca49645STejun Heo } 1725dca49645STejun Heo 1726ccea34b5STejun Heo mutex_unlock(&pcpu_alloc_mutex); 1727e04d3208STejun Heo } 1728ccea34b5STejun Heo 17291a4d7607STejun Heo if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_LOW) 17301a4d7607STejun Heo pcpu_schedule_balance_work(); 17311a4d7607STejun Heo 1732dca49645STejun Heo /* clear the areas and return address relative to base address */ 1733dca49645STejun Heo for_each_possible_cpu(cpu) 1734dca49645STejun Heo memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size); 1735dca49645STejun Heo 1736f528f0b8SCatalin Marinas ptr = __addr_to_pcpu_ptr(chunk->base_addr + off); 17378a8c35faSLarry Finger kmemleak_alloc_percpu(ptr, size, gfp); 1738df95e795SDennis Zhou 1739df95e795SDennis Zhou trace_percpu_alloc_percpu(reserved, is_atomic, size, align, 1740df95e795SDennis Zhou chunk->base_addr, off, ptr); 1741df95e795SDennis Zhou 1742f528f0b8SCatalin Marinas return ptr; 1743ccea34b5STejun Heo 1744ccea34b5STejun Heo fail_unlock: 1745403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1746b38d08f3STejun Heo fail: 1747df95e795SDennis Zhou trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align); 1748df95e795SDennis Zhou 17490ea7eeecSDaniel Borkmann if (!is_atomic && do_warn && warn_limit) { 1750870d4b12SJoe Perches pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n", 17515835d96eSTejun Heo size, align, is_atomic, err); 1752f2badb0cSTejun Heo dump_stack(); 1753f2badb0cSTejun Heo if (!--warn_limit) 1754870d4b12SJoe Perches pr_info("limit reached, disable warning\n"); 1755f2badb0cSTejun Heo } 17561a4d7607STejun Heo if (is_atomic) { 17571a4d7607STejun Heo /* see the flag handling in pcpu_blance_workfn() */ 17581a4d7607STejun Heo pcpu_atomic_alloc_failed = true; 17591a4d7607STejun Heo pcpu_schedule_balance_work(); 17606710e594STejun Heo } else { 17616710e594STejun Heo mutex_unlock(&pcpu_alloc_mutex); 17621a4d7607STejun Heo } 1763ccea34b5STejun Heo return NULL; 1764fbf59bc9STejun Heo } 1765edcb4639STejun Heo 1766edcb4639STejun Heo /** 17675835d96eSTejun Heo * __alloc_percpu_gfp - allocate dynamic percpu area 1768edcb4639STejun Heo * @size: size of area to allocate in bytes 1769edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 17705835d96eSTejun Heo * @gfp: allocation flags 1771edcb4639STejun Heo * 17725835d96eSTejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align. If 17735835d96eSTejun Heo * @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can 17740ea7eeecSDaniel Borkmann * be called from any context but is a lot more likely to fail. If @gfp 17750ea7eeecSDaniel Borkmann * has __GFP_NOWARN then no warning will be triggered on invalid or failed 17760ea7eeecSDaniel Borkmann * allocation requests. 1777ccea34b5STejun Heo * 1778edcb4639STejun Heo * RETURNS: 1779edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1780edcb4639STejun Heo */ 17815835d96eSTejun Heo void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) 17825835d96eSTejun Heo { 17835835d96eSTejun Heo return pcpu_alloc(size, align, false, gfp); 17845835d96eSTejun Heo } 17855835d96eSTejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu_gfp); 17865835d96eSTejun Heo 17875835d96eSTejun Heo /** 17885835d96eSTejun Heo * __alloc_percpu - allocate dynamic percpu area 17895835d96eSTejun Heo * @size: size of area to allocate in bytes 17905835d96eSTejun Heo * @align: alignment of area (max PAGE_SIZE) 17915835d96eSTejun Heo * 17925835d96eSTejun Heo * Equivalent to __alloc_percpu_gfp(size, align, %GFP_KERNEL). 17935835d96eSTejun Heo */ 179443cf38ebSTejun Heo void __percpu *__alloc_percpu(size_t size, size_t align) 1795edcb4639STejun Heo { 17965835d96eSTejun Heo return pcpu_alloc(size, align, false, GFP_KERNEL); 1797edcb4639STejun Heo } 1798fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu); 1799fbf59bc9STejun Heo 1800edcb4639STejun Heo /** 1801edcb4639STejun Heo * __alloc_reserved_percpu - allocate reserved percpu area 1802edcb4639STejun Heo * @size: size of area to allocate in bytes 1803edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 1804edcb4639STejun Heo * 18059329ba97STejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align 18069329ba97STejun Heo * from reserved percpu area if arch has set it up; otherwise, 18079329ba97STejun Heo * allocation is served from the same dynamic area. Might sleep. 18089329ba97STejun Heo * Might trigger writeouts. 1809edcb4639STejun Heo * 1810ccea34b5STejun Heo * CONTEXT: 1811ccea34b5STejun Heo * Does GFP_KERNEL allocation. 1812ccea34b5STejun Heo * 1813edcb4639STejun Heo * RETURNS: 1814edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1815edcb4639STejun Heo */ 181643cf38ebSTejun Heo void __percpu *__alloc_reserved_percpu(size_t size, size_t align) 1817edcb4639STejun Heo { 18185835d96eSTejun Heo return pcpu_alloc(size, align, true, GFP_KERNEL); 1819edcb4639STejun Heo } 1820edcb4639STejun Heo 1821a56dbddfSTejun Heo /** 18221a4d7607STejun Heo * pcpu_balance_workfn - manage the amount of free chunks and populated pages 1823a56dbddfSTejun Heo * @work: unused 1824a56dbddfSTejun Heo * 182547504ee0SDennis Zhou * Reclaim all fully free chunks except for the first one. This is also 182647504ee0SDennis Zhou * responsible for maintaining the pool of empty populated pages. However, 182747504ee0SDennis Zhou * it is possible that this is called when physical memory is scarce causing 182847504ee0SDennis Zhou * OOM killer to be triggered. We should avoid doing so until an actual 182947504ee0SDennis Zhou * allocation causes the failure as it is possible that requests can be 183047504ee0SDennis Zhou * serviced from already backed regions. 1831a56dbddfSTejun Heo */ 1832fe6bd8c3STejun Heo static void pcpu_balance_workfn(struct work_struct *work) 1833fbf59bc9STejun Heo { 183447504ee0SDennis Zhou /* gfp flags passed to underlying allocators */ 1835554fef1cSDennis Zhou const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN; 1836fe6bd8c3STejun Heo LIST_HEAD(to_free); 1837fe6bd8c3STejun Heo struct list_head *free_head = &pcpu_slot[pcpu_nr_slots - 1]; 1838a56dbddfSTejun Heo struct pcpu_chunk *chunk, *next; 18391a4d7607STejun Heo int slot, nr_to_pop, ret; 1840a56dbddfSTejun Heo 18411a4d7607STejun Heo /* 18421a4d7607STejun Heo * There's no reason to keep around multiple unused chunks and VM 18431a4d7607STejun Heo * areas can be scarce. Destroy all free chunks except for one. 18441a4d7607STejun Heo */ 1845ccea34b5STejun Heo mutex_lock(&pcpu_alloc_mutex); 1846ccea34b5STejun Heo spin_lock_irq(&pcpu_lock); 1847a56dbddfSTejun Heo 1848fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, free_head, list) { 18498d408b4bSTejun Heo WARN_ON(chunk->immutable); 1850a56dbddfSTejun Heo 1851a56dbddfSTejun Heo /* spare the first one */ 1852fe6bd8c3STejun Heo if (chunk == list_first_entry(free_head, struct pcpu_chunk, list)) 1853a56dbddfSTejun Heo continue; 1854a56dbddfSTejun Heo 1855fe6bd8c3STejun Heo list_move(&chunk->list, &to_free); 1856a56dbddfSTejun Heo } 1857a56dbddfSTejun Heo 1858ccea34b5STejun Heo spin_unlock_irq(&pcpu_lock); 1859a56dbddfSTejun Heo 1860fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, &to_free, list) { 1861a93ace48STejun Heo int rs, re; 1862dca49645STejun Heo 186391e914c5SDennis Zhou (Facebook) pcpu_for_each_pop_region(chunk->populated, rs, re, 0, 186491e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 1865a93ace48STejun Heo pcpu_depopulate_chunk(chunk, rs, re); 1866b539b87fSTejun Heo spin_lock_irq(&pcpu_lock); 1867b539b87fSTejun Heo pcpu_chunk_depopulated(chunk, rs, re); 1868b539b87fSTejun Heo spin_unlock_irq(&pcpu_lock); 1869a93ace48STejun Heo } 18706081089fSTejun Heo pcpu_destroy_chunk(chunk); 1871accd4f36SEric Dumazet cond_resched(); 1872fbf59bc9STejun Heo } 1873971f3918STejun Heo 18741a4d7607STejun Heo /* 18751a4d7607STejun Heo * Ensure there are certain number of free populated pages for 18761a4d7607STejun Heo * atomic allocs. Fill up from the most packed so that atomic 18771a4d7607STejun Heo * allocs don't increase fragmentation. If atomic allocation 18781a4d7607STejun Heo * failed previously, always populate the maximum amount. This 18791a4d7607STejun Heo * should prevent atomic allocs larger than PAGE_SIZE from keeping 18801a4d7607STejun Heo * failing indefinitely; however, large atomic allocs are not 18811a4d7607STejun Heo * something we support properly and can be highly unreliable and 18821a4d7607STejun Heo * inefficient. 18831a4d7607STejun Heo */ 18841a4d7607STejun Heo retry_pop: 18851a4d7607STejun Heo if (pcpu_atomic_alloc_failed) { 18861a4d7607STejun Heo nr_to_pop = PCPU_EMPTY_POP_PAGES_HIGH; 18871a4d7607STejun Heo /* best effort anyway, don't worry about synchronization */ 18881a4d7607STejun Heo pcpu_atomic_alloc_failed = false; 18891a4d7607STejun Heo } else { 18901a4d7607STejun Heo nr_to_pop = clamp(PCPU_EMPTY_POP_PAGES_HIGH - 18911a4d7607STejun Heo pcpu_nr_empty_pop_pages, 18921a4d7607STejun Heo 0, PCPU_EMPTY_POP_PAGES_HIGH); 18931a4d7607STejun Heo } 18941a4d7607STejun Heo 18951a4d7607STejun Heo for (slot = pcpu_size_to_slot(PAGE_SIZE); slot < pcpu_nr_slots; slot++) { 18961a4d7607STejun Heo int nr_unpop = 0, rs, re; 18971a4d7607STejun Heo 18981a4d7607STejun Heo if (!nr_to_pop) 18991a4d7607STejun Heo break; 19001a4d7607STejun Heo 19011a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 19021a4d7607STejun Heo list_for_each_entry(chunk, &pcpu_slot[slot], list) { 19038ab16c43SDennis Zhou (Facebook) nr_unpop = chunk->nr_pages - chunk->nr_populated; 19041a4d7607STejun Heo if (nr_unpop) 19051a4d7607STejun Heo break; 19061a4d7607STejun Heo } 19071a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 19081a4d7607STejun Heo 19091a4d7607STejun Heo if (!nr_unpop) 19101a4d7607STejun Heo continue; 19111a4d7607STejun Heo 19121a4d7607STejun Heo /* @chunk can't go away while pcpu_alloc_mutex is held */ 191391e914c5SDennis Zhou (Facebook) pcpu_for_each_unpop_region(chunk->populated, rs, re, 0, 191491e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 19151a4d7607STejun Heo int nr = min(re - rs, nr_to_pop); 19161a4d7607STejun Heo 191747504ee0SDennis Zhou ret = pcpu_populate_chunk(chunk, rs, rs + nr, gfp); 19181a4d7607STejun Heo if (!ret) { 19191a4d7607STejun Heo nr_to_pop -= nr; 19201a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 1921b239f7daSDennis Zhou pcpu_chunk_populated(chunk, rs, rs + nr); 19221a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 19231a4d7607STejun Heo } else { 19241a4d7607STejun Heo nr_to_pop = 0; 19251a4d7607STejun Heo } 19261a4d7607STejun Heo 19271a4d7607STejun Heo if (!nr_to_pop) 19281a4d7607STejun Heo break; 19291a4d7607STejun Heo } 19301a4d7607STejun Heo } 19311a4d7607STejun Heo 19321a4d7607STejun Heo if (nr_to_pop) { 19331a4d7607STejun Heo /* ran out of chunks to populate, create a new one and retry */ 193447504ee0SDennis Zhou chunk = pcpu_create_chunk(gfp); 19351a4d7607STejun Heo if (chunk) { 19361a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 19371a4d7607STejun Heo pcpu_chunk_relocate(chunk, -1); 19381a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 19391a4d7607STejun Heo goto retry_pop; 19401a4d7607STejun Heo } 19411a4d7607STejun Heo } 19421a4d7607STejun Heo 1943971f3918STejun Heo mutex_unlock(&pcpu_alloc_mutex); 1944a56dbddfSTejun Heo } 1945fbf59bc9STejun Heo 1946fbf59bc9STejun Heo /** 1947fbf59bc9STejun Heo * free_percpu - free percpu area 1948fbf59bc9STejun Heo * @ptr: pointer to area to free 1949fbf59bc9STejun Heo * 1950ccea34b5STejun Heo * Free percpu area @ptr. 1951ccea34b5STejun Heo * 1952ccea34b5STejun Heo * CONTEXT: 1953ccea34b5STejun Heo * Can be called from atomic context. 1954fbf59bc9STejun Heo */ 195543cf38ebSTejun Heo void free_percpu(void __percpu *ptr) 1956fbf59bc9STejun Heo { 1957129182e5SAndrew Morton void *addr; 1958fbf59bc9STejun Heo struct pcpu_chunk *chunk; 1959ccea34b5STejun Heo unsigned long flags; 196040064aecSDennis Zhou (Facebook) int off; 1961198790d9SJohn Sperbeck bool need_balance = false; 1962fbf59bc9STejun Heo 1963fbf59bc9STejun Heo if (!ptr) 1964fbf59bc9STejun Heo return; 1965fbf59bc9STejun Heo 1966f528f0b8SCatalin Marinas kmemleak_free_percpu(ptr); 1967f528f0b8SCatalin Marinas 1968129182e5SAndrew Morton addr = __pcpu_ptr_to_addr(ptr); 1969129182e5SAndrew Morton 1970ccea34b5STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1971fbf59bc9STejun Heo 1972fbf59bc9STejun Heo chunk = pcpu_chunk_addr_search(addr); 1973bba174f5STejun Heo off = addr - chunk->base_addr; 1974fbf59bc9STejun Heo 197540064aecSDennis Zhou (Facebook) pcpu_free_area(chunk, off); 1976fbf59bc9STejun Heo 1977a56dbddfSTejun Heo /* if there are more than one fully free chunks, wake up grim reaper */ 197840064aecSDennis Zhou (Facebook) if (chunk->free_bytes == pcpu_unit_size) { 1979fbf59bc9STejun Heo struct pcpu_chunk *pos; 1980fbf59bc9STejun Heo 1981a56dbddfSTejun Heo list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list) 1982fbf59bc9STejun Heo if (pos != chunk) { 1983198790d9SJohn Sperbeck need_balance = true; 1984fbf59bc9STejun Heo break; 1985fbf59bc9STejun Heo } 1986fbf59bc9STejun Heo } 1987fbf59bc9STejun Heo 1988df95e795SDennis Zhou trace_percpu_free_percpu(chunk->base_addr, off, ptr); 1989df95e795SDennis Zhou 1990ccea34b5STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1991198790d9SJohn Sperbeck 1992198790d9SJohn Sperbeck if (need_balance) 1993198790d9SJohn Sperbeck pcpu_schedule_balance_work(); 1994fbf59bc9STejun Heo } 1995fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(free_percpu); 1996fbf59bc9STejun Heo 1997383776faSThomas Gleixner bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr) 1998383776faSThomas Gleixner { 1999383776faSThomas Gleixner #ifdef CONFIG_SMP 2000383776faSThomas Gleixner const size_t static_size = __per_cpu_end - __per_cpu_start; 2001383776faSThomas Gleixner void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 2002383776faSThomas Gleixner unsigned int cpu; 2003383776faSThomas Gleixner 2004383776faSThomas Gleixner for_each_possible_cpu(cpu) { 2005383776faSThomas Gleixner void *start = per_cpu_ptr(base, cpu); 2006383776faSThomas Gleixner void *va = (void *)addr; 2007383776faSThomas Gleixner 2008383776faSThomas Gleixner if (va >= start && va < start + static_size) { 20098ce371f9SPeter Zijlstra if (can_addr) { 2010383776faSThomas Gleixner *can_addr = (unsigned long) (va - start); 20118ce371f9SPeter Zijlstra *can_addr += (unsigned long) 20128ce371f9SPeter Zijlstra per_cpu_ptr(base, get_boot_cpu_id()); 20138ce371f9SPeter Zijlstra } 2014383776faSThomas Gleixner return true; 2015383776faSThomas Gleixner } 2016383776faSThomas Gleixner } 2017383776faSThomas Gleixner #endif 2018383776faSThomas Gleixner /* on UP, can't distinguish from other static vars, always false */ 2019383776faSThomas Gleixner return false; 2020383776faSThomas Gleixner } 2021383776faSThomas Gleixner 20223b034b0dSVivek Goyal /** 202310fad5e4STejun Heo * is_kernel_percpu_address - test whether address is from static percpu area 202410fad5e4STejun Heo * @addr: address to test 202510fad5e4STejun Heo * 202610fad5e4STejun Heo * Test whether @addr belongs to in-kernel static percpu area. Module 202710fad5e4STejun Heo * static percpu areas are not considered. For those, use 202810fad5e4STejun Heo * is_module_percpu_address(). 202910fad5e4STejun Heo * 203010fad5e4STejun Heo * RETURNS: 203110fad5e4STejun Heo * %true if @addr is from in-kernel static percpu area, %false otherwise. 203210fad5e4STejun Heo */ 203310fad5e4STejun Heo bool is_kernel_percpu_address(unsigned long addr) 203410fad5e4STejun Heo { 2035383776faSThomas Gleixner return __is_kernel_percpu_address(addr, NULL); 203610fad5e4STejun Heo } 203710fad5e4STejun Heo 203810fad5e4STejun Heo /** 20393b034b0dSVivek Goyal * per_cpu_ptr_to_phys - convert translated percpu address to physical address 20403b034b0dSVivek Goyal * @addr: the address to be converted to physical address 20413b034b0dSVivek Goyal * 20423b034b0dSVivek Goyal * Given @addr which is dereferenceable address obtained via one of 20433b034b0dSVivek Goyal * percpu access macros, this function translates it into its physical 20443b034b0dSVivek Goyal * address. The caller is responsible for ensuring @addr stays valid 20453b034b0dSVivek Goyal * until this function finishes. 20463b034b0dSVivek Goyal * 204767589c71SDave Young * percpu allocator has special setup for the first chunk, which currently 204867589c71SDave Young * supports either embedding in linear address space or vmalloc mapping, 204967589c71SDave Young * and, from the second one, the backing allocator (currently either vm or 205067589c71SDave Young * km) provides translation. 205167589c71SDave Young * 2052bffc4375SYannick Guerrini * The addr can be translated simply without checking if it falls into the 205367589c71SDave Young * first chunk. But the current code reflects better how percpu allocator 205467589c71SDave Young * actually works, and the verification can discover both bugs in percpu 205567589c71SDave Young * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current 205667589c71SDave Young * code. 205767589c71SDave Young * 20583b034b0dSVivek Goyal * RETURNS: 20593b034b0dSVivek Goyal * The physical address for @addr. 20603b034b0dSVivek Goyal */ 20613b034b0dSVivek Goyal phys_addr_t per_cpu_ptr_to_phys(void *addr) 20623b034b0dSVivek Goyal { 20639983b6f0STejun Heo void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 20649983b6f0STejun Heo bool in_first_chunk = false; 2065a855b84cSTejun Heo unsigned long first_low, first_high; 20669983b6f0STejun Heo unsigned int cpu; 20679983b6f0STejun Heo 20689983b6f0STejun Heo /* 2069a855b84cSTejun Heo * The following test on unit_low/high isn't strictly 20709983b6f0STejun Heo * necessary but will speed up lookups of addresses which 20719983b6f0STejun Heo * aren't in the first chunk. 2072c0ebfdc3SDennis Zhou (Facebook) * 2073c0ebfdc3SDennis Zhou (Facebook) * The address check is against full chunk sizes. pcpu_base_addr 2074c0ebfdc3SDennis Zhou (Facebook) * points to the beginning of the first chunk including the 2075c0ebfdc3SDennis Zhou (Facebook) * static region. Assumes good intent as the first chunk may 2076c0ebfdc3SDennis Zhou (Facebook) * not be full (ie. < pcpu_unit_pages in size). 20779983b6f0STejun Heo */ 2078c0ebfdc3SDennis Zhou (Facebook) first_low = (unsigned long)pcpu_base_addr + 2079c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_low_unit_cpu, 0); 2080c0ebfdc3SDennis Zhou (Facebook) first_high = (unsigned long)pcpu_base_addr + 2081c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_high_unit_cpu, pcpu_unit_pages); 2082a855b84cSTejun Heo if ((unsigned long)addr >= first_low && 2083a855b84cSTejun Heo (unsigned long)addr < first_high) { 20849983b6f0STejun Heo for_each_possible_cpu(cpu) { 20859983b6f0STejun Heo void *start = per_cpu_ptr(base, cpu); 20869983b6f0STejun Heo 20879983b6f0STejun Heo if (addr >= start && addr < start + pcpu_unit_size) { 20889983b6f0STejun Heo in_first_chunk = true; 20899983b6f0STejun Heo break; 20909983b6f0STejun Heo } 20919983b6f0STejun Heo } 20929983b6f0STejun Heo } 20939983b6f0STejun Heo 20949983b6f0STejun Heo if (in_first_chunk) { 2095eac522efSDavid Howells if (!is_vmalloc_addr(addr)) 20963b034b0dSVivek Goyal return __pa(addr); 20973b034b0dSVivek Goyal else 20989f57bd4dSEugene Surovegin return page_to_phys(vmalloc_to_page(addr)) + 20999f57bd4dSEugene Surovegin offset_in_page(addr); 2100020ec653STejun Heo } else 21019f57bd4dSEugene Surovegin return page_to_phys(pcpu_addr_to_page(addr)) + 21029f57bd4dSEugene Surovegin offset_in_page(addr); 21033b034b0dSVivek Goyal } 21043b034b0dSVivek Goyal 2105fbf59bc9STejun Heo /** 2106fd1e8a1fSTejun Heo * pcpu_alloc_alloc_info - allocate percpu allocation info 2107fd1e8a1fSTejun Heo * @nr_groups: the number of groups 2108fd1e8a1fSTejun Heo * @nr_units: the number of units 2109033e48fbSTejun Heo * 2110fd1e8a1fSTejun Heo * Allocate ai which is large enough for @nr_groups groups containing 2111fd1e8a1fSTejun Heo * @nr_units units. The returned ai's groups[0].cpu_map points to the 2112fd1e8a1fSTejun Heo * cpu_map array which is long enough for @nr_units and filled with 2113fd1e8a1fSTejun Heo * NR_CPUS. It's the caller's responsibility to initialize cpu_map 2114fd1e8a1fSTejun Heo * pointer of other groups. 2115033e48fbSTejun Heo * 2116033e48fbSTejun Heo * RETURNS: 2117fd1e8a1fSTejun Heo * Pointer to the allocated pcpu_alloc_info on success, NULL on 2118fd1e8a1fSTejun Heo * failure. 2119033e48fbSTejun Heo */ 2120fd1e8a1fSTejun Heo struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups, 2121fd1e8a1fSTejun Heo int nr_units) 2122fd1e8a1fSTejun Heo { 2123fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 2124fd1e8a1fSTejun Heo size_t base_size, ai_size; 2125fd1e8a1fSTejun Heo void *ptr; 2126fd1e8a1fSTejun Heo int unit; 2127fd1e8a1fSTejun Heo 2128fd1e8a1fSTejun Heo base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]), 2129fd1e8a1fSTejun Heo __alignof__(ai->groups[0].cpu_map[0])); 2130fd1e8a1fSTejun Heo ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]); 2131fd1e8a1fSTejun Heo 213226fb3daeSMike Rapoport ptr = memblock_alloc(PFN_ALIGN(ai_size), PAGE_SIZE); 2133fd1e8a1fSTejun Heo if (!ptr) 2134fd1e8a1fSTejun Heo return NULL; 2135fd1e8a1fSTejun Heo ai = ptr; 2136fd1e8a1fSTejun Heo ptr += base_size; 2137fd1e8a1fSTejun Heo 2138fd1e8a1fSTejun Heo ai->groups[0].cpu_map = ptr; 2139fd1e8a1fSTejun Heo 2140fd1e8a1fSTejun Heo for (unit = 0; unit < nr_units; unit++) 2141fd1e8a1fSTejun Heo ai->groups[0].cpu_map[unit] = NR_CPUS; 2142fd1e8a1fSTejun Heo 2143fd1e8a1fSTejun Heo ai->nr_groups = nr_groups; 2144fd1e8a1fSTejun Heo ai->__ai_size = PFN_ALIGN(ai_size); 2145fd1e8a1fSTejun Heo 2146fd1e8a1fSTejun Heo return ai; 2147fd1e8a1fSTejun Heo } 2148fd1e8a1fSTejun Heo 2149fd1e8a1fSTejun Heo /** 2150fd1e8a1fSTejun Heo * pcpu_free_alloc_info - free percpu allocation info 2151fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info to free 2152fd1e8a1fSTejun Heo * 2153fd1e8a1fSTejun Heo * Free @ai which was allocated by pcpu_alloc_alloc_info(). 2154fd1e8a1fSTejun Heo */ 2155fd1e8a1fSTejun Heo void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai) 2156fd1e8a1fSTejun Heo { 2157999c17e3SSantosh Shilimkar memblock_free_early(__pa(ai), ai->__ai_size); 2158fd1e8a1fSTejun Heo } 2159fd1e8a1fSTejun Heo 2160fd1e8a1fSTejun Heo /** 2161fd1e8a1fSTejun Heo * pcpu_dump_alloc_info - print out information about pcpu_alloc_info 2162fd1e8a1fSTejun Heo * @lvl: loglevel 2163fd1e8a1fSTejun Heo * @ai: allocation info to dump 2164fd1e8a1fSTejun Heo * 2165fd1e8a1fSTejun Heo * Print out information about @ai using loglevel @lvl. 2166fd1e8a1fSTejun Heo */ 2167fd1e8a1fSTejun Heo static void pcpu_dump_alloc_info(const char *lvl, 2168fd1e8a1fSTejun Heo const struct pcpu_alloc_info *ai) 2169033e48fbSTejun Heo { 2170fd1e8a1fSTejun Heo int group_width = 1, cpu_width = 1, width; 2171033e48fbSTejun Heo char empty_str[] = "--------"; 2172fd1e8a1fSTejun Heo int alloc = 0, alloc_end = 0; 2173fd1e8a1fSTejun Heo int group, v; 2174fd1e8a1fSTejun Heo int upa, apl; /* units per alloc, allocs per line */ 2175033e48fbSTejun Heo 2176fd1e8a1fSTejun Heo v = ai->nr_groups; 2177033e48fbSTejun Heo while (v /= 10) 2178fd1e8a1fSTejun Heo group_width++; 2179033e48fbSTejun Heo 2180fd1e8a1fSTejun Heo v = num_possible_cpus(); 2181fd1e8a1fSTejun Heo while (v /= 10) 2182fd1e8a1fSTejun Heo cpu_width++; 2183fd1e8a1fSTejun Heo empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0'; 2184033e48fbSTejun Heo 2185fd1e8a1fSTejun Heo upa = ai->alloc_size / ai->unit_size; 2186fd1e8a1fSTejun Heo width = upa * (cpu_width + 1) + group_width + 3; 2187fd1e8a1fSTejun Heo apl = rounddown_pow_of_two(max(60 / width, 1)); 2188033e48fbSTejun Heo 2189fd1e8a1fSTejun Heo printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu", 2190fd1e8a1fSTejun Heo lvl, ai->static_size, ai->reserved_size, ai->dyn_size, 2191fd1e8a1fSTejun Heo ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size); 2192fd1e8a1fSTejun Heo 2193fd1e8a1fSTejun Heo for (group = 0; group < ai->nr_groups; group++) { 2194fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 2195fd1e8a1fSTejun Heo int unit = 0, unit_end = 0; 2196fd1e8a1fSTejun Heo 2197fd1e8a1fSTejun Heo BUG_ON(gi->nr_units % upa); 2198fd1e8a1fSTejun Heo for (alloc_end += gi->nr_units / upa; 2199fd1e8a1fSTejun Heo alloc < alloc_end; alloc++) { 2200fd1e8a1fSTejun Heo if (!(alloc % apl)) { 22011170532bSJoe Perches pr_cont("\n"); 2202fd1e8a1fSTejun Heo printk("%spcpu-alloc: ", lvl); 2203033e48fbSTejun Heo } 22041170532bSJoe Perches pr_cont("[%0*d] ", group_width, group); 2205fd1e8a1fSTejun Heo 2206fd1e8a1fSTejun Heo for (unit_end += upa; unit < unit_end; unit++) 2207fd1e8a1fSTejun Heo if (gi->cpu_map[unit] != NR_CPUS) 22081170532bSJoe Perches pr_cont("%0*d ", 22091170532bSJoe Perches cpu_width, gi->cpu_map[unit]); 2210033e48fbSTejun Heo else 22111170532bSJoe Perches pr_cont("%s ", empty_str); 2212033e48fbSTejun Heo } 2213fd1e8a1fSTejun Heo } 22141170532bSJoe Perches pr_cont("\n"); 2215033e48fbSTejun Heo } 2216033e48fbSTejun Heo 2217fbf59bc9STejun Heo /** 22188d408b4bSTejun Heo * pcpu_setup_first_chunk - initialize the first percpu chunk 2219fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info describing how to percpu area is shaped 222038a6be52STejun Heo * @base_addr: mapped address 2221fbf59bc9STejun Heo * 22228d408b4bSTejun Heo * Initialize the first percpu chunk which contains the kernel static 2223*69ab285bSChristophe JAILLET * percpu area. This function is to be called from arch percpu area 222438a6be52STejun Heo * setup path. 22258d408b4bSTejun Heo * 2226fd1e8a1fSTejun Heo * @ai contains all information necessary to initialize the first 2227fd1e8a1fSTejun Heo * chunk and prime the dynamic percpu allocator. 22288d408b4bSTejun Heo * 2229fd1e8a1fSTejun Heo * @ai->static_size is the size of static percpu area. 2230fd1e8a1fSTejun Heo * 2231fd1e8a1fSTejun Heo * @ai->reserved_size, if non-zero, specifies the amount of bytes to 2232edcb4639STejun Heo * reserve after the static area in the first chunk. This reserves 2233edcb4639STejun Heo * the first chunk such that it's available only through reserved 2234edcb4639STejun Heo * percpu allocation. This is primarily used to serve module percpu 2235edcb4639STejun Heo * static areas on architectures where the addressing model has 2236edcb4639STejun Heo * limited offset range for symbol relocations to guarantee module 2237edcb4639STejun Heo * percpu symbols fall inside the relocatable range. 2238edcb4639STejun Heo * 2239fd1e8a1fSTejun Heo * @ai->dyn_size determines the number of bytes available for dynamic 2240fd1e8a1fSTejun Heo * allocation in the first chunk. The area between @ai->static_size + 2241fd1e8a1fSTejun Heo * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused. 22426074d5b0STejun Heo * 2243fd1e8a1fSTejun Heo * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE 2244fd1e8a1fSTejun Heo * and equal to or larger than @ai->static_size + @ai->reserved_size + 2245fd1e8a1fSTejun Heo * @ai->dyn_size. 22468d408b4bSTejun Heo * 2247fd1e8a1fSTejun Heo * @ai->atom_size is the allocation atom size and used as alignment 2248fd1e8a1fSTejun Heo * for vm areas. 22498d408b4bSTejun Heo * 2250fd1e8a1fSTejun Heo * @ai->alloc_size is the allocation size and always multiple of 2251fd1e8a1fSTejun Heo * @ai->atom_size. This is larger than @ai->atom_size if 2252fd1e8a1fSTejun Heo * @ai->unit_size is larger than @ai->atom_size. 2253fd1e8a1fSTejun Heo * 2254fd1e8a1fSTejun Heo * @ai->nr_groups and @ai->groups describe virtual memory layout of 2255fd1e8a1fSTejun Heo * percpu areas. Units which should be colocated are put into the 2256fd1e8a1fSTejun Heo * same group. Dynamic VM areas will be allocated according to these 2257fd1e8a1fSTejun Heo * groupings. If @ai->nr_groups is zero, a single group containing 2258fd1e8a1fSTejun Heo * all units is assumed. 22598d408b4bSTejun Heo * 226038a6be52STejun Heo * The caller should have mapped the first chunk at @base_addr and 226138a6be52STejun Heo * copied static data to each unit. 2262fbf59bc9STejun Heo * 2263c0ebfdc3SDennis Zhou (Facebook) * The first chunk will always contain a static and a dynamic region. 2264c0ebfdc3SDennis Zhou (Facebook) * However, the static region is not managed by any chunk. If the first 2265c0ebfdc3SDennis Zhou (Facebook) * chunk also contains a reserved region, it is served by two chunks - 2266c0ebfdc3SDennis Zhou (Facebook) * one for the reserved region and one for the dynamic region. They 2267c0ebfdc3SDennis Zhou (Facebook) * share the same vm, but use offset regions in the area allocation map. 2268c0ebfdc3SDennis Zhou (Facebook) * The chunk serving the dynamic region is circulated in the chunk slots 2269c0ebfdc3SDennis Zhou (Facebook) * and available for dynamic allocation like any other chunk. 2270fbf59bc9STejun Heo */ 2271163fa234SKefeng Wang void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai, 2272fd1e8a1fSTejun Heo void *base_addr) 2273fbf59bc9STejun Heo { 2274b9c39442SDennis Zhou (Facebook) size_t size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 2275d2f3c384SDennis Zhou (Facebook) size_t static_size, dyn_size; 22760c4169c3SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 22776563297cSTejun Heo unsigned long *group_offsets; 22786563297cSTejun Heo size_t *group_sizes; 2279fb435d52STejun Heo unsigned long *unit_off; 2280fbf59bc9STejun Heo unsigned int cpu; 2281fd1e8a1fSTejun Heo int *unit_map; 2282fd1e8a1fSTejun Heo int group, unit, i; 2283c0ebfdc3SDennis Zhou (Facebook) int map_size; 2284c0ebfdc3SDennis Zhou (Facebook) unsigned long tmp_addr; 2285f655f405SMike Rapoport size_t alloc_size; 2286fbf59bc9STejun Heo 2287635b75fcSTejun Heo #define PCPU_SETUP_BUG_ON(cond) do { \ 2288635b75fcSTejun Heo if (unlikely(cond)) { \ 2289870d4b12SJoe Perches pr_emerg("failed to initialize, %s\n", #cond); \ 2290870d4b12SJoe Perches pr_emerg("cpu_possible_mask=%*pb\n", \ 2291807de073STejun Heo cpumask_pr_args(cpu_possible_mask)); \ 2292635b75fcSTejun Heo pcpu_dump_alloc_info(KERN_EMERG, ai); \ 2293635b75fcSTejun Heo BUG(); \ 2294635b75fcSTejun Heo } \ 2295635b75fcSTejun Heo } while (0) 2296635b75fcSTejun Heo 22972f39e637STejun Heo /* sanity checks */ 2298635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->nr_groups <= 0); 2299bbddff05STejun Heo #ifdef CONFIG_SMP 2300635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!ai->static_size); 2301f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(__per_cpu_start)); 2302bbddff05STejun Heo #endif 2303635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!base_addr); 2304f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(base_addr)); 2305635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < size_sum); 2306f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(ai->unit_size)); 2307635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE); 2308ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->unit_size, PCPU_BITMAP_BLOCK_SIZE)); 2309099a19d9STejun Heo PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE); 2310fb29a2ccSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!ai->dyn_size); 2311d2f3c384SDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->reserved_size, PCPU_MIN_ALLOC_SIZE)); 2312ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!(IS_ALIGNED(PCPU_BITMAP_BLOCK_SIZE, PAGE_SIZE) || 2313ca460b3cSDennis Zhou (Facebook) IS_ALIGNED(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE))); 23149f645532STejun Heo PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0); 23158d408b4bSTejun Heo 23166563297cSTejun Heo /* process group information and build config tables accordingly */ 2317f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_offsets[0]); 2318f655f405SMike Rapoport group_offsets = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2319f655f405SMike Rapoport if (!group_offsets) 2320f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2321f655f405SMike Rapoport alloc_size); 2322f655f405SMike Rapoport 2323f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_sizes[0]); 2324f655f405SMike Rapoport group_sizes = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2325f655f405SMike Rapoport if (!group_sizes) 2326f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2327f655f405SMike Rapoport alloc_size); 2328f655f405SMike Rapoport 2329f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_map[0]); 2330f655f405SMike Rapoport unit_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2331f655f405SMike Rapoport if (!unit_map) 2332f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2333f655f405SMike Rapoport alloc_size); 2334f655f405SMike Rapoport 2335f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_off[0]); 2336f655f405SMike Rapoport unit_off = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2337f655f405SMike Rapoport if (!unit_off) 2338f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2339f655f405SMike Rapoport alloc_size); 23402f39e637STejun Heo 2341fd1e8a1fSTejun Heo for (cpu = 0; cpu < nr_cpu_ids; cpu++) 2342ffe0d5a5STejun Heo unit_map[cpu] = UINT_MAX; 2343a855b84cSTejun Heo 2344a855b84cSTejun Heo pcpu_low_unit_cpu = NR_CPUS; 2345a855b84cSTejun Heo pcpu_high_unit_cpu = NR_CPUS; 23462f39e637STejun Heo 2347fd1e8a1fSTejun Heo for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) { 2348fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 23492f39e637STejun Heo 23506563297cSTejun Heo group_offsets[group] = gi->base_offset; 23516563297cSTejun Heo group_sizes[group] = gi->nr_units * ai->unit_size; 23526563297cSTejun Heo 2353fd1e8a1fSTejun Heo for (i = 0; i < gi->nr_units; i++) { 2354fd1e8a1fSTejun Heo cpu = gi->cpu_map[i]; 2355fd1e8a1fSTejun Heo if (cpu == NR_CPUS) 2356fd1e8a1fSTejun Heo continue; 2357fd1e8a1fSTejun Heo 23589f295664SDan Carpenter PCPU_SETUP_BUG_ON(cpu >= nr_cpu_ids); 2359635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!cpu_possible(cpu)); 2360635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX); 2361fd1e8a1fSTejun Heo 2362fd1e8a1fSTejun Heo unit_map[cpu] = unit + i; 2363fb435d52STejun Heo unit_off[cpu] = gi->base_offset + i * ai->unit_size; 2364fb435d52STejun Heo 2365a855b84cSTejun Heo /* determine low/high unit_cpu */ 2366a855b84cSTejun Heo if (pcpu_low_unit_cpu == NR_CPUS || 2367a855b84cSTejun Heo unit_off[cpu] < unit_off[pcpu_low_unit_cpu]) 2368a855b84cSTejun Heo pcpu_low_unit_cpu = cpu; 2369a855b84cSTejun Heo if (pcpu_high_unit_cpu == NR_CPUS || 2370a855b84cSTejun Heo unit_off[cpu] > unit_off[pcpu_high_unit_cpu]) 2371a855b84cSTejun Heo pcpu_high_unit_cpu = cpu; 23720fc0531eSLinus Torvalds } 23730fc0531eSLinus Torvalds } 2374fd1e8a1fSTejun Heo pcpu_nr_units = unit; 23752f39e637STejun Heo 23762f39e637STejun Heo for_each_possible_cpu(cpu) 2377635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX); 2378635b75fcSTejun Heo 2379635b75fcSTejun Heo /* we're done parsing the input, undefine BUG macro and dump config */ 2380635b75fcSTejun Heo #undef PCPU_SETUP_BUG_ON 2381bcbea798STejun Heo pcpu_dump_alloc_info(KERN_DEBUG, ai); 23822f39e637STejun Heo 23836563297cSTejun Heo pcpu_nr_groups = ai->nr_groups; 23846563297cSTejun Heo pcpu_group_offsets = group_offsets; 23856563297cSTejun Heo pcpu_group_sizes = group_sizes; 2386fd1e8a1fSTejun Heo pcpu_unit_map = unit_map; 2387fb435d52STejun Heo pcpu_unit_offsets = unit_off; 23882f39e637STejun Heo 23892f39e637STejun Heo /* determine basic parameters */ 2390fd1e8a1fSTejun Heo pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT; 2391d9b55eebSTejun Heo pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; 23926563297cSTejun Heo pcpu_atom_size = ai->atom_size; 2393ce3141a2STejun Heo pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) + 2394ce3141a2STejun Heo BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long); 2395cafe8816STejun Heo 239630a5b536SDennis Zhou pcpu_stats_save_ai(ai); 239730a5b536SDennis Zhou 2398d9b55eebSTejun Heo /* 2399d9b55eebSTejun Heo * Allocate chunk slots. The additional last slot is for 2400d9b55eebSTejun Heo * empty chunks. 2401d9b55eebSTejun Heo */ 2402d9b55eebSTejun Heo pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2; 24037e1c4e27SMike Rapoport pcpu_slot = memblock_alloc(pcpu_nr_slots * sizeof(pcpu_slot[0]), 24047e1c4e27SMike Rapoport SMP_CACHE_BYTES); 2405f655f405SMike Rapoport if (!pcpu_slot) 2406f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2407f655f405SMike Rapoport pcpu_nr_slots * sizeof(pcpu_slot[0])); 2408fbf59bc9STejun Heo for (i = 0; i < pcpu_nr_slots; i++) 2409fbf59bc9STejun Heo INIT_LIST_HEAD(&pcpu_slot[i]); 2410fbf59bc9STejun Heo 2411edcb4639STejun Heo /* 2412d2f3c384SDennis Zhou (Facebook) * The end of the static region needs to be aligned with the 2413d2f3c384SDennis Zhou (Facebook) * minimum allocation size as this offsets the reserved and 2414d2f3c384SDennis Zhou (Facebook) * dynamic region. The first chunk ends page aligned by 2415d2f3c384SDennis Zhou (Facebook) * expanding the dynamic region, therefore the dynamic region 2416d2f3c384SDennis Zhou (Facebook) * can be shrunk to compensate while still staying above the 2417d2f3c384SDennis Zhou (Facebook) * configured sizes. 2418d2f3c384SDennis Zhou (Facebook) */ 2419d2f3c384SDennis Zhou (Facebook) static_size = ALIGN(ai->static_size, PCPU_MIN_ALLOC_SIZE); 2420d2f3c384SDennis Zhou (Facebook) dyn_size = ai->dyn_size - (static_size - ai->static_size); 2421d2f3c384SDennis Zhou (Facebook) 2422d2f3c384SDennis Zhou (Facebook) /* 2423c0ebfdc3SDennis Zhou (Facebook) * Initialize first chunk. 2424c0ebfdc3SDennis Zhou (Facebook) * If the reserved_size is non-zero, this initializes the reserved 2425c0ebfdc3SDennis Zhou (Facebook) * chunk. If the reserved_size is zero, the reserved chunk is NULL 2426c0ebfdc3SDennis Zhou (Facebook) * and the dynamic region is initialized here. The first chunk, 2427c0ebfdc3SDennis Zhou (Facebook) * pcpu_first_chunk, will always point to the chunk that serves 2428c0ebfdc3SDennis Zhou (Facebook) * the dynamic region. 2429edcb4639STejun Heo */ 2430d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size; 2431d2f3c384SDennis Zhou (Facebook) map_size = ai->reserved_size ?: dyn_size; 243240064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 243361ace7faSTejun Heo 2434edcb4639STejun Heo /* init dynamic chunk if necessary */ 2435b9c39442SDennis Zhou (Facebook) if (ai->reserved_size) { 24360c4169c3SDennis Zhou (Facebook) pcpu_reserved_chunk = chunk; 2437b9c39442SDennis Zhou (Facebook) 2438d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size + 2439c0ebfdc3SDennis Zhou (Facebook) ai->reserved_size; 2440d2f3c384SDennis Zhou (Facebook) map_size = dyn_size; 244140064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 2442edcb4639STejun Heo } 2443edcb4639STejun Heo 24442441d15cSTejun Heo /* link the first chunk in */ 24450c4169c3SDennis Zhou (Facebook) pcpu_first_chunk = chunk; 24460cecf50cSDennis Zhou (Facebook) pcpu_nr_empty_pop_pages = pcpu_first_chunk->nr_empty_pop_pages; 2447ae9e6bc9STejun Heo pcpu_chunk_relocate(pcpu_first_chunk, -1); 2448fbf59bc9STejun Heo 24497e8a6304SDennis Zhou (Facebook) /* include all regions of the first chunk */ 24507e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += PFN_DOWN(size_sum); 24517e8a6304SDennis Zhou (Facebook) 245230a5b536SDennis Zhou pcpu_stats_chunk_alloc(); 2453df95e795SDennis Zhou trace_percpu_create_chunk(base_addr); 245430a5b536SDennis Zhou 2455fbf59bc9STejun Heo /* we're done */ 2456bba174f5STejun Heo pcpu_base_addr = base_addr; 2457fbf59bc9STejun Heo } 245866c3a757STejun Heo 2459bbddff05STejun Heo #ifdef CONFIG_SMP 2460bbddff05STejun Heo 246117f3609cSAndi Kleen const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = { 2462f58dc01bSTejun Heo [PCPU_FC_AUTO] = "auto", 2463f58dc01bSTejun Heo [PCPU_FC_EMBED] = "embed", 2464f58dc01bSTejun Heo [PCPU_FC_PAGE] = "page", 2465f58dc01bSTejun Heo }; 246666c3a757STejun Heo 2467f58dc01bSTejun Heo enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO; 2468f58dc01bSTejun Heo 2469f58dc01bSTejun Heo static int __init percpu_alloc_setup(char *str) 247066c3a757STejun Heo { 24715479c78aSCyrill Gorcunov if (!str) 24725479c78aSCyrill Gorcunov return -EINVAL; 24735479c78aSCyrill Gorcunov 2474f58dc01bSTejun Heo if (0) 2475f58dc01bSTejun Heo /* nada */; 2476f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK 2477f58dc01bSTejun Heo else if (!strcmp(str, "embed")) 2478f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_EMBED; 2479f58dc01bSTejun Heo #endif 2480f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 2481f58dc01bSTejun Heo else if (!strcmp(str, "page")) 2482f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 2483f58dc01bSTejun Heo #endif 2484f58dc01bSTejun Heo else 2485870d4b12SJoe Perches pr_warn("unknown allocator %s specified\n", str); 248666c3a757STejun Heo 2487f58dc01bSTejun Heo return 0; 248866c3a757STejun Heo } 2489f58dc01bSTejun Heo early_param("percpu_alloc", percpu_alloc_setup); 249066c3a757STejun Heo 24913c9a024fSTejun Heo /* 24923c9a024fSTejun Heo * pcpu_embed_first_chunk() is used by the generic percpu setup. 24933c9a024fSTejun Heo * Build it if needed by the arch config or the generic setup is going 24943c9a024fSTejun Heo * to be used. 24953c9a024fSTejun Heo */ 249608fc4580STejun Heo #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \ 249708fc4580STejun Heo !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) 24983c9a024fSTejun Heo #define BUILD_EMBED_FIRST_CHUNK 24993c9a024fSTejun Heo #endif 25003c9a024fSTejun Heo 25013c9a024fSTejun Heo /* build pcpu_page_first_chunk() iff needed by the arch config */ 25023c9a024fSTejun Heo #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK) 25033c9a024fSTejun Heo #define BUILD_PAGE_FIRST_CHUNK 25043c9a024fSTejun Heo #endif 25053c9a024fSTejun Heo 25063c9a024fSTejun Heo /* pcpu_build_alloc_info() is used by both embed and page first chunk */ 25073c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK) 25083c9a024fSTejun Heo /** 2509fbf59bc9STejun Heo * pcpu_build_alloc_info - build alloc_info considering distances between CPUs 2510fbf59bc9STejun Heo * @reserved_size: the size of reserved percpu area in bytes 2511fbf59bc9STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 2512fbf59bc9STejun Heo * @atom_size: allocation atom size 2513fbf59bc9STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 2514fbf59bc9STejun Heo * 2515fbf59bc9STejun Heo * This function determines grouping of units, their mappings to cpus 2516fbf59bc9STejun Heo * and other parameters considering needed percpu size, allocation 2517fbf59bc9STejun Heo * atom size and distances between CPUs. 2518fbf59bc9STejun Heo * 2519bffc4375SYannick Guerrini * Groups are always multiples of atom size and CPUs which are of 2520fbf59bc9STejun Heo * LOCAL_DISTANCE both ways are grouped together and share space for 2521fbf59bc9STejun Heo * units in the same group. The returned configuration is guaranteed 2522fbf59bc9STejun Heo * to have CPUs on different nodes on different groups and >=75% usage 2523fbf59bc9STejun Heo * of allocated virtual address space. 2524fbf59bc9STejun Heo * 2525fbf59bc9STejun Heo * RETURNS: 2526fbf59bc9STejun Heo * On success, pointer to the new allocation_info is returned. On 2527fbf59bc9STejun Heo * failure, ERR_PTR value is returned. 2528fbf59bc9STejun Heo */ 2529fbf59bc9STejun Heo static struct pcpu_alloc_info * __init pcpu_build_alloc_info( 2530fbf59bc9STejun Heo size_t reserved_size, size_t dyn_size, 2531fbf59bc9STejun Heo size_t atom_size, 2532fbf59bc9STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn) 2533fbf59bc9STejun Heo { 2534fbf59bc9STejun Heo static int group_map[NR_CPUS] __initdata; 2535fbf59bc9STejun Heo static int group_cnt[NR_CPUS] __initdata; 2536fbf59bc9STejun Heo const size_t static_size = __per_cpu_end - __per_cpu_start; 2537fbf59bc9STejun Heo int nr_groups = 1, nr_units = 0; 2538fbf59bc9STejun Heo size_t size_sum, min_unit_size, alloc_size; 2539fbf59bc9STejun Heo int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */ 2540fbf59bc9STejun Heo int last_allocs, group, unit; 2541fbf59bc9STejun Heo unsigned int cpu, tcpu; 2542fbf59bc9STejun Heo struct pcpu_alloc_info *ai; 2543fbf59bc9STejun Heo unsigned int *cpu_map; 2544fbf59bc9STejun Heo 2545fbf59bc9STejun Heo /* this function may be called multiple times */ 2546fbf59bc9STejun Heo memset(group_map, 0, sizeof(group_map)); 2547fbf59bc9STejun Heo memset(group_cnt, 0, sizeof(group_cnt)); 2548fbf59bc9STejun Heo 2549fbf59bc9STejun Heo /* calculate size_sum and ensure dyn_size is enough for early alloc */ 2550fbf59bc9STejun Heo size_sum = PFN_ALIGN(static_size + reserved_size + 2551fbf59bc9STejun Heo max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE)); 2552fbf59bc9STejun Heo dyn_size = size_sum - static_size - reserved_size; 2553fbf59bc9STejun Heo 2554fbf59bc9STejun Heo /* 2555fbf59bc9STejun Heo * Determine min_unit_size, alloc_size and max_upa such that 2556fbf59bc9STejun Heo * alloc_size is multiple of atom_size and is the smallest 255725985edcSLucas De Marchi * which can accommodate 4k aligned segments which are equal to 2558fbf59bc9STejun Heo * or larger than min_unit_size. 2559fbf59bc9STejun Heo */ 2560fbf59bc9STejun Heo min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); 2561fbf59bc9STejun Heo 25629c015162SDennis Zhou (Facebook) /* determine the maximum # of units that can fit in an allocation */ 2563fbf59bc9STejun Heo alloc_size = roundup(min_unit_size, atom_size); 2564fbf59bc9STejun Heo upa = alloc_size / min_unit_size; 2565f09f1243SAlexander Kuleshov while (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2566fbf59bc9STejun Heo upa--; 2567fbf59bc9STejun Heo max_upa = upa; 2568fbf59bc9STejun Heo 2569fbf59bc9STejun Heo /* group cpus according to their proximity */ 2570fbf59bc9STejun Heo for_each_possible_cpu(cpu) { 2571fbf59bc9STejun Heo group = 0; 2572fbf59bc9STejun Heo next_group: 2573fbf59bc9STejun Heo for_each_possible_cpu(tcpu) { 2574fbf59bc9STejun Heo if (cpu == tcpu) 2575fbf59bc9STejun Heo break; 2576fbf59bc9STejun Heo if (group_map[tcpu] == group && cpu_distance_fn && 2577fbf59bc9STejun Heo (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE || 2578fbf59bc9STejun Heo cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) { 2579fbf59bc9STejun Heo group++; 2580fbf59bc9STejun Heo nr_groups = max(nr_groups, group + 1); 2581fbf59bc9STejun Heo goto next_group; 2582fbf59bc9STejun Heo } 2583fbf59bc9STejun Heo } 2584fbf59bc9STejun Heo group_map[cpu] = group; 2585fbf59bc9STejun Heo group_cnt[group]++; 2586fbf59bc9STejun Heo } 2587fbf59bc9STejun Heo 2588fbf59bc9STejun Heo /* 25899c015162SDennis Zhou (Facebook) * Wasted space is caused by a ratio imbalance of upa to group_cnt. 25909c015162SDennis Zhou (Facebook) * Expand the unit_size until we use >= 75% of the units allocated. 25919c015162SDennis Zhou (Facebook) * Related to atom_size, which could be much larger than the unit_size. 2592fbf59bc9STejun Heo */ 2593fbf59bc9STejun Heo last_allocs = INT_MAX; 2594fbf59bc9STejun Heo for (upa = max_upa; upa; upa--) { 2595fbf59bc9STejun Heo int allocs = 0, wasted = 0; 2596fbf59bc9STejun Heo 2597f09f1243SAlexander Kuleshov if (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2598fbf59bc9STejun Heo continue; 2599fbf59bc9STejun Heo 2600fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2601fbf59bc9STejun Heo int this_allocs = DIV_ROUND_UP(group_cnt[group], upa); 2602fbf59bc9STejun Heo allocs += this_allocs; 2603fbf59bc9STejun Heo wasted += this_allocs * upa - group_cnt[group]; 2604fbf59bc9STejun Heo } 2605fbf59bc9STejun Heo 2606fbf59bc9STejun Heo /* 2607fbf59bc9STejun Heo * Don't accept if wastage is over 1/3. The 2608fbf59bc9STejun Heo * greater-than comparison ensures upa==1 always 2609fbf59bc9STejun Heo * passes the following check. 2610fbf59bc9STejun Heo */ 2611fbf59bc9STejun Heo if (wasted > num_possible_cpus() / 3) 2612fbf59bc9STejun Heo continue; 2613fbf59bc9STejun Heo 2614fbf59bc9STejun Heo /* and then don't consume more memory */ 2615fbf59bc9STejun Heo if (allocs > last_allocs) 2616fbf59bc9STejun Heo break; 2617fbf59bc9STejun Heo last_allocs = allocs; 2618fbf59bc9STejun Heo best_upa = upa; 2619fbf59bc9STejun Heo } 2620fbf59bc9STejun Heo upa = best_upa; 2621fbf59bc9STejun Heo 2622fbf59bc9STejun Heo /* allocate and fill alloc_info */ 2623fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) 2624fbf59bc9STejun Heo nr_units += roundup(group_cnt[group], upa); 2625fbf59bc9STejun Heo 2626fbf59bc9STejun Heo ai = pcpu_alloc_alloc_info(nr_groups, nr_units); 2627fbf59bc9STejun Heo if (!ai) 2628fbf59bc9STejun Heo return ERR_PTR(-ENOMEM); 2629fbf59bc9STejun Heo cpu_map = ai->groups[0].cpu_map; 2630fbf59bc9STejun Heo 2631fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2632fbf59bc9STejun Heo ai->groups[group].cpu_map = cpu_map; 2633fbf59bc9STejun Heo cpu_map += roundup(group_cnt[group], upa); 2634fbf59bc9STejun Heo } 2635fbf59bc9STejun Heo 2636fbf59bc9STejun Heo ai->static_size = static_size; 2637fbf59bc9STejun Heo ai->reserved_size = reserved_size; 2638fbf59bc9STejun Heo ai->dyn_size = dyn_size; 2639fbf59bc9STejun Heo ai->unit_size = alloc_size / upa; 2640fbf59bc9STejun Heo ai->atom_size = atom_size; 2641fbf59bc9STejun Heo ai->alloc_size = alloc_size; 2642fbf59bc9STejun Heo 26432de7852fSPeng Fan for (group = 0, unit = 0; group < nr_groups; group++) { 2644fbf59bc9STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 2645fbf59bc9STejun Heo 2646fbf59bc9STejun Heo /* 2647fbf59bc9STejun Heo * Initialize base_offset as if all groups are located 2648fbf59bc9STejun Heo * back-to-back. The caller should update this to 2649fbf59bc9STejun Heo * reflect actual allocation. 2650fbf59bc9STejun Heo */ 2651fbf59bc9STejun Heo gi->base_offset = unit * ai->unit_size; 2652fbf59bc9STejun Heo 2653fbf59bc9STejun Heo for_each_possible_cpu(cpu) 2654fbf59bc9STejun Heo if (group_map[cpu] == group) 2655fbf59bc9STejun Heo gi->cpu_map[gi->nr_units++] = cpu; 2656fbf59bc9STejun Heo gi->nr_units = roundup(gi->nr_units, upa); 2657fbf59bc9STejun Heo unit += gi->nr_units; 2658fbf59bc9STejun Heo } 2659fbf59bc9STejun Heo BUG_ON(unit != nr_units); 2660fbf59bc9STejun Heo 2661fbf59bc9STejun Heo return ai; 2662fbf59bc9STejun Heo } 26633c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */ 2664fbf59bc9STejun Heo 26653c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) 266666c3a757STejun Heo /** 266766c3a757STejun Heo * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem 266866c3a757STejun Heo * @reserved_size: the size of reserved percpu area in bytes 26694ba6ce25STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 2670c8826dd5STejun Heo * @atom_size: allocation atom size 2671c8826dd5STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 2672c8826dd5STejun Heo * @alloc_fn: function to allocate percpu page 267325985edcSLucas De Marchi * @free_fn: function to free percpu page 267466c3a757STejun Heo * 267566c3a757STejun Heo * This is a helper to ease setting up embedded first percpu chunk and 267666c3a757STejun Heo * can be called where pcpu_setup_first_chunk() is expected. 267766c3a757STejun Heo * 267866c3a757STejun Heo * If this function is used to setup the first chunk, it is allocated 2679c8826dd5STejun Heo * by calling @alloc_fn and used as-is without being mapped into 2680c8826dd5STejun Heo * vmalloc area. Allocations are always whole multiples of @atom_size 2681c8826dd5STejun Heo * aligned to @atom_size. 2682c8826dd5STejun Heo * 2683c8826dd5STejun Heo * This enables the first chunk to piggy back on the linear physical 2684c8826dd5STejun Heo * mapping which often uses larger page size. Please note that this 2685c8826dd5STejun Heo * can result in very sparse cpu->unit mapping on NUMA machines thus 2686c8826dd5STejun Heo * requiring large vmalloc address space. Don't use this allocator if 2687c8826dd5STejun Heo * vmalloc space is not orders of magnitude larger than distances 2688c8826dd5STejun Heo * between node memory addresses (ie. 32bit NUMA machines). 268966c3a757STejun Heo * 26904ba6ce25STejun Heo * @dyn_size specifies the minimum dynamic area size. 269166c3a757STejun Heo * 269266c3a757STejun Heo * If the needed size is smaller than the minimum or specified unit 2693c8826dd5STejun Heo * size, the leftover is returned using @free_fn. 269466c3a757STejun Heo * 269566c3a757STejun Heo * RETURNS: 2696fb435d52STejun Heo * 0 on success, -errno on failure. 269766c3a757STejun Heo */ 26984ba6ce25STejun Heo int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size, 2699c8826dd5STejun Heo size_t atom_size, 2700c8826dd5STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn, 2701c8826dd5STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 2702c8826dd5STejun Heo pcpu_fc_free_fn_t free_fn) 270366c3a757STejun Heo { 2704c8826dd5STejun Heo void *base = (void *)ULONG_MAX; 2705c8826dd5STejun Heo void **areas = NULL; 2706fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 270793c76b6bSzijun_hu size_t size_sum, areas_size; 270893c76b6bSzijun_hu unsigned long max_distance; 2709163fa234SKefeng Wang int group, i, highest_group, rc = 0; 271066c3a757STejun Heo 2711c8826dd5STejun Heo ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size, 2712c8826dd5STejun Heo cpu_distance_fn); 2713fd1e8a1fSTejun Heo if (IS_ERR(ai)) 2714fd1e8a1fSTejun Heo return PTR_ERR(ai); 271566c3a757STejun Heo 2716fd1e8a1fSTejun Heo size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 2717c8826dd5STejun Heo areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *)); 271866c3a757STejun Heo 271926fb3daeSMike Rapoport areas = memblock_alloc(areas_size, SMP_CACHE_BYTES); 2720c8826dd5STejun Heo if (!areas) { 2721fb435d52STejun Heo rc = -ENOMEM; 2722c8826dd5STejun Heo goto out_free; 2723fa8a7094STejun Heo } 272466c3a757STejun Heo 27259b739662Szijun_hu /* allocate, copy and determine base address & max_distance */ 27269b739662Szijun_hu highest_group = 0; 2727c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) { 2728c8826dd5STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 2729c8826dd5STejun Heo unsigned int cpu = NR_CPUS; 2730c8826dd5STejun Heo void *ptr; 273166c3a757STejun Heo 2732c8826dd5STejun Heo for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++) 2733c8826dd5STejun Heo cpu = gi->cpu_map[i]; 2734c8826dd5STejun Heo BUG_ON(cpu == NR_CPUS); 2735c8826dd5STejun Heo 2736c8826dd5STejun Heo /* allocate space for the whole group */ 2737c8826dd5STejun Heo ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size); 2738c8826dd5STejun Heo if (!ptr) { 2739c8826dd5STejun Heo rc = -ENOMEM; 2740c8826dd5STejun Heo goto out_free_areas; 2741c8826dd5STejun Heo } 2742f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 2743f528f0b8SCatalin Marinas kmemleak_free(ptr); 2744c8826dd5STejun Heo areas[group] = ptr; 2745c8826dd5STejun Heo 2746c8826dd5STejun Heo base = min(ptr, base); 27479b739662Szijun_hu if (ptr > areas[highest_group]) 27489b739662Szijun_hu highest_group = group; 27499b739662Szijun_hu } 27509b739662Szijun_hu max_distance = areas[highest_group] - base; 27519b739662Szijun_hu max_distance += ai->unit_size * ai->groups[highest_group].nr_units; 27529b739662Szijun_hu 27539b739662Szijun_hu /* warn if maximum distance is further than 75% of vmalloc space */ 27549b739662Szijun_hu if (max_distance > VMALLOC_TOTAL * 3 / 4) { 27559b739662Szijun_hu pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n", 27569b739662Szijun_hu max_distance, VMALLOC_TOTAL); 27579b739662Szijun_hu #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 27589b739662Szijun_hu /* and fail if we have fallback */ 27599b739662Szijun_hu rc = -EINVAL; 27609b739662Szijun_hu goto out_free_areas; 27619b739662Szijun_hu #endif 276242b64281STejun Heo } 276342b64281STejun Heo 276442b64281STejun Heo /* 276542b64281STejun Heo * Copy data and free unused parts. This should happen after all 276642b64281STejun Heo * allocations are complete; otherwise, we may end up with 276742b64281STejun Heo * overlapping groups. 276842b64281STejun Heo */ 276942b64281STejun Heo for (group = 0; group < ai->nr_groups; group++) { 277042b64281STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 277142b64281STejun Heo void *ptr = areas[group]; 2772c8826dd5STejun Heo 2773c8826dd5STejun Heo for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) { 2774c8826dd5STejun Heo if (gi->cpu_map[i] == NR_CPUS) { 2775c8826dd5STejun Heo /* unused unit, free whole */ 2776c8826dd5STejun Heo free_fn(ptr, ai->unit_size); 2777c8826dd5STejun Heo continue; 2778c8826dd5STejun Heo } 2779c8826dd5STejun Heo /* copy and return the unused part */ 2780fd1e8a1fSTejun Heo memcpy(ptr, __per_cpu_load, ai->static_size); 2781c8826dd5STejun Heo free_fn(ptr + size_sum, ai->unit_size - size_sum); 2782c8826dd5STejun Heo } 278366c3a757STejun Heo } 278466c3a757STejun Heo 2785c8826dd5STejun Heo /* base address is now known, determine group base offsets */ 27866ea529a2STejun Heo for (group = 0; group < ai->nr_groups; group++) { 2787c8826dd5STejun Heo ai->groups[group].base_offset = areas[group] - base; 27886ea529a2STejun Heo } 2789c8826dd5STejun Heo 279000206a69SMatteo Croce pr_info("Embedded %zu pages/cpu s%zu r%zu d%zu u%zu\n", 279100206a69SMatteo Croce PFN_DOWN(size_sum), ai->static_size, ai->reserved_size, 2792fd1e8a1fSTejun Heo ai->dyn_size, ai->unit_size); 279366c3a757STejun Heo 2794163fa234SKefeng Wang pcpu_setup_first_chunk(ai, base); 2795c8826dd5STejun Heo goto out_free; 2796c8826dd5STejun Heo 2797c8826dd5STejun Heo out_free_areas: 2798c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) 2799f851c8d8SMichael Holzheu if (areas[group]) 2800c8826dd5STejun Heo free_fn(areas[group], 2801c8826dd5STejun Heo ai->groups[group].nr_units * ai->unit_size); 2802c8826dd5STejun Heo out_free: 2803fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 2804c8826dd5STejun Heo if (areas) 2805999c17e3SSantosh Shilimkar memblock_free_early(__pa(areas), areas_size); 2806fb435d52STejun Heo return rc; 2807d4b95f80STejun Heo } 28083c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK */ 2809d4b95f80STejun Heo 28103c9a024fSTejun Heo #ifdef BUILD_PAGE_FIRST_CHUNK 2811d4b95f80STejun Heo /** 281200ae4064STejun Heo * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages 2813d4b95f80STejun Heo * @reserved_size: the size of reserved percpu area in bytes 2814d4b95f80STejun Heo * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE 281525985edcSLucas De Marchi * @free_fn: function to free percpu page, always called with PAGE_SIZE 2816d4b95f80STejun Heo * @populate_pte_fn: function to populate pte 2817d4b95f80STejun Heo * 281800ae4064STejun Heo * This is a helper to ease setting up page-remapped first percpu 281900ae4064STejun Heo * chunk and can be called where pcpu_setup_first_chunk() is expected. 2820d4b95f80STejun Heo * 2821d4b95f80STejun Heo * This is the basic allocator. Static percpu area is allocated 2822d4b95f80STejun Heo * page-by-page into vmalloc area. 2823d4b95f80STejun Heo * 2824d4b95f80STejun Heo * RETURNS: 2825fb435d52STejun Heo * 0 on success, -errno on failure. 2826d4b95f80STejun Heo */ 2827fb435d52STejun Heo int __init pcpu_page_first_chunk(size_t reserved_size, 2828d4b95f80STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 2829d4b95f80STejun Heo pcpu_fc_free_fn_t free_fn, 2830d4b95f80STejun Heo pcpu_fc_populate_pte_fn_t populate_pte_fn) 2831d4b95f80STejun Heo { 28328f05a6a6STejun Heo static struct vm_struct vm; 2833fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 283400ae4064STejun Heo char psize_str[16]; 2835ce3141a2STejun Heo int unit_pages; 2836d4b95f80STejun Heo size_t pages_size; 2837ce3141a2STejun Heo struct page **pages; 2838163fa234SKefeng Wang int unit, i, j, rc = 0; 28398f606604Szijun_hu int upa; 28408f606604Szijun_hu int nr_g0_units; 2841d4b95f80STejun Heo 284200ae4064STejun Heo snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10); 284300ae4064STejun Heo 28444ba6ce25STejun Heo ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL); 2845fd1e8a1fSTejun Heo if (IS_ERR(ai)) 2846fd1e8a1fSTejun Heo return PTR_ERR(ai); 2847fd1e8a1fSTejun Heo BUG_ON(ai->nr_groups != 1); 28488f606604Szijun_hu upa = ai->alloc_size/ai->unit_size; 28498f606604Szijun_hu nr_g0_units = roundup(num_possible_cpus(), upa); 28500b59c25fSIgor Stoppa if (WARN_ON(ai->groups[0].nr_units != nr_g0_units)) { 28518f606604Szijun_hu pcpu_free_alloc_info(ai); 28528f606604Szijun_hu return -EINVAL; 28538f606604Szijun_hu } 2854fd1e8a1fSTejun Heo 2855fd1e8a1fSTejun Heo unit_pages = ai->unit_size >> PAGE_SHIFT; 2856d4b95f80STejun Heo 2857d4b95f80STejun Heo /* unaligned allocations can't be freed, round up to page size */ 2858fd1e8a1fSTejun Heo pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() * 2859fd1e8a1fSTejun Heo sizeof(pages[0])); 28607e1c4e27SMike Rapoport pages = memblock_alloc(pages_size, SMP_CACHE_BYTES); 2861f655f405SMike Rapoport if (!pages) 2862f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2863f655f405SMike Rapoport pages_size); 2864d4b95f80STejun Heo 28658f05a6a6STejun Heo /* allocate pages */ 2866d4b95f80STejun Heo j = 0; 28678f606604Szijun_hu for (unit = 0; unit < num_possible_cpus(); unit++) { 2868fd1e8a1fSTejun Heo unsigned int cpu = ai->groups[0].cpu_map[unit]; 28698f606604Szijun_hu for (i = 0; i < unit_pages; i++) { 2870d4b95f80STejun Heo void *ptr; 2871d4b95f80STejun Heo 28723cbc8565STejun Heo ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE); 2873d4b95f80STejun Heo if (!ptr) { 2874870d4b12SJoe Perches pr_warn("failed to allocate %s page for cpu%u\n", 2875598d8091SJoe Perches psize_str, cpu); 2876d4b95f80STejun Heo goto enomem; 2877d4b95f80STejun Heo } 2878f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 2879f528f0b8SCatalin Marinas kmemleak_free(ptr); 2880ce3141a2STejun Heo pages[j++] = virt_to_page(ptr); 2881d4b95f80STejun Heo } 28828f606604Szijun_hu } 2883d4b95f80STejun Heo 28848f05a6a6STejun Heo /* allocate vm area, map the pages and copy static data */ 28858f05a6a6STejun Heo vm.flags = VM_ALLOC; 2886fd1e8a1fSTejun Heo vm.size = num_possible_cpus() * ai->unit_size; 28878f05a6a6STejun Heo vm_area_register_early(&vm, PAGE_SIZE); 28888f05a6a6STejun Heo 2889fd1e8a1fSTejun Heo for (unit = 0; unit < num_possible_cpus(); unit++) { 28901d9d3257STejun Heo unsigned long unit_addr = 2891fd1e8a1fSTejun Heo (unsigned long)vm.addr + unit * ai->unit_size; 28928f05a6a6STejun Heo 2893ce3141a2STejun Heo for (i = 0; i < unit_pages; i++) 28948f05a6a6STejun Heo populate_pte_fn(unit_addr + (i << PAGE_SHIFT)); 28958f05a6a6STejun Heo 28968f05a6a6STejun Heo /* pte already populated, the following shouldn't fail */ 2897fb435d52STejun Heo rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages], 2898ce3141a2STejun Heo unit_pages); 2899fb435d52STejun Heo if (rc < 0) 2900fb435d52STejun Heo panic("failed to map percpu area, err=%d\n", rc); 29018f05a6a6STejun Heo 29028f05a6a6STejun Heo /* 29038f05a6a6STejun Heo * FIXME: Archs with virtual cache should flush local 29048f05a6a6STejun Heo * cache for the linear mapping here - something 29058f05a6a6STejun Heo * equivalent to flush_cache_vmap() on the local cpu. 29068f05a6a6STejun Heo * flush_cache_vmap() can't be used as most supporting 29078f05a6a6STejun Heo * data structures are not set up yet. 29088f05a6a6STejun Heo */ 29098f05a6a6STejun Heo 29108f05a6a6STejun Heo /* copy static data */ 2911fd1e8a1fSTejun Heo memcpy((void *)unit_addr, __per_cpu_load, ai->static_size); 291266c3a757STejun Heo } 291366c3a757STejun Heo 291466c3a757STejun Heo /* we're ready, commit */ 291500206a69SMatteo Croce pr_info("%d %s pages/cpu s%zu r%zu d%zu\n", 291600206a69SMatteo Croce unit_pages, psize_str, ai->static_size, 2917fd1e8a1fSTejun Heo ai->reserved_size, ai->dyn_size); 291866c3a757STejun Heo 2919163fa234SKefeng Wang pcpu_setup_first_chunk(ai, vm.addr); 2920d4b95f80STejun Heo goto out_free_ar; 2921d4b95f80STejun Heo 2922d4b95f80STejun Heo enomem: 2923d4b95f80STejun Heo while (--j >= 0) 2924ce3141a2STejun Heo free_fn(page_address(pages[j]), PAGE_SIZE); 2925fb435d52STejun Heo rc = -ENOMEM; 2926d4b95f80STejun Heo out_free_ar: 2927999c17e3SSantosh Shilimkar memblock_free_early(__pa(pages), pages_size); 2928fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 2929fb435d52STejun Heo return rc; 293066c3a757STejun Heo } 29313c9a024fSTejun Heo #endif /* BUILD_PAGE_FIRST_CHUNK */ 2932d4b95f80STejun Heo 2933bbddff05STejun Heo #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA 29348c4bfc6eSTejun Heo /* 2935bbddff05STejun Heo * Generic SMP percpu area setup. 2936e74e3962STejun Heo * 2937e74e3962STejun Heo * The embedding helper is used because its behavior closely resembles 2938e74e3962STejun Heo * the original non-dynamic generic percpu area setup. This is 2939e74e3962STejun Heo * important because many archs have addressing restrictions and might 2940e74e3962STejun Heo * fail if the percpu area is located far away from the previous 2941e74e3962STejun Heo * location. As an added bonus, in non-NUMA cases, embedding is 2942e74e3962STejun Heo * generally a good idea TLB-wise because percpu area can piggy back 2943e74e3962STejun Heo * on the physical linear memory mapping which uses large page 2944e74e3962STejun Heo * mappings on applicable archs. 2945e74e3962STejun Heo */ 2946e74e3962STejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; 2947e74e3962STejun Heo EXPORT_SYMBOL(__per_cpu_offset); 2948e74e3962STejun Heo 2949c8826dd5STejun Heo static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size, 2950c8826dd5STejun Heo size_t align) 2951c8826dd5STejun Heo { 295226fb3daeSMike Rapoport return memblock_alloc_from(size, align, __pa(MAX_DMA_ADDRESS)); 2953c8826dd5STejun Heo } 2954c8826dd5STejun Heo 2955c8826dd5STejun Heo static void __init pcpu_dfl_fc_free(void *ptr, size_t size) 2956c8826dd5STejun Heo { 2957999c17e3SSantosh Shilimkar memblock_free_early(__pa(ptr), size); 2958c8826dd5STejun Heo } 2959c8826dd5STejun Heo 2960e74e3962STejun Heo void __init setup_per_cpu_areas(void) 2961e74e3962STejun Heo { 2962e74e3962STejun Heo unsigned long delta; 2963e74e3962STejun Heo unsigned int cpu; 2964fb435d52STejun Heo int rc; 2965e74e3962STejun Heo 2966e74e3962STejun Heo /* 2967e74e3962STejun Heo * Always reserve area for module percpu variables. That's 2968e74e3962STejun Heo * what the legacy allocator did. 2969e74e3962STejun Heo */ 2970fb435d52STejun Heo rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, 2971c8826dd5STejun Heo PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL, 2972c8826dd5STejun Heo pcpu_dfl_fc_alloc, pcpu_dfl_fc_free); 2973fb435d52STejun Heo if (rc < 0) 2974bbddff05STejun Heo panic("Failed to initialize percpu areas."); 2975e74e3962STejun Heo 2976e74e3962STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 2977e74e3962STejun Heo for_each_possible_cpu(cpu) 2978fb435d52STejun Heo __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; 2979e74e3962STejun Heo } 2980e74e3962STejun Heo #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */ 2981099a19d9STejun Heo 2982bbddff05STejun Heo #else /* CONFIG_SMP */ 2983bbddff05STejun Heo 2984bbddff05STejun Heo /* 2985bbddff05STejun Heo * UP percpu area setup. 2986bbddff05STejun Heo * 2987bbddff05STejun Heo * UP always uses km-based percpu allocator with identity mapping. 2988bbddff05STejun Heo * Static percpu variables are indistinguishable from the usual static 2989bbddff05STejun Heo * variables and don't require any special preparation. 2990bbddff05STejun Heo */ 2991bbddff05STejun Heo void __init setup_per_cpu_areas(void) 2992bbddff05STejun Heo { 2993bbddff05STejun Heo const size_t unit_size = 2994bbddff05STejun Heo roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE, 2995bbddff05STejun Heo PERCPU_DYNAMIC_RESERVE)); 2996bbddff05STejun Heo struct pcpu_alloc_info *ai; 2997bbddff05STejun Heo void *fc; 2998bbddff05STejun Heo 2999bbddff05STejun Heo ai = pcpu_alloc_alloc_info(1, 1); 300026fb3daeSMike Rapoport fc = memblock_alloc_from(unit_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS)); 3001bbddff05STejun Heo if (!ai || !fc) 3002bbddff05STejun Heo panic("Failed to allocate memory for percpu areas."); 3003100d13c3SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 3004100d13c3SCatalin Marinas kmemleak_free(fc); 3005bbddff05STejun Heo 3006bbddff05STejun Heo ai->dyn_size = unit_size; 3007bbddff05STejun Heo ai->unit_size = unit_size; 3008bbddff05STejun Heo ai->atom_size = unit_size; 3009bbddff05STejun Heo ai->alloc_size = unit_size; 3010bbddff05STejun Heo ai->groups[0].nr_units = 1; 3011bbddff05STejun Heo ai->groups[0].cpu_map[0] = 0; 3012bbddff05STejun Heo 3013163fa234SKefeng Wang pcpu_setup_first_chunk(ai, fc); 3014438a5061SNicolas Pitre pcpu_free_alloc_info(ai); 3015bbddff05STejun Heo } 3016bbddff05STejun Heo 3017bbddff05STejun Heo #endif /* CONFIG_SMP */ 3018bbddff05STejun Heo 3019099a19d9STejun Heo /* 30207e8a6304SDennis Zhou (Facebook) * pcpu_nr_pages - calculate total number of populated backing pages 30217e8a6304SDennis Zhou (Facebook) * 30227e8a6304SDennis Zhou (Facebook) * This reflects the number of pages populated to back chunks. Metadata is 30237e8a6304SDennis Zhou (Facebook) * excluded in the number exposed in meminfo as the number of backing pages 30247e8a6304SDennis Zhou (Facebook) * scales with the number of cpus and can quickly outweigh the memory used for 30257e8a6304SDennis Zhou (Facebook) * metadata. It also keeps this calculation nice and simple. 30267e8a6304SDennis Zhou (Facebook) * 30277e8a6304SDennis Zhou (Facebook) * RETURNS: 30287e8a6304SDennis Zhou (Facebook) * Total number of populated backing pages in use by the allocator. 30297e8a6304SDennis Zhou (Facebook) */ 30307e8a6304SDennis Zhou (Facebook) unsigned long pcpu_nr_pages(void) 30317e8a6304SDennis Zhou (Facebook) { 30327e8a6304SDennis Zhou (Facebook) return pcpu_nr_populated * pcpu_nr_units; 30337e8a6304SDennis Zhou (Facebook) } 30347e8a6304SDennis Zhou (Facebook) 30357e8a6304SDennis Zhou (Facebook) /* 30361a4d7607STejun Heo * Percpu allocator is initialized early during boot when neither slab or 30371a4d7607STejun Heo * workqueue is available. Plug async management until everything is up 30381a4d7607STejun Heo * and running. 30391a4d7607STejun Heo */ 30401a4d7607STejun Heo static int __init percpu_enable_async(void) 30411a4d7607STejun Heo { 30421a4d7607STejun Heo pcpu_async_enabled = true; 30431a4d7607STejun Heo return 0; 30441a4d7607STejun Heo } 30451a4d7607STejun Heo subsys_initcall(percpu_enable_async); 3046