155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2fbf59bc9STejun Heo /* 388999a89STejun Heo * mm/percpu.c - percpu memory allocator 4fbf59bc9STejun Heo * 5fbf59bc9STejun Heo * Copyright (C) 2009 SUSE Linux Products GmbH 6fbf59bc9STejun Heo * Copyright (C) 2009 Tejun Heo <tj@kernel.org> 7fbf59bc9STejun Heo * 85e81ee3eSDennis Zhou (Facebook) * Copyright (C) 2017 Facebook Inc. 9bfacd38fSDennis Zhou * Copyright (C) 2017 Dennis Zhou <dennis@kernel.org> 105e81ee3eSDennis Zhou (Facebook) * 119c015162SDennis Zhou (Facebook) * The percpu allocator handles both static and dynamic areas. Percpu 129c015162SDennis Zhou (Facebook) * areas are allocated in chunks which are divided into units. There is 139c015162SDennis Zhou (Facebook) * a 1-to-1 mapping for units to possible cpus. These units are grouped 149c015162SDennis Zhou (Facebook) * based on NUMA properties of the machine. 15fbf59bc9STejun Heo * 16fbf59bc9STejun Heo * c0 c1 c2 17fbf59bc9STejun Heo * ------------------- ------------------- ------------ 18fbf59bc9STejun Heo * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u 19fbf59bc9STejun Heo * ------------------- ...... ------------------- .... ------------ 20fbf59bc9STejun Heo * 219c015162SDennis Zhou (Facebook) * Allocation is done by offsets into a unit's address space. Ie., an 229c015162SDennis Zhou (Facebook) * area of 512 bytes at 6k in c1 occupies 512 bytes at 6k in c1:u0, 239c015162SDennis Zhou (Facebook) * c1:u1, c1:u2, etc. On NUMA machines, the mapping may be non-linear 249c015162SDennis Zhou (Facebook) * and even sparse. Access is handled by configuring percpu base 259c015162SDennis Zhou (Facebook) * registers according to the cpu to unit mappings and offsetting the 269c015162SDennis Zhou (Facebook) * base address using pcpu_unit_size. 27fbf59bc9STejun Heo * 289c015162SDennis Zhou (Facebook) * There is special consideration for the first chunk which must handle 299c015162SDennis Zhou (Facebook) * the static percpu variables in the kernel image as allocation services 305e81ee3eSDennis Zhou (Facebook) * are not online yet. In short, the first chunk is structured like so: 319c015162SDennis Zhou (Facebook) * 329c015162SDennis Zhou (Facebook) * <Static | [Reserved] | Dynamic> 339c015162SDennis Zhou (Facebook) * 349c015162SDennis Zhou (Facebook) * The static data is copied from the original section managed by the 359c015162SDennis Zhou (Facebook) * linker. The reserved section, if non-zero, primarily manages static 369c015162SDennis Zhou (Facebook) * percpu variables from kernel modules. Finally, the dynamic section 379c015162SDennis Zhou (Facebook) * takes care of normal allocations. 38fbf59bc9STejun Heo * 395e81ee3eSDennis Zhou (Facebook) * The allocator organizes chunks into lists according to free size and 403c7be18aSRoman Gushchin * memcg-awareness. To make a percpu allocation memcg-aware the __GFP_ACCOUNT 413c7be18aSRoman Gushchin * flag should be passed. All memcg-aware allocations are sharing one set 423c7be18aSRoman Gushchin * of chunks and all unaccounted allocations and allocations performed 433c7be18aSRoman Gushchin * by processes belonging to the root memory cgroup are using the second set. 443c7be18aSRoman Gushchin * 453c7be18aSRoman Gushchin * The allocator tries to allocate from the fullest chunk first. Each chunk 463c7be18aSRoman Gushchin * is managed by a bitmap with metadata blocks. The allocation map is updated 473c7be18aSRoman Gushchin * on every allocation and free to reflect the current state while the boundary 485e81ee3eSDennis Zhou (Facebook) * map is only updated on allocation. Each metadata block contains 495e81ee3eSDennis Zhou (Facebook) * information to help mitigate the need to iterate over large portions 505e81ee3eSDennis Zhou (Facebook) * of the bitmap. The reverse mapping from page to chunk is stored in 515e81ee3eSDennis Zhou (Facebook) * the page's index. Lastly, units are lazily backed and grow in unison. 52fbf59bc9STejun Heo * 535e81ee3eSDennis Zhou (Facebook) * There is a unique conversion that goes on here between bytes and bits. 545e81ee3eSDennis Zhou (Facebook) * Each bit represents a fragment of size PCPU_MIN_ALLOC_SIZE. The chunk 555e81ee3eSDennis Zhou (Facebook) * tracks the number of pages it is responsible for in nr_pages. Helper 565e81ee3eSDennis Zhou (Facebook) * functions are used to convert from between the bytes, bits, and blocks. 575e81ee3eSDennis Zhou (Facebook) * All hints are managed in bits unless explicitly stated. 589c015162SDennis Zhou (Facebook) * 594091fb95SMasahiro Yamada * To use this allocator, arch code should do the following: 60fbf59bc9STejun Heo * 61fbf59bc9STejun Heo * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate 62e0100983STejun Heo * regular address to percpu pointer and back if they need to be 63e0100983STejun Heo * different from the default 64fbf59bc9STejun Heo * 658d408b4bSTejun Heo * - use pcpu_setup_first_chunk() during percpu area initialization to 668d408b4bSTejun Heo * setup the first chunk containing the kernel static percpu area 67fbf59bc9STejun Heo */ 68fbf59bc9STejun Heo 69870d4b12SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 70870d4b12SJoe Perches 71fbf59bc9STejun Heo #include <linux/bitmap.h> 72d7d29ac7SWonhyuk Yang #include <linux/cpumask.h> 7357c8a661SMike Rapoport #include <linux/memblock.h> 74fd1e8a1fSTejun Heo #include <linux/err.h> 75ca460b3cSDennis Zhou (Facebook) #include <linux/lcm.h> 76fbf59bc9STejun Heo #include <linux/list.h> 77a530b795STejun Heo #include <linux/log2.h> 78fbf59bc9STejun Heo #include <linux/mm.h> 79fbf59bc9STejun Heo #include <linux/module.h> 80fbf59bc9STejun Heo #include <linux/mutex.h> 81fbf59bc9STejun Heo #include <linux/percpu.h> 82fbf59bc9STejun Heo #include <linux/pfn.h> 83fbf59bc9STejun Heo #include <linux/slab.h> 84ccea34b5STejun Heo #include <linux/spinlock.h> 85fbf59bc9STejun Heo #include <linux/vmalloc.h> 86a56dbddfSTejun Heo #include <linux/workqueue.h> 87f528f0b8SCatalin Marinas #include <linux/kmemleak.h> 8871546d10STejun Heo #include <linux/sched.h> 8928307d93SFilipe Manana #include <linux/sched/mm.h> 903c7be18aSRoman Gushchin #include <linux/memcontrol.h> 91fbf59bc9STejun Heo 92fbf59bc9STejun Heo #include <asm/cacheflush.h> 93e0100983STejun Heo #include <asm/sections.h> 94fbf59bc9STejun Heo #include <asm/tlbflush.h> 953b034b0dSVivek Goyal #include <asm/io.h> 96fbf59bc9STejun Heo 97df95e795SDennis Zhou #define CREATE_TRACE_POINTS 98df95e795SDennis Zhou #include <trace/events/percpu.h> 99df95e795SDennis Zhou 1008fa3ed80SDennis Zhou #include "percpu-internal.h" 1018fa3ed80SDennis Zhou 102ac9380f6SRoman Gushchin /* 103ac9380f6SRoman Gushchin * The slots are sorted by the size of the biggest continuous free area. 104ac9380f6SRoman Gushchin * 1-31 bytes share the same slot. 105ac9380f6SRoman Gushchin */ 10640064aecSDennis Zhou (Facebook) #define PCPU_SLOT_BASE_SHIFT 5 1078744d859SDennis Zhou /* chunks in slots below this are subject to being sidelined on failed alloc */ 1088744d859SDennis Zhou #define PCPU_SLOT_FAIL_THRESHOLD 3 10940064aecSDennis Zhou (Facebook) 1101a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_LOW 2 1111a4d7607STejun Heo #define PCPU_EMPTY_POP_PAGES_HIGH 4 112fbf59bc9STejun Heo 113bbddff05STejun Heo #ifdef CONFIG_SMP 114e0100983STejun Heo /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */ 115e0100983STejun Heo #ifndef __addr_to_pcpu_ptr 116e0100983STejun Heo #define __addr_to_pcpu_ptr(addr) \ 11743cf38ebSTejun Heo (void __percpu *)((unsigned long)(addr) - \ 11843cf38ebSTejun Heo (unsigned long)pcpu_base_addr + \ 11943cf38ebSTejun Heo (unsigned long)__per_cpu_start) 120e0100983STejun Heo #endif 121e0100983STejun Heo #ifndef __pcpu_ptr_to_addr 122e0100983STejun Heo #define __pcpu_ptr_to_addr(ptr) \ 12343cf38ebSTejun Heo (void __force *)((unsigned long)(ptr) + \ 12443cf38ebSTejun Heo (unsigned long)pcpu_base_addr - \ 12543cf38ebSTejun Heo (unsigned long)__per_cpu_start) 126e0100983STejun Heo #endif 127bbddff05STejun Heo #else /* CONFIG_SMP */ 128bbddff05STejun Heo /* on UP, it's always identity mapped */ 129bbddff05STejun Heo #define __addr_to_pcpu_ptr(addr) (void __percpu *)(addr) 130bbddff05STejun Heo #define __pcpu_ptr_to_addr(ptr) (void __force *)(ptr) 131bbddff05STejun Heo #endif /* CONFIG_SMP */ 132e0100983STejun Heo 1331328710bSDaniel Micay static int pcpu_unit_pages __ro_after_init; 1341328710bSDaniel Micay static int pcpu_unit_size __ro_after_init; 1351328710bSDaniel Micay static int pcpu_nr_units __ro_after_init; 1361328710bSDaniel Micay static int pcpu_atom_size __ro_after_init; 1378fa3ed80SDennis Zhou int pcpu_nr_slots __ro_after_init; 1388d55ba5dSWei Yongjun static int pcpu_free_slot __ro_after_init; 139f1833241SRoman Gushchin int pcpu_sidelined_slot __ro_after_init; 140f1833241SRoman Gushchin int pcpu_to_depopulate_slot __ro_after_init; 1411328710bSDaniel Micay static size_t pcpu_chunk_struct_size __ro_after_init; 142fbf59bc9STejun Heo 143a855b84cSTejun Heo /* cpus with the lowest and highest unit addresses */ 1441328710bSDaniel Micay static unsigned int pcpu_low_unit_cpu __ro_after_init; 1451328710bSDaniel Micay static unsigned int pcpu_high_unit_cpu __ro_after_init; 1462f39e637STejun Heo 147fbf59bc9STejun Heo /* the address of the first chunk which starts with the kernel static area */ 1481328710bSDaniel Micay void *pcpu_base_addr __ro_after_init; 149fbf59bc9STejun Heo 1501328710bSDaniel Micay static const int *pcpu_unit_map __ro_after_init; /* cpu -> unit */ 1511328710bSDaniel Micay const unsigned long *pcpu_unit_offsets __ro_after_init; /* cpu -> unit offset */ 1522f39e637STejun Heo 1536563297cSTejun Heo /* group information, used for vm allocation */ 1541328710bSDaniel Micay static int pcpu_nr_groups __ro_after_init; 1551328710bSDaniel Micay static const unsigned long *pcpu_group_offsets __ro_after_init; 1561328710bSDaniel Micay static const size_t *pcpu_group_sizes __ro_after_init; 1576563297cSTejun Heo 158ae9e6bc9STejun Heo /* 159ae9e6bc9STejun Heo * The first chunk which always exists. Note that unlike other 160ae9e6bc9STejun Heo * chunks, this one can be allocated and mapped in several different 161ae9e6bc9STejun Heo * ways and thus often doesn't live in the vmalloc area. 162ae9e6bc9STejun Heo */ 1638fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_first_chunk __ro_after_init; 164ae9e6bc9STejun Heo 165ae9e6bc9STejun Heo /* 166ae9e6bc9STejun Heo * Optional reserved chunk. This chunk reserves part of the first 167e2266705SDennis Zhou (Facebook) * chunk and serves it for reserved allocations. When the reserved 168e2266705SDennis Zhou (Facebook) * region doesn't exist, the following variable is NULL. 169ae9e6bc9STejun Heo */ 1708fa3ed80SDennis Zhou struct pcpu_chunk *pcpu_reserved_chunk __ro_after_init; 171edcb4639STejun Heo 1728fa3ed80SDennis Zhou DEFINE_SPINLOCK(pcpu_lock); /* all internal data structures */ 1736710e594STejun Heo static DEFINE_MUTEX(pcpu_alloc_mutex); /* chunk create/destroy, [de]pop, map ext */ 174fbf59bc9STejun Heo 1753c7be18aSRoman Gushchin struct list_head *pcpu_chunk_lists __ro_after_init; /* chunk list slots */ 176fbf59bc9STejun Heo 1774f996e23STejun Heo /* chunks which need their map areas extended, protected by pcpu_lock */ 1784f996e23STejun Heo static LIST_HEAD(pcpu_map_extend_chunks); 1794f996e23STejun Heo 180b539b87fSTejun Heo /* 181faf65ddeSRoman Gushchin * The number of empty populated pages, protected by pcpu_lock. 1820760fa3dSRoman Gushchin * The reserved chunk doesn't contribute to the count. 183b539b87fSTejun Heo */ 184faf65ddeSRoman Gushchin int pcpu_nr_empty_pop_pages; 185b539b87fSTejun Heo 1861a4d7607STejun Heo /* 1877e8a6304SDennis Zhou (Facebook) * The number of populated pages in use by the allocator, protected by 1887e8a6304SDennis Zhou (Facebook) * pcpu_lock. This number is kept per a unit per chunk (i.e. when a page gets 1897e8a6304SDennis Zhou (Facebook) * allocated/deallocated, it is allocated/deallocated in all units of a chunk 1907e8a6304SDennis Zhou (Facebook) * and increments/decrements this count by 1). 1917e8a6304SDennis Zhou (Facebook) */ 1927e8a6304SDennis Zhou (Facebook) static unsigned long pcpu_nr_populated; 1937e8a6304SDennis Zhou (Facebook) 1947e8a6304SDennis Zhou (Facebook) /* 1951a4d7607STejun Heo * Balance work is used to populate or destroy chunks asynchronously. We 1961a4d7607STejun Heo * try to keep the number of populated free pages between 1971a4d7607STejun Heo * PCPU_EMPTY_POP_PAGES_LOW and HIGH for atomic allocations and at most one 1981a4d7607STejun Heo * empty chunk. 1991a4d7607STejun Heo */ 200fe6bd8c3STejun Heo static void pcpu_balance_workfn(struct work_struct *work); 201fe6bd8c3STejun Heo static DECLARE_WORK(pcpu_balance_work, pcpu_balance_workfn); 2021a4d7607STejun Heo static bool pcpu_async_enabled __read_mostly; 2031a4d7607STejun Heo static bool pcpu_atomic_alloc_failed; 2041a4d7607STejun Heo 2051a4d7607STejun Heo static void pcpu_schedule_balance_work(void) 2061a4d7607STejun Heo { 2071a4d7607STejun Heo if (pcpu_async_enabled) 2081a4d7607STejun Heo schedule_work(&pcpu_balance_work); 2091a4d7607STejun Heo } 210a56dbddfSTejun Heo 211c0ebfdc3SDennis Zhou (Facebook) /** 212560f2c23SDennis Zhou (Facebook) * pcpu_addr_in_chunk - check if the address is served from this chunk 213560f2c23SDennis Zhou (Facebook) * @chunk: chunk of interest 214560f2c23SDennis Zhou (Facebook) * @addr: percpu address 215c0ebfdc3SDennis Zhou (Facebook) * 216c0ebfdc3SDennis Zhou (Facebook) * RETURNS: 217560f2c23SDennis Zhou (Facebook) * True if the address is served from this chunk. 218c0ebfdc3SDennis Zhou (Facebook) */ 219560f2c23SDennis Zhou (Facebook) static bool pcpu_addr_in_chunk(struct pcpu_chunk *chunk, void *addr) 220020ec653STejun Heo { 221c0ebfdc3SDennis Zhou (Facebook) void *start_addr, *end_addr; 222020ec653STejun Heo 223560f2c23SDennis Zhou (Facebook) if (!chunk) 224c0ebfdc3SDennis Zhou (Facebook) return false; 225c0ebfdc3SDennis Zhou (Facebook) 226560f2c23SDennis Zhou (Facebook) start_addr = chunk->base_addr + chunk->start_offset; 227560f2c23SDennis Zhou (Facebook) end_addr = chunk->base_addr + chunk->nr_pages * PAGE_SIZE - 228560f2c23SDennis Zhou (Facebook) chunk->end_offset; 229c0ebfdc3SDennis Zhou (Facebook) 230c0ebfdc3SDennis Zhou (Facebook) return addr >= start_addr && addr < end_addr; 231020ec653STejun Heo } 232020ec653STejun Heo 233d9b55eebSTejun Heo static int __pcpu_size_to_slot(int size) 234fbf59bc9STejun Heo { 235cae3aeb8STejun Heo int highbit = fls(size); /* size is in bytes */ 236fbf59bc9STejun Heo return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1); 237fbf59bc9STejun Heo } 238fbf59bc9STejun Heo 239d9b55eebSTejun Heo static int pcpu_size_to_slot(int size) 240d9b55eebSTejun Heo { 241d9b55eebSTejun Heo if (size == pcpu_unit_size) 2421c29a3ceSDennis Zhou return pcpu_free_slot; 243d9b55eebSTejun Heo return __pcpu_size_to_slot(size); 244d9b55eebSTejun Heo } 245d9b55eebSTejun Heo 246fbf59bc9STejun Heo static int pcpu_chunk_slot(const struct pcpu_chunk *chunk) 247fbf59bc9STejun Heo { 24892c14cabSDennis Zhou const struct pcpu_block_md *chunk_md = &chunk->chunk_md; 24992c14cabSDennis Zhou 25092c14cabSDennis Zhou if (chunk->free_bytes < PCPU_MIN_ALLOC_SIZE || 25192c14cabSDennis Zhou chunk_md->contig_hint == 0) 252fbf59bc9STejun Heo return 0; 253fbf59bc9STejun Heo 25492c14cabSDennis Zhou return pcpu_size_to_slot(chunk_md->contig_hint * PCPU_MIN_ALLOC_SIZE); 255fbf59bc9STejun Heo } 256fbf59bc9STejun Heo 25788999a89STejun Heo /* set the pointer to a chunk in a page struct */ 25888999a89STejun Heo static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu) 25988999a89STejun Heo { 26088999a89STejun Heo page->index = (unsigned long)pcpu; 26188999a89STejun Heo } 26288999a89STejun Heo 26388999a89STejun Heo /* obtain pointer to a chunk from a page struct */ 26488999a89STejun Heo static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page) 26588999a89STejun Heo { 26688999a89STejun Heo return (struct pcpu_chunk *)page->index; 26788999a89STejun Heo } 26888999a89STejun Heo 26988999a89STejun Heo static int __maybe_unused pcpu_page_idx(unsigned int cpu, int page_idx) 270fbf59bc9STejun Heo { 2712f39e637STejun Heo return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx; 272fbf59bc9STejun Heo } 273fbf59bc9STejun Heo 274c0ebfdc3SDennis Zhou (Facebook) static unsigned long pcpu_unit_page_offset(unsigned int cpu, int page_idx) 275c0ebfdc3SDennis Zhou (Facebook) { 276c0ebfdc3SDennis Zhou (Facebook) return pcpu_unit_offsets[cpu] + (page_idx << PAGE_SHIFT); 277c0ebfdc3SDennis Zhou (Facebook) } 278c0ebfdc3SDennis Zhou (Facebook) 2799983b6f0STejun Heo static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk, 280fbf59bc9STejun Heo unsigned int cpu, int page_idx) 281fbf59bc9STejun Heo { 282c0ebfdc3SDennis Zhou (Facebook) return (unsigned long)chunk->base_addr + 283c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(cpu, page_idx); 284fbf59bc9STejun Heo } 285fbf59bc9STejun Heo 286ca460b3cSDennis Zhou (Facebook) /* 287ca460b3cSDennis Zhou (Facebook) * The following are helper functions to help access bitmaps and convert 288ca460b3cSDennis Zhou (Facebook) * between bitmap offsets to address offsets. 289ca460b3cSDennis Zhou (Facebook) */ 290ca460b3cSDennis Zhou (Facebook) static unsigned long *pcpu_index_alloc_map(struct pcpu_chunk *chunk, int index) 291ca460b3cSDennis Zhou (Facebook) { 292ca460b3cSDennis Zhou (Facebook) return chunk->alloc_map + 293ca460b3cSDennis Zhou (Facebook) (index * PCPU_BITMAP_BLOCK_BITS / BITS_PER_LONG); 294ca460b3cSDennis Zhou (Facebook) } 295ca460b3cSDennis Zhou (Facebook) 296ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_index(int off) 297ca460b3cSDennis Zhou (Facebook) { 298ca460b3cSDennis Zhou (Facebook) return off / PCPU_BITMAP_BLOCK_BITS; 299ca460b3cSDennis Zhou (Facebook) } 300ca460b3cSDennis Zhou (Facebook) 301ca460b3cSDennis Zhou (Facebook) static unsigned long pcpu_off_to_block_off(int off) 302ca460b3cSDennis Zhou (Facebook) { 303ca460b3cSDennis Zhou (Facebook) return off & (PCPU_BITMAP_BLOCK_BITS - 1); 304ca460b3cSDennis Zhou (Facebook) } 305ca460b3cSDennis Zhou (Facebook) 306b185cd0dSDennis Zhou (Facebook) static unsigned long pcpu_block_off_to_off(int index, int off) 307b185cd0dSDennis Zhou (Facebook) { 308b185cd0dSDennis Zhou (Facebook) return index * PCPU_BITMAP_BLOCK_BITS + off; 309b185cd0dSDennis Zhou (Facebook) } 310b185cd0dSDennis Zhou (Facebook) 3118ea2e1e3SRoman Gushchin /** 3128ea2e1e3SRoman Gushchin * pcpu_check_block_hint - check against the contig hint 3138ea2e1e3SRoman Gushchin * @block: block of interest 3148ea2e1e3SRoman Gushchin * @bits: size of allocation 3158ea2e1e3SRoman Gushchin * @align: alignment of area (max PAGE_SIZE) 3168ea2e1e3SRoman Gushchin * 3178ea2e1e3SRoman Gushchin * Check to see if the allocation can fit in the block's contig hint. 3188ea2e1e3SRoman Gushchin * Note, a chunk uses the same hints as a block so this can also check against 3198ea2e1e3SRoman Gushchin * the chunk's contig hint. 3208ea2e1e3SRoman Gushchin */ 3218ea2e1e3SRoman Gushchin static bool pcpu_check_block_hint(struct pcpu_block_md *block, int bits, 3228ea2e1e3SRoman Gushchin size_t align) 3238ea2e1e3SRoman Gushchin { 3248ea2e1e3SRoman Gushchin int bit_off = ALIGN(block->contig_hint_start, align) - 3258ea2e1e3SRoman Gushchin block->contig_hint_start; 3268ea2e1e3SRoman Gushchin 3278ea2e1e3SRoman Gushchin return bit_off + bits <= block->contig_hint; 3288ea2e1e3SRoman Gushchin } 3298ea2e1e3SRoman Gushchin 330382b88e9SDennis Zhou /* 331382b88e9SDennis Zhou * pcpu_next_hint - determine which hint to use 332382b88e9SDennis Zhou * @block: block of interest 333382b88e9SDennis Zhou * @alloc_bits: size of allocation 334382b88e9SDennis Zhou * 335382b88e9SDennis Zhou * This determines if we should scan based on the scan_hint or first_free. 336382b88e9SDennis Zhou * In general, we want to scan from first_free to fulfill allocations by 337382b88e9SDennis Zhou * first fit. However, if we know a scan_hint at position scan_hint_start 338382b88e9SDennis Zhou * cannot fulfill an allocation, we can begin scanning from there knowing 339382b88e9SDennis Zhou * the contig_hint will be our fallback. 340382b88e9SDennis Zhou */ 341382b88e9SDennis Zhou static int pcpu_next_hint(struct pcpu_block_md *block, int alloc_bits) 342382b88e9SDennis Zhou { 343382b88e9SDennis Zhou /* 344382b88e9SDennis Zhou * The three conditions below determine if we can skip past the 345382b88e9SDennis Zhou * scan_hint. First, does the scan hint exist. Second, is the 346382b88e9SDennis Zhou * contig_hint after the scan_hint (possibly not true iff 347382b88e9SDennis Zhou * contig_hint == scan_hint). Third, is the allocation request 348382b88e9SDennis Zhou * larger than the scan_hint. 349382b88e9SDennis Zhou */ 350382b88e9SDennis Zhou if (block->scan_hint && 351382b88e9SDennis Zhou block->contig_hint_start > block->scan_hint_start && 352382b88e9SDennis Zhou alloc_bits > block->scan_hint) 353382b88e9SDennis Zhou return block->scan_hint_start + block->scan_hint; 354382b88e9SDennis Zhou 355382b88e9SDennis Zhou return block->first_free; 356382b88e9SDennis Zhou } 357382b88e9SDennis Zhou 358fbf59bc9STejun Heo /** 359525ca84dSDennis Zhou (Facebook) * pcpu_next_md_free_region - finds the next hint free area 360525ca84dSDennis Zhou (Facebook) * @chunk: chunk of interest 361525ca84dSDennis Zhou (Facebook) * @bit_off: chunk offset 362525ca84dSDennis Zhou (Facebook) * @bits: size of free area 363525ca84dSDennis Zhou (Facebook) * 364525ca84dSDennis Zhou (Facebook) * Helper function for pcpu_for_each_md_free_region. It checks 365525ca84dSDennis Zhou (Facebook) * block->contig_hint and performs aggregation across blocks to find the 366525ca84dSDennis Zhou (Facebook) * next hint. It modifies bit_off and bits in-place to be consumed in the 367525ca84dSDennis Zhou (Facebook) * loop. 368525ca84dSDennis Zhou (Facebook) */ 369525ca84dSDennis Zhou (Facebook) static void pcpu_next_md_free_region(struct pcpu_chunk *chunk, int *bit_off, 370525ca84dSDennis Zhou (Facebook) int *bits) 371525ca84dSDennis Zhou (Facebook) { 372525ca84dSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 373525ca84dSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 374525ca84dSDennis Zhou (Facebook) struct pcpu_block_md *block; 375525ca84dSDennis Zhou (Facebook) 376525ca84dSDennis Zhou (Facebook) *bits = 0; 377525ca84dSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 378525ca84dSDennis Zhou (Facebook) block++, i++) { 379525ca84dSDennis Zhou (Facebook) /* handles contig area across blocks */ 380525ca84dSDennis Zhou (Facebook) if (*bits) { 381525ca84dSDennis Zhou (Facebook) *bits += block->left_free; 382525ca84dSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 383525ca84dSDennis Zhou (Facebook) continue; 384525ca84dSDennis Zhou (Facebook) return; 385525ca84dSDennis Zhou (Facebook) } 386525ca84dSDennis Zhou (Facebook) 387525ca84dSDennis Zhou (Facebook) /* 388525ca84dSDennis Zhou (Facebook) * This checks three things. First is there a contig_hint to 389525ca84dSDennis Zhou (Facebook) * check. Second, have we checked this hint before by 390525ca84dSDennis Zhou (Facebook) * comparing the block_off. Third, is this the same as the 391525ca84dSDennis Zhou (Facebook) * right contig hint. In the last case, it spills over into 392525ca84dSDennis Zhou (Facebook) * the next block and should be handled by the contig area 393525ca84dSDennis Zhou (Facebook) * across blocks code. 394525ca84dSDennis Zhou (Facebook) */ 395525ca84dSDennis Zhou (Facebook) *bits = block->contig_hint; 396525ca84dSDennis Zhou (Facebook) if (*bits && block->contig_hint_start >= block_off && 397525ca84dSDennis Zhou (Facebook) *bits + block->contig_hint_start < PCPU_BITMAP_BLOCK_BITS) { 398525ca84dSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, 399525ca84dSDennis Zhou (Facebook) block->contig_hint_start); 400525ca84dSDennis Zhou (Facebook) return; 401525ca84dSDennis Zhou (Facebook) } 4021fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 4031fa4df3eSDennis Zhou block_off = 0; 404525ca84dSDennis Zhou (Facebook) 405525ca84dSDennis Zhou (Facebook) *bits = block->right_free; 406525ca84dSDennis Zhou (Facebook) *bit_off = (i + 1) * PCPU_BITMAP_BLOCK_BITS - block->right_free; 407525ca84dSDennis Zhou (Facebook) } 408525ca84dSDennis Zhou (Facebook) } 409525ca84dSDennis Zhou (Facebook) 410b4c2116cSDennis Zhou (Facebook) /** 411b4c2116cSDennis Zhou (Facebook) * pcpu_next_fit_region - finds fit areas for a given allocation request 412b4c2116cSDennis Zhou (Facebook) * @chunk: chunk of interest 413b4c2116cSDennis Zhou (Facebook) * @alloc_bits: size of allocation 414b4c2116cSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 415b4c2116cSDennis Zhou (Facebook) * @bit_off: chunk offset 416b4c2116cSDennis Zhou (Facebook) * @bits: size of free area 417b4c2116cSDennis Zhou (Facebook) * 418b4c2116cSDennis Zhou (Facebook) * Finds the next free region that is viable for use with a given size and 419b4c2116cSDennis Zhou (Facebook) * alignment. This only returns if there is a valid area to be used for this 420b4c2116cSDennis Zhou (Facebook) * allocation. block->first_free is returned if the allocation request fits 421b4c2116cSDennis Zhou (Facebook) * within the block to see if the request can be fulfilled prior to the contig 422b4c2116cSDennis Zhou (Facebook) * hint. 423b4c2116cSDennis Zhou (Facebook) */ 424b4c2116cSDennis Zhou (Facebook) static void pcpu_next_fit_region(struct pcpu_chunk *chunk, int alloc_bits, 425b4c2116cSDennis Zhou (Facebook) int align, int *bit_off, int *bits) 426b4c2116cSDennis Zhou (Facebook) { 427b4c2116cSDennis Zhou (Facebook) int i = pcpu_off_to_block_index(*bit_off); 428b4c2116cSDennis Zhou (Facebook) int block_off = pcpu_off_to_block_off(*bit_off); 429b4c2116cSDennis Zhou (Facebook) struct pcpu_block_md *block; 430b4c2116cSDennis Zhou (Facebook) 431b4c2116cSDennis Zhou (Facebook) *bits = 0; 432b4c2116cSDennis Zhou (Facebook) for (block = chunk->md_blocks + i; i < pcpu_chunk_nr_blocks(chunk); 433b4c2116cSDennis Zhou (Facebook) block++, i++) { 434b4c2116cSDennis Zhou (Facebook) /* handles contig area across blocks */ 435b4c2116cSDennis Zhou (Facebook) if (*bits) { 436b4c2116cSDennis Zhou (Facebook) *bits += block->left_free; 437b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 438b4c2116cSDennis Zhou (Facebook) return; 439b4c2116cSDennis Zhou (Facebook) if (block->left_free == PCPU_BITMAP_BLOCK_BITS) 440b4c2116cSDennis Zhou (Facebook) continue; 441b4c2116cSDennis Zhou (Facebook) } 442b4c2116cSDennis Zhou (Facebook) 443b4c2116cSDennis Zhou (Facebook) /* check block->contig_hint */ 444b4c2116cSDennis Zhou (Facebook) *bits = ALIGN(block->contig_hint_start, align) - 445b4c2116cSDennis Zhou (Facebook) block->contig_hint_start; 446b4c2116cSDennis Zhou (Facebook) /* 447b4c2116cSDennis Zhou (Facebook) * This uses the block offset to determine if this has been 448b4c2116cSDennis Zhou (Facebook) * checked in the prior iteration. 449b4c2116cSDennis Zhou (Facebook) */ 450b4c2116cSDennis Zhou (Facebook) if (block->contig_hint && 451b4c2116cSDennis Zhou (Facebook) block->contig_hint_start >= block_off && 452b4c2116cSDennis Zhou (Facebook) block->contig_hint >= *bits + alloc_bits) { 453382b88e9SDennis Zhou int start = pcpu_next_hint(block, alloc_bits); 454382b88e9SDennis Zhou 455b4c2116cSDennis Zhou (Facebook) *bits += alloc_bits + block->contig_hint_start - 456382b88e9SDennis Zhou start; 457382b88e9SDennis Zhou *bit_off = pcpu_block_off_to_off(i, start); 458b4c2116cSDennis Zhou (Facebook) return; 459b4c2116cSDennis Zhou (Facebook) } 4601fa4df3eSDennis Zhou /* reset to satisfy the second predicate above */ 4611fa4df3eSDennis Zhou block_off = 0; 462b4c2116cSDennis Zhou (Facebook) 463b4c2116cSDennis Zhou (Facebook) *bit_off = ALIGN(PCPU_BITMAP_BLOCK_BITS - block->right_free, 464b4c2116cSDennis Zhou (Facebook) align); 465b4c2116cSDennis Zhou (Facebook) *bits = PCPU_BITMAP_BLOCK_BITS - *bit_off; 466b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_block_off_to_off(i, *bit_off); 467b4c2116cSDennis Zhou (Facebook) if (*bits >= alloc_bits) 468b4c2116cSDennis Zhou (Facebook) return; 469b4c2116cSDennis Zhou (Facebook) } 470b4c2116cSDennis Zhou (Facebook) 471b4c2116cSDennis Zhou (Facebook) /* no valid offsets were found - fail condition */ 472b4c2116cSDennis Zhou (Facebook) *bit_off = pcpu_chunk_map_bits(chunk); 473b4c2116cSDennis Zhou (Facebook) } 474b4c2116cSDennis Zhou (Facebook) 475525ca84dSDennis Zhou (Facebook) /* 476525ca84dSDennis Zhou (Facebook) * Metadata free area iterators. These perform aggregation of free areas 477525ca84dSDennis Zhou (Facebook) * based on the metadata blocks and return the offset @bit_off and size in 478b4c2116cSDennis Zhou (Facebook) * bits of the free area @bits. pcpu_for_each_fit_region only returns when 479b4c2116cSDennis Zhou (Facebook) * a fit is found for the allocation request. 480525ca84dSDennis Zhou (Facebook) */ 481525ca84dSDennis Zhou (Facebook) #define pcpu_for_each_md_free_region(chunk, bit_off, bits) \ 482525ca84dSDennis Zhou (Facebook) for (pcpu_next_md_free_region((chunk), &(bit_off), &(bits)); \ 483525ca84dSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 484525ca84dSDennis Zhou (Facebook) (bit_off) += (bits) + 1, \ 485525ca84dSDennis Zhou (Facebook) pcpu_next_md_free_region((chunk), &(bit_off), &(bits))) 486525ca84dSDennis Zhou (Facebook) 487b4c2116cSDennis Zhou (Facebook) #define pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) \ 488b4c2116cSDennis Zhou (Facebook) for (pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 489b4c2116cSDennis Zhou (Facebook) &(bits)); \ 490b4c2116cSDennis Zhou (Facebook) (bit_off) < pcpu_chunk_map_bits((chunk)); \ 491b4c2116cSDennis Zhou (Facebook) (bit_off) += (bits), \ 492b4c2116cSDennis Zhou (Facebook) pcpu_next_fit_region((chunk), (alloc_bits), (align), &(bit_off), \ 493b4c2116cSDennis Zhou (Facebook) &(bits))) 494b4c2116cSDennis Zhou (Facebook) 495525ca84dSDennis Zhou (Facebook) /** 49690459ce0SBob Liu * pcpu_mem_zalloc - allocate memory 4971880d93bSTejun Heo * @size: bytes to allocate 49847504ee0SDennis Zhou * @gfp: allocation flags 499fbf59bc9STejun Heo * 5001880d93bSTejun Heo * Allocate @size bytes. If @size is smaller than PAGE_SIZE, 50147504ee0SDennis Zhou * kzalloc() is used; otherwise, the equivalent of vzalloc() is used. 50247504ee0SDennis Zhou * This is to facilitate passing through whitelisted flags. The 50347504ee0SDennis Zhou * returned memory is always zeroed. 504fbf59bc9STejun Heo * 505fbf59bc9STejun Heo * RETURNS: 5061880d93bSTejun Heo * Pointer to the allocated area on success, NULL on failure. 507fbf59bc9STejun Heo */ 50847504ee0SDennis Zhou static void *pcpu_mem_zalloc(size_t size, gfp_t gfp) 509fbf59bc9STejun Heo { 510099a19d9STejun Heo if (WARN_ON_ONCE(!slab_is_available())) 511099a19d9STejun Heo return NULL; 512099a19d9STejun Heo 513fbf59bc9STejun Heo if (size <= PAGE_SIZE) 514554fef1cSDennis Zhou return kzalloc(size, gfp); 5157af4c093SJesper Juhl else 51688dca4caSChristoph Hellwig return __vmalloc(size, gfp | __GFP_ZERO); 5171880d93bSTejun Heo } 518fbf59bc9STejun Heo 5191880d93bSTejun Heo /** 5201880d93bSTejun Heo * pcpu_mem_free - free memory 5211880d93bSTejun Heo * @ptr: memory to free 5221880d93bSTejun Heo * 52390459ce0SBob Liu * Free @ptr. @ptr should have been allocated using pcpu_mem_zalloc(). 5241880d93bSTejun Heo */ 5251d5cfdb0STetsuo Handa static void pcpu_mem_free(void *ptr) 5261880d93bSTejun Heo { 5271d5cfdb0STetsuo Handa kvfree(ptr); 528fbf59bc9STejun Heo } 529fbf59bc9STejun Heo 5308744d859SDennis Zhou static void __pcpu_chunk_move(struct pcpu_chunk *chunk, int slot, 5318744d859SDennis Zhou bool move_front) 5328744d859SDennis Zhou { 5338744d859SDennis Zhou if (chunk != pcpu_reserved_chunk) { 5348744d859SDennis Zhou if (move_front) 535faf65ddeSRoman Gushchin list_move(&chunk->list, &pcpu_chunk_lists[slot]); 5368744d859SDennis Zhou else 537faf65ddeSRoman Gushchin list_move_tail(&chunk->list, &pcpu_chunk_lists[slot]); 5388744d859SDennis Zhou } 5398744d859SDennis Zhou } 5408744d859SDennis Zhou 5418744d859SDennis Zhou static void pcpu_chunk_move(struct pcpu_chunk *chunk, int slot) 5428744d859SDennis Zhou { 5438744d859SDennis Zhou __pcpu_chunk_move(chunk, slot, true); 5448744d859SDennis Zhou } 5458744d859SDennis Zhou 546fbf59bc9STejun Heo /** 547fbf59bc9STejun Heo * pcpu_chunk_relocate - put chunk in the appropriate chunk slot 548fbf59bc9STejun Heo * @chunk: chunk of interest 549fbf59bc9STejun Heo * @oslot: the previous slot it was on 550fbf59bc9STejun Heo * 551fbf59bc9STejun Heo * This function is called after an allocation or free changed @chunk. 552fbf59bc9STejun Heo * New slot according to the changed state is determined and @chunk is 553edcb4639STejun Heo * moved to the slot. Note that the reserved chunk is never put on 554edcb4639STejun Heo * chunk slots. 555ccea34b5STejun Heo * 556ccea34b5STejun Heo * CONTEXT: 557ccea34b5STejun Heo * pcpu_lock. 558fbf59bc9STejun Heo */ 559fbf59bc9STejun Heo static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) 560fbf59bc9STejun Heo { 561fbf59bc9STejun Heo int nslot = pcpu_chunk_slot(chunk); 562fbf59bc9STejun Heo 563f1833241SRoman Gushchin /* leave isolated chunks in-place */ 564f1833241SRoman Gushchin if (chunk->isolated) 565f1833241SRoman Gushchin return; 566f1833241SRoman Gushchin 5678744d859SDennis Zhou if (oslot != nslot) 5688744d859SDennis Zhou __pcpu_chunk_move(chunk, nslot, oslot < nslot); 56940064aecSDennis Zhou (Facebook) } 57040064aecSDennis Zhou (Facebook) 571f1833241SRoman Gushchin static void pcpu_isolate_chunk(struct pcpu_chunk *chunk) 572f1833241SRoman Gushchin { 573f1833241SRoman Gushchin lockdep_assert_held(&pcpu_lock); 574f1833241SRoman Gushchin 575f1833241SRoman Gushchin if (!chunk->isolated) { 576f1833241SRoman Gushchin chunk->isolated = true; 577faf65ddeSRoman Gushchin pcpu_nr_empty_pop_pages -= chunk->nr_empty_pop_pages; 578f1833241SRoman Gushchin } 579faf65ddeSRoman Gushchin list_move(&chunk->list, &pcpu_chunk_lists[pcpu_to_depopulate_slot]); 580f1833241SRoman Gushchin } 581f1833241SRoman Gushchin 582f1833241SRoman Gushchin static void pcpu_reintegrate_chunk(struct pcpu_chunk *chunk) 583f1833241SRoman Gushchin { 584f1833241SRoman Gushchin lockdep_assert_held(&pcpu_lock); 585f1833241SRoman Gushchin 586f1833241SRoman Gushchin if (chunk->isolated) { 587f1833241SRoman Gushchin chunk->isolated = false; 588faf65ddeSRoman Gushchin pcpu_nr_empty_pop_pages += chunk->nr_empty_pop_pages; 589f1833241SRoman Gushchin pcpu_chunk_relocate(chunk, -1); 590f1833241SRoman Gushchin } 591f1833241SRoman Gushchin } 592f1833241SRoman Gushchin 59340064aecSDennis Zhou (Facebook) /* 594b239f7daSDennis Zhou * pcpu_update_empty_pages - update empty page counters 595b239f7daSDennis Zhou * @chunk: chunk of interest 596b239f7daSDennis Zhou * @nr: nr of empty pages 59740064aecSDennis Zhou (Facebook) * 598b239f7daSDennis Zhou * This is used to keep track of the empty pages now based on the premise 599b239f7daSDennis Zhou * a md_block covers a page. The hint update functions recognize if a block 600b239f7daSDennis Zhou * is made full or broken to calculate deltas for keeping track of free pages. 60140064aecSDennis Zhou (Facebook) */ 602b239f7daSDennis Zhou static inline void pcpu_update_empty_pages(struct pcpu_chunk *chunk, int nr) 603b239f7daSDennis Zhou { 604b239f7daSDennis Zhou chunk->nr_empty_pop_pages += nr; 605f1833241SRoman Gushchin if (chunk != pcpu_reserved_chunk && !chunk->isolated) 606faf65ddeSRoman Gushchin pcpu_nr_empty_pop_pages += nr; 60740064aecSDennis Zhou (Facebook) } 60840064aecSDennis Zhou (Facebook) 609d9f3a01eSDennis Zhou /* 610d9f3a01eSDennis Zhou * pcpu_region_overlap - determines if two regions overlap 611d9f3a01eSDennis Zhou * @a: start of first region, inclusive 612d9f3a01eSDennis Zhou * @b: end of first region, exclusive 613d9f3a01eSDennis Zhou * @x: start of second region, inclusive 614d9f3a01eSDennis Zhou * @y: end of second region, exclusive 615d9f3a01eSDennis Zhou * 616d9f3a01eSDennis Zhou * This is used to determine if the hint region [a, b) overlaps with the 617d9f3a01eSDennis Zhou * allocated region [x, y). 618d9f3a01eSDennis Zhou */ 619d9f3a01eSDennis Zhou static inline bool pcpu_region_overlap(int a, int b, int x, int y) 620d9f3a01eSDennis Zhou { 621d9f3a01eSDennis Zhou return (a < y) && (x < b); 62240064aecSDennis Zhou (Facebook) } 62340064aecSDennis Zhou (Facebook) 62440064aecSDennis Zhou (Facebook) /** 625ca460b3cSDennis Zhou (Facebook) * pcpu_block_update - updates a block given a free area 626ca460b3cSDennis Zhou (Facebook) * @block: block of interest 627ca460b3cSDennis Zhou (Facebook) * @start: start offset in block 628ca460b3cSDennis Zhou (Facebook) * @end: end offset in block 629ca460b3cSDennis Zhou (Facebook) * 630ca460b3cSDennis Zhou (Facebook) * Updates a block given a known free area. The region [start, end) is 631268625a6SDennis Zhou (Facebook) * expected to be the entirety of the free area within a block. Chooses 632268625a6SDennis Zhou (Facebook) * the best starting offset if the contig hints are equal. 633ca460b3cSDennis Zhou (Facebook) */ 634ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update(struct pcpu_block_md *block, int start, int end) 635ca460b3cSDennis Zhou (Facebook) { 636ca460b3cSDennis Zhou (Facebook) int contig = end - start; 637ca460b3cSDennis Zhou (Facebook) 638ca460b3cSDennis Zhou (Facebook) block->first_free = min(block->first_free, start); 639ca460b3cSDennis Zhou (Facebook) if (start == 0) 640ca460b3cSDennis Zhou (Facebook) block->left_free = contig; 641ca460b3cSDennis Zhou (Facebook) 642047924c9SDennis Zhou if (end == block->nr_bits) 643ca460b3cSDennis Zhou (Facebook) block->right_free = contig; 644ca460b3cSDennis Zhou (Facebook) 645ca460b3cSDennis Zhou (Facebook) if (contig > block->contig_hint) { 646382b88e9SDennis Zhou /* promote the old contig_hint to be the new scan_hint */ 647382b88e9SDennis Zhou if (start > block->contig_hint_start) { 648382b88e9SDennis Zhou if (block->contig_hint > block->scan_hint) { 649382b88e9SDennis Zhou block->scan_hint_start = 650382b88e9SDennis Zhou block->contig_hint_start; 651382b88e9SDennis Zhou block->scan_hint = block->contig_hint; 652382b88e9SDennis Zhou } else if (start < block->scan_hint_start) { 653382b88e9SDennis Zhou /* 654382b88e9SDennis Zhou * The old contig_hint == scan_hint. But, the 655382b88e9SDennis Zhou * new contig is larger so hold the invariant 656382b88e9SDennis Zhou * scan_hint_start < contig_hint_start. 657382b88e9SDennis Zhou */ 658382b88e9SDennis Zhou block->scan_hint = 0; 659382b88e9SDennis Zhou } 660382b88e9SDennis Zhou } else { 661382b88e9SDennis Zhou block->scan_hint = 0; 662382b88e9SDennis Zhou } 663ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = start; 664ca460b3cSDennis Zhou (Facebook) block->contig_hint = contig; 665382b88e9SDennis Zhou } else if (contig == block->contig_hint) { 666382b88e9SDennis Zhou if (block->contig_hint_start && 667382b88e9SDennis Zhou (!start || 668382b88e9SDennis Zhou __ffs(start) > __ffs(block->contig_hint_start))) { 669382b88e9SDennis Zhou /* start has a better alignment so use it */ 670268625a6SDennis Zhou (Facebook) block->contig_hint_start = start; 671382b88e9SDennis Zhou if (start < block->scan_hint_start && 672382b88e9SDennis Zhou block->contig_hint > block->scan_hint) 673382b88e9SDennis Zhou block->scan_hint = 0; 674382b88e9SDennis Zhou } else if (start > block->scan_hint_start || 675382b88e9SDennis Zhou block->contig_hint > block->scan_hint) { 676382b88e9SDennis Zhou /* 677382b88e9SDennis Zhou * Knowing contig == contig_hint, update the scan_hint 678382b88e9SDennis Zhou * if it is farther than or larger than the current 679382b88e9SDennis Zhou * scan_hint. 680382b88e9SDennis Zhou */ 681382b88e9SDennis Zhou block->scan_hint_start = start; 682382b88e9SDennis Zhou block->scan_hint = contig; 683382b88e9SDennis Zhou } 684382b88e9SDennis Zhou } else { 685382b88e9SDennis Zhou /* 686382b88e9SDennis Zhou * The region is smaller than the contig_hint. So only update 687382b88e9SDennis Zhou * the scan_hint if it is larger than or equal and farther than 688382b88e9SDennis Zhou * the current scan_hint. 689382b88e9SDennis Zhou */ 690382b88e9SDennis Zhou if ((start < block->contig_hint_start && 691382b88e9SDennis Zhou (contig > block->scan_hint || 692382b88e9SDennis Zhou (contig == block->scan_hint && 693382b88e9SDennis Zhou start > block->scan_hint_start)))) { 694382b88e9SDennis Zhou block->scan_hint_start = start; 695382b88e9SDennis Zhou block->scan_hint = contig; 696382b88e9SDennis Zhou } 697ca460b3cSDennis Zhou (Facebook) } 698ca460b3cSDennis Zhou (Facebook) } 699ca460b3cSDennis Zhou (Facebook) 700b89462a9SDennis Zhou /* 701b89462a9SDennis Zhou * pcpu_block_update_scan - update a block given a free area from a scan 702b89462a9SDennis Zhou * @chunk: chunk of interest 703b89462a9SDennis Zhou * @bit_off: chunk offset 704b89462a9SDennis Zhou * @bits: size of free area 705b89462a9SDennis Zhou * 706b89462a9SDennis Zhou * Finding the final allocation spot first goes through pcpu_find_block_fit() 707b89462a9SDennis Zhou * to find a block that can hold the allocation and then pcpu_alloc_area() 708b89462a9SDennis Zhou * where a scan is used. When allocations require specific alignments, 709b89462a9SDennis Zhou * we can inadvertently create holes which will not be seen in the alloc 710b89462a9SDennis Zhou * or free paths. 711b89462a9SDennis Zhou * 712b89462a9SDennis Zhou * This takes a given free area hole and updates a block as it may change the 713b89462a9SDennis Zhou * scan_hint. We need to scan backwards to ensure we don't miss free bits 714b89462a9SDennis Zhou * from alignment. 715b89462a9SDennis Zhou */ 716b89462a9SDennis Zhou static void pcpu_block_update_scan(struct pcpu_chunk *chunk, int bit_off, 717b89462a9SDennis Zhou int bits) 718b89462a9SDennis Zhou { 719b89462a9SDennis Zhou int s_off = pcpu_off_to_block_off(bit_off); 720b89462a9SDennis Zhou int e_off = s_off + bits; 721b89462a9SDennis Zhou int s_index, l_bit; 722b89462a9SDennis Zhou struct pcpu_block_md *block; 723b89462a9SDennis Zhou 724b89462a9SDennis Zhou if (e_off > PCPU_BITMAP_BLOCK_BITS) 725b89462a9SDennis Zhou return; 726b89462a9SDennis Zhou 727b89462a9SDennis Zhou s_index = pcpu_off_to_block_index(bit_off); 728b89462a9SDennis Zhou block = chunk->md_blocks + s_index; 729b89462a9SDennis Zhou 730b89462a9SDennis Zhou /* scan backwards in case of alignment skipping free bits */ 731b89462a9SDennis Zhou l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index), s_off); 732b89462a9SDennis Zhou s_off = (s_off == l_bit) ? 0 : l_bit + 1; 733b89462a9SDennis Zhou 734b89462a9SDennis Zhou pcpu_block_update(block, s_off, e_off); 735b89462a9SDennis Zhou } 736b89462a9SDennis Zhou 737ca460b3cSDennis Zhou (Facebook) /** 73892c14cabSDennis Zhou * pcpu_chunk_refresh_hint - updates metadata about a chunk 73992c14cabSDennis Zhou * @chunk: chunk of interest 740d33d9f3dSDennis Zhou * @full_scan: if we should scan from the beginning 74192c14cabSDennis Zhou * 74292c14cabSDennis Zhou * Iterates over the metadata blocks to find the largest contig area. 743d33d9f3dSDennis Zhou * A full scan can be avoided on the allocation path as this is triggered 744d33d9f3dSDennis Zhou * if we broke the contig_hint. In doing so, the scan_hint will be before 745d33d9f3dSDennis Zhou * the contig_hint or after if the scan_hint == contig_hint. This cannot 746d33d9f3dSDennis Zhou * be prevented on freeing as we want to find the largest area possibly 747d33d9f3dSDennis Zhou * spanning blocks. 74892c14cabSDennis Zhou */ 749d33d9f3dSDennis Zhou static void pcpu_chunk_refresh_hint(struct pcpu_chunk *chunk, bool full_scan) 75092c14cabSDennis Zhou { 75192c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 75292c14cabSDennis Zhou int bit_off, bits; 75392c14cabSDennis Zhou 754d33d9f3dSDennis Zhou /* promote scan_hint to contig_hint */ 755d33d9f3dSDennis Zhou if (!full_scan && chunk_md->scan_hint) { 756d33d9f3dSDennis Zhou bit_off = chunk_md->scan_hint_start + chunk_md->scan_hint; 757d33d9f3dSDennis Zhou chunk_md->contig_hint_start = chunk_md->scan_hint_start; 758d33d9f3dSDennis Zhou chunk_md->contig_hint = chunk_md->scan_hint; 759d33d9f3dSDennis Zhou chunk_md->scan_hint = 0; 760d33d9f3dSDennis Zhou } else { 76192c14cabSDennis Zhou bit_off = chunk_md->first_free; 762d33d9f3dSDennis Zhou chunk_md->contig_hint = 0; 763d33d9f3dSDennis Zhou } 764d33d9f3dSDennis Zhou 76592c14cabSDennis Zhou bits = 0; 766e837dfdeSDennis Zhou pcpu_for_each_md_free_region(chunk, bit_off, bits) 76792c14cabSDennis Zhou pcpu_block_update(chunk_md, bit_off, bit_off + bits); 768ca460b3cSDennis Zhou (Facebook) } 769ca460b3cSDennis Zhou (Facebook) 770ca460b3cSDennis Zhou (Facebook) /** 771ca460b3cSDennis Zhou (Facebook) * pcpu_block_refresh_hint 772ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 773ca460b3cSDennis Zhou (Facebook) * @index: index of the metadata block 774ca460b3cSDennis Zhou (Facebook) * 775ca460b3cSDennis Zhou (Facebook) * Scans over the block beginning at first_free and updates the block 776ca460b3cSDennis Zhou (Facebook) * metadata accordingly. 777ca460b3cSDennis Zhou (Facebook) */ 778ca460b3cSDennis Zhou (Facebook) static void pcpu_block_refresh_hint(struct pcpu_chunk *chunk, int index) 779ca460b3cSDennis Zhou (Facebook) { 780ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *block = chunk->md_blocks + index; 781ca460b3cSDennis Zhou (Facebook) unsigned long *alloc_map = pcpu_index_alloc_map(chunk, index); 782e837dfdeSDennis Zhou unsigned int rs, re, start; /* region start, region end */ 783ca460b3cSDennis Zhou (Facebook) 784da3afdd5SDennis Zhou /* promote scan_hint to contig_hint */ 785da3afdd5SDennis Zhou if (block->scan_hint) { 786da3afdd5SDennis Zhou start = block->scan_hint_start + block->scan_hint; 787da3afdd5SDennis Zhou block->contig_hint_start = block->scan_hint_start; 788da3afdd5SDennis Zhou block->contig_hint = block->scan_hint; 789da3afdd5SDennis Zhou block->scan_hint = 0; 790da3afdd5SDennis Zhou } else { 791da3afdd5SDennis Zhou start = block->first_free; 792ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 793da3afdd5SDennis Zhou } 794da3afdd5SDennis Zhou 795da3afdd5SDennis Zhou block->right_free = 0; 796ca460b3cSDennis Zhou (Facebook) 797ca460b3cSDennis Zhou (Facebook) /* iterate over free areas and update the contig hints */ 798e837dfdeSDennis Zhou bitmap_for_each_clear_region(alloc_map, rs, re, start, 799e837dfdeSDennis Zhou PCPU_BITMAP_BLOCK_BITS) 800ca460b3cSDennis Zhou (Facebook) pcpu_block_update(block, rs, re); 801ca460b3cSDennis Zhou (Facebook) } 802ca460b3cSDennis Zhou (Facebook) 803ca460b3cSDennis Zhou (Facebook) /** 804ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_alloc - update hint on allocation path 805ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 806ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 807ca460b3cSDennis Zhou (Facebook) * @bits: size of request 808fc304334SDennis Zhou (Facebook) * 809fc304334SDennis Zhou (Facebook) * Updates metadata for the allocation path. The metadata only has to be 810fc304334SDennis Zhou (Facebook) * refreshed by a full scan iff the chunk's contig hint is broken. Block level 811fc304334SDennis Zhou (Facebook) * scans are required if the block's contig hint is broken. 812ca460b3cSDennis Zhou (Facebook) */ 813ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_alloc(struct pcpu_chunk *chunk, int bit_off, 814ca460b3cSDennis Zhou (Facebook) int bits) 815ca460b3cSDennis Zhou (Facebook) { 81692c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 817b239f7daSDennis Zhou int nr_empty_pages = 0; 818ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 819ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 820ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 821ca460b3cSDennis Zhou (Facebook) 822ca460b3cSDennis Zhou (Facebook) /* 823ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 824ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 825ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 826ca460b3cSDennis Zhou (Facebook) * range. 827ca460b3cSDennis Zhou (Facebook) */ 828ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 829ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 830ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 831ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 832ca460b3cSDennis Zhou (Facebook) 833ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 834ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 835ca460b3cSDennis Zhou (Facebook) 836ca460b3cSDennis Zhou (Facebook) /* 837ca460b3cSDennis Zhou (Facebook) * Update s_block. 838fc304334SDennis Zhou (Facebook) * block->first_free must be updated if the allocation takes its place. 839fc304334SDennis Zhou (Facebook) * If the allocation breaks the contig_hint, a scan is required to 840fc304334SDennis Zhou (Facebook) * restore this hint. 841ca460b3cSDennis Zhou (Facebook) */ 842b239f7daSDennis Zhou if (s_block->contig_hint == PCPU_BITMAP_BLOCK_BITS) 843b239f7daSDennis Zhou nr_empty_pages++; 844b239f7daSDennis Zhou 845fc304334SDennis Zhou (Facebook) if (s_off == s_block->first_free) 846fc304334SDennis Zhou (Facebook) s_block->first_free = find_next_zero_bit( 847fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, s_index), 848fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, 849fc304334SDennis Zhou (Facebook) s_off + bits); 850fc304334SDennis Zhou (Facebook) 851382b88e9SDennis Zhou if (pcpu_region_overlap(s_block->scan_hint_start, 852382b88e9SDennis Zhou s_block->scan_hint_start + s_block->scan_hint, 853382b88e9SDennis Zhou s_off, 854382b88e9SDennis Zhou s_off + bits)) 855382b88e9SDennis Zhou s_block->scan_hint = 0; 856382b88e9SDennis Zhou 857d9f3a01eSDennis Zhou if (pcpu_region_overlap(s_block->contig_hint_start, 858d9f3a01eSDennis Zhou s_block->contig_hint_start + 859d9f3a01eSDennis Zhou s_block->contig_hint, 860d9f3a01eSDennis Zhou s_off, 861d9f3a01eSDennis Zhou s_off + bits)) { 862fc304334SDennis Zhou (Facebook) /* block contig hint is broken - scan to fix it */ 863da3afdd5SDennis Zhou if (!s_off) 864da3afdd5SDennis Zhou s_block->left_free = 0; 865ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, s_index); 866fc304334SDennis Zhou (Facebook) } else { 867fc304334SDennis Zhou (Facebook) /* update left and right contig manually */ 868fc304334SDennis Zhou (Facebook) s_block->left_free = min(s_block->left_free, s_off); 869fc304334SDennis Zhou (Facebook) if (s_index == e_index) 870fc304334SDennis Zhou (Facebook) s_block->right_free = min_t(int, s_block->right_free, 871fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 872fc304334SDennis Zhou (Facebook) else 873fc304334SDennis Zhou (Facebook) s_block->right_free = 0; 874fc304334SDennis Zhou (Facebook) } 875ca460b3cSDennis Zhou (Facebook) 876ca460b3cSDennis Zhou (Facebook) /* 877ca460b3cSDennis Zhou (Facebook) * Update e_block. 878ca460b3cSDennis Zhou (Facebook) */ 879ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 880b239f7daSDennis Zhou if (e_block->contig_hint == PCPU_BITMAP_BLOCK_BITS) 881b239f7daSDennis Zhou nr_empty_pages++; 882b239f7daSDennis Zhou 883fc304334SDennis Zhou (Facebook) /* 884fc304334SDennis Zhou (Facebook) * When the allocation is across blocks, the end is along 885fc304334SDennis Zhou (Facebook) * the left part of the e_block. 886fc304334SDennis Zhou (Facebook) */ 887fc304334SDennis Zhou (Facebook) e_block->first_free = find_next_zero_bit( 888fc304334SDennis Zhou (Facebook) pcpu_index_alloc_map(chunk, e_index), 889fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, e_off); 890fc304334SDennis Zhou (Facebook) 891fc304334SDennis Zhou (Facebook) if (e_off == PCPU_BITMAP_BLOCK_BITS) { 892fc304334SDennis Zhou (Facebook) /* reset the block */ 893fc304334SDennis Zhou (Facebook) e_block++; 894fc304334SDennis Zhou (Facebook) } else { 895382b88e9SDennis Zhou if (e_off > e_block->scan_hint_start) 896382b88e9SDennis Zhou e_block->scan_hint = 0; 897382b88e9SDennis Zhou 898da3afdd5SDennis Zhou e_block->left_free = 0; 899fc304334SDennis Zhou (Facebook) if (e_off > e_block->contig_hint_start) { 900fc304334SDennis Zhou (Facebook) /* contig hint is broken - scan to fix it */ 901ca460b3cSDennis Zhou (Facebook) pcpu_block_refresh_hint(chunk, e_index); 902fc304334SDennis Zhou (Facebook) } else { 903fc304334SDennis Zhou (Facebook) e_block->right_free = 904fc304334SDennis Zhou (Facebook) min_t(int, e_block->right_free, 905fc304334SDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS - e_off); 906fc304334SDennis Zhou (Facebook) } 907fc304334SDennis Zhou (Facebook) } 908ca460b3cSDennis Zhou (Facebook) 909ca460b3cSDennis Zhou (Facebook) /* update in-between md_blocks */ 910b239f7daSDennis Zhou nr_empty_pages += (e_index - s_index - 1); 911ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 912382b88e9SDennis Zhou block->scan_hint = 0; 913ca460b3cSDennis Zhou (Facebook) block->contig_hint = 0; 914ca460b3cSDennis Zhou (Facebook) block->left_free = 0; 915ca460b3cSDennis Zhou (Facebook) block->right_free = 0; 916ca460b3cSDennis Zhou (Facebook) } 917ca460b3cSDennis Zhou (Facebook) } 918ca460b3cSDennis Zhou (Facebook) 919b239f7daSDennis Zhou if (nr_empty_pages) 920b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, -nr_empty_pages); 921b239f7daSDennis Zhou 922d33d9f3dSDennis Zhou if (pcpu_region_overlap(chunk_md->scan_hint_start, 923d33d9f3dSDennis Zhou chunk_md->scan_hint_start + 924d33d9f3dSDennis Zhou chunk_md->scan_hint, 925d33d9f3dSDennis Zhou bit_off, 926d33d9f3dSDennis Zhou bit_off + bits)) 927d33d9f3dSDennis Zhou chunk_md->scan_hint = 0; 928d33d9f3dSDennis Zhou 929fc304334SDennis Zhou (Facebook) /* 930fc304334SDennis Zhou (Facebook) * The only time a full chunk scan is required is if the chunk 931fc304334SDennis Zhou (Facebook) * contig hint is broken. Otherwise, it means a smaller space 932fc304334SDennis Zhou (Facebook) * was used and therefore the chunk contig hint is still correct. 933fc304334SDennis Zhou (Facebook) */ 93492c14cabSDennis Zhou if (pcpu_region_overlap(chunk_md->contig_hint_start, 93592c14cabSDennis Zhou chunk_md->contig_hint_start + 93692c14cabSDennis Zhou chunk_md->contig_hint, 937d9f3a01eSDennis Zhou bit_off, 938d9f3a01eSDennis Zhou bit_off + bits)) 939d33d9f3dSDennis Zhou pcpu_chunk_refresh_hint(chunk, false); 940ca460b3cSDennis Zhou (Facebook) } 941ca460b3cSDennis Zhou (Facebook) 942ca460b3cSDennis Zhou (Facebook) /** 943ca460b3cSDennis Zhou (Facebook) * pcpu_block_update_hint_free - updates the block hints on the free path 944ca460b3cSDennis Zhou (Facebook) * @chunk: chunk of interest 945ca460b3cSDennis Zhou (Facebook) * @bit_off: chunk offset 946ca460b3cSDennis Zhou (Facebook) * @bits: size of request 947b185cd0dSDennis Zhou (Facebook) * 948b185cd0dSDennis Zhou (Facebook) * Updates metadata for the allocation path. This avoids a blind block 949b185cd0dSDennis Zhou (Facebook) * refresh by making use of the block contig hints. If this fails, it scans 950b185cd0dSDennis Zhou (Facebook) * forward and backward to determine the extent of the free area. This is 951b185cd0dSDennis Zhou (Facebook) * capped at the boundary of blocks. 952b185cd0dSDennis Zhou (Facebook) * 953b185cd0dSDennis Zhou (Facebook) * A chunk update is triggered if a page becomes free, a block becomes free, 954b185cd0dSDennis Zhou (Facebook) * or the free spans across blocks. This tradeoff is to minimize iterating 95592c14cabSDennis Zhou * over the block metadata to update chunk_md->contig_hint. 95692c14cabSDennis Zhou * chunk_md->contig_hint may be off by up to a page, but it will never be more 95792c14cabSDennis Zhou * than the available space. If the contig hint is contained in one block, it 95892c14cabSDennis Zhou * will be accurate. 959ca460b3cSDennis Zhou (Facebook) */ 960ca460b3cSDennis Zhou (Facebook) static void pcpu_block_update_hint_free(struct pcpu_chunk *chunk, int bit_off, 961ca460b3cSDennis Zhou (Facebook) int bits) 962ca460b3cSDennis Zhou (Facebook) { 963b239f7daSDennis Zhou int nr_empty_pages = 0; 964ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *s_block, *e_block, *block; 965ca460b3cSDennis Zhou (Facebook) int s_index, e_index; /* block indexes of the freed allocation */ 966ca460b3cSDennis Zhou (Facebook) int s_off, e_off; /* block offsets of the freed allocation */ 967b185cd0dSDennis Zhou (Facebook) int start, end; /* start and end of the whole free area */ 968ca460b3cSDennis Zhou (Facebook) 969ca460b3cSDennis Zhou (Facebook) /* 970ca460b3cSDennis Zhou (Facebook) * Calculate per block offsets. 971ca460b3cSDennis Zhou (Facebook) * The calculation uses an inclusive range, but the resulting offsets 972ca460b3cSDennis Zhou (Facebook) * are [start, end). e_index always points to the last block in the 973ca460b3cSDennis Zhou (Facebook) * range. 974ca460b3cSDennis Zhou (Facebook) */ 975ca460b3cSDennis Zhou (Facebook) s_index = pcpu_off_to_block_index(bit_off); 976ca460b3cSDennis Zhou (Facebook) e_index = pcpu_off_to_block_index(bit_off + bits - 1); 977ca460b3cSDennis Zhou (Facebook) s_off = pcpu_off_to_block_off(bit_off); 978ca460b3cSDennis Zhou (Facebook) e_off = pcpu_off_to_block_off(bit_off + bits - 1) + 1; 979ca460b3cSDennis Zhou (Facebook) 980ca460b3cSDennis Zhou (Facebook) s_block = chunk->md_blocks + s_index; 981ca460b3cSDennis Zhou (Facebook) e_block = chunk->md_blocks + e_index; 982ca460b3cSDennis Zhou (Facebook) 983b185cd0dSDennis Zhou (Facebook) /* 984b185cd0dSDennis Zhou (Facebook) * Check if the freed area aligns with the block->contig_hint. 985b185cd0dSDennis Zhou (Facebook) * If it does, then the scan to find the beginning/end of the 986b185cd0dSDennis Zhou (Facebook) * larger free area can be avoided. 987b185cd0dSDennis Zhou (Facebook) * 988b185cd0dSDennis Zhou (Facebook) * start and end refer to beginning and end of the free area 989b185cd0dSDennis Zhou (Facebook) * within each their respective blocks. This is not necessarily 990b185cd0dSDennis Zhou (Facebook) * the entire free area as it may span blocks past the beginning 991b185cd0dSDennis Zhou (Facebook) * or end of the block. 992b185cd0dSDennis Zhou (Facebook) */ 993b185cd0dSDennis Zhou (Facebook) start = s_off; 994b185cd0dSDennis Zhou (Facebook) if (s_off == s_block->contig_hint + s_block->contig_hint_start) { 995b185cd0dSDennis Zhou (Facebook) start = s_block->contig_hint_start; 996b185cd0dSDennis Zhou (Facebook) } else { 997b185cd0dSDennis Zhou (Facebook) /* 998b185cd0dSDennis Zhou (Facebook) * Scan backwards to find the extent of the free area. 999b185cd0dSDennis Zhou (Facebook) * find_last_bit returns the starting bit, so if the start bit 1000b185cd0dSDennis Zhou (Facebook) * is returned, that means there was no last bit and the 1001b185cd0dSDennis Zhou (Facebook) * remainder of the chunk is free. 1002b185cd0dSDennis Zhou (Facebook) */ 1003b185cd0dSDennis Zhou (Facebook) int l_bit = find_last_bit(pcpu_index_alloc_map(chunk, s_index), 1004b185cd0dSDennis Zhou (Facebook) start); 1005b185cd0dSDennis Zhou (Facebook) start = (start == l_bit) ? 0 : l_bit + 1; 1006b185cd0dSDennis Zhou (Facebook) } 1007b185cd0dSDennis Zhou (Facebook) 1008b185cd0dSDennis Zhou (Facebook) end = e_off; 1009b185cd0dSDennis Zhou (Facebook) if (e_off == e_block->contig_hint_start) 1010b185cd0dSDennis Zhou (Facebook) end = e_block->contig_hint_start + e_block->contig_hint; 1011b185cd0dSDennis Zhou (Facebook) else 1012b185cd0dSDennis Zhou (Facebook) end = find_next_bit(pcpu_index_alloc_map(chunk, e_index), 1013b185cd0dSDennis Zhou (Facebook) PCPU_BITMAP_BLOCK_BITS, end); 1014b185cd0dSDennis Zhou (Facebook) 1015ca460b3cSDennis Zhou (Facebook) /* update s_block */ 1016b185cd0dSDennis Zhou (Facebook) e_off = (s_index == e_index) ? end : PCPU_BITMAP_BLOCK_BITS; 1017b239f7daSDennis Zhou if (!start && e_off == PCPU_BITMAP_BLOCK_BITS) 1018b239f7daSDennis Zhou nr_empty_pages++; 1019b185cd0dSDennis Zhou (Facebook) pcpu_block_update(s_block, start, e_off); 1020ca460b3cSDennis Zhou (Facebook) 1021ca460b3cSDennis Zhou (Facebook) /* freeing in the same block */ 1022ca460b3cSDennis Zhou (Facebook) if (s_index != e_index) { 1023ca460b3cSDennis Zhou (Facebook) /* update e_block */ 1024b239f7daSDennis Zhou if (end == PCPU_BITMAP_BLOCK_BITS) 1025b239f7daSDennis Zhou nr_empty_pages++; 1026b185cd0dSDennis Zhou (Facebook) pcpu_block_update(e_block, 0, end); 1027ca460b3cSDennis Zhou (Facebook) 1028ca460b3cSDennis Zhou (Facebook) /* reset md_blocks in the middle */ 1029b239f7daSDennis Zhou nr_empty_pages += (e_index - s_index - 1); 1030ca460b3cSDennis Zhou (Facebook) for (block = s_block + 1; block < e_block; block++) { 1031ca460b3cSDennis Zhou (Facebook) block->first_free = 0; 1032382b88e9SDennis Zhou block->scan_hint = 0; 1033ca460b3cSDennis Zhou (Facebook) block->contig_hint_start = 0; 1034ca460b3cSDennis Zhou (Facebook) block->contig_hint = PCPU_BITMAP_BLOCK_BITS; 1035ca460b3cSDennis Zhou (Facebook) block->left_free = PCPU_BITMAP_BLOCK_BITS; 1036ca460b3cSDennis Zhou (Facebook) block->right_free = PCPU_BITMAP_BLOCK_BITS; 1037ca460b3cSDennis Zhou (Facebook) } 1038ca460b3cSDennis Zhou (Facebook) } 1039ca460b3cSDennis Zhou (Facebook) 1040b239f7daSDennis Zhou if (nr_empty_pages) 1041b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, nr_empty_pages); 1042b239f7daSDennis Zhou 1043b185cd0dSDennis Zhou (Facebook) /* 1044b239f7daSDennis Zhou * Refresh chunk metadata when the free makes a block free or spans 1045b239f7daSDennis Zhou * across blocks. The contig_hint may be off by up to a page, but if 1046b239f7daSDennis Zhou * the contig_hint is contained in a block, it will be accurate with 1047b239f7daSDennis Zhou * the else condition below. 1048b185cd0dSDennis Zhou (Facebook) */ 1049b239f7daSDennis Zhou if (((end - start) >= PCPU_BITMAP_BLOCK_BITS) || s_index != e_index) 1050d33d9f3dSDennis Zhou pcpu_chunk_refresh_hint(chunk, true); 1051b185cd0dSDennis Zhou (Facebook) else 105292c14cabSDennis Zhou pcpu_block_update(&chunk->chunk_md, 105392c14cabSDennis Zhou pcpu_block_off_to_off(s_index, start), 105492c14cabSDennis Zhou end); 1055ca460b3cSDennis Zhou (Facebook) } 1056ca460b3cSDennis Zhou (Facebook) 1057ca460b3cSDennis Zhou (Facebook) /** 105840064aecSDennis Zhou (Facebook) * pcpu_is_populated - determines if the region is populated 105940064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 106040064aecSDennis Zhou (Facebook) * @bit_off: chunk offset 106140064aecSDennis Zhou (Facebook) * @bits: size of area 106240064aecSDennis Zhou (Facebook) * @next_off: return value for the next offset to start searching 106340064aecSDennis Zhou (Facebook) * 106440064aecSDennis Zhou (Facebook) * For atomic allocations, check if the backing pages are populated. 106540064aecSDennis Zhou (Facebook) * 106640064aecSDennis Zhou (Facebook) * RETURNS: 106740064aecSDennis Zhou (Facebook) * Bool if the backing pages are populated. 106840064aecSDennis Zhou (Facebook) * next_index is to skip over unpopulated blocks in pcpu_find_block_fit. 106940064aecSDennis Zhou (Facebook) */ 107040064aecSDennis Zhou (Facebook) static bool pcpu_is_populated(struct pcpu_chunk *chunk, int bit_off, int bits, 107140064aecSDennis Zhou (Facebook) int *next_off) 107240064aecSDennis Zhou (Facebook) { 1073e837dfdeSDennis Zhou unsigned int page_start, page_end, rs, re; 107440064aecSDennis Zhou (Facebook) 107540064aecSDennis Zhou (Facebook) page_start = PFN_DOWN(bit_off * PCPU_MIN_ALLOC_SIZE); 107640064aecSDennis Zhou (Facebook) page_end = PFN_UP((bit_off + bits) * PCPU_MIN_ALLOC_SIZE); 107740064aecSDennis Zhou (Facebook) 107840064aecSDennis Zhou (Facebook) rs = page_start; 1079e837dfdeSDennis Zhou bitmap_next_clear_region(chunk->populated, &rs, &re, page_end); 108040064aecSDennis Zhou (Facebook) if (rs >= page_end) 108140064aecSDennis Zhou (Facebook) return true; 108240064aecSDennis Zhou (Facebook) 108340064aecSDennis Zhou (Facebook) *next_off = re * PAGE_SIZE / PCPU_MIN_ALLOC_SIZE; 108440064aecSDennis Zhou (Facebook) return false; 108540064aecSDennis Zhou (Facebook) } 108640064aecSDennis Zhou (Facebook) 108740064aecSDennis Zhou (Facebook) /** 108840064aecSDennis Zhou (Facebook) * pcpu_find_block_fit - finds the block index to start searching 108940064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 109040064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 109140064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE bytes) 109240064aecSDennis Zhou (Facebook) * @pop_only: use populated regions only 109340064aecSDennis Zhou (Facebook) * 1094b4c2116cSDennis Zhou (Facebook) * Given a chunk and an allocation spec, find the offset to begin searching 1095b4c2116cSDennis Zhou (Facebook) * for a free region. This iterates over the bitmap metadata blocks to 1096b4c2116cSDennis Zhou (Facebook) * find an offset that will be guaranteed to fit the requirements. It is 1097b4c2116cSDennis Zhou (Facebook) * not quite first fit as if the allocation does not fit in the contig hint 1098b4c2116cSDennis Zhou (Facebook) * of a block or chunk, it is skipped. This errs on the side of caution 1099b4c2116cSDennis Zhou (Facebook) * to prevent excess iteration. Poor alignment can cause the allocator to 1100b4c2116cSDennis Zhou (Facebook) * skip over blocks and chunks that have valid free areas. 1101b4c2116cSDennis Zhou (Facebook) * 110240064aecSDennis Zhou (Facebook) * RETURNS: 110340064aecSDennis Zhou (Facebook) * The offset in the bitmap to begin searching. 110440064aecSDennis Zhou (Facebook) * -1 if no offset is found. 110540064aecSDennis Zhou (Facebook) */ 110640064aecSDennis Zhou (Facebook) static int pcpu_find_block_fit(struct pcpu_chunk *chunk, int alloc_bits, 110740064aecSDennis Zhou (Facebook) size_t align, bool pop_only) 110840064aecSDennis Zhou (Facebook) { 110992c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 1110b4c2116cSDennis Zhou (Facebook) int bit_off, bits, next_off; 111140064aecSDennis Zhou (Facebook) 111213f96637SDennis Zhou (Facebook) /* 11138ea2e1e3SRoman Gushchin * This is an optimization to prevent scanning by assuming if the 11148ea2e1e3SRoman Gushchin * allocation cannot fit in the global hint, there is memory pressure 11158ea2e1e3SRoman Gushchin * and creating a new chunk would happen soon. 111613f96637SDennis Zhou (Facebook) */ 11178ea2e1e3SRoman Gushchin if (!pcpu_check_block_hint(chunk_md, alloc_bits, align)) 111813f96637SDennis Zhou (Facebook) return -1; 111913f96637SDennis Zhou (Facebook) 1120d33d9f3dSDennis Zhou bit_off = pcpu_next_hint(chunk_md, alloc_bits); 1121b4c2116cSDennis Zhou (Facebook) bits = 0; 1122b4c2116cSDennis Zhou (Facebook) pcpu_for_each_fit_region(chunk, alloc_bits, align, bit_off, bits) { 112340064aecSDennis Zhou (Facebook) if (!pop_only || pcpu_is_populated(chunk, bit_off, bits, 1124b4c2116cSDennis Zhou (Facebook) &next_off)) 112540064aecSDennis Zhou (Facebook) break; 112640064aecSDennis Zhou (Facebook) 1127b4c2116cSDennis Zhou (Facebook) bit_off = next_off; 112840064aecSDennis Zhou (Facebook) bits = 0; 112940064aecSDennis Zhou (Facebook) } 113040064aecSDennis Zhou (Facebook) 113140064aecSDennis Zhou (Facebook) if (bit_off == pcpu_chunk_map_bits(chunk)) 113240064aecSDennis Zhou (Facebook) return -1; 113340064aecSDennis Zhou (Facebook) 113440064aecSDennis Zhou (Facebook) return bit_off; 113540064aecSDennis Zhou (Facebook) } 113640064aecSDennis Zhou (Facebook) 1137b89462a9SDennis Zhou /* 1138b89462a9SDennis Zhou * pcpu_find_zero_area - modified from bitmap_find_next_zero_area_off() 1139b89462a9SDennis Zhou * @map: the address to base the search on 1140b89462a9SDennis Zhou * @size: the bitmap size in bits 1141b89462a9SDennis Zhou * @start: the bitnumber to start searching at 1142b89462a9SDennis Zhou * @nr: the number of zeroed bits we're looking for 1143b89462a9SDennis Zhou * @align_mask: alignment mask for zero area 1144b89462a9SDennis Zhou * @largest_off: offset of the largest area skipped 1145b89462a9SDennis Zhou * @largest_bits: size of the largest area skipped 1146b89462a9SDennis Zhou * 1147b89462a9SDennis Zhou * The @align_mask should be one less than a power of 2. 1148b89462a9SDennis Zhou * 1149b89462a9SDennis Zhou * This is a modified version of bitmap_find_next_zero_area_off() to remember 1150b89462a9SDennis Zhou * the largest area that was skipped. This is imperfect, but in general is 1151b89462a9SDennis Zhou * good enough. The largest remembered region is the largest failed region 1152b89462a9SDennis Zhou * seen. This does not include anything we possibly skipped due to alignment. 1153b89462a9SDennis Zhou * pcpu_block_update_scan() does scan backwards to try and recover what was 1154b89462a9SDennis Zhou * lost to alignment. While this can cause scanning to miss earlier possible 1155b89462a9SDennis Zhou * free areas, smaller allocations will eventually fill those holes. 1156b89462a9SDennis Zhou */ 1157b89462a9SDennis Zhou static unsigned long pcpu_find_zero_area(unsigned long *map, 1158b89462a9SDennis Zhou unsigned long size, 1159b89462a9SDennis Zhou unsigned long start, 1160b89462a9SDennis Zhou unsigned long nr, 1161b89462a9SDennis Zhou unsigned long align_mask, 1162b89462a9SDennis Zhou unsigned long *largest_off, 1163b89462a9SDennis Zhou unsigned long *largest_bits) 1164b89462a9SDennis Zhou { 1165b89462a9SDennis Zhou unsigned long index, end, i, area_off, area_bits; 1166b89462a9SDennis Zhou again: 1167b89462a9SDennis Zhou index = find_next_zero_bit(map, size, start); 1168b89462a9SDennis Zhou 1169b89462a9SDennis Zhou /* Align allocation */ 1170b89462a9SDennis Zhou index = __ALIGN_MASK(index, align_mask); 1171b89462a9SDennis Zhou area_off = index; 1172b89462a9SDennis Zhou 1173b89462a9SDennis Zhou end = index + nr; 1174b89462a9SDennis Zhou if (end > size) 1175b89462a9SDennis Zhou return end; 1176b89462a9SDennis Zhou i = find_next_bit(map, end, index); 1177b89462a9SDennis Zhou if (i < end) { 1178b89462a9SDennis Zhou area_bits = i - area_off; 1179b89462a9SDennis Zhou /* remember largest unused area with best alignment */ 1180b89462a9SDennis Zhou if (area_bits > *largest_bits || 1181b89462a9SDennis Zhou (area_bits == *largest_bits && *largest_off && 1182b89462a9SDennis Zhou (!area_off || __ffs(area_off) > __ffs(*largest_off)))) { 1183b89462a9SDennis Zhou *largest_off = area_off; 1184b89462a9SDennis Zhou *largest_bits = area_bits; 1185b89462a9SDennis Zhou } 1186b89462a9SDennis Zhou 1187b89462a9SDennis Zhou start = i + 1; 1188b89462a9SDennis Zhou goto again; 1189b89462a9SDennis Zhou } 1190b89462a9SDennis Zhou return index; 1191b89462a9SDennis Zhou } 1192b89462a9SDennis Zhou 119340064aecSDennis Zhou (Facebook) /** 119440064aecSDennis Zhou (Facebook) * pcpu_alloc_area - allocates an area from a pcpu_chunk 119540064aecSDennis Zhou (Facebook) * @chunk: chunk of interest 119640064aecSDennis Zhou (Facebook) * @alloc_bits: size of request in allocation units 119740064aecSDennis Zhou (Facebook) * @align: alignment of area (max PAGE_SIZE) 119840064aecSDennis Zhou (Facebook) * @start: bit_off to start searching 119940064aecSDennis Zhou (Facebook) * 120040064aecSDennis Zhou (Facebook) * This function takes in a @start offset to begin searching to fit an 1201b4c2116cSDennis Zhou (Facebook) * allocation of @alloc_bits with alignment @align. It needs to scan 1202b4c2116cSDennis Zhou (Facebook) * the allocation map because if it fits within the block's contig hint, 1203b4c2116cSDennis Zhou (Facebook) * @start will be block->first_free. This is an attempt to fill the 1204b4c2116cSDennis Zhou (Facebook) * allocation prior to breaking the contig hint. The allocation and 1205b4c2116cSDennis Zhou (Facebook) * boundary maps are updated accordingly if it confirms a valid 1206b4c2116cSDennis Zhou (Facebook) * free area. 120740064aecSDennis Zhou (Facebook) * 120840064aecSDennis Zhou (Facebook) * RETURNS: 120940064aecSDennis Zhou (Facebook) * Allocated addr offset in @chunk on success. 121040064aecSDennis Zhou (Facebook) * -1 if no matching area is found. 121140064aecSDennis Zhou (Facebook) */ 121240064aecSDennis Zhou (Facebook) static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits, 121340064aecSDennis Zhou (Facebook) size_t align, int start) 121440064aecSDennis Zhou (Facebook) { 121592c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 121640064aecSDennis Zhou (Facebook) size_t align_mask = (align) ? (align - 1) : 0; 1217b89462a9SDennis Zhou unsigned long area_off = 0, area_bits = 0; 121840064aecSDennis Zhou (Facebook) int bit_off, end, oslot; 12199f7dcf22STejun Heo 12204f996e23STejun Heo lockdep_assert_held(&pcpu_lock); 12214f996e23STejun Heo 122240064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 1223833af842STejun Heo 1224833af842STejun Heo /* 122540064aecSDennis Zhou (Facebook) * Search to find a fit. 1226833af842STejun Heo */ 12278c43004aSDennis Zhou end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS, 12288c43004aSDennis Zhou pcpu_chunk_map_bits(chunk)); 1229b89462a9SDennis Zhou bit_off = pcpu_find_zero_area(chunk->alloc_map, end, start, alloc_bits, 1230b89462a9SDennis Zhou align_mask, &area_off, &area_bits); 123140064aecSDennis Zhou (Facebook) if (bit_off >= end) 1232a16037c8STejun Heo return -1; 1233a16037c8STejun Heo 1234b89462a9SDennis Zhou if (area_bits) 1235b89462a9SDennis Zhou pcpu_block_update_scan(chunk, area_off, area_bits); 1236b89462a9SDennis Zhou 123740064aecSDennis Zhou (Facebook) /* update alloc map */ 123840064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, bit_off, alloc_bits); 1239a16037c8STejun Heo 124040064aecSDennis Zhou (Facebook) /* update boundary map */ 124140064aecSDennis Zhou (Facebook) set_bit(bit_off, chunk->bound_map); 124240064aecSDennis Zhou (Facebook) bitmap_clear(chunk->bound_map, bit_off + 1, alloc_bits - 1); 124340064aecSDennis Zhou (Facebook) set_bit(bit_off + alloc_bits, chunk->bound_map); 1244a16037c8STejun Heo 124540064aecSDennis Zhou (Facebook) chunk->free_bytes -= alloc_bits * PCPU_MIN_ALLOC_SIZE; 124640064aecSDennis Zhou (Facebook) 124786b442fbSDennis Zhou (Facebook) /* update first free bit */ 124892c14cabSDennis Zhou if (bit_off == chunk_md->first_free) 124992c14cabSDennis Zhou chunk_md->first_free = find_next_zero_bit( 125086b442fbSDennis Zhou (Facebook) chunk->alloc_map, 125186b442fbSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk), 125286b442fbSDennis Zhou (Facebook) bit_off + alloc_bits); 125386b442fbSDennis Zhou (Facebook) 1254ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, bit_off, alloc_bits); 125540064aecSDennis Zhou (Facebook) 125640064aecSDennis Zhou (Facebook) pcpu_chunk_relocate(chunk, oslot); 125740064aecSDennis Zhou (Facebook) 125840064aecSDennis Zhou (Facebook) return bit_off * PCPU_MIN_ALLOC_SIZE; 1259a16037c8STejun Heo } 1260a16037c8STejun Heo 1261a16037c8STejun Heo /** 126240064aecSDennis Zhou (Facebook) * pcpu_free_area - frees the corresponding offset 1263fbf59bc9STejun Heo * @chunk: chunk of interest 126440064aecSDennis Zhou (Facebook) * @off: addr offset into chunk 1265fbf59bc9STejun Heo * 126640064aecSDennis Zhou (Facebook) * This function determines the size of an allocation to free using 126740064aecSDennis Zhou (Facebook) * the boundary bitmap and clears the allocation map. 12685b32af91SRoman Gushchin * 12695b32af91SRoman Gushchin * RETURNS: 12705b32af91SRoman Gushchin * Number of freed bytes. 1271fbf59bc9STejun Heo */ 12725b32af91SRoman Gushchin static int pcpu_free_area(struct pcpu_chunk *chunk, int off) 1273fbf59bc9STejun Heo { 127492c14cabSDennis Zhou struct pcpu_block_md *chunk_md = &chunk->chunk_md; 12755b32af91SRoman Gushchin int bit_off, bits, end, oslot, freed; 1276fbf59bc9STejun Heo 12775ccd30e4SDennis Zhou lockdep_assert_held(&pcpu_lock); 127830a5b536SDennis Zhou pcpu_stats_area_dealloc(chunk); 12795ccd30e4SDennis Zhou 128040064aecSDennis Zhou (Facebook) oslot = pcpu_chunk_slot(chunk); 1281723ad1d9SAl Viro 128240064aecSDennis Zhou (Facebook) bit_off = off / PCPU_MIN_ALLOC_SIZE; 1283fbf59bc9STejun Heo 128440064aecSDennis Zhou (Facebook) /* find end index */ 128540064aecSDennis Zhou (Facebook) end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), 128640064aecSDennis Zhou (Facebook) bit_off + 1); 128740064aecSDennis Zhou (Facebook) bits = end - bit_off; 128840064aecSDennis Zhou (Facebook) bitmap_clear(chunk->alloc_map, bit_off, bits); 12893d331ad7SAl Viro 12905b32af91SRoman Gushchin freed = bits * PCPU_MIN_ALLOC_SIZE; 12915b32af91SRoman Gushchin 129240064aecSDennis Zhou (Facebook) /* update metadata */ 12935b32af91SRoman Gushchin chunk->free_bytes += freed; 1294fbf59bc9STejun Heo 129586b442fbSDennis Zhou (Facebook) /* update first free bit */ 129692c14cabSDennis Zhou chunk_md->first_free = min(chunk_md->first_free, bit_off); 129786b442fbSDennis Zhou (Facebook) 1298ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_free(chunk, bit_off, bits); 1299b539b87fSTejun Heo 1300fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, oslot); 13015b32af91SRoman Gushchin 13025b32af91SRoman Gushchin return freed; 1303fbf59bc9STejun Heo } 1304fbf59bc9STejun Heo 1305047924c9SDennis Zhou static void pcpu_init_md_block(struct pcpu_block_md *block, int nr_bits) 1306047924c9SDennis Zhou { 1307047924c9SDennis Zhou block->scan_hint = 0; 1308047924c9SDennis Zhou block->contig_hint = nr_bits; 1309047924c9SDennis Zhou block->left_free = nr_bits; 1310047924c9SDennis Zhou block->right_free = nr_bits; 1311047924c9SDennis Zhou block->first_free = 0; 1312047924c9SDennis Zhou block->nr_bits = nr_bits; 1313047924c9SDennis Zhou } 1314047924c9SDennis Zhou 1315ca460b3cSDennis Zhou (Facebook) static void pcpu_init_md_blocks(struct pcpu_chunk *chunk) 1316ca460b3cSDennis Zhou (Facebook) { 1317ca460b3cSDennis Zhou (Facebook) struct pcpu_block_md *md_block; 1318ca460b3cSDennis Zhou (Facebook) 131992c14cabSDennis Zhou /* init the chunk's block */ 132092c14cabSDennis Zhou pcpu_init_md_block(&chunk->chunk_md, pcpu_chunk_map_bits(chunk)); 132192c14cabSDennis Zhou 1322ca460b3cSDennis Zhou (Facebook) for (md_block = chunk->md_blocks; 1323ca460b3cSDennis Zhou (Facebook) md_block != chunk->md_blocks + pcpu_chunk_nr_blocks(chunk); 1324047924c9SDennis Zhou md_block++) 1325047924c9SDennis Zhou pcpu_init_md_block(md_block, PCPU_BITMAP_BLOCK_BITS); 1326ca460b3cSDennis Zhou (Facebook) } 1327ca460b3cSDennis Zhou (Facebook) 132840064aecSDennis Zhou (Facebook) /** 132940064aecSDennis Zhou (Facebook) * pcpu_alloc_first_chunk - creates chunks that serve the first chunk 133040064aecSDennis Zhou (Facebook) * @tmp_addr: the start of the region served 133140064aecSDennis Zhou (Facebook) * @map_size: size of the region served 133240064aecSDennis Zhou (Facebook) * 133340064aecSDennis Zhou (Facebook) * This is responsible for creating the chunks that serve the first chunk. The 133440064aecSDennis Zhou (Facebook) * base_addr is page aligned down of @tmp_addr while the region end is page 133540064aecSDennis Zhou (Facebook) * aligned up. Offsets are kept track of to determine the region served. All 133640064aecSDennis Zhou (Facebook) * this is done to appease the bitmap allocator in avoiding partial blocks. 133740064aecSDennis Zhou (Facebook) * 133840064aecSDennis Zhou (Facebook) * RETURNS: 133940064aecSDennis Zhou (Facebook) * Chunk serving the region at @tmp_addr of @map_size. 134040064aecSDennis Zhou (Facebook) */ 1341c0ebfdc3SDennis Zhou (Facebook) static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr, 134240064aecSDennis Zhou (Facebook) int map_size) 134310edf5b0SDennis Zhou (Facebook) { 134410edf5b0SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 1345ca460b3cSDennis Zhou (Facebook) unsigned long aligned_addr, lcm_align; 134640064aecSDennis Zhou (Facebook) int start_offset, offset_bits, region_size, region_bits; 1347f655f405SMike Rapoport size_t alloc_size; 1348c0ebfdc3SDennis Zhou (Facebook) 1349c0ebfdc3SDennis Zhou (Facebook) /* region calculations */ 1350c0ebfdc3SDennis Zhou (Facebook) aligned_addr = tmp_addr & PAGE_MASK; 1351c0ebfdc3SDennis Zhou (Facebook) 1352c0ebfdc3SDennis Zhou (Facebook) start_offset = tmp_addr - aligned_addr; 13536b9d7c8eSDennis Zhou (Facebook) 1354ca460b3cSDennis Zhou (Facebook) /* 1355ca460b3cSDennis Zhou (Facebook) * Align the end of the region with the LCM of PAGE_SIZE and 1356ca460b3cSDennis Zhou (Facebook) * PCPU_BITMAP_BLOCK_SIZE. One of these constants is a multiple of 1357ca460b3cSDennis Zhou (Facebook) * the other. 1358ca460b3cSDennis Zhou (Facebook) */ 1359ca460b3cSDennis Zhou (Facebook) lcm_align = lcm(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE); 1360ca460b3cSDennis Zhou (Facebook) region_size = ALIGN(start_offset + map_size, lcm_align); 136110edf5b0SDennis Zhou (Facebook) 1362c0ebfdc3SDennis Zhou (Facebook) /* allocate chunk */ 136361cf93d3SDennis Zhou alloc_size = struct_size(chunk, populated, 136461cf93d3SDennis Zhou BITS_TO_LONGS(region_size >> PAGE_SHIFT)); 1365f655f405SMike Rapoport chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1366f655f405SMike Rapoport if (!chunk) 1367f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1368f655f405SMike Rapoport alloc_size); 1369c0ebfdc3SDennis Zhou (Facebook) 137010edf5b0SDennis Zhou (Facebook) INIT_LIST_HEAD(&chunk->list); 1371c0ebfdc3SDennis Zhou (Facebook) 1372c0ebfdc3SDennis Zhou (Facebook) chunk->base_addr = (void *)aligned_addr; 137310edf5b0SDennis Zhou (Facebook) chunk->start_offset = start_offset; 13746b9d7c8eSDennis Zhou (Facebook) chunk->end_offset = region_size - chunk->start_offset - map_size; 1375c0ebfdc3SDennis Zhou (Facebook) 13768ab16c43SDennis Zhou (Facebook) chunk->nr_pages = region_size >> PAGE_SHIFT; 137740064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 1378c0ebfdc3SDennis Zhou (Facebook) 1379f655f405SMike Rapoport alloc_size = BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]); 1380f655f405SMike Rapoport chunk->alloc_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1381f655f405SMike Rapoport if (!chunk->alloc_map) 1382f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1383f655f405SMike Rapoport alloc_size); 1384f655f405SMike Rapoport 1385f655f405SMike Rapoport alloc_size = 1386f655f405SMike Rapoport BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]); 1387f655f405SMike Rapoport chunk->bound_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1388f655f405SMike Rapoport if (!chunk->bound_map) 1389f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1390f655f405SMike Rapoport alloc_size); 1391f655f405SMike Rapoport 1392f655f405SMike Rapoport alloc_size = pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]); 1393f655f405SMike Rapoport chunk->md_blocks = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 1394f655f405SMike Rapoport if (!chunk->md_blocks) 1395f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 1396f655f405SMike Rapoport alloc_size); 1397f655f405SMike Rapoport 13983c7be18aSRoman Gushchin #ifdef CONFIG_MEMCG_KMEM 1399faf65ddeSRoman Gushchin /* first chunk is free to use */ 14003c7be18aSRoman Gushchin chunk->obj_cgroups = NULL; 14013c7be18aSRoman Gushchin #endif 1402ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 140310edf5b0SDennis Zhou (Facebook) 140410edf5b0SDennis Zhou (Facebook) /* manage populated page bitmap */ 140510edf5b0SDennis Zhou (Facebook) chunk->immutable = true; 14068ab16c43SDennis Zhou (Facebook) bitmap_fill(chunk->populated, chunk->nr_pages); 14078ab16c43SDennis Zhou (Facebook) chunk->nr_populated = chunk->nr_pages; 1408b239f7daSDennis Zhou chunk->nr_empty_pop_pages = chunk->nr_pages; 140910edf5b0SDennis Zhou (Facebook) 141040064aecSDennis Zhou (Facebook) chunk->free_bytes = map_size; 1411c0ebfdc3SDennis Zhou (Facebook) 1412c0ebfdc3SDennis Zhou (Facebook) if (chunk->start_offset) { 1413c0ebfdc3SDennis Zhou (Facebook) /* hide the beginning of the bitmap */ 141440064aecSDennis Zhou (Facebook) offset_bits = chunk->start_offset / PCPU_MIN_ALLOC_SIZE; 141540064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 0, offset_bits); 141640064aecSDennis Zhou (Facebook) set_bit(0, chunk->bound_map); 141740064aecSDennis Zhou (Facebook) set_bit(offset_bits, chunk->bound_map); 1418ca460b3cSDennis Zhou (Facebook) 141992c14cabSDennis Zhou chunk->chunk_md.first_free = offset_bits; 142086b442fbSDennis Zhou (Facebook) 1421ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, 0, offset_bits); 1422c0ebfdc3SDennis Zhou (Facebook) } 1423c0ebfdc3SDennis Zhou (Facebook) 14246b9d7c8eSDennis Zhou (Facebook) if (chunk->end_offset) { 14256b9d7c8eSDennis Zhou (Facebook) /* hide the end of the bitmap */ 142640064aecSDennis Zhou (Facebook) offset_bits = chunk->end_offset / PCPU_MIN_ALLOC_SIZE; 142740064aecSDennis Zhou (Facebook) bitmap_set(chunk->alloc_map, 142840064aecSDennis Zhou (Facebook) pcpu_chunk_map_bits(chunk) - offset_bits, 142940064aecSDennis Zhou (Facebook) offset_bits); 143040064aecSDennis Zhou (Facebook) set_bit((start_offset + map_size) / PCPU_MIN_ALLOC_SIZE, 143140064aecSDennis Zhou (Facebook) chunk->bound_map); 143240064aecSDennis Zhou (Facebook) set_bit(region_bits, chunk->bound_map); 14336b9d7c8eSDennis Zhou (Facebook) 1434ca460b3cSDennis Zhou (Facebook) pcpu_block_update_hint_alloc(chunk, pcpu_chunk_map_bits(chunk) 1435ca460b3cSDennis Zhou (Facebook) - offset_bits, offset_bits); 1436ca460b3cSDennis Zhou (Facebook) } 143740064aecSDennis Zhou (Facebook) 143810edf5b0SDennis Zhou (Facebook) return chunk; 143910edf5b0SDennis Zhou (Facebook) } 144010edf5b0SDennis Zhou (Facebook) 1441faf65ddeSRoman Gushchin static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp) 14426081089fSTejun Heo { 14436081089fSTejun Heo struct pcpu_chunk *chunk; 144440064aecSDennis Zhou (Facebook) int region_bits; 14456081089fSTejun Heo 144647504ee0SDennis Zhou chunk = pcpu_mem_zalloc(pcpu_chunk_struct_size, gfp); 14476081089fSTejun Heo if (!chunk) 14486081089fSTejun Heo return NULL; 14496081089fSTejun Heo 14506081089fSTejun Heo INIT_LIST_HEAD(&chunk->list); 1451c0ebfdc3SDennis Zhou (Facebook) chunk->nr_pages = pcpu_unit_pages; 145240064aecSDennis Zhou (Facebook) region_bits = pcpu_chunk_map_bits(chunk); 145340064aecSDennis Zhou (Facebook) 145440064aecSDennis Zhou (Facebook) chunk->alloc_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits) * 145547504ee0SDennis Zhou sizeof(chunk->alloc_map[0]), gfp); 145640064aecSDennis Zhou (Facebook) if (!chunk->alloc_map) 145740064aecSDennis Zhou (Facebook) goto alloc_map_fail; 145840064aecSDennis Zhou (Facebook) 145940064aecSDennis Zhou (Facebook) chunk->bound_map = pcpu_mem_zalloc(BITS_TO_LONGS(region_bits + 1) * 146047504ee0SDennis Zhou sizeof(chunk->bound_map[0]), gfp); 146140064aecSDennis Zhou (Facebook) if (!chunk->bound_map) 146240064aecSDennis Zhou (Facebook) goto bound_map_fail; 146340064aecSDennis Zhou (Facebook) 1464ca460b3cSDennis Zhou (Facebook) chunk->md_blocks = pcpu_mem_zalloc(pcpu_chunk_nr_blocks(chunk) * 146547504ee0SDennis Zhou sizeof(chunk->md_blocks[0]), gfp); 1466ca460b3cSDennis Zhou (Facebook) if (!chunk->md_blocks) 1467ca460b3cSDennis Zhou (Facebook) goto md_blocks_fail; 1468ca460b3cSDennis Zhou (Facebook) 14693c7be18aSRoman Gushchin #ifdef CONFIG_MEMCG_KMEM 1470faf65ddeSRoman Gushchin if (!mem_cgroup_kmem_disabled()) { 14713c7be18aSRoman Gushchin chunk->obj_cgroups = 14723c7be18aSRoman Gushchin pcpu_mem_zalloc(pcpu_chunk_map_bits(chunk) * 14733c7be18aSRoman Gushchin sizeof(struct obj_cgroup *), gfp); 14743c7be18aSRoman Gushchin if (!chunk->obj_cgroups) 14753c7be18aSRoman Gushchin goto objcg_fail; 14763c7be18aSRoman Gushchin } 14773c7be18aSRoman Gushchin #endif 14783c7be18aSRoman Gushchin 1479ca460b3cSDennis Zhou (Facebook) pcpu_init_md_blocks(chunk); 1480ca460b3cSDennis Zhou (Facebook) 148140064aecSDennis Zhou (Facebook) /* init metadata */ 148240064aecSDennis Zhou (Facebook) chunk->free_bytes = chunk->nr_pages * PAGE_SIZE; 1483c0ebfdc3SDennis Zhou (Facebook) 14846081089fSTejun Heo return chunk; 148540064aecSDennis Zhou (Facebook) 14863c7be18aSRoman Gushchin #ifdef CONFIG_MEMCG_KMEM 14873c7be18aSRoman Gushchin objcg_fail: 14883c7be18aSRoman Gushchin pcpu_mem_free(chunk->md_blocks); 14893c7be18aSRoman Gushchin #endif 1490ca460b3cSDennis Zhou (Facebook) md_blocks_fail: 1491ca460b3cSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 149240064aecSDennis Zhou (Facebook) bound_map_fail: 149340064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 149440064aecSDennis Zhou (Facebook) alloc_map_fail: 149540064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk); 149640064aecSDennis Zhou (Facebook) 149740064aecSDennis Zhou (Facebook) return NULL; 14986081089fSTejun Heo } 14996081089fSTejun Heo 15006081089fSTejun Heo static void pcpu_free_chunk(struct pcpu_chunk *chunk) 15016081089fSTejun Heo { 15026081089fSTejun Heo if (!chunk) 15036081089fSTejun Heo return; 15043c7be18aSRoman Gushchin #ifdef CONFIG_MEMCG_KMEM 15053c7be18aSRoman Gushchin pcpu_mem_free(chunk->obj_cgroups); 15063c7be18aSRoman Gushchin #endif 15076685b357SMike Rapoport pcpu_mem_free(chunk->md_blocks); 150840064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->bound_map); 150940064aecSDennis Zhou (Facebook) pcpu_mem_free(chunk->alloc_map); 15101d5cfdb0STetsuo Handa pcpu_mem_free(chunk); 15116081089fSTejun Heo } 15126081089fSTejun Heo 1513b539b87fSTejun Heo /** 1514b539b87fSTejun Heo * pcpu_chunk_populated - post-population bookkeeping 1515b539b87fSTejun Heo * @chunk: pcpu_chunk which got populated 1516b539b87fSTejun Heo * @page_start: the start page 1517b539b87fSTejun Heo * @page_end: the end page 1518b539b87fSTejun Heo * 1519b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been populated to @chunk. Update 1520b539b87fSTejun Heo * the bookkeeping information accordingly. Must be called after each 1521b539b87fSTejun Heo * successful population. 1522b539b87fSTejun Heo */ 152340064aecSDennis Zhou (Facebook) static void pcpu_chunk_populated(struct pcpu_chunk *chunk, int page_start, 1524b239f7daSDennis Zhou int page_end) 1525b539b87fSTejun Heo { 1526b539b87fSTejun Heo int nr = page_end - page_start; 1527b539b87fSTejun Heo 1528b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1529b539b87fSTejun Heo 1530b539b87fSTejun Heo bitmap_set(chunk->populated, page_start, nr); 1531b539b87fSTejun Heo chunk->nr_populated += nr; 15327e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += nr; 153340064aecSDennis Zhou (Facebook) 1534b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, nr); 153540064aecSDennis Zhou (Facebook) } 1536b539b87fSTejun Heo 1537b539b87fSTejun Heo /** 1538b539b87fSTejun Heo * pcpu_chunk_depopulated - post-depopulation bookkeeping 1539b539b87fSTejun Heo * @chunk: pcpu_chunk which got depopulated 1540b539b87fSTejun Heo * @page_start: the start page 1541b539b87fSTejun Heo * @page_end: the end page 1542b539b87fSTejun Heo * 1543b539b87fSTejun Heo * Pages in [@page_start,@page_end) have been depopulated from @chunk. 1544b539b87fSTejun Heo * Update the bookkeeping information accordingly. Must be called after 1545b539b87fSTejun Heo * each successful depopulation. 1546b539b87fSTejun Heo */ 1547b539b87fSTejun Heo static void pcpu_chunk_depopulated(struct pcpu_chunk *chunk, 1548b539b87fSTejun Heo int page_start, int page_end) 1549b539b87fSTejun Heo { 1550b539b87fSTejun Heo int nr = page_end - page_start; 1551b539b87fSTejun Heo 1552b539b87fSTejun Heo lockdep_assert_held(&pcpu_lock); 1553b539b87fSTejun Heo 1554b539b87fSTejun Heo bitmap_clear(chunk->populated, page_start, nr); 1555b539b87fSTejun Heo chunk->nr_populated -= nr; 15567e8a6304SDennis Zhou (Facebook) pcpu_nr_populated -= nr; 1557b239f7daSDennis Zhou 1558b239f7daSDennis Zhou pcpu_update_empty_pages(chunk, -nr); 1559b539b87fSTejun Heo } 1560b539b87fSTejun Heo 1561fbf59bc9STejun Heo /* 15629f645532STejun Heo * Chunk management implementation. 1563fbf59bc9STejun Heo * 15649f645532STejun Heo * To allow different implementations, chunk alloc/free and 15659f645532STejun Heo * [de]population are implemented in a separate file which is pulled 15669f645532STejun Heo * into this file and compiled together. The following functions 15679f645532STejun Heo * should be implemented. 1568ccea34b5STejun Heo * 15699f645532STejun Heo * pcpu_populate_chunk - populate the specified range of a chunk 15709f645532STejun Heo * pcpu_depopulate_chunk - depopulate the specified range of a chunk 157193274f1dSDennis Zhou * pcpu_post_unmap_tlb_flush - flush tlb for the specified range of a chunk 15729f645532STejun Heo * pcpu_create_chunk - create a new chunk 15739f645532STejun Heo * pcpu_destroy_chunk - destroy a chunk, always preceded by full depop 15749f645532STejun Heo * pcpu_addr_to_page - translate address to physical address 15759f645532STejun Heo * pcpu_verify_alloc_info - check alloc_info is acceptable during init 1576fbf59bc9STejun Heo */ 157715d9f3d1SDennis Zhou static int pcpu_populate_chunk(struct pcpu_chunk *chunk, 157847504ee0SDennis Zhou int page_start, int page_end, gfp_t gfp); 157915d9f3d1SDennis Zhou static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, 158015d9f3d1SDennis Zhou int page_start, int page_end); 158193274f1dSDennis Zhou static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk, 158293274f1dSDennis Zhou int page_start, int page_end); 1583faf65ddeSRoman Gushchin static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp); 15849f645532STejun Heo static void pcpu_destroy_chunk(struct pcpu_chunk *chunk); 15859f645532STejun Heo static struct page *pcpu_addr_to_page(void *addr); 15869f645532STejun Heo static int __init pcpu_verify_alloc_info(const struct pcpu_alloc_info *ai); 1587fbf59bc9STejun Heo 1588b0c9778bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_KM 1589b0c9778bSTejun Heo #include "percpu-km.c" 1590b0c9778bSTejun Heo #else 15919f645532STejun Heo #include "percpu-vm.c" 1592b0c9778bSTejun Heo #endif 1593fbf59bc9STejun Heo 1594fbf59bc9STejun Heo /** 159588999a89STejun Heo * pcpu_chunk_addr_search - determine chunk containing specified address 159688999a89STejun Heo * @addr: address for which the chunk needs to be determined. 159788999a89STejun Heo * 1598c0ebfdc3SDennis Zhou (Facebook) * This is an internal function that handles all but static allocations. 1599c0ebfdc3SDennis Zhou (Facebook) * Static percpu address values should never be passed into the allocator. 1600c0ebfdc3SDennis Zhou (Facebook) * 160188999a89STejun Heo * RETURNS: 160288999a89STejun Heo * The address of the found chunk. 160388999a89STejun Heo */ 160488999a89STejun Heo static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr) 160588999a89STejun Heo { 1606c0ebfdc3SDennis Zhou (Facebook) /* is it in the dynamic region (first chunk)? */ 1607560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_first_chunk, addr)) 1608c0ebfdc3SDennis Zhou (Facebook) return pcpu_first_chunk; 1609c0ebfdc3SDennis Zhou (Facebook) 1610c0ebfdc3SDennis Zhou (Facebook) /* is it in the reserved region? */ 1611560f2c23SDennis Zhou (Facebook) if (pcpu_addr_in_chunk(pcpu_reserved_chunk, addr)) 161288999a89STejun Heo return pcpu_reserved_chunk; 161388999a89STejun Heo 161488999a89STejun Heo /* 161588999a89STejun Heo * The address is relative to unit0 which might be unused and 161688999a89STejun Heo * thus unmapped. Offset the address to the unit space of the 161788999a89STejun Heo * current processor before looking it up in the vmalloc 161888999a89STejun Heo * space. Note that any possible cpu id can be used here, so 161988999a89STejun Heo * there's no need to worry about preemption or cpu hotplug. 162088999a89STejun Heo */ 162188999a89STejun Heo addr += pcpu_unit_offsets[raw_smp_processor_id()]; 16229f645532STejun Heo return pcpu_get_page_chunk(pcpu_addr_to_page(addr)); 162388999a89STejun Heo } 162488999a89STejun Heo 16253c7be18aSRoman Gushchin #ifdef CONFIG_MEMCG_KMEM 1626faf65ddeSRoman Gushchin static bool pcpu_memcg_pre_alloc_hook(size_t size, gfp_t gfp, 16273c7be18aSRoman Gushchin struct obj_cgroup **objcgp) 16283c7be18aSRoman Gushchin { 16293c7be18aSRoman Gushchin struct obj_cgroup *objcg; 16303c7be18aSRoman Gushchin 1631279c3393SRoman Gushchin if (!memcg_kmem_enabled() || !(gfp & __GFP_ACCOUNT)) 1632faf65ddeSRoman Gushchin return true; 16333c7be18aSRoman Gushchin 16343c7be18aSRoman Gushchin objcg = get_obj_cgroup_from_current(); 16353c7be18aSRoman Gushchin if (!objcg) 1636faf65ddeSRoman Gushchin return true; 16373c7be18aSRoman Gushchin 1638*8c57c077SQi Zheng if (obj_cgroup_charge(objcg, gfp, pcpu_obj_full_size(size))) { 16393c7be18aSRoman Gushchin obj_cgroup_put(objcg); 1640faf65ddeSRoman Gushchin return false; 16413c7be18aSRoman Gushchin } 16423c7be18aSRoman Gushchin 16433c7be18aSRoman Gushchin *objcgp = objcg; 1644faf65ddeSRoman Gushchin return true; 16453c7be18aSRoman Gushchin } 16463c7be18aSRoman Gushchin 16473c7be18aSRoman Gushchin static void pcpu_memcg_post_alloc_hook(struct obj_cgroup *objcg, 16483c7be18aSRoman Gushchin struct pcpu_chunk *chunk, int off, 16493c7be18aSRoman Gushchin size_t size) 16503c7be18aSRoman Gushchin { 16513c7be18aSRoman Gushchin if (!objcg) 16523c7be18aSRoman Gushchin return; 16533c7be18aSRoman Gushchin 1654faf65ddeSRoman Gushchin if (likely(chunk && chunk->obj_cgroups)) { 16553c7be18aSRoman Gushchin chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT] = objcg; 1656772616b0SRoman Gushchin 1657772616b0SRoman Gushchin rcu_read_lock(); 1658772616b0SRoman Gushchin mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B, 1659*8c57c077SQi Zheng pcpu_obj_full_size(size)); 1660772616b0SRoman Gushchin rcu_read_unlock(); 16613c7be18aSRoman Gushchin } else { 1662*8c57c077SQi Zheng obj_cgroup_uncharge(objcg, pcpu_obj_full_size(size)); 16633c7be18aSRoman Gushchin obj_cgroup_put(objcg); 16643c7be18aSRoman Gushchin } 16653c7be18aSRoman Gushchin } 16663c7be18aSRoman Gushchin 16673c7be18aSRoman Gushchin static void pcpu_memcg_free_hook(struct pcpu_chunk *chunk, int off, size_t size) 16683c7be18aSRoman Gushchin { 16693c7be18aSRoman Gushchin struct obj_cgroup *objcg; 16703c7be18aSRoman Gushchin 1671faf65ddeSRoman Gushchin if (unlikely(!chunk->obj_cgroups)) 16723c7be18aSRoman Gushchin return; 16733c7be18aSRoman Gushchin 16743c7be18aSRoman Gushchin objcg = chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT]; 1675faf65ddeSRoman Gushchin if (!objcg) 1676faf65ddeSRoman Gushchin return; 16773c7be18aSRoman Gushchin chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT] = NULL; 16783c7be18aSRoman Gushchin 1679*8c57c077SQi Zheng obj_cgroup_uncharge(objcg, pcpu_obj_full_size(size)); 16803c7be18aSRoman Gushchin 1681772616b0SRoman Gushchin rcu_read_lock(); 1682772616b0SRoman Gushchin mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B, 1683*8c57c077SQi Zheng -pcpu_obj_full_size(size)); 1684772616b0SRoman Gushchin rcu_read_unlock(); 1685772616b0SRoman Gushchin 16863c7be18aSRoman Gushchin obj_cgroup_put(objcg); 16873c7be18aSRoman Gushchin } 16883c7be18aSRoman Gushchin 16893c7be18aSRoman Gushchin #else /* CONFIG_MEMCG_KMEM */ 1690faf65ddeSRoman Gushchin static bool 16913c7be18aSRoman Gushchin pcpu_memcg_pre_alloc_hook(size_t size, gfp_t gfp, struct obj_cgroup **objcgp) 16923c7be18aSRoman Gushchin { 1693faf65ddeSRoman Gushchin return true; 16943c7be18aSRoman Gushchin } 16953c7be18aSRoman Gushchin 16963c7be18aSRoman Gushchin static void pcpu_memcg_post_alloc_hook(struct obj_cgroup *objcg, 16973c7be18aSRoman Gushchin struct pcpu_chunk *chunk, int off, 16983c7be18aSRoman Gushchin size_t size) 16993c7be18aSRoman Gushchin { 17003c7be18aSRoman Gushchin } 17013c7be18aSRoman Gushchin 17023c7be18aSRoman Gushchin static void pcpu_memcg_free_hook(struct pcpu_chunk *chunk, int off, size_t size) 17033c7be18aSRoman Gushchin { 17043c7be18aSRoman Gushchin } 17053c7be18aSRoman Gushchin #endif /* CONFIG_MEMCG_KMEM */ 17063c7be18aSRoman Gushchin 170788999a89STejun Heo /** 1708edcb4639STejun Heo * pcpu_alloc - the percpu allocator 1709cae3aeb8STejun Heo * @size: size of area to allocate in bytes 1710fbf59bc9STejun Heo * @align: alignment of area (max PAGE_SIZE) 1711edcb4639STejun Heo * @reserved: allocate from the reserved chunk if available 17125835d96eSTejun Heo * @gfp: allocation flags 1713fbf59bc9STejun Heo * 17145835d96eSTejun Heo * Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't 17150ea7eeecSDaniel Borkmann * contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN 17160ea7eeecSDaniel Borkmann * then no warning will be triggered on invalid or failed allocation 17170ea7eeecSDaniel Borkmann * requests. 1718fbf59bc9STejun Heo * 1719fbf59bc9STejun Heo * RETURNS: 1720fbf59bc9STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1721fbf59bc9STejun Heo */ 17225835d96eSTejun Heo static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved, 17235835d96eSTejun Heo gfp_t gfp) 1724fbf59bc9STejun Heo { 172528307d93SFilipe Manana gfp_t pcpu_gfp; 172628307d93SFilipe Manana bool is_atomic; 172728307d93SFilipe Manana bool do_warn; 17283c7be18aSRoman Gushchin struct obj_cgroup *objcg = NULL; 1729f2badb0cSTejun Heo static int warn_limit = 10; 17308744d859SDennis Zhou struct pcpu_chunk *chunk, *next; 1731f2badb0cSTejun Heo const char *err; 173240064aecSDennis Zhou (Facebook) int slot, off, cpu, ret; 1733403a91b1SJiri Kosina unsigned long flags; 1734f528f0b8SCatalin Marinas void __percpu *ptr; 173540064aecSDennis Zhou (Facebook) size_t bits, bit_align; 1736fbf59bc9STejun Heo 173728307d93SFilipe Manana gfp = current_gfp_context(gfp); 173828307d93SFilipe Manana /* whitelisted flags that can be passed to the backing allocators */ 173928307d93SFilipe Manana pcpu_gfp = gfp & (GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); 174028307d93SFilipe Manana is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL; 174128307d93SFilipe Manana do_warn = !(gfp & __GFP_NOWARN); 174228307d93SFilipe Manana 1743723ad1d9SAl Viro /* 174440064aecSDennis Zhou (Facebook) * There is now a minimum allocation size of PCPU_MIN_ALLOC_SIZE, 174540064aecSDennis Zhou (Facebook) * therefore alignment must be a minimum of that many bytes. 174640064aecSDennis Zhou (Facebook) * An allocation may have internal fragmentation from rounding up 174740064aecSDennis Zhou (Facebook) * of up to PCPU_MIN_ALLOC_SIZE - 1 bytes. 1748723ad1d9SAl Viro */ 1749d2f3c384SDennis Zhou (Facebook) if (unlikely(align < PCPU_MIN_ALLOC_SIZE)) 1750d2f3c384SDennis Zhou (Facebook) align = PCPU_MIN_ALLOC_SIZE; 1751723ad1d9SAl Viro 1752d2f3c384SDennis Zhou (Facebook) size = ALIGN(size, PCPU_MIN_ALLOC_SIZE); 175340064aecSDennis Zhou (Facebook) bits = size >> PCPU_MIN_ALLOC_SHIFT; 175440064aecSDennis Zhou (Facebook) bit_align = align >> PCPU_MIN_ALLOC_SHIFT; 17552f69fa82SViro 17563ca45a46Szijun_hu if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE || 17573ca45a46Szijun_hu !is_power_of_2(align))) { 17580ea7eeecSDaniel Borkmann WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n", 1759756a025fSJoe Perches size, align); 1760fbf59bc9STejun Heo return NULL; 1761fbf59bc9STejun Heo } 1762fbf59bc9STejun Heo 1763faf65ddeSRoman Gushchin if (unlikely(!pcpu_memcg_pre_alloc_hook(size, gfp, &objcg))) 17643c7be18aSRoman Gushchin return NULL; 17653c7be18aSRoman Gushchin 1766f52ba1feSKirill Tkhai if (!is_atomic) { 1767f52ba1feSKirill Tkhai /* 1768f52ba1feSKirill Tkhai * pcpu_balance_workfn() allocates memory under this mutex, 1769f52ba1feSKirill Tkhai * and it may wait for memory reclaim. Allow current task 1770f52ba1feSKirill Tkhai * to become OOM victim, in case of memory pressure. 1771f52ba1feSKirill Tkhai */ 17723c7be18aSRoman Gushchin if (gfp & __GFP_NOFAIL) { 17736710e594STejun Heo mutex_lock(&pcpu_alloc_mutex); 17743c7be18aSRoman Gushchin } else if (mutex_lock_killable(&pcpu_alloc_mutex)) { 17753c7be18aSRoman Gushchin pcpu_memcg_post_alloc_hook(objcg, NULL, 0, size); 1776f52ba1feSKirill Tkhai return NULL; 1777f52ba1feSKirill Tkhai } 17783c7be18aSRoman Gushchin } 17796710e594STejun Heo 1780403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1781fbf59bc9STejun Heo 1782edcb4639STejun Heo /* serve reserved allocations from the reserved chunk if available */ 1783edcb4639STejun Heo if (reserved && pcpu_reserved_chunk) { 1784edcb4639STejun Heo chunk = pcpu_reserved_chunk; 1785833af842STejun Heo 178640064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, is_atomic); 178740064aecSDennis Zhou (Facebook) if (off < 0) { 1788833af842STejun Heo err = "alloc from reserved chunk failed"; 1789ccea34b5STejun Heo goto fail_unlock; 1790f2badb0cSTejun Heo } 1791833af842STejun Heo 179240064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1793edcb4639STejun Heo if (off >= 0) 1794edcb4639STejun Heo goto area_found; 1795833af842STejun Heo 1796f2badb0cSTejun Heo err = "alloc from reserved chunk failed"; 1797ccea34b5STejun Heo goto fail_unlock; 1798edcb4639STejun Heo } 1799edcb4639STejun Heo 1800ccea34b5STejun Heo restart: 1801edcb4639STejun Heo /* search through normal chunks */ 1802f1833241SRoman Gushchin for (slot = pcpu_size_to_slot(size); slot <= pcpu_free_slot; slot++) { 1803faf65ddeSRoman Gushchin list_for_each_entry_safe(chunk, next, &pcpu_chunk_lists[slot], 1804faf65ddeSRoman Gushchin list) { 180540064aecSDennis Zhou (Facebook) off = pcpu_find_block_fit(chunk, bits, bit_align, 180640064aecSDennis Zhou (Facebook) is_atomic); 18078744d859SDennis Zhou if (off < 0) { 18088744d859SDennis Zhou if (slot < PCPU_SLOT_FAIL_THRESHOLD) 18098744d859SDennis Zhou pcpu_chunk_move(chunk, 0); 1810fbf59bc9STejun Heo continue; 18118744d859SDennis Zhou } 1812ccea34b5STejun Heo 181340064aecSDennis Zhou (Facebook) off = pcpu_alloc_area(chunk, bits, bit_align, off); 1814f1833241SRoman Gushchin if (off >= 0) { 1815f1833241SRoman Gushchin pcpu_reintegrate_chunk(chunk); 1816fbf59bc9STejun Heo goto area_found; 1817f1833241SRoman Gushchin } 1818fbf59bc9STejun Heo } 1819fbf59bc9STejun Heo } 1820fbf59bc9STejun Heo 1821403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1822ccea34b5STejun Heo 1823b38d08f3STejun Heo /* 1824b38d08f3STejun Heo * No space left. Create a new chunk. We don't want multiple 1825b38d08f3STejun Heo * tasks to create chunks simultaneously. Serialize and create iff 1826b38d08f3STejun Heo * there's still no empty chunk after grabbing the mutex. 1827b38d08f3STejun Heo */ 182811df02bfSDennis Zhou if (is_atomic) { 182911df02bfSDennis Zhou err = "atomic alloc failed, no space left"; 18305835d96eSTejun Heo goto fail; 183111df02bfSDennis Zhou } 18325835d96eSTejun Heo 1833faf65ddeSRoman Gushchin if (list_empty(&pcpu_chunk_lists[pcpu_free_slot])) { 1834faf65ddeSRoman Gushchin chunk = pcpu_create_chunk(pcpu_gfp); 1835f2badb0cSTejun Heo if (!chunk) { 1836f2badb0cSTejun Heo err = "failed to allocate new chunk"; 1837b38d08f3STejun Heo goto fail; 1838f2badb0cSTejun Heo } 1839ccea34b5STejun Heo 1840403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1841fbf59bc9STejun Heo pcpu_chunk_relocate(chunk, -1); 1842b38d08f3STejun Heo } else { 1843b38d08f3STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 1844b38d08f3STejun Heo } 1845b38d08f3STejun Heo 1846ccea34b5STejun Heo goto restart; 1847fbf59bc9STejun Heo 1848fbf59bc9STejun Heo area_found: 184930a5b536SDennis Zhou pcpu_stats_area_alloc(chunk, size); 1850403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1851ccea34b5STejun Heo 1852dca49645STejun Heo /* populate if not all pages are already there */ 18535835d96eSTejun Heo if (!is_atomic) { 1854e837dfdeSDennis Zhou unsigned int page_start, page_end, rs, re; 1855e04d3208STejun Heo 1856dca49645STejun Heo page_start = PFN_DOWN(off); 1857dca49645STejun Heo page_end = PFN_UP(off + size); 1858dca49645STejun Heo 1859e837dfdeSDennis Zhou bitmap_for_each_clear_region(chunk->populated, rs, re, 186091e914c5SDennis Zhou (Facebook) page_start, page_end) { 1861dca49645STejun Heo WARN_ON(chunk->immutable); 1862dca49645STejun Heo 1863554fef1cSDennis Zhou ret = pcpu_populate_chunk(chunk, rs, re, pcpu_gfp); 1864b38d08f3STejun Heo 1865403a91b1SJiri Kosina spin_lock_irqsave(&pcpu_lock, flags); 1866b38d08f3STejun Heo if (ret) { 186740064aecSDennis Zhou (Facebook) pcpu_free_area(chunk, off); 1868f2badb0cSTejun Heo err = "failed to populate"; 1869ccea34b5STejun Heo goto fail_unlock; 1870fbf59bc9STejun Heo } 1871b239f7daSDennis Zhou pcpu_chunk_populated(chunk, rs, re); 1872b38d08f3STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 1873dca49645STejun Heo } 1874dca49645STejun Heo 1875ccea34b5STejun Heo mutex_unlock(&pcpu_alloc_mutex); 1876e04d3208STejun Heo } 1877ccea34b5STejun Heo 1878faf65ddeSRoman Gushchin if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_LOW) 18791a4d7607STejun Heo pcpu_schedule_balance_work(); 18801a4d7607STejun Heo 1881dca49645STejun Heo /* clear the areas and return address relative to base address */ 1882dca49645STejun Heo for_each_possible_cpu(cpu) 1883dca49645STejun Heo memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size); 1884dca49645STejun Heo 1885f528f0b8SCatalin Marinas ptr = __addr_to_pcpu_ptr(chunk->base_addr + off); 18868a8c35faSLarry Finger kmemleak_alloc_percpu(ptr, size, gfp); 1887df95e795SDennis Zhou 1888df95e795SDennis Zhou trace_percpu_alloc_percpu(reserved, is_atomic, size, align, 1889df95e795SDennis Zhou chunk->base_addr, off, ptr); 1890df95e795SDennis Zhou 18913c7be18aSRoman Gushchin pcpu_memcg_post_alloc_hook(objcg, chunk, off, size); 18923c7be18aSRoman Gushchin 1893f528f0b8SCatalin Marinas return ptr; 1894ccea34b5STejun Heo 1895ccea34b5STejun Heo fail_unlock: 1896403a91b1SJiri Kosina spin_unlock_irqrestore(&pcpu_lock, flags); 1897b38d08f3STejun Heo fail: 1898df95e795SDennis Zhou trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align); 1899df95e795SDennis Zhou 19000ea7eeecSDaniel Borkmann if (!is_atomic && do_warn && warn_limit) { 1901870d4b12SJoe Perches pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n", 19025835d96eSTejun Heo size, align, is_atomic, err); 1903f2badb0cSTejun Heo dump_stack(); 1904f2badb0cSTejun Heo if (!--warn_limit) 1905870d4b12SJoe Perches pr_info("limit reached, disable warning\n"); 1906f2badb0cSTejun Heo } 19071a4d7607STejun Heo if (is_atomic) { 1908f0953a1bSIngo Molnar /* see the flag handling in pcpu_balance_workfn() */ 19091a4d7607STejun Heo pcpu_atomic_alloc_failed = true; 19101a4d7607STejun Heo pcpu_schedule_balance_work(); 19116710e594STejun Heo } else { 19126710e594STejun Heo mutex_unlock(&pcpu_alloc_mutex); 19131a4d7607STejun Heo } 19143c7be18aSRoman Gushchin 19153c7be18aSRoman Gushchin pcpu_memcg_post_alloc_hook(objcg, NULL, 0, size); 19163c7be18aSRoman Gushchin 1917ccea34b5STejun Heo return NULL; 1918fbf59bc9STejun Heo } 1919edcb4639STejun Heo 1920edcb4639STejun Heo /** 19215835d96eSTejun Heo * __alloc_percpu_gfp - allocate dynamic percpu area 1922edcb4639STejun Heo * @size: size of area to allocate in bytes 1923edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 19245835d96eSTejun Heo * @gfp: allocation flags 1925edcb4639STejun Heo * 19265835d96eSTejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align. If 19275835d96eSTejun Heo * @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can 19280ea7eeecSDaniel Borkmann * be called from any context but is a lot more likely to fail. If @gfp 19290ea7eeecSDaniel Borkmann * has __GFP_NOWARN then no warning will be triggered on invalid or failed 19300ea7eeecSDaniel Borkmann * allocation requests. 1931ccea34b5STejun Heo * 1932edcb4639STejun Heo * RETURNS: 1933edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1934edcb4639STejun Heo */ 19355835d96eSTejun Heo void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) 19365835d96eSTejun Heo { 19375835d96eSTejun Heo return pcpu_alloc(size, align, false, gfp); 19385835d96eSTejun Heo } 19395835d96eSTejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu_gfp); 19405835d96eSTejun Heo 19415835d96eSTejun Heo /** 19425835d96eSTejun Heo * __alloc_percpu - allocate dynamic percpu area 19435835d96eSTejun Heo * @size: size of area to allocate in bytes 19445835d96eSTejun Heo * @align: alignment of area (max PAGE_SIZE) 19455835d96eSTejun Heo * 19465835d96eSTejun Heo * Equivalent to __alloc_percpu_gfp(size, align, %GFP_KERNEL). 19475835d96eSTejun Heo */ 194843cf38ebSTejun Heo void __percpu *__alloc_percpu(size_t size, size_t align) 1949edcb4639STejun Heo { 19505835d96eSTejun Heo return pcpu_alloc(size, align, false, GFP_KERNEL); 1951edcb4639STejun Heo } 1952fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(__alloc_percpu); 1953fbf59bc9STejun Heo 1954edcb4639STejun Heo /** 1955edcb4639STejun Heo * __alloc_reserved_percpu - allocate reserved percpu area 1956edcb4639STejun Heo * @size: size of area to allocate in bytes 1957edcb4639STejun Heo * @align: alignment of area (max PAGE_SIZE) 1958edcb4639STejun Heo * 19599329ba97STejun Heo * Allocate zero-filled percpu area of @size bytes aligned at @align 19609329ba97STejun Heo * from reserved percpu area if arch has set it up; otherwise, 19619329ba97STejun Heo * allocation is served from the same dynamic area. Might sleep. 19629329ba97STejun Heo * Might trigger writeouts. 1963edcb4639STejun Heo * 1964ccea34b5STejun Heo * CONTEXT: 1965ccea34b5STejun Heo * Does GFP_KERNEL allocation. 1966ccea34b5STejun Heo * 1967edcb4639STejun Heo * RETURNS: 1968edcb4639STejun Heo * Percpu pointer to the allocated area on success, NULL on failure. 1969edcb4639STejun Heo */ 197043cf38ebSTejun Heo void __percpu *__alloc_reserved_percpu(size_t size, size_t align) 1971edcb4639STejun Heo { 19725835d96eSTejun Heo return pcpu_alloc(size, align, true, GFP_KERNEL); 1973edcb4639STejun Heo } 1974edcb4639STejun Heo 1975a56dbddfSTejun Heo /** 197667c2669dSRoman Gushchin * pcpu_balance_free - manage the amount of free chunks 1977f1833241SRoman Gushchin * @empty_only: free chunks only if there are no populated pages 1978a56dbddfSTejun Heo * 1979f1833241SRoman Gushchin * If empty_only is %false, reclaim all fully free chunks regardless of the 1980f1833241SRoman Gushchin * number of populated pages. Otherwise, only reclaim chunks that have no 1981f1833241SRoman Gushchin * populated pages. 1982e4d77700SRoman Gushchin * 1983e4d77700SRoman Gushchin * CONTEXT: 1984e4d77700SRoman Gushchin * pcpu_lock (can be dropped temporarily) 1985a56dbddfSTejun Heo */ 1986faf65ddeSRoman Gushchin static void pcpu_balance_free(bool empty_only) 1987fbf59bc9STejun Heo { 1988fe6bd8c3STejun Heo LIST_HEAD(to_free); 1989faf65ddeSRoman Gushchin struct list_head *free_head = &pcpu_chunk_lists[pcpu_free_slot]; 1990a56dbddfSTejun Heo struct pcpu_chunk *chunk, *next; 1991a56dbddfSTejun Heo 1992e4d77700SRoman Gushchin lockdep_assert_held(&pcpu_lock); 1993a56dbddfSTejun Heo 19941a4d7607STejun Heo /* 19951a4d7607STejun Heo * There's no reason to keep around multiple unused chunks and VM 19961a4d7607STejun Heo * areas can be scarce. Destroy all free chunks except for one. 19971a4d7607STejun Heo */ 1998fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, free_head, list) { 19998d408b4bSTejun Heo WARN_ON(chunk->immutable); 2000a56dbddfSTejun Heo 2001a56dbddfSTejun Heo /* spare the first one */ 2002fe6bd8c3STejun Heo if (chunk == list_first_entry(free_head, struct pcpu_chunk, list)) 2003a56dbddfSTejun Heo continue; 2004a56dbddfSTejun Heo 2005f1833241SRoman Gushchin if (!empty_only || chunk->nr_empty_pop_pages == 0) 2006fe6bd8c3STejun Heo list_move(&chunk->list, &to_free); 2007a56dbddfSTejun Heo } 2008a56dbddfSTejun Heo 2009e4d77700SRoman Gushchin if (list_empty(&to_free)) 2010e4d77700SRoman Gushchin return; 2011a56dbddfSTejun Heo 2012e4d77700SRoman Gushchin spin_unlock_irq(&pcpu_lock); 2013fe6bd8c3STejun Heo list_for_each_entry_safe(chunk, next, &to_free, list) { 2014e837dfdeSDennis Zhou unsigned int rs, re; 2015dca49645STejun Heo 2016e837dfdeSDennis Zhou bitmap_for_each_set_region(chunk->populated, rs, re, 0, 201791e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 2018a93ace48STejun Heo pcpu_depopulate_chunk(chunk, rs, re); 2019b539b87fSTejun Heo spin_lock_irq(&pcpu_lock); 2020b539b87fSTejun Heo pcpu_chunk_depopulated(chunk, rs, re); 2021b539b87fSTejun Heo spin_unlock_irq(&pcpu_lock); 2022a93ace48STejun Heo } 20236081089fSTejun Heo pcpu_destroy_chunk(chunk); 2024accd4f36SEric Dumazet cond_resched(); 2025fbf59bc9STejun Heo } 2026e4d77700SRoman Gushchin spin_lock_irq(&pcpu_lock); 202767c2669dSRoman Gushchin } 202867c2669dSRoman Gushchin 202967c2669dSRoman Gushchin /** 203067c2669dSRoman Gushchin * pcpu_balance_populated - manage the amount of populated pages 203167c2669dSRoman Gushchin * 203267c2669dSRoman Gushchin * Maintain a certain amount of populated pages to satisfy atomic allocations. 203367c2669dSRoman Gushchin * It is possible that this is called when physical memory is scarce causing 203467c2669dSRoman Gushchin * OOM killer to be triggered. We should avoid doing so until an actual 203567c2669dSRoman Gushchin * allocation causes the failure as it is possible that requests can be 203667c2669dSRoman Gushchin * serviced from already backed regions. 2037e4d77700SRoman Gushchin * 2038e4d77700SRoman Gushchin * CONTEXT: 2039e4d77700SRoman Gushchin * pcpu_lock (can be dropped temporarily) 204067c2669dSRoman Gushchin */ 2041faf65ddeSRoman Gushchin static void pcpu_balance_populated(void) 204267c2669dSRoman Gushchin { 204367c2669dSRoman Gushchin /* gfp flags passed to underlying allocators */ 204467c2669dSRoman Gushchin const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN; 204567c2669dSRoman Gushchin struct pcpu_chunk *chunk; 204667c2669dSRoman Gushchin int slot, nr_to_pop, ret; 2047971f3918STejun Heo 2048e4d77700SRoman Gushchin lockdep_assert_held(&pcpu_lock); 2049971f3918STejun Heo 20501a4d7607STejun Heo /* 20511a4d7607STejun Heo * Ensure there are certain number of free populated pages for 20521a4d7607STejun Heo * atomic allocs. Fill up from the most packed so that atomic 20531a4d7607STejun Heo * allocs don't increase fragmentation. If atomic allocation 20541a4d7607STejun Heo * failed previously, always populate the maximum amount. This 20551a4d7607STejun Heo * should prevent atomic allocs larger than PAGE_SIZE from keeping 20561a4d7607STejun Heo * failing indefinitely; however, large atomic allocs are not 20571a4d7607STejun Heo * something we support properly and can be highly unreliable and 20581a4d7607STejun Heo * inefficient. 20591a4d7607STejun Heo */ 20601a4d7607STejun Heo retry_pop: 20611a4d7607STejun Heo if (pcpu_atomic_alloc_failed) { 20621a4d7607STejun Heo nr_to_pop = PCPU_EMPTY_POP_PAGES_HIGH; 20631a4d7607STejun Heo /* best effort anyway, don't worry about synchronization */ 20641a4d7607STejun Heo pcpu_atomic_alloc_failed = false; 20651a4d7607STejun Heo } else { 20661a4d7607STejun Heo nr_to_pop = clamp(PCPU_EMPTY_POP_PAGES_HIGH - 2067faf65ddeSRoman Gushchin pcpu_nr_empty_pop_pages, 20681a4d7607STejun Heo 0, PCPU_EMPTY_POP_PAGES_HIGH); 20691a4d7607STejun Heo } 20701a4d7607STejun Heo 20711c29a3ceSDennis Zhou for (slot = pcpu_size_to_slot(PAGE_SIZE); slot <= pcpu_free_slot; slot++) { 2072e837dfdeSDennis Zhou unsigned int nr_unpop = 0, rs, re; 20731a4d7607STejun Heo 20741a4d7607STejun Heo if (!nr_to_pop) 20751a4d7607STejun Heo break; 20761a4d7607STejun Heo 2077faf65ddeSRoman Gushchin list_for_each_entry(chunk, &pcpu_chunk_lists[slot], list) { 20788ab16c43SDennis Zhou (Facebook) nr_unpop = chunk->nr_pages - chunk->nr_populated; 20791a4d7607STejun Heo if (nr_unpop) 20801a4d7607STejun Heo break; 20811a4d7607STejun Heo } 20821a4d7607STejun Heo 20831a4d7607STejun Heo if (!nr_unpop) 20841a4d7607STejun Heo continue; 20851a4d7607STejun Heo 20861a4d7607STejun Heo /* @chunk can't go away while pcpu_alloc_mutex is held */ 2087e837dfdeSDennis Zhou bitmap_for_each_clear_region(chunk->populated, rs, re, 0, 208891e914c5SDennis Zhou (Facebook) chunk->nr_pages) { 2089e837dfdeSDennis Zhou int nr = min_t(int, re - rs, nr_to_pop); 20901a4d7607STejun Heo 2091e4d77700SRoman Gushchin spin_unlock_irq(&pcpu_lock); 209247504ee0SDennis Zhou ret = pcpu_populate_chunk(chunk, rs, rs + nr, gfp); 2093e4d77700SRoman Gushchin cond_resched(); 2094e4d77700SRoman Gushchin spin_lock_irq(&pcpu_lock); 20951a4d7607STejun Heo if (!ret) { 20961a4d7607STejun Heo nr_to_pop -= nr; 2097b239f7daSDennis Zhou pcpu_chunk_populated(chunk, rs, rs + nr); 20981a4d7607STejun Heo } else { 20991a4d7607STejun Heo nr_to_pop = 0; 21001a4d7607STejun Heo } 21011a4d7607STejun Heo 21021a4d7607STejun Heo if (!nr_to_pop) 21031a4d7607STejun Heo break; 21041a4d7607STejun Heo } 21051a4d7607STejun Heo } 21061a4d7607STejun Heo 21071a4d7607STejun Heo if (nr_to_pop) { 21081a4d7607STejun Heo /* ran out of chunks to populate, create a new one and retry */ 21091a4d7607STejun Heo spin_unlock_irq(&pcpu_lock); 2110e4d77700SRoman Gushchin chunk = pcpu_create_chunk(gfp); 2111e4d77700SRoman Gushchin cond_resched(); 2112e4d77700SRoman Gushchin spin_lock_irq(&pcpu_lock); 2113e4d77700SRoman Gushchin if (chunk) { 2114e4d77700SRoman Gushchin pcpu_chunk_relocate(chunk, -1); 21151a4d7607STejun Heo goto retry_pop; 21161a4d7607STejun Heo } 21171a4d7607STejun Heo } 2118a56dbddfSTejun Heo } 2119fbf59bc9STejun Heo 2120fbf59bc9STejun Heo /** 2121f1833241SRoman Gushchin * pcpu_reclaim_populated - scan over to_depopulate chunks and free empty pages 2122f1833241SRoman Gushchin * 2123f1833241SRoman Gushchin * Scan over chunks in the depopulate list and try to release unused populated 2124f1833241SRoman Gushchin * pages back to the system. Depopulated chunks are sidelined to prevent 2125f1833241SRoman Gushchin * repopulating these pages unless required. Fully free chunks are reintegrated 2126f1833241SRoman Gushchin * and freed accordingly (1 is kept around). If we drop below the empty 2127f1833241SRoman Gushchin * populated pages threshold, reintegrate the chunk if it has empty free pages. 2128f1833241SRoman Gushchin * Each chunk is scanned in the reverse order to keep populated pages close to 2129f1833241SRoman Gushchin * the beginning of the chunk. 2130e4d77700SRoman Gushchin * 2131e4d77700SRoman Gushchin * CONTEXT: 2132e4d77700SRoman Gushchin * pcpu_lock (can be dropped temporarily) 2133e4d77700SRoman Gushchin * 2134f1833241SRoman Gushchin */ 2135faf65ddeSRoman Gushchin static void pcpu_reclaim_populated(void) 2136f1833241SRoman Gushchin { 2137f1833241SRoman Gushchin struct pcpu_chunk *chunk; 2138f1833241SRoman Gushchin struct pcpu_block_md *block; 213993274f1dSDennis Zhou int freed_page_start, freed_page_end; 2140f1833241SRoman Gushchin int i, end; 214193274f1dSDennis Zhou bool reintegrate; 2142f1833241SRoman Gushchin 2143e4d77700SRoman Gushchin lockdep_assert_held(&pcpu_lock); 2144f1833241SRoman Gushchin 2145f1833241SRoman Gushchin /* 2146f1833241SRoman Gushchin * Once a chunk is isolated to the to_depopulate list, the chunk is no 2147f1833241SRoman Gushchin * longer discoverable to allocations whom may populate pages. The only 2148f1833241SRoman Gushchin * other accessor is the free path which only returns area back to the 2149f1833241SRoman Gushchin * allocator not touching the populated bitmap. 2150f1833241SRoman Gushchin */ 2151faf65ddeSRoman Gushchin while (!list_empty(&pcpu_chunk_lists[pcpu_to_depopulate_slot])) { 2152faf65ddeSRoman Gushchin chunk = list_first_entry(&pcpu_chunk_lists[pcpu_to_depopulate_slot], 2153f1833241SRoman Gushchin struct pcpu_chunk, list); 2154f1833241SRoman Gushchin WARN_ON(chunk->immutable); 2155f1833241SRoman Gushchin 2156f1833241SRoman Gushchin /* 2157f1833241SRoman Gushchin * Scan chunk's pages in the reverse order to keep populated 2158f1833241SRoman Gushchin * pages close to the beginning of the chunk. 2159f1833241SRoman Gushchin */ 216093274f1dSDennis Zhou freed_page_start = chunk->nr_pages; 216193274f1dSDennis Zhou freed_page_end = 0; 216293274f1dSDennis Zhou reintegrate = false; 2163f1833241SRoman Gushchin for (i = chunk->nr_pages - 1, end = -1; i >= 0; i--) { 2164f1833241SRoman Gushchin /* no more work to do */ 2165f1833241SRoman Gushchin if (chunk->nr_empty_pop_pages == 0) 2166f1833241SRoman Gushchin break; 2167f1833241SRoman Gushchin 2168f1833241SRoman Gushchin /* reintegrate chunk to prevent atomic alloc failures */ 2169faf65ddeSRoman Gushchin if (pcpu_nr_empty_pop_pages < PCPU_EMPTY_POP_PAGES_HIGH) { 217093274f1dSDennis Zhou reintegrate = true; 217193274f1dSDennis Zhou goto end_chunk; 2172f1833241SRoman Gushchin } 2173f1833241SRoman Gushchin 2174f1833241SRoman Gushchin /* 2175f1833241SRoman Gushchin * If the page is empty and populated, start or 2176f1833241SRoman Gushchin * extend the (i, end) range. If i == 0, decrease 2177f1833241SRoman Gushchin * i and perform the depopulation to cover the last 2178f1833241SRoman Gushchin * (first) page in the chunk. 2179f1833241SRoman Gushchin */ 2180f1833241SRoman Gushchin block = chunk->md_blocks + i; 2181f1833241SRoman Gushchin if (block->contig_hint == PCPU_BITMAP_BLOCK_BITS && 2182f1833241SRoman Gushchin test_bit(i, chunk->populated)) { 2183f1833241SRoman Gushchin if (end == -1) 2184f1833241SRoman Gushchin end = i; 2185f1833241SRoman Gushchin if (i > 0) 2186f1833241SRoman Gushchin continue; 2187f1833241SRoman Gushchin i--; 2188f1833241SRoman Gushchin } 2189f1833241SRoman Gushchin 2190f1833241SRoman Gushchin /* depopulate if there is an active range */ 2191f1833241SRoman Gushchin if (end == -1) 2192f1833241SRoman Gushchin continue; 2193f1833241SRoman Gushchin 2194f1833241SRoman Gushchin spin_unlock_irq(&pcpu_lock); 2195f1833241SRoman Gushchin pcpu_depopulate_chunk(chunk, i + 1, end + 1); 2196f1833241SRoman Gushchin cond_resched(); 2197f1833241SRoman Gushchin spin_lock_irq(&pcpu_lock); 2198f1833241SRoman Gushchin 2199f1833241SRoman Gushchin pcpu_chunk_depopulated(chunk, i + 1, end + 1); 220093274f1dSDennis Zhou freed_page_start = min(freed_page_start, i + 1); 220193274f1dSDennis Zhou freed_page_end = max(freed_page_end, end + 1); 2202f1833241SRoman Gushchin 2203f1833241SRoman Gushchin /* reset the range and continue */ 2204f1833241SRoman Gushchin end = -1; 2205f1833241SRoman Gushchin } 2206f1833241SRoman Gushchin 220793274f1dSDennis Zhou end_chunk: 220893274f1dSDennis Zhou /* batch tlb flush per chunk to amortize cost */ 220993274f1dSDennis Zhou if (freed_page_start < freed_page_end) { 221093274f1dSDennis Zhou spin_unlock_irq(&pcpu_lock); 221193274f1dSDennis Zhou pcpu_post_unmap_tlb_flush(chunk, 221293274f1dSDennis Zhou freed_page_start, 221393274f1dSDennis Zhou freed_page_end); 221493274f1dSDennis Zhou cond_resched(); 221593274f1dSDennis Zhou spin_lock_irq(&pcpu_lock); 221693274f1dSDennis Zhou } 221793274f1dSDennis Zhou 221893274f1dSDennis Zhou if (reintegrate || chunk->free_bytes == pcpu_unit_size) 2219f1833241SRoman Gushchin pcpu_reintegrate_chunk(chunk); 2220f1833241SRoman Gushchin else 222193274f1dSDennis Zhou list_move_tail(&chunk->list, 2222faf65ddeSRoman Gushchin &pcpu_chunk_lists[pcpu_sidelined_slot]); 2223f1833241SRoman Gushchin } 2224fbf59bc9STejun Heo } 2225fbf59bc9STejun Heo 2226fbf59bc9STejun Heo /** 22273c7be18aSRoman Gushchin * pcpu_balance_workfn - manage the amount of free chunks and populated pages 22283c7be18aSRoman Gushchin * @work: unused 22293c7be18aSRoman Gushchin * 2230f1833241SRoman Gushchin * For each chunk type, manage the number of fully free chunks and the number of 2231f1833241SRoman Gushchin * populated pages. An important thing to consider is when pages are freed and 2232f1833241SRoman Gushchin * how they contribute to the global counts. 22333c7be18aSRoman Gushchin */ 22343c7be18aSRoman Gushchin static void pcpu_balance_workfn(struct work_struct *work) 22353c7be18aSRoman Gushchin { 2236f1833241SRoman Gushchin /* 2237f1833241SRoman Gushchin * pcpu_balance_free() is called twice because the first time we may 2238f1833241SRoman Gushchin * trim pages in the active pcpu_nr_empty_pop_pages which may cause us 2239f1833241SRoman Gushchin * to grow other chunks. This then gives pcpu_reclaim_populated() time 2240f1833241SRoman Gushchin * to move fully free chunks to the active list to be freed if 2241f1833241SRoman Gushchin * appropriate. 2242f1833241SRoman Gushchin */ 224367c2669dSRoman Gushchin mutex_lock(&pcpu_alloc_mutex); 2244e4d77700SRoman Gushchin spin_lock_irq(&pcpu_lock); 22453c7be18aSRoman Gushchin 2246faf65ddeSRoman Gushchin pcpu_balance_free(false); 2247faf65ddeSRoman Gushchin pcpu_reclaim_populated(); 2248faf65ddeSRoman Gushchin pcpu_balance_populated(); 2249faf65ddeSRoman Gushchin pcpu_balance_free(true); 2250e4d77700SRoman Gushchin 2251e4d77700SRoman Gushchin spin_unlock_irq(&pcpu_lock); 225267c2669dSRoman Gushchin mutex_unlock(&pcpu_alloc_mutex); 22533c7be18aSRoman Gushchin } 22543c7be18aSRoman Gushchin 22553c7be18aSRoman Gushchin /** 2256fbf59bc9STejun Heo * free_percpu - free percpu area 2257fbf59bc9STejun Heo * @ptr: pointer to area to free 2258fbf59bc9STejun Heo * 2259ccea34b5STejun Heo * Free percpu area @ptr. 2260ccea34b5STejun Heo * 2261ccea34b5STejun Heo * CONTEXT: 2262ccea34b5STejun Heo * Can be called from atomic context. 2263fbf59bc9STejun Heo */ 226443cf38ebSTejun Heo void free_percpu(void __percpu *ptr) 2265fbf59bc9STejun Heo { 2266129182e5SAndrew Morton void *addr; 2267fbf59bc9STejun Heo struct pcpu_chunk *chunk; 2268ccea34b5STejun Heo unsigned long flags; 22693c7be18aSRoman Gushchin int size, off; 2270198790d9SJohn Sperbeck bool need_balance = false; 2271fbf59bc9STejun Heo 2272fbf59bc9STejun Heo if (!ptr) 2273fbf59bc9STejun Heo return; 2274fbf59bc9STejun Heo 2275f528f0b8SCatalin Marinas kmemleak_free_percpu(ptr); 2276f528f0b8SCatalin Marinas 2277129182e5SAndrew Morton addr = __pcpu_ptr_to_addr(ptr); 2278129182e5SAndrew Morton 2279ccea34b5STejun Heo spin_lock_irqsave(&pcpu_lock, flags); 2280fbf59bc9STejun Heo 2281fbf59bc9STejun Heo chunk = pcpu_chunk_addr_search(addr); 2282bba174f5STejun Heo off = addr - chunk->base_addr; 2283fbf59bc9STejun Heo 22843c7be18aSRoman Gushchin size = pcpu_free_area(chunk, off); 22853c7be18aSRoman Gushchin 22863c7be18aSRoman Gushchin pcpu_memcg_free_hook(chunk, off, size); 2287fbf59bc9STejun Heo 2288f1833241SRoman Gushchin /* 2289f1833241SRoman Gushchin * If there are more than one fully free chunks, wake up grim reaper. 2290f1833241SRoman Gushchin * If the chunk is isolated, it may be in the process of being 2291f1833241SRoman Gushchin * reclaimed. Let reclaim manage cleaning up of that chunk. 2292f1833241SRoman Gushchin */ 2293f1833241SRoman Gushchin if (!chunk->isolated && chunk->free_bytes == pcpu_unit_size) { 2294fbf59bc9STejun Heo struct pcpu_chunk *pos; 2295fbf59bc9STejun Heo 2296faf65ddeSRoman Gushchin list_for_each_entry(pos, &pcpu_chunk_lists[pcpu_free_slot], list) 2297fbf59bc9STejun Heo if (pos != chunk) { 2298198790d9SJohn Sperbeck need_balance = true; 2299fbf59bc9STejun Heo break; 2300fbf59bc9STejun Heo } 2301f1833241SRoman Gushchin } else if (pcpu_should_reclaim_chunk(chunk)) { 2302f1833241SRoman Gushchin pcpu_isolate_chunk(chunk); 2303f1833241SRoman Gushchin need_balance = true; 2304fbf59bc9STejun Heo } 2305fbf59bc9STejun Heo 2306df95e795SDennis Zhou trace_percpu_free_percpu(chunk->base_addr, off, ptr); 2307df95e795SDennis Zhou 2308ccea34b5STejun Heo spin_unlock_irqrestore(&pcpu_lock, flags); 2309198790d9SJohn Sperbeck 2310198790d9SJohn Sperbeck if (need_balance) 2311198790d9SJohn Sperbeck pcpu_schedule_balance_work(); 2312fbf59bc9STejun Heo } 2313fbf59bc9STejun Heo EXPORT_SYMBOL_GPL(free_percpu); 2314fbf59bc9STejun Heo 2315383776faSThomas Gleixner bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr) 2316383776faSThomas Gleixner { 2317383776faSThomas Gleixner #ifdef CONFIG_SMP 2318383776faSThomas Gleixner const size_t static_size = __per_cpu_end - __per_cpu_start; 2319383776faSThomas Gleixner void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 2320383776faSThomas Gleixner unsigned int cpu; 2321383776faSThomas Gleixner 2322383776faSThomas Gleixner for_each_possible_cpu(cpu) { 2323383776faSThomas Gleixner void *start = per_cpu_ptr(base, cpu); 2324383776faSThomas Gleixner void *va = (void *)addr; 2325383776faSThomas Gleixner 2326383776faSThomas Gleixner if (va >= start && va < start + static_size) { 23278ce371f9SPeter Zijlstra if (can_addr) { 2328383776faSThomas Gleixner *can_addr = (unsigned long) (va - start); 23298ce371f9SPeter Zijlstra *can_addr += (unsigned long) 23308ce371f9SPeter Zijlstra per_cpu_ptr(base, get_boot_cpu_id()); 23318ce371f9SPeter Zijlstra } 2332383776faSThomas Gleixner return true; 2333383776faSThomas Gleixner } 2334383776faSThomas Gleixner } 2335383776faSThomas Gleixner #endif 2336383776faSThomas Gleixner /* on UP, can't distinguish from other static vars, always false */ 2337383776faSThomas Gleixner return false; 2338383776faSThomas Gleixner } 2339383776faSThomas Gleixner 23403b034b0dSVivek Goyal /** 234110fad5e4STejun Heo * is_kernel_percpu_address - test whether address is from static percpu area 234210fad5e4STejun Heo * @addr: address to test 234310fad5e4STejun Heo * 234410fad5e4STejun Heo * Test whether @addr belongs to in-kernel static percpu area. Module 234510fad5e4STejun Heo * static percpu areas are not considered. For those, use 234610fad5e4STejun Heo * is_module_percpu_address(). 234710fad5e4STejun Heo * 234810fad5e4STejun Heo * RETURNS: 234910fad5e4STejun Heo * %true if @addr is from in-kernel static percpu area, %false otherwise. 235010fad5e4STejun Heo */ 235110fad5e4STejun Heo bool is_kernel_percpu_address(unsigned long addr) 235210fad5e4STejun Heo { 2353383776faSThomas Gleixner return __is_kernel_percpu_address(addr, NULL); 235410fad5e4STejun Heo } 235510fad5e4STejun Heo 235610fad5e4STejun Heo /** 23573b034b0dSVivek Goyal * per_cpu_ptr_to_phys - convert translated percpu address to physical address 23583b034b0dSVivek Goyal * @addr: the address to be converted to physical address 23593b034b0dSVivek Goyal * 23603b034b0dSVivek Goyal * Given @addr which is dereferenceable address obtained via one of 23613b034b0dSVivek Goyal * percpu access macros, this function translates it into its physical 23623b034b0dSVivek Goyal * address. The caller is responsible for ensuring @addr stays valid 23633b034b0dSVivek Goyal * until this function finishes. 23643b034b0dSVivek Goyal * 236567589c71SDave Young * percpu allocator has special setup for the first chunk, which currently 236667589c71SDave Young * supports either embedding in linear address space or vmalloc mapping, 236767589c71SDave Young * and, from the second one, the backing allocator (currently either vm or 236867589c71SDave Young * km) provides translation. 236967589c71SDave Young * 2370bffc4375SYannick Guerrini * The addr can be translated simply without checking if it falls into the 237167589c71SDave Young * first chunk. But the current code reflects better how percpu allocator 237267589c71SDave Young * actually works, and the verification can discover both bugs in percpu 237367589c71SDave Young * allocator itself and per_cpu_ptr_to_phys() callers. So we keep current 237467589c71SDave Young * code. 237567589c71SDave Young * 23763b034b0dSVivek Goyal * RETURNS: 23773b034b0dSVivek Goyal * The physical address for @addr. 23783b034b0dSVivek Goyal */ 23793b034b0dSVivek Goyal phys_addr_t per_cpu_ptr_to_phys(void *addr) 23803b034b0dSVivek Goyal { 23819983b6f0STejun Heo void __percpu *base = __addr_to_pcpu_ptr(pcpu_base_addr); 23829983b6f0STejun Heo bool in_first_chunk = false; 2383a855b84cSTejun Heo unsigned long first_low, first_high; 23849983b6f0STejun Heo unsigned int cpu; 23859983b6f0STejun Heo 23869983b6f0STejun Heo /* 2387a855b84cSTejun Heo * The following test on unit_low/high isn't strictly 23889983b6f0STejun Heo * necessary but will speed up lookups of addresses which 23899983b6f0STejun Heo * aren't in the first chunk. 2390c0ebfdc3SDennis Zhou (Facebook) * 2391c0ebfdc3SDennis Zhou (Facebook) * The address check is against full chunk sizes. pcpu_base_addr 2392c0ebfdc3SDennis Zhou (Facebook) * points to the beginning of the first chunk including the 2393c0ebfdc3SDennis Zhou (Facebook) * static region. Assumes good intent as the first chunk may 2394c0ebfdc3SDennis Zhou (Facebook) * not be full (ie. < pcpu_unit_pages in size). 23959983b6f0STejun Heo */ 2396c0ebfdc3SDennis Zhou (Facebook) first_low = (unsigned long)pcpu_base_addr + 2397c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_low_unit_cpu, 0); 2398c0ebfdc3SDennis Zhou (Facebook) first_high = (unsigned long)pcpu_base_addr + 2399c0ebfdc3SDennis Zhou (Facebook) pcpu_unit_page_offset(pcpu_high_unit_cpu, pcpu_unit_pages); 2400a855b84cSTejun Heo if ((unsigned long)addr >= first_low && 2401a855b84cSTejun Heo (unsigned long)addr < first_high) { 24029983b6f0STejun Heo for_each_possible_cpu(cpu) { 24039983b6f0STejun Heo void *start = per_cpu_ptr(base, cpu); 24049983b6f0STejun Heo 24059983b6f0STejun Heo if (addr >= start && addr < start + pcpu_unit_size) { 24069983b6f0STejun Heo in_first_chunk = true; 24079983b6f0STejun Heo break; 24089983b6f0STejun Heo } 24099983b6f0STejun Heo } 24109983b6f0STejun Heo } 24119983b6f0STejun Heo 24129983b6f0STejun Heo if (in_first_chunk) { 2413eac522efSDavid Howells if (!is_vmalloc_addr(addr)) 24143b034b0dSVivek Goyal return __pa(addr); 24153b034b0dSVivek Goyal else 24169f57bd4dSEugene Surovegin return page_to_phys(vmalloc_to_page(addr)) + 24179f57bd4dSEugene Surovegin offset_in_page(addr); 2418020ec653STejun Heo } else 24199f57bd4dSEugene Surovegin return page_to_phys(pcpu_addr_to_page(addr)) + 24209f57bd4dSEugene Surovegin offset_in_page(addr); 24213b034b0dSVivek Goyal } 24223b034b0dSVivek Goyal 2423fbf59bc9STejun Heo /** 2424fd1e8a1fSTejun Heo * pcpu_alloc_alloc_info - allocate percpu allocation info 2425fd1e8a1fSTejun Heo * @nr_groups: the number of groups 2426fd1e8a1fSTejun Heo * @nr_units: the number of units 2427033e48fbSTejun Heo * 2428fd1e8a1fSTejun Heo * Allocate ai which is large enough for @nr_groups groups containing 2429fd1e8a1fSTejun Heo * @nr_units units. The returned ai's groups[0].cpu_map points to the 2430fd1e8a1fSTejun Heo * cpu_map array which is long enough for @nr_units and filled with 2431fd1e8a1fSTejun Heo * NR_CPUS. It's the caller's responsibility to initialize cpu_map 2432fd1e8a1fSTejun Heo * pointer of other groups. 2433033e48fbSTejun Heo * 2434033e48fbSTejun Heo * RETURNS: 2435fd1e8a1fSTejun Heo * Pointer to the allocated pcpu_alloc_info on success, NULL on 2436fd1e8a1fSTejun Heo * failure. 2437033e48fbSTejun Heo */ 2438fd1e8a1fSTejun Heo struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups, 2439fd1e8a1fSTejun Heo int nr_units) 2440fd1e8a1fSTejun Heo { 2441fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 2442fd1e8a1fSTejun Heo size_t base_size, ai_size; 2443fd1e8a1fSTejun Heo void *ptr; 2444fd1e8a1fSTejun Heo int unit; 2445fd1e8a1fSTejun Heo 244614d37612SGustavo A. R. Silva base_size = ALIGN(struct_size(ai, groups, nr_groups), 2447fd1e8a1fSTejun Heo __alignof__(ai->groups[0].cpu_map[0])); 2448fd1e8a1fSTejun Heo ai_size = base_size + nr_units * sizeof(ai->groups[0].cpu_map[0]); 2449fd1e8a1fSTejun Heo 245026fb3daeSMike Rapoport ptr = memblock_alloc(PFN_ALIGN(ai_size), PAGE_SIZE); 2451fd1e8a1fSTejun Heo if (!ptr) 2452fd1e8a1fSTejun Heo return NULL; 2453fd1e8a1fSTejun Heo ai = ptr; 2454fd1e8a1fSTejun Heo ptr += base_size; 2455fd1e8a1fSTejun Heo 2456fd1e8a1fSTejun Heo ai->groups[0].cpu_map = ptr; 2457fd1e8a1fSTejun Heo 2458fd1e8a1fSTejun Heo for (unit = 0; unit < nr_units; unit++) 2459fd1e8a1fSTejun Heo ai->groups[0].cpu_map[unit] = NR_CPUS; 2460fd1e8a1fSTejun Heo 2461fd1e8a1fSTejun Heo ai->nr_groups = nr_groups; 2462fd1e8a1fSTejun Heo ai->__ai_size = PFN_ALIGN(ai_size); 2463fd1e8a1fSTejun Heo 2464fd1e8a1fSTejun Heo return ai; 2465fd1e8a1fSTejun Heo } 2466fd1e8a1fSTejun Heo 2467fd1e8a1fSTejun Heo /** 2468fd1e8a1fSTejun Heo * pcpu_free_alloc_info - free percpu allocation info 2469fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info to free 2470fd1e8a1fSTejun Heo * 2471fd1e8a1fSTejun Heo * Free @ai which was allocated by pcpu_alloc_alloc_info(). 2472fd1e8a1fSTejun Heo */ 2473fd1e8a1fSTejun Heo void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai) 2474fd1e8a1fSTejun Heo { 24754421cca0SMike Rapoport memblock_free(ai, ai->__ai_size); 2476fd1e8a1fSTejun Heo } 2477fd1e8a1fSTejun Heo 2478fd1e8a1fSTejun Heo /** 2479fd1e8a1fSTejun Heo * pcpu_dump_alloc_info - print out information about pcpu_alloc_info 2480fd1e8a1fSTejun Heo * @lvl: loglevel 2481fd1e8a1fSTejun Heo * @ai: allocation info to dump 2482fd1e8a1fSTejun Heo * 2483fd1e8a1fSTejun Heo * Print out information about @ai using loglevel @lvl. 2484fd1e8a1fSTejun Heo */ 2485fd1e8a1fSTejun Heo static void pcpu_dump_alloc_info(const char *lvl, 2486fd1e8a1fSTejun Heo const struct pcpu_alloc_info *ai) 2487033e48fbSTejun Heo { 2488fd1e8a1fSTejun Heo int group_width = 1, cpu_width = 1, width; 2489033e48fbSTejun Heo char empty_str[] = "--------"; 2490fd1e8a1fSTejun Heo int alloc = 0, alloc_end = 0; 2491fd1e8a1fSTejun Heo int group, v; 2492fd1e8a1fSTejun Heo int upa, apl; /* units per alloc, allocs per line */ 2493033e48fbSTejun Heo 2494fd1e8a1fSTejun Heo v = ai->nr_groups; 2495033e48fbSTejun Heo while (v /= 10) 2496fd1e8a1fSTejun Heo group_width++; 2497033e48fbSTejun Heo 2498fd1e8a1fSTejun Heo v = num_possible_cpus(); 2499fd1e8a1fSTejun Heo while (v /= 10) 2500fd1e8a1fSTejun Heo cpu_width++; 2501fd1e8a1fSTejun Heo empty_str[min_t(int, cpu_width, sizeof(empty_str) - 1)] = '\0'; 2502033e48fbSTejun Heo 2503fd1e8a1fSTejun Heo upa = ai->alloc_size / ai->unit_size; 2504fd1e8a1fSTejun Heo width = upa * (cpu_width + 1) + group_width + 3; 2505fd1e8a1fSTejun Heo apl = rounddown_pow_of_two(max(60 / width, 1)); 2506033e48fbSTejun Heo 2507fd1e8a1fSTejun Heo printk("%spcpu-alloc: s%zu r%zu d%zu u%zu alloc=%zu*%zu", 2508fd1e8a1fSTejun Heo lvl, ai->static_size, ai->reserved_size, ai->dyn_size, 2509fd1e8a1fSTejun Heo ai->unit_size, ai->alloc_size / ai->atom_size, ai->atom_size); 2510fd1e8a1fSTejun Heo 2511fd1e8a1fSTejun Heo for (group = 0; group < ai->nr_groups; group++) { 2512fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 2513fd1e8a1fSTejun Heo int unit = 0, unit_end = 0; 2514fd1e8a1fSTejun Heo 2515fd1e8a1fSTejun Heo BUG_ON(gi->nr_units % upa); 2516fd1e8a1fSTejun Heo for (alloc_end += gi->nr_units / upa; 2517fd1e8a1fSTejun Heo alloc < alloc_end; alloc++) { 2518fd1e8a1fSTejun Heo if (!(alloc % apl)) { 25191170532bSJoe Perches pr_cont("\n"); 2520fd1e8a1fSTejun Heo printk("%spcpu-alloc: ", lvl); 2521033e48fbSTejun Heo } 25221170532bSJoe Perches pr_cont("[%0*d] ", group_width, group); 2523fd1e8a1fSTejun Heo 2524fd1e8a1fSTejun Heo for (unit_end += upa; unit < unit_end; unit++) 2525fd1e8a1fSTejun Heo if (gi->cpu_map[unit] != NR_CPUS) 25261170532bSJoe Perches pr_cont("%0*d ", 25271170532bSJoe Perches cpu_width, gi->cpu_map[unit]); 2528033e48fbSTejun Heo else 25291170532bSJoe Perches pr_cont("%s ", empty_str); 2530033e48fbSTejun Heo } 2531fd1e8a1fSTejun Heo } 25321170532bSJoe Perches pr_cont("\n"); 2533033e48fbSTejun Heo } 2534033e48fbSTejun Heo 2535fbf59bc9STejun Heo /** 25368d408b4bSTejun Heo * pcpu_setup_first_chunk - initialize the first percpu chunk 2537fd1e8a1fSTejun Heo * @ai: pcpu_alloc_info describing how to percpu area is shaped 253838a6be52STejun Heo * @base_addr: mapped address 2539fbf59bc9STejun Heo * 25408d408b4bSTejun Heo * Initialize the first percpu chunk which contains the kernel static 254169ab285bSChristophe JAILLET * percpu area. This function is to be called from arch percpu area 254238a6be52STejun Heo * setup path. 25438d408b4bSTejun Heo * 2544fd1e8a1fSTejun Heo * @ai contains all information necessary to initialize the first 2545fd1e8a1fSTejun Heo * chunk and prime the dynamic percpu allocator. 25468d408b4bSTejun Heo * 2547fd1e8a1fSTejun Heo * @ai->static_size is the size of static percpu area. 2548fd1e8a1fSTejun Heo * 2549fd1e8a1fSTejun Heo * @ai->reserved_size, if non-zero, specifies the amount of bytes to 2550edcb4639STejun Heo * reserve after the static area in the first chunk. This reserves 2551edcb4639STejun Heo * the first chunk such that it's available only through reserved 2552edcb4639STejun Heo * percpu allocation. This is primarily used to serve module percpu 2553edcb4639STejun Heo * static areas on architectures where the addressing model has 2554edcb4639STejun Heo * limited offset range for symbol relocations to guarantee module 2555edcb4639STejun Heo * percpu symbols fall inside the relocatable range. 2556edcb4639STejun Heo * 2557fd1e8a1fSTejun Heo * @ai->dyn_size determines the number of bytes available for dynamic 2558fd1e8a1fSTejun Heo * allocation in the first chunk. The area between @ai->static_size + 2559fd1e8a1fSTejun Heo * @ai->reserved_size + @ai->dyn_size and @ai->unit_size is unused. 25606074d5b0STejun Heo * 2561fd1e8a1fSTejun Heo * @ai->unit_size specifies unit size and must be aligned to PAGE_SIZE 2562fd1e8a1fSTejun Heo * and equal to or larger than @ai->static_size + @ai->reserved_size + 2563fd1e8a1fSTejun Heo * @ai->dyn_size. 25648d408b4bSTejun Heo * 2565fd1e8a1fSTejun Heo * @ai->atom_size is the allocation atom size and used as alignment 2566fd1e8a1fSTejun Heo * for vm areas. 25678d408b4bSTejun Heo * 2568fd1e8a1fSTejun Heo * @ai->alloc_size is the allocation size and always multiple of 2569fd1e8a1fSTejun Heo * @ai->atom_size. This is larger than @ai->atom_size if 2570fd1e8a1fSTejun Heo * @ai->unit_size is larger than @ai->atom_size. 2571fd1e8a1fSTejun Heo * 2572fd1e8a1fSTejun Heo * @ai->nr_groups and @ai->groups describe virtual memory layout of 2573fd1e8a1fSTejun Heo * percpu areas. Units which should be colocated are put into the 2574fd1e8a1fSTejun Heo * same group. Dynamic VM areas will be allocated according to these 2575fd1e8a1fSTejun Heo * groupings. If @ai->nr_groups is zero, a single group containing 2576fd1e8a1fSTejun Heo * all units is assumed. 25778d408b4bSTejun Heo * 257838a6be52STejun Heo * The caller should have mapped the first chunk at @base_addr and 257938a6be52STejun Heo * copied static data to each unit. 2580fbf59bc9STejun Heo * 2581c0ebfdc3SDennis Zhou (Facebook) * The first chunk will always contain a static and a dynamic region. 2582c0ebfdc3SDennis Zhou (Facebook) * However, the static region is not managed by any chunk. If the first 2583c0ebfdc3SDennis Zhou (Facebook) * chunk also contains a reserved region, it is served by two chunks - 2584c0ebfdc3SDennis Zhou (Facebook) * one for the reserved region and one for the dynamic region. They 2585c0ebfdc3SDennis Zhou (Facebook) * share the same vm, but use offset regions in the area allocation map. 2586c0ebfdc3SDennis Zhou (Facebook) * The chunk serving the dynamic region is circulated in the chunk slots 2587c0ebfdc3SDennis Zhou (Facebook) * and available for dynamic allocation like any other chunk. 2588fbf59bc9STejun Heo */ 2589163fa234SKefeng Wang void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai, 2590fd1e8a1fSTejun Heo void *base_addr) 2591fbf59bc9STejun Heo { 2592b9c39442SDennis Zhou (Facebook) size_t size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 2593d2f3c384SDennis Zhou (Facebook) size_t static_size, dyn_size; 25940c4169c3SDennis Zhou (Facebook) struct pcpu_chunk *chunk; 25956563297cSTejun Heo unsigned long *group_offsets; 25966563297cSTejun Heo size_t *group_sizes; 2597fb435d52STejun Heo unsigned long *unit_off; 2598fbf59bc9STejun Heo unsigned int cpu; 2599fd1e8a1fSTejun Heo int *unit_map; 2600fd1e8a1fSTejun Heo int group, unit, i; 2601c0ebfdc3SDennis Zhou (Facebook) int map_size; 2602c0ebfdc3SDennis Zhou (Facebook) unsigned long tmp_addr; 2603f655f405SMike Rapoport size_t alloc_size; 2604fbf59bc9STejun Heo 2605635b75fcSTejun Heo #define PCPU_SETUP_BUG_ON(cond) do { \ 2606635b75fcSTejun Heo if (unlikely(cond)) { \ 2607870d4b12SJoe Perches pr_emerg("failed to initialize, %s\n", #cond); \ 2608870d4b12SJoe Perches pr_emerg("cpu_possible_mask=%*pb\n", \ 2609807de073STejun Heo cpumask_pr_args(cpu_possible_mask)); \ 2610635b75fcSTejun Heo pcpu_dump_alloc_info(KERN_EMERG, ai); \ 2611635b75fcSTejun Heo BUG(); \ 2612635b75fcSTejun Heo } \ 2613635b75fcSTejun Heo } while (0) 2614635b75fcSTejun Heo 26152f39e637STejun Heo /* sanity checks */ 2616635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->nr_groups <= 0); 2617bbddff05STejun Heo #ifdef CONFIG_SMP 2618635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!ai->static_size); 2619f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(__per_cpu_start)); 2620bbddff05STejun Heo #endif 2621635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!base_addr); 2622f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(base_addr)); 2623635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < size_sum); 2624f09f1243SAlexander Kuleshov PCPU_SETUP_BUG_ON(offset_in_page(ai->unit_size)); 2625635b75fcSTejun Heo PCPU_SETUP_BUG_ON(ai->unit_size < PCPU_MIN_UNIT_SIZE); 2626ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->unit_size, PCPU_BITMAP_BLOCK_SIZE)); 2627099a19d9STejun Heo PCPU_SETUP_BUG_ON(ai->dyn_size < PERCPU_DYNAMIC_EARLY_SIZE); 2628fb29a2ccSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!ai->dyn_size); 2629d2f3c384SDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!IS_ALIGNED(ai->reserved_size, PCPU_MIN_ALLOC_SIZE)); 2630ca460b3cSDennis Zhou (Facebook) PCPU_SETUP_BUG_ON(!(IS_ALIGNED(PCPU_BITMAP_BLOCK_SIZE, PAGE_SIZE) || 2631ca460b3cSDennis Zhou (Facebook) IS_ALIGNED(PAGE_SIZE, PCPU_BITMAP_BLOCK_SIZE))); 26329f645532STejun Heo PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0); 26338d408b4bSTejun Heo 26346563297cSTejun Heo /* process group information and build config tables accordingly */ 2635f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_offsets[0]); 2636f655f405SMike Rapoport group_offsets = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2637f655f405SMike Rapoport if (!group_offsets) 2638f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2639f655f405SMike Rapoport alloc_size); 2640f655f405SMike Rapoport 2641f655f405SMike Rapoport alloc_size = ai->nr_groups * sizeof(group_sizes[0]); 2642f655f405SMike Rapoport group_sizes = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2643f655f405SMike Rapoport if (!group_sizes) 2644f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2645f655f405SMike Rapoport alloc_size); 2646f655f405SMike Rapoport 2647f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_map[0]); 2648f655f405SMike Rapoport unit_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2649f655f405SMike Rapoport if (!unit_map) 2650f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2651f655f405SMike Rapoport alloc_size); 2652f655f405SMike Rapoport 2653f655f405SMike Rapoport alloc_size = nr_cpu_ids * sizeof(unit_off[0]); 2654f655f405SMike Rapoport unit_off = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 2655f655f405SMike Rapoport if (!unit_off) 2656f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2657f655f405SMike Rapoport alloc_size); 26582f39e637STejun Heo 2659fd1e8a1fSTejun Heo for (cpu = 0; cpu < nr_cpu_ids; cpu++) 2660ffe0d5a5STejun Heo unit_map[cpu] = UINT_MAX; 2661a855b84cSTejun Heo 2662a855b84cSTejun Heo pcpu_low_unit_cpu = NR_CPUS; 2663a855b84cSTejun Heo pcpu_high_unit_cpu = NR_CPUS; 26642f39e637STejun Heo 2665fd1e8a1fSTejun Heo for (group = 0, unit = 0; group < ai->nr_groups; group++, unit += i) { 2666fd1e8a1fSTejun Heo const struct pcpu_group_info *gi = &ai->groups[group]; 26672f39e637STejun Heo 26686563297cSTejun Heo group_offsets[group] = gi->base_offset; 26696563297cSTejun Heo group_sizes[group] = gi->nr_units * ai->unit_size; 26706563297cSTejun Heo 2671fd1e8a1fSTejun Heo for (i = 0; i < gi->nr_units; i++) { 2672fd1e8a1fSTejun Heo cpu = gi->cpu_map[i]; 2673fd1e8a1fSTejun Heo if (cpu == NR_CPUS) 2674fd1e8a1fSTejun Heo continue; 2675fd1e8a1fSTejun Heo 26769f295664SDan Carpenter PCPU_SETUP_BUG_ON(cpu >= nr_cpu_ids); 2677635b75fcSTejun Heo PCPU_SETUP_BUG_ON(!cpu_possible(cpu)); 2678635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] != UINT_MAX); 2679fd1e8a1fSTejun Heo 2680fd1e8a1fSTejun Heo unit_map[cpu] = unit + i; 2681fb435d52STejun Heo unit_off[cpu] = gi->base_offset + i * ai->unit_size; 2682fb435d52STejun Heo 2683a855b84cSTejun Heo /* determine low/high unit_cpu */ 2684a855b84cSTejun Heo if (pcpu_low_unit_cpu == NR_CPUS || 2685a855b84cSTejun Heo unit_off[cpu] < unit_off[pcpu_low_unit_cpu]) 2686a855b84cSTejun Heo pcpu_low_unit_cpu = cpu; 2687a855b84cSTejun Heo if (pcpu_high_unit_cpu == NR_CPUS || 2688a855b84cSTejun Heo unit_off[cpu] > unit_off[pcpu_high_unit_cpu]) 2689a855b84cSTejun Heo pcpu_high_unit_cpu = cpu; 26900fc0531eSLinus Torvalds } 26910fc0531eSLinus Torvalds } 2692fd1e8a1fSTejun Heo pcpu_nr_units = unit; 26932f39e637STejun Heo 26942f39e637STejun Heo for_each_possible_cpu(cpu) 2695635b75fcSTejun Heo PCPU_SETUP_BUG_ON(unit_map[cpu] == UINT_MAX); 2696635b75fcSTejun Heo 2697635b75fcSTejun Heo /* we're done parsing the input, undefine BUG macro and dump config */ 2698635b75fcSTejun Heo #undef PCPU_SETUP_BUG_ON 2699bcbea798STejun Heo pcpu_dump_alloc_info(KERN_DEBUG, ai); 27002f39e637STejun Heo 27016563297cSTejun Heo pcpu_nr_groups = ai->nr_groups; 27026563297cSTejun Heo pcpu_group_offsets = group_offsets; 27036563297cSTejun Heo pcpu_group_sizes = group_sizes; 2704fd1e8a1fSTejun Heo pcpu_unit_map = unit_map; 2705fb435d52STejun Heo pcpu_unit_offsets = unit_off; 27062f39e637STejun Heo 27072f39e637STejun Heo /* determine basic parameters */ 2708fd1e8a1fSTejun Heo pcpu_unit_pages = ai->unit_size >> PAGE_SHIFT; 2709d9b55eebSTejun Heo pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; 27106563297cSTejun Heo pcpu_atom_size = ai->atom_size; 271161cf93d3SDennis Zhou pcpu_chunk_struct_size = struct_size(chunk, populated, 271261cf93d3SDennis Zhou BITS_TO_LONGS(pcpu_unit_pages)); 2713cafe8816STejun Heo 271430a5b536SDennis Zhou pcpu_stats_save_ai(ai); 271530a5b536SDennis Zhou 2716d9b55eebSTejun Heo /* 2717f1833241SRoman Gushchin * Allocate chunk slots. The slots after the active slots are: 2718f1833241SRoman Gushchin * sidelined_slot - isolated, depopulated chunks 2719f1833241SRoman Gushchin * free_slot - fully free chunks 2720f1833241SRoman Gushchin * to_depopulate_slot - isolated, chunks to depopulate 2721d9b55eebSTejun Heo */ 2722f1833241SRoman Gushchin pcpu_sidelined_slot = __pcpu_size_to_slot(pcpu_unit_size) + 1; 2723f1833241SRoman Gushchin pcpu_free_slot = pcpu_sidelined_slot + 1; 2724f1833241SRoman Gushchin pcpu_to_depopulate_slot = pcpu_free_slot + 1; 2725f1833241SRoman Gushchin pcpu_nr_slots = pcpu_to_depopulate_slot + 1; 27263c7be18aSRoman Gushchin pcpu_chunk_lists = memblock_alloc(pcpu_nr_slots * 2727faf65ddeSRoman Gushchin sizeof(pcpu_chunk_lists[0]), 27287e1c4e27SMike Rapoport SMP_CACHE_BYTES); 27293c7be18aSRoman Gushchin if (!pcpu_chunk_lists) 2730f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 2731faf65ddeSRoman Gushchin pcpu_nr_slots * sizeof(pcpu_chunk_lists[0])); 27323c7be18aSRoman Gushchin 2733fbf59bc9STejun Heo for (i = 0; i < pcpu_nr_slots; i++) 2734faf65ddeSRoman Gushchin INIT_LIST_HEAD(&pcpu_chunk_lists[i]); 2735fbf59bc9STejun Heo 2736edcb4639STejun Heo /* 2737d2f3c384SDennis Zhou (Facebook) * The end of the static region needs to be aligned with the 2738d2f3c384SDennis Zhou (Facebook) * minimum allocation size as this offsets the reserved and 2739d2f3c384SDennis Zhou (Facebook) * dynamic region. The first chunk ends page aligned by 2740d2f3c384SDennis Zhou (Facebook) * expanding the dynamic region, therefore the dynamic region 2741d2f3c384SDennis Zhou (Facebook) * can be shrunk to compensate while still staying above the 2742d2f3c384SDennis Zhou (Facebook) * configured sizes. 2743d2f3c384SDennis Zhou (Facebook) */ 2744d2f3c384SDennis Zhou (Facebook) static_size = ALIGN(ai->static_size, PCPU_MIN_ALLOC_SIZE); 2745d2f3c384SDennis Zhou (Facebook) dyn_size = ai->dyn_size - (static_size - ai->static_size); 2746d2f3c384SDennis Zhou (Facebook) 2747d2f3c384SDennis Zhou (Facebook) /* 2748c0ebfdc3SDennis Zhou (Facebook) * Initialize first chunk. 2749c0ebfdc3SDennis Zhou (Facebook) * If the reserved_size is non-zero, this initializes the reserved 2750c0ebfdc3SDennis Zhou (Facebook) * chunk. If the reserved_size is zero, the reserved chunk is NULL 2751c0ebfdc3SDennis Zhou (Facebook) * and the dynamic region is initialized here. The first chunk, 2752c0ebfdc3SDennis Zhou (Facebook) * pcpu_first_chunk, will always point to the chunk that serves 2753c0ebfdc3SDennis Zhou (Facebook) * the dynamic region. 2754edcb4639STejun Heo */ 2755d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size; 2756d2f3c384SDennis Zhou (Facebook) map_size = ai->reserved_size ?: dyn_size; 275740064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 275861ace7faSTejun Heo 2759edcb4639STejun Heo /* init dynamic chunk if necessary */ 2760b9c39442SDennis Zhou (Facebook) if (ai->reserved_size) { 27610c4169c3SDennis Zhou (Facebook) pcpu_reserved_chunk = chunk; 2762b9c39442SDennis Zhou (Facebook) 2763d2f3c384SDennis Zhou (Facebook) tmp_addr = (unsigned long)base_addr + static_size + 2764c0ebfdc3SDennis Zhou (Facebook) ai->reserved_size; 2765d2f3c384SDennis Zhou (Facebook) map_size = dyn_size; 276640064aecSDennis Zhou (Facebook) chunk = pcpu_alloc_first_chunk(tmp_addr, map_size); 2767edcb4639STejun Heo } 2768edcb4639STejun Heo 27692441d15cSTejun Heo /* link the first chunk in */ 27700c4169c3SDennis Zhou (Facebook) pcpu_first_chunk = chunk; 2771faf65ddeSRoman Gushchin pcpu_nr_empty_pop_pages = pcpu_first_chunk->nr_empty_pop_pages; 2772ae9e6bc9STejun Heo pcpu_chunk_relocate(pcpu_first_chunk, -1); 2773fbf59bc9STejun Heo 27747e8a6304SDennis Zhou (Facebook) /* include all regions of the first chunk */ 27757e8a6304SDennis Zhou (Facebook) pcpu_nr_populated += PFN_DOWN(size_sum); 27767e8a6304SDennis Zhou (Facebook) 277730a5b536SDennis Zhou pcpu_stats_chunk_alloc(); 2778df95e795SDennis Zhou trace_percpu_create_chunk(base_addr); 277930a5b536SDennis Zhou 2780fbf59bc9STejun Heo /* we're done */ 2781bba174f5STejun Heo pcpu_base_addr = base_addr; 2782fbf59bc9STejun Heo } 278366c3a757STejun Heo 2784bbddff05STejun Heo #ifdef CONFIG_SMP 2785bbddff05STejun Heo 278617f3609cSAndi Kleen const char * const pcpu_fc_names[PCPU_FC_NR] __initconst = { 2787f58dc01bSTejun Heo [PCPU_FC_AUTO] = "auto", 2788f58dc01bSTejun Heo [PCPU_FC_EMBED] = "embed", 2789f58dc01bSTejun Heo [PCPU_FC_PAGE] = "page", 2790f58dc01bSTejun Heo }; 279166c3a757STejun Heo 2792f58dc01bSTejun Heo enum pcpu_fc pcpu_chosen_fc __initdata = PCPU_FC_AUTO; 2793f58dc01bSTejun Heo 2794f58dc01bSTejun Heo static int __init percpu_alloc_setup(char *str) 279566c3a757STejun Heo { 27965479c78aSCyrill Gorcunov if (!str) 27975479c78aSCyrill Gorcunov return -EINVAL; 27985479c78aSCyrill Gorcunov 2799f58dc01bSTejun Heo if (0) 2800f58dc01bSTejun Heo /* nada */; 2801f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK 2802f58dc01bSTejun Heo else if (!strcmp(str, "embed")) 2803f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_EMBED; 2804f58dc01bSTejun Heo #endif 2805f58dc01bSTejun Heo #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 2806f58dc01bSTejun Heo else if (!strcmp(str, "page")) 2807f58dc01bSTejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 2808f58dc01bSTejun Heo #endif 2809f58dc01bSTejun Heo else 2810870d4b12SJoe Perches pr_warn("unknown allocator %s specified\n", str); 281166c3a757STejun Heo 2812f58dc01bSTejun Heo return 0; 281366c3a757STejun Heo } 2814f58dc01bSTejun Heo early_param("percpu_alloc", percpu_alloc_setup); 281566c3a757STejun Heo 28163c9a024fSTejun Heo /* 28173c9a024fSTejun Heo * pcpu_embed_first_chunk() is used by the generic percpu setup. 28183c9a024fSTejun Heo * Build it if needed by the arch config or the generic setup is going 28193c9a024fSTejun Heo * to be used. 28203c9a024fSTejun Heo */ 282108fc4580STejun Heo #if defined(CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK) || \ 282208fc4580STejun Heo !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) 28233c9a024fSTejun Heo #define BUILD_EMBED_FIRST_CHUNK 28243c9a024fSTejun Heo #endif 28253c9a024fSTejun Heo 28263c9a024fSTejun Heo /* build pcpu_page_first_chunk() iff needed by the arch config */ 28273c9a024fSTejun Heo #if defined(CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK) 28283c9a024fSTejun Heo #define BUILD_PAGE_FIRST_CHUNK 28293c9a024fSTejun Heo #endif 28303c9a024fSTejun Heo 28313c9a024fSTejun Heo /* pcpu_build_alloc_info() is used by both embed and page first chunk */ 28323c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) || defined(BUILD_PAGE_FIRST_CHUNK) 28333c9a024fSTejun Heo /** 2834fbf59bc9STejun Heo * pcpu_build_alloc_info - build alloc_info considering distances between CPUs 2835fbf59bc9STejun Heo * @reserved_size: the size of reserved percpu area in bytes 2836fbf59bc9STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 2837fbf59bc9STejun Heo * @atom_size: allocation atom size 2838fbf59bc9STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 2839fbf59bc9STejun Heo * 2840fbf59bc9STejun Heo * This function determines grouping of units, their mappings to cpus 2841fbf59bc9STejun Heo * and other parameters considering needed percpu size, allocation 2842fbf59bc9STejun Heo * atom size and distances between CPUs. 2843fbf59bc9STejun Heo * 2844bffc4375SYannick Guerrini * Groups are always multiples of atom size and CPUs which are of 2845fbf59bc9STejun Heo * LOCAL_DISTANCE both ways are grouped together and share space for 2846fbf59bc9STejun Heo * units in the same group. The returned configuration is guaranteed 2847fbf59bc9STejun Heo * to have CPUs on different nodes on different groups and >=75% usage 2848fbf59bc9STejun Heo * of allocated virtual address space. 2849fbf59bc9STejun Heo * 2850fbf59bc9STejun Heo * RETURNS: 2851fbf59bc9STejun Heo * On success, pointer to the new allocation_info is returned. On 2852fbf59bc9STejun Heo * failure, ERR_PTR value is returned. 2853fbf59bc9STejun Heo */ 2854258e0815SDennis Zhou static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info( 2855fbf59bc9STejun Heo size_t reserved_size, size_t dyn_size, 2856fbf59bc9STejun Heo size_t atom_size, 2857fbf59bc9STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn) 2858fbf59bc9STejun Heo { 2859fbf59bc9STejun Heo static int group_map[NR_CPUS] __initdata; 2860fbf59bc9STejun Heo static int group_cnt[NR_CPUS] __initdata; 2861d7d29ac7SWonhyuk Yang static struct cpumask mask __initdata; 2862fbf59bc9STejun Heo const size_t static_size = __per_cpu_end - __per_cpu_start; 2863fbf59bc9STejun Heo int nr_groups = 1, nr_units = 0; 2864fbf59bc9STejun Heo size_t size_sum, min_unit_size, alloc_size; 28653f649ab7SKees Cook int upa, max_upa, best_upa; /* units_per_alloc */ 2866fbf59bc9STejun Heo int last_allocs, group, unit; 2867fbf59bc9STejun Heo unsigned int cpu, tcpu; 2868fbf59bc9STejun Heo struct pcpu_alloc_info *ai; 2869fbf59bc9STejun Heo unsigned int *cpu_map; 2870fbf59bc9STejun Heo 2871fbf59bc9STejun Heo /* this function may be called multiple times */ 2872fbf59bc9STejun Heo memset(group_map, 0, sizeof(group_map)); 2873fbf59bc9STejun Heo memset(group_cnt, 0, sizeof(group_cnt)); 2874d7d29ac7SWonhyuk Yang cpumask_clear(&mask); 2875fbf59bc9STejun Heo 2876fbf59bc9STejun Heo /* calculate size_sum and ensure dyn_size is enough for early alloc */ 2877fbf59bc9STejun Heo size_sum = PFN_ALIGN(static_size + reserved_size + 2878fbf59bc9STejun Heo max_t(size_t, dyn_size, PERCPU_DYNAMIC_EARLY_SIZE)); 2879fbf59bc9STejun Heo dyn_size = size_sum - static_size - reserved_size; 2880fbf59bc9STejun Heo 2881fbf59bc9STejun Heo /* 2882fbf59bc9STejun Heo * Determine min_unit_size, alloc_size and max_upa such that 2883fbf59bc9STejun Heo * alloc_size is multiple of atom_size and is the smallest 288425985edcSLucas De Marchi * which can accommodate 4k aligned segments which are equal to 2885fbf59bc9STejun Heo * or larger than min_unit_size. 2886fbf59bc9STejun Heo */ 2887fbf59bc9STejun Heo min_unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); 2888fbf59bc9STejun Heo 28899c015162SDennis Zhou (Facebook) /* determine the maximum # of units that can fit in an allocation */ 2890fbf59bc9STejun Heo alloc_size = roundup(min_unit_size, atom_size); 2891fbf59bc9STejun Heo upa = alloc_size / min_unit_size; 2892f09f1243SAlexander Kuleshov while (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2893fbf59bc9STejun Heo upa--; 2894fbf59bc9STejun Heo max_upa = upa; 2895fbf59bc9STejun Heo 2896d7d29ac7SWonhyuk Yang cpumask_copy(&mask, cpu_possible_mask); 2897d7d29ac7SWonhyuk Yang 2898fbf59bc9STejun Heo /* group cpus according to their proximity */ 2899d7d29ac7SWonhyuk Yang for (group = 0; !cpumask_empty(&mask); group++) { 2900d7d29ac7SWonhyuk Yang /* pop the group's first cpu */ 2901d7d29ac7SWonhyuk Yang cpu = cpumask_first(&mask); 2902fbf59bc9STejun Heo group_map[cpu] = group; 2903fbf59bc9STejun Heo group_cnt[group]++; 2904d7d29ac7SWonhyuk Yang cpumask_clear_cpu(cpu, &mask); 2905d7d29ac7SWonhyuk Yang 2906d7d29ac7SWonhyuk Yang for_each_cpu(tcpu, &mask) { 2907d7d29ac7SWonhyuk Yang if (!cpu_distance_fn || 2908d7d29ac7SWonhyuk Yang (cpu_distance_fn(cpu, tcpu) == LOCAL_DISTANCE && 2909d7d29ac7SWonhyuk Yang cpu_distance_fn(tcpu, cpu) == LOCAL_DISTANCE)) { 2910d7d29ac7SWonhyuk Yang group_map[tcpu] = group; 2911d7d29ac7SWonhyuk Yang group_cnt[group]++; 2912d7d29ac7SWonhyuk Yang cpumask_clear_cpu(tcpu, &mask); 2913fbf59bc9STejun Heo } 2914d7d29ac7SWonhyuk Yang } 2915d7d29ac7SWonhyuk Yang } 2916d7d29ac7SWonhyuk Yang nr_groups = group; 2917fbf59bc9STejun Heo 2918fbf59bc9STejun Heo /* 29199c015162SDennis Zhou (Facebook) * Wasted space is caused by a ratio imbalance of upa to group_cnt. 29209c015162SDennis Zhou (Facebook) * Expand the unit_size until we use >= 75% of the units allocated. 29219c015162SDennis Zhou (Facebook) * Related to atom_size, which could be much larger than the unit_size. 2922fbf59bc9STejun Heo */ 2923fbf59bc9STejun Heo last_allocs = INT_MAX; 29244829c791SDennis Zhou best_upa = 0; 2925fbf59bc9STejun Heo for (upa = max_upa; upa; upa--) { 2926fbf59bc9STejun Heo int allocs = 0, wasted = 0; 2927fbf59bc9STejun Heo 2928f09f1243SAlexander Kuleshov if (alloc_size % upa || (offset_in_page(alloc_size / upa))) 2929fbf59bc9STejun Heo continue; 2930fbf59bc9STejun Heo 2931fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2932fbf59bc9STejun Heo int this_allocs = DIV_ROUND_UP(group_cnt[group], upa); 2933fbf59bc9STejun Heo allocs += this_allocs; 2934fbf59bc9STejun Heo wasted += this_allocs * upa - group_cnt[group]; 2935fbf59bc9STejun Heo } 2936fbf59bc9STejun Heo 2937fbf59bc9STejun Heo /* 2938fbf59bc9STejun Heo * Don't accept if wastage is over 1/3. The 2939fbf59bc9STejun Heo * greater-than comparison ensures upa==1 always 2940fbf59bc9STejun Heo * passes the following check. 2941fbf59bc9STejun Heo */ 2942fbf59bc9STejun Heo if (wasted > num_possible_cpus() / 3) 2943fbf59bc9STejun Heo continue; 2944fbf59bc9STejun Heo 2945fbf59bc9STejun Heo /* and then don't consume more memory */ 2946fbf59bc9STejun Heo if (allocs > last_allocs) 2947fbf59bc9STejun Heo break; 2948fbf59bc9STejun Heo last_allocs = allocs; 2949fbf59bc9STejun Heo best_upa = upa; 2950fbf59bc9STejun Heo } 29514829c791SDennis Zhou BUG_ON(!best_upa); 2952fbf59bc9STejun Heo upa = best_upa; 2953fbf59bc9STejun Heo 2954fbf59bc9STejun Heo /* allocate and fill alloc_info */ 2955fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) 2956fbf59bc9STejun Heo nr_units += roundup(group_cnt[group], upa); 2957fbf59bc9STejun Heo 2958fbf59bc9STejun Heo ai = pcpu_alloc_alloc_info(nr_groups, nr_units); 2959fbf59bc9STejun Heo if (!ai) 2960fbf59bc9STejun Heo return ERR_PTR(-ENOMEM); 2961fbf59bc9STejun Heo cpu_map = ai->groups[0].cpu_map; 2962fbf59bc9STejun Heo 2963fbf59bc9STejun Heo for (group = 0; group < nr_groups; group++) { 2964fbf59bc9STejun Heo ai->groups[group].cpu_map = cpu_map; 2965fbf59bc9STejun Heo cpu_map += roundup(group_cnt[group], upa); 2966fbf59bc9STejun Heo } 2967fbf59bc9STejun Heo 2968fbf59bc9STejun Heo ai->static_size = static_size; 2969fbf59bc9STejun Heo ai->reserved_size = reserved_size; 2970fbf59bc9STejun Heo ai->dyn_size = dyn_size; 2971fbf59bc9STejun Heo ai->unit_size = alloc_size / upa; 2972fbf59bc9STejun Heo ai->atom_size = atom_size; 2973fbf59bc9STejun Heo ai->alloc_size = alloc_size; 2974fbf59bc9STejun Heo 29752de7852fSPeng Fan for (group = 0, unit = 0; group < nr_groups; group++) { 2976fbf59bc9STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 2977fbf59bc9STejun Heo 2978fbf59bc9STejun Heo /* 2979fbf59bc9STejun Heo * Initialize base_offset as if all groups are located 2980fbf59bc9STejun Heo * back-to-back. The caller should update this to 2981fbf59bc9STejun Heo * reflect actual allocation. 2982fbf59bc9STejun Heo */ 2983fbf59bc9STejun Heo gi->base_offset = unit * ai->unit_size; 2984fbf59bc9STejun Heo 2985fbf59bc9STejun Heo for_each_possible_cpu(cpu) 2986fbf59bc9STejun Heo if (group_map[cpu] == group) 2987fbf59bc9STejun Heo gi->cpu_map[gi->nr_units++] = cpu; 2988fbf59bc9STejun Heo gi->nr_units = roundup(gi->nr_units, upa); 2989fbf59bc9STejun Heo unit += gi->nr_units; 2990fbf59bc9STejun Heo } 2991fbf59bc9STejun Heo BUG_ON(unit != nr_units); 2992fbf59bc9STejun Heo 2993fbf59bc9STejun Heo return ai; 2994fbf59bc9STejun Heo } 29953c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK || BUILD_PAGE_FIRST_CHUNK */ 2996fbf59bc9STejun Heo 29973c9a024fSTejun Heo #if defined(BUILD_EMBED_FIRST_CHUNK) 299866c3a757STejun Heo /** 299966c3a757STejun Heo * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem 300066c3a757STejun Heo * @reserved_size: the size of reserved percpu area in bytes 30014ba6ce25STejun Heo * @dyn_size: minimum free size for dynamic allocation in bytes 3002c8826dd5STejun Heo * @atom_size: allocation atom size 3003c8826dd5STejun Heo * @cpu_distance_fn: callback to determine distance between cpus, optional 3004c8826dd5STejun Heo * @alloc_fn: function to allocate percpu page 300525985edcSLucas De Marchi * @free_fn: function to free percpu page 300666c3a757STejun Heo * 300766c3a757STejun Heo * This is a helper to ease setting up embedded first percpu chunk and 300866c3a757STejun Heo * can be called where pcpu_setup_first_chunk() is expected. 300966c3a757STejun Heo * 301066c3a757STejun Heo * If this function is used to setup the first chunk, it is allocated 3011c8826dd5STejun Heo * by calling @alloc_fn and used as-is without being mapped into 3012c8826dd5STejun Heo * vmalloc area. Allocations are always whole multiples of @atom_size 3013c8826dd5STejun Heo * aligned to @atom_size. 3014c8826dd5STejun Heo * 3015c8826dd5STejun Heo * This enables the first chunk to piggy back on the linear physical 3016c8826dd5STejun Heo * mapping which often uses larger page size. Please note that this 3017c8826dd5STejun Heo * can result in very sparse cpu->unit mapping on NUMA machines thus 3018c8826dd5STejun Heo * requiring large vmalloc address space. Don't use this allocator if 3019c8826dd5STejun Heo * vmalloc space is not orders of magnitude larger than distances 3020c8826dd5STejun Heo * between node memory addresses (ie. 32bit NUMA machines). 302166c3a757STejun Heo * 30224ba6ce25STejun Heo * @dyn_size specifies the minimum dynamic area size. 302366c3a757STejun Heo * 302466c3a757STejun Heo * If the needed size is smaller than the minimum or specified unit 3025c8826dd5STejun Heo * size, the leftover is returned using @free_fn. 302666c3a757STejun Heo * 302766c3a757STejun Heo * RETURNS: 3028fb435d52STejun Heo * 0 on success, -errno on failure. 302966c3a757STejun Heo */ 30304ba6ce25STejun Heo int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size, 3031c8826dd5STejun Heo size_t atom_size, 3032c8826dd5STejun Heo pcpu_fc_cpu_distance_fn_t cpu_distance_fn, 3033c8826dd5STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 3034c8826dd5STejun Heo pcpu_fc_free_fn_t free_fn) 303566c3a757STejun Heo { 3036c8826dd5STejun Heo void *base = (void *)ULONG_MAX; 3037c8826dd5STejun Heo void **areas = NULL; 3038fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 303993c76b6bSzijun_hu size_t size_sum, areas_size; 304093c76b6bSzijun_hu unsigned long max_distance; 3041163fa234SKefeng Wang int group, i, highest_group, rc = 0; 304266c3a757STejun Heo 3043c8826dd5STejun Heo ai = pcpu_build_alloc_info(reserved_size, dyn_size, atom_size, 3044c8826dd5STejun Heo cpu_distance_fn); 3045fd1e8a1fSTejun Heo if (IS_ERR(ai)) 3046fd1e8a1fSTejun Heo return PTR_ERR(ai); 304766c3a757STejun Heo 3048fd1e8a1fSTejun Heo size_sum = ai->static_size + ai->reserved_size + ai->dyn_size; 3049c8826dd5STejun Heo areas_size = PFN_ALIGN(ai->nr_groups * sizeof(void *)); 305066c3a757STejun Heo 305126fb3daeSMike Rapoport areas = memblock_alloc(areas_size, SMP_CACHE_BYTES); 3052c8826dd5STejun Heo if (!areas) { 3053fb435d52STejun Heo rc = -ENOMEM; 3054c8826dd5STejun Heo goto out_free; 3055fa8a7094STejun Heo } 305666c3a757STejun Heo 30579b739662Szijun_hu /* allocate, copy and determine base address & max_distance */ 30589b739662Szijun_hu highest_group = 0; 3059c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) { 3060c8826dd5STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 3061c8826dd5STejun Heo unsigned int cpu = NR_CPUS; 3062c8826dd5STejun Heo void *ptr; 306366c3a757STejun Heo 3064c8826dd5STejun Heo for (i = 0; i < gi->nr_units && cpu == NR_CPUS; i++) 3065c8826dd5STejun Heo cpu = gi->cpu_map[i]; 3066c8826dd5STejun Heo BUG_ON(cpu == NR_CPUS); 3067c8826dd5STejun Heo 3068c8826dd5STejun Heo /* allocate space for the whole group */ 3069c8826dd5STejun Heo ptr = alloc_fn(cpu, gi->nr_units * ai->unit_size, atom_size); 3070c8826dd5STejun Heo if (!ptr) { 3071c8826dd5STejun Heo rc = -ENOMEM; 3072c8826dd5STejun Heo goto out_free_areas; 3073c8826dd5STejun Heo } 3074f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 3075f528f0b8SCatalin Marinas kmemleak_free(ptr); 3076c8826dd5STejun Heo areas[group] = ptr; 3077c8826dd5STejun Heo 3078c8826dd5STejun Heo base = min(ptr, base); 30799b739662Szijun_hu if (ptr > areas[highest_group]) 30809b739662Szijun_hu highest_group = group; 30819b739662Szijun_hu } 30829b739662Szijun_hu max_distance = areas[highest_group] - base; 30839b739662Szijun_hu max_distance += ai->unit_size * ai->groups[highest_group].nr_units; 30849b739662Szijun_hu 30859b739662Szijun_hu /* warn if maximum distance is further than 75% of vmalloc space */ 30869b739662Szijun_hu if (max_distance > VMALLOC_TOTAL * 3 / 4) { 30879b739662Szijun_hu pr_warn("max_distance=0x%lx too large for vmalloc space 0x%lx\n", 30889b739662Szijun_hu max_distance, VMALLOC_TOTAL); 30899b739662Szijun_hu #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK 30909b739662Szijun_hu /* and fail if we have fallback */ 30919b739662Szijun_hu rc = -EINVAL; 30929b739662Szijun_hu goto out_free_areas; 30939b739662Szijun_hu #endif 309442b64281STejun Heo } 309542b64281STejun Heo 309642b64281STejun Heo /* 309742b64281STejun Heo * Copy data and free unused parts. This should happen after all 309842b64281STejun Heo * allocations are complete; otherwise, we may end up with 309942b64281STejun Heo * overlapping groups. 310042b64281STejun Heo */ 310142b64281STejun Heo for (group = 0; group < ai->nr_groups; group++) { 310242b64281STejun Heo struct pcpu_group_info *gi = &ai->groups[group]; 310342b64281STejun Heo void *ptr = areas[group]; 3104c8826dd5STejun Heo 3105c8826dd5STejun Heo for (i = 0; i < gi->nr_units; i++, ptr += ai->unit_size) { 3106c8826dd5STejun Heo if (gi->cpu_map[i] == NR_CPUS) { 3107c8826dd5STejun Heo /* unused unit, free whole */ 3108c8826dd5STejun Heo free_fn(ptr, ai->unit_size); 3109c8826dd5STejun Heo continue; 3110c8826dd5STejun Heo } 3111c8826dd5STejun Heo /* copy and return the unused part */ 3112fd1e8a1fSTejun Heo memcpy(ptr, __per_cpu_load, ai->static_size); 3113c8826dd5STejun Heo free_fn(ptr + size_sum, ai->unit_size - size_sum); 3114c8826dd5STejun Heo } 311566c3a757STejun Heo } 311666c3a757STejun Heo 3117c8826dd5STejun Heo /* base address is now known, determine group base offsets */ 31186ea529a2STejun Heo for (group = 0; group < ai->nr_groups; group++) { 3119c8826dd5STejun Heo ai->groups[group].base_offset = areas[group] - base; 31206ea529a2STejun Heo } 3121c8826dd5STejun Heo 312200206a69SMatteo Croce pr_info("Embedded %zu pages/cpu s%zu r%zu d%zu u%zu\n", 312300206a69SMatteo Croce PFN_DOWN(size_sum), ai->static_size, ai->reserved_size, 3124fd1e8a1fSTejun Heo ai->dyn_size, ai->unit_size); 312566c3a757STejun Heo 3126163fa234SKefeng Wang pcpu_setup_first_chunk(ai, base); 3127c8826dd5STejun Heo goto out_free; 3128c8826dd5STejun Heo 3129c8826dd5STejun Heo out_free_areas: 3130c8826dd5STejun Heo for (group = 0; group < ai->nr_groups; group++) 3131f851c8d8SMichael Holzheu if (areas[group]) 3132c8826dd5STejun Heo free_fn(areas[group], 3133c8826dd5STejun Heo ai->groups[group].nr_units * ai->unit_size); 3134c8826dd5STejun Heo out_free: 3135fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 3136c8826dd5STejun Heo if (areas) 31374421cca0SMike Rapoport memblock_free(areas, areas_size); 3138fb435d52STejun Heo return rc; 3139d4b95f80STejun Heo } 31403c9a024fSTejun Heo #endif /* BUILD_EMBED_FIRST_CHUNK */ 3141d4b95f80STejun Heo 31423c9a024fSTejun Heo #ifdef BUILD_PAGE_FIRST_CHUNK 3143d4b95f80STejun Heo /** 314400ae4064STejun Heo * pcpu_page_first_chunk - map the first chunk using PAGE_SIZE pages 3145d4b95f80STejun Heo * @reserved_size: the size of reserved percpu area in bytes 3146d4b95f80STejun Heo * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE 314725985edcSLucas De Marchi * @free_fn: function to free percpu page, always called with PAGE_SIZE 3148d4b95f80STejun Heo * @populate_pte_fn: function to populate pte 3149d4b95f80STejun Heo * 315000ae4064STejun Heo * This is a helper to ease setting up page-remapped first percpu 315100ae4064STejun Heo * chunk and can be called where pcpu_setup_first_chunk() is expected. 3152d4b95f80STejun Heo * 3153d4b95f80STejun Heo * This is the basic allocator. Static percpu area is allocated 3154d4b95f80STejun Heo * page-by-page into vmalloc area. 3155d4b95f80STejun Heo * 3156d4b95f80STejun Heo * RETURNS: 3157fb435d52STejun Heo * 0 on success, -errno on failure. 3158d4b95f80STejun Heo */ 3159fb435d52STejun Heo int __init pcpu_page_first_chunk(size_t reserved_size, 3160d4b95f80STejun Heo pcpu_fc_alloc_fn_t alloc_fn, 3161d4b95f80STejun Heo pcpu_fc_free_fn_t free_fn, 3162d4b95f80STejun Heo pcpu_fc_populate_pte_fn_t populate_pte_fn) 3163d4b95f80STejun Heo { 31648f05a6a6STejun Heo static struct vm_struct vm; 3165fd1e8a1fSTejun Heo struct pcpu_alloc_info *ai; 316600ae4064STejun Heo char psize_str[16]; 3167ce3141a2STejun Heo int unit_pages; 3168d4b95f80STejun Heo size_t pages_size; 3169ce3141a2STejun Heo struct page **pages; 3170163fa234SKefeng Wang int unit, i, j, rc = 0; 31718f606604Szijun_hu int upa; 31728f606604Szijun_hu int nr_g0_units; 3173d4b95f80STejun Heo 317400ae4064STejun Heo snprintf(psize_str, sizeof(psize_str), "%luK", PAGE_SIZE >> 10); 317500ae4064STejun Heo 31764ba6ce25STejun Heo ai = pcpu_build_alloc_info(reserved_size, 0, PAGE_SIZE, NULL); 3177fd1e8a1fSTejun Heo if (IS_ERR(ai)) 3178fd1e8a1fSTejun Heo return PTR_ERR(ai); 3179fd1e8a1fSTejun Heo BUG_ON(ai->nr_groups != 1); 31808f606604Szijun_hu upa = ai->alloc_size/ai->unit_size; 31818f606604Szijun_hu nr_g0_units = roundup(num_possible_cpus(), upa); 31820b59c25fSIgor Stoppa if (WARN_ON(ai->groups[0].nr_units != nr_g0_units)) { 31838f606604Szijun_hu pcpu_free_alloc_info(ai); 31848f606604Szijun_hu return -EINVAL; 31858f606604Szijun_hu } 3186fd1e8a1fSTejun Heo 3187fd1e8a1fSTejun Heo unit_pages = ai->unit_size >> PAGE_SHIFT; 3188d4b95f80STejun Heo 3189d4b95f80STejun Heo /* unaligned allocations can't be freed, round up to page size */ 3190fd1e8a1fSTejun Heo pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() * 3191fd1e8a1fSTejun Heo sizeof(pages[0])); 31927e1c4e27SMike Rapoport pages = memblock_alloc(pages_size, SMP_CACHE_BYTES); 3193f655f405SMike Rapoport if (!pages) 3194f655f405SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__, 3195f655f405SMike Rapoport pages_size); 3196d4b95f80STejun Heo 31978f05a6a6STejun Heo /* allocate pages */ 3198d4b95f80STejun Heo j = 0; 31998f606604Szijun_hu for (unit = 0; unit < num_possible_cpus(); unit++) { 3200fd1e8a1fSTejun Heo unsigned int cpu = ai->groups[0].cpu_map[unit]; 32018f606604Szijun_hu for (i = 0; i < unit_pages; i++) { 3202d4b95f80STejun Heo void *ptr; 3203d4b95f80STejun Heo 32043cbc8565STejun Heo ptr = alloc_fn(cpu, PAGE_SIZE, PAGE_SIZE); 3205d4b95f80STejun Heo if (!ptr) { 3206870d4b12SJoe Perches pr_warn("failed to allocate %s page for cpu%u\n", 3207598d8091SJoe Perches psize_str, cpu); 3208d4b95f80STejun Heo goto enomem; 3209d4b95f80STejun Heo } 3210f528f0b8SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 3211f528f0b8SCatalin Marinas kmemleak_free(ptr); 3212ce3141a2STejun Heo pages[j++] = virt_to_page(ptr); 3213d4b95f80STejun Heo } 32148f606604Szijun_hu } 3215d4b95f80STejun Heo 32168f05a6a6STejun Heo /* allocate vm area, map the pages and copy static data */ 32178f05a6a6STejun Heo vm.flags = VM_ALLOC; 3218fd1e8a1fSTejun Heo vm.size = num_possible_cpus() * ai->unit_size; 32198f05a6a6STejun Heo vm_area_register_early(&vm, PAGE_SIZE); 32208f05a6a6STejun Heo 3221fd1e8a1fSTejun Heo for (unit = 0; unit < num_possible_cpus(); unit++) { 32221d9d3257STejun Heo unsigned long unit_addr = 3223fd1e8a1fSTejun Heo (unsigned long)vm.addr + unit * ai->unit_size; 32248f05a6a6STejun Heo 3225ce3141a2STejun Heo for (i = 0; i < unit_pages; i++) 32268f05a6a6STejun Heo populate_pte_fn(unit_addr + (i << PAGE_SHIFT)); 32278f05a6a6STejun Heo 32288f05a6a6STejun Heo /* pte already populated, the following shouldn't fail */ 3229fb435d52STejun Heo rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages], 3230ce3141a2STejun Heo unit_pages); 3231fb435d52STejun Heo if (rc < 0) 3232fb435d52STejun Heo panic("failed to map percpu area, err=%d\n", rc); 32338f05a6a6STejun Heo 32348f05a6a6STejun Heo /* 32358f05a6a6STejun Heo * FIXME: Archs with virtual cache should flush local 32368f05a6a6STejun Heo * cache for the linear mapping here - something 32378f05a6a6STejun Heo * equivalent to flush_cache_vmap() on the local cpu. 32388f05a6a6STejun Heo * flush_cache_vmap() can't be used as most supporting 32398f05a6a6STejun Heo * data structures are not set up yet. 32408f05a6a6STejun Heo */ 32418f05a6a6STejun Heo 32428f05a6a6STejun Heo /* copy static data */ 3243fd1e8a1fSTejun Heo memcpy((void *)unit_addr, __per_cpu_load, ai->static_size); 324466c3a757STejun Heo } 324566c3a757STejun Heo 324666c3a757STejun Heo /* we're ready, commit */ 324700206a69SMatteo Croce pr_info("%d %s pages/cpu s%zu r%zu d%zu\n", 324800206a69SMatteo Croce unit_pages, psize_str, ai->static_size, 3249fd1e8a1fSTejun Heo ai->reserved_size, ai->dyn_size); 325066c3a757STejun Heo 3251163fa234SKefeng Wang pcpu_setup_first_chunk(ai, vm.addr); 3252d4b95f80STejun Heo goto out_free_ar; 3253d4b95f80STejun Heo 3254d4b95f80STejun Heo enomem: 3255d4b95f80STejun Heo while (--j >= 0) 3256ce3141a2STejun Heo free_fn(page_address(pages[j]), PAGE_SIZE); 3257fb435d52STejun Heo rc = -ENOMEM; 3258d4b95f80STejun Heo out_free_ar: 32594421cca0SMike Rapoport memblock_free(pages, pages_size); 3260fd1e8a1fSTejun Heo pcpu_free_alloc_info(ai); 3261fb435d52STejun Heo return rc; 326266c3a757STejun Heo } 32633c9a024fSTejun Heo #endif /* BUILD_PAGE_FIRST_CHUNK */ 3264d4b95f80STejun Heo 3265bbddff05STejun Heo #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA 32668c4bfc6eSTejun Heo /* 3267bbddff05STejun Heo * Generic SMP percpu area setup. 3268e74e3962STejun Heo * 3269e74e3962STejun Heo * The embedding helper is used because its behavior closely resembles 3270e74e3962STejun Heo * the original non-dynamic generic percpu area setup. This is 3271e74e3962STejun Heo * important because many archs have addressing restrictions and might 3272e74e3962STejun Heo * fail if the percpu area is located far away from the previous 3273e74e3962STejun Heo * location. As an added bonus, in non-NUMA cases, embedding is 3274e74e3962STejun Heo * generally a good idea TLB-wise because percpu area can piggy back 3275e74e3962STejun Heo * on the physical linear memory mapping which uses large page 3276e74e3962STejun Heo * mappings on applicable archs. 3277e74e3962STejun Heo */ 3278e74e3962STejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; 3279e74e3962STejun Heo EXPORT_SYMBOL(__per_cpu_offset); 3280e74e3962STejun Heo 3281c8826dd5STejun Heo static void * __init pcpu_dfl_fc_alloc(unsigned int cpu, size_t size, 3282c8826dd5STejun Heo size_t align) 3283c8826dd5STejun Heo { 328426fb3daeSMike Rapoport return memblock_alloc_from(size, align, __pa(MAX_DMA_ADDRESS)); 3285c8826dd5STejun Heo } 3286c8826dd5STejun Heo 3287c8826dd5STejun Heo static void __init pcpu_dfl_fc_free(void *ptr, size_t size) 3288c8826dd5STejun Heo { 32894421cca0SMike Rapoport memblock_free(ptr, size); 3290c8826dd5STejun Heo } 3291c8826dd5STejun Heo 3292e74e3962STejun Heo void __init setup_per_cpu_areas(void) 3293e74e3962STejun Heo { 3294e74e3962STejun Heo unsigned long delta; 3295e74e3962STejun Heo unsigned int cpu; 3296fb435d52STejun Heo int rc; 3297e74e3962STejun Heo 3298e74e3962STejun Heo /* 3299e74e3962STejun Heo * Always reserve area for module percpu variables. That's 3300e74e3962STejun Heo * what the legacy allocator did. 3301e74e3962STejun Heo */ 3302fb435d52STejun Heo rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, 3303c8826dd5STejun Heo PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, NULL, 3304c8826dd5STejun Heo pcpu_dfl_fc_alloc, pcpu_dfl_fc_free); 3305fb435d52STejun Heo if (rc < 0) 3306bbddff05STejun Heo panic("Failed to initialize percpu areas."); 3307e74e3962STejun Heo 3308e74e3962STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 3309e74e3962STejun Heo for_each_possible_cpu(cpu) 3310fb435d52STejun Heo __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; 3311e74e3962STejun Heo } 3312e74e3962STejun Heo #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */ 3313099a19d9STejun Heo 3314bbddff05STejun Heo #else /* CONFIG_SMP */ 3315bbddff05STejun Heo 3316bbddff05STejun Heo /* 3317bbddff05STejun Heo * UP percpu area setup. 3318bbddff05STejun Heo * 3319bbddff05STejun Heo * UP always uses km-based percpu allocator with identity mapping. 3320bbddff05STejun Heo * Static percpu variables are indistinguishable from the usual static 3321bbddff05STejun Heo * variables and don't require any special preparation. 3322bbddff05STejun Heo */ 3323bbddff05STejun Heo void __init setup_per_cpu_areas(void) 3324bbddff05STejun Heo { 3325bbddff05STejun Heo const size_t unit_size = 3326bbddff05STejun Heo roundup_pow_of_two(max_t(size_t, PCPU_MIN_UNIT_SIZE, 3327bbddff05STejun Heo PERCPU_DYNAMIC_RESERVE)); 3328bbddff05STejun Heo struct pcpu_alloc_info *ai; 3329bbddff05STejun Heo void *fc; 3330bbddff05STejun Heo 3331bbddff05STejun Heo ai = pcpu_alloc_alloc_info(1, 1); 333226fb3daeSMike Rapoport fc = memblock_alloc_from(unit_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS)); 3333bbddff05STejun Heo if (!ai || !fc) 3334bbddff05STejun Heo panic("Failed to allocate memory for percpu areas."); 3335100d13c3SCatalin Marinas /* kmemleak tracks the percpu allocations separately */ 3336100d13c3SCatalin Marinas kmemleak_free(fc); 3337bbddff05STejun Heo 3338bbddff05STejun Heo ai->dyn_size = unit_size; 3339bbddff05STejun Heo ai->unit_size = unit_size; 3340bbddff05STejun Heo ai->atom_size = unit_size; 3341bbddff05STejun Heo ai->alloc_size = unit_size; 3342bbddff05STejun Heo ai->groups[0].nr_units = 1; 3343bbddff05STejun Heo ai->groups[0].cpu_map[0] = 0; 3344bbddff05STejun Heo 3345163fa234SKefeng Wang pcpu_setup_first_chunk(ai, fc); 3346438a5061SNicolas Pitre pcpu_free_alloc_info(ai); 3347bbddff05STejun Heo } 3348bbddff05STejun Heo 3349bbddff05STejun Heo #endif /* CONFIG_SMP */ 3350bbddff05STejun Heo 3351099a19d9STejun Heo /* 33527e8a6304SDennis Zhou (Facebook) * pcpu_nr_pages - calculate total number of populated backing pages 33537e8a6304SDennis Zhou (Facebook) * 33547e8a6304SDennis Zhou (Facebook) * This reflects the number of pages populated to back chunks. Metadata is 33557e8a6304SDennis Zhou (Facebook) * excluded in the number exposed in meminfo as the number of backing pages 33567e8a6304SDennis Zhou (Facebook) * scales with the number of cpus and can quickly outweigh the memory used for 33577e8a6304SDennis Zhou (Facebook) * metadata. It also keeps this calculation nice and simple. 33587e8a6304SDennis Zhou (Facebook) * 33597e8a6304SDennis Zhou (Facebook) * RETURNS: 33607e8a6304SDennis Zhou (Facebook) * Total number of populated backing pages in use by the allocator. 33617e8a6304SDennis Zhou (Facebook) */ 33627e8a6304SDennis Zhou (Facebook) unsigned long pcpu_nr_pages(void) 33637e8a6304SDennis Zhou (Facebook) { 33647e8a6304SDennis Zhou (Facebook) return pcpu_nr_populated * pcpu_nr_units; 33657e8a6304SDennis Zhou (Facebook) } 33667e8a6304SDennis Zhou (Facebook) 33677e8a6304SDennis Zhou (Facebook) /* 33681a4d7607STejun Heo * Percpu allocator is initialized early during boot when neither slab or 33691a4d7607STejun Heo * workqueue is available. Plug async management until everything is up 33701a4d7607STejun Heo * and running. 33711a4d7607STejun Heo */ 33721a4d7607STejun Heo static int __init percpu_enable_async(void) 33731a4d7607STejun Heo { 33741a4d7607STejun Heo pcpu_async_enabled = true; 33751a4d7607STejun Heo return 0; 33761a4d7607STejun Heo } 33771a4d7607STejun Heo subsys_initcall(percpu_enable_async); 3378