blob: 45cab6bbc1c758b53b1cdc98a02830aee7a0ae4d [file] [log] [blame]
Omar Sandoval88459642016-09-17 08:38:44 -06001/*
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
Ingo Molnaraf8601a2017-02-03 09:57:00 +010018#include <linux/sched.h>
Omar Sandoval98d95412016-09-17 01:28:25 -070019#include <linux/random.h>
Omar Sandoval88459642016-09-17 08:38:44 -060020#include <linux/sbitmap.h>
Omar Sandoval24af1ccf2017-01-25 14:32:13 -080021#include <linux/seq_file.h>
Omar Sandoval88459642016-09-17 08:38:44 -060022
23int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
24 gfp_t flags, int node)
25{
26 unsigned int bits_per_word;
27 unsigned int i;
28
29 if (shift < 0) {
30 shift = ilog2(BITS_PER_LONG);
31 /*
32 * If the bitmap is small, shrink the number of bits per word so
33 * we spread over a few cachelines, at least. If less than 4
34 * bits, just forget about it, it's not going to work optimally
35 * anyway.
36 */
37 if (depth >= 4) {
38 while ((4U << shift) > depth)
39 shift--;
40 }
41 }
42 bits_per_word = 1U << shift;
43 if (bits_per_word > BITS_PER_LONG)
44 return -EINVAL;
45
46 sb->shift = shift;
47 sb->depth = depth;
48 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
49
50 if (depth == 0) {
51 sb->map = NULL;
52 return 0;
53 }
54
Kees Cook590b5b72018-06-12 14:04:20 -070055 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
Omar Sandoval88459642016-09-17 08:38:44 -060056 if (!sb->map)
57 return -ENOMEM;
58
59 for (i = 0; i < sb->map_nr; i++) {
60 sb->map[i].depth = min(depth, bits_per_word);
61 depth -= sb->map[i].depth;
62 }
63 return 0;
64}
65EXPORT_SYMBOL_GPL(sbitmap_init_node);
66
67void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
68{
69 unsigned int bits_per_word = 1U << sb->shift;
70 unsigned int i;
71
72 sb->depth = depth;
73 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
74
75 for (i = 0; i < sb->map_nr; i++) {
76 sb->map[i].depth = min(depth, bits_per_word);
77 depth -= sb->map[i].depth;
78 }
79}
80EXPORT_SYMBOL_GPL(sbitmap_resize);
81
Omar Sandovalc05e6672017-04-14 00:59:58 -070082static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
83 unsigned int hint, bool wrap)
Omar Sandoval88459642016-09-17 08:38:44 -060084{
85 unsigned int orig_hint = hint;
86 int nr;
87
88 while (1) {
Omar Sandovalc05e6672017-04-14 00:59:58 -070089 nr = find_next_zero_bit(word, depth, hint);
90 if (unlikely(nr >= depth)) {
Omar Sandoval88459642016-09-17 08:38:44 -060091 /*
92 * We started with an offset, and we didn't reset the
93 * offset to 0 in a failure case, so start from 0 to
94 * exhaust the map.
95 */
96 if (orig_hint && hint && wrap) {
97 hint = orig_hint = 0;
98 continue;
99 }
100 return -1;
101 }
102
Omar Sandoval4ace53f2018-02-27 16:56:43 -0800103 if (!test_and_set_bit_lock(nr, word))
Omar Sandoval88459642016-09-17 08:38:44 -0600104 break;
105
106 hint = nr + 1;
Omar Sandovalc05e6672017-04-14 00:59:58 -0700107 if (hint >= depth - 1)
Omar Sandoval88459642016-09-17 08:38:44 -0600108 hint = 0;
109 }
110
111 return nr;
112}
113
114int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
115{
116 unsigned int i, index;
117 int nr = -1;
118
119 index = SB_NR_TO_INDEX(sb, alloc_hint);
120
Jens Axboe27fae422018-11-29 12:35:16 -0700121 /*
122 * Unless we're doing round robin tag allocation, just use the
123 * alloc_hint to find the right word index. No point in looping
124 * twice in find_next_zero_bit() for that case.
125 */
126 if (round_robin)
127 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
128 else
129 alloc_hint = 0;
130
Omar Sandoval88459642016-09-17 08:38:44 -0600131 for (i = 0; i < sb->map_nr; i++) {
Omar Sandovalc05e6672017-04-14 00:59:58 -0700132 nr = __sbitmap_get_word(&sb->map[index].word,
Jens Axboe27fae422018-11-29 12:35:16 -0700133 sb->map[index].depth, alloc_hint,
Omar Sandoval88459642016-09-17 08:38:44 -0600134 !round_robin);
135 if (nr != -1) {
136 nr += index << sb->shift;
137 break;
138 }
139
140 /* Jump to next index. */
Jens Axboe27fae422018-11-29 12:35:16 -0700141 alloc_hint = 0;
142 if (++index >= sb->map_nr)
Omar Sandoval88459642016-09-17 08:38:44 -0600143 index = 0;
Omar Sandoval88459642016-09-17 08:38:44 -0600144 }
145
146 return nr;
147}
148EXPORT_SYMBOL_GPL(sbitmap_get);
149
Omar Sandovalc05e6672017-04-14 00:59:58 -0700150int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
151 unsigned long shallow_depth)
152{
153 unsigned int i, index;
154 int nr = -1;
155
156 index = SB_NR_TO_INDEX(sb, alloc_hint);
157
158 for (i = 0; i < sb->map_nr; i++) {
159 nr = __sbitmap_get_word(&sb->map[index].word,
160 min(sb->map[index].depth, shallow_depth),
161 SB_NR_TO_BIT(sb, alloc_hint), true);
162 if (nr != -1) {
163 nr += index << sb->shift;
164 break;
165 }
166
167 /* Jump to next index. */
168 index++;
169 alloc_hint = index << sb->shift;
170
171 if (index >= sb->map_nr) {
172 index = 0;
173 alloc_hint = 0;
174 }
175 }
176
177 return nr;
178}
179EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
180
Omar Sandoval88459642016-09-17 08:38:44 -0600181bool sbitmap_any_bit_set(const struct sbitmap *sb)
182{
183 unsigned int i;
184
185 for (i = 0; i < sb->map_nr; i++) {
186 if (sb->map[i].word)
187 return true;
188 }
189 return false;
190}
191EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
192
193bool sbitmap_any_bit_clear(const struct sbitmap *sb)
194{
195 unsigned int i;
196
197 for (i = 0; i < sb->map_nr; i++) {
198 const struct sbitmap_word *word = &sb->map[i];
199 unsigned long ret;
200
201 ret = find_first_zero_bit(&word->word, word->depth);
202 if (ret < word->depth)
203 return true;
204 }
205 return false;
206}
207EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
208
209unsigned int sbitmap_weight(const struct sbitmap *sb)
210{
Colin Ian King60658e02016-09-19 14:34:08 +0100211 unsigned int i, weight = 0;
Omar Sandoval88459642016-09-17 08:38:44 -0600212
213 for (i = 0; i < sb->map_nr; i++) {
214 const struct sbitmap_word *word = &sb->map[i];
215
216 weight += bitmap_weight(&word->word, word->depth);
217 }
218 return weight;
219}
220EXPORT_SYMBOL_GPL(sbitmap_weight);
221
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800222void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
223{
224 seq_printf(m, "depth=%u\n", sb->depth);
225 seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
226 seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
227 seq_printf(m, "map_nr=%u\n", sb->map_nr);
228}
229EXPORT_SYMBOL_GPL(sbitmap_show);
230
231static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
232{
233 if ((offset & 0xf) == 0) {
234 if (offset != 0)
235 seq_putc(m, '\n');
236 seq_printf(m, "%08x:", offset);
237 }
238 if ((offset & 0x1) == 0)
239 seq_putc(m, ' ');
240 seq_printf(m, "%02x", byte);
241}
242
243void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
244{
245 u8 byte = 0;
246 unsigned int byte_bits = 0;
247 unsigned int offset = 0;
248 int i;
249
250 for (i = 0; i < sb->map_nr; i++) {
251 unsigned long word = READ_ONCE(sb->map[i].word);
252 unsigned int word_bits = READ_ONCE(sb->map[i].depth);
253
254 while (word_bits > 0) {
255 unsigned int bits = min(8 - byte_bits, word_bits);
256
257 byte |= (word & (BIT(bits) - 1)) << byte_bits;
258 byte_bits += bits;
259 if (byte_bits == 8) {
260 emit_byte(m, offset, byte);
261 byte = 0;
262 byte_bits = 0;
263 offset++;
264 }
265 word >>= bits;
266 word_bits -= bits;
267 }
268 }
269 if (byte_bits) {
270 emit_byte(m, offset, byte);
271 offset++;
272 }
273 if (offset)
274 seq_putc(m, '\n');
275}
276EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
277
Omar Sandovala3275532018-05-09 17:16:31 -0700278static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
279 unsigned int depth)
Omar Sandoval88459642016-09-17 08:38:44 -0600280{
281 unsigned int wake_batch;
Omar Sandovala3275532018-05-09 17:16:31 -0700282 unsigned int shallow_depth;
Omar Sandoval88459642016-09-17 08:38:44 -0600283
284 /*
285 * For each batch, we wake up one queue. We need to make sure that our
Omar Sandovala3275532018-05-09 17:16:31 -0700286 * batch size is small enough that the full depth of the bitmap,
287 * potentially limited by a shallow depth, is enough to wake up all of
288 * the queues.
289 *
290 * Each full word of the bitmap has bits_per_word bits, and there might
291 * be a partial word. There are depth / bits_per_word full words and
292 * depth % bits_per_word bits left over. In bitwise arithmetic:
293 *
294 * bits_per_word = 1 << shift
295 * depth / bits_per_word = depth >> shift
296 * depth % bits_per_word = depth & ((1 << shift) - 1)
297 *
298 * Each word can be limited to sbq->min_shallow_depth bits.
Omar Sandoval88459642016-09-17 08:38:44 -0600299 */
Omar Sandovala3275532018-05-09 17:16:31 -0700300 shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
301 depth = ((depth >> sbq->sb.shift) * shallow_depth +
302 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
303 wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
304 SBQ_WAKE_BATCH);
Omar Sandoval88459642016-09-17 08:38:44 -0600305
306 return wake_batch;
307}
308
309int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700310 int shift, bool round_robin, gfp_t flags, int node)
Omar Sandoval88459642016-09-17 08:38:44 -0600311{
312 int ret;
313 int i;
314
315 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
316 if (ret)
317 return ret;
318
Omar Sandoval40aabb62016-09-17 01:28:23 -0700319 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
320 if (!sbq->alloc_hint) {
321 sbitmap_free(&sbq->sb);
322 return -ENOMEM;
323 }
324
Omar Sandoval98d95412016-09-17 01:28:25 -0700325 if (depth && !round_robin) {
326 for_each_possible_cpu(i)
327 *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
328 }
329
Omar Sandovala3275532018-05-09 17:16:31 -0700330 sbq->min_shallow_depth = UINT_MAX;
331 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
Omar Sandoval88459642016-09-17 08:38:44 -0600332 atomic_set(&sbq->wake_index, 0);
333
Omar Sandoval48e28162016-09-17 01:28:22 -0700334 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
Omar Sandoval88459642016-09-17 08:38:44 -0600335 if (!sbq->ws) {
Omar Sandoval40aabb62016-09-17 01:28:23 -0700336 free_percpu(sbq->alloc_hint);
Omar Sandoval88459642016-09-17 08:38:44 -0600337 sbitmap_free(&sbq->sb);
338 return -ENOMEM;
339 }
340
341 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
342 init_waitqueue_head(&sbq->ws[i].wait);
343 atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
344 }
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700345
346 sbq->round_robin = round_robin;
Omar Sandoval88459642016-09-17 08:38:44 -0600347 return 0;
348}
349EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
350
Omar Sandovala3275532018-05-09 17:16:31 -0700351static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
352 unsigned int depth)
Omar Sandoval88459642016-09-17 08:38:44 -0600353{
Omar Sandovala3275532018-05-09 17:16:31 -0700354 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800355 int i;
356
357 if (sbq->wake_batch != wake_batch) {
358 WRITE_ONCE(sbq->wake_batch, wake_batch);
359 /*
Ming Leie6fc4642018-05-24 11:00:39 -0600360 * Pairs with the memory barrier in sbitmap_queue_wake_up()
361 * to ensure that the batch size is updated before the wait
362 * counts.
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800363 */
364 smp_mb__before_atomic();
365 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
366 atomic_set(&sbq->ws[i].wait_cnt, 1);
367 }
Omar Sandovala3275532018-05-09 17:16:31 -0700368}
369
370void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
371{
372 sbitmap_queue_update_wake_batch(sbq, depth);
Omar Sandoval88459642016-09-17 08:38:44 -0600373 sbitmap_resize(&sbq->sb, depth);
374}
375EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
376
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700377int __sbitmap_queue_get(struct sbitmap_queue *sbq)
Omar Sandoval40aabb62016-09-17 01:28:23 -0700378{
Omar Sandoval05fd0952016-09-17 01:28:26 -0700379 unsigned int hint, depth;
Omar Sandoval40aabb62016-09-17 01:28:23 -0700380 int nr;
381
382 hint = this_cpu_read(*sbq->alloc_hint);
Omar Sandoval05fd0952016-09-17 01:28:26 -0700383 depth = READ_ONCE(sbq->sb.depth);
384 if (unlikely(hint >= depth)) {
385 hint = depth ? prandom_u32() % depth : 0;
386 this_cpu_write(*sbq->alloc_hint, hint);
387 }
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700388 nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700389
390 if (nr == -1) {
391 /* If the map is full, a hint won't do us much good. */
392 this_cpu_write(*sbq->alloc_hint, 0);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700393 } else if (nr == hint || unlikely(sbq->round_robin)) {
Omar Sandoval40aabb62016-09-17 01:28:23 -0700394 /* Only update the hint if we used it. */
395 hint = nr + 1;
Omar Sandoval05fd0952016-09-17 01:28:26 -0700396 if (hint >= depth - 1)
Omar Sandoval40aabb62016-09-17 01:28:23 -0700397 hint = 0;
398 this_cpu_write(*sbq->alloc_hint, hint);
399 }
400
401 return nr;
402}
403EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
404
Omar Sandovalc05e6672017-04-14 00:59:58 -0700405int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
406 unsigned int shallow_depth)
407{
408 unsigned int hint, depth;
409 int nr;
410
Omar Sandoval61445b562018-05-09 17:29:24 -0700411 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
412
Omar Sandovalc05e6672017-04-14 00:59:58 -0700413 hint = this_cpu_read(*sbq->alloc_hint);
414 depth = READ_ONCE(sbq->sb.depth);
415 if (unlikely(hint >= depth)) {
416 hint = depth ? prandom_u32() % depth : 0;
417 this_cpu_write(*sbq->alloc_hint, hint);
418 }
419 nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
420
421 if (nr == -1) {
422 /* If the map is full, a hint won't do us much good. */
423 this_cpu_write(*sbq->alloc_hint, 0);
424 } else if (nr == hint || unlikely(sbq->round_robin)) {
425 /* Only update the hint if we used it. */
426 hint = nr + 1;
427 if (hint >= depth - 1)
428 hint = 0;
429 this_cpu_write(*sbq->alloc_hint, hint);
430 }
431
432 return nr;
433}
434EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
435
Omar Sandovala3275532018-05-09 17:16:31 -0700436void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
437 unsigned int min_shallow_depth)
438{
439 sbq->min_shallow_depth = min_shallow_depth;
440 sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
441}
442EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
443
Omar Sandoval88459642016-09-17 08:38:44 -0600444static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
445{
446 int i, wake_index;
447
448 wake_index = atomic_read(&sbq->wake_index);
449 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
450 struct sbq_wait_state *ws = &sbq->ws[wake_index];
451
452 if (waitqueue_active(&ws->wait)) {
453 int o = atomic_read(&sbq->wake_index);
454
455 if (wake_index != o)
456 atomic_cmpxchg(&sbq->wake_index, o, wake_index);
457 return ws;
458 }
459
460 wake_index = sbq_index_inc(wake_index);
461 }
462
463 return NULL;
464}
465
Jens Axboec854ab52018-05-14 12:17:31 -0600466static bool __sbq_wake_up(struct sbitmap_queue *sbq)
Omar Sandoval88459642016-09-17 08:38:44 -0600467{
468 struct sbq_wait_state *ws;
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800469 unsigned int wake_batch;
Omar Sandoval88459642016-09-17 08:38:44 -0600470 int wait_cnt;
471
Omar Sandoval88459642016-09-17 08:38:44 -0600472 ws = sbq_wake_ptr(sbq);
473 if (!ws)
Jens Axboec854ab52018-05-14 12:17:31 -0600474 return false;
Omar Sandoval88459642016-09-17 08:38:44 -0600475
476 wait_cnt = atomic_dec_return(&ws->wait_cnt);
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800477 if (wait_cnt <= 0) {
Jens Axboec854ab52018-05-14 12:17:31 -0600478 int ret;
479
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800480 wake_batch = READ_ONCE(sbq->wake_batch);
Jens Axboec854ab52018-05-14 12:17:31 -0600481
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800482 /*
483 * Pairs with the memory barrier in sbitmap_queue_resize() to
484 * ensure that we see the batch size update before the wait
485 * count is reset.
486 */
487 smp_mb__before_atomic();
Jens Axboec854ab52018-05-14 12:17:31 -0600488
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800489 /*
Jens Axboec854ab52018-05-14 12:17:31 -0600490 * For concurrent callers of this, the one that failed the
491 * atomic_cmpxhcg() race should call this function again
492 * to wakeup a new batch on a different 'ws'.
Omar Sandoval6c0ca7a2017-01-18 11:55:22 -0800493 */
Jens Axboec854ab52018-05-14 12:17:31 -0600494 ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
495 if (ret == wait_cnt) {
496 sbq_index_atomic_inc(&sbq->wake_index);
497 wake_up_nr(&ws->wait, wake_batch);
498 return false;
499 }
500
501 return true;
Omar Sandoval88459642016-09-17 08:38:44 -0600502 }
Jens Axboec854ab52018-05-14 12:17:31 -0600503
504 return false;
505}
506
Ming Leie6fc4642018-05-24 11:00:39 -0600507void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
Jens Axboec854ab52018-05-14 12:17:31 -0600508{
509 while (__sbq_wake_up(sbq))
510 ;
Omar Sandoval88459642016-09-17 08:38:44 -0600511}
Ming Leie6fc4642018-05-24 11:00:39 -0600512EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
Omar Sandoval88459642016-09-17 08:38:44 -0600513
Omar Sandoval40aabb62016-09-17 01:28:23 -0700514void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700515 unsigned int cpu)
Omar Sandoval88459642016-09-17 08:38:44 -0600516{
Omar Sandoval4ace53f2018-02-27 16:56:43 -0800517 sbitmap_clear_bit_unlock(&sbq->sb, nr);
Ming Leie6fc4642018-05-24 11:00:39 -0600518 /*
519 * Pairs with the memory barrier in set_current_state() to ensure the
520 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
521 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
522 * waiter. See the comment on waitqueue_active().
523 */
524 smp_mb__after_atomic();
525 sbitmap_queue_wake_up(sbq);
526
Omar Sandoval5c64a8d2016-09-17 12:20:54 -0700527 if (likely(!sbq->round_robin && nr < sbq->sb.depth))
Omar Sandoval40aabb62016-09-17 01:28:23 -0700528 *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
Omar Sandoval88459642016-09-17 08:38:44 -0600529}
530EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
531
532void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
533{
534 int i, wake_index;
535
536 /*
Omar Sandovalf66227d2017-01-18 11:55:21 -0800537 * Pairs with the memory barrier in set_current_state() like in
Ming Leie6fc4642018-05-24 11:00:39 -0600538 * sbitmap_queue_wake_up().
Omar Sandoval88459642016-09-17 08:38:44 -0600539 */
540 smp_mb();
541 wake_index = atomic_read(&sbq->wake_index);
542 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
543 struct sbq_wait_state *ws = &sbq->ws[wake_index];
544
545 if (waitqueue_active(&ws->wait))
546 wake_up(&ws->wait);
547
548 wake_index = sbq_index_inc(wake_index);
549 }
550}
551EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800552
553void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
554{
555 bool first;
556 int i;
557
558 sbitmap_show(&sbq->sb, m);
559
560 seq_puts(m, "alloc_hint={");
561 first = true;
562 for_each_possible_cpu(i) {
563 if (!first)
564 seq_puts(m, ", ");
565 first = false;
566 seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
567 }
568 seq_puts(m, "}\n");
569
570 seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
571 seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
572
573 seq_puts(m, "ws={\n");
574 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
575 struct sbq_wait_state *ws = &sbq->ws[i];
576
577 seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
578 atomic_read(&ws->wait_cnt),
579 waitqueue_active(&ws->wait) ? "active" : "inactive");
580 }
581 seq_puts(m, "}\n");
582
583 seq_printf(m, "round_robin=%d\n", sbq->round_robin);
Omar Sandovala3275532018-05-09 17:16:31 -0700584 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800585}
586EXPORT_SYMBOL_GPL(sbitmap_queue_show);