Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 2 | #ifndef _LINUX_RCUWAIT_H_ |
| 3 | #define _LINUX_RCUWAIT_H_ |
| 4 | |
| 5 | #include <linux/rcupdate.h> |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 6 | #include <linux/sched/signal.h> |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * rcuwait provides a way of blocking and waking up a single |
Eric W. Biederman | 154abaf | 2019-09-14 07:34:30 -0500 | [diff] [blame] | 10 | * task in an rcu-safe manner. |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 11 | * |
Eric W. Biederman | 154abaf | 2019-09-14 07:34:30 -0500 | [diff] [blame] | 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. |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 15 | */ |
| 16 | struct rcuwait { |
Joel Fernandes (Google) | 03f4b48 | 2019-03-20 20:34:25 -0400 | [diff] [blame] | 17 | struct task_struct __rcu *task; |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | #define __RCUWAIT_INITIALIZER(name) \ |
| 21 | { .task = NULL, } |
| 22 | |
| 23 | static inline void rcuwait_init(struct rcuwait *w) |
| 24 | { |
| 25 | w->task = NULL; |
| 26 | } |
| 27 | |
| 28 | extern void rcuwait_wake_up(struct rcuwait *w); |
| 29 | |
| 30 | /* |
| 31 | * The caller is responsible for locking around rcuwait_wait_event(), |
| 32 | * such that writes to @task are properly serialized. |
| 33 | */ |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 34 | #define rcuwait_wait_event(w, condition, state) \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 35 | ({ \ |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 36 | int __ret = 0; \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 37 | rcu_assign_pointer((w)->task, current); \ |
| 38 | for (;;) { \ |
| 39 | /* \ |
| 40 | * Implicit barrier (A) pairs with (B) in \ |
Davidlohr Bueso | 7e1f946 | 2017-01-29 07:42:12 -0800 | [diff] [blame] | 41 | * rcuwait_wake_up(). \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 42 | */ \ |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 43 | set_current_state(state); \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 44 | if (condition) \ |
| 45 | break; \ |
| 46 | \ |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 47 | if (signal_pending_state(state, current)) { \ |
| 48 | __ret = -EINTR; \ |
| 49 | break; \ |
| 50 | } \ |
| 51 | \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 52 | schedule(); \ |
| 53 | } \ |
| 54 | \ |
| 55 | WRITE_ONCE((w)->task, NULL); \ |
| 56 | __set_current_state(TASK_RUNNING); \ |
Peter Zijlstra (Intel) | 80fbaf1 | 2020-03-21 12:25:55 +0100 | [diff] [blame] | 57 | __ret; \ |
Davidlohr Bueso | 8f95c90 | 2017-01-11 07:22:25 -0800 | [diff] [blame] | 58 | }) |
| 59 | |
| 60 | #endif /* _LINUX_RCUWAIT_H_ */ |