173604da5SPaul E. McKenney/* SPDX-License-Identifier: GPL-2.0+ */
21da177e4SLinus Torvalds/*
3a71fca58SPaul E. McKenney * Read-Copy Update mechanism for mutual exclusion
41da177e4SLinus Torvalds *
501c1c660SPaul E. McKenney * Copyright IBM Corporation, 2001
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Author: Dipankar Sarma <dipankar@in.ibm.com>
8a71fca58SPaul E. McKenney *
973604da5SPaul E. McKenney * Based on the original work by Paul McKenney <paulmck@vnet.ibm.com>
101da177e4SLinus Torvalds * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
111da177e4SLinus Torvalds * Papers:
121da177e4SLinus Torvalds * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
131da177e4SLinus Torvalds * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * For detailed explanation of Read-Copy Update mechanism see -
16a71fca58SPaul E. McKenney *		http://lse.sourceforge.net/locking/rcupdate.html
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds#ifndef __LINUX_RCUPDATE_H
211da177e4SLinus Torvalds#define __LINUX_RCUPDATE_H
221da177e4SLinus Torvalds
2399098751SPaul E. McKenney#include <linux/types.h>
24ca5ecddfSPaul E. McKenney#include <linux/compiler.h>
255f192ab0SPaul E. McKenney#include <linux/atomic.h>
264929c913SPaul E. McKenney#include <linux/irqflags.h>
275f192ab0SPaul E. McKenney#include <linux/preempt.h>
285f192ab0SPaul E. McKenney#include <linux/bottom_half.h>
295f192ab0SPaul E. McKenney#include <linux/lockdep.h>
305f192ab0SPaul E. McKenney#include <asm/processor.h>
315f192ab0SPaul E. McKenney#include <linux/cpumask.h>
32c1ad348bSThomas Gleixner
33a3dc3fb1SPaul E. McKenney#define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
34a3dc3fb1SPaul E. McKenney#define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
35c0f4dfd4SPaul E. McKenney#define ulong2long(a)		(*(long *)(&(a)))
368b5bd67cSPaul E. McKenney#define USHORT_CMP_GE(a, b)	(USHRT_MAX / 2 >= (unsigned short)((a) - (b)))
378b5bd67cSPaul E. McKenney#define USHORT_CMP_LT(a, b)	(USHRT_MAX / 2 < (unsigned short)((a) - (b)))
38a3dc3fb1SPaul E. McKenney
3903b042bfSPaul E. McKenney/* Exported common interfaces */
40709fdce7SPaul E. McKenneyvoid call_rcu(struct rcu_head *head, rcu_callback_t func);
4153c6d4edSPaul E. McKenneyvoid rcu_barrier_tasks(void);
42c84aad76SPaul E. McKenneyvoid rcu_barrier_tasks_rude(void);
43709fdce7SPaul E. McKenneyvoid synchronize_rcu(void);
448315f422SPaul E. McKenney
45a3dc3fb1SPaul E. McKenney#ifdef CONFIG_PREEMPT_RCU
46a3dc3fb1SPaul E. McKenney
47584dc4ceSTeodora Balutavoid __rcu_read_lock(void);
48584dc4ceSTeodora Balutavoid __rcu_read_unlock(void);
497b0b759bSPaul E. McKenney
50a3dc3fb1SPaul E. McKenney/*
51a3dc3fb1SPaul E. McKenney * Defined as a macro as it is a very low level header included from
52a3dc3fb1SPaul E. McKenney * areas that don't even know about current.  This gives the rcu_read_lock()
53a3dc3fb1SPaul E. McKenney * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
54a3dc3fb1SPaul E. McKenney * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
55a3dc3fb1SPaul E. McKenney */
565fcb3a5fSPaul E. McKenney#define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
57a3dc3fb1SPaul E. McKenney
587b0b759bSPaul E. McKenney#else /* #ifdef CONFIG_PREEMPT_RCU */
597b0b759bSPaul E. McKenney
60aa40c138SPaul E. McKenney#ifdef CONFIG_TINY_RCU
61aa40c138SPaul E. McKenney#define rcu_read_unlock_strict() do { } while (0)
62aa40c138SPaul E. McKenney#else
63aa40c138SPaul E. McKenneyvoid rcu_read_unlock_strict(void);
64aa40c138SPaul E. McKenney#endif
65aa40c138SPaul E. McKenney
667b0b759bSPaul E. McKenneystatic inline void __rcu_read_lock(void)
677b0b759bSPaul E. McKenney{
6866be4e66SLinus Torvalds	preempt_disable();
697b0b759bSPaul E. McKenney}
707b0b759bSPaul E. McKenney
717b0b759bSPaul E. McKenneystatic inline void __rcu_read_unlock(void)
727b0b759bSPaul E. McKenney{
7366be4e66SLinus Torvalds	preempt_enable();
75925da92bSWaiman Long		rcu_read_unlock_strict();
767b0b759bSPaul E. McKenney}
777b0b759bSPaul E. McKenney
787b0b759bSPaul E. McKenneystatic inline int rcu_preempt_depth(void)
797b0b759bSPaul E. McKenney{
807b0b759bSPaul E. McKenney	return 0;
817b0b759bSPaul E. McKenney}
827b0b759bSPaul E. McKenney
837b0b759bSPaul E. McKenney#endif /* #else #ifdef CONFIG_PREEMPT_RCU */
847b0b759bSPaul E. McKenney
857b0b759bSPaul E. McKenney/* Internal to kernel */
86584dc4ceSTeodora Balutavoid rcu_init(void);
87825c5bd2SPaul E. McKenneyextern int rcu_scheduler_active __read_mostly;
88c98cac60SPaul E. McKenneyvoid rcu_sched_clock_irq(int user);
8927d50c7eSThomas Gleixnervoid rcu_report_dead(unsigned int cpu);
90a58163d8SPaul E. McKenneyvoid rcutree_migrate_callbacks(int cpu);
912b1d5024SFrederic Weisbecker
921b04fa99SUladzislau Rezki (Sony)#ifdef CONFIG_TASKS_RCU_GENERIC
931b04fa99SUladzislau Rezki (Sony)void rcu_init_tasks_generic(void);
941b04fa99SUladzislau Rezki (Sony)#else
951b04fa99SUladzislau Rezki (Sony)static inline void rcu_init_tasks_generic(void) { }
961b04fa99SUladzislau Rezki (Sony)#endif
971b04fa99SUladzislau Rezki (Sony)
9861f38db3SRik van Riel#ifdef CONFIG_RCU_STALL_COMMON
9961f38db3SRik van Rielvoid rcu_sysrq_start(void);
10061f38db3SRik van Rielvoid rcu_sysrq_end(void);
10161f38db3SRik van Riel#else /* #ifdef CONFIG_RCU_STALL_COMMON */
102d0df7a34SPaul E. McKenneystatic inline void rcu_sysrq_start(void) { }
103d0df7a34SPaul E. McKenneystatic inline void rcu_sysrq_end(void) { }
10461f38db3SRik van Riel#endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
10561f38db3SRik van Riel
106d1ec4c34SPaul E. McKenney#ifdef CONFIG_NO_HZ_FULL
107584dc4ceSTeodora Balutavoid rcu_user_enter(void);
108584dc4ceSTeodora Balutavoid rcu_user_exit(void);
1092b1d5024SFrederic Weisbecker#else
1102b1d5024SFrederic Weisbeckerstatic inline void rcu_user_enter(void) { }
1112b1d5024SFrederic Weisbeckerstatic inline void rcu_user_exit(void) { }
112d1ec4c34SPaul E. McKenney#endif /* CONFIG_NO_HZ_FULL */
1132b1d5024SFrederic Weisbecker
114f4579fc5SPaul E. McKenney#ifdef CONFIG_RCU_NOCB_CPU
115f4579fc5SPaul E. McKenneyvoid rcu_init_nohz(void);
116254e11efSFrederic Weisbeckerint rcu_nocb_cpu_offload(int cpu);
117d97b0781SFrederic Weisbeckerint rcu_nocb_cpu_deoffload(int cpu);
11843789ef3SFrederic Weisbeckervoid rcu_nocb_flush_deferred_wakeup(void);
119f4579fc5SPaul E. McKenney#else /* #ifdef CONFIG_RCU_NOCB_CPU */
120d0df7a34SPaul E. McKenneystatic inline void rcu_init_nohz(void) { }
121254e11efSFrederic Weisbeckerstatic inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
122d97b0781SFrederic Weisbeckerstatic inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
12343789ef3SFrederic Weisbeckerstatic inline void rcu_nocb_flush_deferred_wakeup(void) { }
124f4579fc5SPaul E. McKenney#endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
125f4579fc5SPaul E. McKenney
1268a2ecf47SPaul E. McKenney/**
1278a2ecf47SPaul E. McKenney * RCU_NONIDLE - Indicate idle-loop code that needs RCU readers
1288a2ecf47SPaul E. McKenney * @a: Code that RCU needs to pay attention to.
1298a2ecf47SPaul E. McKenney *
1302bd8b1a2SPaul E. McKenney * RCU read-side critical sections are forbidden in the inner idle loop,
1312bd8b1a2SPaul E. McKenney * that is, between the rcu_idle_enter() and the rcu_idle_exit() -- RCU
1322bd8b1a2SPaul E. McKenney * will happily ignore any such read-side critical sections.  However,
1332bd8b1a2SPaul E. McKenney * things like powertop need tracepoints in the inner idle loop.
1348a2ecf47SPaul E. McKenney *
1358a2ecf47SPaul E. McKenney * This macro provides the way out:  RCU_NONIDLE(do_something_with_RCU())
136810ce8b5SPaul E. McKenney * will tell RCU that it needs to pay attention, invoke its argument
137810ce8b5SPaul E. McKenney * (in this example, calling the do_something_with_RCU() function),
1388a2ecf47SPaul E. McKenney * and then tell RCU to go back to ignoring this CPU.  It is permissible
139810ce8b5SPaul E. McKenney * to nest RCU_NONIDLE() wrappers, but not indefinitely (but the limit is
140810ce8b5SPaul E. McKenney * on the order of a million or so, even on 32-bit systems).  It is
141810ce8b5SPaul E. McKenney * not legal to block within RCU_NONIDLE(), nor is it permissible to
142810ce8b5SPaul E. McKenney * transfer control either into or out of RCU_NONIDLE()'s statement.
1438a2ecf47SPaul E. McKenney */
1448a2ecf47SPaul E. McKenney#define RCU_NONIDLE(a) \
1458a2ecf47SPaul E. McKenney	do { \
1467c9906caSPaul E. McKenney		rcu_irq_enter_irqson(); \
1478a2ecf47SPaul E. McKenney		do { a; } while (0); \
1487c9906caSPaul E. McKenney		rcu_irq_exit_irqson(); \
1498a2ecf47SPaul E. McKenney	} while (0)
1508a2ecf47SPaul E. McKenney
1518315f422SPaul E. McKenney/*
1526f56f714SPaul E. McKenney * Note a quasi-voluntary context switch for RCU-tasks's benefit.
1536f56f714SPaul E. McKenney * This is a macro rather than an inline function to avoid #include hell.
1548315f422SPaul E. McKenney */
1555873b8a9SPaul E. McKenney#ifdef CONFIG_TASKS_RCU_GENERIC
15643766c3eSPaul E. McKenney
15743766c3eSPaul E. McKenney# ifdef CONFIG_TASKS_RCU
15843766c3eSPaul E. McKenney# define rcu_tasks_classic_qs(t, preempt)				\
15943766c3eSPaul E. McKenney	do {								\
16043766c3eSPaul E. McKenney		if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout))	\
16143766c3eSPaul E. McKenney			WRITE_ONCE((t)->rcu_tasks_holdout, false);	\
1628315f422SPaul E. McKenney	} while (0)
1637e42776dSPaul E. McKenneyvoid call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
1647e42776dSPaul E. McKenneyvoid synchronize_rcu_tasks(void);
16543766c3eSPaul E. McKenney# else
16643766c3eSPaul E. McKenney# define rcu_tasks_classic_qs(t, preempt) do { } while (0)
16743766c3eSPaul E. McKenney# define call_rcu_tasks call_rcu
16843766c3eSPaul E. McKenney# define synchronize_rcu_tasks synchronize_rcu
16943766c3eSPaul E. McKenney# endif
17043766c3eSPaul E. McKenney
171fed31a4dSZhouyi Zhou# ifdef CONFIG_TASKS_TRACE_RCU
17243766c3eSPaul E. McKenney# define rcu_tasks_trace_qs(t)						\
17343766c3eSPaul E. McKenney	do {								\
17443766c3eSPaul E. McKenney		if (!likely(READ_ONCE((t)->trc_reader_checked)) &&	\
17543766c3eSPaul E. McKenney		    !unlikely(READ_ONCE((t)->trc_reader_nesting))) {	\
17643766c3eSPaul E. McKenney			smp_store_release(&(t)->trc_reader_checked, true); \
17743766c3eSPaul E. McKenney			smp_mb(); /* Readers partitioned by store. */	\
17843766c3eSPaul E. McKenney		}							\
17943766c3eSPaul E. McKenney	} while (0)
18043766c3eSPaul E. McKenney# else
18143766c3eSPaul E. McKenney# define rcu_tasks_trace_qs(t) do { } while (0)
18243766c3eSPaul E. McKenney# endif
18343766c3eSPaul E. McKenney
18443766c3eSPaul E. McKenney#define rcu_tasks_qs(t, preempt)					\
18543766c3eSPaul E. McKenneydo {									\
18643766c3eSPaul E. McKenney	rcu_tasks_classic_qs((t), (preempt));				\
18743766c3eSPaul E. McKenney	rcu_tasks_trace_qs((t));					\
18843766c3eSPaul E. McKenney} while (0)
18943766c3eSPaul E. McKenney
19043766c3eSPaul E. McKenney# ifdef CONFIG_TASKS_RUDE_RCU
191c84aad76SPaul E. McKenneyvoid call_rcu_tasks_rude(struct rcu_head *head, rcu_callback_t func);
192c84aad76SPaul E. McKenneyvoid synchronize_rcu_tasks_rude(void);
19343766c3eSPaul E. McKenney# endif
19443766c3eSPaul E. McKenney
19543766c3eSPaul E. McKenney#define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
196ccdd29ffSPaul E. McKenneyvoid exit_tasks_rcu_start(void);
197ccdd29ffSPaul E. McKenneyvoid exit_tasks_rcu_finish(void);
1985873b8a9SPaul E. McKenney#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
19943766c3eSPaul E. McKenney#define rcu_tasks_qs(t, preempt) do { } while (0)
2004d232dfeSPaul E. McKenney#define rcu_note_voluntary_context_switch(t) do { } while (0)
2012bd8b1a2SPaul E. McKenney#define call_rcu_tasks call_rcu
202a8bb74acSPaul E. McKenney#define synchronize_rcu_tasks synchronize_rcu
203ccdd29ffSPaul E. McKenneystatic inline void exit_tasks_rcu_start(void) { }
204ccdd29ffSPaul E. McKenneystatic inline void exit_tasks_rcu_finish(void) { }
2055873b8a9SPaul E. McKenney#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
2068315f422SPaul E. McKenney
207bde6c3aaSPaul E. McKenney/**
208cee43939SPaul E. McKenney * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
209bde6c3aaSPaul E. McKenney *
210bde6c3aaSPaul E. McKenney * This macro resembles cond_resched(), except that it is defined to
211bde6c3aaSPaul E. McKenney * report potential quiescent states to RCU-tasks even if the cond_resched()
21290326f05SSebastian Andrzej Siewior * machinery were to be shut off, as some advocate for PREEMPTION kernels.
213bde6c3aaSPaul E. McKenney */
214cee43939SPaul E. McKenney#define cond_resched_tasks_rcu_qs() \
215bde6c3aaSPaul E. McKenneydo { \
21643766c3eSPaul E. McKenney	rcu_tasks_qs(current, false); \
21707f27570SByungchul Park	cond_resched(); \
218bde6c3aaSPaul E. McKenney} while (0)
219bde6c3aaSPaul E. McKenney
2202c42818eSPaul E. McKenney/*
2212c42818eSPaul E. McKenney * Infrastructure to implement the synchronize_() primitives in
2222c42818eSPaul E. McKenney * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
2232c42818eSPaul E. McKenney */
2242c42818eSPaul E. McKenney
225b3e627d3SLai Jiangshan#if defined(CONFIG_TREE_RCU)
22664db4cffSPaul E. McKenney#include <linux/rcutree.h>
227127781d1SPaul E. McKenney#elif defined(CONFIG_TINY_RCU)
2289b1d82faSPaul E. McKenney#include <linux/rcutiny.h>
22964db4cffSPaul E. McKenney#else
23064db4cffSPaul E. McKenney#error "Unknown RCU implementation specified to kernel configuration"
2316b3ef48aSPaul E. McKenney#endif
23201c1c660SPaul E. McKenney
233551d55a9SMathieu Desnoyers/*
234b5482a06SPaul E. McKenney * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
235b5482a06SPaul E. McKenney * are needed for dynamic initialization and destruction of rcu_head
236b5482a06SPaul E. McKenney * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
237b5482a06SPaul E. McKenney * dynamic initialization and destruction of statically allocated rcu_head
238b5482a06SPaul E. McKenney * structures.  However, rcu_head structures allocated dynamically in the
239b5482a06SPaul E. McKenney * heap don't need any initialization.
240551d55a9SMathieu Desnoyers */
241551d55a9SMathieu Desnoyers#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
242546a9d85SPaul E. McKenneyvoid init_rcu_head(struct rcu_head *head);
243546a9d85SPaul E. McKenneyvoid destroy_rcu_head(struct rcu_head *head);
244584dc4ceSTeodora Balutavoid init_rcu_head_on_stack(struct rcu_head *head);
245584dc4ceSTeodora Balutavoid destroy_rcu_head_on_stack(struct rcu_head *head);
246551d55a9SMathieu Desnoyers#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
247d0df7a34SPaul E. McKenneystatic inline void init_rcu_head(struct rcu_head *head) { }
248d0df7a34SPaul E. McKenneystatic inline void destroy_rcu_head(struct rcu_head *head) { }
249d0df7a34SPaul E. McKenneystatic inline void init_rcu_head_on_stack(struct rcu_head *head) { }
250d0df7a34SPaul E. McKenneystatic inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
251551d55a9SMathieu Desnoyers#endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2524376030aSMathieu Desnoyers
253c0d6d01bSPaul E. McKenney#if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
254c0d6d01bSPaul E. McKenneybool rcu_lockdep_current_cpu_online(void);
255c0d6d01bSPaul E. McKenney#else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
25617a8c187SPaul E. McKenneystatic inline bool rcu_lockdep_current_cpu_online(void) { return true; }
257c0d6d01bSPaul E. McKenney#endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
258c0d6d01bSPaul E. McKenney
259891cd1f9SJakub Kicinskiextern struct lockdep_map rcu_lock_map;
260891cd1f9SJakub Kicinskiextern struct lockdep_map rcu_bh_lock_map;
261891cd1f9SJakub Kicinskiextern struct lockdep_map rcu_sched_lock_map;
262891cd1f9SJakub Kicinskiextern struct lockdep_map rcu_callback_map;
263891cd1f9SJakub Kicinski
264bc33f24bSPaul E. McKenney#ifdef CONFIG_DEBUG_LOCK_ALLOC
265632ee200SPaul E. McKenney
26600f49e57SFrederic Weisbeckerstatic inline void rcu_lock_acquire(struct lockdep_map *map)
26700f49e57SFrederic Weisbecker{
268fb9edbe9SOleg Nesterov	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
26900f49e57SFrederic Weisbecker}
27000f49e57SFrederic Weisbecker
27100f49e57SFrederic Weisbeckerstatic inline void rcu_lock_release(struct lockdep_map *map)
27200f49e57SFrederic Weisbecker{
2735facae4fSQian Cai	lock_release(map, _THIS_IP_);
27400f49e57SFrederic Weisbecker}
27500f49e57SFrederic Weisbecker
276a235c091SIulia Mandaint debug_lockdep_rcu_enabled(void);
27785b39d30SOleg Nesterovint rcu_read_lock_held(void);
278584dc4ceSTeodora Balutaint rcu_read_lock_bh_held(void);
279d5671f6bSDenys Vlasenkoint rcu_read_lock_sched_held(void);
28028875945SJoel Fernandes (Google)int rcu_read_lock_any_held(void);
281632ee200SPaul E. McKenney
282632ee200SPaul E. McKenney#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
283632ee200SPaul E. McKenney
284d8ab29f8SPaul E. McKenney# define rcu_lock_acquire(a)		do { } while (0)
285d8ab29f8SPaul E. McKenney# define rcu_lock_release(a)		do { } while (0)
286632ee200SPaul E. McKenney
287632ee200SPaul E. McKenneystatic inline int rcu_read_lock_held(void)
288632ee200SPaul E. McKenney{
289632ee200SPaul E. McKenney	return 1;
290632ee200SPaul E. McKenney}
291632ee200SPaul E. McKenney
292632ee200SPaul E. McKenneystatic inline int rcu_read_lock_bh_held(void)
293632ee200SPaul E. McKenney{
294632ee200SPaul E. McKenney	return 1;
295632ee200SPaul E. McKenney}
296632ee200SPaul E. McKenney
297632ee200SPaul E. McKenneystatic inline int rcu_read_lock_sched_held(void)
298632ee200SPaul E. McKenney{
299293e2421SBoqun Feng	return !preemptible();
300632ee200SPaul E. McKenney}
30128875945SJoel Fernandes (Google)
30228875945SJoel Fernandes (Google)static inline int rcu_read_lock_any_held(void)
30328875945SJoel Fernandes (Google){
30428875945SJoel Fernandes (Google)	return !preemptible();
30528875945SJoel Fernandes (Google)}
30628875945SJoel Fernandes (Google)
307632ee200SPaul E. McKenney#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
308632ee200SPaul E. McKenney
309632ee200SPaul E. McKenney#ifdef CONFIG_PROVE_RCU
310632ee200SPaul E. McKenney
311f78f5b90SPaul E. McKenney/**
312f78f5b90SPaul E. McKenney * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
313f78f5b90SPaul E. McKenney * @c: condition to check
314f78f5b90SPaul E. McKenney * @s: informative message
315f78f5b90SPaul E. McKenney */
316f78f5b90SPaul E. McKenney#define RCU_LOCKDEP_WARN(c, s)						\
317f78f5b90SPaul E. McKenney	do {								\
31833def849SJoe Perches		static bool __section(".data.unlikely") __warned;	\
31930668200SPaul E. McKenney		if ((c) && debug_lockdep_rcu_enabled() && !__warned) {	\
320f78f5b90SPaul E. McKenney			__warned = true;				\
321f78f5b90SPaul E. McKenney			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
322f78f5b90SPaul E. McKenney		}							\
323f78f5b90SPaul E. McKenney	} while (0)
324f78f5b90SPaul E. McKenney
32550406b98SPaul E. McKenney#if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU)
32650406b98SPaul E. McKenneystatic inline void rcu_preempt_sleep_check(void)
32750406b98SPaul E. McKenney{
328f78f5b90SPaul E. McKenney	RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
329f78f5b90SPaul E. McKenney			 "Illegal context switch in RCU read-side critical section");
33050406b98SPaul E. McKenney}
33150406b98SPaul E. McKenney#else /* #ifdef CONFIG_PROVE_RCU */
332d0df7a34SPaul E. McKenneystatic inline void rcu_preempt_sleep_check(void) { }
33350406b98SPaul E. McKenney#endif /* #else #ifdef CONFIG_PROVE_RCU */
33450406b98SPaul E. McKenney
335b3fbab05SPaul E. McKenney#define rcu_sleep_check()						\
336b3fbab05SPaul E. McKenney	do {								\
33750406b98SPaul E. McKenney		rcu_preempt_sleep_check();				\
338ba9e6cabSThomas Gleixner		if (!IS_ENABLED(CONFIG_PREEMPT_RT))			\
339ba9e6cabSThomas Gleixner		    RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),	\
340f78f5b90SPaul E. McKenney				 "Illegal context switch in RCU-bh read-side critical section"); \
341f78f5b90SPaul E. McKenney		RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),	\
342f78f5b90SPaul E. McKenney				 "Illegal context switch in RCU-sched read-side critical section"); \
343b3fbab05SPaul E. McKenney	} while (0)
344b3fbab05SPaul E. McKenney
345ca5ecddfSPaul E. McKenney#else /* #ifdef CONFIG_PROVE_RCU */
346ca5ecddfSPaul E. McKenney
34765e9eb1cSJakub Kicinski#define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c))
348b3fbab05SPaul E. McKenney#define rcu_sleep_check() do { } while (0)
349ca5ecddfSPaul E. McKenney
350ca5ecddfSPaul E. McKenney#endif /* #else #ifdef CONFIG_PROVE_RCU */
351ca5ecddfSPaul E. McKenney
352ca5ecddfSPaul E. McKenney/*
353ca5ecddfSPaul E. McKenney * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
354ca5ecddfSPaul E. McKenney * and rcu_assign_pointer().  Some of these could be folded into their
355ca5ecddfSPaul E. McKenney * callers, but they are left separate in order to ease introduction of
3562bd8b1a2SPaul E. McKenney * multiple pointers markings to match different RCU implementations
3572bd8b1a2SPaul E. McKenney * (e.g., __srcu), should this make sense in the future.
358ca5ecddfSPaul E. McKenney */
35953ecfba2SPaul E. McKenney
36053ecfba2SPaul E. McKenney#ifdef __CHECKER__
361423a86a6SJoel Fernandes (Google)#define rcu_check_sparse(p, space) \
36253ecfba2SPaul E. McKenney	((void)(((typeof(*p) space *)p) == p))
36353ecfba2SPaul E. McKenney#else /* #ifdef __CHECKER__ */
364423a86a6SJoel Fernandes (Google)#define rcu_check_sparse(p, space)
36553ecfba2SPaul E. McKenney#endif /* #else #ifdef __CHECKER__ */
36653ecfba2SPaul E. McKenney
36724ba5301SChun-Hung Tseng#define __unrcu_pointer(p, local)					\
36824ba5301SChun-Hung Tseng({									\
36924ba5301SChun-Hung Tseng	typeof(*p) *local = (typeof(*p) *__force)(p);			\
37024ba5301SChun-Hung Tseng	rcu_check_sparse(p, __rcu);					\
37124ba5301SChun-Hung Tseng	((typeof(*p) __force __kernel *)(local)); 			\
37224ba5301SChun-Hung Tseng})
37376c8eaafSPaul E. McKenney/**
37476c8eaafSPaul E. McKenney * unrcu_pointer - mark a pointer as not being RCU protected
37576c8eaafSPaul E. McKenney * @p: pointer needing to lose its __rcu property
37676c8eaafSPaul E. McKenney *
37776c8eaafSPaul E. McKenney * Converts @p from an __rcu pointer to a __kernel pointer.
37876c8eaafSPaul E. McKenney * This allows an __rcu pointer to be used with xchg() and friends.
37976c8eaafSPaul E. McKenney */