rculist.h 17 KB

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