Skip to content

Commit 6e29032

Browse files
committed
x86/cpu: Move cpu_l[l2]c_id into topology info
The topology IDs which identify the LLC and L2 domains clearly belong to the per CPU topology information. Move them into cpuinfo_x86::cpuinfo_topo and get rid of the extra per CPU data and the related exports. This also paves the way to do proper topology evaluation during early boot because it removes the only per CPU dependency for that. No functional change. Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Juergen Gross <[email protected]> Tested-by: Sohil Mehta <[email protected]> Tested-by: Michael Kelley <[email protected]> Tested-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Zhang Rui <[email protected]> Reviewed-by: Arjan van de Ven <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 22dc963 commit 6e29032

File tree

13 files changed

+48
-67
lines changed

13 files changed

+48
-67
lines changed

Documentation/arch/x86/topology.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ Package-related topology information in the kernel:
7979
The maximum possible number of packages in the system. Helpful for per
8080
package facilities to preallocate per package information.
8181

82-
- cpu_llc_id:
83-
84-
A per-CPU variable containing:
82+
- cpuinfo_x86.topo.llc_id:
8583

8684
- On Intel, the first APIC ID of the list of CPUs sharing the Last Level
8785
Cache

arch/x86/events/amd/uncore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ static int amd_uncore_cpu_starting(unsigned int cpu)
537537

538538
if (amd_uncore_llc) {
539539
uncore = *per_cpu_ptr(amd_uncore_llc, cpu);
540-
uncore->id = get_llc_id(cpu);
540+
uncore->id = per_cpu_llc_id(cpu);
541541

542542
uncore = amd_uncore_find_online_sibling(uncore, amd_uncore_llc);
543543
*per_cpu_ptr(amd_uncore_llc, cpu) = uncore;

arch/x86/include/asm/cacheinfo.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ extern unsigned int memory_caching_control;
77
#define CACHE_MTRR 0x01
88
#define CACHE_PAT 0x02
99

10-
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu);
11-
void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu);
12-
1310
void cache_disable(void);
1411
void cache_enable(void);
1512
void set_cache_aps_delayed_init(bool val);

arch/x86/include/asm/processor.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ struct cpuinfo_topology {
9999
// Logical ID mappings
100100
u32 logical_pkg_id;
101101
u32 logical_die_id;
102+
103+
// Cache level topology IDs
104+
u32 llc_id;
105+
u32 l2c_id;
102106
};
103107

104108
struct cpuinfo_x86 {
@@ -689,7 +693,15 @@ extern int set_tsc_mode(unsigned int val);
689693

690694
DECLARE_PER_CPU(u64, msr_misc_features_shadow);
691695

692-
extern u16 get_llc_id(unsigned int cpu);
696+
static inline u16 per_cpu_llc_id(unsigned int cpu)
697+
{
698+
return per_cpu(cpu_info.topo.llc_id, cpu);
699+
}
700+
701+
static inline u16 per_cpu_l2c_id(unsigned int cpu)
702+
{
703+
return per_cpu(cpu_info.topo.l2c_id, cpu);
704+
}
693705

694706
#ifdef CONFIG_CPU_SUP_AMD
695707
extern u32 amd_get_nodes_per_socket(void);

arch/x86/include/asm/smp.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ DECLARE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_die_map);
1717
/* cpus sharing the last level cache: */
1818
DECLARE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
1919
DECLARE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_l2c_shared_map);
20-
DECLARE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id);
21-
DECLARE_PER_CPU_READ_MOSTLY(u16, cpu_l2c_id);
2220

2321
DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid);
2422
DECLARE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid);

arch/x86/include/asm/topology.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern const struct cpumask *cpu_clustergroup_mask(int cpu);
115115
extern unsigned int __max_die_per_package;
116116

117117
#ifdef CONFIG_SMP
118-
#define topology_cluster_id(cpu) (per_cpu(cpu_l2c_id, cpu))
118+
#define topology_cluster_id(cpu) (cpu_data(cpu).topo.l2c_id)
119119
#define topology_die_cpumask(cpu) (per_cpu(cpu_die_map, cpu))
120120
#define topology_cluster_cpumask(cpu) (cpu_clustergroup_mask(cpu))
121121
#define topology_core_cpumask(cpu) (per_cpu(cpu_core_map, cpu))

