1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/random.h>
5 #include <linux/blk-mq.h>
8 #include "blk-mq-tag.h"
10 void blk_mq_wait_for_tags(struct blk_mq_hw_ctx *hctx, bool reserved)
14 tag = blk_mq_get_tag(hctx, &zero, __GFP_WAIT, reserved);
15 blk_mq_put_tag(hctx, tag, &zero);
18 static bool bt_has_free_tags(struct blk_mq_bitmap_tags *bt)
22 for (i = 0; i < bt->map_nr; i++) {
23 struct blk_align_bitmap *bm = &bt->map[i];
26 ret = find_first_zero_bit(&bm->word, bm->depth);
34 bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
39 return bt_has_free_tags(&tags->bitmap_tags);
42 static inline void bt_index_inc(unsigned int *index)
44 *index = (*index + 1) & (BT_WAIT_QUEUES - 1);
48 * If a previously inactive queue goes active, bump the active user count.
50 bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
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);
60 * If a previously busy queue goes inactive, potential waiters could now
61 * be allowed to queue. Wake them up and check.
63 void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
65 struct blk_mq_tags *tags = hctx->tags;
66 struct blk_mq_bitmap_tags *bt;
69 if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
72 atomic_dec(&tags->active_queues);
75 * Will only throttle depth on non-reserved tags
77 bt = &tags->bitmap_tags;
78 wake_index = bt->wake_index;
79 for (i = 0; i < BT_WAIT_QUEUES; i++) {
80 struct bt_wait_state *bs = &bt->bs[wake_index];
82 if (waitqueue_active(&bs->wait))
85 bt_index_inc(&wake_index);
90 * For shared tag users, we track the number of currently active users
91 * and attempt to provide a fair share of the tag depth for each of them.
93 static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
94 struct blk_mq_bitmap_tags *bt)
96 unsigned int depth, users;
98 if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
100 if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
104 * Don't try dividing an ant
109 users = atomic_read(&hctx->tags->active_queues);
114 * Allow at least some tags
116 depth = max((bt->depth + users - 1) / users, 4U);
117 return atomic_read(&hctx->nr_active) < depth;
120 static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag)
122 int tag, org_last_tag, end;
124 org_last_tag = last_tag;
128 tag = find_next_zero_bit(&bm->word, end, last_tag);
129 if (unlikely(tag >= end)) {
131 * We started with an offset, start from 0 to
134 if (org_last_tag && last_tag) {
142 } while (test_and_set_bit_lock(tag, &bm->word));
148 * Straight forward bitmap tag implementation, where each bit is a tag
149 * (cleared == free, and set == busy). The small twist is using per-cpu
150 * last_tag caches, which blk-mq stores in the blk_mq_ctx software queue
151 * contexts. This enables us to drastically limit the space searched,
152 * without dirtying an extra shared cacheline like we would if we stored
153 * the cache value inside the shared blk_mq_bitmap_tags structure. On top
154 * of that, each word of tags is in a separate cacheline. This means that
155 * multiple users will tend to stick to different cachelines, at least
156 * until the map is exhausted.
158 static int __bt_get(struct blk_mq_hw_ctx *hctx, struct blk_mq_bitmap_tags *bt,
159 unsigned int *tag_cache)
161 unsigned int last_tag, org_last_tag;
164 if (!hctx_may_queue(hctx, bt))
167 last_tag = org_last_tag = *tag_cache;
168 index = TAG_TO_INDEX(bt, last_tag);
170 for (i = 0; i < bt->map_nr; i++) {
171 tag = __bt_get_word(&bt->map[index], TAG_TO_BIT(bt, last_tag));
173 tag += (index << bt->bits_per_word);
178 if (++index >= bt->map_nr)
186 * Only update the cache from the allocation path, if we ended
187 * up using the specific cached tag.
190 if (tag == org_last_tag) {
192 if (last_tag >= bt->depth - 1)
195 *tag_cache = last_tag;
201 static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
202 struct blk_mq_hw_ctx *hctx)
204 struct bt_wait_state *bs;
209 bs = &bt->bs[hctx->wait_index];
210 bt_index_inc(&hctx->wait_index);
214 static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
215 unsigned int *last_tag, gfp_t gfp)
217 struct bt_wait_state *bs;
221 tag = __bt_get(hctx, bt, last_tag);
225 if (!(gfp & __GFP_WAIT))
228 bs = bt_wait_ptr(bt, hctx);
232 was_empty = list_empty(&wait.task_list);
233 prepare_to_wait(&bs->wait, &wait, TASK_UNINTERRUPTIBLE);
235 tag = __bt_get(hctx, bt, last_tag);
240 atomic_set(&bs->wait_cnt, bt->wake_cnt);
245 finish_wait(&bs->wait, &wait);
249 static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags,
250 struct blk_mq_hw_ctx *hctx,
251 unsigned int *last_tag, gfp_t gfp)
255 tag = bt_get(&tags->bitmap_tags, hctx, last_tag, gfp);
257 return tag + tags->nr_reserved_tags;
259 return BLK_MQ_TAG_FAIL;
262 static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
267 if (unlikely(!tags->nr_reserved_tags)) {
269 return BLK_MQ_TAG_FAIL;
272 tag = bt_get(&tags->breserved_tags, NULL, &zero, gfp);
274 return BLK_MQ_TAG_FAIL;
279 unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag,
280 gfp_t gfp, bool reserved)
283 return __blk_mq_get_tag(hctx->tags, hctx, last_tag, gfp);
285 return __blk_mq_get_reserved_tag(hctx->tags, gfp);
288 static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)
292 wake_index = bt->wake_index;
293 for (i = 0; i < BT_WAIT_QUEUES; i++) {
294 struct bt_wait_state *bs = &bt->bs[wake_index];
296 if (waitqueue_active(&bs->wait)) {
297 if (wake_index != bt->wake_index)
298 bt->wake_index = wake_index;
303 bt_index_inc(&wake_index);
309 static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
311 const int index = TAG_TO_INDEX(bt, tag);
312 struct bt_wait_state *bs;
315 * The unlock memory barrier need to order access to req in free
316 * path and clearing tag bit
318 clear_bit_unlock(TAG_TO_BIT(bt, tag), &bt->map[index].word);
320 bs = bt_wake_ptr(bt);
321 if (bs && atomic_dec_and_test(&bs->wait_cnt)) {
322 atomic_set(&bs->wait_cnt, bt->wake_cnt);
323 bt_index_inc(&bt->wake_index);
328 static void __blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag)
330 BUG_ON(tag >= tags->nr_tags);
332 bt_clear_tag(&tags->bitmap_tags, tag);
335 static void __blk_mq_put_reserved_tag(struct blk_mq_tags *tags,
338 BUG_ON(tag >= tags->nr_reserved_tags);
340 bt_clear_tag(&tags->breserved_tags, tag);
343 void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
344 unsigned int *last_tag)
346 struct blk_mq_tags *tags = hctx->tags;
348 if (tag >= tags->nr_reserved_tags) {
349 const int real_tag = tag - tags->nr_reserved_tags;
351 __blk_mq_put_tag(tags, real_tag);
352 *last_tag = real_tag;
354 __blk_mq_put_reserved_tag(tags, tag);
357 static void bt_for_each_free(struct blk_mq_bitmap_tags *bt,
358 unsigned long *free_map, unsigned int off)
362 for (i = 0; i < bt->map_nr; i++) {
363 struct blk_align_bitmap *bm = &bt->map[i];
367 bit = find_next_zero_bit(&bm->word, bm->depth, bit);
368 if (bit >= bm->depth)
371 __set_bit(bit + off, free_map);
375 off += (1 << bt->bits_per_word);
379 void blk_mq_tag_busy_iter(struct blk_mq_tags *tags,
380 void (*fn)(void *, unsigned long *), void *data)
382 unsigned long *tag_map;
385 map_size = ALIGN(tags->nr_tags, BITS_PER_LONG) / BITS_PER_LONG;
386 tag_map = kzalloc(map_size * sizeof(unsigned long), GFP_ATOMIC);
390 bt_for_each_free(&tags->bitmap_tags, tag_map, tags->nr_reserved_tags);
391 if (tags->nr_reserved_tags)
392 bt_for_each_free(&tags->breserved_tags, tag_map, 0);
398 static unsigned int bt_unused_tags(struct blk_mq_bitmap_tags *bt)
400 unsigned int i, used;
402 for (i = 0, used = 0; i < bt->map_nr; i++) {
403 struct blk_align_bitmap *bm = &bt->map[i];
405 used += bitmap_weight(&bm->word, bm->depth);
408 return bt->depth - used;
411 static int bt_alloc(struct blk_mq_bitmap_tags *bt, unsigned int depth,
412 int node, bool reserved)
416 bt->bits_per_word = ilog2(BITS_PER_LONG);
419 * Depth can be zero for reserved tags, that's not a failure
423 unsigned int nr, i, map_depth, tags_per_word;
425 tags_per_word = (1 << bt->bits_per_word);
428 * If the tag space is small, shrink the number of tags
429 * per word so we spread over a few cachelines, at least.
430 * If less than 4 tags, just forget about it, it's not
431 * going to work optimally anyway.
434 while (tags_per_word * 4 > depth) {
436 tags_per_word = (1 << bt->bits_per_word);
440 nr = ALIGN(depth, tags_per_word) / tags_per_word;
441 bt->map = kzalloc_node(nr * sizeof(struct blk_align_bitmap),
448 for (i = 0; i < nr; i++) {
449 bt->map[i].depth = min(map_depth, tags_per_word);
450 map_depth -= tags_per_word;
454 bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
460 for (i = 0; i < BT_WAIT_QUEUES; i++)
461 init_waitqueue_head(&bt->bs[i].wait);
463 bt->wake_cnt = BT_WAIT_BATCH;
464 if (bt->wake_cnt > depth / 4)
465 bt->wake_cnt = max(1U, depth / 4);
471 static void bt_free(struct blk_mq_bitmap_tags *bt)
477 static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
480 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
482 if (bt_alloc(&tags->bitmap_tags, depth, node, false))
484 if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node, true))
489 bt_free(&tags->bitmap_tags);
494 struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
495 unsigned int reserved_tags, int node)
497 struct blk_mq_tags *tags;
499 if (total_tags > BLK_MQ_TAG_MAX) {
500 pr_err("blk-mq: tag depth too large\n");
504 tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
508 tags->nr_tags = total_tags;
509 tags->nr_reserved_tags = reserved_tags;
511 return blk_mq_init_bitmap_tags(tags, node);
514 void blk_mq_free_tags(struct blk_mq_tags *tags)
516 bt_free(&tags->bitmap_tags);
517 bt_free(&tags->breserved_tags);
521 void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
523 unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
525 *tag = prandom_u32() % depth;
528 ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
530 char *orig_page = page;
531 unsigned int free, res;
536 page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
537 "bits_per_word=%u\n",
538 tags->nr_tags, tags->nr_reserved_tags,
539 tags->bitmap_tags.bits_per_word);
541 free = bt_unused_tags(&tags->bitmap_tags);
542 res = bt_unused_tags(&tags->breserved_tags);
544 page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
545 page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
547 return page - orig_page;