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;
29 unsigned long __maybe_unused flags;
32 /* Silence bogus lockdep warning */
33 #if defined(CONFIG_LOCKDEP)
34 local_irq_save(flags);
36 spin_lock(&sb->map[index].swap_lock);
38 if (!sb->map[index].cleared)
42 * First get a stable cleared mask, setting the old mask to 0.
45 mask = sb->map[index].cleared;
46 } while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask);
49 * Now clear the masked bits in our free word
52 val = sb->map[index].word;
53 } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
57 spin_unlock(&sb->map[index].swap_lock);
58 #if defined(CONFIG_LOCKDEP)
59 local_irq_restore(flags);
64 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
65 gfp_t flags, int node)
67 unsigned int bits_per_word;
71 shift = ilog2(BITS_PER_LONG);
73 * If the bitmap is small, shrink the number of bits per word so
74 * we spread over a few cachelines, at least. If less than 4
75 * bits, just forget about it, it's not going to work optimally
79 while ((4U << shift) > depth)
83 bits_per_word = 1U << shift;
84 if (bits_per_word > BITS_PER_LONG)
89 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
96 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
100 for (i = 0; i < sb->map_nr; i++) {
101 sb->map[i].depth = min(depth, bits_per_word);
102 depth -= sb->map[i].depth;
103 spin_lock_init(&sb->map[i].swap_lock);
107 EXPORT_SYMBOL_GPL(sbitmap_init_node);
109 void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
111 unsigned int bits_per_word = 1U << sb->shift;
114 for (i = 0; i < sb->map_nr; i++)
115 sbitmap_deferred_clear(sb, i);
118 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
120 for (i = 0; i < sb->map_nr; i++) {
121 sb->map[i].depth = min(depth, bits_per_word);
122 depth -= sb->map[i].depth;
125 EXPORT_SYMBOL_GPL(sbitmap_resize);
127 static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
128 unsigned int hint, bool wrap)
130 unsigned int orig_hint = hint;
134 nr = find_next_zero_bit(word, depth, hint);
135 if (unlikely(nr >= depth)) {
137 * We started with an offset, and we didn't reset the
138 * offset to 0 in a failure case, so start from 0 to
141 if (orig_hint && hint && wrap) {
142 hint = orig_hint = 0;
148 if (!test_and_set_bit_lock(nr, word))
152 if (hint >= depth - 1)
159 static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
160 unsigned int alloc_hint, bool round_robin)
165 nr = __sbitmap_get_word(&sb->map[index].word,
166 sb->map[index].depth, alloc_hint,
170 if (!sbitmap_deferred_clear(sb, index))
177 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
179 unsigned int i, index;
182 index = SB_NR_TO_INDEX(sb, alloc_hint);
185 * Unless we're doing round robin tag allocation, just use the
186 * alloc_hint to find the right word index. No point in looping
187 * twice in find_next_zero_bit() for that case.
190 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
194 for (i = 0; i < sb->map_nr; i++) {
195 nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
198 nr += index << sb->shift;
202 /* Jump to next index. */
204 if (++index >= sb->map_nr)
210 EXPORT_SYMBOL_GPL(sbitmap_get);
212 int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
213 unsigned long shallow_depth)
215 unsigned int i, index;
218 index = SB_NR_TO_INDEX(sb, alloc_hint);
220 for (i = 0; i < sb->map_nr; i++) {
222 nr = __sbitmap_get_word(&sb->map[index].word,
223 min(sb->map[index].depth, shallow_depth),
224 SB_NR_TO_BIT(sb, alloc_hint), true);
226 nr += index << sb->shift;
230 if (sbitmap_deferred_clear(sb, index))
233 /* Jump to next index. */
235 alloc_hint = index << sb->shift;
237 if (index >= sb->map_nr) {
245 EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
247 bool sbitmap_any_bit_set(const struct sbitmap *sb)
251 for (i = 0; i < sb->map_nr; i++) {
252 if (sb->map[i].word & ~sb->map[i].cleared)
257 EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
259 bool sbitmap_any_bit_clear(const struct sbitmap *sb)
263 for (i = 0; i < sb->map_nr; i++) {
264 const struct sbitmap_word *word = &sb->map[i];
265 unsigned long mask = word->word & ~word->cleared;
268 ret = find_first_zero_bit(&mask, word->depth);
269 if (ret < word->depth)
274 EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
276 static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
278 unsigned int i, weight = 0;
280 for (i = 0; i < sb->map_nr; i++) {
281 const struct sbitmap_word *word = &sb->map[i];
284 weight += bitmap_weight(&word->word, word->depth);
286 weight += bitmap_weight(&word->cleared, word->depth);
291 static unsigned int sbitmap_weight(const struct sbitmap *sb)
293 return __sbitmap_weight(sb, true);
296 static unsigned int sbitmap_cleared(const struct sbitmap *sb)
298 return __sbitmap_weight(sb, false);
301 void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
303 seq_printf(m, "depth=%u\n", sb->depth);
304 seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb));
305 seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
306 seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
307 seq_printf(m, "map_nr=%u\n", sb->map_nr);
309 EXPORT_SYMBOL_GPL(sbitmap_show);
311 static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
313 if ((offset & 0xf) == 0) {
316 seq_printf(m, "%08x:", offset);
318 if ((offset & 0x1) == 0)
320 seq_printf(m, "%02x", byte);
323 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
326 unsigned int byte_bits = 0;
327 unsigned int offset = 0;
330 for (i = 0; i < sb->map_nr; i++) {
331 unsigned long word = READ_ONCE(sb->map[i].word);
332 unsigned int word_bits = READ_ONCE(sb->map[i].depth);
334 while (word_bits > 0) {
335 unsigned int bits = min(8 - byte_bits, word_bits);
337 byte |= (word & (BIT(bits) - 1)) << byte_bits;
339 if (byte_bits == 8) {
340 emit_byte(m, offset, byte);
350 emit_byte(m, offset, byte);
356 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
358 static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
361 unsigned int wake_batch;
362 unsigned int shallow_depth;
365 * For each batch, we wake up one queue. We need to make sure that our
366 * batch size is small enough that the full depth of the bitmap,
367 * potentially limited by a shallow depth, is enough to wake up all of
370 * Each full word of the bitmap has bits_per_word bits, and there might
371 * be a partial word. There are depth / bits_per_word full words and
372 * depth % bits_per_word bits left over. In bitwise arithmetic:
374 * bits_per_word = 1 << shift
375 * depth / bits_per_word = depth >> shift
376 * depth % bits_per_word = depth & ((1 << shift) - 1)
378 * Each word can be limited to sbq->min_shallow_depth bits.
380 shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
381 depth = ((depth >> sbq->sb.shift) * shallow_depth +
382 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
383 wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
389 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
390 int shift, bool round_robin, gfp_t flags, int node)
395 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
399 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
400 if (!sbq->alloc_hint) {
401 sbitmap_free(&sbq->sb);
405 if (depth && !round_robin) {
406 for_each_possible_cpu(i)
407 *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
410 sbq->min_shallow_depth = UINT_MAX;
411 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
412 atomic_set(&sbq->wake_index, 0);
413 atomic_set(&sbq->ws_active, 0);
415 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
417 free_percpu(sbq->alloc_hint);
418 sbitmap_free(&sbq->sb);
422 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
423 init_waitqueue_head(&sbq->ws[i].wait);
424 atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
427 sbq->round_robin = round_robin;
430 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
432 static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
435 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
438 if (sbq->wake_batch != wake_batch) {
439 WRITE_ONCE(sbq->wake_batch, wake_batch);
441 * Pairs with the memory barrier in sbitmap_queue_wake_up()
442 * to ensure that the batch size is updated before the wait
445 smp_mb__before_atomic();
446 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
447 atomic_set(&sbq->ws[i].wait_cnt, 1);
451 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
453 sbitmap_queue_update_wake_batch(sbq, depth);
454 sbitmap_resize(&sbq->sb, depth);
456 EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
458 int __sbitmap_queue_get(struct sbitmap_queue *sbq)
460 unsigned int hint, depth;
463 hint = this_cpu_read(*sbq->alloc_hint);
464 depth = READ_ONCE(sbq->sb.depth);
465 if (unlikely(hint >= depth)) {
466 hint = depth ? prandom_u32() % depth : 0;
467 this_cpu_write(*sbq->alloc_hint, hint);
469 nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
472 /* If the map is full, a hint won't do us much good. */
473 this_cpu_write(*sbq->alloc_hint, 0);
474 } else if (nr == hint || unlikely(sbq->round_robin)) {
475 /* Only update the hint if we used it. */
477 if (hint >= depth - 1)
479 this_cpu_write(*sbq->alloc_hint, hint);
484 EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
486 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
487 unsigned int shallow_depth)
489 unsigned int hint, depth;
492 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
494 hint = this_cpu_read(*sbq->alloc_hint);
495 depth = READ_ONCE(sbq->sb.depth);
496 if (unlikely(hint >= depth)) {
497 hint = depth ? prandom_u32() % depth : 0;
498 this_cpu_write(*sbq->alloc_hint, hint);
500 nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
503 /* If the map is full, a hint won't do us much good. */
504 this_cpu_write(*sbq->alloc_hint, 0);
505 } else if (nr == hint || unlikely(sbq->round_robin)) {
506 /* Only update the hint if we used it. */
508 if (hint >= depth - 1)
510 this_cpu_write(*sbq->alloc_hint, hint);
515 EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
517 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
518 unsigned int min_shallow_depth)
520 sbq->min_shallow_depth = min_shallow_depth;
521 sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
523 EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
525 static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
529 if (!atomic_read(&sbq->ws_active))
532 wake_index = atomic_read(&sbq->wake_index);
533 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
534 struct sbq_wait_state *ws = &sbq->ws[wake_index];
536 if (waitqueue_active(&ws->wait)) {
537 int o = atomic_read(&sbq->wake_index);
540 atomic_cmpxchg(&sbq->wake_index, o, wake_index);
544 wake_index = sbq_index_inc(wake_index);
550 static bool __sbq_wake_up(struct sbitmap_queue *sbq)
552 struct sbq_wait_state *ws;
553 unsigned int wake_batch;
556 ws = sbq_wake_ptr(sbq);
560 wait_cnt = atomic_dec_return(&ws->wait_cnt);
564 wake_batch = READ_ONCE(sbq->wake_batch);
567 * Pairs with the memory barrier in sbitmap_queue_resize() to
568 * ensure that we see the batch size update before the wait
571 smp_mb__before_atomic();
574 * For concurrent callers of this, the one that failed the
575 * atomic_cmpxhcg() race should call this function again
576 * to wakeup a new batch on a different 'ws'.
578 ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
579 if (ret == wait_cnt) {
580 sbq_index_atomic_inc(&sbq->wake_index);
581 wake_up_nr(&ws->wait, wake_batch);
591 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
593 while (__sbq_wake_up(sbq))
596 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
598 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
601 sbitmap_deferred_clear_bit(&sbq->sb, nr);
604 * Pairs with the memory barrier in set_current_state() to ensure the
605 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
606 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
607 * waiter. See the comment on waitqueue_active().
609 smp_mb__after_atomic();
610 sbitmap_queue_wake_up(sbq);
612 if (likely(!sbq->round_robin && nr < sbq->sb.depth))
613 *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
615 EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
617 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
622 * Pairs with the memory barrier in set_current_state() like in
623 * sbitmap_queue_wake_up().
626 wake_index = atomic_read(&sbq->wake_index);
627 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
628 struct sbq_wait_state *ws = &sbq->ws[wake_index];
630 if (waitqueue_active(&ws->wait))
633 wake_index = sbq_index_inc(wake_index);
636 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
638 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
643 sbitmap_show(&sbq->sb, m);
645 seq_puts(m, "alloc_hint={");
647 for_each_possible_cpu(i) {
651 seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
655 seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
656 seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
657 seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
659 seq_puts(m, "ws={\n");
660 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
661 struct sbq_wait_state *ws = &sbq->ws[i];
663 seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
664 atomic_read(&ws->wait_cnt),
665 waitqueue_active(&ws->wait) ? "active" : "inactive");
669 seq_printf(m, "round_robin=%d\n", sbq->round_robin);
670 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
672 EXPORT_SYMBOL_GPL(sbitmap_queue_show);
674 void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
675 struct sbq_wait_state *ws,
676 struct sbq_wait *sbq_wait)
678 if (!sbq_wait->sbq) {
680 atomic_inc(&sbq->ws_active);
682 add_wait_queue(&ws->wait, &sbq_wait->wait);
684 EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
686 void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
688 list_del_init(&sbq_wait->wait.entry);
690 atomic_dec(&sbq_wait->sbq->ws_active);
691 sbq_wait->sbq = NULL;
694 EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
696 void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
697 struct sbq_wait_state *ws,
698 struct sbq_wait *sbq_wait, int state)
700 if (!sbq_wait->sbq) {
701 atomic_inc(&sbq->ws_active);
704 prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
706 EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
708 void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
709 struct sbq_wait *sbq_wait)
711 finish_wait(&ws->wait, &sbq_wait->wait);
713 atomic_dec(&sbq->ws_active);
714 sbq_wait->sbq = NULL;
717 EXPORT_SYMBOL_GPL(sbitmap_finish_wait);