[PATCH v3] dmadev: fix structure alignment

Ma, WenwuX wenwux.ma at intel.com
Thu Mar 21 09:57:58 CET 2024


Hi, Thomas

> -----Original Message-----
> From: Thomas Monjalon <thomas at monjalon.net>
> Sent: Thursday, March 21, 2024 4:31 PM
> To: fengchengwen at huawei.com; Ma, WenwuX <wenwux.ma at intel.com>
> Cc: dev at dpdk.org; Jiale, SongX <songx.jiale at intel.com>; stable at dpdk.org
> Subject: Re: [PATCH v3] dmadev: fix structure alignment
> 
> 21/03/2024 02:25, Ma, WenwuX:
> > Hi, Thomas
> >
> > From: Thomas Monjalon <thomas at monjalon.net>
> > > 20/03/2024 08:23, Wenwu Ma:
> > > > The structure rte_dma_dev needs to be aligned to the cache line,
> > > > but the return value of malloc may not be aligned to the cache
> > > > line. When we use memset to clear the rte_dma_dev object, it may
> > > > cause a segmentation fault in clang-x86-platform.
> > > >
> > > > This is because clang uses the "vmovaps" assembly instruction for
> > > > memset, which requires that the operands (rte_dma_dev objects)
> > > > must aligned on a 16-byte boundary or a general-protection
> > > > exception (#GP) is generated.
> > > >
> > > > Therefore, either additional memory is applied for re-alignment,
> > > > or the rte_dma_dev object does not require cache line alignment.
> > > > The patch chooses the former option to fix the issue.
> > > >
> > > > Fixes: b36970f2e13e ("dmadev: introduce DMA device library")
> > > > Cc: stable at dpdk.org
> > > >
> > > > Signed-off-by: Wenwu Ma <wenwux.ma at intel.com>
> > > [..]
> > > > -	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);
> > >
> > > Why not using aligned_alloc()?
> > > https://en.cppreference.com/w/c/memory/aligned_alloc
> > >
> > >
> > because it is version dependent on libc.
> 
> Which libc is required?
> 
In the NOTE section of the link you gave there is this quote:

This function is not supported in Microsoft C Runtime library because its implementation of std::free is unable to handle aligned allocations of any kind. Instead, MS CRT provides _aligned_malloc (to be freed with _aligned_free).



More information about the stable mailing list