2 * linux/drivers/block/as-iosched.c
4 * Anticipatory & deadline i/o scheduler.
6 * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
7 * Nick Piggin <piggin@cyberone.com.au>
10 #include <linux/kernel.h>
12 #include <linux/blkdev.h>
13 #include <linux/elevator.h>
14 #include <linux/bio.h>
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/compiler.h>
20 #include <linux/hash.h>
21 #include <linux/rbtree.h>
22 #include <linux/interrupt.h>
28 * See Documentation/block/as-iosched.txt
32 * max time before a read is submitted.
34 #define default_read_expire (HZ / 8)
37 * ditto for writes, these limits are not hard, even
38 * if the disk is capable of satisfying them.
40 #define default_write_expire (HZ / 4)
43 * read_batch_expire describes how long we will allow a stream of reads to
44 * persist before looking to see whether it is time to switch over to writes.
46 #define default_read_batch_expire (HZ / 2)
49 * write_batch_expire describes how long we want a stream of writes to run for.
50 * This is not a hard limit, but a target we set for the auto-tuning thingy.
51 * See, the problem is: we can send a lot of writes to disk cache / TCQ in
52 * a short amount of time...
54 #define default_write_batch_expire (HZ / 8)
57 * max time we may wait to anticipate a read (default around 6ms)
59 #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
62 * Keep track of up to 20ms thinktimes. We can go as big as we like here,
63 * however huge values tend to interfere and not decay fast enough. A program
64 * might be in a non-io phase of operation. Waiting on user input for example,
65 * or doing a lengthy computation. A small penalty can be justified there, and
66 * will still catch out those processes that constantly have large thinktimes.
68 #define MAX_THINKTIME (HZ/50UL)
70 /* Bits in as_io_context.state */
72 AS_TASK_RUNNING=0, /* Process has not exitted */
73 AS_TASK_IOSTARTED, /* Process has started some IO */
74 AS_TASK_IORUNNING, /* Process has completed some IO */
77 enum anticipation_status {
78 ANTIC_OFF=0, /* Not anticipating (normal operation) */
79 ANTIC_WAIT_REQ, /* The last read has not yet completed */
80 ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
81 last read (which has completed) */
82 ANTIC_FINISHED, /* Anticipating but have found a candidate
91 struct request_queue *q; /* the "owner" queue */
94 * requests (as_rq s) are present on both sort_list and fifo_list
96 struct rb_root sort_list[2];
97 struct list_head fifo_list[2];
99 struct as_rq *next_arq[2]; /* next in sort order */
100 sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
101 struct list_head *dispatch; /* driver dispatch queue */
102 struct list_head *hash; /* request hash */
104 unsigned long exit_prob; /* probability a task will exit while
106 unsigned long new_ttime_total; /* mean thinktime on new proc */
107 unsigned long new_ttime_mean;
108 u64 new_seek_total; /* mean seek on new proc */
109 sector_t new_seek_mean;
111 unsigned long current_batch_expires;
112 unsigned long last_check_fifo[2];
113 int changed_batch; /* 1: waiting for old batch to end */
114 int new_batch; /* 1: waiting on first read complete */
115 int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
116 int write_batch_count; /* max # of reqs in a write batch */
117 int current_write_count; /* how many requests left this batch */
118 int write_batch_idled; /* has the write batch gone idle? */
121 enum anticipation_status antic_status;
122 unsigned long antic_start; /* jiffies: when it started */
123 struct timer_list antic_timer; /* anticipatory scheduling timer */
124 struct work_struct antic_work; /* Deferred unplugging */
125 struct io_context *io_context; /* Identify the expected process */
126 int ioc_finished; /* IO associated with io_context is finished */
130 * settings that change how the i/o scheduler behaves
132 unsigned long fifo_expire[2];
133 unsigned long batch_expire[2];
134 unsigned long antic_expire;
137 #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
143 AS_RQ_NEW=0, /* New - not referenced and not on any lists */
144 AS_RQ_QUEUED, /* In the request queue. It belongs to the
146 AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
148 AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
151 AS_RQ_POSTSCHED, /* when they shouldn't be */
156 * rbtree index, key is the starting offset
158 struct rb_node rb_node;
161 struct request *request;
163 struct io_context *io_context; /* The submitting task */
166 * request hash, key is the ending offset (for back merge lookup)
168 struct list_head hash;
169 unsigned int on_hash;
174 struct list_head fifo;
175 unsigned long expires;
177 unsigned int is_sync;
178 enum arq_state state;
181 #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
183 static kmem_cache_t *arq_pool;
186 * IO Context helper functions
189 /* Called to deallocate the as_io_context */
190 static void free_as_io_context(struct as_io_context *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;
226 * If the current task has no AS IO context then create one and initialise it.
227 * Then take a ref on the task's io context and return it.
229 static struct io_context *as_get_io_context(void)
231 struct io_context *ioc = get_io_context(GFP_ATOMIC);
232 if (ioc && !ioc->aic) {
233 ioc->aic = alloc_as_io_context();
243 * the back merge hash support functions
245 static const int as_hash_shift = 6;
246 #define AS_HASH_BLOCK(sec) ((sec) >> 3)
247 #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
248 #define AS_HASH_ENTRIES (1 << as_hash_shift)
249 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
250 #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
252 static inline void __as_del_arq_hash(struct as_rq *arq)
255 list_del_init(&arq->hash);
258 static inline void as_del_arq_hash(struct as_rq *arq)
261 __as_del_arq_hash(arq);
264 static void as_remove_merge_hints(request_queue_t *q, struct as_rq *arq)
266 as_del_arq_hash(arq);
268 if (q->last_merge == arq->request)
269 q->last_merge = NULL;
272 static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
274 struct request *rq = arq->request;
276 BUG_ON(arq->on_hash);
279 list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
283 * move hot entry to front of chain
285 static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
287 struct request *rq = arq->request;
288 struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
295 if (arq->hash.prev != head) {
296 list_del(&arq->hash);
297 list_add(&arq->hash, head);
301 static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
303 struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
304 struct list_head *entry, *next = hash_list->next;
306 while ((entry = next) != hash_list) {
307 struct as_rq *arq = list_entry_hash(entry);
308 struct request *__rq = arq->request;
312 BUG_ON(!arq->on_hash);
314 if (!rq_mergeable(__rq)) {
315 as_remove_merge_hints(ad->q, arq);
319 if (rq_hash_key(__rq) == offset)
327 * rb tree support functions
330 #define RB_EMPTY(root) ((root)->rb_node == NULL)
331 #define ON_RB(node) ((node)->rb_color != RB_NONE)
332 #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
333 #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
334 #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
335 #define rq_rb_key(rq) (rq)->sector
338 * as_find_first_arq finds the first (lowest sector numbered) request
339 * for the specified data_dir. Used to sweep back to the start of the disk
340 * (1-way elevator) after we process the last (highest sector) request.
342 static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
344 struct rb_node *n = ad->sort_list[data_dir].rb_node;
350 if (n->rb_left == NULL)
351 return rb_entry_arq(n);
358 * Add the request to the rb tree if it is unique. If there is an alias (an
359 * existing request against the same sector), which can happen when using
360 * direct IO, then return the alias.
362 static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
364 struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
365 struct rb_node *parent = NULL;
367 struct request *rq = arq->request;
369 arq->rb_key = rq_rb_key(rq);
373 __arq = rb_entry_arq(parent);
375 if (arq->rb_key < __arq->rb_key)
377 else if (arq->rb_key > __arq->rb_key)
383 rb_link_node(&arq->rb_node, parent, p);
384 rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
389 static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
391 if (!ON_RB(&arq->rb_node)) {
396 rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
397 RB_CLEAR(&arq->rb_node);
400 static struct request *
401 as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
403 struct rb_node *n = ad->sort_list[data_dir].rb_node;
407 arq = rb_entry_arq(n);
409 if (sector < arq->rb_key)
411 else if (sector > arq->rb_key)
421 * IO Scheduler proper
424 #define MAXBACK (1024 * 1024) /*
425 * Maximum distance the disk will go backward
429 #define BACK_PENALTY 2
432 * as_choose_req selects the preferred one of two requests of the same data_dir
433 * ignoring time - eg. timeouts, which is the job of as_dispatch_request
435 static struct as_rq *
436 as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
439 sector_t last, s1, s2, d1, d2;
440 int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
441 const sector_t maxback = MAXBACK;
443 if (arq1 == NULL || arq1 == arq2)
448 data_dir = arq1->is_sync;
450 last = ad->last_sector[data_dir];
451 s1 = arq1->request->sector;
452 s2 = arq2->request->sector;
454 BUG_ON(data_dir != arq2->is_sync);
457 * Strict one way elevator _except_ in the case where we allow
458 * short backward seeks which are biased as twice the cost of a
459 * similar forward seek.
463 else if (s1+maxback >= last)
464 d1 = (last - s1)*BACK_PENALTY;
467 d1 = 0; /* shut up, gcc */
472 else if (s2+maxback >= last)
473 d2 = (last - s2)*BACK_PENALTY;
479 /* Found required data */
480 if (!r1_wrap && r2_wrap)
482 else if (!r2_wrap && r1_wrap)
484 else if (r1_wrap && r2_wrap) {
485 /* both behind the head */
492 /* Both requests in front of the head */
506 * as_find_next_arq finds the next request after @prev in elevator order.
507 * this with as_choose_req form the basis for how the scheduler chooses
508 * what request to process next. Anticipation works on top of this.
510 static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
512 const int data_dir = last->is_sync;
514 struct rb_node *rbnext = rb_next(&last->rb_node);
515 struct rb_node *rbprev = rb_prev(&last->rb_node);
516 struct as_rq *arq_next, *arq_prev;
518 BUG_ON(!ON_RB(&last->rb_node));
521 arq_prev = rb_entry_arq(rbprev);
526 arq_next = rb_entry_arq(rbnext);
528 arq_next = as_find_first_arq(ad, data_dir);
529 if (arq_next == last)
533 ret = as_choose_req(ad, arq_next, arq_prev);
539 * anticipatory scheduling functions follow
543 * as_antic_expired tells us when we have anticipated too long.
544 * The funny "absolute difference" math on the elapsed time is to handle
545 * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
547 static int as_antic_expired(struct as_data *ad)
551 delta_jif = jiffies - ad->antic_start;
552 if (unlikely(delta_jif < 0))
553 delta_jif = -delta_jif;
554 if (delta_jif < ad->antic_expire)
561 * as_antic_waitnext starts anticipating that a nice request will soon be
562 * submitted. See also as_antic_waitreq
564 static void as_antic_waitnext(struct as_data *ad)
566 unsigned long timeout;
568 BUG_ON(ad->antic_status != ANTIC_OFF
569 && ad->antic_status != ANTIC_WAIT_REQ);
571 timeout = ad->antic_start + ad->antic_expire;
573 mod_timer(&ad->antic_timer, timeout);
575 ad->antic_status = ANTIC_WAIT_NEXT;
579 * as_antic_waitreq starts anticipating. We don't start timing the anticipation
580 * until the request that we're anticipating on has finished. This means we
581 * are timing from when the candidate process wakes up hopefully.
583 static void as_antic_waitreq(struct as_data *ad)
585 BUG_ON(ad->antic_status == ANTIC_FINISHED);
586 if (ad->antic_status == ANTIC_OFF) {
587 if (!ad->io_context || ad->ioc_finished)
588 as_antic_waitnext(ad);
590 ad->antic_status = ANTIC_WAIT_REQ;
595 * This is called directly by the functions in this file to stop anticipation.
596 * We kill the timer and schedule a call to the request_fn asap.
598 static void as_antic_stop(struct as_data *ad)
600 int status = ad->antic_status;
602 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
603 if (status == ANTIC_WAIT_NEXT)
604 del_timer(&ad->antic_timer);
605 ad->antic_status = ANTIC_FINISHED;
606 /* see as_work_handler */
607 kblockd_schedule_work(&ad->antic_work);
612 * as_antic_timeout is the timer function set by as_antic_waitnext.
614 static void as_antic_timeout(unsigned long data)
616 struct request_queue *q = (struct request_queue *)data;
617 struct as_data *ad = q->elevator->elevator_data;
620 spin_lock_irqsave(q->queue_lock, flags);
621 if (ad->antic_status == ANTIC_WAIT_REQ
622 || ad->antic_status == ANTIC_WAIT_NEXT) {
623 struct as_io_context *aic = ad->io_context->aic;
625 ad->antic_status = ANTIC_FINISHED;
626 kblockd_schedule_work(&ad->antic_work);
628 if (aic->ttime_samples == 0) {
629 /* process anticipated on has exitted or timed out*/
630 ad->exit_prob = (7*ad->exit_prob + 256)/8;
633 spin_unlock_irqrestore(q->queue_lock, flags);
637 * as_close_req decides if one request is considered "close" to the
638 * previous one issued.
640 static int as_close_req(struct as_data *ad, struct as_rq *arq)
642 unsigned long delay; /* milliseconds */
643 sector_t last = ad->last_sector[ad->batch_data_dir];
644 sector_t next = arq->request->sector;
645 sector_t delta; /* acceptable close offset (in sectors) */
647 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
650 delay = ((jiffies - ad->antic_start) * 1000) / HZ;
654 else if (delay <= 20 && delay <= ad->antic_expire)
655 delta = 64 << (delay-1);
659 return (last - (delta>>1) <= next) && (next <= last + delta);
663 * as_can_break_anticipation returns true if we have been anticipating this
666 * It also returns true if the process against which we are anticipating
667 * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
668 * dispatch it ASAP, because we know that application will not be submitting
671 * If the task which has submitted the request has exitted, break anticipation.
673 * If this task has queued some other IO, do not enter enticipation.
675 static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
677 struct io_context *ioc;
678 struct as_io_context *aic;
681 ioc = ad->io_context;
684 if (arq && ioc == arq->io_context) {
685 /* request from same process */
689 if (ad->ioc_finished && as_antic_expired(ad)) {
691 * In this situation status should really be FINISHED,
692 * however the timer hasn't had the chance to run yet.
701 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702 /* process anticipated on has exitted */
703 if (aic->ttime_samples == 0)
704 ad->exit_prob = (7*ad->exit_prob + 256)/8;
708 if (atomic_read(&aic->nr_queued) > 0) {
709 /* process has more requests queued */
713 if (atomic_read(&aic->nr_dispatched) > 0) {
714 /* process has more requests dispatched */
718 if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, arq)) {
720 * Found a close request that is not one of ours.
722 * This makes close requests from another process reset
723 * our thinktime delay. Is generally useful when there are
724 * two or more cooperating processes working in the same
727 spin_lock(&aic->lock);
728 aic->last_end_request = jiffies;
729 spin_unlock(&aic->lock);
734 if (aic->ttime_samples == 0) {
735 if (ad->new_ttime_mean > ad->antic_expire)
737 if (ad->exit_prob > 128)
739 } else if (aic->ttime_mean > ad->antic_expire) {
740 /* the process thinks too much between requests */
747 if (ad->last_sector[REQ_SYNC] < arq->request->sector)
748 s = arq->request->sector - ad->last_sector[REQ_SYNC];
750 s = ad->last_sector[REQ_SYNC] - arq->request->sector;
752 if (aic->seek_samples == 0) {
754 * Process has just started IO. Use past statistics to
755 * guage success possibility
757 if (ad->new_seek_mean > s) {
758 /* this request is better than what we're expecting */
763 if (aic->seek_mean > s) {
764 /* this request is better than what we're expecting */
773 * as_can_anticipate indicates weather we should either run arq
774 * or keep anticipating a better request.
776 static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
780 * Last request submitted was a write
784 if (ad->antic_status == ANTIC_FINISHED)
786 * Don't restart if we have just finished. Run the next request
790 if (as_can_break_anticipation(ad, arq))
792 * This request is a good candidate. Don't keep anticipating,
798 * OK from here, we haven't finished, and don't have a decent request!
799 * Status is either ANTIC_OFF so start waiting,
800 * ANTIC_WAIT_REQ so continue waiting for request to finish
801 * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
808 static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, unsigned long ttime)
810 /* fixed point: 1.0 == 1<<8 */
811 if (aic->ttime_samples == 0) {
812 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
813 ad->new_ttime_mean = ad->new_ttime_total / 256;
815 ad->exit_prob = (7*ad->exit_prob)/8;
817 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
818 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
819 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
822 static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, sector_t sdist)
826 if (aic->seek_samples == 0) {
827 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
828 ad->new_seek_mean = ad->new_seek_total / 256;
832 * Don't allow the seek distance to get too large from the
833 * odd fragment, pagein, etc
835 if (aic->seek_samples <= 60) /* second&third seek */
836 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
838 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
840 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
841 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
842 total = aic->seek_total + (aic->seek_samples/2);
843 do_div(total, aic->seek_samples);
844 aic->seek_mean = (sector_t)total;
848 * as_update_iohist keeps a decaying histogram of IO thinktimes, and
849 * updates @aic->ttime_mean based on that. It is called when a new
852 static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, struct request *rq)
854 struct as_rq *arq = RQ_DATA(rq);
855 int data_dir = arq->is_sync;
856 unsigned long thinktime;
862 if (data_dir == REQ_SYNC) {
863 unsigned long in_flight = atomic_read(&aic->nr_queued)
864 + atomic_read(&aic->nr_dispatched);
865 spin_lock(&aic->lock);
866 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
867 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
868 /* Calculate read -> read thinktime */
869 if (test_bit(AS_TASK_IORUNNING, &aic->state)
871 thinktime = jiffies - aic->last_end_request;
872 thinktime = min(thinktime, MAX_THINKTIME-1);
875 as_update_thinktime(ad, aic, thinktime);
877 /* Calculate read -> read seek distance */
878 if (aic->last_request_pos < rq->sector)
879 seek_dist = rq->sector - aic->last_request_pos;
881 seek_dist = aic->last_request_pos - rq->sector;
882 as_update_seekdist(ad, aic, seek_dist);
884 aic->last_request_pos = rq->sector + rq->nr_sectors;
885 set_bit(AS_TASK_IOSTARTED, &aic->state);
886 spin_unlock(&aic->lock);
891 * as_update_arq must be called whenever a request (arq) is added to
892 * the sort_list. This function keeps caches up to date, and checks if the
893 * request might be one we are "anticipating"
895 static void as_update_arq(struct as_data *ad, struct as_rq *arq)
897 const int data_dir = arq->is_sync;
899 /* keep the next_arq cache up to date */
900 ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
903 * have we been anticipating this request?
904 * or does it come from the same process as the one we are anticipating
907 if (ad->antic_status == ANTIC_WAIT_REQ
908 || ad->antic_status == ANTIC_WAIT_NEXT) {
909 if (as_can_break_anticipation(ad, arq))
915 * Gathers timings and resizes the write batch automatically
917 static void update_write_batch(struct as_data *ad)
919 unsigned long batch = ad->batch_expire[REQ_ASYNC];
922 write_time = (jiffies - ad->current_batch_expires) + batch;
926 if (write_time > batch && !ad->write_batch_idled) {
927 if (write_time > batch * 3)
928 ad->write_batch_count /= 2;
930 ad->write_batch_count--;
931 } else if (write_time < batch && ad->current_write_count == 0) {
932 if (batch > write_time * 3)
933 ad->write_batch_count *= 2;
935 ad->write_batch_count++;
938 if (ad->write_batch_count < 1)
939 ad->write_batch_count = 1;
943 * as_completed_request is to be called when a request has completed and
944 * returned something to the requesting process, be it an error or data.
946 static void as_completed_request(request_queue_t *q, struct request *rq)
948 struct as_data *ad = q->elevator->elevator_data;
949 struct as_rq *arq = RQ_DATA(rq);
951 WARN_ON(!list_empty(&rq->queuelist));
953 if (arq->state == AS_RQ_PRESCHED) {
954 WARN_ON(arq->io_context);
958 if (arq->state == AS_RQ_MERGED)
961 if (arq->state != AS_RQ_REMOVED) {
962 printk("arq->state %d\n", arq->state);
967 if (!blk_fs_request(rq))
970 if (ad->changed_batch && ad->nr_dispatched == 1) {
971 kblockd_schedule_work(&ad->antic_work);
972 ad->changed_batch = 0;
974 if (ad->batch_data_dir == REQ_SYNC)
977 WARN_ON(ad->nr_dispatched == 0);
981 * Start counting the batch from when a request of that direction is
982 * actually serviced. This should help devices with big TCQ windows
983 * and writeback caches
985 if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
986 update_write_batch(ad);
987 ad->current_batch_expires = jiffies +
988 ad->batch_expire[REQ_SYNC];
992 if (ad->io_context == arq->io_context && ad->io_context) {
993 ad->antic_start = jiffies;
994 ad->ioc_finished = 1;
995 if (ad->antic_status == ANTIC_WAIT_REQ) {
997 * We were waiting on this request, now anticipate
1000 as_antic_waitnext(ad);
1005 if (!arq->io_context)
1008 if (arq->is_sync == REQ_SYNC) {
1009 struct as_io_context *aic = arq->io_context->aic;
1011 spin_lock(&aic->lock);
1012 set_bit(AS_TASK_IORUNNING, &aic->state);
1013 aic->last_end_request = jiffies;
1014 spin_unlock(&aic->lock);
1018 put_io_context(arq->io_context);
1020 arq->state = AS_RQ_POSTSCHED;
1024 * as_remove_queued_request removes a request from the pre dispatch queue
1025 * without updating refcounts. It is expected the caller will drop the
1026 * reference unless it replaces the request at somepart of the elevator
1027 * (ie. the dispatch queue)
1029 static void as_remove_queued_request(request_queue_t *q, struct request *rq)
1031 struct as_rq *arq = RQ_DATA(rq);
1032 const int data_dir = arq->is_sync;
1033 struct as_data *ad = q->elevator->elevator_data;
1035 WARN_ON(arq->state != AS_RQ_QUEUED);
1037 if (arq->io_context && arq->io_context->aic) {
1038 BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
1039 atomic_dec(&arq->io_context->aic->nr_queued);
1043 * Update the "next_arq" cache if we are about to remove its
1046 if (ad->next_arq[data_dir] == arq)
1047 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1049 list_del_init(&arq->fifo);
1050 as_remove_merge_hints(q, arq);
1051 as_del_arq_rb(ad, arq);
1055 * as_remove_dispatched_request is called to remove a request which has gone
1056 * to the dispatch list.
1058 static void as_remove_dispatched_request(request_queue_t *q, struct request *rq)
1060 struct as_rq *arq = RQ_DATA(rq);
1061 struct as_io_context *aic;
1068 WARN_ON(arq->state != AS_RQ_DISPATCHED);
1069 WARN_ON(ON_RB(&arq->rb_node));
1070 if (arq->io_context && arq->io_context->aic) {
1071 aic = arq->io_context->aic;
1073 WARN_ON(!atomic_read(&aic->nr_dispatched));
1074 atomic_dec(&aic->nr_dispatched);
1080 * as_remove_request is called when a driver has finished with a request.
1081 * This should be only called for dispatched requests, but for some reason
1082 * a POWER4 box running hwscan it does not.
1084 static void as_remove_request(request_queue_t *q, struct request *rq)
1086 struct as_rq *arq = RQ_DATA(rq);
1088 if (unlikely(arq->state == AS_RQ_NEW))
1091 if (ON_RB(&arq->rb_node)) {
1092 if (arq->state != AS_RQ_QUEUED) {
1093 printk("arq->state %d\n", arq->state);
1098 * We'll lose the aliased request(s) here. I don't think this
1099 * will ever happen, but if it does, hopefully someone will
1102 WARN_ON(!list_empty(&rq->queuelist));
1103 as_remove_queued_request(q, rq);
1105 if (arq->state != AS_RQ_DISPATCHED) {
1106 printk("arq->state %d\n", arq->state);
1110 as_remove_dispatched_request(q, rq);
1113 arq->state = AS_RQ_REMOVED;
1117 * as_fifo_expired returns 0 if there are no expired reads on the fifo,
1118 * 1 otherwise. It is ratelimited so that we only perform the check once per
1119 * `fifo_expire' interval. Otherwise a large number of expired requests
1120 * would create a hopeless seekstorm.
1122 * See as_antic_expired comment.
1124 static int as_fifo_expired(struct as_data *ad, int adir)
1129 delta_jif = jiffies - ad->last_check_fifo[adir];
1130 if (unlikely(delta_jif < 0))
1131 delta_jif = -delta_jif;
1132 if (delta_jif < ad->fifo_expire[adir])
1135 ad->last_check_fifo[adir] = jiffies;
1137 if (list_empty(&ad->fifo_list[adir]))
1140 arq = list_entry_fifo(ad->fifo_list[adir].next);
1142 return time_after(jiffies, arq->expires);
1146 * as_batch_expired returns true if the current batch has expired. A batch
1147 * is a set of reads or a set of writes.
1149 static inline int as_batch_expired(struct as_data *ad)
1151 if (ad->changed_batch || ad->new_batch)
1154 if (ad->batch_data_dir == REQ_SYNC)
1155 /* TODO! add a check so a complete fifo gets written? */
1156 return time_after(jiffies, ad->current_batch_expires);
1158 return time_after(jiffies, ad->current_batch_expires)
1159 || ad->current_write_count == 0;
1163 * move an entry to dispatch queue
1165 static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
1167 struct request *rq = arq->request;
1168 struct list_head *insert;
1169 const int data_dir = arq->is_sync;
1171 BUG_ON(!ON_RB(&arq->rb_node));
1174 ad->antic_status = ANTIC_OFF;
1177 * This has to be set in order to be correctly updated by
1180 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
1182 if (data_dir == REQ_SYNC) {
1183 /* In case we have to anticipate after this */
1184 copy_io_context(&ad->io_context, &arq->io_context);
1186 if (ad->io_context) {
1187 put_io_context(ad->io_context);
1188 ad->io_context = NULL;
1191 if (ad->current_write_count != 0)
1192 ad->current_write_count--;
1194 ad->ioc_finished = 0;
1196 ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
1199 * take it off the sort and fifo list, add to dispatch queue
1201 insert = ad->dispatch->prev;
1203 while (!list_empty(&rq->queuelist)) {
1204 struct request *__rq = list_entry_rq(rq->queuelist.next);
1205 struct as_rq *__arq = RQ_DATA(__rq);
1207 list_move_tail(&__rq->queuelist, ad->dispatch);
1209 if (__arq->io_context && __arq->io_context->aic)
1210 atomic_inc(&__arq->io_context->aic->nr_dispatched);
1212 WARN_ON(__arq->state != AS_RQ_QUEUED);
1213 __arq->state = AS_RQ_DISPATCHED;
1215 ad->nr_dispatched++;
1218 as_remove_queued_request(ad->q, rq);
1219 WARN_ON(arq->state != AS_RQ_QUEUED);
1221 list_add(&rq->queuelist, insert);
1222 arq->state = AS_RQ_DISPATCHED;
1223 if (arq->io_context && arq->io_context->aic)
1224 atomic_inc(&arq->io_context->aic->nr_dispatched);
1225 ad->nr_dispatched++;
1229 * as_dispatch_request selects the best request according to
1230 * read/write expire, batch expire, etc, and moves it to the dispatch
1231 * queue. Returns 1 if a request was found, 0 otherwise.
1233 static int as_dispatch_request(struct as_data *ad)
1236 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1237 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1239 /* Signal that the write batch was uncontended, so we can't time it */
1240 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1241 if (ad->current_write_count == 0 || !writes)
1242 ad->write_batch_idled = 1;
1245 if (!(reads || writes)
1246 || ad->antic_status == ANTIC_WAIT_REQ
1247 || ad->antic_status == ANTIC_WAIT_NEXT
1248 || ad->changed_batch)
1251 if (!(reads && writes && as_batch_expired(ad)) ) {
1253 * batch is still running or no reads or no writes
1255 arq = ad->next_arq[ad->batch_data_dir];
1257 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1258 if (as_fifo_expired(ad, REQ_SYNC))
1261 if (as_can_anticipate(ad, arq)) {
1262 as_antic_waitreq(ad);
1268 /* we have a "next request" */
1269 if (reads && !writes)
1270 ad->current_batch_expires =
1271 jiffies + ad->batch_expire[REQ_SYNC];
1272 goto dispatch_request;
1277 * at this point we are not running a batch. select the appropriate
1278 * data direction (read / write)
1282 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
1284 if (writes && ad->batch_data_dir == REQ_SYNC)
1286 * Last batch was a read, switch to writes
1288 goto dispatch_writes;
1290 if (ad->batch_data_dir == REQ_ASYNC) {
1291 WARN_ON(ad->new_batch);
1292 ad->changed_batch = 1;
1294 ad->batch_data_dir = REQ_SYNC;
1295 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1296 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1297 goto dispatch_request;
1301 * the last batch was a read
1306 BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
1308 if (ad->batch_data_dir == REQ_SYNC) {
1309 ad->changed_batch = 1;
1312 * new_batch might be 1 when the queue runs out of
1313 * reads. A subsequent submission of a write might
1314 * cause a change of batch before the read is finished.
1318 ad->batch_data_dir = REQ_ASYNC;
1319 ad->current_write_count = ad->write_batch_count;
1320 ad->write_batch_idled = 0;
1321 arq = ad->next_arq[ad->batch_data_dir];
1322 goto dispatch_request;
1330 * If a request has expired, service it.
1333 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1335 arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1336 BUG_ON(arq == NULL);
1339 if (ad->changed_batch) {
1340 WARN_ON(ad->new_batch);
1342 if (ad->nr_dispatched)
1345 if (ad->batch_data_dir == REQ_ASYNC)
1346 ad->current_batch_expires = jiffies +
1347 ad->batch_expire[REQ_ASYNC];
1351 ad->changed_batch = 0;
1355 * arq is the selected appropriate request.
1357 as_move_to_dispatch(ad, arq);
1362 static struct request *as_next_request(request_queue_t *q)
1364 struct as_data *ad = q->elevator->elevator_data;
1365 struct request *rq = NULL;
1368 * if there are still requests on the dispatch queue, grab the first
1370 if (!list_empty(ad->dispatch) || as_dispatch_request(ad))
1371 rq = list_entry_rq(ad->dispatch->next);
1377 * Add arq to a list behind alias
1380 as_add_aliased_request(struct as_data *ad, struct as_rq *arq, struct as_rq *alias)
1382 struct request *req = arq->request;
1383 struct list_head *insert = alias->request->queuelist.prev;
1386 * Transfer list of aliases
1388 while (!list_empty(&req->queuelist)) {
1389 struct request *__rq = list_entry_rq(req->queuelist.next);
1390 struct as_rq *__arq = RQ_DATA(__rq);
1392 list_move_tail(&__rq->queuelist, &alias->request->queuelist);
1394 WARN_ON(__arq->state != AS_RQ_QUEUED);
1398 * Another request with the same start sector on the rbtree.
1399 * Link this request to that sector. They are untangled in
1400 * as_move_to_dispatch
1402 list_add(&arq->request->queuelist, insert);
1405 * Don't want to have to handle merges.
1407 as_remove_merge_hints(ad->q, arq);
1411 * add arq to rbtree and fifo
1413 static void as_add_request(struct as_data *ad, struct as_rq *arq)
1415 struct as_rq *alias;
1418 if (rq_data_dir(arq->request) == READ
1419 || current->flags&PF_SYNCWRITE)
1423 data_dir = arq->is_sync;
1425 arq->io_context = as_get_io_context();
1427 if (arq->io_context) {
1428 as_update_iohist(ad, arq->io_context->aic, arq->request);
1429 atomic_inc(&arq->io_context->aic->nr_queued);
1432 alias = as_add_arq_rb(ad, arq);
1435 * set expire time (only used for reads) and add to fifo list
1437 arq->expires = jiffies + ad->fifo_expire[data_dir];
1438 list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
1440 if (rq_mergeable(arq->request)) {
1441 as_add_arq_hash(ad, arq);
1443 if (!ad->q->last_merge)
1444 ad->q->last_merge = arq->request;
1446 as_update_arq(ad, arq); /* keep state machine up to date */
1449 as_add_aliased_request(ad, arq, alias);
1452 * have we been anticipating this request?
1453 * or does it come from the same process as the one we are
1456 if (ad->antic_status == ANTIC_WAIT_REQ
1457 || ad->antic_status == ANTIC_WAIT_NEXT) {
1458 if (as_can_break_anticipation(ad, arq))
1463 arq->state = AS_RQ_QUEUED;
1466 static void as_deactivate_request(request_queue_t *q, struct request *rq)
1468 struct as_data *ad = q->elevator->elevator_data;
1469 struct as_rq *arq = RQ_DATA(rq);
1472 if (arq->state == AS_RQ_REMOVED) {
1473 arq->state = AS_RQ_DISPATCHED;
1474 if (arq->io_context && arq->io_context->aic)
1475 atomic_inc(&arq->io_context->aic->nr_dispatched);
1478 WARN_ON(blk_fs_request(rq)
1479 && (!(rq->flags & (REQ_HARDBARRIER|REQ_SOFTBARRIER))) );
1481 /* Stop anticipating - let this request get through */
1486 * requeue the request. The request has not been completed, nor is it a
1487 * new request, so don't touch accounting.
1489 static void as_requeue_request(request_queue_t *q, struct request *rq)
1491 as_deactivate_request(q, rq);
1492 list_add(&rq->queuelist, &q->queue_head);
1496 * Account a request that is inserted directly onto the dispatch queue.
1497 * arq->io_context->aic->nr_dispatched should not need to be incremented
1498 * because only new requests should come through here: requeues go through
1499 * our explicit requeue handler.
1501 static void as_account_queued_request(struct as_data *ad, struct request *rq)
1503 if (blk_fs_request(rq)) {
1504 struct as_rq *arq = RQ_DATA(rq);
1505 arq->state = AS_RQ_DISPATCHED;
1506 ad->nr_dispatched++;
1511 as_insert_request(request_queue_t *q, struct request *rq, int where)
1513 struct as_data *ad = q->elevator->elevator_data;
1514 struct as_rq *arq = RQ_DATA(rq);
1517 if (arq->state != AS_RQ_PRESCHED) {
1518 printk("arq->state: %d\n", arq->state);
1521 arq->state = AS_RQ_NEW;
1524 /* barriers must flush the reorder queue */
1525 if (unlikely(rq->flags & (REQ_SOFTBARRIER | REQ_HARDBARRIER)
1526 && where == ELEVATOR_INSERT_SORT)) {
1528 where = ELEVATOR_INSERT_BACK;
1532 case ELEVATOR_INSERT_BACK:
1533 while (ad->next_arq[REQ_SYNC])
1534 as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
1536 while (ad->next_arq[REQ_ASYNC])
1537 as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
1539 list_add_tail(&rq->queuelist, ad->dispatch);
1540 as_account_queued_request(ad, rq);
1543 case ELEVATOR_INSERT_FRONT:
1544 list_add(&rq->queuelist, ad->dispatch);
1545 as_account_queued_request(ad, rq);
1548 case ELEVATOR_INSERT_SORT:
1549 BUG_ON(!blk_fs_request(rq));
1550 as_add_request(ad, arq);
1559 * as_queue_empty tells us if there are requests left in the device. It may
1560 * not be the case that a driver can get the next request even if the queue
1561 * is not empty - it is used in the block layer to check for plugging and
1562 * merging opportunities
1564 static int as_queue_empty(request_queue_t *q)
1566 struct as_data *ad = q->elevator->elevator_data;
1568 if (!list_empty(&ad->fifo_list[REQ_ASYNC])
1569 || !list_empty(&ad->fifo_list[REQ_SYNC])
1570 || !list_empty(ad->dispatch))
1576 static struct request *
1577 as_former_request(request_queue_t *q, struct request *rq)
1579 struct as_rq *arq = RQ_DATA(rq);
1580 struct rb_node *rbprev = rb_prev(&arq->rb_node);
1581 struct request *ret = NULL;
1584 ret = rb_entry_arq(rbprev)->request;
1589 static struct request *
1590 as_latter_request(request_queue_t *q, struct request *rq)
1592 struct as_rq *arq = RQ_DATA(rq);
1593 struct rb_node *rbnext = rb_next(&arq->rb_node);
1594 struct request *ret = NULL;
1597 ret = rb_entry_arq(rbnext)->request;
1603 as_merge(request_queue_t *q, struct request **req, struct bio *bio)
1605 struct as_data *ad = q->elevator->elevator_data;
1606 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1607 struct request *__rq;
1611 * try last_merge to avoid going to hash
1613 ret = elv_try_last_merge(q, bio);
1614 if (ret != ELEVATOR_NO_MERGE) {
1615 __rq = q->last_merge;
1620 * see if the merge hash can satisfy a back merge
1622 __rq = as_find_arq_hash(ad, bio->bi_sector);
1624 BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
1626 if (elv_rq_merge_ok(__rq, bio)) {
1627 ret = ELEVATOR_BACK_MERGE;
1633 * check for front merge
1635 __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
1637 BUG_ON(rb_key != rq_rb_key(__rq));
1639 if (elv_rq_merge_ok(__rq, bio)) {
1640 ret = ELEVATOR_FRONT_MERGE;
1645 return ELEVATOR_NO_MERGE;
1647 if (rq_mergeable(__rq))
1648 q->last_merge = __rq;
1651 if (rq_mergeable(__rq))
1652 as_hot_arq_hash(ad, RQ_DATA(__rq));
1658 static void as_merged_request(request_queue_t *q, struct request *req)
1660 struct as_data *ad = q->elevator->elevator_data;
1661 struct as_rq *arq = RQ_DATA(req);
1664 * hash always needs to be repositioned, key is end sector
1666 as_del_arq_hash(arq);
1667 as_add_arq_hash(ad, arq);
1670 * if the merge was a front merge, we need to reposition request
1672 if (rq_rb_key(req) != arq->rb_key) {
1673 struct as_rq *alias, *next_arq = NULL;
1675 if (ad->next_arq[arq->is_sync] == arq)
1676 next_arq = as_find_next_arq(ad, arq);
1679 * Note! We should really be moving any old aliased requests
1680 * off this request and try to insert them into the rbtree. We
1681 * currently don't bother. Ditto the next function.
1683 as_del_arq_rb(ad, arq);
1684 if ((alias = as_add_arq_rb(ad, arq)) ) {
1685 list_del_init(&arq->fifo);
1686 as_add_aliased_request(ad, arq, alias);
1688 ad->next_arq[arq->is_sync] = next_arq;
1691 * Note! At this stage of this and the next function, our next
1692 * request may not be optimal - eg the request may have "grown"
1693 * behind the disk head. We currently don't bother adjusting.
1698 q->last_merge = req;
1702 as_merged_requests(request_queue_t *q, struct request *req,
1703 struct request *next)
1705 struct as_data *ad = q->elevator->elevator_data;
1706 struct as_rq *arq = RQ_DATA(req);
1707 struct as_rq *anext = RQ_DATA(next);
1713 * reposition arq (this is the merged request) in hash, and in rbtree
1714 * in case of a front merge
1716 as_del_arq_hash(arq);
1717 as_add_arq_hash(ad, arq);
1719 if (rq_rb_key(req) != arq->rb_key) {
1720 struct as_rq *alias, *next_arq = NULL;
1722 if (ad->next_arq[arq->is_sync] == arq)
1723 next_arq = as_find_next_arq(ad, arq);
1725 as_del_arq_rb(ad, arq);
1726 if ((alias = as_add_arq_rb(ad, arq)) ) {
1727 list_del_init(&arq->fifo);
1728 as_add_aliased_request(ad, arq, alias);
1730 ad->next_arq[arq->is_sync] = next_arq;
1735 * if anext expires before arq, assign its expire time to arq
1736 * and move into anext position (anext will be deleted) in fifo
1738 if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
1739 if (time_before(anext->expires, arq->expires)) {
1740 list_move(&arq->fifo, &anext->fifo);
1741 arq->expires = anext->expires;
1743 * Don't copy here but swap, because when anext is
1744 * removed below, it must contain the unused context
1746 swap_io_context(&arq->io_context, &anext->io_context);
1751 * Transfer list of aliases
1753 while (!list_empty(&next->queuelist)) {
1754 struct request *__rq = list_entry_rq(next->queuelist.next);
1755 struct as_rq *__arq = RQ_DATA(__rq);
1757 list_move_tail(&__rq->queuelist, &req->queuelist);
1759 WARN_ON(__arq->state != AS_RQ_QUEUED);
1763 * kill knowledge of next, this one is a goner
1765 as_remove_queued_request(q, next);
1767 anext->state = AS_RQ_MERGED;
1771 * This is executed in a "deferred" process context, by kblockd. It calls the
1772 * driver's request_fn so the driver can submit that request.
1774 * IMPORTANT! This guy will reenter the elevator, so set up all queue global
1775 * state before calling, and don't rely on any state over calls.
1777 * FIXME! dispatch queue is not a queue at all!
1779 static void as_work_handler(void *data)
1781 struct request_queue *q = data;
1782 unsigned long flags;
1784 spin_lock_irqsave(q->queue_lock, flags);
1785 if (as_next_request(q))
1787 spin_unlock_irqrestore(q->queue_lock, flags);
1790 static void as_put_request(request_queue_t *q, struct request *rq)
1792 struct as_data *ad = q->elevator->elevator_data;
1793 struct as_rq *arq = RQ_DATA(rq);
1800 if (arq->state != AS_RQ_POSTSCHED && arq->state != AS_RQ_PRESCHED) {
1801 printk("arq->state %d\n", arq->state);
1805 mempool_free(arq, ad->arq_pool);
1806 rq->elevator_private = NULL;
1809 static int as_set_request(request_queue_t *q, struct request *rq,
1810 struct bio *bio, int gfp_mask)
1812 struct as_data *ad = q->elevator->elevator_data;
1813 struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
1816 memset(arq, 0, sizeof(*arq));
1817 RB_CLEAR(&arq->rb_node);
1819 arq->state = AS_RQ_PRESCHED;
1820 arq->io_context = NULL;
1821 INIT_LIST_HEAD(&arq->hash);
1823 INIT_LIST_HEAD(&arq->fifo);
1824 rq->elevator_private = arq;
1831 static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
1833 int ret = ELV_MQUEUE_MAY;
1834 struct as_data *ad = q->elevator->elevator_data;
1835 struct io_context *ioc;
1836 if (ad->antic_status == ANTIC_WAIT_REQ ||
1837 ad->antic_status == ANTIC_WAIT_NEXT) {
1838 ioc = as_get_io_context();
1839 if (ad->io_context == ioc)
1840 ret = ELV_MQUEUE_MUST;
1841 put_io_context(ioc);
1847 static void as_exit_queue(elevator_t *e)
1849 struct as_data *ad = e->elevator_data;
1851 del_timer_sync(&ad->antic_timer);
1854 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1855 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1857 mempool_destroy(ad->arq_pool);
1858 put_io_context(ad->io_context);
1864 * initialize elevator private data (as_data), and alloc a arq for
1865 * each request on the free lists
1867 static int as_init_queue(request_queue_t *q, elevator_t *e)
1875 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
1878 memset(ad, 0, sizeof(*ad));
1880 ad->q = q; /* Identify what queue the data belongs to */
1882 ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
1883 GFP_KERNEL, q->node);
1889 ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1890 mempool_free_slab, arq_pool, q->node);
1891 if (!ad->arq_pool) {
1897 /* anticipatory scheduling helpers */
1898 ad->antic_timer.function = as_antic_timeout;
1899 ad->antic_timer.data = (unsigned long)q;
1900 init_timer(&ad->antic_timer);
1901 INIT_WORK(&ad->antic_work, as_work_handler, q);
1903 for (i = 0; i < AS_HASH_ENTRIES; i++)
1904 INIT_LIST_HEAD(&ad->hash[i]);
1906 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1907 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1908 ad->sort_list[REQ_SYNC] = RB_ROOT;
1909 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1910 ad->dispatch = &q->queue_head;
1911 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1912 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1913 ad->antic_expire = default_antic_expire;
1914 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1915 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1916 e->elevator_data = ad;
1918 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1919 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1920 if (ad->write_batch_count < 2)
1921 ad->write_batch_count = 2;
1929 struct as_fs_entry {
1930 struct attribute attr;
1931 ssize_t (*show)(struct as_data *, char *);
1932 ssize_t (*store)(struct as_data *, const char *, size_t);
1936 as_var_show(unsigned int var, char *page)
1938 return sprintf(page, "%d\n", var);
1942 as_var_store(unsigned long *var, const char *page, size_t count)
1944 char *p = (char *) page;
1946 *var = simple_strtoul(p, &p, 10);
1950 static ssize_t as_est_show(struct as_data *ad, char *page)
1954 pos += sprintf(page+pos, "%lu %% exit probability\n", 100*ad->exit_prob/256);
1955 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1956 pos += sprintf(page+pos, "%llu sectors new seek distance\n", (unsigned long long)ad->new_seek_mean);
1961 #define SHOW_FUNCTION(__FUNC, __VAR) \
1962 static ssize_t __FUNC(struct as_data *ad, char *page) \
1964 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1966 SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
1967 SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
1968 SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
1969 SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
1970 SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
1971 #undef SHOW_FUNCTION
1973 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1974 static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
1976 int ret = as_var_store(__PTR, (page), count); \
1977 if (*(__PTR) < (MIN)) \
1979 else if (*(__PTR) > (MAX)) \
1981 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1984 STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1985 STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1986 STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
1987 STORE_FUNCTION(as_read_batchexpire_store,
1988 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1989 STORE_FUNCTION(as_write_batchexpire_store,
1990 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1991 #undef STORE_FUNCTION
1993 static struct as_fs_entry as_est_entry = {
1994 .attr = {.name = "est_time", .mode = S_IRUGO },
1995 .show = as_est_show,
1997 static struct as_fs_entry as_readexpire_entry = {
1998 .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
1999 .show = as_readexpire_show,
2000 .store = as_readexpire_store,
2002 static struct as_fs_entry as_writeexpire_entry = {
2003 .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
2004 .show = as_writeexpire_show,
2005 .store = as_writeexpire_store,
2007 static struct as_fs_entry as_anticexpire_entry = {
2008 .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
2009 .show = as_anticexpire_show,
2010 .store = as_anticexpire_store,
2012 static struct as_fs_entry as_read_batchexpire_entry = {
2013 .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
2014 .show = as_read_batchexpire_show,
2015 .store = as_read_batchexpire_store,
2017 static struct as_fs_entry as_write_batchexpire_entry = {
2018 .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
2019 .show = as_write_batchexpire_show,
2020 .store = as_write_batchexpire_store,
2023 static struct attribute *default_attrs[] = {
2025 &as_readexpire_entry.attr,
2026 &as_writeexpire_entry.attr,
2027 &as_anticexpire_entry.attr,
2028 &as_read_batchexpire_entry.attr,
2029 &as_write_batchexpire_entry.attr,
2033 #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
2036 as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2038 elevator_t *e = container_of(kobj, elevator_t, kobj);
2039 struct as_fs_entry *entry = to_as(attr);
2044 return entry->show(e->elevator_data, page);
2048 as_attr_store(struct kobject *kobj, struct attribute *attr,
2049 const char *page, size_t length)
2051 elevator_t *e = container_of(kobj, elevator_t, kobj);
2052 struct as_fs_entry *entry = to_as(attr);
2057 return entry->store(e->elevator_data, page, length);
2060 static struct sysfs_ops as_sysfs_ops = {
2061 .show = as_attr_show,
2062 .store = as_attr_store,
2065 static struct kobj_type as_ktype = {
2066 .sysfs_ops = &as_sysfs_ops,
2067 .default_attrs = default_attrs,
2070 static struct elevator_type iosched_as = {
2072 .elevator_merge_fn = as_merge,
2073 .elevator_merged_fn = as_merged_request,
2074 .elevator_merge_req_fn = as_merged_requests,
2075 .elevator_next_req_fn = as_next_request,
2076 .elevator_add_req_fn = as_insert_request,
2077 .elevator_remove_req_fn = as_remove_request,
2078 .elevator_requeue_req_fn = as_requeue_request,
2079 .elevator_deactivate_req_fn = as_deactivate_request,
2080 .elevator_queue_empty_fn = as_queue_empty,
2081 .elevator_completed_req_fn = as_completed_request,
2082 .elevator_former_req_fn = as_former_request,
2083 .elevator_latter_req_fn = as_latter_request,
2084 .elevator_set_req_fn = as_set_request,
2085 .elevator_put_req_fn = as_put_request,
2086 .elevator_may_queue_fn = as_may_queue,
2087 .elevator_init_fn = as_init_queue,
2088 .elevator_exit_fn = as_exit_queue,
2091 .elevator_ktype = &as_ktype,
2092 .elevator_name = "anticipatory",
2093 .elevator_owner = THIS_MODULE,
2096 static int __init as_init(void)
2100 arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
2105 ret = elv_register(&iosched_as);
2108 * don't allow AS to get unregistered, since we would have
2109 * to browse all tasks in the system and release their
2110 * as_io_context first
2112 __module_get(THIS_MODULE);
2116 kmem_cache_destroy(arq_pool);
2120 static void __exit as_exit(void)
2122 kmem_cache_destroy(arq_pool);
2123 elv_unregister(&iosched_as);
2126 module_init(as_init);
2127 module_exit(as_exit);
2129 MODULE_AUTHOR("Nick Piggin");
2130 MODULE_LICENSE("GPL");
2131 MODULE_DESCRIPTION("anticipatory IO scheduler");