1 #ifndef _LINUX_LIST_BL_H
2 #define _LINUX_LIST_BL_H
4 #include <linux/list.h>
5 #include <linux/spinlock.h>
6 #include <linux/bit_spinlock.h>
9 * Special version of lists, where head of the list has a lock in the lowest
10 * bit. This is useful for scalable hash tables without increasing memory
13 * For modification operations, the 0 bit of hlist_bl_head->first
14 * pointer must be set.
16 * With some small modifications, this can easily be adapted to store several
17 * arbitrary bits (not just a single lock bit), if the need arises to store
18 * some fast and compact auxiliary data.
21 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
22 #define LIST_BL_LOCKMASK 1UL
24 #define LIST_BL_LOCKMASK 0UL
27 #ifdef CONFIG_DEBUG_LIST
28 #define LIST_BL_BUG_ON(x) BUG_ON(x)
30 #define LIST_BL_BUG_ON(x)
34 struct hlist_bl_head {
35 struct hlist_bl_node *first;
36 #ifdef CONFIG_PREEMPT_RT_BASE
41 struct hlist_bl_node {
42 struct hlist_bl_node *next, **pprev;
45 static inline void INIT_HLIST_BL_HEAD(struct hlist_bl_head *h)
48 #ifdef CONFIG_PREEMPT_RT_BASE
49 raw_spin_lock_init(&h->lock);
53 static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
59 #define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
61 static inline int hlist_bl_unhashed(const struct hlist_bl_node *h)
66 static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
68 return (struct hlist_bl_node *)
69 ((unsigned long)h->first & ~LIST_BL_LOCKMASK);
72 static inline void hlist_bl_set_first(struct hlist_bl_head *h,
73 struct hlist_bl_node *n)
75 LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
76 LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
78 h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
81 static inline int hlist_bl_empty(const struct hlist_bl_head *h)
83 return !((unsigned long)h->first & ~LIST_BL_LOCKMASK);
86 static inline void hlist_bl_add_head(struct hlist_bl_node *n,
87 struct hlist_bl_head *h)
89 struct hlist_bl_node *first = hlist_bl_first(h);
93 first->pprev = &n->next;
95 hlist_bl_set_first(h, n);
98 static inline void __hlist_bl_del(struct hlist_bl_node *n)
100 struct hlist_bl_node *next = n->next;
101 struct hlist_bl_node **pprev = n->pprev;
103 LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
105 /* pprev may be `first`, so be careful not to lose the lock bit */
106 *pprev = (struct hlist_bl_node *)
107 ((unsigned long)next |
108 ((unsigned long)*pprev & LIST_BL_LOCKMASK));
113 static inline void hlist_bl_del(struct hlist_bl_node *n)
116 n->next = LIST_POISON1;
117 n->pprev = LIST_POISON2;
120 static inline void hlist_bl_del_init(struct hlist_bl_node *n)
122 if (!hlist_bl_unhashed(n)) {
124 INIT_HLIST_BL_NODE(n);
128 static inline void hlist_bl_lock(struct hlist_bl_head *b)
130 #ifndef CONFIG_PREEMPT_RT_BASE
131 bit_spin_lock(0, (unsigned long *)b);
133 raw_spin_lock(&b->lock);
134 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
135 __set_bit(0, (unsigned long *)b);
140 static inline void hlist_bl_unlock(struct hlist_bl_head *b)
142 #ifndef CONFIG_PREEMPT_RT_BASE
143 __bit_spin_unlock(0, (unsigned long *)b);
145 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
146 __clear_bit(0, (unsigned long *)b);
148 raw_spin_unlock(&b->lock);
152 static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
154 return bit_spin_is_locked(0, (unsigned long *)b);
158 * hlist_bl_for_each_entry - iterate over list of given type
159 * @tpos: the type * to use as a loop cursor.
160 * @pos: the &struct hlist_node to use as a loop cursor.
161 * @head: the head for your list.
162 * @member: the name of the hlist_node within the struct.
165 #define hlist_bl_for_each_entry(tpos, pos, head, member) \
166 for (pos = hlist_bl_first(head); \
168 ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
172 * hlist_bl_for_each_entry_safe - iterate over list of given type safe against removal of list entry
173 * @tpos: the type * to use as a loop cursor.
174 * @pos: the &struct hlist_node to use as a loop cursor.
175 * @n: another &struct hlist_node to use as temporary storage
176 * @head: the head for your list.
177 * @member: the name of the hlist_node within the struct.
179 #define hlist_bl_for_each_entry_safe(tpos, pos, n, head, member) \
180 for (pos = hlist_bl_first(head); \
181 pos && ({ n = pos->next; 1; }) && \
182 ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \