crypto: cavium/nitrox - no need to check return value of debugfs_create functions
[linux/fpc-iii.git] / lib / sbitmap.c
blob65c2d06250a63acd453614011fa42ccd55836beb
1 /*
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;
30 bool ret = false;
32 /* Silence bogus lockdep warning */
33 #if defined(CONFIG_LOCKDEP)
34 local_irq_save(flags);
35 #endif
36 spin_lock(&sb->map[index].swap_lock);
38 if (!sb->map[index].cleared)
39 goto out_unlock;
42 * First get a stable cleared mask, setting the old mask to 0.
44 do {
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
51 do {
52 val = sb->map[index].word;
53 } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
55 ret = true;
56 out_unlock:
57 spin_unlock(&sb->map[index].swap_lock);
58 #if defined(CONFIG_LOCKDEP)
59 local_irq_restore(flags);
60 #endif
61 return ret;
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;
68 unsigned int i;
70 if (shift < 0) {
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
76 * anyway.
78 if (depth >= 4) {
79 while ((4U << shift) > depth)
80 shift--;
83 bits_per_word = 1U << shift;
84 if (bits_per_word > BITS_PER_LONG)
85 return -EINVAL;
87 sb->shift = shift;
88 sb->depth = depth;
89 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
91 if (depth == 0) {
92 sb->map = NULL;
93 return 0;
96 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
97 if (!sb->map)
98 return -ENOMEM;
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);
105 return 0;
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;
112 unsigned int i;
114 for (i = 0; i < sb->map_nr; i++)
115 sbitmap_deferred_clear(sb, i);
117 sb->depth = depth;
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;
131 int nr;
133 while (1) {
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
139 * exhaust the map.
141 if (orig_hint && hint && wrap) {
142 hint = orig_hint = 0;
143 continue;
145 return -1;
148 if (!test_and_set_bit_lock(nr, word))
149 break;
151 hint = nr + 1;
152 if (hint >= depth - 1)
153 hint = 0;
156 return nr;
159 static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
160 unsigned int alloc_hint, bool round_robin)
162 int nr;
164 do {
165 nr = __sbitmap_get_word(&sb->map[index].word,
166 sb->map[index].depth, alloc_hint,
167 !round_robin);
168 if (nr != -1)
169 break;
170 if (!sbitmap_deferred_clear(sb, index))
171 break;
172 } while (1);
174 return nr;
177 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
179 unsigned int i, index;
180 int nr = -1;
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.
189 if (round_robin)
190 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
191 else
192 alloc_hint = 0;
194 for (i = 0; i < sb->map_nr; i++) {
195 nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
196 round_robin);
197 if (nr != -1) {
198 nr += index << sb->shift;
199 break;
202 /* Jump to next index. */
203 alloc_hint = 0;
204 if (++index >= sb->map_nr)
205 index = 0;
208 return 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;
216 int nr = -1;
218 index = SB_NR_TO_INDEX(sb, alloc_hint);
220 for (i = 0; i < sb->map_nr; i++) {
221 again:
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);
225 if (nr != -1) {
226 nr += index << sb->shift;
227 break;
230 if (sbitmap_deferred_clear(sb, index))
231 goto again;
233 /* Jump to next index. */
234 index++;
235 alloc_hint = index << sb->shift;
237 if (index >= sb->map_nr) {
238 index = 0;
239 alloc_hint = 0;
243 return nr;
245 EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
247 bool sbitmap_any_bit_set(const struct sbitmap *sb)
249 unsigned int i;
251 for (i = 0; i < sb->map_nr; i++) {
252 if (sb->map[i].word & ~sb->map[i].cleared)
253 return true;
255 return false;
257 EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
259 bool sbitmap_any_bit_clear(const struct sbitmap *sb)
261 unsigned int i;
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;
266 unsigned long ret;
268 ret = find_first_zero_bit(&mask, word->depth);
269 if (ret < word->depth)
270 return true;
272 return false;
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];
283 if (set)
284 weight += bitmap_weight(&word->word, word->depth);
285 else
286 weight += bitmap_weight(&word->cleared, word->depth);
288 return weight;
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) {
314 if (offset != 0)
315 seq_putc(m, '\n');
316 seq_printf(m, "%08x:", offset);
318 if ((offset & 0x1) == 0)
319 seq_putc(m, ' ');
320 seq_printf(m, "%02x", byte);
323 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
325 u8 byte = 0;
326 unsigned int byte_bits = 0;
327 unsigned int offset = 0;
328 int i;
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;
338 byte_bits += bits;
339 if (byte_bits == 8) {
340 emit_byte(m, offset, byte);
341 byte = 0;
342 byte_bits = 0;
343 offset++;
345 word >>= bits;
346 word_bits -= bits;
349 if (byte_bits) {
350 emit_byte(m, offset, byte);
351 offset++;
353 if (offset)
354 seq_putc(m, '\n');
356 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
358 static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
359 unsigned int depth)
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
368 * the queues.
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,
384 SBQ_WAKE_BATCH);
386 return wake_batch;
389 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
390 int shift, bool round_robin, gfp_t flags, int node)
392 int ret;
393 int i;
395 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
396 if (ret)
397 return ret;
399 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
400 if (!sbq->alloc_hint) {
401 sbitmap_free(&sbq->sb);
402 return -ENOMEM;
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);
416 if (!sbq->ws) {
417 free_percpu(sbq->alloc_hint);
418 sbitmap_free(&sbq->sb);
419 return -ENOMEM;
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;
428 return 0;
430 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
432 static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
433 unsigned int depth)
435 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
436 int i;
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
443 * counts.
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;
461 int nr;
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);
471 if (nr == -1) {
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. */
476 hint = nr + 1;
477 if (hint >= depth - 1)
478 hint = 0;
479 this_cpu_write(*sbq->alloc_hint, hint);
482 return nr;
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;
490 int nr;
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);
502 if (nr == -1) {
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. */
507 hint = nr + 1;
508 if (hint >= depth - 1)
509 hint = 0;
510 this_cpu_write(*sbq->alloc_hint, hint);
513 return nr;
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)
527 int i, wake_index;
529 if (!atomic_read(&sbq->ws_active))
530 return NULL;
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);
539 if (wake_index != o)
540 atomic_cmpxchg(&sbq->wake_index, o, wake_index);
541 return ws;
544 wake_index = sbq_index_inc(wake_index);
547 return NULL;
550 static bool __sbq_wake_up(struct sbitmap_queue *sbq)
552 struct sbq_wait_state *ws;
553 unsigned int wake_batch;
554 int wait_cnt;
556 ws = sbq_wake_ptr(sbq);
557 if (!ws)
558 return false;
560 wait_cnt = atomic_dec_return(&ws->wait_cnt);
561 if (wait_cnt <= 0) {
562 int ret;
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
569 * count is reset.
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);
582 return false;
585 return true;
588 return false;
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,
599 unsigned int cpu)
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)
619 int i, wake_index;
622 * Pairs with the memory barrier in set_current_state() like in
623 * sbitmap_queue_wake_up().
625 smp_mb();
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))
631 wake_up(&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)
640 bool first;
641 int i;
643 sbitmap_show(&sbq->sb, m);
645 seq_puts(m, "alloc_hint={");
646 first = true;
647 for_each_possible_cpu(i) {
648 if (!first)
649 seq_puts(m, ", ");
650 first = false;
651 seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
653 seq_puts(m, "}\n");
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");
667 seq_puts(m, "}\n");
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) {
679 sbq_wait->sbq = 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);
689 if (sbq_wait->sbq) {
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);
702 sbq_wait->sbq = sbq;
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);
712 if (sbq_wait->sbq) {
713 atomic_dec(&sbq->ws_active);
714 sbq_wait->sbq = NULL;
717 EXPORT_SYMBOL_GPL(sbitmap_finish_wait);