2 * linux/drivers/block/cfq-iosched.c
4 * CFQ, or complete fairness queueing, disk scheduler.
6 * Based on ideas from a previously unfinished io
7 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
9 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
11 #include <linux/kernel.h>
13 #include <linux/blkdev.h>
14 #include <linux/elevator.h>
15 #include <linux/bio.h>
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/compiler.h>
21 #include <linux/hash.h>
22 #include <linux/rbtree.h>
23 #include <linux/mempool.h>
24 #include <linux/ioprio.h>
25 #include <linux/writeback.h>
30 static int cfq_quantum = 4; /* max queue in one round of service */
31 static int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
32 static int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
33 static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
34 static int cfq_back_penalty = 2; /* penalty of a backwards seek */
36 static int cfq_slice_sync = HZ / 10;
37 static int cfq_slice_async = HZ / 25;
38 static int cfq_slice_async_rq = 2;
39 static int cfq_slice_idle = HZ / 100;
41 #define CFQ_IDLE_GRACE (HZ / 10)
42 #define CFQ_SLICE_SCALE (5)
44 #define CFQ_KEY_ASYNC (0)
45 #define CFQ_KEY_ANY (0xffff)
48 * disable queueing at the driver/hardware level
50 static int cfq_max_depth = 2;
53 * for the hash of cfqq inside the cfqd
55 #define CFQ_QHASH_SHIFT 6
56 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
57 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
60 * for the hash of crq inside the cfqq
62 #define CFQ_MHASH_SHIFT 6
63 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
64 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
65 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
66 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
67 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
69 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
70 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
72 #define RQ_DATA(rq) (rq)->elevator_private
78 #define RB_EMPTY(node) ((node)->rb_node == NULL)
79 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
80 #define RB_CLEAR(node) do { \
81 (node)->rb_parent = NULL; \
82 RB_CLEAR_COLOR((node)); \
83 (node)->rb_right = NULL; \
84 (node)->rb_left = NULL; \
86 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
87 #define ON_RB(node) ((node)->rb_color != RB_NONE)
88 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
89 #define rq_rb_key(rq) (rq)->sector
91 static kmem_cache_t *crq_pool;
92 static kmem_cache_t *cfq_pool;
93 static kmem_cache_t *cfq_ioc_pool;
95 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
96 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
97 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
98 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
103 #define cfq_cfqq_dispatched(cfqq) \
104 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
106 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
108 #define cfq_cfqq_sync(cfqq) \
109 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
112 * Per block device queue structure
116 request_queue_t *queue;
119 * rr list of queues with requests and the count of them
121 struct list_head rr_list[CFQ_PRIO_LISTS];
122 struct list_head busy_rr;
123 struct list_head cur_rr;
124 struct list_head idle_rr;
125 unsigned int busy_queues;
128 * non-ordered list of empty cfqq's
130 struct list_head empty_list;
135 struct hlist_head *cfq_hash;
138 * global crq hash for all queues
140 struct hlist_head *crq_hash;
142 unsigned int max_queued;
149 * schedule slice state info
152 * idle window management
154 struct timer_list idle_slice_timer;
155 struct work_struct unplug_work;
157 struct cfq_queue *active_queue;
158 struct cfq_io_context *active_cic;
159 int cur_prio, cur_end_prio;
160 unsigned int dispatch_slice;
162 struct timer_list idle_class_timer;
164 sector_t last_sector;
165 unsigned long last_end_request;
167 unsigned int rq_starved;
170 * tunables, see top of file
172 unsigned int cfq_quantum;
173 unsigned int cfq_queued;
174 unsigned int cfq_fifo_expire[2];
175 unsigned int cfq_back_penalty;
176 unsigned int cfq_back_max;
177 unsigned int cfq_slice[2];
178 unsigned int cfq_slice_async_rq;
179 unsigned int cfq_slice_idle;
180 unsigned int cfq_max_depth;
184 * Per process-grouping structure
187 /* reference count */
189 /* parent cfq_data */
190 struct cfq_data *cfqd;
191 /* cfqq lookup hash */
192 struct hlist_node cfq_hash;
195 /* on either rr or empty list of cfqd */
196 struct list_head cfq_list;
197 /* sorted list of pending requests */
198 struct rb_root sort_list;
199 /* if fifo isn't expired, next request to serve */
200 struct cfq_rq *next_crq;
201 /* requests queued in sort_list */
203 /* currently allocated requests */
205 /* fifo list of requests in sort_list */
206 struct list_head fifo;
208 unsigned long slice_start;
209 unsigned long slice_end;
210 unsigned long slice_left;
211 unsigned long service_last;
213 /* number of requests that are on the dispatch list */
216 /* io prio of this group */
217 unsigned short ioprio, org_ioprio;
218 unsigned short ioprio_class, org_ioprio_class;
220 /* various state flags, see below */
225 struct rb_node rb_node;
227 struct request *request;
228 struct hlist_node hash;
230 struct cfq_queue *cfq_queue;
231 struct cfq_io_context *io_context;
233 unsigned int crq_flags;
236 enum cfqq_state_flags {
237 CFQ_CFQQ_FLAG_on_rr = 0,
238 CFQ_CFQQ_FLAG_wait_request,
239 CFQ_CFQQ_FLAG_must_alloc,
240 CFQ_CFQQ_FLAG_must_alloc_slice,
241 CFQ_CFQQ_FLAG_must_dispatch,
242 CFQ_CFQQ_FLAG_fifo_expire,
243 CFQ_CFQQ_FLAG_idle_window,
244 CFQ_CFQQ_FLAG_prio_changed,
245 CFQ_CFQQ_FLAG_expired,
248 #define CFQ_CFQQ_FNS(name) \
249 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
251 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
253 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
255 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
257 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
259 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
263 CFQ_CFQQ_FNS(wait_request);
264 CFQ_CFQQ_FNS(must_alloc);
265 CFQ_CFQQ_FNS(must_alloc_slice);
266 CFQ_CFQQ_FNS(must_dispatch);
267 CFQ_CFQQ_FNS(fifo_expire);
268 CFQ_CFQQ_FNS(idle_window);
269 CFQ_CFQQ_FNS(prio_changed);
270 CFQ_CFQQ_FNS(expired);
273 enum cfq_rq_state_flags {
274 CFQ_CRQ_FLAG_in_flight = 0,
275 CFQ_CRQ_FLAG_in_driver,
276 CFQ_CRQ_FLAG_is_sync,
277 CFQ_CRQ_FLAG_requeued,
280 #define CFQ_CRQ_FNS(name) \
281 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
283 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
285 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
287 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
289 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
291 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
294 CFQ_CRQ_FNS(in_flight);
295 CFQ_CRQ_FNS(in_driver);
296 CFQ_CRQ_FNS(is_sync);
297 CFQ_CRQ_FNS(requeued);
300 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
301 static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
302 static void cfq_put_cfqd(struct cfq_data *cfqd);
304 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
307 * lots of deadline iosched dupes, can be abstracted later...
309 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
311 hlist_del_init(&crq->hash);
314 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
316 cfq_del_crq_hash(crq);
318 if (q->last_merge == crq->request)
319 q->last_merge = NULL;
322 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
324 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
326 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
329 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
331 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
332 struct hlist_node *entry, *next;
334 hlist_for_each_safe(entry, next, hash_list) {
335 struct cfq_rq *crq = list_entry_hash(entry);
336 struct request *__rq = crq->request;
338 if (!rq_mergeable(__rq)) {
339 cfq_del_crq_hash(crq);
343 if (rq_hash_key(__rq) == offset)
350 static inline int cfq_pending_requests(struct cfq_data *cfqd)
352 return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
356 * scheduler run of queue, if there are requests pending and no one in the
357 * driver that will restart queueing
359 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
361 if (!cfqd->rq_in_driver && cfq_pending_requests(cfqd))
362 kblockd_schedule_work(&cfqd->unplug_work);
365 static int cfq_queue_empty(request_queue_t *q)
367 struct cfq_data *cfqd = q->elevator->elevator_data;
369 return !cfq_pending_requests(cfqd);
373 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
374 * We choose the request that is closest to the head right now. Distance
375 * behind the head are penalized and only allowed to a certain extent.
377 static struct cfq_rq *
378 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
380 sector_t last, s1, s2, d1 = 0, d2 = 0;
381 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
382 unsigned long back_max;
384 if (crq1 == NULL || crq1 == crq2)
389 if (cfq_crq_requeued(crq1) && !cfq_crq_requeued(crq2))
391 else if (cfq_crq_requeued(crq2) && !cfq_crq_requeued(crq1))
394 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
396 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
399 s1 = crq1->request->sector;
400 s2 = crq2->request->sector;
402 last = cfqd->last_sector;
405 * by definition, 1KiB is 2 sectors
407 back_max = cfqd->cfq_back_max * 2;
410 * Strict one way elevator _except_ in the case where we allow
411 * short backward seeks which are biased as twice the cost of a
412 * similar forward seek.
416 else if (s1 + back_max >= last)
417 d1 = (last - s1) * cfqd->cfq_back_penalty;
423 else if (s2 + back_max >= last)
424 d2 = (last - s2) * cfqd->cfq_back_penalty;
428 /* Found required data */
429 if (!r1_wrap && r2_wrap)
431 else if (!r2_wrap && r1_wrap)
433 else if (r1_wrap && r2_wrap) {
434 /* both behind the head */
441 /* Both requests in front of the head */
455 * would be nice to take fifo expire time into account as well
457 static struct cfq_rq *
458 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
461 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
462 struct rb_node *rbnext, *rbprev;
465 if (ON_RB(&last->rb_node))
466 rbnext = rb_next(&last->rb_node);
468 rbnext = rb_first(&cfqq->sort_list);
469 if (rbnext == &last->rb_node)
473 rbprev = rb_prev(&last->rb_node);
476 crq_prev = rb_entry_crq(rbprev);
478 crq_next = rb_entry_crq(rbnext);
480 return cfq_choose_req(cfqd, crq_next, crq_prev);
483 static void cfq_update_next_crq(struct cfq_rq *crq)
485 struct cfq_queue *cfqq = crq->cfq_queue;
487 if (cfqq->next_crq == crq)
488 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
491 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
493 struct cfq_data *cfqd = cfqq->cfqd;
494 struct list_head *list, *entry;
496 BUG_ON(!cfq_cfqq_on_rr(cfqq));
498 list_del(&cfqq->cfq_list);
500 if (cfq_class_rt(cfqq))
501 list = &cfqd->cur_rr;
502 else if (cfq_class_idle(cfqq))
503 list = &cfqd->idle_rr;
506 * if cfqq has requests in flight, don't allow it to be
507 * found in cfq_set_active_queue before it has finished them.
508 * this is done to increase fairness between a process that
509 * has lots of io pending vs one that only generates one
510 * sporadically or synchronously
512 if (cfq_cfqq_dispatched(cfqq))
513 list = &cfqd->busy_rr;
515 list = &cfqd->rr_list[cfqq->ioprio];
519 * if queue was preempted, just add to front to be fair. busy_rr
522 if (preempted || list == &cfqd->busy_rr) {
523 list_add(&cfqq->cfq_list, list);
528 * sort by when queue was last serviced
531 while ((entry = entry->prev) != list) {
532 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
534 if (!__cfqq->service_last)
536 if (time_before(__cfqq->service_last, cfqq->service_last))
540 list_add(&cfqq->cfq_list, entry);
544 * add to busy list of queues for service, trying to be fair in ordering
545 * the pending list according to last request service
548 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
550 BUG_ON(cfq_cfqq_on_rr(cfqq));
551 cfq_mark_cfqq_on_rr(cfqq);
554 cfq_resort_rr_list(cfqq, requeue);
558 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
560 BUG_ON(!cfq_cfqq_on_rr(cfqq));
561 cfq_clear_cfqq_on_rr(cfqq);
562 list_move(&cfqq->cfq_list, &cfqd->empty_list);
564 BUG_ON(!cfqd->busy_queues);
569 * rb tree support functions
571 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
573 struct cfq_queue *cfqq = crq->cfq_queue;
575 if (ON_RB(&crq->rb_node)) {
576 struct cfq_data *cfqd = cfqq->cfqd;
577 const int sync = cfq_crq_is_sync(crq);
579 BUG_ON(!cfqq->queued[sync]);
580 cfqq->queued[sync]--;
582 cfq_update_next_crq(crq);
584 rb_erase(&crq->rb_node, &cfqq->sort_list);
585 RB_CLEAR_COLOR(&crq->rb_node);
587 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
588 cfq_del_cfqq_rr(cfqd, cfqq);
592 static struct cfq_rq *
593 __cfq_add_crq_rb(struct cfq_rq *crq)
595 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
596 struct rb_node *parent = NULL;
597 struct cfq_rq *__crq;
601 __crq = rb_entry_crq(parent);
603 if (crq->rb_key < __crq->rb_key)
605 else if (crq->rb_key > __crq->rb_key)
611 rb_link_node(&crq->rb_node, parent, p);
615 static void cfq_add_crq_rb(struct cfq_rq *crq)
617 struct cfq_queue *cfqq = crq->cfq_queue;
618 struct cfq_data *cfqd = cfqq->cfqd;
619 struct request *rq = crq->request;
620 struct cfq_rq *__alias;
622 crq->rb_key = rq_rb_key(rq);
623 cfqq->queued[cfq_crq_is_sync(crq)]++;
626 * looks a little odd, but the first insert might return an alias.
627 * if that happens, put the alias on the dispatch list
629 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
630 cfq_dispatch_sort(cfqd->queue, __alias);
632 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
634 if (!cfq_cfqq_on_rr(cfqq))
635 cfq_add_cfqq_rr(cfqd, cfqq, cfq_crq_requeued(crq));
638 * check if this request is a better next-serve candidate
640 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
644 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
646 if (ON_RB(&crq->rb_node)) {
647 rb_erase(&crq->rb_node, &cfqq->sort_list);
648 cfqq->queued[cfq_crq_is_sync(crq)]--;
654 static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
657 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
663 n = cfqq->sort_list.rb_node;
665 struct cfq_rq *crq = rb_entry_crq(n);
667 if (sector < crq->rb_key)
669 else if (sector > crq->rb_key)
679 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
681 struct cfq_data *cfqd = q->elevator->elevator_data;
682 struct cfq_rq *crq = RQ_DATA(rq);
685 struct cfq_queue *cfqq = crq->cfq_queue;
687 if (cfq_crq_in_driver(crq)) {
688 cfq_clear_crq_in_driver(crq);
689 WARN_ON(!cfqd->rq_in_driver);
690 cfqd->rq_in_driver--;
692 if (cfq_crq_in_flight(crq)) {
693 const int sync = cfq_crq_is_sync(crq);
695 cfq_clear_crq_in_flight(crq);
696 WARN_ON(!cfqq->on_dispatch[sync]);
697 cfqq->on_dispatch[sync]--;
699 cfq_mark_crq_requeued(crq);
704 * make sure the service time gets corrected on reissue of this request
706 static void cfq_requeue_request(request_queue_t *q, struct request *rq)
708 cfq_deactivate_request(q, rq);
709 list_add(&rq->queuelist, &q->queue_head);
712 static void cfq_remove_request(request_queue_t *q, struct request *rq)
714 struct cfq_rq *crq = RQ_DATA(rq);
717 list_del_init(&rq->queuelist);
719 cfq_remove_merge_hints(q, crq);
725 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
727 struct cfq_data *cfqd = q->elevator->elevator_data;
728 struct request *__rq;
731 ret = elv_try_last_merge(q, bio);
732 if (ret != ELEVATOR_NO_MERGE) {
733 __rq = q->last_merge;
737 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
738 if (__rq && elv_rq_merge_ok(__rq, bio)) {
739 ret = ELEVATOR_BACK_MERGE;
743 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
744 if (__rq && elv_rq_merge_ok(__rq, bio)) {
745 ret = ELEVATOR_FRONT_MERGE;
749 return ELEVATOR_NO_MERGE;
751 q->last_merge = __rq;
757 static void cfq_merged_request(request_queue_t *q, struct request *req)
759 struct cfq_data *cfqd = q->elevator->elevator_data;
760 struct cfq_rq *crq = RQ_DATA(req);
762 cfq_del_crq_hash(crq);
763 cfq_add_crq_hash(cfqd, crq);
765 if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
766 struct cfq_queue *cfqq = crq->cfq_queue;
768 cfq_update_next_crq(crq);
769 cfq_reposition_crq_rb(cfqq, crq);
776 cfq_merged_requests(request_queue_t *q, struct request *rq,
777 struct request *next)
779 cfq_merged_request(q, rq);
782 * reposition in fifo if next is older than rq
784 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
785 time_before(next->start_time, rq->start_time))
786 list_move(&rq->queuelist, &next->queuelist);
788 cfq_remove_request(q, next);
792 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
796 * stop potential idle class queues waiting service
798 del_timer(&cfqd->idle_class_timer);
800 cfqq->slice_start = jiffies;
802 cfqq->slice_left = 0;
803 cfq_clear_cfqq_must_alloc_slice(cfqq);
804 cfq_clear_cfqq_fifo_expire(cfqq);
805 cfq_clear_cfqq_expired(cfqq);
808 cfqd->active_queue = cfqq;
821 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
830 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
831 if (!list_empty(&cfqd->rr_list[p])) {
840 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
841 cfqd->cur_end_prio = 0;
848 if (unlikely(prio == -1))
851 BUG_ON(prio >= CFQ_PRIO_LISTS);
853 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
855 cfqd->cur_prio = prio + 1;
856 if (cfqd->cur_prio > cfqd->cur_end_prio) {
857 cfqd->cur_end_prio = cfqd->cur_prio;
860 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
862 cfqd->cur_end_prio = 0;
868 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
870 struct cfq_queue *cfqq;
873 * if current queue is expired but not done with its requests yet,
874 * wait for that to happen
876 if ((cfqq = cfqd->active_queue) != NULL) {
877 if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
882 * if current list is non-empty, grab first entry. if it is empty,
883 * get next prio level and grab first entry then if any are spliced
885 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
886 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
889 * if we have idle queues and no rt or be queues had pending
890 * requests, either allow immediate service if the grace period
891 * has passed or arm the idle grace timer
893 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
894 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
896 if (time_after_eq(jiffies, end))
897 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
899 mod_timer(&cfqd->idle_class_timer, end);
902 __cfq_set_active_queue(cfqd, cfqq);
907 * current cfqq expired its slice (or was too idle), select new one
910 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
913 unsigned long now = jiffies;
915 if (cfq_cfqq_wait_request(cfqq))
916 del_timer(&cfqd->idle_slice_timer);
918 if (!preempted && !cfq_cfqq_dispatched(cfqq))
919 cfqq->service_last = now;
921 cfq_clear_cfqq_must_dispatch(cfqq);
922 cfq_clear_cfqq_wait_request(cfqq);
925 * store what was left of this slice, if the queue idled out
928 if (time_after(now, cfqq->slice_end))
929 cfqq->slice_left = now - cfqq->slice_end;
931 cfqq->slice_left = 0;
933 if (cfq_cfqq_on_rr(cfqq))
934 cfq_resort_rr_list(cfqq, preempted);
936 if (cfqq == cfqd->active_queue)
937 cfqd->active_queue = NULL;
939 if (cfqd->active_cic) {
940 put_io_context(cfqd->active_cic->ioc);
941 cfqd->active_cic = NULL;
944 cfqd->dispatch_slice = 0;
947 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
949 struct cfq_queue *cfqq = cfqd->active_queue;
953 * use deferred expiry, if there are requests in progress as
954 * not to disturb the slice of the next queue
956 if (cfq_cfqq_dispatched(cfqq))
957 cfq_mark_cfqq_expired(cfqq);
959 __cfq_slice_expired(cfqd, cfqq, preempted);
963 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
966 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
967 WARN_ON(cfqq != cfqd->active_queue);
970 * idle is disabled, either manually or by past process history
972 if (!cfqd->cfq_slice_idle)
974 if (!cfq_cfqq_idle_window(cfqq))
977 * task has exited, don't wait
979 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
982 cfq_mark_cfqq_must_dispatch(cfqq);
983 cfq_mark_cfqq_wait_request(cfqq);
985 if (!timer_pending(&cfqd->idle_slice_timer)) {
986 unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
988 cfqd->idle_slice_timer.expires = jiffies + slice_left;
989 add_timer(&cfqd->idle_slice_timer);
996 * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
997 * this function sector sorts the selected request to minimize seeks. we start
998 * at cfqd->last_sector, not 0.
1000 static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
1002 struct cfq_data *cfqd = q->elevator->elevator_data;
1003 struct cfq_queue *cfqq = crq->cfq_queue;
1004 struct list_head *head = &q->queue_head, *entry = head;
1005 struct request *__rq;
1008 list_del(&crq->request->queuelist);
1010 last = cfqd->last_sector;
1011 list_for_each_entry_reverse(__rq, head, queuelist) {
1012 struct cfq_rq *__crq = RQ_DATA(__rq);
1014 if (blk_barrier_rq(__rq))
1016 if (!blk_fs_request(__rq))
1018 if (cfq_crq_requeued(__crq))
1021 if (__rq->sector <= crq->request->sector)
1023 if (__rq->sector > last && crq->request->sector < last) {
1024 last = crq->request->sector + crq->request->nr_sectors;
1027 entry = &__rq->queuelist;
1030 cfqd->last_sector = last;
1032 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
1034 cfq_del_crq_rb(crq);
1035 cfq_remove_merge_hints(q, crq);
1037 cfq_mark_crq_in_flight(crq);
1038 cfq_clear_crq_requeued(crq);
1040 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
1041 list_add_tail(&crq->request->queuelist, entry);
1045 * return expired entry, or NULL to just start from scratch in rbtree
1047 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
1049 struct cfq_data *cfqd = cfqq->cfqd;
1053 if (cfq_cfqq_fifo_expire(cfqq))
1056 if (!list_empty(&cfqq->fifo)) {
1057 int fifo = cfq_cfqq_class_sync(cfqq);
1059 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
1061 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
1062 cfq_mark_cfqq_fifo_expire(cfqq);
1071 * Scale schedule slice based on io priority. Use the sync time slice only
1072 * if a queue is marked sync and has sync io queued. A sync queue with async
1073 * io only, should not get full sync slice length.
1076 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1078 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
1080 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1082 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
1086 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1088 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1092 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1094 const int base_rq = cfqd->cfq_slice_async_rq;
1096 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1098 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1102 * get next queue for service
1104 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1106 unsigned long now = jiffies;
1107 struct cfq_queue *cfqq;
1109 cfqq = cfqd->active_queue;
1113 if (cfq_cfqq_expired(cfqq))
1119 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1123 * if queue has requests, dispatch one. if not, check if
1124 * enough slice is left to wait for one
1126 if (!RB_EMPTY(&cfqq->sort_list))
1128 else if (!force && cfq_cfqq_class_sync(cfqq) &&
1129 time_before(now, cfqq->slice_end)) {
1130 if (cfq_arm_slice_timer(cfqd, cfqq))
1135 cfq_slice_expired(cfqd, 0);
1137 cfqq = cfq_set_active_queue(cfqd);
1143 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1148 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1154 * follow expired path, else get first next available
1156 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1157 crq = cfqq->next_crq;
1160 * finally, insert request into driver dispatch list
1162 cfq_dispatch_sort(cfqd->queue, crq);
1164 cfqd->dispatch_slice++;
1167 if (!cfqd->active_cic) {
1168 atomic_inc(&crq->io_context->ioc->refcount);
1169 cfqd->active_cic = crq->io_context;
1172 if (RB_EMPTY(&cfqq->sort_list))
1175 } while (dispatched < max_dispatch);
1178 * if slice end isn't set yet, set it. if at least one request was
1179 * sync, use the sync time slice value
1181 if (!cfqq->slice_end)
1182 cfq_set_prio_slice(cfqd, cfqq);
1185 * expire an async queue immediately if it has used up its slice. idle
1186 * queue always expire after 1 dispatch round.
1188 if ((!cfq_cfqq_sync(cfqq) &&
1189 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1190 cfq_class_idle(cfqq))
1191 cfq_slice_expired(cfqd, 0);
1197 cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1199 struct cfq_data *cfqd = q->elevator->elevator_data;
1200 struct cfq_queue *cfqq;
1202 if (!cfqd->busy_queues)
1205 cfqq = cfq_select_queue(cfqd, force);
1207 cfq_clear_cfqq_must_dispatch(cfqq);
1208 cfq_clear_cfqq_wait_request(cfqq);
1209 del_timer(&cfqd->idle_slice_timer);
1211 if (cfq_class_idle(cfqq))
1214 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1220 static inline void cfq_account_dispatch(struct cfq_rq *crq)
1222 struct cfq_queue *cfqq = crq->cfq_queue;
1223 struct cfq_data *cfqd = cfqq->cfqd;
1225 if (unlikely(!blk_fs_request(crq->request)))
1229 * accounted bit is necessary since some drivers will call
1230 * elv_next_request() many times for the same request (eg ide)
1232 if (cfq_crq_in_driver(crq))
1235 cfq_mark_crq_in_driver(crq);
1236 cfqd->rq_in_driver++;
1240 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1242 struct cfq_data *cfqd = cfqq->cfqd;
1245 if (!cfq_crq_in_driver(crq))
1250 WARN_ON(!cfqd->rq_in_driver);
1251 cfqd->rq_in_driver--;
1253 if (!cfq_class_idle(cfqq))
1254 cfqd->last_end_request = now;
1256 if (!cfq_cfqq_dispatched(cfqq)) {
1257 if (cfq_cfqq_on_rr(cfqq)) {
1258 cfqq->service_last = now;
1259 cfq_resort_rr_list(cfqq, 0);
1261 if (cfq_cfqq_expired(cfqq)) {
1262 __cfq_slice_expired(cfqd, cfqq, 0);
1263 cfq_schedule_dispatch(cfqd);
1267 if (cfq_crq_is_sync(crq))
1268 crq->io_context->last_end_request = now;
1271 static struct request *cfq_next_request(request_queue_t *q)
1273 struct cfq_data *cfqd = q->elevator->elevator_data;
1276 if (!list_empty(&q->queue_head)) {
1279 rq = list_entry_rq(q->queue_head.next);
1283 struct cfq_queue *cfqq = crq->cfq_queue;
1286 * if idle window is disabled, allow queue buildup
1288 if (!cfq_crq_in_driver(crq) &&
1289 !cfq_cfqq_idle_window(cfqq) &&
1290 !blk_barrier_rq(rq) &&
1291 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1294 cfq_remove_merge_hints(q, crq);
1295 cfq_account_dispatch(crq);
1301 if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1308 * task holds one reference to the queue, dropped when task exits. each crq
1309 * in-flight on this queue also holds a reference, dropped when crq is freed.
1311 * queue lock must be held here.
1313 static void cfq_put_queue(struct cfq_queue *cfqq)
1315 struct cfq_data *cfqd = cfqq->cfqd;
1317 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1319 if (!atomic_dec_and_test(&cfqq->ref))
1322 BUG_ON(rb_first(&cfqq->sort_list));
1323 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1324 BUG_ON(cfq_cfqq_on_rr(cfqq));
1326 if (unlikely(cfqd->active_queue == cfqq)) {
1327 __cfq_slice_expired(cfqd, cfqq, 0);
1328 cfq_schedule_dispatch(cfqd);
1331 cfq_put_cfqd(cfqq->cfqd);
1334 * it's on the empty list and still hashed
1336 list_del(&cfqq->cfq_list);
1337 hlist_del(&cfqq->cfq_hash);
1338 kmem_cache_free(cfq_pool, cfqq);
1341 static inline struct cfq_queue *
1342 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1345 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1346 struct hlist_node *entry, *next;
1348 hlist_for_each_safe(entry, next, hash_list) {
1349 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1350 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1352 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1359 static struct cfq_queue *
1360 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1362 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1365 static void cfq_free_io_context(struct cfq_io_context *cic)
1367 struct cfq_io_context *__cic;
1368 struct list_head *entry, *next;
1370 list_for_each_safe(entry, next, &cic->list) {
1371 __cic = list_entry(entry, struct cfq_io_context, list);
1372 kmem_cache_free(cfq_ioc_pool, __cic);
1375 kmem_cache_free(cfq_ioc_pool, cic);
1379 * Called with interrupts disabled
1381 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1383 struct cfq_data *cfqd = cic->cfqq->cfqd;
1384 request_queue_t *q = cfqd->queue;
1386 WARN_ON(!irqs_disabled());
1388 spin_lock(q->queue_lock);
1390 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1391 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1392 cfq_schedule_dispatch(cfqd);
1395 cfq_put_queue(cic->cfqq);
1397 spin_unlock(q->queue_lock);
1401 * Another task may update the task cic list, if it is doing a queue lookup
1402 * on its behalf. cfq_cic_lock excludes such concurrent updates
1404 static void cfq_exit_io_context(struct cfq_io_context *cic)
1406 struct cfq_io_context *__cic;
1407 struct list_head *entry;
1408 unsigned long flags;
1410 local_irq_save(flags);
1413 * put the reference this task is holding to the various queues
1415 list_for_each(entry, &cic->list) {
1416 __cic = list_entry(entry, struct cfq_io_context, list);
1417 cfq_exit_single_io_context(__cic);
1420 cfq_exit_single_io_context(cic);
1421 local_irq_restore(flags);
1424 static struct cfq_io_context *
1425 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1427 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1430 INIT_LIST_HEAD(&cic->list);
1433 cic->last_end_request = jiffies;
1434 cic->ttime_total = 0;
1435 cic->ttime_samples = 0;
1436 cic->ttime_mean = 0;
1437 cic->dtor = cfq_free_io_context;
1438 cic->exit = cfq_exit_io_context;
1444 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1446 struct task_struct *tsk = current;
1449 if (!cfq_cfqq_prio_changed(cfqq))
1452 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1453 switch (ioprio_class) {
1455 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1456 case IOPRIO_CLASS_NONE:
1458 * no prio set, place us in the middle of the BE classes
1460 cfqq->ioprio = task_nice_ioprio(tsk);
1461 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1463 case IOPRIO_CLASS_RT:
1464 cfqq->ioprio = task_ioprio(tsk);
1465 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1467 case IOPRIO_CLASS_BE:
1468 cfqq->ioprio = task_ioprio(tsk);
1469 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1471 case IOPRIO_CLASS_IDLE:
1472 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1474 cfq_clear_cfqq_idle_window(cfqq);
1479 * keep track of original prio settings in case we have to temporarily
1480 * elevate the priority of this queue
1482 cfqq->org_ioprio = cfqq->ioprio;
1483 cfqq->org_ioprio_class = cfqq->ioprio_class;
1485 if (cfq_cfqq_on_rr(cfqq))
1486 cfq_resort_rr_list(cfqq, 0);
1488 cfq_clear_cfqq_prio_changed(cfqq);
1491 static inline void changed_ioprio(struct cfq_queue *cfqq)
1494 struct cfq_data *cfqd = cfqq->cfqd;
1496 spin_lock(cfqd->queue->queue_lock);
1497 cfq_mark_cfqq_prio_changed(cfqq);
1498 cfq_init_prio_data(cfqq);
1499 spin_unlock(cfqd->queue->queue_lock);
1504 * callback from sys_ioprio_set, irqs are disabled
1506 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1508 struct cfq_io_context *cic = ioc->cic;
1510 changed_ioprio(cic->cfqq);
1512 list_for_each_entry(cic, &cic->list, list)
1513 changed_ioprio(cic->cfqq);
1518 static struct cfq_queue *
1519 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1522 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1523 struct cfq_queue *cfqq, *new_cfqq = NULL;
1526 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1532 } else if (gfp_mask & __GFP_WAIT) {
1533 spin_unlock_irq(cfqd->queue->queue_lock);
1534 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1535 spin_lock_irq(cfqd->queue->queue_lock);
1538 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1543 memset(cfqq, 0, sizeof(*cfqq));
1545 INIT_HLIST_NODE(&cfqq->cfq_hash);
1546 INIT_LIST_HEAD(&cfqq->cfq_list);
1547 RB_CLEAR_ROOT(&cfqq->sort_list);
1548 INIT_LIST_HEAD(&cfqq->fifo);
1551 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1552 atomic_set(&cfqq->ref, 0);
1554 atomic_inc(&cfqd->ref);
1555 cfqq->service_last = 0;
1557 * set ->slice_left to allow preemption for a new process
1559 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1560 cfq_mark_cfqq_idle_window(cfqq);
1561 cfq_mark_cfqq_prio_changed(cfqq);
1562 cfq_init_prio_data(cfqq);
1566 kmem_cache_free(cfq_pool, new_cfqq);
1568 atomic_inc(&cfqq->ref);
1570 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1575 * Setup general io context and cfq io context. There can be several cfq
1576 * io contexts per general io context, if this process is doing io to more
1577 * than one device managed by cfq. Note that caller is holding a reference to
1578 * cfqq, so we don't need to worry about it disappearing
1580 static struct cfq_io_context *
1581 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1583 struct io_context *ioc = NULL;
1584 struct cfq_io_context *cic;
1586 might_sleep_if(gfp_mask & __GFP_WAIT);
1588 ioc = get_io_context(gfp_mask);
1592 if ((cic = ioc->cic) == NULL) {
1593 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1599 * manually increment generic io_context usage count, it
1600 * cannot go away since we are already holding one ref to it
1603 ioc->set_ioprio = cfq_ioc_set_ioprio;
1606 atomic_inc(&cfqd->ref);
1608 struct cfq_io_context *__cic;
1611 * the first cic on the list is actually the head itself
1613 if (cic->key == cfqd)
1617 * cic exists, check if we already are there. linear search
1618 * should be ok here, the list will usually not be more than
1619 * 1 or a few entries long
1621 list_for_each_entry(__cic, &cic->list, list) {
1623 * this process is already holding a reference to
1624 * this queue, so no need to get one more
1626 if (__cic->key == cfqd) {
1633 * nope, process doesn't have a cic assoicated with this
1634 * cfqq yet. get a new one and add to list
1636 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1642 atomic_inc(&cfqd->ref);
1643 list_add(&__cic->list, &cic->list);
1650 put_io_context(ioc);
1655 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1657 unsigned long elapsed, ttime;
1660 * if this context already has stuff queued, thinktime is from
1661 * last queue not last end
1664 if (time_after(cic->last_end_request, cic->last_queue))
1665 elapsed = jiffies - cic->last_end_request;
1667 elapsed = jiffies - cic->last_queue;
1669 elapsed = jiffies - cic->last_end_request;
1672 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1674 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1675 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1676 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1679 #define sample_valid(samples) ((samples) > 80)
1682 * Disable idle window if the process thinks too long or seeks so much that
1686 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1687 struct cfq_io_context *cic)
1689 int enable_idle = cfq_cfqq_idle_window(cfqq);
1691 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1693 else if (sample_valid(cic->ttime_samples)) {
1694 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1701 cfq_mark_cfqq_idle_window(cfqq);
1703 cfq_clear_cfqq_idle_window(cfqq);
1708 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1709 * no or if we aren't sure, a 1 will cause a preempt.
1712 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1715 struct cfq_queue *cfqq = cfqd->active_queue;
1717 if (cfq_class_idle(new_cfqq))
1723 if (cfq_class_idle(cfqq))
1725 if (!cfq_cfqq_wait_request(new_cfqq))
1728 * if it doesn't have slice left, forget it
1730 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1732 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1739 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1740 * let it have half of its nominal slice.
1742 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1744 struct cfq_queue *__cfqq, *next;
1746 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1747 cfq_resort_rr_list(__cfqq, 1);
1749 if (!cfqq->slice_left)
1750 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1752 cfqq->slice_end = cfqq->slice_left + jiffies;
1753 __cfq_slice_expired(cfqd, cfqq, 1);
1754 __cfq_set_active_queue(cfqd, cfqq);
1758 * should really be a ll_rw_blk.c helper
1760 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1762 request_queue_t *q = cfqd->queue;
1764 if (!blk_queue_plugged(q))
1767 __generic_unplug_device(q);
1771 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1772 * something we should do about it
1775 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1778 struct cfq_io_context *cic;
1780 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1783 * we never wait for an async request and we don't allow preemption
1784 * of an async request. so just return early
1786 if (!cfq_crq_is_sync(crq))
1789 cic = crq->io_context;
1791 cfq_update_io_thinktime(cfqd, cic);
1792 cfq_update_idle_window(cfqd, cfqq, cic);
1794 cic->last_queue = jiffies;
1796 if (cfqq == cfqd->active_queue) {
1798 * if we are waiting for a request for this queue, let it rip
1799 * immediately and flag that we must not expire this queue
1802 if (cfq_cfqq_wait_request(cfqq)) {
1803 cfq_mark_cfqq_must_dispatch(cfqq);
1804 del_timer(&cfqd->idle_slice_timer);
1805 cfq_start_queueing(cfqd, cfqq);
1807 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1809 * not the active queue - expire current slice if it is
1810 * idle and has expired it's mean thinktime or this new queue
1811 * has some old slice time left and is of higher priority
1813 cfq_preempt_queue(cfqd, cfqq);
1814 cfq_mark_cfqq_must_dispatch(cfqq);
1815 cfq_start_queueing(cfqd, cfqq);
1819 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1821 struct cfq_rq *crq = RQ_DATA(rq);
1822 struct cfq_queue *cfqq = crq->cfq_queue;
1824 cfq_init_prio_data(cfqq);
1826 cfq_add_crq_rb(crq);
1828 list_add_tail(&rq->queuelist, &cfqq->fifo);
1830 if (rq_mergeable(rq)) {
1831 cfq_add_crq_hash(cfqd, crq);
1833 if (!cfqd->queue->last_merge)
1834 cfqd->queue->last_merge = rq;
1837 cfq_crq_enqueued(cfqd, cfqq, crq);
1841 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1843 struct cfq_data *cfqd = q->elevator->elevator_data;
1846 case ELEVATOR_INSERT_BACK:
1847 while (cfq_dispatch_requests(q, INT_MAX, 1))
1849 list_add_tail(&rq->queuelist, &q->queue_head);
1851 * If we were idling with pending requests on
1852 * inactive cfqqs, force dispatching will
1853 * remove the idle timer and the queue won't
1854 * be kicked by __make_request() afterward.
1857 cfq_schedule_dispatch(cfqd);
1859 case ELEVATOR_INSERT_FRONT:
1860 list_add(&rq->queuelist, &q->queue_head);
1862 case ELEVATOR_INSERT_SORT:
1863 BUG_ON(!blk_fs_request(rq));
1864 cfq_enqueue(cfqd, rq);
1867 printk("%s: bad insert point %d\n", __FUNCTION__,where);
1872 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1874 struct cfq_rq *crq = RQ_DATA(rq);
1875 struct cfq_queue *cfqq;
1877 if (unlikely(!blk_fs_request(rq)))
1880 cfqq = crq->cfq_queue;
1882 if (cfq_crq_in_flight(crq)) {
1883 const int sync = cfq_crq_is_sync(crq);
1885 WARN_ON(!cfqq->on_dispatch[sync]);
1886 cfqq->on_dispatch[sync]--;
1889 cfq_account_completion(cfqq, crq);
1892 static struct request *
1893 cfq_former_request(request_queue_t *q, struct request *rq)
1895 struct cfq_rq *crq = RQ_DATA(rq);
1896 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1899 return rb_entry_crq(rbprev)->request;
1904 static struct request *
1905 cfq_latter_request(request_queue_t *q, struct request *rq)
1907 struct cfq_rq *crq = RQ_DATA(rq);
1908 struct rb_node *rbnext = rb_next(&crq->rb_node);
1911 return rb_entry_crq(rbnext)->request;
1917 * we temporarily boost lower priority queues if they are holding fs exclusive
1918 * resources. they are boosted to normal prio (CLASS_BE/4)
1920 static void cfq_prio_boost(struct cfq_queue *cfqq)
1922 const int ioprio_class = cfqq->ioprio_class;
1923 const int ioprio = cfqq->ioprio;
1925 if (has_fs_excl()) {
1927 * boost idle prio on transactions that would lock out other
1928 * users of the filesystem
1930 if (cfq_class_idle(cfqq))
1931 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1932 if (cfqq->ioprio > IOPRIO_NORM)
1933 cfqq->ioprio = IOPRIO_NORM;
1936 * check if we need to unboost the queue
1938 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1939 cfqq->ioprio_class = cfqq->org_ioprio_class;
1940 if (cfqq->ioprio != cfqq->org_ioprio)
1941 cfqq->ioprio = cfqq->org_ioprio;
1945 * refile between round-robin lists if we moved the priority class
1947 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1948 cfq_cfqq_on_rr(cfqq))
1949 cfq_resort_rr_list(cfqq, 0);
1952 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1954 if (rw == READ || process_sync(task))
1957 return CFQ_KEY_ASYNC;
1961 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1962 struct task_struct *task, int rw)
1965 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1966 !cfq_cfqq_must_alloc_slice(cfqq)) {
1967 cfq_mark_cfqq_must_alloc_slice(cfqq);
1968 return ELV_MQUEUE_MUST;
1971 return ELV_MQUEUE_MAY;
1973 if (!cfqq || task->flags & PF_MEMALLOC)
1974 return ELV_MQUEUE_MAY;
1975 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1976 if (cfq_cfqq_wait_request(cfqq))
1977 return ELV_MQUEUE_MUST;
1980 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1981 * can quickly flood the queue with writes from a single task
1983 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
1984 cfq_mark_cfqq_must_alloc_slice(cfqq);
1985 return ELV_MQUEUE_MUST;
1988 return ELV_MQUEUE_MAY;
1990 if (cfq_class_idle(cfqq))
1991 return ELV_MQUEUE_NO;
1992 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1993 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1994 int ret = ELV_MQUEUE_NO;
1996 if (ioc && ioc->nr_batch_requests)
1997 ret = ELV_MQUEUE_MAY;
1999 put_io_context(ioc);
2003 return ELV_MQUEUE_MAY;
2007 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
2009 struct cfq_data *cfqd = q->elevator->elevator_data;
2010 struct task_struct *tsk = current;
2011 struct cfq_queue *cfqq;
2014 * don't force setup of a queue from here, as a call to may_queue
2015 * does not necessarily imply that a request actually will be queued.
2016 * so just lookup a possibly existing queue, or return 'may queue'
2019 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
2021 cfq_init_prio_data(cfqq);
2022 cfq_prio_boost(cfqq);
2024 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
2027 return ELV_MQUEUE_MAY;
2030 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
2032 struct cfq_data *cfqd = q->elevator->elevator_data;
2033 struct request_list *rl = &q->rq;
2035 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
2037 if (waitqueue_active(&rl->wait[READ]))
2038 wake_up(&rl->wait[READ]);
2041 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
2043 if (waitqueue_active(&rl->wait[WRITE]))
2044 wake_up(&rl->wait[WRITE]);
2049 * queue lock held here
2051 static void cfq_put_request(request_queue_t *q, struct request *rq)
2053 struct cfq_data *cfqd = q->elevator->elevator_data;
2054 struct cfq_rq *crq = RQ_DATA(rq);
2057 struct cfq_queue *cfqq = crq->cfq_queue;
2058 const int rw = rq_data_dir(rq);
2060 BUG_ON(!cfqq->allocated[rw]);
2061 cfqq->allocated[rw]--;
2063 put_io_context(crq->io_context->ioc);
2065 mempool_free(crq, cfqd->crq_pool);
2066 rq->elevator_private = NULL;
2068 cfq_check_waiters(q, cfqq);
2069 cfq_put_queue(cfqq);
2074 * Allocate cfq data structures associated with this request.
2077 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
2080 struct cfq_data *cfqd = q->elevator->elevator_data;
2081 struct task_struct *tsk = current;
2082 struct cfq_io_context *cic;
2083 const int rw = rq_data_dir(rq);
2084 pid_t key = cfq_queue_pid(tsk, rw);
2085 struct cfq_queue *cfqq;
2087 unsigned long flags;
2089 might_sleep_if(gfp_mask & __GFP_WAIT);
2091 cic = cfq_get_io_context(cfqd, key, gfp_mask);
2093 spin_lock_irqsave(q->queue_lock, flags);
2099 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
2107 cfqq->allocated[rw]++;
2108 cfq_clear_cfqq_must_alloc(cfqq);
2109 cfqd->rq_starved = 0;
2110 atomic_inc(&cfqq->ref);
2111 spin_unlock_irqrestore(q->queue_lock, flags);
2113 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2115 RB_CLEAR(&crq->rb_node);
2118 INIT_HLIST_NODE(&crq->hash);
2119 crq->cfq_queue = cfqq;
2120 crq->io_context = cic;
2121 cfq_clear_crq_in_flight(crq);
2122 cfq_clear_crq_in_driver(crq);
2123 cfq_clear_crq_requeued(crq);
2125 if (rw == READ || process_sync(tsk))
2126 cfq_mark_crq_is_sync(crq);
2128 cfq_clear_crq_is_sync(crq);
2130 rq->elevator_private = crq;
2134 spin_lock_irqsave(q->queue_lock, flags);
2135 cfqq->allocated[rw]--;
2136 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
2137 cfq_mark_cfqq_must_alloc(cfqq);
2138 cfq_put_queue(cfqq);
2141 put_io_context(cic->ioc);
2143 * mark us rq allocation starved. we need to kickstart the process
2144 * ourselves if there are no pending requests that can do it for us.
2145 * that would be an extremely rare OOM situation
2147 cfqd->rq_starved = 1;
2148 cfq_schedule_dispatch(cfqd);
2149 spin_unlock_irqrestore(q->queue_lock, flags);
2153 static void cfq_kick_queue(void *data)
2155 request_queue_t *q = data;
2156 struct cfq_data *cfqd = q->elevator->elevator_data;
2157 unsigned long flags;
2159 spin_lock_irqsave(q->queue_lock, flags);
2161 if (cfqd->rq_starved) {
2162 struct request_list *rl = &q->rq;
2165 * we aren't guaranteed to get a request after this, but we
2166 * have to be opportunistic
2169 if (waitqueue_active(&rl->wait[READ]))
2170 wake_up(&rl->wait[READ]);
2171 if (waitqueue_active(&rl->wait[WRITE]))
2172 wake_up(&rl->wait[WRITE]);
2177 spin_unlock_irqrestore(q->queue_lock, flags);
2181 * Timer running if the active_queue is currently idling inside its time slice
2183 static void cfq_idle_slice_timer(unsigned long data)
2185 struct cfq_data *cfqd = (struct cfq_data *) data;
2186 struct cfq_queue *cfqq;
2187 unsigned long flags;
2189 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2191 if ((cfqq = cfqd->active_queue) != NULL) {
2192 unsigned long now = jiffies;
2197 if (time_after(now, cfqq->slice_end))
2201 * only expire and reinvoke request handler, if there are
2202 * other queues with pending requests
2204 if (!cfq_pending_requests(cfqd)) {
2205 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2206 add_timer(&cfqd->idle_slice_timer);
2211 * not expired and it has a request pending, let it dispatch
2213 if (!RB_EMPTY(&cfqq->sort_list)) {
2214 cfq_mark_cfqq_must_dispatch(cfqq);
2219 cfq_slice_expired(cfqd, 0);
2221 cfq_schedule_dispatch(cfqd);
2223 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2227 * Timer running if an idle class queue is waiting for service
2229 static void cfq_idle_class_timer(unsigned long data)
2231 struct cfq_data *cfqd = (struct cfq_data *) data;
2232 unsigned long flags, end;
2234 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2237 * race with a non-idle queue, reset timer
2239 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2240 if (!time_after_eq(jiffies, end)) {
2241 cfqd->idle_class_timer.expires = end;
2242 add_timer(&cfqd->idle_class_timer);
2244 cfq_schedule_dispatch(cfqd);
2246 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2249 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2251 del_timer_sync(&cfqd->idle_slice_timer);
2252 del_timer_sync(&cfqd->idle_class_timer);
2253 blk_sync_queue(cfqd->queue);
2256 static void cfq_put_cfqd(struct cfq_data *cfqd)
2258 request_queue_t *q = cfqd->queue;
2260 if (!atomic_dec_and_test(&cfqd->ref))
2263 cfq_shutdown_timer_wq(cfqd);
2264 q->elevator->elevator_data = NULL;
2266 mempool_destroy(cfqd->crq_pool);
2267 kfree(cfqd->crq_hash);
2268 kfree(cfqd->cfq_hash);
2272 static void cfq_exit_queue(elevator_t *e)
2274 struct cfq_data *cfqd = e->elevator_data;
2276 cfq_shutdown_timer_wq(cfqd);
2280 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2282 struct cfq_data *cfqd;
2285 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2289 memset(cfqd, 0, sizeof(*cfqd));
2291 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2292 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2294 INIT_LIST_HEAD(&cfqd->busy_rr);
2295 INIT_LIST_HEAD(&cfqd->cur_rr);
2296 INIT_LIST_HEAD(&cfqd->idle_rr);
2297 INIT_LIST_HEAD(&cfqd->empty_list);
2299 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2300 if (!cfqd->crq_hash)
2303 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2304 if (!cfqd->cfq_hash)
2307 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2308 if (!cfqd->crq_pool)
2311 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2312 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2313 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2314 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2316 e->elevator_data = cfqd;
2320 cfqd->max_queued = q->nr_requests / 4;
2321 q->nr_batching = cfq_queued;
2323 init_timer(&cfqd->idle_slice_timer);
2324 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2325 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2327 init_timer(&cfqd->idle_class_timer);
2328 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2329 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2331 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2333 atomic_set(&cfqd->ref, 1);
2335 cfqd->cfq_queued = cfq_queued;
2336 cfqd->cfq_quantum = cfq_quantum;
2337 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2338 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2339 cfqd->cfq_back_max = cfq_back_max;
2340 cfqd->cfq_back_penalty = cfq_back_penalty;
2341 cfqd->cfq_slice[0] = cfq_slice_async;
2342 cfqd->cfq_slice[1] = cfq_slice_sync;
2343 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2344 cfqd->cfq_slice_idle = cfq_slice_idle;
2345 cfqd->cfq_max_depth = cfq_max_depth;
2349 kfree(cfqd->cfq_hash);
2351 kfree(cfqd->crq_hash);
2357 static void cfq_slab_kill(void)
2360 kmem_cache_destroy(crq_pool);
2362 kmem_cache_destroy(cfq_pool);
2364 kmem_cache_destroy(cfq_ioc_pool);
2367 static int __init cfq_slab_setup(void)
2369 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2374 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2379 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2380 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2391 * sysfs parts below -->
2393 struct cfq_fs_entry {
2394 struct attribute attr;
2395 ssize_t (*show)(struct cfq_data *, char *);
2396 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2400 cfq_var_show(unsigned int var, char *page)
2402 return sprintf(page, "%d\n", var);
2406 cfq_var_store(unsigned int *var, const char *page, size_t count)
2408 char *p = (char *) page;
2410 *var = simple_strtoul(p, &p, 10);
2414 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2415 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2417 unsigned int __data = __VAR; \
2419 __data = jiffies_to_msecs(__data); \
2420 return cfq_var_show(__data, (page)); \
2422 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2423 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2424 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2425 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2426 SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2427 SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
2428 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2429 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2430 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2431 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2432 SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
2433 #undef SHOW_FUNCTION
2435 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2436 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2438 unsigned int __data; \
2439 int ret = cfq_var_store(&__data, (page), count); \
2440 if (__data < (MIN)) \
2442 else if (__data > (MAX)) \
2445 *(__PTR) = msecs_to_jiffies(__data); \
2447 *(__PTR) = __data; \
2450 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2451 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2452 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2453 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2454 STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2455 STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2456 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2457 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2458 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2459 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2460 STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
2461 #undef STORE_FUNCTION
2463 static struct cfq_fs_entry cfq_quantum_entry = {
2464 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2465 .show = cfq_quantum_show,
2466 .store = cfq_quantum_store,
2468 static struct cfq_fs_entry cfq_queued_entry = {
2469 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2470 .show = cfq_queued_show,
2471 .store = cfq_queued_store,
2473 static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
2474 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
2475 .show = cfq_fifo_expire_sync_show,
2476 .store = cfq_fifo_expire_sync_store,
2478 static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
2479 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
2480 .show = cfq_fifo_expire_async_show,
2481 .store = cfq_fifo_expire_async_store,
2483 static struct cfq_fs_entry cfq_back_max_entry = {
2484 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2485 .show = cfq_back_max_show,
2486 .store = cfq_back_max_store,
2488 static struct cfq_fs_entry cfq_back_penalty_entry = {
2489 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2490 .show = cfq_back_penalty_show,
2491 .store = cfq_back_penalty_store,
2493 static struct cfq_fs_entry cfq_slice_sync_entry = {
2494 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2495 .show = cfq_slice_sync_show,
2496 .store = cfq_slice_sync_store,
2498 static struct cfq_fs_entry cfq_slice_async_entry = {
2499 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2500 .show = cfq_slice_async_show,
2501 .store = cfq_slice_async_store,
2503 static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2504 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2505 .show = cfq_slice_async_rq_show,
2506 .store = cfq_slice_async_rq_store,
2508 static struct cfq_fs_entry cfq_slice_idle_entry = {
2509 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2510 .show = cfq_slice_idle_show,
2511 .store = cfq_slice_idle_store,
2513 static struct cfq_fs_entry cfq_max_depth_entry = {
2514 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2515 .show = cfq_max_depth_show,
2516 .store = cfq_max_depth_store,
2519 static struct attribute *default_attrs[] = {
2520 &cfq_quantum_entry.attr,
2521 &cfq_queued_entry.attr,
2522 &cfq_fifo_expire_sync_entry.attr,
2523 &cfq_fifo_expire_async_entry.attr,
2524 &cfq_back_max_entry.attr,
2525 &cfq_back_penalty_entry.attr,
2526 &cfq_slice_sync_entry.attr,
2527 &cfq_slice_async_entry.attr,
2528 &cfq_slice_async_rq_entry.attr,
2529 &cfq_slice_idle_entry.attr,
2530 &cfq_max_depth_entry.attr,
2534 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2537 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2539 elevator_t *e = container_of(kobj, elevator_t, kobj);
2540 struct cfq_fs_entry *entry = to_cfq(attr);
2545 return entry->show(e->elevator_data, page);
2549 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2550 const char *page, size_t length)
2552 elevator_t *e = container_of(kobj, elevator_t, kobj);
2553 struct cfq_fs_entry *entry = to_cfq(attr);
2558 return entry->store(e->elevator_data, page, length);
2561 static struct sysfs_ops cfq_sysfs_ops = {
2562 .show = cfq_attr_show,
2563 .store = cfq_attr_store,
2566 static struct kobj_type cfq_ktype = {
2567 .sysfs_ops = &cfq_sysfs_ops,
2568 .default_attrs = default_attrs,
2571 static struct elevator_type iosched_cfq = {
2573 .elevator_merge_fn = cfq_merge,
2574 .elevator_merged_fn = cfq_merged_request,
2575 .elevator_merge_req_fn = cfq_merged_requests,
2576 .elevator_next_req_fn = cfq_next_request,
2577 .elevator_add_req_fn = cfq_insert_request,
2578 .elevator_remove_req_fn = cfq_remove_request,
2579 .elevator_requeue_req_fn = cfq_requeue_request,
2580 .elevator_deactivate_req_fn = cfq_deactivate_request,
2581 .elevator_queue_empty_fn = cfq_queue_empty,
2582 .elevator_completed_req_fn = cfq_completed_request,
2583 .elevator_former_req_fn = cfq_former_request,
2584 .elevator_latter_req_fn = cfq_latter_request,
2585 .elevator_set_req_fn = cfq_set_request,
2586 .elevator_put_req_fn = cfq_put_request,
2587 .elevator_may_queue_fn = cfq_may_queue,
2588 .elevator_init_fn = cfq_init_queue,
2589 .elevator_exit_fn = cfq_exit_queue,
2591 .elevator_ktype = &cfq_ktype,
2592 .elevator_name = "cfq",
2593 .elevator_owner = THIS_MODULE,
2596 static int __init cfq_init(void)
2601 * could be 0 on HZ < 1000 setups
2603 if (!cfq_slice_async)
2604 cfq_slice_async = 1;
2605 if (!cfq_slice_idle)
2608 if (cfq_slab_setup())
2611 ret = elv_register(&iosched_cfq);
2618 static void __exit cfq_exit(void)
2620 struct task_struct *g, *p;
2621 unsigned long flags;
2623 read_lock_irqsave(&tasklist_lock, flags);
2626 * iterate each process in the system, removing our io_context
2628 do_each_thread(g, p) {
2629 struct io_context *ioc = p->io_context;
2631 if (ioc && ioc->cic) {
2632 ioc->cic->exit(ioc->cic);
2633 cfq_free_io_context(ioc->cic);
2636 } while_each_thread(g, p);
2638 read_unlock_irqrestore(&tasklist_lock, flags);
2641 elv_unregister(&iosched_cfq);
2644 module_init(cfq_init);
2645 module_exit(cfq_exit);
2647 MODULE_AUTHOR("Jens Axboe");
2648 MODULE_LICENSE("GPL");
2649 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");