block: avoid extra bio reference for async O_DIRECT
[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
OS
22
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
590b5b7d 55 sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
88459642
OS
56 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
c05e6673
OS
82static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
83 unsigned int hint, bool wrap)
88459642
OS
84{
85 unsigned int orig_hint = hint;
86 int nr;
87
88 while (1) {
c05e6673
OS
89 nr = find_next_zero_bit(word, depth, hint);
90 if (unlikely(nr >= depth)) {
88459642
OS
91 /*
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
4ace53f1 103 if (!test_and_set_bit_lock(nr, word))
88459642
OS
104 break;
105
106 hint = nr + 1;
c05e6673 107 if (hint >= depth - 1)
88459642
OS
108 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
27fae429
JA
121 /*
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
88459642 131 for (i = 0; i < sb->map_nr; i++) {
c05e6673 132 nr = __sbitmap_get_word(&sb->map[index].word,
27fae429 133 sb->map[index].depth, alloc_hint,
88459642
OS
134 !round_robin);
135 if (nr != -1) {
136 nr += index << sb->shift;
137 break;
138 }
139
140 /* Jump to next index. */
27fae429
JA
141 alloc_hint = 0;
142 if (++index >= sb->map_nr)
88459642 143 index = 0;
88459642
OS
144 }
145
146 return nr;
147}
148EXPORT_SYMBOL_GPL(sbitmap_get);
149
c05e6673
OS
150int 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
88459642
OS
181bool 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{
60658e0d 211 unsigned int i, weight = 0;
88459642
OS
212
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
24af1ccf
OS
222void 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
a3275539
OS
278static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
279 unsigned int depth)
88459642
OS
280{
281 unsigned int wake_batch;
a3275539 282 unsigned int shallow_depth;
88459642
OS
283
284 /*
285 * For each batch, we wake up one queue. We need to make sure that our
a3275539
OS
286 * 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.
88459642 299 */
a3275539
OS
300 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);
88459642
OS
305
306 return wake_batch;
307}
308
309int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
f4a644db 310 int shift, bool round_robin, gfp_t flags, int node)
88459642
OS
311{
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
40aabb67
OS
319 sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
320 if (!sbq->alloc_hint) {
321 sbitmap_free(&sbq->sb);
322 return -ENOMEM;
323 }
324
98d95416
OS
325 if (depth && !round_robin) {
326 for_each_possible_cpu(i)
327 *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
328 }
329
a3275539
OS
330 sbq->min_shallow_depth = UINT_MAX;
331 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
88459642
OS
332 atomic_set(&sbq->wake_index, 0);
333
48e28166 334 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
88459642 335 if (!sbq->ws) {
40aabb67 336 free_percpu(sbq->alloc_hint);
88459642
OS
337 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 }
f4a644db
OS
345
346 sbq->round_robin = round_robin;
88459642
OS
347 return 0;
348}
349EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
350
a3275539
OS
351static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
352 unsigned int depth)
88459642 353{
a3275539 354 unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
6c0ca7ae
OS
355 int i;
356
357 if (sbq->wake_batch != wake_batch) {
358 WRITE_ONCE(sbq->wake_batch, wake_batch);
359 /*
e6fc4649
ML
360 * Pairs with the memory barrier in sbitmap_queue_wake_up()
361 * to ensure that the batch size is updated before the wait
362 * counts.
6c0ca7ae
OS
363 */
364 smp_mb__before_atomic();
365 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
366 atomic_set(&sbq->ws[i].wait_cnt, 1);
367 }
a3275539
OS
368}
369
370void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
371{
372 sbitmap_queue_update_wake_batch(sbq, depth);
88459642
OS
373 sbitmap_resize(&sbq->sb, depth);
374}
375EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
376
f4a644db 377int __sbitmap_queue_get(struct sbitmap_queue *sbq)
40aabb67 378{
05fd095d 379 unsigned int hint, depth;
40aabb67
OS
380 int nr;
381
382 hint = this_cpu_read(*sbq->alloc_hint);
05fd095d
OS
383 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 }
f4a644db 388 nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
40aabb67
OS
389
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);
f4a644db 393 } else if (nr == hint || unlikely(sbq->round_robin)) {
40aabb67
OS
394 /* Only update the hint if we used it. */
395 hint = nr + 1;
05fd095d 396 if (hint >= depth - 1)
40aabb67
OS
397 hint = 0;
398 this_cpu_write(*sbq->alloc_hint, hint);
399 }
400
401 return nr;
402}
403EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
404
c05e6673
OS
405int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
406 unsigned int shallow_depth)
407{
408 unsigned int hint, depth;
409 int nr;
410
61445b56
OS
411 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
412
c05e6673
OS
413 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
a3275539
OS
436void 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
88459642
OS
444static 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
c854ab57 466static bool __sbq_wake_up(struct sbitmap_queue *sbq)
88459642
OS
467{
468 struct sbq_wait_state *ws;
6c0ca7ae 469 unsigned int wake_batch;
88459642
OS
470 int wait_cnt;
471
88459642
OS
472 ws = sbq_wake_ptr(sbq);
473 if (!ws)
c854ab57 474 return false;
88459642
OS
475
476 wait_cnt = atomic_dec_return(&ws->wait_cnt);
6c0ca7ae 477 if (wait_cnt <= 0) {
c854ab57
JA
478 int ret;
479
6c0ca7ae 480 wake_batch = READ_ONCE(sbq->wake_batch);
c854ab57 481
6c0ca7ae
OS
482 /*
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();
c854ab57 488
6c0ca7ae 489 /*
c854ab57
JA
490 * 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'.
6c0ca7ae 493 */
c854ab57
JA
494 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;
88459642 502 }
c854ab57
JA
503
504 return false;
505}
506
e6fc4649 507void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
c854ab57
JA
508{
509 while (__sbq_wake_up(sbq))
510 ;
88459642 511}
e6fc4649 512EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
88459642 513
40aabb67 514void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
f4a644db 515 unsigned int cpu)
88459642 516{
4ace53f1 517 sbitmap_clear_bit_unlock(&sbq->sb, nr);
e6fc4649
ML
518 /*
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
5c64a8df 527 if (likely(!sbq->round_robin && nr < sbq->sb.depth))
40aabb67 528 *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
88459642
OS
529}
530EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
531
532void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
533{
534 int i, wake_index;
535
536 /*
f66227de 537 * Pairs with the memory barrier in set_current_state() like in
e6fc4649 538 * sbitmap_queue_wake_up().
88459642
OS
539 */
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);
24af1ccf
OS
552
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);
a3275539 584 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
24af1ccf
OS
585}
586EXPORT_SYMBOL_GPL(sbitmap_queue_show);