xref: /linux-6.15/include/linux/rcupdate.h (revision 6309a5c4)
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 <[email protected]>
81da177e4SLinus Torvalds  *
973604da5SPaul E. McKenney  * Based on the original work by Paul McKenney <[email protected]>
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>
326f0e6c15SFrederic Weisbecker #include <linux/context_tracking_irq.h>
33c1ad348bSThomas Gleixner 
34a3dc3fb1SPaul E. McKenney #define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
35a3dc3fb1SPaul E. McKenney #define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
36a3dc3fb1SPaul E. McKenney 
3729bc83e4SJP Kobryn #define RCU_SEQ_CTR_SHIFT    2
3829bc83e4SJP Kobryn #define RCU_SEQ_STATE_MASK   ((1 << RCU_SEQ_CTR_SHIFT) - 1)
3929bc83e4SJP Kobryn 
4003b042bfSPaul E. McKenney /* Exported common interfaces */
41709fdce7SPaul E. McKenney void call_rcu(struct rcu_head *head, rcu_callback_t func);
4253c6d4edSPaul E. McKenney void rcu_barrier_tasks(void);
43709fdce7SPaul E. McKenney void synchronize_rcu(void);
4491a967fdSPaul E. McKenney 
4591a967fdSPaul E. McKenney struct rcu_gp_oldstate;
46414c1238SPaul E. McKenney unsigned long get_completed_synchronize_rcu(void);
4791a967fdSPaul E. McKenney void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp);
488315f422SPaul E. McKenney 
4918538248SPaul E. McKenney // Maximum number of unsigned long values corresponding to
5018538248SPaul E. McKenney // not-yet-completed RCU grace periods.
5118538248SPaul E. McKenney #define NUM_ACTIVE_RCU_POLL_OLDSTATE 2
5218538248SPaul E. McKenney 
5318538248SPaul E. McKenney /**
5418538248SPaul E. McKenney  * same_state_synchronize_rcu - Are two old-state values identical?
5518538248SPaul E. McKenney  * @oldstate1: First old-state value.
5618538248SPaul E. McKenney  * @oldstate2: Second old-state value.
5718538248SPaul E. McKenney  *
5818538248SPaul E. McKenney  * The two old-state values must have been obtained from either
5918538248SPaul E. McKenney  * get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or
6018538248SPaul E. McKenney  * get_completed_synchronize_rcu().  Returns @true if the two values are
6118538248SPaul E. McKenney  * identical and @false otherwise.  This allows structures whose lifetimes
6218538248SPaul E. McKenney  * are tracked by old-state values to push these values to a list header,
6318538248SPaul E. McKenney  * allowing those structures to be slightly smaller.
6418538248SPaul E. McKenney  */
same_state_synchronize_rcu(unsigned long oldstate1,unsigned long oldstate2)6518538248SPaul E. McKenney static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned long oldstate2)
6618538248SPaul E. McKenney {
6718538248SPaul E. McKenney 	return oldstate1 == oldstate2;
6818538248SPaul E. McKenney }
6903b042bfSPaul E. McKenney 
70a3dc3fb1SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
71a3dc3fb1SPaul E. McKenney 
72584dc4ceSTeodora Baluta void __rcu_read_lock(void);
73584dc4ceSTeodora Baluta void __rcu_read_unlock(void);
747b0b759bSPaul E. McKenney 
75a3dc3fb1SPaul E. McKenney /*
76a3dc3fb1SPaul E. McKenney  * Defined as a macro as it is a very low level header included from
77a3dc3fb1SPaul E. McKenney  * areas that don't even know about current.  This gives the rcu_read_lock()
78a3dc3fb1SPaul E. McKenney  * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
79a3dc3fb1SPaul E. McKenney  * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
80a3dc3fb1SPaul E. McKenney  */
815fcb3a5fSPaul E. McKenney #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
82a3dc3fb1SPaul E. McKenney 
837b0b759bSPaul E. McKenney #else /* #ifdef CONFIG_PREEMPT_RCU */
847b0b759bSPaul E. McKenney 
85aa40c138SPaul E. McKenney #ifdef CONFIG_TINY_RCU
86aa40c138SPaul E. McKenney #define rcu_read_unlock_strict() do { } while (0)
87aa40c138SPaul E. McKenney #else
88aa40c138SPaul E. McKenney void rcu_read_unlock_strict(void);
89aa40c138SPaul E. McKenney #endif
90aa40c138SPaul E. McKenney 
__rcu_read_lock(void)917b0b759bSPaul E. McKenney static inline void __rcu_read_lock(void)
927b0b759bSPaul E. McKenney {
937b0b759bSPaul E. McKenney 	preempt_disable();
947b0b759bSPaul E. McKenney }
957b0b759bSPaul E. McKenney 
__rcu_read_unlock(void)967b0b759bSPaul E. McKenney static inline void __rcu_read_unlock(void)
977b0b759bSPaul E. McKenney {
98925da92bSWaiman Long 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
99aa40c138SPaul E. McKenney 		rcu_read_unlock_strict();
100fcf0e25aSAnkur Arora 	preempt_enable();
1017b0b759bSPaul E. McKenney }
1027b0b759bSPaul E. McKenney 
rcu_preempt_depth(void)1037b0b759bSPaul E. McKenney static inline int rcu_preempt_depth(void)
1047b0b759bSPaul E. McKenney {
1057b0b759bSPaul E. McKenney 	return 0;
1067b0b759bSPaul E. McKenney }
1077b0b759bSPaul E. McKenney 
1087b0b759bSPaul E. McKenney #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
1097b0b759bSPaul E. McKenney 
1103cb278e7SJoel Fernandes (Google) #ifdef CONFIG_RCU_LAZY
1113cb278e7SJoel Fernandes (Google) void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func);
1123cb278e7SJoel Fernandes (Google) #else
call_rcu_hurry(struct rcu_head * head,rcu_callback_t func)1133cb278e7SJoel Fernandes (Google) static inline void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func)
1143cb278e7SJoel Fernandes (Google) {
1153cb278e7SJoel Fernandes (Google) 	call_rcu(head, func);
1163cb278e7SJoel Fernandes (Google) }
1173cb278e7SJoel Fernandes (Google) #endif
1183cb278e7SJoel Fernandes (Google) 
1197b0b759bSPaul E. McKenney /* Internal to kernel */
120584dc4ceSTeodora Baluta void rcu_init(void);
121e6339d3bSIngo Molnar extern int rcu_scheduler_active;
122c98cac60SPaul E. McKenney void rcu_sched_clock_irq(int user);
1232b1d5024SFrederic Weisbecker 
12461f38db3SRik van Riel #ifdef CONFIG_RCU_STALL_COMMON
12561f38db3SRik van Riel void rcu_sysrq_start(void);
12661f38db3SRik van Riel void rcu_sysrq_end(void);
12761f38db3SRik van Riel #else /* #ifdef CONFIG_RCU_STALL_COMMON */
rcu_sysrq_start(void)128d0df7a34SPaul E. McKenney static inline void rcu_sysrq_start(void) { }
rcu_sysrq_end(void)129d0df7a34SPaul E. McKenney static inline void rcu_sysrq_end(void) { }
13061f38db3SRik van Riel #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
13161f38db3SRik van Riel 
13256450649SFrederic Weisbecker #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_KVM_XFER_TO_GUEST_WORK))
13356450649SFrederic Weisbecker void rcu_irq_work_resched(void);
1342b1d5024SFrederic Weisbecker #else
rcu_irq_work_resched(void)135*6309a5c4SJosh Poimboeuf static __always_inline void rcu_irq_work_resched(void) { }
13656450649SFrederic Weisbecker #endif
1372b1d5024SFrederic Weisbecker 
138f4579fc5SPaul E. McKenney #ifdef CONFIG_RCU_NOCB_CPU
139f4579fc5SPaul E. McKenney void rcu_init_nohz(void);
140254e11efSFrederic Weisbecker int rcu_nocb_cpu_offload(int cpu);
141d97b0781SFrederic Weisbecker int rcu_nocb_cpu_deoffload(int cpu);
14243789ef3SFrederic Weisbecker void rcu_nocb_flush_deferred_wakeup(void);
1437aeba709SFrederic Weisbecker 
1447aeba709SFrederic Weisbecker #define RCU_NOCB_LOCKDEP_WARN(c, s) RCU_LOCKDEP_WARN(c, s)
1457aeba709SFrederic Weisbecker 
146f4579fc5SPaul E. McKenney #else /* #ifdef CONFIG_RCU_NOCB_CPU */
1477aeba709SFrederic Weisbecker 
rcu_init_nohz(void)148d0df7a34SPaul E. McKenney static inline void rcu_init_nohz(void) { }
rcu_nocb_cpu_offload(int cpu)149254e11efSFrederic Weisbecker static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; }
rcu_nocb_cpu_deoffload(int cpu)150d97b0781SFrederic Weisbecker static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; }
rcu_nocb_flush_deferred_wakeup(void)15143789ef3SFrederic Weisbecker static inline void rcu_nocb_flush_deferred_wakeup(void) { }
1527aeba709SFrederic Weisbecker 
1537aeba709SFrederic Weisbecker #define RCU_NOCB_LOCKDEP_WARN(c, s)
1547aeba709SFrederic Weisbecker 
155f4579fc5SPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
156f4579fc5SPaul E. McKenney 
1578315f422SPaul E. McKenney /*
1586f56f714SPaul E. McKenney  * Note a quasi-voluntary context switch for RCU-tasks's benefit.
1596f56f714SPaul E. McKenney  * This is a macro rather than an inline function to avoid #include hell.
1608315f422SPaul E. McKenney  */
1615873b8a9SPaul E. McKenney #ifdef CONFIG_TASKS_RCU_GENERIC
16243766c3eSPaul E. McKenney 
16343766c3eSPaul E. McKenney # ifdef CONFIG_TASKS_RCU
16443766c3eSPaul E. McKenney # define rcu_tasks_classic_qs(t, preempt)				\
1658315f422SPaul E. McKenney 	do {								\
16643766c3eSPaul E. McKenney 		if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout))	\
1677d0ae808SPaul E. McKenney 			WRITE_ONCE((t)->rcu_tasks_holdout, false);	\
1688315f422SPaul E. McKenney 	} while (0)
1697e42776dSPaul E. McKenney void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
1707e42776dSPaul E. McKenney void synchronize_rcu_tasks(void);
171fe91cf39SPaul E. McKenney void rcu_tasks_torture_stats_print(char *tt, char *tf);
17243766c3eSPaul E. McKenney # else
17343766c3eSPaul E. McKenney # define rcu_tasks_classic_qs(t, preempt) do { } while (0)
17443766c3eSPaul E. McKenney # define call_rcu_tasks call_rcu
17543766c3eSPaul E. McKenney # define synchronize_rcu_tasks synchronize_rcu
17643766c3eSPaul E. McKenney # endif
17743766c3eSPaul E. McKenney 
178fed31a4dSZhouyi Zhou # ifdef CONFIG_TASKS_TRACE_RCU
1793847b645SPaul E. McKenney // Bits for ->trc_reader_special.b.need_qs field.
1803847b645SPaul E. McKenney #define TRC_NEED_QS		0x1  // Task needs a quiescent state.
1813847b645SPaul E. McKenney #define TRC_NEED_QS_CHECKED	0x2  // Task has been checked for needing quiescent state.
1823847b645SPaul E. McKenney 
1833847b645SPaul E. McKenney u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new);
1840356d4e6SPaul E. McKenney void rcu_tasks_trace_qs_blkd(struct task_struct *t);
1853847b645SPaul E. McKenney 
18643766c3eSPaul E. McKenney # define rcu_tasks_trace_qs(t)							\
18743766c3eSPaul E. McKenney 	do {									\
1880356d4e6SPaul E. McKenney 		int ___rttq_nesting = READ_ONCE((t)->trc_reader_nesting);	\
1890356d4e6SPaul E. McKenney 										\
1902eb52fa8SPaul E. McKenney 		if (unlikely(READ_ONCE((t)->trc_reader_special.b.need_qs) == TRC_NEED_QS) &&	\
1910356d4e6SPaul E. McKenney 		    likely(!___rttq_nesting)) {					\
1922eb52fa8SPaul E. McKenney 			rcu_trc_cmpxchg_need_qs((t), TRC_NEED_QS, TRC_NEED_QS_CHECKED);	\
1930356d4e6SPaul E. McKenney 		} else if (___rttq_nesting && ___rttq_nesting != INT_MIN &&	\
1940356d4e6SPaul E. McKenney 			   !READ_ONCE((t)->trc_reader_special.b.blocked)) {	\
1950356d4e6SPaul E. McKenney 			rcu_tasks_trace_qs_blkd(t);				\
19643766c3eSPaul E. McKenney 		}								\
19743766c3eSPaul E. McKenney 	} while (0)
198fe91cf39SPaul E. McKenney void rcu_tasks_trace_torture_stats_print(char *tt, char *tf);
19943766c3eSPaul E. McKenney # else
20043766c3eSPaul E. McKenney # define rcu_tasks_trace_qs(t) do { } while (0)
20143766c3eSPaul E. McKenney # endif
20243766c3eSPaul E. McKenney 
20343766c3eSPaul E. McKenney #define rcu_tasks_qs(t, preempt)					\
20443766c3eSPaul E. McKenney do {									\
20543766c3eSPaul E. McKenney 	rcu_tasks_classic_qs((t), (preempt));				\
2060356d4e6SPaul E. McKenney 	rcu_tasks_trace_qs(t);						\
20743766c3eSPaul E. McKenney } while (0)
20843766c3eSPaul E. McKenney 
20943766c3eSPaul E. McKenney # ifdef CONFIG_TASKS_RUDE_RCU
210c84aad76SPaul E. McKenney void synchronize_rcu_tasks_rude(void);
211fe91cf39SPaul E. McKenney void rcu_tasks_rude_torture_stats_print(char *tt, char *tf);
21243766c3eSPaul E. McKenney # endif
21343766c3eSPaul E. McKenney 
21443766c3eSPaul E. McKenney #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
215ccdd29ffSPaul E. McKenney void exit_tasks_rcu_start(void);
216ccdd29ffSPaul E. McKenney void exit_tasks_rcu_finish(void);
2175873b8a9SPaul E. McKenney #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
2185d900708SPaul E. McKenney #define rcu_tasks_classic_qs(t, preempt) do { } while (0)
21943766c3eSPaul E. McKenney #define rcu_tasks_qs(t, preempt) do { } while (0)
2204d232dfeSPaul E. McKenney #define rcu_note_voluntary_context_switch(t) do { } while (0)
2212bd8b1a2SPaul E. McKenney #define call_rcu_tasks call_rcu
222a8bb74acSPaul E. McKenney #define synchronize_rcu_tasks synchronize_rcu
exit_tasks_rcu_start(void)223ccdd29ffSPaul E. McKenney static inline void exit_tasks_rcu_start(void) { }
exit_tasks_rcu_finish(void)224ccdd29ffSPaul E. McKenney static inline void exit_tasks_rcu_finish(void) { }
2255873b8a9SPaul E. McKenney #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
2268315f422SPaul E. McKenney 
227bde6c3aaSPaul E. McKenney /**
228e6c86c51SPaul E. McKenney  * rcu_trace_implies_rcu_gp - does an RCU Tasks Trace grace period imply an RCU grace period?
229e6c86c51SPaul E. McKenney  *
230e6c86c51SPaul E. McKenney  * As an accident of implementation, an RCU Tasks Trace grace period also
231e6c86c51SPaul E. McKenney  * acts as an RCU grace period.  However, this could change at any time.
232e6c86c51SPaul E. McKenney  * Code relying on this accident must call this function to verify that
233e6c86c51SPaul E. McKenney  * this accident is still happening.
234e6c86c51SPaul E. McKenney  *
235e6c86c51SPaul E. McKenney  * You have been warned!
236e6c86c51SPaul E. McKenney  */
rcu_trace_implies_rcu_gp(void)237e6c86c51SPaul E. McKenney static inline bool rcu_trace_implies_rcu_gp(void) { return true; }
238e6c86c51SPaul E. McKenney 
239e6c86c51SPaul E. McKenney /**
240cee43939SPaul E. McKenney  * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
241bde6c3aaSPaul E. McKenney  *
242bde6c3aaSPaul E. McKenney  * This macro resembles cond_resched(), except that it is defined to
243bde6c3aaSPaul E. McKenney  * report potential quiescent states to RCU-tasks even if the cond_resched()
24490326f05SSebastian Andrzej Siewior  * machinery were to be shut off, as some advocate for PREEMPTION kernels.
245bde6c3aaSPaul E. McKenney  */
246cee43939SPaul E. McKenney #define cond_resched_tasks_rcu_qs() \
247bde6c3aaSPaul E. McKenney do { \
24843766c3eSPaul E. McKenney 	rcu_tasks_qs(current, false); \
24907f27570SByungchul Park 	cond_resched(); \
250bde6c3aaSPaul E. McKenney } while (0)
251bde6c3aaSPaul E. McKenney 
2521a77557dSYan Zhai /**
2531a77557dSYan Zhai  * rcu_softirq_qs_periodic - Report RCU and RCU-Tasks quiescent states
2541a77557dSYan Zhai  * @old_ts: jiffies at start of processing.
2551a77557dSYan Zhai  *
2561a77557dSYan Zhai  * This helper is for long-running softirq handlers, such as NAPI threads in
2571a77557dSYan Zhai  * networking. The caller should initialize the variable passed in as @old_ts
2581a77557dSYan Zhai  * at the beginning of the softirq handler. When invoked frequently, this macro
2591a77557dSYan Zhai  * will invoke rcu_softirq_qs() every 100 milliseconds thereafter, which will
2601a77557dSYan Zhai  * provide both RCU and RCU-Tasks quiescent states. Note that this macro
2611a77557dSYan Zhai  * modifies its old_ts argument.
2621a77557dSYan Zhai  *
2631a77557dSYan Zhai  * Because regions of code that have disabled softirq act as RCU read-side
2641a77557dSYan Zhai  * critical sections, this macro should be invoked with softirq (and
2651a77557dSYan Zhai  * preemption) enabled.
2661a77557dSYan Zhai  *
2671a77557dSYan Zhai  * The macro is not needed when CONFIG_PREEMPT_RT is defined. RT kernels would
2681a77557dSYan Zhai  * have more chance to invoke schedule() calls and provide necessary quiescent
2691a77557dSYan Zhai  * states. As a contrast, calling cond_resched() only won't achieve the same
2701a77557dSYan Zhai  * effect because cond_resched() does not provide RCU-Tasks quiescent states.
2711a77557dSYan Zhai  */
2721a77557dSYan Zhai #define rcu_softirq_qs_periodic(old_ts) \
2731a77557dSYan Zhai do { \
2741a77557dSYan Zhai 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && \
2751a77557dSYan Zhai 	    time_after(jiffies, (old_ts) + HZ / 10)) { \
2761a77557dSYan Zhai 		preempt_disable(); \
2771a77557dSYan Zhai 		rcu_softirq_qs(); \
2781a77557dSYan Zhai 		preempt_enable(); \
2791a77557dSYan Zhai 		(old_ts) = jiffies; \
2801a77557dSYan Zhai 	} \
2811a77557dSYan Zhai } while (0)
2821a77557dSYan Zhai 
2832c42818eSPaul E. McKenney /*
2842c42818eSPaul E. McKenney  * Infrastructure to implement the synchronize_() primitives in
2852c42818eSPaul E. McKenney  * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
2862c42818eSPaul E. McKenney  */
2872c42818eSPaul E. McKenney 
288b3e627d3SLai Jiangshan #if defined(CONFIG_TREE_RCU)
28964db4cffSPaul E. McKenney #include <linux/rcutree.h>
290127781d1SPaul E. McKenney #elif defined(CONFIG_TINY_RCU)
2919b1d82faSPaul E. McKenney #include <linux/rcutiny.h>
29264db4cffSPaul E. McKenney #else
29364db4cffSPaul E. McKenney #error "Unknown RCU implementation specified to kernel configuration"
2946b3ef48aSPaul E. McKenney #endif
29501c1c660SPaul E. McKenney 
296551d55a9SMathieu Desnoyers /*
297b5482a06SPaul E. McKenney  * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls
298b5482a06SPaul E. McKenney  * are needed for dynamic initialization and destruction of rcu_head
299b5482a06SPaul E. McKenney  * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for
300b5482a06SPaul E. McKenney  * dynamic initialization and destruction of statically allocated rcu_head
301b5482a06SPaul E. McKenney  * structures.  However, rcu_head structures allocated dynamically in the
302b5482a06SPaul E. McKenney  * heap don't need any initialization.
303551d55a9SMathieu Desnoyers  */
304551d55a9SMathieu Desnoyers #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
305546a9d85SPaul E. McKenney void init_rcu_head(struct rcu_head *head);
306546a9d85SPaul E. McKenney void destroy_rcu_head(struct rcu_head *head);
307584dc4ceSTeodora Baluta void init_rcu_head_on_stack(struct rcu_head *head);
308584dc4ceSTeodora Baluta void destroy_rcu_head_on_stack(struct rcu_head *head);
309551d55a9SMathieu Desnoyers #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
init_rcu_head(struct rcu_head * head)310d0df7a34SPaul E. McKenney static inline void init_rcu_head(struct rcu_head *head) { }
destroy_rcu_head(struct rcu_head * head)311d0df7a34SPaul E. McKenney static inline void destroy_rcu_head(struct rcu_head *head) { }
init_rcu_head_on_stack(struct rcu_head * head)312d0df7a34SPaul E. McKenney static inline void init_rcu_head_on_stack(struct rcu_head *head) { }
destroy_rcu_head_on_stack(struct rcu_head * head)313d0df7a34SPaul E. McKenney static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { }
314551d55a9SMathieu Desnoyers #endif	/* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
3154376030aSMathieu Desnoyers 
316c0d6d01bSPaul E. McKenney #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
317c0d6d01bSPaul E. McKenney bool rcu_lockdep_current_cpu_online(void);
318c0d6d01bSPaul E. McKenney #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
rcu_lockdep_current_cpu_online(void)31917a8c187SPaul E. McKenney static inline bool rcu_lockdep_current_cpu_online(void) { return true; }
320c0d6d01bSPaul E. McKenney #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
321c0d6d01bSPaul E. McKenney 
322891cd1f9SJakub Kicinski extern struct lockdep_map rcu_lock_map;
323891cd1f9SJakub Kicinski extern struct lockdep_map rcu_bh_lock_map;
324891cd1f9SJakub Kicinski extern struct lockdep_map rcu_sched_lock_map;
325891cd1f9SJakub Kicinski extern struct lockdep_map rcu_callback_map;
326891cd1f9SJakub Kicinski 
327bc33f24bSPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
328632ee200SPaul E. McKenney 
rcu_lock_acquire(struct lockdep_map * map)32900f49e57SFrederic Weisbecker static inline void rcu_lock_acquire(struct lockdep_map *map)
33000f49e57SFrederic Weisbecker {
331fb9edbe9SOleg Nesterov 	lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
33200f49e57SFrederic Weisbecker }
33300f49e57SFrederic Weisbecker 
rcu_try_lock_acquire(struct lockdep_map * map)3343c6b0c1cSSebastian Andrzej Siewior static inline void rcu_try_lock_acquire(struct lockdep_map *map)
3353c6b0c1cSSebastian Andrzej Siewior {
3363c6b0c1cSSebastian Andrzej Siewior 	lock_acquire(map, 0, 1, 2, 0, NULL, _THIS_IP_);
3373c6b0c1cSSebastian Andrzej Siewior }
3383c6b0c1cSSebastian Andrzej Siewior 
rcu_lock_release(struct lockdep_map * map)33900f49e57SFrederic Weisbecker static inline void rcu_lock_release(struct lockdep_map *map)
34000f49e57SFrederic Weisbecker {
3415facae4fSQian Cai 	lock_release(map, _THIS_IP_);
34200f49e57SFrederic Weisbecker }
34300f49e57SFrederic Weisbecker 
344a235c091SIulia Manda int debug_lockdep_rcu_enabled(void);
34585b39d30SOleg Nesterov int rcu_read_lock_held(void);
346584dc4ceSTeodora Baluta int rcu_read_lock_bh_held(void);
347d5671f6bSDenys Vlasenko int rcu_read_lock_sched_held(void);
34828875945SJoel Fernandes (Google) int rcu_read_lock_any_held(void);
349632ee200SPaul E. McKenney 
350632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
351632ee200SPaul E. McKenney 
352d8ab29f8SPaul E. McKenney # define rcu_lock_acquire(a)		do { } while (0)
3533c6b0c1cSSebastian Andrzej Siewior # define rcu_try_lock_acquire(a)	do { } while (0)
354d8ab29f8SPaul E. McKenney # define rcu_lock_release(a)		do { } while (0)
355632ee200SPaul E. McKenney 
rcu_read_lock_held(void)356632ee200SPaul E. McKenney static inline int rcu_read_lock_held(void)
357632ee200SPaul E. McKenney {
358632ee200SPaul E. McKenney 	return 1;
359632ee200SPaul E. McKenney }
360632ee200SPaul E. McKenney 
rcu_read_lock_bh_held(void)361632ee200SPaul E. McKenney static inline int rcu_read_lock_bh_held(void)
362632ee200SPaul E. McKenney {
363632ee200SPaul E. McKenney 	return 1;
364632ee200SPaul E. McKenney }
365632ee200SPaul E. McKenney 
rcu_read_lock_sched_held(void)366632ee200SPaul E. McKenney static inline int rcu_read_lock_sched_held(void)
367632ee200SPaul E. McKenney {
368293e2421SBoqun Feng 	return !preemptible();
369632ee200SPaul E. McKenney }
37028875945SJoel Fernandes (Google) 
rcu_read_lock_any_held(void)37128875945SJoel Fernandes (Google) static inline int rcu_read_lock_any_held(void)
37228875945SJoel Fernandes (Google) {
37328875945SJoel Fernandes (Google) 	return !preemptible();
37428875945SJoel Fernandes (Google) }
37528875945SJoel Fernandes (Google) 
debug_lockdep_rcu_enabled(void)376f733615eSJohn Ogness static inline int debug_lockdep_rcu_enabled(void)
377f733615eSJohn Ogness {
378f733615eSJohn Ogness 	return 0;
379f733615eSJohn Ogness }
380f733615eSJohn Ogness 
381632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
382632ee200SPaul E. McKenney 
383632ee200SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
384632ee200SPaul E. McKenney 
3854221a991STetsuo Handa /**
386f78f5b90SPaul E. McKenney  * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
387f78f5b90SPaul E. McKenney  * @c: condition to check
388f78f5b90SPaul E. McKenney  * @s: informative message
3890cae5dedSPaul E. McKenney  *
3900cae5dedSPaul E. McKenney  * This checks debug_lockdep_rcu_enabled() before checking (c) to
3910cae5dedSPaul E. McKenney  * prevent early boot splats due to lockdep not yet being initialized,
3920cae5dedSPaul E. McKenney  * and rechecks it after checking (c) to prevent false-positive splats
3930cae5dedSPaul E. McKenney  * due to races with lockdep being disabled.  See commit 3066820034b5dd
3940cae5dedSPaul E. McKenney  * ("rcu: Reject RCU_LOCKDEP_WARN() false positives") for more detail.
395f78f5b90SPaul E. McKenney  */
396f78f5b90SPaul E. McKenney #define RCU_LOCKDEP_WARN(c, s)						\
397f78f5b90SPaul E. McKenney 	do {								\
398bb43a599SMasahiro Yamada 		static bool __section(".data..unlikely") __warned;	\
3990cae5dedSPaul E. McKenney 		if (debug_lockdep_rcu_enabled() && (c) &&		\
4000cae5dedSPaul E. McKenney 		    debug_lockdep_rcu_enabled() && !__warned) {		\
401f78f5b90SPaul E. McKenney 			__warned = true;				\
402f78f5b90SPaul E. McKenney 			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
403f78f5b90SPaul E. McKenney 		}							\
404f78f5b90SPaul E. McKenney 	} while (0)
405f78f5b90SPaul E. McKenney 
40610c9e40fSPaul E. McKenney #ifndef CONFIG_PREEMPT_RCU
rcu_preempt_sleep_check(void)40750406b98SPaul E. McKenney static inline void rcu_preempt_sleep_check(void)
40850406b98SPaul E. McKenney {
409f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
4105cf05ad7SPaul E. McKenney 			 "Illegal context switch in RCU read-side critical section");
41150406b98SPaul E. McKenney }
41210c9e40fSPaul E. McKenney #else // #ifndef CONFIG_PREEMPT_RCU
rcu_preempt_sleep_check(void)413d0df7a34SPaul E. McKenney static inline void rcu_preempt_sleep_check(void) { }
41410c9e40fSPaul E. McKenney #endif // #else // #ifndef CONFIG_PREEMPT_RCU
41550406b98SPaul E. McKenney 
416b3fbab05SPaul E. McKenney #define rcu_sleep_check()						\
417b3fbab05SPaul E. McKenney 	do {								\
41850406b98SPaul E. McKenney 		rcu_preempt_sleep_check();				\
419ba9e6cabSThomas Gleixner 		if (!IS_ENABLED(CONFIG_PREEMPT_RT))			\
420f78f5b90SPaul E. McKenney 		    RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),	\
42141f4abd9SJoe Perches 				 "Illegal context switch in RCU-bh read-side critical section"); \
422f78f5b90SPaul E. McKenney 		RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),	\
42341f4abd9SJoe Perches 				 "Illegal context switch in RCU-sched read-side critical section"); \
424b3fbab05SPaul E. McKenney 	} while (0)
425b3fbab05SPaul E. McKenney 
42632d99593SPaul E. McKenney // See RCU_LOCKDEP_WARN() for an explanation of the double call to
42732d99593SPaul E. McKenney // debug_lockdep_rcu_enabled().
lockdep_assert_rcu_helper(bool c)42832d99593SPaul E. McKenney static inline bool lockdep_assert_rcu_helper(bool c)
42932d99593SPaul E. McKenney {
43032d99593SPaul E. McKenney 	return debug_lockdep_rcu_enabled() &&
43132d99593SPaul E. McKenney 	       (c || !rcu_is_watching() || !rcu_lockdep_current_cpu_online()) &&
43232d99593SPaul E. McKenney 	       debug_lockdep_rcu_enabled();
43332d99593SPaul E. McKenney }
43432d99593SPaul E. McKenney 
43532d99593SPaul E. McKenney /**
43632d99593SPaul E. McKenney  * lockdep_assert_in_rcu_read_lock - WARN if not protected by rcu_read_lock()
43732d99593SPaul E. McKenney  *
43832d99593SPaul E. McKenney  * Splats if lockdep is enabled and there is no rcu_read_lock() in effect.
43932d99593SPaul E. McKenney  */
44032d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock() \
44132d99593SPaul E. McKenney 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map)))
44232d99593SPaul E. McKenney 
44332d99593SPaul E. McKenney /**
44432d99593SPaul E. McKenney  * lockdep_assert_in_rcu_read_lock_bh - WARN if not protected by rcu_read_lock_bh()
44532d99593SPaul E. McKenney  *
44632d99593SPaul E. McKenney  * Splats if lockdep is enabled and there is no rcu_read_lock_bh() in effect.
44732d99593SPaul E. McKenney  * Note that local_bh_disable() and friends do not suffice here, instead an
44832d99593SPaul E. McKenney  * actual rcu_read_lock_bh() is required.
44932d99593SPaul E. McKenney  */
45032d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock_bh() \
45132d99593SPaul E. McKenney 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_bh_lock_map)))
45232d99593SPaul E. McKenney 
45332d99593SPaul E. McKenney /**
45432d99593SPaul E. McKenney  * lockdep_assert_in_rcu_read_lock_sched - WARN if not protected by rcu_read_lock_sched()
45532d99593SPaul E. McKenney  *
45632d99593SPaul E. McKenney  * Splats if lockdep is enabled and there is no rcu_read_lock_sched()
45732d99593SPaul E. McKenney  * in effect.  Note that preempt_disable() and friends do not suffice here,
45832d99593SPaul E. McKenney  * instead an actual rcu_read_lock_sched() is required.
45932d99593SPaul E. McKenney  */
46032d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock_sched() \
46132d99593SPaul E. McKenney 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_sched_lock_map)))
46232d99593SPaul E. McKenney 
46332d99593SPaul E. McKenney /**
46432d99593SPaul E. McKenney  * lockdep_assert_in_rcu_reader - WARN if not within some type of RCU reader
46532d99593SPaul E. McKenney  *
46632d99593SPaul E. McKenney  * Splats if lockdep is enabled and there is no RCU reader of any
46732d99593SPaul E. McKenney  * type in effect.  Note that regions of code protected by things like
46832d99593SPaul E. McKenney  * preempt_disable, local_bh_disable(), and local_irq_disable() all qualify
46932d99593SPaul E. McKenney  * as RCU readers.
47032d99593SPaul E. McKenney  *
47132d99593SPaul E. McKenney  * Note that this will never trigger in PREEMPT_NONE or PREEMPT_VOLUNTARY
47232d99593SPaul E. McKenney  * kernels that are not also built with PREEMPT_COUNT.  But if you have
47332d99593SPaul E. McKenney  * lockdep enabled, you might as well also enable PREEMPT_COUNT.
47432d99593SPaul E. McKenney  */
47532d99593SPaul E. McKenney #define lockdep_assert_in_rcu_reader()								\
47632d99593SPaul E. McKenney 	WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map) &&			\
47732d99593SPaul E. McKenney 					       !lock_is_held(&rcu_bh_lock_map) &&		\
47832d99593SPaul E. McKenney 					       !lock_is_held(&rcu_sched_lock_map) &&		\
47932d99593SPaul E. McKenney 					       preemptible()))
48032d99593SPaul E. McKenney 
481ca5ecddfSPaul E. McKenney #else /* #ifdef CONFIG_PROVE_RCU */
482ca5ecddfSPaul E. McKenney 
48365e9eb1cSJakub Kicinski #define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c))
484b3fbab05SPaul E. McKenney #define rcu_sleep_check() do { } while (0)
485ca5ecddfSPaul E. McKenney 
48632d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock() do { } while (0)
48732d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock_bh() do { } while (0)
48832d99593SPaul E. McKenney #define lockdep_assert_in_rcu_read_lock_sched() do { } while (0)
48932d99593SPaul E. McKenney #define lockdep_assert_in_rcu_reader() do { } while (0)
49032d99593SPaul E. McKenney 
491ca5ecddfSPaul E. McKenney #endif /* #else #ifdef CONFIG_PROVE_RCU */
492ca5ecddfSPaul E. McKenney 
493ca5ecddfSPaul E. McKenney /*
494ca5ecddfSPaul E. McKenney  * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
495ca5ecddfSPaul E. McKenney  * and rcu_assign_pointer().  Some of these could be folded into their
496ca5ecddfSPaul E. McKenney  * callers, but they are left separate in order to ease introduction of
4972bd8b1a2SPaul E. McKenney  * multiple pointers markings to match different RCU implementations
4982bd8b1a2SPaul E. McKenney  * (e.g., __srcu), should this make sense in the future.
499ca5ecddfSPaul E. McKenney  */
50053ecfba2SPaul E. McKenney 
50153ecfba2SPaul E. McKenney #ifdef __CHECKER__
502423a86a6SJoel Fernandes (Google) #define rcu_check_sparse(p, space) \
50353ecfba2SPaul E. McKenney 	((void)(((typeof(*p) space *)p) == p))
50453ecfba2SPaul E. McKenney #else /* #ifdef __CHECKER__ */
505423a86a6SJoel Fernandes (Google) #define rcu_check_sparse(p, space)
50653ecfba2SPaul E. McKenney #endif /* #else #ifdef __CHECKER__ */
50753ecfba2SPaul E. McKenney 
50824ba5301SChun-Hung Tseng #define __unrcu_pointer(p, local)					\
50924ba5301SChun-Hung Tseng ({									\
51024ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)(p);			\
51124ba5301SChun-Hung Tseng 	rcu_check_sparse(p, __rcu);					\
51224ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); 			\
51324ba5301SChun-Hung Tseng })
51476c8eaafSPaul E. McKenney /**
51576c8eaafSPaul E. McKenney  * unrcu_pointer - mark a pointer as not being RCU protected
51676c8eaafSPaul E. McKenney  * @p: pointer needing to lose its __rcu property
51776c8eaafSPaul E. McKenney  *
51876c8eaafSPaul E. McKenney  * Converts @p from an __rcu pointer to a __kernel pointer.
51976c8eaafSPaul E. McKenney  * This allows an __rcu pointer to be used with xchg() and friends.
52076c8eaafSPaul E. McKenney  */
52124ba5301SChun-Hung Tseng #define unrcu_pointer(p) __unrcu_pointer(p, __UNIQUE_ID(rcu))
52276c8eaafSPaul E. McKenney 
52324ba5301SChun-Hung Tseng #define __rcu_access_pointer(p, local, space) \
524ca5ecddfSPaul E. McKenney ({ \
52524ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
526423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
52724ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
528ca5ecddfSPaul E. McKenney })
52924ba5301SChun-Hung Tseng #define __rcu_dereference_check(p, local, c, space) \
530ca5ecddfSPaul E. McKenney ({ \
531ac59853cSPranith Kumar 	/* Dependency order vs. p above. */ \
53224ba5301SChun-Hung Tseng 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
533f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
534423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
53524ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
536ca5ecddfSPaul E. McKenney })
53724ba5301SChun-Hung Tseng #define __rcu_dereference_protected(p, local, c, space) \
538ca5ecddfSPaul E. McKenney ({ \
539f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
540423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, space); \
541ca5ecddfSPaul E. McKenney 	((typeof(*p) __force __kernel *)(p)); \
542ca5ecddfSPaul E. McKenney })
54324ba5301SChun-Hung Tseng #define __rcu_dereference_raw(p, local) \
544995f1405SPaul E. McKenney ({ \
545995f1405SPaul E. McKenney 	/* Dependency order vs. p above. */ \
54624ba5301SChun-Hung Tseng 	typeof(p) local = READ_ONCE(p); \
54724ba5301SChun-Hung Tseng 	((typeof(*p) __force __kernel *)(local)); \
548995f1405SPaul E. McKenney })
54924ba5301SChun-Hung Tseng #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
550ca5ecddfSPaul E. McKenney 
551462225aeSPaul E. McKenney /**
552462225aeSPaul E. McKenney  * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
553462225aeSPaul E. McKenney  * @v: The value to statically initialize with.
554462225aeSPaul E. McKenney  */
555462225aeSPaul E. McKenney #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
556462225aeSPaul E. McKenney 
557462225aeSPaul E. McKenney /**
558462225aeSPaul E. McKenney  * rcu_assign_pointer() - assign to RCU-protected pointer
559462225aeSPaul E. McKenney  * @p: pointer to assign to
560462225aeSPaul E. McKenney  * @v: value to assign (publish)
561462225aeSPaul E. McKenney  *
562462225aeSPaul E. McKenney  * Assigns the specified value to the specified RCU-protected
563462225aeSPaul E. McKenney  * pointer, ensuring that any concurrent RCU readers will see
564462225aeSPaul E. McKenney  * any prior initialization.
565462225aeSPaul E. McKenney  *
566462225aeSPaul E. McKenney  * Inserts memory barriers on architectures that require them
567462225aeSPaul E. McKenney  * (which is most of them), and also prevents the compiler from
568462225aeSPaul E. McKenney  * reordering the code that initializes the structure after the pointer
569462225aeSPaul E. McKenney  * assignment.  More importantly, this call documents which pointers
570462225aeSPaul E. McKenney  * will be dereferenced by RCU read-side code.
571462225aeSPaul E. McKenney  *
572462225aeSPaul E. McKenney  * In some special cases, you may use RCU_INIT_POINTER() instead
573462225aeSPaul E. McKenney  * of rcu_assign_pointer().  RCU_INIT_POINTER() is a bit faster due
574462225aeSPaul E. McKenney  * to the fact that it does not constrain either the CPU or the compiler.
575462225aeSPaul E. McKenney  * That said, using RCU_INIT_POINTER() when you should have used
576462225aeSPaul E. McKenney  * rcu_assign_pointer() is a very bad thing that results in
577462225aeSPaul E. McKenney  * impossible-to-diagnose memory corruption.  So please be careful.
578462225aeSPaul E. McKenney  * See the RCU_INIT_POINTER() comment header for details.
579462225aeSPaul E. McKenney  *
580462225aeSPaul E. McKenney  * Note that rcu_assign_pointer() evaluates each of its arguments only
581462225aeSPaul E. McKenney  * once, appearances notwithstanding.  One of the "extra" evaluations
582462225aeSPaul E. McKenney  * is in typeof() and the other visible only to sparse (__CHECKER__),
583462225aeSPaul E. McKenney  * neither of which actually execute the argument.  As with most cpp
584462225aeSPaul E. McKenney  * macros, this execute-arguments-only-once property is important, so
585462225aeSPaul E. McKenney  * please be careful when making changes to rcu_assign_pointer() and the
586462225aeSPaul E. McKenney  * other macros that it invokes.
587462225aeSPaul E. McKenney  */
5883a37f727SPaul E. McKenney #define rcu_assign_pointer(p, v)					      \
5899129b017SAndrea Parri do {									      \
5903a37f727SPaul E. McKenney 	uintptr_t _r_a_p__v = (uintptr_t)(v);				      \
591423a86a6SJoel Fernandes (Google) 	rcu_check_sparse(p, __rcu);					      \
5923a37f727SPaul E. McKenney 									      \
5933a37f727SPaul E. McKenney 	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL)	      \
5943a37f727SPaul E. McKenney 		WRITE_ONCE((p), (typeof(p))(_r_a_p__v));		      \
5953a37f727SPaul E. McKenney 	else								      \
5963a37f727SPaul E. McKenney 		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
5979129b017SAndrea Parri } while (0)
598ca5ecddfSPaul E. McKenney 
599632ee200SPaul E. McKenney /**
600a63fc6b7SPaul E. McKenney  * rcu_replace_pointer() - replace an RCU pointer, returning its old value
601a63fc6b7SPaul E. McKenney  * @rcu_ptr: RCU pointer, whose old value is returned
602a63fc6b7SPaul E. McKenney  * @ptr: regular pointer
603a63fc6b7SPaul E. McKenney  * @c: the lockdep conditions under which the dereference will take place
604a63fc6b7SPaul E. McKenney  *
605a63fc6b7SPaul E. McKenney  * Perform a replacement, where @rcu_ptr is an RCU-annotated
606a63fc6b7SPaul E. McKenney  * pointer and @c is the lockdep argument that is passed to the
607a63fc6b7SPaul E. McKenney  * rcu_dereference_protected() call used to read that pointer.  The old
608a63fc6b7SPaul E. McKenney  * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr.
609a63fc6b7SPaul E. McKenney  */
610a63fc6b7SPaul E. McKenney #define rcu_replace_pointer(rcu_ptr, ptr, c)				\
611a63fc6b7SPaul E. McKenney ({									\
612a63fc6b7SPaul E. McKenney 	typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c));	\
613a63fc6b7SPaul E. McKenney 	rcu_assign_pointer((rcu_ptr), (ptr));				\
614a63fc6b7SPaul E. McKenney 	__tmp;								\
615a63fc6b7SPaul E. McKenney })
616a63fc6b7SPaul E. McKenney 
617a63fc6b7SPaul E. McKenney /**
618ca5ecddfSPaul E. McKenney  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
619ca5ecddfSPaul E. McKenney  * @p: The pointer to read
620ca5ecddfSPaul E. McKenney  *
621ca5ecddfSPaul E. McKenney  * Return the value of the specified RCU-protected pointer, but omit the
622137f61f6SPaul E. McKenney  * lockdep checks for being in an RCU read-side critical section.  This is
623137f61f6SPaul E. McKenney  * useful when the value of this pointer is accessed, but the pointer is
624137f61f6SPaul E. McKenney  * not dereferenced, for example, when testing an RCU-protected pointer
625137f61f6SPaul E. McKenney  * against NULL.  Although rcu_access_pointer() may also be used in cases
626137f61f6SPaul E. McKenney  * where update-side locks prevent the value of the pointer from changing,
627137f61f6SPaul E. McKenney  * you should instead use rcu_dereference_protected() for this use case.
628d8f3f583SPaul E. McKenney  * Within an RCU read-side critical section, there is little reason to
629d8f3f583SPaul E. McKenney  * use rcu_access_pointer().
630d8f3f583SPaul E. McKenney  *
631d8f3f583SPaul E. McKenney  * It is usually best to test the rcu_access_pointer() return value
632d8f3f583SPaul E. McKenney  * directly in order to avoid accidental dereferences being introduced
633d8f3f583SPaul E. McKenney  * by later inattentive changes.  In other words, assigning the
634d8f3f583SPaul E. McKenney  * rcu_access_pointer() return value to a local variable results in an
635d8f3f583SPaul E. McKenney  * accident waiting to happen.
6365e1ee6e1SPaul E. McKenney  *
6375e1ee6e1SPaul E. McKenney  * It is also permissible to use rcu_access_pointer() when read-side
638d8f3f583SPaul E. McKenney  * access to the pointer was removed at least one grace period ago, as is
639d8f3f583SPaul E. McKenney  * the case in the context of the RCU callback that is freeing up the data,
640d8f3f583SPaul E. McKenney  * or after a synchronize_rcu() returns.  This can be useful when tearing
641d8f3f583SPaul E. McKenney  * down multi-linked structures after a grace period has elapsed.  However,
642d8f3f583SPaul E. McKenney  * rcu_dereference_protected() is normally preferred for this use case.
643ca5ecddfSPaul E. McKenney  */
64424ba5301SChun-Hung Tseng #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
645ca5ecddfSPaul E. McKenney 
646ca5ecddfSPaul E. McKenney /**
647ca5ecddfSPaul E. McKenney  * rcu_dereference_check() - rcu_dereference with debug checking
648c08c68ddSDavid Howells  * @p: The pointer to read, prior to dereferencing
649c08c68ddSDavid Howells  * @c: The conditions under which the dereference will take place
650632ee200SPaul E. McKenney  *
651c08c68ddSDavid Howells  * Do an rcu_dereference(), but check that the conditions under which the
652ca5ecddfSPaul E. McKenney  * dereference will take place are correct.  Typically the conditions
653ca5ecddfSPaul E. McKenney  * indicate the various locking conditions that should be held at that
654ca5ecddfSPaul E. McKenney  * point.  The check should return true if the conditions are satisfied.
655ca5ecddfSPaul E. McKenney  * An implicit check for being in an RCU read-side critical section
656ca5ecddfSPaul E. McKenney  * (rcu_read_lock()) is included.
657c08c68ddSDavid Howells  *
658c08c68ddSDavid Howells  * For example:
659c08c68ddSDavid Howells  *
660ca5ecddfSPaul E. McKenney  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
661c08c68ddSDavid Howells  *
662c08c68ddSDavid Howells  * could be used to indicate to lockdep that foo->bar may only be dereferenced
663ca5ecddfSPaul E. McKenney  * if either rcu_read_lock() is held, or that the lock required to replace
664c08c68ddSDavid Howells  * the bar struct at foo->bar is held.
665c08c68ddSDavid Howells  *
666c08c68ddSDavid Howells  * Note that the list of conditions may also include indications of when a lock
667c08c68ddSDavid Howells  * need not be held, for example during initialisation or destruction of the
668c08c68ddSDavid Howells  * target struct:
669c08c68ddSDavid Howells  *
670ca5ecddfSPaul E. McKenney  *	bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
671c08c68ddSDavid Howells  *					      atomic_read(&foo->usage) == 0);
672ca5ecddfSPaul E. McKenney  *
673ca5ecddfSPaul E. McKenney  * Inserts memory barriers on architectures that require them
674ca5ecddfSPaul E. McKenney  * (currently only the Alpha), prevents the compiler from refetching
675ca5ecddfSPaul E. McKenney  * (and from merging fetches), and, more importantly, documents exactly
676ca5ecddfSPaul E. McKenney  * which pointers are protected by RCU and checks that the pointer is
677ca5ecddfSPaul E. McKenney  * annotated as __rcu.
678632ee200SPaul E. McKenney  */
679632ee200SPaul E. McKenney #define rcu_dereference_check(p, c) \
68024ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
68124ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_held(), __rcu)
682632ee200SPaul E. McKenney 
683b62730baSPaul E. McKenney /**
684ca5ecddfSPaul E. McKenney  * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
685ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
686ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
687ca5ecddfSPaul E. McKenney  *
6881893afd6SPaul E. McKenney  * This is the RCU-bh counterpart to rcu_dereference_check().  However,
6891893afd6SPaul E. McKenney  * please note that starting in v5.0 kernels, vanilla RCU grace periods
6901893afd6SPaul E. McKenney  * wait for local_bh_disable() regions of code in addition to regions of
6911893afd6SPaul E. McKenney  * code demarked by rcu_read_lock() and rcu_read_unlock().  This means
6921893afd6SPaul E. McKenney  * that synchronize_rcu(), call_rcu, and friends all take not only
6931893afd6SPaul E. McKenney  * rcu_read_lock() but also rcu_read_lock_bh() into account.
694ca5ecddfSPaul E. McKenney  */
695ca5ecddfSPaul E. McKenney #define rcu_dereference_bh_check(p, c) \
69624ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
69724ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_bh_held(), __rcu)
698ca5ecddfSPaul E. McKenney 
699ca5ecddfSPaul E. McKenney /**
700ca5ecddfSPaul E. McKenney  * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
701ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
702ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
703ca5ecddfSPaul E. McKenney  *
704ca5ecddfSPaul E. McKenney  * This is the RCU-sched counterpart to rcu_dereference_check().
7051893afd6SPaul E. McKenney  * However, please note that starting in v5.0 kernels, vanilla RCU grace
7061893afd6SPaul E. McKenney  * periods wait for preempt_disable() regions of code in addition to
7071893afd6SPaul E. McKenney  * regions of code demarked by rcu_read_lock() and rcu_read_unlock().
7081893afd6SPaul E. McKenney  * This means that synchronize_rcu(), call_rcu, and friends all take not
7091893afd6SPaul E. McKenney  * only rcu_read_lock() but also rcu_read_lock_sched() into account.
710ca5ecddfSPaul E. McKenney  */
711ca5ecddfSPaul E. McKenney #define rcu_dereference_sched_check(p, c) \
71224ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), \
71324ba5301SChun-Hung Tseng 				(c) || rcu_read_lock_sched_held(), \
714ca5ecddfSPaul E. McKenney 				__rcu)
715ca5ecddfSPaul E. McKenney 
71612bcbe66SSteven Rostedt /*
71712bcbe66SSteven Rostedt  * The tracing infrastructure traces RCU (we want that), but unfortunately
71812bcbe66SSteven Rostedt  * some of the RCU checks causes tracing to lock up the system.
71912bcbe66SSteven Rostedt  *
720f039f0afSAlexey Kardashevskiy  * The no-tracing version of rcu_dereference_raw() must not call
72112bcbe66SSteven Rostedt  * rcu_read_lock_held().
72212bcbe66SSteven Rostedt  */
72324ba5301SChun-Hung Tseng #define rcu_dereference_raw_check(p) \
72424ba5301SChun-Hung Tseng 	__rcu_dereference_check((p), __UNIQUE_ID(rcu), 1, __rcu)
72512bcbe66SSteven Rostedt 
726ca5ecddfSPaul E. McKenney /**
727ca5ecddfSPaul E. McKenney  * rcu_dereference_protected() - fetch RCU pointer when updates prevented
728ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
729ca5ecddfSPaul E. McKenney  * @c: The conditions under which the dereference will take place
730b62730baSPaul E. McKenney  *
731b62730baSPaul E. McKenney  * Return the value of the specified RCU-protected pointer, but omit
732137f61f6SPaul E. McKenney  * the READ_ONCE().  This is useful in cases where update-side locks
733137f61f6SPaul E. McKenney  * prevent the value of the pointer from changing.  Please note that this
734137f61f6SPaul E. McKenney  * primitive does *not* prevent the compiler from repeating this reference
735137f61f6SPaul E. McKenney  * or combining it with other references, so it should not be used without
736137f61f6SPaul E. McKenney  * protection of appropriate locks.
737ca5ecddfSPaul E. McKenney  *
738ca5ecddfSPaul E. McKenney  * This function is only for update-side use.  Using this function
739ca5ecddfSPaul E. McKenney  * when protected only by rcu_read_lock() will result in infrequent
740ca5ecddfSPaul E. McKenney  * but very ugly failures.
741b62730baSPaul E. McKenney  */
742b62730baSPaul E. McKenney #define rcu_dereference_protected(p, c) \
74324ba5301SChun-Hung Tseng 	__rcu_dereference_protected((p), __UNIQUE_ID(rcu), (c), __rcu)
744bc33f24bSPaul E. McKenney 
745ca5ecddfSPaul E. McKenney 
746ca5ecddfSPaul E. McKenney /**
747ca5ecddfSPaul E. McKenney  * rcu_dereference() - fetch RCU-protected pointer for dereferencing
748ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
749ca5ecddfSPaul E. McKenney  *
750ca5ecddfSPaul E. McKenney  * This is a simple wrapper around rcu_dereference_check().
751ca5ecddfSPaul E. McKenney  */
752ca5ecddfSPaul E. McKenney #define rcu_dereference(p) rcu_dereference_check(p, 0)
753ca5ecddfSPaul E. McKenney 
754ca5ecddfSPaul E. McKenney /**
755ca5ecddfSPaul E. McKenney  * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
756ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
757ca5ecddfSPaul E. McKenney  *
758ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.
759ca5ecddfSPaul E. McKenney  */
760ca5ecddfSPaul E. McKenney #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
761ca5ecddfSPaul E. McKenney 
762ca5ecddfSPaul E. McKenney /**
763ca5ecddfSPaul E. McKenney  * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
764ca5ecddfSPaul E. McKenney  * @p: The pointer to read, prior to dereferencing
765ca5ecddfSPaul E. McKenney  *
766ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.
767ca5ecddfSPaul E. McKenney  */
768ca5ecddfSPaul E. McKenney #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
769ca5ecddfSPaul E. McKenney 
770ca5ecddfSPaul E. McKenney /**
771c3ac7cf1SPaul E. McKenney  * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
772c3ac7cf1SPaul E. McKenney  * @p: The pointer to hand off
773c3ac7cf1SPaul E. McKenney  *
774c3ac7cf1SPaul E. McKenney  * This is simply an identity function, but it documents where a pointer
775c3ac7cf1SPaul E. McKenney  * is handed off from RCU to some other synchronization mechanism, for
776c3ac7cf1SPaul E. McKenney  * example, reference counting or locking.  In C11, it would map to
7771445e917SMauro Carvalho Chehab  * kill_dependency().  It could be used as follows::
7781445e917SMauro Carvalho Chehab  *
779c3ac7cf1SPaul E. McKenney  *	rcu_read_lock();
780c3ac7cf1SPaul E. McKenney  *	p = rcu_dereference(gp);
781c3ac7cf1SPaul E. McKenney  *	long_lived = is_long_lived(p);
782c3ac7cf1SPaul E. McKenney  *	if (long_lived) {
783c3ac7cf1SPaul E. McKenney  *		if (!atomic_inc_not_zero(p->refcnt))
784c3ac7cf1SPaul E. McKenney  *			long_lived = false;
785c3ac7cf1SPaul E. McKenney  *		else
786c3ac7cf1SPaul E. McKenney  *			p = rcu_pointer_handoff(p);
787c3ac7cf1SPaul E. McKenney  *	}
788c3ac7cf1SPaul E. McKenney  *	rcu_read_unlock();
789c3ac7cf1SPaul E. McKenney  */
790c3ac7cf1SPaul E. McKenney #define rcu_pointer_handoff(p) (p)
791c3ac7cf1SPaul E. McKenney 
792c3ac7cf1SPaul E. McKenney /**
793ca5ecddfSPaul E. McKenney  * rcu_read_lock() - mark the beginning of an RCU read-side critical section
7941da177e4SLinus Torvalds  *
7959b06e818SPaul E. McKenney  * When synchronize_rcu() is invoked on one CPU while other CPUs
7961da177e4SLinus Torvalds  * are within RCU read-side critical sections, then the
7979b06e818SPaul E. McKenney  * synchronize_rcu() is guaranteed to block until after all the other
7981da177e4SLinus Torvalds  * CPUs exit their critical sections.  Similarly, if call_rcu() is invoked
7991da177e4SLinus Torvalds  * on one CPU while other CPUs are within RCU read-side critical
8001da177e4SLinus Torvalds  * sections, invocation of the corresponding RCU callback is deferred
8011da177e4SLinus Torvalds  * until after the all the other CPUs exit their critical sections.
8021da177e4SLinus Torvalds  *
80373298c7cSPaul E. McKenney  * Both synchronize_rcu() and call_rcu() also wait for regions of code
80473298c7cSPaul E. McKenney  * with preemption disabled, including regions of code with interrupts or
80573298c7cSPaul E. McKenney  * softirqs disabled.
8061893afd6SPaul E. McKenney  *
8071da177e4SLinus Torvalds  * Note, however, that RCU callbacks are permitted to run concurrently
80877d8485aSPaul E. McKenney  * with new RCU read-side critical sections.  One way that this can happen
8091da177e4SLinus Torvalds  * is via the following sequence of events: (1) CPU 0 enters an RCU
8101da177e4SLinus Torvalds  * read-side critical section, (2) CPU 1 invokes call_rcu() to register
8111da177e4SLinus Torvalds  * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
8121da177e4SLinus Torvalds  * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
8131da177e4SLinus Torvalds  * callback is invoked.  This is legal, because the RCU read-side critical
8141da177e4SLinus Torvalds  * section that was running concurrently with the call_rcu() (and which
8151da177e4SLinus Torvalds  * therefore might be referencing something that the corresponding RCU
8161da177e4SLinus Torvalds  * callback would free up) has completed before the corresponding
8171da177e4SLinus Torvalds  * RCU callback is invoked.
8181da177e4SLinus Torvalds  *
8191da177e4SLinus Torvalds  * RCU read-side critical sections may be nested.  Any deferred actions
8201da177e4SLinus Torvalds  * will be deferred until the outermost RCU read-side critical section
8211da177e4SLinus Torvalds  * completes.
8221da177e4SLinus Torvalds  *
8239079fd7cSPaul E. McKenney  * You can avoid reading and understanding the next paragraph by
8249079fd7cSPaul E. McKenney  * following this rule: don't put anything in an rcu_read_lock() RCU
82590326f05SSebastian Andrzej Siewior  * read-side critical section that would block in a !PREEMPTION kernel.
8269079fd7cSPaul E. McKenney  * But if you want the full story, read on!
8279079fd7cSPaul E. McKenney  *
828b3e627d3SLai Jiangshan  * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU),
829ab74fdfdSPaul E. McKenney  * it is illegal to block while in an RCU read-side critical section.
83001b1d88bSThomas Gleixner  * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION
831ab74fdfdSPaul E. McKenney  * kernel builds, RCU read-side critical sections may be preempted,
832ab74fdfdSPaul E. McKenney  * but explicit blocking is illegal.  Finally, in preemptible RCU
833ab74fdfdSPaul E. McKenney  * implementations in real-time (with -rt patchset) kernel builds, RCU
834ab74fdfdSPaul E. McKenney  * read-side critical sections may be preempted and they may also block, but
835ab74fdfdSPaul E. McKenney  * only when acquiring spinlocks that are subject to priority inheritance.
8361da177e4SLinus Torvalds  */
rcu_read_lock(void)8376da9f775SWaiman Long static __always_inline void rcu_read_lock(void)
838bc33f24bSPaul E. McKenney {
839bc33f24bSPaul E. McKenney 	__rcu_read_lock();
840bc33f24bSPaul E. McKenney 	__acquire(RCU);
841d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_lock_map);
842f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
843bde23c68SHeiko Carstens 			 "rcu_read_lock() used illegally while idle");
844bc33f24bSPaul E. McKenney }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /*
8471da177e4SLinus Torvalds  * So where is rcu_write_lock()?  It does not exist, as there is no
8481da177e4SLinus Torvalds  * way for writers to lock out RCU readers.  This is a feature, not
8491da177e4SLinus Torvalds  * a bug -- this property is what provides RCU's performance benefits.
8501da177e4SLinus Torvalds  * Of course, writers must coordinate with each other.  The normal
8511da177e4SLinus Torvalds  * spinlock primitives work well for this, but any other technique may be
8521da177e4SLinus Torvalds  * used as well.  RCU does not care how the writers keep out of each
8531da177e4SLinus Torvalds  * others' way, as long as they do so.
8541da177e4SLinus Torvalds  */
8553d76c082SPaul E. McKenney 
8563d76c082SPaul E. McKenney /**
857ca5ecddfSPaul E. McKenney  * rcu_read_unlock() - marks the end of an RCU read-side critical section.
8583d76c082SPaul E. McKenney  *
85902238460SPaul E. McKenney  * In almost all situations, rcu_read_unlock() is immune from deadlock.
86073298c7cSPaul E. McKenney  * This deadlock immunity also extends to the scheduler's runqueue
86173298c7cSPaul E. McKenney  * and priority-inheritance spinlocks, courtesy of the quiescent-state
86273298c7cSPaul E. McKenney  * deferral that is carried out when rcu_read_unlock() is invoked with
86373298c7cSPaul E. McKenney  * interrupts disabled.
864f27bc487SPaul E. McKenney  *
8653d76c082SPaul E. McKenney  * See rcu_read_lock() for more information.
8663d76c082SPaul E. McKenney  */
rcu_read_unlock(void)867bc33f24bSPaul E. McKenney static inline void rcu_read_unlock(void)
868bc33f24bSPaul E. McKenney {
869f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
870bde23c68SHeiko Carstens 			 "rcu_read_unlock() used illegally while idle");
871058e8778SUladzislau Rezki (Sony) 	rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
872bc33f24bSPaul E. McKenney 	__release(RCU);
873bc33f24bSPaul E. McKenney 	__rcu_read_unlock();
874bc33f24bSPaul E. McKenney }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /**
877ca5ecddfSPaul E. McKenney  * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
8781da177e4SLinus Torvalds  *
8791893afd6SPaul E. McKenney  * This is equivalent to rcu_read_lock(), but also disables softirqs.
8801893afd6SPaul E. McKenney  * Note that anything else that disables softirqs can also serve as an RCU
8811893afd6SPaul E. McKenney  * read-side critical section.  However, please note that this equivalence
8821893afd6SPaul E. McKenney  * applies only to v5.0 and later.  Before v5.0, rcu_read_lock() and
8831893afd6SPaul E. McKenney  * rcu_read_lock_bh() were unrelated.
8843842a083SPaul E. McKenney  *
8853842a083SPaul E. McKenney  * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
8863842a083SPaul E. McKenney  * must occur in the same context, for example, it is illegal to invoke
8873842a083SPaul E. McKenney  * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
8883842a083SPaul E. McKenney  * was invoked from some other task.
8891da177e4SLinus Torvalds  */
rcu_read_lock_bh(void)890bc33f24bSPaul E. McKenney static inline void rcu_read_lock_bh(void)
891bc33f24bSPaul E. McKenney {
8926206ab9bSPaul E. McKenney 	local_bh_disable();
893bc33f24bSPaul E. McKenney 	__acquire(RCU_BH);
894d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_bh_lock_map);
895f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
896bde23c68SHeiko Carstens 			 "rcu_read_lock_bh() used illegally while idle");
897bc33f24bSPaul E. McKenney }
8981da177e4SLinus Torvalds 
899000601bbSTobias Klauser /**
900000601bbSTobias Klauser  * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section
9011da177e4SLinus Torvalds  *
9021da177e4SLinus Torvalds  * See rcu_read_lock_bh() for more information.
9031da177e4SLinus Torvalds  */
rcu_read_unlock_bh(void)904bc33f24bSPaul E. McKenney static inline void rcu_read_unlock_bh(void)
905bc33f24bSPaul E. McKenney {
906f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
907bde23c68SHeiko Carstens 			 "rcu_read_unlock_bh() used illegally while idle");
908d8ab29f8SPaul E. McKenney 	rcu_lock_release(&rcu_bh_lock_map);
909bc33f24bSPaul E. McKenney 	__release(RCU_BH);
9106206ab9bSPaul E. McKenney 	local_bh_enable();
911bc33f24bSPaul E. McKenney }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /**
914ca5ecddfSPaul E. McKenney  * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
9151c50b728SMathieu Desnoyers  *
9161893afd6SPaul E. McKenney  * This is equivalent to rcu_read_lock(), but also disables preemption.
9171893afd6SPaul E. McKenney  * Read-side critical sections can also be introduced by anything else that
9181893afd6SPaul E. McKenney  * disables preemption, including local_irq_disable() and friends.  However,
9191893afd6SPaul E. McKenney  * please note that the equivalence to rcu_read_lock() applies only to
9201893afd6SPaul E. McKenney  * v5.0 and later.  Before v5.0, rcu_read_lock() and rcu_read_lock_sched()
9211893afd6SPaul E. McKenney  * were unrelated.
9223842a083SPaul E. McKenney  *
9233842a083SPaul E. McKenney  * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
9243842a083SPaul E. McKenney  * must occur in the same context, for example, it is illegal to invoke
9253842a083SPaul E. McKenney  * rcu_read_unlock_sched() from process context if the matching
9263842a083SPaul E. McKenney  * rcu_read_lock_sched() was invoked from an NMI handler.
9271c50b728SMathieu Desnoyers  */
rcu_read_lock_sched(void)928d6714c22SPaul E. McKenney static inline void rcu_read_lock_sched(void)
929d6714c22SPaul E. McKenney {
930d6714c22SPaul E. McKenney 	preempt_disable();
931bc33f24bSPaul E. McKenney 	__acquire(RCU_SCHED);
932d8ab29f8SPaul E. McKenney 	rcu_lock_acquire(&rcu_sched_lock_map);
933f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
934bde23c68SHeiko Carstens 			 "rcu_read_lock_sched() used illegally while idle");
935d6714c22SPaul E. McKenney }
9361eba8f84SPaul E. McKenney 
9371eba8f84SPaul E. McKenney /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_lock_sched_notrace(void)9387c614d64SPaul E. McKenney static inline notrace void rcu_read_lock_sched_notrace(void)
939d6714c22SPaul E. McKenney {
940d6714c22SPaul E. McKenney 	preempt_disable_notrace();
941bc33f24bSPaul E. McKenney 	__acquire(RCU_SCHED);
942d6714c22SPaul E. McKenney }
9431c50b728SMathieu Desnoyers 
944000601bbSTobias Klauser /**
945000601bbSTobias Klauser  * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section
9461c50b728SMathieu Desnoyers  *
947000601bbSTobias Klauser  * See rcu_read_lock_sched() for more information.
9481c50b728SMathieu Desnoyers  */
rcu_read_unlock_sched(void)949d6714c22SPaul E. McKenney static inline void rcu_read_unlock_sched(void)
950d6714c22SPaul E. McKenney {
951f78f5b90SPaul E. McKenney 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
952bde23c68SHeiko Carstens 			 "rcu_read_unlock_sched() used illegally while idle");
953d8ab29f8SPaul E. McKenney 	rcu_lock_release(&rcu_sched_lock_map);
954bc33f24bSPaul E. McKenney 	__release(RCU_SCHED);
955d6714c22SPaul E. McKenney 	preempt_enable();
956d6714c22SPaul E. McKenney }
9571eba8f84SPaul E. McKenney 
9581eba8f84SPaul E. McKenney /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
rcu_read_unlock_sched_notrace(void)9597c614d64SPaul E. McKenney static inline notrace void rcu_read_unlock_sched_notrace(void)
960d6714c22SPaul E. McKenney {
961bc33f24bSPaul E. McKenney 	__release(RCU_SCHED);
962d6714c22SPaul E. McKenney 	preempt_enable_notrace();
963d6714c22SPaul E. McKenney }
9641c50b728SMathieu Desnoyers 
9651c50b728SMathieu Desnoyers /**
966ca5ecddfSPaul E. McKenney  * RCU_INIT_POINTER() - initialize an RCU protected pointer
96727fdb35fSPaul E. McKenney  * @p: The pointer to be initialized.
96827fdb35fSPaul E. McKenney  * @v: The value to initialized the pointer to.
969ca5ecddfSPaul E. McKenney  *
9706846c0c5SPaul E. McKenney  * Initialize an RCU-protected pointer in special cases where readers
9716846c0c5SPaul E. McKenney  * do not need ordering constraints on the CPU or the compiler.  These
9726846c0c5SPaul E. McKenney  * special cases are:
9736846c0c5SPaul E. McKenney  *
97427fdb35fSPaul E. McKenney  * 1.	This use of RCU_INIT_POINTER() is NULLing out the pointer *or*
9756846c0c5SPaul E. McKenney  * 2.	The caller has taken whatever steps are required to prevent
97627fdb35fSPaul E. McKenney  *	RCU readers from concurrently accessing this pointer *or*
9776846c0c5SPaul E. McKenney  * 3.	The referenced data structure has already been exposed to
97827fdb35fSPaul E. McKenney  *	readers either at compile time or via rcu_assign_pointer() *and*
97927fdb35fSPaul E. McKenney  *
98027fdb35fSPaul E. McKenney  *	a.	You have not made *any* reader-visible changes to
98127fdb35fSPaul E. McKenney  *		this structure since then *or*
9826846c0c5SPaul E. McKenney  *	b.	It is OK for readers accessing this structure from its
9836846c0c5SPaul E. McKenney  *		new location to see the old state of the structure.  (For
9846846c0c5SPaul E. McKenney  *		example, the changes were to statistical counters or to
9856846c0c5SPaul E. McKenney  *		other state where exact synchronization is not required.)
9866846c0c5SPaul E. McKenney  *
9876846c0c5SPaul E. McKenney  * Failure to follow these rules governing use of RCU_INIT_POINTER() will
9886846c0c5SPaul E. McKenney  * result in impossible-to-diagnose memory corruption.  As in the structures
9896846c0c5SPaul E. McKenney  * will look OK in crash dumps, but any concurrent RCU readers might
9906846c0c5SPaul E. McKenney  * see pre-initialized values of the referenced data structure.  So
9916846c0c5SPaul E. McKenney  * please be very careful how you use RCU_INIT_POINTER()!!!
9926846c0c5SPaul E. McKenney  *
9936846c0c5SPaul E. McKenney  * If you are creating an RCU-protected linked structure that is accessed
9946846c0c5SPaul E. McKenney  * by a single external-to-structure RCU-protected pointer, then you may
9956846c0c5SPaul E. McKenney  * use RCU_INIT_POINTER() to initialize the internal RCU-protected
9966846c0c5SPaul E. McKenney  * pointers, but you must use rcu_assign_pointer() to initialize the
99727fdb35fSPaul E. McKenney  * external-to-structure pointer *after* you have completely initialized
9986846c0c5SPaul E. McKenney  * the reader-accessible portions of the linked structure.
99971a9b269SPaul E. McKenney  *
100071a9b269SPaul E. McKenney  * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
100171a9b269SPaul E. McKenney  * ordering guarantees for either the CPU or the compiler.
1002ca5ecddfSPaul E. McKenney  */
1003ca5ecddfSPaul E. McKenney #define RCU_INIT_POINTER(p, v) \
1004d1b88eb9SPaul E. McKenney 	do { \
1005423a86a6SJoel Fernandes (Google) 		rcu_check_sparse(p, __rcu); \
1006155d1d12SPeter Zijlstra 		WRITE_ONCE(p, RCU_INITIALIZER(v)); \
1007d1b88eb9SPaul E. McKenney 	} while (0)
10081da177e4SLinus Torvalds 
1009172708d0SPaul E. McKenney /**
1010172708d0SPaul E. McKenney  * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
101127fdb35fSPaul E. McKenney  * @p: The pointer to be initialized.
101227fdb35fSPaul E. McKenney  * @v: The value to initialized the pointer to.
1013172708d0SPaul E. McKenney  *
1014172708d0SPaul E. McKenney  * GCC-style initialization for an RCU-protected pointer in a structure field.
1015172708d0SPaul E. McKenney  */
1016172708d0SPaul E. McKenney #define RCU_POINTER_INITIALIZER(p, v) \
1017462225aeSPaul E. McKenney 		.p = RCU_INITIALIZER(v)
10189ab1544eSLai Jiangshan 
10199ab1544eSLai Jiangshan /**
10209ab1544eSLai Jiangshan  * kfree_rcu() - kfree an object after a grace period.
10217e3f926bSUladzislau Rezki (Sony)  * @ptr: pointer to kfree for double-argument invocations.
10227e3f926bSUladzislau Rezki (Sony)  * @rhf: the name of the struct rcu_head within the type of @ptr.
10239ab1544eSLai Jiangshan  *
10249ab1544eSLai Jiangshan  * Many rcu callbacks functions just call kfree() on the base structure.
10259ab1544eSLai Jiangshan  * These functions are trivial, but their size adds up, and furthermore
10269ab1544eSLai Jiangshan  * when they are used in a kernel module, that module must invoke the
10279ab1544eSLai Jiangshan  * high-latency rcu_barrier() function at module-unload time.
10289ab1544eSLai Jiangshan  *
102949d5377bSVlastimil Babka  * The kfree_rcu() function handles this issue. In order to have a universal
103049d5377bSVlastimil Babka  * callback function handling different offsets of rcu_head, the callback needs
103149d5377bSVlastimil Babka  * to determine the starting address of the freed object, which can be a large
103249d5377bSVlastimil Babka  * kmalloc or vmalloc allocation. To allow simply aligning the pointer down to
103349d5377bSVlastimil Babka  * page boundary for those, only offsets up to 4095 bytes can be accommodated.
10349ab1544eSLai Jiangshan  * If the offset is larger than 4095 bytes, a compile-time error will
10355ea5d1edSUladzislau Rezki (Sony)  * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can
10369ab1544eSLai Jiangshan  * either fall back to use of call_rcu() or rearrange the structure to
10379ab1544eSLai Jiangshan  * position the rcu_head structure into the first 4096 bytes.
10389ab1544eSLai Jiangshan  *
1039ae65a521SVlastimil Babka  * The object to be freed can be allocated either by kmalloc() or
1040ae65a521SVlastimil Babka  * kmem_cache_alloc().
1041ae65a521SVlastimil Babka  *
1042ae65a521SVlastimil Babka  * Note that the allowable offset might decrease in the future.
1043d8169d4cSJan Engelhardt  *
1044d8169d4cSJan Engelhardt  * The BUILD_BUG_ON check must not involve any function calls, hence the
1045d8169d4cSJan Engelhardt  * checks are done in macros here.
10469ab1544eSLai Jiangshan  */
10477e3f926bSUladzislau Rezki (Sony) #define kfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
10487e3f926bSUladzislau Rezki (Sony) #define kvfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf)
10490edd1b17SPaul E. McKenney 
1050ce4dce12SUladzislau Rezki (Sony) /**
10517e3f926bSUladzislau Rezki (Sony)  * kfree_rcu_mightsleep() - kfree an object after a grace period.
10527e3f926bSUladzislau Rezki (Sony)  * @ptr: pointer to kfree for single-argument invocations.
10531835f475SUladzislau Rezki (Sony)  *
10541835f475SUladzislau Rezki (Sony)  * When it comes to head-less variant, only one argument
10551835f475SUladzislau Rezki (Sony)  * is passed and that is just a pointer which has to be
10561835f475SUladzislau Rezki (Sony)  * freed after a grace period. Therefore the semantic is
10571835f475SUladzislau Rezki (Sony)  *
10587e3f926bSUladzislau Rezki (Sony)  *     kfree_rcu_mightsleep(ptr);
10591835f475SUladzislau Rezki (Sony)  *
1060150154aaSUladzislau Rezki (Sony)  * where @ptr is the pointer to be freed by kvfree().
10611835f475SUladzislau Rezki (Sony)  *
10621835f475SUladzislau Rezki (Sony)  * Please note, head-less way of freeing is permitted to
10631835f475SUladzislau Rezki (Sony)  * use from a context that has to follow might_sleep()
10641835f475SUladzislau Rezki (Sony)  * annotation. Otherwise, please switch and embed the
10651835f475SUladzislau Rezki (Sony)  * rcu_head structure within the type of @ptr.
1066ce4dce12SUladzislau Rezki (Sony)  */
10677e3f926bSUladzislau Rezki (Sony) #define kfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1068608723c4SUladzislau Rezki (Sony) #define kvfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr)
1069608723c4SUladzislau Rezki (Sony) 
1070b14ff274SVlastimil Babka /*
1071b14ff274SVlastimil Babka  * In mm/slab_common.c, no suitable header to include here.
1072b14ff274SVlastimil Babka  */
1073b14ff274SVlastimil Babka void kvfree_call_rcu(struct rcu_head *head, void *ptr);
1074b14ff274SVlastimil Babka 
107549d5377bSVlastimil Babka /*
107649d5377bSVlastimil Babka  * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the
107749d5377bSVlastimil Babka  * comment of kfree_rcu() for details.
107849d5377bSVlastimil Babka  */
10795130b8fdSUladzislau Rezki (Sony) #define kvfree_rcu_arg_2(ptr, rhf)					\
10805130b8fdSUladzislau Rezki (Sony) do {									\
10815130b8fdSUladzislau Rezki (Sony) 	typeof (ptr) ___p = (ptr);					\
10825130b8fdSUladzislau Rezki (Sony) 									\
10835ea5d1edSUladzislau Rezki (Sony) 	if (___p) {							\
108449d5377bSVlastimil Babka 		BUILD_BUG_ON(offsetof(typeof(*(ptr)), rhf) >= 4096);	\
108504a522b7SUladzislau Rezki (Sony) 		kvfree_call_rcu(&((___p)->rhf), (void *) (___p));	\
10865ea5d1edSUladzislau Rezki (Sony) 	}								\
10875130b8fdSUladzislau Rezki (Sony) } while (0)
10885130b8fdSUladzislau Rezki (Sony) 
10891835f475SUladzislau Rezki (Sony) #define kvfree_rcu_arg_1(ptr)					\
10901835f475SUladzislau Rezki (Sony) do {								\
10911835f475SUladzislau Rezki (Sony) 	typeof(ptr) ___p = (ptr);				\
10921835f475SUladzislau Rezki (Sony) 								\
10931835f475SUladzislau Rezki (Sony) 	if (___p)						\
109404a522b7SUladzislau Rezki (Sony) 		kvfree_call_rcu(NULL, (void *) (___p));		\
10951835f475SUladzislau Rezki (Sony) } while (0)
1096ce4dce12SUladzislau Rezki (Sony) 
1097274529baSPaul E. McKenney /*
1098d85b62f1SPaul E. McKenney  * Place this after a lock-acquisition primitive to guarantee that
1099d85b62f1SPaul E. McKenney  * an UNLOCK+LOCK pair acts as a full barrier.  This guarantee applies
1100d85b62f1SPaul E. McKenney  * if the UNLOCK and LOCK are executed by the same CPU or if the
1101d85b62f1SPaul E. McKenney  * UNLOCK and LOCK operate on the same lock variable.
1102d85b62f1SPaul E. McKenney  */
110377e58496SPaul E. McKenney #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
1104d85b62f1SPaul E. McKenney #define smp_mb__after_unlock_lock()	smp_mb()  /* Full ordering for lock. */
110577e58496SPaul E. McKenney #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1106d85b62f1SPaul E. McKenney #define smp_mb__after_unlock_lock()	do { } while (0)
110777e58496SPaul E. McKenney #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
1108d85b62f1SPaul E. McKenney 
1109274529baSPaul E. McKenney 
111074de6960SPaul E. McKenney /* Has the specified rcu_head structure been handed to call_rcu()? */
111174de6960SPaul E. McKenney 
11122aa55030SPaul E. McKenney /**
111374de6960SPaul E. McKenney  * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu()
111474de6960SPaul E. McKenney  * @rhp: The rcu_head structure to initialize.
111574de6960SPaul E. McKenney  *
111674de6960SPaul E. McKenney  * If you intend to invoke rcu_head_after_call_rcu() to test whether a
111774de6960SPaul E. McKenney  * given rcu_head structure has already been passed to call_rcu(), then
111874de6960SPaul E. McKenney  * you must also invoke this rcu_head_init() function on it just after
111974de6960SPaul E. McKenney  * allocating that structure.  Calls to this function must not race with
112074de6960SPaul E. McKenney  * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation.
112174de6960SPaul E. McKenney  */
rcu_head_init(struct rcu_head * rhp)112274de6960SPaul E. McKenney static inline void rcu_head_init(struct rcu_head *rhp)
112374de6960SPaul E. McKenney {
112474de6960SPaul E. McKenney 	rhp->func = (rcu_callback_t)~0L;
112574de6960SPaul E. McKenney }
112674de6960SPaul E. McKenney 
11272aa55030SPaul E. McKenney /**
1128000601bbSTobias Klauser  * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()?
112974de6960SPaul E. McKenney  * @rhp: The rcu_head structure to test.
11302aa55030SPaul E. McKenney  * @f: The function passed to call_rcu() along with @rhp.
113174de6960SPaul E. McKenney  *
113274de6960SPaul E. McKenney  * Returns @true if the @rhp has been passed to call_rcu() with @func,
113374de6960SPaul E. McKenney  * and @false otherwise.  Emits a warning in any other case, including
113474de6960SPaul E. McKenney  * the case where @rhp has already been invoked after a grace period.
113574de6960SPaul E. McKenney  * Calls to this function must not race with callback invocation.  One way
113674de6960SPaul E. McKenney  * to avoid such races is to enclose the call to rcu_head_after_call_rcu()
113774de6960SPaul E. McKenney  * in an RCU read-side critical section that includes a read-side fetch
113874de6960SPaul E. McKenney  * of the pointer to the structure containing @rhp.
113974de6960SPaul E. McKenney  */
114074de6960SPaul E. McKenney static inline bool
rcu_head_after_call_rcu(struct rcu_head * rhp,rcu_callback_t f)114174de6960SPaul E. McKenney rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
114274de6960SPaul E. McKenney {
1143b699cce1SNeeraj Upadhyay 	rcu_callback_t func = READ_ONCE(rhp->func);
1144b699cce1SNeeraj Upadhyay 
1145b699cce1SNeeraj Upadhyay 	if (func == f)
114674de6960SPaul E. McKenney 		return true;
1147b699cce1SNeeraj Upadhyay 	WARN_ON_ONCE(func != (rcu_callback_t)~0L);
114874de6960SPaul E. McKenney 	return false;
114974de6960SPaul E. McKenney }
115074de6960SPaul E. McKenney 
1151e1350e8eSBen Dooks /* kernel/ksysfs.c definitions */
1152e1350e8eSBen Dooks extern int rcu_expedited;
1153e1350e8eSBen Dooks extern int rcu_normal;
1154e1350e8eSBen Dooks 
115580cd613aSJohannes Berg DEFINE_LOCK_GUARD_0(rcu,
115680cd613aSJohannes Berg 	do {
115780cd613aSJohannes Berg 		rcu_read_lock();
115880cd613aSJohannes Berg 		/*
115980cd613aSJohannes Berg 		 * sparse doesn't call the cleanup function,
116080cd613aSJohannes Berg 		 * so just release immediately and don't track
116180cd613aSJohannes Berg 		 * the context. We don't need to anyway, since
116280cd613aSJohannes Berg 		 * the whole point of the guard is to not need
116380cd613aSJohannes Berg 		 * the explicit unlock.
116480cd613aSJohannes Berg 		 */
116580cd613aSJohannes Berg 		__release(RCU);
116680cd613aSJohannes Berg 	} while (0),
116780cd613aSJohannes Berg 	rcu_read_unlock())
116854da6a09SPeter Zijlstra 
11691da177e4SLinus Torvalds #endif /* __LINUX_RCUPDATE_H */
1170