xref: /openbmc/linux/Documentation/RCU/checklist.rst (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
16b05dfacSMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
26b05dfacSMauro Carvalho Chehab
36b05dfacSMauro Carvalho Chehab================================
46b05dfacSMauro Carvalho ChehabReview Checklist for RCU Patches
56b05dfacSMauro Carvalho Chehab================================
66b05dfacSMauro Carvalho Chehab
76b05dfacSMauro Carvalho Chehab
86b05dfacSMauro Carvalho ChehabThis document contains a checklist for producing and reviewing patches
96b05dfacSMauro Carvalho Chehabthat make use of RCU.  Violating any of the rules listed below will
106b05dfacSMauro Carvalho Chehabresult in the same sorts of problems that leaving out a locking primitive
116b05dfacSMauro Carvalho Chehabwould cause.  This list is based on experiences reviewing such patches
126b05dfacSMauro Carvalho Chehabover a rather long period of time, but improvements are always welcome!
136b05dfacSMauro Carvalho Chehab
146b05dfacSMauro Carvalho Chehab0.	Is RCU being applied to a read-mostly situation?  If the data
156b05dfacSMauro Carvalho Chehab	structure is updated more than about 10% of the time, then you
166b05dfacSMauro Carvalho Chehab	should strongly consider some other approach, unless detailed
176b05dfacSMauro Carvalho Chehab	performance measurements show that RCU is nonetheless the right
186b05dfacSMauro Carvalho Chehab	tool for the job.  Yes, RCU does reduce read-side overhead by
196b05dfacSMauro Carvalho Chehab	increasing write-side overhead, which is exactly why normal uses
206b05dfacSMauro Carvalho Chehab	of RCU will do much more reading than updating.
216b05dfacSMauro Carvalho Chehab
226b05dfacSMauro Carvalho Chehab	Another exception is where performance is not an issue, and RCU
236b05dfacSMauro Carvalho Chehab	provides a simpler implementation.  An example of this situation
246b05dfacSMauro Carvalho Chehab	is the dynamic NMI code in the Linux 2.6 kernel, at least on
256b05dfacSMauro Carvalho Chehab	architectures where NMIs are rare.
266b05dfacSMauro Carvalho Chehab
276b05dfacSMauro Carvalho Chehab	Yet another exception is where the low real-time latency of RCU's
286b05dfacSMauro Carvalho Chehab	read-side primitives is critically important.
296b05dfacSMauro Carvalho Chehab
306b05dfacSMauro Carvalho Chehab	One final exception is where RCU readers are used to prevent
316b05dfacSMauro Carvalho Chehab	the ABA problem (https://en.wikipedia.org/wiki/ABA_problem)
326b05dfacSMauro Carvalho Chehab	for lockless updates.  This does result in the mildly
336b05dfacSMauro Carvalho Chehab	counter-intuitive situation where rcu_read_lock() and
346b05dfacSMauro Carvalho Chehab	rcu_read_unlock() are used to protect updates, however, this
353e7768b7SPaul E. McKenney	approach can provide the same simplifications to certain types
363e7768b7SPaul E. McKenney	of lockless algorithms that garbage collectors do.
376b05dfacSMauro Carvalho Chehab
386b05dfacSMauro Carvalho Chehab1.	Does the update code have proper mutual exclusion?
396b05dfacSMauro Carvalho Chehab
40e3879ecdSAkira Yokosawa	RCU does allow *readers* to run (almost) naked, but *writers* must
416b05dfacSMauro Carvalho Chehab	still use some sort of mutual exclusion, such as:
426b05dfacSMauro Carvalho Chehab
436b05dfacSMauro Carvalho Chehab	a.	locking,
446b05dfacSMauro Carvalho Chehab	b.	atomic operations, or
456b05dfacSMauro Carvalho Chehab	c.	restricting updates to a single task.
466b05dfacSMauro Carvalho Chehab
476b05dfacSMauro Carvalho Chehab	If you choose #b, be prepared to describe how you have handled
486b05dfacSMauro Carvalho Chehab	memory barriers on weakly ordered machines (pretty much all of
496b05dfacSMauro Carvalho Chehab	them -- even x86 allows later loads to be reordered to precede
506b05dfacSMauro Carvalho Chehab	earlier stores), and be prepared to explain why this added
516b05dfacSMauro Carvalho Chehab	complexity is worthwhile.  If you choose #c, be prepared to
523e7768b7SPaul E. McKenney	explain how this single task does not become a major bottleneck
533e7768b7SPaul E. McKenney	on large systems (for example, if the task is updating information
543e7768b7SPaul E. McKenney	relating to itself that other tasks can read, there by definition
553e7768b7SPaul E. McKenney	can be no bottleneck).	Note that the definition of "large" has
563e7768b7SPaul E. McKenney	changed significantly:	Eight CPUs was "large" in the year 2000,
573e7768b7SPaul E. McKenney	but a hundred CPUs was unremarkable in 2017.
586b05dfacSMauro Carvalho Chehab
596b05dfacSMauro Carvalho Chehab2.	Do the RCU read-side critical sections make proper use of
606b05dfacSMauro Carvalho Chehab	rcu_read_lock() and friends?  These primitives are needed
616b05dfacSMauro Carvalho Chehab	to prevent grace periods from ending prematurely, which
626b05dfacSMauro Carvalho Chehab	could result in data being unceremoniously freed out from
636b05dfacSMauro Carvalho Chehab	under your read-side code, which can greatly increase the
646b05dfacSMauro Carvalho Chehab	actuarial risk of your kernel.
656b05dfacSMauro Carvalho Chehab
666b05dfacSMauro Carvalho Chehab	As a rough rule of thumb, any dereference of an RCU-protected
676b05dfacSMauro Carvalho Chehab	pointer must be covered by rcu_read_lock(), rcu_read_lock_bh(),
686b05dfacSMauro Carvalho Chehab	rcu_read_lock_sched(), or by the appropriate update-side lock.
694d2f862bSPaul E. McKenney	Explicit disabling of preemption (preempt_disable(), for example)
704d2f862bSPaul E. McKenney	can serve as rcu_read_lock_sched(), but is less readable and
714d2f862bSPaul E. McKenney	prevents lockdep from detecting locking issues.
724d2f862bSPaul E. McKenney
73*e5ad8b68SQiuxu Zhuo	Please note that you *cannot* rely on code known to be built
744d2f862bSPaul E. McKenney	only in non-preemptible kernels.  Such code can and will break,
754d2f862bSPaul E. McKenney	especially in kernels built with CONFIG_PREEMPT_COUNT=y.
766b05dfacSMauro Carvalho Chehab
776b05dfacSMauro Carvalho Chehab	Letting RCU-protected pointers "leak" out of an RCU read-side
789d3a0485SPaul Gortmaker	critical section is every bit as bad as letting them leak out
796b05dfacSMauro Carvalho Chehab	from under a lock.  Unless, of course, you have arranged some
806b05dfacSMauro Carvalho Chehab	other means of protection, such as a lock or a reference count
81e3879ecdSAkira Yokosawa	*before* letting them out of the RCU read-side critical section.
826b05dfacSMauro Carvalho Chehab
836b05dfacSMauro Carvalho Chehab3.	Does the update code tolerate concurrent accesses?
846b05dfacSMauro Carvalho Chehab
856b05dfacSMauro Carvalho Chehab	The whole point of RCU is to permit readers to run without
866b05dfacSMauro Carvalho Chehab	any locks or atomic operations.  This means that readers will
876b05dfacSMauro Carvalho Chehab	be running while updates are in progress.  There are a number
886b05dfacSMauro Carvalho Chehab	of ways to handle this concurrency, depending on the situation:
896b05dfacSMauro Carvalho Chehab
906b05dfacSMauro Carvalho Chehab	a.	Use the RCU variants of the list and hlist update
916b05dfacSMauro Carvalho Chehab		primitives to add, remove, and replace elements on
926b05dfacSMauro Carvalho Chehab		an RCU-protected list.	Alternatively, use the other
936b05dfacSMauro Carvalho Chehab		RCU-protected data structures that have been added to
946b05dfacSMauro Carvalho Chehab		the Linux kernel.
956b05dfacSMauro Carvalho Chehab
966b05dfacSMauro Carvalho Chehab		This is almost always the best approach.
976b05dfacSMauro Carvalho Chehab
986b05dfacSMauro Carvalho Chehab	b.	Proceed as in (a) above, but also maintain per-element
996b05dfacSMauro Carvalho Chehab		locks (that are acquired by both readers and writers)
1003e7768b7SPaul E. McKenney		that guard per-element state.  Fields that the readers
1013e7768b7SPaul E. McKenney		refrain from accessing can be guarded by some other lock
1023e7768b7SPaul E. McKenney		acquired only by updaters, if desired.
1036b05dfacSMauro Carvalho Chehab
1043e7768b7SPaul E. McKenney		This also works quite well.
1056b05dfacSMauro Carvalho Chehab
1066b05dfacSMauro Carvalho Chehab	c.	Make updates appear atomic to readers.	For example,
1076b05dfacSMauro Carvalho Chehab		pointer updates to properly aligned fields will
1086b05dfacSMauro Carvalho Chehab		appear atomic, as will individual atomic primitives.
109e3879ecdSAkira Yokosawa		Sequences of operations performed under a lock will *not*
1106b05dfacSMauro Carvalho Chehab		appear to be atomic to RCU readers, nor will sequences
1113e7768b7SPaul E. McKenney		of multiple atomic primitives.	One alternative is to
1123e7768b7SPaul E. McKenney		move multiple individual fields to a separate structure,
1133e7768b7SPaul E. McKenney		thus solving the multiple-field problem by imposing an
1143e7768b7SPaul E. McKenney		additional level of indirection.
1156b05dfacSMauro Carvalho Chehab
1166b05dfacSMauro Carvalho Chehab		This can work, but is starting to get a bit tricky.
1176b05dfacSMauro Carvalho Chehab
1183e7768b7SPaul E. McKenney	d.	Carefully order the updates and the reads so that readers
1193e7768b7SPaul E. McKenney		see valid data at all phases of the update.  This is often
1203e7768b7SPaul E. McKenney		more difficult than it sounds, especially given modern
1213e7768b7SPaul E. McKenney		CPUs' tendency to reorder memory references.  One must
1223e7768b7SPaul E. McKenney		usually liberally sprinkle memory-ordering operations
1233e7768b7SPaul E. McKenney		through the code, making it difficult to understand and
1243e7768b7SPaul E. McKenney		to test.  Where it works, it is better to use things
1253e7768b7SPaul E. McKenney		like smp_store_release() and smp_load_acquire(), but in
1263e7768b7SPaul E. McKenney		some cases the smp_mb() full memory barrier is required.
1276b05dfacSMauro Carvalho Chehab
1283e7768b7SPaul E. McKenney		As noted earlier, it is usually better to group the
1293e7768b7SPaul E. McKenney		changing data into a separate structure, so that the
1303e7768b7SPaul E. McKenney		change may be made to appear atomic by updating a pointer
1313e7768b7SPaul E. McKenney		to reference a new structure containing updated values.
1326b05dfacSMauro Carvalho Chehab
1336b05dfacSMauro Carvalho Chehab4.	Weakly ordered CPUs pose special challenges.  Almost all CPUs
1346b05dfacSMauro Carvalho Chehab	are weakly ordered -- even x86 CPUs allow later loads to be
1356b05dfacSMauro Carvalho Chehab	reordered to precede earlier stores.  RCU code must take all of
1366b05dfacSMauro Carvalho Chehab	the following measures to prevent memory-corruption problems:
1376b05dfacSMauro Carvalho Chehab
1386b05dfacSMauro Carvalho Chehab	a.	Readers must maintain proper ordering of their memory
1396b05dfacSMauro Carvalho Chehab		accesses.  The rcu_dereference() primitive ensures that
1406b05dfacSMauro Carvalho Chehab		the CPU picks up the pointer before it picks up the data
1416b05dfacSMauro Carvalho Chehab		that the pointer points to.  This really is necessary
1429d3a0485SPaul Gortmaker		on Alpha CPUs.
1436b05dfacSMauro Carvalho Chehab
1446b05dfacSMauro Carvalho Chehab		The rcu_dereference() primitive is also an excellent
1456b05dfacSMauro Carvalho Chehab		documentation aid, letting the person reading the
1466b05dfacSMauro Carvalho Chehab		code know exactly which pointers are protected by RCU.
1476b05dfacSMauro Carvalho Chehab		Please note that compilers can also reorder code, and
1486b05dfacSMauro Carvalho Chehab		they are becoming increasingly aggressive about doing
1496b05dfacSMauro Carvalho Chehab		just that.  The rcu_dereference() primitive therefore also
1506b05dfacSMauro Carvalho Chehab		prevents destructive compiler optimizations.  However,
1516b05dfacSMauro Carvalho Chehab		with a bit of devious creativity, it is possible to
1526b05dfacSMauro Carvalho Chehab		mishandle the return value from rcu_dereference().
153404147faSAkira Yokosawa		Please see rcu_dereference.rst for more information.
1546b05dfacSMauro Carvalho Chehab
1556b05dfacSMauro Carvalho Chehab		The rcu_dereference() primitive is used by the
1566b05dfacSMauro Carvalho Chehab		various "_rcu()" list-traversal primitives, such
1576b05dfacSMauro Carvalho Chehab		as the list_for_each_entry_rcu().  Note that it is
1586b05dfacSMauro Carvalho Chehab		perfectly legal (if redundant) for update-side code to
1596b05dfacSMauro Carvalho Chehab		use rcu_dereference() and the "_rcu()" list-traversal
1606b05dfacSMauro Carvalho Chehab		primitives.  This is particularly useful in code that
1616b05dfacSMauro Carvalho Chehab		is common to readers and updaters.  However, lockdep
1626b05dfacSMauro Carvalho Chehab		will complain if you access rcu_dereference() outside
163404147faSAkira Yokosawa		of an RCU read-side critical section.  See lockdep.rst
1646b05dfacSMauro Carvalho Chehab		to learn what to do about this.
1656b05dfacSMauro Carvalho Chehab
1666b05dfacSMauro Carvalho Chehab		Of course, neither rcu_dereference() nor the "_rcu()"
1676b05dfacSMauro Carvalho Chehab		list-traversal primitives can substitute for a good
1686b05dfacSMauro Carvalho Chehab		concurrency design coordinating among multiple updaters.
1696b05dfacSMauro Carvalho Chehab
1706b05dfacSMauro Carvalho Chehab	b.	If the list macros are being used, the list_add_tail_rcu()
1716b05dfacSMauro Carvalho Chehab		and list_add_rcu() primitives must be used in order
1726b05dfacSMauro Carvalho Chehab		to prevent weakly ordered machines from misordering
1736b05dfacSMauro Carvalho Chehab		structure initialization and pointer planting.
1746b05dfacSMauro Carvalho Chehab		Similarly, if the hlist macros are being used, the
1756b05dfacSMauro Carvalho Chehab		hlist_add_head_rcu() primitive is required.
1766b05dfacSMauro Carvalho Chehab
1776b05dfacSMauro Carvalho Chehab	c.	If the list macros are being used, the list_del_rcu()
1786b05dfacSMauro Carvalho Chehab		primitive must be used to keep list_del()'s pointer
1796b05dfacSMauro Carvalho Chehab		poisoning from inflicting toxic effects on concurrent
1806b05dfacSMauro Carvalho Chehab		readers.  Similarly, if the hlist macros are being used,
1816b05dfacSMauro Carvalho Chehab		the hlist_del_rcu() primitive is required.
1826b05dfacSMauro Carvalho Chehab
1836b05dfacSMauro Carvalho Chehab		The list_replace_rcu() and hlist_replace_rcu() primitives
1846b05dfacSMauro Carvalho Chehab		may be used to replace an old structure with a new one
1856b05dfacSMauro Carvalho Chehab		in their respective types of RCU-protected lists.
1866b05dfacSMauro Carvalho Chehab
1876b05dfacSMauro Carvalho Chehab	d.	Rules similar to (4b) and (4c) apply to the "hlist_nulls"
1886b05dfacSMauro Carvalho Chehab		type of RCU-protected linked lists.
1896b05dfacSMauro Carvalho Chehab
1906b05dfacSMauro Carvalho Chehab	e.	Updates must ensure that initialization of a given
1916b05dfacSMauro Carvalho Chehab		structure happens before pointers to that structure are
1926b05dfacSMauro Carvalho Chehab		publicized.  Use the rcu_assign_pointer() primitive
1936b05dfacSMauro Carvalho Chehab		when publicizing a pointer to a structure that can
1946b05dfacSMauro Carvalho Chehab		be traversed by an RCU read-side critical section.
1956b05dfacSMauro Carvalho Chehab
1963e7768b7SPaul E. McKenney5.	If any of call_rcu(), call_srcu(), call_rcu_tasks(),
1973e7768b7SPaul E. McKenney	call_rcu_tasks_rude(), or call_rcu_tasks_trace() is used,
1983e7768b7SPaul E. McKenney	the callback function may be invoked from softirq context,
1993e7768b7SPaul E. McKenney	and in any case with bottom halves disabled.  In particular,
2003e7768b7SPaul E. McKenney	this callback function cannot block.  If you need the callback
2013e7768b7SPaul E. McKenney	to block, run that code in a workqueue handler scheduled from
2023e7768b7SPaul E. McKenney	the callback.  The queue_rcu_work() function does this for you
2033e7768b7SPaul E. McKenney	in the case of call_rcu().
2046b05dfacSMauro Carvalho Chehab
2056b05dfacSMauro Carvalho Chehab6.	Since synchronize_rcu() can block, it cannot be called
2066b05dfacSMauro Carvalho Chehab	from any sort of irq context.  The same rule applies
2073e7768b7SPaul E. McKenney	for synchronize_srcu(), synchronize_rcu_expedited(),
2083e7768b7SPaul E. McKenney	synchronize_srcu_expedited(), synchronize_rcu_tasks(),
2093e7768b7SPaul E. McKenney	synchronize_rcu_tasks_rude(), and synchronize_rcu_tasks_trace().
2106b05dfacSMauro Carvalho Chehab
2116b05dfacSMauro Carvalho Chehab	The expedited forms of these primitives have the same semantics
2123e7768b7SPaul E. McKenney	as the non-expedited forms, but expediting is more CPU intensive.
2133e7768b7SPaul E. McKenney	Use of the expedited primitives should be restricted to rare
2143e7768b7SPaul E. McKenney	configuration-change operations that would not normally be
2153e7768b7SPaul E. McKenney	undertaken while a real-time workload is running.  Note that
2163e7768b7SPaul E. McKenney	IPI-sensitive real-time workloads can use the rcupdate.rcu_normal
2173e7768b7SPaul E. McKenney	kernel boot parameter to completely disable expedited grace
2183e7768b7SPaul E. McKenney	periods, though this might have performance implications.
2196b05dfacSMauro Carvalho Chehab
2206b05dfacSMauro Carvalho Chehab	In particular, if you find yourself invoking one of the expedited
2216b05dfacSMauro Carvalho Chehab	primitives repeatedly in a loop, please do everyone a favor:
2226b05dfacSMauro Carvalho Chehab	Restructure your code so that it batches the updates, allowing
2236b05dfacSMauro Carvalho Chehab	a single non-expedited primitive to cover the entire batch.
2246b05dfacSMauro Carvalho Chehab	This will very likely be faster than the loop containing the
2256b05dfacSMauro Carvalho Chehab	expedited primitive, and will be much much easier on the rest
2263e7768b7SPaul E. McKenney	of the system, especially to real-time workloads running on the
2273e7768b7SPaul E. McKenney	rest of the system.  Alternatively, instead use asynchronous
2283e7768b7SPaul E. McKenney	primitives such as call_rcu().
2296b05dfacSMauro Carvalho Chehab
2309a145c04SPaul E. McKenney7.	As of v4.20, a given kernel implements only one RCU flavor, which
2319a145c04SPaul E. McKenney	is RCU-sched for PREEMPTION=n and RCU-preempt for PREEMPTION=y.
2329a145c04SPaul E. McKenney	If the updater uses call_rcu() or synchronize_rcu(), then
2339a145c04SPaul E. McKenney	the corresponding readers may use:  (1) rcu_read_lock() and
2349a145c04SPaul E. McKenney	rcu_read_unlock(), (2) any pair of primitives that disables
2359a145c04SPaul E. McKenney	and re-enables softirq, for example, rcu_read_lock_bh() and
2369a145c04SPaul E. McKenney	rcu_read_unlock_bh(), or (3) any pair of primitives that disables
2379a145c04SPaul E. McKenney	and re-enables preemption, for example, rcu_read_lock_sched() and
2389a145c04SPaul E. McKenney	rcu_read_unlock_sched().  If the updater uses synchronize_srcu()
2399a145c04SPaul E. McKenney	or call_srcu(), then the corresponding readers must use
2409a145c04SPaul E. McKenney	srcu_read_lock() and srcu_read_unlock(), and with the same
2419a145c04SPaul E. McKenney	srcu_struct.  The rules for the expedited RCU grace-period-wait
2429a145c04SPaul E. McKenney	primitives are the same as for their non-expedited counterparts.
2436b05dfacSMauro Carvalho Chehab
2449a145c04SPaul E. McKenney	If the updater uses call_rcu_tasks() or synchronize_rcu_tasks(),
2459a145c04SPaul E. McKenney	then the readers must refrain from executing voluntary
2469a145c04SPaul E. McKenney	context switches, that is, from blocking.  If the updater uses
2479a145c04SPaul E. McKenney	call_rcu_tasks_trace() or synchronize_rcu_tasks_trace(), then
2489a145c04SPaul E. McKenney	the corresponding readers must use rcu_read_lock_trace() and
2499a145c04SPaul E. McKenney	rcu_read_unlock_trace().  If an updater uses call_rcu_tasks_rude()
2509a145c04SPaul E. McKenney	or synchronize_rcu_tasks_rude(), then the corresponding readers
2513e7768b7SPaul E. McKenney	must use anything that disables preemption, for example,
2523e7768b7SPaul E. McKenney	preempt_disable() and preempt_enable().
2539a145c04SPaul E. McKenney
2549a145c04SPaul E. McKenney	Mixing things up will result in confusion and broken kernels, and
2559a145c04SPaul E. McKenney	has even resulted in an exploitable security issue.  Therefore,
256e74c74f9SToke Høiland-Jørgensen	when using non-obvious pairs of primitives, commenting is
257e74c74f9SToke Høiland-Jørgensen	of course a must.  One example of non-obvious pairing is
258e74c74f9SToke Høiland-Jørgensen	the XDP feature in networking, which calls BPF programs from
259e74c74f9SToke Høiland-Jørgensen	network-driver NAPI (softirq) context.	BPF relies heavily on RCU
260e74c74f9SToke Høiland-Jørgensen	protection for its data structures, but because the BPF program
261e74c74f9SToke Høiland-Jørgensen	invocation happens entirely within a single local_bh_disable()
262e74c74f9SToke Høiland-Jørgensen	section in a NAPI poll cycle, this usage is safe.  The reason
263e74c74f9SToke Høiland-Jørgensen	that this usage is safe is that readers can use anything that
264e74c74f9SToke Høiland-Jørgensen	disables BH when updaters use call_rcu() or synchronize_rcu().
2656b05dfacSMauro Carvalho Chehab
2663e7768b7SPaul E. McKenney8.	Although synchronize_rcu() is slower than is call_rcu(),
2673e7768b7SPaul E. McKenney	it usually results in simpler code.  So, unless update
2683e7768b7SPaul E. McKenney	performance is critically important, the updaters cannot block,
2693e7768b7SPaul E. McKenney	or the latency of synchronize_rcu() is visible from userspace,
2703e7768b7SPaul E. McKenney	synchronize_rcu() should be used in preference to call_rcu().
2713e7768b7SPaul E. McKenney	Furthermore, kfree_rcu() and kvfree_rcu() usually result
2723e7768b7SPaul E. McKenney	in even simpler code than does synchronize_rcu() without
2733e7768b7SPaul E. McKenney	synchronize_rcu()'s multi-millisecond latency.	So please take
2743e7768b7SPaul E. McKenney	advantage of kfree_rcu()'s and kvfree_rcu()'s "fire and forget"
2753e7768b7SPaul E. McKenney	memory-freeing capabilities where it applies.
2766b05dfacSMauro Carvalho Chehab
2776b05dfacSMauro Carvalho Chehab	An especially important property of the synchronize_rcu()
2786b05dfacSMauro Carvalho Chehab	primitive is that it automatically self-limits: if grace periods
2796b05dfacSMauro Carvalho Chehab	are delayed for whatever reason, then the synchronize_rcu()
2806b05dfacSMauro Carvalho Chehab	primitive will correspondingly delay updates.  In contrast,
2816b05dfacSMauro Carvalho Chehab	code using call_rcu() should explicitly limit update rate in
2826b05dfacSMauro Carvalho Chehab	cases where grace periods are delayed, as failing to do so can
2836b05dfacSMauro Carvalho Chehab	result in excessive realtime latencies or even OOM conditions.
2846b05dfacSMauro Carvalho Chehab
2853e7768b7SPaul E. McKenney	Ways of gaining this self-limiting property when using call_rcu(),
2863e7768b7SPaul E. McKenney	kfree_rcu(), or kvfree_rcu() include:
2876b05dfacSMauro Carvalho Chehab
2886b05dfacSMauro Carvalho Chehab	a.	Keeping a count of the number of data-structure elements
2896b05dfacSMauro Carvalho Chehab		used by the RCU-protected data structure, including
2906b05dfacSMauro Carvalho Chehab		those waiting for a grace period to elapse.  Enforce a
2916b05dfacSMauro Carvalho Chehab		limit on this number, stalling updates as needed to allow
2926b05dfacSMauro Carvalho Chehab		previously deferred frees to complete.	Alternatively,
2936b05dfacSMauro Carvalho Chehab		limit only the number awaiting deferred free rather than
2946b05dfacSMauro Carvalho Chehab		the total number of elements.
2956b05dfacSMauro Carvalho Chehab
2966b05dfacSMauro Carvalho Chehab		One way to stall the updates is to acquire the update-side
2976b05dfacSMauro Carvalho Chehab		mutex.	(Don't try this with a spinlock -- other CPUs
2986b05dfacSMauro Carvalho Chehab		spinning on the lock could prevent the grace period
2996b05dfacSMauro Carvalho Chehab		from ever ending.)  Another way to stall the updates
3006b05dfacSMauro Carvalho Chehab		is for the updates to use a wrapper function around
3016b05dfacSMauro Carvalho Chehab		the memory allocator, so that this wrapper function
3026b05dfacSMauro Carvalho Chehab		simulates OOM when there is too much memory awaiting an
3036b05dfacSMauro Carvalho Chehab		RCU grace period.  There are of course many other
3046b05dfacSMauro Carvalho Chehab		variations on this theme.
3056b05dfacSMauro Carvalho Chehab
3066b05dfacSMauro Carvalho Chehab	b.	Limiting update rate.  For example, if updates occur only
3076b05dfacSMauro Carvalho Chehab		once per hour, then no explicit rate limiting is
3086b05dfacSMauro Carvalho Chehab		required, unless your system is already badly broken.
3096b05dfacSMauro Carvalho Chehab		Older versions of the dcache subsystem take this approach,
3106b05dfacSMauro Carvalho Chehab		guarding updates with a global lock, limiting their rate.
3116b05dfacSMauro Carvalho Chehab
3126b05dfacSMauro Carvalho Chehab	c.	Trusted update -- if updates can only be done manually by
3136b05dfacSMauro Carvalho Chehab		superuser or some other trusted user, then it might not
3146b05dfacSMauro Carvalho Chehab		be necessary to automatically limit them.  The theory
3156b05dfacSMauro Carvalho Chehab		here is that superuser already has lots of ways to crash
3166b05dfacSMauro Carvalho Chehab		the machine.
3176b05dfacSMauro Carvalho Chehab
3183e7768b7SPaul E. McKenney	d.	Periodically invoke rcu_barrier(), permitting a limited
3193e7768b7SPaul E. McKenney		number of updates per grace period.
3206b05dfacSMauro Carvalho Chehab
3213e7768b7SPaul E. McKenney	The same cautions apply to call_srcu(), call_rcu_tasks(),
3223e7768b7SPaul E. McKenney	call_rcu_tasks_rude(), and call_rcu_tasks_trace().  This is
3233e7768b7SPaul E. McKenney	why there is an srcu_barrier(), rcu_barrier_tasks(),
3243e7768b7SPaul E. McKenney	rcu_barrier_tasks_rude(), and rcu_barrier_tasks_rude(),
3253e7768b7SPaul E. McKenney	respectively.
3266b05dfacSMauro Carvalho Chehab
3273e7768b7SPaul E. McKenney	Note that although these primitives do take action to avoid
3283e7768b7SPaul E. McKenney	memory exhaustion when any given CPU has too many callbacks,
3293e7768b7SPaul E. McKenney	a determined user or administrator can still exhaust memory.
3303e7768b7SPaul E. McKenney	This is especially the case if a system with a large number of
3313e7768b7SPaul E. McKenney	CPUs has been configured to offload all of its RCU callbacks onto
3323e7768b7SPaul E. McKenney	a single CPU, or if the system has relatively little free memory.
3336b05dfacSMauro Carvalho Chehab
3346b05dfacSMauro Carvalho Chehab9.	All RCU list-traversal primitives, which include
3356b05dfacSMauro Carvalho Chehab	rcu_dereference(), list_for_each_entry_rcu(), and
3366b05dfacSMauro Carvalho Chehab	list_for_each_safe_rcu(), must be either within an RCU read-side
3376b05dfacSMauro Carvalho Chehab	critical section or must be protected by appropriate update-side
3386b05dfacSMauro Carvalho Chehab	locks.	RCU read-side critical sections are delimited by
3396b05dfacSMauro Carvalho Chehab	rcu_read_lock() and rcu_read_unlock(), or by similar primitives
3406b05dfacSMauro Carvalho Chehab	such as rcu_read_lock_bh() and rcu_read_unlock_bh(), in which
3416b05dfacSMauro Carvalho Chehab	case the matching rcu_dereference() primitive must be used in
3426b05dfacSMauro Carvalho Chehab	order to keep lockdep happy, in this case, rcu_dereference_bh().
3436b05dfacSMauro Carvalho Chehab
3446b05dfacSMauro Carvalho Chehab	The reason that it is permissible to use RCU list-traversal
3456b05dfacSMauro Carvalho Chehab	primitives when the update-side lock is held is that doing so
3466b05dfacSMauro Carvalho Chehab	can be quite helpful in reducing code bloat when common code is
3476b05dfacSMauro Carvalho Chehab	shared between readers and updaters.  Additional primitives
348404147faSAkira Yokosawa	are provided for this case, as discussed in lockdep.rst.
3496b05dfacSMauro Carvalho Chehab
35086b5a738SPaul E. McKenney	One exception to this rule is when data is only ever added to
35186b5a738SPaul E. McKenney	the linked data structure, and is never removed during any
35286b5a738SPaul E. McKenney	time that readers might be accessing that structure.  In such
35386b5a738SPaul E. McKenney	cases, READ_ONCE() may be used in place of rcu_dereference()
35486b5a738SPaul E. McKenney	and the read-side markers (rcu_read_lock() and rcu_read_unlock(),
35586b5a738SPaul E. McKenney	for example) may be omitted.
35686b5a738SPaul E. McKenney
3576b05dfacSMauro Carvalho Chehab10.	Conversely, if you are in an RCU read-side critical section,
358e3879ecdSAkira Yokosawa	and you don't hold the appropriate update-side lock, you *must*
3596b05dfacSMauro Carvalho Chehab	use the "_rcu()" variants of the list macros.  Failing to do so
3606b05dfacSMauro Carvalho Chehab	will break Alpha, cause aggressive compilers to generate bad code,
3613e7768b7SPaul E. McKenney	and confuse people trying to understand your code.
3626b05dfacSMauro Carvalho Chehab
3636b05dfacSMauro Carvalho Chehab11.	Any lock acquired by an RCU callback must be acquired elsewhere
3643e7768b7SPaul E. McKenney	with softirq disabled, e.g., via spin_lock_bh().  Failing to
3653e7768b7SPaul E. McKenney	disable softirq on a given acquisition of that lock will result
3663e7768b7SPaul E. McKenney	in deadlock as soon as the RCU softirq handler happens to run
3673e7768b7SPaul E. McKenney	your RCU callback while interrupting that acquisition's critical
3683e7768b7SPaul E. McKenney	section.
3696b05dfacSMauro Carvalho Chehab
3706b05dfacSMauro Carvalho Chehab12.	RCU callbacks can be and are executed in parallel.  In many cases,
3716b05dfacSMauro Carvalho Chehab	the callback code simply wrappers around kfree(), so that this
3726b05dfacSMauro Carvalho Chehab	is not an issue (or, more accurately, to the extent that it is
3736b05dfacSMauro Carvalho Chehab	an issue, the memory-allocator locking handles it).  However,
3746b05dfacSMauro Carvalho Chehab	if the callbacks do manipulate a shared data structure, they
3756b05dfacSMauro Carvalho Chehab	must use whatever locking or other synchronization is required
3766b05dfacSMauro Carvalho Chehab	to safely access and/or modify that data structure.
3776b05dfacSMauro Carvalho Chehab
3786b05dfacSMauro Carvalho Chehab	Do not assume that RCU callbacks will be executed on the same
3796b05dfacSMauro Carvalho Chehab	CPU that executed the corresponding call_rcu() or call_srcu().
3806b05dfacSMauro Carvalho Chehab	For example, if a given CPU goes offline while having an RCU
3816b05dfacSMauro Carvalho Chehab	callback pending, then that RCU callback will execute on some
3826b05dfacSMauro Carvalho Chehab	surviving CPU.	(If this was not the case, a self-spawning RCU
3836b05dfacSMauro Carvalho Chehab	callback would prevent the victim CPU from ever going offline.)
384e3879ecdSAkira Yokosawa	Furthermore, CPUs designated by rcu_nocbs= might well *always*
3856b05dfacSMauro Carvalho Chehab	have their RCU callbacks executed on some other CPUs, in fact,
3866b05dfacSMauro Carvalho Chehab	for some  real-time workloads, this is the whole point of using
3876b05dfacSMauro Carvalho Chehab	the rcu_nocbs= kernel boot parameter.
3886b05dfacSMauro Carvalho Chehab
3893e7768b7SPaul E. McKenney	In addition, do not assume that callbacks queued in a given order
3903e7768b7SPaul E. McKenney	will be invoked in that order, even if they all are queued on the
3913e7768b7SPaul E. McKenney	same CPU.  Furthermore, do not assume that same-CPU callbacks will
3923e7768b7SPaul E. McKenney	be invoked serially.  For example, in recent kernels, CPUs can be
3933e7768b7SPaul E. McKenney	switched between offloaded and de-offloaded callback invocation,
3943e7768b7SPaul E. McKenney	and while a given CPU is undergoing such a switch, its callbacks
3953e7768b7SPaul E. McKenney	might be concurrently invoked by that CPU's softirq handler and
3963e7768b7SPaul E. McKenney	that CPU's rcuo kthread.  At such times, that CPU's callbacks
3973e7768b7SPaul E. McKenney	might be executed both concurrently and out of order.
3983e7768b7SPaul E. McKenney
3993e7768b7SPaul E. McKenney13.	Unlike most flavors of RCU, it *is* permissible to block in an
4006b05dfacSMauro Carvalho Chehab	SRCU read-side critical section (demarked by srcu_read_lock()
4016b05dfacSMauro Carvalho Chehab	and srcu_read_unlock()), hence the "SRCU": "sleepable RCU".
4026b05dfacSMauro Carvalho Chehab	Please note that if you don't need to sleep in read-side critical
4036b05dfacSMauro Carvalho Chehab	sections, you should be using RCU rather than SRCU, because RCU
4046b05dfacSMauro Carvalho Chehab	is almost always faster and easier to use than is SRCU.
4056b05dfacSMauro Carvalho Chehab
4066b05dfacSMauro Carvalho Chehab	Also unlike other forms of RCU, explicit initialization and
4076b05dfacSMauro Carvalho Chehab	cleanup is required either at build time via DEFINE_SRCU()
4086b05dfacSMauro Carvalho Chehab	or DEFINE_STATIC_SRCU() or at runtime via init_srcu_struct()
4096b05dfacSMauro Carvalho Chehab	and cleanup_srcu_struct().  These last two are passed a
4106b05dfacSMauro Carvalho Chehab	"struct srcu_struct" that defines the scope of a given
4116b05dfacSMauro Carvalho Chehab	SRCU domain.  Once initialized, the srcu_struct is passed
4126b05dfacSMauro Carvalho Chehab	to srcu_read_lock(), srcu_read_unlock() synchronize_srcu(),
4136b05dfacSMauro Carvalho Chehab	synchronize_srcu_expedited(), and call_srcu().	A given
4146b05dfacSMauro Carvalho Chehab	synchronize_srcu() waits only for SRCU read-side critical
4156b05dfacSMauro Carvalho Chehab	sections governed by srcu_read_lock() and srcu_read_unlock()
4166b05dfacSMauro Carvalho Chehab	calls that have been passed the same srcu_struct.  This property
4176b05dfacSMauro Carvalho Chehab	is what makes sleeping read-side critical sections tolerable --
4186b05dfacSMauro Carvalho Chehab	a given subsystem delays only its own updates, not those of other
4196b05dfacSMauro Carvalho Chehab	subsystems using SRCU.	Therefore, SRCU is less prone to OOM the
4206b05dfacSMauro Carvalho Chehab	system than RCU would be if RCU's read-side critical sections
4216b05dfacSMauro Carvalho Chehab	were permitted to sleep.
4226b05dfacSMauro Carvalho Chehab
4236b05dfacSMauro Carvalho Chehab	The ability to sleep in read-side critical sections does not
4246b05dfacSMauro Carvalho Chehab	come for free.	First, corresponding srcu_read_lock() and
4256b05dfacSMauro Carvalho Chehab	srcu_read_unlock() calls must be passed the same srcu_struct.
4266b05dfacSMauro Carvalho Chehab	Second, grace-period-detection overhead is amortized only
4276b05dfacSMauro Carvalho Chehab	over those updates sharing a given srcu_struct, rather than
4286b05dfacSMauro Carvalho Chehab	being globally amortized as they are for other forms of RCU.
4296b05dfacSMauro Carvalho Chehab	Therefore, SRCU should be used in preference to rw_semaphore
4306b05dfacSMauro Carvalho Chehab	only in extremely read-intensive situations, or in situations
4316b05dfacSMauro Carvalho Chehab	requiring SRCU's read-side deadlock immunity or low read-side
4326b05dfacSMauro Carvalho Chehab	realtime latency.  You should also consider percpu_rw_semaphore
4336b05dfacSMauro Carvalho Chehab	when you need lightweight readers.
4346b05dfacSMauro Carvalho Chehab
4356b05dfacSMauro Carvalho Chehab	SRCU's expedited primitive (synchronize_srcu_expedited())
4366b05dfacSMauro Carvalho Chehab	never sends IPIs to other CPUs, so it is easier on
4376b05dfacSMauro Carvalho Chehab	real-time workloads than is synchronize_rcu_expedited().
4386b05dfacSMauro Carvalho Chehab
4393e7768b7SPaul E. McKenney	It is also permissible to sleep in RCU Tasks Trace read-side
4403e7768b7SPaul E. McKenney	critical, which are delimited by rcu_read_lock_trace() and
4413e7768b7SPaul E. McKenney	rcu_read_unlock_trace().  However, this is a specialized flavor
4423e7768b7SPaul E. McKenney	of RCU, and you should not use it without first checking with
4433e7768b7SPaul E. McKenney	its current users.  In most cases, you should instead use SRCU.
4443e7768b7SPaul E. McKenney
4456b05dfacSMauro Carvalho Chehab	Note that rcu_assign_pointer() relates to SRCU just as it does to
4466b05dfacSMauro Carvalho Chehab	other forms of RCU, but instead of rcu_dereference() you should
4476b05dfacSMauro Carvalho Chehab	use srcu_dereference() in order to avoid lockdep splats.
4486b05dfacSMauro Carvalho Chehab
4496b05dfacSMauro Carvalho Chehab14.	The whole point of call_rcu(), synchronize_rcu(), and friends
4506b05dfacSMauro Carvalho Chehab	is to wait until all pre-existing readers have finished before
4516b05dfacSMauro Carvalho Chehab	carrying out some otherwise-destructive operation.  It is
452e3879ecdSAkira Yokosawa	therefore critically important to *first* remove any path
4536b05dfacSMauro Carvalho Chehab	that readers can follow that could be affected by the
454e3879ecdSAkira Yokosawa	destructive operation, and *only then* invoke call_rcu(),
4556b05dfacSMauro Carvalho Chehab	synchronize_rcu(), or friends.
4566b05dfacSMauro Carvalho Chehab
4576b05dfacSMauro Carvalho Chehab	Because these primitives only wait for pre-existing readers, it
4586b05dfacSMauro Carvalho Chehab	is the caller's responsibility to guarantee that any subsequent
4596b05dfacSMauro Carvalho Chehab	readers will execute safely.
4606b05dfacSMauro Carvalho Chehab
461e3879ecdSAkira Yokosawa15.	The various RCU read-side primitives do *not* necessarily contain
4626b05dfacSMauro Carvalho Chehab	memory barriers.  You should therefore plan for the CPU
4636b05dfacSMauro Carvalho Chehab	and the compiler to freely reorder code into and out of RCU
4646b05dfacSMauro Carvalho Chehab	read-side critical sections.  It is the responsibility of the
4656b05dfacSMauro Carvalho Chehab	RCU update-side primitives to deal with this.
4666b05dfacSMauro Carvalho Chehab
4676b05dfacSMauro Carvalho Chehab	For SRCU readers, you can use smp_mb__after_srcu_read_unlock()
4686b05dfacSMauro Carvalho Chehab	immediately after an srcu_read_unlock() to get a full barrier.
4696b05dfacSMauro Carvalho Chehab
4706b05dfacSMauro Carvalho Chehab16.	Use CONFIG_PROVE_LOCKING, CONFIG_DEBUG_OBJECTS_RCU_HEAD, and the
4716b05dfacSMauro Carvalho Chehab	__rcu sparse checks to validate your RCU code.	These can help
4726b05dfacSMauro Carvalho Chehab	find problems as follows:
4736b05dfacSMauro Carvalho Chehab
4746b05dfacSMauro Carvalho Chehab	CONFIG_PROVE_LOCKING:
4753e7768b7SPaul E. McKenney		check that accesses to RCU-protected data structures
4763e7768b7SPaul E. McKenney		are carried out under the proper RCU read-side critical
4773e7768b7SPaul E. McKenney		section, while holding the right combination of locks,
4783e7768b7SPaul E. McKenney		or whatever other conditions are appropriate.
4796b05dfacSMauro Carvalho Chehab
4806b05dfacSMauro Carvalho Chehab	CONFIG_DEBUG_OBJECTS_RCU_HEAD:
4813e7768b7SPaul E. McKenney		check that you don't pass the same object to call_rcu()
4823e7768b7SPaul E. McKenney		(or friends) before an RCU grace period has elapsed
4833e7768b7SPaul E. McKenney		since the last time that you passed that same object to
4843e7768b7SPaul E. McKenney		call_rcu() (or friends).
4856b05dfacSMauro Carvalho Chehab
4866b05dfacSMauro Carvalho Chehab	__rcu sparse checks:
4873e7768b7SPaul E. McKenney		tag the pointer to the RCU-protected data structure
4883e7768b7SPaul E. McKenney		with __rcu, and sparse will warn you if you access that
4893e7768b7SPaul E. McKenney		pointer without the services of one of the variants
4903e7768b7SPaul E. McKenney		of rcu_dereference().
4916b05dfacSMauro Carvalho Chehab
4926b05dfacSMauro Carvalho Chehab	These debugging aids can help you find problems that are
4936b05dfacSMauro Carvalho Chehab	otherwise extremely difficult to spot.
4946b05dfacSMauro Carvalho Chehab
4953e7768b7SPaul E. McKenney17.	If you pass a callback function defined within a module to one of
4963e7768b7SPaul E. McKenney	call_rcu(), call_srcu(), call_rcu_tasks(), call_rcu_tasks_rude(),
4973e7768b7SPaul E. McKenney	or call_rcu_tasks_trace(), then it is necessary to wait for all
4983e7768b7SPaul E. McKenney	pending callbacks to be invoked before unloading that module.
4993e7768b7SPaul E. McKenney	Note that it is absolutely *not* sufficient to wait for a grace
5003e7768b7SPaul E. McKenney	period!  For example, synchronize_rcu() implementation is *not*
5013e7768b7SPaul E. McKenney	guaranteed to wait for callbacks registered on other CPUs via
5023e7768b7SPaul E. McKenney	call_rcu().  Or even on the current CPU if that CPU recently
5033e7768b7SPaul E. McKenney	went offline and came back online.
5046b05dfacSMauro Carvalho Chehab
5056b05dfacSMauro Carvalho Chehab	You instead need to use one of the barrier functions:
5066b05dfacSMauro Carvalho Chehab
5076b05dfacSMauro Carvalho Chehab	-	call_rcu() -> rcu_barrier()
5086b05dfacSMauro Carvalho Chehab	-	call_srcu() -> srcu_barrier()
5093e7768b7SPaul E. McKenney	-	call_rcu_tasks() -> rcu_barrier_tasks()
5103e7768b7SPaul E. McKenney	-	call_rcu_tasks_rude() -> rcu_barrier_tasks_rude()
5113e7768b7SPaul E. McKenney	-	call_rcu_tasks_trace() -> rcu_barrier_tasks_trace()
5126b05dfacSMauro Carvalho Chehab
513e3879ecdSAkira Yokosawa	However, these barrier functions are absolutely *not* guaranteed
5143e7768b7SPaul E. McKenney	to wait for a grace period.  For example, if there are no
5153e7768b7SPaul E. McKenney	call_rcu() callbacks queued anywhere in the system, rcu_barrier()
5163e7768b7SPaul E. McKenney	can and will return immediately.
5176b05dfacSMauro Carvalho Chehab
5183e7768b7SPaul E. McKenney	So if you need to wait for both a grace period and for all
5193e7768b7SPaul E. McKenney	pre-existing callbacks, you will need to invoke both functions,
5203e7768b7SPaul E. McKenney	with the pair depending on the flavor of RCU:
5213e7768b7SPaul E. McKenney
5223e7768b7SPaul E. McKenney	-	Either synchronize_rcu() or synchronize_rcu_expedited(),
5233e7768b7SPaul E. McKenney		together with rcu_barrier()
5243e7768b7SPaul E. McKenney	-	Either synchronize_srcu() or synchronize_srcu_expedited(),
5253e7768b7SPaul E. McKenney		together with and srcu_barrier()
5263e7768b7SPaul E. McKenney	-	synchronize_rcu_tasks() and rcu_barrier_tasks()
5273e7768b7SPaul E. McKenney	-	synchronize_tasks_rude() and rcu_barrier_tasks_rude()
5283e7768b7SPaul E. McKenney	-	synchronize_tasks_trace() and rcu_barrier_tasks_trace()
5293e7768b7SPaul E. McKenney
5303e7768b7SPaul E. McKenney	If necessary, you can use something like workqueues to execute
5313e7768b7SPaul E. McKenney	the requisite pair of functions concurrently.
5326b05dfacSMauro Carvalho Chehab
533404147faSAkira Yokosawa	See rcubarrier.rst for more information.
534