2 * Tag allocation using scalable bitmaps. Uses active queue tracking to support
3 * fairer distribution of tags between multiple submitters when a shared tag map
6 * Copyright (C) 2013-2014 Jens Axboe
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/random.h>
12 #include <linux/blk-mq.h>
15 #include "blk-mq-tag.h"
17 bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
22 return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
26 * If a previously inactive queue goes active, bump the active user count.
28 bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
30 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
31 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
32 atomic_inc(&hctx->tags->active_queues);
38 * Wakeup all potentially sleeping on tags
40 void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
42 sbitmap_queue_wake_all(&tags->bitmap_tags);
44 sbitmap_queue_wake_all(&tags->breserved_tags);
48 * If a previously busy queue goes inactive, potential waiters could now
49 * be allowed to queue. Wake them up and check.
51 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
53 struct blk_mq_tags *tags = hctx->tags;
55 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
58 atomic_dec(&tags->active_queues);
60 blk_mq_tag_wakeup_all(tags, false);
64 * For shared tag users, we track the number of currently active users
65 * and attempt to provide a fair share of the tag depth for each of them.
67 static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
68 struct sbitmap_queue *bt)
70 unsigned int depth, users;
72 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
74 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
78 * Don't try dividing an ant
80 if (bt->sb.depth == 1)
83 users = atomic_read(&hctx->tags->active_queues);
88 * Allow at least some tags
90 depth = max((bt->sb.depth + users - 1) / users, 4U);
91 return atomic_read(&hctx->nr_active) < depth;
94 #define BT_ALLOC_RR(tags) (tags->alloc_policy == BLK_TAG_ALLOC_RR)
96 static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
97 struct blk_mq_tags *tags)
99 if (!hctx_may_queue(hctx, bt))
101 return __sbitmap_queue_get(bt, BT_ALLOC_RR(tags));
104 static int bt_get(struct blk_mq_alloc_data *data, struct sbitmap_queue *bt,
105 struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags)
107 struct sbq_wait_state *ws;
111 tag = __bt_get(hctx, bt, tags);
115 if (data->flags & BLK_MQ_REQ_NOWAIT)
118 ws = bt_wait_ptr(bt, hctx);
120 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
122 tag = __bt_get(hctx, bt, tags);
127 * We're out of tags on this hardware queue, kick any
128 * pending IO submits before going to sleep waiting for
129 * some to complete. Note that hctx can be NULL here for
130 * reserved tag allocation.
133 blk_mq_run_hw_queue(hctx, false);
136 * Retry tag allocation after running the hardware queue,
137 * as running the queue may also have found completions.
139 tag = __bt_get(hctx, bt, tags);
143 blk_mq_put_ctx(data->ctx);
147 data->ctx = blk_mq_get_ctx(data->q);
148 data->hctx = data->q->mq_ops->map_queue(data->q,
150 if (data->flags & BLK_MQ_REQ_RESERVED) {
151 bt = &data->hctx->tags->breserved_tags;
154 bt = &hctx->tags->bitmap_tags;
156 finish_wait(&ws->wait, &wait);
157 ws = bt_wait_ptr(bt, hctx);
160 finish_wait(&ws->wait, &wait);
164 static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
168 tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
171 return tag + data->hctx->tags->nr_reserved_tags;
173 return BLK_MQ_TAG_FAIL;
176 static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
180 if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
182 return BLK_MQ_TAG_FAIL;
185 tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL,
188 return BLK_MQ_TAG_FAIL;
193 unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
195 if (data->flags & BLK_MQ_REQ_RESERVED)
196 return __blk_mq_get_reserved_tag(data);
197 return __blk_mq_get_tag(data);
200 void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
203 struct blk_mq_tags *tags = hctx->tags;
205 if (tag >= tags->nr_reserved_tags) {
206 const int real_tag = tag - tags->nr_reserved_tags;
208 BUG_ON(real_tag >= tags->nr_tags);
209 sbitmap_queue_clear(&tags->bitmap_tags, real_tag,
210 BT_ALLOC_RR(tags), ctx->cpu);
212 BUG_ON(tag >= tags->nr_reserved_tags);
213 sbitmap_queue_clear(&tags->breserved_tags, tag,
214 BT_ALLOC_RR(tags), ctx->cpu);
218 struct bt_iter_data {
219 struct blk_mq_hw_ctx *hctx;
225 static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
227 struct bt_iter_data *iter_data = data;
228 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
229 struct blk_mq_tags *tags = hctx->tags;
230 bool reserved = iter_data->reserved;
234 bitnr += tags->nr_reserved_tags;
235 rq = tags->rqs[bitnr];
237 if (rq->q == hctx->queue)
238 iter_data->fn(hctx, rq, iter_data->data, reserved);
242 static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
243 busy_iter_fn *fn, void *data, bool reserved)
245 struct bt_iter_data iter_data = {
249 .reserved = reserved,
252 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
255 struct bt_tags_iter_data {
256 struct blk_mq_tags *tags;
257 busy_tag_iter_fn *fn;
262 static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
264 struct bt_tags_iter_data *iter_data = data;
265 struct blk_mq_tags *tags = iter_data->tags;
266 bool reserved = iter_data->reserved;
270 bitnr += tags->nr_reserved_tags;
271 rq = tags->rqs[bitnr];
273 iter_data->fn(rq, iter_data->data, reserved);
277 static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
278 busy_tag_iter_fn *fn, void *data, bool reserved)
280 struct bt_tags_iter_data iter_data = {
284 .reserved = reserved,
288 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
291 static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
292 busy_tag_iter_fn *fn, void *priv)
294 if (tags->nr_reserved_tags)
295 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
296 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
299 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
300 busy_tag_iter_fn *fn, void *priv)
304 for (i = 0; i < tagset->nr_hw_queues; i++) {
305 if (tagset->tags && tagset->tags[i])
306 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
309 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
311 int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
315 if (!set->ops->reinit_request)
318 for (i = 0; i < set->nr_hw_queues; i++) {
319 struct blk_mq_tags *tags = set->tags[i];
321 for (j = 0; j < tags->nr_tags; j++) {
325 ret = set->ops->reinit_request(set->driver_data,
335 EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
337 void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
340 struct blk_mq_hw_ctx *hctx;
344 queue_for_each_hw_ctx(q, hctx, i) {
345 struct blk_mq_tags *tags = hctx->tags;
348 * If not software queues are currently mapped to this
349 * hardware queue, there's nothing to check
351 if (!blk_mq_hw_queue_mapped(hctx))
354 if (tags->nr_reserved_tags)
355 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
356 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
361 static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
363 return bt->sb.depth - sbitmap_weight(&bt->sb);
366 static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, int node)
368 return sbitmap_queue_init_node(bt, depth, -1, GFP_KERNEL, node);
371 static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
372 int node, int alloc_policy)
374 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
376 tags->alloc_policy = alloc_policy;
378 if (bt_alloc(&tags->bitmap_tags, depth, node))
380 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node))
381 goto free_bitmap_tags;
385 sbitmap_queue_free(&tags->bitmap_tags);
391 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
392 unsigned int reserved_tags,
393 int node, int alloc_policy)
395 struct blk_mq_tags *tags;
397 if (total_tags > BLK_MQ_TAG_MAX) {
398 pr_err("blk-mq: tag depth too large\n");
402 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
406 if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
411 tags->nr_tags = total_tags;
412 tags->nr_reserved_tags = reserved_tags;
414 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
417 void blk_mq_free_tags(struct blk_mq_tags *tags)
419 sbitmap_queue_free(&tags->bitmap_tags);
420 sbitmap_queue_free(&tags->breserved_tags);
421 free_cpumask_var(tags->cpumask);
425 void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
427 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
429 *tag = prandom_u32() % depth;
432 int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
434 tdepth -= tags->nr_reserved_tags;
435 if (tdepth > tags->nr_tags)
439 * Don't need (or can't) update reserved tags here, they remain
440 * static and should never need resizing.
442 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
444 blk_mq_tag_wakeup_all(tags, false);
449 * blk_mq_unique_tag() - return a tag that is unique queue-wide
450 * @rq: request for which to compute a unique tag
452 * The tag field in struct request is unique per hardware queue but not over
453 * all hardware queues. Hence this function that returns a tag with the
454 * hardware context index in the upper bits and the per hardware queue tag in
457 * Note: When called for a request that is queued on a non-multiqueue request
458 * queue, the hardware context index is set to zero.
460 u32 blk_mq_unique_tag(struct request *rq)
462 struct request_queue *q = rq->q;
463 struct blk_mq_hw_ctx *hctx;
467 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
468 hwq = hctx->queue_num;
471 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
472 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
474 EXPORT_SYMBOL(blk_mq_unique_tag);
476 ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
478 char *orig_page = page;
479 unsigned int free, res;
484 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
485 "bits_per_word=%u\n",
486 tags->nr_tags, tags->nr_reserved_tags,
487 1U << tags->bitmap_tags.sb.shift);
489 free = bt_unused_tags(&tags->bitmap_tags);
490 res = bt_unused_tags(&tags->breserved_tags);
492 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
493 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
495 return page - orig_page;