diff mbox series

net/avf/base: fix shifting 32 bits signed variable 31 times

Message ID 20181028035133.67106-1-ferruh.yigit@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Ferruh Yigit
Headers show
Series net/avf/base: fix shifting 32 bits signed variable 31 times | expand

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Ferruh Yigit Oct. 28, 2018, 3:51 a.m. UTC
Fixes: e5b2a9e957e7 ("net/avf/base: add base code for avf PMD")
Cc: stable@dpdk.org

Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
---
 drivers/net/avf/base/avf_register.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Zhao1, Wei Oct. 31, 2018, 3:34 a.m. UTC | #1
> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Ferruh Yigit
> Sent: Sunday, October 28, 2018 11:52 AM
> To: Wu, Jingjing <jingjing.wu@intel.com>; Lu, Wenzhuo
> <wenzhuo.lu@intel.com>
> Cc: dev@dpdk.org; Yigit, Ferruh <ferruh.yigit@intel.com>; stable@dpdk.org
> Subject: [dpdk-dev] [PATCH] net/avf/base: fix shifting 32 bits signed variable
> 31 times
> 
> Fixes: e5b2a9e957e7 ("net/avf/base: add base code for avf PMD")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
> ---
>  drivers/net/avf/base/avf_register.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/avf/base/avf_register.h
> b/drivers/net/avf/base/avf_register.h
> index ba5a9f3fa..adb989583 100644
> --- a/drivers/net/avf/base/avf_register.h
> +++ b/drivers/net/avf/base/avf_register.h
> @@ -76,7 +76,7 @@ POSSIBILITY OF SUCH DAMAGE.
>  #define AVF_ARQLEN1_ARQCRIT_SHIFT   30
>  #define AVF_ARQLEN1_ARQCRIT_MASK    AVF_MASK(0x1,
> AVF_ARQLEN1_ARQCRIT_SHIFT)
>  #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
> -#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1,
> AVF_ARQLEN1_ARQENABLE_SHIFT)
> +#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1U,
> +AVF_ARQLEN1_ARQENABLE_SHIFT)
>  #define AVF_ARQT1            0x00007000 /* Reset: EMPR */
>  #define AVF_ARQT1_ARQT_SHIFT 0
>  #define AVF_ARQT1_ARQT_MASK  AVF_MASK(0x3FF,
> AVF_ARQT1_ARQT_SHIFT) @@ -99,7 +99,7 @@ POSSIBILITY OF SUCH
> DAMAGE.
>  #define AVF_ATQLEN1_ATQCRIT_SHIFT   30
>  #define AVF_ATQLEN1_ATQCRIT_MASK    AVF_MASK(0x1,
> AVF_ATQLEN1_ATQCRIT_SHIFT)
>  #define AVF_ATQLEN1_ATQENABLE_SHIFT 31
> -#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1,
> AVF_ATQLEN1_ATQENABLE_SHIFT)
> +#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1U,
> +AVF_ATQLEN1_ATQENABLE_SHIFT)
>  #define AVF_ATQT1            0x00008400 /* Reset: EMPR */
>  #define AVF_ATQT1_ATQT_SHIFT 0
>  #define AVF_ATQT1_ATQT_MASK  AVF_MASK(0x3FF,
> AVF_ATQT1_ATQT_SHIFT)
> --
> 2.17.2

I have test this code change with a small function to observe the difference.
Reviewed-by: Wei Zhao <wei.zhao1@intel.com>

#define AVF_ARQLEN1_ARQENABLE_SHIFT 31
#define AVF_MASK(mask, shift) (mask << shift)
#define AVF_ARQLEN1_ARQENABLE_MASK1  AVF_MASK(0x1, AVF_ARQLEN1_ARQENABLE_SHIFT)
#define AVF_ARQLEN1_ARQENABLE_MASK2  AVF_MASK(0x1U, AVF_ARQLEN1_ARQENABLE_SHIFT)

