1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 240685236SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 340685236SJoe Perches 4378b39a4SYinghai Lu #include <linux/kernel.h> 5523d0fb4SPaul Gortmaker #include <linux/export.h> 6378b39a4SYinghai Lu #include <linux/init.h> 72013288fSMike Rapoport #include <linux/memblock.h> 8378b39a4SYinghai Lu #include <linux/percpu.h> 9378b39a4SYinghai Lu #include <linux/kexec.h> 10378b39a4SYinghai Lu #include <linux/crash_dump.h> 118a87dd9aSJaswinder Singh Rajput #include <linux/smp.h> 128a87dd9aSJaswinder Singh Rajput #include <linux/topology.h> 135f5d8405STejun Heo #include <linux/pfn.h> 14378b39a4SYinghai Lu #include <asm/sections.h> 15378b39a4SYinghai Lu #include <asm/processor.h> 16523d0fb4SPaul Gortmaker #include <asm/desc.h> 17378b39a4SYinghai Lu #include <asm/setup.h> 18378b39a4SYinghai Lu #include <asm/mpspec.h> 19378b39a4SYinghai Lu #include <asm/apicdef.h> 20378b39a4SYinghai Lu #include <asm/highmem.h> 211a51e3a0STejun Heo #include <asm/proto.h> 2206879033SJaswinder Singh Rajput #include <asm/cpumask.h> 2334019be1SBrian Gerst #include <asm/cpu.h> 2460a5317fSTejun Heo #include <asm/stackprotector.h> 25378b39a4SYinghai Lu 260816b0f0SVlad Zolotarov DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number); 27ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number); 28ea927906SBrian Gerst 291688401aSBrian Gerst #ifdef CONFIG_X86_64 301688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load) 311688401aSBrian Gerst #else 321688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0 331688401aSBrian Gerst #endif 341688401aSBrian Gerst 352c773dd3SJan Beulich DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; 361688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off); 371688401aSBrian Gerst 38404f6aacSKees Cook unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init = { 3934019be1SBrian Gerst [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET, 409939ddafSTejun Heo }; 419939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset); 42378b39a4SYinghai Lu 436b19b0c2STejun Heo /* 446b19b0c2STejun Heo * On x86_64 symbols referenced from code should be reachable using 456b19b0c2STejun Heo * 32bit relocations. Reserve space for static percpu variables in 466b19b0c2STejun Heo * modules so that they are always served from the first chunk which 476b19b0c2STejun Heo * is located at the percpu segment base. On x86_32, anything can 486b19b0c2STejun Heo * address anywhere. No need to reserve space in the first chunk. 496b19b0c2STejun Heo */ 506b19b0c2STejun Heo #ifdef CONFIG_X86_64 516b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE PERCPU_MODULE_RESERVE 526b19b0c2STejun Heo #else 536b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE 0 546b19b0c2STejun Heo #endif 556b19b0c2STejun Heo 564518e6a0STejun Heo #ifdef CONFIG_X86_32 575f5d8405STejun Heo /** 5889c92151STejun Heo * pcpu_need_numa - determine percpu allocation needs to consider NUMA 5989c92151STejun Heo * 6089c92151STejun Heo * If NUMA is not configured or there is only one NUMA node available, 6189c92151STejun Heo * there is no reason to consider NUMA. This function determines 6289c92151STejun Heo * whether percpu allocation should consider NUMA or not. 6389c92151STejun Heo * 6489c92151STejun Heo * RETURNS: 6589c92151STejun Heo * true if NUMA should be considered; otherwise, false. 6689c92151STejun Heo */ 6789c92151STejun Heo static bool __init pcpu_need_numa(void) 6889c92151STejun Heo { 69a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 7089c92151STejun Heo pg_data_t *last = NULL; 7189c92151STejun Heo unsigned int cpu; 7289c92151STejun Heo 7389c92151STejun Heo for_each_possible_cpu(cpu) { 7489c92151STejun Heo int node = early_cpu_to_node(cpu); 7589c92151STejun Heo 7689c92151STejun Heo if (node_online(node) && NODE_DATA(node) && 7789c92151STejun Heo last && last != NODE_DATA(node)) 7889c92151STejun Heo return true; 7989c92151STejun Heo 8089c92151STejun Heo last = NODE_DATA(node); 8189c92151STejun Heo } 8289c92151STejun Heo #endif 8389c92151STejun Heo return false; 8489c92151STejun Heo } 854518e6a0STejun Heo #endif 8689c92151STejun Heo 8789c92151STejun Heo /** 885f5d8405STejun Heo * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu 895f5d8405STejun Heo * @cpu: cpu to allocate for 905f5d8405STejun Heo * @size: size allocation in bytes 915f5d8405STejun Heo * @align: alignment 925f5d8405STejun Heo * 935f5d8405STejun Heo * Allocate @size bytes aligned at @align for cpu @cpu. This wrapper 945f5d8405STejun Heo * does the right thing for NUMA regardless of the current 955f5d8405STejun Heo * configuration. 965f5d8405STejun Heo * 975f5d8405STejun Heo * RETURNS: 985f5d8405STejun Heo * Pointer to the allocated area on success, NULL on failure. 995f5d8405STejun Heo */ 100*1ca3fb3aSKefeng Wang static void * __init pcpu_alloc_bootmem(unsigned int cpu, unsigned long size, unsigned long align, 101*1ca3fb3aSKefeng Wang pcpu_fc_cpu_to_node_fn_t cpu_to_nd_fn) 1025f5d8405STejun Heo { 1035f5d8405STejun Heo const unsigned long goal = __pa(MAX_DMA_ADDRESS); 104a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 105*1ca3fb3aSKefeng Wang int node = cpu_to_nd_fn(cpu); 1065f5d8405STejun Heo void *ptr; 1075f5d8405STejun Heo 1085f5d8405STejun Heo if (!node_online(node) || !NODE_DATA(node)) { 10926fb3daeSMike Rapoport ptr = memblock_alloc_from(size, align, goal); 1105f5d8405STejun Heo pr_info("cpu %d has no node %d or node-local memory\n", 1115f5d8405STejun Heo cpu, node); 1125f5d8405STejun Heo pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n", 1135f5d8405STejun Heo cpu, size, __pa(ptr)); 1145f5d8405STejun Heo } else { 11526fb3daeSMike Rapoport ptr = memblock_alloc_try_nid(size, align, goal, 11697ad1087SMike Rapoport MEMBLOCK_ALLOC_ACCESSIBLE, 117bf2886efSMike Rapoport node); 118bf2886efSMike Rapoport 11940685236SJoe Perches pr_debug("per cpu data for cpu%d %lu bytes on node%d at %016lx\n", 12040685236SJoe Perches cpu, size, node, __pa(ptr)); 1215f5d8405STejun Heo } 1225f5d8405STejun Heo return ptr; 1235f5d8405STejun Heo #else 12426fb3daeSMike Rapoport return memblock_alloc_from(size, align, goal); 1255f5d8405STejun Heo #endif 1265f5d8405STejun Heo } 1275f5d8405STejun Heo 1285f5d8405STejun Heo /* 129d4b95f80STejun Heo * Helpers for first chunk memory allocation 130d4b95f80STejun Heo */ 131*1ca3fb3aSKefeng Wang static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align, 132*1ca3fb3aSKefeng Wang pcpu_fc_cpu_to_node_fn_t cpu_to_nd_fn) 133d4b95f80STejun Heo { 134*1ca3fb3aSKefeng Wang return pcpu_alloc_bootmem(cpu, size, align, cpu_to_nd_fn); 135d4b95f80STejun Heo } 136d4b95f80STejun Heo 137d4b95f80STejun Heo static void __init pcpu_fc_free(void *ptr, size_t size) 138d4b95f80STejun Heo { 1394421cca0SMike Rapoport memblock_free(ptr, size); 140d4b95f80STejun Heo } 141d4b95f80STejun Heo 1424518e6a0STejun Heo static int __init pcpu_cpu_distance(unsigned int from, unsigned int to) 1434518e6a0STejun Heo { 144a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 145a530b795STejun Heo if (early_cpu_to_node(from) == early_cpu_to_node(to)) 146a530b795STejun Heo return LOCAL_DISTANCE; 147a530b795STejun Heo else 148a530b795STejun Heo return REMOTE_DISTANCE; 1498ac83757STejun Heo #else 1504518e6a0STejun Heo return LOCAL_DISTANCE; 1518ac83757STejun Heo #endif 15289c92151STejun Heo } 15389c92151STejun Heo 154*1ca3fb3aSKefeng Wang static int __init pcpu_cpu_to_node(int cpu) 155*1ca3fb3aSKefeng Wang { 156*1ca3fb3aSKefeng Wang return early_cpu_to_node(cpu); 157*1ca3fb3aSKefeng Wang } 158*1ca3fb3aSKefeng Wang 15900ae4064STejun Heo static void __init pcpup_populate_pte(unsigned long addr) 160458a3e64STejun Heo { 161458a3e64STejun Heo populate_extra_pte(addr); 162458a3e64STejun Heo } 163458a3e64STejun Heo 164b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu) 165b2d2f431SBrian Gerst { 166b2d2f431SBrian Gerst #ifdef CONFIG_X86_32 1671dd439feSThomas Gleixner struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu), 1681dd439feSThomas Gleixner 0xFFFFF); 169b2d2f431SBrian Gerst 1701dd439feSThomas Gleixner write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S); 171b2d2f431SBrian Gerst #endif 172b2d2f431SBrian Gerst } 173b2d2f431SBrian Gerst 174378b39a4SYinghai Lu void __init setup_per_cpu_areas(void) 175378b39a4SYinghai Lu { 1765f5d8405STejun Heo unsigned int cpu; 17711124411STejun Heo unsigned long delta; 178fb435d52STejun Heo int rc; 179a1681965SMike Travis 180b9726c26SAlexey Dobriyan pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n", 181a1681965SMike Travis NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids); 182a1681965SMike Travis 1838ac83757STejun Heo /* 1844518e6a0STejun Heo * Allocate percpu area. Embedding allocator is our favorite; 1854518e6a0STejun Heo * however, on NUMA configurations, it can result in very 1864518e6a0STejun Heo * sparse unit mapping and vmalloc area isn't spacious enough 1874518e6a0STejun Heo * on 32bit. Use page in that case. 1888ac83757STejun Heo */ 1894518e6a0STejun Heo #ifdef CONFIG_X86_32 1904518e6a0STejun Heo if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa()) 1914518e6a0STejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 1924518e6a0STejun Heo #endif 193fb435d52STejun Heo rc = -EINVAL; 194f58dc01bSTejun Heo if (pcpu_chosen_fc != PCPU_FC_PAGE) { 1954518e6a0STejun Heo const size_t dyn_size = PERCPU_MODULE_RESERVE + 1964518e6a0STejun Heo PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE; 197d5e28005STejun Heo size_t atom_size; 198f58dc01bSTejun Heo 199d5e28005STejun Heo /* 200d5e28005STejun Heo * On 64bit, use PMD_SIZE for atom_size so that embedded 201d5e28005STejun Heo * percpu areas are aligned to PMD. This, in the future, 202d5e28005STejun Heo * can also allow using PMD mappings in vmalloc area. Use 203d5e28005STejun Heo * PAGE_SIZE on 32bit as vmalloc space is highly contended 204d5e28005STejun Heo * and large vmalloc area allocs can easily fail. 205d5e28005STejun Heo */ 206d5e28005STejun Heo #ifdef CONFIG_X86_64 207d5e28005STejun Heo atom_size = PMD_SIZE; 208d5e28005STejun Heo #else 209d5e28005STejun Heo atom_size = PAGE_SIZE; 210d5e28005STejun Heo #endif 2114518e6a0STejun Heo rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE, 2124518e6a0STejun Heo dyn_size, atom_size, 2134518e6a0STejun Heo pcpu_cpu_distance, 214*1ca3fb3aSKefeng Wang pcpu_cpu_to_node, 2154518e6a0STejun Heo pcpu_fc_alloc, pcpu_fc_free); 216fb435d52STejun Heo if (rc < 0) 2178d3bcc44SKefeng Wang pr_warn("%s allocator failed (%d), falling back to page size\n", 218fb435d52STejun Heo pcpu_fc_names[pcpu_chosen_fc], rc); 219fa8a7094STejun Heo } 220fb435d52STejun Heo if (rc < 0) 2214518e6a0STejun Heo rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE, 222*1ca3fb3aSKefeng Wang pcpu_cpu_to_node, 2234518e6a0STejun Heo pcpu_fc_alloc, pcpu_fc_free, 2244518e6a0STejun Heo pcpup_populate_pte); 225fb435d52STejun Heo if (rc < 0) 226fb435d52STejun Heo panic("cannot initialize percpu area (err=%d)", rc); 22711124411STejun Heo 2285f5d8405STejun Heo /* alrighty, percpu areas up and running */ 22911124411STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 23011124411STejun Heo for_each_possible_cpu(cpu) { 231fb435d52STejun Heo per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu]; 23226f80bd6SBrian Gerst per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu); 233ea927906SBrian Gerst per_cpu(cpu_number, cpu) = cpu; 234b2d2f431SBrian Gerst setup_percpu_segment(cpu); 2350d77e7f0SBrian Gerst /* 236cf3997f5STejun Heo * Copy data used in early init routines from the 237cf3997f5STejun Heo * initial arrays to the per cpu data areas. These 238cf3997f5STejun Heo * arrays then become expendable and the *_early_ptr's 239cf3997f5STejun Heo * are zeroed indicating that the static arrays are 240cf3997f5STejun Heo * gone. 2410d77e7f0SBrian Gerst */ 242ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC 2430d77e7f0SBrian Gerst per_cpu(x86_cpu_to_apicid, cpu) = 2440d77e7f0SBrian Gerst early_per_cpu_map(x86_cpu_to_apicid, cpu); 2450d77e7f0SBrian Gerst per_cpu(x86_bios_cpu_apicid, cpu) = 2460d77e7f0SBrian Gerst early_per_cpu_map(x86_bios_cpu_apicid, cpu); 2473e9e57faSVitaly Kuznetsov per_cpu(x86_cpu_to_acpiid, cpu) = 2483e9e57faSVitaly Kuznetsov early_per_cpu_map(x86_cpu_to_acpiid, cpu); 249ec70de8bSBrian Gerst #endif 2504c321ff8STejun Heo #ifdef CONFIG_X86_32 2514c321ff8STejun Heo per_cpu(x86_cpu_to_logical_apicid, cpu) = 2524c321ff8STejun Heo early_per_cpu_map(x86_cpu_to_logical_apicid, cpu); 2534c321ff8STejun Heo #endif 2546470aff6SBrian Gerst #ifdef CONFIG_NUMA 2556470aff6SBrian Gerst per_cpu(x86_cpu_to_node_map, cpu) = 2566470aff6SBrian Gerst early_per_cpu_map(x86_cpu_to_node_map, cpu); 2579aebbdb6SYinghai Lu /* 258a4ce96acSLinus Torvalds * Ensure that the boot cpu numa_node is correct when the boot 2599aebbdb6SYinghai Lu * cpu is on a node that doesn't have memory installed. 2609aebbdb6SYinghai Lu * Also cpu_up() will call cpu_to_node() for APs when 2619aebbdb6SYinghai Lu * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set 2629aebbdb6SYinghai Lu * up later with c_init aka intel_init/amd_init. 2639aebbdb6SYinghai Lu * So set them all (boot cpu and all APs). 2649aebbdb6SYinghai Lu */ 2659aebbdb6SYinghai Lu set_cpu_numa_node(cpu, early_cpu_to_node(cpu)); 2666470aff6SBrian Gerst #endif 2671a51e3a0STejun Heo /* 268c273fb3bSDenys Vlasenko * Up to this point, the boot CPU has been using .init.data 2692697fbd5SBrian Gerst * area. Reload any changed state for the boot CPU. 2701a51e3a0STejun Heo */ 271f6e9456cSRobert Richter if (!cpu) 272552be871SBrian Gerst switch_to_new_gdt(cpu); 273378b39a4SYinghai Lu } 274378b39a4SYinghai Lu 2750d77e7f0SBrian Gerst /* indicate the early static arrays will soon be gone */ 27622f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC 2770d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_apicid) = NULL; 2780d77e7f0SBrian Gerst early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL; 2793e9e57faSVitaly Kuznetsov early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL; 28022f25138SJames Bottomley #endif 2814c321ff8STejun Heo #ifdef CONFIG_X86_32 2824c321ff8STejun Heo early_per_cpu_ptr(x86_cpu_to_logical_apicid) = NULL; 2834c321ff8STejun Heo #endif 284645a7919STejun Heo #ifdef CONFIG_NUMA 2850d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_node_map) = NULL; 2860d77e7f0SBrian Gerst #endif 287378b39a4SYinghai Lu 288378b39a4SYinghai Lu /* Setup node to cpumask map */ 289378b39a4SYinghai Lu setup_node_to_cpumask_map(); 290c2d1cec1SMike Travis 291c2d1cec1SMike Travis /* Setup cpu initialized, callin, callout masks */ 292c2d1cec1SMike Travis setup_cpu_local_masks(); 29323b2a4ddSAndy Lutomirski 29423b2a4ddSAndy Lutomirski /* 295d2b6dc61SAndy Lutomirski * Sync back kernel address range again. We already did this in 296d2b6dc61SAndy Lutomirski * setup_arch(), but percpu data also needs to be available in 2977f0a002bSJoerg Roedel * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to 2987f0a002bSJoerg Roedel * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available 2997f0a002bSJoerg Roedel * there too. 300945fd17aSThomas Gleixner * 301945fd17aSThomas Gleixner * FIXME: Can the later sync in setup_cpu_entry_areas() replace 302945fd17aSThomas Gleixner * this call? 30323b2a4ddSAndy Lutomirski */ 304945fd17aSThomas Gleixner sync_initial_page_table(); 305378b39a4SYinghai Lu } 306