blob: 2949920df45dea477507843bc4ddc77b1ab9c256 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
8 */
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/blkdev.h>
12#include <linux/elevator.h>
13#include <linux/bio.h>
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/compiler.h>
19#include <linux/hash.h>
20#include <linux/rbtree.h>
21#include <linux/mempool.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020022#include <linux/ioprio.h>
23#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * tunables
27 */
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_quantum = 4; /* max queue in one round of service */
29static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
30static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
31static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
32static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020035static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010036static const int cfq_slice_async_rq = 2;
Jens Axboe3b181522005-06-27 10:56:24 +020037static int cfq_slice_idle = HZ / 100;
Jens Axboe22e2c502005-06-27 10:55:12 +020038
39#define CFQ_IDLE_GRACE (HZ / 10)
40#define CFQ_SLICE_SCALE (5)
41
42#define CFQ_KEY_ASYNC (0)
Jens Axboe3b181522005-06-27 10:56:24 +020043#define CFQ_KEY_ANY (0xffff)
Jens Axboe22e2c502005-06-27 10:55:12 +020044
45/*
46 * disable queueing at the driver/hardware level
47 */
Arjan van de Ven64100092006-01-06 09:46:02 +010048static const int cfq_max_depth = 2;
Jens Axboe22e2c502005-06-27 10:55:12 +020049
Al Viroa6a07632006-03-18 13:26:44 -050050static DEFINE_RWLOCK(cfq_exit_lock);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * for the hash of cfqq inside the cfqd
54 */
55#define CFQ_QHASH_SHIFT 6
56#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
57#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
58
59/*
60 * for the hash of crq inside the cfqq
61 */
62#define CFQ_MHASH_SHIFT 6
63#define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
64#define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
65#define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
66#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
67#define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
68
69#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
Jens Axboe22e2c502005-06-27 10:55:12 +020070#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define RQ_DATA(rq) (rq)->elevator_private
73
74/*
75 * rb-tree defines
76 */
77#define RB_NONE (2)
78#define RB_EMPTY(node) ((node)->rb_node == NULL)
79#define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
80#define RB_CLEAR(node) do { \
81 (node)->rb_parent = NULL; \
82 RB_CLEAR_COLOR((node)); \
83 (node)->rb_right = NULL; \
84 (node)->rb_left = NULL; \
85} while (0)
86#define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88#define rq_rb_key(rq) (rq)->sector
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static kmem_cache_t *crq_pool;
91static kmem_cache_t *cfq_pool;
92static kmem_cache_t *cfq_ioc_pool;
93
Al Viro334e94d2006-03-18 15:05:53 -050094static atomic_t ioc_count = ATOMIC_INIT(0);
95static struct completion *ioc_gone;
96
Jens Axboe22e2c502005-06-27 10:55:12 +020097#define CFQ_PRIO_LISTS IOPRIO_BE_NR
98#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
99#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
100#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
101
Jens Axboe3b181522005-06-27 10:56:24 +0200102#define ASYNC (0)
103#define SYNC (1)
104
105#define cfq_cfqq_dispatched(cfqq) \
106 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
107
108#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
109
110#define cfq_cfqq_sync(cfqq) \
111 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +0200112
113/*
114 * Per block device queue structure
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +0200117 atomic_t ref;
118 request_queue_t *queue;
119
120 /*
121 * rr list of queues with requests and the count of them
122 */
123 struct list_head rr_list[CFQ_PRIO_LISTS];
124 struct list_head busy_rr;
125 struct list_head cur_rr;
126 struct list_head idle_rr;
127 unsigned int busy_queues;
128
129 /*
130 * non-ordered list of empty cfqq's
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct list_head empty_list;
133
Jens Axboe22e2c502005-06-27 10:55:12 +0200134 /*
135 * cfqq lookup hash
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jens Axboe22e2c502005-06-27 10:55:12 +0200139 /*
140 * global crq hash for all queues
141 */
142 struct hlist_head *crq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 unsigned int max_queued;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mempool_t *crq_pool;
147
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 int rq_in_driver;
149
150 /*
151 * schedule slice state info
152 */
153 /*
154 * idle window management
155 */
156 struct timer_list idle_slice_timer;
157 struct work_struct unplug_work;
158
159 struct cfq_queue *active_queue;
160 struct cfq_io_context *active_cic;
161 int cur_prio, cur_end_prio;
162 unsigned int dispatch_slice;
163
164 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200167 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jens Axboe22e2c502005-06-27 10:55:12 +0200169 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * tunables, see top of file
173 */
174 unsigned int cfq_quantum;
175 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200176 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int cfq_back_penalty;
178 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200179 unsigned int cfq_slice[2];
180 unsigned int cfq_slice_async_rq;
181 unsigned int cfq_slice_idle;
182 unsigned int cfq_max_depth;
Al Virod9ff4182006-03-18 13:51:22 -0500183
184 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Jens Axboe22e2c502005-06-27 10:55:12 +0200187/*
188 * Per process-grouping structure
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190struct cfq_queue {
191 /* reference count */
192 atomic_t ref;
193 /* parent cfq_data */
194 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200195 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct hlist_node cfq_hash;
197 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200198 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* on either rr or empty list of cfqd */
200 struct list_head cfq_list;
201 /* sorted list of pending requests */
202 struct rb_root sort_list;
203 /* if fifo isn't expired, next request to serve */
204 struct cfq_rq *next_crq;
205 /* requests queued in sort_list */
206 int queued[2];
207 /* currently allocated requests */
208 int allocated[2];
209 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200210 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jens Axboe22e2c502005-06-27 10:55:12 +0200212 unsigned long slice_start;
213 unsigned long slice_end;
214 unsigned long slice_left;
215 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jens Axboe3b181522005-06-27 10:56:24 +0200217 /* number of requests that are on the dispatch list */
218 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200219
220 /* io prio of this group */
221 unsigned short ioprio, org_ioprio;
222 unsigned short ioprio_class, org_ioprio_class;
223
Jens Axboe3b181522005-06-27 10:56:24 +0200224 /* various state flags, see below */
225 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228struct cfq_rq {
229 struct rb_node rb_node;
230 sector_t rb_key;
231 struct request *request;
232 struct hlist_node hash;
233
234 struct cfq_queue *cfq_queue;
235 struct cfq_io_context *io_context;
236
Jens Axboe3b181522005-06-27 10:56:24 +0200237 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Jens Axboe3b181522005-06-27 10:56:24 +0200240enum cfqq_state_flags {
241 CFQ_CFQQ_FLAG_on_rr = 0,
242 CFQ_CFQQ_FLAG_wait_request,
243 CFQ_CFQQ_FLAG_must_alloc,
244 CFQ_CFQQ_FLAG_must_alloc_slice,
245 CFQ_CFQQ_FLAG_must_dispatch,
246 CFQ_CFQQ_FLAG_fifo_expire,
247 CFQ_CFQQ_FLAG_idle_window,
248 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200249};
250
251#define CFQ_CFQQ_FNS(name) \
252static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
253{ \
254 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
255} \
256static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
257{ \
258 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
259} \
260static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
261{ \
262 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
263}
264
265CFQ_CFQQ_FNS(on_rr);
266CFQ_CFQQ_FNS(wait_request);
267CFQ_CFQQ_FNS(must_alloc);
268CFQ_CFQQ_FNS(must_alloc_slice);
269CFQ_CFQQ_FNS(must_dispatch);
270CFQ_CFQQ_FNS(fifo_expire);
271CFQ_CFQQ_FNS(idle_window);
272CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200273#undef CFQ_CFQQ_FNS
274
275enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200276 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200277};
278
279#define CFQ_CRQ_FNS(name) \
280static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
281{ \
282 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
283} \
284static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
285{ \
286 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
287} \
288static inline int cfq_crq_##name(const struct cfq_rq *crq) \
289{ \
290 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
291}
292
Jens Axboe3b181522005-06-27 10:56:24 +0200293CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200294#undef CFQ_CRQ_FNS
295
296static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200297static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void cfq_put_cfqd(struct cfq_data *cfqd);
Al Viro6f325a12006-03-18 14:58:37 -0500299static 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 -0700300
Jens Axboe22e2c502005-06-27 10:55:12 +0200301#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/*
304 * lots of deadline iosched dupes, can be abstracted later...
305 */
306static inline void cfq_del_crq_hash(struct cfq_rq *crq)
307{
308 hlist_del_init(&crq->hash);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
312{
313 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
316}
317
318static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
319{
320 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
321 struct hlist_node *entry, *next;
322
323 hlist_for_each_safe(entry, next, hash_list) {
324 struct cfq_rq *crq = list_entry_hash(entry);
325 struct request *__rq = crq->request;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!rq_mergeable(__rq)) {
328 cfq_del_crq_hash(crq);
329 continue;
330 }
331
332 if (rq_hash_key(__rq) == offset)
333 return __rq;
334 }
335
336 return NULL;
337}
338
Andrew Morton99f95e52005-06-27 20:14:05 -0700339/*
340 * scheduler run of queue, if there are requests pending and no one in the
341 * driver that will restart queueing
342 */
343static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
344{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100345 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700346 kblockd_schedule_work(&cfqd->unplug_work);
347}
348
349static int cfq_queue_empty(request_queue_t *q)
350{
351 struct cfq_data *cfqd = q->elevator->elevator_data;
352
Jens Axboeb4878f22005-10-20 16:42:29 +0200353 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
357 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
358 * We choose the request that is closest to the head right now. Distance
359 * behind the head are penalized and only allowed to a certain extent.
360 */
361static struct cfq_rq *
362cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
363{
364 sector_t last, s1, s2, d1 = 0, d2 = 0;
365 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
366 unsigned long back_max;
367
368 if (crq1 == NULL || crq1 == crq2)
369 return crq2;
370 if (crq2 == NULL)
371 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200372
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200373 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
374 return crq1;
375 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200376 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 s1 = crq1->request->sector;
379 s2 = crq2->request->sector;
380
381 last = cfqd->last_sector;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * by definition, 1KiB is 2 sectors
385 */
386 back_max = cfqd->cfq_back_max * 2;
387
388 /*
389 * Strict one way elevator _except_ in the case where we allow
390 * short backward seeks which are biased as twice the cost of a
391 * similar forward seek.
392 */
393 if (s1 >= last)
394 d1 = s1 - last;
395 else if (s1 + back_max >= last)
396 d1 = (last - s1) * cfqd->cfq_back_penalty;
397 else
398 r1_wrap = 1;
399
400 if (s2 >= last)
401 d2 = s2 - last;
402 else if (s2 + back_max >= last)
403 d2 = (last - s2) * cfqd->cfq_back_penalty;
404 else
405 r2_wrap = 1;
406
407 /* Found required data */
408 if (!r1_wrap && r2_wrap)
409 return crq1;
410 else if (!r2_wrap && r1_wrap)
411 return crq2;
412 else if (r1_wrap && r2_wrap) {
413 /* both behind the head */
414 if (s1 <= s2)
415 return crq1;
416 else
417 return crq2;
418 }
419
420 /* Both requests in front of the head */
421 if (d1 < d2)
422 return crq1;
423 else if (d2 < d1)
424 return crq2;
425 else {
426 if (s1 >= s2)
427 return crq1;
428 else
429 return crq2;
430 }
431}
432
433/*
434 * would be nice to take fifo expire time into account as well
435 */
436static struct cfq_rq *
437cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
438 struct cfq_rq *last)
439{
440 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
441 struct rb_node *rbnext, *rbprev;
442
Jens Axboeb4878f22005-10-20 16:42:29 +0200443 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200445 if (rbnext == &last->rb_node)
446 rbnext = NULL;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 rbprev = rb_prev(&last->rb_node);
450
451 if (rbprev)
452 crq_prev = rb_entry_crq(rbprev);
453 if (rbnext)
454 crq_next = rb_entry_crq(rbnext);
455
456 return cfq_choose_req(cfqd, crq_next, crq_prev);
457}
458
459static void cfq_update_next_crq(struct cfq_rq *crq)
460{
461 struct cfq_queue *cfqq = crq->cfq_queue;
462
463 if (cfqq->next_crq == crq)
464 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
465}
466
Jens Axboe22e2c502005-06-27 10:55:12 +0200467static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jens Axboe22e2c502005-06-27 10:55:12 +0200469 struct cfq_data *cfqd = cfqq->cfqd;
470 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jens Axboe3b181522005-06-27 10:56:24 +0200472 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 list_del(&cfqq->cfq_list);
475
Jens Axboe22e2c502005-06-27 10:55:12 +0200476 if (cfq_class_rt(cfqq))
477 list = &cfqd->cur_rr;
478 else if (cfq_class_idle(cfqq))
479 list = &cfqd->idle_rr;
480 else {
481 /*
482 * if cfqq has requests in flight, don't allow it to be
483 * found in cfq_set_active_queue before it has finished them.
484 * this is done to increase fairness between a process that
485 * has lots of io pending vs one that only generates one
486 * sporadically or synchronously
487 */
Jens Axboe3b181522005-06-27 10:56:24 +0200488 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200489 list = &cfqd->busy_rr;
490 else
491 list = &cfqd->rr_list[cfqq->ioprio];
492 }
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200495 * if queue was preempted, just add to front to be fair. busy_rr
496 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200498 if (preempted || list == &cfqd->busy_rr) {
499 list_add(&cfqq->cfq_list, list);
500 return;
501 }
502
503 /*
504 * sort by when queue was last serviced
505 */
506 entry = list;
507 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
509
Jens Axboe22e2c502005-06-27 10:55:12 +0200510 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200512 if (time_before(__cfqq->service_last, cfqq->service_last))
513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 list_add(&cfqq->cfq_list, entry);
517}
518
519/*
520 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200521 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200524cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Jens Axboe3b181522005-06-27 10:56:24 +0200526 BUG_ON(cfq_cfqq_on_rr(cfqq));
527 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 cfqd->busy_queues++;
529
Jens Axboeb4878f22005-10-20 16:42:29 +0200530 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533static inline void
534cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
535{
Jens Axboe3b181522005-06-27 10:56:24 +0200536 BUG_ON(!cfq_cfqq_on_rr(cfqq));
537 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200538 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 BUG_ON(!cfqd->busy_queues);
541 cfqd->busy_queues--;
542}
543
544/*
545 * rb tree support functions
546 */
547static inline void cfq_del_crq_rb(struct cfq_rq *crq)
548{
549 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200550 struct cfq_data *cfqd = cfqq->cfqd;
551 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jens Axboeb4878f22005-10-20 16:42:29 +0200553 BUG_ON(!cfqq->queued[sync]);
554 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jens Axboeb4878f22005-10-20 16:42:29 +0200556 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jens Axboeb4878f22005-10-20 16:42:29 +0200558 rb_erase(&crq->rb_node, &cfqq->sort_list);
559 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jens Axboeb4878f22005-10-20 16:42:29 +0200561 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
562 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static struct cfq_rq *
566__cfq_add_crq_rb(struct cfq_rq *crq)
567{
568 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
569 struct rb_node *parent = NULL;
570 struct cfq_rq *__crq;
571
572 while (*p) {
573 parent = *p;
574 __crq = rb_entry_crq(parent);
575
576 if (crq->rb_key < __crq->rb_key)
577 p = &(*p)->rb_left;
578 else if (crq->rb_key > __crq->rb_key)
579 p = &(*p)->rb_right;
580 else
581 return __crq;
582 }
583
584 rb_link_node(&crq->rb_node, parent, p);
585 return NULL;
586}
587
588static void cfq_add_crq_rb(struct cfq_rq *crq)
589{
590 struct cfq_queue *cfqq = crq->cfq_queue;
591 struct cfq_data *cfqd = cfqq->cfqd;
592 struct request *rq = crq->request;
593 struct cfq_rq *__alias;
594
595 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200596 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /*
599 * looks a little odd, but the first insert might return an alias.
600 * if that happens, put the alias on the dispatch list
601 */
602 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200603 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
606
Jens Axboe3b181522005-06-27 10:56:24 +0200607 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200608 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
611 * check if this request is a better next-serve candidate
612 */
613 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
614}
615
616static inline void
617cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
618{
Jens Axboeb4878f22005-10-20 16:42:29 +0200619 rb_erase(&crq->rb_node, &cfqq->sort_list);
620 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 cfq_add_crq_rb(crq);
623}
624
Jens Axboe22e2c502005-06-27 10:55:12 +0200625static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Jens Axboe3b181522005-06-27 10:56:24 +0200628 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct rb_node *n;
630
631 if (!cfqq)
632 goto out;
633
634 n = cfqq->sort_list.rb_node;
635 while (n) {
636 struct cfq_rq *crq = rb_entry_crq(n);
637
638 if (sector < crq->rb_key)
639 n = n->rb_left;
640 else if (sector > crq->rb_key)
641 n = n->rb_right;
642 else
643 return crq->request;
644 }
645
646out:
647 return NULL;
648}
649
Jens Axboeb4878f22005-10-20 16:42:29 +0200650static void cfq_activate_request(request_queue_t *q, struct request *rq)
651{
652 struct cfq_data *cfqd = q->elevator->elevator_data;
653
654 cfqd->rq_in_driver++;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
658{
Jens Axboe22e2c502005-06-27 10:55:12 +0200659 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jens Axboeb4878f22005-10-20 16:42:29 +0200661 WARN_ON(!cfqd->rq_in_driver);
662 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Jens Axboeb4878f22005-10-20 16:42:29 +0200665static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct cfq_rq *crq = RQ_DATA(rq);
668
Jens Axboeb4878f22005-10-20 16:42:29 +0200669 list_del_init(&rq->queuelist);
670 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200671 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674static int
675cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
676{
677 struct cfq_data *cfqd = q->elevator->elevator_data;
678 struct request *__rq;
679 int ret;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200682 if (__rq && elv_rq_merge_ok(__rq, bio)) {
683 ret = ELEVATOR_BACK_MERGE;
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
Jens Axboe22e2c502005-06-27 10:55:12 +0200688 if (__rq && elv_rq_merge_ok(__rq, bio)) {
689 ret = ELEVATOR_FRONT_MERGE;
690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 return ELEVATOR_NO_MERGE;
694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *req = __rq;
696 return ret;
697}
698
699static void cfq_merged_request(request_queue_t *q, struct request *req)
700{
701 struct cfq_data *cfqd = q->elevator->elevator_data;
702 struct cfq_rq *crq = RQ_DATA(req);
703
704 cfq_del_crq_hash(crq);
705 cfq_add_crq_hash(cfqd, crq);
706
Jens Axboeb4878f22005-10-20 16:42:29 +0200707 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct cfq_queue *cfqq = crq->cfq_queue;
709
710 cfq_update_next_crq(crq);
711 cfq_reposition_crq_rb(cfqq, crq);
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static void
716cfq_merged_requests(request_queue_t *q, struct request *rq,
717 struct request *next)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cfq_merged_request(q, rq);
720
Jens Axboe22e2c502005-06-27 10:55:12 +0200721 /*
722 * reposition in fifo if next is older than rq
723 */
724 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
725 time_before(next->start_time, rq->start_time))
726 list_move(&rq->queuelist, &next->queuelist);
727
Jens Axboeb4878f22005-10-20 16:42:29 +0200728 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200729}
730
731static inline void
732__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
733{
734 if (cfqq) {
735 /*
736 * stop potential idle class queues waiting service
737 */
738 del_timer(&cfqd->idle_class_timer);
739
740 cfqq->slice_start = jiffies;
741 cfqq->slice_end = 0;
742 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200743 cfq_clear_cfqq_must_alloc_slice(cfqq);
744 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200745 }
746
747 cfqd->active_queue = cfqq;
748}
749
750/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100751 * current cfqq expired its slice (or was too idle), select new one
752 */
753static void
754__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
755 int preempted)
756{
757 unsigned long now = jiffies;
758
759 if (cfq_cfqq_wait_request(cfqq))
760 del_timer(&cfqd->idle_slice_timer);
761
762 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
763 cfqq->service_last = now;
764 cfq_schedule_dispatch(cfqd);
765 }
766
767 cfq_clear_cfqq_must_dispatch(cfqq);
768 cfq_clear_cfqq_wait_request(cfqq);
769
770 /*
771 * store what was left of this slice, if the queue idled out
772 * or was preempted
773 */
774 if (time_after(cfqq->slice_end, now))
775 cfqq->slice_left = cfqq->slice_end - now;
776 else
777 cfqq->slice_left = 0;
778
779 if (cfq_cfqq_on_rr(cfqq))
780 cfq_resort_rr_list(cfqq, preempted);
781
782 if (cfqq == cfqd->active_queue)
783 cfqd->active_queue = NULL;
784
785 if (cfqd->active_cic) {
786 put_io_context(cfqd->active_cic->ioc);
787 cfqd->active_cic = NULL;
788 }
789
790 cfqd->dispatch_slice = 0;
791}
792
793static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
794{
795 struct cfq_queue *cfqq = cfqd->active_queue;
796
797 if (cfqq)
798 __cfq_slice_expired(cfqd, cfqq, preempted);
799}
800
801/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200802 * 0
803 * 0,1
804 * 0,1,2
805 * 0,1,2,3
806 * 0,1,2,3,4
807 * 0,1,2,3,4,5
808 * 0,1,2,3,4,5,6
809 * 0,1,2,3,4,5,6,7
810 */
811static int cfq_get_next_prio_level(struct cfq_data *cfqd)
812{
813 int prio, wrap;
814
815 prio = -1;
816 wrap = 0;
817 do {
818 int p;
819
820 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
821 if (!list_empty(&cfqd->rr_list[p])) {
822 prio = p;
823 break;
824 }
825 }
826
827 if (prio != -1)
828 break;
829 cfqd->cur_prio = 0;
830 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
831 cfqd->cur_end_prio = 0;
832 if (wrap)
833 break;
834 wrap = 1;
835 }
836 } while (1);
837
838 if (unlikely(prio == -1))
839 return -1;
840
841 BUG_ON(prio >= CFQ_PRIO_LISTS);
842
843 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
844
845 cfqd->cur_prio = prio + 1;
846 if (cfqd->cur_prio > cfqd->cur_end_prio) {
847 cfqd->cur_end_prio = cfqd->cur_prio;
848 cfqd->cur_prio = 0;
849 }
850 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
851 cfqd->cur_prio = 0;
852 cfqd->cur_end_prio = 0;
853 }
854
855 return prio;
856}
857
Jens Axboe3b181522005-06-27 10:56:24 +0200858static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200859{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100860 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200861
862 /*
863 * if current list is non-empty, grab first entry. if it is empty,
864 * get next prio level and grab first entry then if any are spliced
865 */
866 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
867 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
868
869 /*
870 * if we have idle queues and no rt or be queues had pending
871 * requests, either allow immediate service if the grace period
872 * has passed or arm the idle grace timer
873 */
874 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
875 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
876
877 if (time_after_eq(jiffies, end))
878 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
879 else
880 mod_timer(&cfqd->idle_class_timer, end);
881 }
882
883 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200884 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200885}
886
Jens Axboe22e2c502005-06-27 10:55:12 +0200887static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
888
889{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100890 unsigned long sl;
891
Jens Axboe22e2c502005-06-27 10:55:12 +0200892 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
893 WARN_ON(cfqq != cfqd->active_queue);
894
895 /*
896 * idle is disabled, either manually or by past process history
897 */
898 if (!cfqd->cfq_slice_idle)
899 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200900 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200901 return 0;
902 /*
903 * task has exited, don't wait
904 */
905 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
906 return 0;
907
Jens Axboe3b181522005-06-27 10:56:24 +0200908 cfq_mark_cfqq_must_dispatch(cfqq);
909 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200910
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100911 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
912 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200913 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Jens Axboeb4878f22005-10-20 16:42:29 +0200916static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct cfq_data *cfqd = q->elevator->elevator_data;
919 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200920
921 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200922 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200923 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200924 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/*
928 * return expired entry, or NULL to just start from scratch in rbtree
929 */
930static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
931{
932 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200933 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct cfq_rq *crq;
935
Jens Axboe3b181522005-06-27 10:56:24 +0200936 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return NULL;
938
Jens Axboe22e2c502005-06-27 10:55:12 +0200939 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200940 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jens Axboe22e2c502005-06-27 10:55:12 +0200942 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
943 rq = crq->request;
944 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200945 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200946 return crq;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 return NULL;
951}
952
953/*
Jens Axboe3b181522005-06-27 10:56:24 +0200954 * Scale schedule slice based on io priority. Use the sync time slice only
955 * if a queue is marked sync and has sync io queued. A sync queue with async
956 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200958static inline int
959cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jens Axboe22e2c502005-06-27 10:55:12 +0200961 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jens Axboe22e2c502005-06-27 10:55:12 +0200963 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jens Axboe22e2c502005-06-27 10:55:12 +0200965 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Jens Axboe22e2c502005-06-27 10:55:12 +0200968static inline void
969cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
970{
971 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
972}
973
974static inline int
975cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
976{
977 const int base_rq = cfqd->cfq_slice_async_rq;
978
979 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
980
981 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
982}
983
984/*
985 * get next queue for service
986 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100987static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200988{
989 unsigned long now = jiffies;
990 struct cfq_queue *cfqq;
991
992 cfqq = cfqd->active_queue;
993 if (!cfqq)
994 goto new_queue;
995
996 /*
997 * slice has expired
998 */
Jens Axboe3b181522005-06-27 10:56:24 +0200999 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1000 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001001
1002 /*
1003 * if queue has requests, dispatch one. if not, check if
1004 * enough slice is left to wait for one
1005 */
1006 if (!RB_EMPTY(&cfqq->sort_list))
1007 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001008 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001009 time_before(now, cfqq->slice_end)) {
1010 if (cfq_arm_slice_timer(cfqd, cfqq))
1011 return NULL;
1012 }
1013
Jens Axboe3b181522005-06-27 10:56:24 +02001014expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001015 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001016new_queue:
1017 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001018keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001019 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001020}
1021
1022static int
1023__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1024 int max_dispatch)
1025{
1026 int dispatched = 0;
1027
1028 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1029
1030 do {
1031 struct cfq_rq *crq;
1032
1033 /*
1034 * follow expired path, else get first next available
1035 */
1036 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1037 crq = cfqq->next_crq;
1038
1039 /*
1040 * finally, insert request into driver dispatch list
1041 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001042 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001043
1044 cfqd->dispatch_slice++;
1045 dispatched++;
1046
1047 if (!cfqd->active_cic) {
1048 atomic_inc(&crq->io_context->ioc->refcount);
1049 cfqd->active_cic = crq->io_context;
1050 }
1051
1052 if (RB_EMPTY(&cfqq->sort_list))
1053 break;
1054
1055 } while (dispatched < max_dispatch);
1056
1057 /*
1058 * if slice end isn't set yet, set it. if at least one request was
1059 * sync, use the sync time slice value
1060 */
1061 if (!cfqq->slice_end)
1062 cfq_set_prio_slice(cfqd, cfqq);
1063
1064 /*
1065 * expire an async queue immediately if it has used up its slice. idle
1066 * queue always expire after 1 dispatch round.
1067 */
1068 if ((!cfq_cfqq_sync(cfqq) &&
1069 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1070 cfq_class_idle(cfqq))
1071 cfq_slice_expired(cfqd, 0);
1072
1073 return dispatched;
1074}
1075
1076static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001077cfq_forced_dispatch_cfqqs(struct list_head *list)
1078{
1079 int dispatched = 0;
1080 struct cfq_queue *cfqq, *next;
1081 struct cfq_rq *crq;
1082
1083 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1084 while ((crq = cfqq->next_crq)) {
1085 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1086 dispatched++;
1087 }
1088 BUG_ON(!list_empty(&cfqq->fifo));
1089 }
1090 return dispatched;
1091}
1092
1093static int
1094cfq_forced_dispatch(struct cfq_data *cfqd)
1095{
1096 int i, dispatched = 0;
1097
1098 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1099 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1100
1101 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1102 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1103 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1104
1105 cfq_slice_expired(cfqd, 0);
1106
1107 BUG_ON(cfqd->busy_queues);
1108
1109 return dispatched;
1110}
1111
1112static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001113cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct cfq_data *cfqd = q->elevator->elevator_data;
1116 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Jens Axboe22e2c502005-06-27 10:55:12 +02001118 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001121 if (unlikely(force))
1122 return cfq_forced_dispatch(cfqd);
1123
1124 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001125 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001126 int max_dispatch;
1127
1128 /*
1129 * if idle window is disabled, allow queue buildup
1130 */
1131 if (!cfq_cfqq_idle_window(cfqq) &&
1132 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1133 return 0;
1134
Jens Axboe3b181522005-06-27 10:56:24 +02001135 cfq_clear_cfqq_must_dispatch(cfqq);
1136 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001137 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001139 max_dispatch = cfqd->cfq_quantum;
1140 if (cfq_class_idle(cfqq))
1141 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jens Axboe22e2c502005-06-27 10:55:12 +02001143 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
Jens Axboe22e2c502005-06-27 10:55:12 +02001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/*
1150 * task holds one reference to the queue, dropped when task exits. each crq
1151 * in-flight on this queue also holds a reference, dropped when crq is freed.
1152 *
1153 * queue lock must be held here.
1154 */
1155static void cfq_put_queue(struct cfq_queue *cfqq)
1156{
Jens Axboe22e2c502005-06-27 10:55:12 +02001157 struct cfq_data *cfqd = cfqq->cfqd;
1158
1159 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (!atomic_dec_and_test(&cfqq->ref))
1162 return;
1163
1164 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001165 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001166 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001168 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001169 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 cfq_put_cfqd(cfqq->cfqd);
1172
1173 /*
1174 * it's on the empty list and still hashed
1175 */
1176 list_del(&cfqq->cfq_list);
1177 hlist_del(&cfqq->cfq_hash);
1178 kmem_cache_free(cfq_pool, cfqq);
1179}
1180
1181static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001182__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1183 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1186 struct hlist_node *entry, *next;
1187
1188 hlist_for_each_safe(entry, next, hash_list) {
1189 struct cfq_queue *__cfqq = list_entry_qhash(entry);
Al Virob0a69162006-03-14 15:32:50 -05001190 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jens Axboe3b181522005-06-27 10:56:24 +02001192 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return __cfqq;
1194 }
1195
1196 return NULL;
1197}
1198
1199static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001200cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Jens Axboe3b181522005-06-27 10:56:24 +02001202 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static void cfq_free_io_context(struct cfq_io_context *cic)
1206{
Jens Axboe22e2c502005-06-27 10:55:12 +02001207 struct cfq_io_context *__cic;
1208 struct list_head *entry, *next;
Al Viro334e94d2006-03-18 15:05:53 -05001209 int freed = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001210
1211 list_for_each_safe(entry, next, &cic->list) {
1212 __cic = list_entry(entry, struct cfq_io_context, list);
1213 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001214 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001215 }
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kmem_cache_free(cfq_ioc_pool, cic);
Al Viro334e94d2006-03-18 15:05:53 -05001218 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1219 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Al Viroe17a9482006-03-18 13:21:20 -05001222static void cfq_trim(struct io_context *ioc)
1223{
1224 ioc->set_ioprio = NULL;
1225 if (ioc->cic)
1226 cfq_free_io_context(ioc->cic);
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001230 * Called with interrupts disabled
1231 */
1232static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1233{
Al Viro478a82b2006-03-18 13:25:24 -05001234 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001235 request_queue_t *q;
1236
1237 if (!cfqd)
1238 return;
1239
1240 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001241
1242 WARN_ON(!irqs_disabled());
1243
1244 spin_lock(q->queue_lock);
1245
Al Viro12a05732006-03-18 13:38:01 -05001246 if (cic->cfqq[ASYNC]) {
1247 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1248 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1249 cfq_put_queue(cic->cfqq[ASYNC]);
1250 cic->cfqq[ASYNC] = NULL;
1251 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001252
Al Viro12a05732006-03-18 13:38:01 -05001253 if (cic->cfqq[SYNC]) {
1254 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1255 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1256 cfq_put_queue(cic->cfqq[SYNC]);
1257 cic->cfqq[SYNC] = NULL;
1258 }
1259
Al Viro478a82b2006-03-18 13:25:24 -05001260 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001261 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001262 spin_unlock(q->queue_lock);
1263}
1264
1265/*
1266 * Another task may update the task cic list, if it is doing a queue lookup
1267 * on its behalf. cfq_cic_lock excludes such concurrent updates
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
1269static void cfq_exit_io_context(struct cfq_io_context *cic)
1270{
Jens Axboe22e2c502005-06-27 10:55:12 +02001271 struct cfq_io_context *__cic;
1272 struct list_head *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned long flags;
1274
Jens Axboe22e2c502005-06-27 10:55:12 +02001275 local_irq_save(flags);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * put the reference this task is holding to the various queues
1279 */
Al Virod9ff4182006-03-18 13:51:22 -05001280 read_lock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001281 list_for_each(entry, &cic->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 __cic = list_entry(entry, struct cfq_io_context, list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001283 cfq_exit_single_io_context(__cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
Jens Axboe22e2c502005-06-27 10:55:12 +02001286 cfq_exit_single_io_context(cic);
Al Virod9ff4182006-03-18 13:51:22 -05001287 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001288 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Jens Axboe22e2c502005-06-27 10:55:12 +02001291static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001292cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Jens Axboe22e2c502005-06-27 10:55:12 +02001294 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (cic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 INIT_LIST_HEAD(&cic->list);
Al Viro12a05732006-03-18 13:38:01 -05001298 cic->cfqq[ASYNC] = NULL;
1299 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001300 cic->key = NULL;
1301 cic->last_end_request = jiffies;
1302 cic->ttime_total = 0;
1303 cic->ttime_samples = 0;
1304 cic->ttime_mean = 0;
1305 cic->dtor = cfq_free_io_context;
1306 cic->exit = cfq_exit_io_context;
Al Virod9ff4182006-03-18 13:51:22 -05001307 INIT_LIST_HEAD(&cic->queue_list);
Al Viro334e94d2006-03-18 15:05:53 -05001308 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
1311 return cic;
1312}
1313
Jens Axboe22e2c502005-06-27 10:55:12 +02001314static void cfq_init_prio_data(struct cfq_queue *cfqq)
1315{
1316 struct task_struct *tsk = current;
1317 int ioprio_class;
1318
Jens Axboe3b181522005-06-27 10:56:24 +02001319 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001320 return;
1321
1322 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1323 switch (ioprio_class) {
1324 default:
1325 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1326 case IOPRIO_CLASS_NONE:
1327 /*
1328 * no prio set, place us in the middle of the BE classes
1329 */
1330 cfqq->ioprio = task_nice_ioprio(tsk);
1331 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1332 break;
1333 case IOPRIO_CLASS_RT:
1334 cfqq->ioprio = task_ioprio(tsk);
1335 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1336 break;
1337 case IOPRIO_CLASS_BE:
1338 cfqq->ioprio = task_ioprio(tsk);
1339 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1340 break;
1341 case IOPRIO_CLASS_IDLE:
1342 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1343 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001344 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001345 break;
1346 }
1347
1348 /*
1349 * keep track of original prio settings in case we have to temporarily
1350 * elevate the priority of this queue
1351 */
1352 cfqq->org_ioprio = cfqq->ioprio;
1353 cfqq->org_ioprio_class = cfqq->ioprio_class;
1354
Jens Axboe3b181522005-06-27 10:56:24 +02001355 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001356 cfq_resort_rr_list(cfqq, 0);
1357
Jens Axboe3b181522005-06-27 10:56:24 +02001358 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001359}
1360
Al Viro478a82b2006-03-18 13:25:24 -05001361static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001362{
Al Viro478a82b2006-03-18 13:25:24 -05001363 struct cfq_data *cfqd = cic->key;
1364 struct cfq_queue *cfqq;
1365 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001366 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001367 cfqq = cic->cfqq[ASYNC];
1368 if (cfqq) {
Al Viro6f325a12006-03-18 14:58:37 -05001369 struct cfq_queue *new_cfqq;
1370 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
1371 cic->ioc->task, GFP_ATOMIC);
1372 if (new_cfqq) {
1373 cic->cfqq[ASYNC] = new_cfqq;
1374 cfq_put_queue(cfqq);
1375 }
Al Viro12a05732006-03-18 13:38:01 -05001376 }
1377 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001378 if (cfqq) {
1379 cfq_mark_cfqq_prio_changed(cfqq);
1380 cfq_init_prio_data(cfqq);
1381 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001382 spin_unlock(cfqd->queue->queue_lock);
1383 }
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001387 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001389static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Al Viroa6a07632006-03-18 13:26:44 -05001391 struct cfq_io_context *cic;
1392
1393 write_lock(&cfq_exit_lock);
1394
1395 cic = ioc->cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Al Viro478a82b2006-03-18 13:25:24 -05001397 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Jens Axboe22e2c502005-06-27 10:55:12 +02001399 list_for_each_entry(cic, &cic->list, list)
Al Viro478a82b2006-03-18 13:25:24 -05001400 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Al Viroa6a07632006-03-18 13:26:44 -05001402 write_unlock(&cfq_exit_lock);
1403
Jens Axboe22e2c502005-06-27 10:55:12 +02001404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
1407static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001408cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001409 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1412 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001413 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415retry:
Al Viro6f325a12006-03-18 14:58:37 -05001416 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001417 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (!cfqq) {
1420 if (new_cfqq) {
1421 cfqq = new_cfqq;
1422 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001423 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_unlock_irq(cfqd->queue->queue_lock);
1425 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1426 spin_lock_irq(cfqd->queue->queue_lock);
1427 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001428 } else {
1429 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1430 if (!cfqq)
1431 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 memset(cfqq, 0, sizeof(*cfqq));
1435
1436 INIT_HLIST_NODE(&cfqq->cfq_hash);
1437 INIT_LIST_HEAD(&cfqq->cfq_list);
1438 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001439 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 cfqq->key = key;
1442 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1443 atomic_set(&cfqq->ref, 0);
1444 cfqq->cfqd = cfqd;
1445 atomic_inc(&cfqd->ref);
Jens Axboe22e2c502005-06-27 10:55:12 +02001446 cfqq->service_last = 0;
1447 /*
1448 * set ->slice_left to allow preemption for a new process
1449 */
1450 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001451 cfq_mark_cfqq_idle_window(cfqq);
1452 cfq_mark_cfqq_prio_changed(cfqq);
1453 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
1456 if (new_cfqq)
1457 kmem_cache_free(cfq_pool, new_cfqq);
1458
1459 atomic_inc(&cfqq->ref);
1460out:
1461 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1462 return cfqq;
1463}
1464
Jens Axboe22e2c502005-06-27 10:55:12 +02001465/*
1466 * Setup general io context and cfq io context. There can be several cfq
1467 * io contexts per general io context, if this process is doing io to more
1468 * than one device managed by cfq. Note that caller is holding a reference to
1469 * cfqq, so we don't need to worry about it disappearing
1470 */
1471static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001472cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Jens Axboe22e2c502005-06-27 10:55:12 +02001474 struct io_context *ioc = NULL;
1475 struct cfq_io_context *cic;
1476
1477 might_sleep_if(gfp_mask & __GFP_WAIT);
1478
1479 ioc = get_io_context(gfp_mask);
1480 if (!ioc)
1481 return NULL;
1482
Al Virod9ff4182006-03-18 13:51:22 -05001483restart:
Jens Axboe22e2c502005-06-27 10:55:12 +02001484 if ((cic = ioc->cic) == NULL) {
1485 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1486
1487 if (cic == NULL)
1488 goto err;
1489
1490 /*
1491 * manually increment generic io_context usage count, it
1492 * cannot go away since we are already holding one ref to it
1493 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001494 cic->ioc = ioc;
1495 cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001496 read_lock(&cfq_exit_lock);
Al Viro478a82b2006-03-18 13:25:24 -05001497 ioc->set_ioprio = cfq_ioc_set_ioprio;
1498 ioc->cic = cic;
Al Virod9ff4182006-03-18 13:51:22 -05001499 list_add(&cic->queue_list, &cfqd->cic_list);
Al Viroa6a07632006-03-18 13:26:44 -05001500 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001501 } else {
1502 struct cfq_io_context *__cic;
1503
1504 /*
1505 * the first cic on the list is actually the head itself
1506 */
1507 if (cic->key == cfqd)
1508 goto out;
1509
Al Virod9ff4182006-03-18 13:51:22 -05001510 if (unlikely(!cic->key)) {
1511 read_lock(&cfq_exit_lock);
1512 if (list_empty(&cic->list))
1513 ioc->cic = NULL;
1514 else
1515 ioc->cic = list_entry(cic->list.next,
1516 struct cfq_io_context,
1517 list);
1518 read_unlock(&cfq_exit_lock);
1519 kmem_cache_free(cfq_ioc_pool, cic);
Al Viro334e94d2006-03-18 15:05:53 -05001520 atomic_dec(&ioc_count);
Al Virod9ff4182006-03-18 13:51:22 -05001521 goto restart;
1522 }
1523
Jens Axboe22e2c502005-06-27 10:55:12 +02001524 /*
1525 * cic exists, check if we already are there. linear search
1526 * should be ok here, the list will usually not be more than
1527 * 1 or a few entries long
1528 */
1529 list_for_each_entry(__cic, &cic->list, list) {
1530 /*
1531 * this process is already holding a reference to
1532 * this queue, so no need to get one more
1533 */
1534 if (__cic->key == cfqd) {
1535 cic = __cic;
1536 goto out;
1537 }
Al Virod9ff4182006-03-18 13:51:22 -05001538 if (unlikely(!__cic->key)) {
1539 read_lock(&cfq_exit_lock);
1540 list_del(&__cic->list);
1541 read_unlock(&cfq_exit_lock);
1542 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001543 atomic_dec(&ioc_count);
Al Virod9ff4182006-03-18 13:51:22 -05001544 goto restart;
1545 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001546 }
1547
1548 /*
1549 * nope, process doesn't have a cic assoicated with this
1550 * cfqq yet. get a new one and add to list
1551 */
1552 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1553 if (__cic == NULL)
1554 goto err;
1555
1556 __cic->ioc = ioc;
1557 __cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001558 read_lock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001559 list_add(&__cic->list, &cic->list);
Al Virod9ff4182006-03-18 13:51:22 -05001560 list_add(&__cic->queue_list, &cfqd->cic_list);
Al Viroa6a07632006-03-18 13:26:44 -05001561 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001562 cic = __cic;
1563 }
1564
1565out:
1566 return cic;
1567err:
1568 put_io_context(ioc);
1569 return NULL;
1570}
1571
1572static void
1573cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1574{
1575 unsigned long elapsed, ttime;
1576
1577 /*
1578 * if this context already has stuff queued, thinktime is from
1579 * last queue not last end
1580 */
1581#if 0
1582 if (time_after(cic->last_end_request, cic->last_queue))
1583 elapsed = jiffies - cic->last_end_request;
1584 else
1585 elapsed = jiffies - cic->last_queue;
1586#else
1587 elapsed = jiffies - cic->last_end_request;
1588#endif
1589
1590 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1591
1592 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1593 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1594 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1595}
1596
1597#define sample_valid(samples) ((samples) > 80)
1598
1599/*
1600 * Disable idle window if the process thinks too long or seeks so much that
1601 * it doesn't matter
1602 */
1603static void
1604cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1605 struct cfq_io_context *cic)
1606{
Jens Axboe3b181522005-06-27 10:56:24 +02001607 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001608
1609 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1610 enable_idle = 0;
1611 else if (sample_valid(cic->ttime_samples)) {
1612 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1613 enable_idle = 0;
1614 else
1615 enable_idle = 1;
1616 }
1617
Jens Axboe3b181522005-06-27 10:56:24 +02001618 if (enable_idle)
1619 cfq_mark_cfqq_idle_window(cfqq);
1620 else
1621 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001622}
1623
1624
1625/*
1626 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1627 * no or if we aren't sure, a 1 will cause a preempt.
1628 */
1629static int
1630cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1631 struct cfq_rq *crq)
1632{
1633 struct cfq_queue *cfqq = cfqd->active_queue;
1634
1635 if (cfq_class_idle(new_cfqq))
1636 return 0;
1637
1638 if (!cfqq)
1639 return 1;
1640
1641 if (cfq_class_idle(cfqq))
1642 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001643 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001644 return 0;
1645 /*
1646 * if it doesn't have slice left, forget it
1647 */
1648 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1649 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001650 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001651 return 1;
1652
1653 return 0;
1654}
1655
1656/*
1657 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1658 * let it have half of its nominal slice.
1659 */
1660static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1661{
1662 struct cfq_queue *__cfqq, *next;
1663
1664 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1665 cfq_resort_rr_list(__cfqq, 1);
1666
1667 if (!cfqq->slice_left)
1668 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1669
1670 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001671 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001672 __cfq_set_active_queue(cfqd, cfqq);
1673}
1674
1675/*
1676 * should really be a ll_rw_blk.c helper
1677 */
1678static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1679{
1680 request_queue_t *q = cfqd->queue;
1681
1682 if (!blk_queue_plugged(q))
1683 q->request_fn(q);
1684 else
1685 __generic_unplug_device(q);
1686}
1687
1688/*
1689 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1690 * something we should do about it
1691 */
1692static void
1693cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1694 struct cfq_rq *crq)
1695{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001696 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001697
1698 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1699
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001700 /*
1701 * we never wait for an async request and we don't allow preemption
1702 * of an async request. so just return early
1703 */
1704 if (!cfq_crq_is_sync(crq))
1705 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001706
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001707 cic = crq->io_context;
Jens Axboe22e2c502005-06-27 10:55:12 +02001708
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001709 cfq_update_io_thinktime(cfqd, cic);
1710 cfq_update_idle_window(cfqd, cfqq, cic);
1711
1712 cic->last_queue = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001713
1714 if (cfqq == cfqd->active_queue) {
1715 /*
1716 * if we are waiting for a request for this queue, let it rip
1717 * immediately and flag that we must not expire this queue
1718 * just now
1719 */
Jens Axboe3b181522005-06-27 10:56:24 +02001720 if (cfq_cfqq_wait_request(cfqq)) {
1721 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001722 del_timer(&cfqd->idle_slice_timer);
1723 cfq_start_queueing(cfqd, cfqq);
1724 }
1725 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1726 /*
1727 * not the active queue - expire current slice if it is
1728 * idle and has expired it's mean thinktime or this new queue
1729 * has some old slice time left and is of higher priority
1730 */
1731 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001732 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001733 cfq_start_queueing(cfqd, cfqq);
1734 }
1735}
1736
Jens Axboeb4878f22005-10-20 16:42:29 +02001737static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001738{
Jens Axboeb4878f22005-10-20 16:42:29 +02001739 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001740 struct cfq_rq *crq = RQ_DATA(rq);
1741 struct cfq_queue *cfqq = crq->cfq_queue;
1742
1743 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jens Axboe22e2c502005-06-27 10:55:12 +02001747 list_add_tail(&rq->queuelist, &cfqq->fifo);
1748
Tejun Heo98b11472005-10-20 16:46:54 +02001749 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001750 cfq_add_crq_hash(cfqd, crq);
1751
Jens Axboe22e2c502005-06-27 10:55:12 +02001752 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755static void cfq_completed_request(request_queue_t *q, struct request *rq)
1756{
1757 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001758 struct cfq_queue *cfqq = crq->cfq_queue;
1759 struct cfq_data *cfqd = cfqq->cfqd;
1760 const int sync = cfq_crq_is_sync(crq);
1761 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jens Axboeb4878f22005-10-20 16:42:29 +02001763 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Jens Axboeb4878f22005-10-20 16:42:29 +02001765 WARN_ON(!cfqd->rq_in_driver);
1766 WARN_ON(!cfqq->on_dispatch[sync]);
1767 cfqd->rq_in_driver--;
1768 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jens Axboeb4878f22005-10-20 16:42:29 +02001770 if (!cfq_class_idle(cfqq))
1771 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001772
Jens Axboeb4878f22005-10-20 16:42:29 +02001773 if (!cfq_cfqq_dispatched(cfqq)) {
1774 if (cfq_cfqq_on_rr(cfqq)) {
1775 cfqq->service_last = now;
1776 cfq_resort_rr_list(cfqq, 0);
1777 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001778 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
Jens Axboeb4878f22005-10-20 16:42:29 +02001781 if (cfq_crq_is_sync(crq))
1782 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785static struct request *
1786cfq_former_request(request_queue_t *q, struct request *rq)
1787{
1788 struct cfq_rq *crq = RQ_DATA(rq);
1789 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1790
1791 if (rbprev)
1792 return rb_entry_crq(rbprev)->request;
1793
1794 return NULL;
1795}
1796
1797static struct request *
1798cfq_latter_request(request_queue_t *q, struct request *rq)
1799{
1800 struct cfq_rq *crq = RQ_DATA(rq);
1801 struct rb_node *rbnext = rb_next(&crq->rb_node);
1802
1803 if (rbnext)
1804 return rb_entry_crq(rbnext)->request;
1805
1806 return NULL;
1807}
1808
Jens Axboe22e2c502005-06-27 10:55:12 +02001809/*
1810 * we temporarily boost lower priority queues if they are holding fs exclusive
1811 * resources. they are boosted to normal prio (CLASS_BE/4)
1812 */
1813static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Jens Axboe22e2c502005-06-27 10:55:12 +02001815 const int ioprio_class = cfqq->ioprio_class;
1816 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jens Axboe22e2c502005-06-27 10:55:12 +02001818 if (has_fs_excl()) {
1819 /*
1820 * boost idle prio on transactions that would lock out other
1821 * users of the filesystem
1822 */
1823 if (cfq_class_idle(cfqq))
1824 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1825 if (cfqq->ioprio > IOPRIO_NORM)
1826 cfqq->ioprio = IOPRIO_NORM;
1827 } else {
1828 /*
1829 * check if we need to unboost the queue
1830 */
1831 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1832 cfqq->ioprio_class = cfqq->org_ioprio_class;
1833 if (cfqq->ioprio != cfqq->org_ioprio)
1834 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
Jens Axboe22e2c502005-06-27 10:55:12 +02001837 /*
1838 * refile between round-robin lists if we moved the priority class
1839 */
1840 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001841 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001842 cfq_resort_rr_list(cfqq, 0);
1843}
1844
1845static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1846{
1847 if (rw == READ || process_sync(task))
1848 return task->pid;
1849
1850 return CFQ_KEY_ASYNC;
1851}
1852
1853static inline int
1854__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1855 struct task_struct *task, int rw)
1856{
Jens Axboe3b181522005-06-27 10:56:24 +02001857#if 1
1858 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001859 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001860 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001861 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001862 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001863
1864 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001865#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001866 if (!cfqq || task->flags & PF_MEMALLOC)
1867 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001868 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1869 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001870 return ELV_MQUEUE_MUST;
1871
1872 /*
1873 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1874 * can quickly flood the queue with writes from a single task
1875 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001876 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001877 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001878 return ELV_MQUEUE_MUST;
1879 }
1880
1881 return ELV_MQUEUE_MAY;
1882 }
1883 if (cfq_class_idle(cfqq))
1884 return ELV_MQUEUE_NO;
1885 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1886 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1887 int ret = ELV_MQUEUE_NO;
1888
1889 if (ioc && ioc->nr_batch_requests)
1890 ret = ELV_MQUEUE_MAY;
1891
1892 put_io_context(ioc);
1893 return ret;
1894 }
1895
1896 return ELV_MQUEUE_MAY;
1897#endif
1898}
1899
1900static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1901{
1902 struct cfq_data *cfqd = q->elevator->elevator_data;
1903 struct task_struct *tsk = current;
1904 struct cfq_queue *cfqq;
1905
1906 /*
1907 * don't force setup of a queue from here, as a call to may_queue
1908 * does not necessarily imply that a request actually will be queued.
1909 * so just lookup a possibly existing queue, or return 'may queue'
1910 * if that fails
1911 */
Jens Axboe3b181522005-06-27 10:56:24 +02001912 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001913 if (cfqq) {
1914 cfq_init_prio_data(cfqq);
1915 cfq_prio_boost(cfqq);
1916
1917 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1918 }
1919
1920 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1924{
Jens Axboe22e2c502005-06-27 10:55:12 +02001925 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Jens Axboe22e2c502005-06-27 10:55:12 +02001928 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1929 smp_mb();
1930 if (waitqueue_active(&rl->wait[READ]))
1931 wake_up(&rl->wait[READ]);
1932 }
1933
1934 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1935 smp_mb();
1936 if (waitqueue_active(&rl->wait[WRITE]))
1937 wake_up(&rl->wait[WRITE]);
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/*
1942 * queue lock held here
1943 */
1944static void cfq_put_request(request_queue_t *q, struct request *rq)
1945{
1946 struct cfq_data *cfqd = q->elevator->elevator_data;
1947 struct cfq_rq *crq = RQ_DATA(rq);
1948
1949 if (crq) {
1950 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001951 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jens Axboe22e2c502005-06-27 10:55:12 +02001953 BUG_ON(!cfqq->allocated[rw]);
1954 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jens Axboe22e2c502005-06-27 10:55:12 +02001956 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 mempool_free(crq, cfqd->crq_pool);
1959 rq->elevator_private = NULL;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 cfq_check_waiters(q, cfqq);
1962 cfq_put_queue(cfqq);
1963 }
1964}
1965
1966/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001967 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001969static int
1970cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001971 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001974 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct cfq_io_context *cic;
1976 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001977 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001978 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct cfq_rq *crq;
1980 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001981 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 might_sleep_if(gfp_mask & __GFP_WAIT);
1984
Jens Axboe3b181522005-06-27 10:56:24 +02001985 cic = cfq_get_io_context(cfqd, key, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 spin_lock_irqsave(q->queue_lock, flags);
1988
Jens Axboe22e2c502005-06-27 10:55:12 +02001989 if (!cic)
1990 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Al Viro12a05732006-03-18 13:38:01 -05001992 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001993 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001994 if (!cfqq)
1995 goto queue_fail;
1996
Al Viro12a05732006-03-18 13:38:01 -05001997 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001998 } else
Al Viro12a05732006-03-18 13:38:01 -05001999 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02002002 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002003 cfqd->rq_starved = 0;
2004 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 spin_unlock_irqrestore(q->queue_lock, flags);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2008 if (crq) {
2009 RB_CLEAR(&crq->rb_node);
2010 crq->rb_key = 0;
2011 crq->request = rq;
2012 INIT_HLIST_NODE(&crq->hash);
2013 crq->cfq_queue = cfqq;
2014 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02002015
Al Viro12a05732006-03-18 13:38:01 -05002016 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02002017 cfq_mark_crq_is_sync(crq);
2018 else
2019 cfq_clear_crq_is_sync(crq);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return 0;
2023 }
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 spin_lock_irqsave(q->queue_lock, flags);
2026 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02002027 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02002028 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002030queue_fail:
2031 if (cic)
2032 put_io_context(cic->ioc);
2033 /*
2034 * mark us rq allocation starved. we need to kickstart the process
2035 * ourselves if there are no pending requests that can do it for us.
2036 * that would be an extremely rare OOM situation
2037 */
2038 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002039 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 spin_unlock_irqrestore(q->queue_lock, flags);
2041 return 1;
2042}
2043
Jens Axboe22e2c502005-06-27 10:55:12 +02002044static void cfq_kick_queue(void *data)
2045{
2046 request_queue_t *q = data;
2047 struct cfq_data *cfqd = q->elevator->elevator_data;
2048 unsigned long flags;
2049
2050 spin_lock_irqsave(q->queue_lock, flags);
2051
2052 if (cfqd->rq_starved) {
2053 struct request_list *rl = &q->rq;
2054
2055 /*
2056 * we aren't guaranteed to get a request after this, but we
2057 * have to be opportunistic
2058 */
2059 smp_mb();
2060 if (waitqueue_active(&rl->wait[READ]))
2061 wake_up(&rl->wait[READ]);
2062 if (waitqueue_active(&rl->wait[WRITE]))
2063 wake_up(&rl->wait[WRITE]);
2064 }
2065
2066 blk_remove_plug(q);
2067 q->request_fn(q);
2068 spin_unlock_irqrestore(q->queue_lock, flags);
2069}
2070
2071/*
2072 * Timer running if the active_queue is currently idling inside its time slice
2073 */
2074static void cfq_idle_slice_timer(unsigned long data)
2075{
2076 struct cfq_data *cfqd = (struct cfq_data *) data;
2077 struct cfq_queue *cfqq;
2078 unsigned long flags;
2079
2080 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2081
2082 if ((cfqq = cfqd->active_queue) != NULL) {
2083 unsigned long now = jiffies;
2084
2085 /*
2086 * expired
2087 */
2088 if (time_after(now, cfqq->slice_end))
2089 goto expire;
2090
2091 /*
2092 * only expire and reinvoke request handler, if there are
2093 * other queues with pending requests
2094 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002095 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002096 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2097 add_timer(&cfqd->idle_slice_timer);
2098 goto out_cont;
2099 }
2100
2101 /*
2102 * not expired and it has a request pending, let it dispatch
2103 */
2104 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002105 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002106 goto out_kick;
2107 }
2108 }
2109expire:
2110 cfq_slice_expired(cfqd, 0);
2111out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002112 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002113out_cont:
2114 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2115}
2116
2117/*
2118 * Timer running if an idle class queue is waiting for service
2119 */
2120static void cfq_idle_class_timer(unsigned long data)
2121{
2122 struct cfq_data *cfqd = (struct cfq_data *) data;
2123 unsigned long flags, end;
2124
2125 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2126
2127 /*
2128 * race with a non-idle queue, reset timer
2129 */
2130 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2131 if (!time_after_eq(jiffies, end)) {
2132 cfqd->idle_class_timer.expires = end;
2133 add_timer(&cfqd->idle_class_timer);
2134 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002135 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002136
2137 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2138}
2139
Jens Axboe3b181522005-06-27 10:56:24 +02002140static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2141{
2142 del_timer_sync(&cfqd->idle_slice_timer);
2143 del_timer_sync(&cfqd->idle_class_timer);
2144 blk_sync_queue(cfqd->queue);
2145}
Jens Axboe22e2c502005-06-27 10:55:12 +02002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147static void cfq_put_cfqd(struct cfq_data *cfqd)
2148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (!atomic_dec_and_test(&cfqd->ref))
2150 return;
2151
Jens Axboe96c51ce2005-06-27 14:49:39 +02002152 cfq_shutdown_timer_wq(cfqd);
Jens Axboe96c51ce2005-06-27 14:49:39 +02002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 mempool_destroy(cfqd->crq_pool);
2155 kfree(cfqd->crq_hash);
2156 kfree(cfqd->cfq_hash);
2157 kfree(cfqd);
2158}
2159
2160static void cfq_exit_queue(elevator_t *e)
2161{
Jens Axboe22e2c502005-06-27 10:55:12 +02002162 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002163 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002164
Jens Axboe3b181522005-06-27 10:56:24 +02002165 cfq_shutdown_timer_wq(cfqd);
Al Virod9ff4182006-03-18 13:51:22 -05002166 write_lock(&cfq_exit_lock);
2167 spin_lock_irq(q->queue_lock);
2168 if (cfqd->active_queue)
2169 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2170 while(!list_empty(&cfqd->cic_list)) {
2171 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2172 struct cfq_io_context,
2173 queue_list);
2174 if (cic->cfqq[ASYNC]) {
2175 cfq_put_queue(cic->cfqq[ASYNC]);
2176 cic->cfqq[ASYNC] = NULL;
2177 }
2178 if (cic->cfqq[SYNC]) {
2179 cfq_put_queue(cic->cfqq[SYNC]);
2180 cic->cfqq[SYNC] = NULL;
2181 }
2182 cic->key = NULL;
2183 list_del_init(&cic->queue_list);
2184 }
2185 spin_unlock_irq(q->queue_lock);
2186 write_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02002187 cfq_put_cfqd(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
2190static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2191{
2192 struct cfq_data *cfqd;
2193 int i;
2194
2195 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2196 if (!cfqd)
2197 return -ENOMEM;
2198
2199 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002200
2201 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2202 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2203
2204 INIT_LIST_HEAD(&cfqd->busy_rr);
2205 INIT_LIST_HEAD(&cfqd->cur_rr);
2206 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002208 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2211 if (!cfqd->crq_hash)
2212 goto out_crqhash;
2213
2214 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2215 if (!cfqd->cfq_hash)
2216 goto out_cfqhash;
2217
2218 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2219 if (!cfqd->crq_pool)
2220 goto out_crqpool;
2221
2222 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2223 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2224 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2225 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2226
2227 e->elevator_data = cfqd;
2228
2229 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Jens Axboe22e2c502005-06-27 10:55:12 +02002231 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002233
2234 init_timer(&cfqd->idle_slice_timer);
2235 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2236 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2237
2238 init_timer(&cfqd->idle_class_timer);
2239 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2240 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2241
2242 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 atomic_set(&cfqd->ref, 1);
2245
2246 cfqd->cfq_queued = cfq_queued;
2247 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002248 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2249 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 cfqd->cfq_back_max = cfq_back_max;
2251 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002252 cfqd->cfq_slice[0] = cfq_slice_async;
2253 cfqd->cfq_slice[1] = cfq_slice_sync;
2254 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2255 cfqd->cfq_slice_idle = cfq_slice_idle;
2256 cfqd->cfq_max_depth = cfq_max_depth;
Jens Axboe3b181522005-06-27 10:56:24 +02002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
2259out_crqpool:
2260 kfree(cfqd->cfq_hash);
2261out_cfqhash:
2262 kfree(cfqd->crq_hash);
2263out_crqhash:
2264 kfree(cfqd);
2265 return -ENOMEM;
2266}
2267
2268static void cfq_slab_kill(void)
2269{
2270 if (crq_pool)
2271 kmem_cache_destroy(crq_pool);
2272 if (cfq_pool)
2273 kmem_cache_destroy(cfq_pool);
2274 if (cfq_ioc_pool)
2275 kmem_cache_destroy(cfq_ioc_pool);
2276}
2277
2278static int __init cfq_slab_setup(void)
2279{
2280 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2281 NULL, NULL);
2282 if (!crq_pool)
2283 goto fail;
2284
2285 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2286 NULL, NULL);
2287 if (!cfq_pool)
2288 goto fail;
2289
2290 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2291 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2292 if (!cfq_ioc_pool)
2293 goto fail;
2294
2295 return 0;
2296fail:
2297 cfq_slab_kill();
2298 return -ENOMEM;
2299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301/*
2302 * sysfs parts below -->
2303 */
2304struct cfq_fs_entry {
2305 struct attribute attr;
2306 ssize_t (*show)(struct cfq_data *, char *);
2307 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2308};
2309
2310static ssize_t
2311cfq_var_show(unsigned int var, char *page)
2312{
2313 return sprintf(page, "%d\n", var);
2314}
2315
2316static ssize_t
2317cfq_var_store(unsigned int *var, const char *page, size_t count)
2318{
2319 char *p = (char *) page;
2320
2321 *var = simple_strtoul(p, &p, 10);
2322 return count;
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2326static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2327{ \
2328 unsigned int __data = __VAR; \
2329 if (__CONV) \
2330 __data = jiffies_to_msecs(__data); \
2331 return cfq_var_show(__data, (page)); \
2332}
2333SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2334SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002335SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2336SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2338SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002339SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2340SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2341SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2342SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2343SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344#undef SHOW_FUNCTION
2345
2346#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2347static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2348{ \
2349 unsigned int __data; \
2350 int ret = cfq_var_store(&__data, (page), count); \
2351 if (__data < (MIN)) \
2352 __data = (MIN); \
2353 else if (__data > (MAX)) \
2354 __data = (MAX); \
2355 if (__CONV) \
2356 *(__PTR) = msecs_to_jiffies(__data); \
2357 else \
2358 *(__PTR) = __data; \
2359 return ret; \
2360}
2361STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2362STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002363STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2364STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2366STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002367STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2368STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2369STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2370STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2371STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372#undef STORE_FUNCTION
2373
2374static struct cfq_fs_entry cfq_quantum_entry = {
2375 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2376 .show = cfq_quantum_show,
2377 .store = cfq_quantum_store,
2378};
2379static struct cfq_fs_entry cfq_queued_entry = {
2380 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2381 .show = cfq_queued_show,
2382 .store = cfq_queued_store,
2383};
Jens Axboe22e2c502005-06-27 10:55:12 +02002384static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002386 .show = cfq_fifo_expire_sync_show,
2387 .store = cfq_fifo_expire_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388};
Jens Axboe22e2c502005-06-27 10:55:12 +02002389static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002391 .show = cfq_fifo_expire_async_show,
2392 .store = cfq_fifo_expire_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393};
2394static struct cfq_fs_entry cfq_back_max_entry = {
2395 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2396 .show = cfq_back_max_show,
2397 .store = cfq_back_max_store,
2398};
2399static struct cfq_fs_entry cfq_back_penalty_entry = {
2400 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2401 .show = cfq_back_penalty_show,
2402 .store = cfq_back_penalty_store,
2403};
Jens Axboe22e2c502005-06-27 10:55:12 +02002404static struct cfq_fs_entry cfq_slice_sync_entry = {
2405 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2406 .show = cfq_slice_sync_show,
2407 .store = cfq_slice_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408};
Jens Axboe22e2c502005-06-27 10:55:12 +02002409static struct cfq_fs_entry cfq_slice_async_entry = {
2410 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2411 .show = cfq_slice_async_show,
2412 .store = cfq_slice_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413};
Jens Axboe22e2c502005-06-27 10:55:12 +02002414static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2415 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2416 .show = cfq_slice_async_rq_show,
2417 .store = cfq_slice_async_rq_store,
2418};
2419static struct cfq_fs_entry cfq_slice_idle_entry = {
2420 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2421 .show = cfq_slice_idle_show,
2422 .store = cfq_slice_idle_store,
2423};
2424static struct cfq_fs_entry cfq_max_depth_entry = {
2425 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2426 .show = cfq_max_depth_show,
2427 .store = cfq_max_depth_store,
2428};
Jens Axboe3b181522005-06-27 10:56:24 +02002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430static struct attribute *default_attrs[] = {
2431 &cfq_quantum_entry.attr,
2432 &cfq_queued_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002433 &cfq_fifo_expire_sync_entry.attr,
2434 &cfq_fifo_expire_async_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 &cfq_back_max_entry.attr,
2436 &cfq_back_penalty_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002437 &cfq_slice_sync_entry.attr,
2438 &cfq_slice_async_entry.attr,
2439 &cfq_slice_async_rq_entry.attr,
2440 &cfq_slice_idle_entry.attr,
2441 &cfq_max_depth_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 NULL,
2443};
2444
2445#define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2446
2447static ssize_t
2448cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2449{
2450 elevator_t *e = container_of(kobj, elevator_t, kobj);
2451 struct cfq_fs_entry *entry = to_cfq(attr);
2452
2453 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002454 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 return entry->show(e->elevator_data, page);
2457}
2458
2459static ssize_t
2460cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2461 const char *page, size_t length)
2462{
2463 elevator_t *e = container_of(kobj, elevator_t, kobj);
2464 struct cfq_fs_entry *entry = to_cfq(attr);
2465
2466 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002467 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 return entry->store(e->elevator_data, page, length);
2470}
2471
2472static struct sysfs_ops cfq_sysfs_ops = {
2473 .show = cfq_attr_show,
2474 .store = cfq_attr_store,
2475};
2476
2477static struct kobj_type cfq_ktype = {
2478 .sysfs_ops = &cfq_sysfs_ops,
2479 .default_attrs = default_attrs,
2480};
2481
2482static struct elevator_type iosched_cfq = {
2483 .ops = {
2484 .elevator_merge_fn = cfq_merge,
2485 .elevator_merged_fn = cfq_merged_request,
2486 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002487 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002489 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .elevator_deactivate_req_fn = cfq_deactivate_request,
2491 .elevator_queue_empty_fn = cfq_queue_empty,
2492 .elevator_completed_req_fn = cfq_completed_request,
2493 .elevator_former_req_fn = cfq_former_request,
2494 .elevator_latter_req_fn = cfq_latter_request,
2495 .elevator_set_req_fn = cfq_set_request,
2496 .elevator_put_req_fn = cfq_put_request,
2497 .elevator_may_queue_fn = cfq_may_queue,
2498 .elevator_init_fn = cfq_init_queue,
2499 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002500 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 },
2502 .elevator_ktype = &cfq_ktype,
2503 .elevator_name = "cfq",
2504 .elevator_owner = THIS_MODULE,
2505};
2506
2507static int __init cfq_init(void)
2508{
2509 int ret;
2510
Jens Axboe22e2c502005-06-27 10:55:12 +02002511 /*
2512 * could be 0 on HZ < 1000 setups
2513 */
2514 if (!cfq_slice_async)
2515 cfq_slice_async = 1;
2516 if (!cfq_slice_idle)
2517 cfq_slice_idle = 1;
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (cfq_slab_setup())
2520 return -ENOMEM;
2521
2522 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002523 if (ret)
2524 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return ret;
2527}
2528
2529static void __exit cfq_exit(void)
2530{
Al Viro334e94d2006-03-18 15:05:53 -05002531 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002533 ioc_gone = &all_gone;
2534 barrier();
2535 if (atomic_read(&ioc_count))
2536 complete(ioc_gone);
2537 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002538 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541module_init(cfq_init);
2542module_exit(cfq_exit);
2543
2544MODULE_AUTHOR("Jens Axboe");
2545MODULE_LICENSE("GPL");
2546MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");