Skip to contentSkip to conversation

Fix unused memseg length for memory issue

PATCH In review Yang Ming 3 participants 6 messages Dec 26, 2024 – Feb 7, 2025

lore
Share & tools
lore

Yang Ming

author
#0 lore raw

PATCH 1/1 Fix unused memseg length for memory issue

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init releases the VA space for unused memseg lists, it does not reset their length to 0. As a result, mlx5_mem_is_rte may incorrectly identify OS memory as DPDK memory. This can lead to mlx_free calling rte_free on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte to incorrectly treat it as DPDK memory.

Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")
---
 lib/eal/linux/eal_memory.c | 1 +
 1 file changed, 1 insertion(+)
lib/eal/linux/eal_memory.c +1 -0
index 45879ca743..9dda60c0e1 100644
@@ -1472,6 +1472,7 @@ eal_legacy_hugepage_init(void)
 		mem_sz = msl->len;
 		munmap(msl->base_va, mem_sz);
 		msl->base_va = NULL;
+		msl->len = 0;
 		msl->heap = 0;
 
 		/* destroy backing fbarray */
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw

2024-12-26 16:10 (UTC+0800), Yang Ming:

Quoted 29 lines

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init releases the VA space for unused memseg lists, it does not reset their length to 0. As a result, mlx5_mem_is_rte may incorrectly identify OS memory as DPDK memory. This can lead to mlx_free calling rte_free on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte to incorrectly treat it as DPDK memory.

Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")
---
 lib/eal/linux/eal_memory.c | 1 +
 1 file changed, 1 insertion(+)
lib/eal/linux/eal_memory.c +0 -0
index 45879ca743..9dda60c0e1 100644

lib/eal/linux/eal_memory.c eal_legacy_hugepage_init(void) · line 1478

Quoted 7 lines
lib/eal/linux/eal_memory.c +1 -0
@@ -1472,6 +1472,7 @@ eal_legacy_hugepage_init(void)
 		mem_sz = msl->len;
 		munmap(msl->base_va, mem_sz);
 		msl->base_va = NULL;
+		msl->len = 0;
 		msl->heap = 0;
 
 		/* destroy backing fbarray */

Hi Yang,

It seems the bug affects more than just mlx5 PMD.

Consider how the MSL with base_va == NULL ends up in mlx5_mem_is_rte(). It comes from rte_mem_virt2memseg_list() which iterates MSLs and checks that an address belongs to [base_va; base_va+len) without checking whether base_va == NULL i.e. that the MSL is inactive. Your patch also corrects rte_mem_virt2memseg_list() behavior. Please mention this in the commit message.

author
#2 lore raw
Quoted 41 lines · Dmitry Kozlyuk

2024-12-26 16:10 (UTC+0800), Yang Ming:

Quoted 29 lines

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init releases the VA space for unused memseg lists, it does not reset their length to 0. As a result, mlx5_mem_is_rte may incorrectly identify OS memory as DPDK memory. This can lead to mlx_free calling rte_free on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte to incorrectly treat it as DPDK memory.

Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")
---
  lib/eal/linux/eal_memory.c | 1 +
  1 file changed, 1 insertion(+)
lib/eal/linux/eal_memory.c +0 -0
index 45879ca743..9dda60c0e1 100644

lib/eal/linux/eal_memory.c eal_legacy_hugepage_init(void) · line 1478

Quoted 7 lines
lib/eal/linux/eal_memory.c +1 -0
@@ -1472,6 +1472,7 @@ eal_legacy_hugepage_init(void)
  		mem_sz = msl->len;
  		munmap(msl->base_va, mem_sz);
  		msl->base_va = NULL;
+		msl->len = 0;
  		msl->heap = 0;
 
  		/* destroy backing fbarray */

Hi Yang,

It seems the bug affects more than just mlx5 PMD.

Quoted 8 lines

Consider how the MSL with base_va == NULL ends up in mlx5_mem_is_rte(). It comes from rte_mem_virt2memseg_list() which iterates MSLs and checks that an address belongs to [base_va; base_va+len) without checking whether base_va == NULL i.e. that the MSL is inactive. Your patch also corrects rte_mem_virt2memseg_list() behavior. Please mention this in the commit message.

