list_bl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _LINUX_LIST_BL_H
  2. #define _LINUX_LIST_BL_H
  3. #include <linux/list.h>
  4. #include <linux/bit_spinlock.h>
  5. /*
  6. * Special version of lists, where head of the list has a lock in the lowest
  7. * bit. This is useful for scalable hash tables without increasing memory
  8. * footprint overhead.
  9. *
  10. * For modification operations, the 0 bit of hlist_bl_head->first
  11. * pointer must be set.
  12. *
  13. * With some small modifications, this can easily be adapted to store several
  14. * arbitrary bits (not just a single lock bit), if the need arises to store
  15. * some fast and compact auxiliary data.
  16. */
  17. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  18. #define LIST_BL_LOCKMASK 1UL
  19. #else
  20. #define LIST_BL_LOCKMASK 0UL
  21. #endif
  22. #ifdef CONFIG_DEBUG_LIST
  23. #define LIST_BL_BUG_ON(x) BUG_ON(x)
  24. #else
  25. #define LIST_BL_BUG_ON(x)
  26. #endif
  27. struct hlist_bl_head {
  28. struct hlist_bl_node *first;
  29. };
  30. struct hlist_bl_node {
  31. struct hlist_bl_node *next, **pprev;
  32. };
  33. #define INIT_HLIST_BL_HEAD(ptr) \
  34. ((ptr)->first = NULL)
  35. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  36. {
  37. h->next = NULL;
  38. h->pprev = NULL;
  39. }
  40. #define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
  41. static inline int hlist_bl_unhashed(const struct hlist_bl_node *h)
  42. {
  43. return !h->pprev;
  44. }
  45. static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
  46. {
  47. return (struct hlist_bl_node *)
  48. ((unsigned long)h->first & ~LIST_BL_LOCKMASK);
  49. }
  50. static inline void hlist_bl_set_first(struct hlist_bl_head *h,
  51. struct hlist_bl_node *n)
  52. {
  53. LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
  54. LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
  55. LIST_BL_LOCKMASK);
  56. h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
  57. }
  58. static inline int hlist_bl_empty(const struct hlist_bl_head *h)
  59. {
  60. return !((unsigned long)h->first & ~LIST_BL_LOCKMASK);
  61. }
  62. static inline void hlist_bl_add_head(struct hlist_bl_node *n,
  63. struct hlist_bl_head *h)
  64. {
  65. struct hlist_bl_node *first = hlist_bl_first(h);
  66. n->next = first;
  67. if (first)
  68. first->pprev = &n->next;
  69. n->pprev = &h->first;
  70. hlist_bl_set_first(h, n);
  71. }
  72. static inline void __hlist_bl_del(struct hlist_bl_node *n)
  73. {
  74. struct hlist_bl_node *next = n->next;
  75. struct hlist_bl_node **pprev = n->pprev;
  76. LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
  77. /* pprev may be `first`, so be careful not to lose the lock bit */
  78. WRITE_ONCE(*pprev,
  79. (struct hlist_bl_node *)
  80. ((unsigned long)next |
  81. ((unsigned long)*pprev & LIST_BL_LOCKMASK)));
  82. if (next)
  83. next->pprev = pprev;
  84. }
  85. static inline void hlist_bl_del(struct hlist_bl_node *n)
  86. {
  87. __hlist_bl_del(n);
  88. n->next = LIST_POISON1;
  89. n->pprev = LIST_POISON2;
  90. }
  91. static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  92. {
  93. if (!hlist_bl_unhashed(n)) {
  94. __hlist_bl_del(n);
  95. INIT_HLIST_BL_NODE(n);
  96. }
  97. }
  98. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  99. {
  100. bit_spin_lock(0, (unsigned long *)b);
  101. }
  102. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  103. {
  104. __bit_spin_unlock(0, (unsigned long *)b);
  105. }
  106. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  107. {
  108. return bit_spin_is_locked(0, (unsigned long *)b);
  109. }
  110. /**
  111. * hlist_bl_for_each_entry - iterate over list of given type
  112. * @tpos: the type * to use as a loop cursor.
  113. * @pos: the &struct hlist_node to use as a loop cursor.
  114. * @head: the head for your list.
  115. * @member: the name of the hlist_node within the struct.
  116. *
  117. */
  118. #define hlist_bl_for_each_entry(tpos, pos, head, member) \
  119. for (pos = hlist_bl_first(head); \
  120. pos && \
  121. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  122. pos = pos->next)
  123. /**
  124. * hlist_bl_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  125. * @tpos: the type * to use as a loop cursor.
  126. * @pos: the &struct hlist_node to use as a loop cursor.
  127. * @n: another &struct hlist_node to use as temporary storage
  128. * @head: the head for your list.
  129. * @member: the name of the hlist_node within the struct.
  130. */
  131. #define hlist_bl_for_each_entry_safe(tpos, pos, n, head, member) \
  132. for (pos = hlist_bl_first(head); \
  133. pos && ({ n = pos->next; 1; }) && \
  134. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  135. pos = n)
  136. #endif