blob: 3bacf4bb7dd2ce62495c68f57ab65fda06bb5c16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
8 */
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/blkdev.h>
12#include <linux/elevator.h>
13#include <linux/bio.h>
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/compiler.h>
19#include <linux/hash.h>
20#include <linux/rbtree.h>
21#include <linux/mempool.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020022#include <linux/ioprio.h>
23#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * tunables
27 */
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_quantum = 4; /* max queue in one round of service */
29static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
30static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
31static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
32static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020035static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010036static const int cfq_slice_async_rq = 2;
Jens Axboe3b181522005-06-27 10:56:24 +020037static int cfq_slice_idle = HZ / 100;
Jens Axboe22e2c502005-06-27 10:55:12 +020038
39#define CFQ_IDLE_GRACE (HZ / 10)
40#define CFQ_SLICE_SCALE (5)
41
42#define CFQ_KEY_ASYNC (0)
Jens Axboe3b181522005-06-27 10:56:24 +020043#define CFQ_KEY_ANY (0xffff)
Jens Axboe22e2c502005-06-27 10:55:12 +020044
45/*
46 * disable queueing at the driver/hardware level
47 */
Arjan van de Ven64100092006-01-06 09:46:02 +010048static const int cfq_max_depth = 2;
Jens Axboe22e2c502005-06-27 10:55:12 +020049
Al Viroa6a07632006-03-18 13:26:44 -050050static DEFINE_RWLOCK(cfq_exit_lock);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
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)
Jens Axboe22e2c502005-06-27 10:55:12 +020070#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88#define rq_rb_key(rq) (rq)->sector
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static kmem_cache_t *crq_pool;
91static kmem_cache_t *cfq_pool;
92static kmem_cache_t *cfq_ioc_pool;
93
Jens Axboe22e2c502005-06-27 10:55:12 +020094#define CFQ_PRIO_LISTS IOPRIO_BE_NR
95#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
96#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
98
Jens Axboe3b181522005-06-27 10:56:24 +020099#define ASYNC (0)
100#define SYNC (1)
101
102#define cfq_cfqq_dispatched(cfqq) \
103 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
104
105#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
106
107#define cfq_cfqq_sync(cfqq) \
108 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +0200109
110/*
111 * Per block device queue structure
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +0200114 atomic_t ref;
115 request_queue_t *queue;
116
117 /*
118 * rr list of queues with requests and the count of them
119 */
120 struct list_head rr_list[CFQ_PRIO_LISTS];
121 struct list_head busy_rr;
122 struct list_head cur_rr;
123 struct list_head idle_rr;
124 unsigned int busy_queues;
125
126 /*
127 * non-ordered list of empty cfqq's
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct list_head empty_list;
130
Jens Axboe22e2c502005-06-27 10:55:12 +0200131 /*
132 * cfqq lookup hash
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jens Axboe22e2c502005-06-27 10:55:12 +0200136 /*
137 * global crq hash for all queues
138 */
139 struct hlist_head *crq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 unsigned int max_queued;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 mempool_t *crq_pool;
144
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 int rq_in_driver;
146
147 /*
148 * schedule slice state info
149 */
150 /*
151 * idle window management
152 */
153 struct timer_list idle_slice_timer;
154 struct work_struct unplug_work;
155
156 struct cfq_queue *active_queue;
157 struct cfq_io_context *active_cic;
158 int cur_prio, cur_end_prio;
159 unsigned int dispatch_slice;
160
161 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200164 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * tunables, see top of file
170 */
171 unsigned int cfq_quantum;
172 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200173 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int cfq_back_penalty;
175 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200176 unsigned int cfq_slice[2];
177 unsigned int cfq_slice_async_rq;
178 unsigned int cfq_slice_idle;
179 unsigned int cfq_max_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Jens Axboe22e2c502005-06-27 10:55:12 +0200182/*
183 * Per process-grouping structure
184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185struct cfq_queue {
186 /* reference count */
187 atomic_t ref;
188 /* parent cfq_data */
189 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200190 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct hlist_node cfq_hash;
192 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200193 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* on either rr or empty list of cfqd */
195 struct list_head cfq_list;
196 /* sorted list of pending requests */
197 struct rb_root sort_list;
198 /* if fifo isn't expired, next request to serve */
199 struct cfq_rq *next_crq;
200 /* requests queued in sort_list */
201 int queued[2];
202 /* currently allocated requests */
203 int allocated[2];
204 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200205 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jens Axboe22e2c502005-06-27 10:55:12 +0200207 unsigned long slice_start;
208 unsigned long slice_end;
209 unsigned long slice_left;
210 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jens Axboe3b181522005-06-27 10:56:24 +0200212 /* number of requests that are on the dispatch list */
213 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200214
215 /* io prio of this group */
216 unsigned short ioprio, org_ioprio;
217 unsigned short ioprio_class, org_ioprio_class;
218
Jens Axboe3b181522005-06-27 10:56:24 +0200219 /* various state flags, see below */
220 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
223struct cfq_rq {
224 struct rb_node rb_node;
225 sector_t rb_key;
226 struct request *request;
227 struct hlist_node hash;
228
229 struct cfq_queue *cfq_queue;
230 struct cfq_io_context *io_context;
231
Jens Axboe3b181522005-06-27 10:56:24 +0200232 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Jens Axboe3b181522005-06-27 10:56:24 +0200235enum cfqq_state_flags {
236 CFQ_CFQQ_FLAG_on_rr = 0,
237 CFQ_CFQQ_FLAG_wait_request,
238 CFQ_CFQQ_FLAG_must_alloc,
239 CFQ_CFQQ_FLAG_must_alloc_slice,
240 CFQ_CFQQ_FLAG_must_dispatch,
241 CFQ_CFQQ_FLAG_fifo_expire,
242 CFQ_CFQQ_FLAG_idle_window,
243 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200244};
245
246#define CFQ_CFQQ_FNS(name) \
247static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
248{ \
249 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
250} \
251static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
252{ \
253 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
254} \
255static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
256{ \
257 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
258}
259
260CFQ_CFQQ_FNS(on_rr);
261CFQ_CFQQ_FNS(wait_request);
262CFQ_CFQQ_FNS(must_alloc);
263CFQ_CFQQ_FNS(must_alloc_slice);
264CFQ_CFQQ_FNS(must_dispatch);
265CFQ_CFQQ_FNS(fifo_expire);
266CFQ_CFQQ_FNS(idle_window);
267CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200268#undef CFQ_CFQQ_FNS
269
270enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200271 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200272};
273
274#define CFQ_CRQ_FNS(name) \
275static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
276{ \
277 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
278} \
279static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
280{ \
281 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
282} \
283static inline int cfq_crq_##name(const struct cfq_rq *crq) \
284{ \
285 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
286}
287
Jens Axboe3b181522005-06-27 10:56:24 +0200288CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200289#undef CFQ_CRQ_FNS
290
291static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200292static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static void cfq_put_cfqd(struct cfq_data *cfqd);
294
Jens Axboe22e2c502005-06-27 10:55:12 +0200295#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/*
298 * lots of deadline iosched dupes, can be abstracted later...
299 */
300static inline void cfq_del_crq_hash(struct cfq_rq *crq)
301{
302 hlist_del_init(&crq->hash);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
306{
307 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
310}
311
312static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
313{
314 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
315 struct hlist_node *entry, *next;
316
317 hlist_for_each_safe(entry, next, hash_list) {
318 struct cfq_rq *crq = list_entry_hash(entry);
319 struct request *__rq = crq->request;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!rq_mergeable(__rq)) {
322 cfq_del_crq_hash(crq);
323 continue;
324 }
325
326 if (rq_hash_key(__rq) == offset)
327 return __rq;
328 }
329
330 return NULL;
331}
332
Andrew Morton99f95e52005-06-27 20:14:05 -0700333/*
334 * scheduler run of queue, if there are requests pending and no one in the
335 * driver that will restart queueing
336 */
337static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
338{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100339 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700340 kblockd_schedule_work(&cfqd->unplug_work);
341}
342
343static int cfq_queue_empty(request_queue_t *q)
344{
345 struct cfq_data *cfqd = q->elevator->elevator_data;
346
Jens Axboeb4878f22005-10-20 16:42:29 +0200347 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
351 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
352 * We choose the request that is closest to the head right now. Distance
353 * behind the head are penalized and only allowed to a certain extent.
354 */
355static struct cfq_rq *
356cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
357{
358 sector_t last, s1, s2, d1 = 0, d2 = 0;
359 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
360 unsigned long back_max;
361
362 if (crq1 == NULL || crq1 == crq2)
363 return crq2;
364 if (crq2 == NULL)
365 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200366
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200367 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
368 return crq1;
369 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200370 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 s1 = crq1->request->sector;
373 s2 = crq2->request->sector;
374
375 last = cfqd->last_sector;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * by definition, 1KiB is 2 sectors
379 */
380 back_max = cfqd->cfq_back_max * 2;
381
382 /*
383 * Strict one way elevator _except_ in the case where we allow
384 * short backward seeks which are biased as twice the cost of a
385 * similar forward seek.
386 */
387 if (s1 >= last)
388 d1 = s1 - last;
389 else if (s1 + back_max >= last)
390 d1 = (last - s1) * cfqd->cfq_back_penalty;
391 else
392 r1_wrap = 1;
393
394 if (s2 >= last)
395 d2 = s2 - last;
396 else if (s2 + back_max >= last)
397 d2 = (last - s2) * cfqd->cfq_back_penalty;
398 else
399 r2_wrap = 1;
400
401 /* Found required data */
402 if (!r1_wrap && r2_wrap)
403 return crq1;
404 else if (!r2_wrap && r1_wrap)
405 return crq2;
406 else if (r1_wrap && r2_wrap) {
407 /* both behind the head */
408 if (s1 <= s2)
409 return crq1;
410 else
411 return crq2;
412 }
413
414 /* Both requests in front of the head */
415 if (d1 < d2)
416 return crq1;
417 else if (d2 < d1)
418 return crq2;
419 else {
420 if (s1 >= s2)
421 return crq1;
422 else
423 return crq2;
424 }
425}
426
427/*
428 * would be nice to take fifo expire time into account as well
429 */
430static struct cfq_rq *
431cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
432 struct cfq_rq *last)
433{
434 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
435 struct rb_node *rbnext, *rbprev;
436
Jens Axboeb4878f22005-10-20 16:42:29 +0200437 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200439 if (rbnext == &last->rb_node)
440 rbnext = NULL;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 rbprev = rb_prev(&last->rb_node);
444
445 if (rbprev)
446 crq_prev = rb_entry_crq(rbprev);
447 if (rbnext)
448 crq_next = rb_entry_crq(rbnext);
449
450 return cfq_choose_req(cfqd, crq_next, crq_prev);
451}
452
453static void cfq_update_next_crq(struct cfq_rq *crq)
454{
455 struct cfq_queue *cfqq = crq->cfq_queue;
456
457 if (cfqq->next_crq == crq)
458 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
459}
460
Jens Axboe22e2c502005-06-27 10:55:12 +0200461static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Jens Axboe22e2c502005-06-27 10:55:12 +0200463 struct cfq_data *cfqd = cfqq->cfqd;
464 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Jens Axboe3b181522005-06-27 10:56:24 +0200466 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 list_del(&cfqq->cfq_list);
469
Jens Axboe22e2c502005-06-27 10:55:12 +0200470 if (cfq_class_rt(cfqq))
471 list = &cfqd->cur_rr;
472 else if (cfq_class_idle(cfqq))
473 list = &cfqd->idle_rr;
474 else {
475 /*
476 * if cfqq has requests in flight, don't allow it to be
477 * found in cfq_set_active_queue before it has finished them.
478 * this is done to increase fairness between a process that
479 * has lots of io pending vs one that only generates one
480 * sporadically or synchronously
481 */
Jens Axboe3b181522005-06-27 10:56:24 +0200482 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200483 list = &cfqd->busy_rr;
484 else
485 list = &cfqd->rr_list[cfqq->ioprio];
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200489 * if queue was preempted, just add to front to be fair. busy_rr
490 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200492 if (preempted || list == &cfqd->busy_rr) {
493 list_add(&cfqq->cfq_list, list);
494 return;
495 }
496
497 /*
498 * sort by when queue was last serviced
499 */
500 entry = list;
501 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
503
Jens Axboe22e2c502005-06-27 10:55:12 +0200504 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200506 if (time_before(__cfqq->service_last, cfqq->service_last))
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 list_add(&cfqq->cfq_list, entry);
511}
512
513/*
514 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200515 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200518cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Jens Axboe3b181522005-06-27 10:56:24 +0200520 BUG_ON(cfq_cfqq_on_rr(cfqq));
521 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 cfqd->busy_queues++;
523
Jens Axboeb4878f22005-10-20 16:42:29 +0200524 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527static inline void
528cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
529{
Jens Axboe3b181522005-06-27 10:56:24 +0200530 BUG_ON(!cfq_cfqq_on_rr(cfqq));
531 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200532 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 BUG_ON(!cfqd->busy_queues);
535 cfqd->busy_queues--;
536}
537
538/*
539 * rb tree support functions
540 */
541static inline void cfq_del_crq_rb(struct cfq_rq *crq)
542{
543 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200544 struct cfq_data *cfqd = cfqq->cfqd;
545 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jens Axboeb4878f22005-10-20 16:42:29 +0200547 BUG_ON(!cfqq->queued[sync]);
548 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Jens Axboeb4878f22005-10-20 16:42:29 +0200550 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jens Axboeb4878f22005-10-20 16:42:29 +0200552 rb_erase(&crq->rb_node, &cfqq->sort_list);
553 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jens Axboeb4878f22005-10-20 16:42:29 +0200555 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
556 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static struct cfq_rq *
560__cfq_add_crq_rb(struct cfq_rq *crq)
561{
562 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
563 struct rb_node *parent = NULL;
564 struct cfq_rq *__crq;
565
566 while (*p) {
567 parent = *p;
568 __crq = rb_entry_crq(parent);
569
570 if (crq->rb_key < __crq->rb_key)
571 p = &(*p)->rb_left;
572 else if (crq->rb_key > __crq->rb_key)
573 p = &(*p)->rb_right;
574 else
575 return __crq;
576 }
577
578 rb_link_node(&crq->rb_node, parent, p);
579 return NULL;
580}
581
582static void cfq_add_crq_rb(struct cfq_rq *crq)
583{
584 struct cfq_queue *cfqq = crq->cfq_queue;
585 struct cfq_data *cfqd = cfqq->cfqd;
586 struct request *rq = crq->request;
587 struct cfq_rq *__alias;
588
589 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200590 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * looks a little odd, but the first insert might return an alias.
594 * if that happens, put the alias on the dispatch list
595 */
596 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200597 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
600
Jens Axboe3b181522005-06-27 10:56:24 +0200601 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200602 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /*
605 * check if this request is a better next-serve candidate
606 */
607 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
608}
609
610static inline void
611cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
612{
Jens Axboeb4878f22005-10-20 16:42:29 +0200613 rb_erase(&crq->rb_node, &cfqq->sort_list);
614 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 cfq_add_crq_rb(crq);
617}
618
Jens Axboe22e2c502005-06-27 10:55:12 +0200619static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jens Axboe3b181522005-06-27 10:56:24 +0200622 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct rb_node *n;
624
625 if (!cfqq)
626 goto out;
627
628 n = cfqq->sort_list.rb_node;
629 while (n) {
630 struct cfq_rq *crq = rb_entry_crq(n);
631
632 if (sector < crq->rb_key)
633 n = n->rb_left;
634 else if (sector > crq->rb_key)
635 n = n->rb_right;
636 else
637 return crq->request;
638 }
639
640out:
641 return NULL;
642}
643
Jens Axboeb4878f22005-10-20 16:42:29 +0200644static void cfq_activate_request(request_queue_t *q, struct request *rq)
645{
646 struct cfq_data *cfqd = q->elevator->elevator_data;
647
648 cfqd->rq_in_driver++;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
652{
Jens Axboe22e2c502005-06-27 10:55:12 +0200653 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jens Axboeb4878f22005-10-20 16:42:29 +0200655 WARN_ON(!cfqd->rq_in_driver);
656 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Jens Axboeb4878f22005-10-20 16:42:29 +0200659static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct cfq_rq *crq = RQ_DATA(rq);
662
Jens Axboeb4878f22005-10-20 16:42:29 +0200663 list_del_init(&rq->queuelist);
664 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200665 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static int
669cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
670{
671 struct cfq_data *cfqd = q->elevator->elevator_data;
672 struct request *__rq;
673 int ret;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200676 if (__rq && elv_rq_merge_ok(__rq, bio)) {
677 ret = ELEVATOR_BACK_MERGE;
678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
Jens Axboe22e2c502005-06-27 10:55:12 +0200682 if (__rq && elv_rq_merge_ok(__rq, bio)) {
683 ret = ELEVATOR_FRONT_MERGE;
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 return ELEVATOR_NO_MERGE;
688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *req = __rq;
690 return ret;
691}
692
693static void cfq_merged_request(request_queue_t *q, struct request *req)
694{
695 struct cfq_data *cfqd = q->elevator->elevator_data;
696 struct cfq_rq *crq = RQ_DATA(req);
697
698 cfq_del_crq_hash(crq);
699 cfq_add_crq_hash(cfqd, crq);
700
Jens Axboeb4878f22005-10-20 16:42:29 +0200701 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct cfq_queue *cfqq = crq->cfq_queue;
703
704 cfq_update_next_crq(crq);
705 cfq_reposition_crq_rb(cfqq, crq);
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static void
710cfq_merged_requests(request_queue_t *q, struct request *rq,
711 struct request *next)
712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cfq_merged_request(q, rq);
714
Jens Axboe22e2c502005-06-27 10:55:12 +0200715 /*
716 * reposition in fifo if next is older than rq
717 */
718 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
719 time_before(next->start_time, rq->start_time))
720 list_move(&rq->queuelist, &next->queuelist);
721
Jens Axboeb4878f22005-10-20 16:42:29 +0200722 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200723}
724
725static inline void
726__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
727{
728 if (cfqq) {
729 /*
730 * stop potential idle class queues waiting service
731 */
732 del_timer(&cfqd->idle_class_timer);
733
734 cfqq->slice_start = jiffies;
735 cfqq->slice_end = 0;
736 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200737 cfq_clear_cfqq_must_alloc_slice(cfqq);
738 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200739 }
740
741 cfqd->active_queue = cfqq;
742}
743
744/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100745 * current cfqq expired its slice (or was too idle), select new one
746 */
747static void
748__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
749 int preempted)
750{
751 unsigned long now = jiffies;
752
753 if (cfq_cfqq_wait_request(cfqq))
754 del_timer(&cfqd->idle_slice_timer);
755
756 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
757 cfqq->service_last = now;
758 cfq_schedule_dispatch(cfqd);
759 }
760
761 cfq_clear_cfqq_must_dispatch(cfqq);
762 cfq_clear_cfqq_wait_request(cfqq);
763
764 /*
765 * store what was left of this slice, if the queue idled out
766 * or was preempted
767 */
768 if (time_after(cfqq->slice_end, now))
769 cfqq->slice_left = cfqq->slice_end - now;
770 else
771 cfqq->slice_left = 0;
772
773 if (cfq_cfqq_on_rr(cfqq))
774 cfq_resort_rr_list(cfqq, preempted);
775
776 if (cfqq == cfqd->active_queue)
777 cfqd->active_queue = NULL;
778
779 if (cfqd->active_cic) {
780 put_io_context(cfqd->active_cic->ioc);
781 cfqd->active_cic = NULL;
782 }
783
784 cfqd->dispatch_slice = 0;
785}
786
787static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
788{
789 struct cfq_queue *cfqq = cfqd->active_queue;
790
791 if (cfqq)
792 __cfq_slice_expired(cfqd, cfqq, preempted);
793}
794
795/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200796 * 0
797 * 0,1
798 * 0,1,2
799 * 0,1,2,3
800 * 0,1,2,3,4
801 * 0,1,2,3,4,5
802 * 0,1,2,3,4,5,6
803 * 0,1,2,3,4,5,6,7
804 */
805static int cfq_get_next_prio_level(struct cfq_data *cfqd)
806{
807 int prio, wrap;
808
809 prio = -1;
810 wrap = 0;
811 do {
812 int p;
813
814 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
815 if (!list_empty(&cfqd->rr_list[p])) {
816 prio = p;
817 break;
818 }
819 }
820
821 if (prio != -1)
822 break;
823 cfqd->cur_prio = 0;
824 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
825 cfqd->cur_end_prio = 0;
826 if (wrap)
827 break;
828 wrap = 1;
829 }
830 } while (1);
831
832 if (unlikely(prio == -1))
833 return -1;
834
835 BUG_ON(prio >= CFQ_PRIO_LISTS);
836
837 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
838
839 cfqd->cur_prio = prio + 1;
840 if (cfqd->cur_prio > cfqd->cur_end_prio) {
841 cfqd->cur_end_prio = cfqd->cur_prio;
842 cfqd->cur_prio = 0;
843 }
844 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
845 cfqd->cur_prio = 0;
846 cfqd->cur_end_prio = 0;
847 }
848
849 return prio;
850}
851
Jens Axboe3b181522005-06-27 10:56:24 +0200852static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200853{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100854 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200855
856 /*
857 * if current list is non-empty, grab first entry. if it is empty,
858 * get next prio level and grab first entry then if any are spliced
859 */
860 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
861 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
862
863 /*
864 * if we have idle queues and no rt or be queues had pending
865 * requests, either allow immediate service if the grace period
866 * has passed or arm the idle grace timer
867 */
868 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
869 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
870
871 if (time_after_eq(jiffies, end))
872 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
873 else
874 mod_timer(&cfqd->idle_class_timer, end);
875 }
876
877 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200878 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200879}
880
Jens Axboe22e2c502005-06-27 10:55:12 +0200881static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
882
883{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100884 unsigned long sl;
885
Jens Axboe22e2c502005-06-27 10:55:12 +0200886 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
887 WARN_ON(cfqq != cfqd->active_queue);
888
889 /*
890 * idle is disabled, either manually or by past process history
891 */
892 if (!cfqd->cfq_slice_idle)
893 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200894 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200895 return 0;
896 /*
897 * task has exited, don't wait
898 */
899 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
900 return 0;
901
Jens Axboe3b181522005-06-27 10:56:24 +0200902 cfq_mark_cfqq_must_dispatch(cfqq);
903 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200904
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100905 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
906 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200907 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Jens Axboeb4878f22005-10-20 16:42:29 +0200910static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct cfq_data *cfqd = q->elevator->elevator_data;
913 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200914
915 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200916 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200917 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200918 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
922 * return expired entry, or NULL to just start from scratch in rbtree
923 */
924static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
925{
926 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct cfq_rq *crq;
929
Jens Axboe3b181522005-06-27 10:56:24 +0200930 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return NULL;
932
Jens Axboe22e2c502005-06-27 10:55:12 +0200933 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200934 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Jens Axboe22e2c502005-06-27 10:55:12 +0200936 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
937 rq = crq->request;
938 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200939 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200940 return crq;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944 return NULL;
945}
946
947/*
Jens Axboe3b181522005-06-27 10:56:24 +0200948 * Scale schedule slice based on io priority. Use the sync time slice only
949 * if a queue is marked sync and has sync io queued. A sync queue with async
950 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200952static inline int
953cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jens Axboe22e2c502005-06-27 10:55:12 +0200955 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jens Axboe22e2c502005-06-27 10:55:12 +0200957 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jens Axboe22e2c502005-06-27 10:55:12 +0200959 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Jens Axboe22e2c502005-06-27 10:55:12 +0200962static inline void
963cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
964{
965 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
966}
967
968static inline int
969cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
970{
971 const int base_rq = cfqd->cfq_slice_async_rq;
972
973 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
974
975 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
976}
977
978/*
979 * get next queue for service
980 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100981static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200982{
983 unsigned long now = jiffies;
984 struct cfq_queue *cfqq;
985
986 cfqq = cfqd->active_queue;
987 if (!cfqq)
988 goto new_queue;
989
990 /*
991 * slice has expired
992 */
Jens Axboe3b181522005-06-27 10:56:24 +0200993 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
994 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200995
996 /*
997 * if queue has requests, dispatch one. if not, check if
998 * enough slice is left to wait for one
999 */
1000 if (!RB_EMPTY(&cfqq->sort_list))
1001 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001002 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001003 time_before(now, cfqq->slice_end)) {
1004 if (cfq_arm_slice_timer(cfqd, cfqq))
1005 return NULL;
1006 }
1007
Jens Axboe3b181522005-06-27 10:56:24 +02001008expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001009 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001010new_queue:
1011 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001012keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001013 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001014}
1015
1016static int
1017__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1018 int max_dispatch)
1019{
1020 int dispatched = 0;
1021
1022 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1023
1024 do {
1025 struct cfq_rq *crq;
1026
1027 /*
1028 * follow expired path, else get first next available
1029 */
1030 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1031 crq = cfqq->next_crq;
1032
1033 /*
1034 * finally, insert request into driver dispatch list
1035 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001036 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001037
1038 cfqd->dispatch_slice++;
1039 dispatched++;
1040
1041 if (!cfqd->active_cic) {
1042 atomic_inc(&crq->io_context->ioc->refcount);
1043 cfqd->active_cic = crq->io_context;
1044 }
1045
1046 if (RB_EMPTY(&cfqq->sort_list))
1047 break;
1048
1049 } while (dispatched < max_dispatch);
1050
1051 /*
1052 * if slice end isn't set yet, set it. if at least one request was
1053 * sync, use the sync time slice value
1054 */
1055 if (!cfqq->slice_end)
1056 cfq_set_prio_slice(cfqd, cfqq);
1057
1058 /*
1059 * expire an async queue immediately if it has used up its slice. idle
1060 * queue always expire after 1 dispatch round.
1061 */
1062 if ((!cfq_cfqq_sync(cfqq) &&
1063 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1064 cfq_class_idle(cfqq))
1065 cfq_slice_expired(cfqd, 0);
1066
1067 return dispatched;
1068}
1069
1070static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001071cfq_forced_dispatch_cfqqs(struct list_head *list)
1072{
1073 int dispatched = 0;
1074 struct cfq_queue *cfqq, *next;
1075 struct cfq_rq *crq;
1076
1077 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1078 while ((crq = cfqq->next_crq)) {
1079 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1080 dispatched++;
1081 }
1082 BUG_ON(!list_empty(&cfqq->fifo));
1083 }
1084 return dispatched;
1085}
1086
1087static int
1088cfq_forced_dispatch(struct cfq_data *cfqd)
1089{
1090 int i, dispatched = 0;
1091
1092 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1093 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1094
1095 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1096 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1097 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1098
1099 cfq_slice_expired(cfqd, 0);
1100
1101 BUG_ON(cfqd->busy_queues);
1102
1103 return dispatched;
1104}
1105
1106static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001107cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct cfq_data *cfqd = q->elevator->elevator_data;
1110 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Jens Axboe22e2c502005-06-27 10:55:12 +02001112 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001115 if (unlikely(force))
1116 return cfq_forced_dispatch(cfqd);
1117
1118 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001119 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001120 int max_dispatch;
1121
1122 /*
1123 * if idle window is disabled, allow queue buildup
1124 */
1125 if (!cfq_cfqq_idle_window(cfqq) &&
1126 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1127 return 0;
1128
Jens Axboe3b181522005-06-27 10:56:24 +02001129 cfq_clear_cfqq_must_dispatch(cfqq);
1130 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001131 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001133 max_dispatch = cfqd->cfq_quantum;
1134 if (cfq_class_idle(cfqq))
1135 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jens Axboe22e2c502005-06-27 10:55:12 +02001137 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Jens Axboe22e2c502005-06-27 10:55:12 +02001140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * task holds one reference to the queue, dropped when task exits. each crq
1145 * in-flight on this queue also holds a reference, dropped when crq is freed.
1146 *
1147 * queue lock must be held here.
1148 */
1149static void cfq_put_queue(struct cfq_queue *cfqq)
1150{
Jens Axboe22e2c502005-06-27 10:55:12 +02001151 struct cfq_data *cfqd = cfqq->cfqd;
1152
1153 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 if (!atomic_dec_and_test(&cfqq->ref))
1156 return;
1157
1158 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001159 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001160 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001162 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001163 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 cfq_put_cfqd(cfqq->cfqd);
1166
1167 /*
1168 * it's on the empty list and still hashed
1169 */
1170 list_del(&cfqq->cfq_list);
1171 hlist_del(&cfqq->cfq_hash);
1172 kmem_cache_free(cfq_pool, cfqq);
1173}
1174
1175static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001176__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1177 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1180 struct hlist_node *entry, *next;
1181
1182 hlist_for_each_safe(entry, next, hash_list) {
1183 struct cfq_queue *__cfqq = list_entry_qhash(entry);
Al Virob0a69162006-03-14 15:32:50 -05001184 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jens Axboe3b181522005-06-27 10:56:24 +02001186 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return __cfqq;
1188 }
1189
1190 return NULL;
1191}
1192
1193static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001194cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Jens Axboe3b181522005-06-27 10:56:24 +02001196 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199static void cfq_free_io_context(struct cfq_io_context *cic)
1200{
Jens Axboe22e2c502005-06-27 10:55:12 +02001201 struct cfq_io_context *__cic;
1202 struct list_head *entry, *next;
1203
1204 list_for_each_safe(entry, next, &cic->list) {
1205 __cic = list_entry(entry, struct cfq_io_context, list);
1206 kmem_cache_free(cfq_ioc_pool, __cic);
1207 }
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 kmem_cache_free(cfq_ioc_pool, cic);
1210}
1211
1212/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001213 * Called with interrupts disabled
1214 */
1215static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1216{
Al Viro478a82b2006-03-18 13:25:24 -05001217 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001218 request_queue_t *q = cfqd->queue;
1219
1220 WARN_ON(!irqs_disabled());
1221
1222 spin_lock(q->queue_lock);
1223
Al Viro12a05732006-03-18 13:38:01 -05001224 if (cic->cfqq[ASYNC]) {
1225 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1226 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1227 cfq_put_queue(cic->cfqq[ASYNC]);
1228 cic->cfqq[ASYNC] = NULL;
1229 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001230
Al Viro12a05732006-03-18 13:38:01 -05001231 if (cic->cfqq[SYNC]) {
1232 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1233 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1234 cfq_put_queue(cic->cfqq[SYNC]);
1235 cic->cfqq[SYNC] = NULL;
1236 }
1237
Al Viro478a82b2006-03-18 13:25:24 -05001238 cic->key = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001239 spin_unlock(q->queue_lock);
1240}
1241
1242/*
1243 * Another task may update the task cic list, if it is doing a queue lookup
1244 * on its behalf. cfq_cic_lock excludes such concurrent updates
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
1246static void cfq_exit_io_context(struct cfq_io_context *cic)
1247{
Jens Axboe22e2c502005-06-27 10:55:12 +02001248 struct cfq_io_context *__cic;
1249 struct list_head *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 unsigned long flags;
1251
Jens Axboe22e2c502005-06-27 10:55:12 +02001252 local_irq_save(flags);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * put the reference this task is holding to the various queues
1256 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001257 list_for_each(entry, &cic->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 __cic = list_entry(entry, struct cfq_io_context, list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001259 cfq_exit_single_io_context(__cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Jens Axboe22e2c502005-06-27 10:55:12 +02001262 cfq_exit_single_io_context(cic);
1263 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Jens Axboe22e2c502005-06-27 10:55:12 +02001266static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001267cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Jens Axboe22e2c502005-06-27 10:55:12 +02001269 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 if (cic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 INIT_LIST_HEAD(&cic->list);
Al Viro12a05732006-03-18 13:38:01 -05001273 cic->cfqq[ASYNC] = NULL;
1274 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001275 cic->key = NULL;
1276 cic->last_end_request = jiffies;
1277 cic->ttime_total = 0;
1278 cic->ttime_samples = 0;
1279 cic->ttime_mean = 0;
1280 cic->dtor = cfq_free_io_context;
1281 cic->exit = cfq_exit_io_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 return cic;
1285}
1286
Jens Axboe22e2c502005-06-27 10:55:12 +02001287static void cfq_init_prio_data(struct cfq_queue *cfqq)
1288{
1289 struct task_struct *tsk = current;
1290 int ioprio_class;
1291
Jens Axboe3b181522005-06-27 10:56:24 +02001292 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001293 return;
1294
1295 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1296 switch (ioprio_class) {
1297 default:
1298 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1299 case IOPRIO_CLASS_NONE:
1300 /*
1301 * no prio set, place us in the middle of the BE classes
1302 */
1303 cfqq->ioprio = task_nice_ioprio(tsk);
1304 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1305 break;
1306 case IOPRIO_CLASS_RT:
1307 cfqq->ioprio = task_ioprio(tsk);
1308 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1309 break;
1310 case IOPRIO_CLASS_BE:
1311 cfqq->ioprio = task_ioprio(tsk);
1312 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1313 break;
1314 case IOPRIO_CLASS_IDLE:
1315 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1316 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001317 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001318 break;
1319 }
1320
1321 /*
1322 * keep track of original prio settings in case we have to temporarily
1323 * elevate the priority of this queue
1324 */
1325 cfqq->org_ioprio = cfqq->ioprio;
1326 cfqq->org_ioprio_class = cfqq->ioprio_class;
1327
Jens Axboe3b181522005-06-27 10:56:24 +02001328 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001329 cfq_resort_rr_list(cfqq, 0);
1330
Jens Axboe3b181522005-06-27 10:56:24 +02001331 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001332}
1333
Al Viro478a82b2006-03-18 13:25:24 -05001334static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001335{
Al Viro478a82b2006-03-18 13:25:24 -05001336 struct cfq_data *cfqd = cic->key;
1337 struct cfq_queue *cfqq;
1338 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001339 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001340 cfqq = cic->cfqq[ASYNC];
1341 if (cfqq) {
1342 cfq_mark_cfqq_prio_changed(cfqq);
1343 cfq_init_prio_data(cfqq);
1344 }
1345 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001346 if (cfqq) {
1347 cfq_mark_cfqq_prio_changed(cfqq);
1348 cfq_init_prio_data(cfqq);
1349 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001350 spin_unlock(cfqd->queue->queue_lock);
1351 }
1352}
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001355 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001357static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Al Viroa6a07632006-03-18 13:26:44 -05001359 struct cfq_io_context *cic;
1360
1361 write_lock(&cfq_exit_lock);
1362
1363 cic = ioc->cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Al Viro478a82b2006-03-18 13:25:24 -05001365 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jens Axboe22e2c502005-06-27 10:55:12 +02001367 list_for_each_entry(cic, &cic->list, list)
Al Viro478a82b2006-03-18 13:25:24 -05001368 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Al Viroa6a07632006-03-18 13:26:44 -05001370 write_unlock(&cfq_exit_lock);
1371
Jens Axboe22e2c502005-06-27 10:55:12 +02001372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001376cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
Al Viro8267e262005-10-21 03:20:53 -04001377 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1380 struct cfq_queue *cfqq, *new_cfqq = NULL;
1381
1382retry:
Jens Axboe3b181522005-06-27 10:56:24 +02001383 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (!cfqq) {
1386 if (new_cfqq) {
1387 cfqq = new_cfqq;
1388 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001389 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 spin_unlock_irq(cfqd->queue->queue_lock);
1391 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1392 spin_lock_irq(cfqd->queue->queue_lock);
1393 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001394 } else {
1395 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1396 if (!cfqq)
1397 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 memset(cfqq, 0, sizeof(*cfqq));
1401
1402 INIT_HLIST_NODE(&cfqq->cfq_hash);
1403 INIT_LIST_HEAD(&cfqq->cfq_list);
1404 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001405 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 cfqq->key = key;
1408 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1409 atomic_set(&cfqq->ref, 0);
1410 cfqq->cfqd = cfqd;
1411 atomic_inc(&cfqd->ref);
Jens Axboe22e2c502005-06-27 10:55:12 +02001412 cfqq->service_last = 0;
1413 /*
1414 * set ->slice_left to allow preemption for a new process
1415 */
1416 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001417 cfq_mark_cfqq_idle_window(cfqq);
1418 cfq_mark_cfqq_prio_changed(cfqq);
1419 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
1422 if (new_cfqq)
1423 kmem_cache_free(cfq_pool, new_cfqq);
1424
1425 atomic_inc(&cfqq->ref);
1426out:
1427 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1428 return cfqq;
1429}
1430
Jens Axboe22e2c502005-06-27 10:55:12 +02001431/*
1432 * Setup general io context and cfq io context. There can be several cfq
1433 * io contexts per general io context, if this process is doing io to more
1434 * than one device managed by cfq. Note that caller is holding a reference to
1435 * cfqq, so we don't need to worry about it disappearing
1436 */
1437static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001438cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Jens Axboe22e2c502005-06-27 10:55:12 +02001440 struct io_context *ioc = NULL;
1441 struct cfq_io_context *cic;
1442
1443 might_sleep_if(gfp_mask & __GFP_WAIT);
1444
1445 ioc = get_io_context(gfp_mask);
1446 if (!ioc)
1447 return NULL;
1448
1449 if ((cic = ioc->cic) == NULL) {
1450 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1451
1452 if (cic == NULL)
1453 goto err;
1454
1455 /*
1456 * manually increment generic io_context usage count, it
1457 * cannot go away since we are already holding one ref to it
1458 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001459 cic->ioc = ioc;
1460 cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001461 read_lock(&cfq_exit_lock);
Al Viro478a82b2006-03-18 13:25:24 -05001462 ioc->set_ioprio = cfq_ioc_set_ioprio;
1463 ioc->cic = cic;
Al Viroa6a07632006-03-18 13:26:44 -05001464 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001465 } else {
1466 struct cfq_io_context *__cic;
1467
1468 /*
1469 * the first cic on the list is actually the head itself
1470 */
1471 if (cic->key == cfqd)
1472 goto out;
1473
1474 /*
1475 * cic exists, check if we already are there. linear search
1476 * should be ok here, the list will usually not be more than
1477 * 1 or a few entries long
1478 */
1479 list_for_each_entry(__cic, &cic->list, list) {
1480 /*
1481 * this process is already holding a reference to
1482 * this queue, so no need to get one more
1483 */
1484 if (__cic->key == cfqd) {
1485 cic = __cic;
1486 goto out;
1487 }
1488 }
1489
1490 /*
1491 * nope, process doesn't have a cic assoicated with this
1492 * cfqq yet. get a new one and add to list
1493 */
1494 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1495 if (__cic == NULL)
1496 goto err;
1497
1498 __cic->ioc = ioc;
1499 __cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001500 read_lock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001501 list_add(&__cic->list, &cic->list);
Al Viroa6a07632006-03-18 13:26:44 -05001502 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001503 cic = __cic;
1504 }
1505
1506out:
1507 return cic;
1508err:
1509 put_io_context(ioc);
1510 return NULL;
1511}
1512
1513static void
1514cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1515{
1516 unsigned long elapsed, ttime;
1517
1518 /*
1519 * if this context already has stuff queued, thinktime is from
1520 * last queue not last end
1521 */
1522#if 0
1523 if (time_after(cic->last_end_request, cic->last_queue))
1524 elapsed = jiffies - cic->last_end_request;
1525 else
1526 elapsed = jiffies - cic->last_queue;
1527#else
1528 elapsed = jiffies - cic->last_end_request;
1529#endif
1530
1531 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1532
1533 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1534 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1535 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1536}
1537
1538#define sample_valid(samples) ((samples) > 80)
1539
1540/*
1541 * Disable idle window if the process thinks too long or seeks so much that
1542 * it doesn't matter
1543 */
1544static void
1545cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1546 struct cfq_io_context *cic)
1547{
Jens Axboe3b181522005-06-27 10:56:24 +02001548 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001549
1550 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1551 enable_idle = 0;
1552 else if (sample_valid(cic->ttime_samples)) {
1553 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1554 enable_idle = 0;
1555 else
1556 enable_idle = 1;
1557 }
1558
Jens Axboe3b181522005-06-27 10:56:24 +02001559 if (enable_idle)
1560 cfq_mark_cfqq_idle_window(cfqq);
1561 else
1562 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001563}
1564
1565
1566/*
1567 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1568 * no or if we aren't sure, a 1 will cause a preempt.
1569 */
1570static int
1571cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1572 struct cfq_rq *crq)
1573{
1574 struct cfq_queue *cfqq = cfqd->active_queue;
1575
1576 if (cfq_class_idle(new_cfqq))
1577 return 0;
1578
1579 if (!cfqq)
1580 return 1;
1581
1582 if (cfq_class_idle(cfqq))
1583 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001584 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001585 return 0;
1586 /*
1587 * if it doesn't have slice left, forget it
1588 */
1589 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1590 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001591 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001592 return 1;
1593
1594 return 0;
1595}
1596
1597/*
1598 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1599 * let it have half of its nominal slice.
1600 */
1601static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1602{
1603 struct cfq_queue *__cfqq, *next;
1604
1605 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1606 cfq_resort_rr_list(__cfqq, 1);
1607
1608 if (!cfqq->slice_left)
1609 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1610
1611 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001612 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001613 __cfq_set_active_queue(cfqd, cfqq);
1614}
1615
1616/*
1617 * should really be a ll_rw_blk.c helper
1618 */
1619static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1620{
1621 request_queue_t *q = cfqd->queue;
1622
1623 if (!blk_queue_plugged(q))
1624 q->request_fn(q);
1625 else
1626 __generic_unplug_device(q);
1627}
1628
1629/*
1630 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1631 * something we should do about it
1632 */
1633static void
1634cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1635 struct cfq_rq *crq)
1636{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001637 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001638
1639 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1640
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001641 /*
1642 * we never wait for an async request and we don't allow preemption
1643 * of an async request. so just return early
1644 */
1645 if (!cfq_crq_is_sync(crq))
1646 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001647
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001648 cic = crq->io_context;
Jens Axboe22e2c502005-06-27 10:55:12 +02001649
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001650 cfq_update_io_thinktime(cfqd, cic);
1651 cfq_update_idle_window(cfqd, cfqq, cic);
1652
1653 cic->last_queue = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001654
1655 if (cfqq == cfqd->active_queue) {
1656 /*
1657 * if we are waiting for a request for this queue, let it rip
1658 * immediately and flag that we must not expire this queue
1659 * just now
1660 */
Jens Axboe3b181522005-06-27 10:56:24 +02001661 if (cfq_cfqq_wait_request(cfqq)) {
1662 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001663 del_timer(&cfqd->idle_slice_timer);
1664 cfq_start_queueing(cfqd, cfqq);
1665 }
1666 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1667 /*
1668 * not the active queue - expire current slice if it is
1669 * idle and has expired it's mean thinktime or this new queue
1670 * has some old slice time left and is of higher priority
1671 */
1672 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001673 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001674 cfq_start_queueing(cfqd, cfqq);
1675 }
1676}
1677
Jens Axboeb4878f22005-10-20 16:42:29 +02001678static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001679{
Jens Axboeb4878f22005-10-20 16:42:29 +02001680 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001681 struct cfq_rq *crq = RQ_DATA(rq);
1682 struct cfq_queue *cfqq = crq->cfq_queue;
1683
1684 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Jens Axboe22e2c502005-06-27 10:55:12 +02001688 list_add_tail(&rq->queuelist, &cfqq->fifo);
1689
Tejun Heo98b11472005-10-20 16:46:54 +02001690 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001691 cfq_add_crq_hash(cfqd, crq);
1692
Jens Axboe22e2c502005-06-27 10:55:12 +02001693 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static void cfq_completed_request(request_queue_t *q, struct request *rq)
1697{
1698 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001699 struct cfq_queue *cfqq = crq->cfq_queue;
1700 struct cfq_data *cfqd = cfqq->cfqd;
1701 const int sync = cfq_crq_is_sync(crq);
1702 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jens Axboeb4878f22005-10-20 16:42:29 +02001704 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jens Axboeb4878f22005-10-20 16:42:29 +02001706 WARN_ON(!cfqd->rq_in_driver);
1707 WARN_ON(!cfqq->on_dispatch[sync]);
1708 cfqd->rq_in_driver--;
1709 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jens Axboeb4878f22005-10-20 16:42:29 +02001711 if (!cfq_class_idle(cfqq))
1712 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001713
Jens Axboeb4878f22005-10-20 16:42:29 +02001714 if (!cfq_cfqq_dispatched(cfqq)) {
1715 if (cfq_cfqq_on_rr(cfqq)) {
1716 cfqq->service_last = now;
1717 cfq_resort_rr_list(cfqq, 0);
1718 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001719 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721
Jens Axboeb4878f22005-10-20 16:42:29 +02001722 if (cfq_crq_is_sync(crq))
1723 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static struct request *
1727cfq_former_request(request_queue_t *q, struct request *rq)
1728{
1729 struct cfq_rq *crq = RQ_DATA(rq);
1730 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1731
1732 if (rbprev)
1733 return rb_entry_crq(rbprev)->request;
1734
1735 return NULL;
1736}
1737
1738static struct request *
1739cfq_latter_request(request_queue_t *q, struct request *rq)
1740{
1741 struct cfq_rq *crq = RQ_DATA(rq);
1742 struct rb_node *rbnext = rb_next(&crq->rb_node);
1743
1744 if (rbnext)
1745 return rb_entry_crq(rbnext)->request;
1746
1747 return NULL;
1748}
1749
Jens Axboe22e2c502005-06-27 10:55:12 +02001750/*
1751 * we temporarily boost lower priority queues if they are holding fs exclusive
1752 * resources. they are boosted to normal prio (CLASS_BE/4)
1753 */
1754static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Jens Axboe22e2c502005-06-27 10:55:12 +02001756 const int ioprio_class = cfqq->ioprio_class;
1757 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Jens Axboe22e2c502005-06-27 10:55:12 +02001759 if (has_fs_excl()) {
1760 /*
1761 * boost idle prio on transactions that would lock out other
1762 * users of the filesystem
1763 */
1764 if (cfq_class_idle(cfqq))
1765 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1766 if (cfqq->ioprio > IOPRIO_NORM)
1767 cfqq->ioprio = IOPRIO_NORM;
1768 } else {
1769 /*
1770 * check if we need to unboost the queue
1771 */
1772 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1773 cfqq->ioprio_class = cfqq->org_ioprio_class;
1774 if (cfqq->ioprio != cfqq->org_ioprio)
1775 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
Jens Axboe22e2c502005-06-27 10:55:12 +02001778 /*
1779 * refile between round-robin lists if we moved the priority class
1780 */
1781 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001782 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001783 cfq_resort_rr_list(cfqq, 0);
1784}
1785
1786static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1787{
1788 if (rw == READ || process_sync(task))
1789 return task->pid;
1790
1791 return CFQ_KEY_ASYNC;
1792}
1793
1794static inline int
1795__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1796 struct task_struct *task, int rw)
1797{
Jens Axboe3b181522005-06-27 10:56:24 +02001798#if 1
1799 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001800 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001801 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001802 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001803 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001804
1805 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001806#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001807 if (!cfqq || task->flags & PF_MEMALLOC)
1808 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001809 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1810 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001811 return ELV_MQUEUE_MUST;
1812
1813 /*
1814 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1815 * can quickly flood the queue with writes from a single task
1816 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001817 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001818 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001819 return ELV_MQUEUE_MUST;
1820 }
1821
1822 return ELV_MQUEUE_MAY;
1823 }
1824 if (cfq_class_idle(cfqq))
1825 return ELV_MQUEUE_NO;
1826 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1827 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1828 int ret = ELV_MQUEUE_NO;
1829
1830 if (ioc && ioc->nr_batch_requests)
1831 ret = ELV_MQUEUE_MAY;
1832
1833 put_io_context(ioc);
1834 return ret;
1835 }
1836
1837 return ELV_MQUEUE_MAY;
1838#endif
1839}
1840
1841static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1842{
1843 struct cfq_data *cfqd = q->elevator->elevator_data;
1844 struct task_struct *tsk = current;
1845 struct cfq_queue *cfqq;
1846
1847 /*
1848 * don't force setup of a queue from here, as a call to may_queue
1849 * does not necessarily imply that a request actually will be queued.
1850 * so just lookup a possibly existing queue, or return 'may queue'
1851 * if that fails
1852 */
Jens Axboe3b181522005-06-27 10:56:24 +02001853 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001854 if (cfqq) {
1855 cfq_init_prio_data(cfqq);
1856 cfq_prio_boost(cfqq);
1857
1858 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1859 }
1860
1861 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1865{
Jens Axboe22e2c502005-06-27 10:55:12 +02001866 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jens Axboe22e2c502005-06-27 10:55:12 +02001869 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1870 smp_mb();
1871 if (waitqueue_active(&rl->wait[READ]))
1872 wake_up(&rl->wait[READ]);
1873 }
1874
1875 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1876 smp_mb();
1877 if (waitqueue_active(&rl->wait[WRITE]))
1878 wake_up(&rl->wait[WRITE]);
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882/*
1883 * queue lock held here
1884 */
1885static void cfq_put_request(request_queue_t *q, struct request *rq)
1886{
1887 struct cfq_data *cfqd = q->elevator->elevator_data;
1888 struct cfq_rq *crq = RQ_DATA(rq);
1889
1890 if (crq) {
1891 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001892 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Jens Axboe22e2c502005-06-27 10:55:12 +02001894 BUG_ON(!cfqq->allocated[rw]);
1895 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Jens Axboe22e2c502005-06-27 10:55:12 +02001897 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 mempool_free(crq, cfqd->crq_pool);
1900 rq->elevator_private = NULL;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 cfq_check_waiters(q, cfqq);
1903 cfq_put_queue(cfqq);
1904 }
1905}
1906
1907/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001908 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001910static int
1911cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001912 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001915 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct cfq_io_context *cic;
1917 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001918 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001919 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct cfq_rq *crq;
1921 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001922 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 might_sleep_if(gfp_mask & __GFP_WAIT);
1925
Jens Axboe3b181522005-06-27 10:56:24 +02001926 cic = cfq_get_io_context(cfqd, key, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 spin_lock_irqsave(q->queue_lock, flags);
1929
Jens Axboe22e2c502005-06-27 10:55:12 +02001930 if (!cic)
1931 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Al Viro12a05732006-03-18 13:38:01 -05001933 if (!cic->cfqq[is_sync]) {
Jens Axboe3b181522005-06-27 10:56:24 +02001934 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001935 if (!cfqq)
1936 goto queue_fail;
1937
Al Viro12a05732006-03-18 13:38:01 -05001938 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001939 } else
Al Viro12a05732006-03-18 13:38:01 -05001940 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001943 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001944 cfqd->rq_starved = 0;
1945 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 spin_unlock_irqrestore(q->queue_lock, flags);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1949 if (crq) {
1950 RB_CLEAR(&crq->rb_node);
1951 crq->rb_key = 0;
1952 crq->request = rq;
1953 INIT_HLIST_NODE(&crq->hash);
1954 crq->cfq_queue = cfqq;
1955 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001956
Al Viro12a05732006-03-18 13:38:01 -05001957 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02001958 cfq_mark_crq_is_sync(crq);
1959 else
1960 cfq_clear_crq_is_sync(crq);
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964 }
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 spin_lock_irqsave(q->queue_lock, flags);
1967 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02001968 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02001969 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001971queue_fail:
1972 if (cic)
1973 put_io_context(cic->ioc);
1974 /*
1975 * mark us rq allocation starved. we need to kickstart the process
1976 * ourselves if there are no pending requests that can do it for us.
1977 * that would be an extremely rare OOM situation
1978 */
1979 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001980 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 spin_unlock_irqrestore(q->queue_lock, flags);
1982 return 1;
1983}
1984
Jens Axboe22e2c502005-06-27 10:55:12 +02001985static void cfq_kick_queue(void *data)
1986{
1987 request_queue_t *q = data;
1988 struct cfq_data *cfqd = q->elevator->elevator_data;
1989 unsigned long flags;
1990
1991 spin_lock_irqsave(q->queue_lock, flags);
1992
1993 if (cfqd->rq_starved) {
1994 struct request_list *rl = &q->rq;
1995
1996 /*
1997 * we aren't guaranteed to get a request after this, but we
1998 * have to be opportunistic
1999 */
2000 smp_mb();
2001 if (waitqueue_active(&rl->wait[READ]))
2002 wake_up(&rl->wait[READ]);
2003 if (waitqueue_active(&rl->wait[WRITE]))
2004 wake_up(&rl->wait[WRITE]);
2005 }
2006
2007 blk_remove_plug(q);
2008 q->request_fn(q);
2009 spin_unlock_irqrestore(q->queue_lock, flags);
2010}
2011
2012/*
2013 * Timer running if the active_queue is currently idling inside its time slice
2014 */
2015static void cfq_idle_slice_timer(unsigned long data)
2016{
2017 struct cfq_data *cfqd = (struct cfq_data *) data;
2018 struct cfq_queue *cfqq;
2019 unsigned long flags;
2020
2021 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2022
2023 if ((cfqq = cfqd->active_queue) != NULL) {
2024 unsigned long now = jiffies;
2025
2026 /*
2027 * expired
2028 */
2029 if (time_after(now, cfqq->slice_end))
2030 goto expire;
2031
2032 /*
2033 * only expire and reinvoke request handler, if there are
2034 * other queues with pending requests
2035 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002036 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002037 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2038 add_timer(&cfqd->idle_slice_timer);
2039 goto out_cont;
2040 }
2041
2042 /*
2043 * not expired and it has a request pending, let it dispatch
2044 */
2045 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002046 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002047 goto out_kick;
2048 }
2049 }
2050expire:
2051 cfq_slice_expired(cfqd, 0);
2052out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002053 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002054out_cont:
2055 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2056}
2057
2058/*
2059 * Timer running if an idle class queue is waiting for service
2060 */
2061static void cfq_idle_class_timer(unsigned long data)
2062{
2063 struct cfq_data *cfqd = (struct cfq_data *) data;
2064 unsigned long flags, end;
2065
2066 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2067
2068 /*
2069 * race with a non-idle queue, reset timer
2070 */
2071 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2072 if (!time_after_eq(jiffies, end)) {
2073 cfqd->idle_class_timer.expires = end;
2074 add_timer(&cfqd->idle_class_timer);
2075 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002076 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002077
2078 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2079}
2080
Jens Axboe3b181522005-06-27 10:56:24 +02002081static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2082{
2083 del_timer_sync(&cfqd->idle_slice_timer);
2084 del_timer_sync(&cfqd->idle_class_timer);
2085 blk_sync_queue(cfqd->queue);
2086}
Jens Axboe22e2c502005-06-27 10:55:12 +02002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static void cfq_put_cfqd(struct cfq_data *cfqd)
2089{
2090 request_queue_t *q = cfqd->queue;
2091
2092 if (!atomic_dec_and_test(&cfqd->ref))
2093 return;
2094
Jens Axboe96c51ce2005-06-27 14:49:39 +02002095 cfq_shutdown_timer_wq(cfqd);
Jens Axboe4fc20742005-10-31 13:51:33 +01002096 blk_put_queue(q);
Jens Axboe96c51ce2005-06-27 14:49:39 +02002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 mempool_destroy(cfqd->crq_pool);
2099 kfree(cfqd->crq_hash);
2100 kfree(cfqd->cfq_hash);
2101 kfree(cfqd);
2102}
2103
2104static void cfq_exit_queue(elevator_t *e)
2105{
Jens Axboe22e2c502005-06-27 10:55:12 +02002106 struct cfq_data *cfqd = e->elevator_data;
2107
Jens Axboe3b181522005-06-27 10:56:24 +02002108 cfq_shutdown_timer_wq(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002109 cfq_put_cfqd(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2113{
2114 struct cfq_data *cfqd;
2115 int i;
2116
2117 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2118 if (!cfqd)
2119 return -ENOMEM;
2120
2121 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002122
2123 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2124 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2125
2126 INIT_LIST_HEAD(&cfqd->busy_rr);
2127 INIT_LIST_HEAD(&cfqd->cur_rr);
2128 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 INIT_LIST_HEAD(&cfqd->empty_list);
2130
2131 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2132 if (!cfqd->crq_hash)
2133 goto out_crqhash;
2134
2135 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2136 if (!cfqd->cfq_hash)
2137 goto out_cfqhash;
2138
2139 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2140 if (!cfqd->crq_pool)
2141 goto out_crqpool;
2142
2143 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2144 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2145 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2146 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2147
2148 e->elevator_data = cfqd;
2149
2150 cfqd->queue = q;
Jens Axboe35797132005-09-10 14:17:10 +02002151 atomic_inc(&q->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Jens Axboe22e2c502005-06-27 10:55:12 +02002153 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002155
2156 init_timer(&cfqd->idle_slice_timer);
2157 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2158 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2159
2160 init_timer(&cfqd->idle_class_timer);
2161 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2162 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2163
2164 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 atomic_set(&cfqd->ref, 1);
2167
2168 cfqd->cfq_queued = cfq_queued;
2169 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002170 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2171 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 cfqd->cfq_back_max = cfq_back_max;
2173 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002174 cfqd->cfq_slice[0] = cfq_slice_async;
2175 cfqd->cfq_slice[1] = cfq_slice_sync;
2176 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2177 cfqd->cfq_slice_idle = cfq_slice_idle;
2178 cfqd->cfq_max_depth = cfq_max_depth;
Jens Axboe3b181522005-06-27 10:56:24 +02002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181out_crqpool:
2182 kfree(cfqd->cfq_hash);
2183out_cfqhash:
2184 kfree(cfqd->crq_hash);
2185out_crqhash:
2186 kfree(cfqd);
2187 return -ENOMEM;
2188}
2189
2190static void cfq_slab_kill(void)
2191{
2192 if (crq_pool)
2193 kmem_cache_destroy(crq_pool);
2194 if (cfq_pool)
2195 kmem_cache_destroy(cfq_pool);
2196 if (cfq_ioc_pool)
2197 kmem_cache_destroy(cfq_ioc_pool);
2198}
2199
2200static int __init cfq_slab_setup(void)
2201{
2202 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2203 NULL, NULL);
2204 if (!crq_pool)
2205 goto fail;
2206
2207 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2208 NULL, NULL);
2209 if (!cfq_pool)
2210 goto fail;
2211
2212 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2213 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2214 if (!cfq_ioc_pool)
2215 goto fail;
2216
2217 return 0;
2218fail:
2219 cfq_slab_kill();
2220 return -ENOMEM;
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/*
2224 * sysfs parts below -->
2225 */
2226struct cfq_fs_entry {
2227 struct attribute attr;
2228 ssize_t (*show)(struct cfq_data *, char *);
2229 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2230};
2231
2232static ssize_t
2233cfq_var_show(unsigned int var, char *page)
2234{
2235 return sprintf(page, "%d\n", var);
2236}
2237
2238static ssize_t
2239cfq_var_store(unsigned int *var, const char *page, size_t count)
2240{
2241 char *p = (char *) page;
2242
2243 *var = simple_strtoul(p, &p, 10);
2244 return count;
2245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2248static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2249{ \
2250 unsigned int __data = __VAR; \
2251 if (__CONV) \
2252 __data = jiffies_to_msecs(__data); \
2253 return cfq_var_show(__data, (page)); \
2254}
2255SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2256SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002257SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2258SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2260SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002261SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2262SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2263SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2264SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2265SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266#undef SHOW_FUNCTION
2267
2268#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2269static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2270{ \
2271 unsigned int __data; \
2272 int ret = cfq_var_store(&__data, (page), count); \
2273 if (__data < (MIN)) \
2274 __data = (MIN); \
2275 else if (__data > (MAX)) \
2276 __data = (MAX); \
2277 if (__CONV) \
2278 *(__PTR) = msecs_to_jiffies(__data); \
2279 else \
2280 *(__PTR) = __data; \
2281 return ret; \
2282}
2283STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2284STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002285STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2286STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2288STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002289STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2290STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2291STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2292STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2293STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294#undef STORE_FUNCTION
2295
2296static struct cfq_fs_entry cfq_quantum_entry = {
2297 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2298 .show = cfq_quantum_show,
2299 .store = cfq_quantum_store,
2300};
2301static struct cfq_fs_entry cfq_queued_entry = {
2302 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2303 .show = cfq_queued_show,
2304 .store = cfq_queued_store,
2305};
Jens Axboe22e2c502005-06-27 10:55:12 +02002306static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002308 .show = cfq_fifo_expire_sync_show,
2309 .store = cfq_fifo_expire_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310};
Jens Axboe22e2c502005-06-27 10:55:12 +02002311static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002313 .show = cfq_fifo_expire_async_show,
2314 .store = cfq_fifo_expire_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315};
2316static struct cfq_fs_entry cfq_back_max_entry = {
2317 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2318 .show = cfq_back_max_show,
2319 .store = cfq_back_max_store,
2320};
2321static struct cfq_fs_entry cfq_back_penalty_entry = {
2322 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2323 .show = cfq_back_penalty_show,
2324 .store = cfq_back_penalty_store,
2325};
Jens Axboe22e2c502005-06-27 10:55:12 +02002326static struct cfq_fs_entry cfq_slice_sync_entry = {
2327 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2328 .show = cfq_slice_sync_show,
2329 .store = cfq_slice_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330};
Jens Axboe22e2c502005-06-27 10:55:12 +02002331static struct cfq_fs_entry cfq_slice_async_entry = {
2332 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2333 .show = cfq_slice_async_show,
2334 .store = cfq_slice_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
Jens Axboe22e2c502005-06-27 10:55:12 +02002336static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2337 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2338 .show = cfq_slice_async_rq_show,
2339 .store = cfq_slice_async_rq_store,
2340};
2341static struct cfq_fs_entry cfq_slice_idle_entry = {
2342 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2343 .show = cfq_slice_idle_show,
2344 .store = cfq_slice_idle_store,
2345};
2346static struct cfq_fs_entry cfq_max_depth_entry = {
2347 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2348 .show = cfq_max_depth_show,
2349 .store = cfq_max_depth_store,
2350};
Jens Axboe3b181522005-06-27 10:56:24 +02002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352static struct attribute *default_attrs[] = {
2353 &cfq_quantum_entry.attr,
2354 &cfq_queued_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002355 &cfq_fifo_expire_sync_entry.attr,
2356 &cfq_fifo_expire_async_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 &cfq_back_max_entry.attr,
2358 &cfq_back_penalty_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002359 &cfq_slice_sync_entry.attr,
2360 &cfq_slice_async_entry.attr,
2361 &cfq_slice_async_rq_entry.attr,
2362 &cfq_slice_idle_entry.attr,
2363 &cfq_max_depth_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 NULL,
2365};
2366
2367#define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2368
2369static ssize_t
2370cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2371{
2372 elevator_t *e = container_of(kobj, elevator_t, kobj);
2373 struct cfq_fs_entry *entry = to_cfq(attr);
2374
2375 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002376 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 return entry->show(e->elevator_data, page);
2379}
2380
2381static ssize_t
2382cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2383 const char *page, size_t length)
2384{
2385 elevator_t *e = container_of(kobj, elevator_t, kobj);
2386 struct cfq_fs_entry *entry = to_cfq(attr);
2387
2388 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002389 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 return entry->store(e->elevator_data, page, length);
2392}
2393
2394static struct sysfs_ops cfq_sysfs_ops = {
2395 .show = cfq_attr_show,
2396 .store = cfq_attr_store,
2397};
2398
2399static struct kobj_type cfq_ktype = {
2400 .sysfs_ops = &cfq_sysfs_ops,
2401 .default_attrs = default_attrs,
2402};
2403
2404static struct elevator_type iosched_cfq = {
2405 .ops = {
2406 .elevator_merge_fn = cfq_merge,
2407 .elevator_merged_fn = cfq_merged_request,
2408 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002409 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002411 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 .elevator_deactivate_req_fn = cfq_deactivate_request,
2413 .elevator_queue_empty_fn = cfq_queue_empty,
2414 .elevator_completed_req_fn = cfq_completed_request,
2415 .elevator_former_req_fn = cfq_former_request,
2416 .elevator_latter_req_fn = cfq_latter_request,
2417 .elevator_set_req_fn = cfq_set_request,
2418 .elevator_put_req_fn = cfq_put_request,
2419 .elevator_may_queue_fn = cfq_may_queue,
2420 .elevator_init_fn = cfq_init_queue,
2421 .elevator_exit_fn = cfq_exit_queue,
2422 },
2423 .elevator_ktype = &cfq_ktype,
2424 .elevator_name = "cfq",
2425 .elevator_owner = THIS_MODULE,
2426};
2427
2428static int __init cfq_init(void)
2429{
2430 int ret;
2431
Jens Axboe22e2c502005-06-27 10:55:12 +02002432 /*
2433 * could be 0 on HZ < 1000 setups
2434 */
2435 if (!cfq_slice_async)
2436 cfq_slice_async = 1;
2437 if (!cfq_slice_idle)
2438 cfq_slice_idle = 1;
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (cfq_slab_setup())
2441 return -ENOMEM;
2442
2443 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002444 if (ret)
2445 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return ret;
2448}
2449
2450static void __exit cfq_exit(void)
2451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 elv_unregister(&iosched_cfq);
Christoph Hellwig83521d32005-10-30 15:01:39 -08002453 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456module_init(cfq_init);
2457module_exit(cfq_exit);
2458
2459MODULE_AUTHOR("Jens Axboe");
2460MODULE_LICENSE("GPL");
2461MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");