]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq-tag.c
blk-mq: export blk_mq_tag_busy_iter
[mirror_ubuntu-artful-kernel.git] / block / blk-mq-tag.c
CommitLineData
320ae51f
JA
1#include <linux/kernel.h>
2#include <linux/module.h>
4bb659b1 3#include <linux/random.h>
320ae51f
JA
4
5#include <linux/blk-mq.h>
6#include "blk.h"
7#include "blk-mq.h"
8#include "blk-mq-tag.h"
9
0d2602ca 10void blk_mq_wait_for_tags(struct blk_mq_hw_ctx *hctx, bool reserved)
320ae51f 11{
4bb659b1
JA
12 int tag, zero = 0;
13
0d2602ca
JA
14 tag = blk_mq_get_tag(hctx, &zero, __GFP_WAIT, reserved);
15 blk_mq_put_tag(hctx, tag, &zero);
4bb659b1
JA
16}
17
18static bool bt_has_free_tags(struct blk_mq_bitmap_tags *bt)
19{
20 int i;
21
22 for (i = 0; i < bt->map_nr; i++) {
e93ecf60 23 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
24 int ret;
25
26 ret = find_first_zero_bit(&bm->word, bm->depth);
27 if (ret < bm->depth)
28 return true;
29 }
30
31 return false;
320ae51f
JA
32}
33
34bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
35{
4bb659b1
JA
36 if (!tags)
37 return true;
38
39 return bt_has_free_tags(&tags->bitmap_tags);
40}
41
0d2602ca
JA
42static inline void bt_index_inc(unsigned int *index)
43{
44 *index = (*index + 1) & (BT_WAIT_QUEUES - 1);
45}
46
47/*
48 * If a previously inactive queue goes active, bump the active user count.
49 */
50bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
51{
52 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
53 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
54 atomic_inc(&hctx->tags->active_queues);
55
56 return true;
57}
58
59/*
e3a2b3f9 60 * Wakeup all potentially sleeping on normal (non-reserved) tags
0d2602ca 61 */
e3a2b3f9 62static void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags)
0d2602ca 63{
0d2602ca
JA
64 struct blk_mq_bitmap_tags *bt;
65 int i, wake_index;
66
0d2602ca
JA
67 bt = &tags->bitmap_tags;
68 wake_index = bt->wake_index;
69 for (i = 0; i < BT_WAIT_QUEUES; i++) {
70 struct bt_wait_state *bs = &bt->bs[wake_index];
71
72 if (waitqueue_active(&bs->wait))
73 wake_up(&bs->wait);
74
75 bt_index_inc(&wake_index);
76 }
77}
78
e3a2b3f9
JA
79/*
80 * If a previously busy queue goes inactive, potential waiters could now
81 * be allowed to queue. Wake them up and check.
82 */
83void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
84{
85 struct blk_mq_tags *tags = hctx->tags;
86
87 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
88 return;
89
90 atomic_dec(&tags->active_queues);
91
92 blk_mq_tag_wakeup_all(tags);
93}
94
0d2602ca
JA
95/*
96 * For shared tag users, we track the number of currently active users
97 * and attempt to provide a fair share of the tag depth for each of them.
98 */
99static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
100 struct blk_mq_bitmap_tags *bt)
101{
102 unsigned int depth, users;
103
104 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
105 return true;
106 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
107 return true;
108
109 /*
110 * Don't try dividing an ant
111 */
112 if (bt->depth == 1)
113 return true;
114
115 users = atomic_read(&hctx->tags->active_queues);
116 if (!users)
117 return true;
118
119 /*
120 * Allow at least some tags
121 */
122 depth = max((bt->depth + users - 1) / users, 4U);
123 return atomic_read(&hctx->nr_active) < depth;
124}
125
e93ecf60 126static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag)
4bb659b1
JA
127{
128 int tag, org_last_tag, end;
129
59d13bf5 130 org_last_tag = last_tag;
4bb659b1
JA
131 end = bm->depth;
132 do {
133restart:
134 tag = find_next_zero_bit(&bm->word, end, last_tag);
135 if (unlikely(tag >= end)) {
136 /*
137 * We started with an offset, start from 0 to
138 * exhaust the map.
139 */
140 if (org_last_tag && last_tag) {
141 end = last_tag;
142 last_tag = 0;
143 goto restart;
144 }
145 return -1;
146 }
147 last_tag = tag + 1;
148 } while (test_and_set_bit_lock(tag, &bm->word));
149
150 return tag;
151}
152
153/*
154 * Straight forward bitmap tag implementation, where each bit is a tag
155 * (cleared == free, and set == busy). The small twist is using per-cpu
156 * last_tag caches, which blk-mq stores in the blk_mq_ctx software queue
157 * contexts. This enables us to drastically limit the space searched,
158 * without dirtying an extra shared cacheline like we would if we stored
159 * the cache value inside the shared blk_mq_bitmap_tags structure. On top
160 * of that, each word of tags is in a separate cacheline. This means that
161 * multiple users will tend to stick to different cachelines, at least
162 * until the map is exhausted.
163 */
0d2602ca
JA
164static int __bt_get(struct blk_mq_hw_ctx *hctx, struct blk_mq_bitmap_tags *bt,
165 unsigned int *tag_cache)
4bb659b1
JA
166{
167 unsigned int last_tag, org_last_tag;
168 int index, i, tag;
169
0d2602ca
JA
170 if (!hctx_may_queue(hctx, bt))
171 return -1;
172
4bb659b1 173 last_tag = org_last_tag = *tag_cache;
59d13bf5 174 index = TAG_TO_INDEX(bt, last_tag);
4bb659b1
JA
175
176 for (i = 0; i < bt->map_nr; i++) {
59d13bf5 177 tag = __bt_get_word(&bt->map[index], TAG_TO_BIT(bt, last_tag));
4bb659b1 178 if (tag != -1) {
59d13bf5 179 tag += (index << bt->bits_per_word);
4bb659b1
JA
180 goto done;
181 }
182
183 last_tag = 0;
184 if (++index >= bt->map_nr)
185 index = 0;
186 }
187
188 *tag_cache = 0;
189 return -1;
190
191 /*
192 * Only update the cache from the allocation path, if we ended
193 * up using the specific cached tag.
194 */
195done:
196 if (tag == org_last_tag) {
197 last_tag = tag + 1;
198 if (last_tag >= bt->depth - 1)
199 last_tag = 0;
200
201 *tag_cache = last_tag;
202 }
203
204 return tag;
205}
206
4bb659b1
JA
207static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
208 struct blk_mq_hw_ctx *hctx)
209{
210 struct bt_wait_state *bs;
211
212 if (!hctx)
213 return &bt->bs[0];
214
215 bs = &bt->bs[hctx->wait_index];
216 bt_index_inc(&hctx->wait_index);
217 return bs;
320ae51f
JA
218}
219
4bb659b1
JA
220static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
221 unsigned int *last_tag, gfp_t gfp)
320ae51f 222{
4bb659b1
JA
223 struct bt_wait_state *bs;
224 DEFINE_WAIT(wait);
320ae51f
JA
225 int tag;
226
0d2602ca 227 tag = __bt_get(hctx, bt, last_tag);
4bb659b1
JA
228 if (tag != -1)
229 return tag;
230
231 if (!(gfp & __GFP_WAIT))
232 return -1;
233
234 bs = bt_wait_ptr(bt, hctx);
235 do {
236 bool was_empty;
237
238 was_empty = list_empty(&wait.task_list);
239 prepare_to_wait(&bs->wait, &wait, TASK_UNINTERRUPTIBLE);
240
0d2602ca 241 tag = __bt_get(hctx, bt, last_tag);
4bb659b1
JA
242 if (tag != -1)
243 break;
244
245 if (was_empty)
246 atomic_set(&bs->wait_cnt, bt->wake_cnt);
247
248 io_schedule();
249 } while (1);
250
251 finish_wait(&bs->wait, &wait);
252 return tag;
253}
254
255static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags,
256 struct blk_mq_hw_ctx *hctx,
257 unsigned int *last_tag, gfp_t gfp)
258{
259 int tag;
260
261 tag = bt_get(&tags->bitmap_tags, hctx, last_tag, gfp);
262 if (tag >= 0)
263 return tag + tags->nr_reserved_tags;
264
265 return BLK_MQ_TAG_FAIL;
320ae51f
JA
266}
267
268static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
269 gfp_t gfp)
270{
4bb659b1 271 int tag, zero = 0;
320ae51f
JA
272
273 if (unlikely(!tags->nr_reserved_tags)) {
274 WARN_ON_ONCE(1);
275 return BLK_MQ_TAG_FAIL;
276 }
277
4bb659b1 278 tag = bt_get(&tags->breserved_tags, NULL, &zero, gfp);
320ae51f
JA
279 if (tag < 0)
280 return BLK_MQ_TAG_FAIL;
4bb659b1 281
320ae51f
JA
282 return tag;
283}
284
0d2602ca 285unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag,
4bb659b1 286 gfp_t gfp, bool reserved)
320ae51f
JA
287{
288 if (!reserved)
0d2602ca 289 return __blk_mq_get_tag(hctx->tags, hctx, last_tag, gfp);
320ae51f 290
0d2602ca 291 return __blk_mq_get_reserved_tag(hctx->tags, gfp);
320ae51f
JA
292}
293
4bb659b1
JA
294static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)
295{
296 int i, wake_index;
297
298 wake_index = bt->wake_index;
299 for (i = 0; i < BT_WAIT_QUEUES; i++) {
300 struct bt_wait_state *bs = &bt->bs[wake_index];
301
302 if (waitqueue_active(&bs->wait)) {
303 if (wake_index != bt->wake_index)
304 bt->wake_index = wake_index;
305
306 return bs;
307 }
308
309 bt_index_inc(&wake_index);
310 }
311
312 return NULL;
313}
314
315static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
316{
59d13bf5 317 const int index = TAG_TO_INDEX(bt, tag);
4bb659b1
JA
318 struct bt_wait_state *bs;
319
0289b2e1
ML
320 /*
321 * The unlock memory barrier need to order access to req in free
322 * path and clearing tag bit
323 */
324 clear_bit_unlock(TAG_TO_BIT(bt, tag), &bt->map[index].word);
4bb659b1
JA
325
326 bs = bt_wake_ptr(bt);
327 if (bs && atomic_dec_and_test(&bs->wait_cnt)) {
4bb659b1
JA
328 atomic_set(&bs->wait_cnt, bt->wake_cnt);
329 bt_index_inc(&bt->wake_index);
330 wake_up(&bs->wait);
331 }
332}
333
320ae51f
JA
334static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
335{
336 BUG_ON(tag >= tags->nr_tags);
337
4bb659b1 338 bt_clear_tag(&tags->bitmap_tags, tag);
320ae51f
JA
339}
340
341static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags,
342 unsigned int tag)
343{
344 BUG_ON(tag >= tags->nr_reserved_tags);
345
4bb659b1 346 bt_clear_tag(&tags->breserved_tags, tag);
320ae51f
JA
347}
348
0d2602ca 349void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
4bb659b1 350 unsigned int *last_tag)
320ae51f 351{
0d2602ca
JA
352 struct blk_mq_tags *tags = hctx->tags;
353
4bb659b1
JA
354 if (tag >= tags->nr_reserved_tags) {
355 const int real_tag = tag - tags->nr_reserved_tags;
356
357 __blk_mq_put_tag(tags, real_tag);
358 *last_tag = real_tag;
359 } else
320ae51f
JA
360 __blk_mq_put_reserved_tag(tags, tag);
361}
362
4bb659b1
JA
363static void bt_for_each_free(struct blk_mq_bitmap_tags *bt,
364 unsigned long *free_map, unsigned int off)
320ae51f 365{
4bb659b1
JA
366 int i;
367
368 for (i = 0; i < bt->map_nr; i++) {
e93ecf60 369 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
370 int bit = 0;
371
372 do {
373 bit = find_next_zero_bit(&bm->word, bm->depth, bit);
374 if (bit >= bm->depth)
375 break;
376
377 __set_bit(bit + off, free_map);
378 bit++;
379 } while (1);
380
59d13bf5 381 off += (1 << bt->bits_per_word);
4bb659b1 382 }
320ae51f
JA
383}
384
385void blk_mq_tag_busy_iter(struct blk_mq_tags *tags,
386 void (*fn)(void *, unsigned long *), void *data)
387{
388 unsigned long *tag_map;
389 size_t map_size;
390
391 map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG;
392 tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC);
393 if (!tag_map)
394 return;
395
4bb659b1 396 bt_for_each_free(&tags->bitmap_tags, tag_map, tags->nr_reserved_tags);
320ae51f 397 if (tags->nr_reserved_tags)
4bb659b1 398 bt_for_each_free(&tags->breserved_tags, tag_map, 0);
320ae51f
JA
399
400 fn(data, tag_map);
401 kfree(tag_map);
402}
edf866b3 403EXPORT_SYMBOL(blk_mq_tag_busy_iter);
320ae51f 404
4bb659b1
JA
405static unsigned int bt_unused_tags(struct blk_mq_bitmap_tags *bt)
406{
407 unsigned int i, used;
408
409 for (i = 0, used = 0; i < bt->map_nr; i++) {
e93ecf60 410 struct blk_align_bitmap *bm = &bt->map[i];
4bb659b1
JA
411
412 used += bitmap_weight(&bm->word, bm->depth);
413 }
414
415 return bt->depth - used;
416}
417
e3a2b3f9
JA
418static void bt_update_count(struct blk_mq_bitmap_tags *bt,
419 unsigned int depth)
420{
421 unsigned int tags_per_word = 1U << bt->bits_per_word;
422 unsigned int map_depth = depth;
423
424 if (depth) {
425 int i;
426
427 for (i = 0; i < bt->map_nr; i++) {
428 bt->map[i].depth = min(map_depth, tags_per_word);
429 map_depth -= bt->map[i].depth;
430 }
431 }
432
433 bt->wake_cnt = BT_WAIT_BATCH;
434 if (bt->wake_cnt > depth / 4)
435 bt->wake_cnt = max(1U, depth / 4);
436
437 bt->depth = depth;
438}
439
4bb659b1
JA
440static int bt_alloc(struct blk_mq_bitmap_tags *bt, unsigned int depth,
441 int node, bool reserved)
442{
443 int i;
444
59d13bf5
JA
445 bt->bits_per_word = ilog2(BITS_PER_LONG);
446
4bb659b1
JA
447 /*
448 * Depth can be zero for reserved tags, that's not a failure
449 * condition.
450 */
451 if (depth) {
e3a2b3f9 452 unsigned int nr, tags_per_word;
59d13bf5
JA
453
454 tags_per_word = (1 << bt->bits_per_word);
455
456 /*
457 * If the tag space is small, shrink the number of tags
458 * per word so we spread over a few cachelines, at least.
459 * If less than 4 tags, just forget about it, it's not
460 * going to work optimally anyway.
461 */
462 if (depth >= 4) {
463 while (tags_per_word * 4 > depth) {
464 bt->bits_per_word--;
465 tags_per_word = (1 << bt->bits_per_word);
466 }
467 }
4bb659b1 468
59d13bf5 469 nr = ALIGN(depth, tags_per_word) / tags_per_word;
e93ecf60 470 bt->map = kzalloc_node(nr * sizeof(struct blk_align_bitmap),
4bb659b1
JA
471 GFP_KERNEL, node);
472 if (!bt->map)
473 return -ENOMEM;
474
475 bt->map_nr = nr;
4bb659b1
JA
476 }
477
478 bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
479 if (!bt->bs) {
480 kfree(bt->map);
481 return -ENOMEM;
482 }
483
484 for (i = 0; i < BT_WAIT_QUEUES; i++)
485 init_waitqueue_head(&bt->bs[i].wait);
486
e3a2b3f9 487 bt_update_count(bt, depth);
4bb659b1
JA
488 return 0;
489}
490
491static void bt_free(struct blk_mq_bitmap_tags *bt)
492{
493 kfree(bt->map);
494 kfree(bt->bs);
495}
496
497static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
498 int node)
499{
500 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
501
502 if (bt_alloc(&tags->bitmap_tags, depth, node, false))
503 goto enomem;
504 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node, true))
505 goto enomem;
506
507 return tags;
508enomem:
509 bt_free(&tags->bitmap_tags);
510 kfree(tags);
511 return NULL;
512}
513
320ae51f
JA
514struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
515 unsigned int reserved_tags, int node)
516{
320ae51f 517 struct blk_mq_tags *tags;
320ae51f
JA
518
519 if (total_tags > BLK_MQ_TAG_MAX) {
520 pr_err("blk-mq: tag depth too large\n");
521 return NULL;
522 }
523
524 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
525 if (!tags)
526 return NULL;
527
320ae51f
JA
528 tags->nr_tags = total_tags;
529 tags->nr_reserved_tags = reserved_tags;
320ae51f 530
4bb659b1 531 return blk_mq_init_bitmap_tags(tags, node);
320ae51f
JA
532}
533
534void blk_mq_free_tags(struct blk_mq_tags *tags)
535{
4bb659b1
JA
536 bt_free(&tags->bitmap_tags);
537 bt_free(&tags->breserved_tags);
320ae51f
JA
538 kfree(tags);
539}
540
4bb659b1
JA
541void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
542{
543 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
544
9d3d21ae 545 *tag = prandom_u32() % depth;
4bb659b1
JA
546}
547
e3a2b3f9
JA
548int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
549{
550 tdepth -= tags->nr_reserved_tags;
551 if (tdepth > tags->nr_tags)
552 return -EINVAL;
553
554 /*
555 * Don't need (or can't) update reserved tags here, they remain
556 * static and should never need resizing.
557 */
558 bt_update_count(&tags->bitmap_tags, tdepth);
559 blk_mq_tag_wakeup_all(tags);
560 return 0;
561}
562
320ae51f
JA
563ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
564{
565 char *orig_page = page;
4bb659b1 566 unsigned int free, res;
320ae51f
JA
567
568 if (!tags)
569 return 0;
570
59d13bf5
JA
571 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
572 "bits_per_word=%u\n",
573 tags->nr_tags, tags->nr_reserved_tags,
574 tags->bitmap_tags.bits_per_word);
320ae51f 575
4bb659b1
JA
576 free = bt_unused_tags(&tags->bitmap_tags);
577 res = bt_unused_tags(&tags->breserved_tags);
320ae51f 578
4bb659b1 579 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
0d2602ca 580 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
320ae51f
JA
581
582 return page - orig_page;
583}