]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
x86/resctrl: Fix memory bandwidth counter width for Hygon
authorXiaochen Shen <shenxiaochen@open-hieco.net>
Tue, 9 Dec 2025 06:26:50 +0000 (14:26 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Feb 2026 15:44:00 +0000 (16:44 +0100)
commit 7517e899e1b87b4c22a92c7e40d8733c48e4ec3c upstream.

The memory bandwidth calculation relies on reading the hardware counter
and measuring the delta between samples. To ensure accurate measurement,
the software reads the counter frequently enough to prevent it from
rolling over twice between reads.

The default Memory Bandwidth Monitoring (MBM) counter width is 24 bits.
Hygon CPUs provide a 32-bit width counter, but they do not support the
MBM capability CPUID leaf (0xF.[ECX=1]:EAX) to report the width offset
(from 24 bits).

Consequently, the kernel falls back to the 24-bit default counter width,
which causes incorrect overflow handling on Hygon CPUs.

Fix this by explicitly setting the counter width offset to 8 bits (resulting
in a 32-bit total counter width) for Hygon CPUs.

Fixes: d8df126349da ("x86/cpu/hygon: Add missing resctrl_cpu_detect() in bsp_init helper")
Signed-off-by: Xiaochen Shen <shenxiaochen@open-hieco.net>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20251209062650.1536952-3-shenxiaochen@open-hieco.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kernel/cpu/resctrl/core.c
arch/x86/kernel/cpu/resctrl/internal.h

index 13382523cf1f32c37865949a682c07d1ebd99390..6a52b217cfe294369c859cdd885ee9f9ce4b4c46 100644 (file)
@@ -896,8 +896,19 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c)
                c->x86_cache_occ_scale = ebx;
                c->x86_cache_mbm_width_offset = eax & 0xff;
 
-               if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
-                       c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
+               if (!c->x86_cache_mbm_width_offset) {
+                       switch (c->x86_vendor) {
+                       case X86_VENDOR_AMD:
+                               c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
+                               break;
+                       case X86_VENDOR_HYGON:
+                               c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_HYGON;
+                               break;
+                       default:
+                               /* Leave c->x86_cache_mbm_width_offset as 0 */
+                               break;
+                       }
+               }
        }
 }
 
index 4761d489a117ab07d252f15b61b840ed78035d9f..c699f3a035cc4dbfcac12e20a9d5d1b1dedb954f 100644 (file)
@@ -31,6 +31,9 @@
 #define MAX_MBA_BW_AMD                 0x800
 #define MBM_CNTR_WIDTH_OFFSET_AMD      20
 
+/* Hygon MBM counter width as an offset from MBM_CNTR_WIDTH_BASE */
+#define MBM_CNTR_WIDTH_OFFSET_HYGON    8
+
 #define RMID_VAL_ERROR                 BIT_ULL(63)
 #define RMID_VAL_UNAVAIL               BIT_ULL(62)
 /*