list_bl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. *pprev = (struct hlist_bl_node *)
  79. ((unsigned long)next |
  80. ((unsigned long)*pprev & LIST_BL_LOCKMASK));
  81. if (next)
  82. next->pprev = pprev;
  83. }
  84. static inline void hlist_bl_del(struct hlist_bl_node *n)
  85. {
  86. __hlist_bl_del(n);
  87. n->next = LIST_POISON1;
  88. n->pprev = LIST_POISON2;
  89. }
  90. static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  91. {
  92. if (!hlist_bl_unhashed(n)) {
  93. __hlist_bl_del(n);
  94. INIT_HLIST_BL_NODE(n);
  95. }
  96. }
  97. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  98. {
  99. bit_spin_lock(0, (unsigned long *)b);
  100. }
  101. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  102. {
  103. __bit_spin_unlock(0, (unsigned long *)b);
  104. }
  105. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  106. {
  107. return bit_spin_is_locked(0, (unsigned long *)b);
  108. }
  109. /**
  110. * hlist_bl_for_each_entry - iterate over list of given type
  111. * @tpos: the type * to use as a loop cursor.
  112. * @pos: the &struct hlist_node to use as a loop cursor.
  113. * @head: the head for your list.
  114. * @member: the name of the hlist_node within the struct.
  115. *
  116. */
  117. #define hlist_bl_for_each_entry(tpos, pos, head, member) \
  118. for (pos = hlist_bl_first(head); \
  119. pos && \
  120. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  121. pos = pos->next)
  122. /**
  123. * hlist_bl_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  124. * @tpos: the type * to use as a loop cursor.
  125. * @pos: the &struct hlist_node to use as a loop cursor.
  126. * @n: another &struct hlist_node to use as temporary storage
  127. * @head: the head for your list.
  128. * @member: the name of the hlist_node within the struct.
  129. */
  130. #define hlist_bl_for_each_entry_safe(tpos, pos, n, head, member) \
  131. for (pos = hlist_bl_first(head); \
  132. pos && ({ n = pos->next; 1; }) && \
  133. ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
  134. pos = n)
  135. #endif