Re: [PATCH RT v2] Fix a lockup in wait_for_completion() and friends

From: Peter Zijlstra
Date: Tue May 14 2019 - 04:45:45 EST


On Thu, May 09, 2019 at 06:19:25PM +0200, Sebastian Andrzej Siewior wrote:
> On 2019-05-08 15:57:28 [-0500], minyard@xxxxxxx wrote:

> > kernel/sched/completion.c | 8 ++++----
> > 1 file changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
> > index 755a58084978..4f9b4cc0c95a 100644
> > --- a/kernel/sched/completion.c
> > +++ b/kernel/sched/completion.c
> > @@ -70,20 +70,20 @@ do_wait_for_common(struct completion *x,
> > long (*action)(long), long timeout, int state)
> > {
> > if (!x->done) {
> > - DECLARE_SWAITQUEUE(wait);
> > -
> > - __prepare_to_swait(&x->wait, &wait);
>
> you can keep DECLARE_SWAITQUEUE remove just __prepare_to_swait()
>
> > do {
> > + DECLARE_SWAITQUEUE(wait);
> > +
> > if (signal_pending_state(state, current)) {
> > timeout = -ERESTARTSYS;
> > break;
> > }
> > + __prepare_to_swait(&x->wait, &wait);
>
> add this, yes and you are done.
>
> > __set_current_state(state);
> > raw_spin_unlock_irq(&x->wait.lock);
> > timeout = action(timeout);
> > raw_spin_lock_irq(&x->wait.lock);
> > + __finish_swait(&x->wait, &wait);
> > } while (!x->done && timeout);
> > - __finish_swait(&x->wait, &wait);
> > if (!x->done)
> > return timeout;
> > }

Now.. that will fix it, but I think it is also wrong.

The problem being that it violates FIFO, something that might be more
important on -RT than elsewhere.

The regular wait API seems confused/inconsistent when it uses
autoremove_wake_function and default_wake_function, which doesn't help,
but we can easily support this with swait -- the problematic thing is
the custom wake functions, we musn't do that.

(also, mingo went and renamed a whole bunch of wait_* crap and didn't do
the same to swait_ so now its named all different :/)

Something like the below perhaps.

---
diff --git a/include/linux/swait.h b/include/linux/swait.h
index 73e06e9986d4..f194437ae7d2 100644
--- a/include/linux/swait.h
+++ b/include/linux/swait.h
@@ -61,11 +61,13 @@ struct swait_queue_head {
struct swait_queue {
struct task_struct *task;
struct list_head task_list;
+ unsigned int remove;
};

#define __SWAITQUEUE_INITIALIZER(name) { \
.task = current, \
.task_list = LIST_HEAD_INIT((name).task_list), \
+ .remove = 1, \
}

#define DECLARE_SWAITQUEUE(name) \
diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
index e83a3f8449f6..86974ecbabfc 100644
--- a/kernel/sched/swait.c
+++ b/kernel/sched/swait.c
@@ -28,7 +28,8 @@ void swake_up_locked(struct swait_queue_head *q)

curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
wake_up_process(curr->task);
- list_del_init(&curr->task_list);
+ if (curr->remove)
+ list_del_init(&curr->task_list);
}
EXPORT_SYMBOL(swake_up_locked);

@@ -57,7 +58,8 @@ void swake_up_all(struct swait_queue_head *q)
curr = list_first_entry(&tmp, typeof(*curr), task_list);

wake_up_state(curr->task, TASK_NORMAL);
- list_del_init(&curr->task_list);
+ if (curr->remove)
+ list_del_init(&curr->task_list);

if (list_empty(&tmp))
break;