arch/x86/kernel/apic/apic_numachip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void fixup_cpu_id(struct cpuinfo_x86 *c, int node)
161161
u64 val;
162162
u32 nodes = 1;
163163

164-
this_cpu_write(cpu_llc_id, node);
164+
c->topo.llc_id = node;
165165

166166
/* Account for nodes per socket in multi-core-module processors */
167167
if (boot_cpu_has(X86_FEATURE_NODEID_MSR)) {

arch/x86/kernel/cpu/amd.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ static void legacy_fixup_core_id(struct cpuinfo_x86 *c)
401401
*/
402402
static void amd_get_topology(struct cpuinfo_x86 *c)
403403
{
404-
int cpu = smp_processor_id();
405-
406404
/* get information required for multi-node processors */
407405
if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
408406
int err;
@@ -430,15 +428,14 @@ static void amd_get_topology(struct cpuinfo_x86 *c)
430428
if (!err)
431429
c->x86_coreid_bits = get_count_order(c->x86_max_cores);
432430

433-
cacheinfo_amd_init_llc_id(c, cpu);
431+
cacheinfo_amd_init_llc_id(c);
434432

435433
} else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
436434
u64 value;
437435

438436
rdmsrl(MSR_FAM10H_NODE_ID, value);
439437
c->topo.die_id = value & 7;
440-
441-
per_cpu(cpu_llc_id, cpu) = c->topo.die_id;
438+
c->topo.llc_id = c->topo.die_id;
442439
} else
443440
return;
444441

@@ -455,15 +452,14 @@ static void amd_get_topology(struct cpuinfo_x86 *c)
455452
static void amd_detect_cmp(struct cpuinfo_x86 *c)
456453
{
457454
unsigned bits;
458-
int cpu = smp_processor_id();
459455

460456
bits = c->x86_coreid_bits;
461457
/* Low order bits define the core id (index of core in socket) */
462458
c->topo.core_id = c->topo.initial_apicid & ((1 << bits)-1);
463459
/* Convert the initial APIC ID into the socket ID */
464460
c->topo.pkg_id = c->topo.initial_apicid >> bits;
465461
/* use socket ID also for last level cache */
466-
per_cpu(cpu_llc_id, cpu) = c->topo.die_id = c->topo.pkg_id;
462+
c->topo.llc_id = c->topo.die_id = c->topo.pkg_id;
467463
}
468464

469465
u32 amd_get_nodes_per_socket(void)
@@ -481,7 +477,7 @@ static void srat_detect_node(struct cpuinfo_x86 *c)
481477

482478
node = numa_cpu_node(cpu);
483479
if (node == NUMA_NO_NODE)
484-
node = get_llc_id(cpu);
480+
node = per_cpu_llc_id(cpu);
485481

486482
/*
487483
* On multi-fabric platform (e.g. Numascale NumaChip) a

arch/x86/kernel/cpu/cacheinfo.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *c)
661661
return i;
662662
}
663663

664-
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu)
664+
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c)
665665
{
666666
/*
667667
* We may have multiple LLCs if L3 caches exist, so check if we
@@ -672,13 +672,13 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu)
672672

673673
if (c->x86 < 0x17) {
674674
/* LLC is at the node level. */
675-
per_cpu(cpu_llc_id, cpu) = c->topo.die_id;
675+
c->topo.llc_id = c->topo.die_id;
676676
} else if (c->x86 == 0x17 && c->x86_model <= 0x1F) {
677677
/*
678678
* LLC is at the core complex level.
679679
* Core complex ID is ApicId[3] for these processors.
680680
*/
681-
per_cpu(cpu_llc_id, cpu) = c->topo.apicid >> 3;
681+
c->topo.llc_id = c->topo.apicid >> 3;
682682
} else {
683683
/*
684684
* LLC ID is calculated from the number of threads sharing the
@@ -694,12 +694,12 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu)
694694
if (num_sharing_cache) {
695695
int bits = get_count_order(num_sharing_cache);
696696

697-
per_cpu(cpu_llc_id, cpu) = c->topo.apicid >> bits;
697+
c->topo.llc_id = c->topo.apicid >> bits;
698698
}
699699
}
700700
}
701701

702-
void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu)
702+
void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c)
703703
{
704704
/*
705705
* We may have multiple LLCs if L3 caches exist, so check if we
@@ -712,7 +712,7 @@ void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c, int cpu)
712712
* LLC is at the core complex level.
713713
* Core complex ID is ApicId[3] for these processors.
714714
*/
715-
per_cpu(cpu_llc_id, cpu) = c->topo.apicid >> 3;
715+
c->topo.llc_id = c->topo.apicid >> 3;
716716
}
717717

