1 Using RCU to Protect Read-Mostly Arrays
4 Although RCU is more commonly used to protect linked lists, it can
5 also be used to protect arrays. Three situations are as follows:
13 Each of these three situations involves an RCU-protected pointer to an
14 array that is separately indexed. It might be tempting to consider use
15 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
17 arrays is that compilers can play way too many optimization games with
18 integers, which means that the rules governing handling of these indexes
19 are far more trouble than they are worth. If RCU-protected indexes into
20 arrays prove to be particularly valuable (which they have not thus far),
21 explicit cooperation from the compiler will be required to permit them
24 That aside, each of the three RCU-protected pointer situations are
25 described in the following sections.
28 Situation 1: Hash Tables
30 Hash tables are often implemented as an array, where each array entry
31 has a linked-list hash chain. Each hash chain can be protected by RCU
32 as described in the listRCU.txt document. This approach also applies
33 to other array-of-list situations, such as radix trees.
36 Situation 2: Static Arrays
38 Static arrays, where the data (rather than a pointer to the data) is
39 located in each array element, and where the array is never resized,
40 have not been used with RCU. Rik van Riel recommends using seqlock in
41 this situation, which would also have minimal read-side overhead as long
44 Quick Quiz: Why is it so important that updates be rare when
48 Situation 3: Resizeable Arrays
50 Use of RCU for resizeable arrays is demonstrated by the grow_ary()
51 function formerly used by the System V IPC code. The array is used
52 to map from semaphore, message-queue, and shared-memory IDs to the data
53 structure that represents the corresponding IPC construct. The grow_ary()
54 function does not acquire any locks; instead its caller must hold the
57 The grow_ary() function, shown below, does some limit checks, allocates a
58 new ipc_id_ary, copies the old to the new portion of the new, initializes
59 the remainder of the new, updates the ids->entries pointer to point to
60 the new array, and invokes ipc_rcu_putref() to free up the old array.
61 Note that rcu_assign_pointer() is used to update the ids->entries pointer,
62 which includes any memory barriers required on whatever architecture
65 static int grow_ary(struct ipc_ids* ids, int newsize)
67 struct ipc_id_ary* new;
68 struct ipc_id_ary* old;
70 int size = ids->entries->size;
77 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
78 sizeof(struct ipc_id_ary));
82 memcpy(new->p, ids->entries->p,
83 sizeof(struct kern_ipc_perm *)*size +
84 sizeof(struct ipc_id_ary));
85 for(i=size;i<newsize;i++) {
91 * Use rcu_assign_pointer() to make sure the memcpyed
92 * contents of the new array are visible before the new
93 * array becomes visible.
95 rcu_assign_pointer(ids->entries, new);
101 The ipc_rcu_putref() function decrements the array's reference count
102 and then, if the reference count has dropped to zero, uses call_rcu()
103 to free the array after a grace period has elapsed.
105 The array is traversed by the ipc_lock() function. This function
106 indexes into the array under the protection of rcu_read_lock(),
107 using rcu_dereference() to pick up the pointer to the array so
108 that it may later safely be dereferenced -- memory barriers are
109 required on the Alpha CPU. Since the size of the array is stored
110 with the array itself, there can be no array-size mismatches, so
111 a simple check suffices. The pointer to the structure corresponding
112 to the desired IPC object is placed in "out", with NULL indicating
113 a non-existent entry. After acquiring "out->lock", the "out->deleted"
114 flag indicates whether the IPC object is in the process of being
115 deleted, and, if not, the pointer is returned.
117 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
119 struct kern_ipc_perm* out;
120 int lid = id % SEQ_MULTIPLIER;
121 struct ipc_id_ary* entries;
124 entries = rcu_dereference(ids->entries);
125 if(lid >= entries->size) {
129 out = entries->p[lid];
134 spin_lock(&out->lock);
136 /* ipc_rmid() may have already freed the ID while ipc_lock
137 * was spinning: here verify that the structure is still valid
140 spin_unlock(&out->lock);
148 Answer to Quick Quiz:
150 The reason that it is important that updates be rare when
151 using seqlock is that frequent updates can livelock readers.
152 One way to avoid this problem is to assign a seqlock for
153 each array entry rather than to the entire array.