xref: /linux/arch/x86/kernel/setup_percpu.c (revision c273fb3b5d0490d3058f6cce77a92860671ee7b6)
140685236SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
240685236SJoe Perches 
3378b39a4SYinghai Lu #include <linux/kernel.h>
4378b39a4SYinghai Lu #include <linux/module.h>
5378b39a4SYinghai Lu #include <linux/init.h>
6378b39a4SYinghai Lu #include <linux/bootmem.h>
7378b39a4SYinghai Lu #include <linux/percpu.h>
8378b39a4SYinghai Lu #include <linux/kexec.h>
9378b39a4SYinghai Lu #include <linux/crash_dump.h>
108a87dd9aSJaswinder Singh Rajput #include <linux/smp.h>
118a87dd9aSJaswinder Singh Rajput #include <linux/topology.h>
125f5d8405STejun Heo #include <linux/pfn.h>
13378b39a4SYinghai Lu #include <asm/sections.h>
14378b39a4SYinghai Lu #include <asm/processor.h>
15378b39a4SYinghai Lu #include <asm/setup.h>
16378b39a4SYinghai Lu #include <asm/mpspec.h>
17378b39a4SYinghai Lu #include <asm/apicdef.h>
18378b39a4SYinghai Lu #include <asm/highmem.h>
191a51e3a0STejun Heo #include <asm/proto.h>
2006879033SJaswinder Singh Rajput #include <asm/cpumask.h>
2134019be1SBrian Gerst #include <asm/cpu.h>
2260a5317fSTejun Heo #include <asm/stackprotector.h>
23378b39a4SYinghai Lu 
24c90aa894SMike Travis #ifdef CONFIG_DEBUG_PER_CPU_MAPS
2540685236SJoe Perches # define DBG(fmt, ...) pr_dbg(fmt, ##__VA_ARGS__)
26c90aa894SMike Travis #else
2740685236SJoe Perches # define DBG(fmt, ...) do { if (0) pr_dbg(fmt, ##__VA_ARGS__); } while (0)
28c90aa894SMike Travis #endif
29c90aa894SMike Travis 
30ea927906SBrian Gerst DEFINE_PER_CPU(int, cpu_number);
31ea927906SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number);
32ea927906SBrian Gerst 
331688401aSBrian Gerst #ifdef CONFIG_X86_64
341688401aSBrian Gerst #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
351688401aSBrian Gerst #else
361688401aSBrian Gerst #define BOOT_PERCPU_OFFSET 0
371688401aSBrian Gerst #endif
381688401aSBrian Gerst 
391688401aSBrian Gerst DEFINE_PER_CPU(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
401688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off);
411688401aSBrian Gerst 
429939ddafSTejun Heo unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
4334019be1SBrian Gerst 	[0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
449939ddafSTejun Heo };
459939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset);
46378b39a4SYinghai Lu 
476b19b0c2STejun Heo /*
486b19b0c2STejun Heo  * On x86_64 symbols referenced from code should be reachable using
496b19b0c2STejun Heo  * 32bit relocations.  Reserve space for static percpu variables in
506b19b0c2STejun Heo  * modules so that they are always served from the first chunk which
516b19b0c2STejun Heo  * is located at the percpu segment base.  On x86_32, anything can
526b19b0c2STejun Heo  * address anywhere.  No need to reserve space in the first chunk.
536b19b0c2STejun Heo  */
546b19b0c2STejun Heo #ifdef CONFIG_X86_64
556b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE	PERCPU_MODULE_RESERVE
566b19b0c2STejun Heo #else
576b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE	0
586b19b0c2STejun Heo #endif
596b19b0c2STejun Heo 
604518e6a0STejun Heo #ifdef CONFIG_X86_32
615f5d8405STejun Heo /**
6289c92151STejun Heo  * pcpu_need_numa - determine percpu allocation needs to consider NUMA
6389c92151STejun Heo  *
6489c92151STejun Heo  * If NUMA is not configured or there is only one NUMA node available,
6589c92151STejun Heo  * there is no reason to consider NUMA.  This function determines
6689c92151STejun Heo  * whether percpu allocation should consider NUMA or not.
6789c92151STejun Heo  *
6889c92151STejun Heo  * RETURNS:
6989c92151STejun Heo  * true if NUMA should be considered; otherwise, false.
7089c92151STejun Heo  */
7189c92151STejun Heo static bool __init pcpu_need_numa(void)
7289c92151STejun Heo {
7389c92151STejun Heo #ifdef CONFIG_NEED_MULTIPLE_NODES
7489c92151STejun Heo 	pg_data_t *last = NULL;
7589c92151STejun Heo 	unsigned int cpu;
7689c92151STejun Heo 
7789c92151STejun Heo 	for_each_possible_cpu(cpu) {
7889c92151STejun Heo 		int node = early_cpu_to_node(cpu);
7989c92151STejun Heo 
8089c92151STejun Heo 		if (node_online(node) && NODE_DATA(node) &&
8189c92151STejun Heo 		    last && last != NODE_DATA(node))
8289c92151STejun Heo 			return true;
8389c92151STejun Heo 
8489c92151STejun Heo 		last = NODE_DATA(node);
8589c92151STejun Heo 	}
8689c92151STejun Heo #endif
8789c92151STejun Heo 	return false;
8889c92151STejun Heo }
894518e6a0STejun Heo #endif
9089c92151STejun Heo 
9189c92151STejun Heo /**
925f5d8405STejun Heo  * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
935f5d8405STejun Heo  * @cpu: cpu to allocate for
945f5d8405STejun Heo  * @size: size allocation in bytes
955f5d8405STejun Heo  * @align: alignment
965f5d8405STejun Heo  *
975f5d8405STejun Heo  * Allocate @size bytes aligned at @align for cpu @cpu.  This wrapper
985f5d8405STejun Heo  * does the right thing for NUMA regardless of the current
995f5d8405STejun Heo  * configuration.
1005f5d8405STejun Heo  *
1015f5d8405STejun Heo  * RETURNS:
1025f5d8405STejun Heo  * Pointer to the allocated area on success, NULL on failure.
1035f5d8405STejun Heo  */
1045f5d8405STejun Heo static void * __init pcpu_alloc_bootmem(unsigned int cpu, unsigned long size,
1055f5d8405STejun Heo 					unsigned long align)
1065f5d8405STejun Heo {
1075f5d8405STejun Heo 	const unsigned long goal = __pa(MAX_DMA_ADDRESS);
1085f5d8405STejun Heo #ifdef CONFIG_NEED_MULTIPLE_NODES
1095f5d8405STejun Heo 	int node = early_cpu_to_node(cpu);
1105f5d8405STejun Heo 	void *ptr;
1115f5d8405STejun Heo 
1125f5d8405STejun Heo 	if (!node_online(node) || !NODE_DATA(node)) {
1135f5d8405STejun Heo 		ptr = __alloc_bootmem_nopanic(size, align, goal);
1145f5d8405STejun Heo 		pr_info("cpu %d has no node %d or node-local memory\n",
1155f5d8405STejun Heo 			cpu, node);
1165f5d8405STejun Heo 		pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
1175f5d8405STejun Heo 			 cpu, size, __pa(ptr));
1185f5d8405STejun Heo 	} else {
1195f5d8405STejun Heo 		ptr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
1205f5d8405STejun Heo 						   size, align, goal);
12140685236SJoe Perches 		pr_debug("per cpu data for cpu%d %lu bytes on node%d at %016lx\n",
12240685236SJoe Perches 			 cpu, size, node, __pa(ptr));
1235f5d8405STejun Heo 	}
1245f5d8405STejun Heo 	return ptr;
1255f5d8405STejun Heo #else
1265f5d8405STejun Heo 	return __alloc_bootmem_nopanic(size, align, goal);
1275f5d8405STejun Heo #endif
1285f5d8405STejun Heo }
1295f5d8405STejun Heo 
1305f5d8405STejun Heo /*
131d4b95f80STejun Heo  * Helpers for first chunk memory allocation
132d4b95f80STejun Heo  */
1333cbc8565STejun Heo static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
134d4b95f80STejun Heo {
1353cbc8565STejun Heo 	return pcpu_alloc_bootmem(cpu, size, align);
136d4b95f80STejun Heo }
137d4b95f80STejun Heo 
138d4b95f80STejun Heo static void __init pcpu_fc_free(void *ptr, size_t size)
139d4b95f80STejun Heo {
140d4b95f80STejun Heo 	free_bootmem(__pa(ptr), size);
141d4b95f80STejun Heo }
142d4b95f80STejun Heo 
1434518e6a0STejun Heo static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
1444518e6a0STejun Heo {
1458ac83757STejun Heo #ifdef CONFIG_NEED_MULTIPLE_NODES
146a530b795STejun Heo 	if (early_cpu_to_node(from) == early_cpu_to_node(to))
147a530b795STejun Heo 		return LOCAL_DISTANCE;
148a530b795STejun Heo 	else
149a530b795STejun Heo 		return REMOTE_DISTANCE;
1508ac83757STejun Heo #else
1514518e6a0STejun Heo 	return LOCAL_DISTANCE;
1528ac83757STejun Heo #endif
15389c92151STejun Heo }
15489c92151STejun Heo 
15500ae4064STejun Heo static void __init pcpup_populate_pte(unsigned long addr)
156458a3e64STejun Heo {
157458a3e64STejun Heo 	populate_extra_pte(addr);
158458a3e64STejun Heo }
159458a3e64STejun Heo 
160b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu)
161b2d2f431SBrian Gerst {
162b2d2f431SBrian Gerst #ifdef CONFIG_X86_32
163b2d2f431SBrian Gerst 	struct desc_struct gdt;
164b2d2f431SBrian Gerst 
165b2d2f431SBrian Gerst 	pack_descriptor(&gdt, per_cpu_offset(cpu), 0xFFFFF,
166b2d2f431SBrian Gerst 			0x2 | DESCTYPE_S, 0x8);
167b2d2f431SBrian Gerst 	gdt.s = 1;
168b2d2f431SBrian Gerst 	write_gdt_entry(get_cpu_gdt_table(cpu),
169b2d2f431SBrian Gerst 			GDT_ENTRY_PERCPU, &gdt, DESCTYPE_S);
170b2d2f431SBrian Gerst #endif
171b2d2f431SBrian Gerst }
172b2d2f431SBrian Gerst 
173378b39a4SYinghai Lu void __init setup_per_cpu_areas(void)
174378b39a4SYinghai Lu {
1755f5d8405STejun Heo 	unsigned int cpu;
17611124411STejun Heo 	unsigned long delta;
177fb435d52STejun Heo 	int rc;
178a1681965SMike Travis 
179ab14398aSCyrill Gorcunov 	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
180a1681965SMike Travis 		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
181a1681965SMike Travis 
1828ac83757STejun Heo 	/*
1834518e6a0STejun Heo 	 * Allocate percpu area.  Embedding allocator is our favorite;
1844518e6a0STejun Heo 	 * however, on NUMA configurations, it can result in very
1854518e6a0STejun Heo 	 * sparse unit mapping and vmalloc area isn't spacious enough
1864518e6a0STejun Heo 	 * on 32bit.  Use page in that case.
1878ac83757STejun Heo 	 */
1884518e6a0STejun Heo #ifdef CONFIG_X86_32
1894518e6a0STejun Heo 	if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
1904518e6a0STejun Heo 		pcpu_chosen_fc = PCPU_FC_PAGE;
1914518e6a0STejun Heo #endif
192fb435d52STejun Heo 	rc = -EINVAL;
193f58dc01bSTejun Heo 	if (pcpu_chosen_fc != PCPU_FC_PAGE) {
1944518e6a0STejun Heo 		const size_t atom_size = cpu_has_pse ? PMD_SIZE : PAGE_SIZE;
1954518e6a0STejun Heo 		const size_t dyn_size = PERCPU_MODULE_RESERVE +
1964518e6a0STejun Heo 			PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
197f58dc01bSTejun Heo 
1984518e6a0STejun Heo 		rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
1994518e6a0STejun Heo 					    dyn_size, atom_size,
2004518e6a0STejun Heo 					    pcpu_cpu_distance,
2014518e6a0STejun Heo 					    pcpu_fc_alloc, pcpu_fc_free);
202fb435d52STejun Heo 		if (rc < 0)
20340685236SJoe Perches 			pr_warning("%s allocator failed (%d), falling back to page size\n",
204fb435d52STejun Heo 				   pcpu_fc_names[pcpu_chosen_fc], rc);
205fa8a7094STejun Heo 	}
206fb435d52STejun Heo 	if (rc < 0)
2074518e6a0STejun Heo 		rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
2084518e6a0STejun Heo 					   pcpu_fc_alloc, pcpu_fc_free,
2094518e6a0STejun Heo 					   pcpup_populate_pte);
210fb435d52STejun Heo 	if (rc < 0)
211fb435d52STejun Heo 		panic("cannot initialize percpu area (err=%d)", rc);
21211124411STejun Heo 
2135f5d8405STejun Heo 	/* alrighty, percpu areas up and running */
21411124411STejun Heo 	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
21511124411STejun Heo 	for_each_possible_cpu(cpu) {
216fb435d52STejun Heo 		per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
21726f80bd6SBrian Gerst 		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
218ea927906SBrian Gerst 		per_cpu(cpu_number, cpu) = cpu;
219b2d2f431SBrian Gerst 		setup_percpu_segment(cpu);
22060a5317fSTejun Heo 		setup_stack_canary_segment(cpu);
2210d77e7f0SBrian Gerst 		/*
222cf3997f5STejun Heo 		 * Copy data used in early init routines from the
223cf3997f5STejun Heo 		 * initial arrays to the per cpu data areas.  These
224cf3997f5STejun Heo 		 * arrays then become expendable and the *_early_ptr's
225cf3997f5STejun Heo 		 * are zeroed indicating that the static arrays are
226cf3997f5STejun Heo 		 * gone.
2270d77e7f0SBrian Gerst 		 */
228ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC
2290d77e7f0SBrian Gerst 		per_cpu(x86_cpu_to_apicid, cpu) =
2300d77e7f0SBrian Gerst 			early_per_cpu_map(x86_cpu_to_apicid, cpu);
2310d77e7f0SBrian Gerst 		per_cpu(x86_bios_cpu_apicid, cpu) =
2320d77e7f0SBrian Gerst 			early_per_cpu_map(x86_bios_cpu_apicid, cpu);
233ec70de8bSBrian Gerst #endif
2341a51e3a0STejun Heo #ifdef CONFIG_X86_64
23526f80bd6SBrian Gerst 		per_cpu(irq_stack_ptr, cpu) =
236cf3997f5STejun Heo 			per_cpu(irq_stack_union.irq_stack, cpu) +
237cf3997f5STejun Heo 			IRQ_STACK_SIZE - 64;
2386470aff6SBrian Gerst #ifdef CONFIG_NUMA
2396470aff6SBrian Gerst 		per_cpu(x86_cpu_to_node_map, cpu) =
2406470aff6SBrian Gerst 			early_per_cpu_map(x86_cpu_to_node_map, cpu);
2416470aff6SBrian Gerst #endif
2422697fbd5SBrian Gerst #endif
2431a51e3a0STejun Heo 		/*
244*c273fb3bSDenys Vlasenko 		 * Up to this point, the boot CPU has been using .init.data
2452697fbd5SBrian Gerst 		 * area.  Reload any changed state for the boot CPU.
2461a51e3a0STejun Heo 		 */
24734019be1SBrian Gerst 		if (cpu == boot_cpu_id)
248552be871SBrian Gerst 			switch_to_new_gdt(cpu);
249378b39a4SYinghai Lu 	}
250378b39a4SYinghai Lu 
2510d77e7f0SBrian Gerst 	/* indicate the early static arrays will soon be gone */
25222f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC
2530d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
2540d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
25522f25138SJames Bottomley #endif
2566470aff6SBrian Gerst #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
2570d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
2580d77e7f0SBrian Gerst #endif
259378b39a4SYinghai Lu 
26035d5a9a6SYinghai Lu #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
26135d5a9a6SYinghai Lu 	/*
26235d5a9a6SYinghai Lu 	 * make sure boot cpu node_number is right, when boot cpu is on the
26335d5a9a6SYinghai Lu 	 * node that doesn't have mem installed
26435d5a9a6SYinghai Lu 	 */
26535d5a9a6SYinghai Lu 	per_cpu(node_number, boot_cpu_id) = cpu_to_node(boot_cpu_id);
26635d5a9a6SYinghai Lu #endif
26735d5a9a6SYinghai Lu 
268378b39a4SYinghai Lu 	/* Setup node to cpumask map */
269378b39a4SYinghai Lu 	setup_node_to_cpumask_map();
270c2d1cec1SMike Travis 
271c2d1cec1SMike Travis 	/* Setup cpu initialized, callin, callout masks */
272c2d1cec1SMike Travis 	setup_cpu_local_masks();
273378b39a4SYinghai Lu }
274