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_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;
29 #define CFQ_IDLE_GRACE (HZ / 10)
30 #define CFQ_SLICE_SCALE (5)
32 #define CFQ_KEY_ASYNC (0)
34 static DEFINE_SPINLOCK(cfq_exit_lock
);
37 * for the hash of cfqq inside the cfqd
39 #define CFQ_QHASH_SHIFT 6
40 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
41 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
43 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
45 #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
46 #define RQ_CFQQ(rq) ((rq)->elevator_private2)
48 static kmem_cache_t
*cfq_pool
;
49 static kmem_cache_t
*cfq_ioc_pool
;
51 static atomic_t ioc_count
= ATOMIC_INIT(0);
52 static struct completion
*ioc_gone
;
54 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
55 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
56 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
61 #define cfq_cfqq_dispatched(cfqq) \
62 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
64 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
66 #define cfq_cfqq_sync(cfqq) \
67 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
69 #define sample_valid(samples) ((samples) > 80)
72 * Per block device queue structure
75 request_queue_t
*queue
;
78 * rr list of queues with requests and the count of them
80 struct list_head rr_list
[CFQ_PRIO_LISTS
];
81 struct list_head busy_rr
;
82 struct list_head cur_rr
;
83 struct list_head idle_rr
;
84 unsigned int busy_queues
;
87 * non-ordered list of empty cfqq's
89 struct list_head empty_list
;
94 struct hlist_head
*cfq_hash
;
100 * idle window management
102 struct timer_list idle_slice_timer
;
103 struct work_struct unplug_work
;
105 struct cfq_queue
*active_queue
;
106 struct cfq_io_context
*active_cic
;
107 int cur_prio
, cur_end_prio
;
108 unsigned int dispatch_slice
;
110 struct timer_list idle_class_timer
;
112 sector_t last_sector
;
113 unsigned long last_end_request
;
116 * tunables, see top of file
118 unsigned int cfq_quantum
;
119 unsigned int cfq_fifo_expire
[2];
120 unsigned int cfq_back_penalty
;
121 unsigned int cfq_back_max
;
122 unsigned int cfq_slice
[2];
123 unsigned int cfq_slice_async_rq
;
124 unsigned int cfq_slice_idle
;
126 struct list_head cic_list
;
130 * Per process-grouping structure
133 /* reference count */
135 /* parent cfq_data */
136 struct cfq_data
*cfqd
;
137 /* cfqq lookup hash */
138 struct hlist_node cfq_hash
;
141 /* on either rr or empty list of cfqd */
142 struct list_head cfq_list
;
143 /* sorted list of pending requests */
144 struct rb_root sort_list
;
145 /* if fifo isn't expired, next request to serve */
146 struct request
*next_rq
;
147 /* requests queued in sort_list */
149 /* currently allocated requests */
151 /* fifo list of requests in sort_list */
152 struct list_head fifo
;
154 unsigned long slice_start
;
155 unsigned long slice_end
;
156 unsigned long slice_left
;
157 unsigned long service_last
;
159 /* number of requests that are on the dispatch list */
162 /* io prio of this group */
163 unsigned short ioprio
, org_ioprio
;
164 unsigned short ioprio_class
, org_ioprio_class
;
166 /* various state flags, see below */
170 enum cfqq_state_flags
{
171 CFQ_CFQQ_FLAG_on_rr
= 0,
172 CFQ_CFQQ_FLAG_wait_request
,
173 CFQ_CFQQ_FLAG_must_alloc
,
174 CFQ_CFQQ_FLAG_must_alloc_slice
,
175 CFQ_CFQQ_FLAG_must_dispatch
,
176 CFQ_CFQQ_FLAG_fifo_expire
,
177 CFQ_CFQQ_FLAG_idle_window
,
178 CFQ_CFQQ_FLAG_prio_changed
,
181 #define CFQ_CFQQ_FNS(name) \
182 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
184 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
186 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
188 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
190 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
192 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
196 CFQ_CFQQ_FNS(wait_request
);
197 CFQ_CFQQ_FNS(must_alloc
);
198 CFQ_CFQQ_FNS(must_alloc_slice
);
199 CFQ_CFQQ_FNS(must_dispatch
);
200 CFQ_CFQQ_FNS(fifo_expire
);
201 CFQ_CFQQ_FNS(idle_window
);
202 CFQ_CFQQ_FNS(prio_changed
);
205 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
206 static void cfq_dispatch_insert(request_queue_t
*, struct request
*);
207 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
, gfp_t gfp_mask
);
210 * scheduler run of queue, if there are requests pending and no one in the
211 * driver that will restart queueing
213 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
215 if (cfqd
->busy_queues
)
216 kblockd_schedule_work(&cfqd
->unplug_work
);
219 static int cfq_queue_empty(request_queue_t
*q
)
221 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
223 return !cfqd
->busy_queues
;
226 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
)
228 if (rw
== READ
|| rw
== WRITE_SYNC
)
231 return CFQ_KEY_ASYNC
;
235 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
236 * We choose the request that is closest to the head right now. Distance
237 * behind the head is penalized and only allowed to a certain extent.
239 static struct request
*
240 cfq_choose_req(struct cfq_data
*cfqd
, struct request
*rq1
, struct request
*rq2
)
242 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
243 unsigned long back_max
;
244 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
245 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
246 unsigned wrap
= 0; /* bit mask: requests behind the disk head? */
248 if (rq1
== NULL
|| rq1
== rq2
)
253 if (rq_is_sync(rq1
) && !rq_is_sync(rq2
))
255 else if (rq_is_sync(rq2
) && !rq_is_sync(rq1
))
261 last
= cfqd
->last_sector
;
264 * by definition, 1KiB is 2 sectors
266 back_max
= cfqd
->cfq_back_max
* 2;
269 * Strict one way elevator _except_ in the case where we allow
270 * short backward seeks which are biased as twice the cost of a
271 * similar forward seek.
275 else if (s1
+ back_max
>= last
)
276 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
278 wrap
|= CFQ_RQ1_WRAP
;
282 else if (s2
+ back_max
>= last
)
283 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
285 wrap
|= CFQ_RQ2_WRAP
;
287 /* Found required data */
290 * By doing switch() on the bit mask "wrap" we avoid having to
291 * check two variables for all permutations: --> faster!
294 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
310 case (CFQ_RQ1_WRAP
|CFQ_RQ2_WRAP
): /* both rqs wrapped */
313 * Since both rqs are wrapped,
314 * start with the one that's further behind head
315 * (--> only *one* back seek required),
316 * since back seek takes more time than forward.
326 * would be nice to take fifo expire time into account as well
328 static struct request
*
329 cfq_find_next_rq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
330 struct request
*last
)
332 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
333 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
334 struct request
*next
= NULL
, *prev
= NULL
;
336 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
339 prev
= rb_entry_rq(rbprev
);
342 next
= rb_entry_rq(rbnext
);
344 rbnext
= rb_first(&cfqq
->sort_list
);
345 if (rbnext
&& rbnext
!= &last
->rb_node
)
346 next
= rb_entry_rq(rbnext
);
349 return cfq_choose_req(cfqd
, next
, prev
);
352 static void cfq_resort_rr_list(struct cfq_queue
*cfqq
, int preempted
)
354 struct cfq_data
*cfqd
= cfqq
->cfqd
;
355 struct list_head
*list
, *entry
;
357 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
359 list_del(&cfqq
->cfq_list
);
361 if (cfq_class_rt(cfqq
))
362 list
= &cfqd
->cur_rr
;
363 else if (cfq_class_idle(cfqq
))
364 list
= &cfqd
->idle_rr
;
367 * if cfqq has requests in flight, don't allow it to be
368 * found in cfq_set_active_queue before it has finished them.
369 * this is done to increase fairness between a process that
370 * has lots of io pending vs one that only generates one
371 * sporadically or synchronously
373 if (cfq_cfqq_dispatched(cfqq
))
374 list
= &cfqd
->busy_rr
;
376 list
= &cfqd
->rr_list
[cfqq
->ioprio
];
380 * if queue was preempted, just add to front to be fair. busy_rr
381 * isn't sorted, but insert at the back for fairness.
383 if (preempted
|| list
== &cfqd
->busy_rr
) {
387 list_add_tail(&cfqq
->cfq_list
, list
);
392 * sort by when queue was last serviced
395 while ((entry
= entry
->prev
) != list
) {
396 struct cfq_queue
*__cfqq
= list_entry_cfqq(entry
);
398 if (!__cfqq
->service_last
)
400 if (time_before(__cfqq
->service_last
, cfqq
->service_last
))
404 list_add(&cfqq
->cfq_list
, entry
);
408 * add to busy list of queues for service, trying to be fair in ordering
409 * the pending list according to last request service
412 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
414 BUG_ON(cfq_cfqq_on_rr(cfqq
));
415 cfq_mark_cfqq_on_rr(cfqq
);
418 cfq_resort_rr_list(cfqq
, 0);
422 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
424 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
425 cfq_clear_cfqq_on_rr(cfqq
);
426 list_move(&cfqq
->cfq_list
, &cfqd
->empty_list
);
428 BUG_ON(!cfqd
->busy_queues
);
433 * rb tree support functions
435 static inline void cfq_del_rq_rb(struct request
*rq
)
437 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
438 struct cfq_data
*cfqd
= cfqq
->cfqd
;
439 const int sync
= rq_is_sync(rq
);
441 BUG_ON(!cfqq
->queued
[sync
]);
442 cfqq
->queued
[sync
]--;
444 elv_rb_del(&cfqq
->sort_list
, rq
);
446 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
))
447 cfq_del_cfqq_rr(cfqd
, cfqq
);
450 static void cfq_add_rq_rb(struct request
*rq
)
452 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
453 struct cfq_data
*cfqd
= cfqq
->cfqd
;
454 struct request
*__alias
;
456 cfqq
->queued
[rq_is_sync(rq
)]++;
459 * looks a little odd, but the first insert might return an alias.
460 * if that happens, put the alias on the dispatch list
462 while ((__alias
= elv_rb_add(&cfqq
->sort_list
, rq
)) != NULL
)
463 cfq_dispatch_insert(cfqd
->queue
, __alias
);
467 cfq_reposition_rq_rb(struct cfq_queue
*cfqq
, struct request
*rq
)
469 elv_rb_del(&cfqq
->sort_list
, rq
);
470 cfqq
->queued
[rq_is_sync(rq
)]--;
474 static struct request
*
475 cfq_find_rq_fmerge(struct cfq_data
*cfqd
, struct bio
*bio
)
477 struct task_struct
*tsk
= current
;
478 pid_t key
= cfq_queue_pid(tsk
, bio_data_dir(bio
));
479 struct cfq_queue
*cfqq
;
481 cfqq
= cfq_find_cfq_hash(cfqd
, key
, tsk
->ioprio
);
483 sector_t sector
= bio
->bi_sector
+ bio_sectors(bio
);
485 return elv_rb_find(&cfqq
->sort_list
, sector
);
491 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
493 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
495 cfqd
->rq_in_driver
++;
498 * If the depth is larger 1, it really could be queueing. But lets
499 * make the mark a little higher - idling could still be good for
500 * low queueing, and a low queueing number could also just indicate
501 * a SCSI mid layer like behaviour where limit+1 is often seen.
503 if (!cfqd
->hw_tag
&& cfqd
->rq_in_driver
> 4)
507 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
509 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
511 WARN_ON(!cfqd
->rq_in_driver
);
512 cfqd
->rq_in_driver
--;
515 static void cfq_remove_request(struct request
*rq
)
517 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
519 if (cfqq
->next_rq
== rq
)
520 cfqq
->next_rq
= cfq_find_next_rq(cfqq
->cfqd
, cfqq
, rq
);
522 list_del_init(&rq
->queuelist
);
527 cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
529 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
530 struct request
*__rq
;
532 __rq
= cfq_find_rq_fmerge(cfqd
, bio
);
533 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
535 return ELEVATOR_FRONT_MERGE
;
538 return ELEVATOR_NO_MERGE
;
541 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
,
544 if (type
== ELEVATOR_FRONT_MERGE
) {
545 struct cfq_queue
*cfqq
= RQ_CFQQ(req
);
547 cfq_reposition_rq_rb(cfqq
, req
);
552 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
553 struct request
*next
)
556 * reposition in fifo if next is older than rq
558 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
559 time_before(next
->start_time
, rq
->start_time
))
560 list_move(&rq
->queuelist
, &next
->queuelist
);
562 cfq_remove_request(next
);
566 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
570 * stop potential idle class queues waiting service
572 del_timer(&cfqd
->idle_class_timer
);
574 cfqq
->slice_start
= jiffies
;
576 cfqq
->slice_left
= 0;
577 cfq_clear_cfqq_must_alloc_slice(cfqq
);
578 cfq_clear_cfqq_fifo_expire(cfqq
);
581 cfqd
->active_queue
= cfqq
;
585 * current cfqq expired its slice (or was too idle), select new one
588 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
591 unsigned long now
= jiffies
;
593 if (cfq_cfqq_wait_request(cfqq
))
594 del_timer(&cfqd
->idle_slice_timer
);
596 if (!preempted
&& !cfq_cfqq_dispatched(cfqq
)) {
597 cfqq
->service_last
= now
;
598 cfq_schedule_dispatch(cfqd
);
601 cfq_clear_cfqq_must_dispatch(cfqq
);
602 cfq_clear_cfqq_wait_request(cfqq
);
605 * store what was left of this slice, if the queue idled out
608 if (time_after(cfqq
->slice_end
, now
))
609 cfqq
->slice_left
= cfqq
->slice_end
- now
;
611 cfqq
->slice_left
= 0;
613 if (cfq_cfqq_on_rr(cfqq
))
614 cfq_resort_rr_list(cfqq
, preempted
);
616 if (cfqq
== cfqd
->active_queue
)
617 cfqd
->active_queue
= NULL
;
619 if (cfqd
->active_cic
) {
620 put_io_context(cfqd
->active_cic
->ioc
);
621 cfqd
->active_cic
= NULL
;
624 cfqd
->dispatch_slice
= 0;
627 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int preempted
)
629 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
632 __cfq_slice_expired(cfqd
, cfqq
, preempted
);
645 static int cfq_get_next_prio_level(struct cfq_data
*cfqd
)
654 for (p
= cfqd
->cur_prio
; p
<= cfqd
->cur_end_prio
; p
++) {
655 if (!list_empty(&cfqd
->rr_list
[p
])) {
664 if (++cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
665 cfqd
->cur_end_prio
= 0;
672 if (unlikely(prio
== -1))
675 BUG_ON(prio
>= CFQ_PRIO_LISTS
);
677 list_splice_init(&cfqd
->rr_list
[prio
], &cfqd
->cur_rr
);
679 cfqd
->cur_prio
= prio
+ 1;
680 if (cfqd
->cur_prio
> cfqd
->cur_end_prio
) {
681 cfqd
->cur_end_prio
= cfqd
->cur_prio
;
684 if (cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
686 cfqd
->cur_end_prio
= 0;
692 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
694 struct cfq_queue
*cfqq
= NULL
;
696 if (!list_empty(&cfqd
->cur_rr
) || cfq_get_next_prio_level(cfqd
) != -1) {
698 * if current list is non-empty, grab first entry. if it is
699 * empty, get next prio level and grab first entry then if any
702 cfqq
= list_entry_cfqq(cfqd
->cur_rr
.next
);
703 } else if (!list_empty(&cfqd
->busy_rr
)) {
705 * If no new queues are available, check if the busy list has
706 * some before falling back to idle io.
708 cfqq
= list_entry_cfqq(cfqd
->busy_rr
.next
);
709 } else if (!list_empty(&cfqd
->idle_rr
)) {
711 * if we have idle queues and no rt or be queues had pending
712 * requests, either allow immediate service if the grace period
713 * has passed or arm the idle grace timer
715 unsigned long end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
717 if (time_after_eq(jiffies
, end
))
718 cfqq
= list_entry_cfqq(cfqd
->idle_rr
.next
);
720 mod_timer(&cfqd
->idle_class_timer
, end
);
723 __cfq_set_active_queue(cfqd
, cfqq
);
727 #define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
729 static int cfq_arm_slice_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
732 struct cfq_io_context
*cic
;
735 WARN_ON(!RB_EMPTY_ROOT(&cfqq
->sort_list
));
736 WARN_ON(cfqq
!= cfqd
->active_queue
);
739 * idle is disabled, either manually or by past process history
741 if (!cfqd
->cfq_slice_idle
)
743 if (!cfq_cfqq_idle_window(cfqq
))
746 * task has exited, don't wait
748 cic
= cfqd
->active_cic
;
749 if (!cic
|| !cic
->ioc
->task
)
752 cfq_mark_cfqq_must_dispatch(cfqq
);
753 cfq_mark_cfqq_wait_request(cfqq
);
755 sl
= min(cfqq
->slice_end
- 1, (unsigned long) cfqd
->cfq_slice_idle
);
758 * we don't want to idle for seeks, but we do want to allow
759 * fair distribution of slice time for a process doing back-to-back
760 * seeks. so allow a little bit of time for him to submit a new rq
762 if (sample_valid(cic
->seek_samples
) && CIC_SEEKY(cic
))
763 sl
= min(sl
, msecs_to_jiffies(2));
765 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
769 static void cfq_dispatch_insert(request_queue_t
*q
, struct request
*rq
)
771 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
772 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
774 cfq_remove_request(rq
);
775 cfqq
->on_dispatch
[rq_is_sync(rq
)]++;
776 elv_dispatch_sort(q
, rq
);
778 rq
= list_entry(q
->queue_head
.prev
, struct request
, queuelist
);
779 cfqd
->last_sector
= rq
->sector
+ rq
->nr_sectors
;
783 * return expired entry, or NULL to just start from scratch in rbtree
785 static inline struct request
*cfq_check_fifo(struct cfq_queue
*cfqq
)
787 struct cfq_data
*cfqd
= cfqq
->cfqd
;
791 if (cfq_cfqq_fifo_expire(cfqq
))
793 if (list_empty(&cfqq
->fifo
))
796 fifo
= cfq_cfqq_class_sync(cfqq
);
797 rq
= rq_entry_fifo(cfqq
->fifo
.next
);
799 if (time_after(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
])) {
800 cfq_mark_cfqq_fifo_expire(cfqq
);
808 * Scale schedule slice based on io priority. Use the sync time slice only
809 * if a queue is marked sync and has sync io queued. A sync queue with async
810 * io only, should not get full sync slice length.
813 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
815 const int base_slice
= cfqd
->cfq_slice
[cfq_cfqq_sync(cfqq
)];
817 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
819 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - cfqq
->ioprio
));
823 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
825 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
829 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
831 const int base_rq
= cfqd
->cfq_slice_async_rq
;
833 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
835 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
839 * get next queue for service
841 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
843 unsigned long now
= jiffies
;
844 struct cfq_queue
*cfqq
;
846 cfqq
= cfqd
->active_queue
;
853 if (!cfq_cfqq_must_dispatch(cfqq
) && time_after(now
, cfqq
->slice_end
))
857 * if queue has requests, dispatch one. if not, check if
858 * enough slice is left to wait for one
860 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
862 else if (cfq_cfqq_dispatched(cfqq
)) {
865 } else if (cfq_cfqq_class_sync(cfqq
)) {
866 if (cfq_arm_slice_timer(cfqd
, cfqq
))
871 cfq_slice_expired(cfqd
, 0);
873 cfqq
= cfq_set_active_queue(cfqd
);
879 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
884 BUG_ON(RB_EMPTY_ROOT(&cfqq
->sort_list
));
890 * follow expired path, else get first next available
892 if ((rq
= cfq_check_fifo(cfqq
)) == NULL
)
896 * finally, insert request into driver dispatch list
898 cfq_dispatch_insert(cfqd
->queue
, rq
);
900 cfqd
->dispatch_slice
++;
903 if (!cfqd
->active_cic
) {
904 atomic_inc(&RQ_CIC(rq
)->ioc
->refcount
);
905 cfqd
->active_cic
= RQ_CIC(rq
);
908 if (RB_EMPTY_ROOT(&cfqq
->sort_list
))
911 } while (dispatched
< max_dispatch
);
914 * if slice end isn't set yet, set it.
916 if (!cfqq
->slice_end
)
917 cfq_set_prio_slice(cfqd
, cfqq
);
920 * expire an async queue immediately if it has used up its slice. idle
921 * queue always expire after 1 dispatch round.
923 if ((!cfq_cfqq_sync(cfqq
) &&
924 cfqd
->dispatch_slice
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
925 cfq_class_idle(cfqq
) ||
926 !cfq_cfqq_idle_window(cfqq
))
927 cfq_slice_expired(cfqd
, 0);
933 cfq_forced_dispatch_cfqqs(struct list_head
*list
)
935 struct cfq_queue
*cfqq
, *next
;
939 list_for_each_entry_safe(cfqq
, next
, list
, cfq_list
) {
940 while (cfqq
->next_rq
) {
941 cfq_dispatch_insert(cfqq
->cfqd
->queue
, cfqq
->next_rq
);
944 BUG_ON(!list_empty(&cfqq
->fifo
));
951 cfq_forced_dispatch(struct cfq_data
*cfqd
)
953 int i
, dispatched
= 0;
955 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
956 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->rr_list
[i
]);
958 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->busy_rr
);
959 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->cur_rr
);
960 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->idle_rr
);
962 cfq_slice_expired(cfqd
, 0);
964 BUG_ON(cfqd
->busy_queues
);
970 cfq_dispatch_requests(request_queue_t
*q
, int force
)
972 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
973 struct cfq_queue
*cfqq
, *prev_cfqq
;
976 if (!cfqd
->busy_queues
)
980 return cfq_forced_dispatch(cfqd
);
984 while ((cfqq
= cfq_select_queue(cfqd
)) != NULL
) {
988 * Don't repeat dispatch from the previous queue.
990 if (prev_cfqq
== cfqq
)
993 cfq_clear_cfqq_must_dispatch(cfqq
);
994 cfq_clear_cfqq_wait_request(cfqq
);
995 del_timer(&cfqd
->idle_slice_timer
);
997 max_dispatch
= cfqd
->cfq_quantum
;
998 if (cfq_class_idle(cfqq
))
1001 dispatched
+= __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1004 * If the dispatch cfqq has idling enabled and is still
1005 * the active queue, break out.
1007 if (cfq_cfqq_idle_window(cfqq
) && cfqd
->active_queue
)
1017 * task holds one reference to the queue, dropped when task exits. each rq
1018 * in-flight on this queue also holds a reference, dropped when rq is freed.
1020 * queue lock must be held here.
1022 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1024 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1026 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1028 if (!atomic_dec_and_test(&cfqq
->ref
))
1031 BUG_ON(rb_first(&cfqq
->sort_list
));
1032 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1033 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1035 if (unlikely(cfqd
->active_queue
== cfqq
))
1036 __cfq_slice_expired(cfqd
, cfqq
, 0);
1039 * it's on the empty list and still hashed
1041 list_del(&cfqq
->cfq_list
);
1042 hlist_del(&cfqq
->cfq_hash
);
1043 kmem_cache_free(cfq_pool
, cfqq
);
1046 static inline struct cfq_queue
*
1047 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1050 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1051 struct hlist_node
*entry
;
1052 struct cfq_queue
*__cfqq
;
1054 hlist_for_each_entry(__cfqq
, entry
, hash_list
, cfq_hash
) {
1055 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->org_ioprio_class
, __cfqq
->org_ioprio
);
1057 if (__cfqq
->key
== key
&& (__p
== prio
|| !prio
))
1064 static struct cfq_queue
*
1065 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1067 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1070 static void cfq_free_io_context(struct io_context
*ioc
)
1072 struct cfq_io_context
*__cic
;
1076 while ((n
= rb_first(&ioc
->cic_root
)) != NULL
) {
1077 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1078 rb_erase(&__cic
->rb_node
, &ioc
->cic_root
);
1079 kmem_cache_free(cfq_ioc_pool
, __cic
);
1083 if (atomic_sub_and_test(freed
, &ioc_count
) && ioc_gone
)
1087 static void cfq_trim(struct io_context
*ioc
)
1089 ioc
->set_ioprio
= NULL
;
1090 cfq_free_io_context(ioc
);
1093 static void cfq_exit_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1095 if (unlikely(cfqq
== cfqd
->active_queue
))
1096 __cfq_slice_expired(cfqd
, cfqq
, 0);
1098 cfq_put_queue(cfqq
);
1101 static void __cfq_exit_single_io_context(struct cfq_data
*cfqd
,
1102 struct cfq_io_context
*cic
)
1104 if (cic
->cfqq
[ASYNC
]) {
1105 cfq_exit_cfqq(cfqd
, cic
->cfqq
[ASYNC
]);
1106 cic
->cfqq
[ASYNC
] = NULL
;
1109 if (cic
->cfqq
[SYNC
]) {
1110 cfq_exit_cfqq(cfqd
, cic
->cfqq
[SYNC
]);
1111 cic
->cfqq
[SYNC
] = NULL
;
1115 list_del_init(&cic
->queue_list
);
1120 * Called with interrupts disabled
1122 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1124 struct cfq_data
*cfqd
= cic
->key
;
1126 WARN_ON(!irqs_disabled());
1129 request_queue_t
*q
= cfqd
->queue
;
1131 spin_lock(q
->queue_lock
);
1132 __cfq_exit_single_io_context(cfqd
, cic
);
1133 spin_unlock(q
->queue_lock
);
1137 static void cfq_exit_io_context(struct io_context
*ioc
)
1139 struct cfq_io_context
*__cic
;
1140 unsigned long flags
;
1144 * put the reference this task is holding to the various queues
1146 spin_lock_irqsave(&cfq_exit_lock
, flags
);
1148 n
= rb_first(&ioc
->cic_root
);
1150 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1152 cfq_exit_single_io_context(__cic
);
1156 spin_unlock_irqrestore(&cfq_exit_lock
, flags
);
1159 static struct cfq_io_context
*
1160 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1162 struct cfq_io_context
*cic
= kmem_cache_alloc(cfq_ioc_pool
, gfp_mask
);
1165 memset(cic
, 0, sizeof(*cic
));
1166 cic
->last_end_request
= jiffies
;
1167 INIT_LIST_HEAD(&cic
->queue_list
);
1168 cic
->dtor
= cfq_free_io_context
;
1169 cic
->exit
= cfq_exit_io_context
;
1170 atomic_inc(&ioc_count
);
1176 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1178 struct task_struct
*tsk
= current
;
1181 if (!cfq_cfqq_prio_changed(cfqq
))
1184 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1185 switch (ioprio_class
) {
1187 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1188 case IOPRIO_CLASS_NONE
:
1190 * no prio set, place us in the middle of the BE classes
1192 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1193 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1195 case IOPRIO_CLASS_RT
:
1196 cfqq
->ioprio
= task_ioprio(tsk
);
1197 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1199 case IOPRIO_CLASS_BE
:
1200 cfqq
->ioprio
= task_ioprio(tsk
);
1201 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1203 case IOPRIO_CLASS_IDLE
:
1204 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1206 cfq_clear_cfqq_idle_window(cfqq
);
1211 * keep track of original prio settings in case we have to temporarily
1212 * elevate the priority of this queue
1214 cfqq
->org_ioprio
= cfqq
->ioprio
;
1215 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1217 if (cfq_cfqq_on_rr(cfqq
))
1218 cfq_resort_rr_list(cfqq
, 0);
1220 cfq_clear_cfqq_prio_changed(cfqq
);
1223 static inline void changed_ioprio(struct cfq_io_context
*cic
)
1225 struct cfq_data
*cfqd
= cic
->key
;
1226 struct cfq_queue
*cfqq
;
1228 if (unlikely(!cfqd
))
1231 spin_lock(cfqd
->queue
->queue_lock
);
1233 cfqq
= cic
->cfqq
[ASYNC
];
1235 struct cfq_queue
*new_cfqq
;
1236 new_cfqq
= cfq_get_queue(cfqd
, CFQ_KEY_ASYNC
, cic
->ioc
->task
,
1239 cic
->cfqq
[ASYNC
] = new_cfqq
;
1240 cfq_put_queue(cfqq
);
1244 cfqq
= cic
->cfqq
[SYNC
];
1246 cfq_mark_cfqq_prio_changed(cfqq
);
1248 spin_unlock(cfqd
->queue
->queue_lock
);
1252 * callback from sys_ioprio_set, irqs are disabled
1254 static int cfq_ioc_set_ioprio(struct io_context
*ioc
, unsigned int ioprio
)
1256 struct cfq_io_context
*cic
;
1259 spin_lock(&cfq_exit_lock
);
1261 n
= rb_first(&ioc
->cic_root
);
1263 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1265 changed_ioprio(cic
);
1269 spin_unlock(&cfq_exit_lock
);
1274 static struct cfq_queue
*
1275 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
,
1278 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1279 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1280 unsigned short ioprio
;
1283 ioprio
= tsk
->ioprio
;
1284 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1290 } else if (gfp_mask
& __GFP_WAIT
) {
1292 * Inform the allocator of the fact that we will
1293 * just repeat this allocation if it fails, to allow
1294 * the allocator to do whatever it needs to attempt to
1297 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1298 new_cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
|__GFP_NOFAIL
);
1299 spin_lock_irq(cfqd
->queue
->queue_lock
);
1302 cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1307 memset(cfqq
, 0, sizeof(*cfqq
));
1309 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1310 INIT_LIST_HEAD(&cfqq
->cfq_list
);
1311 INIT_LIST_HEAD(&cfqq
->fifo
);
1314 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1315 atomic_set(&cfqq
->ref
, 0);
1317 cfqq
->service_last
= 0;
1319 * set ->slice_left to allow preemption for a new process
1321 cfqq
->slice_left
= 2 * cfqd
->cfq_slice_idle
;
1322 cfq_mark_cfqq_idle_window(cfqq
);
1323 cfq_mark_cfqq_prio_changed(cfqq
);
1324 cfq_init_prio_data(cfqq
);
1328 kmem_cache_free(cfq_pool
, new_cfqq
);
1330 atomic_inc(&cfqq
->ref
);
1332 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1337 cfq_drop_dead_cic(struct io_context
*ioc
, struct cfq_io_context
*cic
)
1339 spin_lock(&cfq_exit_lock
);
1340 rb_erase(&cic
->rb_node
, &ioc
->cic_root
);
1341 list_del_init(&cic
->queue_list
);
1342 spin_unlock(&cfq_exit_lock
);
1343 kmem_cache_free(cfq_ioc_pool
, cic
);
1344 atomic_dec(&ioc_count
);
1347 static struct cfq_io_context
*
1348 cfq_cic_rb_lookup(struct cfq_data
*cfqd
, struct io_context
*ioc
)
1351 struct cfq_io_context
*cic
;
1352 void *k
, *key
= cfqd
;
1355 n
= ioc
->cic_root
.rb_node
;
1357 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1358 /* ->key must be copied to avoid race with cfq_exit_queue() */
1361 cfq_drop_dead_cic(ioc
, cic
);
1377 cfq_cic_link(struct cfq_data
*cfqd
, struct io_context
*ioc
,
1378 struct cfq_io_context
*cic
)
1381 struct rb_node
*parent
;
1382 struct cfq_io_context
*__cic
;
1388 ioc
->set_ioprio
= cfq_ioc_set_ioprio
;
1391 p
= &ioc
->cic_root
.rb_node
;
1394 __cic
= rb_entry(parent
, struct cfq_io_context
, rb_node
);
1395 /* ->key must be copied to avoid race with cfq_exit_queue() */
1398 cfq_drop_dead_cic(ioc
, __cic
);
1404 else if (cic
->key
> k
)
1405 p
= &(*p
)->rb_right
;
1410 spin_lock(&cfq_exit_lock
);
1411 rb_link_node(&cic
->rb_node
, parent
, p
);
1412 rb_insert_color(&cic
->rb_node
, &ioc
->cic_root
);
1413 list_add(&cic
->queue_list
, &cfqd
->cic_list
);
1414 spin_unlock(&cfq_exit_lock
);
1418 * Setup general io context and cfq io context. There can be several cfq
1419 * io contexts per general io context, if this process is doing io to more
1420 * than one device managed by cfq.
1422 static struct cfq_io_context
*
1423 cfq_get_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1425 struct io_context
*ioc
= NULL
;
1426 struct cfq_io_context
*cic
;
1428 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1430 ioc
= get_io_context(gfp_mask
);
1434 cic
= cfq_cic_rb_lookup(cfqd
, ioc
);
1438 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1442 cfq_cic_link(cfqd
, ioc
, cic
);
1446 put_io_context(ioc
);
1451 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1453 unsigned long elapsed
, ttime
;
1456 * if this context already has stuff queued, thinktime is from
1457 * last queue not last end
1460 if (time_after(cic
->last_end_request
, cic
->last_queue
))
1461 elapsed
= jiffies
- cic
->last_end_request
;
1463 elapsed
= jiffies
- cic
->last_queue
;
1465 elapsed
= jiffies
- cic
->last_end_request
;
1468 ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1470 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1471 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1472 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1476 cfq_update_io_seektime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
,
1482 if (cic
->last_request_pos
< rq
->sector
)
1483 sdist
= rq
->sector
- cic
->last_request_pos
;
1485 sdist
= cic
->last_request_pos
- rq
->sector
;
1488 * Don't allow the seek distance to get too large from the
1489 * odd fragment, pagein, etc
1491 if (cic
->seek_samples
<= 60) /* second&third seek */
1492 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*1024);
1494 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*64);
1496 cic
->seek_samples
= (7*cic
->seek_samples
+ 256) / 8;
1497 cic
->seek_total
= (7*cic
->seek_total
+ (u64
)256*sdist
) / 8;
1498 total
= cic
->seek_total
+ (cic
->seek_samples
/2);
1499 do_div(total
, cic
->seek_samples
);
1500 cic
->seek_mean
= (sector_t
)total
;
1504 * Disable idle window if the process thinks too long or seeks so much that
1508 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1509 struct cfq_io_context
*cic
)
1511 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1513 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
||
1514 (cfqd
->hw_tag
&& CIC_SEEKY(cic
)))
1516 else if (sample_valid(cic
->ttime_samples
)) {
1517 if (cic
->ttime_mean
> cfqd
->cfq_slice_idle
)
1524 cfq_mark_cfqq_idle_window(cfqq
);
1526 cfq_clear_cfqq_idle_window(cfqq
);
1531 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1532 * no or if we aren't sure, a 1 will cause a preempt.
1535 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1538 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
1540 if (cfq_class_idle(new_cfqq
))
1546 if (cfq_class_idle(cfqq
))
1548 if (!cfq_cfqq_wait_request(new_cfqq
))
1551 * if it doesn't have slice left, forget it
1553 if (new_cfqq
->slice_left
< cfqd
->cfq_slice_idle
)
1555 if (rq_is_sync(rq
) && !cfq_cfqq_sync(cfqq
))
1562 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1563 * let it have half of its nominal slice.
1565 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1567 struct cfq_queue
*__cfqq
, *next
;
1569 list_for_each_entry_safe(__cfqq
, next
, &cfqd
->cur_rr
, cfq_list
)
1570 cfq_resort_rr_list(__cfqq
, 1);
1572 if (!cfqq
->slice_left
)
1573 cfqq
->slice_left
= cfq_prio_to_slice(cfqd
, cfqq
) / 2;
1575 cfqq
->slice_end
= cfqq
->slice_left
+ jiffies
;
1576 cfq_slice_expired(cfqd
, 1);
1577 __cfq_set_active_queue(cfqd
, cfqq
);
1581 * should really be a ll_rw_blk.c helper
1583 static void cfq_start_queueing(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1585 request_queue_t
*q
= cfqd
->queue
;
1587 if (!blk_queue_plugged(q
))
1590 __generic_unplug_device(q
);
1594 * Called when a new fs request (rq) is added (to cfqq). Check if there's
1595 * something we should do about it
1598 cfq_rq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1601 struct cfq_io_context
*cic
= RQ_CIC(rq
);
1604 * check if this request is a better next-serve candidate)) {
1606 cfqq
->next_rq
= cfq_choose_req(cfqd
, cfqq
->next_rq
, rq
);
1607 BUG_ON(!cfqq
->next_rq
);
1610 * we never wait for an async request and we don't allow preemption
1611 * of an async request. so just return early
1613 if (!rq_is_sync(rq
)) {
1615 * sync process issued an async request, if it's waiting
1616 * then expire it and kick rq handling.
1618 if (cic
== cfqd
->active_cic
&&
1619 del_timer(&cfqd
->idle_slice_timer
)) {
1620 cfq_slice_expired(cfqd
, 0);
1621 cfq_start_queueing(cfqd
, cfqq
);
1626 cfq_update_io_thinktime(cfqd
, cic
);
1627 cfq_update_io_seektime(cfqd
, cic
, rq
);
1628 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1630 cic
->last_queue
= jiffies
;
1631 cic
->last_request_pos
= rq
->sector
+ rq
->nr_sectors
;
1633 if (cfqq
== cfqd
->active_queue
) {
1635 * if we are waiting for a request for this queue, let it rip
1636 * immediately and flag that we must not expire this queue
1639 if (cfq_cfqq_wait_request(cfqq
)) {
1640 cfq_mark_cfqq_must_dispatch(cfqq
);
1641 del_timer(&cfqd
->idle_slice_timer
);
1642 cfq_start_queueing(cfqd
, cfqq
);
1644 } else if (cfq_should_preempt(cfqd
, cfqq
, rq
)) {
1646 * not the active queue - expire current slice if it is
1647 * idle and has expired it's mean thinktime or this new queue
1648 * has some old slice time left and is of higher priority
1650 cfq_preempt_queue(cfqd
, cfqq
);
1651 cfq_mark_cfqq_must_dispatch(cfqq
);
1652 cfq_start_queueing(cfqd
, cfqq
);
1656 static void cfq_insert_request(request_queue_t
*q
, struct request
*rq
)
1658 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1659 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
1661 cfq_init_prio_data(cfqq
);
1665 if (!cfq_cfqq_on_rr(cfqq
))
1666 cfq_add_cfqq_rr(cfqd
, cfqq
);
1668 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1670 cfq_rq_enqueued(cfqd
, cfqq
, rq
);
1673 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1675 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
1676 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1677 const int sync
= rq_is_sync(rq
);
1682 WARN_ON(!cfqd
->rq_in_driver
);
1683 WARN_ON(!cfqq
->on_dispatch
[sync
]);
1684 cfqd
->rq_in_driver
--;
1685 cfqq
->on_dispatch
[sync
]--;
1687 if (!cfq_class_idle(cfqq
))
1688 cfqd
->last_end_request
= now
;
1690 if (!cfq_cfqq_dispatched(cfqq
)) {
1691 if (cfq_cfqq_on_rr(cfqq
)) {
1692 cfqq
->service_last
= now
;
1693 cfq_resort_rr_list(cfqq
, 0);
1698 RQ_CIC(rq
)->last_end_request
= now
;
1701 * If this is the active queue, check if it needs to be expired,
1702 * or if we want to idle in case it has no pending requests.
1704 if (cfqd
->active_queue
== cfqq
) {
1705 if (time_after(now
, cfqq
->slice_end
))
1706 cfq_slice_expired(cfqd
, 0);
1707 else if (sync
&& RB_EMPTY_ROOT(&cfqq
->sort_list
)) {
1708 if (!cfq_arm_slice_timer(cfqd
, cfqq
))
1709 cfq_schedule_dispatch(cfqd
);
1715 * we temporarily boost lower priority queues if they are holding fs exclusive
1716 * resources. they are boosted to normal prio (CLASS_BE/4)
1718 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1720 const int ioprio_class
= cfqq
->ioprio_class
;
1721 const int ioprio
= cfqq
->ioprio
;
1723 if (has_fs_excl()) {
1725 * boost idle prio on transactions that would lock out other
1726 * users of the filesystem
1728 if (cfq_class_idle(cfqq
))
1729 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1730 if (cfqq
->ioprio
> IOPRIO_NORM
)
1731 cfqq
->ioprio
= IOPRIO_NORM
;
1734 * check if we need to unboost the queue
1736 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1737 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1738 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1739 cfqq
->ioprio
= cfqq
->org_ioprio
;
1743 * refile between round-robin lists if we moved the priority class
1745 if ((ioprio_class
!= cfqq
->ioprio_class
|| ioprio
!= cfqq
->ioprio
) &&
1746 cfq_cfqq_on_rr(cfqq
))
1747 cfq_resort_rr_list(cfqq
, 0);
1750 static inline int __cfq_may_queue(struct cfq_queue
*cfqq
)
1752 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1753 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1754 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1755 return ELV_MQUEUE_MUST
;
1758 return ELV_MQUEUE_MAY
;
1761 static int cfq_may_queue(request_queue_t
*q
, int rw
)
1763 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1764 struct task_struct
*tsk
= current
;
1765 struct cfq_queue
*cfqq
;
1768 * don't force setup of a queue from here, as a call to may_queue
1769 * does not necessarily imply that a request actually will be queued.
1770 * so just lookup a possibly existing queue, or return 'may queue'
1773 cfqq
= cfq_find_cfq_hash(cfqd
, cfq_queue_pid(tsk
, rw
), tsk
->ioprio
);
1775 cfq_init_prio_data(cfqq
);
1776 cfq_prio_boost(cfqq
);
1778 return __cfq_may_queue(cfqq
);
1781 return ELV_MQUEUE_MAY
;
1785 * queue lock held here
1787 static void cfq_put_request(request_queue_t
*q
, struct request
*rq
)
1789 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
1792 const int rw
= rq_data_dir(rq
);
1794 BUG_ON(!cfqq
->allocated
[rw
]);
1795 cfqq
->allocated
[rw
]--;
1797 put_io_context(RQ_CIC(rq
)->ioc
);
1799 rq
->elevator_private
= NULL
;
1800 rq
->elevator_private2
= NULL
;
1802 cfq_put_queue(cfqq
);
1807 * Allocate cfq data structures associated with this request.
1810 cfq_set_request(request_queue_t
*q
, struct request
*rq
, gfp_t gfp_mask
)
1812 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1813 struct task_struct
*tsk
= current
;
1814 struct cfq_io_context
*cic
;
1815 const int rw
= rq_data_dir(rq
);
1816 pid_t key
= cfq_queue_pid(tsk
, rw
);
1817 struct cfq_queue
*cfqq
;
1818 unsigned long flags
;
1819 int is_sync
= key
!= CFQ_KEY_ASYNC
;
1821 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1823 cic
= cfq_get_io_context(cfqd
, gfp_mask
);
1825 spin_lock_irqsave(q
->queue_lock
, flags
);
1830 if (!cic
->cfqq
[is_sync
]) {
1831 cfqq
= cfq_get_queue(cfqd
, key
, tsk
, gfp_mask
);
1835 cic
->cfqq
[is_sync
] = cfqq
;
1837 cfqq
= cic
->cfqq
[is_sync
];
1839 cfqq
->allocated
[rw
]++;
1840 cfq_clear_cfqq_must_alloc(cfqq
);
1841 atomic_inc(&cfqq
->ref
);
1843 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1845 rq
->elevator_private
= cic
;
1846 rq
->elevator_private2
= cfqq
;
1851 put_io_context(cic
->ioc
);
1853 cfq_schedule_dispatch(cfqd
);
1854 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1858 static void cfq_kick_queue(void *data
)
1860 request_queue_t
*q
= data
;
1861 unsigned long flags
;
1863 spin_lock_irqsave(q
->queue_lock
, flags
);
1866 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1870 * Timer running if the active_queue is currently idling inside its time slice
1872 static void cfq_idle_slice_timer(unsigned long data
)
1874 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
1875 struct cfq_queue
*cfqq
;
1876 unsigned long flags
;
1878 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1880 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
1881 unsigned long now
= jiffies
;
1886 if (time_after(now
, cfqq
->slice_end
))
1890 * only expire and reinvoke request handler, if there are
1891 * other queues with pending requests
1893 if (!cfqd
->busy_queues
)
1897 * not expired and it has a request pending, let it dispatch
1899 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
)) {
1900 cfq_mark_cfqq_must_dispatch(cfqq
);
1905 cfq_slice_expired(cfqd
, 0);
1907 cfq_schedule_dispatch(cfqd
);
1909 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
1913 * Timer running if an idle class queue is waiting for service
1915 static void cfq_idle_class_timer(unsigned long data
)
1917 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
1918 unsigned long flags
, end
;
1920 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1923 * race with a non-idle queue, reset timer
1925 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
1926 if (!time_after_eq(jiffies
, end
))
1927 mod_timer(&cfqd
->idle_class_timer
, end
);
1929 cfq_schedule_dispatch(cfqd
);
1931 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
1934 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
1936 del_timer_sync(&cfqd
->idle_slice_timer
);
1937 del_timer_sync(&cfqd
->idle_class_timer
);
1938 blk_sync_queue(cfqd
->queue
);
1941 static void cfq_exit_queue(elevator_t
*e
)
1943 struct cfq_data
*cfqd
= e
->elevator_data
;
1944 request_queue_t
*q
= cfqd
->queue
;
1946 cfq_shutdown_timer_wq(cfqd
);
1948 spin_lock(&cfq_exit_lock
);
1949 spin_lock_irq(q
->queue_lock
);
1951 if (cfqd
->active_queue
)
1952 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
1954 while (!list_empty(&cfqd
->cic_list
)) {
1955 struct cfq_io_context
*cic
= list_entry(cfqd
->cic_list
.next
,
1956 struct cfq_io_context
,
1959 __cfq_exit_single_io_context(cfqd
, cic
);
1962 spin_unlock_irq(q
->queue_lock
);
1963 spin_unlock(&cfq_exit_lock
);
1965 cfq_shutdown_timer_wq(cfqd
);
1967 kfree(cfqd
->cfq_hash
);
1971 static void *cfq_init_queue(request_queue_t
*q
, elevator_t
*e
)
1973 struct cfq_data
*cfqd
;
1976 cfqd
= kmalloc(sizeof(*cfqd
), GFP_KERNEL
);
1980 memset(cfqd
, 0, sizeof(*cfqd
));
1982 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
1983 INIT_LIST_HEAD(&cfqd
->rr_list
[i
]);
1985 INIT_LIST_HEAD(&cfqd
->busy_rr
);
1986 INIT_LIST_HEAD(&cfqd
->cur_rr
);
1987 INIT_LIST_HEAD(&cfqd
->idle_rr
);
1988 INIT_LIST_HEAD(&cfqd
->empty_list
);
1989 INIT_LIST_HEAD(&cfqd
->cic_list
);
1991 cfqd
->cfq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
);
1992 if (!cfqd
->cfq_hash
)
1995 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
1996 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2000 init_timer(&cfqd
->idle_slice_timer
);
2001 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2002 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2004 init_timer(&cfqd
->idle_class_timer
);
2005 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2006 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2008 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
, q
);
2010 cfqd
->cfq_quantum
= cfq_quantum
;
2011 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2012 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2013 cfqd
->cfq_back_max
= cfq_back_max
;
2014 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2015 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2016 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2017 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2018 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2026 static void cfq_slab_kill(void)
2029 kmem_cache_destroy(cfq_pool
);
2031 kmem_cache_destroy(cfq_ioc_pool
);
2034 static int __init
cfq_slab_setup(void)
2036 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2041 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2042 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2053 * sysfs parts below -->
2057 cfq_var_show(unsigned int var
, char *page
)
2059 return sprintf(page
, "%d\n", var
);
2063 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2065 char *p
= (char *) page
;
2067 *var
= simple_strtoul(p
, &p
, 10);
2071 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2072 static ssize_t __FUNC(elevator_t *e, char *page) \
2074 struct cfq_data *cfqd = e->elevator_data; \
2075 unsigned int __data = __VAR; \
2077 __data = jiffies_to_msecs(__data); \
2078 return cfq_var_show(__data, (page)); \
2080 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2081 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2082 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2083 SHOW_FUNCTION(cfq_back_seek_max_show
, cfqd
->cfq_back_max
, 0);
2084 SHOW_FUNCTION(cfq_back_seek_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2085 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2086 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2087 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2088 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2089 #undef SHOW_FUNCTION
2091 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2092 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2094 struct cfq_data *cfqd = e->elevator_data; \
2095 unsigned int __data; \
2096 int ret = cfq_var_store(&__data, (page), count); \
2097 if (__data < (MIN)) \
2099 else if (__data > (MAX)) \
2102 *(__PTR) = msecs_to_jiffies(__data); \
2104 *(__PTR) = __data; \
2107 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2108 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2109 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2110 STORE_FUNCTION(cfq_back_seek_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2111 STORE_FUNCTION(cfq_back_seek_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2112 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2113 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2114 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2115 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2116 #undef STORE_FUNCTION
2118 #define CFQ_ATTR(name) \
2119 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2121 static struct elv_fs_entry cfq_attrs
[] = {
2123 CFQ_ATTR(fifo_expire_sync
),
2124 CFQ_ATTR(fifo_expire_async
),
2125 CFQ_ATTR(back_seek_max
),
2126 CFQ_ATTR(back_seek_penalty
),
2127 CFQ_ATTR(slice_sync
),
2128 CFQ_ATTR(slice_async
),
2129 CFQ_ATTR(slice_async_rq
),
2130 CFQ_ATTR(slice_idle
),
2134 static struct elevator_type iosched_cfq
= {
2136 .elevator_merge_fn
= cfq_merge
,
2137 .elevator_merged_fn
= cfq_merged_request
,
2138 .elevator_merge_req_fn
= cfq_merged_requests
,
2139 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2140 .elevator_add_req_fn
= cfq_insert_request
,
2141 .elevator_activate_req_fn
= cfq_activate_request
,
2142 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2143 .elevator_queue_empty_fn
= cfq_queue_empty
,
2144 .elevator_completed_req_fn
= cfq_completed_request
,
2145 .elevator_former_req_fn
= elv_rb_former_request
,
2146 .elevator_latter_req_fn
= elv_rb_latter_request
,
2147 .elevator_set_req_fn
= cfq_set_request
,
2148 .elevator_put_req_fn
= cfq_put_request
,
2149 .elevator_may_queue_fn
= cfq_may_queue
,
2150 .elevator_init_fn
= cfq_init_queue
,
2151 .elevator_exit_fn
= cfq_exit_queue
,
2154 .elevator_attrs
= cfq_attrs
,
2155 .elevator_name
= "cfq",
2156 .elevator_owner
= THIS_MODULE
,
2159 static int __init
cfq_init(void)
2164 * could be 0 on HZ < 1000 setups
2166 if (!cfq_slice_async
)
2167 cfq_slice_async
= 1;
2168 if (!cfq_slice_idle
)
2171 if (cfq_slab_setup())
2174 ret
= elv_register(&iosched_cfq
);
2181 static void __exit
cfq_exit(void)
2183 DECLARE_COMPLETION(all_gone
);
2184 elv_unregister(&iosched_cfq
);
2185 ioc_gone
= &all_gone
;
2186 /* ioc_gone's update must be visible before reading ioc_count */
2188 if (atomic_read(&ioc_count
))
2189 wait_for_completion(ioc_gone
);
2194 module_init(cfq_init
);
2195 module_exit(cfq_exit
);
2197 MODULE_AUTHOR("Jens Axboe");
2198 MODULE_LICENSE("GPL");
2199 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");