rculist.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #ifndef _LINUX_RCULIST_H
  2. #define _LINUX_RCULIST_H
  3. #ifdef __KERNEL__
  4. /*
  5. * RCU-protected list version
  6. */
  7. #include <linux/list.h>
  8. /*
  9. * Insert a new entry between two known consecutive entries.
  10. *
  11. * This is only for internal list manipulation where we know
  12. * the prev/next entries already!
  13. */
  14. static inline void __list_add_rcu(struct list_head *new,
  15. struct list_head *prev, struct list_head *next)
  16. {
  17. new->next = next;
  18. new->prev = prev;
  19. smp_wmb();
  20. next->prev = new;
  21. prev->next = new;
  22. }
  23. /**
  24. * list_add_rcu - add a new entry to rcu-protected list
  25. * @new: new entry to be added
  26. * @head: list head to add it after
  27. *
  28. * Insert a new entry after the specified head.
  29. * This is good for implementing stacks.
  30. *
  31. * The caller must take whatever precautions are necessary
  32. * (such as holding appropriate locks) to avoid racing
  33. * with another list-mutation primitive, such as list_add_rcu()
  34. * or list_del_rcu(), running on this same list.
  35. * However, it is perfectly legal to run concurrently with
  36. * the _rcu list-traversal primitives, such as
  37. * list_for_each_entry_rcu().
  38. */
  39. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  40. {
  41. __list_add_rcu(new, head, head->next);
  42. }
  43. /**
  44. * list_add_tail_rcu - add a new entry to rcu-protected list
  45. * @new: new entry to be added
  46. * @head: list head to add it before
  47. *
  48. * Insert a new entry before the specified head.
  49. * This is useful for implementing queues.
  50. *
  51. * The caller must take whatever precautions are necessary
  52. * (such as holding appropriate locks) to avoid racing
  53. * with another list-mutation primitive, such as list_add_tail_rcu()
  54. * or list_del_rcu(), running on this same list.
  55. * However, it is perfectly legal to run concurrently with
  56. * the _rcu list-traversal primitives, such as
  57. * list_for_each_entry_rcu().
  58. */
  59. static inline void list_add_tail_rcu(struct list_head *new,
  60. struct list_head *head)
  61. {
  62. __list_add_rcu(new, head->prev, head);
  63. }
  64. /**
  65. * list_del_rcu - deletes entry from list without re-initialization
  66. * @entry: the element to delete from the list.
  67. *
  68. * Note: list_empty() on entry does not return true after this,
  69. * the entry is in an undefined state. It is useful for RCU based
  70. * lockfree traversal.
  71. *
  72. * In particular, it means that we can not poison the forward
  73. * pointers that may still be used for walking the list.
  74. *
  75. * The caller must take whatever precautions are necessary
  76. * (such as holding appropriate locks) to avoid racing
  77. * with another list-mutation primitive, such as list_del_rcu()
  78. * or list_add_rcu(), running on this same list.
  79. * However, it is perfectly legal to run concurrently with
  80. * the _rcu list-traversal primitives, such as
  81. * list_for_each_entry_rcu().
  82. *
  83. * Note that the caller is not permitted to immediately free
  84. * the newly deleted entry. Instead, either synchronize_rcu()
  85. * or call_rcu() must be used to defer freeing until an RCU
  86. * grace period has elapsed.
  87. */
  88. static inline void list_del_rcu(struct list_head *entry)
  89. {
  90. __list_del(entry->prev, entry->next);
  91. entry->prev = LIST_POISON2;
  92. }
  93. /**
  94. * list_replace_rcu - replace old entry by new one
  95. * @old : the element to be replaced
  96. * @new : the new element to insert
  97. *
  98. * The @old entry will be replaced with the @new entry atomically.
  99. * Note: @old should not be empty.
  100. */
  101. static inline void list_replace_rcu(struct list_head *old,
  102. struct list_head *new)
  103. {
  104. new->next = old->next;
  105. new->prev = old->prev;
  106. smp_wmb();
  107. new->next->prev = new;
  108. new->prev->next = new;
  109. old->prev = LIST_POISON2;
  110. }
  111. /**
  112. * list_splice_init_rcu - splice an RCU-protected list into an existing list.
  113. * @list: the RCU-protected list to splice
  114. * @head: the place in the list to splice the first list into
  115. * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
  116. *
  117. * @head can be RCU-read traversed concurrently with this function.
  118. *
  119. * Note that this function blocks.
  120. *
  121. * Important note: the caller must take whatever action is necessary to
  122. * prevent any other updates to @head. In principle, it is possible
  123. * to modify the list as soon as sync() begins execution.
  124. * If this sort of thing becomes necessary, an alternative version
  125. * based on call_rcu() could be created. But only if -really-
  126. * needed -- there is no shortage of RCU API members.
  127. */
  128. static inline void list_splice_init_rcu(struct list_head *list,
  129. struct list_head *head,
  130. void (*sync)(void))
  131. {
  132. struct list_head *first = list->next;
  133. struct list_head *last = list->prev;
  134. struct list_head *at = head->next;
  135. if (list_empty(head))
  136. return;
  137. /* "first" and "last" tracking list, so initialize it. */
  138. INIT_LIST_HEAD(list);
  139. /*
  140. * At this point, the list body still points to the source list.
  141. * Wait for any readers to finish using the list before splicing
  142. * the list body into the new list. Any new readers will see
  143. * an empty list.
  144. */
  145. sync();
  146. /*
  147. * Readers are finished with the source list, so perform splice.
  148. * The order is important if the new list is global and accessible
  149. * to concurrent RCU readers. Note that RCU readers are not
  150. * permitted to traverse the prev pointers without excluding
  151. * this function.
  152. */
  153. last->next = at;
  154. smp_wmb();
  155. head->next = first;
  156. first->prev = head;
  157. at->prev = last;
  158. }
  159. /**
  160. * list_for_each_rcu - iterate over an rcu-protected list
  161. * @pos: the &struct list_head to use as a loop cursor.
  162. * @head: the head for your list.
  163. *
  164. * This list-traversal primitive may safely run concurrently with
  165. * the _rcu list-mutation primitives such as list_add_rcu()
  166. * as long as the traversal is guarded by rcu_read_lock().
  167. */
  168. #define list_for_each_rcu(pos, head) \
  169. for (pos = (head)->next; \
  170. prefetch(rcu_dereference(pos)->next), pos != (head); \
  171. pos = pos->next)
  172. #define __list_for_each_rcu(pos, head) \
  173. for (pos = (head)->next; \
  174. rcu_dereference(pos) != (head); \
  175. pos = pos->next)
  176. /**
  177. * list_for_each_safe_rcu
  178. * @pos: the &struct list_head to use as a loop cursor.
  179. * @n: another &struct list_head to use as temporary storage
  180. * @head: the head for your list.
  181. *
  182. * Iterate over an rcu-protected list, safe against removal of list entry.
  183. *
  184. * This list-traversal primitive may safely run concurrently with
  185. * the _rcu list-mutation primitives such as list_add_rcu()
  186. * as long as the traversal is guarded by rcu_read_lock().
  187. */
  188. #define list_for_each_safe_rcu(pos, n, head) \
  189. for (pos = (head)->next; \
  190. n = rcu_dereference(pos)->next, pos != (head); \
  191. pos = n)
  192. /**
  193. * list_for_each_entry_rcu - iterate over rcu list of given type
  194. * @pos: the type * to use as a loop cursor.
  195. * @head: the head for your list.
  196. * @member: the name of the list_struct within the struct.
  197. *
  198. * This list-traversal primitive may safely run concurrently with
  199. * the _rcu list-mutation primitives such as list_add_rcu()
  200. * as long as the traversal is guarded by rcu_read_lock().
  201. */
  202. #define list_for_each_entry_rcu(pos, head, member) \
  203. for (pos = list_entry((head)->next, typeof(*pos), member); \
  204. prefetch(rcu_dereference(pos)->member.next), \
  205. &pos->member != (head); \
  206. pos = list_entry(pos->member.next, typeof(*pos), member))
  207. /**
  208. * list_for_each_continue_rcu
  209. * @pos: the &struct list_head to use as a loop cursor.
  210. * @head: the head for your list.
  211. *
  212. * Iterate over an rcu-protected list, continuing after current point.
  213. *
  214. * This list-traversal primitive may safely run concurrently with
  215. * the _rcu list-mutation primitives such as list_add_rcu()
  216. * as long as the traversal is guarded by rcu_read_lock().
  217. */
  218. #define list_for_each_continue_rcu(pos, head) \
  219. for ((pos) = (pos)->next; \
  220. prefetch(rcu_dereference((pos))->next), (pos) != (head); \
  221. (pos) = (pos)->next)
  222. /**
  223. * hlist_del_rcu - deletes entry from hash list without re-initialization
  224. * @n: the element to delete from the hash list.
  225. *
  226. * Note: list_unhashed() on entry does not return true after this,
  227. * the entry is in an undefined state. It is useful for RCU based
  228. * lockfree traversal.
  229. *
  230. * In particular, it means that we can not poison the forward
  231. * pointers that may still be used for walking the hash list.
  232. *
  233. * The caller must take whatever precautions are necessary
  234. * (such as holding appropriate locks) to avoid racing
  235. * with another list-mutation primitive, such as hlist_add_head_rcu()
  236. * or hlist_del_rcu(), running on this same list.
  237. * However, it is perfectly legal to run concurrently with
  238. * the _rcu list-traversal primitives, such as
  239. * hlist_for_each_entry().
  240. */
  241. static inline void hlist_del_rcu(struct hlist_node *n)
  242. {
  243. __hlist_del(n);
  244. n->pprev = LIST_POISON2;
  245. }
  246. /**
  247. * hlist_replace_rcu - replace old entry by new one
  248. * @old : the element to be replaced
  249. * @new : the new element to insert
  250. *
  251. * The @old entry will be replaced with the @new entry atomically.
  252. */
  253. static inline void hlist_replace_rcu(struct hlist_node *old,
  254. struct hlist_node *new)
  255. {
  256. struct hlist_node *next = old->next;
  257. new->next = next;
  258. new->pprev = old->pprev;
  259. smp_wmb();
  260. if (next)
  261. new->next->pprev = &new->next;
  262. *new->pprev = new;
  263. old->pprev = LIST_POISON2;
  264. }
  265. /**
  266. * hlist_add_head_rcu
  267. * @n: the element to add to the hash list.
  268. * @h: the list to add to.
  269. *
  270. * Description:
  271. * Adds the specified element to the specified hlist,
  272. * while permitting racing traversals.
  273. *
  274. * The caller must take whatever precautions are necessary
  275. * (such as holding appropriate locks) to avoid racing
  276. * with another list-mutation primitive, such as hlist_add_head_rcu()
  277. * or hlist_del_rcu(), running on this same list.
  278. * However, it is perfectly legal to run concurrently with
  279. * the _rcu list-traversal primitives, such as
  280. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  281. * problems on Alpha CPUs. Regardless of the type of CPU, the
  282. * list-traversal primitive must be guarded by rcu_read_lock().
  283. */
  284. static inline void hlist_add_head_rcu(struct hlist_node *n,
  285. struct hlist_head *h)
  286. {
  287. struct hlist_node *first = h->first;
  288. n->next = first;
  289. n->pprev = &h->first;
  290. smp_wmb();
  291. if (first)
  292. first->pprev = &n->next;
  293. h->first = n;
  294. }
  295. /**
  296. * hlist_add_before_rcu
  297. * @n: the new element to add to the hash list.
  298. * @next: the existing element to add the new element before.
  299. *
  300. * Description:
  301. * Adds the specified element to the specified hlist
  302. * before the specified node while permitting racing traversals.
  303. *
  304. * The caller must take whatever precautions are necessary
  305. * (such as holding appropriate locks) to avoid racing
  306. * with another list-mutation primitive, such as hlist_add_head_rcu()
  307. * or hlist_del_rcu(), running on this same list.
  308. * However, it is perfectly legal to run concurrently with
  309. * the _rcu list-traversal primitives, such as
  310. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  311. * problems on Alpha CPUs.
  312. */
  313. static inline void hlist_add_before_rcu(struct hlist_node *n,
  314. struct hlist_node *next)
  315. {
  316. n->pprev = next->pprev;
  317. n->next = next;
  318. smp_wmb();
  319. next->pprev = &n->next;
  320. *(n->pprev) = n;
  321. }
  322. /**
  323. * hlist_add_after_rcu
  324. * @prev: the existing element to add the new element after.
  325. * @n: the new element to add to the hash list.
  326. *
  327. * Description:
  328. * Adds the specified element to the specified hlist
  329. * after the specified node while permitting racing traversals.
  330. *
  331. * The caller must take whatever precautions are necessary
  332. * (such as holding appropriate locks) to avoid racing
  333. * with another list-mutation primitive, such as hlist_add_head_rcu()
  334. * or hlist_del_rcu(), running on this same list.
  335. * However, it is perfectly legal to run concurrently with
  336. * the _rcu list-traversal primitives, such as
  337. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  338. * problems on Alpha CPUs.
  339. */
  340. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  341. struct hlist_node *n)
  342. {
  343. n->next = prev->next;
  344. n->pprev = &prev->next;
  345. smp_wmb();
  346. prev->next = n;
  347. if (n->next)
  348. n->next->pprev = &n->next;
  349. }
  350. /**
  351. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  352. * @tpos: the type * to use as a loop cursor.
  353. * @pos: the &struct hlist_node to use as a loop cursor.
  354. * @head: the head for your list.
  355. * @member: the name of the hlist_node within the struct.
  356. *
  357. * This list-traversal primitive may safely run concurrently with
  358. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  359. * as long as the traversal is guarded by rcu_read_lock().
  360. */
  361. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  362. for (pos = (head)->first; \
  363. rcu_dereference(pos) && ({ prefetch(pos->next); 1; }) && \
  364. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  365. pos = pos->next)
  366. #endif /* __KERNEL__ */
  367. #endif