blk-mq-tag.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Tag allocation using scalable bitmaps. Uses active queue tracking to support
  4. * fairer distribution of tags between multiple submitters when a shared tag map
  5. * is used.
  6. *
  7. * Copyright (C) 2013-2014 Jens Axboe
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include "blk.h"
  13. #include "blk-mq.h"
  14. #include "blk-mq-sched.h"
  15. /*
  16. * Recalculate wakeup batch when tag is shared by hctx.
  17. */
  18. static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
  19. unsigned int users)
  20. {
  21. if (!users)
  22. return;
  23. sbitmap_queue_recalculate_wake_batch(&tags->bitmap_tags,
  24. users);
  25. sbitmap_queue_recalculate_wake_batch(&tags->breserved_tags,
  26. users);
  27. }
  28. /*
  29. * If a previously inactive queue goes active, bump the active user count.
  30. * We need to do this before try to allocate driver tag, then even if fail
  31. * to get tag when first time, the other shared-tag users could reserve
  32. * budget for it.
  33. */
  34. void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
  35. {
  36. unsigned int users;
  37. unsigned long flags;
  38. struct blk_mq_tags *tags = hctx->tags;
  39. /*
  40. * calling test_bit() prior to test_and_set_bit() is intentional,
  41. * it avoids dirtying the cacheline if the queue is already active.
  42. */
  43. if (blk_mq_is_shared_tags(hctx->flags)) {
  44. struct request_queue *q = hctx->queue;
  45. if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags) ||
  46. test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
  47. return;
  48. } else {
  49. if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) ||
  50. test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  51. return;
  52. }
  53. spin_lock_irqsave(&tags->lock, flags);
  54. users = tags->active_queues + 1;
  55. WRITE_ONCE(tags->active_queues, users);
  56. blk_mq_update_wake_batch(tags, users);
  57. spin_unlock_irqrestore(&tags->lock, flags);
  58. }
  59. /*
  60. * Wakeup all potentially sleeping on tags
  61. */
  62. void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool include_reserve)
  63. {
  64. sbitmap_queue_wake_all(&tags->bitmap_tags);
  65. if (include_reserve)
  66. sbitmap_queue_wake_all(&tags->breserved_tags);
  67. }
  68. /*
  69. * If a previously busy queue goes inactive, potential waiters could now
  70. * be allowed to queue. Wake them up and check.
  71. */
  72. void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
  73. {
  74. struct blk_mq_tags *tags = hctx->tags;
  75. unsigned int users;
  76. if (blk_mq_is_shared_tags(hctx->flags)) {
  77. struct request_queue *q = hctx->queue;
  78. if (!test_and_clear_bit(QUEUE_FLAG_HCTX_ACTIVE,
  79. &q->queue_flags))
  80. return;
  81. } else {
  82. if (!test_and_clear_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  83. return;
  84. }
  85. spin_lock_irq(&tags->lock);
  86. users = tags->active_queues - 1;
  87. WRITE_ONCE(tags->active_queues, users);
  88. blk_mq_update_wake_batch(tags, users);
  89. spin_unlock_irq(&tags->lock);
  90. blk_mq_tag_wakeup_all(tags, false);
  91. }
  92. static int __blk_mq_get_tag(struct blk_mq_alloc_data *data,
  93. struct sbitmap_queue *bt)
  94. {
  95. if (!data->q->elevator && !(data->flags & BLK_MQ_REQ_RESERVED) &&
  96. !hctx_may_queue(data->hctx, bt))
  97. return BLK_MQ_NO_TAG;
  98. if (data->shallow_depth)
  99. return sbitmap_queue_get_shallow(bt, data->shallow_depth);
  100. else
  101. return __sbitmap_queue_get(bt);
  102. }
  103. unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags,
  104. unsigned int *offset)
  105. {
  106. struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
  107. struct sbitmap_queue *bt = &tags->bitmap_tags;
  108. unsigned long ret;
  109. if (data->shallow_depth ||data->flags & BLK_MQ_REQ_RESERVED ||
  110. data->hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
  111. return 0;
  112. ret = __sbitmap_queue_get_batch(bt, nr_tags, offset);
  113. *offset += tags->nr_reserved_tags;
  114. return ret;
  115. }
  116. unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
  117. {
  118. struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
  119. struct sbitmap_queue *bt;
  120. struct sbq_wait_state *ws;
  121. DEFINE_SBQ_WAIT(wait);
  122. unsigned int tag_offset;
  123. int tag;
  124. if (data->flags & BLK_MQ_REQ_RESERVED) {
  125. if (unlikely(!tags->nr_reserved_tags)) {
  126. WARN_ON_ONCE(1);
  127. return BLK_MQ_NO_TAG;
  128. }
  129. bt = &tags->breserved_tags;
  130. tag_offset = 0;
  131. } else {
  132. bt = &tags->bitmap_tags;
  133. tag_offset = tags->nr_reserved_tags;
  134. }
  135. tag = __blk_mq_get_tag(data, bt);
  136. if (tag != BLK_MQ_NO_TAG)
  137. goto found_tag;
  138. if (data->flags & BLK_MQ_REQ_NOWAIT)
  139. return BLK_MQ_NO_TAG;
  140. ws = bt_wait_ptr(bt, data->hctx);
  141. do {
  142. struct sbitmap_queue *bt_prev;
  143. /*
  144. * We're out of tags on this hardware queue, kick any
  145. * pending IO submits before going to sleep waiting for
  146. * some to complete.
  147. */
  148. blk_mq_run_hw_queue(data->hctx, false);
  149. /*
  150. * Retry tag allocation after running the hardware queue,
  151. * as running the queue may also have found completions.
  152. */
  153. tag = __blk_mq_get_tag(data, bt);
  154. if (tag != BLK_MQ_NO_TAG)
  155. break;
  156. sbitmap_prepare_to_wait(bt, ws, &wait, TASK_UNINTERRUPTIBLE);
  157. tag = __blk_mq_get_tag(data, bt);
  158. if (tag != BLK_MQ_NO_TAG)
  159. break;
  160. bt_prev = bt;
  161. io_schedule();
  162. sbitmap_finish_wait(bt, ws, &wait);
  163. data->ctx = blk_mq_get_ctx(data->q);
  164. data->hctx = blk_mq_map_queue(data->q, data->cmd_flags,
  165. data->ctx);
  166. tags = blk_mq_tags_from_data(data);
  167. if (data->flags & BLK_MQ_REQ_RESERVED)
  168. bt = &tags->breserved_tags;
  169. else
  170. bt = &tags->bitmap_tags;
  171. /*
  172. * If destination hw queue is changed, fake wake up on
  173. * previous queue for compensating the wake up miss, so
  174. * other allocations on previous queue won't be starved.
  175. */
  176. if (bt != bt_prev)
  177. sbitmap_queue_wake_up(bt_prev, 1);
  178. ws = bt_wait_ptr(bt, data->hctx);
  179. } while (1);
  180. sbitmap_finish_wait(bt, ws, &wait);
  181. found_tag:
  182. /*
  183. * Give up this allocation if the hctx is inactive. The caller will
  184. * retry on an active hctx.
  185. */
  186. if (unlikely(test_bit(BLK_MQ_S_INACTIVE, &data->hctx->state))) {
  187. blk_mq_put_tag(tags, data->ctx, tag + tag_offset);
  188. return BLK_MQ_NO_TAG;
  189. }
  190. return tag + tag_offset;
  191. }
  192. void blk_mq_put_tag(struct blk_mq_tags *tags, struct blk_mq_ctx *ctx,
  193. unsigned int tag)
  194. {
  195. if (!blk_mq_tag_is_reserved(tags, tag)) {
  196. const int real_tag = tag - tags->nr_reserved_tags;
  197. BUG_ON(real_tag >= tags->nr_tags);
  198. sbitmap_queue_clear(&tags->bitmap_tags, real_tag, ctx->cpu);
  199. } else {
  200. sbitmap_queue_clear(&tags->breserved_tags, tag, ctx->cpu);
  201. }
  202. }
  203. void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
  204. {
  205. sbitmap_queue_clear_batch(&tags->bitmap_tags, tags->nr_reserved_tags,
  206. tag_array, nr_tags);
  207. }
  208. struct bt_iter_data {
  209. struct blk_mq_hw_ctx *hctx;
  210. struct request_queue *q;
  211. busy_tag_iter_fn *fn;
  212. void *data;
  213. bool reserved;
  214. };
  215. static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
  216. unsigned int bitnr)
  217. {
  218. struct request *rq;
  219. unsigned long flags;
  220. spin_lock_irqsave(&tags->lock, flags);
  221. rq = tags->rqs[bitnr];
  222. if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
  223. rq = NULL;
  224. spin_unlock_irqrestore(&tags->lock, flags);
  225. return rq;
  226. }
  227. static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
  228. {
  229. struct bt_iter_data *iter_data = data;
  230. struct blk_mq_hw_ctx *hctx = iter_data->hctx;
  231. struct request_queue *q = iter_data->q;
  232. struct blk_mq_tag_set *set = q->tag_set;
  233. struct blk_mq_tags *tags;
  234. struct request *rq;
  235. bool ret = true;
  236. if (blk_mq_is_shared_tags(set->flags))
  237. tags = set->shared_tags;
  238. else
  239. tags = hctx->tags;
  240. if (!iter_data->reserved)
  241. bitnr += tags->nr_reserved_tags;
  242. /*
  243. * We can hit rq == NULL here, because the tagging functions
  244. * test and set the bit before assigning ->rqs[].
  245. */
  246. rq = blk_mq_find_and_get_req(tags, bitnr);
  247. if (!rq)
  248. return true;
  249. if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
  250. ret = iter_data->fn(rq, iter_data->data);
  251. blk_mq_put_rq_ref(rq);
  252. return ret;
  253. }
  254. /**
  255. * bt_for_each - iterate over the requests associated with a hardware queue
  256. * @hctx: Hardware queue to examine.
  257. * @q: Request queue to examine.
  258. * @bt: sbitmap to examine. This is either the breserved_tags member
  259. * or the bitmap_tags member of struct blk_mq_tags.
  260. * @fn: Pointer to the function that will be called for each request
  261. * associated with @hctx that has been assigned a driver tag.
  262. * @fn will be called as follows: @fn(@hctx, rq, @data, @reserved)
  263. * where rq is a pointer to a request. Return true to continue
  264. * iterating tags, false to stop.
  265. * @data: Will be passed as third argument to @fn.
  266. * @reserved: Indicates whether @bt is the breserved_tags member or the
  267. * bitmap_tags member of struct blk_mq_tags.
  268. */
  269. static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
  270. struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
  271. void *data, bool reserved)
  272. {
  273. struct bt_iter_data iter_data = {
  274. .hctx = hctx,
  275. .fn = fn,
  276. .data = data,
  277. .reserved = reserved,
  278. .q = q,
  279. };
  280. sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
  281. }
  282. struct bt_tags_iter_data {
  283. struct blk_mq_tags *tags;
  284. busy_tag_iter_fn *fn;
  285. void *data;
  286. unsigned int flags;
  287. };
  288. #define BT_TAG_ITER_RESERVED (1 << 0)
  289. #define BT_TAG_ITER_STARTED (1 << 1)
  290. #define BT_TAG_ITER_STATIC_RQS (1 << 2)
  291. static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
  292. {
  293. struct bt_tags_iter_data *iter_data = data;
  294. struct blk_mq_tags *tags = iter_data->tags;
  295. struct request *rq;
  296. bool ret = true;
  297. bool iter_static_rqs = !!(iter_data->flags & BT_TAG_ITER_STATIC_RQS);
  298. if (!(iter_data->flags & BT_TAG_ITER_RESERVED))
  299. bitnr += tags->nr_reserved_tags;
  300. /*
  301. * We can hit rq == NULL here, because the tagging functions
  302. * test and set the bit before assigning ->rqs[].
  303. */
  304. if (iter_static_rqs)
  305. rq = tags->static_rqs[bitnr];
  306. else
  307. rq = blk_mq_find_and_get_req(tags, bitnr);
  308. if (!rq)
  309. return true;
  310. if (!(iter_data->flags & BT_TAG_ITER_STARTED) ||
  311. blk_mq_request_started(rq))
  312. ret = iter_data->fn(rq, iter_data->data);
  313. if (!iter_static_rqs)
  314. blk_mq_put_rq_ref(rq);
  315. return ret;
  316. }
  317. /**
  318. * bt_tags_for_each - iterate over the requests in a tag map
  319. * @tags: Tag map to iterate over.
  320. * @bt: sbitmap to examine. This is either the breserved_tags member
  321. * or the bitmap_tags member of struct blk_mq_tags.
  322. * @fn: Pointer to the function that will be called for each started
  323. * request. @fn will be called as follows: @fn(rq, @data,
  324. * @reserved) where rq is a pointer to a request. Return true
  325. * to continue iterating tags, false to stop.
  326. * @data: Will be passed as second argument to @fn.
  327. * @flags: BT_TAG_ITER_*
  328. */
  329. static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt,
  330. busy_tag_iter_fn *fn, void *data, unsigned int flags)
  331. {
  332. struct bt_tags_iter_data iter_data = {
  333. .tags = tags,
  334. .fn = fn,
  335. .data = data,
  336. .flags = flags,
  337. };
  338. if (tags->rqs)
  339. sbitmap_for_each_set(&bt->sb, bt_tags_iter, &iter_data);
  340. }
  341. static void __blk_mq_all_tag_iter(struct blk_mq_tags *tags,
  342. busy_tag_iter_fn *fn, void *priv, unsigned int flags)
  343. {
  344. WARN_ON_ONCE(flags & BT_TAG_ITER_RESERVED);
  345. if (tags->nr_reserved_tags)
  346. bt_tags_for_each(tags, &tags->breserved_tags, fn, priv,
  347. flags | BT_TAG_ITER_RESERVED);
  348. bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, flags);
  349. }
  350. /**
  351. * blk_mq_all_tag_iter - iterate over all requests in a tag map
  352. * @tags: Tag map to iterate over.
  353. * @fn: Pointer to the function that will be called for each
  354. * request. @fn will be called as follows: @fn(rq, @priv,
  355. * reserved) where rq is a pointer to a request. 'reserved'
  356. * indicates whether or not @rq is a reserved request. Return
  357. * true to continue iterating tags, false to stop.
  358. * @priv: Will be passed as second argument to @fn.
  359. *
  360. * Caller has to pass the tag map from which requests are allocated.
  361. */
  362. void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn,
  363. void *priv)
  364. {
  365. __blk_mq_all_tag_iter(tags, fn, priv, BT_TAG_ITER_STATIC_RQS);
  366. }
  367. /**
  368. * blk_mq_tagset_busy_iter - iterate over all started requests in a tag set
  369. * @tagset: Tag set to iterate over.
  370. * @fn: Pointer to the function that will be called for each started
  371. * request. @fn will be called as follows: @fn(rq, @priv,
  372. * reserved) where rq is a pointer to a request. 'reserved'
  373. * indicates whether or not @rq is a reserved request. Return
  374. * true to continue iterating tags, false to stop.
  375. * @priv: Will be passed as second argument to @fn.
  376. *
  377. * We grab one request reference before calling @fn and release it after
  378. * @fn returns.
  379. */
  380. void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
  381. busy_tag_iter_fn *fn, void *priv)
  382. {
  383. unsigned int flags = tagset->flags;
  384. int i, nr_tags;
  385. nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
  386. for (i = 0; i < nr_tags; i++) {
  387. if (tagset->tags && tagset->tags[i])
  388. __blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
  389. BT_TAG_ITER_STARTED);
  390. }
  391. }
  392. EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
  393. static bool blk_mq_tagset_count_completed_rqs(struct request *rq, void *data)
  394. {
  395. unsigned *count = data;
  396. if (blk_mq_request_completed(rq))
  397. (*count)++;
  398. return true;
  399. }
  400. /**
  401. * blk_mq_tagset_wait_completed_request - Wait until all scheduled request
  402. * completions have finished.
  403. * @tagset: Tag set to drain completed request
  404. *
  405. * Note: This function has to be run after all IO queues are shutdown
  406. */
  407. void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
  408. {
  409. while (true) {
  410. unsigned count = 0;
  411. blk_mq_tagset_busy_iter(tagset,
  412. blk_mq_tagset_count_completed_rqs, &count);
  413. if (!count)
  414. break;
  415. msleep(5);
  416. }
  417. }
  418. EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
  419. /**
  420. * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
  421. * @q: Request queue to examine.
  422. * @fn: Pointer to the function that will be called for each request
  423. * on @q. @fn will be called as follows: @fn(hctx, rq, @priv,
  424. * reserved) where rq is a pointer to a request and hctx points
  425. * to the hardware queue associated with the request. 'reserved'
  426. * indicates whether or not @rq is a reserved request.
  427. * @priv: Will be passed as third argument to @fn.
  428. *
  429. * Note: if @q->tag_set is shared with other request queues then @fn will be
  430. * called for all requests on all queues that share that tag set and not only
  431. * for requests associated with @q.
  432. */
  433. void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
  434. void *priv)
  435. {
  436. /*
  437. * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
  438. * while the queue is frozen. So we can use q_usage_counter to avoid
  439. * racing with it.
  440. */
  441. if (!percpu_ref_tryget(&q->q_usage_counter))
  442. return;
  443. if (blk_mq_is_shared_tags(q->tag_set->flags)) {
  444. struct blk_mq_tags *tags = q->tag_set->shared_tags;
  445. struct sbitmap_queue *bresv = &tags->breserved_tags;
  446. struct sbitmap_queue *btags = &tags->bitmap_tags;
  447. if (tags->nr_reserved_tags)
  448. bt_for_each(NULL, q, bresv, fn, priv, true);
  449. bt_for_each(NULL, q, btags, fn, priv, false);
  450. } else {
  451. struct blk_mq_hw_ctx *hctx;
  452. unsigned long i;
  453. queue_for_each_hw_ctx(q, hctx, i) {
  454. struct blk_mq_tags *tags = hctx->tags;
  455. struct sbitmap_queue *bresv = &tags->breserved_tags;
  456. struct sbitmap_queue *btags = &tags->bitmap_tags;
  457. /*
  458. * If no software queues are currently mapped to this
  459. * hardware queue, there's nothing to check
  460. */
  461. if (!blk_mq_hw_queue_mapped(hctx))
  462. continue;
  463. if (tags->nr_reserved_tags)
  464. bt_for_each(hctx, q, bresv, fn, priv, true);
  465. bt_for_each(hctx, q, btags, fn, priv, false);
  466. }
  467. }
  468. blk_queue_exit(q);
  469. }
  470. static int bt_alloc(struct sbitmap_queue *bt, unsigned int depth,
  471. bool round_robin, int node)
  472. {
  473. return sbitmap_queue_init_node(bt, depth, -1, round_robin, GFP_KERNEL,
  474. node);
  475. }
  476. int blk_mq_init_bitmaps(struct sbitmap_queue *bitmap_tags,
  477. struct sbitmap_queue *breserved_tags,
  478. unsigned int queue_depth, unsigned int reserved,
  479. int node, int alloc_policy)
  480. {
  481. unsigned int depth = queue_depth - reserved;
  482. bool round_robin = alloc_policy == BLK_TAG_ALLOC_RR;
  483. if (bt_alloc(bitmap_tags, depth, round_robin, node))
  484. return -ENOMEM;
  485. if (bt_alloc(breserved_tags, reserved, round_robin, node))
  486. goto free_bitmap_tags;
  487. return 0;
  488. free_bitmap_tags:
  489. sbitmap_queue_free(bitmap_tags);
  490. return -ENOMEM;
  491. }
  492. struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
  493. unsigned int reserved_tags,
  494. int node, int alloc_policy)
  495. {
  496. struct blk_mq_tags *tags;
  497. if (total_tags > BLK_MQ_TAG_MAX) {
  498. pr_err("blk-mq: tag depth too large\n");
  499. return NULL;
  500. }
  501. tags = kzalloc_node(sizeof(*tags), GFP_KERNEL, node);
  502. if (!tags)
  503. return NULL;
  504. tags->nr_tags = total_tags;
  505. tags->nr_reserved_tags = reserved_tags;
  506. spin_lock_init(&tags->lock);
  507. if (blk_mq_init_bitmaps(&tags->bitmap_tags, &tags->breserved_tags,
  508. total_tags, reserved_tags, node,
  509. alloc_policy) < 0) {
  510. kfree(tags);
  511. return NULL;
  512. }
  513. return tags;
  514. }
  515. void blk_mq_free_tags(struct blk_mq_tags *tags)
  516. {
  517. sbitmap_queue_free(&tags->bitmap_tags);
  518. sbitmap_queue_free(&tags->breserved_tags);
  519. kfree(tags);
  520. }
  521. int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx,
  522. struct blk_mq_tags **tagsptr, unsigned int tdepth,
  523. bool can_grow)
  524. {
  525. struct blk_mq_tags *tags = *tagsptr;
  526. if (tdepth <= tags->nr_reserved_tags)
  527. return -EINVAL;
  528. /*
  529. * If we are allowed to grow beyond the original size, allocate
  530. * a new set of tags before freeing the old one.
  531. */
  532. if (tdepth > tags->nr_tags) {
  533. struct blk_mq_tag_set *set = hctx->queue->tag_set;
  534. struct blk_mq_tags *new;
  535. if (!can_grow)
  536. return -EINVAL;
  537. /*
  538. * We need some sort of upper limit, set it high enough that
  539. * no valid use cases should require more.
  540. */
  541. if (tdepth > MAX_SCHED_RQ)
  542. return -EINVAL;
  543. /*
  544. * Only the sbitmap needs resizing since we allocated the max
  545. * initially.
  546. */
  547. if (blk_mq_is_shared_tags(set->flags))
  548. return 0;
  549. new = blk_mq_alloc_map_and_rqs(set, hctx->queue_num, tdepth);
  550. if (!new)
  551. return -ENOMEM;
  552. blk_mq_free_map_and_rqs(set, *tagsptr, hctx->queue_num);
  553. *tagsptr = new;
  554. } else {
  555. /*
  556. * Don't need (or can't) update reserved tags here, they
  557. * remain static and should never need resizing.
  558. */
  559. sbitmap_queue_resize(&tags->bitmap_tags,
  560. tdepth - tags->nr_reserved_tags);
  561. }
  562. return 0;
  563. }
  564. void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size)
  565. {
  566. struct blk_mq_tags *tags = set->shared_tags;
  567. sbitmap_queue_resize(&tags->bitmap_tags, size - set->reserved_tags);
  568. }
  569. void blk_mq_tag_update_sched_shared_tags(struct request_queue *q)
  570. {
  571. sbitmap_queue_resize(&q->sched_shared_tags->bitmap_tags,
  572. q->nr_requests - q->tag_set->reserved_tags);
  573. }
  574. /**
  575. * blk_mq_unique_tag() - return a tag that is unique queue-wide
  576. * @rq: request for which to compute a unique tag
  577. *
  578. * The tag field in struct request is unique per hardware queue but not over
  579. * all hardware queues. Hence this function that returns a tag with the
  580. * hardware context index in the upper bits and the per hardware queue tag in
  581. * the lower bits.
  582. *
  583. * Note: When called for a request that is queued on a non-multiqueue request
  584. * queue, the hardware context index is set to zero.
  585. */
  586. u32 blk_mq_unique_tag(struct request *rq)
  587. {
  588. return (rq->mq_hctx->queue_num << BLK_MQ_UNIQUE_TAG_BITS) |
  589. (rq->tag & BLK_MQ_UNIQUE_TAG_MASK);
  590. }
  591. EXPORT_SYMBOL(blk_mq_unique_tag);