diff mbox

pinctrl: nsp-gpio: fix type of iterator

Message ID 1450867041-729-2-git-send-email-a.hajda@samsung.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andrzej Hajda Dec. 23, 2015, 10:37 a.m. UTC
Iterator i declared as unsigned is always non-negative so the
loop will never end.

The problem has been detected using proposed semantic patch
scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci [1].

[1]: http://permalink.gmane.org/gmane.linux.kernel/2038576

Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
---
 drivers/pinctrl/bcm/pinctrl-nsp-gpio.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Ray Jui Dec. 23, 2015, 10:35 p.m. UTC | #1
+ Reddy

On 12/23/2015 2:37 AM, Andrzej Hajda wrote:
> Iterator i declared as unsigned is always non-negative so the
> loop will never end.
>
> The problem has been detected using proposed semantic patch
> scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci [1].
>
> [1]: http://permalink.gmane.org/gmane.linux.kernel/2038576
>
> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
> ---
>   drivers/pinctrl/bcm/pinctrl-nsp-gpio.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
> index 34648f6..ad5b04c 100644
> --- a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
> +++ b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
> @@ -439,7 +439,8 @@ static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
>   static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
>   				 u16 *strength)
>   {
> -	unsigned int i, offset, shift;
> +	unsigned int offset, shift;
> +	int i;
>   	u32 val;
>   	unsigned long flags;
>
>

The fix is a valid fix. And at the same time it exposes other potential 
issues in the driver. I just found the loop used in _set_strength and 
_get_strength is inconsistent:

In _set_strength:

427         for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {

For i to start at GPIO_DRV_STRENGTH_BITS, it seems to be wrong.

428                 val = readl(chip->io_ctrl + offset);
429                 val &= ~BIT(shift);
430                 val |= ((strength >> (i-1)) & 0x1) << shift;
431                 writel(val, chip->io_ctrl + offset);
432                 offset += 4;
433         }

In _get_strength:

451         for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
452                 val = readl(chip->io_ctrl + offset) & BIT(shift);
453                 val >>= shift;
454                 *strength += (val << i);
455                 offset += 4;
456         }

Reddy, could you please review and comment?

Thanks,

Ray
Yendapally Reddy Dhananjaya Reddy Dec. 24, 2015, 5:33 a.m. UTC | #2
On 12/24/2015 4:05 AM, Ray Jui wrote:
> + Reddy
>
> On 12/23/2015 2:37 AM, Andrzej Hajda wrote:
>> Iterator i declared as unsigned is always non-negative so the
>> loop will never end.
>>
>> The problem has been detected using proposed semantic patch
>> scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci [1].
>>
>> [1]: http://permalink.gmane.org/gmane.linux.kernel/2038576
>>
>> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>
>> ---
>>   drivers/pinctrl/bcm/pinctrl-nsp-gpio.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
>> index 34648f6..ad5b04c 100644
>> --- a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
>> +++ b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
>> @@ -439,7 +439,8 @@ static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
>>   static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
>>   				 u16 *strength)
>>   {
>> -	unsigned int i, offset, shift;
>> +	unsigned int offset, shift;
>> +	int i;
>>   	u32 val;
>>   	unsigned long flags;
>>
>>
> The fix is a valid fix. And at the same time it exposes other potential 
> issues in the driver. I just found the loop used in _set_strength and 
> _get_strength is inconsistent:
>
> In _set_strength:
>
> 427         for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {
>
> For i to start at GPIO_DRV_STRENGTH_BITS, it seems to be wrong.
>
> 428                 val = readl(chip->io_ctrl + offset);
> 429                 val &= ~BIT(shift);
> 430                 val |= ((strength >> (i-1)) & 0x1) << shift;
> 431                 writel(val, chip->io_ctrl + offset);
> 432                 offset += 4;
> 433         }
>
> In _get_strength:
>
> 451         for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
> 452                 val = readl(chip->io_ctrl + offset) & BIT(shift);
> 453                 val >>= shift;
> 454                 *strength += (val << i);
> 455                 offset += 4;
> 456         }
>
> Reddy, could you please review and comment?

Hi Ray,

             The logic is correct. The drive strength has three bits distributed in three registers. In one case the "-1" is in for loop initialization and the other case it is in the for loop body. The fix looks good.

Thanks
Dhananjay

> Thanks,
>
> Ray
>
>
Linus Walleij Dec. 24, 2015, 9:03 a.m. UTC | #3
On Wed, Dec 23, 2015 at 11:37 AM, Andrzej Hajda <a.hajda@samsung.com> wrote:

> Iterator i declared as unsigned is always non-negative so the
> loop will never end.
>
> The problem has been detected using proposed semantic patch
> scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci [1].
>
> [1]: http://permalink.gmane.org/gmane.linux.kernel/2038576
>
> Signed-off-by: Andrzej Hajda <a.hajda@samsung.com>

I already applied Dan Carpenters fix, somehow it was earlier
in my mail flow. Thanks anyways!

Yours,
Linus Walleij
diff mbox

Patch

diff --git a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
index 34648f6..ad5b04c 100644
--- a/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
+++ b/drivers/pinctrl/bcm/pinctrl-nsp-gpio.c
@@ -439,7 +439,8 @@  static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
 static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
 				 u16 *strength)
 {
-	unsigned int i, offset, shift;
+	unsigned int offset, shift;
+	int i;
 	u32 val;
 	unsigned long flags;