2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/blkdev.h>
12 #include <linux/elevator.h>
13 #include <linux/hash.h>
14 #include <linux/rbtree.h>
15 #include <linux/ioprio.h>
20 static const int cfq_quantum = 4; /* max queue in one round of service */
21 static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
22 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
23 static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
24 static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
26 static const int cfq_slice_sync = HZ / 10;
27 static int cfq_slice_async = HZ / 25;
28 static const int cfq_slice_async_rq = 2;
29 static int cfq_slice_idle = HZ / 70;
31 #define CFQ_IDLE_GRACE (HZ / 10)
32 #define CFQ_SLICE_SCALE (5)
34 #define CFQ_KEY_ASYNC (0)
36 static DEFINE_SPINLOCK(cfq_exit_lock);
39 * for the hash of cfqq inside the cfqd
41 #define CFQ_QHASH_SHIFT 6
42 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
43 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
46 * for the hash of crq inside the cfqq
48 #define CFQ_MHASH_SHIFT 6
49 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
50 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
51 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
52 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
53 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
55 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
56 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
58 #define RQ_DATA(rq) (rq)->elevator_private
64 #define RB_EMPTY(node) ((node)->rb_node == NULL)
65 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
66 #define RB_CLEAR(node) do { \
67 (node)->rb_parent = NULL; \
68 RB_CLEAR_COLOR((node)); \
69 (node)->rb_right = NULL; \
70 (node)->rb_left = NULL; \
72 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
73 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
74 #define rq_rb_key(rq) (rq)->sector
76 static kmem_cache_t *crq_pool;
77 static kmem_cache_t *cfq_pool;
78 static kmem_cache_t *cfq_ioc_pool;
80 static atomic_t ioc_count = ATOMIC_INIT(0);
81 static struct completion *ioc_gone;
83 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
84 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
85 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
86 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
91 #define cfq_cfqq_dispatched(cfqq) \
92 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
94 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
96 #define cfq_cfqq_sync(cfqq) \
97 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
99 #define sample_valid(samples) ((samples) > 80)
102 * Per block device queue structure
105 request_queue_t *queue;
108 * rr list of queues with requests and the count of them
110 struct list_head rr_list[CFQ_PRIO_LISTS];
111 struct list_head busy_rr;
112 struct list_head cur_rr;
113 struct list_head idle_rr;
114 unsigned int busy_queues;
117 * non-ordered list of empty cfqq's
119 struct list_head empty_list;
124 struct hlist_head *cfq_hash;
127 * global crq hash for all queues
129 struct hlist_head *crq_hash;
131 unsigned int max_queued;
138 * schedule slice state info
141 * idle window management
143 struct timer_list idle_slice_timer;
144 struct work_struct unplug_work;
146 struct cfq_queue *active_queue;
147 struct cfq_io_context *active_cic;
148 int cur_prio, cur_end_prio;
149 unsigned int dispatch_slice;
151 struct timer_list idle_class_timer;
153 sector_t last_sector;
154 unsigned long last_end_request;
156 unsigned int rq_starved;
159 * tunables, see top of file
161 unsigned int cfq_quantum;
162 unsigned int cfq_queued;
163 unsigned int cfq_fifo_expire[2];
164 unsigned int cfq_back_penalty;
165 unsigned int cfq_back_max;
166 unsigned int cfq_slice[2];
167 unsigned int cfq_slice_async_rq;
168 unsigned int cfq_slice_idle;
170 struct list_head cic_list;
174 * Per process-grouping structure
177 /* reference count */
179 /* parent cfq_data */
180 struct cfq_data *cfqd;
181 /* cfqq lookup hash */
182 struct hlist_node cfq_hash;
185 /* on either rr or empty list of cfqd */
186 struct list_head cfq_list;
187 /* sorted list of pending requests */
188 struct rb_root sort_list;
189 /* if fifo isn't expired, next request to serve */
190 struct cfq_rq *next_crq;
191 /* requests queued in sort_list */
193 /* currently allocated requests */
195 /* fifo list of requests in sort_list */
196 struct list_head fifo;
198 unsigned long slice_start;
199 unsigned long slice_end;
200 unsigned long slice_left;
201 unsigned long service_last;
203 /* number of requests that are on the dispatch list */
206 /* io prio of this group */
207 unsigned short ioprio, org_ioprio;
208 unsigned short ioprio_class, org_ioprio_class;
210 /* various state flags, see below */
215 struct rb_node rb_node;
217 struct request *request;
218 struct hlist_node hash;
220 struct cfq_queue *cfq_queue;
221 struct cfq_io_context *io_context;
223 unsigned int crq_flags;
226 enum cfqq_state_flags {
227 CFQ_CFQQ_FLAG_on_rr = 0,
228 CFQ_CFQQ_FLAG_wait_request,
229 CFQ_CFQQ_FLAG_must_alloc,
230 CFQ_CFQQ_FLAG_must_alloc_slice,
231 CFQ_CFQQ_FLAG_must_dispatch,
232 CFQ_CFQQ_FLAG_fifo_expire,
233 CFQ_CFQQ_FLAG_idle_window,
234 CFQ_CFQQ_FLAG_prio_changed,
237 #define CFQ_CFQQ_FNS(name) \
238 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
240 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
242 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
244 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
246 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
248 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
252 CFQ_CFQQ_FNS(wait_request);
253 CFQ_CFQQ_FNS(must_alloc);
254 CFQ_CFQQ_FNS(must_alloc_slice);
255 CFQ_CFQQ_FNS(must_dispatch);
256 CFQ_CFQQ_FNS(fifo_expire);
257 CFQ_CFQQ_FNS(idle_window);
258 CFQ_CFQQ_FNS(prio_changed);
261 enum cfq_rq_state_flags {
262 CFQ_CRQ_FLAG_is_sync = 0,
265 #define CFQ_CRQ_FNS(name) \
266 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
268 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
270 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
272 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
274 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
276 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
279 CFQ_CRQ_FNS(is_sync);
282 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
283 static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
284 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
286 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
289 * lots of deadline iosched dupes, can be abstracted later...
291 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
293 hlist_del_init(&crq->hash);
296 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
298 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
300 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
303 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
305 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
306 struct hlist_node *entry, *next;
308 hlist_for_each_safe(entry, next, hash_list) {
309 struct cfq_rq *crq = list_entry_hash(entry);
310 struct request *__rq = crq->request;
312 if (!rq_mergeable(__rq)) {
313 cfq_del_crq_hash(crq);
317 if (rq_hash_key(__rq) == offset)
325 * scheduler run of queue, if there are requests pending and no one in the
326 * driver that will restart queueing
328 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
330 if (cfqd->busy_queues)
331 kblockd_schedule_work(&cfqd->unplug_work);
334 static int cfq_queue_empty(request_queue_t *q)
336 struct cfq_data *cfqd = q->elevator->elevator_data;
338 return !cfqd->busy_queues;
341 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
343 if (rw == READ || process_sync(task))
346 return CFQ_KEY_ASYNC;
350 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
351 * We choose the request that is closest to the head right now. Distance
352 * behind the head is penalized and only allowed to a certain extent.
354 static struct cfq_rq *
355 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
357 sector_t last, s1, s2, d1 = 0, d2 = 0;
358 unsigned long back_max;
359 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
360 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
361 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
363 if (crq1 == NULL || crq1 == crq2)
368 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
370 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
373 s1 = crq1->request->sector;
374 s2 = crq2->request->sector;
376 last = cfqd->last_sector;
379 * by definition, 1KiB is 2 sectors
381 back_max = cfqd->cfq_back_max * 2;
384 * Strict one way elevator _except_ in the case where we allow
385 * short backward seeks which are biased as twice the cost of a
386 * similar forward seek.
390 else if (s1 + back_max >= last)
391 d1 = (last - s1) * cfqd->cfq_back_penalty;
393 wrap |= CFQ_RQ1_WRAP;
397 else if (s2 + back_max >= last)
398 d2 = (last - s2) * cfqd->cfq_back_penalty;
400 wrap |= CFQ_RQ2_WRAP;
402 /* Found required data */
405 * By doing switch() on the bit mask "wrap" we avoid having to
406 * check two variables for all permutations: --> faster!
409 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
425 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
428 * Since both rqs are wrapped,
429 * start with the one that's further behind head
430 * (--> only *one* back seek required),
431 * since back seek takes more time than forward.
441 * would be nice to take fifo expire time into account as well
443 static struct cfq_rq *
444 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
447 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
448 struct rb_node *rbnext, *rbprev;
450 if (!(rbnext = rb_next(&last->rb_node))) {
451 rbnext = rb_first(&cfqq->sort_list);
452 if (rbnext == &last->rb_node)
456 rbprev = rb_prev(&last->rb_node);
459 crq_prev = rb_entry_crq(rbprev);
461 crq_next = rb_entry_crq(rbnext);
463 return cfq_choose_req(cfqd, crq_next, crq_prev);
466 static void cfq_update_next_crq(struct cfq_rq *crq)
468 struct cfq_queue *cfqq = crq->cfq_queue;
470 if (cfqq->next_crq == crq)
471 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
474 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
476 struct cfq_data *cfqd = cfqq->cfqd;
477 struct list_head *list, *entry;
479 BUG_ON(!cfq_cfqq_on_rr(cfqq));
481 list_del(&cfqq->cfq_list);
483 if (cfq_class_rt(cfqq))
484 list = &cfqd->cur_rr;
485 else if (cfq_class_idle(cfqq))
486 list = &cfqd->idle_rr;
489 * if cfqq has requests in flight, don't allow it to be
490 * found in cfq_set_active_queue before it has finished them.
491 * this is done to increase fairness between a process that
492 * has lots of io pending vs one that only generates one
493 * sporadically or synchronously
495 if (cfq_cfqq_dispatched(cfqq))
496 list = &cfqd->busy_rr;
498 list = &cfqd->rr_list[cfqq->ioprio];
502 * if queue was preempted, just add to front to be fair. busy_rr
505 if (preempted || list == &cfqd->busy_rr) {
506 list_add(&cfqq->cfq_list, list);
511 * sort by when queue was last serviced
514 while ((entry = entry->prev) != list) {
515 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
517 if (!__cfqq->service_last)
519 if (time_before(__cfqq->service_last, cfqq->service_last))
523 list_add(&cfqq->cfq_list, entry);
527 * add to busy list of queues for service, trying to be fair in ordering
528 * the pending list according to last request service
531 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
533 BUG_ON(cfq_cfqq_on_rr(cfqq));
534 cfq_mark_cfqq_on_rr(cfqq);
537 cfq_resort_rr_list(cfqq, 0);
541 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
543 BUG_ON(!cfq_cfqq_on_rr(cfqq));
544 cfq_clear_cfqq_on_rr(cfqq);
545 list_move(&cfqq->cfq_list, &cfqd->empty_list);
547 BUG_ON(!cfqd->busy_queues);
552 * rb tree support functions
554 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
556 struct cfq_queue *cfqq = crq->cfq_queue;
557 struct cfq_data *cfqd = cfqq->cfqd;
558 const int sync = cfq_crq_is_sync(crq);
560 BUG_ON(!cfqq->queued[sync]);
561 cfqq->queued[sync]--;
563 cfq_update_next_crq(crq);
565 rb_erase(&crq->rb_node, &cfqq->sort_list);
566 RB_CLEAR_COLOR(&crq->rb_node);
568 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
569 cfq_del_cfqq_rr(cfqd, cfqq);
572 static struct cfq_rq *
573 __cfq_add_crq_rb(struct cfq_rq *crq)
575 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
576 struct rb_node *parent = NULL;
577 struct cfq_rq *__crq;
581 __crq = rb_entry_crq(parent);
583 if (crq->rb_key < __crq->rb_key)
585 else if (crq->rb_key > __crq->rb_key)
591 rb_link_node(&crq->rb_node, parent, p);
595 static void cfq_add_crq_rb(struct cfq_rq *crq)
597 struct cfq_queue *cfqq = crq->cfq_queue;
598 struct cfq_data *cfqd = cfqq->cfqd;
599 struct request *rq = crq->request;
600 struct cfq_rq *__alias;
602 crq->rb_key = rq_rb_key(rq);
603 cfqq->queued[cfq_crq_is_sync(crq)]++;
606 * looks a little odd, but the first insert might return an alias.
607 * if that happens, put the alias on the dispatch list
609 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
610 cfq_dispatch_insert(cfqd->queue, __alias);
612 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
614 if (!cfq_cfqq_on_rr(cfqq))
615 cfq_add_cfqq_rr(cfqd, cfqq);
618 * check if this request is a better next-serve candidate
620 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
624 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
626 rb_erase(&crq->rb_node, &cfqq->sort_list);
627 cfqq->queued[cfq_crq_is_sync(crq)]--;
632 static struct request *
633 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
635 struct task_struct *tsk = current;
636 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
637 struct cfq_queue *cfqq;
641 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
645 sector = bio->bi_sector + bio_sectors(bio);
646 n = cfqq->sort_list.rb_node;
648 struct cfq_rq *crq = rb_entry_crq(n);
650 if (sector < crq->rb_key)
652 else if (sector > crq->rb_key)
662 static void cfq_activate_request(request_queue_t *q, struct request *rq)
664 struct cfq_data *cfqd = q->elevator->elevator_data;
666 cfqd->rq_in_driver++;
669 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
671 struct cfq_data *cfqd = q->elevator->elevator_data;
673 WARN_ON(!cfqd->rq_in_driver);
674 cfqd->rq_in_driver--;
677 static void cfq_remove_request(struct request *rq)
679 struct cfq_rq *crq = RQ_DATA(rq);
681 list_del_init(&rq->queuelist);
683 cfq_del_crq_hash(crq);
687 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
689 struct cfq_data *cfqd = q->elevator->elevator_data;
690 struct request *__rq;
693 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
694 if (__rq && elv_rq_merge_ok(__rq, bio)) {
695 ret = ELEVATOR_BACK_MERGE;
699 __rq = cfq_find_rq_fmerge(cfqd, bio);
700 if (__rq && elv_rq_merge_ok(__rq, bio)) {
701 ret = ELEVATOR_FRONT_MERGE;
705 return ELEVATOR_NO_MERGE;
711 static void cfq_merged_request(request_queue_t *q, struct request *req)
713 struct cfq_data *cfqd = q->elevator->elevator_data;
714 struct cfq_rq *crq = RQ_DATA(req);
716 cfq_del_crq_hash(crq);
717 cfq_add_crq_hash(cfqd, crq);
719 if (rq_rb_key(req) != crq->rb_key) {
720 struct cfq_queue *cfqq = crq->cfq_queue;
722 cfq_update_next_crq(crq);
723 cfq_reposition_crq_rb(cfqq, crq);
728 cfq_merged_requests(request_queue_t *q, struct request *rq,
729 struct request *next)
731 cfq_merged_request(q, rq);
734 * reposition in fifo if next is older than rq
736 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
737 time_before(next->start_time, rq->start_time))
738 list_move(&rq->queuelist, &next->queuelist);
740 cfq_remove_request(next);
744 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
748 * stop potential idle class queues waiting service
750 del_timer(&cfqd->idle_class_timer);
752 cfqq->slice_start = jiffies;
754 cfqq->slice_left = 0;
755 cfq_clear_cfqq_must_alloc_slice(cfqq);
756 cfq_clear_cfqq_fifo_expire(cfqq);
759 cfqd->active_queue = cfqq;
763 * current cfqq expired its slice (or was too idle), select new one
766 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
769 unsigned long now = jiffies;
771 if (cfq_cfqq_wait_request(cfqq))
772 del_timer(&cfqd->idle_slice_timer);
774 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
775 cfqq->service_last = now;
776 cfq_schedule_dispatch(cfqd);
779 cfq_clear_cfqq_must_dispatch(cfqq);
780 cfq_clear_cfqq_wait_request(cfqq);
783 * store what was left of this slice, if the queue idled out
786 if (time_after(cfqq->slice_end, now))
787 cfqq->slice_left = cfqq->slice_end - now;
789 cfqq->slice_left = 0;
791 if (cfq_cfqq_on_rr(cfqq))
792 cfq_resort_rr_list(cfqq, preempted);
794 if (cfqq == cfqd->active_queue)
795 cfqd->active_queue = NULL;
797 if (cfqd->active_cic) {
798 put_io_context(cfqd->active_cic->ioc);
799 cfqd->active_cic = NULL;
802 cfqd->dispatch_slice = 0;
805 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
807 struct cfq_queue *cfqq = cfqd->active_queue;
810 __cfq_slice_expired(cfqd, cfqq, preempted);
823 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
832 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
833 if (!list_empty(&cfqd->rr_list[p])) {
842 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
843 cfqd->cur_end_prio = 0;
850 if (unlikely(prio == -1))
853 BUG_ON(prio >= CFQ_PRIO_LISTS);
855 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
857 cfqd->cur_prio = prio + 1;
858 if (cfqd->cur_prio > cfqd->cur_end_prio) {
859 cfqd->cur_end_prio = cfqd->cur_prio;
862 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
864 cfqd->cur_end_prio = 0;
870 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
872 struct cfq_queue *cfqq = NULL;
875 * if current list is non-empty, grab first entry. if it is empty,
876 * get next prio level and grab first entry then if any are spliced
878 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
879 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
882 * If no new queues are available, check if the busy list has some
883 * before falling back to idle io.
885 if (!cfqq && !list_empty(&cfqd->busy_rr))
886 cfqq = list_entry_cfqq(cfqd->busy_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);
906 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
909 struct cfq_io_context *cic;
912 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
913 WARN_ON(cfqq != cfqd->active_queue);
916 * idle is disabled, either manually or by past process history
918 if (!cfqd->cfq_slice_idle)
920 if (!cfq_cfqq_idle_window(cfqq))
923 * task has exited, don't wait
925 cic = cfqd->active_cic;
926 if (!cic || !cic->ioc->task)
929 cfq_mark_cfqq_must_dispatch(cfqq);
930 cfq_mark_cfqq_wait_request(cfqq);
932 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
935 * we don't want to idle for seeks, but we do want to allow
936 * fair distribution of slice time for a process doing back-to-back
937 * seeks. so allow a little bit of time for him to submit a new rq
939 if (sample_valid(cic->seek_samples) && cic->seek_mean > 131072)
942 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
946 static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
948 struct cfq_data *cfqd = q->elevator->elevator_data;
949 struct cfq_queue *cfqq = crq->cfq_queue;
951 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
952 cfq_remove_request(crq->request);
953 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
954 elv_dispatch_sort(q, crq->request);
958 * return expired entry, or NULL to just start from scratch in rbtree
960 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
962 struct cfq_data *cfqd = cfqq->cfqd;
966 if (cfq_cfqq_fifo_expire(cfqq))
969 if (!list_empty(&cfqq->fifo)) {
970 int fifo = cfq_cfqq_class_sync(cfqq);
972 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
974 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
975 cfq_mark_cfqq_fifo_expire(cfqq);
984 * Scale schedule slice based on io priority. Use the sync time slice only
985 * if a queue is marked sync and has sync io queued. A sync queue with async
986 * io only, should not get full sync slice length.
989 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
991 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
993 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
995 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
999 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1001 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1005 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1007 const int base_rq = cfqd->cfq_slice_async_rq;
1009 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1011 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1015 * get next queue for service
1017 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1019 unsigned long now = jiffies;
1020 struct cfq_queue *cfqq;
1022 cfqq = cfqd->active_queue;
1029 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1033 * if queue has requests, dispatch one. if not, check if
1034 * enough slice is left to wait for one
1036 if (!RB_EMPTY(&cfqq->sort_list))
1038 else if (cfq_cfqq_class_sync(cfqq) &&
1039 time_before(now, cfqq->slice_end)) {
1040 if (cfq_arm_slice_timer(cfqd, cfqq))
1045 cfq_slice_expired(cfqd, 0);
1047 cfqq = cfq_set_active_queue(cfqd);
1053 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1058 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1064 * follow expired path, else get first next available
1066 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1067 crq = cfqq->next_crq;
1070 * finally, insert request into driver dispatch list
1072 cfq_dispatch_insert(cfqd->queue, crq);
1074 cfqd->dispatch_slice++;
1077 if (!cfqd->active_cic) {
1078 atomic_inc(&crq->io_context->ioc->refcount);
1079 cfqd->active_cic = crq->io_context;
1082 if (RB_EMPTY(&cfqq->sort_list))
1085 } while (dispatched < max_dispatch);
1088 * if slice end isn't set yet, set it. if at least one request was
1089 * sync, use the sync time slice value
1091 if (!cfqq->slice_end)
1092 cfq_set_prio_slice(cfqd, cfqq);
1095 * expire an async queue immediately if it has used up its slice. idle
1096 * queue always expire after 1 dispatch round.
1098 if ((!cfq_cfqq_sync(cfqq) &&
1099 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1100 cfq_class_idle(cfqq))
1101 cfq_slice_expired(cfqd, 0);
1107 cfq_forced_dispatch_cfqqs(struct list_head *list)
1110 struct cfq_queue *cfqq, *next;
1113 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1114 while ((crq = cfqq->next_crq)) {
1115 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1118 BUG_ON(!list_empty(&cfqq->fifo));
1124 cfq_forced_dispatch(struct cfq_data *cfqd)
1126 int i, dispatched = 0;
1128 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1129 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1131 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1132 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1133 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1135 cfq_slice_expired(cfqd, 0);
1137 BUG_ON(cfqd->busy_queues);
1143 cfq_dispatch_requests(request_queue_t *q, int force)
1145 struct cfq_data *cfqd = q->elevator->elevator_data;
1146 struct cfq_queue *cfqq;
1148 if (!cfqd->busy_queues)
1151 if (unlikely(force))
1152 return cfq_forced_dispatch(cfqd);
1154 cfqq = cfq_select_queue(cfqd);
1158 cfq_clear_cfqq_must_dispatch(cfqq);
1159 cfq_clear_cfqq_wait_request(cfqq);
1160 del_timer(&cfqd->idle_slice_timer);
1162 max_dispatch = cfqd->cfq_quantum;
1163 if (cfq_class_idle(cfqq))
1166 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1173 * task holds one reference to the queue, dropped when task exits. each crq
1174 * in-flight on this queue also holds a reference, dropped when crq is freed.
1176 * queue lock must be held here.
1178 static void cfq_put_queue(struct cfq_queue *cfqq)
1180 struct cfq_data *cfqd = cfqq->cfqd;
1182 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1184 if (!atomic_dec_and_test(&cfqq->ref))
1187 BUG_ON(rb_first(&cfqq->sort_list));
1188 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1189 BUG_ON(cfq_cfqq_on_rr(cfqq));
1191 if (unlikely(cfqd->active_queue == cfqq))
1192 __cfq_slice_expired(cfqd, cfqq, 0);
1195 * it's on the empty list and still hashed
1197 list_del(&cfqq->cfq_list);
1198 hlist_del(&cfqq->cfq_hash);
1199 kmem_cache_free(cfq_pool, cfqq);
1202 static inline struct cfq_queue *
1203 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1206 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1207 struct hlist_node *entry;
1208 struct cfq_queue *__cfqq;
1210 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
1211 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1213 if (__cfqq->key == key && (__p == prio || !prio))
1220 static struct cfq_queue *
1221 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1223 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1226 static void cfq_free_io_context(struct io_context *ioc)
1228 struct cfq_io_context *__cic;
1232 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1233 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1234 rb_erase(&__cic->rb_node, &ioc->cic_root);
1235 kmem_cache_free(cfq_ioc_pool, __cic);
1239 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1243 static void cfq_trim(struct io_context *ioc)
1245 ioc->set_ioprio = NULL;
1246 cfq_free_io_context(ioc);
1250 * Called with interrupts disabled
1252 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1254 struct cfq_data *cfqd = cic->key;
1262 WARN_ON(!irqs_disabled());
1264 spin_lock(q->queue_lock);
1266 if (cic->cfqq[ASYNC]) {
1267 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1268 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1269 cfq_put_queue(cic->cfqq[ASYNC]);
1270 cic->cfqq[ASYNC] = NULL;
1273 if (cic->cfqq[SYNC]) {
1274 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1275 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1276 cfq_put_queue(cic->cfqq[SYNC]);
1277 cic->cfqq[SYNC] = NULL;
1281 list_del_init(&cic->queue_list);
1282 spin_unlock(q->queue_lock);
1285 static void cfq_exit_io_context(struct io_context *ioc)
1287 struct cfq_io_context *__cic;
1288 unsigned long flags;
1292 * put the reference this task is holding to the various queues
1294 spin_lock_irqsave(&cfq_exit_lock, flags);
1296 n = rb_first(&ioc->cic_root);
1298 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1300 cfq_exit_single_io_context(__cic);
1304 spin_unlock_irqrestore(&cfq_exit_lock, flags);
1307 static struct cfq_io_context *
1308 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1310 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1313 RB_CLEAR(&cic->rb_node);
1315 cic->cfqq[ASYNC] = NULL;
1316 cic->cfqq[SYNC] = NULL;
1317 cic->last_end_request = jiffies;
1318 cic->ttime_total = 0;
1319 cic->ttime_samples = 0;
1320 cic->ttime_mean = 0;
1321 cic->dtor = cfq_free_io_context;
1322 cic->exit = cfq_exit_io_context;
1323 INIT_LIST_HEAD(&cic->queue_list);
1324 atomic_inc(&ioc_count);
1330 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1332 struct task_struct *tsk = current;
1335 if (!cfq_cfqq_prio_changed(cfqq))
1338 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1339 switch (ioprio_class) {
1341 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1342 case IOPRIO_CLASS_NONE:
1344 * no prio set, place us in the middle of the BE classes
1346 cfqq->ioprio = task_nice_ioprio(tsk);
1347 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1349 case IOPRIO_CLASS_RT:
1350 cfqq->ioprio = task_ioprio(tsk);
1351 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1353 case IOPRIO_CLASS_BE:
1354 cfqq->ioprio = task_ioprio(tsk);
1355 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1357 case IOPRIO_CLASS_IDLE:
1358 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1360 cfq_clear_cfqq_idle_window(cfqq);
1365 * keep track of original prio settings in case we have to temporarily
1366 * elevate the priority of this queue
1368 cfqq->org_ioprio = cfqq->ioprio;
1369 cfqq->org_ioprio_class = cfqq->ioprio_class;
1371 if (cfq_cfqq_on_rr(cfqq))
1372 cfq_resort_rr_list(cfqq, 0);
1374 cfq_clear_cfqq_prio_changed(cfqq);
1377 static inline void changed_ioprio(struct cfq_io_context *cic)
1379 struct cfq_data *cfqd = cic->key;
1380 struct cfq_queue *cfqq;
1382 spin_lock(cfqd->queue->queue_lock);
1383 cfqq = cic->cfqq[ASYNC];
1385 struct cfq_queue *new_cfqq;
1386 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
1387 cic->ioc->task, GFP_ATOMIC);
1389 cic->cfqq[ASYNC] = new_cfqq;
1390 cfq_put_queue(cfqq);
1393 cfqq = cic->cfqq[SYNC];
1395 cfq_mark_cfqq_prio_changed(cfqq);
1396 cfq_init_prio_data(cfqq);
1398 spin_unlock(cfqd->queue->queue_lock);
1403 * callback from sys_ioprio_set, irqs are disabled
1405 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1407 struct cfq_io_context *cic;
1410 spin_lock(&cfq_exit_lock);
1412 n = rb_first(&ioc->cic_root);
1414 cic = rb_entry(n, struct cfq_io_context, rb_node);
1416 changed_ioprio(cic);
1420 spin_unlock(&cfq_exit_lock);
1425 static struct cfq_queue *
1426 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
1429 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1430 struct cfq_queue *cfqq, *new_cfqq = NULL;
1431 unsigned short ioprio;
1434 ioprio = tsk->ioprio;
1435 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1441 } else if (gfp_mask & __GFP_WAIT) {
1442 spin_unlock_irq(cfqd->queue->queue_lock);
1443 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1444 spin_lock_irq(cfqd->queue->queue_lock);
1447 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1452 memset(cfqq, 0, sizeof(*cfqq));
1454 INIT_HLIST_NODE(&cfqq->cfq_hash);
1455 INIT_LIST_HEAD(&cfqq->cfq_list);
1456 RB_CLEAR_ROOT(&cfqq->sort_list);
1457 INIT_LIST_HEAD(&cfqq->fifo);
1460 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1461 atomic_set(&cfqq->ref, 0);
1463 cfqq->service_last = 0;
1465 * set ->slice_left to allow preemption for a new process
1467 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1468 cfq_mark_cfqq_idle_window(cfqq);
1469 cfq_mark_cfqq_prio_changed(cfqq);
1470 cfq_init_prio_data(cfqq);
1474 kmem_cache_free(cfq_pool, new_cfqq);
1476 atomic_inc(&cfqq->ref);
1478 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1483 cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1485 spin_lock(&cfq_exit_lock);
1486 rb_erase(&cic->rb_node, &ioc->cic_root);
1487 list_del_init(&cic->queue_list);
1488 spin_unlock(&cfq_exit_lock);
1489 kmem_cache_free(cfq_ioc_pool, cic);
1490 atomic_dec(&ioc_count);
1493 static struct cfq_io_context *
1494 cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1497 struct cfq_io_context *cic;
1498 void *k, *key = cfqd;
1501 n = ioc->cic_root.rb_node;
1503 cic = rb_entry(n, struct cfq_io_context, rb_node);
1504 /* ->key must be copied to avoid race with cfq_exit_queue() */
1507 cfq_drop_dead_cic(ioc, cic);
1523 cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1524 struct cfq_io_context *cic)
1527 struct rb_node *parent;
1528 struct cfq_io_context *__cic;
1534 ioc->set_ioprio = cfq_ioc_set_ioprio;
1537 p = &ioc->cic_root.rb_node;
1540 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1541 /* ->key must be copied to avoid race with cfq_exit_queue() */
1544 cfq_drop_dead_cic(ioc, cic);
1550 else if (cic->key > k)
1551 p = &(*p)->rb_right;
1556 spin_lock(&cfq_exit_lock);
1557 rb_link_node(&cic->rb_node, parent, p);
1558 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1559 list_add(&cic->queue_list, &cfqd->cic_list);
1560 spin_unlock(&cfq_exit_lock);
1564 * Setup general io context and cfq io context. There can be several cfq
1565 * io contexts per general io context, if this process is doing io to more
1566 * than one device managed by cfq.
1568 static struct cfq_io_context *
1569 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1571 struct io_context *ioc = NULL;
1572 struct cfq_io_context *cic;
1574 might_sleep_if(gfp_mask & __GFP_WAIT);
1576 ioc = get_io_context(gfp_mask);
1580 cic = cfq_cic_rb_lookup(cfqd, ioc);
1584 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1588 cfq_cic_link(cfqd, ioc, cic);
1592 put_io_context(ioc);
1597 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1599 unsigned long elapsed, ttime;
1602 * if this context already has stuff queued, thinktime is from
1603 * last queue not last end
1606 if (time_after(cic->last_end_request, cic->last_queue))
1607 elapsed = jiffies - cic->last_end_request;
1609 elapsed = jiffies - cic->last_queue;
1611 elapsed = jiffies - cic->last_end_request;
1614 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1616 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1617 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1618 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1622 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1628 if (cic->last_request_pos < crq->request->sector)
1629 sdist = crq->request->sector - cic->last_request_pos;
1631 sdist = cic->last_request_pos - crq->request->sector;
1634 * Don't allow the seek distance to get too large from the
1635 * odd fragment, pagein, etc
1637 if (cic->seek_samples <= 60) /* second&third seek */
1638 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1640 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1642 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1643 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1644 total = cic->seek_total + (cic->seek_samples/2);
1645 do_div(total, cic->seek_samples);
1646 cic->seek_mean = (sector_t)total;
1650 * Disable idle window if the process thinks too long or seeks so much that
1654 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1655 struct cfq_io_context *cic)
1657 int enable_idle = cfq_cfqq_idle_window(cfqq);
1659 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1661 else if (sample_valid(cic->ttime_samples)) {
1662 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1669 cfq_mark_cfqq_idle_window(cfqq);
1671 cfq_clear_cfqq_idle_window(cfqq);
1676 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1677 * no or if we aren't sure, a 1 will cause a preempt.
1680 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1683 struct cfq_queue *cfqq = cfqd->active_queue;
1685 if (cfq_class_idle(new_cfqq))
1691 if (cfq_class_idle(cfqq))
1693 if (!cfq_cfqq_wait_request(new_cfqq))
1696 * if it doesn't have slice left, forget it
1698 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1700 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1707 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1708 * let it have half of its nominal slice.
1710 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1712 struct cfq_queue *__cfqq, *next;
1714 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1715 cfq_resort_rr_list(__cfqq, 1);
1717 if (!cfqq->slice_left)
1718 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1720 cfqq->slice_end = cfqq->slice_left + jiffies;
1721 __cfq_slice_expired(cfqd, cfqq, 1);
1722 __cfq_set_active_queue(cfqd, cfqq);
1726 * should really be a ll_rw_blk.c helper
1728 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1730 request_queue_t *q = cfqd->queue;
1732 if (!blk_queue_plugged(q))
1735 __generic_unplug_device(q);
1739 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1740 * something we should do about it
1743 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1746 struct cfq_io_context *cic;
1748 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1751 * we never wait for an async request and we don't allow preemption
1752 * of an async request. so just return early
1754 if (!cfq_crq_is_sync(crq))
1757 cic = crq->io_context;
1759 cfq_update_io_thinktime(cfqd, cic);
1760 cfq_update_io_seektime(cfqd, cic, crq);
1761 cfq_update_idle_window(cfqd, cfqq, cic);
1763 cic->last_queue = jiffies;
1764 cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
1766 if (cfqq == cfqd->active_queue) {
1768 * if we are waiting for a request for this queue, let it rip
1769 * immediately and flag that we must not expire this queue
1772 if (cfq_cfqq_wait_request(cfqq)) {
1773 cfq_mark_cfqq_must_dispatch(cfqq);
1774 del_timer(&cfqd->idle_slice_timer);
1775 cfq_start_queueing(cfqd, cfqq);
1777 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1779 * not the active queue - expire current slice if it is
1780 * idle and has expired it's mean thinktime or this new queue
1781 * has some old slice time left and is of higher priority
1783 cfq_preempt_queue(cfqd, cfqq);
1784 cfq_mark_cfqq_must_dispatch(cfqq);
1785 cfq_start_queueing(cfqd, cfqq);
1789 static void cfq_insert_request(request_queue_t *q, struct request *rq)
1791 struct cfq_data *cfqd = q->elevator->elevator_data;
1792 struct cfq_rq *crq = RQ_DATA(rq);
1793 struct cfq_queue *cfqq = crq->cfq_queue;
1795 cfq_init_prio_data(cfqq);
1797 cfq_add_crq_rb(crq);
1799 list_add_tail(&rq->queuelist, &cfqq->fifo);
1801 if (rq_mergeable(rq))
1802 cfq_add_crq_hash(cfqd, crq);
1804 cfq_crq_enqueued(cfqd, cfqq, crq);
1807 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1809 struct cfq_rq *crq = RQ_DATA(rq);
1810 struct cfq_queue *cfqq = crq->cfq_queue;
1811 struct cfq_data *cfqd = cfqq->cfqd;
1812 const int sync = cfq_crq_is_sync(crq);
1817 WARN_ON(!cfqd->rq_in_driver);
1818 WARN_ON(!cfqq->on_dispatch[sync]);
1819 cfqd->rq_in_driver--;
1820 cfqq->on_dispatch[sync]--;
1822 if (!cfq_class_idle(cfqq))
1823 cfqd->last_end_request = now;
1825 if (!cfq_cfqq_dispatched(cfqq)) {
1826 if (cfq_cfqq_on_rr(cfqq)) {
1827 cfqq->service_last = now;
1828 cfq_resort_rr_list(cfqq, 0);
1830 cfq_schedule_dispatch(cfqd);
1833 if (cfq_crq_is_sync(crq))
1834 crq->io_context->last_end_request = now;
1837 static struct request *
1838 cfq_former_request(request_queue_t *q, struct request *rq)
1840 struct cfq_rq *crq = RQ_DATA(rq);
1841 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1844 return rb_entry_crq(rbprev)->request;
1849 static struct request *
1850 cfq_latter_request(request_queue_t *q, struct request *rq)
1852 struct cfq_rq *crq = RQ_DATA(rq);
1853 struct rb_node *rbnext = rb_next(&crq->rb_node);
1856 return rb_entry_crq(rbnext)->request;
1862 * we temporarily boost lower priority queues if they are holding fs exclusive
1863 * resources. they are boosted to normal prio (CLASS_BE/4)
1865 static void cfq_prio_boost(struct cfq_queue *cfqq)
1867 const int ioprio_class = cfqq->ioprio_class;
1868 const int ioprio = cfqq->ioprio;
1870 if (has_fs_excl()) {
1872 * boost idle prio on transactions that would lock out other
1873 * users of the filesystem
1875 if (cfq_class_idle(cfqq))
1876 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1877 if (cfqq->ioprio > IOPRIO_NORM)
1878 cfqq->ioprio = IOPRIO_NORM;
1881 * check if we need to unboost the queue
1883 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1884 cfqq->ioprio_class = cfqq->org_ioprio_class;
1885 if (cfqq->ioprio != cfqq->org_ioprio)
1886 cfqq->ioprio = cfqq->org_ioprio;
1890 * refile between round-robin lists if we moved the priority class
1892 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1893 cfq_cfqq_on_rr(cfqq))
1894 cfq_resort_rr_list(cfqq, 0);
1898 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1899 struct task_struct *task, int rw)
1902 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1903 !cfq_cfqq_must_alloc_slice(cfqq)) {
1904 cfq_mark_cfqq_must_alloc_slice(cfqq);
1905 return ELV_MQUEUE_MUST;
1908 return ELV_MQUEUE_MAY;
1910 if (!cfqq || task->flags & PF_MEMALLOC)
1911 return ELV_MQUEUE_MAY;
1912 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1913 if (cfq_cfqq_wait_request(cfqq))
1914 return ELV_MQUEUE_MUST;
1917 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1918 * can quickly flood the queue with writes from a single task
1920 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
1921 cfq_mark_cfqq_must_alloc_slice(cfqq);
1922 return ELV_MQUEUE_MUST;
1925 return ELV_MQUEUE_MAY;
1927 if (cfq_class_idle(cfqq))
1928 return ELV_MQUEUE_NO;
1929 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1930 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1931 int ret = ELV_MQUEUE_NO;
1933 if (ioc && ioc->nr_batch_requests)
1934 ret = ELV_MQUEUE_MAY;
1936 put_io_context(ioc);
1940 return ELV_MQUEUE_MAY;
1944 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1946 struct cfq_data *cfqd = q->elevator->elevator_data;
1947 struct task_struct *tsk = current;
1948 struct cfq_queue *cfqq;
1951 * don't force setup of a queue from here, as a call to may_queue
1952 * does not necessarily imply that a request actually will be queued.
1953 * so just lookup a possibly existing queue, or return 'may queue'
1956 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
1958 cfq_init_prio_data(cfqq);
1959 cfq_prio_boost(cfqq);
1961 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1964 return ELV_MQUEUE_MAY;
1967 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1969 struct cfq_data *cfqd = q->elevator->elevator_data;
1970 struct request_list *rl = &q->rq;
1972 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1974 if (waitqueue_active(&rl->wait[READ]))
1975 wake_up(&rl->wait[READ]);
1978 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1980 if (waitqueue_active(&rl->wait[WRITE]))
1981 wake_up(&rl->wait[WRITE]);
1986 * queue lock held here
1988 static void cfq_put_request(request_queue_t *q, struct request *rq)
1990 struct cfq_data *cfqd = q->elevator->elevator_data;
1991 struct cfq_rq *crq = RQ_DATA(rq);
1994 struct cfq_queue *cfqq = crq->cfq_queue;
1995 const int rw = rq_data_dir(rq);
1997 BUG_ON(!cfqq->allocated[rw]);
1998 cfqq->allocated[rw]--;
2000 put_io_context(crq->io_context->ioc);
2002 mempool_free(crq, cfqd->crq_pool);
2003 rq->elevator_private = NULL;
2005 cfq_check_waiters(q, cfqq);
2006 cfq_put_queue(cfqq);
2011 * Allocate cfq data structures associated with this request.
2014 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
2017 struct cfq_data *cfqd = q->elevator->elevator_data;
2018 struct task_struct *tsk = current;
2019 struct cfq_io_context *cic;
2020 const int rw = rq_data_dir(rq);
2021 pid_t key = cfq_queue_pid(tsk, rw);
2022 struct cfq_queue *cfqq;
2024 unsigned long flags;
2025 int is_sync = key != CFQ_KEY_ASYNC;
2027 might_sleep_if(gfp_mask & __GFP_WAIT);
2029 cic = cfq_get_io_context(cfqd, gfp_mask);
2031 spin_lock_irqsave(q->queue_lock, flags);
2036 if (!cic->cfqq[is_sync]) {
2037 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
2041 cic->cfqq[is_sync] = cfqq;
2043 cfqq = cic->cfqq[is_sync];
2045 cfqq->allocated[rw]++;
2046 cfq_clear_cfqq_must_alloc(cfqq);
2047 cfqd->rq_starved = 0;
2048 atomic_inc(&cfqq->ref);
2049 spin_unlock_irqrestore(q->queue_lock, flags);
2051 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2053 RB_CLEAR(&crq->rb_node);
2056 INIT_HLIST_NODE(&crq->hash);
2057 crq->cfq_queue = cfqq;
2058 crq->io_context = cic;
2061 cfq_mark_crq_is_sync(crq);
2063 cfq_clear_crq_is_sync(crq);
2065 rq->elevator_private = crq;
2069 spin_lock_irqsave(q->queue_lock, flags);
2070 cfqq->allocated[rw]--;
2071 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
2072 cfq_mark_cfqq_must_alloc(cfqq);
2073 cfq_put_queue(cfqq);
2076 put_io_context(cic->ioc);
2078 * mark us rq allocation starved. we need to kickstart the process
2079 * ourselves if there are no pending requests that can do it for us.
2080 * that would be an extremely rare OOM situation
2082 cfqd->rq_starved = 1;
2083 cfq_schedule_dispatch(cfqd);
2084 spin_unlock_irqrestore(q->queue_lock, flags);
2088 static void cfq_kick_queue(void *data)
2090 request_queue_t *q = data;
2091 struct cfq_data *cfqd = q->elevator->elevator_data;
2092 unsigned long flags;
2094 spin_lock_irqsave(q->queue_lock, flags);
2096 if (cfqd->rq_starved) {
2097 struct request_list *rl = &q->rq;
2100 * we aren't guaranteed to get a request after this, but we
2101 * have to be opportunistic
2104 if (waitqueue_active(&rl->wait[READ]))
2105 wake_up(&rl->wait[READ]);
2106 if (waitqueue_active(&rl->wait[WRITE]))
2107 wake_up(&rl->wait[WRITE]);
2112 spin_unlock_irqrestore(q->queue_lock, flags);
2116 * Timer running if the active_queue is currently idling inside its time slice
2118 static void cfq_idle_slice_timer(unsigned long data)
2120 struct cfq_data *cfqd = (struct cfq_data *) data;
2121 struct cfq_queue *cfqq;
2122 unsigned long flags;
2124 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2126 if ((cfqq = cfqd->active_queue) != NULL) {
2127 unsigned long now = jiffies;
2132 if (time_after(now, cfqq->slice_end))
2136 * only expire and reinvoke request handler, if there are
2137 * other queues with pending requests
2139 if (!cfqd->busy_queues) {
2140 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2141 add_timer(&cfqd->idle_slice_timer);
2146 * not expired and it has a request pending, let it dispatch
2148 if (!RB_EMPTY(&cfqq->sort_list)) {
2149 cfq_mark_cfqq_must_dispatch(cfqq);
2154 cfq_slice_expired(cfqd, 0);
2156 cfq_schedule_dispatch(cfqd);
2158 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2162 * Timer running if an idle class queue is waiting for service
2164 static void cfq_idle_class_timer(unsigned long data)
2166 struct cfq_data *cfqd = (struct cfq_data *) data;
2167 unsigned long flags, end;
2169 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2172 * race with a non-idle queue, reset timer
2174 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2175 if (!time_after_eq(jiffies, end)) {
2176 cfqd->idle_class_timer.expires = end;
2177 add_timer(&cfqd->idle_class_timer);
2179 cfq_schedule_dispatch(cfqd);
2181 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2184 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2186 del_timer_sync(&cfqd->idle_slice_timer);
2187 del_timer_sync(&cfqd->idle_class_timer);
2188 blk_sync_queue(cfqd->queue);
2191 static void cfq_exit_queue(elevator_t *e)
2193 struct cfq_data *cfqd = e->elevator_data;
2194 request_queue_t *q = cfqd->queue;
2196 cfq_shutdown_timer_wq(cfqd);
2198 spin_lock(&cfq_exit_lock);
2199 spin_lock_irq(q->queue_lock);
2201 if (cfqd->active_queue)
2202 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2204 while (!list_empty(&cfqd->cic_list)) {
2205 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2206 struct cfq_io_context,
2208 if (cic->cfqq[ASYNC]) {
2209 cfq_put_queue(cic->cfqq[ASYNC]);
2210 cic->cfqq[ASYNC] = NULL;
2212 if (cic->cfqq[SYNC]) {
2213 cfq_put_queue(cic->cfqq[SYNC]);
2214 cic->cfqq[SYNC] = NULL;
2217 list_del_init(&cic->queue_list);
2220 spin_unlock_irq(q->queue_lock);
2221 spin_unlock(&cfq_exit_lock);
2223 cfq_shutdown_timer_wq(cfqd);
2225 mempool_destroy(cfqd->crq_pool);
2226 kfree(cfqd->crq_hash);
2227 kfree(cfqd->cfq_hash);
2231 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2233 struct cfq_data *cfqd;
2236 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2240 memset(cfqd, 0, sizeof(*cfqd));
2242 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2243 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2245 INIT_LIST_HEAD(&cfqd->busy_rr);
2246 INIT_LIST_HEAD(&cfqd->cur_rr);
2247 INIT_LIST_HEAD(&cfqd->idle_rr);
2248 INIT_LIST_HEAD(&cfqd->empty_list);
2249 INIT_LIST_HEAD(&cfqd->cic_list);
2251 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2252 if (!cfqd->crq_hash)
2255 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2256 if (!cfqd->cfq_hash)
2259 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
2260 if (!cfqd->crq_pool)
2263 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2264 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2265 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2266 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2268 e->elevator_data = cfqd;
2272 cfqd->max_queued = q->nr_requests / 4;
2273 q->nr_batching = cfq_queued;
2275 init_timer(&cfqd->idle_slice_timer);
2276 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2277 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2279 init_timer(&cfqd->idle_class_timer);
2280 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2281 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2283 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2285 cfqd->cfq_queued = cfq_queued;
2286 cfqd->cfq_quantum = cfq_quantum;
2287 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2288 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2289 cfqd->cfq_back_max = cfq_back_max;
2290 cfqd->cfq_back_penalty = cfq_back_penalty;
2291 cfqd->cfq_slice[0] = cfq_slice_async;
2292 cfqd->cfq_slice[1] = cfq_slice_sync;
2293 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2294 cfqd->cfq_slice_idle = cfq_slice_idle;
2298 kfree(cfqd->cfq_hash);
2300 kfree(cfqd->crq_hash);
2306 static void cfq_slab_kill(void)
2309 kmem_cache_destroy(crq_pool);
2311 kmem_cache_destroy(cfq_pool);
2313 kmem_cache_destroy(cfq_ioc_pool);
2316 static int __init cfq_slab_setup(void)
2318 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2323 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2328 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2329 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2340 * sysfs parts below -->
2344 cfq_var_show(unsigned int var, char *page)
2346 return sprintf(page, "%d\n", var);
2350 cfq_var_store(unsigned int *var, const char *page, size_t count)
2352 char *p = (char *) page;
2354 *var = simple_strtoul(p, &p, 10);
2358 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2359 static ssize_t __FUNC(elevator_t *e, char *page) \
2361 struct cfq_data *cfqd = e->elevator_data; \
2362 unsigned int __data = __VAR; \
2364 __data = jiffies_to_msecs(__data); \
2365 return cfq_var_show(__data, (page)); \
2367 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2368 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2369 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2370 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2371 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2372 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2373 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2374 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2375 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2376 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2377 #undef SHOW_FUNCTION
2379 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2380 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2382 struct cfq_data *cfqd = e->elevator_data; \
2383 unsigned int __data; \
2384 int ret = cfq_var_store(&__data, (page), count); \
2385 if (__data < (MIN)) \
2387 else if (__data > (MAX)) \
2390 *(__PTR) = msecs_to_jiffies(__data); \
2392 *(__PTR) = __data; \
2395 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2396 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2397 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2398 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2399 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2400 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2401 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2402 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2403 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2404 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2405 #undef STORE_FUNCTION
2407 #define CFQ_ATTR(name) \
2408 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2410 static struct elv_fs_entry cfq_attrs[] = {
2413 CFQ_ATTR(fifo_expire_sync),
2414 CFQ_ATTR(fifo_expire_async),
2415 CFQ_ATTR(back_seek_max),
2416 CFQ_ATTR(back_seek_penalty),
2417 CFQ_ATTR(slice_sync),
2418 CFQ_ATTR(slice_async),
2419 CFQ_ATTR(slice_async_rq),
2420 CFQ_ATTR(slice_idle),
2424 static struct elevator_type iosched_cfq = {
2426 .elevator_merge_fn = cfq_merge,
2427 .elevator_merged_fn = cfq_merged_request,
2428 .elevator_merge_req_fn = cfq_merged_requests,
2429 .elevator_dispatch_fn = cfq_dispatch_requests,
2430 .elevator_add_req_fn = cfq_insert_request,
2431 .elevator_activate_req_fn = cfq_activate_request,
2432 .elevator_deactivate_req_fn = cfq_deactivate_request,
2433 .elevator_queue_empty_fn = cfq_queue_empty,
2434 .elevator_completed_req_fn = cfq_completed_request,
2435 .elevator_former_req_fn = cfq_former_request,
2436 .elevator_latter_req_fn = cfq_latter_request,
2437 .elevator_set_req_fn = cfq_set_request,
2438 .elevator_put_req_fn = cfq_put_request,
2439 .elevator_may_queue_fn = cfq_may_queue,
2440 .elevator_init_fn = cfq_init_queue,
2441 .elevator_exit_fn = cfq_exit_queue,
2444 .elevator_attrs = cfq_attrs,
2445 .elevator_name = "cfq",
2446 .elevator_owner = THIS_MODULE,
2449 static int __init cfq_init(void)
2454 * could be 0 on HZ < 1000 setups
2456 if (!cfq_slice_async)
2457 cfq_slice_async = 1;
2458 if (!cfq_slice_idle)
2461 if (cfq_slab_setup())
2464 ret = elv_register(&iosched_cfq);
2471 static void __exit cfq_exit(void)
2473 DECLARE_COMPLETION(all_gone);
2474 elv_unregister(&iosched_cfq);
2475 ioc_gone = &all_gone;
2476 /* ioc_gone's update must be visible before reading ioc_count */
2478 if (atomic_read(&ioc_count))
2479 wait_for_completion(ioc_gone);
2484 module_init(cfq_init);
2485 module_exit(cfq_exit);
2487 MODULE_AUTHOR("Jens Axboe");
2488 MODULE_LICENSE("GPL");
2489 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");