dmadev: fix structure alignment
Checks
Commit Message
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
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;
> }
>
@@ -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;
}