From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796273908; b=L405MefgcagQDWPQ0qHuHxkUd4Lc2VimHrtaOMDx8HgdaAbdxXepEGrEHubU/GzHoo6J r+0AY38E4In9qHLPxk3bkgWLZQFRhKnDcXYbLeTbTxnuEdF/U2scQ+aknCBkuOSgVFrnX iaQ+KQd9gTdQfm/Yv6PXkzTyobSFVqe9tbXikRmV0Tt4l6Dtm+c0Pd0+A2dVoTYVOCy4d eCyXU1Rp1nAOJTfUP5W7IkvuaZhtZmQwnJE94rM4LDaNs/f9CXnByQMXM0GyFiWqb8Gq3 tyf4onKv615ybwJHnamoQvcz9IeTKvGoM78I9+Qj7JryJTBcaLAAQCVXPbuUl4JK/fQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273908; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key; bh=AGWYpw51txveICj8U9ZstIvoGP6epytiLZS8simjeqw=; b=g2jN9NHuitztsds+2h0Mugt0JudPpcA2BiyqdDfCH57ijpYR3lS2vWqwWcjJXOclLdHX 52p5yLBkQy4fCQr1PHeCYzP865+X8aN/cTmgCQmQ5/aBmCU4VeVTQAbWjlDWf8yuF0NNz +BQzqraZdguOfWqTtrMSRFBe8YDWzDyyS4iPBZM6+3GuZHCkGzp0kLRHL0bN4ORBlTMNj 2cdd64lXaZaaX7wo6+Vd9FgsfHiRxqEp1W0PFtJ/fmia5qeaz2NhmrtxQdsFzuzWGKOaN EbBI9BMD9MltcrbfLnxlQ1Og2nXb19NajMB64Yv0JvLbLsVpXMcAA5TVGK7UbpAyaqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=hlMBTFyE; arc=none smtp.remote-ip=198.175.65.11 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DF48606A335 for ; Thu, 3 Dec 2026 04:58:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9DF48606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=hlMBTFyE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796273908; x=1827809908; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=AGWYpw51txveICj8U9ZstIvoGP6epytiLZS8simjeqw=; b=hlMBTFyEE31E2+P+y4w3wmY5veKWQYXEVEoL8x/ePTc70Rtz6IsL2p57 JpjMdWlZkwfV+ioz33jUpAVz6IH0256RhiucXo8aLbDUdLss5ajAzVr6K NhONDwWpl2bc+XECHk+DBUL6gcmV4U1EvvGwYkvZpgs5+GXFW2FmUpXl1 QP9pLwELI/ccH3LlHjKySvePo1O+dm927+xiferRoMf1R9l25oRGgoo2g cf6Yg6n+9GRsyoP3nrxyOWavHaZzxaelw67+U2d5sNebldXu9Uoq7ksti hRNaVeyl4KxKKtUNDusXLBOGy8/Wnl+7kWLoLYyVuVUZkjljjLNiwwPwB w==; X-CSE-ConnectionGUID: RezMjUu2RhSAF7a3kdQ87A== X-CSE-MsgGUID: 5zsrGVhcS+W7ovZ8Nxg8Vg== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5115264" X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="5115264" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 20:58:27 -0800 X-CSE-ConnectionGUID: mdO5izpjQLSTaWv0b0nxig== X-CSE-MsgGUID: Dq9Wt7pjT1uL7WUnk28ERw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="13534972" Received: from unknown (HELO [172.25.112.21]) ([172.25.112.21]) by orviesa004.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 20:58:27 -0800 From: Ricardo Neri Date: Wed, 02 Dec 2026 20:58:04 -0800 Subject: [PATCH v2 1/2] x86/cacheinfo: Fix cache ID collisions on hybrid processors X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-rneri-x86-cacheinfoid-bug-v2-1-a00d768927f2@linux.intel.com> References: <20261202-rneri-x86-cacheinfoid-bug-v2-0-a00d768927f2@linux.intel.com> In-Reply-To: <20261202-rneri-x86-cacheinfoid-bug-v2-0-a00d768927f2@linux.intel.com> To: x86@kernel.org Cc: Reinette Chatre , Tony Luck , K Prateek Nayak , "Ahmed S. Darwish" , Len Brown , Javid Rzayev , Ricardo Neri , linux-kernel@vger.kernel.org, Ricardo Neri X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796273905; l=4994; i=ricardo.neri-calderon@linux.intel.com; s=20250602; h=from:subject:message-id; bh=ITBBvXYKLzeFTSMAl8Df14Bopj5HQIKiL3CvjkjqZ0E=; b=szbb9dttBBxewalhWwm4NenYVjkgWerw2yFZEtJOY6Tpkrt5UI/raoFD72IwIwmsNUb0gyngQ TTnrn/w3QQtBQQS8DtfaZu6JRXN76edntCK9ToBV1lvhaSNeMOrl3bL X-Developer-Key: i=ricardo.neri-calderon@linux.intel.com; a=ed25519; pk=NfZw5SyQ2lxVfmNMaMR6KUj3+0OhcwDPyRzFDH9gY2w= 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 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 Reported-by: Javid Rzayev Closes: https://lore.kernel.org/r/20261109165537.6656-1-rz.cavid@gmail.com Signed-off-by: Ricardo Neri Tested-by: Len Brown Tested-by: Javid Rzayev --- 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 cache ID fail to start. - Describe the userspace impact (resctrl domain IDs, libvirt ). - 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