blob: 9f684cc66bd1631042d7d53fe9c5e52f6cbb3e54 [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 */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven64100092006-01-06 09:46:02 +010024static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020025static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020027static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020028
29#define CFQ_IDLE_GRACE (HZ / 10)
30#define CFQ_SLICE_SCALE (5)
31
32#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * for the hash of cfqq inside the cfqd
36 */
37#define CFQ_QHASH_SHIFT 6
38#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
39#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
42
Jens Axboe5e705372006-07-13 12:39:25 +020043#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
44#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static kmem_cache_t *cfq_pool;
47static kmem_cache_t *cfq_ioc_pool;
48
Jens Axboe4050cf12006-07-19 05:07:12 +020049static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050050static struct completion *ioc_gone;
51
Jens Axboe22e2c502005-06-27 10:55:12 +020052#define CFQ_PRIO_LISTS IOPRIO_BE_NR
53#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020054#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
55
Jens Axboe3b181522005-06-27 10:56:24 +020056#define ASYNC (0)
57#define SYNC (1)
58
59#define cfq_cfqq_dispatched(cfqq) \
60 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
61
62#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
63
64#define cfq_cfqq_sync(cfqq) \
65 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020066
Jens Axboe206dc692006-03-28 13:03:44 +020067#define sample_valid(samples) ((samples) > 80)
68
Jens Axboe22e2c502005-06-27 10:55:12 +020069/*
70 * Per block device queue structure
71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020073 request_queue_t *queue;
74
75 /*
76 * rr list of queues with requests and the count of them
77 */
78 struct list_head rr_list[CFQ_PRIO_LISTS];
79 struct list_head busy_rr;
80 struct list_head cur_rr;
81 struct list_head idle_rr;
82 unsigned int busy_queues;
83
84 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020085 * cfqq lookup hash
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jens Axboe22e2c502005-06-27 10:55:12 +020089 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020090 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020091
92 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020093 * idle window management
94 */
95 struct timer_list idle_slice_timer;
96 struct work_struct unplug_work;
97
98 struct cfq_queue *active_queue;
99 struct cfq_io_context *active_cic;
100 int cur_prio, cur_end_prio;
101 unsigned int dispatch_slice;
102
103 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200106 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * tunables, see top of file
110 */
111 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200112 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int cfq_back_penalty;
114 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200115 unsigned int cfq_slice[2];
116 unsigned int cfq_slice_async_rq;
117 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500118
119 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Jens Axboe22e2c502005-06-27 10:55:12 +0200122/*
123 * Per process-grouping structure
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct cfq_queue {
126 /* reference count */
127 atomic_t ref;
128 /* parent cfq_data */
129 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200130 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct hlist_node cfq_hash;
132 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200133 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200134 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct list_head cfq_list;
136 /* sorted list of pending requests */
137 struct rb_root sort_list;
138 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200139 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* requests queued in sort_list */
141 int queued[2];
142 /* currently allocated requests */
143 int allocated[2];
144 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jens Axboe22e2c502005-06-27 10:55:12 +0200147 unsigned long slice_start;
148 unsigned long slice_end;
149 unsigned long slice_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jens Axboe3b181522005-06-27 10:56:24 +0200151 /* number of requests that are on the dispatch list */
152 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200153
154 /* io prio of this group */
155 unsigned short ioprio, org_ioprio;
156 unsigned short ioprio_class, org_ioprio_class;
157
Jens Axboe3b181522005-06-27 10:56:24 +0200158 /* various state flags, see below */
159 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
Jens Axboe3b181522005-06-27 10:56:24 +0200162enum cfqq_state_flags {
163 CFQ_CFQQ_FLAG_on_rr = 0,
164 CFQ_CFQQ_FLAG_wait_request,
165 CFQ_CFQQ_FLAG_must_alloc,
166 CFQ_CFQQ_FLAG_must_alloc_slice,
167 CFQ_CFQQ_FLAG_must_dispatch,
168 CFQ_CFQQ_FLAG_fifo_expire,
169 CFQ_CFQQ_FLAG_idle_window,
170 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe53b037442006-07-28 09:48:51 +0200171 CFQ_CFQQ_FLAG_queue_new,
Jens Axboe3b181522005-06-27 10:56:24 +0200172};
173
174#define CFQ_CFQQ_FNS(name) \
175static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
176{ \
177 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
178} \
179static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
180{ \
181 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
182} \
183static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
184{ \
185 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
186}
187
188CFQ_CFQQ_FNS(on_rr);
189CFQ_CFQQ_FNS(wait_request);
190CFQ_CFQQ_FNS(must_alloc);
191CFQ_CFQQ_FNS(must_alloc_slice);
192CFQ_CFQQ_FNS(must_dispatch);
193CFQ_CFQQ_FNS(fifo_expire);
194CFQ_CFQQ_FNS(idle_window);
195CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b037442006-07-28 09:48:51 +0200196CFQ_CFQQ_FNS(queue_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200197#undef CFQ_CFQQ_FNS
198
Jens Axboe3b181522005-06-27 10:56:24 +0200199static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200200static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500201static 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 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700204 * scheduler run of queue, if there are requests pending and no one in the
205 * driver that will restart queueing
206 */
207static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
208{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100209 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700210 kblockd_schedule_work(&cfqd->unplug_work);
211}
212
213static int cfq_queue_empty(request_queue_t *q)
214{
215 struct cfq_data *cfqd = q->elevator->elevator_data;
216
Jens Axboeb4878f22005-10-20 16:42:29 +0200217 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700218}
219
Jens Axboe206dc692006-03-28 13:03:44 +0200220static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
221{
Jens Axboeb31dc662006-06-13 08:26:10 +0200222 if (rw == READ || rw == WRITE_SYNC)
Jens Axboe206dc692006-03-28 13:03:44 +0200223 return task->pid;
224
225 return CFQ_KEY_ASYNC;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
Jens Axboe5e705372006-07-13 12:39:25 +0200229 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200231 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Jens Axboe5e705372006-07-13 12:39:25 +0200233static struct request *
234cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200238#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
239#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
240 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jens Axboe5e705372006-07-13 12:39:25 +0200242 if (rq1 == NULL || rq1 == rq2)
243 return rq2;
244 if (rq2 == NULL)
245 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200246
Jens Axboe5e705372006-07-13 12:39:25 +0200247 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
248 return rq1;
249 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
250 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jens Axboe5e705372006-07-13 12:39:25 +0200252 s1 = rq1->sector;
253 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 last = cfqd->last_sector;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * by definition, 1KiB is 2 sectors
259 */
260 back_max = cfqd->cfq_back_max * 2;
261
262 /*
263 * Strict one way elevator _except_ in the case where we allow
264 * short backward seeks which are biased as twice the cost of a
265 * similar forward seek.
266 */
267 if (s1 >= last)
268 d1 = s1 - last;
269 else if (s1 + back_max >= last)
270 d1 = (last - s1) * cfqd->cfq_back_penalty;
271 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200272 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (s2 >= last)
275 d2 = s2 - last;
276 else if (s2 + back_max >= last)
277 d2 = (last - s2) * cfqd->cfq_back_penalty;
278 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200279 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Andreas Mohre8a99052006-03-28 08:59:49 +0200283 /*
284 * By doing switch() on the bit mask "wrap" we avoid having to
285 * check two variables for all permutations: --> faster!
286 */
287 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200288 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200289 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200290 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200291 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200292 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200293 else {
294 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200295 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200296 else
Jens Axboe5e705372006-07-13 12:39:25 +0200297 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200298 }
299
300 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200301 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200302 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200303 return rq2;
304 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200305 default:
306 /*
307 * Since both rqs are wrapped,
308 * start with the one that's further behind head
309 * (--> only *one* back seek required),
310 * since back seek takes more time than forward.
311 */
312 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200313 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 else
Jens Axboe5e705372006-07-13 12:39:25 +0200315 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317}
318
319/*
320 * would be nice to take fifo expire time into account as well
321 */
Jens Axboe5e705372006-07-13 12:39:25 +0200322static struct request *
323cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
324 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Jens Axboe21183b02006-07-13 12:33:14 +0200326 struct rb_node *rbnext = rb_next(&last->rb_node);
327 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200328 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Jens Axboe21183b02006-07-13 12:33:14 +0200330 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200333 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200336 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200337 else {
338 rbnext = rb_first(&cfqq->sort_list);
339 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200340 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jens Axboe21183b02006-07-13 12:33:14 +0200343 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Jens Axboe22e2c502005-06-27 10:55:12 +0200346static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jens Axboe22e2c502005-06-27 10:55:12 +0200348 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe53b037442006-07-28 09:48:51 +0200349 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jens Axboe3b181522005-06-27 10:56:24 +0200351 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 list_del(&cfqq->cfq_list);
354
Jens Axboe22e2c502005-06-27 10:55:12 +0200355 if (cfq_class_rt(cfqq))
356 list = &cfqd->cur_rr;
357 else if (cfq_class_idle(cfqq))
358 list = &cfqd->idle_rr;
359 else {
360 /*
361 * if cfqq has requests in flight, don't allow it to be
362 * found in cfq_set_active_queue before it has finished them.
363 * this is done to increase fairness between a process that
364 * has lots of io pending vs one that only generates one
365 * sporadically or synchronously
366 */
Jens Axboe3b181522005-06-27 10:56:24 +0200367 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200368 list = &cfqd->busy_rr;
369 else
370 list = &cfqd->rr_list[cfqq->ioprio];
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
Jens Axboe53b037442006-07-28 09:48:51 +0200374 * If this queue was preempted or is new (never been serviced), let
375 * it be added first for fairness but beind other new queues.
376 * Otherwise, just add to the back of the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Jens Axboe53b037442006-07-28 09:48:51 +0200378 if (preempted || cfq_cfqq_queue_new(cfqq)) {
379 struct list_head *n = list;
380 struct cfq_queue *__cfqq;
Jens Axboeb52a8342006-06-01 18:53:43 +0200381
Jens Axboe53b037442006-07-28 09:48:51 +0200382 while (n->next != list) {
383 __cfqq = list_entry_cfqq(n->next);
384 if (!cfq_cfqq_queue_new(__cfqq))
385 break;
386
387 n = n->next;
388 }
389
390 list = n;
Jens Axboe22e2c502005-06-27 10:55:12 +0200391 }
392
Jens Axboe53b037442006-07-28 09:48:51 +0200393 list_add_tail(&cfqq->cfq_list, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200398 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200401cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Jens Axboe3b181522005-06-27 10:56:24 +0200403 BUG_ON(cfq_cfqq_on_rr(cfqq));
404 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 cfqd->busy_queues++;
406
Jens Axboeb4878f22005-10-20 16:42:29 +0200407 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static inline void
411cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
412{
Jens Axboe3b181522005-06-27 10:56:24 +0200413 BUG_ON(!cfq_cfqq_on_rr(cfqq));
414 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200415 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 BUG_ON(!cfqd->busy_queues);
418 cfqd->busy_queues--;
419}
420
421/*
422 * rb tree support functions
423 */
Jens Axboe5e705372006-07-13 12:39:25 +0200424static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Jens Axboe5e705372006-07-13 12:39:25 +0200426 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200427 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200428 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jens Axboeb4878f22005-10-20 16:42:29 +0200430 BUG_ON(!cfqq->queued[sync]);
431 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jens Axboe5e705372006-07-13 12:39:25 +0200433 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jens Axboedd67d052006-06-21 09:36:18 +0200435 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200436 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Jens Axboe5e705372006-07-13 12:39:25 +0200439static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jens Axboe5e705372006-07-13 12:39:25 +0200441 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200443 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jens Axboe5380a102006-07-13 12:37:56 +0200445 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * looks a little odd, but the first insert might return an alias.
449 * if that happens, put the alias on the dispatch list
450 */
Jens Axboe21183b02006-07-13 12:33:14 +0200451 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200452 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200456cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Jens Axboe5380a102006-07-13 12:37:56 +0200458 elv_rb_del(&cfqq->sort_list, rq);
459 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200460 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Jens Axboe206dc692006-03-28 13:03:44 +0200463static struct request *
464cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jens Axboe206dc692006-03-28 13:03:44 +0200466 struct task_struct *tsk = current;
467 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
468 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Jens Axboe206dc692006-03-28 13:03:44 +0200470 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200471 if (cfqq) {
472 sector_t sector = bio->bi_sector + bio_sectors(bio);
473
Jens Axboe21183b02006-07-13 12:33:14 +0200474 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return NULL;
478}
479
Jens Axboeb4878f22005-10-20 16:42:29 +0200480static void cfq_activate_request(request_queue_t *q, struct request *rq)
481{
482 struct cfq_data *cfqd = q->elevator->elevator_data;
483
484 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200485
486 /*
487 * If the depth is larger 1, it really could be queueing. But lets
488 * make the mark a little higher - idling could still be good for
489 * low queueing, and a low queueing number could also just indicate
490 * a SCSI mid layer like behaviour where limit+1 is often seen.
491 */
492 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
493 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
497{
Jens Axboe22e2c502005-06-27 10:55:12 +0200498 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Jens Axboeb4878f22005-10-20 16:42:29 +0200500 WARN_ON(!cfqd->rq_in_driver);
501 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Jens Axboeb4878f22005-10-20 16:42:29 +0200504static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jens Axboe5e705372006-07-13 12:39:25 +0200506 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200507
Jens Axboe5e705372006-07-13 12:39:25 +0200508 if (cfqq->next_rq == rq)
509 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Jens Axboeb4878f22005-10-20 16:42:29 +0200511 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200512 cfq_del_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static int
516cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
517{
518 struct cfq_data *cfqd = q->elevator->elevator_data;
519 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jens Axboe206dc692006-03-28 13:03:44 +0200521 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200522 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200523 *req = __rq;
524 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
527 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Jens Axboe21183b02006-07-13 12:33:14 +0200530static void cfq_merged_request(request_queue_t *q, struct request *req,
531 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jens Axboe21183b02006-07-13 12:33:14 +0200533 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200534 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jens Axboe5e705372006-07-13 12:39:25 +0200536 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540static void
541cfq_merged_requests(request_queue_t *q, struct request *rq,
542 struct request *next)
543{
Jens Axboe22e2c502005-06-27 10:55:12 +0200544 /*
545 * reposition in fifo if next is older than rq
546 */
547 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
548 time_before(next->start_time, rq->start_time))
549 list_move(&rq->queuelist, &next->queuelist);
550
Jens Axboeb4878f22005-10-20 16:42:29 +0200551 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200552}
553
554static inline void
555__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
556{
557 if (cfqq) {
558 /*
559 * stop potential idle class queues waiting service
560 */
561 del_timer(&cfqd->idle_class_timer);
562
563 cfqq->slice_start = jiffies;
564 cfqq->slice_end = 0;
565 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200566 cfq_clear_cfqq_must_alloc_slice(cfqq);
567 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200568 }
569
570 cfqd->active_queue = cfqq;
571}
572
573/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100574 * current cfqq expired its slice (or was too idle), select new one
575 */
576static void
577__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
578 int preempted)
579{
580 unsigned long now = jiffies;
581
582 if (cfq_cfqq_wait_request(cfqq))
583 del_timer(&cfqd->idle_slice_timer);
584
Jens Axboe53b037442006-07-28 09:48:51 +0200585 if (!preempted && !cfq_cfqq_dispatched(cfqq))
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100586 cfq_schedule_dispatch(cfqd);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100587
588 cfq_clear_cfqq_must_dispatch(cfqq);
589 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe53b037442006-07-28 09:48:51 +0200590 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100591
592 /*
593 * store what was left of this slice, if the queue idled out
594 * or was preempted
595 */
596 if (time_after(cfqq->slice_end, now))
597 cfqq->slice_left = cfqq->slice_end - now;
598 else
599 cfqq->slice_left = 0;
600
601 if (cfq_cfqq_on_rr(cfqq))
602 cfq_resort_rr_list(cfqq, preempted);
603
604 if (cfqq == cfqd->active_queue)
605 cfqd->active_queue = NULL;
606
607 if (cfqd->active_cic) {
608 put_io_context(cfqd->active_cic->ioc);
609 cfqd->active_cic = NULL;
610 }
611
612 cfqd->dispatch_slice = 0;
613}
614
615static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
616{
617 struct cfq_queue *cfqq = cfqd->active_queue;
618
619 if (cfqq)
620 __cfq_slice_expired(cfqd, cfqq, preempted);
621}
622
623/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200624 * 0
625 * 0,1
626 * 0,1,2
627 * 0,1,2,3
628 * 0,1,2,3,4
629 * 0,1,2,3,4,5
630 * 0,1,2,3,4,5,6
631 * 0,1,2,3,4,5,6,7
632 */
633static int cfq_get_next_prio_level(struct cfq_data *cfqd)
634{
635 int prio, wrap;
636
637 prio = -1;
638 wrap = 0;
639 do {
640 int p;
641
642 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
643 if (!list_empty(&cfqd->rr_list[p])) {
644 prio = p;
645 break;
646 }
647 }
648
649 if (prio != -1)
650 break;
651 cfqd->cur_prio = 0;
652 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
653 cfqd->cur_end_prio = 0;
654 if (wrap)
655 break;
656 wrap = 1;
657 }
658 } while (1);
659
660 if (unlikely(prio == -1))
661 return -1;
662
663 BUG_ON(prio >= CFQ_PRIO_LISTS);
664
665 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
666
667 cfqd->cur_prio = prio + 1;
668 if (cfqd->cur_prio > cfqd->cur_end_prio) {
669 cfqd->cur_end_prio = cfqd->cur_prio;
670 cfqd->cur_prio = 0;
671 }
672 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
673 cfqd->cur_prio = 0;
674 cfqd->cur_end_prio = 0;
675 }
676
677 return prio;
678}
679
Jens Axboe3b181522005-06-27 10:56:24 +0200680static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200681{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100682 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200683
Jens Axboe89850f72006-07-22 16:48:31 +0200684 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
685 /*
686 * if current list is non-empty, grab first entry. if it is
687 * empty, get next prio level and grab first entry then if any
688 * are spliced
689 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200690 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200691 } else if (!list_empty(&cfqd->busy_rr)) {
692 /*
693 * If no new queues are available, check if the busy list has
694 * some before falling back to idle io.
695 */
Jens Axboee0de0202006-06-01 10:07:26 +0200696 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200697 } else if (!list_empty(&cfqd->idle_rr)) {
698 /*
699 * if we have idle queues and no rt or be queues had pending
700 * requests, either allow immediate service if the grace period
701 * has passed or arm the idle grace timer
702 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200703 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
704
705 if (time_after_eq(jiffies, end))
706 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
707 else
708 mod_timer(&cfqd->idle_class_timer, end);
709 }
710
711 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200712 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200713}
714
Jens Axboecaaa5f92006-06-16 11:23:00 +0200715#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
716
Jens Axboe22e2c502005-06-27 10:55:12 +0200717static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
718
719{
Jens Axboe206dc692006-03-28 13:03:44 +0200720 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100721 unsigned long sl;
722
Jens Axboedd67d052006-06-21 09:36:18 +0200723 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200724 WARN_ON(cfqq != cfqd->active_queue);
725
726 /*
727 * idle is disabled, either manually or by past process history
728 */
729 if (!cfqd->cfq_slice_idle)
730 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200731 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200732 return 0;
733 /*
734 * task has exited, don't wait
735 */
Jens Axboe206dc692006-03-28 13:03:44 +0200736 cic = cfqd->active_cic;
737 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200738 return 0;
739
Jens Axboe3b181522005-06-27 10:56:24 +0200740 cfq_mark_cfqq_must_dispatch(cfqq);
741 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200742
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100743 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200744
745 /*
746 * we don't want to idle for seeks, but we do want to allow
747 * fair distribution of slice time for a process doing back-to-back
748 * seeks. so allow a little bit of time for him to submit a new rq
749 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200750 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200751 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200752
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100753 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200754 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Jens Axboe5e705372006-07-13 12:39:25 +0200757static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200760 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200761
Jens Axboe5380a102006-07-13 12:37:56 +0200762 cfq_remove_request(rq);
763 cfqq->on_dispatch[rq_is_sync(rq)]++;
764 elv_dispatch_sort(q, rq);
Jens Axboefd61af02006-06-16 15:35:39 +0200765
766 rq = list_entry(q->queue_head.prev, struct request, queuelist);
767 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * return expired entry, or NULL to just start from scratch in rbtree
772 */
Jens Axboe5e705372006-07-13 12:39:25 +0200773static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200776 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200777 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jens Axboe3b181522005-06-27 10:56:24 +0200779 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return NULL;
Jens Axboe89850f72006-07-22 16:48:31 +0200781 if (list_empty(&cfqq->fifo))
782 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jens Axboe89850f72006-07-22 16:48:31 +0200784 fifo = cfq_cfqq_class_sync(cfqq);
785 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jens Axboe89850f72006-07-22 16:48:31 +0200787 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
788 cfq_mark_cfqq_fifo_expire(cfqq);
789 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 return NULL;
793}
794
795/*
Jens Axboe3b181522005-06-27 10:56:24 +0200796 * Scale schedule slice based on io priority. Use the sync time slice only
797 * if a queue is marked sync and has sync io queued. A sync queue with async
798 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200800static inline int
801cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Jens Axboe22e2c502005-06-27 10:55:12 +0200803 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jens Axboe22e2c502005-06-27 10:55:12 +0200805 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jens Axboe22e2c502005-06-27 10:55:12 +0200807 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Jens Axboe22e2c502005-06-27 10:55:12 +0200810static inline void
811cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
812{
813 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
814}
815
816static inline int
817cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
818{
819 const int base_rq = cfqd->cfq_slice_async_rq;
820
821 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
822
823 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
824}
825
826/*
827 * get next queue for service
828 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100829static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200830{
831 unsigned long now = jiffies;
832 struct cfq_queue *cfqq;
833
834 cfqq = cfqd->active_queue;
835 if (!cfqq)
836 goto new_queue;
837
838 /*
839 * slice has expired
840 */
Jens Axboe3b181522005-06-27 10:56:24 +0200841 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
842 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200843
844 /*
845 * if queue has requests, dispatch one. if not, check if
846 * enough slice is left to wait for one
847 */
Jens Axboedd67d052006-06-21 09:36:18 +0200848 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200849 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200850 else if (cfq_cfqq_dispatched(cfqq)) {
851 cfqq = NULL;
852 goto keep_queue;
853 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200854 if (cfq_arm_slice_timer(cfqd, cfqq))
855 return NULL;
856 }
857
Jens Axboe3b181522005-06-27 10:56:24 +0200858expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200859 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200860new_queue:
861 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200862keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200863 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200864}
865
866static int
867__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
868 int max_dispatch)
869{
870 int dispatched = 0;
871
Jens Axboedd67d052006-06-21 09:36:18 +0200872 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200873
874 do {
Jens Axboe5e705372006-07-13 12:39:25 +0200875 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200876
877 /*
878 * follow expired path, else get first next available
879 */
Jens Axboe5e705372006-07-13 12:39:25 +0200880 if ((rq = cfq_check_fifo(cfqq)) == NULL)
881 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200882
883 /*
884 * finally, insert request into driver dispatch list
885 */
Jens Axboe5e705372006-07-13 12:39:25 +0200886 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200887
888 cfqd->dispatch_slice++;
889 dispatched++;
890
891 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +0200892 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
893 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200894 }
895
Jens Axboedd67d052006-06-21 09:36:18 +0200896 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200897 break;
898
899 } while (dispatched < max_dispatch);
900
901 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +0200902 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +0200903 */
904 if (!cfqq->slice_end)
905 cfq_set_prio_slice(cfqd, cfqq);
906
907 /*
908 * expire an async queue immediately if it has used up its slice. idle
909 * queue always expire after 1 dispatch round.
910 */
911 if ((!cfq_cfqq_sync(cfqq) &&
912 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +0200913 cfq_class_idle(cfqq) ||
914 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200915 cfq_slice_expired(cfqd, 0);
916
917 return dispatched;
918}
919
920static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100921cfq_forced_dispatch_cfqqs(struct list_head *list)
922{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100923 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200924 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100925
Jens Axboecaaa5f92006-06-16 11:23:00 +0200926 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100927 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +0200928 while (cfqq->next_rq) {
929 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100930 dispatched++;
931 }
932 BUG_ON(!list_empty(&cfqq->fifo));
933 }
Jens Axboecaaa5f92006-06-16 11:23:00 +0200934
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100935 return dispatched;
936}
937
938static int
939cfq_forced_dispatch(struct cfq_data *cfqd)
940{
941 int i, dispatched = 0;
942
943 for (i = 0; i < CFQ_PRIO_LISTS; i++)
944 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
945
946 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
947 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
948 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
949
950 cfq_slice_expired(cfqd, 0);
951
952 BUG_ON(cfqd->busy_queues);
953
954 return dispatched;
955}
956
957static int
Jens Axboeb4878f22005-10-20 16:42:29 +0200958cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200961 struct cfq_queue *cfqq, *prev_cfqq;
962 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jens Axboe22e2c502005-06-27 10:55:12 +0200964 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100967 if (unlikely(force))
968 return cfq_forced_dispatch(cfqd);
969
Jens Axboecaaa5f92006-06-16 11:23:00 +0200970 dispatched = 0;
971 prev_cfqq = NULL;
972 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +0200973 int max_dispatch;
974
Jens Axboecaaa5f92006-06-16 11:23:00 +0200975 /*
976 * Don't repeat dispatch from the previous queue.
977 */
978 if (prev_cfqq == cfqq)
979 break;
980
Jens Axboe3b181522005-06-27 10:56:24 +0200981 cfq_clear_cfqq_must_dispatch(cfqq);
982 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200983 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100985 max_dispatch = cfqd->cfq_quantum;
986 if (cfq_class_idle(cfqq))
987 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Jens Axboecaaa5f92006-06-16 11:23:00 +0200989 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
990
991 /*
992 * If the dispatch cfqq has idling enabled and is still
993 * the active queue, break out.
994 */
995 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
996 break;
997
998 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
Jens Axboecaaa5f92006-06-16 11:23:00 +02001001 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/*
Jens Axboe5e705372006-07-13 12:39:25 +02001005 * task holds one reference to the queue, dropped when task exits. each rq
1006 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
1008 * queue lock must be held here.
1009 */
1010static void cfq_put_queue(struct cfq_queue *cfqq)
1011{
Jens Axboe22e2c502005-06-27 10:55:12 +02001012 struct cfq_data *cfqd = cfqq->cfqd;
1013
1014 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (!atomic_dec_and_test(&cfqq->ref))
1017 return;
1018
1019 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001020 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001021 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001023 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001024 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * it's on the empty list and still hashed
1028 */
1029 list_del(&cfqq->cfq_list);
1030 hlist_del(&cfqq->cfq_hash);
1031 kmem_cache_free(cfq_pool, cfqq);
1032}
1033
Jens Axboe1ea25ecb2006-07-18 22:24:11 +02001034static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001035__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1036 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001039 struct hlist_node *entry;
1040 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jens Axboe206dc692006-03-28 13:03:44 +02001042 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001043 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jens Axboe206dc692006-03-28 13:03:44 +02001045 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return __cfqq;
1047 }
1048
1049 return NULL;
1050}
1051
1052static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001053cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jens Axboe3b181522005-06-27 10:56:24 +02001055 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Jens Axboee2d74ac2006-03-28 08:59:01 +02001058static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jens Axboe22e2c502005-06-27 10:55:12 +02001060 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001061 struct rb_node *n;
1062 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001063
Jens Axboee2d74ac2006-03-28 08:59:01 +02001064 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1065 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1066 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001067 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001068 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001069 }
1070
Jens Axboe4050cf12006-07-19 05:07:12 +02001071 elv_ioc_count_mod(ioc_count, -freed);
1072
1073 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001074 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Jens Axboe89850f72006-07-22 16:48:31 +02001077static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1078{
1079 if (unlikely(cfqq == cfqd->active_queue))
1080 __cfq_slice_expired(cfqd, cfqq, 0);
1081
1082 cfq_put_queue(cfqq);
1083}
1084
1085static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1086 struct cfq_io_context *cic)
1087{
Jens Axboefc463792006-08-29 09:05:44 +02001088 list_del_init(&cic->queue_list);
1089 smp_wmb();
1090 cic->key = NULL;
1091
Jens Axboe89850f72006-07-22 16:48:31 +02001092 if (cic->cfqq[ASYNC]) {
1093 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1094 cic->cfqq[ASYNC] = NULL;
1095 }
1096
1097 if (cic->cfqq[SYNC]) {
1098 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1099 cic->cfqq[SYNC] = NULL;
1100 }
Jens Axboe89850f72006-07-22 16:48:31 +02001101}
1102
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001105 * Called with interrupts disabled
1106 */
1107static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1108{
Al Viro478a82b2006-03-18 13:25:24 -05001109 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001110
Jens Axboe89850f72006-07-22 16:48:31 +02001111 if (cfqd) {
1112 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001113
Jens Axboefc463792006-08-29 09:05:44 +02001114 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001115 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001116 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001117 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001118}
1119
Jens Axboee2d74ac2006-03-28 08:59:01 +02001120static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Jens Axboe22e2c502005-06-27 10:55:12 +02001122 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001123 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * put the reference this task is holding to the various queues
1127 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001128
1129 n = rb_first(&ioc->cic_root);
1130 while (n != NULL) {
1131 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1132
Jens Axboe22e2c502005-06-27 10:55:12 +02001133 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001134 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Jens Axboe22e2c502005-06-27 10:55:12 +02001138static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001139cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Jens Axboeb5deef92006-07-19 23:39:40 +02001141 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jens Axboeb5deef92006-07-19 23:39:40 +02001143 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001145 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001146 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001147 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001148 cic->dtor = cfq_free_io_context;
1149 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001150 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 return cic;
1154}
1155
Jens Axboe22e2c502005-06-27 10:55:12 +02001156static void cfq_init_prio_data(struct cfq_queue *cfqq)
1157{
1158 struct task_struct *tsk = current;
1159 int ioprio_class;
1160
Jens Axboe3b181522005-06-27 10:56:24 +02001161 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001162 return;
1163
1164 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1165 switch (ioprio_class) {
1166 default:
1167 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1168 case IOPRIO_CLASS_NONE:
1169 /*
1170 * no prio set, place us in the middle of the BE classes
1171 */
1172 cfqq->ioprio = task_nice_ioprio(tsk);
1173 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1174 break;
1175 case IOPRIO_CLASS_RT:
1176 cfqq->ioprio = task_ioprio(tsk);
1177 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1178 break;
1179 case IOPRIO_CLASS_BE:
1180 cfqq->ioprio = task_ioprio(tsk);
1181 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1182 break;
1183 case IOPRIO_CLASS_IDLE:
1184 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1185 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001186 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001187 break;
1188 }
1189
1190 /*
1191 * keep track of original prio settings in case we have to temporarily
1192 * elevate the priority of this queue
1193 */
1194 cfqq->org_ioprio = cfqq->ioprio;
1195 cfqq->org_ioprio_class = cfqq->ioprio_class;
1196
Jens Axboe3b181522005-06-27 10:56:24 +02001197 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001198 cfq_resort_rr_list(cfqq, 0);
1199
Jens Axboe3b181522005-06-27 10:56:24 +02001200 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001201}
1202
Al Viro478a82b2006-03-18 13:25:24 -05001203static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001204{
Al Viro478a82b2006-03-18 13:25:24 -05001205 struct cfq_data *cfqd = cic->key;
1206 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02001207
Jens Axboecaaa5f92006-06-16 11:23:00 +02001208 if (unlikely(!cfqd))
1209 return;
1210
1211 spin_lock(cfqd->queue->queue_lock);
1212
1213 cfqq = cic->cfqq[ASYNC];
1214 if (cfqq) {
1215 struct cfq_queue *new_cfqq;
1216 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1217 GFP_ATOMIC);
1218 if (new_cfqq) {
1219 cic->cfqq[ASYNC] = new_cfqq;
1220 cfq_put_queue(cfqq);
1221 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001222 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001223
1224 cfqq = cic->cfqq[SYNC];
1225 if (cfqq)
1226 cfq_mark_cfqq_prio_changed(cfqq);
1227
1228 spin_unlock(cfqd->queue->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001229}
1230
Jens Axboefc463792006-08-29 09:05:44 +02001231static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Al Viroa6a07632006-03-18 13:26:44 -05001233 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001234 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001235
Jens Axboefc463792006-08-29 09:05:44 +02001236 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001237
Jens Axboee2d74ac2006-03-28 08:59:01 +02001238 n = rb_first(&ioc->cic_root);
1239 while (n != NULL) {
1240 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001241
Al Viro478a82b2006-03-18 13:25:24 -05001242 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001243 n = rb_next(n);
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001248cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001249 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1252 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001253 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255retry:
Al Viro6f325a12006-03-18 14:58:37 -05001256 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001257 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (!cfqq) {
1260 if (new_cfqq) {
1261 cfqq = new_cfqq;
1262 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001263 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001264 /*
1265 * Inform the allocator of the fact that we will
1266 * just repeat this allocation if it fails, to allow
1267 * the allocator to do whatever it needs to attempt to
1268 * free memory.
1269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001271 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 spin_lock_irq(cfqd->queue->queue_lock);
1273 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001274 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001275 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001276 if (!cfqq)
1277 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 memset(cfqq, 0, sizeof(*cfqq));
1281
1282 INIT_HLIST_NODE(&cfqq->cfq_hash);
1283 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001284 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 cfqq->key = key;
1287 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1288 atomic_set(&cfqq->ref, 0);
1289 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001290 /*
1291 * set ->slice_left to allow preemption for a new process
1292 */
1293 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001294 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001295 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b037442006-07-28 09:48:51 +02001296 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001297 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
1300 if (new_cfqq)
1301 kmem_cache_free(cfq_pool, new_cfqq);
1302
1303 atomic_inc(&cfqq->ref);
1304out:
1305 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1306 return cfqq;
1307}
1308
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001309static void
1310cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1311{
Jens Axboefc463792006-08-29 09:05:44 +02001312 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001313 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001314 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001315 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001316}
1317
Jens Axboee2d74ac2006-03-28 08:59:01 +02001318static struct cfq_io_context *
1319cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1320{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001321 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001322 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001323 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001324
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001325restart:
1326 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001327 while (n) {
1328 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001329 /* ->key must be copied to avoid race with cfq_exit_queue() */
1330 k = cic->key;
1331 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001332 cfq_drop_dead_cic(ioc, cic);
1333 goto restart;
1334 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001335
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001336 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001337 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001338 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001339 n = n->rb_right;
1340 else
1341 return cic;
1342 }
1343
1344 return NULL;
1345}
1346
1347static inline void
1348cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1349 struct cfq_io_context *cic)
1350{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001351 struct rb_node **p;
1352 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001353 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001354 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001355
Jens Axboee2d74ac2006-03-28 08:59:01 +02001356 cic->ioc = ioc;
1357 cic->key = cfqd;
1358
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001359restart:
1360 parent = NULL;
1361 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001362 while (*p) {
1363 parent = *p;
1364 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001365 /* ->key must be copied to avoid race with cfq_exit_queue() */
1366 k = __cic->key;
1367 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001368 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001369 goto restart;
1370 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001371
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001372 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001373 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001374 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001375 p = &(*p)->rb_right;
1376 else
1377 BUG();
1378 }
1379
1380 rb_link_node(&cic->rb_node, parent, p);
1381 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001382
1383 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001384 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboefc463792006-08-29 09:05:44 +02001385 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001386}
1387
Jens Axboe22e2c502005-06-27 10:55:12 +02001388/*
1389 * Setup general io context and cfq io context. There can be several cfq
1390 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001391 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001392 */
1393static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001394cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Jens Axboe22e2c502005-06-27 10:55:12 +02001396 struct io_context *ioc = NULL;
1397 struct cfq_io_context *cic;
1398
1399 might_sleep_if(gfp_mask & __GFP_WAIT);
1400
Jens Axboeb5deef92006-07-19 23:39:40 +02001401 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001402 if (!ioc)
1403 return NULL;
1404
Jens Axboee2d74ac2006-03-28 08:59:01 +02001405 cic = cfq_cic_rb_lookup(cfqd, ioc);
1406 if (cic)
1407 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001408
Jens Axboee2d74ac2006-03-28 08:59:01 +02001409 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1410 if (cic == NULL)
1411 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001412
Jens Axboee2d74ac2006-03-28 08:59:01 +02001413 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001414out:
Jens Axboefc463792006-08-29 09:05:44 +02001415 smp_read_barrier_depends();
1416 if (unlikely(ioc->ioprio_changed))
1417 cfq_ioc_set_ioprio(ioc);
1418
Jens Axboe22e2c502005-06-27 10:55:12 +02001419 return cic;
1420err:
1421 put_io_context(ioc);
1422 return NULL;
1423}
1424
1425static void
1426cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1427{
1428 unsigned long elapsed, ttime;
1429
1430 /*
1431 * if this context already has stuff queued, thinktime is from
1432 * last queue not last end
1433 */
1434#if 0
1435 if (time_after(cic->last_end_request, cic->last_queue))
1436 elapsed = jiffies - cic->last_end_request;
1437 else
1438 elapsed = jiffies - cic->last_queue;
1439#else
1440 elapsed = jiffies - cic->last_end_request;
1441#endif
1442
1443 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1444
1445 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1446 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1447 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1448}
1449
Jens Axboe206dc692006-03-28 13:03:44 +02001450static void
1451cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
Jens Axboe5e705372006-07-13 12:39:25 +02001452 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001453{
1454 sector_t sdist;
1455 u64 total;
1456
Jens Axboe5e705372006-07-13 12:39:25 +02001457 if (cic->last_request_pos < rq->sector)
1458 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001459 else
Jens Axboe5e705372006-07-13 12:39:25 +02001460 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001461
1462 /*
1463 * Don't allow the seek distance to get too large from the
1464 * odd fragment, pagein, etc
1465 */
1466 if (cic->seek_samples <= 60) /* second&third seek */
1467 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1468 else
1469 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1470
1471 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1472 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1473 total = cic->seek_total + (cic->seek_samples/2);
1474 do_div(total, cic->seek_samples);
1475 cic->seek_mean = (sector_t)total;
1476}
Jens Axboe22e2c502005-06-27 10:55:12 +02001477
1478/*
1479 * Disable idle window if the process thinks too long or seeks so much that
1480 * it doesn't matter
1481 */
1482static void
1483cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1484 struct cfq_io_context *cic)
1485{
Jens Axboe3b181522005-06-27 10:56:24 +02001486 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001487
Jens Axboecaaa5f92006-06-16 11:23:00 +02001488 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1489 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001490 enable_idle = 0;
1491 else if (sample_valid(cic->ttime_samples)) {
1492 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1493 enable_idle = 0;
1494 else
1495 enable_idle = 1;
1496 }
1497
Jens Axboe3b181522005-06-27 10:56:24 +02001498 if (enable_idle)
1499 cfq_mark_cfqq_idle_window(cfqq);
1500 else
1501 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001502}
1503
1504
1505/*
1506 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1507 * no or if we aren't sure, a 1 will cause a preempt.
1508 */
1509static int
1510cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001511 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001512{
1513 struct cfq_queue *cfqq = cfqd->active_queue;
1514
1515 if (cfq_class_idle(new_cfqq))
1516 return 0;
1517
1518 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001519 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001520
1521 if (cfq_class_idle(cfqq))
1522 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001523 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001524 return 0;
1525 /*
1526 * if it doesn't have slice left, forget it
1527 */
1528 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1529 return 0;
Jens Axboe5e705372006-07-13 12:39:25 +02001530 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001531 return 1;
1532
1533 return 0;
1534}
1535
1536/*
1537 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1538 * let it have half of its nominal slice.
1539 */
1540static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1541{
1542 struct cfq_queue *__cfqq, *next;
1543
1544 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1545 cfq_resort_rr_list(__cfqq, 1);
1546
1547 if (!cfqq->slice_left)
1548 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1549
1550 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001551 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001552 __cfq_set_active_queue(cfqd, cfqq);
1553}
1554
1555/*
Jens Axboe5e705372006-07-13 12:39:25 +02001556 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001557 * something we should do about it
1558 */
1559static void
Jens Axboe5e705372006-07-13 12:39:25 +02001560cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1561 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001562{
Jens Axboe5e705372006-07-13 12:39:25 +02001563 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001564
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001565 /*
Jens Axboe5380a102006-07-13 12:37:56 +02001566 * check if this request is a better next-serve candidate)) {
Jens Axboe21183b02006-07-13 12:33:14 +02001567 */
Jens Axboe5e705372006-07-13 12:39:25 +02001568 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
1569 BUG_ON(!cfqq->next_rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001570
1571 /*
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001572 * we never wait for an async request and we don't allow preemption
1573 * of an async request. so just return early
1574 */
Jens Axboe5e705372006-07-13 12:39:25 +02001575 if (!rq_is_sync(rq)) {
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001576 /*
1577 * sync process issued an async request, if it's waiting
1578 * then expire it and kick rq handling.
1579 */
1580 if (cic == cfqd->active_cic &&
1581 del_timer(&cfqd->idle_slice_timer)) {
1582 cfq_slice_expired(cfqd, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +02001583 blk_start_queueing(cfqd->queue);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001584 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001585 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001586 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001587
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001588 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe5e705372006-07-13 12:39:25 +02001589 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001590 cfq_update_idle_window(cfqd, cfqq, cic);
1591
1592 cic->last_queue = jiffies;
Jens Axboe5e705372006-07-13 12:39:25 +02001593 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001594
1595 if (cfqq == cfqd->active_queue) {
1596 /*
1597 * if we are waiting for a request for this queue, let it rip
1598 * immediately and flag that we must not expire this queue
1599 * just now
1600 */
Jens Axboe3b181522005-06-27 10:56:24 +02001601 if (cfq_cfqq_wait_request(cfqq)) {
1602 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001603 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001604 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001605 }
Jens Axboe5e705372006-07-13 12:39:25 +02001606 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001607 /*
1608 * not the active queue - expire current slice if it is
1609 * idle and has expired it's mean thinktime or this new queue
1610 * has some old slice time left and is of higher priority
1611 */
1612 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001613 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001614 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001615 }
1616}
1617
Jens Axboeb4878f22005-10-20 16:42:29 +02001618static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001619{
Jens Axboeb4878f22005-10-20 16:42:29 +02001620 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001621 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001622
1623 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Jens Axboe5e705372006-07-13 12:39:25 +02001625 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jens Axboe21183b02006-07-13 12:33:14 +02001627 if (!cfq_cfqq_on_rr(cfqq))
1628 cfq_add_cfqq_rr(cfqd, cfqq);
1629
Jens Axboe22e2c502005-06-27 10:55:12 +02001630 list_add_tail(&rq->queuelist, &cfqq->fifo);
1631
Jens Axboe5e705372006-07-13 12:39:25 +02001632 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635static void cfq_completed_request(request_queue_t *q, struct request *rq)
1636{
Jens Axboe5e705372006-07-13 12:39:25 +02001637 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001638 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001639 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001640 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jens Axboeb4878f22005-10-20 16:42:29 +02001642 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jens Axboeb4878f22005-10-20 16:42:29 +02001644 WARN_ON(!cfqd->rq_in_driver);
1645 WARN_ON(!cfqq->on_dispatch[sync]);
1646 cfqd->rq_in_driver--;
1647 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Jens Axboeb4878f22005-10-20 16:42:29 +02001649 if (!cfq_class_idle(cfqq))
1650 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001651
Jens Axboe53b037442006-07-28 09:48:51 +02001652 if (!cfq_cfqq_dispatched(cfqq) && cfq_cfqq_on_rr(cfqq))
1653 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jens Axboecaaa5f92006-06-16 11:23:00 +02001655 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001656 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001657
1658 /*
1659 * If this is the active queue, check if it needs to be expired,
1660 * or if we want to idle in case it has no pending requests.
1661 */
1662 if (cfqd->active_queue == cfqq) {
1663 if (time_after(now, cfqq->slice_end))
1664 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001665 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001666 if (!cfq_arm_slice_timer(cfqd, cfqq))
1667 cfq_schedule_dispatch(cfqd);
1668 }
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Jens Axboe22e2c502005-06-27 10:55:12 +02001672/*
1673 * we temporarily boost lower priority queues if they are holding fs exclusive
1674 * resources. they are boosted to normal prio (CLASS_BE/4)
1675 */
1676static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Jens Axboe22e2c502005-06-27 10:55:12 +02001678 const int ioprio_class = cfqq->ioprio_class;
1679 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Jens Axboe22e2c502005-06-27 10:55:12 +02001681 if (has_fs_excl()) {
1682 /*
1683 * boost idle prio on transactions that would lock out other
1684 * users of the filesystem
1685 */
1686 if (cfq_class_idle(cfqq))
1687 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1688 if (cfqq->ioprio > IOPRIO_NORM)
1689 cfqq->ioprio = IOPRIO_NORM;
1690 } else {
1691 /*
1692 * check if we need to unboost the queue
1693 */
1694 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1695 cfqq->ioprio_class = cfqq->org_ioprio_class;
1696 if (cfqq->ioprio != cfqq->org_ioprio)
1697 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699
Jens Axboe22e2c502005-06-27 10:55:12 +02001700 /*
1701 * refile between round-robin lists if we moved the priority class
1702 */
1703 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001704 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001705 cfq_resort_rr_list(cfqq, 0);
1706}
1707
Jens Axboe89850f72006-07-22 16:48:31 +02001708static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001709{
Jens Axboe3b181522005-06-27 10:56:24 +02001710 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001711 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001712 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001713 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001714 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001715
1716 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001717}
1718
Jens Axboecb78b282006-07-28 09:32:57 +02001719static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001720{
1721 struct cfq_data *cfqd = q->elevator->elevator_data;
1722 struct task_struct *tsk = current;
1723 struct cfq_queue *cfqq;
1724
1725 /*
1726 * don't force setup of a queue from here, as a call to may_queue
1727 * does not necessarily imply that a request actually will be queued.
1728 * so just lookup a possibly existing queue, or return 'may queue'
1729 * if that fails
1730 */
Jens Axboe3b181522005-06-27 10:56:24 +02001731 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001732 if (cfqq) {
1733 cfq_init_prio_data(cfqq);
1734 cfq_prio_boost(cfqq);
1735
Jens Axboe89850f72006-07-22 16:48:31 +02001736 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001737 }
1738
1739 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/*
1743 * queue lock held here
1744 */
1745static void cfq_put_request(request_queue_t *q, struct request *rq)
1746{
Jens Axboe5e705372006-07-13 12:39:25 +02001747 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Jens Axboe5e705372006-07-13 12:39:25 +02001749 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001750 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jens Axboe22e2c502005-06-27 10:55:12 +02001752 BUG_ON(!cfqq->allocated[rw]);
1753 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jens Axboe5e705372006-07-13 12:39:25 +02001755 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001758 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 cfq_put_queue(cfqq);
1761 }
1762}
1763
1764/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001765 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001767static int
Jens Axboecb78b282006-07-28 09:32:57 +02001768cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001771 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct cfq_io_context *cic;
1773 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001774 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001775 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001777 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 might_sleep_if(gfp_mask & __GFP_WAIT);
1780
Jens Axboee2d74ac2006-03-28 08:59:01 +02001781 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 spin_lock_irqsave(q->queue_lock, flags);
1784
Jens Axboe22e2c502005-06-27 10:55:12 +02001785 if (!cic)
1786 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Al Viro12a05732006-03-18 13:38:01 -05001788 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001789 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001790 if (!cfqq)
1791 goto queue_fail;
1792
Al Viro12a05732006-03-18 13:38:01 -05001793 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001794 } else
Al Viro12a05732006-03-18 13:38:01 -05001795 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001798 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001799 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 spin_unlock_irqrestore(q->queue_lock, flags);
1802
Jens Axboe5e705372006-07-13 12:39:25 +02001803 rq->elevator_private = cic;
1804 rq->elevator_private2 = cfqq;
1805 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001806
Jens Axboe22e2c502005-06-27 10:55:12 +02001807queue_fail:
1808 if (cic)
1809 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001810
Jens Axboe3b181522005-06-27 10:56:24 +02001811 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 spin_unlock_irqrestore(q->queue_lock, flags);
1813 return 1;
1814}
1815
Jens Axboe22e2c502005-06-27 10:55:12 +02001816static void cfq_kick_queue(void *data)
1817{
1818 request_queue_t *q = data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001819 unsigned long flags;
1820
1821 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001822 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001823 spin_unlock_irqrestore(q->queue_lock, flags);
1824}
1825
1826/*
1827 * Timer running if the active_queue is currently idling inside its time slice
1828 */
1829static void cfq_idle_slice_timer(unsigned long data)
1830{
1831 struct cfq_data *cfqd = (struct cfq_data *) data;
1832 struct cfq_queue *cfqq;
1833 unsigned long flags;
1834
1835 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1836
1837 if ((cfqq = cfqd->active_queue) != NULL) {
1838 unsigned long now = jiffies;
1839
1840 /*
1841 * expired
1842 */
1843 if (time_after(now, cfqq->slice_end))
1844 goto expire;
1845
1846 /*
1847 * only expire and reinvoke request handler, if there are
1848 * other queues with pending requests
1849 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001850 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001851 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001852
1853 /*
1854 * not expired and it has a request pending, let it dispatch
1855 */
Jens Axboedd67d052006-06-21 09:36:18 +02001856 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001857 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 goto out_kick;
1859 }
1860 }
1861expire:
1862 cfq_slice_expired(cfqd, 0);
1863out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001864 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001865out_cont:
1866 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1867}
1868
1869/*
1870 * Timer running if an idle class queue is waiting for service
1871 */
1872static void cfq_idle_class_timer(unsigned long data)
1873{
1874 struct cfq_data *cfqd = (struct cfq_data *) data;
1875 unsigned long flags, end;
1876
1877 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1878
1879 /*
1880 * race with a non-idle queue, reset timer
1881 */
1882 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001883 if (!time_after_eq(jiffies, end))
1884 mod_timer(&cfqd->idle_class_timer, end);
1885 else
Jens Axboe3b181522005-06-27 10:56:24 +02001886 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001887
1888 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1889}
1890
Jens Axboe3b181522005-06-27 10:56:24 +02001891static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1892{
1893 del_timer_sync(&cfqd->idle_slice_timer);
1894 del_timer_sync(&cfqd->idle_class_timer);
1895 blk_sync_queue(cfqd->queue);
1896}
Jens Axboe22e2c502005-06-27 10:55:12 +02001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898static void cfq_exit_queue(elevator_t *e)
1899{
Jens Axboe22e2c502005-06-27 10:55:12 +02001900 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05001901 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001902
Jens Axboe3b181522005-06-27 10:56:24 +02001903 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001904
Al Virod9ff4182006-03-18 13:51:22 -05001905 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001906
Al Virod9ff4182006-03-18 13:51:22 -05001907 if (cfqd->active_queue)
1908 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001909
1910 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05001911 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1912 struct cfq_io_context,
1913 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02001914
1915 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05001916 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001917
Al Virod9ff4182006-03-18 13:51:22 -05001918 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05001919
1920 cfq_shutdown_timer_wq(cfqd);
1921
Al Viroa90d7422006-03-18 12:05:37 -05001922 kfree(cfqd->cfq_hash);
1923 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Jens Axboebc1c1162006-06-08 08:49:06 +02001926static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 struct cfq_data *cfqd;
1929 int i;
1930
Jens Axboeb5deef92006-07-19 23:39:40 +02001931 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02001933 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02001936
1937 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1938 INIT_LIST_HEAD(&cfqd->rr_list[i]);
1939
1940 INIT_LIST_HEAD(&cfqd->busy_rr);
1941 INIT_LIST_HEAD(&cfqd->cur_rr);
1942 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05001943 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Jens Axboeb5deef92006-07-19 23:39:40 +02001945 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02001947 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
1950 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Jens Axboe22e2c502005-06-27 10:55:12 +02001954 init_timer(&cfqd->idle_slice_timer);
1955 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
1956 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
1957
1958 init_timer(&cfqd->idle_class_timer);
1959 cfqd->idle_class_timer.function = cfq_idle_class_timer;
1960 cfqd->idle_class_timer.data = (unsigned long) cfqd;
1961
1962 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02001965 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
1966 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 cfqd->cfq_back_max = cfq_back_max;
1968 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02001969 cfqd->cfq_slice[0] = cfq_slice_async;
1970 cfqd->cfq_slice[1] = cfq_slice_sync;
1971 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
1972 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001973
Jens Axboebc1c1162006-06-08 08:49:06 +02001974 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02001975out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02001977 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980static void cfq_slab_kill(void)
1981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (cfq_pool)
1983 kmem_cache_destroy(cfq_pool);
1984 if (cfq_ioc_pool)
1985 kmem_cache_destroy(cfq_ioc_pool);
1986}
1987
1988static int __init cfq_slab_setup(void)
1989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
1991 NULL, NULL);
1992 if (!cfq_pool)
1993 goto fail;
1994
1995 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
1996 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
1997 if (!cfq_ioc_pool)
1998 goto fail;
1999
2000 return 0;
2001fail:
2002 cfq_slab_kill();
2003 return -ENOMEM;
2004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
2007 * sysfs parts below -->
2008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010static ssize_t
2011cfq_var_show(unsigned int var, char *page)
2012{
2013 return sprintf(page, "%d\n", var);
2014}
2015
2016static ssize_t
2017cfq_var_store(unsigned int *var, const char *page, size_t count)
2018{
2019 char *p = (char *) page;
2020
2021 *var = simple_strtoul(p, &p, 10);
2022 return count;
2023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002026static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002028 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 unsigned int __data = __VAR; \
2030 if (__CONV) \
2031 __data = jiffies_to_msecs(__data); \
2032 return cfq_var_show(__data, (page)); \
2033}
2034SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002035SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2036SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002037SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2038SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002039SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2040SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2041SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2042SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043#undef SHOW_FUNCTION
2044
2045#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002046static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002048 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 unsigned int __data; \
2050 int ret = cfq_var_store(&__data, (page), count); \
2051 if (__data < (MIN)) \
2052 __data = (MIN); \
2053 else if (__data > (MAX)) \
2054 __data = (MAX); \
2055 if (__CONV) \
2056 *(__PTR) = msecs_to_jiffies(__data); \
2057 else \
2058 *(__PTR) = __data; \
2059 return ret; \
2060}
2061STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002062STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2063STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002064STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2065STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002066STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2067STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2068STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2069STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#undef STORE_FUNCTION
2071
Al Viroe572ec72006-03-18 22:27:18 -05002072#define CFQ_ATTR(name) \
2073 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002074
Al Viroe572ec72006-03-18 22:27:18 -05002075static struct elv_fs_entry cfq_attrs[] = {
2076 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002077 CFQ_ATTR(fifo_expire_sync),
2078 CFQ_ATTR(fifo_expire_async),
2079 CFQ_ATTR(back_seek_max),
2080 CFQ_ATTR(back_seek_penalty),
2081 CFQ_ATTR(slice_sync),
2082 CFQ_ATTR(slice_async),
2083 CFQ_ATTR(slice_async_rq),
2084 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002085 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086};
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static struct elevator_type iosched_cfq = {
2089 .ops = {
2090 .elevator_merge_fn = cfq_merge,
2091 .elevator_merged_fn = cfq_merged_request,
2092 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002093 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002095 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 .elevator_deactivate_req_fn = cfq_deactivate_request,
2097 .elevator_queue_empty_fn = cfq_queue_empty,
2098 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002099 .elevator_former_req_fn = elv_rb_former_request,
2100 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 .elevator_set_req_fn = cfq_set_request,
2102 .elevator_put_req_fn = cfq_put_request,
2103 .elevator_may_queue_fn = cfq_may_queue,
2104 .elevator_init_fn = cfq_init_queue,
2105 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002106 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 },
Al Viro3d1ab402006-03-18 18:35:43 -05002108 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 .elevator_name = "cfq",
2110 .elevator_owner = THIS_MODULE,
2111};
2112
2113static int __init cfq_init(void)
2114{
2115 int ret;
2116
Jens Axboe22e2c502005-06-27 10:55:12 +02002117 /*
2118 * could be 0 on HZ < 1000 setups
2119 */
2120 if (!cfq_slice_async)
2121 cfq_slice_async = 1;
2122 if (!cfq_slice_idle)
2123 cfq_slice_idle = 1;
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (cfq_slab_setup())
2126 return -ENOMEM;
2127
2128 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002129 if (ret)
2130 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return ret;
2133}
2134
2135static void __exit cfq_exit(void)
2136{
Al Viro334e94d2006-03-18 15:05:53 -05002137 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002139 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002140 /* ioc_gone's update must be visible before reading ioc_count */
2141 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002142 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002143 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002144 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002145 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148module_init(cfq_init);
2149module_exit(cfq_exit);
2150
2151MODULE_AUTHOR("Jens Axboe");
2152MODULE_LICENSE("GPL");
2153MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");