2 * linux/drivers/block/cfq-iosched.c
4 * CFQ, or complete fairness queueing, disk scheduler.
6 * Based on ideas from a previously unfinished io
7 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
9 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
11 #include <linux/kernel.h>
13 #include <linux/blkdev.h>
14 #include <linux/elevator.h>
15 #include <linux/bio.h>
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/compiler.h>
21 #include <linux/hash.h>
22 #include <linux/rbtree.h>
23 #include <linux/mempool.h>
24 #include <linux/ioprio.h>
25 #include <linux/writeback.h>
30 static int cfq_quantum = 4; /* max queue in one round of service */
31 static int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
32 static int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
33 static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
34 static int cfq_back_penalty = 2; /* penalty of a backwards seek */
36 static int cfq_slice_sync = HZ / 10;
37 static int cfq_slice_async = HZ / 25;
38 static int cfq_slice_async_rq = 2;
39 static int cfq_slice_idle = HZ / 100;
41 #define CFQ_IDLE_GRACE (HZ / 10)
42 #define CFQ_SLICE_SCALE (5)
44 #define CFQ_KEY_ASYNC (0)
45 #define CFQ_KEY_ANY (0xffff)
48 * disable queueing at the driver/hardware level
50 static int cfq_max_depth = 1;
53 * for the hash of cfqq inside the cfqd
55 #define CFQ_QHASH_SHIFT 6
56 #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
57 #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
60 * for the hash of crq inside the cfqq
62 #define CFQ_MHASH_SHIFT 6
63 #define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
64 #define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
65 #define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
66 #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
67 #define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
69 #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
70 #define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
72 #define RQ_DATA(rq) (rq)->elevator_private
78 #define RB_EMPTY(node) ((node)->rb_node == NULL)
79 #define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
80 #define RB_CLEAR(node) do { \
81 (node)->rb_parent = NULL; \
82 RB_CLEAR_COLOR((node)); \
83 (node)->rb_right = NULL; \
84 (node)->rb_left = NULL; \
86 #define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
87 #define ON_RB(node) ((node)->rb_color != RB_NONE)
88 #define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
89 #define rq_rb_key(rq) (rq)->sector
91 static kmem_cache_t *crq_pool;
92 static kmem_cache_t *cfq_pool;
93 static kmem_cache_t *cfq_ioc_pool;
95 #define CFQ_PRIO_LISTS IOPRIO_BE_NR
96 #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
97 #define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
98 #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
103 #define cfq_cfqq_dispatched(cfqq) \
104 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
106 #define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
108 #define cfq_cfqq_sync(cfqq) \
109 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
112 * Per block device queue structure
116 request_queue_t *queue;
119 * rr list of queues with requests and the count of them
121 struct list_head rr_list[CFQ_PRIO_LISTS];
122 struct list_head busy_rr;
123 struct list_head cur_rr;
124 struct list_head idle_rr;
125 unsigned int busy_queues;
128 * non-ordered list of empty cfqq's
130 struct list_head empty_list;
135 struct hlist_head *cfq_hash;
138 * global crq hash for all queues
140 struct hlist_head *crq_hash;
142 unsigned int max_queued;
149 * schedule slice state info
152 * idle window management
154 struct timer_list idle_slice_timer;
155 struct work_struct unplug_work;
157 struct cfq_queue *active_queue;
158 struct cfq_io_context *active_cic;
159 int cur_prio, cur_end_prio;
160 unsigned int dispatch_slice;
162 struct timer_list idle_class_timer;
164 sector_t last_sector;
165 unsigned long last_end_request;
167 unsigned int rq_starved;
170 * tunables, see top of file
172 unsigned int cfq_quantum;
173 unsigned int cfq_queued;
174 unsigned int cfq_fifo_expire[2];
175 unsigned int cfq_back_penalty;
176 unsigned int cfq_back_max;
177 unsigned int cfq_slice[2];
178 unsigned int cfq_slice_async_rq;
179 unsigned int cfq_slice_idle;
180 unsigned int cfq_max_depth;
184 * Per process-grouping structure
187 /* reference count */
189 /* parent cfq_data */
190 struct cfq_data *cfqd;
191 /* cfqq lookup hash */
192 struct hlist_node cfq_hash;
195 /* on either rr or empty list of cfqd */
196 struct list_head cfq_list;
197 /* sorted list of pending requests */
198 struct rb_root sort_list;
199 /* if fifo isn't expired, next request to serve */
200 struct cfq_rq *next_crq;
201 /* requests queued in sort_list */
203 /* currently allocated requests */
205 /* fifo list of requests in sort_list */
206 struct list_head fifo;
208 unsigned long slice_start;
209 unsigned long slice_end;
210 unsigned long slice_left;
211 unsigned long service_last;
213 /* number of requests that are on the dispatch list */
216 /* io prio of this group */
217 unsigned short ioprio, org_ioprio;
218 unsigned short ioprio_class, org_ioprio_class;
220 /* various state flags, see below */
225 struct rb_node rb_node;
227 struct request *request;
228 struct hlist_node hash;
230 struct cfq_queue *cfq_queue;
231 struct cfq_io_context *io_context;
233 unsigned int crq_flags;
236 enum cfqq_state_flags {
237 CFQ_CFQQ_FLAG_on_rr = 0,
238 CFQ_CFQQ_FLAG_wait_request,
239 CFQ_CFQQ_FLAG_must_alloc,
240 CFQ_CFQQ_FLAG_must_alloc_slice,
241 CFQ_CFQQ_FLAG_must_dispatch,
242 CFQ_CFQQ_FLAG_fifo_expire,
243 CFQ_CFQQ_FLAG_idle_window,
244 CFQ_CFQQ_FLAG_prio_changed,
245 CFQ_CFQQ_FLAG_expired,
248 #define CFQ_CFQQ_FNS(name) \
249 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
251 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
253 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
255 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
257 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
259 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
263 CFQ_CFQQ_FNS(wait_request);
264 CFQ_CFQQ_FNS(must_alloc);
265 CFQ_CFQQ_FNS(must_alloc_slice);
266 CFQ_CFQQ_FNS(must_dispatch);
267 CFQ_CFQQ_FNS(fifo_expire);
268 CFQ_CFQQ_FNS(idle_window);
269 CFQ_CFQQ_FNS(prio_changed);
270 CFQ_CFQQ_FNS(expired);
273 enum cfq_rq_state_flags {
274 CFQ_CRQ_FLAG_in_flight = 0,
275 CFQ_CRQ_FLAG_in_driver,
276 CFQ_CRQ_FLAG_is_sync,
277 CFQ_CRQ_FLAG_requeued,
280 #define CFQ_CRQ_FNS(name) \
281 static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
283 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
285 static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
287 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
289 static inline int cfq_crq_##name(const struct cfq_rq *crq) \
291 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
294 CFQ_CRQ_FNS(in_flight);
295 CFQ_CRQ_FNS(in_driver);
296 CFQ_CRQ_FNS(is_sync);
297 CFQ_CRQ_FNS(requeued);
300 static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
301 static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
302 static void cfq_put_cfqd(struct cfq_data *cfqd);
304 #define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
307 * lots of deadline iosched dupes, can be abstracted later...
309 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
311 hlist_del_init(&crq->hash);
314 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
316 cfq_del_crq_hash(crq);
318 if (q->last_merge == crq->request)
319 q->last_merge = NULL;
322 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
324 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
326 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
329 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
331 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
332 struct hlist_node *entry, *next;
334 hlist_for_each_safe(entry, next, hash_list) {
335 struct cfq_rq *crq = list_entry_hash(entry);
336 struct request *__rq = crq->request;
338 if (!rq_mergeable(__rq)) {
339 cfq_del_crq_hash(crq);
343 if (rq_hash_key(__rq) == offset)
350 static inline int cfq_pending_requests(struct cfq_data *cfqd)
352 return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
356 * scheduler run of queue, if there are requests pending and no one in the
357 * driver that will restart queueing
359 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
361 if (!cfqd->rq_in_driver && cfq_pending_requests(cfqd))
362 kblockd_schedule_work(&cfqd->unplug_work);
365 static int cfq_queue_empty(request_queue_t *q)
367 struct cfq_data *cfqd = q->elevator->elevator_data;
369 return !cfq_pending_requests(cfqd);
373 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
374 * We choose the request that is closest to the head right now. Distance
375 * behind the head are penalized and only allowed to a certain extent.
377 static struct cfq_rq *
378 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
380 sector_t last, s1, s2, d1 = 0, d2 = 0;
381 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
382 unsigned long back_max;
384 if (crq1 == NULL || crq1 == crq2)
388 if (cfq_crq_requeued(crq1))
390 if (cfq_crq_requeued(crq2))
393 s1 = crq1->request->sector;
394 s2 = crq2->request->sector;
396 last = cfqd->last_sector;
399 * by definition, 1KiB is 2 sectors
401 back_max = cfqd->cfq_back_max * 2;
404 * Strict one way elevator _except_ in the case where we allow
405 * short backward seeks which are biased as twice the cost of a
406 * similar forward seek.
410 else if (s1 + back_max >= last)
411 d1 = (last - s1) * cfqd->cfq_back_penalty;
417 else if (s2 + back_max >= last)
418 d2 = (last - s2) * cfqd->cfq_back_penalty;
422 /* Found required data */
423 if (!r1_wrap && r2_wrap)
425 else if (!r2_wrap && r1_wrap)
427 else if (r1_wrap && r2_wrap) {
428 /* both behind the head */
435 /* Both requests in front of the head */
449 * would be nice to take fifo expire time into account as well
451 static struct cfq_rq *
452 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
455 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
456 struct rb_node *rbnext, *rbprev;
459 if (ON_RB(&last->rb_node))
460 rbnext = rb_next(&last->rb_node);
462 rbnext = rb_first(&cfqq->sort_list);
463 if (rbnext == &last->rb_node)
467 rbprev = rb_prev(&last->rb_node);
470 crq_prev = rb_entry_crq(rbprev);
472 crq_next = rb_entry_crq(rbnext);
474 return cfq_choose_req(cfqd, crq_next, crq_prev);
477 static void cfq_update_next_crq(struct cfq_rq *crq)
479 struct cfq_queue *cfqq = crq->cfq_queue;
481 if (cfqq->next_crq == crq)
482 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
485 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
487 struct cfq_data *cfqd = cfqq->cfqd;
488 struct list_head *list, *entry;
490 BUG_ON(!cfq_cfqq_on_rr(cfqq));
492 list_del(&cfqq->cfq_list);
494 if (cfq_class_rt(cfqq))
495 list = &cfqd->cur_rr;
496 else if (cfq_class_idle(cfqq))
497 list = &cfqd->idle_rr;
500 * if cfqq has requests in flight, don't allow it to be
501 * found in cfq_set_active_queue before it has finished them.
502 * this is done to increase fairness between a process that
503 * has lots of io pending vs one that only generates one
504 * sporadically or synchronously
506 if (cfq_cfqq_dispatched(cfqq))
507 list = &cfqd->busy_rr;
509 list = &cfqd->rr_list[cfqq->ioprio];
513 * if queue was preempted, just add to front to be fair. busy_rr
516 if (preempted || list == &cfqd->busy_rr) {
517 list_add(&cfqq->cfq_list, list);
522 * sort by when queue was last serviced
525 while ((entry = entry->prev) != list) {
526 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
528 if (!__cfqq->service_last)
530 if (time_before(__cfqq->service_last, cfqq->service_last))
534 list_add(&cfqq->cfq_list, entry);
538 * add to busy list of queues for service, trying to be fair in ordering
539 * the pending list according to last request service
542 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
544 BUG_ON(cfq_cfqq_on_rr(cfqq));
545 cfq_mark_cfqq_on_rr(cfqq);
548 cfq_resort_rr_list(cfqq, requeue);
552 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
554 BUG_ON(!cfq_cfqq_on_rr(cfqq));
555 cfq_clear_cfqq_on_rr(cfqq);
556 list_move(&cfqq->cfq_list, &cfqd->empty_list);
558 BUG_ON(!cfqd->busy_queues);
563 * rb tree support functions
565 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
567 struct cfq_queue *cfqq = crq->cfq_queue;
569 if (ON_RB(&crq->rb_node)) {
570 struct cfq_data *cfqd = cfqq->cfqd;
571 const int sync = cfq_crq_is_sync(crq);
573 BUG_ON(!cfqq->queued[sync]);
574 cfqq->queued[sync]--;
576 cfq_update_next_crq(crq);
578 rb_erase(&crq->rb_node, &cfqq->sort_list);
579 RB_CLEAR_COLOR(&crq->rb_node);
581 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
582 cfq_del_cfqq_rr(cfqd, cfqq);
586 static struct cfq_rq *
587 __cfq_add_crq_rb(struct cfq_rq *crq)
589 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
590 struct rb_node *parent = NULL;
591 struct cfq_rq *__crq;
595 __crq = rb_entry_crq(parent);
597 if (crq->rb_key < __crq->rb_key)
599 else if (crq->rb_key > __crq->rb_key)
605 rb_link_node(&crq->rb_node, parent, p);
609 static void cfq_add_crq_rb(struct cfq_rq *crq)
611 struct cfq_queue *cfqq = crq->cfq_queue;
612 struct cfq_data *cfqd = cfqq->cfqd;
613 struct request *rq = crq->request;
614 struct cfq_rq *__alias;
616 crq->rb_key = rq_rb_key(rq);
617 cfqq->queued[cfq_crq_is_sync(crq)]++;
620 * looks a little odd, but the first insert might return an alias.
621 * if that happens, put the alias on the dispatch list
623 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
624 cfq_dispatch_sort(cfqd->queue, __alias);
626 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
628 if (!cfq_cfqq_on_rr(cfqq))
629 cfq_add_cfqq_rr(cfqd, cfqq, cfq_crq_requeued(crq));
632 * check if this request is a better next-serve candidate
634 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
638 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
640 if (ON_RB(&crq->rb_node)) {
641 rb_erase(&crq->rb_node, &cfqq->sort_list);
642 cfqq->queued[cfq_crq_is_sync(crq)]--;
648 static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
651 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
657 n = cfqq->sort_list.rb_node;
659 struct cfq_rq *crq = rb_entry_crq(n);
661 if (sector < crq->rb_key)
663 else if (sector > crq->rb_key)
673 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
675 struct cfq_data *cfqd = q->elevator->elevator_data;
676 struct cfq_rq *crq = RQ_DATA(rq);
679 struct cfq_queue *cfqq = crq->cfq_queue;
681 if (cfq_crq_in_driver(crq)) {
682 cfq_clear_crq_in_driver(crq);
683 WARN_ON(!cfqd->rq_in_driver);
684 cfqd->rq_in_driver--;
686 if (cfq_crq_in_flight(crq)) {
687 const int sync = cfq_crq_is_sync(crq);
689 cfq_clear_crq_in_flight(crq);
690 WARN_ON(!cfqq->on_dispatch[sync]);
691 cfqq->on_dispatch[sync]--;
693 cfq_mark_crq_requeued(crq);
698 * make sure the service time gets corrected on reissue of this request
700 static void cfq_requeue_request(request_queue_t *q, struct request *rq)
702 cfq_deactivate_request(q, rq);
703 list_add(&rq->queuelist, &q->queue_head);
706 static void cfq_remove_request(request_queue_t *q, struct request *rq)
708 struct cfq_rq *crq = RQ_DATA(rq);
711 list_del_init(&rq->queuelist);
713 cfq_remove_merge_hints(q, crq);
719 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
721 struct cfq_data *cfqd = q->elevator->elevator_data;
722 struct request *__rq;
725 ret = elv_try_last_merge(q, bio);
726 if (ret != ELEVATOR_NO_MERGE) {
727 __rq = q->last_merge;
731 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
732 if (__rq && elv_rq_merge_ok(__rq, bio)) {
733 ret = ELEVATOR_BACK_MERGE;
737 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
738 if (__rq && elv_rq_merge_ok(__rq, bio)) {
739 ret = ELEVATOR_FRONT_MERGE;
743 return ELEVATOR_NO_MERGE;
745 q->last_merge = __rq;
751 static void cfq_merged_request(request_queue_t *q, struct request *req)
753 struct cfq_data *cfqd = q->elevator->elevator_data;
754 struct cfq_rq *crq = RQ_DATA(req);
756 cfq_del_crq_hash(crq);
757 cfq_add_crq_hash(cfqd, crq);
759 if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
760 struct cfq_queue *cfqq = crq->cfq_queue;
762 cfq_update_next_crq(crq);
763 cfq_reposition_crq_rb(cfqq, crq);
770 cfq_merged_requests(request_queue_t *q, struct request *rq,
771 struct request *next)
773 cfq_merged_request(q, rq);
776 * reposition in fifo if next is older than rq
778 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
779 time_before(next->start_time, rq->start_time))
780 list_move(&rq->queuelist, &next->queuelist);
782 cfq_remove_request(q, next);
786 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
790 * stop potential idle class queues waiting service
792 del_timer(&cfqd->idle_class_timer);
794 cfqq->slice_start = jiffies;
796 cfqq->slice_left = 0;
797 cfq_clear_cfqq_must_alloc_slice(cfqq);
798 cfq_clear_cfqq_fifo_expire(cfqq);
799 cfq_clear_cfqq_expired(cfqq);
802 cfqd->active_queue = cfqq;
815 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
824 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
825 if (!list_empty(&cfqd->rr_list[p])) {
834 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
835 cfqd->cur_end_prio = 0;
842 if (unlikely(prio == -1))
845 BUG_ON(prio >= CFQ_PRIO_LISTS);
847 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
849 cfqd->cur_prio = prio + 1;
850 if (cfqd->cur_prio > cfqd->cur_end_prio) {
851 cfqd->cur_end_prio = cfqd->cur_prio;
854 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
856 cfqd->cur_end_prio = 0;
862 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
864 struct cfq_queue *cfqq;
867 * if current queue is expired but not done with its requests yet,
868 * wait for that to happen
870 if ((cfqq = cfqd->active_queue) != NULL) {
871 if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
876 * if current list is non-empty, grab first entry. if it is empty,
877 * get next prio level and grab first entry then if any are spliced
879 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
880 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
883 * if we have idle queues and no rt or be queues had pending
884 * requests, either allow immediate service if the grace period
885 * has passed or arm the idle grace timer
887 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
888 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
890 if (time_after_eq(jiffies, end))
891 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
893 mod_timer(&cfqd->idle_class_timer, end);
896 __cfq_set_active_queue(cfqd, cfqq);
901 * current cfqq expired its slice (or was too idle), select new one
904 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
907 unsigned long now = jiffies;
909 if (cfq_cfqq_wait_request(cfqq))
910 del_timer(&cfqd->idle_slice_timer);
912 if (!preempted && !cfq_cfqq_dispatched(cfqq))
913 cfqq->service_last = now;
915 cfq_clear_cfqq_must_dispatch(cfqq);
916 cfq_clear_cfqq_wait_request(cfqq);
919 * store what was left of this slice, if the queue idled out
922 if (time_after(now, cfqq->slice_end))
923 cfqq->slice_left = now - cfqq->slice_end;
925 cfqq->slice_left = 0;
927 if (cfq_cfqq_on_rr(cfqq))
928 cfq_resort_rr_list(cfqq, preempted);
930 if (cfqq == cfqd->active_queue)
931 cfqd->active_queue = NULL;
933 if (cfqd->active_cic) {
934 put_io_context(cfqd->active_cic->ioc);
935 cfqd->active_cic = NULL;
938 cfqd->dispatch_slice = 0;
941 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
943 struct cfq_queue *cfqq = cfqd->active_queue;
947 * use deferred expiry, if there are requests in progress as
948 * not to disturb the slice of the next queue
950 if (cfq_cfqq_dispatched(cfqq))
951 cfq_mark_cfqq_expired(cfqq);
953 __cfq_slice_expired(cfqd, cfqq, preempted);
957 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
960 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
961 WARN_ON(cfqq != cfqd->active_queue);
964 * idle is disabled, either manually or by past process history
966 if (!cfqd->cfq_slice_idle)
968 if (!cfq_cfqq_idle_window(cfqq))
971 * task has exited, don't wait
973 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
976 cfq_mark_cfqq_must_dispatch(cfqq);
977 cfq_mark_cfqq_wait_request(cfqq);
979 if (!timer_pending(&cfqd->idle_slice_timer)) {
980 unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
982 cfqd->idle_slice_timer.expires = jiffies + slice_left;
983 add_timer(&cfqd->idle_slice_timer);
990 * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
991 * this function sector sorts the selected request to minimize seeks. we start
992 * at cfqd->last_sector, not 0.
994 static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
996 struct cfq_data *cfqd = q->elevator->elevator_data;
997 struct cfq_queue *cfqq = crq->cfq_queue;
998 struct list_head *head = &q->queue_head, *entry = head;
999 struct request *__rq;
1002 list_del(&crq->request->queuelist);
1004 last = cfqd->last_sector;
1005 list_for_each_entry_reverse(__rq, head, queuelist) {
1006 struct cfq_rq *__crq = RQ_DATA(__rq);
1008 if (blk_barrier_rq(__rq))
1010 if (!blk_fs_request(__rq))
1012 if (cfq_crq_requeued(__crq))
1015 if (__rq->sector <= crq->request->sector)
1017 if (__rq->sector > last && crq->request->sector < last) {
1018 last = crq->request->sector + crq->request->nr_sectors;
1021 entry = &__rq->queuelist;
1024 cfqd->last_sector = last;
1026 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
1028 cfq_del_crq_rb(crq);
1029 cfq_remove_merge_hints(q, crq);
1031 cfq_mark_crq_in_flight(crq);
1032 cfq_clear_crq_requeued(crq);
1034 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
1035 list_add_tail(&crq->request->queuelist, entry);
1039 * return expired entry, or NULL to just start from scratch in rbtree
1041 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
1043 struct cfq_data *cfqd = cfqq->cfqd;
1047 if (cfq_cfqq_fifo_expire(cfqq))
1050 if (!list_empty(&cfqq->fifo)) {
1051 int fifo = cfq_cfqq_class_sync(cfqq);
1053 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
1055 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
1056 cfq_mark_cfqq_fifo_expire(cfqq);
1065 * Scale schedule slice based on io priority. Use the sync time slice only
1066 * if a queue is marked sync and has sync io queued. A sync queue with async
1067 * io only, should not get full sync slice length.
1070 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1072 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
1074 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1076 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
1080 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1082 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1086 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1088 const int base_rq = cfqd->cfq_slice_async_rq;
1090 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1092 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1096 * get next queue for service
1098 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1100 unsigned long now = jiffies;
1101 struct cfq_queue *cfqq;
1103 cfqq = cfqd->active_queue;
1107 if (cfq_cfqq_expired(cfqq))
1113 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1117 * if queue has requests, dispatch one. if not, check if
1118 * enough slice is left to wait for one
1120 if (!RB_EMPTY(&cfqq->sort_list))
1122 else if (!force && cfq_cfqq_class_sync(cfqq) &&
1123 time_before(now, cfqq->slice_end)) {
1124 if (cfq_arm_slice_timer(cfqd, cfqq))
1129 cfq_slice_expired(cfqd, 0);
1131 cfqq = cfq_set_active_queue(cfqd);
1137 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1142 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1148 * follow expired path, else get first next available
1150 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1151 crq = cfqq->next_crq;
1154 * finally, insert request into driver dispatch list
1156 cfq_dispatch_sort(cfqd->queue, crq);
1158 cfqd->dispatch_slice++;
1161 if (!cfqd->active_cic) {
1162 atomic_inc(&crq->io_context->ioc->refcount);
1163 cfqd->active_cic = crq->io_context;
1166 if (RB_EMPTY(&cfqq->sort_list))
1169 } while (dispatched < max_dispatch);
1172 * if slice end isn't set yet, set it. if at least one request was
1173 * sync, use the sync time slice value
1175 if (!cfqq->slice_end)
1176 cfq_set_prio_slice(cfqd, cfqq);
1179 * expire an async queue immediately if it has used up its slice. idle
1180 * queue always expire after 1 dispatch round.
1182 if ((!cfq_cfqq_sync(cfqq) &&
1183 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1184 cfq_class_idle(cfqq))
1185 cfq_slice_expired(cfqd, 0);
1191 cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1193 struct cfq_data *cfqd = q->elevator->elevator_data;
1194 struct cfq_queue *cfqq;
1196 if (!cfqd->busy_queues)
1199 cfqq = cfq_select_queue(cfqd, force);
1201 cfq_clear_cfqq_must_dispatch(cfqq);
1202 cfq_clear_cfqq_wait_request(cfqq);
1203 del_timer(&cfqd->idle_slice_timer);
1205 if (cfq_class_idle(cfqq))
1208 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1214 static inline void cfq_account_dispatch(struct cfq_rq *crq)
1216 struct cfq_queue *cfqq = crq->cfq_queue;
1217 struct cfq_data *cfqd = cfqq->cfqd;
1219 if (unlikely(!blk_fs_request(crq->request)))
1223 * accounted bit is necessary since some drivers will call
1224 * elv_next_request() many times for the same request (eg ide)
1226 if (cfq_crq_in_driver(crq))
1229 cfq_mark_crq_in_driver(crq);
1230 cfqd->rq_in_driver++;
1234 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1236 struct cfq_data *cfqd = cfqq->cfqd;
1239 if (!cfq_crq_in_driver(crq))
1244 WARN_ON(!cfqd->rq_in_driver);
1245 cfqd->rq_in_driver--;
1247 if (!cfq_class_idle(cfqq))
1248 cfqd->last_end_request = now;
1250 if (!cfq_cfqq_dispatched(cfqq)) {
1251 if (cfq_cfqq_on_rr(cfqq)) {
1252 cfqq->service_last = now;
1253 cfq_resort_rr_list(cfqq, 0);
1255 if (cfq_cfqq_expired(cfqq)) {
1256 __cfq_slice_expired(cfqd, cfqq, 0);
1257 cfq_schedule_dispatch(cfqd);
1261 if (cfq_crq_is_sync(crq))
1262 crq->io_context->last_end_request = now;
1265 static struct request *cfq_next_request(request_queue_t *q)
1267 struct cfq_data *cfqd = q->elevator->elevator_data;
1270 if (!list_empty(&q->queue_head)) {
1273 rq = list_entry_rq(q->queue_head.next);
1277 struct cfq_queue *cfqq = crq->cfq_queue;
1280 * if idle window is disabled, allow queue buildup
1282 if (!cfq_crq_in_driver(crq) &&
1283 !cfq_cfqq_idle_window(cfqq) &&
1284 !blk_barrier_rq(rq) &&
1285 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1288 cfq_remove_merge_hints(q, crq);
1289 cfq_account_dispatch(crq);
1295 if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1302 * task holds one reference to the queue, dropped when task exits. each crq
1303 * in-flight on this queue also holds a reference, dropped when crq is freed.
1305 * queue lock must be held here.
1307 static void cfq_put_queue(struct cfq_queue *cfqq)
1309 struct cfq_data *cfqd = cfqq->cfqd;
1311 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1313 if (!atomic_dec_and_test(&cfqq->ref))
1316 BUG_ON(rb_first(&cfqq->sort_list));
1317 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1318 BUG_ON(cfq_cfqq_on_rr(cfqq));
1320 if (unlikely(cfqd->active_queue == cfqq)) {
1321 __cfq_slice_expired(cfqd, cfqq, 0);
1322 cfq_schedule_dispatch(cfqd);
1325 cfq_put_cfqd(cfqq->cfqd);
1328 * it's on the empty list and still hashed
1330 list_del(&cfqq->cfq_list);
1331 hlist_del(&cfqq->cfq_hash);
1332 kmem_cache_free(cfq_pool, cfqq);
1335 static inline struct cfq_queue *
1336 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1339 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1340 struct hlist_node *entry, *next;
1342 hlist_for_each_safe(entry, next, hash_list) {
1343 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1344 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1346 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1353 static struct cfq_queue *
1354 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1356 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1359 static void cfq_free_io_context(struct cfq_io_context *cic)
1361 struct cfq_io_context *__cic;
1362 struct list_head *entry, *next;
1364 list_for_each_safe(entry, next, &cic->list) {
1365 __cic = list_entry(entry, struct cfq_io_context, list);
1366 kmem_cache_free(cfq_ioc_pool, __cic);
1369 kmem_cache_free(cfq_ioc_pool, cic);
1373 * Called with interrupts disabled
1375 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1377 struct cfq_data *cfqd = cic->cfqq->cfqd;
1378 request_queue_t *q = cfqd->queue;
1380 WARN_ON(!irqs_disabled());
1382 spin_lock(q->queue_lock);
1384 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1385 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1386 cfq_schedule_dispatch(cfqd);
1389 cfq_put_queue(cic->cfqq);
1391 spin_unlock(q->queue_lock);
1395 * Another task may update the task cic list, if it is doing a queue lookup
1396 * on its behalf. cfq_cic_lock excludes such concurrent updates
1398 static void cfq_exit_io_context(struct cfq_io_context *cic)
1400 struct cfq_io_context *__cic;
1401 struct list_head *entry;
1402 unsigned long flags;
1404 local_irq_save(flags);
1407 * put the reference this task is holding to the various queues
1409 list_for_each(entry, &cic->list) {
1410 __cic = list_entry(entry, struct cfq_io_context, list);
1411 cfq_exit_single_io_context(__cic);
1414 cfq_exit_single_io_context(cic);
1415 local_irq_restore(flags);
1418 static struct cfq_io_context *
1419 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1421 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1424 INIT_LIST_HEAD(&cic->list);
1427 cic->last_end_request = jiffies;
1428 cic->ttime_total = 0;
1429 cic->ttime_samples = 0;
1430 cic->ttime_mean = 0;
1431 cic->dtor = cfq_free_io_context;
1432 cic->exit = cfq_exit_io_context;
1438 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1440 struct task_struct *tsk = current;
1443 if (!cfq_cfqq_prio_changed(cfqq))
1446 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1447 switch (ioprio_class) {
1449 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1450 case IOPRIO_CLASS_NONE:
1452 * no prio set, place us in the middle of the BE classes
1454 cfqq->ioprio = task_nice_ioprio(tsk);
1455 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1457 case IOPRIO_CLASS_RT:
1458 cfqq->ioprio = task_ioprio(tsk);
1459 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1461 case IOPRIO_CLASS_BE:
1462 cfqq->ioprio = task_ioprio(tsk);
1463 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1465 case IOPRIO_CLASS_IDLE:
1466 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1468 cfq_clear_cfqq_idle_window(cfqq);
1473 * keep track of original prio settings in case we have to temporarily
1474 * elevate the priority of this queue
1476 cfqq->org_ioprio = cfqq->ioprio;
1477 cfqq->org_ioprio_class = cfqq->ioprio_class;
1479 if (cfq_cfqq_on_rr(cfqq))
1480 cfq_resort_rr_list(cfqq, 0);
1482 cfq_clear_cfqq_prio_changed(cfqq);
1485 static inline void changed_ioprio(struct cfq_queue *cfqq)
1488 struct cfq_data *cfqd = cfqq->cfqd;
1490 spin_lock(cfqd->queue->queue_lock);
1491 cfq_mark_cfqq_prio_changed(cfqq);
1492 cfq_init_prio_data(cfqq);
1493 spin_unlock(cfqd->queue->queue_lock);
1498 * callback from sys_ioprio_set, irqs are disabled
1500 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1502 struct cfq_io_context *cic = ioc->cic;
1504 changed_ioprio(cic->cfqq);
1506 list_for_each_entry(cic, &cic->list, list)
1507 changed_ioprio(cic->cfqq);
1512 static struct cfq_queue *
1513 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1516 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1517 struct cfq_queue *cfqq, *new_cfqq = NULL;
1520 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1526 } else if (gfp_mask & __GFP_WAIT) {
1527 spin_unlock_irq(cfqd->queue->queue_lock);
1528 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1529 spin_lock_irq(cfqd->queue->queue_lock);
1532 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1537 memset(cfqq, 0, sizeof(*cfqq));
1539 INIT_HLIST_NODE(&cfqq->cfq_hash);
1540 INIT_LIST_HEAD(&cfqq->cfq_list);
1541 RB_CLEAR_ROOT(&cfqq->sort_list);
1542 INIT_LIST_HEAD(&cfqq->fifo);
1545 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1546 atomic_set(&cfqq->ref, 0);
1548 atomic_inc(&cfqd->ref);
1549 cfqq->service_last = 0;
1551 * set ->slice_left to allow preemption for a new process
1553 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1554 cfq_mark_cfqq_idle_window(cfqq);
1555 cfq_mark_cfqq_prio_changed(cfqq);
1556 cfq_init_prio_data(cfqq);
1560 kmem_cache_free(cfq_pool, new_cfqq);
1562 atomic_inc(&cfqq->ref);
1564 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1569 * Setup general io context and cfq io context. There can be several cfq
1570 * io contexts per general io context, if this process is doing io to more
1571 * than one device managed by cfq. Note that caller is holding a reference to
1572 * cfqq, so we don't need to worry about it disappearing
1574 static struct cfq_io_context *
1575 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1577 struct io_context *ioc = NULL;
1578 struct cfq_io_context *cic;
1580 might_sleep_if(gfp_mask & __GFP_WAIT);
1582 ioc = get_io_context(gfp_mask);
1586 if ((cic = ioc->cic) == NULL) {
1587 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1593 * manually increment generic io_context usage count, it
1594 * cannot go away since we are already holding one ref to it
1597 ioc->set_ioprio = cfq_ioc_set_ioprio;
1600 atomic_inc(&cfqd->ref);
1602 struct cfq_io_context *__cic;
1605 * the first cic on the list is actually the head itself
1607 if (cic->key == cfqd)
1611 * cic exists, check if we already are there. linear search
1612 * should be ok here, the list will usually not be more than
1613 * 1 or a few entries long
1615 list_for_each_entry(__cic, &cic->list, list) {
1617 * this process is already holding a reference to
1618 * this queue, so no need to get one more
1620 if (__cic->key == cfqd) {
1627 * nope, process doesn't have a cic assoicated with this
1628 * cfqq yet. get a new one and add to list
1630 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1636 atomic_inc(&cfqd->ref);
1637 list_add(&__cic->list, &cic->list);
1644 put_io_context(ioc);
1649 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1651 unsigned long elapsed, ttime;
1654 * if this context already has stuff queued, thinktime is from
1655 * last queue not last end
1658 if (time_after(cic->last_end_request, cic->last_queue))
1659 elapsed = jiffies - cic->last_end_request;
1661 elapsed = jiffies - cic->last_queue;
1663 elapsed = jiffies - cic->last_end_request;
1666 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1668 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1669 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1670 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1673 #define sample_valid(samples) ((samples) > 80)
1676 * Disable idle window if the process thinks too long or seeks so much that
1680 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1681 struct cfq_io_context *cic)
1683 int enable_idle = cfq_cfqq_idle_window(cfqq);
1685 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1687 else if (sample_valid(cic->ttime_samples)) {
1688 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1695 cfq_mark_cfqq_idle_window(cfqq);
1697 cfq_clear_cfqq_idle_window(cfqq);
1702 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1703 * no or if we aren't sure, a 1 will cause a preempt.
1706 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1709 struct cfq_queue *cfqq = cfqd->active_queue;
1711 if (cfq_class_idle(new_cfqq))
1717 if (cfq_class_idle(cfqq))
1719 if (!cfq_cfqq_wait_request(new_cfqq))
1722 * if it doesn't have slice left, forget it
1724 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1726 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1733 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1734 * let it have half of its nominal slice.
1736 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1738 struct cfq_queue *__cfqq, *next;
1740 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1741 cfq_resort_rr_list(__cfqq, 1);
1743 if (!cfqq->slice_left)
1744 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1746 cfqq->slice_end = cfqq->slice_left + jiffies;
1747 __cfq_slice_expired(cfqd, cfqq, 1);
1748 __cfq_set_active_queue(cfqd, cfqq);
1752 * should really be a ll_rw_blk.c helper
1754 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1756 request_queue_t *q = cfqd->queue;
1758 if (!blk_queue_plugged(q))
1761 __generic_unplug_device(q);
1765 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1766 * something we should do about it
1769 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1772 const int sync = cfq_crq_is_sync(crq);
1774 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1777 struct cfq_io_context *cic = crq->io_context;
1779 cfq_update_io_thinktime(cfqd, cic);
1780 cfq_update_idle_window(cfqd, cfqq, cic);
1782 cic->last_queue = jiffies;
1785 if (cfqq == cfqd->active_queue) {
1787 * if we are waiting for a request for this queue, let it rip
1788 * immediately and flag that we must not expire this queue
1791 if (cfq_cfqq_wait_request(cfqq)) {
1792 cfq_mark_cfqq_must_dispatch(cfqq);
1793 del_timer(&cfqd->idle_slice_timer);
1794 cfq_start_queueing(cfqd, cfqq);
1796 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1798 * not the active queue - expire current slice if it is
1799 * idle and has expired it's mean thinktime or this new queue
1800 * has some old slice time left and is of higher priority
1802 cfq_preempt_queue(cfqd, cfqq);
1803 cfq_mark_cfqq_must_dispatch(cfqq);
1804 cfq_start_queueing(cfqd, cfqq);
1808 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1810 struct cfq_rq *crq = RQ_DATA(rq);
1811 struct cfq_queue *cfqq = crq->cfq_queue;
1813 cfq_init_prio_data(cfqq);
1815 cfq_add_crq_rb(crq);
1817 list_add_tail(&rq->queuelist, &cfqq->fifo);
1819 if (rq_mergeable(rq)) {
1820 cfq_add_crq_hash(cfqd, crq);
1822 if (!cfqd->queue->last_merge)
1823 cfqd->queue->last_merge = rq;
1826 cfq_crq_enqueued(cfqd, cfqq, crq);
1830 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1832 struct cfq_data *cfqd = q->elevator->elevator_data;
1835 case ELEVATOR_INSERT_BACK:
1836 while (cfq_dispatch_requests(q, INT_MAX, 1))
1838 list_add_tail(&rq->queuelist, &q->queue_head);
1840 * If we were idling with pending requests on
1841 * inactive cfqqs, force dispatching will
1842 * remove the idle timer and the queue won't
1843 * be kicked by __make_request() afterward.
1846 cfq_schedule_dispatch(cfqd);
1848 case ELEVATOR_INSERT_FRONT:
1849 list_add(&rq->queuelist, &q->queue_head);
1851 case ELEVATOR_INSERT_SORT:
1852 BUG_ON(!blk_fs_request(rq));
1853 cfq_enqueue(cfqd, rq);
1856 printk("%s: bad insert point %d\n", __FUNCTION__,where);
1861 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1863 struct cfq_rq *crq = RQ_DATA(rq);
1864 struct cfq_queue *cfqq;
1866 if (unlikely(!blk_fs_request(rq)))
1869 cfqq = crq->cfq_queue;
1871 if (cfq_crq_in_flight(crq)) {
1872 const int sync = cfq_crq_is_sync(crq);
1874 WARN_ON(!cfqq->on_dispatch[sync]);
1875 cfqq->on_dispatch[sync]--;
1878 cfq_account_completion(cfqq, crq);
1881 static struct request *
1882 cfq_former_request(request_queue_t *q, struct request *rq)
1884 struct cfq_rq *crq = RQ_DATA(rq);
1885 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1888 return rb_entry_crq(rbprev)->request;
1893 static struct request *
1894 cfq_latter_request(request_queue_t *q, struct request *rq)
1896 struct cfq_rq *crq = RQ_DATA(rq);
1897 struct rb_node *rbnext = rb_next(&crq->rb_node);
1900 return rb_entry_crq(rbnext)->request;
1906 * we temporarily boost lower priority queues if they are holding fs exclusive
1907 * resources. they are boosted to normal prio (CLASS_BE/4)
1909 static void cfq_prio_boost(struct cfq_queue *cfqq)
1911 const int ioprio_class = cfqq->ioprio_class;
1912 const int ioprio = cfqq->ioprio;
1914 if (has_fs_excl()) {
1916 * boost idle prio on transactions that would lock out other
1917 * users of the filesystem
1919 if (cfq_class_idle(cfqq))
1920 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1921 if (cfqq->ioprio > IOPRIO_NORM)
1922 cfqq->ioprio = IOPRIO_NORM;
1925 * check if we need to unboost the queue
1927 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1928 cfqq->ioprio_class = cfqq->org_ioprio_class;
1929 if (cfqq->ioprio != cfqq->org_ioprio)
1930 cfqq->ioprio = cfqq->org_ioprio;
1934 * refile between round-robin lists if we moved the priority class
1936 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1937 cfq_cfqq_on_rr(cfqq))
1938 cfq_resort_rr_list(cfqq, 0);
1941 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1943 if (rw == READ || process_sync(task))
1946 return CFQ_KEY_ASYNC;
1950 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1951 struct task_struct *task, int rw)
1954 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1955 !cfq_cfqq_must_alloc_slice(cfqq)) {
1956 cfq_mark_cfqq_must_alloc_slice(cfqq);
1957 return ELV_MQUEUE_MUST;
1960 return ELV_MQUEUE_MAY;
1962 if (!cfqq || task->flags & PF_MEMALLOC)
1963 return ELV_MQUEUE_MAY;
1964 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1965 if (cfq_cfqq_wait_request(cfqq))
1966 return ELV_MQUEUE_MUST;
1969 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1970 * can quickly flood the queue with writes from a single task
1972 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
1973 cfq_mark_cfqq_must_alloc_slice(cfqq);
1974 return ELV_MQUEUE_MUST;
1977 return ELV_MQUEUE_MAY;
1979 if (cfq_class_idle(cfqq))
1980 return ELV_MQUEUE_NO;
1981 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1982 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1983 int ret = ELV_MQUEUE_NO;
1985 if (ioc && ioc->nr_batch_requests)
1986 ret = ELV_MQUEUE_MAY;
1988 put_io_context(ioc);
1992 return ELV_MQUEUE_MAY;
1996 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1998 struct cfq_data *cfqd = q->elevator->elevator_data;
1999 struct task_struct *tsk = current;
2000 struct cfq_queue *cfqq;
2003 * don't force setup of a queue from here, as a call to may_queue
2004 * does not necessarily imply that a request actually will be queued.
2005 * so just lookup a possibly existing queue, or return 'may queue'
2008 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
2010 cfq_init_prio_data(cfqq);
2011 cfq_prio_boost(cfqq);
2013 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
2016 return ELV_MQUEUE_MAY;
2019 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
2021 struct cfq_data *cfqd = q->elevator->elevator_data;
2022 struct request_list *rl = &q->rq;
2024 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
2026 if (waitqueue_active(&rl->wait[READ]))
2027 wake_up(&rl->wait[READ]);
2030 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
2032 if (waitqueue_active(&rl->wait[WRITE]))
2033 wake_up(&rl->wait[WRITE]);
2038 * queue lock held here
2040 static void cfq_put_request(request_queue_t *q, struct request *rq)
2042 struct cfq_data *cfqd = q->elevator->elevator_data;
2043 struct cfq_rq *crq = RQ_DATA(rq);
2046 struct cfq_queue *cfqq = crq->cfq_queue;
2047 const int rw = rq_data_dir(rq);
2049 BUG_ON(!cfqq->allocated[rw]);
2050 cfqq->allocated[rw]--;
2052 put_io_context(crq->io_context->ioc);
2054 mempool_free(crq, cfqd->crq_pool);
2055 rq->elevator_private = NULL;
2057 cfq_check_waiters(q, cfqq);
2058 cfq_put_queue(cfqq);
2063 * Allocate cfq data structures associated with this request.
2066 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
2069 struct cfq_data *cfqd = q->elevator->elevator_data;
2070 struct task_struct *tsk = current;
2071 struct cfq_io_context *cic;
2072 const int rw = rq_data_dir(rq);
2073 pid_t key = cfq_queue_pid(tsk, rw);
2074 struct cfq_queue *cfqq;
2076 unsigned long flags;
2078 might_sleep_if(gfp_mask & __GFP_WAIT);
2080 cic = cfq_get_io_context(cfqd, key, gfp_mask);
2082 spin_lock_irqsave(q->queue_lock, flags);
2088 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
2096 cfqq->allocated[rw]++;
2097 cfq_clear_cfqq_must_alloc(cfqq);
2098 cfqd->rq_starved = 0;
2099 atomic_inc(&cfqq->ref);
2100 spin_unlock_irqrestore(q->queue_lock, flags);
2102 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2104 RB_CLEAR(&crq->rb_node);
2107 INIT_HLIST_NODE(&crq->hash);
2108 crq->cfq_queue = cfqq;
2109 crq->io_context = cic;
2110 cfq_clear_crq_in_flight(crq);
2111 cfq_clear_crq_in_driver(crq);
2112 cfq_clear_crq_requeued(crq);
2114 if (rw == READ || process_sync(tsk))
2115 cfq_mark_crq_is_sync(crq);
2117 cfq_clear_crq_is_sync(crq);
2119 rq->elevator_private = crq;
2123 spin_lock_irqsave(q->queue_lock, flags);
2124 cfqq->allocated[rw]--;
2125 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
2126 cfq_mark_cfqq_must_alloc(cfqq);
2127 cfq_put_queue(cfqq);
2130 put_io_context(cic->ioc);
2132 * mark us rq allocation starved. we need to kickstart the process
2133 * ourselves if there are no pending requests that can do it for us.
2134 * that would be an extremely rare OOM situation
2136 cfqd->rq_starved = 1;
2137 cfq_schedule_dispatch(cfqd);
2138 spin_unlock_irqrestore(q->queue_lock, flags);
2142 static void cfq_kick_queue(void *data)
2144 request_queue_t *q = data;
2145 struct cfq_data *cfqd = q->elevator->elevator_data;
2146 unsigned long flags;
2148 spin_lock_irqsave(q->queue_lock, flags);
2150 if (cfqd->rq_starved) {
2151 struct request_list *rl = &q->rq;
2154 * we aren't guaranteed to get a request after this, but we
2155 * have to be opportunistic
2158 if (waitqueue_active(&rl->wait[READ]))
2159 wake_up(&rl->wait[READ]);
2160 if (waitqueue_active(&rl->wait[WRITE]))
2161 wake_up(&rl->wait[WRITE]);
2166 spin_unlock_irqrestore(q->queue_lock, flags);
2170 * Timer running if the active_queue is currently idling inside its time slice
2172 static void cfq_idle_slice_timer(unsigned long data)
2174 struct cfq_data *cfqd = (struct cfq_data *) data;
2175 struct cfq_queue *cfqq;
2176 unsigned long flags;
2178 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2180 if ((cfqq = cfqd->active_queue) != NULL) {
2181 unsigned long now = jiffies;
2186 if (time_after(now, cfqq->slice_end))
2190 * only expire and reinvoke request handler, if there are
2191 * other queues with pending requests
2193 if (!cfq_pending_requests(cfqd)) {
2194 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2195 add_timer(&cfqd->idle_slice_timer);
2200 * not expired and it has a request pending, let it dispatch
2202 if (!RB_EMPTY(&cfqq->sort_list)) {
2203 cfq_mark_cfqq_must_dispatch(cfqq);
2208 cfq_slice_expired(cfqd, 0);
2210 cfq_schedule_dispatch(cfqd);
2212 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2216 * Timer running if an idle class queue is waiting for service
2218 static void cfq_idle_class_timer(unsigned long data)
2220 struct cfq_data *cfqd = (struct cfq_data *) data;
2221 unsigned long flags, end;
2223 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2226 * race with a non-idle queue, reset timer
2228 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2229 if (!time_after_eq(jiffies, end)) {
2230 cfqd->idle_class_timer.expires = end;
2231 add_timer(&cfqd->idle_class_timer);
2233 cfq_schedule_dispatch(cfqd);
2235 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2238 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2240 del_timer_sync(&cfqd->idle_slice_timer);
2241 del_timer_sync(&cfqd->idle_class_timer);
2242 blk_sync_queue(cfqd->queue);
2245 static void cfq_put_cfqd(struct cfq_data *cfqd)
2247 request_queue_t *q = cfqd->queue;
2249 if (!atomic_dec_and_test(&cfqd->ref))
2254 cfq_shutdown_timer_wq(cfqd);
2255 q->elevator->elevator_data = NULL;
2257 mempool_destroy(cfqd->crq_pool);
2258 kfree(cfqd->crq_hash);
2259 kfree(cfqd->cfq_hash);
2263 static void cfq_exit_queue(elevator_t *e)
2265 struct cfq_data *cfqd = e->elevator_data;
2267 cfq_shutdown_timer_wq(cfqd);
2271 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2273 struct cfq_data *cfqd;
2276 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2280 memset(cfqd, 0, sizeof(*cfqd));
2282 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2283 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2285 INIT_LIST_HEAD(&cfqd->busy_rr);
2286 INIT_LIST_HEAD(&cfqd->cur_rr);
2287 INIT_LIST_HEAD(&cfqd->idle_rr);
2288 INIT_LIST_HEAD(&cfqd->empty_list);
2290 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2291 if (!cfqd->crq_hash)
2294 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2295 if (!cfqd->cfq_hash)
2298 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2299 if (!cfqd->crq_pool)
2302 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2303 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2304 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2305 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2307 e->elevator_data = cfqd;
2310 atomic_inc(&q->refcnt);
2312 cfqd->max_queued = q->nr_requests / 4;
2313 q->nr_batching = cfq_queued;
2315 init_timer(&cfqd->idle_slice_timer);
2316 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2317 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2319 init_timer(&cfqd->idle_class_timer);
2320 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2321 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2323 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2325 atomic_set(&cfqd->ref, 1);
2327 cfqd->cfq_queued = cfq_queued;
2328 cfqd->cfq_quantum = cfq_quantum;
2329 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2330 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2331 cfqd->cfq_back_max = cfq_back_max;
2332 cfqd->cfq_back_penalty = cfq_back_penalty;
2333 cfqd->cfq_slice[0] = cfq_slice_async;
2334 cfqd->cfq_slice[1] = cfq_slice_sync;
2335 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2336 cfqd->cfq_slice_idle = cfq_slice_idle;
2337 cfqd->cfq_max_depth = cfq_max_depth;
2341 kfree(cfqd->cfq_hash);
2343 kfree(cfqd->crq_hash);
2349 static void cfq_slab_kill(void)
2352 kmem_cache_destroy(crq_pool);
2354 kmem_cache_destroy(cfq_pool);
2356 kmem_cache_destroy(cfq_ioc_pool);
2359 static int __init cfq_slab_setup(void)
2361 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2366 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2371 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2372 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2383 * sysfs parts below -->
2385 struct cfq_fs_entry {
2386 struct attribute attr;
2387 ssize_t (*show)(struct cfq_data *, char *);
2388 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2392 cfq_var_show(unsigned int var, char *page)
2394 return sprintf(page, "%d\n", var);
2398 cfq_var_store(unsigned int *var, const char *page, size_t count)
2400 char *p = (char *) page;
2402 *var = simple_strtoul(p, &p, 10);
2406 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2407 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2409 unsigned int __data = __VAR; \
2411 __data = jiffies_to_msecs(__data); \
2412 return cfq_var_show(__data, (page)); \
2414 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2415 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2416 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2417 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2418 SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2419 SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
2420 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2421 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2422 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2423 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2424 SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
2425 #undef SHOW_FUNCTION
2427 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2428 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2430 unsigned int __data; \
2431 int ret = cfq_var_store(&__data, (page), count); \
2432 if (__data < (MIN)) \
2434 else if (__data > (MAX)) \
2437 *(__PTR) = msecs_to_jiffies(__data); \
2439 *(__PTR) = __data; \
2442 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2443 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2444 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2445 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2446 STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2447 STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2448 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2449 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2450 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2451 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2452 STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
2453 #undef STORE_FUNCTION
2455 static struct cfq_fs_entry cfq_quantum_entry = {
2456 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2457 .show = cfq_quantum_show,
2458 .store = cfq_quantum_store,
2460 static struct cfq_fs_entry cfq_queued_entry = {
2461 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2462 .show = cfq_queued_show,
2463 .store = cfq_queued_store,
2465 static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
2466 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
2467 .show = cfq_fifo_expire_sync_show,
2468 .store = cfq_fifo_expire_sync_store,
2470 static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
2471 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
2472 .show = cfq_fifo_expire_async_show,
2473 .store = cfq_fifo_expire_async_store,
2475 static struct cfq_fs_entry cfq_back_max_entry = {
2476 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2477 .show = cfq_back_max_show,
2478 .store = cfq_back_max_store,
2480 static struct cfq_fs_entry cfq_back_penalty_entry = {
2481 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2482 .show = cfq_back_penalty_show,
2483 .store = cfq_back_penalty_store,
2485 static struct cfq_fs_entry cfq_slice_sync_entry = {
2486 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2487 .show = cfq_slice_sync_show,
2488 .store = cfq_slice_sync_store,
2490 static struct cfq_fs_entry cfq_slice_async_entry = {
2491 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2492 .show = cfq_slice_async_show,
2493 .store = cfq_slice_async_store,
2495 static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2496 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2497 .show = cfq_slice_async_rq_show,
2498 .store = cfq_slice_async_rq_store,
2500 static struct cfq_fs_entry cfq_slice_idle_entry = {
2501 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2502 .show = cfq_slice_idle_show,
2503 .store = cfq_slice_idle_store,
2505 static struct cfq_fs_entry cfq_max_depth_entry = {
2506 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2507 .show = cfq_max_depth_show,
2508 .store = cfq_max_depth_store,
2511 static struct attribute *default_attrs[] = {
2512 &cfq_quantum_entry.attr,
2513 &cfq_queued_entry.attr,
2514 &cfq_fifo_expire_sync_entry.attr,
2515 &cfq_fifo_expire_async_entry.attr,
2516 &cfq_back_max_entry.attr,
2517 &cfq_back_penalty_entry.attr,
2518 &cfq_slice_sync_entry.attr,
2519 &cfq_slice_async_entry.attr,
2520 &cfq_slice_async_rq_entry.attr,
2521 &cfq_slice_idle_entry.attr,
2522 &cfq_max_depth_entry.attr,
2526 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2529 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2531 elevator_t *e = container_of(kobj, elevator_t, kobj);
2532 struct cfq_fs_entry *entry = to_cfq(attr);
2537 return entry->show(e->elevator_data, page);
2541 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2542 const char *page, size_t length)
2544 elevator_t *e = container_of(kobj, elevator_t, kobj);
2545 struct cfq_fs_entry *entry = to_cfq(attr);
2550 return entry->store(e->elevator_data, page, length);
2553 static struct sysfs_ops cfq_sysfs_ops = {
2554 .show = cfq_attr_show,
2555 .store = cfq_attr_store,
2558 static struct kobj_type cfq_ktype = {
2559 .sysfs_ops = &cfq_sysfs_ops,
2560 .default_attrs = default_attrs,
2563 static struct elevator_type iosched_cfq = {
2565 .elevator_merge_fn = cfq_merge,
2566 .elevator_merged_fn = cfq_merged_request,
2567 .elevator_merge_req_fn = cfq_merged_requests,
2568 .elevator_next_req_fn = cfq_next_request,
2569 .elevator_add_req_fn = cfq_insert_request,
2570 .elevator_remove_req_fn = cfq_remove_request,
2571 .elevator_requeue_req_fn = cfq_requeue_request,
2572 .elevator_deactivate_req_fn = cfq_deactivate_request,
2573 .elevator_queue_empty_fn = cfq_queue_empty,
2574 .elevator_completed_req_fn = cfq_completed_request,
2575 .elevator_former_req_fn = cfq_former_request,
2576 .elevator_latter_req_fn = cfq_latter_request,
2577 .elevator_set_req_fn = cfq_set_request,
2578 .elevator_put_req_fn = cfq_put_request,
2579 .elevator_may_queue_fn = cfq_may_queue,
2580 .elevator_init_fn = cfq_init_queue,
2581 .elevator_exit_fn = cfq_exit_queue,
2583 .elevator_ktype = &cfq_ktype,
2584 .elevator_name = "cfq",
2585 .elevator_owner = THIS_MODULE,
2588 static int __init cfq_init(void)
2593 * could be 0 on HZ < 1000 setups
2595 if (!cfq_slice_async)
2596 cfq_slice_async = 1;
2597 if (!cfq_slice_idle)
2600 if (cfq_slab_setup())
2603 ret = elv_register(&iosched_cfq);
2610 static void __exit cfq_exit(void)
2612 struct task_struct *g, *p;
2613 unsigned long flags;
2615 read_lock_irqsave(&tasklist_lock, flags);
2618 * iterate each process in the system, removing our io_context
2620 do_each_thread(g, p) {
2621 struct io_context *ioc = p->io_context;
2623 if (ioc && ioc->cic) {
2624 ioc->cic->exit(ioc->cic);
2625 cfq_free_io_context(ioc->cic);
2628 } while_each_thread(g, p);
2630 read_unlock_irqrestore(&tasklist_lock, flags);
2633 elv_unregister(&iosched_cfq);
2636 module_init(cfq_init);
2637 module_exit(cfq_exit);
2639 MODULE_AUTHOR("Jens Axboe");
2640 MODULE_LICENSE("GPL");
2641 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");