rculist_nulls.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. n->pprev = NULL;
  35. }
  36. }
  37. #define hlist_nulls_first_rcu(head) \
  38. (*((struct hlist_nulls_node __rcu __force **)&(head)->first))
  39. #define hlist_nulls_next_rcu(node) \
  40. (*((struct hlist_nulls_node __rcu __force **)&(node)->next))
  41. /**
  42. * hlist_nulls_del_rcu - deletes entry from hash list without re-initialization
  43. * @n: the element to delete from the hash list.
  44. *
  45. * Note: hlist_nulls_unhashed() on entry does not return true after this,
  46. * the entry is in an undefined state. It is useful for RCU based
  47. * lockfree traversal.
  48. *
  49. * In particular, it means that we can not poison the forward
  50. * pointers that may still be used for walking the hash list.
  51. *
  52. * The caller must take whatever precautions are necessary
  53. * (such as holding appropriate locks) to avoid racing
  54. * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
  55. * or hlist_nulls_del_rcu(), running on this same list.
  56. * However, it is perfectly legal to run concurrently with
  57. * the _rcu list-traversal primitives, such as
  58. * hlist_nulls_for_each_entry().
  59. */
  60. static inline void hlist_nulls_del_rcu(struct hlist_nulls_node *n)
  61. {
  62. __hlist_nulls_del(n);
  63. n->pprev = LIST_POISON2;
  64. }
  65. /**
  66. * hlist_nulls_add_head_rcu
  67. * @n: the element to add to the hash list.
  68. * @h: the list to add to.
  69. *
  70. * Description:
  71. * Adds the specified element to the specified hlist_nulls,
  72. * while permitting racing traversals.
  73. *
  74. * The caller must take whatever precautions are necessary
  75. * (such as holding appropriate locks) to avoid racing
  76. * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
  77. * or hlist_nulls_del_rcu(), running on this same list.
  78. * However, it is perfectly legal to run concurrently with
  79. * the _rcu list-traversal primitives, such as
  80. * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
  81. * problems on Alpha CPUs. Regardless of the type of CPU, the
  82. * list-traversal primitive must be guarded by rcu_read_lock().
  83. */
  84. static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
  85. struct hlist_nulls_head *h)
  86. {
  87. struct hlist_nulls_node *first = h->first;
  88. n->next = first;
  89. n->pprev = &h->first;
  90. rcu_assign_pointer(hlist_nulls_first_rcu(h), n);
  91. if (!is_a_nulls(first))
  92. first->pprev = &n->next;
  93. }
  94. /**
  95. * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
  96. * @tpos: the type * to use as a loop cursor.
  97. * @pos: the &struct hlist_nulls_node to use as a loop cursor.
  98. * @head: the head for your list.
  99. * @member: the name of the hlist_nulls_node within the struct.
  100. *
  101. * The barrier() is needed to make sure compiler doesn't cache first element [1],
  102. * as this loop can be restarted [2]
  103. * [1] Documentation/atomic_ops.txt around line 114
  104. * [2] Documentation/RCU/rculist_nulls.txt around line 146
  105. */
  106. #define hlist_nulls_for_each_entry_rcu(tpos, pos, head, member) \
  107. for (({barrier();}), \
  108. pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
  109. (!is_a_nulls(pos)) && \
  110. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1; }); \
  111. pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)))
  112. /**
  113. * hlist_nulls_for_each_entry_safe -
  114. * iterate over list of given type safe against removal of list entry
  115. * @tpos: the type * to use as a loop cursor.
  116. * @pos: the &struct hlist_nulls_node to use as a loop cursor.
  117. * @head: the head for your list.
  118. * @member: the name of the hlist_nulls_node within the struct.
  119. */
  120. #define hlist_nulls_for_each_entry_safe(tpos, pos, head, member) \
  121. for (({barrier();}), \
  122. pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
  123. (!is_a_nulls(pos)) && \
  124. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); \
  125. pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)); 1; });)
  126. #endif
  127. #endif