Skip to contentSkip to conversation

staging: rtl8712: usleep_range is preferred over udelay

PATCH Superseded Amitoj Kaur Chawla 3 participants 12 messages Oct 5, 2015 – Oct 9, 2015 Staging

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: rtl8712: usleep_range is preferred over udelay

Fix checkpatch.pl issue: "CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt".
Replace udelay() with a call to usleep_range() with a reasonable upper limit.

---
 drivers/staging/rtl8712/hal_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/rtl8712/hal_init.c +1 -1
index 469c566..897dc77 100644
@@ -237,7 +237,7 @@ static u8 rtl8712_dl_fw(struct _adapter *padapter)
 		i = 5;
 		tmp16 = r8712_read16(padapter, TCR);
 		while (((tmp16 & _EMEM_CODE_DONE) == 0) && (i > 0)) {
-			udelay(10);
+			usleep_range(10, 1000);
 			tmp16 = r8712_read16(padapter, TCR);
 			i--;
 		}
Signature · 1 line
1.9.1
#1 lore raw
Quoted 3 lines · Amitoj Kaur Chawla

Fix checkpatch.pl issue: "CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt".
Replace udelay() with a call to usleep_range() with a reasonable upper limit.

What is the definition of reasonable? Do maybe other similar drivers use these values?

julia

Quoted 29 lines
---
 drivers/staging/rtl8712/hal_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/rtl8712/hal_init.c +1 -2
index 469c566..897dc77 100644
@@ -237,7 +237,7 @@ static u8 rtl8712_dl_fw(struct _adapter *padapter)
 		i = 5;
 		tmp16 = r8712_read16(padapter, TCR);
 		while (((tmp16 & _EMEM_CODE_DONE) == 0) && (i > 0)) {
-			udelay(10);
+			usleep_range(10, 1000);
 			tmp16 = r8712_read16(padapter, TCR);
 			i--;
 		}
--

1.9.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20151005195012.GA21881%40amitoj-Inspiron-3542.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
Quoted 3 lines
Quoted 3 lines

Fix checkpatch.pl issue: "CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt".
Replace udelay() with a call to usleep_range() with a reasonable

upper limit.

Quoted 4 lines

What is the definition of reasonable? Do maybe other similar drivers use these values?

I checked over here
<https://git.kernel.org/cgit/linux/kernel/git/gregkh/staging.git/commit/?h=staging-testing&id=120bdac7376a36418eb1d55e0161dc0e660a45c3>
and here
<https://git.kernel.org/cgit/linux/kernel/git/gregkh/staging.git/commit/?h=staging-testing&id=3fe6929bbbca11b015d182d4864d92d0bf5c244d>.
They both used 1000. However, this
<https://git.kernel.org/cgit/linux/kernel/git/gregkh/staging.git/commit/?h=staging-testing&id=f98a20068dfcc16d1292d3e4d0d690e123da1538>
uses
20. But, the first two drivers seemed more similar to me. I couldn't find anything in the documentation specifying the selection of range.

Quoted 3 lines

julia

Quoted 1 line
#3 lore raw
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Fix checkpatch.pl issue: "CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt".
Replace udelay() with a call to usleep_range() with a reasonable

upper limit.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

What is the definition of reasonable?  Do maybe other similar drivers use these values?

I checked over here and here. They both used 1000. However, this uses 20. But, the first two drivers seemed more similar to me. I couldn't find anything in the documentation specifying the selection of range.

OK.

julia

Quoted 15 lines
Quoted 3 lines

julia

Quoted 1 line

--
You received this message because you are subscribed to the Google Groups
"outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/CA%2B5yK5Hh%3D0vgxfVb1Wz Xiy4w6z9GG9mJEHC21Bsvd9zdGzCJMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
Quoted 9 lines · On Tuesday 06 October 2015 08:12:58 Julia Lawall wrote:
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Fix checkpatch.pl issue: "CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt".
Replace udelay() with a call to usleep_range() with a reasonable

upper limit.

Quoted 4 lines

What is the definition of reasonable? Do maybe other similar drivers use these values?

Quoted 5 lines
Quoted 3 lines

