Commit | Line | Data |
---|---|---|
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> | |
4bb659b1 | 10 | #include <linux/random.h> |
320ae51f JA |
11 | |
12 | #include <linux/blk-mq.h> | |
13 | #include "blk.h" | |
14 | #include "blk-mq.h" | |
15 | #include "blk-mq-tag.h" | |
16 | ||
320ae51f JA |
17 | bool blk_mq_has_free_tags(struct blk_mq_tags *tags) |
18 | { | |
4bb659b1 JA |
19 | if (!tags) |
20 | return true; | |
21 | ||
88459642 | 22 | return sbitmap_any_bit_clear(&tags->bitmap_tags.sb); |
0d2602ca JA |
23 | } |
24 | ||
25 | /* | |
26 | * If a previously inactive queue goes active, bump the active user count. | |
27 | */ | |
28 | bool __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 | /* | |
aed3ea94 | 38 | * Wakeup all potentially sleeping on tags |
0d2602ca | 39 | */ |
aed3ea94 | 40 | void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve) |
0d2602ca | 41 | { |
88459642 OS |
42 | sbitmap_queue_wake_all(&tags->bitmap_tags); |
43 | if (include_reserve) | |
44 | sbitmap_queue_wake_all(&tags->breserved_tags); | |
0d2602ca JA |
45 | } |
46 | ||
e3a2b3f9 JA |
47 | /* |
48 | * If a previously busy queue goes inactive, potential waiters could now | |
49 | * be allowed to queue. Wake them up and check. | |
50 | */ | |
51 | void __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 | ||
aed3ea94 | 60 | blk_mq_tag_wakeup_all(tags, false); |
e3a2b3f9 JA |
61 | } |
62 | ||
0d2602ca JA |
63 | /* |
64 | * 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 | */ | |
67 | static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx, | |
88459642 | 68 | struct sbitmap_queue *bt) |
0d2602ca JA |
69 | { |
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 | */ | |
88459642 | 80 | if (bt->sb.depth == 1) |
0d2602ca JA |
81 | 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 | */ | |
88459642 | 90 | depth = max((bt->sb.depth + users - 1) / users, 4U); |
0d2602ca JA |
91 | return atomic_read(&hctx->nr_active) < depth; |
92 | } | |
93 | ||
f4a644db | 94 | static int __bt_get(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt) |
4bb659b1 | 95 | { |
0d2602ca JA |
96 | if (!hctx_may_queue(hctx, bt)) |
97 | return -1; | |
f4a644db | 98 | return __sbitmap_queue_get(bt); |
4bb659b1 JA |
99 | } |
100 | ||
40aabb67 OS |
101 | static 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) | |
320ae51f | 103 | { |
88459642 | 104 | struct sbq_wait_state *ws; |
4bb659b1 | 105 | DEFINE_WAIT(wait); |
320ae51f JA |
106 | int tag; |
107 | ||
f4a644db | 108 | tag = __bt_get(hctx, bt); |
4bb659b1 JA |
109 | if (tag != -1) |
110 | return tag; | |
111 | ||
6f3b0e8b | 112 | if (data->flags & BLK_MQ_REQ_NOWAIT) |
4bb659b1 JA |
113 | return -1; |
114 | ||
88459642 | 115 | ws = bt_wait_ptr(bt, hctx); |
4bb659b1 | 116 | do { |
88459642 | 117 | prepare_to_wait(&ws->wait, &wait, TASK_UNINTERRUPTIBLE); |
4bb659b1 | 118 | |
f4a644db | 119 | tag = __bt_get(hctx, bt); |
4bb659b1 JA |
120 | if (tag != -1) |
121 | break; | |
122 | ||
b3223207 BVA |
123 | /* |
124 | * We're out of tags on this hardware queue, kick any | |
125 | * pending IO submits before going to sleep waiting for | |
bc188d81 SB |
126 | * some to complete. Note that hctx can be NULL here for |
127 | * reserved tag allocation. | |
b3223207 | 128 | */ |
bc188d81 SB |
129 | if (hctx) |
130 | blk_mq_run_hw_queue(hctx, false); | |
b3223207 | 131 | |
080ff351 JA |
132 | /* |
133 | * Retry tag allocation after running the hardware queue, | |
134 | * as running the queue may also have found completions. | |
135 | */ | |
f4a644db | 136 | tag = __bt_get(hctx, bt); |
080ff351 JA |
137 | if (tag != -1) |
138 | break; | |
139 | ||
cb96a42c ML |
140 | blk_mq_put_ctx(data->ctx); |
141 | ||
4bb659b1 | 142 | io_schedule(); |
cb96a42c ML |
143 | |
144 | data->ctx = blk_mq_get_ctx(data->q); | |
145 | data->hctx = data->q->mq_ops->map_queue(data->q, | |
146 | data->ctx->cpu); | |
6f3b0e8b | 147 | if (data->flags & BLK_MQ_REQ_RESERVED) { |
cb96a42c ML |
148 | bt = &data->hctx->tags->breserved_tags; |
149 | } else { | |
cb96a42c ML |
150 | hctx = data->hctx; |
151 | bt = &hctx->tags->bitmap_tags; | |
152 | } | |
88459642 OS |
153 | finish_wait(&ws->wait, &wait); |
154 | ws = bt_wait_ptr(bt, hctx); | |
4bb659b1 JA |
155 | } while (1); |
156 | ||
88459642 | 157 | finish_wait(&ws->wait, &wait); |
4bb659b1 JA |
158 | return tag; |
159 | } | |
160 | ||
cb96a42c | 161 | static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data) |
4bb659b1 JA |
162 | { |
163 | int tag; | |
164 | ||
cb96a42c | 165 | tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx, |
40aabb67 | 166 | data->hctx->tags); |
4bb659b1 | 167 | if (tag >= 0) |
cb96a42c | 168 | return tag + data->hctx->tags->nr_reserved_tags; |
4bb659b1 JA |
169 | |
170 | return BLK_MQ_TAG_FAIL; | |
320ae51f JA |
171 | } |
172 | ||
cb96a42c | 173 | static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data) |
320ae51f | 174 | { |
40aabb67 | 175 | int tag; |
320ae51f | 176 | |
cb96a42c | 177 | if (unlikely(!data->hctx->tags->nr_reserved_tags)) { |
320ae51f JA |
178 | WARN_ON_ONCE(1); |
179 | return BLK_MQ_TAG_FAIL; | |
180 | } | |
181 | ||
40aabb67 OS |
182 | tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL, |
183 | data->hctx->tags); | |
320ae51f JA |
184 | if (tag < 0) |
185 | return BLK_MQ_TAG_FAIL; | |
4bb659b1 | 186 | |
320ae51f JA |
187 | return tag; |
188 | } | |
189 | ||
cb96a42c | 190 | unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data) |
320ae51f | 191 | { |
6f3b0e8b CH |
192 | if (data->flags & BLK_MQ_REQ_RESERVED) |
193 | return __blk_mq_get_reserved_tag(data); | |
194 | return __blk_mq_get_tag(data); | |
320ae51f JA |
195 | } |
196 | ||
40aabb67 OS |
197 | void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
198 | unsigned int tag) | |
320ae51f | 199 | { |
0d2602ca JA |
200 | struct blk_mq_tags *tags = hctx->tags; |
201 | ||
4bb659b1 JA |
202 | if (tag >= tags->nr_reserved_tags) { |
203 | const int real_tag = tag - tags->nr_reserved_tags; | |
204 | ||
70114c39 | 205 | BUG_ON(real_tag >= tags->nr_tags); |
f4a644db | 206 | sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu); |
70114c39 JA |
207 | } else { |
208 | BUG_ON(tag >= tags->nr_reserved_tags); | |
f4a644db | 209 | sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu); |
70114c39 | 210 | } |
320ae51f JA |
211 | } |
212 | ||
88459642 OS |
213 | struct bt_iter_data { |
214 | struct blk_mq_hw_ctx *hctx; | |
215 | busy_iter_fn *fn; | |
216 | void *data; | |
217 | bool reserved; | |
218 | }; | |
219 | ||
220 | static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) | |
320ae51f | 221 | { |
88459642 OS |
222 | 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; | |
81481eb4 | 226 | struct request *rq; |
4bb659b1 | 227 | |
88459642 OS |
228 | if (!reserved) |
229 | bitnr += tags->nr_reserved_tags; | |
230 | rq = tags->rqs[bitnr]; | |
4bb659b1 | 231 | |
88459642 OS |
232 | if (rq->q == hctx->queue) |
233 | iter_data->fn(hctx, rq, iter_data->data, reserved); | |
234 | return true; | |
235 | } | |
4bb659b1 | 236 | |
88459642 OS |
237 | static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt, |
238 | busy_iter_fn *fn, void *data, bool reserved) | |
239 | { | |
240 | 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); | |
320ae51f JA |
248 | } |
249 | ||
88459642 OS |
250 | struct bt_tags_iter_data { |
251 | struct blk_mq_tags *tags; | |
252 | busy_tag_iter_fn *fn; | |
253 | void *data; | |
254 | bool reserved; | |
255 | }; | |
256 | ||
257 | static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) | |
f26cdc85 | 258 | { |
88459642 OS |
259 | struct bt_tags_iter_data *iter_data = data; |
260 | struct blk_mq_tags *tags = iter_data->tags; | |
261 | bool reserved = iter_data->reserved; | |
f26cdc85 | 262 | struct request *rq; |
f26cdc85 | 263 | |
88459642 OS |
264 | if (!reserved) |
265 | bitnr += tags->nr_reserved_tags; | |
266 | rq = tags->rqs[bitnr]; | |
f26cdc85 | 267 | |
88459642 OS |
268 | iter_data->fn(rq, iter_data->data, reserved); |
269 | return true; | |
270 | } | |
271 | ||
272 | static 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); | |
f26cdc85 KB |
284 | } |
285 | ||
e8f1e163 SG |
286 | static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, |
287 | busy_tag_iter_fn *fn, void *priv) | |
f26cdc85 KB |
288 | { |
289 | if (tags->nr_reserved_tags) | |
88459642 OS |
290 | bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true); |
291 | bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false); | |
f26cdc85 | 292 | } |
f26cdc85 | 293 | |
e0489487 SG |
294 | void 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 | } | |
304 | EXPORT_SYMBOL(blk_mq_tagset_busy_iter); | |
305 | ||
486cf989 SG |
306 | int 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 | ||
327 | out: | |
328 | return ret; | |
329 | } | |
330 | EXPORT_SYMBOL_GPL(blk_mq_reinit_tagset); | |
331 | ||
0bf6cd5b | 332 | void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn, |
81481eb4 | 333 | void *priv) |
320ae51f | 334 | { |
0bf6cd5b CH |
335 | struct blk_mq_hw_ctx *hctx; |
336 | int i; | |
337 | ||
338 | ||
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) | |
88459642 OS |
350 | bt_for_each(hctx, &tags->breserved_tags, fn, priv, true); |
351 | bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false); | |
4bb659b1 JA |
352 | } |
353 | ||
4bb659b1 JA |
354 | } |
355 | ||
88459642 | 356 | static unsigned int bt_unused_tags(const struct sbitmap_queue *bt) |
e3a2b3f9 | 357 | { |
88459642 | 358 | return bt->sb.depth - sbitmap_weight(&bt->sb); |
e3a2b3f9 JA |
359 | } |
360 | ||
f4a644db OS |
361 | static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth, |
362 | bool round_robin, int node) | |
4bb659b1 | 363 | { |
f4a644db OS |
364 | return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL, |
365 | node); | |
4bb659b1 JA |
366 | } |
367 | ||
368 | static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags, | |
24391c0d | 369 | int node, int alloc_policy) |
4bb659b1 JA |
370 | { |
371 | unsigned int depth = tags->nr_tags - tags->nr_reserved_tags; | |
f4a644db | 372 | bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR; |
4bb659b1 | 373 | |
f4a644db | 374 | if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node)) |
88459642 | 375 | goto free_tags; |
f4a644db OS |
376 | if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, round_robin, |
377 | node)) | |
88459642 | 378 | goto free_bitmap_tags; |
4bb659b1 JA |
379 | |
380 | return tags; | |
88459642 OS |
381 | free_bitmap_tags: |
382 | sbitmap_queue_free(&tags->bitmap_tags); | |
383 | free_tags: | |
4bb659b1 JA |
384 | kfree(tags); |
385 | return NULL; | |
386 | } | |
387 | ||
320ae51f | 388 | struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags, |
24391c0d SL |
389 | unsigned int reserved_tags, |
390 | int node, int alloc_policy) | |
320ae51f | 391 | { |
320ae51f | 392 | struct blk_mq_tags *tags; |
320ae51f JA |
393 | |
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 | ||
f26cdc85 KB |
403 | if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) { |
404 | kfree(tags); | |
405 | return NULL; | |
406 | } | |
407 | ||
320ae51f JA |
408 | tags->nr_tags = total_tags; |
409 | tags->nr_reserved_tags = reserved_tags; | |
320ae51f | 410 | |
24391c0d | 411 | return blk_mq_init_bitmap_tags(tags, node, alloc_policy); |
320ae51f JA |
412 | } |
413 | ||
414 | void blk_mq_free_tags(struct blk_mq_tags *tags) | |
415 | { | |
88459642 OS |
416 | sbitmap_queue_free(&tags->bitmap_tags); |
417 | sbitmap_queue_free(&tags->breserved_tags); | |
f42d79ab | 418 | free_cpumask_var(tags->cpumask); |
320ae51f JA |
419 | kfree(tags); |
420 | } | |
421 | ||
4bb659b1 JA |
422 | void 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 | ||
9d3d21ae | 426 | *tag = prandom_u32() % depth; |
4bb659b1 JA |
427 | } |
428 | ||
e3a2b3f9 JA |
429 | int 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 | */ | |
88459642 OS |
439 | sbitmap_queue_resize(&tags->bitmap_tags, tdepth); |
440 | ||
aed3ea94 | 441 | blk_mq_tag_wakeup_all(tags, false); |
e3a2b3f9 JA |
442 | return 0; |
443 | } | |
444 | ||
205fb5f5 BVA |
445 | /** |
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 | */ | |
457 | u32 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 | } | |
471 | EXPORT_SYMBOL(blk_mq_unique_tag); | |
472 | ||
320ae51f JA |
473 | ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page) |
474 | { | |
475 | char *orig_page = page; | |
4bb659b1 | 476 | unsigned int free, res; |
320ae51f JA |
477 | |
478 | if (!tags) | |
479 | return 0; | |
480 | ||
59d13bf5 JA |
481 | page += sprintf(page, "nr_tags=%u, reserved_tags=%u, " |
482 | "bits_per_word=%u\n", | |
483 | tags->nr_tags, tags->nr_reserved_tags, | |
88459642 | 484 | 1U << tags->bitmap_tags.sb.shift); |
320ae51f | 485 | |
4bb659b1 JA |
486 | free = bt_unused_tags(&tags->bitmap_tags); |
487 | res = bt_unused_tags(&tags->breserved_tags); | |
320ae51f | 488 | |
4bb659b1 | 489 | page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res); |
0d2602ca | 490 | page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues)); |
320ae51f JA |
491 | |
492 | return page - orig_page; | |
493 | } |