blk-mq-tag.c 14 KB

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