[PATCH] cfq build fix
[linux-2.6/zen-sources.git] / drivers / block / cfq-iosched.c
blobde5746e38af935a01c45a866ae7598d24108a6ac
1 /*
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>
12 #include <linux/fs.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>
28 * tunables
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
75 * rb-tree defines
77 #define RB_NONE (2)
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; \
85 } while (0)
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)
100 #define ASYNC (0)
101 #define SYNC (1)
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
114 struct cfq_data {
115 atomic_t ref;
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;
133 * cfqq lookup hash
135 struct hlist_head *cfq_hash;
138 * global crq hash for all queues
140 struct hlist_head *crq_hash;
142 unsigned int max_queued;
144 mempool_t *crq_pool;
146 int rq_in_driver;
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
186 struct cfq_queue {
187 /* reference count */
188 atomic_t ref;
189 /* parent cfq_data */
190 struct cfq_data *cfqd;
191 /* cfqq lookup hash */
192 struct hlist_node cfq_hash;
193 /* hash key */
194 unsigned int key;
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 */
202 int queued[2];
203 /* currently allocated requests */
204 int allocated[2];
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 */
214 int on_dispatch[2];
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 */
221 unsigned int flags;
224 struct cfq_rq {
225 struct rb_node rb_node;
226 sector_t rb_key;
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; \
262 CFQ_CFQQ_FNS(on_rr);
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);
271 #undef CFQ_CFQQ_FNS
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);
298 #undef CFQ_CRQ_FNS
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);
340 continue;
343 if (rq_hash_key(__rq) == offset)
344 return __rq;
347 return NULL;
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)
385 return crq2;
386 if (crq2 == NULL)
387 return crq1;
388 if (cfq_crq_requeued(crq1))
389 return crq1;
390 if (cfq_crq_requeued(crq2))
391 return 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.
408 if (s1 >= last)
409 d1 = s1 - last;
410 else if (s1 + back_max >= last)
411 d1 = (last - s1) * cfqd->cfq_back_penalty;
412 else
413 r1_wrap = 1;
415 if (s2 >= last)
416 d2 = s2 - last;
417 else if (s2 + back_max >= last)
418 d2 = (last - s2) * cfqd->cfq_back_penalty;
419 else
420 r2_wrap = 1;
422 /* Found required data */
423 if (!r1_wrap && r2_wrap)
424 return crq1;
425 else if (!r2_wrap && r1_wrap)
426 return crq2;
427 else if (r1_wrap && r2_wrap) {
428 /* both behind the head */
429 if (s1 <= s2)
430 return crq1;
431 else
432 return crq2;
435 /* Both requests in front of the head */
436 if (d1 < d2)
437 return crq1;
438 else if (d2 < d1)
439 return crq2;
440 else {
441 if (s1 >= s2)
442 return crq1;
443 else
444 return crq2;
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,
453 struct cfq_rq *last)
455 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
456 struct rb_node *rbnext, *rbprev;
458 rbnext = NULL;
459 if (ON_RB(&last->rb_node))
460 rbnext = rb_next(&last->rb_node);
461 if (!rbnext) {
462 rbnext = rb_first(&cfqq->sort_list);
463 if (rbnext == &last->rb_node)
464 rbnext = NULL;
467 rbprev = rb_prev(&last->rb_node);
469 if (rbprev)
470 crq_prev = rb_entry_crq(rbprev);
471 if (rbnext)
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;
498 else {
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;
508 else
509 list = &cfqd->rr_list[cfqq->ioprio];
513 * if queue was preempted, just add to front to be fair. busy_rr
514 * isn't sorted.
516 if (preempted || list == &cfqd->busy_rr) {
517 list_add(&cfqq->cfq_list, list);
518 return;
522 * sort by when queue was last serviced
524 entry = list;
525 while ((entry = entry->prev) != list) {
526 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
528 if (!__cfqq->service_last)
529 break;
530 if (time_before(__cfqq->service_last, cfqq->service_last))
531 break;
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
541 static inline void
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);
546 cfqd->busy_queues++;
548 cfq_resort_rr_list(cfqq, requeue);
551 static inline void
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);
559 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;
593 while (*p) {
594 parent = *p;
595 __crq = rb_entry_crq(parent);
597 if (crq->rb_key < __crq->rb_key)
598 p = &(*p)->rb_left;
599 else if (crq->rb_key > __crq->rb_key)
600 p = &(*p)->rb_right;
601 else
602 return __crq;
605 rb_link_node(&crq->rb_node, parent, p);
606 return NULL;
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);
637 static inline void
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)]--;
645 cfq_add_crq_rb(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);
652 struct rb_node *n;
654 if (!cfqq)
655 goto out;
657 n = cfqq->sort_list.rb_node;
658 while (n) {
659 struct cfq_rq *crq = rb_entry_crq(n);
661 if (sector < crq->rb_key)
662 n = n->rb_left;
663 else if (sector > crq->rb_key)
664 n = n->rb_right;
665 else
666 return crq->request;
669 out:
670 return NULL;
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);
678 if (crq) {
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);
710 if (crq) {
711 list_del_init(&rq->queuelist);
712 cfq_del_crq_rb(crq);
713 cfq_remove_merge_hints(q, crq);
718 static int
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;
723 int ret;
725 ret = elv_try_last_merge(q, bio);
726 if (ret != ELEVATOR_NO_MERGE) {
727 __rq = q->last_merge;
728 goto out_insert;
731 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
732 if (__rq && elv_rq_merge_ok(__rq, bio)) {
733 ret = ELEVATOR_BACK_MERGE;
734 goto out;
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;
740 goto out;
743 return ELEVATOR_NO_MERGE;
744 out:
745 q->last_merge = __rq;
746 out_insert:
747 *req = __rq;
748 return ret;
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);
766 q->last_merge = req;
769 static void
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);
785 static inline void
786 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
788 if (cfqq) {
790 * stop potential idle class queues waiting service
792 del_timer(&cfqd->idle_class_timer);
794 cfqq->slice_start = jiffies;
795 cfqq->slice_end = 0;
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;
807 * 0,1
808 * 0,1,2
809 * 0,1,2,3
810 * 0,1,2,3,4
811 * 0,1,2,3,4,5
812 * 0,1,2,3,4,5,6
813 * 0,1,2,3,4,5,6,7
815 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
817 int prio, wrap;
819 prio = -1;
820 wrap = 0;
821 do {
822 int p;
824 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
825 if (!list_empty(&cfqd->rr_list[p])) {
826 prio = p;
827 break;
831 if (prio != -1)
832 break;
833 cfqd->cur_prio = 0;
834 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
835 cfqd->cur_end_prio = 0;
836 if (wrap)
837 break;
838 wrap = 1;
840 } while (1);
842 if (unlikely(prio == -1))
843 return -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;
852 cfqd->cur_prio = 0;
854 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
855 cfqd->cur_prio = 0;
856 cfqd->cur_end_prio = 0;
859 return prio;
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))
872 return NULL;
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);
892 else
893 mod_timer(&cfqd->idle_class_timer, end);
896 __cfq_set_active_queue(cfqd, cfqq);
897 return cfqq;
901 * current cfqq expired its slice (or was too idle), select new one
903 static void
904 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
905 int preempted)
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
920 * or was preempted
922 if (time_after(now, cfqq->slice_end))
923 cfqq->slice_left = now - cfqq->slice_end;
924 else
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;
945 if (cfqq) {
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);
952 else
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)
967 return 0;
968 if (!cfq_cfqq_idle_window(cfqq))
969 return 0;
971 * task has exited, don't wait
973 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
974 return 0;
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);
986 return 1;
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;
1000 sector_t last;
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))
1009 break;
1010 if (!blk_fs_request(__rq))
1011 break;
1012 if (cfq_crq_requeued(__crq))
1013 break;
1015 if (__rq->sector <= crq->request->sector)
1016 break;
1017 if (__rq->sector > last && crq->request->sector < last) {
1018 last = crq->request->sector + crq->request->nr_sectors;
1019 break;
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;
1044 struct request *rq;
1045 struct cfq_rq *crq;
1047 if (cfq_cfqq_fifo_expire(cfqq))
1048 return NULL;
1050 if (!list_empty(&cfqq->fifo)) {
1051 int fifo = cfq_cfqq_class_sync(cfqq);
1053 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
1054 rq = crq->request;
1055 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
1056 cfq_mark_cfqq_fifo_expire(cfqq);
1057 return crq;
1061 return NULL;
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.
1069 static inline int
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));
1079 static inline void
1080 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1082 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1085 static inline int
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;
1104 if (!cfqq)
1105 goto new_queue;
1107 if (cfq_cfqq_expired(cfqq))
1108 goto new_queue;
1111 * slice has expired
1113 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1114 goto expire;
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))
1121 goto keep_queue;
1122 else if (!force && cfq_cfqq_class_sync(cfqq) &&
1123 time_before(now, cfqq->slice_end)) {
1124 if (cfq_arm_slice_timer(cfqd, cfqq))
1125 return NULL;
1128 expire:
1129 cfq_slice_expired(cfqd, 0);
1130 new_queue:
1131 cfqq = cfq_set_active_queue(cfqd);
1132 keep_queue:
1133 return cfqq;
1136 static int
1137 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1138 int max_dispatch)
1140 int dispatched = 0;
1142 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1144 do {
1145 struct cfq_rq *crq;
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++;
1159 dispatched++;
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))
1167 break;
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);
1187 return dispatched;
1190 static int
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)
1197 return 0;
1199 cfqq = cfq_select_queue(cfqd, force);
1200 if (cfqq) {
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))
1206 max_dispatch = 1;
1208 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1211 return 0;
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)))
1220 return;
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))
1227 return;
1229 cfq_mark_crq_in_driver(crq);
1230 cfqd->rq_in_driver++;
1233 static inline void
1234 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1236 struct cfq_data *cfqd = cfqq->cfqd;
1237 unsigned long now;
1239 if (!cfq_crq_in_driver(crq))
1240 return;
1242 now = jiffies;
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;
1268 struct request *rq;
1270 if (!list_empty(&q->queue_head)) {
1271 struct cfq_rq *crq;
1272 dispatch:
1273 rq = list_entry_rq(q->queue_head.next);
1275 crq = RQ_DATA(rq);
1276 if (crq) {
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 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1285 return NULL;
1287 cfq_remove_merge_hints(q, crq);
1288 cfq_account_dispatch(crq);
1291 return rq;
1294 if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1295 goto dispatch;
1297 return NULL;
1301 * task holds one reference to the queue, dropped when task exits. each crq
1302 * in-flight on this queue also holds a reference, dropped when crq is freed.
1304 * queue lock must be held here.
1306 static void cfq_put_queue(struct cfq_queue *cfqq)
1308 struct cfq_data *cfqd = cfqq->cfqd;
1310 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1312 if (!atomic_dec_and_test(&cfqq->ref))
1313 return;
1315 BUG_ON(rb_first(&cfqq->sort_list));
1316 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1317 BUG_ON(cfq_cfqq_on_rr(cfqq));
1319 if (unlikely(cfqd->active_queue == cfqq)) {
1320 __cfq_slice_expired(cfqd, cfqq, 0);
1321 cfq_schedule_dispatch(cfqd);
1324 cfq_put_cfqd(cfqq->cfqd);
1327 * it's on the empty list and still hashed
1329 list_del(&cfqq->cfq_list);
1330 hlist_del(&cfqq->cfq_hash);
1331 kmem_cache_free(cfq_pool, cfqq);
1334 static inline struct cfq_queue *
1335 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1336 const int hashval)
1338 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1339 struct hlist_node *entry, *next;
1341 hlist_for_each_safe(entry, next, hash_list) {
1342 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1343 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1345 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1346 return __cfqq;
1349 return NULL;
1352 static struct cfq_queue *
1353 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1355 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1358 static void cfq_free_io_context(struct cfq_io_context *cic)
1360 struct cfq_io_context *__cic;
1361 struct list_head *entry, *next;
1363 list_for_each_safe(entry, next, &cic->list) {
1364 __cic = list_entry(entry, struct cfq_io_context, list);
1365 kmem_cache_free(cfq_ioc_pool, __cic);
1368 kmem_cache_free(cfq_ioc_pool, cic);
1372 * Called with interrupts disabled
1374 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1376 struct cfq_data *cfqd = cic->cfqq->cfqd;
1377 request_queue_t *q = cfqd->queue;
1379 WARN_ON(!irqs_disabled());
1381 spin_lock(q->queue_lock);
1383 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1384 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1385 cfq_schedule_dispatch(cfqd);
1388 cfq_put_queue(cic->cfqq);
1389 cic->cfqq = NULL;
1390 spin_unlock(q->queue_lock);
1394 * Another task may update the task cic list, if it is doing a queue lookup
1395 * on its behalf. cfq_cic_lock excludes such concurrent updates
1397 static void cfq_exit_io_context(struct cfq_io_context *cic)
1399 struct cfq_io_context *__cic;
1400 struct list_head *entry;
1401 unsigned long flags;
1403 local_irq_save(flags);
1406 * put the reference this task is holding to the various queues
1408 list_for_each(entry, &cic->list) {
1409 __cic = list_entry(entry, struct cfq_io_context, list);
1410 cfq_exit_single_io_context(__cic);
1413 cfq_exit_single_io_context(cic);
1414 local_irq_restore(flags);
1417 static struct cfq_io_context *
1418 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1420 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1422 if (cic) {
1423 INIT_LIST_HEAD(&cic->list);
1424 cic->cfqq = NULL;
1425 cic->key = NULL;
1426 cic->last_end_request = jiffies;
1427 cic->ttime_total = 0;
1428 cic->ttime_samples = 0;
1429 cic->ttime_mean = 0;
1430 cic->dtor = cfq_free_io_context;
1431 cic->exit = cfq_exit_io_context;
1434 return cic;
1437 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1439 struct task_struct *tsk = current;
1440 int ioprio_class;
1442 if (!cfq_cfqq_prio_changed(cfqq))
1443 return;
1445 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1446 switch (ioprio_class) {
1447 default:
1448 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1449 case IOPRIO_CLASS_NONE:
1451 * no prio set, place us in the middle of the BE classes
1453 cfqq->ioprio = task_nice_ioprio(tsk);
1454 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1455 break;
1456 case IOPRIO_CLASS_RT:
1457 cfqq->ioprio = task_ioprio(tsk);
1458 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1459 break;
1460 case IOPRIO_CLASS_BE:
1461 cfqq->ioprio = task_ioprio(tsk);
1462 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1463 break;
1464 case IOPRIO_CLASS_IDLE:
1465 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1466 cfqq->ioprio = 7;
1467 cfq_clear_cfqq_idle_window(cfqq);
1468 break;
1472 * keep track of original prio settings in case we have to temporarily
1473 * elevate the priority of this queue
1475 cfqq->org_ioprio = cfqq->ioprio;
1476 cfqq->org_ioprio_class = cfqq->ioprio_class;
1478 if (cfq_cfqq_on_rr(cfqq))
1479 cfq_resort_rr_list(cfqq, 0);
1481 cfq_clear_cfqq_prio_changed(cfqq);
1484 static inline void changed_ioprio(struct cfq_queue *cfqq)
1486 if (cfqq) {
1487 struct cfq_data *cfqd = cfqq->cfqd;
1489 spin_lock(cfqd->queue->queue_lock);
1490 cfq_mark_cfqq_prio_changed(cfqq);
1491 cfq_init_prio_data(cfqq);
1492 spin_unlock(cfqd->queue->queue_lock);
1497 * callback from sys_ioprio_set, irqs are disabled
1499 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1501 struct cfq_io_context *cic = ioc->cic;
1503 changed_ioprio(cic->cfqq);
1505 list_for_each_entry(cic, &cic->list, list)
1506 changed_ioprio(cic->cfqq);
1508 return 0;
1511 static struct cfq_queue *
1512 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1513 int gfp_mask)
1515 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1516 struct cfq_queue *cfqq, *new_cfqq = NULL;
1518 retry:
1519 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1521 if (!cfqq) {
1522 if (new_cfqq) {
1523 cfqq = new_cfqq;
1524 new_cfqq = NULL;
1525 } else if (gfp_mask & __GFP_WAIT) {
1526 spin_unlock_irq(cfqd->queue->queue_lock);
1527 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1528 spin_lock_irq(cfqd->queue->queue_lock);
1529 goto retry;
1530 } else {
1531 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1532 if (!cfqq)
1533 goto out;
1536 memset(cfqq, 0, sizeof(*cfqq));
1538 INIT_HLIST_NODE(&cfqq->cfq_hash);
1539 INIT_LIST_HEAD(&cfqq->cfq_list);
1540 RB_CLEAR_ROOT(&cfqq->sort_list);
1541 INIT_LIST_HEAD(&cfqq->fifo);
1543 cfqq->key = key;
1544 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1545 atomic_set(&cfqq->ref, 0);
1546 cfqq->cfqd = cfqd;
1547 atomic_inc(&cfqd->ref);
1548 cfqq->service_last = 0;
1550 * set ->slice_left to allow preemption for a new process
1552 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1553 cfq_mark_cfqq_idle_window(cfqq);
1554 cfq_mark_cfqq_prio_changed(cfqq);
1555 cfq_init_prio_data(cfqq);
1558 if (new_cfqq)
1559 kmem_cache_free(cfq_pool, new_cfqq);
1561 atomic_inc(&cfqq->ref);
1562 out:
1563 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1564 return cfqq;
1568 * Setup general io context and cfq io context. There can be several cfq
1569 * io contexts per general io context, if this process is doing io to more
1570 * than one device managed by cfq. Note that caller is holding a reference to
1571 * cfqq, so we don't need to worry about it disappearing
1573 static struct cfq_io_context *
1574 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1576 struct io_context *ioc = NULL;
1577 struct cfq_io_context *cic;
1579 might_sleep_if(gfp_mask & __GFP_WAIT);
1581 ioc = get_io_context(gfp_mask);
1582 if (!ioc)
1583 return NULL;
1585 if ((cic = ioc->cic) == NULL) {
1586 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1588 if (cic == NULL)
1589 goto err;
1592 * manually increment generic io_context usage count, it
1593 * cannot go away since we are already holding one ref to it
1595 ioc->cic = cic;
1596 ioc->set_ioprio = cfq_ioc_set_ioprio;
1597 cic->ioc = ioc;
1598 cic->key = cfqd;
1599 atomic_inc(&cfqd->ref);
1600 } else {
1601 struct cfq_io_context *__cic;
1604 * the first cic on the list is actually the head itself
1606 if (cic->key == cfqd)
1607 goto out;
1610 * cic exists, check if we already are there. linear search
1611 * should be ok here, the list will usually not be more than
1612 * 1 or a few entries long
1614 list_for_each_entry(__cic, &cic->list, list) {
1616 * this process is already holding a reference to
1617 * this queue, so no need to get one more
1619 if (__cic->key == cfqd) {
1620 cic = __cic;
1621 goto out;
1626 * nope, process doesn't have a cic assoicated with this
1627 * cfqq yet. get a new one and add to list
1629 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1630 if (__cic == NULL)
1631 goto err;
1633 __cic->ioc = ioc;
1634 __cic->key = cfqd;
1635 atomic_inc(&cfqd->ref);
1636 list_add(&__cic->list, &cic->list);
1637 cic = __cic;
1640 out:
1641 return cic;
1642 err:
1643 put_io_context(ioc);
1644 return NULL;
1647 static void
1648 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1650 unsigned long elapsed, ttime;
1653 * if this context already has stuff queued, thinktime is from
1654 * last queue not last end
1656 #if 0
1657 if (time_after(cic->last_end_request, cic->last_queue))
1658 elapsed = jiffies - cic->last_end_request;
1659 else
1660 elapsed = jiffies - cic->last_queue;
1661 #else
1662 elapsed = jiffies - cic->last_end_request;
1663 #endif
1665 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1667 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1668 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1669 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1672 #define sample_valid(samples) ((samples) > 80)
1675 * Disable idle window if the process thinks too long or seeks so much that
1676 * it doesn't matter
1678 static void
1679 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1680 struct cfq_io_context *cic)
1682 int enable_idle = cfq_cfqq_idle_window(cfqq);
1684 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1685 enable_idle = 0;
1686 else if (sample_valid(cic->ttime_samples)) {
1687 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1688 enable_idle = 0;
1689 else
1690 enable_idle = 1;
1693 if (enable_idle)
1694 cfq_mark_cfqq_idle_window(cfqq);
1695 else
1696 cfq_clear_cfqq_idle_window(cfqq);
1701 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1702 * no or if we aren't sure, a 1 will cause a preempt.
1704 static int
1705 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1706 struct cfq_rq *crq)
1708 struct cfq_queue *cfqq = cfqd->active_queue;
1710 if (cfq_class_idle(new_cfqq))
1711 return 0;
1713 if (!cfqq)
1714 return 1;
1716 if (cfq_class_idle(cfqq))
1717 return 1;
1718 if (!cfq_cfqq_wait_request(new_cfqq))
1719 return 0;
1721 * if it doesn't have slice left, forget it
1723 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1724 return 0;
1725 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1726 return 1;
1728 return 0;
1732 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1733 * let it have half of its nominal slice.
1735 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1737 struct cfq_queue *__cfqq, *next;
1739 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1740 cfq_resort_rr_list(__cfqq, 1);
1742 if (!cfqq->slice_left)
1743 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1745 cfqq->slice_end = cfqq->slice_left + jiffies;
1746 __cfq_slice_expired(cfqd, cfqq, 1);
1747 __cfq_set_active_queue(cfqd, cfqq);
1751 * should really be a ll_rw_blk.c helper
1753 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1755 request_queue_t *q = cfqd->queue;
1757 if (!blk_queue_plugged(q))
1758 q->request_fn(q);
1759 else
1760 __generic_unplug_device(q);
1764 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1765 * something we should do about it
1767 static void
1768 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1769 struct cfq_rq *crq)
1771 const int sync = cfq_crq_is_sync(crq);
1773 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1775 if (sync) {
1776 struct cfq_io_context *cic = crq->io_context;
1778 cfq_update_io_thinktime(cfqd, cic);
1779 cfq_update_idle_window(cfqd, cfqq, cic);
1781 cic->last_queue = jiffies;
1784 if (cfqq == cfqd->active_queue) {
1786 * if we are waiting for a request for this queue, let it rip
1787 * immediately and flag that we must not expire this queue
1788 * just now
1790 if (cfq_cfqq_wait_request(cfqq)) {
1791 cfq_mark_cfqq_must_dispatch(cfqq);
1792 del_timer(&cfqd->idle_slice_timer);
1793 cfq_start_queueing(cfqd, cfqq);
1795 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1797 * not the active queue - expire current slice if it is
1798 * idle and has expired it's mean thinktime or this new queue
1799 * has some old slice time left and is of higher priority
1801 cfq_preempt_queue(cfqd, cfqq);
1802 cfq_mark_cfqq_must_dispatch(cfqq);
1803 cfq_start_queueing(cfqd, cfqq);
1807 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1809 struct cfq_rq *crq = RQ_DATA(rq);
1810 struct cfq_queue *cfqq = crq->cfq_queue;
1812 cfq_init_prio_data(cfqq);
1814 cfq_add_crq_rb(crq);
1816 list_add_tail(&rq->queuelist, &cfqq->fifo);
1818 if (rq_mergeable(rq)) {
1819 cfq_add_crq_hash(cfqd, crq);
1821 if (!cfqd->queue->last_merge)
1822 cfqd->queue->last_merge = rq;
1825 cfq_crq_enqueued(cfqd, cfqq, crq);
1828 static void
1829 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1831 struct cfq_data *cfqd = q->elevator->elevator_data;
1833 switch (where) {
1834 case ELEVATOR_INSERT_BACK:
1835 while (cfq_dispatch_requests(q, INT_MAX, 1))
1837 list_add_tail(&rq->queuelist, &q->queue_head);
1839 * If we were idling with pending requests on
1840 * inactive cfqqs, force dispatching will
1841 * remove the idle timer and the queue won't
1842 * be kicked by __make_request() afterward.
1843 * Kick it here.
1845 cfq_schedule_dispatch(cfqd);
1846 break;
1847 case ELEVATOR_INSERT_FRONT:
1848 list_add(&rq->queuelist, &q->queue_head);
1849 break;
1850 case ELEVATOR_INSERT_SORT:
1851 BUG_ON(!blk_fs_request(rq));
1852 cfq_enqueue(cfqd, rq);
1853 break;
1854 default:
1855 printk("%s: bad insert point %d\n", __FUNCTION__,where);
1856 return;
1860 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1862 struct cfq_rq *crq = RQ_DATA(rq);
1863 struct cfq_queue *cfqq;
1865 if (unlikely(!blk_fs_request(rq)))
1866 return;
1868 cfqq = crq->cfq_queue;
1870 if (cfq_crq_in_flight(crq)) {
1871 const int sync = cfq_crq_is_sync(crq);
1873 WARN_ON(!cfqq->on_dispatch[sync]);
1874 cfqq->on_dispatch[sync]--;
1877 cfq_account_completion(cfqq, crq);
1880 static struct request *
1881 cfq_former_request(request_queue_t *q, struct request *rq)
1883 struct cfq_rq *crq = RQ_DATA(rq);
1884 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1886 if (rbprev)
1887 return rb_entry_crq(rbprev)->request;
1889 return NULL;
1892 static struct request *
1893 cfq_latter_request(request_queue_t *q, struct request *rq)
1895 struct cfq_rq *crq = RQ_DATA(rq);
1896 struct rb_node *rbnext = rb_next(&crq->rb_node);
1898 if (rbnext)
1899 return rb_entry_crq(rbnext)->request;
1901 return NULL;
1905 * we temporarily boost lower priority queues if they are holding fs exclusive
1906 * resources. they are boosted to normal prio (CLASS_BE/4)
1908 static void cfq_prio_boost(struct cfq_queue *cfqq)
1910 const int ioprio_class = cfqq->ioprio_class;
1911 const int ioprio = cfqq->ioprio;
1913 if (has_fs_excl()) {
1915 * boost idle prio on transactions that would lock out other
1916 * users of the filesystem
1918 if (cfq_class_idle(cfqq))
1919 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1920 if (cfqq->ioprio > IOPRIO_NORM)
1921 cfqq->ioprio = IOPRIO_NORM;
1922 } else {
1924 * check if we need to unboost the queue
1926 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1927 cfqq->ioprio_class = cfqq->org_ioprio_class;
1928 if (cfqq->ioprio != cfqq->org_ioprio)
1929 cfqq->ioprio = cfqq->org_ioprio;
1933 * refile between round-robin lists if we moved the priority class
1935 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1936 cfq_cfqq_on_rr(cfqq))
1937 cfq_resort_rr_list(cfqq, 0);
1940 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1942 if (rw == READ || process_sync(task))
1943 return task->pid;
1945 return CFQ_KEY_ASYNC;
1948 static inline int
1949 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1950 struct task_struct *task, int rw)
1952 #if 1
1953 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1954 !cfq_cfqq_must_alloc_slice(cfqq)) {
1955 cfq_mark_cfqq_must_alloc_slice(cfqq);
1956 return ELV_MQUEUE_MUST;
1959 return ELV_MQUEUE_MAY;
1960 #else
1961 if (!cfqq || task->flags & PF_MEMALLOC)
1962 return ELV_MQUEUE_MAY;
1963 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1964 if (cfq_cfqq_wait_request(cfqq))
1965 return ELV_MQUEUE_MUST;
1968 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1969 * can quickly flood the queue with writes from a single task
1971 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
1972 cfq_mark_cfqq_must_alloc_slice(cfqq);
1973 return ELV_MQUEUE_MUST;
1976 return ELV_MQUEUE_MAY;
1978 if (cfq_class_idle(cfqq))
1979 return ELV_MQUEUE_NO;
1980 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1981 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1982 int ret = ELV_MQUEUE_NO;
1984 if (ioc && ioc->nr_batch_requests)
1985 ret = ELV_MQUEUE_MAY;
1987 put_io_context(ioc);
1988 return ret;
1991 return ELV_MQUEUE_MAY;
1992 #endif
1995 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1997 struct cfq_data *cfqd = q->elevator->elevator_data;
1998 struct task_struct *tsk = current;
1999 struct cfq_queue *cfqq;
2002 * don't force setup of a queue from here, as a call to may_queue
2003 * does not necessarily imply that a request actually will be queued.
2004 * so just lookup a possibly existing queue, or return 'may queue'
2005 * if that fails
2007 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
2008 if (cfqq) {
2009 cfq_init_prio_data(cfqq);
2010 cfq_prio_boost(cfqq);
2012 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
2015 return ELV_MQUEUE_MAY;
2018 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
2020 struct cfq_data *cfqd = q->elevator->elevator_data;
2021 struct request_list *rl = &q->rq;
2023 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
2024 smp_mb();
2025 if (waitqueue_active(&rl->wait[READ]))
2026 wake_up(&rl->wait[READ]);
2029 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
2030 smp_mb();
2031 if (waitqueue_active(&rl->wait[WRITE]))
2032 wake_up(&rl->wait[WRITE]);
2037 * queue lock held here
2039 static void cfq_put_request(request_queue_t *q, struct request *rq)
2041 struct cfq_data *cfqd = q->elevator->elevator_data;
2042 struct cfq_rq *crq = RQ_DATA(rq);
2044 if (crq) {
2045 struct cfq_queue *cfqq = crq->cfq_queue;
2046 const int rw = rq_data_dir(rq);
2048 BUG_ON(!cfqq->allocated[rw]);
2049 cfqq->allocated[rw]--;
2051 put_io_context(crq->io_context->ioc);
2053 mempool_free(crq, cfqd->crq_pool);
2054 rq->elevator_private = NULL;
2056 cfq_check_waiters(q, cfqq);
2057 cfq_put_queue(cfqq);
2062 * Allocate cfq data structures associated with this request.
2064 static int
2065 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
2066 int gfp_mask)
2068 struct cfq_data *cfqd = q->elevator->elevator_data;
2069 struct task_struct *tsk = current;
2070 struct cfq_io_context *cic;
2071 const int rw = rq_data_dir(rq);
2072 pid_t key = cfq_queue_pid(tsk, rw);
2073 struct cfq_queue *cfqq;
2074 struct cfq_rq *crq;
2075 unsigned long flags;
2077 might_sleep_if(gfp_mask & __GFP_WAIT);
2079 cic = cfq_get_io_context(cfqd, key, gfp_mask);
2081 spin_lock_irqsave(q->queue_lock, flags);
2083 if (!cic)
2084 goto queue_fail;
2086 if (!cic->cfqq) {
2087 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
2088 if (!cfqq)
2089 goto queue_fail;
2091 cic->cfqq = cfqq;
2092 } else
2093 cfqq = cic->cfqq;
2095 cfqq->allocated[rw]++;
2096 cfq_clear_cfqq_must_alloc(cfqq);
2097 cfqd->rq_starved = 0;
2098 atomic_inc(&cfqq->ref);
2099 spin_unlock_irqrestore(q->queue_lock, flags);
2101 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2102 if (crq) {
2103 RB_CLEAR(&crq->rb_node);
2104 crq->rb_key = 0;
2105 crq->request = rq;
2106 INIT_HLIST_NODE(&crq->hash);
2107 crq->cfq_queue = cfqq;
2108 crq->io_context = cic;
2109 cfq_clear_crq_in_flight(crq);
2110 cfq_clear_crq_in_driver(crq);
2111 cfq_clear_crq_requeued(crq);
2113 if (rw == READ || process_sync(tsk))
2114 cfq_mark_crq_is_sync(crq);
2115 else
2116 cfq_clear_crq_is_sync(crq);
2118 rq->elevator_private = crq;
2119 return 0;
2122 spin_lock_irqsave(q->queue_lock, flags);
2123 cfqq->allocated[rw]--;
2124 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
2125 cfq_mark_cfqq_must_alloc(cfqq);
2126 cfq_put_queue(cfqq);
2127 queue_fail:
2128 if (cic)
2129 put_io_context(cic->ioc);
2131 * mark us rq allocation starved. we need to kickstart the process
2132 * ourselves if there are no pending requests that can do it for us.
2133 * that would be an extremely rare OOM situation
2135 cfqd->rq_starved = 1;
2136 cfq_schedule_dispatch(cfqd);
2137 spin_unlock_irqrestore(q->queue_lock, flags);
2138 return 1;
2141 static void cfq_kick_queue(void *data)
2143 request_queue_t *q = data;
2144 struct cfq_data *cfqd = q->elevator->elevator_data;
2145 unsigned long flags;
2147 spin_lock_irqsave(q->queue_lock, flags);
2149 if (cfqd->rq_starved) {
2150 struct request_list *rl = &q->rq;
2153 * we aren't guaranteed to get a request after this, but we
2154 * have to be opportunistic
2156 smp_mb();
2157 if (waitqueue_active(&rl->wait[READ]))
2158 wake_up(&rl->wait[READ]);
2159 if (waitqueue_active(&rl->wait[WRITE]))
2160 wake_up(&rl->wait[WRITE]);
2163 blk_remove_plug(q);
2164 q->request_fn(q);
2165 spin_unlock_irqrestore(q->queue_lock, flags);
2169 * Timer running if the active_queue is currently idling inside its time slice
2171 static void cfq_idle_slice_timer(unsigned long data)
2173 struct cfq_data *cfqd = (struct cfq_data *) data;
2174 struct cfq_queue *cfqq;
2175 unsigned long flags;
2177 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2179 if ((cfqq = cfqd->active_queue) != NULL) {
2180 unsigned long now = jiffies;
2183 * expired
2185 if (time_after(now, cfqq->slice_end))
2186 goto expire;
2189 * only expire and reinvoke request handler, if there are
2190 * other queues with pending requests
2192 if (!cfq_pending_requests(cfqd)) {
2193 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2194 add_timer(&cfqd->idle_slice_timer);
2195 goto out_cont;
2199 * not expired and it has a request pending, let it dispatch
2201 if (!RB_EMPTY(&cfqq->sort_list)) {
2202 cfq_mark_cfqq_must_dispatch(cfqq);
2203 goto out_kick;
2206 expire:
2207 cfq_slice_expired(cfqd, 0);
2208 out_kick:
2209 cfq_schedule_dispatch(cfqd);
2210 out_cont:
2211 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2215 * Timer running if an idle class queue is waiting for service
2217 static void cfq_idle_class_timer(unsigned long data)
2219 struct cfq_data *cfqd = (struct cfq_data *) data;
2220 unsigned long flags, end;
2222 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2225 * race with a non-idle queue, reset timer
2227 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2228 if (!time_after_eq(jiffies, end)) {
2229 cfqd->idle_class_timer.expires = end;
2230 add_timer(&cfqd->idle_class_timer);
2231 } else
2232 cfq_schedule_dispatch(cfqd);
2234 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2237 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2239 del_timer_sync(&cfqd->idle_slice_timer);
2240 del_timer_sync(&cfqd->idle_class_timer);
2241 blk_sync_queue(cfqd->queue);
2244 static void cfq_put_cfqd(struct cfq_data *cfqd)
2246 request_queue_t *q = cfqd->queue;
2248 if (!atomic_dec_and_test(&cfqd->ref))
2249 return;
2251 blk_put_queue(q);
2253 cfq_shutdown_timer_wq(cfqd);
2254 q->elevator->elevator_data = NULL;
2256 mempool_destroy(cfqd->crq_pool);
2257 kfree(cfqd->crq_hash);
2258 kfree(cfqd->cfq_hash);
2259 kfree(cfqd);
2262 static void cfq_exit_queue(elevator_t *e)
2264 struct cfq_data *cfqd = e->elevator_data;
2266 cfq_shutdown_timer_wq(cfqd);
2267 cfq_put_cfqd(cfqd);
2270 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2272 struct cfq_data *cfqd;
2273 int i;
2275 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2276 if (!cfqd)
2277 return -ENOMEM;
2279 memset(cfqd, 0, sizeof(*cfqd));
2281 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2282 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2284 INIT_LIST_HEAD(&cfqd->busy_rr);
2285 INIT_LIST_HEAD(&cfqd->cur_rr);
2286 INIT_LIST_HEAD(&cfqd->idle_rr);
2287 INIT_LIST_HEAD(&cfqd->empty_list);
2289 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2290 if (!cfqd->crq_hash)
2291 goto out_crqhash;
2293 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2294 if (!cfqd->cfq_hash)
2295 goto out_cfqhash;
2297 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2298 if (!cfqd->crq_pool)
2299 goto out_crqpool;
2301 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2302 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2303 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2304 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2306 e->elevator_data = cfqd;
2308 cfqd->queue = q;
2309 atomic_inc(&q->refcnt);
2311 cfqd->max_queued = q->nr_requests / 4;
2312 q->nr_batching = cfq_queued;
2314 init_timer(&cfqd->idle_slice_timer);
2315 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2316 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2318 init_timer(&cfqd->idle_class_timer);
2319 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2320 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2322 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2324 atomic_set(&cfqd->ref, 1);
2326 cfqd->cfq_queued = cfq_queued;
2327 cfqd->cfq_quantum = cfq_quantum;
2328 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2329 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
2330 cfqd->cfq_back_max = cfq_back_max;
2331 cfqd->cfq_back_penalty = cfq_back_penalty;
2332 cfqd->cfq_slice[0] = cfq_slice_async;
2333 cfqd->cfq_slice[1] = cfq_slice_sync;
2334 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2335 cfqd->cfq_slice_idle = cfq_slice_idle;
2336 cfqd->cfq_max_depth = cfq_max_depth;
2338 return 0;
2339 out_crqpool:
2340 kfree(cfqd->cfq_hash);
2341 out_cfqhash:
2342 kfree(cfqd->crq_hash);
2343 out_crqhash:
2344 kfree(cfqd);
2345 return -ENOMEM;
2348 static void cfq_slab_kill(void)
2350 if (crq_pool)
2351 kmem_cache_destroy(crq_pool);
2352 if (cfq_pool)
2353 kmem_cache_destroy(cfq_pool);
2354 if (cfq_ioc_pool)
2355 kmem_cache_destroy(cfq_ioc_pool);
2358 static int __init cfq_slab_setup(void)
2360 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2361 NULL, NULL);
2362 if (!crq_pool)
2363 goto fail;
2365 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2366 NULL, NULL);
2367 if (!cfq_pool)
2368 goto fail;
2370 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2371 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2372 if (!cfq_ioc_pool)
2373 goto fail;
2375 return 0;
2376 fail:
2377 cfq_slab_kill();
2378 return -ENOMEM;
2382 * sysfs parts below -->
2384 struct cfq_fs_entry {
2385 struct attribute attr;
2386 ssize_t (*show)(struct cfq_data *, char *);
2387 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2390 static ssize_t
2391 cfq_var_show(unsigned int var, char *page)
2393 return sprintf(page, "%d\n", var);
2396 static ssize_t
2397 cfq_var_store(unsigned int *var, const char *page, size_t count)
2399 char *p = (char *) page;
2401 *var = simple_strtoul(p, &p, 10);
2402 return count;
2405 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2406 static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2408 unsigned int __data = __VAR; \
2409 if (__CONV) \
2410 __data = jiffies_to_msecs(__data); \
2411 return cfq_var_show(__data, (page)); \
2413 SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2414 SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
2415 SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2416 SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
2417 SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2418 SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
2419 SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2420 SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2421 SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2422 SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2423 SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
2424 #undef SHOW_FUNCTION
2426 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2427 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2429 unsigned int __data; \
2430 int ret = cfq_var_store(&__data, (page), count); \
2431 if (__data < (MIN)) \
2432 __data = (MIN); \
2433 else if (__data > (MAX)) \
2434 __data = (MAX); \
2435 if (__CONV) \
2436 *(__PTR) = msecs_to_jiffies(__data); \
2437 else \
2438 *(__PTR) = __data; \
2439 return ret; \
2441 STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2442 STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
2443 STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2444 STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
2445 STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2446 STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
2447 STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2448 STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2449 STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2450 STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2451 STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
2452 #undef STORE_FUNCTION
2454 static struct cfq_fs_entry cfq_quantum_entry = {
2455 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2456 .show = cfq_quantum_show,
2457 .store = cfq_quantum_store,
2459 static struct cfq_fs_entry cfq_queued_entry = {
2460 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2461 .show = cfq_queued_show,
2462 .store = cfq_queued_store,
2464 static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
2465 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
2466 .show = cfq_fifo_expire_sync_show,
2467 .store = cfq_fifo_expire_sync_store,
2469 static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
2470 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
2471 .show = cfq_fifo_expire_async_show,
2472 .store = cfq_fifo_expire_async_store,
2474 static struct cfq_fs_entry cfq_back_max_entry = {
2475 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2476 .show = cfq_back_max_show,
2477 .store = cfq_back_max_store,
2479 static struct cfq_fs_entry cfq_back_penalty_entry = {
2480 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2481 .show = cfq_back_penalty_show,
2482 .store = cfq_back_penalty_store,
2484 static struct cfq_fs_entry cfq_slice_sync_entry = {
2485 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2486 .show = cfq_slice_sync_show,
2487 .store = cfq_slice_sync_store,
2489 static struct cfq_fs_entry cfq_slice_async_entry = {
2490 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2491 .show = cfq_slice_async_show,
2492 .store = cfq_slice_async_store,
2494 static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2495 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2496 .show = cfq_slice_async_rq_show,
2497 .store = cfq_slice_async_rq_store,
2499 static struct cfq_fs_entry cfq_slice_idle_entry = {
2500 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2501 .show = cfq_slice_idle_show,
2502 .store = cfq_slice_idle_store,
2504 static struct cfq_fs_entry cfq_max_depth_entry = {
2505 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2506 .show = cfq_max_depth_show,
2507 .store = cfq_max_depth_store,
2510 static struct attribute *default_attrs[] = {
2511 &cfq_quantum_entry.attr,
2512 &cfq_queued_entry.attr,
2513 &cfq_fifo_expire_sync_entry.attr,
2514 &cfq_fifo_expire_async_entry.attr,
2515 &cfq_back_max_entry.attr,
2516 &cfq_back_penalty_entry.attr,
2517 &cfq_slice_sync_entry.attr,
2518 &cfq_slice_async_entry.attr,
2519 &cfq_slice_async_rq_entry.attr,
2520 &cfq_slice_idle_entry.attr,
2521 &cfq_max_depth_entry.attr,
2522 NULL,
2525 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2527 static ssize_t
2528 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2530 elevator_t *e = container_of(kobj, elevator_t, kobj);
2531 struct cfq_fs_entry *entry = to_cfq(attr);
2533 if (!entry->show)
2534 return -EIO;
2536 return entry->show(e->elevator_data, page);
2539 static ssize_t
2540 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2541 const char *page, size_t length)
2543 elevator_t *e = container_of(kobj, elevator_t, kobj);
2544 struct cfq_fs_entry *entry = to_cfq(attr);
2546 if (!entry->store)
2547 return -EIO;
2549 return entry->store(e->elevator_data, page, length);
2552 static struct sysfs_ops cfq_sysfs_ops = {
2553 .show = cfq_attr_show,
2554 .store = cfq_attr_store,
2557 static struct kobj_type cfq_ktype = {
2558 .sysfs_ops = &cfq_sysfs_ops,
2559 .default_attrs = default_attrs,
2562 static struct elevator_type iosched_cfq = {
2563 .ops = {
2564 .elevator_merge_fn = cfq_merge,
2565 .elevator_merged_fn = cfq_merged_request,
2566 .elevator_merge_req_fn = cfq_merged_requests,
2567 .elevator_next_req_fn = cfq_next_request,
2568 .elevator_add_req_fn = cfq_insert_request,
2569 .elevator_remove_req_fn = cfq_remove_request,
2570 .elevator_requeue_req_fn = cfq_requeue_request,
2571 .elevator_deactivate_req_fn = cfq_deactivate_request,
2572 .elevator_queue_empty_fn = cfq_queue_empty,
2573 .elevator_completed_req_fn = cfq_completed_request,
2574 .elevator_former_req_fn = cfq_former_request,
2575 .elevator_latter_req_fn = cfq_latter_request,
2576 .elevator_set_req_fn = cfq_set_request,
2577 .elevator_put_req_fn = cfq_put_request,
2578 .elevator_may_queue_fn = cfq_may_queue,
2579 .elevator_init_fn = cfq_init_queue,
2580 .elevator_exit_fn = cfq_exit_queue,
2582 .elevator_ktype = &cfq_ktype,
2583 .elevator_name = "cfq",
2584 .elevator_owner = THIS_MODULE,
2587 static int __init cfq_init(void)
2589 int ret;
2592 * could be 0 on HZ < 1000 setups
2594 if (!cfq_slice_async)
2595 cfq_slice_async = 1;
2596 if (!cfq_slice_idle)
2597 cfq_slice_idle = 1;
2599 if (cfq_slab_setup())
2600 return -ENOMEM;
2602 ret = elv_register(&iosched_cfq);
2603 if (ret)
2604 cfq_slab_kill();
2606 return ret;
2609 static void __exit cfq_exit(void)
2611 struct task_struct *g, *p;
2612 unsigned long flags;
2614 read_lock_irqsave(&tasklist_lock, flags);
2617 * iterate each process in the system, removing our io_context
2619 do_each_thread(g, p) {
2620 struct io_context *ioc = p->io_context;
2622 if (ioc && ioc->cic) {
2623 ioc->cic->exit(ioc->cic);
2624 cfq_free_io_context(ioc->cic);
2625 ioc->cic = NULL;
2627 } while_each_thread(g, p);
2629 read_unlock_irqrestore(&tasklist_lock, flags);
2631 cfq_slab_kill();
2632 elv_unregister(&iosched_cfq);
2635 module_init(cfq_init);
2636 module_exit(cfq_exit);
2638 MODULE_AUTHOR("Jens Axboe");
2639 MODULE_LICENSE("GPL");
2640 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");