xref: /openbmc/linux/include/linux/rcupdate.h (revision 24e5e1ef)
173604da5SPaul E. McKenney /* SPDX-License-Identifier: GPL-2.0+ */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * 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>
81da177e4SLinus Torvalds  *
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 -
161da177e4SLinus Torvalds  *		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>
3054da6a09SPeter Zijlstra #include <linux/cleanup.h>
315f192ab0SPaul E. McKenney #include <asm/processor.h>
325f192ab0SPaul E. McKenney #include <linux/cpumask.h>
336f0e6c15SFrederic Weisbecker #include <linux/context_tracking_irq.h>
34c1ad348bSThomas Gleixner 
35a3dc3fb1SPaul E. McKenney #define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
36a3dc3fb1SPaul E. McKenney #define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
37c0f4dfd4SPaul E. McKenney #define ulong2long(a)		(*(long *)(&(a)))
388b5bd67cSPaul E. McKenney #define USHORT_CMP_GE(a, b)	(USHRT_MAX / 2 >= (unsigned short)((a) - (b)))
398b5bd67cSPaul E. McKenney #define USHORT_CMP_LT(a, b)	(USHRT_MAX / 2 < (unsigned short)((a) - (b)))
40a3dc3fb1SPaul E. McKenney 
4103b042bfSPaul E. McKenney /* Exported common interfaces */
42709fdce7SPaul E. McKenney void call_rcu(struct rcu_head *head, rcu_callback_t func);
4353c6d4edSPaul E. McKenney void rcu_barrier_tasks(void);
44c84aad76SPaul E. McKenney void rcu_barrier_tasks_rude(void);
45709fdce7SPaul E. McKenney void synchronize_rcu(void);
4691a967fdSPaul E. McKenney 
4791a967fdSPaul E. McKenney struct rcu_gp_oldstate;
48414c1238SPaul E. McKenney unsigned long get_completed_synchronize_rcu(void);
4991a967fdSPaul E. McKenney void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp);
508315f422SPaul E. McKenney 
5118538248SPaul E. McKenney // Maximum number of unsigned long values corresponding to
5218538248SPaul E. McKenney // not-yet-completed RCU grace periods.
5318538248SPaul E. McKenney #define NUM_ACTIVE_RCU_POLL_OLDSTATE 2
5418538248SPaul E. McKenney 
5518538248SPaul E. McKenney /**
5618538248SPaul E. McKenney  * same_state_synchronize_rcu - Are two old-state values identical?
5718538248SPaul E. McKenney  * @oldstate1: First old-state value.
5818538248SPaul E. McKenney  * @oldstate2: Second old-state value.
5918538248SPaul E. McKenney  *
6018538248SPaul E. McKenney  * The two old-state values must have been obtained from either
6118538248SPaul E. McKenney  * get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or
6218538248SPaul E. McKenney  * get_completed_synchronize_rcu().  Returns @true if the two values are
6318538248SPaul E. McKenney  * identical and @false otherwise.  This allows structures whose lifetimes
6418538248SPaul E. McKenney  * are tracked by old-state values to push these values to a list header,
6518538248SPaul E. McKenney  * allowing those structures to be slightly smaller.
6618538248SPaul E. McKenney  */
same_state_synchronize_rcu(unsigned long oldstate1,unsigned long oldstate2)6718538248SPaul E. McKenney static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned long oldstate2)
6818538248SPaul E. McKenney {
6918538248SPaul E. McKenney 	return oldstate1 == oldstate2;
7018538248SPaul E. McKenney }
7103b042bfSPaul E. McKenney 
72a3dc3fb1SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
73a3dc3fb1SPaul E. McKenney 
74584dc4ceSTeodora Baluta void __rcu_read_lock(void);
75584dc4ceSTeodora Baluta void __rcu_read_unlock(void);
767b0b759bSPaul E. McKenney 
77a3dc3fb1SPaul E. McKenney /*
78a3dc3fb1SPaul E. McKenney  * Defined as a macro as it is a very low level header included from
79a3dc3fb1SPaul E. McKenney  * areas that don't even know about current.  This gives the rcu_read_lock()
80a3dc3fb1SPaul E. McKenney  * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
81a3dc3fb1SPaul E. McKenney  * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
82a3dc3fb1SPaul E. McKenney  */
835fcb3a5fSPaul E. McKenney #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
84a3dc3fb1SPaul E. McKenney 
857b0b759bSPaul E. McKenney #else /* #ifdef CONFIG_PREEMPT_RCU */
867b0b759bSPaul E. McKenney 
87aa40c138SPaul E. McKenney #ifdef CONFIG_TINY_RCU
88aa40c138SPaul E. McKenney #define rcu_read_unlock_strict() do { } while (0)
89aa40c138SPaul E. McKenney #else
90aa40c138SPaul E. McKenney void rcu_read_unlock_strict(void);
91aa40c138SPaul E. McKenney #endif
92aa40c138SPaul E. McKenney 
__rcu_read_lock(void)937b0b759bSPaul E. McKenney static inline void __rcu_read_lock(void)
947b0b759bSPaul E. McKenney {
957b0b759bSPaul E. McKenney 	preempt_disable();
967b0b759bSPaul E. McKenney }
977b0b759bSPaul E. McKenney 
__rcu_read_unlock(void)987b0b759bSPaul E. McKenney static inline void __rcu_read_unlock(void)
997b0b759bSPaul E. McKenney {
1007b0b759bSPaul E. McKenney 	preempt_enable();
101925da92bSWaiman Long 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
102aa40c138SPaul E. McKenney 		rcu_read_unlock_strict();
1037b0b759bSPaul E. McKenney }
1047b0b759bSPaul E. McKenney 
rcu_preempt_depth(void)1057b0b759bSPaul E. McKenney static inline int rcu_preempt_depth(void)
1067b0b759bSPaul E. McKenney {
1077b0b759bSPaul E. McKenney 	return 0;
1087b0b759bSPaul E. McKenney }
1097b0b759bSPaul E. McKenney 
1107b0b759bSPaul E. McKenney #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
1117b0b759bSPaul E. McKenney 
1123cb278e7SJoel Fernandes (Google) #ifdef CONFIG_RCU_LAZY
1133cb278e7SJoel Fernandes (Google) void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func);
1143cb278e7SJoel Fernandes (Google) #else
call_rcu_hurry(struct rcu_head * head,rcu_callback_t func)1153cb278e7SJoel Fernandes (Google) static inline void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
1163cb278e7SJoel Fernandes (Google) {
1173cb278e7SJoel Fernandes (Google) 	call_rcu(head, func);
1183cb278e7SJoel Fernandes (Google) }
1193cb278e7SJoel Fernandes (Google) #endif
1203cb278e7SJoel Fernandes (Google) 
1217b0b759bSPaul E. McKenney /* Internal to kernel */
122584dc4ceSTeodora Baluta void rcu_init(void);
123e6339d3bSIngo Molnar extern int rcu_scheduler_active;
124c98cac60SPaul E. McKenney void rcu_sched_clock_irq(int user);
12527d50c7eSThomas Gleixner void rcu_report_dead(unsigned int cpu);
126a58163d8SPaul E. McKenney void rcutree_migrate_callbacks(int cpu);
1272b1d5024SFrederic Weisbecker 
1281b04fa99SUladzislau Rezki (Sony) #ifdef CONFIG_TASKS_RCU_GENERIC
1291b04fa99SUladzislau Rezki (Sony) void rcu_init_tasks_generic(void);
1301b04fa99SUladzislau Rezki (Sony) #else
rcu_init_tasks_generic(void)1311b04fa99SUladzislau Rezki (Sony) static inline void rcu_init_tasks_generic(void) { }
1321b04fa99SUladzislau Rezki (Sony) #endif
1331b04fa99SUladzislau Rezki (Sony) 
13461f38db3SRik van Riel #ifdef CONFIG_RCU_STALL_COMMON
13561f38db3SRik van Riel void rcu_sysrq_start(void);
13661f38db3SRik van Riel void rcu_sysrq_end(void);
13761f38db3SRik van Riel #else /* #ifdef CONFIG_RCU_STALL_COMMON */
rcu_sysrq_start(void)138d0df7a34SPaul E. McKenney static inline void rcu_sysrq_start(void) { }
rcu_sysrq_end(void)139d0df7a34SPaul E. McKenney static inline void rcu_sysrq_end(void) { }
14061f38db3SRik van Riel #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
14161f38db3SRik van Riel 
14256450649SFrederic Weisbecker #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK))
14356450649SFrederic Weisbecker void rcu_irq_work_resched(void);
1442b1d5024SFrederic Weisbecker #else
rcu_irq_work_resched(void)14556450649SFrederic Weisbecker static inline void rcu_irq_work_resched(void) { }
14656450649SFrederic Weisbecker #endif
1472b1d5024SFrederic Weisbecker 
148f4579fc5SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
149f4579fc5SPaul E. McKenney void rcu_init_nohz(void);
150254e11efSFrederic Weisbecker int rcu_nocb_cpu_offload(int cpu);
151d97b0781SFrederic Weisbecker int rcu_nocb_cpu_deoffload(int cpu);
15243789ef3SFrederic Weisbecker void rcu_nocb_flush_deferred_wakeup(void);
153f4579fc5SPaul E. McKenney #else /* #ifdef CONFIG_RCU_NOCB_CPU */
rcu_init_nohz(void)154d0df7a34SPaul E. McKenney static inline void rcu_init_nohz(void) { }
rcu_nocb_cpu_offload(int cpu)155254e11efSFrederic Weisbecker static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
rcu_nocb_cpu_deoffload(int cpu)156d97b0781SFrederic Weisbecker static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
rcu_nocb_flush_deferred_wakeup(void)15743789ef3SFrederic Weisbecker static inline void rcu_nocb_flush_deferred_wakeup(void) { }
158f4579fc5SPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
159f4579fc5SPaul E. McKenney 
1608315f422SPaul E. McKenney /*
1616f56f714SPaul E. McKenney  * Note a quasi-voluntary context switch for RCU-tasks's benefit.
1626f56f714SPaul E. McKenney  * This is a macro rather than an inline function to avoid #include hell.
1638315f422SPaul E. McKenney  */
1645873b8a9SPaul E. McKenney #ifdef CONFIG_TASKS_RCU_GENERIC
16543766c3eSPaul E. McKenney 
16643766c3eSPaul E. McKenney # ifdef CONFIG_TASKS_RCU
16743766c3eSPaul E. McKenney # define rcu_tasks_classic_qs(t, preempt)				\
1688315f422SPaul E. McKenney 	do {								\
16943766c3eSPaul E. McKenney 		if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout))	\
1707d0ae808SPaul E. McKenney 			WRITE_ONCE((t)->rcu_tasks_holdout, false);	\
1718315f422SPaul E. McKenney 	} while (0)
1727e42776dSPaul E. McKenney void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
1737e42776dSPaul E. McKenney void synchronize_rcu_tasks(void);
17443766c3eSPaul E. McKenney # else
17543766c3eSPaul E. McKenney # define rcu_tasks_classic_qs(t, preempt) do { } while (0)
17643766c3eSPaul E. McKenney # define call_rcu_tasks call_rcu
17743766c3eSPaul E. McKenney # define synchronize_rcu_tasks synchronize_rcu
17843766c3eSPaul E. McKenney # endif
17943766c3eSPaul E. McKenney 
180fed31a4dSZhouyi Zhou # ifdef CONFIG_TASKS_TRACE_RCU
1813847b645SPaul E. McKenney // Bits for ->trc_reader_special.b.need_qs field.
1823847b645SPaul E. McKenney #define TRC_NEED_QS		0x1  // Task needs a quiescent state.
1833847b645SPaul E. McKenney #define TRC_NEED_QS_CHECKED	0x2  // Task has been checked for needing quiescent state.
1843847b645SPaul E. McKenney 
1853847b645SPaul E. McKenney u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new);
1860356d4e6SPaul E. McKenney void rcu_tasks_trace_qs_blkd(struct task_struct *t);
1873847b645SPaul E. McKenney 
18843766c3eSPaul E. McKenney # define rcu_tasks_trace_qs(t)							\
18943766c3eSPaul E. McKenney 	do {									\
1900356d4e6SPaul E. McKenney 		int ___rttq_nesting = READ_ONCE((t)->trc_reader_nesting);	\
1910356d4e6SPaul E. McKenney 										\
19224e5e1efSPaul E. McKenney 		if (unlikely(READ_ONCE((t)->trc_reader_special.b.need_qs) == TRC_NEED_QS) &&	\
1930356d4e6SPaul E. McKenney 		    likely(!___rttq_nesting)) {					\
19424e5e1efSPaul E. McKenney 			rcu_trc_cmpxchg_need_qs((t), TRC_NEED_QS, TRC_NEED_QS_CHECKED);	\
1950356d4e6SPaul E. McKenney 		} else if (___rttq_nesting && ___rttq_nesting != INT_MIN &&	\
1960356d4e6SPaul E. McKenney 			   !READ_ONCE((t)->trc_reader_special.b.blocked)) {	\
1970356d4e6SPaul E. McKenney 			rcu_tasks_trace_qs_blkd(t);				\
19843766c3eSPaul E. McKenney 		}								\
19943766c3eSPaul E. McKenney 	} while (0)
20043766c3eSPaul E. McKenney # else
20143766c3eSPaul E. McKenney # define rcu_tasks_trace_qs(t) do { } while (0)
20243766c3eSPaul E. McKenney # endif
20343766c3eSPaul E. McKenney 
20443766c3eSPaul E. McKenney #define rcu_tasks_qs(t, preempt)					\
20543766c3eSPaul E. McKenney do {									\
20643766c3eSPaul E. McKenney 	rcu_tasks_classic_qs((t), (preempt));				\
2070356d4e6SPaul E. McKenney 	rcu_tasks_trace_qs(t);						\
20843766c3eSPaul E. McKenney } while (0)
20943766c3eSPaul E. McKenney 
21043766c3eSPaul E. McKenney # ifdef CONFIG_TASKS_RUDE_RCU
211c84aad76SPaul E. McKenney void call_rcu_tasks_rude(struct rcu_head *head, rcu_callback_t func);
212c84aad76SPaul E. McKenney void synchronize_rcu_tasks_rude(void);
21343766c3eSPaul E. McKenney # endif
21443766c3eSPaul E. McKenney 
21543766c3eSPaul E. McKenney #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
216ccdd29ffSPaul E. McKenney void exit_tasks_rcu_start(void);
21728319d6dSFrederic Weisbecker void exit_tasks_rcu_stop(void);
218ccdd29ffSPaul E. McKenney void exit_tasks_rcu_finish(void);
2195873b8a9SPaul E. McKenney #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
2205d900708SPaul E. McKenney #define rcu_tasks_classic_qs(t, preempt) do { } while (0)
22143766c3eSPaul E. McKenney #define rcu_tasks_qs(t, preempt) do { } while (0)
2224d232dfeSPaul E. McKenney #define rcu_note_voluntary_context_switch(t) do { } while (0)
2232bd8b1a2SPaul E. McKenney #define call_rcu_tasks call_rcu
224a8bb74acSPaul E. McKenney #define synchronize_rcu_tasks synchronize_rcu
exit_tasks_rcu_start(void)225ccdd29ffSPaul E. McKenney static inline void exit_tasks_rcu_start(void) { }
exit_tasks_rcu_stop(void)22628319d6dSFrederic Weisbecker static inline void exit_tasks_rcu_stop(void) { }
exit_tasks_rcu_finish(void)227ccdd29ffSPaul E. McKenney static inline void exit_tasks_rcu_finish(void) { }
2285873b8a9SPaul E. McKenney #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
2298315f422SPaul E. McKenney 
230bde6c3aaSPaul E. McKenney /**
231e6c86c51SPaul E. McKenney  * rcu_trace_implies_rcu_gp - does an RCU Tasks Trace grace period imply an RCU grace period?
232e6c86c51SPaul E. McKenney  *
233e6c86c51SPaul E. McKenney  * As an accident of implementation, an RCU Tasks Trace grace period also
234e6c86c51SPaul E. McKenney  * acts as an RCU grace period.  However, this could change at any time.
235e6c86c51SPaul E. McKenney  * Code relying on this accident must call this function to verify that
236e6c86c51SPaul E. McKenney  * this accident is still happening.
237e6c86c51SPaul E. McKenney  *
238e6c86c51SPaul E. McKenney  * You have been warned!
239e6c86c51SPaul E. McKenney  */
rcu_trace_implies_rcu_gp(void)240e6c86c51SPaul E. McKenney static inline bool rcu_trace_implies_rcu_gp(void) { return true; }
241e6c86c51SPaul E. McKenney 
242e6c86c51SPaul E. McKenney /**
243cee43939SPaul E. McKenney  * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
244bde6c3aaSPaul E. McKenney  *
245bde6c3aaSPaul E. McKenney  * This macro resembles cond_resched(), except that it is defined to
246bde6c3aaSPaul E. McKenney  * report potential quiescent states to RCU-tasks even if the cond_resched()
24790326f05SSebastian Andrzej Siewior  * machinery were to be shut off, as some advocate for PREEMPTION kernels.
248bde6c3aaSPaul E. McKenney  */
249cee43939SPaul E. McKenney #define cond_resched_tasks_rcu_qs() \
250bde6c3aaSPaul E. McKenney do { \
25143766c3eSPaul E. McKenney 	rcu_tasks_qs(current, false); \
25207f27570SByungchul Park 	cond_resched(); \
253bde6c3aaSPaul E. McKenney } while (0)
254bde6c3aaSPaul E. McKenney 
255c2619021SYan Zhai /**
256c2619021SYan Zhai  * rcu_softirq_qs_periodic - Report RCU and RCU-Tasks quiescent states
257c2619021SYan Zhai  * @old_ts: jiffies at start of processing.
258c2619021SYan Zhai  *
259c2619021SYan Zhai  * This helper is for long-running softirq handlers, such as NAPI threads in
260c2619021SYan Zhai  * networking. The caller should initialize the variable passed in as @old_ts
261c2619021SYan Zhai  * at the beginning of the softirq handler. When invoked frequently, this macro
262c2619021SYan Zhai  * will invoke rcu_softirq_qs() every 100 milliseconds thereafter, which will
263c2619021SYan Zhai  * provide both RCU and RCU-Tasks quiescent states. Note that this macro
264c2619021SYan Zhai  * modifies its old_ts argument.
265c2619021SYan Zhai  *
266c2619021SYan Zhai  * Because regions of code that have disabled softirq act as RCU read-side
267c2619021SYan Zhai  * critical sections, this macro should be invoked with softirq (and
268c2619021SYan Zhai  * preemption) enabled.
269c2619021SYan Zhai  *
270c2619021SYan Zhai  * The macro is not needed when CONFIG_PREEMPT_RT is defined. RT kernels would
271c2619021SYan Zhai  * have more chance to invoke schedule() calls and provide necessary quiescent
272c2619021SYan Zhai  * states. As a contrast, calling cond_resched() only won't achieve the same
273c2619021SYan Zhai  * effect because cond_resched() does not provide RCU-Tasks quiescent states.
274c2619021SYan Zhai  */
275c2619021SYan Zhai #define rcu_softirq_qs_periodic(old_ts) \
276c2619021SYan Zhai do { \
277c2619021SYan Zhai 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && \
278c2619021SYan Zhai 	    time_after(jiffies, (old_ts) + HZ / 10)) { \
279c2619021SYan Zhai 		preempt_disable(); \
280c2619021SYan Zhai 		rcu_softirq_qs(); \
281c2619021SYan Zhai 		preempt_enable(); \
282c2619021SYan Zhai 		(old_ts) = jiffies; \
283c2619021SYan Zhai 	} \
284c2619021SYan Zhai } while (0)
285c2619021SYan Zhai 
2862c42818eSPaul E. McKenney /*
2872c42818eSPaul E. McKenney  * Infrastructure to implement the synchronize_() primitives in
2882c42818eSPaul E. McKenney  * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
2892c42818eSPaul E. McKenney  */
2902c42818eSPaul E. McKenney 
291b3e627d3SLai Jiangshan #if defined(CONFIG_TREE_RCU)
29264db4cffSPaul E. McKenney #include <linux/rcutree.h>
293127781d1SPaul E. McKenney #elif defined(CONFIG_TINY_RCU)
2949b1d82faSPaul E. McKenney #include <linux/rcutiny.h>
29564db4cffSPaul E. McKenney #else
29664db4cffSPaul E. McKenney #error "Unknown RCU implementation specified to kernel configuration"
2976b3ef48aSPaul E. McKenney #endif
29801c1c660SPaul E. McKenney 
299551d55a9SMathieu Desnoyers /*
300b5482a06SPaul E. McKenney  * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
301b5482a06SPaul E. McKenney  * are needed for dynamic initialization and destruction of rcu_head
302b5482a06SPaul E. McKenney  * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
303b5482a06SPaul E. McKenney  * dynamic initialization and destruction of statically allocated rcu_head
304b5482a06SPaul E. McKenney  * structures.  However, rcu_head structures allocated dynamically in the
305b5482a06SPaul E. McKenney  * heap don't need any initialization.
306551d55a9SMathieu Desnoyers  */
307551d55a9SMathieu Desnoyers #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
308546a9d85SPaul E. McKenney void init_rcu_head(struct rcu_head *head);
309546a9d85SPaul E. McKenney void destroy_rcu_head(struct rcu_head *head);
310584dc4ceSTeodora Baluta void init_rcu_head_on_stack(struct rcu_head *head);
311584dc4ceSTeodora Baluta void destroy_rcu_head_on_stack(struct rcu_head *head);
312551d55a9SMathieu Desnoyers #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
init_rcu_head(struct rcu_head * head)313d0df7a34SPaul E. McKenney static inline void init_rcu_head(struct rcu_head *head) { }
destroy_rcu_head(struct rcu_head * head)314d0df7a34SPaul E. McKenney static inline void destroy_rcu_head(struct rcu_head *head) { }
init_rcu_head_on_stack(struct rcu_head * head)315d0df7a34SPaul E. McKenney static inline void init_rcu_head_on_stack(struct rcu_head *head) { }
destroy_rcu_head_on_stack(struct rcu_head * head)316d0df7a34SPaul E. McKenney static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
317551d55a9SMathieu Desnoyers #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
3184376030aSMathieu Desnoyers 
319c0d6d01bSPaul E. McKenney #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
320c0d6d01bSPaul E. McKenney bool rcu_lockdep_current_cpu_online(void);
321c0d6d01bSPaul E. McKenney #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
rcu_lockdep_current_cpu_online(void)32217a8c187SPaul E. McKenney static inline bool rcu_lockdep_current_cpu_online(void) { return true; }
323c0d6d01bSPaul E. McKenney #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
324c0d6d01bSPaul E. McKenney 
325891cd1f9SJakub Kicinski extern struct lockdep_map rcu_lock_map;
326891cd1f9SJakub Kicinski extern struct lockdep_map rcu_bh_lock_map;
327891cd1f9SJakub Kicinski extern struct lockdep_map rcu_sched_lock_map;
328891cd1f9SJakub Kicinski extern struct lockdep_map rcu_callback_map;
329891cd1f9SJakub Kicinski 
330bc33f24bSPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
331632ee200SPaul E. McKenney 
rcu_lock_acquire(struct lockdep_map * map)33200f49e57SFrederic Weisbecker static inline void rcu_lock_acquire(struct lockdep_map *map)
33300f49e57SFrederic Weisbecker {
334fb9edbe9SOleg Nesterov 	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
33500f49e57SFrederic Weisbecker }
33600f49e57SFrederic Weisbecker 
rcu_try_lock_acquire(struct lockdep_map * map)337ac0de86fSSebastian Andrzej Siewior static inline void rcu_try_lock_acquire(struct lockdep_map *map)
338ac0de86fSSebastian Andrzej Siewior {
339ac0de86fSSebastian Andrzej Siewior 	lock_acquire(map, 0, 1, 2, 0, NULL, _THIS_IP_);
340ac0de86fSSebastian Andrzej Siewior }
341ac0de86fSSebastian Andrzej Siewior 
rcu_lock_release(struct lockdep_map * map)34200f49e57SFrederic Weisbecker static inline void rcu_lock_release(struct lockdep_map *map)
34300f49e57SFrederic Weisbecker {
3445facae4fSQian Cai 	lock_release(map, _THIS_IP_);
34500f49e57SFrederic Weisbecker }
34600f49e57SFrederic Weisbecker 
347a235c091SIulia Manda int debug_lockdep_rcu_enabled(void);
34885b39d30SOleg Nesterov int rcu_read_lock_held(void);
349584dc4ceSTeodora Baluta int rcu_read_lock_bh_held(void);
350d5671f6bSDenys Vlasenko int rcu_read_lock_sched_held(void);
35128875945SJoel Fernandes (Google) int rcu_read_lock_any_held(void);
352632ee200SPaul E. McKenney 
353632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
354632ee200SPaul E. McKenney 
355d8ab29f8SPaul E. McKenney # define rcu_lock_acquire(a)		do { } while (0)
356ac0de86fSSebastian Andrzej Siewior # define rcu_try_lock_acquire(a)	do { } while (0)
357d8ab29f8SPaul E. McKenney # define rcu_lock_release(a)		do { } while (0)
358632ee200SPaul E. McKenney 
rcu_read_lock_held(void)359632ee200SPaul E. McKenney static inline int rcu_read_lock_held(void)
360632ee200SPaul E. McKenney {
361632ee200SPaul E. McKenney 	return 1;
362632ee200SPaul E. McKenney }
363632ee200SPaul E. McKenney 
rcu_read_lock_bh_held(void)364632ee200SPaul E. McKenney static inline int rcu_read_lock_bh_held(void)
365632ee200SPaul E. McKenney {
366632ee200SPaul E. McKenney 	return 1;
367632ee200SPaul E. McKenney }
368632ee200SPaul E. McKenney 
rcu_read_lock_sched_held(void)369632ee200SPaul E. McKenney static inline int rcu_read_lock_sched_held(void)
370632ee200SPaul E. McKenney {
371293e2421SBoqun Feng 	return !preemptible();
372632ee200SPaul E. McKenney }
37328875945SJoel Fernandes (Google) 
rcu_read_lock_any_held(void)37428875945SJoel Fernandes (Google) static inline int rcu_read_lock_any_held(void)
37528875945SJoel Fernandes (Google) {
37628875945SJoel Fernandes (Google) 	return !preemptible();
37728875945SJoel Fernandes (Google) }
37828875945SJoel Fernandes (Google) 
debug_lockdep_rcu_enabled(void)379f733615eSJohn Ogness static inline int debug_lockdep_rcu_enabled(void)
380f733615eSJohn Ogness {
381f733615eSJohn Ogness 	return 0;
382f733615eSJohn Ogness }
383f733615eSJohn Ogness 
384632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
385632ee200SPaul E. McKenney 
386632ee200SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
387632ee200SPaul E. McKenney 
3884221a991STetsuo Handa /**
389f78f5b90SPaul E. McKenney  * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
390f78f5b90SPaul E. McKenney  * @c: condition to check
391f78f5b90SPaul E. McKenney  * @s: informative message
3920cae5dedSPaul E. McKenney  *
3930cae5dedSPaul E. McKenney  * This checks debug_lockdep_rcu_enabled() before checking (c) to
3940cae5dedSPaul E. McKenney  * prevent early boot splats due to lockdep not yet being initialized,
3950cae5dedSPaul E. McKenney  * and rechecks it after checking (c) to prevent false-positive splats
3960cae5dedSPaul E. McKenney  * due to races with lockdep being disabled.  See commit 3066820034b5dd
3970cae5dedSPaul E. McKenney  * ("rcu: Reject RCU_LOCKDEP_WARN() false positives") for more detail.
398f78f5b90SPaul E. McKenney  */
399f78f5b90SPaul E. McKenney #define RCU_LOCKDEP_WARN(c, s)						\
400f78f5b90SPaul E. McKenney 	do {								\
40133def849SJoe Perches 		static bool __section(".data.unlikely") __warned;	\
4020cae5dedSPaul E. McKenney 		if (debug_lockdep_rcu_enabled() && (c) &&		\
4030cae5dedSPaul E. McKenney 		    debug_lockdep_rcu_enabled() && !__warned) {		\
404f78f5b90SPaul E. McKenney 			__warned = true;				\
405f78f5b90SPaul E. McKenney 			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
406f78f5b90SPaul E. McKenney 		}							\
407f78f5b90SPaul E. McKenney 	} while (0)
408f78f5b90SPaul E. McKenney 
40950406b98SPaul E. McKenney #if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU)
rcu_preempt_sleep_check(void)41050406b98SPaul E. McKenney static inline void rcu_preempt_sleep_check(void)
41150406b98SPaul E. McKenney {
412f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
4135cf05ad7SPaul E. McKenney 			 "Illegal context switch in RCU read-side critical section");
41450406b98SPaul E. McKenney }
41550406b98SPaul E. McKenney #else /* #ifdef CONFIG_PROVE_RCU */
rcu_preempt_sleep_check(void)416d0df7a34SPaul E. McKenney static inline void rcu_preempt_sleep_check(void) { }
41750406b98SPaul E. McKenney #endif /* #else #ifdef CONFIG_PROVE_RCU */
41850406b98SPaul E. McKenney 
419b3fbab05SPaul E. McKenney #define rcu_sleep_check()						\
420b3fbab05SPaul E. McKenney 	do {								\
42150406b98SPaul E. McKenney 		rcu_preempt_sleep_check();				\
422ba9e6cabSThomas Gleixner 		if (!IS_ENABLED(CONFIG_PREEMPT_RT))			\
423f78f5b90SPaul E. McKenney 		    RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),	\
42441f4abd9SJoe Perches 				 "Illegal context switch in RCU-bh read-side critical section"); \
425f78f5b90SPaul E. McKenney 		RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),	\
42641f4abd9SJoe Perches 				 "Illegal context switch in RCU-sched read-side critical section"); \
427b3fbab05SPaul E. McKenney 	} while (0)
428b3fbab05SPaul E. McKenney 
429ca5ecddfSPaul E. McKenney #else /* #ifdef CONFIG_PROVE_RCU */
430ca5ecddfSPaul E. McKenney 
43165e9eb1cSJakub Kicinski #define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c))
432b3fbab05SPaul E. McKenney #define rcu_sleep_check() do { } while (0)
433ca5ecddfSPaul E. McKenney 
434ca5ecddfSPaul E. McKenney #endif /* #else #ifdef CONFIG_PROVE_RCU */
435ca5ecddfSPaul E. McKenney 
436ca5ecddfSPaul E. McKenney /*
437ca5ecddfSPaul E. McKenney  * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
438ca5ecddfSPaul E. McKenney  * and rcu_assign_pointer().  Some of these could be folded into their
439ca5ecddfSPaul E. McKenney  * callers, but they are left separate in order to ease introduction of
4402bd8b1a2SPaul E. McKenney  * multiple pointers markings to match different RCU implementations
4412bd8b1a2SPaul E. McKenney  * (e.g., __srcu), should this make sense in the future.
442ca5ecddfSPaul E. McKenney  */
44353ecfba2SPaul E. McKenney 
44453ecfba2SPaul E. McKenney #ifdef __CHECKER__
445423a86a6SJoel Fernandes (Google) #define rcu_check_sparse(p, space) \
44653ecfba2SPaul E. McKenney 	((void)(((typeof(*p) space *)p) == p))
44753ecfba2SPaul E. McKenney #else /* #ifdef __CHECKER__ */
448423a86a6SJoel Fernandes (Google) #define rcu_check_sparse(p, space)
44953ecfba2SPaul E. McKenney #endif /* #else #ifdef __CHECKER__ */
45053ecfba2SPaul E. McKenney 
45124ba5301SChun-Hung Tseng #define __unrcu_pointer(p, local)					\
45224ba5301SChun-Hung Tseng ({									\
45324ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)(p);			\
45424ba5301SChun-Hung Tseng 	rcu_check_sparse(p, __rcu);					\
45524ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); 			\
45624ba5301SChun-Hung Tseng })
45776c8eaafSPaul E. McKenney /**
45876c8eaafSPaul E. McKenney  * unrcu_pointer - mark a pointer as not being RCU protected
45976c8eaafSPaul E. McKenney  * @p: pointer needing to lose its __rcu property
46076c8eaafSPaul E. McKenney  *
46176c8eaafSPaul E. McKenney  * Converts @p from an __rcu pointer to a __kernel pointer.
46276c8eaafSPaul E. McKenney  * This allows an __rcu pointer to be used with xchg() and friends.
46376c8eaafSPaul E. McKenney  */
46424ba5301SChun-Hung Tseng #define unrcu_pointer(p) __unrcu_pointer(p, __UNIQUE_ID(rcu))
46576c8eaafSPaul E. McKenney 
46624ba5301SChun-Hung Tseng #define __rcu_access_pointer(p, local, space) \
467ca5ecddfSPaul E. McKenney ({ \
46824ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
469423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
47024ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
471ca5ecddfSPaul E. McKenney })
47224ba5301SChun-Hung Tseng #define __rcu_dereference_check(p, local, c, space) \
473ca5ecddfSPaul E. McKenney ({ \
474ac59853cSPranith Kumar 	/* Dependency order vs. p above. */ \
47524ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
476f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
477423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
47824ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
479ca5ecddfSPaul E. McKenney })
48024ba5301SChun-Hung Tseng #define __rcu_dereference_protected(p, local, c, space) \
481ca5ecddfSPaul E. McKenney ({ \
482f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
483423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
484ca5ecddfSPaul E. McKenney 	((typeof(*p) __force __kernel *)(p)); \
485ca5ecddfSPaul E. McKenney })
48624ba5301SChun-Hung Tseng #define __rcu_dereference_raw(p, local) \
487995f1405SPaul E. McKenney ({ \
488995f1405SPaul E. McKenney 	/* Dependency order vs. p above. */ \
48924ba5301SChun-Hung Tseng 	typeof(p) local = READ_ONCE(p); \
49024ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
491995f1405SPaul E. McKenney })
49224ba5301SChun-Hung Tseng #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
493ca5ecddfSPaul E. McKenney 
494462225aeSPaul E. McKenney /**
495462225aeSPaul E. McKenney  * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
496462225aeSPaul E. McKenney  * @v: The value to statically initialize with.
497462225aeSPaul E. McKenney  */
498462225aeSPaul E. McKenney #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
499462225aeSPaul E. McKenney 
500462225aeSPaul E. McKenney /**
501462225aeSPaul E. McKenney  * rcu_assign_pointer() - assign to RCU-protected pointer
502462225aeSPaul E. McKenney  * @p: pointer to assign to
503462225aeSPaul E. McKenney  * @v: value to assign (publish)
504462225aeSPaul E. McKenney  *
505462225aeSPaul E. McKenney  * Assigns the specified value to the specified RCU-protected
506462225aeSPaul E. McKenney  * pointer, ensuring that any concurrent RCU readers will see
507462225aeSPaul E. McKenney  * any prior initialization.
508462225aeSPaul E. McKenney  *
509462225aeSPaul E. McKenney  * Inserts memory barriers on architectures that require them
510462225aeSPaul E. McKenney  * (which is most of them), and also prevents the compiler from
511462225aeSPaul E. McKenney  * reordering the code that initializes the structure after the pointer
512462225aeSPaul E. McKenney  * assignment.  More importantly, this call documents which pointers
513462225aeSPaul E. McKenney  * will be dereferenced by RCU read-side code.
514462225aeSPaul E. McKenney  *
515462225aeSPaul E. McKenney  * In some special cases, you may use RCU_INIT_POINTER() instead
516462225aeSPaul E. McKenney  * of rcu_assign_pointer().  RCU_INIT_POINTER() is a bit faster due
517462225aeSPaul E. McKenney  * to the fact that it does not constrain either the CPU or the compiler.
518462225aeSPaul E. McKenney  * That said, using RCU_INIT_POINTER() when you should have used
519462225aeSPaul E. McKenney  * rcu_assign_pointer() is a very bad thing that results in
520462225aeSPaul E. McKenney  * impossible-to-diagnose memory corruption.  So please be careful.
521462225aeSPaul E. McKenney  * See the RCU_INIT_POINTER() comment header for details.
522462225aeSPaul E. McKenney  *
523462225aeSPaul E. McKenney  * Note that rcu_assign_pointer() evaluates each of its arguments only
524462225aeSPaul E. McKenney  * once, appearances notwithstanding.  One of the "extra" evaluations
525462225aeSPaul E. McKenney  * is in typeof() and the other visible only to sparse (__CHECKER__),
526462225aeSPaul E. McKenney  * neither of which actually execute the argument.  As with most cpp
527462225aeSPaul E. McKenney  * macros, this execute-arguments-only-once property is important, so
528462225aeSPaul E. McKenney  * please be careful when making changes to rcu_assign_pointer() and the
529462225aeSPaul E. McKenney  * other macros that it invokes.
530462225aeSPaul E. McKenney  */
5313a37f727SPaul E. McKenney #define rcu_assign_pointer(p, v)					      \
5329129b017SAndrea Parri do {									      \
5333a37f727SPaul E. McKenney 	uintptr_t _r_a_p__v = (uintptr_t)(v);				      \
534423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, __rcu);					      \
5353a37f727SPaul E. McKenney 									      \
5363a37f727SPaul E. McKenney 	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL)	      \
5373a37f727SPaul E. McKenney 		WRITE_ONCE((p), (typeof(p))(_r_a_p__v));		      \
5383a37f727SPaul E. McKenney 	else								      \
5393a37f727SPaul E. McKenney 		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
5409129b017SAndrea Parri } while (0)
541ca5ecddfSPaul E. McKenney 
542632ee200SPaul E. McKenney /**
543a63fc6b7SPaul E. McKenney  * rcu_replace_pointer() - replace an RCU pointer, returning its old value
544a63fc6b7SPaul E. McKenney  * @rcu_ptr: RCU pointer, whose old value is returned
545a63fc6b7SPaul E. McKenney  * @ptr: regular pointer
546a63fc6b7SPaul E. McKenney  * @c: the lockdep conditions under which the dereference will take place
547a63fc6b7SPaul E. McKenney  *
548a63fc6b7SPaul E. McKenney  * Perform a replacement, where @rcu_ptr is an RCU-annotated
549a63fc6b7SPaul E. McKenney  * pointer and @c is the lockdep argument that is passed to the
550a63fc6b7SPaul E. McKenney  * rcu_dereference_protected() call used to read that pointer.  The old
551a63fc6b7SPaul E. McKenney  * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr.
552a63fc6b7SPaul E. McKenney  */
553a63fc6b7SPaul E. McKenney #define rcu_replace_pointer(rcu_ptr, ptr, c)				\
554a63fc6b7SPaul E. McKenney ({									\
555a63fc6b7SPaul E. McKenney 	typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c));	\
556a63fc6b7SPaul E. McKenney 	rcu_assign_pointer((rcu_ptr), (ptr));				\
557a63fc6b7SPaul E. McKenney 	__tmp;								\
558a63fc6b7SPaul E. McKenney })
559a63fc6b7SPaul E. McKenney 
560a63fc6b7SPaul E. McKenney /**
561ca5ecddfSPaul E. McKenney  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
562ca5ecddfSPaul E. McKenney  * @p: The pointer to read
563ca5ecddfSPaul E. McKenney  *
564ca5ecddfSPaul E. McKenney  * Return the value of the specified RCU-protected pointer, but omit the
565137f61f6SPaul E. McKenney  * lockdep checks for being in an RCU read-side critical section.  This is
566137f61f6SPaul E. McKenney  * useful when the value of this pointer is accessed, but the pointer is
567137f61f6SPaul E. McKenney  * not dereferenced, for example, when testing an RCU-protected pointer
568137f61f6SPaul E. McKenney  * against NULL.  Although rcu_access_pointer() may also be used in cases
569137f61f6SPaul E. McKenney  * where update-side locks prevent the value of the pointer from changing,
570137f61f6SPaul E. McKenney  * you should instead use rcu_dereference_protected() for this use case.
571d8f3f583SPaul E. McKenney  * Within an RCU read-side critical section, there is little reason to
572d8f3f583SPaul E. McKenney  * use rcu_access_pointer().
573d8f3f583SPaul E. McKenney  *
574d8f3f583SPaul E. McKenney  * It is usually best to test the rcu_access_pointer() return value
575d8f3f583SPaul E. McKenney  * directly in order to avoid accidental dereferences being introduced
576d8f3f583SPaul E. McKenney  * by later inattentive changes.  In other words, assigning the
577d8f3f583SPaul E. McKenney  * rcu_access_pointer() return value to a local variable results in an
578d8f3f583SPaul E. McKenney  * accident waiting to happen.
5795e1ee6e1SPaul E. McKenney  *
5805e1ee6e1SPaul E. McKenney  * It is also permissible to use rcu_access_pointer() when read-side
581d8f3f583SPaul E. McKenney  * access to the pointer was removed at least one grace period ago, as is
582d8f3f583SPaul E. McKenney  * the case in the context of the RCU callback that is freeing up the data,
583d8f3f583SPaul E. McKenney  * or after a synchronize_rcu() returns.  This can be useful when tearing
584d8f3f583SPaul E. McKenney  * down multi-linked structures after a grace period has elapsed.  However,
585d8f3f583SPaul E. McKenney  * rcu_dereference_protected() is normally preferred for this use case.
586ca5ecddfSPaul E. McKenney  */
58724ba5301SChun-Hung Tseng #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
588ca5ecddfSPaul E. McKenney 
589ca5ecddfSPaul E. McKenney /**
590ca5ecddfSPaul E. McKenney  * rcu_dereference_check() - rcu_dereference with debug checking
591c08c68ddSDavid Howells  * @p: The pointer to read, prior to dereferencing
592c08c68ddSDavid Howells  * @c: The conditions under which the dereference will take place
593632ee200SPaul E. McKenney  *
594c08c68ddSDavid Howells  * Do an rcu_dereference(), but check that the conditions under which the
595ca5ecddfSPaul E. McKenney  * dereference will take place are correct.  Typically the conditions
596ca5ecddfSPaul E. McKenney  * indicate the various locking conditions that should be held at that
597ca5ecddfSPaul E. McKenney  * point.  The check should return true if the conditions are satisfied.
598ca5ecddfSPaul E. McKenney  * An implicit check for being in an RCU read-side critical section
599ca5ecddfSPaul E. McKenney  * (rcu_read_lock()) is included.
600c08c68ddSDavid Howells  *
601c08c68ddSDavid Howells  * For example:
602c08c68ddSDavid Howells  *
603ca5ecddfSPaul E. McKenney  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
604c08c68ddSDavid Howells  *
605c08c68ddSDavid Howells  * could be used to indicate to lockdep that foo->bar may only be dereferenced
606ca5ecddfSPaul E. McKenney  * if either rcu_read_lock() is held, or that the lock required to replace
607c08c68ddSDavid Howells  * the bar struct at foo->bar is held.
608c08c68ddSDavid Howells  *
609c08c68ddSDavid Howells  * Note that the list of conditions may also include indications of when a lock
610c08c68ddSDavid Howells  * need not be held, for example during initialisation or destruction of the
611c08c68ddSDavid Howells  * target struct:
612c08c68ddSDavid Howells  *
613ca5ecddfSPaul E. McKenney  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
614c08c68ddSDavid Howells  *					      atomic_read(&foo->usage) == 0);
615ca5ecddfSPaul E. McKenney  *
616ca5ecddfSPaul E. McKenney  * Inserts memory barriers on architectures that require them
617ca5ecddfSPaul E. McKenney  * (currently only the Alpha), prevents the compiler from refetching
618ca5ecddfSPaul E. McKenney  * (and from merging fetches), and, more importantly, documents exactly
619ca5ecddfSPaul E. McKenney  * which pointers are protected by RCU and checks that the pointer is
620ca5ecddfSPaul E. McKenney  * annotated as __rcu.
621632ee200SPaul E. McKenney  */
622632ee200SPaul E. McKenney #define rcu_dereference_check(p, c) \
62324ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
62424ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_held(), __rcu)
625632ee200SPaul E. McKenney 
626b62730baSPaul E. McKenney /**
627ca5ecddfSPaul E. McKenney  * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
628ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
629ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
630ca5ecddfSPaul E. McKenney  *
6311893afd6SPaul E. McKenney  * This is the RCU-bh counterpart to rcu_dereference_check().  However,
6321893afd6SPaul E. McKenney  * please note that starting in v5.0 kernels, vanilla RCU grace periods
6331893afd6SPaul E. McKenney  * wait for local_bh_disable() regions of code in addition to regions of
6341893afd6SPaul E. McKenney  * code demarked by rcu_read_lock() and rcu_read_unlock().  This means
6351893afd6SPaul E. McKenney  * that synchronize_rcu(), call_rcu, and friends all take not only
6361893afd6SPaul E. McKenney  * rcu_read_lock() but also rcu_read_lock_bh() into account.
637ca5ecddfSPaul E. McKenney  */
638ca5ecddfSPaul E. McKenney #define rcu_dereference_bh_check(p, c) \
63924ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
64024ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_bh_held(), __rcu)
641ca5ecddfSPaul E. McKenney 
642ca5ecddfSPaul E. McKenney /**
643ca5ecddfSPaul E. McKenney  * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
644ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
645ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
646ca5ecddfSPaul E. McKenney  *
647ca5ecddfSPaul E. McKenney  * This is the RCU-sched counterpart to rcu_dereference_check().
6481893afd6SPaul E. McKenney  * However, please note that starting in v5.0 kernels, vanilla RCU grace
6491893afd6SPaul E. McKenney  * periods wait for preempt_disable() regions of code in addition to
6501893afd6SPaul E. McKenney  * regions of code demarked by rcu_read_lock() and rcu_read_unlock().
6511893afd6SPaul E. McKenney  * This means that synchronize_rcu(), call_rcu, and friends all take not
6521893afd6SPaul E. McKenney  * only rcu_read_lock() but also rcu_read_lock_sched() into account.
653ca5ecddfSPaul E. McKenney  */
654ca5ecddfSPaul E. McKenney #define rcu_dereference_sched_check(p, c) \
65524ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
65624ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_sched_held(), \
657ca5ecddfSPaul E. McKenney 				__rcu)
658ca5ecddfSPaul E. McKenney 
65912bcbe66SSteven Rostedt /*
66012bcbe66SSteven Rostedt  * The tracing infrastructure traces RCU (we want that), but unfortunately
66112bcbe66SSteven Rostedt  * some of the RCU checks causes tracing to lock up the system.
66212bcbe66SSteven Rostedt  *
663f039f0afSAlexey Kardashevskiy  * The no-tracing version of rcu_dereference_raw() must not call
66412bcbe66SSteven Rostedt  * rcu_read_lock_held().
66512bcbe66SSteven Rostedt  */
66624ba5301SChun-Hung Tseng #define rcu_dereference_raw_check(p) \
66724ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), 1, __rcu)
66812bcbe66SSteven Rostedt 
669ca5ecddfSPaul E. McKenney /**
670ca5ecddfSPaul E. McKenney  * rcu_dereference_protected() - fetch RCU pointer when updates prevented
671ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
672ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
673b62730baSPaul E. McKenney  *
674b62730baSPaul E. McKenney  * Return the value of the specified RCU-protected pointer, but omit
675137f61f6SPaul E. McKenney  * the READ_ONCE().  This is useful in cases where update-side locks
676137f61f6SPaul E. McKenney  * prevent the value of the pointer from changing.  Please note that this
677137f61f6SPaul E. McKenney  * primitive does *not* prevent the compiler from repeating this reference
678137f61f6SPaul E. McKenney  * or combining it with other references, so it should not be used without
679137f61f6SPaul E. McKenney  * protection of appropriate locks.
680ca5ecddfSPaul E. McKenney  *
681ca5ecddfSPaul E. McKenney  * This function is only for update-side use.  Using this function
682ca5ecddfSPaul E. McKenney  * when protected only by rcu_read_lock() will result in infrequent
683ca5ecddfSPaul E. McKenney  * but very ugly failures.
684b62730baSPaul E. McKenney  */
685b62730baSPaul E. McKenney #define rcu_dereference_protected(p, c) \
68624ba5301SChun-Hung Tseng 	__rcu_dereference_protected((p), __UNIQUE_ID(rcu), (c), __rcu)
687bc33f24bSPaul E. McKenney 
688ca5ecddfSPaul E. McKenney 
689ca5ecddfSPaul E. McKenney /**
690ca5ecddfSPaul E. McKenney  * rcu_dereference() - fetch RCU-protected pointer for dereferencing
691ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
692ca5ecddfSPaul E. McKenney  *
693ca5ecddfSPaul E. McKenney  * This is a simple wrapper around rcu_dereference_check().
694ca5ecddfSPaul E. McKenney  */
695ca5ecddfSPaul E. McKenney #define rcu_dereference(p) rcu_dereference_check(p, 0)
696ca5ecddfSPaul E. McKenney 
697ca5ecddfSPaul E. McKenney /**
698ca5ecddfSPaul E. McKenney  * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
699ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
700ca5ecddfSPaul E. McKenney  *
701ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.
702ca5ecddfSPaul E. McKenney  */
703ca5ecddfSPaul E. McKenney #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
704ca5ecddfSPaul E. McKenney 
705ca5ecddfSPaul E. McKenney /**
706ca5ecddfSPaul E. McKenney  * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
707ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
708ca5ecddfSPaul E. McKenney  *
709ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.
710ca5ecddfSPaul E. McKenney  */
711ca5ecddfSPaul E. McKenney #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
712ca5ecddfSPaul E. McKenney 
713ca5ecddfSPaul E. McKenney /**
714c3ac7cf1SPaul E. McKenney  * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
715c3ac7cf1SPaul E. McKenney  * @p: The pointer to hand off
716c3ac7cf1SPaul E. McKenney  *
717c3ac7cf1SPaul E. McKenney  * This is simply an identity function, but it documents where a pointer
718c3ac7cf1SPaul E. McKenney  * is handed off from RCU to some other synchronization mechanism, for
719c3ac7cf1SPaul E. McKenney  * example, reference counting or locking.  In C11, it would map to
7201445e917SMauro Carvalho Chehab  * kill_dependency().  It could be used as follows::
7211445e917SMauro Carvalho Chehab  *
722c3ac7cf1SPaul E. McKenney  *	rcu_read_lock();
723c3ac7cf1SPaul E. McKenney  *	p = rcu_dereference(gp);
724c3ac7cf1SPaul E. McKenney  *	long_lived = is_long_lived(p);
725c3ac7cf1SPaul E. McKenney  *	if (long_lived) {
726c3ac7cf1SPaul E. McKenney  *		if (!atomic_inc_not_zero(p->refcnt))
727c3ac7cf1SPaul E. McKenney  *			long_lived = false;
728c3ac7cf1SPaul E. McKenney  *		else
729c3ac7cf1SPaul E. McKenney  *			p = rcu_pointer_handoff(p);
730c3ac7cf1SPaul E. McKenney  *	}
731c3ac7cf1SPaul E. McKenney  *	rcu_read_unlock();
732c3ac7cf1SPaul E. McKenney  */
733c3ac7cf1SPaul E. McKenney #define rcu_pointer_handoff(p) (p)
734c3ac7cf1SPaul E. McKenney 
735c3ac7cf1SPaul E. McKenney /**
736ca5ecddfSPaul E. McKenney  * rcu_read_lock() - mark the beginning of an RCU read-side critical section
7371da177e4SLinus Torvalds  *
7389b06e818SPaul E. McKenney  * When synchronize_rcu() is invoked on one CPU while other CPUs
7391da177e4SLinus Torvalds  * are within RCU read-side critical sections, then the
7409b06e818SPaul E. McKenney  * synchronize_rcu() is guaranteed to block until after all the other
7411da177e4SLinus Torvalds  * CPUs exit their critical sections.  Similarly, if call_rcu() is invoked
7421da177e4SLinus Torvalds  * on one CPU while other CPUs are within RCU read-side critical
7431da177e4SLinus Torvalds  * sections, invocation of the corresponding RCU callback is deferred
7441da177e4SLinus Torvalds  * until after the all the other CPUs exit their critical sections.
7451da177e4SLinus Torvalds  *
7461893afd6SPaul E. McKenney  * In v5.0 and later kernels, synchronize_rcu() and call_rcu() also
7471893afd6SPaul E. McKenney  * wait for regions of code with preemption disabled, including regions of
7481893afd6SPaul E. McKenney  * code with interrupts or softirqs disabled.  In pre-v5.0 kernels, which
7491893afd6SPaul E. McKenney  * define synchronize_sched(), only code enclosed within rcu_read_lock()
7501893afd6SPaul E. McKenney  * and rcu_read_unlock() are guaranteed to be waited for.
7511893afd6SPaul E. McKenney  *
7521da177e4SLinus Torvalds  * Note, however, that RCU callbacks are permitted to run concurrently
75377d8485aSPaul E. McKenney  * with new RCU read-side critical sections.  One way that this can happen
7541da177e4SLinus Torvalds  * is via the following sequence of events: (1) CPU 0 enters an RCU
7551da177e4SLinus Torvalds  * read-side critical section, (2) CPU 1 invokes call_rcu() to register
7561da177e4SLinus Torvalds  * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
7571da177e4SLinus Torvalds  * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
7581da177e4SLinus Torvalds  * callback is invoked.  This is legal, because the RCU read-side critical
7591da177e4SLinus Torvalds  * section that was running concurrently with the call_rcu() (and which
7601da177e4SLinus Torvalds  * therefore might be referencing something that the corresponding RCU
7611da177e4SLinus Torvalds  * callback would free up) has completed before the corresponding
7621da177e4SLinus Torvalds  * RCU callback is invoked.
7631da177e4SLinus Torvalds  *
7641da177e4SLinus Torvalds  * RCU read-side critical sections may be nested.  Any deferred actions
7651da177e4SLinus Torvalds  * will be deferred until the outermost RCU read-side critical section
7661da177e4SLinus Torvalds  * completes.
7671da177e4SLinus Torvalds  *
7689079fd7cSPaul E. McKenney  * You can avoid reading and understanding the next paragraph by
7699079fd7cSPaul E. McKenney  * following this rule: don't put anything in an rcu_read_lock() RCU
77090326f05SSebastian Andrzej Siewior  * read-side critical section that would block in a !PREEMPTION kernel.
7719079fd7cSPaul E. McKenney  * But if you want the full story, read on!
7729079fd7cSPaul E. McKenney  *
773b3e627d3SLai Jiangshan  * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU),
774ab74fdfdSPaul E. McKenney  * it is illegal to block while in an RCU read-side critical section.
77501b1d88bSThomas Gleixner  * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION
776ab74fdfdSPaul E. McKenney  * kernel builds, RCU read-side critical sections may be preempted,
777ab74fdfdSPaul E. McKenney  * but explicit blocking is illegal.  Finally, in preemptible RCU
778ab74fdfdSPaul E. McKenney  * implementations in real-time (with -rt patchset) kernel builds, RCU
779ab74fdfdSPaul E. McKenney  * read-side critical sections may be preempted and they may also block, but
780ab74fdfdSPaul E. McKenney  * only when acquiring spinlocks that are subject to priority inheritance.
7811da177e4SLinus Torvalds  */
rcu_read_lock(void)7826da9f775SWaiman Long static __always_inline void rcu_read_lock(void)
783bc33f24bSPaul E. McKenney {
784bc33f24bSPaul E. McKenney 	__rcu_read_lock();
785bc33f24bSPaul E. McKenney 	__acquire(RCU);
786d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_lock_map);
787f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
788bde23c68SHeiko Carstens 			 "rcu_read_lock() used illegally while idle");
789bc33f24bSPaul E. McKenney }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*
7921da177e4SLinus Torvalds  * So where is rcu_write_lock()?  It does not exist, as there is no
7931da177e4SLinus Torvalds  * way for writers to lock out RCU readers.  This is a feature, not
7941da177e4SLinus Torvalds  * a bug -- this property is what provides RCU's performance benefits.
7951da177e4SLinus Torvalds  * Of course, writers must coordinate with each other.  The normal
7961da177e4SLinus Torvalds  * spinlock primitives work well for this, but any other technique may be
7971da177e4SLinus Torvalds  * used as well.  RCU does not care how the writers keep out of each
7981da177e4SLinus Torvalds  * others' way, as long as they do so.
7991da177e4SLinus Torvalds  */
8003d76c082SPaul E. McKenney 
8013d76c082SPaul E. McKenney /**
802ca5ecddfSPaul E. McKenney  * rcu_read_unlock() - marks the end of an RCU read-side critical section.
8033d76c082SPaul E. McKenney  *
80402238460SPaul E. McKenney  * In almost all situations, rcu_read_unlock() is immune from deadlock.
80502238460SPaul E. McKenney  * In recent kernels that have consolidated synchronize_sched() and
80602238460SPaul E. McKenney  * synchronize_rcu_bh() into synchronize_rcu(), this deadlock immunity
80702238460SPaul E. McKenney  * also extends to the scheduler's runqueue and priority-inheritance
80802238460SPaul E. McKenney  * spinlocks, courtesy of the quiescent-state deferral that is carried
80902238460SPaul E. McKenney  * out when rcu_read_unlock() is invoked with interrupts disabled.
810f27bc487SPaul E. McKenney  *
8113d76c082SPaul E. McKenney  * See rcu_read_lock() for more information.
8123d76c082SPaul E. McKenney  */
rcu_read_unlock(void)813bc33f24bSPaul E. McKenney static inline void rcu_read_unlock(void)
814bc33f24bSPaul E. McKenney {
815f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
816bde23c68SHeiko Carstens 			 "rcu_read_unlock() used illegally while idle");
817bc33f24bSPaul E. McKenney 	__release(RCU);
818bc33f24bSPaul E. McKenney 	__rcu_read_unlock();
819d24209bbSPaul E. McKenney 	rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
820bc33f24bSPaul E. McKenney }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds /**
823ca5ecddfSPaul E. McKenney  * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
8241da177e4SLinus Torvalds  *
8251893afd6SPaul E. McKenney  * This is equivalent to rcu_read_lock(), but also disables softirqs.
8261893afd6SPaul E. McKenney  * Note that anything else that disables softirqs can also serve as an RCU
8271893afd6SPaul E. McKenney  * read-side critical section.  However, please note that this equivalence
8281893afd6SPaul E. McKenney  * applies only to v5.0 and later.  Before v5.0, rcu_read_lock() and
8291893afd6SPaul E. McKenney  * rcu_read_lock_bh() were unrelated.
8303842a083SPaul E. McKenney  *
8313842a083SPaul E. McKenney  * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
8323842a083SPaul E. McKenney  * must occur in the same context, for example, it is illegal to invoke
8333842a083SPaul E. McKenney  * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
8343842a083SPaul E. McKenney  * was invoked from some other task.
8351da177e4SLinus Torvalds  */
rcu_read_lock_bh(void)836bc33f24bSPaul E. McKenney static inline void rcu_read_lock_bh(void)
837bc33f24bSPaul E. McKenney {
8386206ab9bSPaul E. McKenney 	local_bh_disable();
839bc33f24bSPaul E. McKenney 	__acquire(RCU_BH);
840d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_bh_lock_map);
841f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
842bde23c68SHeiko Carstens 			 "rcu_read_lock_bh() used illegally while idle");
843bc33f24bSPaul E. McKenney }
8441da177e4SLinus Torvalds 
845000601bbSTobias Klauser /**
846000601bbSTobias Klauser  * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section
8471da177e4SLinus Torvalds  *
8481da177e4SLinus Torvalds  * See rcu_read_lock_bh() for more information.
8491da177e4SLinus Torvalds  */
rcu_read_unlock_bh(void)850bc33f24bSPaul E. McKenney static inline void rcu_read_unlock_bh(void)
851bc33f24bSPaul E. McKenney {
852f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
853bde23c68SHeiko Carstens 			 "rcu_read_unlock_bh() used illegally while idle");
854d8ab29f8SPaul E. McKenney 	rcu_lock_release(&rcu_bh_lock_map);
855bc33f24bSPaul E. McKenney 	__release(RCU_BH);
8566206ab9bSPaul E. McKenney 	local_bh_enable();
857bc33f24bSPaul E. McKenney }
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds /**
860ca5ecddfSPaul E. McKenney  * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
8611c50b728SMathieu Desnoyers  *
8621893afd6SPaul E. McKenney  * This is equivalent to rcu_read_lock(), but also disables preemption.
8631893afd6SPaul E. McKenney  * Read-side critical sections can also be introduced by anything else that
8641893afd6SPaul E. McKenney  * disables preemption, including local_irq_disable() and friends.  However,
8651893afd6SPaul E. McKenney  * please note that the equivalence to rcu_read_lock() applies only to
8661893afd6SPaul E. McKenney  * v5.0 and later.  Before v5.0, rcu_read_lock() and rcu_read_lock_sched()
8671893afd6SPaul E. McKenney  * were unrelated.
8683842a083SPaul E. McKenney  *
8693842a083SPaul E. McKenney  * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
8703842a083SPaul E. McKenney  * must occur in the same context, for example, it is illegal to invoke
8713842a083SPaul E. McKenney  * rcu_read_unlock_sched() from process context if the matching
8723842a083SPaul E. McKenney  * rcu_read_lock_sched() was invoked from an NMI handler.
8731c50b728SMathieu Desnoyers  */
rcu_read_lock_sched(void)874d6714c22SPaul E. McKenney static inline void rcu_read_lock_sched(void)
875d6714c22SPaul E. McKenney {
876d6714c22SPaul E. McKenney 	preempt_disable();
877bc33f24bSPaul E. McKenney 	__acquire(RCU_SCHED);
878d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_sched_lock_map);
879f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
880bde23c68SHeiko Carstens 			 "rcu_read_lock_sched() used illegally while idle");
881d6714c22SPaul E. McKenney }
8821eba8f84SPaul E. McKenney 
8831eba8f84SPaul E. McKenney /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_lock_sched_notrace(void)8847c614d64SPaul E. McKenney static inline notrace void rcu_read_lock_sched_notrace(void)
885d6714c22SPaul E. McKenney {
886d6714c22SPaul E. McKenney 	preempt_disable_notrace();
887bc33f24bSPaul E. McKenney 	__acquire(RCU_SCHED);
888d6714c22SPaul E. McKenney }
8891c50b728SMathieu Desnoyers 
890000601bbSTobias Klauser /**
891000601bbSTobias Klauser  * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section
8921c50b728SMathieu Desnoyers  *
893000601bbSTobias Klauser  * See rcu_read_lock_sched() for more information.
8941c50b728SMathieu Desnoyers  */
rcu_read_unlock_sched(void)895d6714c22SPaul E. McKenney static inline void rcu_read_unlock_sched(void)
896d6714c22SPaul E. McKenney {
897f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
898bde23c68SHeiko Carstens 			 "rcu_read_unlock_sched() used illegally while idle");
899d8ab29f8SPaul E. McKenney 	rcu_lock_release(&rcu_sched_lock_map);
900bc33f24bSPaul E. McKenney 	__release(RCU_SCHED);
901d6714c22SPaul E. McKenney 	preempt_enable();
902d6714c22SPaul E. McKenney }
9031eba8f84SPaul E. McKenney 
9041eba8f84SPaul E. McKenney /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_unlock_sched_notrace(void)9057c614d64SPaul E. McKenney static inline notrace void rcu_read_unlock_sched_notrace(void)
906d6714c22SPaul E. McKenney {
907bc33f24bSPaul E. McKenney 	__release(RCU_SCHED);
908d6714c22SPaul E. McKenney 	preempt_enable_notrace();
909d6714c22SPaul E. McKenney }
9101c50b728SMathieu Desnoyers 
9111c50b728SMathieu Desnoyers /**
912ca5ecddfSPaul E. McKenney  * RCU_INIT_POINTER() - initialize an RCU protected pointer
91327fdb35fSPaul E. McKenney  * @p: The pointer to be initialized.
91427fdb35fSPaul E. McKenney  * @v: The value to initialized the pointer to.
915ca5ecddfSPaul E. McKenney  *
9166846c0c5SPaul E. McKenney  * Initialize an RCU-protected pointer in special cases where readers
9176846c0c5SPaul E. McKenney  * do not need ordering constraints on the CPU or the compiler.  These
9186846c0c5SPaul E. McKenney  * special cases are:
9196846c0c5SPaul E. McKenney  *
92027fdb35fSPaul E. McKenney  * 1.	This use of RCU_INIT_POINTER() is NULLing out the pointer *or*
9216846c0c5SPaul E. McKenney  * 2.	The caller has taken whatever steps are required to prevent
92227fdb35fSPaul E. McKenney  *	RCU readers from concurrently accessing this pointer *or*
9236846c0c5SPaul E. McKenney  * 3.	The referenced data structure has already been exposed to
92427fdb35fSPaul E. McKenney  *	readers either at compile time or via rcu_assign_pointer() *and*
92527fdb35fSPaul E. McKenney  *
92627fdb35fSPaul E. McKenney  *	a.	You have not made *any* reader-visible changes to
92727fdb35fSPaul E. McKenney  *		this structure since then *or*
9286846c0c5SPaul E. McKenney  *	b.	It is OK for readers accessing this structure from its
9296846c0c5SPaul E. McKenney  *		new location to see the old state of the structure.  (For
9306846c0c5SPaul E. McKenney  *		example, the changes were to statistical counters or to
9316846c0c5SPaul E. McKenney  *		other state where exact synchronization is not required.)
9326846c0c5SPaul E. McKenney  *
9336846c0c5SPaul E. McKenney  * Failure to follow these rules governing use of RCU_INIT_POINTER() will
9346846c0c5SPaul E. McKenney  * result in impossible-to-diagnose memory corruption.  As in the structures
9356846c0c5SPaul E. McKenney  * will look OK in crash dumps, but any concurrent RCU readers might
9366846c0c5SPaul E. McKenney  * see pre-initialized values of the referenced data structure.  So
9376846c0c5SPaul E. McKenney  * please be very careful how you use RCU_INIT_POINTER()!!!
9386846c0c5SPaul E. McKenney  *
9396846c0c5SPaul E. McKenney  * If you are creating an RCU-protected linked structure that is accessed
9406846c0c5SPaul E. McKenney  * by a single external-to-structure RCU-protected pointer, then you may
9416846c0c5SPaul E. McKenney  * use RCU_INIT_POINTER() to initialize the internal RCU-protected
9426846c0c5SPaul E. McKenney  * pointers, but you must use rcu_assign_pointer() to initialize the
94327fdb35fSPaul E. McKenney  * external-to-structure pointer *after* you have completely initialized
9446846c0c5SPaul E. McKenney  * the reader-accessible portions of the linked structure.
94571a9b269SPaul E. McKenney  *
94671a9b269SPaul E. McKenney  * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
94771a9b269SPaul E. McKenney  * ordering guarantees for either the CPU or the compiler.
948ca5ecddfSPaul E. McKenney  */
949ca5ecddfSPaul E. McKenney #define RCU_INIT_POINTER(p, v) \
950d1b88eb9SPaul E. McKenney 	do { \
951423a86a6SJoel Fernandes (Google) 		rcu_check_sparse(p, __rcu); \
952155d1d12SPeter Zijlstra 		WRITE_ONCE(p, RCU_INITIALIZER(v)); \
953d1b88eb9SPaul E. McKenney 	} while (0)
9541da177e4SLinus Torvalds 
955172708d0SPaul E. McKenney /**
956172708d0SPaul E. McKenney  * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
95727fdb35fSPaul E. McKenney  * @p: The pointer to be initialized.
95827fdb35fSPaul E. McKenney  * @v: The value to initialized the pointer to.
959172708d0SPaul E. McKenney  *
960172708d0SPaul E. McKenney  * GCC-style initialization for an RCU-protected pointer in a structure field.
961172708d0SPaul E. McKenney  */
962172708d0SPaul E. McKenney #define RCU_POINTER_INITIALIZER(p, v) \
963462225aeSPaul E. McKenney 		.p = RCU_INITIALIZER(v)
9649ab1544eSLai Jiangshan 
965d8169d4cSJan Engelhardt /*
966d8169d4cSJan Engelhardt  * Does the specified offset indicate that the corresponding rcu_head
967c408b215SUladzislau Rezki (Sony)  * structure can be handled by kvfree_rcu()?
968d8169d4cSJan Engelhardt  */
969c408b215SUladzislau Rezki (Sony) #define __is_kvfree_rcu_offset(offset) ((offset) < 4096)
970d8169d4cSJan Engelhardt 
9719ab1544eSLai Jiangshan /**
9729ab1544eSLai Jiangshan  * kfree_rcu() - kfree an object after a grace period.
9737e3f926bSUladzislau Rezki (Sony)  * @ptr: pointer to kfree for double-argument invocations.
9747e3f926bSUladzislau Rezki (Sony)  * @rhf: the name of the struct rcu_head within the type of @ptr.
9759ab1544eSLai Jiangshan  *
9769ab1544eSLai Jiangshan  * Many rcu callbacks functions just call kfree() on the base structure.
9779ab1544eSLai Jiangshan  * These functions are trivial, but their size adds up, and furthermore
9789ab1544eSLai Jiangshan  * when they are used in a kernel module, that module must invoke the
9799ab1544eSLai Jiangshan  * high-latency rcu_barrier() function at module-unload time.
9809ab1544eSLai Jiangshan  *
9819ab1544eSLai Jiangshan  * The kfree_rcu() function handles this issue.  Rather than encoding a
9829ab1544eSLai Jiangshan  * function address in the embedded rcu_head structure, kfree_rcu() instead
9839ab1544eSLai Jiangshan  * encodes the offset of the rcu_head structure within the base structure.
9849ab1544eSLai Jiangshan  * Because the functions are not allowed in the low-order 4096 bytes of
9859ab1544eSLai Jiangshan  * kernel virtual memory, offsets up to 4095 bytes can be accommodated.
9869ab1544eSLai Jiangshan  * If the offset is larger than 4095 bytes, a compile-time error will
9875ea5d1edSUladzislau Rezki (Sony)  * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can
9889ab1544eSLai Jiangshan  * either fall back to use of call_rcu() or rearrange the structure to
9899ab1544eSLai Jiangshan  * position the rcu_head structure into the first 4096 bytes.
9909ab1544eSLai Jiangshan  *
991ae65a521SVlastimil Babka  * The object to be freed can be allocated either by kmalloc() or
992ae65a521SVlastimil Babka  * kmem_cache_alloc().
993ae65a521SVlastimil Babka  *
994ae65a521SVlastimil Babka  * Note that the allowable offset might decrease in the future.
995d8169d4cSJan Engelhardt  *
996d8169d4cSJan Engelhardt  * The BUILD_BUG_ON check must not involve any function calls, hence the
997d8169d4cSJan Engelhardt  * checks are done in macros here.
9989ab1544eSLai Jiangshan  */
9997e3f926bSUladzislau Rezki (Sony) #define kfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
10007e3f926bSUladzislau Rezki (Sony) #define kvfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
10010edd1b17SPaul E. McKenney 
1002ce4dce12SUladzislau Rezki (Sony) /**
10037e3f926bSUladzislau Rezki (Sony)  * kfree_rcu_mightsleep() - kfree an object after a grace period.
10047e3f926bSUladzislau Rezki (Sony)  * @ptr: pointer to kfree for single-argument invocations.
10051835f475SUladzislau Rezki (Sony)  *
10061835f475SUladzislau Rezki (Sony)  * When it comes to head-less variant, only one argument
10071835f475SUladzislau Rezki (Sony)  * is passed and that is just a pointer which has to be
10081835f475SUladzislau Rezki (Sony)  * freed after a grace period. Therefore the semantic is
10091835f475SUladzislau Rezki (Sony)  *
10107e3f926bSUladzislau Rezki (Sony)  *     kfree_rcu_mightsleep(ptr);
10111835f475SUladzislau Rezki (Sony)  *
1012150154aaSUladzislau Rezki (Sony)  * where @ptr is the pointer to be freed by kvfree().
10131835f475SUladzislau Rezki (Sony)  *
10141835f475SUladzislau Rezki (Sony)  * Please note, head-less way of freeing is permitted to
10151835f475SUladzislau Rezki (Sony)  * use from a context that has to follow might_sleep()
10161835f475SUladzislau Rezki (Sony)  * annotation. Otherwise, please switch and embed the
10171835f475SUladzislau Rezki (Sony)  * rcu_head structure within the type of @ptr.
1018ce4dce12SUladzislau Rezki (Sony)  */
10197e3f926bSUladzislau Rezki (Sony) #define kfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1020608723c4SUladzislau Rezki (Sony) #define kvfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1021608723c4SUladzislau Rezki (Sony) 
10225130b8fdSUladzislau Rezki (Sony) #define kvfree_rcu_arg_2(ptr, rhf)					\
10235130b8fdSUladzislau Rezki (Sony) do {									\
10245130b8fdSUladzislau Rezki (Sony) 	typeof (ptr) ___p = (ptr);					\
10255130b8fdSUladzislau Rezki (Sony) 									\
10265ea5d1edSUladzislau Rezki (Sony) 	if (___p) {									\
10275ea5d1edSUladzislau Rezki (Sony) 		BUILD_BUG_ON(!__is_kvfree_rcu_offset(offsetof(typeof(*(ptr)), rhf)));	\
102804a522b7SUladzislau Rezki (Sony) 		kvfree_call_rcu(&((___p)->rhf), (void *) (___p));			\
10295ea5d1edSUladzislau Rezki (Sony) 	}										\
10305130b8fdSUladzislau Rezki (Sony) } while (0)
10315130b8fdSUladzislau Rezki (Sony) 
10321835f475SUladzislau Rezki (Sony) #define kvfree_rcu_arg_1(ptr)					\
10331835f475SUladzislau Rezki (Sony) do {								\
10341835f475SUladzislau Rezki (Sony) 	typeof(ptr) ___p = (ptr);				\
10351835f475SUladzislau Rezki (Sony) 								\
10361835f475SUladzislau Rezki (Sony) 	if (___p)						\
103704a522b7SUladzislau Rezki (Sony) 		kvfree_call_rcu(NULL, (void *) (___p));		\
10381835f475SUladzislau Rezki (Sony) } while (0)
1039ce4dce12SUladzislau Rezki (Sony) 
1040274529baSPaul E. McKenney /*
1041d85b62f1SPaul E. McKenney  * Place this after a lock-acquisition primitive to guarantee that
1042d85b62f1SPaul E. McKenney  * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies
1043d85b62f1SPaul E. McKenney  * if the UNLOCK and LOCK are executed by the same CPU or if the
1044d85b62f1SPaul E. McKenney  * UNLOCK and LOCK operate on the same lock variable.
1045d85b62f1SPaul E. McKenney  */
104677e58496SPaul E. McKenney #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
1047d85b62f1SPaul E. McKenney #define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
104877e58496SPaul E. McKenney #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1049d85b62f1SPaul E. McKenney #define smp_mb__after_unlock_lock()	do { } while (0)
105077e58496SPaul E. McKenney #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1051d85b62f1SPaul E. McKenney 
1052274529baSPaul E. McKenney 
105374de6960SPaul E. McKenney /* Has the specified rcu_head structure been handed to call_rcu()? */
105474de6960SPaul E. McKenney 
10552aa55030SPaul E. McKenney /**
105674de6960SPaul E. McKenney  * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu()
105774de6960SPaul E. McKenney  * @rhp: The rcu_head structure to initialize.
105874de6960SPaul E. McKenney  *
105974de6960SPaul E. McKenney  * If you intend to invoke rcu_head_after_call_rcu() to test whether a
106074de6960SPaul E. McKenney  * given rcu_head structure has already been passed to call_rcu(), then
106174de6960SPaul E. McKenney  * you must also invoke this rcu_head_init() function on it just after
106274de6960SPaul E. McKenney  * allocating that structure.  Calls to this function must not race with
106374de6960SPaul E. McKenney  * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation.
106474de6960SPaul E. McKenney  */
rcu_head_init(struct rcu_head * rhp)106574de6960SPaul E. McKenney static inline void rcu_head_init(struct rcu_head *rhp)
106674de6960SPaul E. McKenney {
106774de6960SPaul E. McKenney 	rhp->func = (rcu_callback_t)~0L;
106874de6960SPaul E. McKenney }
106974de6960SPaul E. McKenney 
10702aa55030SPaul E. McKenney /**
1071000601bbSTobias Klauser  * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()?
107274de6960SPaul E. McKenney  * @rhp: The rcu_head structure to test.
10732aa55030SPaul E. McKenney  * @f: The function passed to call_rcu() along with @rhp.
107474de6960SPaul E. McKenney  *
107574de6960SPaul E. McKenney  * Returns @true if the @rhp has been passed to call_rcu() with @func,
107674de6960SPaul E. McKenney  * and @false otherwise.  Emits a warning in any other case, including
107774de6960SPaul E. McKenney  * the case where @rhp has already been invoked after a grace period.
107874de6960SPaul E. McKenney  * Calls to this function must not race with callback invocation.  One way
107974de6960SPaul E. McKenney  * to avoid such races is to enclose the call to rcu_head_after_call_rcu()
108074de6960SPaul E. McKenney  * in an RCU read-side critical section that includes a read-side fetch
108174de6960SPaul E. McKenney  * of the pointer to the structure containing @rhp.
108274de6960SPaul E. McKenney  */
108374de6960SPaul E. McKenney static inline bool
rcu_head_after_call_rcu(struct rcu_head * rhp,rcu_callback_t f)108474de6960SPaul E. McKenney rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
108574de6960SPaul E. McKenney {
1086b699cce1SNeeraj Upadhyay 	rcu_callback_t func = READ_ONCE(rhp->func);
1087b699cce1SNeeraj Upadhyay 
1088b699cce1SNeeraj Upadhyay 	if (func == f)
108974de6960SPaul E. McKenney 		return true;
1090b699cce1SNeeraj Upadhyay 	WARN_ON_ONCE(func != (rcu_callback_t)~0L);
109174de6960SPaul E. McKenney 	return false;
109274de6960SPaul E. McKenney }
109374de6960SPaul E. McKenney 
1094e1350e8eSBen Dooks /* kernel/ksysfs.c definitions */
1095e1350e8eSBen Dooks extern int rcu_expedited;
1096e1350e8eSBen Dooks extern int rcu_normal;
1097e1350e8eSBen Dooks 
109854da6a09SPeter Zijlstra DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
109954da6a09SPeter Zijlstra 
11001da177e4SLinus Torvalds #endif /* __LINUX_RCUPDATE_H */
1101