dmadev: fix structure alignment

Message ID 20240308053711.1260154-1-wenwux.ma@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series dmadev: fix structure alignment |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/github-robot: build success github build: passed
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-broadcom-Performance fail Performance Testing issues
ci/iol-broadcom-Functional success Functional Testing PASS

Commit Message

Ma, WenwuX March 8, 2024, 5:37 a.m. UTC
  The structure rte_dma_dev needs cacheline alignment, but the return
value of malloc may not be aligned to the cacheline. Therefore,
extra memory is applied for realignment.

Fixes: b36970f2e13e ("dmadev: introduce DMA device library")
Cc: stable@dpdk.org

Signed-off-by: Wenwu Ma <wenwux.ma@intel.com>
---
 lib/dmadev/rte_dmadev.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
  

Comments

fengchengwen March 8, 2024, 7:01 a.m. UTC | #1
Acked-by: Chengwen Feng <fengchengwen@huawei.com>

On 2024/3/8 13:37, Wenwu Ma wrote:
> The structure rte_dma_dev needs cacheline alignment, but the return
> value of malloc may not be aligned to the cacheline. Therefore,
> extra memory is applied for realignment.
> 
> Fixes: b36970f2e13e ("dmadev: introduce DMA device library")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Wenwu Ma <wenwux.ma@intel.com>
> ---
>  lib/dmadev/rte_dmadev.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/dmadev/rte_dmadev.c b/lib/dmadev/rte_dmadev.c
> index 5953a77bd6..61e106d574 100644
> --- a/lib/dmadev/rte_dmadev.c
> +++ b/lib/dmadev/rte_dmadev.c
> @@ -160,15 +160,25 @@ static int
>  dma_dev_data_prepare(void)
>  {
>  	size_t size;
> +	void *ptr;
>  
>  	if (rte_dma_devices != NULL)
>  		return 0;
>  
> -	size = dma_devices_max * sizeof(struct rte_dma_dev);
> -	rte_dma_devices = malloc(size);
> -	if (rte_dma_devices == NULL)
> +	/* The dma device object is expected to align cacheline, but
> +	 * the return value of malloc may not be aligned to the cache line.
> +	 * Therefore, extra memory is applied for realignment.
> +	 * note: We do not call posix_memalign/aligned_alloc because it is
> +	 * version dependent on libc.
> +	 */
> +	size = dma_devices_max * sizeof(struct rte_dma_dev) +
> +		RTE_CACHE_LINE_SIZE;
> +	ptr = malloc(size);
> +	if (ptr == NULL)
>  		return -ENOMEM;
> -	memset(rte_dma_devices, 0, size);
> +	memset(ptr, 0, size);
> +
> +	rte_dma_devices = RTE_PTR_ALIGN(ptr, RTE_CACHE_LINE_SIZE);
>  
>  	return 0;
>  }
>
  

Patch

diff --git a/lib/dmadev/rte_dmadev.c b/lib/dmadev/rte_dmadev.c
index 5953a77bd6..61e106d574 100644
--- a/lib/dmadev/rte_dmadev.c
+++ b/lib/dmadev/rte_dmadev.c
@@ -160,15 +160,25 @@  static int
 dma_dev_data_prepare(void)
 {
 	size_t size;
+	void *ptr;
 
 	if (rte_dma_devices != NULL)
 		return 0;
 
-	size = dma_devices_max * sizeof(struct rte_dma_dev);
-	rte_dma_devices = malloc(size);
-	if (rte_dma_devices == NULL)
+	/* The dma device object is expected to align cacheline, but
+	 * the return value of malloc may not be aligned to the cache line.
+	 * Therefore, extra memory is applied for realignment.
+	 * note: We do not call posix_memalign/aligned_alloc because it is
+	 * version dependent on libc.
+	 */
+	size = dma_devices_max * sizeof(struct rte_dma_dev) +
+		RTE_CACHE_LINE_SIZE;
+	ptr = malloc(size);
+	if (ptr == NULL)
 		return -ENOMEM;
-	memset(rte_dma_devices, 0, size);
+	memset(ptr, 0, size);
+
+	rte_dma_devices = RTE_PTR_ALIGN(ptr, RTE_CACHE_LINE_SIZE);
 
 	return 0;
 }