1
- Using RCU to Protect Read-Mostly Arrays
1
+ .. _ array_rcu_doc :
2
2
3
+ Using RCU to Protect Read-Mostly Arrays
4
+ =======================================
3
5
4
6
Although RCU is more commonly used to protect linked lists, it can
5
7
also be used to protect arrays. Three situations are as follows:
6
8
7
- 1. Hash Tables
9
+ 1. :ref: ` Hash Tables < hash_tables >`
8
10
9
- 2. Static Arrays
11
+ 2. :ref: ` Static Arrays < static_arrays >`
10
12
11
- 3. Resizeable Arrays
13
+ 3. :ref: ` Resizable Arrays < resizable_arrays >`
12
14
13
15
Each of these three situations involves an RCU-protected pointer to an
14
16
array that is separately indexed. It might be tempting to consider use
15
17
of RCU to instead protect the index into an array, however, this use
16
- case is - not- supported. The problem with RCU-protected indexes into
18
+ case is ** not ** supported. The problem with RCU-protected indexes into
17
19
arrays is that compilers can play way too many optimization games with
18
20
integers, which means that the rules governing handling of these indexes
19
21
are far more trouble than they are worth. If RCU-protected indexes into
@@ -24,30 +26,38 @@ to be safely used.
24
26
That aside, each of the three RCU-protected pointer situations are
25
27
described in the following sections.
26
28
29
+ .. _hash_tables :
27
30
28
31
Situation 1: Hash Tables
32
+ ------------------------
29
33
30
34
Hash tables are often implemented as an array, where each array entry
31
35
has a linked-list hash chain. Each hash chain can be protected by RCU
32
36
as described in the listRCU.txt document. This approach also applies
33
37
to other array-of-list situations, such as radix trees.
34
38
39
+ .. _static_arrays :
35
40
36
41
Situation 2: Static Arrays
42
+ --------------------------
37
43
38
44
Static arrays, where the data (rather than a pointer to the data) is
39
45
located in each array element, and where the array is never resized,
40
46
have not been used with RCU. Rik van Riel recommends using seqlock in
41
47
this situation, which would also have minimal read-side overhead as long
42
48
as updates are rare.
43
49
44
- Quick Quiz: Why is it so important that updates be rare when
45
- using seqlock?
50
+ Quick Quiz:
51
+ Why is it so important that updates be rare when using seqlock?
52
+
53
+ :ref: `Answer to Quick Quiz <answer_quick_quiz_seqlock >`
46
54
55
+ .. _resizable_arrays :
47
56
48
- Situation 3: Resizeable Arrays
57
+ Situation 3: Resizable Arrays
58
+ ------------------------------
49
59
50
- Use of RCU for resizeable arrays is demonstrated by the grow_ary()
60
+ Use of RCU for resizable arrays is demonstrated by the grow_ary()
51
61
function formerly used by the System V IPC code. The array is used
52
62
to map from semaphore, message-queue, and shared-memory IDs to the data
53
63
structure that represents the corresponding IPC construct. The grow_ary()
@@ -60,7 +70,7 @@ the remainder of the new, updates the ids->entries pointer to point to
60
70
the new array, and invokes ipc_rcu_putref() to free up the old array.
61
71
Note that rcu_assign_pointer() is used to update the ids->entries pointer,
62
72
which includes any memory barriers required on whatever architecture
63
- you are running on.
73
+ you are running on::
64
74
65
75
static int grow_ary(struct ipc_ids* ids, int newsize)
66
76
{
@@ -112,7 +122,7 @@ a simple check suffices. The pointer to the structure corresponding
112
122
to the desired IPC object is placed in "out", with NULL indicating
113
123
a non-existent entry. After acquiring "out->lock", the "out->deleted"
114
124
flag indicates whether the IPC object is in the process of being
115
- deleted, and, if not, the pointer is returned.
125
+ deleted, and, if not, the pointer is returned::
116
126
117
127
struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
118
128
{
@@ -144,8 +154,10 @@ deleted, and, if not, the pointer is returned.
144
154
return out;
145
155
}
146
156
157
+ .. _answer_quick_quiz_seqlock :
147
158
148
159
Answer to Quick Quiz:
160
+ Why is it so important that updates be rare when using seqlock?
149
161
150
162
The reason that it is important that updates be rare when
151
163
using seqlock is that frequent updates can livelock readers.
0 commit comments