Hi Dmitry,

Thanks. I will update this patch within new version (v2) to add these content to the commit log.

author
#3 lore raw

PATCH v2 1/1 eal: fix unused memseg length

In reply to Yang Ming

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init() releases the VA space for unused memseg lists(MSLs), it does not reset MSLs' length to 0. As a result, mlx5_mem_is_rte() may incorrectly identify OS memory as rte memory.
This can lead to mlx_free() calling rte_free() on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte() to incorrectly treat it as rte memory.

Also, consider how the MSL with base_va == NULL ends up in mlx5_mem_is_rte(). It comes from rte_mem_virt2memseg_list() which iterates MSLs and checks that an address belongs to [base_va; base_va+len) without checking whether base_va == NULL i.e. that the MSL is inactive. So this patch also corrects rte_mem_virt2memseg_list() behavior.

Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")
---
 lib/eal/linux/eal_memory.c | 1 +
 1 file changed, 1 insertion(+)
lib/eal/linux/eal_memory.c +1 -0
index 45879ca743..9dda60c0e1 100644
@@ -1472,6 +1472,7 @@ eal_legacy_hugepage_init(void)
 		mem_sz = msl->len;
 		munmap(msl->base_va, mem_sz);
 		msl->base_va = NULL;
+		msl->len = 0;
 		msl->heap = 0;
 
 		/* destroy backing fbarray */
Signature · 1 line
2.34.1
author
#4 lore raw

Re: [PATCH v2] eal: fix unused memseg length

Hi experts, is there any chance to review and accept this patch?

Quoted 46 lines · Yang Ming

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init() releases the VA space for unused memseg lists(MSLs), it does not reset MSLs' length to 0. As a result, mlx5_mem_is_rte() may incorrectly identify OS memory as rte memory.
This can lead to mlx_free() calling rte_free() on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte() to incorrectly treat it as rte memory.

Also, consider how the MSL with base_va == NULL ends up in mlx5_mem_is_rte(). It comes from rte_mem_virt2memseg_list() which iterates MSLs and checks that an address belongs to [base_va; base_va+len) without checking whether base_va == NULL i.e. that the MSL is inactive. So this patch also corrects rte_mem_virt2memseg_list() behavior.

Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")
---
  lib/eal/linux/eal_memory.c | 1 +
  1 file changed, 1 insertion(+)
lib/eal/linux/eal_memory.c +1 -0
index 45879ca743..9dda60c0e1 100644
@@ -1472,6 +1472,7 @@ eal_legacy_hugepage_init(void)
  		mem_sz = msl->len;
  		munmap(msl->base_va, mem_sz);
  		msl->base_va = NULL;
+		msl->len = 0;
  		msl->heap = 0;
 
  		/* destroy backing fbarray */
#5 lore raw

Re: [PATCH v2] eal: fix unused memseg length

In reply to Yang Ming
Quoted 25 lines · Yang Ming

Fix the issue where OS memory is mistakenly freed with rte_free by setting the length (len) of unused memseg to 0.

When eal_legacy_hugepage_init() releases the VA space for unused memseg lists(MSLs), it does not reset MSLs' length to 0. As a result, mlx5_mem_is_rte() may incorrectly identify OS memory as rte memory.
This can lead to mlx_free() calling rte_free() on OS memory, causing an "EAL: Error: Invalid memory" log and failing to free the OS memory.

This issue is occasional and occurs when the DPDK program’s memory map places the heap address range between 0 and len(32G). In such cases, malloc may return an address less than len, causing mlx5_mem_is_rte() to incorrectly treat it as rte memory.

Also, consider how the MSL with base_va == NULL ends up in mlx5_mem_is_rte(). It comes from rte_mem_virt2memseg_list() which iterates MSLs and checks that an address belongs to [base_va; base_va+len) without checking whether base_va == NULL i.e. that the MSL is inactive. So this patch also corrects rte_mem_virt2memseg_list() behavior.

Quoted 5 lines
Fixes 66cc45e293ed ("mem: replace memseg with memseg lists")

Applied, thanks for the fix.

Signature · 1 line
David Marchand