void main(int argc,char **argv)
{
        signed int a=1;

        printf("Hello Linux\n");
        printf("%x,=%x\n",AVF_ARQLEN1_ARQENABLE_MASK1,AVF_ARQLEN1_ARQENABLE_MASK2);
        printf("a=%x\n",a<<31);

}
Ferruh Yigit Nov. 5, 2018, 2:30 p.m. UTC | #2
On 10/31/2018 3:34 AM, Zhao1, Wei wrote:
> 
> 
>> -----Original Message-----
>> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Ferruh Yigit
>> Sent: Sunday, October 28, 2018 11:52 AM
>> To: Wu, Jingjing <jingjing.wu@intel.com>; Lu, Wenzhuo
>> <wenzhuo.lu@intel.com>
>> Cc: dev@dpdk.org; Yigit, Ferruh <ferruh.yigit@intel.com>; stable@dpdk.org
>> Subject: [dpdk-dev] [PATCH] net/avf/base: fix shifting 32 bits signed variable
>> 31 times
>>
>> Fixes: e5b2a9e957e7 ("net/avf/base: add base code for avf PMD")
>> Cc: stable@dpdk.org
>>
>> Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
>> ---
>>  drivers/net/avf/base/avf_register.h | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/avf/base/avf_register.h
>> b/drivers/net/avf/base/avf_register.h
>> index ba5a9f3fa..adb989583 100644
>> --- a/drivers/net/avf/base/avf_register.h
>> +++ b/drivers/net/avf/base/avf_register.h
>> @@ -76,7 +76,7 @@ POSSIBILITY OF SUCH DAMAGE.
>>  #define AVF_ARQLEN1_ARQCRIT_SHIFT   30
>>  #define AVF_ARQLEN1_ARQCRIT_MASK    AVF_MASK(0x1,
>> AVF_ARQLEN1_ARQCRIT_SHIFT)
>>  #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
>> -#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1,
>> AVF_ARQLEN1_ARQENABLE_SHIFT)
>> +#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1U,
>> +AVF_ARQLEN1_ARQENABLE_SHIFT)
>>  #define AVF_ARQT1            0x00007000 /* Reset: EMPR */
>>  #define AVF_ARQT1_ARQT_SHIFT 0
>>  #define AVF_ARQT1_ARQT_MASK  AVF_MASK(0x3FF,
>> AVF_ARQT1_ARQT_SHIFT) @@ -99,7 +99,7 @@ POSSIBILITY OF SUCH
>> DAMAGE.
>>  #define AVF_ATQLEN1_ATQCRIT_SHIFT   30
>>  #define AVF_ATQLEN1_ATQCRIT_MASK    AVF_MASK(0x1,
>> AVF_ATQLEN1_ATQCRIT_SHIFT)
>>  #define AVF_ATQLEN1_ATQENABLE_SHIFT 31
>> -#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1,
>> AVF_ATQLEN1_ATQENABLE_SHIFT)
>> +#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1U,
>> +AVF_ATQLEN1_ATQENABLE_SHIFT)
>>  #define AVF_ATQT1            0x00008400 /* Reset: EMPR */
>>  #define AVF_ATQT1_ATQT_SHIFT 0
>>  #define AVF_ATQT1_ATQT_MASK  AVF_MASK(0x3FF,
>> AVF_ATQT1_ATQT_SHIFT)
>> --
>> 2.17.2
> 
> I have test this code change with a small function to observe the difference.
> Reviewed-by: Wei Zhao <wei.zhao1@intel.com>
> 
> #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
> #define AVF_MASK(mask, shift) (mask << shift)
> #define AVF_ARQLEN1_ARQENABLE_MASK1  AVF_MASK(0x1, AVF_ARQLEN1_ARQENABLE_SHIFT)
> #define AVF_ARQLEN1_ARQENABLE_MASK2  AVF_MASK(0x1U, AVF_ARQLEN1_ARQENABLE_SHIFT)
> 
> void main(int argc,char **argv)
> {
>         signed int a=1;
> 
>         printf("Hello Linux\n");
>         printf("%x,=%x\n",AVF_ARQLEN1_ARQENABLE_MASK1,AVF_ARQLEN1_ARQENABLE_MASK2);
>         printf("a=%x\n",a<<31);
> 
> }

