blob: e1c2bedb0bf9cfdbf481bb1c20adeb291301a059 [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
Omar Sandovalf4a644d2016-09-17 01:28:24 -070094static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -060095{
Jens Axboe0d2602c2014-05-13 15:10:52 -060096 if (!hctx_may_queue(hctx, bt))
97 return -1;
Omar Sandovalf4a644d2016-09-17 01:28:24 -070098 return __sbitmap_queue_get(bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -060099}
100
Omar Sandoval40aabb62016-09-17 01:28:23 -0700101static int bt_get(struct blk_mq_alloc_data *data, struct sbitmap_queue *bt,
102 struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600103{
Omar Sandoval88459642016-09-17 08:38:44 -0600104 struct sbq_wait_state *ws;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600105 DEFINE_WAIT(wait);
106 int tag;
107
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700108 tag = __bt_get(hctx, bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600109 if (tag != -1)
110 return tag;
111
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100112 if (data->flags & BLK_MQ_REQ_NOWAIT)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600113 return -1;
114
Omar Sandoval88459642016-09-17 08:38:44 -0600115 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600116 do {
Omar Sandoval88459642016-09-17 08:38:44 -0600117 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600118
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700119 tag = __bt_get(hctx, bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600120 if (tag != -1)
121 break;
122
Bart Van Asscheb3223202014-12-08 08:46:34 -0700123 /*
124 * We're out of tags on this hardware queue, kick any
125 * pending IO submits before going to sleep waiting for
Sam Bradshawbc188d82015-03-18 17:06:18 -0600126 * some to complete. Note that hctx can be NULL here for
127 * reserved tag allocation.
Bart Van Asscheb3223202014-12-08 08:46:34 -0700128 */
Sam Bradshawbc188d82015-03-18 17:06:18 -0600129 if (hctx)
130 blk_mq_run_hw_queue(hctx, false);
Bart Van Asscheb3223202014-12-08 08:46:34 -0700131
Jens Axboe080ff352014-12-08 08:49:06 -0700132 /*
133 * Retry tag allocation after running the hardware queue,
134 * as running the queue may also have found completions.
135 */
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700136 tag = __bt_get(hctx, bt);
Jens Axboe080ff352014-12-08 08:49:06 -0700137 if (tag != -1)
138 break;
139
Ming Leicb96a422014-06-01 00:43:37 +0800140 blk_mq_put_ctx(data->ctx);
141
Jens Axboe4bb659b2014-05-09 09:36:49 -0600142 io_schedule();
Ming Leicb96a422014-06-01 00:43:37 +0800143
144 data->ctx = blk_mq_get_ctx(data->q);
145 data->hctx = data->q->mq_ops->map_queue(data->q,
146 data->ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100147 if (data->flags & BLK_MQ_REQ_RESERVED) {
Ming Leicb96a422014-06-01 00:43:37 +0800148 bt = &data->hctx->tags->breserved_tags;
149 } else {
Ming Leicb96a422014-06-01 00:43:37 +0800150 hctx = data->hctx;
151 bt = &hctx->tags->bitmap_tags;
152 }
Omar Sandoval88459642016-09-17 08:38:44 -0600153 finish_wait(&ws->wait, &wait);
154 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600155 } while (1);
156
Omar Sandoval88459642016-09-17 08:38:44 -0600157 finish_wait(&ws->wait, &wait);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600158 return tag;
159}
160
Ming Leicb96a422014-06-01 00:43:37 +0800161static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100162{
163 int tag;
164
Ming Leicb96a422014-06-01 00:43:37 +0800165 tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
Omar Sandoval40aabb62016-09-17 01:28:23 -0700166 data->hctx->tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600167 if (tag >= 0)
Ming Leicb96a422014-06-01 00:43:37 +0800168 return tag + data->hctx->tags->nr_reserved_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600169
170 return BLK_MQ_TAG_FAIL;
Jens Axboe320ae512013-10-24 09:20:05 +0100171}
172
Ming Leicb96a422014-06-01 00:43:37 +0800173static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100174{
Omar Sandoval40aabb62016-09-17 01:28:23 -0700175 int tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100176
Ming Leicb96a422014-06-01 00:43:37 +0800177 if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100178 WARN_ON_ONCE(1);
179 return BLK_MQ_TAG_FAIL;
180 }
181
Omar Sandoval40aabb62016-09-17 01:28:23 -0700182 tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL,
183 data->hctx->tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100184 if (tag < 0)
185 return BLK_MQ_TAG_FAIL;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600186
Jens Axboe320ae512013-10-24 09:20:05 +0100187 return tag;
188}
189
Ming Leicb96a422014-06-01 00:43:37 +0800190unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100191{
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100192 if (data->flags & BLK_MQ_REQ_RESERVED)
193 return __blk_mq_get_reserved_tag(data);
194 return __blk_mq_get_tag(data);
Jens Axboe320ae512013-10-24 09:20:05 +0100195}
196
Omar Sandoval40aabb62016-09-17 01:28:23 -0700197void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
198 unsigned int tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100199{
Jens Axboe0d2602c2014-05-13 15:10:52 -0600200 struct blk_mq_tags *tags = hctx->tags;
201
Jens Axboe4bb659b2014-05-09 09:36:49 -0600202 if (tag >= tags->nr_reserved_tags) {
203 const int real_tag = tag - tags->nr_reserved_tags;
204
Jens Axboe70114c32014-11-24 15:52:30 -0700205 BUG_ON(real_tag >= tags->nr_tags);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700206 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700207 } else {
208 BUG_ON(tag >= tags->nr_reserved_tags);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700209 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700210 }
Jens Axboe320ae512013-10-24 09:20:05 +0100211}
212
Omar Sandoval88459642016-09-17 08:38:44 -0600213struct bt_iter_data {
214 struct blk_mq_hw_ctx *hctx;
215 busy_iter_fn *fn;
216 void *data;
217 bool reserved;
218};
219
220static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100221{
Omar Sandoval88459642016-09-17 08:38:44 -0600222 struct bt_iter_data *iter_data = data;
223 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
224 struct blk_mq_tags *tags = hctx->tags;
225 bool reserved = iter_data->reserved;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700226 struct request *rq;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600227
Omar Sandoval88459642016-09-17 08:38:44 -0600228 if (!reserved)
229 bitnr += tags->nr_reserved_tags;
230 rq = tags->rqs[bitnr];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600231
Omar Sandoval88459642016-09-17 08:38:44 -0600232 if (rq->q == hctx->queue)
233 iter_data->fn(hctx, rq, iter_data->data, reserved);
234 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100235}
236
Omar Sandoval88459642016-09-17 08:38:44 -0600237static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
238 busy_iter_fn *fn, void *data, bool reserved)
Keith Buschf26cdc82015-06-01 09:29:53 -0600239{
Omar Sandoval88459642016-09-17 08:38:44 -0600240 struct bt_iter_data iter_data = {
241 .hctx = hctx,
242 .fn = fn,
243 .data = data,
244 .reserved = reserved,
245 };
246
247 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
248}
249
250struct bt_tags_iter_data {
251 struct blk_mq_tags *tags;
252 busy_tag_iter_fn *fn;
253 void *data;
254 bool reserved;
255};
256
257static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
258{
259 struct bt_tags_iter_data *iter_data = data;
260 struct blk_mq_tags *tags = iter_data->tags;
261 bool reserved = iter_data->reserved;
Keith Buschf26cdc82015-06-01 09:29:53 -0600262 struct request *rq;
Keith Buschf26cdc82015-06-01 09:29:53 -0600263
Omar Sandoval88459642016-09-17 08:38:44 -0600264 if (!reserved)
265 bitnr += tags->nr_reserved_tags;
266 rq = tags->rqs[bitnr];
Keith Buschf26cdc82015-06-01 09:29:53 -0600267
Omar Sandoval88459642016-09-17 08:38:44 -0600268 iter_data->fn(rq, iter_data->data, reserved);
269 return true;
270}
Keith Buschf26cdc82015-06-01 09:29:53 -0600271
Omar Sandoval88459642016-09-17 08:38:44 -0600272static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
273 busy_tag_iter_fn *fn, void *data, bool reserved)
274{
275 struct bt_tags_iter_data iter_data = {
276 .tags = tags,
277 .fn = fn,
278 .data = data,
279 .reserved = reserved,
280 };
281
282 if (tags->rqs)
283 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
Keith Buschf26cdc82015-06-01 09:29:53 -0600284}
285
Sagi Grimberge8f1e162016-03-10 13:58:49 +0200286static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
287 busy_tag_iter_fn *fn, void *priv)
Keith Buschf26cdc82015-06-01 09:29:53 -0600288{
289 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600290 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
291 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
Keith Buschf26cdc82015-06-01 09:29:53 -0600292}
Keith Buschf26cdc82015-06-01 09:29:53 -0600293
Sagi Grimberge0489482016-03-10 13:58:46 +0200294void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
295 busy_tag_iter_fn *fn, void *priv)
296{
297 int i;
298
299 for (i = 0; i < tagset->nr_hw_queues; i++) {
300 if (tagset->tags && tagset->tags[i])
301 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
302 }
303}
304EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
305
Sagi Grimberg486cf982016-07-06 21:55:48 +0900306int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
307{
308 int i, j, ret = 0;
309
310 if (!set->ops->reinit_request)
311 goto out;
312
313 for (i = 0; i < set->nr_hw_queues; i++) {
314 struct blk_mq_tags *tags = set->tags[i];
315
316 for (j = 0; j < tags->nr_tags; j++) {
317 if (!tags->rqs[j])
318 continue;
319
320 ret = set->ops->reinit_request(set->driver_data,
321 tags->rqs[j]);
322 if (ret)
323 goto out;
324 }
325 }
326
327out:
328 return ret;
329}
330EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
331
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200332void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700333 void *priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100334{
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200335 struct blk_mq_hw_ctx *hctx;
336 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100337
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200338
339 queue_for_each_hw_ctx(q, hctx, i) {
340 struct blk_mq_tags *tags = hctx->tags;
341
342 /*
343 * If not software queues are currently mapped to this
344 * hardware queue, there's nothing to check
345 */
346 if (!blk_mq_hw_queue_mapped(hctx))
347 continue;
348
349 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600350 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
351 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200352 }
353
Jens Axboe320ae512013-10-24 09:20:05 +0100354}
355
Omar Sandoval88459642016-09-17 08:38:44 -0600356static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600357{
Omar Sandoval88459642016-09-17 08:38:44 -0600358 return bt->sb.depth - sbitmap_weight(&bt->sb);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600359}
360
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700361static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
362 bool round_robin, int node)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600363{
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700364 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
365 node);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600366}
367
368static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700369 int node, int alloc_policy)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600370{
371 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700372 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600373
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700374 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
Omar Sandoval88459642016-09-17 08:38:44 -0600375 goto free_tags;
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700376 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin,
377 node))
Omar Sandoval88459642016-09-17 08:38:44 -0600378 goto free_bitmap_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600379
380 return tags;
Omar Sandoval88459642016-09-17 08:38:44 -0600381free_bitmap_tags:
382 sbitmap_queue_free(&tags->bitmap_tags);
383free_tags:
Jens Axboe4bb659b2014-05-09 09:36:49 -0600384 kfree(tags);
385 return NULL;
386}
387
Jens Axboe320ae512013-10-24 09:20:05 +0100388struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700389 unsigned int reserved_tags,
390 int node, int alloc_policy)
Jens Axboe320ae512013-10-24 09:20:05 +0100391{
Jens Axboe320ae512013-10-24 09:20:05 +0100392 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100393
394 if (total_tags > BLK_MQ_TAG_MAX) {
395 pr_err("blk-mq: tag depth too large\n");
396 return NULL;
397 }
398
399 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
400 if (!tags)
401 return NULL;
402
Keith Buschf26cdc82015-06-01 09:29:53 -0600403 if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
404 kfree(tags);
405 return NULL;
406 }
407
Jens Axboe320ae512013-10-24 09:20:05 +0100408 tags->nr_tags = total_tags;
409 tags->nr_reserved_tags = reserved_tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100410
Shaohua Li24391c02015-01-23 14:18:00 -0700411 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
Jens Axboe320ae512013-10-24 09:20:05 +0100412}
413
414void blk_mq_free_tags(struct blk_mq_tags *tags)
415{
Omar Sandoval88459642016-09-17 08:38:44 -0600416 sbitmap_queue_free(&tags->bitmap_tags);
417 sbitmap_queue_free(&tags->breserved_tags);
Junichi Nomuraf42d79a2015-10-14 05:02:15 +0000418 free_cpumask_var(tags->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +0100419 kfree(tags);
420}
421
Jens Axboe4bb659b2014-05-09 09:36:49 -0600422void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
423{
424 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
425
Ming Lei9d3d21a2014-05-10 15:43:14 -0600426 *tag = prandom_u32() % depth;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600427}
428
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600429int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
430{
431 tdepth -= tags->nr_reserved_tags;
432 if (tdepth > tags->nr_tags)
433 return -EINVAL;
434
435 /*
436 * Don't need (or can't) update reserved tags here, they remain
437 * static and should never need resizing.
438 */
Omar Sandoval88459642016-09-17 08:38:44 -0600439 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
440
Jens Axboeaed3ea92014-12-22 14:04:42 -0700441 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600442 return 0;
443}
444
Bart Van Assche205fb5f2014-10-30 14:45:11 +0100445/**
446 * blk_mq_unique_tag() - return a tag that is unique queue-wide
447 * @rq: request for which to compute a unique tag
448 *
449 * The tag field in struct request is unique per hardware queue but not over
450 * all hardware queues. Hence this function that returns a tag with the
451 * hardware context index in the upper bits and the per hardware queue tag in
452 * the lower bits.
453 *
454 * Note: When called for a request that is queued on a non-multiqueue request
455 * queue, the hardware context index is set to zero.
456 */
457u32 blk_mq_unique_tag(struct request *rq)
458{
459 struct request_queue *q = rq->q;
460 struct blk_mq_hw_ctx *hctx;
461 int hwq = 0;
462
463 if (q->mq_ops) {
464 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
465 hwq = hctx->queue_num;
466 }
467
468 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
469 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
470}
471EXPORT_SYMBOL(blk_mq_unique_tag);
472
Jens Axboe320ae512013-10-24 09:20:05 +0100473ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
474{
475 char *orig_page = page;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600476 unsigned int free, res;
Jens Axboe320ae512013-10-24 09:20:05 +0100477
478 if (!tags)
479 return 0;
480
Jens Axboe59d13bf2014-05-09 13:41:15 -0600481 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
482 "bits_per_word=%u\n",
483 tags->nr_tags, tags->nr_reserved_tags,
Omar Sandoval88459642016-09-17 08:38:44 -0600484 1U << tags->bitmap_tags.sb.shift);
Jens Axboe320ae512013-10-24 09:20:05 +0100485
Jens Axboe4bb659b2014-05-09 09:36:49 -0600486 free = bt_unused_tags(&tags->bitmap_tags);
487 res = bt_unused_tags(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100488
Jens Axboe4bb659b2014-05-09 09:36:49 -0600489 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600490 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
Jens Axboe320ae512013-10-24 09:20:05 +0100491
492 return page - orig_page;
493}