rculist.h 18 KB

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