mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] x86/cacheinfo: Make cache IDs unique on hybrid processors
@ 2026-12-03  4:58 Ricardo Neri
  2026-12-03  4:58 ` [PATCH v2 1/2] x86/cacheinfo: Fix cache ID collisions " Ricardo Neri
  2026-12-03  4:58 ` [PATCH v2 2/2] x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup() Ricardo Neri
  0 siblings, 2 replies; 3+ messages in thread
From: Ricardo Neri @ 2026-12-03  4:58 UTC (permalink / raw)
  To: x86
  Cc: Reinette Chatre, Tony Luck, K Prateek Nayak, Ahmed S. Darwish,
	Len Brown, Javid Rzayev, Ricardo Neri, linux-kernel,
	Ricardo Neri

get_cache_id() computes the cache IDs in
/sys/devices/system/cpu/cpuX/cache/index*/id by right-shifting the APIC
ID by an amount that depends on the CPU. On hybrid processors, cores of
different types use different shifts, and caches that are not shared
end up with the same ID. Since v6.4, the generic cacheinfo code rebuilds
shared_cpu_map from these IDs when a CPU comes back online, including
after suspend to RAM and hibernation. shared_cpu_list then also reports
a P-core and an E-core as sharing their L1 cache.

Patch 1 is the fix and can be applied on its own. On hybrid processors,
it computes the exposed IDs with calc_cache_topo_id(), which masks the
APIC ID instead. All other processors keep their IDs: they do not
collide there. Changing them would break userspace that stored them,
such as libvirt's <cachetune>.

Patch 2 uses calc_cache_topo_id() for the boot-time sharing masks as
well. It does not change which CPUs are matched.

Testing:

- Lenovo ThinkPad X1 Carbon Gen 12 (Meteor Lake): without the series,
  offlining and onlining an E-core, or a hibernate/resume cycle, makes
  shared_cpu_list report the E-core as sharing its L1 cache with a
  P-core. With the series, the lists stay correct.
- AMD Ryzen AI 9 HX 370 (Zen 5 + Zen 5c), 2-socket EPYC 9555 (Zen 5) and
  2-socket EPYC 9354 (Zen 4): none of them sets X86_FEATURE_HYBRID_CPU.
  The Ryzen mixes core types, but its IDs do not collide. The series
  changes nothing userspace sees: every cache ID, resctrl domain ID and
  shared_cpu_list stays the same. This also holds across 20
  offline/online cycles of each of three CPUs per machine (the first CPU
  of an L3, another CPU in it and the first CPU's SMT sibling), and
  across a hibernate/resume cycle on the Ryzen.
- On the EPYC 9354, a libvirt domain with a stored <cachetune> cache ID
  keeps starting with the series.
- Javid tested v1 on an Arrow Lake-S Core Ultra 7 265: unique L1 IDs,
  and shared_cpu_list stays correct across CPU hotplug. Len tested v1 on
  a Meteor Lake-based Lenovo ThinkPad T16 Gen 3. The hybrid code path is
  the same in v2.
- Patch 1 alone was build-tested only.

Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
---
Changes in v2:
- Change the IDs only on hybrid processors, so that stored IDs keep
  working on all others (patch 1).
- Move the no-op __cache_cpumap_setup() change into patch 2.
- Changelog: CPU hotplug effect, Fixes: tag, resctrl IDs.
- Link to v1: https://lore.kernel.org/r/20260319-rneri-x86-cacheinfoid-bug-v1-1-5875849dd0e5@linux.intel.com

---
Ricardo Neri (2):
      x86/cacheinfo: Fix cache ID collisions on hybrid processors
      x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup()

 arch/x86/kernel/cpu/cacheinfo.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
---
base-commit: 782a14f283513f52785f7f9c207b0af2ba4aecb6
change-id: 20260220-rneri-x86-cacheinfoid-bug-71e3f7e28e94

Best regards,
-- 
Ricardo Neri <ricardo.neri-calderon@linux.intel.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] x86/cacheinfo: Fix cache ID collisions on hybrid processors
  2026-12-03  4:58 [PATCH v2 0/2] x86/cacheinfo: Make cache IDs unique on hybrid processors Ricardo Neri
@ 2026-12-03  4:58 ` Ricardo Neri
  2026-12-03  4:58 ` [PATCH v2 2/2] x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup() Ricardo Neri
  1 sibling, 0 replies; 3+ messages in thread
From: Ricardo Neri @ 2026-12-03  4:58 UTC (permalink / raw)
  To: x86
  Cc: Reinette Chatre, Tony Luck, K Prateek Nayak, Ahmed S. Darwish,
	Len Brown, Javid Rzayev, Ricardo Neri, linux-kernel,
	Ricardo Neri

calc_cache_topo_id() and get_cache_id() both compute a cache ID from an
APIC ID. Intel processors use calc_cache_topo_id() for the cache IDs in
struct cpuinfo_x86. On all vendors, get_cache_id() computes the IDs in
/sys/devices/system/cpu/cpuX/cache/index*/id. On hybrid processors,
those IDs can collide.

Both functions use N, the smallest integer such that 2^N is greater than or
equal to the maximum number of addressable IDs for logical processors
sharing a cache. That number is CPUID(0x4).EAX[25:14] + 1, with ECX as
the cache index. calc_cache_topo_id() masks off the N least-significant
bits of the APIC ID. get_cache_id() right-shifts the APIC ID by N instead.
Masking yields unique IDs even when N differs across CPUs. Shifting does
not. On hybrid processors, cores of different types can report different
values of N.

