rculist_nulls.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_RCULIST_NULLS_H
  3. #define _LINUX_RCULIST_NULLS_H
  4. #ifdef __KERNEL__
  5. /*
  6. * RCU-protected list version
  7. */
  8. #include <linux/list_nulls.h>
  9. #include <linux/rcupdate.h>
  10. /**
  11. * hlist_nulls_del_init_rcu - deletes entry from hash list with re-initialization
  12. * @n: the element to delete from the hash list.
  13. *
  14. * Note: hlist_nulls_unhashed() on the node return true after this. It is
  15. * useful for RCU based read lockfree traversal if the writer side
  16. * must know if the list entry is still hashed or already unhashed.
  17. *
  18. * In particular, it means that we can not poison the forward pointers
  19. * that may still be used for walking the hash list and we can only
  20. * zero the pprev pointer so list_unhashed() will return true after
  21. * this.
  22. *
  23. * The caller must take whatever precautions are necessary (such as
  24. * holding appropriate locks) to avoid racing with another
  25. * list-mutation primitive, such as hlist_nulls_add_head_rcu() or
  26. * hlist_nulls_del_rcu(), running on this same list. However, it is
  27. * perfectly legal to run concurrently with the _rcu list-traversal
  28. * primitives, such as hlist_nulls_for_each_entry_rcu().
  29. */
  30. static inline void hlist_nulls_del_init_rcu(struct hlist_nulls_node *n)
  31. {
  32. if (!hlist_nulls_unhashed(n)) {
  33. __hlist_nulls_del(n);
  34. WRITE_ONCE(n->pprev, NULL);
  35. }
  36. }
  37. /**
  38. * hlist_nulls_first_rcu - returns the first element of the hash list.
  39. * @head: the head of the list.
  40. */
  41. #define hlist_nulls_first_rcu(head) \
  42. (*((struct hlist_nulls_node __rcu __force **)&(head)->first))
  43. /**
  44. * hlist_nulls_next_rcu - returns the element of the list after @node.
  45. * @node: element of the list.
  46. */
  47. #define hlist_nulls_next_rcu(node) \
  48. (*((struct hlist_nulls_node __rcu __force **)&(node)->next))
  49. /**
  50. * hlist_nulls_del_rcu - deletes entry from hash list without re-initialization
  51. * @n: the element to delete from the hash list.
  52. *
  53. * Note: hlist_nulls_unhashed() on entry does not return true after this,
  54. * the entry is in an undefined state. It is useful for RCU based
  55. * lockfree traversal.
  56. *
  57. * In particular, it means that we can not poison the forward
  58. * pointers that may still be used for walking the hash list.
  59. *
  60. * The caller must take whatever precautions are necessary
  61. * (such as holding appropriate locks) to avoid racing
  62. * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
  63. * or hlist_nulls_del_rcu(), running on this same list.
  64. * However, it is perfectly legal to run concurrently with
  65. * the _rcu list-traversal primitives, such as
  66. * hlist_nulls_for_each_entry().
  67. */
  68. static inline void hlist_nulls_del_rcu(struct hlist_nulls_node *n)
  69. {
  70. __hlist_nulls_del(n);
  71. WRITE_ONCE(n->pprev, LIST_POISON2);
  72. }
  73. /**
  74. * hlist_nulls_add_head_rcu
  75. * @n: the element to add to the hash list.
  76. * @h: the list to add to.
  77. *
  78. * Description:
  79. * Adds the specified element to the specified hlist_nulls,
  80. * while permitting racing traversals.
  81. *
  82. * The caller must take whatever precautions are necessary
  83. * (such as holding appropriate locks) to avoid racing
  84. * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
  85. * or hlist_nulls_del_rcu(), running on this same list.
  86. * However, it is perfectly legal to run concurrently with
  87. * the _rcu list-traversal primitives, such as
  88. * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
  89. * problems on Alpha CPUs. Regardless of the type of CPU, the
  90. * list-traversal primitive must be guarded by rcu_read_lock().
  91. */
  92. static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
  93. struct hlist_nulls_head *h)
  94. {
  95. struct hlist_nulls_node *first = h->first;
  96. n->next = first;
  97. WRITE_ONCE(n->pprev, &h->first);
  98. rcu_assign_pointer(hlist_nulls_first_rcu(h), n);
  99. if (!is_a_nulls(first))
  100. WRITE_ONCE(first->pprev, &n->next);
  101. }
  102. /**
  103. * hlist_nulls_add_tail_rcu
  104. * @n: the element to add to the hash list.
  105. * @h: the list to add to.
  106. *
  107. * Description:
  108. * Adds the specified element to the specified hlist_nulls,
  109. * while permitting racing traversals.
  110. *
  111. * The caller must take whatever precautions are necessary
  112. * (such as holding appropriate locks) to avoid racing
  113. * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
  114. * or hlist_nulls_del_rcu(), running on this same list.
  115. * However, it is perfectly legal to run concurrently with
  116. * the _rcu list-traversal primitives, such as
  117. * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
  118. * problems on Alpha CPUs. Regardless of the type of CPU, the
  119. * list-traversal primitive must be guarded by rcu_read_lock().
  120. */
  121. static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
  122. struct hlist_nulls_head *h)
  123. {
  124. struct hlist_nulls_node *i, *last = NULL;
  125. /* Note: write side code, so rcu accessors are not needed. */
  126. for (i = h->first; !is_a_nulls(i); i = i->next)
  127. last = i;
  128. if (last) {
  129. n->next = last->next;
  130. n->pprev = &last->next;
  131. rcu_assign_pointer(hlist_next_rcu(last), n);
  132. } else {
  133. hlist_nulls_add_head_rcu(n, h);
  134. }
  135. }
  136. /* after that hlist_nulls_del will work */
  137. static inline void hlist_nulls_add_fake(struct hlist_nulls_node *n)
  138. {
  139. n->pprev = &n->next;
  140. n->next = (struct hlist_nulls_node *)NULLS_MARKER(NULL);
  141. }
  142. /**
  143. * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
  144. * @tpos: the type * to use as a loop cursor.
  145. * @pos: the &struct hlist_nulls_node to use as a loop cursor.
  146. * @head: the head of the list.
  147. * @member: the name of the hlist_nulls_node within the struct.
  148. *
  149. * The barrier() is needed to make sure compiler doesn't cache first element [1],
  150. * as this loop can be restarted [2]
  151. * [1] Documentation/memory-barriers.txt around line 1533
  152. * [2] Documentation/RCU/rculist_nulls.rst around line 146
  153. */
  154. #define hlist_nulls_for_each_entry_rcu(tpos, pos, head, member) \
  155. for (({barrier();}), \
  156. pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
  157. (!is_a_nulls(pos)) && \
  158. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1; }); \
  159. pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)))
  160. /**
  161. * hlist_nulls_for_each_entry_safe -
  162. * iterate over list of given type safe against removal of list entry
  163. * @tpos: the type * to use as a loop cursor.
  164. * @pos: the &struct hlist_nulls_node to use as a loop cursor.
  165. * @head: the head of the list.
  166. * @member: the name of the hlist_nulls_node within the struct.
  167. */
  168. #define hlist_nulls_for_each_entry_safe(tpos, pos, head, member) \
  169. for (({barrier();}), \
  170. pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
  171. (!is_a_nulls(pos)) && \
  172. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); \
  173. pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)); 1; });)
  174. #endif
  175. #endif