rculist.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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_raw(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_raw((head)->next); \
  210. pos != (head); \
  211. pos = rcu_dereference_raw(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_raw((pos)->next); \
  239. prefetch((pos)->next), (pos) != (head); \
  240. (pos) = rcu_dereference_raw((pos)->next))
  241. /**
  242. * list_for_each_entry_continue_rcu - continue iteration over list of given type
  243. * @pos: the type * to use as a loop cursor.
  244. * @head: the head for your list.
  245. * @member: the name of the list_struct within the struct.
  246. *
  247. * Continue to iterate over list of given type, continuing after
  248. * the current position.
  249. */
  250. #define list_for_each_entry_continue_rcu(pos, head, member) \
  251. for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
  252. prefetch(pos->member.next), &pos->member != (head); \
  253. pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
  254. /**
  255. * hlist_del_rcu - deletes entry from hash list without re-initialization
  256. * @n: the element to delete from the hash list.
  257. *
  258. * Note: list_unhashed() on entry does not return true after this,
  259. * the entry is in an undefined state. It is useful for RCU based
  260. * lockfree traversal.
  261. *
  262. * In particular, it means that we can not poison the forward
  263. * pointers that may still be used for walking the hash list.
  264. *
  265. * The caller must take whatever precautions are necessary
  266. * (such as holding appropriate locks) to avoid racing
  267. * with another list-mutation primitive, such as hlist_add_head_rcu()
  268. * or hlist_del_rcu(), running on this same list.
  269. * However, it is perfectly legal to run concurrently with
  270. * the _rcu list-traversal primitives, such as
  271. * hlist_for_each_entry().
  272. */
  273. static inline void hlist_del_rcu(struct hlist_node *n)
  274. {
  275. __hlist_del(n);
  276. n->pprev = LIST_POISON2;
  277. }
  278. /**
  279. * hlist_replace_rcu - replace old entry by new one
  280. * @old : the element to be replaced
  281. * @new : the new element to insert
  282. *
  283. * The @old entry will be replaced with the @new entry atomically.
  284. */
  285. static inline void hlist_replace_rcu(struct hlist_node *old,
  286. struct hlist_node *new)
  287. {
  288. struct hlist_node *next = old->next;
  289. new->next = next;
  290. new->pprev = old->pprev;
  291. rcu_assign_pointer(*new->pprev, new);
  292. if (next)
  293. new->next->pprev = &new->next;
  294. old->pprev = LIST_POISON2;
  295. }
  296. /**
  297. * hlist_add_head_rcu
  298. * @n: the element to add to the hash list.
  299. * @h: the list to add to.
  300. *
  301. * Description:
  302. * Adds the specified element to the specified hlist,
  303. * while permitting racing traversals.
  304. *
  305. * The caller must take whatever precautions are necessary
  306. * (such as holding appropriate locks) to avoid racing
  307. * with another list-mutation primitive, such as hlist_add_head_rcu()
  308. * or hlist_del_rcu(), running on this same list.
  309. * However, it is perfectly legal to run concurrently with
  310. * the _rcu list-traversal primitives, such as
  311. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  312. * problems on Alpha CPUs. Regardless of the type of CPU, the
  313. * list-traversal primitive must be guarded by rcu_read_lock().
  314. */
  315. static inline void hlist_add_head_rcu(struct hlist_node *n,
  316. struct hlist_head *h)
  317. {
  318. struct hlist_node *first = h->first;
  319. n->next = first;
  320. n->pprev = &h->first;
  321. rcu_assign_pointer(h->first, n);
  322. if (first)
  323. first->pprev = &n->next;
  324. }
  325. /**
  326. * hlist_add_before_rcu
  327. * @n: the new element to add to the hash list.
  328. * @next: the existing element to add the new element before.
  329. *
  330. * Description:
  331. * Adds the specified element to the specified hlist
  332. * before the specified node while permitting racing traversals.
  333. *
  334. * The caller must take whatever precautions are necessary
  335. * (such as holding appropriate locks) to avoid racing
  336. * with another list-mutation primitive, such as hlist_add_head_rcu()
  337. * or hlist_del_rcu(), running on this same list.
  338. * However, it is perfectly legal to run concurrently with
  339. * the _rcu list-traversal primitives, such as
  340. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  341. * problems on Alpha CPUs.
  342. */
  343. static inline void hlist_add_before_rcu(struct hlist_node *n,
  344. struct hlist_node *next)
  345. {
  346. n->pprev = next->pprev;
  347. n->next = next;
  348. rcu_assign_pointer(*(n->pprev), n);
  349. next->pprev = &n->next;
  350. }
  351. /**
  352. * hlist_add_after_rcu
  353. * @prev: the existing element to add the new element after.
  354. * @n: the new element to add to the hash list.
  355. *
  356. * Description:
  357. * Adds the specified element to the specified hlist
  358. * after the specified node while permitting racing traversals.
  359. *
  360. * The caller must take whatever precautions are necessary
  361. * (such as holding appropriate locks) to avoid racing
  362. * with another list-mutation primitive, such as hlist_add_head_rcu()
  363. * or hlist_del_rcu(), running on this same list.
  364. * However, it is perfectly legal to run concurrently with
  365. * the _rcu list-traversal primitives, such as
  366. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  367. * problems on Alpha CPUs.
  368. */
  369. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  370. struct hlist_node *n)
  371. {
  372. n->next = prev->next;
  373. n->pprev = &prev->next;
  374. rcu_assign_pointer(prev->next, n);
  375. if (n->next)
  376. n->next->pprev = &n->next;
  377. }
  378. #define __hlist_for_each_rcu(pos, head) \
  379. for (pos = rcu_dereference((head)->first); \
  380. pos && ({ prefetch(pos->next); 1; }); \
  381. pos = rcu_dereference(pos->next))
  382. /**
  383. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  384. * @tpos: the type * to use as a loop cursor.
  385. * @pos: the &struct hlist_node to use as a loop cursor.
  386. * @head: the head for your list.
  387. * @member: the name of the hlist_node within the struct.
  388. *
  389. * This list-traversal primitive may safely run concurrently with
  390. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  391. * as long as the traversal is guarded by rcu_read_lock().
  392. */
  393. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  394. for (pos = rcu_dereference_raw((head)->first); \
  395. pos && ({ prefetch(pos->next); 1; }) && \
  396. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  397. pos = rcu_dereference_raw(pos->next))
  398. /**
  399. * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
  400. * @tpos: the type * to use as a loop cursor.
  401. * @pos: the &struct hlist_node to use as a loop cursor.
  402. * @head: the head for your list.
  403. * @member: the name of the hlist_node within the struct.
  404. *
  405. * This list-traversal primitive may safely run concurrently with
  406. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  407. * as long as the traversal is guarded by rcu_read_lock().
  408. */
  409. #define hlist_for_each_entry_rcu_bh(tpos, pos, head, member) \
  410. for (pos = rcu_dereference_bh((head)->first); \
  411. pos && ({ prefetch(pos->next); 1; }) && \
  412. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  413. pos = rcu_dereference_bh(pos->next))
  414. /**
  415. * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
  416. * @tpos: the type * to use as a loop cursor.
  417. * @pos: the &struct hlist_node to use as a loop cursor.
  418. * @member: the name of the hlist_node within the struct.
  419. */
  420. #define hlist_for_each_entry_continue_rcu(tpos, pos, member) \
  421. for (pos = rcu_dereference((pos)->next); \
  422. pos && ({ prefetch(pos->next); 1; }) && \
  423. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  424. pos = rcu_dereference(pos->next))
  425. /**
  426. * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
  427. * @tpos: the type * to use as a loop cursor.
  428. * @pos: the &struct hlist_node to use as a loop cursor.
  429. * @member: the name of the hlist_node within the struct.
  430. */
  431. #define hlist_for_each_entry_continue_rcu_bh(tpos, pos, member) \
  432. for (pos = rcu_dereference_bh((pos)->next); \
  433. pos && ({ prefetch(pos->next); 1; }) && \
  434. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  435. pos = rcu_dereference_bh(pos->next))
  436. #endif /* __KERNEL__ */
  437. #endif