ALSA: use subsys_initcall for sound core instead of module_init
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / cfq-iosched.c
blob1c9fba6b616db86d511c32741e230a76c28555fc
1 /*
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>
8 */
9 #include <linux/module.h>
10 #include <linux/blkdev.h>
11 #include <linux/elevator.h>
12 #include <linux/rbtree.h>
13 #include <linux/ioprio.h>
14 #include <linux/blktrace_api.h>
17 * tunables
19 /* max queue in one round of service */
20 static const int cfq_quantum = 4;
21 static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22 /* maximum backwards seek, in KiB */
23 static const int cfq_back_max = 16 * 1024;
24 /* penalty of a backwards seek */
25 static const int cfq_back_penalty = 2;
26 static const int cfq_slice_sync = HZ / 10;
27 static int cfq_slice_async = HZ / 25;
28 static const int cfq_slice_async_rq = 2;
29 static int cfq_slice_idle = HZ / 125;
32 * offset from end of service tree
34 #define CFQ_IDLE_DELAY (HZ / 5)
37 * below this threshold, we consider thinktime immediate
39 #define CFQ_MIN_TT (2)
42 * Allow merged cfqqs to perform this amount of seeky I/O before
43 * deciding to break the queues up again.
45 #define CFQQ_COOP_TOUT (HZ)
47 #define CFQ_SLICE_SCALE (5)
48 #define CFQ_HW_QUEUE_MIN (5)
50 #define RQ_CIC(rq) \
51 ((struct cfq_io_context *) (rq)->elevator_private)
52 #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
54 static struct kmem_cache *cfq_pool;
55 static struct kmem_cache *cfq_ioc_pool;
57 static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
58 static struct completion *ioc_gone;
59 static DEFINE_SPINLOCK(ioc_gone_lock);
61 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
62 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
63 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
65 #define sample_valid(samples) ((samples) > 80)
68 * Most of our rbtree usage is for sorting with min extraction, so
69 * if we cache the leftmost node we don't have to walk down the tree
70 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
71 * move this into the elevator for the rq sorting as well.
73 struct cfq_rb_root {
74 struct rb_root rb;
75 struct rb_node *left;
77 #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
80 * Per process-grouping structure
82 struct cfq_queue {
83 /* reference count */
84 atomic_t ref;
85 /* various state flags, see below */
86 unsigned int flags;
87 /* parent cfq_data */
88 struct cfq_data *cfqd;
89 /* service_tree member */
90 struct rb_node rb_node;
91 /* service_tree key */
92 unsigned long rb_key;
93 /* prio tree member */
94 struct rb_node p_node;
95 /* prio tree root we belong to, if any */
96 struct rb_root *p_root;
97 /* sorted list of pending requests */
98 struct rb_root sort_list;
99 /* if fifo isn't expired, next request to serve */
100 struct request *next_rq;
101 /* requests queued in sort_list */
102 int queued[2];
103 /* currently allocated requests */
104 int allocated[2];
105 /* fifo list of requests in sort_list */
106 struct list_head fifo;
108 unsigned long slice_end;
109 long slice_resid;
110 unsigned int slice_dispatch;
112 /* pending metadata requests */
113 int meta_pending;
114 /* number of requests that are on the dispatch list or inside driver */
115 int dispatched;
117 /* io prio of this group */
118 unsigned short ioprio, org_ioprio;
119 unsigned short ioprio_class, org_ioprio_class;
121 unsigned int seek_samples;
122 u64 seek_total;
123 sector_t seek_mean;
124 sector_t last_request_pos;
125 unsigned long seeky_start;
127 pid_t pid;
129 struct cfq_queue *new_cfqq;
133 * Per block device queue structure
135 struct cfq_data {
136 struct request_queue *queue;
139 * rr list of queues with requests and the count of them
141 struct cfq_rb_root service_tree;
144 * Each priority tree is sorted by next_request position. These
145 * trees are used when determining if two or more queues are
146 * interleaving requests (see cfq_close_cooperator).
148 struct rb_root prio_trees[CFQ_PRIO_LISTS];
150 unsigned int busy_queues;
152 int rq_in_driver[2];
153 int sync_flight;
156 * queue-depth detection
158 int rq_queued;
159 int hw_tag;
160 int hw_tag_samples;
161 int rq_in_driver_peak;
164 * idle window management
166 struct timer_list idle_slice_timer;
167 struct work_struct unplug_work;
169 struct cfq_queue *active_queue;
170 struct cfq_io_context *active_cic;
173 * async queue for each priority case
175 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
176 struct cfq_queue *async_idle_cfqq;
178 sector_t last_position;
181 * tunables, see top of file
183 unsigned int cfq_quantum;
184 unsigned int cfq_fifo_expire[2];
185 unsigned int cfq_back_penalty;
186 unsigned int cfq_back_max;
187 unsigned int cfq_slice[2];
188 unsigned int cfq_slice_async_rq;
189 unsigned int cfq_slice_idle;
190 unsigned int cfq_latency;
192 struct list_head cic_list;
195 * Fallback dummy cfqq for extreme OOM conditions
197 struct cfq_queue oom_cfqq;
199 unsigned long last_end_sync_rq;
202 enum cfqq_state_flags {
203 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
204 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
205 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
206 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
207 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
208 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
209 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
210 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
211 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
212 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
215 #define CFQ_CFQQ_FNS(name) \
216 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
218 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
220 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
222 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
224 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
226 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
229 CFQ_CFQQ_FNS(on_rr);
230 CFQ_CFQQ_FNS(wait_request);
231 CFQ_CFQQ_FNS(must_dispatch);
232 CFQ_CFQQ_FNS(must_alloc_slice);
233 CFQ_CFQQ_FNS(fifo_expire);
234 CFQ_CFQQ_FNS(idle_window);
235 CFQ_CFQQ_FNS(prio_changed);
236 CFQ_CFQQ_FNS(slice_new);
237 CFQ_CFQQ_FNS(sync);
238 CFQ_CFQQ_FNS(coop);
239 #undef CFQ_CFQQ_FNS
241 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
242 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
243 #define cfq_log(cfqd, fmt, args...) \
244 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
246 static void cfq_dispatch_insert(struct request_queue *, struct request *);
247 static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
248 struct io_context *, gfp_t);
249 static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
250 struct io_context *);
252 static inline int rq_in_driver(struct cfq_data *cfqd)
254 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
257 static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
258 bool is_sync)
260 return cic->cfqq[is_sync];
263 static inline void cic_set_cfqq(struct cfq_io_context *cic,
264 struct cfq_queue *cfqq, bool is_sync)
266 cic->cfqq[is_sync] = cfqq;
270 * We regard a request as SYNC, if it's either a read or has the SYNC bit
271 * set (in which case it could also be direct WRITE).
273 static inline bool cfq_bio_sync(struct bio *bio)
275 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
279 * scheduler run of queue, if there are requests pending and no one in the
280 * driver that will restart queueing
282 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
284 if (cfqd->busy_queues) {
285 cfq_log(cfqd, "schedule dispatch");
286 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
290 static int cfq_queue_empty(struct request_queue *q)
292 struct cfq_data *cfqd = q->elevator->elevator_data;
294 return !cfqd->busy_queues;
298 * Scale schedule slice based on io priority. Use the sync time slice only
299 * if a queue is marked sync and has sync io queued. A sync queue with async
300 * io only, should not get full sync slice length.
302 static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
303 unsigned short prio)
305 const int base_slice = cfqd->cfq_slice[sync];
307 WARN_ON(prio >= IOPRIO_BE_NR);
309 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
312 static inline int
313 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
315 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
318 static inline void
319 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
321 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
322 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
326 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
327 * isn't valid until the first request from the dispatch is activated
328 * and the slice time set.
330 static inline bool cfq_slice_used(struct cfq_queue *cfqq)
332 if (cfq_cfqq_slice_new(cfqq))
333 return 0;
334 if (time_before(jiffies, cfqq->slice_end))
335 return 0;
337 return 1;
341 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
342 * We choose the request that is closest to the head right now. Distance
343 * behind the head is penalized and only allowed to a certain extent.
345 static struct request *
346 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
348 sector_t last, s1, s2, d1 = 0, d2 = 0;
349 unsigned long back_max;
350 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
351 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
352 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
354 if (rq1 == NULL || rq1 == rq2)
355 return rq2;
356 if (rq2 == NULL)
357 return rq1;
359 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
360 return rq1;
361 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
362 return rq2;
363 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
364 return rq1;
365 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
366 return rq2;
368 s1 = blk_rq_pos(rq1);
369 s2 = blk_rq_pos(rq2);
371 last = cfqd->last_position;
374 * by definition, 1KiB is 2 sectors
376 back_max = cfqd->cfq_back_max * 2;
379 * Strict one way elevator _except_ in the case where we allow
380 * short backward seeks which are biased as twice the cost of a
381 * similar forward seek.
383 if (s1 >= last)
384 d1 = s1 - last;
385 else if (s1 + back_max >= last)
386 d1 = (last - s1) * cfqd->cfq_back_penalty;
387 else
388 wrap |= CFQ_RQ1_WRAP;
390 if (s2 >= last)
391 d2 = s2 - last;
392 else if (s2 + back_max >= last)
393 d2 = (last - s2) * cfqd->cfq_back_penalty;
394 else
395 wrap |= CFQ_RQ2_WRAP;
397 /* Found required data */
400 * By doing switch() on the bit mask "wrap" we avoid having to
401 * check two variables for all permutations: --> faster!
403 switch (wrap) {
404 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
405 if (d1 < d2)
406 return rq1;
407 else if (d2 < d1)
408 return rq2;
409 else {
410 if (s1 >= s2)
411 return rq1;
412 else
413 return rq2;
416 case CFQ_RQ2_WRAP:
417 return rq1;
418 case CFQ_RQ1_WRAP:
419 return rq2;
420 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
421 default:
423 * Since both rqs are wrapped,
424 * start with the one that's further behind head
425 * (--> only *one* back seek required),
426 * since back seek takes more time than forward.
428 if (s1 <= s2)
429 return rq1;
430 else
431 return rq2;
436 * The below is leftmost cache rbtree addon
438 static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
440 if (!root->left)
441 root->left = rb_first(&root->rb);
443 if (root->left)
444 return rb_entry(root->left, struct cfq_queue, rb_node);
446 return NULL;
449 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
451 rb_erase(n, root);
452 RB_CLEAR_NODE(n);
455 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
457 if (root->left == n)
458 root->left = NULL;
459 rb_erase_init(n, &root->rb);
463 * would be nice to take fifo expire time into account as well
465 static struct request *
466 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
467 struct request *last)
469 struct rb_node *rbnext = rb_next(&last->rb_node);
470 struct rb_node *rbprev = rb_prev(&last->rb_node);
471 struct request *next = NULL, *prev = NULL;
473 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
475 if (rbprev)
476 prev = rb_entry_rq(rbprev);
478 if (rbnext)
479 next = rb_entry_rq(rbnext);
480 else {
481 rbnext = rb_first(&cfqq->sort_list);
482 if (rbnext && rbnext != &last->rb_node)
483 next = rb_entry_rq(rbnext);
486 return cfq_choose_req(cfqd, next, prev);
489 static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
490 struct cfq_queue *cfqq)
493 * just an approximation, should be ok.
495 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
496 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
500 * The cfqd->service_tree holds all pending cfq_queue's that have
501 * requests waiting to be processed. It is sorted in the order that
502 * we will service the queues.
504 static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
505 bool add_front)
507 struct rb_node **p, *parent;
508 struct cfq_queue *__cfqq;
509 unsigned long rb_key;
510 int left;
512 if (cfq_class_idle(cfqq)) {
513 rb_key = CFQ_IDLE_DELAY;
514 parent = rb_last(&cfqd->service_tree.rb);
515 if (parent && parent != &cfqq->rb_node) {
516 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
517 rb_key += __cfqq->rb_key;
518 } else
519 rb_key += jiffies;
520 } else if (!add_front) {
522 * Get our rb key offset. Subtract any residual slice
523 * value carried from last service. A negative resid
524 * count indicates slice overrun, and this should position
525 * the next service time further away in the tree.
527 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
528 rb_key -= cfqq->slice_resid;
529 cfqq->slice_resid = 0;
530 } else {
531 rb_key = -HZ;
532 __cfqq = cfq_rb_first(&cfqd->service_tree);
533 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
536 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
538 * same position, nothing more to do
540 if (rb_key == cfqq->rb_key)
541 return;
543 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
546 left = 1;
547 parent = NULL;
548 p = &cfqd->service_tree.rb.rb_node;
549 while (*p) {
550 struct rb_node **n;
552 parent = *p;
553 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
556 * sort RT queues first, we always want to give
557 * preference to them. IDLE queues goes to the back.
558 * after that, sort on the next service time.
560 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
561 n = &(*p)->rb_left;
562 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
563 n = &(*p)->rb_right;
564 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
565 n = &(*p)->rb_left;
566 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
567 n = &(*p)->rb_right;
568 else if (time_before(rb_key, __cfqq->rb_key))
569 n = &(*p)->rb_left;
570 else
571 n = &(*p)->rb_right;
573 if (n == &(*p)->rb_right)
574 left = 0;
576 p = n;
579 if (left)
580 cfqd->service_tree.left = &cfqq->rb_node;
582 cfqq->rb_key = rb_key;
583 rb_link_node(&cfqq->rb_node, parent, p);
584 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
587 static struct cfq_queue *
588 cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
589 sector_t sector, struct rb_node **ret_parent,
590 struct rb_node ***rb_link)
592 struct rb_node **p, *parent;
593 struct cfq_queue *cfqq = NULL;
595 parent = NULL;
596 p = &root->rb_node;
597 while (*p) {
598 struct rb_node **n;
600 parent = *p;
601 cfqq = rb_entry(parent, struct cfq_queue, p_node);
604 * Sort strictly based on sector. Smallest to the left,
605 * largest to the right.
607 if (sector > blk_rq_pos(cfqq->next_rq))
608 n = &(*p)->rb_right;
609 else if (sector < blk_rq_pos(cfqq->next_rq))
610 n = &(*p)->rb_left;
611 else
612 break;
613 p = n;
614 cfqq = NULL;
617 *ret_parent = parent;
618 if (rb_link)
619 *rb_link = p;
620 return cfqq;
623 static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
625 struct rb_node **p, *parent;
626 struct cfq_queue *__cfqq;
628 if (cfqq->p_root) {
629 rb_erase(&cfqq->p_node, cfqq->p_root);
630 cfqq->p_root = NULL;
633 if (cfq_class_idle(cfqq))
634 return;
635 if (!cfqq->next_rq)
636 return;
638 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
639 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
640 blk_rq_pos(cfqq->next_rq), &parent, &p);
641 if (!__cfqq) {
642 rb_link_node(&cfqq->p_node, parent, p);
643 rb_insert_color(&cfqq->p_node, cfqq->p_root);
644 } else
645 cfqq->p_root = NULL;
649 * Update cfqq's position in the service tree.
651 static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
654 * Resorting requires the cfqq to be on the RR list already.
656 if (cfq_cfqq_on_rr(cfqq)) {
657 cfq_service_tree_add(cfqd, cfqq, 0);
658 cfq_prio_tree_add(cfqd, cfqq);
663 * add to busy list of queues for service, trying to be fair in ordering
664 * the pending list according to last request service
666 static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
668 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
669 BUG_ON(cfq_cfqq_on_rr(cfqq));
670 cfq_mark_cfqq_on_rr(cfqq);
671 cfqd->busy_queues++;
673 cfq_resort_rr_list(cfqd, cfqq);
677 * Called when the cfqq no longer has requests pending, remove it from
678 * the service tree.
680 static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
682 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
683 BUG_ON(!cfq_cfqq_on_rr(cfqq));
684 cfq_clear_cfqq_on_rr(cfqq);
686 if (!RB_EMPTY_NODE(&cfqq->rb_node))
687 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
688 if (cfqq->p_root) {
689 rb_erase(&cfqq->p_node, cfqq->p_root);
690 cfqq->p_root = NULL;
693 BUG_ON(!cfqd->busy_queues);
694 cfqd->busy_queues--;
698 * rb tree support functions
700 static void cfq_del_rq_rb(struct request *rq)
702 struct cfq_queue *cfqq = RQ_CFQQ(rq);
703 struct cfq_data *cfqd = cfqq->cfqd;
704 const int sync = rq_is_sync(rq);
706 BUG_ON(!cfqq->queued[sync]);
707 cfqq->queued[sync]--;
709 elv_rb_del(&cfqq->sort_list, rq);
711 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
712 cfq_del_cfqq_rr(cfqd, cfqq);
715 static void cfq_add_rq_rb(struct request *rq)
717 struct cfq_queue *cfqq = RQ_CFQQ(rq);
718 struct cfq_data *cfqd = cfqq->cfqd;
719 struct request *__alias, *prev;
721 cfqq->queued[rq_is_sync(rq)]++;
724 * looks a little odd, but the first insert might return an alias.
725 * if that happens, put the alias on the dispatch list
727 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
728 cfq_dispatch_insert(cfqd->queue, __alias);
730 if (!cfq_cfqq_on_rr(cfqq))
731 cfq_add_cfqq_rr(cfqd, cfqq);
734 * check if this request is a better next-serve candidate
736 prev = cfqq->next_rq;
737 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
740 * adjust priority tree position, if ->next_rq changes
742 if (prev != cfqq->next_rq)
743 cfq_prio_tree_add(cfqd, cfqq);
745 BUG_ON(!cfqq->next_rq);
748 static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
750 elv_rb_del(&cfqq->sort_list, rq);
751 cfqq->queued[rq_is_sync(rq)]--;
752 cfq_add_rq_rb(rq);
755 static struct request *
756 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
758 struct task_struct *tsk = current;
759 struct cfq_io_context *cic;
760 struct cfq_queue *cfqq;
762 cic = cfq_cic_lookup(cfqd, tsk->io_context);
763 if (!cic)
764 return NULL;
766 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
767 if (cfqq) {
768 sector_t sector = bio->bi_sector + bio_sectors(bio);
770 return elv_rb_find(&cfqq->sort_list, sector);
773 return NULL;
776 static void cfq_activate_request(struct request_queue *q, struct request *rq)
778 struct cfq_data *cfqd = q->elevator->elevator_data;
780 cfqd->rq_in_driver[rq_is_sync(rq)]++;
781 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
782 rq_in_driver(cfqd));
784 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
787 static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
789 struct cfq_data *cfqd = q->elevator->elevator_data;
790 const int sync = rq_is_sync(rq);
792 WARN_ON(!cfqd->rq_in_driver[sync]);
793 cfqd->rq_in_driver[sync]--;
794 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
795 rq_in_driver(cfqd));
798 static void cfq_remove_request(struct request *rq)
800 struct cfq_queue *cfqq = RQ_CFQQ(rq);
802 if (cfqq->next_rq == rq)
803 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
805 list_del_init(&rq->queuelist);
806 cfq_del_rq_rb(rq);
808 cfqq->cfqd->rq_queued--;
809 if (rq_is_meta(rq)) {
810 WARN_ON(!cfqq->meta_pending);
811 cfqq->meta_pending--;
815 static int cfq_merge(struct request_queue *q, struct request **req,
816 struct bio *bio)
818 struct cfq_data *cfqd = q->elevator->elevator_data;
819 struct request *__rq;
821 __rq = cfq_find_rq_fmerge(cfqd, bio);
822 if (__rq && elv_rq_merge_ok(__rq, bio)) {
823 *req = __rq;
824 return ELEVATOR_FRONT_MERGE;
827 return ELEVATOR_NO_MERGE;
830 static void cfq_merged_request(struct request_queue *q, struct request *req,
831 int type)
833 if (type == ELEVATOR_FRONT_MERGE) {
834 struct cfq_queue *cfqq = RQ_CFQQ(req);
836 cfq_reposition_rq_rb(cfqq, req);
840 static void
841 cfq_merged_requests(struct request_queue *q, struct request *rq,
842 struct request *next)
845 * reposition in fifo if next is older than rq
847 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
848 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
849 list_move(&rq->queuelist, &next->queuelist);
850 rq_set_fifo_time(rq, rq_fifo_time(next));
853 cfq_remove_request(next);
856 static int cfq_allow_merge(struct request_queue *q, struct request *rq,
857 struct bio *bio)
859 struct cfq_data *cfqd = q->elevator->elevator_data;
860 struct cfq_io_context *cic;
861 struct cfq_queue *cfqq;
864 * Disallow merge of a sync bio into an async request.
866 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
867 return false;
870 * Lookup the cfqq that this bio will be queued with. Allow
871 * merge only if rq is queued there.
873 cic = cfq_cic_lookup(cfqd, current->io_context);
874 if (!cic)
875 return false;
877 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
878 return cfqq == RQ_CFQQ(rq);
881 static void __cfq_set_active_queue(struct cfq_data *cfqd,
882 struct cfq_queue *cfqq)
884 if (cfqq) {
885 cfq_log_cfqq(cfqd, cfqq, "set_active");
886 cfqq->slice_end = 0;
887 cfqq->slice_dispatch = 0;
889 cfq_clear_cfqq_wait_request(cfqq);
890 cfq_clear_cfqq_must_dispatch(cfqq);
891 cfq_clear_cfqq_must_alloc_slice(cfqq);
892 cfq_clear_cfqq_fifo_expire(cfqq);
893 cfq_mark_cfqq_slice_new(cfqq);
895 del_timer(&cfqd->idle_slice_timer);
898 cfqd->active_queue = cfqq;
902 * current cfqq expired its slice (or was too idle), select new one
904 static void
905 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
906 bool timed_out)
908 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
910 if (cfq_cfqq_wait_request(cfqq))
911 del_timer(&cfqd->idle_slice_timer);
913 cfq_clear_cfqq_wait_request(cfqq);
916 * store what was left of this slice, if the queue idled/timed out
918 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
919 cfqq->slice_resid = cfqq->slice_end - jiffies;
920 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
923 cfq_resort_rr_list(cfqd, cfqq);
925 if (cfqq == cfqd->active_queue)
926 cfqd->active_queue = NULL;
928 if (cfqd->active_cic) {
929 put_io_context(cfqd->active_cic->ioc);
930 cfqd->active_cic = NULL;
934 static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
936 struct cfq_queue *cfqq = cfqd->active_queue;
938 if (cfqq)
939 __cfq_slice_expired(cfqd, cfqq, timed_out);
943 * Get next queue for service. Unless we have a queue preemption,
944 * we'll simply select the first cfqq in the service tree.
946 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
948 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
949 return NULL;
951 return cfq_rb_first(&cfqd->service_tree);
955 * Get and set a new active queue for service.
957 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
958 struct cfq_queue *cfqq)
960 if (!cfqq)
961 cfqq = cfq_get_next_queue(cfqd);
963 __cfq_set_active_queue(cfqd, cfqq);
964 return cfqq;
967 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
968 struct request *rq)
970 if (blk_rq_pos(rq) >= cfqd->last_position)
971 return blk_rq_pos(rq) - cfqd->last_position;
972 else
973 return cfqd->last_position - blk_rq_pos(rq);
976 #define CFQQ_SEEK_THR 8 * 1024
977 #define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
979 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
980 struct request *rq)
982 sector_t sdist = cfqq->seek_mean;
984 if (!sample_valid(cfqq->seek_samples))
985 sdist = CFQQ_SEEK_THR;
987 return cfq_dist_from_last(cfqd, rq) <= sdist;
990 static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
991 struct cfq_queue *cur_cfqq)
993 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
994 struct rb_node *parent, *node;
995 struct cfq_queue *__cfqq;
996 sector_t sector = cfqd->last_position;
998 if (RB_EMPTY_ROOT(root))
999 return NULL;
1002 * First, if we find a request starting at the end of the last
1003 * request, choose it.
1005 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
1006 if (__cfqq)
1007 return __cfqq;
1010 * If the exact sector wasn't found, the parent of the NULL leaf
1011 * will contain the closest sector.
1013 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
1014 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1015 return __cfqq;
1017 if (blk_rq_pos(__cfqq->next_rq) < sector)
1018 node = rb_next(&__cfqq->p_node);
1019 else
1020 node = rb_prev(&__cfqq->p_node);
1021 if (!node)
1022 return NULL;
1024 __cfqq = rb_entry(node, struct cfq_queue, p_node);
1025 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1026 return __cfqq;
1028 return NULL;
1032 * cfqd - obvious
1033 * cur_cfqq - passed in so that we don't decide that the current queue is
1034 * closely cooperating with itself.
1036 * So, basically we're assuming that that cur_cfqq has dispatched at least
1037 * one request, and that cfqd->last_position reflects a position on the disk
1038 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1039 * assumption.
1041 static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1042 struct cfq_queue *cur_cfqq)
1044 struct cfq_queue *cfqq;
1046 if (!cfq_cfqq_sync(cur_cfqq))
1047 return NULL;
1048 if (CFQQ_SEEKY(cur_cfqq))
1049 return NULL;
1052 * We should notice if some of the queues are cooperating, eg
1053 * working closely on the same area of the disk. In that case,
1054 * we can group them together and don't waste time idling.
1056 cfqq = cfqq_close(cfqd, cur_cfqq);
1057 if (!cfqq)
1058 return NULL;
1061 * It only makes sense to merge sync queues.
1063 if (!cfq_cfqq_sync(cfqq))
1064 return NULL;
1065 if (CFQQ_SEEKY(cfqq))
1066 return NULL;
1068 return cfqq;
1071 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1073 struct cfq_queue *cfqq = cfqd->active_queue;
1074 struct cfq_io_context *cic;
1075 unsigned long sl;
1078 * SSD device without seek penalty, disable idling. But only do so
1079 * for devices that support queuing, otherwise we still have a problem
1080 * with sync vs async workloads.
1082 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
1083 return;
1085 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
1086 WARN_ON(cfq_cfqq_slice_new(cfqq));
1089 * idle is disabled, either manually or by past process history
1091 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
1092 return;
1095 * still requests with the driver, don't idle
1097 if (rq_in_driver(cfqd))
1098 return;
1101 * task has exited, don't wait
1103 cic = cfqd->active_cic;
1104 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
1105 return;
1108 * If our average think time is larger than the remaining time
1109 * slice, then don't idle. This avoids overrunning the allotted
1110 * time slice.
1112 if (sample_valid(cic->ttime_samples) &&
1113 (cfqq->slice_end - jiffies < cic->ttime_mean))
1114 return;
1116 cfq_mark_cfqq_wait_request(cfqq);
1119 * we don't want to idle for seeks, but we do want to allow
1120 * fair distribution of slice time for a process doing back-to-back
1121 * seeks. so allow a little bit of time for him to submit a new rq
1123 sl = cfqd->cfq_slice_idle;
1124 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
1125 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
1127 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1128 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
1132 * Move request from internal lists to the request queue dispatch list.
1134 static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1136 struct cfq_data *cfqd = q->elevator->elevator_data;
1137 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1139 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1141 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
1142 cfq_remove_request(rq);
1143 cfqq->dispatched++;
1144 elv_dispatch_sort(q, rq);
1146 if (cfq_cfqq_sync(cfqq))
1147 cfqd->sync_flight++;
1151 * return expired entry, or NULL to just start from scratch in rbtree
1153 static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1155 struct request *rq = NULL;
1157 if (cfq_cfqq_fifo_expire(cfqq))
1158 return NULL;
1160 cfq_mark_cfqq_fifo_expire(cfqq);
1162 if (list_empty(&cfqq->fifo))
1163 return NULL;
1165 rq = rq_entry_fifo(cfqq->fifo.next);
1166 if (time_before(jiffies, rq_fifo_time(rq)))
1167 rq = NULL;
1169 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
1170 return rq;
1173 static inline int
1174 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1176 const int base_rq = cfqd->cfq_slice_async_rq;
1178 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1180 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1184 * Must be called with the queue_lock held.
1186 static int cfqq_process_refs(struct cfq_queue *cfqq)
1188 int process_refs, io_refs;
1190 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1191 process_refs = atomic_read(&cfqq->ref) - io_refs;
1192 BUG_ON(process_refs < 0);
1193 return process_refs;
1196 static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1198 int process_refs, new_process_refs;
1199 struct cfq_queue *__cfqq;
1202 * If there are no process references on the new_cfqq, then it is
1203 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
1204 * chain may have dropped their last reference (not just their
1205 * last process reference).
1207 if (!cfqq_process_refs(new_cfqq))
1208 return;
1210 /* Avoid a circular list and skip interim queue merges */
1211 while ((__cfqq = new_cfqq->new_cfqq)) {
1212 if (__cfqq == cfqq)
1213 return;
1214 new_cfqq = __cfqq;
1217 process_refs = cfqq_process_refs(cfqq);
1218 new_process_refs = cfqq_process_refs(new_cfqq);
1220 * If the process for the cfqq has gone away, there is no
1221 * sense in merging the queues.
1223 if (process_refs == 0 || new_process_refs == 0)
1224 return;
1227 * Merge in the direction of the lesser amount of work.
1229 if (new_process_refs >= process_refs) {
1230 cfqq->new_cfqq = new_cfqq;
1231 atomic_add(process_refs, &new_cfqq->ref);
1232 } else {
1233 new_cfqq->new_cfqq = cfqq;
1234 atomic_add(new_process_refs, &cfqq->ref);
1239 * Select a queue for service. If we have a current active queue,
1240 * check whether to continue servicing it, or retrieve and set a new one.
1242 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1244 struct cfq_queue *cfqq, *new_cfqq = NULL;
1246 cfqq = cfqd->active_queue;
1247 if (!cfqq)
1248 goto new_queue;
1251 * The active queue has run out of time, expire it and select new.
1253 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
1254 goto expire;
1257 * The active queue has requests and isn't expired, allow it to
1258 * dispatch.
1260 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
1261 goto keep_queue;
1264 * If another queue has a request waiting within our mean seek
1265 * distance, let it run. The expire code will check for close
1266 * cooperators and put the close queue at the front of the service
1267 * tree. If possible, merge the expiring queue with the new cfqq.
1269 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
1270 if (new_cfqq) {
1271 if (!cfqq->new_cfqq)
1272 cfq_setup_merge(cfqq, new_cfqq);
1273 goto expire;
1277 * No requests pending. If the active queue still has requests in
1278 * flight or is idling for a new request, allow either of these
1279 * conditions to happen (or time out) before selecting a new queue.
1281 if (timer_pending(&cfqd->idle_slice_timer) ||
1282 (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
1283 cfqq = NULL;
1284 goto keep_queue;
1287 expire:
1288 cfq_slice_expired(cfqd, 0);
1289 new_queue:
1290 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
1291 keep_queue:
1292 return cfqq;
1295 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1297 int dispatched = 0;
1299 while (cfqq->next_rq) {
1300 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1301 dispatched++;
1304 BUG_ON(!list_empty(&cfqq->fifo));
1305 return dispatched;
1309 * Drain our current requests. Used for barriers and when switching
1310 * io schedulers on-the-fly.
1312 static int cfq_forced_dispatch(struct cfq_data *cfqd)
1314 struct cfq_queue *cfqq;
1315 int dispatched = 0;
1317 while ((cfqq = cfq_rb_first(&cfqd->service_tree)) != NULL)
1318 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1320 cfq_slice_expired(cfqd, 0);
1322 BUG_ON(cfqd->busy_queues);
1324 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1325 return dispatched;
1328 static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1330 unsigned int max_dispatch;
1333 * Drain async requests before we start sync IO
1335 if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
1336 return false;
1339 * If this is an async queue and we have sync IO in flight, let it wait
1341 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
1342 return false;
1344 max_dispatch = cfqd->cfq_quantum;
1345 if (cfq_class_idle(cfqq))
1346 max_dispatch = 1;
1349 * Does this cfqq already have too much IO in flight?
1351 if (cfqq->dispatched >= max_dispatch) {
1353 * idle queue must always only have a single IO in flight
1355 if (cfq_class_idle(cfqq))
1356 return false;
1359 * We have other queues, don't allow more IO from this one
1361 if (cfqd->busy_queues > 1)
1362 return false;
1365 * Sole queue user, allow bigger slice
1367 max_dispatch *= 4;
1371 * Async queues must wait a bit before being allowed dispatch.
1372 * We also ramp up the dispatch depth gradually for async IO,
1373 * based on the last sync IO we serviced
1375 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
1376 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
1377 unsigned int depth;
1379 depth = last_sync / cfqd->cfq_slice[1];
1380 if (!depth && !cfqq->dispatched)
1381 depth = 1;
1382 if (depth < max_dispatch)
1383 max_dispatch = depth;
1387 * If we're below the current max, allow a dispatch
1389 return cfqq->dispatched < max_dispatch;
1393 * Dispatch a request from cfqq, moving them to the request queue
1394 * dispatch list.
1396 static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1398 struct request *rq;
1400 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1402 if (!cfq_may_dispatch(cfqd, cfqq))
1403 return false;
1406 * follow expired path, else get first next available
1408 rq = cfq_check_fifo(cfqq);
1409 if (!rq)
1410 rq = cfqq->next_rq;
1413 * insert request into driver dispatch list
1415 cfq_dispatch_insert(cfqd->queue, rq);
1417 if (!cfqd->active_cic) {
1418 struct cfq_io_context *cic = RQ_CIC(rq);
1420 atomic_long_inc(&cic->ioc->refcount);
1421 cfqd->active_cic = cic;
1424 return true;
1428 * Find the cfqq that we need to service and move a request from that to the
1429 * dispatch list
1431 static int cfq_dispatch_requests(struct request_queue *q, int force)
1433 struct cfq_data *cfqd = q->elevator->elevator_data;
1434 struct cfq_queue *cfqq;
1436 if (!cfqd->busy_queues)
1437 return 0;
1439 if (unlikely(force))
1440 return cfq_forced_dispatch(cfqd);
1442 cfqq = cfq_select_queue(cfqd);
1443 if (!cfqq)
1444 return 0;
1447 * Dispatch a request from this cfqq, if it is allowed
1449 if (!cfq_dispatch_request(cfqd, cfqq))
1450 return 0;
1452 cfqq->slice_dispatch++;
1453 cfq_clear_cfqq_must_dispatch(cfqq);
1456 * expire an async queue immediately if it has used up its slice. idle
1457 * queue always expire after 1 dispatch round.
1459 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1460 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1461 cfq_class_idle(cfqq))) {
1462 cfqq->slice_end = jiffies + 1;
1463 cfq_slice_expired(cfqd, 0);
1466 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
1467 return 1;
1471 * task holds one reference to the queue, dropped when task exits. each rq
1472 * in-flight on this queue also holds a reference, dropped when rq is freed.
1474 * queue lock must be held here.
1476 static void cfq_put_queue(struct cfq_queue *cfqq)
1478 struct cfq_data *cfqd = cfqq->cfqd;
1480 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1482 if (!atomic_dec_and_test(&cfqq->ref))
1483 return;
1485 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1486 BUG_ON(rb_first(&cfqq->sort_list));
1487 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1488 BUG_ON(cfq_cfqq_on_rr(cfqq));
1490 if (unlikely(cfqd->active_queue == cfqq)) {
1491 __cfq_slice_expired(cfqd, cfqq, 0);
1492 cfq_schedule_dispatch(cfqd);
1495 kmem_cache_free(cfq_pool, cfqq);
1499 * Must always be called with the rcu_read_lock() held
1501 static void
1502 __call_for_each_cic(struct io_context *ioc,
1503 void (*func)(struct io_context *, struct cfq_io_context *))
1505 struct cfq_io_context *cic;
1506 struct hlist_node *n;
1508 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
1509 func(ioc, cic);
1513 * Call func for each cic attached to this ioc.
1515 static void
1516 call_for_each_cic(struct io_context *ioc,
1517 void (*func)(struct io_context *, struct cfq_io_context *))
1519 rcu_read_lock();
1520 __call_for_each_cic(ioc, func);
1521 rcu_read_unlock();
1524 static void cfq_cic_free_rcu(struct rcu_head *head)
1526 struct cfq_io_context *cic;
1528 cic = container_of(head, struct cfq_io_context, rcu_head);
1530 kmem_cache_free(cfq_ioc_pool, cic);
1531 elv_ioc_count_dec(cfq_ioc_count);
1533 if (ioc_gone) {
1535 * CFQ scheduler is exiting, grab exit lock and check
1536 * the pending io context count. If it hits zero,
1537 * complete ioc_gone and set it back to NULL
1539 spin_lock(&ioc_gone_lock);
1540 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
1541 complete(ioc_gone);
1542 ioc_gone = NULL;
1544 spin_unlock(&ioc_gone_lock);
1548 static void cfq_cic_free(struct cfq_io_context *cic)
1550 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
1553 static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
1555 unsigned long flags;
1557 BUG_ON(!cic->dead_key);
1559 spin_lock_irqsave(&ioc->lock, flags);
1560 radix_tree_delete(&ioc->radix_root, cic->dead_key);
1561 hlist_del_rcu(&cic->cic_list);
1562 spin_unlock_irqrestore(&ioc->lock, flags);
1564 cfq_cic_free(cic);
1568 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
1569 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
1570 * and ->trim() which is called with the task lock held
1572 static void cfq_free_io_context(struct io_context *ioc)
1575 * ioc->refcount is zero here, or we are called from elv_unregister(),
1576 * so no more cic's are allowed to be linked into this ioc. So it
1577 * should be ok to iterate over the known list, we will see all cic's
1578 * since no new ones are added.
1580 __call_for_each_cic(ioc, cic_free_func);
1583 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1585 struct cfq_queue *__cfqq, *next;
1587 if (unlikely(cfqq == cfqd->active_queue)) {
1588 __cfq_slice_expired(cfqd, cfqq, 0);
1589 cfq_schedule_dispatch(cfqd);
1593 * If this queue was scheduled to merge with another queue, be
1594 * sure to drop the reference taken on that queue (and others in
1595 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
1597 __cfqq = cfqq->new_cfqq;
1598 while (__cfqq) {
1599 if (__cfqq == cfqq) {
1600 WARN(1, "cfqq->new_cfqq loop detected\n");
1601 break;
1603 next = __cfqq->new_cfqq;
1604 cfq_put_queue(__cfqq);
1605 __cfqq = next;
1608 cfq_put_queue(cfqq);
1611 static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1612 struct cfq_io_context *cic)
1614 struct io_context *ioc = cic->ioc;
1616 list_del_init(&cic->queue_list);
1619 * Make sure key == NULL is seen for dead queues
1621 smp_wmb();
1622 cic->dead_key = (unsigned long) cic->key;
1623 cic->key = NULL;
1625 rcu_read_lock();
1626 if (rcu_dereference(ioc->ioc_data) == cic) {
1627 rcu_read_unlock();
1628 spin_lock(&ioc->lock);
1629 rcu_assign_pointer(ioc->ioc_data, NULL);
1630 spin_unlock(&ioc->lock);
1631 } else
1632 rcu_read_unlock();
1634 if (cic->cfqq[BLK_RW_ASYNC]) {
1635 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
1636 cic->cfqq[BLK_RW_ASYNC] = NULL;
1639 if (cic->cfqq[BLK_RW_SYNC]) {
1640 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
1641 cic->cfqq[BLK_RW_SYNC] = NULL;
1645 static void cfq_exit_single_io_context(struct io_context *ioc,
1646 struct cfq_io_context *cic)
1648 struct cfq_data *cfqd = cic->key;
1650 if (cfqd) {
1651 struct request_queue *q = cfqd->queue;
1652 unsigned long flags;
1654 spin_lock_irqsave(q->queue_lock, flags);
1657 * Ensure we get a fresh copy of the ->key to prevent
1658 * race between exiting task and queue
1660 smp_read_barrier_depends();
1661 if (cic->key)
1662 __cfq_exit_single_io_context(cfqd, cic);
1664 spin_unlock_irqrestore(q->queue_lock, flags);
1669 * The process that ioc belongs to has exited, we need to clean up
1670 * and put the internal structures we have that belongs to that process.
1672 static void cfq_exit_io_context(struct io_context *ioc)
1674 call_for_each_cic(ioc, cfq_exit_single_io_context);
1677 static struct cfq_io_context *
1678 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1680 struct cfq_io_context *cic;
1682 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
1683 cfqd->queue->node);
1684 if (cic) {
1685 cic->last_end_request = jiffies;
1686 INIT_LIST_HEAD(&cic->queue_list);
1687 INIT_HLIST_NODE(&cic->cic_list);
1688 cic->dtor = cfq_free_io_context;
1689 cic->exit = cfq_exit_io_context;
1690 elv_ioc_count_inc(cfq_ioc_count);
1693 return cic;
1696 static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
1698 struct task_struct *tsk = current;
1699 int ioprio_class;
1701 if (!cfq_cfqq_prio_changed(cfqq))
1702 return;
1704 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
1705 switch (ioprio_class) {
1706 default:
1707 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1708 case IOPRIO_CLASS_NONE:
1710 * no prio set, inherit CPU scheduling settings
1712 cfqq->ioprio = task_nice_ioprio(tsk);
1713 cfqq->ioprio_class = task_nice_ioclass(tsk);
1714 break;
1715 case IOPRIO_CLASS_RT:
1716 cfqq->ioprio = task_ioprio(ioc);
1717 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1718 break;
1719 case IOPRIO_CLASS_BE:
1720 cfqq->ioprio = task_ioprio(ioc);
1721 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1722 break;
1723 case IOPRIO_CLASS_IDLE:
1724 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1725 cfqq->ioprio = 7;
1726 cfq_clear_cfqq_idle_window(cfqq);
1727 break;
1731 * keep track of original prio settings in case we have to temporarily
1732 * elevate the priority of this queue
1734 cfqq->org_ioprio = cfqq->ioprio;
1735 cfqq->org_ioprio_class = cfqq->ioprio_class;
1736 cfq_clear_cfqq_prio_changed(cfqq);
1739 static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
1741 struct cfq_data *cfqd = cic->key;
1742 struct cfq_queue *cfqq;
1743 unsigned long flags;
1745 if (unlikely(!cfqd))
1746 return;
1748 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1750 cfqq = cic->cfqq[BLK_RW_ASYNC];
1751 if (cfqq) {
1752 struct cfq_queue *new_cfqq;
1753 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
1754 GFP_ATOMIC);
1755 if (new_cfqq) {
1756 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
1757 cfq_put_queue(cfqq);
1761 cfqq = cic->cfqq[BLK_RW_SYNC];
1762 if (cfqq)
1763 cfq_mark_cfqq_prio_changed(cfqq);
1765 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1768 static void cfq_ioc_set_ioprio(struct io_context *ioc)
1770 call_for_each_cic(ioc, changed_ioprio);
1771 ioc->ioprio_changed = 0;
1774 static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1775 pid_t pid, bool is_sync)
1777 RB_CLEAR_NODE(&cfqq->rb_node);
1778 RB_CLEAR_NODE(&cfqq->p_node);
1779 INIT_LIST_HEAD(&cfqq->fifo);
1781 atomic_set(&cfqq->ref, 0);
1782 cfqq->cfqd = cfqd;
1784 cfq_mark_cfqq_prio_changed(cfqq);
1786 if (is_sync) {
1787 if (!cfq_class_idle(cfqq))
1788 cfq_mark_cfqq_idle_window(cfqq);
1789 cfq_mark_cfqq_sync(cfqq);
1791 cfqq->pid = pid;
1794 static struct cfq_queue *
1795 cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
1796 struct io_context *ioc, gfp_t gfp_mask)
1798 struct cfq_queue *cfqq, *new_cfqq = NULL;
1799 struct cfq_io_context *cic;
1801 retry:
1802 cic = cfq_cic_lookup(cfqd, ioc);
1803 /* cic always exists here */
1804 cfqq = cic_to_cfqq(cic, is_sync);
1807 * Always try a new alloc if we fell back to the OOM cfqq
1808 * originally, since it should just be a temporary situation.
1810 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
1811 cfqq = NULL;
1812 if (new_cfqq) {
1813 cfqq = new_cfqq;
1814 new_cfqq = NULL;
1815 } else if (gfp_mask & __GFP_WAIT) {
1816 spin_unlock_irq(cfqd->queue->queue_lock);
1817 new_cfqq = kmem_cache_alloc_node(cfq_pool,
1818 gfp_mask | __GFP_ZERO,
1819 cfqd->queue->node);
1820 spin_lock_irq(cfqd->queue->queue_lock);
1821 if (new_cfqq)
1822 goto retry;
1823 } else {
1824 cfqq = kmem_cache_alloc_node(cfq_pool,
1825 gfp_mask | __GFP_ZERO,
1826 cfqd->queue->node);
1829 if (cfqq) {
1830 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
1831 cfq_init_prio_data(cfqq, ioc);
1832 cfq_log_cfqq(cfqd, cfqq, "alloced");
1833 } else
1834 cfqq = &cfqd->oom_cfqq;
1837 if (new_cfqq)
1838 kmem_cache_free(cfq_pool, new_cfqq);
1840 return cfqq;
1843 static struct cfq_queue **
1844 cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
1846 switch (ioprio_class) {
1847 case IOPRIO_CLASS_RT:
1848 return &cfqd->async_cfqq[0][ioprio];
1849 case IOPRIO_CLASS_BE:
1850 return &cfqd->async_cfqq[1][ioprio];
1851 case IOPRIO_CLASS_IDLE:
1852 return &cfqd->async_idle_cfqq;
1853 default:
1854 BUG();
1858 static struct cfq_queue *
1859 cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
1860 gfp_t gfp_mask)
1862 const int ioprio = task_ioprio(ioc);
1863 const int ioprio_class = task_ioprio_class(ioc);
1864 struct cfq_queue **async_cfqq = NULL;
1865 struct cfq_queue *cfqq = NULL;
1867 if (!is_sync) {
1868 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
1869 cfqq = *async_cfqq;
1872 if (!cfqq)
1873 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
1876 * pin the queue now that it's allocated, scheduler exit will prune it
1878 if (!is_sync && !(*async_cfqq)) {
1879 atomic_inc(&cfqq->ref);
1880 *async_cfqq = cfqq;
1883 atomic_inc(&cfqq->ref);
1884 return cfqq;
1888 * We drop cfq io contexts lazily, so we may find a dead one.
1890 static void
1891 cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
1892 struct cfq_io_context *cic)
1894 unsigned long flags;
1896 WARN_ON(!list_empty(&cic->queue_list));
1898 spin_lock_irqsave(&ioc->lock, flags);
1900 BUG_ON(ioc->ioc_data == cic);
1902 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
1903 hlist_del_rcu(&cic->cic_list);
1904 spin_unlock_irqrestore(&ioc->lock, flags);
1906 cfq_cic_free(cic);
1909 static struct cfq_io_context *
1910 cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1912 struct cfq_io_context *cic;
1913 unsigned long flags;
1914 void *k;
1916 if (unlikely(!ioc))
1917 return NULL;
1919 rcu_read_lock();
1922 * we maintain a last-hit cache, to avoid browsing over the tree
1924 cic = rcu_dereference(ioc->ioc_data);
1925 if (cic && cic->key == cfqd) {
1926 rcu_read_unlock();
1927 return cic;
1930 do {
1931 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
1932 rcu_read_unlock();
1933 if (!cic)
1934 break;
1935 /* ->key must be copied to avoid race with cfq_exit_queue() */
1936 k = cic->key;
1937 if (unlikely(!k)) {
1938 cfq_drop_dead_cic(cfqd, ioc, cic);
1939 rcu_read_lock();
1940 continue;
1943 spin_lock_irqsave(&ioc->lock, flags);
1944 rcu_assign_pointer(ioc->ioc_data, cic);
1945 spin_unlock_irqrestore(&ioc->lock, flags);
1946 break;
1947 } while (1);
1949 return cic;
1953 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
1954 * the process specific cfq io context when entered from the block layer.
1955 * Also adds the cic to a per-cfqd list, used when this queue is removed.
1957 static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1958 struct cfq_io_context *cic, gfp_t gfp_mask)
1960 unsigned long flags;
1961 int ret;
1963 ret = radix_tree_preload(gfp_mask);
1964 if (!ret) {
1965 cic->ioc = ioc;
1966 cic->key = cfqd;
1968 spin_lock_irqsave(&ioc->lock, flags);
1969 ret = radix_tree_insert(&ioc->radix_root,
1970 (unsigned long) cfqd, cic);
1971 if (!ret)
1972 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
1973 spin_unlock_irqrestore(&ioc->lock, flags);
1975 radix_tree_preload_end();
1977 if (!ret) {
1978 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1979 list_add(&cic->queue_list, &cfqd->cic_list);
1980 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1984 if (ret)
1985 printk(KERN_ERR "cfq: cic link failed!\n");
1987 return ret;
1991 * Setup general io context and cfq io context. There can be several cfq
1992 * io contexts per general io context, if this process is doing io to more
1993 * than one device managed by cfq.
1995 static struct cfq_io_context *
1996 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1998 struct io_context *ioc = NULL;
1999 struct cfq_io_context *cic;
2001 might_sleep_if(gfp_mask & __GFP_WAIT);
2003 ioc = get_io_context(gfp_mask, cfqd->queue->node);
2004 if (!ioc)
2005 return NULL;
2007 cic = cfq_cic_lookup(cfqd, ioc);
2008 if (cic)
2009 goto out;
2011 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2012 if (cic == NULL)
2013 goto err;
2015 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2016 goto err_free;
2018 out:
2019 smp_read_barrier_depends();
2020 if (unlikely(ioc->ioprio_changed))
2021 cfq_ioc_set_ioprio(ioc);
2023 return cic;
2024 err_free:
2025 cfq_cic_free(cic);
2026 err:
2027 put_io_context(ioc);
2028 return NULL;
2031 static void
2032 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2034 unsigned long elapsed = jiffies - cic->last_end_request;
2035 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
2037 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2038 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2039 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2042 static void
2043 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2044 struct request *rq)
2046 sector_t sdist;
2047 u64 total;
2049 if (!cfqq->last_request_pos)
2050 sdist = 0;
2051 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2052 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2053 else
2054 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2057 * Don't allow the seek distance to get too large from the
2058 * odd fragment, pagein, etc
2060 if (cfqq->seek_samples <= 60) /* second&third seek */
2061 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
2062 else
2063 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
2065 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2066 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2067 total = cfqq->seek_total + (cfqq->seek_samples/2);
2068 do_div(total, cfqq->seek_samples);
2069 cfqq->seek_mean = (sector_t)total;
2072 * If this cfqq is shared between multiple processes, check to
2073 * make sure that those processes are still issuing I/Os within
2074 * the mean seek distance. If not, it may be time to break the
2075 * queues apart again.
2077 if (cfq_cfqq_coop(cfqq)) {
2078 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2079 cfqq->seeky_start = jiffies;
2080 else if (!CFQQ_SEEKY(cfqq))
2081 cfqq->seeky_start = 0;
2086 * Disable idle window if the process thinks too long or seeks so much that
2087 * it doesn't matter
2089 static void
2090 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2091 struct cfq_io_context *cic)
2093 int old_idle, enable_idle;
2096 * Don't idle for async or idle io prio class
2098 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
2099 return;
2101 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
2103 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
2104 (!cfqd->cfq_latency && cfqd->hw_tag && CFQQ_SEEKY(cfqq)))
2105 enable_idle = 0;
2106 else if (sample_valid(cic->ttime_samples)) {
2107 unsigned int slice_idle = cfqd->cfq_slice_idle;
2108 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
2109 slice_idle = msecs_to_jiffies(CFQ_MIN_TT);
2110 if (cic->ttime_mean > slice_idle)
2111 enable_idle = 0;
2112 else
2113 enable_idle = 1;
2116 if (old_idle != enable_idle) {
2117 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2118 if (enable_idle)
2119 cfq_mark_cfqq_idle_window(cfqq);
2120 else
2121 cfq_clear_cfqq_idle_window(cfqq);
2126 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2127 * no or if we aren't sure, a 1 will cause a preempt.
2129 static bool
2130 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2131 struct request *rq)
2133 struct cfq_queue *cfqq;
2135 cfqq = cfqd->active_queue;
2136 if (!cfqq)
2137 return false;
2139 if (cfq_slice_used(cfqq))
2140 return true;
2142 if (cfq_class_idle(new_cfqq))
2143 return false;
2145 if (cfq_class_idle(cfqq))
2146 return true;
2149 * if the new request is sync, but the currently running queue is
2150 * not, let the sync request have priority.
2152 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
2153 return true;
2156 * So both queues are sync. Let the new request get disk time if
2157 * it's a metadata request and the current queue is doing regular IO.
2159 if (rq_is_meta(rq) && !cfqq->meta_pending)
2160 return true;
2163 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2165 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
2166 return true;
2168 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
2169 return false;
2172 * if this request is as-good as one we would expect from the
2173 * current cfqq, let it preempt
2175 if (cfq_rq_close(cfqd, cfqq, rq))
2176 return true;
2178 return false;
2182 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2183 * let it have half of its nominal slice.
2185 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2187 cfq_log_cfqq(cfqd, cfqq, "preempt");
2188 cfq_slice_expired(cfqd, 1);
2191 * Put the new queue at the front of the of the current list,
2192 * so we know that it will be selected next.
2194 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2196 cfq_service_tree_add(cfqd, cfqq, 1);
2198 cfqq->slice_end = 0;
2199 cfq_mark_cfqq_slice_new(cfqq);
2203 * Called when a new fs request (rq) is added (to cfqq). Check if there's
2204 * something we should do about it
2206 static void
2207 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2208 struct request *rq)
2210 struct cfq_io_context *cic = RQ_CIC(rq);
2212 cfqd->rq_queued++;
2213 if (rq_is_meta(rq))
2214 cfqq->meta_pending++;
2216 cfq_update_io_thinktime(cfqd, cic);
2217 cfq_update_io_seektime(cfqd, cfqq, rq);
2218 cfq_update_idle_window(cfqd, cfqq, cic);
2220 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
2222 if (cfqq == cfqd->active_queue) {
2224 * Remember that we saw a request from this process, but
2225 * don't start queuing just yet. Otherwise we risk seeing lots
2226 * of tiny requests, because we disrupt the normal plugging
2227 * and merging. If the request is already larger than a single
2228 * page, let it rip immediately. For that case we assume that
2229 * merging is already done. Ditto for a busy system that
2230 * has other work pending, don't risk delaying until the
2231 * idle timer unplug to continue working.
2233 if (cfq_cfqq_wait_request(cfqq)) {
2234 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2235 cfqd->busy_queues > 1) {
2236 del_timer(&cfqd->idle_slice_timer);
2237 __blk_run_queue(cfqd->queue);
2239 cfq_mark_cfqq_must_dispatch(cfqq);
2241 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
2243 * not the active queue - expire current slice if it is
2244 * idle and has expired it's mean thinktime or this new queue
2245 * has some old slice time left and is of higher priority or
2246 * this new queue is RT and the current one is BE
2248 cfq_preempt_queue(cfqd, cfqq);
2249 __blk_run_queue(cfqd->queue);
2253 static void cfq_insert_request(struct request_queue *q, struct request *rq)
2255 struct cfq_data *cfqd = q->elevator->elevator_data;
2256 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2258 cfq_log_cfqq(cfqd, cfqq, "insert_request");
2259 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
2261 cfq_add_rq_rb(rq);
2263 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
2264 list_add_tail(&rq->queuelist, &cfqq->fifo);
2266 cfq_rq_enqueued(cfqd, cfqq, rq);
2270 * Update hw_tag based on peak queue depth over 50 samples under
2271 * sufficient load.
2273 static void cfq_update_hw_tag(struct cfq_data *cfqd)
2275 if (rq_in_driver(cfqd) > cfqd->rq_in_driver_peak)
2276 cfqd->rq_in_driver_peak = rq_in_driver(cfqd);
2278 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
2279 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
2280 return;
2282 if (cfqd->hw_tag_samples++ < 50)
2283 return;
2285 if (cfqd->rq_in_driver_peak >= CFQ_HW_QUEUE_MIN)
2286 cfqd->hw_tag = 1;
2287 else
2288 cfqd->hw_tag = 0;
2290 cfqd->hw_tag_samples = 0;
2291 cfqd->rq_in_driver_peak = 0;
2294 static void cfq_completed_request(struct request_queue *q, struct request *rq)
2296 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2297 struct cfq_data *cfqd = cfqq->cfqd;
2298 const int sync = rq_is_sync(rq);
2299 unsigned long now;
2301 now = jiffies;
2302 cfq_log_cfqq(cfqd, cfqq, "complete");
2304 cfq_update_hw_tag(cfqd);
2306 WARN_ON(!cfqd->rq_in_driver[sync]);
2307 WARN_ON(!cfqq->dispatched);
2308 cfqd->rq_in_driver[sync]--;
2309 cfqq->dispatched--;
2311 if (cfq_cfqq_sync(cfqq))
2312 cfqd->sync_flight--;
2314 if (sync) {
2315 RQ_CIC(rq)->last_end_request = now;
2316 cfqd->last_end_sync_rq = now;
2320 * If this is the active queue, check if it needs to be expired,
2321 * or if we want to idle in case it has no pending requests.
2323 if (cfqd->active_queue == cfqq) {
2324 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
2326 if (cfq_cfqq_slice_new(cfqq)) {
2327 cfq_set_prio_slice(cfqd, cfqq);
2328 cfq_clear_cfqq_slice_new(cfqq);
2331 * If there are no requests waiting in this queue, and
2332 * there are other queues ready to issue requests, AND
2333 * those other queues are issuing requests within our
2334 * mean seek distance, give them a chance to run instead
2335 * of idling.
2337 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
2338 cfq_slice_expired(cfqd, 1);
2339 else if (cfqq_empty && !cfq_close_cooperator(cfqd, cfqq) &&
2340 sync && !rq_noidle(rq))
2341 cfq_arm_slice_timer(cfqd);
2344 if (!rq_in_driver(cfqd))
2345 cfq_schedule_dispatch(cfqd);
2349 * we temporarily boost lower priority queues if they are holding fs exclusive
2350 * resources. they are boosted to normal prio (CLASS_BE/4)
2352 static void cfq_prio_boost(struct cfq_queue *cfqq)
2354 if (has_fs_excl()) {
2356 * boost idle prio on transactions that would lock out other
2357 * users of the filesystem
2359 if (cfq_class_idle(cfqq))
2360 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2361 if (cfqq->ioprio > IOPRIO_NORM)
2362 cfqq->ioprio = IOPRIO_NORM;
2363 } else {
2365 * check if we need to unboost the queue
2367 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
2368 cfqq->ioprio_class = cfqq->org_ioprio_class;
2369 if (cfqq->ioprio != cfqq->org_ioprio)
2370 cfqq->ioprio = cfqq->org_ioprio;
2374 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
2376 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
2377 cfq_mark_cfqq_must_alloc_slice(cfqq);
2378 return ELV_MQUEUE_MUST;
2381 return ELV_MQUEUE_MAY;
2384 static int cfq_may_queue(struct request_queue *q, int rw)
2386 struct cfq_data *cfqd = q->elevator->elevator_data;
2387 struct task_struct *tsk = current;
2388 struct cfq_io_context *cic;
2389 struct cfq_queue *cfqq;
2392 * don't force setup of a queue from here, as a call to may_queue
2393 * does not necessarily imply that a request actually will be queued.
2394 * so just lookup a possibly existing queue, or return 'may queue'
2395 * if that fails
2397 cic = cfq_cic_lookup(cfqd, tsk->io_context);
2398 if (!cic)
2399 return ELV_MQUEUE_MAY;
2401 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
2402 if (cfqq) {
2403 cfq_init_prio_data(cfqq, cic->ioc);
2404 cfq_prio_boost(cfqq);
2406 return __cfq_may_queue(cfqq);
2409 return ELV_MQUEUE_MAY;
2413 * queue lock held here
2415 static void cfq_put_request(struct request *rq)
2417 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2419 if (cfqq) {
2420 const int rw = rq_data_dir(rq);
2422 BUG_ON(!cfqq->allocated[rw]);
2423 cfqq->allocated[rw]--;
2425 put_io_context(RQ_CIC(rq)->ioc);
2427 rq->elevator_private = NULL;
2428 rq->elevator_private2 = NULL;
2430 cfq_put_queue(cfqq);
2434 static struct cfq_queue *
2435 cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
2436 struct cfq_queue *cfqq)
2438 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
2439 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
2440 cfq_mark_cfqq_coop(cfqq->new_cfqq);
2441 cfq_put_queue(cfqq);
2442 return cic_to_cfqq(cic, 1);
2445 static int should_split_cfqq(struct cfq_queue *cfqq)
2447 if (cfqq->seeky_start &&
2448 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
2449 return 1;
2450 return 0;
2454 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
2455 * was the last process referring to said cfqq.
2457 static struct cfq_queue *
2458 split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
2460 if (cfqq_process_refs(cfqq) == 1) {
2461 cfqq->seeky_start = 0;
2462 cfqq->pid = current->pid;
2463 cfq_clear_cfqq_coop(cfqq);
2464 return cfqq;
2467 cic_set_cfqq(cic, NULL, 1);
2468 cfq_put_queue(cfqq);
2469 return NULL;
2472 * Allocate cfq data structures associated with this request.
2474 static int
2475 cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
2477 struct cfq_data *cfqd = q->elevator->elevator_data;
2478 struct cfq_io_context *cic;
2479 const int rw = rq_data_dir(rq);
2480 const bool is_sync = rq_is_sync(rq);
2481 struct cfq_queue *cfqq;
2482 unsigned long flags;
2484 might_sleep_if(gfp_mask & __GFP_WAIT);
2486 cic = cfq_get_io_context(cfqd, gfp_mask);
2488 spin_lock_irqsave(q->queue_lock, flags);
2490 if (!cic)
2491 goto queue_fail;
2493 new_queue:
2494 cfqq = cic_to_cfqq(cic, is_sync);
2495 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2496 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
2497 cic_set_cfqq(cic, cfqq, is_sync);
2498 } else {
2500 * If the queue was seeky for too long, break it apart.
2502 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
2503 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
2504 cfqq = split_cfqq(cic, cfqq);
2505 if (!cfqq)
2506 goto new_queue;
2510 * Check to see if this queue is scheduled to merge with
2511 * another, closely cooperating queue. The merging of
2512 * queues happens here as it must be done in process context.
2513 * The reference on new_cfqq was taken in merge_cfqqs.
2515 if (cfqq->new_cfqq)
2516 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
2519 cfqq->allocated[rw]++;
2520 atomic_inc(&cfqq->ref);
2522 spin_unlock_irqrestore(q->queue_lock, flags);
2524 rq->elevator_private = cic;
2525 rq->elevator_private2 = cfqq;
2526 return 0;
2528 queue_fail:
2529 if (cic)
2530 put_io_context(cic->ioc);
2532 cfq_schedule_dispatch(cfqd);
2533 spin_unlock_irqrestore(q->queue_lock, flags);
2534 cfq_log(cfqd, "set_request fail");
2535 return 1;
2538 static void cfq_kick_queue(struct work_struct *work)
2540 struct cfq_data *cfqd =
2541 container_of(work, struct cfq_data, unplug_work);
2542 struct request_queue *q = cfqd->queue;
2544 spin_lock_irq(q->queue_lock);
2545 __blk_run_queue(cfqd->queue);
2546 spin_unlock_irq(q->queue_lock);
2550 * Timer running if the active_queue is currently idling inside its time slice
2552 static void cfq_idle_slice_timer(unsigned long data)
2554 struct cfq_data *cfqd = (struct cfq_data *) data;
2555 struct cfq_queue *cfqq;
2556 unsigned long flags;
2557 int timed_out = 1;
2559 cfq_log(cfqd, "idle timer fired");
2561 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2563 cfqq = cfqd->active_queue;
2564 if (cfqq) {
2565 timed_out = 0;
2568 * We saw a request before the queue expired, let it through
2570 if (cfq_cfqq_must_dispatch(cfqq))
2571 goto out_kick;
2574 * expired
2576 if (cfq_slice_used(cfqq))
2577 goto expire;
2580 * only expire and reinvoke request handler, if there are
2581 * other queues with pending requests
2583 if (!cfqd->busy_queues)
2584 goto out_cont;
2587 * not expired and it has a request pending, let it dispatch
2589 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
2590 goto out_kick;
2592 expire:
2593 cfq_slice_expired(cfqd, timed_out);
2594 out_kick:
2595 cfq_schedule_dispatch(cfqd);
2596 out_cont:
2597 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2600 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2602 del_timer_sync(&cfqd->idle_slice_timer);
2603 cancel_work_sync(&cfqd->unplug_work);
2606 static void cfq_put_async_queues(struct cfq_data *cfqd)
2608 int i;
2610 for (i = 0; i < IOPRIO_BE_NR; i++) {
2611 if (cfqd->async_cfqq[0][i])
2612 cfq_put_queue(cfqd->async_cfqq[0][i]);
2613 if (cfqd->async_cfqq[1][i])
2614 cfq_put_queue(cfqd->async_cfqq[1][i]);
2617 if (cfqd->async_idle_cfqq)
2618 cfq_put_queue(cfqd->async_idle_cfqq);
2621 static void cfq_exit_queue(struct elevator_queue *e)
2623 struct cfq_data *cfqd = e->elevator_data;
2624 struct request_queue *q = cfqd->queue;
2626 cfq_shutdown_timer_wq(cfqd);
2628 spin_lock_irq(q->queue_lock);
2630 if (cfqd->active_queue)
2631 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2633 while (!list_empty(&cfqd->cic_list)) {
2634 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2635 struct cfq_io_context,
2636 queue_list);
2638 __cfq_exit_single_io_context(cfqd, cic);
2641 cfq_put_async_queues(cfqd);
2643 spin_unlock_irq(q->queue_lock);
2645 cfq_shutdown_timer_wq(cfqd);
2647 kfree(cfqd);
2650 static void *cfq_init_queue(struct request_queue *q)
2652 struct cfq_data *cfqd;
2653 int i;
2655 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
2656 if (!cfqd)
2657 return NULL;
2659 cfqd->service_tree = CFQ_RB_ROOT;
2662 * Not strictly needed (since RB_ROOT just clears the node and we
2663 * zeroed cfqd on alloc), but better be safe in case someone decides
2664 * to add magic to the rb code
2666 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2667 cfqd->prio_trees[i] = RB_ROOT;
2670 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
2671 * Grab a permanent reference to it, so that the normal code flow
2672 * will not attempt to free it.
2674 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
2675 atomic_inc(&cfqd->oom_cfqq.ref);
2677 INIT_LIST_HEAD(&cfqd->cic_list);
2679 cfqd->queue = q;
2681 init_timer(&cfqd->idle_slice_timer);
2682 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2683 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2685 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
2687 cfqd->cfq_quantum = cfq_quantum;
2688 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2689 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2690 cfqd->cfq_back_max = cfq_back_max;
2691 cfqd->cfq_back_penalty = cfq_back_penalty;
2692 cfqd->cfq_slice[0] = cfq_slice_async;
2693 cfqd->cfq_slice[1] = cfq_slice_sync;
2694 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2695 cfqd->cfq_slice_idle = cfq_slice_idle;
2696 cfqd->cfq_latency = 1;
2697 cfqd->hw_tag = 1;
2698 cfqd->last_end_sync_rq = jiffies;
2699 return cfqd;
2702 static void cfq_slab_kill(void)
2705 * Caller already ensured that pending RCU callbacks are completed,
2706 * so we should have no busy allocations at this point.
2708 if (cfq_pool)
2709 kmem_cache_destroy(cfq_pool);
2710 if (cfq_ioc_pool)
2711 kmem_cache_destroy(cfq_ioc_pool);
2714 static int __init cfq_slab_setup(void)
2716 cfq_pool = KMEM_CACHE(cfq_queue, 0);
2717 if (!cfq_pool)
2718 goto fail;
2720 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
2721 if (!cfq_ioc_pool)
2722 goto fail;
2724 return 0;
2725 fail:
2726 cfq_slab_kill();
2727 return -ENOMEM;
2731 * sysfs parts below -->
2733 static ssize_t
2734 cfq_var_show(unsigned int var, char *page)
2736 return sprintf(page, "%d\n", var);
2739 static ssize_t
2740 cfq_var_store(unsigned int *var, const char *page, size_t count)
2742 char *p = (char *) page;
2744 *var = simple_strtoul(p, &p, 10);
2745 return count;
2748 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2749 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
2751 struct cfq_data *cfqd = e->elevator_data; \
2752 unsigned int __data = __VAR; \
2753 if (__CONV) \
2754 __data = jiffies_to_msecs(__data); \
2755 return cfq_var_show(__data, (page)); \
2757 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2758 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2759 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2760 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2761 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2762 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2763 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2764 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2765 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2766 SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
2767 #undef SHOW_FUNCTION
2769 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2770 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
2772 struct cfq_data *cfqd = e->elevator_data; \
2773 unsigned int __data; \
2774 int ret = cfq_var_store(&__data, (page), count); \
2775 if (__data < (MIN)) \
2776 __data = (MIN); \
2777 else if (__data > (MAX)) \
2778 __data = (MAX); \
2779 if (__CONV) \
2780 *(__PTR) = msecs_to_jiffies(__data); \
2781 else \
2782 *(__PTR) = __data; \
2783 return ret; \
2785 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2786 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
2787 UINT_MAX, 1);
2788 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
2789 UINT_MAX, 1);
2790 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2791 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
2792 UINT_MAX, 0);
2793 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2794 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2795 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2796 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
2797 UINT_MAX, 0);
2798 STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
2799 #undef STORE_FUNCTION
2801 #define CFQ_ATTR(name) \
2802 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2804 static struct elv_fs_entry cfq_attrs[] = {
2805 CFQ_ATTR(quantum),
2806 CFQ_ATTR(fifo_expire_sync),
2807 CFQ_ATTR(fifo_expire_async),
2808 CFQ_ATTR(back_seek_max),
2809 CFQ_ATTR(back_seek_penalty),
2810 CFQ_ATTR(slice_sync),
2811 CFQ_ATTR(slice_async),
2812 CFQ_ATTR(slice_async_rq),
2813 CFQ_ATTR(slice_idle),
2814 CFQ_ATTR(low_latency),
2815 __ATTR_NULL
2818 static struct elevator_type iosched_cfq = {
2819 .ops = {
2820 .elevator_merge_fn = cfq_merge,
2821 .elevator_merged_fn = cfq_merged_request,
2822 .elevator_merge_req_fn = cfq_merged_requests,
2823 .elevator_allow_merge_fn = cfq_allow_merge,
2824 .elevator_dispatch_fn = cfq_dispatch_requests,
2825 .elevator_add_req_fn = cfq_insert_request,
2826 .elevator_activate_req_fn = cfq_activate_request,
2827 .elevator_deactivate_req_fn = cfq_deactivate_request,
2828 .elevator_queue_empty_fn = cfq_queue_empty,
2829 .elevator_completed_req_fn = cfq_completed_request,
2830 .elevator_former_req_fn = elv_rb_former_request,
2831 .elevator_latter_req_fn = elv_rb_latter_request,
2832 .elevator_set_req_fn = cfq_set_request,
2833 .elevator_put_req_fn = cfq_put_request,
2834 .elevator_may_queue_fn = cfq_may_queue,
2835 .elevator_init_fn = cfq_init_queue,
2836 .elevator_exit_fn = cfq_exit_queue,
2837 .trim = cfq_free_io_context,
2839 .elevator_attrs = cfq_attrs,
2840 .elevator_name = "cfq",
2841 .elevator_owner = THIS_MODULE,
2844 static int __init cfq_init(void)
2847 * could be 0 on HZ < 1000 setups
2849 if (!cfq_slice_async)
2850 cfq_slice_async = 1;
2851 if (!cfq_slice_idle)
2852 cfq_slice_idle = 1;
2854 if (cfq_slab_setup())
2855 return -ENOMEM;
2857 elv_register(&iosched_cfq);
2859 return 0;
2862 static void __exit cfq_exit(void)
2864 DECLARE_COMPLETION_ONSTACK(all_gone);
2865 elv_unregister(&iosched_cfq);
2866 ioc_gone = &all_gone;
2867 /* ioc_gone's update must be visible before reading ioc_count */
2868 smp_wmb();
2871 * this also protects us from entering cfq_slab_kill() with
2872 * pending RCU callbacks
2874 if (elv_ioc_count_read(cfq_ioc_count))
2875 wait_for_completion(&all_gone);
2876 cfq_slab_kill();
2879 module_init(cfq_init);
2880 module_exit(cfq_exit);
2882 MODULE_AUTHOR("Jens Axboe");
2883 MODULE_LICENSE("GPL");
2884 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");