Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / list_bl.h
1 #ifndef _LINUX_LIST_BL_H
2 #define _LINUX_LIST_BL_H
3
4 #include <linux/list.h>
5 #include <linux/spinlock.h>
6 #include <linux/bit_spinlock.h>
7
8 /*
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
11  * footprint overhead.
12  *
13  * For modification operations, the 0 bit of hlist_bl_head->first
14  * pointer must be set.
15  *
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.
19  */
20
21 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
22 #define LIST_BL_LOCKMASK        1UL
23 #else
24 #define LIST_BL_LOCKMASK        0UL
25 #endif
26
27 #ifdef CONFIG_DEBUG_LIST
28 #define LIST_BL_BUG_ON(x) BUG_ON(x)
29 #else
30 #define LIST_BL_BUG_ON(x)
31 #endif
32
33
34 struct hlist_bl_head {
35         struct hlist_bl_node *first;
36 #ifdef CONFIG_PREEMPT_RT_BASE
37         raw_spinlock_t lock;
38 #endif
39 };
40
41 struct hlist_bl_node {
42         struct hlist_bl_node *next, **pprev;
43 };
44
45 static inline void INIT_HLIST_BL_HEAD(struct hlist_bl_head *h)
46 {
47         h->first = NULL;
48 #ifdef CONFIG_PREEMPT_RT_BASE
49         raw_spin_lock_init(&h->lock);
50 #endif
51 }
52
53 static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
54 {
55         h->next = NULL;
56         h->pprev = NULL;
57 }
58
59 #define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
60
61 static inline int hlist_bl_unhashed(const struct hlist_bl_node *h)
62 {
63         return !h->pprev;
64 }
65
66 static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
67 {
68         return (struct hlist_bl_node *)
69                 ((unsigned long)h->first & ~LIST_BL_LOCKMASK);
70 }
71
72 static inline void hlist_bl_set_first(struct hlist_bl_head *h,
73                                         struct hlist_bl_node *n)
74 {
75         LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
76         LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
77                                                         LIST_BL_LOCKMASK);
78         h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
79 }
80
81 static inline int hlist_bl_empty(const struct hlist_bl_head *h)
82 {
83         return !((unsigned long)h->first & ~LIST_BL_LOCKMASK);
84 }
85
86 static inline void hlist_bl_add_head(struct hlist_bl_node *n,
87                                         struct hlist_bl_head *h)
88 {
89         struct hlist_bl_node *first = hlist_bl_first(h);
90
91         n->next = first;
92         if (first)
93                 first->pprev = &n->next;
94         n->pprev = &h->first;
95         hlist_bl_set_first(h, n);
96 }
97
98 static inline void __hlist_bl_del(struct hlist_bl_node *n)
99 {
100         struct hlist_bl_node *next = n->next;
101         struct hlist_bl_node **pprev = n->pprev;
102
103         LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
104
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));
109         if (next)
110                 next->pprev = pprev;
111 }
112
113 static inline void hlist_bl_del(struct hlist_bl_node *n)
114 {
115         __hlist_bl_del(n);
116         n->next = LIST_POISON1;
117         n->pprev = LIST_POISON2;
118 }
119
120 static inline void hlist_bl_del_init(struct hlist_bl_node *n)
121 {
122         if (!hlist_bl_unhashed(n)) {
123                 __hlist_bl_del(n);
124                 INIT_HLIST_BL_NODE(n);
125         }
126 }
127
128 static inline void hlist_bl_lock(struct hlist_bl_head *b)
129 {
130 #ifndef CONFIG_PREEMPT_RT_BASE
131         bit_spin_lock(0, (unsigned long *)b);
132 #else
133         raw_spin_lock(&b->lock);
134 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
135         __set_bit(0, (unsigned long *)b);
136 #endif
137 #endif
138 }
139
140 static inline void hlist_bl_unlock(struct hlist_bl_head *b)
141 {
142 #ifndef CONFIG_PREEMPT_RT_BASE
143         __bit_spin_unlock(0, (unsigned long *)b);
144 #else
145 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
146         __clear_bit(0, (unsigned long *)b);
147 #endif
148         raw_spin_unlock(&b->lock);
149 #endif
150 }
151
152 static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
153 {
154         return bit_spin_is_locked(0, (unsigned long *)b);
155 }
156
157 /**
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.
163  *
164  */
165 #define hlist_bl_for_each_entry(tpos, pos, head, member)                \
166         for (pos = hlist_bl_first(head);                                \
167              pos &&                                                     \
168                 ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
169              pos = pos->next)
170
171 /**
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.
178  */
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;}); \
183              pos = n)
184
185 #endif