[PATCH v2 2/2] x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup()
From: Ricardo Neri
Date: Wed Dec 02 2026 - 23:58:35 EST
__cache_cpumap_setup() open-codes a cache ID comparison: it right-shifts
the APIC IDs of two CPUs by N and compares the results. N is the
smallest integer such that 2^N is greater than or equal to the maximum
number of addressable IDs for logical processors sharing the cache of
the CPU being set up. calc_cache_topo_id() already computes the cache
IDs exposed in sysfs on hybrid processors.
Use calc_cache_topo_id() for that comparison as well, so that the file
no longer open-codes a cache ID computation next to its two helpers.
No functional change intended: both sides use the N of the CPU being set
up. With the same N, masking and shifting select the same CPUs. A different
N on other CPUs does not matter. The CPUs sharing a cache with the CPU
being set up are those whose APIC IDs match its APIC ID in all but the N
least-significant bits.
Signed-off-by: Ricardo Neri <ricardo.neri-calderon@xxxxxxxxxxxxxxx>
---
Changes in v2:
- Introduce this patch.
---
arch/x86/kernel/cpu/cacheinfo.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
index 4d0f47e5f6c0..5e830ee44d13 100644
--- a/arch/x86/kernel/cpu/cacheinfo.c
+++ b/arch/x86/kernel/cpu/cacheinfo.c
@@ -551,7 +551,8 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,
struct cpuinfo_x86 *c = &cpu_data(cpu);
struct cacheinfo *ci, *sibling_ci;
unsigned long num_threads_sharing;
- int index_msb, i;
+ unsigned int cache_id;
+ int i;
if (c->x86_vendor == X86_VENDOR_AMD || c->x86_vendor == X86_VENDOR_HYGON) {
if (__cache_amd_cpumap_setup(cpu, index, id4))
@@ -565,10 +566,15 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,
if (num_threads_sharing == 1)
return;
- index_msb = get_count_order(num_threads_sharing);
+ cache_id = calc_cache_topo_id(c, id4);
- for_each_online_cpu(i)
- if (cpu_data(i).topo.apicid >> index_msb == c->topo.apicid >> index_msb) {
+ for_each_online_cpu(i) {
+ /*
+ * Use @cpu's CPUID(0x4) data for CPU i too: CPU i shares this
+ * cache iff its APIC ID matches @cpu's in all bits above those
+ * reserved for the CPUs sharing the cache.
+ */
+ if (cache_id == calc_cache_topo_id(&cpu_data(i), id4)) {
struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
/* Skip if itself or no cacheinfo */
@@ -579,6 +585,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,
cpumask_set_cpu(i, &ci->shared_cpu_map);
cpumask_set_cpu(cpu, &sibling_ci->shared_cpu_map);
}
+ }
}
static void ci_info_init(struct cacheinfo *ci, const struct _cpuid4_info *id4,
--
2.43.0