I guess you are tying to say there is no difference, output of your code is:
Hello Linux
80000000,=80000000
a=80000000


Let's have a little addition to your program,
 #include <stdio.h>

 #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
 #define AVF_MASK(mask, shift) (mask << shift)
 #define AVF_ARQLEN1_ARQENABLE_MASK1  AVF_MASK(0x1, AVF_ARQLEN1_ARQENABLE_SHIFT)
 #define AVF_ARQLEN1_ARQENABLE_MASK2  AVF_MASK(0x1U, AVF_ARQLEN1_ARQENABLE_SHIFT)
 #define AVF_MASK2(mask, shift) (mask >> shift)
 #define AVF_ARQLEN1_ARQENABLE_MASK11  AVF_MASK2(AVF_ARQLEN1_ARQENABLE_MASK1, 30)
 #define AVF_ARQLEN1_ARQENABLE_MASK22  AVF_MASK2(AVF_ARQLEN1_ARQENABLE_MASK2, 30)

 void main(int argc,char **argv)
 {
         signed int a=1;

         printf("Hello Linux\n");
         printf("%x,=%x\n", AVF_ARQLEN1_ARQENABLE_MASK1,
AVF_ARQLEN1_ARQENABLE_MASK2);
         printf("%x,=%x\n", AVF_ARQLEN1_ARQENABLE_MASK11,
AVF_ARQLEN1_ARQENABLE_MASK22);
         printf("a=%x\n", a<<31);

 }


Will results same for second line? Both does ((1 << 31) >> 30).


'1' is promoted to signed int by default and cppcheck complains that shifting
signed variable 31 times is undefined behaviour, and here intention is not
really have the 1 as signed integer, so better to fix it.

Thanks,
ferruh
Ferruh Yigit Nov. 5, 2018, 3:03 p.m. UTC | #3
On 10/31/2018 3:34 AM, Zhao1, Wei wrote:
> 
> 
>> -----Original Message-----
>> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Ferruh Yigit
>> Sent: Sunday, October 28, 2018 11:52 AM
>> To: Wu, Jingjing <jingjing.wu@intel.com>; Lu, Wenzhuo
>> <wenzhuo.lu@intel.com>
>> Cc: dev@dpdk.org; Yigit, Ferruh <ferruh.yigit@intel.com>; stable@dpdk.org
>> Subject: [dpdk-dev] [PATCH] net/avf/base: fix shifting 32 bits signed variable
>> 31 times
>>
>> Fixes: e5b2a9e957e7 ("net/avf/base: add base code for avf PMD")
>> Cc: stable@dpdk.org
>>
>> Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>

<...>

> Reviewed-by: Wei Zhao <wei.zhao1@intel.com>

