blob: 3c5fd9c2c205af258d49d167bfab1457fd420034 [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
Jens Axboe5e705372006-07-13 12:39:25 +020046#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
47#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static 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
Jens Axboe22e2c502005-06-27 10:55:12 +020098 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020099 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200100
101 /*
102 * schedule slice state info
103 */
104 /*
105 * idle window management
106 */
107 struct timer_list idle_slice_timer;
108 struct work_struct unplug_work;
109
110 struct cfq_queue *active_queue;
111 struct cfq_io_context *active_cic;
112 int cur_prio, cur_end_prio;
113 unsigned int dispatch_slice;
114
115 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200118 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jens Axboe22e2c502005-06-27 10:55:12 +0200120 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /*
123 * tunables, see top of file
124 */
125 unsigned int cfq_quantum;
126 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200127 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned int cfq_back_penalty;
129 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200130 unsigned int cfq_slice[2];
131 unsigned int cfq_slice_async_rq;
132 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500133
134 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Jens Axboe22e2c502005-06-27 10:55:12 +0200137/*
138 * Per process-grouping structure
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct cfq_queue {
141 /* reference count */
142 atomic_t ref;
143 /* parent cfq_data */
144 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct hlist_node cfq_hash;
147 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* on either rr or empty list of cfqd */
150 struct list_head cfq_list;
151 /* sorted list of pending requests */
152 struct rb_root sort_list;
153 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200154 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* requests queued in sort_list */
156 int queued[2];
157 /* currently allocated requests */
158 int allocated[2];
159 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200160 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jens Axboe22e2c502005-06-27 10:55:12 +0200162 unsigned long slice_start;
163 unsigned long slice_end;
164 unsigned long slice_left;
165 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jens Axboe3b181522005-06-27 10:56:24 +0200167 /* number of requests that are on the dispatch list */
168 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200169
170 /* io prio of this group */
171 unsigned short ioprio, org_ioprio;
172 unsigned short ioprio_class, org_ioprio_class;
173
Jens Axboe3b181522005-06-27 10:56:24 +0200174 /* various state flags, see below */
175 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Jens Axboe3b181522005-06-27 10:56:24 +0200178enum cfqq_state_flags {
179 CFQ_CFQQ_FLAG_on_rr = 0,
180 CFQ_CFQQ_FLAG_wait_request,
181 CFQ_CFQQ_FLAG_must_alloc,
182 CFQ_CFQQ_FLAG_must_alloc_slice,
183 CFQ_CFQQ_FLAG_must_dispatch,
184 CFQ_CFQQ_FLAG_fifo_expire,
185 CFQ_CFQQ_FLAG_idle_window,
186 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200187};
188
189#define CFQ_CFQQ_FNS(name) \
190static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
191{ \
192 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
193} \
194static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
195{ \
196 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
197} \
198static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
199{ \
200 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
201}
202
203CFQ_CFQQ_FNS(on_rr);
204CFQ_CFQQ_FNS(wait_request);
205CFQ_CFQQ_FNS(must_alloc);
206CFQ_CFQQ_FNS(must_alloc_slice);
207CFQ_CFQQ_FNS(must_dispatch);
208CFQ_CFQQ_FNS(fifo_expire);
209CFQ_CFQQ_FNS(idle_window);
210CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200211#undef CFQ_CFQQ_FNS
212
Jens Axboe3b181522005-06-27 10:56:24 +0200213static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200214static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500215static 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 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700218 * scheduler run of queue, if there are requests pending and no one in the
219 * driver that will restart queueing
220 */
221static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
222{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100223 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700224 kblockd_schedule_work(&cfqd->unplug_work);
225}
226
227static int cfq_queue_empty(request_queue_t *q)
228{
229 struct cfq_data *cfqd = q->elevator->elevator_data;
230
Jens Axboeb4878f22005-10-20 16:42:29 +0200231 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700232}
233
Jens Axboe206dc692006-03-28 13:03:44 +0200234static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
235{
Jens Axboeb31dc662006-06-13 08:26:10 +0200236 if (rw == READ || rw == WRITE_SYNC)
Jens Axboe206dc692006-03-28 13:03:44 +0200237 return task->pid;
238
239 return CFQ_KEY_ASYNC;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*
Jens Axboe5e705372006-07-13 12:39:25 +0200243 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200245 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Jens Axboe5e705372006-07-13 12:39:25 +0200247static struct request *
248cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200252#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
253#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
254 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jens Axboe5e705372006-07-13 12:39:25 +0200256 if (rq1 == NULL || rq1 == rq2)
257 return rq2;
258 if (rq2 == NULL)
259 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200260
Jens Axboe5e705372006-07-13 12:39:25 +0200261 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
262 return rq1;
263 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
264 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jens Axboe5e705372006-07-13 12:39:25 +0200266 s1 = rq1->sector;
267 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 last = cfqd->last_sector;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * by definition, 1KiB is 2 sectors
273 */
274 back_max = cfqd->cfq_back_max * 2;
275
276 /*
277 * Strict one way elevator _except_ in the case where we allow
278 * short backward seeks which are biased as twice the cost of a
279 * similar forward seek.
280 */
281 if (s1 >= last)
282 d1 = s1 - last;
283 else if (s1 + back_max >= last)
284 d1 = (last - s1) * cfqd->cfq_back_penalty;
285 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200286 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (s2 >= last)
289 d2 = s2 - last;
290 else if (s2 + back_max >= last)
291 d2 = (last - s2) * cfqd->cfq_back_penalty;
292 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200293 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Andreas Mohre8a99052006-03-28 08:59:49 +0200297 /*
298 * By doing switch() on the bit mask "wrap" we avoid having to
299 * check two variables for all permutations: --> faster!
300 */
301 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200302 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200303 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200304 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200305 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200306 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200307 else {
308 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200309 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200310 else
Jens Axboe5e705372006-07-13 12:39:25 +0200311 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200312 }
313
314 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200315 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200316 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200317 return rq2;
318 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200319 default:
320 /*
321 * Since both rqs are wrapped,
322 * start with the one that's further behind head
323 * (--> only *one* back seek required),
324 * since back seek takes more time than forward.
325 */
326 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200327 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 else
Jens Axboe5e705372006-07-13 12:39:25 +0200329 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331}
332
333/*
334 * would be nice to take fifo expire time into account as well
335 */
Jens Axboe5e705372006-07-13 12:39:25 +0200336static struct request *
337cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
338 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Jens Axboe21183b02006-07-13 12:33:14 +0200340 struct rb_node *rbnext = rb_next(&last->rb_node);
341 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200342 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jens Axboe21183b02006-07-13 12:33:14 +0200344 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200347 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200350 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200351 else {
352 rbnext = rb_first(&cfqq->sort_list);
353 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200354 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jens Axboe21183b02006-07-13 12:33:14 +0200357 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Jens Axboe22e2c502005-06-27 10:55:12 +0200360static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Jens Axboe22e2c502005-06-27 10:55:12 +0200362 struct cfq_data *cfqd = cfqq->cfqd;
363 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jens Axboe3b181522005-06-27 10:56:24 +0200365 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 list_del(&cfqq->cfq_list);
368
Jens Axboe22e2c502005-06-27 10:55:12 +0200369 if (cfq_class_rt(cfqq))
370 list = &cfqd->cur_rr;
371 else if (cfq_class_idle(cfqq))
372 list = &cfqd->idle_rr;
373 else {
374 /*
375 * if cfqq has requests in flight, don't allow it to be
376 * found in cfq_set_active_queue before it has finished them.
377 * this is done to increase fairness between a process that
378 * has lots of io pending vs one that only generates one
379 * sporadically or synchronously
380 */
Jens Axboe3b181522005-06-27 10:56:24 +0200381 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200382 list = &cfqd->busy_rr;
383 else
384 list = &cfqd->rr_list[cfqq->ioprio];
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200388 * if queue was preempted, just add to front to be fair. busy_rr
Jens Axboeb52a8342006-06-01 18:53:43 +0200389 * isn't sorted, but insert at the back for fairness.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200391 if (preempted || list == &cfqd->busy_rr) {
Jens Axboeb52a8342006-06-01 18:53:43 +0200392 if (preempted)
393 list = list->prev;
394
395 list_add_tail(&cfqq->cfq_list, list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200396 return;
397 }
398
399 /*
400 * sort by when queue was last serviced
401 */
402 entry = list;
403 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
405
Jens Axboe22e2c502005-06-27 10:55:12 +0200406 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200408 if (time_before(__cfqq->service_last, cfqq->service_last))
409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 list_add(&cfqq->cfq_list, entry);
413}
414
415/*
416 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200417 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200420cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jens Axboe3b181522005-06-27 10:56:24 +0200422 BUG_ON(cfq_cfqq_on_rr(cfqq));
423 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 cfqd->busy_queues++;
425
Jens Axboeb4878f22005-10-20 16:42:29 +0200426 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429static inline void
430cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
431{
Jens Axboe3b181522005-06-27 10:56:24 +0200432 BUG_ON(!cfq_cfqq_on_rr(cfqq));
433 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200434 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 BUG_ON(!cfqd->busy_queues);
437 cfqd->busy_queues--;
438}
439
440/*
441 * rb tree support functions
442 */
Jens Axboe5e705372006-07-13 12:39:25 +0200443static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Jens Axboe5e705372006-07-13 12:39:25 +0200445 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200446 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200447 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jens Axboeb4878f22005-10-20 16:42:29 +0200449 BUG_ON(!cfqq->queued[sync]);
450 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jens Axboe5e705372006-07-13 12:39:25 +0200452 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Jens Axboedd67d052006-06-21 09:36:18 +0200454 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200455 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Jens Axboe5e705372006-07-13 12:39:25 +0200458static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Jens Axboe5e705372006-07-13 12:39:25 +0200460 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200462 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Jens Axboe5380a102006-07-13 12:37:56 +0200464 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * looks a little odd, but the first insert might return an alias.
468 * if that happens, put the alias on the dispatch list
469 */
Jens Axboe21183b02006-07-13 12:33:14 +0200470 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200471 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200475cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jens Axboe5380a102006-07-13 12:37:56 +0200477 elv_rb_del(&cfqq->sort_list, rq);
478 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200479 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Jens Axboe206dc692006-03-28 13:03:44 +0200482static struct request *
483cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Jens Axboe206dc692006-03-28 13:03:44 +0200485 struct task_struct *tsk = current;
486 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
Jens Axboe21183b02006-07-13 12:33:14 +0200487 sector_t sector = bio->bi_sector + bio_sectors(bio);
Jens Axboe206dc692006-03-28 13:03:44 +0200488 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Jens Axboe206dc692006-03-28 13:03:44 +0200490 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe21183b02006-07-13 12:33:14 +0200491 if (cfqq)
492 return elv_rb_find(&cfqq->sort_list, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return NULL;
495}
496
Jens Axboeb4878f22005-10-20 16:42:29 +0200497static void cfq_activate_request(request_queue_t *q, struct request *rq)
498{
499 struct cfq_data *cfqd = q->elevator->elevator_data;
500
501 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200502
503 /*
504 * If the depth is larger 1, it really could be queueing. But lets
505 * make the mark a little higher - idling could still be good for
506 * low queueing, and a low queueing number could also just indicate
507 * a SCSI mid layer like behaviour where limit+1 is often seen.
508 */
509 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
510 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
514{
Jens Axboe22e2c502005-06-27 10:55:12 +0200515 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jens Axboeb4878f22005-10-20 16:42:29 +0200517 WARN_ON(!cfqd->rq_in_driver);
518 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Jens Axboeb4878f22005-10-20 16:42:29 +0200521static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Jens Axboe5e705372006-07-13 12:39:25 +0200523 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200524
Jens Axboe5e705372006-07-13 12:39:25 +0200525 if (cfqq->next_rq == rq)
526 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jens Axboeb4878f22005-10-20 16:42:29 +0200528 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200529 cfq_del_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static int
533cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
534{
535 struct cfq_data *cfqd = q->elevator->elevator_data;
536 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jens Axboe206dc692006-03-28 13:03:44 +0200538 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200539 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200540 *req = __rq;
541 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Jens Axboe21183b02006-07-13 12:33:14 +0200547static void cfq_merged_request(request_queue_t *q, struct request *req,
548 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Jens Axboe21183b02006-07-13 12:33:14 +0200550 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200551 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jens Axboe5e705372006-07-13 12:39:25 +0200553 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static void
558cfq_merged_requests(request_queue_t *q, struct request *rq,
559 struct request *next)
560{
Jens Axboe22e2c502005-06-27 10:55:12 +0200561 /*
562 * reposition in fifo if next is older than rq
563 */
564 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
565 time_before(next->start_time, rq->start_time))
566 list_move(&rq->queuelist, &next->queuelist);
567
Jens Axboeb4878f22005-10-20 16:42:29 +0200568 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200569}
570
571static inline void
572__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
573{
574 if (cfqq) {
575 /*
576 * stop potential idle class queues waiting service
577 */
578 del_timer(&cfqd->idle_class_timer);
579
580 cfqq->slice_start = jiffies;
581 cfqq->slice_end = 0;
582 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200583 cfq_clear_cfqq_must_alloc_slice(cfqq);
584 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200585 }
586
587 cfqd->active_queue = cfqq;
588}
589
590/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100591 * current cfqq expired its slice (or was too idle), select new one
592 */
593static void
594__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
595 int preempted)
596{
597 unsigned long now = jiffies;
598
599 if (cfq_cfqq_wait_request(cfqq))
600 del_timer(&cfqd->idle_slice_timer);
601
602 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
603 cfqq->service_last = now;
604 cfq_schedule_dispatch(cfqd);
605 }
606
607 cfq_clear_cfqq_must_dispatch(cfqq);
608 cfq_clear_cfqq_wait_request(cfqq);
609
610 /*
611 * store what was left of this slice, if the queue idled out
612 * or was preempted
613 */
614 if (time_after(cfqq->slice_end, now))
615 cfqq->slice_left = cfqq->slice_end - now;
616 else
617 cfqq->slice_left = 0;
618
619 if (cfq_cfqq_on_rr(cfqq))
620 cfq_resort_rr_list(cfqq, preempted);
621
622 if (cfqq == cfqd->active_queue)
623 cfqd->active_queue = NULL;
624
625 if (cfqd->active_cic) {
626 put_io_context(cfqd->active_cic->ioc);
627 cfqd->active_cic = NULL;
628 }
629
630 cfqd->dispatch_slice = 0;
631}
632
633static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
634{
635 struct cfq_queue *cfqq = cfqd->active_queue;
636
637 if (cfqq)
638 __cfq_slice_expired(cfqd, cfqq, preempted);
639}
640
641/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200642 * 0
643 * 0,1
644 * 0,1,2
645 * 0,1,2,3
646 * 0,1,2,3,4
647 * 0,1,2,3,4,5
648 * 0,1,2,3,4,5,6
649 * 0,1,2,3,4,5,6,7
650 */
651static int cfq_get_next_prio_level(struct cfq_data *cfqd)
652{
653 int prio, wrap;
654
655 prio = -1;
656 wrap = 0;
657 do {
658 int p;
659
660 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
661 if (!list_empty(&cfqd->rr_list[p])) {
662 prio = p;
663 break;
664 }
665 }
666
667 if (prio != -1)
668 break;
669 cfqd->cur_prio = 0;
670 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
671 cfqd->cur_end_prio = 0;
672 if (wrap)
673 break;
674 wrap = 1;
675 }
676 } while (1);
677
678 if (unlikely(prio == -1))
679 return -1;
680
681 BUG_ON(prio >= CFQ_PRIO_LISTS);
682
683 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
684
685 cfqd->cur_prio = prio + 1;
686 if (cfqd->cur_prio > cfqd->cur_end_prio) {
687 cfqd->cur_end_prio = cfqd->cur_prio;
688 cfqd->cur_prio = 0;
689 }
690 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
691 cfqd->cur_prio = 0;
692 cfqd->cur_end_prio = 0;
693 }
694
695 return prio;
696}
697
Jens Axboe3b181522005-06-27 10:56:24 +0200698static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200699{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100700 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200701
702 /*
703 * if current list is non-empty, grab first entry. if it is empty,
704 * get next prio level and grab first entry then if any are spliced
705 */
706 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
707 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
708
709 /*
Jens Axboee0de0202006-06-01 10:07:26 +0200710 * If no new queues are available, check if the busy list has some
711 * before falling back to idle io.
712 */
713 if (!cfqq && !list_empty(&cfqd->busy_rr))
714 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
715
716 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200717 * if we have idle queues and no rt or be queues had pending
718 * requests, either allow immediate service if the grace period
719 * has passed or arm the idle grace timer
720 */
721 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
722 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
723
724 if (time_after_eq(jiffies, end))
725 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
726 else
727 mod_timer(&cfqd->idle_class_timer, end);
728 }
729
730 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200731 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200732}
733
Jens Axboecaaa5f92006-06-16 11:23:00 +0200734#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
735
Jens Axboe22e2c502005-06-27 10:55:12 +0200736static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
737
738{
Jens Axboe206dc692006-03-28 13:03:44 +0200739 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100740 unsigned long sl;
741
Jens Axboedd67d052006-06-21 09:36:18 +0200742 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200743 WARN_ON(cfqq != cfqd->active_queue);
744
745 /*
746 * idle is disabled, either manually or by past process history
747 */
748 if (!cfqd->cfq_slice_idle)
749 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200750 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200751 return 0;
752 /*
753 * task has exited, don't wait
754 */
Jens Axboe206dc692006-03-28 13:03:44 +0200755 cic = cfqd->active_cic;
756 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200757 return 0;
758
Jens Axboe3b181522005-06-27 10:56:24 +0200759 cfq_mark_cfqq_must_dispatch(cfqq);
760 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200761
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100762 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200763
764 /*
765 * we don't want to idle for seeks, but we do want to allow
766 * fair distribution of slice time for a process doing back-to-back
767 * seeks. so allow a little bit of time for him to submit a new rq
768 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200769 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200770 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200771
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100772 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200773 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Jens Axboe5e705372006-07-13 12:39:25 +0200776static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200779 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200780
Jens Axboe5380a102006-07-13 12:37:56 +0200781 cfq_remove_request(rq);
782 cfqq->on_dispatch[rq_is_sync(rq)]++;
783 elv_dispatch_sort(q, rq);
Jens Axboefd61af02006-06-16 15:35:39 +0200784
785 rq = list_entry(q->queue_head.prev, struct request, queuelist);
786 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/*
790 * return expired entry, or NULL to just start from scratch in rbtree
791 */
Jens Axboe5e705372006-07-13 12:39:25 +0200792static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200795 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Jens Axboe3b181522005-06-27 10:56:24 +0200797 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return NULL;
799
Jens Axboe22e2c502005-06-27 10:55:12 +0200800 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200801 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Jens Axboe5e705372006-07-13 12:39:25 +0200803 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200804 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200805 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe5e705372006-07-13 12:39:25 +0200806 return rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 return NULL;
811}
812
813/*
Jens Axboe3b181522005-06-27 10:56:24 +0200814 * Scale schedule slice based on io priority. Use the sync time slice only
815 * if a queue is marked sync and has sync io queued. A sync queue with async
816 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200818static inline int
819cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Jens Axboe22e2c502005-06-27 10:55:12 +0200821 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jens Axboe22e2c502005-06-27 10:55:12 +0200823 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jens Axboe22e2c502005-06-27 10:55:12 +0200825 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Jens Axboe22e2c502005-06-27 10:55:12 +0200828static inline void
829cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
830{
831 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
832}
833
834static inline int
835cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
836{
837 const int base_rq = cfqd->cfq_slice_async_rq;
838
839 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
840
841 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
842}
843
844/*
845 * get next queue for service
846 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100847static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200848{
849 unsigned long now = jiffies;
850 struct cfq_queue *cfqq;
851
852 cfqq = cfqd->active_queue;
853 if (!cfqq)
854 goto new_queue;
855
856 /*
857 * slice has expired
858 */
Jens Axboe3b181522005-06-27 10:56:24 +0200859 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
860 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200861
862 /*
863 * if queue has requests, dispatch one. if not, check if
864 * enough slice is left to wait for one
865 */
Jens Axboedd67d052006-06-21 09:36:18 +0200866 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200867 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200868 else if (cfq_cfqq_dispatched(cfqq)) {
869 cfqq = NULL;
870 goto keep_queue;
871 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200872 if (cfq_arm_slice_timer(cfqd, cfqq))
873 return NULL;
874 }
875
Jens Axboe3b181522005-06-27 10:56:24 +0200876expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200877 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200878new_queue:
879 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200880keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200881 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200882}
883
884static int
885__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
886 int max_dispatch)
887{
888 int dispatched = 0;
889
Jens Axboedd67d052006-06-21 09:36:18 +0200890 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200891
892 do {
Jens Axboe5e705372006-07-13 12:39:25 +0200893 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200894
895 /*
896 * follow expired path, else get first next available
897 */
Jens Axboe5e705372006-07-13 12:39:25 +0200898 if ((rq = cfq_check_fifo(cfqq)) == NULL)
899 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200900
901 /*
902 * finally, insert request into driver dispatch list
903 */
Jens Axboe5e705372006-07-13 12:39:25 +0200904 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200905
906 cfqd->dispatch_slice++;
907 dispatched++;
908
909 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +0200910 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
911 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200912 }
913
Jens Axboedd67d052006-06-21 09:36:18 +0200914 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200915 break;
916
917 } while (dispatched < max_dispatch);
918
919 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +0200920 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +0200921 */
922 if (!cfqq->slice_end)
923 cfq_set_prio_slice(cfqd, cfqq);
924
925 /*
926 * expire an async queue immediately if it has used up its slice. idle
927 * queue always expire after 1 dispatch round.
928 */
929 if ((!cfq_cfqq_sync(cfqq) &&
930 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +0200931 cfq_class_idle(cfqq) ||
932 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200933 cfq_slice_expired(cfqd, 0);
934
935 return dispatched;
936}
937
938static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100939cfq_forced_dispatch_cfqqs(struct list_head *list)
940{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100941 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200942 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100943
Jens Axboecaaa5f92006-06-16 11:23:00 +0200944 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100945 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +0200946 while (cfqq->next_rq) {
947 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100948 dispatched++;
949 }
950 BUG_ON(!list_empty(&cfqq->fifo));
951 }
Jens Axboecaaa5f92006-06-16 11:23:00 +0200952
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100953 return dispatched;
954}
955
956static int
957cfq_forced_dispatch(struct cfq_data *cfqd)
958{
959 int i, dispatched = 0;
960
961 for (i = 0; i < CFQ_PRIO_LISTS; i++)
962 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
963
964 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
965 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
966 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
967
968 cfq_slice_expired(cfqd, 0);
969
970 BUG_ON(cfqd->busy_queues);
971
972 return dispatched;
973}
974
975static int
Jens Axboeb4878f22005-10-20 16:42:29 +0200976cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200979 struct cfq_queue *cfqq, *prev_cfqq;
980 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jens Axboe22e2c502005-06-27 10:55:12 +0200982 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100985 if (unlikely(force))
986 return cfq_forced_dispatch(cfqd);
987
Jens Axboecaaa5f92006-06-16 11:23:00 +0200988 dispatched = 0;
989 prev_cfqq = NULL;
990 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +0200991 int max_dispatch;
992
Jens Axboecaaa5f92006-06-16 11:23:00 +0200993 /*
994 * Don't repeat dispatch from the previous queue.
995 */
996 if (prev_cfqq == cfqq)
997 break;
998
Jens Axboe3b181522005-06-27 10:56:24 +0200999 cfq_clear_cfqq_must_dispatch(cfqq);
1000 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001001 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001003 max_dispatch = cfqd->cfq_quantum;
1004 if (cfq_class_idle(cfqq))
1005 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Jens Axboecaaa5f92006-06-16 11:23:00 +02001007 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1008
1009 /*
1010 * If the dispatch cfqq has idling enabled and is still
1011 * the active queue, break out.
1012 */
1013 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1014 break;
1015
1016 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
Jens Axboecaaa5f92006-06-16 11:23:00 +02001019 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
Jens Axboe5e705372006-07-13 12:39:25 +02001023 * task holds one reference to the queue, dropped when task exits. each rq
1024 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 *
1026 * queue lock must be held here.
1027 */
1028static void cfq_put_queue(struct cfq_queue *cfqq)
1029{
Jens Axboe22e2c502005-06-27 10:55:12 +02001030 struct cfq_data *cfqd = cfqq->cfqd;
1031
1032 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (!atomic_dec_and_test(&cfqq->ref))
1035 return;
1036
1037 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001038 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001039 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001041 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001042 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * it's on the empty list and still hashed
1046 */
1047 list_del(&cfqq->cfq_list);
1048 hlist_del(&cfqq->cfq_hash);
1049 kmem_cache_free(cfq_pool, cfqq);
1050}
1051
1052static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001053__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1054 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001057 struct hlist_node *entry;
1058 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jens Axboe206dc692006-03-28 13:03:44 +02001060 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001061 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Jens Axboe206dc692006-03-28 13:03:44 +02001063 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return __cfqq;
1065 }
1066
1067 return NULL;
1068}
1069
1070static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001071cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Jens Axboe3b181522005-06-27 10:56:24 +02001073 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Jens Axboee2d74ac2006-03-28 08:59:01 +02001076static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Jens Axboe22e2c502005-06-27 10:55:12 +02001078 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001079 struct rb_node *n;
1080 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001081
Jens Axboee2d74ac2006-03-28 08:59:01 +02001082 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1083 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1084 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001085 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001086 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001087 }
1088
Al Viro334e94d2006-03-18 15:05:53 -05001089 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1090 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Al Viroe17a9482006-03-18 13:21:20 -05001093static void cfq_trim(struct io_context *ioc)
1094{
1095 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001096 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001100 * Called with interrupts disabled
1101 */
1102static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1103{
Al Viro478a82b2006-03-18 13:25:24 -05001104 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001105 request_queue_t *q;
1106
1107 if (!cfqd)
1108 return;
1109
1110 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001111
1112 WARN_ON(!irqs_disabled());
1113
1114 spin_lock(q->queue_lock);
1115
Al Viro12a05732006-03-18 13:38:01 -05001116 if (cic->cfqq[ASYNC]) {
1117 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1118 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1119 cfq_put_queue(cic->cfqq[ASYNC]);
1120 cic->cfqq[ASYNC] = NULL;
1121 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001122
Al Viro12a05732006-03-18 13:38:01 -05001123 if (cic->cfqq[SYNC]) {
1124 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1125 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1126 cfq_put_queue(cic->cfqq[SYNC]);
1127 cic->cfqq[SYNC] = NULL;
1128 }
1129
Al Viro478a82b2006-03-18 13:25:24 -05001130 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001131 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001132 spin_unlock(q->queue_lock);
1133}
1134
Jens Axboee2d74ac2006-03-28 08:59:01 +02001135static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jens Axboe22e2c502005-06-27 10:55:12 +02001137 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001139 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
1142 * put the reference this task is holding to the various queues
1143 */
Jens Axboe3793c652006-05-30 21:11:04 +02001144 spin_lock_irqsave(&cfq_exit_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001145
1146 n = rb_first(&ioc->cic_root);
1147 while (n != NULL) {
1148 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1149
Jens Axboe22e2c502005-06-27 10:55:12 +02001150 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001151 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Jens Axboe3793c652006-05-30 21:11:04 +02001154 spin_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Jens Axboe22e2c502005-06-27 10:55:12 +02001157static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001158cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Jens Axboe22e2c502005-06-27 10:55:12 +02001160 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001163 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001164 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001165 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001166 cic->dtor = cfq_free_io_context;
1167 cic->exit = cfq_exit_io_context;
Al Viro334e94d2006-03-18 15:05:53 -05001168 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
1171 return cic;
1172}
1173
Jens Axboe22e2c502005-06-27 10:55:12 +02001174static void cfq_init_prio_data(struct cfq_queue *cfqq)
1175{
1176 struct task_struct *tsk = current;
1177 int ioprio_class;
1178
Jens Axboe3b181522005-06-27 10:56:24 +02001179 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001180 return;
1181
1182 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1183 switch (ioprio_class) {
1184 default:
1185 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1186 case IOPRIO_CLASS_NONE:
1187 /*
1188 * no prio set, place us in the middle of the BE classes
1189 */
1190 cfqq->ioprio = task_nice_ioprio(tsk);
1191 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1192 break;
1193 case IOPRIO_CLASS_RT:
1194 cfqq->ioprio = task_ioprio(tsk);
1195 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1196 break;
1197 case IOPRIO_CLASS_BE:
1198 cfqq->ioprio = task_ioprio(tsk);
1199 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1200 break;
1201 case IOPRIO_CLASS_IDLE:
1202 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1203 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001204 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001205 break;
1206 }
1207
1208 /*
1209 * keep track of original prio settings in case we have to temporarily
1210 * elevate the priority of this queue
1211 */
1212 cfqq->org_ioprio = cfqq->ioprio;
1213 cfqq->org_ioprio_class = cfqq->ioprio_class;
1214
Jens Axboe3b181522005-06-27 10:56:24 +02001215 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001216 cfq_resort_rr_list(cfqq, 0);
1217
Jens Axboe3b181522005-06-27 10:56:24 +02001218 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001219}
1220
Al Viro478a82b2006-03-18 13:25:24 -05001221static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001222{
Al Viro478a82b2006-03-18 13:25:24 -05001223 struct cfq_data *cfqd = cic->key;
1224 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02001225
Jens Axboecaaa5f92006-06-16 11:23:00 +02001226 if (unlikely(!cfqd))
1227 return;
1228
1229 spin_lock(cfqd->queue->queue_lock);
1230
1231 cfqq = cic->cfqq[ASYNC];
1232 if (cfqq) {
1233 struct cfq_queue *new_cfqq;
1234 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1235 GFP_ATOMIC);
1236 if (new_cfqq) {
1237 cic->cfqq[ASYNC] = new_cfqq;
1238 cfq_put_queue(cfqq);
1239 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001240 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001241
1242 cfqq = cic->cfqq[SYNC];
1243 if (cfqq)
1244 cfq_mark_cfqq_prio_changed(cfqq);
1245
1246 spin_unlock(cfqd->queue->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001250 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001252static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Al Viroa6a07632006-03-18 13:26:44 -05001254 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001255 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001256
Jens Axboe3793c652006-05-30 21:11:04 +02001257 spin_lock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001258
Jens Axboee2d74ac2006-03-28 08:59:01 +02001259 n = rb_first(&ioc->cic_root);
1260 while (n != NULL) {
1261 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001262
Al Viro478a82b2006-03-18 13:25:24 -05001263 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001264 n = rb_next(n);
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Jens Axboe3793c652006-05-30 21:11:04 +02001267 spin_unlock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001268
Jens Axboe22e2c502005-06-27 10:55:12 +02001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001273cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001274 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1277 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001278 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280retry:
Al Viro6f325a12006-03-18 14:58:37 -05001281 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001282 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 if (!cfqq) {
1285 if (new_cfqq) {
1286 cfqq = new_cfqq;
1287 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001288 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 spin_unlock_irq(cfqd->queue->queue_lock);
1290 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1291 spin_lock_irq(cfqd->queue->queue_lock);
1292 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001293 } else {
1294 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1295 if (!cfqq)
1296 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 memset(cfqq, 0, sizeof(*cfqq));
1300
1301 INIT_HLIST_NODE(&cfqq->cfq_hash);
1302 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001303 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 cfqq->key = key;
1306 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1307 atomic_set(&cfqq->ref, 0);
1308 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001309 cfqq->service_last = 0;
1310 /*
1311 * set ->slice_left to allow preemption for a new process
1312 */
1313 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001314 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001315 cfq_mark_cfqq_prio_changed(cfqq);
1316 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 if (new_cfqq)
1320 kmem_cache_free(cfq_pool, new_cfqq);
1321
1322 atomic_inc(&cfqq->ref);
1323out:
1324 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1325 return cfqq;
1326}
1327
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001328static void
1329cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1330{
Jens Axboe3793c652006-05-30 21:11:04 +02001331 spin_lock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001332 rb_erase(&cic->rb_node, &ioc->cic_root);
Jens Axboe3793c652006-05-30 21:11:04 +02001333 list_del_init(&cic->queue_list);
1334 spin_unlock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001335 kmem_cache_free(cfq_ioc_pool, cic);
1336 atomic_dec(&ioc_count);
1337}
1338
Jens Axboee2d74ac2006-03-28 08:59:01 +02001339static struct cfq_io_context *
1340cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1341{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001342 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001343 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001344 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001345
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001346restart:
1347 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001348 while (n) {
1349 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001350 /* ->key must be copied to avoid race with cfq_exit_queue() */
1351 k = cic->key;
1352 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001353 cfq_drop_dead_cic(ioc, cic);
1354 goto restart;
1355 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001356
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001357 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001358 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001359 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001360 n = n->rb_right;
1361 else
1362 return cic;
1363 }
1364
1365 return NULL;
1366}
1367
1368static inline void
1369cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1370 struct cfq_io_context *cic)
1371{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001372 struct rb_node **p;
1373 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001374 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001375 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001376
Jens Axboee2d74ac2006-03-28 08:59:01 +02001377 cic->ioc = ioc;
1378 cic->key = cfqd;
1379
1380 ioc->set_ioprio = cfq_ioc_set_ioprio;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001381restart:
1382 parent = NULL;
1383 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001384 while (*p) {
1385 parent = *p;
1386 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001387 /* ->key must be copied to avoid race with cfq_exit_queue() */
1388 k = __cic->key;
1389 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001390 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001391 goto restart;
1392 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001393
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001394 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001395 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001396 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001397 p = &(*p)->rb_right;
1398 else
1399 BUG();
1400 }
1401
Jens Axboe3793c652006-05-30 21:11:04 +02001402 spin_lock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001403 rb_link_node(&cic->rb_node, parent, p);
1404 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1405 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe3793c652006-05-30 21:11:04 +02001406 spin_unlock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001407}
1408
Jens Axboe22e2c502005-06-27 10:55:12 +02001409/*
1410 * Setup general io context and cfq io context. There can be several cfq
1411 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001412 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001413 */
1414static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001415cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Jens Axboe22e2c502005-06-27 10:55:12 +02001417 struct io_context *ioc = NULL;
1418 struct cfq_io_context *cic;
1419
1420 might_sleep_if(gfp_mask & __GFP_WAIT);
1421
1422 ioc = get_io_context(gfp_mask);
1423 if (!ioc)
1424 return NULL;
1425
Jens Axboee2d74ac2006-03-28 08:59:01 +02001426 cic = cfq_cic_rb_lookup(cfqd, ioc);
1427 if (cic)
1428 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001429
Jens Axboee2d74ac2006-03-28 08:59:01 +02001430 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1431 if (cic == NULL)
1432 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001433
Jens Axboee2d74ac2006-03-28 08:59:01 +02001434 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001435out:
1436 return cic;
1437err:
1438 put_io_context(ioc);
1439 return NULL;
1440}
1441
1442static void
1443cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1444{
1445 unsigned long elapsed, ttime;
1446
1447 /*
1448 * if this context already has stuff queued, thinktime is from
1449 * last queue not last end
1450 */
1451#if 0
1452 if (time_after(cic->last_end_request, cic->last_queue))
1453 elapsed = jiffies - cic->last_end_request;
1454 else
1455 elapsed = jiffies - cic->last_queue;
1456#else
1457 elapsed = jiffies - cic->last_end_request;
1458#endif
1459
1460 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1461
1462 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1463 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1464 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1465}
1466
Jens Axboe206dc692006-03-28 13:03:44 +02001467static void
1468cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
Jens Axboe5e705372006-07-13 12:39:25 +02001469 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001470{
1471 sector_t sdist;
1472 u64 total;
1473
Jens Axboe5e705372006-07-13 12:39:25 +02001474 if (cic->last_request_pos < rq->sector)
1475 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001476 else
Jens Axboe5e705372006-07-13 12:39:25 +02001477 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001478
1479 /*
1480 * Don't allow the seek distance to get too large from the
1481 * odd fragment, pagein, etc
1482 */
1483 if (cic->seek_samples <= 60) /* second&third seek */
1484 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1485 else
1486 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1487
1488 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1489 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1490 total = cic->seek_total + (cic->seek_samples/2);
1491 do_div(total, cic->seek_samples);
1492 cic->seek_mean = (sector_t)total;
1493}
Jens Axboe22e2c502005-06-27 10:55:12 +02001494
1495/*
1496 * Disable idle window if the process thinks too long or seeks so much that
1497 * it doesn't matter
1498 */
1499static void
1500cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1501 struct cfq_io_context *cic)
1502{
Jens Axboe3b181522005-06-27 10:56:24 +02001503 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001504
Jens Axboecaaa5f92006-06-16 11:23:00 +02001505 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1506 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001507 enable_idle = 0;
1508 else if (sample_valid(cic->ttime_samples)) {
1509 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1510 enable_idle = 0;
1511 else
1512 enable_idle = 1;
1513 }
1514
Jens Axboe3b181522005-06-27 10:56:24 +02001515 if (enable_idle)
1516 cfq_mark_cfqq_idle_window(cfqq);
1517 else
1518 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001519}
1520
1521
1522/*
1523 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1524 * no or if we aren't sure, a 1 will cause a preempt.
1525 */
1526static int
1527cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001528 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001529{
1530 struct cfq_queue *cfqq = cfqd->active_queue;
1531
1532 if (cfq_class_idle(new_cfqq))
1533 return 0;
1534
1535 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001536 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001537
1538 if (cfq_class_idle(cfqq))
1539 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001540 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001541 return 0;
1542 /*
1543 * if it doesn't have slice left, forget it
1544 */
1545 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1546 return 0;
Jens Axboe5e705372006-07-13 12:39:25 +02001547 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001548 return 1;
1549
1550 return 0;
1551}
1552
1553/*
1554 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1555 * let it have half of its nominal slice.
1556 */
1557static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1558{
1559 struct cfq_queue *__cfqq, *next;
1560
1561 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1562 cfq_resort_rr_list(__cfqq, 1);
1563
1564 if (!cfqq->slice_left)
1565 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1566
1567 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001568 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001569 __cfq_set_active_queue(cfqd, cfqq);
1570}
1571
1572/*
1573 * should really be a ll_rw_blk.c helper
1574 */
1575static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1576{
1577 request_queue_t *q = cfqd->queue;
1578
1579 if (!blk_queue_plugged(q))
1580 q->request_fn(q);
1581 else
1582 __generic_unplug_device(q);
1583}
1584
1585/*
Jens Axboe5e705372006-07-13 12:39:25 +02001586 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001587 * something we should do about it
1588 */
1589static void
Jens Axboe5e705372006-07-13 12:39:25 +02001590cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1591 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001592{
Jens Axboe5e705372006-07-13 12:39:25 +02001593 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001594
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001595 /*
Jens Axboe5380a102006-07-13 12:37:56 +02001596 * check if this request is a better next-serve candidate)) {
Jens Axboe21183b02006-07-13 12:33:14 +02001597 */
Jens Axboe5e705372006-07-13 12:39:25 +02001598 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
1599 BUG_ON(!cfqq->next_rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001600
1601 /*
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001602 * we never wait for an async request and we don't allow preemption
1603 * of an async request. so just return early
1604 */
Jens Axboe5e705372006-07-13 12:39:25 +02001605 if (!rq_is_sync(rq)) {
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001606 /*
1607 * sync process issued an async request, if it's waiting
1608 * then expire it and kick rq handling.
1609 */
1610 if (cic == cfqd->active_cic &&
1611 del_timer(&cfqd->idle_slice_timer)) {
1612 cfq_slice_expired(cfqd, 0);
1613 cfq_start_queueing(cfqd, cfqq);
1614 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001615 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001616 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001617
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001618 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe5e705372006-07-13 12:39:25 +02001619 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001620 cfq_update_idle_window(cfqd, cfqq, cic);
1621
1622 cic->last_queue = jiffies;
Jens Axboe5e705372006-07-13 12:39:25 +02001623 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001624
1625 if (cfqq == cfqd->active_queue) {
1626 /*
1627 * if we are waiting for a request for this queue, let it rip
1628 * immediately and flag that we must not expire this queue
1629 * just now
1630 */
Jens Axboe3b181522005-06-27 10:56:24 +02001631 if (cfq_cfqq_wait_request(cfqq)) {
1632 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001633 del_timer(&cfqd->idle_slice_timer);
1634 cfq_start_queueing(cfqd, cfqq);
1635 }
Jens Axboe5e705372006-07-13 12:39:25 +02001636 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001637 /*
1638 * not the active queue - expire current slice if it is
1639 * idle and has expired it's mean thinktime or this new queue
1640 * has some old slice time left and is of higher priority
1641 */
1642 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001643 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001644 cfq_start_queueing(cfqd, cfqq);
1645 }
1646}
1647
Jens Axboeb4878f22005-10-20 16:42:29 +02001648static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001649{
Jens Axboeb4878f22005-10-20 16:42:29 +02001650 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001651 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001652
1653 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jens Axboe5e705372006-07-13 12:39:25 +02001655 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Jens Axboe21183b02006-07-13 12:33:14 +02001657 if (!cfq_cfqq_on_rr(cfqq))
1658 cfq_add_cfqq_rr(cfqd, cfqq);
1659
Jens Axboe22e2c502005-06-27 10:55:12 +02001660 list_add_tail(&rq->queuelist, &cfqq->fifo);
1661
Jens Axboe5e705372006-07-13 12:39:25 +02001662 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static void cfq_completed_request(request_queue_t *q, struct request *rq)
1666{
Jens Axboe5e705372006-07-13 12:39:25 +02001667 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001668 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001669 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001670 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Jens Axboeb4878f22005-10-20 16:42:29 +02001672 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jens Axboeb4878f22005-10-20 16:42:29 +02001674 WARN_ON(!cfqd->rq_in_driver);
1675 WARN_ON(!cfqq->on_dispatch[sync]);
1676 cfqd->rq_in_driver--;
1677 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jens Axboeb4878f22005-10-20 16:42:29 +02001679 if (!cfq_class_idle(cfqq))
1680 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001681
Jens Axboeb4878f22005-10-20 16:42:29 +02001682 if (!cfq_cfqq_dispatched(cfqq)) {
1683 if (cfq_cfqq_on_rr(cfqq)) {
1684 cfqq->service_last = now;
1685 cfq_resort_rr_list(cfqq, 0);
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Jens Axboecaaa5f92006-06-16 11:23:00 +02001689 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001690 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001691
1692 /*
1693 * If this is the active queue, check if it needs to be expired,
1694 * or if we want to idle in case it has no pending requests.
1695 */
1696 if (cfqd->active_queue == cfqq) {
1697 if (time_after(now, cfqq->slice_end))
1698 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001699 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001700 if (!cfq_arm_slice_timer(cfqd, cfqq))
1701 cfq_schedule_dispatch(cfqd);
1702 }
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Jens Axboe22e2c502005-06-27 10:55:12 +02001706/*
1707 * we temporarily boost lower priority queues if they are holding fs exclusive
1708 * resources. they are boosted to normal prio (CLASS_BE/4)
1709 */
1710static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Jens Axboe22e2c502005-06-27 10:55:12 +02001712 const int ioprio_class = cfqq->ioprio_class;
1713 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Jens Axboe22e2c502005-06-27 10:55:12 +02001715 if (has_fs_excl()) {
1716 /*
1717 * boost idle prio on transactions that would lock out other
1718 * users of the filesystem
1719 */
1720 if (cfq_class_idle(cfqq))
1721 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1722 if (cfqq->ioprio > IOPRIO_NORM)
1723 cfqq->ioprio = IOPRIO_NORM;
1724 } else {
1725 /*
1726 * check if we need to unboost the queue
1727 */
1728 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1729 cfqq->ioprio_class = cfqq->org_ioprio_class;
1730 if (cfqq->ioprio != cfqq->org_ioprio)
1731 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Jens Axboe22e2c502005-06-27 10:55:12 +02001734 /*
1735 * refile between round-robin lists if we moved the priority class
1736 */
1737 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001738 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001739 cfq_resort_rr_list(cfqq, 0);
1740}
1741
Jens Axboe22e2c502005-06-27 10:55:12 +02001742static inline int
1743__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1744 struct task_struct *task, int rw)
1745{
Jens Axboe3b181522005-06-27 10:56:24 +02001746 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001747 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001748 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001749 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001750 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001751
1752 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001753}
1754
1755static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1756{
1757 struct cfq_data *cfqd = q->elevator->elevator_data;
1758 struct task_struct *tsk = current;
1759 struct cfq_queue *cfqq;
1760
1761 /*
1762 * don't force setup of a queue from here, as a call to may_queue
1763 * does not necessarily imply that a request actually will be queued.
1764 * so just lookup a possibly existing queue, or return 'may queue'
1765 * if that fails
1766 */
Jens Axboe3b181522005-06-27 10:56:24 +02001767 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001768 if (cfqq) {
1769 cfq_init_prio_data(cfqq);
1770 cfq_prio_boost(cfqq);
1771
1772 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1773 }
1774
1775 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1779{
Jens Axboe22e2c502005-06-27 10:55:12 +02001780 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jens Axboe271f18f2006-06-13 08:08:38 +02001782 if (unlikely(cfqd->rq_starved)) {
1783 struct request_list *rl = &q->rq;
1784
Jens Axboe22e2c502005-06-27 10:55:12 +02001785 smp_mb();
1786 if (waitqueue_active(&rl->wait[READ]))
1787 wake_up(&rl->wait[READ]);
Jens Axboe22e2c502005-06-27 10:55:12 +02001788 if (waitqueue_active(&rl->wait[WRITE]))
1789 wake_up(&rl->wait[WRITE]);
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793/*
1794 * queue lock held here
1795 */
1796static void cfq_put_request(request_queue_t *q, struct request *rq)
1797{
Jens Axboe5e705372006-07-13 12:39:25 +02001798 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jens Axboe5e705372006-07-13 12:39:25 +02001800 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001801 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Jens Axboe22e2c502005-06-27 10:55:12 +02001803 BUG_ON(!cfqq->allocated[rw]);
1804 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Jens Axboe5e705372006-07-13 12:39:25 +02001806 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001809 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 cfq_check_waiters(q, cfqq);
1812 cfq_put_queue(cfqq);
1813 }
1814}
1815
1816/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001817 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001819static int
1820cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001821 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001824 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct cfq_io_context *cic;
1826 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001827 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001828 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001830 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 might_sleep_if(gfp_mask & __GFP_WAIT);
1833
Jens Axboee2d74ac2006-03-28 08:59:01 +02001834 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 spin_lock_irqsave(q->queue_lock, flags);
1837
Jens Axboe22e2c502005-06-27 10:55:12 +02001838 if (!cic)
1839 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Al Viro12a05732006-03-18 13:38:01 -05001841 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001842 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001843 if (!cfqq)
1844 goto queue_fail;
1845
Al Viro12a05732006-03-18 13:38:01 -05001846 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001847 } else
Al Viro12a05732006-03-18 13:38:01 -05001848 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001851 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001852 cfqd->rq_starved = 0;
1853 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 spin_unlock_irqrestore(q->queue_lock, flags);
1856
Jens Axboe5e705372006-07-13 12:39:25 +02001857 rq->elevator_private = cic;
1858 rq->elevator_private2 = cfqq;
1859 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001860
Jens Axboe22e2c502005-06-27 10:55:12 +02001861queue_fail:
1862 if (cic)
1863 put_io_context(cic->ioc);
1864 /*
1865 * mark us rq allocation starved. we need to kickstart the process
1866 * ourselves if there are no pending requests that can do it for us.
1867 * that would be an extremely rare OOM situation
1868 */
1869 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001870 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 spin_unlock_irqrestore(q->queue_lock, flags);
1872 return 1;
1873}
1874
Jens Axboe22e2c502005-06-27 10:55:12 +02001875static void cfq_kick_queue(void *data)
1876{
1877 request_queue_t *q = data;
1878 struct cfq_data *cfqd = q->elevator->elevator_data;
1879 unsigned long flags;
1880
1881 spin_lock_irqsave(q->queue_lock, flags);
1882
1883 if (cfqd->rq_starved) {
1884 struct request_list *rl = &q->rq;
1885
1886 /*
1887 * we aren't guaranteed to get a request after this, but we
1888 * have to be opportunistic
1889 */
1890 smp_mb();
1891 if (waitqueue_active(&rl->wait[READ]))
1892 wake_up(&rl->wait[READ]);
1893 if (waitqueue_active(&rl->wait[WRITE]))
1894 wake_up(&rl->wait[WRITE]);
1895 }
1896
1897 blk_remove_plug(q);
1898 q->request_fn(q);
1899 spin_unlock_irqrestore(q->queue_lock, flags);
1900}
1901
1902/*
1903 * Timer running if the active_queue is currently idling inside its time slice
1904 */
1905static void cfq_idle_slice_timer(unsigned long data)
1906{
1907 struct cfq_data *cfqd = (struct cfq_data *) data;
1908 struct cfq_queue *cfqq;
1909 unsigned long flags;
1910
1911 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1912
1913 if ((cfqq = cfqd->active_queue) != NULL) {
1914 unsigned long now = jiffies;
1915
1916 /*
1917 * expired
1918 */
1919 if (time_after(now, cfqq->slice_end))
1920 goto expire;
1921
1922 /*
1923 * only expire and reinvoke request handler, if there are
1924 * other queues with pending requests
1925 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001926 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001927 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001928
1929 /*
1930 * not expired and it has a request pending, let it dispatch
1931 */
Jens Axboedd67d052006-06-21 09:36:18 +02001932 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001933 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001934 goto out_kick;
1935 }
1936 }
1937expire:
1938 cfq_slice_expired(cfqd, 0);
1939out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001940 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001941out_cont:
1942 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1943}
1944
1945/*
1946 * Timer running if an idle class queue is waiting for service
1947 */
1948static void cfq_idle_class_timer(unsigned long data)
1949{
1950 struct cfq_data *cfqd = (struct cfq_data *) data;
1951 unsigned long flags, end;
1952
1953 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1954
1955 /*
1956 * race with a non-idle queue, reset timer
1957 */
1958 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001959 if (!time_after_eq(jiffies, end))
1960 mod_timer(&cfqd->idle_class_timer, end);
1961 else
Jens Axboe3b181522005-06-27 10:56:24 +02001962 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001963
1964 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1965}
1966
Jens Axboe3b181522005-06-27 10:56:24 +02001967static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1968{
1969 del_timer_sync(&cfqd->idle_slice_timer);
1970 del_timer_sync(&cfqd->idle_class_timer);
1971 blk_sync_queue(cfqd->queue);
1972}
Jens Axboe22e2c502005-06-27 10:55:12 +02001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974static void cfq_exit_queue(elevator_t *e)
1975{
Jens Axboe22e2c502005-06-27 10:55:12 +02001976 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05001977 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001978
Jens Axboe3b181522005-06-27 10:56:24 +02001979 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001980
Jens Axboe3793c652006-05-30 21:11:04 +02001981 spin_lock(&cfq_exit_lock);
Al Virod9ff4182006-03-18 13:51:22 -05001982 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001983
Al Virod9ff4182006-03-18 13:51:22 -05001984 if (cfqd->active_queue)
1985 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001986
1987 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05001988 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1989 struct cfq_io_context,
1990 queue_list);
1991 if (cic->cfqq[ASYNC]) {
1992 cfq_put_queue(cic->cfqq[ASYNC]);
1993 cic->cfqq[ASYNC] = NULL;
1994 }
1995 if (cic->cfqq[SYNC]) {
1996 cfq_put_queue(cic->cfqq[SYNC]);
1997 cic->cfqq[SYNC] = NULL;
1998 }
1999 cic->key = NULL;
2000 list_del_init(&cic->queue_list);
2001 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002002
Al Virod9ff4182006-03-18 13:51:22 -05002003 spin_unlock_irq(q->queue_lock);
Jens Axboe3793c652006-05-30 21:11:04 +02002004 spin_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002005
2006 cfq_shutdown_timer_wq(cfqd);
2007
Al Viroa90d7422006-03-18 12:05:37 -05002008 kfree(cfqd->cfq_hash);
2009 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Jens Axboebc1c1162006-06-08 08:49:06 +02002012static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 struct cfq_data *cfqd;
2015 int i;
2016
2017 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2018 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002019 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002022
2023 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2024 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2025
2026 INIT_LIST_HEAD(&cfqd->busy_rr);
2027 INIT_LIST_HEAD(&cfqd->cur_rr);
2028 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002030 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2033 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002034 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2037 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jens Axboe22e2c502005-06-27 10:55:12 +02002041 init_timer(&cfqd->idle_slice_timer);
2042 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2043 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2044
2045 init_timer(&cfqd->idle_class_timer);
2046 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2047 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2048
2049 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 cfqd->cfq_queued = cfq_queued;
2052 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002053 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2054 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 cfqd->cfq_back_max = cfq_back_max;
2056 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002057 cfqd->cfq_slice[0] = cfq_slice_async;
2058 cfqd->cfq_slice[1] = cfq_slice_sync;
2059 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2060 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002061
Jens Axboebc1c1162006-06-08 08:49:06 +02002062 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002063out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002065 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068static void cfq_slab_kill(void)
2069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (cfq_pool)
2071 kmem_cache_destroy(cfq_pool);
2072 if (cfq_ioc_pool)
2073 kmem_cache_destroy(cfq_ioc_pool);
2074}
2075
2076static int __init cfq_slab_setup(void)
2077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2079 NULL, NULL);
2080 if (!cfq_pool)
2081 goto fail;
2082
2083 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2084 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2085 if (!cfq_ioc_pool)
2086 goto fail;
2087
2088 return 0;
2089fail:
2090 cfq_slab_kill();
2091 return -ENOMEM;
2092}
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * sysfs parts below -->
2096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098static ssize_t
2099cfq_var_show(unsigned int var, char *page)
2100{
2101 return sprintf(page, "%d\n", var);
2102}
2103
2104static ssize_t
2105cfq_var_store(unsigned int *var, const char *page, size_t count)
2106{
2107 char *p = (char *) page;
2108
2109 *var = simple_strtoul(p, &p, 10);
2110 return count;
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002114static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002116 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned int __data = __VAR; \
2118 if (__CONV) \
2119 __data = jiffies_to_msecs(__data); \
2120 return cfq_var_show(__data, (page)); \
2121}
2122SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2123SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002124SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2125SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002126SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2127SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002128SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2129SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2130SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2131SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#undef SHOW_FUNCTION
2133
2134#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002135static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002137 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 unsigned int __data; \
2139 int ret = cfq_var_store(&__data, (page), count); \
2140 if (__data < (MIN)) \
2141 __data = (MIN); \
2142 else if (__data > (MAX)) \
2143 __data = (MAX); \
2144 if (__CONV) \
2145 *(__PTR) = msecs_to_jiffies(__data); \
2146 else \
2147 *(__PTR) = __data; \
2148 return ret; \
2149}
2150STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2151STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002152STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2153STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002154STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2155STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002156STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2157STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2158STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2159STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#undef STORE_FUNCTION
2161
Al Viroe572ec72006-03-18 22:27:18 -05002162#define CFQ_ATTR(name) \
2163 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002164
Al Viroe572ec72006-03-18 22:27:18 -05002165static struct elv_fs_entry cfq_attrs[] = {
2166 CFQ_ATTR(quantum),
2167 CFQ_ATTR(queued),
2168 CFQ_ATTR(fifo_expire_sync),
2169 CFQ_ATTR(fifo_expire_async),
2170 CFQ_ATTR(back_seek_max),
2171 CFQ_ATTR(back_seek_penalty),
2172 CFQ_ATTR(slice_sync),
2173 CFQ_ATTR(slice_async),
2174 CFQ_ATTR(slice_async_rq),
2175 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002176 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179static struct elevator_type iosched_cfq = {
2180 .ops = {
2181 .elevator_merge_fn = cfq_merge,
2182 .elevator_merged_fn = cfq_merged_request,
2183 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002184 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002186 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 .elevator_deactivate_req_fn = cfq_deactivate_request,
2188 .elevator_queue_empty_fn = cfq_queue_empty,
2189 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002190 .elevator_former_req_fn = elv_rb_former_request,
2191 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 .elevator_set_req_fn = cfq_set_request,
2193 .elevator_put_req_fn = cfq_put_request,
2194 .elevator_may_queue_fn = cfq_may_queue,
2195 .elevator_init_fn = cfq_init_queue,
2196 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002197 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 },
Al Viro3d1ab402006-03-18 18:35:43 -05002199 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .elevator_name = "cfq",
2201 .elevator_owner = THIS_MODULE,
2202};
2203
2204static int __init cfq_init(void)
2205{
2206 int ret;
2207
Jens Axboe22e2c502005-06-27 10:55:12 +02002208 /*
2209 * could be 0 on HZ < 1000 setups
2210 */
2211 if (!cfq_slice_async)
2212 cfq_slice_async = 1;
2213 if (!cfq_slice_idle)
2214 cfq_slice_idle = 1;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (cfq_slab_setup())
2217 return -ENOMEM;
2218
2219 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002220 if (ret)
2221 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return ret;
2224}
2225
2226static void __exit cfq_exit(void)
2227{
Al Viro334e94d2006-03-18 15:05:53 -05002228 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002230 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002231 /* ioc_gone's update must be visible before reading ioc_count */
2232 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05002233 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002234 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002235 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002236 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239module_init(cfq_init);
2240module_exit(cfq_exit);
2241
2242MODULE_AUTHOR("Jens Axboe");
2243MODULE_LICENSE("GPL");
2244MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");