Skip to content

Commit 7253046

Browse files
committed
x86/cpu/topology: Use a data structure for topology info
Put the processor accounting into a data structure, which will gain more topology related information in the next steps, and sanitize the accounting. Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Michael Kelley <[email protected]> Tested-by: Sohil Mehta <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4c4c6f3 commit 7253046

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

arch/x86/kernel/cpu/topology.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,25 @@ DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
2222
/* Used for CPU number allocation and parallel CPU bringup */
2323
u32 cpuid_to_apicid[] __read_mostly = { [0 ... NR_CPUS - 1] = BAD_APICID, };
2424

25+
/*
26+
* Keep track of assigned, disabled and rejected CPUs. Present assigned
27+
* with 1 as CPU #0 is reserved for the boot CPU.
28+
*/
29+
static struct {
30+
unsigned int nr_assigned_cpus;
31+
unsigned int nr_disabled_cpus;
32+
unsigned int nr_rejected_cpus;
33+
} topo_info __read_mostly = {
34+
.nr_assigned_cpus = 1,
35+
};
36+
2537
/*
2638
* Processor to be disabled specified by kernel parameter
2739
* disable_cpu_apicid=<int>, mostly used for the kdump 2nd kernel to
2840
* avoid undefined behaviour caused by sending INIT from AP to BSP.
2941
*/
3042
static u32 disabled_cpu_apicid __ro_after_init = BAD_APICID;
3143

32-
static unsigned int num_processors;
33-
static unsigned int disabled_cpus;
34-
35-
/*
36-
* The number of allocated logical CPU IDs. Since logical CPU IDs are allocated
37-
* contiguously, it equals to current allocated max logical CPU ID plus 1.
38-
* All allocated CPU IDs should be in the [0, nr_logical_cpuids) range,
39-
* so the maximum of nr_logical_cpuids is nr_cpu_ids.
40-
*
41-
* NOTE: Reserve 0 for BSP.
42-
*/
43-
static int nr_logical_cpuids = 1;
44-
4544
bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
4645
{
4746
return phys_id == (u64)cpuid_to_apicid[cpu];
@@ -75,7 +74,7 @@ static int __init smp_init_primary_thread_mask(void)
7574
return 0;
7675
}
7776

78-
for (cpu = 0; cpu < nr_logical_cpuids; cpu++)
77+
for (cpu = 0; cpu < topo_info.nr_assigned_cpus; cpu++)
7978
cpu_mark_primary_thread(cpu, cpuid_to_apicid[cpu]);
8079
return 0;
8180
}
@@ -89,7 +88,7 @@ static int topo_lookup_cpuid(u32 apic_id)
8988
int i;
9089

9190
/* CPU# to APICID mapping is persistent once it is established */
92-
for (i = 0; i < nr_logical_cpuids; i++) {
91+
for (i = 0; i < topo_info.nr_assigned_cpus; i++) {
9392
if (cpuid_to_apicid[i] == apic_id)
9493
return i;
9594
}
@@ -107,22 +106,21 @@ static int allocate_logical_cpuid(u32 apic_id)
107106
if (cpu >= 0)
108107
return cpu;
109108

110-
cpuid_to_apicid[nr_logical_cpuids] = apic_id;
111-
return nr_logical_cpuids++;
109+
return topo_info.nr_assigned_cpus++;
112110
}
113111

114-
static void cpu_update_apic(int cpu, u32 apicid)
112+
static void cpu_update_apic(unsigned int cpu, u32 apic_id)
115113
{
116114
#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
117-
early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
115+
early_per_cpu(x86_cpu_to_apicid, cpu) = apic_id;
118116
#endif
117+
cpuid_to_apicid[cpu] = apic_id;
119118
set_cpu_possible(cpu, true);
120-
set_bit(apicid, phys_cpu_present_map);
119+
set_bit(apic_id, phys_cpu_present_map);
121120
set_cpu_present(cpu, true);
122-
num_processors++;
123121

124122
if (system_state != SYSTEM_BOOTING)
125-
cpu_mark_primary_thread(cpu, apicid);
123+
cpu_mark_primary_thread(cpu, apic_id);
126124
}
127125

128126
static int generic_processor_info(int apicid)
@@ -137,18 +135,18 @@ static int generic_processor_info(int apicid)
137135
return 0;
138136

139137
if (disabled_cpu_apicid == apicid) {
140-
int thiscpu = num_processors + disabled_cpus;
138+
int thiscpu = topo_info.nr_assigned_cpus + topo_info.nr_disabled_cpus;
141139

142140
pr_warn("APIC: Disabling requested cpu. Processor %d/0x%x ignored.\n",
143141
thiscpu, apicid);
144142

145-
disabled_cpus++;
143+
topo_info.nr_rejected_cpus++;
146144
return -ENODEV;
147145
}
148146

149-
if (num_processors >= nr_cpu_ids) {
147+
if (topo_info.nr_assigned_cpus >= nr_cpu_ids) {
150148
pr_warn_once("APIC: CPU limit of %d reached. Ignoring further CPUs\n", nr_cpu_ids);
151-
disabled_cpus++;
149+
topo_info.nr_rejected_cpus++;
152150
return -ENOSPC;
153151
}
154152

@@ -178,14 +176,16 @@ static int __initdata setup_possible_cpus = -1;
178176
*/
179177
__init void prefill_possible_map(void)
180178
{
179+
unsigned int num_processors = topo_info.nr_assigned_cpus;
180+
unsigned int disabled_cpus = topo_info.nr_disabled_cpus;
181181
int i, possible;
182182

183183
i = setup_max_cpus ?: 1;
184184
if (setup_possible_cpus == -1) {
185-
possible = num_processors;
185+
possible = topo_info.nr_assigned_cpus;
186186
#ifdef CONFIG_HOTPLUG_CPU
187187
if (setup_max_cpus)
188-
possible += disabled_cpus;
188+
possible += num_processors;
189189
#else
190190
if (possible > i)
191191
possible = i;
@@ -238,7 +238,7 @@ void __init topology_register_apic(u32 apic_id, u32 acpi_id, bool present)
238238
}
239239

240240
if (!present) {
241-
disabled_cpus++;
241+
topo_info.nr_disabled_cpus++;
242242
return;
243243
}
244244

@@ -295,7 +295,6 @@ void topology_hotunplug_apic(unsigned int cpu)
295295
per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
296296
clear_bit(apic_id, phys_cpu_present_map);
297297
set_cpu_present(cpu, false);
298-
num_processors--;
299298
}
300299
#endif
301300

0 commit comments

Comments
 (0)