3 Using RCU to Protect Read-Mostly Linked Lists
4 =============================================
6 One of the most common uses of RCU is protecting read-mostly linked lists
7 (``struct list_head`` in list.h). One big advantage of this approach is
8 that all of the required memory ordering is provided by the list macros.
9 This document describes several list-based RCU use cases.
11 When iterating a list while holding the rcu_read_lock(), writers may
12 modify the list. The reader is guaranteed to see all of the elements
13 which were added to the list before they acquired the rcu_read_lock()
14 and are still on the list when they drop the rcu_read_unlock().
15 Elements which are added to, or removed from the list may or may not
16 be seen. If the writer calls list_replace_rcu(), the reader may see
17 either the old element or the new element; they will not see both,
18 nor will they see neither.
21 Example 1: Read-mostly list: Deferred Destruction
22 -------------------------------------------------
24 A widely used usecase for RCU lists in the kernel is lockless iteration over
25 all processes in the system. ``task_struct::tasks`` represents the list node that
26 links all the processes. The list can be traversed in parallel to any list
27 additions or removals.
29 The traversal of the list is done using ``for_each_process()`` which is defined
32 #define next_task(p) \
33 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
35 #define for_each_process(p) \
36 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
38 The code traversing the list of all processes typically looks like::
42 /* Do something with p */
46 The simplified and heavily inlined code for removing a process from a
49 void release_task(struct task_struct *p)
51 write_lock(&tasklist_lock);
52 list_del_rcu(&p->tasks);
53 write_unlock(&tasklist_lock);
54 call_rcu(&p->rcu, delayed_put_task_struct);
57 When a process exits, ``release_task()`` calls ``list_del_rcu(&p->tasks)``
58 via __exit_signal() and __unhash_process() under ``tasklist_lock``
59 writer lock protection. The list_del_rcu() invocation removes
60 the task from the list of all tasks. The ``tasklist_lock``
61 prevents concurrent list additions/removals from corrupting the
62 list. Readers using ``for_each_process()`` are not protected with the
63 ``tasklist_lock``. To prevent readers from noticing changes in the list
64 pointers, the ``task_struct`` object is freed only after one or more
65 grace periods elapse, with the help of call_rcu(), which is invoked via
66 put_task_struct_rcu_user(). This deferring of destruction ensures that
67 any readers traversing the list will see valid ``p->tasks.next`` pointers
68 and deletion/freeing can happen in parallel with traversal of the list.
69 This pattern is also called an **existence lock**, since RCU refrains
70 from invoking the delayed_put_task_struct() callback function until
71 all existing readers finish, which guarantees that the ``task_struct``
72 object in question will remain in existence until after the completion
73 of all RCU readers that might possibly have a reference to that object.
76 Example 2: Read-Side Action Taken Outside of Lock: No In-Place Updates
77 ----------------------------------------------------------------------
79 Some reader-writer locking use cases compute a value while holding
80 the read-side lock, but continue to use that value after that lock is
81 released. These use cases are often good candidates for conversion
82 to RCU. One prominent example involves network packet routing.
83 Because the packet-routing data tracks the state of equipment outside
84 of the computer, it will at times contain stale data. Therefore, once
85 the route has been computed, there is no need to hold the routing table
86 static during transmission of the packet. After all, you can hold the
87 routing table static all you want, but that won't keep the external
88 Internet from changing, and it is the state of the external Internet
89 that really matters. In addition, routing entries are typically added
90 or deleted, rather than being modified in place. This is a rare example
91 of the finite speed of light and the non-zero size of atoms actually
92 helping make synchronization be lighter weight.
94 A straightforward example of this type of RCU use case may be found in
95 the system-call auditing support. For example, a reader-writer locked
96 implementation of ``audit_filter_task()`` might be as follows::
98 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
100 struct audit_entry *e;
101 enum audit_state state;
103 read_lock(&auditsc_lock);
104 /* Note: audit_filter_mutex held by caller. */
105 list_for_each_entry(e, &audit_tsklist, list) {
106 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
107 if (state == AUDIT_STATE_RECORD)
108 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
109 read_unlock(&auditsc_lock);
113 read_unlock(&auditsc_lock);
114 return AUDIT_BUILD_CONTEXT;
117 Here the list is searched under the lock, but the lock is dropped before
118 the corresponding value is returned. By the time that this value is acted
119 on, the list may well have been modified. This makes sense, since if
120 you are turning auditing off, it is OK to audit a few extra system calls.
122 This means that RCU can be easily applied to the read side, as follows::
124 static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
126 struct audit_entry *e;
127 enum audit_state state;
130 /* Note: audit_filter_mutex held by caller. */
131 list_for_each_entry_rcu(e, &audit_tsklist, list) {
132 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
133 if (state == AUDIT_STATE_RECORD)
134 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
140 return AUDIT_BUILD_CONTEXT;
143 The read_lock() and read_unlock() calls have become rcu_read_lock()
144 and rcu_read_unlock(), respectively, and the list_for_each_entry()
145 has become list_for_each_entry_rcu(). The **_rcu()** list-traversal
146 primitives add READ_ONCE() and diagnostic checks for incorrect use
147 outside of an RCU read-side critical section.
149 The changes to the update side are also straightforward. A reader-writer lock
150 might be used as follows for deletion and insertion in these simplified
151 versions of audit_del_rule() and audit_add_rule()::
153 static inline int audit_del_rule(struct audit_rule *rule,
154 struct list_head *list)
156 struct audit_entry *e;
158 write_lock(&auditsc_lock);
159 list_for_each_entry(e, list, list) {
160 if (!audit_compare_rule(rule, &e->rule)) {
162 write_unlock(&auditsc_lock);
166 write_unlock(&auditsc_lock);
167 return -EFAULT; /* No matching rule */
170 static inline int audit_add_rule(struct audit_entry *entry,
171 struct list_head *list)
173 write_lock(&auditsc_lock);
174 if (entry->rule.flags & AUDIT_PREPEND) {
175 entry->rule.flags &= ~AUDIT_PREPEND;
176 list_add(&entry->list, list);
178 list_add_tail(&entry->list, list);
180 write_unlock(&auditsc_lock);
184 Following are the RCU equivalents for these two functions::
186 static inline int audit_del_rule(struct audit_rule *rule,
187 struct list_head *list)
189 struct audit_entry *e;
191 /* No need to use the _rcu iterator here, since this is the only
192 * deletion routine. */
193 list_for_each_entry(e, list, list) {
194 if (!audit_compare_rule(rule, &e->rule)) {
195 list_del_rcu(&e->list);
196 call_rcu(&e->rcu, audit_free_rule);
200 return -EFAULT; /* No matching rule */
203 static inline int audit_add_rule(struct audit_entry *entry,
204 struct list_head *list)
206 if (entry->rule.flags & AUDIT_PREPEND) {
207 entry->rule.flags &= ~AUDIT_PREPEND;
208 list_add_rcu(&entry->list, list);
210 list_add_tail_rcu(&entry->list, list);
215 Normally, the write_lock() and write_unlock() would be replaced by a
216 spin_lock() and a spin_unlock(). But in this case, all callers hold
217 ``audit_filter_mutex``, so no additional locking is required. The
218 auditsc_lock can therefore be eliminated, since use of RCU eliminates the
219 need for writers to exclude readers.
221 The list_del(), list_add(), and list_add_tail() primitives have been
222 replaced by list_del_rcu(), list_add_rcu(), and list_add_tail_rcu().
223 The **_rcu()** list-manipulation primitives add memory barriers that are
224 needed on weakly ordered CPUs. The list_del_rcu() primitive omits the
225 pointer poisoning debug-assist code that would otherwise cause concurrent
226 readers to fail spectacularly.
228 So, when readers can tolerate stale data and when entries are either added or
229 deleted, without in-place modification, it is very easy to use RCU!
232 Example 3: Handling In-Place Updates
233 ------------------------------------
235 The system-call auditing code does not update auditing rules in place. However,
236 if it did, the reader-writer-locked code to do so might look as follows
237 (assuming only ``field_count`` is updated, otherwise, the added fields would
238 need to be filled in)::
240 static inline int audit_upd_rule(struct audit_rule *rule,
241 struct list_head *list,
243 __u32 newfield_count)
245 struct audit_entry *e;
246 struct audit_entry *ne;
248 write_lock(&auditsc_lock);
249 /* Note: audit_filter_mutex held by caller. */
250 list_for_each_entry(e, list, list) {
251 if (!audit_compare_rule(rule, &e->rule)) {
252 e->rule.action = newaction;
253 e->rule.field_count = newfield_count;
254 write_unlock(&auditsc_lock);
258 write_unlock(&auditsc_lock);
259 return -EFAULT; /* No matching rule */
262 The RCU version creates a copy, updates the copy, then replaces the old
263 entry with the newly updated entry. This sequence of actions, allowing
264 concurrent reads while making a copy to perform an update, is what gives
265 RCU (*read-copy update*) its name.
267 The RCU version of audit_upd_rule() is as follows::
269 static inline int audit_upd_rule(struct audit_rule *rule,
270 struct list_head *list,
272 __u32 newfield_count)
274 struct audit_entry *e;
275 struct audit_entry *ne;
277 list_for_each_entry(e, list, list) {
278 if (!audit_compare_rule(rule, &e->rule)) {
279 ne = kmalloc(sizeof(*entry), GFP_ATOMIC);
282 audit_copy_rule(&ne->rule, &e->rule);
283 ne->rule.action = newaction;
284 ne->rule.field_count = newfield_count;
285 list_replace_rcu(&e->list, &ne->list);
286 call_rcu(&e->rcu, audit_free_rule);
290 return -EFAULT; /* No matching rule */
293 Again, this assumes that the caller holds ``audit_filter_mutex``. Normally, the
294 writer lock would become a spinlock in this sort of code.
296 The update_lsm_rule() does something very similar, for those who would
297 prefer to look at real Linux-kernel code.
299 Another use of this pattern can be found in the openswitch driver's *connection
300 tracking table* code in ``ct_limit_set()``. The table holds connection tracking
301 entries and has a limit on the maximum entries. There is one such table
302 per-zone and hence one *limit* per zone. The zones are mapped to their limits
303 through a hashtable using an RCU-managed hlist for the hash chains. When a new
304 limit is set, a new limit object is allocated and ``ct_limit_set()`` is called
305 to replace the old limit object with the new one using list_replace_rcu().
306 The old limit object is then freed after a grace period using kfree_rcu().
309 Example 4: Eliminating Stale Data
310 ---------------------------------
312 The auditing example above tolerates stale data, as do most algorithms
313 that are tracking external state. After all, given there is a delay
314 from the time the external state changes before Linux becomes aware
315 of the change, and so as noted earlier, a small quantity of additional
316 RCU-induced staleness is generally not a problem.
318 However, there are many examples where stale data cannot be tolerated.
319 One example in the Linux kernel is the System V IPC (see the shm_lock()
320 function in ipc/shm.c). This code checks a *deleted* flag under a
321 per-entry spinlock, and, if the *deleted* flag is set, pretends that the
322 entry does not exist. For this to be helpful, the search function must
323 return holding the per-entry spinlock, as shm_lock() does in fact do.
328 For the deleted-flag technique to be helpful, why is it necessary
329 to hold the per-entry lock while returning from the search function?
331 :ref:`Answer to Quick Quiz <quick_quiz_answer>`
333 If the system-call audit module were to ever need to reject stale data, one way
334 to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the
335 ``audit_entry`` structure, and modify audit_filter_task() as follows::
337 static enum audit_state audit_filter_task(struct task_struct *tsk)
339 struct audit_entry *e;
340 enum audit_state state;
343 list_for_each_entry_rcu(e, &audit_tsklist, list) {
344 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
347 spin_unlock(&e->lock);
349 return AUDIT_BUILD_CONTEXT;
352 if (state == AUDIT_STATE_RECORD)
353 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
358 return AUDIT_BUILD_CONTEXT;
361 The ``audit_del_rule()`` function would need to set the ``deleted`` flag under the
362 spinlock as follows::
364 static inline int audit_del_rule(struct audit_rule *rule,
365 struct list_head *list)
367 struct audit_entry *e;
369 /* No need to use the _rcu iterator here, since this
370 * is the only deletion routine. */
371 list_for_each_entry(e, list, list) {
372 if (!audit_compare_rule(rule, &e->rule)) {
374 list_del_rcu(&e->list);
376 spin_unlock(&e->lock);
377 call_rcu(&e->rcu, audit_free_rule);
381 return -EFAULT; /* No matching rule */
384 This too assumes that the caller holds ``audit_filter_mutex``.
386 Note that this example assumes that entries are only added and deleted.
387 Additional mechanism is required to deal correctly with the update-in-place
388 performed by audit_upd_rule(). For one thing, audit_upd_rule() would
389 need to hold the locks of both the old ``audit_entry`` and its replacement
390 while executing the list_replace_rcu().
393 Example 5: Skipping Stale Objects
394 ---------------------------------
396 For some use cases, reader performance can be improved by skipping
397 stale objects during read-side list traversal, where stale objects
398 are those that will be removed and destroyed after one or more grace
399 periods. One such example can be found in the timerfd subsystem. When a
400 ``CLOCK_REALTIME`` clock is reprogrammed (for example due to setting
401 of the system time) then all programmed ``timerfds`` that depend on
402 this clock get triggered and processes waiting on them are awakened in
403 advance of their scheduled expiry. To facilitate this, all such timers
404 are added to an RCU-managed ``cancel_list`` when they are setup in
405 ``timerfd_setup_cancel()``::
407 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
409 spin_lock(&ctx->cancel_lock);
410 if ((ctx->clockid == CLOCK_REALTIME ||
411 ctx->clockid == CLOCK_REALTIME_ALARM) &&
412 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
413 if (!ctx->might_cancel) {
414 ctx->might_cancel = true;
415 spin_lock(&cancel_lock);
416 list_add_rcu(&ctx->clist, &cancel_list);
417 spin_unlock(&cancel_lock);
420 __timerfd_remove_cancel(ctx);
422 spin_unlock(&ctx->cancel_lock);
425 When a timerfd is freed (fd is closed), then the ``might_cancel``
426 flag of the timerfd object is cleared, the object removed from the
427 ``cancel_list`` and destroyed, as shown in this simplified and inlined
428 version of timerfd_release()::
430 int timerfd_release(struct inode *inode, struct file *file)
432 struct timerfd_ctx *ctx = file->private_data;
434 spin_lock(&ctx->cancel_lock);
435 if (ctx->might_cancel) {
436 ctx->might_cancel = false;
437 spin_lock(&cancel_lock);
438 list_del_rcu(&ctx->clist);
439 spin_unlock(&cancel_lock);
441 spin_unlock(&ctx->cancel_lock);
444 alarm_cancel(&ctx->t.alarm);
446 hrtimer_cancel(&ctx->t.tmr);
451 If the ``CLOCK_REALTIME`` clock is set, for example by a time server, the
452 hrtimer framework calls ``timerfd_clock_was_set()`` which walks the
453 ``cancel_list`` and wakes up processes waiting on the timerfd. While iterating
454 the ``cancel_list``, the ``might_cancel`` flag is consulted to skip stale
457 void timerfd_clock_was_set(void)
459 ktime_t moffs = ktime_mono_to_real(0);
460 struct timerfd_ctx *ctx;
464 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
465 if (!ctx->might_cancel)
467 spin_lock_irqsave(&ctx->wqh.lock, flags);
468 if (ctx->moffs != moffs) {
469 ctx->moffs = KTIME_MAX;
471 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
473 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
478 The key point is that because RCU-protected traversal of the
479 ``cancel_list`` happens concurrently with object addition and removal,
480 sometimes the traversal can access an object that has been removed from
481 the list. In this example, a flag is used to skip such objects.
487 Read-mostly list-based data structures that can tolerate stale data are
488 the most amenable to use of RCU. The simplest case is where entries are
489 either added or deleted from the data structure (or atomically modified
490 in place), but non-atomic in-place modifications can be handled by making
491 a copy, updating the copy, then replacing the original with the copy.
492 If stale data cannot be tolerated, then a *deleted* flag may be used
493 in conjunction with a per-entry spinlock in order to allow the search
494 function to reject newly deleted data.
496 .. _quick_quiz_answer:
498 Answer to Quick Quiz:
499 For the deleted-flag technique to be helpful, why is it necessary
500 to hold the per-entry lock while returning from the search function?
502 If the search function drops the per-entry lock before returning,
503 then the caller will be processing stale data in any case. If it
504 is really OK to be processing stale data, then you don't need a
505 *deleted* flag. If processing stale data really is a problem,
506 then you need to hold the per-entry lock across all of the code
507 that uses the value that was returned.
509 :ref:`Back to Quick Quiz <quick_quiz>`