Skip to content

Commit ea2dd8a

Browse files
committed
x86/cpu/topology: Assign hotpluggable CPUIDs during init
There is no point in assigning the CPU numbers during ACPI physical hotplug. The number of possible hotplug CPUs is known when the possible map is initialized, so the CPU numbers can be associated to the registered non-present APIC IDs right there. This allows to put more code into the __init section and makes the related data __ro_after_init. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Michael Kelley <mhklinux@outlook.com> Tested-by: Sohil Mehta <sohil.mehta@intel.com> Link: https://lore.kernel.org/r/20240213210252.517339971@linutronix.de
1 parent 7cdcdab commit ea2dd8a

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

arch/x86/kernel/cpu/topology.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
4545
DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
4646

4747
/* Used for CPU number allocation and parallel CPU bringup */
48-
u32 cpuid_to_apicid[] __read_mostly = { [0 ... NR_CPUS - 1] = BAD_APICID, };
48+
u32 cpuid_to_apicid[] __ro_after_init = { [0 ... NR_CPUS - 1] = BAD_APICID, };
4949

5050
/* Bitmaps to mark registered APICs at each topology domain */
5151
static struct { DECLARE_BITMAP(map, MAX_LOCAL_APIC); } apic_maps[TOPO_MAX_DOMAIN] __ro_after_init;
@@ -60,7 +60,7 @@ static struct {
6060
unsigned int nr_rejected_cpus;
6161
u32 boot_cpu_apic_id;
6262
u32 real_bsp_apic_id;
63-
} topo_info __read_mostly = {
63+
} topo_info __ro_after_init = {
6464
.nr_assigned_cpus = 1,
6565
.boot_cpu_apic_id = BAD_APICID,
6666
.real_bsp_apic_id = BAD_APICID,
@@ -133,7 +133,7 @@ static int topo_lookup_cpuid(u32 apic_id)
133133
return -ENODEV;
134134
}
135135

136-
static int topo_get_cpunr(u32 apic_id)
136+
static __init int topo_get_cpunr(u32 apic_id)
137137
{
138138
int cpu = topo_lookup_cpuid(apic_id);
139139

@@ -149,8 +149,6 @@ static void topo_set_cpuids(unsigned int cpu, u32 apic_id, u32 acpi_id)
149149
early_per_cpu(x86_cpu_to_apicid, cpu) = apic_id;
150150
early_per_cpu(x86_cpu_to_acpiid, cpu) = acpi_id;
151151
#endif
152-
cpuid_to_apicid[cpu] = apic_id;
153-
154152
set_cpu_possible(cpu, true);
155153
set_cpu_present(cpu, true);
156154

@@ -206,6 +204,8 @@ static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
206204
cpu = 0;
207205
else
208206
cpu = topo_get_cpunr(apic_id);
207+
208+
cpuid_to_apicid[cpu] = apic_id;
209209
topo_set_cpuids(cpu, apic_id, acpi_id);
210210
} else {
211211
topo_info.nr_disabled_cpus++;
@@ -277,12 +277,9 @@ int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
277277
return -ENODEV;
278278

279279
cpu = topo_lookup_cpuid(apic_id);
280-
if (cpu < 0) {
281-
if (topo_info.nr_assigned_cpus >= nr_cpu_ids)
282-
return -ENOSPC;
280+
if (cpu < 0)
281+
return -ENOSPC;
283282

284-
cpu = topo_assign_cpunr(apic_id);
285-
}
286283
set_bit(apic_id, phys_cpu_present_map);
287284
topo_set_cpuids(cpu, apic_id, acpi_id);
288285
return cpu;
@@ -353,6 +350,7 @@ void __init topology_init_possible_cpus(void)
353350
unsigned int disabled = topo_info.nr_disabled_cpus;
354351
unsigned int total = assigned + disabled;
355352
unsigned int cpu, allowed = 1;
353+
u32 apicid;
356354

357355
if (!restrict_to_up()) {
358356
if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
@@ -381,8 +379,17 @@ void __init topology_init_possible_cpus(void)
381379
init_cpu_present(cpumask_of(0));
382380
init_cpu_possible(cpumask_of(0));
383381

382+
/* Assign CPU numbers to non-present CPUs */
383+
for (apicid = 0; disabled; disabled--, apicid++) {
384+
apicid = find_next_andnot_bit(apic_maps[TOPO_SMT_DOMAIN].map, phys_cpu_present_map,
385+
MAX_LOCAL_APIC, apicid);
386+
if (apicid >= MAX_LOCAL_APIC)
387+
break;
388+
cpuid_to_apicid[topo_info.nr_assigned_cpus++] = apicid;
389+
}
390+
384391
for (cpu = 0; cpu < allowed; cpu++) {
385-
u32 apicid = cpuid_to_apicid[cpu];
392+
apicid = cpuid_to_apicid[cpu];
386393

387394
set_cpu_possible(cpu, true);
388395

0 commit comments

Comments
 (0)