Applied to dpdk-next-net/master, thanks.
Zhao1, Wei Nov. 7, 2018, 2:42 a.m. UTC | #4
> -----Original Message-----
> From: Yigit, Ferruh
> Sent: Monday, November 5, 2018 10:31 PM
> To: Zhao1, Wei <wei.zhao1@intel.com>; Wu, Jingjing
> <jingjing.wu@intel.com>; Lu, Wenzhuo <wenzhuo.lu@intel.com>
> Cc: dev@dpdk.org; stable@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH] net/avf/base: fix shifting 32 bits signed
> variable 31 times
> 
> On 10/31/2018 3:34 AM, Zhao1, Wei wrote:
> >
> >
> >> -----Original Message-----
> >> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Ferruh Yigit
> >> Sent: Sunday, October 28, 2018 11:52 AM
> >> To: Wu, Jingjing <jingjing.wu@intel.com>; Lu, Wenzhuo
> >> <wenzhuo.lu@intel.com>
> >> Cc: dev@dpdk.org; Yigit, Ferruh <ferruh.yigit@intel.com>;
> >> stable@dpdk.org
> >> Subject: [dpdk-dev] [PATCH] net/avf/base: fix shifting 32 bits signed
> >> variable
> >> 31 times
> >>
> >> Fixes: e5b2a9e957e7 ("net/avf/base: add base code for avf PMD")
> >> Cc: stable@dpdk.org
> >>
> >> Signed-off-by: Ferruh Yigit <ferruh.yigit@intel.com>
> >> ---
> >>  drivers/net/avf/base/avf_register.h | 4 ++--
> >>  1 file changed, 2 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/drivers/net/avf/base/avf_register.h
> >> b/drivers/net/avf/base/avf_register.h
> >> index ba5a9f3fa..adb989583 100644
> >> --- a/drivers/net/avf/base/avf_register.h
> >> +++ b/drivers/net/avf/base/avf_register.h
> >> @@ -76,7 +76,7 @@ POSSIBILITY OF SUCH DAMAGE.
> >>  #define AVF_ARQLEN1_ARQCRIT_SHIFT   30
> >>  #define AVF_ARQLEN1_ARQCRIT_MASK    AVF_MASK(0x1,
> >> AVF_ARQLEN1_ARQCRIT_SHIFT)
> >>  #define AVF_ARQLEN1_ARQENABLE_SHIFT 31 -#define
> >> AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1,
> >> AVF_ARQLEN1_ARQENABLE_SHIFT)
> >> +#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1U,
> >> +AVF_ARQLEN1_ARQENABLE_SHIFT)
> >>  #define AVF_ARQT1            0x00007000 /* Reset: EMPR */
> >>  #define AVF_ARQT1_ARQT_SHIFT 0
> >>  #define AVF_ARQT1_ARQT_MASK  AVF_MASK(0x3FF,
> >> AVF_ARQT1_ARQT_SHIFT) @@ -99,7 +99,7 @@ POSSIBILITY OF SUCH
> DAMAGE.
> >>  #define AVF_ATQLEN1_ATQCRIT_SHIFT   30
> >>  #define AVF_ATQLEN1_ATQCRIT_MASK    AVF_MASK(0x1,
> >> AVF_ATQLEN1_ATQCRIT_SHIFT)
> >>  #define AVF_ATQLEN1_ATQENABLE_SHIFT 31 -#define
> >> AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1,
> >> AVF_ATQLEN1_ATQENABLE_SHIFT)
> >> +#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1U,
> >> +AVF_ATQLEN1_ATQENABLE_SHIFT)
> >>  #define AVF_ATQT1            0x00008400 /* Reset: EMPR */
> >>  #define AVF_ATQT1_ATQT_SHIFT 0
> >>  #define AVF_ATQT1_ATQT_MASK  AVF_MASK(0x3FF,
> >> AVF_ATQT1_ATQT_SHIFT)
> >> --
> >> 2.17.2
> >
> > I have test this code change with a small function to observe the difference.
> > Reviewed-by: Wei Zhao <wei.zhao1@intel.com>
> >
> > #define AVF_ARQLEN1_ARQENABLE_SHIFT 31 #define AVF_MASK(mask,
> shift)
> > (mask << shift) #define AVF_ARQLEN1_ARQENABLE_MASK1
> AVF_MASK(0x1,
> > AVF_ARQLEN1_ARQENABLE_SHIFT) #define
> AVF_ARQLEN1_ARQENABLE_MASK2
> > AVF_MASK(0x1U, AVF_ARQLEN1_ARQENABLE_SHIFT)
> >
> > void main(int argc,char **argv)
> > {
> >         signed int a=1;
> >
> >         printf("Hello Linux\n");
> >
> printf("%x,=%x\n",AVF_ARQLEN1_ARQENABLE_MASK1,AVF_ARQLEN1_ARQ
> ENABLE_MASK2);
> >         printf("a=%x\n",a<<31);
> >
> > }
> 
> I guess you are tying to say there is no difference, output of your code is:
> Hello Linux
> 80000000,=80000000
> a=80000000
> 
> 
> Let's have a little addition to your program,  #include <stdio.h>
> 
>  #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
>  #define AVF_MASK(mask, shift) (mask << shift)  #define
> AVF_ARQLEN1_ARQENABLE_MASK1  AVF_MASK(0x1,
> AVF_ARQLEN1_ARQENABLE_SHIFT)  #define
> AVF_ARQLEN1_ARQENABLE_MASK2  AVF_MASK(0x1U,
> AVF_ARQLEN1_ARQENABLE_SHIFT)  #define AVF_MASK2(mask, shift)
> (mask >> shift)  #define AVF_ARQLEN1_ARQENABLE_MASK11
> AVF_MASK2(AVF_ARQLEN1_ARQENABLE_MASK1, 30)  #define
> AVF_ARQLEN1_ARQENABLE_MASK22
> AVF_MASK2(AVF_ARQLEN1_ARQENABLE_MASK2, 30)
> 
>  void main(int argc,char **argv)
>  {
>          signed int a=1;
> 
>          printf("Hello Linux\n");
>          printf("%x,=%x\n", AVF_ARQLEN1_ARQENABLE_MASK1,
> AVF_ARQLEN1_ARQENABLE_MASK2);
>          printf("%x,=%x\n", AVF_ARQLEN1_ARQENABLE_MASK11,
> AVF_ARQLEN1_ARQENABLE_MASK22);
>          printf("a=%x\n", a<<31);
> 
>  }
> 
> 
> Will results same for second line? Both does ((1 << 31) >> 30).
> 
> 
> '1' is promoted to signed int by default and cppcheck complains that shifting
> signed variable 31 times is undefined behaviour, and here intention is not
> really have the 1 as signed integer, so better to fix it.
> 