I checked over here and here. They both used 1000. However, this uses 20. But, the first two drivers seemed more similar to me. I couldn't find anything in the documentation specifying the selection of range.

OK.

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

The questions to answer here are:

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

Arnd

#5 lore raw
Quoted 8 lines · Arnd Bergmann

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

Quoted 7 lines

The questions to answer here are:

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

There aren't any other sleeping functions in this function. However, udelay() has been used one more time in a loop.

Quoted 6 lines

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

How can I find out these 2 things? Only udelay() is the sleeping function.

Signature · 1 line
Amitoj
#6 lore raw
Quoted 11 lines · On Thursday 08 October 2015 08:23:10 Amitoj Kaur Chawla wrote:
Quoted 10 lines · Arnd Bergmann

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

The questions to answer here are:

Quoted 8 lines
Quoted 5 lines

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

There aren't any other sleeping functions in this function. However, udelay() has been used one more time in a loop.

udelay() is not a sleeping function, so that doesn't count here.

Try to look further out if you can't find a sleeping function or a spinlock here, scanning the callers of this function and everything they call for common sleeping functions like kmalloc(..., GFP_KERNEL), mutex_lock() or msleep().

Quoted 8 lines
Quoted 6 lines

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

How can I find out these 2 things? Only udelay() is the sleeping function.

Try understanding what the functions actually do by looking at the call chain for the first one, it should be fairly easy in this case.

For the second one, look for functions that get called from this one, if any of them contains a mdelay() or msleep(), or a loop around udelay(), try guessing how long that can take.

Arnd

#7 lore raw
Quoted 21 lines · Arnd Bergmann
Quoted 11 lines · On Thursday 08 October 2015 08:23:10 Amitoj Kaur Chawla wrote:
Quoted 10 lines · Arnd Bergmann

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

The questions to answer here are:

Quoted 8 lines
Quoted 5 lines

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

There aren't any other sleeping functions in this function. However, udelay() has been used one more time in a loop.

Quoted 6 lines

udelay() is not a sleeping function, so that doesn't count here.

Try to look further out if you can't find a sleeping function or a spinlock here, scanning the callers of this function and everything they call for common sleeping functions like kmalloc(..., GFP_KERNEL), mutex_lock() or msleep().

kmalloc(..., GFP_ATOMIC) is being called in the function. Callers of this function aren't calling these sleeping functions.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

Quoted 8 lines
Quoted 1 line

How can I find out these 2 things? Only udelay() is the sleeping function.

Try understanding what the functions actually do by looking at the call chain for the first one, it should be fairly easy in this case.

For the second one, look for functions that get called from this one, if any of them contains a mdelay() or msleep(), or a loop around udelay(), try guessing how long that can take.

The function is calling msleep(20) and has a loop around it, repeating hundred times so that means a couple of milliseconds delay, right? Is usleep_range() only used for short delays and msleep() for the longer ones?

Amitoj

#8 lore raw
Quoted 24 lines · On Saturday 10 October 2015 00:26:33 Amitoj Kaur Chawla wrote:
Quoted 23 lines · Arnd Bergmann
Quoted 11 lines · On Thursday 08 October 2015 08:23:10 Amitoj Kaur Chawla wrote:
Quoted 10 lines · Arnd Bergmann

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

The questions to answer here are:

Quoted 8 lines
Quoted 5 lines

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

There aren't any other sleeping functions in this function. However, udelay() has been used one more time in a loop.

udelay() is not a sleeping function, so that doesn't count here.

Quoted 7 lines
Quoted 4 lines

Try to look further out if you can't find a sleeping function or a spinlock here, scanning the callers of this function and everything they call for common sleeping functions like kmalloc(..., GFP_KERNEL), mutex_lock() or msleep().

kmalloc(..., GFP_ATOMIC) is being called in the function. Callers of this function aren't calling these sleeping functions.

Ah, interesting. Using kmalloc(..., GFP_ATOMIC) is usually a sign that the function is called in atomic context and is not allowed to sleep. However based on what you write below, I suspect that is actually a mistake and it should use GFP_KERNEL instead, because GFP_ATOMIC allocations are much more likely to fail.

Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

How can I find out these 2 things? Only udelay() is the sleeping function.

