diff mbox series

[2/3] mbuf: avoid cast-align warning in pktmbuf mtod offset macro

Message ID 20210713064910.12793-3-elibr@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series Avoid cast-align warnings | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Eli Britstein July 13, 2021, 6:49 a.m. UTC
In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
't', which may cause cast-align warning when using gcc flags
'-Werror -Wcast-align':

.../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
    of target type [-Werror=cast-align]
  723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
      |   ^

As the code assumes correct alignment, add first a (void *) casting, to
avoid the warning.

Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org

Signed-off-by: Eli Britstein <elibr@nvidia.com>
---
 lib/mbuf/rte_mbuf_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Thomas Monjalon July 13, 2021, 7:43 a.m. UTC | #1
+Cc mbuf maintainers

Please use --cc-cmd devtools/get-maintainer.sh to make it automatic.


13/07/2021 08:49, Eli Britstein:
> In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
> 't', which may cause cast-align warning when using gcc flags
> '-Werror -Wcast-align':
> 
> .../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
>     of target type [-Werror=cast-align]
>   723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>       |   ^
> 
> As the code assumes correct alignment, add first a (void *) casting, to
> avoid the warning.
> 
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Eli Britstein <elibr@nvidia.com>
> ---
>  lib/mbuf/rte_mbuf_core.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
> index bb38d7f581..dabdeee604 100644
> --- a/lib/mbuf/rte_mbuf_core.h
> +++ b/lib/mbuf/rte_mbuf_core.h
> @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
>   *   The type to cast the result into.
>   */
>  #define rte_pktmbuf_mtod_offset(m, t, o)	\
> -	((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
> +	((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
>  
>  /**
>   * A macro that points to the start of the data in the mbuf.
>
Olivier Matz July 28, 2021, 3:28 p.m. UTC | #2
On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
> In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
> 't', which may cause cast-align warning when using gcc flags
> '-Werror -Wcast-align':
> 
> .../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
>     of target type [-Werror=cast-align]
>   723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>       |   ^
> 
> As the code assumes correct alignment, add first a (void *) casting, to
> avoid the warning.
> 
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Eli Britstein <elibr@nvidia.com>

My initial thinking was that it's the problem of the application: if
-Werror=cast-align is used, it is up to the application to cast the
return value of rte_pktmbuf_mtod_offset() to (void *) before casting it
to the network type.

But, if I understand correctly, the problem is not about the application
code itself, but about inlined code in the header files of dpdk
(i.e. compiling an empty C file that just includes the dpdk headers with
-Werror=cast-align). Is it correct? If yes I think it should be
highlighted in the commit log.

Out of curiosity, how did you find the errors? I mean, is it possible
that some casts are missing some other headers, or is this patchset
exhaustive?

Thanks,
Olivier


> ---
>  lib/mbuf/rte_mbuf_core.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
> index bb38d7f581..dabdeee604 100644
> --- a/lib/mbuf/rte_mbuf_core.h
> +++ b/lib/mbuf/rte_mbuf_core.h
> @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
>   *   The type to cast the result into.
>   */
>  #define rte_pktmbuf_mtod_offset(m, t, o)	\
> -	((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
> +	((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
>  
>  /**
>   * A macro that points to the start of the data in the mbuf.
> -- 
> 2.28.0.2311.g225365fb51
>
Eli Britstein July 29, 2021, 7:13 a.m. UTC | #3
On 7/28/2021 6:28 PM, Olivier Matz wrote:
> External email: Use caution opening links or attachments
>
>
> On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
>> In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
>> 't', which may cause cast-align warning when using gcc flags
>> '-Werror -Wcast-align':
>>
>> .../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
>>      of target type [-Werror=cast-align]
>>    723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>        |   ^
>>
>> As the code assumes correct alignment, add first a (void *) casting, to
>> avoid the warning.
>>
>> Fixes: af75078fece3 ("first public release")
>> Cc: stable@dpdk.org
>>
>> Signed-off-by: Eli Britstein <elibr@nvidia.com>
> My initial thinking was that it's the problem of the application: if
> -Werror=cast-align is used, it is up to the application to cast the
> return value of rte_pktmbuf_mtod_offset() to (void *) before casting it
> to the network type.
>
> But, if I understand correctly, the problem is not about the application
> code itself, but about inlined code in the header files of dpdk
> (i.e. compiling an empty C file that just includes the dpdk headers with
> -Werror=cast-align). Is it correct? If yes I think it should be
> highlighted in the commit log.

I think yes, though in this specific patch it is not even an inline 
function, but a macro.

However, I don't have a synthetic application example to show those 
warnings, thus didn't put such in the commit msg.

>
> Out of curiosity, how did you find the errors? I mean, is it possible
> that some casts are missing some other headers, or is this patchset
> exhaustive?
Currently OVS-DPDK is compiled only with -Wno-cast-align.

Following complaint that a recent commit introduced a degradation in OVS 
[1], I compiled OVS without this warning deprecation.
The fixes in OVS are [2] and [3] (already merged). The fixes in DPDK are 
in this patch-set.

[1] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/385084.html
[2] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386278.html
     e8cccd3a3589 ("netdev-offload-dpdk: Fix IPv6 rewrite cast-align 
warning.")
[3] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386279.html
     1f7f557603a5 ("netdev-offload-dpdk: Fix vxlan vni cast-align 
warnings.")
> Thanks,
> Olivier
>
>
>> ---
>>   lib/mbuf/rte_mbuf_core.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
>> index bb38d7f581..dabdeee604 100644
>> --- a/lib/mbuf/rte_mbuf_core.h
>> +++ b/lib/mbuf/rte_mbuf_core.h
>> @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
>>    *   The type to cast the result into.
>>    */
>>   #define rte_pktmbuf_mtod_offset(m, t, o)     \
>> -     ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>> +     ((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>
>>   /**
>>    * A macro that points to the start of the data in the mbuf.
>> --
>> 2.28.0.2311.g225365fb51
>>
Olivier Matz July 30, 2021, 11:10 a.m. UTC | #4
Hi Eli,

On Thu, Jul 29, 2021 at 10:13:45AM +0300, Eli Britstein wrote:
> 
> On 7/28/2021 6:28 PM, Olivier Matz wrote:
> > External email: Use caution opening links or attachments
> > 
> > 
> > On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
> > > In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
> > > 't', which may cause cast-align warning when using gcc flags
> > > '-Werror -Wcast-align':
> > > 
> > > .../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
> > >      of target type [-Werror=cast-align]
> > >    723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
> > >        |   ^
> > > 
> > > As the code assumes correct alignment, add first a (void *) casting, to
> > > avoid the warning.
> > > 
> > > Fixes: af75078fece3 ("first public release")
> > > Cc: stable@dpdk.org
> > > 
> > > Signed-off-by: Eli Britstein <elibr@nvidia.com>
> > My initial thinking was that it's the problem of the application: if
> > -Werror=cast-align is used, it is up to the application to cast the
> > return value of rte_pktmbuf_mtod_offset() to (void *) before casting it
> > to the network type.
> > 
> > But, if I understand correctly, the problem is not about the application
> > code itself, but about inlined code in the header files of dpdk
> > (i.e. compiling an empty C file that just includes the dpdk headers with
> > -Werror=cast-align). Is it correct? If yes I think it should be
> > highlighted in the commit log.
> 
> I think yes, though in this specific patch it is not even an inline
> function, but a macro.
> 
> However, I don't have a synthetic application example to show those
> warnings, thus didn't put such in the commit msg.

For this patch, I think it would be useful to have a way to reproduce
the issue first, so we can check whether it is the proper place to fix
the problem.

To me, it is assumed in the DPDK project that we can mmap a network
structure on mbuf data (maybe I'm wrong?). If an external application
like OVS wants to use -Werror=cast-align, it has to cast the result of
calls to rte_pktmbuf_mtod() family.

The only corner cases are DPDK header files which have static inline
functions or macro that forces the use of rte_pktmbuf_mtod() family
without a cast (like for your patch 1/3), because it cannot be fixed in
the external project.

I think we have to make our header files compliant to projects that want
to use -Werror=cast-align, like we do to make our header files compliant
to C++.

What you suggest in this patch forces the cast to (void *) for all users
of rte_pktmbuf_mtod() family. This could be a problem for projects that
want to see these warnings.

Would it be possible instead to add a cast in DPDK headers, in inline
functions that make use of these mtod functions?

Regards,
Olivier



> > 
> > Out of curiosity, how did you find the errors? I mean, is it possible
> > that some casts are missing some other headers, or is this patchset
> > exhaustive?
> Currently OVS-DPDK is compiled only with -Wno-cast-align.
> 
> Following complaint that a recent commit introduced a degradation in OVS
> [1], I compiled OVS without this warning deprecation.
> The fixes in OVS are [2] and [3] (already merged). The fixes in DPDK are in
> this patch-set.
> 
> [1] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/385084.html
> [2] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386278.html
>     e8cccd3a3589 ("netdev-offload-dpdk: Fix IPv6 rewrite cast-align
> warning.")
> [3] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386279.html
>     1f7f557603a5 ("netdev-offload-dpdk: Fix vxlan vni cast-align warnings.")
> > Thanks,
> > Olivier
> > 
> > 
> > > ---
> > >   lib/mbuf/rte_mbuf_core.h | 2 +-
> > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
> > > index bb38d7f581..dabdeee604 100644
> > > --- a/lib/mbuf/rte_mbuf_core.h
> > > +++ b/lib/mbuf/rte_mbuf_core.h
> > > @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
> > >    *   The type to cast the result into.
> > >    */
> > >   #define rte_pktmbuf_mtod_offset(m, t, o)     \
> > > -     ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
> > > +     ((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
> > > 
> > >   /**
> > >    * A macro that points to the start of the data in the mbuf.
> > > --
> > > 2.28.0.2311.g225365fb51
> > >
Eli Britstein Aug. 1, 2021, 8:06 a.m. UTC | #5
On 7/30/2021 2:10 PM, Olivier Matz wrote:
> External email: Use caution opening links or attachments
>
>
> Hi Eli,
>
> On Thu, Jul 29, 2021 at 10:13:45AM +0300, Eli Britstein wrote:
>> On 7/28/2021 6:28 PM, Olivier Matz wrote:
>>> External email: Use caution opening links or attachments
>>>
>>>
>>> On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
>>>> In rte_pktmbuf_mtod_offset macro, there is a casting from char * to type
>>>> 't', which may cause cast-align warning when using gcc flags
>>>> '-Werror -Wcast-align':
>>>>
>>>> .../include/rte_mbuf_core.h:723:3: error: cast increases required alignment
>>>>       of target type [-Werror=cast-align]
>>>>     723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>>         |   ^
>>>>
>>>> As the code assumes correct alignment, add first a (void *) casting, to
>>>> avoid the warning.
>>>>
>>>> Fixes: af75078fece3 ("first public release")
>>>> Cc: stable@dpdk.org
>>>>
>>>> Signed-off-by: Eli Britstein <elibr@nvidia.com>
>>> My initial thinking was that it's the problem of the application: if
>>> -Werror=cast-align is used, it is up to the application to cast the
>>> return value of rte_pktmbuf_mtod_offset() to (void *) before casting it
>>> to the network type.
>>>
>>> But, if I understand correctly, the problem is not about the application
>>> code itself, but about inlined code in the header files of dpdk
>>> (i.e. compiling an empty C file that just includes the dpdk headers with
>>> -Werror=cast-align). Is it correct? If yes I think it should be
>>> highlighted in the commit log.
>> I think yes, though in this specific patch it is not even an inline
>> function, but a macro.
>>
>> However, I don't have a synthetic application example to show those
>> warnings, thus didn't put such in the commit msg.
> For this patch, I think it would be useful to have a way to reproduce
> the issue first, so we can check whether it is the proper place to fix
> the problem.
--- a/examples/l2fwd/Makefile
+++ b/examples/l2fwd/Makefile
@@ -22,6 +22,7 @@ static: build/$(APP)-static
         ln -sf $(APP)-static build/$(APP)

  PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
+CFLAGS += -Wcast-align=strict
  CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)

gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

make -C examples/l2fwd clean static

>
> To me, it is assumed in the DPDK project that we can mmap a network
> structure on mbuf data (maybe I'm wrong?). If an external application
> like OVS wants to use -Werror=cast-align, it has to cast the result of
> calls to rte_pktmbuf_mtod() family.
>
> The only corner cases are DPDK header files which have static inline
> functions or macro that forces the use of rte_pktmbuf_mtod() family
> without a cast (like for your patch 1/3), because it cannot be fixed in
> the external project.
>
> I think we have to make our header files compliant to projects that want
> to use -Werror=cast-align, like we do to make our header files compliant
> to C++.
>
> What you suggest in this patch forces the cast to (void *) for all users
> of rte_pktmbuf_mtod() family. This could be a problem for projects that
> want to see these warnings.
>
> Would it be possible instead to add a cast in DPDK headers, in inline
> functions that make use of these mtod functions?
>
> Regards,
> Olivier
>
>
>
>>> Out of curiosity, how did you find the errors? I mean, is it possible
>>> that some casts are missing some other headers, or is this patchset
>>> exhaustive?
>> Currently OVS-DPDK is compiled only with -Wno-cast-align.
>>
>> Following complaint that a recent commit introduced a degradation in OVS
>> [1], I compiled OVS without this warning deprecation.
>> The fixes in OVS are [2] and [3] (already merged). The fixes in DPDK are in
>> this patch-set.
>>
>> [1] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/385084.html
>> [2] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386278.html
>>      e8cccd3a3589 ("netdev-offload-dpdk: Fix IPv6 rewrite cast-align
>> warning.")
>> [3] https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386279.html
>>      1f7f557603a5 ("netdev-offload-dpdk: Fix vxlan vni cast-align warnings.")
>>> Thanks,
>>> Olivier
>>>
>>>
>>>> ---
>>>>    lib/mbuf/rte_mbuf_core.h | 2 +-
>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
>>>> index bb38d7f581..dabdeee604 100644
>>>> --- a/lib/mbuf/rte_mbuf_core.h
>>>> +++ b/lib/mbuf/rte_mbuf_core.h
>>>> @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
>>>>     *   The type to cast the result into.
>>>>     */
>>>>    #define rte_pktmbuf_mtod_offset(m, t, o)     \
>>>> -     ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>> +     ((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>>
>>>>    /**
>>>>     * A macro that points to the start of the data in the mbuf.
>>>> --
>>>> 2.28.0.2311.g225365fb51
>>>>
Eli Britstein Oct. 19, 2021, 6:41 a.m. UTC | #6
Hi Olivier,

On 8/1/2021 11:06 AM, Eli Britstein wrote:
>
> On 7/30/2021 2:10 PM, Olivier Matz wrote:
>> External email: Use caution opening links or attachments
>>
>>
>> Hi Eli,
>>
>> On Thu, Jul 29, 2021 at 10:13:45AM +0300, Eli Britstein wrote:
>>> On 7/28/2021 6:28 PM, Olivier Matz wrote:
>>>> External email: Use caution opening links or attachments
>>>>
>>>>
>>>> On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
>>>>> In rte_pktmbuf_mtod_offset macro, there is a casting from char * 
>>>>> to type
>>>>> 't', which may cause cast-align warning when using gcc flags
>>>>> '-Werror -Wcast-align':
>>>>>
>>>>> .../include/rte_mbuf_core.h:723:3: error: cast increases required 
>>>>> alignment
>>>>>       of target type [-Werror=cast-align]
>>>>>     723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>>>         |   ^
>>>>>
>>>>> As the code assumes correct alignment, add first a (void *) 
>>>>> casting, to
>>>>> avoid the warning.
>>>>>
>>>>> Fixes: af75078fece3 ("first public release")
>>>>> Cc: stable@dpdk.org
>>>>>
>>>>> Signed-off-by: Eli Britstein <elibr@nvidia.com>
>>>> My initial thinking was that it's the problem of the application: if
>>>> -Werror=cast-align is used, it is up to the application to cast the
>>>> return value of rte_pktmbuf_mtod_offset() to (void *) before 
>>>> casting it
>>>> to the network type.
>>>>
>>>> But, if I understand correctly, the problem is not about the 
>>>> application
>>>> code itself, but about inlined code in the header files of dpdk
>>>> (i.e. compiling an empty C file that just includes the dpdk headers 
>>>> with
>>>> -Werror=cast-align). Is it correct? If yes I think it should be
>>>> highlighted in the commit log.
>>> I think yes, though in this specific patch it is not even an inline
>>> function, but a macro.
>>>
>>> However, I don't have a synthetic application example to show those
>>> warnings, thus didn't put such in the commit msg.
>> For this patch, I think it would be useful to have a way to reproduce
>> the issue first, so we can check whether it is the proper place to fix
>> the problem.
> --- a/examples/l2fwd/Makefile
> +++ b/examples/l2fwd/Makefile
> @@ -22,6 +22,7 @@ static: build/$(APP)-static
>         ln -sf $(APP)-static build/$(APP)
>
>  PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
> +CFLAGS += -Wcast-align=strict
>  CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
>
> gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
> Copyright (C) 2019 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
> PURPOSE.
>
> make -C examples/l2fwd clean static

To reproduce locally with DPDK only, no need to change any file. Only run:

CFLAGS="-Wcast-align=strict" make V=1 -C examples/l2fwd clean static

How would you like to proceed?

Thanks,

Eli

>
>>
>> To me, it is assumed in the DPDK project that we can mmap a network
>> structure on mbuf data (maybe I'm wrong?). If an external application
>> like OVS wants to use -Werror=cast-align, it has to cast the result of
>> calls to rte_pktmbuf_mtod() family.
>>
>> The only corner cases are DPDK header files which have static inline
>> functions or macro that forces the use of rte_pktmbuf_mtod() family
>> without a cast (like for your patch 1/3), because it cannot be fixed in
>> the external project.
>>
>> I think we have to make our header files compliant to projects that want
>> to use -Werror=cast-align, like we do to make our header files compliant
>> to C++.
>>
>> What you suggest in this patch forces the cast to (void *) for all users
>> of rte_pktmbuf_mtod() family. This could be a problem for projects that
>> want to see these warnings.
>>
>> Would it be possible instead to add a cast in DPDK headers, in inline
>> functions that make use of these mtod functions?
>>
>> Regards,
>> Olivier
>>
>>
>>
>>>> Out of curiosity, how did you find the errors? I mean, is it possible
>>>> that some casts are missing some other headers, or is this patchset
>>>> exhaustive?
>>> Currently OVS-DPDK is compiled only with -Wno-cast-align.
>>>
>>> Following complaint that a recent commit introduced a degradation in 
>>> OVS
>>> [1], I compiled OVS without this warning deprecation.
>>> The fixes in OVS are [2] and [3] (already merged). The fixes in DPDK 
>>> are in
>>> this patch-set.
>>>
>>> [1] 
>>> https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/385084.html
>>> [2] 
>>> https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386278.html
>>>      e8cccd3a3589 ("netdev-offload-dpdk: Fix IPv6 rewrite cast-align
>>> warning.")
>>> [3] 
>>> https://mail.openvswitch.org/pipermail/ovs-dev/2021-July/386279.html
>>>      1f7f557603a5 ("netdev-offload-dpdk: Fix vxlan vni cast-align 
>>> warnings.")
>>>> Thanks,
>>>> Olivier
>>>>
>>>>
>>>>> ---
>>>>>    lib/mbuf/rte_mbuf_core.h | 2 +-
>>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>>
>>>>> diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
>>>>> index bb38d7f581..dabdeee604 100644
>>>>> --- a/lib/mbuf/rte_mbuf_core.h
>>>>> +++ b/lib/mbuf/rte_mbuf_core.h
>>>>> @@ -720,7 +720,7 @@ struct rte_mbuf_ext_shared_info {
>>>>>     *   The type to cast the result into.
>>>>>     */
>>>>>    #define rte_pktmbuf_mtod_offset(m, t, o)     \
>>>>> -     ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>>> +     ((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
>>>>>
>>>>>    /**
>>>>>     * A macro that points to the start of the data in the mbuf.
>>>>> -- 
>>>>> 2.28.0.2311.g225365fb51
>>>>>
Olivier Matz Oct. 19, 2021, 9:47 a.m. UTC | #7
Hi Eli,

On Tue, Oct 19, 2021 at 09:41:56AM +0300, Eli Britstein wrote:
> Hi Olivier,
> 
> On 8/1/2021 11:06 AM, Eli Britstein wrote:
> > 
> > On 7/30/2021 2:10 PM, Olivier Matz wrote:
> > > External email: Use caution opening links or attachments
> > > 
> > > 
> > > Hi Eli,
> > > 
> > > On Thu, Jul 29, 2021 at 10:13:45AM +0300, Eli Britstein wrote:
> > > > On 7/28/2021 6:28 PM, Olivier Matz wrote:
> > > > > External email: Use caution opening links or attachments
> > > > > 
> > > > > 
> > > > > On Tue, Jul 13, 2021 at 09:49:09AM +0300, Eli Britstein wrote:
> > > > > > In rte_pktmbuf_mtod_offset macro, there is a casting
> > > > > > from char * to type
> > > > > > 't', which may cause cast-align warning when using gcc flags
> > > > > > '-Werror -Wcast-align':
> > > > > > 
> > > > > > .../include/rte_mbuf_core.h:723:3: error: cast increases
> > > > > > required alignment
> > > > > >       of target type [-Werror=cast-align]
> > > > > >     723 |  ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
> > > > > >         |   ^
> > > > > > 
> > > > > > As the code assumes correct alignment, add first a (void
> > > > > > *) casting, to
> > > > > > avoid the warning.
> > > > > > 
> > > > > > Fixes: af75078fece3 ("first public release")
> > > > > > Cc: stable@dpdk.org
> > > > > > 
> > > > > > Signed-off-by: Eli Britstein <elibr@nvidia.com>
> > > > > My initial thinking was that it's the problem of the application: if
> > > > > -Werror=cast-align is used, it is up to the application to cast the
> > > > > return value of rte_pktmbuf_mtod_offset() to (void *) before
> > > > > casting it
> > > > > to the network type.
> > > > > 
> > > > > But, if I understand correctly, the problem is not about the
> > > > > application
> > > > > code itself, but about inlined code in the header files of dpdk
> > > > > (i.e. compiling an empty C file that just includes the dpdk
> > > > > headers with
> > > > > -Werror=cast-align). Is it correct? If yes I think it should be
> > > > > highlighted in the commit log.
> > > > I think yes, though in this specific patch it is not even an inline
> > > > function, but a macro.
> > > > 
> > > > However, I don't have a synthetic application example to show those
> > > > warnings, thus didn't put such in the commit msg.
> > > For this patch, I think it would be useful to have a way to reproduce
> > > the issue first, so we can check whether it is the proper place to fix
> > > the problem.
> > --- a/examples/l2fwd/Makefile
> > +++ b/examples/l2fwd/Makefile
> > @@ -22,6 +22,7 @@ static: build/$(APP)-static
> >         ln -sf $(APP)-static build/$(APP)
> > 
> >  PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
> > +CFLAGS += -Wcast-align=strict
> >  CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
> > 
> > gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
> > Copyright (C) 2019 Free Software Foundation, Inc.
> > This is free software; see the source for copying conditions. There is NO
> > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
> > PURPOSE.
> > 
> > make -C examples/l2fwd clean static
> 
> To reproduce locally with DPDK only, no need to change any file. Only run:
> 
> CFLAGS="-Wcast-align=strict" make V=1 -C examples/l2fwd clean static
> 
> How would you like to proceed?

Sorry, I missed your previous message. I reproduced the issue, with
a slightly modified command:

  # no error, my gcc is 8.3.0-6 (debian)
  CFLAGS="-Wcast-align=strict" make V=1 -C examples/l2fwd clean static


  # bad option name with clang
  CC=clang CFLAGS="-Wcast-align=strict" make V=1 -C examples/l2fwd clean static
  ...
  warning: unknown warning option '-Wcast-align=strict'; did you mean '-Wcast-align'? [-Wunknown-warning-option]


  # problem reproduced with clang
  CC=clang CFLAGS="-Wcast-align" make V=1 -C examples/l2fwd clean static
  main.c:170:8: warning: cast from 'char *' to 'struct rte_ether_hdr *' increases required alignment from 1 to 2 [-Wcast-align]
          eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/local/include/rte_mbuf_core.h:830:32: note: expanded from macro 'rte_pktmbuf_mtod'
  #define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/local/include/rte_mbuf_core.h:816:3: note: expanded from macro 'rte_pktmbuf_mtod_offset'
          ((t)((char *)(m)->buf_addr + (m)->data_off + (o)))


I confirm the patch fixes the issue.

Acked-by: Olivier Matz <olivier.matz@6wind.com>

Thanks
diff mbox series

Patch

diff --git a/lib/mbuf/rte_mbuf_core.h b/lib/mbuf/rte_mbuf_core.h
index bb38d7f581..dabdeee604 100644
--- a/lib/mbuf/rte_mbuf_core.h
+++ b/lib/mbuf/rte_mbuf_core.h
@@ -720,7 +720,7 @@  struct rte_mbuf_ext_shared_info {
  *   The type to cast the result into.
  */
 #define rte_pktmbuf_mtod_offset(m, t, o)	\
-	((t)((char *)(m)->buf_addr + (m)->data_off + (o)))
+	((t)(void *)((char *)(m)->buf_addr + (m)->data_off + (o)))
 
 /**
  * A macro that points to the start of the data in the mbuf.