blk-mq-sched.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * blk-mq scheduling framework
  3. *
  4. * Copyright (C) 2016 Jens Axboe
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/blk-mq.h>
  9. #include <trace/events/block.h>
  10. #include "blk.h"
  11. #include "blk-mq.h"
  12. #include "blk-mq-debugfs.h"
  13. #include "blk-mq-sched.h"
  14. #include "blk-mq-tag.h"
  15. #include "blk-wbt.h"
  16. void blk_mq_sched_free_hctx_data(struct request_queue *q,
  17. void (*exit)(struct blk_mq_hw_ctx *))
  18. {
  19. struct blk_mq_hw_ctx *hctx;
  20. int i;
  21. queue_for_each_hw_ctx(q, hctx, i) {
  22. if (exit && hctx->sched_data)
  23. exit(hctx);
  24. kfree(hctx->sched_data);
  25. hctx->sched_data = NULL;
  26. }
  27. }
  28. EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
  29. void blk_mq_sched_assign_ioc(struct request *rq, struct bio *bio)
  30. {
  31. struct request_queue *q = rq->q;
  32. struct io_context *ioc = rq_ioc(bio);
  33. struct io_cq *icq;
  34. spin_lock_irq(q->queue_lock);
  35. icq = ioc_lookup_icq(ioc, q);
  36. spin_unlock_irq(q->queue_lock);
  37. if (!icq) {
  38. icq = ioc_create_icq(ioc, q, GFP_ATOMIC);
  39. if (!icq)
  40. return;
  41. }
  42. get_io_context(icq->ioc);
  43. rq->elv.icq = icq;
  44. }
  45. /*
  46. * Mark a hardware queue as needing a restart. For shared queues, maintain
  47. * a count of how many hardware queues are marked for restart.
  48. */
  49. static void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx)
  50. {
  51. if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  52. return;
  53. if (hctx->flags & BLK_MQ_F_TAG_SHARED) {
  54. struct request_queue *q = hctx->queue;
  55. if (!test_and_set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  56. atomic_inc(&q->shared_hctx_restart);
  57. } else
  58. set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  59. }
  60. static bool blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx *hctx)
  61. {
  62. if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  63. return false;
  64. if (hctx->flags & BLK_MQ_F_TAG_SHARED) {
  65. struct request_queue *q = hctx->queue;
  66. if (test_and_clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  67. atomic_dec(&q->shared_hctx_restart);
  68. } else
  69. clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  70. if (blk_mq_hctx_has_pending(hctx)) {
  71. blk_mq_run_hw_queue(hctx, true);
  72. return true;
  73. }
  74. return false;
  75. }
  76. void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
  77. {
  78. struct request_queue *q = hctx->queue;
  79. struct elevator_queue *e = q->elevator;
  80. const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
  81. bool do_sched_dispatch = true;
  82. LIST_HEAD(rq_list);
  83. /* RCU or SRCU read lock is needed before checking quiesced flag */
  84. if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
  85. return;
  86. hctx->run++;
  87. /*
  88. * If we have previous entries on our dispatch list, grab them first for
  89. * more fair dispatch.
  90. */
  91. if (!list_empty_careful(&hctx->dispatch)) {
  92. spin_lock(&hctx->lock);
  93. if (!list_empty(&hctx->dispatch))
  94. list_splice_init(&hctx->dispatch, &rq_list);
  95. spin_unlock(&hctx->lock);
  96. }
  97. /*
  98. * Only ask the scheduler for requests, if we didn't have residual
  99. * requests from the dispatch list. This is to avoid the case where
  100. * we only ever dispatch a fraction of the requests available because
  101. * of low device queue depth. Once we pull requests out of the IO
  102. * scheduler, we can no longer merge or sort them. So it's best to
  103. * leave them there for as long as we can. Mark the hw queue as
  104. * needing a restart in that case.
  105. */
  106. if (!list_empty(&rq_list)) {
  107. blk_mq_sched_mark_restart_hctx(hctx);
  108. do_sched_dispatch = blk_mq_dispatch_rq_list(q, &rq_list);
  109. } else if (!has_sched_dispatch) {
  110. blk_mq_flush_busy_ctxs(hctx, &rq_list);
  111. blk_mq_dispatch_rq_list(q, &rq_list);
  112. }
  113. /*
  114. * We want to dispatch from the scheduler if there was nothing
  115. * on the dispatch list or we were able to dispatch from the
  116. * dispatch list.
  117. */
  118. if (do_sched_dispatch && has_sched_dispatch) {
  119. do {
  120. struct request *rq;
  121. rq = e->type->ops.mq.dispatch_request(hctx);
  122. if (!rq)
  123. break;
  124. list_add(&rq->queuelist, &rq_list);
  125. } while (blk_mq_dispatch_rq_list(q, &rq_list));
  126. }
  127. }
  128. bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
  129. struct request **merged_request)
  130. {
  131. struct request *rq;
  132. switch (elv_merge(q, &rq, bio)) {
  133. case ELEVATOR_BACK_MERGE:
  134. if (!blk_mq_sched_allow_merge(q, rq, bio))
  135. return false;
  136. if (!bio_attempt_back_merge(q, rq, bio))
  137. return false;
  138. *merged_request = attempt_back_merge(q, rq);
  139. if (!*merged_request)
  140. elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
  141. return true;
  142. case ELEVATOR_FRONT_MERGE:
  143. if (!blk_mq_sched_allow_merge(q, rq, bio))
  144. return false;
  145. if (!bio_attempt_front_merge(q, rq, bio))
  146. return false;
  147. *merged_request = attempt_front_merge(q, rq);
  148. if (!*merged_request)
  149. elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
  150. return true;
  151. default:
  152. return false;
  153. }
  154. }
  155. EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
  156. /*
  157. * Reverse check our software queue for entries that we could potentially
  158. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  159. * too much time checking for merges.
  160. */
  161. static bool blk_mq_attempt_merge(struct request_queue *q,
  162. struct blk_mq_ctx *ctx, struct bio *bio)
  163. {
  164. struct request *rq;
  165. int checked = 8;
  166. lockdep_assert_held(&ctx->lock);
  167. list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
  168. bool merged = false;
  169. if (!checked--)
  170. break;
  171. if (!blk_rq_merge_ok(rq, bio))
  172. continue;
  173. switch (blk_try_merge(rq, bio)) {
  174. case ELEVATOR_BACK_MERGE:
  175. if (blk_mq_sched_allow_merge(q, rq, bio))
  176. merged = bio_attempt_back_merge(q, rq, bio);
  177. break;
  178. case ELEVATOR_FRONT_MERGE:
  179. if (blk_mq_sched_allow_merge(q, rq, bio))
  180. merged = bio_attempt_front_merge(q, rq, bio);
  181. break;
  182. case ELEVATOR_DISCARD_MERGE:
  183. merged = bio_attempt_discard_merge(q, rq, bio);
  184. break;
  185. default:
  186. continue;
  187. }
  188. if (merged)
  189. ctx->rq_merged++;
  190. return merged;
  191. }
  192. return false;
  193. }
  194. bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
  195. {
  196. struct elevator_queue *e = q->elevator;
  197. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  198. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  199. bool ret = false;
  200. if (e && e->type->ops.mq.bio_merge) {
  201. blk_mq_put_ctx(ctx);
  202. return e->type->ops.mq.bio_merge(hctx, bio);
  203. }
  204. if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  205. !list_empty_careful(&ctx->rq_list)) {
  206. /* default per sw-queue merge */
  207. spin_lock(&ctx->lock);
  208. ret = blk_mq_attempt_merge(q, ctx, bio);
  209. spin_unlock(&ctx->lock);
  210. }
  211. blk_mq_put_ctx(ctx);
  212. return ret;
  213. }
  214. bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
  215. {
  216. return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
  217. }
  218. EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
  219. void blk_mq_sched_request_inserted(struct request *rq)
  220. {
  221. trace_block_rq_insert(rq->q, rq);
  222. }
  223. EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
  224. static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
  225. struct request *rq)
  226. {
  227. if (rq->tag == -1) {
  228. rq->rq_flags |= RQF_SORTED;
  229. return false;
  230. }
  231. /*
  232. * If we already have a real request tag, send directly to
  233. * the dispatch list.
  234. */
  235. spin_lock(&hctx->lock);
  236. list_add(&rq->queuelist, &hctx->dispatch);
  237. spin_unlock(&hctx->lock);
  238. return true;
  239. }
  240. /**
  241. * list_for_each_entry_rcu_rr - iterate in a round-robin fashion over rcu list
  242. * @pos: loop cursor.
  243. * @skip: the list element that will not be examined. Iteration starts at
  244. * @skip->next.
  245. * @head: head of the list to examine. This list must have at least one
  246. * element, namely @skip.
  247. * @member: name of the list_head structure within typeof(*pos).
  248. */
  249. #define list_for_each_entry_rcu_rr(pos, skip, head, member) \
  250. for ((pos) = (skip); \
  251. (pos = (pos)->member.next != (head) ? list_entry_rcu( \
  252. (pos)->member.next, typeof(*pos), member) : \
  253. list_entry_rcu((pos)->member.next->next, typeof(*pos), member)), \
  254. (pos) != (skip); )
  255. /*
  256. * Called after a driver tag has been freed to check whether a hctx needs to
  257. * be restarted. Restarts @hctx if its tag set is not shared. Restarts hardware
  258. * queues in a round-robin fashion if the tag set of @hctx is shared with other
  259. * hardware queues.
  260. */
  261. void blk_mq_sched_restart(struct blk_mq_hw_ctx *const hctx)
  262. {
  263. struct blk_mq_tags *const tags = hctx->tags;
  264. struct blk_mq_tag_set *const set = hctx->queue->tag_set;
  265. struct request_queue *const queue = hctx->queue, *q;
  266. struct blk_mq_hw_ctx *hctx2;
  267. unsigned int i, j;
  268. if (set->flags & BLK_MQ_F_TAG_SHARED) {
  269. /*
  270. * If this is 0, then we know that no hardware queues
  271. * have RESTART marked. We're done.
  272. */
  273. if (!atomic_read(&queue->shared_hctx_restart))
  274. return;
  275. rcu_read_lock();
  276. list_for_each_entry_rcu_rr(q, queue, &set->tag_list,
  277. tag_set_list) {
  278. queue_for_each_hw_ctx(q, hctx2, i)
  279. if (hctx2->tags == tags &&
  280. blk_mq_sched_restart_hctx(hctx2))
  281. goto done;
  282. }
  283. j = hctx->queue_num + 1;
  284. for (i = 0; i < queue->nr_hw_queues; i++, j++) {
  285. if (j == queue->nr_hw_queues)
  286. j = 0;
  287. hctx2 = queue->queue_hw_ctx[j];
  288. if (hctx2->tags == tags &&
  289. blk_mq_sched_restart_hctx(hctx2))
  290. break;
  291. }
  292. done:
  293. rcu_read_unlock();
  294. } else {
  295. blk_mq_sched_restart_hctx(hctx);
  296. }
  297. }
  298. /*
  299. * Add flush/fua to the queue. If we fail getting a driver tag, then
  300. * punt to the requeue list. Requeue will re-invoke us from a context
  301. * that's safe to block from.
  302. */
  303. static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx,
  304. struct request *rq, bool can_block)
  305. {
  306. if (blk_mq_get_driver_tag(rq, &hctx, can_block)) {
  307. blk_insert_flush(rq);
  308. blk_mq_run_hw_queue(hctx, true);
  309. } else
  310. blk_mq_add_to_requeue_list(rq, false, true);
  311. }
  312. void blk_mq_sched_insert_request(struct request *rq, bool at_head,
  313. bool run_queue, bool async, bool can_block)
  314. {
  315. struct request_queue *q = rq->q;
  316. struct elevator_queue *e = q->elevator;
  317. struct blk_mq_ctx *ctx = rq->mq_ctx;
  318. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  319. if (rq->tag == -1 && op_is_flush(rq->cmd_flags)) {
  320. blk_mq_sched_insert_flush(hctx, rq, can_block);
  321. return;
  322. }
  323. if (e && blk_mq_sched_bypass_insert(hctx, rq))
  324. goto run;
  325. if (e && e->type->ops.mq.insert_requests) {
  326. LIST_HEAD(list);
  327. list_add(&rq->queuelist, &list);
  328. e->type->ops.mq.insert_requests(hctx, &list, at_head);
  329. } else {
  330. spin_lock(&ctx->lock);
  331. __blk_mq_insert_request(hctx, rq, at_head);
  332. spin_unlock(&ctx->lock);
  333. }
  334. run:
  335. if (run_queue)
  336. blk_mq_run_hw_queue(hctx, async);
  337. }
  338. void blk_mq_sched_insert_requests(struct request_queue *q,
  339. struct blk_mq_ctx *ctx,
  340. struct list_head *list, bool run_queue_async)
  341. {
  342. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  343. struct elevator_queue *e = hctx->queue->elevator;
  344. if (e) {
  345. struct request *rq, *next;
  346. /*
  347. * We bypass requests that already have a driver tag assigned,
  348. * which should only be flushes. Flushes are only ever inserted
  349. * as single requests, so we shouldn't ever hit the
  350. * WARN_ON_ONCE() below (but let's handle it just in case).
  351. */
  352. list_for_each_entry_safe(rq, next, list, queuelist) {
  353. if (WARN_ON_ONCE(rq->tag != -1)) {
  354. list_del_init(&rq->queuelist);
  355. blk_mq_sched_bypass_insert(hctx, rq);
  356. }
  357. }
  358. }
  359. if (e && e->type->ops.mq.insert_requests)
  360. e->type->ops.mq.insert_requests(hctx, list, false);
  361. else
  362. blk_mq_insert_requests(hctx, ctx, list);
  363. blk_mq_run_hw_queue(hctx, run_queue_async);
  364. }
  365. static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
  366. struct blk_mq_hw_ctx *hctx,
  367. unsigned int hctx_idx)
  368. {
  369. if (hctx->sched_tags) {
  370. blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
  371. blk_mq_free_rq_map(hctx->sched_tags);
  372. hctx->sched_tags = NULL;
  373. }
  374. }
  375. static int blk_mq_sched_alloc_tags(struct request_queue *q,
  376. struct blk_mq_hw_ctx *hctx,
  377. unsigned int hctx_idx)
  378. {
  379. struct blk_mq_tag_set *set = q->tag_set;
  380. int ret;
  381. hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
  382. set->reserved_tags);
  383. if (!hctx->sched_tags)
  384. return -ENOMEM;
  385. ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
  386. if (ret)
  387. blk_mq_sched_free_tags(set, hctx, hctx_idx);
  388. return ret;
  389. }
  390. static void blk_mq_sched_tags_teardown(struct request_queue *q)
  391. {
  392. struct blk_mq_tag_set *set = q->tag_set;
  393. struct blk_mq_hw_ctx *hctx;
  394. int i;
  395. queue_for_each_hw_ctx(q, hctx, i)
  396. blk_mq_sched_free_tags(set, hctx, i);
  397. }
  398. int blk_mq_sched_init_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  399. unsigned int hctx_idx)
  400. {
  401. struct elevator_queue *e = q->elevator;
  402. int ret;
  403. if (!e)
  404. return 0;
  405. ret = blk_mq_sched_alloc_tags(q, hctx, hctx_idx);
  406. if (ret)
  407. return ret;
  408. if (e->type->ops.mq.init_hctx) {
  409. ret = e->type->ops.mq.init_hctx(hctx, hctx_idx);
  410. if (ret) {
  411. blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
  412. return ret;
  413. }
  414. }
  415. blk_mq_debugfs_register_sched_hctx(q, hctx);
  416. return 0;
  417. }
  418. void blk_mq_sched_exit_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
  419. unsigned int hctx_idx)
  420. {
  421. struct elevator_queue *e = q->elevator;
  422. if (!e)
  423. return;
  424. blk_mq_debugfs_unregister_sched_hctx(hctx);
  425. if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
  426. e->type->ops.mq.exit_hctx(hctx, hctx_idx);
  427. hctx->sched_data = NULL;
  428. }
  429. blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
  430. }
  431. int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
  432. {
  433. struct blk_mq_hw_ctx *hctx;
  434. struct elevator_queue *eq;
  435. unsigned int i;
  436. int ret;
  437. if (!e) {
  438. q->elevator = NULL;
  439. return 0;
  440. }
  441. /*
  442. * Default to double of smaller one between hw queue_depth and 128,
  443. * since we don't split into sync/async like the old code did.
  444. * Additionally, this is a per-hw queue depth.
  445. */
  446. q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
  447. BLKDEV_MAX_RQ);
  448. queue_for_each_hw_ctx(q, hctx, i) {
  449. ret = blk_mq_sched_alloc_tags(q, hctx, i);
  450. if (ret)
  451. goto err;
  452. }
  453. ret = e->ops.mq.init_sched(q, e);
  454. if (ret)
  455. goto err;
  456. blk_mq_debugfs_register_sched(q);
  457. queue_for_each_hw_ctx(q, hctx, i) {
  458. if (e->ops.mq.init_hctx) {
  459. ret = e->ops.mq.init_hctx(hctx, i);
  460. if (ret) {
  461. eq = q->elevator;
  462. blk_mq_exit_sched(q, eq);
  463. kobject_put(&eq->kobj);
  464. return ret;
  465. }
  466. }
  467. blk_mq_debugfs_register_sched_hctx(q, hctx);
  468. }
  469. return 0;
  470. err:
  471. blk_mq_sched_tags_teardown(q);
  472. q->elevator = NULL;
  473. return ret;
  474. }
  475. void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
  476. {
  477. struct blk_mq_hw_ctx *hctx;
  478. unsigned int i;
  479. queue_for_each_hw_ctx(q, hctx, i) {
  480. blk_mq_debugfs_unregister_sched_hctx(hctx);
  481. if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
  482. e->type->ops.mq.exit_hctx(hctx, i);
  483. hctx->sched_data = NULL;
  484. }
  485. }
  486. blk_mq_debugfs_unregister_sched(q);
  487. if (e->type->ops.mq.exit_sched)
  488. e->type->ops.mq.exit_sched(e);
  489. blk_mq_sched_tags_teardown(q);
  490. q->elevator = NULL;
  491. }
  492. int blk_mq_sched_init(struct request_queue *q)
  493. {
  494. int ret;
  495. mutex_lock(&q->sysfs_lock);
  496. ret = elevator_init(q, NULL);
  497. mutex_unlock(&q->sysfs_lock);
  498. return ret;
  499. }