blk-mq-sched.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  54. }
  55. void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
  56. {
  57. if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  58. return;
  59. clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  60. blk_mq_run_hw_queue(hctx, true);
  61. }
  62. /*
  63. * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
  64. * its queue by itself in its completion handler, so we don't need to
  65. * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
  66. */
  67. static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
  68. {
  69. struct request_queue *q = hctx->queue;
  70. struct elevator_queue *e = q->elevator;
  71. LIST_HEAD(rq_list);
  72. do {
  73. struct request *rq;
  74. if (e->type->ops.mq.has_work &&
  75. !e->type->ops.mq.has_work(hctx))
  76. break;
  77. if (!blk_mq_get_dispatch_budget(hctx))
  78. break;
  79. rq = e->type->ops.mq.dispatch_request(hctx);
  80. if (!rq) {
  81. blk_mq_put_dispatch_budget(hctx);
  82. break;
  83. }
  84. /*
  85. * Now this rq owns the budget which has to be released
  86. * if this rq won't be queued to driver via .queue_rq()
  87. * in blk_mq_dispatch_rq_list().
  88. */
  89. list_add(&rq->queuelist, &rq_list);
  90. } while (blk_mq_dispatch_rq_list(q, &rq_list, true));
  91. }
  92. static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
  93. struct blk_mq_ctx *ctx)
  94. {
  95. unsigned idx = ctx->index_hw;
  96. if (++idx == hctx->nr_ctx)
  97. idx = 0;
  98. return hctx->ctxs[idx];
  99. }
  100. /*
  101. * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
  102. * its queue by itself in its completion handler, so we don't need to
  103. * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
  104. */
  105. static void blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
  106. {
  107. struct request_queue *q = hctx->queue;
  108. LIST_HEAD(rq_list);
  109. struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
  110. do {
  111. struct request *rq;
  112. if (!sbitmap_any_bit_set(&hctx->ctx_map))
  113. break;
  114. if (!blk_mq_get_dispatch_budget(hctx))
  115. break;
  116. rq = blk_mq_dequeue_from_ctx(hctx, ctx);
  117. if (!rq) {
  118. blk_mq_put_dispatch_budget(hctx);
  119. break;
  120. }
  121. /*
  122. * Now this rq owns the budget which has to be released
  123. * if this rq won't be queued to driver via .queue_rq()
  124. * in blk_mq_dispatch_rq_list().
  125. */
  126. list_add(&rq->queuelist, &rq_list);
  127. /* round robin for fair dispatch */
  128. ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
  129. } while (blk_mq_dispatch_rq_list(q, &rq_list, true));
  130. WRITE_ONCE(hctx->dispatch_from, ctx);
  131. }
  132. void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
  133. {
  134. struct request_queue *q = hctx->queue;
  135. struct elevator_queue *e = q->elevator;
  136. const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
  137. LIST_HEAD(rq_list);
  138. /* RCU or SRCU read lock is needed before checking quiesced flag */
  139. if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
  140. return;
  141. hctx->run++;
  142. /*
  143. * If we have previous entries on our dispatch list, grab them first for
  144. * more fair dispatch.
  145. */
  146. if (!list_empty_careful(&hctx->dispatch)) {
  147. spin_lock(&hctx->lock);
  148. if (!list_empty(&hctx->dispatch))
  149. list_splice_init(&hctx->dispatch, &rq_list);
  150. spin_unlock(&hctx->lock);
  151. }
  152. /*
  153. * Only ask the scheduler for requests, if we didn't have residual
  154. * requests from the dispatch list. This is to avoid the case where
  155. * we only ever dispatch a fraction of the requests available because
  156. * of low device queue depth. Once we pull requests out of the IO
  157. * scheduler, we can no longer merge or sort them. So it's best to
  158. * leave them there for as long as we can. Mark the hw queue as
  159. * needing a restart in that case.
  160. *
  161. * We want to dispatch from the scheduler if there was nothing
  162. * on the dispatch list or we were able to dispatch from the
  163. * dispatch list.
  164. */
  165. if (!list_empty(&rq_list)) {
  166. blk_mq_sched_mark_restart_hctx(hctx);
  167. if (blk_mq_dispatch_rq_list(q, &rq_list, false)) {
  168. if (has_sched_dispatch)
  169. blk_mq_do_dispatch_sched(hctx);
  170. else
  171. blk_mq_do_dispatch_ctx(hctx);
  172. }
  173. } else if (has_sched_dispatch) {
  174. blk_mq_do_dispatch_sched(hctx);
  175. } else if (hctx->dispatch_busy) {
  176. /* dequeue request one by one from sw queue if queue is busy */
  177. blk_mq_do_dispatch_ctx(hctx);
  178. } else {
  179. blk_mq_flush_busy_ctxs(hctx, &rq_list);
  180. blk_mq_dispatch_rq_list(q, &rq_list, false);
  181. }
  182. }
  183. bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
  184. struct request **merged_request)
  185. {
  186. struct request *rq;
  187. switch (elv_merge(q, &rq, bio)) {
  188. case ELEVATOR_BACK_MERGE:
  189. if (!blk_mq_sched_allow_merge(q, rq, bio))
  190. return false;
  191. if (!bio_attempt_back_merge(q, rq, bio))
  192. return false;
  193. *merged_request = attempt_back_merge(q, rq);
  194. if (!*merged_request)
  195. elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
  196. return true;
  197. case ELEVATOR_FRONT_MERGE:
  198. if (!blk_mq_sched_allow_merge(q, rq, bio))
  199. return false;
  200. if (!bio_attempt_front_merge(q, rq, bio))
  201. return false;
  202. *merged_request = attempt_front_merge(q, rq);
  203. if (!*merged_request)
  204. elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
  205. return true;
  206. case ELEVATOR_DISCARD_MERGE:
  207. return bio_attempt_discard_merge(q, rq, bio);
  208. default:
  209. return false;
  210. }
  211. }
  212. EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
  213. /*
  214. * Iterate list of requests and see if we can merge this bio with any
  215. * of them.
  216. */
  217. bool blk_mq_bio_list_merge(struct request_queue *q, struct list_head *list,
  218. struct bio *bio)
  219. {
  220. struct request *rq;
  221. int checked = 8;
  222. list_for_each_entry_reverse(rq, list, queuelist) {
  223. bool merged = false;
  224. if (!checked--)
  225. break;
  226. if (!blk_rq_merge_ok(rq, bio))
  227. continue;
  228. switch (blk_try_merge(rq, bio)) {
  229. case ELEVATOR_BACK_MERGE:
  230. if (blk_mq_sched_allow_merge(q, rq, bio))
  231. merged = bio_attempt_back_merge(q, rq, bio);
  232. break;
  233. case ELEVATOR_FRONT_MERGE:
  234. if (blk_mq_sched_allow_merge(q, rq, bio))
  235. merged = bio_attempt_front_merge(q, rq, bio);
  236. break;
  237. case ELEVATOR_DISCARD_MERGE:
  238. merged = bio_attempt_discard_merge(q, rq, bio);
  239. break;
  240. default:
  241. continue;
  242. }
  243. return merged;
  244. }
  245. return false;
  246. }
  247. EXPORT_SYMBOL_GPL(blk_mq_bio_list_merge);
  248. /*
  249. * Reverse check our software queue for entries that we could potentially
  250. * merge with. Currently includes a hand-wavy stop count of 8, to not spend
  251. * too much time checking for merges.
  252. */
  253. static bool blk_mq_attempt_merge(struct request_queue *q,
  254. struct blk_mq_ctx *ctx, struct bio *bio)
  255. {
  256. lockdep_assert_held(&ctx->lock);
  257. if (blk_mq_bio_list_merge(q, &ctx->rq_list, bio)) {
  258. ctx->rq_merged++;
  259. return true;
  260. }
  261. return false;
  262. }
  263. bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
  264. {
  265. struct elevator_queue *e = q->elevator;
  266. struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
  267. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  268. bool ret = false;
  269. if (e && e->type->ops.mq.bio_merge) {
  270. blk_mq_put_ctx(ctx);
  271. return e->type->ops.mq.bio_merge(hctx, bio);
  272. }
  273. if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
  274. !list_empty_careful(&ctx->rq_list)) {
  275. /* default per sw-queue merge */
  276. spin_lock(&ctx->lock);
  277. ret = blk_mq_attempt_merge(q, ctx, bio);
  278. spin_unlock(&ctx->lock);
  279. }
  280. blk_mq_put_ctx(ctx);
  281. return ret;
  282. }
  283. bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
  284. {
  285. return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
  286. }
  287. EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
  288. void blk_mq_sched_request_inserted(struct request *rq)
  289. {
  290. trace_block_rq_insert(rq->q, rq);
  291. }
  292. EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
  293. static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
  294. bool has_sched,
  295. struct request *rq)
  296. {
  297. /* dispatch flush rq directly */
  298. if (rq->rq_flags & RQF_FLUSH_SEQ) {
  299. spin_lock(&hctx->lock);
  300. list_add(&rq->queuelist, &hctx->dispatch);
  301. spin_unlock(&hctx->lock);
  302. return true;
  303. }
  304. if (has_sched)
  305. rq->rq_flags |= RQF_SORTED;
  306. return false;
  307. }
  308. void blk_mq_sched_insert_request(struct request *rq, bool at_head,
  309. bool run_queue, bool async)
  310. {
  311. struct request_queue *q = rq->q;
  312. struct elevator_queue *e = q->elevator;
  313. struct blk_mq_ctx *ctx = rq->mq_ctx;
  314. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  315. /* flush rq in flush machinery need to be dispatched directly */
  316. if (!(rq->rq_flags & RQF_FLUSH_SEQ) && op_is_flush(rq->cmd_flags)) {
  317. blk_insert_flush(rq);
  318. goto run;
  319. }
  320. WARN_ON(e && (rq->tag != -1));
  321. if (blk_mq_sched_bypass_insert(hctx, !!e, rq))
  322. goto run;
  323. if (e && e->type->ops.mq.insert_requests) {
  324. LIST_HEAD(list);
  325. list_add(&rq->queuelist, &list);
  326. e->type->ops.mq.insert_requests(hctx, &list, at_head);
  327. } else {
  328. spin_lock(&ctx->lock);
  329. __blk_mq_insert_request(hctx, rq, at_head);
  330. spin_unlock(&ctx->lock);
  331. }
  332. run:
  333. if (run_queue)
  334. blk_mq_run_hw_queue(hctx, async);
  335. }
  336. void blk_mq_sched_insert_requests(struct request_queue *q,
  337. struct blk_mq_ctx *ctx,
  338. struct list_head *list, bool run_queue_async)
  339. {
  340. struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
  341. struct elevator_queue *e = hctx->queue->elevator;
  342. if (e && e->type->ops.mq.insert_requests)
  343. e->type->ops.mq.insert_requests(hctx, list, false);
  344. else {
  345. /*
  346. * try to issue requests directly if the hw queue isn't
  347. * busy in case of 'none' scheduler, and this way may save
  348. * us one extra enqueue & dequeue to sw queue.
  349. */
  350. if (!hctx->dispatch_busy && !e && !run_queue_async) {
  351. blk_mq_try_issue_list_directly(hctx, list);
  352. if (list_empty(list))
  353. return;
  354. }
  355. blk_mq_insert_requests(hctx, ctx, list);
  356. }
  357. blk_mq_run_hw_queue(hctx, run_queue_async);
  358. }
  359. static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
  360. struct blk_mq_hw_ctx *hctx,
  361. unsigned int hctx_idx)
  362. {
  363. if (hctx->sched_tags) {
  364. blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
  365. blk_mq_free_rq_map(hctx->sched_tags);
  366. hctx->sched_tags = NULL;
  367. }
  368. }
  369. static int blk_mq_sched_alloc_tags(struct request_queue *q,
  370. struct blk_mq_hw_ctx *hctx,
  371. unsigned int hctx_idx)
  372. {
  373. struct blk_mq_tag_set *set = q->tag_set;
  374. int ret;
  375. hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
  376. set->reserved_tags);
  377. if (!hctx->sched_tags)
  378. return -ENOMEM;
  379. ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
  380. if (ret)
  381. blk_mq_sched_free_tags(set, hctx, hctx_idx);
  382. return ret;
  383. }
  384. static void blk_mq_sched_tags_teardown(struct request_queue *q)
  385. {
  386. struct blk_mq_tag_set *set = q->tag_set;
  387. struct blk_mq_hw_ctx *hctx;
  388. int i;
  389. queue_for_each_hw_ctx(q, hctx, i)
  390. blk_mq_sched_free_tags(set, hctx, i);
  391. }
  392. int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
  393. {
  394. struct blk_mq_hw_ctx *hctx;
  395. struct elevator_queue *eq;
  396. unsigned int i;
  397. int ret;
  398. if (!e) {
  399. q->elevator = NULL;
  400. q->nr_requests = q->tag_set->queue_depth;
  401. return 0;
  402. }
  403. /*
  404. * Default to double of smaller one between hw queue_depth and 128,
  405. * since we don't split into sync/async like the old code did.
  406. * Additionally, this is a per-hw queue depth.
  407. */
  408. q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
  409. BLKDEV_MAX_RQ);
  410. queue_for_each_hw_ctx(q, hctx, i) {
  411. ret = blk_mq_sched_alloc_tags(q, hctx, i);
  412. if (ret)
  413. goto err;
  414. }
  415. ret = e->ops.mq.init_sched(q, e);
  416. if (ret)
  417. goto err;
  418. blk_mq_debugfs_register_sched(q);
  419. queue_for_each_hw_ctx(q, hctx, i) {
  420. if (e->ops.mq.init_hctx) {
  421. ret = e->ops.mq.init_hctx(hctx, i);
  422. if (ret) {
  423. eq = q->elevator;
  424. blk_mq_exit_sched(q, eq);
  425. kobject_put(&eq->kobj);
  426. return ret;
  427. }
  428. }
  429. blk_mq_debugfs_register_sched_hctx(q, hctx);
  430. }
  431. return 0;
  432. err:
  433. blk_mq_sched_tags_teardown(q);
  434. q->elevator = NULL;
  435. return ret;
  436. }
  437. void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
  438. {
  439. struct blk_mq_hw_ctx *hctx;
  440. unsigned int i;
  441. queue_for_each_hw_ctx(q, hctx, i) {
  442. blk_mq_debugfs_unregister_sched_hctx(hctx);
  443. if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
  444. e->type->ops.mq.exit_hctx(hctx, i);
  445. hctx->sched_data = NULL;
  446. }
  447. }
  448. blk_mq_debugfs_unregister_sched(q);
  449. if (e->type->ops.mq.exit_sched)
  450. e->type->ops.mq.exit_sched(e);
  451. blk_mq_sched_tags_teardown(q);
  452. q->elevator = NULL;
  453. }