Merge tag 'linux-kselftest-5.4-rc1.1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / block / blk-mq-tag.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
75bb4625 2/*
88459642
OS
3 * Tag allocation using scalable bitmaps. Uses active queue tracking to support
4 * fairer distribution of tags between multiple submitters when a shared tag map
5 * is used.
75bb4625
JA
6 *
7 * Copyright (C) 2013-2014 Jens Axboe
8 */
320ae51f
JA
9#include <linux/kernel.h>
10#include <linux/module.h>
320ae51f
JA
11
12#include <linux/blk-mq.h>
f9934a80 13#include <linux/delay.h>
320ae51f
JA
14#include "blk.h"
15#include "blk-mq.h"
16#include "blk-mq-tag.h"
17
320ae51f
JA
18bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
19{
4bb659b1
JA
20 if (!tags)
21 return true;
22
88459642 23 return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
0d2602ca
JA
24}
25
26/*
27 * If a previously inactive queue goes active, bump the active user count.
d263ed99
JW
28 * We need to do this before try to allocate driver tag, then even if fail
29 * to get tag when first time, the other shared-tag users could reserve
30 * budget for it.
0d2602ca
JA
31 */
32bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
33{
34 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
35 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
36 atomic_inc(&hctx->tags->active_queues);
37
38 return true;
39}
40
41/*
aed3ea94 42 * Wakeup all potentially sleeping on tags
0d2602ca 43 */
aed3ea94 44void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
0d2602ca 45{
88459642
OS
46 sbitmap_queue_wake_all(&tags->bitmap_tags);
47 if (include_reserve)
48 sbitmap_queue_wake_all(&tags->breserved_tags);
0d2602ca
JA
49}
50
e3a2b3f9
JA
51/*
52 * If a previously busy queue goes inactive, potential waiters could now
53 * be allowed to queue. Wake them up and check.
54 */
55void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
56{
57 struct blk_mq_tags *tags = hctx->tags;
58
59 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
60 return;
61
62 atomic_dec(&tags->active_queues);
63
aed3ea94 64 blk_mq_tag_wakeup_all(tags, false);
e3a2b3f9
JA
65}
66
0d2602ca
JA
67/*
68 * For shared tag users, we track the number of currently active users
69 * and attempt to provide a fair share of the tag depth for each of them.
70 */
71static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
88459642 72 struct sbitmap_queue *bt)
0d2602ca
JA
73{
74 unsigned int depth, users;
75
76 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
77 return true;
78 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
79 return true;
80
81 /*
82 * Don't try dividing an ant
83 */
88459642 84 if (bt->sb.depth == 1)
0d2602ca
JA
85 return true;
86
87 users = atomic_read(&hctx->tags->active_queues);
88 if (!users)
89 return true;
90
91 /*
92 * Allow at least some tags
93 */
88459642 94 depth = max((bt->sb.depth + users - 1) / users, 4U);
0d2602ca
JA
95 return atomic_read(&hctx->nr_active) < depth;
96}
97
200e86b3
JA
98static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
99 struct sbitmap_queue *bt)
4bb659b1 100{
200e86b3
JA
101 if (!(data->flags & BLK_MQ_REQ_INTERNAL) &&
102 !hctx_may_queue(data->hctx, bt))
0d2602ca 103 return -1;
229a9287
OS
104 if (data->shallow_depth)
105 return __sbitmap_queue_get_shallow(bt, data->shallow_depth);
106 else
107 return __sbitmap_queue_get(bt);
4bb659b1
JA
108}
109
4941115b 110unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
320ae51f 111{
4941115b
JA
112 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
113 struct sbitmap_queue *bt;
88459642 114 struct sbq_wait_state *ws;
5d2ee712 115 DEFINE_SBQ_WAIT(wait);
4941115b 116 unsigned int tag_offset;
320ae51f
JA
117 int tag;
118
4941115b
JA
119 if (data->flags & BLK_MQ_REQ_RESERVED) {
120 if (unlikely(!tags->nr_reserved_tags)) {
121 WARN_ON_ONCE(1);
122 return BLK_MQ_TAG_FAIL;
123 }
124 bt = &tags->breserved_tags;
125 tag_offset = 0;
126 } else {
127 bt = &tags->bitmap_tags;
128 tag_offset = tags->nr_reserved_tags;
129 }
130
200e86b3 131 tag = __blk_mq_get_tag(data, bt);
4bb659b1 132 if (tag != -1)
4941115b 133 goto found_tag;
4bb659b1 134
6f3b0e8b 135 if (data->flags & BLK_MQ_REQ_NOWAIT)
4941115b 136 return BLK_MQ_TAG_FAIL;
4bb659b1 137
4941115b 138 ws = bt_wait_ptr(bt, data->hctx);
4bb659b1 139 do {
e6fc4649
ML
140 struct sbitmap_queue *bt_prev;
141
b3223207
BVA
142 /*
143 * We're out of tags on this hardware queue, kick any
144 * pending IO submits before going to sleep waiting for
8cecb07d 145 * some to complete.
b3223207 146 */
8cecb07d 147 blk_mq_run_hw_queue(data->hctx, false);
b3223207 148
080ff351
JA
149 /*
150 * Retry tag allocation after running the hardware queue,
151 * as running the queue may also have found completions.
152 */
200e86b3 153 tag = __blk_mq_get_tag(data, bt);
080ff351
JA
154 if (tag != -1)
155 break;
156
5d2ee712 157 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
4e5dff41
JA
158
159 tag = __blk_mq_get_tag(data, bt);
160 if (tag != -1)
161 break;
162
e6fc4649 163 bt_prev = bt;
4bb659b1 164 io_schedule();
cb96a42c 165
5d2ee712
JA
166 sbitmap_finish_wait(bt, ws, &wait);
167
cb96a42c 168 data->ctx = blk_mq_get_ctx(data->q);
f9afca4d 169 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
8ccdf4a3 170 data->ctx);
4941115b
JA
171 tags = blk_mq_tags_from_data(data);
172 if (data->flags & BLK_MQ_REQ_RESERVED)
173 bt = &tags->breserved_tags;
174 else
175 bt = &tags->bitmap_tags;
176
e6fc4649
ML
177 /*
178 * If destination hw queue is changed, fake wake up on
179 * previous queue for compensating the wake up miss, so
180 * other allocations on previous queue won't be starved.
181 */
182 if (bt != bt_prev)
183 sbitmap_queue_wake_up(bt_prev);
184
4941115b 185 ws = bt_wait_ptr(bt, data->hctx);
4bb659b1
JA
186 } while (1);
187
5d2ee712 188 sbitmap_finish_wait(bt, ws, &wait);
320ae51f 189
4941115b
JA
190found_tag:
191 return tag + tag_offset;
320ae51f
JA
192}
193
4941115b
JA
194void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags,
195 struct blk_mq_ctx *ctx, unsigned int tag)
320ae51f 196{
415b806d 197 if (!blk_mq_tag_is_reserved(tags, tag)) {
4bb659b1
JA
198 const int real_tag = tag - tags->nr_reserved_tags;
199
70114c39 200 BUG_ON(real_tag >= tags->nr_tags);
f4a644db 201 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
70114c39
JA
202 } else {
203 BUG_ON(tag >= tags->nr_reserved_tags);
f4a644db 204 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
70114c39 205 }
320ae51f
JA
206}
207
88459642
OS
208struct bt_iter_data {
209 struct blk_mq_hw_ctx *hctx;
210 busy_iter_fn *fn;
211 void *data;
212 bool reserved;
213};
214
215static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
320ae51f 216{
88459642
OS
217 struct bt_iter_data *iter_data = data;
218 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
219 struct blk_mq_tags *tags = hctx->tags;
220 bool reserved = iter_data->reserved;
81481eb4 221 struct request *rq;
4bb659b1 222
88459642
OS
223 if (!reserved)
224 bitnr += tags->nr_reserved_tags;
225 rq = tags->rqs[bitnr];
4bb659b1 226
7f5562d5
JA
227 /*
228 * We can hit rq == NULL here, because the tagging functions
c7b1bf5c 229 * test and set the bit before assigning ->rqs[].
7f5562d5
JA
230 */
231 if (rq && rq->q == hctx->queue)
7baa8572 232 return iter_data->fn(hctx, rq, iter_data->data, reserved);
88459642
OS
233 return true;
234}
4bb659b1 235
c7b1bf5c
BVA
236/**
237 * bt_for_each - iterate over the requests associated with a hardware queue
238 * @hctx: Hardware queue to examine.
239 * @bt: sbitmap to examine. This is either the breserved_tags member
240 * or the bitmap_tags member of struct blk_mq_tags.
241 * @fn: Pointer to the function that will be called for each request
242 * associated with @hctx that has been assigned a driver tag.
243 * @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
ab11fe5a
JA
244 * where rq is a pointer to a request. Return true to continue
245 * iterating tags, false to stop.
c7b1bf5c
BVA
246 * @data: Will be passed as third argument to @fn.
247 * @reserved: Indicates whether @bt is the breserved_tags member or the
248 * bitmap_tags member of struct blk_mq_tags.
249 */
88459642
OS
250static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
251 busy_iter_fn *fn, void *data, bool reserved)
252{
253 struct bt_iter_data iter_data = {
254 .hctx = hctx,
255 .fn = fn,
256 .data = data,
257 .reserved = reserved,
258 };
259
260 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
320ae51f
JA
261}
262
88459642
OS
263struct bt_tags_iter_data {
264 struct blk_mq_tags *tags;
265 busy_tag_iter_fn *fn;
266 void *data;
267 bool reserved;
268};
269
270static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
f26cdc85 271{
88459642
OS
272 struct bt_tags_iter_data *iter_data = data;
273 struct blk_mq_tags *tags = iter_data->tags;
274 bool reserved = iter_data->reserved;
f26cdc85 275 struct request *rq;
f26cdc85 276
88459642
OS
277 if (!reserved)
278 bitnr += tags->nr_reserved_tags;
7f5562d5
JA
279
280 /*
281 * We can hit rq == NULL here, because the tagging functions
282 * test and set the bit before assining ->rqs[].
283 */
88459642 284 rq = tags->rqs[bitnr];
2d5ba0e2 285 if (rq && blk_mq_request_started(rq))
7baa8572 286 return iter_data->fn(rq, iter_data->data, reserved);
f26cdc85 287
88459642
OS
288 return true;
289}
290
c7b1bf5c
BVA
291/**
292 * bt_tags_for_each - iterate over the requests in a tag map
293 * @tags: Tag map to iterate over.
294 * @bt: sbitmap to examine. This is either the breserved_tags member
295 * or the bitmap_tags member of struct blk_mq_tags.
296 * @fn: Pointer to the function that will be called for each started
297 * request. @fn will be called as follows: @fn(rq, @data,
ab11fe5a
JA
298 * @reserved) where rq is a pointer to a request. Return true
299 * to continue iterating tags, false to stop.
c7b1bf5c
BVA
300 * @data: Will be passed as second argument to @fn.
301 * @reserved: Indicates whether @bt is the breserved_tags member or the
302 * bitmap_tags member of struct blk_mq_tags.
303 */
88459642
OS
304static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
305 busy_tag_iter_fn *fn, void *data, bool reserved)
306{
307 struct bt_tags_iter_data iter_data = {
308 .tags = tags,
309 .fn = fn,
310 .data = data,
311 .reserved = reserved,
312 };
313
314 if (tags->rqs)
315 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
f26cdc85
KB
316}
317
c7b1bf5c
BVA
318/**
319 * blk_mq_all_tag_busy_iter - iterate over all started requests in a tag map
320 * @tags: Tag map to iterate over.
321 * @fn: Pointer to the function that will be called for each started
322 * request. @fn will be called as follows: @fn(rq, @priv,
323 * reserved) where rq is a pointer to a request. 'reserved'
ab11fe5a
JA
324 * indicates whether or not @rq is a reserved request. Return
325 * true to continue iterating tags, false to stop.
c7b1bf5c
BVA
326 * @priv: Will be passed as second argument to @fn.
327 */
e8f1e163
SG
328static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
329 busy_tag_iter_fn *fn, void *priv)
f26cdc85
KB
330{
331 if (tags->nr_reserved_tags)
88459642
OS
332 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
333 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
f26cdc85 334}
f26cdc85 335
c7b1bf5c
BVA
336/**
337 * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
338 * @tagset: Tag set to iterate over.
339 * @fn: Pointer to the function that will be called for each started
340 * request. @fn will be called as follows: @fn(rq, @priv,
341 * reserved) where rq is a pointer to a request. 'reserved'
ab11fe5a
JA
342 * indicates whether or not @rq is a reserved request. Return
343 * true to continue iterating tags, false to stop.
c7b1bf5c
BVA
344 * @priv: Will be passed as second argument to @fn.
345 */
e0489487
SG
346void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
347 busy_tag_iter_fn *fn, void *priv)
348{
349 int i;
350
351 for (i = 0; i < tagset->nr_hw_queues; i++) {
352 if (tagset->tags && tagset->tags[i])
353 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
354 }
355}
356EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
357
f9934a80
ML
358static bool blk_mq_tagset_count_completed_rqs(struct request *rq,
359 void *data, bool reserved)
360{
361 unsigned *count = data;
362
363 if (blk_mq_request_completed(rq))
364 (*count)++;
365 return true;
366}
367
368/**
369 * blk_mq_tagset_wait_completed_request - wait until all completed req's
370 * complete funtion is run
371 * @tagset: Tag set to drain completed request
372 *
373 * Note: This function has to be run after all IO queues are shutdown
374 */
375void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
376{
377 while (true) {
378 unsigned count = 0;
379
380 blk_mq_tagset_busy_iter(tagset,
381 blk_mq_tagset_count_completed_rqs, &count);
382 if (!count)
383 break;
384 msleep(5);
385 }
386}
387EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
388
c7b1bf5c
BVA
389/**
390 * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
391 * @q: Request queue to examine.
392 * @fn: Pointer to the function that will be called for each request
393 * on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
394 * reserved) where rq is a pointer to a request and hctx points
395 * to the hardware queue associated with the request. 'reserved'
396 * indicates whether or not @rq is a reserved request.
397 * @priv: Will be passed as third argument to @fn.
398 *
399 * Note: if @q->tag_set is shared with other request queues then @fn will be
400 * called for all requests on all queues that share that tag set and not only
401 * for requests associated with @q.
402 */
0bf6cd5b 403void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
81481eb4 404 void *priv)
320ae51f 405{
0bf6cd5b
CH
406 struct blk_mq_hw_ctx *hctx;
407 int i;
408
f5bbbbe4 409 /*
c7b1bf5c
BVA
410 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and queue_hw_ctx
411 * while the queue is frozen. So we can use q_usage_counter to avoid
412 * racing with it. __blk_mq_update_nr_hw_queues() uses
413 * synchronize_rcu() to ensure this function left the critical section
414 * below.
f5bbbbe4 415 */
530ca2c9 416 if (!percpu_ref_tryget(&q->q_usage_counter))
f5bbbbe4 417 return;
0bf6cd5b
CH
418
419 queue_for_each_hw_ctx(q, hctx, i) {
420 struct blk_mq_tags *tags = hctx->tags;
421
422 /*
c7b1bf5c 423 * If no software queues are currently mapped to this
0bf6cd5b
CH
424 * hardware queue, there's nothing to check
425 */
426 if (!blk_mq_hw_queue_mapped(hctx))
427 continue;
428
429 if (tags->nr_reserved_tags)
88459642
OS
430 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
431 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
4bb659b1 432 }
530ca2c9 433 blk_queue_exit(q);
4bb659b1
JA
434}
435
f4a644db
OS
436static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
437 bool round_robin, int node)
4bb659b1 438{
f4a644db
OS
439 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
440 node);
4bb659b1
JA
441}
442
443static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
24391c0d 444 int node, int alloc_policy)
4bb659b1
JA
445{
446 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
f4a644db 447 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
4bb659b1 448
f4a644db 449 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
88459642 450 goto free_tags;
f4a644db
OS
451 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin,
452 node))
88459642 453 goto free_bitmap_tags;
4bb659b1
JA
454
455 return tags;
88459642
OS
456free_bitmap_tags:
457 sbitmap_queue_free(&tags->bitmap_tags);
458free_tags:
4bb659b1
JA
459 kfree(tags);
460 return NULL;
461}
462
320ae51f 463struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
24391c0d
SL
464 unsigned int reserved_tags,
465 int node, int alloc_policy)
320ae51f 466{
320ae51f 467 struct blk_mq_tags *tags;
320ae51f
JA
468
469 if (total_tags > BLK_MQ_TAG_MAX) {
470 pr_err("blk-mq: tag depth too large\n");
471 return NULL;
472 }
473
474 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
475 if (!tags)
476 return NULL;
477
320ae51f
JA
478 tags->nr_tags = total_tags;
479 tags->nr_reserved_tags = reserved_tags;
320ae51f 480
24391c0d 481 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
320ae51f
JA
482}
483
484void blk_mq_free_tags(struct blk_mq_tags *tags)
485{
88459642
OS
486 sbitmap_queue_free(&tags->bitmap_tags);
487 sbitmap_queue_free(&tags->breserved_tags);
320ae51f
JA
488 kfree(tags);
489}
490
70f36b60
JA
491int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
492 struct blk_mq_tags **tagsptr, unsigned int tdepth,
493 bool can_grow)
e3a2b3f9 494{
70f36b60
JA
495 struct blk_mq_tags *tags = *tagsptr;
496
497 if (tdepth <= tags->nr_reserved_tags)
e3a2b3f9
JA
498 return -EINVAL;
499
500 /*
70f36b60
JA
501 * If we are allowed to grow beyond the original size, allocate
502 * a new set of tags before freeing the old one.
e3a2b3f9 503 */
70f36b60
JA
504 if (tdepth > tags->nr_tags) {
505 struct blk_mq_tag_set *set = hctx->queue->tag_set;
506 struct blk_mq_tags *new;
507 bool ret;
508
509 if (!can_grow)
510 return -EINVAL;
511
512 /*
513 * We need some sort of upper limit, set it high enough that
514 * no valid use cases should require more.
515 */
516 if (tdepth > 16 * BLKDEV_MAX_RQ)
517 return -EINVAL;
518
75d6e175
ML
519 new = blk_mq_alloc_rq_map(set, hctx->queue_num, tdepth,
520 tags->nr_reserved_tags);
70f36b60
JA
521 if (!new)
522 return -ENOMEM;
523 ret = blk_mq_alloc_rqs(set, new, hctx->queue_num, tdepth);
524 if (ret) {
525 blk_mq_free_rq_map(new);
526 return -ENOMEM;
527 }
528
529 blk_mq_free_rqs(set, *tagsptr, hctx->queue_num);
530 blk_mq_free_rq_map(*tagsptr);
531 *tagsptr = new;
532 } else {
533 /*
534 * Don't need (or can't) update reserved tags here, they
535 * remain static and should never need resizing.
536 */
75d6e175
ML
537 sbitmap_queue_resize(&tags->bitmap_tags,
538 tdepth - tags->nr_reserved_tags);
70f36b60 539 }
88459642 540
e3a2b3f9
JA
541 return 0;
542}
543
205fb5f5
BVA
544/**
545 * blk_mq_unique_tag() - return a tag that is unique queue-wide
546 * @rq: request for which to compute a unique tag
547 *
548 * The tag field in struct request is unique per hardware queue but not over
549 * all hardware queues. Hence this function that returns a tag with the
550 * hardware context index in the upper bits and the per hardware queue tag in
551 * the lower bits.
552 *
553 * Note: When called for a request that is queued on a non-multiqueue request
554 * queue, the hardware context index is set to zero.
555 */
556u32 blk_mq_unique_tag(struct request *rq)
557{
ea4f995e 558 return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
205fb5f5
BVA
559 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
560}
561EXPORT_SYMBOL(blk_mq_unique_tag);