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>
29 * struct sbitmap_word - Word in a &struct sbitmap.
33 * @depth: Number of bits being used in @word/@cleared
38 * @word: word holding free bits
40 unsigned long word ____cacheline_aligned_in_smp;
43 * @cleared: word holding cleared bits
45 unsigned long cleared ____cacheline_aligned_in_smp;
48 * @swap_lock: Held while swapping word <-> cleared
51 } ____cacheline_aligned_in_smp;
54 * struct sbitmap - Scalable bitmap.
56 * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
57 * trades off higher memory usage for better scalability.
61 * @depth: Number of bits used in the whole bitmap.
66 * @shift: log2(number of bits used per word)
71 * @map_nr: Number of words (cachelines) being used for the bitmap.
76 * @map: Allocated bitmap.
78 struct sbitmap_word *map;
81 #define SBQ_WAIT_QUEUES 8
82 #define SBQ_WAKE_BATCH 8
85 * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
87 struct sbq_wait_state {
89 * @wait_cnt: Number of frees remaining before we wake up.
96 wait_queue_head_t wait;
97 } ____cacheline_aligned_in_smp;
100 * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
103 * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to
104 * avoid contention on the wait queue spinlock. This ensures that we don't hit a
105 * scalability wall when we run out of free bits and have to start putting tasks
108 struct sbitmap_queue {
110 * @sb: Scalable bitmap.
115 * @alloc_hint: Cache of last successfully allocated or freed bit.
117 * This is per-cpu, which allows multiple users to stick to different
118 * cachelines until the map is exhausted.
120 unsigned int __percpu *alloc_hint;
123 * @wake_batch: Number of bits which must be freed before we wake up any
126 unsigned int wake_batch;
129 * @wake_index: Next wait queue in @ws to wake up.
136 struct sbq_wait_state *ws;
139 * @ws_active: count of currently active ws waitqueues
144 * @round_robin: Allocate bits in strict round-robin order.
149 * @min_shallow_depth: The minimum shallow depth which may be passed to
150 * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
152 unsigned int min_shallow_depth;
156 * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
157 * @sb: Bitmap to initialize.
158 * @depth: Number of bits to allocate.
159 * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
160 * given, a good default is chosen.
161 * @flags: Allocation flags.
162 * @node: Memory node to allocate on.
164 * Return: Zero on success or negative errno on failure.
166 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
167 gfp_t flags, int node);
170 * sbitmap_free() - Free memory used by a &struct sbitmap.
171 * @sb: Bitmap to free.
173 static inline void sbitmap_free(struct sbitmap *sb)
180 * sbitmap_resize() - Resize a &struct sbitmap.
181 * @sb: Bitmap to resize.
182 * @depth: New number of bits to resize to.
184 * Doesn't reallocate anything. It's up to the caller to ensure that the new
185 * depth doesn't exceed the depth that the sb was initialized with.
187 void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
190 * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
191 * @sb: Bitmap to allocate from.
192 * @alloc_hint: Hint for where to start searching for a free bit.
193 * @round_robin: If true, be stricter about allocation order; always allocate
194 * starting from the last allocated bit. This is less efficient
195 * than the default behavior (false).
197 * This operation provides acquire barrier semantics if it succeeds.
199 * Return: Non-negative allocated bit number if successful, -1 otherwise.
201 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
204 * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
205 * limiting the depth used from each word.
206 * @sb: Bitmap to allocate from.
207 * @alloc_hint: Hint for where to start searching for a free bit.
208 * @shallow_depth: The maximum number of bits to allocate from a single word.
210 * This rather specific operation allows for having multiple users with
211 * different allocation limits. E.g., there can be a high-priority class that
212 * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
213 * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority
214 * class can only allocate half of the total bits in the bitmap, preventing it
215 * from starving out the high-priority class.
217 * Return: Non-negative allocated bit number if successful, -1 otherwise.
219 int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
220 unsigned long shallow_depth);
223 * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
224 * @sb: Bitmap to check.
226 * Return: true if any bit in the bitmap is set, false otherwise.
228 bool sbitmap_any_bit_set(const struct sbitmap *sb);
231 * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
233 * @sb: Bitmap to check.
235 * Return: true if any bit in the bitmap is clear, false otherwise.
237 bool sbitmap_any_bit_clear(const struct sbitmap *sb);
239 #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
240 #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
242 typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
245 * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
246 * @start: Where to start the iteration.
247 * @sb: Bitmap to iterate over.
248 * @fn: Callback. Should return true to continue or false to break early.
249 * @data: Pointer to pass to callback.
251 * This is inline even though it's non-trivial so that the function calls to the
252 * callback will hopefully get optimized away.
254 static inline void __sbitmap_for_each_set(struct sbitmap *sb,
256 sb_for_each_fn fn, void *data)
260 unsigned int scanned = 0;
262 if (start >= sb->depth)
264 index = SB_NR_TO_INDEX(sb, start);
265 nr = SB_NR_TO_BIT(sb, start);
267 while (scanned < sb->depth) {
269 unsigned int depth = min_t(unsigned int,
270 sb->map[index].depth - nr,
271 sb->depth - scanned);
274 word = sb->map[index].word & ~sb->map[index].cleared;
279 * On the first iteration of the outer loop, we need to add the
280 * bit offset back to the size of the word for find_next_bit().
281 * On all other iterations, nr is zero, so this is a noop.
285 nr = find_next_bit(&word, depth, nr);
288 if (!fn(sb, (index << sb->shift) + nr, data))
295 if (++index >= sb->map_nr)
301 * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
302 * @sb: Bitmap to iterate over.
303 * @fn: Callback. Should return true to continue or false to break early.
304 * @data: Pointer to pass to callback.
306 static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
309 __sbitmap_for_each_set(sb, 0, fn, data);
312 static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
315 return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
318 /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
320 static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
322 set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
325 static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
327 clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
331 * This one is special, since it doesn't actually clear the bit, rather it
332 * sets the corresponding bit in the ->cleared mask instead. Paired with
333 * the caller doing sbitmap_deferred_clear() if a given index is full, which
334 * will clear the previously freed entries in the corresponding ->word.
336 static inline void sbitmap_deferred_clear_bit(struct sbitmap *sb, unsigned int bitnr)
338 unsigned long *addr = &sb->map[SB_NR_TO_INDEX(sb, bitnr)].cleared;
340 set_bit(SB_NR_TO_BIT(sb, bitnr), addr);
343 static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb,
346 clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
349 static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
351 return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
355 * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file.
356 * @sb: Bitmap to show.
357 * @m: struct seq_file to write to.
359 * This is intended for debugging. The format may change at any time.
361 void sbitmap_show(struct sbitmap *sb, struct seq_file *m);
364 * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct
366 * @sb: Bitmap to show.
367 * @m: struct seq_file to write to.
369 * This is intended for debugging. The output isn't guaranteed to be internally
372 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m);
375 * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
377 * @sbq: Bitmap queue to initialize.
378 * @depth: See sbitmap_init_node().
379 * @shift: See sbitmap_init_node().
380 * @round_robin: See sbitmap_get().
381 * @flags: Allocation flags.
382 * @node: Memory node to allocate on.
384 * Return: Zero on success or negative errno on failure.
386 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
387 int shift, bool round_robin, gfp_t flags, int node);
390 * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
392 * @sbq: Bitmap queue to free.
394 static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
397 free_percpu(sbq->alloc_hint);
398 sbitmap_free(&sbq->sb);
402 * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
403 * @sbq: Bitmap queue to resize.
404 * @depth: New number of bits to resize to.
406 * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
407 * some extra work on the &struct sbitmap_queue, so it's not safe to just
408 * resize the underlying &struct sbitmap.
410 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
413 * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
414 * sbitmap_queue with preemption already disabled.
415 * @sbq: Bitmap queue to allocate from.
417 * Return: Non-negative allocated bit number if successful, -1 otherwise.
419 int __sbitmap_queue_get(struct sbitmap_queue *sbq);
422 * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
423 * sbitmap_queue, limiting the depth used from each word, with preemption
425 * @sbq: Bitmap queue to allocate from.
426 * @shallow_depth: The maximum number of bits to allocate from a single word.
427 * See sbitmap_get_shallow().
429 * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
432 * Return: Non-negative allocated bit number if successful, -1 otherwise.
434 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
435 unsigned int shallow_depth);
438 * sbitmap_queue_get() - Try to allocate a free bit from a &struct
440 * @sbq: Bitmap queue to allocate from.
441 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
442 * sbitmap_queue_clear()).
444 * Return: Non-negative allocated bit number if successful, -1 otherwise.
446 static inline int sbitmap_queue_get(struct sbitmap_queue *sbq,
452 nr = __sbitmap_queue_get(sbq);
458 * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
459 * sbitmap_queue, limiting the depth used from each word.
460 * @sbq: Bitmap queue to allocate from.
461 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
462 * sbitmap_queue_clear()).
463 * @shallow_depth: The maximum number of bits to allocate from a single word.
464 * See sbitmap_get_shallow().
466 * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
469 * Return: Non-negative allocated bit number if successful, -1 otherwise.
471 static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
473 unsigned int shallow_depth)
478 nr = __sbitmap_queue_get_shallow(sbq, shallow_depth);
484 * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the
485 * minimum shallow depth that will be used.
486 * @sbq: Bitmap queue in question.
487 * @min_shallow_depth: The minimum shallow depth that will be passed to
488 * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
490 * sbitmap_queue_clear() batches wakeups as an optimization. The batch size
491 * depends on the depth of the bitmap. Since the shallow allocation functions
492 * effectively operate with a different depth, the shallow depth must be taken
493 * into account when calculating the batch size. This function must be called
494 * with the minimum shallow depth that will be used. Failure to do so can result
497 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
498 unsigned int min_shallow_depth);
501 * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
502 * &struct sbitmap_queue.
503 * @sbq: Bitmap to free from.
504 * @nr: Bit number to free.
505 * @cpu: CPU the bit was allocated on.
507 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
510 static inline int sbq_index_inc(int index)
512 return (index + 1) & (SBQ_WAIT_QUEUES - 1);
515 static inline void sbq_index_atomic_inc(atomic_t *index)
517 int old = atomic_read(index);
518 int new = sbq_index_inc(old);
519 atomic_cmpxchg(index, old, new);
523 * sbq_wait_ptr() - Get the next wait queue to use for a &struct
525 * @sbq: Bitmap queue to wait on.
526 * @wait_index: A counter per "user" of @sbq.
528 static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
529 atomic_t *wait_index)
531 struct sbq_wait_state *ws;
533 ws = &sbq->ws[atomic_read(wait_index)];
534 sbq_index_atomic_inc(wait_index);
539 * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
541 * @sbq: Bitmap queue to wake up.
543 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
546 * sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue
547 * on a &struct sbitmap_queue.
548 * @sbq: Bitmap queue to wake up.
550 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq);
553 * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct
555 * @sbq: Bitmap queue to show.
556 * @m: struct seq_file to write to.
558 * This is intended for debugging. The format may change at any time.
560 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m);
563 struct sbitmap_queue *sbq; /* if set, sbq_wait is accounted */
564 struct wait_queue_entry wait;
567 #define DEFINE_SBQ_WAIT(name) \
568 struct sbq_wait name = { \
571 .private = current, \
572 .func = autoremove_wake_function, \
573 .entry = LIST_HEAD_INIT((name).wait.entry), \
578 * Wrapper around prepare_to_wait_exclusive(), which maintains some extra
581 void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
582 struct sbq_wait_state *ws,
583 struct sbq_wait *sbq_wait, int state);
586 * Must be paired with sbitmap_prepare_to_wait().
588 void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
589 struct sbq_wait *sbq_wait);
592 * Wrapper around add_wait_queue(), which maintains some extra internal state
594 void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
595 struct sbq_wait_state *ws,
596 struct sbq_wait *sbq_wait);
599 * Must be paired with sbitmap_add_wait_queue()
601 void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait);
603 #endif /* __LINUX_SCALE_BITMAP_H */