blob: 2cbdecd594e9704c6c807c0e30b6bc0ffe2fc196 [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,
Shaohua Li24391c02015-01-23 14:18:00 -070097 unsigned int *tag_cache, struct blk_mq_tags *tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -060098{
Omar Sandoval88459642016-09-17 08:38:44 -060099 unsigned int last_tag;
100 int tag;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600101
Jens Axboe0d2602c2014-05-13 15:10:52 -0600102 if (!hctx_may_queue(hctx, bt))
103 return -1;
104
Omar Sandoval88459642016-09-17 08:38:44 -0600105 last_tag = *tag_cache;
106 tag = sbitmap_get(&bt->sb, last_tag, BT_ALLOC_RR(tags));
Jens Axboe4bb659b2014-05-09 09:36:49 -0600107
Omar Sandoval88459642016-09-17 08:38:44 -0600108 if (tag == -1) {
109 *tag_cache = 0;
110 } else if (tag == last_tag || unlikely(BT_ALLOC_RR(tags))) {
Jens Axboe4bb659b2014-05-09 09:36:49 -0600111 last_tag = tag + 1;
Omar Sandoval88459642016-09-17 08:38:44 -0600112 if (last_tag >= bt->sb.depth - 1)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600113 last_tag = 0;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600114 *tag_cache = last_tag;
115 }
116
117 return tag;
118}
119
Ming Leicb96a422014-06-01 00:43:37 +0800120static int bt_get(struct blk_mq_alloc_data *data,
Omar Sandoval88459642016-09-17 08:38:44 -0600121 struct sbitmap_queue *bt,
122 struct blk_mq_hw_ctx *hctx,
123 unsigned int *last_tag, struct blk_mq_tags *tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600124{
Omar Sandoval88459642016-09-17 08:38:44 -0600125 struct sbq_wait_state *ws;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600126 DEFINE_WAIT(wait);
127 int tag;
128
Shaohua Li24391c02015-01-23 14:18:00 -0700129 tag = __bt_get(hctx, bt, last_tag, tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600130 if (tag != -1)
131 return tag;
132
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100133 if (data->flags & BLK_MQ_REQ_NOWAIT)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600134 return -1;
135
Omar Sandoval88459642016-09-17 08:38:44 -0600136 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600137 do {
Omar Sandoval88459642016-09-17 08:38:44 -0600138 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600139
Shaohua Li24391c02015-01-23 14:18:00 -0700140 tag = __bt_get(hctx, bt, last_tag, tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600141 if (tag != -1)
142 break;
143
Bart Van Asscheb3223202014-12-08 08:46:34 -0700144 /*
145 * We're out of tags on this hardware queue, kick any
146 * pending IO submits before going to sleep waiting for
Sam Bradshawbc188d82015-03-18 17:06:18 -0600147 * some to complete. Note that hctx can be NULL here for
148 * reserved tag allocation.
Bart Van Asscheb3223202014-12-08 08:46:34 -0700149 */
Sam Bradshawbc188d82015-03-18 17:06:18 -0600150 if (hctx)
151 blk_mq_run_hw_queue(hctx, false);
Bart Van Asscheb3223202014-12-08 08:46:34 -0700152
Jens Axboe080ff352014-12-08 08:49:06 -0700153 /*
154 * Retry tag allocation after running the hardware queue,
155 * as running the queue may also have found completions.
156 */
Shaohua Li24391c02015-01-23 14:18:00 -0700157 tag = __bt_get(hctx, bt, last_tag, tags);
Jens Axboe080ff352014-12-08 08:49:06 -0700158 if (tag != -1)
159 break;
160
Ming Leicb96a422014-06-01 00:43:37 +0800161 blk_mq_put_ctx(data->ctx);
162
Jens Axboe4bb659b2014-05-09 09:36:49 -0600163 io_schedule();
Ming Leicb96a422014-06-01 00:43:37 +0800164
165 data->ctx = blk_mq_get_ctx(data->q);
166 data->hctx = data->q->mq_ops->map_queue(data->q,
167 data->ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100168 if (data->flags & BLK_MQ_REQ_RESERVED) {
Ming Leicb96a422014-06-01 00:43:37 +0800169 bt = &data->hctx->tags->breserved_tags;
170 } else {
171 last_tag = &data->ctx->last_tag;
172 hctx = data->hctx;
173 bt = &hctx->tags->bitmap_tags;
174 }
Omar Sandoval88459642016-09-17 08:38:44 -0600175 finish_wait(&ws->wait, &wait);
176 ws = bt_wait_ptr(bt, hctx);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600177 } while (1);
178
Omar Sandoval88459642016-09-17 08:38:44 -0600179 finish_wait(&ws->wait, &wait);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600180 return tag;
181}
182
Ming Leicb96a422014-06-01 00:43:37 +0800183static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100184{
185 int tag;
186
Ming Leicb96a422014-06-01 00:43:37 +0800187 tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
Shaohua Li24391c02015-01-23 14:18:00 -0700188 &data->ctx->last_tag, data->hctx->tags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600189 if (tag >= 0)
Ming Leicb96a422014-06-01 00:43:37 +0800190 return tag + data->hctx->tags->nr_reserved_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600191
192 return BLK_MQ_TAG_FAIL;
Jens Axboe320ae512013-10-24 09:20:05 +0100193}
194
Ming Leicb96a422014-06-01 00:43:37 +0800195static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100196{
Jens Axboe4bb659b2014-05-09 09:36:49 -0600197 int tag, zero = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100198
Ming Leicb96a422014-06-01 00:43:37 +0800199 if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100200 WARN_ON_ONCE(1);
201 return BLK_MQ_TAG_FAIL;
202 }
203
Shaohua Li24391c02015-01-23 14:18:00 -0700204 tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL, &zero,
205 data->hctx->tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100206 if (tag < 0)
207 return BLK_MQ_TAG_FAIL;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600208
Jens Axboe320ae512013-10-24 09:20:05 +0100209 return tag;
210}
211
Ming Leicb96a422014-06-01 00:43:37 +0800212unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100213{
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100214 if (data->flags & BLK_MQ_REQ_RESERVED)
215 return __blk_mq_get_reserved_tag(data);
216 return __blk_mq_get_tag(data);
Jens Axboe320ae512013-10-24 09:20:05 +0100217}
218
Jens Axboe0d2602c2014-05-13 15:10:52 -0600219void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
Jens Axboe4bb659b2014-05-09 09:36:49 -0600220 unsigned int *last_tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100221{
Jens Axboe0d2602c2014-05-13 15:10:52 -0600222 struct blk_mq_tags *tags = hctx->tags;
223
Jens Axboe4bb659b2014-05-09 09:36:49 -0600224 if (tag >= tags->nr_reserved_tags) {
225 const int real_tag = tag - tags->nr_reserved_tags;
226
Jens Axboe70114c32014-11-24 15:52:30 -0700227 BUG_ON(real_tag >= tags->nr_tags);
Omar Sandoval88459642016-09-17 08:38:44 -0600228 sbitmap_queue_clear(&tags->bitmap_tags, real_tag);
Shaohua Li24391c02015-01-23 14:18:00 -0700229 if (likely(tags->alloc_policy == BLK_TAG_ALLOC_FIFO))
230 *last_tag = real_tag;
Jens Axboe70114c32014-11-24 15:52:30 -0700231 } else {
232 BUG_ON(tag >= tags->nr_reserved_tags);
Omar Sandoval88459642016-09-17 08:38:44 -0600233 sbitmap_queue_clear(&tags->breserved_tags, tag);
Jens Axboe70114c32014-11-24 15:52:30 -0700234 }
Jens Axboe320ae512013-10-24 09:20:05 +0100235}
236
Omar Sandoval88459642016-09-17 08:38:44 -0600237struct bt_iter_data {
238 struct blk_mq_hw_ctx *hctx;
239 busy_iter_fn *fn;
240 void *data;
241 bool reserved;
242};
243
244static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100245{
Omar Sandoval88459642016-09-17 08:38:44 -0600246 struct bt_iter_data *iter_data = data;
247 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
248 struct blk_mq_tags *tags = hctx->tags;
249 bool reserved = iter_data->reserved;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700250 struct request *rq;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600251
Omar Sandoval88459642016-09-17 08:38:44 -0600252 if (!reserved)
253 bitnr += tags->nr_reserved_tags;
254 rq = tags->rqs[bitnr];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600255
Omar Sandoval88459642016-09-17 08:38:44 -0600256 if (rq->q == hctx->queue)
257 iter_data->fn(hctx, rq, iter_data->data, reserved);
258 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100259}
260
Omar Sandoval88459642016-09-17 08:38:44 -0600261static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
262 busy_iter_fn *fn, void *data, bool reserved)
Keith Buschf26cdc82015-06-01 09:29:53 -0600263{
Omar Sandoval88459642016-09-17 08:38:44 -0600264 struct bt_iter_data iter_data = {
265 .hctx = hctx,
266 .fn = fn,
267 .data = data,
268 .reserved = reserved,
269 };
270
271 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
272}
273
274struct bt_tags_iter_data {
275 struct blk_mq_tags *tags;
276 busy_tag_iter_fn *fn;
277 void *data;
278 bool reserved;
279};
280
281static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
282{
283 struct bt_tags_iter_data *iter_data = data;
284 struct blk_mq_tags *tags = iter_data->tags;
285 bool reserved = iter_data->reserved;
Keith Buschf26cdc82015-06-01 09:29:53 -0600286 struct request *rq;
Keith Buschf26cdc82015-06-01 09:29:53 -0600287
Omar Sandoval88459642016-09-17 08:38:44 -0600288 if (!reserved)
289 bitnr += tags->nr_reserved_tags;
290 rq = tags->rqs[bitnr];
Keith Buschf26cdc82015-06-01 09:29:53 -0600291
Omar Sandoval88459642016-09-17 08:38:44 -0600292 iter_data->fn(rq, iter_data->data, reserved);
293 return true;
294}
Keith Buschf26cdc82015-06-01 09:29:53 -0600295
Omar Sandoval88459642016-09-17 08:38:44 -0600296static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
297 busy_tag_iter_fn *fn, void *data, bool reserved)
298{
299 struct bt_tags_iter_data iter_data = {
300 .tags = tags,
301 .fn = fn,
302 .data = data,
303 .reserved = reserved,
304 };
305
306 if (tags->rqs)
307 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
Keith Buschf26cdc82015-06-01 09:29:53 -0600308}
309
Sagi Grimberge8f1e162016-03-10 13:58:49 +0200310static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
311 busy_tag_iter_fn *fn, void *priv)
Keith Buschf26cdc82015-06-01 09:29:53 -0600312{
313 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600314 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
315 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
Keith Buschf26cdc82015-06-01 09:29:53 -0600316}
Keith Buschf26cdc82015-06-01 09:29:53 -0600317
Sagi Grimberge0489482016-03-10 13:58:46 +0200318void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
319 busy_tag_iter_fn *fn, void *priv)
320{
321 int i;
322
323 for (i = 0; i < tagset->nr_hw_queues; i++) {
324 if (tagset->tags && tagset->tags[i])
325 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
326 }
327}
328EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
329
Sagi Grimberg486cf982016-07-06 21:55:48 +0900330int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
331{
332 int i, j, ret = 0;
333
334 if (!set->ops->reinit_request)
335 goto out;
336
337 for (i = 0; i < set->nr_hw_queues; i++) {
338 struct blk_mq_tags *tags = set->tags[i];
339
340 for (j = 0; j < tags->nr_tags; j++) {
341 if (!tags->rqs[j])
342 continue;
343
344 ret = set->ops->reinit_request(set->driver_data,
345 tags->rqs[j]);
346 if (ret)
347 goto out;
348 }
349 }
350
351out:
352 return ret;
353}
354EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
355
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200356void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700357 void *priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100358{
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200359 struct blk_mq_hw_ctx *hctx;
360 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100361
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200362
363 queue_for_each_hw_ctx(q, hctx, i) {
364 struct blk_mq_tags *tags = hctx->tags;
365
366 /*
367 * If not software queues are currently mapped to this
368 * hardware queue, there's nothing to check
369 */
370 if (!blk_mq_hw_queue_mapped(hctx))
371 continue;
372
373 if (tags->nr_reserved_tags)
Omar Sandoval88459642016-09-17 08:38:44 -0600374 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
375 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200376 }
377
Jens Axboe320ae512013-10-24 09:20:05 +0100378}
379
Omar Sandoval88459642016-09-17 08:38:44 -0600380static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600381{
Omar Sandoval88459642016-09-17 08:38:44 -0600382 return bt->sb.depth - sbitmap_weight(&bt->sb);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600383}
384
Omar Sandoval88459642016-09-17 08:38:44 -0600385static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, int node)
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600386{
Omar Sandoval88459642016-09-17 08:38:44 -0600387 return sbitmap_queue_init_node(bt, depth, -1, GFP_KERNEL, node);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600388}
389
390static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700391 int node, int alloc_policy)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600392{
393 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
394
Shaohua Li24391c02015-01-23 14:18:00 -0700395 tags->alloc_policy = alloc_policy;
396
Omar Sandoval88459642016-09-17 08:38:44 -0600397 if (bt_alloc(&tags->bitmap_tags, depth, node))
398 goto free_tags;
399 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node))
400 goto free_bitmap_tags;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600401
402 return tags;
Omar Sandoval88459642016-09-17 08:38:44 -0600403free_bitmap_tags:
404 sbitmap_queue_free(&tags->bitmap_tags);
405free_tags:
Jens Axboe4bb659b2014-05-09 09:36:49 -0600406 kfree(tags);
407 return NULL;
408}
409
Jens Axboe320ae512013-10-24 09:20:05 +0100410struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
Shaohua Li24391c02015-01-23 14:18:00 -0700411 unsigned int reserved_tags,
412 int node, int alloc_policy)
Jens Axboe320ae512013-10-24 09:20:05 +0100413{
Jens Axboe320ae512013-10-24 09:20:05 +0100414 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100415
416 if (total_tags > BLK_MQ_TAG_MAX) {
417 pr_err("blk-mq: tag depth too large\n");
418 return NULL;
419 }
420
421 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
422 if (!tags)
423 return NULL;
424
Keith Buschf26cdc82015-06-01 09:29:53 -0600425 if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
426 kfree(tags);
427 return NULL;
428 }
429
Jens Axboe320ae512013-10-24 09:20:05 +0100430 tags->nr_tags = total_tags;
431 tags->nr_reserved_tags = reserved_tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100432
Shaohua Li24391c02015-01-23 14:18:00 -0700433 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
Jens Axboe320ae512013-10-24 09:20:05 +0100434}
435
436void blk_mq_free_tags(struct blk_mq_tags *tags)
437{
Omar Sandoval88459642016-09-17 08:38:44 -0600438 sbitmap_queue_free(&tags->bitmap_tags);
439 sbitmap_queue_free(&tags->breserved_tags);
Junichi Nomuraf42d79a2015-10-14 05:02:15 +0000440 free_cpumask_var(tags->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +0100441 kfree(tags);
442}
443
Jens Axboe4bb659b2014-05-09 09:36:49 -0600444void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
445{
446 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
447
Ming Lei9d3d21a2014-05-10 15:43:14 -0600448 *tag = prandom_u32() % depth;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600449}
450
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600451int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
452{
453 tdepth -= tags->nr_reserved_tags;
454 if (tdepth > tags->nr_tags)
455 return -EINVAL;
456
457 /*
458 * Don't need (or can't) update reserved tags here, they remain
459 * static and should never need resizing.
460 */
Omar Sandoval88459642016-09-17 08:38:44 -0600461 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
462
Jens Axboeaed3ea92014-12-22 14:04:42 -0700463 blk_mq_tag_wakeup_all(tags, false);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600464 return 0;
465}
466
Bart Van Assche205fb5f2014-10-30 14:45:11 +0100467/**
468 * blk_mq_unique_tag() - return a tag that is unique queue-wide
469 * @rq: request for which to compute a unique tag
470 *
471 * The tag field in struct request is unique per hardware queue but not over
472 * all hardware queues. Hence this function that returns a tag with the
473 * hardware context index in the upper bits and the per hardware queue tag in
474 * the lower bits.
475 *
476 * Note: When called for a request that is queued on a non-multiqueue request
477 * queue, the hardware context index is set to zero.
478 */
479u32 blk_mq_unique_tag(struct request *rq)
480{
481 struct request_queue *q = rq->q;
482 struct blk_mq_hw_ctx *hctx;
483 int hwq = 0;
484
485 if (q->mq_ops) {
486 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
487 hwq = hctx->queue_num;
488 }
489
490 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
491 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
492}
493EXPORT_SYMBOL(blk_mq_unique_tag);
494
Jens Axboe320ae512013-10-24 09:20:05 +0100495ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
496{
497 char *orig_page = page;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600498 unsigned int free, res;
Jens Axboe320ae512013-10-24 09:20:05 +0100499
500 if (!tags)
501 return 0;
502
Jens Axboe59d13bf2014-05-09 13:41:15 -0600503 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
504 "bits_per_word=%u\n",
505 tags->nr_tags, tags->nr_reserved_tags,
Omar Sandoval88459642016-09-17 08:38:44 -0600506 1U << tags->bitmap_tags.sb.shift);
Jens Axboe320ae512013-10-24 09:20:05 +0100507
Jens Axboe4bb659b2014-05-09 09:36:49 -0600508 free = bt_unused_tags(&tags->bitmap_tags);
509 res = bt_unused_tags(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100510
Jens Axboe4bb659b2014-05-09 09:36:49 -0600511 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600512 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
Jens Axboe320ae512013-10-24 09:20:05 +0100513
514 return page - orig_page;
515}