diff options
author | Joel Fernandes (Google) <joel@joelfernandes.org> | 2019-03-29 10:05:55 -0400 |
---|---|---|
committer | Paul E. McKenney <paulmck@linux.ibm.com> | 2019-05-28 12:02:16 -0400 |
commit | de1dbcee433ccff3e0a37698c65b40542c9d4cf1 (patch) | |
tree | 6465fbfa57570c5cd5e850f6c13d831c5cb7a8b0 /Documentation/RCU | |
parent | a188339ca5a396acc588e5851ed7e19f66b0ebd9 (diff) |
doc/rcuref: Document real world examples in kernel
Document similar real world examples in the kernel corresponding to the
second and third code snippets. Also correct an issue in
release_referenced() in the code snippet example.
Cc: oleg@redhat.com
Cc: jannh@google.com
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
[ paulmck: Do a bit of wordsmithing. ]
Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
Diffstat (limited to 'Documentation/RCU')
-rw-r--r-- | Documentation/RCU/rcuref.txt | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Documentation/RCU/rcuref.txt b/Documentation/RCU/rcuref.txt index 613033ff2b9b..5e6429d66c24 100644 --- a/Documentation/RCU/rcuref.txt +++ b/Documentation/RCU/rcuref.txt | |||
@@ -12,6 +12,7 @@ please read on. | |||
12 | Reference counting on elements of lists which are protected by traditional | 12 | Reference counting on elements of lists which are protected by traditional |
13 | reader/writer spinlocks or semaphores are straightforward: | 13 | reader/writer spinlocks or semaphores are straightforward: |
14 | 14 | ||
15 | CODE LISTING A: | ||
15 | 1. 2. | 16 | 1. 2. |
16 | add() search_and_reference() | 17 | add() search_and_reference() |
17 | { { | 18 | { { |
@@ -28,7 +29,8 @@ add() search_and_reference() | |||
28 | release_referenced() delete() | 29 | release_referenced() delete() |
29 | { { | 30 | { { |
30 | ... write_lock(&list_lock); | 31 | ... write_lock(&list_lock); |
31 | atomic_dec(&el->rc, relfunc) ... | 32 | if(atomic_dec_and_test(&el->rc)) ... |
33 | kfree(el); | ||
32 | ... remove_element | 34 | ... remove_element |
33 | } write_unlock(&list_lock); | 35 | } write_unlock(&list_lock); |
34 | ... | 36 | ... |
@@ -44,6 +46,7 @@ search_and_reference() could potentially hold reference to an element which | |||
44 | has already been deleted from the list/array. Use atomic_inc_not_zero() | 46 | has already been deleted from the list/array. Use atomic_inc_not_zero() |
45 | in this scenario as follows: | 47 | in this scenario as follows: |
46 | 48 | ||
49 | CODE LISTING B: | ||
47 | 1. 2. | 50 | 1. 2. |
48 | add() search_and_reference() | 51 | add() search_and_reference() |
49 | { { | 52 | { { |
@@ -79,6 +82,7 @@ search_and_reference() code path. In such cases, the | |||
79 | atomic_dec_and_test() may be moved from delete() to el_free() | 82 | atomic_dec_and_test() may be moved from delete() to el_free() |
80 | as follows: | 83 | as follows: |
81 | 84 | ||
85 | CODE LISTING C: | ||
82 | 1. 2. | 86 | 1. 2. |
83 | add() search_and_reference() | 87 | add() search_and_reference() |
84 | { { | 88 | { { |
@@ -114,6 +118,17 @@ element can therefore safely be freed. This in turn guarantees that if | |||
114 | any reader finds the element, that reader may safely acquire a reference | 118 | any reader finds the element, that reader may safely acquire a reference |
115 | without checking the value of the reference counter. | 119 | without checking the value of the reference counter. |
116 | 120 | ||
121 | A clear advantage of the RCU-based pattern in listing C over the one | ||
122 | in listing B is that any call to search_and_reference() that locates | ||
123 | a given object will succeed in obtaining a reference to that object, | ||
124 | even given a concurrent invocation of delete() for that same object. | ||
125 | Similarly, a clear advantage of both listings B and C over listing A is | ||
126 | that a call to delete() is not delayed even if there are an arbitrarily | ||
127 | large number of calls to search_and_reference() searching for the same | ||
128 | object that delete() was invoked on. Instead, all that is delayed is | ||
129 | the eventual invocation of kfree(), which is usually not a problem on | ||
130 | modern computer systems, even the small ones. | ||
131 | |||
117 | In cases where delete() can sleep, synchronize_rcu() can be called from | 132 | In cases where delete() can sleep, synchronize_rcu() can be called from |
118 | delete(), so that el_free() can be subsumed into delete as follows: | 133 | delete(), so that el_free() can be subsumed into delete as follows: |
119 | 134 | ||
@@ -130,3 +145,7 @@ delete() | |||
130 | kfree(el); | 145 | kfree(el); |
131 | ... | 146 | ... |
132 | } | 147 | } |
148 | |||
149 | As additional examples in the kernel, the pattern in listing C is used by | ||
150 | reference counting of struct pid, while the pattern in listing B is used by | ||
151 | struct posix_acl. | ||