1 // SPDX-License-Identifier: GPL-2.0
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
7 * Copyright (C) 2013-2014 Jens Axboe
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/blk-mq.h>
13 #include <linux/delay.h>
16 #include "blk-mq-sched.h"
19 * Recalculate wakeup batch when tag is shared by hctx.
21 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
27 sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
29 sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
34 * If a previously inactive queue goes active, bump the active user count.
35 * We need to do this before try to allocate driver tag, then even if fail
36 * to get tag when first time, the other shared-tag users could reserve
39 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
43 if (blk_mq_is_shared_tags(hctx->flags)) {
44 struct request_queue *q = hctx->queue;
46 if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
48 set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags);
50 if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
52 set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state);
55 users = atomic_inc_return(&hctx->tags->active_queues);
57 blk_mq_update_wake_batch(hctx->tags, users);
61 * Wakeup all potentially sleeping on tags
63 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
65 sbitmap_queue_wake_all(&tags->bitmap_tags);
67 sbitmap_queue_wake_all(&tags->breserved_tags);
71 * If a previously busy queue goes inactive, potential waiters could now
72 * be allowed to queue. Wake them up and check.
74 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
76 struct blk_mq_tags *tags = hctx->tags;
79 if (blk_mq_is_shared_tags(hctx->flags)) {
80 struct request_queue *q = hctx->queue;
82 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
86 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
90 users = atomic_dec_return(&tags->active_queues);
92 blk_mq_update_wake_batch(tags, users);
94 blk_mq_tag_wakeup_all(tags, false);
97 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
98 struct sbitmap_queue *bt)
100 if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
101 !hctx_may_queue(data->hctx, bt))
102 return BLK_MQ_NO_TAG;
104 if (data->shallow_depth)
105 return sbitmap_queue_get_shallow(bt, data->shallow_depth);
107 return __sbitmap_queue_get(bt);
110 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
111 unsigned int *offset)
113 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
114 struct sbitmap_queue *bt = &tags->bitmap_tags;
117 if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
118 data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
120 ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
121 *offset += tags->nr_reserved_tags;
125 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
127 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
128 struct sbitmap_queue *bt;
129 struct sbq_wait_state *ws;
130 DEFINE_SBQ_WAIT(wait);
131 unsigned int tag_offset;
134 if (data->flags & BLK_MQ_REQ_RESERVED) {
135 if (unlikely(!tags->nr_reserved_tags)) {
137 return BLK_MQ_NO_TAG;
139 bt = &tags->breserved_tags;
142 bt = &tags->bitmap_tags;
143 tag_offset = tags->nr_reserved_tags;
146 tag = __blk_mq_get_tag(data, bt);
147 if (tag != BLK_MQ_NO_TAG)
150 if (data->flags & BLK_MQ_REQ_NOWAIT)
151 return BLK_MQ_NO_TAG;
153 ws = bt_wait_ptr(bt, data->hctx);
155 struct sbitmap_queue *bt_prev;
158 * We're out of tags on this hardware queue, kick any
159 * pending IO submits before going to sleep waiting for
162 blk_mq_run_hw_queue(data->hctx, false);
165 * Retry tag allocation after running the hardware queue,
166 * as running the queue may also have found completions.
168 tag = __blk_mq_get_tag(data, bt);
169 if (tag != BLK_MQ_NO_TAG)
172 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
174 tag = __blk_mq_get_tag(data, bt);
175 if (tag != BLK_MQ_NO_TAG)
181 sbitmap_finish_wait(bt, ws, &wait);
183 data->ctx = blk_mq_get_ctx(data->q);
184 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
186 tags = blk_mq_tags_from_data(data);
187 if (data->flags & BLK_MQ_REQ_RESERVED)
188 bt = &tags->breserved_tags;
190 bt = &tags->bitmap_tags;
193 * If destination hw queue is changed, fake wake up on
194 * previous queue for compensating the wake up miss, so
195 * other allocations on previous queue won't be starved.
198 sbitmap_queue_wake_up(bt_prev, 1);
200 ws = bt_wait_ptr(bt, data->hctx);
203 sbitmap_finish_wait(bt, ws, &wait);
207 * Give up this allocation if the hctx is inactive. The caller will
208 * retry on an active hctx.
210 if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
211 blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
212 return BLK_MQ_NO_TAG;
214 return tag + tag_offset;
217 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
220 if (!blk_mq_tag_is_reserved(tags, tag)) {
221 const int real_tag = tag - tags->nr_reserved_tags;
223 BUG_ON(real_tag >= tags->nr_tags);
224 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
226 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
230 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
232 sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
236 struct bt_iter_data {
237 struct blk_mq_hw_ctx *hctx;
238 struct request_queue *q;
239 busy_tag_iter_fn *fn;
244 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
250 spin_lock_irqsave(&tags->lock, flags);
251 rq = tags->rqs[bitnr];
252 if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
254 spin_unlock_irqrestore(&tags->lock, flags);
258 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
260 struct bt_iter_data *iter_data = data;
261 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
262 struct request_queue *q = iter_data->q;
263 struct blk_mq_tag_set *set = q->tag_set;
264 struct blk_mq_tags *tags;
268 if (blk_mq_is_shared_tags(set->flags))
269 tags = set->shared_tags;
273 if (!iter_data->reserved)
274 bitnr += tags->nr_reserved_tags;
276 * We can hit rq == NULL here, because the tagging functions
277 * test and set the bit before assigning ->rqs[].
279 rq = blk_mq_find_and_get_req(tags, bitnr);
283 if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
284 ret = iter_data->fn(rq, iter_data->data);
285 blk_mq_put_rq_ref(rq);
290 * bt_for_each - iterate over the requests associated with a hardware queue
291 * @hctx: Hardware queue to examine.
292 * @q: Request queue to examine.
293 * @bt: sbitmap to examine. This is either the breserved_tags member
294 * or the bitmap_tags member of struct blk_mq_tags.
295 * @fn: Pointer to the function that will be called for each request
296 * associated with @hctx that has been assigned a driver tag.
297 * @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
298 * where rq is a pointer to a request. Return true to continue
299 * iterating tags, false to stop.
300 * @data: Will be passed as third argument to @fn.
301 * @reserved: Indicates whether @bt is the breserved_tags member or the
302 * bitmap_tags member of struct blk_mq_tags.
304 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
305 struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
306 void *data, bool reserved)
308 struct bt_iter_data iter_data = {
312 .reserved = reserved,
316 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
319 struct bt_tags_iter_data {
320 struct blk_mq_tags *tags;
321 busy_tag_iter_fn *fn;
326 #define BT_TAG_ITER_RESERVED (1 << 0)
327 #define BT_TAG_ITER_STARTED (1 << 1)
328 #define BT_TAG_ITER_STATIC_RQS (1 << 2)
330 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
332 struct bt_tags_iter_data *iter_data = data;
333 struct blk_mq_tags *tags = iter_data->tags;
336 bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
338 if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
339 bitnr += tags->nr_reserved_tags;
342 * We can hit rq == NULL here, because the tagging functions
343 * test and set the bit before assigning ->rqs[].
346 rq = tags->static_rqs[bitnr];
348 rq = blk_mq_find_and_get_req(tags, bitnr);
352 if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
353 blk_mq_request_started(rq))
354 ret = iter_data->fn(rq, iter_data->data);
355 if (!iter_static_rqs)
356 blk_mq_put_rq_ref(rq);
361 * bt_tags_for_each - iterate over the requests in a tag map
362 * @tags: Tag map to iterate over.
363 * @bt: sbitmap to examine. This is either the breserved_tags member
364 * or the bitmap_tags member of struct blk_mq_tags.
365 * @fn: Pointer to the function that will be called for each started
366 * request. @fn will be called as follows: @fn(rq, @data,
367 * @reserved) where rq is a pointer to a request. Return true
368 * to continue iterating tags, false to stop.
369 * @data: Will be passed as second argument to @fn.
370 * @flags: BT_TAG_ITER_*
372 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
373 busy_tag_iter_fn *fn, void *data, unsigned int flags)
375 struct bt_tags_iter_data iter_data = {
383 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
386 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
387 busy_tag_iter_fn *fn, void *priv, unsigned int flags)
389 WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
391 if (tags->nr_reserved_tags)
392 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
393 flags | BT_TAG_ITER_RESERVED);
394 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
398 * blk_mq_all_tag_iter - iterate over all requests in a tag map
399 * @tags: Tag map to iterate over.
400 * @fn: Pointer to the function that will be called for each
401 * request. @fn will be called as follows: @fn(rq, @priv,
402 * reserved) where rq is a pointer to a request. 'reserved'
403 * indicates whether or not @rq is a reserved request. Return
404 * true to continue iterating tags, false to stop.
405 * @priv: Will be passed as second argument to @fn.
407 * Caller has to pass the tag map from which requests are allocated.
409 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
412 __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
416 * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
417 * @tagset: Tag set to iterate over.
418 * @fn: Pointer to the function that will be called for each started
419 * request. @fn will be called as follows: @fn(rq, @priv,
420 * reserved) where rq is a pointer to a request. 'reserved'
421 * indicates whether or not @rq is a reserved request. Return
422 * true to continue iterating tags, false to stop.
423 * @priv: Will be passed as second argument to @fn.
425 * We grab one request reference before calling @fn and release it after
428 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
429 busy_tag_iter_fn *fn, void *priv)
431 unsigned int flags = tagset->flags;
434 nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
436 for (i = 0; i < nr_tags; i++) {
437 if (tagset->tags && tagset->tags[i])
438 __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
439 BT_TAG_ITER_STARTED);
442 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
444 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
446 unsigned *count = data;
448 if (blk_mq_request_completed(rq))
454 * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
455 * completions have finished.
456 * @tagset: Tag set to drain completed request
458 * Note: This function has to be run after all IO queues are shutdown
460 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
465 blk_mq_tagset_busy_iter(tagset,
466 blk_mq_tagset_count_completed_rqs, &count);
472 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
475 * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
476 * @q: Request queue to examine.
477 * @fn: Pointer to the function that will be called for each request
478 * on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
479 * reserved) where rq is a pointer to a request and hctx points
480 * to the hardware queue associated with the request. 'reserved'
481 * indicates whether or not @rq is a reserved request.
482 * @priv: Will be passed as third argument to @fn.
484 * Note: if @q->tag_set is shared with other request queues then @fn will be
485 * called for all requests on all queues that share that tag set and not only
486 * for requests associated with @q.
488 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
492 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
493 * while the queue is frozen. So we can use q_usage_counter to avoid
496 if (!percpu_ref_tryget(&q->q_usage_counter))
499 if (blk_mq_is_shared_tags(q->tag_set->flags)) {
500 struct blk_mq_tags *tags = q->tag_set->shared_tags;
501 struct sbitmap_queue *bresv = &tags->breserved_tags;
502 struct sbitmap_queue *btags = &tags->bitmap_tags;
504 if (tags->nr_reserved_tags)
505 bt_for_each(NULL, q, bresv, fn, priv, true);
506 bt_for_each(NULL, q, btags, fn, priv, false);
508 struct blk_mq_hw_ctx *hctx;
511 queue_for_each_hw_ctx(q, hctx, i) {
512 struct blk_mq_tags *tags = hctx->tags;
513 struct sbitmap_queue *bresv = &tags->breserved_tags;
514 struct sbitmap_queue *btags = &tags->bitmap_tags;
517 * If no software queues are currently mapped to this
518 * hardware queue, there's nothing to check
520 if (!blk_mq_hw_queue_mapped(hctx))
523 if (tags->nr_reserved_tags)
524 bt_for_each(hctx, q, bresv, fn, priv, true);
525 bt_for_each(hctx, q, btags, fn, priv, false);
531 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
532 bool round_robin, int node)
534 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
538 int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
539 struct sbitmap_queue *breserved_tags,
540 unsigned int queue_depth, unsigned int reserved,
541 int node, int alloc_policy)
543 unsigned int depth = queue_depth - reserved;
544 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
546 if (bt_alloc(bitmap_tags, depth, round_robin, node))
548 if (bt_alloc(breserved_tags, reserved, round_robin, node))
549 goto free_bitmap_tags;
554 sbitmap_queue_free(bitmap_tags);
558 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
559 unsigned int reserved_tags,
560 int node, int alloc_policy)
562 struct blk_mq_tags *tags;
564 if (total_tags > BLK_MQ_TAG_MAX) {
565 pr_err("blk-mq: tag depth too large\n");
569 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
573 tags->nr_tags = total_tags;
574 tags->nr_reserved_tags = reserved_tags;
575 spin_lock_init(&tags->lock);
577 if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
578 total_tags, reserved_tags, node,
586 void blk_mq_free_tags(struct blk_mq_tags *tags)
588 sbitmap_queue_free(&tags->bitmap_tags);
589 sbitmap_queue_free(&tags->breserved_tags);
593 int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
594 struct blk_mq_tags **tagsptr, unsigned int tdepth,
597 struct blk_mq_tags *tags = *tagsptr;
599 if (tdepth <= tags->nr_reserved_tags)
603 * If we are allowed to grow beyond the original size, allocate
604 * a new set of tags before freeing the old one.
606 if (tdepth > tags->nr_tags) {
607 struct blk_mq_tag_set *set = hctx->queue->tag_set;
608 struct blk_mq_tags *new;
614 * We need some sort of upper limit, set it high enough that
615 * no valid use cases should require more.
617 if (tdepth > MAX_SCHED_RQ)
621 * Only the sbitmap needs resizing since we allocated the max
624 if (blk_mq_is_shared_tags(set->flags))
627 new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
631 blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
635 * Don't need (or can't) update reserved tags here, they
636 * remain static and should never need resizing.
638 sbitmap_queue_resize(&tags->bitmap_tags,
639 tdepth - tags->nr_reserved_tags);
645 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
647 struct blk_mq_tags *tags = set->shared_tags;
649 sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
652 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
654 sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
655 q->nr_requests - q->tag_set->reserved_tags);
659 * blk_mq_unique_tag() - return a tag that is unique queue-wide
660 * @rq: request for which to compute a unique tag
662 * The tag field in struct request is unique per hardware queue but not over
663 * all hardware queues. Hence this function that returns a tag with the
664 * hardware context index in the upper bits and the per hardware queue tag in
667 * Note: When called for a request that is queued on a non-multiqueue request
668 * queue, the hardware context index is set to zero.
670 u32 blk_mq_unique_tag(struct request *rq)
672 return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
673 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
675 EXPORT_SYMBOL(blk_mq_unique_tag);