blob: c9a22dbbbda1e701b5ce18b938c48a0cfe463abe [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
Omar Sandoval88459642016-09-17 08:38:44 -06002 * Tag allocation using scalable bitmaps. Uses active queue tracking to support
3 * fairer distribution of tags between multiple submitters when a shared tag map
4 * is used.
Jens Axboe75bb4622014-05-28 10:15:41 -06005 *
6 * Copyright (C) 2013-2014 Jens Axboe
7 */
Jens Axboe320ae512013-10-24 09:20:05 +01008#include <linux/kernel.h>
9#include <linux/module.h>
Jens Axboe4bb659b2014-05-09 09:36:49 -060010#include <linux/random.h>
Jens Axboe320ae512013-10-24 09:20:05 +010011
12#include <linux/blk-mq.h>
13#include "blk.h"
14#include "blk-mq.h"
15#include "blk-mq-tag.h"
16
Jens Axboe320ae512013-10-24 09:20:05 +010017bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
18{
Jens Axboe4bb659b2014-05-09 09:36:49 -060019 if (!tags)
20 return true;
21
Omar Sandoval88459642016-09-17 08:38:44 -060022 return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
Jens Axboe0d2602c2014-05-13 15:10:52 -060023}
24
25/*
26 * If a previously inactive queue goes active, bump the active user count.
27 */
28bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
29{
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);
33
34 return true;
35}
36
37/*
Jens Axboeaed3ea92014-12-22 14:04:42 -070038 * Wakeup all potentially sleeping on tags
Jens Axboe0d2602c2014-05-13 15:10:52 -060039 */
Jens Axboeaed3ea92014-12-22 14:04:42 -070040void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
Jens Axboe0d2602c2014-05-13 15:10:52 -060041{
Omar Sandoval88459642016-09-17 08:38:44 -060042 sbitmap_queue_wake_all(&tags->bitmap_tags);
43 if (include_reserve)
44 sbitmap_queue_wake_all(&tags->breserved_tags);
Jens Axboe0d2602c2014-05-13 15:10:52 -060045}
46
47/*
Jens Axboee3a2b3f2014-05-20 11:49:02 -060048 * If a previously busy queue goes inactive, potential waiters could now
49 * be allowed to queue. Wake them up and check.
50 */
51void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
52{
53 struct blk_mq_tags *tags = hctx->tags;
54
55 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
56 return;
57
58 atomic_dec(&tags->active_queues);
59
Jens Axboeaed3ea92014-12-22 14:04:42 -070060 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -060061}
62
63/*
Jens Axboe0d2602c2014-05-13 15:10:52 -060064 * 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.
66 */
67static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
Omar Sandoval88459642016-09-17 08:38:44 -060068 struct sbitmap_queue *bt)
Jens Axboe0d2602c2014-05-13 15:10:52 -060069{
70 unsigned int depth, users;
71
72 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
73 return true;
74 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
75 return true;
76
77 /*
78 * Don't try dividing an ant
79 */
Omar Sandoval88459642016-09-17 08:38:44 -060080 if (bt->sb.depth == 1)
Jens Axboe0d2602c2014-05-13 15:10:52 -060081 return true;
82
83 users = atomic_read(&hctx->tags->active_queues);
84 if (!users)
85 return true;
86
87 /*
88 * Allow at least some tags
89 */
Omar Sandoval88459642016-09-17 08:38:44 -060090 depth = max((bt->sb.depth + users - 1) / users, 4U);
Jens Axboe0d2602c2014-05-13 15:10:52 -060091 return atomic_read(&hctx->nr_active) < depth;
92}
93
Shaohua Li24391c02015-01-23 14:18:00 -070094#define BT_ALLOC_RR(tags) (tags->alloc_policy == BLK_TAG_ALLOC_RR)
95
Omar Sandoval88459642016-09-17 08:38:44 -060096static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
Omar Sandoval40aabb62016-09-17 01:28:23 -070097 struct blk_mq_tags *tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -060098{
Jens Axboe0d2602c2014-05-13 15:10:52 -060099 if (!hctx_may_queue(hctx, bt))
100 return -1;
Omar Sandoval40aabb62016-09-17 01:28:23 -0700101 return __sbitmap_queue_get(bt, BT_ALLOC_RR(tags));
Jens Axboe4bb659b2014-05-09 09:36:49 -0600102}
103
Omar Sandoval40aabb62016-09-17 01:28:23 -0700104static 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)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600106{
Omar Sandoval88459642016-09-17 08:38:44 -0600107 struct sbq_wait_state *ws;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600108 DEFINE_WAIT(wait);
109 int tag;
110
Omar Sandoval40aabb62016-09-17 01:28:23 -0700111 tag = __bt_get(hctx, bt, tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600112 if (tag != -1)
113 return tag;
114
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100115 if (data->flags & BLK_MQ_REQ_NOWAIT)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600116 return -1;
117
Omar Sandoval88459642016-09-17 08:38:44 -0600118 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600119 do {
Omar Sandoval88459642016-09-17 08:38:44 -0600120 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600121
Omar Sandoval40aabb62016-09-17 01:28:23 -0700122 tag = __bt_get(hctx, bt, tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600123 if (tag != -1)
124 break;
125
Bart Van Asscheb3223202014-12-08 08:46:34 -0700126 /*
127 * We're out of tags on this hardware queue, kick any
128 * pending IO submits before going to sleep waiting for
Sam Bradshawbc188d82015-03-18 17:06:18 -0600129 * some to complete. Note that hctx can be NULL here for
130 * reserved tag allocation.
Bart Van Asscheb3223202014-12-08 08:46:34 -0700131 */
Sam Bradshawbc188d82015-03-18 17:06:18 -0600132 if (hctx)
133 blk_mq_run_hw_queue(hctx, false);
Bart Van Asscheb3223202014-12-08 08:46:34 -0700134
Jens Axboe080ff352014-12-08 08:49:06 -0700135 /*
136 * Retry tag allocation after running the hardware queue,
137 * as running the queue may also have found completions.
138 */
Omar Sandoval40aabb62016-09-17 01:28:23 -0700139 tag = __bt_get(hctx, bt, tags);
Jens Axboe080ff352014-12-08 08:49:06 -0700140 if (tag != -1)
141 break;
142
Ming Leicb96a422014-06-01 00:43:37 +0800143 blk_mq_put_ctx(data->ctx);
144
Jens Axboe4bb659b2014-05-09 09:36:49 -0600145 io_schedule();
Ming Leicb96a422014-06-01 00:43:37 +0800146
147 data->ctx = blk_mq_get_ctx(data->q);
148 data->hctx = data->q->mq_ops->map_queue(data->q,
149 data->ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100150 if (data->flags & BLK_MQ_REQ_RESERVED) {
Ming Leicb96a422014-06-01 00:43:37 +0800151 bt = &data->hctx->tags->breserved_tags;
152 } else {
Ming Leicb96a422014-06-01 00:43:37 +0800153 hctx = data->hctx;
154 bt = &hctx->tags->bitmap_tags;
155 }
Omar Sandoval88459642016-09-17 08:38:44 -0600156 finish_wait(&ws->wait, &wait);
157 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600158 } while (1);
159
Omar Sandoval88459642016-09-17 08:38:44 -0600160 finish_wait(&ws->wait, &wait);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600161 return tag;
162}
163
Ming Leicb96a422014-06-01 00:43:37 +0800164static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100165{
166 int tag;
167
Ming Leicb96a422014-06-01 00:43:37 +0800168 tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
Omar Sandoval40aabb62016-09-17 01:28:23 -0700169 data->hctx->tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600170 if (tag >= 0)
Ming Leicb96a422014-06-01 00:43:37 +0800171 return tag + data->hctx->tags->nr_reserved_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600172
173 return BLK_MQ_TAG_FAIL;
Jens Axboe320ae512013-10-24 09:20:05 +0100174}
175
Ming Leicb96a422014-06-01 00:43:37 +0800176static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100177{
Omar Sandoval40aabb62016-09-17 01:28:23 -0700178 int tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100179
Ming Leicb96a422014-06-01 00:43:37 +0800180 if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100181 WARN_ON_ONCE(1);
182 return BLK_MQ_TAG_FAIL;
183 }
184
Omar Sandoval40aabb62016-09-17 01:28:23 -0700185 tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL,
186 data->hctx->tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100187 if (tag < 0)
188 return BLK_MQ_TAG_FAIL;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600189
Jens Axboe320ae512013-10-24 09:20:05 +0100190 return tag;
191}
192
Ming Leicb96a422014-06-01 00:43:37 +0800193unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100194{
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100195 if (data->flags & BLK_MQ_REQ_RESERVED)
196 return __blk_mq_get_reserved_tag(data);
197 return __blk_mq_get_tag(data);
Jens Axboe320ae512013-10-24 09:20:05 +0100198}
199
Omar Sandoval40aabb62016-09-17 01:28:23 -0700200void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
201 unsigned int tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100202{
Jens Axboe0d2602c2014-05-13 15:10:52 -0600203 struct blk_mq_tags *tags = hctx->tags;
204
Jens Axboe4bb659b2014-05-09 09:36:49 -0600205 if (tag >= tags->nr_reserved_tags) {
206 const int real_tag = tag - tags->nr_reserved_tags;
207
Jens Axboe70114c32014-11-24 15:52:30 -0700208 BUG_ON(real_tag >= tags->nr_tags);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700209 sbitmap_queue_clear(&tags->bitmap_tags, real_tag,
210 BT_ALLOC_RR(tags), ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700211 } else {
212 BUG_ON(tag >= tags->nr_reserved_tags);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700213 sbitmap_queue_clear(&tags->breserved_tags, tag,
214 BT_ALLOC_RR(tags), ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700215 }
Jens Axboe320ae512013-10-24 09:20:05 +0100216}
217
Omar Sandoval88459642016-09-17 08:38:44 -0600218struct bt_iter_data {
219 struct blk_mq_hw_ctx *hctx;
220 busy_iter_fn *fn;
221 void *data;
222 bool reserved;
223};
224
225static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100226{
Omar Sandoval88459642016-09-17 08:38:44 -0600227 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;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700231 struct request *rq;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600232
Omar Sandoval88459642016-09-17 08:38:44 -0600233 if (!reserved)
234 bitnr += tags->nr_reserved_tags;
235 rq = tags->rqs[bitnr];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600236
Omar Sandoval88459642016-09-17 08:38:44 -0600237 if (rq->q == hctx->queue)
238 iter_data->fn(hctx, rq, iter_data->data, reserved);
239 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100240}
241
Omar Sandoval88459642016-09-17 08:38:44 -0600242static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
243 busy_iter_fn *fn, void *data, bool reserved)
Keith Buschf26cdc82015-06-01 09:29:53 -0600244{
Omar Sandoval88459642016-09-17 08:38:44 -0600245 struct bt_iter_data iter_data = {
246 .hctx = hctx,
247 .fn = fn,
248 .data = data,
249 .reserved = reserved,
250 };
251
252 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
253}
254
255struct bt_tags_iter_data {
256 struct blk_mq_tags *tags;
257 busy_tag_iter_fn *fn;
258 void *data;
259 bool reserved;
260};
261
262static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
263{
264 struct bt_tags_iter_data *iter_data = data;
265 struct blk_mq_tags *tags = iter_data->tags;
266 bool reserved = iter_data->reserved;
Keith Buschf26cdc82015-06-01 09:29:53 -0600267 struct request *rq;
Keith Buschf26cdc82015-06-01 09:29:53 -0600268
Omar Sandoval88459642016-09-17 08:38:44 -0600269 if (!reserved)
270 bitnr += tags->nr_reserved_tags;
271 rq = tags->rqs[bitnr];
Keith Buschf26cdc82015-06-01 09:29:53 -0600272
Omar Sandoval88459642016-09-17 08:38:44 -0600273 iter_data->fn(rq, iter_data->data, reserved);
274 return true;
275}
Keith Buschf26cdc82015-06-01 09:29:53 -0600276
Omar Sandoval88459642016-09-17 08:38:44 -0600277static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
278 busy_tag_iter_fn *fn, void *data, bool reserved)
279{
280 struct bt_tags_iter_data iter_data = {
281 .tags = tags,
282 .fn = fn,
283 .data = data,
284 .reserved = reserved,
285 };
286
287 if (tags->rqs)
288 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
Keith Buschf26cdc82015-06-01 09:29:53 -0600289}
290
Sagi Grimberge8f1e162016-03-10 13:58:49 +0200291static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
292 busy_tag_iter_fn *fn, void *priv)
Keith Buschf26cdc82015-06-01 09:29:53 -0600293{
294 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600295 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
296 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
Keith Buschf26cdc82015-06-01 09:29:53 -0600297}
Keith Buschf26cdc82015-06-01 09:29:53 -0600298
Sagi Grimberge0489482016-03-10 13:58:46 +0200299void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
300 busy_tag_iter_fn *fn, void *priv)
301{
302 int i;
303
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);
307 }
308}
309EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
310
Sagi Grimberg486cf982016-07-06 21:55:48 +0900311int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
312{
313 int i, j, ret = 0;
314
315 if (!set->ops->reinit_request)
316 goto out;
317
318 for (i = 0; i < set->nr_hw_queues; i++) {
319 struct blk_mq_tags *tags = set->tags[i];
320
321 for (j = 0; j < tags->nr_tags; j++) {
322 if (!tags->rqs[j])
323 continue;
324
325 ret = set->ops->reinit_request(set->driver_data,
326 tags->rqs[j]);
327 if (ret)
328 goto out;
329 }
330 }
331
332out:
333 return ret;
334}
335EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
336
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200337void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700338 void *priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100339{
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200340 struct blk_mq_hw_ctx *hctx;
341 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100342
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200343
344 queue_for_each_hw_ctx(q, hctx, i) {
345 struct blk_mq_tags *tags = hctx->tags;
346
347 /*
348 * If not software queues are currently mapped to this
349 * hardware queue, there's nothing to check
350 */
351 if (!blk_mq_hw_queue_mapped(hctx))
352 continue;
353
354 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600355 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
356 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200357 }
358
Jens Axboe320ae512013-10-24 09:20:05 +0100359}
360
Omar Sandoval88459642016-09-17 08:38:44 -0600361static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600362{
Omar Sandoval88459642016-09-17 08:38:44 -0600363 return bt->sb.depth - sbitmap_weight(&bt->sb);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600364}
365
Omar Sandoval88459642016-09-17 08:38:44 -0600366static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, int node)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600367{
Omar Sandoval88459642016-09-17 08:38:44 -0600368 return sbitmap_queue_init_node(bt, depth, -1, GFP_KERNEL, node);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600369}
370
371static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700372 int node, int alloc_policy)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600373{
374 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
375
Shaohua Li24391c02015-01-23 14:18:00 -0700376 tags->alloc_policy = alloc_policy;
377
Omar Sandoval88459642016-09-17 08:38:44 -0600378 if (bt_alloc(&tags->bitmap_tags, depth, node))
379 goto free_tags;
380 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node))
381 goto free_bitmap_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600382
383 return tags;
Omar Sandoval88459642016-09-17 08:38:44 -0600384free_bitmap_tags:
385 sbitmap_queue_free(&tags->bitmap_tags);
386free_tags:
Jens Axboe4bb659b2014-05-09 09:36:49 -0600387 kfree(tags);
388 return NULL;
389}
390
Jens Axboe320ae512013-10-24 09:20:05 +0100391struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700392 unsigned int reserved_tags,
393 int node, int alloc_policy)
Jens Axboe320ae512013-10-24 09:20:05 +0100394{
Jens Axboe320ae512013-10-24 09:20:05 +0100395 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100396
397 if (total_tags > BLK_MQ_TAG_MAX) {
398 pr_err("blk-mq: tag depth too large\n");
399 return NULL;
400 }
401
402 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
403 if (!tags)
404 return NULL;
405
Keith Buschf26cdc82015-06-01 09:29:53 -0600406 if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
407 kfree(tags);
408 return NULL;
409 }
410
Jens Axboe320ae512013-10-24 09:20:05 +0100411 tags->nr_tags = total_tags;
412 tags->nr_reserved_tags = reserved_tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100413
Shaohua Li24391c02015-01-23 14:18:00 -0700414 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
Jens Axboe320ae512013-10-24 09:20:05 +0100415}
416
417void blk_mq_free_tags(struct blk_mq_tags *tags)
418{
Omar Sandoval88459642016-09-17 08:38:44 -0600419 sbitmap_queue_free(&tags->bitmap_tags);
420 sbitmap_queue_free(&tags->breserved_tags);
Junichi Nomuraf42d79a2015-10-14 05:02:15 +0000421 free_cpumask_var(tags->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +0100422 kfree(tags);
423}
424
Jens Axboe4bb659b2014-05-09 09:36:49 -0600425void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
426{
427 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
428
Ming Lei9d3d21a2014-05-10 15:43:14 -0600429 *tag = prandom_u32() % depth;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600430}
431
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600432int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
433{
434 tdepth -= tags->nr_reserved_tags;
435 if (tdepth > tags->nr_tags)
436 return -EINVAL;
437
438 /*
439 * Don't need (or can't) update reserved tags here, they remain
440 * static and should never need resizing.
441 */
Omar Sandoval88459642016-09-17 08:38:44 -0600442 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
443
Jens Axboeaed3ea92014-12-22 14:04:42 -0700444 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600445 return 0;
446}
447
Bart Van Assche205fb5f2014-10-30 14:45:11 +0100448/**
449 * blk_mq_unique_tag() - return a tag that is unique queue-wide
450 * @rq: request for which to compute a unique tag
451 *
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
455 * the lower bits.
456 *
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.
459 */
460u32 blk_mq_unique_tag(struct request *rq)
461{
462 struct request_queue *q = rq->q;
463 struct blk_mq_hw_ctx *hctx;
464 int hwq = 0;
465
466 if (q->mq_ops) {
467 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
468 hwq = hctx->queue_num;
469 }
470
471 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
472 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
473}
474EXPORT_SYMBOL(blk_mq_unique_tag);
475
Jens Axboe320ae512013-10-24 09:20:05 +0100476ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
477{
478 char *orig_page = page;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600479 unsigned int free, res;
Jens Axboe320ae512013-10-24 09:20:05 +0100480
481 if (!tags)
482 return 0;
483
Jens Axboe59d13bf2014-05-09 13:41:15 -0600484 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
485 "bits_per_word=%u\n",
486 tags->nr_tags, tags->nr_reserved_tags,
Omar Sandoval88459642016-09-17 08:38:44 -0600487 1U << tags->bitmap_tags.sb.shift);
Jens Axboe320ae512013-10-24 09:20:05 +0100488
Jens Axboe4bb659b2014-05-09 09:36:49 -0600489 free = bt_unused_tags(&tags->bitmap_tags);
490 res = bt_unused_tags(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100491
Jens Axboe4bb659b2014-05-09 09:36:49 -0600492 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600493 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
Jens Axboe320ae512013-10-24 09:20:05 +0100494
495 return page - orig_page;
496}