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
;
134 #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
140 AS_RQ_NEW
=0, /* New - not referenced and not on any lists */
141 AS_RQ_QUEUED
, /* In the request queue. It belongs to the
143 AS_RQ_DISPATCHED
, /* On the dispatch list. It belongs to the
145 AS_RQ_PRESCHED
, /* Debug poisoning for requests being used */
148 AS_RQ_POSTSCHED
, /* when they shouldn't be */
152 struct request
*request
;
154 struct io_context
*io_context
; /* The submitting task */
159 struct list_head fifo
;
160 unsigned long expires
;
162 unsigned int is_sync
;
163 enum arq_state state
;
166 #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
168 static kmem_cache_t
*arq_pool
;
170 static atomic_t ioc_count
= ATOMIC_INIT(0);
171 static struct completion
*ioc_gone
;
173 static void as_move_to_dispatch(struct as_data
*ad
, struct as_rq
*arq
);
174 static void as_antic_stop(struct as_data
*ad
);
177 * IO Context helper functions
180 /* Called to deallocate the as_io_context */
181 static void free_as_io_context(struct as_io_context
*aic
)
184 if (atomic_dec_and_test(&ioc_count
) && ioc_gone
)
188 static void as_trim(struct io_context
*ioc
)
191 free_as_io_context(ioc
->aic
);
195 /* Called when the task exits */
196 static void exit_as_io_context(struct as_io_context
*aic
)
198 WARN_ON(!test_bit(AS_TASK_RUNNING
, &aic
->state
));
199 clear_bit(AS_TASK_RUNNING
, &aic
->state
);
202 static struct as_io_context
*alloc_as_io_context(void)
204 struct as_io_context
*ret
;
206 ret
= kmalloc(sizeof(*ret
), GFP_ATOMIC
);
208 ret
->dtor
= free_as_io_context
;
209 ret
->exit
= exit_as_io_context
;
210 ret
->state
= 1 << AS_TASK_RUNNING
;
211 atomic_set(&ret
->nr_queued
, 0);
212 atomic_set(&ret
->nr_dispatched
, 0);
213 spin_lock_init(&ret
->lock
);
214 ret
->ttime_total
= 0;
215 ret
->ttime_samples
= 0;
218 ret
->seek_samples
= 0;
220 atomic_inc(&ioc_count
);
227 * If the current task has no AS IO context then create one and initialise it.
228 * Then take a ref on the task's io context and return it.
230 static struct io_context
*as_get_io_context(void)
232 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
);
233 if (ioc
&& !ioc
->aic
) {
234 ioc
->aic
= alloc_as_io_context();
243 static void as_put_io_context(struct as_rq
*arq
)
245 struct as_io_context
*aic
;
247 if (unlikely(!arq
->io_context
))
250 aic
= arq
->io_context
->aic
;
252 if (arq
->is_sync
== REQ_SYNC
&& aic
) {
253 spin_lock(&aic
->lock
);
254 set_bit(AS_TASK_IORUNNING
, &aic
->state
);
255 aic
->last_end_request
= jiffies
;
256 spin_unlock(&aic
->lock
);
259 put_io_context(arq
->io_context
);
263 * rb tree support functions
265 #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
267 static void as_add_arq_rb(struct as_data
*ad
, struct request
*rq
)
269 struct as_rq
*arq
= RQ_DATA(rq
);
270 struct request
*alias
;
272 while ((unlikely(alias
= elv_rb_add(ARQ_RB_ROOT(ad
, arq
), rq
)))) {
273 as_move_to_dispatch(ad
, RQ_DATA(alias
));
278 static inline void as_del_arq_rb(struct as_data
*ad
, struct request
*rq
)
280 elv_rb_del(ARQ_RB_ROOT(ad
, RQ_DATA(rq
)), rq
);
284 * IO Scheduler proper
287 #define MAXBACK (1024 * 1024) /*
288 * Maximum distance the disk will go backward
292 #define BACK_PENALTY 2
295 * as_choose_req selects the preferred one of two requests of the same data_dir
296 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
298 static struct as_rq
*
299 as_choose_req(struct as_data
*ad
, struct as_rq
*arq1
, struct as_rq
*arq2
)
302 sector_t last
, s1
, s2
, d1
, d2
;
303 int r1_wrap
=0, r2_wrap
=0; /* requests are behind the disk head */
304 const sector_t maxback
= MAXBACK
;
306 if (arq1
== NULL
|| arq1
== arq2
)
311 data_dir
= arq1
->is_sync
;
313 last
= ad
->last_sector
[data_dir
];
314 s1
= arq1
->request
->sector
;
315 s2
= arq2
->request
->sector
;
317 BUG_ON(data_dir
!= arq2
->is_sync
);
320 * Strict one way elevator _except_ in the case where we allow
321 * short backward seeks which are biased as twice the cost of a
322 * similar forward seek.
326 else if (s1
+maxback
>= last
)
327 d1
= (last
- s1
)*BACK_PENALTY
;
330 d1
= 0; /* shut up, gcc */
335 else if (s2
+maxback
>= last
)
336 d2
= (last
- s2
)*BACK_PENALTY
;
342 /* Found required data */
343 if (!r1_wrap
&& r2_wrap
)
345 else if (!r2_wrap
&& r1_wrap
)
347 else if (r1_wrap
&& r2_wrap
) {
348 /* both behind the head */
355 /* Both requests in front of the head */
369 * as_find_next_arq finds the next request after @prev in elevator order.
370 * this with as_choose_req form the basis for how the scheduler chooses
371 * what request to process next. Anticipation works on top of this.
373 static struct as_rq
*as_find_next_arq(struct as_data
*ad
, struct as_rq
*arq
)
375 struct request
*last
= arq
->request
;
376 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
377 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
378 struct as_rq
*next
= NULL
, *prev
= NULL
;
380 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
383 prev
= RQ_DATA(rb_entry_rq(rbprev
));
386 next
= RQ_DATA(rb_entry_rq(rbnext
));
388 const int data_dir
= arq
->is_sync
;
390 rbnext
= rb_first(&ad
->sort_list
[data_dir
]);
391 if (rbnext
&& rbnext
!= &last
->rb_node
)
392 next
= RQ_DATA(rb_entry_rq(rbnext
));
395 return as_choose_req(ad
, next
, prev
);
399 * anticipatory scheduling functions follow
403 * as_antic_expired tells us when we have anticipated too long.
404 * The funny "absolute difference" math on the elapsed time is to handle
405 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
407 static int as_antic_expired(struct as_data
*ad
)
411 delta_jif
= jiffies
- ad
->antic_start
;
412 if (unlikely(delta_jif
< 0))
413 delta_jif
= -delta_jif
;
414 if (delta_jif
< ad
->antic_expire
)
421 * as_antic_waitnext starts anticipating that a nice request will soon be
422 * submitted. See also as_antic_waitreq
424 static void as_antic_waitnext(struct as_data
*ad
)
426 unsigned long timeout
;
428 BUG_ON(ad
->antic_status
!= ANTIC_OFF
429 && ad
->antic_status
!= ANTIC_WAIT_REQ
);
431 timeout
= ad
->antic_start
+ ad
->antic_expire
;
433 mod_timer(&ad
->antic_timer
, timeout
);
435 ad
->antic_status
= ANTIC_WAIT_NEXT
;
439 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
440 * until the request that we're anticipating on has finished. This means we
441 * are timing from when the candidate process wakes up hopefully.
443 static void as_antic_waitreq(struct as_data
*ad
)
445 BUG_ON(ad
->antic_status
== ANTIC_FINISHED
);
446 if (ad
->antic_status
== ANTIC_OFF
) {
447 if (!ad
->io_context
|| ad
->ioc_finished
)
448 as_antic_waitnext(ad
);
450 ad
->antic_status
= ANTIC_WAIT_REQ
;
455 * This is called directly by the functions in this file to stop anticipation.
456 * We kill the timer and schedule a call to the request_fn asap.
458 static void as_antic_stop(struct as_data
*ad
)
460 int status
= ad
->antic_status
;
462 if (status
== ANTIC_WAIT_REQ
|| status
== ANTIC_WAIT_NEXT
) {
463 if (status
== ANTIC_WAIT_NEXT
)
464 del_timer(&ad
->antic_timer
);
465 ad
->antic_status
= ANTIC_FINISHED
;
466 /* see as_work_handler */
467 kblockd_schedule_work(&ad
->antic_work
);
472 * as_antic_timeout is the timer function set by as_antic_waitnext.
474 static void as_antic_timeout(unsigned long data
)
476 struct request_queue
*q
= (struct request_queue
*)data
;
477 struct as_data
*ad
= q
->elevator
->elevator_data
;
480 spin_lock_irqsave(q
->queue_lock
, flags
);
481 if (ad
->antic_status
== ANTIC_WAIT_REQ
482 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
483 struct as_io_context
*aic
= ad
->io_context
->aic
;
485 ad
->antic_status
= ANTIC_FINISHED
;
486 kblockd_schedule_work(&ad
->antic_work
);
488 if (aic
->ttime_samples
== 0) {
489 /* process anticipated on has exited or timed out*/
490 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
492 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
493 /* process not "saved" by a cooperating request */
494 ad
->exit_no_coop
= (7*ad
->exit_no_coop
+ 256)/8;
497 spin_unlock_irqrestore(q
->queue_lock
, flags
);
500 static void as_update_thinktime(struct as_data
*ad
, struct as_io_context
*aic
,
503 /* fixed point: 1.0 == 1<<8 */
504 if (aic
->ttime_samples
== 0) {
505 ad
->new_ttime_total
= (7*ad
->new_ttime_total
+ 256*ttime
) / 8;
506 ad
->new_ttime_mean
= ad
->new_ttime_total
/ 256;
508 ad
->exit_prob
= (7*ad
->exit_prob
)/8;
510 aic
->ttime_samples
= (7*aic
->ttime_samples
+ 256) / 8;
511 aic
->ttime_total
= (7*aic
->ttime_total
+ 256*ttime
) / 8;
512 aic
->ttime_mean
= (aic
->ttime_total
+ 128) / aic
->ttime_samples
;
515 static void as_update_seekdist(struct as_data
*ad
, struct as_io_context
*aic
,
520 if (aic
->seek_samples
== 0) {
521 ad
->new_seek_total
= (7*ad
->new_seek_total
+ 256*(u64
)sdist
)/8;
522 ad
->new_seek_mean
= ad
->new_seek_total
/ 256;
526 * Don't allow the seek distance to get too large from the
527 * odd fragment, pagein, etc
529 if (aic
->seek_samples
<= 60) /* second&third seek */
530 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*1024);
532 sdist
= min(sdist
, (aic
->seek_mean
* 4) + 2*1024*64);
534 aic
->seek_samples
= (7*aic
->seek_samples
+ 256) / 8;
535 aic
->seek_total
= (7*aic
->seek_total
+ (u64
)256*sdist
) / 8;
536 total
= aic
->seek_total
+ (aic
->seek_samples
/2);
537 do_div(total
, aic
->seek_samples
);
538 aic
->seek_mean
= (sector_t
)total
;
542 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
543 * updates @aic->ttime_mean based on that. It is called when a new
546 static void as_update_iohist(struct as_data
*ad
, struct as_io_context
*aic
,
549 struct as_rq
*arq
= RQ_DATA(rq
);
550 int data_dir
= arq
->is_sync
;
551 unsigned long thinktime
= 0;
557 if (data_dir
== REQ_SYNC
) {
558 unsigned long in_flight
= atomic_read(&aic
->nr_queued
)
559 + atomic_read(&aic
->nr_dispatched
);
560 spin_lock(&aic
->lock
);
561 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
) ||
562 test_bit(AS_TASK_IOSTARTED
, &aic
->state
)) {
563 /* Calculate read -> read thinktime */
564 if (test_bit(AS_TASK_IORUNNING
, &aic
->state
)
566 thinktime
= jiffies
- aic
->last_end_request
;
567 thinktime
= min(thinktime
, MAX_THINKTIME
-1);
569 as_update_thinktime(ad
, aic
, thinktime
);
571 /* Calculate read -> read seek distance */
572 if (aic
->last_request_pos
< rq
->sector
)
573 seek_dist
= rq
->sector
- aic
->last_request_pos
;
575 seek_dist
= aic
->last_request_pos
- rq
->sector
;
576 as_update_seekdist(ad
, aic
, seek_dist
);
578 aic
->last_request_pos
= rq
->sector
+ rq
->nr_sectors
;
579 set_bit(AS_TASK_IOSTARTED
, &aic
->state
);
580 spin_unlock(&aic
->lock
);
585 * as_close_req decides if one request is considered "close" to the
586 * previous one issued.
588 static int as_close_req(struct as_data
*ad
, struct as_io_context
*aic
,
591 unsigned long delay
; /* milliseconds */
592 sector_t last
= ad
->last_sector
[ad
->batch_data_dir
];
593 sector_t next
= arq
->request
->sector
;
594 sector_t delta
; /* acceptable close offset (in sectors) */
597 if (ad
->antic_status
== ANTIC_OFF
|| !ad
->ioc_finished
)
600 delay
= ((jiffies
- ad
->antic_start
) * 1000) / HZ
;
604 else if (delay
<= 20 && delay
<= ad
->antic_expire
)
605 delta
= 8192 << delay
;
609 if ((last
<= next
+ (delta
>>1)) && (next
<= last
+ delta
))
617 if (aic
->seek_samples
== 0) {
619 * Process has just started IO. Use past statistics to
620 * gauge success possibility
622 if (ad
->new_seek_mean
> s
) {
623 /* this request is better than what we're expecting */
628 if (aic
->seek_mean
> s
) {
629 /* this request is better than what we're expecting */
638 * as_can_break_anticipation returns true if we have been anticipating this
641 * It also returns true if the process against which we are anticipating
642 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
643 * dispatch it ASAP, because we know that application will not be submitting
646 * If the task which has submitted the request has exited, break anticipation.
648 * If this task has queued some other IO, do not enter enticipation.
650 static int as_can_break_anticipation(struct as_data
*ad
, struct as_rq
*arq
)
652 struct io_context
*ioc
;
653 struct as_io_context
*aic
;
655 ioc
= ad
->io_context
;
658 if (arq
&& ioc
== arq
->io_context
) {
659 /* request from same process */
663 if (ad
->ioc_finished
&& as_antic_expired(ad
)) {
665 * In this situation status should really be FINISHED,
666 * however the timer hasn't had the chance to run yet.
675 if (atomic_read(&aic
->nr_queued
) > 0) {
676 /* process has more requests queued */
680 if (atomic_read(&aic
->nr_dispatched
) > 0) {
681 /* process has more requests dispatched */
685 if (arq
&& arq
->is_sync
== REQ_SYNC
&& as_close_req(ad
, aic
, arq
)) {
687 * Found a close request that is not one of ours.
689 * This makes close requests from another process update
690 * our IO history. Is generally useful when there are
691 * two or more cooperating processes working in the same
694 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
695 if (aic
->ttime_samples
== 0)
696 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
698 ad
->exit_no_coop
= (7*ad
->exit_no_coop
)/8;
701 as_update_iohist(ad
, aic
, arq
->request
);
705 if (!test_bit(AS_TASK_RUNNING
, &aic
->state
)) {
706 /* process anticipated on has exited */
707 if (aic
->ttime_samples
== 0)
708 ad
->exit_prob
= (7*ad
->exit_prob
+ 256)/8;
710 if (ad
->exit_no_coop
> 128)
714 if (aic
->ttime_samples
== 0) {
715 if (ad
->new_ttime_mean
> ad
->antic_expire
)
717 if (ad
->exit_prob
* ad
->exit_no_coop
> 128*256)
719 } else if (aic
->ttime_mean
> ad
->antic_expire
) {
720 /* the process thinks too much between requests */
728 * as_can_anticipate indicates whether we should either run arq
729 * or keep anticipating a better request.
731 static int as_can_anticipate(struct as_data
*ad
, struct as_rq
*arq
)
735 * Last request submitted was a write
739 if (ad
->antic_status
== ANTIC_FINISHED
)
741 * Don't restart if we have just finished. Run the next request
745 if (as_can_break_anticipation(ad
, arq
))
747 * This request is a good candidate. Don't keep anticipating,
753 * OK from here, we haven't finished, and don't have a decent request!
754 * Status is either ANTIC_OFF so start waiting,
755 * ANTIC_WAIT_REQ so continue waiting for request to finish
756 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
763 * as_update_arq must be called whenever a request (arq) is added to
764 * the sort_list. This function keeps caches up to date, and checks if the
765 * request might be one we are "anticipating"
767 static void as_update_arq(struct as_data
*ad
, struct as_rq
*arq
)
769 const int data_dir
= arq
->is_sync
;
771 /* keep the next_arq cache up to date */
772 ad
->next_arq
[data_dir
] = as_choose_req(ad
, arq
, ad
->next_arq
[data_dir
]);
775 * have we been anticipating this request?
776 * or does it come from the same process as the one we are anticipating
779 if (ad
->antic_status
== ANTIC_WAIT_REQ
780 || ad
->antic_status
== ANTIC_WAIT_NEXT
) {
781 if (as_can_break_anticipation(ad
, arq
))
787 * Gathers timings and resizes the write batch automatically
789 static void update_write_batch(struct as_data
*ad
)
791 unsigned long batch
= ad
->batch_expire
[REQ_ASYNC
];
794 write_time
= (jiffies
- ad
->current_batch_expires
) + batch
;
798 if (write_time
> batch
&& !ad
->write_batch_idled
) {
799 if (write_time
> batch
* 3)
800 ad
->write_batch_count
/= 2;
802 ad
->write_batch_count
--;
803 } else if (write_time
< batch
&& ad
->current_write_count
== 0) {
804 if (batch
> write_time
* 3)
805 ad
->write_batch_count
*= 2;
807 ad
->write_batch_count
++;
810 if (ad
->write_batch_count
< 1)
811 ad
->write_batch_count
= 1;
815 * as_completed_request is to be called when a request has completed and
816 * returned something to the requesting process, be it an error or data.
818 static void as_completed_request(request_queue_t
*q
, struct request
*rq
)
820 struct as_data
*ad
= q
->elevator
->elevator_data
;
821 struct as_rq
*arq
= RQ_DATA(rq
);
823 WARN_ON(!list_empty(&rq
->queuelist
));
825 if (arq
->state
!= AS_RQ_REMOVED
) {
826 printk("arq->state %d\n", arq
->state
);
831 if (ad
->changed_batch
&& ad
->nr_dispatched
== 1) {
832 kblockd_schedule_work(&ad
->antic_work
);
833 ad
->changed_batch
= 0;
835 if (ad
->batch_data_dir
== REQ_SYNC
)
838 WARN_ON(ad
->nr_dispatched
== 0);
842 * Start counting the batch from when a request of that direction is
843 * actually serviced. This should help devices with big TCQ windows
844 * and writeback caches
846 if (ad
->new_batch
&& ad
->batch_data_dir
== arq
->is_sync
) {
847 update_write_batch(ad
);
848 ad
->current_batch_expires
= jiffies
+
849 ad
->batch_expire
[REQ_SYNC
];
853 if (ad
->io_context
== arq
->io_context
&& ad
->io_context
) {
854 ad
->antic_start
= jiffies
;
855 ad
->ioc_finished
= 1;
856 if (ad
->antic_status
== ANTIC_WAIT_REQ
) {
858 * We were waiting on this request, now anticipate
861 as_antic_waitnext(ad
);
865 as_put_io_context(arq
);
867 arq
->state
= AS_RQ_POSTSCHED
;
871 * as_remove_queued_request removes a request from the pre dispatch queue
872 * without updating refcounts. It is expected the caller will drop the
873 * reference unless it replaces the request at somepart of the elevator
874 * (ie. the dispatch queue)
876 static void as_remove_queued_request(request_queue_t
*q
, struct request
*rq
)
878 struct as_rq
*arq
= RQ_DATA(rq
);
879 const int data_dir
= arq
->is_sync
;
880 struct as_data
*ad
= q
->elevator
->elevator_data
;
882 WARN_ON(arq
->state
!= AS_RQ_QUEUED
);
884 if (arq
->io_context
&& arq
->io_context
->aic
) {
885 BUG_ON(!atomic_read(&arq
->io_context
->aic
->nr_queued
));
886 atomic_dec(&arq
->io_context
->aic
->nr_queued
);
890 * Update the "next_arq" cache if we are about to remove its
893 if (ad
->next_arq
[data_dir
] == arq
)
894 ad
->next_arq
[data_dir
] = as_find_next_arq(ad
, arq
);
896 list_del_init(&arq
->fifo
);
897 as_del_arq_rb(ad
, rq
);
901 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
902 * 1 otherwise. It is ratelimited so that we only perform the check once per
903 * `fifo_expire' interval. Otherwise a large number of expired requests
904 * would create a hopeless seekstorm.
906 * See as_antic_expired comment.
908 static int as_fifo_expired(struct as_data
*ad
, int adir
)
913 delta_jif
= jiffies
- ad
->last_check_fifo
[adir
];
914 if (unlikely(delta_jif
< 0))
915 delta_jif
= -delta_jif
;
916 if (delta_jif
< ad
->fifo_expire
[adir
])
919 ad
->last_check_fifo
[adir
] = jiffies
;
921 if (list_empty(&ad
->fifo_list
[adir
]))
924 arq
= list_entry_fifo(ad
->fifo_list
[adir
].next
);
926 return time_after(jiffies
, arq
->expires
);
930 * as_batch_expired returns true if the current batch has expired. A batch
931 * is a set of reads or a set of writes.
933 static inline int as_batch_expired(struct as_data
*ad
)
935 if (ad
->changed_batch
|| ad
->new_batch
)
938 if (ad
->batch_data_dir
== REQ_SYNC
)
939 /* TODO! add a check so a complete fifo gets written? */
940 return time_after(jiffies
, ad
->current_batch_expires
);
942 return time_after(jiffies
, ad
->current_batch_expires
)
943 || ad
->current_write_count
== 0;
947 * move an entry to dispatch queue
949 static void as_move_to_dispatch(struct as_data
*ad
, struct as_rq
*arq
)
951 struct request
*rq
= arq
->request
;
952 const int data_dir
= arq
->is_sync
;
954 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
957 ad
->antic_status
= ANTIC_OFF
;
960 * This has to be set in order to be correctly updated by
963 ad
->last_sector
[data_dir
] = rq
->sector
+ rq
->nr_sectors
;
965 if (data_dir
== REQ_SYNC
) {
966 /* In case we have to anticipate after this */
967 copy_io_context(&ad
->io_context
, &arq
->io_context
);
969 if (ad
->io_context
) {
970 put_io_context(ad
->io_context
);
971 ad
->io_context
= NULL
;
974 if (ad
->current_write_count
!= 0)
975 ad
->current_write_count
--;
977 ad
->ioc_finished
= 0;
979 ad
->next_arq
[data_dir
] = as_find_next_arq(ad
, arq
);
982 * take it off the sort and fifo list, add to dispatch queue
984 as_remove_queued_request(ad
->q
, rq
);
985 WARN_ON(arq
->state
!= AS_RQ_QUEUED
);
987 elv_dispatch_sort(ad
->q
, rq
);
989 arq
->state
= AS_RQ_DISPATCHED
;
990 if (arq
->io_context
&& arq
->io_context
->aic
)
991 atomic_inc(&arq
->io_context
->aic
->nr_dispatched
);
996 * as_dispatch_request selects the best request according to
997 * read/write expire, batch expire, etc, and moves it to the dispatch
998 * queue. Returns 1 if a request was found, 0 otherwise.
1000 static int as_dispatch_request(request_queue_t
*q
, int force
)
1002 struct as_data
*ad
= q
->elevator
->elevator_data
;
1004 const int reads
= !list_empty(&ad
->fifo_list
[REQ_SYNC
]);
1005 const int writes
= !list_empty(&ad
->fifo_list
[REQ_ASYNC
]);
1007 if (unlikely(force
)) {
1009 * Forced dispatch, accounting is useless. Reset
1010 * accounting states and dump fifo_lists. Note that
1011 * batch_data_dir is reset to REQ_SYNC to avoid
1012 * screwing write batch accounting as write batch
1013 * accounting occurs on W->R transition.
1017 ad
->batch_data_dir
= REQ_SYNC
;
1018 ad
->changed_batch
= 0;
1021 while (ad
->next_arq
[REQ_SYNC
]) {
1022 as_move_to_dispatch(ad
, ad
->next_arq
[REQ_SYNC
]);
1025 ad
->last_check_fifo
[REQ_SYNC
] = jiffies
;
1027 while (ad
->next_arq
[REQ_ASYNC
]) {
1028 as_move_to_dispatch(ad
, ad
->next_arq
[REQ_ASYNC
]);
1031 ad
->last_check_fifo
[REQ_ASYNC
] = jiffies
;
1036 /* Signal that the write batch was uncontended, so we can't time it */
1037 if (ad
->batch_data_dir
== REQ_ASYNC
&& !reads
) {
1038 if (ad
->current_write_count
== 0 || !writes
)
1039 ad
->write_batch_idled
= 1;
1042 if (!(reads
|| writes
)
1043 || ad
->antic_status
== ANTIC_WAIT_REQ
1044 || ad
->antic_status
== ANTIC_WAIT_NEXT
1045 || ad
->changed_batch
)
1048 if (!(reads
&& writes
&& as_batch_expired(ad
))) {
1050 * batch is still running or no reads or no writes
1052 arq
= ad
->next_arq
[ad
->batch_data_dir
];
1054 if (ad
->batch_data_dir
== REQ_SYNC
&& ad
->antic_expire
) {
1055 if (as_fifo_expired(ad
, REQ_SYNC
))
1058 if (as_can_anticipate(ad
, arq
)) {
1059 as_antic_waitreq(ad
);
1065 /* we have a "next request" */
1066 if (reads
&& !writes
)
1067 ad
->current_batch_expires
=
1068 jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1069 goto dispatch_request
;
1074 * at this point we are not running a batch. select the appropriate
1075 * data direction (read / write)
1079 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_SYNC
]));
1081 if (writes
&& ad
->batch_data_dir
== REQ_SYNC
)
1083 * Last batch was a read, switch to writes
1085 goto dispatch_writes
;
1087 if (ad
->batch_data_dir
== REQ_ASYNC
) {
1088 WARN_ON(ad
->new_batch
);
1089 ad
->changed_batch
= 1;
1091 ad
->batch_data_dir
= REQ_SYNC
;
1092 arq
= list_entry_fifo(ad
->fifo_list
[ad
->batch_data_dir
].next
);
1093 ad
->last_check_fifo
[ad
->batch_data_dir
] = jiffies
;
1094 goto dispatch_request
;
1098 * the last batch was a read
1103 BUG_ON(RB_EMPTY_ROOT(&ad
->sort_list
[REQ_ASYNC
]));
1105 if (ad
->batch_data_dir
== REQ_SYNC
) {
1106 ad
->changed_batch
= 1;
1109 * new_batch might be 1 when the queue runs out of
1110 * reads. A subsequent submission of a write might
1111 * cause a change of batch before the read is finished.
1115 ad
->batch_data_dir
= REQ_ASYNC
;
1116 ad
->current_write_count
= ad
->write_batch_count
;
1117 ad
->write_batch_idled
= 0;
1118 arq
= ad
->next_arq
[ad
->batch_data_dir
];
1119 goto dispatch_request
;
1127 * If a request has expired, service it.
1130 if (as_fifo_expired(ad
, ad
->batch_data_dir
)) {
1132 arq
= list_entry_fifo(ad
->fifo_list
[ad
->batch_data_dir
].next
);
1133 BUG_ON(arq
== NULL
);
1136 if (ad
->changed_batch
) {
1137 WARN_ON(ad
->new_batch
);
1139 if (ad
->nr_dispatched
)
1142 if (ad
->batch_data_dir
== REQ_ASYNC
)
1143 ad
->current_batch_expires
= jiffies
+
1144 ad
->batch_expire
[REQ_ASYNC
];
1148 ad
->changed_batch
= 0;
1152 * arq is the selected appropriate request.
1154 as_move_to_dispatch(ad
, arq
);
1160 * add arq to rbtree and fifo
1162 static void as_add_request(request_queue_t
*q
, struct request
*rq
)
1164 struct as_data
*ad
= q
->elevator
->elevator_data
;
1165 struct as_rq
*arq
= RQ_DATA(rq
);
1168 arq
->state
= AS_RQ_NEW
;
1170 if (rq_data_dir(arq
->request
) == READ
1171 || (arq
->request
->cmd_flags
& REQ_RW_SYNC
))
1175 data_dir
= arq
->is_sync
;
1177 arq
->io_context
= as_get_io_context();
1179 if (arq
->io_context
) {
1180 as_update_iohist(ad
, arq
->io_context
->aic
, arq
->request
);
1181 atomic_inc(&arq
->io_context
->aic
->nr_queued
);
1184 as_add_arq_rb(ad
, rq
);
1187 * set expire time (only used for reads) and add to fifo list
1189 arq
->expires
= jiffies
+ ad
->fifo_expire
[data_dir
];
1190 list_add_tail(&arq
->fifo
, &ad
->fifo_list
[data_dir
]);
1192 as_update_arq(ad
, arq
); /* keep state machine up to date */
1193 arq
->state
= AS_RQ_QUEUED
;
1196 static void as_activate_request(request_queue_t
*q
, struct request
*rq
)
1198 struct as_rq
*arq
= RQ_DATA(rq
);
1200 WARN_ON(arq
->state
!= AS_RQ_DISPATCHED
);
1201 arq
->state
= AS_RQ_REMOVED
;
1202 if (arq
->io_context
&& arq
->io_context
->aic
)
1203 atomic_dec(&arq
->io_context
->aic
->nr_dispatched
);
1206 static void as_deactivate_request(request_queue_t
*q
, struct request
*rq
)
1208 struct as_rq
*arq
= RQ_DATA(rq
);
1210 WARN_ON(arq
->state
!= AS_RQ_REMOVED
);
1211 arq
->state
= AS_RQ_DISPATCHED
;
1212 if (arq
->io_context
&& arq
->io_context
->aic
)
1213 atomic_inc(&arq
->io_context
->aic
->nr_dispatched
);
1217 * as_queue_empty tells us if there are requests left in the device. It may
1218 * not be the case that a driver can get the next request even if the queue
1219 * is not empty - it is used in the block layer to check for plugging and
1220 * merging opportunities
1222 static int as_queue_empty(request_queue_t
*q
)
1224 struct as_data
*ad
= q
->elevator
->elevator_data
;
1226 return list_empty(&ad
->fifo_list
[REQ_ASYNC
])
1227 && list_empty(&ad
->fifo_list
[REQ_SYNC
]);
1231 as_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
1233 struct as_data
*ad
= q
->elevator
->elevator_data
;
1234 sector_t rb_key
= bio
->bi_sector
+ bio_sectors(bio
);
1235 struct request
*__rq
;
1238 * check for front merge
1240 __rq
= elv_rb_find(&ad
->sort_list
[bio_data_dir(bio
)], rb_key
);
1241 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
1243 return ELEVATOR_FRONT_MERGE
;
1246 return ELEVATOR_NO_MERGE
;
1249 static void as_merged_request(request_queue_t
*q
, struct request
*req
, int type
)
1251 struct as_data
*ad
= q
->elevator
->elevator_data
;
1254 * if the merge was a front merge, we need to reposition request
1256 if (type
== ELEVATOR_FRONT_MERGE
) {
1257 as_del_arq_rb(ad
, req
);
1258 as_add_arq_rb(ad
, req
);
1260 * Note! At this stage of this and the next function, our next
1261 * request may not be optimal - eg the request may have "grown"
1262 * behind the disk head. We currently don't bother adjusting.
1267 static void as_merged_requests(request_queue_t
*q
, struct request
*req
,
1268 struct request
*next
)
1270 struct as_rq
*arq
= RQ_DATA(req
);
1271 struct as_rq
*anext
= RQ_DATA(next
);
1277 * if anext expires before arq, assign its expire time to arq
1278 * and move into anext position (anext will be deleted) in fifo
1280 if (!list_empty(&arq
->fifo
) && !list_empty(&anext
->fifo
)) {
1281 if (time_before(anext
->expires
, arq
->expires
)) {
1282 list_move(&arq
->fifo
, &anext
->fifo
);
1283 arq
->expires
= anext
->expires
;
1285 * Don't copy here but swap, because when anext is
1286 * removed below, it must contain the unused context
1288 swap_io_context(&arq
->io_context
, &anext
->io_context
);
1293 * kill knowledge of next, this one is a goner
1295 as_remove_queued_request(q
, next
);
1296 as_put_io_context(anext
);
1298 anext
->state
= AS_RQ_MERGED
;
1302 * This is executed in a "deferred" process context, by kblockd. It calls the
1303 * driver's request_fn so the driver can submit that request.
1305 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1306 * state before calling, and don't rely on any state over calls.
1308 * FIXME! dispatch queue is not a queue at all!
1310 static void as_work_handler(void *data
)
1312 struct request_queue
*q
= data
;
1313 unsigned long flags
;
1315 spin_lock_irqsave(q
->queue_lock
, flags
);
1316 if (!as_queue_empty(q
))
1318 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1321 static void as_put_request(request_queue_t
*q
, struct request
*rq
)
1323 struct as_data
*ad
= q
->elevator
->elevator_data
;
1324 struct as_rq
*arq
= RQ_DATA(rq
);
1331 if (unlikely(arq
->state
!= AS_RQ_POSTSCHED
&&
1332 arq
->state
!= AS_RQ_PRESCHED
&&
1333 arq
->state
!= AS_RQ_MERGED
)) {
1334 printk("arq->state %d\n", arq
->state
);
1338 mempool_free(arq
, ad
->arq_pool
);
1339 rq
->elevator_private
= NULL
;
1342 static int as_set_request(request_queue_t
*q
, struct request
*rq
,
1343 struct bio
*bio
, gfp_t gfp_mask
)
1345 struct as_data
*ad
= q
->elevator
->elevator_data
;
1346 struct as_rq
*arq
= mempool_alloc(ad
->arq_pool
, gfp_mask
);
1349 memset(arq
, 0, sizeof(*arq
));
1351 arq
->state
= AS_RQ_PRESCHED
;
1352 arq
->io_context
= NULL
;
1353 INIT_LIST_HEAD(&arq
->fifo
);
1354 rq
->elevator_private
= arq
;
1361 static int as_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1363 int ret
= ELV_MQUEUE_MAY
;
1364 struct as_data
*ad
= q
->elevator
->elevator_data
;
1365 struct io_context
*ioc
;
1366 if (ad
->antic_status
== ANTIC_WAIT_REQ
||
1367 ad
->antic_status
== ANTIC_WAIT_NEXT
) {
1368 ioc
= as_get_io_context();
1369 if (ad
->io_context
== ioc
)
1370 ret
= ELV_MQUEUE_MUST
;
1371 put_io_context(ioc
);
1377 static void as_exit_queue(elevator_t
*e
)
1379 struct as_data
*ad
= e
->elevator_data
;
1381 del_timer_sync(&ad
->antic_timer
);
1384 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_SYNC
]));
1385 BUG_ON(!list_empty(&ad
->fifo_list
[REQ_ASYNC
]));
1387 mempool_destroy(ad
->arq_pool
);
1388 put_io_context(ad
->io_context
);
1393 * initialize elevator private data (as_data), and alloc a arq for
1394 * each request on the free lists
1396 static void *as_init_queue(request_queue_t
*q
, elevator_t
*e
)
1403 ad
= kmalloc_node(sizeof(*ad
), GFP_KERNEL
, q
->node
);
1406 memset(ad
, 0, sizeof(*ad
));
1408 ad
->q
= q
; /* Identify what queue the data belongs to */
1410 ad
->arq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
1411 mempool_free_slab
, arq_pool
, q
->node
);
1412 if (!ad
->arq_pool
) {
1417 /* anticipatory scheduling helpers */
1418 ad
->antic_timer
.function
= as_antic_timeout
;
1419 ad
->antic_timer
.data
= (unsigned long)q
;
1420 init_timer(&ad
->antic_timer
);
1421 INIT_WORK(&ad
->antic_work
, as_work_handler
, q
);
1423 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_SYNC
]);
1424 INIT_LIST_HEAD(&ad
->fifo_list
[REQ_ASYNC
]);
1425 ad
->sort_list
[REQ_SYNC
] = RB_ROOT
;
1426 ad
->sort_list
[REQ_ASYNC
] = RB_ROOT
;
1427 ad
->fifo_expire
[REQ_SYNC
] = default_read_expire
;
1428 ad
->fifo_expire
[REQ_ASYNC
] = default_write_expire
;
1429 ad
->antic_expire
= default_antic_expire
;
1430 ad
->batch_expire
[REQ_SYNC
] = default_read_batch_expire
;
1431 ad
->batch_expire
[REQ_ASYNC
] = default_write_batch_expire
;
1433 ad
->current_batch_expires
= jiffies
+ ad
->batch_expire
[REQ_SYNC
];
1434 ad
->write_batch_count
= ad
->batch_expire
[REQ_ASYNC
] / 10;
1435 if (ad
->write_batch_count
< 2)
1436 ad
->write_batch_count
= 2;
1446 as_var_show(unsigned int var
, char *page
)
1448 return sprintf(page
, "%d\n", var
);
1452 as_var_store(unsigned long *var
, const char *page
, size_t count
)
1454 char *p
= (char *) page
;
1456 *var
= simple_strtoul(p
, &p
, 10);
1460 static ssize_t
est_time_show(elevator_t
*e
, char *page
)
1462 struct as_data
*ad
= e
->elevator_data
;
1465 pos
+= sprintf(page
+pos
, "%lu %% exit probability\n",
1466 100*ad
->exit_prob
/256);
1467 pos
+= sprintf(page
+pos
, "%lu %% probability of exiting without a "
1468 "cooperating process submitting IO\n",
1469 100*ad
->exit_no_coop
/256);
1470 pos
+= sprintf(page
+pos
, "%lu ms new thinktime\n", ad
->new_ttime_mean
);
1471 pos
+= sprintf(page
+pos
, "%llu sectors new seek distance\n",
1472 (unsigned long long)ad
->new_seek_mean
);
1477 #define SHOW_FUNCTION(__FUNC, __VAR) \
1478 static ssize_t __FUNC(elevator_t *e, char *page) \
1480 struct as_data *ad = e->elevator_data; \
1481 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1483 SHOW_FUNCTION(as_read_expire_show
, ad
->fifo_expire
[REQ_SYNC
]);
1484 SHOW_FUNCTION(as_write_expire_show
, ad
->fifo_expire
[REQ_ASYNC
]);
1485 SHOW_FUNCTION(as_antic_expire_show
, ad
->antic_expire
);
1486 SHOW_FUNCTION(as_read_batch_expire_show
, ad
->batch_expire
[REQ_SYNC
]);
1487 SHOW_FUNCTION(as_write_batch_expire_show
, ad
->batch_expire
[REQ_ASYNC
]);
1488 #undef SHOW_FUNCTION
1490 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1491 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1493 struct as_data *ad = e->elevator_data; \
1494 int ret = as_var_store(__PTR, (page), count); \
1495 if (*(__PTR) < (MIN)) \
1497 else if (*(__PTR) > (MAX)) \
1499 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1502 STORE_FUNCTION(as_read_expire_store
, &ad
->fifo_expire
[REQ_SYNC
], 0, INT_MAX
);
1503 STORE_FUNCTION(as_write_expire_store
, &ad
->fifo_expire
[REQ_ASYNC
], 0, INT_MAX
);
1504 STORE_FUNCTION(as_antic_expire_store
, &ad
->antic_expire
, 0, INT_MAX
);
1505 STORE_FUNCTION(as_read_batch_expire_store
,
1506 &ad
->batch_expire
[REQ_SYNC
], 0, INT_MAX
);
1507 STORE_FUNCTION(as_write_batch_expire_store
,
1508 &ad
->batch_expire
[REQ_ASYNC
], 0, INT_MAX
);
1509 #undef STORE_FUNCTION
1511 #define AS_ATTR(name) \
1512 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
1514 static struct elv_fs_entry as_attrs
[] = {
1515 __ATTR_RO(est_time
),
1516 AS_ATTR(read_expire
),
1517 AS_ATTR(write_expire
),
1518 AS_ATTR(antic_expire
),
1519 AS_ATTR(read_batch_expire
),
1520 AS_ATTR(write_batch_expire
),
1524 static struct elevator_type iosched_as
= {
1526 .elevator_merge_fn
= as_merge
,
1527 .elevator_merged_fn
= as_merged_request
,
1528 .elevator_merge_req_fn
= as_merged_requests
,
1529 .elevator_dispatch_fn
= as_dispatch_request
,
1530 .elevator_add_req_fn
= as_add_request
,
1531 .elevator_activate_req_fn
= as_activate_request
,
1532 .elevator_deactivate_req_fn
= as_deactivate_request
,
1533 .elevator_queue_empty_fn
= as_queue_empty
,
1534 .elevator_completed_req_fn
= as_completed_request
,
1535 .elevator_former_req_fn
= elv_rb_former_request
,
1536 .elevator_latter_req_fn
= elv_rb_latter_request
,
1537 .elevator_set_req_fn
= as_set_request
,
1538 .elevator_put_req_fn
= as_put_request
,
1539 .elevator_may_queue_fn
= as_may_queue
,
1540 .elevator_init_fn
= as_init_queue
,
1541 .elevator_exit_fn
= as_exit_queue
,
1545 .elevator_attrs
= as_attrs
,
1546 .elevator_name
= "anticipatory",
1547 .elevator_owner
= THIS_MODULE
,
1550 static int __init
as_init(void)
1554 arq_pool
= kmem_cache_create("as_arq", sizeof(struct as_rq
),
1559 ret
= elv_register(&iosched_as
);
1562 * don't allow AS to get unregistered, since we would have
1563 * to browse all tasks in the system and release their
1564 * as_io_context first
1566 __module_get(THIS_MODULE
);
1570 kmem_cache_destroy(arq_pool
);
1574 static void __exit
as_exit(void)
1576 DECLARE_COMPLETION(all_gone
);
1577 elv_unregister(&iosched_as
);
1578 ioc_gone
= &all_gone
;
1579 /* ioc_gone's update must be visible before reading ioc_count */
1581 if (atomic_read(&ioc_count
))
1582 wait_for_completion(ioc_gone
);
1584 kmem_cache_destroy(arq_pool
);
1587 module_init(as_init
);
1588 module_exit(as_exit
);
1590 MODULE_AUTHOR("Nick Piggin");
1591 MODULE_LICENSE("GPL");
1592 MODULE_DESCRIPTION("anticipatory IO scheduler");