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 static atomic_t ioc_count
= ATOMIC_INIT(0);
95 static struct completion
*ioc_gone
;
97 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
98 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
99 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
100 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
105 #define cfq_cfqq_dispatched(cfqq) \
106 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
108 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
110 #define cfq_cfqq_sync(cfqq) \
111 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
114 * Per block device queue structure
118 request_queue_t
*queue
;
121 * rr list of queues with requests and the count of them
123 struct list_head rr_list
[CFQ_PRIO_LISTS
];
124 struct list_head busy_rr
;
125 struct list_head cur_rr
;
126 struct list_head idle_rr
;
127 unsigned int busy_queues
;
130 * non-ordered list of empty cfqq's
132 struct list_head empty_list
;
137 struct hlist_head
*cfq_hash
;
140 * global crq hash for all queues
142 struct hlist_head
*crq_hash
;
144 unsigned int max_queued
;
151 * schedule slice state info
154 * idle window management
156 struct timer_list idle_slice_timer
;
157 struct work_struct unplug_work
;
159 struct cfq_queue
*active_queue
;
160 struct cfq_io_context
*active_cic
;
161 int cur_prio
, cur_end_prio
;
162 unsigned int dispatch_slice
;
164 struct timer_list idle_class_timer
;
166 sector_t last_sector
;
167 unsigned long last_end_request
;
169 unsigned int rq_starved
;
172 * tunables, see top of file
174 unsigned int cfq_quantum
;
175 unsigned int cfq_queued
;
176 unsigned int cfq_fifo_expire
[2];
177 unsigned int cfq_back_penalty
;
178 unsigned int cfq_back_max
;
179 unsigned int cfq_slice
[2];
180 unsigned int cfq_slice_async_rq
;
181 unsigned int cfq_slice_idle
;
182 unsigned int cfq_max_depth
;
184 struct list_head cic_list
;
188 * Per process-grouping structure
191 /* reference count */
193 /* parent cfq_data */
194 struct cfq_data
*cfqd
;
195 /* cfqq lookup hash */
196 struct hlist_node cfq_hash
;
199 /* on either rr or empty list of cfqd */
200 struct list_head cfq_list
;
201 /* sorted list of pending requests */
202 struct rb_root sort_list
;
203 /* if fifo isn't expired, next request to serve */
204 struct cfq_rq
*next_crq
;
205 /* requests queued in sort_list */
207 /* currently allocated requests */
209 /* fifo list of requests in sort_list */
210 struct list_head fifo
;
212 unsigned long slice_start
;
213 unsigned long slice_end
;
214 unsigned long slice_left
;
215 unsigned long service_last
;
217 /* number of requests that are on the dispatch list */
220 /* io prio of this group */
221 unsigned short ioprio
, org_ioprio
;
222 unsigned short ioprio_class
, org_ioprio_class
;
224 /* various state flags, see below */
229 struct rb_node rb_node
;
231 struct request
*request
;
232 struct hlist_node hash
;
234 struct cfq_queue
*cfq_queue
;
235 struct cfq_io_context
*io_context
;
237 unsigned int crq_flags
;
240 enum cfqq_state_flags
{
241 CFQ_CFQQ_FLAG_on_rr
= 0,
242 CFQ_CFQQ_FLAG_wait_request
,
243 CFQ_CFQQ_FLAG_must_alloc
,
244 CFQ_CFQQ_FLAG_must_alloc_slice
,
245 CFQ_CFQQ_FLAG_must_dispatch
,
246 CFQ_CFQQ_FLAG_fifo_expire
,
247 CFQ_CFQQ_FLAG_idle_window
,
248 CFQ_CFQQ_FLAG_prio_changed
,
251 #define CFQ_CFQQ_FNS(name) \
252 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
254 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
256 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
258 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
260 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
262 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
266 CFQ_CFQQ_FNS(wait_request
);
267 CFQ_CFQQ_FNS(must_alloc
);
268 CFQ_CFQQ_FNS(must_alloc_slice
);
269 CFQ_CFQQ_FNS(must_dispatch
);
270 CFQ_CFQQ_FNS(fifo_expire
);
271 CFQ_CFQQ_FNS(idle_window
);
272 CFQ_CFQQ_FNS(prio_changed
);
275 enum cfq_rq_state_flags
{
276 CFQ_CRQ_FLAG_is_sync
= 0,
279 #define CFQ_CRQ_FNS(name) \
280 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
282 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
284 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
286 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
288 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
290 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
293 CFQ_CRQ_FNS(is_sync
);
296 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
297 static void cfq_dispatch_insert(request_queue_t
*, struct cfq_rq
*);
298 static void cfq_put_cfqd(struct cfq_data
*cfqd
);
299 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
, gfp_t gfp_mask
);
301 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
304 * lots of deadline iosched dupes, can be abstracted later...
306 static inline void cfq_del_crq_hash(struct cfq_rq
*crq
)
308 hlist_del_init(&crq
->hash
);
311 static inline void cfq_add_crq_hash(struct cfq_data
*cfqd
, struct cfq_rq
*crq
)
313 const int hash_idx
= CFQ_MHASH_FN(rq_hash_key(crq
->request
));
315 hlist_add_head(&crq
->hash
, &cfqd
->crq_hash
[hash_idx
]);
318 static struct request
*cfq_find_rq_hash(struct cfq_data
*cfqd
, sector_t offset
)
320 struct hlist_head
*hash_list
= &cfqd
->crq_hash
[CFQ_MHASH_FN(offset
)];
321 struct hlist_node
*entry
, *next
;
323 hlist_for_each_safe(entry
, next
, hash_list
) {
324 struct cfq_rq
*crq
= list_entry_hash(entry
);
325 struct request
*__rq
= crq
->request
;
327 if (!rq_mergeable(__rq
)) {
328 cfq_del_crq_hash(crq
);
332 if (rq_hash_key(__rq
) == offset
)
340 * scheduler run of queue, if there are requests pending and no one in the
341 * driver that will restart queueing
343 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
345 if (cfqd
->busy_queues
)
346 kblockd_schedule_work(&cfqd
->unplug_work
);
349 static int cfq_queue_empty(request_queue_t
*q
)
351 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
353 return !cfqd
->busy_queues
;
357 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
358 * We choose the request that is closest to the head right now. Distance
359 * behind the head are penalized and only allowed to a certain extent.
361 static struct cfq_rq
*
362 cfq_choose_req(struct cfq_data
*cfqd
, struct cfq_rq
*crq1
, struct cfq_rq
*crq2
)
364 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
365 int r1_wrap
= 0, r2_wrap
= 0; /* requests are behind the disk head */
366 unsigned long back_max
;
368 if (crq1
== NULL
|| crq1
== crq2
)
373 if (cfq_crq_is_sync(crq1
) && !cfq_crq_is_sync(crq2
))
375 else if (cfq_crq_is_sync(crq2
) && !cfq_crq_is_sync(crq1
))
378 s1
= crq1
->request
->sector
;
379 s2
= crq2
->request
->sector
;
381 last
= cfqd
->last_sector
;
384 * by definition, 1KiB is 2 sectors
386 back_max
= cfqd
->cfq_back_max
* 2;
389 * Strict one way elevator _except_ in the case where we allow
390 * short backward seeks which are biased as twice the cost of a
391 * similar forward seek.
395 else if (s1
+ back_max
>= last
)
396 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
402 else if (s2
+ back_max
>= last
)
403 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
407 /* Found required data */
408 if (!r1_wrap
&& r2_wrap
)
410 else if (!r2_wrap
&& r1_wrap
)
412 else if (r1_wrap
&& r2_wrap
) {
413 /* both behind the head */
420 /* Both requests in front of the head */
434 * would be nice to take fifo expire time into account as well
436 static struct cfq_rq
*
437 cfq_find_next_crq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
440 struct cfq_rq
*crq_next
= NULL
, *crq_prev
= NULL
;
441 struct rb_node
*rbnext
, *rbprev
;
443 if (!(rbnext
= rb_next(&last
->rb_node
))) {
444 rbnext
= rb_first(&cfqq
->sort_list
);
445 if (rbnext
== &last
->rb_node
)
449 rbprev
= rb_prev(&last
->rb_node
);
452 crq_prev
= rb_entry_crq(rbprev
);
454 crq_next
= rb_entry_crq(rbnext
);
456 return cfq_choose_req(cfqd
, crq_next
, crq_prev
);
459 static void cfq_update_next_crq(struct cfq_rq
*crq
)
461 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
463 if (cfqq
->next_crq
== crq
)
464 cfqq
->next_crq
= cfq_find_next_crq(cfqq
->cfqd
, cfqq
, crq
);
467 static void cfq_resort_rr_list(struct cfq_queue
*cfqq
, int preempted
)
469 struct cfq_data
*cfqd
= cfqq
->cfqd
;
470 struct list_head
*list
, *entry
;
472 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
474 list_del(&cfqq
->cfq_list
);
476 if (cfq_class_rt(cfqq
))
477 list
= &cfqd
->cur_rr
;
478 else if (cfq_class_idle(cfqq
))
479 list
= &cfqd
->idle_rr
;
482 * if cfqq has requests in flight, don't allow it to be
483 * found in cfq_set_active_queue before it has finished them.
484 * this is done to increase fairness between a process that
485 * has lots of io pending vs one that only generates one
486 * sporadically or synchronously
488 if (cfq_cfqq_dispatched(cfqq
))
489 list
= &cfqd
->busy_rr
;
491 list
= &cfqd
->rr_list
[cfqq
->ioprio
];
495 * if queue was preempted, just add to front to be fair. busy_rr
498 if (preempted
|| list
== &cfqd
->busy_rr
) {
499 list_add(&cfqq
->cfq_list
, list
);
504 * sort by when queue was last serviced
507 while ((entry
= entry
->prev
) != list
) {
508 struct cfq_queue
*__cfqq
= list_entry_cfqq(entry
);
510 if (!__cfqq
->service_last
)
512 if (time_before(__cfqq
->service_last
, cfqq
->service_last
))
516 list_add(&cfqq
->cfq_list
, entry
);
520 * add to busy list of queues for service, trying to be fair in ordering
521 * the pending list according to last request service
524 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
526 BUG_ON(cfq_cfqq_on_rr(cfqq
));
527 cfq_mark_cfqq_on_rr(cfqq
);
530 cfq_resort_rr_list(cfqq
, 0);
534 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
536 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
537 cfq_clear_cfqq_on_rr(cfqq
);
538 list_move(&cfqq
->cfq_list
, &cfqd
->empty_list
);
540 BUG_ON(!cfqd
->busy_queues
);
545 * rb tree support functions
547 static inline void cfq_del_crq_rb(struct cfq_rq
*crq
)
549 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
550 struct cfq_data
*cfqd
= cfqq
->cfqd
;
551 const int sync
= cfq_crq_is_sync(crq
);
553 BUG_ON(!cfqq
->queued
[sync
]);
554 cfqq
->queued
[sync
]--;
556 cfq_update_next_crq(crq
);
558 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
559 RB_CLEAR_COLOR(&crq
->rb_node
);
561 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY(&cfqq
->sort_list
))
562 cfq_del_cfqq_rr(cfqd
, cfqq
);
565 static struct cfq_rq
*
566 __cfq_add_crq_rb(struct cfq_rq
*crq
)
568 struct rb_node
**p
= &crq
->cfq_queue
->sort_list
.rb_node
;
569 struct rb_node
*parent
= NULL
;
570 struct cfq_rq
*__crq
;
574 __crq
= rb_entry_crq(parent
);
576 if (crq
->rb_key
< __crq
->rb_key
)
578 else if (crq
->rb_key
> __crq
->rb_key
)
584 rb_link_node(&crq
->rb_node
, parent
, p
);
588 static void cfq_add_crq_rb(struct cfq_rq
*crq
)
590 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
591 struct cfq_data
*cfqd
= cfqq
->cfqd
;
592 struct request
*rq
= crq
->request
;
593 struct cfq_rq
*__alias
;
595 crq
->rb_key
= rq_rb_key(rq
);
596 cfqq
->queued
[cfq_crq_is_sync(crq
)]++;
599 * looks a little odd, but the first insert might return an alias.
600 * if that happens, put the alias on the dispatch list
602 while ((__alias
= __cfq_add_crq_rb(crq
)) != NULL
)
603 cfq_dispatch_insert(cfqd
->queue
, __alias
);
605 rb_insert_color(&crq
->rb_node
, &cfqq
->sort_list
);
607 if (!cfq_cfqq_on_rr(cfqq
))
608 cfq_add_cfqq_rr(cfqd
, cfqq
);
611 * check if this request is a better next-serve candidate
613 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
617 cfq_reposition_crq_rb(struct cfq_queue
*cfqq
, struct cfq_rq
*crq
)
619 rb_erase(&crq
->rb_node
, &cfqq
->sort_list
);
620 cfqq
->queued
[cfq_crq_is_sync(crq
)]--;
625 static struct request
*cfq_find_rq_rb(struct cfq_data
*cfqd
, sector_t sector
)
628 struct cfq_queue
*cfqq
= cfq_find_cfq_hash(cfqd
, current
->pid
, CFQ_KEY_ANY
);
634 n
= cfqq
->sort_list
.rb_node
;
636 struct cfq_rq
*crq
= rb_entry_crq(n
);
638 if (sector
< crq
->rb_key
)
640 else if (sector
> crq
->rb_key
)
650 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
652 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
654 cfqd
->rq_in_driver
++;
657 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
659 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
661 WARN_ON(!cfqd
->rq_in_driver
);
662 cfqd
->rq_in_driver
--;
665 static void cfq_remove_request(struct request
*rq
)
667 struct cfq_rq
*crq
= RQ_DATA(rq
);
669 list_del_init(&rq
->queuelist
);
671 cfq_del_crq_hash(crq
);
675 cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
677 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
678 struct request
*__rq
;
681 __rq
= cfq_find_rq_hash(cfqd
, bio
->bi_sector
);
682 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
683 ret
= ELEVATOR_BACK_MERGE
;
687 __rq
= cfq_find_rq_rb(cfqd
, bio
->bi_sector
+ bio_sectors(bio
));
688 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
689 ret
= ELEVATOR_FRONT_MERGE
;
693 return ELEVATOR_NO_MERGE
;
699 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
)
701 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
702 struct cfq_rq
*crq
= RQ_DATA(req
);
704 cfq_del_crq_hash(crq
);
705 cfq_add_crq_hash(cfqd
, crq
);
707 if (rq_rb_key(req
) != crq
->rb_key
) {
708 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
710 cfq_update_next_crq(crq
);
711 cfq_reposition_crq_rb(cfqq
, crq
);
716 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
717 struct request
*next
)
719 cfq_merged_request(q
, rq
);
722 * reposition in fifo if next is older than rq
724 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
725 time_before(next
->start_time
, rq
->start_time
))
726 list_move(&rq
->queuelist
, &next
->queuelist
);
728 cfq_remove_request(next
);
732 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
736 * stop potential idle class queues waiting service
738 del_timer(&cfqd
->idle_class_timer
);
740 cfqq
->slice_start
= jiffies
;
742 cfqq
->slice_left
= 0;
743 cfq_clear_cfqq_must_alloc_slice(cfqq
);
744 cfq_clear_cfqq_fifo_expire(cfqq
);
747 cfqd
->active_queue
= cfqq
;
751 * current cfqq expired its slice (or was too idle), select new one
754 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
757 unsigned long now
= jiffies
;
759 if (cfq_cfqq_wait_request(cfqq
))
760 del_timer(&cfqd
->idle_slice_timer
);
762 if (!preempted
&& !cfq_cfqq_dispatched(cfqq
)) {
763 cfqq
->service_last
= now
;
764 cfq_schedule_dispatch(cfqd
);
767 cfq_clear_cfqq_must_dispatch(cfqq
);
768 cfq_clear_cfqq_wait_request(cfqq
);
771 * store what was left of this slice, if the queue idled out
774 if (time_after(cfqq
->slice_end
, now
))
775 cfqq
->slice_left
= cfqq
->slice_end
- now
;
777 cfqq
->slice_left
= 0;
779 if (cfq_cfqq_on_rr(cfqq
))
780 cfq_resort_rr_list(cfqq
, preempted
);
782 if (cfqq
== cfqd
->active_queue
)
783 cfqd
->active_queue
= NULL
;
785 if (cfqd
->active_cic
) {
786 put_io_context(cfqd
->active_cic
->ioc
);
787 cfqd
->active_cic
= NULL
;
790 cfqd
->dispatch_slice
= 0;
793 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int preempted
)
795 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
798 __cfq_slice_expired(cfqd
, cfqq
, preempted
);
811 static int cfq_get_next_prio_level(struct cfq_data
*cfqd
)
820 for (p
= cfqd
->cur_prio
; p
<= cfqd
->cur_end_prio
; p
++) {
821 if (!list_empty(&cfqd
->rr_list
[p
])) {
830 if (++cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
831 cfqd
->cur_end_prio
= 0;
838 if (unlikely(prio
== -1))
841 BUG_ON(prio
>= CFQ_PRIO_LISTS
);
843 list_splice_init(&cfqd
->rr_list
[prio
], &cfqd
->cur_rr
);
845 cfqd
->cur_prio
= prio
+ 1;
846 if (cfqd
->cur_prio
> cfqd
->cur_end_prio
) {
847 cfqd
->cur_end_prio
= cfqd
->cur_prio
;
850 if (cfqd
->cur_end_prio
== CFQ_PRIO_LISTS
) {
852 cfqd
->cur_end_prio
= 0;
858 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
860 struct cfq_queue
*cfqq
= NULL
;
863 * if current list is non-empty, grab first entry. if it is empty,
864 * get next prio level and grab first entry then if any are spliced
866 if (!list_empty(&cfqd
->cur_rr
) || cfq_get_next_prio_level(cfqd
) != -1)
867 cfqq
= list_entry_cfqq(cfqd
->cur_rr
.next
);
870 * if we have idle queues and no rt or be queues had pending
871 * requests, either allow immediate service if the grace period
872 * has passed or arm the idle grace timer
874 if (!cfqq
&& !list_empty(&cfqd
->idle_rr
)) {
875 unsigned long end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
877 if (time_after_eq(jiffies
, end
))
878 cfqq
= list_entry_cfqq(cfqd
->idle_rr
.next
);
880 mod_timer(&cfqd
->idle_class_timer
, end
);
883 __cfq_set_active_queue(cfqd
, cfqq
);
887 static int cfq_arm_slice_timer(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
892 WARN_ON(!RB_EMPTY(&cfqq
->sort_list
));
893 WARN_ON(cfqq
!= cfqd
->active_queue
);
896 * idle is disabled, either manually or by past process history
898 if (!cfqd
->cfq_slice_idle
)
900 if (!cfq_cfqq_idle_window(cfqq
))
903 * task has exited, don't wait
905 if (cfqd
->active_cic
&& !cfqd
->active_cic
->ioc
->task
)
908 cfq_mark_cfqq_must_dispatch(cfqq
);
909 cfq_mark_cfqq_wait_request(cfqq
);
911 sl
= min(cfqq
->slice_end
- 1, (unsigned long) cfqd
->cfq_slice_idle
);
912 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
916 static void cfq_dispatch_insert(request_queue_t
*q
, struct cfq_rq
*crq
)
918 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
919 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
921 cfqq
->next_crq
= cfq_find_next_crq(cfqd
, cfqq
, crq
);
922 cfq_remove_request(crq
->request
);
923 cfqq
->on_dispatch
[cfq_crq_is_sync(crq
)]++;
924 elv_dispatch_sort(q
, crq
->request
);
928 * return expired entry, or NULL to just start from scratch in rbtree
930 static inline struct cfq_rq
*cfq_check_fifo(struct cfq_queue
*cfqq
)
932 struct cfq_data
*cfqd
= cfqq
->cfqd
;
936 if (cfq_cfqq_fifo_expire(cfqq
))
939 if (!list_empty(&cfqq
->fifo
)) {
940 int fifo
= cfq_cfqq_class_sync(cfqq
);
942 crq
= RQ_DATA(list_entry_fifo(cfqq
->fifo
.next
));
944 if (time_after(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
])) {
945 cfq_mark_cfqq_fifo_expire(cfqq
);
954 * Scale schedule slice based on io priority. Use the sync time slice only
955 * if a queue is marked sync and has sync io queued. A sync queue with async
956 * io only, should not get full sync slice length.
959 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
961 const int base_slice
= cfqd
->cfq_slice
[cfq_cfqq_sync(cfqq
)];
963 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
965 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - cfqq
->ioprio
));
969 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
971 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
975 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
977 const int base_rq
= cfqd
->cfq_slice_async_rq
;
979 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
981 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
985 * get next queue for service
987 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
989 unsigned long now
= jiffies
;
990 struct cfq_queue
*cfqq
;
992 cfqq
= cfqd
->active_queue
;
999 if (!cfq_cfqq_must_dispatch(cfqq
) && time_after(now
, cfqq
->slice_end
))
1003 * if queue has requests, dispatch one. if not, check if
1004 * enough slice is left to wait for one
1006 if (!RB_EMPTY(&cfqq
->sort_list
))
1008 else if (cfq_cfqq_class_sync(cfqq
) &&
1009 time_before(now
, cfqq
->slice_end
)) {
1010 if (cfq_arm_slice_timer(cfqd
, cfqq
))
1015 cfq_slice_expired(cfqd
, 0);
1017 cfqq
= cfq_set_active_queue(cfqd
);
1023 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1028 BUG_ON(RB_EMPTY(&cfqq
->sort_list
));
1034 * follow expired path, else get first next available
1036 if ((crq
= cfq_check_fifo(cfqq
)) == NULL
)
1037 crq
= cfqq
->next_crq
;
1040 * finally, insert request into driver dispatch list
1042 cfq_dispatch_insert(cfqd
->queue
, crq
);
1044 cfqd
->dispatch_slice
++;
1047 if (!cfqd
->active_cic
) {
1048 atomic_inc(&crq
->io_context
->ioc
->refcount
);
1049 cfqd
->active_cic
= crq
->io_context
;
1052 if (RB_EMPTY(&cfqq
->sort_list
))
1055 } while (dispatched
< max_dispatch
);
1058 * if slice end isn't set yet, set it. if at least one request was
1059 * sync, use the sync time slice value
1061 if (!cfqq
->slice_end
)
1062 cfq_set_prio_slice(cfqd
, cfqq
);
1065 * expire an async queue immediately if it has used up its slice. idle
1066 * queue always expire after 1 dispatch round.
1068 if ((!cfq_cfqq_sync(cfqq
) &&
1069 cfqd
->dispatch_slice
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
1070 cfq_class_idle(cfqq
))
1071 cfq_slice_expired(cfqd
, 0);
1077 cfq_forced_dispatch_cfqqs(struct list_head
*list
)
1080 struct cfq_queue
*cfqq
, *next
;
1083 list_for_each_entry_safe(cfqq
, next
, list
, cfq_list
) {
1084 while ((crq
= cfqq
->next_crq
)) {
1085 cfq_dispatch_insert(cfqq
->cfqd
->queue
, crq
);
1088 BUG_ON(!list_empty(&cfqq
->fifo
));
1094 cfq_forced_dispatch(struct cfq_data
*cfqd
)
1096 int i
, dispatched
= 0;
1098 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
1099 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->rr_list
[i
]);
1101 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->busy_rr
);
1102 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->cur_rr
);
1103 dispatched
+= cfq_forced_dispatch_cfqqs(&cfqd
->idle_rr
);
1105 cfq_slice_expired(cfqd
, 0);
1107 BUG_ON(cfqd
->busy_queues
);
1113 cfq_dispatch_requests(request_queue_t
*q
, int force
)
1115 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1116 struct cfq_queue
*cfqq
;
1118 if (!cfqd
->busy_queues
)
1121 if (unlikely(force
))
1122 return cfq_forced_dispatch(cfqd
);
1124 cfqq
= cfq_select_queue(cfqd
);
1129 * if idle window is disabled, allow queue buildup
1131 if (!cfq_cfqq_idle_window(cfqq
) &&
1132 cfqd
->rq_in_driver
>= cfqd
->cfq_max_depth
)
1135 cfq_clear_cfqq_must_dispatch(cfqq
);
1136 cfq_clear_cfqq_wait_request(cfqq
);
1137 del_timer(&cfqd
->idle_slice_timer
);
1139 max_dispatch
= cfqd
->cfq_quantum
;
1140 if (cfq_class_idle(cfqq
))
1143 return __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1150 * task holds one reference to the queue, dropped when task exits. each crq
1151 * in-flight on this queue also holds a reference, dropped when crq is freed.
1153 * queue lock must be held here.
1155 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1157 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1159 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1161 if (!atomic_dec_and_test(&cfqq
->ref
))
1164 BUG_ON(rb_first(&cfqq
->sort_list
));
1165 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1166 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1168 if (unlikely(cfqd
->active_queue
== cfqq
))
1169 __cfq_slice_expired(cfqd
, cfqq
, 0);
1171 cfq_put_cfqd(cfqq
->cfqd
);
1174 * it's on the empty list and still hashed
1176 list_del(&cfqq
->cfq_list
);
1177 hlist_del(&cfqq
->cfq_hash
);
1178 kmem_cache_free(cfq_pool
, cfqq
);
1181 static inline struct cfq_queue
*
1182 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1185 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1186 struct hlist_node
*entry
, *next
;
1188 hlist_for_each_safe(entry
, next
, hash_list
) {
1189 struct cfq_queue
*__cfqq
= list_entry_qhash(entry
);
1190 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->org_ioprio_class
, __cfqq
->org_ioprio
);
1192 if (__cfqq
->key
== key
&& (__p
== prio
|| prio
== CFQ_KEY_ANY
))
1199 static struct cfq_queue
*
1200 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1202 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1205 static void cfq_free_io_context(struct cfq_io_context
*cic
)
1207 struct cfq_io_context
*__cic
;
1208 struct list_head
*entry
, *next
;
1211 list_for_each_safe(entry
, next
, &cic
->list
) {
1212 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1213 kmem_cache_free(cfq_ioc_pool
, __cic
);
1217 kmem_cache_free(cfq_ioc_pool
, cic
);
1218 if (atomic_sub_and_test(freed
, &ioc_count
) && ioc_gone
)
1222 static void cfq_trim(struct io_context
*ioc
)
1224 ioc
->set_ioprio
= NULL
;
1226 cfq_free_io_context(ioc
->cic
);
1230 * Called with interrupts disabled
1232 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1234 struct cfq_data
*cfqd
= cic
->key
;
1242 WARN_ON(!irqs_disabled());
1244 spin_lock(q
->queue_lock
);
1246 if (cic
->cfqq
[ASYNC
]) {
1247 if (unlikely(cic
->cfqq
[ASYNC
] == cfqd
->active_queue
))
1248 __cfq_slice_expired(cfqd
, cic
->cfqq
[ASYNC
], 0);
1249 cfq_put_queue(cic
->cfqq
[ASYNC
]);
1250 cic
->cfqq
[ASYNC
] = NULL
;
1253 if (cic
->cfqq
[SYNC
]) {
1254 if (unlikely(cic
->cfqq
[SYNC
] == cfqd
->active_queue
))
1255 __cfq_slice_expired(cfqd
, cic
->cfqq
[SYNC
], 0);
1256 cfq_put_queue(cic
->cfqq
[SYNC
]);
1257 cic
->cfqq
[SYNC
] = NULL
;
1261 list_del_init(&cic
->queue_list
);
1262 spin_unlock(q
->queue_lock
);
1266 * Another task may update the task cic list, if it is doing a queue lookup
1267 * on its behalf. cfq_cic_lock excludes such concurrent updates
1269 static void cfq_exit_io_context(struct cfq_io_context
*cic
)
1271 struct cfq_io_context
*__cic
;
1272 struct list_head
*entry
;
1273 unsigned long flags
;
1275 local_irq_save(flags
);
1278 * put the reference this task is holding to the various queues
1280 read_lock(&cfq_exit_lock
);
1281 list_for_each(entry
, &cic
->list
) {
1282 __cic
= list_entry(entry
, struct cfq_io_context
, list
);
1283 cfq_exit_single_io_context(__cic
);
1286 cfq_exit_single_io_context(cic
);
1287 read_unlock(&cfq_exit_lock
);
1288 local_irq_restore(flags
);
1291 static struct cfq_io_context
*
1292 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1294 struct cfq_io_context
*cic
= kmem_cache_alloc(cfq_ioc_pool
, gfp_mask
);
1297 INIT_LIST_HEAD(&cic
->list
);
1298 cic
->cfqq
[ASYNC
] = NULL
;
1299 cic
->cfqq
[SYNC
] = NULL
;
1301 cic
->last_end_request
= jiffies
;
1302 cic
->ttime_total
= 0;
1303 cic
->ttime_samples
= 0;
1304 cic
->ttime_mean
= 0;
1305 cic
->dtor
= cfq_free_io_context
;
1306 cic
->exit
= cfq_exit_io_context
;
1307 INIT_LIST_HEAD(&cic
->queue_list
);
1308 atomic_inc(&ioc_count
);
1314 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1316 struct task_struct
*tsk
= current
;
1319 if (!cfq_cfqq_prio_changed(cfqq
))
1322 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1323 switch (ioprio_class
) {
1325 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1326 case IOPRIO_CLASS_NONE
:
1328 * no prio set, place us in the middle of the BE classes
1330 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1331 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1333 case IOPRIO_CLASS_RT
:
1334 cfqq
->ioprio
= task_ioprio(tsk
);
1335 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1337 case IOPRIO_CLASS_BE
:
1338 cfqq
->ioprio
= task_ioprio(tsk
);
1339 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1341 case IOPRIO_CLASS_IDLE
:
1342 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1344 cfq_clear_cfqq_idle_window(cfqq
);
1349 * keep track of original prio settings in case we have to temporarily
1350 * elevate the priority of this queue
1352 cfqq
->org_ioprio
= cfqq
->ioprio
;
1353 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1355 if (cfq_cfqq_on_rr(cfqq
))
1356 cfq_resort_rr_list(cfqq
, 0);
1358 cfq_clear_cfqq_prio_changed(cfqq
);
1361 static inline void changed_ioprio(struct cfq_io_context
*cic
)
1363 struct cfq_data
*cfqd
= cic
->key
;
1364 struct cfq_queue
*cfqq
;
1366 spin_lock(cfqd
->queue
->queue_lock
);
1367 cfqq
= cic
->cfqq
[ASYNC
];
1369 struct cfq_queue
*new_cfqq
;
1370 new_cfqq
= cfq_get_queue(cfqd
, CFQ_KEY_ASYNC
,
1371 cic
->ioc
->task
, GFP_ATOMIC
);
1373 cic
->cfqq
[ASYNC
] = new_cfqq
;
1374 cfq_put_queue(cfqq
);
1377 cfqq
= cic
->cfqq
[SYNC
];
1379 cfq_mark_cfqq_prio_changed(cfqq
);
1380 cfq_init_prio_data(cfqq
);
1382 spin_unlock(cfqd
->queue
->queue_lock
);
1387 * callback from sys_ioprio_set, irqs are disabled
1389 static int cfq_ioc_set_ioprio(struct io_context
*ioc
, unsigned int ioprio
)
1391 struct cfq_io_context
*cic
;
1393 write_lock(&cfq_exit_lock
);
1397 changed_ioprio(cic
);
1399 list_for_each_entry(cic
, &cic
->list
, list
)
1400 changed_ioprio(cic
);
1402 write_unlock(&cfq_exit_lock
);
1407 static struct cfq_queue
*
1408 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
,
1411 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1412 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1413 unsigned short ioprio
;
1416 ioprio
= tsk
->ioprio
;
1417 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1423 } else if (gfp_mask
& __GFP_WAIT
) {
1424 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1425 new_cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1426 spin_lock_irq(cfqd
->queue
->queue_lock
);
1429 cfqq
= kmem_cache_alloc(cfq_pool
, gfp_mask
);
1434 memset(cfqq
, 0, sizeof(*cfqq
));
1436 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1437 INIT_LIST_HEAD(&cfqq
->cfq_list
);
1438 RB_CLEAR_ROOT(&cfqq
->sort_list
);
1439 INIT_LIST_HEAD(&cfqq
->fifo
);
1442 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1443 atomic_set(&cfqq
->ref
, 0);
1445 atomic_inc(&cfqd
->ref
);
1446 cfqq
->service_last
= 0;
1448 * set ->slice_left to allow preemption for a new process
1450 cfqq
->slice_left
= 2 * cfqd
->cfq_slice_idle
;
1451 cfq_mark_cfqq_idle_window(cfqq
);
1452 cfq_mark_cfqq_prio_changed(cfqq
);
1453 cfq_init_prio_data(cfqq
);
1457 kmem_cache_free(cfq_pool
, new_cfqq
);
1459 atomic_inc(&cfqq
->ref
);
1461 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1466 * Setup general io context and cfq io context. There can be several cfq
1467 * io contexts per general io context, if this process is doing io to more
1468 * than one device managed by cfq. Note that caller is holding a reference to
1469 * cfqq, so we don't need to worry about it disappearing
1471 static struct cfq_io_context
*
1472 cfq_get_io_context(struct cfq_data
*cfqd
, pid_t pid
, gfp_t gfp_mask
)
1474 struct io_context
*ioc
= NULL
;
1475 struct cfq_io_context
*cic
;
1477 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1479 ioc
= get_io_context(gfp_mask
);
1484 if ((cic
= ioc
->cic
) == NULL
) {
1485 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1491 * manually increment generic io_context usage count, it
1492 * cannot go away since we are already holding one ref to it
1496 read_lock(&cfq_exit_lock
);
1497 ioc
->set_ioprio
= cfq_ioc_set_ioprio
;
1499 list_add(&cic
->queue_list
, &cfqd
->cic_list
);
1500 read_unlock(&cfq_exit_lock
);
1502 struct cfq_io_context
*__cic
;
1505 * the first cic on the list is actually the head itself
1507 if (cic
->key
== cfqd
)
1510 if (unlikely(!cic
->key
)) {
1511 read_lock(&cfq_exit_lock
);
1512 if (list_empty(&cic
->list
))
1515 ioc
->cic
= list_entry(cic
->list
.next
,
1516 struct cfq_io_context
,
1518 read_unlock(&cfq_exit_lock
);
1519 kmem_cache_free(cfq_ioc_pool
, cic
);
1520 atomic_dec(&ioc_count
);
1525 * cic exists, check if we already are there. linear search
1526 * should be ok here, the list will usually not be more than
1527 * 1 or a few entries long
1529 list_for_each_entry(__cic
, &cic
->list
, list
) {
1531 * this process is already holding a reference to
1532 * this queue, so no need to get one more
1534 if (__cic
->key
== cfqd
) {
1538 if (unlikely(!__cic
->key
)) {
1539 read_lock(&cfq_exit_lock
);
1540 list_del(&__cic
->list
);
1541 read_unlock(&cfq_exit_lock
);
1542 kmem_cache_free(cfq_ioc_pool
, __cic
);
1543 atomic_dec(&ioc_count
);
1549 * nope, process doesn't have a cic assoicated with this
1550 * cfqq yet. get a new one and add to list
1552 __cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1558 read_lock(&cfq_exit_lock
);
1559 list_add(&__cic
->list
, &cic
->list
);
1560 list_add(&__cic
->queue_list
, &cfqd
->cic_list
);
1561 read_unlock(&cfq_exit_lock
);
1568 put_io_context(ioc
);
1573 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1575 unsigned long elapsed
, ttime
;
1578 * if this context already has stuff queued, thinktime is from
1579 * last queue not last end
1582 if (time_after(cic
->last_end_request
, cic
->last_queue
))
1583 elapsed
= jiffies
- cic
->last_end_request
;
1585 elapsed
= jiffies
- cic
->last_queue
;
1587 elapsed
= jiffies
- cic
->last_end_request
;
1590 ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1592 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1593 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1594 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1597 #define sample_valid(samples) ((samples) > 80)
1600 * Disable idle window if the process thinks too long or seeks so much that
1604 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1605 struct cfq_io_context
*cic
)
1607 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1609 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
)
1611 else if (sample_valid(cic
->ttime_samples
)) {
1612 if (cic
->ttime_mean
> cfqd
->cfq_slice_idle
)
1619 cfq_mark_cfqq_idle_window(cfqq
);
1621 cfq_clear_cfqq_idle_window(cfqq
);
1626 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1627 * no or if we aren't sure, a 1 will cause a preempt.
1630 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1633 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
1635 if (cfq_class_idle(new_cfqq
))
1641 if (cfq_class_idle(cfqq
))
1643 if (!cfq_cfqq_wait_request(new_cfqq
))
1646 * if it doesn't have slice left, forget it
1648 if (new_cfqq
->slice_left
< cfqd
->cfq_slice_idle
)
1650 if (cfq_crq_is_sync(crq
) && !cfq_cfqq_sync(cfqq
))
1657 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1658 * let it have half of its nominal slice.
1660 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1662 struct cfq_queue
*__cfqq
, *next
;
1664 list_for_each_entry_safe(__cfqq
, next
, &cfqd
->cur_rr
, cfq_list
)
1665 cfq_resort_rr_list(__cfqq
, 1);
1667 if (!cfqq
->slice_left
)
1668 cfqq
->slice_left
= cfq_prio_to_slice(cfqd
, cfqq
) / 2;
1670 cfqq
->slice_end
= cfqq
->slice_left
+ jiffies
;
1671 __cfq_slice_expired(cfqd
, cfqq
, 1);
1672 __cfq_set_active_queue(cfqd
, cfqq
);
1676 * should really be a ll_rw_blk.c helper
1678 static void cfq_start_queueing(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1680 request_queue_t
*q
= cfqd
->queue
;
1682 if (!blk_queue_plugged(q
))
1685 __generic_unplug_device(q
);
1689 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1690 * something we should do about it
1693 cfq_crq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1696 struct cfq_io_context
*cic
;
1698 cfqq
->next_crq
= cfq_choose_req(cfqd
, cfqq
->next_crq
, crq
);
1701 * we never wait for an async request and we don't allow preemption
1702 * of an async request. so just return early
1704 if (!cfq_crq_is_sync(crq
))
1707 cic
= crq
->io_context
;
1709 cfq_update_io_thinktime(cfqd
, cic
);
1710 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1712 cic
->last_queue
= jiffies
;
1714 if (cfqq
== cfqd
->active_queue
) {
1716 * if we are waiting for a request for this queue, let it rip
1717 * immediately and flag that we must not expire this queue
1720 if (cfq_cfqq_wait_request(cfqq
)) {
1721 cfq_mark_cfqq_must_dispatch(cfqq
);
1722 del_timer(&cfqd
->idle_slice_timer
);
1723 cfq_start_queueing(cfqd
, cfqq
);
1725 } else if (cfq_should_preempt(cfqd
, cfqq
, crq
)) {
1727 * not the active queue - expire current slice if it is
1728 * idle and has expired it's mean thinktime or this new queue
1729 * has some old slice time left and is of higher priority
1731 cfq_preempt_queue(cfqd
, cfqq
);
1732 cfq_mark_cfqq_must_dispatch(cfqq
);
1733 cfq_start_queueing(cfqd
, cfqq
);
1737 static void cfq_insert_request(request_queue_t
*q
, struct request
*rq
)
1739 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1740 struct cfq_rq
*crq
= RQ_DATA(rq
);
1741 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1743 cfq_init_prio_data(cfqq
);
1745 cfq_add_crq_rb(crq
);
1747 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1749 if (rq_mergeable(rq
))
1750 cfq_add_crq_hash(cfqd
, crq
);
1752 cfq_crq_enqueued(cfqd
, cfqq
, crq
);
1755 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1757 struct cfq_rq
*crq
= RQ_DATA(rq
);
1758 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1759 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1760 const int sync
= cfq_crq_is_sync(crq
);
1765 WARN_ON(!cfqd
->rq_in_driver
);
1766 WARN_ON(!cfqq
->on_dispatch
[sync
]);
1767 cfqd
->rq_in_driver
--;
1768 cfqq
->on_dispatch
[sync
]--;
1770 if (!cfq_class_idle(cfqq
))
1771 cfqd
->last_end_request
= now
;
1773 if (!cfq_cfqq_dispatched(cfqq
)) {
1774 if (cfq_cfqq_on_rr(cfqq
)) {
1775 cfqq
->service_last
= now
;
1776 cfq_resort_rr_list(cfqq
, 0);
1778 cfq_schedule_dispatch(cfqd
);
1781 if (cfq_crq_is_sync(crq
))
1782 crq
->io_context
->last_end_request
= now
;
1785 static struct request
*
1786 cfq_former_request(request_queue_t
*q
, struct request
*rq
)
1788 struct cfq_rq
*crq
= RQ_DATA(rq
);
1789 struct rb_node
*rbprev
= rb_prev(&crq
->rb_node
);
1792 return rb_entry_crq(rbprev
)->request
;
1797 static struct request
*
1798 cfq_latter_request(request_queue_t
*q
, struct request
*rq
)
1800 struct cfq_rq
*crq
= RQ_DATA(rq
);
1801 struct rb_node
*rbnext
= rb_next(&crq
->rb_node
);
1804 return rb_entry_crq(rbnext
)->request
;
1810 * we temporarily boost lower priority queues if they are holding fs exclusive
1811 * resources. they are boosted to normal prio (CLASS_BE/4)
1813 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1815 const int ioprio_class
= cfqq
->ioprio_class
;
1816 const int ioprio
= cfqq
->ioprio
;
1818 if (has_fs_excl()) {
1820 * boost idle prio on transactions that would lock out other
1821 * users of the filesystem
1823 if (cfq_class_idle(cfqq
))
1824 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1825 if (cfqq
->ioprio
> IOPRIO_NORM
)
1826 cfqq
->ioprio
= IOPRIO_NORM
;
1829 * check if we need to unboost the queue
1831 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1832 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1833 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1834 cfqq
->ioprio
= cfqq
->org_ioprio
;
1838 * refile between round-robin lists if we moved the priority class
1840 if ((ioprio_class
!= cfqq
->ioprio_class
|| ioprio
!= cfqq
->ioprio
) &&
1841 cfq_cfqq_on_rr(cfqq
))
1842 cfq_resort_rr_list(cfqq
, 0);
1845 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
)
1847 if (rw
== READ
|| process_sync(task
))
1850 return CFQ_KEY_ASYNC
;
1854 __cfq_may_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1855 struct task_struct
*task
, int rw
)
1858 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1859 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1860 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1861 return ELV_MQUEUE_MUST
;
1864 return ELV_MQUEUE_MAY
;
1866 if (!cfqq
|| task
->flags
& PF_MEMALLOC
)
1867 return ELV_MQUEUE_MAY
;
1868 if (!cfqq
->allocated
[rw
] || cfq_cfqq_must_alloc(cfqq
)) {
1869 if (cfq_cfqq_wait_request(cfqq
))
1870 return ELV_MQUEUE_MUST
;
1873 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1874 * can quickly flood the queue with writes from a single task
1876 if (rw
== READ
|| !cfq_cfqq_must_alloc_slice(cfqq
)) {
1877 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1878 return ELV_MQUEUE_MUST
;
1881 return ELV_MQUEUE_MAY
;
1883 if (cfq_class_idle(cfqq
))
1884 return ELV_MQUEUE_NO
;
1885 if (cfqq
->allocated
[rw
] >= cfqd
->max_queued
) {
1886 struct io_context
*ioc
= get_io_context(GFP_ATOMIC
);
1887 int ret
= ELV_MQUEUE_NO
;
1889 if (ioc
&& ioc
->nr_batch_requests
)
1890 ret
= ELV_MQUEUE_MAY
;
1892 put_io_context(ioc
);
1896 return ELV_MQUEUE_MAY
;
1900 static int cfq_may_queue(request_queue_t
*q
, int rw
, struct bio
*bio
)
1902 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1903 struct task_struct
*tsk
= current
;
1904 struct cfq_queue
*cfqq
;
1907 * don't force setup of a queue from here, as a call to may_queue
1908 * does not necessarily imply that a request actually will be queued.
1909 * so just lookup a possibly existing queue, or return 'may queue'
1912 cfqq
= cfq_find_cfq_hash(cfqd
, cfq_queue_pid(tsk
, rw
), tsk
->ioprio
);
1914 cfq_init_prio_data(cfqq
);
1915 cfq_prio_boost(cfqq
);
1917 return __cfq_may_queue(cfqd
, cfqq
, tsk
, rw
);
1920 return ELV_MQUEUE_MAY
;
1923 static void cfq_check_waiters(request_queue_t
*q
, struct cfq_queue
*cfqq
)
1925 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1926 struct request_list
*rl
= &q
->rq
;
1928 if (cfqq
->allocated
[READ
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1930 if (waitqueue_active(&rl
->wait
[READ
]))
1931 wake_up(&rl
->wait
[READ
]);
1934 if (cfqq
->allocated
[WRITE
] <= cfqd
->max_queued
|| cfqd
->rq_starved
) {
1936 if (waitqueue_active(&rl
->wait
[WRITE
]))
1937 wake_up(&rl
->wait
[WRITE
]);
1942 * queue lock held here
1944 static void cfq_put_request(request_queue_t
*q
, struct request
*rq
)
1946 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1947 struct cfq_rq
*crq
= RQ_DATA(rq
);
1950 struct cfq_queue
*cfqq
= crq
->cfq_queue
;
1951 const int rw
= rq_data_dir(rq
);
1953 BUG_ON(!cfqq
->allocated
[rw
]);
1954 cfqq
->allocated
[rw
]--;
1956 put_io_context(crq
->io_context
->ioc
);
1958 mempool_free(crq
, cfqd
->crq_pool
);
1959 rq
->elevator_private
= NULL
;
1961 cfq_check_waiters(q
, cfqq
);
1962 cfq_put_queue(cfqq
);
1967 * Allocate cfq data structures associated with this request.
1970 cfq_set_request(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
,
1973 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1974 struct task_struct
*tsk
= current
;
1975 struct cfq_io_context
*cic
;
1976 const int rw
= rq_data_dir(rq
);
1977 pid_t key
= cfq_queue_pid(tsk
, rw
);
1978 struct cfq_queue
*cfqq
;
1980 unsigned long flags
;
1981 int is_sync
= key
!= CFQ_KEY_ASYNC
;
1983 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1985 cic
= cfq_get_io_context(cfqd
, key
, gfp_mask
);
1987 spin_lock_irqsave(q
->queue_lock
, flags
);
1992 if (!cic
->cfqq
[is_sync
]) {
1993 cfqq
= cfq_get_queue(cfqd
, key
, tsk
, gfp_mask
);
1997 cic
->cfqq
[is_sync
] = cfqq
;
1999 cfqq
= cic
->cfqq
[is_sync
];
2001 cfqq
->allocated
[rw
]++;
2002 cfq_clear_cfqq_must_alloc(cfqq
);
2003 cfqd
->rq_starved
= 0;
2004 atomic_inc(&cfqq
->ref
);
2005 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2007 crq
= mempool_alloc(cfqd
->crq_pool
, gfp_mask
);
2009 RB_CLEAR(&crq
->rb_node
);
2012 INIT_HLIST_NODE(&crq
->hash
);
2013 crq
->cfq_queue
= cfqq
;
2014 crq
->io_context
= cic
;
2017 cfq_mark_crq_is_sync(crq
);
2019 cfq_clear_crq_is_sync(crq
);
2021 rq
->elevator_private
= crq
;
2025 spin_lock_irqsave(q
->queue_lock
, flags
);
2026 cfqq
->allocated
[rw
]--;
2027 if (!(cfqq
->allocated
[0] + cfqq
->allocated
[1]))
2028 cfq_mark_cfqq_must_alloc(cfqq
);
2029 cfq_put_queue(cfqq
);
2032 put_io_context(cic
->ioc
);
2034 * mark us rq allocation starved. we need to kickstart the process
2035 * ourselves if there are no pending requests that can do it for us.
2036 * that would be an extremely rare OOM situation
2038 cfqd
->rq_starved
= 1;
2039 cfq_schedule_dispatch(cfqd
);
2040 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2044 static void cfq_kick_queue(void *data
)
2046 request_queue_t
*q
= data
;
2047 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
2048 unsigned long flags
;
2050 spin_lock_irqsave(q
->queue_lock
, flags
);
2052 if (cfqd
->rq_starved
) {
2053 struct request_list
*rl
= &q
->rq
;
2056 * we aren't guaranteed to get a request after this, but we
2057 * have to be opportunistic
2060 if (waitqueue_active(&rl
->wait
[READ
]))
2061 wake_up(&rl
->wait
[READ
]);
2062 if (waitqueue_active(&rl
->wait
[WRITE
]))
2063 wake_up(&rl
->wait
[WRITE
]);
2068 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2072 * Timer running if the active_queue is currently idling inside its time slice
2074 static void cfq_idle_slice_timer(unsigned long data
)
2076 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2077 struct cfq_queue
*cfqq
;
2078 unsigned long flags
;
2080 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2082 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
2083 unsigned long now
= jiffies
;
2088 if (time_after(now
, cfqq
->slice_end
))
2092 * only expire and reinvoke request handler, if there are
2093 * other queues with pending requests
2095 if (!cfqd
->busy_queues
) {
2096 cfqd
->idle_slice_timer
.expires
= min(now
+ cfqd
->cfq_slice_idle
, cfqq
->slice_end
);
2097 add_timer(&cfqd
->idle_slice_timer
);
2102 * not expired and it has a request pending, let it dispatch
2104 if (!RB_EMPTY(&cfqq
->sort_list
)) {
2105 cfq_mark_cfqq_must_dispatch(cfqq
);
2110 cfq_slice_expired(cfqd
, 0);
2112 cfq_schedule_dispatch(cfqd
);
2114 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2118 * Timer running if an idle class queue is waiting for service
2120 static void cfq_idle_class_timer(unsigned long data
)
2122 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
2123 unsigned long flags
, end
;
2125 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
2128 * race with a non-idle queue, reset timer
2130 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
2131 if (!time_after_eq(jiffies
, end
)) {
2132 cfqd
->idle_class_timer
.expires
= end
;
2133 add_timer(&cfqd
->idle_class_timer
);
2135 cfq_schedule_dispatch(cfqd
);
2137 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2140 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
2142 del_timer_sync(&cfqd
->idle_slice_timer
);
2143 del_timer_sync(&cfqd
->idle_class_timer
);
2144 blk_sync_queue(cfqd
->queue
);
2147 static void cfq_put_cfqd(struct cfq_data
*cfqd
)
2149 if (!atomic_dec_and_test(&cfqd
->ref
))
2152 cfq_shutdown_timer_wq(cfqd
);
2154 mempool_destroy(cfqd
->crq_pool
);
2155 kfree(cfqd
->crq_hash
);
2156 kfree(cfqd
->cfq_hash
);
2160 static void cfq_exit_queue(elevator_t
*e
)
2162 struct cfq_data
*cfqd
= e
->elevator_data
;
2163 request_queue_t
*q
= cfqd
->queue
;
2165 cfq_shutdown_timer_wq(cfqd
);
2166 write_lock(&cfq_exit_lock
);
2167 spin_lock_irq(q
->queue_lock
);
2168 if (cfqd
->active_queue
)
2169 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
2170 while(!list_empty(&cfqd
->cic_list
)) {
2171 struct cfq_io_context
*cic
= list_entry(cfqd
->cic_list
.next
,
2172 struct cfq_io_context
,
2174 if (cic
->cfqq
[ASYNC
]) {
2175 cfq_put_queue(cic
->cfqq
[ASYNC
]);
2176 cic
->cfqq
[ASYNC
] = NULL
;
2178 if (cic
->cfqq
[SYNC
]) {
2179 cfq_put_queue(cic
->cfqq
[SYNC
]);
2180 cic
->cfqq
[SYNC
] = NULL
;
2183 list_del_init(&cic
->queue_list
);
2185 spin_unlock_irq(q
->queue_lock
);
2186 write_unlock(&cfq_exit_lock
);
2190 static int cfq_init_queue(request_queue_t
*q
, elevator_t
*e
)
2192 struct cfq_data
*cfqd
;
2195 cfqd
= kmalloc(sizeof(*cfqd
), GFP_KERNEL
);
2199 memset(cfqd
, 0, sizeof(*cfqd
));
2201 for (i
= 0; i
< CFQ_PRIO_LISTS
; i
++)
2202 INIT_LIST_HEAD(&cfqd
->rr_list
[i
]);
2204 INIT_LIST_HEAD(&cfqd
->busy_rr
);
2205 INIT_LIST_HEAD(&cfqd
->cur_rr
);
2206 INIT_LIST_HEAD(&cfqd
->idle_rr
);
2207 INIT_LIST_HEAD(&cfqd
->empty_list
);
2208 INIT_LIST_HEAD(&cfqd
->cic_list
);
2210 cfqd
->crq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_MHASH_ENTRIES
, GFP_KERNEL
);
2211 if (!cfqd
->crq_hash
)
2214 cfqd
->cfq_hash
= kmalloc(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
);
2215 if (!cfqd
->cfq_hash
)
2218 cfqd
->crq_pool
= mempool_create(BLKDEV_MIN_RQ
, mempool_alloc_slab
, mempool_free_slab
, crq_pool
);
2219 if (!cfqd
->crq_pool
)
2222 for (i
= 0; i
< CFQ_MHASH_ENTRIES
; i
++)
2223 INIT_HLIST_HEAD(&cfqd
->crq_hash
[i
]);
2224 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
2225 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2227 e
->elevator_data
= cfqd
;
2231 cfqd
->max_queued
= q
->nr_requests
/ 4;
2232 q
->nr_batching
= cfq_queued
;
2234 init_timer(&cfqd
->idle_slice_timer
);
2235 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2236 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2238 init_timer(&cfqd
->idle_class_timer
);
2239 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2240 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2242 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
, q
);
2244 atomic_set(&cfqd
->ref
, 1);
2246 cfqd
->cfq_queued
= cfq_queued
;
2247 cfqd
->cfq_quantum
= cfq_quantum
;
2248 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2249 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2250 cfqd
->cfq_back_max
= cfq_back_max
;
2251 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2252 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2253 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2254 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2255 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2256 cfqd
->cfq_max_depth
= cfq_max_depth
;
2260 kfree(cfqd
->cfq_hash
);
2262 kfree(cfqd
->crq_hash
);
2268 static void cfq_slab_kill(void)
2271 kmem_cache_destroy(crq_pool
);
2273 kmem_cache_destroy(cfq_pool
);
2275 kmem_cache_destroy(cfq_ioc_pool
);
2278 static int __init
cfq_slab_setup(void)
2280 crq_pool
= kmem_cache_create("crq_pool", sizeof(struct cfq_rq
), 0, 0,
2285 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2290 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2291 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2302 * sysfs parts below -->
2304 struct cfq_fs_entry
{
2305 struct attribute attr
;
2306 ssize_t (*show
)(struct cfq_data
*, char *);
2307 ssize_t (*store
)(struct cfq_data
*, const char *, size_t);
2311 cfq_var_show(unsigned int var
, char *page
)
2313 return sprintf(page
, "%d\n", var
);
2317 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2319 char *p
= (char *) page
;
2321 *var
= simple_strtoul(p
, &p
, 10);
2325 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2326 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2328 unsigned int __data = __VAR; \
2330 __data = jiffies_to_msecs(__data); \
2331 return cfq_var_show(__data, (page)); \
2333 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2334 SHOW_FUNCTION(cfq_queued_show
, cfqd
->cfq_queued
, 0);
2335 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2336 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2337 SHOW_FUNCTION(cfq_back_max_show
, cfqd
->cfq_back_max
, 0);
2338 SHOW_FUNCTION(cfq_back_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2339 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2340 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2341 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2342 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2343 SHOW_FUNCTION(cfq_max_depth_show
, cfqd
->cfq_max_depth
, 0);
2344 #undef SHOW_FUNCTION
2346 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2347 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2349 unsigned int __data; \
2350 int ret = cfq_var_store(&__data, (page), count); \
2351 if (__data < (MIN)) \
2353 else if (__data > (MAX)) \
2356 *(__PTR) = msecs_to_jiffies(__data); \
2358 *(__PTR) = __data; \
2361 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2362 STORE_FUNCTION(cfq_queued_store
, &cfqd
->cfq_queued
, 1, UINT_MAX
, 0);
2363 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2364 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2365 STORE_FUNCTION(cfq_back_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2366 STORE_FUNCTION(cfq_back_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2367 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2368 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2369 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2370 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2371 STORE_FUNCTION(cfq_max_depth_store
, &cfqd
->cfq_max_depth
, 1, UINT_MAX
, 0);
2372 #undef STORE_FUNCTION
2374 static struct cfq_fs_entry cfq_quantum_entry
= {
2375 .attr
= {.name
= "quantum", .mode
= S_IRUGO
| S_IWUSR
},
2376 .show
= cfq_quantum_show
,
2377 .store
= cfq_quantum_store
,
2379 static struct cfq_fs_entry cfq_queued_entry
= {
2380 .attr
= {.name
= "queued", .mode
= S_IRUGO
| S_IWUSR
},
2381 .show
= cfq_queued_show
,
2382 .store
= cfq_queued_store
,
2384 static struct cfq_fs_entry cfq_fifo_expire_sync_entry
= {
2385 .attr
= {.name
= "fifo_expire_sync", .mode
= S_IRUGO
| S_IWUSR
},
2386 .show
= cfq_fifo_expire_sync_show
,
2387 .store
= cfq_fifo_expire_sync_store
,
2389 static struct cfq_fs_entry cfq_fifo_expire_async_entry
= {
2390 .attr
= {.name
= "fifo_expire_async", .mode
= S_IRUGO
| S_IWUSR
},
2391 .show
= cfq_fifo_expire_async_show
,
2392 .store
= cfq_fifo_expire_async_store
,
2394 static struct cfq_fs_entry cfq_back_max_entry
= {
2395 .attr
= {.name
= "back_seek_max", .mode
= S_IRUGO
| S_IWUSR
},
2396 .show
= cfq_back_max_show
,
2397 .store
= cfq_back_max_store
,
2399 static struct cfq_fs_entry cfq_back_penalty_entry
= {
2400 .attr
= {.name
= "back_seek_penalty", .mode
= S_IRUGO
| S_IWUSR
},
2401 .show
= cfq_back_penalty_show
,
2402 .store
= cfq_back_penalty_store
,
2404 static struct cfq_fs_entry cfq_slice_sync_entry
= {
2405 .attr
= {.name
= "slice_sync", .mode
= S_IRUGO
| S_IWUSR
},
2406 .show
= cfq_slice_sync_show
,
2407 .store
= cfq_slice_sync_store
,
2409 static struct cfq_fs_entry cfq_slice_async_entry
= {
2410 .attr
= {.name
= "slice_async", .mode
= S_IRUGO
| S_IWUSR
},
2411 .show
= cfq_slice_async_show
,
2412 .store
= cfq_slice_async_store
,
2414 static struct cfq_fs_entry cfq_slice_async_rq_entry
= {
2415 .attr
= {.name
= "slice_async_rq", .mode
= S_IRUGO
| S_IWUSR
},
2416 .show
= cfq_slice_async_rq_show
,
2417 .store
= cfq_slice_async_rq_store
,
2419 static struct cfq_fs_entry cfq_slice_idle_entry
= {
2420 .attr
= {.name
= "slice_idle", .mode
= S_IRUGO
| S_IWUSR
},
2421 .show
= cfq_slice_idle_show
,
2422 .store
= cfq_slice_idle_store
,
2424 static struct cfq_fs_entry cfq_max_depth_entry
= {
2425 .attr
= {.name
= "max_depth", .mode
= S_IRUGO
| S_IWUSR
},
2426 .show
= cfq_max_depth_show
,
2427 .store
= cfq_max_depth_store
,
2430 static struct attribute
*default_attrs
[] = {
2431 &cfq_quantum_entry
.attr
,
2432 &cfq_queued_entry
.attr
,
2433 &cfq_fifo_expire_sync_entry
.attr
,
2434 &cfq_fifo_expire_async_entry
.attr
,
2435 &cfq_back_max_entry
.attr
,
2436 &cfq_back_penalty_entry
.attr
,
2437 &cfq_slice_sync_entry
.attr
,
2438 &cfq_slice_async_entry
.attr
,
2439 &cfq_slice_async_rq_entry
.attr
,
2440 &cfq_slice_idle_entry
.attr
,
2441 &cfq_max_depth_entry
.attr
,
2445 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2448 cfq_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2450 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2451 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2456 return entry
->show(e
->elevator_data
, page
);
2460 cfq_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2461 const char *page
, size_t length
)
2463 elevator_t
*e
= container_of(kobj
, elevator_t
, kobj
);
2464 struct cfq_fs_entry
*entry
= to_cfq(attr
);
2469 return entry
->store(e
->elevator_data
, page
, length
);
2472 static struct sysfs_ops cfq_sysfs_ops
= {
2473 .show
= cfq_attr_show
,
2474 .store
= cfq_attr_store
,
2477 static struct kobj_type cfq_ktype
= {
2478 .sysfs_ops
= &cfq_sysfs_ops
,
2479 .default_attrs
= default_attrs
,
2482 static struct elevator_type iosched_cfq
= {
2484 .elevator_merge_fn
= cfq_merge
,
2485 .elevator_merged_fn
= cfq_merged_request
,
2486 .elevator_merge_req_fn
= cfq_merged_requests
,
2487 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2488 .elevator_add_req_fn
= cfq_insert_request
,
2489 .elevator_activate_req_fn
= cfq_activate_request
,
2490 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2491 .elevator_queue_empty_fn
= cfq_queue_empty
,
2492 .elevator_completed_req_fn
= cfq_completed_request
,
2493 .elevator_former_req_fn
= cfq_former_request
,
2494 .elevator_latter_req_fn
= cfq_latter_request
,
2495 .elevator_set_req_fn
= cfq_set_request
,
2496 .elevator_put_req_fn
= cfq_put_request
,
2497 .elevator_may_queue_fn
= cfq_may_queue
,
2498 .elevator_init_fn
= cfq_init_queue
,
2499 .elevator_exit_fn
= cfq_exit_queue
,
2502 .elevator_ktype
= &cfq_ktype
,
2503 .elevator_name
= "cfq",
2504 .elevator_owner
= THIS_MODULE
,
2507 static int __init
cfq_init(void)
2512 * could be 0 on HZ < 1000 setups
2514 if (!cfq_slice_async
)
2515 cfq_slice_async
= 1;
2516 if (!cfq_slice_idle
)
2519 if (cfq_slab_setup())
2522 ret
= elv_register(&iosched_cfq
);
2529 static void __exit
cfq_exit(void)
2531 DECLARE_COMPLETION(all_gone
);
2532 elv_unregister(&iosched_cfq
);
2533 ioc_gone
= &all_gone
;
2535 if (atomic_read(&ioc_count
))
2541 module_init(cfq_init
);
2542 module_exit(cfq_exit
);
2544 MODULE_AUTHOR("Jens Axboe");
2545 MODULE_LICENSE("GPL");
2546 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");