blk-mq-sched.c 13 KB

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