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 situations are discussed below.
16 Situation 1: Hash Tables
18 Hash tables are often implemented as an array, where each array entry
19 has a linked-list hash chain. Each hash chain can be protected by RCU
20 as described in the listRCU.txt document. This approach also applies
21 to other array-of-list situations, such as radix trees.
24 Situation 2: Static Arrays
26 Static arrays, where the data (rather than a pointer to the data) is
27 located in each array element, and where the array is never resized,
28 have not been used with RCU. Rik van Riel recommends using seqlock in
29 this situation, which would also have minimal read-side overhead as long
32 Quick Quiz: Why is it so important that updates be rare when
36 Situation 3: Resizeable Arrays
38 Use of RCU for resizeable arrays is demonstrated by the grow_ary()
39 function used by the System V IPC code. The array is used to map from
40 semaphore, message-queue, and shared-memory IDs to the data structure
41 that represents the corresponding IPC construct. The grow_ary()
42 function does not acquire any locks; instead its caller must hold the
45 The grow_ary() function, shown below, does some limit checks, allocates a
46 new ipc_id_ary, copies the old to the new portion of the new, initializes
47 the remainder of the new, updates the ids->entries pointer to point to
48 the new array, and invokes ipc_rcu_putref() to free up the old array.
49 Note that rcu_assign_pointer() is used to update the ids->entries pointer,
50 which includes any memory barriers required on whatever architecture
53 static int grow_ary(struct ipc_ids* ids, int newsize)
55 struct ipc_id_ary* new;
56 struct ipc_id_ary* old;
58 int size = ids->entries->size;
65 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
66 sizeof(struct ipc_id_ary));
70 memcpy(new->p, ids->entries->p,
71 sizeof(struct kern_ipc_perm *)*size +
72 sizeof(struct ipc_id_ary));
73 for(i=size;i<newsize;i++) {
79 * Use rcu_assign_pointer() to make sure the memcpyed
80 * contents of the new array are visible before the new
81 * array becomes visible.
83 rcu_assign_pointer(ids->entries, new);
89 The ipc_rcu_putref() function decrements the array's reference count
90 and then, if the reference count has dropped to zero, uses call_rcu()
91 to free the array after a grace period has elapsed.
93 The array is traversed by the ipc_lock() function. This function
94 indexes into the array under the protection of rcu_read_lock(),
95 using rcu_dereference() to pick up the pointer to the array so
96 that it may later safely be dereferenced -- memory barriers are
97 required on the Alpha CPU. Since the size of the array is stored
98 with the array itself, there can be no array-size mismatches, so
99 a simple check suffices. The pointer to the structure corresponding
100 to the desired IPC object is placed in "out", with NULL indicating
101 a non-existent entry. After acquiring "out->lock", the "out->deleted"
102 flag indicates whether the IPC object is in the process of being
103 deleted, and, if not, the pointer is returned.
105 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
107 struct kern_ipc_perm* out;
108 int lid = id % SEQ_MULTIPLIER;
109 struct ipc_id_ary* entries;
112 entries = rcu_dereference(ids->entries);
113 if(lid >= entries->size) {
117 out = entries->p[lid];
122 spin_lock(&out->lock);
124 /* ipc_rmid() may have already freed the ID while ipc_lock
125 * was spinning: here verify that the structure is still valid
128 spin_unlock(&out->lock);
136 Answer to Quick Quiz:
138 The reason that it is important that updates be rare when
139 using seqlock is that frequent updates can livelock readers.
140 One way to avoid this problem is to assign a seqlock for
141 each array entry rather than to the entire array.