| .. | .. |
|---|
| 3 | 3 | #define _LINUX_RCUWAIT_H_ |
|---|
| 4 | 4 | |
|---|
| 5 | 5 | #include <linux/rcupdate.h> |
|---|
| 6 | +#include <linux/sched/signal.h> |
|---|
| 6 | 7 | |
|---|
| 7 | 8 | /* |
|---|
| 8 | 9 | * rcuwait provides a way of blocking and waking up a single |
|---|
| 9 | | - * task in an rcu-safe manner; where it is forbidden to use |
|---|
| 10 | | - * after exit_notify(). task_struct is not properly rcu protected, |
|---|
| 11 | | - * unless dealing with rcu-aware lists, ie: find_task_by_*(). |
|---|
| 10 | + * task in an rcu-safe manner. |
|---|
| 12 | 11 | * |
|---|
| 13 | | - * Alternatively we have task_rcu_dereference(), but the return |
|---|
| 14 | | - * semantics have different implications which would break the |
|---|
| 15 | | - * wakeup side. The only time @task is non-nil is when a user is |
|---|
| 16 | | - * blocked (or checking if it needs to) on a condition, and reset |
|---|
| 17 | | - * as soon as we know that the condition has succeeded and are |
|---|
| 18 | | - * awoken. |
|---|
| 12 | + * The only time @task is non-nil is when a user is blocked (or |
|---|
| 13 | + * checking if it needs to) on a condition, and reset as soon as we |
|---|
| 14 | + * know that the condition has succeeded and are awoken. |
|---|
| 19 | 15 | */ |
|---|
| 20 | 16 | struct rcuwait { |
|---|
| 21 | | - struct task_struct *task; |
|---|
| 17 | + struct task_struct __rcu *task; |
|---|
| 22 | 18 | }; |
|---|
| 23 | 19 | |
|---|
| 24 | 20 | #define __RCUWAIT_INITIALIZER(name) \ |
|---|
| .. | .. |
|---|
| 29 | 25 | w->task = NULL; |
|---|
| 30 | 26 | } |
|---|
| 31 | 27 | |
|---|
| 32 | | -extern void rcuwait_wake_up(struct rcuwait *w); |
|---|
| 28 | +/* |
|---|
| 29 | + * Note: this provides no serialization and, just as with waitqueues, |
|---|
| 30 | + * requires care to estimate as to whether or not the wait is active. |
|---|
| 31 | + */ |
|---|
| 32 | +static inline int rcuwait_active(struct rcuwait *w) |
|---|
| 33 | +{ |
|---|
| 34 | + return !!rcu_access_pointer(w->task); |
|---|
| 35 | +} |
|---|
| 36 | + |
|---|
| 37 | +extern int rcuwait_wake_up(struct rcuwait *w); |
|---|
| 33 | 38 | |
|---|
| 34 | 39 | /* |
|---|
| 35 | 40 | * The caller is responsible for locking around rcuwait_wait_event(), |
|---|
| 36 | | - * such that writes to @task are properly serialized. |
|---|
| 41 | + * and [prepare_to/finish]_rcuwait() such that writes to @task are |
|---|
| 42 | + * properly serialized. |
|---|
| 37 | 43 | */ |
|---|
| 38 | | -#define rcuwait_wait_event(w, condition) \ |
|---|
| 44 | + |
|---|
| 45 | +static inline void prepare_to_rcuwait(struct rcuwait *w) |
|---|
| 46 | +{ |
|---|
| 47 | + rcu_assign_pointer(w->task, current); |
|---|
| 48 | +} |
|---|
| 49 | + |
|---|
| 50 | +static inline void finish_rcuwait(struct rcuwait *w) |
|---|
| 51 | +{ |
|---|
| 52 | + rcu_assign_pointer(w->task, NULL); |
|---|
| 53 | + __set_current_state(TASK_RUNNING); |
|---|
| 54 | +} |
|---|
| 55 | + |
|---|
| 56 | +#define rcuwait_wait_event(w, condition, state) \ |
|---|
| 39 | 57 | ({ \ |
|---|
| 40 | | - /* \ |
|---|
| 41 | | - * Complain if we are called after do_exit()/exit_notify(), \ |
|---|
| 42 | | - * as we cannot rely on the rcu critical region for the \ |
|---|
| 43 | | - * wakeup side. \ |
|---|
| 44 | | - */ \ |
|---|
| 45 | | - WARN_ON(current->exit_state); \ |
|---|
| 46 | | - \ |
|---|
| 47 | | - rcu_assign_pointer((w)->task, current); \ |
|---|
| 58 | + int __ret = 0; \ |
|---|
| 59 | + prepare_to_rcuwait(w); \ |
|---|
| 48 | 60 | for (;;) { \ |
|---|
| 49 | 61 | /* \ |
|---|
| 50 | 62 | * Implicit barrier (A) pairs with (B) in \ |
|---|
| 51 | 63 | * rcuwait_wake_up(). \ |
|---|
| 52 | 64 | */ \ |
|---|
| 53 | | - set_current_state(TASK_UNINTERRUPTIBLE); \ |
|---|
| 65 | + set_current_state(state); \ |
|---|
| 54 | 66 | if (condition) \ |
|---|
| 55 | 67 | break; \ |
|---|
| 56 | 68 | \ |
|---|
| 69 | + if (signal_pending_state(state, current)) { \ |
|---|
| 70 | + __ret = -EINTR; \ |
|---|
| 71 | + break; \ |
|---|
| 72 | + } \ |
|---|
| 73 | + \ |
|---|
| 57 | 74 | schedule(); \ |
|---|
| 58 | 75 | } \ |
|---|
| 59 | | - \ |
|---|
| 60 | | - WRITE_ONCE((w)->task, NULL); \ |
|---|
| 61 | | - __set_current_state(TASK_RUNNING); \ |
|---|
| 76 | + finish_rcuwait(w); \ |
|---|
| 77 | + __ret; \ |
|---|
| 62 | 78 | }) |
|---|
| 63 | 79 | |
|---|
| 64 | 80 | #endif /* _LINUX_RCUWAIT_H_ */ |
|---|