mq-deadline.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler,
  3. * for the blk-mq scheduling framework
  4. *
  5. * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/fs.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/blk-mq.h>
  11. #include <linux/elevator.h>
  12. #include <linux/bio.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/compiler.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/sbitmap.h>
  19. #include "blk.h"
  20. #include "blk-mq.h"
  21. #include "blk-mq-debugfs.h"
  22. #include "blk-mq-tag.h"
  23. #include "blk-mq-sched.h"
  24. /*
  25. * See Documentation/block/deadline-iosched.txt
  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. static const int writes_starved = 2; /* max times reads can starve a write */
  30. static const int fifo_batch = 16; /* # of sequential requests treated as one
  31. by the above parameters. For throughput. */
  32. struct deadline_data {
  33. /*
  34. * run time data
  35. */
  36. /*
  37. * requests (deadline_rq s) are present on both sort_list and fifo_list
  38. */
  39. struct rb_root sort_list[2];
  40. struct list_head fifo_list[2];
  41. /*
  42. * next in sort order. read, write or both are NULL
  43. */
  44. struct request *next_rq[2];
  45. unsigned int batching; /* number of sequential requests made */
  46. unsigned int starved; /* times reads have starved writes */
  47. /*
  48. * settings that change how the i/o scheduler behaves
  49. */
  50. int fifo_expire[2];
  51. int fifo_batch;
  52. int writes_starved;
  53. int front_merges;
  54. spinlock_t lock;
  55. spinlock_t zone_lock;
  56. struct list_head dispatch;
  57. };
  58. static inline struct rb_root *
  59. deadline_rb_root(struct deadline_data *dd, struct request *rq)
  60. {
  61. return &dd->sort_list[rq_data_dir(rq)];
  62. }
  63. /*
  64. * get the request after `rq' in sector-sorted order
  65. */
  66. static inline struct request *
  67. deadline_latter_request(struct request *rq)
  68. {
  69. struct rb_node *node = rb_next(&rq->rb_node);
  70. if (node)
  71. return rb_entry_rq(node);
  72. return NULL;
  73. }
  74. static void
  75. deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
  76. {
  77. struct rb_root *root = deadline_rb_root(dd, rq);
  78. elv_rb_add(root, rq);
  79. }
  80. static inline void
  81. deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
  82. {
  83. const int data_dir = rq_data_dir(rq);
  84. if (dd->next_rq[data_dir] == rq)
  85. dd->next_rq[data_dir] = deadline_latter_request(rq);
  86. elv_rb_del(deadline_rb_root(dd, rq), rq);
  87. }
  88. /*
  89. * remove rq from rbtree and fifo.
  90. */
  91. static void deadline_remove_request(struct request_queue *q, struct request *rq)
  92. {
  93. struct deadline_data *dd = q->elevator->elevator_data;
  94. list_del_init(&rq->queuelist);
  95. /*
  96. * We might not be on the rbtree, if we are doing an insert merge
  97. */
  98. if (!RB_EMPTY_NODE(&rq->rb_node))
  99. deadline_del_rq_rb(dd, rq);
  100. elv_rqhash_del(q, rq);
  101. if (q->last_merge == rq)
  102. q->last_merge = NULL;
  103. }
  104. static void dd_request_merged(struct request_queue *q, struct request *req,
  105. enum elv_merge type)
  106. {
  107. struct deadline_data *dd = q->elevator->elevator_data;
  108. /*
  109. * if the merge was a front merge, we need to reposition request
  110. */
  111. if (type == ELEVATOR_FRONT_MERGE) {
  112. elv_rb_del(deadline_rb_root(dd, req), req);
  113. deadline_add_rq_rb(dd, req);
  114. }
  115. }
  116. static void dd_merged_requests(struct request_queue *q, struct request *req,
  117. struct request *next)
  118. {
  119. /*
  120. * if next expires before rq, assign its expire time to rq
  121. * and move into next position (next will be deleted) in fifo
  122. */
  123. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
  124. if (time_before((unsigned long)next->fifo_time,
  125. (unsigned long)req->fifo_time)) {
  126. list_move(&req->queuelist, &next->queuelist);
  127. req->fifo_time = next->fifo_time;
  128. }
  129. }
  130. /*
  131. * kill knowledge of next, this one is a goner
  132. */
  133. deadline_remove_request(q, next);
  134. }
  135. /*
  136. * move an entry to dispatch queue
  137. */
  138. static void
  139. deadline_move_request(struct deadline_data *dd, struct request *rq)
  140. {
  141. const int data_dir = rq_data_dir(rq);
  142. dd->next_rq[READ] = NULL;
  143. dd->next_rq[WRITE] = NULL;
  144. dd->next_rq[data_dir] = deadline_latter_request(rq);
  145. /*
  146. * take it off the sort and fifo list
  147. */
  148. deadline_remove_request(rq->q, rq);
  149. }
  150. /*
  151. * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
  152. * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
  153. */
  154. static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
  155. {
  156. struct request *rq = rq_entry_fifo(dd->fifo_list[ddir].next);
  157. /*
  158. * rq is expired!
  159. */
  160. if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
  161. return 1;
  162. return 0;
  163. }
  164. /*
  165. * For the specified data direction, return the next request to
  166. * dispatch using arrival ordered lists.
  167. */
  168. static struct request *
  169. deadline_fifo_request(struct deadline_data *dd, int data_dir)
  170. {
  171. struct request *rq;
  172. unsigned long flags;
  173. if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
  174. return NULL;
  175. if (list_empty(&dd->fifo_list[data_dir]))
  176. return NULL;
  177. rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
  178. if (data_dir == READ || !blk_queue_is_zoned(rq->q))
  179. return rq;
  180. /*
  181. * Look for a write request that can be dispatched, that is one with
  182. * an unlocked target zone.
  183. */
  184. spin_lock_irqsave(&dd->zone_lock, flags);
  185. list_for_each_entry(rq, &dd->fifo_list[WRITE], queuelist) {
  186. if (blk_req_can_dispatch_to_zone(rq))
  187. goto out;
  188. }
  189. rq = NULL;
  190. out:
  191. spin_unlock_irqrestore(&dd->zone_lock, flags);
  192. return rq;
  193. }
  194. /*
  195. * For the specified data direction, return the next request to
  196. * dispatch using sector position sorted lists.
  197. */
  198. static struct request *
  199. deadline_next_request(struct deadline_data *dd, int data_dir)
  200. {
  201. struct request *rq;
  202. unsigned long flags;
  203. if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
  204. return NULL;
  205. rq = dd->next_rq[data_dir];
  206. if (!rq)
  207. return NULL;
  208. if (data_dir == READ || !blk_queue_is_zoned(rq->q))
  209. return rq;
  210. /*
  211. * Look for a write request that can be dispatched, that is one with
  212. * an unlocked target zone.
  213. */
  214. spin_lock_irqsave(&dd->zone_lock, flags);
  215. while (rq) {
  216. if (blk_req_can_dispatch_to_zone(rq))
  217. break;
  218. rq = deadline_latter_request(rq);
  219. }
  220. spin_unlock_irqrestore(&dd->zone_lock, flags);
  221. return rq;
  222. }
  223. /*
  224. * deadline_dispatch_requests selects the best request according to
  225. * read/write expire, fifo_batch, etc
  226. */
  227. static struct request *__dd_dispatch_request(struct deadline_data *dd)
  228. {
  229. struct request *rq, *next_rq;
  230. bool reads, writes;
  231. int data_dir;
  232. if (!list_empty(&dd->dispatch)) {
  233. rq = list_first_entry(&dd->dispatch, struct request, queuelist);
  234. list_del_init(&rq->queuelist);
  235. goto done;
  236. }
  237. reads = !list_empty(&dd->fifo_list[READ]);
  238. writes = !list_empty(&dd->fifo_list[WRITE]);
  239. /*
  240. * batches are currently reads XOR writes
  241. */
  242. rq = deadline_next_request(dd, WRITE);
  243. if (!rq)
  244. rq = deadline_next_request(dd, READ);
  245. if (rq && dd->batching < dd->fifo_batch)
  246. /* we have a next request are still entitled to batch */
  247. goto dispatch_request;
  248. /*
  249. * at this point we are not running a batch. select the appropriate
  250. * data direction (read / write)
  251. */
  252. if (reads) {
  253. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
  254. if (deadline_fifo_request(dd, WRITE) &&
  255. (dd->starved++ >= dd->writes_starved))
  256. goto dispatch_writes;
  257. data_dir = READ;
  258. goto dispatch_find_request;
  259. }
  260. /*
  261. * there are either no reads or writes have been starved
  262. */
  263. if (writes) {
  264. dispatch_writes:
  265. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
  266. dd->starved = 0;
  267. data_dir = WRITE;
  268. goto dispatch_find_request;
  269. }
  270. return NULL;
  271. dispatch_find_request:
  272. /*
  273. * we are not running a batch, find best request for selected data_dir
  274. */
  275. next_rq = deadline_next_request(dd, data_dir);
  276. if (deadline_check_fifo(dd, data_dir) || !next_rq) {
  277. /*
  278. * A deadline has expired, the last request was in the other
  279. * direction, or we have run out of higher-sectored requests.
  280. * Start again from the request with the earliest expiry time.
  281. */
  282. rq = deadline_fifo_request(dd, data_dir);
  283. } else {
  284. /*
  285. * The last req was the same dir and we have a next request in
  286. * sort order. No expired requests so continue on from here.
  287. */
  288. rq = next_rq;
  289. }
  290. /*
  291. * For a zoned block device, if we only have writes queued and none of
  292. * them can be dispatched, rq will be NULL.
  293. */
  294. if (!rq)
  295. return NULL;
  296. dd->batching = 0;
  297. dispatch_request:
  298. /*
  299. * rq is the selected appropriate request.
  300. */
  301. dd->batching++;
  302. deadline_move_request(dd, rq);
  303. done:
  304. /*
  305. * If the request needs its target zone locked, do it.
  306. */
  307. blk_req_zone_write_lock(rq);
  308. rq->rq_flags |= RQF_STARTED;
  309. return rq;
  310. }
  311. /*
  312. * One confusing aspect here is that we get called for a specific
  313. * hardware queue, but we may return a request that is for a
  314. * different hardware queue. This is because mq-deadline has shared
  315. * state for all hardware queues, in terms of sorting, FIFOs, etc.
  316. */
  317. static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
  318. {
  319. struct deadline_data *dd = hctx->queue->elevator->elevator_data;
  320. struct request *rq;
  321. spin_lock(&dd->lock);
  322. rq = __dd_dispatch_request(dd);
  323. spin_unlock(&dd->lock);
  324. return rq;
  325. }
  326. static void dd_exit_queue(struct elevator_queue *e)
  327. {
  328. struct deadline_data *dd = e->elevator_data;
  329. BUG_ON(!list_empty(&dd->fifo_list[READ]));
  330. BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
  331. kfree(dd);
  332. }
  333. /*
  334. * initialize elevator private data (deadline_data).
  335. */
  336. static int dd_init_queue(struct request_queue *q, struct elevator_type *e)
  337. {
  338. struct deadline_data *dd;
  339. struct elevator_queue *eq;
  340. eq = elevator_alloc(q, e);
  341. if (!eq)
  342. return -ENOMEM;
  343. dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
  344. if (!dd) {
  345. kobject_put(&eq->kobj);
  346. return -ENOMEM;
  347. }
  348. eq->elevator_data = dd;
  349. INIT_LIST_HEAD(&dd->fifo_list[READ]);
  350. INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
  351. dd->sort_list[READ] = RB_ROOT;
  352. dd->sort_list[WRITE] = RB_ROOT;
  353. dd->fifo_expire[READ] = read_expire;
  354. dd->fifo_expire[WRITE] = write_expire;
  355. dd->writes_starved = writes_starved;
  356. dd->front_merges = 1;
  357. dd->fifo_batch = fifo_batch;
  358. spin_lock_init(&dd->lock);
  359. spin_lock_init(&dd->zone_lock);
  360. INIT_LIST_HEAD(&dd->dispatch);
  361. q->elevator = eq;
  362. return 0;
  363. }
  364. static int dd_request_merge(struct request_queue *q, struct request **rq,
  365. struct bio *bio)
  366. {
  367. struct deadline_data *dd = q->elevator->elevator_data;
  368. sector_t sector = bio_end_sector(bio);
  369. struct request *__rq;
  370. if (!dd->front_merges)
  371. return ELEVATOR_NO_MERGE;
  372. __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
  373. if (__rq) {
  374. BUG_ON(sector != blk_rq_pos(__rq));
  375. if (elv_bio_merge_ok(__rq, bio)) {
  376. *rq = __rq;
  377. return ELEVATOR_FRONT_MERGE;
  378. }
  379. }
  380. return ELEVATOR_NO_MERGE;
  381. }
  382. static bool dd_bio_merge(struct blk_mq_hw_ctx *hctx, struct bio *bio)
  383. {
  384. struct request_queue *q = hctx->queue;
  385. struct deadline_data *dd = q->elevator->elevator_data;
  386. struct request *free = NULL;
  387. bool ret;
  388. spin_lock(&dd->lock);
  389. ret = blk_mq_sched_try_merge(q, bio, &free);
  390. spin_unlock(&dd->lock);
  391. if (free)
  392. blk_mq_free_request(free);
  393. return ret;
  394. }
  395. /*
  396. * add rq to rbtree and fifo
  397. */
  398. static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  399. bool at_head)
  400. {
  401. struct request_queue *q = hctx->queue;
  402. struct deadline_data *dd = q->elevator->elevator_data;
  403. const int data_dir = rq_data_dir(rq);
  404. /*
  405. * This may be a requeue of a write request that has locked its
  406. * target zone. If it is the case, this releases the zone lock.
  407. */
  408. blk_req_zone_write_unlock(rq);
  409. if (blk_mq_sched_try_insert_merge(q, rq))
  410. return;
  411. blk_mq_sched_request_inserted(rq);
  412. if (at_head || blk_rq_is_passthrough(rq)) {
  413. if (at_head)
  414. list_add(&rq->queuelist, &dd->dispatch);
  415. else
  416. list_add_tail(&rq->queuelist, &dd->dispatch);
  417. } else {
  418. deadline_add_rq_rb(dd, rq);
  419. if (rq_mergeable(rq)) {
  420. elv_rqhash_add(q, rq);
  421. if (!q->last_merge)
  422. q->last_merge = rq;
  423. }
  424. /*
  425. * set expire time and add to fifo list
  426. */
  427. rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
  428. list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);
  429. }
  430. }
  431. static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
  432. struct list_head *list, bool at_head)
  433. {
  434. struct request_queue *q = hctx->queue;
  435. struct deadline_data *dd = q->elevator->elevator_data;
  436. spin_lock(&dd->lock);
  437. while (!list_empty(list)) {
  438. struct request *rq;
  439. rq = list_first_entry(list, struct request, queuelist);
  440. list_del_init(&rq->queuelist);
  441. dd_insert_request(hctx, rq, at_head);
  442. }
  443. spin_unlock(&dd->lock);
  444. }
  445. /*
  446. * Nothing to do here. This is defined only to ensure that .finish_request
  447. * method is called upon request completion.
  448. */
  449. static void dd_prepare_request(struct request *rq, struct bio *bio)
  450. {
  451. }
  452. /*
  453. * For zoned block devices, write unlock the target zone of
  454. * completed write requests. Do this while holding the zone lock
  455. * spinlock so that the zone is never unlocked while deadline_fifo_request()
  456. * or deadline_next_request() are executing. This function is called for
  457. * all requests, whether or not these requests complete successfully.
  458. *
  459. * For a zoned block device, __dd_dispatch_request() may have stopped
  460. * dispatching requests if all the queued requests are write requests directed
  461. * at zones that are already locked due to on-going write requests. To ensure
  462. * write request dispatch progress in this case, mark the queue as needing a
  463. * restart to ensure that the queue is run again after completion of the
  464. * request and zones being unlocked.
  465. */
  466. static void dd_finish_request(struct request *rq)
  467. {
  468. struct request_queue *q = rq->q;
  469. if (blk_queue_is_zoned(q)) {
  470. struct deadline_data *dd = q->elevator->elevator_data;
  471. unsigned long flags;
  472. spin_lock_irqsave(&dd->zone_lock, flags);
  473. blk_req_zone_write_unlock(rq);
  474. if (!list_empty(&dd->fifo_list[WRITE])) {
  475. struct blk_mq_hw_ctx *hctx;
  476. hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
  477. blk_mq_sched_mark_restart_hctx(hctx);
  478. }
  479. spin_unlock_irqrestore(&dd->zone_lock, flags);
  480. }
  481. }
  482. static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
  483. {
  484. struct deadline_data *dd = hctx->queue->elevator->elevator_data;
  485. return !list_empty_careful(&dd->dispatch) ||
  486. !list_empty_careful(&dd->fifo_list[0]) ||
  487. !list_empty_careful(&dd->fifo_list[1]);
  488. }
  489. /*
  490. * sysfs parts below
  491. */
  492. static ssize_t
  493. deadline_var_show(int var, char *page)
  494. {
  495. return sprintf(page, "%d\n", var);
  496. }
  497. static void
  498. deadline_var_store(int *var, const char *page)
  499. {
  500. char *p = (char *) page;
  501. *var = simple_strtol(p, &p, 10);
  502. }
  503. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  504. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  505. { \
  506. struct deadline_data *dd = e->elevator_data; \
  507. int __data = __VAR; \
  508. if (__CONV) \
  509. __data = jiffies_to_msecs(__data); \
  510. return deadline_var_show(__data, (page)); \
  511. }
  512. SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);
  513. SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);
  514. SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);
  515. SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);
  516. SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);
  517. #undef SHOW_FUNCTION
  518. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  519. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  520. { \
  521. struct deadline_data *dd = e->elevator_data; \
  522. int __data; \
  523. deadline_var_store(&__data, (page)); \
  524. if (__data < (MIN)) \
  525. __data = (MIN); \
  526. else if (__data > (MAX)) \
  527. __data = (MAX); \
  528. if (__CONV) \
  529. *(__PTR) = msecs_to_jiffies(__data); \
  530. else \
  531. *(__PTR) = __data; \
  532. return count; \
  533. }
  534. STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
  535. STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
  536. STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
  537. STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);
  538. STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);
  539. #undef STORE_FUNCTION
  540. #define DD_ATTR(name) \
  541. __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
  542. static struct elv_fs_entry deadline_attrs[] = {
  543. DD_ATTR(read_expire),
  544. DD_ATTR(write_expire),
  545. DD_ATTR(writes_starved),
  546. DD_ATTR(front_merges),
  547. DD_ATTR(fifo_batch),
  548. __ATTR_NULL
  549. };
  550. #ifdef CONFIG_BLK_DEBUG_FS
  551. #define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \
  552. static void *deadline_##name##_fifo_start(struct seq_file *m, \
  553. loff_t *pos) \
  554. __acquires(&dd->lock) \
  555. { \
  556. struct request_queue *q = m->private; \
  557. struct deadline_data *dd = q->elevator->elevator_data; \
  558. \
  559. spin_lock(&dd->lock); \
  560. return seq_list_start(&dd->fifo_list[ddir], *pos); \
  561. } \
  562. \
  563. static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
  564. loff_t *pos) \
  565. { \
  566. struct request_queue *q = m->private; \
  567. struct deadline_data *dd = q->elevator->elevator_data; \
  568. \
  569. return seq_list_next(v, &dd->fifo_list[ddir], pos); \
  570. } \
  571. \
  572. static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
  573. __releases(&dd->lock) \
  574. { \
  575. struct request_queue *q = m->private; \
  576. struct deadline_data *dd = q->elevator->elevator_data; \
  577. \
  578. spin_unlock(&dd->lock); \
  579. } \
  580. \
  581. static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
  582. .start = deadline_##name##_fifo_start, \
  583. .next = deadline_##name##_fifo_next, \
  584. .stop = deadline_##name##_fifo_stop, \
  585. .show = blk_mq_debugfs_rq_show, \
  586. }; \
  587. \
  588. static int deadline_##name##_next_rq_show(void *data, \
  589. struct seq_file *m) \
  590. { \
  591. struct request_queue *q = data; \
  592. struct deadline_data *dd = q->elevator->elevator_data; \
  593. struct request *rq = dd->next_rq[ddir]; \
  594. \
  595. if (rq) \
  596. __blk_mq_debugfs_rq_show(m, rq); \
  597. return 0; \
  598. }
  599. DEADLINE_DEBUGFS_DDIR_ATTRS(READ, read)
  600. DEADLINE_DEBUGFS_DDIR_ATTRS(WRITE, write)
  601. #undef DEADLINE_DEBUGFS_DDIR_ATTRS
  602. static int deadline_batching_show(void *data, struct seq_file *m)
  603. {
  604. struct request_queue *q = data;
  605. struct deadline_data *dd = q->elevator->elevator_data;
  606. seq_printf(m, "%u\n", dd->batching);
  607. return 0;
  608. }
  609. static int deadline_starved_show(void *data, struct seq_file *m)
  610. {
  611. struct request_queue *q = data;
  612. struct deadline_data *dd = q->elevator->elevator_data;
  613. seq_printf(m, "%u\n", dd->starved);
  614. return 0;
  615. }
  616. static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos)
  617. __acquires(&dd->lock)
  618. {
  619. struct request_queue *q = m->private;
  620. struct deadline_data *dd = q->elevator->elevator_data;
  621. spin_lock(&dd->lock);
  622. return seq_list_start(&dd->dispatch, *pos);
  623. }
  624. static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
  625. {
  626. struct request_queue *q = m->private;
  627. struct deadline_data *dd = q->elevator->elevator_data;
  628. return seq_list_next(v, &dd->dispatch, pos);
  629. }
  630. static void deadline_dispatch_stop(struct seq_file *m, void *v)
  631. __releases(&dd->lock)
  632. {
  633. struct request_queue *q = m->private;
  634. struct deadline_data *dd = q->elevator->elevator_data;
  635. spin_unlock(&dd->lock);
  636. }
  637. static const struct seq_operations deadline_dispatch_seq_ops = {
  638. .start = deadline_dispatch_start,
  639. .next = deadline_dispatch_next,
  640. .stop = deadline_dispatch_stop,
  641. .show = blk_mq_debugfs_rq_show,
  642. };
  643. #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
  644. {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \
  645. {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
  646. static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
  647. DEADLINE_QUEUE_DDIR_ATTRS(read),
  648. DEADLINE_QUEUE_DDIR_ATTRS(write),
  649. {"batching", 0400, deadline_batching_show},
  650. {"starved", 0400, deadline_starved_show},
  651. {"dispatch", 0400, .seq_ops = &deadline_dispatch_seq_ops},
  652. {},
  653. };
  654. #undef DEADLINE_QUEUE_DDIR_ATTRS
  655. #endif
  656. static struct elevator_type mq_deadline = {
  657. .ops.mq = {
  658. .insert_requests = dd_insert_requests,
  659. .dispatch_request = dd_dispatch_request,
  660. .prepare_request = dd_prepare_request,
  661. .finish_request = dd_finish_request,
  662. .next_request = elv_rb_latter_request,
  663. .former_request = elv_rb_former_request,
  664. .bio_merge = dd_bio_merge,
  665. .request_merge = dd_request_merge,
  666. .requests_merged = dd_merged_requests,
  667. .request_merged = dd_request_merged,
  668. .has_work = dd_has_work,
  669. .init_sched = dd_init_queue,
  670. .exit_sched = dd_exit_queue,
  671. },
  672. .uses_mq = true,
  673. #ifdef CONFIG_BLK_DEBUG_FS
  674. .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
  675. #endif
  676. .elevator_attrs = deadline_attrs,
  677. .elevator_name = "mq-deadline",
  678. .elevator_alias = "deadline",
  679. .elevator_owner = THIS_MODULE,
  680. };
  681. MODULE_ALIAS("mq-deadline-iosched");
  682. static int __init deadline_init(void)
  683. {
  684. return elv_register(&mq_deadline);
  685. }
  686. static void __exit deadline_exit(void)
  687. {
  688. elv_unregister(&mq_deadline);
  689. }
  690. module_init(deadline_init);
  691. module_exit(deadline_exit);
  692. MODULE_AUTHOR("Jens Axboe");
  693. MODULE_LICENSE("GPL");
  694. MODULE_DESCRIPTION("MQ deadline IO scheduler");