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/rbtree.h>
13 #include <linux/ioprio.h>
14 #include <linux/blktrace_api.h>
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;
30 static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
31 static const int cfq_hist_divisor = 4;
34 * offset from end of service tree
36 #define CFQ_IDLE_DELAY (HZ / 5)
39 * below this threshold, we consider thinktime immediate
41 #define CFQ_MIN_TT (2)
44 * Allow merged cfqqs to perform this amount of seeky I/O before
45 * deciding to break the queues up again.
47 #define CFQQ_COOP_TOUT (HZ)
49 #define CFQ_SLICE_SCALE (5)
50 #define CFQ_HW_QUEUE_MIN (5)
53 ((struct cfq_io_context *) (rq)->elevator_private)
54 #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
56 static struct kmem_cache *cfq_pool;
57 static struct kmem_cache *cfq_ioc_pool;
59 static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
60 static struct completion *ioc_gone;
61 static DEFINE_SPINLOCK(ioc_gone_lock);
63 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
64 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
65 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
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 process-grouping structure
87 /* various state flags, see below */
90 struct cfq_data *cfqd;
91 /* service_tree member */
92 struct rb_node rb_node;
93 /* service_tree key */
95 /* prio tree member */
96 struct rb_node p_node;
97 /* prio tree root we belong to, if any */
98 struct rb_root *p_root;
99 /* sorted list of pending requests */
100 struct rb_root sort_list;
101 /* if fifo isn't expired, next request to serve */
102 struct request *next_rq;
103 /* requests queued in sort_list */
105 /* currently allocated requests */
107 /* fifo list of requests in sort_list */
108 struct list_head fifo;
110 unsigned long slice_end;
112 unsigned int slice_dispatch;
114 /* pending metadata requests */
116 /* number of requests that are on the dispatch list or inside driver */
119 /* io prio of this group */
120 unsigned short ioprio, org_ioprio;
121 unsigned short ioprio_class, org_ioprio_class;
123 unsigned int seek_samples;
126 sector_t last_request_pos;
127 unsigned long seeky_start;
131 struct cfq_queue *new_cfqq;
135 * Per block device queue structure
138 struct request_queue *queue;
141 * rr list of queues with requests and the count of them
143 struct cfq_rb_root service_tree;
146 * Each priority tree is sorted by next_request position. These
147 * trees are used when determining if two or more queues are
148 * interleaving requests (see cfq_close_cooperator).
150 struct rb_root prio_trees[CFQ_PRIO_LISTS];
152 unsigned int busy_queues;
153 unsigned int busy_rt_queues;
154 unsigned int busy_queues_avg[2];
160 * queue-depth detection
165 int rq_in_driver_peak;
168 * idle window management
170 struct timer_list idle_slice_timer;
171 struct work_struct unplug_work;
173 struct cfq_queue *active_queue;
174 struct cfq_io_context *active_cic;
177 * async queue for each priority case
179 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
180 struct cfq_queue *async_idle_cfqq;
182 sector_t last_position;
185 * tunables, see top of file
187 unsigned int cfq_quantum;
188 unsigned int cfq_fifo_expire[2];
189 unsigned int cfq_back_penalty;
190 unsigned int cfq_back_max;
191 unsigned int cfq_slice[2];
192 unsigned int cfq_slice_async_rq;
193 unsigned int cfq_slice_idle;
194 unsigned int cfq_latency;
196 struct list_head cic_list;
199 * Fallback dummy cfqq for extreme OOM conditions
201 struct cfq_queue oom_cfqq;
203 unsigned long last_end_sync_rq;
206 enum cfqq_state_flags {
207 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
208 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
209 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
210 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
211 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
212 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
213 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
214 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
215 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
216 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
219 #define CFQ_CFQQ_FNS(name) \
220 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
222 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
224 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
226 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
228 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
230 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
234 CFQ_CFQQ_FNS(wait_request);
235 CFQ_CFQQ_FNS(must_dispatch);
236 CFQ_CFQQ_FNS(must_alloc_slice);
237 CFQ_CFQQ_FNS(fifo_expire);
238 CFQ_CFQQ_FNS(idle_window);
239 CFQ_CFQQ_FNS(prio_changed);
240 CFQ_CFQQ_FNS(slice_new);
245 #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
246 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
247 #define cfq_log(cfqd, fmt, args...) \
248 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
250 static void cfq_dispatch_insert(struct request_queue *, struct request *);
251 static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
252 struct io_context *, gfp_t);
253 static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
254 struct io_context *);
256 static inline int rq_in_driver(struct cfq_data *cfqd)
258 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
261 static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
264 return cic->cfqq[is_sync];
267 static inline void cic_set_cfqq(struct cfq_io_context *cic,
268 struct cfq_queue *cfqq, bool is_sync)
270 cic->cfqq[is_sync] = cfqq;
274 * We regard a request as SYNC, if it's either a read or has the SYNC bit
275 * set (in which case it could also be direct WRITE).
277 static inline bool cfq_bio_sync(struct bio *bio)
279 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
283 * scheduler run of queue, if there are requests pending and no one in the
284 * driver that will restart queueing
286 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
288 if (cfqd->busy_queues) {
289 cfq_log(cfqd, "schedule dispatch");
290 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
294 static int cfq_queue_empty(struct request_queue *q)
296 struct cfq_data *cfqd = q->elevator->elevator_data;
298 return !cfqd->busy_queues;
302 * Scale schedule slice based on io priority. Use the sync time slice only
303 * if a queue is marked sync and has sync io queued. A sync queue with async
304 * io only, should not get full sync slice length.
306 static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
309 const int base_slice = cfqd->cfq_slice[sync];
311 WARN_ON(prio >= IOPRIO_BE_NR);
313 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
317 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
319 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
323 * get averaged number of queues of RT/BE priority.
324 * average is updated, with a formula that gives more weight to higher numbers,
325 * to quickly follows sudden increases and decrease slowly
328 static inline unsigned
329 cfq_get_avg_queues(struct cfq_data *cfqd, bool rt) {
330 unsigned min_q, max_q;
331 unsigned mult = cfq_hist_divisor - 1;
332 unsigned round = cfq_hist_divisor / 2;
333 unsigned busy = cfqd->busy_rt_queues;
336 busy = cfqd->busy_queues - cfqd->busy_rt_queues;
338 min_q = min(cfqd->busy_queues_avg[rt], busy);
339 max_q = max(cfqd->busy_queues_avg[rt], busy);
340 cfqd->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
342 return cfqd->busy_queues_avg[rt];
346 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
348 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
349 if (cfqd->cfq_latency) {
350 /* interested queues (we consider only the ones with the same
352 unsigned iq = cfq_get_avg_queues(cfqd, cfq_class_rt(cfqq));
353 unsigned sync_slice = cfqd->cfq_slice[1];
354 unsigned expect_latency = sync_slice * iq;
355 if (expect_latency > cfq_target_latency) {
356 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
357 /* scale low_slice according to IO priority
358 * and sync vs async */
360 min(slice, base_low_slice * slice / sync_slice);
361 /* the adapted slice value is scaled to fit all iqs
362 * into the target latency */
363 slice = max(slice * cfq_target_latency / expect_latency,
367 cfqq->slice_end = jiffies + slice;
368 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
372 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
373 * isn't valid until the first request from the dispatch is activated
374 * and the slice time set.
376 static inline bool cfq_slice_used(struct cfq_queue *cfqq)
378 if (cfq_cfqq_slice_new(cfqq))
380 if (time_before(jiffies, cfqq->slice_end))
387 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
388 * We choose the request that is closest to the head right now. Distance
389 * behind the head is penalized and only allowed to a certain extent.
391 static struct request *
392 cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
394 sector_t last, s1, s2, d1 = 0, d2 = 0;
395 unsigned long back_max;
396 #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
397 #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
398 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
400 if (rq1 == NULL || rq1 == rq2)
405 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
407 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
409 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
411 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
414 s1 = blk_rq_pos(rq1);
415 s2 = blk_rq_pos(rq2);
417 last = cfqd->last_position;
420 * by definition, 1KiB is 2 sectors
422 back_max = cfqd->cfq_back_max * 2;
425 * Strict one way elevator _except_ in the case where we allow
426 * short backward seeks which are biased as twice the cost of a
427 * similar forward seek.
431 else if (s1 + back_max >= last)
432 d1 = (last - s1) * cfqd->cfq_back_penalty;
434 wrap |= CFQ_RQ1_WRAP;
438 else if (s2 + back_max >= last)
439 d2 = (last - s2) * cfqd->cfq_back_penalty;
441 wrap |= CFQ_RQ2_WRAP;
443 /* Found required data */
446 * By doing switch() on the bit mask "wrap" we avoid having to
447 * check two variables for all permutations: --> faster!
450 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
466 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
469 * Since both rqs are wrapped,
470 * start with the one that's further behind head
471 * (--> only *one* back seek required),
472 * since back seek takes more time than forward.
482 * The below is leftmost cache rbtree addon
484 static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
487 root->left = rb_first(&root->rb);
490 return rb_entry(root->left, struct cfq_queue, rb_node);
495 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
501 static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
505 rb_erase_init(n, &root->rb);
509 * would be nice to take fifo expire time into account as well
511 static struct request *
512 cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
513 struct request *last)
515 struct rb_node *rbnext = rb_next(&last->rb_node);
516 struct rb_node *rbprev = rb_prev(&last->rb_node);
517 struct request *next = NULL, *prev = NULL;
519 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
522 prev = rb_entry_rq(rbprev);
525 next = rb_entry_rq(rbnext);
527 rbnext = rb_first(&cfqq->sort_list);
528 if (rbnext && rbnext != &last->rb_node)
529 next = rb_entry_rq(rbnext);
532 return cfq_choose_req(cfqd, next, prev);
535 static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
536 struct cfq_queue *cfqq)
539 * just an approximation, should be ok.
541 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
542 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
546 * The cfqd->service_tree holds all pending cfq_queue's that have
547 * requests waiting to be processed. It is sorted in the order that
548 * we will service the queues.
550 static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
553 struct rb_node **p, *parent;
554 struct cfq_queue *__cfqq;
555 unsigned long rb_key;
558 if (cfq_class_idle(cfqq)) {
559 rb_key = CFQ_IDLE_DELAY;
560 parent = rb_last(&cfqd->service_tree.rb);
561 if (parent && parent != &cfqq->rb_node) {
562 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
563 rb_key += __cfqq->rb_key;
566 } else if (!add_front) {
568 * Get our rb key offset. Subtract any residual slice
569 * value carried from last service. A negative resid
570 * count indicates slice overrun, and this should position
571 * the next service time further away in the tree.
573 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
574 rb_key -= cfqq->slice_resid;
575 cfqq->slice_resid = 0;
578 __cfqq = cfq_rb_first(&cfqd->service_tree);
579 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
582 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
584 * same position, nothing more to do
586 if (rb_key == cfqq->rb_key)
589 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
594 p = &cfqd->service_tree.rb.rb_node;
599 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
602 * sort RT queues first, we always want to give
603 * preference to them. IDLE queues goes to the back.
604 * after that, sort on the next service time.
606 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
608 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
610 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
612 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
614 else if (time_before(rb_key, __cfqq->rb_key))
619 if (n == &(*p)->rb_right)
626 cfqd->service_tree.left = &cfqq->rb_node;
628 cfqq->rb_key = rb_key;
629 rb_link_node(&cfqq->rb_node, parent, p);
630 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
633 static struct cfq_queue *
634 cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
635 sector_t sector, struct rb_node **ret_parent,
636 struct rb_node ***rb_link)
638 struct rb_node **p, *parent;
639 struct cfq_queue *cfqq = NULL;
647 cfqq = rb_entry(parent, struct cfq_queue, p_node);
650 * Sort strictly based on sector. Smallest to the left,
651 * largest to the right.
653 if (sector > blk_rq_pos(cfqq->next_rq))
655 else if (sector < blk_rq_pos(cfqq->next_rq))
663 *ret_parent = parent;
669 static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
671 struct rb_node **p, *parent;
672 struct cfq_queue *__cfqq;
675 rb_erase(&cfqq->p_node, cfqq->p_root);
679 if (cfq_class_idle(cfqq))
684 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
685 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
686 blk_rq_pos(cfqq->next_rq), &parent, &p);
688 rb_link_node(&cfqq->p_node, parent, p);
689 rb_insert_color(&cfqq->p_node, cfqq->p_root);
695 * Update cfqq's position in the service tree.
697 static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
700 * Resorting requires the cfqq to be on the RR list already.
702 if (cfq_cfqq_on_rr(cfqq)) {
703 cfq_service_tree_add(cfqd, cfqq, 0);
704 cfq_prio_tree_add(cfqd, cfqq);
709 * add to busy list of queues for service, trying to be fair in ordering
710 * the pending list according to last request service
712 static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
714 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
715 BUG_ON(cfq_cfqq_on_rr(cfqq));
716 cfq_mark_cfqq_on_rr(cfqq);
718 if (cfq_class_rt(cfqq))
719 cfqd->busy_rt_queues++;
720 cfq_resort_rr_list(cfqd, cfqq);
724 * Called when the cfqq no longer has requests pending, remove it from
727 static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
729 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
730 BUG_ON(!cfq_cfqq_on_rr(cfqq));
731 cfq_clear_cfqq_on_rr(cfqq);
733 if (!RB_EMPTY_NODE(&cfqq->rb_node))
734 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
736 rb_erase(&cfqq->p_node, cfqq->p_root);
740 BUG_ON(!cfqd->busy_queues);
742 if (cfq_class_rt(cfqq))
743 cfqd->busy_rt_queues--;
747 * rb tree support functions
749 static void cfq_del_rq_rb(struct request *rq)
751 struct cfq_queue *cfqq = RQ_CFQQ(rq);
752 struct cfq_data *cfqd = cfqq->cfqd;
753 const int sync = rq_is_sync(rq);
755 BUG_ON(!cfqq->queued[sync]);
756 cfqq->queued[sync]--;
758 elv_rb_del(&cfqq->sort_list, rq);
760 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
761 cfq_del_cfqq_rr(cfqd, cfqq);
764 static void cfq_add_rq_rb(struct request *rq)
766 struct cfq_queue *cfqq = RQ_CFQQ(rq);
767 struct cfq_data *cfqd = cfqq->cfqd;
768 struct request *__alias, *prev;
770 cfqq->queued[rq_is_sync(rq)]++;
773 * looks a little odd, but the first insert might return an alias.
774 * if that happens, put the alias on the dispatch list
776 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
777 cfq_dispatch_insert(cfqd->queue, __alias);
779 if (!cfq_cfqq_on_rr(cfqq))
780 cfq_add_cfqq_rr(cfqd, cfqq);
783 * check if this request is a better next-serve candidate
785 prev = cfqq->next_rq;
786 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
789 * adjust priority tree position, if ->next_rq changes
791 if (prev != cfqq->next_rq)
792 cfq_prio_tree_add(cfqd, cfqq);
794 BUG_ON(!cfqq->next_rq);
797 static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
799 elv_rb_del(&cfqq->sort_list, rq);
800 cfqq->queued[rq_is_sync(rq)]--;
804 static struct request *
805 cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
807 struct task_struct *tsk = current;
808 struct cfq_io_context *cic;
809 struct cfq_queue *cfqq;
811 cic = cfq_cic_lookup(cfqd, tsk->io_context);
815 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
817 sector_t sector = bio->bi_sector + bio_sectors(bio);
819 return elv_rb_find(&cfqq->sort_list, sector);
825 static void cfq_activate_request(struct request_queue *q, struct request *rq)
827 struct cfq_data *cfqd = q->elevator->elevator_data;
829 cfqd->rq_in_driver[rq_is_sync(rq)]++;
830 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
833 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
836 static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
838 struct cfq_data *cfqd = q->elevator->elevator_data;
839 const int sync = rq_is_sync(rq);
841 WARN_ON(!cfqd->rq_in_driver[sync]);
842 cfqd->rq_in_driver[sync]--;
843 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
847 static void cfq_remove_request(struct request *rq)
849 struct cfq_queue *cfqq = RQ_CFQQ(rq);
851 if (cfqq->next_rq == rq)
852 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
854 list_del_init(&rq->queuelist);
857 cfqq->cfqd->rq_queued--;
858 if (rq_is_meta(rq)) {
859 WARN_ON(!cfqq->meta_pending);
860 cfqq->meta_pending--;
864 static int cfq_merge(struct request_queue *q, struct request **req,
867 struct cfq_data *cfqd = q->elevator->elevator_data;
868 struct request *__rq;
870 __rq = cfq_find_rq_fmerge(cfqd, bio);
871 if (__rq && elv_rq_merge_ok(__rq, bio)) {
873 return ELEVATOR_FRONT_MERGE;
876 return ELEVATOR_NO_MERGE;
879 static void cfq_merged_request(struct request_queue *q, struct request *req,
882 if (type == ELEVATOR_FRONT_MERGE) {
883 struct cfq_queue *cfqq = RQ_CFQQ(req);
885 cfq_reposition_rq_rb(cfqq, req);
890 cfq_merged_requests(struct request_queue *q, struct request *rq,
891 struct request *next)
894 * reposition in fifo if next is older than rq
896 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
897 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
898 list_move(&rq->queuelist, &next->queuelist);
899 rq_set_fifo_time(rq, rq_fifo_time(next));
902 cfq_remove_request(next);
905 static int cfq_allow_merge(struct request_queue *q, struct request *rq,
908 struct cfq_data *cfqd = q->elevator->elevator_data;
909 struct cfq_io_context *cic;
910 struct cfq_queue *cfqq;
913 * Disallow merge of a sync bio into an async request.
915 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
919 * Lookup the cfqq that this bio will be queued with. Allow
920 * merge only if rq is queued there.
922 cic = cfq_cic_lookup(cfqd, current->io_context);
926 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
927 return cfqq == RQ_CFQQ(rq);
930 static void __cfq_set_active_queue(struct cfq_data *cfqd,
931 struct cfq_queue *cfqq)
934 cfq_log_cfqq(cfqd, cfqq, "set_active");
936 cfqq->slice_dispatch = 0;
938 cfq_clear_cfqq_wait_request(cfqq);
939 cfq_clear_cfqq_must_dispatch(cfqq);
940 cfq_clear_cfqq_must_alloc_slice(cfqq);
941 cfq_clear_cfqq_fifo_expire(cfqq);
942 cfq_mark_cfqq_slice_new(cfqq);
944 del_timer(&cfqd->idle_slice_timer);
947 cfqd->active_queue = cfqq;
951 * current cfqq expired its slice (or was too idle), select new one
954 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
957 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
959 if (cfq_cfqq_wait_request(cfqq))
960 del_timer(&cfqd->idle_slice_timer);
962 cfq_clear_cfqq_wait_request(cfqq);
965 * store what was left of this slice, if the queue idled/timed out
967 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
968 cfqq->slice_resid = cfqq->slice_end - jiffies;
969 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
972 cfq_resort_rr_list(cfqd, cfqq);
974 if (cfqq == cfqd->active_queue)
975 cfqd->active_queue = NULL;
977 if (cfqd->active_cic) {
978 put_io_context(cfqd->active_cic->ioc);
979 cfqd->active_cic = NULL;
983 static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
985 struct cfq_queue *cfqq = cfqd->active_queue;
988 __cfq_slice_expired(cfqd, cfqq, timed_out);
992 * Get next queue for service. Unless we have a queue preemption,
993 * we'll simply select the first cfqq in the service tree.
995 static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
997 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
1000 return cfq_rb_first(&cfqd->service_tree);
1004 * Get and set a new active queue for service.
1006 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1007 struct cfq_queue *cfqq)
1010 cfqq = cfq_get_next_queue(cfqd);
1012 __cfq_set_active_queue(cfqd, cfqq);
1016 static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1019 if (blk_rq_pos(rq) >= cfqd->last_position)
1020 return blk_rq_pos(rq) - cfqd->last_position;
1022 return cfqd->last_position - blk_rq_pos(rq);
1025 #define CFQQ_SEEK_THR 8 * 1024
1026 #define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
1028 static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1031 sector_t sdist = cfqq->seek_mean;
1033 if (!sample_valid(cfqq->seek_samples))
1034 sdist = CFQQ_SEEK_THR;
1036 return cfq_dist_from_last(cfqd, rq) <= sdist;
1039 static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1040 struct cfq_queue *cur_cfqq)
1042 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
1043 struct rb_node *parent, *node;
1044 struct cfq_queue *__cfqq;
1045 sector_t sector = cfqd->last_position;
1047 if (RB_EMPTY_ROOT(root))
1051 * First, if we find a request starting at the end of the last
1052 * request, choose it.
1054 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
1059 * If the exact sector wasn't found, the parent of the NULL leaf
1060 * will contain the closest sector.
1062 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
1063 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1066 if (blk_rq_pos(__cfqq->next_rq) < sector)
1067 node = rb_next(&__cfqq->p_node);
1069 node = rb_prev(&__cfqq->p_node);
1073 __cfqq = rb_entry(node, struct cfq_queue, p_node);
1074 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
1082 * cur_cfqq - passed in so that we don't decide that the current queue is
1083 * closely cooperating with itself.
1085 * So, basically we're assuming that that cur_cfqq has dispatched at least
1086 * one request, and that cfqd->last_position reflects a position on the disk
1087 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1090 static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
1091 struct cfq_queue *cur_cfqq)
1093 struct cfq_queue *cfqq;
1095 if (!cfq_cfqq_sync(cur_cfqq))
1097 if (CFQQ_SEEKY(cur_cfqq))
1101 * We should notice if some of the queues are cooperating, eg
1102 * working closely on the same area of the disk. In that case,
1103 * we can group them together and don't waste time idling.
1105 cfqq = cfqq_close(cfqd, cur_cfqq);
1110 * It only makes sense to merge sync queues.
1112 if (!cfq_cfqq_sync(cfqq))
1114 if (CFQQ_SEEKY(cfqq))
1120 static void cfq_arm_slice_timer(struct cfq_data *cfqd)
1122 struct cfq_queue *cfqq = cfqd->active_queue;
1123 struct cfq_io_context *cic;
1127 * SSD device without seek penalty, disable idling. But only do so
1128 * for devices that support queuing, otherwise we still have a problem
1129 * with sync vs async workloads.
1131 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
1134 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
1135 WARN_ON(cfq_cfqq_slice_new(cfqq));
1138 * idle is disabled, either manually or by past process history
1140 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
1144 * still requests with the driver, don't idle
1146 if (rq_in_driver(cfqd))
1150 * task has exited, don't wait
1152 cic = cfqd->active_cic;
1153 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
1157 * If our average think time is larger than the remaining time
1158 * slice, then don't idle. This avoids overrunning the allotted
1161 if (sample_valid(cic->ttime_samples) &&
1162 (cfqq->slice_end - jiffies < cic->ttime_mean))
1165 cfq_mark_cfqq_wait_request(cfqq);
1168 * we don't want to idle for seeks, but we do want to allow
1169 * fair distribution of slice time for a process doing back-to-back
1170 * seeks. so allow a little bit of time for him to submit a new rq
1172 sl = cfqd->cfq_slice_idle;
1173 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
1174 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
1176 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1177 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
1181 * Move request from internal lists to the request queue dispatch list.
1183 static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1185 struct cfq_data *cfqd = q->elevator->elevator_data;
1186 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1188 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1190 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
1191 cfq_remove_request(rq);
1193 elv_dispatch_sort(q, rq);
1195 if (cfq_cfqq_sync(cfqq))
1196 cfqd->sync_flight++;
1200 * return expired entry, or NULL to just start from scratch in rbtree
1202 static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1204 struct request *rq = NULL;
1206 if (cfq_cfqq_fifo_expire(cfqq))
1209 cfq_mark_cfqq_fifo_expire(cfqq);
1211 if (list_empty(&cfqq->fifo))
1214 rq = rq_entry_fifo(cfqq->fifo.next);
1215 if (time_before(jiffies, rq_fifo_time(rq)))
1218 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
1223 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1225 const int base_rq = cfqd->cfq_slice_async_rq;
1227 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1229 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1233 * Must be called with the queue_lock held.
1235 static int cfqq_process_refs(struct cfq_queue *cfqq)
1237 int process_refs, io_refs;
1239 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1240 process_refs = atomic_read(&cfqq->ref) - io_refs;
1241 BUG_ON(process_refs < 0);
1242 return process_refs;
1245 static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1247 int process_refs, new_process_refs;
1248 struct cfq_queue *__cfqq;
1250 /* Avoid a circular list and skip interim queue merges */
1251 while ((__cfqq = new_cfqq->new_cfqq)) {
1257 process_refs = cfqq_process_refs(cfqq);
1259 * If the process for the cfqq has gone away, there is no
1260 * sense in merging the queues.
1262 if (process_refs == 0)
1266 * Merge in the direction of the lesser amount of work.
1268 new_process_refs = cfqq_process_refs(new_cfqq);
1269 if (new_process_refs >= process_refs) {
1270 cfqq->new_cfqq = new_cfqq;
1271 atomic_add(process_refs, &new_cfqq->ref);
1273 new_cfqq->new_cfqq = cfqq;
1274 atomic_add(new_process_refs, &cfqq->ref);
1279 * Select a queue for service. If we have a current active queue,
1280 * check whether to continue servicing it, or retrieve and set a new one.
1282 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1284 struct cfq_queue *cfqq, *new_cfqq = NULL;
1286 cfqq = cfqd->active_queue;
1291 * The active queue has run out of time, expire it and select new.
1293 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
1297 * The active queue has requests and isn't expired, allow it to
1300 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
1304 * If another queue has a request waiting within our mean seek
1305 * distance, let it run. The expire code will check for close
1306 * cooperators and put the close queue at the front of the service
1307 * tree. If possible, merge the expiring queue with the new cfqq.
1309 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
1311 if (!cfqq->new_cfqq)
1312 cfq_setup_merge(cfqq, new_cfqq);
1317 * No requests pending. If the active queue still has requests in
1318 * flight or is idling for a new request, allow either of these
1319 * conditions to happen (or time out) before selecting a new queue.
1321 if (timer_pending(&cfqd->idle_slice_timer) ||
1322 (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
1328 cfq_slice_expired(cfqd, 0);
1330 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
1335 static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1339 while (cfqq->next_rq) {
1340 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1344 BUG_ON(!list_empty(&cfqq->fifo));
1349 * Drain our current requests. Used for barriers and when switching
1350 * io schedulers on-the-fly.
1352 static int cfq_forced_dispatch(struct cfq_data *cfqd)
1354 struct cfq_queue *cfqq;
1357 while ((cfqq = cfq_rb_first(&cfqd->service_tree)) != NULL)
1358 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1360 cfq_slice_expired(cfqd, 0);
1362 BUG_ON(cfqd->busy_queues);
1364 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1368 static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1370 unsigned int max_dispatch;
1373 * Drain async requests before we start sync IO
1375 if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
1379 * If this is an async queue and we have sync IO in flight, let it wait
1381 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
1384 max_dispatch = cfqd->cfq_quantum;
1385 if (cfq_class_idle(cfqq))
1389 * Does this cfqq already have too much IO in flight?
1391 if (cfqq->dispatched >= max_dispatch) {
1393 * idle queue must always only have a single IO in flight
1395 if (cfq_class_idle(cfqq))
1399 * We have other queues, don't allow more IO from this one
1401 if (cfqd->busy_queues > 1)
1405 * Sole queue user, allow bigger slice
1411 * Async queues must wait a bit before being allowed dispatch.
1412 * We also ramp up the dispatch depth gradually for async IO,
1413 * based on the last sync IO we serviced
1415 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
1416 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
1419 depth = last_sync / cfqd->cfq_slice[1];
1420 if (!depth && !cfqq->dispatched)
1422 if (depth < max_dispatch)
1423 max_dispatch = depth;
1427 * If we're below the current max, allow a dispatch
1429 return cfqq->dispatched < max_dispatch;
1433 * Dispatch a request from cfqq, moving them to the request queue
1436 static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1440 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1442 if (!cfq_may_dispatch(cfqd, cfqq))
1446 * follow expired path, else get first next available
1448 rq = cfq_check_fifo(cfqq);
1453 * insert request into driver dispatch list
1455 cfq_dispatch_insert(cfqd->queue, rq);
1457 if (!cfqd->active_cic) {
1458 struct cfq_io_context *cic = RQ_CIC(rq);
1460 atomic_long_inc(&cic->ioc->refcount);
1461 cfqd->active_cic = cic;
1468 * Find the cfqq that we need to service and move a request from that to the
1471 static int cfq_dispatch_requests(struct request_queue *q, int force)
1473 struct cfq_data *cfqd = q->elevator->elevator_data;
1474 struct cfq_queue *cfqq;
1476 if (!cfqd->busy_queues)
1479 if (unlikely(force))
1480 return cfq_forced_dispatch(cfqd);
1482 cfqq = cfq_select_queue(cfqd);
1487 * Dispatch a request from this cfqq, if it is allowed
1489 if (!cfq_dispatch_request(cfqd, cfqq))
1492 cfqq->slice_dispatch++;
1493 cfq_clear_cfqq_must_dispatch(cfqq);
1496 * expire an async queue immediately if it has used up its slice. idle
1497 * queue always expire after 1 dispatch round.
1499 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1500 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1501 cfq_class_idle(cfqq))) {
1502 cfqq->slice_end = jiffies + 1;
1503 cfq_slice_expired(cfqd, 0);
1506 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
1511 * task holds one reference to the queue, dropped when task exits. each rq
1512 * in-flight on this queue also holds a reference, dropped when rq is freed.
1514 * queue lock must be held here.
1516 static void cfq_put_queue(struct cfq_queue *cfqq)
1518 struct cfq_data *cfqd = cfqq->cfqd;
1520 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1522 if (!atomic_dec_and_test(&cfqq->ref))
1525 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1526 BUG_ON(rb_first(&cfqq->sort_list));
1527 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1528 BUG_ON(cfq_cfqq_on_rr(cfqq));
1530 if (unlikely(cfqd->active_queue == cfqq)) {
1531 __cfq_slice_expired(cfqd, cfqq, 0);
1532 cfq_schedule_dispatch(cfqd);
1535 kmem_cache_free(cfq_pool, cfqq);
1539 * Must always be called with the rcu_read_lock() held
1542 __call_for_each_cic(struct io_context *ioc,
1543 void (*func)(struct io_context *, struct cfq_io_context *))
1545 struct cfq_io_context *cic;
1546 struct hlist_node *n;
1548 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
1553 * Call func for each cic attached to this ioc.
1556 call_for_each_cic(struct io_context *ioc,
1557 void (*func)(struct io_context *, struct cfq_io_context *))
1560 __call_for_each_cic(ioc, func);
1564 static void cfq_cic_free_rcu(struct rcu_head *head)
1566 struct cfq_io_context *cic;
1568 cic = container_of(head, struct cfq_io_context, rcu_head);
1570 kmem_cache_free(cfq_ioc_pool, cic);
1571 elv_ioc_count_dec(cfq_ioc_count);
1575 * CFQ scheduler is exiting, grab exit lock and check
1576 * the pending io context count. If it hits zero,
1577 * complete ioc_gone and set it back to NULL
1579 spin_lock(&ioc_gone_lock);
1580 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
1584 spin_unlock(&ioc_gone_lock);
1588 static void cfq_cic_free(struct cfq_io_context *cic)
1590 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
1593 static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
1595 unsigned long flags;
1597 BUG_ON(!cic->dead_key);
1599 spin_lock_irqsave(&ioc->lock, flags);
1600 radix_tree_delete(&ioc->radix_root, cic->dead_key);
1601 hlist_del_rcu(&cic->cic_list);
1602 spin_unlock_irqrestore(&ioc->lock, flags);
1608 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
1609 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
1610 * and ->trim() which is called with the task lock held
1612 static void cfq_free_io_context(struct io_context *ioc)
1615 * ioc->refcount is zero here, or we are called from elv_unregister(),
1616 * so no more cic's are allowed to be linked into this ioc. So it
1617 * should be ok to iterate over the known list, we will see all cic's
1618 * since no new ones are added.
1620 __call_for_each_cic(ioc, cic_free_func);
1623 static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1625 struct cfq_queue *__cfqq, *next;
1627 if (unlikely(cfqq == cfqd->active_queue)) {
1628 __cfq_slice_expired(cfqd, cfqq, 0);
1629 cfq_schedule_dispatch(cfqd);
1633 * If this queue was scheduled to merge with another queue, be
1634 * sure to drop the reference taken on that queue (and others in
1635 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
1637 __cfqq = cfqq->new_cfqq;
1639 if (__cfqq == cfqq) {
1640 WARN(1, "cfqq->new_cfqq loop detected\n");
1643 next = __cfqq->new_cfqq;
1644 cfq_put_queue(__cfqq);
1648 cfq_put_queue(cfqq);
1651 static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1652 struct cfq_io_context *cic)
1654 struct io_context *ioc = cic->ioc;
1656 list_del_init(&cic->queue_list);
1659 * Make sure key == NULL is seen for dead queues
1662 cic->dead_key = (unsigned long) cic->key;
1665 if (ioc->ioc_data == cic)
1666 rcu_assign_pointer(ioc->ioc_data, NULL);
1668 if (cic->cfqq[BLK_RW_ASYNC]) {
1669 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
1670 cic->cfqq[BLK_RW_ASYNC] = NULL;
1673 if (cic->cfqq[BLK_RW_SYNC]) {
1674 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
1675 cic->cfqq[BLK_RW_SYNC] = NULL;
1679 static void cfq_exit_single_io_context(struct io_context *ioc,
1680 struct cfq_io_context *cic)
1682 struct cfq_data *cfqd = cic->key;
1685 struct request_queue *q = cfqd->queue;
1686 unsigned long flags;
1688 spin_lock_irqsave(q->queue_lock, flags);
1691 * Ensure we get a fresh copy of the ->key to prevent
1692 * race between exiting task and queue
1694 smp_read_barrier_depends();
1696 __cfq_exit_single_io_context(cfqd, cic);
1698 spin_unlock_irqrestore(q->queue_lock, flags);
1703 * The process that ioc belongs to has exited, we need to clean up
1704 * and put the internal structures we have that belongs to that process.
1706 static void cfq_exit_io_context(struct io_context *ioc)
1708 call_for_each_cic(ioc, cfq_exit_single_io_context);
1711 static struct cfq_io_context *
1712 cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1714 struct cfq_io_context *cic;
1716 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
1719 cic->last_end_request = jiffies;
1720 INIT_LIST_HEAD(&cic->queue_list);
1721 INIT_HLIST_NODE(&cic->cic_list);
1722 cic->dtor = cfq_free_io_context;
1723 cic->exit = cfq_exit_io_context;
1724 elv_ioc_count_inc(cfq_ioc_count);
1730 static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
1732 struct task_struct *tsk = current;
1735 if (!cfq_cfqq_prio_changed(cfqq))
1738 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
1739 switch (ioprio_class) {
1741 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1742 case IOPRIO_CLASS_NONE:
1744 * no prio set, inherit CPU scheduling settings
1746 cfqq->ioprio = task_nice_ioprio(tsk);
1747 cfqq->ioprio_class = task_nice_ioclass(tsk);
1749 case IOPRIO_CLASS_RT:
1750 cfqq->ioprio = task_ioprio(ioc);
1751 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1753 case IOPRIO_CLASS_BE:
1754 cfqq->ioprio = task_ioprio(ioc);
1755 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1757 case IOPRIO_CLASS_IDLE:
1758 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1760 cfq_clear_cfqq_idle_window(cfqq);
1765 * keep track of original prio settings in case we have to temporarily
1766 * elevate the priority of this queue
1768 cfqq->org_ioprio = cfqq->ioprio;
1769 cfqq->org_ioprio_class = cfqq->ioprio_class;
1770 cfq_clear_cfqq_prio_changed(cfqq);
1773 static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
1775 struct cfq_data *cfqd = cic->key;
1776 struct cfq_queue *cfqq;
1777 unsigned long flags;
1779 if (unlikely(!cfqd))
1782 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1784 cfqq = cic->cfqq[BLK_RW_ASYNC];
1786 struct cfq_queue *new_cfqq;
1787 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
1790 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
1791 cfq_put_queue(cfqq);
1795 cfqq = cic->cfqq[BLK_RW_SYNC];
1797 cfq_mark_cfqq_prio_changed(cfqq);
1799 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1802 static void cfq_ioc_set_ioprio(struct io_context *ioc)
1804 call_for_each_cic(ioc, changed_ioprio);
1805 ioc->ioprio_changed = 0;
1808 static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1809 pid_t pid, bool is_sync)
1811 RB_CLEAR_NODE(&cfqq->rb_node);
1812 RB_CLEAR_NODE(&cfqq->p_node);
1813 INIT_LIST_HEAD(&cfqq->fifo);
1815 atomic_set(&cfqq->ref, 0);
1818 cfq_mark_cfqq_prio_changed(cfqq);
1821 if (!cfq_class_idle(cfqq))
1822 cfq_mark_cfqq_idle_window(cfqq);
1823 cfq_mark_cfqq_sync(cfqq);
1828 static struct cfq_queue *
1829 cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
1830 struct io_context *ioc, gfp_t gfp_mask)
1832 struct cfq_queue *cfqq, *new_cfqq = NULL;
1833 struct cfq_io_context *cic;
1836 cic = cfq_cic_lookup(cfqd, ioc);
1837 /* cic always exists here */
1838 cfqq = cic_to_cfqq(cic, is_sync);
1841 * Always try a new alloc if we fell back to the OOM cfqq
1842 * originally, since it should just be a temporary situation.
1844 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
1849 } else if (gfp_mask & __GFP_WAIT) {
1850 spin_unlock_irq(cfqd->queue->queue_lock);
1851 new_cfqq = kmem_cache_alloc_node(cfq_pool,
1852 gfp_mask | __GFP_ZERO,
1854 spin_lock_irq(cfqd->queue->queue_lock);
1858 cfqq = kmem_cache_alloc_node(cfq_pool,
1859 gfp_mask | __GFP_ZERO,
1864 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
1865 cfq_init_prio_data(cfqq, ioc);
1866 cfq_log_cfqq(cfqd, cfqq, "alloced");
1868 cfqq = &cfqd->oom_cfqq;
1872 kmem_cache_free(cfq_pool, new_cfqq);
1877 static struct cfq_queue **
1878 cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
1880 switch (ioprio_class) {
1881 case IOPRIO_CLASS_RT:
1882 return &cfqd->async_cfqq[0][ioprio];
1883 case IOPRIO_CLASS_BE:
1884 return &cfqd->async_cfqq[1][ioprio];
1885 case IOPRIO_CLASS_IDLE:
1886 return &cfqd->async_idle_cfqq;
1892 static struct cfq_queue *
1893 cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
1896 const int ioprio = task_ioprio(ioc);
1897 const int ioprio_class = task_ioprio_class(ioc);
1898 struct cfq_queue **async_cfqq = NULL;
1899 struct cfq_queue *cfqq = NULL;
1902 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
1907 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
1910 * pin the queue now that it's allocated, scheduler exit will prune it
1912 if (!is_sync && !(*async_cfqq)) {
1913 atomic_inc(&cfqq->ref);
1917 atomic_inc(&cfqq->ref);
1922 * We drop cfq io contexts lazily, so we may find a dead one.
1925 cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
1926 struct cfq_io_context *cic)
1928 unsigned long flags;
1930 WARN_ON(!list_empty(&cic->queue_list));
1932 spin_lock_irqsave(&ioc->lock, flags);
1934 BUG_ON(ioc->ioc_data == cic);
1936 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
1937 hlist_del_rcu(&cic->cic_list);
1938 spin_unlock_irqrestore(&ioc->lock, flags);
1943 static struct cfq_io_context *
1944 cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1946 struct cfq_io_context *cic;
1947 unsigned long flags;
1956 * we maintain a last-hit cache, to avoid browsing over the tree
1958 cic = rcu_dereference(ioc->ioc_data);
1959 if (cic && cic->key == cfqd) {
1965 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
1969 /* ->key must be copied to avoid race with cfq_exit_queue() */
1972 cfq_drop_dead_cic(cfqd, ioc, cic);
1977 spin_lock_irqsave(&ioc->lock, flags);
1978 rcu_assign_pointer(ioc->ioc_data, cic);
1979 spin_unlock_irqrestore(&ioc->lock, flags);
1987 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
1988 * the process specific cfq io context when entered from the block layer.
1989 * Also adds the cic to a per-cfqd list, used when this queue is removed.
1991 static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1992 struct cfq_io_context *cic, gfp_t gfp_mask)
1994 unsigned long flags;
1997 ret = radix_tree_preload(gfp_mask);
2002 spin_lock_irqsave(&ioc->lock, flags);
2003 ret = radix_tree_insert(&ioc->radix_root,
2004 (unsigned long) cfqd, cic);
2006 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
2007 spin_unlock_irqrestore(&ioc->lock, flags);
2009 radix_tree_preload_end();
2012 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2013 list_add(&cic->queue_list, &cfqd->cic_list);
2014 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2019 printk(KERN_ERR "cfq: cic link failed!\n");
2025 * Setup general io context and cfq io context. There can be several cfq
2026 * io contexts per general io context, if this process is doing io to more
2027 * than one device managed by cfq.
2029 static struct cfq_io_context *
2030 cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
2032 struct io_context *ioc = NULL;
2033 struct cfq_io_context *cic;
2035 might_sleep_if(gfp_mask & __GFP_WAIT);
2037 ioc = get_io_context(gfp_mask, cfqd->queue->node);
2041 cic = cfq_cic_lookup(cfqd, ioc);
2045 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2049 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2053 smp_read_barrier_depends();
2054 if (unlikely(ioc->ioprio_changed))
2055 cfq_ioc_set_ioprio(ioc);
2061 put_io_context(ioc);
2066 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2068 unsigned long elapsed = jiffies - cic->last_end_request;
2069 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
2071 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2072 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2073 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2077 cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2083 if (!cfqq->last_request_pos)
2085 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2086 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2088 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2091 * Don't allow the seek distance to get too large from the
2092 * odd fragment, pagein, etc
2094 if (cfqq->seek_samples <= 60) /* second&third seek */
2095 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
2097 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
2099 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2100 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2101 total = cfqq->seek_total + (cfqq->seek_samples/2);
2102 do_div(total, cfqq->seek_samples);
2103 cfqq->seek_mean = (sector_t)total;
2106 * If this cfqq is shared between multiple processes, check to
2107 * make sure that those processes are still issuing I/Os within
2108 * the mean seek distance. If not, it may be time to break the
2109 * queues apart again.
2111 if (cfq_cfqq_coop(cfqq)) {
2112 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2113 cfqq->seeky_start = jiffies;
2114 else if (!CFQQ_SEEKY(cfqq))
2115 cfqq->seeky_start = 0;
2120 * Disable idle window if the process thinks too long or seeks so much that
2124 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2125 struct cfq_io_context *cic)
2127 int old_idle, enable_idle;
2130 * Don't idle for async or idle io prio class
2132 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
2135 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
2137 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
2138 (!cfqd->cfq_latency && cfqd->hw_tag && CFQQ_SEEKY(cfqq)))
2140 else if (sample_valid(cic->ttime_samples)) {
2141 unsigned int slice_idle = cfqd->cfq_slice_idle;
2142 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
2143 slice_idle = msecs_to_jiffies(CFQ_MIN_TT);
2144 if (cic->ttime_mean > slice_idle)
2150 if (old_idle != enable_idle) {
2151 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2153 cfq_mark_cfqq_idle_window(cfqq);
2155 cfq_clear_cfqq_idle_window(cfqq);
2160 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2161 * no or if we aren't sure, a 1 will cause a preempt.
2164 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
2167 struct cfq_queue *cfqq;
2169 cfqq = cfqd->active_queue;
2173 if (cfq_slice_used(cfqq))
2176 if (cfq_class_idle(new_cfqq))
2179 if (cfq_class_idle(cfqq))
2183 * if the new request is sync, but the currently running queue is
2184 * not, let the sync request have priority.
2186 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
2190 * So both queues are sync. Let the new request get disk time if
2191 * it's a metadata request and the current queue is doing regular IO.
2193 if (rq_is_meta(rq) && !cfqq->meta_pending)
2197 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2199 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
2202 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
2206 * if this request is as-good as one we would expect from the
2207 * current cfqq, let it preempt
2209 if (cfq_rq_close(cfqd, cfqq, rq))
2216 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2217 * let it have half of its nominal slice.
2219 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2221 cfq_log_cfqq(cfqd, cfqq, "preempt");
2222 cfq_slice_expired(cfqd, 1);
2225 * Put the new queue at the front of the of the current list,
2226 * so we know that it will be selected next.
2228 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2230 cfq_service_tree_add(cfqd, cfqq, 1);
2232 cfqq->slice_end = 0;
2233 cfq_mark_cfqq_slice_new(cfqq);
2237 * Called when a new fs request (rq) is added (to cfqq). Check if there's
2238 * something we should do about it
2241 cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2244 struct cfq_io_context *cic = RQ_CIC(rq);
2248 cfqq->meta_pending++;
2250 cfq_update_io_thinktime(cfqd, cic);
2251 cfq_update_io_seektime(cfqd, cfqq, rq);
2252 cfq_update_idle_window(cfqd, cfqq, cic);
2254 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
2256 if (cfqq == cfqd->active_queue) {
2258 * Remember that we saw a request from this process, but
2259 * don't start queuing just yet. Otherwise we risk seeing lots
2260 * of tiny requests, because we disrupt the normal plugging
2261 * and merging. If the request is already larger than a single
2262 * page, let it rip immediately. For that case we assume that
2263 * merging is already done. Ditto for a busy system that
2264 * has other work pending, don't risk delaying until the
2265 * idle timer unplug to continue working.
2267 if (cfq_cfqq_wait_request(cfqq)) {
2268 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2269 cfqd->busy_queues > 1) {
2270 del_timer(&cfqd->idle_slice_timer);
2271 __blk_run_queue(cfqd->queue);
2273 cfq_mark_cfqq_must_dispatch(cfqq);
2275 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
2277 * not the active queue - expire current slice if it is
2278 * idle and has expired it's mean thinktime or this new queue
2279 * has some old slice time left and is of higher priority or
2280 * this new queue is RT and the current one is BE
2282 cfq_preempt_queue(cfqd, cfqq);
2283 __blk_run_queue(cfqd->queue);
2287 static void cfq_insert_request(struct request_queue *q, struct request *rq)
2289 struct cfq_data *cfqd = q->elevator->elevator_data;
2290 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2292 cfq_log_cfqq(cfqd, cfqq, "insert_request");
2293 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
2297 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
2298 list_add_tail(&rq->queuelist, &cfqq->fifo);
2300 cfq_rq_enqueued(cfqd, cfqq, rq);
2304 * Update hw_tag based on peak queue depth over 50 samples under
2307 static void cfq_update_hw_tag(struct cfq_data *cfqd)
2309 struct cfq_queue *cfqq = cfqd->active_queue;
2311 if (rq_in_driver(cfqd) > cfqd->rq_in_driver_peak)
2312 cfqd->rq_in_driver_peak = rq_in_driver(cfqd);
2314 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
2315 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
2319 * If active queue hasn't enough requests and can idle, cfq might not
2320 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
2323 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
2324 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
2325 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
2328 if (cfqd->hw_tag_samples++ < 50)
2331 if (cfqd->rq_in_driver_peak >= CFQ_HW_QUEUE_MIN)
2336 cfqd->hw_tag_samples = 0;
2337 cfqd->rq_in_driver_peak = 0;
2340 static void cfq_completed_request(struct request_queue *q, struct request *rq)
2342 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2343 struct cfq_data *cfqd = cfqq->cfqd;
2344 const int sync = rq_is_sync(rq);
2348 cfq_log_cfqq(cfqd, cfqq, "complete");
2350 cfq_update_hw_tag(cfqd);
2352 WARN_ON(!cfqd->rq_in_driver[sync]);
2353 WARN_ON(!cfqq->dispatched);
2354 cfqd->rq_in_driver[sync]--;
2357 if (cfq_cfqq_sync(cfqq))
2358 cfqd->sync_flight--;
2361 RQ_CIC(rq)->last_end_request = now;
2362 cfqd->last_end_sync_rq = now;
2366 * If this is the active queue, check if it needs to be expired,
2367 * or if we want to idle in case it has no pending requests.
2369 if (cfqd->active_queue == cfqq) {
2370 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
2372 if (cfq_cfqq_slice_new(cfqq)) {
2373 cfq_set_prio_slice(cfqd, cfqq);
2374 cfq_clear_cfqq_slice_new(cfqq);
2377 * If there are no requests waiting in this queue, and
2378 * there are other queues ready to issue requests, AND
2379 * those other queues are issuing requests within our
2380 * mean seek distance, give them a chance to run instead
2383 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
2384 cfq_slice_expired(cfqd, 1);
2385 else if (cfqq_empty && !cfq_close_cooperator(cfqd, cfqq) &&
2386 sync && !rq_noidle(rq))
2387 cfq_arm_slice_timer(cfqd);
2390 if (!rq_in_driver(cfqd))
2391 cfq_schedule_dispatch(cfqd);
2395 * we temporarily boost lower priority queues if they are holding fs exclusive
2396 * resources. they are boosted to normal prio (CLASS_BE/4)
2398 static void cfq_prio_boost(struct cfq_queue *cfqq)
2400 if (has_fs_excl()) {
2402 * boost idle prio on transactions that would lock out other
2403 * users of the filesystem
2405 if (cfq_class_idle(cfqq))
2406 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2407 if (cfqq->ioprio > IOPRIO_NORM)
2408 cfqq->ioprio = IOPRIO_NORM;
2411 * check if we need to unboost the queue
2413 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
2414 cfqq->ioprio_class = cfqq->org_ioprio_class;
2415 if (cfqq->ioprio != cfqq->org_ioprio)
2416 cfqq->ioprio = cfqq->org_ioprio;
2420 static inline int __cfq_may_queue(struct cfq_queue *cfqq)
2422 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
2423 cfq_mark_cfqq_must_alloc_slice(cfqq);
2424 return ELV_MQUEUE_MUST;
2427 return ELV_MQUEUE_MAY;
2430 static int cfq_may_queue(struct request_queue *q, int rw)
2432 struct cfq_data *cfqd = q->elevator->elevator_data;
2433 struct task_struct *tsk = current;
2434 struct cfq_io_context *cic;
2435 struct cfq_queue *cfqq;
2438 * don't force setup of a queue from here, as a call to may_queue
2439 * does not necessarily imply that a request actually will be queued.
2440 * so just lookup a possibly existing queue, or return 'may queue'
2443 cic = cfq_cic_lookup(cfqd, tsk->io_context);
2445 return ELV_MQUEUE_MAY;
2447 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
2449 cfq_init_prio_data(cfqq, cic->ioc);
2450 cfq_prio_boost(cfqq);
2452 return __cfq_may_queue(cfqq);
2455 return ELV_MQUEUE_MAY;
2459 * queue lock held here
2461 static void cfq_put_request(struct request *rq)
2463 struct cfq_queue *cfqq = RQ_CFQQ(rq);
2466 const int rw = rq_data_dir(rq);
2468 BUG_ON(!cfqq->allocated[rw]);
2469 cfqq->allocated[rw]--;
2471 put_io_context(RQ_CIC(rq)->ioc);
2473 rq->elevator_private = NULL;
2474 rq->elevator_private2 = NULL;
2476 cfq_put_queue(cfqq);
2480 static struct cfq_queue *
2481 cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
2482 struct cfq_queue *cfqq)
2484 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
2485 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
2486 cfq_mark_cfqq_coop(cfqq->new_cfqq);
2487 cfq_put_queue(cfqq);
2488 return cic_to_cfqq(cic, 1);
2491 static int should_split_cfqq(struct cfq_queue *cfqq)
2493 if (cfqq->seeky_start &&
2494 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
2500 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
2501 * was the last process referring to said cfqq.
2503 static struct cfq_queue *
2504 split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
2506 if (cfqq_process_refs(cfqq) == 1) {
2507 cfqq->seeky_start = 0;
2508 cfqq->pid = current->pid;
2509 cfq_clear_cfqq_coop(cfqq);
2513 cic_set_cfqq(cic, NULL, 1);
2514 cfq_put_queue(cfqq);
2518 * Allocate cfq data structures associated with this request.
2521 cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
2523 struct cfq_data *cfqd = q->elevator->elevator_data;
2524 struct cfq_io_context *cic;
2525 const int rw = rq_data_dir(rq);
2526 const bool is_sync = rq_is_sync(rq);
2527 struct cfq_queue *cfqq;
2528 unsigned long flags;
2530 might_sleep_if(gfp_mask & __GFP_WAIT);
2532 cic = cfq_get_io_context(cfqd, gfp_mask);
2534 spin_lock_irqsave(q->queue_lock, flags);
2540 cfqq = cic_to_cfqq(cic, is_sync);
2541 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2542 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
2543 cic_set_cfqq(cic, cfqq, is_sync);
2546 * If the queue was seeky for too long, break it apart.
2548 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
2549 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
2550 cfqq = split_cfqq(cic, cfqq);
2556 * Check to see if this queue is scheduled to merge with
2557 * another, closely cooperating queue. The merging of
2558 * queues happens here as it must be done in process context.
2559 * The reference on new_cfqq was taken in merge_cfqqs.
2562 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
2565 cfqq->allocated[rw]++;
2566 atomic_inc(&cfqq->ref);
2568 spin_unlock_irqrestore(q->queue_lock, flags);
2570 rq->elevator_private = cic;
2571 rq->elevator_private2 = cfqq;
2576 put_io_context(cic->ioc);
2578 cfq_schedule_dispatch(cfqd);
2579 spin_unlock_irqrestore(q->queue_lock, flags);
2580 cfq_log(cfqd, "set_request fail");
2584 static void cfq_kick_queue(struct work_struct *work)
2586 struct cfq_data *cfqd =
2587 container_of(work, struct cfq_data, unplug_work);
2588 struct request_queue *q = cfqd->queue;
2590 spin_lock_irq(q->queue_lock);
2591 __blk_run_queue(cfqd->queue);
2592 spin_unlock_irq(q->queue_lock);
2596 * Timer running if the active_queue is currently idling inside its time slice
2598 static void cfq_idle_slice_timer(unsigned long data)
2600 struct cfq_data *cfqd = (struct cfq_data *) data;
2601 struct cfq_queue *cfqq;
2602 unsigned long flags;
2605 cfq_log(cfqd, "idle timer fired");
2607 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2609 cfqq = cfqd->active_queue;
2614 * We saw a request before the queue expired, let it through
2616 if (cfq_cfqq_must_dispatch(cfqq))
2622 if (cfq_slice_used(cfqq))
2626 * only expire and reinvoke request handler, if there are
2627 * other queues with pending requests
2629 if (!cfqd->busy_queues)
2633 * not expired and it has a request pending, let it dispatch
2635 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
2639 cfq_slice_expired(cfqd, timed_out);
2641 cfq_schedule_dispatch(cfqd);
2643 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2646 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2648 del_timer_sync(&cfqd->idle_slice_timer);
2649 cancel_work_sync(&cfqd->unplug_work);
2652 static void cfq_put_async_queues(struct cfq_data *cfqd)
2656 for (i = 0; i < IOPRIO_BE_NR; i++) {
2657 if (cfqd->async_cfqq[0][i])
2658 cfq_put_queue(cfqd->async_cfqq[0][i]);
2659 if (cfqd->async_cfqq[1][i])
2660 cfq_put_queue(cfqd->async_cfqq[1][i]);
2663 if (cfqd->async_idle_cfqq)
2664 cfq_put_queue(cfqd->async_idle_cfqq);
2667 static void cfq_exit_queue(struct elevator_queue *e)
2669 struct cfq_data *cfqd = e->elevator_data;
2670 struct request_queue *q = cfqd->queue;
2672 cfq_shutdown_timer_wq(cfqd);
2674 spin_lock_irq(q->queue_lock);
2676 if (cfqd->active_queue)
2677 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2679 while (!list_empty(&cfqd->cic_list)) {
2680 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2681 struct cfq_io_context,
2684 __cfq_exit_single_io_context(cfqd, cic);
2687 cfq_put_async_queues(cfqd);
2689 spin_unlock_irq(q->queue_lock);
2691 cfq_shutdown_timer_wq(cfqd);
2696 static void *cfq_init_queue(struct request_queue *q)
2698 struct cfq_data *cfqd;
2701 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
2705 cfqd->service_tree = CFQ_RB_ROOT;
2708 * Not strictly needed (since RB_ROOT just clears the node and we
2709 * zeroed cfqd on alloc), but better be safe in case someone decides
2710 * to add magic to the rb code
2712 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2713 cfqd->prio_trees[i] = RB_ROOT;
2716 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
2717 * Grab a permanent reference to it, so that the normal code flow
2718 * will not attempt to free it.
2720 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
2721 atomic_inc(&cfqd->oom_cfqq.ref);
2723 INIT_LIST_HEAD(&cfqd->cic_list);
2727 init_timer(&cfqd->idle_slice_timer);
2728 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2729 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2731 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
2733 cfqd->cfq_quantum = cfq_quantum;
2734 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2735 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2736 cfqd->cfq_back_max = cfq_back_max;
2737 cfqd->cfq_back_penalty = cfq_back_penalty;
2738 cfqd->cfq_slice[0] = cfq_slice_async;
2739 cfqd->cfq_slice[1] = cfq_slice_sync;
2740 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2741 cfqd->cfq_slice_idle = cfq_slice_idle;
2742 cfqd->cfq_latency = 1;
2744 cfqd->last_end_sync_rq = jiffies;
2748 static void cfq_slab_kill(void)
2751 * Caller already ensured that pending RCU callbacks are completed,
2752 * so we should have no busy allocations at this point.
2755 kmem_cache_destroy(cfq_pool);
2757 kmem_cache_destroy(cfq_ioc_pool);
2760 static int __init cfq_slab_setup(void)
2762 cfq_pool = KMEM_CACHE(cfq_queue, 0);
2766 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
2777 * sysfs parts below -->
2780 cfq_var_show(unsigned int var, char *page)
2782 return sprintf(page, "%d\n", var);
2786 cfq_var_store(unsigned int *var, const char *page, size_t count)
2788 char *p = (char *) page;
2790 *var = simple_strtoul(p, &p, 10);
2794 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2795 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
2797 struct cfq_data *cfqd = e->elevator_data; \
2798 unsigned int __data = __VAR; \
2800 __data = jiffies_to_msecs(__data); \
2801 return cfq_var_show(__data, (page)); \
2803 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2804 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2805 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2806 SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2807 SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
2808 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2809 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2810 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2811 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2812 SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
2813 #undef SHOW_FUNCTION
2815 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2816 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
2818 struct cfq_data *cfqd = e->elevator_data; \
2819 unsigned int __data; \
2820 int ret = cfq_var_store(&__data, (page), count); \
2821 if (__data < (MIN)) \
2823 else if (__data > (MAX)) \
2826 *(__PTR) = msecs_to_jiffies(__data); \
2828 *(__PTR) = __data; \
2831 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2832 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
2834 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
2836 STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2837 STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
2839 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2840 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2841 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2842 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
2844 STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
2845 #undef STORE_FUNCTION
2847 #define CFQ_ATTR(name) \
2848 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2850 static struct elv_fs_entry cfq_attrs[] = {
2852 CFQ_ATTR(fifo_expire_sync),
2853 CFQ_ATTR(fifo_expire_async),
2854 CFQ_ATTR(back_seek_max),
2855 CFQ_ATTR(back_seek_penalty),
2856 CFQ_ATTR(slice_sync),
2857 CFQ_ATTR(slice_async),
2858 CFQ_ATTR(slice_async_rq),
2859 CFQ_ATTR(slice_idle),
2860 CFQ_ATTR(low_latency),
2864 static struct elevator_type iosched_cfq = {
2866 .elevator_merge_fn = cfq_merge,
2867 .elevator_merged_fn = cfq_merged_request,
2868 .elevator_merge_req_fn = cfq_merged_requests,
2869 .elevator_allow_merge_fn = cfq_allow_merge,
2870 .elevator_dispatch_fn = cfq_dispatch_requests,
2871 .elevator_add_req_fn = cfq_insert_request,
2872 .elevator_activate_req_fn = cfq_activate_request,
2873 .elevator_deactivate_req_fn = cfq_deactivate_request,
2874 .elevator_queue_empty_fn = cfq_queue_empty,
2875 .elevator_completed_req_fn = cfq_completed_request,
2876 .elevator_former_req_fn = elv_rb_former_request,
2877 .elevator_latter_req_fn = elv_rb_latter_request,
2878 .elevator_set_req_fn = cfq_set_request,
2879 .elevator_put_req_fn = cfq_put_request,
2880 .elevator_may_queue_fn = cfq_may_queue,
2881 .elevator_init_fn = cfq_init_queue,
2882 .elevator_exit_fn = cfq_exit_queue,
2883 .trim = cfq_free_io_context,
2885 .elevator_attrs = cfq_attrs,
2886 .elevator_name = "cfq",
2887 .elevator_owner = THIS_MODULE,
2890 static int __init cfq_init(void)
2893 * could be 0 on HZ < 1000 setups
2895 if (!cfq_slice_async)
2896 cfq_slice_async = 1;
2897 if (!cfq_slice_idle)
2900 if (cfq_slab_setup())
2903 elv_register(&iosched_cfq);
2908 static void __exit cfq_exit(void)
2910 DECLARE_COMPLETION_ONSTACK(all_gone);
2911 elv_unregister(&iosched_cfq);
2912 ioc_gone = &all_gone;
2913 /* ioc_gone's update must be visible before reading ioc_count */
2917 * this also protects us from entering cfq_slab_kill() with
2918 * pending RCU callbacks
2920 if (elv_ioc_count_read(cfq_ioc_count))
2921 wait_for_completion(&all_gone);
2925 module_init(cfq_init);
2926 module_exit(cfq_exit);
2928 MODULE_AUTHOR("Jens Axboe");
2929 MODULE_LICENSE("GPL");
2930 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");