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/module.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/hash.h>
13 #include <linux/rbtree.h>
14 #include <linux/ioprio.h>
19 static const int cfq_quantum = 4; /* max queue in one round of service */
20 static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
21 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22 static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
23 static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
25 static const int cfq_slice_sync = HZ / 10;
26 static int cfq_slice_async = HZ / 25;
27 static const int cfq_slice_async_rq = 2;
28 static int cfq_slice_idle = HZ / 125;
30 #define CFQ_IDLE_GRACE (HZ / 10)
31 #define CFQ_SLICE_SCALE (5)
33 #define CFQ_KEY_ASYNC (0)
35 static DEFINE_SPINLOCK(cfq_exit_lock);
38 * for the hash of cfqq inside the cfqd
40 #define CFQ_QHASH_SHIFT 6
41 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
42 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
44 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
46 #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
47 #define RQ_CFQQ(rq) ((rq)->elevator_private2)
49 static kmem_cache_t *cfq_pool;
50 static kmem_cache_t *cfq_ioc_pool;
52 static atomic_t ioc_count = ATOMIC_INIT(0);
53 static struct completion *ioc_gone;
55 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
56 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
57 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
58 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
63 #define cfq_cfqq_dispatched(cfqq) \
64 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
66 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
68 #define cfq_cfqq_sync(cfqq) \
69 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
71 #define sample_valid(samples) ((samples) > 80)
74 * Per block device queue structure
77 request_queue_t *queue;
80 * rr list of queues with requests and the count of them
82 struct list_head rr_list[CFQ_PRIO_LISTS];
83 struct list_head busy_rr;
84 struct list_head cur_rr;
85 struct list_head idle_rr;
86 unsigned int busy_queues;
89 * non-ordered list of empty cfqq's
91 struct list_head empty_list;
96 struct hlist_head *cfq_hash;
102 * schedule slice state info
105 * idle window management
107 struct timer_list idle_slice_timer;
108 struct work_struct unplug_work;
110 struct cfq_queue *active_queue;
111 struct cfq_io_context *active_cic;
112 int cur_prio, cur_end_prio;
113 unsigned int dispatch_slice;
115 struct timer_list idle_class_timer;
117 sector_t last_sector;
118 unsigned long last_end_request;
120 unsigned int rq_starved;
123 * tunables, see top of file
125 unsigned int cfq_quantum;
126 unsigned int cfq_queued;
127 unsigned int cfq_fifo_expire[2];
128 unsigned int cfq_back_penalty;
129 unsigned int cfq_back_max;
130 unsigned int cfq_slice[2];
131 unsigned int cfq_slice_async_rq;
132 unsigned int cfq_slice_idle;
134 struct list_head cic_list;
138 * Per process-grouping structure
141 /* reference count */
143 /* parent cfq_data */
144 struct cfq_data *cfqd;
145 /* cfqq lookup hash */
146 struct hlist_node cfq_hash;
149 /* on either rr or empty list of cfqd */
150 struct list_head cfq_list;
151 /* sorted list of pending requests */
152 struct rb_root sort_list;
153 /* if fifo isn't expired, next request to serve */
154 struct request *next_rq;
155 /* requests queued in sort_list */
157 /* currently allocated requests */
159 /* fifo list of requests in sort_list */
160 struct list_head fifo;
162 unsigned long slice_start;
163 unsigned long slice_end;
164 unsigned long slice_left;
165 unsigned long service_last;
167 /* number of requests that are on the dispatch list */
170 /* io prio of this group */
171 unsigned short ioprio, org_ioprio;
172 unsigned short ioprio_class, org_ioprio_class;
174 /* various state flags, see below */
178 enum cfqq_state_flags {
179 CFQ_CFQQ_FLAG_on_rr = 0,
180 CFQ_CFQQ_FLAG_wait_request,
181 CFQ_CFQQ_FLAG_must_alloc,
182 CFQ_CFQQ_FLAG_must_alloc_slice,
183 CFQ_CFQQ_FLAG_must_dispatch,
184 CFQ_CFQQ_FLAG_fifo_expire,
185 CFQ_CFQQ_FLAG_idle_window,
186 CFQ_CFQQ_FLAG_prio_changed,
189 #define CFQ_CFQQ_FNS(name) \
190 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
192 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
194 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
196 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
198 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
200 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
204 CFQ_CFQQ_FNS(wait_request);
205 CFQ_CFQQ_FNS(must_alloc);
206 CFQ_CFQQ_FNS(must_alloc_slice);
207 CFQ_CFQQ_FNS(must_dispatch);
208 CFQ_CFQQ_FNS(fifo_expire);
209 CFQ_CFQQ_FNS(idle_window);
210 CFQ_CFQQ_FNS(prio_changed);
213 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
214 static void cfq_dispatch_insert(request_queue_t *, struct request *);
215 static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
218 * scheduler run of queue, if there are requests pending and no one in the
219 * driver that will restart queueing
221 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
223 if (cfqd->busy_queues)
224 kblockd_schedule_work(&cfqd->unplug_work);
227 static int cfq_queue_empty(request_queue_t *q)
229 struct cfq_data *cfqd = q->elevator->elevator_data;
231 return !cfqd->busy_queues;
234 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
236 if (rw == READ || rw == WRITE_SYNC)
239 return CFQ_KEY_ASYNC;
243 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
244 * We choose the request that is closest to the head right now. Distance
245 * behind the head is penalized and only allowed to a certain extent.
247 static struct request *
248 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
250 sector_t last, s1, s2, d1 = 0, d2 = 0;
251 unsigned long back_max;
252 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
253 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
254 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
256 if (rq1 == NULL || rq1 == rq2)
261 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
263 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
269 last = cfqd->last_sector;
272 * by definition, 1KiB is 2 sectors
274 back_max = cfqd->cfq_back_max * 2;
277 * Strict one way elevator _except_ in the case where we allow
278 * short backward seeks which are biased as twice the cost of a
279 * similar forward seek.
283 else if (s1 + back_max >= last)
284 d1 = (last - s1) * cfqd->cfq_back_penalty;
286 wrap |= CFQ_RQ1_WRAP;
290 else if (s2 + back_max >= last)
291 d2 = (last - s2) * cfqd->cfq_back_penalty;
293 wrap |= CFQ_RQ2_WRAP;
295 /* Found required data */
298 * By doing switch() on the bit mask "wrap" we avoid having to
299 * check two variables for all permutations: --> faster!
302 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
318 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
321 * Since both rqs are wrapped,
322 * start with the one that's further behind head
323 * (--> only *one* back seek required),
324 * since back seek takes more time than forward.
334 * would be nice to take fifo expire time into account as well
336 static struct request *
337 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
338 struct request *last)
340 struct rb_node *rbnext = rb_next(&last->rb_node);
341 struct rb_node *rbprev = rb_prev(&last->rb_node);
342 struct request *next = NULL, *prev = NULL;
344 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
347 prev = rb_entry_rq(rbprev);
350 next = rb_entry_rq(rbnext);
352 rbnext = rb_first(&cfqq->sort_list);
353 if (rbnext && rbnext != &last->rb_node)
354 next = rb_entry_rq(rbnext);
357 return cfq_choose_req(cfqd, next, prev);
360 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
362 struct cfq_data *cfqd = cfqq->cfqd;
363 struct list_head *list, *entry;
365 BUG_ON(!cfq_cfqq_on_rr(cfqq));
367 list_del(&cfqq->cfq_list);
369 if (cfq_class_rt(cfqq))
370 list = &cfqd->cur_rr;
371 else if (cfq_class_idle(cfqq))
372 list = &cfqd->idle_rr;
375 * if cfqq has requests in flight, don't allow it to be
376 * found in cfq_set_active_queue before it has finished them.
377 * this is done to increase fairness between a process that
378 * has lots of io pending vs one that only generates one
379 * sporadically or synchronously
381 if (cfq_cfqq_dispatched(cfqq))
382 list = &cfqd->busy_rr;
384 list = &cfqd->rr_list[cfqq->ioprio];
388 * if queue was preempted, just add to front to be fair. busy_rr
389 * isn't sorted, but insert at the back for fairness.
391 if (preempted || list == &cfqd->busy_rr) {
395 list_add_tail(&cfqq->cfq_list, list);
400 * sort by when queue was last serviced
403 while ((entry = entry->prev) != list) {
404 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
406 if (!__cfqq->service_last)
408 if (time_before(__cfqq->service_last, cfqq->service_last))
412 list_add(&cfqq->cfq_list, entry);
416 * add to busy list of queues for service, trying to be fair in ordering
417 * the pending list according to last request service
420 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
422 BUG_ON(cfq_cfqq_on_rr(cfqq));
423 cfq_mark_cfqq_on_rr(cfqq);
426 cfq_resort_rr_list(cfqq, 0);
430 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
432 BUG_ON(!cfq_cfqq_on_rr(cfqq));
433 cfq_clear_cfqq_on_rr(cfqq);
434 list_move(&cfqq->cfq_list, &cfqd->empty_list);
436 BUG_ON(!cfqd->busy_queues);
441 * rb tree support functions
443 static inline void cfq_del_rq_rb(struct request *rq)
445 struct cfq_queue *cfqq = RQ_CFQQ(rq);
446 struct cfq_data *cfqd = cfqq->cfqd;
447 const int sync = rq_is_sync(rq);
449 BUG_ON(!cfqq->queued[sync]);
450 cfqq->queued[sync]--;
452 elv_rb_del(&cfqq->sort_list, rq);
454 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
455 cfq_del_cfqq_rr(cfqd, cfqq);
458 static void cfq_add_rq_rb(struct request *rq)
460 struct cfq_queue *cfqq = RQ_CFQQ(rq);
461 struct cfq_data *cfqd = cfqq->cfqd;
462 struct request *__alias;
464 cfqq->queued[rq_is_sync(rq)]++;
467 * looks a little odd, but the first insert might return an alias.
468 * if that happens, put the alias on the dispatch list
470 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
471 cfq_dispatch_insert(cfqd->queue, __alias);
475 cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
477 elv_rb_del(&cfqq->sort_list, rq);
478 cfqq->queued[rq_is_sync(rq)]--;
482 static struct request *
483 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
485 struct task_struct *tsk = current;
486 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
487 sector_t sector = bio->bi_sector + bio_sectors(bio);
488 struct cfq_queue *cfqq;
490 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
492 return elv_rb_find(&cfqq->sort_list, sector);
497 static void cfq_activate_request(request_queue_t *q, struct request *rq)
499 struct cfq_data *cfqd = q->elevator->elevator_data;
501 cfqd->rq_in_driver++;
504 * If the depth is larger 1, it really could be queueing. But lets
505 * make the mark a little higher - idling could still be good for
506 * low queueing, and a low queueing number could also just indicate
507 * a SCSI mid layer like behaviour where limit+1 is often seen.
509 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
513 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
515 struct cfq_data *cfqd = q->elevator->elevator_data;
517 WARN_ON(!cfqd->rq_in_driver);
518 cfqd->rq_in_driver--;
521 static void cfq_remove_request(struct request *rq)
523 struct cfq_queue *cfqq = RQ_CFQQ(rq);
525 if (cfqq->next_rq == rq)
526 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
528 list_del_init(&rq->queuelist);
533 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
535 struct cfq_data *cfqd = q->elevator->elevator_data;
536 struct request *__rq;
538 __rq = cfq_find_rq_fmerge(cfqd, bio);
539 if (__rq && elv_rq_merge_ok(__rq, bio)) {
541 return ELEVATOR_FRONT_MERGE;
544 return ELEVATOR_NO_MERGE;
547 static void cfq_merged_request(request_queue_t *q, struct request *req,
550 if (type == ELEVATOR_FRONT_MERGE) {
551 struct cfq_queue *cfqq = RQ_CFQQ(req);
553 cfq_reposition_rq_rb(cfqq, req);
558 cfq_merged_requests(request_queue_t *q, struct request *rq,
559 struct request *next)
562 * reposition in fifo if next is older than rq
564 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
565 time_before(next->start_time, rq->start_time))
566 list_move(&rq->queuelist, &next->queuelist);
568 cfq_remove_request(next);
572 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
576 * stop potential idle class queues waiting service
578 del_timer(&cfqd->idle_class_timer);
580 cfqq->slice_start = jiffies;
582 cfqq->slice_left = 0;
583 cfq_clear_cfqq_must_alloc_slice(cfqq);
584 cfq_clear_cfqq_fifo_expire(cfqq);
587 cfqd->active_queue = cfqq;
591 * current cfqq expired its slice (or was too idle), select new one
594 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
597 unsigned long now = jiffies;
599 if (cfq_cfqq_wait_request(cfqq))
600 del_timer(&cfqd->idle_slice_timer);
602 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
603 cfqq->service_last = now;
604 cfq_schedule_dispatch(cfqd);
607 cfq_clear_cfqq_must_dispatch(cfqq);
608 cfq_clear_cfqq_wait_request(cfqq);
611 * store what was left of this slice, if the queue idled out
614 if (time_after(cfqq->slice_end, now))
615 cfqq->slice_left = cfqq->slice_end - now;
617 cfqq->slice_left = 0;
619 if (cfq_cfqq_on_rr(cfqq))
620 cfq_resort_rr_list(cfqq, preempted);
622 if (cfqq == cfqd->active_queue)
623 cfqd->active_queue = NULL;
625 if (cfqd->active_cic) {
626 put_io_context(cfqd->active_cic->ioc);
627 cfqd->active_cic = NULL;
630 cfqd->dispatch_slice = 0;
633 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
635 struct cfq_queue *cfqq = cfqd->active_queue;
638 __cfq_slice_expired(cfqd, cfqq, preempted);
651 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
660 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
661 if (!list_empty(&cfqd->rr_list[p])) {
670 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
671 cfqd->cur_end_prio = 0;
678 if (unlikely(prio == -1))
681 BUG_ON(prio >= CFQ_PRIO_LISTS);
683 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
685 cfqd->cur_prio = prio + 1;
686 if (cfqd->cur_prio > cfqd->cur_end_prio) {
687 cfqd->cur_end_prio = cfqd->cur_prio;
690 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
692 cfqd->cur_end_prio = 0;
698 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
700 struct cfq_queue *cfqq = NULL;
703 * if current list is non-empty, grab first entry. if it is empty,
704 * get next prio level and grab first entry then if any are spliced
706 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
707 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
710 * If no new queues are available, check if the busy list has some
711 * before falling back to idle io.
713 if (!cfqq && !list_empty(&cfqd->busy_rr))
714 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
717 * if we have idle queues and no rt or be queues had pending
718 * requests, either allow immediate service if the grace period
719 * has passed or arm the idle grace timer
721 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
722 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
724 if (time_after_eq(jiffies, end))
725 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
727 mod_timer(&cfqd->idle_class_timer, end);
730 __cfq_set_active_queue(cfqd, cfqq);
734 #define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
736 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
739 struct cfq_io_context *cic;
742 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
743 WARN_ON(cfqq != cfqd->active_queue);
746 * idle is disabled, either manually or by past process history
748 if (!cfqd->cfq_slice_idle)
750 if (!cfq_cfqq_idle_window(cfqq))
753 * task has exited, don't wait
755 cic = cfqd->active_cic;
756 if (!cic || !cic->ioc->task)
759 cfq_mark_cfqq_must_dispatch(cfqq);
760 cfq_mark_cfqq_wait_request(cfqq);
762 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
765 * we don't want to idle for seeks, but we do want to allow
766 * fair distribution of slice time for a process doing back-to-back
767 * seeks. so allow a little bit of time for him to submit a new rq
769 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
770 sl = min(sl, msecs_to_jiffies(2));
772 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
776 static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
778 struct cfq_data *cfqd = q->elevator->elevator_data;
779 struct cfq_queue *cfqq = RQ_CFQQ(rq);
781 cfq_remove_request(rq);
782 cfqq->on_dispatch[rq_is_sync(rq)]++;
783 elv_dispatch_sort(q, rq);
785 rq = list_entry(q->queue_head.prev, struct request, queuelist);
786 cfqd->last_sector = rq->sector + rq->nr_sectors;
790 * return expired entry, or NULL to just start from scratch in rbtree
792 static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
794 struct cfq_data *cfqd = cfqq->cfqd;
797 if (cfq_cfqq_fifo_expire(cfqq))
800 if (!list_empty(&cfqq->fifo)) {
801 int fifo = cfq_cfqq_class_sync(cfqq);
803 rq = rq_entry_fifo(cfqq->fifo.next);
804 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
805 cfq_mark_cfqq_fifo_expire(cfqq);
814 * Scale schedule slice based on io priority. Use the sync time slice only
815 * if a queue is marked sync and has sync io queued. A sync queue with async
816 * io only, should not get full sync slice length.
819 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
821 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
823 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
825 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
829 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
831 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
835 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
837 const int base_rq = cfqd->cfq_slice_async_rq;
839 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
841 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
845 * get next queue for service
847 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
849 unsigned long now = jiffies;
850 struct cfq_queue *cfqq;
852 cfqq = cfqd->active_queue;
859 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
863 * if queue has requests, dispatch one. if not, check if
864 * enough slice is left to wait for one
866 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
868 else if (cfq_cfqq_dispatched(cfqq)) {
871 } else if (cfq_cfqq_class_sync(cfqq)) {
872 if (cfq_arm_slice_timer(cfqd, cfqq))
877 cfq_slice_expired(cfqd, 0);
879 cfqq = cfq_set_active_queue(cfqd);
885 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
890 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
896 * follow expired path, else get first next available
898 if ((rq = cfq_check_fifo(cfqq)) == NULL)
902 * finally, insert request into driver dispatch list
904 cfq_dispatch_insert(cfqd->queue, rq);
906 cfqd->dispatch_slice++;
909 if (!cfqd->active_cic) {
910 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
911 cfqd->active_cic = RQ_CIC(rq);
914 if (RB_EMPTY_ROOT(&cfqq->sort_list))
917 } while (dispatched < max_dispatch);
920 * if slice end isn't set yet, set it.
922 if (!cfqq->slice_end)
923 cfq_set_prio_slice(cfqd, cfqq);
926 * expire an async queue immediately if it has used up its slice. idle
927 * queue always expire after 1 dispatch round.
929 if ((!cfq_cfqq_sync(cfqq) &&
930 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
931 cfq_class_idle(cfqq) ||
932 !cfq_cfqq_idle_window(cfqq))
933 cfq_slice_expired(cfqd, 0);
939 cfq_forced_dispatch_cfqqs(struct list_head *list)
941 struct cfq_queue *cfqq, *next;
945 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
946 while (cfqq->next_rq) {
947 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
950 BUG_ON(!list_empty(&cfqq->fifo));
957 cfq_forced_dispatch(struct cfq_data *cfqd)
959 int i, dispatched = 0;
961 for (i = 0; i < CFQ_PRIO_LISTS; i++)
962 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
964 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
965 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
966 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
968 cfq_slice_expired(cfqd, 0);
970 BUG_ON(cfqd->busy_queues);
976 cfq_dispatch_requests(request_queue_t *q, int force)
978 struct cfq_data *cfqd = q->elevator->elevator_data;
979 struct cfq_queue *cfqq, *prev_cfqq;
982 if (!cfqd->busy_queues)
986 return cfq_forced_dispatch(cfqd);
990 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
994 * Don't repeat dispatch from the previous queue.
996 if (prev_cfqq == cfqq)
999 cfq_clear_cfqq_must_dispatch(cfqq);
1000 cfq_clear_cfqq_wait_request(cfqq);
1001 del_timer(&cfqd->idle_slice_timer);
1003 max_dispatch = cfqd->cfq_quantum;
1004 if (cfq_class_idle(cfqq))
1007 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1010 * If the dispatch cfqq has idling enabled and is still
1011 * the active queue, break out.
1013 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1023 * task holds one reference to the queue, dropped when task exits. each rq
1024 * in-flight on this queue also holds a reference, dropped when rq is freed.
1026 * queue lock must be held here.
1028 static void cfq_put_queue(struct cfq_queue *cfqq)
1030 struct cfq_data *cfqd = cfqq->cfqd;
1032 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1034 if (!atomic_dec_and_test(&cfqq->ref))
1037 BUG_ON(rb_first(&cfqq->sort_list));
1038 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1039 BUG_ON(cfq_cfqq_on_rr(cfqq));
1041 if (unlikely(cfqd->active_queue == cfqq))
1042 __cfq_slice_expired(cfqd, cfqq, 0);
1045 * it's on the empty list and still hashed
1047 list_del(&cfqq->cfq_list);
1048 hlist_del(&cfqq->cfq_hash);
1049 kmem_cache_free(cfq_pool, cfqq);
1052 static inline struct cfq_queue *
1053 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1056 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1057 struct hlist_node *entry;
1058 struct cfq_queue *__cfqq;
1060 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
1061 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1063 if (__cfqq->key == key && (__p == prio || !prio))
1070 static struct cfq_queue *
1071 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1073 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1076 static void cfq_free_io_context(struct io_context *ioc)
1078 struct cfq_io_context *__cic;
1082 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1083 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1084 rb_erase(&__cic->rb_node, &ioc->cic_root);
1085 kmem_cache_free(cfq_ioc_pool, __cic);
1089 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1093 static void cfq_trim(struct io_context *ioc)
1095 ioc->set_ioprio = NULL;
1096 cfq_free_io_context(ioc);
1100 * Called with interrupts disabled
1102 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1104 struct cfq_data *cfqd = cic->key;
1112 WARN_ON(!irqs_disabled());
1114 spin_lock(q->queue_lock);
1116 if (cic->cfqq[ASYNC]) {
1117 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1118 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1119 cfq_put_queue(cic->cfqq[ASYNC]);
1120 cic->cfqq[ASYNC] = NULL;
1123 if (cic->cfqq[SYNC]) {
1124 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1125 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1126 cfq_put_queue(cic->cfqq[SYNC]);
1127 cic->cfqq[SYNC] = NULL;
1131 list_del_init(&cic->queue_list);
1132 spin_unlock(q->queue_lock);
1135 static void cfq_exit_io_context(struct io_context *ioc)
1137 struct cfq_io_context *__cic;
1138 unsigned long flags;
1142 * put the reference this task is holding to the various queues
1144 spin_lock_irqsave(&cfq_exit_lock, flags);
1146 n = rb_first(&ioc->cic_root);
1148 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1150 cfq_exit_single_io_context(__cic);
1154 spin_unlock_irqrestore(&cfq_exit_lock, flags);
1157 static struct cfq_io_context *
1158 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1160 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1163 memset(cic, 0, sizeof(*cic));
1164 cic->last_end_request = jiffies;
1165 INIT_LIST_HEAD(&cic->queue_list);
1166 cic->dtor = cfq_free_io_context;
1167 cic->exit = cfq_exit_io_context;
1168 atomic_inc(&ioc_count);
1174 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1176 struct task_struct *tsk = current;
1179 if (!cfq_cfqq_prio_changed(cfqq))
1182 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1183 switch (ioprio_class) {
1185 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1186 case IOPRIO_CLASS_NONE:
1188 * no prio set, place us in the middle of the BE classes
1190 cfqq->ioprio = task_nice_ioprio(tsk);
1191 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1193 case IOPRIO_CLASS_RT:
1194 cfqq->ioprio = task_ioprio(tsk);
1195 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1197 case IOPRIO_CLASS_BE:
1198 cfqq->ioprio = task_ioprio(tsk);
1199 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1201 case IOPRIO_CLASS_IDLE:
1202 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1204 cfq_clear_cfqq_idle_window(cfqq);
1209 * keep track of original prio settings in case we have to temporarily
1210 * elevate the priority of this queue
1212 cfqq->org_ioprio = cfqq->ioprio;
1213 cfqq->org_ioprio_class = cfqq->ioprio_class;
1215 if (cfq_cfqq_on_rr(cfqq))
1216 cfq_resort_rr_list(cfqq, 0);
1218 cfq_clear_cfqq_prio_changed(cfqq);
1221 static inline void changed_ioprio(struct cfq_io_context *cic)
1223 struct cfq_data *cfqd = cic->key;
1224 struct cfq_queue *cfqq;
1226 if (unlikely(!cfqd))
1229 spin_lock(cfqd->queue->queue_lock);
1231 cfqq = cic->cfqq[ASYNC];
1233 struct cfq_queue *new_cfqq;
1234 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1237 cic->cfqq[ASYNC] = new_cfqq;
1238 cfq_put_queue(cfqq);
1242 cfqq = cic->cfqq[SYNC];
1244 cfq_mark_cfqq_prio_changed(cfqq);
1246 spin_unlock(cfqd->queue->queue_lock);
1250 * callback from sys_ioprio_set, irqs are disabled
1252 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1254 struct cfq_io_context *cic;
1257 spin_lock(&cfq_exit_lock);
1259 n = rb_first(&ioc->cic_root);
1261 cic = rb_entry(n, struct cfq_io_context, rb_node);
1263 changed_ioprio(cic);
1267 spin_unlock(&cfq_exit_lock);
1272 static struct cfq_queue *
1273 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
1276 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1277 struct cfq_queue *cfqq, *new_cfqq = NULL;
1278 unsigned short ioprio;
1281 ioprio = tsk->ioprio;
1282 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1288 } else if (gfp_mask & __GFP_WAIT) {
1289 spin_unlock_irq(cfqd->queue->queue_lock);
1290 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1291 spin_lock_irq(cfqd->queue->queue_lock);
1294 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1299 memset(cfqq, 0, sizeof(*cfqq));
1301 INIT_HLIST_NODE(&cfqq->cfq_hash);
1302 INIT_LIST_HEAD(&cfqq->cfq_list);
1303 INIT_LIST_HEAD(&cfqq->fifo);
1306 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1307 atomic_set(&cfqq->ref, 0);
1309 cfqq->service_last = 0;
1311 * set ->slice_left to allow preemption for a new process
1313 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1314 cfq_mark_cfqq_idle_window(cfqq);
1315 cfq_mark_cfqq_prio_changed(cfqq);
1316 cfq_init_prio_data(cfqq);
1320 kmem_cache_free(cfq_pool, new_cfqq);
1322 atomic_inc(&cfqq->ref);
1324 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1329 cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1331 spin_lock(&cfq_exit_lock);
1332 rb_erase(&cic->rb_node, &ioc->cic_root);
1333 list_del_init(&cic->queue_list);
1334 spin_unlock(&cfq_exit_lock);
1335 kmem_cache_free(cfq_ioc_pool, cic);
1336 atomic_dec(&ioc_count);
1339 static struct cfq_io_context *
1340 cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1343 struct cfq_io_context *cic;
1344 void *k, *key = cfqd;
1347 n = ioc->cic_root.rb_node;
1349 cic = rb_entry(n, struct cfq_io_context, rb_node);
1350 /* ->key must be copied to avoid race with cfq_exit_queue() */
1353 cfq_drop_dead_cic(ioc, cic);
1369 cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1370 struct cfq_io_context *cic)
1373 struct rb_node *parent;
1374 struct cfq_io_context *__cic;
1380 ioc->set_ioprio = cfq_ioc_set_ioprio;
1383 p = &ioc->cic_root.rb_node;
1386 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1387 /* ->key must be copied to avoid race with cfq_exit_queue() */
1390 cfq_drop_dead_cic(ioc, __cic);
1396 else if (cic->key > k)
1397 p = &(*p)->rb_right;
1402 spin_lock(&cfq_exit_lock);
1403 rb_link_node(&cic->rb_node, parent, p);
1404 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1405 list_add(&cic->queue_list, &cfqd->cic_list);
1406 spin_unlock(&cfq_exit_lock);
1410 * Setup general io context and cfq io context. There can be several cfq
1411 * io contexts per general io context, if this process is doing io to more
1412 * than one device managed by cfq.
1414 static struct cfq_io_context *
1415 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1417 struct io_context *ioc = NULL;
1418 struct cfq_io_context *cic;
1420 might_sleep_if(gfp_mask & __GFP_WAIT);
1422 ioc = get_io_context(gfp_mask);
1426 cic = cfq_cic_rb_lookup(cfqd, ioc);
1430 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1434 cfq_cic_link(cfqd, ioc, cic);
1438 put_io_context(ioc);
1443 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1445 unsigned long elapsed, ttime;
1448 * if this context already has stuff queued, thinktime is from
1449 * last queue not last end
1452 if (time_after(cic->last_end_request, cic->last_queue))
1453 elapsed = jiffies - cic->last_end_request;
1455 elapsed = jiffies - cic->last_queue;
1457 elapsed = jiffies - cic->last_end_request;
1460 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1462 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1463 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1464 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1468 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1474 if (cic->last_request_pos < rq->sector)
1475 sdist = rq->sector - cic->last_request_pos;
1477 sdist = cic->last_request_pos - rq->sector;
1480 * Don't allow the seek distance to get too large from the
1481 * odd fragment, pagein, etc
1483 if (cic->seek_samples <= 60) /* second&third seek */
1484 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1486 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1488 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1489 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1490 total = cic->seek_total + (cic->seek_samples/2);
1491 do_div(total, cic->seek_samples);
1492 cic->seek_mean = (sector_t)total;
1496 * Disable idle window if the process thinks too long or seeks so much that
1500 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1501 struct cfq_io_context *cic)
1503 int enable_idle = cfq_cfqq_idle_window(cfqq);
1505 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1506 (cfqd->hw_tag && CIC_SEEKY(cic)))
1508 else if (sample_valid(cic->ttime_samples)) {
1509 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1516 cfq_mark_cfqq_idle_window(cfqq);
1518 cfq_clear_cfqq_idle_window(cfqq);
1523 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1524 * no or if we aren't sure, a 1 will cause a preempt.
1527 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1530 struct cfq_queue *cfqq = cfqd->active_queue;
1532 if (cfq_class_idle(new_cfqq))
1538 if (cfq_class_idle(cfqq))
1540 if (!cfq_cfqq_wait_request(new_cfqq))
1543 * if it doesn't have slice left, forget it
1545 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1547 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
1554 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1555 * let it have half of its nominal slice.
1557 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1559 struct cfq_queue *__cfqq, *next;
1561 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1562 cfq_resort_rr_list(__cfqq, 1);
1564 if (!cfqq->slice_left)
1565 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1567 cfqq->slice_end = cfqq->slice_left + jiffies;
1568 cfq_slice_expired(cfqd, 1);
1569 __cfq_set_active_queue(cfqd, cfqq);
1573 * should really be a ll_rw_blk.c helper
1575 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1577 request_queue_t *q = cfqd->queue;
1579 if (!blk_queue_plugged(q))
1582 __generic_unplug_device(q);
1586 * Called when a new fs request (rq) is added (to cfqq). Check if there's
1587 * something we should do about it
1590 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1593 struct cfq_io_context *cic = RQ_CIC(rq);
1596 * check if this request is a better next-serve candidate)) {
1598 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
1599 BUG_ON(!cfqq->next_rq);
1602 * we never wait for an async request and we don't allow preemption
1603 * of an async request. so just return early
1605 if (!rq_is_sync(rq)) {
1607 * sync process issued an async request, if it's waiting
1608 * then expire it and kick rq handling.
1610 if (cic == cfqd->active_cic &&
1611 del_timer(&cfqd->idle_slice_timer)) {
1612 cfq_slice_expired(cfqd, 0);
1613 cfq_start_queueing(cfqd, cfqq);
1618 cfq_update_io_thinktime(cfqd, cic);
1619 cfq_update_io_seektime(cfqd, cic, rq);
1620 cfq_update_idle_window(cfqd, cfqq, cic);
1622 cic->last_queue = jiffies;
1623 cic->last_request_pos = rq->sector + rq->nr_sectors;
1625 if (cfqq == cfqd->active_queue) {
1627 * if we are waiting for a request for this queue, let it rip
1628 * immediately and flag that we must not expire this queue
1631 if (cfq_cfqq_wait_request(cfqq)) {
1632 cfq_mark_cfqq_must_dispatch(cfqq);
1633 del_timer(&cfqd->idle_slice_timer);
1634 cfq_start_queueing(cfqd, cfqq);
1636 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
1638 * not the active queue - expire current slice if it is
1639 * idle and has expired it's mean thinktime or this new queue
1640 * has some old slice time left and is of higher priority
1642 cfq_preempt_queue(cfqd, cfqq);
1643 cfq_mark_cfqq_must_dispatch(cfqq);
1644 cfq_start_queueing(cfqd, cfqq);
1648 static void cfq_insert_request(request_queue_t *q, struct request *rq)
1650 struct cfq_data *cfqd = q->elevator->elevator_data;
1651 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1653 cfq_init_prio_data(cfqq);
1657 if (!cfq_cfqq_on_rr(cfqq))
1658 cfq_add_cfqq_rr(cfqd, cfqq);
1660 list_add_tail(&rq->queuelist, &cfqq->fifo);
1662 cfq_rq_enqueued(cfqd, cfqq, rq);
1665 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1667 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1668 struct cfq_data *cfqd = cfqq->cfqd;
1669 const int sync = rq_is_sync(rq);
1674 WARN_ON(!cfqd->rq_in_driver);
1675 WARN_ON(!cfqq->on_dispatch[sync]);
1676 cfqd->rq_in_driver--;
1677 cfqq->on_dispatch[sync]--;
1679 if (!cfq_class_idle(cfqq))
1680 cfqd->last_end_request = now;
1682 if (!cfq_cfqq_dispatched(cfqq)) {
1683 if (cfq_cfqq_on_rr(cfqq)) {
1684 cfqq->service_last = now;
1685 cfq_resort_rr_list(cfqq, 0);
1690 RQ_CIC(rq)->last_end_request = now;
1693 * If this is the active queue, check if it needs to be expired,
1694 * or if we want to idle in case it has no pending requests.
1696 if (cfqd->active_queue == cfqq) {
1697 if (time_after(now, cfqq->slice_end))
1698 cfq_slice_expired(cfqd, 0);
1699 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1700 if (!cfq_arm_slice_timer(cfqd, cfqq))
1701 cfq_schedule_dispatch(cfqd);
1707 * we temporarily boost lower priority queues if they are holding fs exclusive
1708 * resources. they are boosted to normal prio (CLASS_BE/4)
1710 static void cfq_prio_boost(struct cfq_queue *cfqq)
1712 const int ioprio_class = cfqq->ioprio_class;
1713 const int ioprio = cfqq->ioprio;
1715 if (has_fs_excl()) {
1717 * boost idle prio on transactions that would lock out other
1718 * users of the filesystem
1720 if (cfq_class_idle(cfqq))
1721 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1722 if (cfqq->ioprio > IOPRIO_NORM)
1723 cfqq->ioprio = IOPRIO_NORM;
1726 * check if we need to unboost the queue
1728 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1729 cfqq->ioprio_class = cfqq->org_ioprio_class;
1730 if (cfqq->ioprio != cfqq->org_ioprio)
1731 cfqq->ioprio = cfqq->org_ioprio;
1735 * refile between round-robin lists if we moved the priority class
1737 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1738 cfq_cfqq_on_rr(cfqq))
1739 cfq_resort_rr_list(cfqq, 0);
1743 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1744 struct task_struct *task, int rw)
1746 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1747 !cfq_cfqq_must_alloc_slice(cfqq)) {
1748 cfq_mark_cfqq_must_alloc_slice(cfqq);
1749 return ELV_MQUEUE_MUST;
1752 return ELV_MQUEUE_MAY;
1755 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1757 struct cfq_data *cfqd = q->elevator->elevator_data;
1758 struct task_struct *tsk = current;
1759 struct cfq_queue *cfqq;
1762 * don't force setup of a queue from here, as a call to may_queue
1763 * does not necessarily imply that a request actually will be queued.
1764 * so just lookup a possibly existing queue, or return 'may queue'
1767 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
1769 cfq_init_prio_data(cfqq);
1770 cfq_prio_boost(cfqq);
1772 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1775 return ELV_MQUEUE_MAY;
1778 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1780 struct cfq_data *cfqd = q->elevator->elevator_data;
1782 if (unlikely(cfqd->rq_starved)) {
1783 struct request_list *rl = &q->rq;
1786 if (waitqueue_active(&rl->wait[READ]))
1787 wake_up(&rl->wait[READ]);
1788 if (waitqueue_active(&rl->wait[WRITE]))
1789 wake_up(&rl->wait[WRITE]);
1794 * queue lock held here
1796 static void cfq_put_request(request_queue_t *q, struct request *rq)
1798 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1801 const int rw = rq_data_dir(rq);
1803 BUG_ON(!cfqq->allocated[rw]);
1804 cfqq->allocated[rw]--;
1806 put_io_context(RQ_CIC(rq)->ioc);
1808 rq->elevator_private = NULL;
1809 rq->elevator_private2 = NULL;
1811 cfq_check_waiters(q, cfqq);
1812 cfq_put_queue(cfqq);
1817 * Allocate cfq data structures associated with this request.
1820 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1823 struct cfq_data *cfqd = q->elevator->elevator_data;
1824 struct task_struct *tsk = current;
1825 struct cfq_io_context *cic;
1826 const int rw = rq_data_dir(rq);
1827 pid_t key = cfq_queue_pid(tsk, rw);
1828 struct cfq_queue *cfqq;
1829 unsigned long flags;
1830 int is_sync = key != CFQ_KEY_ASYNC;
1832 might_sleep_if(gfp_mask & __GFP_WAIT);
1834 cic = cfq_get_io_context(cfqd, gfp_mask);
1836 spin_lock_irqsave(q->queue_lock, flags);
1841 if (!cic->cfqq[is_sync]) {
1842 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
1846 cic->cfqq[is_sync] = cfqq;
1848 cfqq = cic->cfqq[is_sync];
1850 cfqq->allocated[rw]++;
1851 cfq_clear_cfqq_must_alloc(cfqq);
1852 cfqd->rq_starved = 0;
1853 atomic_inc(&cfqq->ref);
1855 spin_unlock_irqrestore(q->queue_lock, flags);
1857 rq->elevator_private = cic;
1858 rq->elevator_private2 = cfqq;
1863 put_io_context(cic->ioc);
1865 * mark us rq allocation starved. we need to kickstart the process
1866 * ourselves if there are no pending requests that can do it for us.
1867 * that would be an extremely rare OOM situation
1869 cfqd->rq_starved = 1;
1870 cfq_schedule_dispatch(cfqd);
1871 spin_unlock_irqrestore(q->queue_lock, flags);
1875 static void cfq_kick_queue(void *data)
1877 request_queue_t *q = data;
1878 struct cfq_data *cfqd = q->elevator->elevator_data;
1879 unsigned long flags;
1881 spin_lock_irqsave(q->queue_lock, flags);
1883 if (cfqd->rq_starved) {
1884 struct request_list *rl = &q->rq;
1887 * we aren't guaranteed to get a request after this, but we
1888 * have to be opportunistic
1891 if (waitqueue_active(&rl->wait[READ]))
1892 wake_up(&rl->wait[READ]);
1893 if (waitqueue_active(&rl->wait[WRITE]))
1894 wake_up(&rl->wait[WRITE]);
1899 spin_unlock_irqrestore(q->queue_lock, flags);
1903 * Timer running if the active_queue is currently idling inside its time slice
1905 static void cfq_idle_slice_timer(unsigned long data)
1907 struct cfq_data *cfqd = (struct cfq_data *) data;
1908 struct cfq_queue *cfqq;
1909 unsigned long flags;
1911 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1913 if ((cfqq = cfqd->active_queue) != NULL) {
1914 unsigned long now = jiffies;
1919 if (time_after(now, cfqq->slice_end))
1923 * only expire and reinvoke request handler, if there are
1924 * other queues with pending requests
1926 if (!cfqd->busy_queues)
1930 * not expired and it has a request pending, let it dispatch
1932 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
1933 cfq_mark_cfqq_must_dispatch(cfqq);
1938 cfq_slice_expired(cfqd, 0);
1940 cfq_schedule_dispatch(cfqd);
1942 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1946 * Timer running if an idle class queue is waiting for service
1948 static void cfq_idle_class_timer(unsigned long data)
1950 struct cfq_data *cfqd = (struct cfq_data *) data;
1951 unsigned long flags, end;
1953 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1956 * race with a non-idle queue, reset timer
1958 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
1959 if (!time_after_eq(jiffies, end))
1960 mod_timer(&cfqd->idle_class_timer, end);
1962 cfq_schedule_dispatch(cfqd);
1964 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1967 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1969 del_timer_sync(&cfqd->idle_slice_timer);
1970 del_timer_sync(&cfqd->idle_class_timer);
1971 blk_sync_queue(cfqd->queue);
1974 static void cfq_exit_queue(elevator_t *e)
1976 struct cfq_data *cfqd = e->elevator_data;
1977 request_queue_t *q = cfqd->queue;
1979 cfq_shutdown_timer_wq(cfqd);
1981 spin_lock(&cfq_exit_lock);
1982 spin_lock_irq(q->queue_lock);
1984 if (cfqd->active_queue)
1985 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
1987 while (!list_empty(&cfqd->cic_list)) {
1988 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1989 struct cfq_io_context,
1991 if (cic->cfqq[ASYNC]) {
1992 cfq_put_queue(cic->cfqq[ASYNC]);
1993 cic->cfqq[ASYNC] = NULL;
1995 if (cic->cfqq[SYNC]) {
1996 cfq_put_queue(cic->cfqq[SYNC]);
1997 cic->cfqq[SYNC] = NULL;
2000 list_del_init(&cic->queue_list);
2003 spin_unlock_irq(q->queue_lock);
2004 spin_unlock(&cfq_exit_lock);
2006 cfq_shutdown_timer_wq(cfqd);
2008 kfree(cfqd->cfq_hash);
2012 static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
2014 struct cfq_data *cfqd;
2017 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2021 memset(cfqd, 0, sizeof(*cfqd));
2023 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2024 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2026 INIT_LIST_HEAD(&cfqd->busy_rr);
2027 INIT_LIST_HEAD(&cfqd->cur_rr);
2028 INIT_LIST_HEAD(&cfqd->idle_rr);
2029 INIT_LIST_HEAD(&cfqd->empty_list);
2030 INIT_LIST_HEAD(&cfqd->cic_list);
2032 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2033 if (!cfqd->cfq_hash)
2036 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2037 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2041 init_timer(&cfqd->idle_slice_timer);
2042 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2043 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2045 init_timer(&cfqd->idle_class_timer);
2046 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2047 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2049 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2051 cfqd->cfq_queued = cfq_queued;
2052 cfqd->cfq_quantum = cfq_quantum;
2053 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2054 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2055 cfqd->cfq_back_max = cfq_back_max;
2056 cfqd->cfq_back_penalty = cfq_back_penalty;
2057 cfqd->cfq_slice[0] = cfq_slice_async;
2058 cfqd->cfq_slice[1] = cfq_slice_sync;
2059 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2060 cfqd->cfq_slice_idle = cfq_slice_idle;
2068 static void cfq_slab_kill(void)
2071 kmem_cache_destroy(cfq_pool);
2073 kmem_cache_destroy(cfq_ioc_pool);
2076 static int __init cfq_slab_setup(void)
2078 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2083 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2084 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2095 * sysfs parts below -->
2099 cfq_var_show(unsigned int var, char *page)
2101 return sprintf(page, "%d\n", var);
2105 cfq_var_store(unsigned int *var, const char *page, size_t count)
2107 char *p = (char *) page;
2109 *var = simple_strtoul(p, &p, 10);
2113 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2114 static ssize_t __FUNC(elevator_t *e, char *page) \
2116 struct cfq_data *cfqd = e->elevator_data; \
2117 unsigned int __data = __VAR; \
2119 __data = jiffies_to_msecs(__data); \
2120 return cfq_var_show(__data, (page)); \
2122 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2123 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2124 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2125 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2126 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2127 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2128 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2129 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2130 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2131 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2132 #undef SHOW_FUNCTION
2134 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2135 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2137 struct cfq_data *cfqd = e->elevator_data; \
2138 unsigned int __data; \
2139 int ret = cfq_var_store(&__data, (page), count); \
2140 if (__data < (MIN)) \
2142 else if (__data > (MAX)) \
2145 *(__PTR) = msecs_to_jiffies(__data); \
2147 *(__PTR) = __data; \
2150 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2151 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2152 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2153 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2154 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2155 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2156 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2157 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2158 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2159 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2160 #undef STORE_FUNCTION
2162 #define CFQ_ATTR(name) \
2163 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2165 static struct elv_fs_entry cfq_attrs[] = {
2168 CFQ_ATTR(fifo_expire_sync),
2169 CFQ_ATTR(fifo_expire_async),
2170 CFQ_ATTR(back_seek_max),
2171 CFQ_ATTR(back_seek_penalty),
2172 CFQ_ATTR(slice_sync),
2173 CFQ_ATTR(slice_async),
2174 CFQ_ATTR(slice_async_rq),
2175 CFQ_ATTR(slice_idle),
2179 static struct elevator_type iosched_cfq = {
2181 .elevator_merge_fn = cfq_merge,
2182 .elevator_merged_fn = cfq_merged_request,
2183 .elevator_merge_req_fn = cfq_merged_requests,
2184 .elevator_dispatch_fn = cfq_dispatch_requests,
2185 .elevator_add_req_fn = cfq_insert_request,
2186 .elevator_activate_req_fn = cfq_activate_request,
2187 .elevator_deactivate_req_fn = cfq_deactivate_request,
2188 .elevator_queue_empty_fn = cfq_queue_empty,
2189 .elevator_completed_req_fn = cfq_completed_request,
2190 .elevator_former_req_fn = elv_rb_former_request,
2191 .elevator_latter_req_fn = elv_rb_latter_request,
2192 .elevator_set_req_fn = cfq_set_request,
2193 .elevator_put_req_fn = cfq_put_request,
2194 .elevator_may_queue_fn = cfq_may_queue,
2195 .elevator_init_fn = cfq_init_queue,
2196 .elevator_exit_fn = cfq_exit_queue,
2199 .elevator_attrs = cfq_attrs,
2200 .elevator_name = "cfq",
2201 .elevator_owner = THIS_MODULE,
2204 static int __init cfq_init(void)
2209 * could be 0 on HZ < 1000 setups
2211 if (!cfq_slice_async)
2212 cfq_slice_async = 1;
2213 if (!cfq_slice_idle)
2216 if (cfq_slab_setup())
2219 ret = elv_register(&iosched_cfq);
2226 static void __exit cfq_exit(void)
2228 DECLARE_COMPLETION(all_gone);
2229 elv_unregister(&iosched_cfq);
2230 ioc_gone = &all_gone;
2231 /* ioc_gone's update must be visible before reading ioc_count */
2233 if (atomic_read(&ioc_count))
2234 wait_for_completion(ioc_gone);
2239 module_init(cfq_init);
2240 module_exit(cfq_exit);
2242 MODULE_AUTHOR("Jens Axboe");
2243 MODULE_LICENSE("GPL");
2244 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");