mbuf: fix strict aliasing error in allocator

Message ID 20240925140021.46320-2-rjarry@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series 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/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/github-robot: build fail github build: failed
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-unit-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, 2 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 changing the type of the returned variable.

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---
 lib/mbuf/rte_mbuf.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Morten Brørup Sept. 25, 2024, 3:21 p.m. UTC | #1
> From: Robin Jarry [mailto:rjarry@redhat.com]
> Sent: Wednesday, 25 September 2024 10.00
> 
> 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 changing the type of the returned variable.
> 
> Signed-off-by: Robin Jarry <rjarry@redhat.com>
> ---
>  lib/mbuf/rte_mbuf.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
> index babe16c72ccb..bab1fb94d41d 100644
> --- a/lib/mbuf/rte_mbuf.h
> +++ b/lib/mbuf/rte_mbuf.h
> @@ -595,9 +595,9 @@ __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;
> +	void *m;
> 
> -	if (rte_mempool_get(mp, (void **)&m) < 0)
> +	if (rte_mempool_get(mp, &m) < 0)
>  		return NULL;
>  	__rte_mbuf_raw_sanity_check(m);
>  	return m;

Suggest:
	__rte_mbuf_raw_sanity_check((struct rte_mbuf *)m);
	return (struct rte_mbuf *)m;

> --
> 2.46.1

Please confirm that this builds with RTE_LIBRTE_MBUF_DEBUG and RTE_ENABLE_ASSERT set in config/rte_config.h. When confirmed, you may add:
Acked-by: Morten Brørup <mb@smartsharesystems.com>
  
Stephen Hemminger Sept. 25, 2024, 3:23 p.m. UTC | #2
On Wed, 25 Sep 2024 17:21:12 +0200
Morten Brørup <mb@smartsharesystems.com> wrote:

> From: Morten Brørup <mb@smartsharesystems.com>
> To: "Robin Jarry" <rjarry@redhat.com>,  <dev@dpdk.org>
> Subject: RE: [PATCH dpdk] mbuf: fix strict aliasing error in allocator
> Date: Wed, 25 Sep 2024 17:21:12 +0200
> 
> > From: Robin Jarry [mailto:rjarry@redhat.com]
> > Sent: Wednesday, 25 September 2024 10.00
> > 
> > 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 changing the type of the returned variable.
> > 
> > Signed-off-by: Robin Jarry <rjarry@redhat.com>
> > ---
> >  lib/mbuf/rte_mbuf.h | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
> > index babe16c72ccb..bab1fb94d41d 100644
> > --- a/lib/mbuf/rte_mbuf.h
> > +++ b/lib/mbuf/rte_mbuf.h
> > @@ -595,9 +595,9 @@ __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;
> > +	void *m;
> > 
> > -	if (rte_mempool_get(mp, (void **)&m) < 0)
> > +	if (rte_mempool_get(mp, &m) < 0)
> >  		return NULL;
> >  	__rte_mbuf_raw_sanity_check(m);
> >  	return m;  
> 
> Suggest:
> 	__rte_mbuf_raw_sanity_check((struct rte_mbuf *)m);
> 	return (struct rte_mbuf *)m;

Another way to avoid the warning would be to use a union?
  
Robin Jarry Sept. 25, 2024, 3:24 p.m. UTC | #3
Stephen Hemminger, Sep 25, 2024 at 11:23:
> On Wed, 25 Sep 2024 17:21:12 +0200
> Morten Brørup <mb@smartsharesystems.com> wrote:
>
> > From: Morten Brørup <mb@smartsharesystems.com>
> > To: "Robin Jarry" <rjarry@redhat.com>,  <dev@dpdk.org>
> > Subject: RE: [PATCH dpdk] mbuf: fix strict aliasing error in allocator
> > Date: Wed, 25 Sep 2024 17:21:12 +0200
> > 
> > > From: Robin Jarry [mailto:rjarry@redhat.com]
> > > Sent: Wednesday, 25 September 2024 10.00
> > > 
> > > 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 changing the type of the returned variable.
> > > 
> > > Signed-off-by: Robin Jarry <rjarry@redhat.com>
> > > ---
> > >  lib/mbuf/rte_mbuf.h | 4 ++--
> > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
> > > index babe16c72ccb..bab1fb94d41d 100644
> > > --- a/lib/mbuf/rte_mbuf.h
> > > +++ b/lib/mbuf/rte_mbuf.h
> > > @@ -595,9 +595,9 @@ __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;
> > > +	void *m;
> > > 
> > > -	if (rte_mempool_get(mp, (void **)&m) < 0)
> > > +	if (rte_mempool_get(mp, &m) < 0)
> > >  		return NULL;
> > >  	__rte_mbuf_raw_sanity_check(m);
> > >  	return m;  
> > 
> > Suggest:
> > 	__rte_mbuf_raw_sanity_check((struct rte_mbuf *)m);
> > 	return (struct rte_mbuf *)m;
>
> Another way to avoid the warning would be to use a union?

I can use an inline union, it will be less code.
  
Patrick Robb Sept. 26, 2024, 4:38 p.m. UTC | #4
Recheck-request: iol-mellanox-Performance

When I enabled this test on Monday I accidentally set the delta threshold
to 1.5% instead of 5%. So the accepted results window was too small and led
to this false positive test fail.
  

Patch

diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
index babe16c72ccb..bab1fb94d41d 100644
--- a/lib/mbuf/rte_mbuf.h
+++ b/lib/mbuf/rte_mbuf.h
@@ -595,9 +595,9 @@  __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;
+	void *m;
 
-	if (rte_mempool_get(mp, (void **)&m) < 0)
+	if (rte_mempool_get(mp, &m) < 0)
 		return NULL;
 	__rte_mbuf_raw_sanity_check(m);
 	return m;