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