blob: 89bb6250633de2c572dac8cb3ad8e3777afa5bd4 [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 Axboe320ae512013-10-24 09:20:05 +010010
11#include <linux/blk-mq.h>
12#include "blk.h"
13#include "blk-mq.h"
14#include "blk-mq-tag.h"
15
Jens Axboe320ae512013-10-24 09:20:05 +010016bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
17{
Jens Axboe4bb659b2014-05-09 09:36:49 -060018 if (!tags)
19 return true;
20
Omar Sandoval88459642016-09-17 08:38:44 -060021 return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
Jens Axboe0d2602c2014-05-13 15:10:52 -060022}
23
24/*
25 * If a previously inactive queue goes active, bump the active user count.
26 */
27bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
28{
29 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
30 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
31 atomic_inc(&hctx->tags->active_queues);
32
33 return true;
34}
35
36/*
Jens Axboeaed3ea92014-12-22 14:04:42 -070037 * Wakeup all potentially sleeping on tags
Jens Axboe0d2602c2014-05-13 15:10:52 -060038 */
Jens Axboeaed3ea92014-12-22 14:04:42 -070039void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
Jens Axboe0d2602c2014-05-13 15:10:52 -060040{
Omar Sandoval88459642016-09-17 08:38:44 -060041 sbitmap_queue_wake_all(&tags->bitmap_tags);
42 if (include_reserve)
43 sbitmap_queue_wake_all(&tags->breserved_tags);
Jens Axboe0d2602c2014-05-13 15:10:52 -060044}
45
46/*
Jens Axboee3a2b3f2014-05-20 11:49:02 -060047 * If a previously busy queue goes inactive, potential waiters could now
48 * be allowed to queue. Wake them up and check.
49 */
50void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
51{
52 struct blk_mq_tags *tags = hctx->tags;
53
54 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
55 return;
56
57 atomic_dec(&tags->active_queues);
58
Jens Axboeaed3ea92014-12-22 14:04:42 -070059 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -060060}
61
62/*
Jens Axboe0d2602c2014-05-13 15:10:52 -060063 * For shared tag users, we track the number of currently active users
64 * and attempt to provide a fair share of the tag depth for each of them.
65 */
66static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
Omar Sandoval88459642016-09-17 08:38:44 -060067 struct sbitmap_queue *bt)
Jens Axboe0d2602c2014-05-13 15:10:52 -060068{
69 unsigned int depth, users;
70
71 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
72 return true;
73 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
74 return true;
75
76 /*
77 * Don't try dividing an ant
78 */
Omar Sandoval88459642016-09-17 08:38:44 -060079 if (bt->sb.depth == 1)
Jens Axboe0d2602c2014-05-13 15:10:52 -060080 return true;
81
82 users = atomic_read(&hctx->tags->active_queues);
83 if (!users)
84 return true;
85
86 /*
87 * Allow at least some tags
88 */
Omar Sandoval88459642016-09-17 08:38:44 -060089 depth = max((bt->sb.depth + users - 1) / users, 4U);
Jens Axboe0d2602c2014-05-13 15:10:52 -060090 return atomic_read(&hctx->nr_active) < depth;
91}
92
Omar Sandovalf4a644d2016-09-17 01:28:24 -070093static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -060094{
Jens Axboe0d2602c2014-05-13 15:10:52 -060095 if (!hctx_may_queue(hctx, bt))
96 return -1;
Omar Sandovalf4a644d2016-09-17 01:28:24 -070097 return __sbitmap_queue_get(bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -060098}
99
Omar Sandoval40aabb62016-09-17 01:28:23 -0700100static int bt_get(struct blk_mq_alloc_data *data, struct sbitmap_queue *bt,
101 struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600102{
Omar Sandoval88459642016-09-17 08:38:44 -0600103 struct sbq_wait_state *ws;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600104 DEFINE_WAIT(wait);
105 int tag;
106
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700107 tag = __bt_get(hctx, bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600108 if (tag != -1)
109 return tag;
110
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100111 if (data->flags & BLK_MQ_REQ_NOWAIT)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600112 return -1;
113
Omar Sandoval88459642016-09-17 08:38:44 -0600114 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600115 do {
Omar Sandoval88459642016-09-17 08:38:44 -0600116 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600117
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700118 tag = __bt_get(hctx, bt);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600119 if (tag != -1)
120 break;
121
Bart Van Asscheb3223202014-12-08 08:46:34 -0700122 /*
123 * We're out of tags on this hardware queue, kick any
124 * pending IO submits before going to sleep waiting for
Sam Bradshawbc188d82015-03-18 17:06:18 -0600125 * some to complete. Note that hctx can be NULL here for
126 * reserved tag allocation.
Bart Van Asscheb3223202014-12-08 08:46:34 -0700127 */
Sam Bradshawbc188d82015-03-18 17:06:18 -0600128 if (hctx)
129 blk_mq_run_hw_queue(hctx, false);
Bart Van Asscheb3223202014-12-08 08:46:34 -0700130
Jens Axboe080ff352014-12-08 08:49:06 -0700131 /*
132 * Retry tag allocation after running the hardware queue,
133 * as running the queue may also have found completions.
134 */
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700135 tag = __bt_get(hctx, bt);
Jens Axboe080ff352014-12-08 08:49:06 -0700136 if (tag != -1)
137 break;
138
Ming Leicb96a422014-06-01 00:43:37 +0800139 blk_mq_put_ctx(data->ctx);
140
Jens Axboe4bb659b2014-05-09 09:36:49 -0600141 io_schedule();
Ming Leicb96a422014-06-01 00:43:37 +0800142
143 data->ctx = blk_mq_get_ctx(data->q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200144 data->hctx = blk_mq_map_queue(data->q, data->ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100145 if (data->flags & BLK_MQ_REQ_RESERVED) {
Ming Leicb96a422014-06-01 00:43:37 +0800146 bt = &data->hctx->tags->breserved_tags;
147 } else {
Ming Leicb96a422014-06-01 00:43:37 +0800148 hctx = data->hctx;
149 bt = &hctx->tags->bitmap_tags;
150 }
Omar Sandoval88459642016-09-17 08:38:44 -0600151 finish_wait(&ws->wait, &wait);
152 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600153 } while (1);
154
Omar Sandoval88459642016-09-17 08:38:44 -0600155 finish_wait(&ws->wait, &wait);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600156 return tag;
157}
158
Ming Leicb96a422014-06-01 00:43:37 +0800159static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100160{
161 int tag;
162
Ming Leicb96a422014-06-01 00:43:37 +0800163 tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
Omar Sandoval40aabb62016-09-17 01:28:23 -0700164 data->hctx->tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600165 if (tag >= 0)
Ming Leicb96a422014-06-01 00:43:37 +0800166 return tag + data->hctx->tags->nr_reserved_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600167
168 return BLK_MQ_TAG_FAIL;
Jens Axboe320ae512013-10-24 09:20:05 +0100169}
170
Ming Leicb96a422014-06-01 00:43:37 +0800171static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100172{
Omar Sandoval40aabb62016-09-17 01:28:23 -0700173 int tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100174
Ming Leicb96a422014-06-01 00:43:37 +0800175 if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100176 WARN_ON_ONCE(1);
177 return BLK_MQ_TAG_FAIL;
178 }
179
Omar Sandoval40aabb62016-09-17 01:28:23 -0700180 tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL,
181 data->hctx->tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100182 if (tag < 0)
183 return BLK_MQ_TAG_FAIL;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600184
Jens Axboe320ae512013-10-24 09:20:05 +0100185 return tag;
186}
187
Ming Leicb96a422014-06-01 00:43:37 +0800188unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100189{
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100190 if (data->flags & BLK_MQ_REQ_RESERVED)
191 return __blk_mq_get_reserved_tag(data);
192 return __blk_mq_get_tag(data);
Jens Axboe320ae512013-10-24 09:20:05 +0100193}
194
Omar Sandoval40aabb62016-09-17 01:28:23 -0700195void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
196 unsigned int tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100197{
Jens Axboe0d2602c2014-05-13 15:10:52 -0600198 struct blk_mq_tags *tags = hctx->tags;
199
Jens Axboe4bb659b2014-05-09 09:36:49 -0600200 if (tag >= tags->nr_reserved_tags) {
201 const int real_tag = tag - tags->nr_reserved_tags;
202
Jens Axboe70114c32014-11-24 15:52:30 -0700203 BUG_ON(real_tag >= tags->nr_tags);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700204 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700205 } else {
206 BUG_ON(tag >= tags->nr_reserved_tags);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700207 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
Jens Axboe70114c32014-11-24 15:52:30 -0700208 }
Jens Axboe320ae512013-10-24 09:20:05 +0100209}
210
Omar Sandoval88459642016-09-17 08:38:44 -0600211struct bt_iter_data {
212 struct blk_mq_hw_ctx *hctx;
213 busy_iter_fn *fn;
214 void *data;
215 bool reserved;
216};
217
218static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100219{
Omar Sandoval88459642016-09-17 08:38:44 -0600220 struct bt_iter_data *iter_data = data;
221 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
222 struct blk_mq_tags *tags = hctx->tags;
223 bool reserved = iter_data->reserved;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700224 struct request *rq;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600225
Omar Sandoval88459642016-09-17 08:38:44 -0600226 if (!reserved)
227 bitnr += tags->nr_reserved_tags;
228 rq = tags->rqs[bitnr];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600229
Omar Sandoval88459642016-09-17 08:38:44 -0600230 if (rq->q == hctx->queue)
231 iter_data->fn(hctx, rq, iter_data->data, reserved);
232 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100233}
234
Omar Sandoval88459642016-09-17 08:38:44 -0600235static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
236 busy_iter_fn *fn, void *data, bool reserved)
Keith Buschf26cdc82015-06-01 09:29:53 -0600237{
Omar Sandoval88459642016-09-17 08:38:44 -0600238 struct bt_iter_data iter_data = {
239 .hctx = hctx,
240 .fn = fn,
241 .data = data,
242 .reserved = reserved,
243 };
244
245 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
246}
247
248struct bt_tags_iter_data {
249 struct blk_mq_tags *tags;
250 busy_tag_iter_fn *fn;
251 void *data;
252 bool reserved;
253};
254
255static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
256{
257 struct bt_tags_iter_data *iter_data = data;
258 struct blk_mq_tags *tags = iter_data->tags;
259 bool reserved = iter_data->reserved;
Keith Buschf26cdc82015-06-01 09:29:53 -0600260 struct request *rq;
Keith Buschf26cdc82015-06-01 09:29:53 -0600261
Omar Sandoval88459642016-09-17 08:38:44 -0600262 if (!reserved)
263 bitnr += tags->nr_reserved_tags;
264 rq = tags->rqs[bitnr];
Keith Buschf26cdc82015-06-01 09:29:53 -0600265
Omar Sandoval88459642016-09-17 08:38:44 -0600266 iter_data->fn(rq, iter_data->data, reserved);
267 return true;
268}
Keith Buschf26cdc82015-06-01 09:29:53 -0600269
Omar Sandoval88459642016-09-17 08:38:44 -0600270static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
271 busy_tag_iter_fn *fn, void *data, bool reserved)
272{
273 struct bt_tags_iter_data iter_data = {
274 .tags = tags,
275 .fn = fn,
276 .data = data,
277 .reserved = reserved,
278 };
279
280 if (tags->rqs)
281 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
Keith Buschf26cdc82015-06-01 09:29:53 -0600282}
283
Sagi Grimberge8f1e162016-03-10 13:58:49 +0200284static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
285 busy_tag_iter_fn *fn, void *priv)
Keith Buschf26cdc82015-06-01 09:29:53 -0600286{
287 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600288 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
289 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
Keith Buschf26cdc82015-06-01 09:29:53 -0600290}
Keith Buschf26cdc82015-06-01 09:29:53 -0600291
Sagi Grimberge0489482016-03-10 13:58:46 +0200292void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
293 busy_tag_iter_fn *fn, void *priv)
294{
295 int i;
296
297 for (i = 0; i < tagset->nr_hw_queues; i++) {
298 if (tagset->tags && tagset->tags[i])
299 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
300 }
301}
302EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
303
Sagi Grimberg486cf982016-07-06 21:55:48 +0900304int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
305{
306 int i, j, ret = 0;
307
308 if (!set->ops->reinit_request)
309 goto out;
310
311 for (i = 0; i < set->nr_hw_queues; i++) {
312 struct blk_mq_tags *tags = set->tags[i];
313
Sagi Grimbergd28046f2017-03-13 16:10:11 +0200314 if (!tags)
315 continue;
316
Sagi Grimberg486cf982016-07-06 21:55:48 +0900317 for (j = 0; j < tags->nr_tags; j++) {
318 if (!tags->rqs[j])
319 continue;
320
321 ret = set->ops->reinit_request(set->driver_data,
322 tags->rqs[j]);
323 if (ret)
324 goto out;
325 }
326 }
327
328out:
329 return ret;
330}
331EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
332
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200333void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700334 void *priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100335{
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200336 struct blk_mq_hw_ctx *hctx;
337 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100338
Jianchao Wangf530afb2018-08-21 15:15:04 +0800339 /*
340 * __blk_mq_update_nr_hw_queues will update the nr_hw_queues and
Keith Buschd4d74442018-09-25 10:36:20 -0600341 * queue_hw_ctx after freeze the queue, so we use q_usage_counter
342 * to avoid race with it.
Jianchao Wangf530afb2018-08-21 15:15:04 +0800343 */
Keith Buschd4d74442018-09-25 10:36:20 -0600344 if (!percpu_ref_tryget(&q->q_usage_counter))
Jianchao Wangf530afb2018-08-21 15:15:04 +0800345 return;
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200346
347 queue_for_each_hw_ctx(q, hctx, i) {
348 struct blk_mq_tags *tags = hctx->tags;
349
350 /*
351 * If not software queues are currently mapped to this
352 * hardware queue, there's nothing to check
353 */
354 if (!blk_mq_hw_queue_mapped(hctx))
355 continue;
356
357 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600358 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
359 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200360 }
Keith Buschd4d74442018-09-25 10:36:20 -0600361 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100362}
363
Omar Sandoval88459642016-09-17 08:38:44 -0600364static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600365{
Omar Sandoval88459642016-09-17 08:38:44 -0600366 return bt->sb.depth - sbitmap_weight(&bt->sb);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600367}
368
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700369static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
370 bool round_robin, int node)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600371{
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700372 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
373 node);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600374}
375
376static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700377 int node, int alloc_policy)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600378{
379 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700380 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600381
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700382 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
Omar Sandoval88459642016-09-17 08:38:44 -0600383 goto free_tags;
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700384 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin,
385 node))
Omar Sandoval88459642016-09-17 08:38:44 -0600386 goto free_bitmap_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600387
388 return tags;
Omar Sandoval88459642016-09-17 08:38:44 -0600389free_bitmap_tags:
390 sbitmap_queue_free(&tags->bitmap_tags);
391free_tags:
Jens Axboe4bb659b2014-05-09 09:36:49 -0600392 kfree(tags);
393 return NULL;
394}
395
Jens Axboe320ae512013-10-24 09:20:05 +0100396struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700397 unsigned int reserved_tags,
398 int node, int alloc_policy)
Jens Axboe320ae512013-10-24 09:20:05 +0100399{
Jens Axboe320ae512013-10-24 09:20:05 +0100400 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100401
402 if (total_tags > BLK_MQ_TAG_MAX) {
403 pr_err("blk-mq: tag depth too large\n");
404 return NULL;
405 }
406
407 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
408 if (!tags)
409 return NULL;
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);
Jens Axboe320ae512013-10-24 09:20:05 +0100421 kfree(tags);
422}
423
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600424int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
425{
426 tdepth -= tags->nr_reserved_tags;
427 if (tdepth > tags->nr_tags)
428 return -EINVAL;
429
430 /*
431 * Don't need (or can't) update reserved tags here, they remain
432 * static and should never need resizing.
433 */
Omar Sandoval88459642016-09-17 08:38:44 -0600434 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
435
Jens Axboeaed3ea92014-12-22 14:04:42 -0700436 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600437 return 0;
438}
439
Bart Van Assche205fb5f2014-10-30 14:45:11 +0100440/**
441 * blk_mq_unique_tag() - return a tag that is unique queue-wide
442 * @rq: request for which to compute a unique tag
443 *
444 * The tag field in struct request is unique per hardware queue but not over
445 * all hardware queues. Hence this function that returns a tag with the
446 * hardware context index in the upper bits and the per hardware queue tag in
447 * the lower bits.
448 *
449 * Note: When called for a request that is queued on a non-multiqueue request
450 * queue, the hardware context index is set to zero.
451 */
452u32 blk_mq_unique_tag(struct request *rq)
453{
454 struct request_queue *q = rq->q;
455 struct blk_mq_hw_ctx *hctx;
456 int hwq = 0;
457
458 if (q->mq_ops) {
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200459 hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
Bart Van Assche205fb5f2014-10-30 14:45:11 +0100460 hwq = hctx->queue_num;
461 }
462
463 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
464 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
465}
466EXPORT_SYMBOL(blk_mq_unique_tag);
467
Jens Axboe320ae512013-10-24 09:20:05 +0100468ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
469{
470 char *orig_page = page;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600471 unsigned int free, res;
Jens Axboe320ae512013-10-24 09:20:05 +0100472
473 if (!tags)
474 return 0;
475
Jens Axboe59d13bf2014-05-09 13:41:15 -0600476 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
477 "bits_per_word=%u\n",
478 tags->nr_tags, tags->nr_reserved_tags,
Omar Sandoval88459642016-09-17 08:38:44 -0600479 1U << tags->bitmap_tags.sb.shift);
Jens Axboe320ae512013-10-24 09:20:05 +0100480
Jens Axboe4bb659b2014-05-09 09:36:49 -0600481 free = bt_unused_tags(&tags->bitmap_tags);
482 res = bt_unused_tags(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100483
Jens Axboe4bb659b2014-05-09 09:36:49 -0600484 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600485 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
Jens Axboe320ae512013-10-24 09:20:05 +0100486
487 return page - orig_page;
488}