blob: 4051ea3871eff0075843d9eee1725b178e4090fd [file] [log] [blame]
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +05301.. _array_rcu_doc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +05303Using RCU to Protect Read-Mostly Arrays
4=======================================
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6Although RCU is more commonly used to protect linked lists, it can
7also be used to protect arrays. Three situations are as follows:
8
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053091. :ref:`Hash Tables <hash_tables>`
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +0530112. :ref:`Static Arrays <static_arrays>`
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +0530133. :ref:`Resizable Arrays <resizable_arrays>`
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Paul E. McKenneycf9fbf82015-04-20 06:09:27 -070015Each of these three situations involves an RCU-protected pointer to an
16array that is separately indexed. It might be tempting to consider use
17of RCU to instead protect the index into an array, however, this use
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053018case is **not** supported. The problem with RCU-protected indexes into
Paul E. McKenneycf9fbf82015-04-20 06:09:27 -070019arrays is that compilers can play way too many optimization games with
20integers, which means that the rules governing handling of these indexes
21are far more trouble than they are worth. If RCU-protected indexes into
22arrays prove to be particularly valuable (which they have not thus far),
23explicit cooperation from the compiler will be required to permit them
24to be safely used.
25
26That aside, each of the three RCU-protected pointer situations are
27described in the following sections.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053029.. _hash_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31Situation 1: Hash Tables
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053032------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34Hash tables are often implemented as an array, where each array entry
35has a linked-list hash chain. Each hash chain can be protected by RCU
36as described in the listRCU.txt document. This approach also applies
37to other array-of-list situations, such as radix trees.
38
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053039.. _static_arrays:
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41Situation 2: Static Arrays
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053042--------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44Static arrays, where the data (rather than a pointer to the data) is
45located in each array element, and where the array is never resized,
46have not been used with RCU. Rik van Riel recommends using seqlock in
47this situation, which would also have minimal read-side overhead as long
48as updates are rare.
49
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053050Quick Quiz:
51 Why is it so important that updates be rare when using seqlock?
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053053:ref:`Answer to Quick Quiz <answer_quick_quiz_seqlock>`
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053055.. _resizable_arrays:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053057Situation 3: Resizable Arrays
58------------------------------
59
60Use of RCU for resizable arrays is demonstrated by the grow_ary()
Paul E. McKenneycf9fbf82015-04-20 06:09:27 -070061function formerly used by the System V IPC code. The array is used
62to map from semaphore, message-queue, and shared-memory IDs to the data
63structure that represents the corresponding IPC construct. The grow_ary()
Linus Torvalds1da177e2005-04-16 15:20:36 -070064function does not acquire any locks; instead its caller must hold the
65ids->sem semaphore.
66
67The grow_ary() function, shown below, does some limit checks, allocates a
68new ipc_id_ary, copies the old to the new portion of the new, initializes
69the remainder of the new, updates the ids->entries pointer to point to
70the new array, and invokes ipc_rcu_putref() to free up the old array.
71Note that rcu_assign_pointer() is used to update the ids->entries pointer,
72which includes any memory barriers required on whatever architecture
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +053073you are running on::
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 static int grow_ary(struct ipc_ids* ids, int newsize)
76 {
77 struct ipc_id_ary* new;
78 struct ipc_id_ary* old;
79 int i;
80 int size = ids->entries->size;
81
82 if(newsize > IPCMNI)
83 newsize = IPCMNI;
84 if(newsize <= size)
85 return newsize;
86
87 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
88 sizeof(struct ipc_id_ary));
89 if(new == NULL)
90 return size;
91 new->size = newsize;
92 memcpy(new->p, ids->entries->p,
93 sizeof(struct kern_ipc_perm *)*size +
94 sizeof(struct ipc_id_ary));
95 for(i=size;i<newsize;i++) {
96 new->p[i] = NULL;
97 }
98 old = ids->entries;
99
100 /*
101 * Use rcu_assign_pointer() to make sure the memcpyed
102 * contents of the new array are visible before the new
103 * array becomes visible.
104 */
105 rcu_assign_pointer(ids->entries, new);
106
107 ipc_rcu_putref(old);
108 return newsize;
109 }
110
111The ipc_rcu_putref() function decrements the array's reference count
112and then, if the reference count has dropped to zero, uses call_rcu()
113to free the array after a grace period has elapsed.
114
115The array is traversed by the ipc_lock() function. This function
116indexes into the array under the protection of rcu_read_lock(),
117using rcu_dereference() to pick up the pointer to the array so
118that it may later safely be dereferenced -- memory barriers are
119required on the Alpha CPU. Since the size of the array is stored
120with the array itself, there can be no array-size mismatches, so
121a simple check suffices. The pointer to the structure corresponding
122to the desired IPC object is placed in "out", with NULL indicating
123a non-existent entry. After acquiring "out->lock", the "out->deleted"
124flag indicates whether the IPC object is in the process of being
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +0530125deleted, and, if not, the pointer is returned::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
128 {
129 struct kern_ipc_perm* out;
130 int lid = id % SEQ_MULTIPLIER;
131 struct ipc_id_ary* entries;
132
133 rcu_read_lock();
134 entries = rcu_dereference(ids->entries);
135 if(lid >= entries->size) {
136 rcu_read_unlock();
137 return NULL;
138 }
139 out = entries->p[lid];
140 if(out == NULL) {
141 rcu_read_unlock();
142 return NULL;
143 }
144 spin_lock(&out->lock);
145
146 /* ipc_rmid() may have already freed the ID while ipc_lock
147 * was spinning: here verify that the structure is still valid
148 */
149 if (out->deleted) {
150 spin_unlock(&out->lock);
151 rcu_read_unlock();
152 return NULL;
153 }
154 return out;
155 }
156
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +0530157.. _answer_quick_quiz_seqlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159Answer to Quick Quiz:
Madhuparna Bhowmik9ffdd792019-10-29 01:54:17 +0530160 Why is it so important that updates be rare when using seqlock?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 The reason that it is important that updates be rare when
163 using seqlock is that frequent updates can livelock readers.
164 One way to avoid this problem is to assign a seqlock for
165 each array entry rather than to the entire array.