ff1cc968f96d12a573f84dbe77fcc2cf547460c3
[linux-3.10.git] / drivers / block / cfq-iosched.c
1 /*
2  *  linux/drivers/block/cfq-iosched.c
3  *
4  *  CFQ, or complete fairness queueing, disk scheduler.
5  *
6  *  Based on ideas from a previously unfinished io
7  *  scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
8  *
9  *  Copyright (C) 2003 Jens Axboe <axboe@suse.de>
10  */
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>
26
27 /*
28  * tunables
29  */
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 */
35
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;
40
41 #define CFQ_IDLE_GRACE          (HZ / 10)
42 #define CFQ_SLICE_SCALE         (5)
43
44 #define CFQ_KEY_ASYNC           (0)
45 #define CFQ_KEY_ANY             (0xffff)
46
47 /*
48  * disable queueing at the driver/hardware level
49  */
50 static int cfq_max_depth = 1;
51
52 /*
53  * for the hash of cfqq inside the cfqd
54  */
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)
58
59 /*
60  * for the hash of crq inside the cfqq
61  */
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)
68
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)
71
72 #define RQ_DATA(rq)             (rq)->elevator_private
73
74 /*
75  * rb-tree defines
76  */
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
90
91 static kmem_cache_t *crq_pool;
92 static kmem_cache_t *cfq_pool;
93 static kmem_cache_t *cfq_ioc_pool;
94
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)
99
100 #define ASYNC                   (0)
101 #define SYNC                    (1)
102
103 #define cfq_cfqq_dispatched(cfqq)       \
104         ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
105
106 #define cfq_cfqq_class_sync(cfqq)       ((cfqq)->key != CFQ_KEY_ASYNC)
107
108 #define cfq_cfqq_sync(cfqq)             \
109         (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
110
111 /*
112  * Per block device queue structure
113  */
114 struct cfq_data {
115         atomic_t ref;
116         request_queue_t *queue;
117
118         /*
119          * rr list of queues with requests and the count of them
120          */
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;
126
127         /*
128          * non-ordered list of empty cfqq's
129          */
130         struct list_head empty_list;
131
132         /*
133          * cfqq lookup hash
134          */
135         struct hlist_head *cfq_hash;
136
137         /*
138          * global crq hash for all queues
139          */
140         struct hlist_head *crq_hash;
141
142         unsigned int max_queued;
143
144         mempool_t *crq_pool;
145
146         int rq_in_driver;
147
148         /*
149          * schedule slice state info
150          */
151         /*
152          * idle window management
153          */
154         struct timer_list idle_slice_timer;
155         struct work_struct unplug_work;
156
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;
161
162         struct timer_list idle_class_timer;
163
164         sector_t last_sector;
165         unsigned long last_end_request;
166
167         unsigned int rq_starved;
168
169         /*
170          * tunables, see top of file
171          */
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;
181 };
182
183 /*
184  * Per process-grouping structure
185  */
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;
207
208         unsigned long slice_start;
209         unsigned long slice_end;
210         unsigned long slice_left;
211         unsigned long service_last;
212
213         /* number of requests that are on the dispatch list */
214         int on_dispatch[2];
215
216         /* io prio of this group */
217         unsigned short ioprio, org_ioprio;
218         unsigned short ioprio_class, org_ioprio_class;
219
220         /* various state flags, see below */
221         unsigned int flags;
222 };
223
224 struct cfq_rq {
225         struct rb_node rb_node;
226         sector_t rb_key;
227         struct request *request;
228         struct hlist_node hash;
229
230         struct cfq_queue *cfq_queue;
231         struct cfq_io_context *io_context;
232
233         unsigned int crq_flags;
234 };
235
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,
246 };
247
248 #define CFQ_CFQQ_FNS(name)                                              \
249 static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq)         \
250 {                                                                       \
251         cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name);                     \
252 }                                                                       \
253 static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq)        \
254 {                                                                       \
255         cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name);                    \
256 }                                                                       \
257 static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq)         \
258 {                                                                       \
259         return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0;        \
260 }
261
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
272
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,
278 };
279
280 #define CFQ_CRQ_FNS(name)                                               \
281 static inline void cfq_mark_crq_##name(struct cfq_rq *crq)              \
282 {                                                                       \
283         crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name);                   \
284 }                                                                       \
285 static inline void cfq_clear_crq_##name(struct cfq_rq *crq)             \
286 {                                                                       \
287         crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name);                  \
288 }                                                                       \
289 static inline int cfq_crq_##name(const struct cfq_rq *crq)              \
290 {                                                                       \
291         return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0;      \
292 }
293
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
299
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);
303 static inline int cfq_pending_requests(struct cfq_data *cfqd);
304
305 #define process_sync(tsk)       ((tsk)->flags & PF_SYNCWRITE)
306
307 /*
308  * lots of deadline iosched dupes, can be abstracted later...
309  */
310 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
311 {
312         hlist_del_init(&crq->hash);
313 }
314
315 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
316 {
317         cfq_del_crq_hash(crq);
318
319         if (q->last_merge == crq->request)
320                 q->last_merge = NULL;
321 }
322
323 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
324 {
325         const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
326
327         hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
328 }
329
330 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
331 {
332         struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
333         struct hlist_node *entry, *next;
334
335         hlist_for_each_safe(entry, next, hash_list) {
336                 struct cfq_rq *crq = list_entry_hash(entry);
337                 struct request *__rq = crq->request;
338
339                 if (!rq_mergeable(__rq)) {
340                         cfq_del_crq_hash(crq);
341                         continue;
342                 }
343
344                 if (rq_hash_key(__rq) == offset)
345                         return __rq;
346         }
347
348         return NULL;
349 }
350
351 /*
352  * Lifted from AS - choose which of crq1 and crq2 that is best served now.
353  * We choose the request that is closest to the head right now. Distance
354  * behind the head are penalized and only allowed to a certain extent.
355  */
356 static struct cfq_rq *
357 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
358 {
359         sector_t last, s1, s2, d1 = 0, d2 = 0;
360         int r1_wrap = 0, r2_wrap = 0;   /* requests are behind the disk head */
361         unsigned long back_max;
362
363         if (crq1 == NULL || crq1 == crq2)
364                 return crq2;
365         if (crq2 == NULL)
366                 return crq1;
367         if (cfq_crq_requeued(crq1))
368                 return crq1;
369         if (cfq_crq_requeued(crq2))
370                 return crq2;
371
372         s1 = crq1->request->sector;
373         s2 = crq2->request->sector;
374
375         last = cfqd->last_sector;
376
377         /*
378          * by definition, 1KiB is 2 sectors
379          */
380         back_max = cfqd->cfq_back_max * 2;
381
382         /*
383          * Strict one way elevator _except_ in the case where we allow
384          * short backward seeks which are biased as twice the cost of a
385          * similar forward seek.
386          */
387         if (s1 >= last)
388                 d1 = s1 - last;
389         else if (s1 + back_max >= last)
390                 d1 = (last - s1) * cfqd->cfq_back_penalty;
391         else
392                 r1_wrap = 1;
393
394         if (s2 >= last)
395                 d2 = s2 - last;
396         else if (s2 + back_max >= last)
397                 d2 = (last - s2) * cfqd->cfq_back_penalty;
398         else
399                 r2_wrap = 1;
400
401         /* Found required data */
402         if (!r1_wrap && r2_wrap)
403                 return crq1;
404         else if (!r2_wrap && r1_wrap)
405                 return crq2;
406         else if (r1_wrap && r2_wrap) {
407                 /* both behind the head */
408                 if (s1 <= s2)
409                         return crq1;
410                 else
411                         return crq2;
412         }
413
414         /* Both requests in front of the head */
415         if (d1 < d2)
416                 return crq1;
417         else if (d2 < d1)
418                 return crq2;
419         else {
420                 if (s1 >= s2)
421                         return crq1;
422                 else
423                         return crq2;
424         }
425 }
426
427 /*
428  * would be nice to take fifo expire time into account as well
429  */
430 static struct cfq_rq *
431 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
432                   struct cfq_rq *last)
433 {
434         struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
435         struct rb_node *rbnext, *rbprev;
436
437         rbnext = NULL;
438         if (ON_RB(&last->rb_node))
439                 rbnext = rb_next(&last->rb_node);
440         if (!rbnext) {
441                 rbnext = rb_first(&cfqq->sort_list);
442                 if (rbnext == &last->rb_node)
443                         rbnext = NULL;
444         }
445
446         rbprev = rb_prev(&last->rb_node);
447
448         if (rbprev)
449                 crq_prev = rb_entry_crq(rbprev);
450         if (rbnext)
451                 crq_next = rb_entry_crq(rbnext);
452
453         return cfq_choose_req(cfqd, crq_next, crq_prev);
454 }
455
456 static void cfq_update_next_crq(struct cfq_rq *crq)
457 {
458         struct cfq_queue *cfqq = crq->cfq_queue;
459
460         if (cfqq->next_crq == crq)
461                 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
462 }
463
464 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
465 {
466         struct cfq_data *cfqd = cfqq->cfqd;
467         struct list_head *list, *entry;
468
469         BUG_ON(!cfq_cfqq_on_rr(cfqq));
470
471         list_del(&cfqq->cfq_list);
472
473         if (cfq_class_rt(cfqq))
474                 list = &cfqd->cur_rr;
475         else if (cfq_class_idle(cfqq))
476                 list = &cfqd->idle_rr;
477         else {
478                 /*
479                  * if cfqq has requests in flight, don't allow it to be
480                  * found in cfq_set_active_queue before it has finished them.
481                  * this is done to increase fairness between a process that
482                  * has lots of io pending vs one that only generates one
483                  * sporadically or synchronously
484                  */
485                 if (cfq_cfqq_dispatched(cfqq))
486                         list = &cfqd->busy_rr;
487                 else
488                         list = &cfqd->rr_list[cfqq->ioprio];
489         }
490
491         /*
492          * if queue was preempted, just add to front to be fair. busy_rr
493          * isn't sorted.
494          */
495         if (preempted || list == &cfqd->busy_rr) {
496                 list_add(&cfqq->cfq_list, list);
497                 return;
498         }
499
500         /*
501          * sort by when queue was last serviced
502          */
503         entry = list;
504         while ((entry = entry->prev) != list) {
505                 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
506
507                 if (!__cfqq->service_last)
508                         break;
509                 if (time_before(__cfqq->service_last, cfqq->service_last))
510                         break;
511         }
512
513         list_add(&cfqq->cfq_list, entry);
514 }
515
516 /*
517  * add to busy list of queues for service, trying to be fair in ordering
518  * the pending list according to last request service
519  */
520 static inline void
521 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
522 {
523         BUG_ON(cfq_cfqq_on_rr(cfqq));
524         cfq_mark_cfqq_on_rr(cfqq);
525         cfqd->busy_queues++;
526
527         cfq_resort_rr_list(cfqq, requeue);
528 }
529
530 static inline void
531 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
532 {
533         BUG_ON(!cfq_cfqq_on_rr(cfqq));
534         cfq_clear_cfqq_on_rr(cfqq);
535         list_move(&cfqq->cfq_list, &cfqd->empty_list);
536
537         BUG_ON(!cfqd->busy_queues);
538         cfqd->busy_queues--;
539 }
540
541 /*
542  * rb tree support functions
543  */
544 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
545 {
546         struct cfq_queue *cfqq = crq->cfq_queue;
547
548         if (ON_RB(&crq->rb_node)) {
549                 struct cfq_data *cfqd = cfqq->cfqd;
550                 const int sync = cfq_crq_is_sync(crq);
551
552                 BUG_ON(!cfqq->queued[sync]);
553                 cfqq->queued[sync]--;
554
555                 cfq_update_next_crq(crq);
556
557                 rb_erase(&crq->rb_node, &cfqq->sort_list);
558                 RB_CLEAR_COLOR(&crq->rb_node);
559
560                 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
561                         cfq_del_cfqq_rr(cfqd, cfqq);
562         }
563 }
564
565 static struct cfq_rq *
566 __cfq_add_crq_rb(struct cfq_rq *crq)
567 {
568         struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
569         struct rb_node *parent = NULL;
570         struct cfq_rq *__crq;
571
572         while (*p) {
573                 parent = *p;
574                 __crq = rb_entry_crq(parent);
575
576                 if (crq->rb_key < __crq->rb_key)
577                         p = &(*p)->rb_left;
578                 else if (crq->rb_key > __crq->rb_key)
579                         p = &(*p)->rb_right;
580                 else
581                         return __crq;
582         }
583
584         rb_link_node(&crq->rb_node, parent, p);
585         return NULL;
586 }
587
588 static void cfq_add_crq_rb(struct cfq_rq *crq)
589 {
590         struct cfq_queue *cfqq = crq->cfq_queue;
591         struct cfq_data *cfqd = cfqq->cfqd;
592         struct request *rq = crq->request;
593         struct cfq_rq *__alias;
594
595         crq->rb_key = rq_rb_key(rq);
596         cfqq->queued[cfq_crq_is_sync(crq)]++;
597
598         /*
599          * looks a little odd, but the first insert might return an alias.
600          * if that happens, put the alias on the dispatch list
601          */
602         while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
603                 cfq_dispatch_sort(cfqd->queue, __alias);
604
605         rb_insert_color(&crq->rb_node, &cfqq->sort_list);
606
607         if (!cfq_cfqq_on_rr(cfqq))
608                 cfq_add_cfqq_rr(cfqd, cfqq, cfq_crq_requeued(crq));
609
610         /*
611          * check if this request is a better next-serve candidate
612          */
613         cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
614 }
615
616 static inline void
617 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
618 {
619         if (ON_RB(&crq->rb_node)) {
620                 rb_erase(&crq->rb_node, &cfqq->sort_list);
621                 cfqq->queued[cfq_crq_is_sync(crq)]--;
622         }
623
624         cfq_add_crq_rb(crq);
625 }
626
627 static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
628
629 {
630         struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
631         struct rb_node *n;
632
633         if (!cfqq)
634                 goto out;
635
636         n = cfqq->sort_list.rb_node;
637         while (n) {
638                 struct cfq_rq *crq = rb_entry_crq(n);
639
640                 if (sector < crq->rb_key)
641                         n = n->rb_left;
642                 else if (sector > crq->rb_key)
643                         n = n->rb_right;
644                 else
645                         return crq->request;
646         }
647
648 out:
649         return NULL;
650 }
651
652 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
653 {
654         struct cfq_data *cfqd = q->elevator->elevator_data;
655         struct cfq_rq *crq = RQ_DATA(rq);
656
657         if (crq) {
658                 struct cfq_queue *cfqq = crq->cfq_queue;
659
660                 if (cfq_crq_in_driver(crq)) {
661                         cfq_clear_crq_in_driver(crq);
662                         WARN_ON(!cfqd->rq_in_driver);
663                         cfqd->rq_in_driver--;
664                 }
665                 if (cfq_crq_in_flight(crq)) {
666                         const int sync = cfq_crq_is_sync(crq);
667
668                         cfq_clear_crq_in_flight(crq);
669                         WARN_ON(!cfqq->on_dispatch[sync]);
670                         cfqq->on_dispatch[sync]--;
671                 }
672                 cfq_mark_crq_requeued(crq);
673         }
674 }
675
676 /*
677  * make sure the service time gets corrected on reissue of this request
678  */
679 static void cfq_requeue_request(request_queue_t *q, struct request *rq)
680 {
681         cfq_deactivate_request(q, rq);
682         list_add(&rq->queuelist, &q->queue_head);
683 }
684
685 static void cfq_remove_request(request_queue_t *q, struct request *rq)
686 {
687         struct cfq_rq *crq = RQ_DATA(rq);
688
689         if (crq) {
690                 list_del_init(&rq->queuelist);
691                 cfq_del_crq_rb(crq);
692                 cfq_remove_merge_hints(q, crq);
693
694         }
695 }
696
697 static int
698 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
699 {
700         struct cfq_data *cfqd = q->elevator->elevator_data;
701         struct request *__rq;
702         int ret;
703
704         ret = elv_try_last_merge(q, bio);
705         if (ret != ELEVATOR_NO_MERGE) {
706                 __rq = q->last_merge;
707                 goto out_insert;
708         }
709
710         __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
711         if (__rq && elv_rq_merge_ok(__rq, bio)) {
712                 ret = ELEVATOR_BACK_MERGE;
713                 goto out;
714         }
715
716         __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
717         if (__rq && elv_rq_merge_ok(__rq, bio)) {
718                 ret = ELEVATOR_FRONT_MERGE;
719                 goto out;
720         }
721
722         return ELEVATOR_NO_MERGE;
723 out:
724         q->last_merge = __rq;
725 out_insert:
726         *req = __rq;
727         return ret;
728 }
729
730 static void cfq_merged_request(request_queue_t *q, struct request *req)
731 {
732         struct cfq_data *cfqd = q->elevator->elevator_data;
733         struct cfq_rq *crq = RQ_DATA(req);
734
735         cfq_del_crq_hash(crq);
736         cfq_add_crq_hash(cfqd, crq);
737
738         if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
739                 struct cfq_queue *cfqq = crq->cfq_queue;
740
741                 cfq_update_next_crq(crq);
742                 cfq_reposition_crq_rb(cfqq, crq);
743         }
744
745         q->last_merge = req;
746 }
747
748 static void
749 cfq_merged_requests(request_queue_t *q, struct request *rq,
750                     struct request *next)
751 {
752         cfq_merged_request(q, rq);
753
754         /*
755          * reposition in fifo if next is older than rq
756          */
757         if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
758             time_before(next->start_time, rq->start_time))
759                 list_move(&rq->queuelist, &next->queuelist);
760
761         cfq_remove_request(q, next);
762 }
763
764 static inline void
765 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
766 {
767         if (cfqq) {
768                 /*
769                  * stop potential idle class queues waiting service
770                  */
771                 del_timer(&cfqd->idle_class_timer);
772
773                 cfqq->slice_start = jiffies;
774                 cfqq->slice_end = 0;
775                 cfqq->slice_left = 0;
776                 cfq_clear_cfqq_must_alloc_slice(cfqq);
777                 cfq_clear_cfqq_fifo_expire(cfqq);
778                 cfq_clear_cfqq_expired(cfqq);
779         }
780
781         cfqd->active_queue = cfqq;
782 }
783
784 /*
785  * 0
786  * 0,1
787  * 0,1,2
788  * 0,1,2,3
789  * 0,1,2,3,4
790  * 0,1,2,3,4,5
791  * 0,1,2,3,4,5,6
792  * 0,1,2,3,4,5,6,7
793  */
794 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
795 {
796         int prio, wrap;
797
798         prio = -1;
799         wrap = 0;
800         do {
801                 int p;
802
803                 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
804                         if (!list_empty(&cfqd->rr_list[p])) {
805                                 prio = p;
806                                 break;
807                         }
808                 }
809
810                 if (prio != -1)
811                         break;
812                 cfqd->cur_prio = 0;
813                 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
814                         cfqd->cur_end_prio = 0;
815                         if (wrap)
816                                 break;
817                         wrap = 1;
818                 }
819         } while (1);
820
821         if (unlikely(prio == -1))
822                 return -1;
823
824         BUG_ON(prio >= CFQ_PRIO_LISTS);
825
826         list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
827
828         cfqd->cur_prio = prio + 1;
829         if (cfqd->cur_prio > cfqd->cur_end_prio) {
830                 cfqd->cur_end_prio = cfqd->cur_prio;
831                 cfqd->cur_prio = 0;
832         }
833         if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
834                 cfqd->cur_prio = 0;
835                 cfqd->cur_end_prio = 0;
836         }
837
838         return prio;
839 }
840
841 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
842 {
843         struct cfq_queue *cfqq;
844
845         /*
846          * if current queue is expired but not done with its requests yet,
847          * wait for that to happen
848          */
849         if ((cfqq = cfqd->active_queue) != NULL) {
850                 if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
851                         return NULL;
852         }
853
854         /*
855          * if current list is non-empty, grab first entry. if it is empty,
856          * get next prio level and grab first entry then if any are spliced
857          */
858         if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
859                 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
860
861         /*
862          * if we have idle queues and no rt or be queues had pending
863          * requests, either allow immediate service if the grace period
864          * has passed or arm the idle grace timer
865          */
866         if (!cfqq && !list_empty(&cfqd->idle_rr)) {
867                 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
868
869                 if (time_after_eq(jiffies, end))
870                         cfqq = list_entry_cfqq(cfqd->idle_rr.next);
871                 else
872                         mod_timer(&cfqd->idle_class_timer, end);
873         }
874
875         __cfq_set_active_queue(cfqd, cfqq);
876         return cfqq;
877 }
878
879 /*
880  * current cfqq expired its slice (or was too idle), select new one
881  */
882 static void
883 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
884                     int preempted)
885 {
886         unsigned long now = jiffies;
887
888         if (cfq_cfqq_wait_request(cfqq))
889                 del_timer(&cfqd->idle_slice_timer);
890
891         if (!preempted && !cfq_cfqq_dispatched(cfqq))
892                 cfqq->service_last = now;
893
894         cfq_clear_cfqq_must_dispatch(cfqq);
895         cfq_clear_cfqq_wait_request(cfqq);
896
897         /*
898          * store what was left of this slice, if the queue idled out
899          * or was preempted
900          */
901         if (time_after(now, cfqq->slice_end))
902                 cfqq->slice_left = now - cfqq->slice_end;
903         else
904                 cfqq->slice_left = 0;
905
906         if (cfq_cfqq_on_rr(cfqq))
907                 cfq_resort_rr_list(cfqq, preempted);
908
909         if (cfqq == cfqd->active_queue)
910                 cfqd->active_queue = NULL;
911
912         if (cfqd->active_cic) {
913                 put_io_context(cfqd->active_cic->ioc);
914                 cfqd->active_cic = NULL;
915         }
916
917         cfqd->dispatch_slice = 0;
918 }
919
920 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
921 {
922         struct cfq_queue *cfqq = cfqd->active_queue;
923
924         if (cfqq) {
925                 /*
926                  * use deferred expiry, if there are requests in progress as
927                  * not to disturb the slice of the next queue
928                  */
929                 if (cfq_cfqq_dispatched(cfqq))
930                         cfq_mark_cfqq_expired(cfqq);
931                 else
932                         __cfq_slice_expired(cfqd, cfqq, preempted);
933         }
934 }
935
936 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
937
938 {
939         WARN_ON(!RB_EMPTY(&cfqq->sort_list));
940         WARN_ON(cfqq != cfqd->active_queue);
941
942         /*
943          * idle is disabled, either manually or by past process history
944          */
945         if (!cfqd->cfq_slice_idle)
946                 return 0;
947         if (!cfq_cfqq_idle_window(cfqq))
948                 return 0;
949         /*
950          * task has exited, don't wait
951          */
952         if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
953                 return 0;
954
955         cfq_mark_cfqq_must_dispatch(cfqq);
956         cfq_mark_cfqq_wait_request(cfqq);
957
958         if (!timer_pending(&cfqd->idle_slice_timer)) {
959                 unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
960
961                 cfqd->idle_slice_timer.expires = jiffies + slice_left;
962                 add_timer(&cfqd->idle_slice_timer);
963         }
964
965         return 1;
966 }
967
968 /*
969  * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
970  * this function sector sorts the selected request to minimize seeks. we start
971  * at cfqd->last_sector, not 0.
972  */
973 static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
974 {
975         struct cfq_data *cfqd = q->elevator->elevator_data;
976         struct cfq_queue *cfqq = crq->cfq_queue;
977         struct list_head *head = &q->queue_head, *entry = head;
978         struct request *__rq;
979         sector_t last;
980
981         list_del(&crq->request->queuelist);
982
983         last = cfqd->last_sector;
984         list_for_each_entry_reverse(__rq, head, queuelist) {
985                 struct cfq_rq *__crq = RQ_DATA(__rq);
986
987                 if (blk_barrier_rq(__rq))
988                         break;
989                 if (!blk_fs_request(__rq))
990                         break;
991                 if (cfq_crq_requeued(__crq))
992                         break;
993
994                 if (__rq->sector <= crq->request->sector)
995                         break;
996                 if (__rq->sector > last && crq->request->sector < last) {
997                         last = crq->request->sector + crq->request->nr_sectors;
998                         break;
999                 }
1000                 entry = &__rq->queuelist;
1001         }
1002
1003         cfqd->last_sector = last;
1004
1005         cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
1006
1007         cfq_del_crq_rb(crq);
1008         cfq_remove_merge_hints(q, crq);
1009
1010         cfq_mark_crq_in_flight(crq);
1011         cfq_clear_crq_requeued(crq);
1012
1013         cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
1014         list_add_tail(&crq->request->queuelist, entry);
1015 }
1016
1017 /*
1018  * return expired entry, or NULL to just start from scratch in rbtree
1019  */
1020 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
1021 {
1022         struct cfq_data *cfqd = cfqq->cfqd;
1023         struct request *rq;
1024         struct cfq_rq *crq;
1025
1026         if (cfq_cfqq_fifo_expire(cfqq))
1027                 return NULL;
1028
1029         if (!list_empty(&cfqq->fifo)) {
1030                 int fifo = cfq_cfqq_class_sync(cfqq);
1031
1032                 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
1033                 rq = crq->request;
1034                 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
1035                         cfq_mark_cfqq_fifo_expire(cfqq);
1036                         return crq;
1037                 }
1038         }
1039
1040         return NULL;
1041 }
1042
1043 /*
1044  * Scale schedule slice based on io priority. Use the sync time slice only
1045  * if a queue is marked sync and has sync io queued. A sync queue with async
1046  * io only, should not get full sync slice length.
1047  */
1048 static inline int
1049 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1050 {
1051         const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
1052
1053         WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1054
1055         return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
1056 }
1057
1058 static inline void
1059 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1060 {
1061         cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1062 }
1063
1064 static inline int
1065 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1066 {
1067         const int base_rq = cfqd->cfq_slice_async_rq;
1068
1069         WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1070
1071         return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1072 }
1073
1074 /*
1075  * scheduler run of queue, if there are requests pending and no one in the
1076  * driver that will restart queueing
1077  */
1078 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
1079 {
1080         if (!cfqd->rq_in_driver && cfq_pending_requests(cfqd))
1081                 kblockd_schedule_work(&cfqd->unplug_work);
1082 }
1083
1084 /*
1085  * get next queue for service
1086  */
1087 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1088 {
1089         unsigned long now = jiffies;
1090         struct cfq_queue *cfqq;
1091
1092         cfqq = cfqd->active_queue;
1093         if (!cfqq)
1094                 goto new_queue;
1095
1096         if (cfq_cfqq_expired(cfqq))
1097                 goto new_queue;
1098
1099         /*
1100          * slice has expired
1101          */
1102         if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1103                 goto expire;
1104
1105         /*
1106          * if queue has requests, dispatch one. if not, check if
1107          * enough slice is left to wait for one
1108          */
1109         if (!RB_EMPTY(&cfqq->sort_list))
1110                 goto keep_queue;
1111         else if (!force && cfq_cfqq_class_sync(cfqq) &&
1112                  time_before(now, cfqq->slice_end)) {
1113                 if (cfq_arm_slice_timer(cfqd, cfqq))
1114                         return NULL;
1115         }
1116
1117 expire:
1118         cfq_slice_expired(cfqd, 0);
1119 new_queue:
1120         cfqq = cfq_set_active_queue(cfqd);
1121 keep_queue:
1122         return cfqq;
1123 }
1124
1125 static int
1126 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1127                         int max_dispatch)
1128 {
1129         int dispatched = 0;
1130
1131         BUG_ON(RB_EMPTY(&cfqq->sort_list));
1132
1133         do {
1134                 struct cfq_rq *crq;
1135
1136                 /*
1137                  * follow expired path, else get first next available
1138                  */
1139                 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1140                         crq = cfqq->next_crq;
1141
1142                 /*
1143                  * finally, insert request into driver dispatch list
1144                  */
1145                 cfq_dispatch_sort(cfqd->queue, crq);
1146
1147                 cfqd->dispatch_slice++;
1148                 dispatched++;
1149
1150                 if (!cfqd->active_cic) {
1151                         atomic_inc(&crq->io_context->ioc->refcount);
1152                         cfqd->active_cic = crq->io_context;
1153                 }
1154
1155                 if (RB_EMPTY(&cfqq->sort_list))
1156                         break;
1157
1158         } while (dispatched < max_dispatch);
1159
1160         /*
1161          * if slice end isn't set yet, set it. if at least one request was
1162          * sync, use the sync time slice value
1163          */
1164         if (!cfqq->slice_end)
1165                 cfq_set_prio_slice(cfqd, cfqq);
1166
1167         /*
1168          * expire an async queue immediately if it has used up its slice. idle
1169          * queue always expire after 1 dispatch round.
1170          */
1171         if ((!cfq_cfqq_sync(cfqq) &&
1172             cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1173             cfq_class_idle(cfqq))
1174                 cfq_slice_expired(cfqd, 0);
1175
1176         return dispatched;
1177 }
1178
1179 static int
1180 cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1181 {
1182         struct cfq_data *cfqd = q->elevator->elevator_data;
1183         struct cfq_queue *cfqq;
1184
1185         if (!cfqd->busy_queues)
1186                 return 0;
1187
1188         cfqq = cfq_select_queue(cfqd, force);
1189         if (cfqq) {
1190                 cfq_clear_cfqq_must_dispatch(cfqq);
1191                 cfq_clear_cfqq_wait_request(cfqq);
1192                 del_timer(&cfqd->idle_slice_timer);
1193
1194                 if (cfq_class_idle(cfqq))
1195                         max_dispatch = 1;
1196
1197                 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1198         }
1199
1200         return 0;
1201 }
1202
1203 static inline void cfq_account_dispatch(struct cfq_rq *crq)
1204 {
1205         struct cfq_queue *cfqq = crq->cfq_queue;
1206         struct cfq_data *cfqd = cfqq->cfqd;
1207
1208         if (unlikely(!blk_fs_request(crq->request)))
1209                 return;
1210
1211         /*
1212          * accounted bit is necessary since some drivers will call
1213          * elv_next_request() many times for the same request (eg ide)
1214          */
1215         if (cfq_crq_in_driver(crq))
1216                 return;
1217
1218         cfq_mark_crq_in_driver(crq);
1219         cfqd->rq_in_driver++;
1220 }
1221
1222 static inline void
1223 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1224 {
1225         struct cfq_data *cfqd = cfqq->cfqd;
1226         unsigned long now;
1227
1228         if (!cfq_crq_in_driver(crq))
1229                 return;
1230
1231         now = jiffies;
1232
1233         WARN_ON(!cfqd->rq_in_driver);
1234         cfqd->rq_in_driver--;
1235
1236         if (!cfq_class_idle(cfqq))
1237                 cfqd->last_end_request = now;
1238
1239         if (!cfq_cfqq_dispatched(cfqq)) {
1240                 if (cfq_cfqq_on_rr(cfqq)) {
1241                         cfqq->service_last = now;
1242                         cfq_resort_rr_list(cfqq, 0);
1243                 }
1244                 if (cfq_cfqq_expired(cfqq)) {
1245                         __cfq_slice_expired(cfqd, cfqq, 0);
1246                         cfq_schedule_dispatch(cfqd);
1247                 }
1248         }
1249
1250         if (cfq_crq_is_sync(crq))
1251                 crq->io_context->last_end_request = now;
1252 }
1253
1254 static struct request *cfq_next_request(request_queue_t *q)
1255 {
1256         struct cfq_data *cfqd = q->elevator->elevator_data;
1257         struct request *rq;
1258
1259         if (!list_empty(&q->queue_head)) {
1260                 struct cfq_rq *crq;
1261 dispatch:
1262                 rq = list_entry_rq(q->queue_head.next);
1263
1264                 crq = RQ_DATA(rq);
1265                 if (crq) {
1266                         struct cfq_queue *cfqq = crq->cfq_queue;
1267
1268                         /*
1269                          * if idle window is disabled, allow queue buildup
1270                          */
1271                         if (!cfq_crq_in_driver(crq) &&
1272                             !cfq_cfqq_idle_window(cfqq) &&
1273                             cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1274                                 return NULL;
1275
1276                         cfq_remove_merge_hints(q, crq);
1277                         cfq_account_dispatch(crq);
1278                 }
1279
1280                 return rq;
1281         }
1282
1283         if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1284                 goto dispatch;
1285
1286         return NULL;
1287 }
1288
1289 /*
1290  * task holds one reference to the queue, dropped when task exits. each crq
1291  * in-flight on this queue also holds a reference, dropped when crq is freed.
1292  *
1293  * queue lock must be held here.
1294  */
1295 static void cfq_put_queue(struct cfq_queue *cfqq)
1296 {
1297         struct cfq_data *cfqd = cfqq->cfqd;
1298
1299         BUG_ON(atomic_read(&cfqq->ref) <= 0);
1300
1301         if (!atomic_dec_and_test(&cfqq->ref))
1302                 return;
1303
1304         BUG_ON(rb_first(&cfqq->sort_list));
1305         BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1306         BUG_ON(cfq_cfqq_on_rr(cfqq));
1307
1308         if (unlikely(cfqd->active_queue == cfqq)) {
1309                 __cfq_slice_expired(cfqd, cfqq, 0);
1310                 cfq_schedule_dispatch(cfqd);
1311         }
1312
1313         cfq_put_cfqd(cfqq->cfqd);
1314
1315         /*
1316          * it's on the empty list and still hashed
1317          */
1318         list_del(&cfqq->cfq_list);
1319         hlist_del(&cfqq->cfq_hash);
1320         kmem_cache_free(cfq_pool, cfqq);
1321 }
1322
1323 static inline struct cfq_queue *
1324 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1325                     const int hashval)
1326 {
1327         struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1328         struct hlist_node *entry, *next;
1329
1330         hlist_for_each_safe(entry, next, hash_list) {
1331                 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1332                 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1333
1334                 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1335                         return __cfqq;
1336         }
1337
1338         return NULL;
1339 }
1340
1341 static struct cfq_queue *
1342 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1343 {
1344         return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1345 }
1346
1347 static void cfq_free_io_context(struct cfq_io_context *cic)
1348 {
1349         struct cfq_io_context *__cic;
1350         struct list_head *entry, *next;
1351
1352         list_for_each_safe(entry, next, &cic->list) {
1353                 __cic = list_entry(entry, struct cfq_io_context, list);
1354                 kmem_cache_free(cfq_ioc_pool, __cic);
1355         }
1356
1357         kmem_cache_free(cfq_ioc_pool, cic);
1358 }
1359
1360 /*
1361  * Called with interrupts disabled
1362  */
1363 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1364 {
1365         struct cfq_data *cfqd = cic->cfqq->cfqd;
1366         request_queue_t *q = cfqd->queue;
1367
1368         WARN_ON(!irqs_disabled());
1369
1370         spin_lock(q->queue_lock);
1371
1372         if (unlikely(cic->cfqq == cfqd->active_queue)) {
1373                 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1374                 cfq_schedule_dispatch(cfqd);
1375         }
1376
1377         cfq_put_queue(cic->cfqq);
1378         cic->cfqq = NULL;
1379         spin_unlock(q->queue_lock);
1380 }
1381
1382 /*
1383  * Another task may update the task cic list, if it is doing a queue lookup
1384  * on its behalf. cfq_cic_lock excludes such concurrent updates
1385  */
1386 static void cfq_exit_io_context(struct cfq_io_context *cic)
1387 {
1388         struct cfq_io_context *__cic;
1389         struct list_head *entry;
1390         unsigned long flags;
1391
1392         local_irq_save(flags);
1393
1394         /*
1395          * put the reference this task is holding to the various queues
1396          */
1397         list_for_each(entry, &cic->list) {
1398                 __cic = list_entry(entry, struct cfq_io_context, list);
1399                 cfq_exit_single_io_context(__cic);
1400         }
1401
1402         cfq_exit_single_io_context(cic);
1403         local_irq_restore(flags);
1404 }
1405
1406 static struct cfq_io_context *
1407 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1408 {
1409         struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1410
1411         if (cic) {
1412                 INIT_LIST_HEAD(&cic->list);
1413                 cic->cfqq = NULL;
1414                 cic->key = NULL;
1415                 cic->last_end_request = jiffies;
1416                 cic->ttime_total = 0;
1417                 cic->ttime_samples = 0;
1418                 cic->ttime_mean = 0;
1419                 cic->dtor = cfq_free_io_context;
1420                 cic->exit = cfq_exit_io_context;
1421         }
1422
1423         return cic;
1424 }
1425
1426 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1427 {
1428         struct task_struct *tsk = current;
1429         int ioprio_class;
1430
1431         if (!cfq_cfqq_prio_changed(cfqq))
1432                 return;
1433
1434         ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1435         switch (ioprio_class) {
1436                 default:
1437                         printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1438                 case IOPRIO_CLASS_NONE:
1439                         /*
1440                          * no prio set, place us in the middle of the BE classes
1441                          */
1442                         cfqq->ioprio = task_nice_ioprio(tsk);
1443                         cfqq->ioprio_class = IOPRIO_CLASS_BE;
1444                         break;
1445                 case IOPRIO_CLASS_RT:
1446                         cfqq->ioprio = task_ioprio(tsk);
1447                         cfqq->ioprio_class = IOPRIO_CLASS_RT;
1448                         break;
1449                 case IOPRIO_CLASS_BE:
1450                         cfqq->ioprio = task_ioprio(tsk);
1451                         cfqq->ioprio_class = IOPRIO_CLASS_BE;
1452                         break;
1453                 case IOPRIO_CLASS_IDLE:
1454                         cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1455                         cfqq->ioprio = 7;
1456                         cfq_clear_cfqq_idle_window(cfqq);
1457                         break;
1458         }
1459
1460         /*
1461          * keep track of original prio settings in case we have to temporarily
1462          * elevate the priority of this queue
1463          */
1464         cfqq->org_ioprio = cfqq->ioprio;
1465         cfqq->org_ioprio_class = cfqq->ioprio_class;
1466
1467         if (cfq_cfqq_on_rr(cfqq))
1468                 cfq_resort_rr_list(cfqq, 0);
1469
1470         cfq_clear_cfqq_prio_changed(cfqq);
1471 }
1472
1473 static inline void changed_ioprio(struct cfq_queue *cfqq)
1474 {
1475         if (cfqq) {
1476                 struct cfq_data *cfqd = cfqq->cfqd;
1477
1478                 spin_lock(cfqd->queue->queue_lock);
1479                 cfq_mark_cfqq_prio_changed(cfqq);
1480                 cfq_init_prio_data(cfqq);
1481                 spin_unlock(cfqd->queue->queue_lock);
1482         }
1483 }
1484
1485 /*
1486  * callback from sys_ioprio_set, irqs are disabled
1487  */
1488 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1489 {
1490         struct cfq_io_context *cic = ioc->cic;
1491
1492         changed_ioprio(cic->cfqq);
1493
1494         list_for_each_entry(cic, &cic->list, list)
1495                 changed_ioprio(cic->cfqq);
1496
1497         return 0;
1498 }
1499
1500 static struct cfq_queue *
1501 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1502               int gfp_mask)
1503 {
1504         const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1505         struct cfq_queue *cfqq, *new_cfqq = NULL;
1506
1507 retry:
1508         cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1509
1510         if (!cfqq) {
1511                 if (new_cfqq) {
1512                         cfqq = new_cfqq;
1513                         new_cfqq = NULL;
1514                 } else if (gfp_mask & __GFP_WAIT) {
1515                         spin_unlock_irq(cfqd->queue->queue_lock);
1516                         new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1517                         spin_lock_irq(cfqd->queue->queue_lock);
1518                         goto retry;
1519                 } else {
1520                         cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1521                         if (!cfqq)
1522                                 goto out;
1523                 }
1524
1525                 memset(cfqq, 0, sizeof(*cfqq));
1526
1527                 INIT_HLIST_NODE(&cfqq->cfq_hash);
1528                 INIT_LIST_HEAD(&cfqq->cfq_list);
1529                 RB_CLEAR_ROOT(&cfqq->sort_list);
1530                 INIT_LIST_HEAD(&cfqq->fifo);
1531
1532                 cfqq->key = key;
1533                 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1534                 atomic_set(&cfqq->ref, 0);
1535                 cfqq->cfqd = cfqd;
1536                 atomic_inc(&cfqd->ref);
1537                 cfqq->service_last = 0;
1538                 /*
1539                  * set ->slice_left to allow preemption for a new process
1540                  */
1541                 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1542                 cfq_mark_cfqq_idle_window(cfqq);
1543                 cfq_mark_cfqq_prio_changed(cfqq);
1544                 cfq_init_prio_data(cfqq);
1545         }
1546
1547         if (new_cfqq)
1548                 kmem_cache_free(cfq_pool, new_cfqq);
1549
1550         atomic_inc(&cfqq->ref);
1551 out:
1552         WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1553         return cfqq;
1554 }
1555
1556 /*
1557  * Setup general io context and cfq io context. There can be several cfq
1558  * io contexts per general io context, if this process is doing io to more
1559  * than one device managed by cfq. Note that caller is holding a reference to
1560  * cfqq, so we don't need to worry about it disappearing
1561  */
1562 static struct cfq_io_context *
1563 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1564 {
1565         struct io_context *ioc = NULL;
1566         struct cfq_io_context *cic;
1567
1568         might_sleep_if(gfp_mask & __GFP_WAIT);
1569
1570         ioc = get_io_context(gfp_mask);
1571         if (!ioc)
1572                 return NULL;
1573
1574         if ((cic = ioc->cic) == NULL) {
1575                 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1576
1577                 if (cic == NULL)
1578                         goto err;
1579
1580                 /*
1581                  * manually increment generic io_context usage count, it
1582                  * cannot go away since we are already holding one ref to it
1583                  */
1584                 ioc->cic = cic;
1585                 ioc->set_ioprio = cfq_ioc_set_ioprio;
1586                 cic->ioc = ioc;
1587                 cic->key = cfqd;
1588                 atomic_inc(&cfqd->ref);
1589         } else {
1590                 struct cfq_io_context *__cic;
1591
1592                 /*
1593                  * the first cic on the list is actually the head itself
1594                  */
1595                 if (cic->key == cfqd)
1596                         goto out;
1597
1598                 /*
1599                  * cic exists, check if we already are there. linear search
1600                  * should be ok here, the list will usually not be more than
1601                  * 1 or a few entries long
1602                  */
1603                 list_for_each_entry(__cic, &cic->list, list) {
1604                         /*
1605                          * this process is already holding a reference to
1606                          * this queue, so no need to get one more
1607                          */
1608                         if (__cic->key == cfqd) {
1609                                 cic = __cic;
1610                                 goto out;
1611                         }
1612                 }
1613
1614                 /*
1615                  * nope, process doesn't have a cic assoicated with this
1616                  * cfqq yet. get a new one and add to list
1617                  */
1618                 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1619                 if (__cic == NULL)
1620                         goto err;
1621
1622                 __cic->ioc = ioc;
1623                 __cic->key = cfqd;
1624                 atomic_inc(&cfqd->ref);
1625                 list_add(&__cic->list, &cic->list);
1626                 cic = __cic;
1627         }
1628
1629 out:
1630         return cic;
1631 err:
1632         put_io_context(ioc);
1633         return NULL;
1634 }
1635
1636 static void
1637 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1638 {
1639         unsigned long elapsed, ttime;
1640
1641         /*
1642          * if this context already has stuff queued, thinktime is from
1643          * last queue not last end
1644          */
1645 #if 0
1646         if (time_after(cic->last_end_request, cic->last_queue))
1647                 elapsed = jiffies - cic->last_end_request;
1648         else
1649                 elapsed = jiffies - cic->last_queue;
1650 #else
1651                 elapsed = jiffies - cic->last_end_request;
1652 #endif
1653
1654         ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1655
1656         cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1657         cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1658         cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1659 }
1660
1661 #define sample_valid(samples)   ((samples) > 80)
1662
1663 /*
1664  * Disable idle window if the process thinks too long or seeks so much that
1665  * it doesn't matter
1666  */
1667 static void
1668 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1669                        struct cfq_io_context *cic)
1670 {
1671         int enable_idle = cfq_cfqq_idle_window(cfqq);
1672
1673         if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1674                 enable_idle = 0;
1675         else if (sample_valid(cic->ttime_samples)) {
1676                 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1677                         enable_idle = 0;
1678                 else
1679                         enable_idle = 1;
1680         }
1681
1682         if (enable_idle)
1683                 cfq_mark_cfqq_idle_window(cfqq);
1684         else
1685                 cfq_clear_cfqq_idle_window(cfqq);
1686 }
1687
1688
1689 /*
1690  * Check if new_cfqq should preempt the currently active queue. Return 0 for
1691  * no or if we aren't sure, a 1 will cause a preempt.
1692  */
1693 static int
1694 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1695                    struct cfq_rq *crq)
1696 {
1697         struct cfq_queue *cfqq = cfqd->active_queue;
1698
1699         if (cfq_class_idle(new_cfqq))
1700                 return 0;
1701
1702         if (!cfqq)
1703                 return 1;
1704
1705         if (cfq_class_idle(cfqq))
1706                 return 1;
1707         if (!cfq_cfqq_wait_request(new_cfqq))
1708                 return 0;
1709         /*
1710          * if it doesn't have slice left, forget it
1711          */
1712         if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1713                 return 0;
1714         if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1715                 return 1;
1716
1717         return 0;
1718 }
1719
1720 /*
1721  * cfqq preempts the active queue. if we allowed preempt with no slice left,
1722  * let it have half of its nominal slice.
1723  */
1724 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1725 {
1726         struct cfq_queue *__cfqq, *next;
1727
1728         list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1729                 cfq_resort_rr_list(__cfqq, 1);
1730
1731         if (!cfqq->slice_left)
1732                 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1733
1734         cfqq->slice_end = cfqq->slice_left + jiffies;
1735         __cfq_slice_expired(cfqd, cfqq, 1);
1736         __cfq_set_active_queue(cfqd, cfqq);
1737 }
1738
1739 /*
1740  * should really be a ll_rw_blk.c helper
1741  */
1742 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1743 {
1744         request_queue_t *q = cfqd->queue;
1745
1746         if (!blk_queue_plugged(q))
1747                 q->request_fn(q);
1748         else
1749                 __generic_unplug_device(q);
1750 }
1751
1752 /*
1753  * Called when a new fs request (crq) is added (to cfqq). Check if there's
1754  * something we should do about it
1755  */
1756 static void
1757 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1758                  struct cfq_rq *crq)
1759 {
1760         const int sync = cfq_crq_is_sync(crq);
1761
1762         cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1763
1764         if (sync) {
1765                 struct cfq_io_context *cic = crq->io_context;
1766
1767                 cfq_update_io_thinktime(cfqd, cic);
1768                 cfq_update_idle_window(cfqd, cfqq, cic);
1769
1770                 cic->last_queue = jiffies;
1771         }
1772
1773         if (cfqq == cfqd->active_queue) {
1774                 /*
1775                  * if we are waiting for a request for this queue, let it rip
1776                  * immediately and flag that we must not expire this queue
1777                  * just now
1778                  */
1779                 if (cfq_cfqq_wait_request(cfqq)) {
1780                         cfq_mark_cfqq_must_dispatch(cfqq);
1781                         del_timer(&cfqd->idle_slice_timer);
1782                         cfq_start_queueing(cfqd, cfqq);
1783                 }
1784         } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1785                 /*
1786                  * not the active queue - expire current slice if it is
1787                  * idle and has expired it's mean thinktime or this new queue
1788                  * has some old slice time left and is of higher priority
1789                  */
1790                 cfq_preempt_queue(cfqd, cfqq);
1791                 cfq_mark_cfqq_must_dispatch(cfqq);
1792                 cfq_start_queueing(cfqd, cfqq);
1793         }
1794 }
1795
1796 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1797 {
1798         struct cfq_rq *crq = RQ_DATA(rq);
1799         struct cfq_queue *cfqq = crq->cfq_queue;
1800
1801         cfq_init_prio_data(cfqq);
1802
1803         cfq_add_crq_rb(crq);
1804
1805         list_add_tail(&rq->queuelist, &cfqq->fifo);
1806
1807         if (rq_mergeable(rq)) {
1808                 cfq_add_crq_hash(cfqd, crq);
1809
1810                 if (!cfqd->queue->last_merge)
1811                         cfqd->queue->last_merge = rq;
1812         }
1813
1814         cfq_crq_enqueued(cfqd, cfqq, crq);
1815 }
1816
1817 static void
1818 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1819 {
1820         struct cfq_data *cfqd = q->elevator->elevator_data;
1821
1822         switch (where) {
1823                 case ELEVATOR_INSERT_BACK:
1824                         while (cfq_dispatch_requests(q, INT_MAX, 1))
1825                                 ;
1826                         list_add_tail(&rq->queuelist, &q->queue_head);
1827                         /*
1828                          * If we were idling with pending requests on
1829                          * inactive cfqqs, force dispatching will
1830                          * remove the idle timer and the queue won't
1831                          * be kicked by __make_request() afterward.
1832                          * Kick it here.
1833                          */
1834                         cfq_schedule_dispatch(cfqd);
1835                         break;
1836                 case ELEVATOR_INSERT_FRONT:
1837                         list_add(&rq->queuelist, &q->queue_head);
1838                         break;
1839                 case ELEVATOR_INSERT_SORT:
1840                         BUG_ON(!blk_fs_request(rq));
1841                         cfq_enqueue(cfqd, rq);
1842                         break;
1843                 default:
1844                         printk("%s: bad insert point %d\n", __FUNCTION__,where);
1845                         return;
1846         }
1847 }
1848
1849 static inline int cfq_pending_requests(struct cfq_data *cfqd)
1850 {
1851         return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
1852 }
1853
1854 static int cfq_queue_empty(request_queue_t *q)
1855 {
1856         struct cfq_data *cfqd = q->elevator->elevator_data;
1857
1858         return !cfq_pending_requests(cfqd);
1859 }
1860
1861 static void cfq_completed_request(request_queue_t *q, struct request *rq)
1862 {
1863         struct cfq_rq *crq = RQ_DATA(rq);
1864         struct cfq_queue *cfqq;
1865
1866         if (unlikely(!blk_fs_request(rq)))
1867                 return;
1868
1869         cfqq = crq->cfq_queue;
1870
1871         if (cfq_crq_in_flight(crq)) {
1872                 const int sync = cfq_crq_is_sync(crq);
1873
1874                 WARN_ON(!cfqq->on_dispatch[sync]);
1875                 cfqq->on_dispatch[sync]--;
1876         }
1877
1878         cfq_account_completion(cfqq, crq);
1879 }
1880
1881 static struct request *
1882 cfq_former_request(request_queue_t *q, struct request *rq)
1883 {
1884         struct cfq_rq *crq = RQ_DATA(rq);
1885         struct rb_node *rbprev = rb_prev(&crq->rb_node);
1886
1887         if (rbprev)
1888                 return rb_entry_crq(rbprev)->request;
1889
1890         return NULL;
1891 }
1892
1893 static struct request *
1894 cfq_latter_request(request_queue_t *q, struct request *rq)
1895 {
1896         struct cfq_rq *crq = RQ_DATA(rq);
1897         struct rb_node *rbnext = rb_next(&crq->rb_node);
1898
1899         if (rbnext)
1900                 return rb_entry_crq(rbnext)->request;
1901
1902         return NULL;
1903 }
1904
1905 /*
1906  * we temporarily boost lower priority queues if they are holding fs exclusive
1907  * resources. they are boosted to normal prio (CLASS_BE/4)
1908  */
1909 static void cfq_prio_boost(struct cfq_queue *cfqq)
1910 {
1911         const int ioprio_class = cfqq->ioprio_class;
1912         const int ioprio = cfqq->ioprio;
1913
1914         if (has_fs_excl()) {
1915                 /*
1916                  * boost idle prio on transactions that would lock out other
1917                  * users of the filesystem
1918                  */
1919                 if (cfq_class_idle(cfqq))
1920                         cfqq->ioprio_class = IOPRIO_CLASS_BE;
1921                 if (cfqq->ioprio > IOPRIO_NORM)
1922                         cfqq->ioprio = IOPRIO_NORM;
1923         } else {
1924                 /*
1925                  * check if we need to unboost the queue
1926                  */
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;
1931         }
1932
1933         /*
1934          * refile between round-robin lists if we moved the priority class
1935          */
1936         if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1937             cfq_cfqq_on_rr(cfqq))
1938                 cfq_resort_rr_list(cfqq, 0);
1939 }
1940
1941 static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1942 {
1943         if (rw == READ || process_sync(task))
1944                 return task->pid;
1945
1946         return CFQ_KEY_ASYNC;
1947 }
1948
1949 static inline int
1950 __cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1951                 struct task_struct *task, int rw)
1952 {
1953 #if 1
1954         if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1955             !cfq_cfqq_must_alloc_slice) {
1956                 cfq_mark_cfqq_must_alloc_slice(cfqq);
1957                 return ELV_MQUEUE_MUST;
1958         }
1959
1960         return ELV_MQUEUE_MAY;
1961 #else
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;
1967
1968                 /*
1969                  * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1970                  * can quickly flood the queue with writes from a single task
1971                  */
1972                 if (rw == READ || !cfq_cfqq_must_alloc_slice) {
1973                         cfq_mark_cfqq_must_alloc_slice(cfqq);
1974                         return ELV_MQUEUE_MUST;
1975                 }
1976
1977                 return ELV_MQUEUE_MAY;
1978         }
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;
1984
1985                 if (ioc && ioc->nr_batch_requests)
1986                         ret = ELV_MQUEUE_MAY;
1987
1988                 put_io_context(ioc);
1989                 return ret;
1990         }
1991
1992         return ELV_MQUEUE_MAY;
1993 #endif
1994 }
1995
1996 static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1997 {
1998         struct cfq_data *cfqd = q->elevator->elevator_data;
1999         struct task_struct *tsk = current;
2000         struct cfq_queue *cfqq;
2001
2002         /*
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'
2006          * if that fails
2007          */
2008         cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
2009         if (cfqq) {
2010                 cfq_init_prio_data(cfqq);
2011                 cfq_prio_boost(cfqq);
2012
2013                 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
2014         }
2015
2016         return ELV_MQUEUE_MAY;
2017 }
2018
2019 static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
2020 {
2021         struct cfq_data *cfqd = q->elevator->elevator_data;
2022         struct request_list *rl = &q->rq;
2023
2024         if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
2025                 smp_mb();
2026                 if (waitqueue_active(&rl->wait[READ]))
2027                         wake_up(&rl->wait[READ]);
2028         }
2029
2030         if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
2031                 smp_mb();
2032                 if (waitqueue_active(&rl->wait[WRITE]))
2033                         wake_up(&rl->wait[WRITE]);
2034         }
2035 }
2036
2037 /*
2038  * queue lock held here
2039  */
2040 static void cfq_put_request(request_queue_t *q, struct request *rq)
2041 {
2042         struct cfq_data *cfqd = q->elevator->elevator_data;
2043         struct cfq_rq *crq = RQ_DATA(rq);
2044
2045         if (crq) {
2046                 struct cfq_queue *cfqq = crq->cfq_queue;
2047                 const int rw = rq_data_dir(rq);
2048
2049                 BUG_ON(!cfqq->allocated[rw]);
2050                 cfqq->allocated[rw]--;
2051
2052                 put_io_context(crq->io_context->ioc);
2053
2054                 mempool_free(crq, cfqd->crq_pool);
2055                 rq->elevator_private = NULL;
2056
2057                 cfq_check_waiters(q, cfqq);
2058                 cfq_put_queue(cfqq);
2059         }
2060 }
2061
2062 /*
2063  * Allocate cfq data structures associated with this request.
2064  */
2065 static int
2066 cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
2067                 int gfp_mask)
2068 {
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;
2075         struct cfq_rq *crq;
2076         unsigned long flags;
2077
2078         might_sleep_if(gfp_mask & __GFP_WAIT);
2079
2080         cic = cfq_get_io_context(cfqd, key, gfp_mask);
2081
2082         spin_lock_irqsave(q->queue_lock, flags);
2083
2084         if (!cic)
2085                 goto queue_fail;
2086
2087         if (!cic->cfqq) {
2088                 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
2089                 if (!cfqq)
2090                         goto queue_fail;
2091
2092                 cic->cfqq = cfqq;
2093         } else
2094                 cfqq = cic->cfqq;
2095
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);
2101
2102         crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2103         if (crq) {
2104                 RB_CLEAR(&crq->rb_node);
2105                 crq->rb_key = 0;
2106                 crq->request = rq;
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);
2113
2114                 if (rw == READ || process_sync(tsk))
2115                         cfq_mark_crq_is_sync(crq);
2116                 else
2117                         cfq_clear_crq_is_sync(crq);
2118
2119                 rq->elevator_private = crq;
2120                 return 0;
2121         }
2122
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);
2128 queue_fail:
2129         if (cic)
2130                 put_io_context(cic->ioc);
2131         /*
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
2135          */
2136         cfqd->rq_starved = 1;
2137         cfq_schedule_dispatch(cfqd);
2138         spin_unlock_irqrestore(q->queue_lock, flags);
2139         return 1;
2140 }
2141
2142 static void cfq_kick_queue(void *data)
2143 {
2144         request_queue_t *q = data;
2145         struct cfq_data *cfqd = q->elevator->elevator_data;
2146         unsigned long flags;
2147
2148         spin_lock_irqsave(q->queue_lock, flags);
2149
2150         if (cfqd->rq_starved) {
2151                 struct request_list *rl = &q->rq;
2152
2153                 /*
2154                  * we aren't guaranteed to get a request after this, but we
2155                  * have to be opportunistic
2156                  */
2157                 smp_mb();
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]);
2162         }
2163
2164         blk_remove_plug(q);
2165         q->request_fn(q);
2166         spin_unlock_irqrestore(q->queue_lock, flags);
2167 }
2168
2169 /*
2170  * Timer running if the active_queue is currently idling inside its time slice
2171  */
2172 static void cfq_idle_slice_timer(unsigned long data)
2173 {
2174         struct cfq_data *cfqd = (struct cfq_data *) data;
2175         struct cfq_queue *cfqq;
2176         unsigned long flags;
2177
2178         spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2179
2180         if ((cfqq = cfqd->active_queue) != NULL) {
2181                 unsigned long now = jiffies;
2182
2183                 /*
2184                  * expired
2185                  */
2186                 if (time_after(now, cfqq->slice_end))
2187                         goto expire;
2188
2189                 /*
2190                  * only expire and reinvoke request handler, if there are
2191                  * other queues with pending requests
2192                  */
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);
2196                         goto out_cont;
2197                 }
2198
2199                 /*
2200                  * not expired and it has a request pending, let it dispatch
2201                  */
2202                 if (!RB_EMPTY(&cfqq->sort_list)) {
2203                         cfq_mark_cfqq_must_dispatch(cfqq);
2204                         goto out_kick;
2205                 }
2206         }
2207 expire:
2208         cfq_slice_expired(cfqd, 0);
2209 out_kick:
2210         cfq_schedule_dispatch(cfqd);
2211 out_cont:
2212         spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2213 }
2214
2215 /*
2216  * Timer running if an idle class queue is waiting for service
2217  */
2218 static void cfq_idle_class_timer(unsigned long data)
2219 {
2220         struct cfq_data *cfqd = (struct cfq_data *) data;
2221         unsigned long flags, end;
2222
2223         spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2224
2225         /*
2226          * race with a non-idle queue, reset timer
2227          */
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);
2232         } else
2233                 cfq_schedule_dispatch(cfqd);
2234
2235         spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2236 }
2237
2238 static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2239 {
2240         del_timer_sync(&cfqd->idle_slice_timer);
2241         del_timer_sync(&cfqd->idle_class_timer);
2242         blk_sync_queue(cfqd->queue);
2243 }
2244
2245 static void cfq_put_cfqd(struct cfq_data *cfqd)
2246 {
2247         request_queue_t *q = cfqd->queue;
2248
2249         if (!atomic_dec_and_test(&cfqd->ref))
2250                 return;
2251
2252         blk_put_queue(q);
2253
2254         cfq_shutdown_timer_wq(cfqd);
2255         q->elevator->elevator_data = NULL;
2256
2257         mempool_destroy(cfqd->crq_pool);
2258         kfree(cfqd->crq_hash);
2259         kfree(cfqd->cfq_hash);
2260         kfree(cfqd);
2261 }
2262
2263 static void cfq_exit_queue(elevator_t *e)
2264 {
2265         struct cfq_data *cfqd = e->elevator_data;
2266
2267         cfq_shutdown_timer_wq(cfqd);
2268         cfq_put_cfqd(cfqd);
2269 }
2270
2271 static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2272 {
2273         struct cfq_data *cfqd;
2274         int i;
2275
2276         cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2277         if (!cfqd)
2278                 return -ENOMEM;
2279
2280         memset(cfqd, 0, sizeof(*cfqd));
2281
2282         for (i = 0; i < CFQ_PRIO_LISTS; i++)
2283                 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2284
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);
2289
2290         cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2291         if (!cfqd->crq_hash)
2292                 goto out_crqhash;
2293
2294         cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2295         if (!cfqd->cfq_hash)
2296                 goto out_cfqhash;
2297
2298         cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2299         if (!cfqd->crq_pool)
2300                 goto out_crqpool;
2301
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]);
2306
2307         e->elevator_data = cfqd;
2308
2309         cfqd->queue = q;
2310         atomic_inc(&q->refcnt);
2311
2312         cfqd->max_queued = q->nr_requests / 4;
2313         q->nr_batching = cfq_queued;
2314
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;
2318
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;
2322
2323         INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2324
2325         atomic_set(&cfqd->ref, 1);
2326
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;
2338
2339         return 0;
2340 out_crqpool:
2341         kfree(cfqd->cfq_hash);
2342 out_cfqhash:
2343         kfree(cfqd->crq_hash);
2344 out_crqhash:
2345         kfree(cfqd);
2346         return -ENOMEM;
2347 }
2348
2349 static void cfq_slab_kill(void)
2350 {
2351         if (crq_pool)
2352                 kmem_cache_destroy(crq_pool);
2353         if (cfq_pool)
2354                 kmem_cache_destroy(cfq_pool);
2355         if (cfq_ioc_pool)
2356                 kmem_cache_destroy(cfq_ioc_pool);
2357 }
2358
2359 static int __init cfq_slab_setup(void)
2360 {
2361         crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2362                                         NULL, NULL);
2363         if (!crq_pool)
2364                 goto fail;
2365
2366         cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2367                                         NULL, NULL);
2368         if (!cfq_pool)
2369                 goto fail;
2370
2371         cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2372                         sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2373         if (!cfq_ioc_pool)
2374                 goto fail;
2375
2376         return 0;
2377 fail:
2378         cfq_slab_kill();
2379         return -ENOMEM;
2380 }
2381
2382 /*
2383  * sysfs parts below -->
2384  */
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);
2389 };
2390
2391 static ssize_t
2392 cfq_var_show(unsigned int var, char *page)
2393 {
2394         return sprintf(page, "%d\n", var);
2395 }
2396
2397 static ssize_t
2398 cfq_var_store(unsigned int *var, const char *page, size_t count)
2399 {
2400         char *p = (char *) page;
2401
2402         *var = simple_strtoul(p, &p, 10);
2403         return count;
2404 }
2405
2406 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)                            \
2407 static ssize_t __FUNC(struct cfq_data *cfqd, char *page)                \
2408 {                                                                       \
2409         unsigned int __data = __VAR;                                    \
2410         if (__CONV)                                                     \
2411                 __data = jiffies_to_msecs(__data);                      \
2412         return cfq_var_show(__data, (page));                            \
2413 }
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
2426
2427 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)                 \
2428 static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count)    \
2429 {                                                                       \
2430         unsigned int __data;                                            \
2431         int ret = cfq_var_store(&__data, (page), count);                \
2432         if (__data < (MIN))                                             \
2433                 __data = (MIN);                                         \
2434         else if (__data > (MAX))                                        \
2435                 __data = (MAX);                                         \
2436         if (__CONV)                                                     \
2437                 *(__PTR) = msecs_to_jiffies(__data);                    \
2438         else                                                            \
2439                 *(__PTR) = __data;                                      \
2440         return ret;                                                     \
2441 }
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
2454
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,
2459 };
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,
2464 };
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,
2469 };
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,
2474 };
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,
2479 };
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,
2484 };
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,
2489 };
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,
2494 };
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,
2499 };
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,
2504 };
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,
2509 };
2510
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,
2523         NULL,
2524 };
2525
2526 #define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2527
2528 static ssize_t
2529 cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2530 {
2531         elevator_t *e = container_of(kobj, elevator_t, kobj);
2532         struct cfq_fs_entry *entry = to_cfq(attr);
2533
2534         if (!entry->show)
2535                 return -EIO;
2536
2537         return entry->show(e->elevator_data, page);
2538 }
2539
2540 static ssize_t
2541 cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2542                const char *page, size_t length)
2543 {
2544         elevator_t *e = container_of(kobj, elevator_t, kobj);
2545         struct cfq_fs_entry *entry = to_cfq(attr);
2546
2547         if (!entry->store)
2548                 return -EIO;
2549
2550         return entry->store(e->elevator_data, page, length);
2551 }
2552
2553 static struct sysfs_ops cfq_sysfs_ops = {
2554         .show   = cfq_attr_show,
2555         .store  = cfq_attr_store,
2556 };
2557
2558 static struct kobj_type cfq_ktype = {
2559         .sysfs_ops      = &cfq_sysfs_ops,
2560         .default_attrs  = default_attrs,
2561 };
2562
2563 static struct elevator_type iosched_cfq = {
2564         .ops = {
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,
2582         },
2583         .elevator_ktype =       &cfq_ktype,
2584         .elevator_name =        "cfq",
2585         .elevator_owner =       THIS_MODULE,
2586 };
2587
2588 static int __init cfq_init(void)
2589 {
2590         int ret;
2591
2592         /*
2593          * could be 0 on HZ < 1000 setups
2594          */
2595         if (!cfq_slice_async)
2596                 cfq_slice_async = 1;
2597         if (!cfq_slice_idle)
2598                 cfq_slice_idle = 1;
2599
2600         if (cfq_slab_setup())
2601                 return -ENOMEM;
2602
2603         ret = elv_register(&iosched_cfq);
2604         if (ret)
2605                 cfq_slab_kill();
2606
2607         return ret;
2608 }
2609
2610 static void __exit cfq_exit(void)
2611 {
2612         struct task_struct *g, *p;
2613         unsigned long flags;
2614
2615         read_lock_irqsave(&tasklist_lock, flags);
2616
2617         /*
2618          * iterate each process in the system, removing our io_context
2619          */
2620         do_each_thread(g, p) {
2621                 struct io_context *ioc = p->io_context;
2622
2623                 if (ioc && ioc->cic) {
2624                         ioc->cic->exit(ioc->cic);
2625                         cfq_free_io_context(ioc->cic);
2626                         ioc->cic = NULL;
2627                 }
2628         } while_each_thread(g, p);
2629
2630         read_unlock_irqrestore(&tasklist_lock, flags);
2631
2632         cfq_slab_kill();
2633         elv_unregister(&iosched_cfq);
2634 }
2635
2636 module_init(cfq_init);
2637 module_exit(cfq_exit);
2638
2639 MODULE_AUTHOR("Jens Axboe");
2640 MODULE_LICENSE("GPL");
2641 MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");