xref: /linux-6.15/arch/x86/kernel/setup_percpu.c (revision 06aa0305)
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 
2601c7bc51SBrian Gerst DEFINE_PER_CPU_CACHE_HOT(int, cpu_number);
2701c7bc51SBrian Gerst EXPORT_PER_CPU_SYMBOL(cpu_number);
2801c7bc51SBrian Gerst 
29*06aa0305SBrian Gerst DEFINE_PER_CPU_CACHE_HOT(unsigned long, this_cpu_off);
301688401aSBrian Gerst EXPORT_PER_CPU_SYMBOL(this_cpu_off);
311688401aSBrian Gerst 
329d7de2aaSBrian Gerst unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init;
339939ddafSTejun Heo EXPORT_SYMBOL(__per_cpu_offset);
34378b39a4SYinghai Lu 
356b19b0c2STejun Heo /*
366b19b0c2STejun Heo  * On x86_64 symbols referenced from code should be reachable using
376b19b0c2STejun Heo  * 32bit relocations.  Reserve space for static percpu variables in
386b19b0c2STejun Heo  * modules so that they are always served from the first chunk which
396b19b0c2STejun Heo  * is located at the percpu segment base.  On x86_32, anything can
406b19b0c2STejun Heo  * address anywhere.  No need to reserve space in the first chunk.
416b19b0c2STejun Heo  */
426b19b0c2STejun Heo #ifdef CONFIG_X86_64
436b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE	PERCPU_MODULE_RESERVE
446b19b0c2STejun Heo #else
456b19b0c2STejun Heo #define PERCPU_FIRST_CHUNK_RESERVE	0
466b19b0c2STejun Heo #endif
476b19b0c2STejun Heo 
484518e6a0STejun Heo #ifdef CONFIG_X86_32
495f5d8405STejun Heo /**
5089c92151STejun Heo  * pcpu_need_numa - determine percpu allocation needs to consider NUMA
5189c92151STejun Heo  *
5289c92151STejun Heo  * If NUMA is not configured or there is only one NUMA node available,
5389c92151STejun Heo  * there is no reason to consider NUMA.  This function determines
5489c92151STejun Heo  * whether percpu allocation should consider NUMA or not.
5589c92151STejun Heo  *
5689c92151STejun Heo  * RETURNS:
5789c92151STejun Heo  * true if NUMA should be considered; otherwise, false.
5889c92151STejun Heo  */
pcpu_need_numa(void)5989c92151STejun Heo static bool __init pcpu_need_numa(void)
6089c92151STejun Heo {
61a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
6289c92151STejun Heo 	pg_data_t *last = NULL;
6389c92151STejun Heo 	unsigned int cpu;
6489c92151STejun Heo 
6589c92151STejun Heo 	for_each_possible_cpu(cpu) {
6689c92151STejun Heo 		int node = early_cpu_to_node(cpu);
6789c92151STejun Heo 
6889c92151STejun Heo 		if (node_online(node) && NODE_DATA(node) &&
6989c92151STejun Heo 		    last && last != NODE_DATA(node))
7089c92151STejun Heo 			return true;
7189c92151STejun Heo 
7289c92151STejun Heo 		last = NODE_DATA(node);
7389c92151STejun Heo 	}
7489c92151STejun Heo #endif
7589c92151STejun Heo 	return false;
7689c92151STejun Heo }
774518e6a0STejun Heo #endif
7889c92151STejun Heo 
pcpu_cpu_distance(unsigned int from,unsigned int to)794518e6a0STejun Heo static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
804518e6a0STejun Heo {
81a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
82a530b795STejun Heo 	if (early_cpu_to_node(from) == early_cpu_to_node(to))
83a530b795STejun Heo 		return LOCAL_DISTANCE;
84a530b795STejun Heo 	else
85a530b795STejun Heo 		return REMOTE_DISTANCE;
868ac83757STejun Heo #else
874518e6a0STejun Heo 	return LOCAL_DISTANCE;
888ac83757STejun Heo #endif
8989c92151STejun Heo }
9089c92151STejun Heo 
pcpu_cpu_to_node(int cpu)911ca3fb3aSKefeng Wang static int __init pcpu_cpu_to_node(int cpu)
921ca3fb3aSKefeng Wang {
931ca3fb3aSKefeng Wang 	return early_cpu_to_node(cpu);
941ca3fb3aSKefeng Wang }
951ca3fb3aSKefeng Wang 
pcpu_populate_pte(unsigned long addr)9620c03576SKefeng Wang void __init pcpu_populate_pte(unsigned long addr)
97458a3e64STejun Heo {
98458a3e64STejun Heo 	populate_extra_pte(addr);
99458a3e64STejun Heo }
100458a3e64STejun Heo 
setup_percpu_segment(int cpu)101b2d2f431SBrian Gerst static inline void setup_percpu_segment(int cpu)
102b2d2f431SBrian Gerst {
103b2d2f431SBrian Gerst #ifdef CONFIG_X86_32
104bc90aefaSVegard Nossum 	struct desc_struct d = GDT_ENTRY_INIT(DESC_DATA32,
1051445f6e1SVegard Nossum 					      per_cpu_offset(cpu), 0xFFFFF);
106b2d2f431SBrian Gerst 
1071dd439feSThomas Gleixner 	write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
108b2d2f431SBrian Gerst #endif
109b2d2f431SBrian Gerst }
110b2d2f431SBrian Gerst 
setup_per_cpu_areas(void)111378b39a4SYinghai Lu void __init setup_per_cpu_areas(void)
112378b39a4SYinghai Lu {
1135f5d8405STejun Heo 	unsigned int cpu;
11411124411STejun Heo 	unsigned long delta;
115fb435d52STejun Heo 	int rc;
116a1681965SMike Travis 
117b9726c26SAlexey Dobriyan 	pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
118a1681965SMike Travis 		NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
119a1681965SMike Travis 
1208ac83757STejun Heo 	/*
1214518e6a0STejun Heo 	 * Allocate percpu area.  Embedding allocator is our favorite;
1224518e6a0STejun Heo 	 * however, on NUMA configurations, it can result in very
1234518e6a0STejun Heo 	 * sparse unit mapping and vmalloc area isn't spacious enough
1244518e6a0STejun Heo 	 * on 32bit.  Use page in that case.
1258ac83757STejun Heo 	 */
1264518e6a0STejun Heo #ifdef CONFIG_X86_32
1274518e6a0STejun Heo 	if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
1284518e6a0STejun Heo 		pcpu_chosen_fc = PCPU_FC_PAGE;
1294518e6a0STejun Heo #endif
130fb435d52STejun Heo 	rc = -EINVAL;
131f58dc01bSTejun Heo 	if (pcpu_chosen_fc != PCPU_FC_PAGE) {
1324518e6a0STejun Heo 		const size_t dyn_size = PERCPU_MODULE_RESERVE +
1334518e6a0STejun Heo 			PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
134d5e28005STejun Heo 		size_t atom_size;
135f58dc01bSTejun Heo 
136d5e28005STejun Heo 		/*
137d5e28005STejun Heo 		 * On 64bit, use PMD_SIZE for atom_size so that embedded
138d5e28005STejun Heo 		 * percpu areas are aligned to PMD.  This, in the future,
139d5e28005STejun Heo 		 * can also allow using PMD mappings in vmalloc area.  Use
140d5e28005STejun Heo 		 * PAGE_SIZE on 32bit as vmalloc space is highly contended
141d5e28005STejun Heo 		 * and large vmalloc area allocs can easily fail.
142d5e28005STejun Heo 		 */
143d5e28005STejun Heo #ifdef CONFIG_X86_64
144d5e28005STejun Heo 		atom_size = PMD_SIZE;
145d5e28005STejun Heo #else
146d5e28005STejun Heo 		atom_size = PAGE_SIZE;
147d5e28005STejun Heo #endif
1484518e6a0STejun Heo 		rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
1494518e6a0STejun Heo 					    dyn_size, atom_size,
1504518e6a0STejun Heo 					    pcpu_cpu_distance,
15123f91716SKefeng Wang 					    pcpu_cpu_to_node);
152fb435d52STejun Heo 		if (rc < 0)
1538d3bcc44SKefeng Wang 			pr_warn("%s allocator failed (%d), falling back to page size\n",
154fb435d52STejun Heo 				pcpu_fc_names[pcpu_chosen_fc], rc);
155fa8a7094STejun Heo 	}
156fb435d52STejun Heo 	if (rc < 0)
1574518e6a0STejun Heo 		rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
15820c03576SKefeng Wang 					   pcpu_cpu_to_node);
159fb435d52STejun Heo 	if (rc < 0)
160fb435d52STejun Heo 		panic("cannot initialize percpu area (err=%d)", rc);
16111124411STejun Heo 
1625f5d8405STejun Heo 	/* alrighty, percpu areas up and running */
16311124411STejun Heo 	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
16411124411STejun Heo 	for_each_possible_cpu(cpu) {
165fb435d52STejun Heo 		per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
16626f80bd6SBrian Gerst 		per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
16701c7bc51SBrian Gerst 		per_cpu(cpu_number, cpu) = cpu;
168b2d2f431SBrian Gerst 		setup_percpu_segment(cpu);
1690d77e7f0SBrian Gerst 		/*
170cf3997f5STejun Heo 		 * Copy data used in early init routines from the
171cf3997f5STejun Heo 		 * initial arrays to the per cpu data areas.  These
172cf3997f5STejun Heo 		 * arrays then become expendable and the *_early_ptr's
173cf3997f5STejun Heo 		 * are zeroed indicating that the static arrays are
174cf3997f5STejun Heo 		 * gone.
1750d77e7f0SBrian Gerst 		 */
176ec70de8bSBrian Gerst #ifdef CONFIG_X86_LOCAL_APIC
1770d77e7f0SBrian Gerst 		per_cpu(x86_cpu_to_apicid, cpu) =
1780d77e7f0SBrian Gerst 			early_per_cpu_map(x86_cpu_to_apicid, cpu);
1793e9e57faSVitaly Kuznetsov 		per_cpu(x86_cpu_to_acpiid, cpu) =
1803e9e57faSVitaly Kuznetsov 			early_per_cpu_map(x86_cpu_to_acpiid, cpu);
181ec70de8bSBrian Gerst #endif
1826470aff6SBrian Gerst #ifdef CONFIG_NUMA
1836470aff6SBrian Gerst 		per_cpu(x86_cpu_to_node_map, cpu) =
1846470aff6SBrian Gerst 			early_per_cpu_map(x86_cpu_to_node_map, cpu);
1859aebbdb6SYinghai Lu 		/*
186a4ce96acSLinus Torvalds 		 * Ensure that the boot cpu numa_node is correct when the boot
1879aebbdb6SYinghai Lu 		 * cpu is on a node that doesn't have memory installed.
1889aebbdb6SYinghai Lu 		 * Also cpu_up() will call cpu_to_node() for APs when
1899aebbdb6SYinghai Lu 		 * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set
1909aebbdb6SYinghai Lu 		 * up later with c_init aka intel_init/amd_init.
1919aebbdb6SYinghai Lu 		 * So set them all (boot cpu and all APs).
1929aebbdb6SYinghai Lu 		 */
1939aebbdb6SYinghai Lu 		set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
1946470aff6SBrian Gerst #endif
1951a51e3a0STejun Heo 		/*
196c273fb3bSDenys Vlasenko 		 * Up to this point, the boot CPU has been using .init.data
1972697fbd5SBrian Gerst 		 * area.  Reload any changed state for the boot CPU.
1981a51e3a0STejun Heo 		 */
199f6e9456cSRobert Richter 		if (!cpu)
2001f19e2d5SThomas Gleixner 			switch_gdt_and_percpu_base(cpu);
201378b39a4SYinghai Lu 	}
202378b39a4SYinghai Lu 
2030d77e7f0SBrian Gerst 	/* indicate the early static arrays will soon be gone */
20422f25138SJames Bottomley #ifdef CONFIG_X86_LOCAL_APIC
2050d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
2063e9e57faSVitaly Kuznetsov 	early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
20722f25138SJames Bottomley #endif
208645a7919STejun Heo #ifdef CONFIG_NUMA
2090d77e7f0SBrian Gerst 	early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
2100d77e7f0SBrian Gerst #endif
211378b39a4SYinghai Lu 
212378b39a4SYinghai Lu 	/* Setup node to cpumask map */
213378b39a4SYinghai Lu 	setup_node_to_cpumask_map();
214c2d1cec1SMike Travis 
215c2d1cec1SMike Travis 	/* Setup cpu initialized, callin, callout masks */
216c2d1cec1SMike Travis 	setup_cpu_local_masks();
21723b2a4ddSAndy Lutomirski 
21823b2a4ddSAndy Lutomirski 	/*
219d2b6dc61SAndy Lutomirski 	 * Sync back kernel address range again.  We already did this in
220d2b6dc61SAndy Lutomirski 	 * setup_arch(), but percpu data also needs to be available in
2217f0a002bSJoerg Roedel 	 * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to
2227f0a002bSJoerg Roedel 	 * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available
2237f0a002bSJoerg Roedel 	 * there too.
224945fd17aSThomas Gleixner 	 *
225945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
226945fd17aSThomas Gleixner 	 * this call?
22723b2a4ddSAndy Lutomirski 	 */
228945fd17aSThomas Gleixner 	sync_initial_page_table();
229378b39a4SYinghai Lu }
230