blob: bb43a1677626b0ab98b2ff9ebabdb0a995a9fee0 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/config.h>
10#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hash.h>
14#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * tunables
19 */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_quantum = 4; /* max queue in one round of service */
21static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
22static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
23static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
24static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020027static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_slice_async_rq = 2;
Jens Axboe3b181522005-06-27 10:56:24 +020029static int cfq_slice_idle = HZ / 100;
Jens Axboe22e2c502005-06-27 10:55:12 +020030
31#define CFQ_IDLE_GRACE (HZ / 10)
32#define CFQ_SLICE_SCALE (5)
33
34#define CFQ_KEY_ASYNC (0)
Jens Axboe3b181522005-06-27 10:56:24 +020035#define CFQ_KEY_ANY (0xffff)
Jens Axboe22e2c502005-06-27 10:55:12 +020036
37/*
38 * disable queueing at the driver/hardware level
39 */
Arjan van de Ven64100092006-01-06 09:46:02 +010040static const int cfq_max_depth = 2;
Jens Axboe22e2c502005-06-27 10:55:12 +020041
Al Viroa6a07632006-03-18 13:26:44 -050042static DEFINE_RWLOCK(cfq_exit_lock);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * for the hash of cfqq inside the cfqd
46 */
47#define CFQ_QHASH_SHIFT 6
48#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
49#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
50
51/*
52 * for the hash of crq inside the cfqq
53 */
54#define CFQ_MHASH_SHIFT 6
55#define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
56#define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
57#define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
58#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
59#define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
60
61#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
Jens Axboe22e2c502005-06-27 10:55:12 +020062#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define RQ_DATA(rq) (rq)->elevator_private
65
66/*
67 * rb-tree defines
68 */
69#define RB_NONE (2)
70#define RB_EMPTY(node) ((node)->rb_node == NULL)
71#define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
72#define RB_CLEAR(node) do { \
73 (node)->rb_parent = NULL; \
74 RB_CLEAR_COLOR((node)); \
75 (node)->rb_right = NULL; \
76 (node)->rb_left = NULL; \
77} while (0)
78#define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
80#define rq_rb_key(rq) (rq)->sector
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static kmem_cache_t *crq_pool;
83static kmem_cache_t *cfq_pool;
84static kmem_cache_t *cfq_ioc_pool;
85
Al Viro334e94d2006-03-18 15:05:53 -050086static atomic_t ioc_count = ATOMIC_INIT(0);
87static struct completion *ioc_gone;
88
Jens Axboe22e2c502005-06-27 10:55:12 +020089#define CFQ_PRIO_LISTS IOPRIO_BE_NR
90#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
91#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
92#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
93
Jens Axboe3b181522005-06-27 10:56:24 +020094#define ASYNC (0)
95#define SYNC (1)
96
97#define cfq_cfqq_dispatched(cfqq) \
98 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
99
100#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
101
102#define cfq_cfqq_sync(cfqq) \
103 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +0200104
105/*
106 * Per block device queue structure
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +0200109 request_queue_t *queue;
110
111 /*
112 * rr list of queues with requests and the count of them
113 */
114 struct list_head rr_list[CFQ_PRIO_LISTS];
115 struct list_head busy_rr;
116 struct list_head cur_rr;
117 struct list_head idle_rr;
118 unsigned int busy_queues;
119
120 /*
121 * non-ordered list of empty cfqq's
122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct list_head empty_list;
124
Jens Axboe22e2c502005-06-27 10:55:12 +0200125 /*
126 * cfqq lookup hash
127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jens Axboe22e2c502005-06-27 10:55:12 +0200130 /*
131 * global crq hash for all queues
132 */
133 struct hlist_head *crq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 unsigned int max_queued;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 mempool_t *crq_pool;
138
Jens Axboe22e2c502005-06-27 10:55:12 +0200139 int rq_in_driver;
140
141 /*
142 * schedule slice state info
143 */
144 /*
145 * idle window management
146 */
147 struct timer_list idle_slice_timer;
148 struct work_struct unplug_work;
149
150 struct cfq_queue *active_queue;
151 struct cfq_io_context *active_cic;
152 int cur_prio, cur_end_prio;
153 unsigned int dispatch_slice;
154
155 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200158 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jens Axboe22e2c502005-06-27 10:55:12 +0200160 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
163 * tunables, see top of file
164 */
165 unsigned int cfq_quantum;
166 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200167 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int cfq_back_penalty;
169 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200170 unsigned int cfq_slice[2];
171 unsigned int cfq_slice_async_rq;
172 unsigned int cfq_slice_idle;
173 unsigned int cfq_max_depth;
Al Virod9ff4182006-03-18 13:51:22 -0500174
175 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Jens Axboe22e2c502005-06-27 10:55:12 +0200178/*
179 * Per process-grouping structure
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181struct cfq_queue {
182 /* reference count */
183 atomic_t ref;
184 /* parent cfq_data */
185 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200186 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct hlist_node cfq_hash;
188 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200189 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* on either rr or empty list of cfqd */
191 struct list_head cfq_list;
192 /* sorted list of pending requests */
193 struct rb_root sort_list;
194 /* if fifo isn't expired, next request to serve */
195 struct cfq_rq *next_crq;
196 /* requests queued in sort_list */
197 int queued[2];
198 /* currently allocated requests */
199 int allocated[2];
200 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200201 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jens Axboe22e2c502005-06-27 10:55:12 +0200203 unsigned long slice_start;
204 unsigned long slice_end;
205 unsigned long slice_left;
206 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jens Axboe3b181522005-06-27 10:56:24 +0200208 /* number of requests that are on the dispatch list */
209 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200210
211 /* io prio of this group */
212 unsigned short ioprio, org_ioprio;
213 unsigned short ioprio_class, org_ioprio_class;
214
Jens Axboe3b181522005-06-27 10:56:24 +0200215 /* various state flags, see below */
216 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219struct cfq_rq {
220 struct rb_node rb_node;
221 sector_t rb_key;
222 struct request *request;
223 struct hlist_node hash;
224
225 struct cfq_queue *cfq_queue;
226 struct cfq_io_context *io_context;
227
Jens Axboe3b181522005-06-27 10:56:24 +0200228 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Jens Axboe3b181522005-06-27 10:56:24 +0200231enum cfqq_state_flags {
232 CFQ_CFQQ_FLAG_on_rr = 0,
233 CFQ_CFQQ_FLAG_wait_request,
234 CFQ_CFQQ_FLAG_must_alloc,
235 CFQ_CFQQ_FLAG_must_alloc_slice,
236 CFQ_CFQQ_FLAG_must_dispatch,
237 CFQ_CFQQ_FLAG_fifo_expire,
238 CFQ_CFQQ_FLAG_idle_window,
239 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200240};
241
242#define CFQ_CFQQ_FNS(name) \
243static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
244{ \
245 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
246} \
247static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
248{ \
249 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
250} \
251static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
252{ \
253 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
254}
255
256CFQ_CFQQ_FNS(on_rr);
257CFQ_CFQQ_FNS(wait_request);
258CFQ_CFQQ_FNS(must_alloc);
259CFQ_CFQQ_FNS(must_alloc_slice);
260CFQ_CFQQ_FNS(must_dispatch);
261CFQ_CFQQ_FNS(fifo_expire);
262CFQ_CFQQ_FNS(idle_window);
263CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200264#undef CFQ_CFQQ_FNS
265
266enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200267 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200268};
269
270#define CFQ_CRQ_FNS(name) \
271static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
272{ \
273 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
274} \
275static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
276{ \
277 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
278} \
279static inline int cfq_crq_##name(const struct cfq_rq *crq) \
280{ \
281 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
282}
283
Jens Axboe3b181522005-06-27 10:56:24 +0200284CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200285#undef CFQ_CRQ_FNS
286
287static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200288static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Al Viro6f325a12006-03-18 14:58:37 -0500289static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jens Axboe22e2c502005-06-27 10:55:12 +0200291#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/*
294 * lots of deadline iosched dupes, can be abstracted later...
295 */
296static inline void cfq_del_crq_hash(struct cfq_rq *crq)
297{
298 hlist_del_init(&crq->hash);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
302{
303 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
306}
307
308static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
309{
310 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
311 struct hlist_node *entry, *next;
312
313 hlist_for_each_safe(entry, next, hash_list) {
314 struct cfq_rq *crq = list_entry_hash(entry);
315 struct request *__rq = crq->request;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!rq_mergeable(__rq)) {
318 cfq_del_crq_hash(crq);
319 continue;
320 }
321
322 if (rq_hash_key(__rq) == offset)
323 return __rq;
324 }
325
326 return NULL;
327}
328
Andrew Morton99f95e52005-06-27 20:14:05 -0700329/*
330 * scheduler run of queue, if there are requests pending and no one in the
331 * driver that will restart queueing
332 */
333static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
334{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100335 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700336 kblockd_schedule_work(&cfqd->unplug_work);
337}
338
339static int cfq_queue_empty(request_queue_t *q)
340{
341 struct cfq_data *cfqd = q->elevator->elevator_data;
342
Jens Axboeb4878f22005-10-20 16:42:29 +0200343 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
348 * We choose the request that is closest to the head right now. Distance
349 * behind the head are penalized and only allowed to a certain extent.
350 */
351static struct cfq_rq *
352cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
353{
354 sector_t last, s1, s2, d1 = 0, d2 = 0;
355 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
356 unsigned long back_max;
357
358 if (crq1 == NULL || crq1 == crq2)
359 return crq2;
360 if (crq2 == NULL)
361 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200362
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200363 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
364 return crq1;
365 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200366 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 s1 = crq1->request->sector;
369 s2 = crq2->request->sector;
370
371 last = cfqd->last_sector;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * by definition, 1KiB is 2 sectors
375 */
376 back_max = cfqd->cfq_back_max * 2;
377
378 /*
379 * Strict one way elevator _except_ in the case where we allow
380 * short backward seeks which are biased as twice the cost of a
381 * similar forward seek.
382 */
383 if (s1 >= last)
384 d1 = s1 - last;
385 else if (s1 + back_max >= last)
386 d1 = (last - s1) * cfqd->cfq_back_penalty;
387 else
388 r1_wrap = 1;
389
390 if (s2 >= last)
391 d2 = s2 - last;
392 else if (s2 + back_max >= last)
393 d2 = (last - s2) * cfqd->cfq_back_penalty;
394 else
395 r2_wrap = 1;
396
397 /* Found required data */
398 if (!r1_wrap && r2_wrap)
399 return crq1;
400 else if (!r2_wrap && r1_wrap)
401 return crq2;
402 else if (r1_wrap && r2_wrap) {
403 /* both behind the head */
404 if (s1 <= s2)
405 return crq1;
406 else
407 return crq2;
408 }
409
410 /* Both requests in front of the head */
411 if (d1 < d2)
412 return crq1;
413 else if (d2 < d1)
414 return crq2;
415 else {
416 if (s1 >= s2)
417 return crq1;
418 else
419 return crq2;
420 }
421}
422
423/*
424 * would be nice to take fifo expire time into account as well
425 */
426static struct cfq_rq *
427cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
428 struct cfq_rq *last)
429{
430 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
431 struct rb_node *rbnext, *rbprev;
432
Jens Axboeb4878f22005-10-20 16:42:29 +0200433 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200435 if (rbnext == &last->rb_node)
436 rbnext = NULL;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 rbprev = rb_prev(&last->rb_node);
440
441 if (rbprev)
442 crq_prev = rb_entry_crq(rbprev);
443 if (rbnext)
444 crq_next = rb_entry_crq(rbnext);
445
446 return cfq_choose_req(cfqd, crq_next, crq_prev);
447}
448
449static void cfq_update_next_crq(struct cfq_rq *crq)
450{
451 struct cfq_queue *cfqq = crq->cfq_queue;
452
453 if (cfqq->next_crq == crq)
454 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
455}
456
Jens Axboe22e2c502005-06-27 10:55:12 +0200457static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jens Axboe22e2c502005-06-27 10:55:12 +0200459 struct cfq_data *cfqd = cfqq->cfqd;
460 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jens Axboe3b181522005-06-27 10:56:24 +0200462 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 list_del(&cfqq->cfq_list);
465
Jens Axboe22e2c502005-06-27 10:55:12 +0200466 if (cfq_class_rt(cfqq))
467 list = &cfqd->cur_rr;
468 else if (cfq_class_idle(cfqq))
469 list = &cfqd->idle_rr;
470 else {
471 /*
472 * if cfqq has requests in flight, don't allow it to be
473 * found in cfq_set_active_queue before it has finished them.
474 * this is done to increase fairness between a process that
475 * has lots of io pending vs one that only generates one
476 * sporadically or synchronously
477 */
Jens Axboe3b181522005-06-27 10:56:24 +0200478 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200479 list = &cfqd->busy_rr;
480 else
481 list = &cfqd->rr_list[cfqq->ioprio];
482 }
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200485 * if queue was preempted, just add to front to be fair. busy_rr
486 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200488 if (preempted || list == &cfqd->busy_rr) {
489 list_add(&cfqq->cfq_list, list);
490 return;
491 }
492
493 /*
494 * sort by when queue was last serviced
495 */
496 entry = list;
497 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
499
Jens Axboe22e2c502005-06-27 10:55:12 +0200500 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200502 if (time_before(__cfqq->service_last, cfqq->service_last))
503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 list_add(&cfqq->cfq_list, entry);
507}
508
509/*
510 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200511 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200514cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jens Axboe3b181522005-06-27 10:56:24 +0200516 BUG_ON(cfq_cfqq_on_rr(cfqq));
517 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 cfqd->busy_queues++;
519
Jens Axboeb4878f22005-10-20 16:42:29 +0200520 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523static inline void
524cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
525{
Jens Axboe3b181522005-06-27 10:56:24 +0200526 BUG_ON(!cfq_cfqq_on_rr(cfqq));
527 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200528 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 BUG_ON(!cfqd->busy_queues);
531 cfqd->busy_queues--;
532}
533
534/*
535 * rb tree support functions
536 */
537static inline void cfq_del_crq_rb(struct cfq_rq *crq)
538{
539 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200540 struct cfq_data *cfqd = cfqq->cfqd;
541 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jens Axboeb4878f22005-10-20 16:42:29 +0200543 BUG_ON(!cfqq->queued[sync]);
544 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Jens Axboeb4878f22005-10-20 16:42:29 +0200546 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jens Axboeb4878f22005-10-20 16:42:29 +0200548 rb_erase(&crq->rb_node, &cfqq->sort_list);
549 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jens Axboeb4878f22005-10-20 16:42:29 +0200551 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
552 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static struct cfq_rq *
556__cfq_add_crq_rb(struct cfq_rq *crq)
557{
558 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
559 struct rb_node *parent = NULL;
560 struct cfq_rq *__crq;
561
562 while (*p) {
563 parent = *p;
564 __crq = rb_entry_crq(parent);
565
566 if (crq->rb_key < __crq->rb_key)
567 p = &(*p)->rb_left;
568 else if (crq->rb_key > __crq->rb_key)
569 p = &(*p)->rb_right;
570 else
571 return __crq;
572 }
573
574 rb_link_node(&crq->rb_node, parent, p);
575 return NULL;
576}
577
578static void cfq_add_crq_rb(struct cfq_rq *crq)
579{
580 struct cfq_queue *cfqq = crq->cfq_queue;
581 struct cfq_data *cfqd = cfqq->cfqd;
582 struct request *rq = crq->request;
583 struct cfq_rq *__alias;
584
585 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200586 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
589 * looks a little odd, but the first insert might return an alias.
590 * if that happens, put the alias on the dispatch list
591 */
592 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200593 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
596
Jens Axboe3b181522005-06-27 10:56:24 +0200597 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200598 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /*
601 * check if this request is a better next-serve candidate
602 */
603 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
604}
605
606static inline void
607cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
608{
Jens Axboeb4878f22005-10-20 16:42:29 +0200609 rb_erase(&crq->rb_node, &cfqq->sort_list);
610 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 cfq_add_crq_rb(crq);
613}
614
Jens Axboe22e2c502005-06-27 10:55:12 +0200615static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jens Axboe3b181522005-06-27 10:56:24 +0200618 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct rb_node *n;
620
621 if (!cfqq)
622 goto out;
623
624 n = cfqq->sort_list.rb_node;
625 while (n) {
626 struct cfq_rq *crq = rb_entry_crq(n);
627
628 if (sector < crq->rb_key)
629 n = n->rb_left;
630 else if (sector > crq->rb_key)
631 n = n->rb_right;
632 else
633 return crq->request;
634 }
635
636out:
637 return NULL;
638}
639
Jens Axboeb4878f22005-10-20 16:42:29 +0200640static void cfq_activate_request(request_queue_t *q, struct request *rq)
641{
642 struct cfq_data *cfqd = q->elevator->elevator_data;
643
644 cfqd->rq_in_driver++;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
648{
Jens Axboe22e2c502005-06-27 10:55:12 +0200649 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jens Axboeb4878f22005-10-20 16:42:29 +0200651 WARN_ON(!cfqd->rq_in_driver);
652 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Jens Axboeb4878f22005-10-20 16:42:29 +0200655static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct cfq_rq *crq = RQ_DATA(rq);
658
Jens Axboeb4878f22005-10-20 16:42:29 +0200659 list_del_init(&rq->queuelist);
660 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200661 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static int
665cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
666{
667 struct cfq_data *cfqd = q->elevator->elevator_data;
668 struct request *__rq;
669 int ret;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200672 if (__rq && elv_rq_merge_ok(__rq, bio)) {
673 ret = ELEVATOR_BACK_MERGE;
674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
Jens Axboe22e2c502005-06-27 10:55:12 +0200678 if (__rq && elv_rq_merge_ok(__rq, bio)) {
679 ret = ELEVATOR_FRONT_MERGE;
680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 return ELEVATOR_NO_MERGE;
684out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *req = __rq;
686 return ret;
687}
688
689static void cfq_merged_request(request_queue_t *q, struct request *req)
690{
691 struct cfq_data *cfqd = q->elevator->elevator_data;
692 struct cfq_rq *crq = RQ_DATA(req);
693
694 cfq_del_crq_hash(crq);
695 cfq_add_crq_hash(cfqd, crq);
696
Jens Axboeb4878f22005-10-20 16:42:29 +0200697 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct cfq_queue *cfqq = crq->cfq_queue;
699
700 cfq_update_next_crq(crq);
701 cfq_reposition_crq_rb(cfqq, crq);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705static void
706cfq_merged_requests(request_queue_t *q, struct request *rq,
707 struct request *next)
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 cfq_merged_request(q, rq);
710
Jens Axboe22e2c502005-06-27 10:55:12 +0200711 /*
712 * reposition in fifo if next is older than rq
713 */
714 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
715 time_before(next->start_time, rq->start_time))
716 list_move(&rq->queuelist, &next->queuelist);
717
Jens Axboeb4878f22005-10-20 16:42:29 +0200718 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200719}
720
721static inline void
722__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
723{
724 if (cfqq) {
725 /*
726 * stop potential idle class queues waiting service
727 */
728 del_timer(&cfqd->idle_class_timer);
729
730 cfqq->slice_start = jiffies;
731 cfqq->slice_end = 0;
732 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200733 cfq_clear_cfqq_must_alloc_slice(cfqq);
734 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200735 }
736
737 cfqd->active_queue = cfqq;
738}
739
740/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100741 * current cfqq expired its slice (or was too idle), select new one
742 */
743static void
744__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
745 int preempted)
746{
747 unsigned long now = jiffies;
748
749 if (cfq_cfqq_wait_request(cfqq))
750 del_timer(&cfqd->idle_slice_timer);
751
752 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
753 cfqq->service_last = now;
754 cfq_schedule_dispatch(cfqd);
755 }
756
757 cfq_clear_cfqq_must_dispatch(cfqq);
758 cfq_clear_cfqq_wait_request(cfqq);
759
760 /*
761 * store what was left of this slice, if the queue idled out
762 * or was preempted
763 */
764 if (time_after(cfqq->slice_end, now))
765 cfqq->slice_left = cfqq->slice_end - now;
766 else
767 cfqq->slice_left = 0;
768
769 if (cfq_cfqq_on_rr(cfqq))
770 cfq_resort_rr_list(cfqq, preempted);
771
772 if (cfqq == cfqd->active_queue)
773 cfqd->active_queue = NULL;
774
775 if (cfqd->active_cic) {
776 put_io_context(cfqd->active_cic->ioc);
777 cfqd->active_cic = NULL;
778 }
779
780 cfqd->dispatch_slice = 0;
781}
782
783static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
784{
785 struct cfq_queue *cfqq = cfqd->active_queue;
786
787 if (cfqq)
788 __cfq_slice_expired(cfqd, cfqq, preempted);
789}
790
791/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200792 * 0
793 * 0,1
794 * 0,1,2
795 * 0,1,2,3
796 * 0,1,2,3,4
797 * 0,1,2,3,4,5
798 * 0,1,2,3,4,5,6
799 * 0,1,2,3,4,5,6,7
800 */
801static int cfq_get_next_prio_level(struct cfq_data *cfqd)
802{
803 int prio, wrap;
804
805 prio = -1;
806 wrap = 0;
807 do {
808 int p;
809
810 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
811 if (!list_empty(&cfqd->rr_list[p])) {
812 prio = p;
813 break;
814 }
815 }
816
817 if (prio != -1)
818 break;
819 cfqd->cur_prio = 0;
820 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
821 cfqd->cur_end_prio = 0;
822 if (wrap)
823 break;
824 wrap = 1;
825 }
826 } while (1);
827
828 if (unlikely(prio == -1))
829 return -1;
830
831 BUG_ON(prio >= CFQ_PRIO_LISTS);
832
833 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
834
835 cfqd->cur_prio = prio + 1;
836 if (cfqd->cur_prio > cfqd->cur_end_prio) {
837 cfqd->cur_end_prio = cfqd->cur_prio;
838 cfqd->cur_prio = 0;
839 }
840 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
841 cfqd->cur_prio = 0;
842 cfqd->cur_end_prio = 0;
843 }
844
845 return prio;
846}
847
Jens Axboe3b181522005-06-27 10:56:24 +0200848static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200849{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100850 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200851
852 /*
853 * if current list is non-empty, grab first entry. if it is empty,
854 * get next prio level and grab first entry then if any are spliced
855 */
856 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
857 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
858
859 /*
860 * if we have idle queues and no rt or be queues had pending
861 * requests, either allow immediate service if the grace period
862 * has passed or arm the idle grace timer
863 */
864 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
865 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
866
867 if (time_after_eq(jiffies, end))
868 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
869 else
870 mod_timer(&cfqd->idle_class_timer, end);
871 }
872
873 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200874 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200875}
876
Jens Axboe22e2c502005-06-27 10:55:12 +0200877static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
878
879{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100880 unsigned long sl;
881
Jens Axboe22e2c502005-06-27 10:55:12 +0200882 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
883 WARN_ON(cfqq != cfqd->active_queue);
884
885 /*
886 * idle is disabled, either manually or by past process history
887 */
888 if (!cfqd->cfq_slice_idle)
889 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200890 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200891 return 0;
892 /*
893 * task has exited, don't wait
894 */
895 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
896 return 0;
897
Jens Axboe3b181522005-06-27 10:56:24 +0200898 cfq_mark_cfqq_must_dispatch(cfqq);
899 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200900
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100901 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
902 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200903 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Jens Axboeb4878f22005-10-20 16:42:29 +0200906static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct cfq_data *cfqd = q->elevator->elevator_data;
909 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200910
911 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200912 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200913 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200914 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/*
918 * return expired entry, or NULL to just start from scratch in rbtree
919 */
920static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
921{
922 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200923 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct cfq_rq *crq;
925
Jens Axboe3b181522005-06-27 10:56:24 +0200926 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return NULL;
928
Jens Axboe22e2c502005-06-27 10:55:12 +0200929 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200930 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Jens Axboe22e2c502005-06-27 10:55:12 +0200932 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
933 rq = crq->request;
934 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200935 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200936 return crq;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 return NULL;
941}
942
943/*
Jens Axboe3b181522005-06-27 10:56:24 +0200944 * Scale schedule slice based on io priority. Use the sync time slice only
945 * if a queue is marked sync and has sync io queued. A sync queue with async
946 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200948static inline int
949cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Jens Axboe22e2c502005-06-27 10:55:12 +0200951 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Jens Axboe22e2c502005-06-27 10:55:12 +0200953 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Jens Axboe22e2c502005-06-27 10:55:12 +0200955 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Jens Axboe22e2c502005-06-27 10:55:12 +0200958static inline void
959cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
960{
961 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
962}
963
964static inline int
965cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
966{
967 const int base_rq = cfqd->cfq_slice_async_rq;
968
969 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
970
971 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
972}
973
974/*
975 * get next queue for service
976 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100977static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200978{
979 unsigned long now = jiffies;
980 struct cfq_queue *cfqq;
981
982 cfqq = cfqd->active_queue;
983 if (!cfqq)
984 goto new_queue;
985
986 /*
987 * slice has expired
988 */
Jens Axboe3b181522005-06-27 10:56:24 +0200989 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
990 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200991
992 /*
993 * if queue has requests, dispatch one. if not, check if
994 * enough slice is left to wait for one
995 */
996 if (!RB_EMPTY(&cfqq->sort_list))
997 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100998 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +0200999 time_before(now, cfqq->slice_end)) {
1000 if (cfq_arm_slice_timer(cfqd, cfqq))
1001 return NULL;
1002 }
1003
Jens Axboe3b181522005-06-27 10:56:24 +02001004expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001005 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001006new_queue:
1007 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001008keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001009 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001010}
1011
1012static int
1013__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1014 int max_dispatch)
1015{
1016 int dispatched = 0;
1017
1018 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1019
1020 do {
1021 struct cfq_rq *crq;
1022
1023 /*
1024 * follow expired path, else get first next available
1025 */
1026 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1027 crq = cfqq->next_crq;
1028
1029 /*
1030 * finally, insert request into driver dispatch list
1031 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001032 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001033
1034 cfqd->dispatch_slice++;
1035 dispatched++;
1036
1037 if (!cfqd->active_cic) {
1038 atomic_inc(&crq->io_context->ioc->refcount);
1039 cfqd->active_cic = crq->io_context;
1040 }
1041
1042 if (RB_EMPTY(&cfqq->sort_list))
1043 break;
1044
1045 } while (dispatched < max_dispatch);
1046
1047 /*
1048 * if slice end isn't set yet, set it. if at least one request was
1049 * sync, use the sync time slice value
1050 */
1051 if (!cfqq->slice_end)
1052 cfq_set_prio_slice(cfqd, cfqq);
1053
1054 /*
1055 * expire an async queue immediately if it has used up its slice. idle
1056 * queue always expire after 1 dispatch round.
1057 */
1058 if ((!cfq_cfqq_sync(cfqq) &&
1059 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1060 cfq_class_idle(cfqq))
1061 cfq_slice_expired(cfqd, 0);
1062
1063 return dispatched;
1064}
1065
1066static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001067cfq_forced_dispatch_cfqqs(struct list_head *list)
1068{
1069 int dispatched = 0;
1070 struct cfq_queue *cfqq, *next;
1071 struct cfq_rq *crq;
1072
1073 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1074 while ((crq = cfqq->next_crq)) {
1075 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1076 dispatched++;
1077 }
1078 BUG_ON(!list_empty(&cfqq->fifo));
1079 }
1080 return dispatched;
1081}
1082
1083static int
1084cfq_forced_dispatch(struct cfq_data *cfqd)
1085{
1086 int i, dispatched = 0;
1087
1088 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1089 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1090
1091 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1092 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1093 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1094
1095 cfq_slice_expired(cfqd, 0);
1096
1097 BUG_ON(cfqd->busy_queues);
1098
1099 return dispatched;
1100}
1101
1102static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001103cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct cfq_data *cfqd = q->elevator->elevator_data;
1106 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jens Axboe22e2c502005-06-27 10:55:12 +02001108 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001111 if (unlikely(force))
1112 return cfq_forced_dispatch(cfqd);
1113
1114 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001115 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001116 int max_dispatch;
1117
1118 /*
1119 * if idle window is disabled, allow queue buildup
1120 */
1121 if (!cfq_cfqq_idle_window(cfqq) &&
1122 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1123 return 0;
1124
Jens Axboe3b181522005-06-27 10:56:24 +02001125 cfq_clear_cfqq_must_dispatch(cfqq);
1126 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001127 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001129 max_dispatch = cfqd->cfq_quantum;
1130 if (cfq_class_idle(cfqq))
1131 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jens Axboe22e2c502005-06-27 10:55:12 +02001133 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Jens Axboe22e2c502005-06-27 10:55:12 +02001136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * task holds one reference to the queue, dropped when task exits. each crq
1141 * in-flight on this queue also holds a reference, dropped when crq is freed.
1142 *
1143 * queue lock must be held here.
1144 */
1145static void cfq_put_queue(struct cfq_queue *cfqq)
1146{
Jens Axboe22e2c502005-06-27 10:55:12 +02001147 struct cfq_data *cfqd = cfqq->cfqd;
1148
1149 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 if (!atomic_dec_and_test(&cfqq->ref))
1152 return;
1153
1154 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001155 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001156 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001158 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001159 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * it's on the empty list and still hashed
1163 */
1164 list_del(&cfqq->cfq_list);
1165 hlist_del(&cfqq->cfq_hash);
1166 kmem_cache_free(cfq_pool, cfqq);
1167}
1168
1169static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001170__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1171 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1174 struct hlist_node *entry, *next;
1175
1176 hlist_for_each_safe(entry, next, hash_list) {
1177 struct cfq_queue *__cfqq = list_entry_qhash(entry);
Al Virob0a69162006-03-14 15:32:50 -05001178 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Jens Axboe3b181522005-06-27 10:56:24 +02001180 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return __cfqq;
1182 }
1183
1184 return NULL;
1185}
1186
1187static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001188cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Jens Axboe3b181522005-06-27 10:56:24 +02001190 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Jens Axboee2d74ac2006-03-28 08:59:01 +02001193static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Jens Axboe22e2c502005-06-27 10:55:12 +02001195 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001196 struct rb_node *n;
1197 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001198
Jens Axboee2d74ac2006-03-28 08:59:01 +02001199 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1200 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1201 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001202 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001203 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001204 }
1205
Al Viro334e94d2006-03-18 15:05:53 -05001206 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1207 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Al Viroe17a9482006-03-18 13:21:20 -05001210static void cfq_trim(struct io_context *ioc)
1211{
1212 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001213 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001217 * Called with interrupts disabled
1218 */
1219static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1220{
Al Viro478a82b2006-03-18 13:25:24 -05001221 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001222 request_queue_t *q;
1223
1224 if (!cfqd)
1225 return;
1226
1227 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001228
1229 WARN_ON(!irqs_disabled());
1230
1231 spin_lock(q->queue_lock);
1232
Al Viro12a05732006-03-18 13:38:01 -05001233 if (cic->cfqq[ASYNC]) {
1234 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1235 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1236 cfq_put_queue(cic->cfqq[ASYNC]);
1237 cic->cfqq[ASYNC] = NULL;
1238 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001239
Al Viro12a05732006-03-18 13:38:01 -05001240 if (cic->cfqq[SYNC]) {
1241 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1242 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1243 cfq_put_queue(cic->cfqq[SYNC]);
1244 cic->cfqq[SYNC] = NULL;
1245 }
1246
Al Viro478a82b2006-03-18 13:25:24 -05001247 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001248 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001249 spin_unlock(q->queue_lock);
1250}
1251
Jens Axboee2d74ac2006-03-28 08:59:01 +02001252static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Jens Axboe22e2c502005-06-27 10:55:12 +02001254 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001256 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * put the reference this task is holding to the various queues
1260 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001261 read_lock_irqsave(&cfq_exit_lock, flags);
1262
1263 n = rb_first(&ioc->cic_root);
1264 while (n != NULL) {
1265 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1266
Jens Axboe22e2c502005-06-27 10:55:12 +02001267 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001268 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
Jens Axboee2d74ac2006-03-28 08:59:01 +02001271 read_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Jens Axboe22e2c502005-06-27 10:55:12 +02001274static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001275cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Jens Axboe22e2c502005-06-27 10:55:12 +02001277 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (cic) {
Jens Axboee2d74ac2006-03-28 08:59:01 +02001280 RB_CLEAR(&cic->rb_node);
1281 cic->key = NULL;
Al Viro12a05732006-03-18 13:38:01 -05001282 cic->cfqq[ASYNC] = NULL;
1283 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001284 cic->last_end_request = jiffies;
1285 cic->ttime_total = 0;
1286 cic->ttime_samples = 0;
1287 cic->ttime_mean = 0;
1288 cic->dtor = cfq_free_io_context;
1289 cic->exit = cfq_exit_io_context;
Al Virod9ff4182006-03-18 13:51:22 -05001290 INIT_LIST_HEAD(&cic->queue_list);
Al Viro334e94d2006-03-18 15:05:53 -05001291 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 return cic;
1295}
1296
Jens Axboe22e2c502005-06-27 10:55:12 +02001297static void cfq_init_prio_data(struct cfq_queue *cfqq)
1298{
1299 struct task_struct *tsk = current;
1300 int ioprio_class;
1301
Jens Axboe3b181522005-06-27 10:56:24 +02001302 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001303 return;
1304
1305 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1306 switch (ioprio_class) {
1307 default:
1308 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1309 case IOPRIO_CLASS_NONE:
1310 /*
1311 * no prio set, place us in the middle of the BE classes
1312 */
1313 cfqq->ioprio = task_nice_ioprio(tsk);
1314 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1315 break;
1316 case IOPRIO_CLASS_RT:
1317 cfqq->ioprio = task_ioprio(tsk);
1318 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1319 break;
1320 case IOPRIO_CLASS_BE:
1321 cfqq->ioprio = task_ioprio(tsk);
1322 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1323 break;
1324 case IOPRIO_CLASS_IDLE:
1325 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1326 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001327 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001328 break;
1329 }
1330
1331 /*
1332 * keep track of original prio settings in case we have to temporarily
1333 * elevate the priority of this queue
1334 */
1335 cfqq->org_ioprio = cfqq->ioprio;
1336 cfqq->org_ioprio_class = cfqq->ioprio_class;
1337
Jens Axboe3b181522005-06-27 10:56:24 +02001338 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001339 cfq_resort_rr_list(cfqq, 0);
1340
Jens Axboe3b181522005-06-27 10:56:24 +02001341 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001342}
1343
Al Viro478a82b2006-03-18 13:25:24 -05001344static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001345{
Al Viro478a82b2006-03-18 13:25:24 -05001346 struct cfq_data *cfqd = cic->key;
1347 struct cfq_queue *cfqq;
1348 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001349 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001350 cfqq = cic->cfqq[ASYNC];
1351 if (cfqq) {
Al Viro6f325a12006-03-18 14:58:37 -05001352 struct cfq_queue *new_cfqq;
1353 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
1354 cic->ioc->task, GFP_ATOMIC);
1355 if (new_cfqq) {
1356 cic->cfqq[ASYNC] = new_cfqq;
1357 cfq_put_queue(cfqq);
1358 }
Al Viro12a05732006-03-18 13:38:01 -05001359 }
1360 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001361 if (cfqq) {
1362 cfq_mark_cfqq_prio_changed(cfqq);
1363 cfq_init_prio_data(cfqq);
1364 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001365 spin_unlock(cfqd->queue->queue_lock);
1366 }
1367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001370 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001372static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Al Viroa6a07632006-03-18 13:26:44 -05001374 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001375 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001376
1377 write_lock(&cfq_exit_lock);
1378
Jens Axboee2d74ac2006-03-28 08:59:01 +02001379 n = rb_first(&ioc->cic_root);
1380 while (n != NULL) {
1381 cic = rb_entry(n, struct cfq_io_context, rb_node);
1382
Al Viro478a82b2006-03-18 13:25:24 -05001383 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001384 n = rb_next(n);
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Al Viroa6a07632006-03-18 13:26:44 -05001387 write_unlock(&cfq_exit_lock);
1388
Jens Axboe22e2c502005-06-27 10:55:12 +02001389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001393cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001394 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1397 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001398 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400retry:
Al Viro6f325a12006-03-18 14:58:37 -05001401 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001402 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (!cfqq) {
1405 if (new_cfqq) {
1406 cfqq = new_cfqq;
1407 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001408 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 spin_unlock_irq(cfqd->queue->queue_lock);
1410 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1411 spin_lock_irq(cfqd->queue->queue_lock);
1412 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001413 } else {
1414 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1415 if (!cfqq)
1416 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 memset(cfqq, 0, sizeof(*cfqq));
1420
1421 INIT_HLIST_NODE(&cfqq->cfq_hash);
1422 INIT_LIST_HEAD(&cfqq->cfq_list);
1423 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001424 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 cfqq->key = key;
1427 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1428 atomic_set(&cfqq->ref, 0);
1429 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001430 cfqq->service_last = 0;
1431 /*
1432 * set ->slice_left to allow preemption for a new process
1433 */
1434 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001435 cfq_mark_cfqq_idle_window(cfqq);
1436 cfq_mark_cfqq_prio_changed(cfqq);
1437 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
1440 if (new_cfqq)
1441 kmem_cache_free(cfq_pool, new_cfqq);
1442
1443 atomic_inc(&cfqq->ref);
1444out:
1445 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1446 return cfqq;
1447}
1448
Jens Axboee2d74ac2006-03-28 08:59:01 +02001449static struct cfq_io_context *
1450cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1451{
1452 struct rb_node *n = ioc->cic_root.rb_node;
1453 struct cfq_io_context *cic;
1454 void *key = cfqd;
1455
1456 while (n) {
1457 cic = rb_entry(n, struct cfq_io_context, rb_node);
1458
1459 if (key < cic->key)
1460 n = n->rb_left;
1461 else if (key > cic->key)
1462 n = n->rb_right;
1463 else
1464 return cic;
1465 }
1466
1467 return NULL;
1468}
1469
1470static inline void
1471cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1472 struct cfq_io_context *cic)
1473{
1474 struct rb_node **p = &ioc->cic_root.rb_node;
1475 struct rb_node *parent = NULL;
1476 struct cfq_io_context *__cic;
1477
1478 read_lock(&cfq_exit_lock);
1479
1480 cic->ioc = ioc;
1481 cic->key = cfqd;
1482
1483 ioc->set_ioprio = cfq_ioc_set_ioprio;
1484
1485 while (*p) {
1486 parent = *p;
1487 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1488
1489 if (cic->key < __cic->key)
1490 p = &(*p)->rb_left;
1491 else if (cic->key > __cic->key)
1492 p = &(*p)->rb_right;
1493 else
1494 BUG();
1495 }
1496
1497 rb_link_node(&cic->rb_node, parent, p);
1498 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1499 list_add(&cic->queue_list, &cfqd->cic_list);
1500 read_unlock(&cfq_exit_lock);
1501}
1502
Jens Axboe22e2c502005-06-27 10:55:12 +02001503/*
1504 * Setup general io context and cfq io context. There can be several cfq
1505 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001506 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001507 */
1508static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001509cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Jens Axboe22e2c502005-06-27 10:55:12 +02001511 struct io_context *ioc = NULL;
1512 struct cfq_io_context *cic;
1513
1514 might_sleep_if(gfp_mask & __GFP_WAIT);
1515
1516 ioc = get_io_context(gfp_mask);
1517 if (!ioc)
1518 return NULL;
1519
Jens Axboee2d74ac2006-03-28 08:59:01 +02001520 cic = cfq_cic_rb_lookup(cfqd, ioc);
1521 if (cic)
1522 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001523
Jens Axboee2d74ac2006-03-28 08:59:01 +02001524 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1525 if (cic == NULL)
1526 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001527
Jens Axboee2d74ac2006-03-28 08:59:01 +02001528 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001529out:
1530 return cic;
1531err:
1532 put_io_context(ioc);
1533 return NULL;
1534}
1535
1536static void
1537cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1538{
1539 unsigned long elapsed, ttime;
1540
1541 /*
1542 * if this context already has stuff queued, thinktime is from
1543 * last queue not last end
1544 */
1545#if 0
1546 if (time_after(cic->last_end_request, cic->last_queue))
1547 elapsed = jiffies - cic->last_end_request;
1548 else
1549 elapsed = jiffies - cic->last_queue;
1550#else
1551 elapsed = jiffies - cic->last_end_request;
1552#endif
1553
1554 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1555
1556 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1557 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1558 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1559}
1560
1561#define sample_valid(samples) ((samples) > 80)
1562
1563/*
1564 * Disable idle window if the process thinks too long or seeks so much that
1565 * it doesn't matter
1566 */
1567static void
1568cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1569 struct cfq_io_context *cic)
1570{
Jens Axboe3b181522005-06-27 10:56:24 +02001571 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001572
1573 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1574 enable_idle = 0;
1575 else if (sample_valid(cic->ttime_samples)) {
1576 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1577 enable_idle = 0;
1578 else
1579 enable_idle = 1;
1580 }
1581
Jens Axboe3b181522005-06-27 10:56:24 +02001582 if (enable_idle)
1583 cfq_mark_cfqq_idle_window(cfqq);
1584 else
1585 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001586}
1587
1588
1589/*
1590 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1591 * no or if we aren't sure, a 1 will cause a preempt.
1592 */
1593static int
1594cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1595 struct cfq_rq *crq)
1596{
1597 struct cfq_queue *cfqq = cfqd->active_queue;
1598
1599 if (cfq_class_idle(new_cfqq))
1600 return 0;
1601
1602 if (!cfqq)
1603 return 1;
1604
1605 if (cfq_class_idle(cfqq))
1606 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001607 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001608 return 0;
1609 /*
1610 * if it doesn't have slice left, forget it
1611 */
1612 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1613 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001614 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001615 return 1;
1616
1617 return 0;
1618}
1619
1620/*
1621 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1622 * let it have half of its nominal slice.
1623 */
1624static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1625{
1626 struct cfq_queue *__cfqq, *next;
1627
1628 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1629 cfq_resort_rr_list(__cfqq, 1);
1630
1631 if (!cfqq->slice_left)
1632 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1633
1634 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001635 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001636 __cfq_set_active_queue(cfqd, cfqq);
1637}
1638
1639/*
1640 * should really be a ll_rw_blk.c helper
1641 */
1642static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1643{
1644 request_queue_t *q = cfqd->queue;
1645
1646 if (!blk_queue_plugged(q))
1647 q->request_fn(q);
1648 else
1649 __generic_unplug_device(q);
1650}
1651
1652/*
1653 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1654 * something we should do about it
1655 */
1656static void
1657cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1658 struct cfq_rq *crq)
1659{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001660 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001661
1662 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1663
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001664 /*
1665 * we never wait for an async request and we don't allow preemption
1666 * of an async request. so just return early
1667 */
1668 if (!cfq_crq_is_sync(crq))
1669 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001670
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001671 cic = crq->io_context;
Jens Axboe22e2c502005-06-27 10:55:12 +02001672
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001673 cfq_update_io_thinktime(cfqd, cic);
1674 cfq_update_idle_window(cfqd, cfqq, cic);
1675
1676 cic->last_queue = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001677
1678 if (cfqq == cfqd->active_queue) {
1679 /*
1680 * if we are waiting for a request for this queue, let it rip
1681 * immediately and flag that we must not expire this queue
1682 * just now
1683 */
Jens Axboe3b181522005-06-27 10:56:24 +02001684 if (cfq_cfqq_wait_request(cfqq)) {
1685 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001686 del_timer(&cfqd->idle_slice_timer);
1687 cfq_start_queueing(cfqd, cfqq);
1688 }
1689 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1690 /*
1691 * not the active queue - expire current slice if it is
1692 * idle and has expired it's mean thinktime or this new queue
1693 * has some old slice time left and is of higher priority
1694 */
1695 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001696 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001697 cfq_start_queueing(cfqd, cfqq);
1698 }
1699}
1700
Jens Axboeb4878f22005-10-20 16:42:29 +02001701static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001702{
Jens Axboeb4878f22005-10-20 16:42:29 +02001703 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001704 struct cfq_rq *crq = RQ_DATA(rq);
1705 struct cfq_queue *cfqq = crq->cfq_queue;
1706
1707 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jens Axboe22e2c502005-06-27 10:55:12 +02001711 list_add_tail(&rq->queuelist, &cfqq->fifo);
1712
Tejun Heo98b11472005-10-20 16:46:54 +02001713 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001714 cfq_add_crq_hash(cfqd, crq);
1715
Jens Axboe22e2c502005-06-27 10:55:12 +02001716 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719static void cfq_completed_request(request_queue_t *q, struct request *rq)
1720{
1721 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001722 struct cfq_queue *cfqq = crq->cfq_queue;
1723 struct cfq_data *cfqd = cfqq->cfqd;
1724 const int sync = cfq_crq_is_sync(crq);
1725 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Jens Axboeb4878f22005-10-20 16:42:29 +02001727 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Jens Axboeb4878f22005-10-20 16:42:29 +02001729 WARN_ON(!cfqd->rq_in_driver);
1730 WARN_ON(!cfqq->on_dispatch[sync]);
1731 cfqd->rq_in_driver--;
1732 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Jens Axboeb4878f22005-10-20 16:42:29 +02001734 if (!cfq_class_idle(cfqq))
1735 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001736
Jens Axboeb4878f22005-10-20 16:42:29 +02001737 if (!cfq_cfqq_dispatched(cfqq)) {
1738 if (cfq_cfqq_on_rr(cfqq)) {
1739 cfqq->service_last = now;
1740 cfq_resort_rr_list(cfqq, 0);
1741 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001742 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
Jens Axboeb4878f22005-10-20 16:42:29 +02001745 if (cfq_crq_is_sync(crq))
1746 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749static struct request *
1750cfq_former_request(request_queue_t *q, struct request *rq)
1751{
1752 struct cfq_rq *crq = RQ_DATA(rq);
1753 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1754
1755 if (rbprev)
1756 return rb_entry_crq(rbprev)->request;
1757
1758 return NULL;
1759}
1760
1761static struct request *
1762cfq_latter_request(request_queue_t *q, struct request *rq)
1763{
1764 struct cfq_rq *crq = RQ_DATA(rq);
1765 struct rb_node *rbnext = rb_next(&crq->rb_node);
1766
1767 if (rbnext)
1768 return rb_entry_crq(rbnext)->request;
1769
1770 return NULL;
1771}
1772
Jens Axboe22e2c502005-06-27 10:55:12 +02001773/*
1774 * we temporarily boost lower priority queues if they are holding fs exclusive
1775 * resources. they are boosted to normal prio (CLASS_BE/4)
1776 */
1777static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Jens Axboe22e2c502005-06-27 10:55:12 +02001779 const int ioprio_class = cfqq->ioprio_class;
1780 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jens Axboe22e2c502005-06-27 10:55:12 +02001782 if (has_fs_excl()) {
1783 /*
1784 * boost idle prio on transactions that would lock out other
1785 * users of the filesystem
1786 */
1787 if (cfq_class_idle(cfqq))
1788 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1789 if (cfqq->ioprio > IOPRIO_NORM)
1790 cfqq->ioprio = IOPRIO_NORM;
1791 } else {
1792 /*
1793 * check if we need to unboost the queue
1794 */
1795 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1796 cfqq->ioprio_class = cfqq->org_ioprio_class;
1797 if (cfqq->ioprio != cfqq->org_ioprio)
1798 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
Jens Axboe22e2c502005-06-27 10:55:12 +02001801 /*
1802 * refile between round-robin lists if we moved the priority class
1803 */
1804 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001805 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001806 cfq_resort_rr_list(cfqq, 0);
1807}
1808
1809static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1810{
1811 if (rw == READ || process_sync(task))
1812 return task->pid;
1813
1814 return CFQ_KEY_ASYNC;
1815}
1816
1817static inline int
1818__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1819 struct task_struct *task, int rw)
1820{
Jens Axboe3b181522005-06-27 10:56:24 +02001821#if 1
1822 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001823 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001824 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001825 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001826 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001827
1828 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001829#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001830 if (!cfqq || task->flags & PF_MEMALLOC)
1831 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001832 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1833 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001834 return ELV_MQUEUE_MUST;
1835
1836 /*
1837 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1838 * can quickly flood the queue with writes from a single task
1839 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001840 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001841 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001842 return ELV_MQUEUE_MUST;
1843 }
1844
1845 return ELV_MQUEUE_MAY;
1846 }
1847 if (cfq_class_idle(cfqq))
1848 return ELV_MQUEUE_NO;
1849 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1850 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1851 int ret = ELV_MQUEUE_NO;
1852
1853 if (ioc && ioc->nr_batch_requests)
1854 ret = ELV_MQUEUE_MAY;
1855
1856 put_io_context(ioc);
1857 return ret;
1858 }
1859
1860 return ELV_MQUEUE_MAY;
1861#endif
1862}
1863
1864static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1865{
1866 struct cfq_data *cfqd = q->elevator->elevator_data;
1867 struct task_struct *tsk = current;
1868 struct cfq_queue *cfqq;
1869
1870 /*
1871 * don't force setup of a queue from here, as a call to may_queue
1872 * does not necessarily imply that a request actually will be queued.
1873 * so just lookup a possibly existing queue, or return 'may queue'
1874 * if that fails
1875 */
Jens Axboe3b181522005-06-27 10:56:24 +02001876 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001877 if (cfqq) {
1878 cfq_init_prio_data(cfqq);
1879 cfq_prio_boost(cfqq);
1880
1881 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1882 }
1883
1884 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1888{
Jens Axboe22e2c502005-06-27 10:55:12 +02001889 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Jens Axboe22e2c502005-06-27 10:55:12 +02001892 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1893 smp_mb();
1894 if (waitqueue_active(&rl->wait[READ]))
1895 wake_up(&rl->wait[READ]);
1896 }
1897
1898 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1899 smp_mb();
1900 if (waitqueue_active(&rl->wait[WRITE]))
1901 wake_up(&rl->wait[WRITE]);
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905/*
1906 * queue lock held here
1907 */
1908static void cfq_put_request(request_queue_t *q, struct request *rq)
1909{
1910 struct cfq_data *cfqd = q->elevator->elevator_data;
1911 struct cfq_rq *crq = RQ_DATA(rq);
1912
1913 if (crq) {
1914 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001915 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Jens Axboe22e2c502005-06-27 10:55:12 +02001917 BUG_ON(!cfqq->allocated[rw]);
1918 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jens Axboe22e2c502005-06-27 10:55:12 +02001920 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 mempool_free(crq, cfqd->crq_pool);
1923 rq->elevator_private = NULL;
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 cfq_check_waiters(q, cfqq);
1926 cfq_put_queue(cfqq);
1927 }
1928}
1929
1930/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001931 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001933static int
1934cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001935 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001938 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct cfq_io_context *cic;
1940 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001941 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001942 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct cfq_rq *crq;
1944 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001945 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 might_sleep_if(gfp_mask & __GFP_WAIT);
1948
Jens Axboee2d74ac2006-03-28 08:59:01 +02001949 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 spin_lock_irqsave(q->queue_lock, flags);
1952
Jens Axboe22e2c502005-06-27 10:55:12 +02001953 if (!cic)
1954 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Al Viro12a05732006-03-18 13:38:01 -05001956 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001957 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001958 if (!cfqq)
1959 goto queue_fail;
1960
Al Viro12a05732006-03-18 13:38:01 -05001961 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001962 } else
Al Viro12a05732006-03-18 13:38:01 -05001963 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001966 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001967 cfqd->rq_starved = 0;
1968 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 spin_unlock_irqrestore(q->queue_lock, flags);
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1972 if (crq) {
1973 RB_CLEAR(&crq->rb_node);
1974 crq->rb_key = 0;
1975 crq->request = rq;
1976 INIT_HLIST_NODE(&crq->hash);
1977 crq->cfq_queue = cfqq;
1978 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001979
Al Viro12a05732006-03-18 13:38:01 -05001980 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02001981 cfq_mark_crq_is_sync(crq);
1982 else
1983 cfq_clear_crq_is_sync(crq);
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_lock_irqsave(q->queue_lock, flags);
1990 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02001991 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02001992 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001994queue_fail:
1995 if (cic)
1996 put_io_context(cic->ioc);
1997 /*
1998 * mark us rq allocation starved. we need to kickstart the process
1999 * ourselves if there are no pending requests that can do it for us.
2000 * that would be an extremely rare OOM situation
2001 */
2002 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002003 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 spin_unlock_irqrestore(q->queue_lock, flags);
2005 return 1;
2006}
2007
Jens Axboe22e2c502005-06-27 10:55:12 +02002008static void cfq_kick_queue(void *data)
2009{
2010 request_queue_t *q = data;
2011 struct cfq_data *cfqd = q->elevator->elevator_data;
2012 unsigned long flags;
2013
2014 spin_lock_irqsave(q->queue_lock, flags);
2015
2016 if (cfqd->rq_starved) {
2017 struct request_list *rl = &q->rq;
2018
2019 /*
2020 * we aren't guaranteed to get a request after this, but we
2021 * have to be opportunistic
2022 */
2023 smp_mb();
2024 if (waitqueue_active(&rl->wait[READ]))
2025 wake_up(&rl->wait[READ]);
2026 if (waitqueue_active(&rl->wait[WRITE]))
2027 wake_up(&rl->wait[WRITE]);
2028 }
2029
2030 blk_remove_plug(q);
2031 q->request_fn(q);
2032 spin_unlock_irqrestore(q->queue_lock, flags);
2033}
2034
2035/*
2036 * Timer running if the active_queue is currently idling inside its time slice
2037 */
2038static void cfq_idle_slice_timer(unsigned long data)
2039{
2040 struct cfq_data *cfqd = (struct cfq_data *) data;
2041 struct cfq_queue *cfqq;
2042 unsigned long flags;
2043
2044 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2045
2046 if ((cfqq = cfqd->active_queue) != NULL) {
2047 unsigned long now = jiffies;
2048
2049 /*
2050 * expired
2051 */
2052 if (time_after(now, cfqq->slice_end))
2053 goto expire;
2054
2055 /*
2056 * only expire and reinvoke request handler, if there are
2057 * other queues with pending requests
2058 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002059 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002060 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2061 add_timer(&cfqd->idle_slice_timer);
2062 goto out_cont;
2063 }
2064
2065 /*
2066 * not expired and it has a request pending, let it dispatch
2067 */
2068 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002069 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002070 goto out_kick;
2071 }
2072 }
2073expire:
2074 cfq_slice_expired(cfqd, 0);
2075out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002076 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002077out_cont:
2078 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2079}
2080
2081/*
2082 * Timer running if an idle class queue is waiting for service
2083 */
2084static void cfq_idle_class_timer(unsigned long data)
2085{
2086 struct cfq_data *cfqd = (struct cfq_data *) data;
2087 unsigned long flags, end;
2088
2089 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2090
2091 /*
2092 * race with a non-idle queue, reset timer
2093 */
2094 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2095 if (!time_after_eq(jiffies, end)) {
2096 cfqd->idle_class_timer.expires = end;
2097 add_timer(&cfqd->idle_class_timer);
2098 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002099 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002100
2101 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2102}
2103
Jens Axboe3b181522005-06-27 10:56:24 +02002104static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2105{
2106 del_timer_sync(&cfqd->idle_slice_timer);
2107 del_timer_sync(&cfqd->idle_class_timer);
2108 blk_sync_queue(cfqd->queue);
2109}
Jens Axboe22e2c502005-06-27 10:55:12 +02002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111static void cfq_exit_queue(elevator_t *e)
2112{
Jens Axboe22e2c502005-06-27 10:55:12 +02002113 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002114 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002115
Jens Axboe3b181522005-06-27 10:56:24 +02002116 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002117
Al Virod9ff4182006-03-18 13:51:22 -05002118 write_lock(&cfq_exit_lock);
2119 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002120
Al Virod9ff4182006-03-18 13:51:22 -05002121 if (cfqd->active_queue)
2122 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002123
2124 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002125 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2126 struct cfq_io_context,
2127 queue_list);
2128 if (cic->cfqq[ASYNC]) {
2129 cfq_put_queue(cic->cfqq[ASYNC]);
2130 cic->cfqq[ASYNC] = NULL;
2131 }
2132 if (cic->cfqq[SYNC]) {
2133 cfq_put_queue(cic->cfqq[SYNC]);
2134 cic->cfqq[SYNC] = NULL;
2135 }
2136 cic->key = NULL;
2137 list_del_init(&cic->queue_list);
2138 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002139
Al Virod9ff4182006-03-18 13:51:22 -05002140 spin_unlock_irq(q->queue_lock);
2141 write_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002142
2143 cfq_shutdown_timer_wq(cfqd);
2144
2145 mempool_destroy(cfqd->crq_pool);
2146 kfree(cfqd->crq_hash);
2147 kfree(cfqd->cfq_hash);
2148 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2152{
2153 struct cfq_data *cfqd;
2154 int i;
2155
2156 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2157 if (!cfqd)
2158 return -ENOMEM;
2159
2160 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002161
2162 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2163 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2164
2165 INIT_LIST_HEAD(&cfqd->busy_rr);
2166 INIT_LIST_HEAD(&cfqd->cur_rr);
2167 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002169 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2172 if (!cfqd->crq_hash)
2173 goto out_crqhash;
2174
2175 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2176 if (!cfqd->cfq_hash)
2177 goto out_cfqhash;
2178
Matthew Dobson93d23412006-03-26 01:37:50 -08002179 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!cfqd->crq_pool)
2181 goto out_crqpool;
2182
2183 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2184 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2185 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2186 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2187
2188 e->elevator_data = cfqd;
2189
2190 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jens Axboe22e2c502005-06-27 10:55:12 +02002192 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002194
2195 init_timer(&cfqd->idle_slice_timer);
2196 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2197 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2198
2199 init_timer(&cfqd->idle_class_timer);
2200 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2201 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2202
2203 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 cfqd->cfq_queued = cfq_queued;
2206 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002207 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2208 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 cfqd->cfq_back_max = cfq_back_max;
2210 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002211 cfqd->cfq_slice[0] = cfq_slice_async;
2212 cfqd->cfq_slice[1] = cfq_slice_sync;
2213 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2214 cfqd->cfq_slice_idle = cfq_slice_idle;
2215 cfqd->cfq_max_depth = cfq_max_depth;
Jens Axboe3b181522005-06-27 10:56:24 +02002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218out_crqpool:
2219 kfree(cfqd->cfq_hash);
2220out_cfqhash:
2221 kfree(cfqd->crq_hash);
2222out_crqhash:
2223 kfree(cfqd);
2224 return -ENOMEM;
2225}
2226
2227static void cfq_slab_kill(void)
2228{
2229 if (crq_pool)
2230 kmem_cache_destroy(crq_pool);
2231 if (cfq_pool)
2232 kmem_cache_destroy(cfq_pool);
2233 if (cfq_ioc_pool)
2234 kmem_cache_destroy(cfq_ioc_pool);
2235}
2236
2237static int __init cfq_slab_setup(void)
2238{
2239 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2240 NULL, NULL);
2241 if (!crq_pool)
2242 goto fail;
2243
2244 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2245 NULL, NULL);
2246 if (!cfq_pool)
2247 goto fail;
2248
2249 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2250 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2251 if (!cfq_ioc_pool)
2252 goto fail;
2253
2254 return 0;
2255fail:
2256 cfq_slab_kill();
2257 return -ENOMEM;
2258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/*
2261 * sysfs parts below -->
2262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264static ssize_t
2265cfq_var_show(unsigned int var, char *page)
2266{
2267 return sprintf(page, "%d\n", var);
2268}
2269
2270static ssize_t
2271cfq_var_store(unsigned int *var, const char *page, size_t count)
2272{
2273 char *p = (char *) page;
2274
2275 *var = simple_strtoul(p, &p, 10);
2276 return count;
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002280static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002282 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 unsigned int __data = __VAR; \
2284 if (__CONV) \
2285 __data = jiffies_to_msecs(__data); \
2286 return cfq_var_show(__data, (page)); \
2287}
2288SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2289SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002290SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2291SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002292SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2293SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002294SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2295SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2296SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2297SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2298SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299#undef SHOW_FUNCTION
2300
2301#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002302static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002304 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 unsigned int __data; \
2306 int ret = cfq_var_store(&__data, (page), count); \
2307 if (__data < (MIN)) \
2308 __data = (MIN); \
2309 else if (__data > (MAX)) \
2310 __data = (MAX); \
2311 if (__CONV) \
2312 *(__PTR) = msecs_to_jiffies(__data); \
2313 else \
2314 *(__PTR) = __data; \
2315 return ret; \
2316}
2317STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2318STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002319STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2320STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002321STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2322STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002323STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2324STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2325STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2326STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2327STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328#undef STORE_FUNCTION
2329
Al Viroe572ec72006-03-18 22:27:18 -05002330#define CFQ_ATTR(name) \
2331 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002332
Al Viroe572ec72006-03-18 22:27:18 -05002333static struct elv_fs_entry cfq_attrs[] = {
2334 CFQ_ATTR(quantum),
2335 CFQ_ATTR(queued),
2336 CFQ_ATTR(fifo_expire_sync),
2337 CFQ_ATTR(fifo_expire_async),
2338 CFQ_ATTR(back_seek_max),
2339 CFQ_ATTR(back_seek_penalty),
2340 CFQ_ATTR(slice_sync),
2341 CFQ_ATTR(slice_async),
2342 CFQ_ATTR(slice_async_rq),
2343 CFQ_ATTR(slice_idle),
2344 CFQ_ATTR(max_depth),
2345 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346};
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static struct elevator_type iosched_cfq = {
2349 .ops = {
2350 .elevator_merge_fn = cfq_merge,
2351 .elevator_merged_fn = cfq_merged_request,
2352 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002353 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002355 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 .elevator_deactivate_req_fn = cfq_deactivate_request,
2357 .elevator_queue_empty_fn = cfq_queue_empty,
2358 .elevator_completed_req_fn = cfq_completed_request,
2359 .elevator_former_req_fn = cfq_former_request,
2360 .elevator_latter_req_fn = cfq_latter_request,
2361 .elevator_set_req_fn = cfq_set_request,
2362 .elevator_put_req_fn = cfq_put_request,
2363 .elevator_may_queue_fn = cfq_may_queue,
2364 .elevator_init_fn = cfq_init_queue,
2365 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002366 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 },
Al Viro3d1ab402006-03-18 18:35:43 -05002368 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .elevator_name = "cfq",
2370 .elevator_owner = THIS_MODULE,
2371};
2372
2373static int __init cfq_init(void)
2374{
2375 int ret;
2376
Jens Axboe22e2c502005-06-27 10:55:12 +02002377 /*
2378 * could be 0 on HZ < 1000 setups
2379 */
2380 if (!cfq_slice_async)
2381 cfq_slice_async = 1;
2382 if (!cfq_slice_idle)
2383 cfq_slice_idle = 1;
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (cfq_slab_setup())
2386 return -ENOMEM;
2387
2388 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002389 if (ret)
2390 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return ret;
2393}
2394
2395static void __exit cfq_exit(void)
2396{
Al Viro334e94d2006-03-18 15:05:53 -05002397 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002399 ioc_gone = &all_gone;
2400 barrier();
2401 if (atomic_read(&ioc_count))
2402 complete(ioc_gone);
2403 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002404 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
2407module_init(cfq_init);
2408module_exit(cfq_exit);
2409
2410MODULE_AUTHOR("Jens Axboe");
2411MODULE_LICENSE("GPL");
2412MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");