2 * Anticipatory & deadline i/o scheduler.
4 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
5 * Nick Piggin <nickpiggin@yahoo.com.au>
8 #include <linux/kernel.h>
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 #include <linux/compiler.h>
17 #include <linux/rbtree.h>
18 #include <linux/interrupt.h>
24 * See Documentation/block/as-iosched.txt
28 * max time before a read is submitted.
30 #define default_read_expire (HZ / 8)
33 * ditto for writes, these limits are not hard, even
34 * if the disk is capable of satisfying them.
36 #define default_write_expire (HZ / 4)
39 * read_batch_expire describes how long we will allow a stream of reads to
40 * persist before looking to see whether it is time to switch over to writes.
42 #define default_read_batch_expire (HZ / 2)
45 * write_batch_expire describes how long we want a stream of writes to run for.
46 * This is not a hard limit, but a target we set for the auto-tuning thingy.
47 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
48 * a short amount of time...
50 #define default_write_batch_expire (HZ / 8)
53 * max time we may wait to anticipate a read (default around 6ms)
55 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
58 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
59 * however huge values tend to interfere and not decay fast enough. A program
60 * might be in a non-io phase of operation. Waiting on user input for example,
61 * or doing a lengthy computation. A small penalty can be justified there, and
62 * will still catch out those processes that constantly have large thinktimes.
64 #define MAX_THINKTIME (HZ/50UL)
66 /* Bits in as_io_context.state */
68 AS_TASK_RUNNING
=0, /* Process has not exited */
69 AS_TASK_IOSTARTED
, /* Process has started some IO */
70 AS_TASK_IORUNNING
, /* Process has completed some IO */
73 enum anticipation_status
{
74 ANTIC_OFF
=0, /* Not anticipating (normal operation) */
75 ANTIC_WAIT_REQ
, /* The last read has not yet completed */
76 ANTIC_WAIT_NEXT
, /* Currently anticipating a request vs
77 last read (which has completed) */
78 ANTIC_FINISHED
, /* Anticipating but have found a candidate
87 struct request_queue
*q
; /* the "owner" queue */
90 * requests (as_rq s) are present on both sort_list and fifo_list
92 struct rb_root sort_list
[2];
93 struct list_head fifo_list
[2];
95 struct as_rq
*next_arq
[2]; /* next in sort order */
96 sector_t last_sector
[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
98 unsigned long exit_prob
; /* probability a task will exit while
100 unsigned long exit_no_coop
; /* probablility an exited task will
101 not be part of a later cooperating
103 unsigned long new_ttime_total
; /* mean thinktime on new proc */
104 unsigned long new_ttime_mean
;
105 u64 new_seek_total
; /* mean seek on new proc */
106 sector_t new_seek_mean
;
108 unsigned long current_batch_expires
;
109 unsigned long last_check_fifo
[2];
110 int changed_batch
; /* 1: waiting for old batch to end */
111 int new_batch
; /* 1: waiting on first read complete */
112 int batch_data_dir
; /* current batch REQ_SYNC / REQ_ASYNC */
113 int write_batch_count
; /* max # of reqs in a write batch */
114 int current_write_count
; /* how many requests left this batch */
115 int write_batch_idled
; /* has the write batch gone idle? */
118 enum anticipation_status antic_status
;
119 unsigned long antic_start
; /* jiffies: when it started */
120 struct timer_list antic_timer
; /* anticipatory scheduling timer */
121 struct work_struct antic_work
; /* Deferred unplugging */
122 struct io_context
*io_context
; /* Identify the expected process */
123 int ioc_finished
; /* IO associated with io_context is finished */
127 * settings that change how the i/o scheduler behaves
129 unsigned long fifo_expire
[2];
130 unsigned long batch_expire
[2];
131 unsigned long antic_expire
;
138 AS_RQ_NEW
=0, /* New - not referenced and not on any lists */
139 AS_RQ_QUEUED
, /* In the request queue. It belongs to the
141 AS_RQ_DISPATCHED
, /* On the dispatch list. It belongs to the
143 AS_RQ_PRESCHED
, /* Debug poisoning for requests being used */
146 AS_RQ_POSTSCHED
, /* when they shouldn't be */
150 struct request
*request
;
152 struct io_context
*io_context
; /* The submitting task */
154 unsigned int is_sync
;
155 enum arq_state state
;
158 #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
160 static kmem_cache_t
*arq_pool
;
162 static atomic_t ioc_count
= ATOMIC_INIT(0);
163 static struct completion
*ioc_gone
;
165 static void as_move_to_dispatch(struct as_data
*ad
, struct as_rq
*arq
);
166 static void as_antic_stop(struct as_data
*ad
);
169 * IO Context helper functions
172 /* Called to deallocate the as_io_context */
173 static void free_as_io_context(struct as_io_context
*aic
)
176 if (atomic_dec_and_test(&ioc_count
) && ioc_gone
)
180 static void as_trim(struct io_context
*ioc
)
183 free_as_io_context(ioc
->aic
);
187 /* Called when the task exits */
188 static void exit_as_io_context(struct as_io_context
*aic
)
190 WARN_ON(!test_bit(AS_TASK_RUNNING
, &aic
->state
));
191 clear_bit(AS_TASK_RUNNING
, &aic
->state
);
194 static struct as_io_context
*alloc_as_io_context(void)
196 struct as_io_context
*ret
;
198 ret
= kmalloc(sizeof(*ret
), GFP_ATOMIC
);
200 ret
->dtor
= free_as_io_context
;
201 ret
->exit
= exit_as_io_context
;
202 ret
->state
= 1 << AS_TASK_RUNNING
;
203 atomic_set(&ret
->nr_queued
, 0);
204 atomic_set(&ret
->nr_dispatched
, 0);
205 spin_lock_init(&ret
->lock
);
206 ret
->ttime_total
= 0;
207 ret
->ttime_samples
= 0;
210 ret
->seek_samples
= 0;
212 atomic_inc(&ioc_count
);
219 * If the current task has no AS IO context then create one and initialise it.
220 * Then take a ref on the task's io context and return it.
222 static struct io_context
*as_get_io_context(void)
224 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
);
225 if (ioc
&& !ioc
->aic
) {
226 ioc
->aic
= alloc_as_io_context();
235 static void as_put_io_context(struct as_rq
*arq
)
237 struct as_io_context
*aic
;
239 if (unlikely(!arq
->io_context
))
242 aic
= arq
->io_context
->aic
;
244 if (arq
->is_sync
== REQ_SYNC
&& aic
) {
245 spin_lock(&aic
->lock
);
246 set_bit(AS_TASK_IORUNNING
, &aic
->state
);
247 aic
->last_end_request
= jiffies
;
248 spin_unlock(&aic
->lock
);
251 put_io_context(arq
->io_context
);
255 * rb tree support functions
257 #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
259 static void as_add_arq_rb(struct as_data
*ad
, struct request
*rq
)
261 struct as_rq
*arq
= RQ_DATA(rq
);
262 struct request
*alias
;
264 while ((unlikely(alias
= elv_rb_add(ARQ_RB_ROOT(ad
, arq
), rq
)))) {
265 as_move_to_dispatch(ad
, RQ_DATA(alias
));
270 static inline void as_del_arq_rb(struct as_data
*ad
, struct request
*rq
)
272 elv_rb_del(ARQ_RB_ROOT(ad
, RQ_DATA(rq
)), rq
);
276 * IO Scheduler proper
279 #define MAXBACK (1024 * 1024) /*
280 * Maximum distance the disk will go backward
284 #define BACK_PENALTY 2
287 * as_choose_req selects the preferred one of two requests of the same data_dir
288 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
290 static struct as_rq
*
291 as_choose_req(struct as_data
*ad
, struct as_rq
*arq1
, struct as_rq
*arq2
)
294 sector_t last
, s1
, s2
, d1
, d2
;
295 int r1_wrap
=0, r2_wrap
=0; /* requests are behind the disk head */
296 const sector_t maxback
= MAXBACK
;
298 if (arq1
== NULL
|| arq1
== arq2
)
303 data_dir
= arq1
->is_sync
;
305 last
= ad
->last_sector
[data_dir
];
306 s1
= arq1
->request
->sector
;
307 s2
= arq2
->request
->sector
;
309 BUG_ON(data_dir
!= arq2
->is_sync
);
312 * Strict one way elevator _except_ in the case where we allow
313 * short backward seeks which are biased as twice the cost of a
314 * similar forward seek.
318 else if (s1
+maxback
>= last
)
319 d1
= (last
- s1
)*BACK_PENALTY
;
322 d1
= 0; /* shut up, gcc */
327 else if (s2
+maxback
>= last
)
328 d2
= (last
- s2
)*BACK_PENALTY
;
334 /* Found required data */
335 if (!r1_wrap
&& r2_wrap
)
337 else if (!r2_wrap
&& r1_wrap
)
339 else if (r1_wrap
&& r2_wrap
) {
340 /* both behind the head */
347 /* Both requests in front of the head */
361 * as_find_next_arq finds the next request after @prev in elevator order.
362 * this with as_choose_req form the basis for how the scheduler chooses
363 * what request to process next. Anticipation works on top of this.
365 static struct as_rq
*as_find_next_arq(struct as_data
*ad
, struct as_rq
*arq
)
367 struct request
*last
= arq
->request
;
368 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
369 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
370 struct as_rq
*next
= NULL
, *prev
= NULL
;
372 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
375 prev
= RQ_DATA(rb_entry_rq(rbprev
));
378 next
= RQ_DATA(rb_entry_rq(rbnext
));
380 const int data_dir
= arq
->is_sync
;
382 rbnext
= rb_first(&ad
->sort_list
[data_dir
]);
383 if (rbnext
&& rbnext
!= &last
->rb_node
)
384 next
= RQ_DATA(rb_entry_rq(rbnext
));
387 return as_choose_req(ad
, next
, prev
);
391 * anticipatory scheduling functions follow
395 * as_antic_expired tells us when we have anticipated too long.
396 * The funny "absolute difference" math on the elapsed time is to handle
397 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
399 static int as_antic_expired(struct as_data
*ad
)
403 delta_jif
= jiffies
- ad
->antic_start
;
404 if (unlikely(delta_jif
< 0))
405 delta_jif
= -delta_jif
;
406 if (delta_jif
< ad
->antic_expire
)
413 * as_antic_waitnext starts anticipating that a nice request will soon be
414 * submitted. See also as_antic_waitreq
416 static void as_antic_waitnext(struct as_data
*ad
)
418 unsigned long timeout
;
420 BUG_ON(ad
->antic_status
!= ANTIC_OFF
421 && ad
->antic_status
!= ANTIC_WAIT_REQ
);
423 timeout
= ad
->antic_start
+ ad
->antic_expire
;
425 mod_timer(&ad
->antic_timer
, timeout
);
427 ad
->antic_status
= ANTIC_WAIT_NEXT
;
431 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
432 * until the request that we're anticipating on has finished. This means we
433 * are timing from when the candidate process wakes up hopefully.
435 static void as_antic_waitreq(struct as_data
*ad
)
437 BUG_ON(ad
->antic_status
== ANTIC_FINISHED
);
438 if (ad
->antic_status
== ANTIC_OFF
) {
439 if (!ad
->io_context
|| ad
->ioc_finished
)
440 as_antic_waitnext(ad
);
442 ad
->antic_status
= ANTIC_WAIT_REQ
;
447 * This is called directly by the functions in this file to stop anticipation.
448 * We kill the timer and schedule a call to the request_fn asap.
450 static void as_antic_stop(struct as_data
*ad
)
452 int status
= ad
->antic_status
;
454 if (status
== ANTIC_WAIT_REQ
|| status
== ANTIC_WAIT_NEXT
) {
455 if (status
== ANTIC_WAIT_NEXT
)
456 del_timer(&ad
->antic_timer
);
457 ad
->antic_status
= ANTIC_FINISHED
;
458 /* see as_work_handler */
459 kblockd_schedule_work(&ad
->antic_work
);
464 * as_antic_timeout is the timer function set by as_antic_waitnext.
466 static void as_antic_timeout(unsigned long data
)
468 struct request_queue
*q
= (struct request_queue
*)data
;
469 struct as_data
*ad
= q
->elevator
->elevator_data
;
472 spin_lock_irqsave(q
->queue_lock
, flags
);
473 if (ad
->antic_status
== ANTIC_WAIT_REQ
474 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
475 struct as_io_context
*aic
= ad
->io_context
->aic
;
477 ad
->antic_status
= ANTIC_FINISHED
;
478 kblockd_schedule_work(&ad
->antic_work
);
480 if (aic
->ttime_samples
== 0) {
481 /* process anticipated on has exited or timed out*/
482 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
484 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
485 /* process not "saved" by a cooperating request */
486 ad
->exit_no_coop
= (7*ad
->exit_no_coop
+ 256)/8;
489 spin_unlock_irqrestore(q
->queue_lock
, flags
);
492 static void as_update_thinktime(struct as_data
*ad
, struct as_io_context
*aic
,
495 /* fixed point: 1.0 == 1<<8 */
496 if (aic
->ttime_samples
== 0) {
497 ad
->new_ttime_total
= (7*ad
->new_ttime_total
+ 256*ttime
) / 8;
498 ad
->new_ttime_mean
= ad
->new_ttime_total
/ 256;
500 ad
->exit_prob
= (7*ad
->exit_prob
)/8;
502 aic
->ttime_samples
= (7*aic
->ttime_samples
+ 256) / 8;
503 aic
->ttime_total
= (7*aic
->ttime_total
+ 256*ttime
) / 8;
504 aic
->ttime_mean
= (aic
->ttime_total
+ 128) / aic
->ttime_samples
;
507 static void as_update_seekdist(struct as_data
*ad
, struct as_io_context
*aic
,
512 if (aic
->seek_samples
== 0) {
513 ad
->new_seek_total
= (7*ad
->new_seek_total
+ 256*(u64
)sdist
)/8;
514 ad
->new_seek_mean
= ad
->new_seek_total
/ 256;
518 * Don't allow the seek distance to get too large from the
519 * odd fragment, pagein, etc
521 if (aic
->seek_samples
<= 60) /* second&third seek */
522 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*1024);
524 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*64);
526 aic
->seek_samples
= (7*aic
->seek_samples
+ 256) / 8;
527 aic
->seek_total
= (7*aic
->seek_total
+ (u64
)256*sdist
) / 8;
528 total
= aic
->seek_total
+ (aic
->seek_samples
/2);
529 do_div(total
, aic
->seek_samples
);
530 aic
->seek_mean
= (sector_t
)total
;
534 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
535 * updates @aic->ttime_mean based on that. It is called when a new
538 static void as_update_iohist(struct as_data
*ad
, struct as_io_context
*aic
,
541 struct as_rq
*arq
= RQ_DATA(rq
);
542 int data_dir
= arq
->is_sync
;
543 unsigned long thinktime
= 0;
549 if (data_dir
== REQ_SYNC
) {
550 unsigned long in_flight
= atomic_read(&aic
->nr_queued
)
551 + atomic_read(&aic
->nr_dispatched
);
552 spin_lock(&aic
->lock
);
553 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
) ||
554 test_bit(AS_TASK_IOSTARTED
, &aic
->state
)) {
555 /* Calculate read -> read thinktime */
556 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
)
558 thinktime
= jiffies
- aic
->last_end_request
;
559 thinktime
= min(thinktime
, MAX_THINKTIME
-1);
561 as_update_thinktime(ad
, aic
, thinktime
);
563 /* Calculate read -> read seek distance */
564 if (aic
->last_request_pos
< rq
->sector
)
565 seek_dist
= rq
->sector
- aic
->last_request_pos
;
567 seek_dist
= aic
->last_request_pos
- rq
->sector
;
568 as_update_seekdist(ad
, aic
, seek_dist
);
570 aic
->last_request_pos
= rq
->sector
+ rq
->nr_sectors
;
571 set_bit(AS_TASK_IOSTARTED
, &aic
->state
);
572 spin_unlock(&aic
->lock
);
577 * as_close_req decides if one request is considered "close" to the
578 * previous one issued.
580 static int as_close_req(struct as_data
*ad
, struct as_io_context
*aic
,
583 unsigned long delay
; /* milliseconds */
584 sector_t last
= ad
->last_sector
[ad
->batch_data_dir
];
585 sector_t next
= arq
->request
->sector
;
586 sector_t delta
; /* acceptable close offset (in sectors) */
589 if (ad
->antic_status
== ANTIC_OFF
|| !ad
->ioc_finished
)
592 delay
= ((jiffies
- ad
->antic_start
) * 1000) / HZ
;
596 else if (delay
<= 20 && delay
<= ad
->antic_expire
)
597 delta
= 8192 << delay
;
601 if ((last
<= next
+ (delta
>>1)) && (next
<= last
+ delta
))
609 if (aic
->seek_samples
== 0) {
611 * Process has just started IO. Use past statistics to
612 * gauge success possibility
614 if (ad
->new_seek_mean
> s
) {
615 /* this request is better than what we're expecting */
620 if (aic
->seek_mean
> s
) {
621 /* this request is better than what we're expecting */
630 * as_can_break_anticipation returns true if we have been anticipating this
633 * It also returns true if the process against which we are anticipating
634 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
635 * dispatch it ASAP, because we know that application will not be submitting
638 * If the task which has submitted the request has exited, break anticipation.
640 * If this task has queued some other IO, do not enter enticipation.
642 static int as_can_break_anticipation(struct as_data
*ad
, struct as_rq
*arq
)
644 struct io_context
*ioc
;
645 struct as_io_context
*aic
;
647 ioc
= ad
->io_context
;
650 if (arq
&& ioc
== arq
->io_context
) {
651 /* request from same process */
655 if (ad
->ioc_finished
&& as_antic_expired(ad
)) {
657 * In this situation status should really be FINISHED,
658 * however the timer hasn't had the chance to run yet.
667 if (atomic_read(&aic
->nr_queued
) > 0) {
668 /* process has more requests queued */
672 if (atomic_read(&aic
->nr_dispatched
) > 0) {
673 /* process has more requests dispatched */
677 if (arq
&& arq
->is_sync
== REQ_SYNC
&& as_close_req(ad
, aic
, arq
)) {
679 * Found a close request that is not one of ours.
681 * This makes close requests from another process update
682 * our IO history. Is generally useful when there are
683 * two or more cooperating processes working in the same
686 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
687 if (aic
->ttime_samples
== 0)
688 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
690 ad
->exit_no_coop
= (7*ad
->exit_no_coop
)/8;
693 as_update_iohist(ad
, aic
, arq
->request
);
697 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
698 /* process anticipated on has exited */
699 if (aic
->ttime_samples
== 0)
700 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
702 if (ad
->exit_no_coop
> 128)
706 if (aic
->ttime_samples
== 0) {
707 if (ad
->new_ttime_mean
> ad
->antic_expire
)
709 if (ad
->exit_prob
* ad
->exit_no_coop
> 128*256)
711 } else if (aic
->ttime_mean
> ad
->antic_expire
) {
712 /* the process thinks too much between requests */
720 * as_can_anticipate indicates whether we should either run arq
721 * or keep anticipating a better request.
723 static int as_can_anticipate(struct as_data
*ad
, struct as_rq
*arq
)
727 * Last request submitted was a write
731 if (ad
->antic_status
== ANTIC_FINISHED
)
733 * Don't restart if we have just finished. Run the next request
737 if (as_can_break_anticipation(ad
, arq
))
739 * This request is a good candidate. Don't keep anticipating,
745 * OK from here, we haven't finished, and don't have a decent request!
746 * Status is either ANTIC_OFF so start waiting,
747 * ANTIC_WAIT_REQ so continue waiting for request to finish
748 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
755 * as_update_arq must be called whenever a request (arq) is added to
756 * the sort_list. This function keeps caches up to date, and checks if the
757 * request might be one we are "anticipating"
759 static void as_update_arq(struct as_data
*ad
, struct as_rq
*arq
)
761 const int data_dir
= arq
->is_sync
;
763 /* keep the next_arq cache up to date */
764 ad
->next_arq
[data_dir
] = as_choose_req(ad
, arq
, ad
->next_arq
[data_dir
]);
767 * have we been anticipating this request?
768 * or does it come from the same process as the one we are anticipating
771 if (ad
->antic_status
== ANTIC_WAIT_REQ
772 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
773 if (as_can_break_anticipation(ad
, arq
))
779 * Gathers timings and resizes the write batch automatically
781 static void update_write_batch(struct as_data
*ad
)
783 unsigned long batch
= ad
->batch_expire
[REQ_ASYNC
];
786 write_time
= (jiffies
- ad
->current_batch_expires
) + batch
;
790 if (write_time
> batch
&& !ad
->write_batch_idled
) {
791 if (write_time
> batch
* 3)
792 ad
->write_batch_count
/= 2;
794 ad
->write_batch_count
--;
795 } else if (write_time
< batch
&& ad
->current_write_count
== 0) {
796 if (batch
> write_time
* 3)
797 ad
->write_batch_count
*= 2;
799 ad
->write_batch_count
++;
802 if (ad
->write_batch_count
< 1)
803 ad
->write_batch_count
= 1;
807 * as_completed_request is to be called when a request has completed and
808 * returned something to the requesting process, be it an error or data.
810 static void as_completed_request(request_queue_t
*q
, struct request
*rq
)
812 struct as_data
*ad
= q
->elevator
->elevator_data
;
813 struct as_rq
*arq
= RQ_DATA(rq
);
815 WARN_ON(!list_empty(&rq
->queuelist
));
817 if (arq
->state
!= AS_RQ_REMOVED
) {
818 printk("arq->state %d\n", arq
->state
);
823 if (ad
->changed_batch
&& ad
->nr_dispatched
== 1) {
824 kblockd_schedule_work(&ad
->antic_work
);
825 ad
->changed_batch
= 0;
827 if (ad
->batch_data_dir
== REQ_SYNC
)
830 WARN_ON(ad
->nr_dispatched
== 0);
834 * Start counting the batch from when a request of that direction is
835 * actually serviced. This should help devices with big TCQ windows
836 * and writeback caches
838 if (ad
->new_batch
&& ad
->batch_data_dir
== arq
->is_sync
) {
839 update_write_batch(ad
);
840 ad
->current_batch_expires
= jiffies
+
841 ad
->batch_expire
[REQ_SYNC
];
845 if (ad
->io_context
== arq
->io_context
&& ad
->io_context
) {
846 ad
->antic_start
= jiffies
;
847 ad
->ioc_finished
= 1;
848 if (ad
->antic_status
== ANTIC_WAIT_REQ
) {
850 * We were waiting on this request, now anticipate
853 as_antic_waitnext(ad
);
857 as_put_io_context(arq
);
859 arq
->state
= AS_RQ_POSTSCHED
;
863 * as_remove_queued_request removes a request from the pre dispatch queue
864 * without updating refcounts. It is expected the caller will drop the
865 * reference unless it replaces the request at somepart of the elevator
866 * (ie. the dispatch queue)
868 static void as_remove_queued_request(request_queue_t
*q
, struct request
*rq
)
870 struct as_rq
*arq
= RQ_DATA(rq
);
871 const int data_dir
= arq
->is_sync
;
872 struct as_data
*ad
= q
->elevator
->elevator_data
;
874 WARN_ON(arq
->state
!= AS_RQ_QUEUED
);
876 if (arq
->io_context
&& arq
->io_context
->aic
) {
877 BUG_ON(!atomic_read(&arq
->io_context
->aic
->nr_queued
));
878 atomic_dec(&arq
->io_context
->aic
->nr_queued
);
882 * Update the "next_arq" cache if we are about to remove its
885 if (ad
->next_arq
[data_dir
] == arq
)
886 ad
->next_arq
[data_dir
] = as_find_next_arq(ad
, arq
);
889 as_del_arq_rb(ad
, rq
);
893 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
894 * 1 otherwise. It is ratelimited so that we only perform the check once per
895 * `fifo_expire' interval. Otherwise a large number of expired requests
896 * would create a hopeless seekstorm.
898 * See as_antic_expired comment.
900 static int as_fifo_expired(struct as_data
*ad
, int adir
)
905 delta_jif
= jiffies
- ad
->last_check_fifo
[adir
];
906 if (unlikely(delta_jif
< 0))
907 delta_jif
= -delta_jif
;
908 if (delta_jif
< ad
->fifo_expire
[adir
])
911 ad
->last_check_fifo
[adir
] = jiffies
;
913 if (list_empty(&ad
->fifo_list
[adir
]))
916 rq
= rq_entry_fifo(ad
->fifo_list
[adir
].next
);
918 return time_after(jiffies
, rq_fifo_time(rq
));
922 * as_batch_expired returns true if the current batch has expired. A batch
923 * is a set of reads or a set of writes.
925 static inline int as_batch_expired(struct as_data
*ad
)
927 if (ad
->changed_batch
|| ad
->new_batch
)
930 if (ad
->batch_data_dir
== REQ_SYNC
)
931 /* TODO! add a check so a complete fifo gets written? */
932 return time_after(jiffies
, ad
->current_batch_expires
);
934 return time_after(jiffies
, ad
->current_batch_expires
)
935 || ad
->current_write_count
== 0;
939 * move an entry to dispatch queue
941 static void as_move_to_dispatch(struct as_data
*ad
, struct as_rq
*arq
)
943 struct request
*rq
= arq
->request
;
944 const int data_dir
= arq
->is_sync
;
946 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
949 ad
->antic_status
= ANTIC_OFF
;
952 * This has to be set in order to be correctly updated by
955 ad
->last_sector
[data_dir
] = rq
->sector
+ rq
->nr_sectors
;
957 if (data_dir
== REQ_SYNC
) {
958 /* In case we have to anticipate after this */
959 copy_io_context(&ad
->io_context
, &arq
->io_context
);
961 if (ad
->io_context
) {
962 put_io_context(ad
->io_context
);
963 ad
->io_context
= NULL
;
966 if (ad
->current_write_count
!= 0)
967 ad
->current_write_count
--;
969 ad
->ioc_finished
= 0;
971 ad
->next_arq
[data_dir
] = as_find_next_arq(ad
, arq
);
974 * take it off the sort and fifo list, add to dispatch queue
976 as_remove_queued_request(ad
->q
, rq
);
977 WARN_ON(arq
->state
!= AS_RQ_QUEUED
);
979 elv_dispatch_sort(ad
->q
, rq
);
981 arq
->state
= AS_RQ_DISPATCHED
;
982 if (arq
->io_context
&& arq
->io_context
->aic
)
983 atomic_inc(&arq
->io_context
->aic
->nr_dispatched
);
988 * as_dispatch_request selects the best request according to
989 * read/write expire, batch expire, etc, and moves it to the dispatch
990 * queue. Returns 1 if a request was found, 0 otherwise.
992 static int as_dispatch_request(request_queue_t
*q
, int force
)
994 struct as_data
*ad
= q
->elevator
->elevator_data
;
996 const int reads
= !list_empty(&ad
->fifo_list
[REQ_SYNC
]);
997 const int writes
= !list_empty(&ad
->fifo_list
[REQ_ASYNC
]);
999 if (unlikely(force
)) {
1001 * Forced dispatch, accounting is useless. Reset
1002 * accounting states and dump fifo_lists. Note that
1003 * batch_data_dir is reset to REQ_SYNC to avoid
1004 * screwing write batch accounting as write batch
1005 * accounting occurs on W->R transition.
1009 ad
->batch_data_dir
= REQ_SYNC
;
1010 ad
->changed_batch
= 0;
1013 while (ad
->next_arq
[REQ_SYNC
]) {
1014 as_move_to_dispatch(ad
, ad
->next_arq
[REQ_SYNC
]);
1017 ad
->last_check_fifo
[REQ_SYNC
] = jiffies
;
1019 while (ad
->next_arq
[REQ_ASYNC
]) {
1020 as_move_to_dispatch(ad
, ad
->next_arq
[REQ_ASYNC
]);
1023 ad
->last_check_fifo
[REQ_ASYNC
] = jiffies
;
1028 /* Signal that the write batch was uncontended, so we can't time it */
1029 if (ad
->batch_data_dir
== REQ_ASYNC
&& !reads
) {
1030 if (ad
->current_write_count
== 0 || !writes
)
1031 ad
->write_batch_idled
= 1;
1034 if (!(reads
|| writes
)
1035 || ad
->antic_status
== ANTIC_WAIT_REQ
1036 || ad
->antic_status
== ANTIC_WAIT_NEXT
1037 || ad
->changed_batch
)
1040 if (!(reads
&& writes
&& as_batch_expired(ad
))) {
1042 * batch is still running or no reads or no writes
1044 arq
= ad
->next_arq
[ad
->batch_data_dir
];
1046 if (ad
->batch_data_dir
== REQ_SYNC
&& ad
->antic_expire
) {
1047 if (as_fifo_expired(ad
, REQ_SYNC
))
1050 if (as_can_anticipate(ad
, arq
)) {
1051 as_antic_waitreq(ad
);
1057 /* we have a "next request" */
1058 if (reads
&& !writes
)
1059 ad
->current_batch_expires
=
1060 jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1061 goto dispatch_request
;
1066 * at this point we are not running a batch. select the appropriate
1067 * data direction (read / write)
1071 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_SYNC
]));
1073 if (writes
&& ad
->batch_data_dir
== REQ_SYNC
)
1075 * Last batch was a read, switch to writes
1077 goto dispatch_writes
;
1079 if (ad
->batch_data_dir
== REQ_ASYNC
) {
1080 WARN_ON(ad
->new_batch
);
1081 ad
->changed_batch
= 1;
1083 ad
->batch_data_dir
= REQ_SYNC
;
1084 arq
= RQ_DATA(rq_entry_fifo(ad
->fifo_list
[REQ_SYNC
].next
));
1085 ad
->last_check_fifo
[ad
->batch_data_dir
] = jiffies
;
1086 goto dispatch_request
;
1090 * the last batch was a read
1095 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_ASYNC
]));
1097 if (ad
->batch_data_dir
== REQ_SYNC
) {
1098 ad
->changed_batch
= 1;
1101 * new_batch might be 1 when the queue runs out of
1102 * reads. A subsequent submission of a write might
1103 * cause a change of batch before the read is finished.
1107 ad
->batch_data_dir
= REQ_ASYNC
;
1108 ad
->current_write_count
= ad
->write_batch_count
;
1109 ad
->write_batch_idled
= 0;
1110 arq
= ad
->next_arq
[ad
->batch_data_dir
];
1111 goto dispatch_request
;
1119 * If a request has expired, service it.
1122 if (as_fifo_expired(ad
, ad
->batch_data_dir
)) {
1124 arq
= RQ_DATA(rq_entry_fifo(ad
->fifo_list
[ad
->batch_data_dir
].next
));
1127 if (ad
->changed_batch
) {
1128 WARN_ON(ad
->new_batch
);
1130 if (ad
->nr_dispatched
)
1133 if (ad
->batch_data_dir
== REQ_ASYNC
)
1134 ad
->current_batch_expires
= jiffies
+
1135 ad
->batch_expire
[REQ_ASYNC
];
1139 ad
->changed_batch
= 0;
1143 * arq is the selected appropriate request.
1145 as_move_to_dispatch(ad
, arq
);
1151 * add arq to rbtree and fifo
1153 static void as_add_request(request_queue_t
*q
, struct request
*rq
)
1155 struct as_data
*ad
= q
->elevator
->elevator_data
;
1156 struct as_rq
*arq
= RQ_DATA(rq
);
1159 arq
->state
= AS_RQ_NEW
;
1161 if (rq_data_dir(arq
->request
) == READ
1162 || (arq
->request
->cmd_flags
& REQ_RW_SYNC
))
1166 data_dir
= arq
->is_sync
;
1168 arq
->io_context
= as_get_io_context();
1170 if (arq
->io_context
) {
1171 as_update_iohist(ad
, arq
->io_context
->aic
, arq
->request
);
1172 atomic_inc(&arq
->io_context
->aic
->nr_queued
);
1175 as_add_arq_rb(ad
, rq
);
1178 * set expire time (only used for reads) and add to fifo list
1180 rq_set_fifo_time(rq
, jiffies
+ ad
->fifo_expire
[data_dir
]);
1181 list_add_tail(&rq
->queuelist
, &ad
->fifo_list
[data_dir
]);
1183 as_update_arq(ad
, arq
); /* keep state machine up to date */
1184 arq
->state
= AS_RQ_QUEUED
;
1187 static void as_activate_request(request_queue_t
*q
, struct request
*rq
)
1189 struct as_rq
*arq
= RQ_DATA(rq
);
1191 WARN_ON(arq
->state
!= AS_RQ_DISPATCHED
);
1192 arq
->state
= AS_RQ_REMOVED
;
1193 if (arq
->io_context
&& arq
->io_context
->aic
)
1194 atomic_dec(&arq
->io_context
->aic
->nr_dispatched
);
1197 static void as_deactivate_request(request_queue_t
*q
, struct request
*rq
)
1199 struct as_rq
*arq
= RQ_DATA(rq
);
1201 WARN_ON(arq
->state
!= AS_RQ_REMOVED
);
1202 arq
->state
= AS_RQ_DISPATCHED
;
1203 if (arq
->io_context
&& arq
->io_context
->aic
)
1204 atomic_inc(&arq
->io_context
->aic
->nr_dispatched
);
1208 * as_queue_empty tells us if there are requests left in the device. It may
1209 * not be the case that a driver can get the next request even if the queue
1210 * is not empty - it is used in the block layer to check for plugging and
1211 * merging opportunities
1213 static int as_queue_empty(request_queue_t
*q
)
1215 struct as_data
*ad
= q
->elevator
->elevator_data
;
1217 return list_empty(&ad
->fifo_list
[REQ_ASYNC
])
1218 && list_empty(&ad
->fifo_list
[REQ_SYNC
]);
1222 as_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
1224 struct as_data
*ad
= q
->elevator
->elevator_data
;
1225 sector_t rb_key
= bio
->bi_sector
+ bio_sectors(bio
);
1226 struct request
*__rq
;
1229 * check for front merge
1231 __rq
= elv_rb_find(&ad
->sort_list
[bio_data_dir(bio
)], rb_key
);
1232 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
1234 return ELEVATOR_FRONT_MERGE
;
1237 return ELEVATOR_NO_MERGE
;
1240 static void as_merged_request(request_queue_t
*q
, struct request
*req
, int type
)
1242 struct as_data
*ad
= q
->elevator
->elevator_data
;
1245 * if the merge was a front merge, we need to reposition request
1247 if (type
== ELEVATOR_FRONT_MERGE
) {
1248 as_del_arq_rb(ad
, req
);
1249 as_add_arq_rb(ad
, req
);
1251 * Note! At this stage of this and the next function, our next
1252 * request may not be optimal - eg the request may have "grown"
1253 * behind the disk head. We currently don't bother adjusting.
1258 static void as_merged_requests(request_queue_t
*q
, struct request
*req
,
1259 struct request
*next
)
1261 struct as_rq
*arq
= RQ_DATA(req
);
1262 struct as_rq
*anext
= RQ_DATA(next
);
1268 * if anext expires before arq, assign its expire time to arq
1269 * and move into anext position (anext will be deleted) in fifo
1271 if (!list_empty(&req
->queuelist
) && !list_empty(&next
->queuelist
)) {
1272 if (time_before(rq_fifo_time(next
), rq_fifo_time(req
))) {
1273 list_move(&req
->queuelist
, &next
->queuelist
);
1274 rq_set_fifo_time(req
, rq_fifo_time(next
));
1276 * Don't copy here but swap, because when anext is
1277 * removed below, it must contain the unused context
1279 swap_io_context(&arq
->io_context
, &anext
->io_context
);
1284 * kill knowledge of next, this one is a goner
1286 as_remove_queued_request(q
, next
);
1287 as_put_io_context(anext
);
1289 anext
->state
= AS_RQ_MERGED
;
1293 * This is executed in a "deferred" process context, by kblockd. It calls the
1294 * driver's request_fn so the driver can submit that request.
1296 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1297 * state before calling, and don't rely on any state over calls.
1299 * FIXME! dispatch queue is not a queue at all!
1301 static void as_work_handler(void *data
)
1303 struct request_queue
*q
= data
;
1304 unsigned long flags
;
1306 spin_lock_irqsave(q
->queue_lock
, flags
);
1307 if (!as_queue_empty(q
))
1309 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1312 static void as_put_request(request_queue_t
*q
, struct request
*rq
)
1314 struct as_data
*ad
= q
->elevator
->elevator_data
;
1315 struct as_rq
*arq
= RQ_DATA(rq
);
1322 if (unlikely(arq
->state
!= AS_RQ_POSTSCHED
&&
1323 arq
->state
!= AS_RQ_PRESCHED
&&
1324 arq
->state
!= AS_RQ_MERGED
)) {
1325 printk("arq->state %d\n", arq
->state
);
1329 mempool_free(arq
, ad
->arq_pool
);
1330 rq
->elevator_private
= NULL
;
1333 static int as_set_request(request_queue_t
*q
, struct request
*rq
,
1334 struct bio
*bio
, gfp_t gfp_mask
)
1336 struct as_data
*ad
= q
->elevator
->elevator_data
;
1337 struct as_rq
*arq
= mempool_alloc(ad
->arq_pool
, gfp_mask
);
1340 memset(arq
, 0, sizeof(*arq
));
1342 arq
->state
= AS_RQ_PRESCHED
;
1343 arq
->io_context
= NULL
;
1344 rq
->elevator_private
= arq
;
1351 static int as_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1353 int ret
= ELV_MQUEUE_MAY
;
1354 struct as_data
*ad
= q
->elevator
->elevator_data
;
1355 struct io_context
*ioc
;
1356 if (ad
->antic_status
== ANTIC_WAIT_REQ
||
1357 ad
->antic_status
== ANTIC_WAIT_NEXT
) {
1358 ioc
= as_get_io_context();
1359 if (ad
->io_context
== ioc
)
1360 ret
= ELV_MQUEUE_MUST
;
1361 put_io_context(ioc
);
1367 static void as_exit_queue(elevator_t
*e
)
1369 struct as_data
*ad
= e
->elevator_data
;
1371 del_timer_sync(&ad
->antic_timer
);
1374 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_SYNC
]));
1375 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_ASYNC
]));
1377 mempool_destroy(ad
->arq_pool
);
1378 put_io_context(ad
->io_context
);
1383 * initialize elevator private data (as_data), and alloc a arq for
1384 * each request on the free lists
1386 static void *as_init_queue(request_queue_t
*q
, elevator_t
*e
)
1393 ad
= kmalloc_node(sizeof(*ad
), GFP_KERNEL
, q
->node
);
1396 memset(ad
, 0, sizeof(*ad
));
1398 ad
->q
= q
; /* Identify what queue the data belongs to */
1400 ad
->arq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
1401 mempool_free_slab
, arq_pool
, q
->node
);
1402 if (!ad
->arq_pool
) {
1407 /* anticipatory scheduling helpers */
1408 ad
->antic_timer
.function
= as_antic_timeout
;
1409 ad
->antic_timer
.data
= (unsigned long)q
;
1410 init_timer(&ad
->antic_timer
);
1411 INIT_WORK(&ad
->antic_work
, as_work_handler
, q
);
1413 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_SYNC
]);
1414 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_ASYNC
]);
1415 ad
->sort_list
[REQ_SYNC
] = RB_ROOT
;
1416 ad
->sort_list
[REQ_ASYNC
] = RB_ROOT
;
1417 ad
->fifo_expire
[REQ_SYNC
] = default_read_expire
;
1418 ad
->fifo_expire
[REQ_ASYNC
] = default_write_expire
;
1419 ad
->antic_expire
= default_antic_expire
;
1420 ad
->batch_expire
[REQ_SYNC
] = default_read_batch_expire
;
1421 ad
->batch_expire
[REQ_ASYNC
] = default_write_batch_expire
;
1423 ad
->current_batch_expires
= jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1424 ad
->write_batch_count
= ad
->batch_expire
[REQ_ASYNC
] / 10;
1425 if (ad
->write_batch_count
< 2)
1426 ad
->write_batch_count
= 2;
1436 as_var_show(unsigned int var
, char *page
)
1438 return sprintf(page
, "%d\n", var
);
1442 as_var_store(unsigned long *var
, const char *page
, size_t count
)
1444 char *p
= (char *) page
;
1446 *var
= simple_strtoul(p
, &p
, 10);
1450 static ssize_t
est_time_show(elevator_t
*e
, char *page
)
1452 struct as_data
*ad
= e
->elevator_data
;
1455 pos
+= sprintf(page
+pos
, "%lu %% exit probability\n",
1456 100*ad
->exit_prob
/256);
1457 pos
+= sprintf(page
+pos
, "%lu %% probability of exiting without a "
1458 "cooperating process submitting IO\n",
1459 100*ad
->exit_no_coop
/256);
1460 pos
+= sprintf(page
+pos
, "%lu ms new thinktime\n", ad
->new_ttime_mean
);
1461 pos
+= sprintf(page
+pos
, "%llu sectors new seek distance\n",
1462 (unsigned long long)ad
->new_seek_mean
);
1467 #define SHOW_FUNCTION(__FUNC, __VAR) \
1468 static ssize_t __FUNC(elevator_t *e, char *page) \
1470 struct as_data *ad = e->elevator_data; \
1471 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1473 SHOW_FUNCTION(as_read_expire_show
, ad
->fifo_expire
[REQ_SYNC
]);
1474 SHOW_FUNCTION(as_write_expire_show
, ad
->fifo_expire
[REQ_ASYNC
]);
1475 SHOW_FUNCTION(as_antic_expire_show
, ad
->antic_expire
);
1476 SHOW_FUNCTION(as_read_batch_expire_show
, ad
->batch_expire
[REQ_SYNC
]);
1477 SHOW_FUNCTION(as_write_batch_expire_show
, ad
->batch_expire
[REQ_ASYNC
]);
1478 #undef SHOW_FUNCTION
1480 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1481 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1483 struct as_data *ad = e->elevator_data; \
1484 int ret = as_var_store(__PTR, (page), count); \
1485 if (*(__PTR) < (MIN)) \
1487 else if (*(__PTR) > (MAX)) \
1489 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1492 STORE_FUNCTION(as_read_expire_store
, &ad
->fifo_expire
[REQ_SYNC
], 0, INT_MAX
);
1493 STORE_FUNCTION(as_write_expire_store
, &ad
->fifo_expire
[REQ_ASYNC
], 0, INT_MAX
);
1494 STORE_FUNCTION(as_antic_expire_store
, &ad
->antic_expire
, 0, INT_MAX
);
1495 STORE_FUNCTION(as_read_batch_expire_store
,
1496 &ad
->batch_expire
[REQ_SYNC
], 0, INT_MAX
);
1497 STORE_FUNCTION(as_write_batch_expire_store
,
1498 &ad
->batch_expire
[REQ_ASYNC
], 0, INT_MAX
);
1499 #undef STORE_FUNCTION
1501 #define AS_ATTR(name) \
1502 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
1504 static struct elv_fs_entry as_attrs
[] = {
1505 __ATTR_RO(est_time
),
1506 AS_ATTR(read_expire
),
1507 AS_ATTR(write_expire
),
1508 AS_ATTR(antic_expire
),
1509 AS_ATTR(read_batch_expire
),
1510 AS_ATTR(write_batch_expire
),
1514 static struct elevator_type iosched_as
= {
1516 .elevator_merge_fn
= as_merge
,
1517 .elevator_merged_fn
= as_merged_request
,
1518 .elevator_merge_req_fn
= as_merged_requests
,
1519 .elevator_dispatch_fn
= as_dispatch_request
,
1520 .elevator_add_req_fn
= as_add_request
,
1521 .elevator_activate_req_fn
= as_activate_request
,
1522 .elevator_deactivate_req_fn
= as_deactivate_request
,
1523 .elevator_queue_empty_fn
= as_queue_empty
,
1524 .elevator_completed_req_fn
= as_completed_request
,
1525 .elevator_former_req_fn
= elv_rb_former_request
,
1526 .elevator_latter_req_fn
= elv_rb_latter_request
,
1527 .elevator_set_req_fn
= as_set_request
,
1528 .elevator_put_req_fn
= as_put_request
,
1529 .elevator_may_queue_fn
= as_may_queue
,
1530 .elevator_init_fn
= as_init_queue
,
1531 .elevator_exit_fn
= as_exit_queue
,
1535 .elevator_attrs
= as_attrs
,
1536 .elevator_name
= "anticipatory",
1537 .elevator_owner
= THIS_MODULE
,
1540 static int __init
as_init(void)
1544 arq_pool
= kmem_cache_create("as_arq", sizeof(struct as_rq
),
1549 ret
= elv_register(&iosched_as
);
1552 * don't allow AS to get unregistered, since we would have
1553 * to browse all tasks in the system and release their
1554 * as_io_context first
1556 __module_get(THIS_MODULE
);
1560 kmem_cache_destroy(arq_pool
);
1564 static void __exit
as_exit(void)
1566 DECLARE_COMPLETION(all_gone
);
1567 elv_unregister(&iosched_as
);
1568 ioc_gone
= &all_gone
;
1569 /* ioc_gone's update must be visible before reading ioc_count */
1571 if (atomic_read(&ioc_count
))
1572 wait_for_completion(ioc_gone
);
1574 kmem_cache_destroy(arq_pool
);
1577 module_init(as_init
);
1578 module_exit(as_exit
);
1580 MODULE_AUTHOR("Nick Piggin");
1581 MODULE_LICENSE("GPL");
1582 MODULE_DESCRIPTION("anticipatory IO scheduler");