718718
void init_amd_cacheinfo(struct cpuinfo_x86 *c)
@@ -740,9 +740,6 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)
740740
unsigned int new_l1d = 0, new_l1i = 0; /* Cache sizes from cpuid(4) */
741741
unsigned int new_l2 = 0, new_l3 = 0, i; /* Cache sizes from cpuid(4) */
742742
unsigned int l2_id = 0, l3_id = 0, num_threads_sharing, index_msb;
743-
#ifdef CONFIG_SMP
744-
unsigned int cpu = c->cpu_index;
745-
#endif
746743

747744
if (c->cpuid_level > 3) {
748745
static int is_initialized;
@@ -856,30 +853,24 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)
856853

857854
if (new_l2) {
858855
l2 = new_l2;
859-
#ifdef CONFIG_SMP
860-
per_cpu(cpu_llc_id, cpu) = l2_id;
861-
per_cpu(cpu_l2c_id, cpu) = l2_id;
862-
#endif
856+
c->topo.llc_id = l2_id;
857+
c->topo.l2c_id = l2_id;
863858
}
864859

865860
if (new_l3) {
866861
l3 = new_l3;
867-
#ifdef CONFIG_SMP
868-
per_cpu(cpu_llc_id, cpu) = l3_id;
869-
#endif
862+
c->topo.llc_id = l3_id;
870863
}
871864

872-
#ifdef CONFIG_SMP
873865
/*
874-
* If cpu_llc_id is not yet set, this means cpuid_level < 4 which in
866+
* If llc_id is not yet set, this means cpuid_level < 4 which in
875867
* turns means that the only possibility is SMT (as indicated in
876868
* cpuid1). Since cpuid2 doesn't specify shared caches, and we know
877869
* that SMT shares all caches, we can unconditionally set cpu_llc_id to
878870
* c->topo.pkg_id.
879871
*/
880-
if (per_cpu(cpu_llc_id, cpu) == BAD_APICID)
881-
per_cpu(cpu_llc_id, cpu) = c->topo.pkg_id;
882-
#endif
872+
if (c->topo.llc_id == BAD_APICID)
873+
c->topo.llc_id = c->topo.pkg_id;
883874

884875
c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d));
885876

arch/x86/kernel/cpu/common.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ u32 elf_hwcap2 __read_mostly;
7474
int smp_num_siblings = 1;
7575
EXPORT_SYMBOL(smp_num_siblings);
7676

77-
/* Last level cache ID of each logical CPU */
78-
DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
79-
80-
u16 get_llc_id(unsigned int cpu)
81-
{
82-
return per_cpu(cpu_llc_id, cpu);
83-
}
84-
EXPORT_SYMBOL_GPL(get_llc_id);
85-
86-
/* L2 cache ID of each logical CPU */
87-
DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_l2c_id) = BAD_APICID;
88-
8977
static struct ppin_info {
9078
int feature;
9179
int msr_ppin_ctl;
@@ -1830,6 +1818,8 @@ static void identify_cpu(struct cpuinfo_x86 *c)
18301818
c->x86_max_cores = 1;
18311819
c->x86_coreid_bits = 0;
18321820
c->topo.cu_id = 0xff;
1821+
c->topo.llc_id = BAD_APICID;
1822+
c->topo.l2c_id = BAD_APICID;
18331823
#ifdef CONFIG_X86_64
18341824
c->x86_clflush_size = 64;
18351825
c->x86_phys_bits = 36;

arch/x86/kernel/cpu/cpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ extern int detect_ht_early(struct cpuinfo_x86 *c);
7878
extern void detect_ht(struct cpuinfo_x86 *c);
7979
extern void check_null_seg_clears_base(struct cpuinfo_x86 *c);
8080

81+
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c);
82+
void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c);
83+
8184
unsigned int aperfmperf_get_khz(int cpu);
8285
void cpu_select_mitigations(void);
8386

