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/kernel.h>
11 #include <linux/blkdev.h>
12 #include <linux/elevator.h>
13 #include <linux/bio.h>
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/compiler.h>
19 #include <linux/hash.h>
20 #include <linux/rbtree.h>
21 #include <linux/mempool.h>
22 #include <linux/ioprio.h>
23 #include <linux/writeback.h>
28 static int cfq_quantum
= 4; /* max queue in one round of service */
29 static int cfq_queued
= 8; /* minimum rq allocate limit per-queue*/
30 static int cfq_fifo_expire
[2] = { HZ
/ 4, HZ
/ 8 };
31 static int cfq_back_max
= 16 * 1024; /* maximum backwards seek, in KiB */
32 static int cfq_back_penalty
= 2; /* penalty of a backwards seek */
34 static int cfq_slice_sync
= HZ
/ 10;
35 static int cfq_slice_async
= HZ
/ 25;
36 static int cfq_slice_async_rq
= 2;
37 static int cfq_slice_idle
= HZ
/ 100;
39 #define CFQ_IDLE_GRACE (HZ / 10)
40 #define CFQ_SLICE_SCALE (5)
42 #define CFQ_KEY_ASYNC (0)
43 #define CFQ_KEY_ANY (0xffff)
46 * disable queueing at the driver/hardware level
48 static int cfq_max_depth
= 2;
51 * for the hash of cfqq inside the cfqd
53 #define CFQ_QHASH_SHIFT 6
54 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
55 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
58 * for the hash of crq inside the cfqq
60 #define CFQ_MHASH_SHIFT 6
61 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
62 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
63 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
64 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
65 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
67 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
68 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
70 #define RQ_DATA(rq) (rq)->elevator_private
76 #define RB_EMPTY(node) ((node)->rb_node == NULL)
77 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
78 #define RB_CLEAR(node) do { \
79 (node)->rb_parent = NULL; \
80 RB_CLEAR_COLOR((node)); \
81 (node)->rb_right = NULL; \
82 (node)->rb_left = NULL; \
84 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
85 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
86 #define rq_rb_key(rq) (rq)->sector
88 static kmem_cache_t
*crq_pool
;
89 static kmem_cache_t
*cfq_pool
;
90 static kmem_cache_t
*cfq_ioc_pool
;
92 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
93 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
94 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
95 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
100 #define cfq_cfqq_dispatched(cfqq) \
101 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
103 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
105 #define cfq_cfqq_sync(cfqq) \
106 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
109 * Per block device queue structure
113 request_queue_t
*queue
;
116 * rr list of queues with requests and the count of them
118 struct list_head rr_list
[CFQ_PRIO_LISTS
];
119 struct list_head busy_rr
;
120 struct list_head cur_rr
;
121 struct list_head idle_rr
;
122 unsigned int busy_queues
;
125 * non-ordered list of empty cfqq's
127 struct list_head empty_list
;
132 struct hlist_head
*cfq_hash
;
135 * global crq hash for all queues
137 struct hlist_head
*crq_hash
;
139 unsigned int max_queued
;
146 * schedule slice state info
149 * idle window management
151 struct timer_list idle_slice_timer
;
152 struct work_struct unplug_work
;
154 struct cfq_queue
*active_queue
;
155 struct cfq_io_context
*active_cic
;
156 int cur_prio
, cur_end_prio
;
157 unsigned int dispatch_slice
;
159 struct timer_list idle_class_timer
;
161 sector_t last_sector
;
162 unsigned long last_end_request
;
164 unsigned int rq_starved
;
167 * tunables, see top of file
169 unsigned int cfq_quantum
;
170 unsigned int cfq_queued
;
171 unsigned int cfq_fifo_expire
[2];
172 unsigned int cfq_back_penalty
;
173 unsigned int cfq_back_max
;
174 unsigned int cfq_slice
[2];
175 unsigned int cfq_slice_async_rq
;
176 unsigned int cfq_slice_idle
;
177 unsigned int cfq_max_depth
;
181 * Per process-grouping structure
184 /* reference count */
186 /* parent cfq_data */
187 struct cfq_data
*cfqd
;
188 /* cfqq lookup hash */
189 struct hlist_node cfq_hash
;
192 /* on either rr or empty list of cfqd */
193 struct list_head cfq_list
;
194 /* sorted list of pending requests */
195 struct rb_root sort_list
;
196 /* if fifo isn't expired, next request to serve */
197 struct cfq_rq
*next_crq
;
198 /* requests queued in sort_list */
200 /* currently allocated requests */
202 /* fifo list of requests in sort_list */
203 struct list_head fifo
;
205 unsigned long slice_start
;
206 unsigned long slice_end
;
207 unsigned long slice_left
;
208 unsigned long service_last
;
210 /* number of requests that are on the dispatch list */
213 /* io prio of this group */
214 unsigned short ioprio
, org_ioprio
;
215 unsigned short ioprio_class
, org_ioprio_class
;
217 /* various state flags, see below */
222 struct rb_node rb_node
;
224 struct request
*request
;
225 struct hlist_node hash
;
227 struct cfq_queue
*cfq_queue
;
228 struct cfq_io_context
*io_context
;
230 unsigned int crq_flags
;
233 enum cfqq_state_flags
{
234 CFQ_CFQQ_FLAG_on_rr
= 0,
235 CFQ_CFQQ_FLAG_wait_request
,
236 CFQ_CFQQ_FLAG_must_alloc
,
237 CFQ_CFQQ_FLAG_must_alloc_slice
,
238 CFQ_CFQQ_FLAG_must_dispatch
,
239 CFQ_CFQQ_FLAG_fifo_expire
,
240 CFQ_CFQQ_FLAG_idle_window
,
241 CFQ_CFQQ_FLAG_prio_changed
,
242 CFQ_CFQQ_FLAG_expired
,
245 #define CFQ_CFQQ_FNS(name) \
246 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
248 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
250 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
252 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
254 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
256 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
260 CFQ_CFQQ_FNS(wait_request
);
261 CFQ_CFQQ_FNS(must_alloc
);
262 CFQ_CFQQ_FNS(must_alloc_slice
);
263 CFQ_CFQQ_FNS(must_dispatch
);
264 CFQ_CFQQ_FNS(fifo_expire
);
265 CFQ_CFQQ_FNS(idle_window
);
266 CFQ_CFQQ_FNS(prio_changed
);
267 CFQ_CFQQ_FNS(expired
);
270 enum cfq_rq_state_flags
{
271 CFQ_CRQ_FLAG_is_sync
= 0,
274 #define CFQ_CRQ_FNS(name) \
275 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
277 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
279 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
281 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
283 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
285 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
288 CFQ_CRQ_FNS(is_sync
);
291 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
292 static void cfq_dispatch_insert(request_queue_t
*, struct cfq_rq
*);
293 static void cfq_put_cfqd(struct cfq_data
*cfqd
);
295 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
298 * lots of deadline iosched dupes, can be abstracted later...
300 static inline void cfq_del_crq_hash(struct cfq_rq
*crq
)
302 hlist_del_init(&crq
->hash
);
305 static inline void cfq_add_crq_hash(struct cfq_data
*cfqd
, struct cfq_rq
*crq
)
307 const int hash_idx
= CFQ_MHASH_FN(rq_hash_key(crq
->request
));
309 hlist_add_head(&crq
->hash
, &cfqd
->crq_hash
[hash_idx
]);
312 static struct request
*cfq_find_rq_hash(struct cfq_data
*cfqd
, sector_t offset
)
314 struct hlist_head
*hash_list
= &cfqd
->crq_hash
[CFQ_MHASH_FN(offset
)];
315 struct hlist_node
*entry
, *next
;
317 hlist_for_each_safe(entry
, next
, hash_list
) {
318 struct cfq_rq
*crq
= list_entry_hash(entry
);
319 struct request
*__rq
= crq
->request
;
321 if (!rq_mergeable(__rq
)) {
322 cfq_del_crq_hash(crq
);
326 if (rq_hash_key(__rq
) == offset
)
334 * scheduler run of queue, if there are requests pending and no one in the
335 * driver that will restart queueing
337 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
339 if (!cfqd
->rq_in_driver
&& cfqd
->busy_queues
)
340 kblockd_schedule_work(&cfqd
->unplug_work
);
343 static int cfq_queue_empty(request_queue_t
*q
)
345 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
347 return !cfqd
->busy_queues
;
351 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
352 * We choose the request that is closest to the head right now. Distance
353 * behind the head are penalized and only allowed to a certain extent.
355 static struct cfq_rq
*
356 cfq_choose_req(struct cfq_data
*cfqd
, struct cfq_rq
*crq1
, struct cfq_rq
*crq2
)
358 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
359 int r1_wrap
= 0, r2_wrap
= 0; /* requests are behind the disk head */
360 unsigned long back_max
;
362 if (crq1
== NULL
|| crq1
== crq2
)
367 if (cfq_crq_is_sync(crq1
) && !cfq_crq_is_sync(crq2
))
369 else if (cfq_crq_is_sync(crq2
) && !cfq_crq_is_sync(crq1
))
372 s1
= crq1
->request
->sector
;
373 s2
= crq2
->request
->sector
;
375 last
= cfqd
->last_sector
;
378 * by definition, 1KiB is 2 sectors
380 back_max
= cfqd
->cfq_back_max
* 2;
383 * Strict one way elevator _except_ in the case where we allow
384 * short backward seeks which are biased as twice the cost of a
385 * similar forward seek.
389 else if (s1
+ back_max
>= last
)
390 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
396 else if (s2
+ back_max
>= last
)
397 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
401 /* Found required data */
402 if (!r1_wrap
&& r2_wrap
)
404 else if (!r2_wrap
&& r1_wrap
)
406 else if (r1_wrap
&& r2_wrap
) {
407 /* both behind the head */
414 /* Both requests in front of the head */
428 * would be nice to take fifo expire time into account as well
430 static struct cfq_rq
*
431 cfq_find_next_crq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
434 struct cfq_rq
*crq_next
= NULL
, *crq_prev
= NULL
;
435 struct rb_node
*rbnext
, *rbprev
;
437 if (!(rbnext
= rb_next(&last
->rb_node
))) {
438 rbnext
= rb_first(&cfqq
->sort_list
);
439 if (rbnext
== &last
->rb_node
)
443 rbprev
= rb_prev(&last
->rb_node
);
446 crq_prev
= rb_entry_crq(rbprev
);
448 crq_next
= rb_entry_crq(rbnext
);
450 return cfq_choose_req(cfqd
, crq_next
, crq_prev
);
453 static void cfq_update_next_crq(struct cfq_rq
*crq
)
455 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
457 if (cfqq
->next_crq
== crq
)
458 cfqq
->next_crq
= cfq_find_next_crq(cfqq
->cfqd
, cfqq
, crq
);
461 static void cfq_resort_rr_list(struct cfq_queue
*cfqq
, int preempted
)
463 struct cfq_data
*cfqd
= cfqq
->cfqd
;
464 struct list_head
*list
, *entry
;
466 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
468 list_del(&cfqq
->cfq_list
);
470 if (cfq_class_rt(cfqq
))
471 list
= &cfqd
->cur_rr
;
472 else if (cfq_class_idle(cfqq
))
473 list
= &cfqd
->idle_rr
;
476 * if cfqq has requests in flight, don't allow it to be
477 * found in cfq_set_active_queue before it has finished them.
478 * this is done to increase fairness between a process that
479 * has lots of io pending vs one that only generates one
480 * sporadically or synchronously
482 if (cfq_cfqq_dispatched(cfqq
))
483 list
= &cfqd
->busy_rr
;
485 list
= &cfqd
->rr_list
[cfqq
->ioprio
];
489 * if queue was preempted, just add to front to be fair. busy_rr
492 if (preempted
|| list
== &cfqd
->busy_rr
) {
493 list_add(&cfqq
->cfq_list
, list
);
498 * sort by when queue was last serviced
501 while ((entry
= entry
->prev
) != list
) {
502 struct cfq_queue
*__cfqq
= list_entry_cfqq(entry
);
504 if (!__cfqq
->service_last
)
506 if (time_before(__cfqq
->service_last
, cfqq
->service_last
))
510 list_add(&cfqq
->cfq_list
, entry
);
514 * add to busy list of queues for service, trying to be fair in ordering
515 * the pending list according to last request service
518 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
520 BUG_ON(cfq_cfqq_on_rr(cfqq
));
521 cfq_mark_cfqq_on_rr(cfqq
);
524 cfq_resort_rr_list(cfqq
, 0);
528 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
530 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
531 cfq_clear_cfqq_on_rr(cfqq
);
532 list_move(&cfqq
->cfq_list
, &cfqd
->empty_list
);
534 BUG_ON(!cfqd
->busy_queues
);
539 * rb tree support functions
541 static inline void cfq_del_crq_rb(struct cfq_rq
*crq
)
543 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
544 struct cfq_data
*cfqd
= cfqq
->cfqd
;
545 const int sync
= cfq_crq_is_sync(crq
);
547 BUG_ON(!cfqq
->queued
[sync
]);
548 cfqq
->queued
[sync
]--;
550 cfq_update_next_crq(crq
);
552 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
553 RB_CLEAR_COLOR(&crq
->rb_node
);
555 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY(&cfqq
->sort_list
))
556 cfq_del_cfqq_rr(cfqd
, cfqq
);
559 static struct cfq_rq
*
560 __cfq_add_crq_rb(struct cfq_rq
*crq
)
562 struct rb_node
**p
= &crq
->cfq_queue
->sort_list
.rb_node
;
563 struct rb_node
*parent
= NULL
;
564 struct cfq_rq
*__crq
;
568 __crq
= rb_entry_crq(parent
);
570 if (crq
->rb_key
< __crq
->rb_key
)
572 else if (crq
->rb_key
> __crq
->rb_key
)
578 rb_link_node(&crq
->rb_node
, parent
, p
);
582 static void cfq_add_crq_rb(struct cfq_rq
*crq
)
584 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
585 struct cfq_data
*cfqd
= cfqq
->cfqd
;
586 struct request
*rq
= crq
->request
;
587 struct cfq_rq
*__alias
;
589 crq
->rb_key
= rq_rb_key(rq
);
590 cfqq
->queued
[cfq_crq_is_sync(crq
)]++;
593 * looks a little odd, but the first insert might return an alias.
594 * if that happens, put the alias on the dispatch list
596 while ((__alias
= __cfq_add_crq_rb(crq
)) != NULL
)
597 cfq_dispatch_insert(cfqd
->queue
, __alias
);
599 rb_insert_color(&crq
->rb_node
, &cfqq
->sort_list
);
601 if (!cfq_cfqq_on_rr(cfqq
))
602 cfq_add_cfqq_rr(cfqd
, cfqq
);
605 * check if this request is a better next-serve candidate
607 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
611 cfq_reposition_crq_rb(struct cfq_queue
*cfqq
, struct cfq_rq
*crq
)
613 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
614 cfqq
->queued
[cfq_crq_is_sync(crq
)]--;
619 static struct request
*cfq_find_rq_rb(struct cfq_data
*cfqd
, sector_t sector
)
622 struct cfq_queue
*cfqq
= cfq_find_cfq_hash(cfqd
, current
->pid
, CFQ_KEY_ANY
);
628 n
= cfqq
->sort_list
.rb_node
;
630 struct cfq_rq
*crq
= rb_entry_crq(n
);
632 if (sector
< crq
->rb_key
)
634 else if (sector
> crq
->rb_key
)
644 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
646 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
648 cfqd
->rq_in_driver
++;
651 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
653 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
655 WARN_ON(!cfqd
->rq_in_driver
);
656 cfqd
->rq_in_driver
--;
659 static void cfq_remove_request(struct request
*rq
)
661 struct cfq_rq
*crq
= RQ_DATA(rq
);
663 list_del_init(&rq
->queuelist
);
665 cfq_del_crq_hash(crq
);
669 cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
671 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
672 struct request
*__rq
;
675 __rq
= cfq_find_rq_hash(cfqd
, bio
->bi_sector
);
676 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
677 ret
= ELEVATOR_BACK_MERGE
;
681 __rq
= cfq_find_rq_rb(cfqd
, bio
->bi_sector
+ bio_sectors(bio
));
682 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
683 ret
= ELEVATOR_FRONT_MERGE
;
687 return ELEVATOR_NO_MERGE
;
693 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
)
695 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
696 struct cfq_rq
*crq
= RQ_DATA(req
);
698 cfq_del_crq_hash(crq
);
699 cfq_add_crq_hash(cfqd
, crq
);
701 if (rq_rb_key(req
) != crq
->rb_key
) {
702 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
704 cfq_update_next_crq(crq
);
705 cfq_reposition_crq_rb(cfqq
, crq
);
710 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
711 struct request
*next
)
713 cfq_merged_request(q
, rq
);
716 * reposition in fifo if next is older than rq
718 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
719 time_before(next
->start_time
, rq
->start_time
))
720 list_move(&rq
->queuelist
, &next
->queuelist
);
722 cfq_remove_request(next
);
726 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
730 * stop potential idle class queues waiting service
732 del_timer(&cfqd
->idle_class_timer
);
734 cfqq
->slice_start
= jiffies
;
736 cfqq
->slice_left
= 0;
737 cfq_clear_cfqq_must_alloc_slice(cfqq
);
738 cfq_clear_cfqq_fifo_expire(cfqq
);
739 cfq_clear_cfqq_expired(cfqq
);
742 cfqd
->active_queue
= cfqq
;
755 static int cfq_get_next_prio_level(struct cfq_data
*cfqd
)
764 for (p
= cfqd
->cur_prio
; p
<= cfqd
->cur_end_prio
; p
++) {
765 if (!list_empty(&cfqd
->rr_list
[p
])) {
774 if (++cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
775 cfqd
->cur_end_prio
= 0;
782 if (unlikely(prio
== -1))
785 BUG_ON(prio
>= CFQ_PRIO_LISTS
);
787 list_splice_init(&cfqd
->rr_list
[prio
], &cfqd
->cur_rr
);
789 cfqd
->cur_prio
= prio
+ 1;
790 if (cfqd
->cur_prio
> cfqd
->cur_end_prio
) {
791 cfqd
->cur_end_prio
= cfqd
->cur_prio
;
794 if (cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
796 cfqd
->cur_end_prio
= 0;
802 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
804 struct cfq_queue
*cfqq
;
807 * if current queue is expired but not done with its requests yet,
808 * wait for that to happen
810 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
811 if (cfq_cfqq_expired(cfqq
) && cfq_cfqq_dispatched(cfqq
))
816 * if current list is non-empty, grab first entry. if it is empty,
817 * get next prio level and grab first entry then if any are spliced
819 if (!list_empty(&cfqd
->cur_rr
) || cfq_get_next_prio_level(cfqd
) != -1)
820 cfqq
= list_entry_cfqq(cfqd
->cur_rr
.next
);
823 * if we have idle queues and no rt or be queues had pending
824 * requests, either allow immediate service if the grace period
825 * has passed or arm the idle grace timer
827 if (!cfqq
&& !list_empty(&cfqd
->idle_rr
)) {
828 unsigned long end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
830 if (time_after_eq(jiffies
, end
))
831 cfqq
= list_entry_cfqq(cfqd
->idle_rr
.next
);
833 mod_timer(&cfqd
->idle_class_timer
, end
);
836 __cfq_set_active_queue(cfqd
, cfqq
);
841 * current cfqq expired its slice (or was too idle), select new one
844 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
847 unsigned long now
= jiffies
;
849 if (cfq_cfqq_wait_request(cfqq
))
850 del_timer(&cfqd
->idle_slice_timer
);
852 if (!preempted
&& !cfq_cfqq_dispatched(cfqq
))
853 cfqq
->service_last
= now
;
855 cfq_clear_cfqq_must_dispatch(cfqq
);
856 cfq_clear_cfqq_wait_request(cfqq
);
859 * store what was left of this slice, if the queue idled out
862 if (time_after(cfqq
->slice_end
, now
))
863 cfqq
->slice_left
= cfqq
->slice_end
- now
;
865 cfqq
->slice_left
= 0;
867 if (cfq_cfqq_on_rr(cfqq
))
868 cfq_resort_rr_list(cfqq
, preempted
);
870 if (cfqq
== cfqd
->active_queue
)
871 cfqd
->active_queue
= NULL
;
873 if (cfqd
->active_cic
) {
874 put_io_context(cfqd
->active_cic
->ioc
);
875 cfqd
->active_cic
= NULL
;
878 cfqd
->dispatch_slice
= 0;
881 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int preempted
)
883 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
887 * use deferred expiry, if there are requests in progress as
888 * not to disturb the slice of the next queue
890 if (cfq_cfqq_dispatched(cfqq
))
891 cfq_mark_cfqq_expired(cfqq
);
893 __cfq_slice_expired(cfqd
, cfqq
, preempted
);
897 static int cfq_arm_slice_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
900 WARN_ON(!RB_EMPTY(&cfqq
->sort_list
));
901 WARN_ON(cfqq
!= cfqd
->active_queue
);
904 * idle is disabled, either manually or by past process history
906 if (!cfqd
->cfq_slice_idle
)
908 if (!cfq_cfqq_idle_window(cfqq
))
911 * task has exited, don't wait
913 if (cfqd
->active_cic
&& !cfqd
->active_cic
->ioc
->task
)
916 cfq_mark_cfqq_must_dispatch(cfqq
);
917 cfq_mark_cfqq_wait_request(cfqq
);
919 if (!timer_pending(&cfqd
->idle_slice_timer
)) {
920 unsigned long slice_left
= min(cfqq
->slice_end
- 1, (unsigned long) cfqd
->cfq_slice_idle
);
922 cfqd
->idle_slice_timer
.expires
= jiffies
+ slice_left
;
923 add_timer(&cfqd
->idle_slice_timer
);
929 static void cfq_dispatch_insert(request_queue_t
*q
, struct cfq_rq
*crq
)
931 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
932 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
934 cfqq
->next_crq
= cfq_find_next_crq(cfqd
, cfqq
, crq
);
935 cfq_remove_request(crq
->request
);
936 cfqq
->on_dispatch
[cfq_crq_is_sync(crq
)]++;
937 elv_dispatch_sort(q
, crq
->request
);
941 * return expired entry, or NULL to just start from scratch in rbtree
943 static inline struct cfq_rq
*cfq_check_fifo(struct cfq_queue
*cfqq
)
945 struct cfq_data
*cfqd
= cfqq
->cfqd
;
949 if (cfq_cfqq_fifo_expire(cfqq
))
952 if (!list_empty(&cfqq
->fifo
)) {
953 int fifo
= cfq_cfqq_class_sync(cfqq
);
955 crq
= RQ_DATA(list_entry_fifo(cfqq
->fifo
.next
));
957 if (time_after(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
])) {
958 cfq_mark_cfqq_fifo_expire(cfqq
);
967 * Scale schedule slice based on io priority. Use the sync time slice only
968 * if a queue is marked sync and has sync io queued. A sync queue with async
969 * io only, should not get full sync slice length.
972 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
974 const int base_slice
= cfqd
->cfq_slice
[cfq_cfqq_sync(cfqq
)];
976 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
978 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - cfqq
->ioprio
));
982 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
984 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
988 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
990 const int base_rq
= cfqd
->cfq_slice_async_rq
;
992 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
994 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
998 * get next queue for service
1000 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
1002 unsigned long now
= jiffies
;
1003 struct cfq_queue
*cfqq
;
1005 cfqq
= cfqd
->active_queue
;
1009 if (cfq_cfqq_expired(cfqq
))
1015 if (!cfq_cfqq_must_dispatch(cfqq
) && time_after(now
, cfqq
->slice_end
))
1019 * if queue has requests, dispatch one. if not, check if
1020 * enough slice is left to wait for one
1022 if (!RB_EMPTY(&cfqq
->sort_list
))
1024 else if (cfq_cfqq_class_sync(cfqq
) &&
1025 time_before(now
, cfqq
->slice_end
)) {
1026 if (cfq_arm_slice_timer(cfqd
, cfqq
))
1031 cfq_slice_expired(cfqd
, 0);
1033 cfqq
= cfq_set_active_queue(cfqd
);
1039 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1044 BUG_ON(RB_EMPTY(&cfqq
->sort_list
));
1050 * follow expired path, else get first next available
1052 if ((crq
= cfq_check_fifo(cfqq
)) == NULL
)
1053 crq
= cfqq
->next_crq
;
1056 * finally, insert request into driver dispatch list
1058 cfq_dispatch_insert(cfqd
->queue
, crq
);
1060 cfqd
->dispatch_slice
++;
1063 if (!cfqd
->active_cic
) {
1064 atomic_inc(&crq
->io_context
->ioc
->refcount
);
1065 cfqd
->active_cic
= crq
->io_context
;
1068 if (RB_EMPTY(&cfqq
->sort_list
))
1071 } while (dispatched
< max_dispatch
);
1074 * if slice end isn't set yet, set it. if at least one request was
1075 * sync, use the sync time slice value
1077 if (!cfqq
->slice_end
)
1078 cfq_set_prio_slice(cfqd
, cfqq
);
1081 * expire an async queue immediately if it has used up its slice. idle
1082 * queue always expire after 1 dispatch round.
1084 if ((!cfq_cfqq_sync(cfqq
) &&
1085 cfqd
->dispatch_slice
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
1086 cfq_class_idle(cfqq
))
1087 cfq_slice_expired(cfqd
, 0);
1093 cfq_forced_dispatch_cfqqs(struct list_head
*list
)
1096 struct cfq_queue
*cfqq
, *next
;
1099 list_for_each_entry_safe(cfqq
, next
, list
, cfq_list
) {
1100 while ((crq
= cfqq
->next_crq
)) {
1101 cfq_dispatch_insert(cfqq
->cfqd
->queue
, crq
);
1104 BUG_ON(!list_empty(&cfqq
->fifo
));
1110 cfq_forced_dispatch(struct cfq_data
*cfqd
)
1112 int i
, dispatched
= 0;
1114 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
1115 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->rr_list
[i
]);
1117 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->busy_rr
);
1118 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->cur_rr
);
1119 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->idle_rr
);
1121 cfq_slice_expired(cfqd
, 0);
1123 BUG_ON(cfqd
->busy_queues
);
1129 cfq_dispatch_requests(request_queue_t
*q
, int force
)
1131 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1132 struct cfq_queue
*cfqq
;
1134 if (!cfqd
->busy_queues
)
1137 if (unlikely(force
))
1138 return cfq_forced_dispatch(cfqd
);
1140 cfqq
= cfq_select_queue(cfqd
);
1145 * if idle window is disabled, allow queue buildup
1147 if (!cfq_cfqq_idle_window(cfqq
) &&
1148 cfqd
->rq_in_driver
>= cfqd
->cfq_max_depth
)
1151 cfq_clear_cfqq_must_dispatch(cfqq
);
1152 cfq_clear_cfqq_wait_request(cfqq
);
1153 del_timer(&cfqd
->idle_slice_timer
);
1155 max_dispatch
= cfqd
->cfq_quantum
;
1156 if (cfq_class_idle(cfqq
))
1159 return __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1166 * task holds one reference to the queue, dropped when task exits. each crq
1167 * in-flight on this queue also holds a reference, dropped when crq is freed.
1169 * queue lock must be held here.
1171 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1173 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1175 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1177 if (!atomic_dec_and_test(&cfqq
->ref
))
1180 BUG_ON(rb_first(&cfqq
->sort_list
));
1181 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1182 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1184 if (unlikely(cfqd
->active_queue
== cfqq
)) {
1185 __cfq_slice_expired(cfqd
, cfqq
, 0);
1186 cfq_schedule_dispatch(cfqd
);
1189 cfq_put_cfqd(cfqq
->cfqd
);
1192 * it's on the empty list and still hashed
1194 list_del(&cfqq
->cfq_list
);
1195 hlist_del(&cfqq
->cfq_hash
);
1196 kmem_cache_free(cfq_pool
, cfqq
);
1199 static inline struct cfq_queue
*
1200 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1203 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1204 struct hlist_node
*entry
, *next
;
1206 hlist_for_each_safe(entry
, next
, hash_list
) {
1207 struct cfq_queue
*__cfqq
= list_entry_qhash(entry
);
1208 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->ioprio_class
, __cfqq
->ioprio
);
1210 if (__cfqq
->key
== key
&& (__p
== prio
|| prio
== CFQ_KEY_ANY
))
1217 static struct cfq_queue
*
1218 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1220 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1223 static void cfq_free_io_context(struct cfq_io_context
*cic
)
1225 struct cfq_io_context
*__cic
;
1226 struct list_head
*entry
, *next
;
1228 list_for_each_safe(entry
, next
, &cic
->list
) {
1229 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1230 kmem_cache_free(cfq_ioc_pool
, __cic
);
1233 kmem_cache_free(cfq_ioc_pool
, cic
);
1237 * Called with interrupts disabled
1239 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1241 struct cfq_data
*cfqd
= cic
->cfqq
->cfqd
;
1242 request_queue_t
*q
= cfqd
->queue
;
1244 WARN_ON(!irqs_disabled());
1246 spin_lock(q
->queue_lock
);
1248 if (unlikely(cic
->cfqq
== cfqd
->active_queue
)) {
1249 __cfq_slice_expired(cfqd
, cic
->cfqq
, 0);
1250 cfq_schedule_dispatch(cfqd
);
1253 cfq_put_queue(cic
->cfqq
);
1255 spin_unlock(q
->queue_lock
);
1259 * Another task may update the task cic list, if it is doing a queue lookup
1260 * on its behalf. cfq_cic_lock excludes such concurrent updates
1262 static void cfq_exit_io_context(struct cfq_io_context
*cic
)
1264 struct cfq_io_context
*__cic
;
1265 struct list_head
*entry
;
1266 unsigned long flags
;
1268 local_irq_save(flags
);
1271 * put the reference this task is holding to the various queues
1273 list_for_each(entry
, &cic
->list
) {
1274 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1275 cfq_exit_single_io_context(__cic
);
1278 cfq_exit_single_io_context(cic
);
1279 local_irq_restore(flags
);
1282 static struct cfq_io_context
*
1283 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1285 struct cfq_io_context
*cic
= kmem_cache_alloc(cfq_ioc_pool
, gfp_mask
);
1288 INIT_LIST_HEAD(&cic
->list
);
1291 cic
->last_end_request
= jiffies
;
1292 cic
->ttime_total
= 0;
1293 cic
->ttime_samples
= 0;
1294 cic
->ttime_mean
= 0;
1295 cic
->dtor
= cfq_free_io_context
;
1296 cic
->exit
= cfq_exit_io_context
;
1302 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1304 struct task_struct
*tsk
= current
;
1307 if (!cfq_cfqq_prio_changed(cfqq
))
1310 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1311 switch (ioprio_class
) {
1313 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1314 case IOPRIO_CLASS_NONE
:
1316 * no prio set, place us in the middle of the BE classes
1318 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1319 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1321 case IOPRIO_CLASS_RT
:
1322 cfqq
->ioprio
= task_ioprio(tsk
);
1323 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1325 case IOPRIO_CLASS_BE
:
1326 cfqq
->ioprio
= task_ioprio(tsk
);
1327 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1329 case IOPRIO_CLASS_IDLE
:
1330 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1332 cfq_clear_cfqq_idle_window(cfqq
);
1337 * keep track of original prio settings in case we have to temporarily
1338 * elevate the priority of this queue
1340 cfqq
->org_ioprio
= cfqq
->ioprio
;
1341 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1343 if (cfq_cfqq_on_rr(cfqq
))
1344 cfq_resort_rr_list(cfqq
, 0);
1346 cfq_clear_cfqq_prio_changed(cfqq
);
1349 static inline void changed_ioprio(struct cfq_queue
*cfqq
)
1352 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1354 spin_lock(cfqd
->queue
->queue_lock
);
1355 cfq_mark_cfqq_prio_changed(cfqq
);
1356 cfq_init_prio_data(cfqq
);
1357 spin_unlock(cfqd
->queue
->queue_lock
);
1362 * callback from sys_ioprio_set, irqs are disabled
1364 static int cfq_ioc_set_ioprio(struct io_context
*ioc
, unsigned int ioprio
)
1366 struct cfq_io_context
*cic
= ioc
->cic
;
1368 changed_ioprio(cic
->cfqq
);
1370 list_for_each_entry(cic
, &cic
->list
, list
)
1371 changed_ioprio(cic
->cfqq
);
1376 static struct cfq_queue
*
1377 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, unsigned short ioprio
,
1380 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1381 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1384 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1390 } else if (gfp_mask
& __GFP_WAIT
) {
1391 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1392 new_cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1393 spin_lock_irq(cfqd
->queue
->queue_lock
);
1396 cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1401 memset(cfqq
, 0, sizeof(*cfqq
));
1403 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1404 INIT_LIST_HEAD(&cfqq
->cfq_list
);
1405 RB_CLEAR_ROOT(&cfqq
->sort_list
);
1406 INIT_LIST_HEAD(&cfqq
->fifo
);
1409 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1410 atomic_set(&cfqq
->ref
, 0);
1412 atomic_inc(&cfqd
->ref
);
1413 cfqq
->service_last
= 0;
1415 * set ->slice_left to allow preemption for a new process
1417 cfqq
->slice_left
= 2 * cfqd
->cfq_slice_idle
;
1418 cfq_mark_cfqq_idle_window(cfqq
);
1419 cfq_mark_cfqq_prio_changed(cfqq
);
1420 cfq_init_prio_data(cfqq
);
1424 kmem_cache_free(cfq_pool
, new_cfqq
);
1426 atomic_inc(&cfqq
->ref
);
1428 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1433 * Setup general io context and cfq io context. There can be several cfq
1434 * io contexts per general io context, if this process is doing io to more
1435 * than one device managed by cfq. Note that caller is holding a reference to
1436 * cfqq, so we don't need to worry about it disappearing
1438 static struct cfq_io_context
*
1439 cfq_get_io_context(struct cfq_data
*cfqd
, pid_t pid
, gfp_t gfp_mask
)
1441 struct io_context
*ioc
= NULL
;
1442 struct cfq_io_context
*cic
;
1444 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1446 ioc
= get_io_context(gfp_mask
);
1450 if ((cic
= ioc
->cic
) == NULL
) {
1451 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1457 * manually increment generic io_context usage count, it
1458 * cannot go away since we are already holding one ref to it
1461 ioc
->set_ioprio
= cfq_ioc_set_ioprio
;
1464 atomic_inc(&cfqd
->ref
);
1466 struct cfq_io_context
*__cic
;
1469 * the first cic on the list is actually the head itself
1471 if (cic
->key
== cfqd
)
1475 * cic exists, check if we already are there. linear search
1476 * should be ok here, the list will usually not be more than
1477 * 1 or a few entries long
1479 list_for_each_entry(__cic
, &cic
->list
, list
) {
1481 * this process is already holding a reference to
1482 * this queue, so no need to get one more
1484 if (__cic
->key
== cfqd
) {
1491 * nope, process doesn't have a cic assoicated with this
1492 * cfqq yet. get a new one and add to list
1494 __cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1500 atomic_inc(&cfqd
->ref
);
1501 list_add(&__cic
->list
, &cic
->list
);
1508 put_io_context(ioc
);
1513 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1515 unsigned long elapsed
, ttime
;
1518 * if this context already has stuff queued, thinktime is from
1519 * last queue not last end
1522 if (time_after(cic
->last_end_request
, cic
->last_queue
))
1523 elapsed
= jiffies
- cic
->last_end_request
;
1525 elapsed
= jiffies
- cic
->last_queue
;
1527 elapsed
= jiffies
- cic
->last_end_request
;
1530 ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1532 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1533 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1534 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1537 #define sample_valid(samples) ((samples) > 80)
1540 * Disable idle window if the process thinks too long or seeks so much that
1544 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1545 struct cfq_io_context
*cic
)
1547 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1549 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
)
1551 else if (sample_valid(cic
->ttime_samples
)) {
1552 if (cic
->ttime_mean
> cfqd
->cfq_slice_idle
)
1559 cfq_mark_cfqq_idle_window(cfqq
);
1561 cfq_clear_cfqq_idle_window(cfqq
);
1566 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1567 * no or if we aren't sure, a 1 will cause a preempt.
1570 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1573 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
1575 if (cfq_class_idle(new_cfqq
))
1581 if (cfq_class_idle(cfqq
))
1583 if (!cfq_cfqq_wait_request(new_cfqq
))
1586 * if it doesn't have slice left, forget it
1588 if (new_cfqq
->slice_left
< cfqd
->cfq_slice_idle
)
1590 if (cfq_crq_is_sync(crq
) && !cfq_cfqq_sync(cfqq
))
1597 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1598 * let it have half of its nominal slice.
1600 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1602 struct cfq_queue
*__cfqq
, *next
;
1604 list_for_each_entry_safe(__cfqq
, next
, &cfqd
->cur_rr
, cfq_list
)
1605 cfq_resort_rr_list(__cfqq
, 1);
1607 if (!cfqq
->slice_left
)
1608 cfqq
->slice_left
= cfq_prio_to_slice(cfqd
, cfqq
) / 2;
1610 cfqq
->slice_end
= cfqq
->slice_left
+ jiffies
;
1611 __cfq_slice_expired(cfqd
, cfqq
, 1);
1612 __cfq_set_active_queue(cfqd
, cfqq
);
1616 * should really be a ll_rw_blk.c helper
1618 static void cfq_start_queueing(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1620 request_queue_t
*q
= cfqd
->queue
;
1622 if (!blk_queue_plugged(q
))
1625 __generic_unplug_device(q
);
1629 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1630 * something we should do about it
1633 cfq_crq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1636 struct cfq_io_context
*cic
;
1638 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
1641 * we never wait for an async request and we don't allow preemption
1642 * of an async request. so just return early
1644 if (!cfq_crq_is_sync(crq
))
1647 cic
= crq
->io_context
;
1649 cfq_update_io_thinktime(cfqd
, cic
);
1650 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1652 cic
->last_queue
= jiffies
;
1654 if (cfqq
== cfqd
->active_queue
) {
1656 * if we are waiting for a request for this queue, let it rip
1657 * immediately and flag that we must not expire this queue
1660 if (cfq_cfqq_wait_request(cfqq
)) {
1661 cfq_mark_cfqq_must_dispatch(cfqq
);
1662 del_timer(&cfqd
->idle_slice_timer
);
1663 cfq_start_queueing(cfqd
, cfqq
);
1665 } else if (cfq_should_preempt(cfqd
, cfqq
, crq
)) {
1667 * not the active queue - expire current slice if it is
1668 * idle and has expired it's mean thinktime or this new queue
1669 * has some old slice time left and is of higher priority
1671 cfq_preempt_queue(cfqd
, cfqq
);
1672 cfq_mark_cfqq_must_dispatch(cfqq
);
1673 cfq_start_queueing(cfqd
, cfqq
);
1677 static void cfq_insert_request(request_queue_t
*q
, struct request
*rq
)
1679 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1680 struct cfq_rq
*crq
= RQ_DATA(rq
);
1681 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1683 cfq_init_prio_data(cfqq
);
1685 cfq_add_crq_rb(crq
);
1687 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1689 if (rq_mergeable(rq
))
1690 cfq_add_crq_hash(cfqd
, crq
);
1692 cfq_crq_enqueued(cfqd
, cfqq
, crq
);
1695 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1697 struct cfq_rq
*crq
= RQ_DATA(rq
);
1698 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1699 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1700 const int sync
= cfq_crq_is_sync(crq
);
1705 WARN_ON(!cfqd
->rq_in_driver
);
1706 WARN_ON(!cfqq
->on_dispatch
[sync
]);
1707 cfqd
->rq_in_driver
--;
1708 cfqq
->on_dispatch
[sync
]--;
1710 if (!cfq_class_idle(cfqq
))
1711 cfqd
->last_end_request
= now
;
1713 if (!cfq_cfqq_dispatched(cfqq
)) {
1714 if (cfq_cfqq_on_rr(cfqq
)) {
1715 cfqq
->service_last
= now
;
1716 cfq_resort_rr_list(cfqq
, 0);
1718 if (cfq_cfqq_expired(cfqq
)) {
1719 __cfq_slice_expired(cfqd
, cfqq
, 0);
1720 cfq_schedule_dispatch(cfqd
);
1724 if (cfq_crq_is_sync(crq
))
1725 crq
->io_context
->last_end_request
= now
;
1728 static struct request
*
1729 cfq_former_request(request_queue_t
*q
, struct request
*rq
)
1731 struct cfq_rq
*crq
= RQ_DATA(rq
);
1732 struct rb_node
*rbprev
= rb_prev(&crq
->rb_node
);
1735 return rb_entry_crq(rbprev
)->request
;
1740 static struct request
*
1741 cfq_latter_request(request_queue_t
*q
, struct request
*rq
)
1743 struct cfq_rq
*crq
= RQ_DATA(rq
);
1744 struct rb_node
*rbnext
= rb_next(&crq
->rb_node
);
1747 return rb_entry_crq(rbnext
)->request
;
1753 * we temporarily boost lower priority queues if they are holding fs exclusive
1754 * resources. they are boosted to normal prio (CLASS_BE/4)
1756 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1758 const int ioprio_class
= cfqq
->ioprio_class
;
1759 const int ioprio
= cfqq
->ioprio
;
1761 if (has_fs_excl()) {
1763 * boost idle prio on transactions that would lock out other
1764 * users of the filesystem
1766 if (cfq_class_idle(cfqq
))
1767 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1768 if (cfqq
->ioprio
> IOPRIO_NORM
)
1769 cfqq
->ioprio
= IOPRIO_NORM
;
1772 * check if we need to unboost the queue
1774 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1775 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1776 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1777 cfqq
->ioprio
= cfqq
->org_ioprio
;
1781 * refile between round-robin lists if we moved the priority class
1783 if ((ioprio_class
!= cfqq
->ioprio_class
|| ioprio
!= cfqq
->ioprio
) &&
1784 cfq_cfqq_on_rr(cfqq
))
1785 cfq_resort_rr_list(cfqq
, 0);
1788 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
)
1790 if (rw
== READ
|| process_sync(task
))
1793 return CFQ_KEY_ASYNC
;
1797 __cfq_may_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1798 struct task_struct
*task
, int rw
)
1801 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1802 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1803 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1804 return ELV_MQUEUE_MUST
;
1807 return ELV_MQUEUE_MAY
;
1809 if (!cfqq
|| task
->flags
& PF_MEMALLOC
)
1810 return ELV_MQUEUE_MAY
;
1811 if (!cfqq
->allocated
[rw
] || cfq_cfqq_must_alloc(cfqq
)) {
1812 if (cfq_cfqq_wait_request(cfqq
))
1813 return ELV_MQUEUE_MUST
;
1816 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1817 * can quickly flood the queue with writes from a single task
1819 if (rw
== READ
|| !cfq_cfqq_must_alloc_slice(cfqq
)) {
1820 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1821 return ELV_MQUEUE_MUST
;
1824 return ELV_MQUEUE_MAY
;
1826 if (cfq_class_idle(cfqq
))
1827 return ELV_MQUEUE_NO
;
1828 if (cfqq
->allocated
[rw
] >= cfqd
->max_queued
) {
1829 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
);
1830 int ret
= ELV_MQUEUE_NO
;
1832 if (ioc
&& ioc
->nr_batch_requests
)
1833 ret
= ELV_MQUEUE_MAY
;
1835 put_io_context(ioc
);
1839 return ELV_MQUEUE_MAY
;
1843 static int cfq_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1845 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1846 struct task_struct
*tsk
= current
;
1847 struct cfq_queue
*cfqq
;
1850 * don't force setup of a queue from here, as a call to may_queue
1851 * does not necessarily imply that a request actually will be queued.
1852 * so just lookup a possibly existing queue, or return 'may queue'
1855 cfqq
= cfq_find_cfq_hash(cfqd
, cfq_queue_pid(tsk
, rw
), tsk
->ioprio
);
1857 cfq_init_prio_data(cfqq
);
1858 cfq_prio_boost(cfqq
);
1860 return __cfq_may_queue(cfqd
, cfqq
, tsk
, rw
);
1863 return ELV_MQUEUE_MAY
;
1866 static void cfq_check_waiters(request_queue_t
*q
, struct cfq_queue
*cfqq
)
1868 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1869 struct request_list
*rl
= &q
->rq
;
1871 if (cfqq
->allocated
[READ
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1873 if (waitqueue_active(&rl
->wait
[READ
]))
1874 wake_up(&rl
->wait
[READ
]);
1877 if (cfqq
->allocated
[WRITE
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1879 if (waitqueue_active(&rl
->wait
[WRITE
]))
1880 wake_up(&rl
->wait
[WRITE
]);
1885 * queue lock held here
1887 static void cfq_put_request(request_queue_t
*q
, struct request
*rq
)
1889 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1890 struct cfq_rq
*crq
= RQ_DATA(rq
);
1893 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1894 const int rw
= rq_data_dir(rq
);
1896 BUG_ON(!cfqq
->allocated
[rw
]);
1897 cfqq
->allocated
[rw
]--;
1899 put_io_context(crq
->io_context
->ioc
);
1901 mempool_free(crq
, cfqd
->crq_pool
);
1902 rq
->elevator_private
= NULL
;
1904 cfq_check_waiters(q
, cfqq
);
1905 cfq_put_queue(cfqq
);
1910 * Allocate cfq data structures associated with this request.
1913 cfq_set_request(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
,
1916 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1917 struct task_struct
*tsk
= current
;
1918 struct cfq_io_context
*cic
;
1919 const int rw
= rq_data_dir(rq
);
1920 pid_t key
= cfq_queue_pid(tsk
, rw
);
1921 struct cfq_queue
*cfqq
;
1923 unsigned long flags
;
1925 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1927 cic
= cfq_get_io_context(cfqd
, key
, gfp_mask
);
1929 spin_lock_irqsave(q
->queue_lock
, flags
);
1935 cfqq
= cfq_get_queue(cfqd
, key
, tsk
->ioprio
, gfp_mask
);
1943 cfqq
->allocated
[rw
]++;
1944 cfq_clear_cfqq_must_alloc(cfqq
);
1945 cfqd
->rq_starved
= 0;
1946 atomic_inc(&cfqq
->ref
);
1947 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1949 crq
= mempool_alloc(cfqd
->crq_pool
, gfp_mask
);
1951 RB_CLEAR(&crq
->rb_node
);
1954 INIT_HLIST_NODE(&crq
->hash
);
1955 crq
->cfq_queue
= cfqq
;
1956 crq
->io_context
= cic
;
1958 if (rw
== READ
|| process_sync(tsk
))
1959 cfq_mark_crq_is_sync(crq
);
1961 cfq_clear_crq_is_sync(crq
);
1963 rq
->elevator_private
= crq
;
1967 spin_lock_irqsave(q
->queue_lock
, flags
);
1968 cfqq
->allocated
[rw
]--;
1969 if (!(cfqq
->allocated
[0] + cfqq
->allocated
[1]))
1970 cfq_mark_cfqq_must_alloc(cfqq
);
1971 cfq_put_queue(cfqq
);
1974 put_io_context(cic
->ioc
);
1976 * mark us rq allocation starved. we need to kickstart the process
1977 * ourselves if there are no pending requests that can do it for us.
1978 * that would be an extremely rare OOM situation
1980 cfqd
->rq_starved
= 1;
1981 cfq_schedule_dispatch(cfqd
);
1982 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1986 static void cfq_kick_queue(void *data
)
1988 request_queue_t
*q
= data
;
1989 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1990 unsigned long flags
;
1992 spin_lock_irqsave(q
->queue_lock
, flags
);
1994 if (cfqd
->rq_starved
) {
1995 struct request_list
*rl
= &q
->rq
;
1998 * we aren't guaranteed to get a request after this, but we
1999 * have to be opportunistic
2002 if (waitqueue_active(&rl
->wait
[READ
]))
2003 wake_up(&rl
->wait
[READ
]);
2004 if (waitqueue_active(&rl
->wait
[WRITE
]))
2005 wake_up(&rl
->wait
[WRITE
]);
2010 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2014 * Timer running if the active_queue is currently idling inside its time slice
2016 static void cfq_idle_slice_timer(unsigned long data
)
2018 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2019 struct cfq_queue
*cfqq
;
2020 unsigned long flags
;
2022 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2024 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
2025 unsigned long now
= jiffies
;
2030 if (time_after(now
, cfqq
->slice_end
))
2034 * only expire and reinvoke request handler, if there are
2035 * other queues with pending requests
2037 if (!cfqd
->busy_queues
) {
2038 cfqd
->idle_slice_timer
.expires
= min(now
+ cfqd
->cfq_slice_idle
, cfqq
->slice_end
);
2039 add_timer(&cfqd
->idle_slice_timer
);
2044 * not expired and it has a request pending, let it dispatch
2046 if (!RB_EMPTY(&cfqq
->sort_list
)) {
2047 cfq_mark_cfqq_must_dispatch(cfqq
);
2052 cfq_slice_expired(cfqd
, 0);
2054 cfq_schedule_dispatch(cfqd
);
2056 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2060 * Timer running if an idle class queue is waiting for service
2062 static void cfq_idle_class_timer(unsigned long data
)
2064 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2065 unsigned long flags
, end
;
2067 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2070 * race with a non-idle queue, reset timer
2072 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
2073 if (!time_after_eq(jiffies
, end
)) {
2074 cfqd
->idle_class_timer
.expires
= end
;
2075 add_timer(&cfqd
->idle_class_timer
);
2077 cfq_schedule_dispatch(cfqd
);
2079 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2082 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
2084 del_timer_sync(&cfqd
->idle_slice_timer
);
2085 del_timer_sync(&cfqd
->idle_class_timer
);
2086 blk_sync_queue(cfqd
->queue
);
2089 static void cfq_put_cfqd(struct cfq_data
*cfqd
)
2091 request_queue_t
*q
= cfqd
->queue
;
2093 if (!atomic_dec_and_test(&cfqd
->ref
))
2096 cfq_shutdown_timer_wq(cfqd
);
2099 mempool_destroy(cfqd
->crq_pool
);
2100 kfree(cfqd
->crq_hash
);
2101 kfree(cfqd
->cfq_hash
);
2105 static void cfq_exit_queue(elevator_t
*e
)
2107 struct cfq_data
*cfqd
= e
->elevator_data
;
2109 cfq_shutdown_timer_wq(cfqd
);
2113 static int cfq_init_queue(request_queue_t
*q
, elevator_t
*e
)
2115 struct cfq_data
*cfqd
;
2118 cfqd
= kmalloc(sizeof(*cfqd
), GFP_KERNEL
);
2122 memset(cfqd
, 0, sizeof(*cfqd
));
2124 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
2125 INIT_LIST_HEAD(&cfqd
->rr_list
[i
]);
2127 INIT_LIST_HEAD(&cfqd
->busy_rr
);
2128 INIT_LIST_HEAD(&cfqd
->cur_rr
);
2129 INIT_LIST_HEAD(&cfqd
->idle_rr
);
2130 INIT_LIST_HEAD(&cfqd
->empty_list
);
2132 cfqd
->crq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_MHASH_ENTRIES
, GFP_KERNEL
);
2133 if (!cfqd
->crq_hash
)
2136 cfqd
->cfq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
);
2137 if (!cfqd
->cfq_hash
)
2140 cfqd
->crq_pool
= mempool_create(BLKDEV_MIN_RQ
, mempool_alloc_slab
, mempool_free_slab
, crq_pool
);
2141 if (!cfqd
->crq_pool
)
2144 for (i
= 0; i
< CFQ_MHASH_ENTRIES
; i
++)
2145 INIT_HLIST_HEAD(&cfqd
->crq_hash
[i
]);
2146 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
2147 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2149 e
->elevator_data
= cfqd
;
2152 atomic_inc(&q
->refcnt
);
2154 cfqd
->max_queued
= q
->nr_requests
/ 4;
2155 q
->nr_batching
= cfq_queued
;
2157 init_timer(&cfqd
->idle_slice_timer
);
2158 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2159 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2161 init_timer(&cfqd
->idle_class_timer
);
2162 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2163 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2165 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
, q
);
2167 atomic_set(&cfqd
->ref
, 1);
2169 cfqd
->cfq_queued
= cfq_queued
;
2170 cfqd
->cfq_quantum
= cfq_quantum
;
2171 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2172 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2173 cfqd
->cfq_back_max
= cfq_back_max
;
2174 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2175 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2176 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2177 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2178 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2179 cfqd
->cfq_max_depth
= cfq_max_depth
;
2183 kfree(cfqd
->cfq_hash
);
2185 kfree(cfqd
->crq_hash
);
2191 static void cfq_slab_kill(void)
2194 kmem_cache_destroy(crq_pool
);
2196 kmem_cache_destroy(cfq_pool
);
2198 kmem_cache_destroy(cfq_ioc_pool
);
2201 static int __init
cfq_slab_setup(void)
2203 crq_pool
= kmem_cache_create("crq_pool", sizeof(struct cfq_rq
), 0, 0,
2208 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2213 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2214 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2225 * sysfs parts below -->
2227 struct cfq_fs_entry
{
2228 struct attribute attr
;
2229 ssize_t (*show
)(struct cfq_data
*, char *);
2230 ssize_t (*store
)(struct cfq_data
*, const char *, size_t);
2234 cfq_var_show(unsigned int var
, char *page
)
2236 return sprintf(page
, "%d\n", var
);
2240 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2242 char *p
= (char *) page
;
2244 *var
= simple_strtoul(p
, &p
, 10);
2248 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2249 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2251 unsigned int __data = __VAR; \
2253 __data = jiffies_to_msecs(__data); \
2254 return cfq_var_show(__data, (page)); \
2256 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2257 SHOW_FUNCTION(cfq_queued_show
, cfqd
->cfq_queued
, 0);
2258 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2259 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2260 SHOW_FUNCTION(cfq_back_max_show
, cfqd
->cfq_back_max
, 0);
2261 SHOW_FUNCTION(cfq_back_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2262 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2263 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2264 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2265 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2266 SHOW_FUNCTION(cfq_max_depth_show
, cfqd
->cfq_max_depth
, 0);
2267 #undef SHOW_FUNCTION
2269 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2270 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2272 unsigned int __data; \
2273 int ret = cfq_var_store(&__data, (page), count); \
2274 if (__data < (MIN)) \
2276 else if (__data > (MAX)) \
2279 *(__PTR) = msecs_to_jiffies(__data); \
2281 *(__PTR) = __data; \
2284 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2285 STORE_FUNCTION(cfq_queued_store
, &cfqd
->cfq_queued
, 1, UINT_MAX
, 0);
2286 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2287 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2288 STORE_FUNCTION(cfq_back_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2289 STORE_FUNCTION(cfq_back_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2290 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2291 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2292 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2293 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2294 STORE_FUNCTION(cfq_max_depth_store
, &cfqd
->cfq_max_depth
, 1, UINT_MAX
, 0);
2295 #undef STORE_FUNCTION
2297 static struct cfq_fs_entry cfq_quantum_entry
= {
2298 .attr
= {.name
= "quantum", .mode
= S_IRUGO
| S_IWUSR
},
2299 .show
= cfq_quantum_show
,
2300 .store
= cfq_quantum_store
,
2302 static struct cfq_fs_entry cfq_queued_entry
= {
2303 .attr
= {.name
= "queued", .mode
= S_IRUGO
| S_IWUSR
},
2304 .show
= cfq_queued_show
,
2305 .store
= cfq_queued_store
,
2307 static struct cfq_fs_entry cfq_fifo_expire_sync_entry
= {
2308 .attr
= {.name
= "fifo_expire_sync", .mode
= S_IRUGO
| S_IWUSR
},
2309 .show
= cfq_fifo_expire_sync_show
,
2310 .store
= cfq_fifo_expire_sync_store
,
2312 static struct cfq_fs_entry cfq_fifo_expire_async_entry
= {
2313 .attr
= {.name
= "fifo_expire_async", .mode
= S_IRUGO
| S_IWUSR
},
2314 .show
= cfq_fifo_expire_async_show
,
2315 .store
= cfq_fifo_expire_async_store
,
2317 static struct cfq_fs_entry cfq_back_max_entry
= {
2318 .attr
= {.name
= "back_seek_max", .mode
= S_IRUGO
| S_IWUSR
},
2319 .show
= cfq_back_max_show
,
2320 .store
= cfq_back_max_store
,
2322 static struct cfq_fs_entry cfq_back_penalty_entry
= {
2323 .attr
= {.name
= "back_seek_penalty", .mode
= S_IRUGO
| S_IWUSR
},
2324 .show
= cfq_back_penalty_show
,
2325 .store
= cfq_back_penalty_store
,
2327 static struct cfq_fs_entry cfq_slice_sync_entry
= {
2328 .attr
= {.name
= "slice_sync", .mode
= S_IRUGO
| S_IWUSR
},
2329 .show
= cfq_slice_sync_show
,
2330 .store
= cfq_slice_sync_store
,
2332 static struct cfq_fs_entry cfq_slice_async_entry
= {
2333 .attr
= {.name
= "slice_async", .mode
= S_IRUGO
| S_IWUSR
},
2334 .show
= cfq_slice_async_show
,
2335 .store
= cfq_slice_async_store
,
2337 static struct cfq_fs_entry cfq_slice_async_rq_entry
= {
2338 .attr
= {.name
= "slice_async_rq", .mode
= S_IRUGO
| S_IWUSR
},
2339 .show
= cfq_slice_async_rq_show
,
2340 .store
= cfq_slice_async_rq_store
,
2342 static struct cfq_fs_entry cfq_slice_idle_entry
= {
2343 .attr
= {.name
= "slice_idle", .mode
= S_IRUGO
| S_IWUSR
},
2344 .show
= cfq_slice_idle_show
,
2345 .store
= cfq_slice_idle_store
,
2347 static struct cfq_fs_entry cfq_max_depth_entry
= {
2348 .attr
= {.name
= "max_depth", .mode
= S_IRUGO
| S_IWUSR
},
2349 .show
= cfq_max_depth_show
,
2350 .store
= cfq_max_depth_store
,
2353 static struct attribute
*default_attrs
[] = {
2354 &cfq_quantum_entry
.attr
,
2355 &cfq_queued_entry
.attr
,
2356 &cfq_fifo_expire_sync_entry
.attr
,
2357 &cfq_fifo_expire_async_entry
.attr
,
2358 &cfq_back_max_entry
.attr
,
2359 &cfq_back_penalty_entry
.attr
,
2360 &cfq_slice_sync_entry
.attr
,
2361 &cfq_slice_async_entry
.attr
,
2362 &cfq_slice_async_rq_entry
.attr
,
2363 &cfq_slice_idle_entry
.attr
,
2364 &cfq_max_depth_entry
.attr
,
2368 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2371 cfq_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2373 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2374 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2379 return entry
->show(e
->elevator_data
, page
);
2383 cfq_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2384 const char *page
, size_t length
)
2386 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2387 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2392 return entry
->store(e
->elevator_data
, page
, length
);
2395 static struct sysfs_ops cfq_sysfs_ops
= {
2396 .show
= cfq_attr_show
,
2397 .store
= cfq_attr_store
,
2400 static struct kobj_type cfq_ktype
= {
2401 .sysfs_ops
= &cfq_sysfs_ops
,
2402 .default_attrs
= default_attrs
,
2405 static struct elevator_type iosched_cfq
= {
2407 .elevator_merge_fn
= cfq_merge
,
2408 .elevator_merged_fn
= cfq_merged_request
,
2409 .elevator_merge_req_fn
= cfq_merged_requests
,
2410 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2411 .elevator_add_req_fn
= cfq_insert_request
,
2412 .elevator_activate_req_fn
= cfq_activate_request
,
2413 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2414 .elevator_queue_empty_fn
= cfq_queue_empty
,
2415 .elevator_completed_req_fn
= cfq_completed_request
,
2416 .elevator_former_req_fn
= cfq_former_request
,
2417 .elevator_latter_req_fn
= cfq_latter_request
,
2418 .elevator_set_req_fn
= cfq_set_request
,
2419 .elevator_put_req_fn
= cfq_put_request
,
2420 .elevator_may_queue_fn
= cfq_may_queue
,
2421 .elevator_init_fn
= cfq_init_queue
,
2422 .elevator_exit_fn
= cfq_exit_queue
,
2424 .elevator_ktype
= &cfq_ktype
,
2425 .elevator_name
= "cfq",
2426 .elevator_owner
= THIS_MODULE
,
2429 static int __init
cfq_init(void)
2434 * could be 0 on HZ < 1000 setups
2436 if (!cfq_slice_async
)
2437 cfq_slice_async
= 1;
2438 if (!cfq_slice_idle
)
2441 if (cfq_slab_setup())
2444 ret
= elv_register(&iosched_cfq
);
2451 static void __exit
cfq_exit(void)
2453 elv_unregister(&iosched_cfq
);
2457 module_init(cfq_init
);
2458 module_exit(cfq_exit
);
2460 MODULE_AUTHOR("Jens Axboe");
2461 MODULE_LICENSE("GPL");
2462 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");