2 * Copyright (C) 2016 Facebook
3 * Copyright (C) 2013-2014 Jens Axboe
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 #include <linux/sched.h>
19 #include <linux/random.h>
20 #include <linux/sbitmap.h>
21 #include <linux/seq_file.h>
24 * See if we have deferred clears that we can batch move
26 static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index)
28 unsigned long mask, val;
32 spin_lock_irqsave(&sb->map[index].swap_lock, flags);
34 if (!sb->map[index].cleared)
38 * First get a stable cleared mask, setting the old mask to 0.
41 mask = sb->map[index].cleared;
42 } while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask);
45 * Now clear the masked bits in our free word
48 val = sb->map[index].word;
49 } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
53 spin_unlock_irqrestore(&sb->map[index].swap_lock, flags);
57 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
58 gfp_t flags, int node)
60 unsigned int bits_per_word;
64 shift = ilog2(BITS_PER_LONG);
66 * If the bitmap is small, shrink the number of bits per word so
67 * we spread over a few cachelines, at least. If less than 4
68 * bits, just forget about it, it's not going to work optimally
72 while ((4U << shift) > depth)
76 bits_per_word = 1U << shift;
77 if (bits_per_word > BITS_PER_LONG)
82 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
89 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
93 for (i = 0; i < sb->map_nr; i++) {
94 sb->map[i].depth = min(depth, bits_per_word);
95 depth -= sb->map[i].depth;
96 spin_lock_init(&sb->map[i].swap_lock);
100 EXPORT_SYMBOL_GPL(sbitmap_init_node);
102 void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
104 unsigned int bits_per_word = 1U << sb->shift;
107 for (i = 0; i < sb->map_nr; i++)
108 sbitmap_deferred_clear(sb, i);
111 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
113 for (i = 0; i < sb->map_nr; i++) {
114 sb->map[i].depth = min(depth, bits_per_word);
115 depth -= sb->map[i].depth;
118 EXPORT_SYMBOL_GPL(sbitmap_resize);
120 static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
121 unsigned int hint, bool wrap)
123 unsigned int orig_hint = hint;
127 nr = find_next_zero_bit(word, depth, hint);
128 if (unlikely(nr >= depth)) {
130 * We started with an offset, and we didn't reset the
131 * offset to 0 in a failure case, so start from 0 to
134 if (orig_hint && hint && wrap) {
135 hint = orig_hint = 0;
141 if (!test_and_set_bit_lock(nr, word))
145 if (hint >= depth - 1)
152 static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
153 unsigned int alloc_hint, bool round_robin)
158 nr = __sbitmap_get_word(&sb->map[index].word,
159 sb->map[index].depth, alloc_hint,
163 if (!sbitmap_deferred_clear(sb, index))
170 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
172 unsigned int i, index;
175 index = SB_NR_TO_INDEX(sb, alloc_hint);
178 * Unless we're doing round robin tag allocation, just use the
179 * alloc_hint to find the right word index. No point in looping
180 * twice in find_next_zero_bit() for that case.
183 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
187 for (i = 0; i < sb->map_nr; i++) {
188 nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
191 nr += index << sb->shift;
195 /* Jump to next index. */
197 if (++index >= sb->map_nr)
203 EXPORT_SYMBOL_GPL(sbitmap_get);
205 int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
206 unsigned long shallow_depth)
208 unsigned int i, index;
211 index = SB_NR_TO_INDEX(sb, alloc_hint);
213 for (i = 0; i < sb->map_nr; i++) {
215 nr = __sbitmap_get_word(&sb->map[index].word,
216 min(sb->map[index].depth, shallow_depth),
217 SB_NR_TO_BIT(sb, alloc_hint), true);
219 nr += index << sb->shift;
223 if (sbitmap_deferred_clear(sb, index))
226 /* Jump to next index. */
228 alloc_hint = index << sb->shift;
230 if (index >= sb->map_nr) {
238 EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
240 bool sbitmap_any_bit_set(const struct sbitmap *sb)
244 for (i = 0; i < sb->map_nr; i++) {
245 if (sb->map[i].word & ~sb->map[i].cleared)
250 EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
252 bool sbitmap_any_bit_clear(const struct sbitmap *sb)
256 for (i = 0; i < sb->map_nr; i++) {
257 const struct sbitmap_word *word = &sb->map[i];
258 unsigned long mask = word->word & ~word->cleared;
261 ret = find_first_zero_bit(&mask, word->depth);
262 if (ret < word->depth)
267 EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
269 static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
271 unsigned int i, weight = 0;
273 for (i = 0; i < sb->map_nr; i++) {
274 const struct sbitmap_word *word = &sb->map[i];
277 weight += bitmap_weight(&word->word, word->depth);
279 weight += bitmap_weight(&word->cleared, word->depth);
284 static unsigned int sbitmap_weight(const struct sbitmap *sb)
286 return __sbitmap_weight(sb, true);
289 static unsigned int sbitmap_cleared(const struct sbitmap *sb)
291 return __sbitmap_weight(sb, false);
294 void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
296 seq_printf(m, "depth=%u\n", sb->depth);
297 seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb));
298 seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
299 seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
300 seq_printf(m, "map_nr=%u\n", sb->map_nr);
302 EXPORT_SYMBOL_GPL(sbitmap_show);
304 static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
306 if ((offset & 0xf) == 0) {
309 seq_printf(m, "%08x:", offset);
311 if ((offset & 0x1) == 0)
313 seq_printf(m, "%02x", byte);
316 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
319 unsigned int byte_bits = 0;
320 unsigned int offset = 0;
323 for (i = 0; i < sb->map_nr; i++) {
324 unsigned long word = READ_ONCE(sb->map[i].word);
325 unsigned int word_bits = READ_ONCE(sb->map[i].depth);
327 while (word_bits > 0) {
328 unsigned int bits = min(8 - byte_bits, word_bits);
330 byte |= (word & (BIT(bits) - 1)) << byte_bits;
332 if (byte_bits == 8) {
333 emit_byte(m, offset, byte);
343 emit_byte(m, offset, byte);
349 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
351 static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
354 unsigned int wake_batch;
355 unsigned int shallow_depth;
358 * For each batch, we wake up one queue. We need to make sure that our
359 * batch size is small enough that the full depth of the bitmap,
360 * potentially limited by a shallow depth, is enough to wake up all of
363 * Each full word of the bitmap has bits_per_word bits, and there might
364 * be a partial word. There are depth / bits_per_word full words and
365 * depth % bits_per_word bits left over. In bitwise arithmetic:
367 * bits_per_word = 1 << shift
368 * depth / bits_per_word = depth >> shift
369 * depth % bits_per_word = depth & ((1 << shift) - 1)
371 * Each word can be limited to sbq->min_shallow_depth bits.
373 shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
374 depth = ((depth >> sbq->sb.shift) * shallow_depth +
375 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
376 wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
382 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
383 int shift, bool round_robin, gfp_t flags, int node)
388 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
392 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
393 if (!sbq->alloc_hint) {
394 sbitmap_free(&sbq->sb);
398 if (depth && !round_robin) {
399 for_each_possible_cpu(i)
400 *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
403 sbq->min_shallow_depth = UINT_MAX;
404 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
405 atomic_set(&sbq->wake_index, 0);
406 atomic_set(&sbq->ws_active, 0);
408 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
410 free_percpu(sbq->alloc_hint);
411 sbitmap_free(&sbq->sb);
415 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
416 init_waitqueue_head(&sbq->ws[i].wait);
417 atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
420 sbq->round_robin = round_robin;
423 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
425 static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
428 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
431 if (sbq->wake_batch != wake_batch) {
432 WRITE_ONCE(sbq->wake_batch, wake_batch);
434 * Pairs with the memory barrier in sbitmap_queue_wake_up()
435 * to ensure that the batch size is updated before the wait
438 smp_mb__before_atomic();
439 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
440 atomic_set(&sbq->ws[i].wait_cnt, 1);
444 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
446 sbitmap_queue_update_wake_batch(sbq, depth);
447 sbitmap_resize(&sbq->sb, depth);
449 EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
451 int __sbitmap_queue_get(struct sbitmap_queue *sbq)
453 unsigned int hint, depth;
456 hint = this_cpu_read(*sbq->alloc_hint);
457 depth = READ_ONCE(sbq->sb.depth);
458 if (unlikely(hint >= depth)) {
459 hint = depth ? prandom_u32() % depth : 0;
460 this_cpu_write(*sbq->alloc_hint, hint);
462 nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
465 /* If the map is full, a hint won't do us much good. */
466 this_cpu_write(*sbq->alloc_hint, 0);
467 } else if (nr == hint || unlikely(sbq->round_robin)) {
468 /* Only update the hint if we used it. */
470 if (hint >= depth - 1)
472 this_cpu_write(*sbq->alloc_hint, hint);
477 EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
479 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
480 unsigned int shallow_depth)
482 unsigned int hint, depth;
485 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
487 hint = this_cpu_read(*sbq->alloc_hint);
488 depth = READ_ONCE(sbq->sb.depth);
489 if (unlikely(hint >= depth)) {
490 hint = depth ? prandom_u32() % depth : 0;
491 this_cpu_write(*sbq->alloc_hint, hint);
493 nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
496 /* If the map is full, a hint won't do us much good. */
497 this_cpu_write(*sbq->alloc_hint, 0);
498 } else if (nr == hint || unlikely(sbq->round_robin)) {
499 /* Only update the hint if we used it. */
501 if (hint >= depth - 1)
503 this_cpu_write(*sbq->alloc_hint, hint);
508 EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
510 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
511 unsigned int min_shallow_depth)
513 sbq->min_shallow_depth = min_shallow_depth;
514 sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
516 EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
518 static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
522 if (!atomic_read(&sbq->ws_active))
525 wake_index = atomic_read(&sbq->wake_index);
526 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
527 struct sbq_wait_state *ws = &sbq->ws[wake_index];
529 if (waitqueue_active(&ws->wait)) {
530 int o = atomic_read(&sbq->wake_index);
533 atomic_cmpxchg(&sbq->wake_index, o, wake_index);
537 wake_index = sbq_index_inc(wake_index);
543 static bool __sbq_wake_up(struct sbitmap_queue *sbq)
545 struct sbq_wait_state *ws;
546 unsigned int wake_batch;
549 ws = sbq_wake_ptr(sbq);
553 wait_cnt = atomic_dec_return(&ws->wait_cnt);
557 wake_batch = READ_ONCE(sbq->wake_batch);
560 * Pairs with the memory barrier in sbitmap_queue_resize() to
561 * ensure that we see the batch size update before the wait
564 smp_mb__before_atomic();
567 * For concurrent callers of this, the one that failed the
568 * atomic_cmpxhcg() race should call this function again
569 * to wakeup a new batch on a different 'ws'.
571 ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
572 if (ret == wait_cnt) {
573 sbq_index_atomic_inc(&sbq->wake_index);
574 wake_up_nr(&ws->wait, wake_batch);
584 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
586 while (__sbq_wake_up(sbq))
589 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
591 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
595 * Once the clear bit is set, the bit may be allocated out.
597 * Orders READ/WRITE on the asssociated instance(such as request
598 * of blk_mq) by this bit for avoiding race with re-allocation,
599 * and its pair is the memory barrier implied in __sbitmap_get_word.
601 * One invariant is that the clear bit has to be zero when the bit
604 smp_mb__before_atomic();
605 sbitmap_deferred_clear_bit(&sbq->sb, nr);
608 * Pairs with the memory barrier in set_current_state() to ensure the
609 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
610 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
611 * waiter. See the comment on waitqueue_active().
613 smp_mb__after_atomic();
614 sbitmap_queue_wake_up(sbq);
616 if (likely(!sbq->round_robin && nr < sbq->sb.depth))
617 *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
619 EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
621 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
626 * Pairs with the memory barrier in set_current_state() like in
627 * sbitmap_queue_wake_up().
630 wake_index = atomic_read(&sbq->wake_index);
631 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
632 struct sbq_wait_state *ws = &sbq->ws[wake_index];
634 if (waitqueue_active(&ws->wait))
637 wake_index = sbq_index_inc(wake_index);
640 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
642 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
647 sbitmap_show(&sbq->sb, m);
649 seq_puts(m, "alloc_hint={");
651 for_each_possible_cpu(i) {
655 seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
659 seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
660 seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
661 seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
663 seq_puts(m, "ws={\n");
664 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
665 struct sbq_wait_state *ws = &sbq->ws[i];
667 seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
668 atomic_read(&ws->wait_cnt),
669 waitqueue_active(&ws->wait) ? "active" : "inactive");
673 seq_printf(m, "round_robin=%d\n", sbq->round_robin);
674 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
676 EXPORT_SYMBOL_GPL(sbitmap_queue_show);
678 void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
679 struct sbq_wait_state *ws,
680 struct sbq_wait *sbq_wait)
682 if (!sbq_wait->sbq) {
684 atomic_inc(&sbq->ws_active);
686 add_wait_queue(&ws->wait, &sbq_wait->wait);
688 EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
690 void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
692 list_del_init(&sbq_wait->wait.entry);
694 atomic_dec(&sbq_wait->sbq->ws_active);
695 sbq_wait->sbq = NULL;
698 EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
700 void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
701 struct sbq_wait_state *ws,
702 struct sbq_wait *sbq_wait, int state)
704 if (!sbq_wait->sbq) {
705 atomic_inc(&sbq->ws_active);
708 prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
710 EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
712 void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
713 struct sbq_wait *sbq_wait)
715 finish_wait(&ws->wait, &sbq_wait->wait);
717 atomic_dec(&sbq->ws_active);
718 sbq_wait->sbq = NULL;
721 EXPORT_SYMBOL_GPL(sbitmap_finish_wait);