[PATCH] cfq-iosched: fix problem with barriers and max_depth == 1
[linux-2.6.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
304 #define process_sync(tsk)       ((tsk)->flags & PF_SYNCWRITE)
305
306 /*
307  * lots of deadline iosched dupes, can be abstracted later...
308  */
309 static inline void cfq_del_crq_hash(struct cfq_rq *crq)
310 {
311         hlist_del_init(&crq->hash);
312 }
313
314 static void cfq_remove_merge_hints(request_queue_t *q, struct cfq_rq *crq)
315 {
316         cfq_del_crq_hash(crq);
317
318         if (q->last_merge == crq->request)
319                 q->last_merge = NULL;
320 }
321
322 static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
323 {
324         const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
325
326         hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
327 }
328
329 static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
330 {
331         struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
332         struct hlist_node *entry, *next;
333
334         hlist_for_each_safe(entry, next, hash_list) {
335                 struct cfq_rq *crq = list_entry_hash(entry);
336                 struct request *__rq = crq->request;
337
338                 if (!rq_mergeable(__rq)) {
339                         cfq_del_crq_hash(crq);
340                         continue;
341                 }
342
343                 if (rq_hash_key(__rq) == offset)
344                         return __rq;
345         }
346
347         return NULL;
348 }
349
350 static inline int cfq_pending_requests(struct cfq_data *cfqd)
351 {
352         return !list_empty(&cfqd->queue->queue_head) || cfqd->busy_queues;
353 }
354
355 /*
356  * scheduler run of queue, if there are requests pending and no one in the
357  * driver that will restart queueing
358  */
359 static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
360 {
361         if (!cfqd->rq_in_driver && cfq_pending_requests(cfqd))
362                 kblockd_schedule_work(&cfqd->unplug_work);
363 }
364
365 static int cfq_queue_empty(request_queue_t *q)
366 {
367         struct cfq_data *cfqd = q->elevator->elevator_data;
368
369         return !cfq_pending_requests(cfqd);
370 }
371
372 /*
373  * Lifted from AS - choose which of crq1 and crq2 that is best served now.
374  * We choose the request that is closest to the head right now. Distance
375  * behind the head are penalized and only allowed to a certain extent.
376  */
377 static struct cfq_rq *
378 cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
379 {
380         sector_t last, s1, s2, d1 = 0, d2 = 0;
381         int r1_wrap = 0, r2_wrap = 0;   /* requests are behind the disk head */
382         unsigned long back_max;
383
384         if (crq1 == NULL || crq1 == crq2)
385                 return crq2;
386         if (crq2 == NULL)
387                 return crq1;
388         if (cfq_crq_requeued(crq1))
389                 return crq1;
390         if (cfq_crq_requeued(crq2))
391                 return crq2;
392
393         s1 = crq1->request->sector;
394         s2 = crq2->request->sector;
395
396         last = cfqd->last_sector;
397
398         /*
399          * by definition, 1KiB is 2 sectors
400          */
401         back_max = cfqd->cfq_back_max * 2;
402
403         /*
404          * Strict one way elevator _except_ in the case where we allow
405          * short backward seeks which are biased as twice the cost of a
406          * similar forward seek.
407          */
408         if (s1 >= last)
409                 d1 = s1 - last;
410         else if (s1 + back_max >= last)
411                 d1 = (last - s1) * cfqd->cfq_back_penalty;
412         else
413                 r1_wrap = 1;
414
415         if (s2 >= last)
416                 d2 = s2 - last;
417         else if (s2 + back_max >= last)
418                 d2 = (last - s2) * cfqd->cfq_back_penalty;
419         else
420                 r2_wrap = 1;
421
422         /* Found required data */
423         if (!r1_wrap && r2_wrap)
424                 return crq1;
425         else if (!r2_wrap && r1_wrap)
426                 return crq2;
427         else if (r1_wrap && r2_wrap) {
428                 /* both behind the head */
429                 if (s1 <= s2)
430                         return crq1;
431                 else
432                         return crq2;
433         }
434
435         /* Both requests in front of the head */
436         if (d1 < d2)
437                 return crq1;
438         else if (d2 < d1)
439                 return crq2;
440         else {
441                 if (s1 >= s2)
442                         return crq1;
443                 else
444                         return crq2;
445         }
446 }
447
448 /*
449  * would be nice to take fifo expire time into account as well
450  */
451 static struct cfq_rq *
452 cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
453                   struct cfq_rq *last)
454 {
455         struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
456         struct rb_node *rbnext, *rbprev;
457
458         rbnext = NULL;
459         if (ON_RB(&last->rb_node))
460                 rbnext = rb_next(&last->rb_node);
461         if (!rbnext) {
462                 rbnext = rb_first(&cfqq->sort_list);
463                 if (rbnext == &last->rb_node)
464                         rbnext = NULL;
465         }
466
467         rbprev = rb_prev(&last->rb_node);
468
469         if (rbprev)
470                 crq_prev = rb_entry_crq(rbprev);
471         if (rbnext)
472                 crq_next = rb_entry_crq(rbnext);
473
474         return cfq_choose_req(cfqd, crq_next, crq_prev);
475 }
476
477 static void cfq_update_next_crq(struct cfq_rq *crq)
478 {
479         struct cfq_queue *cfqq = crq->cfq_queue;
480
481         if (cfqq->next_crq == crq)
482                 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
483 }
484
485 static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
486 {
487         struct cfq_data *cfqd = cfqq->cfqd;
488         struct list_head *list, *entry;
489
490         BUG_ON(!cfq_cfqq_on_rr(cfqq));
491
492         list_del(&cfqq->cfq_list);
493
494         if (cfq_class_rt(cfqq))
495                 list = &cfqd->cur_rr;
496         else if (cfq_class_idle(cfqq))
497                 list = &cfqd->idle_rr;
498         else {
499                 /*
500                  * if cfqq has requests in flight, don't allow it to be
501                  * found in cfq_set_active_queue before it has finished them.
502                  * this is done to increase fairness between a process that
503                  * has lots of io pending vs one that only generates one
504                  * sporadically or synchronously
505                  */
506                 if (cfq_cfqq_dispatched(cfqq))
507                         list = &cfqd->busy_rr;
508                 else
509                         list = &cfqd->rr_list[cfqq->ioprio];
510         }
511
512         /*
513          * if queue was preempted, just add to front to be fair. busy_rr
514          * isn't sorted.
515          */
516         if (preempted || list == &cfqd->busy_rr) {
517                 list_add(&cfqq->cfq_list, list);
518                 return;
519         }
520
521         /*
522          * sort by when queue was last serviced
523          */
524         entry = list;
525         while ((entry = entry->prev) != list) {
526                 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
527
528                 if (!__cfqq->service_last)
529                         break;
530                 if (time_before(__cfqq->service_last, cfqq->service_last))
531                         break;
532         }
533
534         list_add(&cfqq->cfq_list, entry);
535 }
536
537 /*
538  * add to busy list of queues for service, trying to be fair in ordering
539  * the pending list according to last request service
540  */
541 static inline void
542 cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
543 {
544         BUG_ON(cfq_cfqq_on_rr(cfqq));
545         cfq_mark_cfqq_on_rr(cfqq);
546         cfqd->busy_queues++;
547
548         cfq_resort_rr_list(cfqq, requeue);
549 }
550
551 static inline void
552 cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
553 {
554         BUG_ON(!cfq_cfqq_on_rr(cfqq));
555         cfq_clear_cfqq_on_rr(cfqq);
556         list_move(&cfqq->cfq_list, &cfqd->empty_list);
557
558         BUG_ON(!cfqd->busy_queues);
559         cfqd->busy_queues--;
560 }
561
562 /*
563  * rb tree support functions
564  */
565 static inline void cfq_del_crq_rb(struct cfq_rq *crq)
566 {
567         struct cfq_queue *cfqq = crq->cfq_queue;
568
569         if (ON_RB(&crq->rb_node)) {
570                 struct cfq_data *cfqd = cfqq->cfqd;
571                 const int sync = cfq_crq_is_sync(crq);
572
573                 BUG_ON(!cfqq->queued[sync]);
574                 cfqq->queued[sync]--;
575
576                 cfq_update_next_crq(crq);
577
578                 rb_erase(&crq->rb_node, &cfqq->sort_list);
579                 RB_CLEAR_COLOR(&crq->rb_node);
580
581                 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
582                         cfq_del_cfqq_rr(cfqd, cfqq);
583         }
584 }
585
586 static struct cfq_rq *
587 __cfq_add_crq_rb(struct cfq_rq *crq)
588 {
589         struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
590         struct rb_node *parent = NULL;
591         struct cfq_rq *__crq;
592
593         while (*p) {
594                 parent = *p;
595                 __crq = rb_entry_crq(parent);
596
597                 if (crq->rb_key < __crq->rb_key)
598                         p = &(*p)->rb_left;
599                 else if (crq->rb_key > __crq->rb_key)
600                         p = &(*p)->rb_right;
601                 else
602                         return __crq;
603         }
604
605         rb_link_node(&crq->rb_node, parent, p);
606         return NULL;
607 }
608
609 static void cfq_add_crq_rb(struct cfq_rq *crq)
610 {
611         struct cfq_queue *cfqq = crq->cfq_queue;
612         struct cfq_data *cfqd = cfqq->cfqd;
613         struct request *rq = crq->request;
614         struct cfq_rq *__alias;
615
616         crq->rb_key = rq_rb_key(rq);
617         cfqq->queued[cfq_crq_is_sync(crq)]++;
618
619         /*
620          * looks a little odd, but the first insert might return an alias.
621          * if that happens, put the alias on the dispatch list
622          */
623         while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
624                 cfq_dispatch_sort(cfqd->queue, __alias);
625
626         rb_insert_color(&crq->rb_node, &cfqq->sort_list);
627
628         if (!cfq_cfqq_on_rr(cfqq))
629                 cfq_add_cfqq_rr(cfqd, cfqq, cfq_crq_requeued(crq));
630
631         /*
632          * check if this request is a better next-serve candidate
633          */
634         cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
635 }
636
637 static inline void
638 cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
639 {
640         if (ON_RB(&crq->rb_node)) {
641                 rb_erase(&crq->rb_node, &cfqq->sort_list);
642                 cfqq->queued[cfq_crq_is_sync(crq)]--;
643         }
644
645         cfq_add_crq_rb(crq);
646 }
647
648 static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
649
650 {
651         struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
652         struct rb_node *n;
653
654         if (!cfqq)
655                 goto out;
656
657         n = cfqq->sort_list.rb_node;
658         while (n) {
659                 struct cfq_rq *crq = rb_entry_crq(n);
660
661                 if (sector < crq->rb_key)
662                         n = n->rb_left;
663                 else if (sector > crq->rb_key)
664                         n = n->rb_right;
665                 else
666                         return crq->request;
667         }
668
669 out:
670         return NULL;
671 }
672
673 static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
674 {
675         struct cfq_data *cfqd = q->elevator->elevator_data;
676         struct cfq_rq *crq = RQ_DATA(rq);
677
678         if (crq) {
679                 struct cfq_queue *cfqq = crq->cfq_queue;
680
681                 if (cfq_crq_in_driver(crq)) {
682                         cfq_clear_crq_in_driver(crq);
683                         WARN_ON(!cfqd->rq_in_driver);
684                         cfqd->rq_in_driver--;
685                 }
686                 if (cfq_crq_in_flight(crq)) {
687                         const int sync = cfq_crq_is_sync(crq);
688
689                         cfq_clear_crq_in_flight(crq);
690                         WARN_ON(!cfqq->on_dispatch[sync]);
691                         cfqq->on_dispatch[sync]--;
692                 }
693                 cfq_mark_crq_requeued(crq);
694         }
695 }
696
697 /*
698  * make sure the service time gets corrected on reissue of this request
699  */
700 static void cfq_requeue_request(request_queue_t *q, struct request *rq)
701 {
702         cfq_deactivate_request(q, rq);
703         list_add(&rq->queuelist, &q->queue_head);
704 }
705
706 static void cfq_remove_request(request_queue_t *q, struct request *rq)
707 {
708         struct cfq_rq *crq = RQ_DATA(rq);
709
710         if (crq) {
711                 list_del_init(&rq->queuelist);
712                 cfq_del_crq_rb(crq);
713                 cfq_remove_merge_hints(q, crq);
714
715         }
716 }
717
718 static int
719 cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
720 {
721         struct cfq_data *cfqd = q->elevator->elevator_data;
722         struct request *__rq;
723         int ret;
724
725         ret = elv_try_last_merge(q, bio);
726         if (ret != ELEVATOR_NO_MERGE) {
727                 __rq = q->last_merge;
728                 goto out_insert;
729         }
730
731         __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
732         if (__rq && elv_rq_merge_ok(__rq, bio)) {
733                 ret = ELEVATOR_BACK_MERGE;
734                 goto out;
735         }
736
737         __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
738         if (__rq && elv_rq_merge_ok(__rq, bio)) {
739                 ret = ELEVATOR_FRONT_MERGE;
740                 goto out;
741         }
742
743         return ELEVATOR_NO_MERGE;
744 out:
745         q->last_merge = __rq;
746 out_insert:
747         *req = __rq;
748         return ret;
749 }
750
751 static void cfq_merged_request(request_queue_t *q, struct request *req)
752 {
753         struct cfq_data *cfqd = q->elevator->elevator_data;
754         struct cfq_rq *crq = RQ_DATA(req);
755
756         cfq_del_crq_hash(crq);
757         cfq_add_crq_hash(cfqd, crq);
758
759         if (ON_RB(&crq->rb_node) && (rq_rb_key(req) != crq->rb_key)) {
760                 struct cfq_queue *cfqq = crq->cfq_queue;
761
762                 cfq_update_next_crq(crq);
763                 cfq_reposition_crq_rb(cfqq, crq);
764         }
765
766         q->last_merge = req;
767 }
768
769 static void
770 cfq_merged_requests(request_queue_t *q, struct request *rq,
771                     struct request *next)
772 {
773         cfq_merged_request(q, rq);
774
775         /*
776          * reposition in fifo if next is older than rq
777          */
778         if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
779             time_before(next->start_time, rq->start_time))
780                 list_move(&rq->queuelist, &next->queuelist);
781
782         cfq_remove_request(q, next);
783 }
784
785 static inline void
786 __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
787 {
788         if (cfqq) {
789                 /*
790                  * stop potential idle class queues waiting service
791                  */
792                 del_timer(&cfqd->idle_class_timer);
793
794                 cfqq->slice_start = jiffies;
795                 cfqq->slice_end = 0;
796                 cfqq->slice_left = 0;
797                 cfq_clear_cfqq_must_alloc_slice(cfqq);
798                 cfq_clear_cfqq_fifo_expire(cfqq);
799                 cfq_clear_cfqq_expired(cfqq);
800         }
801
802         cfqd->active_queue = cfqq;
803 }
804
805 /*
806  * 0
807  * 0,1
808  * 0,1,2
809  * 0,1,2,3
810  * 0,1,2,3,4
811  * 0,1,2,3,4,5
812  * 0,1,2,3,4,5,6
813  * 0,1,2,3,4,5,6,7
814  */
815 static int cfq_get_next_prio_level(struct cfq_data *cfqd)
816 {
817         int prio, wrap;
818
819         prio = -1;
820         wrap = 0;
821         do {
822                 int p;
823
824                 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
825                         if (!list_empty(&cfqd->rr_list[p])) {
826                                 prio = p;
827                                 break;
828                         }
829                 }
830
831                 if (prio != -1)
832                         break;
833                 cfqd->cur_prio = 0;
834                 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
835                         cfqd->cur_end_prio = 0;
836                         if (wrap)
837                                 break;
838                         wrap = 1;
839                 }
840         } while (1);
841
842         if (unlikely(prio == -1))
843                 return -1;
844
845         BUG_ON(prio >= CFQ_PRIO_LISTS);
846
847         list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
848
849         cfqd->cur_prio = prio + 1;
850         if (cfqd->cur_prio > cfqd->cur_end_prio) {
851                 cfqd->cur_end_prio = cfqd->cur_prio;
852                 cfqd->cur_prio = 0;
853         }
854         if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
855                 cfqd->cur_prio = 0;
856                 cfqd->cur_end_prio = 0;
857         }
858
859         return prio;
860 }
861
862 static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
863 {
864         struct cfq_queue *cfqq;
865
866         /*
867          * if current queue is expired but not done with its requests yet,
868          * wait for that to happen
869          */
870         if ((cfqq = cfqd->active_queue) != NULL) {
871                 if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
872                         return NULL;
873         }
874
875         /*
876          * if current list is non-empty, grab first entry. if it is empty,
877          * get next prio level and grab first entry then if any are spliced
878          */
879         if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
880                 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
881
882         /*
883          * if we have idle queues and no rt or be queues had pending
884          * requests, either allow immediate service if the grace period
885          * has passed or arm the idle grace timer
886          */
887         if (!cfqq && !list_empty(&cfqd->idle_rr)) {
888                 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
889
890                 if (time_after_eq(jiffies, end))
891                         cfqq = list_entry_cfqq(cfqd->idle_rr.next);
892                 else
893                         mod_timer(&cfqd->idle_class_timer, end);
894         }
895
896         __cfq_set_active_queue(cfqd, cfqq);
897         return cfqq;
898 }
899
900 /*
901  * current cfqq expired its slice (or was too idle), select new one
902  */
903 static void
904 __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
905                     int preempted)
906 {
907         unsigned long now = jiffies;
908
909         if (cfq_cfqq_wait_request(cfqq))
910                 del_timer(&cfqd->idle_slice_timer);
911
912         if (!preempted && !cfq_cfqq_dispatched(cfqq))
913                 cfqq->service_last = now;
914
915         cfq_clear_cfqq_must_dispatch(cfqq);
916         cfq_clear_cfqq_wait_request(cfqq);
917
918         /*
919          * store what was left of this slice, if the queue idled out
920          * or was preempted
921          */
922         if (time_after(now, cfqq->slice_end))
923                 cfqq->slice_left = now - cfqq->slice_end;
924         else
925                 cfqq->slice_left = 0;
926
927         if (cfq_cfqq_on_rr(cfqq))
928                 cfq_resort_rr_list(cfqq, preempted);
929
930         if (cfqq == cfqd->active_queue)
931                 cfqd->active_queue = NULL;
932
933         if (cfqd->active_cic) {
934                 put_io_context(cfqd->active_cic->ioc);
935                 cfqd->active_cic = NULL;
936         }
937
938         cfqd->dispatch_slice = 0;
939 }
940
941 static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
942 {
943         struct cfq_queue *cfqq = cfqd->active_queue;
944
945         if (cfqq) {
946                 /*
947                  * use deferred expiry, if there are requests in progress as
948                  * not to disturb the slice of the next queue
949                  */
950                 if (cfq_cfqq_dispatched(cfqq))
951                         cfq_mark_cfqq_expired(cfqq);
952                 else
953                         __cfq_slice_expired(cfqd, cfqq, preempted);
954         }
955 }
956
957 static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
958
959 {
960         WARN_ON(!RB_EMPTY(&cfqq->sort_list));
961         WARN_ON(cfqq != cfqd->active_queue);
962
963         /*
964          * idle is disabled, either manually or by past process history
965          */
966         if (!cfqd->cfq_slice_idle)
967                 return 0;
968         if (!cfq_cfqq_idle_window(cfqq))
969                 return 0;
970         /*
971          * task has exited, don't wait
972          */
973         if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
974                 return 0;
975
976         cfq_mark_cfqq_must_dispatch(cfqq);
977         cfq_mark_cfqq_wait_request(cfqq);
978
979         if (!timer_pending(&cfqd->idle_slice_timer)) {
980                 unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
981
982                 cfqd->idle_slice_timer.expires = jiffies + slice_left;
983                 add_timer(&cfqd->idle_slice_timer);
984         }
985
986         return 1;
987 }
988
989 /*
990  * we dispatch cfqd->cfq_quantum requests in total from the rr_list queues,
991  * this function sector sorts the selected request to minimize seeks. we start
992  * at cfqd->last_sector, not 0.
993  */
994 static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
995 {
996         struct cfq_data *cfqd = q->elevator->elevator_data;
997         struct cfq_queue *cfqq = crq->cfq_queue;
998         struct list_head *head = &q->queue_head, *entry = head;
999         struct request *__rq;
1000         sector_t last;
1001
1002         list_del(&crq->request->queuelist);
1003
1004         last = cfqd->last_sector;
1005         list_for_each_entry_reverse(__rq, head, queuelist) {
1006                 struct cfq_rq *__crq = RQ_DATA(__rq);
1007
1008                 if (blk_barrier_rq(__rq))
1009                         break;
1010                 if (!blk_fs_request(__rq))
1011                         break;
1012                 if (cfq_crq_requeued(__crq))
1013                         break;
1014
1015                 if (__rq->sector <= crq->request->sector)
1016                         break;
1017                 if (__rq->sector > last && crq->request->sector < last) {
1018                         last = crq->request->sector + crq->request->nr_sectors;
1019                         break;
1020                 }
1021                 entry = &__rq->queuelist;
1022         }
1023
1024         cfqd->last_sector = last;
1025
1026         cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
1027
1028         cfq_del_crq_rb(crq);
1029         cfq_remove_merge_hints(q, crq);
1030
1031         cfq_mark_crq_in_flight(crq);
1032         cfq_clear_crq_requeued(crq);
1033
1034         cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
1035         list_add_tail(&crq->request->queuelist, entry);
1036 }
1037
1038 /*
1039  * return expired entry, or NULL to just start from scratch in rbtree
1040  */
1041 static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
1042 {
1043         struct cfq_data *cfqd = cfqq->cfqd;
1044         struct request *rq;
1045         struct cfq_rq *crq;
1046
1047         if (cfq_cfqq_fifo_expire(cfqq))
1048                 return NULL;
1049
1050         if (!list_empty(&cfqq->fifo)) {
1051                 int fifo = cfq_cfqq_class_sync(cfqq);
1052
1053                 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
1054                 rq = crq->request;
1055                 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
1056                         cfq_mark_cfqq_fifo_expire(cfqq);
1057                         return crq;
1058                 }
1059         }
1060
1061         return NULL;
1062 }
1063
1064 /*
1065  * Scale schedule slice based on io priority. Use the sync time slice only
1066  * if a queue is marked sync and has sync io queued. A sync queue with async
1067  * io only, should not get full sync slice length.
1068  */
1069 static inline int
1070 cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1071 {
1072         const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
1073
1074         WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1075
1076         return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
1077 }
1078
1079 static inline void
1080 cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1081 {
1082         cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1083 }
1084
1085 static inline int
1086 cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1087 {
1088         const int base_rq = cfqd->cfq_slice_async_rq;
1089
1090         WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1091
1092         return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1093 }
1094
1095 /*
1096  * get next queue for service
1097  */
1098 static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1099 {
1100         unsigned long now = jiffies;
1101         struct cfq_queue *cfqq;
1102
1103         cfqq = cfqd->active_queue;
1104         if (!cfqq)
1105                 goto new_queue;
1106
1107         if (cfq_cfqq_expired(cfqq))
1108                 goto new_queue;
1109
1110         /*
1111          * slice has expired
1112          */
1113         if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1114                 goto expire;
1115
1116         /*
1117          * if queue has requests, dispatch one. if not, check if
1118          * enough slice is left to wait for one
1119          */
1120         if (!RB_EMPTY(&cfqq->sort_list))
1121                 goto keep_queue;
1122         else if (!force && cfq_cfqq_class_sync(cfqq) &&
1123                  time_before(now, cfqq->slice_end)) {
1124                 if (cfq_arm_slice_timer(cfqd, cfqq))
1125                         return NULL;
1126         }
1127
1128 expire:
1129         cfq_slice_expired(cfqd, 0);
1130 new_queue:
1131         cfqq = cfq_set_active_queue(cfqd);
1132 keep_queue:
1133         return cfqq;
1134 }
1135
1136 static int
1137 __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1138                         int max_dispatch)
1139 {
1140         int dispatched = 0;
1141
1142         BUG_ON(RB_EMPTY(&cfqq->sort_list));
1143
1144         do {
1145                 struct cfq_rq *crq;
1146
1147                 /*
1148                  * follow expired path, else get first next available
1149                  */
1150                 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1151                         crq = cfqq->next_crq;
1152
1153                 /*
1154                  * finally, insert request into driver dispatch list
1155                  */
1156                 cfq_dispatch_sort(cfqd->queue, crq);
1157
1158                 cfqd->dispatch_slice++;
1159                 dispatched++;
1160
1161                 if (!cfqd->active_cic) {
1162                         atomic_inc(&crq->io_context->ioc->refcount);
1163                         cfqd->active_cic = crq->io_context;
1164                 }
1165
1166                 if (RB_EMPTY(&cfqq->sort_list))
1167                         break;
1168
1169         } while (dispatched < max_dispatch);
1170
1171         /*
1172          * if slice end isn't set yet, set it. if at least one request was
1173          * sync, use the sync time slice value
1174          */
1175         if (!cfqq->slice_end)
1176                 cfq_set_prio_slice(cfqd, cfqq);
1177
1178         /*
1179          * expire an async queue immediately if it has used up its slice. idle
1180          * queue always expire after 1 dispatch round.
1181          */
1182         if ((!cfq_cfqq_sync(cfqq) &&
1183             cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1184             cfq_class_idle(cfqq))
1185                 cfq_slice_expired(cfqd, 0);
1186
1187         return dispatched;
1188 }
1189
1190 static int
1191 cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1192 {
1193         struct cfq_data *cfqd = q->elevator->elevator_data;
1194         struct cfq_queue *cfqq;
1195
1196         if (!cfqd->busy_queues)
1197                 return 0;
1198
1199         cfqq = cfq_select_queue(cfqd, force);
1200         if (cfqq) {
1201                 cfq_clear_cfqq_must_dispatch(cfqq);
1202                 cfq_clear_cfqq_wait_request(cfqq);
1203                 del_timer(&cfqd->idle_slice_timer);
1204
1205                 if (cfq_class_idle(cfqq))
1206                         max_dispatch = 1;
1207
1208                 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1209         }
1210
1211         return 0;
1212 }
1213
1214 static inline void cfq_account_dispatch(struct cfq_rq *crq)
1215 {
1216         struct cfq_queue *cfqq = crq->cfq_queue;
1217         struct cfq_data *cfqd = cfqq->cfqd;
1218
1219         if (unlikely(!blk_fs_request(crq->request)))
1220                 return;
1221
1222         /*
1223          * accounted bit is necessary since some drivers will call
1224          * elv_next_request() many times for the same request (eg ide)
1225          */
1226         if (cfq_crq_in_driver(crq))
1227                 return;
1228
1229         cfq_mark_crq_in_driver(crq);
1230         cfqd->rq_in_driver++;
1231 }
1232
1233 static inline void
1234 cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1235 {
1236         struct cfq_data *cfqd = cfqq->cfqd;
1237         unsigned long now;
1238
1239         if (!cfq_crq_in_driver(crq))
1240                 return;
1241
1242         now = jiffies;
1243
1244         WARN_ON(!cfqd->rq_in_driver);
1245         cfqd->rq_in_driver--;
1246
1247         if (!cfq_class_idle(cfqq))
1248                 cfqd->last_end_request = now;
1249
1250         if (!cfq_cfqq_dispatched(cfqq)) {
1251                 if (cfq_cfqq_on_rr(cfqq)) {
1252                         cfqq->service_last = now;
1253                         cfq_resort_rr_list(cfqq, 0);
1254                 }
1255                 if (cfq_cfqq_expired(cfqq)) {
1256                         __cfq_slice_expired(cfqd, cfqq, 0);
1257                         cfq_schedule_dispatch(cfqd);
1258                 }
1259         }
1260
1261         if (cfq_crq_is_sync(crq))
1262                 crq->io_context->last_end_request = now;
1263 }
1264
1265 static struct request *cfq_next_request(request_queue_t *q)
1266 {
1267         struct cfq_data *cfqd = q->elevator->elevator_data;
1268         struct request *rq;
1269
1270         if (!list_empty(&q->queue_head)) {
1271                 struct cfq_rq *crq;
1272 dispatch:
1273                 rq = list_entry_rq(q->queue_head.next);
1274
1275                 crq = RQ_DATA(rq);
1276                 if (crq) {
1277                         struct cfq_queue *cfqq = crq->cfq_queue;
1278
1279                         /*
1280                          * if idle window is disabled, allow queue buildup
1281                          */
1282                         if (!cfq_crq_in_driver(crq) &&
1283                             !cfq_cfqq_idle_window(cfqq) &&
1284                             !blk_barrier_rq(rq) &&
1285                             cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1286                                 return NULL;
1287
1288                         cfq_remove_merge_hints(q, crq);
1289                         cfq_account_dispatch(crq);
1290                 }
1291
1292                 return rq;
1293         }
1294
1295         if (cfq_dispatch_requests(q, cfqd->cfq_quantum, 0))
1296                 goto dispatch;
1297
1298         return NULL;
1299 }
1300
1301 /*
1302  * task holds one reference to the queue, dropped when task exits. each crq
1303  * in-flight on this queue also holds a reference, dropped when crq is freed.
1304  *
1305  * queue lock must be held here.
1306  */
1307 static void cfq_put_queue(struct cfq_queue *cfqq)
1308 {
1309         struct cfq_data *cfqd = cfqq->cfqd;
1310
1311         BUG_ON(atomic_read(&cfqq->ref) <= 0);
1312
1313         if (!atomic_dec_and_test(&cfqq->ref))
1314                 return;
1315
1316         BUG_ON(rb_first(&cfqq->sort_list));
1317         BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1318         BUG_ON(cfq_cfqq_on_rr(cfqq));
1319
1320         if (unlikely(cfqd->active_queue == cfqq)) {
1321                 __cfq_slice_expired(cfqd, cfqq, 0);
1322                 cfq_schedule_dispatch(cfqd);
1323         }
1324
1325         cfq_put_cfqd(cfqq->cfqd);
1326
1327         /*
1328          * it's on the empty list and still hashed
1329          */
1330         list_del(&cfqq->cfq_list);
1331         hlist_del(&cfqq->cfq_hash);
1332         kmem_cache_free(cfq_pool, cfqq);
1333 }
1334
1335 static inline struct cfq_queue *
1336 __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1337                     const int hashval)
1338 {
1339         struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1340         struct hlist_node *entry, *next;
1341
1342         hlist_for_each_safe(entry, next, hash_list) {
1343                 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1344                 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1345
1346                 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1347                         return __cfqq;
1348         }
1349
1350         return NULL;
1351 }
1352
1353 static struct cfq_queue *
1354 cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1355 {
1356         return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1357 }
1358
1359 static void cfq_free_io_context(struct cfq_io_context *cic)
1360 {
1361         struct cfq_io_context *__cic;
1362         struct list_head *entry, *next;
1363
1364         list_for_each_safe(entry, next, &cic->list) {
1365                 __cic = list_entry(entry, struct cfq_io_context, list);
1366                 kmem_cache_free(cfq_ioc_pool, __cic);
1367         }
1368
1369         kmem_cache_free(cfq_ioc_pool, cic);
1370 }
1371
1372 /*
1373  * Called with interrupts disabled
1374  */
1375 static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1376 {
1377         struct cfq_data *cfqd = cic->cfqq->cfqd;
1378         request_queue_t *q = cfqd->queue;
1379
1380         WARN_ON(!irqs_disabled());
1381
1382         spin_lock(q->queue_lock);
1383
1384         if (unlikely(cic->cfqq == cfqd->active_queue)) {
1385                 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1386                 cfq_schedule_dispatch(cfqd);
1387         }
1388
1389         cfq_put_queue(cic->cfqq);
1390         cic->cfqq = NULL;
1391         spin_unlock(q->queue_lock);
1392 }
1393
1394 /*
1395  * Another task may update the task cic list, if it is doing a queue lookup
1396  * on its behalf. cfq_cic_lock excludes such concurrent updates
1397  */
1398 static void cfq_exit_io_context(struct cfq_io_context *cic)
1399 {
1400         struct cfq_io_context *__cic;
1401         struct list_head *entry;
1402         unsigned long flags;
1403
1404         local_irq_save(flags);
1405
1406         /*
1407          * put the reference this task is holding to the various queues
1408          */
1409         list_for_each(entry, &cic->list) {
1410                 __cic = list_entry(entry, struct cfq_io_context, list);
1411                 cfq_exit_single_io_context(__cic);
1412         }
1413
1414         cfq_exit_single_io_context(cic);
1415         local_irq_restore(flags);
1416 }
1417
1418 static struct cfq_io_context *
1419 cfq_alloc_io_context(struct cfq_data *cfqd, int gfp_mask)
1420 {
1421         struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
1422
1423         if (cic) {
1424                 INIT_LIST_HEAD(&cic->list);
1425                 cic->cfqq = NULL;
1426                 cic->key = NULL;
1427                 cic->last_end_request = jiffies;
1428                 cic->ttime_total = 0;
1429                 cic->ttime_samples = 0;
1430                 cic->ttime_mean = 0;
1431                 cic->dtor = cfq_free_io_context;
1432                 cic->exit = cfq_exit_io_context;
1433         }
1434
1435         return cic;
1436 }
1437
1438 static void cfq_init_prio_data(struct cfq_queue *cfqq)
1439 {
1440         struct task_struct *tsk = current;
1441         int ioprio_class;
1442
1443         if (!cfq_cfqq_prio_changed(cfqq))
1444                 return;
1445
1446         ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1447         switch (ioprio_class) {
1448                 default:
1449                         printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1450                 case IOPRIO_CLASS_NONE:
1451                         /*
1452                          * no prio set, place us in the middle of the BE classes
1453                          */
1454                         cfqq->ioprio = task_nice_ioprio(tsk);
1455                         cfqq->ioprio_class = IOPRIO_CLASS_BE;
1456                         break;
1457                 case IOPRIO_CLASS_RT:
1458                         cfqq->ioprio = task_ioprio(tsk);
1459                         cfqq->ioprio_class = IOPRIO_CLASS_RT;
1460                         break;
1461                 case IOPRIO_CLASS_BE:
1462                         cfqq->ioprio = task_ioprio(tsk);
1463                         cfqq->ioprio_class = IOPRIO_CLASS_BE;
1464                         break;
1465                 case IOPRIO_CLASS_IDLE:
1466                         cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1467                         cfqq->ioprio = 7;
1468                         cfq_clear_cfqq_idle_window(cfqq);
1469                         break;
1470         }
1471
1472         /*
1473          * keep track of original prio settings in case we have to temporarily
1474          * elevate the priority of this queue
1475          */
1476         cfqq->org_ioprio = cfqq->ioprio;
1477         cfqq->org_ioprio_class = cfqq->ioprio_class;
1478
1479         if (cfq_cfqq_on_rr(cfqq))
1480                 cfq_resort_rr_list(cfqq, 0);
1481
1482         cfq_clear_cfqq_prio_changed(cfqq);
1483 }
1484
1485 static inline void changed_ioprio(struct cfq_queue *cfqq)
1486 {
1487         if (cfqq) {
1488                 struct cfq_data *cfqd = cfqq->cfqd;
1489
1490                 spin_lock(cfqd->queue->queue_lock);
1491                 cfq_mark_cfqq_prio_changed(cfqq);
1492                 cfq_init_prio_data(cfqq);
1493                 spin_unlock(cfqd->queue->queue_lock);
1494         }
1495 }
1496
1497 /*
1498  * callback from sys_ioprio_set, irqs are disabled
1499  */
1500 static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1501 {
1502         struct cfq_io_context *cic = ioc->cic;
1503
1504         changed_ioprio(cic->cfqq);
1505
1506         list_for_each_entry(cic, &cic->list, list)
1507                 changed_ioprio(cic->cfqq);
1508
1509         return 0;
1510 }
1511
1512 static struct cfq_queue *
1513 cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1514               int gfp_mask)
1515 {
1516         const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1517         struct cfq_queue *cfqq, *new_cfqq = NULL;
1518
1519 retry:
1520         cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1521
1522         if (!cfqq) {
1523                 if (new_cfqq) {
1524                         cfqq = new_cfqq;
1525                         new_cfqq = NULL;
1526                 } else if (gfp_mask & __GFP_WAIT) {
1527                         spin_unlock_irq(cfqd->queue->queue_lock);
1528                         new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1529                         spin_lock_irq(cfqd->queue->queue_lock);
1530                         goto retry;
1531                 } else {
1532                         cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1533                         if (!cfqq)
1534                                 goto out;
1535                 }
1536
1537                 memset(cfqq, 0, sizeof(*cfqq));
1538
1539                 INIT_HLIST_NODE(&cfqq->cfq_hash);
1540                 INIT_LIST_HEAD(&cfqq->cfq_list);
1541                 RB_CLEAR_ROOT(&cfqq->sort_list);
1542                 INIT_LIST_HEAD(&cfqq->fifo);
1543
1544                 cfqq->key = key;
1545                 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1546                 atomic_set(&cfqq->ref, 0);
1547                 cfqq->cfqd = cfqd;
1548                 atomic_inc(&cfqd->ref);
1549                 cfqq->service_last = 0;
1550                 /*
1551                  * set ->slice_left to allow preemption for a new process
1552                  */
1553                 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1554                 cfq_mark_cfqq_idle_window(cfqq);
1555                 cfq_mark_cfqq_prio_changed(cfqq);
1556                 cfq_init_prio_data(cfqq);
1557         }
1558
1559         if (new_cfqq)
1560                 kmem_cache_free(cfq_pool, new_cfqq);
1561
1562         atomic_inc(&cfqq->ref);
1563 out:
1564         WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1565         return cfqq;
1566 }
1567
1568 /*
1569  * Setup general io context and cfq io context. There can be several cfq
1570  * io contexts per general io context, if this process is doing io to more
1571  * than one device managed by cfq. Note that caller is holding a reference to
1572  * cfqq, so we don't need to worry about it disappearing
1573  */
1574 static struct cfq_io_context *
1575 cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, int gfp_mask)
1576 {
1577         struct io_context *ioc = NULL;
1578         struct cfq_io_context *cic;
1579
1580         might_sleep_if(gfp_mask & __GFP_WAIT);
1581
1582         ioc = get_io_context(gfp_mask);
1583         if (!ioc)
1584                 return NULL;
1585
1586         if ((cic = ioc->cic) == NULL) {
1587                 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1588
1589                 if (cic == NULL)
1590                         goto err;
1591
1592                 /*
1593                  * manually increment generic io_context usage count, it
1594                  * cannot go away since we are already holding one ref to it
1595                  */
1596                 ioc->cic = cic;
1597                 ioc->set_ioprio = cfq_ioc_set_ioprio;
1598                 cic->ioc = ioc;
1599                 cic->key = cfqd;
1600                 atomic_inc(&cfqd->ref);
1601         } else {
1602                 struct cfq_io_context *__cic;
1603
1604                 /*
1605                  * the first cic on the list is actually the head itself
1606                  */
1607                 if (cic->key == cfqd)
1608                         goto out;
1609
1610                 /*
1611                  * cic exists, check if we already are there. linear search
1612                  * should be ok here, the list will usually not be more than
1613                  * 1 or a few entries long
1614                  */
1615                 list_for_each_entry(__cic, &cic->list, list) {
1616                         /*
1617                          * this process is already holding a reference to
1618                          * this queue, so no need to get one more
1619                          */
1620                         if (__cic->key == cfqd) {
1621                                 cic = __cic;
1622                                 goto out;
1623                         }
1624                 }
1625
1626                 /*
1627                  * nope, process doesn't have a cic assoicated with this
1628                  * cfqq yet. get a new one and add to list
1629                  */
1630                 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1631                 if (__cic == NULL)
1632                         goto err;
1633
1634                 __cic->ioc = ioc;
1635                 __cic->key = cfqd;
1636                 atomic_inc(&cfqd->ref);
1637                 list_add(&__cic->list, &cic->list);
1638                 cic = __cic;
1639         }
1640
1641 out:
1642         return cic;
1643 err:
1644         put_io_context(ioc);
1645         return NULL;
1646 }
1647
1648 static void
1649 cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1650 {
1651         unsigned long elapsed, ttime;
1652
1653         /*
1654          * if this context already has stuff queued, thinktime is from
1655          * last queue not last end
1656          */
1657 #if 0
1658         if (time_after(cic->last_end_request, cic->last_queue))
1659                 elapsed = jiffies - cic->last_end_request;
1660         else
1661                 elapsed = jiffies - cic->last_queue;
1662 #else
1663                 elapsed = jiffies - cic->last_end_request;
1664 #endif
1665
1666         ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1667
1668         cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1669         cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1670         cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1671 }
1672
1673 #define sample_valid(samples)   ((samples) > 80)
1674
1675 /*
1676  * Disable idle window if the process thinks too long or seeks so much that
1677  * it doesn't matter
1678  */
1679 static void
1680 cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1681                        struct cfq_io_context *cic)
1682 {
1683         int enable_idle = cfq_cfqq_idle_window(cfqq);
1684
1685         if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1686                 enable_idle = 0;
1687         else if (sample_valid(cic->ttime_samples)) {
1688                 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1689                         enable_idle = 0;
1690                 else
1691                         enable_idle = 1;
1692         }
1693
1694         if (enable_idle)
1695                 cfq_mark_cfqq_idle_window(cfqq);
1696         else
1697                 cfq_clear_cfqq_idle_window(cfqq);
1698 }
1699
1700
1701 /*
1702  * Check if new_cfqq should preempt the currently active queue. Return 0 for
1703  * no or if we aren't sure, a 1 will cause a preempt.
1704  */
1705 static int
1706 cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1707                    struct cfq_rq *crq)
1708 {
1709         struct cfq_queue *cfqq = cfqd->active_queue;
1710
1711         if (cfq_class_idle(new_cfqq))
1712                 return 0;
1713
1714         if (!cfqq)
1715                 return 1;
1716
1717         if (cfq_class_idle(cfqq))
1718                 return 1;
1719         if (!cfq_cfqq_wait_request(new_cfqq))
1720                 return 0;
1721         /*
1722          * if it doesn't have slice left, forget it
1723          */
1724         if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1725                 return 0;
1726         if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1727                 return 1;
1728
1729         return 0;
1730 }
1731
1732 /*
1733  * cfqq preempts the active queue. if we allowed preempt with no slice left,
1734  * let it have half of its nominal slice.
1735  */
1736 static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1737 {
1738         struct cfq_queue *__cfqq, *next;
1739
1740         list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1741                 cfq_resort_rr_list(__cfqq, 1);
1742
1743         if (!cfqq->slice_left)
1744                 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1745
1746         cfqq->slice_end = cfqq->slice_left + jiffies;
1747         __cfq_slice_expired(cfqd, cfqq, 1);
1748         __cfq_set_active_queue(cfqd, cfqq);
1749 }
1750
1751 /*
1752  * should really be a ll_rw_blk.c helper
1753  */
1754 static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1755 {
1756         request_queue_t *q = cfqd->queue;
1757
1758         if (!blk_queue_plugged(q))
1759                 q->request_fn(q);
1760         else
1761                 __generic_unplug_device(q);
1762 }
1763
1764 /*
1765  * Called when a new fs request (crq) is added (to cfqq). Check if there's
1766  * something we should do about it
1767  */
1768 static void
1769 cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1770                  struct cfq_rq *crq)
1771 {
1772         const int sync = cfq_crq_is_sync(crq);
1773
1774         cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1775
1776         if (sync) {
1777                 struct cfq_io_context *cic = crq->io_context;
1778
1779                 cfq_update_io_thinktime(cfqd, cic);
1780                 cfq_update_idle_window(cfqd, cfqq, cic);
1781
1782                 cic->last_queue = jiffies;
1783         }
1784
1785         if (cfqq == cfqd->active_queue) {
1786                 /*
1787                  * if we are waiting for a request for this queue, let it rip
1788                  * immediately and flag that we must not expire this queue
1789                  * just now
1790                  */
1791                 if (cfq_cfqq_wait_request(cfqq)) {
1792                         cfq_mark_cfqq_must_dispatch(cfqq);
1793                         del_timer(&cfqd->idle_slice_timer);
1794                         cfq_start_queueing(cfqd, cfqq);
1795                 }
1796         } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1797                 /*
1798                  * not the active queue - expire current slice if it is
1799                  * idle and has expired it's mean thinktime or this new queue
1800                  * has some old slice time left and is of higher priority
1801                  */
1802                 cfq_preempt_queue(cfqd, cfqq);
1803                 cfq_mark_cfqq_must_dispatch(cfqq);
1804                 cfq_start_queueing(cfqd, cfqq);
1805         }
1806 }
1807
1808 static void cfq_enqueue(struct cfq_data *cfqd, struct request *rq)
1809 {
1810         struct cfq_rq *crq = RQ_DATA(rq);
1811         struct cfq_queue *cfqq = crq->cfq_queue;
1812
1813         cfq_init_prio_data(cfqq);
1814
1815         cfq_add_crq_rb(crq);
1816
1817         list_add_tail(&rq->queuelist, &cfqq->fifo);
1818
1819         if (rq_mergeable(rq)) {
1820                 cfq_add_crq_hash(cfqd, crq);
1821
1822                 if (!cfqd->queue->last_merge)
1823                         cfqd->queue->last_merge = rq;
1824         }
1825
1826         cfq_crq_enqueued(cfqd, cfqq, crq);
1827 }
1828
1829 static void
1830 cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1831 {
1832         struct cfq_data *cfqd = q->elevator->elevator_data;
1833
1834         switch (where) {
1835                 case ELEVATOR_INSERT_BACK:
1836                         while (cfq_dispatch_requests(q, INT_MAX, 1))
1837                                 ;
1838                         list_add_tail(&rq->queuelist, &q->queue_head);
1839                         /*
1840                          * If we were idling with pending requests on
1841                          * inactive cfqqs, force dispatching will
1842                          * remove the idle timer and the queue won't
1843                          * be kicked by __make_request() afterward.
1844                          * Kick it here.
1845                          */
1846                         cfq_schedule_dispatch(cfqd);
1847                         break;
1848                 case ELEVATOR_INSERT_FRONT:
1849                         list_add(&rq->queuelist, &q->queue_head);
1850                         break;
1851                 case ELEVATOR_INSERT_SORT:
1852                         BUG_ON(!blk_fs_request(rq));
1853                         cfq_enqueue(cfqd, rq);
1854                         break;
1855                 default:
1856                         printk("%s: bad insert point %d\n", __FUNCTION__,where);
1857                         return;
1858         }
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(cfqq)) {
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(cfqq)) {
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");