rculist.h 19 KB

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