blob: 61c56cca95c433167f334711a2e41c5be837729a [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_RCUWAIT_H_
3#define _LINUX_RCUWAIT_H_
4
5#include <linux/rcupdate.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02006#include <linux/sched/signal.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007
8/*
9 * rcuwait provides a way of blocking and waking up a single
David Brazdil0f672f62019-12-10 10:32:29 +000010 * task in an rcu-safe manner.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011 *
David Brazdil0f672f62019-12-10 10:32:29 +000012 * 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.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015 */
16struct rcuwait {
David Brazdil0f672f62019-12-10 10:32:29 +000017 struct task_struct __rcu *task;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018};
19
20#define __RCUWAIT_INITIALIZER(name) \
21 { .task = NULL, }
22
23static inline void rcuwait_init(struct rcuwait *w)
24{
25 w->task = NULL;
26}
27
Olivier Deprez157378f2022-04-04 15:47:50 +020028/*
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 */
32static inline int rcuwait_active(struct rcuwait *w)
33{
34 return !!rcu_access_pointer(w->task);
35}
36
37extern int rcuwait_wake_up(struct rcuwait *w);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038
39/*
40 * The caller is responsible for locking around rcuwait_wait_event(),
Olivier Deprez157378f2022-04-04 15:47:50 +020041 * and [prepare_to/finish]_rcuwait() such that writes to @task are
42 * properly serialized.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 */
Olivier Deprez157378f2022-04-04 15:47:50 +020044
45static inline void prepare_to_rcuwait(struct rcuwait *w)
46{
47 rcu_assign_pointer(w->task, current);
48}
49
50static 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) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057({ \
Olivier Deprez157378f2022-04-04 15:47:50 +020058 int __ret = 0; \
59 prepare_to_rcuwait(w); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060 for (;;) { \
61 /* \
62 * Implicit barrier (A) pairs with (B) in \
63 * rcuwait_wake_up(). \
64 */ \
Olivier Deprez157378f2022-04-04 15:47:50 +020065 set_current_state(state); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066 if (condition) \
67 break; \
68 \
Olivier Deprez157378f2022-04-04 15:47:50 +020069 if (signal_pending_state(state, current)) { \
70 __ret = -EINTR; \
71 break; \
72 } \
73 \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 schedule(); \
75 } \
Olivier Deprez157378f2022-04-04 15:47:50 +020076 finish_rcuwait(w); \
77 __ret; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078})
79
80#endif /* _LINUX_RCUWAIT_H_ */