rculist.h 17 KB

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