]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-tag.c
elevator: fix unnecessary put of elevator in failure case
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq-tag.c
CommitLineData
75bb4625 1/*
88459642
OS
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
4 * is used.
75bb4625
JA
5 *
6 * Copyright (C) 2013-2014 Jens Axboe
7 */
320ae51f
JA
8#include <linux/kernel.h>
9#include <linux/module.h>
320ae51f
JA
10
11#include <linux/blk-mq.h>
12#include "blk.h"
13#include "blk-mq.h"
14#include "blk-mq-tag.h"
15
320ae51f
JA
16bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
17{
4bb659b1
JA
18 if (!tags)
19 return true;
20
88459642 21 return sbitmap_any_bit_clear(&tags->bitmap_tags.sb);
0d2602ca
JA
22}
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/*
aed3ea94 37 * Wakeup all potentially sleeping on tags
0d2602ca 38 */
aed3ea94 39void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
0d2602ca 40{
88459642
OS
41 sbitmap_queue_wake_all(&tags->bitmap_tags);
42 if (include_reserve)
43 sbitmap_queue_wake_all(&tags->breserved_tags);
0d2602ca
JA
44}
45
e3a2b3f9
JA
46/*
47 * 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
aed3ea94 59 blk_mq_tag_wakeup_all(tags, false);
e3a2b3f9
JA
60}
61
0d2602ca
JA
62/*
63 * 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,
88459642 67 struct sbitmap_queue *bt)
0d2602ca
JA
68{
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 */
88459642 79 if (bt->sb.depth == 1)
0d2602ca
JA
80 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 */
88459642 89 depth = max((bt->sb.depth + users - 1) / users, 4U);
0d2602ca
JA
90 return atomic_read(&hctx->nr_active) < depth;
91}
92
4941115b 93static int __blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt)
4bb659b1 94{
0d2602ca
JA
95 if (!hctx_may_queue(hctx, bt))
96 return -1;
f4a644db 97 return __sbitmap_queue_get(bt);
4bb659b1
JA
98}
99
4941115b 100unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
320ae51f 101{
4941115b
JA
102 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
103 struct sbitmap_queue *bt;
88459642 104 struct sbq_wait_state *ws;
4bb659b1 105 DEFINE_WAIT(wait);
4941115b 106 unsigned int tag_offset;
320ae51f
JA
107 int tag;
108
4941115b
JA
109 if (data->flags & BLK_MQ_REQ_RESERVED) {
110 if (unlikely(!tags->nr_reserved_tags)) {
111 WARN_ON_ONCE(1);
112 return BLK_MQ_TAG_FAIL;
113 }
114 bt = &tags->breserved_tags;
115 tag_offset = 0;
116 } else {
117 bt = &tags->bitmap_tags;
118 tag_offset = tags->nr_reserved_tags;
119 }
120
121 tag = __blk_mq_get_tag(data->hctx, bt);
4bb659b1 122 if (tag != -1)
4941115b 123 goto found_tag;
4bb659b1 124
6f3b0e8b 125 if (data->flags & BLK_MQ_REQ_NOWAIT)
4941115b 126 return BLK_MQ_TAG_FAIL;
4bb659b1 127
4941115b 128 ws = bt_wait_ptr(bt, data->hctx);
4bb659b1 129 do {
88459642 130 prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE);
4bb659b1 131
4941115b 132 tag = __blk_mq_get_tag(data->hctx, bt);
4bb659b1
JA
133 if (tag != -1)
134 break;
135
b3223207
BVA
136 /*
137 * We're out of tags on this hardware queue, kick any
138 * pending IO submits before going to sleep waiting for
bc188d81
SB
139 * some to complete. Note that hctx can be NULL here for
140 * reserved tag allocation.
b3223207 141 */
4941115b
JA
142 if (data->hctx)
143 blk_mq_run_hw_queue(data->hctx, false);
b3223207 144
080ff351
JA
145 /*
146 * Retry tag allocation after running the hardware queue,
147 * as running the queue may also have found completions.
148 */
4941115b 149 tag = __blk_mq_get_tag(data->hctx, bt);
080ff351
JA
150 if (tag != -1)
151 break;
152
cb96a42c
ML
153 blk_mq_put_ctx(data->ctx);
154
4bb659b1 155 io_schedule();
cb96a42c
ML
156
157 data->ctx = blk_mq_get_ctx(data->q);
7d7e0f90 158 data->hctx = blk_mq_map_queue(data->q, data->ctx->cpu);
4941115b
JA
159 tags = blk_mq_tags_from_data(data);
160 if (data->flags & BLK_MQ_REQ_RESERVED)
161 bt = &tags->breserved_tags;
162 else
163 bt = &tags->bitmap_tags;
164
88459642 165 finish_wait(&ws->wait, &wait);
4941115b 166 ws = bt_wait_ptr(bt, data->hctx);
4bb659b1
JA
167 } while (1);
168
88459642 169 finish_wait(&ws->wait, &wait);
320ae51f 170
4941115b
JA
171found_tag:
172 return tag + tag_offset;
320ae51f
JA
173}
174
4941115b
JA
175void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags,
176 struct blk_mq_ctx *ctx, unsigned int tag)
320ae51f 177{
4bb659b1
JA
178 if (tag >= tags->nr_reserved_tags) {
179 const int real_tag = tag - tags->nr_reserved_tags;
180
70114c39 181 BUG_ON(real_tag >= tags->nr_tags);
f4a644db 182 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
70114c39
JA
183 } else {
184 BUG_ON(tag >= tags->nr_reserved_tags);
f4a644db 185 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
70114c39 186 }
320ae51f
JA
187}
188
88459642
OS
189struct bt_iter_data {
190 struct blk_mq_hw_ctx *hctx;
191 busy_iter_fn *fn;
192 void *data;
193 bool reserved;
194};
195
196static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
320ae51f 197{
88459642
OS
198 struct bt_iter_data *iter_data = data;
199 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
200 struct blk_mq_tags *tags = hctx->tags;
201 bool reserved = iter_data->reserved;
81481eb4 202 struct request *rq;
4bb659b1 203
88459642
OS
204 if (!reserved)
205 bitnr += tags->nr_reserved_tags;
206 rq = tags->rqs[bitnr];
4bb659b1 207
88459642
OS
208 if (rq->q == hctx->queue)
209 iter_data->fn(hctx, rq, iter_data->data, reserved);
210 return true;
211}
4bb659b1 212
88459642
OS
213static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
214 busy_iter_fn *fn, void *data, bool reserved)
215{
216 struct bt_iter_data iter_data = {
217 .hctx = hctx,
218 .fn = fn,
219 .data = data,
220 .reserved = reserved,
221 };
222
223 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
320ae51f
JA
224}
225
88459642
OS
226struct bt_tags_iter_data {
227 struct blk_mq_tags *tags;
228 busy_tag_iter_fn *fn;
229 void *data;
230 bool reserved;
231};
232
233static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
f26cdc85 234{
88459642
OS
235 struct bt_tags_iter_data *iter_data = data;
236 struct blk_mq_tags *tags = iter_data->tags;
237 bool reserved = iter_data->reserved;
f26cdc85 238 struct request *rq;
f26cdc85 239
88459642
OS
240 if (!reserved)
241 bitnr += tags->nr_reserved_tags;
242 rq = tags->rqs[bitnr];
f26cdc85 243
88459642
OS
244 iter_data->fn(rq, iter_data->data, reserved);
245 return true;
246}
247
248static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
249 busy_tag_iter_fn *fn, void *data, bool reserved)
250{
251 struct bt_tags_iter_data iter_data = {
252 .tags = tags,
253 .fn = fn,
254 .data = data,
255 .reserved = reserved,
256 };
257
258 if (tags->rqs)
259 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
f26cdc85
KB
260}
261
e8f1e163
SG
262static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
263 busy_tag_iter_fn *fn, void *priv)
f26cdc85
KB
264{
265 if (tags->nr_reserved_tags)
88459642
OS
266 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
267 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
f26cdc85 268}
f26cdc85 269
e0489487
SG
270void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
271 busy_tag_iter_fn *fn, void *priv)
272{
273 int i;
274
275 for (i = 0; i < tagset->nr_hw_queues; i++) {
276 if (tagset->tags && tagset->tags[i])
277 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
278 }
279}
280EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
281
486cf989
SG
282int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
283{
284 int i, j, ret = 0;
285
286 if (!set->ops->reinit_request)
287 goto out;
288
289 for (i = 0; i < set->nr_hw_queues; i++) {
290 struct blk_mq_tags *tags = set->tags[i];
291
292 for (j = 0; j < tags->nr_tags; j++) {
2af8cbe3 293 if (!tags->static_rqs[j])
486cf989
SG
294 continue;
295
296 ret = set->ops->reinit_request(set->driver_data,
2af8cbe3 297 tags->static_rqs[j]);
486cf989
SG
298 if (ret)
299 goto out;
300 }
301 }
302
303out:
304 return ret;
305}
306EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
307
0bf6cd5b 308void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
81481eb4 309 void *priv)
320ae51f 310{
0bf6cd5b
CH
311 struct blk_mq_hw_ctx *hctx;
312 int i;
313
314
315 queue_for_each_hw_ctx(q, hctx, i) {
316 struct blk_mq_tags *tags = hctx->tags;
317
318 /*
319 * If not software queues are currently mapped to this
320 * hardware queue, there's nothing to check
321 */
322 if (!blk_mq_hw_queue_mapped(hctx))
323 continue;
324
325 if (tags->nr_reserved_tags)
88459642
OS
326 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
327 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
4bb659b1
JA
328 }
329
4bb659b1
JA
330}
331
88459642 332static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
e3a2b3f9 333{
88459642 334 return bt->sb.depth - sbitmap_weight(&bt->sb);
e3a2b3f9
JA
335}
336
f4a644db
OS
337static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
338 bool round_robin, int node)
4bb659b1 339{
f4a644db
OS
340 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
341 node);
4bb659b1
JA
342}
343
344static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
24391c0d 345 int node, int alloc_policy)
4bb659b1
JA
346{
347 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
f4a644db 348 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
4bb659b1 349
f4a644db 350 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
88459642 351 goto free_tags;
f4a644db
OS
352 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin,
353 node))
88459642 354 goto free_bitmap_tags;
4bb659b1
JA
355
356 return tags;
88459642
OS
357free_bitmap_tags:
358 sbitmap_queue_free(&tags->bitmap_tags);
359free_tags:
4bb659b1
JA
360 kfree(tags);
361 return NULL;
362}
363
320ae51f 364struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
24391c0d
SL
365 unsigned int reserved_tags,
366 int node, int alloc_policy)
320ae51f 367{
320ae51f 368 struct blk_mq_tags *tags;
320ae51f
JA
369
370 if (total_tags > BLK_MQ_TAG_MAX) {
371 pr_err("blk-mq: tag depth too large\n");
372 return NULL;
373 }
374
375 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
376 if (!tags)
377 return NULL;
378
320ae51f
JA
379 tags->nr_tags = total_tags;
380 tags->nr_reserved_tags = reserved_tags;
320ae51f 381
24391c0d 382 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
320ae51f
JA
383}
384
385void blk_mq_free_tags(struct blk_mq_tags *tags)
386{
88459642
OS
387 sbitmap_queue_free(&tags->bitmap_tags);
388 sbitmap_queue_free(&tags->breserved_tags);
320ae51f
JA
389 kfree(tags);
390}
391
e3a2b3f9
JA
392int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
393{
394 tdepth -= tags->nr_reserved_tags;
395 if (tdepth > tags->nr_tags)
396 return -EINVAL;
397
398 /*
399 * Don't need (or can't) update reserved tags here, they remain
400 * static and should never need resizing.
401 */
88459642
OS
402 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
403
aed3ea94 404 blk_mq_tag_wakeup_all(tags, false);
e3a2b3f9
JA
405 return 0;
406}
407
205fb5f5
BVA
408/**
409 * blk_mq_unique_tag() - return a tag that is unique queue-wide
410 * @rq: request for which to compute a unique tag
411 *
412 * The tag field in struct request is unique per hardware queue but not over
413 * all hardware queues. Hence this function that returns a tag with the
414 * hardware context index in the upper bits and the per hardware queue tag in
415 * the lower bits.
416 *
417 * Note: When called for a request that is queued on a non-multiqueue request
418 * queue, the hardware context index is set to zero.
419 */
420u32 blk_mq_unique_tag(struct request *rq)
421{
422 struct request_queue *q = rq->q;
423 struct blk_mq_hw_ctx *hctx;
424 int hwq = 0;
425
426 if (q->mq_ops) {
7d7e0f90 427 hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
205fb5f5
BVA
428 hwq = hctx->queue_num;
429 }
430
431 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
432 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
433}
434EXPORT_SYMBOL(blk_mq_unique_tag);
435
320ae51f
JA
436ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
437{
438 char *orig_page = page;
4bb659b1 439 unsigned int free, res;
320ae51f
JA
440
441 if (!tags)
442 return 0;
443
59d13bf5
JA
444 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
445 "bits_per_word=%u\n",
446 tags->nr_tags, tags->nr_reserved_tags,
88459642 447 1U << tags->bitmap_tags.sb.shift);
320ae51f 448
4bb659b1
JA
449 free = bt_unused_tags(&tags->bitmap_tags);
450 res = bt_unused_tags(&tags->breserved_tags);
320ae51f 451
4bb659b1 452 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
0d2602ca 453 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
320ae51f
JA
454
455 return page - orig_page;
456}