xref: /linux/arch/x86/kernel/setup_percpu.c (revision b3883a9a1f09e7b41f4dcb1bbd7262216a62d253)
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>
14*b3883a9aSJason 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 
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 
874518e6a0STejun Heo static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
884518e6a0STejun Heo {
89a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
90a530b795STejun Heo 	if (early_cpu_to_node(from) == early_cpu_to_node(to))
91a530b795STejun Heo 		return LOCAL_DISTANCE;
92a530b795STejun Heo 	else
93a530b795STejun Heo 		return REMOTE_DISTANCE;
948ac83757STejun Heo #else
954518e6a0STejun Heo 	return LOCAL_DISTANCE;
968ac83757STejun Heo #endif
9789c92151STejun Heo }
9889c92151STejun Heo 
991ca3fb3aSKefeng Wang static int __init pcpu_cpu_to_node(int cpu)
1001ca3fb3aSKefeng Wang {
1011ca3fb3aSKefeng Wang 	return early_cpu_to_node(cpu);
1021ca3fb3aSKefeng Wang }
1031ca3fb3aSKefeng Wang 
10420c03576SKefeng Wang void __init pcpu_populate_pte(unsigned long addr)
105458a3e64STejun Heo {
106458a3e64STejun Heo 	populate_extra_pte(addr);
107458a3e64STejun Heo }
108458a3e64STejun Heo 
109b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu)
110b2d2f431SBrian Gerst {
111b2d2f431SBrian Gerst #ifdef CONFIG_X86_32
1121dd439feSThomas Gleixner 	struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu),
1131dd439feSThomas Gleixner 					      0xFFFFF);
114b2d2f431SBrian Gerst 
1151dd439feSThomas Gleixner 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
116b2d2f431SBrian Gerst #endif
117b2d2f431SBrian Gerst }
118b2d2f431SBrian Gerst 
119378b39a4SYinghai Lu void __init setup_per_cpu_areas(void)
120378b39a4SYinghai Lu {
1215f5d8405STejun Heo 	unsigned int cpu;
12211124411STejun Heo 	unsigned long delta;
123fb435d52STejun Heo 	int rc;
124a1681965SMike Travis 
125b9726c26SAlexey Dobriyan 	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
126a1681965SMike Travis 		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
127a1681965SMike Travis 
1288ac83757STejun Heo 	/*
1294518e6a0STejun Heo 	 * Allocate percpu area.  Embedding allocator is our favorite;
1304518e6a0STejun Heo 	 * however, on NUMA configurations, it can result in very
1314518e6a0STejun Heo 	 * sparse unit mapping and vmalloc area isn't spacious enough
1324518e6a0STejun Heo 	 * on 32bit.  Use page in that case.
1338ac83757STejun Heo 	 */
1344518e6a0STejun Heo #ifdef CONFIG_X86_32
1354518e6a0STejun Heo 	if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
1364518e6a0STejun Heo 		pcpu_chosen_fc = PCPU_FC_PAGE;
1374518e6a0STejun Heo #endif
138fb435d52STejun Heo 	rc = -EINVAL;
139f58dc01bSTejun Heo 	if (pcpu_chosen_fc != PCPU_FC_PAGE) {
1404518e6a0STejun Heo 		const size_t dyn_size = PERCPU_MODULE_RESERVE +
1414518e6a0STejun Heo 			PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
142d5e28005STejun Heo 		size_t atom_size;
143f58dc01bSTejun Heo 
144d5e28005STejun Heo 		/*
145d5e28005STejun Heo 		 * On 64bit, use PMD_SIZE for atom_size so that embedded
146d5e28005STejun Heo 		 * percpu areas are aligned to PMD.  This, in the future,
147d5e28005STejun Heo 		 * can also allow using PMD mappings in vmalloc area.  Use
148d5e28005STejun Heo 		 * PAGE_SIZE on 32bit as vmalloc space is highly contended
149d5e28005STejun Heo 		 * and large vmalloc area allocs can easily fail.
150d5e28005STejun Heo 		 */
151d5e28005STejun Heo #ifdef CONFIG_X86_64
152d5e28005STejun Heo 		atom_size = PMD_SIZE;
153d5e28005STejun Heo #else
154d5e28005STejun Heo 		atom_size = PAGE_SIZE;
155d5e28005STejun Heo #endif
1564518e6a0STejun Heo 		rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
1574518e6a0STejun Heo 					    dyn_size, atom_size,
1584518e6a0STejun Heo 					    pcpu_cpu_distance,
15923f91716SKefeng Wang 					    pcpu_cpu_to_node);
160fb435d52STejun Heo 		if (rc < 0)
1618d3bcc44SKefeng Wang 			pr_warn("%s allocator failed (%d), falling back to page size\n",
162fb435d52STejun Heo 				pcpu_fc_names[pcpu_chosen_fc], rc);
163fa8a7094STejun Heo 	}
164fb435d52STejun Heo 	if (rc < 0)
1654518e6a0STejun Heo 		rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
16620c03576SKefeng Wang 					   pcpu_cpu_to_node);
167fb435d52STejun Heo 	if (rc < 0)
168fb435d52STejun Heo 		panic("cannot initialize percpu area (err=%d)", rc);
16911124411STejun Heo 
1705f5d8405STejun Heo 	/* alrighty, percpu areas up and running */
17111124411STejun Heo 	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
17211124411STejun Heo 	for_each_possible_cpu(cpu) {
173fb435d52STejun Heo 		per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
17426f80bd6SBrian Gerst 		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
175ea927906SBrian Gerst 		per_cpu(cpu_number, cpu) = cpu;
176b2d2f431SBrian Gerst 		setup_percpu_segment(cpu);
1770d77e7f0SBrian Gerst 		/*
178cf3997f5STejun Heo 		 * Copy data used in early init routines from the
179cf3997f5STejun Heo 		 * initial arrays to the per cpu data areas.  These
180cf3997f5STejun Heo 		 * arrays then become expendable and the *_early_ptr's
181cf3997f5STejun Heo 		 * are zeroed indicating that the static arrays are
182cf3997f5STejun Heo 		 * gone.
1830d77e7f0SBrian Gerst 		 */
184ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC
1850d77e7f0SBrian Gerst 		per_cpu(x86_cpu_to_apicid, cpu) =
1860d77e7f0SBrian Gerst 			early_per_cpu_map(x86_cpu_to_apicid, cpu);
1870d77e7f0SBrian Gerst 		per_cpu(x86_bios_cpu_apicid, cpu) =
1880d77e7f0SBrian Gerst 			early_per_cpu_map(x86_bios_cpu_apicid, cpu);
1893e9e57faSVitaly Kuznetsov 		per_cpu(x86_cpu_to_acpiid, cpu) =
1903e9e57faSVitaly Kuznetsov 			early_per_cpu_map(x86_cpu_to_acpiid, cpu);
191ec70de8bSBrian Gerst #endif
1924c321ff8STejun Heo #ifdef CONFIG_X86_32
1934c321ff8STejun Heo 		per_cpu(x86_cpu_to_logical_apicid, cpu) =
1944c321ff8STejun Heo 			early_per_cpu_map(x86_cpu_to_logical_apicid, cpu);
1954c321ff8STejun Heo #endif
1966470aff6SBrian Gerst #ifdef CONFIG_NUMA
1976470aff6SBrian Gerst 		per_cpu(x86_cpu_to_node_map, cpu) =
1986470aff6SBrian Gerst 			early_per_cpu_map(x86_cpu_to_node_map, cpu);
1999aebbdb6SYinghai Lu 		/*
200a4ce96acSLinus Torvalds 		 * Ensure that the boot cpu numa_node is correct when the boot
2019aebbdb6SYinghai Lu 		 * cpu is on a node that doesn't have memory installed.
2029aebbdb6SYinghai Lu 		 * Also cpu_up() will call cpu_to_node() for APs when
2039aebbdb6SYinghai Lu 		 * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set
2049aebbdb6SYinghai Lu 		 * up later with c_init aka intel_init/amd_init.
2059aebbdb6SYinghai Lu 		 * So set them all (boot cpu and all APs).
2069aebbdb6SYinghai Lu 		 */
2079aebbdb6SYinghai Lu 		set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
2086470aff6SBrian Gerst #endif
2091a51e3a0STejun Heo 		/*
210c273fb3bSDenys Vlasenko 		 * Up to this point, the boot CPU has been using .init.data
2112697fbd5SBrian Gerst 		 * area.  Reload any changed state for the boot CPU.
2121a51e3a0STejun Heo 		 */
213f6e9456cSRobert Richter 		if (!cpu)
214552be871SBrian Gerst 			switch_to_new_gdt(cpu);
215378b39a4SYinghai Lu 	}
216378b39a4SYinghai Lu 
2170d77e7f0SBrian Gerst 	/* indicate the early static arrays will soon be gone */
21822f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC
2190d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
2200d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
2213e9e57faSVitaly Kuznetsov 	early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
22222f25138SJames Bottomley #endif
2234c321ff8STejun Heo #ifdef CONFIG_X86_32
2244c321ff8STejun Heo 	early_per_cpu_ptr(x86_cpu_to_logical_apicid) = NULL;
2254c321ff8STejun Heo #endif
226645a7919STejun Heo #ifdef CONFIG_NUMA
2270d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
2280d77e7f0SBrian Gerst #endif
229378b39a4SYinghai Lu 
230378b39a4SYinghai Lu 	/* Setup node to cpumask map */
231378b39a4SYinghai Lu 	setup_node_to_cpumask_map();
232c2d1cec1SMike Travis 
233c2d1cec1SMike Travis 	/* Setup cpu initialized, callin, callout masks */
234c2d1cec1SMike Travis 	setup_cpu_local_masks();
23523b2a4ddSAndy Lutomirski 
23623b2a4ddSAndy Lutomirski 	/*
237d2b6dc61SAndy Lutomirski 	 * Sync back kernel address range again.  We already did this in
238d2b6dc61SAndy Lutomirski 	 * setup_arch(), but percpu data also needs to be available in
2397f0a002bSJoerg Roedel 	 * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to
2407f0a002bSJoerg Roedel 	 * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available
2417f0a002bSJoerg Roedel 	 * there too.
242945fd17aSThomas Gleixner 	 *
243945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
244945fd17aSThomas Gleixner 	 * this call?
24523b2a4ddSAndy Lutomirski 	 */
246945fd17aSThomas Gleixner 	sync_initial_page_table();
247378b39a4SYinghai Lu }
248