deadline-iosched.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Deadline i/o scheduler.
  3. *
  4. * Copyright (C) 2002 Jens Axboe <axboe@kernel.dk>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/fs.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/elevator.h>
  10. #include <linux/bio.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/compiler.h>
  15. #include <linux/rbtree.h>
  16. /*
  17. * See Documentation/block/deadline-iosched.txt
  18. */
  19. static const int read_expire = HZ / 2; /* max time before a read is submitted. */
  20. static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
  21. static const int writes_starved = 2; /* max times reads can starve a write */
  22. static const int fifo_batch = 16; /* # of sequential requests treated as one
  23. by the above parameters. For throughput. */
  24. struct deadline_data {
  25. /*
  26. * run time data
  27. */
  28. /*
  29. * requests (deadline_rq s) are present on both sort_list and fifo_list
  30. */
  31. struct rb_root sort_list[2];
  32. struct list_head fifo_list[2];
  33. /*
  34. * next in sort order. read, write or both are NULL
  35. */
  36. struct request *next_rq[2];
  37. unsigned int batching; /* number of sequential requests made */
  38. unsigned int starved; /* times reads have starved writes */
  39. /*
  40. * settings that change how the i/o scheduler behaves
  41. */
  42. int fifo_expire[2];
  43. int fifo_batch;
  44. int writes_starved;
  45. int front_merges;
  46. };
  47. static inline struct rb_root *
  48. deadline_rb_root(struct deadline_data *dd, struct request *rq)
  49. {
  50. return &dd->sort_list[rq_data_dir(rq)];
  51. }
  52. /*
  53. * get the request after `rq' in sector-sorted order
  54. */
  55. static inline struct request *
  56. deadline_latter_request(struct request *rq)
  57. {
  58. struct rb_node *node = rb_next(&rq->rb_node);
  59. if (node)
  60. return rb_entry_rq(node);
  61. return NULL;
  62. }
  63. static void
  64. deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
  65. {
  66. struct rb_root *root = deadline_rb_root(dd, rq);
  67. elv_rb_add(root, rq);
  68. }
  69. static inline void
  70. deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
  71. {
  72. const int data_dir = rq_data_dir(rq);
  73. if (dd->next_rq[data_dir] == rq)
  74. dd->next_rq[data_dir] = deadline_latter_request(rq);
  75. elv_rb_del(deadline_rb_root(dd, rq), rq);
  76. }
  77. /*
  78. * add rq to rbtree and fifo
  79. */
  80. static void
  81. deadline_add_request(struct request_queue *q, struct request *rq)
  82. {
  83. struct deadline_data *dd = q->elevator->elevator_data;
  84. const int data_dir = rq_data_dir(rq);
  85. /*
  86. * This may be a requeue of a write request that has locked its
  87. * target zone. If it is the case, this releases the zone lock.
  88. */
  89. blk_req_zone_write_unlock(rq);
  90. deadline_add_rq_rb(dd, rq);
  91. /*
  92. * set expire time and add to fifo list
  93. */
  94. rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
  95. list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);
  96. }
  97. /*
  98. * remove rq from rbtree and fifo.
  99. */
  100. static void deadline_remove_request(struct request_queue *q, struct request *rq)
  101. {
  102. struct deadline_data *dd = q->elevator->elevator_data;
  103. rq_fifo_clear(rq);
  104. deadline_del_rq_rb(dd, rq);
  105. }
  106. static enum elv_merge
  107. deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)
  108. {
  109. struct deadline_data *dd = q->elevator->elevator_data;
  110. struct request *__rq;
  111. /*
  112. * check for front merge
  113. */
  114. if (dd->front_merges) {
  115. sector_t sector = bio_end_sector(bio);
  116. __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
  117. if (__rq) {
  118. BUG_ON(sector != blk_rq_pos(__rq));
  119. if (elv_bio_merge_ok(__rq, bio)) {
  120. *req = __rq;
  121. return ELEVATOR_FRONT_MERGE;
  122. }
  123. }
  124. }
  125. return ELEVATOR_NO_MERGE;
  126. }
  127. static void deadline_merged_request(struct request_queue *q,
  128. struct request *req, enum elv_merge type)
  129. {
  130. struct deadline_data *dd = q->elevator->elevator_data;
  131. /*
  132. * if the merge was a front merge, we need to reposition request
  133. */
  134. if (type == ELEVATOR_FRONT_MERGE) {
  135. elv_rb_del(deadline_rb_root(dd, req), req);
  136. deadline_add_rq_rb(dd, req);
  137. }
  138. }
  139. static void
  140. deadline_merged_requests(struct request_queue *q, struct request *req,
  141. struct request *next)
  142. {
  143. /*
  144. * if next expires before rq, assign its expire time to rq
  145. * and move into next position (next will be deleted) in fifo
  146. */
  147. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
  148. if (time_before((unsigned long)next->fifo_time,
  149. (unsigned long)req->fifo_time)) {
  150. list_move(&req->queuelist, &next->queuelist);
  151. req->fifo_time = next->fifo_time;
  152. }
  153. }
  154. /*
  155. * kill knowledge of next, this one is a goner
  156. */
  157. deadline_remove_request(q, next);
  158. }
  159. /*
  160. * move request from sort list to dispatch queue.
  161. */
  162. static inline void
  163. deadline_move_to_dispatch(struct deadline_data *dd, struct request *rq)
  164. {
  165. struct request_queue *q = rq->q;
  166. /*
  167. * For a zoned block device, write requests must write lock their
  168. * target zone.
  169. */
  170. blk_req_zone_write_lock(rq);
  171. deadline_remove_request(q, rq);
  172. elv_dispatch_add_tail(q, rq);
  173. }
  174. /*
  175. * move an entry to dispatch queue
  176. */
  177. static void
  178. deadline_move_request(struct deadline_data *dd, struct request *rq)
  179. {
  180. const int data_dir = rq_data_dir(rq);
  181. dd->next_rq[READ] = NULL;
  182. dd->next_rq[WRITE] = NULL;
  183. dd->next_rq[data_dir] = deadline_latter_request(rq);
  184. /*
  185. * take it off the sort and fifo list, move
  186. * to dispatch queue
  187. */
  188. deadline_move_to_dispatch(dd, rq);
  189. }
  190. /*
  191. * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
  192. * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
  193. */
  194. static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
  195. {
  196. struct request *rq = rq_entry_fifo(dd->fifo_list[ddir].next);
  197. /*
  198. * rq is expired!
  199. */
  200. if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
  201. return 1;
  202. return 0;
  203. }
  204. /*
  205. * For the specified data direction, return the next request to dispatch using
  206. * arrival ordered lists.
  207. */
  208. static struct request *
  209. deadline_fifo_request(struct deadline_data *dd, int data_dir)
  210. {
  211. struct request *rq;
  212. if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
  213. return NULL;
  214. if (list_empty(&dd->fifo_list[data_dir]))
  215. return NULL;
  216. rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
  217. if (data_dir == READ || !blk_queue_is_zoned(rq->q))
  218. return rq;
  219. /*
  220. * Look for a write request that can be dispatched, that is one with
  221. * an unlocked target zone.
  222. */
  223. list_for_each_entry(rq, &dd->fifo_list[WRITE], queuelist) {
  224. if (blk_req_can_dispatch_to_zone(rq))
  225. return rq;
  226. }
  227. return NULL;
  228. }
  229. /*
  230. * For the specified data direction, return the next request to dispatch using
  231. * sector position sorted lists.
  232. */
  233. static struct request *
  234. deadline_next_request(struct deadline_data *dd, int data_dir)
  235. {
  236. struct request *rq;
  237. if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
  238. return NULL;
  239. rq = dd->next_rq[data_dir];
  240. if (!rq)
  241. return NULL;
  242. if (data_dir == READ || !blk_queue_is_zoned(rq->q))
  243. return rq;
  244. /*
  245. * Look for a write request that can be dispatched, that is one with
  246. * an unlocked target zone.
  247. */
  248. while (rq) {
  249. if (blk_req_can_dispatch_to_zone(rq))
  250. return rq;
  251. rq = deadline_latter_request(rq);
  252. }
  253. return NULL;
  254. }
  255. /*
  256. * deadline_dispatch_requests selects the best request according to
  257. * read/write expire, fifo_batch, etc
  258. */
  259. static int deadline_dispatch_requests(struct request_queue *q, int force)
  260. {
  261. struct deadline_data *dd = q->elevator->elevator_data;
  262. const int reads = !list_empty(&dd->fifo_list[READ]);
  263. const int writes = !list_empty(&dd->fifo_list[WRITE]);
  264. struct request *rq, *next_rq;
  265. int data_dir;
  266. /*
  267. * batches are currently reads XOR writes
  268. */
  269. rq = deadline_next_request(dd, WRITE);
  270. if (!rq)
  271. rq = deadline_next_request(dd, READ);
  272. if (rq && dd->batching < dd->fifo_batch)
  273. /* we have a next request are still entitled to batch */
  274. goto dispatch_request;
  275. /*
  276. * at this point we are not running a batch. select the appropriate
  277. * data direction (read / write)
  278. */
  279. if (reads) {
  280. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
  281. if (deadline_fifo_request(dd, WRITE) &&
  282. (dd->starved++ >= dd->writes_starved))
  283. goto dispatch_writes;
  284. data_dir = READ;
  285. goto dispatch_find_request;
  286. }
  287. /*
  288. * there are either no reads or writes have been starved
  289. */
  290. if (writes) {
  291. dispatch_writes:
  292. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
  293. dd->starved = 0;
  294. data_dir = WRITE;
  295. goto dispatch_find_request;
  296. }
  297. return 0;
  298. dispatch_find_request:
  299. /*
  300. * we are not running a batch, find best request for selected data_dir
  301. */
  302. next_rq = deadline_next_request(dd, data_dir);
  303. if (deadline_check_fifo(dd, data_dir) || !next_rq) {
  304. /*
  305. * A deadline has expired, the last request was in the other
  306. * direction, or we have run out of higher-sectored requests.
  307. * Start again from the request with the earliest expiry time.
  308. */
  309. rq = deadline_fifo_request(dd, data_dir);
  310. } else {
  311. /*
  312. * The last req was the same dir and we have a next request in
  313. * sort order. No expired requests so continue on from here.
  314. */
  315. rq = next_rq;
  316. }
  317. /*
  318. * For a zoned block device, if we only have writes queued and none of
  319. * them can be dispatched, rq will be NULL.
  320. */
  321. if (!rq)
  322. return 0;
  323. dd->batching = 0;
  324. dispatch_request:
  325. /*
  326. * rq is the selected appropriate request.
  327. */
  328. dd->batching++;
  329. deadline_move_request(dd, rq);
  330. return 1;
  331. }
  332. /*
  333. * For zoned block devices, write unlock the target zone of completed
  334. * write requests.
  335. */
  336. static void
  337. deadline_completed_request(struct request_queue *q, struct request *rq)
  338. {
  339. blk_req_zone_write_unlock(rq);
  340. }
  341. static void deadline_exit_queue(struct elevator_queue *e)
  342. {
  343. struct deadline_data *dd = e->elevator_data;
  344. BUG_ON(!list_empty(&dd->fifo_list[READ]));
  345. BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
  346. kfree(dd);
  347. }
  348. /*
  349. * initialize elevator private data (deadline_data).
  350. */
  351. static int deadline_init_queue(struct request_queue *q, struct elevator_type *e)
  352. {
  353. struct deadline_data *dd;
  354. struct elevator_queue *eq;
  355. eq = elevator_alloc(q, e);
  356. if (!eq)
  357. return -ENOMEM;
  358. dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
  359. if (!dd) {
  360. kobject_put(&eq->kobj);
  361. return -ENOMEM;
  362. }
  363. eq->elevator_data = dd;
  364. INIT_LIST_HEAD(&dd->fifo_list[READ]);
  365. INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
  366. dd->sort_list[READ] = RB_ROOT;
  367. dd->sort_list[WRITE] = RB_ROOT;
  368. dd->fifo_expire[READ] = read_expire;
  369. dd->fifo_expire[WRITE] = write_expire;
  370. dd->writes_starved = writes_starved;
  371. dd->front_merges = 1;
  372. dd->fifo_batch = fifo_batch;
  373. spin_lock_irq(q->queue_lock);
  374. q->elevator = eq;
  375. spin_unlock_irq(q->queue_lock);
  376. return 0;
  377. }
  378. /*
  379. * sysfs parts below
  380. */
  381. static ssize_t
  382. deadline_var_show(int var, char *page)
  383. {
  384. return sprintf(page, "%d\n", var);
  385. }
  386. static void
  387. deadline_var_store(int *var, const char *page)
  388. {
  389. char *p = (char *) page;
  390. *var = simple_strtol(p, &p, 10);
  391. }
  392. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  393. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  394. { \
  395. struct deadline_data *dd = e->elevator_data; \
  396. int __data = __VAR; \
  397. if (__CONV) \
  398. __data = jiffies_to_msecs(__data); \
  399. return deadline_var_show(__data, (page)); \
  400. }
  401. SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);
  402. SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);
  403. SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);
  404. SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);
  405. SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);
  406. #undef SHOW_FUNCTION
  407. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  408. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  409. { \
  410. struct deadline_data *dd = e->elevator_data; \
  411. int __data; \
  412. deadline_var_store(&__data, (page)); \
  413. if (__data < (MIN)) \
  414. __data = (MIN); \
  415. else if (__data > (MAX)) \
  416. __data = (MAX); \
  417. if (__CONV) \
  418. *(__PTR) = msecs_to_jiffies(__data); \
  419. else \
  420. *(__PTR) = __data; \
  421. return count; \
  422. }
  423. STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
  424. STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
  425. STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
  426. STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);
  427. STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);
  428. #undef STORE_FUNCTION
  429. #define DD_ATTR(name) \
  430. __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
  431. static struct elv_fs_entry deadline_attrs[] = {
  432. DD_ATTR(read_expire),
  433. DD_ATTR(write_expire),
  434. DD_ATTR(writes_starved),
  435. DD_ATTR(front_merges),
  436. DD_ATTR(fifo_batch),
  437. __ATTR_NULL
  438. };
  439. static struct elevator_type iosched_deadline = {
  440. .ops.sq = {
  441. .elevator_merge_fn = deadline_merge,
  442. .elevator_merged_fn = deadline_merged_request,
  443. .elevator_merge_req_fn = deadline_merged_requests,
  444. .elevator_dispatch_fn = deadline_dispatch_requests,
  445. .elevator_completed_req_fn = deadline_completed_request,
  446. .elevator_add_req_fn = deadline_add_request,
  447. .elevator_former_req_fn = elv_rb_former_request,
  448. .elevator_latter_req_fn = elv_rb_latter_request,
  449. .elevator_init_fn = deadline_init_queue,
  450. .elevator_exit_fn = deadline_exit_queue,
  451. },
  452. .elevator_attrs = deadline_attrs,
  453. .elevator_name = "deadline",
  454. .elevator_owner = THIS_MODULE,
  455. };
  456. static int __init deadline_init(void)
  457. {
  458. return elv_register(&iosched_deadline);
  459. }
  460. static void __exit deadline_exit(void)
  461. {
  462. elv_unregister(&iosched_deadline);
  463. }
  464. module_init(deadline_init);
  465. module_exit(deadline_exit);
  466. MODULE_AUTHOR("Jens Axboe");
  467. MODULE_LICENSE("GPL");
  468. MODULE_DESCRIPTION("deadline IO scheduler");