Skip to contentSkip to conversation

Change wait_for_completion_*_timeout to return a signed long

PATCH In review NeilBrown 4 participants 7 messages Jan 5, 2011 – Feb 15, 2011

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

PATCH 1/1 Change wait_for_completion_*_timeout to return a signed long

wait_for_completion_*_timeout can return:

0 if the wait timed out
-ve if the wait was interrupted
+ve if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
include/linux/completion.h +4 -4
index 36d57f7..142fe6f 100644
@@ -80,11 +80,11 @@ extern void wait_for_completion(struct completion *);
 extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 
kernel/sched.c +2 -2
index 297d1a0..8f35baf 100644
@@ -4526,7 +4526,7 @@ EXPORT_SYMBOL(wait_for_completion_interruptible);
  * This waits for either a completion of a specific task to be signaled or for a
  * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_interruptible_timeout(struct completion *x,
 					  unsigned long timeout)
 {
@@ -4559,7 +4559,7 @@ EXPORT_SYMBOL(wait_for_completion_killable);
  * signaled or for a specified timeout to expire. It can be
  * interrupted by a kill signal. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_killable_timeout(struct completion *x,
 				     unsigned long timeout)
 {
#1 lore raw

[tip:sched/core] sched: Change wait_for_completion_*_timeout() to return a signed long

Commit-ID:  6bf4123760a5aece6e4829ce90b70b6ffd751d65
Gitweb:     http://git.kernel.org/tip/6bf4123760a5aece6e4829ce90b70b6ffd751d65
Author:     NeilBrown <[email protected]>
AuthorDate: Wed, 5 Jan 2011 12:50:16 +1100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 5 Jan 2011 14:15:50 +0100

sched: Change wait_for_completion_*_timeout() to return a signed long

wait_for_completion_*_timeout() can return:

0: if the wait timed out
-ve: if the wait was interrupted
+ve: if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout() call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

Cc Peter Zijlstra <[email protected]>
Cc J. Bruce Fields <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>

LKML-Reference: <[email protected]>

---
 include/linux/completion.h |    8 ++++----
 kernel/sched.c             |    4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/completion.h +4 -4
index 36d57f7..51494e6 100644
@@ -81,10 +81,10 @@ extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 
kernel/sched.c +2 -2
index f2f914e..114a0de 100644
@@ -4395,7 +4395,7 @@ EXPORT_SYMBOL(wait_for_completion_interruptible);
  * This waits for either a completion of a specific task to be signaled or for a
  * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_interruptible_timeout(struct completion *x,
 					  unsigned long timeout)
 {
@@ -4428,7 +4428,7 @@ EXPORT_SYMBOL(wait_for_completion_killable);
  * signaled or for a specified timeout to expire. It can be
  * interrupted by a kill signal. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_killable_timeout(struct completion *x,
 				     unsigned long timeout)
 {
#2 lore raw

Re: [tip:sched/core] sched: Change wait_for_completion_*_timeout() to return a signed long

Doesn't this belong in 2.6.37.y as well?

--b.

Quoted 77 lines · tip-bot for NeilBrown
Commit-ID:  6bf4123760a5aece6e4829ce90b70b6ffd751d65
Gitweb:     http://git.kernel.org/tip/6bf4123760a5aece6e4829ce90b70b6ffd751d65
Author:     NeilBrown <[email protected]>
AuthorDate: Wed, 5 Jan 2011 12:50:16 +1100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 5 Jan 2011 14:15:50 +0100

sched: Change wait_for_completion_*_timeout() to return a signed long

wait_for_completion_*_timeout() can return:

0: if the wait timed out
-ve: if the wait was interrupted
+ve: if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout() call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

Cc Peter Zijlstra <[email protected]>
Cc J. Bruce Fields <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>

LKML-Reference: <[email protected]>

---
 include/linux/completion.h |    8 ++++----
 kernel/sched.c             |    4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/completion.h +4 -4
index 36d57f7..51494e6 100644
@@ -81,10 +81,10 @@ extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 
kernel/sched.c +2 -2
index f2f914e..114a0de 100644
@@ -4395,7 +4395,7 @@ EXPORT_SYMBOL(wait_for_completion_interruptible);
  * This waits for either a completion of a specific task to be signaled or for a
  * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_interruptible_timeout(struct completion *x,
 					  unsigned long timeout)
 {
@@ -4428,7 +4428,7 @@ EXPORT_SYMBOL(wait_for_completion_killable);
  * signaled or for a specified timeout to expire. It can be
  * interrupted by a kill signal. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_killable_timeout(struct completion *x,
 				     unsigned long timeout)
 {
author
#3 lore raw

Re: [tip:sched/core] sched: Change wait_for_completion_*_timeout() to return a signed long

Quoted 1 line · On Tue, 11 Jan 2011 16:44:58 -0500 "J. Bruce Fields" <[email protected]>…

Doesn't this belong in 2.6.37.y as well?

Yes indeed!
I should have tagged it for Cc:[email protected] in the first place. I had been meaning to forward it to [email protected], but you beat me to it!

Thanks,
NeilBrown

Quoted 81 lines

--b.

Quoted 77 lines · tip-bot for NeilBrown
Commit-ID:  6bf4123760a5aece6e4829ce90b70b6ffd751d65
Gitweb:     http://git.kernel.org/tip/6bf4123760a5aece6e4829ce90b70b6ffd751d65
Author:     NeilBrown <[email protected]>
AuthorDate: Wed, 5 Jan 2011 12:50:16 +1100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 5 Jan 2011 14:15:50 +0100

sched: Change wait_for_completion_*_timeout() to return a signed long

wait_for_completion_*_timeout() can return:

0: if the wait timed out
-ve: if the wait was interrupted
+ve: if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout() call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

Cc Peter Zijlstra <[email protected]>
Cc J. Bruce Fields <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>

LKML-Reference: <[email protected]>

---
 include/linux/completion.h |    8 ++++----
 kernel/sched.c             |    4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/completion.h +4 -4
index 36d57f7..51494e6 100644
@@ -81,10 +81,10 @@ extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 
kernel/sched.c +2 -2
index f2f914e..114a0de 100644
@@ -4395,7 +4395,7 @@ EXPORT_SYMBOL(wait_for_completion_interruptible);
  * This waits for either a completion of a specific task to be signaled or for a
  * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_interruptible_timeout(struct completion *x,
 					  unsigned long timeout)
 {
@@ -4428,7 +4428,7 @@ EXPORT_SYMBOL(wait_for_completion_killable);
  * signaled or for a specified timeout to expire. It can be
  * interrupted by a kill signal. The timeout is in jiffies.
  */
-unsigned long __sched
+long __sched
 wait_for_completion_killable_timeout(struct completion *x,
 				     unsigned long timeout)
 {
#4 lore raw

Re: [stable] [tip:sched/core] sched: Change wait_for_completion_*_timeout() to return a signed long

Quoted 8 lines · NeilBrown
Quoted 1 line · On Tue, 11 Jan 2011 16:44:58 -0500 "J. Bruce Fields" <[email protected]>…

Doesn't this belong in 2.6.37.y as well?

Yes indeed!
I should have tagged it for Cc:[email protected] in the first place. I had been meaning to forward it to [email protected], but you beat me to it!

Now applied, thanks.

greg k-h

#5 lore raw
In reply to NeilBrown
Quoted 25 lines · On Wed, 5 Jan 2011 12:50:16 +1100 NeilBrown <[email protected]> wrote:

wait_for_completion_*_timeout can return:

0 if the wait timed out
-ve if the wait was interrupted
+ve if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

...

include/linux/completion.h +0 -1
@@ -80,11 +80,11 @@ extern void wait_for_completion(struct completion *);
 extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(

include/linux/completion.h extern void wait_for_completion(struct completion *); · line 90

Quoted 10 lines
include/linux/completion.h +4 -3
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 

That's a half-patch, isn't it? wait_for_completion_interruptible() still incorrectly returns int' and wait_for_completion_timeout() still returns unsigned long'.

author
#6 lore raw
Quoted 34 lines · On Wed, 5 Jan 2011 14:57:25 -0800 Andrew Morton <[email protected]>…
Quoted 31 lines · On Wed, 5 Jan 2011 12:50:16 +1100 NeilBrown <[email protected]> wrote:

wait_for_completion_*_timeout can return:

0 if the wait timed out
-ve if the wait was interrupted
+ve if the completion was completed.

As they currently return an 'unsigned long', the last two cases are not easily distinguished which can easily result in buggy code, as is the case for the recently added wait_for_completion_interruptible_timeout call in net/sunrpc/cache.c

So change them both to return 'long'. As MAX_SCHEDULE_TIMEOUT is LONG_MAX, a large +ve return value should never overflow.

...

include/linux/completion.h +3 -4
@@ -80,11 +80,11 @@ extern void wait_for_completion(struct completion *);
 extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
-extern unsigned long wait_for_completion_interruptible_timeout(
-			struct completion *x, unsigned long timeout);
-extern unsigned long wait_for_completion_killable_timeout(
-			struct completion *x, unsigned long timeout);
+extern long wait_for_completion_interruptible_timeout(
+	struct completion *x, unsigned long timeout);
+extern long wait_for_completion_killable_timeout(
Quoted 8 lines

include/linux/completion.h extern void wait_for_completion(struct completion *); · line 90

Quoted 4 lines
include/linux/completion.h +1 -0
+	struct completion *x, unsigned long timeout);
 extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 

That's a half-patch, isn't it? wait_for_completion_interruptible() still incorrectly returns int' and wait_for_completion_timeout() still returns unsigned long'.

Half a patch is better than half a bug report is better than half an oops message is better than none ..... (am I showing my age?)

wait_for_completion_interruptile returns either a negative error (-ERESTART) or 0. So 'int' is fine. Ditto for wait_for_completion_killable. wait_for_completion_timeout returns a positive timeout-remaining or 0, so 'unsigned long' is fine.

It is only when we come to combining 'interruptible' and 'timeout' that we need signed and long.

Now maybe it would be good to use "signed long" for all of them because consistency is a nice thing. Or maybe it is best to specify in each case the return type that is most clearly meaningful because that communicates more clearly.
I really cannot say - I just wanted to fix a bug :-)

NeilBrown