Try understanding what the functions actually do by looking at the call chain for the first one, it should be fairly easy in this case.

Quoted 6 lines
Quoted 3 lines

For the second one, look for functions that get called from this one, if any of them contains a mdelay() or msleep(), or a loop around udelay(), try guessing how long that can take.

The function is calling msleep(20) and has a loop around it, repeating hundred times so that means a couple of milliseconds delay, right?

This means the function can sleep up to 2 seconds, which is basically an eternity from a driver's perspective, so adding a few shorter sleep periods is certainly not harmful.

Quoted 2 lines

Is usleep_range() only used for short delays and msleep() for the longer ones?

Yes. There are other differences, but a main one is that msleep() can not sleep less than a millisecond, while usleep_range() can sleep much shorter periods.

Arnd

#9 lore raw
Quoted 62 lines · Arnd Bergmann
Quoted 24 lines · On Saturday 10 October 2015 00:26:33 Amitoj Kaur Chawla wrote:
Quoted 23 lines · Arnd Bergmann
Quoted 11 lines · On Thursday 08 October 2015 08:23:10 Amitoj Kaur Chawla wrote:
Quoted 10 lines · Arnd Bergmann

It really depends on what the driver is trying to do. In this case, it waits for a device register to signal 'done', and it tries this five times for a maximum of 50 microseconds, which is a relatively short period.

Changing the maximum to 1000 means that it can now sleep for up to 5 milliseconds instead, which is a noticeable time, especially if it gets called frequently.

The questions to answer here are:

Quoted 8 lines
Quoted 5 lines

* Is it safe to call a sleeping function from this point at all? Look for other sleeping functions (proves that it is allowed to sleep) and for spinlocks (if a lock is held, you may not sleep) in this function.

There aren't any other sleeping functions in this function. However, udelay() has been used one more time in a loop.

udelay() is not a sleeping function, so that doesn't count here.

Quoted 7 lines
Quoted 4 lines

Try to look further out if you can't find a sleeping function or a spinlock here, scanning the callers of this function and everything they call for common sleeping functions like kmalloc(..., GFP_KERNEL), mutex_lock() or msleep().

kmalloc(..., GFP_ATOMIC) is being called in the function. Callers of this function aren't calling these sleeping functions.

Ah, interesting. Using kmalloc(..., GFP_ATOMIC) is usually a sign that the function is called in atomic context and is not allowed to sleep. However based on what you write below, I suspect that is actually a mistake and it should use GFP_KERNEL instead, because GFP_ATOMIC allocations are much more likely to fail.

Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines

* How often do you expect the function to get called? Once per data packet means it's not ok to sleep long, once an hour would mean it's clearly ok.

* Are there other calls in this function that take longer than a millisecond?

How can I find out these 2 things? Only udelay() is the sleeping function.

Try understanding what the functions actually do by looking at the call chain for the first one, it should be fairly easy in this case.

Quoted 6 lines
Quoted 3 lines

For the second one, look for functions that get called from this one, if any of them contains a mdelay() or msleep(), or a loop around udelay(), try guessing how long that can take.

The function is calling msleep(20) and has a loop around it, repeating hundred times so that means a couple of milliseconds delay, right?

This means the function can sleep up to 2 seconds, which is basically an eternity from a driver's perspective, so adding a few shorter sleep periods is certainly not harmful.

Quoted 7 lines
Quoted 2 lines

Is usleep_range() only used for short delays and msleep() for the longer ones?

Yes. There are other differences, but a main one is that msleep() can not sleep less than a millisecond, while usleep_range() can sleep much shorter periods.

Conclusively, Shorter sleep periods means using usleep_range() instead of msleep() in this case with a small range since the loop is repeating a 100 times?

Amitoj

#10 lore raw
Quoted 8 lines · On Saturday 10 October 2015 00:41:37 Amitoj Kaur Chawla wrote:
Quoted 7 lines · Arnd Bergmann

This means the function can sleep up to 2 seconds, which is basically an eternity from a driver's perspective, so adding a few shorter sleep periods is certainly not harmful.

Quoted 2 lines

Is usleep_range() only used for short delays and msleep() for the longer ones?

Quoted 8 lines
Quoted 4 lines

Yes. There are other differences, but a main one is that msleep() can not sleep less than a millisecond, while usleep_range() can sleep much shorter periods.

Conclusively, Shorter sleep periods means using usleep_range() instead of msleep() in this case with a small range since the loop is repeating a 100 times?

That is a bit oversimplified. What we found so far is

- This is a function that is allowed to sleep. We know that because it calls msleep(). This means your original patch is good, but you should explain this in the changelog.

- you found another bug in the same function, which is allowed to sleep but calls kmalloc(..., GFP_ATOMIC) when it should use GFP_KERNEL.

- usleep_range(10, 1000) is a huge range (a factor of 100). This is ok here, because even the time spent in a 1000 microsecond wait is dwarfed by the 100 * 20ms wait time later in the function.

Other points that we have not talked about are:

- using a smaller range would also be ok, typically one would use a factor between 1.1 (+10%) and 10 (+1000%), but not a factor of 100.

- the function actually has two copies of the same loop with udelay() in it. If you change one of them, it would be logical to change both in the same patch.

- the loop you found below is interesting as well. Waiting 100 times for 20ms means we can actually wait even longer, as each 20ms interval can turn into 30 or 40 milliseconds. If we care about the actual maximum time, this could be improved by writing it as:

	unsigned long timeout = jiffies + 2 * HZ; /* up to two seconds */
	do {
		tmp16 = r8712_read16(padapter, TCR);
		if ((tmp16 & _DMEM_CODE_DONE)
			break;
		msleep(20);
	} while (time_before(timeout, jiffies));

We probably don't care about that here though.

Arnd

#11 lore raw
Quoted 18 lines · Arnd Bergmann
Quoted 8 lines · On Saturday 10 October 2015 00:41:37 Amitoj Kaur Chawla wrote:
Quoted 7 lines · Arnd Bergmann

This means the function can sleep up to 2 seconds, which is basically an eternity from a driver's perspective, so adding a few shorter sleep periods is certainly not harmful.

Quoted 2 lines

Is usleep_range() only used for short delays and msleep() for the longer ones?

Quoted 8 lines
Quoted 4 lines

Yes. There are other differences, but a main one is that msleep() can not sleep less than a millisecond, while usleep_range() can sleep much shorter periods.

Conclusively, Shorter sleep periods means using usleep_range() instead of msleep() in this case with a small range since the loop is repeating a 100 times?

Quoted 9 lines

That is a bit oversimplified. What we found so far is

- This is a function that is allowed to sleep. We know that because it calls msleep(). This means your original patch is good, but you should explain this in the changelog.

- you found another bug in the same function, which is allowed to sleep but calls kmalloc(..., GFP_ATOMIC) when it should use GFP_KERNEL.

Yes, I'll send a separate patch for this bug.

Quoted 4 lines

- usleep_range(10, 1000) is a huge range (a factor of 100). This is ok here, because even the time spent in a 1000 microsecond wait is dwarfed by the 100 * 20ms wait time later in the function.

Quoted 9 lines

Other points that we have not talked about are:

- using a smaller range would also be ok, typically one would use a factor between 1.1 (+10%) and 10 (+1000%), but not a factor of 100.

- the function actually has two copies of the same loop with udelay() in it. If you change one of them, it would be logical to change both in the same patch.

I'll send a v2 of this patch changing both udelay() functions.

Quoted 5 lines

- the loop you found below is interesting as well. Waiting 100 times for 20ms means we can actually wait even longer, as each 20ms interval can turn into 30 or 40 milliseconds. If we care about the actual maximum time, this could be improved by writing it as:

Quoted 8 lines
        unsigned long timeout = jiffies + 2 * HZ; /* up to two seconds */
        do {
                tmp16 = r8712_read16(padapter, TCR);
                if ((tmp16 & _DMEM_CODE_DONE)
                        break;
                msleep(20);
        } while (time_before(timeout, jiffies));

This piece of code is actually easier to understand than the original.

Quoted 2 lines

We probably don't care about that here though.

Thanks for all the feedback and patience. Things are way clearer now.

Signature · 1 line
Amitoj