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@kernel.dk>
9 #include <linux/module.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/hash.h>
13 #include <linux/rbtree.h>
14 #include <linux/ioprio.h>
19 static const int cfq_quantum
= 4; /* max queue in one round of service */
20 static const int cfq_fifo_expire
[2] = { HZ
/ 4, HZ
/ 8 };
21 static const int cfq_back_max
= 16 * 1024; /* maximum backwards seek, in KiB */
22 static const int cfq_back_penalty
= 2; /* penalty of a backwards seek */
24 static const int cfq_slice_sync
= HZ
/ 10;
25 static int cfq_slice_async
= HZ
/ 25;
26 static const int cfq_slice_async_rq
= 2;
27 static int cfq_slice_idle
= HZ
/ 125;
30 * grace period before allowing idle class to get disk access
32 #define CFQ_IDLE_GRACE (HZ / 10)
35 * below this threshold, we consider thinktime immediate
37 #define CFQ_MIN_TT (2)
39 #define CFQ_SLICE_SCALE (5)
41 #define CFQ_KEY_ASYNC (0)
44 * for the hash of cfqq inside the cfqd
46 #define CFQ_QHASH_SHIFT 6
47 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
49 #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
50 #define RQ_CFQQ(rq) ((rq)->elevator_private2)
52 static struct kmem_cache
*cfq_pool
;
53 static struct kmem_cache
*cfq_ioc_pool
;
55 static DEFINE_PER_CPU(unsigned long, ioc_count
);
56 static struct completion
*ioc_gone
;
58 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
59 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
60 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
65 #define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
67 #define sample_valid(samples) ((samples) > 80)
70 * Most of our rbtree usage is for sorting with min extraction, so
71 * if we cache the leftmost node we don't have to walk down the tree
72 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
73 * move this into the elevator for the rq sorting as well.
79 #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
82 * Per block device queue structure
85 request_queue_t
*queue
;
88 * rr list of queues with requests and the count of them
90 struct cfq_rb_root service_tree
;
91 unsigned int busy_queues
;
96 struct hlist_head
*cfq_hash
;
102 * idle window management
104 struct timer_list idle_slice_timer
;
105 struct work_struct unplug_work
;
107 struct cfq_queue
*active_queue
;
108 struct cfq_io_context
*active_cic
;
110 struct timer_list idle_class_timer
;
112 sector_t last_position
;
113 unsigned long last_end_request
;
116 * tunables, see top of file
118 unsigned int cfq_quantum
;
119 unsigned int cfq_fifo_expire
[2];
120 unsigned int cfq_back_penalty
;
121 unsigned int cfq_back_max
;
122 unsigned int cfq_slice
[2];
123 unsigned int cfq_slice_async_rq
;
124 unsigned int cfq_slice_idle
;
126 struct list_head cic_list
;
128 sector_t new_seek_mean
;
133 * Per process-grouping structure
136 /* reference count */
138 /* parent cfq_data */
139 struct cfq_data
*cfqd
;
140 /* cfqq lookup hash */
141 struct hlist_node cfq_hash
;
144 /* service_tree member */
145 struct rb_node rb_node
;
146 /* service_tree key */
147 unsigned long rb_key
;
148 /* sorted list of pending requests */
149 struct rb_root sort_list
;
150 /* if fifo isn't expired, next request to serve */
151 struct request
*next_rq
;
152 /* requests queued in sort_list */
154 /* currently allocated requests */
156 /* pending metadata requests */
158 /* fifo list of requests in sort_list */
159 struct list_head fifo
;
161 unsigned long slice_end
;
164 /* number of requests that are on the dispatch list or inside driver */
167 /* io prio of this group */
168 unsigned short ioprio
, org_ioprio
;
169 unsigned short ioprio_class
, org_ioprio_class
;
171 /* various state flags, see below */
174 sector_t last_request_pos
;
177 enum cfqq_state_flags
{
178 CFQ_CFQQ_FLAG_on_rr
= 0, /* on round-robin busy list */
179 CFQ_CFQQ_FLAG_wait_request
, /* waiting for a request */
180 CFQ_CFQQ_FLAG_must_alloc
, /* must be allowed rq alloc */
181 CFQ_CFQQ_FLAG_must_alloc_slice
, /* per-slice must_alloc flag */
182 CFQ_CFQQ_FLAG_must_dispatch
, /* must dispatch, even if expired */
183 CFQ_CFQQ_FLAG_fifo_expire
, /* FIFO checked in this slice */
184 CFQ_CFQQ_FLAG_idle_window
, /* slice idling enabled */
185 CFQ_CFQQ_FLAG_prio_changed
, /* task priority has changed */
186 CFQ_CFQQ_FLAG_queue_new
, /* queue never been serviced */
187 CFQ_CFQQ_FLAG_slice_new
, /* no requests dispatched in slice */
190 #define CFQ_CFQQ_FNS(name) \
191 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
193 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
195 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
197 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
199 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
201 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
205 CFQ_CFQQ_FNS(wait_request
);
206 CFQ_CFQQ_FNS(must_alloc
);
207 CFQ_CFQQ_FNS(must_alloc_slice
);
208 CFQ_CFQQ_FNS(must_dispatch
);
209 CFQ_CFQQ_FNS(fifo_expire
);
210 CFQ_CFQQ_FNS(idle_window
);
211 CFQ_CFQQ_FNS(prio_changed
);
212 CFQ_CFQQ_FNS(queue_new
);
213 CFQ_CFQQ_FNS(slice_new
);
216 static struct cfq_queue
*cfq_find_cfq_hash(struct cfq_data
*, unsigned int, unsigned short);
217 static void cfq_dispatch_insert(request_queue_t
*, struct request
*);
218 static struct cfq_queue
*cfq_get_queue(struct cfq_data
*, unsigned int, struct task_struct
*, gfp_t
);
221 * scheduler run of queue, if there are requests pending and no one in the
222 * driver that will restart queueing
224 static inline void cfq_schedule_dispatch(struct cfq_data
*cfqd
)
226 if (cfqd
->busy_queues
)
227 kblockd_schedule_work(&cfqd
->unplug_work
);
230 static int cfq_queue_empty(request_queue_t
*q
)
232 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
234 return !cfqd
->busy_queues
;
237 static inline pid_t
cfq_queue_pid(struct task_struct
*task
, int rw
, int is_sync
)
240 * Use the per-process queue, for read requests and syncronous writes
242 if (!(rw
& REQ_RW
) || is_sync
)
245 return CFQ_KEY_ASYNC
;
249 * Scale schedule slice based on io priority. Use the sync time slice only
250 * if a queue is marked sync and has sync io queued. A sync queue with async
251 * io only, should not get full sync slice length.
253 static inline int cfq_prio_slice(struct cfq_data
*cfqd
, int sync
,
256 const int base_slice
= cfqd
->cfq_slice
[sync
];
258 WARN_ON(prio
>= IOPRIO_BE_NR
);
260 return base_slice
+ (base_slice
/CFQ_SLICE_SCALE
* (4 - prio
));
264 cfq_prio_to_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
266 return cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
);
270 cfq_set_prio_slice(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
272 cfqq
->slice_end
= cfq_prio_to_slice(cfqd
, cfqq
) + jiffies
;
276 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
277 * isn't valid until the first request from the dispatch is activated
278 * and the slice time set.
280 static inline int cfq_slice_used(struct cfq_queue
*cfqq
)
282 if (cfq_cfqq_slice_new(cfqq
))
284 if (time_before(jiffies
, cfqq
->slice_end
))
291 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
292 * We choose the request that is closest to the head right now. Distance
293 * behind the head is penalized and only allowed to a certain extent.
295 static struct request
*
296 cfq_choose_req(struct cfq_data
*cfqd
, struct request
*rq1
, struct request
*rq2
)
298 sector_t last
, s1
, s2
, d1
= 0, d2
= 0;
299 unsigned long back_max
;
300 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
301 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
302 unsigned wrap
= 0; /* bit mask: requests behind the disk head? */
304 if (rq1
== NULL
|| rq1
== rq2
)
309 if (rq_is_sync(rq1
) && !rq_is_sync(rq2
))
311 else if (rq_is_sync(rq2
) && !rq_is_sync(rq1
))
313 if (rq_is_meta(rq1
) && !rq_is_meta(rq2
))
315 else if (rq_is_meta(rq2
) && !rq_is_meta(rq1
))
321 last
= cfqd
->last_position
;
324 * by definition, 1KiB is 2 sectors
326 back_max
= cfqd
->cfq_back_max
* 2;
329 * Strict one way elevator _except_ in the case where we allow
330 * short backward seeks which are biased as twice the cost of a
331 * similar forward seek.
335 else if (s1
+ back_max
>= last
)
336 d1
= (last
- s1
) * cfqd
->cfq_back_penalty
;
338 wrap
|= CFQ_RQ1_WRAP
;
342 else if (s2
+ back_max
>= last
)
343 d2
= (last
- s2
) * cfqd
->cfq_back_penalty
;
345 wrap
|= CFQ_RQ2_WRAP
;
347 /* Found required data */
350 * By doing switch() on the bit mask "wrap" we avoid having to
351 * check two variables for all permutations: --> faster!
354 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
370 case (CFQ_RQ1_WRAP
|CFQ_RQ2_WRAP
): /* both rqs wrapped */
373 * Since both rqs are wrapped,
374 * start with the one that's further behind head
375 * (--> only *one* back seek required),
376 * since back seek takes more time than forward.
386 * The below is leftmost cache rbtree addon
388 static struct rb_node
*cfq_rb_first(struct cfq_rb_root
*root
)
391 root
->left
= rb_first(&root
->rb
);
396 static void cfq_rb_erase(struct rb_node
*n
, struct cfq_rb_root
*root
)
401 rb_erase(n
, &root
->rb
);
406 * would be nice to take fifo expire time into account as well
408 static struct request
*
409 cfq_find_next_rq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
410 struct request
*last
)
412 struct rb_node
*rbnext
= rb_next(&last
->rb_node
);
413 struct rb_node
*rbprev
= rb_prev(&last
->rb_node
);
414 struct request
*next
= NULL
, *prev
= NULL
;
416 BUG_ON(RB_EMPTY_NODE(&last
->rb_node
));
419 prev
= rb_entry_rq(rbprev
);
422 next
= rb_entry_rq(rbnext
);
424 rbnext
= rb_first(&cfqq
->sort_list
);
425 if (rbnext
&& rbnext
!= &last
->rb_node
)
426 next
= rb_entry_rq(rbnext
);
429 return cfq_choose_req(cfqd
, next
, prev
);
432 static unsigned long cfq_slice_offset(struct cfq_data
*cfqd
,
433 struct cfq_queue
*cfqq
)
436 * just an approximation, should be ok.
438 return (cfqd
->busy_queues
- 1) * (cfq_prio_slice(cfqd
, 1, 0) -
439 cfq_prio_slice(cfqd
, cfq_cfqq_sync(cfqq
), cfqq
->ioprio
));
443 * The cfqd->service_tree holds all pending cfq_queue's that have
444 * requests waiting to be processed. It is sorted in the order that
445 * we will service the queues.
447 static void cfq_service_tree_add(struct cfq_data
*cfqd
,
448 struct cfq_queue
*cfqq
, int add_front
)
450 struct rb_node
**p
= &cfqd
->service_tree
.rb
.rb_node
;
451 struct rb_node
*parent
= NULL
;
452 unsigned long rb_key
;
456 rb_key
= cfq_slice_offset(cfqd
, cfqq
) + jiffies
;
457 rb_key
+= cfqq
->slice_resid
;
458 cfqq
->slice_resid
= 0;
462 if (!RB_EMPTY_NODE(&cfqq
->rb_node
)) {
464 * same position, nothing more to do
466 if (rb_key
== cfqq
->rb_key
)
469 cfq_rb_erase(&cfqq
->rb_node
, &cfqd
->service_tree
);
474 struct cfq_queue
*__cfqq
;
478 __cfqq
= rb_entry(parent
, struct cfq_queue
, rb_node
);
481 * sort RT queues first, we always want to give
482 * preference to them. IDLE queues goes to the back.
483 * after that, sort on the next service time.
485 if (cfq_class_rt(cfqq
) > cfq_class_rt(__cfqq
))
487 else if (cfq_class_rt(cfqq
) < cfq_class_rt(__cfqq
))
489 else if (cfq_class_idle(cfqq
) < cfq_class_idle(__cfqq
))
491 else if (cfq_class_idle(cfqq
) > cfq_class_idle(__cfqq
))
493 else if (rb_key
< __cfqq
->rb_key
)
498 if (n
== &(*p
)->rb_right
)
505 cfqd
->service_tree
.left
= &cfqq
->rb_node
;
507 cfqq
->rb_key
= rb_key
;
508 rb_link_node(&cfqq
->rb_node
, parent
, p
);
509 rb_insert_color(&cfqq
->rb_node
, &cfqd
->service_tree
.rb
);
513 * Update cfqq's position in the service tree.
515 static void cfq_resort_rr_list(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
518 * Resorting requires the cfqq to be on the RR list already.
520 if (cfq_cfqq_on_rr(cfqq
))
521 cfq_service_tree_add(cfqd
, cfqq
, 0);
525 * add to busy list of queues for service, trying to be fair in ordering
526 * the pending list according to last request service
529 cfq_add_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
531 BUG_ON(cfq_cfqq_on_rr(cfqq
));
532 cfq_mark_cfqq_on_rr(cfqq
);
535 cfq_resort_rr_list(cfqd
, cfqq
);
539 * Called when the cfqq no longer has requests pending, remove it from
543 cfq_del_cfqq_rr(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
545 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
546 cfq_clear_cfqq_on_rr(cfqq
);
548 if (!RB_EMPTY_NODE(&cfqq
->rb_node
))
549 cfq_rb_erase(&cfqq
->rb_node
, &cfqd
->service_tree
);
551 BUG_ON(!cfqd
->busy_queues
);
556 * rb tree support functions
558 static inline void cfq_del_rq_rb(struct request
*rq
)
560 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
561 struct cfq_data
*cfqd
= cfqq
->cfqd
;
562 const int sync
= rq_is_sync(rq
);
564 BUG_ON(!cfqq
->queued
[sync
]);
565 cfqq
->queued
[sync
]--;
567 elv_rb_del(&cfqq
->sort_list
, rq
);
569 if (cfq_cfqq_on_rr(cfqq
) && RB_EMPTY_ROOT(&cfqq
->sort_list
))
570 cfq_del_cfqq_rr(cfqd
, cfqq
);
573 static void cfq_add_rq_rb(struct request
*rq
)
575 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
576 struct cfq_data
*cfqd
= cfqq
->cfqd
;
577 struct request
*__alias
;
579 cfqq
->queued
[rq_is_sync(rq
)]++;
582 * looks a little odd, but the first insert might return an alias.
583 * if that happens, put the alias on the dispatch list
585 while ((__alias
= elv_rb_add(&cfqq
->sort_list
, rq
)) != NULL
)
586 cfq_dispatch_insert(cfqd
->queue
, __alias
);
588 if (!cfq_cfqq_on_rr(cfqq
))
589 cfq_add_cfqq_rr(cfqd
, cfqq
);
592 * check if this request is a better next-serve candidate
594 cfqq
->next_rq
= cfq_choose_req(cfqd
, cfqq
->next_rq
, rq
);
595 BUG_ON(!cfqq
->next_rq
);
599 cfq_reposition_rq_rb(struct cfq_queue
*cfqq
, struct request
*rq
)
601 elv_rb_del(&cfqq
->sort_list
, rq
);
602 cfqq
->queued
[rq_is_sync(rq
)]--;
606 static struct request
*
607 cfq_find_rq_fmerge(struct cfq_data
*cfqd
, struct bio
*bio
)
609 struct task_struct
*tsk
= current
;
610 pid_t key
= cfq_queue_pid(tsk
, bio_data_dir(bio
), bio_sync(bio
));
611 struct cfq_queue
*cfqq
;
613 cfqq
= cfq_find_cfq_hash(cfqd
, key
, tsk
->ioprio
);
615 sector_t sector
= bio
->bi_sector
+ bio_sectors(bio
);
617 return elv_rb_find(&cfqq
->sort_list
, sector
);
623 static void cfq_activate_request(request_queue_t
*q
, struct request
*rq
)
625 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
627 cfqd
->rq_in_driver
++;
630 * If the depth is larger 1, it really could be queueing. But lets
631 * make the mark a little higher - idling could still be good for
632 * low queueing, and a low queueing number could also just indicate
633 * a SCSI mid layer like behaviour where limit+1 is often seen.
635 if (!cfqd
->hw_tag
&& cfqd
->rq_in_driver
> 4)
638 cfqd
->last_position
= rq
->hard_sector
+ rq
->hard_nr_sectors
;
641 static void cfq_deactivate_request(request_queue_t
*q
, struct request
*rq
)
643 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
645 WARN_ON(!cfqd
->rq_in_driver
);
646 cfqd
->rq_in_driver
--;
649 static void cfq_remove_request(struct request
*rq
)
651 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
653 if (cfqq
->next_rq
== rq
)
654 cfqq
->next_rq
= cfq_find_next_rq(cfqq
->cfqd
, cfqq
, rq
);
656 list_del_init(&rq
->queuelist
);
659 if (rq_is_meta(rq
)) {
660 WARN_ON(!cfqq
->meta_pending
);
661 cfqq
->meta_pending
--;
665 static int cfq_merge(request_queue_t
*q
, struct request
**req
, struct bio
*bio
)
667 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
668 struct request
*__rq
;
670 __rq
= cfq_find_rq_fmerge(cfqd
, bio
);
671 if (__rq
&& elv_rq_merge_ok(__rq
, bio
)) {
673 return ELEVATOR_FRONT_MERGE
;
676 return ELEVATOR_NO_MERGE
;
679 static void cfq_merged_request(request_queue_t
*q
, struct request
*req
,
682 if (type
== ELEVATOR_FRONT_MERGE
) {
683 struct cfq_queue
*cfqq
= RQ_CFQQ(req
);
685 cfq_reposition_rq_rb(cfqq
, req
);
690 cfq_merged_requests(request_queue_t
*q
, struct request
*rq
,
691 struct request
*next
)
694 * reposition in fifo if next is older than rq
696 if (!list_empty(&rq
->queuelist
) && !list_empty(&next
->queuelist
) &&
697 time_before(next
->start_time
, rq
->start_time
))
698 list_move(&rq
->queuelist
, &next
->queuelist
);
700 cfq_remove_request(next
);
703 static int cfq_allow_merge(request_queue_t
*q
, struct request
*rq
,
706 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
707 const int rw
= bio_data_dir(bio
);
708 struct cfq_queue
*cfqq
;
712 * Disallow merge of a sync bio into an async request.
714 if ((bio_data_dir(bio
) == READ
|| bio_sync(bio
)) && !rq_is_sync(rq
))
718 * Lookup the cfqq that this bio will be queued with. Allow
719 * merge only if rq is queued there.
721 key
= cfq_queue_pid(current
, rw
, bio_sync(bio
));
722 cfqq
= cfq_find_cfq_hash(cfqd
, key
, current
->ioprio
);
724 if (cfqq
== RQ_CFQQ(rq
))
731 __cfq_set_active_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
735 * stop potential idle class queues waiting service
737 del_timer(&cfqd
->idle_class_timer
);
740 cfq_clear_cfqq_must_alloc_slice(cfqq
);
741 cfq_clear_cfqq_fifo_expire(cfqq
);
742 cfq_mark_cfqq_slice_new(cfqq
);
743 cfq_clear_cfqq_queue_new(cfqq
);
746 cfqd
->active_queue
= cfqq
;
750 * current cfqq expired its slice (or was too idle), select new one
753 __cfq_slice_expired(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
756 if (cfq_cfqq_wait_request(cfqq
))
757 del_timer(&cfqd
->idle_slice_timer
);
759 cfq_clear_cfqq_must_dispatch(cfqq
);
760 cfq_clear_cfqq_wait_request(cfqq
);
763 * store what was left of this slice, if the queue idled/timed out
765 if (timed_out
&& !cfq_cfqq_slice_new(cfqq
))
766 cfqq
->slice_resid
= cfqq
->slice_end
- jiffies
;
768 cfq_resort_rr_list(cfqd
, cfqq
);
770 if (cfqq
== cfqd
->active_queue
)
771 cfqd
->active_queue
= NULL
;
773 if (cfqd
->active_cic
) {
774 put_io_context(cfqd
->active_cic
->ioc
);
775 cfqd
->active_cic
= NULL
;
779 static inline void cfq_slice_expired(struct cfq_data
*cfqd
, int timed_out
)
781 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
784 __cfq_slice_expired(cfqd
, cfqq
, timed_out
);
788 * Get next queue for service. Unless we have a queue preemption,
789 * we'll simply select the first cfqq in the service tree.
791 static struct cfq_queue
*cfq_get_next_queue(struct cfq_data
*cfqd
)
793 struct cfq_queue
*cfqq
;
796 if (RB_EMPTY_ROOT(&cfqd
->service_tree
.rb
))
799 n
= cfq_rb_first(&cfqd
->service_tree
);
800 cfqq
= rb_entry(n
, struct cfq_queue
, rb_node
);
802 if (cfq_class_idle(cfqq
)) {
806 * if we have idle queues and no rt or be queues had
807 * pending requests, either allow immediate service if
808 * the grace period has passed or arm the idle grace
811 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
812 if (time_before(jiffies
, end
)) {
813 mod_timer(&cfqd
->idle_class_timer
, end
);
822 * Get and set a new active queue for service.
824 static struct cfq_queue
*cfq_set_active_queue(struct cfq_data
*cfqd
)
826 struct cfq_queue
*cfqq
;
828 cfqq
= cfq_get_next_queue(cfqd
);
829 __cfq_set_active_queue(cfqd
, cfqq
);
833 static inline sector_t
cfq_dist_from_last(struct cfq_data
*cfqd
,
836 if (rq
->sector
>= cfqd
->last_position
)
837 return rq
->sector
- cfqd
->last_position
;
839 return cfqd
->last_position
- rq
->sector
;
842 static inline int cfq_rq_close(struct cfq_data
*cfqd
, struct request
*rq
)
844 struct cfq_io_context
*cic
= cfqd
->active_cic
;
846 if (!sample_valid(cic
->seek_samples
))
849 return cfq_dist_from_last(cfqd
, rq
) <= cic
->seek_mean
;
852 static int cfq_close_cooperator(struct cfq_data
*cfq_data
,
853 struct cfq_queue
*cfqq
)
856 * We should notice if some of the queues are cooperating, eg
857 * working closely on the same area of the disk. In that case,
858 * we can group them together and don't waste time idling.
863 #define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
865 static void cfq_arm_slice_timer(struct cfq_data
*cfqd
)
867 struct cfq_queue
*cfqq
= cfqd
->active_queue
;
868 struct cfq_io_context
*cic
;
871 WARN_ON(!RB_EMPTY_ROOT(&cfqq
->sort_list
));
872 WARN_ON(cfq_cfqq_slice_new(cfqq
));
875 * idle is disabled, either manually or by past process history
877 if (!cfqd
->cfq_slice_idle
|| !cfq_cfqq_idle_window(cfqq
))
881 * task has exited, don't wait
883 cic
= cfqd
->active_cic
;
884 if (!cic
|| !cic
->ioc
->task
)
888 * See if this prio level has a good candidate
890 if (cfq_close_cooperator(cfqd
, cfqq
) &&
891 (sample_valid(cic
->ttime_samples
) && cic
->ttime_mean
> 2))
894 cfq_mark_cfqq_must_dispatch(cfqq
);
895 cfq_mark_cfqq_wait_request(cfqq
);
898 * we don't want to idle for seeks, but we do want to allow
899 * fair distribution of slice time for a process doing back-to-back
900 * seeks. so allow a little bit of time for him to submit a new rq
902 sl
= cfqd
->cfq_slice_idle
;
903 if (sample_valid(cic
->seek_samples
) && CIC_SEEKY(cic
))
904 sl
= min(sl
, msecs_to_jiffies(CFQ_MIN_TT
));
906 mod_timer(&cfqd
->idle_slice_timer
, jiffies
+ sl
);
910 * Move request from internal lists to the request queue dispatch list.
912 static void cfq_dispatch_insert(request_queue_t
*q
, struct request
*rq
)
914 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
916 cfq_remove_request(rq
);
918 elv_dispatch_sort(q
, rq
);
922 * return expired entry, or NULL to just start from scratch in rbtree
924 static inline struct request
*cfq_check_fifo(struct cfq_queue
*cfqq
)
926 struct cfq_data
*cfqd
= cfqq
->cfqd
;
930 if (cfq_cfqq_fifo_expire(cfqq
))
933 cfq_mark_cfqq_fifo_expire(cfqq
);
935 if (list_empty(&cfqq
->fifo
))
938 fifo
= cfq_cfqq_sync(cfqq
);
939 rq
= rq_entry_fifo(cfqq
->fifo
.next
);
941 if (time_before(jiffies
, rq
->start_time
+ cfqd
->cfq_fifo_expire
[fifo
]))
948 cfq_prio_to_maxrq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
950 const int base_rq
= cfqd
->cfq_slice_async_rq
;
952 WARN_ON(cfqq
->ioprio
>= IOPRIO_BE_NR
);
954 return 2 * (base_rq
+ base_rq
* (CFQ_PRIO_LISTS
- 1 - cfqq
->ioprio
));
958 * Select a queue for service. If we have a current active queue,
959 * check whether to continue servicing it, or retrieve and set a new one.
961 static struct cfq_queue
*cfq_select_queue(struct cfq_data
*cfqd
)
963 struct cfq_queue
*cfqq
;
965 cfqq
= cfqd
->active_queue
;
970 * The active queue has run out of time, expire it and select new.
972 if (cfq_slice_used(cfqq
))
976 * The active queue has requests and isn't expired, allow it to
979 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
))
983 * No requests pending. If the active queue still has requests in
984 * flight or is idling for a new request, allow either of these
985 * conditions to happen (or time out) before selecting a new queue.
987 if (cfqq
->dispatched
|| timer_pending(&cfqd
->idle_slice_timer
)) {
993 cfq_slice_expired(cfqd
, 0);
995 cfqq
= cfq_set_active_queue(cfqd
);
1001 * Dispatch some requests from cfqq, moving them to the request queue
1005 __cfq_dispatch_requests(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1010 BUG_ON(RB_EMPTY_ROOT(&cfqq
->sort_list
));
1016 * follow expired path, else get first next available
1018 if ((rq
= cfq_check_fifo(cfqq
)) == NULL
)
1022 * finally, insert request into driver dispatch list
1024 cfq_dispatch_insert(cfqd
->queue
, rq
);
1028 if (!cfqd
->active_cic
) {
1029 atomic_inc(&RQ_CIC(rq
)->ioc
->refcount
);
1030 cfqd
->active_cic
= RQ_CIC(rq
);
1033 if (RB_EMPTY_ROOT(&cfqq
->sort_list
))
1036 } while (dispatched
< max_dispatch
);
1039 * expire an async queue immediately if it has used up its slice. idle
1040 * queue always expire after 1 dispatch round.
1042 if (cfqd
->busy_queues
> 1 && ((!cfq_cfqq_sync(cfqq
) &&
1043 dispatched
>= cfq_prio_to_maxrq(cfqd
, cfqq
)) ||
1044 cfq_class_idle(cfqq
))) {
1045 cfqq
->slice_end
= jiffies
+ 1;
1046 cfq_slice_expired(cfqd
, 0);
1052 static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue
*cfqq
)
1056 while (cfqq
->next_rq
) {
1057 cfq_dispatch_insert(cfqq
->cfqd
->queue
, cfqq
->next_rq
);
1061 BUG_ON(!list_empty(&cfqq
->fifo
));
1066 * Drain our current requests. Used for barriers and when switching
1067 * io schedulers on-the-fly.
1069 static int cfq_forced_dispatch(struct cfq_data
*cfqd
)
1074 while ((n
= cfq_rb_first(&cfqd
->service_tree
)) != NULL
) {
1075 struct cfq_queue
*cfqq
= rb_entry(n
, struct cfq_queue
, rb_node
);
1077 dispatched
+= __cfq_forced_dispatch_cfqq(cfqq
);
1080 cfq_slice_expired(cfqd
, 0);
1082 BUG_ON(cfqd
->busy_queues
);
1087 static int cfq_dispatch_requests(request_queue_t
*q
, int force
)
1089 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1090 struct cfq_queue
*cfqq
;
1093 if (!cfqd
->busy_queues
)
1096 if (unlikely(force
))
1097 return cfq_forced_dispatch(cfqd
);
1100 while ((cfqq
= cfq_select_queue(cfqd
)) != NULL
) {
1103 if (cfqd
->busy_queues
> 1) {
1105 * So we have dispatched before in this round, if the
1106 * next queue has idling enabled (must be sync), don't
1107 * allow it service until the previous have completed.
1109 if (cfqd
->rq_in_driver
&& cfq_cfqq_idle_window(cfqq
) &&
1112 if (cfqq
->dispatched
>= cfqd
->cfq_quantum
)
1116 cfq_clear_cfqq_must_dispatch(cfqq
);
1117 cfq_clear_cfqq_wait_request(cfqq
);
1118 del_timer(&cfqd
->idle_slice_timer
);
1120 max_dispatch
= cfqd
->cfq_quantum
;
1121 if (cfq_class_idle(cfqq
))
1124 dispatched
+= __cfq_dispatch_requests(cfqd
, cfqq
, max_dispatch
);
1131 * task holds one reference to the queue, dropped when task exits. each rq
1132 * in-flight on this queue also holds a reference, dropped when rq is freed.
1134 * queue lock must be held here.
1136 static void cfq_put_queue(struct cfq_queue
*cfqq
)
1138 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1140 BUG_ON(atomic_read(&cfqq
->ref
) <= 0);
1142 if (!atomic_dec_and_test(&cfqq
->ref
))
1145 BUG_ON(rb_first(&cfqq
->sort_list
));
1146 BUG_ON(cfqq
->allocated
[READ
] + cfqq
->allocated
[WRITE
]);
1147 BUG_ON(cfq_cfqq_on_rr(cfqq
));
1149 if (unlikely(cfqd
->active_queue
== cfqq
)) {
1150 __cfq_slice_expired(cfqd
, cfqq
, 0);
1151 cfq_schedule_dispatch(cfqd
);
1155 * it's on the empty list and still hashed
1157 hlist_del(&cfqq
->cfq_hash
);
1158 kmem_cache_free(cfq_pool
, cfqq
);
1161 static struct cfq_queue
*
1162 __cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned int prio
,
1165 struct hlist_head
*hash_list
= &cfqd
->cfq_hash
[hashval
];
1166 struct hlist_node
*entry
;
1167 struct cfq_queue
*__cfqq
;
1169 hlist_for_each_entry(__cfqq
, entry
, hash_list
, cfq_hash
) {
1170 const unsigned short __p
= IOPRIO_PRIO_VALUE(__cfqq
->org_ioprio_class
, __cfqq
->org_ioprio
);
1172 if (__cfqq
->key
== key
&& (__p
== prio
|| !prio
))
1179 static struct cfq_queue
*
1180 cfq_find_cfq_hash(struct cfq_data
*cfqd
, unsigned int key
, unsigned short prio
)
1182 return __cfq_find_cfq_hash(cfqd
, key
, prio
, hash_long(key
, CFQ_QHASH_SHIFT
));
1185 static void cfq_free_io_context(struct io_context
*ioc
)
1187 struct cfq_io_context
*__cic
;
1191 while ((n
= rb_first(&ioc
->cic_root
)) != NULL
) {
1192 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1193 rb_erase(&__cic
->rb_node
, &ioc
->cic_root
);
1194 kmem_cache_free(cfq_ioc_pool
, __cic
);
1198 elv_ioc_count_mod(ioc_count
, -freed
);
1200 if (ioc_gone
&& !elv_ioc_count_read(ioc_count
))
1204 static void cfq_exit_cfqq(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1206 if (unlikely(cfqq
== cfqd
->active_queue
)) {
1207 __cfq_slice_expired(cfqd
, cfqq
, 0);
1208 cfq_schedule_dispatch(cfqd
);
1211 cfq_put_queue(cfqq
);
1214 static void __cfq_exit_single_io_context(struct cfq_data
*cfqd
,
1215 struct cfq_io_context
*cic
)
1217 list_del_init(&cic
->queue_list
);
1221 if (cic
->cfqq
[ASYNC
]) {
1222 cfq_exit_cfqq(cfqd
, cic
->cfqq
[ASYNC
]);
1223 cic
->cfqq
[ASYNC
] = NULL
;
1226 if (cic
->cfqq
[SYNC
]) {
1227 cfq_exit_cfqq(cfqd
, cic
->cfqq
[SYNC
]);
1228 cic
->cfqq
[SYNC
] = NULL
;
1232 static void cfq_exit_single_io_context(struct cfq_io_context
*cic
)
1234 struct cfq_data
*cfqd
= cic
->key
;
1237 request_queue_t
*q
= cfqd
->queue
;
1239 spin_lock_irq(q
->queue_lock
);
1240 __cfq_exit_single_io_context(cfqd
, cic
);
1241 spin_unlock_irq(q
->queue_lock
);
1246 * The process that ioc belongs to has exited, we need to clean up
1247 * and put the internal structures we have that belongs to that process.
1249 static void cfq_exit_io_context(struct io_context
*ioc
)
1251 struct cfq_io_context
*__cic
;
1255 * put the reference this task is holding to the various queues
1258 n
= rb_first(&ioc
->cic_root
);
1260 __cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1262 cfq_exit_single_io_context(__cic
);
1267 static struct cfq_io_context
*
1268 cfq_alloc_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1270 struct cfq_io_context
*cic
;
1272 cic
= kmem_cache_alloc_node(cfq_ioc_pool
, gfp_mask
, cfqd
->queue
->node
);
1274 memset(cic
, 0, sizeof(*cic
));
1275 cic
->last_end_request
= jiffies
;
1276 INIT_LIST_HEAD(&cic
->queue_list
);
1277 cic
->dtor
= cfq_free_io_context
;
1278 cic
->exit
= cfq_exit_io_context
;
1279 elv_ioc_count_inc(ioc_count
);
1285 static void cfq_init_prio_data(struct cfq_queue
*cfqq
)
1287 struct task_struct
*tsk
= current
;
1290 if (!cfq_cfqq_prio_changed(cfqq
))
1293 ioprio_class
= IOPRIO_PRIO_CLASS(tsk
->ioprio
);
1294 switch (ioprio_class
) {
1296 printk(KERN_ERR
"cfq: bad prio %x\n", ioprio_class
);
1297 case IOPRIO_CLASS_NONE
:
1299 * no prio set, place us in the middle of the BE classes
1301 cfqq
->ioprio
= task_nice_ioprio(tsk
);
1302 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1304 case IOPRIO_CLASS_RT
:
1305 cfqq
->ioprio
= task_ioprio(tsk
);
1306 cfqq
->ioprio_class
= IOPRIO_CLASS_RT
;
1308 case IOPRIO_CLASS_BE
:
1309 cfqq
->ioprio
= task_ioprio(tsk
);
1310 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1312 case IOPRIO_CLASS_IDLE
:
1313 cfqq
->ioprio_class
= IOPRIO_CLASS_IDLE
;
1315 cfq_clear_cfqq_idle_window(cfqq
);
1320 * keep track of original prio settings in case we have to temporarily
1321 * elevate the priority of this queue
1323 cfqq
->org_ioprio
= cfqq
->ioprio
;
1324 cfqq
->org_ioprio_class
= cfqq
->ioprio_class
;
1325 cfq_clear_cfqq_prio_changed(cfqq
);
1328 static inline void changed_ioprio(struct cfq_io_context
*cic
)
1330 struct cfq_data
*cfqd
= cic
->key
;
1331 struct cfq_queue
*cfqq
;
1332 unsigned long flags
;
1334 if (unlikely(!cfqd
))
1337 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1339 cfqq
= cic
->cfqq
[ASYNC
];
1341 struct cfq_queue
*new_cfqq
;
1342 new_cfqq
= cfq_get_queue(cfqd
, CFQ_KEY_ASYNC
, cic
->ioc
->task
,
1345 cic
->cfqq
[ASYNC
] = new_cfqq
;
1346 cfq_put_queue(cfqq
);
1350 cfqq
= cic
->cfqq
[SYNC
];
1352 cfq_mark_cfqq_prio_changed(cfqq
);
1354 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
1357 static void cfq_ioc_set_ioprio(struct io_context
*ioc
)
1359 struct cfq_io_context
*cic
;
1362 ioc
->ioprio_changed
= 0;
1364 n
= rb_first(&ioc
->cic_root
);
1366 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1368 changed_ioprio(cic
);
1373 static struct cfq_queue
*
1374 cfq_get_queue(struct cfq_data
*cfqd
, unsigned int key
, struct task_struct
*tsk
,
1377 const int hashval
= hash_long(key
, CFQ_QHASH_SHIFT
);
1378 struct cfq_queue
*cfqq
, *new_cfqq
= NULL
;
1379 unsigned short ioprio
;
1382 ioprio
= tsk
->ioprio
;
1383 cfqq
= __cfq_find_cfq_hash(cfqd
, key
, ioprio
, hashval
);
1389 } else if (gfp_mask
& __GFP_WAIT
) {
1391 * Inform the allocator of the fact that we will
1392 * just repeat this allocation if it fails, to allow
1393 * the allocator to do whatever it needs to attempt to
1396 spin_unlock_irq(cfqd
->queue
->queue_lock
);
1397 new_cfqq
= kmem_cache_alloc_node(cfq_pool
, gfp_mask
|__GFP_NOFAIL
, cfqd
->queue
->node
);
1398 spin_lock_irq(cfqd
->queue
->queue_lock
);
1401 cfqq
= kmem_cache_alloc_node(cfq_pool
, gfp_mask
, cfqd
->queue
->node
);
1406 memset(cfqq
, 0, sizeof(*cfqq
));
1408 INIT_HLIST_NODE(&cfqq
->cfq_hash
);
1409 RB_CLEAR_NODE(&cfqq
->rb_node
);
1410 INIT_LIST_HEAD(&cfqq
->fifo
);
1413 hlist_add_head(&cfqq
->cfq_hash
, &cfqd
->cfq_hash
[hashval
]);
1414 atomic_set(&cfqq
->ref
, 0);
1417 if (key
!= CFQ_KEY_ASYNC
)
1418 cfq_mark_cfqq_idle_window(cfqq
);
1420 cfq_mark_cfqq_prio_changed(cfqq
);
1421 cfq_mark_cfqq_queue_new(cfqq
);
1422 cfq_init_prio_data(cfqq
);
1426 kmem_cache_free(cfq_pool
, new_cfqq
);
1428 atomic_inc(&cfqq
->ref
);
1430 WARN_ON((gfp_mask
& __GFP_WAIT
) && !cfqq
);
1435 * We drop cfq io contexts lazily, so we may find a dead one.
1438 cfq_drop_dead_cic(struct io_context
*ioc
, struct cfq_io_context
*cic
)
1440 WARN_ON(!list_empty(&cic
->queue_list
));
1441 rb_erase(&cic
->rb_node
, &ioc
->cic_root
);
1442 kmem_cache_free(cfq_ioc_pool
, cic
);
1443 elv_ioc_count_dec(ioc_count
);
1446 static struct cfq_io_context
*
1447 cfq_cic_rb_lookup(struct cfq_data
*cfqd
, struct io_context
*ioc
)
1450 struct cfq_io_context
*cic
;
1451 void *k
, *key
= cfqd
;
1454 n
= ioc
->cic_root
.rb_node
;
1456 cic
= rb_entry(n
, struct cfq_io_context
, rb_node
);
1457 /* ->key must be copied to avoid race with cfq_exit_queue() */
1460 cfq_drop_dead_cic(ioc
, cic
);
1476 cfq_cic_link(struct cfq_data
*cfqd
, struct io_context
*ioc
,
1477 struct cfq_io_context
*cic
)
1480 struct rb_node
*parent
;
1481 struct cfq_io_context
*__cic
;
1482 unsigned long flags
;
1490 p
= &ioc
->cic_root
.rb_node
;
1493 __cic
= rb_entry(parent
, struct cfq_io_context
, rb_node
);
1494 /* ->key must be copied to avoid race with cfq_exit_queue() */
1497 cfq_drop_dead_cic(ioc
, __cic
);
1503 else if (cic
->key
> k
)
1504 p
= &(*p
)->rb_right
;
1509 rb_link_node(&cic
->rb_node
, parent
, p
);
1510 rb_insert_color(&cic
->rb_node
, &ioc
->cic_root
);
1512 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1513 list_add(&cic
->queue_list
, &cfqd
->cic_list
);
1514 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
1518 * Setup general io context and cfq io context. There can be several cfq
1519 * io contexts per general io context, if this process is doing io to more
1520 * than one device managed by cfq.
1522 static struct cfq_io_context
*
1523 cfq_get_io_context(struct cfq_data
*cfqd
, gfp_t gfp_mask
)
1525 struct io_context
*ioc
= NULL
;
1526 struct cfq_io_context
*cic
;
1528 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1530 ioc
= get_io_context(gfp_mask
, cfqd
->queue
->node
);
1534 cic
= cfq_cic_rb_lookup(cfqd
, ioc
);
1538 cic
= cfq_alloc_io_context(cfqd
, gfp_mask
);
1542 cfq_cic_link(cfqd
, ioc
, cic
);
1544 smp_read_barrier_depends();
1545 if (unlikely(ioc
->ioprio_changed
))
1546 cfq_ioc_set_ioprio(ioc
);
1550 put_io_context(ioc
);
1555 cfq_update_io_thinktime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
)
1557 unsigned long elapsed
= jiffies
- cic
->last_end_request
;
1558 unsigned long ttime
= min(elapsed
, 2UL * cfqd
->cfq_slice_idle
);
1560 cic
->ttime_samples
= (7*cic
->ttime_samples
+ 256) / 8;
1561 cic
->ttime_total
= (7*cic
->ttime_total
+ 256*ttime
) / 8;
1562 cic
->ttime_mean
= (cic
->ttime_total
+ 128) / cic
->ttime_samples
;
1566 cfq_update_io_seektime(struct cfq_data
*cfqd
, struct cfq_io_context
*cic
,
1572 if (cic
->last_request_pos
< rq
->sector
)
1573 sdist
= rq
->sector
- cic
->last_request_pos
;
1575 sdist
= cic
->last_request_pos
- rq
->sector
;
1577 if (!cic
->seek_samples
) {
1578 cfqd
->new_seek_total
= (7*cic
->seek_total
+ (u64
)256*sdist
) / 8;
1579 cfqd
->new_seek_mean
= cfqd
->new_seek_total
/ 256;
1583 * Don't allow the seek distance to get too large from the
1584 * odd fragment, pagein, etc
1586 if (cic
->seek_samples
<= 60) /* second&third seek */
1587 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*1024);
1589 sdist
= min(sdist
, (cic
->seek_mean
* 4) + 2*1024*64);
1591 cic
->seek_samples
= (7*cic
->seek_samples
+ 256) / 8;
1592 cic
->seek_total
= (7*cic
->seek_total
+ (u64
)256*sdist
) / 8;
1593 total
= cic
->seek_total
+ (cic
->seek_samples
/2);
1594 do_div(total
, cic
->seek_samples
);
1595 cic
->seek_mean
= (sector_t
)total
;
1599 * Disable idle window if the process thinks too long or seeks so much that
1603 cfq_update_idle_window(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1604 struct cfq_io_context
*cic
)
1606 int enable_idle
= cfq_cfqq_idle_window(cfqq
);
1608 if (!cic
->ioc
->task
|| !cfqd
->cfq_slice_idle
||
1609 (cfqd
->hw_tag
&& CIC_SEEKY(cic
)))
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
);
1625 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1626 * no or if we aren't sure, a 1 will cause a preempt.
1629 cfq_should_preempt(struct cfq_data
*cfqd
, struct cfq_queue
*new_cfqq
,
1632 struct cfq_queue
*cfqq
;
1634 cfqq
= cfqd
->active_queue
;
1638 if (cfq_slice_used(cfqq
))
1641 if (cfq_class_idle(new_cfqq
))
1644 if (cfq_class_idle(cfqq
))
1648 * if the new request is sync, but the currently running queue is
1649 * not, let the sync request have priority.
1651 if (rq_is_sync(rq
) && !cfq_cfqq_sync(cfqq
))
1655 * So both queues are sync. Let the new request get disk time if
1656 * it's a metadata request and the current queue is doing regular IO.
1658 if (rq_is_meta(rq
) && !cfqq
->meta_pending
)
1661 if (!cfqd
->active_cic
|| !cfq_cfqq_wait_request(cfqq
))
1665 * if this request is as-good as one we would expect from the
1666 * current cfqq, let it preempt
1668 if (cfq_rq_close(cfqd
, rq
))
1675 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1676 * let it have half of its nominal slice.
1678 static void cfq_preempt_queue(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
)
1680 cfq_slice_expired(cfqd
, 1);
1683 * Put the new queue at the front of the of the current list,
1684 * so we know that it will be selected next.
1686 BUG_ON(!cfq_cfqq_on_rr(cfqq
));
1688 cfq_service_tree_add(cfqd
, cfqq
, 1);
1690 cfqq
->slice_end
= 0;
1691 cfq_mark_cfqq_slice_new(cfqq
);
1695 * Called when a new fs request (rq) is added (to cfqq). Check if there's
1696 * something we should do about it
1699 cfq_rq_enqueued(struct cfq_data
*cfqd
, struct cfq_queue
*cfqq
,
1702 struct cfq_io_context
*cic
= RQ_CIC(rq
);
1705 cfqq
->meta_pending
++;
1707 cfq_update_io_thinktime(cfqd
, cic
);
1708 cfq_update_io_seektime(cfqd
, cic
, rq
);
1709 cfq_update_idle_window(cfqd
, cfqq
, cic
);
1711 cic
->last_request_pos
= rq
->sector
+ rq
->nr_sectors
;
1712 cfqq
->last_request_pos
= cic
->last_request_pos
;
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 blk_start_queueing(cfqd
->queue
);
1725 } else if (cfq_should_preempt(cfqd
, cfqq
, rq
)) {
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 blk_start_queueing(cfqd
->queue
);
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_queue
*cfqq
= RQ_CFQQ(rq
);
1742 cfq_init_prio_data(cfqq
);
1746 list_add_tail(&rq
->queuelist
, &cfqq
->fifo
);
1748 cfq_rq_enqueued(cfqd
, cfqq
, rq
);
1751 static void cfq_completed_request(request_queue_t
*q
, struct request
*rq
)
1753 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
1754 struct cfq_data
*cfqd
= cfqq
->cfqd
;
1755 const int sync
= rq_is_sync(rq
);
1760 WARN_ON(!cfqd
->rq_in_driver
);
1761 WARN_ON(!cfqq
->dispatched
);
1762 cfqd
->rq_in_driver
--;
1765 if (!cfq_class_idle(cfqq
))
1766 cfqd
->last_end_request
= now
;
1769 RQ_CIC(rq
)->last_end_request
= now
;
1772 * If this is the active queue, check if it needs to be expired,
1773 * or if we want to idle in case it has no pending requests.
1775 if (cfqd
->active_queue
== cfqq
) {
1776 if (cfq_cfqq_slice_new(cfqq
)) {
1777 cfq_set_prio_slice(cfqd
, cfqq
);
1778 cfq_clear_cfqq_slice_new(cfqq
);
1780 if (cfq_slice_used(cfqq
))
1781 cfq_slice_expired(cfqd
, 1);
1782 else if (sync
&& RB_EMPTY_ROOT(&cfqq
->sort_list
))
1783 cfq_arm_slice_timer(cfqd
);
1786 if (!cfqd
->rq_in_driver
)
1787 cfq_schedule_dispatch(cfqd
);
1791 * we temporarily boost lower priority queues if they are holding fs exclusive
1792 * resources. they are boosted to normal prio (CLASS_BE/4)
1794 static void cfq_prio_boost(struct cfq_queue
*cfqq
)
1796 if (has_fs_excl()) {
1798 * boost idle prio on transactions that would lock out other
1799 * users of the filesystem
1801 if (cfq_class_idle(cfqq
))
1802 cfqq
->ioprio_class
= IOPRIO_CLASS_BE
;
1803 if (cfqq
->ioprio
> IOPRIO_NORM
)
1804 cfqq
->ioprio
= IOPRIO_NORM
;
1807 * check if we need to unboost the queue
1809 if (cfqq
->ioprio_class
!= cfqq
->org_ioprio_class
)
1810 cfqq
->ioprio_class
= cfqq
->org_ioprio_class
;
1811 if (cfqq
->ioprio
!= cfqq
->org_ioprio
)
1812 cfqq
->ioprio
= cfqq
->org_ioprio
;
1816 static inline int __cfq_may_queue(struct cfq_queue
*cfqq
)
1818 if ((cfq_cfqq_wait_request(cfqq
) || cfq_cfqq_must_alloc(cfqq
)) &&
1819 !cfq_cfqq_must_alloc_slice(cfqq
)) {
1820 cfq_mark_cfqq_must_alloc_slice(cfqq
);
1821 return ELV_MQUEUE_MUST
;
1824 return ELV_MQUEUE_MAY
;
1827 static int cfq_may_queue(request_queue_t
*q
, int rw
)
1829 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1830 struct task_struct
*tsk
= current
;
1831 struct cfq_queue
*cfqq
;
1834 key
= cfq_queue_pid(tsk
, rw
, rw
& REQ_RW_SYNC
);
1837 * don't force setup of a queue from here, as a call to may_queue
1838 * does not necessarily imply that a request actually will be queued.
1839 * so just lookup a possibly existing queue, or return 'may queue'
1842 cfqq
= cfq_find_cfq_hash(cfqd
, key
, tsk
->ioprio
);
1844 cfq_init_prio_data(cfqq
);
1845 cfq_prio_boost(cfqq
);
1847 return __cfq_may_queue(cfqq
);
1850 return ELV_MQUEUE_MAY
;
1854 * queue lock held here
1856 static void cfq_put_request(struct request
*rq
)
1858 struct cfq_queue
*cfqq
= RQ_CFQQ(rq
);
1861 const int rw
= rq_data_dir(rq
);
1863 BUG_ON(!cfqq
->allocated
[rw
]);
1864 cfqq
->allocated
[rw
]--;
1866 put_io_context(RQ_CIC(rq
)->ioc
);
1868 rq
->elevator_private
= NULL
;
1869 rq
->elevator_private2
= NULL
;
1871 cfq_put_queue(cfqq
);
1876 * Allocate cfq data structures associated with this request.
1879 cfq_set_request(request_queue_t
*q
, struct request
*rq
, gfp_t gfp_mask
)
1881 struct cfq_data
*cfqd
= q
->elevator
->elevator_data
;
1882 struct task_struct
*tsk
= current
;
1883 struct cfq_io_context
*cic
;
1884 const int rw
= rq_data_dir(rq
);
1885 const int is_sync
= rq_is_sync(rq
);
1886 pid_t key
= cfq_queue_pid(tsk
, rw
, is_sync
);
1887 struct cfq_queue
*cfqq
;
1888 unsigned long flags
;
1890 might_sleep_if(gfp_mask
& __GFP_WAIT
);
1892 cic
= cfq_get_io_context(cfqd
, gfp_mask
);
1894 spin_lock_irqsave(q
->queue_lock
, flags
);
1899 if (!cic
->cfqq
[is_sync
]) {
1900 cfqq
= cfq_get_queue(cfqd
, key
, tsk
, gfp_mask
);
1904 cic
->cfqq
[is_sync
] = cfqq
;
1906 cfqq
= cic
->cfqq
[is_sync
];
1908 cfqq
->allocated
[rw
]++;
1909 cfq_clear_cfqq_must_alloc(cfqq
);
1910 atomic_inc(&cfqq
->ref
);
1912 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1914 rq
->elevator_private
= cic
;
1915 rq
->elevator_private2
= cfqq
;
1920 put_io_context(cic
->ioc
);
1922 cfq_schedule_dispatch(cfqd
);
1923 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1927 static void cfq_kick_queue(struct work_struct
*work
)
1929 struct cfq_data
*cfqd
=
1930 container_of(work
, struct cfq_data
, unplug_work
);
1931 request_queue_t
*q
= cfqd
->queue
;
1932 unsigned long flags
;
1934 spin_lock_irqsave(q
->queue_lock
, flags
);
1935 blk_start_queueing(q
);
1936 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1940 * Timer running if the active_queue is currently idling inside its time slice
1942 static void cfq_idle_slice_timer(unsigned long data
)
1944 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
1945 struct cfq_queue
*cfqq
;
1946 unsigned long flags
;
1949 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1951 if ((cfqq
= cfqd
->active_queue
) != NULL
) {
1957 if (cfq_slice_used(cfqq
))
1961 * only expire and reinvoke request handler, if there are
1962 * other queues with pending requests
1964 if (!cfqd
->busy_queues
)
1968 * not expired and it has a request pending, let it dispatch
1970 if (!RB_EMPTY_ROOT(&cfqq
->sort_list
)) {
1971 cfq_mark_cfqq_must_dispatch(cfqq
);
1976 cfq_slice_expired(cfqd
, timed_out
);
1978 cfq_schedule_dispatch(cfqd
);
1980 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
1984 * Timer running if an idle class queue is waiting for service
1986 static void cfq_idle_class_timer(unsigned long data
)
1988 struct cfq_data
*cfqd
= (struct cfq_data
*) data
;
1989 unsigned long flags
, end
;
1991 spin_lock_irqsave(cfqd
->queue
->queue_lock
, flags
);
1994 * race with a non-idle queue, reset timer
1996 end
= cfqd
->last_end_request
+ CFQ_IDLE_GRACE
;
1997 if (!time_after_eq(jiffies
, end
))
1998 mod_timer(&cfqd
->idle_class_timer
, end
);
2000 cfq_schedule_dispatch(cfqd
);
2002 spin_unlock_irqrestore(cfqd
->queue
->queue_lock
, flags
);
2005 static void cfq_shutdown_timer_wq(struct cfq_data
*cfqd
)
2007 del_timer_sync(&cfqd
->idle_slice_timer
);
2008 del_timer_sync(&cfqd
->idle_class_timer
);
2009 blk_sync_queue(cfqd
->queue
);
2012 static void cfq_exit_queue(elevator_t
*e
)
2014 struct cfq_data
*cfqd
= e
->elevator_data
;
2015 request_queue_t
*q
= cfqd
->queue
;
2017 cfq_shutdown_timer_wq(cfqd
);
2019 spin_lock_irq(q
->queue_lock
);
2021 if (cfqd
->active_queue
)
2022 __cfq_slice_expired(cfqd
, cfqd
->active_queue
, 0);
2024 while (!list_empty(&cfqd
->cic_list
)) {
2025 struct cfq_io_context
*cic
= list_entry(cfqd
->cic_list
.next
,
2026 struct cfq_io_context
,
2029 __cfq_exit_single_io_context(cfqd
, cic
);
2032 spin_unlock_irq(q
->queue_lock
);
2034 cfq_shutdown_timer_wq(cfqd
);
2036 kfree(cfqd
->cfq_hash
);
2040 static void *cfq_init_queue(request_queue_t
*q
)
2042 struct cfq_data
*cfqd
;
2045 cfqd
= kmalloc_node(sizeof(*cfqd
), GFP_KERNEL
, q
->node
);
2049 memset(cfqd
, 0, sizeof(*cfqd
));
2051 cfqd
->service_tree
= CFQ_RB_ROOT
;
2052 INIT_LIST_HEAD(&cfqd
->cic_list
);
2054 cfqd
->cfq_hash
= kmalloc_node(sizeof(struct hlist_head
) * CFQ_QHASH_ENTRIES
, GFP_KERNEL
, q
->node
);
2055 if (!cfqd
->cfq_hash
)
2058 for (i
= 0; i
< CFQ_QHASH_ENTRIES
; i
++)
2059 INIT_HLIST_HEAD(&cfqd
->cfq_hash
[i
]);
2063 init_timer(&cfqd
->idle_slice_timer
);
2064 cfqd
->idle_slice_timer
.function
= cfq_idle_slice_timer
;
2065 cfqd
->idle_slice_timer
.data
= (unsigned long) cfqd
;
2067 init_timer(&cfqd
->idle_class_timer
);
2068 cfqd
->idle_class_timer
.function
= cfq_idle_class_timer
;
2069 cfqd
->idle_class_timer
.data
= (unsigned long) cfqd
;
2071 INIT_WORK(&cfqd
->unplug_work
, cfq_kick_queue
);
2073 cfqd
->cfq_quantum
= cfq_quantum
;
2074 cfqd
->cfq_fifo_expire
[0] = cfq_fifo_expire
[0];
2075 cfqd
->cfq_fifo_expire
[1] = cfq_fifo_expire
[1];
2076 cfqd
->cfq_back_max
= cfq_back_max
;
2077 cfqd
->cfq_back_penalty
= cfq_back_penalty
;
2078 cfqd
->cfq_slice
[0] = cfq_slice_async
;
2079 cfqd
->cfq_slice
[1] = cfq_slice_sync
;
2080 cfqd
->cfq_slice_async_rq
= cfq_slice_async_rq
;
2081 cfqd
->cfq_slice_idle
= cfq_slice_idle
;
2089 static void cfq_slab_kill(void)
2092 kmem_cache_destroy(cfq_pool
);
2094 kmem_cache_destroy(cfq_ioc_pool
);
2097 static int __init
cfq_slab_setup(void)
2099 cfq_pool
= kmem_cache_create("cfq_pool", sizeof(struct cfq_queue
), 0, 0,
2104 cfq_ioc_pool
= kmem_cache_create("cfq_ioc_pool",
2105 sizeof(struct cfq_io_context
), 0, 0, NULL
, NULL
);
2116 * sysfs parts below -->
2119 cfq_var_show(unsigned int var
, char *page
)
2121 return sprintf(page
, "%d\n", var
);
2125 cfq_var_store(unsigned int *var
, const char *page
, size_t count
)
2127 char *p
= (char *) page
;
2129 *var
= simple_strtoul(p
, &p
, 10);
2133 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2134 static ssize_t __FUNC(elevator_t *e, char *page) \
2136 struct cfq_data *cfqd = e->elevator_data; \
2137 unsigned int __data = __VAR; \
2139 __data = jiffies_to_msecs(__data); \
2140 return cfq_var_show(__data, (page)); \
2142 SHOW_FUNCTION(cfq_quantum_show
, cfqd
->cfq_quantum
, 0);
2143 SHOW_FUNCTION(cfq_fifo_expire_sync_show
, cfqd
->cfq_fifo_expire
[1], 1);
2144 SHOW_FUNCTION(cfq_fifo_expire_async_show
, cfqd
->cfq_fifo_expire
[0], 1);
2145 SHOW_FUNCTION(cfq_back_seek_max_show
, cfqd
->cfq_back_max
, 0);
2146 SHOW_FUNCTION(cfq_back_seek_penalty_show
, cfqd
->cfq_back_penalty
, 0);
2147 SHOW_FUNCTION(cfq_slice_idle_show
, cfqd
->cfq_slice_idle
, 1);
2148 SHOW_FUNCTION(cfq_slice_sync_show
, cfqd
->cfq_slice
[1], 1);
2149 SHOW_FUNCTION(cfq_slice_async_show
, cfqd
->cfq_slice
[0], 1);
2150 SHOW_FUNCTION(cfq_slice_async_rq_show
, cfqd
->cfq_slice_async_rq
, 0);
2151 #undef SHOW_FUNCTION
2153 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2154 static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
2156 struct cfq_data *cfqd = e->elevator_data; \
2157 unsigned int __data; \
2158 int ret = cfq_var_store(&__data, (page), count); \
2159 if (__data < (MIN)) \
2161 else if (__data > (MAX)) \
2164 *(__PTR) = msecs_to_jiffies(__data); \
2166 *(__PTR) = __data; \
2169 STORE_FUNCTION(cfq_quantum_store
, &cfqd
->cfq_quantum
, 1, UINT_MAX
, 0);
2170 STORE_FUNCTION(cfq_fifo_expire_sync_store
, &cfqd
->cfq_fifo_expire
[1], 1, UINT_MAX
, 1);
2171 STORE_FUNCTION(cfq_fifo_expire_async_store
, &cfqd
->cfq_fifo_expire
[0], 1, UINT_MAX
, 1);
2172 STORE_FUNCTION(cfq_back_seek_max_store
, &cfqd
->cfq_back_max
, 0, UINT_MAX
, 0);
2173 STORE_FUNCTION(cfq_back_seek_penalty_store
, &cfqd
->cfq_back_penalty
, 1, UINT_MAX
, 0);
2174 STORE_FUNCTION(cfq_slice_idle_store
, &cfqd
->cfq_slice_idle
, 0, UINT_MAX
, 1);
2175 STORE_FUNCTION(cfq_slice_sync_store
, &cfqd
->cfq_slice
[1], 1, UINT_MAX
, 1);
2176 STORE_FUNCTION(cfq_slice_async_store
, &cfqd
->cfq_slice
[0], 1, UINT_MAX
, 1);
2177 STORE_FUNCTION(cfq_slice_async_rq_store
, &cfqd
->cfq_slice_async_rq
, 1, UINT_MAX
, 0);
2178 #undef STORE_FUNCTION
2180 #define CFQ_ATTR(name) \
2181 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2183 static struct elv_fs_entry cfq_attrs
[] = {
2185 CFQ_ATTR(fifo_expire_sync
),
2186 CFQ_ATTR(fifo_expire_async
),
2187 CFQ_ATTR(back_seek_max
),
2188 CFQ_ATTR(back_seek_penalty
),
2189 CFQ_ATTR(slice_sync
),
2190 CFQ_ATTR(slice_async
),
2191 CFQ_ATTR(slice_async_rq
),
2192 CFQ_ATTR(slice_idle
),
2196 static struct elevator_type iosched_cfq
= {
2198 .elevator_merge_fn
= cfq_merge
,
2199 .elevator_merged_fn
= cfq_merged_request
,
2200 .elevator_merge_req_fn
= cfq_merged_requests
,
2201 .elevator_allow_merge_fn
= cfq_allow_merge
,
2202 .elevator_dispatch_fn
= cfq_dispatch_requests
,
2203 .elevator_add_req_fn
= cfq_insert_request
,
2204 .elevator_activate_req_fn
= cfq_activate_request
,
2205 .elevator_deactivate_req_fn
= cfq_deactivate_request
,
2206 .elevator_queue_empty_fn
= cfq_queue_empty
,
2207 .elevator_completed_req_fn
= cfq_completed_request
,
2208 .elevator_former_req_fn
= elv_rb_former_request
,
2209 .elevator_latter_req_fn
= elv_rb_latter_request
,
2210 .elevator_set_req_fn
= cfq_set_request
,
2211 .elevator_put_req_fn
= cfq_put_request
,
2212 .elevator_may_queue_fn
= cfq_may_queue
,
2213 .elevator_init_fn
= cfq_init_queue
,
2214 .elevator_exit_fn
= cfq_exit_queue
,
2215 .trim
= cfq_free_io_context
,
2217 .elevator_attrs
= cfq_attrs
,
2218 .elevator_name
= "cfq",
2219 .elevator_owner
= THIS_MODULE
,
2222 static int __init
cfq_init(void)
2227 * could be 0 on HZ < 1000 setups
2229 if (!cfq_slice_async
)
2230 cfq_slice_async
= 1;
2231 if (!cfq_slice_idle
)
2234 if (cfq_slab_setup())
2237 ret
= elv_register(&iosched_cfq
);
2244 static void __exit
cfq_exit(void)
2246 DECLARE_COMPLETION_ONSTACK(all_gone
);
2247 elv_unregister(&iosched_cfq
);
2248 ioc_gone
= &all_gone
;
2249 /* ioc_gone's update must be visible before reading ioc_count */
2251 if (elv_ioc_count_read(ioc_count
))
2252 wait_for_completion(ioc_gone
);
2257 module_init(cfq_init
);
2258 module_exit(cfq_exit
);
2260 MODULE_AUTHOR("Jens Axboe");
2261 MODULE_LICENSE("GPL");
2262 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");