linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
@ 2025-06-10  6:27 Li Zhijian
  2025-06-10  6:33 ` Huang, Ying
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Li Zhijian @ 2025-06-10  6:27 UTC (permalink / raw)
  To: linux-mm; +Cc: akpm, linux-kernel, Li Zhijian, Ying Huang

In mt_perf_to_adistance(), the calculation of abstract distance (adist)
involves multiplying several int values including MEMTIER_ADISTANCE_DRAM.
```
*adist = MEMTIER_ADISTANCE_DRAM *
		(perf->read_latency + perf->write_latency) /
		(default_dram_perf.read_latency + default_dram_perf.write_latency) *
		(default_dram_perf.read_bandwidth + default_dram_perf.write_bandwidth) /
		(perf->read_bandwidth + perf->write_bandwidth);
```
Since these values can be large, the multiplication may exceed the maximum
value of an int (INT_MAX) and overflow (Our platform did), leading to an
incorrect adist.

Change MEMTIER_ADISTANCE_DRAM to be a long constant by writing it with the
'L' suffix. This prevents the overflow because the multiplication will then
be done in the long type which has a larger range.

Fixes: 3718c02dbd4c ("acpi, hmat: calculate abstract distance with HMAT")
Cc: Ying Huang <huang.ying.caritas@gmail.com>
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
 include/linux/memory-tiers.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h
index 0dc0cf2863e2..7a805796fcfd 100644
--- a/include/linux/memory-tiers.h
+++ b/include/linux/memory-tiers.h
@@ -18,7 +18,7 @@
  * adistance value (slightly faster) than default DRAM adistance to be part of
  * the same memory tier.
  */
-#define MEMTIER_ADISTANCE_DRAM	((4 * MEMTIER_CHUNK_SIZE) + (MEMTIER_CHUNK_SIZE >> 1))
+#define MEMTIER_ADISTANCE_DRAM	((4L * MEMTIER_CHUNK_SIZE) + (MEMTIER_CHUNK_SIZE >> 1))
 
 struct memory_tier;
 struct memory_dev_type {
-- 
2.41.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-06-11  2:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-10  6:27 [PATCH] mm/memory-tier: Fix abstract distance calculation overflow Li Zhijian
2025-06-10  6:33 ` Huang, Ying
2025-06-10  6:39   ` Zhijian Li (Fujitsu)
2025-06-10  6:40 ` Balbir Singh
2025-06-10  8:14 ` Donet Tom
2025-06-10 18:45 ` Oscar Salvador
2025-06-10 21:02 ` Andrew Morton
2025-06-11  2:34 ` [PATCH v2] " Li Zhijian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox