blob: 15152e2da0d28f7abe42831fc0159f7807587052 [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
Andreas Mohre8a99052006-03-28 08:59:49 +0200349 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200356#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
357#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
358 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (crq1 == NULL || crq1 == crq2)
361 return crq2;
362 if (crq2 == NULL)
363 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200364
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200365 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
366 return crq1;
367 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200368 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 s1 = crq1->request->sector;
371 s2 = crq2->request->sector;
372
373 last = cfqd->last_sector;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * by definition, 1KiB is 2 sectors
377 */
378 back_max = cfqd->cfq_back_max * 2;
379
380 /*
381 * Strict one way elevator _except_ in the case where we allow
382 * short backward seeks which are biased as twice the cost of a
383 * similar forward seek.
384 */
385 if (s1 >= last)
386 d1 = s1 - last;
387 else if (s1 + back_max >= last)
388 d1 = (last - s1) * cfqd->cfq_back_penalty;
389 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200390 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (s2 >= last)
393 d2 = s2 - last;
394 else if (s2 + back_max >= last)
395 d2 = (last - s2) * cfqd->cfq_back_penalty;
396 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200397 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Andreas Mohre8a99052006-03-28 08:59:49 +0200401 /*
402 * By doing switch() on the bit mask "wrap" we avoid having to
403 * check two variables for all permutations: --> faster!
404 */
405 switch (wrap) {
406 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
407 if (d1 < d2)
408 return crq1;
409 else if (d2 < d1)
410 return crq2;
411 else {
412 if (s1 >= s2)
413 return crq1;
414 else
415 return crq2;
416 }
417
418 case CFQ_RQ2_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return crq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200420 case CFQ_RQ1_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return crq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200422 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
423 default:
424 /*
425 * Since both rqs are wrapped,
426 * start with the one that's further behind head
427 * (--> only *one* back seek required),
428 * since back seek takes more time than forward.
429 */
430 if (s1 <= s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return crq1;
432 else
433 return crq2;
434 }
435}
436
437/*
438 * would be nice to take fifo expire time into account as well
439 */
440static struct cfq_rq *
441cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
442 struct cfq_rq *last)
443{
444 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
445 struct rb_node *rbnext, *rbprev;
446
Jens Axboeb4878f22005-10-20 16:42:29 +0200447 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200449 if (rbnext == &last->rb_node)
450 rbnext = NULL;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 rbprev = rb_prev(&last->rb_node);
454
455 if (rbprev)
456 crq_prev = rb_entry_crq(rbprev);
457 if (rbnext)
458 crq_next = rb_entry_crq(rbnext);
459
460 return cfq_choose_req(cfqd, crq_next, crq_prev);
461}
462
463static void cfq_update_next_crq(struct cfq_rq *crq)
464{
465 struct cfq_queue *cfqq = crq->cfq_queue;
466
467 if (cfqq->next_crq == crq)
468 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
469}
470
Jens Axboe22e2c502005-06-27 10:55:12 +0200471static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jens Axboe22e2c502005-06-27 10:55:12 +0200473 struct cfq_data *cfqd = cfqq->cfqd;
474 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jens Axboe3b181522005-06-27 10:56:24 +0200476 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 list_del(&cfqq->cfq_list);
479
Jens Axboe22e2c502005-06-27 10:55:12 +0200480 if (cfq_class_rt(cfqq))
481 list = &cfqd->cur_rr;
482 else if (cfq_class_idle(cfqq))
483 list = &cfqd->idle_rr;
484 else {
485 /*
486 * if cfqq has requests in flight, don't allow it to be
487 * found in cfq_set_active_queue before it has finished them.
488 * this is done to increase fairness between a process that
489 * has lots of io pending vs one that only generates one
490 * sporadically or synchronously
491 */
Jens Axboe3b181522005-06-27 10:56:24 +0200492 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200493 list = &cfqd->busy_rr;
494 else
495 list = &cfqd->rr_list[cfqq->ioprio];
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200499 * if queue was preempted, just add to front to be fair. busy_rr
500 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200502 if (preempted || list == &cfqd->busy_rr) {
503 list_add(&cfqq->cfq_list, list);
504 return;
505 }
506
507 /*
508 * sort by when queue was last serviced
509 */
510 entry = list;
511 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
513
Jens Axboe22e2c502005-06-27 10:55:12 +0200514 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200516 if (time_before(__cfqq->service_last, cfqq->service_last))
517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 list_add(&cfqq->cfq_list, entry);
521}
522
523/*
524 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200525 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
527static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200528cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jens Axboe3b181522005-06-27 10:56:24 +0200530 BUG_ON(cfq_cfqq_on_rr(cfqq));
531 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 cfqd->busy_queues++;
533
Jens Axboeb4878f22005-10-20 16:42:29 +0200534 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static inline void
538cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
539{
Jens Axboe3b181522005-06-27 10:56:24 +0200540 BUG_ON(!cfq_cfqq_on_rr(cfqq));
541 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200542 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 BUG_ON(!cfqd->busy_queues);
545 cfqd->busy_queues--;
546}
547
548/*
549 * rb tree support functions
550 */
551static inline void cfq_del_crq_rb(struct cfq_rq *crq)
552{
553 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200554 struct cfq_data *cfqd = cfqq->cfqd;
555 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jens Axboeb4878f22005-10-20 16:42:29 +0200557 BUG_ON(!cfqq->queued[sync]);
558 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jens Axboeb4878f22005-10-20 16:42:29 +0200560 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Jens Axboeb4878f22005-10-20 16:42:29 +0200562 rb_erase(&crq->rb_node, &cfqq->sort_list);
563 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jens Axboeb4878f22005-10-20 16:42:29 +0200565 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
566 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static struct cfq_rq *
570__cfq_add_crq_rb(struct cfq_rq *crq)
571{
572 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
573 struct rb_node *parent = NULL;
574 struct cfq_rq *__crq;
575
576 while (*p) {
577 parent = *p;
578 __crq = rb_entry_crq(parent);
579
580 if (crq->rb_key < __crq->rb_key)
581 p = &(*p)->rb_left;
582 else if (crq->rb_key > __crq->rb_key)
583 p = &(*p)->rb_right;
584 else
585 return __crq;
586 }
587
588 rb_link_node(&crq->rb_node, parent, p);
589 return NULL;
590}
591
592static void cfq_add_crq_rb(struct cfq_rq *crq)
593{
594 struct cfq_queue *cfqq = crq->cfq_queue;
595 struct cfq_data *cfqd = cfqq->cfqd;
596 struct request *rq = crq->request;
597 struct cfq_rq *__alias;
598
599 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200600 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
603 * looks a little odd, but the first insert might return an alias.
604 * if that happens, put the alias on the dispatch list
605 */
606 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200607 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
610
Jens Axboe3b181522005-06-27 10:56:24 +0200611 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200612 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /*
615 * check if this request is a better next-serve candidate
616 */
617 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
618}
619
620static inline void
621cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
622{
Jens Axboeb4878f22005-10-20 16:42:29 +0200623 rb_erase(&crq->rb_node, &cfqq->sort_list);
624 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 cfq_add_crq_rb(crq);
627}
628
Jens Axboe22e2c502005-06-27 10:55:12 +0200629static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jens Axboe3b181522005-06-27 10:56:24 +0200632 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct rb_node *n;
634
635 if (!cfqq)
636 goto out;
637
638 n = cfqq->sort_list.rb_node;
639 while (n) {
640 struct cfq_rq *crq = rb_entry_crq(n);
641
642 if (sector < crq->rb_key)
643 n = n->rb_left;
644 else if (sector > crq->rb_key)
645 n = n->rb_right;
646 else
647 return crq->request;
648 }
649
650out:
651 return NULL;
652}
653
Jens Axboeb4878f22005-10-20 16:42:29 +0200654static void cfq_activate_request(request_queue_t *q, struct request *rq)
655{
656 struct cfq_data *cfqd = q->elevator->elevator_data;
657
658 cfqd->rq_in_driver++;
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
662{
Jens Axboe22e2c502005-06-27 10:55:12 +0200663 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Jens Axboeb4878f22005-10-20 16:42:29 +0200665 WARN_ON(!cfqd->rq_in_driver);
666 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Jens Axboeb4878f22005-10-20 16:42:29 +0200669static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct cfq_rq *crq = RQ_DATA(rq);
672
Jens Axboeb4878f22005-10-20 16:42:29 +0200673 list_del_init(&rq->queuelist);
674 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200675 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678static int
679cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
680{
681 struct cfq_data *cfqd = q->elevator->elevator_data;
682 struct request *__rq;
683 int ret;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200686 if (__rq && elv_rq_merge_ok(__rq, bio)) {
687 ret = ELEVATOR_BACK_MERGE;
688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
691 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
Jens Axboe22e2c502005-06-27 10:55:12 +0200692 if (__rq && elv_rq_merge_ok(__rq, bio)) {
693 ret = ELEVATOR_FRONT_MERGE;
694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 return ELEVATOR_NO_MERGE;
698out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *req = __rq;
700 return ret;
701}
702
703static void cfq_merged_request(request_queue_t *q, struct request *req)
704{
705 struct cfq_data *cfqd = q->elevator->elevator_data;
706 struct cfq_rq *crq = RQ_DATA(req);
707
708 cfq_del_crq_hash(crq);
709 cfq_add_crq_hash(cfqd, crq);
710
Jens Axboeb4878f22005-10-20 16:42:29 +0200711 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct cfq_queue *cfqq = crq->cfq_queue;
713
714 cfq_update_next_crq(crq);
715 cfq_reposition_crq_rb(cfqq, crq);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719static void
720cfq_merged_requests(request_queue_t *q, struct request *rq,
721 struct request *next)
722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 cfq_merged_request(q, rq);
724
Jens Axboe22e2c502005-06-27 10:55:12 +0200725 /*
726 * reposition in fifo if next is older than rq
727 */
728 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
729 time_before(next->start_time, rq->start_time))
730 list_move(&rq->queuelist, &next->queuelist);
731
Jens Axboeb4878f22005-10-20 16:42:29 +0200732 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200733}
734
735static inline void
736__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
737{
738 if (cfqq) {
739 /*
740 * stop potential idle class queues waiting service
741 */
742 del_timer(&cfqd->idle_class_timer);
743
744 cfqq->slice_start = jiffies;
745 cfqq->slice_end = 0;
746 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200747 cfq_clear_cfqq_must_alloc_slice(cfqq);
748 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200749 }
750
751 cfqd->active_queue = cfqq;
752}
753
754/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100755 * current cfqq expired its slice (or was too idle), select new one
756 */
757static void
758__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
759 int preempted)
760{
761 unsigned long now = jiffies;
762
763 if (cfq_cfqq_wait_request(cfqq))
764 del_timer(&cfqd->idle_slice_timer);
765
766 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
767 cfqq->service_last = now;
768 cfq_schedule_dispatch(cfqd);
769 }
770
771 cfq_clear_cfqq_must_dispatch(cfqq);
772 cfq_clear_cfqq_wait_request(cfqq);
773
774 /*
775 * store what was left of this slice, if the queue idled out
776 * or was preempted
777 */
778 if (time_after(cfqq->slice_end, now))
779 cfqq->slice_left = cfqq->slice_end - now;
780 else
781 cfqq->slice_left = 0;
782
783 if (cfq_cfqq_on_rr(cfqq))
784 cfq_resort_rr_list(cfqq, preempted);
785
786 if (cfqq == cfqd->active_queue)
787 cfqd->active_queue = NULL;
788
789 if (cfqd->active_cic) {
790 put_io_context(cfqd->active_cic->ioc);
791 cfqd->active_cic = NULL;
792 }
793
794 cfqd->dispatch_slice = 0;
795}
796
797static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
798{
799 struct cfq_queue *cfqq = cfqd->active_queue;
800
801 if (cfqq)
802 __cfq_slice_expired(cfqd, cfqq, preempted);
803}
804
805/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200806 * 0
807 * 0,1
808 * 0,1,2
809 * 0,1,2,3
810 * 0,1,2,3,4
811 * 0,1,2,3,4,5
812 * 0,1,2,3,4,5,6
813 * 0,1,2,3,4,5,6,7
814 */
815static int cfq_get_next_prio_level(struct cfq_data *cfqd)
816{
817 int prio, wrap;
818
819 prio = -1;
820 wrap = 0;
821 do {
822 int p;
823
824 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
825 if (!list_empty(&cfqd->rr_list[p])) {
826 prio = p;
827 break;
828 }
829 }
830
831 if (prio != -1)
832 break;
833 cfqd->cur_prio = 0;
834 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
835 cfqd->cur_end_prio = 0;
836 if (wrap)
837 break;
838 wrap = 1;
839 }
840 } while (1);
841
842 if (unlikely(prio == -1))
843 return -1;
844
845 BUG_ON(prio >= CFQ_PRIO_LISTS);
846
847 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
848
849 cfqd->cur_prio = prio + 1;
850 if (cfqd->cur_prio > cfqd->cur_end_prio) {
851 cfqd->cur_end_prio = cfqd->cur_prio;
852 cfqd->cur_prio = 0;
853 }
854 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
855 cfqd->cur_prio = 0;
856 cfqd->cur_end_prio = 0;
857 }
858
859 return prio;
860}
861
Jens Axboe3b181522005-06-27 10:56:24 +0200862static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200863{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100864 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200865
866 /*
867 * if current list is non-empty, grab first entry. if it is empty,
868 * get next prio level and grab first entry then if any are spliced
869 */
870 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
871 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
872
873 /*
874 * if we have idle queues and no rt or be queues had pending
875 * requests, either allow immediate service if the grace period
876 * has passed or arm the idle grace timer
877 */
878 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
879 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
880
881 if (time_after_eq(jiffies, end))
882 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
883 else
884 mod_timer(&cfqd->idle_class_timer, end);
885 }
886
887 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200888 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200889}
890
Jens Axboe22e2c502005-06-27 10:55:12 +0200891static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
892
893{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100894 unsigned long sl;
895
Jens Axboe22e2c502005-06-27 10:55:12 +0200896 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
897 WARN_ON(cfqq != cfqd->active_queue);
898
899 /*
900 * idle is disabled, either manually or by past process history
901 */
902 if (!cfqd->cfq_slice_idle)
903 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200904 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200905 return 0;
906 /*
907 * task has exited, don't wait
908 */
909 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
910 return 0;
911
Jens Axboe3b181522005-06-27 10:56:24 +0200912 cfq_mark_cfqq_must_dispatch(cfqq);
913 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200914
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100915 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
916 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200917 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Jens Axboeb4878f22005-10-20 16:42:29 +0200920static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct cfq_data *cfqd = q->elevator->elevator_data;
923 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200924
925 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200926 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200927 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200928 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931/*
932 * return expired entry, or NULL to just start from scratch in rbtree
933 */
934static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
935{
936 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200937 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct cfq_rq *crq;
939
Jens Axboe3b181522005-06-27 10:56:24 +0200940 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return NULL;
942
Jens Axboe22e2c502005-06-27 10:55:12 +0200943 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200944 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jens Axboe22e2c502005-06-27 10:55:12 +0200946 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
947 rq = crq->request;
948 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200949 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200950 return crq;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
954 return NULL;
955}
956
957/*
Jens Axboe3b181522005-06-27 10:56:24 +0200958 * Scale schedule slice based on io priority. Use the sync time slice only
959 * if a queue is marked sync and has sync io queued. A sync queue with async
960 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200962static inline int
963cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Jens Axboe22e2c502005-06-27 10:55:12 +0200965 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jens Axboe22e2c502005-06-27 10:55:12 +0200967 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jens Axboe22e2c502005-06-27 10:55:12 +0200969 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Jens Axboe22e2c502005-06-27 10:55:12 +0200972static inline void
973cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
974{
975 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
976}
977
978static inline int
979cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
980{
981 const int base_rq = cfqd->cfq_slice_async_rq;
982
983 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
984
985 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
986}
987
988/*
989 * get next queue for service
990 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100991static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200992{
993 unsigned long now = jiffies;
994 struct cfq_queue *cfqq;
995
996 cfqq = cfqd->active_queue;
997 if (!cfqq)
998 goto new_queue;
999
1000 /*
1001 * slice has expired
1002 */
Jens Axboe3b181522005-06-27 10:56:24 +02001003 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1004 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001005
1006 /*
1007 * if queue has requests, dispatch one. if not, check if
1008 * enough slice is left to wait for one
1009 */
1010 if (!RB_EMPTY(&cfqq->sort_list))
1011 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001012 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001013 time_before(now, cfqq->slice_end)) {
1014 if (cfq_arm_slice_timer(cfqd, cfqq))
1015 return NULL;
1016 }
1017
Jens Axboe3b181522005-06-27 10:56:24 +02001018expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001019 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001020new_queue:
1021 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001022keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001023 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001024}
1025
1026static int
1027__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1028 int max_dispatch)
1029{
1030 int dispatched = 0;
1031
1032 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1033
1034 do {
1035 struct cfq_rq *crq;
1036
1037 /*
1038 * follow expired path, else get first next available
1039 */
1040 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1041 crq = cfqq->next_crq;
1042
1043 /*
1044 * finally, insert request into driver dispatch list
1045 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001046 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001047
1048 cfqd->dispatch_slice++;
1049 dispatched++;
1050
1051 if (!cfqd->active_cic) {
1052 atomic_inc(&crq->io_context->ioc->refcount);
1053 cfqd->active_cic = crq->io_context;
1054 }
1055
1056 if (RB_EMPTY(&cfqq->sort_list))
1057 break;
1058
1059 } while (dispatched < max_dispatch);
1060
1061 /*
1062 * if slice end isn't set yet, set it. if at least one request was
1063 * sync, use the sync time slice value
1064 */
1065 if (!cfqq->slice_end)
1066 cfq_set_prio_slice(cfqd, cfqq);
1067
1068 /*
1069 * expire an async queue immediately if it has used up its slice. idle
1070 * queue always expire after 1 dispatch round.
1071 */
1072 if ((!cfq_cfqq_sync(cfqq) &&
1073 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1074 cfq_class_idle(cfqq))
1075 cfq_slice_expired(cfqd, 0);
1076
1077 return dispatched;
1078}
1079
1080static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001081cfq_forced_dispatch_cfqqs(struct list_head *list)
1082{
1083 int dispatched = 0;
1084 struct cfq_queue *cfqq, *next;
1085 struct cfq_rq *crq;
1086
1087 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1088 while ((crq = cfqq->next_crq)) {
1089 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1090 dispatched++;
1091 }
1092 BUG_ON(!list_empty(&cfqq->fifo));
1093 }
1094 return dispatched;
1095}
1096
1097static int
1098cfq_forced_dispatch(struct cfq_data *cfqd)
1099{
1100 int i, dispatched = 0;
1101
1102 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1103 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1104
1105 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1106 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1107 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1108
1109 cfq_slice_expired(cfqd, 0);
1110
1111 BUG_ON(cfqd->busy_queues);
1112
1113 return dispatched;
1114}
1115
1116static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001117cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct cfq_data *cfqd = q->elevator->elevator_data;
1120 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jens Axboe22e2c502005-06-27 10:55:12 +02001122 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001125 if (unlikely(force))
1126 return cfq_forced_dispatch(cfqd);
1127
1128 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001129 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001130 int max_dispatch;
1131
1132 /*
1133 * if idle window is disabled, allow queue buildup
1134 */
1135 if (!cfq_cfqq_idle_window(cfqq) &&
1136 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1137 return 0;
1138
Jens Axboe3b181522005-06-27 10:56:24 +02001139 cfq_clear_cfqq_must_dispatch(cfqq);
1140 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001141 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001143 max_dispatch = cfqd->cfq_quantum;
1144 if (cfq_class_idle(cfqq))
1145 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jens Axboe22e2c502005-06-27 10:55:12 +02001147 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Jens Axboe22e2c502005-06-27 10:55:12 +02001150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
1154 * task holds one reference to the queue, dropped when task exits. each crq
1155 * in-flight on this queue also holds a reference, dropped when crq is freed.
1156 *
1157 * queue lock must be held here.
1158 */
1159static void cfq_put_queue(struct cfq_queue *cfqq)
1160{
Jens Axboe22e2c502005-06-27 10:55:12 +02001161 struct cfq_data *cfqd = cfqq->cfqd;
1162
1163 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (!atomic_dec_and_test(&cfqq->ref))
1166 return;
1167
1168 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001169 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001170 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001172 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001173 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * it's on the empty list and still hashed
1177 */
1178 list_del(&cfqq->cfq_list);
1179 hlist_del(&cfqq->cfq_hash);
1180 kmem_cache_free(cfq_pool, cfqq);
1181}
1182
1183static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001184__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1185 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1188 struct hlist_node *entry, *next;
1189
1190 hlist_for_each_safe(entry, next, hash_list) {
1191 struct cfq_queue *__cfqq = list_entry_qhash(entry);
Al Virob0a69162006-03-14 15:32:50 -05001192 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jens Axboe3b181522005-06-27 10:56:24 +02001194 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return __cfqq;
1196 }
1197
1198 return NULL;
1199}
1200
1201static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001202cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jens Axboe3b181522005-06-27 10:56:24 +02001204 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Jens Axboee2d74ac2006-03-28 08:59:01 +02001207static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jens Axboe22e2c502005-06-27 10:55:12 +02001209 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001210 struct rb_node *n;
1211 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001212
Jens Axboee2d74ac2006-03-28 08:59:01 +02001213 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1214 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1215 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001216 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001217 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001218 }
1219
Al Viro334e94d2006-03-18 15:05:53 -05001220 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1221 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Al Viroe17a9482006-03-18 13:21:20 -05001224static void cfq_trim(struct io_context *ioc)
1225{
1226 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001227 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001231 * Called with interrupts disabled
1232 */
1233static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1234{
Al Viro478a82b2006-03-18 13:25:24 -05001235 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001236 request_queue_t *q;
1237
1238 if (!cfqd)
1239 return;
1240
1241 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001242
1243 WARN_ON(!irqs_disabled());
1244
1245 spin_lock(q->queue_lock);
1246
Al Viro12a05732006-03-18 13:38:01 -05001247 if (cic->cfqq[ASYNC]) {
1248 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1249 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1250 cfq_put_queue(cic->cfqq[ASYNC]);
1251 cic->cfqq[ASYNC] = NULL;
1252 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001253
Al Viro12a05732006-03-18 13:38:01 -05001254 if (cic->cfqq[SYNC]) {
1255 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1256 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1257 cfq_put_queue(cic->cfqq[SYNC]);
1258 cic->cfqq[SYNC] = NULL;
1259 }
1260
Al Viro478a82b2006-03-18 13:25:24 -05001261 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001262 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001263 spin_unlock(q->queue_lock);
1264}
1265
Jens Axboee2d74ac2006-03-28 08:59:01 +02001266static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Jens Axboe22e2c502005-06-27 10:55:12 +02001268 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001270 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * put the reference this task is holding to the various queues
1274 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001275 read_lock_irqsave(&cfq_exit_lock, flags);
1276
1277 n = rb_first(&ioc->cic_root);
1278 while (n != NULL) {
1279 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1280
Jens Axboe22e2c502005-06-27 10:55:12 +02001281 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001282 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Jens Axboee2d74ac2006-03-28 08:59:01 +02001285 read_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Jens Axboe22e2c502005-06-27 10:55:12 +02001288static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001289cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Jens Axboe22e2c502005-06-27 10:55:12 +02001291 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (cic) {
Jens Axboee2d74ac2006-03-28 08:59:01 +02001294 RB_CLEAR(&cic->rb_node);
1295 cic->key = NULL;
Al Viro12a05732006-03-18 13:38:01 -05001296 cic->cfqq[ASYNC] = NULL;
1297 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001298 cic->last_end_request = jiffies;
1299 cic->ttime_total = 0;
1300 cic->ttime_samples = 0;
1301 cic->ttime_mean = 0;
1302 cic->dtor = cfq_free_io_context;
1303 cic->exit = cfq_exit_io_context;
Al Virod9ff4182006-03-18 13:51:22 -05001304 INIT_LIST_HEAD(&cic->queue_list);
Al Viro334e94d2006-03-18 15:05:53 -05001305 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 return cic;
1309}
1310
Jens Axboe22e2c502005-06-27 10:55:12 +02001311static void cfq_init_prio_data(struct cfq_queue *cfqq)
1312{
1313 struct task_struct *tsk = current;
1314 int ioprio_class;
1315
Jens Axboe3b181522005-06-27 10:56:24 +02001316 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001317 return;
1318
1319 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1320 switch (ioprio_class) {
1321 default:
1322 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1323 case IOPRIO_CLASS_NONE:
1324 /*
1325 * no prio set, place us in the middle of the BE classes
1326 */
1327 cfqq->ioprio = task_nice_ioprio(tsk);
1328 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1329 break;
1330 case IOPRIO_CLASS_RT:
1331 cfqq->ioprio = task_ioprio(tsk);
1332 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1333 break;
1334 case IOPRIO_CLASS_BE:
1335 cfqq->ioprio = task_ioprio(tsk);
1336 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1337 break;
1338 case IOPRIO_CLASS_IDLE:
1339 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1340 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001341 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001342 break;
1343 }
1344
1345 /*
1346 * keep track of original prio settings in case we have to temporarily
1347 * elevate the priority of this queue
1348 */
1349 cfqq->org_ioprio = cfqq->ioprio;
1350 cfqq->org_ioprio_class = cfqq->ioprio_class;
1351
Jens Axboe3b181522005-06-27 10:56:24 +02001352 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001353 cfq_resort_rr_list(cfqq, 0);
1354
Jens Axboe3b181522005-06-27 10:56:24 +02001355 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001356}
1357
Al Viro478a82b2006-03-18 13:25:24 -05001358static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001359{
Al Viro478a82b2006-03-18 13:25:24 -05001360 struct cfq_data *cfqd = cic->key;
1361 struct cfq_queue *cfqq;
1362 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001363 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001364 cfqq = cic->cfqq[ASYNC];
1365 if (cfqq) {
Al Viro6f325a12006-03-18 14:58:37 -05001366 struct cfq_queue *new_cfqq;
1367 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
1368 cic->ioc->task, GFP_ATOMIC);
1369 if (new_cfqq) {
1370 cic->cfqq[ASYNC] = new_cfqq;
1371 cfq_put_queue(cfqq);
1372 }
Al Viro12a05732006-03-18 13:38:01 -05001373 }
1374 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001375 if (cfqq) {
1376 cfq_mark_cfqq_prio_changed(cfqq);
1377 cfq_init_prio_data(cfqq);
1378 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001379 spin_unlock(cfqd->queue->queue_lock);
1380 }
1381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001384 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001386static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Al Viroa6a07632006-03-18 13:26:44 -05001388 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001389 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001390
1391 write_lock(&cfq_exit_lock);
1392
Jens Axboee2d74ac2006-03-28 08:59:01 +02001393 n = rb_first(&ioc->cic_root);
1394 while (n != NULL) {
1395 cic = rb_entry(n, struct cfq_io_context, rb_node);
1396
Al Viro478a82b2006-03-18 13:25:24 -05001397 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001398 n = rb_next(n);
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Al Viroa6a07632006-03-18 13:26:44 -05001401 write_unlock(&cfq_exit_lock);
1402
Jens Axboe22e2c502005-06-27 10:55:12 +02001403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001407cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001408 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1411 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001412 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414retry:
Al Viro6f325a12006-03-18 14:58:37 -05001415 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001416 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (!cfqq) {
1419 if (new_cfqq) {
1420 cfqq = new_cfqq;
1421 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001422 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 spin_unlock_irq(cfqd->queue->queue_lock);
1424 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1425 spin_lock_irq(cfqd->queue->queue_lock);
1426 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001427 } else {
1428 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1429 if (!cfqq)
1430 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 memset(cfqq, 0, sizeof(*cfqq));
1434
1435 INIT_HLIST_NODE(&cfqq->cfq_hash);
1436 INIT_LIST_HEAD(&cfqq->cfq_list);
1437 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001438 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 cfqq->key = key;
1441 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1442 atomic_set(&cfqq->ref, 0);
1443 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001444 cfqq->service_last = 0;
1445 /*
1446 * set ->slice_left to allow preemption for a new process
1447 */
1448 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001449 cfq_mark_cfqq_idle_window(cfqq);
1450 cfq_mark_cfqq_prio_changed(cfqq);
1451 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
1454 if (new_cfqq)
1455 kmem_cache_free(cfq_pool, new_cfqq);
1456
1457 atomic_inc(&cfqq->ref);
1458out:
1459 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1460 return cfqq;
1461}
1462
Jens Axboee2d74ac2006-03-28 08:59:01 +02001463static struct cfq_io_context *
1464cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1465{
1466 struct rb_node *n = ioc->cic_root.rb_node;
1467 struct cfq_io_context *cic;
1468 void *key = cfqd;
1469
1470 while (n) {
1471 cic = rb_entry(n, struct cfq_io_context, rb_node);
1472
1473 if (key < cic->key)
1474 n = n->rb_left;
1475 else if (key > cic->key)
1476 n = n->rb_right;
1477 else
1478 return cic;
1479 }
1480
1481 return NULL;
1482}
1483
1484static inline void
1485cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1486 struct cfq_io_context *cic)
1487{
1488 struct rb_node **p = &ioc->cic_root.rb_node;
1489 struct rb_node *parent = NULL;
1490 struct cfq_io_context *__cic;
1491
1492 read_lock(&cfq_exit_lock);
1493
1494 cic->ioc = ioc;
1495 cic->key = cfqd;
1496
1497 ioc->set_ioprio = cfq_ioc_set_ioprio;
1498
1499 while (*p) {
1500 parent = *p;
1501 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
1502
1503 if (cic->key < __cic->key)
1504 p = &(*p)->rb_left;
1505 else if (cic->key > __cic->key)
1506 p = &(*p)->rb_right;
1507 else
1508 BUG();
1509 }
1510
1511 rb_link_node(&cic->rb_node, parent, p);
1512 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1513 list_add(&cic->queue_list, &cfqd->cic_list);
1514 read_unlock(&cfq_exit_lock);
1515}
1516
Jens Axboe22e2c502005-06-27 10:55:12 +02001517/*
1518 * Setup general io context and cfq io context. There can be several cfq
1519 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001520 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001521 */
1522static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001523cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Jens Axboe22e2c502005-06-27 10:55:12 +02001525 struct io_context *ioc = NULL;
1526 struct cfq_io_context *cic;
1527
1528 might_sleep_if(gfp_mask & __GFP_WAIT);
1529
1530 ioc = get_io_context(gfp_mask);
1531 if (!ioc)
1532 return NULL;
1533
Jens Axboee2d74ac2006-03-28 08:59:01 +02001534 cic = cfq_cic_rb_lookup(cfqd, ioc);
1535 if (cic)
1536 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001537
Jens Axboee2d74ac2006-03-28 08:59:01 +02001538 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1539 if (cic == NULL)
1540 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001541
Jens Axboee2d74ac2006-03-28 08:59:01 +02001542 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001543out:
1544 return cic;
1545err:
1546 put_io_context(ioc);
1547 return NULL;
1548}
1549
1550static void
1551cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1552{
1553 unsigned long elapsed, ttime;
1554
1555 /*
1556 * if this context already has stuff queued, thinktime is from
1557 * last queue not last end
1558 */
1559#if 0
1560 if (time_after(cic->last_end_request, cic->last_queue))
1561 elapsed = jiffies - cic->last_end_request;
1562 else
1563 elapsed = jiffies - cic->last_queue;
1564#else
1565 elapsed = jiffies - cic->last_end_request;
1566#endif
1567
1568 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1569
1570 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1571 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1572 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1573}
1574
1575#define sample_valid(samples) ((samples) > 80)
1576
1577/*
1578 * Disable idle window if the process thinks too long or seeks so much that
1579 * it doesn't matter
1580 */
1581static void
1582cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1583 struct cfq_io_context *cic)
1584{
Jens Axboe3b181522005-06-27 10:56:24 +02001585 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001586
1587 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1588 enable_idle = 0;
1589 else if (sample_valid(cic->ttime_samples)) {
1590 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1591 enable_idle = 0;
1592 else
1593 enable_idle = 1;
1594 }
1595
Jens Axboe3b181522005-06-27 10:56:24 +02001596 if (enable_idle)
1597 cfq_mark_cfqq_idle_window(cfqq);
1598 else
1599 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001600}
1601
1602
1603/*
1604 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1605 * no or if we aren't sure, a 1 will cause a preempt.
1606 */
1607static int
1608cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1609 struct cfq_rq *crq)
1610{
1611 struct cfq_queue *cfqq = cfqd->active_queue;
1612
1613 if (cfq_class_idle(new_cfqq))
1614 return 0;
1615
1616 if (!cfqq)
1617 return 1;
1618
1619 if (cfq_class_idle(cfqq))
1620 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001621 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001622 return 0;
1623 /*
1624 * if it doesn't have slice left, forget it
1625 */
1626 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1627 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001628 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001629 return 1;
1630
1631 return 0;
1632}
1633
1634/*
1635 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1636 * let it have half of its nominal slice.
1637 */
1638static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1639{
1640 struct cfq_queue *__cfqq, *next;
1641
1642 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1643 cfq_resort_rr_list(__cfqq, 1);
1644
1645 if (!cfqq->slice_left)
1646 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1647
1648 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001649 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001650 __cfq_set_active_queue(cfqd, cfqq);
1651}
1652
1653/*
1654 * should really be a ll_rw_blk.c helper
1655 */
1656static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1657{
1658 request_queue_t *q = cfqd->queue;
1659
1660 if (!blk_queue_plugged(q))
1661 q->request_fn(q);
1662 else
1663 __generic_unplug_device(q);
1664}
1665
1666/*
1667 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1668 * something we should do about it
1669 */
1670static void
1671cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1672 struct cfq_rq *crq)
1673{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001674 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001675
1676 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1677
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001678 /*
1679 * we never wait for an async request and we don't allow preemption
1680 * of an async request. so just return early
1681 */
1682 if (!cfq_crq_is_sync(crq))
1683 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001684
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001685 cic = crq->io_context;
Jens Axboe22e2c502005-06-27 10:55:12 +02001686
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001687 cfq_update_io_thinktime(cfqd, cic);
1688 cfq_update_idle_window(cfqd, cfqq, cic);
1689
1690 cic->last_queue = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001691
1692 if (cfqq == cfqd->active_queue) {
1693 /*
1694 * if we are waiting for a request for this queue, let it rip
1695 * immediately and flag that we must not expire this queue
1696 * just now
1697 */
Jens Axboe3b181522005-06-27 10:56:24 +02001698 if (cfq_cfqq_wait_request(cfqq)) {
1699 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001700 del_timer(&cfqd->idle_slice_timer);
1701 cfq_start_queueing(cfqd, cfqq);
1702 }
1703 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1704 /*
1705 * not the active queue - expire current slice if it is
1706 * idle and has expired it's mean thinktime or this new queue
1707 * has some old slice time left and is of higher priority
1708 */
1709 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001710 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001711 cfq_start_queueing(cfqd, cfqq);
1712 }
1713}
1714
Jens Axboeb4878f22005-10-20 16:42:29 +02001715static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001716{
Jens Axboeb4878f22005-10-20 16:42:29 +02001717 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001718 struct cfq_rq *crq = RQ_DATA(rq);
1719 struct cfq_queue *cfqq = crq->cfq_queue;
1720
1721 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Jens Axboe22e2c502005-06-27 10:55:12 +02001725 list_add_tail(&rq->queuelist, &cfqq->fifo);
1726
Tejun Heo98b11472005-10-20 16:46:54 +02001727 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001728 cfq_add_crq_hash(cfqd, crq);
1729
Jens Axboe22e2c502005-06-27 10:55:12 +02001730 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733static void cfq_completed_request(request_queue_t *q, struct request *rq)
1734{
1735 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001736 struct cfq_queue *cfqq = crq->cfq_queue;
1737 struct cfq_data *cfqd = cfqq->cfqd;
1738 const int sync = cfq_crq_is_sync(crq);
1739 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Jens Axboeb4878f22005-10-20 16:42:29 +02001741 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jens Axboeb4878f22005-10-20 16:42:29 +02001743 WARN_ON(!cfqd->rq_in_driver);
1744 WARN_ON(!cfqq->on_dispatch[sync]);
1745 cfqd->rq_in_driver--;
1746 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Jens Axboeb4878f22005-10-20 16:42:29 +02001748 if (!cfq_class_idle(cfqq))
1749 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001750
Jens Axboeb4878f22005-10-20 16:42:29 +02001751 if (!cfq_cfqq_dispatched(cfqq)) {
1752 if (cfq_cfqq_on_rr(cfqq)) {
1753 cfqq->service_last = now;
1754 cfq_resort_rr_list(cfqq, 0);
1755 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001756 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Jens Axboeb4878f22005-10-20 16:42:29 +02001759 if (cfq_crq_is_sync(crq))
1760 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763static struct request *
1764cfq_former_request(request_queue_t *q, struct request *rq)
1765{
1766 struct cfq_rq *crq = RQ_DATA(rq);
1767 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1768
1769 if (rbprev)
1770 return rb_entry_crq(rbprev)->request;
1771
1772 return NULL;
1773}
1774
1775static struct request *
1776cfq_latter_request(request_queue_t *q, struct request *rq)
1777{
1778 struct cfq_rq *crq = RQ_DATA(rq);
1779 struct rb_node *rbnext = rb_next(&crq->rb_node);
1780
1781 if (rbnext)
1782 return rb_entry_crq(rbnext)->request;
1783
1784 return NULL;
1785}
1786
Jens Axboe22e2c502005-06-27 10:55:12 +02001787/*
1788 * we temporarily boost lower priority queues if they are holding fs exclusive
1789 * resources. they are boosted to normal prio (CLASS_BE/4)
1790 */
1791static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Jens Axboe22e2c502005-06-27 10:55:12 +02001793 const int ioprio_class = cfqq->ioprio_class;
1794 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jens Axboe22e2c502005-06-27 10:55:12 +02001796 if (has_fs_excl()) {
1797 /*
1798 * boost idle prio on transactions that would lock out other
1799 * users of the filesystem
1800 */
1801 if (cfq_class_idle(cfqq))
1802 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1803 if (cfqq->ioprio > IOPRIO_NORM)
1804 cfqq->ioprio = IOPRIO_NORM;
1805 } else {
1806 /*
1807 * check if we need to unboost the queue
1808 */
1809 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1810 cfqq->ioprio_class = cfqq->org_ioprio_class;
1811 if (cfqq->ioprio != cfqq->org_ioprio)
1812 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814
Jens Axboe22e2c502005-06-27 10:55:12 +02001815 /*
1816 * refile between round-robin lists if we moved the priority class
1817 */
1818 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001819 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001820 cfq_resort_rr_list(cfqq, 0);
1821}
1822
1823static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1824{
1825 if (rw == READ || process_sync(task))
1826 return task->pid;
1827
1828 return CFQ_KEY_ASYNC;
1829}
1830
1831static inline int
1832__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1833 struct task_struct *task, int rw)
1834{
Jens Axboe3b181522005-06-27 10:56:24 +02001835#if 1
1836 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001837 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001838 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001839 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001840 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001841
1842 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001843#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001844 if (!cfqq || task->flags & PF_MEMALLOC)
1845 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001846 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1847 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001848 return ELV_MQUEUE_MUST;
1849
1850 /*
1851 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1852 * can quickly flood the queue with writes from a single task
1853 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001854 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001855 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001856 return ELV_MQUEUE_MUST;
1857 }
1858
1859 return ELV_MQUEUE_MAY;
1860 }
1861 if (cfq_class_idle(cfqq))
1862 return ELV_MQUEUE_NO;
1863 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1864 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1865 int ret = ELV_MQUEUE_NO;
1866
1867 if (ioc && ioc->nr_batch_requests)
1868 ret = ELV_MQUEUE_MAY;
1869
1870 put_io_context(ioc);
1871 return ret;
1872 }
1873
1874 return ELV_MQUEUE_MAY;
1875#endif
1876}
1877
1878static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1879{
1880 struct cfq_data *cfqd = q->elevator->elevator_data;
1881 struct task_struct *tsk = current;
1882 struct cfq_queue *cfqq;
1883
1884 /*
1885 * don't force setup of a queue from here, as a call to may_queue
1886 * does not necessarily imply that a request actually will be queued.
1887 * so just lookup a possibly existing queue, or return 'may queue'
1888 * if that fails
1889 */
Jens Axboe3b181522005-06-27 10:56:24 +02001890 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001891 if (cfqq) {
1892 cfq_init_prio_data(cfqq);
1893 cfq_prio_boost(cfqq);
1894
1895 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1896 }
1897
1898 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
1901static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1902{
Jens Axboe22e2c502005-06-27 10:55:12 +02001903 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jens Axboe22e2c502005-06-27 10:55:12 +02001906 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1907 smp_mb();
1908 if (waitqueue_active(&rl->wait[READ]))
1909 wake_up(&rl->wait[READ]);
1910 }
1911
1912 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1913 smp_mb();
1914 if (waitqueue_active(&rl->wait[WRITE]))
1915 wake_up(&rl->wait[WRITE]);
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
1919/*
1920 * queue lock held here
1921 */
1922static void cfq_put_request(request_queue_t *q, struct request *rq)
1923{
1924 struct cfq_data *cfqd = q->elevator->elevator_data;
1925 struct cfq_rq *crq = RQ_DATA(rq);
1926
1927 if (crq) {
1928 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001929 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Jens Axboe22e2c502005-06-27 10:55:12 +02001931 BUG_ON(!cfqq->allocated[rw]);
1932 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Jens Axboe22e2c502005-06-27 10:55:12 +02001934 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 mempool_free(crq, cfqd->crq_pool);
1937 rq->elevator_private = NULL;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 cfq_check_waiters(q, cfqq);
1940 cfq_put_queue(cfqq);
1941 }
1942}
1943
1944/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001945 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001947static int
1948cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001949 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001952 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct cfq_io_context *cic;
1954 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001955 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001956 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct cfq_rq *crq;
1958 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001959 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 might_sleep_if(gfp_mask & __GFP_WAIT);
1962
Jens Axboee2d74ac2006-03-28 08:59:01 +02001963 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 spin_lock_irqsave(q->queue_lock, flags);
1966
Jens Axboe22e2c502005-06-27 10:55:12 +02001967 if (!cic)
1968 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Al Viro12a05732006-03-18 13:38:01 -05001970 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001971 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001972 if (!cfqq)
1973 goto queue_fail;
1974
Al Viro12a05732006-03-18 13:38:01 -05001975 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001976 } else
Al Viro12a05732006-03-18 13:38:01 -05001977 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001980 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001981 cfqd->rq_starved = 0;
1982 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 spin_unlock_irqrestore(q->queue_lock, flags);
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1986 if (crq) {
1987 RB_CLEAR(&crq->rb_node);
1988 crq->rb_key = 0;
1989 crq->request = rq;
1990 INIT_HLIST_NODE(&crq->hash);
1991 crq->cfq_queue = cfqq;
1992 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001993
Al Viro12a05732006-03-18 13:38:01 -05001994 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02001995 cfq_mark_crq_is_sync(crq);
1996 else
1997 cfq_clear_crq_is_sync(crq);
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return 0;
2001 }
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 spin_lock_irqsave(q->queue_lock, flags);
2004 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02002005 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02002006 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002008queue_fail:
2009 if (cic)
2010 put_io_context(cic->ioc);
2011 /*
2012 * mark us rq allocation starved. we need to kickstart the process
2013 * ourselves if there are no pending requests that can do it for us.
2014 * that would be an extremely rare OOM situation
2015 */
2016 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002017 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 spin_unlock_irqrestore(q->queue_lock, flags);
2019 return 1;
2020}
2021
Jens Axboe22e2c502005-06-27 10:55:12 +02002022static void cfq_kick_queue(void *data)
2023{
2024 request_queue_t *q = data;
2025 struct cfq_data *cfqd = q->elevator->elevator_data;
2026 unsigned long flags;
2027
2028 spin_lock_irqsave(q->queue_lock, flags);
2029
2030 if (cfqd->rq_starved) {
2031 struct request_list *rl = &q->rq;
2032
2033 /*
2034 * we aren't guaranteed to get a request after this, but we
2035 * have to be opportunistic
2036 */
2037 smp_mb();
2038 if (waitqueue_active(&rl->wait[READ]))
2039 wake_up(&rl->wait[READ]);
2040 if (waitqueue_active(&rl->wait[WRITE]))
2041 wake_up(&rl->wait[WRITE]);
2042 }
2043
2044 blk_remove_plug(q);
2045 q->request_fn(q);
2046 spin_unlock_irqrestore(q->queue_lock, flags);
2047}
2048
2049/*
2050 * Timer running if the active_queue is currently idling inside its time slice
2051 */
2052static void cfq_idle_slice_timer(unsigned long data)
2053{
2054 struct cfq_data *cfqd = (struct cfq_data *) data;
2055 struct cfq_queue *cfqq;
2056 unsigned long flags;
2057
2058 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2059
2060 if ((cfqq = cfqd->active_queue) != NULL) {
2061 unsigned long now = jiffies;
2062
2063 /*
2064 * expired
2065 */
2066 if (time_after(now, cfqq->slice_end))
2067 goto expire;
2068
2069 /*
2070 * only expire and reinvoke request handler, if there are
2071 * other queues with pending requests
2072 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002073 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002074 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2075 add_timer(&cfqd->idle_slice_timer);
2076 goto out_cont;
2077 }
2078
2079 /*
2080 * not expired and it has a request pending, let it dispatch
2081 */
2082 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002083 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002084 goto out_kick;
2085 }
2086 }
2087expire:
2088 cfq_slice_expired(cfqd, 0);
2089out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002090 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002091out_cont:
2092 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2093}
2094
2095/*
2096 * Timer running if an idle class queue is waiting for service
2097 */
2098static void cfq_idle_class_timer(unsigned long data)
2099{
2100 struct cfq_data *cfqd = (struct cfq_data *) data;
2101 unsigned long flags, end;
2102
2103 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2104
2105 /*
2106 * race with a non-idle queue, reset timer
2107 */
2108 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2109 if (!time_after_eq(jiffies, end)) {
2110 cfqd->idle_class_timer.expires = end;
2111 add_timer(&cfqd->idle_class_timer);
2112 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002113 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002114
2115 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2116}
2117
Jens Axboe3b181522005-06-27 10:56:24 +02002118static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2119{
2120 del_timer_sync(&cfqd->idle_slice_timer);
2121 del_timer_sync(&cfqd->idle_class_timer);
2122 blk_sync_queue(cfqd->queue);
2123}
Jens Axboe22e2c502005-06-27 10:55:12 +02002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125static void cfq_exit_queue(elevator_t *e)
2126{
Jens Axboe22e2c502005-06-27 10:55:12 +02002127 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002128 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002129
Jens Axboe3b181522005-06-27 10:56:24 +02002130 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002131
Al Virod9ff4182006-03-18 13:51:22 -05002132 write_lock(&cfq_exit_lock);
2133 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002134
Al Virod9ff4182006-03-18 13:51:22 -05002135 if (cfqd->active_queue)
2136 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002137
2138 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002139 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2140 struct cfq_io_context,
2141 queue_list);
2142 if (cic->cfqq[ASYNC]) {
2143 cfq_put_queue(cic->cfqq[ASYNC]);
2144 cic->cfqq[ASYNC] = NULL;
2145 }
2146 if (cic->cfqq[SYNC]) {
2147 cfq_put_queue(cic->cfqq[SYNC]);
2148 cic->cfqq[SYNC] = NULL;
2149 }
2150 cic->key = NULL;
2151 list_del_init(&cic->queue_list);
2152 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002153
Al Virod9ff4182006-03-18 13:51:22 -05002154 spin_unlock_irq(q->queue_lock);
2155 write_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002156
2157 cfq_shutdown_timer_wq(cfqd);
2158
2159 mempool_destroy(cfqd->crq_pool);
2160 kfree(cfqd->crq_hash);
2161 kfree(cfqd->cfq_hash);
2162 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2166{
2167 struct cfq_data *cfqd;
2168 int i;
2169
2170 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2171 if (!cfqd)
2172 return -ENOMEM;
2173
2174 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002175
2176 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2177 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2178
2179 INIT_LIST_HEAD(&cfqd->busy_rr);
2180 INIT_LIST_HEAD(&cfqd->cur_rr);
2181 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002183 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2186 if (!cfqd->crq_hash)
2187 goto out_crqhash;
2188
2189 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2190 if (!cfqd->cfq_hash)
2191 goto out_cfqhash;
2192
Matthew Dobson93d23412006-03-26 01:37:50 -08002193 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!cfqd->crq_pool)
2195 goto out_crqpool;
2196
2197 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2198 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2199 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2200 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2201
2202 e->elevator_data = cfqd;
2203
2204 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Jens Axboe22e2c502005-06-27 10:55:12 +02002206 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002208
2209 init_timer(&cfqd->idle_slice_timer);
2210 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2211 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2212
2213 init_timer(&cfqd->idle_class_timer);
2214 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2215 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2216
2217 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 cfqd->cfq_queued = cfq_queued;
2220 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002221 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2222 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 cfqd->cfq_back_max = cfq_back_max;
2224 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002225 cfqd->cfq_slice[0] = cfq_slice_async;
2226 cfqd->cfq_slice[1] = cfq_slice_sync;
2227 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2228 cfqd->cfq_slice_idle = cfq_slice_idle;
2229 cfqd->cfq_max_depth = cfq_max_depth;
Jens Axboe3b181522005-06-27 10:56:24 +02002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 0;
2232out_crqpool:
2233 kfree(cfqd->cfq_hash);
2234out_cfqhash:
2235 kfree(cfqd->crq_hash);
2236out_crqhash:
2237 kfree(cfqd);
2238 return -ENOMEM;
2239}
2240
2241static void cfq_slab_kill(void)
2242{
2243 if (crq_pool)
2244 kmem_cache_destroy(crq_pool);
2245 if (cfq_pool)
2246 kmem_cache_destroy(cfq_pool);
2247 if (cfq_ioc_pool)
2248 kmem_cache_destroy(cfq_ioc_pool);
2249}
2250
2251static int __init cfq_slab_setup(void)
2252{
2253 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2254 NULL, NULL);
2255 if (!crq_pool)
2256 goto fail;
2257
2258 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2259 NULL, NULL);
2260 if (!cfq_pool)
2261 goto fail;
2262
2263 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2264 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2265 if (!cfq_ioc_pool)
2266 goto fail;
2267
2268 return 0;
2269fail:
2270 cfq_slab_kill();
2271 return -ENOMEM;
2272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274/*
2275 * sysfs parts below -->
2276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278static ssize_t
2279cfq_var_show(unsigned int var, char *page)
2280{
2281 return sprintf(page, "%d\n", var);
2282}
2283
2284static ssize_t
2285cfq_var_store(unsigned int *var, const char *page, size_t count)
2286{
2287 char *p = (char *) page;
2288
2289 *var = simple_strtoul(p, &p, 10);
2290 return count;
2291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002294static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002296 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 unsigned int __data = __VAR; \
2298 if (__CONV) \
2299 __data = jiffies_to_msecs(__data); \
2300 return cfq_var_show(__data, (page)); \
2301}
2302SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2303SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002304SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2305SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002306SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2307SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002308SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2309SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2310SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2311SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2312SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313#undef SHOW_FUNCTION
2314
2315#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002316static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002318 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 unsigned int __data; \
2320 int ret = cfq_var_store(&__data, (page), count); \
2321 if (__data < (MIN)) \
2322 __data = (MIN); \
2323 else if (__data > (MAX)) \
2324 __data = (MAX); \
2325 if (__CONV) \
2326 *(__PTR) = msecs_to_jiffies(__data); \
2327 else \
2328 *(__PTR) = __data; \
2329 return ret; \
2330}
2331STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2332STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002333STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2334STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002335STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2336STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002337STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2338STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2339STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2340STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2341STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342#undef STORE_FUNCTION
2343
Al Viroe572ec72006-03-18 22:27:18 -05002344#define CFQ_ATTR(name) \
2345 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002346
Al Viroe572ec72006-03-18 22:27:18 -05002347static struct elv_fs_entry cfq_attrs[] = {
2348 CFQ_ATTR(quantum),
2349 CFQ_ATTR(queued),
2350 CFQ_ATTR(fifo_expire_sync),
2351 CFQ_ATTR(fifo_expire_async),
2352 CFQ_ATTR(back_seek_max),
2353 CFQ_ATTR(back_seek_penalty),
2354 CFQ_ATTR(slice_sync),
2355 CFQ_ATTR(slice_async),
2356 CFQ_ATTR(slice_async_rq),
2357 CFQ_ATTR(slice_idle),
2358 CFQ_ATTR(max_depth),
2359 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360};
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362static struct elevator_type iosched_cfq = {
2363 .ops = {
2364 .elevator_merge_fn = cfq_merge,
2365 .elevator_merged_fn = cfq_merged_request,
2366 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002367 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002369 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 .elevator_deactivate_req_fn = cfq_deactivate_request,
2371 .elevator_queue_empty_fn = cfq_queue_empty,
2372 .elevator_completed_req_fn = cfq_completed_request,
2373 .elevator_former_req_fn = cfq_former_request,
2374 .elevator_latter_req_fn = cfq_latter_request,
2375 .elevator_set_req_fn = cfq_set_request,
2376 .elevator_put_req_fn = cfq_put_request,
2377 .elevator_may_queue_fn = cfq_may_queue,
2378 .elevator_init_fn = cfq_init_queue,
2379 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002380 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 },
Al Viro3d1ab402006-03-18 18:35:43 -05002382 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 .elevator_name = "cfq",
2384 .elevator_owner = THIS_MODULE,
2385};
2386
2387static int __init cfq_init(void)
2388{
2389 int ret;
2390
Jens Axboe22e2c502005-06-27 10:55:12 +02002391 /*
2392 * could be 0 on HZ < 1000 setups
2393 */
2394 if (!cfq_slice_async)
2395 cfq_slice_async = 1;
2396 if (!cfq_slice_idle)
2397 cfq_slice_idle = 1;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (cfq_slab_setup())
2400 return -ENOMEM;
2401
2402 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002403 if (ret)
2404 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return ret;
2407}
2408
2409static void __exit cfq_exit(void)
2410{
Al Viro334e94d2006-03-18 15:05:53 -05002411 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002413 ioc_gone = &all_gone;
2414 barrier();
2415 if (atomic_read(&ioc_count))
2416 complete(ioc_gone);
2417 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002418 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
2421module_init(cfq_init);
2422module_exit(cfq_exit);
2423
2424MODULE_AUTHOR("Jens Axboe");
2425MODULE_LICENSE("GPL");
2426MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");