[PATCH] sched: Make wakeup side variants of completion API irq safe (was: Re: spinlock in completion_done())

Rafael J. Wysocki
2009-12-12T23:07:30Z

On Thursday 10 December 2009, Ingo Molnar wrote:
>
> * Rafael J. Wysocki <rjw@sisk.pl> wrote:
>
> > On Wednesday 09 December 2009, Ingo Molnar wrote:
> > >
> > > * Rafael J. Wysocki <rjw@sisk.pl> wrote:
> > >
> > > > On Tuesday 08 December 2009, Alan Stern wrote:
> > > > > On Tue, 8 Dec 2009, Rafael J. Wysocki wrote:
> > > > >
> > > > > > BTW, is there a good reason why completion_done() doesn't use spin_lock_irqsave
> > > > > > and spin_unlock_irqrestore? complete() and complete_all() use them, so why not
> > > > > > here?
> > > > >
> > > > > And likewise in try_wait_for_completion(). It looks like a bug. Maybe
> > > > > these routines were not intended to be called with interrupts disabled,
> > > > > but that requirement doesn't seem to be documented. And it isn't a
> > > > > natural requirement anyway.
> > > >
> > > > OK, let's ask Ingo about that.
> > > >
> > > > Ingo, is there any particular reason why completion_done() and
> > > > try_wait_for_completion() don't use spin_lock_irqsave() and
> > > > spin_unlock_irqrestore()?
> > >
> > > that's a bug that should be fixed - all the wakeup side (and atomic)
> > > variants of completetion API should be irq safe.
> > >
> > > It appears that these new completion APIs were added via the XFS tree
> > > about a year ago:
> > >
> > > 39d2f1a: [XFS] extend completions to provide XFS object flush requirements
> > >
> > > Please Cc: scheduler folks to all scheduler patches.
> >
> > If you haven't fixed it locally yet, would you mind me posting a fix?
>
> I wouldnt mind it at all.

Is appended.

Thanks,
Rafael

---
From: Rafael J. Wysocki <rjw@sisk.pl>
Subject: sched: Make wakeup side variants of completion API irq safe

All the wakeup side variants of the completion API shoild be irq
safe, but completion_done() and try_wait_for_completion() aren't.
Fix the problem by making them use spin_lock_irqsave() and
spin_lock_irqrestore().

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
---
kernel/sched.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/sched.c
===================================================================
--- linux-2.6.orig/kernel/sched.c
+++ linux-2.6/kernel/sched.c
@@ -5931,14 +5931,15 @@ EXPORT_SYMBOL(wait_for_completion_killab
*/
bool try_wait_for_completion(struct completion *x)
{
+ unsigned long flags;
int ret = 1;

- spin_lock_irq(&x->wait.lock);
+ spin_lock_irqsave(&x->wait.lock, flags);
if (!x->done)
ret = 0;
else
x->done--;
- spin_unlock_irq(&x->wait.lock);
+ spin_unlock_irqrestore(&x->wait.lock, flags);
return ret;
}
EXPORT_SYMBOL(try_wait_for_completion);
@@ -5953,12 +5954,13 @@ EXPORT_SYMBOL(try_wait_for_completion);
*/
bool completion_done(struct completion *x)
{
+ unsigned long flags;
int ret = 1;

- spin_lock_irq(&x->wait.lock);
+ spin_lock_irqsave(&x->wait.lock, flags);
if (!x->done)
ret = 0;
- spin_unlock_irq(&x->wait.lock);
+ spin_unlock_irqrestore(&x->wait.lock, flags);
return ret;
}
EXPORT_SYMBOL(completion_done);