blk-mq-tag.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * Fast and scalable bitmap tagging variant. Uses sparser bitmaps spread
  3. * over multiple cachelines to avoid ping-pong between multiple submitters
  4. * or submitter and completer. Uses rolling wakeups to avoid falling of
  5. * the scaling cliff when we run out of tags and have to start putting
  6. * submitters to sleep.
  7. *
  8. * Uses active queue tracking to support fairer distribution of tags
  9. * between multiple submitters when a shared tag map is used.
  10. *
  11. * Copyright (C) 2013-2014 Jens Axboe
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/random.h>
  16. #include <linux/blk-mq.h>
  17. #include "blk.h"
  18. #include "blk-mq.h"
  19. #include "blk-mq-tag.h"
  20. static bool bt_has_free_tags(struct blk_mq_bitmap_tags *bt)
  21. {
  22. int i;
  23. for (i = 0; i < bt->map_nr; i++) {
  24. struct blk_align_bitmap *bm = &bt->map[i];
  25. int ret;
  26. ret = find_first_zero_bit(&bm->word, bm->depth);
  27. if (ret < bm->depth)
  28. return true;
  29. }
  30. return false;
  31. }
  32. bool blk_mq_has_free_tags(struct blk_mq_tags *tags)
  33. {
  34. if (!tags)
  35. return true;
  36. return bt_has_free_tags(&tags->bitmap_tags);
  37. }
  38. static inline int bt_index_inc(int index)
  39. {
  40. return (index + 1) & (BT_WAIT_QUEUES - 1);
  41. }
  42. static inline void bt_index_atomic_inc(atomic_t *index)
  43. {
  44. int old = atomic_read(index);
  45. int new = bt_index_inc(old);
  46. atomic_cmpxchg(index, old, new);
  47. }
  48. /*
  49. * If a previously inactive queue goes active, bump the active user count.
  50. */
  51. bool __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
  52. {
  53. if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) &&
  54. !test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  55. atomic_inc(&hctx->tags->active_queues);
  56. return true;
  57. }
  58. /*
  59. * Wakeup all potentially sleeping on tags
  60. */
  61. void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
  62. {
  63. struct blk_mq_bitmap_tags *bt;
  64. int i, wake_index;
  65. /*
  66. * Make sure all changes prior to this are visible from other CPUs.
  67. */
  68. smp_mb();
  69. bt = &tags->bitmap_tags;
  70. wake_index = atomic_read(&bt->wake_index);
  71. for (i = 0; i < BT_WAIT_QUEUES; i++) {
  72. struct bt_wait_state *bs = &bt->bs[wake_index];
  73. if (waitqueue_active(&bs->wait))
  74. wake_up(&bs->wait);
  75. wake_index = bt_index_inc(wake_index);
  76. }
  77. if (include_reserve) {
  78. bt = &tags->breserved_tags;
  79. if (waitqueue_active(&bt->bs[0].wait))
  80. wake_up(&bt->bs[0].wait);
  81. }
  82. }
  83. /*
  84. * If a previously busy queue goes inactive, potential waiters could now
  85. * be allowed to queue. Wake them up and check.
  86. */
  87. void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
  88. {
  89. struct blk_mq_tags *tags = hctx->tags;
  90. if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  91. return;
  92. atomic_dec(&tags->active_queues);
  93. blk_mq_tag_wakeup_all(tags, false);
  94. }
  95. /*
  96. * 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. */
  99. static 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. if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_SHARED))
  104. return true;
  105. if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  106. return true;
  107. /*
  108. * Don't try dividing an ant
  109. */
  110. if (bt->depth == 1)
  111. return true;
  112. users = atomic_read(&hctx->tags->active_queues);
  113. if (!users)
  114. return true;
  115. /*
  116. * Allow at least some tags
  117. */
  118. depth = max((bt->depth + users - 1) / users, 4U);
  119. return atomic_read(&hctx->nr_active) < depth;
  120. }
  121. static int __bt_get_word(struct blk_align_bitmap *bm, unsigned int last_tag,
  122. bool nowrap)
  123. {
  124. int tag, org_last_tag = last_tag;
  125. while (1) {
  126. tag = find_next_zero_bit(&bm->word, bm->depth, last_tag);
  127. if (unlikely(tag >= bm->depth)) {
  128. /*
  129. * We started with an offset, and we didn't reset the
  130. * offset to 0 in a failure case, so start from 0 to
  131. * exhaust the map.
  132. */
  133. if (org_last_tag && last_tag && !nowrap) {
  134. last_tag = org_last_tag = 0;
  135. continue;
  136. }
  137. return -1;
  138. }
  139. if (!test_and_set_bit(tag, &bm->word))
  140. break;
  141. last_tag = tag + 1;
  142. if (last_tag >= bm->depth - 1)
  143. last_tag = 0;
  144. }
  145. return tag;
  146. }
  147. #define BT_ALLOC_RR(tags) (tags->alloc_policy == BLK_TAG_ALLOC_RR)
  148. /*
  149. * Straight forward bitmap tag implementation, where each bit is a tag
  150. * (cleared == free, and set == busy). The small twist is using per-cpu
  151. * last_tag caches, which blk-mq stores in the blk_mq_ctx software queue
  152. * contexts. This enables us to drastically limit the space searched,
  153. * without dirtying an extra shared cacheline like we would if we stored
  154. * the cache value inside the shared blk_mq_bitmap_tags structure. On top
  155. * of that, each word of tags is in a separate cacheline. This means that
  156. * multiple users will tend to stick to different cachelines, at least
  157. * until the map is exhausted.
  158. */
  159. static int __bt_get(struct blk_mq_hw_ctx *hctx, struct blk_mq_bitmap_tags *bt,
  160. unsigned int *tag_cache, struct blk_mq_tags *tags)
  161. {
  162. unsigned int last_tag, org_last_tag;
  163. int index, i, tag;
  164. if (!hctx_may_queue(hctx, bt))
  165. return -1;
  166. last_tag = org_last_tag = *tag_cache;
  167. index = TAG_TO_INDEX(bt, last_tag);
  168. for (i = 0; i < bt->map_nr; i++) {
  169. tag = __bt_get_word(&bt->map[index], TAG_TO_BIT(bt, last_tag),
  170. BT_ALLOC_RR(tags));
  171. if (tag != -1) {
  172. tag += (index << bt->bits_per_word);
  173. goto done;
  174. }
  175. /*
  176. * Jump to next index, and reset the last tag to be the
  177. * first tag of that index
  178. */
  179. index++;
  180. last_tag = (index << bt->bits_per_word);
  181. if (index >= bt->map_nr) {
  182. index = 0;
  183. last_tag = 0;
  184. }
  185. }
  186. *tag_cache = 0;
  187. return -1;
  188. /*
  189. * Only update the cache from the allocation path, if we ended
  190. * up using the specific cached tag.
  191. */
  192. done:
  193. if (tag == org_last_tag || unlikely(BT_ALLOC_RR(tags))) {
  194. last_tag = tag + 1;
  195. if (last_tag >= bt->depth - 1)
  196. last_tag = 0;
  197. *tag_cache = last_tag;
  198. }
  199. return tag;
  200. }
  201. static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
  202. struct blk_mq_hw_ctx *hctx)
  203. {
  204. struct bt_wait_state *bs;
  205. int wait_index;
  206. if (!hctx)
  207. return &bt->bs[0];
  208. wait_index = atomic_read(&hctx->wait_index);
  209. bs = &bt->bs[wait_index];
  210. bt_index_atomic_inc(&hctx->wait_index);
  211. return bs;
  212. }
  213. static int bt_get(struct blk_mq_alloc_data *data,
  214. struct blk_mq_bitmap_tags *bt,
  215. struct blk_mq_hw_ctx *hctx,
  216. unsigned int *last_tag, struct blk_mq_tags *tags)
  217. {
  218. struct bt_wait_state *bs;
  219. DEFINE_WAIT(wait);
  220. int tag;
  221. tag = __bt_get(hctx, bt, last_tag, tags);
  222. if (tag != -1)
  223. return tag;
  224. if (!gfpflags_allow_blocking(data->gfp))
  225. return -1;
  226. bs = bt_wait_ptr(bt, hctx);
  227. do {
  228. prepare_to_wait(&bs->wait, &wait, TASK_UNINTERRUPTIBLE);
  229. tag = __bt_get(hctx, bt, last_tag, tags);
  230. if (tag != -1)
  231. break;
  232. /*
  233. * We're out of tags on this hardware queue, kick any
  234. * pending IO submits before going to sleep waiting for
  235. * some to complete. Note that hctx can be NULL here for
  236. * reserved tag allocation.
  237. */
  238. if (hctx)
  239. blk_mq_run_hw_queue(hctx, false);
  240. /*
  241. * Retry tag allocation after running the hardware queue,
  242. * as running the queue may also have found completions.
  243. */
  244. tag = __bt_get(hctx, bt, last_tag, tags);
  245. if (tag != -1)
  246. break;
  247. blk_mq_put_ctx(data->ctx);
  248. io_schedule();
  249. data->ctx = blk_mq_get_ctx(data->q);
  250. data->hctx = data->q->mq_ops->map_queue(data->q,
  251. data->ctx->cpu);
  252. if (data->reserved) {
  253. bt = &data->hctx->tags->breserved_tags;
  254. } else {
  255. last_tag = &data->ctx->last_tag;
  256. hctx = data->hctx;
  257. bt = &hctx->tags->bitmap_tags;
  258. }
  259. finish_wait(&bs->wait, &wait);
  260. bs = bt_wait_ptr(bt, hctx);
  261. } while (1);
  262. finish_wait(&bs->wait, &wait);
  263. return tag;
  264. }
  265. static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
  266. {
  267. int tag;
  268. tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
  269. &data->ctx->last_tag, data->hctx->tags);
  270. if (tag >= 0)
  271. return tag + data->hctx->tags->nr_reserved_tags;
  272. return BLK_MQ_TAG_FAIL;
  273. }
  274. static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
  275. {
  276. int tag, zero = 0;
  277. if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
  278. WARN_ON_ONCE(1);
  279. return BLK_MQ_TAG_FAIL;
  280. }
  281. tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL, &zero,
  282. data->hctx->tags);
  283. if (tag < 0)
  284. return BLK_MQ_TAG_FAIL;
  285. return tag;
  286. }
  287. unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
  288. {
  289. if (!data->reserved)
  290. return __blk_mq_get_tag(data);
  291. return __blk_mq_get_reserved_tag(data);
  292. }
  293. static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)
  294. {
  295. int i, wake_index;
  296. wake_index = atomic_read(&bt->wake_index);
  297. for (i = 0; i < BT_WAIT_QUEUES; i++) {
  298. struct bt_wait_state *bs = &bt->bs[wake_index];
  299. if (waitqueue_active(&bs->wait)) {
  300. int o = atomic_read(&bt->wake_index);
  301. if (wake_index != o)
  302. atomic_cmpxchg(&bt->wake_index, o, wake_index);
  303. return bs;
  304. }
  305. wake_index = bt_index_inc(wake_index);
  306. }
  307. return NULL;
  308. }
  309. static void bt_clear_tag(struct blk_mq_bitmap_tags *bt, unsigned int tag)
  310. {
  311. const int index = TAG_TO_INDEX(bt, tag);
  312. struct bt_wait_state *bs;
  313. int wait_cnt;
  314. clear_bit(TAG_TO_BIT(bt, tag), &bt->map[index].word);
  315. /* Ensure that the wait list checks occur after clear_bit(). */
  316. smp_mb();
  317. bs = bt_wake_ptr(bt);
  318. if (!bs)
  319. return;
  320. wait_cnt = atomic_dec_return(&bs->wait_cnt);
  321. if (unlikely(wait_cnt < 0))
  322. wait_cnt = atomic_inc_return(&bs->wait_cnt);
  323. if (wait_cnt == 0) {
  324. atomic_add(bt->wake_cnt, &bs->wait_cnt);
  325. bt_index_atomic_inc(&bt->wake_index);
  326. wake_up(&bs->wait);
  327. }
  328. }
  329. void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag,
  330. unsigned int *last_tag)
  331. {
  332. struct blk_mq_tags *tags = hctx->tags;
  333. if (tag >= tags->nr_reserved_tags) {
  334. const int real_tag = tag - tags->nr_reserved_tags;
  335. BUG_ON(real_tag >= tags->nr_tags);
  336. bt_clear_tag(&tags->bitmap_tags, real_tag);
  337. if (likely(tags->alloc_policy == BLK_TAG_ALLOC_FIFO))
  338. *last_tag = real_tag;
  339. } else {
  340. BUG_ON(tag >= tags->nr_reserved_tags);
  341. bt_clear_tag(&tags->breserved_tags, tag);
  342. }
  343. }
  344. static void bt_for_each(struct blk_mq_hw_ctx *hctx,
  345. struct blk_mq_bitmap_tags *bt, unsigned int off,
  346. busy_iter_fn *fn, void *data, bool reserved)
  347. {
  348. struct request *rq;
  349. int bit, i;
  350. for (i = 0; i < bt->map_nr; i++) {
  351. struct blk_align_bitmap *bm = &bt->map[i];
  352. for (bit = find_first_bit(&bm->word, bm->depth);
  353. bit < bm->depth;
  354. bit = find_next_bit(&bm->word, bm->depth, bit + 1)) {
  355. rq = hctx->tags->rqs[off + bit];
  356. if (rq->q == hctx->queue)
  357. fn(hctx, rq, data, reserved);
  358. }
  359. off += (1 << bt->bits_per_word);
  360. }
  361. }
  362. static void bt_tags_for_each(struct blk_mq_tags *tags,
  363. struct blk_mq_bitmap_tags *bt, unsigned int off,
  364. busy_tag_iter_fn *fn, void *data, bool reserved)
  365. {
  366. struct request *rq;
  367. int bit, i;
  368. if (!tags->rqs)
  369. return;
  370. for (i = 0; i < bt->map_nr; i++) {
  371. struct blk_align_bitmap *bm = &bt->map[i];
  372. for (bit = find_first_bit(&bm->word, bm->depth);
  373. bit < bm->depth;
  374. bit = find_next_bit(&bm->word, bm->depth, bit + 1)) {
  375. rq = tags->rqs[off + bit];
  376. fn(rq, data, reserved);
  377. }
  378. off += (1 << bt->bits_per_word);
  379. }
  380. }
  381. void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
  382. void *priv)
  383. {
  384. if (tags->nr_reserved_tags)
  385. bt_tags_for_each(tags, &tags->breserved_tags, 0, fn, priv, true);
  386. bt_tags_for_each(tags, &tags->bitmap_tags, tags->nr_reserved_tags, fn, priv,
  387. false);
  388. }
  389. EXPORT_SYMBOL(blk_mq_all_tag_busy_iter);
  390. void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn,
  391. void *priv)
  392. {
  393. struct blk_mq_hw_ctx *hctx;
  394. int i;
  395. queue_for_each_hw_ctx(q, hctx, i) {
  396. struct blk_mq_tags *tags = hctx->tags;
  397. /*
  398. * If not software queues are currently mapped to this
  399. * hardware queue, there's nothing to check
  400. */
  401. if (!blk_mq_hw_queue_mapped(hctx))
  402. continue;
  403. if (tags->nr_reserved_tags)
  404. bt_for_each(hctx, &tags->breserved_tags, 0, fn, priv, true);
  405. bt_for_each(hctx, &tags->bitmap_tags, tags->nr_reserved_tags, fn, priv,
  406. false);
  407. }
  408. }
  409. static unsigned int bt_unused_tags(struct blk_mq_bitmap_tags *bt)
  410. {
  411. unsigned int i, used;
  412. for (i = 0, used = 0; i < bt->map_nr; i++) {
  413. struct blk_align_bitmap *bm = &bt->map[i];
  414. used += bitmap_weight(&bm->word, bm->depth);
  415. }
  416. return bt->depth - used;
  417. }
  418. static void bt_update_count(struct blk_mq_bitmap_tags *bt,
  419. unsigned int depth)
  420. {
  421. unsigned int tags_per_word = 1U << bt->bits_per_word;
  422. unsigned int map_depth = depth;
  423. if (depth) {
  424. int i;
  425. for (i = 0; i < bt->map_nr; i++) {
  426. bt->map[i].depth = min(map_depth, tags_per_word);
  427. map_depth -= bt->map[i].depth;
  428. }
  429. }
  430. bt->wake_cnt = BT_WAIT_BATCH;
  431. if (bt->wake_cnt > depth / BT_WAIT_QUEUES)
  432. bt->wake_cnt = max(1U, depth / BT_WAIT_QUEUES);
  433. bt->depth = depth;
  434. }
  435. static int bt_alloc(struct blk_mq_bitmap_tags *bt, unsigned int depth,
  436. int node, bool reserved)
  437. {
  438. int i;
  439. bt->bits_per_word = ilog2(BITS_PER_LONG);
  440. /*
  441. * Depth can be zero for reserved tags, that's not a failure
  442. * condition.
  443. */
  444. if (depth) {
  445. unsigned int nr, tags_per_word;
  446. tags_per_word = (1 << bt->bits_per_word);
  447. /*
  448. * If the tag space is small, shrink the number of tags
  449. * per word so we spread over a few cachelines, at least.
  450. * If less than 4 tags, just forget about it, it's not
  451. * going to work optimally anyway.
  452. */
  453. if (depth >= 4) {
  454. while (tags_per_word * 4 > depth) {
  455. bt->bits_per_word--;
  456. tags_per_word = (1 << bt->bits_per_word);
  457. }
  458. }
  459. nr = ALIGN(depth, tags_per_word) / tags_per_word;
  460. bt->map = kzalloc_node(nr * sizeof(struct blk_align_bitmap),
  461. GFP_KERNEL, node);
  462. if (!bt->map)
  463. return -ENOMEM;
  464. bt->map_nr = nr;
  465. }
  466. bt->bs = kzalloc(BT_WAIT_QUEUES * sizeof(*bt->bs), GFP_KERNEL);
  467. if (!bt->bs) {
  468. kfree(bt->map);
  469. bt->map = NULL;
  470. return -ENOMEM;
  471. }
  472. bt_update_count(bt, depth);
  473. for (i = 0; i < BT_WAIT_QUEUES; i++) {
  474. init_waitqueue_head(&bt->bs[i].wait);
  475. atomic_set(&bt->bs[i].wait_cnt, bt->wake_cnt);
  476. }
  477. return 0;
  478. }
  479. static void bt_free(struct blk_mq_bitmap_tags *bt)
  480. {
  481. kfree(bt->map);
  482. kfree(bt->bs);
  483. }
  484. static struct blk_mq_tags *blk_mq_init_bitmap_tags(struct blk_mq_tags *tags,
  485. int node, int alloc_policy)
  486. {
  487. unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
  488. tags->alloc_policy = alloc_policy;
  489. if (bt_alloc(&tags->bitmap_tags, depth, node, false))
  490. goto enomem;
  491. if (bt_alloc(&tags->breserved_tags, tags->nr_reserved_tags, node, true))
  492. goto enomem;
  493. return tags;
  494. enomem:
  495. bt_free(&tags->bitmap_tags);
  496. kfree(tags);
  497. return NULL;
  498. }
  499. struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
  500. unsigned int reserved_tags,
  501. int node, int alloc_policy)
  502. {
  503. struct blk_mq_tags *tags;
  504. if (total_tags > BLK_MQ_TAG_MAX) {
  505. pr_err("blk-mq: tag depth too large\n");
  506. return NULL;
  507. }
  508. tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
  509. if (!tags)
  510. return NULL;
  511. if (!zalloc_cpumask_var(&tags->cpumask, GFP_KERNEL)) {
  512. kfree(tags);
  513. return NULL;
  514. }
  515. tags->nr_tags = total_tags;
  516. tags->nr_reserved_tags = reserved_tags;
  517. return blk_mq_init_bitmap_tags(tags, node, alloc_policy);
  518. }
  519. void blk_mq_free_tags(struct blk_mq_tags *tags)
  520. {
  521. bt_free(&tags->bitmap_tags);
  522. bt_free(&tags->breserved_tags);
  523. free_cpumask_var(tags->cpumask);
  524. kfree(tags);
  525. }
  526. void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *tag)
  527. {
  528. unsigned int depth = tags->nr_tags - tags->nr_reserved_tags;
  529. *tag = prandom_u32() % depth;
  530. }
  531. int blk_mq_tag_update_depth(struct blk_mq_tags *tags, unsigned int tdepth)
  532. {
  533. tdepth -= tags->nr_reserved_tags;
  534. if (tdepth > tags->nr_tags)
  535. return -EINVAL;
  536. /*
  537. * Don't need (or can't) update reserved tags here, they remain
  538. * static and should never need resizing.
  539. */
  540. bt_update_count(&tags->bitmap_tags, tdepth);
  541. blk_mq_tag_wakeup_all(tags, false);
  542. return 0;
  543. }
  544. /**
  545. * blk_mq_unique_tag() - return a tag that is unique queue-wide
  546. * @rq: request for which to compute a unique tag
  547. *
  548. * The tag field in struct request is unique per hardware queue but not over
  549. * all hardware queues. Hence this function that returns a tag with the
  550. * hardware context index in the upper bits and the per hardware queue tag in
  551. * the lower bits.
  552. *
  553. * Note: When called for a request that is queued on a non-multiqueue request
  554. * queue, the hardware context index is set to zero.
  555. */
  556. u32 blk_mq_unique_tag(struct request *rq)
  557. {
  558. struct request_queue *q = rq->q;
  559. struct blk_mq_hw_ctx *hctx;
  560. int hwq = 0;
  561. if (q->mq_ops) {
  562. hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
  563. hwq = hctx->queue_num;
  564. }
  565. return (hwq << BLK_MQ_UNIQUE_TAG_BITS) |
  566. (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
  567. }
  568. EXPORT_SYMBOL(blk_mq_unique_tag);
  569. ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page)
  570. {
  571. char *orig_page = page;
  572. unsigned int free, res;
  573. if (!tags)
  574. return 0;
  575. page += sprintf(page, "nr_tags=%u, reserved_tags=%u, "
  576. "bits_per_word=%u\n",
  577. tags->nr_tags, tags->nr_reserved_tags,
  578. tags->bitmap_tags.bits_per_word);
  579. free = bt_unused_tags(&tags->bitmap_tags);
  580. res = bt_unused_tags(&tags->breserved_tags);
  581. page += sprintf(page, "nr_free=%u, nr_reserved=%u\n", free, res);
  582. page += sprintf(page, "active_queues=%u\n", atomic_read(&tags->active_queues));
  583. return page - orig_page;
  584. }