blob: f6dea968b710bee95c56d0e06b8ddfc49cb0e330 [file] [log] [blame]
Jens Axboe320ae512013-10-24 09:20:05 +01001#include <linux/kernel.h>
2#include <linux/module.h>
Jens Axboe4bb659b2014-05-09 09:36:49 -06003#include <linux/random.h>
Jens Axboe320ae512013-10-24 09:20:05 +01004
5#include <linux/blk-mq.h>
6#include "blk.h"
7#include "blk-mq.h"
8#include "blk-mq-tag.h"
9
Jens Axboe0d2602c2014-05-13 15:10:52 -060010void blk_mq_wait_for_tags(struct blk_mq_hw_ctx *hctx, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +010011{
Jens Axboe4bb659b2014-05-09 09:36:49 -060012 int tag, zero = 0;
13
Jens Axboe0d2602c2014-05-13 15:10:52 -060014 tag = blk_mq_get_tag(hctx, &zero, __GFP_WAIT, reserved);
15 blk_mq_put_tag(hctx, tag, &zero);
Jens Axboe4bb659b2014-05-09 09:36:49 -060016}
17
18static bool bt_has_free_tags(struct blk_mq_bitmap_tags *bt)
19{
20 int i;
21
22 for (i = 0; i < bt->map_nr; i++) {
Jens Axboee93ecf62014-05-19 09:17:48 -060023 struct blk_align_bitmap *bm = &bt->map[i];
Jens Axboe4bb659b2014-05-09 09:36:49 -060024 int ret;
25
26 ret = find_first_zero_bit(&bm->word, bm->depth);
27 if (ret < bm->depth)
28 return true;
29 }
30
31 return false;
Jens Axboe320ae512013-10-24 09:20:05 +010032}
33
34bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
35{
Jens Axboe4bb659b2014-05-09 09:36:49 -060036 if (!tags)
37 return true;
38
39 return bt_has_free_tags(&tags->bitmap_tags);
Jens Axboe320ae512013-10-24 09:20:05 +010040}
41
Jens Axboe0d2602c2014-05-13 15:10:52 -060042static inline void bt_index_inc(unsigned int *index)
43{
44 *index = (*index + 1) & (BT_WAIT_QUEUES - 1);
45}
46
47/*
48 * If a previously inactive queue goes active, bump the active user count.
49 */
50bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
51{
52 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
53 !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
54 atomic_inc(&hctx->tags->active_queues);
55
56 return true;
57}
58
59/*
Jens Axboee3a2b3f2014-05-20 11:49:02 -060060 * Wakeup all potentially sleeping on normal (non-reserved) tags
Jens Axboe0d2602c2014-05-13 15:10:52 -060061 */
Jens Axboee3a2b3f2014-05-20 11:49:02 -060062static void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags)
Jens Axboe0d2602c2014-05-13 15:10:52 -060063{
Jens Axboe0d2602c2014-05-13 15:10:52 -060064 struct blk_mq_bitmap_tags *bt;
65 int i, wake_index;
66
Jens Axboe0d2602c2014-05-13 15:10:52 -060067 bt = &tags->bitmap_tags;
68 wake_index = bt->wake_index;
69 for (i = 0; i < BT_WAIT_QUEUES; i++) {
70 struct bt_wait_state *bs = &bt->bs[wake_index];
71
72 if (waitqueue_active(&bs->wait))
73 wake_up(&bs->wait);
74
75 bt_index_inc(&wake_index);
76 }
77}
78
79/*
Jens Axboee3a2b3f2014-05-20 11:49:02 -060080 * If a previously busy queue goes inactive, potential waiters could now
81 * be allowed to queue. Wake them up and check.
82 */
83void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
84{
85 struct blk_mq_tags *tags = hctx->tags;
86
87 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
88 return;
89
90 atomic_dec(&tags->active_queues);
91
92 blk_mq_tag_wakeup_all(tags);
93}
94
95/*
Jens Axboe0d2602c2014-05-13 15:10:52 -060096 * For shared tag users, we track the number of currently active users
97 * and attempt to provide a fair share of the tag depth for each of them.
98 */
99static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
100 struct blk_mq_bitmap_tags *bt)
101{
102 unsigned int depth, users;
103
104 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
105 return true;
106 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
107 return true;
108
109 /*
110 * Don't try dividing an ant
111 */
112 if (bt->depth == 1)
113 return true;
114
115 users = atomic_read(&hctx->tags->active_queues);
116 if (!users)
117 return true;
118
119 /*
120 * Allow at least some tags
121 */
122 depth = max((bt->depth + users - 1) / users, 4U);
123 return atomic_read(&hctx->nr_active) < depth;
124}
125
Jens Axboee93ecf62014-05-19 09:17:48 -0600126static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600127{
128 int tag, org_last_tag, end;
129
Jens Axboe59d13bf2014-05-09 13:41:15 -0600130 org_last_tag = last_tag;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600131 end = bm->depth;
132 do {
133restart:
134 tag = find_next_zero_bit(&bm->word, end, last_tag);
135 if (unlikely(tag >= end)) {
136 /*
137 * We started with an offset, start from 0 to
138 * exhaust the map.
139 */
140 if (org_last_tag && last_tag) {
141 end = last_tag;
142 last_tag = 0;
143 goto restart;
144 }
145 return -1;
146 }
147 last_tag = tag + 1;
148 } while (test_and_set_bit_lock(tag, &bm->word));
149
150 return tag;
151}
152
153/*
154 * Straight forward bitmap tag implementation, where each bit is a tag
155 * (cleared == free, and set == busy). The small twist is using per-cpu
156 * last_tag caches, which blk-mq stores in the blk_mq_ctx software queue
157 * contexts. This enables us to drastically limit the space searched,
158 * without dirtying an extra shared cacheline like we would if we stored
159 * the cache value inside the shared blk_mq_bitmap_tags structure. On top
160 * of that, each word of tags is in a separate cacheline. This means that
161 * multiple users will tend to stick to different cachelines, at least
162 * until the map is exhausted.
163 */
Jens Axboe0d2602c2014-05-13 15:10:52 -0600164static int __bt_get(struct blk_mq_hw_ctx *hctx, struct blk_mq_bitmap_tags *bt,
165 unsigned int *tag_cache)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600166{
167 unsigned int last_tag, org_last_tag;
168 int index, i, tag;
169
Jens Axboe0d2602c2014-05-13 15:10:52 -0600170 if (!hctx_may_queue(hctx, bt))
171 return -1;
172
Jens Axboe4bb659b2014-05-09 09:36:49 -0600173 last_tag = org_last_tag = *tag_cache;
Jens Axboe59d13bf2014-05-09 13:41:15 -0600174 index = TAG_TO_INDEX(bt, last_tag);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600175
176 for (i = 0; i < bt->map_nr; i++) {
Jens Axboe59d13bf2014-05-09 13:41:15 -0600177 tag = __bt_get_word(&bt->map[index], TAG_TO_BIT(bt, last_tag));
Jens Axboe4bb659b2014-05-09 09:36:49 -0600178 if (tag != -1) {
Jens Axboe59d13bf2014-05-09 13:41:15 -0600179 tag += (index << bt->bits_per_word);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600180 goto done;
181 }
182
183 last_tag = 0;
184 if (++index >= bt->map_nr)
185 index = 0;
186 }
187
188 *tag_cache = 0;
189 return -1;
190
191 /*
192 * Only update the cache from the allocation path, if we ended
193 * up using the specific cached tag.
194 */
195done:
196 if (tag == org_last_tag) {
197 last_tag = tag + 1;
198 if (last_tag >= bt->depth - 1)
199 last_tag = 0;
200
201 *tag_cache = last_tag;
202 }
203
204 return tag;
205}
206
Jens Axboe4bb659b2014-05-09 09:36:49 -0600207static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
208 struct blk_mq_hw_ctx *hctx)
209{
210 struct bt_wait_state *bs;
211
212 if (!hctx)
213 return &bt->bs[0];
214
215 bs = &bt->bs[hctx->wait_index];
216 bt_index_inc(&hctx->wait_index);
217 return bs;
218}
219
220static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
221 unsigned int *last_tag, gfp_t gfp)
222{
223 struct bt_wait_state *bs;
224 DEFINE_WAIT(wait);
225 int tag;
226
Jens Axboe0d2602c2014-05-13 15:10:52 -0600227 tag = __bt_get(hctx, bt, last_tag);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600228 if (tag != -1)
229 return tag;
230
231 if (!(gfp & __GFP_WAIT))
232 return -1;
233
234 bs = bt_wait_ptr(bt, hctx);
235 do {
236 bool was_empty;
237
238 was_empty = list_empty(&wait.task_list);
239 prepare_to_wait(&bs->wait, &wait, TASK_UNINTERRUPTIBLE);
240
Jens Axboe0d2602c2014-05-13 15:10:52 -0600241 tag = __bt_get(hctx, bt, last_tag);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600242 if (tag != -1)
243 break;
244
245 if (was_empty)
246 atomic_set(&bs->wait_cnt, bt->wake_cnt);
247
248 io_schedule();
249 } while (1);
250
251 finish_wait(&bs->wait, &wait);
252 return tag;
253}
254
255static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags,
256 struct blk_mq_hw_ctx *hctx,
257 unsigned int *last_tag, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100258{
259 int tag;
260
Jens Axboe4bb659b2014-05-09 09:36:49 -0600261 tag = bt_get(&tags->bitmap_tags, hctx, last_tag, gfp);
262 if (tag >= 0)
263 return tag + tags->nr_reserved_tags;
264
265 return BLK_MQ_TAG_FAIL;
Jens Axboe320ae512013-10-24 09:20:05 +0100266}
267
268static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
269 gfp_t gfp)
270{
Jens Axboe4bb659b2014-05-09 09:36:49 -0600271 int tag, zero = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100272
273 if (unlikely(!tags->nr_reserved_tags)) {
274 WARN_ON_ONCE(1);
275 return BLK_MQ_TAG_FAIL;
276 }
277
Jens Axboe4bb659b2014-05-09 09:36:49 -0600278 tag = bt_get(&tags->breserved_tags, NULL, &zero, gfp);
Jens Axboe320ae512013-10-24 09:20:05 +0100279 if (tag < 0)
280 return BLK_MQ_TAG_FAIL;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600281
Jens Axboe320ae512013-10-24 09:20:05 +0100282 return tag;
283}
284
Jens Axboe0d2602c2014-05-13 15:10:52 -0600285unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag,
Jens Axboe4bb659b2014-05-09 09:36:49 -0600286 gfp_t gfp, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100287{
288 if (!reserved)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600289 return __blk_mq_get_tag(hctx->tags, hctx, last_tag, gfp);
Jens Axboe320ae512013-10-24 09:20:05 +0100290
Jens Axboe0d2602c2014-05-13 15:10:52 -0600291 return __blk_mq_get_reserved_tag(hctx->tags, gfp);
Jens Axboe320ae512013-10-24 09:20:05 +0100292}
293
Jens Axboe4bb659b2014-05-09 09:36:49 -0600294static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)
295{
296 int i, wake_index;
297
298 wake_index = bt->wake_index;
299 for (i = 0; i < BT_WAIT_QUEUES; i++) {
300 struct bt_wait_state *bs = &bt->bs[wake_index];
301
302 if (waitqueue_active(&bs->wait)) {
303 if (wake_index != bt->wake_index)
304 bt->wake_index = wake_index;
305
306 return bs;
307 }
308
309 bt_index_inc(&wake_index);
310 }
311
312 return NULL;
313}
314
315static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
316{
Jens Axboe59d13bf2014-05-09 13:41:15 -0600317 const int index = TAG_TO_INDEX(bt, tag);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600318 struct bt_wait_state *bs;
319
Ming Lei0289b2e2014-05-11 01:01:48 +0800320 /*
321 * The unlock memory barrier need to order access to req in free
322 * path and clearing tag bit
323 */
324 clear_bit_unlock(TAG_TO_BIT(bt, tag), &bt->map[index].word);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600325
326 bs = bt_wake_ptr(bt);
327 if (bs && atomic_dec_and_test(&bs->wait_cnt)) {
Jens Axboe4bb659b2014-05-09 09:36:49 -0600328 atomic_set(&bs->wait_cnt, bt->wake_cnt);
329 bt_index_inc(&bt->wake_index);
330 wake_up(&bs->wait);
331 }
332}
333
Jens Axboe320ae512013-10-24 09:20:05 +0100334static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
335{
336 BUG_ON(tag >= tags->nr_tags);
337
Jens Axboe4bb659b2014-05-09 09:36:49 -0600338 bt_clear_tag(&tags->bitmap_tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100339}
340
341static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags,
342 unsigned int tag)
343{
344 BUG_ON(tag >= tags->nr_reserved_tags);
345
Jens Axboe4bb659b2014-05-09 09:36:49 -0600346 bt_clear_tag(&tags->breserved_tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100347}
348
Jens Axboe0d2602c2014-05-13 15:10:52 -0600349void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
Jens Axboe4bb659b2014-05-09 09:36:49 -0600350 unsigned int *last_tag)
Jens Axboe320ae512013-10-24 09:20:05 +0100351{
Jens Axboe0d2602c2014-05-13 15:10:52 -0600352 struct blk_mq_tags *tags = hctx->tags;
353
Jens Axboe4bb659b2014-05-09 09:36:49 -0600354 if (tag >= tags->nr_reserved_tags) {
355 const int real_tag = tag - tags->nr_reserved_tags;
356
357 __blk_mq_put_tag(tags, real_tag);
358 *last_tag = real_tag;
359 } else
Jens Axboe320ae512013-10-24 09:20:05 +0100360 __blk_mq_put_reserved_tag(tags, tag);
361}
362
Jens Axboe4bb659b2014-05-09 09:36:49 -0600363static void bt_for_each_free(struct blk_mq_bitmap_tags *bt,
364 unsigned long *free_map, unsigned int off)
Jens Axboe320ae512013-10-24 09:20:05 +0100365{
Jens Axboe4bb659b2014-05-09 09:36:49 -0600366 int i;
367
368 for (i = 0; i < bt->map_nr; i++) {
Jens Axboee93ecf62014-05-19 09:17:48 -0600369 struct blk_align_bitmap *bm = &bt->map[i];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600370 int bit = 0;
371
372 do {
373 bit = find_next_zero_bit(&bm->word, bm->depth, bit);
374 if (bit >= bm->depth)
375 break;
376
377 __set_bit(bit + off, free_map);
378 bit++;
379 } while (1);
380
Jens Axboe59d13bf2014-05-09 13:41:15 -0600381 off += (1 << bt->bits_per_word);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600382 }
Jens Axboe320ae512013-10-24 09:20:05 +0100383}
384
385void blk_mq_tag_busy_iter(struct blk_mq_tags *tags,
386 void (*fn)(void *, unsigned long *), void *data)
387{
388 unsigned long *tag_map;
389 size_t map_size;
390
391 map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG;
392 tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC);
393 if (!tag_map)
394 return;
395
Jens Axboe4bb659b2014-05-09 09:36:49 -0600396 bt_for_each_free(&tags->bitmap_tags, tag_map, tags->nr_reserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100397 if (tags->nr_reserved_tags)
Jens Axboe4bb659b2014-05-09 09:36:49 -0600398 bt_for_each_free(&tags->breserved_tags, tag_map, 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100399
400 fn(data, tag_map);
401 kfree(tag_map);
402}
403
Jens Axboe4bb659b2014-05-09 09:36:49 -0600404static unsigned int bt_unused_tags(struct blk_mq_bitmap_tags *bt)
405{
406 unsigned int i, used;
407
408 for (i = 0, used = 0; i < bt->map_nr; i++) {
Jens Axboee93ecf62014-05-19 09:17:48 -0600409 struct blk_align_bitmap *bm = &bt->map[i];
Jens Axboe4bb659b2014-05-09 09:36:49 -0600410
411 used += bitmap_weight(&bm->word, bm->depth);
412 }
413
414 return bt->depth - used;
415}
416
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600417static void bt_update_count(struct blk_mq_bitmap_tags *bt,
418 unsigned int depth)
419{
420 unsigned int tags_per_word = 1U << bt->bits_per_word;
421 unsigned int map_depth = depth;
422
423 if (depth) {
424 int i;
425
426 for (i = 0; i < bt->map_nr; i++) {
427 bt->map[i].depth = min(map_depth, tags_per_word);
428 map_depth -= bt->map[i].depth;
429 }
430 }
431
432 bt->wake_cnt = BT_WAIT_BATCH;
433 if (bt->wake_cnt > depth / 4)
434 bt->wake_cnt = max(1U, depth / 4);
435
436 bt->depth = depth;
437}
438
Jens Axboe4bb659b2014-05-09 09:36:49 -0600439static int bt_alloc(struct blk_mq_bitmap_tags *bt, unsigned int depth,
440 int node, bool reserved)
441{
442 int i;
443
Jens Axboe59d13bf2014-05-09 13:41:15 -0600444 bt->bits_per_word = ilog2(BITS_PER_LONG);
445
Jens Axboe4bb659b2014-05-09 09:36:49 -0600446 /*
447 * Depth can be zero for reserved tags, that's not a failure
448 * condition.
449 */
450 if (depth) {
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600451 unsigned int nr, tags_per_word;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600452
Jens Axboe59d13bf2014-05-09 13:41:15 -0600453 tags_per_word = (1 << bt->bits_per_word);
454
455 /*
456 * If the tag space is small, shrink the number of tags
457 * per word so we spread over a few cachelines, at least.
458 * If less than 4 tags, just forget about it, it's not
459 * going to work optimally anyway.
460 */
461 if (depth >= 4) {
462 while (tags_per_word * 4 > depth) {
463 bt->bits_per_word--;
464 tags_per_word = (1 << bt->bits_per_word);
465 }
466 }
467
468 nr = ALIGN(depth, tags_per_word) / tags_per_word;
Jens Axboee93ecf62014-05-19 09:17:48 -0600469 bt->map = kzalloc_node(nr * sizeof(struct blk_align_bitmap),
Jens Axboe4bb659b2014-05-09 09:36:49 -0600470 GFP_KERNEL, node);
471 if (!bt->map)
472 return -ENOMEM;
473
474 bt->map_nr = nr;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600475 }
476
477 bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
478 if (!bt->bs) {
479 kfree(bt->map);
480 return -ENOMEM;
481 }
482
483 for (i = 0; i < BT_WAIT_QUEUES; i++)
484 init_waitqueue_head(&bt->bs[i].wait);
485
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600486 bt_update_count(bt, depth);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600487 return 0;
488}
489
490static void bt_free(struct blk_mq_bitmap_tags *bt)
491{
492 kfree(bt->map);
493 kfree(bt->bs);
494}
495
496static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
497 int node)
498{
499 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
500
501 if (bt_alloc(&tags->bitmap_tags, depth, node, false))
502 goto enomem;
503 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node, true))
504 goto enomem;
505
506 return tags;
507enomem:
508 bt_free(&tags->bitmap_tags);
509 kfree(tags);
510 return NULL;
511}
512
Jens Axboe320ae512013-10-24 09:20:05 +0100513struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
514 unsigned int reserved_tags, int node)
515{
Jens Axboe320ae512013-10-24 09:20:05 +0100516 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100517
518 if (total_tags > BLK_MQ_TAG_MAX) {
519 pr_err("blk-mq: tag depth too large\n");
520 return NULL;
521 }
522
523 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
524 if (!tags)
525 return NULL;
526
Jens Axboe320ae512013-10-24 09:20:05 +0100527 tags->nr_tags = total_tags;
528 tags->nr_reserved_tags = reserved_tags;
Jens Axboe320ae512013-10-24 09:20:05 +0100529
Jens Axboe4bb659b2014-05-09 09:36:49 -0600530 return blk_mq_init_bitmap_tags(tags, node);
Jens Axboe320ae512013-10-24 09:20:05 +0100531}
532
533void blk_mq_free_tags(struct blk_mq_tags *tags)
534{
Jens Axboe4bb659b2014-05-09 09:36:49 -0600535 bt_free(&tags->bitmap_tags);
536 bt_free(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100537 kfree(tags);
538}
539
Jens Axboe4bb659b2014-05-09 09:36:49 -0600540void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
541{
542 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
543
Ming Lei9d3d21a2014-05-10 15:43:14 -0600544 *tag = prandom_u32() % depth;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600545}
546
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600547int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
548{
549 tdepth -= tags->nr_reserved_tags;
550 if (tdepth > tags->nr_tags)
551 return -EINVAL;
552
553 /*
554 * Don't need (or can't) update reserved tags here, they remain
555 * static and should never need resizing.
556 */
557 bt_update_count(&tags->bitmap_tags, tdepth);
558 blk_mq_tag_wakeup_all(tags);
559 return 0;
560}
561
Jens Axboe320ae512013-10-24 09:20:05 +0100562ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
563{
564 char *orig_page = page;
Jens Axboe4bb659b2014-05-09 09:36:49 -0600565 unsigned int free, res;
Jens Axboe320ae512013-10-24 09:20:05 +0100566
567 if (!tags)
568 return 0;
569
Jens Axboe59d13bf2014-05-09 13:41:15 -0600570 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
571 "bits_per_word=%u\n",
572 tags->nr_tags, tags->nr_reserved_tags,
573 tags->bitmap_tags.bits_per_word);
Jens Axboe320ae512013-10-24 09:20:05 +0100574
Jens Axboe4bb659b2014-05-09 09:36:49 -0600575 free = bt_unused_tags(&tags->bitmap_tags);
576 res = bt_unused_tags(&tags->breserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100577
Jens Axboe4bb659b2014-05-09 09:36:49 -0600578 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600579 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
Jens Axboe320ae512013-10-24 09:20:05 +0100580
581 return page - orig_page;
582}