linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Need proper type casting before assignment, Remove compilation Warning.
@ 2016-07-07 17:01 Arvind Yadav
  2016-07-08 15:33 ` Guenter Roeck
  0 siblings, 1 reply; 7+ messages in thread
From: Arvind Yadav @ 2016-07-07 17:01 UTC (permalink / raw)
  To: scottwood; +Cc: qiang.zhao, linuxppc-dev, linux-kernel, Arvind Yadav

-Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
assigned in ucc_fast_tx_virtual_fifo_base_offset and
ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
So before assginment need a proper type casting.

-Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
into a function that takes an 'unsigned long' argument.This happens
to work because the type is sign-extended on 64-bit architectures
before it gets converted into an unsigned type.

-Passing an 'unsigned short' or 'unsigned int'argument into
IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
and types that are wider than 'unsigned long'.

-Any user will get compilation warning for that do not pass an
unsigned long' argument.

Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
---
 drivers/soc/fsl/qe/ucc_fast.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/soc/fsl/qe/ucc_fast.c b/drivers/soc/fsl/qe/ucc_fast.c
index a768931..98eed25 100644
--- a/drivers/soc/fsl/qe/ucc_fast.c
+++ b/drivers/soc/fsl/qe/ucc_fast.c
@@ -267,8 +267,10 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
 
 	/* Allocate memory for Tx Virtual Fifo */
 	uccf->ucc_fast_tx_virtual_fifo_base_offset =
-	    qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
-	if (IS_ERR_VALUE(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
+		(unsigned int)qe_muram_alloc(uf_info->utfs,
+				UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
+	if (IS_ERR_VALUE(
+		(unsigned long)uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
 		printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO\n",
 			__func__);
 		uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
@@ -278,10 +280,11 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
 
 	/* Allocate memory for Rx Virtual Fifo */
 	uccf->ucc_fast_rx_virtual_fifo_base_offset =
-		qe_muram_alloc(uf_info->urfs +
+		(unsigned int)qe_muram_alloc(uf_info->urfs +
 			   UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
 			   UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
-	if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
+	if (IS_ERR_VALUE(
+		(unsigned long)uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
 		printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO\n",
 			__func__);
 		uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: Need proper type casting before assignment, Remove compilation Warning.
  2016-07-07 17:01 [PATCH] Need proper type casting before assignment, Remove compilation Warning Arvind Yadav
@ 2016-07-08 15:33 ` Guenter Roeck
  2016-07-08 21:16   ` arvind Yadav
  2016-07-08 21:47   ` arvind Yadav
  0 siblings, 2 replies; 7+ messages in thread
From: Guenter Roeck @ 2016-07-08 15:33 UTC (permalink / raw)
  To: Arvind Yadav; +Cc: scottwood, qiang.zhao, linuxppc-dev, linux-kernel

On Thu, Jul 07, 2016 at 10:31:11PM +0530, Arvind Yadav wrote:
> -Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
> assigned in ucc_fast_tx_virtual_fifo_base_offset and
> ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
> So before assginment need a proper type casting.

Are they ? In the upstream kernel, they seem to be "u32".

> 
> -Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
> into a function that takes an 'unsigned long' argument.This happens
> to work because the type is sign-extended on 64-bit architectures
> before it gets converted into an unsigned type.
> 
Not really sure I understand if/how this applies to the patch in question.
I don't see an int passed to IS_ERR_VALUE(), I only see u32.

> -Passing an 'unsigned short' or 'unsigned int'argument into
> IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
> and types that are wider than 'unsigned long'.
> 
What does this have to do with this patch ?

> -Any user will get compilation warning for that do not pass an
> unsigned long' argument.
> 
Sure, but that doesn't mean that typecasting the parameter to unsigned long
does any good (other than hiding the real bug).

Your subject line still does not list the affected subsystem and/or driver.
Documentation/SubmittingPatches might give some hints about proper subject
lines, and looking at other patches applied to the same file(s) might help
as well.

