blk-mq: abstract tag allocation out into sbitmap library
[linux-2.6-block.git] / include / linux / sbitmap.h
1 /*
2  * Fast and scalable bitmaps.
3  *
4  * Copyright (C) 2016 Facebook
5  * Copyright (C) 2013-2014 Jens Axboe
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License v2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #ifndef __LINUX_SCALE_BITMAP_H
21 #define __LINUX_SCALE_BITMAP_H
22
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25
26 /**
27  * struct sbitmap_word - Word in a &struct sbitmap.
28  */
29 struct sbitmap_word {
30         /**
31          * @word: The bitmap word itself.
32          */
33         unsigned long word;
34
35         /**
36          * @depth: Number of bits being used in @word.
37          */
38         unsigned long depth;
39 } ____cacheline_aligned_in_smp;
40
41 /**
42  * struct sbitmap - Scalable bitmap.
43  *
44  * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
45  * trades off higher memory usage for better scalability.
46  */
47 struct sbitmap {
48         /**
49          * @depth: Number of bits used in the whole bitmap.
50          */
51         unsigned int depth;
52
53         /**
54          * @shift: log2(number of bits used per word)
55          */
56         unsigned int shift;
57
58         /**
59          * @map_nr: Number of words (cachelines) being used for the bitmap.
60          */
61         unsigned int map_nr;
62
63         /**
64          * @map: Allocated bitmap.
65          */
66         struct sbitmap_word *map;
67 };
68
69 #define SBQ_WAIT_QUEUES 8
70 #define SBQ_WAKE_BATCH 8
71
72 /**
73  * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
74  */
75 struct sbq_wait_state {
76         /**
77          * @wait_cnt: Number of frees remaining before we wake up.
78          */
79         atomic_t wait_cnt;
80
81         /**
82          * @wait: Wait queue.
83          */
84         wait_queue_head_t wait;
85 } ____cacheline_aligned_in_smp;
86
87 /**
88  * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
89  * bits.
90  *
91  * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to
92  * avoid contention on the wait queue spinlock. This ensures that we don't hit a
93  * scalability wall when we run out of free bits and have to start putting tasks
94  * to sleep.
95  */
96 struct sbitmap_queue {
97         /**
98          * @sb: Scalable bitmap.
99          */
100         struct sbitmap sb;
101
102         /**
103          * @wake_batch: Number of bits which must be freed before we wake up any
104          * waiters.
105          */
106         unsigned int wake_batch;
107
108         /**
109          * @wake_index: Next wait queue in @ws to wake up.
110          */
111         atomic_t wake_index;
112
113         /**
114          * @ws: Wait queues.
115          */
116         struct sbq_wait_state *ws;
117 };
118
119 /**
120  * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
121  * @sb: Bitmap to initialize.
122  * @depth: Number of bits to allocate.
123  * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
124  *         given, a good default is chosen.
125  * @flags: Allocation flags.
126  * @node: Memory node to allocate on.
127  *
128  * Return: Zero on success or negative errno on failure.
129  */
130 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
131                       gfp_t flags, int node);
132
133 /**
134  * sbitmap_free() - Free memory used by a &struct sbitmap.
135  * @sb: Bitmap to free.
136  */
137 static inline void sbitmap_free(struct sbitmap *sb)
138 {
139         kfree(sb->map);
140         sb->map = NULL;
141 }
142
143 /**
144  * sbitmap_resize() - Resize a &struct sbitmap.
145  * @sb: Bitmap to resize.
146  * @depth: New number of bits to resize to.
147  *
148  * Doesn't reallocate anything. It's up to the caller to ensure that the new
149  * depth doesn't exceed the depth that the sb was initialized with.
150  */
151 void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
152
153 /**
154  * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
155  * @sb: Bitmap to allocate from.
156  * @alloc_hint: Hint for where to start searching for a free bit.
157  * @round_robin: If true, be stricter about allocation order; always allocate
158  *               starting from the last allocated bit. This is less efficient
159  *               than the default behavior (false).
160  *
161  * Return: Non-negative allocated bit number if successful, -1 otherwise.
162  */
163 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
164
165 /**
166  * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
167  * @sb: Bitmap to check.
168  *
169  * Return: true if any bit in the bitmap is set, false otherwise.
170  */
171 bool sbitmap_any_bit_set(const struct sbitmap *sb);
172
173 /**
174  * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
175  * sbitmap.
176  * @sb: Bitmap to check.
177  *
178  * Return: true if any bit in the bitmap is clear, false otherwise.
179  */
180 bool sbitmap_any_bit_clear(const struct sbitmap *sb);
181
182 typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
183
184 /**
185  * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
186  * @sb: Bitmap to iterate over.
187  * @fn: Callback. Should return true to continue or false to break early.
188  * @data: Pointer to pass to callback.
189  *
190  * This is inline even though it's non-trivial so that the function calls to the
191  * callback will hopefully get optimized away.
192  */
193 static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
194                                         void *data)
195 {
196         unsigned int i;
197
198         for (i = 0; i < sb->map_nr; i++) {
199                 struct sbitmap_word *word = &sb->map[i];
200                 unsigned int off, nr;
201
202                 if (!word->word)
203                         continue;
204
205                 nr = 0;
206                 off = i << sb->shift;
207                 while (1) {
208                         nr = find_next_bit(&word->word, word->depth, nr);
209                         if (nr >= word->depth)
210                                 break;
211
212                         if (!fn(sb, off + nr, data))
213                                 return;
214
215                         nr++;
216                 }
217         }
218 }
219
220 #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
221 #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
222
223 static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
224                                             unsigned int bitnr)
225 {
226         return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
227 }
228
229 /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
230
231 static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
232 {
233         set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
234 }
235
236 static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
237 {
238         clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
239 }
240
241 static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
242 {
243         return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
244 }
245
246 unsigned int sbitmap_weight(const struct sbitmap *sb);
247
248 /**
249  * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
250  * memory node.
251  * @sbq: Bitmap queue to initialize.
252  * @depth: See sbitmap_init_node().
253  * @shift: See sbitmap_init_node().
254  * @flags: Allocation flags.
255  * @node: Memory node to allocate on.
256  *
257  * Return: Zero on success or negative errno on failure.
258  */
259 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
260                             int shift, gfp_t flags, int node);
261
262 /**
263  * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
264  *
265  * @sbq: Bitmap queue to free.
266  */
267 static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
268 {
269         kfree(sbq->ws);
270         sbitmap_free(&sbq->sb);
271 }
272
273 /**
274  * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
275  * @sbq: Bitmap queue to resize.
276  * @depth: New number of bits to resize to.
277  *
278  * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
279  * some extra work on the &struct sbitmap_queue, so it's not safe to just
280  * resize the underlying &struct sbitmap.
281  */
282 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
283
284 /**
285  * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
286  * &struct sbitmap_queue.
287  * @sbq: Bitmap to free from.
288  * @nr: Bit number to free.
289  */
290 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr);
291
292 static inline int sbq_index_inc(int index)
293 {
294         return (index + 1) & (SBQ_WAIT_QUEUES - 1);
295 }
296
297 static inline void sbq_index_atomic_inc(atomic_t *index)
298 {
299         int old = atomic_read(index);
300         int new = sbq_index_inc(old);
301         atomic_cmpxchg(index, old, new);
302 }
303
304 /**
305  * sbq_wait_ptr() - Get the next wait queue to use for a &struct
306  * sbitmap_queue.
307  * @sbq: Bitmap queue to wait on.
308  * @wait_index: A counter per "user" of @sbq.
309  */
310 static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
311                                                   atomic_t *wait_index)
312 {
313         struct sbq_wait_state *ws;
314
315         ws = &sbq->ws[atomic_read(wait_index)];
316         sbq_index_atomic_inc(wait_index);
317         return ws;
318 }
319
320 /**
321  * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
322  * sbitmap_queue.
323  * @sbq: Bitmap queue to wake up.
324  */
325 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
326
327 #endif /* __LINUX_SCALE_BITMAP_H */