2 * Fast and scalable bitmaps.
4 * Copyright (C) 2016 Facebook
5 * Copyright (C) 2013-2014 Jens Axboe
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.
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.
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/>.
20 #ifndef __LINUX_SCALE_BITMAP_H
21 #define __LINUX_SCALE_BITMAP_H
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
27 * struct sbitmap_word - Word in a &struct sbitmap.
31 * @word: The bitmap word itself.
36 * @depth: Number of bits being used in @word.
39 } ____cacheline_aligned_in_smp;
42 * struct sbitmap - Scalable bitmap.
44 * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
45 * trades off higher memory usage for better scalability.
49 * @depth: Number of bits used in the whole bitmap.
54 * @shift: log2(number of bits used per word)
59 * @map_nr: Number of words (cachelines) being used for the bitmap.
64 * @map: Allocated bitmap.
66 struct sbitmap_word *map;
69 #define SBQ_WAIT_QUEUES 8
70 #define SBQ_WAKE_BATCH 8
73 * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
75 struct sbq_wait_state {
77 * @wait_cnt: Number of frees remaining before we wake up.
84 wait_queue_head_t wait;
85 } ____cacheline_aligned_in_smp;
88 * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
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
96 struct sbitmap_queue {
98 * @sb: Scalable bitmap.
103 * @alloc_hint: Cache of last successfully allocated or freed bit.
105 * This is per-cpu, which allows multiple users to stick to different
106 * cachelines until the map is exhausted.
108 unsigned int __percpu *alloc_hint;
111 * @wake_batch: Number of bits which must be freed before we wake up any
114 unsigned int wake_batch;
117 * @wake_index: Next wait queue in @ws to wake up.
124 struct sbq_wait_state *ws;
127 * @round_robin: Allocate bits in strict round-robin order.
132 * @min_shallow_depth: The minimum shallow depth which may be passed to
133 * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
135 unsigned int min_shallow_depth;
139 * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
140 * @sb: Bitmap to initialize.
141 * @depth: Number of bits to allocate.
142 * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
143 * given, a good default is chosen.
144 * @flags: Allocation flags.
145 * @node: Memory node to allocate on.
147 * Return: Zero on success or negative errno on failure.
149 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
150 gfp_t flags, int node);
153 * sbitmap_free() - Free memory used by a &struct sbitmap.
154 * @sb: Bitmap to free.
156 static inline void sbitmap_free(struct sbitmap *sb)
163 * sbitmap_resize() - Resize a &struct sbitmap.
164 * @sb: Bitmap to resize.
165 * @depth: New number of bits to resize to.
167 * Doesn't reallocate anything. It's up to the caller to ensure that the new
168 * depth doesn't exceed the depth that the sb was initialized with.
170 void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
173 * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
174 * @sb: Bitmap to allocate from.
175 * @alloc_hint: Hint for where to start searching for a free bit.
176 * @round_robin: If true, be stricter about allocation order; always allocate
177 * starting from the last allocated bit. This is less efficient
178 * than the default behavior (false).
180 * This operation provides acquire barrier semantics if it succeeds.
182 * Return: Non-negative allocated bit number if successful, -1 otherwise.
184 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
187 * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
188 * limiting the depth used from each word.
189 * @sb: Bitmap to allocate from.
190 * @alloc_hint: Hint for where to start searching for a free bit.
191 * @shallow_depth: The maximum number of bits to allocate from a single word.
193 * This rather specific operation allows for having multiple users with
194 * different allocation limits. E.g., there can be a high-priority class that
195 * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
196 * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority
197 * class can only allocate half of the total bits in the bitmap, preventing it
198 * from starving out the high-priority class.
200 * Return: Non-negative allocated bit number if successful, -1 otherwise.
202 int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
203 unsigned long shallow_depth);
206 * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
207 * @sb: Bitmap to check.
209 * Return: true if any bit in the bitmap is set, false otherwise.
211 bool sbitmap_any_bit_set(const struct sbitmap *sb);
214 * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
216 * @sb: Bitmap to check.
218 * Return: true if any bit in the bitmap is clear, false otherwise.
220 bool sbitmap_any_bit_clear(const struct sbitmap *sb);
222 #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
223 #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
225 typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
228 * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
229 * @start: Where to start the iteration.
230 * @sb: Bitmap to iterate over.
231 * @fn: Callback. Should return true to continue or false to break early.
232 * @data: Pointer to pass to callback.
234 * This is inline even though it's non-trivial so that the function calls to the
235 * callback will hopefully get optimized away.
237 static inline void __sbitmap_for_each_set(struct sbitmap *sb,
239 sb_for_each_fn fn, void *data)
243 unsigned int scanned = 0;
245 if (start >= sb->depth)
247 index = SB_NR_TO_INDEX(sb, start);
248 nr = SB_NR_TO_BIT(sb, start);
250 while (scanned < sb->depth) {
251 struct sbitmap_word *word = &sb->map[index];
252 unsigned int depth = min_t(unsigned int, word->depth - nr,
253 sb->depth - scanned);
260 * On the first iteration of the outer loop, we need to add the
261 * bit offset back to the size of the word for find_next_bit().
262 * On all other iterations, nr is zero, so this is a noop.
266 nr = find_next_bit(&word->word, depth, nr);
269 if (!fn(sb, (index << sb->shift) + nr, data))
276 if (++index >= sb->map_nr)
282 * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
283 * @sb: Bitmap to iterate over.
284 * @fn: Callback. Should return true to continue or false to break early.
285 * @data: Pointer to pass to callback.
287 static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
290 __sbitmap_for_each_set(sb, 0, fn, data);
293 static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
296 return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
299 /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
301 static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
303 set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
306 static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
308 clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
311 static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb,
314 clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
317 static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
319 return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
322 unsigned int sbitmap_weight(const struct sbitmap *sb);
325 * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file.
326 * @sb: Bitmap to show.
327 * @m: struct seq_file to write to.
329 * This is intended for debugging. The format may change at any time.
331 void sbitmap_show(struct sbitmap *sb, struct seq_file *m);
334 * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct
336 * @sb: Bitmap to show.
337 * @m: struct seq_file to write to.
339 * This is intended for debugging. The output isn't guaranteed to be internally
342 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m);
345 * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
347 * @sbq: Bitmap queue to initialize.
348 * @depth: See sbitmap_init_node().
349 * @shift: See sbitmap_init_node().
350 * @round_robin: See sbitmap_get().
351 * @flags: Allocation flags.
352 * @node: Memory node to allocate on.
354 * Return: Zero on success or negative errno on failure.
356 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
357 int shift, bool round_robin, gfp_t flags, int node);
360 * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
362 * @sbq: Bitmap queue to free.
364 static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
367 free_percpu(sbq->alloc_hint);
368 sbitmap_free(&sbq->sb);
372 * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
373 * @sbq: Bitmap queue to resize.
374 * @depth: New number of bits to resize to.
376 * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
377 * some extra work on the &struct sbitmap_queue, so it's not safe to just
378 * resize the underlying &struct sbitmap.
380 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
383 * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
384 * sbitmap_queue with preemption already disabled.
385 * @sbq: Bitmap queue to allocate from.
387 * Return: Non-negative allocated bit number if successful, -1 otherwise.
389 int __sbitmap_queue_get(struct sbitmap_queue *sbq);
392 * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
393 * sbitmap_queue, limiting the depth used from each word, with preemption
395 * @sbq: Bitmap queue to allocate from.
396 * @shallow_depth: The maximum number of bits to allocate from a single word.
397 * See sbitmap_get_shallow().
399 * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
402 * Return: Non-negative allocated bit number if successful, -1 otherwise.
404 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
405 unsigned int shallow_depth);
408 * sbitmap_queue_get() - Try to allocate a free bit from a &struct
410 * @sbq: Bitmap queue to allocate from.
411 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
412 * sbitmap_queue_clear()).
414 * Return: Non-negative allocated bit number if successful, -1 otherwise.
416 static inline int sbitmap_queue_get(struct sbitmap_queue *sbq,
422 nr = __sbitmap_queue_get(sbq);
428 * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
429 * sbitmap_queue, limiting the depth used from each word.
430 * @sbq: Bitmap queue to allocate from.
431 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
432 * sbitmap_queue_clear()).
433 * @shallow_depth: The maximum number of bits to allocate from a single word.
434 * See sbitmap_get_shallow().
436 * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
439 * Return: Non-negative allocated bit number if successful, -1 otherwise.
441 static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
443 unsigned int shallow_depth)
448 nr = __sbitmap_queue_get_shallow(sbq, shallow_depth);
454 * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the
455 * minimum shallow depth that will be used.
456 * @sbq: Bitmap queue in question.
457 * @min_shallow_depth: The minimum shallow depth that will be passed to
458 * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
460 * sbitmap_queue_clear() batches wakeups as an optimization. The batch size
461 * depends on the depth of the bitmap. Since the shallow allocation functions
462 * effectively operate with a different depth, the shallow depth must be taken
463 * into account when calculating the batch size. This function must be called
464 * with the minimum shallow depth that will be used. Failure to do so can result
467 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
468 unsigned int min_shallow_depth);
471 * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
472 * &struct sbitmap_queue.
473 * @sbq: Bitmap to free from.
474 * @nr: Bit number to free.
475 * @cpu: CPU the bit was allocated on.
477 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
480 static inline int sbq_index_inc(int index)
482 return (index + 1) & (SBQ_WAIT_QUEUES - 1);
485 static inline void sbq_index_atomic_inc(atomic_t *index)
487 int old = atomic_read(index);
488 int new = sbq_index_inc(old);
489 atomic_cmpxchg(index, old, new);
493 * sbq_wait_ptr() - Get the next wait queue to use for a &struct
495 * @sbq: Bitmap queue to wait on.
496 * @wait_index: A counter per "user" of @sbq.
498 static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
499 atomic_t *wait_index)
501 struct sbq_wait_state *ws;
503 ws = &sbq->ws[atomic_read(wait_index)];
504 sbq_index_atomic_inc(wait_index);
509 * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
511 * @sbq: Bitmap queue to wake up.
513 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
516 * sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue
517 * on a &struct sbitmap_queue.
518 * @sbq: Bitmap queue to wake up.
520 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq);
523 * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct
525 * @sbq: Bitmap queue to show.
526 * @m: struct seq_file to write to.
528 * This is intended for debugging. The format may change at any time.
530 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m);
532 #endif /* __LINUX_SCALE_BITMAP_H */