rculist.h 17 KB

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