rculist.h 19 KB

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