Consider the APIC IDs and CPUID(0x4) data for the L1 cache of selected
CPUs from a Meteor Lake-based Lenovo ThinkPad T16 Gen 3:

    CPU  APICID  N  calc_cache_topo_id() get_cache_id()
     2    0x18   1          0x18              0xc
     3    0x19   1          0x18              0xc
    10     0xc   0           0xc              0xc

CPUs 2 and 3 are the two logical CPUs of one core and share the L1
cache. CPU 10 does not, yet get_cache_id() assigns it the same ID.

The collision occurs without SMT too. On an Arrow Lake-S Core Ultra 7
265, which has no SMT, N is 1 for the L1 cache of the P-cores and 0 for
that of the E-cores.

The colliding IDs also corrupt shared_cpu_list after CPU hotplug. At boot,
__cache_cpumap_setup() builds the sharing masks correctly by comparing
APIC IDs. When a CPU comes back online, cache_shared_cpu_map_setup()
rebuilds them by comparing cache IDs instead. On a ThinkPad X1 Carbon
Gen 12, whose CPUs 2, 3 and 10 have the APIC IDs in the table above:

    # cat /sys/devices/system/cpu/cpu{2,10}/cache/index0/shared_cpu_list
    2-3
    10
    # echo 0 > /sys/devices/system/cpu/cpu10/online
    # echo 1 > /sys/devices/system/cpu/cpu10/online
    # cat /sys/devices/system/cpu/cpu{2,10}/cache/index0/shared_cpu_list
    2-3,10
    2-3,10

On hybrid processors, use calc_cache_topo_id() for the IDs exposed in
sysfs. If such a processor has resctrl L2 or L3 resources, their domain
IDs, which are derived from the cache IDs, change as well. Users that
read the IDs at run time from sysfs or resctrl are not affected.

On all other processors, keep get_cache_id(): their IDs do not collide.
Changing them would break userspace that stored them. A libvirt domain
whose <cachetune> element names a cache ID that no longer exists fails
to start.

Fixes: d57e3ab7e34c ("x86/intel_cacheinfo: Enable cache id in cache info")
Reported-by: Len Brown <len.brown@intel.com>
Reported-by: Javid Rzayev <rz.cavid@gmail.com>
Closes: https://lore.kernel.org/r/20261109165537.6656-1-rz.cavid@gmail.com
Signed-off-by: Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
Tested-by: Len Brown <len.brown@intel.com>
Tested-by: Javid Rzayev <rz.cavid@gmail.com>
---
Stable maintainers: besides fixing the collisions, this patch changes the
cache IDs that userspace sees on hybrid processors only. It applies
cleanly from v6.16, where calc_cache_topo_id() was introduced. Older
trees need a backport.

Changes in v2:
- Change the IDs only on hybrid processors. v1 changed them on all
  processors, including the LLC ID of AMD processors, which
  srat_detect_node() and the AMD uncore PMU driver need to stay small.
  On an EPYC 9354, the IDs from v1 also make a libvirt domain with a
  stored <cachetune> cache ID fail to start.
- Describe the userspace impact (resctrl domain IDs, libvirt <cachetune>).
- Describe the shared_cpu_list corruption after CPU hotplug (Javid).
- Add a Fixes: tag and a note for the stable maintainers.
- Move the __cache_cpumap_setup() hunk into its own patch.
---
 arch/x86/kernel/cpu/cacheinfo.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c
index 13ed16527905..4d0f47e5f6c0 100644
--- a/arch/x86/kernel/cpu/cacheinfo.c
+++ b/arch/x86/kernel/cpu/cacheinfo.c
@@ -622,7 +622,18 @@ int populate_cache_leaves(unsigned int cpu)
 		if (ret)
 			return ret;
 
-		id4.id = get_cache_id(apicid, &id4);
+		/*
+		 * On hybrid processors, cores of different types can report
+		 * different numbers of addressable IDs for the logical
+		 * processors sharing a cache. The shifted IDs of
+		 * get_cache_id() can then collide. Other processors use the
+		 * shifted IDs, which are unique there and which userspace may
+		 * have stored.
+		 */
+		if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
+			id4.id = calc_cache_topo_id(&cpu_data(cpu), &id4);
+		else
+			id4.id = get_cache_id(apicid, &id4);
 
 		if (cpu_vendor == X86_VENDOR_AMD || cpu_vendor == X86_VENDOR_HYGON)
 			nb = amd_init_l3_cache(idx);

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup()
  2026-12-03  4:58 [PATCH v2 0/2] x86/cacheinfo: Make cache IDs unique on hybrid processors Ricardo Neri
  2026-12-03  4:58 ` [PATCH v2 1/2] x86/cacheinfo: Fix cache ID collisions " Ricardo Neri
@ 2026-12-03  4:58 ` Ricardo Neri
  1 sibling, 0 replies; 3+ messages in thread
From: Ricardo Neri @ 2026-12-03  4:58 UTC (permalink / raw)
  To: x86
  Cc: Reinette Chatre, Tony Luck, K Prateek Nayak, Ahmed S. Darwish,
	Len Brown, Javid Rzayev, Ricardo Neri, linux-kernel,
	Ricardo Neri

__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@linux.intel.com>
---
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03  4:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  4:58 [PATCH v2 0/2] x86/cacheinfo: Make cache IDs unique on hybrid processors Ricardo Neri
2026-12-03  4:58 ` [PATCH v2 1/2] x86/cacheinfo: Fix cache ID collisions " Ricardo Neri
2026-12-03  4:58 ` [PATCH v2 2/2] x86/cacheinfo: Use calc_cache_topo_id() in __cache_cpumap_setup() Ricardo Neri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®