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> 14b3883a9aSJason A. Donenfeld #include <linux/stackprotector.h> 15378b39a4SYinghai Lu #include <asm/sections.h> 16378b39a4SYinghai Lu #include <asm/processor.h> 17523d0fb4SPaul Gortmaker #include <asm/desc.h> 18378b39a4SYinghai Lu #include <asm/setup.h> 19378b39a4SYinghai Lu #include <asm/mpspec.h> 20378b39a4SYinghai Lu #include <asm/apicdef.h> 21378b39a4SYinghai Lu #include <asm/highmem.h> 221a51e3a0STejun Heo #include <asm/proto.h> 2306879033SJaswinder Singh Rajput #include <asm/cpumask.h> 2434019be1SBrian Gerst #include <asm/cpu.h> 25378b39a4SYinghai Lu 261688401aSBrian Gerst #ifdef CONFIG_X86_64 271688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load) 281688401aSBrian Gerst #else 291688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0 301688401aSBrian Gerst #endif 311688401aSBrian Gerst 322c773dd3SJan Beulich DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; 331688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off); 341688401aSBrian Gerst 35404f6aacSKees Cook unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init = { 3634019be1SBrian Gerst [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET, 379939ddafSTejun Heo }; 389939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset); 39378b39a4SYinghai Lu 406b19b0c2STejun Heo /* 416b19b0c2STejun Heo * On x86_64 symbols referenced from code should be reachable using 426b19b0c2STejun Heo * 32bit relocations. Reserve space for static percpu variables in 436b19b0c2STejun Heo * modules so that they are always served from the first chunk which 446b19b0c2STejun Heo * is located at the percpu segment base. On x86_32, anything can 456b19b0c2STejun Heo * address anywhere. No need to reserve space in the first chunk. 466b19b0c2STejun Heo */ 476b19b0c2STejun Heo #ifdef CONFIG_X86_64 486b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE PERCPU_MODULE_RESERVE 496b19b0c2STejun Heo #else 506b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE 0 516b19b0c2STejun Heo #endif 526b19b0c2STejun Heo 534518e6a0STejun Heo #ifdef CONFIG_X86_32 545f5d8405STejun Heo /** 5589c92151STejun Heo * pcpu_need_numa - determine percpu allocation needs to consider NUMA 5689c92151STejun Heo * 5789c92151STejun Heo * If NUMA is not configured or there is only one NUMA node available, 5889c92151STejun Heo * there is no reason to consider NUMA. This function determines 5989c92151STejun Heo * whether percpu allocation should consider NUMA or not. 6089c92151STejun Heo * 6189c92151STejun Heo * RETURNS: 6289c92151STejun Heo * true if NUMA should be considered; otherwise, false. 6389c92151STejun Heo */ 6489c92151STejun Heo static bool __init pcpu_need_numa(void) 6589c92151STejun Heo { 66a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 6789c92151STejun Heo pg_data_t *last = NULL; 6889c92151STejun Heo unsigned int cpu; 6989c92151STejun Heo 7089c92151STejun Heo for_each_possible_cpu(cpu) { 7189c92151STejun Heo int node = early_cpu_to_node(cpu); 7289c92151STejun Heo 7389c92151STejun Heo if (node_online(node) && NODE_DATA(node) && 7489c92151STejun Heo last && last != NODE_DATA(node)) 7589c92151STejun Heo return true; 7689c92151STejun Heo 7789c92151STejun Heo last = NODE_DATA(node); 7889c92151STejun Heo } 7989c92151STejun Heo #endif 8089c92151STejun Heo return false; 8189c92151STejun Heo } 824518e6a0STejun Heo #endif 8389c92151STejun Heo 844518e6a0STejun Heo static int __init pcpu_cpu_distance(unsigned int from, unsigned int to) 854518e6a0STejun Heo { 86a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA 87a530b795STejun Heo if (early_cpu_to_node(from) == early_cpu_to_node(to)) 88a530b795STejun Heo return LOCAL_DISTANCE; 89a530b795STejun Heo else 90a530b795STejun Heo return REMOTE_DISTANCE; 918ac83757STejun Heo #else 924518e6a0STejun Heo return LOCAL_DISTANCE; 938ac83757STejun Heo #endif 9489c92151STejun Heo } 9589c92151STejun Heo 961ca3fb3aSKefeng Wang static int __init pcpu_cpu_to_node(int cpu) 971ca3fb3aSKefeng Wang { 981ca3fb3aSKefeng Wang return early_cpu_to_node(cpu); 991ca3fb3aSKefeng Wang } 1001ca3fb3aSKefeng Wang 10120c03576SKefeng Wang void __init pcpu_populate_pte(unsigned long addr) 102458a3e64STejun Heo { 103458a3e64STejun Heo populate_extra_pte(addr); 104458a3e64STejun Heo } 105458a3e64STejun Heo 106b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu) 107b2d2f431SBrian Gerst { 108b2d2f431SBrian Gerst #ifdef CONFIG_X86_32 109*1445f6e1SVegard Nossum struct desc_struct d = GDT_ENTRY_INIT(DESC_DATA32 & ~_DESC_DB, 110*1445f6e1SVegard Nossum per_cpu_offset(cpu), 0xFFFFF); 111b2d2f431SBrian Gerst 1121dd439feSThomas Gleixner write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S); 113b2d2f431SBrian Gerst #endif 114b2d2f431SBrian Gerst } 115b2d2f431SBrian Gerst 116378b39a4SYinghai Lu void __init setup_per_cpu_areas(void) 117378b39a4SYinghai Lu { 1185f5d8405STejun Heo unsigned int cpu; 11911124411STejun Heo unsigned long delta; 120fb435d52STejun Heo int rc; 121a1681965SMike Travis 122b9726c26SAlexey Dobriyan pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n", 123a1681965SMike Travis NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids); 124a1681965SMike Travis 1258ac83757STejun Heo /* 1264518e6a0STejun Heo * Allocate percpu area. Embedding allocator is our favorite; 1274518e6a0STejun Heo * however, on NUMA configurations, it can result in very 1284518e6a0STejun Heo * sparse unit mapping and vmalloc area isn't spacious enough 1294518e6a0STejun Heo * on 32bit. Use page in that case. 1308ac83757STejun Heo */ 1314518e6a0STejun Heo #ifdef CONFIG_X86_32 1324518e6a0STejun Heo if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa()) 1334518e6a0STejun Heo pcpu_chosen_fc = PCPU_FC_PAGE; 1344518e6a0STejun Heo #endif 135fb435d52STejun Heo rc = -EINVAL; 136f58dc01bSTejun Heo if (pcpu_chosen_fc != PCPU_FC_PAGE) { 1374518e6a0STejun Heo const size_t dyn_size = PERCPU_MODULE_RESERVE + 1384518e6a0STejun Heo PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE; 139d5e28005STejun Heo size_t atom_size; 140f58dc01bSTejun Heo 141d5e28005STejun Heo /* 142d5e28005STejun Heo * On 64bit, use PMD_SIZE for atom_size so that embedded 143d5e28005STejun Heo * percpu areas are aligned to PMD. This, in the future, 144d5e28005STejun Heo * can also allow using PMD mappings in vmalloc area. Use 145d5e28005STejun Heo * PAGE_SIZE on 32bit as vmalloc space is highly contended 146d5e28005STejun Heo * and large vmalloc area allocs can easily fail. 147d5e28005STejun Heo */ 148d5e28005STejun Heo #ifdef CONFIG_X86_64 149d5e28005STejun Heo atom_size = PMD_SIZE; 150d5e28005STejun Heo #else 151d5e28005STejun Heo atom_size = PAGE_SIZE; 152d5e28005STejun Heo #endif 1534518e6a0STejun Heo rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE, 1544518e6a0STejun Heo dyn_size, atom_size, 1554518e6a0STejun Heo pcpu_cpu_distance, 15623f91716SKefeng Wang pcpu_cpu_to_node); 157fb435d52STejun Heo if (rc < 0) 1588d3bcc44SKefeng Wang pr_warn("%s allocator failed (%d), falling back to page size\n", 159fb435d52STejun Heo pcpu_fc_names[pcpu_chosen_fc], rc); 160fa8a7094STejun Heo } 161fb435d52STejun Heo if (rc < 0) 1624518e6a0STejun Heo rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE, 16320c03576SKefeng Wang pcpu_cpu_to_node); 164fb435d52STejun Heo if (rc < 0) 165fb435d52STejun Heo panic("cannot initialize percpu area (err=%d)", rc); 16611124411STejun Heo 1675f5d8405STejun Heo /* alrighty, percpu areas up and running */ 16811124411STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; 16911124411STejun Heo for_each_possible_cpu(cpu) { 170fb435d52STejun Heo per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu]; 17126f80bd6SBrian Gerst per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu); 1727443b296SThomas Gleixner per_cpu(pcpu_hot.cpu_number, cpu) = cpu; 173b2d2f431SBrian Gerst setup_percpu_segment(cpu); 1740d77e7f0SBrian Gerst /* 175cf3997f5STejun Heo * Copy data used in early init routines from the 176cf3997f5STejun Heo * initial arrays to the per cpu data areas. These 177cf3997f5STejun Heo * arrays then become expendable and the *_early_ptr's 178cf3997f5STejun Heo * are zeroed indicating that the static arrays are 179cf3997f5STejun Heo * gone. 1800d77e7f0SBrian Gerst */ 181ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC 1820d77e7f0SBrian Gerst per_cpu(x86_cpu_to_apicid, cpu) = 1830d77e7f0SBrian Gerst early_per_cpu_map(x86_cpu_to_apicid, cpu); 1843e9e57faSVitaly Kuznetsov per_cpu(x86_cpu_to_acpiid, cpu) = 1853e9e57faSVitaly Kuznetsov early_per_cpu_map(x86_cpu_to_acpiid, cpu); 186ec70de8bSBrian Gerst #endif 1876470aff6SBrian Gerst #ifdef CONFIG_NUMA 1886470aff6SBrian Gerst per_cpu(x86_cpu_to_node_map, cpu) = 1896470aff6SBrian Gerst early_per_cpu_map(x86_cpu_to_node_map, cpu); 1909aebbdb6SYinghai Lu /* 191a4ce96acSLinus Torvalds * Ensure that the boot cpu numa_node is correct when the boot 1929aebbdb6SYinghai Lu * cpu is on a node that doesn't have memory installed. 1939aebbdb6SYinghai Lu * Also cpu_up() will call cpu_to_node() for APs when 1949aebbdb6SYinghai Lu * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set 1959aebbdb6SYinghai Lu * up later with c_init aka intel_init/amd_init. 1969aebbdb6SYinghai Lu * So set them all (boot cpu and all APs). 1979aebbdb6SYinghai Lu */ 1989aebbdb6SYinghai Lu set_cpu_numa_node(cpu, early_cpu_to_node(cpu)); 1996470aff6SBrian Gerst #endif 2001a51e3a0STejun Heo /* 201c273fb3bSDenys Vlasenko * Up to this point, the boot CPU has been using .init.data 2022697fbd5SBrian Gerst * area. Reload any changed state for the boot CPU. 2031a51e3a0STejun Heo */ 204f6e9456cSRobert Richter if (!cpu) 2051f19e2d5SThomas Gleixner switch_gdt_and_percpu_base(cpu); 206378b39a4SYinghai Lu } 207378b39a4SYinghai Lu 2080d77e7f0SBrian Gerst /* indicate the early static arrays will soon be gone */ 20922f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC 2100d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_apicid) = NULL; 2113e9e57faSVitaly Kuznetsov early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL; 21222f25138SJames Bottomley #endif 213645a7919STejun Heo #ifdef CONFIG_NUMA 2140d77e7f0SBrian Gerst early_per_cpu_ptr(x86_cpu_to_node_map) = NULL; 2150d77e7f0SBrian Gerst #endif 216378b39a4SYinghai Lu 217378b39a4SYinghai Lu /* Setup node to cpumask map */ 218378b39a4SYinghai Lu setup_node_to_cpumask_map(); 219c2d1cec1SMike Travis 220c2d1cec1SMike Travis /* Setup cpu initialized, callin, callout masks */ 221c2d1cec1SMike Travis setup_cpu_local_masks(); 22223b2a4ddSAndy Lutomirski 22323b2a4ddSAndy Lutomirski /* 224d2b6dc61SAndy Lutomirski * Sync back kernel address range again. We already did this in 225d2b6dc61SAndy Lutomirski * setup_arch(), but percpu data also needs to be available in 2267f0a002bSJoerg Roedel * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to 2277f0a002bSJoerg Roedel * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available 2287f0a002bSJoerg Roedel * there too. 229945fd17aSThomas Gleixner * 230945fd17aSThomas Gleixner * FIXME: Can the later sync in setup_cpu_entry_areas() replace 231945fd17aSThomas Gleixner * this call? 23223b2a4ddSAndy Lutomirski */ 233945fd17aSThomas Gleixner sync_initial_page_table(); 234378b39a4SYinghai Lu } 235