1/* SPDX-License-Identifier: GPL-2.0+ */ 2/* 3 * Read-Copy Update mechanism for mutual exclusion, adapted for tracing. 4 * 5 * Copyright (C) 2020 Paul E. McKenney. 6 */ 7 8#ifndef __LINUX_RCUPDATE_TRACE_H 9#define __LINUX_RCUPDATE_TRACE_H 10 11#include <linux/sched.h> 12#include <linux/rcupdate.h> 13 14#ifdef CONFIG_DEBUG_LOCK_ALLOC 15 16extern struct lockdep_map rcu_trace_lock_map; 17 18static inline int rcu_read_lock_trace_held(void) 19{ 20 return lock_is_held(&rcu_trace_lock_map); 21} 22 23#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 24 25static inline int rcu_read_lock_trace_held(void) 26{ 27 return 1; 28} 29 30#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 31 32#ifdef CONFIG_TASKS_TRACE_RCU 33 34void rcu_read_unlock_trace_special(struct task_struct *t, int nesting); 35 36/** 37 * rcu_read_lock_trace - mark beginning of RCU-trace read-side critical section 38 * 39 * When synchronize_rcu_trace() is invoked by one task, then that task 40 * is guaranteed to block until all other tasks exit their read-side 41 * critical sections. Similarly, if call_rcu_trace() is invoked on one 42 * task while other tasks are within RCU read-side critical sections, 43 * invocation of the corresponding RCU callback is deferred until after 44 * the all the other tasks exit their critical sections. 45 * 46 * For more details, please see the documentation for rcu_read_lock(). 47 */ 48static inline void rcu_read_lock_trace(void) 49{ 50 struct task_struct *t = current; 51 52 WRITE_ONCE(t->trc_reader_nesting, READ_ONCE(t->trc_reader_nesting) + 1); 53 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && 54 t->trc_reader_special.b.need_mb) 55 smp_mb(); // Pairs with update-side barriers 56 rcu_lock_acquire(&rcu_trace_lock_map); 57} 58 59/** 60 * rcu_read_unlock_trace - mark end of RCU-trace read-side critical section 61 * 62 * Pairs with a preceding call to rcu_read_lock_trace(), and nesting is 63 * allowed. Invoking a rcu_read_unlock_trace() when there is no matching 64 * rcu_read_lock_trace() is verboten, and will result in lockdep complaints. 65 * 66 * For more details, please see the documentation for rcu_read_unlock(). 67 */ 68static inline void rcu_read_unlock_trace(void) 69{ 70 int nesting; 71 struct task_struct *t = current; 72 73 rcu_lock_release(&rcu_trace_lock_map); 74 nesting = READ_ONCE(t->trc_reader_nesting) - 1; 75 if (likely(!READ_ONCE(t->trc_reader_special.s)) || nesting) { 76 WRITE_ONCE(t->trc_reader_nesting, nesting); 77 return; // We assume shallow reader nesting. 78 } 79 rcu_read_unlock_trace_special(t, nesting); 80} 81 82void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func); 83void synchronize_rcu_tasks_trace(void); 84void rcu_barrier_tasks_trace(void); 85 86#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 87 88#endif /* __LINUX_RCUPDATE_TRACE_H */ 89