mq-deadline.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler,
  4. * for the blk-mq scheduling framework
  5. *
  6. * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/fs.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/bio.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/compiler.h>
  16. #include <linux/rbtree.h>
  17. #include <linux/sbitmap.h>
  18. #include <trace/events/block.h>
  19. #include "elevator.h"
  20. #include "blk.h"
  21. #include "blk-mq.h"
  22. #include "blk-mq-debugfs.h"
  23. #include "blk-mq-sched.h"
  24. /*
  25. * See Documentation/block/deadline-iosched.rst
  26. */
  27. static const int read_expire = HZ / 2; /* max time before a read is submitted. */
  28. static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
  29. /*
  30. * Time after which to dispatch lower priority requests even if higher
  31. * priority requests are pending.
  32. */
  33. static const int prio_aging_expire = 10 * HZ;
  34. static const int writes_starved = 2; /* max times reads can starve a write */
  35. static const int fifo_batch = 16; /* # of sequential requests treated as one
  36. by the above parameters. For throughput. */
  37. enum dd_data_dir {
  38. DD_READ = READ,
  39. DD_WRITE = WRITE,
  40. };
  41. enum { DD_DIR_COUNT = 2 };
  42. enum dd_prio {
  43. DD_RT_PRIO = 0,
  44. DD_BE_PRIO = 1,
  45. DD_IDLE_PRIO = 2,
  46. DD_PRIO_MAX = 2,
  47. };
  48. enum { DD_PRIO_COUNT = 3 };
  49. /*
  50. * I/O statistics per I/O priority. It is fine if these counters overflow.
  51. * What matters is that these counters are at least as wide as
  52. * log2(max_outstanding_requests).
  53. */
  54. struct io_stats_per_prio {
  55. uint32_t inserted;
  56. uint32_t merged;
  57. uint32_t dispatched;
  58. atomic_t completed;
  59. };
  60. /*
  61. * Deadline scheduler data per I/O priority (enum dd_prio). Requests are
  62. * present on both sort_list[] and fifo_list[].
  63. */
  64. struct dd_per_prio {
  65. struct list_head dispatch;
  66. struct rb_root sort_list[DD_DIR_COUNT];
  67. struct list_head fifo_list[DD_DIR_COUNT];
  68. /* Position of the most recently dispatched request. */
  69. sector_t latest_pos[DD_DIR_COUNT];
  70. struct io_stats_per_prio stats;
  71. };
  72. struct deadline_data {
  73. /*
  74. * run time data
  75. */
  76. struct dd_per_prio per_prio[DD_PRIO_COUNT];
  77. /* Data direction of latest dispatched request. */
  78. enum dd_data_dir last_dir;
  79. unsigned int batching; /* number of sequential requests made */
  80. unsigned int starved; /* times reads have starved writes */
  81. /*
  82. * settings that change how the i/o scheduler behaves
  83. */
  84. int fifo_expire[DD_DIR_COUNT];
  85. int fifo_batch;
  86. int writes_starved;
  87. int front_merges;
  88. u32 async_depth;
  89. int prio_aging_expire;
  90. spinlock_t lock;
  91. };
  92. /* Maps an I/O priority class to a deadline scheduler priority. */
  93. static const enum dd_prio ioprio_class_to_prio[] = {
  94. [IOPRIO_CLASS_NONE] = DD_BE_PRIO,
  95. [IOPRIO_CLASS_RT] = DD_RT_PRIO,
  96. [IOPRIO_CLASS_BE] = DD_BE_PRIO,
  97. [IOPRIO_CLASS_IDLE] = DD_IDLE_PRIO,
  98. };
  99. static inline struct rb_root *
  100. deadline_rb_root(struct dd_per_prio *per_prio, struct request *rq)
  101. {
  102. return &per_prio->sort_list[rq_data_dir(rq)];
  103. }
  104. /*
  105. * Returns the I/O priority class (IOPRIO_CLASS_*) that has been assigned to a
  106. * request.
  107. */
  108. static u8 dd_rq_ioclass(struct request *rq)
  109. {
  110. return IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
  111. }
  112. /*
  113. * Return the first request for which blk_rq_pos() >= @pos.
  114. */
  115. static inline struct request *deadline_from_pos(struct dd_per_prio *per_prio,
  116. enum dd_data_dir data_dir, sector_t pos)
  117. {
  118. struct rb_node *node = per_prio->sort_list[data_dir].rb_node;
  119. struct request *rq, *res = NULL;
  120. if (!node)
  121. return NULL;
  122. rq = rb_entry_rq(node);
  123. while (node) {
  124. rq = rb_entry_rq(node);
  125. if (blk_rq_pos(rq) >= pos) {
  126. res = rq;
  127. node = node->rb_left;
  128. } else {
  129. node = node->rb_right;
  130. }
  131. }
  132. return res;
  133. }
  134. static void
  135. deadline_add_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
  136. {
  137. struct rb_root *root = deadline_rb_root(per_prio, rq);
  138. elv_rb_add(root, rq);
  139. }
  140. static inline void
  141. deadline_del_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
  142. {
  143. elv_rb_del(deadline_rb_root(per_prio, rq), rq);
  144. }
  145. /*
  146. * remove rq from rbtree and fifo.
  147. */
  148. static void deadline_remove_request(struct request_queue *q,
  149. struct dd_per_prio *per_prio,
  150. struct request *rq)
  151. {
  152. list_del_init(&rq->queuelist);
  153. /*
  154. * We might not be on the rbtree, if we are doing an insert merge
  155. */
  156. if (!RB_EMPTY_NODE(&rq->rb_node))
  157. deadline_del_rq_rb(per_prio, rq);
  158. elv_rqhash_del(q, rq);
  159. if (q->last_merge == rq)
  160. q->last_merge = NULL;
  161. }
  162. static void dd_request_merged(struct request_queue *q, struct request *req,
  163. enum elv_merge type)
  164. {
  165. struct deadline_data *dd = q->elevator->elevator_data;
  166. const u8 ioprio_class = dd_rq_ioclass(req);
  167. const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
  168. struct dd_per_prio *per_prio = &dd->per_prio[prio];
  169. /*
  170. * if the merge was a front merge, we need to reposition request
  171. */
  172. if (type == ELEVATOR_FRONT_MERGE) {
  173. elv_rb_del(deadline_rb_root(per_prio, req), req);
  174. deadline_add_rq_rb(per_prio, req);
  175. }
  176. }
  177. /*
  178. * Callback function that is invoked after @next has been merged into @req.
  179. */
  180. static void dd_merged_requests(struct request_queue *q, struct request *req,
  181. struct request *next)
  182. {
  183. struct deadline_data *dd = q->elevator->elevator_data;
  184. const u8 ioprio_class = dd_rq_ioclass(next);
  185. const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
  186. lockdep_assert_held(&dd->lock);
  187. dd->per_prio[prio].stats.merged++;
  188. /*
  189. * if next expires before rq, assign its expire time to rq
  190. * and move into next position (next will be deleted) in fifo
  191. */
  192. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
  193. if (time_before((unsigned long)next->fifo_time,
  194. (unsigned long)req->fifo_time)) {
  195. list_move(&req->queuelist, &next->queuelist);
  196. req->fifo_time = next->fifo_time;
  197. }
  198. }
  199. /*
  200. * kill knowledge of next, this one is a goner
  201. */
  202. deadline_remove_request(q, &dd->per_prio[prio], next);
  203. }
  204. /*
  205. * move an entry to dispatch queue
  206. */
  207. static void
  208. deadline_move_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
  209. struct request *rq)
  210. {
  211. /*
  212. * take it off the sort and fifo list
  213. */
  214. deadline_remove_request(rq->q, per_prio, rq);
  215. }
  216. /* Number of requests queued for a given priority level. */
  217. static u32 dd_queued(struct deadline_data *dd, enum dd_prio prio)
  218. {
  219. const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats;
  220. lockdep_assert_held(&dd->lock);
  221. return stats->inserted - atomic_read(&stats->completed);
  222. }
  223. /*
  224. * deadline_check_fifo returns true if and only if there are expired requests
  225. * in the FIFO list. Requires !list_empty(&dd->fifo_list[data_dir]).
  226. */
  227. static inline bool deadline_check_fifo(struct dd_per_prio *per_prio,
  228. enum dd_data_dir data_dir)
  229. {
  230. struct request *rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next);
  231. return time_is_before_eq_jiffies((unsigned long)rq->fifo_time);
  232. }
  233. /*
  234. * For the specified data direction, return the next request to
  235. * dispatch using arrival ordered lists.
  236. */
  237. static struct request *
  238. deadline_fifo_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
  239. enum dd_data_dir data_dir)
  240. {
  241. if (list_empty(&per_prio->fifo_list[data_dir]))
  242. return NULL;
  243. return rq_entry_fifo(per_prio->fifo_list[data_dir].next);
  244. }
  245. /*
  246. * For the specified data direction, return the next request to
  247. * dispatch using sector position sorted lists.
  248. */
  249. static struct request *
  250. deadline_next_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
  251. enum dd_data_dir data_dir)
  252. {
  253. return deadline_from_pos(per_prio, data_dir,
  254. per_prio->latest_pos[data_dir]);
  255. }
  256. /*
  257. * Returns true if and only if @rq started after @latest_start where
  258. * @latest_start is in jiffies.
  259. */
  260. static bool started_after(struct deadline_data *dd, struct request *rq,
  261. unsigned long latest_start)
  262. {
  263. unsigned long start_time = (unsigned long)rq->fifo_time;
  264. start_time -= dd->fifo_expire[rq_data_dir(rq)];
  265. return time_after(start_time, latest_start);
  266. }
  267. /*
  268. * deadline_dispatch_requests selects the best request according to
  269. * read/write expire, fifo_batch, etc and with a start time <= @latest_start.
  270. */
  271. static struct request *__dd_dispatch_request(struct deadline_data *dd,
  272. struct dd_per_prio *per_prio,
  273. unsigned long latest_start)
  274. {
  275. struct request *rq, *next_rq;
  276. enum dd_data_dir data_dir;
  277. enum dd_prio prio;
  278. u8 ioprio_class;
  279. lockdep_assert_held(&dd->lock);
  280. if (!list_empty(&per_prio->dispatch)) {
  281. rq = list_first_entry(&per_prio->dispatch, struct request,
  282. queuelist);
  283. if (started_after(dd, rq, latest_start))
  284. return NULL;
  285. list_del_init(&rq->queuelist);
  286. data_dir = rq_data_dir(rq);
  287. goto done;
  288. }
  289. /*
  290. * batches are currently reads XOR writes
  291. */
  292. rq = deadline_next_request(dd, per_prio, dd->last_dir);
  293. if (rq && dd->batching < dd->fifo_batch) {
  294. /* we have a next request and are still entitled to batch */
  295. data_dir = rq_data_dir(rq);
  296. goto dispatch_request;
  297. }
  298. /*
  299. * at this point we are not running a batch. select the appropriate
  300. * data direction (read / write)
  301. */
  302. if (!list_empty(&per_prio->fifo_list[DD_READ])) {
  303. BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_READ]));
  304. if (deadline_fifo_request(dd, per_prio, DD_WRITE) &&
  305. (dd->starved++ >= dd->writes_starved))
  306. goto dispatch_writes;
  307. data_dir = DD_READ;
  308. goto dispatch_find_request;
  309. }
  310. /*
  311. * there are either no reads or writes have been starved
  312. */
  313. if (!list_empty(&per_prio->fifo_list[DD_WRITE])) {
  314. dispatch_writes:
  315. BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_WRITE]));
  316. dd->starved = 0;
  317. data_dir = DD_WRITE;
  318. goto dispatch_find_request;
  319. }
  320. return NULL;
  321. dispatch_find_request:
  322. /*
  323. * we are not running a batch, find best request for selected data_dir
  324. */
  325. next_rq = deadline_next_request(dd, per_prio, data_dir);
  326. if (deadline_check_fifo(per_prio, data_dir) || !next_rq) {
  327. /*
  328. * A deadline has expired, the last request was in the other
  329. * direction, or we have run out of higher-sectored requests.
  330. * Start again from the request with the earliest expiry time.
  331. */
  332. rq = deadline_fifo_request(dd, per_prio, data_dir);
  333. } else {
  334. /*
  335. * The last req was the same dir and we have a next request in
  336. * sort order. No expired requests so continue on from here.
  337. */
  338. rq = next_rq;
  339. }
  340. if (!rq)
  341. return NULL;
  342. dd->last_dir = data_dir;
  343. dd->batching = 0;
  344. dispatch_request:
  345. if (started_after(dd, rq, latest_start))
  346. return NULL;
  347. /*
  348. * rq is the selected appropriate request.
  349. */
  350. dd->batching++;
  351. deadline_move_request(dd, per_prio, rq);
  352. done:
  353. ioprio_class = dd_rq_ioclass(rq);
  354. prio = ioprio_class_to_prio[ioprio_class];
  355. dd->per_prio[prio].latest_pos[data_dir] = blk_rq_pos(rq);
  356. dd->per_prio[prio].stats.dispatched++;
  357. rq->rq_flags |= RQF_STARTED;
  358. return rq;
  359. }
  360. /*
  361. * Check whether there are any requests with priority other than DD_RT_PRIO
  362. * that were inserted more than prio_aging_expire jiffies ago.
  363. */
  364. static struct request *dd_dispatch_prio_aged_requests(struct deadline_data *dd,
  365. unsigned long now)
  366. {
  367. struct request *rq;
  368. enum dd_prio prio;
  369. int prio_cnt;
  370. lockdep_assert_held(&dd->lock);
  371. prio_cnt = !!dd_queued(dd, DD_RT_PRIO) + !!dd_queued(dd, DD_BE_PRIO) +
  372. !!dd_queued(dd, DD_IDLE_PRIO);
  373. if (prio_cnt < 2)
  374. return NULL;
  375. for (prio = DD_BE_PRIO; prio <= DD_PRIO_MAX; prio++) {
  376. rq = __dd_dispatch_request(dd, &dd->per_prio[prio],
  377. now - dd->prio_aging_expire);
  378. if (rq)
  379. return rq;
  380. }
  381. return NULL;
  382. }
  383. /*
  384. * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests().
  385. *
  386. * One confusing aspect here is that we get called for a specific
  387. * hardware queue, but we may return a request that is for a
  388. * different hardware queue. This is because mq-deadline has shared
  389. * state for all hardware queues, in terms of sorting, FIFOs, etc.
  390. */
  391. static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
  392. {
  393. struct deadline_data *dd = hctx->queue->elevator->elevator_data;
  394. const unsigned long now = jiffies;
  395. struct request *rq;
  396. enum dd_prio prio;
  397. spin_lock(&dd->lock);
  398. rq = dd_dispatch_prio_aged_requests(dd, now);
  399. if (rq)
  400. goto unlock;
  401. /*
  402. * Next, dispatch requests in priority order. Ignore lower priority
  403. * requests if any higher priority requests are pending.
  404. */
  405. for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
  406. rq = __dd_dispatch_request(dd, &dd->per_prio[prio], now);
  407. if (rq || dd_queued(dd, prio))
  408. break;
  409. }
  410. unlock:
  411. spin_unlock(&dd->lock);
  412. return rq;
  413. }
  414. /*
  415. * Called by __blk_mq_alloc_request(). The shallow_depth value set by this
  416. * function is used by __blk_mq_get_tag().
  417. */
  418. static void dd_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data)
  419. {
  420. struct deadline_data *dd = data->q->elevator->elevator_data;
  421. /* Do not throttle synchronous reads. */
  422. if (op_is_sync(opf) && !op_is_write(opf))
  423. return;
  424. /*
  425. * Throttle asynchronous requests and writes such that these requests
  426. * do not block the allocation of synchronous requests.
  427. */
  428. data->shallow_depth = dd->async_depth;
  429. }
  430. /* Called by blk_mq_update_nr_requests(). */
  431. static void dd_depth_updated(struct blk_mq_hw_ctx *hctx)
  432. {
  433. struct request_queue *q = hctx->queue;
  434. struct deadline_data *dd = q->elevator->elevator_data;
  435. struct blk_mq_tags *tags = hctx->sched_tags;
  436. dd->async_depth = q->nr_requests;
  437. sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, 1);
  438. }
  439. /* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */
  440. static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  441. {
  442. dd_depth_updated(hctx);
  443. return 0;
  444. }
  445. static void dd_exit_sched(struct elevator_queue *e)
  446. {
  447. struct deadline_data *dd = e->elevator_data;
  448. enum dd_prio prio;
  449. for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
  450. struct dd_per_prio *per_prio = &dd->per_prio[prio];
  451. const struct io_stats_per_prio *stats = &per_prio->stats;
  452. uint32_t queued;
  453. WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ]));
  454. WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE]));
  455. spin_lock(&dd->lock);
  456. queued = dd_queued(dd, prio);
  457. spin_unlock(&dd->lock);
  458. WARN_ONCE(queued != 0,
  459. "statistics for priority %d: i %u m %u d %u c %u\n",
  460. prio, stats->inserted, stats->merged,
  461. stats->dispatched, atomic_read(&stats->completed));
  462. }
  463. kfree(dd);
  464. }
  465. /*
  466. * initialize elevator private data (deadline_data).
  467. */
  468. static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
  469. {
  470. struct deadline_data *dd;
  471. struct elevator_queue *eq;
  472. enum dd_prio prio;
  473. int ret = -ENOMEM;
  474. eq = elevator_alloc(q, e);
  475. if (!eq)
  476. return ret;
  477. dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
  478. if (!dd)
  479. goto put_eq;
  480. eq->elevator_data = dd;
  481. for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
  482. struct dd_per_prio *per_prio = &dd->per_prio[prio];
  483. INIT_LIST_HEAD(&per_prio->dispatch);
  484. INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]);
  485. INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]);
  486. per_prio->sort_list[DD_READ] = RB_ROOT;
  487. per_prio->sort_list[DD_WRITE] = RB_ROOT;
  488. }
  489. dd->fifo_expire[DD_READ] = read_expire;
  490. dd->fifo_expire[DD_WRITE] = write_expire;
  491. dd->writes_starved = writes_starved;
  492. dd->front_merges = 1;
  493. dd->last_dir = DD_WRITE;
  494. dd->fifo_batch = fifo_batch;
  495. dd->prio_aging_expire = prio_aging_expire;
  496. spin_lock_init(&dd->lock);
  497. /* We dispatch from request queue wide instead of hw queue */
  498. blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q);
  499. q->elevator = eq;
  500. return 0;
  501. put_eq:
  502. kobject_put(&eq->kobj);
  503. return ret;
  504. }
  505. /*
  506. * Try to merge @bio into an existing request. If @bio has been merged into
  507. * an existing request, store the pointer to that request into *@rq.
  508. */
  509. static int dd_request_merge(struct request_queue *q, struct request **rq,
  510. struct bio *bio)
  511. {
  512. struct deadline_data *dd = q->elevator->elevator_data;
  513. const u8 ioprio_class = IOPRIO_PRIO_CLASS(bio->bi_ioprio);
  514. const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
  515. struct dd_per_prio *per_prio = &dd->per_prio[prio];
  516. sector_t sector = bio_end_sector(bio);
  517. struct request *__rq;
  518. if (!dd->front_merges)
  519. return ELEVATOR_NO_MERGE;
  520. __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector);
  521. if (__rq) {
  522. BUG_ON(sector != blk_rq_pos(__rq));
  523. if (elv_bio_merge_ok(__rq, bio)) {
  524. *rq = __rq;
  525. if (blk_discard_mergable(__rq))
  526. return ELEVATOR_DISCARD_MERGE;
  527. return ELEVATOR_FRONT_MERGE;
  528. }
  529. }
  530. return ELEVATOR_NO_MERGE;
  531. }
  532. /*
  533. * Attempt to merge a bio into an existing request. This function is called
  534. * before @bio is associated with a request.
  535. */
  536. static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
  537. unsigned int nr_segs)
  538. {
  539. struct deadline_data *dd = q->elevator->elevator_data;
  540. struct request *free = NULL;
  541. bool ret;
  542. spin_lock(&dd->lock);
  543. ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
  544. spin_unlock(&dd->lock);
  545. if (free)
  546. blk_mq_free_request(free);
  547. return ret;
  548. }
  549. /*
  550. * add rq to rbtree and fifo
  551. */
  552. static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  553. blk_insert_t flags, struct list_head *free)
  554. {
  555. struct request_queue *q = hctx->queue;
  556. struct deadline_data *dd = q->elevator->elevator_data;
  557. const enum dd_data_dir data_dir = rq_data_dir(rq);
  558. u16 ioprio = req_get_ioprio(rq);
  559. u8 ioprio_class = IOPRIO_PRIO_CLASS(ioprio);
  560. struct dd_per_prio *per_prio;
  561. enum dd_prio prio;
  562. lockdep_assert_held(&dd->lock);
  563. prio = ioprio_class_to_prio[ioprio_class];
  564. per_prio = &dd->per_prio[prio];
  565. if (!rq->elv.priv[0])
  566. per_prio->stats.inserted++;
  567. rq->elv.priv[0] = per_prio;
  568. if (blk_mq_sched_try_insert_merge(q, rq, free))
  569. return;
  570. trace_block_rq_insert(rq);
  571. if (flags & BLK_MQ_INSERT_AT_HEAD) {
  572. list_add(&rq->queuelist, &per_prio->dispatch);
  573. rq->fifo_time = jiffies;
  574. } else {
  575. struct list_head *insert_before;
  576. deadline_add_rq_rb(per_prio, rq);
  577. if (rq_mergeable(rq)) {
  578. elv_rqhash_add(q, rq);
  579. if (!q->last_merge)
  580. q->last_merge = rq;
  581. }
  582. /*
  583. * set expire time and add to fifo list
  584. */
  585. rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
  586. insert_before = &per_prio->fifo_list[data_dir];
  587. list_add_tail(&rq->queuelist, insert_before);
  588. }
  589. }
  590. /*
  591. * Called from blk_mq_insert_request() or blk_mq_dispatch_plug_list().
  592. */
  593. static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
  594. struct list_head *list,
  595. blk_insert_t flags)
  596. {
  597. struct request_queue *q = hctx->queue;
  598. struct deadline_data *dd = q->elevator->elevator_data;
  599. LIST_HEAD(free);
  600. spin_lock(&dd->lock);
  601. while (!list_empty(list)) {
  602. struct request *rq;
  603. rq = list_first_entry(list, struct request, queuelist);
  604. list_del_init(&rq->queuelist);
  605. dd_insert_request(hctx, rq, flags, &free);
  606. }
  607. spin_unlock(&dd->lock);
  608. blk_mq_free_requests(&free);
  609. }
  610. /* Callback from inside blk_mq_rq_ctx_init(). */
  611. static void dd_prepare_request(struct request *rq)
  612. {
  613. rq->elv.priv[0] = NULL;
  614. }
  615. /*
  616. * Callback from inside blk_mq_free_request().
  617. */
  618. static void dd_finish_request(struct request *rq)
  619. {
  620. struct dd_per_prio *per_prio = rq->elv.priv[0];
  621. /*
  622. * The block layer core may call dd_finish_request() without having
  623. * called dd_insert_requests(). Skip requests that bypassed I/O
  624. * scheduling. See also blk_mq_request_bypass_insert().
  625. */
  626. if (per_prio)
  627. atomic_inc(&per_prio->stats.completed);
  628. }
  629. static bool dd_has_work_for_prio(struct dd_per_prio *per_prio)
  630. {
  631. return !list_empty_careful(&per_prio->dispatch) ||
  632. !list_empty_careful(&per_prio->fifo_list[DD_READ]) ||
  633. !list_empty_careful(&per_prio->fifo_list[DD_WRITE]);
  634. }
  635. static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
  636. {
  637. struct deadline_data *dd = hctx->queue->elevator->elevator_data;
  638. enum dd_prio prio;
  639. for (prio = 0; prio <= DD_PRIO_MAX; prio++)
  640. if (dd_has_work_for_prio(&dd->per_prio[prio]))
  641. return true;
  642. return false;
  643. }
  644. /*
  645. * sysfs parts below
  646. */
  647. #define SHOW_INT(__FUNC, __VAR) \
  648. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  649. { \
  650. struct deadline_data *dd = e->elevator_data; \
  651. \
  652. return sysfs_emit(page, "%d\n", __VAR); \
  653. }
  654. #define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR))
  655. SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]);
  656. SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]);
  657. SHOW_JIFFIES(deadline_prio_aging_expire_show, dd->prio_aging_expire);
  658. SHOW_INT(deadline_writes_starved_show, dd->writes_starved);
  659. SHOW_INT(deadline_front_merges_show, dd->front_merges);
  660. SHOW_INT(deadline_async_depth_show, dd->async_depth);
  661. SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch);
  662. #undef SHOW_INT
  663. #undef SHOW_JIFFIES
  664. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  665. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  666. { \
  667. struct deadline_data *dd = e->elevator_data; \
  668. int __data, __ret; \
  669. \
  670. __ret = kstrtoint(page, 0, &__data); \
  671. if (__ret < 0) \
  672. return __ret; \
  673. if (__data < (MIN)) \
  674. __data = (MIN); \
  675. else if (__data > (MAX)) \
  676. __data = (MAX); \
  677. *(__PTR) = __CONV(__data); \
  678. return count; \
  679. }
  680. #define STORE_INT(__FUNC, __PTR, MIN, MAX) \
  681. STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, )
  682. #define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \
  683. STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies)
  684. STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX);
  685. STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX);
  686. STORE_JIFFIES(deadline_prio_aging_expire_store, &dd->prio_aging_expire, 0, INT_MAX);
  687. STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX);
  688. STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1);
  689. STORE_INT(deadline_async_depth_store, &dd->async_depth, 1, INT_MAX);
  690. STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX);
  691. #undef STORE_FUNCTION
  692. #undef STORE_INT
  693. #undef STORE_JIFFIES
  694. #define DD_ATTR(name) \
  695. __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
  696. static struct elv_fs_entry deadline_attrs[] = {
  697. DD_ATTR(read_expire),
  698. DD_ATTR(write_expire),
  699. DD_ATTR(writes_starved),
  700. DD_ATTR(front_merges),
  701. DD_ATTR(async_depth),
  702. DD_ATTR(fifo_batch),
  703. DD_ATTR(prio_aging_expire),
  704. __ATTR_NULL
  705. };
  706. #ifdef CONFIG_BLK_DEBUG_FS
  707. #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \
  708. static void *deadline_##name##_fifo_start(struct seq_file *m, \
  709. loff_t *pos) \
  710. __acquires(&dd->lock) \
  711. { \
  712. struct request_queue *q = m->private; \
  713. struct deadline_data *dd = q->elevator->elevator_data; \
  714. struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
  715. \
  716. spin_lock(&dd->lock); \
  717. return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \
  718. } \
  719. \
  720. static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
  721. loff_t *pos) \
  722. { \
  723. struct request_queue *q = m->private; \
  724. struct deadline_data *dd = q->elevator->elevator_data; \
  725. struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
  726. \
  727. return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \
  728. } \
  729. \
  730. static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
  731. __releases(&dd->lock) \
  732. { \
  733. struct request_queue *q = m->private; \
  734. struct deadline_data *dd = q->elevator->elevator_data; \
  735. \
  736. spin_unlock(&dd->lock); \
  737. } \
  738. \
  739. static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
  740. .start = deadline_##name##_fifo_start, \
  741. .next = deadline_##name##_fifo_next, \
  742. .stop = deadline_##name##_fifo_stop, \
  743. .show = blk_mq_debugfs_rq_show, \
  744. }; \
  745. \
  746. static int deadline_##name##_next_rq_show(void *data, \
  747. struct seq_file *m) \
  748. { \
  749. struct request_queue *q = data; \
  750. struct deadline_data *dd = q->elevator->elevator_data; \
  751. struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
  752. struct request *rq; \
  753. \
  754. rq = deadline_from_pos(per_prio, data_dir, \
  755. per_prio->latest_pos[data_dir]); \
  756. if (rq) \
  757. __blk_mq_debugfs_rq_show(m, rq); \
  758. return 0; \
  759. }
  760. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0);
  761. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0);
  762. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1);
  763. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1);
  764. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2);
  765. DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2);
  766. #undef DEADLINE_DEBUGFS_DDIR_ATTRS
  767. static int deadline_batching_show(void *data, struct seq_file *m)
  768. {
  769. struct request_queue *q = data;
  770. struct deadline_data *dd = q->elevator->elevator_data;
  771. seq_printf(m, "%u\n", dd->batching);
  772. return 0;
  773. }
  774. static int deadline_starved_show(void *data, struct seq_file *m)
  775. {
  776. struct request_queue *q = data;
  777. struct deadline_data *dd = q->elevator->elevator_data;
  778. seq_printf(m, "%u\n", dd->starved);
  779. return 0;
  780. }
  781. static int dd_async_depth_show(void *data, struct seq_file *m)
  782. {
  783. struct request_queue *q = data;
  784. struct deadline_data *dd = q->elevator->elevator_data;
  785. seq_printf(m, "%u\n", dd->async_depth);
  786. return 0;
  787. }
  788. static int dd_queued_show(void *data, struct seq_file *m)
  789. {
  790. struct request_queue *q = data;
  791. struct deadline_data *dd = q->elevator->elevator_data;
  792. u32 rt, be, idle;
  793. spin_lock(&dd->lock);
  794. rt = dd_queued(dd, DD_RT_PRIO);
  795. be = dd_queued(dd, DD_BE_PRIO);
  796. idle = dd_queued(dd, DD_IDLE_PRIO);
  797. spin_unlock(&dd->lock);
  798. seq_printf(m, "%u %u %u\n", rt, be, idle);
  799. return 0;
  800. }
  801. /* Number of requests owned by the block driver for a given priority. */
  802. static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio)
  803. {
  804. const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats;
  805. lockdep_assert_held(&dd->lock);
  806. return stats->dispatched + stats->merged -
  807. atomic_read(&stats->completed);
  808. }
  809. static int dd_owned_by_driver_show(void *data, struct seq_file *m)
  810. {
  811. struct request_queue *q = data;
  812. struct deadline_data *dd = q->elevator->elevator_data;
  813. u32 rt, be, idle;
  814. spin_lock(&dd->lock);
  815. rt = dd_owned_by_driver(dd, DD_RT_PRIO);
  816. be = dd_owned_by_driver(dd, DD_BE_PRIO);
  817. idle = dd_owned_by_driver(dd, DD_IDLE_PRIO);
  818. spin_unlock(&dd->lock);
  819. seq_printf(m, "%u %u %u\n", rt, be, idle);
  820. return 0;
  821. }
  822. #define DEADLINE_DISPATCH_ATTR(prio) \
  823. static void *deadline_dispatch##prio##_start(struct seq_file *m, \
  824. loff_t *pos) \
  825. __acquires(&dd->lock) \
  826. { \
  827. struct request_queue *q = m->private; \
  828. struct deadline_data *dd = q->elevator->elevator_data; \
  829. struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
  830. \
  831. spin_lock(&dd->lock); \
  832. return seq_list_start(&per_prio->dispatch, *pos); \
  833. } \
  834. \
  835. static void *deadline_dispatch##prio##_next(struct seq_file *m, \
  836. void *v, loff_t *pos) \
  837. { \
  838. struct request_queue *q = m->private; \
  839. struct deadline_data *dd = q->elevator->elevator_data; \
  840. struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
  841. \
  842. return seq_list_next(v, &per_prio->dispatch, pos); \
  843. } \
  844. \
  845. static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \
  846. __releases(&dd->lock) \
  847. { \
  848. struct request_queue *q = m->private; \
  849. struct deadline_data *dd = q->elevator->elevator_data; \
  850. \
  851. spin_unlock(&dd->lock); \
  852. } \
  853. \
  854. static const struct seq_operations deadline_dispatch##prio##_seq_ops = { \
  855. .start = deadline_dispatch##prio##_start, \
  856. .next = deadline_dispatch##prio##_next, \
  857. .stop = deadline_dispatch##prio##_stop, \
  858. .show = blk_mq_debugfs_rq_show, \
  859. }
  860. DEADLINE_DISPATCH_ATTR(0);
  861. DEADLINE_DISPATCH_ATTR(1);
  862. DEADLINE_DISPATCH_ATTR(2);
  863. #undef DEADLINE_DISPATCH_ATTR
  864. #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
  865. {#name "_fifo_list", 0400, \
  866. .seq_ops = &deadline_##name##_fifo_seq_ops}
  867. #define DEADLINE_NEXT_RQ_ATTR(name) \
  868. {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
  869. static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
  870. DEADLINE_QUEUE_DDIR_ATTRS(read0),
  871. DEADLINE_QUEUE_DDIR_ATTRS(write0),
  872. DEADLINE_QUEUE_DDIR_ATTRS(read1),
  873. DEADLINE_QUEUE_DDIR_ATTRS(write1),
  874. DEADLINE_QUEUE_DDIR_ATTRS(read2),
  875. DEADLINE_QUEUE_DDIR_ATTRS(write2),
  876. DEADLINE_NEXT_RQ_ATTR(read0),
  877. DEADLINE_NEXT_RQ_ATTR(write0),
  878. DEADLINE_NEXT_RQ_ATTR(read1),
  879. DEADLINE_NEXT_RQ_ATTR(write1),
  880. DEADLINE_NEXT_RQ_ATTR(read2),
  881. DEADLINE_NEXT_RQ_ATTR(write2),
  882. {"batching", 0400, deadline_batching_show},
  883. {"starved", 0400, deadline_starved_show},
  884. {"async_depth", 0400, dd_async_depth_show},
  885. {"dispatch0", 0400, .seq_ops = &deadline_dispatch0_seq_ops},
  886. {"dispatch1", 0400, .seq_ops = &deadline_dispatch1_seq_ops},
  887. {"dispatch2", 0400, .seq_ops = &deadline_dispatch2_seq_ops},
  888. {"owned_by_driver", 0400, dd_owned_by_driver_show},
  889. {"queued", 0400, dd_queued_show},
  890. {},
  891. };
  892. #undef DEADLINE_QUEUE_DDIR_ATTRS
  893. #endif
  894. static struct elevator_type mq_deadline = {
  895. .ops = {
  896. .depth_updated = dd_depth_updated,
  897. .limit_depth = dd_limit_depth,
  898. .insert_requests = dd_insert_requests,
  899. .dispatch_request = dd_dispatch_request,
  900. .prepare_request = dd_prepare_request,
  901. .finish_request = dd_finish_request,
  902. .next_request = elv_rb_latter_request,
  903. .former_request = elv_rb_former_request,
  904. .bio_merge = dd_bio_merge,
  905. .request_merge = dd_request_merge,
  906. .requests_merged = dd_merged_requests,
  907. .request_merged = dd_request_merged,
  908. .has_work = dd_has_work,
  909. .init_sched = dd_init_sched,
  910. .exit_sched = dd_exit_sched,
  911. .init_hctx = dd_init_hctx,
  912. },
  913. #ifdef CONFIG_BLK_DEBUG_FS
  914. .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
  915. #endif
  916. .elevator_attrs = deadline_attrs,
  917. .elevator_name = "mq-deadline",
  918. .elevator_alias = "deadline",
  919. .elevator_owner = THIS_MODULE,
  920. };
  921. MODULE_ALIAS("mq-deadline-iosched");
  922. static int __init deadline_init(void)
  923. {
  924. return elv_register(&mq_deadline);
  925. }
  926. static void __exit deadline_exit(void)
  927. {
  928. elv_unregister(&mq_deadline);
  929. }
  930. module_init(deadline_init);
  931. module_exit(deadline_exit);
  932. MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche");
  933. MODULE_LICENSE("GPL");
  934. MODULE_DESCRIPTION("MQ deadline IO scheduler");