rculist.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. * return the ->next pointer of a list_head in an rcu safe
  11. * way, we must not access it directly
  12. */
  13. #define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next)))
  14. /*
  15. * Insert a new entry between two known consecutive entries.
  16. *
  17. * This is only for internal list manipulation where we know
  18. * the prev/next entries already!
  19. */
  20. static inline void __list_add_rcu(struct list_head *new,
  21. struct list_head *prev, struct list_head *next)
  22. {
  23. new->next = next;
  24. new->prev = prev;
  25. rcu_assign_pointer(list_next_rcu(prev), new);
  26. next->prev = new;
  27. }
  28. /**
  29. * list_add_rcu - add a new entry to rcu-protected list
  30. * @new: new entry to be added
  31. * @head: list head to add it after
  32. *
  33. * Insert a new entry after the specified head.
  34. * This is good for implementing stacks.
  35. *
  36. * The caller must take whatever precautions are necessary
  37. * (such as holding appropriate locks) to avoid racing
  38. * with another list-mutation primitive, such as list_add_rcu()
  39. * or list_del_rcu(), running on this same list.
  40. * However, it is perfectly legal to run concurrently with
  41. * the _rcu list-traversal primitives, such as
  42. * list_for_each_entry_rcu().
  43. */
  44. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  45. {
  46. __list_add_rcu(new, head, head->next);
  47. }
  48. /**
  49. * list_add_tail_rcu - add a new entry to rcu-protected list
  50. * @new: new entry to be added
  51. * @head: list head to add it before
  52. *
  53. * Insert a new entry before the specified head.
  54. * This is useful for implementing queues.
  55. *
  56. * The caller must take whatever precautions are necessary
  57. * (such as holding appropriate locks) to avoid racing
  58. * with another list-mutation primitive, such as list_add_tail_rcu()
  59. * or list_del_rcu(), running on this same list.
  60. * However, it is perfectly legal to run concurrently with
  61. * the _rcu list-traversal primitives, such as
  62. * list_for_each_entry_rcu().
  63. */
  64. static inline void list_add_tail_rcu(struct list_head *new,
  65. struct list_head *head)
  66. {
  67. __list_add_rcu(new, head->prev, head);
  68. }
  69. /**
  70. * list_del_rcu - deletes entry from list without re-initialization
  71. * @entry: the element to delete from the list.
  72. *
  73. * Note: list_empty() on entry does not return true after this,
  74. * the entry is in an undefined state. It is useful for RCU based
  75. * lockfree traversal.
  76. *
  77. * In particular, it means that we can not poison the forward
  78. * pointers that may still be used for walking the list.
  79. *
  80. * The caller must take whatever precautions are necessary
  81. * (such as holding appropriate locks) to avoid racing
  82. * with another list-mutation primitive, such as list_del_rcu()
  83. * or list_add_rcu(), running on this same list.
  84. * However, it is perfectly legal to run concurrently with
  85. * the _rcu list-traversal primitives, such as
  86. * list_for_each_entry_rcu().
  87. *
  88. * Note that the caller is not permitted to immediately free
  89. * the newly deleted entry. Instead, either synchronize_rcu()
  90. * or call_rcu() must be used to defer freeing until an RCU
  91. * grace period has elapsed.
  92. */
  93. static inline void list_del_rcu(struct list_head *entry)
  94. {
  95. __list_del(entry->prev, entry->next);
  96. entry->prev = LIST_POISON2;
  97. }
  98. /**
  99. * hlist_del_init_rcu - deletes entry from hash list with re-initialization
  100. * @n: the element to delete from the hash list.
  101. *
  102. * Note: list_unhashed() on the node return true after this. It is
  103. * useful for RCU based read lockfree traversal if the writer side
  104. * must know if the list entry is still hashed or already unhashed.
  105. *
  106. * In particular, it means that we can not poison the forward pointers
  107. * that may still be used for walking the hash list and we can only
  108. * zero the pprev pointer so list_unhashed() will return true after
  109. * this.
  110. *
  111. * The caller must take whatever precautions are necessary (such as
  112. * holding appropriate locks) to avoid racing with another
  113. * list-mutation primitive, such as hlist_add_head_rcu() or
  114. * hlist_del_rcu(), running on this same list. However, it is
  115. * perfectly legal to run concurrently with the _rcu list-traversal
  116. * primitives, such as hlist_for_each_entry_rcu().
  117. */
  118. static inline void hlist_del_init_rcu(struct hlist_node *n)
  119. {
  120. if (!hlist_unhashed(n)) {
  121. __hlist_del(n);
  122. n->pprev = NULL;
  123. }
  124. }
  125. /**
  126. * list_replace_rcu - replace old entry by new one
  127. * @old : the element to be replaced
  128. * @new : the new element to insert
  129. *
  130. * The @old entry will be replaced with the @new entry atomically.
  131. * Note: @old should not be empty.
  132. */
  133. static inline void list_replace_rcu(struct list_head *old,
  134. struct list_head *new)
  135. {
  136. new->next = old->next;
  137. new->prev = old->prev;
  138. rcu_assign_pointer(list_next_rcu(new->prev), new);
  139. new->next->prev = new;
  140. old->prev = LIST_POISON2;
  141. }
  142. /**
  143. * list_splice_init_rcu - splice an RCU-protected list into an existing list.
  144. * @list: the RCU-protected list to splice
  145. * @head: the place in the list to splice the first list into
  146. * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
  147. *
  148. * @head can be RCU-read traversed concurrently with this function.
  149. *
  150. * Note that this function blocks.
  151. *
  152. * Important note: the caller must take whatever action is necessary to
  153. * prevent any other updates to @head. In principle, it is possible
  154. * to modify the list as soon as sync() begins execution.
  155. * If this sort of thing becomes necessary, an alternative version
  156. * based on call_rcu() could be created. But only if -really-
  157. * needed -- there is no shortage of RCU API members.
  158. */
  159. static inline void list_splice_init_rcu(struct list_head *list,
  160. struct list_head *head,
  161. void (*sync)(void))
  162. {
  163. struct list_head *first = list->next;
  164. struct list_head *last = list->prev;
  165. struct list_head *at = head->next;
  166. if (list_empty(head))
  167. return;
  168. /* "first" and "last" tracking list, so initialize it. */
  169. INIT_LIST_HEAD(list);
  170. /*
  171. * At this point, the list body still points to the source list.
  172. * Wait for any readers to finish using the list before splicing
  173. * the list body into the new list. Any new readers will see
  174. * an empty list.
  175. */
  176. sync();
  177. /*
  178. * Readers are finished with the source list, so perform splice.
  179. * The order is important if the new list is global and accessible
  180. * to concurrent RCU readers. Note that RCU readers are not
  181. * permitted to traverse the prev pointers without excluding
  182. * this function.
  183. */
  184. last->next = at;
  185. rcu_assign_pointer(list_next_rcu(head), first);
  186. first->prev = head;
  187. at->prev = last;
  188. }
  189. /**
  190. * list_entry_rcu - get the struct for this entry
  191. * @ptr: the &struct list_head pointer.
  192. * @type: the type of the struct this is embedded in.
  193. * @member: the name of the list_struct within the struct.
  194. *
  195. * This primitive may safely run concurrently with the _rcu list-mutation
  196. * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  197. */
  198. #define list_entry_rcu(ptr, type, member) \
  199. ({typeof (*ptr) __rcu *__ptr = (typeof (*ptr) __rcu __force *)ptr; \
  200. container_of((typeof(ptr))rcu_dereference_raw(__ptr), type, member); \
  201. })
  202. /**
  203. * list_first_entry_rcu - get the first element from a list
  204. * @ptr: the list head to take the element from.
  205. * @type: the type of the struct this is embedded in.
  206. * @member: the name of the list_struct within the struct.
  207. *
  208. * Note, that list is expected to be not empty.
  209. *
  210. * This primitive may safely run concurrently with the _rcu list-mutation
  211. * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  212. */
  213. #define list_first_entry_rcu(ptr, type, member) \
  214. list_entry_rcu((ptr)->next, type, member)
  215. #define __list_for_each_rcu(pos, head) \
  216. for (pos = rcu_dereference_raw(list_next_rcu(head)); \
  217. pos != (head); \
  218. pos = rcu_dereference_raw(list_next_rcu((pos)))
  219. /**
  220. * list_for_each_entry_rcu - iterate over rcu list of given type
  221. * @pos: the type * to use as a loop cursor.
  222. * @head: the head for your list.
  223. * @member: the name of the list_struct within the struct.
  224. *
  225. * This list-traversal primitive may safely run concurrently with
  226. * the _rcu list-mutation primitives such as list_add_rcu()
  227. * as long as the traversal is guarded by rcu_read_lock().
  228. */
  229. #define list_for_each_entry_rcu(pos, head, member) \
  230. for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
  231. prefetch(pos->member.next), &pos->member != (head); \
  232. pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
  233. /**
  234. * list_for_each_continue_rcu
  235. * @pos: the &struct list_head to use as a loop cursor.
  236. * @head: the head for your list.
  237. *
  238. * Iterate over an rcu-protected list, continuing after current point.
  239. *
  240. * This list-traversal primitive may safely run concurrently with
  241. * the _rcu list-mutation primitives such as list_add_rcu()
  242. * as long as the traversal is guarded by rcu_read_lock().
  243. */
  244. #define list_for_each_continue_rcu(pos, head) \
  245. for ((pos) = rcu_dereference_raw(list_next_rcu(pos)); \
  246. prefetch((pos)->next), (pos) != (head); \
  247. (pos) = rcu_dereference_raw(list_next_rcu(pos)))
  248. /**
  249. * list_for_each_entry_continue_rcu - continue iteration over list of given type
  250. * @pos: the type * to use as a loop cursor.
  251. * @head: the head for your list.
  252. * @member: the name of the list_struct within the struct.
  253. *
  254. * Continue to iterate over list of given type, continuing after
  255. * the current position.
  256. */
  257. #define list_for_each_entry_continue_rcu(pos, head, member) \
  258. for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
  259. prefetch(pos->member.next), &pos->member != (head); \
  260. pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
  261. /**
  262. * hlist_del_rcu - deletes entry from hash list without re-initialization
  263. * @n: the element to delete from the hash list.
  264. *
  265. * Note: list_unhashed() on entry does not return true after this,
  266. * the entry is in an undefined state. It is useful for RCU based
  267. * lockfree traversal.
  268. *
  269. * In particular, it means that we can not poison the forward
  270. * pointers that may still be used for walking the hash list.
  271. *
  272. * The caller must take whatever precautions are necessary
  273. * (such as holding appropriate locks) to avoid racing
  274. * with another list-mutation primitive, such as hlist_add_head_rcu()
  275. * or hlist_del_rcu(), running on this same list.
  276. * However, it is perfectly legal to run concurrently with
  277. * the _rcu list-traversal primitives, such as
  278. * hlist_for_each_entry().
  279. */
  280. static inline void hlist_del_rcu(struct hlist_node *n)
  281. {
  282. __hlist_del(n);
  283. n->pprev = LIST_POISON2;
  284. }
  285. /**
  286. * hlist_replace_rcu - replace old entry by new one
  287. * @old : the element to be replaced
  288. * @new : the new element to insert
  289. *
  290. * The @old entry will be replaced with the @new entry atomically.
  291. */
  292. static inline void hlist_replace_rcu(struct hlist_node *old,
  293. struct hlist_node *new)
  294. {
  295. struct hlist_node *next = old->next;
  296. new->next = next;
  297. new->pprev = old->pprev;
  298. rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new);
  299. if (next)
  300. new->next->pprev = &new->next;
  301. old->pprev = LIST_POISON2;
  302. }
  303. /*
  304. * return the first or the next element in an RCU protected hlist
  305. */
  306. #define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first)))
  307. #define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next)))
  308. #define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev)))
  309. /**
  310. * hlist_add_head_rcu
  311. * @n: the element to add to the hash list.
  312. * @h: the list to add to.
  313. *
  314. * Description:
  315. * Adds the specified element to the specified hlist,
  316. * while permitting racing traversals.
  317. *
  318. * The caller must take whatever precautions are necessary
  319. * (such as holding appropriate locks) to avoid racing
  320. * with another list-mutation primitive, such as hlist_add_head_rcu()
  321. * or hlist_del_rcu(), running on this same list.
  322. * However, it is perfectly legal to run concurrently with
  323. * the _rcu list-traversal primitives, such as
  324. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  325. * problems on Alpha CPUs. Regardless of the type of CPU, the
  326. * list-traversal primitive must be guarded by rcu_read_lock().
  327. */
  328. static inline void hlist_add_head_rcu(struct hlist_node *n,
  329. struct hlist_head *h)
  330. {
  331. struct hlist_node *first = h->first;
  332. n->next = first;
  333. n->pprev = &h->first;
  334. rcu_assign_pointer(hlist_first_rcu(h), n);
  335. if (first)
  336. first->pprev = &n->next;
  337. }
  338. /**
  339. * hlist_add_before_rcu
  340. * @n: the new element to add to the hash list.
  341. * @next: the existing element to add the new element before.
  342. *
  343. * Description:
  344. * Adds the specified element to the specified hlist
  345. * before 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_before_rcu(struct hlist_node *n,
  357. struct hlist_node *next)
  358. {
  359. n->pprev = next->pprev;
  360. n->next = next;
  361. rcu_assign_pointer(hlist_pprev_rcu(n), n);
  362. next->pprev = &n->next;
  363. }
  364. /**
  365. * hlist_add_after_rcu
  366. * @prev: the existing element to add the new element after.
  367. * @n: the new element to add to the hash list.
  368. *
  369. * Description:
  370. * Adds the specified element to the specified hlist
  371. * after the specified node while permitting racing traversals.
  372. *
  373. * The caller must take whatever precautions are necessary
  374. * (such as holding appropriate locks) to avoid racing
  375. * with another list-mutation primitive, such as hlist_add_head_rcu()
  376. * or hlist_del_rcu(), running on this same list.
  377. * However, it is perfectly legal to run concurrently with
  378. * the _rcu list-traversal primitives, such as
  379. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  380. * problems on Alpha CPUs.
  381. */
  382. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  383. struct hlist_node *n)
  384. {
  385. n->next = prev->next;
  386. n->pprev = &prev->next;
  387. rcu_assign_pointer(hlist_next_rcu(prev), n);
  388. if (n->next)
  389. n->next->pprev = &n->next;
  390. }
  391. #define __hlist_for_each_rcu(pos, head) \
  392. for (pos = rcu_dereference(hlist_first_rcu(head)); \
  393. pos && ({ prefetch(pos->next); 1; }); \
  394. pos = rcu_dereference(hlist_next_rcu(pos)))
  395. /**
  396. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  397. * @tpos: the type * to use as a loop cursor.
  398. * @pos: the &struct hlist_node to use as a loop cursor.
  399. * @head: the head for your list.
  400. * @member: the name of the hlist_node within the struct.
  401. *
  402. * This list-traversal primitive may safely run concurrently with
  403. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  404. * as long as the traversal is guarded by rcu_read_lock().
  405. */
  406. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  407. for (pos = rcu_dereference_raw(hlist_first_rcu(head)); \
  408. pos && ({ prefetch(pos->next); 1; }) && \
  409. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  410. pos = rcu_dereference_raw(hlist_next_rcu(pos)))
  411. /**
  412. * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
  413. * @tpos: the type * to use as a loop cursor.
  414. * @pos: the &struct hlist_node to use as a loop cursor.
  415. * @head: the head for your list.
  416. * @member: the name of the hlist_node within the struct.
  417. *
  418. * This list-traversal primitive may safely run concurrently with
  419. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  420. * as long as the traversal is guarded by rcu_read_lock().
  421. */
  422. #define hlist_for_each_entry_rcu_bh(tpos, pos, head, member) \
  423. for (pos = rcu_dereference_bh((head)->first); \
  424. pos && ({ prefetch(pos->next); 1; }) && \
  425. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  426. pos = rcu_dereference_bh(pos->next))
  427. /**
  428. * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
  429. * @tpos: the type * to use as a loop cursor.
  430. * @pos: the &struct hlist_node to use as a loop cursor.
  431. * @member: the name of the hlist_node within the struct.
  432. */
  433. #define hlist_for_each_entry_continue_rcu(tpos, pos, member) \
  434. for (pos = rcu_dereference((pos)->next); \
  435. pos && ({ prefetch(pos->next); 1; }) && \
  436. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  437. pos = rcu_dereference(pos->next))
  438. /**
  439. * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
  440. * @tpos: the type * to use as a loop cursor.
  441. * @pos: the &struct hlist_node to use as a loop cursor.
  442. * @member: the name of the hlist_node within the struct.
  443. */
  444. #define hlist_for_each_entry_continue_rcu_bh(tpos, pos, member) \
  445. for (pos = rcu_dereference_bh((pos)->next); \
  446. pos && ({ prefetch(pos->next); 1; }) && \
  447. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  448. pos = rcu_dereference_bh(pos->next))
  449. #endif /* __KERNEL__ */
  450. #endif