blk-mq-tag.c 18 KB

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