2 * CFQ, or complete fairness queueing, disk scheduler.
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/blkdev.h>
12 #include <linux/elevator.h>
13 #include <linux/hash.h>
14 #include <linux/rbtree.h>
15 #include <linux/ioprio.h>
20 static const int cfq_quantum
= 4; /* max queue in one round of service */
21 static const int cfq_queued
= 8; /* minimum rq allocate limit per-queue*/
22 static const int cfq_fifo_expire
[2] = { HZ
/ 4, HZ
/ 8 };
23 static const int cfq_back_max
= 16 * 1024; /* maximum backwards seek, in KiB */
24 static const int cfq_back_penalty
= 2; /* penalty of a backwards seek */
26 static const int cfq_slice_sync
= HZ
/ 10;
27 static int cfq_slice_async
= HZ
/ 25;
28 static const int cfq_slice_async_rq
= 2;
29 static int cfq_slice_idle
= HZ
/ 125;
31 #define CFQ_IDLE_GRACE (HZ / 10)
32 #define CFQ_SLICE_SCALE (5)
34 #define CFQ_KEY_ASYNC (0)
36 static DEFINE_SPINLOCK(cfq_exit_lock
);
39 * for the hash of cfqq inside the cfqd
41 #define CFQ_QHASH_SHIFT 6
42 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
43 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
46 * for the hash of crq inside the cfqq
48 #define CFQ_MHASH_SHIFT 6
49 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
50 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
51 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
52 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
53 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
55 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
56 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
58 #define RQ_DATA(rq) (rq)->elevator_private
63 #define RB_EMPTY(node) ((node)->rb_node == NULL)
64 #define RB_CLEAR(node) do { \
65 memset(node, 0, sizeof(*node)); \
67 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
68 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
69 #define rq_rb_key(rq) (rq)->sector
71 static kmem_cache_t
*crq_pool
;
72 static kmem_cache_t
*cfq_pool
;
73 static kmem_cache_t
*cfq_ioc_pool
;
75 static atomic_t ioc_count
= ATOMIC_INIT(0);
76 static struct completion
*ioc_gone
;
78 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
79 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
80 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
81 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
86 #define cfq_cfqq_dispatched(cfqq) \
87 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
89 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
91 #define cfq_cfqq_sync(cfqq) \
92 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
94 #define sample_valid(samples) ((samples) > 80)
97 * Per block device queue structure
100 request_queue_t
*queue
;
103 * rr list of queues with requests and the count of them
105 struct list_head rr_list
[CFQ_PRIO_LISTS
];
106 struct list_head busy_rr
;
107 struct list_head cur_rr
;
108 struct list_head idle_rr
;
109 unsigned int busy_queues
;
112 * non-ordered list of empty cfqq's
114 struct list_head empty_list
;
119 struct hlist_head
*cfq_hash
;
122 * global crq hash for all queues
124 struct hlist_head
*crq_hash
;
132 * schedule slice state info
135 * idle window management
137 struct timer_list idle_slice_timer
;
138 struct work_struct unplug_work
;
140 struct cfq_queue
*active_queue
;
141 struct cfq_io_context
*active_cic
;
142 int cur_prio
, cur_end_prio
;
143 unsigned int dispatch_slice
;
145 struct timer_list idle_class_timer
;
147 sector_t last_sector
;
148 unsigned long last_end_request
;
150 unsigned int rq_starved
;
153 * tunables, see top of file
155 unsigned int cfq_quantum
;
156 unsigned int cfq_queued
;
157 unsigned int cfq_fifo_expire
[2];
158 unsigned int cfq_back_penalty
;
159 unsigned int cfq_back_max
;
160 unsigned int cfq_slice
[2];
161 unsigned int cfq_slice_async_rq
;
162 unsigned int cfq_slice_idle
;
164 struct list_head cic_list
;
168 * Per process-grouping structure
171 /* reference count */
173 /* parent cfq_data */
174 struct cfq_data
*cfqd
;
175 /* cfqq lookup hash */
176 struct hlist_node cfq_hash
;
179 /* on either rr or empty list of cfqd */
180 struct list_head cfq_list
;
181 /* sorted list of pending requests */
182 struct rb_root sort_list
;
183 /* if fifo isn't expired, next request to serve */
184 struct cfq_rq
*next_crq
;
185 /* requests queued in sort_list */
187 /* currently allocated requests */
189 /* fifo list of requests in sort_list */
190 struct list_head fifo
;
192 unsigned long slice_start
;
193 unsigned long slice_end
;
194 unsigned long slice_left
;
195 unsigned long service_last
;
197 /* number of requests that are on the dispatch list */
200 /* io prio of this group */
201 unsigned short ioprio
, org_ioprio
;
202 unsigned short ioprio_class
, org_ioprio_class
;
204 /* various state flags, see below */
209 struct rb_node rb_node
;
211 struct request
*request
;
212 struct hlist_node hash
;
214 struct cfq_queue
*cfq_queue
;
215 struct cfq_io_context
*io_context
;
217 unsigned int crq_flags
;
220 enum cfqq_state_flags
{
221 CFQ_CFQQ_FLAG_on_rr
= 0,
222 CFQ_CFQQ_FLAG_wait_request
,
223 CFQ_CFQQ_FLAG_must_alloc
,
224 CFQ_CFQQ_FLAG_must_alloc_slice
,
225 CFQ_CFQQ_FLAG_must_dispatch
,
226 CFQ_CFQQ_FLAG_fifo_expire
,
227 CFQ_CFQQ_FLAG_idle_window
,
228 CFQ_CFQQ_FLAG_prio_changed
,
231 #define CFQ_CFQQ_FNS(name) \
232 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
234 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
236 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
238 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
240 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
242 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
246 CFQ_CFQQ_FNS(wait_request
);
247 CFQ_CFQQ_FNS(must_alloc
);
248 CFQ_CFQQ_FNS(must_alloc_slice
);
249 CFQ_CFQQ_FNS(must_dispatch
);
250 CFQ_CFQQ_FNS(fifo_expire
);
251 CFQ_CFQQ_FNS(idle_window
);
252 CFQ_CFQQ_FNS(prio_changed
);
255 enum cfq_rq_state_flags
{
256 CFQ_CRQ_FLAG_is_sync
= 0,
259 #define CFQ_CRQ_FNS(name) \
260 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
262 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
264 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
266 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
268 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
270 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
273 CFQ_CRQ_FNS(is_sync
);
276 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
277 static void cfq_dispatch_insert(request_queue_t
*, struct cfq_rq
*);
278 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
, gfp_t gfp_mask
);
281 * lots of deadline iosched dupes, can be abstracted later...
283 static inline void cfq_del_crq_hash(struct cfq_rq
*crq
)
285 hlist_del_init(&crq
->hash
);
288 static inline void cfq_add_crq_hash(struct cfq_data
*cfqd
, struct cfq_rq
*crq
)
290 const int hash_idx
= CFQ_MHASH_FN(rq_hash_key(crq
->request
));
292 hlist_add_head(&crq
->hash
, &cfqd
->crq_hash
[hash_idx
]);
295 static struct request
*cfq_find_rq_hash(struct cfq_data
*cfqd
, sector_t offset
)
297 struct hlist_head
*hash_list
= &cfqd
->crq_hash
[CFQ_MHASH_FN(offset
)];
298 struct hlist_node
*entry
, *next
;
300 hlist_for_each_safe(entry
, next
, hash_list
) {
301 struct cfq_rq
*crq
= list_entry_hash(entry
);
302 struct request
*__rq
= crq
->request
;
304 if (!rq_mergeable(__rq
)) {
305 cfq_del_crq_hash(crq
);
309 if (rq_hash_key(__rq
) == offset
)
317 * scheduler run of queue, if there are requests pending and no one in the
318 * driver that will restart queueing
320 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
322 if (cfqd
->busy_queues
)
323 kblockd_schedule_work(&cfqd
->unplug_work
);
326 static int cfq_queue_empty(request_queue_t
*q
)
328 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
330 return !cfqd
->busy_queues
;
333 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
)
335 if (rw
== READ
|| rw
== WRITE_SYNC
)
338 return CFQ_KEY_ASYNC
;
342 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
343 * We choose the request that is closest to the head right now. Distance
344 * behind the head is penalized and only allowed to a certain extent.
346 static struct cfq_rq
*
347 cfq_choose_req(struct cfq_data
*cfqd
, struct cfq_rq
*crq1
, struct cfq_rq
*crq2
)
349 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
350 unsigned long back_max
;
351 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
352 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
353 unsigned wrap
= 0; /* bit mask: requests behind the disk head? */
355 if (crq1
== NULL
|| crq1
== crq2
)
360 if (cfq_crq_is_sync(crq1
) && !cfq_crq_is_sync(crq2
))
362 else if (cfq_crq_is_sync(crq2
) && !cfq_crq_is_sync(crq1
))
365 s1
= crq1
->request
->sector
;
366 s2
= crq2
->request
->sector
;
368 last
= cfqd
->last_sector
;
371 * by definition, 1KiB is 2 sectors
373 back_max
= cfqd
->cfq_back_max
* 2;
376 * Strict one way elevator _except_ in the case where we allow
377 * short backward seeks which are biased as twice the cost of a
378 * similar forward seek.
382 else if (s1
+ back_max
>= last
)
383 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
385 wrap
|= CFQ_RQ1_WRAP
;
389 else if (s2
+ back_max
>= last
)
390 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
392 wrap
|= CFQ_RQ2_WRAP
;
394 /* Found required data */
397 * By doing switch() on the bit mask "wrap" we avoid having to
398 * check two variables for all permutations: --> faster!
401 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
417 case (CFQ_RQ1_WRAP
|CFQ_RQ2_WRAP
): /* both crqs wrapped */
420 * Since both rqs are wrapped,
421 * start with the one that's further behind head
422 * (--> only *one* back seek required),
423 * since back seek takes more time than forward.
433 * would be nice to take fifo expire time into account as well
435 static struct cfq_rq
*
436 cfq_find_next_crq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
439 struct cfq_rq
*crq_next
= NULL
, *crq_prev
= NULL
;
440 struct rb_node
*rbnext
, *rbprev
;
442 if (!(rbnext
= rb_next(&last
->rb_node
))) {
443 rbnext
= rb_first(&cfqq
->sort_list
);
444 if (rbnext
== &last
->rb_node
)
448 rbprev
= rb_prev(&last
->rb_node
);
451 crq_prev
= rb_entry_crq(rbprev
);
453 crq_next
= rb_entry_crq(rbnext
);
455 return cfq_choose_req(cfqd
, crq_next
, crq_prev
);
458 static void cfq_update_next_crq(struct cfq_rq
*crq
)
460 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
462 if (cfqq
->next_crq
== crq
)
463 cfqq
->next_crq
= cfq_find_next_crq(cfqq
->cfqd
, cfqq
, crq
);
466 static void cfq_resort_rr_list(struct cfq_queue
*cfqq
, int preempted
)
468 struct cfq_data
*cfqd
= cfqq
->cfqd
;
469 struct list_head
*list
, *entry
;
471 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
473 list_del(&cfqq
->cfq_list
);
475 if (cfq_class_rt(cfqq
))
476 list
= &cfqd
->cur_rr
;
477 else if (cfq_class_idle(cfqq
))
478 list
= &cfqd
->idle_rr
;
481 * if cfqq has requests in flight, don't allow it to be
482 * found in cfq_set_active_queue before it has finished them.
483 * this is done to increase fairness between a process that
484 * has lots of io pending vs one that only generates one
485 * sporadically or synchronously
487 if (cfq_cfqq_dispatched(cfqq
))
488 list
= &cfqd
->busy_rr
;
490 list
= &cfqd
->rr_list
[cfqq
->ioprio
];
494 * if queue was preempted, just add to front to be fair. busy_rr
495 * isn't sorted, but insert at the back for fairness.
497 if (preempted
|| list
== &cfqd
->busy_rr
) {
501 list_add_tail(&cfqq
->cfq_list
, list
);
506 * sort by when queue was last serviced
509 while ((entry
= entry
->prev
) != list
) {
510 struct cfq_queue
*__cfqq
= list_entry_cfqq(entry
);
512 if (!__cfqq
->service_last
)
514 if (time_before(__cfqq
->service_last
, cfqq
->service_last
))
518 list_add(&cfqq
->cfq_list
, entry
);
522 * add to busy list of queues for service, trying to be fair in ordering
523 * the pending list according to last request service
526 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
528 BUG_ON(cfq_cfqq_on_rr(cfqq
));
529 cfq_mark_cfqq_on_rr(cfqq
);
532 cfq_resort_rr_list(cfqq
, 0);
536 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
538 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
539 cfq_clear_cfqq_on_rr(cfqq
);
540 list_move(&cfqq
->cfq_list
, &cfqd
->empty_list
);
542 BUG_ON(!cfqd
->busy_queues
);
547 * rb tree support functions
549 static inline void cfq_del_crq_rb(struct cfq_rq
*crq
)
551 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
552 struct cfq_data
*cfqd
= cfqq
->cfqd
;
553 const int sync
= cfq_crq_is_sync(crq
);
555 BUG_ON(!cfqq
->queued
[sync
]);
556 cfqq
->queued
[sync
]--;
558 cfq_update_next_crq(crq
);
560 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
562 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY(&cfqq
->sort_list
))
563 cfq_del_cfqq_rr(cfqd
, cfqq
);
566 static struct cfq_rq
*
567 __cfq_add_crq_rb(struct cfq_rq
*crq
)
569 struct rb_node
**p
= &crq
->cfq_queue
->sort_list
.rb_node
;
570 struct rb_node
*parent
= NULL
;
571 struct cfq_rq
*__crq
;
575 __crq
= rb_entry_crq(parent
);
577 if (crq
->rb_key
< __crq
->rb_key
)
579 else if (crq
->rb_key
> __crq
->rb_key
)
585 rb_link_node(&crq
->rb_node
, parent
, p
);
589 static void cfq_add_crq_rb(struct cfq_rq
*crq
)
591 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
592 struct cfq_data
*cfqd
= cfqq
->cfqd
;
593 struct request
*rq
= crq
->request
;
594 struct cfq_rq
*__alias
;
596 crq
->rb_key
= rq_rb_key(rq
);
597 cfqq
->queued
[cfq_crq_is_sync(crq
)]++;
600 * looks a little odd, but the first insert might return an alias.
601 * if that happens, put the alias on the dispatch list
603 while ((__alias
= __cfq_add_crq_rb(crq
)) != NULL
)
604 cfq_dispatch_insert(cfqd
->queue
, __alias
);
606 rb_insert_color(&crq
->rb_node
, &cfqq
->sort_list
);
608 if (!cfq_cfqq_on_rr(cfqq
))
609 cfq_add_cfqq_rr(cfqd
, cfqq
);
612 * check if this request is a better next-serve candidate
614 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
618 cfq_reposition_crq_rb(struct cfq_queue
*cfqq
, struct cfq_rq
*crq
)
620 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
621 cfqq
->queued
[cfq_crq_is_sync(crq
)]--;
626 static struct request
*
627 cfq_find_rq_fmerge(struct cfq_data
*cfqd
, struct bio
*bio
)
629 struct task_struct
*tsk
= current
;
630 pid_t key
= cfq_queue_pid(tsk
, bio_data_dir(bio
));
631 struct cfq_queue
*cfqq
;
635 cfqq
= cfq_find_cfq_hash(cfqd
, key
, tsk
->ioprio
);
639 sector
= bio
->bi_sector
+ bio_sectors(bio
);
640 n
= cfqq
->sort_list
.rb_node
;
642 struct cfq_rq
*crq
= rb_entry_crq(n
);
644 if (sector
< crq
->rb_key
)
646 else if (sector
> crq
->rb_key
)
656 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
658 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
660 cfqd
->rq_in_driver
++;
663 * If the depth is larger 1, it really could be queueing. But lets
664 * make the mark a little higher - idling could still be good for
665 * low queueing, and a low queueing number could also just indicate
666 * a SCSI mid layer like behaviour where limit+1 is often seen.
668 if (!cfqd
->hw_tag
&& cfqd
->rq_in_driver
> 4)
672 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
674 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
676 WARN_ON(!cfqd
->rq_in_driver
);
677 cfqd
->rq_in_driver
--;
680 static void cfq_remove_request(struct request
*rq
)
682 struct cfq_rq
*crq
= RQ_DATA(rq
);
684 list_del_init(&rq
->queuelist
);
686 cfq_del_crq_hash(crq
);
690 cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
692 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
693 struct request
*__rq
;
696 __rq
= cfq_find_rq_hash(cfqd
, bio
->bi_sector
);
697 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
698 ret
= ELEVATOR_BACK_MERGE
;
702 __rq
= cfq_find_rq_fmerge(cfqd
, bio
);
703 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
704 ret
= ELEVATOR_FRONT_MERGE
;
708 return ELEVATOR_NO_MERGE
;
714 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
)
716 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
717 struct cfq_rq
*crq
= RQ_DATA(req
);
719 cfq_del_crq_hash(crq
);
720 cfq_add_crq_hash(cfqd
, crq
);
722 if (rq_rb_key(req
) != crq
->rb_key
) {
723 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
725 cfq_update_next_crq(crq
);
726 cfq_reposition_crq_rb(cfqq
, crq
);
731 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
732 struct request
*next
)
734 cfq_merged_request(q
, rq
);
737 * reposition in fifo if next is older than rq
739 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
740 time_before(next
->start_time
, rq
->start_time
))
741 list_move(&rq
->queuelist
, &next
->queuelist
);
743 cfq_remove_request(next
);
747 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
751 * stop potential idle class queues waiting service
753 del_timer(&cfqd
->idle_class_timer
);
755 cfqq
->slice_start
= jiffies
;
757 cfqq
->slice_left
= 0;
758 cfq_clear_cfqq_must_alloc_slice(cfqq
);
759 cfq_clear_cfqq_fifo_expire(cfqq
);
762 cfqd
->active_queue
= cfqq
;
766 * current cfqq expired its slice (or was too idle), select new one
769 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
772 unsigned long now
= jiffies
;
774 if (cfq_cfqq_wait_request(cfqq
))
775 del_timer(&cfqd
->idle_slice_timer
);
777 if (!preempted
&& !cfq_cfqq_dispatched(cfqq
)) {
778 cfqq
->service_last
= now
;
779 cfq_schedule_dispatch(cfqd
);
782 cfq_clear_cfqq_must_dispatch(cfqq
);
783 cfq_clear_cfqq_wait_request(cfqq
);
786 * store what was left of this slice, if the queue idled out
789 if (time_after(cfqq
->slice_end
, now
))
790 cfqq
->slice_left
= cfqq
->slice_end
- now
;
792 cfqq
->slice_left
= 0;
794 if (cfq_cfqq_on_rr(cfqq
))
795 cfq_resort_rr_list(cfqq
, preempted
);
797 if (cfqq
== cfqd
->active_queue
)
798 cfqd
->active_queue
= NULL
;
800 if (cfqd
->active_cic
) {
801 put_io_context(cfqd
->active_cic
->ioc
);
802 cfqd
->active_cic
= NULL
;
805 cfqd
->dispatch_slice
= 0;
808 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int preempted
)
810 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
813 __cfq_slice_expired(cfqd
, cfqq
, preempted
);
826 static int cfq_get_next_prio_level(struct cfq_data
*cfqd
)
835 for (p
= cfqd
->cur_prio
; p
<= cfqd
->cur_end_prio
; p
++) {
836 if (!list_empty(&cfqd
->rr_list
[p
])) {
845 if (++cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
846 cfqd
->cur_end_prio
= 0;
853 if (unlikely(prio
== -1))
856 BUG_ON(prio
>= CFQ_PRIO_LISTS
);
858 list_splice_init(&cfqd
->rr_list
[prio
], &cfqd
->cur_rr
);
860 cfqd
->cur_prio
= prio
+ 1;
861 if (cfqd
->cur_prio
> cfqd
->cur_end_prio
) {
862 cfqd
->cur_end_prio
= cfqd
->cur_prio
;
865 if (cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
867 cfqd
->cur_end_prio
= 0;
873 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
875 struct cfq_queue
*cfqq
= NULL
;
878 * if current list is non-empty, grab first entry. if it is empty,
879 * get next prio level and grab first entry then if any are spliced
881 if (!list_empty(&cfqd
->cur_rr
) || cfq_get_next_prio_level(cfqd
) != -1)
882 cfqq
= list_entry_cfqq(cfqd
->cur_rr
.next
);
885 * If no new queues are available, check if the busy list has some
886 * before falling back to idle io.
888 if (!cfqq
&& !list_empty(&cfqd
->busy_rr
))
889 cfqq
= list_entry_cfqq(cfqd
->busy_rr
.next
);
892 * if we have idle queues and no rt or be queues had pending
893 * requests, either allow immediate service if the grace period
894 * has passed or arm the idle grace timer
896 if (!cfqq
&& !list_empty(&cfqd
->idle_rr
)) {
897 unsigned long end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
899 if (time_after_eq(jiffies
, end
))
900 cfqq
= list_entry_cfqq(cfqd
->idle_rr
.next
);
902 mod_timer(&cfqd
->idle_class_timer
, end
);
905 __cfq_set_active_queue(cfqd
, cfqq
);
909 #define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
911 static int cfq_arm_slice_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
914 struct cfq_io_context
*cic
;
917 WARN_ON(!RB_EMPTY(&cfqq
->sort_list
));
918 WARN_ON(cfqq
!= cfqd
->active_queue
);
921 * idle is disabled, either manually or by past process history
923 if (!cfqd
->cfq_slice_idle
)
925 if (!cfq_cfqq_idle_window(cfqq
))
928 * task has exited, don't wait
930 cic
= cfqd
->active_cic
;
931 if (!cic
|| !cic
->ioc
->task
)
934 cfq_mark_cfqq_must_dispatch(cfqq
);
935 cfq_mark_cfqq_wait_request(cfqq
);
937 sl
= min(cfqq
->slice_end
- 1, (unsigned long) cfqd
->cfq_slice_idle
);
940 * we don't want to idle for seeks, but we do want to allow
941 * fair distribution of slice time for a process doing back-to-back
942 * seeks. so allow a little bit of time for him to submit a new rq
944 if (sample_valid(cic
->seek_samples
) && CIC_SEEKY(cic
))
947 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
951 static void cfq_dispatch_insert(request_queue_t
*q
, struct cfq_rq
*crq
)
953 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
954 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
957 cfqq
->next_crq
= cfq_find_next_crq(cfqd
, cfqq
, crq
);
958 cfq_remove_request(crq
->request
);
959 cfqq
->on_dispatch
[cfq_crq_is_sync(crq
)]++;
960 elv_dispatch_sort(q
, crq
->request
);
962 rq
= list_entry(q
->queue_head
.prev
, struct request
, queuelist
);
963 cfqd
->last_sector
= rq
->sector
+ rq
->nr_sectors
;
967 * return expired entry, or NULL to just start from scratch in rbtree
969 static inline struct cfq_rq
*cfq_check_fifo(struct cfq_queue
*cfqq
)
971 struct cfq_data
*cfqd
= cfqq
->cfqd
;
975 if (cfq_cfqq_fifo_expire(cfqq
))
978 if (!list_empty(&cfqq
->fifo
)) {
979 int fifo
= cfq_cfqq_class_sync(cfqq
);
981 crq
= RQ_DATA(list_entry_fifo(cfqq
->fifo
.next
));
983 if (time_after(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
])) {
984 cfq_mark_cfqq_fifo_expire(cfqq
);
993 * Scale schedule slice based on io priority. Use the sync time slice only
994 * if a queue is marked sync and has sync io queued. A sync queue with async
995 * io only, should not get full sync slice length.
998 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1000 const int base_slice
= cfqd
->cfq_slice
[cfq_cfqq_sync(cfqq
)];
1002 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
1004 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - cfqq
->ioprio
));
1008 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1010 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
1014 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1016 const int base_rq
= cfqd
->cfq_slice_async_rq
;
1018 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
1020 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
1024 * get next queue for service
1026 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
1028 unsigned long now
= jiffies
;
1029 struct cfq_queue
*cfqq
;
1031 cfqq
= cfqd
->active_queue
;
1038 if (!cfq_cfqq_must_dispatch(cfqq
) && time_after(now
, cfqq
->slice_end
))
1042 * if queue has requests, dispatch one. if not, check if
1043 * enough slice is left to wait for one
1045 if (!RB_EMPTY(&cfqq
->sort_list
))
1047 else if (cfq_cfqq_dispatched(cfqq
)) {
1050 } else if (cfq_cfqq_class_sync(cfqq
)) {
1051 if (cfq_arm_slice_timer(cfqd
, cfqq
))
1056 cfq_slice_expired(cfqd
, 0);
1058 cfqq
= cfq_set_active_queue(cfqd
);
1064 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1069 BUG_ON(RB_EMPTY(&cfqq
->sort_list
));
1075 * follow expired path, else get first next available
1077 if ((crq
= cfq_check_fifo(cfqq
)) == NULL
)
1078 crq
= cfqq
->next_crq
;
1081 * finally, insert request into driver dispatch list
1083 cfq_dispatch_insert(cfqd
->queue
, crq
);
1085 cfqd
->dispatch_slice
++;
1088 if (!cfqd
->active_cic
) {
1089 atomic_inc(&crq
->io_context
->ioc
->refcount
);
1090 cfqd
->active_cic
= crq
->io_context
;
1093 if (RB_EMPTY(&cfqq
->sort_list
))
1096 } while (dispatched
< max_dispatch
);
1099 * if slice end isn't set yet, set it.
1101 if (!cfqq
->slice_end
)
1102 cfq_set_prio_slice(cfqd
, cfqq
);
1105 * expire an async queue immediately if it has used up its slice. idle
1106 * queue always expire after 1 dispatch round.
1108 if ((!cfq_cfqq_sync(cfqq
) &&
1109 cfqd
->dispatch_slice
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
1110 cfq_class_idle(cfqq
) ||
1111 !cfq_cfqq_idle_window(cfqq
))
1112 cfq_slice_expired(cfqd
, 0);
1118 cfq_forced_dispatch_cfqqs(struct list_head
*list
)
1120 struct cfq_queue
*cfqq
, *next
;
1125 list_for_each_entry_safe(cfqq
, next
, list
, cfq_list
) {
1126 while ((crq
= cfqq
->next_crq
)) {
1127 cfq_dispatch_insert(cfqq
->cfqd
->queue
, crq
);
1130 BUG_ON(!list_empty(&cfqq
->fifo
));
1137 cfq_forced_dispatch(struct cfq_data
*cfqd
)
1139 int i
, dispatched
= 0;
1141 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
1142 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->rr_list
[i
]);
1144 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->busy_rr
);
1145 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->cur_rr
);
1146 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->idle_rr
);
1148 cfq_slice_expired(cfqd
, 0);
1150 BUG_ON(cfqd
->busy_queues
);
1156 cfq_dispatch_requests(request_queue_t
*q
, int force
)
1158 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1159 struct cfq_queue
*cfqq
, *prev_cfqq
;
1162 if (!cfqd
->busy_queues
)
1165 if (unlikely(force
))
1166 return cfq_forced_dispatch(cfqd
);
1170 while ((cfqq
= cfq_select_queue(cfqd
)) != NULL
) {
1174 * Don't repeat dispatch from the previous queue.
1176 if (prev_cfqq
== cfqq
)
1179 cfq_clear_cfqq_must_dispatch(cfqq
);
1180 cfq_clear_cfqq_wait_request(cfqq
);
1181 del_timer(&cfqd
->idle_slice_timer
);
1183 max_dispatch
= cfqd
->cfq_quantum
;
1184 if (cfq_class_idle(cfqq
))
1187 dispatched
+= __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1190 * If the dispatch cfqq has idling enabled and is still
1191 * the active queue, break out.
1193 if (cfq_cfqq_idle_window(cfqq
) && cfqd
->active_queue
)
1203 * task holds one reference to the queue, dropped when task exits. each crq
1204 * in-flight on this queue also holds a reference, dropped when crq is freed.
1206 * queue lock must be held here.
1208 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1210 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1212 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1214 if (!atomic_dec_and_test(&cfqq
->ref
))
1217 BUG_ON(rb_first(&cfqq
->sort_list
));
1218 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1219 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1221 if (unlikely(cfqd
->active_queue
== cfqq
))
1222 __cfq_slice_expired(cfqd
, cfqq
, 0);
1225 * it's on the empty list and still hashed
1227 list_del(&cfqq
->cfq_list
);
1228 hlist_del(&cfqq
->cfq_hash
);
1229 kmem_cache_free(cfq_pool
, cfqq
);
1232 static inline struct cfq_queue
*
1233 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1236 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1237 struct hlist_node
*entry
;
1238 struct cfq_queue
*__cfqq
;
1240 hlist_for_each_entry(__cfqq
, entry
, hash_list
, cfq_hash
) {
1241 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->org_ioprio_class
, __cfqq
->org_ioprio
);
1243 if (__cfqq
->key
== key
&& (__p
== prio
|| !prio
))
1250 static struct cfq_queue
*
1251 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1253 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1256 static void cfq_free_io_context(struct io_context
*ioc
)
1258 struct cfq_io_context
*__cic
;
1262 while ((n
= rb_first(&ioc
->cic_root
)) != NULL
) {
1263 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1264 rb_erase(&__cic
->rb_node
, &ioc
->cic_root
);
1265 kmem_cache_free(cfq_ioc_pool
, __cic
);
1269 if (atomic_sub_and_test(freed
, &ioc_count
) && ioc_gone
)
1273 static void cfq_trim(struct io_context
*ioc
)
1275 ioc
->set_ioprio
= NULL
;
1276 cfq_free_io_context(ioc
);
1280 * Called with interrupts disabled
1282 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1284 struct cfq_data
*cfqd
= cic
->key
;
1292 WARN_ON(!irqs_disabled());
1294 spin_lock(q
->queue_lock
);
1296 if (cic
->cfqq
[ASYNC
]) {
1297 if (unlikely(cic
->cfqq
[ASYNC
] == cfqd
->active_queue
))
1298 __cfq_slice_expired(cfqd
, cic
->cfqq
[ASYNC
], 0);
1299 cfq_put_queue(cic
->cfqq
[ASYNC
]);
1300 cic
->cfqq
[ASYNC
] = NULL
;
1303 if (cic
->cfqq
[SYNC
]) {
1304 if (unlikely(cic
->cfqq
[SYNC
] == cfqd
->active_queue
))
1305 __cfq_slice_expired(cfqd
, cic
->cfqq
[SYNC
], 0);
1306 cfq_put_queue(cic
->cfqq
[SYNC
]);
1307 cic
->cfqq
[SYNC
] = NULL
;
1311 list_del_init(&cic
->queue_list
);
1312 spin_unlock(q
->queue_lock
);
1315 static void cfq_exit_io_context(struct io_context
*ioc
)
1317 struct cfq_io_context
*__cic
;
1318 unsigned long flags
;
1322 * put the reference this task is holding to the various queues
1324 spin_lock_irqsave(&cfq_exit_lock
, flags
);
1326 n
= rb_first(&ioc
->cic_root
);
1328 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1330 cfq_exit_single_io_context(__cic
);
1334 spin_unlock_irqrestore(&cfq_exit_lock
, flags
);
1337 static struct cfq_io_context
*
1338 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1340 struct cfq_io_context
*cic
= kmem_cache_alloc(cfq_ioc_pool
, gfp_mask
);
1343 memset(cic
, 0, sizeof(*cic
));
1344 cic
->last_end_request
= jiffies
;
1345 INIT_LIST_HEAD(&cic
->queue_list
);
1346 cic
->dtor
= cfq_free_io_context
;
1347 cic
->exit
= cfq_exit_io_context
;
1348 atomic_inc(&ioc_count
);
1354 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1356 struct task_struct
*tsk
= current
;
1359 if (!cfq_cfqq_prio_changed(cfqq
))
1362 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1363 switch (ioprio_class
) {
1365 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1366 case IOPRIO_CLASS_NONE
:
1368 * no prio set, place us in the middle of the BE classes
1370 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1371 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1373 case IOPRIO_CLASS_RT
:
1374 cfqq
->ioprio
= task_ioprio(tsk
);
1375 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1377 case IOPRIO_CLASS_BE
:
1378 cfqq
->ioprio
= task_ioprio(tsk
);
1379 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1381 case IOPRIO_CLASS_IDLE
:
1382 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1384 cfq_clear_cfqq_idle_window(cfqq
);
1389 * keep track of original prio settings in case we have to temporarily
1390 * elevate the priority of this queue
1392 cfqq
->org_ioprio
= cfqq
->ioprio
;
1393 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1395 if (cfq_cfqq_on_rr(cfqq
))
1396 cfq_resort_rr_list(cfqq
, 0);
1398 cfq_clear_cfqq_prio_changed(cfqq
);
1401 static inline void changed_ioprio(struct cfq_io_context
*cic
)
1403 struct cfq_data
*cfqd
= cic
->key
;
1404 struct cfq_queue
*cfqq
;
1406 if (unlikely(!cfqd
))
1409 spin_lock(cfqd
->queue
->queue_lock
);
1411 cfqq
= cic
->cfqq
[ASYNC
];
1413 struct cfq_queue
*new_cfqq
;
1414 new_cfqq
= cfq_get_queue(cfqd
, CFQ_KEY_ASYNC
, cic
->ioc
->task
,
1417 cic
->cfqq
[ASYNC
] = new_cfqq
;
1418 cfq_put_queue(cfqq
);
1422 cfqq
= cic
->cfqq
[SYNC
];
1424 cfq_mark_cfqq_prio_changed(cfqq
);
1426 spin_unlock(cfqd
->queue
->queue_lock
);
1430 * callback from sys_ioprio_set, irqs are disabled
1432 static int cfq_ioc_set_ioprio(struct io_context
*ioc
, unsigned int ioprio
)
1434 struct cfq_io_context
*cic
;
1437 spin_lock(&cfq_exit_lock
);
1439 n
= rb_first(&ioc
->cic_root
);
1441 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1443 changed_ioprio(cic
);
1447 spin_unlock(&cfq_exit_lock
);
1452 static struct cfq_queue
*
1453 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
,
1456 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1457 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1458 unsigned short ioprio
;
1461 ioprio
= tsk
->ioprio
;
1462 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1468 } else if (gfp_mask
& __GFP_WAIT
) {
1469 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1470 new_cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1471 spin_lock_irq(cfqd
->queue
->queue_lock
);
1474 cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1479 memset(cfqq
, 0, sizeof(*cfqq
));
1481 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1482 INIT_LIST_HEAD(&cfqq
->cfq_list
);
1483 RB_CLEAR_ROOT(&cfqq
->sort_list
);
1484 INIT_LIST_HEAD(&cfqq
->fifo
);
1487 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1488 atomic_set(&cfqq
->ref
, 0);
1490 cfqq
->service_last
= 0;
1492 * set ->slice_left to allow preemption for a new process
1494 cfqq
->slice_left
= 2 * cfqd
->cfq_slice_idle
;
1495 cfq_mark_cfqq_idle_window(cfqq
);
1496 cfq_mark_cfqq_prio_changed(cfqq
);
1497 cfq_init_prio_data(cfqq
);
1501 kmem_cache_free(cfq_pool
, new_cfqq
);
1503 atomic_inc(&cfqq
->ref
);
1505 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1510 cfq_drop_dead_cic(struct io_context
*ioc
, struct cfq_io_context
*cic
)
1512 spin_lock(&cfq_exit_lock
);
1513 rb_erase(&cic
->rb_node
, &ioc
->cic_root
);
1514 list_del_init(&cic
->queue_list
);
1515 spin_unlock(&cfq_exit_lock
);
1516 kmem_cache_free(cfq_ioc_pool
, cic
);
1517 atomic_dec(&ioc_count
);
1520 static struct cfq_io_context
*
1521 cfq_cic_rb_lookup(struct cfq_data
*cfqd
, struct io_context
*ioc
)
1524 struct cfq_io_context
*cic
;
1525 void *k
, *key
= cfqd
;
1528 n
= ioc
->cic_root
.rb_node
;
1530 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1531 /* ->key must be copied to avoid race with cfq_exit_queue() */
1534 cfq_drop_dead_cic(ioc
, cic
);
1550 cfq_cic_link(struct cfq_data
*cfqd
, struct io_context
*ioc
,
1551 struct cfq_io_context
*cic
)
1554 struct rb_node
*parent
;
1555 struct cfq_io_context
*__cic
;
1561 ioc
->set_ioprio
= cfq_ioc_set_ioprio
;
1564 p
= &ioc
->cic_root
.rb_node
;
1567 __cic
= rb_entry(parent
, struct cfq_io_context
, rb_node
);
1568 /* ->key must be copied to avoid race with cfq_exit_queue() */
1571 cfq_drop_dead_cic(ioc
, cic
);
1577 else if (cic
->key
> k
)
1578 p
= &(*p
)->rb_right
;
1583 spin_lock(&cfq_exit_lock
);
1584 rb_link_node(&cic
->rb_node
, parent
, p
);
1585 rb_insert_color(&cic
->rb_node
, &ioc
->cic_root
);
1586 list_add(&cic
->queue_list
, &cfqd
->cic_list
);
1587 spin_unlock(&cfq_exit_lock
);
1591 * Setup general io context and cfq io context. There can be several cfq
1592 * io contexts per general io context, if this process is doing io to more
1593 * than one device managed by cfq.
1595 static struct cfq_io_context
*
1596 cfq_get_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1598 struct io_context
*ioc
= NULL
;
1599 struct cfq_io_context
*cic
;
1601 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1603 ioc
= get_io_context(gfp_mask
);
1607 cic
= cfq_cic_rb_lookup(cfqd
, ioc
);
1611 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1615 cfq_cic_link(cfqd
, ioc
, cic
);
1619 put_io_context(ioc
);
1624 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1626 unsigned long elapsed
, ttime
;
1629 * if this context already has stuff queued, thinktime is from
1630 * last queue not last end
1633 if (time_after(cic
->last_end_request
, cic
->last_queue
))
1634 elapsed
= jiffies
- cic
->last_end_request
;
1636 elapsed
= jiffies
- cic
->last_queue
;
1638 elapsed
= jiffies
- cic
->last_end_request
;
1641 ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1643 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1644 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1645 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1649 cfq_update_io_seektime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
,
1655 if (cic
->last_request_pos
< crq
->request
->sector
)
1656 sdist
= crq
->request
->sector
- cic
->last_request_pos
;
1658 sdist
= cic
->last_request_pos
- crq
->request
->sector
;
1661 * Don't allow the seek distance to get too large from the
1662 * odd fragment, pagein, etc
1664 if (cic
->seek_samples
<= 60) /* second&third seek */
1665 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*1024);
1667 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*64);
1669 cic
->seek_samples
= (7*cic
->seek_samples
+ 256) / 8;
1670 cic
->seek_total
= (7*cic
->seek_total
+ (u64
)256*sdist
) / 8;
1671 total
= cic
->seek_total
+ (cic
->seek_samples
/2);
1672 do_div(total
, cic
->seek_samples
);
1673 cic
->seek_mean
= (sector_t
)total
;
1677 * Disable idle window if the process thinks too long or seeks so much that
1681 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1682 struct cfq_io_context
*cic
)
1684 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1686 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
||
1687 (cfqd
->hw_tag
&& CIC_SEEKY(cic
)))
1689 else if (sample_valid(cic
->ttime_samples
)) {
1690 if (cic
->ttime_mean
> cfqd
->cfq_slice_idle
)
1697 cfq_mark_cfqq_idle_window(cfqq
);
1699 cfq_clear_cfqq_idle_window(cfqq
);
1704 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1705 * no or if we aren't sure, a 1 will cause a preempt.
1708 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1711 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
1713 if (cfq_class_idle(new_cfqq
))
1719 if (cfq_class_idle(cfqq
))
1721 if (!cfq_cfqq_wait_request(new_cfqq
))
1724 * if it doesn't have slice left, forget it
1726 if (new_cfqq
->slice_left
< cfqd
->cfq_slice_idle
)
1728 if (cfq_crq_is_sync(crq
) && !cfq_cfqq_sync(cfqq
))
1735 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1736 * let it have half of its nominal slice.
1738 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1740 struct cfq_queue
*__cfqq
, *next
;
1742 list_for_each_entry_safe(__cfqq
, next
, &cfqd
->cur_rr
, cfq_list
)
1743 cfq_resort_rr_list(__cfqq
, 1);
1745 if (!cfqq
->slice_left
)
1746 cfqq
->slice_left
= cfq_prio_to_slice(cfqd
, cfqq
) / 2;
1748 cfqq
->slice_end
= cfqq
->slice_left
+ jiffies
;
1749 cfq_slice_expired(cfqd
, 1);
1750 __cfq_set_active_queue(cfqd
, cfqq
);
1754 * should really be a ll_rw_blk.c helper
1756 static void cfq_start_queueing(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1758 request_queue_t
*q
= cfqd
->queue
;
1760 if (!blk_queue_plugged(q
))
1763 __generic_unplug_device(q
);
1767 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1768 * something we should do about it
1771 cfq_crq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1774 struct cfq_io_context
*cic
= crq
->io_context
;
1777 * we never wait for an async request and we don't allow preemption
1778 * of an async request. so just return early
1780 if (!cfq_crq_is_sync(crq
)) {
1782 * sync process issued an async request, if it's waiting
1783 * then expire it and kick rq handling.
1785 if (cic
== cfqd
->active_cic
&&
1786 del_timer(&cfqd
->idle_slice_timer
)) {
1787 cfq_slice_expired(cfqd
, 0);
1788 cfq_start_queueing(cfqd
, cfqq
);
1793 cfq_update_io_thinktime(cfqd
, cic
);
1794 cfq_update_io_seektime(cfqd
, cic
, crq
);
1795 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1797 cic
->last_queue
= jiffies
;
1798 cic
->last_request_pos
= crq
->request
->sector
+ crq
->request
->nr_sectors
;
1800 if (cfqq
== cfqd
->active_queue
) {
1802 * if we are waiting for a request for this queue, let it rip
1803 * immediately and flag that we must not expire this queue
1806 if (cfq_cfqq_wait_request(cfqq
)) {
1807 cfq_mark_cfqq_must_dispatch(cfqq
);
1808 del_timer(&cfqd
->idle_slice_timer
);
1809 cfq_start_queueing(cfqd
, cfqq
);
1811 } else if (cfq_should_preempt(cfqd
, cfqq
, crq
)) {
1813 * not the active queue - expire current slice if it is
1814 * idle and has expired it's mean thinktime or this new queue
1815 * has some old slice time left and is of higher priority
1817 cfq_preempt_queue(cfqd
, cfqq
);
1818 cfq_mark_cfqq_must_dispatch(cfqq
);
1819 cfq_start_queueing(cfqd
, cfqq
);
1823 static void cfq_insert_request(request_queue_t
*q
, struct request
*rq
)
1825 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1826 struct cfq_rq
*crq
= RQ_DATA(rq
);
1827 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1829 cfq_init_prio_data(cfqq
);
1831 cfq_add_crq_rb(crq
);
1833 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1835 if (rq_mergeable(rq
))
1836 cfq_add_crq_hash(cfqd
, crq
);
1838 cfq_crq_enqueued(cfqd
, cfqq
, crq
);
1841 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1843 struct cfq_rq
*crq
= RQ_DATA(rq
);
1844 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1845 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1846 const int sync
= cfq_crq_is_sync(crq
);
1851 WARN_ON(!cfqd
->rq_in_driver
);
1852 WARN_ON(!cfqq
->on_dispatch
[sync
]);
1853 cfqd
->rq_in_driver
--;
1854 cfqq
->on_dispatch
[sync
]--;
1856 if (!cfq_class_idle(cfqq
))
1857 cfqd
->last_end_request
= now
;
1859 if (!cfq_cfqq_dispatched(cfqq
)) {
1860 if (cfq_cfqq_on_rr(cfqq
)) {
1861 cfqq
->service_last
= now
;
1862 cfq_resort_rr_list(cfqq
, 0);
1867 crq
->io_context
->last_end_request
= now
;
1870 * If this is the active queue, check if it needs to be expired,
1871 * or if we want to idle in case it has no pending requests.
1873 if (cfqd
->active_queue
== cfqq
) {
1874 if (time_after(now
, cfqq
->slice_end
))
1875 cfq_slice_expired(cfqd
, 0);
1876 else if (sync
&& RB_EMPTY(&cfqq
->sort_list
)) {
1877 if (!cfq_arm_slice_timer(cfqd
, cfqq
))
1878 cfq_schedule_dispatch(cfqd
);
1883 static struct request
*
1884 cfq_former_request(request_queue_t
*q
, struct request
*rq
)
1886 struct cfq_rq
*crq
= RQ_DATA(rq
);
1887 struct rb_node
*rbprev
= rb_prev(&crq
->rb_node
);
1890 return rb_entry_crq(rbprev
)->request
;
1895 static struct request
*
1896 cfq_latter_request(request_queue_t
*q
, struct request
*rq
)
1898 struct cfq_rq
*crq
= RQ_DATA(rq
);
1899 struct rb_node
*rbnext
= rb_next(&crq
->rb_node
);
1902 return rb_entry_crq(rbnext
)->request
;
1908 * we temporarily boost lower priority queues if they are holding fs exclusive
1909 * resources. they are boosted to normal prio (CLASS_BE/4)
1911 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1913 const int ioprio_class
= cfqq
->ioprio_class
;
1914 const int ioprio
= cfqq
->ioprio
;
1916 if (has_fs_excl()) {
1918 * boost idle prio on transactions that would lock out other
1919 * users of the filesystem
1921 if (cfq_class_idle(cfqq
))
1922 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1923 if (cfqq
->ioprio
> IOPRIO_NORM
)
1924 cfqq
->ioprio
= IOPRIO_NORM
;
1927 * check if we need to unboost the queue
1929 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1930 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1931 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1932 cfqq
->ioprio
= cfqq
->org_ioprio
;
1936 * refile between round-robin lists if we moved the priority class
1938 if ((ioprio_class
!= cfqq
->ioprio_class
|| ioprio
!= cfqq
->ioprio
) &&
1939 cfq_cfqq_on_rr(cfqq
))
1940 cfq_resort_rr_list(cfqq
, 0);
1944 __cfq_may_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1945 struct task_struct
*task
, int rw
)
1947 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1948 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1949 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1950 return ELV_MQUEUE_MUST
;
1953 return ELV_MQUEUE_MAY
;
1956 static int cfq_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1958 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1959 struct task_struct
*tsk
= current
;
1960 struct cfq_queue
*cfqq
;
1963 * don't force setup of a queue from here, as a call to may_queue
1964 * does not necessarily imply that a request actually will be queued.
1965 * so just lookup a possibly existing queue, or return 'may queue'
1968 cfqq
= cfq_find_cfq_hash(cfqd
, cfq_queue_pid(tsk
, rw
), tsk
->ioprio
);
1970 cfq_init_prio_data(cfqq
);
1971 cfq_prio_boost(cfqq
);
1973 return __cfq_may_queue(cfqd
, cfqq
, tsk
, rw
);
1976 return ELV_MQUEUE_MAY
;
1979 static void cfq_check_waiters(request_queue_t
*q
, struct cfq_queue
*cfqq
)
1981 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1983 if (unlikely(cfqd
->rq_starved
)) {
1984 struct request_list
*rl
= &q
->rq
;
1987 if (waitqueue_active(&rl
->wait
[READ
]))
1988 wake_up(&rl
->wait
[READ
]);
1989 if (waitqueue_active(&rl
->wait
[WRITE
]))
1990 wake_up(&rl
->wait
[WRITE
]);
1995 * queue lock held here
1997 static void cfq_put_request(request_queue_t
*q
, struct request
*rq
)
1999 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2000 struct cfq_rq
*crq
= RQ_DATA(rq
);
2003 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
2004 const int rw
= rq_data_dir(rq
);
2006 BUG_ON(!cfqq
->allocated
[rw
]);
2007 cfqq
->allocated
[rw
]--;
2009 put_io_context(crq
->io_context
->ioc
);
2011 mempool_free(crq
, cfqd
->crq_pool
);
2012 rq
->elevator_private
= NULL
;
2014 cfq_check_waiters(q
, cfqq
);
2015 cfq_put_queue(cfqq
);
2020 * Allocate cfq data structures associated with this request.
2023 cfq_set_request(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
,
2026 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2027 struct task_struct
*tsk
= current
;
2028 struct cfq_io_context
*cic
;
2029 const int rw
= rq_data_dir(rq
);
2030 pid_t key
= cfq_queue_pid(tsk
, rw
);
2031 struct cfq_queue
*cfqq
;
2033 unsigned long flags
;
2034 int is_sync
= key
!= CFQ_KEY_ASYNC
;
2036 might_sleep_if(gfp_mask
& __GFP_WAIT
);
2038 cic
= cfq_get_io_context(cfqd
, gfp_mask
);
2040 spin_lock_irqsave(q
->queue_lock
, flags
);
2045 if (!cic
->cfqq
[is_sync
]) {
2046 cfqq
= cfq_get_queue(cfqd
, key
, tsk
, gfp_mask
);
2050 cic
->cfqq
[is_sync
] = cfqq
;
2052 cfqq
= cic
->cfqq
[is_sync
];
2054 cfqq
->allocated
[rw
]++;
2055 cfq_clear_cfqq_must_alloc(cfqq
);
2056 cfqd
->rq_starved
= 0;
2057 atomic_inc(&cfqq
->ref
);
2058 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2060 crq
= mempool_alloc(cfqd
->crq_pool
, gfp_mask
);
2062 RB_CLEAR(&crq
->rb_node
);
2065 INIT_HLIST_NODE(&crq
->hash
);
2066 crq
->cfq_queue
= cfqq
;
2067 crq
->io_context
= cic
;
2070 cfq_mark_crq_is_sync(crq
);
2072 cfq_clear_crq_is_sync(crq
);
2074 rq
->elevator_private
= crq
;
2078 spin_lock_irqsave(q
->queue_lock
, flags
);
2079 cfqq
->allocated
[rw
]--;
2080 if (!(cfqq
->allocated
[0] + cfqq
->allocated
[1]))
2081 cfq_mark_cfqq_must_alloc(cfqq
);
2082 cfq_put_queue(cfqq
);
2085 put_io_context(cic
->ioc
);
2087 * mark us rq allocation starved. we need to kickstart the process
2088 * ourselves if there are no pending requests that can do it for us.
2089 * that would be an extremely rare OOM situation
2091 cfqd
->rq_starved
= 1;
2092 cfq_schedule_dispatch(cfqd
);
2093 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2097 static void cfq_kick_queue(void *data
)
2099 request_queue_t
*q
= data
;
2100 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2101 unsigned long flags
;
2103 spin_lock_irqsave(q
->queue_lock
, flags
);
2105 if (cfqd
->rq_starved
) {
2106 struct request_list
*rl
= &q
->rq
;
2109 * we aren't guaranteed to get a request after this, but we
2110 * have to be opportunistic
2113 if (waitqueue_active(&rl
->wait
[READ
]))
2114 wake_up(&rl
->wait
[READ
]);
2115 if (waitqueue_active(&rl
->wait
[WRITE
]))
2116 wake_up(&rl
->wait
[WRITE
]);
2121 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2125 * Timer running if the active_queue is currently idling inside its time slice
2127 static void cfq_idle_slice_timer(unsigned long data
)
2129 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2130 struct cfq_queue
*cfqq
;
2131 unsigned long flags
;
2133 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2135 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
2136 unsigned long now
= jiffies
;
2141 if (time_after(now
, cfqq
->slice_end
))
2145 * only expire and reinvoke request handler, if there are
2146 * other queues with pending requests
2148 if (!cfqd
->busy_queues
)
2152 * not expired and it has a request pending, let it dispatch
2154 if (!RB_EMPTY(&cfqq
->sort_list
)) {
2155 cfq_mark_cfqq_must_dispatch(cfqq
);
2160 cfq_slice_expired(cfqd
, 0);
2162 cfq_schedule_dispatch(cfqd
);
2164 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2168 * Timer running if an idle class queue is waiting for service
2170 static void cfq_idle_class_timer(unsigned long data
)
2172 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2173 unsigned long flags
, end
;
2175 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2178 * race with a non-idle queue, reset timer
2180 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
2181 if (!time_after_eq(jiffies
, end
))
2182 mod_timer(&cfqd
->idle_class_timer
, end
);
2184 cfq_schedule_dispatch(cfqd
);
2186 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2189 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
2191 del_timer_sync(&cfqd
->idle_slice_timer
);
2192 del_timer_sync(&cfqd
->idle_class_timer
);
2193 blk_sync_queue(cfqd
->queue
);
2196 static void cfq_exit_queue(elevator_t
*e
)
2198 struct cfq_data
*cfqd
= e
->elevator_data
;
2199 request_queue_t
*q
= cfqd
->queue
;
2201 cfq_shutdown_timer_wq(cfqd
);
2203 spin_lock(&cfq_exit_lock
);
2204 spin_lock_irq(q
->queue_lock
);
2206 if (cfqd
->active_queue
)
2207 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
2209 while (!list_empty(&cfqd
->cic_list
)) {
2210 struct cfq_io_context
*cic
= list_entry(cfqd
->cic_list
.next
,
2211 struct cfq_io_context
,
2213 if (cic
->cfqq
[ASYNC
]) {
2214 cfq_put_queue(cic
->cfqq
[ASYNC
]);
2215 cic
->cfqq
[ASYNC
] = NULL
;
2217 if (cic
->cfqq
[SYNC
]) {
2218 cfq_put_queue(cic
->cfqq
[SYNC
]);
2219 cic
->cfqq
[SYNC
] = NULL
;
2222 list_del_init(&cic
->queue_list
);
2225 spin_unlock_irq(q
->queue_lock
);
2226 spin_unlock(&cfq_exit_lock
);
2228 cfq_shutdown_timer_wq(cfqd
);
2230 mempool_destroy(cfqd
->crq_pool
);
2231 kfree(cfqd
->crq_hash
);
2232 kfree(cfqd
->cfq_hash
);
2236 static void *cfq_init_queue(request_queue_t
*q
, elevator_t
*e
)
2238 struct cfq_data
*cfqd
;
2241 cfqd
= kmalloc(sizeof(*cfqd
), GFP_KERNEL
);
2245 memset(cfqd
, 0, sizeof(*cfqd
));
2247 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
2248 INIT_LIST_HEAD(&cfqd
->rr_list
[i
]);
2250 INIT_LIST_HEAD(&cfqd
->busy_rr
);
2251 INIT_LIST_HEAD(&cfqd
->cur_rr
);
2252 INIT_LIST_HEAD(&cfqd
->idle_rr
);
2253 INIT_LIST_HEAD(&cfqd
->empty_list
);
2254 INIT_LIST_HEAD(&cfqd
->cic_list
);
2256 cfqd
->crq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_MHASH_ENTRIES
, GFP_KERNEL
);
2257 if (!cfqd
->crq_hash
)
2260 cfqd
->cfq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
);
2261 if (!cfqd
->cfq_hash
)
2264 cfqd
->crq_pool
= mempool_create_slab_pool(BLKDEV_MIN_RQ
, crq_pool
);
2265 if (!cfqd
->crq_pool
)
2268 for (i
= 0; i
< CFQ_MHASH_ENTRIES
; i
++)
2269 INIT_HLIST_HEAD(&cfqd
->crq_hash
[i
]);
2270 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
2271 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2275 init_timer(&cfqd
->idle_slice_timer
);
2276 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2277 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2279 init_timer(&cfqd
->idle_class_timer
);
2280 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2281 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2283 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
, q
);
2285 cfqd
->cfq_queued
= cfq_queued
;
2286 cfqd
->cfq_quantum
= cfq_quantum
;
2287 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2288 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2289 cfqd
->cfq_back_max
= cfq_back_max
;
2290 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2291 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2292 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2293 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2294 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2298 kfree(cfqd
->cfq_hash
);
2300 kfree(cfqd
->crq_hash
);
2306 static void cfq_slab_kill(void)
2309 kmem_cache_destroy(crq_pool
);
2311 kmem_cache_destroy(cfq_pool
);
2313 kmem_cache_destroy(cfq_ioc_pool
);
2316 static int __init
cfq_slab_setup(void)
2318 crq_pool
= kmem_cache_create("crq_pool", sizeof(struct cfq_rq
), 0, 0,
2323 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2328 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2329 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2340 * sysfs parts below -->
2344 cfq_var_show(unsigned int var
, char *page
)
2346 return sprintf(page
, "%d\n", var
);
2350 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2352 char *p
= (char *) page
;
2354 *var
= simple_strtoul(p
, &p
, 10);
2358 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2359 static ssize_t __FUNC(elevator_t *e, char *page) \
2361 struct cfq_data *cfqd = e->elevator_data; \
2362 unsigned int __data = __VAR; \
2364 __data = jiffies_to_msecs(__data); \
2365 return cfq_var_show(__data, (page)); \
2367 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2368 SHOW_FUNCTION(cfq_queued_show
, cfqd
->cfq_queued
, 0);
2369 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2370 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2371 SHOW_FUNCTION(cfq_back_seek_max_show
, cfqd
->cfq_back_max
, 0);
2372 SHOW_FUNCTION(cfq_back_seek_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2373 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2374 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2375 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2376 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2377 #undef SHOW_FUNCTION
2379 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2380 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2382 struct cfq_data *cfqd = e->elevator_data; \
2383 unsigned int __data; \
2384 int ret = cfq_var_store(&__data, (page), count); \
2385 if (__data < (MIN)) \
2387 else if (__data > (MAX)) \
2390 *(__PTR) = msecs_to_jiffies(__data); \
2392 *(__PTR) = __data; \
2395 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2396 STORE_FUNCTION(cfq_queued_store
, &cfqd
->cfq_queued
, 1, UINT_MAX
, 0);
2397 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2398 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2399 STORE_FUNCTION(cfq_back_seek_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2400 STORE_FUNCTION(cfq_back_seek_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2401 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2402 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2403 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2404 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2405 #undef STORE_FUNCTION
2407 #define CFQ_ATTR(name) \
2408 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2410 static struct elv_fs_entry cfq_attrs
[] = {
2413 CFQ_ATTR(fifo_expire_sync
),
2414 CFQ_ATTR(fifo_expire_async
),
2415 CFQ_ATTR(back_seek_max
),
2416 CFQ_ATTR(back_seek_penalty
),
2417 CFQ_ATTR(slice_sync
),
2418 CFQ_ATTR(slice_async
),
2419 CFQ_ATTR(slice_async_rq
),
2420 CFQ_ATTR(slice_idle
),
2424 static struct elevator_type iosched_cfq
= {
2426 .elevator_merge_fn
= cfq_merge
,
2427 .elevator_merged_fn
= cfq_merged_request
,
2428 .elevator_merge_req_fn
= cfq_merged_requests
,
2429 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2430 .elevator_add_req_fn
= cfq_insert_request
,
2431 .elevator_activate_req_fn
= cfq_activate_request
,
2432 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2433 .elevator_queue_empty_fn
= cfq_queue_empty
,
2434 .elevator_completed_req_fn
= cfq_completed_request
,
2435 .elevator_former_req_fn
= cfq_former_request
,
2436 .elevator_latter_req_fn
= cfq_latter_request
,
2437 .elevator_set_req_fn
= cfq_set_request
,
2438 .elevator_put_req_fn
= cfq_put_request
,
2439 .elevator_may_queue_fn
= cfq_may_queue
,
2440 .elevator_init_fn
= cfq_init_queue
,
2441 .elevator_exit_fn
= cfq_exit_queue
,
2444 .elevator_attrs
= cfq_attrs
,
2445 .elevator_name
= "cfq",
2446 .elevator_owner
= THIS_MODULE
,
2449 static int __init
cfq_init(void)
2454 * could be 0 on HZ < 1000 setups
2456 if (!cfq_slice_async
)
2457 cfq_slice_async
= 1;
2458 if (!cfq_slice_idle
)
2461 if (cfq_slab_setup())
2464 ret
= elv_register(&iosched_cfq
);
2471 static void __exit
cfq_exit(void)
2473 DECLARE_COMPLETION(all_gone
);
2474 elv_unregister(&iosched_cfq
);
2475 ioc_gone
= &all_gone
;
2476 /* ioc_gone's update must be visible before reading ioc_count */
2478 if (atomic_read(&ioc_count
))
2479 wait_for_completion(ioc_gone
);
2484 module_init(cfq_init
);
2485 module_exit(cfq_exit
);
2487 MODULE_AUTHOR("Jens Axboe");
2488 MODULE_LICENSE("GPL");
2489 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");