Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 1 | .. _rcu_doc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 3 | RCU Concepts |
| 4 | ============ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | The basic idea behind RCU (read-copy update) is to split destructive |
| 7 | operations into two parts, one that prevents anyone from seeing the data |
| 8 | item being destroyed, and one that actually carries out the destruction. |
| 9 | A "grace period" must elapse between the two parts, and this grace period |
| 10 | must be long enough that any readers accessing the item being deleted have |
| 11 | since dropped their references. For example, an RCU-protected deletion |
| 12 | from a linked list would first remove the item from the list, wait for |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 13 | a grace period to elapse, then free the element. See the |
SeongJae Park | be28956 | 2020-01-06 21:07:59 +0100 | [diff] [blame] | 14 | :ref:`Documentation/RCU/listRCU.rst <list_rcu_doc>` for more information on |
| 15 | using RCU with linked lists. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | Frequently Asked Questions |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 18 | -------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 20 | - Why would anyone want to use RCU? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 22 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 30 | - How can the updater tell when a grace period has completed |
| 31 | if the RCU readers give no indication when they are done? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 33 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 42 | 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. McKenney | f85d6c7 | 2008-01-25 21:08:25 +0100 | [diff] [blame] | 49 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 50 | - 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
SeongJae Park | be28956 | 2020-01-06 21:07:59 +0100 | [diff] [blame] | 53 | See :ref:`Documentation/RCU/UP.rst <up_doc>` for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 55 | - How can I see where RCU is currently used in the Linux kernel? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 57 | 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. McKenney | f85d6c7 | 2008-01-25 21:08:25 +0100 | [diff] [blame] | 62 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 63 | (http://www.rdrop.com/users/paulmck/RCU/linuxusage/rculocktab.html). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 65 | - What guidelines should I follow when writing code that uses RCU? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 67 | See the checklist.txt file in this directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 69 | - Why the name "RCU"? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
SeongJae Park | be28956 | 2020-01-06 21:07:59 +0100 | [diff] [blame] | 71 | "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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 75 | - I hear that RCU is patented? What is with that? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 77 | Yes, it is. There are several known patents related to RCU, |
SeongJae Park | 6a534b2 | 2020-01-06 21:08:00 +0100 | [diff] [blame] | 78 | search for the string "Patent" in Documentation/RCU/RTFP.txt to find them. |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 79 | 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 Park | 06a649b | 2020-01-06 21:08:01 +0100 | [diff] [blame] | 82 | available (https://liburcu.org/). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 84 | - I hear that RCU needs work in order to support realtime kernels? |
Paul E. McKenney | dd81eca | 2005-09-10 00:26:24 -0700 | [diff] [blame] | 85 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 86 | Realtime-friendly RCU can be enabled via the CONFIG_PREEMPT_RCU |
| 87 | kernel configuration parameter. |
Paul E. McKenney | dd81eca | 2005-09-10 00:26:24 -0700 | [diff] [blame] | 88 | |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 89 | - Where can I find more information on RCU? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
SeongJae Park | 6a534b2 | 2020-01-06 21:08:00 +0100 | [diff] [blame] | 91 | See the Documentation/RCU/RTFP.txt file. |
Jiunn Chang | a9f0969 | 2019-06-26 15:07:01 -0500 | [diff] [blame] | 92 | Or point your browser at (http://www.rdrop.com/users/paulmck/RCU/). |