Skip to content

Commit a3d33ce

Browse files
shenxiaochenlanlanxiyiji
authored andcommitted
x86/resctrl: Fix memory bandwidth counter width for Hygon
commit 7517e89 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: d8df126 ("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> (cherry picked from commit b73f2834e1bc6daeb67ef43404149073e7dccadb) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent d390d3d commit a3d33ce

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

arch/x86/kernel/cpu/resctrl/core.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,19 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c)
943943
c->x86_cache_occ_scale = ebx;
944944
c->x86_cache_mbm_width_offset = eax & 0xff;
945945

946-
if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
947-
c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
946+
if (!c->x86_cache_mbm_width_offset) {
947+
switch (c->x86_vendor) {
948+
case X86_VENDOR_AMD:
949+
c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
950+
break;
951+
case X86_VENDOR_HYGON:
952+
c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_HYGON;
953+
break;
954+
default:
955+
/* Leave c->x86_cache_mbm_width_offset as 0 */
956+
break;
957+
}
958+
}
948959
}
949960
}
950961

arch/x86/kernel/cpu/resctrl/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#define MBA_IS_LINEAR 0x4
2121
#define MBM_CNTR_WIDTH_OFFSET_AMD 20
2222

23+
/* Hygon MBM counter width as an offset from MBM_CNTR_WIDTH_BASE */
24+
#define MBM_CNTR_WIDTH_OFFSET_HYGON 8
25+
2326
#define RMID_VAL_ERROR BIT_ULL(63)
2427
#define RMID_VAL_UNAVAIL BIT_ULL(62)
2528
/*

0 commit comments

Comments
 (0)