zen-iosched.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Zen IO scheduler
  3. * Primarily based on Noop, deadline, and SIO IO schedulers.
  4. *
  5. * Copyright (C) 2012 Brandon Berhent <bbedward@gmail.com>
  6. *
  7. * FCFS, dispatches are back-inserted, deadlines ensure fairness.
  8. * Should work best with devices where there is no travel delay.
  9. */
  10. #include <linux/blkdev.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. enum zen_data_dir { ASYNC, SYNC };
  17. static const int sync_expire = HZ / 2; /* max time before a sync is submitted. */
  18. static const int async_expire = 5 * HZ; /* ditto for async, these limits are SOFT! */
  19. static const int fifo_batch = 1;
  20. struct zen_data {
  21. /* Runtime Data */
  22. /* Requests are only present on fifo_list */
  23. struct list_head fifo_list[2];
  24. unsigned int batching; /* number of sequential requests made */
  25. /* tunables */
  26. int fifo_expire[2];
  27. int fifo_batch;
  28. };
  29. static inline struct zen_data *
  30. zen_get_data(struct request_queue *q) {
  31. return q->elevator->elevator_data;
  32. }
  33. static void zen_dispatch(struct zen_data *, struct request *);
  34. static void
  35. zen_merged_requests(struct request_queue *q, struct request *req,
  36. struct request *next)
  37. {
  38. /*
  39. * if next expires before rq, assign its expire time to arq
  40. * and move into next position (next will be deleted) in fifo
  41. */
  42. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
  43. if (time_before((unsigned long)next->fifo_time, (unsigned long)req->fifo_time)) {
  44. list_move(&req->queuelist, &next->queuelist);
  45. req->fifo_time = next->fifo_time;
  46. }
  47. }
  48. /* next request is gone */
  49. rq_fifo_clear(next);
  50. }
  51. static void zen_add_request(struct request_queue *q, struct request *rq)
  52. {
  53. struct zen_data *zdata = zen_get_data(q);
  54. const int sync = rq_is_sync(rq);
  55. if (zdata->fifo_expire[sync]) {
  56. rq->fifo_time = jiffies + zdata->fifo_expire[sync];
  57. list_add_tail(&rq->queuelist, &zdata->fifo_list[sync]);
  58. }
  59. }
  60. static void zen_dispatch(struct zen_data *zdata, struct request *rq)
  61. {
  62. /* Remove request from list and dispatch it */
  63. rq_fifo_clear(rq);
  64. elv_dispatch_add_tail(rq->q, rq);
  65. /* Increment # of sequential requests */
  66. zdata->batching++;
  67. }
  68. /*
  69. * get the first expired request in direction ddir
  70. */
  71. static struct request *
  72. zen_expired_request(struct zen_data *zdata, int ddir)
  73. {
  74. struct request *rq;
  75. if (list_empty(&zdata->fifo_list[ddir]))
  76. return NULL;
  77. rq = rq_entry_fifo(zdata->fifo_list[ddir].next);
  78. if (time_after(jiffies, (unsigned long)rq->fifo_time))
  79. return rq;
  80. return NULL;
  81. }
  82. /*
  83. * zen_check_fifo returns 0 if there are no expired requests on the fifo,
  84. * otherwise it returns the next expired request
  85. */
  86. static struct request *
  87. zen_check_fifo(struct zen_data *zdata)
  88. {
  89. struct request *rq_sync = zen_expired_request(zdata, SYNC);
  90. struct request *rq_async = zen_expired_request(zdata, ASYNC);
  91. if (rq_async && rq_sync) {
  92. if (time_after((unsigned long)rq_async->fifo_time, (unsigned long)rq_sync->fifo_time))
  93. return rq_sync;
  94. } else if (rq_sync) {
  95. return rq_sync;
  96. } else if (rq_async) {
  97. return rq_async;
  98. }
  99. return 0;
  100. }
  101. static struct request *
  102. zen_choose_request(struct zen_data *zdata)
  103. {
  104. /*
  105. * Retrieve request from available fifo list.
  106. * Synchronous requests have priority over asynchronous.
  107. */
  108. if (!list_empty(&zdata->fifo_list[SYNC]))
  109. return rq_entry_fifo(zdata->fifo_list[SYNC].next);
  110. if (!list_empty(&zdata->fifo_list[ASYNC]))
  111. return rq_entry_fifo(zdata->fifo_list[ASYNC].next);
  112. return NULL;
  113. }
  114. static int zen_dispatch_requests(struct request_queue *q, int force)
  115. {
  116. struct zen_data *zdata = zen_get_data(q);
  117. struct request *rq = NULL;
  118. /* Check for and issue expired requests */
  119. if (zdata->batching > zdata->fifo_batch) {
  120. zdata->batching = 0;
  121. rq = zen_check_fifo(zdata);
  122. }
  123. if (!rq) {
  124. rq = zen_choose_request(zdata);
  125. if (!rq)
  126. return 0;
  127. }
  128. zen_dispatch(zdata, rq);
  129. return 1;
  130. }
  131. static int zen_init_queue(struct request_queue *q, struct elevator_type *e)
  132. {
  133. struct zen_data *zdata;
  134. struct elevator_queue *eq;
  135. eq = elevator_alloc(q, e);
  136. if (!eq)
  137. return -ENOMEM;
  138. zdata = kmalloc_node(sizeof(*zdata), GFP_KERNEL, q->node);
  139. if (!zdata) {
  140. kobject_put(&eq->kobj);
  141. return -ENOMEM;
  142. }
  143. eq->elevator_data = zdata;
  144. INIT_LIST_HEAD(&zdata->fifo_list[SYNC]);
  145. INIT_LIST_HEAD(&zdata->fifo_list[ASYNC]);
  146. zdata->fifo_expire[SYNC] = sync_expire;
  147. zdata->fifo_expire[ASYNC] = async_expire;
  148. zdata->fifo_batch = fifo_batch;
  149. spin_lock_irq(q->queue_lock);
  150. q->elevator = eq;
  151. spin_unlock_irq(q->queue_lock);
  152. return 0;
  153. }
  154. static void zen_exit_queue(struct elevator_queue *e)
  155. {
  156. struct zen_data *zdata = e->elevator_data;
  157. BUG_ON(!list_empty(&zdata->fifo_list[SYNC]));
  158. BUG_ON(!list_empty(&zdata->fifo_list[ASYNC]));
  159. kfree(zdata);
  160. }
  161. /* Sysfs */
  162. static ssize_t
  163. zen_var_show(int var, char *page)
  164. {
  165. return sprintf(page, "%d\n", var);
  166. }
  167. static ssize_t
  168. zen_var_store(int *var, const char *page, size_t count)
  169. {
  170. *var = simple_strtol(page, NULL, 10);
  171. return count;
  172. }
  173. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
  174. static ssize_t __FUNC(struct elevator_queue *e, char *page) \
  175. { \
  176. struct zen_data *zdata = e->elevator_data; \
  177. int __data = __VAR; \
  178. if (__CONV) \
  179. __data = jiffies_to_msecs(__data); \
  180. return zen_var_show(__data, (page)); \
  181. }
  182. SHOW_FUNCTION(zen_sync_expire_show, zdata->fifo_expire[SYNC], 1);
  183. SHOW_FUNCTION(zen_async_expire_show, zdata->fifo_expire[ASYNC], 1);
  184. SHOW_FUNCTION(zen_fifo_batch_show, zdata->fifo_batch, 0);
  185. #undef SHOW_FUNCTION
  186. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
  187. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
  188. { \
  189. struct zen_data *zdata = e->elevator_data; \
  190. int __data; \
  191. int ret = zen_var_store(&__data, (page), count); \
  192. if (__data < (MIN)) \
  193. __data = (MIN); \
  194. else if (__data > (MAX)) \
  195. __data = (MAX); \
  196. if (__CONV) \
  197. *(__PTR) = msecs_to_jiffies(__data); \
  198. else \
  199. *(__PTR) = __data; \
  200. return ret; \
  201. }
  202. STORE_FUNCTION(zen_sync_expire_store, &zdata->fifo_expire[SYNC], 0, INT_MAX, 1);
  203. STORE_FUNCTION(zen_async_expire_store, &zdata->fifo_expire[ASYNC], 0, INT_MAX, 1);
  204. STORE_FUNCTION(zen_fifo_batch_store, &zdata->fifo_batch, 0, INT_MAX, 0);
  205. #undef STORE_FUNCTION
  206. #define DD_ATTR(name) \
  207. __ATTR(name, S_IRUGO|S_IWUSR, zen_##name##_show, \
  208. zen_##name##_store)
  209. static struct elv_fs_entry zen_attrs[] = {
  210. DD_ATTR(sync_expire),
  211. DD_ATTR(async_expire),
  212. DD_ATTR(fifo_batch),
  213. __ATTR_NULL
  214. };
  215. static struct elevator_type iosched_zen = {
  216. .ops.sq = {
  217. .elevator_merge_req_fn = zen_merged_requests,
  218. .elevator_dispatch_fn = zen_dispatch_requests,
  219. .elevator_add_req_fn = zen_add_request,
  220. .elevator_former_req_fn = elv_rb_former_request,
  221. .elevator_latter_req_fn = elv_rb_latter_request,
  222. .elevator_init_fn = zen_init_queue,
  223. .elevator_exit_fn = zen_exit_queue,
  224. },
  225. .elevator_attrs = zen_attrs,
  226. .elevator_name = "zen",
  227. .elevator_owner = THIS_MODULE,
  228. };
  229. static int __init zen_init(void)
  230. {
  231. return elv_register(&iosched_zen);
  232. }
  233. static void __exit zen_exit(void)
  234. {
  235. elv_unregister(&iosched_zen);
  236. }
  237. module_init(zen_init);
  238. module_exit(zen_exit);
  239. MODULE_AUTHOR("Brandon Berhent");
  240. MODULE_LICENSE("GPL");
  241. MODULE_DESCRIPTION("Zen IO scheduler");
  242. MODULE_VERSION("1.0");