Skip to content

Commit a5c6234

Browse files
KAGA-KOKOPeter Zijlstra
authored andcommitted
completion: Use simple wait queues
completion uses a wait_queue_head_t to enqueue waiters. wait_queue_head_t contains a spinlock_t to protect the list of waiters which excludes it from being used in truly atomic context on a PREEMPT_RT enabled kernel. The spinlock in the wait queue head cannot be replaced by a raw_spinlock because: - wait queues can have custom wakeup callbacks, which acquire other spinlock_t locks and have potentially long execution times - wake_up() walks an unbounded number of list entries during the wake up and may wake an unbounded number of waiters. For simplicity and performance reasons complete() should be usable on PREEMPT_RT enabled kernels. completions do not use custom wakeup callbacks and are usually single waiter, except for a few corner cases. Replace the wait queue in the completion with a simple wait queue (swait), which uses a raw_spinlock_t for protecting the waiter list and therefore is safe to use inside truly atomic regions on PREEMPT_RT. There is no semantical or functional change: - completions use the exclusive wait mode which is what swait provides - complete() wakes one exclusive waiter - complete_all() wakes all waiters while holding the lock which protects the wait queue against newly incoming waiters. The conversion to swait preserves this behaviour. complete_all() might cause unbound latencies with a large number of waiters being woken at once, but most complete_all() usage sites are either in testing or initialization code or have only a really small number of concurrent waiters which for now does not cause a latency problem. Keep it simple for now. The fixup of the warning check in the USB gadget driver is just a straight forward conversion of the lockless waiter check from one waitqueue type to the other. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Davidlohr Bueso <dbueso@suse.de> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Link: https://lkml.kernel.org/r/20200321113242.317954042@linutronix.de
1 parent b3212fe commit a5c6234

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

drivers/usb/gadget/function/f_fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ static void ffs_data_put(struct ffs_data *ffs)
17031703
pr_info("%s(): freeing\n", __func__);
17041704
ffs_data_clear(ffs);
17051705
BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1706-
waitqueue_active(&ffs->ep0req_completion.wait) ||
1706+
swait_active(&ffs->ep0req_completion.wait) ||
17071707
waitqueue_active(&ffs->wait));
17081708
destroy_workqueue(ffs->io_completion_wq);
17091709
kfree(ffs->dev_name);

include/linux/completion.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* See kernel/sched/completion.c for details.
1010
*/
1111

12-
#include <linux/wait.h>
12+
#include <linux/swait.h>
1313

1414
/*
1515
* struct completion - structure used to maintain state for a "completion"
@@ -25,7 +25,7 @@
2525
*/
2626
struct completion {
2727
unsigned int done;
28-
wait_queue_head_t wait;
28+
struct swait_queue_head wait;
2929
};
3030

3131
#define init_completion_map(x, m) __init_completion(x)
@@ -34,7 +34,7 @@ static inline void complete_acquire(struct completion *x) {}
3434
static inline void complete_release(struct completion *x) {}
3535

3636
#define COMPLETION_INITIALIZER(work) \
37-
{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
37+
{ 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
3838

3939
#define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
4040
(*({ init_completion_map(&(work), &(map)); &(work); }))
@@ -85,7 +85,7 @@ static inline void complete_release(struct completion *x) {}
8585
static inline void __init_completion(struct completion *x)
8686
{
8787
x->done = 0;
88-
init_waitqueue_head(&x->wait);
88+
init_swait_queue_head(&x->wait);
8989
}
9090

9191
/**

kernel/sched/completion.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ void complete(struct completion *x)
2929
{
3030
unsigned long flags;
3131

32-
spin_lock_irqsave(&x->wait.lock, flags);
32+
raw_spin_lock_irqsave(&x->wait.lock, flags);
3333

3434
if (x->done != UINT_MAX)
3535
x->done++;
36-
__wake_up_locked(&x->wait, TASK_NORMAL, 1);
37-
spin_unlock_irqrestore(&x->wait.lock, flags);
36+
swake_up_locked(&x->wait);
37+
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
3838
}
3939
EXPORT_SYMBOL(complete);
4040

@@ -58,10 +58,12 @@ void complete_all(struct completion *x)
5858
{
5959
unsigned long flags;
6060

61-
spin_lock_irqsave(&x->wait.lock, flags);
61+
WARN_ON(irqs_disabled());
62+
63+
raw_spin_lock_irqsave(&x->wait.lock, flags);
6264
x->done = UINT_MAX;
63-
__wake_up_locked(&x->wait, TASK_NORMAL, 0);
64-
spin_unlock_irqrestore(&x->wait.lock, flags);
65+
swake_up_all_locked(&x->wait);
66+
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
6567
}
6668
EXPORT_SYMBOL(complete_all);
6769

@@ -70,20 +72,20 @@ do_wait_for_common(struct completion *x,
7072
long (*action)(long), long timeout, int state)
7173
{
7274
if (!x->done) {
73-
DECLARE_WAITQUEUE(wait, current);
75+
DECLARE_SWAITQUEUE(wait);
7476

75-
__add_wait_queue_entry_tail_exclusive(&x->wait, &wait);
7677
do {
7778
if (signal_pending_state(state, current)) {
7879
timeout = -ERESTARTSYS;
7980
break;
8081
}
82+
__prepare_to_swait(&x->wait, &wait);
8183
__set_current_state(state);
82-
spin_unlock_irq(&x->wait.lock);
84+
raw_spin_unlock_irq(&x->wait.lock);
8385
timeout = action(timeout);
84-
spin_lock_irq(&x->wait.lock);
86+
raw_spin_lock_irq(&x->wait.lock);
8587
} while (!x->done && timeout);
86-
__remove_wait_queue(&x->wait, &wait);
88+
__finish_swait(&x->wait, &wait);
8789
if (!x->done)
8890
return timeout;
8991
}
@@ -100,9 +102,9 @@ __wait_for_common(struct completion *x,
100102

101103
complete_acquire(x);
102104

103-
spin_lock_irq(&x->wait.lock);
105+
raw_spin_lock_irq(&x->wait.lock);
104106
timeout = do_wait_for_common(x, action, timeout, state);
105-
spin_unlock_irq(&x->wait.lock);
107+
raw_spin_unlock_irq(&x->wait.lock);
106108

107109
complete_release(x);
108110

@@ -291,12 +293,12 @@ bool try_wait_for_completion(struct completion *x)
291293
if (!READ_ONCE(x->done))
292294
return false;
293295

294-
spin_lock_irqsave(&x->wait.lock, flags);
296+
raw_spin_lock_irqsave(&x->wait.lock, flags);
295297
if (!x->done)
296298
ret = false;
297299
else if (x->done != UINT_MAX)
298300
x->done--;
299-
spin_unlock_irqrestore(&x->wait.lock, flags);
301+
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
300302
return ret;
301303
}
302304
EXPORT_SYMBOL(try_wait_for_completion);
@@ -322,8 +324,8 @@ bool completion_done(struct completion *x)
322324
* otherwise we can end up freeing the completion before complete()
323325
* is done referencing it.
324326
*/
325-
spin_lock_irqsave(&x->wait.lock, flags);
326-
spin_unlock_irqrestore(&x->wait.lock, flags);
327+
raw_spin_lock_irqsave(&x->wait.lock, flags);
328+
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
327329
return true;
328330
}
329331
EXPORT_SYMBOL(completion_done);

0 commit comments

Comments
 (0)