Acked-by: Wei Zhao <wei.zhao1@intel.com>

> Thanks,
> ferruh
diff mbox series

Patch

diff --git a/drivers/net/avf/base/avf_register.h b/drivers/net/avf/base/avf_register.h
index ba5a9f3fa..adb989583 100644
--- a/drivers/net/avf/base/avf_register.h
+++ b/drivers/net/avf/base/avf_register.h
@@ -76,7 +76,7 @@  POSSIBILITY OF SUCH DAMAGE.
 #define AVF_ARQLEN1_ARQCRIT_SHIFT   30
 #define AVF_ARQLEN1_ARQCRIT_MASK    AVF_MASK(0x1, AVF_ARQLEN1_ARQCRIT_SHIFT)
 #define AVF_ARQLEN1_ARQENABLE_SHIFT 31
-#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1, AVF_ARQLEN1_ARQENABLE_SHIFT)
+#define AVF_ARQLEN1_ARQENABLE_MASK  AVF_MASK(0x1U, AVF_ARQLEN1_ARQENABLE_SHIFT)
 #define AVF_ARQT1            0x00007000 /* Reset: EMPR */
 #define AVF_ARQT1_ARQT_SHIFT 0
 #define AVF_ARQT1_ARQT_MASK  AVF_MASK(0x3FF, AVF_ARQT1_ARQT_SHIFT)
@@ -99,7 +99,7 @@  POSSIBILITY OF SUCH DAMAGE.
 #define AVF_ATQLEN1_ATQCRIT_SHIFT   30
 #define AVF_ATQLEN1_ATQCRIT_MASK    AVF_MASK(0x1, AVF_ATQLEN1_ATQCRIT_SHIFT)
 #define AVF_ATQLEN1_ATQENABLE_SHIFT 31
-#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1, AVF_ATQLEN1_ATQENABLE_SHIFT)
+#define AVF_ATQLEN1_ATQENABLE_MASK  AVF_MASK(0x1U, AVF_ATQLEN1_ATQENABLE_SHIFT)
 #define AVF_ATQT1            0x00008400 /* Reset: EMPR */
 #define AVF_ATQT1_ATQT_SHIFT 0
 #define AVF_ATQT1_ATQT_MASK  AVF_MASK(0x3FF, AVF_ATQT1_ATQT_SHIFT)