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/delay.h>
15 #include "blk-mq-sched.h"
18 * Recalculate wakeup batch when tag is shared by hctx.
20 static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
26 sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
28 sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
33 * If a previously inactive queue goes active, bump the active user count.
34 * We need to do this before try to allocate driver tag, then even if fail
35 * to get tag when first time, the other shared-tag users could reserve
38 void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
41 struct blk_mq_tags *tags = hctx->tags;
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 spin_lock_irq(&tags->lock);
56 users = tags->active_queues + 1;
57 WRITE_ONCE(tags->active_queues, users);
58 blk_mq_update_wake_batch(tags, users);
59 spin_unlock_irq(&tags->lock);
63 * Wakeup all potentially sleeping on tags
65 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
67 sbitmap_queue_wake_all(&tags->bitmap_tags);
69 sbitmap_queue_wake_all(&tags->breserved_tags);
73 * If a previously busy queue goes inactive, potential waiters could now
74 * be allowed to queue. Wake them up and check.
76 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
78 struct blk_mq_tags *tags = hctx->tags;
81 if (blk_mq_is_shared_tags(hctx->flags)) {
82 struct request_queue *q = hctx->queue;
84 if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
88 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
92 spin_lock_irq(&tags->lock);
93 users = tags->active_queues - 1;
94 WRITE_ONCE(tags->active_queues, users);
95 blk_mq_update_wake_batch(tags, users);
96 spin_unlock_irq(&tags->lock);
98 blk_mq_tag_wakeup_all(tags, false);
101 static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
102 struct sbitmap_queue *bt)
104 if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
105 !hctx_may_queue(data->hctx, bt))
106 return BLK_MQ_NO_TAG;
108 if (data->shallow_depth)
109 return sbitmap_queue_get_shallow(bt, data->shallow_depth);
111 return __sbitmap_queue_get(bt);
114 unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
115 unsigned int *offset)
117 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
118 struct sbitmap_queue *bt = &tags->bitmap_tags;
121 if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
122 data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
124 ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
125 *offset += tags->nr_reserved_tags;
129 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
131 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
132 struct sbitmap_queue *bt;
133 struct sbq_wait_state *ws;
134 DEFINE_SBQ_WAIT(wait);
135 unsigned int tag_offset;
138 if (data->flags & BLK_MQ_REQ_RESERVED) {
139 if (unlikely(!tags->nr_reserved_tags)) {
141 return BLK_MQ_NO_TAG;
143 bt = &tags->breserved_tags;
146 bt = &tags->bitmap_tags;
147 tag_offset = tags->nr_reserved_tags;
150 tag = __blk_mq_get_tag(data, bt);
151 if (tag != BLK_MQ_NO_TAG)
154 if (data->flags & BLK_MQ_REQ_NOWAIT)
155 return BLK_MQ_NO_TAG;
157 ws = bt_wait_ptr(bt, data->hctx);
159 struct sbitmap_queue *bt_prev;
162 * We're out of tags on this hardware queue, kick any
163 * pending IO submits before going to sleep waiting for
166 blk_mq_run_hw_queue(data->hctx, false);
169 * Retry tag allocation after running the hardware queue,
170 * as running the queue may also have found completions.
172 tag = __blk_mq_get_tag(data, bt);
173 if (tag != BLK_MQ_NO_TAG)
176 sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
178 tag = __blk_mq_get_tag(data, bt);
179 if (tag != BLK_MQ_NO_TAG)
185 sbitmap_finish_wait(bt, ws, &wait);
187 data->ctx = blk_mq_get_ctx(data->q);
188 data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
190 tags = blk_mq_tags_from_data(data);
191 if (data->flags & BLK_MQ_REQ_RESERVED)
192 bt = &tags->breserved_tags;
194 bt = &tags->bitmap_tags;
197 * If destination hw queue is changed, fake wake up on
198 * previous queue for compensating the wake up miss, so
199 * other allocations on previous queue won't be starved.
202 sbitmap_queue_wake_up(bt_prev, 1);
204 ws = bt_wait_ptr(bt, data->hctx);
207 sbitmap_finish_wait(bt, ws, &wait);
211 * Give up this allocation if the hctx is inactive. The caller will
212 * retry on an active hctx.
214 if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
215 blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
216 return BLK_MQ_NO_TAG;
218 return tag + tag_offset;
221 void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
224 if (!blk_mq_tag_is_reserved(tags, tag)) {
225 const int real_tag = tag - tags->nr_reserved_tags;
227 BUG_ON(real_tag >= tags->nr_tags);
228 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
230 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
234 void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
236 sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
240 struct bt_iter_data {
241 struct blk_mq_hw_ctx *hctx;
242 struct request_queue *q;
243 busy_tag_iter_fn *fn;
248 static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
254 spin_lock_irqsave(&tags->lock, flags);
255 rq = tags->rqs[bitnr];
256 if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
258 spin_unlock_irqrestore(&tags->lock, flags);
262 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
264 struct bt_iter_data *iter_data = data;
265 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
266 struct request_queue *q = iter_data->q;
267 struct blk_mq_tag_set *set = q->tag_set;
268 struct blk_mq_tags *tags;
272 if (blk_mq_is_shared_tags(set->flags))
273 tags = set->shared_tags;
277 if (!iter_data->reserved)
278 bitnr += tags->nr_reserved_tags;
280 * We can hit rq == NULL here, because the tagging functions
281 * test and set the bit before assigning ->rqs[].
283 rq = blk_mq_find_and_get_req(tags, bitnr);
287 if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
288 ret = iter_data->fn(rq, iter_data->data);
289 blk_mq_put_rq_ref(rq);
294 * bt_for_each - iterate over the requests associated with a hardware queue
295 * @hctx: Hardware queue to examine.
296 * @q: Request queue to examine.
297 * @bt: sbitmap to examine. This is either the breserved_tags member
298 * or the bitmap_tags member of struct blk_mq_tags.
299 * @fn: Pointer to the function that will be called for each request
300 * associated with @hctx that has been assigned a driver tag.
301 * @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
302 * where rq is a pointer to a request. Return true to continue
303 * iterating tags, false to stop.
304 * @data: Will be passed as third argument to @fn.
305 * @reserved: Indicates whether @bt is the breserved_tags member or the
306 * bitmap_tags member of struct blk_mq_tags.
308 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
309 struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
310 void *data, bool reserved)
312 struct bt_iter_data iter_data = {
316 .reserved = reserved,
320 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
323 struct bt_tags_iter_data {
324 struct blk_mq_tags *tags;
325 busy_tag_iter_fn *fn;
330 #define BT_TAG_ITER_RESERVED (1 << 0)
331 #define BT_TAG_ITER_STARTED (1 << 1)
332 #define BT_TAG_ITER_STATIC_RQS (1 << 2)
334 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
336 struct bt_tags_iter_data *iter_data = data;
337 struct blk_mq_tags *tags = iter_data->tags;
340 bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
342 if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
343 bitnr += tags->nr_reserved_tags;
346 * We can hit rq == NULL here, because the tagging functions
347 * test and set the bit before assigning ->rqs[].
350 rq = tags->static_rqs[bitnr];
352 rq = blk_mq_find_and_get_req(tags, bitnr);
356 if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
357 blk_mq_request_started(rq))
358 ret = iter_data->fn(rq, iter_data->data);
359 if (!iter_static_rqs)
360 blk_mq_put_rq_ref(rq);
365 * bt_tags_for_each - iterate over the requests in a tag map
366 * @tags: Tag map to iterate over.
367 * @bt: sbitmap to examine. This is either the breserved_tags member
368 * or the bitmap_tags member of struct blk_mq_tags.
369 * @fn: Pointer to the function that will be called for each started
370 * request. @fn will be called as follows: @fn(rq, @data,
371 * @reserved) where rq is a pointer to a request. Return true
372 * to continue iterating tags, false to stop.
373 * @data: Will be passed as second argument to @fn.
374 * @flags: BT_TAG_ITER_*
376 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
377 busy_tag_iter_fn *fn, void *data, unsigned int flags)
379 struct bt_tags_iter_data iter_data = {
387 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
390 static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
391 busy_tag_iter_fn *fn, void *priv, unsigned int flags)
393 WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
395 if (tags->nr_reserved_tags)
396 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
397 flags | BT_TAG_ITER_RESERVED);
398 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
402 * blk_mq_all_tag_iter - iterate over all requests in a tag map
403 * @tags: Tag map to iterate over.
404 * @fn: Pointer to the function that will be called for each
405 * request. @fn will be called as follows: @fn(rq, @priv,
406 * reserved) where rq is a pointer to a request. 'reserved'
407 * indicates whether or not @rq is a reserved request. Return
408 * true to continue iterating tags, false to stop.
409 * @priv: Will be passed as second argument to @fn.
411 * Caller has to pass the tag map from which requests are allocated.
413 void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
416 __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
420 * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
421 * @tagset: Tag set to iterate over.
422 * @fn: Pointer to the function that will be called for each started
423 * request. @fn will be called as follows: @fn(rq, @priv,
424 * reserved) where rq is a pointer to a request. 'reserved'
425 * indicates whether or not @rq is a reserved request. Return
426 * true to continue iterating tags, false to stop.
427 * @priv: Will be passed as second argument to @fn.
429 * We grab one request reference before calling @fn and release it after
432 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
433 busy_tag_iter_fn *fn, void *priv)
435 unsigned int flags = tagset->flags;
438 nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
440 for (i = 0; i < nr_tags; i++) {
441 if (tagset->tags && tagset->tags[i])
442 __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
443 BT_TAG_ITER_STARTED);
446 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
448 static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
450 unsigned *count = data;
452 if (blk_mq_request_completed(rq))
458 * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
459 * completions have finished.
460 * @tagset: Tag set to drain completed request
462 * Note: This function has to be run after all IO queues are shutdown
464 void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
469 blk_mq_tagset_busy_iter(tagset,
470 blk_mq_tagset_count_completed_rqs, &count);
476 EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
479 * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
480 * @q: Request queue to examine.
481 * @fn: Pointer to the function that will be called for each request
482 * on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
483 * reserved) where rq is a pointer to a request and hctx points
484 * to the hardware queue associated with the request. 'reserved'
485 * indicates whether or not @rq is a reserved request.
486 * @priv: Will be passed as third argument to @fn.
488 * Note: if @q->tag_set is shared with other request queues then @fn will be
489 * called for all requests on all queues that share that tag set and not only
490 * for requests associated with @q.
492 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
496 * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
497 * while the queue is frozen. So we can use q_usage_counter to avoid
500 if (!percpu_ref_tryget(&q->q_usage_counter))
503 if (blk_mq_is_shared_tags(q->tag_set->flags)) {
504 struct blk_mq_tags *tags = q->tag_set->shared_tags;
505 struct sbitmap_queue *bresv = &tags->breserved_tags;
506 struct sbitmap_queue *btags = &tags->bitmap_tags;
508 if (tags->nr_reserved_tags)
509 bt_for_each(NULL, q, bresv, fn, priv, true);
510 bt_for_each(NULL, q, btags, fn, priv, false);
512 struct blk_mq_hw_ctx *hctx;
515 queue_for_each_hw_ctx(q, hctx, i) {
516 struct blk_mq_tags *tags = hctx->tags;
517 struct sbitmap_queue *bresv = &tags->breserved_tags;
518 struct sbitmap_queue *btags = &tags->bitmap_tags;
521 * If no software queues are currently mapped to this
522 * hardware queue, there's nothing to check
524 if (!blk_mq_hw_queue_mapped(hctx))
527 if (tags->nr_reserved_tags)
528 bt_for_each(hctx, q, bresv, fn, priv, true);
529 bt_for_each(hctx, q, btags, fn, priv, false);
535 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
536 bool round_robin, int node)
538 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
542 int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
543 struct sbitmap_queue *breserved_tags,
544 unsigned int queue_depth, unsigned int reserved,
545 int node, int alloc_policy)
547 unsigned int depth = queue_depth - reserved;
548 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
550 if (bt_alloc(bitmap_tags, depth, round_robin, node))
552 if (bt_alloc(breserved_tags, reserved, round_robin, node))
553 goto free_bitmap_tags;
558 sbitmap_queue_free(bitmap_tags);
562 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
563 unsigned int reserved_tags,
564 int node, int alloc_policy)
566 struct blk_mq_tags *tags;
568 if (total_tags > BLK_MQ_TAG_MAX) {
569 pr_err("blk-mq: tag depth too large\n");
573 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
577 tags->nr_tags = total_tags;
578 tags->nr_reserved_tags = reserved_tags;
579 spin_lock_init(&tags->lock);
581 if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
582 total_tags, reserved_tags, node,
590 void blk_mq_free_tags(struct blk_mq_tags *tags)
592 sbitmap_queue_free(&tags->bitmap_tags);
593 sbitmap_queue_free(&tags->breserved_tags);
597 int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
598 struct blk_mq_tags **tagsptr, unsigned int tdepth,
601 struct blk_mq_tags *tags = *tagsptr;
603 if (tdepth <= tags->nr_reserved_tags)
607 * If we are allowed to grow beyond the original size, allocate
608 * a new set of tags before freeing the old one.
610 if (tdepth > tags->nr_tags) {
611 struct blk_mq_tag_set *set = hctx->queue->tag_set;
612 struct blk_mq_tags *new;
618 * We need some sort of upper limit, set it high enough that
619 * no valid use cases should require more.
621 if (tdepth > MAX_SCHED_RQ)
625 * Only the sbitmap needs resizing since we allocated the max
628 if (blk_mq_is_shared_tags(set->flags))
631 new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
635 blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
639 * Don't need (or can't) update reserved tags here, they
640 * remain static and should never need resizing.
642 sbitmap_queue_resize(&tags->bitmap_tags,
643 tdepth - tags->nr_reserved_tags);
649 void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
651 struct blk_mq_tags *tags = set->shared_tags;
653 sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
656 void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
658 sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
659 q->nr_requests - q->tag_set->reserved_tags);
663 * blk_mq_unique_tag() - return a tag that is unique queue-wide
664 * @rq: request for which to compute a unique tag
666 * The tag field in struct request is unique per hardware queue but not over
667 * all hardware queues. Hence this function that returns a tag with the
668 * hardware context index in the upper bits and the per hardware queue tag in
671 * Note: When called for a request that is queued on a non-multiqueue request
672 * queue, the hardware context index is set to zero.
674 u32 blk_mq_unique_tag(struct request *rq)
676 return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
677 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
679 EXPORT_SYMBOL(blk_mq_unique_tag);