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@kernel.dk>
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_fifo_expire[2] = { HZ / 4, HZ / 8 };
21 static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22 static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
24 static const int cfq_slice_sync = HZ / 10;
25 static int cfq_slice_async = HZ / 25;
26 static const int cfq_slice_async_rq = 2;
27 static int cfq_slice_idle = HZ / 125;
30 * grace period before allowing idle class to get disk access
32 #define CFQ_IDLE_GRACE (HZ / 10)
35 * below this threshold, we consider thinktime immediate
37 #define CFQ_MIN_TT (2)
39 #define CFQ_SLICE_SCALE (5)
41 #define CFQ_KEY_ASYNC (0)
44 * for the hash of cfqq inside the cfqd
46 #define CFQ_QHASH_SHIFT 6
47 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
48 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
50 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
52 #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
53 #define RQ_CFQQ(rq) ((rq)->elevator_private2)
55 static struct kmem_cache *cfq_pool;
56 static struct kmem_cache *cfq_ioc_pool;
58 static DEFINE_PER_CPU(unsigned long, ioc_count);
59 static struct completion *ioc_gone;
61 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
62 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
63 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
68 #define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
70 #define sample_valid(samples) ((samples) > 80)
73 * Most of our rbtree usage is for sorting with min extraction, so
74 * if we cache the leftmost node we don't have to walk down the tree
75 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
76 * move this into the elevator for the rq sorting as well.
82 #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
85 * Per block device queue structure
88 request_queue_t *queue;
91 * rr list of queues with requests and the count of them
93 struct cfq_rb_root service_tree;
94 struct list_head cur_rr;
95 unsigned int busy_queues;
100 struct hlist_head *cfq_hash;
106 * idle window management
108 struct timer_list idle_slice_timer;
109 struct work_struct unplug_work;
111 struct cfq_queue *active_queue;
112 struct cfq_io_context *active_cic;
113 unsigned int dispatch_slice;
115 struct timer_list idle_class_timer;
117 sector_t last_position;
118 unsigned long last_end_request;
121 * tunables, see top of file
123 unsigned int cfq_quantum;
124 unsigned int cfq_fifo_expire[2];
125 unsigned int cfq_back_penalty;
126 unsigned int cfq_back_max;
127 unsigned int cfq_slice[2];
128 unsigned int cfq_slice_async_rq;
129 unsigned int cfq_slice_idle;
131 struct list_head cic_list;
133 sector_t new_seek_mean;
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 /* member of the rr/busy/cur/idle cfqd list */
150 struct list_head cfq_list;
151 /* service_tree member */
152 struct rb_node rb_node;
153 /* service_tree key */
154 unsigned long rb_key;
155 /* sorted list of pending requests */
156 struct rb_root sort_list;
157 /* if fifo isn't expired, next request to serve */
158 struct request *next_rq;
159 /* requests queued in sort_list */
161 /* currently allocated requests */
163 /* pending metadata requests */
165 /* fifo list of requests in sort_list */
166 struct list_head fifo;
168 unsigned long slice_end;
171 /* number of requests that are on the dispatch list or inside driver */
174 /* io prio of this group */
175 unsigned short ioprio, org_ioprio;
176 unsigned short ioprio_class, org_ioprio_class;
178 /* various state flags, see below */
181 sector_t last_request_pos;
184 enum cfqq_state_flags {
185 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
186 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
187 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
188 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
189 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
190 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
191 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
192 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
193 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
194 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
197 #define CFQ_CFQQ_FNS(name) \
198 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
200 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
202 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
204 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
206 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
208 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
212 CFQ_CFQQ_FNS(wait_request);
213 CFQ_CFQQ_FNS(must_alloc);
214 CFQ_CFQQ_FNS(must_alloc_slice);
215 CFQ_CFQQ_FNS(must_dispatch);
216 CFQ_CFQQ_FNS(fifo_expire);
217 CFQ_CFQQ_FNS(idle_window);
218 CFQ_CFQQ_FNS(prio_changed);
219 CFQ_CFQQ_FNS(queue_new);
220 CFQ_CFQQ_FNS(slice_new);
223 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
224 static void cfq_dispatch_insert(request_queue_t *, struct request *);
225 static struct cfq_queue *cfq_get_queue(struct cfq_data *, unsigned int, struct task_struct *, gfp_t);
228 * scheduler run of queue, if there are requests pending and no one in the
229 * driver that will restart queueing
231 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
233 if (cfqd->busy_queues)
234 kblockd_schedule_work(&cfqd->unplug_work);
237 static int cfq_queue_empty(request_queue_t *q)
239 struct cfq_data *cfqd = q->elevator->elevator_data;
241 return !cfqd->busy_queues;
244 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
247 * Use the per-process queue, for read requests and syncronous writes
249 if (!(rw & REQ_RW) || is_sync)
252 return CFQ_KEY_ASYNC;
256 * Scale schedule slice based on io priority. Use the sync time slice only
257 * if a queue is marked sync and has sync io queued. A sync queue with async
258 * io only, should not get full sync slice length.
260 static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
263 const int base_slice = cfqd->cfq_slice[sync];
265 WARN_ON(prio >= IOPRIO_BE_NR);
267 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
271 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
273 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
277 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
279 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
283 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
284 * isn't valid until the first request from the dispatch is activated
285 * and the slice time set.
287 static inline int cfq_slice_used(struct cfq_queue *cfqq)
289 if (cfq_cfqq_slice_new(cfqq))
291 if (time_before(jiffies, cfqq->slice_end))
298 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
299 * We choose the request that is closest to the head right now. Distance
300 * behind the head is penalized and only allowed to a certain extent.
302 static struct request *
303 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
305 sector_t last, s1, s2, d1 = 0, d2 = 0;
306 unsigned long back_max;
307 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
308 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
309 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
311 if (rq1 == NULL || rq1 == rq2)
316 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
318 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
320 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
322 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
328 last = cfqd->last_position;
331 * by definition, 1KiB is 2 sectors
333 back_max = cfqd->cfq_back_max * 2;
336 * Strict one way elevator _except_ in the case where we allow
337 * short backward seeks which are biased as twice the cost of a
338 * similar forward seek.
342 else if (s1 + back_max >= last)
343 d1 = (last - s1) * cfqd->cfq_back_penalty;
345 wrap |= CFQ_RQ1_WRAP;
349 else if (s2 + back_max >= last)
350 d2 = (last - s2) * cfqd->cfq_back_penalty;
352 wrap |= CFQ_RQ2_WRAP;
354 /* Found required data */
357 * By doing switch() on the bit mask "wrap" we avoid having to
358 * check two variables for all permutations: --> faster!
361 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
377 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
380 * Since both rqs are wrapped,
381 * start with the one that's further behind head
382 * (--> only *one* back seek required),
383 * since back seek takes more time than forward.
393 * The below is leftmost cache rbtree addon
395 static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
398 root->left = rb_first(&root->rb);
403 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
408 rb_erase(n, &root->rb);
413 * would be nice to take fifo expire time into account as well
415 static struct request *
416 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
417 struct request *last)
419 struct rb_node *rbnext = rb_next(&last->rb_node);
420 struct rb_node *rbprev = rb_prev(&last->rb_node);
421 struct request *next = NULL, *prev = NULL;
423 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
426 prev = rb_entry_rq(rbprev);
429 next = rb_entry_rq(rbnext);
431 rbnext = rb_first(&cfqq->sort_list);
432 if (rbnext && rbnext != &last->rb_node)
433 next = rb_entry_rq(rbnext);
436 return cfq_choose_req(cfqd, next, prev);
439 static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
440 struct cfq_queue *cfqq)
443 * just an approximation, should be ok.
445 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
446 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
450 * The cfqd->service_tree holds all pending cfq_queue's that have
451 * requests waiting to be processed. It is sorted in the order that
452 * we will service the queues.
454 static void cfq_service_tree_add(struct cfq_data *cfqd,
455 struct cfq_queue *cfqq)
457 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
458 struct rb_node *parent = NULL;
459 unsigned long rb_key;
462 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
463 rb_key += cfqq->slice_resid;
464 cfqq->slice_resid = 0;
466 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
468 * same position, nothing more to do
470 if (rb_key == cfqq->rb_key)
473 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
478 struct cfq_queue *__cfqq;
482 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
485 * sort RT queues first, we always want to give
486 * preference to them. IDLE queues goes to the back.
487 * after that, sort on the next service time.
489 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
491 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
493 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
495 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
497 else if (rb_key < __cfqq->rb_key)
502 if (n == &(*p)->rb_right)
509 cfqd->service_tree.left = &cfqq->rb_node;
511 cfqq->rb_key = rb_key;
512 rb_link_node(&cfqq->rb_node, parent, p);
513 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
517 * Update cfqq's position in the service tree.
519 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
522 * Resorting requires the cfqq to be on the RR list already.
524 if (cfq_cfqq_on_rr(cfqq))
525 cfq_service_tree_add(cfqq->cfqd, cfqq);
529 * add to busy list of queues for service, trying to be fair in ordering
530 * the pending list according to last request service
533 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
535 BUG_ON(cfq_cfqq_on_rr(cfqq));
536 cfq_mark_cfqq_on_rr(cfqq);
539 cfq_resort_rr_list(cfqq, 0);
543 * Called when the cfqq no longer has requests pending, remove it from
547 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
549 BUG_ON(!cfq_cfqq_on_rr(cfqq));
550 cfq_clear_cfqq_on_rr(cfqq);
551 list_del_init(&cfqq->cfq_list);
553 if (!RB_EMPTY_NODE(&cfqq->rb_node))
554 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
556 BUG_ON(!cfqd->busy_queues);
561 * rb tree support functions
563 static inline void cfq_del_rq_rb(struct request *rq)
565 struct cfq_queue *cfqq = RQ_CFQQ(rq);
566 struct cfq_data *cfqd = cfqq->cfqd;
567 const int sync = rq_is_sync(rq);
569 BUG_ON(!cfqq->queued[sync]);
570 cfqq->queued[sync]--;
572 elv_rb_del(&cfqq->sort_list, rq);
574 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
575 cfq_del_cfqq_rr(cfqd, cfqq);
578 static void cfq_add_rq_rb(struct request *rq)
580 struct cfq_queue *cfqq = RQ_CFQQ(rq);
581 struct cfq_data *cfqd = cfqq->cfqd;
582 struct request *__alias;
584 cfqq->queued[rq_is_sync(rq)]++;
587 * looks a little odd, but the first insert might return an alias.
588 * if that happens, put the alias on the dispatch list
590 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
591 cfq_dispatch_insert(cfqd->queue, __alias);
593 if (!cfq_cfqq_on_rr(cfqq))
594 cfq_add_cfqq_rr(cfqd, cfqq);
597 * check if this request is a better next-serve candidate
599 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
600 BUG_ON(!cfqq->next_rq);
604 cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
606 elv_rb_del(&cfqq->sort_list, rq);
607 cfqq->queued[rq_is_sync(rq)]--;
611 static struct request *
612 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
614 struct task_struct *tsk = current;
615 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
616 struct cfq_queue *cfqq;
618 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
620 sector_t sector = bio->bi_sector + bio_sectors(bio);
622 return elv_rb_find(&cfqq->sort_list, sector);
628 static void cfq_activate_request(request_queue_t *q, struct request *rq)
630 struct cfq_data *cfqd = q->elevator->elevator_data;
632 cfqd->rq_in_driver++;
635 * If the depth is larger 1, it really could be queueing. But lets
636 * make the mark a little higher - idling could still be good for
637 * low queueing, and a low queueing number could also just indicate
638 * a SCSI mid layer like behaviour where limit+1 is often seen.
640 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
643 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
646 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
648 struct cfq_data *cfqd = q->elevator->elevator_data;
650 WARN_ON(!cfqd->rq_in_driver);
651 cfqd->rq_in_driver--;
654 static void cfq_remove_request(struct request *rq)
656 struct cfq_queue *cfqq = RQ_CFQQ(rq);
658 if (cfqq->next_rq == rq)
659 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
661 list_del_init(&rq->queuelist);
664 if (rq_is_meta(rq)) {
665 WARN_ON(!cfqq->meta_pending);
666 cfqq->meta_pending--;
670 static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
672 struct cfq_data *cfqd = q->elevator->elevator_data;
673 struct request *__rq;
675 __rq = cfq_find_rq_fmerge(cfqd, bio);
676 if (__rq && elv_rq_merge_ok(__rq, bio)) {
678 return ELEVATOR_FRONT_MERGE;
681 return ELEVATOR_NO_MERGE;
684 static void cfq_merged_request(request_queue_t *q, struct request *req,
687 if (type == ELEVATOR_FRONT_MERGE) {
688 struct cfq_queue *cfqq = RQ_CFQQ(req);
690 cfq_reposition_rq_rb(cfqq, req);
695 cfq_merged_requests(request_queue_t *q, struct request *rq,
696 struct request *next)
699 * reposition in fifo if next is older than rq
701 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
702 time_before(next->start_time, rq->start_time))
703 list_move(&rq->queuelist, &next->queuelist);
705 cfq_remove_request(next);
708 static int cfq_allow_merge(request_queue_t *q, struct request *rq,
711 struct cfq_data *cfqd = q->elevator->elevator_data;
712 const int rw = bio_data_dir(bio);
713 struct cfq_queue *cfqq;
717 * Disallow merge of a sync bio into an async request.
719 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
723 * Lookup the cfqq that this bio will be queued with. Allow
724 * merge only if rq is queued there.
726 key = cfq_queue_pid(current, rw, bio_sync(bio));
727 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
729 if (cfqq == RQ_CFQQ(rq))
736 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
740 * stop potential idle class queues waiting service
742 del_timer(&cfqd->idle_class_timer);
745 cfq_clear_cfqq_must_alloc_slice(cfqq);
746 cfq_clear_cfqq_fifo_expire(cfqq);
747 cfq_mark_cfqq_slice_new(cfqq);
748 cfq_clear_cfqq_queue_new(cfqq);
751 cfqd->active_queue = cfqq;
755 * current cfqq expired its slice (or was too idle), select new one
758 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
759 int preempted, int timed_out)
761 if (cfq_cfqq_wait_request(cfqq))
762 del_timer(&cfqd->idle_slice_timer);
764 cfq_clear_cfqq_must_dispatch(cfqq);
765 cfq_clear_cfqq_wait_request(cfqq);
768 * store what was left of this slice, if the queue idled out
771 if (timed_out && !cfq_cfqq_slice_new(cfqq))
772 cfqq->slice_resid = cfqq->slice_end - jiffies;
774 cfq_resort_rr_list(cfqq, preempted);
776 if (cfqq == cfqd->active_queue)
777 cfqd->active_queue = NULL;
779 if (cfqd->active_cic) {
780 put_io_context(cfqd->active_cic->ioc);
781 cfqd->active_cic = NULL;
784 cfqd->dispatch_slice = 0;
787 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
790 struct cfq_queue *cfqq = cfqd->active_queue;
793 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
797 * Get next queue for service. Unless we have a queue preemption,
798 * we'll simply select the first cfqq in the service tree.
800 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
802 struct cfq_queue *cfqq = NULL;
804 if (!list_empty(&cfqd->cur_rr)) {
806 * if current list is non-empty, grab first entry.
808 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
809 } else if (!RB_EMPTY_ROOT(&cfqd->service_tree.rb)) {
810 struct rb_node *n = cfq_rb_first(&cfqd->service_tree);
812 cfqq = rb_entry(n, struct cfq_queue, rb_node);
813 if (cfq_class_idle(cfqq)) {
817 * if we have idle queues and no rt or be queues had
818 * pending requests, either allow immediate service if
819 * the grace period has passed or arm the idle grace
822 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
823 if (time_before(jiffies, end)) {
824 mod_timer(&cfqd->idle_class_timer, end);
834 * Get and set a new active queue for service.
836 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
838 struct cfq_queue *cfqq;
840 cfqq = cfq_get_next_queue(cfqd);
841 __cfq_set_active_queue(cfqd, cfqq);
845 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
848 if (rq->sector >= cfqd->last_position)
849 return rq->sector - cfqd->last_position;
851 return cfqd->last_position - rq->sector;
854 static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
856 struct cfq_io_context *cic = cfqd->active_cic;
858 if (!sample_valid(cic->seek_samples))
861 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
864 static int cfq_close_cooperator(struct cfq_data *cfq_data,
865 struct cfq_queue *cfqq)
868 * We should notice if some of the queues are cooperating, eg
869 * working closely on the same area of the disk. In that case,
870 * we can group them together and don't waste time idling.
875 #define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
877 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
879 struct cfq_queue *cfqq = cfqd->active_queue;
880 struct cfq_io_context *cic;
883 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
884 WARN_ON(cfq_cfqq_slice_new(cfqq));
887 * idle is disabled, either manually or by past process history
889 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
893 * task has exited, don't wait
895 cic = cfqd->active_cic;
896 if (!cic || !cic->ioc->task)
900 * See if this prio level has a good candidate
902 if (cfq_close_cooperator(cfqd, cfqq) &&
903 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
906 cfq_mark_cfqq_must_dispatch(cfqq);
907 cfq_mark_cfqq_wait_request(cfqq);
910 * we don't want to idle for seeks, but we do want to allow
911 * fair distribution of slice time for a process doing back-to-back
912 * seeks. so allow a little bit of time for him to submit a new rq
914 sl = cfqd->cfq_slice_idle;
915 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
916 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
918 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
922 * Move request from internal lists to the request queue dispatch list.
924 static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
926 struct cfq_queue *cfqq = RQ_CFQQ(rq);
928 cfq_remove_request(rq);
930 elv_dispatch_sort(q, rq);
934 * return expired entry, or NULL to just start from scratch in rbtree
936 static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
938 struct cfq_data *cfqd = cfqq->cfqd;
942 if (cfq_cfqq_fifo_expire(cfqq))
945 cfq_mark_cfqq_fifo_expire(cfqq);
947 if (list_empty(&cfqq->fifo))
950 fifo = cfq_cfqq_sync(cfqq);
951 rq = rq_entry_fifo(cfqq->fifo.next);
953 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
960 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
962 const int base_rq = cfqd->cfq_slice_async_rq;
964 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
966 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
970 * Select a queue for service. If we have a current active queue,
971 * check whether to continue servicing it, or retrieve and set a new one.
973 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
975 struct cfq_queue *cfqq;
977 cfqq = cfqd->active_queue;
982 * The active queue has run out of time, expire it and select new.
984 if (cfq_slice_used(cfqq))
988 * The active queue has requests and isn't expired, allow it to
991 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
995 * No requests pending. If the active queue still has requests in
996 * flight or is idling for a new request, allow either of these
997 * conditions to happen (or time out) before selecting a new queue.
999 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
1005 cfq_slice_expired(cfqd, 0, 0);
1007 cfqq = cfq_set_active_queue(cfqd);
1013 * Dispatch some requests from cfqq, moving them to the request queue
1017 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1022 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1028 * follow expired path, else get first next available
1030 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1034 * finally, insert request into driver dispatch list
1036 cfq_dispatch_insert(cfqd->queue, rq);
1038 cfqd->dispatch_slice++;
1041 if (!cfqd->active_cic) {
1042 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1043 cfqd->active_cic = RQ_CIC(rq);
1046 if (RB_EMPTY_ROOT(&cfqq->sort_list))
1049 } while (dispatched < max_dispatch);
1052 * expire an async queue immediately if it has used up its slice. idle
1053 * queue always expire after 1 dispatch round.
1055 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1056 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1057 cfq_class_idle(cfqq))) {
1058 cfqq->slice_end = jiffies + 1;
1059 cfq_slice_expired(cfqd, 0, 0);
1065 static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1069 while (cfqq->next_rq) {
1070 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1074 BUG_ON(!list_empty(&cfqq->fifo));
1078 static int cfq_forced_dispatch_cfqqs(struct list_head *list)
1080 struct cfq_queue *cfqq, *next;
1084 list_for_each_entry_safe(cfqq, next, list, cfq_list)
1085 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1091 * Drain our current requests. Used for barriers and when switching
1092 * io schedulers on-the-fly.
1094 static int cfq_forced_dispatch(struct cfq_data *cfqd)
1099 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
1100 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1102 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1105 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1107 cfq_slice_expired(cfqd, 0, 0);
1109 BUG_ON(cfqd->busy_queues);
1114 static int cfq_dispatch_requests(request_queue_t *q, int force)
1116 struct cfq_data *cfqd = q->elevator->elevator_data;
1117 struct cfq_queue *cfqq;
1120 if (!cfqd->busy_queues)
1123 if (unlikely(force))
1124 return cfq_forced_dispatch(cfqd);
1127 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
1130 if (cfqd->busy_queues > 1) {
1132 * So we have dispatched before in this round, if the
1133 * next queue has idling enabled (must be sync), don't
1134 * allow it service until the previous have completed.
1136 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1139 if (cfqq->dispatched >= cfqd->cfq_quantum)
1143 cfq_clear_cfqq_must_dispatch(cfqq);
1144 cfq_clear_cfqq_wait_request(cfqq);
1145 del_timer(&cfqd->idle_slice_timer);
1147 max_dispatch = cfqd->cfq_quantum;
1148 if (cfq_class_idle(cfqq))
1151 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1158 * task holds one reference to the queue, dropped when task exits. each rq
1159 * in-flight on this queue also holds a reference, dropped when rq is freed.
1161 * queue lock must be held here.
1163 static void cfq_put_queue(struct cfq_queue *cfqq)
1165 struct cfq_data *cfqd = cfqq->cfqd;
1167 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1169 if (!atomic_dec_and_test(&cfqq->ref))
1172 BUG_ON(rb_first(&cfqq->sort_list));
1173 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1174 BUG_ON(cfq_cfqq_on_rr(cfqq));
1176 if (unlikely(cfqd->active_queue == cfqq)) {
1177 __cfq_slice_expired(cfqd, cfqq, 0, 0);
1178 cfq_schedule_dispatch(cfqd);
1182 * it's on the empty list and still hashed
1184 hlist_del(&cfqq->cfq_hash);
1185 kmem_cache_free(cfq_pool, cfqq);
1188 static struct cfq_queue *
1189 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1192 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1193 struct hlist_node *entry;
1194 struct cfq_queue *__cfqq;
1196 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
1197 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1199 if (__cfqq->key == key && (__p == prio || !prio))
1206 static struct cfq_queue *
1207 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1209 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1212 static void cfq_free_io_context(struct io_context *ioc)
1214 struct cfq_io_context *__cic;
1218 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1219 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1220 rb_erase(&__cic->rb_node, &ioc->cic_root);
1221 kmem_cache_free(cfq_ioc_pool, __cic);
1225 elv_ioc_count_mod(ioc_count, -freed);
1227 if (ioc_gone && !elv_ioc_count_read(ioc_count))
1231 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1233 if (unlikely(cfqq == cfqd->active_queue)) {
1234 __cfq_slice_expired(cfqd, cfqq, 0, 0);
1235 cfq_schedule_dispatch(cfqd);
1238 cfq_put_queue(cfqq);
1241 static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1242 struct cfq_io_context *cic)
1244 list_del_init(&cic->queue_list);
1248 if (cic->cfqq[ASYNC]) {
1249 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1250 cic->cfqq[ASYNC] = NULL;
1253 if (cic->cfqq[SYNC]) {
1254 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1255 cic->cfqq[SYNC] = NULL;
1259 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1261 struct cfq_data *cfqd = cic->key;
1264 request_queue_t *q = cfqd->queue;
1266 spin_lock_irq(q->queue_lock);
1267 __cfq_exit_single_io_context(cfqd, cic);
1268 spin_unlock_irq(q->queue_lock);
1273 * The process that ioc belongs to has exited, we need to clean up
1274 * and put the internal structures we have that belongs to that process.
1276 static void cfq_exit_io_context(struct io_context *ioc)
1278 struct cfq_io_context *__cic;
1282 * put the reference this task is holding to the various queues
1285 n = rb_first(&ioc->cic_root);
1287 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1289 cfq_exit_single_io_context(__cic);
1294 static struct cfq_io_context *
1295 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1297 struct cfq_io_context *cic;
1299 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
1301 memset(cic, 0, sizeof(*cic));
1302 cic->last_end_request = jiffies;
1303 INIT_LIST_HEAD(&cic->queue_list);
1304 cic->dtor = cfq_free_io_context;
1305 cic->exit = cfq_exit_io_context;
1306 elv_ioc_count_inc(ioc_count);
1312 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1314 struct task_struct *tsk = current;
1317 if (!cfq_cfqq_prio_changed(cfqq))
1320 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1321 switch (ioprio_class) {
1323 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1324 case IOPRIO_CLASS_NONE:
1326 * no prio set, place us in the middle of the BE classes
1328 cfqq->ioprio = task_nice_ioprio(tsk);
1329 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1331 case IOPRIO_CLASS_RT:
1332 cfqq->ioprio = task_ioprio(tsk);
1333 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1335 case IOPRIO_CLASS_BE:
1336 cfqq->ioprio = task_ioprio(tsk);
1337 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1339 case IOPRIO_CLASS_IDLE:
1340 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1342 cfq_clear_cfqq_idle_window(cfqq);
1347 * keep track of original prio settings in case we have to temporarily
1348 * elevate the priority of this queue
1350 cfqq->org_ioprio = cfqq->ioprio;
1351 cfqq->org_ioprio_class = cfqq->ioprio_class;
1352 cfq_clear_cfqq_prio_changed(cfqq);
1355 static inline void changed_ioprio(struct cfq_io_context *cic)
1357 struct cfq_data *cfqd = cic->key;
1358 struct cfq_queue *cfqq;
1359 unsigned long flags;
1361 if (unlikely(!cfqd))
1364 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1366 cfqq = cic->cfqq[ASYNC];
1368 struct cfq_queue *new_cfqq;
1369 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1372 cic->cfqq[ASYNC] = new_cfqq;
1373 cfq_put_queue(cfqq);
1377 cfqq = cic->cfqq[SYNC];
1379 cfq_mark_cfqq_prio_changed(cfqq);
1381 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1384 static void cfq_ioc_set_ioprio(struct io_context *ioc)
1386 struct cfq_io_context *cic;
1389 ioc->ioprio_changed = 0;
1391 n = rb_first(&ioc->cic_root);
1393 cic = rb_entry(n, struct cfq_io_context, rb_node);
1395 changed_ioprio(cic);
1400 static struct cfq_queue *
1401 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
1404 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1405 struct cfq_queue *cfqq, *new_cfqq = NULL;
1406 unsigned short ioprio;
1409 ioprio = tsk->ioprio;
1410 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1416 } else if (gfp_mask & __GFP_WAIT) {
1418 * Inform the allocator of the fact that we will
1419 * just repeat this allocation if it fails, to allow
1420 * the allocator to do whatever it needs to attempt to
1423 spin_unlock_irq(cfqd->queue->queue_lock);
1424 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
1425 spin_lock_irq(cfqd->queue->queue_lock);
1428 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
1433 memset(cfqq, 0, sizeof(*cfqq));
1435 INIT_HLIST_NODE(&cfqq->cfq_hash);
1436 INIT_LIST_HEAD(&cfqq->cfq_list);
1437 RB_CLEAR_NODE(&cfqq->rb_node);
1438 INIT_LIST_HEAD(&cfqq->fifo);
1441 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1442 atomic_set(&cfqq->ref, 0);
1445 if (key != CFQ_KEY_ASYNC)
1446 cfq_mark_cfqq_idle_window(cfqq);
1448 cfq_mark_cfqq_prio_changed(cfqq);
1449 cfq_mark_cfqq_queue_new(cfqq);
1450 cfq_init_prio_data(cfqq);
1454 kmem_cache_free(cfq_pool, new_cfqq);
1456 atomic_inc(&cfqq->ref);
1458 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1463 * We drop cfq io contexts lazily, so we may find a dead one.
1466 cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1468 WARN_ON(!list_empty(&cic->queue_list));
1469 rb_erase(&cic->rb_node, &ioc->cic_root);
1470 kmem_cache_free(cfq_ioc_pool, cic);
1471 elv_ioc_count_dec(ioc_count);
1474 static struct cfq_io_context *
1475 cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1478 struct cfq_io_context *cic;
1479 void *k, *key = cfqd;
1482 n = ioc->cic_root.rb_node;
1484 cic = rb_entry(n, struct cfq_io_context, rb_node);
1485 /* ->key must be copied to avoid race with cfq_exit_queue() */
1488 cfq_drop_dead_cic(ioc, cic);
1504 cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1505 struct cfq_io_context *cic)
1508 struct rb_node *parent;
1509 struct cfq_io_context *__cic;
1510 unsigned long flags;
1518 p = &ioc->cic_root.rb_node;
1521 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1522 /* ->key must be copied to avoid race with cfq_exit_queue() */
1525 cfq_drop_dead_cic(ioc, __cic);
1531 else if (cic->key > k)
1532 p = &(*p)->rb_right;
1537 rb_link_node(&cic->rb_node, parent, p);
1538 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1540 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1541 list_add(&cic->queue_list, &cfqd->cic_list);
1542 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1546 * Setup general io context and cfq io context. There can be several cfq
1547 * io contexts per general io context, if this process is doing io to more
1548 * than one device managed by cfq.
1550 static struct cfq_io_context *
1551 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1553 struct io_context *ioc = NULL;
1554 struct cfq_io_context *cic;
1556 might_sleep_if(gfp_mask & __GFP_WAIT);
1558 ioc = get_io_context(gfp_mask, cfqd->queue->node);
1562 cic = cfq_cic_rb_lookup(cfqd, ioc);
1566 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1570 cfq_cic_link(cfqd, ioc, cic);
1572 smp_read_barrier_depends();
1573 if (unlikely(ioc->ioprio_changed))
1574 cfq_ioc_set_ioprio(ioc);
1578 put_io_context(ioc);
1583 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1585 unsigned long elapsed = jiffies - cic->last_end_request;
1586 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1588 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1589 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1590 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1594 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1600 if (cic->last_request_pos < rq->sector)
1601 sdist = rq->sector - cic->last_request_pos;
1603 sdist = cic->last_request_pos - rq->sector;
1605 if (!cic->seek_samples) {
1606 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1607 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1611 * Don't allow the seek distance to get too large from the
1612 * odd fragment, pagein, etc
1614 if (cic->seek_samples <= 60) /* second&third seek */
1615 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1617 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1619 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1620 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1621 total = cic->seek_total + (cic->seek_samples/2);
1622 do_div(total, cic->seek_samples);
1623 cic->seek_mean = (sector_t)total;
1627 * Disable idle window if the process thinks too long or seeks so much that
1631 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1632 struct cfq_io_context *cic)
1634 int enable_idle = cfq_cfqq_idle_window(cfqq);
1636 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1637 (cfqd->hw_tag && CIC_SEEKY(cic)))
1639 else if (sample_valid(cic->ttime_samples)) {
1640 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1647 cfq_mark_cfqq_idle_window(cfqq);
1649 cfq_clear_cfqq_idle_window(cfqq);
1653 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1654 * no or if we aren't sure, a 1 will cause a preempt.
1657 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1660 struct cfq_queue *cfqq;
1662 cfqq = cfqd->active_queue;
1666 if (cfq_slice_used(cfqq))
1669 if (cfq_class_idle(new_cfqq))
1672 if (cfq_class_idle(cfqq))
1676 * if the new request is sync, but the currently running queue is
1677 * not, let the sync request have priority.
1679 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
1683 * So both queues are sync. Let the new request get disk time if
1684 * it's a metadata request and the current queue is doing regular IO.
1686 if (rq_is_meta(rq) && !cfqq->meta_pending)
1689 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1693 * if this request is as-good as one we would expect from the
1694 * current cfqq, let it preempt
1696 if (cfq_rq_close(cfqd, rq))
1703 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1704 * let it have half of its nominal slice.
1706 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1708 cfq_slice_expired(cfqd, 1, 1);
1711 * Put the new queue at the front of the of the current list,
1712 * so we know that it will be selected next.
1714 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1715 list_del_init(&cfqq->cfq_list);
1716 list_add(&cfqq->cfq_list, &cfqd->cur_rr);
1718 cfqq->slice_end = 0;
1719 cfq_mark_cfqq_slice_new(cfqq);
1723 * Called when a new fs request (rq) is added (to cfqq). Check if there's
1724 * something we should do about it
1727 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1730 struct cfq_io_context *cic = RQ_CIC(rq);
1733 cfqq->meta_pending++;
1735 cfq_update_io_thinktime(cfqd, cic);
1736 cfq_update_io_seektime(cfqd, cic, rq);
1737 cfq_update_idle_window(cfqd, cfqq, cic);
1739 cic->last_request_pos = rq->sector + rq->nr_sectors;
1740 cfqq->last_request_pos = cic->last_request_pos;
1742 if (cfqq == cfqd->active_queue) {
1744 * if we are waiting for a request for this queue, let it rip
1745 * immediately and flag that we must not expire this queue
1748 if (cfq_cfqq_wait_request(cfqq)) {
1749 cfq_mark_cfqq_must_dispatch(cfqq);
1750 del_timer(&cfqd->idle_slice_timer);
1751 blk_start_queueing(cfqd->queue);
1753 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
1755 * not the active queue - expire current slice if it is
1756 * idle and has expired it's mean thinktime or this new queue
1757 * has some old slice time left and is of higher priority
1759 cfq_preempt_queue(cfqd, cfqq);
1760 cfq_mark_cfqq_must_dispatch(cfqq);
1761 blk_start_queueing(cfqd->queue);
1765 static void cfq_insert_request(request_queue_t *q, struct request *rq)
1767 struct cfq_data *cfqd = q->elevator->elevator_data;
1768 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1770 cfq_init_prio_data(cfqq);
1774 list_add_tail(&rq->queuelist, &cfqq->fifo);
1776 cfq_rq_enqueued(cfqd, cfqq, rq);
1779 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1781 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1782 struct cfq_data *cfqd = cfqq->cfqd;
1783 const int sync = rq_is_sync(rq);
1788 WARN_ON(!cfqd->rq_in_driver);
1789 WARN_ON(!cfqq->dispatched);
1790 cfqd->rq_in_driver--;
1793 if (!cfq_class_idle(cfqq))
1794 cfqd->last_end_request = now;
1797 RQ_CIC(rq)->last_end_request = now;
1800 * If this is the active queue, check if it needs to be expired,
1801 * or if we want to idle in case it has no pending requests.
1803 if (cfqd->active_queue == cfqq) {
1804 if (cfq_cfqq_slice_new(cfqq)) {
1805 cfq_set_prio_slice(cfqd, cfqq);
1806 cfq_clear_cfqq_slice_new(cfqq);
1808 if (cfq_slice_used(cfqq))
1809 cfq_slice_expired(cfqd, 0, 1);
1810 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1811 cfq_arm_slice_timer(cfqd);
1814 if (!cfqd->rq_in_driver)
1815 cfq_schedule_dispatch(cfqd);
1819 * we temporarily boost lower priority queues if they are holding fs exclusive
1820 * resources. they are boosted to normal prio (CLASS_BE/4)
1822 static void cfq_prio_boost(struct cfq_queue *cfqq)
1824 if (has_fs_excl()) {
1826 * boost idle prio on transactions that would lock out other
1827 * users of the filesystem
1829 if (cfq_class_idle(cfqq))
1830 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1831 if (cfqq->ioprio > IOPRIO_NORM)
1832 cfqq->ioprio = IOPRIO_NORM;
1835 * check if we need to unboost the queue
1837 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1838 cfqq->ioprio_class = cfqq->org_ioprio_class;
1839 if (cfqq->ioprio != cfqq->org_ioprio)
1840 cfqq->ioprio = cfqq->org_ioprio;
1844 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
1846 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1847 !cfq_cfqq_must_alloc_slice(cfqq)) {
1848 cfq_mark_cfqq_must_alloc_slice(cfqq);
1849 return ELV_MQUEUE_MUST;
1852 return ELV_MQUEUE_MAY;
1855 static int cfq_may_queue(request_queue_t *q, int rw)
1857 struct cfq_data *cfqd = q->elevator->elevator_data;
1858 struct task_struct *tsk = current;
1859 struct cfq_queue *cfqq;
1862 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
1865 * don't force setup of a queue from here, as a call to may_queue
1866 * does not necessarily imply that a request actually will be queued.
1867 * so just lookup a possibly existing queue, or return 'may queue'
1870 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
1872 cfq_init_prio_data(cfqq);
1873 cfq_prio_boost(cfqq);
1875 return __cfq_may_queue(cfqq);
1878 return ELV_MQUEUE_MAY;
1882 * queue lock held here
1884 static void cfq_put_request(struct request *rq)
1886 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1889 const int rw = rq_data_dir(rq);
1891 BUG_ON(!cfqq->allocated[rw]);
1892 cfqq->allocated[rw]--;
1894 put_io_context(RQ_CIC(rq)->ioc);
1896 rq->elevator_private = NULL;
1897 rq->elevator_private2 = NULL;
1899 cfq_put_queue(cfqq);
1904 * Allocate cfq data structures associated with this request.
1907 cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
1909 struct cfq_data *cfqd = q->elevator->elevator_data;
1910 struct task_struct *tsk = current;
1911 struct cfq_io_context *cic;
1912 const int rw = rq_data_dir(rq);
1913 const int is_sync = rq_is_sync(rq);
1914 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
1915 struct cfq_queue *cfqq;
1916 unsigned long flags;
1918 might_sleep_if(gfp_mask & __GFP_WAIT);
1920 cic = cfq_get_io_context(cfqd, gfp_mask);
1922 spin_lock_irqsave(q->queue_lock, flags);
1927 if (!cic->cfqq[is_sync]) {
1928 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
1932 cic->cfqq[is_sync] = cfqq;
1934 cfqq = cic->cfqq[is_sync];
1936 cfqq->allocated[rw]++;
1937 cfq_clear_cfqq_must_alloc(cfqq);
1938 atomic_inc(&cfqq->ref);
1940 spin_unlock_irqrestore(q->queue_lock, flags);
1942 rq->elevator_private = cic;
1943 rq->elevator_private2 = cfqq;
1948 put_io_context(cic->ioc);
1950 cfq_schedule_dispatch(cfqd);
1951 spin_unlock_irqrestore(q->queue_lock, flags);
1955 static void cfq_kick_queue(struct work_struct *work)
1957 struct cfq_data *cfqd =
1958 container_of(work, struct cfq_data, unplug_work);
1959 request_queue_t *q = cfqd->queue;
1960 unsigned long flags;
1962 spin_lock_irqsave(q->queue_lock, flags);
1963 blk_start_queueing(q);
1964 spin_unlock_irqrestore(q->queue_lock, flags);
1968 * Timer running if the active_queue is currently idling inside its time slice
1970 static void cfq_idle_slice_timer(unsigned long data)
1972 struct cfq_data *cfqd = (struct cfq_data *) data;
1973 struct cfq_queue *cfqq;
1974 unsigned long flags;
1977 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1979 if ((cfqq = cfqd->active_queue) != NULL) {
1985 if (cfq_slice_used(cfqq))
1989 * only expire and reinvoke request handler, if there are
1990 * other queues with pending requests
1992 if (!cfqd->busy_queues)
1996 * not expired and it has a request pending, let it dispatch
1998 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
1999 cfq_mark_cfqq_must_dispatch(cfqq);
2004 cfq_slice_expired(cfqd, 0, timed_out);
2006 cfq_schedule_dispatch(cfqd);
2008 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2012 * Timer running if an idle class queue is waiting for service
2014 static void cfq_idle_class_timer(unsigned long data)
2016 struct cfq_data *cfqd = (struct cfq_data *) data;
2017 unsigned long flags, end;
2019 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2022 * race with a non-idle queue, reset timer
2024 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2025 if (!time_after_eq(jiffies, end))
2026 mod_timer(&cfqd->idle_class_timer, end);
2028 cfq_schedule_dispatch(cfqd);
2030 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2033 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2035 del_timer_sync(&cfqd->idle_slice_timer);
2036 del_timer_sync(&cfqd->idle_class_timer);
2037 blk_sync_queue(cfqd->queue);
2040 static void cfq_exit_queue(elevator_t *e)
2042 struct cfq_data *cfqd = e->elevator_data;
2043 request_queue_t *q = cfqd->queue;
2045 cfq_shutdown_timer_wq(cfqd);
2047 spin_lock_irq(q->queue_lock);
2049 if (cfqd->active_queue)
2050 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
2052 while (!list_empty(&cfqd->cic_list)) {
2053 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2054 struct cfq_io_context,
2057 __cfq_exit_single_io_context(cfqd, cic);
2060 spin_unlock_irq(q->queue_lock);
2062 cfq_shutdown_timer_wq(cfqd);
2064 kfree(cfqd->cfq_hash);
2068 static void *cfq_init_queue(request_queue_t *q)
2070 struct cfq_data *cfqd;
2073 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
2077 memset(cfqd, 0, sizeof(*cfqd));
2079 cfqd->service_tree = CFQ_RB_ROOT;
2080 INIT_LIST_HEAD(&cfqd->cur_rr);
2081 INIT_LIST_HEAD(&cfqd->cic_list);
2083 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
2084 if (!cfqd->cfq_hash)
2087 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2088 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2092 init_timer(&cfqd->idle_slice_timer);
2093 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2094 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2096 init_timer(&cfqd->idle_class_timer);
2097 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2098 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2100 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
2102 cfqd->cfq_quantum = cfq_quantum;
2103 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2104 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2105 cfqd->cfq_back_max = cfq_back_max;
2106 cfqd->cfq_back_penalty = cfq_back_penalty;
2107 cfqd->cfq_slice[0] = cfq_slice_async;
2108 cfqd->cfq_slice[1] = cfq_slice_sync;
2109 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2110 cfqd->cfq_slice_idle = cfq_slice_idle;
2118 static void cfq_slab_kill(void)
2121 kmem_cache_destroy(cfq_pool);
2123 kmem_cache_destroy(cfq_ioc_pool);
2126 static int __init cfq_slab_setup(void)
2128 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2133 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2134 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2145 * sysfs parts below -->
2148 cfq_var_show(unsigned int var, char *page)
2150 return sprintf(page, "%d\n", var);
2154 cfq_var_store(unsigned int *var, const char *page, size_t count)
2156 char *p = (char *) page;
2158 *var = simple_strtoul(p, &p, 10);
2162 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2163 static ssize_t __FUNC(elevator_t *e, char *page) \
2165 struct cfq_data *cfqd = e->elevator_data; \
2166 unsigned int __data = __VAR; \
2168 __data = jiffies_to_msecs(__data); \
2169 return cfq_var_show(__data, (page)); \
2171 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2172 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2173 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2174 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2175 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2176 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2177 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2178 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2179 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2180 #undef SHOW_FUNCTION
2182 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2183 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2185 struct cfq_data *cfqd = e->elevator_data; \
2186 unsigned int __data; \
2187 int ret = cfq_var_store(&__data, (page), count); \
2188 if (__data < (MIN)) \
2190 else if (__data > (MAX)) \
2193 *(__PTR) = msecs_to_jiffies(__data); \
2195 *(__PTR) = __data; \
2198 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2199 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2200 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2201 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2202 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2203 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2204 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2205 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2206 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2207 #undef STORE_FUNCTION
2209 #define CFQ_ATTR(name) \
2210 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2212 static struct elv_fs_entry cfq_attrs[] = {
2214 CFQ_ATTR(fifo_expire_sync),
2215 CFQ_ATTR(fifo_expire_async),
2216 CFQ_ATTR(back_seek_max),
2217 CFQ_ATTR(back_seek_penalty),
2218 CFQ_ATTR(slice_sync),
2219 CFQ_ATTR(slice_async),
2220 CFQ_ATTR(slice_async_rq),
2221 CFQ_ATTR(slice_idle),
2225 static struct elevator_type iosched_cfq = {
2227 .elevator_merge_fn = cfq_merge,
2228 .elevator_merged_fn = cfq_merged_request,
2229 .elevator_merge_req_fn = cfq_merged_requests,
2230 .elevator_allow_merge_fn = cfq_allow_merge,
2231 .elevator_dispatch_fn = cfq_dispatch_requests,
2232 .elevator_add_req_fn = cfq_insert_request,
2233 .elevator_activate_req_fn = cfq_activate_request,
2234 .elevator_deactivate_req_fn = cfq_deactivate_request,
2235 .elevator_queue_empty_fn = cfq_queue_empty,
2236 .elevator_completed_req_fn = cfq_completed_request,
2237 .elevator_former_req_fn = elv_rb_former_request,
2238 .elevator_latter_req_fn = elv_rb_latter_request,
2239 .elevator_set_req_fn = cfq_set_request,
2240 .elevator_put_req_fn = cfq_put_request,
2241 .elevator_may_queue_fn = cfq_may_queue,
2242 .elevator_init_fn = cfq_init_queue,
2243 .elevator_exit_fn = cfq_exit_queue,
2244 .trim = cfq_free_io_context,
2246 .elevator_attrs = cfq_attrs,
2247 .elevator_name = "cfq",
2248 .elevator_owner = THIS_MODULE,
2251 static int __init cfq_init(void)
2256 * could be 0 on HZ < 1000 setups
2258 if (!cfq_slice_async)
2259 cfq_slice_async = 1;
2260 if (!cfq_slice_idle)
2263 if (cfq_slab_setup())
2266 ret = elv_register(&iosched_cfq);
2273 static void __exit cfq_exit(void)
2275 DECLARE_COMPLETION_ONSTACK(all_gone);
2276 elv_unregister(&iosched_cfq);
2277 ioc_gone = &all_gone;
2278 /* ioc_gone's update must be visible before reading ioc_count */
2280 if (elv_ioc_count_read(ioc_count))
2281 wait_for_completion(ioc_gone);
2286 module_init(cfq_init);
2287 module_exit(cfq_exit);
2289 MODULE_AUTHOR("Jens Axboe");
2290 MODULE_LICENSE("GPL");
2291 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");