blob: 0e03c6ef3147a3ea06cef16fd3fd931ce7c4081a [file] [log] [blame]
Jiunn Changa9f09692019-06-26 15:07:01 -05001.. _rcu_doc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
Jiunn Changa9f09692019-06-26 15:07:01 -05003RCU Concepts
4============
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6The basic idea behind RCU (read-copy update) is to split destructive
7operations into two parts, one that prevents anyone from seeing the data
8item being destroyed, and one that actually carries out the destruction.
9A "grace period" must elapse between the two parts, and this grace period
10must be long enough that any readers accessing the item being deleted have
11since dropped their references. For example, an RCU-protected deletion
12from a linked list would first remove the item from the list, wait for
Jiunn Changa9f09692019-06-26 15:07:01 -050013a grace period to elapse, then free the element. See the
SeongJae Parkbe289562020-01-06 21:07:59 +010014:ref:`Documentation/RCU/listRCU.rst <list_rcu_doc>` for more information on
15using RCU with linked lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17Frequently Asked Questions
Jiunn Changa9f09692019-06-26 15:07:01 -050018--------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jiunn Changa9f09692019-06-26 15:07:01 -050020- Why would anyone want to use RCU?
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Jiunn Changa9f09692019-06-26 15:07:01 -050022 The advantage of RCU's two-part approach is that RCU readers need
23 not acquire any locks, perform any atomic instructions, write to
24 shared memory, or (on CPUs other than Alpha) execute any memory
25 barriers. The fact that these operations are quite expensive
26 on modern CPUs is what gives RCU its performance advantages
27 in read-mostly situations. The fact that RCU readers need not
28 acquire locks can also greatly simplify deadlock-avoidance code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jiunn Changa9f09692019-06-26 15:07:01 -050030- How can the updater tell when a grace period has completed
31 if the RCU readers give no indication when they are done?
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jiunn Changa9f09692019-06-26 15:07:01 -050033 Just as with spinlocks, RCU readers are not permitted to
34 block, switch to user-mode execution, or enter the idle loop.
35 Therefore, as soon as a CPU is seen passing through any of these
36 three states, we know that that CPU has exited any previous RCU
37 read-side critical sections. So, if we remove an item from a
38 linked list, and then wait until all CPUs have switched context,
39 executed in user mode, or executed in the idle loop, we can
40 safely free up that item.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jiunn Changa9f09692019-06-26 15:07:01 -050042 Preemptible variants of RCU (CONFIG_PREEMPT_RCU) get the
43 same effect, but require that the readers manipulate CPU-local
44 counters. These counters allow limited types of blocking within
45 RCU read-side critical sections. SRCU also uses CPU-local
46 counters, and permits general blocking within RCU read-side
47 critical sections. These variants of RCU detect grace periods
48 by sampling these counters.
Paul E. McKenneyf85d6c72008-01-25 21:08:25 +010049
Jiunn Changa9f09692019-06-26 15:07:01 -050050- If I am running on a uniprocessor kernel, which can only do one
51 thing at a time, why should I wait for a grace period?
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
SeongJae Parkbe289562020-01-06 21:07:59 +010053 See :ref:`Documentation/RCU/UP.rst <up_doc>` for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiunn Changa9f09692019-06-26 15:07:01 -050055- How can I see where RCU is currently used in the Linux kernel?
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jiunn Changa9f09692019-06-26 15:07:01 -050057 Search for "rcu_read_lock", "rcu_read_unlock", "call_rcu",
58 "rcu_read_lock_bh", "rcu_read_unlock_bh", "srcu_read_lock",
59 "srcu_read_unlock", "synchronize_rcu", "synchronize_net",
60 "synchronize_srcu", and the other RCU primitives. Or grab one
61 of the cscope databases from:
Paul E. McKenneyf85d6c72008-01-25 21:08:25 +010062
Jiunn Changa9f09692019-06-26 15:07:01 -050063 (http://www.rdrop.com/users/paulmck/RCU/linuxusage/rculocktab.html).
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jiunn Changa9f09692019-06-26 15:07:01 -050065- What guidelines should I follow when writing code that uses RCU?
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jiunn Changa9f09692019-06-26 15:07:01 -050067 See the checklist.txt file in this directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jiunn Changa9f09692019-06-26 15:07:01 -050069- Why the name "RCU"?
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
SeongJae Parkbe289562020-01-06 21:07:59 +010071 "RCU" stands for "read-copy update".
72 :ref:`Documentation/RCU/listRCU.rst <list_rcu_doc>` has more information on where
73 this name came from, search for "read-copy update" to find it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jiunn Changa9f09692019-06-26 15:07:01 -050075- I hear that RCU is patented? What is with that?
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jiunn Changa9f09692019-06-26 15:07:01 -050077 Yes, it is. There are several known patents related to RCU,
SeongJae Park6a534b22020-01-06 21:08:00 +010078 search for the string "Patent" in Documentation/RCU/RTFP.txt to find them.
Jiunn Changa9f09692019-06-26 15:07:01 -050079 Of these, one was allowed to lapse by the assignee, and the
80 others have been contributed to the Linux kernel under GPL.
81 There are now also LGPL implementations of user-level RCU
SeongJae Park06a649b2020-01-06 21:08:01 +010082 available (https://liburcu.org/).
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jiunn Changa9f09692019-06-26 15:07:01 -050084- I hear that RCU needs work in order to support realtime kernels?
Paul E. McKenneydd81eca2005-09-10 00:26:24 -070085
Jiunn Changa9f09692019-06-26 15:07:01 -050086 Realtime-friendly RCU can be enabled via the CONFIG_PREEMPT_RCU
87 kernel configuration parameter.
Paul E. McKenneydd81eca2005-09-10 00:26:24 -070088
Jiunn Changa9f09692019-06-26 15:07:01 -050089- Where can I find more information on RCU?
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
SeongJae Park6a534b22020-01-06 21:08:00 +010091 See the Documentation/RCU/RTFP.txt file.
Jiunn Changa9f09692019-06-26 15:07:01 -050092 Or point your browser at (http://www.rdrop.com/users/paulmck/RCU/).