sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}(hhparenthuba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget/translations/zh_CN/RCU/listRCUmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}(hhhh2ubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/zh_TW/RCU/listRCUmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}(hhhhFubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/it_IT/RCU/listRCUmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}(hhhhZubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/ja_JP/RCU/listRCUmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}(hhhhnubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/ko_KR/RCU/listRCUmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}(hhhhubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/sp_SP/RCU/listRCUmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhtarget)}(h.. _list_rcu_doc:h]h}(h]h ]h"]h$]h&]refid list-rcu-docuh1hhKhhhhh9/var/lib/git/docbuild/linux/Documentation/RCU/listRCU.rstubhsection)}(hhh](htitle)}(h-Using RCU to Protect Read-Mostly Linked Listsh]h-Using RCU to Protect Read-Mostly Linked Lists}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh paragraph)}(hXOne of the most common uses of RCU is protecting read-mostly linked lists (``struct list_head`` in list.h). One big advantage of this approach is that all of the required memory ordering is provided by the list macros. This document describes several list-based RCU use cases.h](hKOne of the most common uses of RCU is protecting read-mostly linked lists (}(hKOne of the most common uses of RCU is protecting read-mostly linked lists (hhhhhNhNubhliteral)}(h``struct list_head``h]hstruct list_head}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh in list.h). One big advantage of this approach is that all of the required memory ordering is provided by the list macros. This document describes several list-based RCU use cases.}(h in list.h). One big advantage of this approach is that all of the required memory ordering is provided by the list macros. This document describes several list-based RCU use cases.hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hXWhen iterating a list while holding the rcu_read_lock(), writers may modify the list. The reader is guaranteed to see all of the elements which were added to the list before they acquired the rcu_read_lock() and are still on the list when they drop the rcu_read_unlock(). Elements which are added to, or removed from the list may or may not be seen. If the writer calls list_replace_rcu(), the reader may see either the old element or the new element; they will not see both, nor will they see neither.h]hXWhen iterating a list while holding the rcu_read_lock(), writers may modify the list. The reader is guaranteed to see all of the elements which were added to the list before they acquired the rcu_read_lock() and are still on the list when they drop the rcu_read_unlock(). Elements which are added to, or removed from the list may or may not be seen. If the writer calls list_replace_rcu(), the reader may see either the old element or the new element; they will not see both, nor will they see neither.}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hhh](h)}(h1Example 1: Read-mostly list: Deferred Destructionh]h1Example 1: Read-mostly list: Deferred Destruction}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hXA widely used usecase for RCU lists in the kernel is lockless iteration over all processes in the system. ``task_struct::tasks`` represents the list node that links all the processes. The list can be traversed in parallel to any list additions or removals.h](hjA widely used usecase for RCU lists in the kernel is lockless iteration over all processes in the system. }(hjA widely used usecase for RCU lists in the kernel is lockless iteration over all processes in the system. hj hhhNhNubh)}(h``task_struct::tasks``h]htask_struct::tasks}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh represents the list node that links all the processes. The list can be traversed in parallel to any list additions or removals.}(h represents the list node that links all the processes. The list can be traversed in parallel to any list additions or removals.hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(haThe traversal of the list is done using ``for_each_process()`` which is defined by the 2 macros::h](h(The traversal of the list is done using }(h(The traversal of the list is done using hj,hhhNhNubh)}(h``for_each_process()``h]hfor_each_process()}(hhhj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj,ubh" which is defined by the 2 macros:}(h" which is defined by the 2 macros:hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh literal_block)}(h#define next_task(p) \ list_entry_rcu((p)->tasks.next, struct task_struct, tasks) #define for_each_process(p) \ for (p = &init_task ; (p = next_task(p)) != &init_task ; )h]h#define next_task(p) \ list_entry_rcu((p)->tasks.next, struct task_struct, tasks) #define for_each_process(p) \ for (p = &init_task ; (p = next_task(p)) != &init_task ; )}(hhhjPubah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jNhhhK hhhhubh)}(hDThe code traversing the list of all processes typically looks like::h]hCThe code traversing the list of all processes typically looks like:}(hCThe code traversing the list of all processes typically looks like:hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK&hhhhubjO)}(h]rcu_read_lock(); for_each_process(p) { /* Do something with p */ } rcu_read_unlock();h]h]rcu_read_lock(); for_each_process(p) { /* Do something with p */ } rcu_read_unlock();}(hhhjoubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhK(hhhhubh)}(hTThe simplified and heavily inlined code for removing a process from a task list is::h]hSThe simplified and heavily inlined code for removing a process from a task list is:}(hSThe simplified and heavily inlined code for removing a process from a task list is:hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK.hhhhubjO)}(hvoid release_task(struct task_struct *p) { write_lock(&tasklist_lock); list_del_rcu(&p->tasks); write_unlock(&tasklist_lock); call_rcu(&p->rcu, delayed_put_task_struct); }h]hvoid release_task(struct task_struct *p) { write_lock(&tasklist_lock); list_del_rcu(&p->tasks); write_unlock(&tasklist_lock); call_rcu(&p->rcu, delayed_put_task_struct); }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhK1hhhhubh)}(hXWhen a process exits, ``release_task()`` calls ``list_del_rcu(&p->tasks)`` via __exit_signal() and __unhash_process() under ``tasklist_lock`` writer lock protection. The list_del_rcu() invocation removes the task from the list of all tasks. The ``tasklist_lock`` prevents concurrent list additions/removals from corrupting the list. Readers using ``for_each_process()`` are not protected with the ``tasklist_lock``. To prevent readers from noticing changes in the list pointers, the ``task_struct`` object is freed only after one or more grace periods elapse, with the help of call_rcu(), which is invoked via put_task_struct_rcu_user(). This deferring of destruction ensures that any readers traversing the list will see valid ``p->tasks.next`` pointers and deletion/freeing can happen in parallel with traversal of the list. This pattern is also called an **existence lock**, since RCU refrains from invoking the delayed_put_task_struct() callback function until all existing readers finish, which guarantees that the ``task_struct`` object in question will remain in existence until after the completion of all RCU readers that might possibly have a reference to that object.h](hWhen a process exits, }(hWhen a process exits, hjhhhNhNubh)}(h``release_task()``h]hrelease_task()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh calls }(h calls hjhhhNhNubh)}(h``list_del_rcu(&p->tasks)``h]hlist_del_rcu(&p->tasks)}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh2 via __exit_signal() and __unhash_process() under }(h2 via __exit_signal() and __unhash_process() under hjhhhNhNubh)}(h``tasklist_lock``h]h tasklist_lock}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhi writer lock protection. The list_del_rcu() invocation removes the task from the list of all tasks. The }(hi writer lock protection. The list_del_rcu() invocation removes the task from the list of all tasks. The hjhhhNhNubh)}(h``tasklist_lock``h]h tasklist_lock}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhU prevents concurrent list additions/removals from corrupting the list. Readers using }(hU prevents concurrent list additions/removals from corrupting the list. Readers using hjhhhNhNubh)}(h``for_each_process()``h]hfor_each_process()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh are not protected with the }(h are not protected with the hjhhhNhNubh)}(h``tasklist_lock``h]h tasklist_lock}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhE. To prevent readers from noticing changes in the list pointers, the }(hE. To prevent readers from noticing changes in the list pointers, the hjhhhNhNubh)}(h``task_struct``h]h task_struct}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh object is freed only after one or more grace periods elapse, with the help of call_rcu(), which is invoked via put_task_struct_rcu_user(). This deferring of destruction ensures that any readers traversing the list will see valid }(h object is freed only after one or more grace periods elapse, with the help of call_rcu(), which is invoked via put_task_struct_rcu_user(). This deferring of destruction ensures that any readers traversing the list will see valid hjhhhNhNubh)}(h``p->tasks.next``h]h p->tasks.next}(hhhj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhq pointers and deletion/freeing can happen in parallel with traversal of the list. This pattern is also called an }(hq pointers and deletion/freeing can happen in parallel with traversal of the list. This pattern is also called an hjhhhNhNubhstrong)}(h**existence lock**h]hexistence lock}(hhhj=hhhNhNubah}(h]h ]h"]h$]h&]uh1j;hjubh, since RCU refrains from invoking the delayed_put_task_struct() callback function until all existing readers finish, which guarantees that the }(h, since RCU refrains from invoking the delayed_put_task_struct() callback function until all existing readers finish, which guarantees that the hjhhhNhNubh)}(h``task_struct``h]h task_struct}(hhhjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh object in question will remain in existence until after the completion of all RCU readers that might possibly have a reference to that object.}(h object in question will remain in existence until after the completion of all RCU readers that might possibly have a reference to that object.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK9hhhhubeh}(h]/example-1-read-mostly-list-deferred-destructionah ]h"]1example 1: read-mostly list: deferred destructionah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hFExample 2: Read-Side Action Taken Outside of Lock: No In-Place Updatesh]hFExample 2: Read-Side Action Taken Outside of Lock: No In-Place Updates}(hjvhjthhhNhNubah}(h]h ]h"]h$]h&]uh1hhjqhhhhhKMubh)}(hXSome reader-writer locking use cases compute a value while holding the read-side lock, but continue to use that value after that lock is released. These use cases are often good candidates for conversion to RCU. One prominent example involves network packet routing. Because the packet-routing data tracks the state of equipment outside of the computer, it will at times contain stale data. Therefore, once the route has been computed, there is no need to hold the routing table static during transmission of the packet. After all, you can hold the routing table static all you want, but that won't keep the external Internet from changing, and it is the state of the external Internet that really matters. In addition, routing entries are typically added or deleted, rather than being modified in place. This is a rare example of the finite speed of light and the non-zero size of atoms actually helping make synchronization be lighter weight.h]hXSome reader-writer locking use cases compute a value while holding the read-side lock, but continue to use that value after that lock is released. These use cases are often good candidates for conversion to RCU. One prominent example involves network packet routing. Because the packet-routing data tracks the state of equipment outside of the computer, it will at times contain stale data. Therefore, once the route has been computed, there is no need to hold the routing table static during transmission of the packet. After all, you can hold the routing table static all you want, but that won’t keep the external Internet from changing, and it is the state of the external Internet that really matters. In addition, routing entries are typically added or deleted, rather than being modified in place. This is a rare example of the finite speed of light and the non-zero size of atoms actually helping make synchronization be lighter weight.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKOhjqhhubh)}(hA straightforward example of this type of RCU use case may be found in the system-call auditing support. For example, a reader-writer locked implementation of ``audit_filter_task()`` might be as follows::h](hA straightforward example of this type of RCU use case may be found in the system-call auditing support. For example, a reader-writer locked implementation of }(hA straightforward example of this type of RCU use case may be found in the system-call auditing support. For example, a reader-writer locked implementation of hjhhhNhNubh)}(h``audit_filter_task()``h]haudit_filter_task()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh might be as follows:}(h might be as follows:hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK^hjqhhubjO)}(hXstatic enum audit_state audit_filter_task(struct task_struct *tsk, char **key) { struct audit_entry *e; enum audit_state state; read_lock(&auditsc_lock); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); read_unlock(&auditsc_lock); return state; } } read_unlock(&auditsc_lock); return AUDIT_BUILD_CONTEXT; }h]hXstatic enum audit_state audit_filter_task(struct task_struct *tsk, char **key) { struct audit_entry *e; enum audit_state state; read_lock(&auditsc_lock); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); read_unlock(&auditsc_lock); return state; } } read_unlock(&auditsc_lock); return AUDIT_BUILD_CONTEXT; }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhKbhjqhhubh)}(hX#Here the list is searched under the lock, but the lock is dropped before the corresponding value is returned. By the time that this value is acted on, the list may well have been modified. This makes sense, since if you are turning auditing off, it is OK to audit a few extra system calls.h]hX#Here the list is searched under the lock, but the lock is dropped before the corresponding value is returned. By the time that this value is acted on, the list may well have been modified. This makes sense, since if you are turning auditing off, it is OK to audit a few extra system calls.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKuhjqhhubh)}(hHThis means that RCU can be easily applied to the read side, as follows::h]hGThis means that RCU can be easily applied to the read side, as follows:}(hGThis means that RCU can be easily applied to the read side, as follows:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKzhjqhhubjO)}(hXstatic enum audit_state audit_filter_task(struct task_struct *tsk, char **key) { struct audit_entry *e; enum audit_state state; rcu_read_lock(); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry_rcu(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); rcu_read_unlock(); return state; } } rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; }h]hXstatic enum audit_state audit_filter_task(struct task_struct *tsk, char **key) { struct audit_entry *e; enum audit_state state; rcu_read_lock(); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry_rcu(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); rcu_read_unlock(); return state; } } rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhK|hjqhhubh)}(hX<The read_lock() and read_unlock() calls have become rcu_read_lock() and rcu_read_unlock(), respectively, and the list_for_each_entry() has become list_for_each_entry_rcu(). The **_rcu()** list-traversal primitives add READ_ONCE() and diagnostic checks for incorrect use outside of an RCU read-side critical section.h](hThe read_lock() and read_unlock() calls have become rcu_read_lock() and rcu_read_unlock(), respectively, and the list_for_each_entry() has become list_for_each_entry_rcu(). The }(hThe read_lock() and read_unlock() calls have become rcu_read_lock() and rcu_read_unlock(), respectively, and the list_for_each_entry() has become list_for_each_entry_rcu(). The hjhhhNhNubj<)}(h **_rcu()**h]h_rcu()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1j;hjubh list-traversal primitives add READ_ONCE() and diagnostic checks for incorrect use outside of an RCU read-side critical section.}(h list-traversal primitives add READ_ONCE() and diagnostic checks for incorrect use outside of an RCU read-side critical section.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubh)}(hThe changes to the update side are also straightforward. A reader-writer lock might be used as follows for deletion and insertion in these simplified versions of audit_del_rule() and audit_add_rule()::h]hThe changes to the update side are also straightforward. A reader-writer lock might be used as follows for deletion and insertion in these simplified versions of audit_del_rule() and audit_add_rule():}(hThe changes to the update side are also straightforward. A reader-writer lock might be used as follows for deletion and insertion in these simplified versions of audit_del_rule() and audit_add_rule():hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubjO)}(hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; write_lock(&auditsc_lock); list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { list_del(&e->list); write_unlock(&auditsc_lock); return 0; } } write_unlock(&auditsc_lock); return -EFAULT; /* No matching rule */ } static inline int audit_add_rule(struct audit_entry *entry, struct list_head *list) { write_lock(&auditsc_lock); if (entry->rule.flags & AUDIT_PREPEND) { entry->rule.flags &= ~AUDIT_PREPEND; list_add(&entry->list, list); } else { list_add_tail(&entry->list, list); } write_unlock(&auditsc_lock); return 0; }h]hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; write_lock(&auditsc_lock); list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { list_del(&e->list); write_unlock(&auditsc_lock); return 0; } } write_unlock(&auditsc_lock); return -EFAULT; /* No matching rule */ } static inline int audit_add_rule(struct audit_entry *entry, struct list_head *list) { write_lock(&auditsc_lock); if (entry->rule.flags & AUDIT_PREPEND) { entry->rule.flags &= ~AUDIT_PREPEND; list_add(&entry->list, list); } else { list_add_tail(&entry->list, list); } write_unlock(&auditsc_lock); return 0; }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhKhjqhhubh)}(h;Following are the RCU equivalents for these two functions::h]h:Following are the RCU equivalents for these two functions:}(h:Following are the RCU equivalents for these two functions:hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubjO)}(hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; /* No need to use the _rcu iterator here, since this is the only * deletion routine. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { list_del_rcu(&e->list); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ } static inline int audit_add_rule(struct audit_entry *entry, struct list_head *list) { if (entry->rule.flags & AUDIT_PREPEND) { entry->rule.flags &= ~AUDIT_PREPEND; list_add_rcu(&entry->list, list); } else { list_add_tail_rcu(&entry->list, list); } return 0; }h]hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; /* No need to use the _rcu iterator here, since this is the only * deletion routine. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { list_del_rcu(&e->list); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ } static inline int audit_add_rule(struct audit_entry *entry, struct list_head *list) { if (entry->rule.flags & AUDIT_PREPEND) { entry->rule.flags &= ~AUDIT_PREPEND; list_add_rcu(&entry->list, list); } else { list_add_tail_rcu(&entry->list, list); } return 0; }}(hhhj9ubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhKhjqhhubh)}(hX9Normally, the write_lock() and write_unlock() would be replaced by a spin_lock() and a spin_unlock(). But in this case, all callers hold ``audit_filter_mutex``, so no additional locking is required. The auditsc_lock can therefore be eliminated, since use of RCU eliminates the need for writers to exclude readers.h](hNormally, the write_lock() and write_unlock() would be replaced by a spin_lock() and a spin_unlock(). But in this case, all callers hold }(hNormally, the write_lock() and write_unlock() would be replaced by a spin_lock() and a spin_unlock(). But in this case, all callers hold hjGhhhNhNubh)}(h``audit_filter_mutex``h]haudit_filter_mutex}(hhhjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjGubh, so no additional locking is required. The auditsc_lock can therefore be eliminated, since use of RCU eliminates the need for writers to exclude readers.}(h, so no additional locking is required. The auditsc_lock can therefore be eliminated, since use of RCU eliminates the need for writers to exclude readers.hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubh)}(hXThe list_del(), list_add(), and list_add_tail() primitives have been replaced by list_del_rcu(), list_add_rcu(), and list_add_tail_rcu(). The **_rcu()** list-manipulation primitives add memory barriers that are needed on weakly ordered CPUs. The list_del_rcu() primitive omits the pointer poisoning debug-assist code that would otherwise cause concurrent readers to fail spectacularly.h](hThe list_del(), list_add(), and list_add_tail() primitives have been replaced by list_del_rcu(), list_add_rcu(), and list_add_tail_rcu(). The }(hThe list_del(), list_add(), and list_add_tail() primitives have been replaced by list_del_rcu(), list_add_rcu(), and list_add_tail_rcu(). The hjihhhNhNubj<)}(h **_rcu()**h]h_rcu()}(hhhjrhhhNhNubah}(h]h ]h"]h$]h&]uh1j;hjiubh list-manipulation primitives add memory barriers that are needed on weakly ordered CPUs. The list_del_rcu() primitive omits the pointer poisoning debug-assist code that would otherwise cause concurrent readers to fail spectacularly.}(h list-manipulation primitives add memory barriers that are needed on weakly ordered CPUs. The list_del_rcu() primitive omits the pointer poisoning debug-assist code that would otherwise cause concurrent readers to fail spectacularly.hjihhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubh)}(hSo, when readers can tolerate stale data and when entries are either added or deleted, without in-place modification, it is very easy to use RCU!h]hSo, when readers can tolerate stale data and when entries are either added or deleted, without in-place modification, it is very easy to use RCU!}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjqhhubeh}(h]Dexample-2-read-side-action-taken-outside-of-lock-no-in-place-updatesah ]h"]Fexample 2: read-side action taken outside of lock: no in-place updatesah$]h&]uh1hhhhhhhhKMubh)}(hhh](h)}(h$Example 3: Handling In-Place Updatesh]h$Example 3: Handling In-Place Updates}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hThe system-call auditing code does not update auditing rules in place. However, if it did, the reader-writer-locked code to do so might look as follows (assuming only ``field_count`` is updated, otherwise, the added fields would need to be filled in)::h](hThe system-call auditing code does not update auditing rules in place. However, if it did, the reader-writer-locked code to do so might look as follows (assuming only }(hThe system-call auditing code does not update auditing rules in place. However, if it did, the reader-writer-locked code to do so might look as follows (assuming only hjhhhNhNubh)}(h``field_count``h]h field_count}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhE is updated, otherwise, the added fields would need to be filled in):}(hE is updated, otherwise, the added fields would need to be filled in):hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjO)}(hX"static inline int audit_upd_rule(struct audit_rule *rule, struct list_head *list, __u32 newaction, __u32 newfield_count) { struct audit_entry *e; struct audit_entry *ne; write_lock(&auditsc_lock); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { e->rule.action = newaction; e->rule.field_count = newfield_count; write_unlock(&auditsc_lock); return 0; } } write_unlock(&auditsc_lock); return -EFAULT; /* No matching rule */ }h]hX"static inline int audit_upd_rule(struct audit_rule *rule, struct list_head *list, __u32 newaction, __u32 newfield_count) { struct audit_entry *e; struct audit_entry *ne; write_lock(&auditsc_lock); /* Note: audit_filter_mutex held by caller. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { e->rule.action = newaction; e->rule.field_count = newfield_count; write_unlock(&auditsc_lock); return 0; } } write_unlock(&auditsc_lock); return -EFAULT; /* No matching rule */ }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhKhjhhubh)}(hThe RCU version creates a copy, updates the copy, then replaces the old entry with the newly updated entry. This sequence of actions, allowing concurrent reads while making a copy to perform an update, is what gives RCU (*read-copy update*) its name.h](hThe RCU version creates a copy, updates the copy, then replaces the old entry with the newly updated entry. This sequence of actions, allowing concurrent reads while making a copy to perform an update, is what gives RCU (}(hThe RCU version creates a copy, updates the copy, then replaces the old entry with the newly updated entry. This sequence of actions, allowing concurrent reads while making a copy to perform an update, is what gives RCU (hjhhhNhNubhemphasis)}(h*read-copy update*h]hread-copy update}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh ) its name.}(h ) its name.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h3The RCU version of audit_upd_rule() is as follows::h]h2The RCU version of audit_upd_rule() is as follows:}(h2The RCU version of audit_upd_rule() is as follows:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubjO)}(hXstatic inline int audit_upd_rule(struct audit_rule *rule, struct list_head *list, __u32 newaction, __u32 newfield_count) { struct audit_entry *e; struct audit_entry *ne; list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { ne = kmalloc(sizeof(*entry), GFP_ATOMIC); if (ne == NULL) return -ENOMEM; audit_copy_rule(&ne->rule, &e->rule); ne->rule.action = newaction; ne->rule.field_count = newfield_count; list_replace_rcu(&e->list, &ne->list); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ }h]hXstatic inline int audit_upd_rule(struct audit_rule *rule, struct list_head *list, __u32 newaction, __u32 newfield_count) { struct audit_entry *e; struct audit_entry *ne; list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { ne = kmalloc(sizeof(*entry), GFP_ATOMIC); if (ne == NULL) return -ENOMEM; audit_copy_rule(&ne->rule, &e->rule); ne->rule.action = newaction; ne->rule.field_count = newfield_count; list_replace_rcu(&e->list, &ne->list); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhM hjhhubh)}(hAgain, this assumes that the caller holds ``audit_filter_mutex``. Normally, the writer lock would become a spinlock in this sort of code.h](h*Again, this assumes that the caller holds }(h*Again, this assumes that the caller holds hj#hhhNhNubh)}(h``audit_filter_mutex``h]haudit_filter_mutex}(hhhj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#ubhJ. Normally, the writer lock would become a spinlock in this sort of code.}(hJ. Normally, the writer lock would become a spinlock in this sort of code.hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM%hjhhubh)}(hpThe update_lsm_rule() does something very similar, for those who would prefer to look at real Linux-kernel code.h]hpThe update_lsm_rule() does something very similar, for those who would prefer to look at real Linux-kernel code.}(hjGhjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM(hjhhubh)}(hXlAnother use of this pattern can be found in the openswitch driver's *connection tracking table* code in ``ct_limit_set()``. The table holds connection tracking entries and has a limit on the maximum entries. There is one such table per-zone and hence one *limit* per zone. The zones are mapped to their limits through a hashtable using an RCU-managed hlist for the hash chains. When a new limit is set, a new limit object is allocated and ``ct_limit_set()`` is called to replace the old limit object with the new one using list_replace_rcu(). The old limit object is then freed after a grace period using kfree_rcu().h](hFAnother use of this pattern can be found in the openswitch driver’s }(hDAnother use of this pattern can be found in the openswitch driver's hjShhhNhNubj)}(h*connection tracking table*h]hconnection tracking table}(hhhj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh code in }(h code in hjShhhNhNubh)}(h``ct_limit_set()``h]hct_limit_set()}(hhhjohhhNhNubah}(h]h ]h"]h$]h&]uh1hhjSubh. The table holds connection tracking entries and has a limit on the maximum entries. There is one such table per-zone and hence one }(h. The table holds connection tracking entries and has a limit on the maximum entries. There is one such table per-zone and hence one hjShhhNhNubj)}(h*limit*h]hlimit}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh per zone. The zones are mapped to their limits through a hashtable using an RCU-managed hlist for the hash chains. When a new limit is set, a new limit object is allocated and }(h per zone. The zones are mapped to their limits through a hashtable using an RCU-managed hlist for the hash chains. When a new limit is set, a new limit object is allocated and hjShhhNhNubh)}(h``ct_limit_set()``h]hct_limit_set()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjSubh is called to replace the old limit object with the new one using list_replace_rcu(). The old limit object is then freed after a grace period using kfree_rcu().}(h is called to replace the old limit object with the new one using list_replace_rcu(). The old limit object is then freed after a grace period using kfree_rcu().hjShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM+hjhhubeh}(h]#example-3-handling-in-place-updatesah ]h"]$example 3: handling in-place updatesah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h!Example 4: Eliminating Stale Datah]h!Example 4: Eliminating Stale Data}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM6ubh)}(hXHThe auditing example above tolerates stale data, as do most algorithms that are tracking external state. After all, given there is a delay from the time the external state changes before Linux becomes aware of the change, and so as noted earlier, a small quantity of additional RCU-induced staleness is generally not a problem.h]hXHThe auditing example above tolerates stale data, as do most algorithms that are tracking external state. After all, given there is a delay from the time the external state changes before Linux becomes aware of the change, and so as noted earlier, a small quantity of additional RCU-induced staleness is generally not a problem.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM8hjhhubh)}(hXHowever, there are many examples where stale data cannot be tolerated. One example in the Linux kernel is the System V IPC (see the shm_lock() function in ipc/shm.c). This code checks a *deleted* flag under a per-entry spinlock, and, if the *deleted* flag is set, pretends that the entry does not exist. For this to be helpful, the search function must return holding the per-entry spinlock, as shm_lock() does in fact do.h](hHowever, there are many examples where stale data cannot be tolerated. One example in the Linux kernel is the System V IPC (see the shm_lock() function in ipc/shm.c). This code checks a }(hHowever, there are many examples where stale data cannot be tolerated. One example in the Linux kernel is the System V IPC (see the shm_lock() function in ipc/shm.c). This code checks a hjhhhNhNubj)}(h *deleted*h]hdeleted}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. flag under a per-entry spinlock, and, if the }(h. flag under a per-entry spinlock, and, if the hjhhhNhNubj)}(h *deleted*h]hdeleted}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh flag is set, pretends that the entry does not exist. For this to be helpful, the search function must return holding the per-entry spinlock, as shm_lock() does in fact do.}(h flag is set, pretends that the entry does not exist. For this to be helpful, the search function must return holding the per-entry spinlock, as shm_lock() does in fact do.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM>hjhhubh)}(h.. _quick_quiz:h]h}(h]h ]h"]h$]h&]h quick-quizuh1hhMEhjhhhhubhdefinition_list)}(hhh]hdefinition_list_item)}(hQuick Quiz: For the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function? h](hterm)}(h Quick Quiz:h]h Quick Quiz:}(hj$hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1j hhhMIhjubh definition)}(hhh]h)}(hFor the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function?h]hFor the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function?}(hj7hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMHhj2ubah}(h]h ]h"]h$]h&]uh1j0hjubeh}(h]h ]h"]h$]h&]uh1jhhhMIhjubah}(h]jah ]h"] quick_quizah$]h&]uh1jhjhhhhhNexpect_referenced_by_name}jSj sexpect_referenced_by_id}jj subh)}(h/:ref:`Answer to Quick Quiz `h]h)}(hj\h]hinline)}(hj\h]hAnswer to Quick Quiz}(hhhjchhhNhNubah}(h]h ](xrefstdstd-refeh"]h$]h&]uh1jahj^ubah}(h]h ]h"]h$]h&]refdoc RCU/listRCU refdomainjnreftyperef refexplicitrefwarn reftargetquick_quiz_answeruh1hhhhMKhjZubah}(h]h ]h"]h$]h&]uh1hhhhMKhjhhubh)}(hIf the system-call audit module were to ever need to reject stale data, one way to accomplish this would be to add a ``deleted`` flag and a ``lock`` spinlock to the ``audit_entry`` structure, and modify audit_filter_task() as follows::h](huIf the system-call audit module were to ever need to reject stale data, one way to accomplish this would be to add a }(huIf the system-call audit module were to ever need to reject stale data, one way to accomplish this would be to add a hjhhhNhNubh)}(h ``deleted``h]hdeleted}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh flag and a }(h flag and a hjhhhNhNubh)}(h``lock``h]hlock}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh spinlock to the }(h spinlock to the hjhhhNhNubh)}(h``audit_entry``h]h audit_entry}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh6 structure, and modify audit_filter_task() as follows:}(h6 structure, and modify audit_filter_task() as follows:hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubjO)}(hXnstatic enum audit_state audit_filter_task(struct task_struct *tsk) { struct audit_entry *e; enum audit_state state; rcu_read_lock(); list_for_each_entry_rcu(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { spin_lock(&e->lock); if (e->deleted) { spin_unlock(&e->lock); rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; } rcu_read_unlock(); if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); return state; } } rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; }h]hXnstatic enum audit_state audit_filter_task(struct task_struct *tsk) { struct audit_entry *e; enum audit_state state; rcu_read_lock(); list_for_each_entry_rcu(e, &audit_tsklist, list) { if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { spin_lock(&e->lock); if (e->deleted) { spin_unlock(&e->lock); rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; } rcu_read_unlock(); if (state == AUDIT_STATE_RECORD) *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); return state; } } rcu_read_unlock(); return AUDIT_BUILD_CONTEXT; }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhMQhjhhubh)}(hhThe ``audit_del_rule()`` function would need to set the ``deleted`` flag under the spinlock as follows::h](hThe }(hThe hjhhhNhNubh)}(h``audit_del_rule()``h]haudit_del_rule()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh function would need to set the }(h function would need to set the hjhhhNhNubh)}(h ``deleted``h]hdeleted}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh$ flag under the spinlock as follows:}(h$ flag under the spinlock as follows:hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMihjhhubjO)}(hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; /* No need to use the _rcu iterator here, since this * is the only deletion routine. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { spin_lock(&e->lock); list_del_rcu(&e->list); e->deleted = 1; spin_unlock(&e->lock); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ }h]hXstatic inline int audit_del_rule(struct audit_rule *rule, struct list_head *list) { struct audit_entry *e; /* No need to use the _rcu iterator here, since this * is the only deletion routine. */ list_for_each_entry(e, list, list) { if (!audit_compare_rule(rule, &e->rule)) { spin_lock(&e->lock); list_del_rcu(&e->list); e->deleted = 1; spin_unlock(&e->lock); call_rcu(&e->rcu, audit_free_rule); return 0; } } return -EFAULT; /* No matching rule */ }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhMlhjhhubh)}(h>This too assumes that the caller holds ``audit_filter_mutex``.h](h'This too assumes that the caller holds }(h'This too assumes that the caller holds hj!hhhNhNubh)}(h``audit_filter_mutex``h]haudit_filter_mutex}(hhhj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj!ubh.}(h.hj!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXLNote that this example assumes that entries are only added and deleted. Additional mechanism is required to deal correctly with the update-in-place performed by audit_upd_rule(). For one thing, audit_upd_rule() would need to hold the locks of both the old ``audit_entry`` and its replacement while executing the list_replace_rcu().h](hXNote that this example assumes that entries are only added and deleted. Additional mechanism is required to deal correctly with the update-in-place performed by audit_upd_rule(). For one thing, audit_upd_rule() would need to hold the locks of both the old }(hXNote that this example assumes that entries are only added and deleted. Additional mechanism is required to deal correctly with the update-in-place performed by audit_upd_rule(). For one thing, audit_upd_rule() would need to hold the locks of both the old hjChhhNhNubh)}(h``audit_entry``h]h audit_entry}(hhhjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjCubh< and its replacement while executing the list_replace_rcu().}(h< and its replacement while executing the list_replace_rcu().hjChhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h] example-4-eliminating-stale-dataah ]h"]!example 4: eliminating stale dataah$]h&]uh1hhhhhhhhM6ubh)}(hhh](h)}(h!Example 5: Skipping Stale Objectsh]h!Example 5: Skipping Stale Objects}(hjrhjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhjmhhhhhMubh)}(hXFor some use cases, reader performance can be improved by skipping stale objects during read-side list traversal, where stale objects are those that will be removed and destroyed after one or more grace periods. One such example can be found in the timerfd subsystem. When a ``CLOCK_REALTIME`` clock is reprogrammed (for example due to setting of the system time) then all programmed ``timerfds`` that depend on this clock get triggered and processes waiting on them are awakened in advance of their scheduled expiry. To facilitate this, all such timers are added to an RCU-managed ``cancel_list`` when they are setup in ``timerfd_setup_cancel()``::h](hXFor some use cases, reader performance can be improved by skipping stale objects during read-side list traversal, where stale objects are those that will be removed and destroyed after one or more grace periods. One such example can be found in the timerfd subsystem. When a }(hXFor some use cases, reader performance can be improved by skipping stale objects during read-side list traversal, where stale objects are those that will be removed and destroyed after one or more grace periods. One such example can be found in the timerfd subsystem. When a hj~hhhNhNubh)}(h``CLOCK_REALTIME``h]hCLOCK_REALTIME}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~ubh[ clock is reprogrammed (for example due to setting of the system time) then all programmed }(h[ clock is reprogrammed (for example due to setting of the system time) then all programmed hj~hhhNhNubh)}(h ``timerfds``h]htimerfds}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~ubh that depend on this clock get triggered and processes waiting on them are awakened in advance of their scheduled expiry. To facilitate this, all such timers are added to an RCU-managed }(h that depend on this clock get triggered and processes waiting on them are awakened in advance of their scheduled expiry. To facilitate this, all such timers are added to an RCU-managed hj~hhhNhNubh)}(h``cancel_list``h]h cancel_list}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~ubh when they are setup in }(h when they are setup in hj~hhhNhNubh)}(h``timerfd_setup_cancel()``h]htimerfd_setup_cancel()}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~ubh:}(h:hj~hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubjO)}(hXstatic void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) { spin_lock(&ctx->cancel_lock); if ((ctx->clockid == CLOCK_REALTIME || ctx->clockid == CLOCK_REALTIME_ALARM) && (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { if (!ctx->might_cancel) { ctx->might_cancel = true; spin_lock(&cancel_lock); list_add_rcu(&ctx->clist, &cancel_list); spin_unlock(&cancel_lock); } } else { __timerfd_remove_cancel(ctx); } spin_unlock(&ctx->cancel_lock); }h]hXstatic void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) { spin_lock(&ctx->cancel_lock); if ((ctx->clockid == CLOCK_REALTIME || ctx->clockid == CLOCK_REALTIME_ALARM) && (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { if (!ctx->might_cancel) { ctx->might_cancel = true; spin_lock(&cancel_lock); list_add_rcu(&ctx->clist, &cancel_list); spin_unlock(&cancel_lock); } } else { __timerfd_remove_cancel(ctx); } spin_unlock(&ctx->cancel_lock); }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhMhjmhhubh)}(hWhen a timerfd is freed (fd is closed), then the ``might_cancel`` flag of the timerfd object is cleared, the object removed from the ``cancel_list`` and destroyed, as shown in this simplified and inlined version of timerfd_release()::h](h1When a timerfd is freed (fd is closed), then the }(h1When a timerfd is freed (fd is closed), then the hjhhhNhNubh)}(h``might_cancel``h]h might_cancel}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhD flag of the timerfd object is cleared, the object removed from the }(hD flag of the timerfd object is cleared, the object removed from the hjhhhNhNubh)}(h``cancel_list``h]h cancel_list}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhU and destroyed, as shown in this simplified and inlined version of timerfd_release():}(hU and destroyed, as shown in this simplified and inlined version of timerfd_release():hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubjO)}(hXJint timerfd_release(struct inode *inode, struct file *file) { struct timerfd_ctx *ctx = file->private_data; spin_lock(&ctx->cancel_lock); if (ctx->might_cancel) { ctx->might_cancel = false; spin_lock(&cancel_lock); list_del_rcu(&ctx->clist); spin_unlock(&cancel_lock); } spin_unlock(&ctx->cancel_lock); if (isalarm(ctx)) alarm_cancel(&ctx->t.alarm); else hrtimer_cancel(&ctx->t.tmr); kfree_rcu(ctx, rcu); return 0; }h]hXJint timerfd_release(struct inode *inode, struct file *file) { struct timerfd_ctx *ctx = file->private_data; spin_lock(&ctx->cancel_lock); if (ctx->might_cancel) { ctx->might_cancel = false; spin_lock(&cancel_lock); list_del_rcu(&ctx->clist); spin_unlock(&cancel_lock); } spin_unlock(&ctx->cancel_lock); if (isalarm(ctx)) alarm_cancel(&ctx->t.alarm); else hrtimer_cancel(&ctx->t.tmr); kfree_rcu(ctx, rcu); return 0; }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhMhjmhhubh)}(hX0If the ``CLOCK_REALTIME`` clock is set, for example by a time server, the hrtimer framework calls ``timerfd_clock_was_set()`` which walks the ``cancel_list`` and wakes up processes waiting on the timerfd. While iterating the ``cancel_list``, the ``might_cancel`` flag is consulted to skip stale objects::h](hIf the }(hIf the hj*hhhNhNubh)}(h``CLOCK_REALTIME``h]hCLOCK_REALTIME}(hhhj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubhI clock is set, for example by a time server, the hrtimer framework calls }(hI clock is set, for example by a time server, the hrtimer framework calls hj*hhhNhNubh)}(h``timerfd_clock_was_set()``h]htimerfd_clock_was_set()}(hhhjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubh which walks the }(h which walks the hj*hhhNhNubh)}(h``cancel_list``h]h cancel_list}(hhhjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubhD and wakes up processes waiting on the timerfd. While iterating the }(hD and wakes up processes waiting on the timerfd. While iterating the hj*hhhNhNubh)}(h``cancel_list``h]h cancel_list}(hhhjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubh, the }(h, the hj*hhhNhNubh)}(h``might_cancel``h]h might_cancel}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubh) flag is consulted to skip stale objects:}(h) flag is consulted to skip stale objects:hj*hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubjO)}(hXvoid timerfd_clock_was_set(void) { ktime_t moffs = ktime_mono_to_real(0); struct timerfd_ctx *ctx; unsigned long flags; rcu_read_lock(); list_for_each_entry_rcu(ctx, &cancel_list, clist) { if (!ctx->might_cancel) continue; spin_lock_irqsave(&ctx->wqh.lock, flags); if (ctx->moffs != moffs) { ctx->moffs = KTIME_MAX; ctx->ticks++; wake_up_locked_poll(&ctx->wqh, EPOLLIN); } spin_unlock_irqrestore(&ctx->wqh.lock, flags); } rcu_read_unlock(); }h]hXvoid timerfd_clock_was_set(void) { ktime_t moffs = ktime_mono_to_real(0); struct timerfd_ctx *ctx; unsigned long flags; rcu_read_lock(); list_for_each_entry_rcu(ctx, &cancel_list, clist) { if (!ctx->might_cancel) continue; spin_lock_irqsave(&ctx->wqh.lock, flags); if (ctx->moffs != moffs) { ctx->moffs = KTIME_MAX; ctx->ticks++; wake_up_locked_poll(&ctx->wqh, EPOLLIN); } spin_unlock_irqrestore(&ctx->wqh.lock, flags); } rcu_read_unlock(); }}(hhhjubah}(h]h ]h"]h$]h&]j^j_uh1jNhhhMhjmhhubh)}(hX The key point is that because RCU-protected traversal of the ``cancel_list`` happens concurrently with object addition and removal, sometimes the traversal can access an object that has been removed from the list. In this example, a flag is used to skip such objects.h](h=The key point is that because RCU-protected traversal of the }(h=The key point is that because RCU-protected traversal of the hjhhhNhNubh)}(h``cancel_list``h]h cancel_list}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh happens concurrently with object addition and removal, sometimes the traversal can access an object that has been removed from the list. In this example, a flag is used to skip such objects.}(h happens concurrently with object addition and removal, sometimes the traversal can access an object that has been removed from the list. In this example, a flag is used to skip such objects.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubeh}(h] example-5-skipping-stale-objectsah ]h"]!example 5: skipping stale objectsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hSummaryh]hSummary}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXRead-mostly list-based data structures that can tolerate stale data are the most amenable to use of RCU. The simplest case is where entries are either added or deleted from the data structure (or atomically modified in place), but non-atomic in-place modifications can be handled by making a copy, updating the copy, then replacing the original with the copy. If stale data cannot be tolerated, then a *deleted* flag may be used in conjunction with a per-entry spinlock in order to allow the search function to reject newly deleted data.h](hXRead-mostly list-based data structures that can tolerate stale data are the most amenable to use of RCU. The simplest case is where entries are either added or deleted from the data structure (or atomically modified in place), but non-atomic in-place modifications can be handled by making a copy, updating the copy, then replacing the original with the copy. If stale data cannot be tolerated, then a }(hXRead-mostly list-based data structures that can tolerate stale data are the most amenable to use of RCU. The simplest case is where entries are either added or deleted from the data structure (or atomically modified in place), but non-atomic in-place modifications can be handled by making a copy, updating the copy, then replacing the original with the copy. If stale data cannot be tolerated, then a hjhhhNhNubj)}(h *deleted*h]hdeleted}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh~ flag may be used in conjunction with a per-entry spinlock in order to allow the search function to reject newly deleted data.}(h~ flag may be used in conjunction with a per-entry spinlock in order to allow the search function to reject newly deleted data.hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h.. _quick_quiz_answer:h]h}(h]h ]h"]h$]h&]hquick-quiz-answeruh1hhMhjhhhhubj)}(hhh]j)}(hXAnswer to Quick Quiz: For the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function? If the search function drops the per-entry lock before returning, then the caller will be processing stale data in any case. If it is really OK to be processing stale data, then you don't need a *deleted* flag. If processing stale data really is a problem, then you need to hold the per-entry lock across all of the code that uses the value that was returned. h](j!)}(hAnswer to Quick Quiz:h]hAnswer to Quick Quiz:}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hhhMhjubj1)}(hhh](h)}(hFor the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function?h]hFor the deleted-flag technique to be helpful, why is it necessary to hold the per-entry lock while returning from the search function?}(hj(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj#ubh)}(hXiIf the search function drops the per-entry lock before returning, then the caller will be processing stale data in any case. If it is really OK to be processing stale data, then you don't need a *deleted* flag. If processing stale data really is a problem, then you need to hold the per-entry lock across all of the code that uses the value that was returned.h](hIf the search function drops the per-entry lock before returning, then the caller will be processing stale data in any case. If it is really OK to be processing stale data, then you don’t need a }(hIf the search function drops the per-entry lock before returning, then the caller will be processing stale data in any case. If it is really OK to be processing stale data, then you don't need a hj4hhhNhNubj)}(h *deleted*h]hdeleted}(hhhj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubh flag. If processing stale data really is a problem, then you need to hold the per-entry lock across all of the code that uses the value that was returned.}(h flag. If processing stale data really is a problem, then you need to hold the per-entry lock across all of the code that uses the value that was returned.Ehj4hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj#ubeh}(h]h ]h"]h$]h&]uh1j0hjubeh}(h]h ]h"]h$]h&]uh1jhhhMhjubah}(h]j ah ]h"]quick_quiz_answerah$]h&]uh1jhjhhhhhNjV}jfjsjX}j jsubh)}(h&:ref:`Back to Quick Quiz `h]h)}(hjmh]jb)}(hjmh]hBack to Quick Quiz}(hhhjrhhhNhNubah}(h]h ](jmstdstd-refeh"]h$]h&]uh1jahjoubah}(h]h ]h"]h$]h&]refdocjz refdomainj|reftyperef refexplicitrefwarnj quick_quizuh1hhhhMhjkubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]summaryah ]h"]summaryah$]h&]uh1hhhhhhhhMubeh}(h](-using-rcu-to-protect-read-mostly-linked-listsheh ]h"](-using rcu to protect read-mostly linked lists list_rcu_doceh$]h&]uh1hhhhhhhhKjV}jhsjX}hhsubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingUTF-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confapep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacefile_insertion_enabled raw_enabledKline_length_limitM'syntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_link embed_imagesenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}(h]haj]j aj ]jaunameids}(jhjjjnjkjjjjjjjgjSjjjjjjfj u nametypes}(jjNjnNjNjNjjNjSjNjNjfuh}(hhjhjkhjjqjjjgjjjjjmjjj ju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages](hsystem_message)}(hhh]h)}(hhh]h2Hyperlink target "list-rcu-doc" is not referenced.}(hhhj7 ubah}(h]h ]h"]h$]h&]uh1hhj4 ubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehlineKuh1j2 ubj3 )}(hhh]h)}(hhh]h0Hyperlink target "quick-quiz" is not referenced.}(hhhjR ubah}(h]h ]h"]h$]h&]uh1hhjO ubah}(h]h ]h"]h$]h&]levelKtypejL sourcehlineMEuh1j2 ubj3 )}(hhh]h)}(hhh]h7Hyperlink target "quick-quiz-answer" is not referenced.}(hhhjl ubah}(h]h ]h"]h$]h&]uh1hhji ubah}(h]h ]h"]h$]h&]levelKtypejL sourcehlineMuh1j2 ube transformerN include_log] decorationNhhub.