listRCU.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. Using RCU to Protect Read-Mostly Linked Lists
  2. One of the best applications of RCU is to protect read-mostly linked lists
  3. ("struct list_head" in list.h). One big advantage of this approach
  4. is that all of the required memory barriers are included for you in
  5. the list macros. This document describes several applications of RCU,
  6. with the best fits first.
  7. Example 1: Read-Side Action Taken Outside of Lock, No In-Place Updates
  8. The best applications are cases where, if reader-writer locking were
  9. used, the read-side lock would be dropped before taking any action
  10. based on the results of the search. The most celebrated example is
  11. the routing table. Because the routing table is tracking the state of
  12. equipment outside of the computer, it will at times contain stale data.
  13. Therefore, once the route has been computed, there is no need to hold
  14. the routing table static during transmission of the packet. After all,
  15. you can hold the routing table static all you want, but that won't keep
  16. the external Internet from changing, and it is the state of the external
  17. Internet that really matters. In addition, routing entries are typically
  18. added or deleted, rather than being modified in place.
  19. A straightforward example of this use of RCU may be found in the
  20. system-call auditing support. For example, a reader-writer locked
  21. implementation of audit_filter_task() might be as follows:
  22. static enum audit_state audit_filter_task(struct task_struct *tsk)
  23. {
  24. struct audit_entry *e;
  25. enum audit_state state;
  26. read_lock(&auditsc_lock);
  27. /* Note: audit_netlink_sem held by caller. */
  28. list_for_each_entry(e, &audit_tsklist, list) {
  29. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  30. read_unlock(&auditsc_lock);
  31. return state;
  32. }
  33. }
  34. read_unlock(&auditsc_lock);
  35. return AUDIT_BUILD_CONTEXT;
  36. }
  37. Here the list is searched under the lock, but the lock is dropped before
  38. the corresponding value is returned. By the time that this value is acted
  39. on, the list may well have been modified. This makes sense, since if
  40. you are turning auditing off, it is OK to audit a few extra system calls.
  41. This means that RCU can be easily applied to the read side, as follows:
  42. static enum audit_state audit_filter_task(struct task_struct *tsk)
  43. {
  44. struct audit_entry *e;
  45. enum audit_state state;
  46. rcu_read_lock();
  47. /* Note: audit_netlink_sem held by caller. */
  48. list_for_each_entry_rcu(e, &audit_tsklist, list) {
  49. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  50. rcu_read_unlock();
  51. return state;
  52. }
  53. }
  54. rcu_read_unlock();
  55. return AUDIT_BUILD_CONTEXT;
  56. }
  57. The read_lock() and read_unlock() calls have become rcu_read_lock()
  58. and rcu_read_unlock(), respectively, and the list_for_each_entry() has
  59. become list_for_each_entry_rcu(). The _rcu() list-traversal primitives
  60. insert the read-side memory barriers that are required on DEC Alpha CPUs.
  61. The changes to the update side are also straightforward. A reader-writer
  62. lock might be used as follows for deletion and insertion:
  63. static inline int audit_del_rule(struct audit_rule *rule,
  64. struct list_head *list)
  65. {
  66. struct audit_entry *e;
  67. write_lock(&auditsc_lock);
  68. list_for_each_entry(e, list, list) {
  69. if (!audit_compare_rule(rule, &e->rule)) {
  70. list_del(&e->list);
  71. write_unlock(&auditsc_lock);
  72. return 0;
  73. }
  74. }
  75. write_unlock(&auditsc_lock);
  76. return -EFAULT; /* No matching rule */
  77. }
  78. static inline int audit_add_rule(struct audit_entry *entry,
  79. struct list_head *list)
  80. {
  81. write_lock(&auditsc_lock);
  82. if (entry->rule.flags & AUDIT_PREPEND) {
  83. entry->rule.flags &= ~AUDIT_PREPEND;
  84. list_add(&entry->list, list);
  85. } else {
  86. list_add_tail(&entry->list, list);
  87. }
  88. write_unlock(&auditsc_lock);
  89. return 0;
  90. }
  91. Following are the RCU equivalents for these two functions:
  92. static inline int audit_del_rule(struct audit_rule *rule,
  93. struct list_head *list)
  94. {
  95. struct audit_entry *e;
  96. /* Do not use the _rcu iterator here, since this is the only
  97. * deletion routine. */
  98. list_for_each_entry(e, list, list) {
  99. if (!audit_compare_rule(rule, &e->rule)) {
  100. list_del_rcu(&e->list);
  101. call_rcu(&e->rcu, audit_free_rule);
  102. return 0;
  103. }
  104. }
  105. return -EFAULT; /* No matching rule */
  106. }
  107. static inline int audit_add_rule(struct audit_entry *entry,
  108. struct list_head *list)
  109. {
  110. if (entry->rule.flags & AUDIT_PREPEND) {
  111. entry->rule.flags &= ~AUDIT_PREPEND;
  112. list_add_rcu(&entry->list, list);
  113. } else {
  114. list_add_tail_rcu(&entry->list, list);
  115. }
  116. return 0;
  117. }
  118. Normally, the write_lock() and write_unlock() would be replaced by
  119. a spin_lock() and a spin_unlock(), but in this case, all callers hold
  120. audit_netlink_sem, so no additional locking is required. The auditsc_lock
  121. can therefore be eliminated, since use of RCU eliminates the need for
  122. writers to exclude readers. Normally, the write_lock() calls would
  123. be converted into spin_lock() calls.
  124. The list_del(), list_add(), and list_add_tail() primitives have been
  125. replaced by list_del_rcu(), list_add_rcu(), and list_add_tail_rcu().
  126. The _rcu() list-manipulation primitives add memory barriers that are
  127. needed on weakly ordered CPUs (most of them!). The list_del_rcu()
  128. primitive omits the pointer poisoning debug-assist code that would
  129. otherwise cause concurrent readers to fail spectacularly.
  130. So, when readers can tolerate stale data and when entries are either added
  131. or deleted, without in-place modification, it is very easy to use RCU!
  132. Example 2: Handling In-Place Updates
  133. The system-call auditing code does not update auditing rules in place.
  134. However, if it did, reader-writer-locked code to do so might look as
  135. follows (presumably, the field_count is only permitted to decrease,
  136. otherwise, the added fields would need to be filled in):
  137. static inline int audit_upd_rule(struct audit_rule *rule,
  138. struct list_head *list,
  139. __u32 newaction,
  140. __u32 newfield_count)
  141. {
  142. struct audit_entry *e;
  143. struct audit_newentry *ne;
  144. write_lock(&auditsc_lock);
  145. /* Note: audit_netlink_sem held by caller. */
  146. list_for_each_entry(e, list, list) {
  147. if (!audit_compare_rule(rule, &e->rule)) {
  148. e->rule.action = newaction;
  149. e->rule.file_count = newfield_count;
  150. write_unlock(&auditsc_lock);
  151. return 0;
  152. }
  153. }
  154. write_unlock(&auditsc_lock);
  155. return -EFAULT; /* No matching rule */
  156. }
  157. The RCU version creates a copy, updates the copy, then replaces the old
  158. entry with the newly updated entry. This sequence of actions, allowing
  159. concurrent reads while doing a copy to perform an update, is what gives
  160. RCU ("read-copy update") its name. The RCU code is as follows:
  161. static inline int audit_upd_rule(struct audit_rule *rule,
  162. struct list_head *list,
  163. __u32 newaction,
  164. __u32 newfield_count)
  165. {
  166. struct audit_entry *e;
  167. struct audit_newentry *ne;
  168. list_for_each_entry(e, list, list) {
  169. if (!audit_compare_rule(rule, &e->rule)) {
  170. ne = kmalloc(sizeof(*entry), GFP_ATOMIC);
  171. if (ne == NULL)
  172. return -ENOMEM;
  173. audit_copy_rule(&ne->rule, &e->rule);
  174. ne->rule.action = newaction;
  175. ne->rule.file_count = newfield_count;
  176. list_replace_rcu(e, ne);
  177. call_rcu(&e->rcu, audit_free_rule);
  178. return 0;
  179. }
  180. }
  181. return -EFAULT; /* No matching rule */
  182. }
  183. Again, this assumes that the caller holds audit_netlink_sem. Normally,
  184. the reader-writer lock would become a spinlock in this sort of code.
  185. Example 3: Eliminating Stale Data
  186. The auditing examples above tolerate stale data, as do most algorithms
  187. that are tracking external state. Because there is a delay from the
  188. time the external state changes before Linux becomes aware of the change,
  189. additional RCU-induced staleness is normally not a problem.
  190. However, there are many examples where stale data cannot be tolerated.
  191. One example in the Linux kernel is the System V IPC (see the ipc_lock()
  192. function in ipc/util.c). This code checks a "deleted" flag under a
  193. per-entry spinlock, and, if the "deleted" flag is set, pretends that the
  194. entry does not exist. For this to be helpful, the search function must
  195. return holding the per-entry spinlock, as ipc_lock() does in fact do.
  196. Quick Quiz: Why does the search function need to return holding the
  197. per-entry lock for this deleted-flag technique to be helpful?
  198. If the system-call audit module were to ever need to reject stale data,
  199. one way to accomplish this would be to add a "deleted" flag and a "lock"
  200. spinlock to the audit_entry structure, and modify audit_filter_task()
  201. as follows:
  202. static enum audit_state audit_filter_task(struct task_struct *tsk)
  203. {
  204. struct audit_entry *e;
  205. enum audit_state state;
  206. rcu_read_lock();
  207. list_for_each_entry_rcu(e, &audit_tsklist, list) {
  208. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  209. spin_lock(&e->lock);
  210. if (e->deleted) {
  211. spin_unlock(&e->lock);
  212. rcu_read_unlock();
  213. return AUDIT_BUILD_CONTEXT;
  214. }
  215. rcu_read_unlock();
  216. return state;
  217. }
  218. }
  219. rcu_read_unlock();
  220. return AUDIT_BUILD_CONTEXT;
  221. }
  222. Note that this example assumes that entries are only added and deleted.
  223. Additional mechanism is required to deal correctly with the
  224. update-in-place performed by audit_upd_rule(). For one thing,
  225. audit_upd_rule() would need additional memory barriers to ensure
  226. that the list_add_rcu() was really executed before the list_del_rcu().
  227. The audit_del_rule() function would need to set the "deleted"
  228. flag under the spinlock as follows:
  229. static inline int audit_del_rule(struct audit_rule *rule,
  230. struct list_head *list)
  231. {
  232. struct audit_entry *e;
  233. /* Do not need to use the _rcu iterator here, since this
  234. * is the only deletion routine. */
  235. list_for_each_entry(e, list, list) {
  236. if (!audit_compare_rule(rule, &e->rule)) {
  237. spin_lock(&e->lock);
  238. list_del_rcu(&e->list);
  239. e->deleted = 1;
  240. spin_unlock(&e->lock);
  241. call_rcu(&e->rcu, audit_free_rule);
  242. return 0;
  243. }
  244. }
  245. return -EFAULT; /* No matching rule */
  246. }
  247. Summary
  248. Read-mostly list-based data structures that can tolerate stale data are
  249. the most amenable to use of RCU. The simplest case is where entries are
  250. either added or deleted from the data structure (or atomically modified
  251. in place), but non-atomic in-place modifications can be handled by making
  252. a copy, updating the copy, then replacing the original with the copy.
  253. If stale data cannot be tolerated, then a "deleted" flag may be used
  254. in conjunction with a per-entry spinlock in order to allow the search
  255. function to reject newly deleted data.
  256. Answer to Quick Quiz
  257. Why does the search function need to return holding the per-entry
  258. lock for this deleted-flag technique to be helpful?
  259. If the search function drops the per-entry lock before returning,
  260. then the caller will be processing stale data in any case. If it
  261. is really OK to be processing stale data, then you don't need a
  262. "deleted" flag. If processing stale data really is a problem,
  263. then you need to hold the per-entry lock across all of the code
  264. that uses the value that was returned.