Also, if you want someone to review your patches, it helps to Cc: that
someone.

> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
> ---
>  drivers/soc/fsl/qe/ucc_fast.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/soc/fsl/qe/ucc_fast.c b/drivers/soc/fsl/qe/ucc_fast.c
> index a768931..98eed25 100644
> --- a/drivers/soc/fsl/qe/ucc_fast.c
> +++ b/drivers/soc/fsl/qe/ucc_fast.c
> @@ -267,8 +267,10 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>  
>  	/* Allocate memory for Tx Virtual Fifo */
>  	uccf->ucc_fast_tx_virtual_fifo_base_offset =
> -	    qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
> -	if (IS_ERR_VALUE(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
> +		(unsigned int)qe_muram_alloc(uf_info->utfs,

I don't see the point of this typecast.

> +				UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
> +	if (IS_ERR_VALUE(
> +		(unsigned long)uccf->ucc_fast_tx_virtual_fifo_base_offset)) {

If sizeof(u32) == sizeof(unsigned long), this patch does not have an effect.
If sizeof(u32) < sizeof(unsigned long), it does not change anything, and the
resulting code is as wrong as it was before.

>  		printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO\n",
>  			__func__);
>  		uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
> @@ -278,10 +280,11 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>  
>  	/* Allocate memory for Rx Virtual Fifo */
>  	uccf->ucc_fast_rx_virtual_fifo_base_offset =
> -		qe_muram_alloc(uf_info->urfs +
> +		(unsigned int)qe_muram_alloc(uf_info->urfs +
>  			   UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
>  			   UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
> -	if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
> +	if (IS_ERR_VALUE(
> +		(unsigned long)uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
>  		printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO\n",
>  			__func__);
>  		uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Need proper type casting before assignment, Remove compilation Warning.
  2016-07-08 15:33 ` Guenter Roeck
@ 2016-07-08 21:16   ` arvind Yadav
  2016-07-08 21:47   ` arvind Yadav
  1 sibling, 0 replies; 7+ messages in thread
From: arvind Yadav @ 2016-07-08 21:16 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: scottwood, qiang.zhao, linuxppc-dev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 5166 bytes --]


As per you concern, I have submitted one more patch with some changes. 
Please review it.

Thanks,

On Friday 08 July 2016 09:03 PM, Guenter Roeck wrote:
> On Thu, Jul 07, 2016 at 10:31:11PM +0530, Arvind Yadav wrote:
>> -Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
>> assigned in ucc_fast_tx_virtual_fifo_base_offset and
>> ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
>> So before assginment need a proper type casting.
> Are they ? In the upstream kernel, they seem to be "u32".
Yes, I have changed as per you suggestion.
>
>> -Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
>> into a function that takes an 'unsigned long' argument.This happens
>> to work because the type is sign-extended on 64-bit architectures
>> before it gets converted into an unsigned type.
>>
> Not really sure I understand if/how this applies to the patch in question.
> I don't see an int passed to IS_ERR_VALUE(), I only see u32.
-Most users of IS_ERR_VALUE() in the kernel are wrong, as they
pass an 'unsigned int' into a function that takes an 'unsigned long'
argument. This happens to work because the type is sign-extended
on 64-bit architectures before it gets converted into an
unsigned type.

However, anything that passes an 'unsigned short' or 'unsigned int'
argument into IS_ERR_VALUE() is guaranteed to be broken, as are
8-bit integers and types that are wider than 'unsigned long'.
>> -Passing an 'unsigned short' or 'unsigned int'argument into
>> IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
>> and types that are wider than 'unsigned long'.
>>
> What does this have to do with this patch ?
Specific requirement of type casting for 64-bit architectures.

-Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
assigned in ucc_fast_tx_virtual_fifo_base_offset and
ucc_fast_rx_virtual_fifo_base_offset. It will work on 32-bit architectures
But data can be loss on 64-bit architectures if 'qe_muram_alloc' will return
greater then MAX value of 'unsigned int'.


-Passing value in IS_ERR_VALUE() is wrong, as they pass an 'unsigned int'
into a function, It will through this compilation warning.
"
  include/linux/err.h:21:49: warning: cast to pointer from integer of 
different size [-Wint-to-pointer-cast]
  #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= 
(unsigned long)-MAX_ERRNO)
                                                  ^
  include/linux/compiler.h:170:42: note: in definition of macro ‘unlikely’
  # define unlikely(x) __builtin_expect(!!(x), 0)
"
>> -Any user will get compilation warning for that do not pass an
>> unsigned long' argument.
>>
> Sure, but that doesn't mean that typecasting the parameter to unsigned long
> does any good (other than hiding the real bug).
>
> Your subject line still does not list the affected subsystem and/or driver.
> Documentation/SubmittingPatches might give some hints about proper subject
> lines, and looking at other patches applied to the same file(s) might help
> as well.
>
> Also, if you want someone to review your patches, it helps to Cc: that
> someone.
Thanks, For your suggestion.
>> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
>> ---
>>   drivers/soc/fsl/qe/ucc_fast.c | 11 +++++++----
>>   1 file changed, 7 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/soc/fsl/qe/ucc_fast.c b/drivers/soc/fsl/qe/ucc_fast.c
>> index a768931..98eed25 100644
>> --- a/drivers/soc/fsl/qe/ucc_fast.c
>> +++ b/drivers/soc/fsl/qe/ucc_fast.c
>> @@ -267,8 +267,10 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>>   
>>   	/* Allocate memory for Tx Virtual Fifo */
>>   	uccf->ucc_fast_tx_virtual_fifo_base_offset =
>> -	    qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> -	if (IS_ERR_VALUE(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
>> +		(unsigned int)qe_muram_alloc(uf_info->utfs,
> I don't see the point of this typecast.
>
>> +				UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> +	if (IS_ERR_VALUE(
>> +		(unsigned long)uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
> If sizeof(u32) == sizeof(unsigned long), this patch does not have an effect.
> If sizeof(u32) < sizeof(unsigned long), it does not change anything, and the
> resulting code is as wrong as it was before.
>
>>   		printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO\n",
>>   			__func__);
>>   		uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
>> @@ -278,10 +280,11 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>>   
>>   	/* Allocate memory for Rx Virtual Fifo */
>>   	uccf->ucc_fast_rx_virtual_fifo_base_offset =
>> -		qe_muram_alloc(uf_info->urfs +
>> +		(unsigned int)qe_muram_alloc(uf_info->urfs +
>>   			   UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
>>   			   UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> -	if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
>> +	if (IS_ERR_VALUE(
>> +		(unsigned long)uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
>>   		printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO\n",
>>   			__func__);
>>   		uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;


[-- Attachment #2: Type: text/html, Size: 7244 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Need proper type casting before assignment, Remove compilation Warning.
  2016-07-08 15:33 ` Guenter Roeck
  2016-07-08 21:16   ` arvind Yadav
@ 2016-07-08 21:47   ` arvind Yadav
  1 sibling, 0 replies; 7+ messages in thread
From: arvind Yadav @ 2016-07-08 21:47 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: scottwood, qiang.zhao, linuxppc-dev, linux-kernel

As per your concern, I have changed and submitted one more patch.

This answer of your all questions,
-Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
assigned in ucc_fast_tx_virtual_fifo_base_offset and
ucc_fast_rx_virtual_fifo_base_offset. It will work on 32-bit architectures
But data can be loss on 64-bit architectures if 'qe_muram_alloc' will return
greater then MAX value of 'unsigned int'.


-Passing value in IS_ERR_VALUE() is wrong, as they pass an 'unsigned int'
into a function, It will through this compilation warning.
"
  include/linux/err.h:21:49: warning: cast to pointer from integer of 
different size [-Wint-to-pointer-cast]
  #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= 
(unsigned long)-MAX_ERRNO)
                                                  ^
  include/linux/compiler.h:170:42: note: in definition of macro ‘unlikely’
  # define unlikely(x) __builtin_expect(!!(x), 0)
"

-Most users of IS_ERR_VALUE() in the kernel are wrong, as they
pass an 'unsigned int' into a function that takes an 'unsigned long'
argument. This happens to work because the type is sign-extended
on 64-bit architectures before it gets converted into an
unsigned type.

However, anything that passes an 'unsigned short' or 'unsigned int'
argument into IS_ERR_VALUE() is guaranteed to be broken, as are
8-bit integers and types that are wider than 'unsigned long'.


Thanks,
Arvind Yadav

On Friday 08 July 2016 09:03 PM, Guenter Roeck wrote:
> On Thu, Jul 07, 2016 at 10:31:11PM +0530, Arvind Yadav wrote:
>> -Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
>> assigned in ucc_fast_tx_virtual_fifo_base_offset and
>> ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
>> So before assginment need a proper type casting.
> Are they ? In the upstream kernel, they seem to be "u32".
> -Yes, I have changed as per you suggestion.
>> -Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
>> into a function that takes an 'unsigned long' argument.This happens
>> to work because the type is sign-extended on 64-bit architectures
>> before it gets converted into an unsigned type.
>>
> Not really sure I understand if/how this applies to the patch in question.
> I don't see an int passed to IS_ERR_VALUE(), I only see u32.
>
>> -Passing an 'unsigned short' or 'unsigned int'argument into
>> IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
>> and types that are wider than 'unsigned long'.
>>
> What does this have to do with this patch ?
>
>> -Any user will get compilation warning for that do not pass an
>> unsigned long' argument.
>>
> Sure, but that doesn't mean that typecasting the parameter to unsigned long
> does any good (other than hiding the real bug).
>
> Your subject line still does not list the affected subsystem and/or driver.
> Documentation/SubmittingPatches might give some hints about proper subject
> lines, and looking at other patches applied to the same file(s) might help
> as well.
>
> Also, if you want someone to review your patches, it helps to Cc: that
> someone.
>
>> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
>> ---
>>   drivers/soc/fsl/qe/ucc_fast.c | 11 +++++++----
>>   1 file changed, 7 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/soc/fsl/qe/ucc_fast.c b/drivers/soc/fsl/qe/ucc_fast.c
>> index a768931..98eed25 100644
>> --- a/drivers/soc/fsl/qe/ucc_fast.c
>> +++ b/drivers/soc/fsl/qe/ucc_fast.c
>> @@ -267,8 +267,10 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>>   
>>   	/* Allocate memory for Tx Virtual Fifo */
>>   	uccf->ucc_fast_tx_virtual_fifo_base_offset =
>> -	    qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> -	if (IS_ERR_VALUE(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
>> +		(unsigned int)qe_muram_alloc(uf_info->utfs,
> I don't see the point of this typecast.
>
>> +				UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> +	if (IS_ERR_VALUE(
>> +		(unsigned long)uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
> If sizeof(u32) == sizeof(unsigned long), this patch does not have an effect.
> If sizeof(u32) < sizeof(unsigned long), it does not change anything, and the
> resulting code is as wrong as it was before.
>
>>   		printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO\n",
>>   			__func__);
>>   		uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
>> @@ -278,10 +280,11 @@ int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** ucc
>>   
>>   	/* Allocate memory for Rx Virtual Fifo */
>>   	uccf->ucc_fast_rx_virtual_fifo_base_offset =
>> -		qe_muram_alloc(uf_info->urfs +
>> +		(unsigned int)qe_muram_alloc(uf_info->urfs +
>>   			   UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
>>   			   UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
>> -	if (IS_ERR_VALUE(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
>> +	if (IS_ERR_VALUE(
>> +		(unsigned long)uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
>>   		printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO\n",
>>   			__func__);
>>   		uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Need proper type casting before assignment, Remove compilation Warning.
  2016-07-07 18:37 [PATCH] " Arvind Yadav
  2016-07-08  9:59 ` David Laight
@ 2016-07-09 22:06 ` David Miller
  1 sibling, 0 replies; 7+ messages in thread
From: David Miller @ 2016-07-09 22:06 UTC (permalink / raw)
  To: arvind.yadav.cs; +Cc: leoli, netdev, linuxppc-dev, linux-kernel

From: Arvind Yadav <arvind.yadav.cs@gmail.com>
Date: Fri,  8 Jul 2016 00:07:54 +0530

> -Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
> assigned in ucc_fast_tx_virtual_fifo_base_offset and
> ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
> So before assginment need a proper type casting.
> 
> -Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
> into a function that takes an 'unsigned long' argument.This happens
> to work because the type is sign-extended on 64-bit architectures
> before it gets converted into an unsigned type.
> 
> -Passing an 'unsigned short' or 'unsigned int'argument into
> IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
> and types that are wider than 'unsigned long'.
> 
> -Any user will get compilation warning for that do not pass an
> unsigned long' argument.
> 
> Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>

Your subject line is improperly formed.

It must have the subsystem or driver name, followed by a colon ":"
and a space.  Such as:

	[PATCH] ucc_geth: Need proper type ...

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH] Need proper type casting before assignment, Remove compilation Warning.
  2016-07-07 18:37 [PATCH] " Arvind Yadav
@ 2016-07-08  9:59 ` David Laight
  2016-07-09 22:06 ` David Miller
  1 sibling, 0 replies; 7+ messages in thread
From: David Laight @ 2016-07-08  9:59 UTC (permalink / raw)
  To: 'Arvind Yadav', leoli; +Cc: netdev, linuxppc-dev, linux-kernel

From: Arvind Yadav
> Sent: 07 July 2016 19:38
> -Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
> assigned in ucc_fast_tx_virtual_fifo_base_offset and
> ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
> So before assginment need a proper type casting.

Are you sure, seems to me that the type of one of the fields is wrong.
The casts you are adding do not aid readability at all.

> -Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
> into a function that takes an 'unsigned long' argument.This happens
> to work because the type is sign-extended on 64-bit architectures
> before it gets converted into an unsigned type.
>=20
> -Passing an 'unsigned short' or 'unsigned int'argument into
> IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
> and types that are wider than 'unsigned long'.

Signed 8 and 16 bit values will be sign extended to 'int' before being
used in any arithmetic operation.
Unsigned ones get zero extended to 'int'.

That probably means you shouldn't be using IS_ERR_VALUE().

	David

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] Need proper type casting before assignment, Remove compilation Warning.
@ 2016-07-07 18:37 Arvind Yadav
  2016-07-08  9:59 ` David Laight
  2016-07-09 22:06 ` David Miller
  0 siblings, 2 replies; 7+ messages in thread
From: Arvind Yadav @ 2016-07-07 18:37 UTC (permalink / raw)
  To: leoli; +Cc: netdev, linuxppc-dev, linux-kernel, Arvind Yadav

-Return type of 'qe_muram_alloc' is 'unsigned long', That Was trying to
assigned in ucc_fast_tx_virtual_fifo_base_offset and
ucc_fast_rx_virtual_fifo_base_offset. These variable are 'unsigned int'.
So before assginment need a proper type casting.

-Passing value in IS_ERR_VALUE() is wrong, as they pass an 'int'
into a function that takes an 'unsigned long' argument.This happens
to work because the type is sign-extended on 64-bit architectures
before it gets converted into an unsigned type.

-Passing an 'unsigned short' or 'unsigned int'argument into
IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers
and types that are wider than 'unsigned long'.

-Any user will get compilation warning for that do not pass an
unsigned long' argument.

Signed-off-by: Arvind Yadav <arvind.yadav.cs@gmail.com>
---
 drivers/net/ethernet/freescale/ucc_geth.c | 71 +++++++++++++++++--------------
 1 file changed, 38 insertions(+), 33 deletions(-)

diff --git a/drivers/net/ethernet/freescale/ucc_geth.c b/drivers/net/ethernet/freescale/ucc_geth.c
index 5bf1ade..a5086b2 100644
--- a/drivers/net/ethernet/freescale/ucc_geth.c
+++ b/drivers/net/ethernet/freescale/ucc_geth.c
@@ -273,7 +273,7 @@ static int fill_init_enet_entries(struct ucc_geth_private *ugeth,
 				  unsigned int risc,
 				  int skip_page_for_first_entry)
 {
-	u32 init_enet_offset;
+	unsigned long init_enet_offset;
 	u8 i;
 	int snum;
 
@@ -297,8 +297,8 @@ static int fill_init_enet_entries(struct ucc_geth_private *ugeth,
 			}
 		}
 		*(p_start++) =
-		    ((u8) snum << ENET_INIT_PARAM_SNUM_SHIFT) | init_enet_offset
-		    | risc;
+			((u8)snum << ENET_INIT_PARAM_SNUM_SHIFT)
+			| (u32)init_enet_offset | risc;
 	}
 
 	return 0;
@@ -2232,9 +2232,10 @@ static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
 					align) & ~(align - 1));
 		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
 			ugeth->tx_bd_ring_offset[j] =
-			    qe_muram_alloc(length,
+			    (u32)qe_muram_alloc(length,
 					   UCC_GETH_TX_BD_RING_ALIGNMENT);
-			if (!IS_ERR_VALUE(ugeth->tx_bd_ring_offset[j]))
+			if (!IS_ERR_VALUE(
+				(unsigned long)ugeth->tx_bd_ring_offset[j]))
 				ugeth->p_tx_bd_ring[j] =
 				    (u8 __iomem *) qe_muram_addr(ugeth->
 							 tx_bd_ring_offset[j]);
@@ -2309,9 +2310,10 @@ static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth)
 					align) & ~(align - 1));
 		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
 			ugeth->rx_bd_ring_offset[j] =
-			    qe_muram_alloc(length,
+			    (u32)qe_muram_alloc(length,
 					   UCC_GETH_RX_BD_RING_ALIGNMENT);
-			if (!IS_ERR_VALUE(ugeth->rx_bd_ring_offset[j]))
+			if (!IS_ERR_VALUE(
+				(unsigned long)ugeth->rx_bd_ring_offset[j]))
 				ugeth->p_rx_bd_ring[j] =
 				    (u8 __iomem *) qe_muram_addr(ugeth->
 							 rx_bd_ring_offset[j]);
@@ -2367,7 +2369,8 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	struct ucc_geth __iomem *ug_regs;
 	int ret_val = -EINVAL;
 	u32 remoder = UCC_GETH_REMODER_INIT;
-	u32 init_enet_pram_offset, cecr_subblock, command;
+	u32 cecr_subblock, command;
+	unsigned long init_enet_pram_offset;
 	u32 ifstat, i, j, size, l2qt, l3qt;
 	u16 temoder = UCC_GETH_TEMODER_INIT;
 	u16 test;
@@ -2519,9 +2522,9 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* Tx global PRAM */
 	/* Allocate global tx parameter RAM page */
 	ugeth->tx_glbl_pram_offset =
-	    qe_muram_alloc(sizeof(struct ucc_geth_tx_global_pram),
+	    (u32)qe_muram_alloc(sizeof(struct ucc_geth_tx_global_pram),
 			   UCC_GETH_TX_GLOBAL_PRAM_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->tx_glbl_pram_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->tx_glbl_pram_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_tx_glbl_pram\n");
 		return -ENOMEM;
@@ -2537,11 +2540,11 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* TQPTR */
 	/* Size varies with number of Tx threads */
 	ugeth->thread_dat_tx_offset =
-	    qe_muram_alloc(numThreadsTxNumerical *
+	   (u32)qe_muram_alloc(numThreadsTxNumerical *
 			   sizeof(struct ucc_geth_thread_data_tx) +
 			   32 * (numThreadsTxNumerical == 1),
 			   UCC_GETH_THREAD_DATA_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->thread_dat_tx_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->thread_dat_tx_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_thread_data_tx\n");
 		return -ENOMEM;
@@ -2565,10 +2568,10 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* SQPTR */
 	/* Size varies with number of Tx queues */
 	ugeth->send_q_mem_reg_offset =
-	    qe_muram_alloc(ug_info->numQueuesTx *
-			   sizeof(struct ucc_geth_send_queue_qd),
+	    (u32)qe_muram_alloc(ug_info->numQueuesTx
+			   * sizeof(struct ucc_geth_send_queue_qd),
 			   UCC_GETH_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->send_q_mem_reg_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->send_q_mem_reg_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_send_q_mem_reg\n");
 		return -ENOMEM;
@@ -2607,9 +2610,9 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	if (ug_info->numQueuesTx > 1) {
 	/* scheduler exists only if more than 1 tx queue */
 		ugeth->scheduler_offset =
-		    qe_muram_alloc(sizeof(struct ucc_geth_scheduler),
+		    (u32)qe_muram_alloc(sizeof(struct ucc_geth_scheduler),
 				   UCC_GETH_SCHEDULER_ALIGNMENT);
-		if (IS_ERR_VALUE(ugeth->scheduler_offset)) {
+		if (IS_ERR_VALUE((unsigned long)ugeth->scheduler_offset)) {
 			if (netif_msg_ifup(ugeth))
 				pr_err("Can not allocate DPRAM memory for p_scheduler\n");
 			return -ENOMEM;
@@ -2653,10 +2656,11 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	if (ug_info->
 	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
 		ugeth->tx_fw_statistics_pram_offset =
-		    qe_muram_alloc(sizeof
+		    (u32)qe_muram_alloc(sizeof
 				   (struct ucc_geth_tx_firmware_statistics_pram),
 				   UCC_GETH_TX_STATISTICS_ALIGNMENT);
-		if (IS_ERR_VALUE(ugeth->tx_fw_statistics_pram_offset)) {
+		if (IS_ERR_VALUE(
+			(unsigned long)ugeth->tx_fw_statistics_pram_offset)) {
 			if (netif_msg_ifup(ugeth))
 				pr_err("Can not allocate DPRAM memory for p_tx_fw_statistics_pram\n");
 			return -ENOMEM;
@@ -2691,9 +2695,9 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* Rx global PRAM */
 	/* Allocate global rx parameter RAM page */
 	ugeth->rx_glbl_pram_offset =
-	    qe_muram_alloc(sizeof(struct ucc_geth_rx_global_pram),
+	    (u32)qe_muram_alloc(sizeof(struct ucc_geth_rx_global_pram),
 			   UCC_GETH_RX_GLOBAL_PRAM_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->rx_glbl_pram_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->rx_glbl_pram_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_rx_glbl_pram\n");
 		return -ENOMEM;
@@ -2709,10 +2713,10 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* RQPTR */
 	/* Size varies with number of Rx threads */
 	ugeth->thread_dat_rx_offset =
-	    qe_muram_alloc(numThreadsRxNumerical *
+	    (u32)qe_muram_alloc(numThreadsRxNumerical *
 			   sizeof(struct ucc_geth_thread_data_rx),
 			   UCC_GETH_THREAD_DATA_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->thread_dat_rx_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->thread_dat_rx_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_thread_data_rx\n");
 		return -ENOMEM;
@@ -2730,10 +2734,11 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	if (ug_info->
 	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
 		ugeth->rx_fw_statistics_pram_offset =
-		    qe_muram_alloc(sizeof
+		    (u32)qe_muram_alloc(sizeof
 				   (struct ucc_geth_rx_firmware_statistics_pram),
 				   UCC_GETH_RX_STATISTICS_ALIGNMENT);
-		if (IS_ERR_VALUE(ugeth->rx_fw_statistics_pram_offset)) {
+		if (IS_ERR_VALUE(
+			(unsigned long)ugeth->rx_fw_statistics_pram_offset)) {
 			if (netif_msg_ifup(ugeth))
 				pr_err("Can not allocate DPRAM memory for p_rx_fw_statistics_pram\n");
 			return -ENOMEM;
@@ -2750,10 +2755,10 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 
 	/* Size varies with number of Rx queues */
 	ugeth->rx_irq_coalescing_tbl_offset =
-	    qe_muram_alloc(ug_info->numQueuesRx *
-			   sizeof(struct ucc_geth_rx_interrupt_coalescing_entry)
-			   + 4, UCC_GETH_RX_INTERRUPT_COALESCING_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->rx_irq_coalescing_tbl_offset)) {
+	    (u32)qe_muram_alloc(ug_info->numQueuesRx
+			* sizeof(struct ucc_geth_rx_interrupt_coalescing_entry)
+			* + 4, UCC_GETH_RX_INTERRUPT_COALESCING_ALIGNMENT);
+	if (IS_ERR_VALUE((unsigned long)ugeth->rx_irq_coalescing_tbl_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_rx_irq_coalescing_tbl\n");
 		return -ENOMEM;
@@ -2815,11 +2820,11 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 	/* RBDQPTR */
 	/* Size varies with number of Rx queues */
 	ugeth->rx_bd_qs_tbl_offset =
-	    qe_muram_alloc(ug_info->numQueuesRx *
+	    (u32)qe_muram_alloc(ug_info->numQueuesRx *
 			   (sizeof(struct ucc_geth_rx_bd_queues_entry) +
 			    sizeof(struct ucc_geth_rx_prefetched_bds)),
 			   UCC_GETH_RX_BD_QUEUES_ALIGNMENT);
-	if (IS_ERR_VALUE(ugeth->rx_bd_qs_tbl_offset)) {
+	if (IS_ERR_VALUE((unsigned long)ugeth->rx_bd_qs_tbl_offset)) {
 		if (netif_msg_ifup(ugeth))
 			pr_err("Can not allocate DPRAM memory for p_rx_bd_qs_tbl\n");
 		return -ENOMEM;
@@ -2903,9 +2908,9 @@ static int ucc_geth_startup(struct ucc_geth_private *ugeth)
 		/* Allocate memory for extended filtering Mode Global
 		Parameters */
 		ugeth->exf_glbl_param_offset =
-		    qe_muram_alloc(sizeof(struct ucc_geth_exf_global_pram),
+		    (u32)qe_muram_alloc(sizeof(struct ucc_geth_exf_global_pram),
 		UCC_GETH_RX_EXTENDED_FILTERING_GLOBAL_PARAMETERS_ALIGNMENT);
-		if (IS_ERR_VALUE(ugeth->exf_glbl_param_offset)) {
+		if (IS_ERR_VALUE((unsigned long)ugeth->exf_glbl_param_offset)) {
 			if (netif_msg_ifup(ugeth))
 				pr_err("Can not allocate DPRAM memory for p_exf_glbl_param\n");
 			return -ENOMEM;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-07-09 22:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-07 17:01 [PATCH] Need proper type casting before assignment, Remove compilation Warning Arvind Yadav
2016-07-08 15:33 ` Guenter Roeck
2016-07-08 21:16   ` arvind Yadav
2016-07-08 21:47   ` arvind Yadav
2016-07-07 18:37 [PATCH] " Arvind Yadav
2016-07-08  9:59 ` David Laight
2016-07-09 22:06 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).