rculist_bl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_RCULIST_BL_H
  3. #define _LINUX_RCULIST_BL_H
  4. /*
  5. * RCU-protected bl list version. See include/linux/list_bl.h.
  6. */
  7. #include <linux/list_bl.h>
  8. #include <linux/rcupdate.h>
  9. static inline void hlist_bl_set_first_rcu(struct hlist_bl_head *h,
  10. struct hlist_bl_node *n)
  11. {
  12. LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
  13. LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
  14. LIST_BL_LOCKMASK);
  15. rcu_assign_pointer(h->first,
  16. (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK));
  17. }
  18. static inline struct hlist_bl_node *hlist_bl_first_rcu(struct hlist_bl_head *h)
  19. {
  20. return (struct hlist_bl_node *)
  21. ((unsigned long)rcu_dereference_check(h->first, hlist_bl_is_locked(h)) & ~LIST_BL_LOCKMASK);
  22. }
  23. /**
  24. * hlist_bl_del_rcu - deletes entry from hash list without re-initialization
  25. * @n: the element to delete from the hash list.
  26. *
  27. * Note: hlist_bl_unhashed() on entry does not return true after this,
  28. * the entry is in an undefined state. It is useful for RCU based
  29. * lockfree traversal.
  30. *
  31. * In particular, it means that we can not poison the forward
  32. * pointers that may still be used for walking the hash list.
  33. *
  34. * The caller must take whatever precautions are necessary
  35. * (such as holding appropriate locks) to avoid racing
  36. * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
  37. * or hlist_bl_del_rcu(), running on this same list.
  38. * However, it is perfectly legal to run concurrently with
  39. * the _rcu list-traversal primitives, such as
  40. * hlist_bl_for_each_entry().
  41. */
  42. static inline void hlist_bl_del_rcu(struct hlist_bl_node *n)
  43. {
  44. __hlist_bl_del(n);
  45. n->pprev = LIST_POISON2;
  46. }
  47. /**
  48. * hlist_bl_add_head_rcu
  49. * @n: the element to add to the hash list.
  50. * @h: the list to add to.
  51. *
  52. * Description:
  53. * Adds the specified element to the specified hlist_bl,
  54. * while permitting racing traversals.
  55. *
  56. * The caller must take whatever precautions are necessary
  57. * (such as holding appropriate locks) to avoid racing
  58. * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
  59. * or hlist_bl_del_rcu(), running on this same list.
  60. * However, it is perfectly legal to run concurrently with
  61. * the _rcu list-traversal primitives, such as
  62. * hlist_bl_for_each_entry_rcu(), used to prevent memory-consistency
  63. * problems on Alpha CPUs. Regardless of the type of CPU, the
  64. * list-traversal primitive must be guarded by rcu_read_lock().
  65. */
  66. static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,
  67. struct hlist_bl_head *h)
  68. {
  69. struct hlist_bl_node *first;
  70. /* don't need hlist_bl_first_rcu because we're under lock */
  71. first = hlist_bl_first(h);
  72. n->next = first;
  73. if (first)
  74. first->pprev = &n->next;
  75. n->pprev = &h->first;
  76. /* need _rcu because we can have concurrent lock free readers */
  77. hlist_bl_set_first_rcu(h, n);
  78. }
  79. /**
  80. * hlist_bl_for_each_entry_rcu - iterate over rcu list of given type
  81. * @tpos: the type * to use as a loop cursor.
  82. * @pos: the &struct hlist_bl_node to use as a loop cursor.
  83. * @head: the head for your list.
  84. * @member: the name of the hlist_bl_node within the struct.
  85. *
  86. */
  87. #define hlist_bl_for_each_entry_rcu(tpos, pos, head, member) \
  88. for (pos = hlist_bl_first_rcu(head); \
  89. pos && \
  90. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; }); \
  91. pos = rcu_dereference_raw(pos->next))
  92. #endif