]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-tag.c
cfq-iosched: Adjust one function call together with a variable assignment
[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
8cecb07d 139 * some to complete.
b3223207 140 */
8cecb07d 141 blk_mq_run_hw_queue(data->hctx, false);
b3223207 142
080ff351
JA
143 /*
144 * Retry tag allocation after running the hardware queue,
145 * as running the queue may also have found completions.
146 */
4941115b 147 tag = __blk_mq_get_tag(data->hctx, bt);
080ff351
JA
148 if (tag != -1)
149 break;
150
cb96a42c
ML
151 blk_mq_put_ctx(data->ctx);
152
4bb659b1 153 io_schedule();
cb96a42c
ML
154
155 data->ctx = blk_mq_get_ctx(data->q);
7d7e0f90 156 data->hctx = blk_mq_map_queue(data->q, data->ctx->cpu);
4941115b
JA
157 tags = blk_mq_tags_from_data(data);
158 if (data->flags & BLK_MQ_REQ_RESERVED)
159 bt = &tags->breserved_tags;
160 else
161 bt = &tags->bitmap_tags;
162
88459642 163 finish_wait(&ws->wait, &wait);
4941115b 164 ws = bt_wait_ptr(bt, data->hctx);
4bb659b1
JA
165 } while (1);
166
88459642 167 finish_wait(&ws->wait, &wait);
320ae51f 168
4941115b
JA
169found_tag:
170 return tag + tag_offset;
320ae51f
JA
171}
172
4941115b
JA
173void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_tags *tags,
174 struct blk_mq_ctx *ctx, unsigned int tag)
320ae51f 175{
4bb659b1
JA
176 if (tag >= tags->nr_reserved_tags) {
177 const int real_tag = tag - tags->nr_reserved_tags;
178
70114c39 179 BUG_ON(real_tag >= tags->nr_tags);
f4a644db 180 sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
70114c39
JA
181 } else {
182 BUG_ON(tag >= tags->nr_reserved_tags);
f4a644db 183 sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
70114c39 184 }
320ae51f
JA
185}
186
88459642
OS
187struct bt_iter_data {
188 struct blk_mq_hw_ctx *hctx;
189 busy_iter_fn *fn;
190 void *data;
191 bool reserved;
192};
193
194static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
320ae51f 195{
88459642
OS
196 struct bt_iter_data *iter_data = data;
197 struct blk_mq_hw_ctx *hctx = iter_data->hctx;
198 struct blk_mq_tags *tags = hctx->tags;
199 bool reserved = iter_data->reserved;
81481eb4 200 struct request *rq;
4bb659b1 201
88459642
OS
202 if (!reserved)
203 bitnr += tags->nr_reserved_tags;
204 rq = tags->rqs[bitnr];
4bb659b1 205
88459642
OS
206 if (rq->q == hctx->queue)
207 iter_data->fn(hctx, rq, iter_data->data, reserved);
208 return true;
209}
4bb659b1 210
88459642
OS
211static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
212 busy_iter_fn *fn, void *data, bool reserved)
213{
214 struct bt_iter_data iter_data = {
215 .hctx = hctx,
216 .fn = fn,
217 .data = data,
218 .reserved = reserved,
219 };
220
221 sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
320ae51f
JA
222}
223
88459642
OS
224struct bt_tags_iter_data {
225 struct blk_mq_tags *tags;
226 busy_tag_iter_fn *fn;
227 void *data;
228 bool reserved;
229};
230
231static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
f26cdc85 232{
88459642
OS
233 struct bt_tags_iter_data *iter_data = data;
234 struct blk_mq_tags *tags = iter_data->tags;
235 bool reserved = iter_data->reserved;
f26cdc85 236 struct request *rq;
f26cdc85 237
88459642
OS
238 if (!reserved)
239 bitnr += tags->nr_reserved_tags;
240 rq = tags->rqs[bitnr];
f26cdc85 241
88459642
OS
242 iter_data->fn(rq, iter_data->data, reserved);
243 return true;
244}
245
246static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
247 busy_tag_iter_fn *fn, void *data, bool reserved)
248{
249 struct bt_tags_iter_data iter_data = {
250 .tags = tags,
251 .fn = fn,
252 .data = data,
253 .reserved = reserved,
254 };
255
256 if (tags->rqs)
257 sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
f26cdc85
KB
258}
259
e8f1e163
SG
260static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags,
261 busy_tag_iter_fn *fn, void *priv)
f26cdc85
KB
262{
263 if (tags->nr_reserved_tags)
88459642
OS
264 bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true);
265 bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false);
f26cdc85 266}
f26cdc85 267
e0489487
SG
268void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
269 busy_tag_iter_fn *fn, void *priv)
270{
271 int i;
272
273 for (i = 0; i < tagset->nr_hw_queues; i++) {
274 if (tagset->tags && tagset->tags[i])
275 blk_mq_all_tag_busy_iter(tagset->tags[i], fn, priv);
276 }
277}
278EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
279
486cf989
SG
280int blk_mq_reinit_tagset(struct blk_mq_tag_set *set)
281{
282 int i, j, ret = 0;
283
284 if (!set->ops->reinit_request)
285 goto out;
286
287 for (i = 0; i < set->nr_hw_queues; i++) {
288 struct blk_mq_tags *tags = set->tags[i];
289
290 for (j = 0; j < tags->nr_tags; j++) {
2af8cbe3 291 if (!tags->static_rqs[j])
486cf989
SG
292 continue;
293
294 ret = set->ops->reinit_request(set->driver_data,
2af8cbe3 295 tags->static_rqs[j]);
486cf989
SG
296 if (ret)
297 goto out;
298 }
299 }
300
301out:
302 return ret;
303}
304EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset);
305
0bf6cd5b 306void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
81481eb4 307 void *priv)
320ae51f 308{
0bf6cd5b
CH
309 struct blk_mq_hw_ctx *hctx;
310 int i;
311
312
313 queue_for_each_hw_ctx(q, hctx, i) {
314 struct blk_mq_tags *tags = hctx->tags;
315
316 /*
317 * If not software queues are currently mapped to this
318 * hardware queue, there's nothing to check
319 */
320 if (!blk_mq_hw_queue_mapped(hctx))
321 continue;
322
323 if (tags->nr_reserved_tags)
88459642
OS
324 bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
325 bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
4bb659b1
JA
326 }
327
4bb659b1
JA
328}
329
88459642 330static unsigned int bt_unused_tags(const struct sbitmap_queue *bt)
e3a2b3f9 331{
88459642 332 return bt->sb.depth - sbitmap_weight(&bt->sb);
e3a2b3f9
JA
333}
334
f4a644db
OS
335static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
336 bool round_robin, int node)
4bb659b1 337{
f4a644db
OS
338 return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
339 node);
4bb659b1
JA
340}
341
342static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
24391c0d 343 int node, int alloc_policy)
4bb659b1
JA
344{
345 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
f4a644db 346 bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
4bb659b1 347
f4a644db 348 if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
88459642 349 goto free_tags;
f4a644db
OS
350 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin,
351 node))
88459642 352 goto free_bitmap_tags;
4bb659b1
JA
353
354 return tags;
88459642
OS
355free_bitmap_tags:
356 sbitmap_queue_free(&tags->bitmap_tags);
357free_tags:
4bb659b1
JA
358 kfree(tags);
359 return NULL;
360}
361
320ae51f 362struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
24391c0d
SL
363 unsigned int reserved_tags,
364 int node, int alloc_policy)
320ae51f 365{
320ae51f 366 struct blk_mq_tags *tags;
320ae51f
JA
367
368 if (total_tags > BLK_MQ_TAG_MAX) {
369 pr_err("blk-mq: tag depth too large\n");
370 return NULL;
371 }
372
373 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
374 if (!tags)
375 return NULL;
376
320ae51f
JA
377 tags->nr_tags = total_tags;
378 tags->nr_reserved_tags = reserved_tags;
320ae51f 379
24391c0d 380 return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
320ae51f
JA
381}
382
383void blk_mq_free_tags(struct blk_mq_tags *tags)
384{
88459642
OS
385 sbitmap_queue_free(&tags->bitmap_tags);
386 sbitmap_queue_free(&tags->breserved_tags);
320ae51f
JA
387 kfree(tags);
388}
389
70f36b60
JA
390int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
391 struct blk_mq_tags **tagsptr, unsigned int tdepth,
392 bool can_grow)
e3a2b3f9 393{
70f36b60
JA
394 struct blk_mq_tags *tags = *tagsptr;
395
396 if (tdepth <= tags->nr_reserved_tags)
e3a2b3f9
JA
397 return -EINVAL;
398
70f36b60
JA
399 tdepth -= tags->nr_reserved_tags;
400
e3a2b3f9 401 /*
70f36b60
JA
402 * If we are allowed to grow beyond the original size, allocate
403 * a new set of tags before freeing the old one.
e3a2b3f9 404 */
70f36b60
JA
405 if (tdepth > tags->nr_tags) {
406 struct blk_mq_tag_set *set = hctx->queue->tag_set;
407 struct blk_mq_tags *new;
408 bool ret;
409
410 if (!can_grow)
411 return -EINVAL;
412
413 /*
414 * We need some sort of upper limit, set it high enough that
415 * no valid use cases should require more.
416 */
417 if (tdepth > 16 * BLKDEV_MAX_RQ)
418 return -EINVAL;
419
420 new = blk_mq_alloc_rq_map(set, hctx->queue_num, tdepth, 0);
421 if (!new)
422 return -ENOMEM;
423 ret = blk_mq_alloc_rqs(set, new, hctx->queue_num, tdepth);
424 if (ret) {
425 blk_mq_free_rq_map(new);
426 return -ENOMEM;
427 }
428
429 blk_mq_free_rqs(set, *tagsptr, hctx->queue_num);
430 blk_mq_free_rq_map(*tagsptr);
431 *tagsptr = new;
432 } else {
433 /*
434 * Don't need (or can't) update reserved tags here, they
435 * remain static and should never need resizing.
436 */
437 sbitmap_queue_resize(&tags->bitmap_tags, tdepth);
438 }
88459642 439
e3a2b3f9
JA
440 return 0;
441}
442
205fb5f5
BVA
443/**
444 * blk_mq_unique_tag() - return a tag that is unique queue-wide
445 * @rq: request for which to compute a unique tag
446 *
447 * The tag field in struct request is unique per hardware queue but not over
448 * all hardware queues. Hence this function that returns a tag with the
449 * hardware context index in the upper bits and the per hardware queue tag in
450 * the lower bits.
451 *
452 * Note: When called for a request that is queued on a non-multiqueue request
453 * queue, the hardware context index is set to zero.
454 */
455u32 blk_mq_unique_tag(struct request *rq)
456{
457 struct request_queue *q = rq->q;
458 struct blk_mq_hw_ctx *hctx;
459 int hwq = 0;
460
461 if (q->mq_ops) {
7d7e0f90 462 hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
205fb5f5
BVA
463 hwq = hctx->queue_num;
464 }
465
466 return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
467 (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
468}
469EXPORT_SYMBOL(blk_mq_unique_tag);
470
320ae51f
JA
471ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
472{
473 char *orig_page = page;
4bb659b1 474 unsigned int free, res;
320ae51f
JA
475
476 if (!tags)
477 return 0;
478
59d13bf5
JA
479 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
480 "bits_per_word=%u\n",
481 tags->nr_tags, tags->nr_reserved_tags,
88459642 482 1U << tags->bitmap_tags.sb.shift);
320ae51f 483
4bb659b1
JA
484 free = bt_unused_tags(&tags->bitmap_tags);
485 res = bt_unused_tags(&tags->breserved_tags);
320ae51f 486
4bb659b1 487 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
0d2602ca 488 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
320ae51f
JA
489
490 return page - orig_page;
491}