arch/x86/kernel/cpu/hygon.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ static void hygon_get_topology_early(struct cpuinfo_x86 *c)
6363
*/
6464
static void hygon_get_topology(struct cpuinfo_x86 *c)
6565
{
66-
int cpu = smp_processor_id();
67-
6866
/* get information required for multi-node processors */
6967
if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
7068
int err;
@@ -94,14 +92,13 @@ static void hygon_get_topology(struct cpuinfo_x86 *c)
9492
if (!boot_cpu_has(X86_FEATURE_HYPERVISOR) && c->x86_model <= 0x3)
9593
c->topo.pkg_id = c->topo.apicid >> APICID_SOCKET_ID_BIT;
9694

97-
cacheinfo_hygon_init_llc_id(c, cpu);
95+
cacheinfo_hygon_init_llc_id(c);
9896
} else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
9997
u64 value;
10098

10199
rdmsrl(MSR_FAM10H_NODE_ID, value);
102100
c->topo.die_id = value & 7;
103-
104-
per_cpu(cpu_llc_id, cpu) = c->topo.die_id;
101+
c->topo.llc_id = c->topo.die_id;
105102
} else
106103
return;
107104

@@ -116,15 +113,14 @@ static void hygon_get_topology(struct cpuinfo_x86 *c)
116113
static void hygon_detect_cmp(struct cpuinfo_x86 *c)
117114
{
118115
unsigned int bits;
119-
int cpu = smp_processor_id();
120116

121117
bits = c->x86_coreid_bits;
122118
/* Low order bits define the core id (index of core in socket) */
123119
c->topo.core_id = c->topo.initial_apicid & ((1 << bits)-1);
124120
/* Convert the initial APIC ID into the socket ID */
125121
c->topo.pkg_id = c->topo.initial_apicid >> bits;
126-
/* use socket ID also for last level cache */
127-
per_cpu(cpu_llc_id, cpu) = c->topo.die_id = c->topo.pkg_id;
122+
/* Use package ID also for last level cache */
123+
c->topo.llc_id = c->topo.die_id = c->topo.pkg_id;
128124
}
129125

130126
static void srat_detect_node(struct cpuinfo_x86 *c)
@@ -136,7 +132,7 @@ static void srat_detect_node(struct cpuinfo_x86 *c)
136132

137133
node = numa_cpu_node(cpu);
138134
if (node == NUMA_NO_NODE)
139-
node = per_cpu(cpu_llc_id, cpu);
135+
node = c->topo.llc_id;
140136

141137
/*
142138
* On multi-fabric platform (e.g. Numascale NumaChip) a

arch/x86/kernel/smpboot.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
478478

479479
if (c->topo.pkg_id == o->topo.pkg_id &&
480480
c->topo.die_id == o->topo.die_id &&
481-
per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
481+
per_cpu_llc_id(cpu1) == per_cpu_llc_id(cpu2)) {
482482
if (c->topo.core_id == o->topo.core_id)
483483
return topology_sane(c, o, "smt");
484484

@@ -510,11 +510,11 @@ static bool match_l2c(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
510510
int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
511511

512512
/* If the arch didn't set up l2c_id, fall back to SMT */
513-
if (per_cpu(cpu_l2c_id, cpu1) == BAD_APICID)
513+
if (per_cpu_l2c_id(cpu1) == BAD_APICID)
514514
return match_smt(c, o);
515515

516516
/* Do not match if L2 cache id does not match: */
517-
if (per_cpu(cpu_l2c_id, cpu1) != per_cpu(cpu_l2c_id, cpu2))
517+
if (per_cpu_l2c_id(cpu1) != per_cpu_l2c_id(cpu2))
518518
return false;
519519

520520
return topology_sane(c, o, "l2c");
@@ -560,11 +560,11 @@ static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
560560
bool intel_snc = id && id->driver_data;
561561

562562
/* Do not match if we do not have a valid APICID for cpu: */
563-
if (per_cpu(cpu_llc_id, cpu1) == BAD_APICID)
563+
if (per_cpu_llc_id(cpu1) == BAD_APICID)
564564
return false;
565565

566566
/* Do not match if LLC id does not match: */
567-
if (per_cpu(cpu_llc_id, cpu1) != per_cpu(cpu_llc_id, cpu2))
567+
if (per_cpu_llc_id(cpu1) != per_cpu_llc_id(cpu2))
568568
return false;
569569

570570
/*

0 commit comments

Comments
 (0)