rculist.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <linux/rcupdate.h>
  9. /*
  10. * Insert a new entry between two known consecutive entries.
  11. *
  12. * This is only for internal list manipulation where we know
  13. * the prev/next entries already!
  14. */
  15. static inline void __list_add_rcu(struct list_head *new,
  16. struct list_head *prev, struct list_head *next)
  17. {
  18. new->next = next;
  19. new->prev = prev;
  20. rcu_assign_pointer(prev->next, new);
  21. next->prev = 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. * hlist_del_init_rcu - deletes entry from hash list with re-initialization
  95. * @n: the element to delete from the hash list.
  96. *
  97. * Note: list_unhashed() on the node return true after this. It is
  98. * useful for RCU based read lockfree traversal if the writer side
  99. * must know if the list entry is still hashed or already unhashed.
  100. *
  101. * In particular, it means that we can not poison the forward pointers
  102. * that may still be used for walking the hash list and we can only
  103. * zero the pprev pointer so list_unhashed() will return true after
  104. * this.
  105. *
  106. * The caller must take whatever precautions are necessary (such as
  107. * holding appropriate locks) to avoid racing with another
  108. * list-mutation primitive, such as hlist_add_head_rcu() or
  109. * hlist_del_rcu(), running on this same list. However, it is
  110. * perfectly legal to run concurrently with the _rcu list-traversal
  111. * primitives, such as hlist_for_each_entry_rcu().
  112. */
  113. static inline void hlist_del_init_rcu(struct hlist_node *n)
  114. {
  115. if (!hlist_unhashed(n)) {
  116. __hlist_del(n);
  117. n->pprev = NULL;
  118. }
  119. }
  120. /**
  121. * list_replace_rcu - replace old entry by new one
  122. * @old : the element to be replaced
  123. * @new : the new element to insert
  124. *
  125. * The @old entry will be replaced with the @new entry atomically.
  126. * Note: @old should not be empty.
  127. */
  128. static inline void list_replace_rcu(struct list_head *old,
  129. struct list_head *new)
  130. {
  131. new->next = old->next;
  132. new->prev = old->prev;
  133. rcu_assign_pointer(new->prev->next, new);
  134. new->next->prev = new;
  135. old->prev = LIST_POISON2;
  136. }
  137. /**
  138. * list_splice_init_rcu - splice an RCU-protected list into an existing list.
  139. * @list: the RCU-protected list to splice
  140. * @head: the place in the list to splice the first list into
  141. * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
  142. *
  143. * @head can be RCU-read traversed concurrently with this function.
  144. *
  145. * Note that this function blocks.
  146. *
  147. * Important note: the caller must take whatever action is necessary to
  148. * prevent any other updates to @head. In principle, it is possible
  149. * to modify the list as soon as sync() begins execution.
  150. * If this sort of thing becomes necessary, an alternative version
  151. * based on call_rcu() could be created. But only if -really-
  152. * needed -- there is no shortage of RCU API members.
  153. */
  154. static inline void list_splice_init_rcu(struct list_head *list,
  155. struct list_head *head,
  156. void (*sync)(void))
  157. {
  158. struct list_head *first = list->next;
  159. struct list_head *last = list->prev;
  160. struct list_head *at = head->next;
  161. if (list_empty(head))
  162. return;
  163. /* "first" and "last" tracking list, so initialize it. */
  164. INIT_LIST_HEAD(list);
  165. /*
  166. * At this point, the list body still points to the source list.
  167. * Wait for any readers to finish using the list before splicing
  168. * the list body into the new list. Any new readers will see
  169. * an empty list.
  170. */
  171. sync();
  172. /*
  173. * Readers are finished with the source list, so perform splice.
  174. * The order is important if the new list is global and accessible
  175. * to concurrent RCU readers. Note that RCU readers are not
  176. * permitted to traverse the prev pointers without excluding
  177. * this function.
  178. */
  179. last->next = at;
  180. rcu_assign_pointer(head->next, first);
  181. first->prev = head;
  182. at->prev = last;
  183. }
  184. /**
  185. * list_entry_rcu - get the struct for this entry
  186. * @ptr: the &struct list_head pointer.
  187. * @type: the type of the struct this is embedded in.
  188. * @member: the name of the list_struct within the struct.
  189. *
  190. * This primitive may safely run concurrently with the _rcu list-mutation
  191. * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  192. */
  193. #define list_entry_rcu(ptr, type, member) \
  194. container_of(rcu_dereference(ptr), type, member)
  195. /**
  196. * list_first_entry_rcu - get the first element from a list
  197. * @ptr: the list head to take the element from.
  198. * @type: the type of the struct this is embedded in.
  199. * @member: the name of the list_struct within the struct.
  200. *
  201. * Note, that list is expected to be not empty.
  202. *
  203. * This primitive may safely run concurrently with the _rcu list-mutation
  204. * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  205. */
  206. #define list_first_entry_rcu(ptr, type, member) \
  207. list_entry_rcu((ptr)->next, type, member)
  208. #define __list_for_each_rcu(pos, head) \
  209. for (pos = rcu_dereference((head)->next); \
  210. pos != (head); \
  211. pos = rcu_dereference(pos->next))
  212. /**
  213. * list_for_each_entry_rcu - iterate over rcu list of given type
  214. * @pos: the type * to use as a loop cursor.
  215. * @head: the head for your list.
  216. * @member: the name of the list_struct within the struct.
  217. *
  218. * This list-traversal primitive may safely run concurrently with
  219. * the _rcu list-mutation primitives such as list_add_rcu()
  220. * as long as the traversal is guarded by rcu_read_lock().
  221. */
  222. #define list_for_each_entry_rcu(pos, head, member) \
  223. for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
  224. prefetch(pos->member.next), &pos->member != (head); \
  225. pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
  226. /**
  227. * list_for_each_continue_rcu
  228. * @pos: the &struct list_head to use as a loop cursor.
  229. * @head: the head for your list.
  230. *
  231. * Iterate over an rcu-protected list, continuing after current point.
  232. *
  233. * This list-traversal primitive may safely run concurrently with
  234. * the _rcu list-mutation primitives such as list_add_rcu()
  235. * as long as the traversal is guarded by rcu_read_lock().
  236. */
  237. #define list_for_each_continue_rcu(pos, head) \
  238. for ((pos) = rcu_dereference((pos)->next); \
  239. prefetch((pos)->next), (pos) != (head); \
  240. (pos) = rcu_dereference((pos)->next))
  241. /**
  242. * hlist_del_rcu - deletes entry from hash list without re-initialization
  243. * @n: the element to delete from the hash list.
  244. *
  245. * Note: list_unhashed() on entry does not return true after this,
  246. * the entry is in an undefined state. It is useful for RCU based
  247. * lockfree traversal.
  248. *
  249. * In particular, it means that we can not poison the forward
  250. * pointers that may still be used for walking the hash list.
  251. *
  252. * The caller must take whatever precautions are necessary
  253. * (such as holding appropriate locks) to avoid racing
  254. * with another list-mutation primitive, such as hlist_add_head_rcu()
  255. * or hlist_del_rcu(), running on this same list.
  256. * However, it is perfectly legal to run concurrently with
  257. * the _rcu list-traversal primitives, such as
  258. * hlist_for_each_entry().
  259. */
  260. static inline void hlist_del_rcu(struct hlist_node *n)
  261. {
  262. __hlist_del(n);
  263. n->pprev = LIST_POISON2;
  264. }
  265. /**
  266. * hlist_replace_rcu - replace old entry by new one
  267. * @old : the element to be replaced
  268. * @new : the new element to insert
  269. *
  270. * The @old entry will be replaced with the @new entry atomically.
  271. */
  272. static inline void hlist_replace_rcu(struct hlist_node *old,
  273. struct hlist_node *new)
  274. {
  275. struct hlist_node *next = old->next;
  276. new->next = next;
  277. new->pprev = old->pprev;
  278. rcu_assign_pointer(*new->pprev, new);
  279. if (next)
  280. new->next->pprev = &new->next;
  281. old->pprev = LIST_POISON2;
  282. }
  283. /**
  284. * hlist_add_head_rcu
  285. * @n: the element to add to the hash list.
  286. * @h: the list to add to.
  287. *
  288. * Description:
  289. * Adds the specified element to the specified hlist,
  290. * while permitting racing traversals.
  291. *
  292. * The caller must take whatever precautions are necessary
  293. * (such as holding appropriate locks) to avoid racing
  294. * with another list-mutation primitive, such as hlist_add_head_rcu()
  295. * or hlist_del_rcu(), running on this same list.
  296. * However, it is perfectly legal to run concurrently with
  297. * the _rcu list-traversal primitives, such as
  298. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  299. * problems on Alpha CPUs. Regardless of the type of CPU, the
  300. * list-traversal primitive must be guarded by rcu_read_lock().
  301. */
  302. static inline void hlist_add_head_rcu(struct hlist_node *n,
  303. struct hlist_head *h)
  304. {
  305. struct hlist_node *first = h->first;
  306. n->next = first;
  307. n->pprev = &h->first;
  308. rcu_assign_pointer(h->first, n);
  309. if (first)
  310. first->pprev = &n->next;
  311. }
  312. /**
  313. * hlist_add_before_rcu
  314. * @n: the new element to add to the hash list.
  315. * @next: the existing element to add the new element before.
  316. *
  317. * Description:
  318. * Adds the specified element to the specified hlist
  319. * before the specified node while permitting racing traversals.
  320. *
  321. * The caller must take whatever precautions are necessary
  322. * (such as holding appropriate locks) to avoid racing
  323. * with another list-mutation primitive, such as hlist_add_head_rcu()
  324. * or hlist_del_rcu(), running on this same list.
  325. * However, it is perfectly legal to run concurrently with
  326. * the _rcu list-traversal primitives, such as
  327. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  328. * problems on Alpha CPUs.
  329. */
  330. static inline void hlist_add_before_rcu(struct hlist_node *n,
  331. struct hlist_node *next)
  332. {
  333. n->pprev = next->pprev;
  334. n->next = next;
  335. rcu_assign_pointer(*(n->pprev), n);
  336. next->pprev = &n->next;
  337. }
  338. /**
  339. * hlist_add_after_rcu
  340. * @prev: the existing element to add the new element after.
  341. * @n: the new element to add to the hash list.
  342. *
  343. * Description:
  344. * Adds the specified element to the specified hlist
  345. * after the specified node while permitting racing traversals.
  346. *
  347. * The caller must take whatever precautions are necessary
  348. * (such as holding appropriate locks) to avoid racing
  349. * with another list-mutation primitive, such as hlist_add_head_rcu()
  350. * or hlist_del_rcu(), running on this same list.
  351. * However, it is perfectly legal to run concurrently with
  352. * the _rcu list-traversal primitives, such as
  353. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  354. * problems on Alpha CPUs.
  355. */
  356. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  357. struct hlist_node *n)
  358. {
  359. n->next = prev->next;
  360. n->pprev = &prev->next;
  361. rcu_assign_pointer(prev->next, n);
  362. if (n->next)
  363. n->next->pprev = &n->next;
  364. }
  365. /**
  366. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  367. * @tpos: the type * to use as a loop cursor.
  368. * @pos: the &struct hlist_node to use as a loop cursor.
  369. * @head: the head for your list.
  370. * @member: the name of the hlist_node within the struct.
  371. *
  372. * This list-traversal primitive may safely run concurrently with
  373. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  374. * as long as the traversal is guarded by rcu_read_lock().
  375. */
  376. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  377. for (pos = rcu_dereference((head)->first); \
  378. pos && ({ prefetch(pos->next); 1; }) && \
  379. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  380. pos = rcu_dereference(pos->next))
  381. #endif /* __KERNEL__ */
  382. #endif