blk-mq-tag.c 16 KB

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