3 * Primarily based on Noop, deadline, and SIO IO schedulers.
5 * Copyright (C) 2012 Brandon Berhent <bbedward@gmail.com>
6 * (C) 2014 LoungeKatt <twistedumbrella@gmail.com>
7 * (c) 2015 Fixes to stop crashing on 3.10 by Matthew Alex <matthewalex@outlook.com>
8 * (c) 2016 POrt and fixes for Linux 3.18 by engstk <eng.stk@sapo.pt>
10 * FCFS, dispatches are back-inserted, deadlines ensure fairness.
11 * Should work best with devices where there is no travel delay.
13 #include <linux/blkdev.h>
14 #include <linux/elevator.h>
15 #include <linux/bio.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
20 enum zen_data_dir { ASYNC, SYNC };
22 static const int sync_expire = HZ / 2; /* max time before a sync is submitted. */
23 static const int async_expire = 5 * HZ; /* ditto for async, these limits are SOFT! */
24 static const int fifo_batch = 16;
28 /* Requests are only present on fifo_list */
29 struct list_head fifo_list[2];
31 unsigned int batching; /* number of sequential requests made */
38 static inline struct zen_data *
39 zen_get_data(struct request_queue *q) {
40 return q->elevator->elevator_data;
43 static void zen_dispatch(struct zen_data *, struct request *);
46 zen_merged_requests(struct request_queue *q, struct request *req,
50 * if next expires before rq, assign its expire time to arq
51 * and move into next position (next will be deleted) in fifo
53 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
54 if (time_before(next->fifo_time, req->fifo_time)) {
55 list_move(&req->queuelist, &next->queuelist);
56 req->fifo_time = next->fifo_time;
60 /* next request is gone */
64 static void zen_add_request(struct request_queue *q, struct request *rq)
66 struct zen_data *zdata = zen_get_data(q);
67 const int sync = rq_is_sync(rq);
69 if (zdata->fifo_expire[sync]) {
70 rq->fifo_time = jiffies + zdata->fifo_expire[sync];
71 list_add_tail(&rq->queuelist, &zdata->fifo_list[sync]);
75 static void zen_dispatch(struct zen_data *zdata, struct request *rq)
77 /* Remove request from list and dispatch it */
79 elv_dispatch_add_tail(rq->q, rq);
81 /* Increment # of sequential requests */
86 * get the first expired request in direction ddir
88 static struct request *
89 zen_expired_request(struct zen_data *zdata, int ddir)
93 if (list_empty(&zdata->fifo_list[ddir]))
96 rq = rq_entry_fifo(zdata->fifo_list[ddir].next);
97 if (time_after_eq(jiffies, rq->fifo_time))
104 * zen_check_fifo returns 0 if there are no expired requests on the fifo,
105 * otherwise it returns the next expired request
107 static struct request *
108 zen_check_fifo(struct zen_data *zdata)
110 struct request *rq_sync = zen_expired_request(zdata, SYNC);
111 struct request *rq_async = zen_expired_request(zdata, ASYNC);
113 if (rq_async && rq_sync) {
114 if (time_after(rq_async->fifo_time, rq_sync->fifo_time))
116 } else if (rq_sync) {
118 } else if (rq_async) {
125 static struct request *
126 zen_choose_request(struct zen_data *zdata)
129 * Retrieve request from available fifo list.
130 * Synchronous requests have priority over asynchronous.
132 if (!list_empty(&zdata->fifo_list[SYNC]))
133 return rq_entry_fifo(zdata->fifo_list[SYNC].next);
134 if (!list_empty(&zdata->fifo_list[ASYNC]))
135 return rq_entry_fifo(zdata->fifo_list[ASYNC].next);
140 static int zen_dispatch_requests(struct request_queue *q, int force)
142 struct zen_data *zdata = zen_get_data(q);
143 struct request *rq = NULL;
145 /* Check for and issue expired requests */
146 if (zdata->batching > zdata->fifo_batch) {
148 rq = zen_check_fifo(zdata);
152 rq = zen_choose_request(zdata);
157 zen_dispatch(zdata, rq);
162 static int zen_init_queue(struct request_queue *q, struct elevator_type *e)
164 struct zen_data *zdata;
165 struct elevator_queue *eq;
167 eq = elevator_alloc(q, e);
171 zdata = kmalloc_node(sizeof(*zdata), GFP_KERNEL, q->node);
173 kobject_put(&eq->kobj);
176 eq->elevator_data = zdata;
179 spin_lock_irq(q->queue_lock);
181 spin_unlock_irq(q->queue_lock);
183 INIT_LIST_HEAD(&zdata->fifo_list[SYNC]);
184 INIT_LIST_HEAD(&zdata->fifo_list[ASYNC]);
185 zdata->fifo_expire[SYNC] = sync_expire;
186 zdata->fifo_expire[ASYNC] = async_expire;
187 zdata->fifo_batch = fifo_batch;
191 static void zen_exit_queue(struct elevator_queue *e)
193 struct zen_data *zdata = e->elevator_data;
195 BUG_ON(!list_empty(&zdata->fifo_list[SYNC]));
196 BUG_ON(!list_empty(&zdata->fifo_list[ASYNC]));
202 zen_var_show(int var, char *page)
204 return sprintf(page, "%d\n", var);
208 zen_var_store(int *var, const char *page, size_t count)
210 *var = simple_strtol(page, NULL, 10);
214 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
215 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
217 struct zen_data *zdata = e->elevator_data; \
218 int __data = __VAR; \
220 __data = jiffies_to_msecs(__data); \
221 return zen_var_show(__data, (page)); \
223 SHOW_FUNCTION(zen_sync_expire_show, zdata->fifo_expire[SYNC], 1);
224 SHOW_FUNCTION(zen_async_expire_show, zdata->fifo_expire[ASYNC], 1);
225 SHOW_FUNCTION(zen_fifo_batch_show, zdata->fifo_batch, 0);
228 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
229 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
231 struct zen_data *zdata = e->elevator_data; \
233 int ret = zen_var_store(&__data, (page), count); \
234 if (__data < (MIN)) \
236 else if (__data > (MAX)) \
239 *(__PTR) = msecs_to_jiffies(__data); \
244 STORE_FUNCTION(zen_sync_expire_store, &zdata->fifo_expire[SYNC], 0, INT_MAX, 1);
245 STORE_FUNCTION(zen_async_expire_store, &zdata->fifo_expire[ASYNC], 0, INT_MAX, 1);
246 STORE_FUNCTION(zen_fifo_batch_store, &zdata->fifo_batch, 0, INT_MAX, 0);
247 #undef STORE_FUNCTION
249 #define DD_ATTR(name) \
250 __ATTR(name, S_IRUGO|S_IWUSR, zen_##name##_show, \
253 static struct elv_fs_entry zen_attrs[] = {
254 DD_ATTR(sync_expire),
255 DD_ATTR(async_expire),
260 static struct elevator_type iosched_zen = {
262 .elevator_merge_req_fn = zen_merged_requests,
263 .elevator_dispatch_fn = zen_dispatch_requests,
264 .elevator_add_req_fn = zen_add_request,
265 .elevator_former_req_fn = elv_rb_former_request,
266 .elevator_latter_req_fn = elv_rb_latter_request,
267 .elevator_init_fn = zen_init_queue,
268 .elevator_exit_fn = zen_exit_queue,
270 .elevator_attrs = zen_attrs,
271 .elevator_name = "zen",
272 .elevator_owner = THIS_MODULE,
275 static int __init zen_init(void)
277 return elv_register(&iosched_zen);
280 static void __exit zen_exit(void)
282 elv_unregister(&iosched_zen);
285 module_init(zen_init);
286 module_exit(zen_exit);
289 MODULE_AUTHOR("Brandon Berhent");
290 MODULE_LICENSE("GPL");
291 MODULE_DESCRIPTION("Zen IO scheduler");
292 MODULE_VERSION("1.1");