2015-05-19 11:00:35

by Nicholas Mc Guire

[permalink] [raw]
Subject: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

API compliance scanning with coccinelle flagged:

Converting milliseconds to jiffies by "val * HZ / 1000" is technically
is not a clean solution as it does not handle all corner cases correctly.
By changing the conversion to use msecs_to_jiffies(val) conversion is
correct in all cases.

in the current code:
mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
for HZ < 100 (e.g. CONFIG_HZ == 64|32 in alpha) this effectively results
in no delay at all.

Patch was compile tested for x86_64_defconfig (implies CONFIG_USB_IRDA=m)

Patch is against 4.1-rc4 (localversion-next is -next-20150519)

Signed-off-by: Nicholas Mc Guire <[email protected]>
---
drivers/net/irda/irda-usb.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
index f6c9163..f3e4563 100644
--- a/drivers/net/irda/irda-usb.c
+++ b/drivers/net/irda/irda-usb.c
@@ -848,7 +848,9 @@ static void irda_usb_receive(struct urb *urb)
* Jean II */
self->rx_defer_timer.function = irda_usb_rx_defer_expired;
self->rx_defer_timer.data = (unsigned long) urb;
- mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
+ mod_timer(&self->rx_defer_timer,
+ jiffies + (msecs_to_jiffies(10)));
+
return;
}

--
1.7.10.4


2015-05-19 14:32:21

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

On Tue, 2015-05-19 at 12:51 +0200, Nicholas Mc Guire wrote:
> Converting milliseconds to jiffies by "val * HZ / 1000" is technically
> is not a clean solution as it does not handle all corner cases correctly.
> By changing the conversion to use msecs_to_jiffies(val) conversion is
> correct in all cases.
>
> in the current code:
> mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> for HZ < 100 (e.g. CONFIG_HZ == 64|32 in alpha) this effectively results
> in no delay at all.
[]
> diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
[]
> @@ -848,7 +848,9 @@ static void irda_usb_receive(struct urb *urb)
> * Jean II */
> self->rx_defer_timer.function = irda_usb_rx_defer_expired;
> self->rx_defer_timer.data = (unsigned long) urb;
> - mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> + mod_timer(&self->rx_defer_timer,
> + jiffies + (msecs_to_jiffies(10)));

The unnecessary () around msecs_to_jiffies could be removed.

2015-05-19 20:46:00

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

From: Joe Perches <[email protected]>
Date: Tue, 19 May 2015 07:32:15 -0700

> On Tue, 2015-05-19 at 12:51 +0200, Nicholas Mc Guire wrote:
>> Converting milliseconds to jiffies by "val * HZ / 1000" is technically
>> is not a clean solution as it does not handle all corner cases correctly.
>> By changing the conversion to use msecs_to_jiffies(val) conversion is
>> correct in all cases.
>>
>> in the current code:
>> mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
>> for HZ < 100 (e.g. CONFIG_HZ == 64|32 in alpha) this effectively results
>> in no delay at all.
> []
>> diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
> []
>> @@ -848,7 +848,9 @@ static void irda_usb_receive(struct urb *urb)
>> * Jean II */
>> self->rx_defer_timer.function = irda_usb_rx_defer_expired;
>> self->rx_defer_timer.data = (unsigned long) urb;
>> - mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
>> + mod_timer(&self->rx_defer_timer,
>> + jiffies + (msecs_to_jiffies(10)));
>
> The unnecessary () around msecs_to_jiffies could be removed.

Agreed.

2015-05-19 21:09:14

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

On Tue, 2015-05-19 at 16:45 -0400, David Miller wrote:
> From: Joe Perches <[email protected]>
> Date: Tue, 19 May 2015 07:32:15 -0700
>
> > On Tue, 2015-05-19 at 12:51 +0200, Nicholas Mc Guire wrote:
> >> Converting milliseconds to jiffies by "val * HZ / 1000" is technically
> >> is not a clean solution as it does not handle all corner cases correctly.
> >> By changing the conversion to use msecs_to_jiffies(val) conversion is
> >> correct in all cases.
> >>
> >> in the current code:
> >> mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> >> for HZ < 100 (e.g. CONFIG_HZ == 64|32 in alpha) this effectively results
> >> in no delay at all.
> > []
> >> diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
> > []
> >> @@ -848,7 +848,9 @@ static void irda_usb_receive(struct urb *urb)
> >> * Jean II */
> >> self->rx_defer_timer.function = irda_usb_rx_defer_expired;
> >> self->rx_defer_timer.data = (unsigned long) urb;
> >> - mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> >> + mod_timer(&self->rx_defer_timer,
> >> + jiffies + (msecs_to_jiffies(10)));
> >
> > The unnecessary () around msecs_to_jiffies could be removed.

The other thing that could be done is to use
max(1ul, msecs_to_jiffies())
so that there's always some delay even if HZ <= 50


2015-05-20 01:44:19

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

On Tue, May 19, 2015 at 02:09:07PM -0700, Joe Perches wrote:
> On Tue, 2015-05-19 at 16:45 -0400, David Miller wrote:
> > From: Joe Perches <[email protected]>
> > Date: Tue, 19 May 2015 07:32:15 -0700
> >
> > > On Tue, 2015-05-19 at 12:51 +0200, Nicholas Mc Guire wrote:
> > >> Converting milliseconds to jiffies by "val * HZ / 1000" is technically
> > >> is not a clean solution as it does not handle all corner cases correctly.
> > >> By changing the conversion to use msecs_to_jiffies(val) conversion is
> > >> correct in all cases.
> > >>
> > >> in the current code:
> > >> mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> > >> for HZ < 100 (e.g. CONFIG_HZ == 64|32 in alpha) this effectively results
> > >> in no delay at all.
> > > []
> > >> diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
> > > []
> > >> @@ -848,7 +848,9 @@ static void irda_usb_receive(struct urb *urb)
> > >> * Jean II */
> > >> self->rx_defer_timer.function = irda_usb_rx_defer_expired;
> > >> self->rx_defer_timer.data = (unsigned long) urb;
> > >> - mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
> > >> + mod_timer(&self->rx_defer_timer,
> > >> + jiffies + (msecs_to_jiffies(10)));
> > >
> > > The unnecessary () around msecs_to_jiffies could be removed.
>
> The other thing that could be done is to use
> max(1ul, msecs_to_jiffies())
> so that there's always some delay even if HZ <= 50
>
I may be mistaken, but I am quite sure that msecs_to_jiffies() always returns
at least 1 in such situations. Otherwise there would be lots of converstions
to 0 in the kernel.

Guenter

2015-05-20 03:57:23

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] irda: irda-usb: use msecs_to_jiffies for conversions

On Tue, 2015-05-19 at 18:44 -0700, Guenter Roeck wrote:
> On Tue, May 19, 2015 at 02:09:07PM -0700, Joe Perches wrote:
> > The other thing that could be done is to use
> > max(1ul, msecs_to_jiffies())
> > so that there's always some delay even if HZ <= 50
> >
> I may be mistaken, but I am quite sure that msecs_to_jiffies() always returns
> at least 1 in such situations. Otherwise there would be lots of conversions
> to 0 in the kernel.

Thanks Guenter

Nope, you're not mistaken.

Non 0 constants always return a value > 0

The existing uses like max(1ul, msecs_to_jiffies(<variable>))
just guard against <variable> being 0.