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 related	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  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
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Huang, Ying @ 2025-06-10  6:33 UTC (permalink / raw)
  To: Li Zhijian; +Cc: linux-mm, akpm, linux-kernel, Ying Huang

Li Zhijian <lizhijian@fujitsu.com> writes:

> 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>

Good catch!  Feel free to add

Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>

in the future version.

---
Best Regards,
Huang, Ying

[snip]


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

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  2025-06-10  6:33 ` Huang, Ying
@ 2025-06-10  6:39   ` Zhijian Li (Fujitsu)
  0 siblings, 0 replies; 8+ messages in thread
From: Zhijian Li (Fujitsu) @ 2025-06-10  6:39 UTC (permalink / raw)
  To: Huang, Ying
  Cc: linux-mm@kvack.org, akpm@linux-foundation.org,
	linux-kernel@vger.kernel.org, Ying Huang

[-- Attachment #1: Type: text/plain, Size: 1824 bytes --]


thank you Ying, it's a new record, i get a reviewed-by in 5 minutes


________________________________
From: Huang, Ying <ying.huang@linux.alibaba.com>
Sent: Tuesday, June 10, 2025 2:33:10 PM
To: Li, Zhijian/李 智坚 <lizhijian@fujitsu.com>
Cc: linux-mm@kvack.org <linux-mm@kvack.org>; akpm@linux-foundation.org <akpm@linux-foundation.org>; linux-kernel@vger.kernel.org <linux-kernel@vger.kernel.org>; Ying Huang <huang.ying.caritas@gmail.com>
Subject: Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow

Li Zhijian <lizhijian@fujitsu.com> writes:

> 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>

Good catch!  Feel free to add

Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>

in the future version.

---
Best Regards,
Huang, Ying

[snip]

[-- Attachment #2: Type: text/html, Size: 3201 bytes --]

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

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  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:40 ` Balbir Singh
  2025-06-10  8:14 ` Donet Tom
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Balbir Singh @ 2025-06-10  6:40 UTC (permalink / raw)
  To: Li Zhijian, linux-mm; +Cc: akpm, linux-kernel, Ying Huang

On 6/10/25 16:27, Li Zhijian wrote:
> 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 {

Acked-by: Balbir Singh <balbirs@nvidia.com>

Balbir


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

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  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:40 ` Balbir Singh
@ 2025-06-10  8:14 ` Donet Tom
  2025-06-10 18:45 ` Oscar Salvador
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Donet Tom @ 2025-06-10  8:14 UTC (permalink / raw)
  To: Li Zhijian, linux-mm; +Cc: akpm, linux-kernel, Ying Huang


On 6/10/25 11:57 AM, Li Zhijian wrote:
> 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))

Hi Li Zhijian

This looks good to me. Feel free to add

Reviewed-byDonet Tom <donettom@linux.ibm.com>

>   
>   struct memory_tier;
>   struct memory_dev_type {


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

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  2025-06-10  6:27 [PATCH] mm/memory-tier: Fix abstract distance calculation overflow Li Zhijian
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 8+ messages in thread
From: Oscar Salvador @ 2025-06-10 18:45 UTC (permalink / raw)
  To: Li Zhijian; +Cc: linux-mm, akpm, linux-kernel, Ying Huang

On Tue, Jun 10, 2025 at 02:27:51PM +0800, Li Zhijian wrote:
> 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>

Reviewed-by: Oscar Salvador <osalvador@suse.de>


-- 
Oscar Salvador
SUSE Labs


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

* Re: [PATCH] mm/memory-tier: Fix abstract distance calculation overflow
  2025-06-10  6:27 [PATCH] mm/memory-tier: Fix abstract distance calculation overflow Li Zhijian
                   ` (3 preceding siblings ...)
  2025-06-10 18:45 ` Oscar Salvador
@ 2025-06-10 21:02 ` Andrew Morton
  2025-06-11  2:34 ` [PATCH v2] " Li Zhijian
  5 siblings, 0 replies; 8+ messages in thread
From: Andrew Morton @ 2025-06-10 21:02 UTC (permalink / raw)
  To: Li Zhijian
  Cc: linux-mm, linux-kernel, Ying Huang, Balbir Singh, Donet Tom,
	Oscar Salvador

On Tue, 10 Jun 2025 14:27:51 +0800 Li Zhijian <lizhijian@fujitsu.com> wrote:

> 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.

Thanks.  The changelog doesn't describe the userspace-visible effects
of this.  Please always include this info.

I'll assume "minor" and it's been this way for a while so I'll add a
cc:stable to this change and shall queue it for 6.17-rc1, so it will be
backported into 6.17.x and earlier kernels at a later time.


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

* [PATCH v2] mm/memory-tier: Fix abstract distance calculation overflow
  2025-06-10  6:27 [PATCH] mm/memory-tier: Fix abstract distance calculation overflow Li Zhijian
                   ` (4 preceding siblings ...)
  2025-06-10 21:02 ` Andrew Morton
@ 2025-06-11  2:34 ` Li Zhijian
  5 siblings, 0 replies; 8+ messages in thread
From: Li Zhijian @ 2025-06-11  2:34 UTC (permalink / raw)
  To: linux-mm
  Cc: akpm, linux-kernel, Li Zhijian, stable, Huang Ying, Balbir Singh,
	Donet Tom, Oscar Salvador

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.

User-visible impact:
The memory tiering subsystem will misinterpret slow memory (like CXL)
as faster than DRAM, causing inappropriate demotion of pages from
CXL (slow memory) to DRAM (fast memory).

For example, we will see the following demotion chains from the dmesg, where
Node0,1 are DRAM, and Node2,3 are CXL node:
 Demotion targets for Node 0: null
 Demotion targets for Node 1: null
 Demotion targets for Node 2: preferred: 0-1, fallback: 0-1
 Demotion targets for Node 3: preferred: 0-1, fallback: 0-1

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: stable@vger.kernel.org
Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>
Acked-by: Balbir Singh <balbirs@nvidia.com>
Reviewed-by: Donet Tom <donettom@linux.ibm.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
V2:
  Document the 'User-visible impact' # Andrew Morton <akpm@linux-foundation.org>
---
 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 related	[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;
as well as URLs for NNTP newsgroup(s).