sbitmap: Protect swap_lock from softirqs
[linux-2.6-block.git] / lib / sbitmap.c
CommitLineData
88459642
OS
1/*
2 * Copyright (C) 2016 Facebook
3 * Copyright (C) 2013-2014 Jens Axboe
4 *
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.
8 *
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.
13 *
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/>.
16 */
17
af8601ad 18#include <linux/sched.h>
98d95416 19#include <linux/random.h>
88459642 20#include <linux/sbitmap.h>
24af1ccf 21#include <linux/seq_file.h>
88459642 22
b2dbff1b
JA
23/*
24 * See if we have deferred clears that we can batch move
25 */
26static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index)
27{
28 unsigned long mask, val;
b2dbff1b
JA
29 bool ret = false;
30
37198768 31 spin_lock_bh(&sb->map[index].swap_lock);
b2dbff1b
JA
32
33 if (!sb->map[index].cleared)
34 goto out_unlock;
35
36 /*
37 * First get a stable cleared mask, setting the old mask to 0.
38 */
39 do {
40 mask = sb->map[index].cleared;
41 } while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask);
42
43 /*
44 * Now clear the masked bits in our free word
45 */
46 do {
47 val = sb->map[index].word;
48 } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
49
50 ret = true;
51out_unlock:
37198768 52 spin_unlock_bh(&sb->map[index].swap_lock);
b2dbff1b
JA
53 return ret;
54}
55
88459642
OS
56int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
57 gfp_t flags, int node)
58{
59 unsigned int bits_per_word;
60 unsigned int i;
61
62 if (shift < 0) {
63 shift = ilog2(BITS_PER_LONG);
64 /*
65 * If the bitmap is small, shrink the number of bits per word so
66 * we spread over a few cachelines, at least. If less than 4
67 * bits, just forget about it, it's not going to work optimally
68 * anyway.
69 */
70 if (depth >= 4) {
71 while ((4U << shift) > depth)
72 shift--;
73 }
74 }
75 bits_per_word = 1U << shift;
76 if (bits_per_word > BITS_PER_LONG)
77 return -EINVAL;
78
79 sb->shift = shift;
80 sb->depth = depth;
81 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
82
83 if (depth == 0) {
84 sb->map = NULL;
85 return 0;
86 }
87
590b5b7d 88 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
88459642
OS
89 if (!sb->map)
90 return -ENOMEM;
91
92 for (i = 0; i < sb->map_nr; i++) {
93 sb->map[i].depth = min(depth, bits_per_word);
94 depth -= sb->map[i].depth;
ea86ea2c 95 spin_lock_init(&sb->map[i].swap_lock);
88459642
OS
96 }
97 return 0;
98}
99EXPORT_SYMBOL_GPL(sbitmap_init_node);
100
101void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
102{
103 unsigned int bits_per_word = 1U << sb->shift;
104 unsigned int i;
105
b2dbff1b
JA
106 for (i = 0; i < sb->map_nr; i++)
107 sbitmap_deferred_clear(sb, i);
108
88459642
OS
109 sb->depth = depth;
110 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
111
112 for (i = 0; i < sb->map_nr; i++) {
113 sb->map[i].depth = min(depth, bits_per_word);
114 depth -= sb->map[i].depth;
115 }
116}
117EXPORT_SYMBOL_GPL(sbitmap_resize);
118
c05e6673
OS
119static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
120 unsigned int hint, bool wrap)
88459642
OS
121{
122 unsigned int orig_hint = hint;
123 int nr;
124
125 while (1) {
c05e6673
OS
126 nr = find_next_zero_bit(word, depth, hint);
127 if (unlikely(nr >= depth)) {
88459642
OS
128 /*
129 * We started with an offset, and we didn't reset the
130 * offset to 0 in a failure case, so start from 0 to
131 * exhaust the map.
132 */
133 if (orig_hint && hint && wrap) {
134 hint = orig_hint = 0;
135 continue;
136 }
137 return -1;
138 }
139
4ace53f1 140 if (!test_and_set_bit_lock(nr, word))
88459642
OS
141 break;
142
143 hint = nr + 1;
c05e6673 144 if (hint >= depth - 1)
88459642
OS
145 hint = 0;
146 }
147
148 return nr;
149}
150
ea86ea2c
JA
151static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
152 unsigned int alloc_hint, bool round_robin)
153{
154 int nr;
155
156 do {
157 nr = __sbitmap_get_word(&sb->map[index].word,
158 sb->map[index].depth, alloc_hint,
159 !round_robin);
160 if (nr != -1)
161 break;
162 if (!sbitmap_deferred_clear(sb, index))
163 break;
164 } while (1);
165
166 return nr;
167}
168
88459642
OS
169int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
170{
171 unsigned int i, index;
172 int nr = -1;
173
174 index = SB_NR_TO_INDEX(sb, alloc_hint);
175
27fae429
JA
176 /*
177 * Unless we're doing round robin tag allocation, just use the
178 * alloc_hint to find the right word index. No point in looping
179 * twice in find_next_zero_bit() for that case.
180 */
181 if (round_robin)
182 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
183 else
184 alloc_hint = 0;
185
88459642 186 for (i = 0; i < sb->map_nr; i++) {
ea86ea2c
JA
187 nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
188 round_robin);
88459642
OS
189 if (nr != -1) {
190 nr += index << sb->shift;
191 break;
192 }
193
194 /* Jump to next index. */
27fae429
JA
195 alloc_hint = 0;
196 if (++index >= sb->map_nr)
88459642 197 index = 0;
88459642
OS
198 }
199
200 return nr;
201}
202EXPORT_SYMBOL_GPL(sbitmap_get);
203
c05e6673
OS
204int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
205 unsigned long shallow_depth)
206{
207 unsigned int i, index;
208 int nr = -1;
209
210 index = SB_NR_TO_INDEX(sb, alloc_hint);
211
212 for (i = 0; i < sb->map_nr; i++) {
b2dbff1b 213again:
c05e6673
OS
214 nr = __sbitmap_get_word(&sb->map[index].word,
215 min(sb->map[index].depth, shallow_depth),
216 SB_NR_TO_BIT(sb, alloc_hint), true);
217 if (nr != -1) {
218 nr += index << sb->shift;
219 break;
220 }
221
b2dbff1b
JA
222 if (sbitmap_deferred_clear(sb, index))
223 goto again;
224
c05e6673
OS
225 /* Jump to next index. */
226 index++;
227 alloc_hint = index << sb->shift;
228
229 if (index >= sb->map_nr) {
230 index = 0;
231 alloc_hint = 0;
232 }
233 }
234
235 return nr;
236}
237EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
238
88459642
OS
239bool sbitmap_any_bit_set(const struct sbitmap *sb)
240{
241 unsigned int i;
242
243 for (i = 0; i < sb->map_nr; i++) {
b2dbff1b 244 if (sb->map[i].word & ~sb->map[i].cleared)
88459642
OS
245 return true;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
250
251bool sbitmap_any_bit_clear(const struct sbitmap *sb)
252{
253 unsigned int i;
254
255 for (i = 0; i < sb->map_nr; i++) {
256 const struct sbitmap_word *word = &sb->map[i];
b2dbff1b 257 unsigned long mask = word->word & ~word->cleared;
88459642
OS
258 unsigned long ret;
259
b2dbff1b 260 ret = find_first_zero_bit(&mask, word->depth);
88459642
OS
261 if (ret < word->depth)
262 return true;
263 }
264 return false;
265}
266EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
267
ea86ea2c 268static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
88459642 269{
60658e0d 270 unsigned int i, weight = 0;
88459642
OS
271
272 for (i = 0; i < sb->map_nr; i++) {
273 const struct sbitmap_word *word = &sb->map[i];
274
ea86ea2c
JA
275 if (set)
276 weight += bitmap_weight(&word->word, word->depth);
277 else
278 weight += bitmap_weight(&word->cleared, word->depth);
88459642
OS
279 }
280 return weight;
281}
ea86ea2c
JA
282
283static unsigned int sbitmap_weight(const struct sbitmap *sb)
284{
285 return __sbitmap_weight(sb, true);
286}
287
288static unsigned int sbitmap_cleared(const struct sbitmap *sb)
289{
290 return __sbitmap_weight(sb, false);
291}
88459642 292
24af1ccf
OS
293void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
294{
295 seq_printf(m, "depth=%u\n", sb->depth);
ea86ea2c
JA
296 seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb));
297 seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
24af1ccf
OS
298 seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
299 seq_printf(m, "map_nr=%u\n", sb->map_nr);
300}
301EXPORT_SYMBOL_GPL(sbitmap_show);
302
303static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
304{
305 if ((offset & 0xf) == 0) {
306 if (offset != 0)
307 seq_putc(m, '\n');
308 seq_printf(m, "%08x:", offset);
309 }
310 if ((offset & 0x1) == 0)
311 seq_putc(m, ' ');
312 seq_printf(m, "%02x", byte);
313}
314
315void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
316{
317 u8 byte = 0;
318 unsigned int byte_bits = 0;
319 unsigned int offset = 0;
320 int i;
321
322 for (i = 0; i < sb->map_nr; i++) {
323 unsigned long word = READ_ONCE(sb->map[i].word);
324 unsigned int word_bits = READ_ONCE(sb->map[i].depth);
325
326 while (word_bits > 0) {
327 unsigned int bits = min(8 - byte_bits, word_bits);
328
329 byte |= (word & (BIT(bits) - 1)) << byte_bits;
330 byte_bits += bits;
331 if (byte_bits == 8) {
332 emit_byte(m, offset, byte);
333 byte = 0;
334 byte_bits = 0;
335 offset++;
336 }
337 word >>= bits;
338 word_bits -= bits;
339 }
340 }
341 if (byte_bits) {
342 emit_byte(m, offset, byte);
343 offset++;
344 }
345 if (offset)
346 seq_putc(m, '\n');
347}
348EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
349
a3275539
OS
350static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
351 unsigned int depth)
88459642
OS
352{
353 unsigned int wake_batch;
a3275539 354 unsigned int shallow_depth;
88459642
OS
355
356 /*
357 * For each batch, we wake up one queue. We need to make sure that our
a3275539
OS
358 * batch size is small enough that the full depth of the bitmap,
359 * potentially limited by a shallow depth, is enough to wake up all of
360 * the queues.
361 *
362 * Each full word of the bitmap has bits_per_word bits, and there might
363 * be a partial word. There are depth / bits_per_word full words and
364 * depth % bits_per_word bits left over. In bitwise arithmetic:
365 *
366 * bits_per_word = 1 << shift
367 * depth / bits_per_word = depth >> shift
368 * depth % bits_per_word = depth & ((1 << shift) - 1)
369 *
370 * Each word can be limited to sbq->min_shallow_depth bits.
88459642 371 */
a3275539
OS
372 shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
373 depth = ((depth >> sbq->sb.shift) * shallow_depth +
374 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
375 wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
376 SBQ_WAKE_BATCH);
88459642
OS
377
378 return wake_batch;
379}
380
381int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
f4a644db 382 int shift, bool round_robin, gfp_t flags, int node)
88459642
OS
383{
384 int ret;
385 int i;
386
387 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
388 if (ret)
389 return ret;
390
40aabb67
OS
391 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
392 if (!sbq->alloc_hint) {
393 sbitmap_free(&sbq->sb);
394 return -ENOMEM;
395 }
396
98d95416
OS
397 if (depth && !round_robin) {
398 for_each_possible_cpu(i)
399 *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
400 }
401
a3275539
OS
402 sbq->min_shallow_depth = UINT_MAX;
403 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
88459642 404 atomic_set(&sbq->wake_index, 0);
5d2ee712 405 atomic_set(&sbq->ws_active, 0);
88459642 406
48e28166 407 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
88459642 408 if (!sbq->ws) {
40aabb67 409 free_percpu(sbq->alloc_hint);
88459642
OS
410 sbitmap_free(&sbq->sb);
411 return -ENOMEM;
412 }
413
414 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
415 init_waitqueue_head(&sbq->ws[i].wait);
416 atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
417 }
f4a644db
OS
418
419 sbq->round_robin = round_robin;
88459642
OS
420 return 0;
421}
422EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
423
a3275539
OS
424static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
425 unsigned int depth)
88459642 426{
a3275539 427 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
6c0ca7ae
OS
428 int i;
429
430 if (sbq->wake_batch != wake_batch) {
431 WRITE_ONCE(sbq->wake_batch, wake_batch);
432 /*
e6fc4649
ML
433 * Pairs with the memory barrier in sbitmap_queue_wake_up()
434 * to ensure that the batch size is updated before the wait
435 * counts.
6c0ca7ae
OS
436 */
437 smp_mb__before_atomic();
438 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
439 atomic_set(&sbq->ws[i].wait_cnt, 1);
440 }
a3275539
OS
441}
442
443void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
444{
445 sbitmap_queue_update_wake_batch(sbq, depth);
88459642
OS
446 sbitmap_resize(&sbq->sb, depth);
447}
448EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
449
f4a644db 450int __sbitmap_queue_get(struct sbitmap_queue *sbq)
40aabb67 451{
05fd095d 452 unsigned int hint, depth;
40aabb67
OS
453 int nr;
454
455 hint = this_cpu_read(*sbq->alloc_hint);
05fd095d
OS
456 depth = READ_ONCE(sbq->sb.depth);
457 if (unlikely(hint >= depth)) {
458 hint = depth ? prandom_u32() % depth : 0;
459 this_cpu_write(*sbq->alloc_hint, hint);
460 }
f4a644db 461 nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
40aabb67
OS
462
463 if (nr == -1) {
464 /* If the map is full, a hint won't do us much good. */
465 this_cpu_write(*sbq->alloc_hint, 0);
f4a644db 466 } else if (nr == hint || unlikely(sbq->round_robin)) {
40aabb67
OS
467 /* Only update the hint if we used it. */
468 hint = nr + 1;
05fd095d 469 if (hint >= depth - 1)
40aabb67
OS
470 hint = 0;
471 this_cpu_write(*sbq->alloc_hint, hint);
472 }
473
474 return nr;
475}
476EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
477
c05e6673
OS
478int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
479 unsigned int shallow_depth)
480{
481 unsigned int hint, depth;
482 int nr;
483
61445b56
OS
484 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
485
c05e6673
OS
486 hint = this_cpu_read(*sbq->alloc_hint);
487 depth = READ_ONCE(sbq->sb.depth);
488 if (unlikely(hint >= depth)) {
489 hint = depth ? prandom_u32() % depth : 0;
490 this_cpu_write(*sbq->alloc_hint, hint);
491 }
492 nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
493
494 if (nr == -1) {
495 /* If the map is full, a hint won't do us much good. */
496 this_cpu_write(*sbq->alloc_hint, 0);
497 } else if (nr == hint || unlikely(sbq->round_robin)) {
498 /* Only update the hint if we used it. */
499 hint = nr + 1;
500 if (hint >= depth - 1)
501 hint = 0;
502 this_cpu_write(*sbq->alloc_hint, hint);
503 }
504
505 return nr;
506}
507EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
508
a3275539
OS
509void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
510 unsigned int min_shallow_depth)
511{
512 sbq->min_shallow_depth = min_shallow_depth;
513 sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
514}
515EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
516
88459642
OS
517static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
518{
519 int i, wake_index;
520
5d2ee712
JA
521 if (!atomic_read(&sbq->ws_active))
522 return NULL;
523
88459642
OS
524 wake_index = atomic_read(&sbq->wake_index);
525 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
526 struct sbq_wait_state *ws = &sbq->ws[wake_index];
527
528 if (waitqueue_active(&ws->wait)) {
529 int o = atomic_read(&sbq->wake_index);
530
531 if (wake_index != o)
532 atomic_cmpxchg(&sbq->wake_index, o, wake_index);
533 return ws;
534 }
535
536 wake_index = sbq_index_inc(wake_index);
537 }
538
539 return NULL;
540}
541
c854ab57 542static bool __sbq_wake_up(struct sbitmap_queue *sbq)
88459642
OS
543{
544 struct sbq_wait_state *ws;
6c0ca7ae 545 unsigned int wake_batch;
88459642
OS
546 int wait_cnt;
547
88459642
OS
548 ws = sbq_wake_ptr(sbq);
549 if (!ws)
c854ab57 550 return false;
88459642
OS
551
552 wait_cnt = atomic_dec_return(&ws->wait_cnt);
6c0ca7ae 553 if (wait_cnt <= 0) {
c854ab57
JA
554 int ret;
555
6c0ca7ae 556 wake_batch = READ_ONCE(sbq->wake_batch);
c854ab57 557
6c0ca7ae
OS
558 /*
559 * Pairs with the memory barrier in sbitmap_queue_resize() to
560 * ensure that we see the batch size update before the wait
561 * count is reset.
562 */
563 smp_mb__before_atomic();
c854ab57 564
6c0ca7ae 565 /*
c854ab57
JA
566 * For concurrent callers of this, the one that failed the
567 * atomic_cmpxhcg() race should call this function again
568 * to wakeup a new batch on a different 'ws'.
6c0ca7ae 569 */
c854ab57
JA
570 ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
571 if (ret == wait_cnt) {
572 sbq_index_atomic_inc(&sbq->wake_index);
573 wake_up_nr(&ws->wait, wake_batch);
574 return false;
575 }
576
577 return true;
88459642 578 }
c854ab57
JA
579
580 return false;
581}
582
e6fc4649 583void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
c854ab57
JA
584{
585 while (__sbq_wake_up(sbq))
586 ;
88459642 587}
e6fc4649 588EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
88459642 589
40aabb67 590void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
f4a644db 591 unsigned int cpu)
88459642 592{
ea86ea2c
JA
593 sbitmap_deferred_clear_bit(&sbq->sb, nr);
594
e6fc4649
ML
595 /*
596 * Pairs with the memory barrier in set_current_state() to ensure the
597 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
598 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
599 * waiter. See the comment on waitqueue_active().
600 */
601 smp_mb__after_atomic();
602 sbitmap_queue_wake_up(sbq);
603
5c64a8df 604 if (likely(!sbq->round_robin && nr < sbq->sb.depth))
40aabb67 605 *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
88459642
OS
606}
607EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
608
609void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
610{
611 int i, wake_index;
612
613 /*
f66227de 614 * Pairs with the memory barrier in set_current_state() like in
e6fc4649 615 * sbitmap_queue_wake_up().
88459642
OS
616 */
617 smp_mb();
618 wake_index = atomic_read(&sbq->wake_index);
619 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
620 struct sbq_wait_state *ws = &sbq->ws[wake_index];
621
622 if (waitqueue_active(&ws->wait))
623 wake_up(&ws->wait);
624
625 wake_index = sbq_index_inc(wake_index);
626 }
627}
628EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
24af1ccf
OS
629
630void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
631{
632 bool first;
633 int i;
634
635 sbitmap_show(&sbq->sb, m);
636
637 seq_puts(m, "alloc_hint={");
638 first = true;
639 for_each_possible_cpu(i) {
640 if (!first)
641 seq_puts(m, ", ");
642 first = false;
643 seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
644 }
645 seq_puts(m, "}\n");
646
647 seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
648 seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
5d2ee712 649 seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
24af1ccf
OS
650
651 seq_puts(m, "ws={\n");
652 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
653 struct sbq_wait_state *ws = &sbq->ws[i];
654
655 seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
656 atomic_read(&ws->wait_cnt),
657 waitqueue_active(&ws->wait) ? "active" : "inactive");
658 }
659 seq_puts(m, "}\n");
660
661 seq_printf(m, "round_robin=%d\n", sbq->round_robin);
a3275539 662 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
24af1ccf
OS
663}
664EXPORT_SYMBOL_GPL(sbitmap_queue_show);
5d2ee712 665
9f6b7ef6
JA
666void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
667 struct sbq_wait_state *ws,
668 struct sbq_wait *sbq_wait)
669{
670 if (!sbq_wait->sbq) {
671 sbq_wait->sbq = sbq;
672 atomic_inc(&sbq->ws_active);
673 }
674 add_wait_queue(&ws->wait, &sbq_wait->wait);
675}
676EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
677
678void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
679{
680 list_del_init(&sbq_wait->wait.entry);
681 if (sbq_wait->sbq) {
682 atomic_dec(&sbq_wait->sbq->ws_active);
683 sbq_wait->sbq = NULL;
684 }
685}
686EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
687
5d2ee712
JA
688void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
689 struct sbq_wait_state *ws,
690 struct sbq_wait *sbq_wait, int state)
691{
9f6b7ef6 692 if (!sbq_wait->sbq) {
5d2ee712 693 atomic_inc(&sbq->ws_active);
9f6b7ef6 694 sbq_wait->sbq = sbq;
5d2ee712
JA
695 }
696 prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
697}
698EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
699
700void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
701 struct sbq_wait *sbq_wait)
702{
703 finish_wait(&ws->wait, &sbq_wait->wait);
9f6b7ef6 704 if (sbq_wait->sbq) {
5d2ee712 705 atomic_dec(&sbq->ws_active);
9f6b7ef6 706 sbq_wait->sbq = NULL;
5d2ee712
JA
707 }
708}
709EXPORT_SYMBOL_GPL(sbitmap_finish_wait);