From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out28-122.mail.aliyun.com (out28-122.mail.aliyun.com [115.124.28.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1A422F1FC9 for ; Sat, 6 Dec 2025 16:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.28.122 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765037672; cv=none; b=aQL1DxuOtk5QQPPK1R59dxJ+Serbes1mPJ2hz5X7jTqvg2VOwSDbDgESSTSct4sM9UAMwucX0Oa9HF/EQ7ZUUXEML3DS9k+gy1k9bS+1JFTG57YoIgxMkw0q4pNBRQS4KDW3mCbYvqTPPN7dhqjZ0yXXKzRpdSsIfkZgL8qYc1w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765037672; c=relaxed/simple; bh=wyjyHZAle+9GTQmvvxhzNys6jDPf/cKNlY7++Pa/u60=; h=Message-ID:Date:MIME-Version:From:Subject:To:Cc:References: In-Reply-To:Content-Type; b=Wun0FNqZ+Vg1zpkQrCxWyZmg7wcjACgiqpmz4J1+cAEzKIOSebz4K7nA/swyIWz8bwfCQobI7p6KfNVfcd+0KUKkqd8KnucS98HVROGGV0cIkaKqbYEdsXk7UHv2n53XY50cRExMEyE7Rch6mJpJBFMdpVhe+pBtu4PsbJmBCe8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=open-hieco.net; spf=pass smtp.mailfrom=open-hieco.net; arc=none smtp.client-ip=115.124.28.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=open-hieco.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=open-hieco.net Received: from 172.18.26.4(mailfrom:shenxiaochen@open-hieco.net fp:SMTPD_---.feLzeHu_1765037652 cluster:ay29) by smtp.aliyun-inc.com; Sun, 07 Dec 2025 00:14:14 +0800 Message-ID: Date: Sun, 7 Dec 2025 00:14:12 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Xiaochen Shen Subject: Re: [PATCH 2/2] x86/resctrl: Fix memory bandwidth counter width for Hygon To: "Luck, Tony" , "Chatre, Reinette" , "bp@alien8.de" , "fenghuay@nvidia.com" Cc: "babu.moger@amd.com" , "james.morse@arm.com" , "Dave.Martin@arm.com" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , shenxiaochen@open-hieco.net References: <20251204023750.994143-1-shenxiaochen@open-hieco.net> <20251204023750.994143-3-shenxiaochen@open-hieco.net> <03618cf2-b8c8-4493-a991-04bced42c7dd@open-hieco.net> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Tony, On 12/6/2025 4:33 AM, Luck, Tony wrote: >> We have observed a test case where an incorrect counter width leads to random unexpected memory bandwidth readings: >> https://github.com/shenxiaochen/my_documents/blob/main/memory_bandwidth_counter_width_and_overflow_issue_steps_to_reproduce.txt >> >> The issue was resolved by applying this patch. > 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. > --The test output:---- # ./counter_width width = 24 2 width = 32 2 ---------------------- > 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. > >From my understanding, mbm_overflow_count() works as expected if a correct counter width is passed as a parameter. In my opinion, the root cause is that: The incorrect counter width (24-bits) is passed to mbm_overflow_count(), which is much smaller than the hardware counter width (32-bits). As a result, the data between bit 24 and bit 32 of the counter is *discarded* unexpectedly in bandwidth delta calculation in this scenario: (1) Kernel firstly reads the hardware counter. (2) Kernel secondly reads the hardware counter with 1 second interval. Between (1) and (2), if 32-bits hardware counter really hits overflow, mbm_overflow_count() still handles with 24-bits counter width as a parameter: 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; } The calculated bandwidth delta is incorrect at this time, because the data between bit 24 and bit 32 of the counter is *discarded* unexpectedly. See more debugging information as below. > Question: What is the value of "hw_res->mon_scale" on a Hygon system? I have double confirmed with Hygon hardware architect, in the testing Hygon system: hw_res->mon_scale: 64 hw_res->mbm_width: 32 Here is a rough calculation for the theoretical max bandwidth by the hardware counter: (1) 32-bits width and mon_scale is 64: 2 ^32 * 64 = 2 ^38 = 256 (B/s) (2) 24-bits width and mon_scale is 64 (this is highly likely to cause overflow): 2 ^24 * 64 = 2 ^30 = 1G (B/s) FYI - debugging for the overflow issue: -------------------------------------------- I run the test case in [1] again, try to capture more useful information: [1]https://github.com/shenxiaochen/my_documents/blob/main/memory_bandwidth_counter_width_and_overflow_issue_steps_to_reproduce.txt # ./mbm_total_verbose.sh ... // Normal data total b/w (bytes/s): 31192093760 (77687336665701824 - 77687305473608064) total b/w (bytes/s): 31205362048 (77687367871063872 - 77687336665701824) 77687336665701824 / 64 = 1213864635401591 (0x45000E2660577) 77687367871063872 / 64 = 1213865122985373 (0x45000FF75F59D) // Unexpected calculated bandwidth value, hardware should hit overflow: total b/w (bytes/s): 1125656235801792 (78813024106865664 - 77687367871063872) // The data analysis: // Before overflow, low 32-bits of the counter is close to overflow (0xFF75F59D). Will hit hardware overflow immediately! Read 1: 77687367871063872 / 64 = 1213865122985373 (0x45000FF75F59D) // 1 second later, after overflow, the low 32-bits of the counter is (0x1C864190). Read 2: 78813024106865664 / 64 = 1231453501669776 (0x460001C864190) // The calculated delta is incorrect (the data between bit 24 and bit 32 of the counter is discarded unexpectedly). We see the unexpected bandwidth value: total b/w (bytes/s): 1125656235801792 (78813024106865664 - 77687367871063872) Best regards, Xiaochen Shen