[v2] mbuf: fix strict aliasing error in allocator

Message ID 20240925154053.80861-2-rjarry@redhat.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] mbuf: fix strict aliasing error in allocator |

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/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance fail Performance Testing issues
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS

Commit Message

Robin Jarry Sept. 25, 2024, 3:40 p.m. UTC
When building an application with -fstrict-aliasing -Wstrict-aliasing=2,
we get errors triggered by rte_mbuf_raw_alloc() which is called inline
from rte_pktmbuf_alloc().

 ../dpdk/lib/mbuf/rte_mbuf.h: In function ‘rte_mbuf_raw_alloc’:
 ../dpdk/lib/mbuf/rte_mbuf.h:600:42: error: dereferencing type-punned
 pointer might break strict-aliasing rules [-Werror=strict-aliasing]
   600 |         if (rte_mempool_get(mp, (void **)&m) < 0)
       |                                          ^~

Avoid incorrect casting by using an inline union variable.

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---

Notes:
    v2: use inline union to fix -Wincompatible-pointer-types

 lib/mbuf/rte_mbuf.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
  

Comments

Stephen Hemminger Sept. 25, 2024, 3:47 p.m. UTC | #1
On Wed, 25 Sep 2024 11:40:54 -0400
Robin Jarry <rjarry@redhat.com> wrote:

> From: Robin Jarry <rjarry@redhat.com>
> To: dev@dpdk.org
> Subject: [PATCH dpdk v2] mbuf: fix strict aliasing error in allocator
> Date: Wed, 25 Sep 2024 11:40:54 -0400
> 
> When building an application with -fstrict-aliasing -Wstrict-aliasing=2,
> we get errors triggered by rte_mbuf_raw_alloc() which is called inline
> from rte_pktmbuf_alloc().
> 
>  ../dpdk/lib/mbuf/rte_mbuf.h: In function ‘rte_mbuf_raw_alloc’:
>  ../dpdk/lib/mbuf/rte_mbuf.h:600:42: error: dereferencing type-punned
>  pointer might break strict-aliasing rules [-Werror=strict-aliasing]
>    600 |         if (rte_mempool_get(mp, (void **)&m) < 0)
>        |                                          ^~
> 
> Avoid incorrect casting by using an inline union variable.
> 
> Signed-off-by: Robin Jarry <rjarry@redhat.com>

Thanks, union is safer than cast.

Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
  
Ali Alnubani Oct. 17, 2024, 2:17 p.m. UTC | #2
> -----Original Message-----
> From: Robin Jarry <rjarry@redhat.com>
> Sent: Wednesday, September 25, 2024 6:41 PM
> To: dev@dpdk.org
> Subject: [PATCH dpdk v2] mbuf: fix strict aliasing error in allocator
> 
> When building an application with -fstrict-aliasing -Wstrict-aliasing=2,
> we get errors triggered by rte_mbuf_raw_alloc() which is called inline
> from rte_pktmbuf_alloc().
> 
>  ../dpdk/lib/mbuf/rte_mbuf.h: In function ‘rte_mbuf_raw_alloc’:
>  ../dpdk/lib/mbuf/rte_mbuf.h:600:42: error: dereferencing type-punned
>  pointer might break strict-aliasing rules [-Werror=strict-aliasing]
>    600 |         if (rte_mempool_get(mp, (void **)&m) < 0)
>        |                                          ^~
> 
> Avoid incorrect casting by using an inline union variable.
> 
> Signed-off-by: Robin Jarry <rjarry@redhat.com>
> ---
> 
> Notes:
>     v2: use inline union to fix -Wincompatible-pointer-types
> 
>  lib/mbuf/rte_mbuf.h | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
> index babe16c72ccb..0d2e0e64b3ce 100644
> --- a/lib/mbuf/rte_mbuf.h
> +++ b/lib/mbuf/rte_mbuf.h
> @@ -595,12 +595,15 @@ __rte_mbuf_raw_sanity_check(__rte_unused const
> struct rte_mbuf *m)
>   */
>  static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
>  {
> -	struct rte_mbuf *m;
> +	union {
> +		void *ptr;
> +		struct rte_mbuf *m;
> +	} ret;
> 
> -	if (rte_mempool_get(mp, (void **)&m) < 0)
> +	if (rte_mempool_get(mp, &ret.ptr) < 0)
>  		return NULL;
> -	__rte_mbuf_raw_sanity_check(m);
> -	return m;
> +	__rte_mbuf_raw_sanity_check(ret.m);
> +	return ret.m;
>  }
> 
>  /**
> --
> 2.46.1

Hello,

We noticed the failure in ci/iol-mellanox-Performance and ran a few performance test cases on NVIDIA hardware and found no regressions.
Additionally ran build and functional test cases that also passed.

Tested-by: Ali Alnubani <alialnu@nvidia.com>

Regards,
Ali
  
Thomas Monjalon Oct. 17, 2024, 7:44 p.m. UTC | #3
25/09/2024 17:47, Stephen Hemminger:
> On Wed, 25 Sep 2024 11:40:54 -0400
> Robin Jarry <rjarry@redhat.com> wrote:
> 
> > From: Robin Jarry <rjarry@redhat.com>
> > To: dev@dpdk.org
> > Subject: [PATCH dpdk v2] mbuf: fix strict aliasing error in allocator
> > Date: Wed, 25 Sep 2024 11:40:54 -0400
> > 
> > When building an application with -fstrict-aliasing -Wstrict-aliasing=2,
> > we get errors triggered by rte_mbuf_raw_alloc() which is called inline
> > from rte_pktmbuf_alloc().
> > 
> >  ../dpdk/lib/mbuf/rte_mbuf.h: In function ‘rte_mbuf_raw_alloc’:
> >  ../dpdk/lib/mbuf/rte_mbuf.h:600:42: error: dereferencing type-punned
> >  pointer might break strict-aliasing rules [-Werror=strict-aliasing]
> >    600 |         if (rte_mempool_get(mp, (void **)&m) < 0)
> >        |                                          ^~
> > 
> > Avoid incorrect casting by using an inline union variable.
> > 
> > Signed-off-by: Robin Jarry <rjarry@redhat.com>
> 
> Thanks, union is safer than cast.
> 
> Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>

Applied, thanks.
  

Patch

diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
index babe16c72ccb..0d2e0e64b3ce 100644
--- a/lib/mbuf/rte_mbuf.h
+++ b/lib/mbuf/rte_mbuf.h
@@ -595,12 +595,15 @@  __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m)
  */
 static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp)
 {
-	struct rte_mbuf *m;
+	union {
+		void *ptr;
+		struct rte_mbuf *m;
+	} ret;
 
-	if (rte_mempool_get(mp, (void **)&m) < 0)
+	if (rte_mempool_get(mp, &ret.ptr) < 0)
 		return NULL;
-	__rte_mbuf_raw_sanity_check(m);
-	return m;
+	__rte_mbuf_raw_sanity_check(ret.m);
+	return ret.m;
 }
 
 /**