1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Read-Copy Update mechanism for mutual exclusion, adapted for tracing.
5 * Copyright (C) 2020 Paul E. McKenney.
8 #ifndef __LINUX_RCUPDATE_TRACE_H
9 #define __LINUX_RCUPDATE_TRACE_H
11 #include <linux/sched.h>
12 #include <linux/rcupdate.h>
13 #include <linux/cleanup.h>
15 extern struct lockdep_map rcu_trace_lock_map
;
17 #ifdef CONFIG_DEBUG_LOCK_ALLOC
19 static inline int rcu_read_lock_trace_held(void)
21 return lock_is_held(&rcu_trace_lock_map
);
24 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
26 static inline int rcu_read_lock_trace_held(void)
31 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
33 #ifdef CONFIG_TASKS_TRACE_RCU
35 void rcu_read_unlock_trace_special(struct task_struct
*t
);
38 * rcu_read_lock_trace - mark beginning of RCU-trace read-side critical section
40 * When synchronize_rcu_tasks_trace() is invoked by one task, then that
41 * task is guaranteed to block until all other tasks exit their read-side
42 * critical sections. Similarly, if call_rcu_trace() is invoked on one
43 * task while other tasks are within RCU read-side critical sections,
44 * invocation of the corresponding RCU callback is deferred until after
45 * the all the other tasks exit their critical sections.
47 * For more details, please see the documentation for rcu_read_lock().
49 static inline void rcu_read_lock_trace(void)
51 struct task_struct
*t
= current
;
53 WRITE_ONCE(t
->trc_reader_nesting
, READ_ONCE(t
->trc_reader_nesting
) + 1);
55 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB
) &&
56 t
->trc_reader_special
.b
.need_mb
)
57 smp_mb(); // Pairs with update-side barriers
58 rcu_lock_acquire(&rcu_trace_lock_map
);
62 * rcu_read_unlock_trace - mark end of RCU-trace read-side critical section
64 * Pairs with a preceding call to rcu_read_lock_trace(), and nesting is
65 * allowed. Invoking a rcu_read_unlock_trace() when there is no matching
66 * rcu_read_lock_trace() is verboten, and will result in lockdep complaints.
68 * For more details, please see the documentation for rcu_read_unlock().
70 static inline void rcu_read_unlock_trace(void)
73 struct task_struct
*t
= current
;
75 rcu_lock_release(&rcu_trace_lock_map
);
76 nesting
= READ_ONCE(t
->trc_reader_nesting
) - 1;
77 barrier(); // Critical section before disabling.
78 // Disable IPI-based setting of .need_qs.
79 WRITE_ONCE(t
->trc_reader_nesting
, INT_MIN
+ nesting
);
80 if (likely(!READ_ONCE(t
->trc_reader_special
.s
)) || nesting
) {
81 WRITE_ONCE(t
->trc_reader_nesting
, nesting
);
82 return; // We assume shallow reader nesting.
84 WARN_ON_ONCE(nesting
!= 0);
85 rcu_read_unlock_trace_special(t
);
88 void call_rcu_tasks_trace(struct rcu_head
*rhp
, rcu_callback_t func
);
89 void synchronize_rcu_tasks_trace(void);
90 void rcu_barrier_tasks_trace(void);
91 struct task_struct
*get_rcu_tasks_trace_gp_kthread(void);
94 * The BPF JIT forms these addresses even when it doesn't call these
95 * functions, so provide definitions that result in runtime errors.
97 static inline void call_rcu_tasks_trace(struct rcu_head
*rhp
, rcu_callback_t func
) { BUG(); }
98 static inline void rcu_read_lock_trace(void) { BUG(); }
99 static inline void rcu_read_unlock_trace(void) { BUG(); }
100 #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
102 DEFINE_LOCK_GUARD_0(rcu_tasks_trace
,
103 rcu_read_lock_trace(),
104 rcu_read_unlock_trace())
106 #endif /* __LINUX_RCUPDATE_TRACE_H */