eal: change alloc_sz calculation which may cause unnecessarily allocation

Message ID 20220718041142.411770-1-fidaullah.noonari@emumba.com (mailing list archive)
State Superseded, archived
Delegated to: David Marchand
Headers
Series eal: change alloc_sz calculation which may cause unnecessarily allocation |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/github-robot: build fail github build: failed
ci/iol-x86_64-unit-testing fail Testing issues
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS

Commit Message

Fidaullah Noonari July 18, 2022, 4:11 a.m. UTC
In try_expand_heap() alloc_sz is calculated which may result in 
unnecessary allocation of whole huge page, this may cause allocation
limit from system or eal

In response to this mail:
<CAEYuUWCnRZNwxiOHEeTHw0Gy9aFJRLZtvAG9g=smuUvUEMcFXg@mail.gmail.com>

Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
---
 lib/eal/common/malloc_heap.c | 2 +-
 lib/eal/common/malloc_mp.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
  

Comments

Dmitry Kozlyuk July 27, 2022, 8:23 a.m. UTC | #1
2022-07-18 09:11 (UTC+0500), Fidaullah Noonari:
> In try_expand_heap() alloc_sz is calculated
> which may result in unnecessary allocation of whole huge page,
> this may cause allocation limit from system or eal

Let me suggest a rewording:

The amount of memory to allocate from the system for heap expansion
was calculated in a way that may yield one page more than needed.
This could hit the allocation limit from the system or EAL.
The allocation would fail despite enough memory being available.

> In response to this mail:
> <CAEYuUWCnRZNwxiOHEeTHw0Gy9aFJRLZtvAG9g=smuUvUEMcFXg@mail.gmail.com>

Proper links to mails.dpdk.org or inbox.dpdk.org are preferred.

> 
> Signed-off-by: Fidaullah Noonari <fidaullah.noonari@emumba.com>
> ---
>  lib/eal/common/malloc_heap.c | 2 +-
>  lib/eal/common/malloc_mp.c   | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/eal/common/malloc_heap.c b/lib/eal/common/malloc_heap.c
> index 27a52266ad..e348e385e4 100644
> --- a/lib/eal/common/malloc_heap.c
> +++ b/lib/eal/common/malloc_heap.c
> @@ -402,7 +402,7 @@ try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
>  	int n_segs;
>  	bool callback_triggered = false;
>  
> -	alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
> +	alloc_sz = RTE_ALIGN_CEIL(RTE_ALIGN_CEIL(align, elt_size) +
>  			MALLOC_ELEM_OVERHEAD, pg_sz);
>  	n_segs = alloc_sz / pg_sz;
>
> diff --git a/lib/eal/common/malloc_mp.c b/lib/eal/common/malloc_mp.c
> index 2b8eb51067..1b79ed5e21 100644
> --- a/lib/eal/common/malloc_mp.c
> +++ b/lib/eal/common/malloc_mp.c
> @@ -249,7 +249,7 @@ handle_alloc_request(const struct malloc_mp_req *m,
>  		return -1;
>  	}
>  
> -	alloc_sz = RTE_ALIGN_CEIL(ar->align + ar->elt_size +
> +	alloc_sz = RTE_ALIGN_CEIL(RTE_ALIGN_CEIL(ar->align, ar->elt_size) +
>  			MALLOC_ELEM_OVERHEAD, ar->page_sz);
>  	n_segs = alloc_sz / ar->page_sz;
>  

Wrong argument order for RTE_ALIGN_CEIL(size, alignment) in both places.
  

Patch

diff --git a/lib/eal/common/malloc_heap.c b/lib/eal/common/malloc_heap.c
index 27a52266ad..e348e385e4 100644
--- a/lib/eal/common/malloc_heap.c
+++ b/lib/eal/common/malloc_heap.c
@@ -402,7 +402,7 @@  try_expand_heap_primary(struct malloc_heap *heap, uint64_t pg_sz,
 	int n_segs;
 	bool callback_triggered = false;
 
-	alloc_sz = RTE_ALIGN_CEIL(align + elt_size +
+	alloc_sz = RTE_ALIGN_CEIL(RTE_ALIGN_CEIL(align, elt_size) +
 			MALLOC_ELEM_OVERHEAD, pg_sz);
 	n_segs = alloc_sz / pg_sz;
 
diff --git a/lib/eal/common/malloc_mp.c b/lib/eal/common/malloc_mp.c
index 2b8eb51067..1b79ed5e21 100644
--- a/lib/eal/common/malloc_mp.c
+++ b/lib/eal/common/malloc_mp.c
@@ -249,7 +249,7 @@  handle_alloc_request(const struct malloc_mp_req *m,
 		return -1;
 	}
 
-	alloc_sz = RTE_ALIGN_CEIL(ar->align + ar->elt_size +
+	alloc_sz = RTE_ALIGN_CEIL(RTE_ALIGN_CEIL(ar->align, ar->elt_size) +
 			MALLOC_ELEM_OVERHEAD, ar->page_sz);
 	n_segs = alloc_sz / ar->page_sz;