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