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 const int cfq_quantum
= 4; /* max queue in one round of service */
29 static const int cfq_queued
= 8; /* minimum rq allocate limit per-queue*/
30 static const int cfq_fifo_expire
[2] = { HZ
/ 4, HZ
/ 8 };
31 static const int cfq_back_max
= 16 * 1024; /* maximum backwards seek, in KiB */
32 static const int cfq_back_penalty
= 2; /* penalty of a backwards seek */
34 static const int cfq_slice_sync
= HZ
/ 10;
35 static int cfq_slice_async
= HZ
/ 25;
36 static const 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 const int cfq_max_depth
= 2;
50 static DEFINE_RWLOCK(cfq_exit_lock
);
53 * for the hash of cfqq inside the cfqd
55 #define CFQ_QHASH_SHIFT 6
56 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
57 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
60 * for the hash of crq inside the cfqq
62 #define CFQ_MHASH_SHIFT 6
63 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
64 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
65 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
66 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
67 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
69 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
70 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
72 #define RQ_DATA(rq) (rq)->elevator_private
78 #define RB_EMPTY(node) ((node)->rb_node == NULL)
79 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
80 #define RB_CLEAR(node) do { \
81 (node)->rb_parent = NULL; \
82 RB_CLEAR_COLOR((node)); \
83 (node)->rb_right = NULL; \
84 (node)->rb_left = NULL; \
86 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
87 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88 #define rq_rb_key(rq) (rq)->sector
90 static kmem_cache_t
*crq_pool
;
91 static kmem_cache_t
*cfq_pool
;
92 static kmem_cache_t
*cfq_ioc_pool
;
94 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
95 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
96 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
102 #define cfq_cfqq_dispatched(cfqq) \
103 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
105 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
107 #define cfq_cfqq_sync(cfqq) \
108 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
111 * Per block device queue structure
115 request_queue_t
*queue
;
118 * rr list of queues with requests and the count of them
120 struct list_head rr_list
[CFQ_PRIO_LISTS
];
121 struct list_head busy_rr
;
122 struct list_head cur_rr
;
123 struct list_head idle_rr
;
124 unsigned int busy_queues
;
127 * non-ordered list of empty cfqq's
129 struct list_head empty_list
;
134 struct hlist_head
*cfq_hash
;
137 * global crq hash for all queues
139 struct hlist_head
*crq_hash
;
141 unsigned int max_queued
;
148 * schedule slice state info
151 * idle window management
153 struct timer_list idle_slice_timer
;
154 struct work_struct unplug_work
;
156 struct cfq_queue
*active_queue
;
157 struct cfq_io_context
*active_cic
;
158 int cur_prio
, cur_end_prio
;
159 unsigned int dispatch_slice
;
161 struct timer_list idle_class_timer
;
163 sector_t last_sector
;
164 unsigned long last_end_request
;
166 unsigned int rq_starved
;
169 * tunables, see top of file
171 unsigned int cfq_quantum
;
172 unsigned int cfq_queued
;
173 unsigned int cfq_fifo_expire
[2];
174 unsigned int cfq_back_penalty
;
175 unsigned int cfq_back_max
;
176 unsigned int cfq_slice
[2];
177 unsigned int cfq_slice_async_rq
;
178 unsigned int cfq_slice_idle
;
179 unsigned int cfq_max_depth
;
181 struct list_head cic_list
;
185 * Per process-grouping structure
188 /* reference count */
190 /* parent cfq_data */
191 struct cfq_data
*cfqd
;
192 /* cfqq lookup hash */
193 struct hlist_node cfq_hash
;
196 /* on either rr or empty list of cfqd */
197 struct list_head cfq_list
;
198 /* sorted list of pending requests */
199 struct rb_root sort_list
;
200 /* if fifo isn't expired, next request to serve */
201 struct cfq_rq
*next_crq
;
202 /* requests queued in sort_list */
204 /* currently allocated requests */
206 /* fifo list of requests in sort_list */
207 struct list_head fifo
;
209 unsigned long slice_start
;
210 unsigned long slice_end
;
211 unsigned long slice_left
;
212 unsigned long service_last
;
214 /* number of requests that are on the dispatch list */
217 /* io prio of this group */
218 unsigned short ioprio
, org_ioprio
;
219 unsigned short ioprio_class
, org_ioprio_class
;
221 /* various state flags, see below */
226 struct rb_node rb_node
;
228 struct request
*request
;
229 struct hlist_node hash
;
231 struct cfq_queue
*cfq_queue
;
232 struct cfq_io_context
*io_context
;
234 unsigned int crq_flags
;
237 enum cfqq_state_flags
{
238 CFQ_CFQQ_FLAG_on_rr
= 0,
239 CFQ_CFQQ_FLAG_wait_request
,
240 CFQ_CFQQ_FLAG_must_alloc
,
241 CFQ_CFQQ_FLAG_must_alloc_slice
,
242 CFQ_CFQQ_FLAG_must_dispatch
,
243 CFQ_CFQQ_FLAG_fifo_expire
,
244 CFQ_CFQQ_FLAG_idle_window
,
245 CFQ_CFQQ_FLAG_prio_changed
,
248 #define CFQ_CFQQ_FNS(name) \
249 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
251 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
253 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
255 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
257 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
259 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
263 CFQ_CFQQ_FNS(wait_request
);
264 CFQ_CFQQ_FNS(must_alloc
);
265 CFQ_CFQQ_FNS(must_alloc_slice
);
266 CFQ_CFQQ_FNS(must_dispatch
);
267 CFQ_CFQQ_FNS(fifo_expire
);
268 CFQ_CFQQ_FNS(idle_window
);
269 CFQ_CFQQ_FNS(prio_changed
);
272 enum cfq_rq_state_flags
{
273 CFQ_CRQ_FLAG_is_sync
= 0,
276 #define CFQ_CRQ_FNS(name) \
277 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
279 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
281 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
283 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
285 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
287 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
290 CFQ_CRQ_FNS(is_sync
);
293 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
294 static void cfq_dispatch_insert(request_queue_t
*, struct cfq_rq
*);
295 static void cfq_put_cfqd(struct cfq_data
*cfqd
);
297 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
300 * lots of deadline iosched dupes, can be abstracted later...
302 static inline void cfq_del_crq_hash(struct cfq_rq
*crq
)
304 hlist_del_init(&crq
->hash
);
307 static inline void cfq_add_crq_hash(struct cfq_data
*cfqd
, struct cfq_rq
*crq
)
309 const int hash_idx
= CFQ_MHASH_FN(rq_hash_key(crq
->request
));
311 hlist_add_head(&crq
->hash
, &cfqd
->crq_hash
[hash_idx
]);
314 static struct request
*cfq_find_rq_hash(struct cfq_data
*cfqd
, sector_t offset
)
316 struct hlist_head
*hash_list
= &cfqd
->crq_hash
[CFQ_MHASH_FN(offset
)];
317 struct hlist_node
*entry
, *next
;
319 hlist_for_each_safe(entry
, next
, hash_list
) {
320 struct cfq_rq
*crq
= list_entry_hash(entry
);
321 struct request
*__rq
= crq
->request
;
323 if (!rq_mergeable(__rq
)) {
324 cfq_del_crq_hash(crq
);
328 if (rq_hash_key(__rq
) == offset
)
336 * scheduler run of queue, if there are requests pending and no one in the
337 * driver that will restart queueing
339 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
341 if (cfqd
->busy_queues
)
342 kblockd_schedule_work(&cfqd
->unplug_work
);
345 static int cfq_queue_empty(request_queue_t
*q
)
347 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
349 return !cfqd
->busy_queues
;
353 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
354 * We choose the request that is closest to the head right now. Distance
355 * behind the head are penalized and only allowed to a certain extent.
357 static struct cfq_rq
*
358 cfq_choose_req(struct cfq_data
*cfqd
, struct cfq_rq
*crq1
, struct cfq_rq
*crq2
)
360 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
361 int r1_wrap
= 0, r2_wrap
= 0; /* requests are behind the disk head */
362 unsigned long back_max
;
364 if (crq1
== NULL
|| crq1
== crq2
)
369 if (cfq_crq_is_sync(crq1
) && !cfq_crq_is_sync(crq2
))
371 else if (cfq_crq_is_sync(crq2
) && !cfq_crq_is_sync(crq1
))
374 s1
= crq1
->request
->sector
;
375 s2
= crq2
->request
->sector
;
377 last
= cfqd
->last_sector
;
380 * by definition, 1KiB is 2 sectors
382 back_max
= cfqd
->cfq_back_max
* 2;
385 * Strict one way elevator _except_ in the case where we allow
386 * short backward seeks which are biased as twice the cost of a
387 * similar forward seek.
391 else if (s1
+ back_max
>= last
)
392 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
398 else if (s2
+ back_max
>= last
)
399 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
403 /* Found required data */
404 if (!r1_wrap
&& r2_wrap
)
406 else if (!r2_wrap
&& r1_wrap
)
408 else if (r1_wrap
&& r2_wrap
) {
409 /* both behind the head */
416 /* Both requests in front of the head */
430 * would be nice to take fifo expire time into account as well
432 static struct cfq_rq
*
433 cfq_find_next_crq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
436 struct cfq_rq
*crq_next
= NULL
, *crq_prev
= NULL
;
437 struct rb_node
*rbnext
, *rbprev
;
439 if (!(rbnext
= rb_next(&last
->rb_node
))) {
440 rbnext
= rb_first(&cfqq
->sort_list
);
441 if (rbnext
== &last
->rb_node
)
445 rbprev
= rb_prev(&last
->rb_node
);
448 crq_prev
= rb_entry_crq(rbprev
);
450 crq_next
= rb_entry_crq(rbnext
);
452 return cfq_choose_req(cfqd
, crq_next
, crq_prev
);
455 static void cfq_update_next_crq(struct cfq_rq
*crq
)
457 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
459 if (cfqq
->next_crq
== crq
)
460 cfqq
->next_crq
= cfq_find_next_crq(cfqq
->cfqd
, cfqq
, crq
);
463 static void cfq_resort_rr_list(struct cfq_queue
*cfqq
, int preempted
)
465 struct cfq_data
*cfqd
= cfqq
->cfqd
;
466 struct list_head
*list
, *entry
;
468 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
470 list_del(&cfqq
->cfq_list
);
472 if (cfq_class_rt(cfqq
))
473 list
= &cfqd
->cur_rr
;
474 else if (cfq_class_idle(cfqq
))
475 list
= &cfqd
->idle_rr
;
478 * if cfqq has requests in flight, don't allow it to be
479 * found in cfq_set_active_queue before it has finished them.
480 * this is done to increase fairness between a process that
481 * has lots of io pending vs one that only generates one
482 * sporadically or synchronously
484 if (cfq_cfqq_dispatched(cfqq
))
485 list
= &cfqd
->busy_rr
;
487 list
= &cfqd
->rr_list
[cfqq
->ioprio
];
491 * if queue was preempted, just add to front to be fair. busy_rr
494 if (preempted
|| list
== &cfqd
->busy_rr
) {
495 list_add(&cfqq
->cfq_list
, list
);
500 * sort by when queue was last serviced
503 while ((entry
= entry
->prev
) != list
) {
504 struct cfq_queue
*__cfqq
= list_entry_cfqq(entry
);
506 if (!__cfqq
->service_last
)
508 if (time_before(__cfqq
->service_last
, cfqq
->service_last
))
512 list_add(&cfqq
->cfq_list
, entry
);
516 * add to busy list of queues for service, trying to be fair in ordering
517 * the pending list according to last request service
520 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
522 BUG_ON(cfq_cfqq_on_rr(cfqq
));
523 cfq_mark_cfqq_on_rr(cfqq
);
526 cfq_resort_rr_list(cfqq
, 0);
530 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
532 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
533 cfq_clear_cfqq_on_rr(cfqq
);
534 list_move(&cfqq
->cfq_list
, &cfqd
->empty_list
);
536 BUG_ON(!cfqd
->busy_queues
);
541 * rb tree support functions
543 static inline void cfq_del_crq_rb(struct cfq_rq
*crq
)
545 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
546 struct cfq_data
*cfqd
= cfqq
->cfqd
;
547 const int sync
= cfq_crq_is_sync(crq
);
549 BUG_ON(!cfqq
->queued
[sync
]);
550 cfqq
->queued
[sync
]--;
552 cfq_update_next_crq(crq
);
554 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
555 RB_CLEAR_COLOR(&crq
->rb_node
);
557 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY(&cfqq
->sort_list
))
558 cfq_del_cfqq_rr(cfqd
, cfqq
);
561 static struct cfq_rq
*
562 __cfq_add_crq_rb(struct cfq_rq
*crq
)
564 struct rb_node
**p
= &crq
->cfq_queue
->sort_list
.rb_node
;
565 struct rb_node
*parent
= NULL
;
566 struct cfq_rq
*__crq
;
570 __crq
= rb_entry_crq(parent
);
572 if (crq
->rb_key
< __crq
->rb_key
)
574 else if (crq
->rb_key
> __crq
->rb_key
)
580 rb_link_node(&crq
->rb_node
, parent
, p
);
584 static void cfq_add_crq_rb(struct cfq_rq
*crq
)
586 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
587 struct cfq_data
*cfqd
= cfqq
->cfqd
;
588 struct request
*rq
= crq
->request
;
589 struct cfq_rq
*__alias
;
591 crq
->rb_key
= rq_rb_key(rq
);
592 cfqq
->queued
[cfq_crq_is_sync(crq
)]++;
595 * looks a little odd, but the first insert might return an alias.
596 * if that happens, put the alias on the dispatch list
598 while ((__alias
= __cfq_add_crq_rb(crq
)) != NULL
)
599 cfq_dispatch_insert(cfqd
->queue
, __alias
);
601 rb_insert_color(&crq
->rb_node
, &cfqq
->sort_list
);
603 if (!cfq_cfqq_on_rr(cfqq
))
604 cfq_add_cfqq_rr(cfqd
, cfqq
);
607 * check if this request is a better next-serve candidate
609 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
613 cfq_reposition_crq_rb(struct cfq_queue
*cfqq
, struct cfq_rq
*crq
)
615 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
616 cfqq
->queued
[cfq_crq_is_sync(crq
)]--;
621 static struct request
*cfq_find_rq_rb(struct cfq_data
*cfqd
, sector_t sector
)
624 struct cfq_queue
*cfqq
= cfq_find_cfq_hash(cfqd
, current
->pid
, CFQ_KEY_ANY
);
630 n
= cfqq
->sort_list
.rb_node
;
632 struct cfq_rq
*crq
= rb_entry_crq(n
);
634 if (sector
< crq
->rb_key
)
636 else if (sector
> crq
->rb_key
)
646 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
648 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
650 cfqd
->rq_in_driver
++;
653 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
655 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
657 WARN_ON(!cfqd
->rq_in_driver
);
658 cfqd
->rq_in_driver
--;
661 static void cfq_remove_request(struct request
*rq
)
663 struct cfq_rq
*crq
= RQ_DATA(rq
);
665 list_del_init(&rq
->queuelist
);
667 cfq_del_crq_hash(crq
);
671 cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
673 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
674 struct request
*__rq
;
677 __rq
= cfq_find_rq_hash(cfqd
, bio
->bi_sector
);
678 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
679 ret
= ELEVATOR_BACK_MERGE
;
683 __rq
= cfq_find_rq_rb(cfqd
, bio
->bi_sector
+ bio_sectors(bio
));
684 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
685 ret
= ELEVATOR_FRONT_MERGE
;
689 return ELEVATOR_NO_MERGE
;
695 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
)
697 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
698 struct cfq_rq
*crq
= RQ_DATA(req
);
700 cfq_del_crq_hash(crq
);
701 cfq_add_crq_hash(cfqd
, crq
);
703 if (rq_rb_key(req
) != crq
->rb_key
) {
704 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
706 cfq_update_next_crq(crq
);
707 cfq_reposition_crq_rb(cfqq
, crq
);
712 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
713 struct request
*next
)
715 cfq_merged_request(q
, rq
);
718 * reposition in fifo if next is older than rq
720 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
721 time_before(next
->start_time
, rq
->start_time
))
722 list_move(&rq
->queuelist
, &next
->queuelist
);
724 cfq_remove_request(next
);
728 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
732 * stop potential idle class queues waiting service
734 del_timer(&cfqd
->idle_class_timer
);
736 cfqq
->slice_start
= jiffies
;
738 cfqq
->slice_left
= 0;
739 cfq_clear_cfqq_must_alloc_slice(cfqq
);
740 cfq_clear_cfqq_fifo_expire(cfqq
);
743 cfqd
->active_queue
= cfqq
;
747 * current cfqq expired its slice (or was too idle), select new one
750 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
753 unsigned long now
= jiffies
;
755 if (cfq_cfqq_wait_request(cfqq
))
756 del_timer(&cfqd
->idle_slice_timer
);
758 if (!preempted
&& !cfq_cfqq_dispatched(cfqq
)) {
759 cfqq
->service_last
= now
;
760 cfq_schedule_dispatch(cfqd
);
763 cfq_clear_cfqq_must_dispatch(cfqq
);
764 cfq_clear_cfqq_wait_request(cfqq
);
767 * store what was left of this slice, if the queue idled out
770 if (time_after(cfqq
->slice_end
, now
))
771 cfqq
->slice_left
= cfqq
->slice_end
- now
;
773 cfqq
->slice_left
= 0;
775 if (cfq_cfqq_on_rr(cfqq
))
776 cfq_resort_rr_list(cfqq
, preempted
);
778 if (cfqq
== cfqd
->active_queue
)
779 cfqd
->active_queue
= NULL
;
781 if (cfqd
->active_cic
) {
782 put_io_context(cfqd
->active_cic
->ioc
);
783 cfqd
->active_cic
= NULL
;
786 cfqd
->dispatch_slice
= 0;
789 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int preempted
)
791 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
794 __cfq_slice_expired(cfqd
, cfqq
, preempted
);
807 static int cfq_get_next_prio_level(struct cfq_data
*cfqd
)
816 for (p
= cfqd
->cur_prio
; p
<= cfqd
->cur_end_prio
; p
++) {
817 if (!list_empty(&cfqd
->rr_list
[p
])) {
826 if (++cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
827 cfqd
->cur_end_prio
= 0;
834 if (unlikely(prio
== -1))
837 BUG_ON(prio
>= CFQ_PRIO_LISTS
);
839 list_splice_init(&cfqd
->rr_list
[prio
], &cfqd
->cur_rr
);
841 cfqd
->cur_prio
= prio
+ 1;
842 if (cfqd
->cur_prio
> cfqd
->cur_end_prio
) {
843 cfqd
->cur_end_prio
= cfqd
->cur_prio
;
846 if (cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
848 cfqd
->cur_end_prio
= 0;
854 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
856 struct cfq_queue
*cfqq
= NULL
;
859 * if current list is non-empty, grab first entry. if it is empty,
860 * get next prio level and grab first entry then if any are spliced
862 if (!list_empty(&cfqd
->cur_rr
) || cfq_get_next_prio_level(cfqd
) != -1)
863 cfqq
= list_entry_cfqq(cfqd
->cur_rr
.next
);
866 * if we have idle queues and no rt or be queues had pending
867 * requests, either allow immediate service if the grace period
868 * has passed or arm the idle grace timer
870 if (!cfqq
&& !list_empty(&cfqd
->idle_rr
)) {
871 unsigned long end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
873 if (time_after_eq(jiffies
, end
))
874 cfqq
= list_entry_cfqq(cfqd
->idle_rr
.next
);
876 mod_timer(&cfqd
->idle_class_timer
, end
);
879 __cfq_set_active_queue(cfqd
, cfqq
);
883 static int cfq_arm_slice_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
888 WARN_ON(!RB_EMPTY(&cfqq
->sort_list
));
889 WARN_ON(cfqq
!= cfqd
->active_queue
);
892 * idle is disabled, either manually or by past process history
894 if (!cfqd
->cfq_slice_idle
)
896 if (!cfq_cfqq_idle_window(cfqq
))
899 * task has exited, don't wait
901 if (cfqd
->active_cic
&& !cfqd
->active_cic
->ioc
->task
)
904 cfq_mark_cfqq_must_dispatch(cfqq
);
905 cfq_mark_cfqq_wait_request(cfqq
);
907 sl
= min(cfqq
->slice_end
- 1, (unsigned long) cfqd
->cfq_slice_idle
);
908 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
912 static void cfq_dispatch_insert(request_queue_t
*q
, struct cfq_rq
*crq
)
914 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
915 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
917 cfqq
->next_crq
= cfq_find_next_crq(cfqd
, cfqq
, crq
);
918 cfq_remove_request(crq
->request
);
919 cfqq
->on_dispatch
[cfq_crq_is_sync(crq
)]++;
920 elv_dispatch_sort(q
, crq
->request
);
924 * return expired entry, or NULL to just start from scratch in rbtree
926 static inline struct cfq_rq
*cfq_check_fifo(struct cfq_queue
*cfqq
)
928 struct cfq_data
*cfqd
= cfqq
->cfqd
;
932 if (cfq_cfqq_fifo_expire(cfqq
))
935 if (!list_empty(&cfqq
->fifo
)) {
936 int fifo
= cfq_cfqq_class_sync(cfqq
);
938 crq
= RQ_DATA(list_entry_fifo(cfqq
->fifo
.next
));
940 if (time_after(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
])) {
941 cfq_mark_cfqq_fifo_expire(cfqq
);
950 * Scale schedule slice based on io priority. Use the sync time slice only
951 * if a queue is marked sync and has sync io queued. A sync queue with async
952 * io only, should not get full sync slice length.
955 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
957 const int base_slice
= cfqd
->cfq_slice
[cfq_cfqq_sync(cfqq
)];
959 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
961 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - cfqq
->ioprio
));
965 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
967 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
971 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
973 const int base_rq
= cfqd
->cfq_slice_async_rq
;
975 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
977 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
981 * get next queue for service
983 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
985 unsigned long now
= jiffies
;
986 struct cfq_queue
*cfqq
;
988 cfqq
= cfqd
->active_queue
;
995 if (!cfq_cfqq_must_dispatch(cfqq
) && time_after(now
, cfqq
->slice_end
))
999 * if queue has requests, dispatch one. if not, check if
1000 * enough slice is left to wait for one
1002 if (!RB_EMPTY(&cfqq
->sort_list
))
1004 else if (cfq_cfqq_class_sync(cfqq
) &&
1005 time_before(now
, cfqq
->slice_end
)) {
1006 if (cfq_arm_slice_timer(cfqd
, cfqq
))
1011 cfq_slice_expired(cfqd
, 0);
1013 cfqq
= cfq_set_active_queue(cfqd
);
1019 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1024 BUG_ON(RB_EMPTY(&cfqq
->sort_list
));
1030 * follow expired path, else get first next available
1032 if ((crq
= cfq_check_fifo(cfqq
)) == NULL
)
1033 crq
= cfqq
->next_crq
;
1036 * finally, insert request into driver dispatch list
1038 cfq_dispatch_insert(cfqd
->queue
, crq
);
1040 cfqd
->dispatch_slice
++;
1043 if (!cfqd
->active_cic
) {
1044 atomic_inc(&crq
->io_context
->ioc
->refcount
);
1045 cfqd
->active_cic
= crq
->io_context
;
1048 if (RB_EMPTY(&cfqq
->sort_list
))
1051 } while (dispatched
< max_dispatch
);
1054 * if slice end isn't set yet, set it. if at least one request was
1055 * sync, use the sync time slice value
1057 if (!cfqq
->slice_end
)
1058 cfq_set_prio_slice(cfqd
, cfqq
);
1061 * expire an async queue immediately if it has used up its slice. idle
1062 * queue always expire after 1 dispatch round.
1064 if ((!cfq_cfqq_sync(cfqq
) &&
1065 cfqd
->dispatch_slice
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
1066 cfq_class_idle(cfqq
))
1067 cfq_slice_expired(cfqd
, 0);
1073 cfq_forced_dispatch_cfqqs(struct list_head
*list
)
1076 struct cfq_queue
*cfqq
, *next
;
1079 list_for_each_entry_safe(cfqq
, next
, list
, cfq_list
) {
1080 while ((crq
= cfqq
->next_crq
)) {
1081 cfq_dispatch_insert(cfqq
->cfqd
->queue
, crq
);
1084 BUG_ON(!list_empty(&cfqq
->fifo
));
1090 cfq_forced_dispatch(struct cfq_data
*cfqd
)
1092 int i
, dispatched
= 0;
1094 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
1095 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->rr_list
[i
]);
1097 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->busy_rr
);
1098 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->cur_rr
);
1099 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->idle_rr
);
1101 cfq_slice_expired(cfqd
, 0);
1103 BUG_ON(cfqd
->busy_queues
);
1109 cfq_dispatch_requests(request_queue_t
*q
, int force
)
1111 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1112 struct cfq_queue
*cfqq
;
1114 if (!cfqd
->busy_queues
)
1117 if (unlikely(force
))
1118 return cfq_forced_dispatch(cfqd
);
1120 cfqq
= cfq_select_queue(cfqd
);
1125 * if idle window is disabled, allow queue buildup
1127 if (!cfq_cfqq_idle_window(cfqq
) &&
1128 cfqd
->rq_in_driver
>= cfqd
->cfq_max_depth
)
1131 cfq_clear_cfqq_must_dispatch(cfqq
);
1132 cfq_clear_cfqq_wait_request(cfqq
);
1133 del_timer(&cfqd
->idle_slice_timer
);
1135 max_dispatch
= cfqd
->cfq_quantum
;
1136 if (cfq_class_idle(cfqq
))
1139 return __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1146 * task holds one reference to the queue, dropped when task exits. each crq
1147 * in-flight on this queue also holds a reference, dropped when crq is freed.
1149 * queue lock must be held here.
1151 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1153 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1155 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1157 if (!atomic_dec_and_test(&cfqq
->ref
))
1160 BUG_ON(rb_first(&cfqq
->sort_list
));
1161 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1162 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1164 if (unlikely(cfqd
->active_queue
== cfqq
))
1165 __cfq_slice_expired(cfqd
, cfqq
, 0);
1167 cfq_put_cfqd(cfqq
->cfqd
);
1170 * it's on the empty list and still hashed
1172 list_del(&cfqq
->cfq_list
);
1173 hlist_del(&cfqq
->cfq_hash
);
1174 kmem_cache_free(cfq_pool
, cfqq
);
1177 static inline struct cfq_queue
*
1178 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1181 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1182 struct hlist_node
*entry
, *next
;
1184 hlist_for_each_safe(entry
, next
, hash_list
) {
1185 struct cfq_queue
*__cfqq
= list_entry_qhash(entry
);
1186 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->org_ioprio_class
, __cfqq
->org_ioprio
);
1188 if (__cfqq
->key
== key
&& (__p
== prio
|| prio
== CFQ_KEY_ANY
))
1195 static struct cfq_queue
*
1196 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1198 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1201 static void cfq_free_io_context(struct cfq_io_context
*cic
)
1203 struct cfq_io_context
*__cic
;
1204 struct list_head
*entry
, *next
;
1206 list_for_each_safe(entry
, next
, &cic
->list
) {
1207 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1208 kmem_cache_free(cfq_ioc_pool
, __cic
);
1211 kmem_cache_free(cfq_ioc_pool
, cic
);
1215 * Called with interrupts disabled
1217 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1219 struct cfq_data
*cfqd
= cic
->key
;
1227 WARN_ON(!irqs_disabled());
1229 spin_lock(q
->queue_lock
);
1231 if (cic
->cfqq
[ASYNC
]) {
1232 if (unlikely(cic
->cfqq
[ASYNC
] == cfqd
->active_queue
))
1233 __cfq_slice_expired(cfqd
, cic
->cfqq
[ASYNC
], 0);
1234 cfq_put_queue(cic
->cfqq
[ASYNC
]);
1235 cic
->cfqq
[ASYNC
] = NULL
;
1238 if (cic
->cfqq
[SYNC
]) {
1239 if (unlikely(cic
->cfqq
[SYNC
] == cfqd
->active_queue
))
1240 __cfq_slice_expired(cfqd
, cic
->cfqq
[SYNC
], 0);
1241 cfq_put_queue(cic
->cfqq
[SYNC
]);
1242 cic
->cfqq
[SYNC
] = NULL
;
1246 list_del_init(&cic
->queue_list
);
1247 spin_unlock(q
->queue_lock
);
1251 * Another task may update the task cic list, if it is doing a queue lookup
1252 * on its behalf. cfq_cic_lock excludes such concurrent updates
1254 static void cfq_exit_io_context(struct cfq_io_context
*cic
)
1256 struct cfq_io_context
*__cic
;
1257 struct list_head
*entry
;
1258 unsigned long flags
;
1260 local_irq_save(flags
);
1263 * put the reference this task is holding to the various queues
1265 read_lock(&cfq_exit_lock
);
1266 list_for_each(entry
, &cic
->list
) {
1267 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1268 cfq_exit_single_io_context(__cic
);
1271 cfq_exit_single_io_context(cic
);
1272 read_unlock(&cfq_exit_lock
);
1273 local_irq_restore(flags
);
1276 static struct cfq_io_context
*
1277 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1279 struct cfq_io_context
*cic
= kmem_cache_alloc(cfq_ioc_pool
, gfp_mask
);
1282 INIT_LIST_HEAD(&cic
->list
);
1283 cic
->cfqq
[ASYNC
] = NULL
;
1284 cic
->cfqq
[SYNC
] = NULL
;
1286 cic
->last_end_request
= jiffies
;
1287 cic
->ttime_total
= 0;
1288 cic
->ttime_samples
= 0;
1289 cic
->ttime_mean
= 0;
1290 cic
->dtor
= cfq_free_io_context
;
1291 cic
->exit
= cfq_exit_io_context
;
1292 INIT_LIST_HEAD(&cic
->queue_list
);
1298 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1300 struct task_struct
*tsk
= current
;
1303 if (!cfq_cfqq_prio_changed(cfqq
))
1306 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1307 switch (ioprio_class
) {
1309 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1310 case IOPRIO_CLASS_NONE
:
1312 * no prio set, place us in the middle of the BE classes
1314 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1315 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1317 case IOPRIO_CLASS_RT
:
1318 cfqq
->ioprio
= task_ioprio(tsk
);
1319 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1321 case IOPRIO_CLASS_BE
:
1322 cfqq
->ioprio
= task_ioprio(tsk
);
1323 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1325 case IOPRIO_CLASS_IDLE
:
1326 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1328 cfq_clear_cfqq_idle_window(cfqq
);
1333 * keep track of original prio settings in case we have to temporarily
1334 * elevate the priority of this queue
1336 cfqq
->org_ioprio
= cfqq
->ioprio
;
1337 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1339 if (cfq_cfqq_on_rr(cfqq
))
1340 cfq_resort_rr_list(cfqq
, 0);
1342 cfq_clear_cfqq_prio_changed(cfqq
);
1345 static inline void changed_ioprio(struct cfq_io_context
*cic
)
1347 struct cfq_data
*cfqd
= cic
->key
;
1348 struct cfq_queue
*cfqq
;
1350 spin_lock(cfqd
->queue
->queue_lock
);
1351 cfqq
= cic
->cfqq
[ASYNC
];
1353 cfq_mark_cfqq_prio_changed(cfqq
);
1354 cfq_init_prio_data(cfqq
);
1356 cfqq
= cic
->cfqq
[SYNC
];
1358 cfq_mark_cfqq_prio_changed(cfqq
);
1359 cfq_init_prio_data(cfqq
);
1361 spin_unlock(cfqd
->queue
->queue_lock
);
1366 * callback from sys_ioprio_set, irqs are disabled
1368 static int cfq_ioc_set_ioprio(struct io_context
*ioc
, unsigned int ioprio
)
1370 struct cfq_io_context
*cic
;
1372 write_lock(&cfq_exit_lock
);
1376 changed_ioprio(cic
);
1378 list_for_each_entry(cic
, &cic
->list
, list
)
1379 changed_ioprio(cic
);
1381 write_unlock(&cfq_exit_lock
);
1386 static struct cfq_queue
*
1387 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, unsigned short ioprio
,
1390 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1391 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1394 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1400 } else if (gfp_mask
& __GFP_WAIT
) {
1401 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1402 new_cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1403 spin_lock_irq(cfqd
->queue
->queue_lock
);
1406 cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1411 memset(cfqq
, 0, sizeof(*cfqq
));
1413 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1414 INIT_LIST_HEAD(&cfqq
->cfq_list
);
1415 RB_CLEAR_ROOT(&cfqq
->sort_list
);
1416 INIT_LIST_HEAD(&cfqq
->fifo
);
1419 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1420 atomic_set(&cfqq
->ref
, 0);
1422 atomic_inc(&cfqd
->ref
);
1423 cfqq
->service_last
= 0;
1425 * set ->slice_left to allow preemption for a new process
1427 cfqq
->slice_left
= 2 * cfqd
->cfq_slice_idle
;
1428 cfq_mark_cfqq_idle_window(cfqq
);
1429 cfq_mark_cfqq_prio_changed(cfqq
);
1430 cfq_init_prio_data(cfqq
);
1434 kmem_cache_free(cfq_pool
, new_cfqq
);
1436 atomic_inc(&cfqq
->ref
);
1438 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1443 * Setup general io context and cfq io context. There can be several cfq
1444 * io contexts per general io context, if this process is doing io to more
1445 * than one device managed by cfq. Note that caller is holding a reference to
1446 * cfqq, so we don't need to worry about it disappearing
1448 static struct cfq_io_context
*
1449 cfq_get_io_context(struct cfq_data
*cfqd
, pid_t pid
, gfp_t gfp_mask
)
1451 struct io_context
*ioc
= NULL
;
1452 struct cfq_io_context
*cic
;
1454 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1456 ioc
= get_io_context(gfp_mask
);
1461 if ((cic
= ioc
->cic
) == NULL
) {
1462 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1468 * manually increment generic io_context usage count, it
1469 * cannot go away since we are already holding one ref to it
1473 read_lock(&cfq_exit_lock
);
1474 ioc
->set_ioprio
= cfq_ioc_set_ioprio
;
1476 list_add(&cic
->queue_list
, &cfqd
->cic_list
);
1477 read_unlock(&cfq_exit_lock
);
1479 struct cfq_io_context
*__cic
;
1482 * the first cic on the list is actually the head itself
1484 if (cic
->key
== cfqd
)
1487 if (unlikely(!cic
->key
)) {
1488 read_lock(&cfq_exit_lock
);
1489 if (list_empty(&cic
->list
))
1492 ioc
->cic
= list_entry(cic
->list
.next
,
1493 struct cfq_io_context
,
1495 read_unlock(&cfq_exit_lock
);
1496 kmem_cache_free(cfq_ioc_pool
, cic
);
1501 * cic exists, check if we already are there. linear search
1502 * should be ok here, the list will usually not be more than
1503 * 1 or a few entries long
1505 list_for_each_entry(__cic
, &cic
->list
, list
) {
1507 * this process is already holding a reference to
1508 * this queue, so no need to get one more
1510 if (__cic
->key
== cfqd
) {
1514 if (unlikely(!__cic
->key
)) {
1515 read_lock(&cfq_exit_lock
);
1516 list_del(&__cic
->list
);
1517 read_unlock(&cfq_exit_lock
);
1518 kmem_cache_free(cfq_ioc_pool
, __cic
);
1524 * nope, process doesn't have a cic assoicated with this
1525 * cfqq yet. get a new one and add to list
1527 __cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1533 read_lock(&cfq_exit_lock
);
1534 list_add(&__cic
->list
, &cic
->list
);
1535 list_add(&__cic
->queue_list
, &cfqd
->cic_list
);
1536 read_unlock(&cfq_exit_lock
);
1543 put_io_context(ioc
);
1548 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1550 unsigned long elapsed
, ttime
;
1553 * if this context already has stuff queued, thinktime is from
1554 * last queue not last end
1557 if (time_after(cic
->last_end_request
, cic
->last_queue
))
1558 elapsed
= jiffies
- cic
->last_end_request
;
1560 elapsed
= jiffies
- cic
->last_queue
;
1562 elapsed
= jiffies
- cic
->last_end_request
;
1565 ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1567 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1568 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1569 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1572 #define sample_valid(samples) ((samples) > 80)
1575 * Disable idle window if the process thinks too long or seeks so much that
1579 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1580 struct cfq_io_context
*cic
)
1582 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1584 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
)
1586 else if (sample_valid(cic
->ttime_samples
)) {
1587 if (cic
->ttime_mean
> cfqd
->cfq_slice_idle
)
1594 cfq_mark_cfqq_idle_window(cfqq
);
1596 cfq_clear_cfqq_idle_window(cfqq
);
1601 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1602 * no or if we aren't sure, a 1 will cause a preempt.
1605 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1608 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
1610 if (cfq_class_idle(new_cfqq
))
1616 if (cfq_class_idle(cfqq
))
1618 if (!cfq_cfqq_wait_request(new_cfqq
))
1621 * if it doesn't have slice left, forget it
1623 if (new_cfqq
->slice_left
< cfqd
->cfq_slice_idle
)
1625 if (cfq_crq_is_sync(crq
) && !cfq_cfqq_sync(cfqq
))
1632 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1633 * let it have half of its nominal slice.
1635 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1637 struct cfq_queue
*__cfqq
, *next
;
1639 list_for_each_entry_safe(__cfqq
, next
, &cfqd
->cur_rr
, cfq_list
)
1640 cfq_resort_rr_list(__cfqq
, 1);
1642 if (!cfqq
->slice_left
)
1643 cfqq
->slice_left
= cfq_prio_to_slice(cfqd
, cfqq
) / 2;
1645 cfqq
->slice_end
= cfqq
->slice_left
+ jiffies
;
1646 __cfq_slice_expired(cfqd
, cfqq
, 1);
1647 __cfq_set_active_queue(cfqd
, cfqq
);
1651 * should really be a ll_rw_blk.c helper
1653 static void cfq_start_queueing(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1655 request_queue_t
*q
= cfqd
->queue
;
1657 if (!blk_queue_plugged(q
))
1660 __generic_unplug_device(q
);
1664 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1665 * something we should do about it
1668 cfq_crq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1671 struct cfq_io_context
*cic
;
1673 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
1676 * we never wait for an async request and we don't allow preemption
1677 * of an async request. so just return early
1679 if (!cfq_crq_is_sync(crq
))
1682 cic
= crq
->io_context
;
1684 cfq_update_io_thinktime(cfqd
, cic
);
1685 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1687 cic
->last_queue
= jiffies
;
1689 if (cfqq
== cfqd
->active_queue
) {
1691 * if we are waiting for a request for this queue, let it rip
1692 * immediately and flag that we must not expire this queue
1695 if (cfq_cfqq_wait_request(cfqq
)) {
1696 cfq_mark_cfqq_must_dispatch(cfqq
);
1697 del_timer(&cfqd
->idle_slice_timer
);
1698 cfq_start_queueing(cfqd
, cfqq
);
1700 } else if (cfq_should_preempt(cfqd
, cfqq
, crq
)) {
1702 * not the active queue - expire current slice if it is
1703 * idle and has expired it's mean thinktime or this new queue
1704 * has some old slice time left and is of higher priority
1706 cfq_preempt_queue(cfqd
, cfqq
);
1707 cfq_mark_cfqq_must_dispatch(cfqq
);
1708 cfq_start_queueing(cfqd
, cfqq
);
1712 static void cfq_insert_request(request_queue_t
*q
, struct request
*rq
)
1714 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1715 struct cfq_rq
*crq
= RQ_DATA(rq
);
1716 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1718 cfq_init_prio_data(cfqq
);
1720 cfq_add_crq_rb(crq
);
1722 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1724 if (rq_mergeable(rq
))
1725 cfq_add_crq_hash(cfqd
, crq
);
1727 cfq_crq_enqueued(cfqd
, cfqq
, crq
);
1730 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1732 struct cfq_rq
*crq
= RQ_DATA(rq
);
1733 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1734 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1735 const int sync
= cfq_crq_is_sync(crq
);
1740 WARN_ON(!cfqd
->rq_in_driver
);
1741 WARN_ON(!cfqq
->on_dispatch
[sync
]);
1742 cfqd
->rq_in_driver
--;
1743 cfqq
->on_dispatch
[sync
]--;
1745 if (!cfq_class_idle(cfqq
))
1746 cfqd
->last_end_request
= now
;
1748 if (!cfq_cfqq_dispatched(cfqq
)) {
1749 if (cfq_cfqq_on_rr(cfqq
)) {
1750 cfqq
->service_last
= now
;
1751 cfq_resort_rr_list(cfqq
, 0);
1753 cfq_schedule_dispatch(cfqd
);
1756 if (cfq_crq_is_sync(crq
))
1757 crq
->io_context
->last_end_request
= now
;
1760 static struct request
*
1761 cfq_former_request(request_queue_t
*q
, struct request
*rq
)
1763 struct cfq_rq
*crq
= RQ_DATA(rq
);
1764 struct rb_node
*rbprev
= rb_prev(&crq
->rb_node
);
1767 return rb_entry_crq(rbprev
)->request
;
1772 static struct request
*
1773 cfq_latter_request(request_queue_t
*q
, struct request
*rq
)
1775 struct cfq_rq
*crq
= RQ_DATA(rq
);
1776 struct rb_node
*rbnext
= rb_next(&crq
->rb_node
);
1779 return rb_entry_crq(rbnext
)->request
;
1785 * we temporarily boost lower priority queues if they are holding fs exclusive
1786 * resources. they are boosted to normal prio (CLASS_BE/4)
1788 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1790 const int ioprio_class
= cfqq
->ioprio_class
;
1791 const int ioprio
= cfqq
->ioprio
;
1793 if (has_fs_excl()) {
1795 * boost idle prio on transactions that would lock out other
1796 * users of the filesystem
1798 if (cfq_class_idle(cfqq
))
1799 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1800 if (cfqq
->ioprio
> IOPRIO_NORM
)
1801 cfqq
->ioprio
= IOPRIO_NORM
;
1804 * check if we need to unboost the queue
1806 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1807 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1808 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1809 cfqq
->ioprio
= cfqq
->org_ioprio
;
1813 * refile between round-robin lists if we moved the priority class
1815 if ((ioprio_class
!= cfqq
->ioprio_class
|| ioprio
!= cfqq
->ioprio
) &&
1816 cfq_cfqq_on_rr(cfqq
))
1817 cfq_resort_rr_list(cfqq
, 0);
1820 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
)
1822 if (rw
== READ
|| process_sync(task
))
1825 return CFQ_KEY_ASYNC
;
1829 __cfq_may_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1830 struct task_struct
*task
, int rw
)
1833 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1834 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1835 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1836 return ELV_MQUEUE_MUST
;
1839 return ELV_MQUEUE_MAY
;
1841 if (!cfqq
|| task
->flags
& PF_MEMALLOC
)
1842 return ELV_MQUEUE_MAY
;
1843 if (!cfqq
->allocated
[rw
] || cfq_cfqq_must_alloc(cfqq
)) {
1844 if (cfq_cfqq_wait_request(cfqq
))
1845 return ELV_MQUEUE_MUST
;
1848 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1849 * can quickly flood the queue with writes from a single task
1851 if (rw
== READ
|| !cfq_cfqq_must_alloc_slice(cfqq
)) {
1852 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1853 return ELV_MQUEUE_MUST
;
1856 return ELV_MQUEUE_MAY
;
1858 if (cfq_class_idle(cfqq
))
1859 return ELV_MQUEUE_NO
;
1860 if (cfqq
->allocated
[rw
] >= cfqd
->max_queued
) {
1861 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
);
1862 int ret
= ELV_MQUEUE_NO
;
1864 if (ioc
&& ioc
->nr_batch_requests
)
1865 ret
= ELV_MQUEUE_MAY
;
1867 put_io_context(ioc
);
1871 return ELV_MQUEUE_MAY
;
1875 static int cfq_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1877 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1878 struct task_struct
*tsk
= current
;
1879 struct cfq_queue
*cfqq
;
1882 * don't force setup of a queue from here, as a call to may_queue
1883 * does not necessarily imply that a request actually will be queued.
1884 * so just lookup a possibly existing queue, or return 'may queue'
1887 cfqq
= cfq_find_cfq_hash(cfqd
, cfq_queue_pid(tsk
, rw
), tsk
->ioprio
);
1889 cfq_init_prio_data(cfqq
);
1890 cfq_prio_boost(cfqq
);
1892 return __cfq_may_queue(cfqd
, cfqq
, tsk
, rw
);
1895 return ELV_MQUEUE_MAY
;
1898 static void cfq_check_waiters(request_queue_t
*q
, struct cfq_queue
*cfqq
)
1900 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1901 struct request_list
*rl
= &q
->rq
;
1903 if (cfqq
->allocated
[READ
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1905 if (waitqueue_active(&rl
->wait
[READ
]))
1906 wake_up(&rl
->wait
[READ
]);
1909 if (cfqq
->allocated
[WRITE
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1911 if (waitqueue_active(&rl
->wait
[WRITE
]))
1912 wake_up(&rl
->wait
[WRITE
]);
1917 * queue lock held here
1919 static void cfq_put_request(request_queue_t
*q
, struct request
*rq
)
1921 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1922 struct cfq_rq
*crq
= RQ_DATA(rq
);
1925 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1926 const int rw
= rq_data_dir(rq
);
1928 BUG_ON(!cfqq
->allocated
[rw
]);
1929 cfqq
->allocated
[rw
]--;
1931 put_io_context(crq
->io_context
->ioc
);
1933 mempool_free(crq
, cfqd
->crq_pool
);
1934 rq
->elevator_private
= NULL
;
1936 cfq_check_waiters(q
, cfqq
);
1937 cfq_put_queue(cfqq
);
1942 * Allocate cfq data structures associated with this request.
1945 cfq_set_request(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
,
1948 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1949 struct task_struct
*tsk
= current
;
1950 struct cfq_io_context
*cic
;
1951 const int rw
= rq_data_dir(rq
);
1952 pid_t key
= cfq_queue_pid(tsk
, rw
);
1953 struct cfq_queue
*cfqq
;
1955 unsigned long flags
;
1956 int is_sync
= key
!= CFQ_KEY_ASYNC
;
1958 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1960 cic
= cfq_get_io_context(cfqd
, key
, gfp_mask
);
1962 spin_lock_irqsave(q
->queue_lock
, flags
);
1967 if (!cic
->cfqq
[is_sync
]) {
1968 cfqq
= cfq_get_queue(cfqd
, key
, tsk
->ioprio
, gfp_mask
);
1972 cic
->cfqq
[is_sync
] = cfqq
;
1974 cfqq
= cic
->cfqq
[is_sync
];
1976 cfqq
->allocated
[rw
]++;
1977 cfq_clear_cfqq_must_alloc(cfqq
);
1978 cfqd
->rq_starved
= 0;
1979 atomic_inc(&cfqq
->ref
);
1980 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1982 crq
= mempool_alloc(cfqd
->crq_pool
, gfp_mask
);
1984 RB_CLEAR(&crq
->rb_node
);
1987 INIT_HLIST_NODE(&crq
->hash
);
1988 crq
->cfq_queue
= cfqq
;
1989 crq
->io_context
= cic
;
1992 cfq_mark_crq_is_sync(crq
);
1994 cfq_clear_crq_is_sync(crq
);
1996 rq
->elevator_private
= crq
;
2000 spin_lock_irqsave(q
->queue_lock
, flags
);
2001 cfqq
->allocated
[rw
]--;
2002 if (!(cfqq
->allocated
[0] + cfqq
->allocated
[1]))
2003 cfq_mark_cfqq_must_alloc(cfqq
);
2004 cfq_put_queue(cfqq
);
2007 put_io_context(cic
->ioc
);
2009 * mark us rq allocation starved. we need to kickstart the process
2010 * ourselves if there are no pending requests that can do it for us.
2011 * that would be an extremely rare OOM situation
2013 cfqd
->rq_starved
= 1;
2014 cfq_schedule_dispatch(cfqd
);
2015 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2019 static void cfq_kick_queue(void *data
)
2021 request_queue_t
*q
= data
;
2022 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2023 unsigned long flags
;
2025 spin_lock_irqsave(q
->queue_lock
, flags
);
2027 if (cfqd
->rq_starved
) {
2028 struct request_list
*rl
= &q
->rq
;
2031 * we aren't guaranteed to get a request after this, but we
2032 * have to be opportunistic
2035 if (waitqueue_active(&rl
->wait
[READ
]))
2036 wake_up(&rl
->wait
[READ
]);
2037 if (waitqueue_active(&rl
->wait
[WRITE
]))
2038 wake_up(&rl
->wait
[WRITE
]);
2043 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2047 * Timer running if the active_queue is currently idling inside its time slice
2049 static void cfq_idle_slice_timer(unsigned long data
)
2051 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2052 struct cfq_queue
*cfqq
;
2053 unsigned long flags
;
2055 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2057 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
2058 unsigned long now
= jiffies
;
2063 if (time_after(now
, cfqq
->slice_end
))
2067 * only expire and reinvoke request handler, if there are
2068 * other queues with pending requests
2070 if (!cfqd
->busy_queues
) {
2071 cfqd
->idle_slice_timer
.expires
= min(now
+ cfqd
->cfq_slice_idle
, cfqq
->slice_end
);
2072 add_timer(&cfqd
->idle_slice_timer
);
2077 * not expired and it has a request pending, let it dispatch
2079 if (!RB_EMPTY(&cfqq
->sort_list
)) {
2080 cfq_mark_cfqq_must_dispatch(cfqq
);
2085 cfq_slice_expired(cfqd
, 0);
2087 cfq_schedule_dispatch(cfqd
);
2089 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2093 * Timer running if an idle class queue is waiting for service
2095 static void cfq_idle_class_timer(unsigned long data
)
2097 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2098 unsigned long flags
, end
;
2100 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2103 * race with a non-idle queue, reset timer
2105 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
2106 if (!time_after_eq(jiffies
, end
)) {
2107 cfqd
->idle_class_timer
.expires
= end
;
2108 add_timer(&cfqd
->idle_class_timer
);
2110 cfq_schedule_dispatch(cfqd
);
2112 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2115 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
2117 del_timer_sync(&cfqd
->idle_slice_timer
);
2118 del_timer_sync(&cfqd
->idle_class_timer
);
2119 blk_sync_queue(cfqd
->queue
);
2122 static void cfq_put_cfqd(struct cfq_data
*cfqd
)
2124 if (!atomic_dec_and_test(&cfqd
->ref
))
2127 cfq_shutdown_timer_wq(cfqd
);
2129 mempool_destroy(cfqd
->crq_pool
);
2130 kfree(cfqd
->crq_hash
);
2131 kfree(cfqd
->cfq_hash
);
2135 static void cfq_exit_queue(elevator_t
*e
)
2137 struct cfq_data
*cfqd
= e
->elevator_data
;
2138 request_queue_t
*q
= cfqd
->queue
;
2140 cfq_shutdown_timer_wq(cfqd
);
2141 write_lock(&cfq_exit_lock
);
2142 spin_lock_irq(q
->queue_lock
);
2143 if (cfqd
->active_queue
)
2144 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
2145 while(!list_empty(&cfqd
->cic_list
)) {
2146 struct cfq_io_context
*cic
= list_entry(cfqd
->cic_list
.next
,
2147 struct cfq_io_context
,
2149 if (cic
->cfqq
[ASYNC
]) {
2150 cfq_put_queue(cic
->cfqq
[ASYNC
]);
2151 cic
->cfqq
[ASYNC
] = NULL
;
2153 if (cic
->cfqq
[SYNC
]) {
2154 cfq_put_queue(cic
->cfqq
[SYNC
]);
2155 cic
->cfqq
[SYNC
] = NULL
;
2158 list_del_init(&cic
->queue_list
);
2160 spin_unlock_irq(q
->queue_lock
);
2161 write_unlock(&cfq_exit_lock
);
2165 static int cfq_init_queue(request_queue_t
*q
, elevator_t
*e
)
2167 struct cfq_data
*cfqd
;
2170 cfqd
= kmalloc(sizeof(*cfqd
), GFP_KERNEL
);
2174 memset(cfqd
, 0, sizeof(*cfqd
));
2176 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
2177 INIT_LIST_HEAD(&cfqd
->rr_list
[i
]);
2179 INIT_LIST_HEAD(&cfqd
->busy_rr
);
2180 INIT_LIST_HEAD(&cfqd
->cur_rr
);
2181 INIT_LIST_HEAD(&cfqd
->idle_rr
);
2182 INIT_LIST_HEAD(&cfqd
->empty_list
);
2183 INIT_LIST_HEAD(&cfqd
->cic_list
);
2185 cfqd
->crq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_MHASH_ENTRIES
, GFP_KERNEL
);
2186 if (!cfqd
->crq_hash
)
2189 cfqd
->cfq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
);
2190 if (!cfqd
->cfq_hash
)
2193 cfqd
->crq_pool
= mempool_create(BLKDEV_MIN_RQ
, mempool_alloc_slab
, mempool_free_slab
, crq_pool
);
2194 if (!cfqd
->crq_pool
)
2197 for (i
= 0; i
< CFQ_MHASH_ENTRIES
; i
++)
2198 INIT_HLIST_HEAD(&cfqd
->crq_hash
[i
]);
2199 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
2200 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2202 e
->elevator_data
= cfqd
;
2206 cfqd
->max_queued
= q
->nr_requests
/ 4;
2207 q
->nr_batching
= cfq_queued
;
2209 init_timer(&cfqd
->idle_slice_timer
);
2210 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2211 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2213 init_timer(&cfqd
->idle_class_timer
);
2214 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2215 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2217 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
, q
);
2219 atomic_set(&cfqd
->ref
, 1);
2221 cfqd
->cfq_queued
= cfq_queued
;
2222 cfqd
->cfq_quantum
= cfq_quantum
;
2223 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2224 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2225 cfqd
->cfq_back_max
= cfq_back_max
;
2226 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2227 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2228 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2229 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2230 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2231 cfqd
->cfq_max_depth
= cfq_max_depth
;
2235 kfree(cfqd
->cfq_hash
);
2237 kfree(cfqd
->crq_hash
);
2243 static void cfq_slab_kill(void)
2246 kmem_cache_destroy(crq_pool
);
2248 kmem_cache_destroy(cfq_pool
);
2250 kmem_cache_destroy(cfq_ioc_pool
);
2253 static int __init
cfq_slab_setup(void)
2255 crq_pool
= kmem_cache_create("crq_pool", sizeof(struct cfq_rq
), 0, 0,
2260 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2265 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2266 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2277 * sysfs parts below -->
2279 struct cfq_fs_entry
{
2280 struct attribute attr
;
2281 ssize_t (*show
)(struct cfq_data
*, char *);
2282 ssize_t (*store
)(struct cfq_data
*, const char *, size_t);
2286 cfq_var_show(unsigned int var
, char *page
)
2288 return sprintf(page
, "%d\n", var
);
2292 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2294 char *p
= (char *) page
;
2296 *var
= simple_strtoul(p
, &p
, 10);
2300 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2301 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2303 unsigned int __data = __VAR; \
2305 __data = jiffies_to_msecs(__data); \
2306 return cfq_var_show(__data, (page)); \
2308 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2309 SHOW_FUNCTION(cfq_queued_show
, cfqd
->cfq_queued
, 0);
2310 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2311 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2312 SHOW_FUNCTION(cfq_back_max_show
, cfqd
->cfq_back_max
, 0);
2313 SHOW_FUNCTION(cfq_back_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2314 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2315 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2316 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2317 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2318 SHOW_FUNCTION(cfq_max_depth_show
, cfqd
->cfq_max_depth
, 0);
2319 #undef SHOW_FUNCTION
2321 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2322 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2324 unsigned int __data; \
2325 int ret = cfq_var_store(&__data, (page), count); \
2326 if (__data < (MIN)) \
2328 else if (__data > (MAX)) \
2331 *(__PTR) = msecs_to_jiffies(__data); \
2333 *(__PTR) = __data; \
2336 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2337 STORE_FUNCTION(cfq_queued_store
, &cfqd
->cfq_queued
, 1, UINT_MAX
, 0);
2338 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2339 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2340 STORE_FUNCTION(cfq_back_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2341 STORE_FUNCTION(cfq_back_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2342 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2343 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2344 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2345 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2346 STORE_FUNCTION(cfq_max_depth_store
, &cfqd
->cfq_max_depth
, 1, UINT_MAX
, 0);
2347 #undef STORE_FUNCTION
2349 static struct cfq_fs_entry cfq_quantum_entry
= {
2350 .attr
= {.name
= "quantum", .mode
= S_IRUGO
| S_IWUSR
},
2351 .show
= cfq_quantum_show
,
2352 .store
= cfq_quantum_store
,
2354 static struct cfq_fs_entry cfq_queued_entry
= {
2355 .attr
= {.name
= "queued", .mode
= S_IRUGO
| S_IWUSR
},
2356 .show
= cfq_queued_show
,
2357 .store
= cfq_queued_store
,
2359 static struct cfq_fs_entry cfq_fifo_expire_sync_entry
= {
2360 .attr
= {.name
= "fifo_expire_sync", .mode
= S_IRUGO
| S_IWUSR
},
2361 .show
= cfq_fifo_expire_sync_show
,
2362 .store
= cfq_fifo_expire_sync_store
,
2364 static struct cfq_fs_entry cfq_fifo_expire_async_entry
= {
2365 .attr
= {.name
= "fifo_expire_async", .mode
= S_IRUGO
| S_IWUSR
},
2366 .show
= cfq_fifo_expire_async_show
,
2367 .store
= cfq_fifo_expire_async_store
,
2369 static struct cfq_fs_entry cfq_back_max_entry
= {
2370 .attr
= {.name
= "back_seek_max", .mode
= S_IRUGO
| S_IWUSR
},
2371 .show
= cfq_back_max_show
,
2372 .store
= cfq_back_max_store
,
2374 static struct cfq_fs_entry cfq_back_penalty_entry
= {
2375 .attr
= {.name
= "back_seek_penalty", .mode
= S_IRUGO
| S_IWUSR
},
2376 .show
= cfq_back_penalty_show
,
2377 .store
= cfq_back_penalty_store
,
2379 static struct cfq_fs_entry cfq_slice_sync_entry
= {
2380 .attr
= {.name
= "slice_sync", .mode
= S_IRUGO
| S_IWUSR
},
2381 .show
= cfq_slice_sync_show
,
2382 .store
= cfq_slice_sync_store
,
2384 static struct cfq_fs_entry cfq_slice_async_entry
= {
2385 .attr
= {.name
= "slice_async", .mode
= S_IRUGO
| S_IWUSR
},
2386 .show
= cfq_slice_async_show
,
2387 .store
= cfq_slice_async_store
,
2389 static struct cfq_fs_entry cfq_slice_async_rq_entry
= {
2390 .attr
= {.name
= "slice_async_rq", .mode
= S_IRUGO
| S_IWUSR
},
2391 .show
= cfq_slice_async_rq_show
,
2392 .store
= cfq_slice_async_rq_store
,
2394 static struct cfq_fs_entry cfq_slice_idle_entry
= {
2395 .attr
= {.name
= "slice_idle", .mode
= S_IRUGO
| S_IWUSR
},
2396 .show
= cfq_slice_idle_show
,
2397 .store
= cfq_slice_idle_store
,
2399 static struct cfq_fs_entry cfq_max_depth_entry
= {
2400 .attr
= {.name
= "max_depth", .mode
= S_IRUGO
| S_IWUSR
},
2401 .show
= cfq_max_depth_show
,
2402 .store
= cfq_max_depth_store
,
2405 static struct attribute
*default_attrs
[] = {
2406 &cfq_quantum_entry
.attr
,
2407 &cfq_queued_entry
.attr
,
2408 &cfq_fifo_expire_sync_entry
.attr
,
2409 &cfq_fifo_expire_async_entry
.attr
,
2410 &cfq_back_max_entry
.attr
,
2411 &cfq_back_penalty_entry
.attr
,
2412 &cfq_slice_sync_entry
.attr
,
2413 &cfq_slice_async_entry
.attr
,
2414 &cfq_slice_async_rq_entry
.attr
,
2415 &cfq_slice_idle_entry
.attr
,
2416 &cfq_max_depth_entry
.attr
,
2420 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2423 cfq_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2425 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2426 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2431 return entry
->show(e
->elevator_data
, page
);
2435 cfq_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2436 const char *page
, size_t length
)
2438 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2439 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2444 return entry
->store(e
->elevator_data
, page
, length
);
2447 static struct sysfs_ops cfq_sysfs_ops
= {
2448 .show
= cfq_attr_show
,
2449 .store
= cfq_attr_store
,
2452 static struct kobj_type cfq_ktype
= {
2453 .sysfs_ops
= &cfq_sysfs_ops
,
2454 .default_attrs
= default_attrs
,
2457 static struct elevator_type iosched_cfq
= {
2459 .elevator_merge_fn
= cfq_merge
,
2460 .elevator_merged_fn
= cfq_merged_request
,
2461 .elevator_merge_req_fn
= cfq_merged_requests
,
2462 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2463 .elevator_add_req_fn
= cfq_insert_request
,
2464 .elevator_activate_req_fn
= cfq_activate_request
,
2465 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2466 .elevator_queue_empty_fn
= cfq_queue_empty
,
2467 .elevator_completed_req_fn
= cfq_completed_request
,
2468 .elevator_former_req_fn
= cfq_former_request
,
2469 .elevator_latter_req_fn
= cfq_latter_request
,
2470 .elevator_set_req_fn
= cfq_set_request
,
2471 .elevator_put_req_fn
= cfq_put_request
,
2472 .elevator_may_queue_fn
= cfq_may_queue
,
2473 .elevator_init_fn
= cfq_init_queue
,
2474 .elevator_exit_fn
= cfq_exit_queue
,
2476 .elevator_ktype
= &cfq_ktype
,
2477 .elevator_name
= "cfq",
2478 .elevator_owner
= THIS_MODULE
,
2481 static int __init
cfq_init(void)
2486 * could be 0 on HZ < 1000 setups
2488 if (!cfq_slice_async
)
2489 cfq_slice_async
= 1;
2490 if (!cfq_slice_idle
)
2493 if (cfq_slab_setup())
2496 ret
= elv_register(&iosched_cfq
);
2503 static void __exit
cfq_exit(void)
2505 elv_unregister(&iosched_cfq
);
2509 module_init(cfq_init
);
2510 module_exit(cfq_exit
);
2512 MODULE_AUTHOR("Jens Axboe");
2513 MODULE_LICENSE("GPL");
2514 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");