blob: 86f0f4796fdb11d8d034812f9526d42cd2a7ba1b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050010#include <linux/blkdev.h>
11#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hash.h>
13#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * tunables
18 */
Arjan van de Ven64100092006-01-06 09:46:02 +010019static const int cfq_quantum = 4; /* max queue in one round of service */
20static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
21static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
23static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Arjan van de Ven64100092006-01-06 09:46:02 +010025static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020026static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010027static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020028static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020029
30#define CFQ_IDLE_GRACE (HZ / 10)
31#define CFQ_SLICE_SCALE (5)
32
33#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020034
Jens Axboe3793c652006-05-30 21:11:04 +020035static DEFINE_SPINLOCK(cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -050036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * for the hash of cfqq inside the cfqd
39 */
40#define CFQ_QHASH_SHIFT 6
41#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
42#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
45
46#define RQ_DATA(rq) (rq)->elevator_private
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static kmem_cache_t *crq_pool;
49static kmem_cache_t *cfq_pool;
50static kmem_cache_t *cfq_ioc_pool;
51
Al Viro334e94d2006-03-18 15:05:53 -050052static atomic_t ioc_count = ATOMIC_INIT(0);
53static struct completion *ioc_gone;
54
Jens Axboe22e2c502005-06-27 10:55:12 +020055#define CFQ_PRIO_LISTS IOPRIO_BE_NR
56#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
57#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
58#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
59
Jens Axboe3b181522005-06-27 10:56:24 +020060#define ASYNC (0)
61#define SYNC (1)
62
63#define cfq_cfqq_dispatched(cfqq) \
64 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
65
66#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
67
68#define cfq_cfqq_sync(cfqq) \
69 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020070
Jens Axboe206dc692006-03-28 13:03:44 +020071#define sample_valid(samples) ((samples) > 80)
72
Jens Axboe22e2c502005-06-27 10:55:12 +020073/*
74 * Per block device queue structure
75 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020077 request_queue_t *queue;
78
79 /*
80 * rr list of queues with requests and the count of them
81 */
82 struct list_head rr_list[CFQ_PRIO_LISTS];
83 struct list_head busy_rr;
84 struct list_head cur_rr;
85 struct list_head idle_rr;
86 unsigned int busy_queues;
87
88 /*
89 * non-ordered list of empty cfqq's
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct list_head empty_list;
92
Jens Axboe22e2c502005-06-27 10:55:12 +020093 /*
94 * cfqq lookup hash
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mempool_t *crq_pool;
99
Jens Axboe22e2c502005-06-27 10:55:12 +0200100 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +0200101 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200102
103 /*
104 * schedule slice state info
105 */
106 /*
107 * idle window management
108 */
109 struct timer_list idle_slice_timer;
110 struct work_struct unplug_work;
111
112 struct cfq_queue *active_queue;
113 struct cfq_io_context *active_cic;
114 int cur_prio, cur_end_prio;
115 unsigned int dispatch_slice;
116
117 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200120 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jens Axboe22e2c502005-06-27 10:55:12 +0200122 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
125 * tunables, see top of file
126 */
127 unsigned int cfq_quantum;
128 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200129 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned int cfq_back_penalty;
131 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200132 unsigned int cfq_slice[2];
133 unsigned int cfq_slice_async_rq;
134 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500135
136 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Jens Axboe22e2c502005-06-27 10:55:12 +0200139/*
140 * Per process-grouping structure
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct cfq_queue {
143 /* reference count */
144 atomic_t ref;
145 /* parent cfq_data */
146 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200147 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct hlist_node cfq_hash;
149 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200150 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* on either rr or empty list of cfqd */
152 struct list_head cfq_list;
153 /* sorted list of pending requests */
154 struct rb_root sort_list;
155 /* if fifo isn't expired, next request to serve */
156 struct cfq_rq *next_crq;
157 /* requests queued in sort_list */
158 int queued[2];
159 /* currently allocated requests */
160 int allocated[2];
161 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200162 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jens Axboe22e2c502005-06-27 10:55:12 +0200164 unsigned long slice_start;
165 unsigned long slice_end;
166 unsigned long slice_left;
167 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jens Axboe3b181522005-06-27 10:56:24 +0200169 /* number of requests that are on the dispatch list */
170 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200171
172 /* io prio of this group */
173 unsigned short ioprio, org_ioprio;
174 unsigned short ioprio_class, org_ioprio_class;
175
Jens Axboe3b181522005-06-27 10:56:24 +0200176 /* various state flags, see below */
177 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180struct cfq_rq {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 struct cfq_queue *cfq_queue;
184 struct cfq_io_context *io_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Jens Axboe3b181522005-06-27 10:56:24 +0200187enum cfqq_state_flags {
188 CFQ_CFQQ_FLAG_on_rr = 0,
189 CFQ_CFQQ_FLAG_wait_request,
190 CFQ_CFQQ_FLAG_must_alloc,
191 CFQ_CFQQ_FLAG_must_alloc_slice,
192 CFQ_CFQQ_FLAG_must_dispatch,
193 CFQ_CFQQ_FLAG_fifo_expire,
194 CFQ_CFQQ_FLAG_idle_window,
195 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200196};
197
198#define CFQ_CFQQ_FNS(name) \
199static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
200{ \
201 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
202} \
203static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
204{ \
205 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
206} \
207static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
208{ \
209 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
210}
211
212CFQ_CFQQ_FNS(on_rr);
213CFQ_CFQQ_FNS(wait_request);
214CFQ_CFQQ_FNS(must_alloc);
215CFQ_CFQQ_FNS(must_alloc_slice);
216CFQ_CFQQ_FNS(must_dispatch);
217CFQ_CFQQ_FNS(fifo_expire);
218CFQ_CFQQ_FNS(idle_window);
219CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200220#undef CFQ_CFQQ_FNS
221
Jens Axboe3b181522005-06-27 10:56:24 +0200222static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200223static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Al Viro6f325a12006-03-18 14:58:37 -0500224static 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 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700227 * scheduler run of queue, if there are requests pending and no one in the
228 * driver that will restart queueing
229 */
230static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
231{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100232 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700233 kblockd_schedule_work(&cfqd->unplug_work);
234}
235
236static int cfq_queue_empty(request_queue_t *q)
237{
238 struct cfq_data *cfqd = q->elevator->elevator_data;
239
Jens Axboeb4878f22005-10-20 16:42:29 +0200240 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700241}
242
Jens Axboe206dc692006-03-28 13:03:44 +0200243static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
244{
Jens Axboeb31dc662006-06-13 08:26:10 +0200245 if (rw == READ || rw == WRITE_SYNC)
Jens Axboe206dc692006-03-28 13:03:44 +0200246 return task->pid;
247
248 return CFQ_KEY_ASYNC;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
253 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200254 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256static struct cfq_rq *
257cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
258{
259 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200261#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
262#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
263 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (crq1 == NULL || crq1 == crq2)
266 return crq2;
267 if (crq2 == NULL)
268 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200269
Jens Axboe5380a102006-07-13 12:37:56 +0200270 if (rq_is_sync(crq1->request) && !rq_is_sync(crq2->request))
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200271 return crq1;
Jens Axboe5380a102006-07-13 12:37:56 +0200272 else if (rq_is_sync(crq2->request) && !rq_is_sync(crq1->request))
Jens Axboe22e2c502005-06-27 10:55:12 +0200273 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 s1 = crq1->request->sector;
276 s2 = crq2->request->sector;
277
278 last = cfqd->last_sector;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * by definition, 1KiB is 2 sectors
282 */
283 back_max = cfqd->cfq_back_max * 2;
284
285 /*
286 * Strict one way elevator _except_ in the case where we allow
287 * short backward seeks which are biased as twice the cost of a
288 * similar forward seek.
289 */
290 if (s1 >= last)
291 d1 = s1 - last;
292 else if (s1 + back_max >= last)
293 d1 = (last - s1) * cfqd->cfq_back_penalty;
294 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200295 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (s2 >= last)
298 d2 = s2 - last;
299 else if (s2 + back_max >= last)
300 d2 = (last - s2) * cfqd->cfq_back_penalty;
301 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200302 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Andreas Mohre8a99052006-03-28 08:59:49 +0200306 /*
307 * By doing switch() on the bit mask "wrap" we avoid having to
308 * check two variables for all permutations: --> faster!
309 */
310 switch (wrap) {
311 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
312 if (d1 < d2)
313 return crq1;
314 else if (d2 < d1)
315 return crq2;
316 else {
317 if (s1 >= s2)
318 return crq1;
319 else
320 return crq2;
321 }
322
323 case CFQ_RQ2_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return crq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200325 case CFQ_RQ1_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return crq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200327 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
328 default:
329 /*
330 * Since both rqs are wrapped,
331 * start with the one that's further behind head
332 * (--> only *one* back seek required),
333 * since back seek takes more time than forward.
334 */
335 if (s1 <= s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return crq1;
337 else
338 return crq2;
339 }
340}
341
342/*
343 * would be nice to take fifo expire time into account as well
344 */
345static struct cfq_rq *
346cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe21183b02006-07-13 12:33:14 +0200347 struct cfq_rq *last_crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Jens Axboe21183b02006-07-13 12:33:14 +0200349 struct request *last = last_crq->request;
350 struct rb_node *rbnext = rb_next(&last->rb_node);
351 struct rb_node *rbprev = rb_prev(&last->rb_node);
352 struct cfq_rq *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jens Axboe21183b02006-07-13 12:33:14 +0200354 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (rbprev)
Jens Axboe21183b02006-07-13 12:33:14 +0200357 prev = RQ_DATA(rb_entry_rq(rbprev));
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (rbnext)
Jens Axboe21183b02006-07-13 12:33:14 +0200360 next = RQ_DATA(rb_entry_rq(rbnext));
361 else {
362 rbnext = rb_first(&cfqq->sort_list);
363 if (rbnext && rbnext != &last->rb_node)
364 next = RQ_DATA(rb_entry_rq(rbnext));
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Jens Axboe21183b02006-07-13 12:33:14 +0200367 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Jens Axboe22e2c502005-06-27 10:55:12 +0200370static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Jens Axboe22e2c502005-06-27 10:55:12 +0200372 struct cfq_data *cfqd = cfqq->cfqd;
373 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jens Axboe3b181522005-06-27 10:56:24 +0200375 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 list_del(&cfqq->cfq_list);
378
Jens Axboe22e2c502005-06-27 10:55:12 +0200379 if (cfq_class_rt(cfqq))
380 list = &cfqd->cur_rr;
381 else if (cfq_class_idle(cfqq))
382 list = &cfqd->idle_rr;
383 else {
384 /*
385 * if cfqq has requests in flight, don't allow it to be
386 * found in cfq_set_active_queue before it has finished them.
387 * this is done to increase fairness between a process that
388 * has lots of io pending vs one that only generates one
389 * sporadically or synchronously
390 */
Jens Axboe3b181522005-06-27 10:56:24 +0200391 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200392 list = &cfqd->busy_rr;
393 else
394 list = &cfqd->rr_list[cfqq->ioprio];
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200398 * if queue was preempted, just add to front to be fair. busy_rr
Jens Axboeb52a8342006-06-01 18:53:43 +0200399 * isn't sorted, but insert at the back for fairness.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200401 if (preempted || list == &cfqd->busy_rr) {
Jens Axboeb52a8342006-06-01 18:53:43 +0200402 if (preempted)
403 list = list->prev;
404
405 list_add_tail(&cfqq->cfq_list, list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200406 return;
407 }
408
409 /*
410 * sort by when queue was last serviced
411 */
412 entry = list;
413 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
415
Jens Axboe22e2c502005-06-27 10:55:12 +0200416 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200418 if (time_before(__cfqq->service_last, cfqq->service_last))
419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 list_add(&cfqq->cfq_list, entry);
423}
424
425/*
426 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200427 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
429static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200430cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Jens Axboe3b181522005-06-27 10:56:24 +0200432 BUG_ON(cfq_cfqq_on_rr(cfqq));
433 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cfqd->busy_queues++;
435
Jens Axboeb4878f22005-10-20 16:42:29 +0200436 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static inline void
440cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
441{
Jens Axboe3b181522005-06-27 10:56:24 +0200442 BUG_ON(!cfq_cfqq_on_rr(cfqq));
443 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200444 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 BUG_ON(!cfqd->busy_queues);
447 cfqd->busy_queues--;
448}
449
450/*
451 * rb tree support functions
452 */
453static inline void cfq_del_crq_rb(struct cfq_rq *crq)
454{
455 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200456 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +0200457 const int sync = rq_is_sync(crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Jens Axboeb4878f22005-10-20 16:42:29 +0200459 BUG_ON(!cfqq->queued[sync]);
460 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jens Axboe21183b02006-07-13 12:33:14 +0200462 elv_rb_del(&cfqq->sort_list, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Jens Axboedd67d052006-06-21 09:36:18 +0200464 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200465 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static void cfq_add_crq_rb(struct cfq_rq *crq)
469{
470 struct cfq_queue *cfqq = crq->cfq_queue;
471 struct cfq_data *cfqd = cfqq->cfqd;
472 struct request *rq = crq->request;
Jens Axboe21183b02006-07-13 12:33:14 +0200473 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jens Axboe5380a102006-07-13 12:37:56 +0200475 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * looks a little odd, but the first insert might return an alias.
479 * if that happens, put the alias on the dispatch list
480 */
Jens Axboe21183b02006-07-13 12:33:14 +0200481 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
482 cfq_dispatch_insert(cfqd->queue, RQ_DATA(__alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485static inline void
486cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
487{
Jens Axboe5380a102006-07-13 12:37:56 +0200488 struct request *rq = crq->request;
489
490 elv_rb_del(&cfqq->sort_list, rq);
491 cfqq->queued[rq_is_sync(rq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 cfq_add_crq_rb(crq);
493}
494
Jens Axboe206dc692006-03-28 13:03:44 +0200495static struct request *
496cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jens Axboe206dc692006-03-28 13:03:44 +0200498 struct task_struct *tsk = current;
499 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
Jens Axboe21183b02006-07-13 12:33:14 +0200500 sector_t sector = bio->bi_sector + bio_sectors(bio);
Jens Axboe206dc692006-03-28 13:03:44 +0200501 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Jens Axboe206dc692006-03-28 13:03:44 +0200503 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe21183b02006-07-13 12:33:14 +0200504 if (cfqq)
505 return elv_rb_find(&cfqq->sort_list, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return NULL;
508}
509
Jens Axboeb4878f22005-10-20 16:42:29 +0200510static void cfq_activate_request(request_queue_t *q, struct request *rq)
511{
512 struct cfq_data *cfqd = q->elevator->elevator_data;
513
514 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200515
516 /*
517 * If the depth is larger 1, it really could be queueing. But lets
518 * make the mark a little higher - idling could still be good for
519 * low queueing, and a low queueing number could also just indicate
520 * a SCSI mid layer like behaviour where limit+1 is often seen.
521 */
522 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
523 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
527{
Jens Axboe22e2c502005-06-27 10:55:12 +0200528 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jens Axboeb4878f22005-10-20 16:42:29 +0200530 WARN_ON(!cfqd->rq_in_driver);
531 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Jens Axboeb4878f22005-10-20 16:42:29 +0200534static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200537 struct cfq_queue *cfqq = crq->cfq_queue;
538
539 if (cfqq->next_crq == crq)
540 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jens Axboeb4878f22005-10-20 16:42:29 +0200542 list_del_init(&rq->queuelist);
543 cfq_del_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546static int
547cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
548{
549 struct cfq_data *cfqd = q->elevator->elevator_data;
550 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jens Axboe206dc692006-03-28 13:03:44 +0200552 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200553 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200554 *req = __rq;
555 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Jens Axboe21183b02006-07-13 12:33:14 +0200561static void cfq_merged_request(request_queue_t *q, struct request *req,
562 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct cfq_rq *crq = RQ_DATA(req);
565
Jens Axboe21183b02006-07-13 12:33:14 +0200566 if (type == ELEVATOR_FRONT_MERGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct cfq_queue *cfqq = crq->cfq_queue;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 cfq_reposition_crq_rb(cfqq, crq);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static void
574cfq_merged_requests(request_queue_t *q, struct request *rq,
575 struct request *next)
576{
Jens Axboe22e2c502005-06-27 10:55:12 +0200577 /*
578 * reposition in fifo if next is older than rq
579 */
580 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
581 time_before(next->start_time, rq->start_time))
582 list_move(&rq->queuelist, &next->queuelist);
583
Jens Axboeb4878f22005-10-20 16:42:29 +0200584 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200585}
586
587static inline void
588__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
589{
590 if (cfqq) {
591 /*
592 * stop potential idle class queues waiting service
593 */
594 del_timer(&cfqd->idle_class_timer);
595
596 cfqq->slice_start = jiffies;
597 cfqq->slice_end = 0;
598 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200599 cfq_clear_cfqq_must_alloc_slice(cfqq);
600 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200601 }
602
603 cfqd->active_queue = cfqq;
604}
605
606/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100607 * current cfqq expired its slice (or was too idle), select new one
608 */
609static void
610__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
611 int preempted)
612{
613 unsigned long now = jiffies;
614
615 if (cfq_cfqq_wait_request(cfqq))
616 del_timer(&cfqd->idle_slice_timer);
617
618 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
619 cfqq->service_last = now;
620 cfq_schedule_dispatch(cfqd);
621 }
622
623 cfq_clear_cfqq_must_dispatch(cfqq);
624 cfq_clear_cfqq_wait_request(cfqq);
625
626 /*
627 * store what was left of this slice, if the queue idled out
628 * or was preempted
629 */
630 if (time_after(cfqq->slice_end, now))
631 cfqq->slice_left = cfqq->slice_end - now;
632 else
633 cfqq->slice_left = 0;
634
635 if (cfq_cfqq_on_rr(cfqq))
636 cfq_resort_rr_list(cfqq, preempted);
637
638 if (cfqq == cfqd->active_queue)
639 cfqd->active_queue = NULL;
640
641 if (cfqd->active_cic) {
642 put_io_context(cfqd->active_cic->ioc);
643 cfqd->active_cic = NULL;
644 }
645
646 cfqd->dispatch_slice = 0;
647}
648
649static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
650{
651 struct cfq_queue *cfqq = cfqd->active_queue;
652
653 if (cfqq)
654 __cfq_slice_expired(cfqd, cfqq, preempted);
655}
656
657/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200658 * 0
659 * 0,1
660 * 0,1,2
661 * 0,1,2,3
662 * 0,1,2,3,4
663 * 0,1,2,3,4,5
664 * 0,1,2,3,4,5,6
665 * 0,1,2,3,4,5,6,7
666 */
667static int cfq_get_next_prio_level(struct cfq_data *cfqd)
668{
669 int prio, wrap;
670
671 prio = -1;
672 wrap = 0;
673 do {
674 int p;
675
676 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
677 if (!list_empty(&cfqd->rr_list[p])) {
678 prio = p;
679 break;
680 }
681 }
682
683 if (prio != -1)
684 break;
685 cfqd->cur_prio = 0;
686 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
687 cfqd->cur_end_prio = 0;
688 if (wrap)
689 break;
690 wrap = 1;
691 }
692 } while (1);
693
694 if (unlikely(prio == -1))
695 return -1;
696
697 BUG_ON(prio >= CFQ_PRIO_LISTS);
698
699 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
700
701 cfqd->cur_prio = prio + 1;
702 if (cfqd->cur_prio > cfqd->cur_end_prio) {
703 cfqd->cur_end_prio = cfqd->cur_prio;
704 cfqd->cur_prio = 0;
705 }
706 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
707 cfqd->cur_prio = 0;
708 cfqd->cur_end_prio = 0;
709 }
710
711 return prio;
712}
713
Jens Axboe3b181522005-06-27 10:56:24 +0200714static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200715{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100716 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200717
718 /*
719 * if current list is non-empty, grab first entry. if it is empty,
720 * get next prio level and grab first entry then if any are spliced
721 */
722 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
723 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
724
725 /*
Jens Axboee0de0202006-06-01 10:07:26 +0200726 * If no new queues are available, check if the busy list has some
727 * before falling back to idle io.
728 */
729 if (!cfqq && !list_empty(&cfqd->busy_rr))
730 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
731
732 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200733 * if we have idle queues and no rt or be queues had pending
734 * requests, either allow immediate service if the grace period
735 * has passed or arm the idle grace timer
736 */
737 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
738 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
739
740 if (time_after_eq(jiffies, end))
741 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
742 else
743 mod_timer(&cfqd->idle_class_timer, end);
744 }
745
746 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200747 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200748}
749
Jens Axboecaaa5f92006-06-16 11:23:00 +0200750#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
751
Jens Axboe22e2c502005-06-27 10:55:12 +0200752static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
753
754{
Jens Axboe206dc692006-03-28 13:03:44 +0200755 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100756 unsigned long sl;
757
Jens Axboedd67d052006-06-21 09:36:18 +0200758 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200759 WARN_ON(cfqq != cfqd->active_queue);
760
761 /*
762 * idle is disabled, either manually or by past process history
763 */
764 if (!cfqd->cfq_slice_idle)
765 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200766 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200767 return 0;
768 /*
769 * task has exited, don't wait
770 */
Jens Axboe206dc692006-03-28 13:03:44 +0200771 cic = cfqd->active_cic;
772 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200773 return 0;
774
Jens Axboe3b181522005-06-27 10:56:24 +0200775 cfq_mark_cfqq_must_dispatch(cfqq);
776 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200777
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100778 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200779
780 /*
781 * we don't want to idle for seeks, but we do want to allow
782 * fair distribution of slice time for a process doing back-to-back
783 * seeks. so allow a little bit of time for him to submit a new rq
784 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200785 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200786 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200787
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100788 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200789 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Jens Axboeb4878f22005-10-20 16:42:29 +0200792static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct cfq_data *cfqd = q->elevator->elevator_data;
795 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe5380a102006-07-13 12:37:56 +0200796 struct request *rq = crq->request;
Jens Axboe22e2c502005-06-27 10:55:12 +0200797
Jens Axboe5380a102006-07-13 12:37:56 +0200798 cfq_remove_request(rq);
799 cfqq->on_dispatch[rq_is_sync(rq)]++;
800 elv_dispatch_sort(q, rq);
Jens Axboefd61af02006-06-16 15:35:39 +0200801
802 rq = list_entry(q->queue_head.prev, struct request, queuelist);
803 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806/*
807 * return expired entry, or NULL to just start from scratch in rbtree
808 */
809static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
810{
811 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200812 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct cfq_rq *crq;
814
Jens Axboe3b181522005-06-27 10:56:24 +0200815 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return NULL;
817
Jens Axboe22e2c502005-06-27 10:55:12 +0200818 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200819 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Jens Axboe95e88102006-07-11 21:30:31 +0200821 crq = RQ_DATA(rq_entry_fifo(cfqq->fifo.next));
Jens Axboe22e2c502005-06-27 10:55:12 +0200822 rq = crq->request;
823 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200824 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200825 return crq;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 return NULL;
830}
831
832/*
Jens Axboe3b181522005-06-27 10:56:24 +0200833 * Scale schedule slice based on io priority. Use the sync time slice only
834 * if a queue is marked sync and has sync io queued. A sync queue with async
835 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200837static inline int
838cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Jens Axboe22e2c502005-06-27 10:55:12 +0200840 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Jens Axboe22e2c502005-06-27 10:55:12 +0200842 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jens Axboe22e2c502005-06-27 10:55:12 +0200844 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Jens Axboe22e2c502005-06-27 10:55:12 +0200847static inline void
848cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
849{
850 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
851}
852
853static inline int
854cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
855{
856 const int base_rq = cfqd->cfq_slice_async_rq;
857
858 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
859
860 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
861}
862
863/*
864 * get next queue for service
865 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100866static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200867{
868 unsigned long now = jiffies;
869 struct cfq_queue *cfqq;
870
871 cfqq = cfqd->active_queue;
872 if (!cfqq)
873 goto new_queue;
874
875 /*
876 * slice has expired
877 */
Jens Axboe3b181522005-06-27 10:56:24 +0200878 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
879 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200880
881 /*
882 * if queue has requests, dispatch one. if not, check if
883 * enough slice is left to wait for one
884 */
Jens Axboedd67d052006-06-21 09:36:18 +0200885 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200886 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200887 else if (cfq_cfqq_dispatched(cfqq)) {
888 cfqq = NULL;
889 goto keep_queue;
890 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200891 if (cfq_arm_slice_timer(cfqd, cfqq))
892 return NULL;
893 }
894
Jens Axboe3b181522005-06-27 10:56:24 +0200895expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200896 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200897new_queue:
898 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200899keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200900 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200901}
902
903static int
904__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
905 int max_dispatch)
906{
907 int dispatched = 0;
908
Jens Axboedd67d052006-06-21 09:36:18 +0200909 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200910
911 do {
912 struct cfq_rq *crq;
913
914 /*
915 * follow expired path, else get first next available
916 */
917 if ((crq = cfq_check_fifo(cfqq)) == NULL)
918 crq = cfqq->next_crq;
919
920 /*
921 * finally, insert request into driver dispatch list
922 */
Jens Axboeb4878f22005-10-20 16:42:29 +0200923 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200924
925 cfqd->dispatch_slice++;
926 dispatched++;
927
928 if (!cfqd->active_cic) {
929 atomic_inc(&crq->io_context->ioc->refcount);
930 cfqd->active_cic = crq->io_context;
931 }
932
Jens Axboedd67d052006-06-21 09:36:18 +0200933 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200934 break;
935
936 } while (dispatched < max_dispatch);
937
938 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +0200939 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +0200940 */
941 if (!cfqq->slice_end)
942 cfq_set_prio_slice(cfqd, cfqq);
943
944 /*
945 * expire an async queue immediately if it has used up its slice. idle
946 * queue always expire after 1 dispatch round.
947 */
948 if ((!cfq_cfqq_sync(cfqq) &&
949 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +0200950 cfq_class_idle(cfqq) ||
951 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200952 cfq_slice_expired(cfqd, 0);
953
954 return dispatched;
955}
956
957static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100958cfq_forced_dispatch_cfqqs(struct list_head *list)
959{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100960 struct cfq_queue *cfqq, *next;
961 struct cfq_rq *crq;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200962 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100963
Jens Axboecaaa5f92006-06-16 11:23:00 +0200964 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100965 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
966 while ((crq = cfqq->next_crq)) {
967 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
968 dispatched++;
969 }
970 BUG_ON(!list_empty(&cfqq->fifo));
971 }
Jens Axboecaaa5f92006-06-16 11:23:00 +0200972
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100973 return dispatched;
974}
975
976static int
977cfq_forced_dispatch(struct cfq_data *cfqd)
978{
979 int i, dispatched = 0;
980
981 for (i = 0; i < CFQ_PRIO_LISTS; i++)
982 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
983
984 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
985 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
986 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
987
988 cfq_slice_expired(cfqd, 0);
989
990 BUG_ON(cfqd->busy_queues);
991
992 return dispatched;
993}
994
995static int
Jens Axboeb4878f22005-10-20 16:42:29 +0200996cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200999 struct cfq_queue *cfqq, *prev_cfqq;
1000 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jens Axboe22e2c502005-06-27 10:55:12 +02001002 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
1004
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001005 if (unlikely(force))
1006 return cfq_forced_dispatch(cfqd);
1007
Jens Axboecaaa5f92006-06-16 11:23:00 +02001008 dispatched = 0;
1009 prev_cfqq = NULL;
1010 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001011 int max_dispatch;
1012
Jens Axboecaaa5f92006-06-16 11:23:00 +02001013 /*
1014 * Don't repeat dispatch from the previous queue.
1015 */
1016 if (prev_cfqq == cfqq)
1017 break;
1018
Jens Axboe3b181522005-06-27 10:56:24 +02001019 cfq_clear_cfqq_must_dispatch(cfqq);
1020 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001021 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001023 max_dispatch = cfqd->cfq_quantum;
1024 if (cfq_class_idle(cfqq))
1025 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Jens Axboecaaa5f92006-06-16 11:23:00 +02001027 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1028
1029 /*
1030 * If the dispatch cfqq has idling enabled and is still
1031 * the active queue, break out.
1032 */
1033 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1034 break;
1035
1036 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Jens Axboecaaa5f92006-06-16 11:23:00 +02001039 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/*
1043 * task holds one reference to the queue, dropped when task exits. each crq
1044 * in-flight on this queue also holds a reference, dropped when crq is freed.
1045 *
1046 * queue lock must be held here.
1047 */
1048static void cfq_put_queue(struct cfq_queue *cfqq)
1049{
Jens Axboe22e2c502005-06-27 10:55:12 +02001050 struct cfq_data *cfqd = cfqq->cfqd;
1051
1052 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (!atomic_dec_and_test(&cfqq->ref))
1055 return;
1056
1057 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001058 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001059 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001061 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001062 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
1065 * it's on the empty list and still hashed
1066 */
1067 list_del(&cfqq->cfq_list);
1068 hlist_del(&cfqq->cfq_hash);
1069 kmem_cache_free(cfq_pool, cfqq);
1070}
1071
1072static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001073__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1074 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001077 struct hlist_node *entry;
1078 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jens Axboe206dc692006-03-28 13:03:44 +02001080 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001081 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Jens Axboe206dc692006-03-28 13:03:44 +02001083 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return __cfqq;
1085 }
1086
1087 return NULL;
1088}
1089
1090static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001091cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Jens Axboe3b181522005-06-27 10:56:24 +02001093 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Jens Axboee2d74ac2006-03-28 08:59:01 +02001096static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Jens Axboe22e2c502005-06-27 10:55:12 +02001098 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001099 struct rb_node *n;
1100 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001101
Jens Axboee2d74ac2006-03-28 08:59:01 +02001102 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1103 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1104 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001105 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001106 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001107 }
1108
Al Viro334e94d2006-03-18 15:05:53 -05001109 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1110 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Al Viroe17a9482006-03-18 13:21:20 -05001113static void cfq_trim(struct io_context *ioc)
1114{
1115 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001116 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001120 * Called with interrupts disabled
1121 */
1122static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1123{
Al Viro478a82b2006-03-18 13:25:24 -05001124 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001125 request_queue_t *q;
1126
1127 if (!cfqd)
1128 return;
1129
1130 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001131
1132 WARN_ON(!irqs_disabled());
1133
1134 spin_lock(q->queue_lock);
1135
Al Viro12a05732006-03-18 13:38:01 -05001136 if (cic->cfqq[ASYNC]) {
1137 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1138 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1139 cfq_put_queue(cic->cfqq[ASYNC]);
1140 cic->cfqq[ASYNC] = NULL;
1141 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001142
Al Viro12a05732006-03-18 13:38:01 -05001143 if (cic->cfqq[SYNC]) {
1144 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1145 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1146 cfq_put_queue(cic->cfqq[SYNC]);
1147 cic->cfqq[SYNC] = NULL;
1148 }
1149
Al Viro478a82b2006-03-18 13:25:24 -05001150 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001151 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001152 spin_unlock(q->queue_lock);
1153}
1154
Jens Axboee2d74ac2006-03-28 08:59:01 +02001155static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jens Axboe22e2c502005-06-27 10:55:12 +02001157 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001159 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * put the reference this task is holding to the various queues
1163 */
Jens Axboe3793c652006-05-30 21:11:04 +02001164 spin_lock_irqsave(&cfq_exit_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001165
1166 n = rb_first(&ioc->cic_root);
1167 while (n != NULL) {
1168 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1169
Jens Axboe22e2c502005-06-27 10:55:12 +02001170 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001171 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Jens Axboe3793c652006-05-30 21:11:04 +02001174 spin_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Jens Axboe22e2c502005-06-27 10:55:12 +02001177static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001178cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jens Axboe22e2c502005-06-27 10:55:12 +02001180 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001183 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001184 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001185 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001186 cic->dtor = cfq_free_io_context;
1187 cic->exit = cfq_exit_io_context;
Al Viro334e94d2006-03-18 15:05:53 -05001188 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
1191 return cic;
1192}
1193
Jens Axboe22e2c502005-06-27 10:55:12 +02001194static void cfq_init_prio_data(struct cfq_queue *cfqq)
1195{
1196 struct task_struct *tsk = current;
1197 int ioprio_class;
1198
Jens Axboe3b181522005-06-27 10:56:24 +02001199 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001200 return;
1201
1202 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1203 switch (ioprio_class) {
1204 default:
1205 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1206 case IOPRIO_CLASS_NONE:
1207 /*
1208 * no prio set, place us in the middle of the BE classes
1209 */
1210 cfqq->ioprio = task_nice_ioprio(tsk);
1211 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1212 break;
1213 case IOPRIO_CLASS_RT:
1214 cfqq->ioprio = task_ioprio(tsk);
1215 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1216 break;
1217 case IOPRIO_CLASS_BE:
1218 cfqq->ioprio = task_ioprio(tsk);
1219 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1220 break;
1221 case IOPRIO_CLASS_IDLE:
1222 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1223 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001224 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001225 break;
1226 }
1227
1228 /*
1229 * keep track of original prio settings in case we have to temporarily
1230 * elevate the priority of this queue
1231 */
1232 cfqq->org_ioprio = cfqq->ioprio;
1233 cfqq->org_ioprio_class = cfqq->ioprio_class;
1234
Jens Axboe3b181522005-06-27 10:56:24 +02001235 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001236 cfq_resort_rr_list(cfqq, 0);
1237
Jens Axboe3b181522005-06-27 10:56:24 +02001238 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001239}
1240
Al Viro478a82b2006-03-18 13:25:24 -05001241static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001242{
Al Viro478a82b2006-03-18 13:25:24 -05001243 struct cfq_data *cfqd = cic->key;
1244 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02001245
Jens Axboecaaa5f92006-06-16 11:23:00 +02001246 if (unlikely(!cfqd))
1247 return;
1248
1249 spin_lock(cfqd->queue->queue_lock);
1250
1251 cfqq = cic->cfqq[ASYNC];
1252 if (cfqq) {
1253 struct cfq_queue *new_cfqq;
1254 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1255 GFP_ATOMIC);
1256 if (new_cfqq) {
1257 cic->cfqq[ASYNC] = new_cfqq;
1258 cfq_put_queue(cfqq);
1259 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001260 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001261
1262 cfqq = cic->cfqq[SYNC];
1263 if (cfqq)
1264 cfq_mark_cfqq_prio_changed(cfqq);
1265
1266 spin_unlock(cfqd->queue->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001270 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001272static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Al Viroa6a07632006-03-18 13:26:44 -05001274 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001275 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001276
Jens Axboe3793c652006-05-30 21:11:04 +02001277 spin_lock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001278
Jens Axboee2d74ac2006-03-28 08:59:01 +02001279 n = rb_first(&ioc->cic_root);
1280 while (n != NULL) {
1281 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001282
Al Viro478a82b2006-03-18 13:25:24 -05001283 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001284 n = rb_next(n);
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Jens Axboe3793c652006-05-30 21:11:04 +02001287 spin_unlock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001288
Jens Axboe22e2c502005-06-27 10:55:12 +02001289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001293cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001294 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1297 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001298 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300retry:
Al Viro6f325a12006-03-18 14:58:37 -05001301 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001302 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (!cfqq) {
1305 if (new_cfqq) {
1306 cfqq = new_cfqq;
1307 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001308 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 spin_unlock_irq(cfqd->queue->queue_lock);
1310 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1311 spin_lock_irq(cfqd->queue->queue_lock);
1312 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001313 } else {
1314 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1315 if (!cfqq)
1316 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 memset(cfqq, 0, sizeof(*cfqq));
1320
1321 INIT_HLIST_NODE(&cfqq->cfq_hash);
1322 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001323 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 cfqq->key = key;
1326 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1327 atomic_set(&cfqq->ref, 0);
1328 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001329 cfqq->service_last = 0;
1330 /*
1331 * set ->slice_left to allow preemption for a new process
1332 */
1333 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001334 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001335 cfq_mark_cfqq_prio_changed(cfqq);
1336 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
1339 if (new_cfqq)
1340 kmem_cache_free(cfq_pool, new_cfqq);
1341
1342 atomic_inc(&cfqq->ref);
1343out:
1344 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1345 return cfqq;
1346}
1347
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001348static void
1349cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1350{
Jens Axboe3793c652006-05-30 21:11:04 +02001351 spin_lock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001352 rb_erase(&cic->rb_node, &ioc->cic_root);
Jens Axboe3793c652006-05-30 21:11:04 +02001353 list_del_init(&cic->queue_list);
1354 spin_unlock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001355 kmem_cache_free(cfq_ioc_pool, cic);
1356 atomic_dec(&ioc_count);
1357}
1358
Jens Axboee2d74ac2006-03-28 08:59:01 +02001359static struct cfq_io_context *
1360cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1361{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001362 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001363 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001364 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001365
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001366restart:
1367 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001368 while (n) {
1369 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001370 /* ->key must be copied to avoid race with cfq_exit_queue() */
1371 k = cic->key;
1372 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001373 cfq_drop_dead_cic(ioc, cic);
1374 goto restart;
1375 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001376
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001377 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001378 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001379 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001380 n = n->rb_right;
1381 else
1382 return cic;
1383 }
1384
1385 return NULL;
1386}
1387
1388static inline void
1389cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1390 struct cfq_io_context *cic)
1391{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001392 struct rb_node **p;
1393 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001394 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001395 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001396
Jens Axboee2d74ac2006-03-28 08:59:01 +02001397 cic->ioc = ioc;
1398 cic->key = cfqd;
1399
1400 ioc->set_ioprio = cfq_ioc_set_ioprio;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001401restart:
1402 parent = NULL;
1403 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001404 while (*p) {
1405 parent = *p;
1406 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001407 /* ->key must be copied to avoid race with cfq_exit_queue() */
1408 k = __cic->key;
1409 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001410 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001411 goto restart;
1412 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001413
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001414 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001415 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001416 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001417 p = &(*p)->rb_right;
1418 else
1419 BUG();
1420 }
1421
Jens Axboe3793c652006-05-30 21:11:04 +02001422 spin_lock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001423 rb_link_node(&cic->rb_node, parent, p);
1424 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1425 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe3793c652006-05-30 21:11:04 +02001426 spin_unlock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001427}
1428
Jens Axboe22e2c502005-06-27 10:55:12 +02001429/*
1430 * Setup general io context and cfq io context. There can be several cfq
1431 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001432 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001433 */
1434static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001435cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Jens Axboe22e2c502005-06-27 10:55:12 +02001437 struct io_context *ioc = NULL;
1438 struct cfq_io_context *cic;
1439
1440 might_sleep_if(gfp_mask & __GFP_WAIT);
1441
1442 ioc = get_io_context(gfp_mask);
1443 if (!ioc)
1444 return NULL;
1445
Jens Axboee2d74ac2006-03-28 08:59:01 +02001446 cic = cfq_cic_rb_lookup(cfqd, ioc);
1447 if (cic)
1448 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001449
Jens Axboee2d74ac2006-03-28 08:59:01 +02001450 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1451 if (cic == NULL)
1452 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001453
Jens Axboee2d74ac2006-03-28 08:59:01 +02001454 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001455out:
1456 return cic;
1457err:
1458 put_io_context(ioc);
1459 return NULL;
1460}
1461
1462static void
1463cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1464{
1465 unsigned long elapsed, ttime;
1466
1467 /*
1468 * if this context already has stuff queued, thinktime is from
1469 * last queue not last end
1470 */
1471#if 0
1472 if (time_after(cic->last_end_request, cic->last_queue))
1473 elapsed = jiffies - cic->last_end_request;
1474 else
1475 elapsed = jiffies - cic->last_queue;
1476#else
1477 elapsed = jiffies - cic->last_end_request;
1478#endif
1479
1480 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1481
1482 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1483 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1484 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1485}
1486
Jens Axboe206dc692006-03-28 13:03:44 +02001487static void
1488cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1489 struct cfq_rq *crq)
1490{
1491 sector_t sdist;
1492 u64 total;
1493
1494 if (cic->last_request_pos < crq->request->sector)
1495 sdist = crq->request->sector - cic->last_request_pos;
1496 else
1497 sdist = cic->last_request_pos - crq->request->sector;
1498
1499 /*
1500 * Don't allow the seek distance to get too large from the
1501 * odd fragment, pagein, etc
1502 */
1503 if (cic->seek_samples <= 60) /* second&third seek */
1504 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1505 else
1506 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1507
1508 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1509 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1510 total = cic->seek_total + (cic->seek_samples/2);
1511 do_div(total, cic->seek_samples);
1512 cic->seek_mean = (sector_t)total;
1513}
Jens Axboe22e2c502005-06-27 10:55:12 +02001514
1515/*
1516 * Disable idle window if the process thinks too long or seeks so much that
1517 * it doesn't matter
1518 */
1519static void
1520cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1521 struct cfq_io_context *cic)
1522{
Jens Axboe3b181522005-06-27 10:56:24 +02001523 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001524
Jens Axboecaaa5f92006-06-16 11:23:00 +02001525 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1526 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001527 enable_idle = 0;
1528 else if (sample_valid(cic->ttime_samples)) {
1529 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1530 enable_idle = 0;
1531 else
1532 enable_idle = 1;
1533 }
1534
Jens Axboe3b181522005-06-27 10:56:24 +02001535 if (enable_idle)
1536 cfq_mark_cfqq_idle_window(cfqq);
1537 else
1538 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001539}
1540
1541
1542/*
1543 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1544 * no or if we aren't sure, a 1 will cause a preempt.
1545 */
1546static int
1547cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1548 struct cfq_rq *crq)
1549{
1550 struct cfq_queue *cfqq = cfqd->active_queue;
1551
1552 if (cfq_class_idle(new_cfqq))
1553 return 0;
1554
1555 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001556 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001557
1558 if (cfq_class_idle(cfqq))
1559 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001560 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001561 return 0;
1562 /*
1563 * if it doesn't have slice left, forget it
1564 */
1565 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1566 return 0;
Jens Axboe5380a102006-07-13 12:37:56 +02001567 if (rq_is_sync(crq->request) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001568 return 1;
1569
1570 return 0;
1571}
1572
1573/*
1574 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1575 * let it have half of its nominal slice.
1576 */
1577static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1578{
1579 struct cfq_queue *__cfqq, *next;
1580
1581 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1582 cfq_resort_rr_list(__cfqq, 1);
1583
1584 if (!cfqq->slice_left)
1585 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1586
1587 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001588 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001589 __cfq_set_active_queue(cfqd, cfqq);
1590}
1591
1592/*
1593 * should really be a ll_rw_blk.c helper
1594 */
1595static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1596{
1597 request_queue_t *q = cfqd->queue;
1598
1599 if (!blk_queue_plugged(q))
1600 q->request_fn(q);
1601 else
1602 __generic_unplug_device(q);
1603}
1604
1605/*
1606 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1607 * something we should do about it
1608 */
1609static void
1610cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1611 struct cfq_rq *crq)
1612{
Jens Axboefd61af02006-06-16 15:35:39 +02001613 struct cfq_io_context *cic = crq->io_context;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001614
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001615 /*
Jens Axboe5380a102006-07-13 12:37:56 +02001616 * check if this request is a better next-serve candidate)) {
Jens Axboe21183b02006-07-13 12:33:14 +02001617 */
1618 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1619 BUG_ON(!cfqq->next_crq);
1620
1621 /*
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001622 * we never wait for an async request and we don't allow preemption
1623 * of an async request. so just return early
1624 */
Jens Axboe5380a102006-07-13 12:37:56 +02001625 if (!rq_is_sync(crq->request)) {
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001626 /*
1627 * sync process issued an async request, if it's waiting
1628 * then expire it and kick rq handling.
1629 */
1630 if (cic == cfqd->active_cic &&
1631 del_timer(&cfqd->idle_slice_timer)) {
1632 cfq_slice_expired(cfqd, 0);
1633 cfq_start_queueing(cfqd, cfqq);
1634 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001635 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001636 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001637
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001638 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe206dc692006-03-28 13:03:44 +02001639 cfq_update_io_seektime(cfqd, cic, crq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001640 cfq_update_idle_window(cfqd, cfqq, cic);
1641
1642 cic->last_queue = jiffies;
Jens Axboe206dc692006-03-28 13:03:44 +02001643 cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001644
1645 if (cfqq == cfqd->active_queue) {
1646 /*
1647 * if we are waiting for a request for this queue, let it rip
1648 * immediately and flag that we must not expire this queue
1649 * just now
1650 */
Jens Axboe3b181522005-06-27 10:56:24 +02001651 if (cfq_cfqq_wait_request(cfqq)) {
1652 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001653 del_timer(&cfqd->idle_slice_timer);
1654 cfq_start_queueing(cfqd, cfqq);
1655 }
1656 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1657 /*
1658 * not the active queue - expire current slice if it is
1659 * idle and has expired it's mean thinktime or this new queue
1660 * has some old slice time left and is of higher priority
1661 */
1662 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001663 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001664 cfq_start_queueing(cfqd, cfqq);
1665 }
1666}
1667
Jens Axboeb4878f22005-10-20 16:42:29 +02001668static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001669{
Jens Axboeb4878f22005-10-20 16:42:29 +02001670 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001671 struct cfq_rq *crq = RQ_DATA(rq);
1672 struct cfq_queue *cfqq = crq->cfq_queue;
1673
1674 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Jens Axboe21183b02006-07-13 12:33:14 +02001678 if (!cfq_cfqq_on_rr(cfqq))
1679 cfq_add_cfqq_rr(cfqd, cfqq);
1680
Jens Axboe22e2c502005-06-27 10:55:12 +02001681 list_add_tail(&rq->queuelist, &cfqq->fifo);
1682
Jens Axboe22e2c502005-06-27 10:55:12 +02001683 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686static void cfq_completed_request(request_queue_t *q, struct request *rq)
1687{
1688 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001689 struct cfq_queue *cfqq = crq->cfq_queue;
1690 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001691 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001692 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Jens Axboeb4878f22005-10-20 16:42:29 +02001694 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jens Axboeb4878f22005-10-20 16:42:29 +02001696 WARN_ON(!cfqd->rq_in_driver);
1697 WARN_ON(!cfqq->on_dispatch[sync]);
1698 cfqd->rq_in_driver--;
1699 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Jens Axboeb4878f22005-10-20 16:42:29 +02001701 if (!cfq_class_idle(cfqq))
1702 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001703
Jens Axboeb4878f22005-10-20 16:42:29 +02001704 if (!cfq_cfqq_dispatched(cfqq)) {
1705 if (cfq_cfqq_on_rr(cfqq)) {
1706 cfqq->service_last = now;
1707 cfq_resort_rr_list(cfqq, 0);
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Jens Axboecaaa5f92006-06-16 11:23:00 +02001711 if (sync)
Jens Axboeb4878f22005-10-20 16:42:29 +02001712 crq->io_context->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001713
1714 /*
1715 * If this is the active queue, check if it needs to be expired,
1716 * or if we want to idle in case it has no pending requests.
1717 */
1718 if (cfqd->active_queue == cfqq) {
1719 if (time_after(now, cfqq->slice_end))
1720 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001721 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001722 if (!cfq_arm_slice_timer(cfqd, cfqq))
1723 cfq_schedule_dispatch(cfqd);
1724 }
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Jens Axboe22e2c502005-06-27 10:55:12 +02001728/*
1729 * we temporarily boost lower priority queues if they are holding fs exclusive
1730 * resources. they are boosted to normal prio (CLASS_BE/4)
1731 */
1732static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Jens Axboe22e2c502005-06-27 10:55:12 +02001734 const int ioprio_class = cfqq->ioprio_class;
1735 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Jens Axboe22e2c502005-06-27 10:55:12 +02001737 if (has_fs_excl()) {
1738 /*
1739 * boost idle prio on transactions that would lock out other
1740 * users of the filesystem
1741 */
1742 if (cfq_class_idle(cfqq))
1743 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1744 if (cfqq->ioprio > IOPRIO_NORM)
1745 cfqq->ioprio = IOPRIO_NORM;
1746 } else {
1747 /*
1748 * check if we need to unboost the queue
1749 */
1750 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1751 cfqq->ioprio_class = cfqq->org_ioprio_class;
1752 if (cfqq->ioprio != cfqq->org_ioprio)
1753 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Jens Axboe22e2c502005-06-27 10:55:12 +02001756 /*
1757 * refile between round-robin lists if we moved the priority class
1758 */
1759 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001760 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001761 cfq_resort_rr_list(cfqq, 0);
1762}
1763
Jens Axboe22e2c502005-06-27 10:55:12 +02001764static inline int
1765__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1766 struct task_struct *task, int rw)
1767{
Jens Axboe3b181522005-06-27 10:56:24 +02001768 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001769 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001770 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001771 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001772 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001773
1774 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001775}
1776
1777static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1778{
1779 struct cfq_data *cfqd = q->elevator->elevator_data;
1780 struct task_struct *tsk = current;
1781 struct cfq_queue *cfqq;
1782
1783 /*
1784 * don't force setup of a queue from here, as a call to may_queue
1785 * does not necessarily imply that a request actually will be queued.
1786 * so just lookup a possibly existing queue, or return 'may queue'
1787 * if that fails
1788 */
Jens Axboe3b181522005-06-27 10:56:24 +02001789 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001790 if (cfqq) {
1791 cfq_init_prio_data(cfqq);
1792 cfq_prio_boost(cfqq);
1793
1794 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1795 }
1796
1797 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1801{
Jens Axboe22e2c502005-06-27 10:55:12 +02001802 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jens Axboe271f18f2006-06-13 08:08:38 +02001804 if (unlikely(cfqd->rq_starved)) {
1805 struct request_list *rl = &q->rq;
1806
Jens Axboe22e2c502005-06-27 10:55:12 +02001807 smp_mb();
1808 if (waitqueue_active(&rl->wait[READ]))
1809 wake_up(&rl->wait[READ]);
Jens Axboe22e2c502005-06-27 10:55:12 +02001810 if (waitqueue_active(&rl->wait[WRITE]))
1811 wake_up(&rl->wait[WRITE]);
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816 * queue lock held here
1817 */
1818static void cfq_put_request(request_queue_t *q, struct request *rq)
1819{
1820 struct cfq_data *cfqd = q->elevator->elevator_data;
1821 struct cfq_rq *crq = RQ_DATA(rq);
1822
1823 if (crq) {
1824 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001825 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jens Axboe22e2c502005-06-27 10:55:12 +02001827 BUG_ON(!cfqq->allocated[rw]);
1828 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jens Axboe22e2c502005-06-27 10:55:12 +02001830 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 mempool_free(crq, cfqd->crq_pool);
1833 rq->elevator_private = NULL;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 cfq_check_waiters(q, cfqq);
1836 cfq_put_queue(cfqq);
1837 }
1838}
1839
1840/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001841 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001843static int
1844cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001845 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001848 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct cfq_io_context *cic;
1850 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001851 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001852 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct cfq_rq *crq;
1854 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001855 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 might_sleep_if(gfp_mask & __GFP_WAIT);
1858
Jens Axboee2d74ac2006-03-28 08:59:01 +02001859 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 spin_lock_irqsave(q->queue_lock, flags);
1862
Jens Axboe22e2c502005-06-27 10:55:12 +02001863 if (!cic)
1864 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Al Viro12a05732006-03-18 13:38:01 -05001866 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001867 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001868 if (!cfqq)
1869 goto queue_fail;
1870
Al Viro12a05732006-03-18 13:38:01 -05001871 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001872 } else
Al Viro12a05732006-03-18 13:38:01 -05001873 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001876 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001877 cfqd->rq_starved = 0;
1878 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 spin_unlock_irqrestore(q->queue_lock, flags);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1882 if (crq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 crq->request = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 crq->cfq_queue = cfqq;
1885 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889 }
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 spin_lock_irqsave(q->queue_lock, flags);
1892 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02001893 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02001894 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001896queue_fail:
1897 if (cic)
1898 put_io_context(cic->ioc);
1899 /*
1900 * mark us rq allocation starved. we need to kickstart the process
1901 * ourselves if there are no pending requests that can do it for us.
1902 * that would be an extremely rare OOM situation
1903 */
1904 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001905 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 spin_unlock_irqrestore(q->queue_lock, flags);
1907 return 1;
1908}
1909
Jens Axboe22e2c502005-06-27 10:55:12 +02001910static void cfq_kick_queue(void *data)
1911{
1912 request_queue_t *q = data;
1913 struct cfq_data *cfqd = q->elevator->elevator_data;
1914 unsigned long flags;
1915
1916 spin_lock_irqsave(q->queue_lock, flags);
1917
1918 if (cfqd->rq_starved) {
1919 struct request_list *rl = &q->rq;
1920
1921 /*
1922 * we aren't guaranteed to get a request after this, but we
1923 * have to be opportunistic
1924 */
1925 smp_mb();
1926 if (waitqueue_active(&rl->wait[READ]))
1927 wake_up(&rl->wait[READ]);
1928 if (waitqueue_active(&rl->wait[WRITE]))
1929 wake_up(&rl->wait[WRITE]);
1930 }
1931
1932 blk_remove_plug(q);
1933 q->request_fn(q);
1934 spin_unlock_irqrestore(q->queue_lock, flags);
1935}
1936
1937/*
1938 * Timer running if the active_queue is currently idling inside its time slice
1939 */
1940static void cfq_idle_slice_timer(unsigned long data)
1941{
1942 struct cfq_data *cfqd = (struct cfq_data *) data;
1943 struct cfq_queue *cfqq;
1944 unsigned long flags;
1945
1946 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1947
1948 if ((cfqq = cfqd->active_queue) != NULL) {
1949 unsigned long now = jiffies;
1950
1951 /*
1952 * expired
1953 */
1954 if (time_after(now, cfqq->slice_end))
1955 goto expire;
1956
1957 /*
1958 * only expire and reinvoke request handler, if there are
1959 * other queues with pending requests
1960 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001961 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001962 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001963
1964 /*
1965 * not expired and it has a request pending, let it dispatch
1966 */
Jens Axboedd67d052006-06-21 09:36:18 +02001967 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001968 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001969 goto out_kick;
1970 }
1971 }
1972expire:
1973 cfq_slice_expired(cfqd, 0);
1974out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001975 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001976out_cont:
1977 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1978}
1979
1980/*
1981 * Timer running if an idle class queue is waiting for service
1982 */
1983static void cfq_idle_class_timer(unsigned long data)
1984{
1985 struct cfq_data *cfqd = (struct cfq_data *) data;
1986 unsigned long flags, end;
1987
1988 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1989
1990 /*
1991 * race with a non-idle queue, reset timer
1992 */
1993 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001994 if (!time_after_eq(jiffies, end))
1995 mod_timer(&cfqd->idle_class_timer, end);
1996 else
Jens Axboe3b181522005-06-27 10:56:24 +02001997 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001998
1999 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2000}
2001
Jens Axboe3b181522005-06-27 10:56:24 +02002002static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2003{
2004 del_timer_sync(&cfqd->idle_slice_timer);
2005 del_timer_sync(&cfqd->idle_class_timer);
2006 blk_sync_queue(cfqd->queue);
2007}
Jens Axboe22e2c502005-06-27 10:55:12 +02002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static void cfq_exit_queue(elevator_t *e)
2010{
Jens Axboe22e2c502005-06-27 10:55:12 +02002011 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002012 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002013
Jens Axboe3b181522005-06-27 10:56:24 +02002014 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002015
Jens Axboe3793c652006-05-30 21:11:04 +02002016 spin_lock(&cfq_exit_lock);
Al Virod9ff4182006-03-18 13:51:22 -05002017 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002018
Al Virod9ff4182006-03-18 13:51:22 -05002019 if (cfqd->active_queue)
2020 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002021
2022 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002023 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2024 struct cfq_io_context,
2025 queue_list);
2026 if (cic->cfqq[ASYNC]) {
2027 cfq_put_queue(cic->cfqq[ASYNC]);
2028 cic->cfqq[ASYNC] = NULL;
2029 }
2030 if (cic->cfqq[SYNC]) {
2031 cfq_put_queue(cic->cfqq[SYNC]);
2032 cic->cfqq[SYNC] = NULL;
2033 }
2034 cic->key = NULL;
2035 list_del_init(&cic->queue_list);
2036 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002037
Al Virod9ff4182006-03-18 13:51:22 -05002038 spin_unlock_irq(q->queue_lock);
Jens Axboe3793c652006-05-30 21:11:04 +02002039 spin_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002040
2041 cfq_shutdown_timer_wq(cfqd);
2042
2043 mempool_destroy(cfqd->crq_pool);
Al Viroa90d7422006-03-18 12:05:37 -05002044 kfree(cfqd->cfq_hash);
2045 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Jens Axboebc1c1162006-06-08 08:49:06 +02002048static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct cfq_data *cfqd;
2051 int i;
2052
2053 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2054 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002055 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002058
2059 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2060 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2061
2062 INIT_LIST_HEAD(&cfqd->busy_rr);
2063 INIT_LIST_HEAD(&cfqd->cur_rr);
2064 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002066 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2069 if (!cfqd->cfq_hash)
Jens Axboe98170642006-07-28 09:23:08 +02002070 goto out_crqhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Matthew Dobson93d23412006-03-26 01:37:50 -08002072 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!cfqd->crq_pool)
2074 goto out_crqpool;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2077 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Jens Axboe22e2c502005-06-27 10:55:12 +02002081 init_timer(&cfqd->idle_slice_timer);
2082 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2083 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2084
2085 init_timer(&cfqd->idle_class_timer);
2086 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2087 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2088
2089 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 cfqd->cfq_queued = cfq_queued;
2092 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002093 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2094 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 cfqd->cfq_back_max = cfq_back_max;
2096 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002097 cfqd->cfq_slice[0] = cfq_slice_async;
2098 cfqd->cfq_slice[1] = cfq_slice_sync;
2099 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2100 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002101
Jens Axboebc1c1162006-06-08 08:49:06 +02002102 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103out_crqpool:
2104 kfree(cfqd->cfq_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105out_crqhash:
2106 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110static void cfq_slab_kill(void)
2111{
2112 if (crq_pool)
2113 kmem_cache_destroy(crq_pool);
2114 if (cfq_pool)
2115 kmem_cache_destroy(cfq_pool);
2116 if (cfq_ioc_pool)
2117 kmem_cache_destroy(cfq_ioc_pool);
2118}
2119
2120static int __init cfq_slab_setup(void)
2121{
2122 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2123 NULL, NULL);
2124 if (!crq_pool)
2125 goto fail;
2126
2127 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2128 NULL, NULL);
2129 if (!cfq_pool)
2130 goto fail;
2131
2132 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2133 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2134 if (!cfq_ioc_pool)
2135 goto fail;
2136
2137 return 0;
2138fail:
2139 cfq_slab_kill();
2140 return -ENOMEM;
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/*
2144 * sysfs parts below -->
2145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147static ssize_t
2148cfq_var_show(unsigned int var, char *page)
2149{
2150 return sprintf(page, "%d\n", var);
2151}
2152
2153static ssize_t
2154cfq_var_store(unsigned int *var, const char *page, size_t count)
2155{
2156 char *p = (char *) page;
2157
2158 *var = simple_strtoul(p, &p, 10);
2159 return count;
2160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002163static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002165 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 unsigned int __data = __VAR; \
2167 if (__CONV) \
2168 __data = jiffies_to_msecs(__data); \
2169 return cfq_var_show(__data, (page)); \
2170}
2171SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2172SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002173SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2174SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002175SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2176SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002177SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2178SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2179SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2180SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181#undef SHOW_FUNCTION
2182
2183#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002184static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002186 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 unsigned int __data; \
2188 int ret = cfq_var_store(&__data, (page), count); \
2189 if (__data < (MIN)) \
2190 __data = (MIN); \
2191 else if (__data > (MAX)) \
2192 __data = (MAX); \
2193 if (__CONV) \
2194 *(__PTR) = msecs_to_jiffies(__data); \
2195 else \
2196 *(__PTR) = __data; \
2197 return ret; \
2198}
2199STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2200STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002201STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2202STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002203STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2204STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002205STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2206STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2207STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2208STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#undef STORE_FUNCTION
2210
Al Viroe572ec72006-03-18 22:27:18 -05002211#define CFQ_ATTR(name) \
2212 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002213
Al Viroe572ec72006-03-18 22:27:18 -05002214static struct elv_fs_entry cfq_attrs[] = {
2215 CFQ_ATTR(quantum),
2216 CFQ_ATTR(queued),
2217 CFQ_ATTR(fifo_expire_sync),
2218 CFQ_ATTR(fifo_expire_async),
2219 CFQ_ATTR(back_seek_max),
2220 CFQ_ATTR(back_seek_penalty),
2221 CFQ_ATTR(slice_sync),
2222 CFQ_ATTR(slice_async),
2223 CFQ_ATTR(slice_async_rq),
2224 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002225 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226};
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228static struct elevator_type iosched_cfq = {
2229 .ops = {
2230 .elevator_merge_fn = cfq_merge,
2231 .elevator_merged_fn = cfq_merged_request,
2232 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002233 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002235 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 .elevator_deactivate_req_fn = cfq_deactivate_request,
2237 .elevator_queue_empty_fn = cfq_queue_empty,
2238 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002239 .elevator_former_req_fn = elv_rb_former_request,
2240 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 .elevator_set_req_fn = cfq_set_request,
2242 .elevator_put_req_fn = cfq_put_request,
2243 .elevator_may_queue_fn = cfq_may_queue,
2244 .elevator_init_fn = cfq_init_queue,
2245 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002246 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 },
Al Viro3d1ab402006-03-18 18:35:43 -05002248 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 .elevator_name = "cfq",
2250 .elevator_owner = THIS_MODULE,
2251};
2252
2253static int __init cfq_init(void)
2254{
2255 int ret;
2256
Jens Axboe22e2c502005-06-27 10:55:12 +02002257 /*
2258 * could be 0 on HZ < 1000 setups
2259 */
2260 if (!cfq_slice_async)
2261 cfq_slice_async = 1;
2262 if (!cfq_slice_idle)
2263 cfq_slice_idle = 1;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (cfq_slab_setup())
2266 return -ENOMEM;
2267
2268 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002269 if (ret)
2270 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return ret;
2273}
2274
2275static void __exit cfq_exit(void)
2276{
Al Viro334e94d2006-03-18 15:05:53 -05002277 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002279 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002280 /* ioc_gone's update must be visible before reading ioc_count */
2281 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05002282 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002283 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002284 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002285 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288module_init(cfq_init);
2289module_exit(cfq_exit);
2290
2291MODULE_AUTHOR("Jens Axboe");
2292MODULE_LICENSE("GPL");
2293MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");