1fbf59bc9STejun Heo /* 288999a89STejun Heo * mm/percpu.c - percpu memory allocator 3fbf59bc9STejun Heo * 4fbf59bc9STejun Heo * Copyright (C) 2009 SUSE Linux Products GmbH 5fbf59bc9STejun Heo * Copyright (C) 2009 Tejun Heo <tj@kernel.org> 6fbf59bc9STejun Heo * 75e81ee3eSDennis Zhou (Facebook) * Copyright (C) 2017 Facebook Inc. 85e81ee3eSDennis Zhou (Facebook) * Copyright (C) 2017 Dennis Zhou <dennisszhou@gmail.com> 95e81ee3eSDennis Zhou (Facebook) * 109c015162SDennis Zhou (Facebook) * This file is released under the GPLv2 license. 11fbf59bc9STejun Heo * 129c015162SDennis Zhou (Facebook) * The percpu allocator handles both static and dynamic areas. Percpu 139c015162SDennis Zhou (Facebook) * areas are allocated in chunks which are divided into units. There is 149c015162SDennis Zhou (Facebook) * a 1-to-1 mapping for units to possible cpus. These units are grouped 159c015162SDennis Zhou (Facebook) * based on NUMA properties of the machine. 16fbf59bc9STejun Heo * 17fbf59bc9STejun Heo * c0 c1 c2 18fbf59bc9STejun Heo * ------------------- ------------------- ------------ 19fbf59bc9STejun Heo * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u 20fbf59bc9STejun Heo * ------------------- ...... ------------------- .... ------------ 21fbf59bc9STejun Heo * 229c015162SDennis Zhou (Facebook) * Allocation is done by offsets into a unit's address space. Ie., an 239c015162SDennis Zhou (Facebook) * area of 512 bytes at 6k in c1 occupies 512 bytes at 6k in c1:u0, 249c015162SDennis Zhou (Facebook) * c1:u1, c1:u2, etc. On NUMA machines, the mapping may be non-linear 259c015162SDennis Zhou (Facebook) * and even sparse. Access is handled by configuring percpu base 269c015162SDennis Zhou (Facebook) * registers according to the cpu to unit mappings and offsetting the 279c015162SDennis Zhou (Facebook) * base address using pcpu_unit_size. 28fbf59bc9STejun Heo * 299c015162SDennis Zhou (Facebook) * There is special consideration for the first chunk which must handle 309c015162SDennis Zhou (Facebook) * the static percpu variables in the kernel image as allocation services 315e81ee3eSDennis Zhou (Facebook) * are not online yet. In short, the first chunk is structured like so: 329c015162SDennis Zhou (Facebook) * 339c015162SDennis Zhou (Facebook) * <Static | [Reserved] | Dynamic> 349c015162SDennis Zhou (Facebook) * 359c015162SDennis Zhou (Facebook) * The static data is copied from the original section managed by the 369c015162SDennis Zhou (Facebook) * linker. The reserved section, if non-zero, primarily manages static 379c015162SDennis Zhou (Facebook) * percpu variables from kernel modules. Finally, the dynamic section 389c015162SDennis Zhou (Facebook) * takes care of normal allocations. 39fbf59bc9STejun Heo * 405e81ee3eSDennis Zhou (Facebook) * The allocator organizes chunks into lists according to free size and 415e81ee3eSDennis Zhou (Facebook) * tries to allocate from the fullest chunk first. Each chunk is managed 425e81ee3eSDennis Zhou (Facebook) * by a bitmap with metadata blocks. The allocation map is updated on 435e81ee3eSDennis Zhou (Facebook) * every allocation and free to reflect the current state while the boundary 445e81ee3eSDennis Zhou (Facebook) * map is only updated on allocation. Each metadata block contains 455e81ee3eSDennis Zhou (Facebook) * information to help mitigate the need to iterate over large portions 465e81ee3eSDennis Zhou (Facebook) * of the bitmap. The reverse mapping from page to chunk is stored in 475e81ee3eSDennis Zhou (Facebook) * the page's index. Lastly, units are lazily backed and grow in unison. 48fbf59bc9STejun Heo * 495e81ee3eSDennis Zhou (Facebook) * There is a unique conversion that goes on here between bytes and bits. 505e81ee3eSDennis Zhou (Facebook) * Each bit represents a fragment of size PCPU_MIN_ALLOC_SIZE. The chunk 515e81ee3eSDennis Zhou (Facebook) * tracks the number of pages it is responsible for in nr_pages. Helper 525e81ee3eSDennis Zhou (Facebook) * functions are used to convert from between the bytes, bits, and blocks. 535e81ee3eSDennis Zhou (Facebook) * All hints are managed in bits unless explicitly stated. 549c015162SDennis Zhou (Facebook) * 554091fb95SMasahiro Yamada * To use this allocator, arch code should do the following: 56fbf59bc9STejun Heo * 57fbf59bc9STejun Heo * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate 58e0100983STejun Heo * regular address to percpu pointer and back if they need to be 59e0100983STejun Heo * different from the default 60fbf59bc9STejun Heo * 618d408b4bSTejun Heo * - use pcpu_setup_first_chunk() during percpu area initialization to 628d408b4bSTejun Heo * setup the first chunk containing the kernel static percpu area 63fbf59bc9STejun Heo */ 64fbf59bc9STejun Heo 65870d4b12SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 66870d4b12SJoe Perches 67fbf59bc9STejun Heo #include <linux/bitmap.h> 6857c8a661SMike Rapoport #include <linux/memblock.h> 69fd1e8a1fSTejun Heo #include <linux/err.h> 70ca460b3cSDennis Zhou (Facebook) #include <linux/lcm.h> 71fbf59bc9STejun Heo #include <linux/list.h> 72a530b795STejun Heo #include <linux/log2.h> 73fbf59bc9STejun Heo #include <linux/mm.h> 74fbf59bc9STejun Heo #include <linux/module.h> 75fbf59bc9STejun Heo #include <linux/mutex.h> 76fbf59bc9STejun Heo #include <linux/percpu.h> 77fbf59bc9STejun Heo #include <linux/pfn.h> 78fbf59bc9STejun Heo #include <linux/slab.h> 79ccea34b5STejun Heo #include <linux/spinlock.h> 80fbf59bc9STejun Heo #include <linux/vmalloc.h> 81a56dbddfSTejun Heo #include <linux/workqueue.h> 82f528f0b8SCatalin Marinas #include <linux/kmemleak.h> 8371546d10STejun Heo #include <linux/sched.h> 84fbf59bc9STejun Heo 85fbf59bc9STejun Heo #include <asm/cacheflush.h> 86e0100983STejun Heo #include <asm/sections.h> 87fbf59bc9STejun Heo #include <asm/tlbflush.h> 883b034b0dSVivek Goyal #include <asm/io.h> 89fbf59bc9STejun Heo 90df95e795SDennis Zhou #define CREATE_TRACE_POINTS 91df95e795SDennis Zhou #include <trace/events/percpu.h> 92df95e795SDennis Zhou 938fa3ed80SDennis Zhou #include "percpu-internal.h" 948fa3ed80SDennis Zhou 9540064aecSDennis Zhou (Facebook) /* the slots are sorted by free bytes left, 1-31 bytes share the same slot */ 9640064aecSDennis Zhou (Facebook) #define PCPU_SLOT_BASE_SHIFT 5 9740064aecSDennis Zhou (Facebook) 981a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_LOW 2 991a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_HIGH 4 100fbf59bc9STejun Heo 101bbddff05STejun Heo #ifdef CONFIG_SMP 102e0100983STejun Heo /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */ 103e0100983STejun Heo #ifndef __addr_to_pcpu_ptr 104e0100983STejun Heo #define __addr_to_pcpu_ptr(addr) \ 10543cf38ebSTejun Heo (void __percpu *)((unsigned long)(addr) - \ 10643cf38ebSTejun Heo (unsigned long)pcpu_base_addr + \ 10743cf38ebSTejun Heo (unsigned long)__per_cpu_start) 108e0100983STejun Heo #endif 109e0100983STejun Heo #ifndef __pcpu_ptr_to_addr 110e0100983STejun Heo #define __pcpu_ptr_to_addr(ptr) \ 11143cf38ebSTejun Heo (void __force *)((unsigned long)(ptr) + \ 11243cf38ebSTejun Heo (unsigned long)pcpu_base_addr - \ 11343cf38ebSTejun Heo (unsigned long)__per_cpu_start) 114e0100983STejun Heo #endif 115bbddff05STejun Heo #else /* CONFIG_SMP */ 116bbddff05STejun Heo /* on UP, it's always identity mapped */ 117bbddff05STejun Heo #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr) 118bbddff05STejun Heo #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr) 119bbddff05STejun Heo #endif /* CONFIG_SMP */ 120e0100983STejun Heo 1211328710bSDaniel Micay static int pcpu_unit_pages __ro_after_init; 1221328710bSDaniel Micay static int pcpu_unit_size __ro_after_init; 1231328710bSDaniel Micay static int pcpu_nr_units __ro_after_init; 1241328710bSDaniel Micay static int pcpu_atom_size __ro_after_init; 1258fa3ed80SDennis Zhou int pcpu_nr_slots __ro_after_init; 1261328710bSDaniel Micay static size_t pcpu_chunk_struct_size __ro_after_init; 127fbf59bc9STejun Heo 128a855b84cSTejun Heo /* cpus with the lowest and highest unit addresses */ 1291328710bSDaniel Micay static unsigned int pcpu_low_unit_cpu __ro_after_init; 1301328710bSDaniel Micay static unsigned int pcpu_high_unit_cpu __ro_after_init; 1312f39e637STejun Heo 132fbf59bc9STejun Heo /* the address of the first chunk which starts with the kernel static area */ 1331328710bSDaniel Micay void *pcpu_base_addr __ro_after_init; 134fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(pcpu_base_addr); 135fbf59bc9STejun Heo 1361328710bSDaniel Micay static const int *pcpu_unit_map __ro_after_init; /* cpu -> unit */ 1371328710bSDaniel Micay const unsigned long *pcpu_unit_offsets __ro_after_init; /* cpu -> unit offset */ 1382f39e637STejun Heo 1396563297cSTejun Heo /* group information, used for vm allocation */ 1401328710bSDaniel Micay static int pcpu_nr_groups __ro_after_init; 1411328710bSDaniel Micay static const unsigned long *pcpu_group_offsets __ro_after_init; 1421328710bSDaniel Micay static const size_t *pcpu_group_sizes __ro_after_init; 1436563297cSTejun Heo 144ae9e6bc9STejun Heo /* 145ae9e6bc9STejun Heo * The first chunk which always exists. Note that unlike other 146ae9e6bc9STejun Heo * chunks, this one can be allocated and mapped in several different 147ae9e6bc9STejun Heo * ways and thus often doesn't live in the vmalloc area. 148ae9e6bc9STejun Heo */ 1498fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_first_chunk __ro_after_init; 150ae9e6bc9STejun Heo 151ae9e6bc9STejun Heo /* 152ae9e6bc9STejun Heo * Optional reserved chunk. This chunk reserves part of the first 153e2266705SDennis Zhou (Facebook) * chunk and serves it for reserved allocations. When the reserved 154e2266705SDennis Zhou (Facebook) * region doesn't exist, the following variable is NULL. 155ae9e6bc9STejun Heo */ 1568fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_reserved_chunk __ro_after_init; 157edcb4639STejun Heo 1588fa3ed80SDennis Zhou DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */ 1596710e594STejun Heo static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */ 160fbf59bc9STejun Heo 1618fa3ed80SDennis Zhou struct list_head *pcpu_slot __ro_after_init; /* chunk list slots */ 162fbf59bc9STejun Heo 1634f996e23STejun Heo /* chunks which need their map areas extended, protected by pcpu_lock */ 1644f996e23STejun Heo static LIST_HEAD(pcpu_map_extend_chunks); 1654f996e23STejun Heo 166b539b87fSTejun Heo /* 167b539b87fSTejun Heo * The number of empty populated pages, protected by pcpu_lock. The 168b539b87fSTejun Heo * reserved chunk doesn't contribute to the count. 169b539b87fSTejun Heo */ 1706b9b6f39SDennis Zhou (Facebook) int pcpu_nr_empty_pop_pages; 171b539b87fSTejun Heo 1721a4d7607STejun Heo /* 1737e8a6304SDennis Zhou (Facebook) * The number of populated pages in use by the allocator, protected by 1747e8a6304SDennis Zhou (Facebook) * pcpu_lock. This number is kept per a unit per chunk (i.e. when a page gets 1757e8a6304SDennis Zhou (Facebook) * allocated/deallocated, it is allocated/deallocated in all units of a chunk 1767e8a6304SDennis Zhou (Facebook) * and increments/decrements this count by 1). 1777e8a6304SDennis Zhou (Facebook) */ 1787e8a6304SDennis Zhou (Facebook) static unsigned long pcpu_nr_populated; 1797e8a6304SDennis Zhou (Facebook) 1807e8a6304SDennis Zhou (Facebook) /* 1811a4d7607STejun Heo * Balance work is used to populate or destroy chunks asynchronously. We 1821a4d7607STejun Heo * try to keep the number of populated free pages between 1831a4d7607STejun Heo * PCPU_EMPTY_POP_PAGES_LOW and HIGH for atomic allocations and at most one 1841a4d7607STejun Heo * empty chunk. 1851a4d7607STejun Heo */ 186fe6bd8c3STejun Heo static void pcpu_balance_workfn(struct work_struct *work); 187fe6bd8c3STejun Heo static DECLARE_WORK(pcpu_balance_work, pcpu_balance_workfn); 1881a4d7607STejun Heo static bool pcpu_async_enabled __read_mostly; 1891a4d7607STejun Heo static bool pcpu_atomic_alloc_failed; 1901a4d7607STejun Heo 1911a4d7607STejun Heo static void pcpu_schedule_balance_work(void) 1921a4d7607STejun Heo { 1931a4d7607STejun Heo if (pcpu_async_enabled) 1941a4d7607STejun Heo schedule_work(&pcpu_balance_work); 1951a4d7607STejun Heo } 196a56dbddfSTejun Heo 197c0ebfdc3SDennis Zhou (Facebook) /** 198560f2c23SDennis Zhou (Facebook) * pcpu_addr_in_chunk - check if the address is served from this chunk 199560f2c23SDennis Zhou (Facebook) * @chunk: chunk of interest 200560f2c23SDennis Zhou (Facebook) * @addr: percpu address 201c0ebfdc3SDennis Zhou (Facebook) * 202c0ebfdc3SDennis Zhou (Facebook) * RETURNS: 203560f2c23SDennis Zhou (Facebook) * True if the address is served from this chunk. 204c0ebfdc3SDennis Zhou (Facebook) */ 205560f2c23SDennis Zhou (Facebook) static bool pcpu_addr_in_chunk(struct pcpu_chunk *chunk, void *addr) 206020ec653STejun Heo { 207c0ebfdc3SDennis Zhou (Facebook) void *start_addr, *end_addr; 208020ec653STejun Heo 209560f2c23SDennis Zhou (Facebook) if (!chunk) 210c0ebfdc3SDennis Zhou (Facebook) return false; 211c0ebfdc3SDennis Zhou (Facebook) 212560f2c23SDennis Zhou (Facebook) start_addr = chunk->base_addr + chunk->start_offset; 213560f2c23SDennis Zhou (Facebook) end_addr = chunk->base_addr + chunk->nr_pages * PAGE_SIZE - 214560f2c23SDennis Zhou (Facebook) chunk->end_offset; 215c0ebfdc3SDennis Zhou (Facebook) 216c0ebfdc3SDennis Zhou (Facebook) return addr >= start_addr && addr < end_addr; 217020ec653STejun Heo } 218020ec653STejun Heo 219d9b55eebSTejun Heo static int __pcpu_size_to_slot(int size) 220fbf59bc9STejun Heo { 221cae3aeb8STejun Heo int highbit = fls(size); /* size is in bytes */ 222fbf59bc9STejun Heo return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1); 223fbf59bc9STejun Heo } 224fbf59bc9STejun Heo 225d9b55eebSTejun Heo static int pcpu_size_to_slot(int size) 226d9b55eebSTejun Heo { 227d9b55eebSTejun Heo if (size == pcpu_unit_size) 228d9b55eebSTejun Heo return pcpu_nr_slots - 1; 229d9b55eebSTejun Heo return __pcpu_size_to_slot(size); 230d9b55eebSTejun Heo } 231d9b55eebSTejun Heo 232fbf59bc9STejun Heo static int pcpu_chunk_slot(const struct pcpu_chunk *chunk) 233fbf59bc9STejun Heo { 23440064aecSDennis Zhou (Facebook) if (chunk->free_bytes < PCPU_MIN_ALLOC_SIZE || chunk->contig_bits == 0) 235fbf59bc9STejun Heo return 0; 236fbf59bc9STejun Heo 23740064aecSDennis Zhou (Facebook) return pcpu_size_to_slot(chunk->free_bytes); 238fbf59bc9STejun Heo } 239fbf59bc9STejun Heo 24088999a89STejun Heo /* set the pointer to a chunk in a page struct */ 24188999a89STejun Heo static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu) 24288999a89STejun Heo { 24388999a89STejun Heo page->index = (unsigned long)pcpu; 24488999a89STejun Heo } 24588999a89STejun Heo 24688999a89STejun Heo /* obtain pointer to a chunk from a page struct */ 24788999a89STejun Heo static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page) 24888999a89STejun Heo { 24988999a89STejun Heo return (struct pcpu_chunk *)page->index; 25088999a89STejun Heo } 25188999a89STejun Heo 25288999a89STejun Heo static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx) 253fbf59bc9STejun Heo { 2542f39e637STejun Heo return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx; 255fbf59bc9STejun Heo } 256fbf59bc9STejun Heo 257c0ebfdc3SDennis Zhou (Facebook) static unsigned long pcpu_unit_page_offset(unsigned int cpu, int page_idx) 258c0ebfdc3SDennis Zhou (Facebook) { 259c0ebfdc3SDennis Zhou (Facebook) return pcpu_unit_offsets[cpu] + (page_idx << PAGE_SHIFT); 260c0ebfdc3SDennis Zhou (Facebook) } 261c0ebfdc3SDennis Zhou (Facebook) 2629983b6f0STejun Heo static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk, 263fbf59bc9STejun Heo unsigned int cpu, int page_idx) 264fbf59bc9STejun Heo { 265c0ebfdc3SDennis Zhou (Facebook) return (unsigned long)chunk->base_addr + 266c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(cpu, page_idx); 267fbf59bc9STejun Heo } 268fbf59bc9STejun Heo 26991e914c5SDennis Zhou (Facebook) static void pcpu_next_unpop(unsigned long *bitmap, int *rs, int *re, int end) 270ce3141a2STejun Heo { 27191e914c5SDennis Zhou (Facebook) *rs = find_next_zero_bit(bitmap, end, *rs); 27291e914c5SDennis Zhou (Facebook) *re = find_next_bit(bitmap, end, *rs + 1); 273ce3141a2STejun Heo } 274ce3141a2STejun Heo 27591e914c5SDennis Zhou (Facebook) static void pcpu_next_pop(unsigned long *bitmap, int *rs, int *re, int end) 276ce3141a2STejun Heo { 27791e914c5SDennis Zhou (Facebook) *rs = find_next_bit(bitmap, end, *rs); 27891e914c5SDennis Zhou (Facebook) *re = find_next_zero_bit(bitmap, end, *rs + 1); 279ce3141a2STejun Heo } 280ce3141a2STejun Heo 281ce3141a2STejun Heo /* 28291e914c5SDennis Zhou (Facebook) * Bitmap region iterators. Iterates over the bitmap between 28391e914c5SDennis Zhou (Facebook) * [@start, @end) in @chunk. @rs and @re should be integer variables 28491e914c5SDennis Zhou (Facebook) * and will be set to start and end index of the current free region. 285ce3141a2STejun Heo */ 28691e914c5SDennis Zhou (Facebook) #define pcpu_for_each_unpop_region(bitmap, rs, re, start, end) \ 28791e914c5SDennis Zhou (Facebook) for ((rs) = (start), pcpu_next_unpop((bitmap), &(rs), &(re), (end)); \ 288ce3141a2STejun Heo (rs) < (re); \ 28991e914c5SDennis Zhou (Facebook) (rs) = (re) + 1, pcpu_next_unpop((bitmap), &(rs), &(re), (end))) 290ce3141a2STejun Heo 29191e914c5SDennis Zhou (Facebook) #define pcpu_for_each_pop_region(bitmap, rs, re, start, end) \ 29291e914c5SDennis Zhou (Facebook) for ((rs) = (start), pcpu_next_pop((bitmap), &(rs), &(re), (end)); \ 293ce3141a2STejun Heo (rs) < (re); \ 29491e914c5SDennis Zhou (Facebook) (rs) = (re) + 1, pcpu_next_pop((bitmap), &(rs), &(re), (end))) 295ce3141a2STejun Heo 296ca460b3cSDennis Zhou (Facebook) /* 297ca460b3cSDennis Zhou (Facebook) * The following are helper functions to help access bitmaps and convert 298ca460b3cSDennis Zhou (Facebook) * between bitmap offsets to address offsets. 299ca460b3cSDennis Zhou (Facebook) */ 300ca460b3cSDennis Zhou (Facebook) static unsigned long *pcpu_index_alloc_map(struct pcpu_chunk *chunk, int index) 301ca460b3cSDennis Zhou (Facebook) { 302ca460b3cSDennis Zhou (Facebook) return chunk->alloc_map + 303ca460b3cSDennis Zhou (Facebook) (index * PCPU_BITMAP_BLOCK_BITS / BITS_PER_LONG); 304ca460b3cSDennis Zhou (Facebook) } 305ca460b3cSDennis Zhou (Facebook) 306ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_index(int off) 307ca460b3cSDennis Zhou (Facebook) { 308ca460b3cSDennis Zhou (Facebook) return off / PCPU_BITMAP_BLOCK_BITS; 309ca460b3cSDennis Zhou (Facebook) } 310ca460b3cSDennis Zhou (Facebook) 311ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_off(int off) 312ca460b3cSDennis Zhou (Facebook) { 313ca460b3cSDennis Zhou (Facebook) return off & (PCPU_BITMAP_BLOCK_BITS - 1); 314ca460b3cSDennis Zhou (Facebook) } 315ca460b3cSDennis Zhou (Facebook) 316b185cd0dSDennis Zhou (Facebook) static unsigned long pcpu_block_off_to_off(int index, int off) 317b185cd0dSDennis Zhou (Facebook) { 318b185cd0dSDennis Zhou (Facebook) return index * PCPU_BITMAP_BLOCK_BITS + off; 319b185cd0dSDennis Zhou (Facebook) } 320b185cd0dSDennis Zhou (Facebook) 321fbf59bc9STejun Heo /** 322525ca84dSDennis Zhou (Facebook) * pcpu_next_md_free_region - finds the next hint free area 323525ca84dSDennis Zhou (Facebook) * @chunk: chunk of interest 324525ca84dSDennis Zhou (Facebook) * @bit_off: chunk offset 325525ca84dSDennis Zhou (Facebook) * @bits: size of free area 326525ca84dSDennis Zhou (Facebook) * 327525ca84dSDennis Zhou (Facebook) * Helper function for pcpu_for_each_md_free_region. It checks 328525ca84dSDennis Zhou (Facebook) * block->contig_hint and performs aggregation across blocks to find the 329525ca84dSDennis Zhou (Facebook) * next hint. It modifies bit_off and bits in-place to be consumed in the 330525ca84dSDennis Zhou (Facebook) * loop. 331525ca84dSDennis Zhou (Facebook) */ 332525ca84dSDennis Zhou (Facebook) static void pcpu_next_md_free_region(struct pcpu_chunk *chunk, int *bit_off, 333525ca84dSDennis Zhou (Facebook) int *bits) 334525ca84dSDennis Zhou (Facebook) { 335525ca84dSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 336525ca84dSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 337525ca84dSDennis Zhou (Facebook) struct pcpu_block_md *block; 338525ca84dSDennis Zhou (Facebook) 339525ca84dSDennis Zhou (Facebook) *bits = 0; 340525ca84dSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 341525ca84dSDennis Zhou (Facebook) block++, i++) { 342525ca84dSDennis Zhou (Facebook) /* handles contig area across blocks */ 343525ca84dSDennis Zhou (Facebook) if (*bits) { 344525ca84dSDennis Zhou (Facebook) *bits += block->left_free; 345525ca84dSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 346525ca84dSDennis Zhou (Facebook) continue; 347525ca84dSDennis Zhou (Facebook) return; 348525ca84dSDennis Zhou (Facebook) } 349525ca84dSDennis Zhou (Facebook) 350525ca84dSDennis Zhou (Facebook) /* 351525ca84dSDennis Zhou (Facebook) * This checks three things. First is there a contig_hint to 352525ca84dSDennis Zhou (Facebook) * check. Second, have we checked this hint before by 353525ca84dSDennis Zhou (Facebook) * comparing the block_off. Third, is this the same as the 354525ca84dSDennis Zhou (Facebook) * right contig hint. In the last case, it spills over into 355525ca84dSDennis Zhou (Facebook) * the next block and should be handled by the contig area 356525ca84dSDennis Zhou (Facebook) * across blocks code. 357525ca84dSDennis Zhou (Facebook) */ 358525ca84dSDennis Zhou (Facebook) *bits = block->contig_hint; 359525ca84dSDennis Zhou (Facebook) if (*bits && block->contig_hint_start >= block_off && 360525ca84dSDennis Zhou (Facebook) *bits + block->contig_hint_start < PCPU_BITMAP_BLOCK_BITS) { 361525ca84dSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, 362525ca84dSDennis Zhou (Facebook) block->contig_hint_start); 363525ca84dSDennis Zhou (Facebook) return; 364525ca84dSDennis Zhou (Facebook) } 3651fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 3661fa4df3eSDennis Zhou block_off = 0; 367525ca84dSDennis Zhou (Facebook) 368525ca84dSDennis Zhou (Facebook) *bits = block->right_free; 369525ca84dSDennis Zhou (Facebook) *bit_off = (i + 1) * PCPU_BITMAP_BLOCK_BITS - block->right_free; 370525ca84dSDennis Zhou (Facebook) } 371525ca84dSDennis Zhou (Facebook) } 372525ca84dSDennis Zhou (Facebook) 373b4c2116cSDennis Zhou (Facebook) /** 374b4c2116cSDennis Zhou (Facebook) * pcpu_next_fit_region - finds fit areas for a given allocation request 375b4c2116cSDennis Zhou (Facebook) * @chunk: chunk of interest 376b4c2116cSDennis Zhou (Facebook) * @alloc_bits: size of allocation 377b4c2116cSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 378b4c2116cSDennis Zhou (Facebook) * @bit_off: chunk offset 379b4c2116cSDennis Zhou (Facebook) * @bits: size of free area 380b4c2116cSDennis Zhou (Facebook) * 381b4c2116cSDennis Zhou (Facebook) * Finds the next free region that is viable for use with a given size and 382b4c2116cSDennis Zhou (Facebook) * alignment. This only returns if there is a valid area to be used for this 383b4c2116cSDennis Zhou (Facebook) * allocation. block->first_free is returned if the allocation request fits 384b4c2116cSDennis Zhou (Facebook) * within the block to see if the request can be fulfilled prior to the contig 385b4c2116cSDennis Zhou (Facebook) * hint. 386b4c2116cSDennis Zhou (Facebook) */ 387b4c2116cSDennis Zhou (Facebook) static void pcpu_next_fit_region(struct pcpu_chunk *chunk, int alloc_bits, 388b4c2116cSDennis Zhou (Facebook) int align, int *bit_off, int *bits) 389b4c2116cSDennis Zhou (Facebook) { 390b4c2116cSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 391b4c2116cSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 392b4c2116cSDennis Zhou (Facebook) struct pcpu_block_md *block; 393b4c2116cSDennis Zhou (Facebook) 394b4c2116cSDennis Zhou (Facebook) *bits = 0; 395b4c2116cSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 396b4c2116cSDennis Zhou (Facebook) block++, i++) { 397b4c2116cSDennis Zhou (Facebook) /* handles contig area across blocks */ 398b4c2116cSDennis Zhou (Facebook) if (*bits) { 399b4c2116cSDennis Zhou (Facebook) *bits += block->left_free; 400b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 401b4c2116cSDennis Zhou (Facebook) return; 402b4c2116cSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 403b4c2116cSDennis Zhou (Facebook) continue; 404b4c2116cSDennis Zhou (Facebook) } 405b4c2116cSDennis Zhou (Facebook) 406b4c2116cSDennis Zhou (Facebook) /* check block->contig_hint */ 407b4c2116cSDennis Zhou (Facebook) *bits = ALIGN(block->contig_hint_start, align) - 408b4c2116cSDennis Zhou (Facebook) block->contig_hint_start; 409b4c2116cSDennis Zhou (Facebook) /* 410b4c2116cSDennis Zhou (Facebook) * This uses the block offset to determine if this has been 411b4c2116cSDennis Zhou (Facebook) * checked in the prior iteration. 412b4c2116cSDennis Zhou (Facebook) */ 413b4c2116cSDennis Zhou (Facebook) if (block->contig_hint && 414b4c2116cSDennis Zhou (Facebook) block->contig_hint_start >= block_off && 415b4c2116cSDennis Zhou (Facebook) block->contig_hint >= *bits + alloc_bits) { 416b4c2116cSDennis Zhou (Facebook) *bits += alloc_bits + block->contig_hint_start - 417b4c2116cSDennis Zhou (Facebook) block->first_free; 418b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, block->first_free); 419b4c2116cSDennis Zhou (Facebook) return; 420b4c2116cSDennis Zhou (Facebook) } 4211fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 4221fa4df3eSDennis Zhou block_off = 0; 423b4c2116cSDennis Zhou (Facebook) 424b4c2116cSDennis Zhou (Facebook) *bit_off = ALIGN(PCPU_BITMAP_BLOCK_BITS - block->right_free, 425b4c2116cSDennis Zhou (Facebook) align); 426b4c2116cSDennis Zhou (Facebook) *bits = PCPU_BITMAP_BLOCK_BITS - *bit_off; 427b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, *bit_off); 428b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 429b4c2116cSDennis Zhou (Facebook) return; 430b4c2116cSDennis Zhou (Facebook) } 431b4c2116cSDennis Zhou (Facebook) 432b4c2116cSDennis Zhou (Facebook) /* no valid offsets were found - fail condition */ 433b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_chunk_map_bits(chunk); 434b4c2116cSDennis Zhou (Facebook) } 435b4c2116cSDennis Zhou (Facebook) 436525ca84dSDennis Zhou (Facebook) /* 437525ca84dSDennis Zhou (Facebook) * Metadata free area iterators. These perform aggregation of free areas 438525ca84dSDennis Zhou (Facebook) * based on the metadata blocks and return the offset @bit_off and size in 439b4c2116cSDennis Zhou (Facebook) * bits of the free area @bits. pcpu_for_each_fit_region only returns when 440b4c2116cSDennis Zhou (Facebook) * a fit is found for the allocation request. 441525ca84dSDennis Zhou (Facebook) */ 442525ca84dSDennis Zhou (Facebook) #define pcpu_for_each_md_free_region(chunk, bit_off, bits) \ 443525ca84dSDennis Zhou (Facebook) for (pcpu_next_md_free_region((chunk), &(bit_off), &(bits)); \ 444525ca84dSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 445525ca84dSDennis Zhou (Facebook) (bit_off) += (bits) + 1, \ 446525ca84dSDennis Zhou (Facebook) pcpu_next_md_free_region((chunk), &(bit_off), &(bits))) 447525ca84dSDennis Zhou (Facebook) 448b4c2116cSDennis Zhou (Facebook) #define pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) \ 449b4c2116cSDennis Zhou (Facebook) for (pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 450b4c2116cSDennis Zhou (Facebook) &(bits)); \ 451b4c2116cSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 452b4c2116cSDennis Zhou (Facebook) (bit_off) += (bits), \ 453b4c2116cSDennis Zhou (Facebook) pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 454b4c2116cSDennis Zhou (Facebook) &(bits))) 455b4c2116cSDennis Zhou (Facebook) 456525ca84dSDennis Zhou (Facebook) /** 45790459ce0SBob Liu * pcpu_mem_zalloc - allocate memory 4581880d93bSTejun Heo * @size: bytes to allocate 45947504ee0SDennis Zhou * @gfp: allocation flags 460fbf59bc9STejun Heo * 4611880d93bSTejun Heo * Allocate @size bytes. If @size is smaller than PAGE_SIZE, 46247504ee0SDennis Zhou * kzalloc() is used; otherwise, the equivalent of vzalloc() is used. 46347504ee0SDennis Zhou * This is to facilitate passing through whitelisted flags. The 46447504ee0SDennis Zhou * returned memory is always zeroed. 465fbf59bc9STejun Heo * 466fbf59bc9STejun Heo * RETURNS: 4671880d93bSTejun Heo * Pointer to the allocated area on success, NULL on failure. 468fbf59bc9STejun Heo */ 46947504ee0SDennis Zhou static void *pcpu_mem_zalloc(size_t size, gfp_t gfp) 470fbf59bc9STejun Heo { 471099a19d9STejun Heo if (WARN_ON_ONCE(!slab_is_available())) 472099a19d9STejun Heo return NULL; 473099a19d9STejun Heo 474fbf59bc9STejun Heo if (size <= PAGE_SIZE) 475554fef1cSDennis Zhou return kzalloc(size, gfp); 4767af4c093SJesper Juhl else 477554fef1cSDennis Zhou return __vmalloc(size, gfp | __GFP_ZERO, PAGE_KERNEL); 4781880d93bSTejun Heo } 479fbf59bc9STejun Heo 4801880d93bSTejun Heo /** 4811880d93bSTejun Heo * pcpu_mem_free - free memory 4821880d93bSTejun Heo * @ptr: memory to free 4831880d93bSTejun Heo * 48490459ce0SBob Liu * Free @ptr. @ptr should have been allocated using pcpu_mem_zalloc(). 4851880d93bSTejun Heo */ 4861d5cfdb0STetsuo Handa static void pcpu_mem_free(void *ptr) 4871880d93bSTejun Heo { 4881d5cfdb0STetsuo Handa kvfree(ptr); 489fbf59bc9STejun Heo } 490fbf59bc9STejun Heo 491fbf59bc9STejun Heo /** 492fbf59bc9STejun Heo * pcpu_chunk_relocate - put chunk in the appropriate chunk slot 493fbf59bc9STejun Heo * @chunk: chunk of interest 494fbf59bc9STejun Heo * @oslot: the previous slot it was on 495fbf59bc9STejun Heo * 496fbf59bc9STejun Heo * This function is called after an allocation or free changed @chunk. 497fbf59bc9STejun Heo * New slot according to the changed state is determined and @chunk is 498edcb4639STejun Heo * moved to the slot. Note that the reserved chunk is never put on 499edcb4639STejun Heo * chunk slots. 500ccea34b5STejun Heo * 501ccea34b5STejun Heo * CONTEXT: 502ccea34b5STejun Heo * pcpu_lock. 503fbf59bc9STejun Heo */ 504fbf59bc9STejun Heo static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) 505fbf59bc9STejun Heo { 506fbf59bc9STejun Heo int nslot = pcpu_chunk_slot(chunk); 507fbf59bc9STejun Heo 508edcb4639STejun Heo if (chunk != pcpu_reserved_chunk && oslot != nslot) { 509fbf59bc9STejun Heo if (oslot < nslot) 510fbf59bc9STejun Heo list_move(&chunk->list, &pcpu_slot[nslot]); 511fbf59bc9STejun Heo else 512fbf59bc9STejun Heo list_move_tail(&chunk->list, &pcpu_slot[nslot]); 513fbf59bc9STejun Heo } 514fbf59bc9STejun Heo } 515fbf59bc9STejun Heo 516fbf59bc9STejun Heo /** 51740064aecSDennis Zhou (Facebook) * pcpu_cnt_pop_pages- counts populated backing pages in range 518833af842STejun Heo * @chunk: chunk of interest 51940064aecSDennis Zhou (Facebook) * @bit_off: start offset 52040064aecSDennis Zhou (Facebook) * @bits: size of area to check 5219f7dcf22STejun Heo * 52240064aecSDennis Zhou (Facebook) * Calculates the number of populated pages in the region 52340064aecSDennis Zhou (Facebook) * [page_start, page_end). This keeps track of how many empty populated 52440064aecSDennis Zhou (Facebook) * pages are available and decide if async work should be scheduled. 525ccea34b5STejun Heo * 5269f7dcf22STejun Heo * RETURNS: 52740064aecSDennis Zhou (Facebook) * The nr of populated pages. 5289f7dcf22STejun Heo */ 52940064aecSDennis Zhou (Facebook) static inline int pcpu_cnt_pop_pages(struct pcpu_chunk *chunk, int bit_off, 53040064aecSDennis Zhou (Facebook) int bits) 5319f7dcf22STejun Heo { 53240064aecSDennis Zhou (Facebook) int page_start = PFN_UP(bit_off * PCPU_MIN_ALLOC_SIZE); 53340064aecSDennis Zhou (Facebook) int page_end = PFN_DOWN((bit_off + bits) * PCPU_MIN_ALLOC_SIZE); 53440064aecSDennis Zhou (Facebook) 53540064aecSDennis Zhou (Facebook) if (page_start >= page_end) 53640064aecSDennis Zhou (Facebook) return 0; 53740064aecSDennis Zhou (Facebook) 53840064aecSDennis Zhou (Facebook) /* 53940064aecSDennis Zhou (Facebook) * bitmap_weight counts the number of bits set in a bitmap up to 54040064aecSDennis Zhou (Facebook) * the specified number of bits. This is counting the populated 54140064aecSDennis Zhou (Facebook) * pages up to page_end and then subtracting the populated pages 54240064aecSDennis Zhou (Facebook) * up to page_start to count the populated pages in 54340064aecSDennis Zhou (Facebook) * [page_start, page_end). 54440064aecSDennis Zhou (Facebook) */ 54540064aecSDennis Zhou (Facebook) return bitmap_weight(chunk->populated, page_end) - 54640064aecSDennis Zhou (Facebook) bitmap_weight(chunk->populated, page_start); 54740064aecSDennis Zhou (Facebook) } 54840064aecSDennis Zhou (Facebook) 54940064aecSDennis Zhou (Facebook) /** 55040064aecSDennis Zhou (Facebook) * pcpu_chunk_update - updates the chunk metadata given a free area 55140064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 55240064aecSDennis Zhou (Facebook) * @bit_off: chunk offset 55340064aecSDennis Zhou (Facebook) * @bits: size of free area 55440064aecSDennis Zhou (Facebook) * 55513f96637SDennis Zhou (Facebook) * This updates the chunk's contig hint and starting offset given a free area. 556268625a6SDennis Zhou (Facebook) * Choose the best starting offset if the contig hint is equal. 55740064aecSDennis Zhou (Facebook) */ 55840064aecSDennis Zhou (Facebook) static void pcpu_chunk_update(struct pcpu_chunk *chunk, int bit_off, int bits) 55940064aecSDennis Zhou (Facebook) { 56013f96637SDennis Zhou (Facebook) if (bits > chunk->contig_bits) { 56113f96637SDennis Zhou (Facebook) chunk->contig_bits_start = bit_off; 56240064aecSDennis Zhou (Facebook) chunk->contig_bits = bits; 563268625a6SDennis Zhou (Facebook) } else if (bits == chunk->contig_bits && chunk->contig_bits_start && 564268625a6SDennis Zhou (Facebook) (!bit_off || 565268625a6SDennis Zhou (Facebook) __ffs(bit_off) > __ffs(chunk->contig_bits_start))) { 566268625a6SDennis Zhou (Facebook) /* use the start with the best alignment */ 567268625a6SDennis Zhou (Facebook) chunk->contig_bits_start = bit_off; 56840064aecSDennis Zhou (Facebook) } 56913f96637SDennis Zhou (Facebook) } 57040064aecSDennis Zhou (Facebook) 57140064aecSDennis Zhou (Facebook) /** 57240064aecSDennis Zhou (Facebook) * pcpu_chunk_refresh_hint - updates metadata about a chunk 57340064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 57440064aecSDennis Zhou (Facebook) * 575525ca84dSDennis Zhou (Facebook) * Iterates over the metadata blocks to find the largest contig area. 576525ca84dSDennis Zhou (Facebook) * It also counts the populated pages and uses the delta to update the 577525ca84dSDennis Zhou (Facebook) * global count. 57840064aecSDennis Zhou (Facebook) * 57940064aecSDennis Zhou (Facebook) * Updates: 58040064aecSDennis Zhou (Facebook) * chunk->contig_bits 58113f96637SDennis Zhou (Facebook) * chunk->contig_bits_start 582525ca84dSDennis Zhou (Facebook) * nr_empty_pop_pages (chunk and global) 58340064aecSDennis Zhou (Facebook) */ 58440064aecSDennis Zhou (Facebook) static void pcpu_chunk_refresh_hint(struct pcpu_chunk *chunk) 58540064aecSDennis Zhou (Facebook) { 586525ca84dSDennis Zhou (Facebook) int bit_off, bits, nr_empty_pop_pages; 58740064aecSDennis Zhou (Facebook) 58840064aecSDennis Zhou (Facebook) /* clear metadata */ 58940064aecSDennis Zhou (Facebook) chunk->contig_bits = 0; 59040064aecSDennis Zhou (Facebook) 591525ca84dSDennis Zhou (Facebook) bit_off = chunk->first_bit; 59240064aecSDennis Zhou (Facebook) bits = nr_empty_pop_pages = 0; 593525ca84dSDennis Zhou (Facebook) pcpu_for_each_md_free_region(chunk, bit_off, bits) { 594525ca84dSDennis Zhou (Facebook) pcpu_chunk_update(chunk, bit_off, bits); 59540064aecSDennis Zhou (Facebook) 596525ca84dSDennis Zhou (Facebook) nr_empty_pop_pages += pcpu_cnt_pop_pages(chunk, bit_off, bits); 59740064aecSDennis Zhou (Facebook) } 59840064aecSDennis Zhou (Facebook) 59940064aecSDennis Zhou (Facebook) /* 60040064aecSDennis Zhou (Facebook) * Keep track of nr_empty_pop_pages. 60140064aecSDennis Zhou (Facebook) * 60240064aecSDennis Zhou (Facebook) * The chunk maintains the previous number of free pages it held, 60340064aecSDennis Zhou (Facebook) * so the delta is used to update the global counter. The reserved 60440064aecSDennis Zhou (Facebook) * chunk is not part of the free page count as they are populated 60540064aecSDennis Zhou (Facebook) * at init and are special to serving reserved allocations. 60640064aecSDennis Zhou (Facebook) */ 60740064aecSDennis Zhou (Facebook) if (chunk != pcpu_reserved_chunk) 60840064aecSDennis Zhou (Facebook) pcpu_nr_empty_pop_pages += 60940064aecSDennis Zhou (Facebook) (nr_empty_pop_pages - chunk->nr_empty_pop_pages); 61040064aecSDennis Zhou (Facebook) 61140064aecSDennis Zhou (Facebook) chunk->nr_empty_pop_pages = nr_empty_pop_pages; 61240064aecSDennis Zhou (Facebook) } 61340064aecSDennis Zhou (Facebook) 61440064aecSDennis Zhou (Facebook) /** 615ca460b3cSDennis Zhou (Facebook) * pcpu_block_update - updates a block given a free area 616ca460b3cSDennis Zhou (Facebook) * @block: block of interest 617ca460b3cSDennis Zhou (Facebook) * @start: start offset in block 618ca460b3cSDennis Zhou (Facebook) * @end: end offset in block 619ca460b3cSDennis Zhou (Facebook) * 620ca460b3cSDennis Zhou (Facebook) * Updates a block given a known free area. The region [start, end) is 621268625a6SDennis Zhou (Facebook) * expected to be the entirety of the free area within a block. Chooses 622268625a6SDennis Zhou (Facebook) * the best starting offset if the contig hints are equal. 623ca460b3cSDennis Zhou (Facebook) */ 624ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update(struct pcpu_block_md *block, int start, int end) 625ca460b3cSDennis Zhou (Facebook) { 626ca460b3cSDennis Zhou (Facebook) int contig = end - start; 627ca460b3cSDennis Zhou (Facebook) 628ca460b3cSDennis Zhou (Facebook) block->first_free = min(block->first_free, start); 629ca460b3cSDennis Zhou (Facebook) if (start == 0) 630ca460b3cSDennis Zhou (Facebook) block->left_free = contig; 631ca460b3cSDennis Zhou (Facebook) 632ca460b3cSDennis Zhou (Facebook) if (end == PCPU_BITMAP_BLOCK_BITS) 633ca460b3cSDennis Zhou (Facebook) block->right_free = contig; 634ca460b3cSDennis Zhou (Facebook) 635ca460b3cSDennis Zhou (Facebook) if (contig > block->contig_hint) { 636ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = start; 637ca460b3cSDennis Zhou (Facebook) block->contig_hint = contig; 638268625a6SDennis Zhou (Facebook) } else if (block->contig_hint_start && contig == block->contig_hint && 639268625a6SDennis Zhou (Facebook) (!start || __ffs(start) > __ffs(block->contig_hint_start))) { 640268625a6SDennis Zhou (Facebook) /* use the start with the best alignment */ 641268625a6SDennis Zhou (Facebook) block->contig_hint_start = start; 642ca460b3cSDennis Zhou (Facebook) } 643ca460b3cSDennis Zhou (Facebook) } 644ca460b3cSDennis Zhou (Facebook) 645ca460b3cSDennis Zhou (Facebook) /** 646ca460b3cSDennis Zhou (Facebook) * pcpu_block_refresh_hint 647ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 648ca460b3cSDennis Zhou (Facebook) * @index: index of the metadata block 649ca460b3cSDennis Zhou (Facebook) * 650ca460b3cSDennis Zhou (Facebook) * Scans over the block beginning at first_free and updates the block 651ca460b3cSDennis Zhou (Facebook) * metadata accordingly. 652ca460b3cSDennis Zhou (Facebook) */ 653ca460b3cSDennis Zhou (Facebook) static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index) 654ca460b3cSDennis Zhou (Facebook) { 655ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *block = chunk->md_blocks + index; 656ca460b3cSDennis Zhou (Facebook) unsigned long *alloc_map = pcpu_index_alloc_map(chunk, index); 657ca460b3cSDennis Zhou (Facebook) int rs, re; /* region start, region end */ 658ca460b3cSDennis Zhou (Facebook) 659ca460b3cSDennis Zhou (Facebook) /* clear hints */ 660ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 661ca460b3cSDennis Zhou (Facebook) block->left_free = block->right_free = 0; 662ca460b3cSDennis Zhou (Facebook) 663ca460b3cSDennis Zhou (Facebook) /* iterate over free areas and update the contig hints */ 664ca460b3cSDennis Zhou (Facebook) pcpu_for_each_unpop_region(alloc_map, rs, re, block->first_free, 665ca460b3cSDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS) { 666ca460b3cSDennis Zhou (Facebook) pcpu_block_update(block, rs, re); 667ca460b3cSDennis Zhou (Facebook) } 668ca460b3cSDennis Zhou (Facebook) } 669ca460b3cSDennis Zhou (Facebook) 670ca460b3cSDennis Zhou (Facebook) /** 671ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_alloc - update hint on allocation path 672ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 673ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 674ca460b3cSDennis Zhou (Facebook) * @bits: size of request 675fc304334SDennis Zhou (Facebook) * 676fc304334SDennis Zhou (Facebook) * Updates metadata for the allocation path. The metadata only has to be 677fc304334SDennis Zhou (Facebook) * refreshed by a full scan iff the chunk's contig hint is broken. Block level 678fc304334SDennis Zhou (Facebook) * scans are required if the block's contig hint is broken. 679ca460b3cSDennis Zhou (Facebook) */ 680ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_alloc(struct pcpu_chunk *chunk, int bit_off, 681ca460b3cSDennis Zhou (Facebook) int bits) 682ca460b3cSDennis Zhou (Facebook) { 683ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 684ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 685ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 686ca460b3cSDennis Zhou (Facebook) 687ca460b3cSDennis Zhou (Facebook) /* 688ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 689ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 690ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 691ca460b3cSDennis Zhou (Facebook) * range. 692ca460b3cSDennis Zhou (Facebook) */ 693ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 694ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 695ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 696ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 697ca460b3cSDennis Zhou (Facebook) 698ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 699ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 700ca460b3cSDennis Zhou (Facebook) 701ca460b3cSDennis Zhou (Facebook) /* 702ca460b3cSDennis Zhou (Facebook) * Update s_block. 703fc304334SDennis Zhou (Facebook) * block->first_free must be updated if the allocation takes its place. 704fc304334SDennis Zhou (Facebook) * If the allocation breaks the contig_hint, a scan is required to 705fc304334SDennis Zhou (Facebook) * restore this hint. 706ca460b3cSDennis Zhou (Facebook) */ 707fc304334SDennis Zhou (Facebook) if (s_off == s_block->first_free) 708fc304334SDennis Zhou (Facebook) s_block->first_free = find_next_zero_bit( 709fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, s_index), 710fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, 711fc304334SDennis Zhou (Facebook) s_off + bits); 712fc304334SDennis Zhou (Facebook) 713fc304334SDennis Zhou (Facebook) if (s_off >= s_block->contig_hint_start && 714fc304334SDennis Zhou (Facebook) s_off < s_block->contig_hint_start + s_block->contig_hint) { 715fc304334SDennis Zhou (Facebook) /* block contig hint is broken - scan to fix it */ 716ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, s_index); 717fc304334SDennis Zhou (Facebook) } else { 718fc304334SDennis Zhou (Facebook) /* update left and right contig manually */ 719fc304334SDennis Zhou (Facebook) s_block->left_free = min(s_block->left_free, s_off); 720fc304334SDennis Zhou (Facebook) if (s_index == e_index) 721fc304334SDennis Zhou (Facebook) s_block->right_free = min_t(int, s_block->right_free, 722fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 723fc304334SDennis Zhou (Facebook) else 724fc304334SDennis Zhou (Facebook) s_block->right_free = 0; 725fc304334SDennis Zhou (Facebook) } 726ca460b3cSDennis Zhou (Facebook) 727ca460b3cSDennis Zhou (Facebook) /* 728ca460b3cSDennis Zhou (Facebook) * Update e_block. 729ca460b3cSDennis Zhou (Facebook) */ 730ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 731fc304334SDennis Zhou (Facebook) /* 732fc304334SDennis Zhou (Facebook) * When the allocation is across blocks, the end is along 733fc304334SDennis Zhou (Facebook) * the left part of the e_block. 734fc304334SDennis Zhou (Facebook) */ 735fc304334SDennis Zhou (Facebook) e_block->first_free = find_next_zero_bit( 736fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, e_index), 737fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, e_off); 738fc304334SDennis Zhou (Facebook) 739fc304334SDennis Zhou (Facebook) if (e_off == PCPU_BITMAP_BLOCK_BITS) { 740fc304334SDennis Zhou (Facebook) /* reset the block */ 741fc304334SDennis Zhou (Facebook) e_block++; 742fc304334SDennis Zhou (Facebook) } else { 743fc304334SDennis Zhou (Facebook) if (e_off > e_block->contig_hint_start) { 744fc304334SDennis Zhou (Facebook) /* contig hint is broken - scan to fix it */ 745ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, e_index); 746fc304334SDennis Zhou (Facebook) } else { 747fc304334SDennis Zhou (Facebook) e_block->left_free = 0; 748fc304334SDennis Zhou (Facebook) e_block->right_free = 749fc304334SDennis Zhou (Facebook) min_t(int, e_block->right_free, 750fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 751fc304334SDennis Zhou (Facebook) } 752fc304334SDennis Zhou (Facebook) } 753ca460b3cSDennis Zhou (Facebook) 754ca460b3cSDennis Zhou (Facebook) /* update in-between md_blocks */ 755ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 756ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 757ca460b3cSDennis Zhou (Facebook) block->left_free = 0; 758ca460b3cSDennis Zhou (Facebook) block->right_free = 0; 759ca460b3cSDennis Zhou (Facebook) } 760ca460b3cSDennis Zhou (Facebook) } 761ca460b3cSDennis Zhou (Facebook) 762fc304334SDennis Zhou (Facebook) /* 763fc304334SDennis Zhou (Facebook) * The only time a full chunk scan is required is if the chunk 764fc304334SDennis Zhou (Facebook) * contig hint is broken. Otherwise, it means a smaller space 765fc304334SDennis Zhou (Facebook) * was used and therefore the chunk contig hint is still correct. 766fc304334SDennis Zhou (Facebook) */ 767fc304334SDennis Zhou (Facebook) if (bit_off >= chunk->contig_bits_start && 768fc304334SDennis Zhou (Facebook) bit_off < chunk->contig_bits_start + chunk->contig_bits) 769ca460b3cSDennis Zhou (Facebook) pcpu_chunk_refresh_hint(chunk); 770ca460b3cSDennis Zhou (Facebook) } 771ca460b3cSDennis Zhou (Facebook) 772ca460b3cSDennis Zhou (Facebook) /** 773ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_free - updates the block hints on the free path 774ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 775ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 776ca460b3cSDennis Zhou (Facebook) * @bits: size of request 777b185cd0dSDennis Zhou (Facebook) * 778b185cd0dSDennis Zhou (Facebook) * Updates metadata for the allocation path. This avoids a blind block 779b185cd0dSDennis Zhou (Facebook) * refresh by making use of the block contig hints. If this fails, it scans 780b185cd0dSDennis Zhou (Facebook) * forward and backward to determine the extent of the free area. This is 781b185cd0dSDennis Zhou (Facebook) * capped at the boundary of blocks. 782b185cd0dSDennis Zhou (Facebook) * 783b185cd0dSDennis Zhou (Facebook) * A chunk update is triggered if a page becomes free, a block becomes free, 784b185cd0dSDennis Zhou (Facebook) * or the free spans across blocks. This tradeoff is to minimize iterating 785b185cd0dSDennis Zhou (Facebook) * over the block metadata to update chunk->contig_bits. chunk->contig_bits 786b185cd0dSDennis Zhou (Facebook) * may be off by up to a page, but it will never be more than the available 787b185cd0dSDennis Zhou (Facebook) * space. If the contig hint is contained in one block, it will be accurate. 788ca460b3cSDennis Zhou (Facebook) */ 789ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_free(struct pcpu_chunk *chunk, int bit_off, 790ca460b3cSDennis Zhou (Facebook) int bits) 791ca460b3cSDennis Zhou (Facebook) { 792ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 793ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 794ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 795b185cd0dSDennis Zhou (Facebook) int start, end; /* start and end of the whole free area */ 796ca460b3cSDennis Zhou (Facebook) 797ca460b3cSDennis Zhou (Facebook) /* 798ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 799ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 800ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 801ca460b3cSDennis Zhou (Facebook) * range. 802ca460b3cSDennis Zhou (Facebook) */ 803ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 804ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 805ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 806ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 807ca460b3cSDennis Zhou (Facebook) 808ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 809ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 810ca460b3cSDennis Zhou (Facebook) 811b185cd0dSDennis Zhou (Facebook) /* 812b185cd0dSDennis Zhou (Facebook) * Check if the freed area aligns with the block->contig_hint. 813b185cd0dSDennis Zhou (Facebook) * If it does, then the scan to find the beginning/end of the 814b185cd0dSDennis Zhou (Facebook) * larger free area can be avoided. 815b185cd0dSDennis Zhou (Facebook) * 816b185cd0dSDennis Zhou (Facebook) * start and end refer to beginning and end of the free area 817b185cd0dSDennis Zhou (Facebook) * within each their respective blocks. This is not necessarily 818b185cd0dSDennis Zhou (Facebook) * the entire free area as it may span blocks past the beginning 819b185cd0dSDennis Zhou (Facebook) * or end of the block. 820b185cd0dSDennis Zhou (Facebook) */ 821b185cd0dSDennis Zhou (Facebook) start = s_off; 822b185cd0dSDennis Zhou (Facebook) if (s_off == s_block->contig_hint + s_block->contig_hint_start) { 823b185cd0dSDennis Zhou (Facebook) start = s_block->contig_hint_start; 824b185cd0dSDennis Zhou (Facebook) } else { 825b185cd0dSDennis Zhou (Facebook) /* 826b185cd0dSDennis Zhou (Facebook) * Scan backwards to find the extent of the free area. 827b185cd0dSDennis Zhou (Facebook) * find_last_bit returns the starting bit, so if the start bit 828b185cd0dSDennis Zhou (Facebook) * is returned, that means there was no last bit and the 829b185cd0dSDennis Zhou (Facebook) * remainder of the chunk is free. 830b185cd0dSDennis Zhou (Facebook) */ 831b185cd0dSDennis Zhou (Facebook) int l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index), 832b185cd0dSDennis Zhou (Facebook) start); 833b185cd0dSDennis Zhou (Facebook) start = (start == l_bit) ? 0 : l_bit + 1; 834b185cd0dSDennis Zhou (Facebook) } 835b185cd0dSDennis Zhou (Facebook) 836b185cd0dSDennis Zhou (Facebook) end = e_off; 837b185cd0dSDennis Zhou (Facebook) if (e_off == e_block->contig_hint_start) 838b185cd0dSDennis Zhou (Facebook) end = e_block->contig_hint_start + e_block->contig_hint; 839b185cd0dSDennis Zhou (Facebook) else 840b185cd0dSDennis Zhou (Facebook) end = find_next_bit(pcpu_index_alloc_map(chunk, e_index), 841b185cd0dSDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, end); 842b185cd0dSDennis Zhou (Facebook) 843ca460b3cSDennis Zhou (Facebook) /* update s_block */ 844b185cd0dSDennis Zhou (Facebook) e_off = (s_index == e_index) ? end : PCPU_BITMAP_BLOCK_BITS; 845b185cd0dSDennis Zhou (Facebook) pcpu_block_update(s_block, start, e_off); 846ca460b3cSDennis Zhou (Facebook) 847ca460b3cSDennis Zhou (Facebook) /* freeing in the same block */ 848ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 849ca460b3cSDennis Zhou (Facebook) /* update e_block */ 850b185cd0dSDennis Zhou (Facebook) pcpu_block_update(e_block, 0, end); 851ca460b3cSDennis Zhou (Facebook) 852ca460b3cSDennis Zhou (Facebook) /* reset md_blocks in the middle */ 853ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 854ca460b3cSDennis Zhou (Facebook) block->first_free = 0; 855ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = 0; 856ca460b3cSDennis Zhou (Facebook) block->contig_hint = PCPU_BITMAP_BLOCK_BITS; 857ca460b3cSDennis Zhou (Facebook) block->left_free = PCPU_BITMAP_BLOCK_BITS; 858ca460b3cSDennis Zhou (Facebook) block->right_free = PCPU_BITMAP_BLOCK_BITS; 859ca460b3cSDennis Zhou (Facebook) } 860ca460b3cSDennis Zhou (Facebook) } 861ca460b3cSDennis Zhou (Facebook) 862b185cd0dSDennis Zhou (Facebook) /* 863b185cd0dSDennis Zhou (Facebook) * Refresh chunk metadata when the free makes a page free, a block 864b185cd0dSDennis Zhou (Facebook) * free, or spans across blocks. The contig hint may be off by up to 865b185cd0dSDennis Zhou (Facebook) * a page, but if the hint is contained in a block, it will be accurate 866b185cd0dSDennis Zhou (Facebook) * with the else condition below. 867b185cd0dSDennis Zhou (Facebook) */ 868b185cd0dSDennis Zhou (Facebook) if ((ALIGN_DOWN(end, min(PCPU_BITS_PER_PAGE, PCPU_BITMAP_BLOCK_BITS)) > 869b185cd0dSDennis Zhou (Facebook) ALIGN(start, min(PCPU_BITS_PER_PAGE, PCPU_BITMAP_BLOCK_BITS))) || 870b185cd0dSDennis Zhou (Facebook) s_index != e_index) 871ca460b3cSDennis Zhou (Facebook) pcpu_chunk_refresh_hint(chunk); 872b185cd0dSDennis Zhou (Facebook) else 873b185cd0dSDennis Zhou (Facebook) pcpu_chunk_update(chunk, pcpu_block_off_to_off(s_index, start), 874b185cd0dSDennis Zhou (Facebook) s_block->contig_hint); 875ca460b3cSDennis Zhou (Facebook) } 876ca460b3cSDennis Zhou (Facebook) 877ca460b3cSDennis Zhou (Facebook) /** 87840064aecSDennis Zhou (Facebook) * pcpu_is_populated - determines if the region is populated 87940064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 88040064aecSDennis Zhou (Facebook) * @bit_off: chunk offset 88140064aecSDennis Zhou (Facebook) * @bits: size of area 88240064aecSDennis Zhou (Facebook) * @next_off: return value for the next offset to start searching 88340064aecSDennis Zhou (Facebook) * 88440064aecSDennis Zhou (Facebook) * For atomic allocations, check if the backing pages are populated. 88540064aecSDennis Zhou (Facebook) * 88640064aecSDennis Zhou (Facebook) * RETURNS: 88740064aecSDennis Zhou (Facebook) * Bool if the backing pages are populated. 88840064aecSDennis Zhou (Facebook) * next_index is to skip over unpopulated blocks in pcpu_find_block_fit. 88940064aecSDennis Zhou (Facebook) */ 89040064aecSDennis Zhou (Facebook) static bool pcpu_is_populated(struct pcpu_chunk *chunk, int bit_off, int bits, 89140064aecSDennis Zhou (Facebook) int *next_off) 89240064aecSDennis Zhou (Facebook) { 89340064aecSDennis Zhou (Facebook) int page_start, page_end, rs, re; 89440064aecSDennis Zhou (Facebook) 89540064aecSDennis Zhou (Facebook) page_start = PFN_DOWN(bit_off * PCPU_MIN_ALLOC_SIZE); 89640064aecSDennis Zhou (Facebook) page_end = PFN_UP((bit_off + bits) * PCPU_MIN_ALLOC_SIZE); 89740064aecSDennis Zhou (Facebook) 89840064aecSDennis Zhou (Facebook) rs = page_start; 89940064aecSDennis Zhou (Facebook) pcpu_next_unpop(chunk->populated, &rs, &re, page_end); 90040064aecSDennis Zhou (Facebook) if (rs >= page_end) 90140064aecSDennis Zhou (Facebook) return true; 90240064aecSDennis Zhou (Facebook) 90340064aecSDennis Zhou (Facebook) *next_off = re * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE; 90440064aecSDennis Zhou (Facebook) return false; 90540064aecSDennis Zhou (Facebook) } 90640064aecSDennis Zhou (Facebook) 90740064aecSDennis Zhou (Facebook) /** 90840064aecSDennis Zhou (Facebook) * pcpu_find_block_fit - finds the block index to start searching 90940064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 91040064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 91140064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE bytes) 91240064aecSDennis Zhou (Facebook) * @pop_only: use populated regions only 91340064aecSDennis Zhou (Facebook) * 914b4c2116cSDennis Zhou (Facebook) * Given a chunk and an allocation spec, find the offset to begin searching 915b4c2116cSDennis Zhou (Facebook) * for a free region. This iterates over the bitmap metadata blocks to 916b4c2116cSDennis Zhou (Facebook) * find an offset that will be guaranteed to fit the requirements. It is 917b4c2116cSDennis Zhou (Facebook) * not quite first fit as if the allocation does not fit in the contig hint 918b4c2116cSDennis Zhou (Facebook) * of a block or chunk, it is skipped. This errs on the side of caution 919b4c2116cSDennis Zhou (Facebook) * to prevent excess iteration. Poor alignment can cause the allocator to 920b4c2116cSDennis Zhou (Facebook) * skip over blocks and chunks that have valid free areas. 921b4c2116cSDennis Zhou (Facebook) * 92240064aecSDennis Zhou (Facebook) * RETURNS: 92340064aecSDennis Zhou (Facebook) * The offset in the bitmap to begin searching. 92440064aecSDennis Zhou (Facebook) * -1 if no offset is found. 92540064aecSDennis Zhou (Facebook) */ 92640064aecSDennis Zhou (Facebook) static int pcpu_find_block_fit(struct pcpu_chunk *chunk, int alloc_bits, 92740064aecSDennis Zhou (Facebook) size_t align, bool pop_only) 92840064aecSDennis Zhou (Facebook) { 929b4c2116cSDennis Zhou (Facebook) int bit_off, bits, next_off; 93040064aecSDennis Zhou (Facebook) 93113f96637SDennis Zhou (Facebook) /* 93213f96637SDennis Zhou (Facebook) * Check to see if the allocation can fit in the chunk's contig hint. 93313f96637SDennis Zhou (Facebook) * This is an optimization to prevent scanning by assuming if it 93413f96637SDennis Zhou (Facebook) * cannot fit in the global hint, there is memory pressure and creating 93513f96637SDennis Zhou (Facebook) * a new chunk would happen soon. 93613f96637SDennis Zhou (Facebook) */ 93713f96637SDennis Zhou (Facebook) bit_off = ALIGN(chunk->contig_bits_start, align) - 93813f96637SDennis Zhou (Facebook) chunk->contig_bits_start; 93913f96637SDennis Zhou (Facebook) if (bit_off + alloc_bits > chunk->contig_bits) 94013f96637SDennis Zhou (Facebook) return -1; 94113f96637SDennis Zhou (Facebook) 942b4c2116cSDennis Zhou (Facebook) bit_off = chunk->first_bit; 943b4c2116cSDennis Zhou (Facebook) bits = 0; 944b4c2116cSDennis Zhou (Facebook) pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) { 94540064aecSDennis Zhou (Facebook) if (!pop_only || pcpu_is_populated(chunk, bit_off, bits, 946b4c2116cSDennis Zhou (Facebook) &next_off)) 94740064aecSDennis Zhou (Facebook) break; 94840064aecSDennis Zhou (Facebook) 949b4c2116cSDennis Zhou (Facebook) bit_off = next_off; 95040064aecSDennis Zhou (Facebook) bits = 0; 95140064aecSDennis Zhou (Facebook) } 95240064aecSDennis Zhou (Facebook) 95340064aecSDennis Zhou (Facebook) if (bit_off == pcpu_chunk_map_bits(chunk)) 95440064aecSDennis Zhou (Facebook) return -1; 95540064aecSDennis Zhou (Facebook) 95640064aecSDennis Zhou (Facebook) return bit_off; 95740064aecSDennis Zhou (Facebook) } 95840064aecSDennis Zhou (Facebook) 95940064aecSDennis Zhou (Facebook) /** 96040064aecSDennis Zhou (Facebook) * pcpu_alloc_area - allocates an area from a pcpu_chunk 96140064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 96240064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 96340064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 96440064aecSDennis Zhou (Facebook) * @start: bit_off to start searching 96540064aecSDennis Zhou (Facebook) * 96640064aecSDennis Zhou (Facebook) * This function takes in a @start offset to begin searching to fit an 967b4c2116cSDennis Zhou (Facebook) * allocation of @alloc_bits with alignment @align. It needs to scan 968b4c2116cSDennis Zhou (Facebook) * the allocation map because if it fits within the block's contig hint, 969b4c2116cSDennis Zhou (Facebook) * @start will be block->first_free. This is an attempt to fill the 970b4c2116cSDennis Zhou (Facebook) * allocation prior to breaking the contig hint. The allocation and 971b4c2116cSDennis Zhou (Facebook) * boundary maps are updated accordingly if it confirms a valid 972b4c2116cSDennis Zhou (Facebook) * free area. 97340064aecSDennis Zhou (Facebook) * 97440064aecSDennis Zhou (Facebook) * RETURNS: 97540064aecSDennis Zhou (Facebook) * Allocated addr offset in @chunk on success. 97640064aecSDennis Zhou (Facebook) * -1 if no matching area is found. 97740064aecSDennis Zhou (Facebook) */ 97840064aecSDennis Zhou (Facebook) static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits, 97940064aecSDennis Zhou (Facebook) size_t align, int start) 98040064aecSDennis Zhou (Facebook) { 98140064aecSDennis Zhou (Facebook) size_t align_mask = (align) ? (align - 1) : 0; 98240064aecSDennis Zhou (Facebook) int bit_off, end, oslot; 9839f7dcf22STejun Heo 9844f996e23STejun Heo lockdep_assert_held(&pcpu_lock); 9854f996e23STejun Heo 98640064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 987833af842STejun Heo 988833af842STejun Heo /* 98940064aecSDennis Zhou (Facebook) * Search to find a fit. 990833af842STejun Heo */ 991b4c2116cSDennis Zhou (Facebook) end = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS; 99240064aecSDennis Zhou (Facebook) bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start, 99340064aecSDennis Zhou (Facebook) alloc_bits, align_mask); 99440064aecSDennis Zhou (Facebook) if (bit_off >= end) 995a16037c8STejun Heo return -1; 996a16037c8STejun Heo 99740064aecSDennis Zhou (Facebook) /* update alloc map */ 99840064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, bit_off, alloc_bits); 999a16037c8STejun Heo 100040064aecSDennis Zhou (Facebook) /* update boundary map */ 100140064aecSDennis Zhou (Facebook) set_bit(bit_off, chunk->bound_map); 100240064aecSDennis Zhou (Facebook) bitmap_clear(chunk->bound_map, bit_off + 1, alloc_bits - 1); 100340064aecSDennis Zhou (Facebook) set_bit(bit_off + alloc_bits, chunk->bound_map); 1004a16037c8STejun Heo 100540064aecSDennis Zhou (Facebook) chunk->free_bytes -= alloc_bits * PCPU_MIN_ALLOC_SIZE; 100640064aecSDennis Zhou (Facebook) 100786b442fbSDennis Zhou (Facebook) /* update first free bit */ 100886b442fbSDennis Zhou (Facebook) if (bit_off == chunk->first_bit) 100986b442fbSDennis Zhou (Facebook) chunk->first_bit = find_next_zero_bit( 101086b442fbSDennis Zhou (Facebook) chunk->alloc_map, 101186b442fbSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk), 101286b442fbSDennis Zhou (Facebook) bit_off + alloc_bits); 101386b442fbSDennis Zhou (Facebook) 1014ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, bit_off, alloc_bits); 101540064aecSDennis Zhou (Facebook) 101640064aecSDennis Zhou (Facebook) pcpu_chunk_relocate(chunk, oslot); 101740064aecSDennis Zhou (Facebook) 101840064aecSDennis Zhou (Facebook) return bit_off * PCPU_MIN_ALLOC_SIZE; 1019a16037c8STejun Heo } 1020a16037c8STejun Heo 1021a16037c8STejun Heo /** 102240064aecSDennis Zhou (Facebook) * pcpu_free_area - frees the corresponding offset 1023fbf59bc9STejun Heo * @chunk: chunk of interest 102440064aecSDennis Zhou (Facebook) * @off: addr offset into chunk 1025fbf59bc9STejun Heo * 102640064aecSDennis Zhou (Facebook) * This function determines the size of an allocation to free using 102740064aecSDennis Zhou (Facebook) * the boundary bitmap and clears the allocation map. 1028fbf59bc9STejun Heo */ 102940064aecSDennis Zhou (Facebook) static void pcpu_free_area(struct pcpu_chunk *chunk, int off) 1030fbf59bc9STejun Heo { 103140064aecSDennis Zhou (Facebook) int bit_off, bits, end, oslot; 1032fbf59bc9STejun Heo 10335ccd30e4SDennis Zhou lockdep_assert_held(&pcpu_lock); 103430a5b536SDennis Zhou pcpu_stats_area_dealloc(chunk); 10355ccd30e4SDennis Zhou 103640064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 1037723ad1d9SAl Viro 103840064aecSDennis Zhou (Facebook) bit_off = off / PCPU_MIN_ALLOC_SIZE; 1039fbf59bc9STejun Heo 104040064aecSDennis Zhou (Facebook) /* find end index */ 104140064aecSDennis Zhou (Facebook) end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), 104240064aecSDennis Zhou (Facebook) bit_off + 1); 104340064aecSDennis Zhou (Facebook) bits = end - bit_off; 104440064aecSDennis Zhou (Facebook) bitmap_clear(chunk->alloc_map, bit_off, bits); 10453d331ad7SAl Viro 104640064aecSDennis Zhou (Facebook) /* update metadata */ 104740064aecSDennis Zhou (Facebook) chunk->free_bytes += bits * PCPU_MIN_ALLOC_SIZE; 1048fbf59bc9STejun Heo 104986b442fbSDennis Zhou (Facebook) /* update first free bit */ 105086b442fbSDennis Zhou (Facebook) chunk->first_bit = min(chunk->first_bit, bit_off); 105186b442fbSDennis Zhou (Facebook) 1052ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_free(chunk, bit_off, bits); 1053b539b87fSTejun Heo 1054fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 1055fbf59bc9STejun Heo } 1056fbf59bc9STejun Heo 1057ca460b3cSDennis Zhou (Facebook) static void pcpu_init_md_blocks(struct pcpu_chunk *chunk) 1058ca460b3cSDennis Zhou (Facebook) { 1059ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *md_block; 1060ca460b3cSDennis Zhou (Facebook) 1061ca460b3cSDennis Zhou (Facebook) for (md_block = chunk->md_blocks; 1062ca460b3cSDennis Zhou (Facebook) md_block != chunk->md_blocks + pcpu_chunk_nr_blocks(chunk); 1063ca460b3cSDennis Zhou (Facebook) md_block++) { 1064ca460b3cSDennis Zhou (Facebook) md_block->contig_hint = PCPU_BITMAP_BLOCK_BITS; 1065ca460b3cSDennis Zhou (Facebook) md_block->left_free = PCPU_BITMAP_BLOCK_BITS; 1066ca460b3cSDennis Zhou (Facebook) md_block->right_free = PCPU_BITMAP_BLOCK_BITS; 1067ca460b3cSDennis Zhou (Facebook) } 1068ca460b3cSDennis Zhou (Facebook) } 1069ca460b3cSDennis Zhou (Facebook) 107040064aecSDennis Zhou (Facebook) /** 107140064aecSDennis Zhou (Facebook) * pcpu_alloc_first_chunk - creates chunks that serve the first chunk 107240064aecSDennis Zhou (Facebook) * @tmp_addr: the start of the region served 107340064aecSDennis Zhou (Facebook) * @map_size: size of the region served 107440064aecSDennis Zhou (Facebook) * 107540064aecSDennis Zhou (Facebook) * This is responsible for creating the chunks that serve the first chunk. The 107640064aecSDennis Zhou (Facebook) * base_addr is page aligned down of @tmp_addr while the region end is page 107740064aecSDennis Zhou (Facebook) * aligned up. Offsets are kept track of to determine the region served. All 107840064aecSDennis Zhou (Facebook) * this is done to appease the bitmap allocator in avoiding partial blocks. 107940064aecSDennis Zhou (Facebook) * 108040064aecSDennis Zhou (Facebook) * RETURNS: 108140064aecSDennis Zhou (Facebook) * Chunk serving the region at @tmp_addr of @map_size. 108240064aecSDennis Zhou (Facebook) */ 1083c0ebfdc3SDennis Zhou (Facebook) static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr, 108440064aecSDennis Zhou (Facebook) int map_size) 108510edf5b0SDennis Zhou (Facebook) { 108610edf5b0SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 1087ca460b3cSDennis Zhou (Facebook) unsigned long aligned_addr, lcm_align; 108840064aecSDennis Zhou (Facebook) int start_offset, offset_bits, region_size, region_bits; 1089*f655f405SMike Rapoport size_t alloc_size; 1090c0ebfdc3SDennis Zhou (Facebook) 1091c0ebfdc3SDennis Zhou (Facebook) /* region calculations */ 1092c0ebfdc3SDennis Zhou (Facebook) aligned_addr = tmp_addr & PAGE_MASK; 1093c0ebfdc3SDennis Zhou (Facebook) 1094c0ebfdc3SDennis Zhou (Facebook) start_offset = tmp_addr - aligned_addr; 10956b9d7c8eSDennis Zhou (Facebook) 1096ca460b3cSDennis Zhou (Facebook) /* 1097ca460b3cSDennis Zhou (Facebook) * Align the end of the region with the LCM of PAGE_SIZE and 1098ca460b3cSDennis Zhou (Facebook) * PCPU_BITMAP_BLOCK_SIZE. One of these constants is a multiple of 1099ca460b3cSDennis Zhou (Facebook) * the other. 1100ca460b3cSDennis Zhou (Facebook) */ 1101ca460b3cSDennis Zhou (Facebook) lcm_align = lcm(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE); 1102ca460b3cSDennis Zhou (Facebook) region_size = ALIGN(start_offset + map_size, lcm_align); 110310edf5b0SDennis Zhou (Facebook) 1104c0ebfdc3SDennis Zhou (Facebook) /* allocate chunk */ 1105*f655f405SMike Rapoport alloc_size = sizeof(struct pcpu_chunk) + 1106*f655f405SMike Rapoport BITS_TO_LONGS(region_size >> PAGE_SHIFT); 1107*f655f405SMike Rapoport chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1108*f655f405SMike Rapoport if (!chunk) 1109*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1110*f655f405SMike Rapoport alloc_size); 1111c0ebfdc3SDennis Zhou (Facebook) 111210edf5b0SDennis Zhou (Facebook) INIT_LIST_HEAD(&chunk->list); 1113c0ebfdc3SDennis Zhou (Facebook) 1114c0ebfdc3SDennis Zhou (Facebook) chunk->base_addr = (void *)aligned_addr; 111510edf5b0SDennis Zhou (Facebook) chunk->start_offset = start_offset; 11166b9d7c8eSDennis Zhou (Facebook) chunk->end_offset = region_size - chunk->start_offset - map_size; 1117c0ebfdc3SDennis Zhou (Facebook) 11188ab16c43SDennis Zhou (Facebook) chunk->nr_pages = region_size >> PAGE_SHIFT; 111940064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 1120c0ebfdc3SDennis Zhou (Facebook) 1121*f655f405SMike Rapoport alloc_size = BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]); 1122*f655f405SMike Rapoport chunk->alloc_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1123*f655f405SMike Rapoport if (!chunk->alloc_map) 1124*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1125*f655f405SMike Rapoport alloc_size); 1126*f655f405SMike Rapoport 1127*f655f405SMike Rapoport alloc_size = 1128*f655f405SMike Rapoport BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]); 1129*f655f405SMike Rapoport chunk->bound_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1130*f655f405SMike Rapoport if (!chunk->bound_map) 1131*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1132*f655f405SMike Rapoport alloc_size); 1133*f655f405SMike Rapoport 1134*f655f405SMike Rapoport alloc_size = pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]); 1135*f655f405SMike Rapoport chunk->md_blocks = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1136*f655f405SMike Rapoport if (!chunk->md_blocks) 1137*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1138*f655f405SMike Rapoport alloc_size); 1139*f655f405SMike Rapoport 1140ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 114110edf5b0SDennis Zhou (Facebook) 114210edf5b0SDennis Zhou (Facebook) /* manage populated page bitmap */ 114310edf5b0SDennis Zhou (Facebook) chunk->immutable = true; 11448ab16c43SDennis Zhou (Facebook) bitmap_fill(chunk->populated, chunk->nr_pages); 11458ab16c43SDennis Zhou (Facebook) chunk->nr_populated = chunk->nr_pages; 114640064aecSDennis Zhou (Facebook) chunk->nr_empty_pop_pages = 114740064aecSDennis Zhou (Facebook) pcpu_cnt_pop_pages(chunk, start_offset / PCPU_MIN_ALLOC_SIZE, 114840064aecSDennis Zhou (Facebook) map_size / PCPU_MIN_ALLOC_SIZE); 114910edf5b0SDennis Zhou (Facebook) 115040064aecSDennis Zhou (Facebook) chunk->contig_bits = map_size / PCPU_MIN_ALLOC_SIZE; 115140064aecSDennis Zhou (Facebook) chunk->free_bytes = map_size; 1152c0ebfdc3SDennis Zhou (Facebook) 1153c0ebfdc3SDennis Zhou (Facebook) if (chunk->start_offset) { 1154c0ebfdc3SDennis Zhou (Facebook) /* hide the beginning of the bitmap */ 115540064aecSDennis Zhou (Facebook) offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE; 115640064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 0, offset_bits); 115740064aecSDennis Zhou (Facebook) set_bit(0, chunk->bound_map); 115840064aecSDennis Zhou (Facebook) set_bit(offset_bits, chunk->bound_map); 1159ca460b3cSDennis Zhou (Facebook) 116086b442fbSDennis Zhou (Facebook) chunk->first_bit = offset_bits; 116186b442fbSDennis Zhou (Facebook) 1162ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, 0, offset_bits); 1163c0ebfdc3SDennis Zhou (Facebook) } 1164c0ebfdc3SDennis Zhou (Facebook) 11656b9d7c8eSDennis Zhou (Facebook) if (chunk->end_offset) { 11666b9d7c8eSDennis Zhou (Facebook) /* hide the end of the bitmap */ 116740064aecSDennis Zhou (Facebook) offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE; 116840064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 116940064aecSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk) - offset_bits, 117040064aecSDennis Zhou (Facebook) offset_bits); 117140064aecSDennis Zhou (Facebook) set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE, 117240064aecSDennis Zhou (Facebook) chunk->bound_map); 117340064aecSDennis Zhou (Facebook) set_bit(region_bits, chunk->bound_map); 11746b9d7c8eSDennis Zhou (Facebook) 1175ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk) 1176ca460b3cSDennis Zhou (Facebook) - offset_bits, offset_bits); 1177ca460b3cSDennis Zhou (Facebook) } 117840064aecSDennis Zhou (Facebook) 117910edf5b0SDennis Zhou (Facebook) return chunk; 118010edf5b0SDennis Zhou (Facebook) } 118110edf5b0SDennis Zhou (Facebook) 118247504ee0SDennis Zhou static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp) 11836081089fSTejun Heo { 11846081089fSTejun Heo struct pcpu_chunk *chunk; 118540064aecSDennis Zhou (Facebook) int region_bits; 11866081089fSTejun Heo 118747504ee0SDennis Zhou chunk = pcpu_mem_zalloc(pcpu_chunk_struct_size, gfp); 11886081089fSTejun Heo if (!chunk) 11896081089fSTejun Heo return NULL; 11906081089fSTejun Heo 11916081089fSTejun Heo INIT_LIST_HEAD(&chunk->list); 1192c0ebfdc3SDennis Zhou (Facebook) chunk->nr_pages = pcpu_unit_pages; 119340064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 119440064aecSDennis Zhou (Facebook) 119540064aecSDennis Zhou (Facebook) chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) * 119647504ee0SDennis Zhou sizeof(chunk->alloc_map[0]), gfp); 119740064aecSDennis Zhou (Facebook) if (!chunk->alloc_map) 119840064aecSDennis Zhou (Facebook) goto alloc_map_fail; 119940064aecSDennis Zhou (Facebook) 120040064aecSDennis Zhou (Facebook) chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) * 120147504ee0SDennis Zhou sizeof(chunk->bound_map[0]), gfp); 120240064aecSDennis Zhou (Facebook) if (!chunk->bound_map) 120340064aecSDennis Zhou (Facebook) goto bound_map_fail; 120440064aecSDennis Zhou (Facebook) 1205ca460b3cSDennis Zhou (Facebook) chunk->md_blocks = pcpu_mem_zalloc(pcpu_chunk_nr_blocks(chunk) * 120647504ee0SDennis Zhou sizeof(chunk->md_blocks[0]), gfp); 1207ca460b3cSDennis Zhou (Facebook) if (!chunk->md_blocks) 1208ca460b3cSDennis Zhou (Facebook) goto md_blocks_fail; 1209ca460b3cSDennis Zhou (Facebook) 1210ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 1211ca460b3cSDennis Zhou (Facebook) 121240064aecSDennis Zhou (Facebook) /* init metadata */ 121340064aecSDennis Zhou (Facebook) chunk->contig_bits = region_bits; 121440064aecSDennis Zhou (Facebook) chunk->free_bytes = chunk->nr_pages * PAGE_SIZE; 1215c0ebfdc3SDennis Zhou (Facebook) 12166081089fSTejun Heo return chunk; 121740064aecSDennis Zhou (Facebook) 1218ca460b3cSDennis Zhou (Facebook) md_blocks_fail: 1219ca460b3cSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 122040064aecSDennis Zhou (Facebook) bound_map_fail: 122140064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 122240064aecSDennis Zhou (Facebook) alloc_map_fail: 122340064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk); 122440064aecSDennis Zhou (Facebook) 122540064aecSDennis Zhou (Facebook) return NULL; 12266081089fSTejun Heo } 12276081089fSTejun Heo 12286081089fSTejun Heo static void pcpu_free_chunk(struct pcpu_chunk *chunk) 12296081089fSTejun Heo { 12306081089fSTejun Heo if (!chunk) 12316081089fSTejun Heo return; 12326685b357SMike Rapoport pcpu_mem_free(chunk->md_blocks); 123340064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 123440064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 12351d5cfdb0STetsuo Handa pcpu_mem_free(chunk); 12366081089fSTejun Heo } 12376081089fSTejun Heo 1238b539b87fSTejun Heo /** 1239b539b87fSTejun Heo * pcpu_chunk_populated - post-population bookkeeping 1240b539b87fSTejun Heo * @chunk: pcpu_chunk which got populated 1241b539b87fSTejun Heo * @page_start: the start page 1242b539b87fSTejun Heo * @page_end: the end page 124340064aecSDennis Zhou (Facebook) * @for_alloc: if this is to populate for allocation 1244b539b87fSTejun Heo * 1245b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been populated to @chunk. Update 1246b539b87fSTejun Heo * the bookkeeping information accordingly. Must be called after each 1247b539b87fSTejun Heo * successful population. 124840064aecSDennis Zhou (Facebook) * 124940064aecSDennis Zhou (Facebook) * If this is @for_alloc, do not increment pcpu_nr_empty_pop_pages because it 125040064aecSDennis Zhou (Facebook) * is to serve an allocation in that area. 1251b539b87fSTejun Heo */ 125240064aecSDennis Zhou (Facebook) static void pcpu_chunk_populated(struct pcpu_chunk *chunk, int page_start, 125340064aecSDennis Zhou (Facebook) int page_end, bool for_alloc) 1254b539b87fSTejun Heo { 1255b539b87fSTejun Heo int nr = page_end - page_start; 1256b539b87fSTejun Heo 1257b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1258b539b87fSTejun Heo 1259b539b87fSTejun Heo bitmap_set(chunk->populated, page_start, nr); 1260b539b87fSTejun Heo chunk->nr_populated += nr; 12617e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += nr; 126240064aecSDennis Zhou (Facebook) 126340064aecSDennis Zhou (Facebook) if (!for_alloc) { 12640cecf50cSDennis Zhou (Facebook) chunk->nr_empty_pop_pages += nr; 1265b539b87fSTejun Heo pcpu_nr_empty_pop_pages += nr; 1266b539b87fSTejun Heo } 126740064aecSDennis Zhou (Facebook) } 1268b539b87fSTejun Heo 1269b539b87fSTejun Heo /** 1270b539b87fSTejun Heo * pcpu_chunk_depopulated - post-depopulation bookkeeping 1271b539b87fSTejun Heo * @chunk: pcpu_chunk which got depopulated 1272b539b87fSTejun Heo * @page_start: the start page 1273b539b87fSTejun Heo * @page_end: the end page 1274b539b87fSTejun Heo * 1275b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been depopulated from @chunk. 1276b539b87fSTejun Heo * Update the bookkeeping information accordingly. Must be called after 1277b539b87fSTejun Heo * each successful depopulation. 1278b539b87fSTejun Heo */ 1279b539b87fSTejun Heo static void pcpu_chunk_depopulated(struct pcpu_chunk *chunk, 1280b539b87fSTejun Heo int page_start, int page_end) 1281b539b87fSTejun Heo { 1282b539b87fSTejun Heo int nr = page_end - page_start; 1283b539b87fSTejun Heo 1284b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1285b539b87fSTejun Heo 1286b539b87fSTejun Heo bitmap_clear(chunk->populated, page_start, nr); 1287b539b87fSTejun Heo chunk->nr_populated -= nr; 12880cecf50cSDennis Zhou (Facebook) chunk->nr_empty_pop_pages -= nr; 1289b539b87fSTejun Heo pcpu_nr_empty_pop_pages -= nr; 12907e8a6304SDennis Zhou (Facebook) pcpu_nr_populated -= nr; 1291b539b87fSTejun Heo } 1292b539b87fSTejun Heo 1293fbf59bc9STejun Heo /* 12949f645532STejun Heo * Chunk management implementation. 1295fbf59bc9STejun Heo * 12969f645532STejun Heo * To allow different implementations, chunk alloc/free and 12979f645532STejun Heo * [de]population are implemented in a separate file which is pulled 12989f645532STejun Heo * into this file and compiled together. The following functions 12999f645532STejun Heo * should be implemented. 1300ccea34b5STejun Heo * 13019f645532STejun Heo * pcpu_populate_chunk - populate the specified range of a chunk 13029f645532STejun Heo * pcpu_depopulate_chunk - depopulate the specified range of a chunk 13039f645532STejun Heo * pcpu_create_chunk - create a new chunk 13049f645532STejun Heo * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop 13059f645532STejun Heo * pcpu_addr_to_page - translate address to physical address 13069f645532STejun Heo * pcpu_verify_alloc_info - check alloc_info is acceptable during init 1307fbf59bc9STejun Heo */ 130815d9f3d1SDennis Zhou static int pcpu_populate_chunk(struct pcpu_chunk *chunk, 130947504ee0SDennis Zhou int page_start, int page_end, gfp_t gfp); 131015d9f3d1SDennis Zhou static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, 131115d9f3d1SDennis Zhou int page_start, int page_end); 131247504ee0SDennis Zhou static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp); 13139f645532STejun Heo static void pcpu_destroy_chunk(struct pcpu_chunk *chunk); 13149f645532STejun Heo static struct page *pcpu_addr_to_page(void *addr); 13159f645532STejun Heo static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai); 1316fbf59bc9STejun Heo 1317b0c9778bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_KM 1318b0c9778bSTejun Heo #include "percpu-km.c" 1319b0c9778bSTejun Heo #else 13209f645532STejun Heo #include "percpu-vm.c" 1321b0c9778bSTejun Heo #endif 1322fbf59bc9STejun Heo 1323fbf59bc9STejun Heo /** 132488999a89STejun Heo * pcpu_chunk_addr_search - determine chunk containing specified address 132588999a89STejun Heo * @addr: address for which the chunk needs to be determined. 132688999a89STejun Heo * 1327c0ebfdc3SDennis Zhou (Facebook) * This is an internal function that handles all but static allocations. 1328c0ebfdc3SDennis Zhou (Facebook) * Static percpu address values should never be passed into the allocator. 1329c0ebfdc3SDennis Zhou (Facebook) * 133088999a89STejun Heo * RETURNS: 133188999a89STejun Heo * The address of the found chunk. 133288999a89STejun Heo */ 133388999a89STejun Heo static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr) 133488999a89STejun Heo { 1335c0ebfdc3SDennis Zhou (Facebook) /* is it in the dynamic region (first chunk)? */ 1336560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_first_chunk, addr)) 1337c0ebfdc3SDennis Zhou (Facebook) return pcpu_first_chunk; 1338c0ebfdc3SDennis Zhou (Facebook) 1339c0ebfdc3SDennis Zhou (Facebook) /* is it in the reserved region? */ 1340560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_reserved_chunk, addr)) 134188999a89STejun Heo return pcpu_reserved_chunk; 134288999a89STejun Heo 134388999a89STejun Heo /* 134488999a89STejun Heo * The address is relative to unit0 which might be unused and 134588999a89STejun Heo * thus unmapped. Offset the address to the unit space of the 134688999a89STejun Heo * current processor before looking it up in the vmalloc 134788999a89STejun Heo * space. Note that any possible cpu id can be used here, so 134888999a89STejun Heo * there's no need to worry about preemption or cpu hotplug. 134988999a89STejun Heo */ 135088999a89STejun Heo addr += pcpu_unit_offsets[raw_smp_processor_id()]; 13519f645532STejun Heo return pcpu_get_page_chunk(pcpu_addr_to_page(addr)); 135288999a89STejun Heo } 135388999a89STejun Heo 135488999a89STejun Heo /** 1355edcb4639STejun Heo * pcpu_alloc - the percpu allocator 1356cae3aeb8STejun Heo * @size: size of area to allocate in bytes 1357fbf59bc9STejun Heo * @align: alignment of area (max PAGE_SIZE) 1358edcb4639STejun Heo * @reserved: allocate from the reserved chunk if available 13595835d96eSTejun Heo * @gfp: allocation flags 1360fbf59bc9STejun Heo * 13615835d96eSTejun Heo * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't 13620ea7eeecSDaniel Borkmann * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN 13630ea7eeecSDaniel Borkmann * then no warning will be triggered on invalid or failed allocation 13640ea7eeecSDaniel Borkmann * requests. 1365fbf59bc9STejun Heo * 1366fbf59bc9STejun Heo * RETURNS: 1367fbf59bc9STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1368fbf59bc9STejun Heo */ 13695835d96eSTejun Heo static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, 13705835d96eSTejun Heo gfp_t gfp) 1371fbf59bc9STejun Heo { 1372554fef1cSDennis Zhou /* whitelisted flags that can be passed to the backing allocators */ 1373554fef1cSDennis Zhou gfp_t pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 13740ea7eeecSDaniel Borkmann bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL; 13750ea7eeecSDaniel Borkmann bool do_warn = !(gfp & __GFP_NOWARN); 1376f2badb0cSTejun Heo static int warn_limit = 10; 1377fbf59bc9STejun Heo struct pcpu_chunk *chunk; 1378f2badb0cSTejun Heo const char *err; 137940064aecSDennis Zhou (Facebook) int slot, off, cpu, ret; 1380403a91b1SJiri Kosina unsigned long flags; 1381f528f0b8SCatalin Marinas void __percpu *ptr; 138240064aecSDennis Zhou (Facebook) size_t bits, bit_align; 1383fbf59bc9STejun Heo 1384723ad1d9SAl Viro /* 138540064aecSDennis Zhou (Facebook) * There is now a minimum allocation size of PCPU_MIN_ALLOC_SIZE, 138640064aecSDennis Zhou (Facebook) * therefore alignment must be a minimum of that many bytes. 138740064aecSDennis Zhou (Facebook) * An allocation may have internal fragmentation from rounding up 138840064aecSDennis Zhou (Facebook) * of up to PCPU_MIN_ALLOC_SIZE - 1 bytes. 1389723ad1d9SAl Viro */ 1390d2f3c384SDennis Zhou (Facebook) if (unlikely(align < PCPU_MIN_ALLOC_SIZE)) 1391d2f3c384SDennis Zhou (Facebook) align = PCPU_MIN_ALLOC_SIZE; 1392723ad1d9SAl Viro 1393d2f3c384SDennis Zhou (Facebook) size = ALIGN(size, PCPU_MIN_ALLOC_SIZE); 139440064aecSDennis Zhou (Facebook) bits = size >> PCPU_MIN_ALLOC_SHIFT; 139540064aecSDennis Zhou (Facebook) bit_align = align >> PCPU_MIN_ALLOC_SHIFT; 13962f69fa82SViro 13973ca45a46Szijun_hu if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE || 13983ca45a46Szijun_hu !is_power_of_2(align))) { 13990ea7eeecSDaniel Borkmann WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n", 1400756a025fSJoe Perches size, align); 1401fbf59bc9STejun Heo return NULL; 1402fbf59bc9STejun Heo } 1403fbf59bc9STejun Heo 1404f52ba1feSKirill Tkhai if (!is_atomic) { 1405f52ba1feSKirill Tkhai /* 1406f52ba1feSKirill Tkhai * pcpu_balance_workfn() allocates memory under this mutex, 1407f52ba1feSKirill Tkhai * and it may wait for memory reclaim. Allow current task 1408f52ba1feSKirill Tkhai * to become OOM victim, in case of memory pressure. 1409f52ba1feSKirill Tkhai */ 1410f52ba1feSKirill Tkhai if (gfp & __GFP_NOFAIL) 14116710e594STejun Heo mutex_lock(&pcpu_alloc_mutex); 1412f52ba1feSKirill Tkhai else if (mutex_lock_killable(&pcpu_alloc_mutex)) 1413f52ba1feSKirill Tkhai return NULL; 1414f52ba1feSKirill Tkhai } 14156710e594STejun Heo 1416403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1417fbf59bc9STejun Heo 1418edcb4639STejun Heo /* serve reserved allocations from the reserved chunk if available */ 1419edcb4639STejun Heo if (reserved && pcpu_reserved_chunk) { 1420edcb4639STejun Heo chunk = pcpu_reserved_chunk; 1421833af842STejun Heo 142240064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, is_atomic); 142340064aecSDennis Zhou (Facebook) if (off < 0) { 1424833af842STejun Heo err = "alloc from reserved chunk failed"; 1425ccea34b5STejun Heo goto fail_unlock; 1426f2badb0cSTejun Heo } 1427833af842STejun Heo 142840064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1429edcb4639STejun Heo if (off >= 0) 1430edcb4639STejun Heo goto area_found; 1431833af842STejun Heo 1432f2badb0cSTejun Heo err = "alloc from reserved chunk failed"; 1433ccea34b5STejun Heo goto fail_unlock; 1434edcb4639STejun Heo } 1435edcb4639STejun Heo 1436ccea34b5STejun Heo restart: 1437edcb4639STejun Heo /* search through normal chunks */ 1438fbf59bc9STejun Heo for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) { 1439fbf59bc9STejun Heo list_for_each_entry(chunk, &pcpu_slot[slot], list) { 144040064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, 144140064aecSDennis Zhou (Facebook) is_atomic); 144240064aecSDennis Zhou (Facebook) if (off < 0) 1443fbf59bc9STejun Heo continue; 1444ccea34b5STejun Heo 144540064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1446fbf59bc9STejun Heo if (off >= 0) 1447fbf59bc9STejun Heo goto area_found; 144840064aecSDennis Zhou (Facebook) 1449fbf59bc9STejun Heo } 1450fbf59bc9STejun Heo } 1451fbf59bc9STejun Heo 1452403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1453ccea34b5STejun Heo 1454b38d08f3STejun Heo /* 1455b38d08f3STejun Heo * No space left. Create a new chunk. We don't want multiple 1456b38d08f3STejun Heo * tasks to create chunks simultaneously. Serialize and create iff 1457b38d08f3STejun Heo * there's still no empty chunk after grabbing the mutex. 1458b38d08f3STejun Heo */ 145911df02bfSDennis Zhou if (is_atomic) { 146011df02bfSDennis Zhou err = "atomic alloc failed, no space left"; 14615835d96eSTejun Heo goto fail; 146211df02bfSDennis Zhou } 14635835d96eSTejun Heo 1464b38d08f3STejun Heo if (list_empty(&pcpu_slot[pcpu_nr_slots - 1])) { 1465554fef1cSDennis Zhou chunk = pcpu_create_chunk(pcpu_gfp); 1466f2badb0cSTejun Heo if (!chunk) { 1467f2badb0cSTejun Heo err = "failed to allocate new chunk"; 1468b38d08f3STejun Heo goto fail; 1469f2badb0cSTejun Heo } 1470ccea34b5STejun Heo 1471403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1472fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, -1); 1473b38d08f3STejun Heo } else { 1474b38d08f3STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1475b38d08f3STejun Heo } 1476b38d08f3STejun Heo 1477ccea34b5STejun Heo goto restart; 1478fbf59bc9STejun Heo 1479fbf59bc9STejun Heo area_found: 148030a5b536SDennis Zhou pcpu_stats_area_alloc(chunk, size); 1481403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1482ccea34b5STejun Heo 1483dca49645STejun Heo /* populate if not all pages are already there */ 14845835d96eSTejun Heo if (!is_atomic) { 1485e04d3208STejun Heo int page_start, page_end, rs, re; 1486e04d3208STejun Heo 1487dca49645STejun Heo page_start = PFN_DOWN(off); 1488dca49645STejun Heo page_end = PFN_UP(off + size); 1489dca49645STejun Heo 149091e914c5SDennis Zhou (Facebook) pcpu_for_each_unpop_region(chunk->populated, rs, re, 149191e914c5SDennis Zhou (Facebook) page_start, page_end) { 1492dca49645STejun Heo WARN_ON(chunk->immutable); 1493dca49645STejun Heo 1494554fef1cSDennis Zhou ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp); 1495b38d08f3STejun Heo 1496403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1497b38d08f3STejun Heo if (ret) { 149840064aecSDennis Zhou (Facebook) pcpu_free_area(chunk, off); 1499f2badb0cSTejun Heo err = "failed to populate"; 1500ccea34b5STejun Heo goto fail_unlock; 1501fbf59bc9STejun Heo } 150240064aecSDennis Zhou (Facebook) pcpu_chunk_populated(chunk, rs, re, true); 1503b38d08f3STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1504dca49645STejun Heo } 1505dca49645STejun Heo 1506ccea34b5STejun Heo mutex_unlock(&pcpu_alloc_mutex); 1507e04d3208STejun Heo } 1508ccea34b5STejun Heo 15091a4d7607STejun Heo if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_LOW) 15101a4d7607STejun Heo pcpu_schedule_balance_work(); 15111a4d7607STejun Heo 1512dca49645STejun Heo /* clear the areas and return address relative to base address */ 1513dca49645STejun Heo for_each_possible_cpu(cpu) 1514dca49645STejun Heo memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size); 1515dca49645STejun Heo 1516f528f0b8SCatalin Marinas ptr = __addr_to_pcpu_ptr(chunk->base_addr + off); 15178a8c35faSLarry Finger kmemleak_alloc_percpu(ptr, size, gfp); 1518df95e795SDennis Zhou 1519df95e795SDennis Zhou trace_percpu_alloc_percpu(reserved, is_atomic, size, align, 1520df95e795SDennis Zhou chunk->base_addr, off, ptr); 1521df95e795SDennis Zhou 1522f528f0b8SCatalin Marinas return ptr; 1523ccea34b5STejun Heo 1524ccea34b5STejun Heo fail_unlock: 1525403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1526b38d08f3STejun Heo fail: 1527df95e795SDennis Zhou trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align); 1528df95e795SDennis Zhou 15290ea7eeecSDaniel Borkmann if (!is_atomic && do_warn && warn_limit) { 1530870d4b12SJoe Perches pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n", 15315835d96eSTejun Heo size, align, is_atomic, err); 1532f2badb0cSTejun Heo dump_stack(); 1533f2badb0cSTejun Heo if (!--warn_limit) 1534870d4b12SJoe Perches pr_info("limit reached, disable warning\n"); 1535f2badb0cSTejun Heo } 15361a4d7607STejun Heo if (is_atomic) { 15371a4d7607STejun Heo /* see the flag handling in pcpu_blance_workfn() */ 15381a4d7607STejun Heo pcpu_atomic_alloc_failed = true; 15391a4d7607STejun Heo pcpu_schedule_balance_work(); 15406710e594STejun Heo } else { 15416710e594STejun Heo mutex_unlock(&pcpu_alloc_mutex); 15421a4d7607STejun Heo } 1543ccea34b5STejun Heo return NULL; 1544fbf59bc9STejun Heo } 1545edcb4639STejun Heo 1546edcb4639STejun Heo /** 15475835d96eSTejun Heo * __alloc_percpu_gfp - allocate dynamic percpu area 1548edcb4639STejun Heo * @size: size of area to allocate in bytes 1549edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 15505835d96eSTejun Heo * @gfp: allocation flags 1551edcb4639STejun Heo * 15525835d96eSTejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align. If 15535835d96eSTejun Heo * @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can 15540ea7eeecSDaniel Borkmann * be called from any context but is a lot more likely to fail. If @gfp 15550ea7eeecSDaniel Borkmann * has __GFP_NOWARN then no warning will be triggered on invalid or failed 15560ea7eeecSDaniel Borkmann * allocation requests. 1557ccea34b5STejun Heo * 1558edcb4639STejun Heo * RETURNS: 1559edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1560edcb4639STejun Heo */ 15615835d96eSTejun Heo void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) 15625835d96eSTejun Heo { 15635835d96eSTejun Heo return pcpu_alloc(size, align, false, gfp); 15645835d96eSTejun Heo } 15655835d96eSTejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu_gfp); 15665835d96eSTejun Heo 15675835d96eSTejun Heo /** 15685835d96eSTejun Heo * __alloc_percpu - allocate dynamic percpu area 15695835d96eSTejun Heo * @size: size of area to allocate in bytes 15705835d96eSTejun Heo * @align: alignment of area (max PAGE_SIZE) 15715835d96eSTejun Heo * 15725835d96eSTejun Heo * Equivalent to __alloc_percpu_gfp(size, align, %GFP_KERNEL). 15735835d96eSTejun Heo */ 157443cf38ebSTejun Heo void __percpu *__alloc_percpu(size_t size, size_t align) 1575edcb4639STejun Heo { 15765835d96eSTejun Heo return pcpu_alloc(size, align, false, GFP_KERNEL); 1577edcb4639STejun Heo } 1578fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu); 1579fbf59bc9STejun Heo 1580edcb4639STejun Heo /** 1581edcb4639STejun Heo * __alloc_reserved_percpu - allocate reserved percpu area 1582edcb4639STejun Heo * @size: size of area to allocate in bytes 1583edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 1584edcb4639STejun Heo * 15859329ba97STejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align 15869329ba97STejun Heo * from reserved percpu area if arch has set it up; otherwise, 15879329ba97STejun Heo * allocation is served from the same dynamic area. Might sleep. 15889329ba97STejun Heo * Might trigger writeouts. 1589edcb4639STejun Heo * 1590ccea34b5STejun Heo * CONTEXT: 1591ccea34b5STejun Heo * Does GFP_KERNEL allocation. 1592ccea34b5STejun Heo * 1593edcb4639STejun Heo * RETURNS: 1594edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1595edcb4639STejun Heo */ 159643cf38ebSTejun Heo void __percpu *__alloc_reserved_percpu(size_t size, size_t align) 1597edcb4639STejun Heo { 15985835d96eSTejun Heo return pcpu_alloc(size, align, true, GFP_KERNEL); 1599edcb4639STejun Heo } 1600edcb4639STejun Heo 1601a56dbddfSTejun Heo /** 16021a4d7607STejun Heo * pcpu_balance_workfn - manage the amount of free chunks and populated pages 1603a56dbddfSTejun Heo * @work: unused 1604a56dbddfSTejun Heo * 160547504ee0SDennis Zhou * Reclaim all fully free chunks except for the first one. This is also 160647504ee0SDennis Zhou * responsible for maintaining the pool of empty populated pages. However, 160747504ee0SDennis Zhou * it is possible that this is called when physical memory is scarce causing 160847504ee0SDennis Zhou * OOM killer to be triggered. We should avoid doing so until an actual 160947504ee0SDennis Zhou * allocation causes the failure as it is possible that requests can be 161047504ee0SDennis Zhou * serviced from already backed regions. 1611a56dbddfSTejun Heo */ 1612fe6bd8c3STejun Heo static void pcpu_balance_workfn(struct work_struct *work) 1613fbf59bc9STejun Heo { 161447504ee0SDennis Zhou /* gfp flags passed to underlying allocators */ 1615554fef1cSDennis Zhou const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN; 1616fe6bd8c3STejun Heo LIST_HEAD(to_free); 1617fe6bd8c3STejun Heo struct list_head *free_head = &pcpu_slot[pcpu_nr_slots - 1]; 1618a56dbddfSTejun Heo struct pcpu_chunk *chunk, *next; 16191a4d7607STejun Heo int slot, nr_to_pop, ret; 1620a56dbddfSTejun Heo 16211a4d7607STejun Heo /* 16221a4d7607STejun Heo * There's no reason to keep around multiple unused chunks and VM 16231a4d7607STejun Heo * areas can be scarce. Destroy all free chunks except for one. 16241a4d7607STejun Heo */ 1625ccea34b5STejun Heo mutex_lock(&pcpu_alloc_mutex); 1626ccea34b5STejun Heo spin_lock_irq(&pcpu_lock); 1627a56dbddfSTejun Heo 1628fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, free_head, list) { 16298d408b4bSTejun Heo WARN_ON(chunk->immutable); 1630a56dbddfSTejun Heo 1631a56dbddfSTejun Heo /* spare the first one */ 1632fe6bd8c3STejun Heo if (chunk == list_first_entry(free_head, struct pcpu_chunk, list)) 1633a56dbddfSTejun Heo continue; 1634a56dbddfSTejun Heo 1635fe6bd8c3STejun Heo list_move(&chunk->list, &to_free); 1636a56dbddfSTejun Heo } 1637a56dbddfSTejun Heo 1638ccea34b5STejun Heo spin_unlock_irq(&pcpu_lock); 1639a56dbddfSTejun Heo 1640fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, &to_free, list) { 1641a93ace48STejun Heo int rs, re; 1642dca49645STejun Heo 164391e914c5SDennis Zhou (Facebook) pcpu_for_each_pop_region(chunk->populated, rs, re, 0, 164491e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 1645a93ace48STejun Heo pcpu_depopulate_chunk(chunk, rs, re); 1646b539b87fSTejun Heo spin_lock_irq(&pcpu_lock); 1647b539b87fSTejun Heo pcpu_chunk_depopulated(chunk, rs, re); 1648b539b87fSTejun Heo spin_unlock_irq(&pcpu_lock); 1649a93ace48STejun Heo } 16506081089fSTejun Heo pcpu_destroy_chunk(chunk); 1651accd4f36SEric Dumazet cond_resched(); 1652fbf59bc9STejun Heo } 1653971f3918STejun Heo 16541a4d7607STejun Heo /* 16551a4d7607STejun Heo * Ensure there are certain number of free populated pages for 16561a4d7607STejun Heo * atomic allocs. Fill up from the most packed so that atomic 16571a4d7607STejun Heo * allocs don't increase fragmentation. If atomic allocation 16581a4d7607STejun Heo * failed previously, always populate the maximum amount. This 16591a4d7607STejun Heo * should prevent atomic allocs larger than PAGE_SIZE from keeping 16601a4d7607STejun Heo * failing indefinitely; however, large atomic allocs are not 16611a4d7607STejun Heo * something we support properly and can be highly unreliable and 16621a4d7607STejun Heo * inefficient. 16631a4d7607STejun Heo */ 16641a4d7607STejun Heo retry_pop: 16651a4d7607STejun Heo if (pcpu_atomic_alloc_failed) { 16661a4d7607STejun Heo nr_to_pop = PCPU_EMPTY_POP_PAGES_HIGH; 16671a4d7607STejun Heo /* best effort anyway, don't worry about synchronization */ 16681a4d7607STejun Heo pcpu_atomic_alloc_failed = false; 16691a4d7607STejun Heo } else { 16701a4d7607STejun Heo nr_to_pop = clamp(PCPU_EMPTY_POP_PAGES_HIGH - 16711a4d7607STejun Heo pcpu_nr_empty_pop_pages, 16721a4d7607STejun Heo 0, PCPU_EMPTY_POP_PAGES_HIGH); 16731a4d7607STejun Heo } 16741a4d7607STejun Heo 16751a4d7607STejun Heo for (slot = pcpu_size_to_slot(PAGE_SIZE); slot < pcpu_nr_slots; slot++) { 16761a4d7607STejun Heo int nr_unpop = 0, rs, re; 16771a4d7607STejun Heo 16781a4d7607STejun Heo if (!nr_to_pop) 16791a4d7607STejun Heo break; 16801a4d7607STejun Heo 16811a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 16821a4d7607STejun Heo list_for_each_entry(chunk, &pcpu_slot[slot], list) { 16838ab16c43SDennis Zhou (Facebook) nr_unpop = chunk->nr_pages - chunk->nr_populated; 16841a4d7607STejun Heo if (nr_unpop) 16851a4d7607STejun Heo break; 16861a4d7607STejun Heo } 16871a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 16881a4d7607STejun Heo 16891a4d7607STejun Heo if (!nr_unpop) 16901a4d7607STejun Heo continue; 16911a4d7607STejun Heo 16921a4d7607STejun Heo /* @chunk can't go away while pcpu_alloc_mutex is held */ 169391e914c5SDennis Zhou (Facebook) pcpu_for_each_unpop_region(chunk->populated, rs, re, 0, 169491e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 16951a4d7607STejun Heo int nr = min(re - rs, nr_to_pop); 16961a4d7607STejun Heo 169747504ee0SDennis Zhou ret = pcpu_populate_chunk(chunk, rs, rs + nr, gfp); 16981a4d7607STejun Heo if (!ret) { 16991a4d7607STejun Heo nr_to_pop -= nr; 17001a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 170140064aecSDennis Zhou (Facebook) pcpu_chunk_populated(chunk, rs, rs + nr, false); 17021a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 17031a4d7607STejun Heo } else { 17041a4d7607STejun Heo nr_to_pop = 0; 17051a4d7607STejun Heo } 17061a4d7607STejun Heo 17071a4d7607STejun Heo if (!nr_to_pop) 17081a4d7607STejun Heo break; 17091a4d7607STejun Heo } 17101a4d7607STejun Heo } 17111a4d7607STejun Heo 17121a4d7607STejun Heo if (nr_to_pop) { 17131a4d7607STejun Heo /* ran out of chunks to populate, create a new one and retry */ 171447504ee0SDennis Zhou chunk = pcpu_create_chunk(gfp); 17151a4d7607STejun Heo if (chunk) { 17161a4d7607STejun Heo spin_lock_irq(&pcpu_lock); 17171a4d7607STejun Heo pcpu_chunk_relocate(chunk, -1); 17181a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 17191a4d7607STejun Heo goto retry_pop; 17201a4d7607STejun Heo } 17211a4d7607STejun Heo } 17221a4d7607STejun Heo 1723971f3918STejun Heo mutex_unlock(&pcpu_alloc_mutex); 1724a56dbddfSTejun Heo } 1725fbf59bc9STejun Heo 1726fbf59bc9STejun Heo /** 1727fbf59bc9STejun Heo * free_percpu - free percpu area 1728fbf59bc9STejun Heo * @ptr: pointer to area to free 1729fbf59bc9STejun Heo * 1730ccea34b5STejun Heo * Free percpu area @ptr. 1731ccea34b5STejun Heo * 1732ccea34b5STejun Heo * CONTEXT: 1733ccea34b5STejun Heo * Can be called from atomic context. 1734fbf59bc9STejun Heo */ 173543cf38ebSTejun Heo void free_percpu(void __percpu *ptr) 1736fbf59bc9STejun Heo { 1737129182e5SAndrew Morton void *addr; 1738fbf59bc9STejun Heo struct pcpu_chunk *chunk; 1739ccea34b5STejun Heo unsigned long flags; 174040064aecSDennis Zhou (Facebook) int off; 1741fbf59bc9STejun Heo 1742fbf59bc9STejun Heo if (!ptr) 1743fbf59bc9STejun Heo return; 1744fbf59bc9STejun Heo 1745f528f0b8SCatalin Marinas kmemleak_free_percpu(ptr); 1746f528f0b8SCatalin Marinas 1747129182e5SAndrew Morton addr = __pcpu_ptr_to_addr(ptr); 1748129182e5SAndrew Morton 1749ccea34b5STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1750fbf59bc9STejun Heo 1751fbf59bc9STejun Heo chunk = pcpu_chunk_addr_search(addr); 1752bba174f5STejun Heo off = addr - chunk->base_addr; 1753fbf59bc9STejun Heo 175440064aecSDennis Zhou (Facebook) pcpu_free_area(chunk, off); 1755fbf59bc9STejun Heo 1756a56dbddfSTejun Heo /* if there are more than one fully free chunks, wake up grim reaper */ 175740064aecSDennis Zhou (Facebook) if (chunk->free_bytes == pcpu_unit_size) { 1758fbf59bc9STejun Heo struct pcpu_chunk *pos; 1759fbf59bc9STejun Heo 1760a56dbddfSTejun Heo list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list) 1761fbf59bc9STejun Heo if (pos != chunk) { 17621a4d7607STejun Heo pcpu_schedule_balance_work(); 1763fbf59bc9STejun Heo break; 1764fbf59bc9STejun Heo } 1765fbf59bc9STejun Heo } 1766fbf59bc9STejun Heo 1767df95e795SDennis Zhou trace_percpu_free_percpu(chunk->base_addr, off, ptr); 1768df95e795SDennis Zhou 1769ccea34b5STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1770fbf59bc9STejun Heo } 1771fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(free_percpu); 1772fbf59bc9STejun Heo 1773383776faSThomas Gleixner bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr) 1774383776faSThomas Gleixner { 1775383776faSThomas Gleixner #ifdef CONFIG_SMP 1776383776faSThomas Gleixner const size_t static_size = __per_cpu_end - __per_cpu_start; 1777383776faSThomas Gleixner void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 1778383776faSThomas Gleixner unsigned int cpu; 1779383776faSThomas Gleixner 1780383776faSThomas Gleixner for_each_possible_cpu(cpu) { 1781383776faSThomas Gleixner void *start = per_cpu_ptr(base, cpu); 1782383776faSThomas Gleixner void *va = (void *)addr; 1783383776faSThomas Gleixner 1784383776faSThomas Gleixner if (va >= start && va < start + static_size) { 17858ce371f9SPeter Zijlstra if (can_addr) { 1786383776faSThomas Gleixner *can_addr = (unsigned long) (va - start); 17878ce371f9SPeter Zijlstra *can_addr += (unsigned long) 17888ce371f9SPeter Zijlstra per_cpu_ptr(base, get_boot_cpu_id()); 17898ce371f9SPeter Zijlstra } 1790383776faSThomas Gleixner return true; 1791383776faSThomas Gleixner } 1792383776faSThomas Gleixner } 1793383776faSThomas Gleixner #endif 1794383776faSThomas Gleixner /* on UP, can't distinguish from other static vars, always false */ 1795383776faSThomas Gleixner return false; 1796383776faSThomas Gleixner } 1797383776faSThomas Gleixner 17983b034b0dSVivek Goyal /** 179910fad5e4STejun Heo * is_kernel_percpu_address - test whether address is from static percpu area 180010fad5e4STejun Heo * @addr: address to test 180110fad5e4STejun Heo * 180210fad5e4STejun Heo * Test whether @addr belongs to in-kernel static percpu area. Module 180310fad5e4STejun Heo * static percpu areas are not considered. For those, use 180410fad5e4STejun Heo * is_module_percpu_address(). 180510fad5e4STejun Heo * 180610fad5e4STejun Heo * RETURNS: 180710fad5e4STejun Heo * %true if @addr is from in-kernel static percpu area, %false otherwise. 180810fad5e4STejun Heo */ 180910fad5e4STejun Heo bool is_kernel_percpu_address(unsigned long addr) 181010fad5e4STejun Heo { 1811383776faSThomas Gleixner return __is_kernel_percpu_address(addr, NULL); 181210fad5e4STejun Heo } 181310fad5e4STejun Heo 181410fad5e4STejun Heo /** 18153b034b0dSVivek Goyal * per_cpu_ptr_to_phys - convert translated percpu address to physical address 18163b034b0dSVivek Goyal * @addr: the address to be converted to physical address 18173b034b0dSVivek Goyal * 18183b034b0dSVivek Goyal * Given @addr which is dereferenceable address obtained via one of 18193b034b0dSVivek Goyal * percpu access macros, this function translates it into its physical 18203b034b0dSVivek Goyal * address. The caller is responsible for ensuring @addr stays valid 18213b034b0dSVivek Goyal * until this function finishes. 18223b034b0dSVivek Goyal * 182367589c71SDave Young * percpu allocator has special setup for the first chunk, which currently 182467589c71SDave Young * supports either embedding in linear address space or vmalloc mapping, 182567589c71SDave Young * and, from the second one, the backing allocator (currently either vm or 182667589c71SDave Young * km) provides translation. 182767589c71SDave Young * 1828bffc4375SYannick Guerrini * The addr can be translated simply without checking if it falls into the 182967589c71SDave Young * first chunk. But the current code reflects better how percpu allocator 183067589c71SDave Young * actually works, and the verification can discover both bugs in percpu 183167589c71SDave Young * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current 183267589c71SDave Young * code. 183367589c71SDave Young * 18343b034b0dSVivek Goyal * RETURNS: 18353b034b0dSVivek Goyal * The physical address for @addr. 18363b034b0dSVivek Goyal */ 18373b034b0dSVivek Goyal phys_addr_t per_cpu_ptr_to_phys(void *addr) 18383b034b0dSVivek Goyal { 18399983b6f0STejun Heo void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 18409983b6f0STejun Heo bool in_first_chunk = false; 1841a855b84cSTejun Heo unsigned long first_low, first_high; 18429983b6f0STejun Heo unsigned int cpu; 18439983b6f0STejun Heo 18449983b6f0STejun Heo /* 1845a855b84cSTejun Heo * The following test on unit_low/high isn't strictly 18469983b6f0STejun Heo * necessary but will speed up lookups of addresses which 18479983b6f0STejun Heo * aren't in the first chunk. 1848c0ebfdc3SDennis Zhou (Facebook) * 1849c0ebfdc3SDennis Zhou (Facebook) * The address check is against full chunk sizes. pcpu_base_addr 1850c0ebfdc3SDennis Zhou (Facebook) * points to the beginning of the first chunk including the 1851c0ebfdc3SDennis Zhou (Facebook) * static region. Assumes good intent as the first chunk may 1852c0ebfdc3SDennis Zhou (Facebook) * not be full (ie. < pcpu_unit_pages in size). 18539983b6f0STejun Heo */ 1854c0ebfdc3SDennis Zhou (Facebook) first_low = (unsigned long)pcpu_base_addr + 1855c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_low_unit_cpu, 0); 1856c0ebfdc3SDennis Zhou (Facebook) first_high = (unsigned long)pcpu_base_addr + 1857c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_high_unit_cpu, pcpu_unit_pages); 1858a855b84cSTejun Heo if ((unsigned long)addr >= first_low && 1859a855b84cSTejun Heo (unsigned long)addr < first_high) { 18609983b6f0STejun Heo for_each_possible_cpu(cpu) { 18619983b6f0STejun Heo void *start = per_cpu_ptr(base, cpu); 18629983b6f0STejun Heo 18639983b6f0STejun Heo if (addr >= start && addr < start + pcpu_unit_size) { 18649983b6f0STejun Heo in_first_chunk = true; 18659983b6f0STejun Heo break; 18669983b6f0STejun Heo } 18679983b6f0STejun Heo } 18689983b6f0STejun Heo } 18699983b6f0STejun Heo 18709983b6f0STejun Heo if (in_first_chunk) { 1871eac522efSDavid Howells if (!is_vmalloc_addr(addr)) 18723b034b0dSVivek Goyal return __pa(addr); 18733b034b0dSVivek Goyal else 18749f57bd4dSEugene Surovegin return page_to_phys(vmalloc_to_page(addr)) + 18759f57bd4dSEugene Surovegin offset_in_page(addr); 1876020ec653STejun Heo } else 18779f57bd4dSEugene Surovegin return page_to_phys(pcpu_addr_to_page(addr)) + 18789f57bd4dSEugene Surovegin offset_in_page(addr); 18793b034b0dSVivek Goyal } 18803b034b0dSVivek Goyal 1881fbf59bc9STejun Heo /** 1882fd1e8a1fSTejun Heo * pcpu_alloc_alloc_info - allocate percpu allocation info 1883fd1e8a1fSTejun Heo * @nr_groups: the number of groups 1884fd1e8a1fSTejun Heo * @nr_units: the number of units 1885033e48fbSTejun Heo * 1886fd1e8a1fSTejun Heo * Allocate ai which is large enough for @nr_groups groups containing 1887fd1e8a1fSTejun Heo * @nr_units units. The returned ai's groups[0].cpu_map points to the 1888fd1e8a1fSTejun Heo * cpu_map array which is long enough for @nr_units and filled with 1889fd1e8a1fSTejun Heo * NR_CPUS. It's the caller's responsibility to initialize cpu_map 1890fd1e8a1fSTejun Heo * pointer of other groups. 1891033e48fbSTejun Heo * 1892033e48fbSTejun Heo * RETURNS: 1893fd1e8a1fSTejun Heo * Pointer to the allocated pcpu_alloc_info on success, NULL on 1894fd1e8a1fSTejun Heo * failure. 1895033e48fbSTejun Heo */ 1896fd1e8a1fSTejun Heo struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups, 1897fd1e8a1fSTejun Heo int nr_units) 1898fd1e8a1fSTejun Heo { 1899fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 1900fd1e8a1fSTejun Heo size_t base_size, ai_size; 1901fd1e8a1fSTejun Heo void *ptr; 1902fd1e8a1fSTejun Heo int unit; 1903fd1e8a1fSTejun Heo 1904fd1e8a1fSTejun Heo base_size = ALIGN(sizeof(*ai) + nr_groups * sizeof(ai->groups[0]), 1905fd1e8a1fSTejun Heo __alignof__(ai->groups[0].cpu_map[0])); 1906fd1e8a1fSTejun Heo ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]); 1907fd1e8a1fSTejun Heo 1908eb31d559SMike Rapoport ptr = memblock_alloc_nopanic(PFN_ALIGN(ai_size), PAGE_SIZE); 1909fd1e8a1fSTejun Heo if (!ptr) 1910fd1e8a1fSTejun Heo return NULL; 1911fd1e8a1fSTejun Heo ai = ptr; 1912fd1e8a1fSTejun Heo ptr += base_size; 1913fd1e8a1fSTejun Heo 1914fd1e8a1fSTejun Heo ai->groups[0].cpu_map = ptr; 1915fd1e8a1fSTejun Heo 1916fd1e8a1fSTejun Heo for (unit = 0; unit < nr_units; unit++) 1917fd1e8a1fSTejun Heo ai->groups[0].cpu_map[unit] = NR_CPUS; 1918fd1e8a1fSTejun Heo 1919fd1e8a1fSTejun Heo ai->nr_groups = nr_groups; 1920fd1e8a1fSTejun Heo ai->__ai_size = PFN_ALIGN(ai_size); 1921fd1e8a1fSTejun Heo 1922fd1e8a1fSTejun Heo return ai; 1923fd1e8a1fSTejun Heo } 1924fd1e8a1fSTejun Heo 1925fd1e8a1fSTejun Heo /** 1926fd1e8a1fSTejun Heo * pcpu_free_alloc_info - free percpu allocation info 1927fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info to free 1928fd1e8a1fSTejun Heo * 1929fd1e8a1fSTejun Heo * Free @ai which was allocated by pcpu_alloc_alloc_info(). 1930fd1e8a1fSTejun Heo */ 1931fd1e8a1fSTejun Heo void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai) 1932fd1e8a1fSTejun Heo { 1933999c17e3SSantosh Shilimkar memblock_free_early(__pa(ai), ai->__ai_size); 1934fd1e8a1fSTejun Heo } 1935fd1e8a1fSTejun Heo 1936fd1e8a1fSTejun Heo /** 1937fd1e8a1fSTejun Heo * pcpu_dump_alloc_info - print out information about pcpu_alloc_info 1938fd1e8a1fSTejun Heo * @lvl: loglevel 1939fd1e8a1fSTejun Heo * @ai: allocation info to dump 1940fd1e8a1fSTejun Heo * 1941fd1e8a1fSTejun Heo * Print out information about @ai using loglevel @lvl. 1942fd1e8a1fSTejun Heo */ 1943fd1e8a1fSTejun Heo static void pcpu_dump_alloc_info(const char *lvl, 1944fd1e8a1fSTejun Heo const struct pcpu_alloc_info *ai) 1945033e48fbSTejun Heo { 1946fd1e8a1fSTejun Heo int group_width = 1, cpu_width = 1, width; 1947033e48fbSTejun Heo char empty_str[] = "--------"; 1948fd1e8a1fSTejun Heo int alloc = 0, alloc_end = 0; 1949fd1e8a1fSTejun Heo int group, v; 1950fd1e8a1fSTejun Heo int upa, apl; /* units per alloc, allocs per line */ 1951033e48fbSTejun Heo 1952fd1e8a1fSTejun Heo v = ai->nr_groups; 1953033e48fbSTejun Heo while (v /= 10) 1954fd1e8a1fSTejun Heo group_width++; 1955033e48fbSTejun Heo 1956fd1e8a1fSTejun Heo v = num_possible_cpus(); 1957fd1e8a1fSTejun Heo while (v /= 10) 1958fd1e8a1fSTejun Heo cpu_width++; 1959fd1e8a1fSTejun Heo empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0'; 1960033e48fbSTejun Heo 1961fd1e8a1fSTejun Heo upa = ai->alloc_size / ai->unit_size; 1962fd1e8a1fSTejun Heo width = upa * (cpu_width + 1) + group_width + 3; 1963fd1e8a1fSTejun Heo apl = rounddown_pow_of_two(max(60 / width, 1)); 1964033e48fbSTejun Heo 1965fd1e8a1fSTejun Heo printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu", 1966fd1e8a1fSTejun Heo lvl, ai->static_size, ai->reserved_size, ai->dyn_size, 1967fd1e8a1fSTejun Heo ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size); 1968fd1e8a1fSTejun Heo 1969fd1e8a1fSTejun Heo for (group = 0; group < ai->nr_groups; group++) { 1970fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 1971fd1e8a1fSTejun Heo int unit = 0, unit_end = 0; 1972fd1e8a1fSTejun Heo 1973fd1e8a1fSTejun Heo BUG_ON(gi->nr_units % upa); 1974fd1e8a1fSTejun Heo for (alloc_end += gi->nr_units / upa; 1975fd1e8a1fSTejun Heo alloc < alloc_end; alloc++) { 1976fd1e8a1fSTejun Heo if (!(alloc % apl)) { 19771170532bSJoe Perches pr_cont("\n"); 1978fd1e8a1fSTejun Heo printk("%spcpu-alloc: ", lvl); 1979033e48fbSTejun Heo } 19801170532bSJoe Perches pr_cont("[%0*d] ", group_width, group); 1981fd1e8a1fSTejun Heo 1982fd1e8a1fSTejun Heo for (unit_end += upa; unit < unit_end; unit++) 1983fd1e8a1fSTejun Heo if (gi->cpu_map[unit] != NR_CPUS) 19841170532bSJoe Perches pr_cont("%0*d ", 19851170532bSJoe Perches cpu_width, gi->cpu_map[unit]); 1986033e48fbSTejun Heo else 19871170532bSJoe Perches pr_cont("%s ", empty_str); 1988033e48fbSTejun Heo } 1989fd1e8a1fSTejun Heo } 19901170532bSJoe Perches pr_cont("\n"); 1991033e48fbSTejun Heo } 1992033e48fbSTejun Heo 1993fbf59bc9STejun Heo /** 19948d408b4bSTejun Heo * pcpu_setup_first_chunk - initialize the first percpu chunk 1995fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info describing how to percpu area is shaped 199638a6be52STejun Heo * @base_addr: mapped address 1997fbf59bc9STejun Heo * 19988d408b4bSTejun Heo * Initialize the first percpu chunk which contains the kernel static 19998d408b4bSTejun Heo * perpcu area. This function is to be called from arch percpu area 200038a6be52STejun Heo * setup path. 20018d408b4bSTejun Heo * 2002fd1e8a1fSTejun Heo * @ai contains all information necessary to initialize the first 2003fd1e8a1fSTejun Heo * chunk and prime the dynamic percpu allocator. 20048d408b4bSTejun Heo * 2005fd1e8a1fSTejun Heo * @ai->static_size is the size of static percpu area. 2006fd1e8a1fSTejun Heo * 2007fd1e8a1fSTejun Heo * @ai->reserved_size, if non-zero, specifies the amount of bytes to 2008edcb4639STejun Heo * reserve after the static area in the first chunk. This reserves 2009edcb4639STejun Heo * the first chunk such that it's available only through reserved 2010edcb4639STejun Heo * percpu allocation. This is primarily used to serve module percpu 2011edcb4639STejun Heo * static areas on architectures where the addressing model has 2012edcb4639STejun Heo * limited offset range for symbol relocations to guarantee module 2013edcb4639STejun Heo * percpu symbols fall inside the relocatable range. 2014edcb4639STejun Heo * 2015fd1e8a1fSTejun Heo * @ai->dyn_size determines the number of bytes available for dynamic 2016fd1e8a1fSTejun Heo * allocation in the first chunk. The area between @ai->static_size + 2017fd1e8a1fSTejun Heo * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused. 20186074d5b0STejun Heo * 2019fd1e8a1fSTejun Heo * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE 2020fd1e8a1fSTejun Heo * and equal to or larger than @ai->static_size + @ai->reserved_size + 2021fd1e8a1fSTejun Heo * @ai->dyn_size. 20228d408b4bSTejun Heo * 2023fd1e8a1fSTejun Heo * @ai->atom_size is the allocation atom size and used as alignment 2024fd1e8a1fSTejun Heo * for vm areas. 20258d408b4bSTejun Heo * 2026fd1e8a1fSTejun Heo * @ai->alloc_size is the allocation size and always multiple of 2027fd1e8a1fSTejun Heo * @ai->atom_size. This is larger than @ai->atom_size if 2028fd1e8a1fSTejun Heo * @ai->unit_size is larger than @ai->atom_size. 2029fd1e8a1fSTejun Heo * 2030fd1e8a1fSTejun Heo * @ai->nr_groups and @ai->groups describe virtual memory layout of 2031fd1e8a1fSTejun Heo * percpu areas. Units which should be colocated are put into the 2032fd1e8a1fSTejun Heo * same group. Dynamic VM areas will be allocated according to these 2033fd1e8a1fSTejun Heo * groupings. If @ai->nr_groups is zero, a single group containing 2034fd1e8a1fSTejun Heo * all units is assumed. 20358d408b4bSTejun Heo * 203638a6be52STejun Heo * The caller should have mapped the first chunk at @base_addr and 203738a6be52STejun Heo * copied static data to each unit. 2038fbf59bc9STejun Heo * 2039c0ebfdc3SDennis Zhou (Facebook) * The first chunk will always contain a static and a dynamic region. 2040c0ebfdc3SDennis Zhou (Facebook) * However, the static region is not managed by any chunk. If the first 2041c0ebfdc3SDennis Zhou (Facebook) * chunk also contains a reserved region, it is served by two chunks - 2042c0ebfdc3SDennis Zhou (Facebook) * one for the reserved region and one for the dynamic region. They 2043c0ebfdc3SDennis Zhou (Facebook) * share the same vm, but use offset regions in the area allocation map. 2044c0ebfdc3SDennis Zhou (Facebook) * The chunk serving the dynamic region is circulated in the chunk slots 2045c0ebfdc3SDennis Zhou (Facebook) * and available for dynamic allocation like any other chunk. 2046edcb4639STejun Heo * 2047fbf59bc9STejun Heo * RETURNS: 2048fb435d52STejun Heo * 0 on success, -errno on failure. 2049fbf59bc9STejun Heo */ 2050fb435d52STejun Heo int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai, 2051fd1e8a1fSTejun Heo void *base_addr) 2052fbf59bc9STejun Heo { 2053b9c39442SDennis Zhou (Facebook) size_t size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 2054d2f3c384SDennis Zhou (Facebook) size_t static_size, dyn_size; 20550c4169c3SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 20566563297cSTejun Heo unsigned long *group_offsets; 20576563297cSTejun Heo size_t *group_sizes; 2058fb435d52STejun Heo unsigned long *unit_off; 2059fbf59bc9STejun Heo unsigned int cpu; 2060fd1e8a1fSTejun Heo int *unit_map; 2061fd1e8a1fSTejun Heo int group, unit, i; 2062c0ebfdc3SDennis Zhou (Facebook) int map_size; 2063c0ebfdc3SDennis Zhou (Facebook) unsigned long tmp_addr; 2064*f655f405SMike Rapoport size_t alloc_size; 2065fbf59bc9STejun Heo 2066635b75fcSTejun Heo #define PCPU_SETUP_BUG_ON(cond) do { \ 2067635b75fcSTejun Heo if (unlikely(cond)) { \ 2068870d4b12SJoe Perches pr_emerg("failed to initialize, %s\n", #cond); \ 2069870d4b12SJoe Perches pr_emerg("cpu_possible_mask=%*pb\n", \ 2070807de073STejun Heo cpumask_pr_args(cpu_possible_mask)); \ 2071635b75fcSTejun Heo pcpu_dump_alloc_info(KERN_EMERG, ai); \ 2072635b75fcSTejun Heo BUG(); \ 2073635b75fcSTejun Heo } \ 2074635b75fcSTejun Heo } while (0) 2075635b75fcSTejun Heo 20762f39e637STejun Heo /* sanity checks */ 2077635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->nr_groups <= 0); 2078bbddff05STejun Heo #ifdef CONFIG_SMP 2079635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!ai->static_size); 2080f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(__per_cpu_start)); 2081bbddff05STejun Heo #endif 2082635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!base_addr); 2083f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(base_addr)); 2084635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < size_sum); 2085f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(ai->unit_size)); 2086635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE); 2087ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->unit_size, PCPU_BITMAP_BLOCK_SIZE)); 2088099a19d9STejun Heo PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE); 2089fb29a2ccSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!ai->dyn_size); 2090d2f3c384SDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->reserved_size, PCPU_MIN_ALLOC_SIZE)); 2091ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!(IS_ALIGNED(PCPU_BITMAP_BLOCK_SIZE, PAGE_SIZE) || 2092ca460b3cSDennis Zhou (Facebook) IS_ALIGNED(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE))); 20939f645532STejun Heo PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0); 20948d408b4bSTejun Heo 20956563297cSTejun Heo /* process group information and build config tables accordingly */ 2096*f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_offsets[0]); 2097*f655f405SMike Rapoport group_offsets = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2098*f655f405SMike Rapoport if (!group_offsets) 2099*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2100*f655f405SMike Rapoport alloc_size); 2101*f655f405SMike Rapoport 2102*f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_sizes[0]); 2103*f655f405SMike Rapoport group_sizes = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2104*f655f405SMike Rapoport if (!group_sizes) 2105*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2106*f655f405SMike Rapoport alloc_size); 2107*f655f405SMike Rapoport 2108*f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_map[0]); 2109*f655f405SMike Rapoport unit_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2110*f655f405SMike Rapoport if (!unit_map) 2111*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2112*f655f405SMike Rapoport alloc_size); 2113*f655f405SMike Rapoport 2114*f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_off[0]); 2115*f655f405SMike Rapoport unit_off = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2116*f655f405SMike Rapoport if (!unit_off) 2117*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2118*f655f405SMike Rapoport alloc_size); 21192f39e637STejun Heo 2120fd1e8a1fSTejun Heo for (cpu = 0; cpu < nr_cpu_ids; cpu++) 2121ffe0d5a5STejun Heo unit_map[cpu] = UINT_MAX; 2122a855b84cSTejun Heo 2123a855b84cSTejun Heo pcpu_low_unit_cpu = NR_CPUS; 2124a855b84cSTejun Heo pcpu_high_unit_cpu = NR_CPUS; 21252f39e637STejun Heo 2126fd1e8a1fSTejun Heo for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) { 2127fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 21282f39e637STejun Heo 21296563297cSTejun Heo group_offsets[group] = gi->base_offset; 21306563297cSTejun Heo group_sizes[group] = gi->nr_units * ai->unit_size; 21316563297cSTejun Heo 2132fd1e8a1fSTejun Heo for (i = 0; i < gi->nr_units; i++) { 2133fd1e8a1fSTejun Heo cpu = gi->cpu_map[i]; 2134fd1e8a1fSTejun Heo if (cpu == NR_CPUS) 2135fd1e8a1fSTejun Heo continue; 2136fd1e8a1fSTejun Heo 21379f295664SDan Carpenter PCPU_SETUP_BUG_ON(cpu >= nr_cpu_ids); 2138635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!cpu_possible(cpu)); 2139635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX); 2140fd1e8a1fSTejun Heo 2141fd1e8a1fSTejun Heo unit_map[cpu] = unit + i; 2142fb435d52STejun Heo unit_off[cpu] = gi->base_offset + i * ai->unit_size; 2143fb435d52STejun Heo 2144a855b84cSTejun Heo /* determine low/high unit_cpu */ 2145a855b84cSTejun Heo if (pcpu_low_unit_cpu == NR_CPUS || 2146a855b84cSTejun Heo unit_off[cpu] < unit_off[pcpu_low_unit_cpu]) 2147a855b84cSTejun Heo pcpu_low_unit_cpu = cpu; 2148a855b84cSTejun Heo if (pcpu_high_unit_cpu == NR_CPUS || 2149a855b84cSTejun Heo unit_off[cpu] > unit_off[pcpu_high_unit_cpu]) 2150a855b84cSTejun Heo pcpu_high_unit_cpu = cpu; 21510fc0531eSLinus Torvalds } 21520fc0531eSLinus Torvalds } 2153fd1e8a1fSTejun Heo pcpu_nr_units = unit; 21542f39e637STejun Heo 21552f39e637STejun Heo for_each_possible_cpu(cpu) 2156635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX); 2157635b75fcSTejun Heo 2158635b75fcSTejun Heo /* we're done parsing the input, undefine BUG macro and dump config */ 2159635b75fcSTejun Heo #undef PCPU_SETUP_BUG_ON 2160bcbea798STejun Heo pcpu_dump_alloc_info(KERN_DEBUG, ai); 21612f39e637STejun Heo 21626563297cSTejun Heo pcpu_nr_groups = ai->nr_groups; 21636563297cSTejun Heo pcpu_group_offsets = group_offsets; 21646563297cSTejun Heo pcpu_group_sizes = group_sizes; 2165fd1e8a1fSTejun Heo pcpu_unit_map = unit_map; 2166fb435d52STejun Heo pcpu_unit_offsets = unit_off; 21672f39e637STejun Heo 21682f39e637STejun Heo /* determine basic parameters */ 2169fd1e8a1fSTejun Heo pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT; 2170d9b55eebSTejun Heo pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; 21716563297cSTejun Heo pcpu_atom_size = ai->atom_size; 2172ce3141a2STejun Heo pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) + 2173ce3141a2STejun Heo BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long); 2174cafe8816STejun Heo 217530a5b536SDennis Zhou pcpu_stats_save_ai(ai); 217630a5b536SDennis Zhou 2177d9b55eebSTejun Heo /* 2178d9b55eebSTejun Heo * Allocate chunk slots. The additional last slot is for 2179d9b55eebSTejun Heo * empty chunks. 2180d9b55eebSTejun Heo */ 2181d9b55eebSTejun Heo pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2; 21827e1c4e27SMike Rapoport pcpu_slot = memblock_alloc(pcpu_nr_slots * sizeof(pcpu_slot[0]), 21837e1c4e27SMike Rapoport SMP_CACHE_BYTES); 2184*f655f405SMike Rapoport if (!pcpu_slot) 2185*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2186*f655f405SMike Rapoport pcpu_nr_slots * sizeof(pcpu_slot[0])); 2187fbf59bc9STejun Heo for (i = 0; i < pcpu_nr_slots; i++) 2188fbf59bc9STejun Heo INIT_LIST_HEAD(&pcpu_slot[i]); 2189fbf59bc9STejun Heo 2190edcb4639STejun Heo /* 2191d2f3c384SDennis Zhou (Facebook) * The end of the static region needs to be aligned with the 2192d2f3c384SDennis Zhou (Facebook) * minimum allocation size as this offsets the reserved and 2193d2f3c384SDennis Zhou (Facebook) * dynamic region. The first chunk ends page aligned by 2194d2f3c384SDennis Zhou (Facebook) * expanding the dynamic region, therefore the dynamic region 2195d2f3c384SDennis Zhou (Facebook) * can be shrunk to compensate while still staying above the 2196d2f3c384SDennis Zhou (Facebook) * configured sizes. 2197d2f3c384SDennis Zhou (Facebook) */ 2198d2f3c384SDennis Zhou (Facebook) static_size = ALIGN(ai->static_size, PCPU_MIN_ALLOC_SIZE); 2199d2f3c384SDennis Zhou (Facebook) dyn_size = ai->dyn_size - (static_size - ai->static_size); 2200d2f3c384SDennis Zhou (Facebook) 2201d2f3c384SDennis Zhou (Facebook) /* 2202c0ebfdc3SDennis Zhou (Facebook) * Initialize first chunk. 2203c0ebfdc3SDennis Zhou (Facebook) * If the reserved_size is non-zero, this initializes the reserved 2204c0ebfdc3SDennis Zhou (Facebook) * chunk. If the reserved_size is zero, the reserved chunk is NULL 2205c0ebfdc3SDennis Zhou (Facebook) * and the dynamic region is initialized here. The first chunk, 2206c0ebfdc3SDennis Zhou (Facebook) * pcpu_first_chunk, will always point to the chunk that serves 2207c0ebfdc3SDennis Zhou (Facebook) * the dynamic region. 2208edcb4639STejun Heo */ 2209d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size; 2210d2f3c384SDennis Zhou (Facebook) map_size = ai->reserved_size ?: dyn_size; 221140064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 221261ace7faSTejun Heo 2213edcb4639STejun Heo /* init dynamic chunk if necessary */ 2214b9c39442SDennis Zhou (Facebook) if (ai->reserved_size) { 22150c4169c3SDennis Zhou (Facebook) pcpu_reserved_chunk = chunk; 2216b9c39442SDennis Zhou (Facebook) 2217d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size + 2218c0ebfdc3SDennis Zhou (Facebook) ai->reserved_size; 2219d2f3c384SDennis Zhou (Facebook) map_size = dyn_size; 222040064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 2221edcb4639STejun Heo } 2222edcb4639STejun Heo 22232441d15cSTejun Heo /* link the first chunk in */ 22240c4169c3SDennis Zhou (Facebook) pcpu_first_chunk = chunk; 22250cecf50cSDennis Zhou (Facebook) pcpu_nr_empty_pop_pages = pcpu_first_chunk->nr_empty_pop_pages; 2226ae9e6bc9STejun Heo pcpu_chunk_relocate(pcpu_first_chunk, -1); 2227fbf59bc9STejun Heo 22287e8a6304SDennis Zhou (Facebook) /* include all regions of the first chunk */ 22297e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += PFN_DOWN(size_sum); 22307e8a6304SDennis Zhou (Facebook) 223130a5b536SDennis Zhou pcpu_stats_chunk_alloc(); 2232df95e795SDennis Zhou trace_percpu_create_chunk(base_addr); 223330a5b536SDennis Zhou 2234fbf59bc9STejun Heo /* we're done */ 2235bba174f5STejun Heo pcpu_base_addr = base_addr; 2236fb435d52STejun Heo return 0; 2237fbf59bc9STejun Heo } 223866c3a757STejun Heo 2239bbddff05STejun Heo #ifdef CONFIG_SMP 2240bbddff05STejun Heo 224117f3609cSAndi Kleen const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = { 2242f58dc01bSTejun Heo [PCPU_FC_AUTO] = "auto", 2243f58dc01bSTejun Heo [PCPU_FC_EMBED] = "embed", 2244f58dc01bSTejun Heo [PCPU_FC_PAGE] = "page", 2245f58dc01bSTejun Heo }; 224666c3a757STejun Heo 2247f58dc01bSTejun Heo enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO; 2248f58dc01bSTejun Heo 2249f58dc01bSTejun Heo static int __init percpu_alloc_setup(char *str) 225066c3a757STejun Heo { 22515479c78aSCyrill Gorcunov if (!str) 22525479c78aSCyrill Gorcunov return -EINVAL; 22535479c78aSCyrill Gorcunov 2254f58dc01bSTejun Heo if (0) 2255f58dc01bSTejun Heo /* nada */; 2256f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK 2257f58dc01bSTejun Heo else if (!strcmp(str, "embed")) 2258f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_EMBED; 2259f58dc01bSTejun Heo #endif 2260f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 2261f58dc01bSTejun Heo else if (!strcmp(str, "page")) 2262f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 2263f58dc01bSTejun Heo #endif 2264f58dc01bSTejun Heo else 2265870d4b12SJoe Perches pr_warn("unknown allocator %s specified\n", str); 226666c3a757STejun Heo 2267f58dc01bSTejun Heo return 0; 226866c3a757STejun Heo } 2269f58dc01bSTejun Heo early_param("percpu_alloc", percpu_alloc_setup); 227066c3a757STejun Heo 22713c9a024fSTejun Heo /* 22723c9a024fSTejun Heo * pcpu_embed_first_chunk() is used by the generic percpu setup. 22733c9a024fSTejun Heo * Build it if needed by the arch config or the generic setup is going 22743c9a024fSTejun Heo * to be used. 22753c9a024fSTejun Heo */ 227608fc4580STejun Heo #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \ 227708fc4580STejun Heo !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) 22783c9a024fSTejun Heo #define BUILD_EMBED_FIRST_CHUNK 22793c9a024fSTejun Heo #endif 22803c9a024fSTejun Heo 22813c9a024fSTejun Heo /* build pcpu_page_first_chunk() iff needed by the arch config */ 22823c9a024fSTejun Heo #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK) 22833c9a024fSTejun Heo #define BUILD_PAGE_FIRST_CHUNK 22843c9a024fSTejun Heo #endif 22853c9a024fSTejun Heo 22863c9a024fSTejun Heo /* pcpu_build_alloc_info() is used by both embed and page first chunk */ 22873c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK) 22883c9a024fSTejun Heo /** 2289fbf59bc9STejun Heo * pcpu_build_alloc_info - build alloc_info considering distances between CPUs 2290fbf59bc9STejun Heo * @reserved_size: the size of reserved percpu area in bytes 2291fbf59bc9STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 2292fbf59bc9STejun Heo * @atom_size: allocation atom size 2293fbf59bc9STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 2294fbf59bc9STejun Heo * 2295fbf59bc9STejun Heo * This function determines grouping of units, their mappings to cpus 2296fbf59bc9STejun Heo * and other parameters considering needed percpu size, allocation 2297fbf59bc9STejun Heo * atom size and distances between CPUs. 2298fbf59bc9STejun Heo * 2299bffc4375SYannick Guerrini * Groups are always multiples of atom size and CPUs which are of 2300fbf59bc9STejun Heo * LOCAL_DISTANCE both ways are grouped together and share space for 2301fbf59bc9STejun Heo * units in the same group. The returned configuration is guaranteed 2302fbf59bc9STejun Heo * to have CPUs on different nodes on different groups and >=75% usage 2303fbf59bc9STejun Heo * of allocated virtual address space. 2304fbf59bc9STejun Heo * 2305fbf59bc9STejun Heo * RETURNS: 2306fbf59bc9STejun Heo * On success, pointer to the new allocation_info is returned. On 2307fbf59bc9STejun Heo * failure, ERR_PTR value is returned. 2308fbf59bc9STejun Heo */ 2309fbf59bc9STejun Heo static struct pcpu_alloc_info * __init pcpu_build_alloc_info( 2310fbf59bc9STejun Heo size_t reserved_size, size_t dyn_size, 2311fbf59bc9STejun Heo size_t atom_size, 2312fbf59bc9STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn) 2313fbf59bc9STejun Heo { 2314fbf59bc9STejun Heo static int group_map[NR_CPUS] __initdata; 2315fbf59bc9STejun Heo static int group_cnt[NR_CPUS] __initdata; 2316fbf59bc9STejun Heo const size_t static_size = __per_cpu_end - __per_cpu_start; 2317fbf59bc9STejun Heo int nr_groups = 1, nr_units = 0; 2318fbf59bc9STejun Heo size_t size_sum, min_unit_size, alloc_size; 2319fbf59bc9STejun Heo int upa, max_upa, uninitialized_var(best_upa); /* units_per_alloc */ 2320fbf59bc9STejun Heo int last_allocs, group, unit; 2321fbf59bc9STejun Heo unsigned int cpu, tcpu; 2322fbf59bc9STejun Heo struct pcpu_alloc_info *ai; 2323fbf59bc9STejun Heo unsigned int *cpu_map; 2324fbf59bc9STejun Heo 2325fbf59bc9STejun Heo /* this function may be called multiple times */ 2326fbf59bc9STejun Heo memset(group_map, 0, sizeof(group_map)); 2327fbf59bc9STejun Heo memset(group_cnt, 0, sizeof(group_cnt)); 2328fbf59bc9STejun Heo 2329fbf59bc9STejun Heo /* calculate size_sum and ensure dyn_size is enough for early alloc */ 2330fbf59bc9STejun Heo size_sum = PFN_ALIGN(static_size + reserved_size + 2331fbf59bc9STejun Heo max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE)); 2332fbf59bc9STejun Heo dyn_size = size_sum - static_size - reserved_size; 2333fbf59bc9STejun Heo 2334fbf59bc9STejun Heo /* 2335fbf59bc9STejun Heo * Determine min_unit_size, alloc_size and max_upa such that 2336fbf59bc9STejun Heo * alloc_size is multiple of atom_size and is the smallest 233725985edcSLucas De Marchi * which can accommodate 4k aligned segments which are equal to 2338fbf59bc9STejun Heo * or larger than min_unit_size. 2339fbf59bc9STejun Heo */ 2340fbf59bc9STejun Heo min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); 2341fbf59bc9STejun Heo 23429c015162SDennis Zhou (Facebook) /* determine the maximum # of units that can fit in an allocation */ 2343fbf59bc9STejun Heo alloc_size = roundup(min_unit_size, atom_size); 2344fbf59bc9STejun Heo upa = alloc_size / min_unit_size; 2345f09f1243SAlexander Kuleshov while (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2346fbf59bc9STejun Heo upa--; 2347fbf59bc9STejun Heo max_upa = upa; 2348fbf59bc9STejun Heo 2349fbf59bc9STejun Heo /* group cpus according to their proximity */ 2350fbf59bc9STejun Heo for_each_possible_cpu(cpu) { 2351fbf59bc9STejun Heo group = 0; 2352fbf59bc9STejun Heo next_group: 2353fbf59bc9STejun Heo for_each_possible_cpu(tcpu) { 2354fbf59bc9STejun Heo if (cpu == tcpu) 2355fbf59bc9STejun Heo break; 2356fbf59bc9STejun Heo if (group_map[tcpu] == group && cpu_distance_fn && 2357fbf59bc9STejun Heo (cpu_distance_fn(cpu, tcpu) > LOCAL_DISTANCE || 2358fbf59bc9STejun Heo cpu_distance_fn(tcpu, cpu) > LOCAL_DISTANCE)) { 2359fbf59bc9STejun Heo group++; 2360fbf59bc9STejun Heo nr_groups = max(nr_groups, group + 1); 2361fbf59bc9STejun Heo goto next_group; 2362fbf59bc9STejun Heo } 2363fbf59bc9STejun Heo } 2364fbf59bc9STejun Heo group_map[cpu] = group; 2365fbf59bc9STejun Heo group_cnt[group]++; 2366fbf59bc9STejun Heo } 2367fbf59bc9STejun Heo 2368fbf59bc9STejun Heo /* 23699c015162SDennis Zhou (Facebook) * Wasted space is caused by a ratio imbalance of upa to group_cnt. 23709c015162SDennis Zhou (Facebook) * Expand the unit_size until we use >= 75% of the units allocated. 23719c015162SDennis Zhou (Facebook) * Related to atom_size, which could be much larger than the unit_size. 2372fbf59bc9STejun Heo */ 2373fbf59bc9STejun Heo last_allocs = INT_MAX; 2374fbf59bc9STejun Heo for (upa = max_upa; upa; upa--) { 2375fbf59bc9STejun Heo int allocs = 0, wasted = 0; 2376fbf59bc9STejun Heo 2377f09f1243SAlexander Kuleshov if (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2378fbf59bc9STejun Heo continue; 2379fbf59bc9STejun Heo 2380fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2381fbf59bc9STejun Heo int this_allocs = DIV_ROUND_UP(group_cnt[group], upa); 2382fbf59bc9STejun Heo allocs += this_allocs; 2383fbf59bc9STejun Heo wasted += this_allocs * upa - group_cnt[group]; 2384fbf59bc9STejun Heo } 2385fbf59bc9STejun Heo 2386fbf59bc9STejun Heo /* 2387fbf59bc9STejun Heo * Don't accept if wastage is over 1/3. The 2388fbf59bc9STejun Heo * greater-than comparison ensures upa==1 always 2389fbf59bc9STejun Heo * passes the following check. 2390fbf59bc9STejun Heo */ 2391fbf59bc9STejun Heo if (wasted > num_possible_cpus() / 3) 2392fbf59bc9STejun Heo continue; 2393fbf59bc9STejun Heo 2394fbf59bc9STejun Heo /* and then don't consume more memory */ 2395fbf59bc9STejun Heo if (allocs > last_allocs) 2396fbf59bc9STejun Heo break; 2397fbf59bc9STejun Heo last_allocs = allocs; 2398fbf59bc9STejun Heo best_upa = upa; 2399fbf59bc9STejun Heo } 2400fbf59bc9STejun Heo upa = best_upa; 2401fbf59bc9STejun Heo 2402fbf59bc9STejun Heo /* allocate and fill alloc_info */ 2403fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) 2404fbf59bc9STejun Heo nr_units += roundup(group_cnt[group], upa); 2405fbf59bc9STejun Heo 2406fbf59bc9STejun Heo ai = pcpu_alloc_alloc_info(nr_groups, nr_units); 2407fbf59bc9STejun Heo if (!ai) 2408fbf59bc9STejun Heo return ERR_PTR(-ENOMEM); 2409fbf59bc9STejun Heo cpu_map = ai->groups[0].cpu_map; 2410fbf59bc9STejun Heo 2411fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2412fbf59bc9STejun Heo ai->groups[group].cpu_map = cpu_map; 2413fbf59bc9STejun Heo cpu_map += roundup(group_cnt[group], upa); 2414fbf59bc9STejun Heo } 2415fbf59bc9STejun Heo 2416fbf59bc9STejun Heo ai->static_size = static_size; 2417fbf59bc9STejun Heo ai->reserved_size = reserved_size; 2418fbf59bc9STejun Heo ai->dyn_size = dyn_size; 2419fbf59bc9STejun Heo ai->unit_size = alloc_size / upa; 2420fbf59bc9STejun Heo ai->atom_size = atom_size; 2421fbf59bc9STejun Heo ai->alloc_size = alloc_size; 2422fbf59bc9STejun Heo 24232de7852fSPeng Fan for (group = 0, unit = 0; group < nr_groups; group++) { 2424fbf59bc9STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 2425fbf59bc9STejun Heo 2426fbf59bc9STejun Heo /* 2427fbf59bc9STejun Heo * Initialize base_offset as if all groups are located 2428fbf59bc9STejun Heo * back-to-back. The caller should update this to 2429fbf59bc9STejun Heo * reflect actual allocation. 2430fbf59bc9STejun Heo */ 2431fbf59bc9STejun Heo gi->base_offset = unit * ai->unit_size; 2432fbf59bc9STejun Heo 2433fbf59bc9STejun Heo for_each_possible_cpu(cpu) 2434fbf59bc9STejun Heo if (group_map[cpu] == group) 2435fbf59bc9STejun Heo gi->cpu_map[gi->nr_units++] = cpu; 2436fbf59bc9STejun Heo gi->nr_units = roundup(gi->nr_units, upa); 2437fbf59bc9STejun Heo unit += gi->nr_units; 2438fbf59bc9STejun Heo } 2439fbf59bc9STejun Heo BUG_ON(unit != nr_units); 2440fbf59bc9STejun Heo 2441fbf59bc9STejun Heo return ai; 2442fbf59bc9STejun Heo } 24433c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */ 2444fbf59bc9STejun Heo 24453c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) 244666c3a757STejun Heo /** 244766c3a757STejun Heo * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem 244866c3a757STejun Heo * @reserved_size: the size of reserved percpu area in bytes 24494ba6ce25STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 2450c8826dd5STejun Heo * @atom_size: allocation atom size 2451c8826dd5STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 2452c8826dd5STejun Heo * @alloc_fn: function to allocate percpu page 245325985edcSLucas De Marchi * @free_fn: function to free percpu page 245466c3a757STejun Heo * 245566c3a757STejun Heo * This is a helper to ease setting up embedded first percpu chunk and 245666c3a757STejun Heo * can be called where pcpu_setup_first_chunk() is expected. 245766c3a757STejun Heo * 245866c3a757STejun Heo * If this function is used to setup the first chunk, it is allocated 2459c8826dd5STejun Heo * by calling @alloc_fn and used as-is without being mapped into 2460c8826dd5STejun Heo * vmalloc area. Allocations are always whole multiples of @atom_size 2461c8826dd5STejun Heo * aligned to @atom_size. 2462c8826dd5STejun Heo * 2463c8826dd5STejun Heo * This enables the first chunk to piggy back on the linear physical 2464c8826dd5STejun Heo * mapping which often uses larger page size. Please note that this 2465c8826dd5STejun Heo * can result in very sparse cpu->unit mapping on NUMA machines thus 2466c8826dd5STejun Heo * requiring large vmalloc address space. Don't use this allocator if 2467c8826dd5STejun Heo * vmalloc space is not orders of magnitude larger than distances 2468c8826dd5STejun Heo * between node memory addresses (ie. 32bit NUMA machines). 246966c3a757STejun Heo * 24704ba6ce25STejun Heo * @dyn_size specifies the minimum dynamic area size. 247166c3a757STejun Heo * 247266c3a757STejun Heo * If the needed size is smaller than the minimum or specified unit 2473c8826dd5STejun Heo * size, the leftover is returned using @free_fn. 247466c3a757STejun Heo * 247566c3a757STejun Heo * RETURNS: 2476fb435d52STejun Heo * 0 on success, -errno on failure. 247766c3a757STejun Heo */ 24784ba6ce25STejun Heo int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size, 2479c8826dd5STejun Heo size_t atom_size, 2480c8826dd5STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn, 2481c8826dd5STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 2482c8826dd5STejun Heo pcpu_fc_free_fn_t free_fn) 248366c3a757STejun Heo { 2484c8826dd5STejun Heo void *base = (void *)ULONG_MAX; 2485c8826dd5STejun Heo void **areas = NULL; 2486fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 248793c76b6bSzijun_hu size_t size_sum, areas_size; 248893c76b6bSzijun_hu unsigned long max_distance; 24899b739662Szijun_hu int group, i, highest_group, rc; 249066c3a757STejun Heo 2491c8826dd5STejun Heo ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size, 2492c8826dd5STejun Heo cpu_distance_fn); 2493fd1e8a1fSTejun Heo if (IS_ERR(ai)) 2494fd1e8a1fSTejun Heo return PTR_ERR(ai); 249566c3a757STejun Heo 2496fd1e8a1fSTejun Heo size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 2497c8826dd5STejun Heo areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *)); 249866c3a757STejun Heo 24997e1c4e27SMike Rapoport areas = memblock_alloc_nopanic(areas_size, SMP_CACHE_BYTES); 2500c8826dd5STejun Heo if (!areas) { 2501fb435d52STejun Heo rc = -ENOMEM; 2502c8826dd5STejun Heo goto out_free; 2503fa8a7094STejun Heo } 250466c3a757STejun Heo 25059b739662Szijun_hu /* allocate, copy and determine base address & max_distance */ 25069b739662Szijun_hu highest_group = 0; 2507c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) { 2508c8826dd5STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 2509c8826dd5STejun Heo unsigned int cpu = NR_CPUS; 2510c8826dd5STejun Heo void *ptr; 251166c3a757STejun Heo 2512c8826dd5STejun Heo for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++) 2513c8826dd5STejun Heo cpu = gi->cpu_map[i]; 2514c8826dd5STejun Heo BUG_ON(cpu == NR_CPUS); 2515c8826dd5STejun Heo 2516c8826dd5STejun Heo /* allocate space for the whole group */ 2517c8826dd5STejun Heo ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size); 2518c8826dd5STejun Heo if (!ptr) { 2519c8826dd5STejun Heo rc = -ENOMEM; 2520c8826dd5STejun Heo goto out_free_areas; 2521c8826dd5STejun Heo } 2522f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 2523f528f0b8SCatalin Marinas kmemleak_free(ptr); 2524c8826dd5STejun Heo areas[group] = ptr; 2525c8826dd5STejun Heo 2526c8826dd5STejun Heo base = min(ptr, base); 25279b739662Szijun_hu if (ptr > areas[highest_group]) 25289b739662Szijun_hu highest_group = group; 25299b739662Szijun_hu } 25309b739662Szijun_hu max_distance = areas[highest_group] - base; 25319b739662Szijun_hu max_distance += ai->unit_size * ai->groups[highest_group].nr_units; 25329b739662Szijun_hu 25339b739662Szijun_hu /* warn if maximum distance is further than 75% of vmalloc space */ 25349b739662Szijun_hu if (max_distance > VMALLOC_TOTAL * 3 / 4) { 25359b739662Szijun_hu pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n", 25369b739662Szijun_hu max_distance, VMALLOC_TOTAL); 25379b739662Szijun_hu #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 25389b739662Szijun_hu /* and fail if we have fallback */ 25399b739662Szijun_hu rc = -EINVAL; 25409b739662Szijun_hu goto out_free_areas; 25419b739662Szijun_hu #endif 254242b64281STejun Heo } 254342b64281STejun Heo 254442b64281STejun Heo /* 254542b64281STejun Heo * Copy data and free unused parts. This should happen after all 254642b64281STejun Heo * allocations are complete; otherwise, we may end up with 254742b64281STejun Heo * overlapping groups. 254842b64281STejun Heo */ 254942b64281STejun Heo for (group = 0; group < ai->nr_groups; group++) { 255042b64281STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 255142b64281STejun Heo void *ptr = areas[group]; 2552c8826dd5STejun Heo 2553c8826dd5STejun Heo for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) { 2554c8826dd5STejun Heo if (gi->cpu_map[i] == NR_CPUS) { 2555c8826dd5STejun Heo /* unused unit, free whole */ 2556c8826dd5STejun Heo free_fn(ptr, ai->unit_size); 2557c8826dd5STejun Heo continue; 2558c8826dd5STejun Heo } 2559c8826dd5STejun Heo /* copy and return the unused part */ 2560fd1e8a1fSTejun Heo memcpy(ptr, __per_cpu_load, ai->static_size); 2561c8826dd5STejun Heo free_fn(ptr + size_sum, ai->unit_size - size_sum); 2562c8826dd5STejun Heo } 256366c3a757STejun Heo } 256466c3a757STejun Heo 2565c8826dd5STejun Heo /* base address is now known, determine group base offsets */ 25666ea529a2STejun Heo for (group = 0; group < ai->nr_groups; group++) { 2567c8826dd5STejun Heo ai->groups[group].base_offset = areas[group] - base; 25686ea529a2STejun Heo } 2569c8826dd5STejun Heo 2570870d4b12SJoe Perches pr_info("Embedded %zu pages/cpu @%p s%zu r%zu d%zu u%zu\n", 2571fd1e8a1fSTejun Heo PFN_DOWN(size_sum), base, ai->static_size, ai->reserved_size, 2572fd1e8a1fSTejun Heo ai->dyn_size, ai->unit_size); 257366c3a757STejun Heo 2574fb435d52STejun Heo rc = pcpu_setup_first_chunk(ai, base); 2575c8826dd5STejun Heo goto out_free; 2576c8826dd5STejun Heo 2577c8826dd5STejun Heo out_free_areas: 2578c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) 2579f851c8d8SMichael Holzheu if (areas[group]) 2580c8826dd5STejun Heo free_fn(areas[group], 2581c8826dd5STejun Heo ai->groups[group].nr_units * ai->unit_size); 2582c8826dd5STejun Heo out_free: 2583fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 2584c8826dd5STejun Heo if (areas) 2585999c17e3SSantosh Shilimkar memblock_free_early(__pa(areas), areas_size); 2586fb435d52STejun Heo return rc; 2587d4b95f80STejun Heo } 25883c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK */ 2589d4b95f80STejun Heo 25903c9a024fSTejun Heo #ifdef BUILD_PAGE_FIRST_CHUNK 2591d4b95f80STejun Heo /** 259200ae4064STejun Heo * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages 2593d4b95f80STejun Heo * @reserved_size: the size of reserved percpu area in bytes 2594d4b95f80STejun Heo * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE 259525985edcSLucas De Marchi * @free_fn: function to free percpu page, always called with PAGE_SIZE 2596d4b95f80STejun Heo * @populate_pte_fn: function to populate pte 2597d4b95f80STejun Heo * 259800ae4064STejun Heo * This is a helper to ease setting up page-remapped first percpu 259900ae4064STejun Heo * chunk and can be called where pcpu_setup_first_chunk() is expected. 2600d4b95f80STejun Heo * 2601d4b95f80STejun Heo * This is the basic allocator. Static percpu area is allocated 2602d4b95f80STejun Heo * page-by-page into vmalloc area. 2603d4b95f80STejun Heo * 2604d4b95f80STejun Heo * RETURNS: 2605fb435d52STejun Heo * 0 on success, -errno on failure. 2606d4b95f80STejun Heo */ 2607fb435d52STejun Heo int __init pcpu_page_first_chunk(size_t reserved_size, 2608d4b95f80STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 2609d4b95f80STejun Heo pcpu_fc_free_fn_t free_fn, 2610d4b95f80STejun Heo pcpu_fc_populate_pte_fn_t populate_pte_fn) 2611d4b95f80STejun Heo { 26128f05a6a6STejun Heo static struct vm_struct vm; 2613fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 261400ae4064STejun Heo char psize_str[16]; 2615ce3141a2STejun Heo int unit_pages; 2616d4b95f80STejun Heo size_t pages_size; 2617ce3141a2STejun Heo struct page **pages; 2618fb435d52STejun Heo int unit, i, j, rc; 26198f606604Szijun_hu int upa; 26208f606604Szijun_hu int nr_g0_units; 2621d4b95f80STejun Heo 262200ae4064STejun Heo snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10); 262300ae4064STejun Heo 26244ba6ce25STejun Heo ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL); 2625fd1e8a1fSTejun Heo if (IS_ERR(ai)) 2626fd1e8a1fSTejun Heo return PTR_ERR(ai); 2627fd1e8a1fSTejun Heo BUG_ON(ai->nr_groups != 1); 26288f606604Szijun_hu upa = ai->alloc_size/ai->unit_size; 26298f606604Szijun_hu nr_g0_units = roundup(num_possible_cpus(), upa); 26300b59c25fSIgor Stoppa if (WARN_ON(ai->groups[0].nr_units != nr_g0_units)) { 26318f606604Szijun_hu pcpu_free_alloc_info(ai); 26328f606604Szijun_hu return -EINVAL; 26338f606604Szijun_hu } 2634fd1e8a1fSTejun Heo 2635fd1e8a1fSTejun Heo unit_pages = ai->unit_size >> PAGE_SHIFT; 2636d4b95f80STejun Heo 2637d4b95f80STejun Heo /* unaligned allocations can't be freed, round up to page size */ 2638fd1e8a1fSTejun Heo pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() * 2639fd1e8a1fSTejun Heo sizeof(pages[0])); 26407e1c4e27SMike Rapoport pages = memblock_alloc(pages_size, SMP_CACHE_BYTES); 2641*f655f405SMike Rapoport if (!pages) 2642*f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2643*f655f405SMike Rapoport pages_size); 2644d4b95f80STejun Heo 26458f05a6a6STejun Heo /* allocate pages */ 2646d4b95f80STejun Heo j = 0; 26478f606604Szijun_hu for (unit = 0; unit < num_possible_cpus(); unit++) { 2648fd1e8a1fSTejun Heo unsigned int cpu = ai->groups[0].cpu_map[unit]; 26498f606604Szijun_hu for (i = 0; i < unit_pages; i++) { 2650d4b95f80STejun Heo void *ptr; 2651d4b95f80STejun Heo 26523cbc8565STejun Heo ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE); 2653d4b95f80STejun Heo if (!ptr) { 2654870d4b12SJoe Perches pr_warn("failed to allocate %s page for cpu%u\n", 2655598d8091SJoe Perches psize_str, cpu); 2656d4b95f80STejun Heo goto enomem; 2657d4b95f80STejun Heo } 2658f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 2659f528f0b8SCatalin Marinas kmemleak_free(ptr); 2660ce3141a2STejun Heo pages[j++] = virt_to_page(ptr); 2661d4b95f80STejun Heo } 26628f606604Szijun_hu } 2663d4b95f80STejun Heo 26648f05a6a6STejun Heo /* allocate vm area, map the pages and copy static data */ 26658f05a6a6STejun Heo vm.flags = VM_ALLOC; 2666fd1e8a1fSTejun Heo vm.size = num_possible_cpus() * ai->unit_size; 26678f05a6a6STejun Heo vm_area_register_early(&vm, PAGE_SIZE); 26688f05a6a6STejun Heo 2669fd1e8a1fSTejun Heo for (unit = 0; unit < num_possible_cpus(); unit++) { 26701d9d3257STejun Heo unsigned long unit_addr = 2671fd1e8a1fSTejun Heo (unsigned long)vm.addr + unit * ai->unit_size; 26728f05a6a6STejun Heo 2673ce3141a2STejun Heo for (i = 0; i < unit_pages; i++) 26748f05a6a6STejun Heo populate_pte_fn(unit_addr + (i << PAGE_SHIFT)); 26758f05a6a6STejun Heo 26768f05a6a6STejun Heo /* pte already populated, the following shouldn't fail */ 2677fb435d52STejun Heo rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages], 2678ce3141a2STejun Heo unit_pages); 2679fb435d52STejun Heo if (rc < 0) 2680fb435d52STejun Heo panic("failed to map percpu area, err=%d\n", rc); 26818f05a6a6STejun Heo 26828f05a6a6STejun Heo /* 26838f05a6a6STejun Heo * FIXME: Archs with virtual cache should flush local 26848f05a6a6STejun Heo * cache for the linear mapping here - something 26858f05a6a6STejun Heo * equivalent to flush_cache_vmap() on the local cpu. 26868f05a6a6STejun Heo * flush_cache_vmap() can't be used as most supporting 26878f05a6a6STejun Heo * data structures are not set up yet. 26888f05a6a6STejun Heo */ 26898f05a6a6STejun Heo 26908f05a6a6STejun Heo /* copy static data */ 2691fd1e8a1fSTejun Heo memcpy((void *)unit_addr, __per_cpu_load, ai->static_size); 269266c3a757STejun Heo } 269366c3a757STejun Heo 269466c3a757STejun Heo /* we're ready, commit */ 2695870d4b12SJoe Perches pr_info("%d %s pages/cpu @%p s%zu r%zu d%zu\n", 2696fd1e8a1fSTejun Heo unit_pages, psize_str, vm.addr, ai->static_size, 2697fd1e8a1fSTejun Heo ai->reserved_size, ai->dyn_size); 269866c3a757STejun Heo 2699fb435d52STejun Heo rc = pcpu_setup_first_chunk(ai, vm.addr); 2700d4b95f80STejun Heo goto out_free_ar; 2701d4b95f80STejun Heo 2702d4b95f80STejun Heo enomem: 2703d4b95f80STejun Heo while (--j >= 0) 2704ce3141a2STejun Heo free_fn(page_address(pages[j]), PAGE_SIZE); 2705fb435d52STejun Heo rc = -ENOMEM; 2706d4b95f80STejun Heo out_free_ar: 2707999c17e3SSantosh Shilimkar memblock_free_early(__pa(pages), pages_size); 2708fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 2709fb435d52STejun Heo return rc; 271066c3a757STejun Heo } 27113c9a024fSTejun Heo #endif /* BUILD_PAGE_FIRST_CHUNK */ 2712d4b95f80STejun Heo 2713bbddff05STejun Heo #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA 27148c4bfc6eSTejun Heo /* 2715bbddff05STejun Heo * Generic SMP percpu area setup. 2716e74e3962STejun Heo * 2717e74e3962STejun Heo * The embedding helper is used because its behavior closely resembles 2718e74e3962STejun Heo * the original non-dynamic generic percpu area setup. This is 2719e74e3962STejun Heo * important because many archs have addressing restrictions and might 2720e74e3962STejun Heo * fail if the percpu area is located far away from the previous 2721e74e3962STejun Heo * location. As an added bonus, in non-NUMA cases, embedding is 2722e74e3962STejun Heo * generally a good idea TLB-wise because percpu area can piggy back 2723e74e3962STejun Heo * on the physical linear memory mapping which uses large page 2724e74e3962STejun Heo * mappings on applicable archs. 2725e74e3962STejun Heo */ 2726e74e3962STejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; 2727e74e3962STejun Heo EXPORT_SYMBOL(__per_cpu_offset); 2728e74e3962STejun Heo 2729c8826dd5STejun Heo static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size, 2730c8826dd5STejun Heo size_t align) 2731c8826dd5STejun Heo { 2732eb31d559SMike Rapoport return memblock_alloc_from_nopanic( 2733999c17e3SSantosh Shilimkar size, align, __pa(MAX_DMA_ADDRESS)); 2734c8826dd5STejun Heo } 2735c8826dd5STejun Heo 2736c8826dd5STejun Heo static void __init pcpu_dfl_fc_free(void *ptr, size_t size) 2737c8826dd5STejun Heo { 2738999c17e3SSantosh Shilimkar memblock_free_early(__pa(ptr), size); 2739c8826dd5STejun Heo } 2740c8826dd5STejun Heo 2741e74e3962STejun Heo void __init setup_per_cpu_areas(void) 2742e74e3962STejun Heo { 2743e74e3962STejun Heo unsigned long delta; 2744e74e3962STejun Heo unsigned int cpu; 2745fb435d52STejun Heo int rc; 2746e74e3962STejun Heo 2747e74e3962STejun Heo /* 2748e74e3962STejun Heo * Always reserve area for module percpu variables. That's 2749e74e3962STejun Heo * what the legacy allocator did. 2750e74e3962STejun Heo */ 2751fb435d52STejun Heo rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, 2752c8826dd5STejun Heo PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL, 2753c8826dd5STejun Heo pcpu_dfl_fc_alloc, pcpu_dfl_fc_free); 2754fb435d52STejun Heo if (rc < 0) 2755bbddff05STejun Heo panic("Failed to initialize percpu areas."); 2756e74e3962STejun Heo 2757e74e3962STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 2758e74e3962STejun Heo for_each_possible_cpu(cpu) 2759fb435d52STejun Heo __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; 2760e74e3962STejun Heo } 2761e74e3962STejun Heo #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */ 2762099a19d9STejun Heo 2763bbddff05STejun Heo #else /* CONFIG_SMP */ 2764bbddff05STejun Heo 2765bbddff05STejun Heo /* 2766bbddff05STejun Heo * UP percpu area setup. 2767bbddff05STejun Heo * 2768bbddff05STejun Heo * UP always uses km-based percpu allocator with identity mapping. 2769bbddff05STejun Heo * Static percpu variables are indistinguishable from the usual static 2770bbddff05STejun Heo * variables and don't require any special preparation. 2771bbddff05STejun Heo */ 2772bbddff05STejun Heo void __init setup_per_cpu_areas(void) 2773bbddff05STejun Heo { 2774bbddff05STejun Heo const size_t unit_size = 2775bbddff05STejun Heo roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE, 2776bbddff05STejun Heo PERCPU_DYNAMIC_RESERVE)); 2777bbddff05STejun Heo struct pcpu_alloc_info *ai; 2778bbddff05STejun Heo void *fc; 2779bbddff05STejun Heo 2780bbddff05STejun Heo ai = pcpu_alloc_alloc_info(1, 1); 2781eb31d559SMike Rapoport fc = memblock_alloc_from_nopanic(unit_size, 2782999c17e3SSantosh Shilimkar PAGE_SIZE, 2783999c17e3SSantosh Shilimkar __pa(MAX_DMA_ADDRESS)); 2784bbddff05STejun Heo if (!ai || !fc) 2785bbddff05STejun Heo panic("Failed to allocate memory for percpu areas."); 2786100d13c3SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 2787100d13c3SCatalin Marinas kmemleak_free(fc); 2788bbddff05STejun Heo 2789bbddff05STejun Heo ai->dyn_size = unit_size; 2790bbddff05STejun Heo ai->unit_size = unit_size; 2791bbddff05STejun Heo ai->atom_size = unit_size; 2792bbddff05STejun Heo ai->alloc_size = unit_size; 2793bbddff05STejun Heo ai->groups[0].nr_units = 1; 2794bbddff05STejun Heo ai->groups[0].cpu_map[0] = 0; 2795bbddff05STejun Heo 2796bbddff05STejun Heo if (pcpu_setup_first_chunk(ai, fc) < 0) 2797bbddff05STejun Heo panic("Failed to initialize percpu areas."); 2798438a5061SNicolas Pitre pcpu_free_alloc_info(ai); 2799bbddff05STejun Heo } 2800bbddff05STejun Heo 2801bbddff05STejun Heo #endif /* CONFIG_SMP */ 2802bbddff05STejun Heo 2803099a19d9STejun Heo /* 28047e8a6304SDennis Zhou (Facebook) * pcpu_nr_pages - calculate total number of populated backing pages 28057e8a6304SDennis Zhou (Facebook) * 28067e8a6304SDennis Zhou (Facebook) * This reflects the number of pages populated to back chunks. Metadata is 28077e8a6304SDennis Zhou (Facebook) * excluded in the number exposed in meminfo as the number of backing pages 28087e8a6304SDennis Zhou (Facebook) * scales with the number of cpus and can quickly outweigh the memory used for 28097e8a6304SDennis Zhou (Facebook) * metadata. It also keeps this calculation nice and simple. 28107e8a6304SDennis Zhou (Facebook) * 28117e8a6304SDennis Zhou (Facebook) * RETURNS: 28127e8a6304SDennis Zhou (Facebook) * Total number of populated backing pages in use by the allocator. 28137e8a6304SDennis Zhou (Facebook) */ 28147e8a6304SDennis Zhou (Facebook) unsigned long pcpu_nr_pages(void) 28157e8a6304SDennis Zhou (Facebook) { 28167e8a6304SDennis Zhou (Facebook) return pcpu_nr_populated * pcpu_nr_units; 28177e8a6304SDennis Zhou (Facebook) } 28187e8a6304SDennis Zhou (Facebook) 28197e8a6304SDennis Zhou (Facebook) /* 28201a4d7607STejun Heo * Percpu allocator is initialized early during boot when neither slab or 28211a4d7607STejun Heo * workqueue is available. Plug async management until everything is up 28221a4d7607STejun Heo * and running. 28231a4d7607STejun Heo */ 28241a4d7607STejun Heo static int __init percpu_enable_async(void) 28251a4d7607STejun Heo { 28261a4d7607STejun Heo pcpu_async_enabled = true; 28271a4d7607STejun Heo return 0; 28281a4d7607STejun Heo } 28291a4d7607STejun Heo subsys_initcall(percpu_enable_async); 2830