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