Re: [PATCH 2/2] x86/resctrl: Fix memory bandwidth counter width for Hygon
From: Xiaochen Shen
Date: Sat Dec 06 2025 - 19:57:04 EST
Hi Tony,
(Sorry to resent. The previous thread sent to some recipients was bounced by Recipient System)
On 12/6/2025 4:33 AM, Luck, Tony wrote:
> Clearly something is going wrong, and you sometime see enormous values for
> memory bandwidth. But I'm still puzzled about what is going wrong.
>
> I pasted the resctrl wraparound function into a small user-mode test, and it
> seems to be able to ignore bits above the width used.
>
> The test below prints "2" when told the width is either 24 or 32.
>
> Your patch to use width = 32 is good, but if the problem isn't in the mbm_overflow_count()
> function, then you might just have made the problem 256X harder to hit.#include <stdio.h>
>
> typedef unsigned long long u64;
>
> static u64 mbm_overflow_count(u64 prev_msr, u64 cur_msr, unsigned int width)
> {
> u64 shift = 64 - width, chunks;
>
> chunks = (cur_msr << shift) - (prev_msr << shift);
> return chunks >> shift;
> }
>
> int main(void)
> {
> u64 prev, cur;
>
> prev = 0xffffff;
> cur = 0x1000001;
>
> printf("width = 24 %lld\n", mbm_overflow_count(prev, cur, 24));
>
> printf("width = 32 %lld\n", mbm_overflow_count(prev, cur, 32));
>
> return 0;
> }
I think, this issue could be reproduced by your test program with a minor change (e.g., cur.bits[31:24] > 1):
- cur = 0x1000001;
+ cur = 0xf000001;
// The calculated value 2 is incorrrect with passing 24 bits counter width:
# ./counter_width
width = 24 2
width = 32 234881026
Thank you!
Best regards,
Xiaochen Shen