blob: ec24284e9d397048f4c4e5ee162363adca45d8b6 [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
Jens Axboe3793c652006-05-30 21:11:04 +020034static DEFINE_SPINLOCK(cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -050035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * for the hash of cfqq inside the cfqd
38 */
39#define CFQ_QHASH_SHIFT 6
40#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
41#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
44
Jens Axboe5e705372006-07-13 12:39:25 +020045#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
46#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static kmem_cache_t *cfq_pool;
49static kmem_cache_t *cfq_ioc_pool;
50
Al Viro334e94d2006-03-18 15:05:53 -050051static atomic_t ioc_count = ATOMIC_INIT(0);
52static struct completion *ioc_gone;
53
Jens Axboe22e2c502005-06-27 10:55:12 +020054#define CFQ_PRIO_LISTS IOPRIO_BE_NR
55#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020056#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
57
Jens Axboe3b181522005-06-27 10:56:24 +020058#define ASYNC (0)
59#define SYNC (1)
60
61#define cfq_cfqq_dispatched(cfqq) \
62 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
63
64#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
65
66#define cfq_cfqq_sync(cfqq) \
67 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020068
Jens Axboe206dc692006-03-28 13:03:44 +020069#define sample_valid(samples) ((samples) > 80)
70
Jens Axboe22e2c502005-06-27 10:55:12 +020071/*
72 * Per block device queue structure
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020075 request_queue_t *queue;
76
77 /*
78 * rr list of queues with requests and the count of them
79 */
80 struct list_head rr_list[CFQ_PRIO_LISTS];
81 struct list_head busy_rr;
82 struct list_head cur_rr;
83 struct list_head idle_rr;
84 unsigned int busy_queues;
85
86 /*
87 * non-ordered list of empty cfqq's
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct list_head empty_list;
90
Jens Axboe22e2c502005-06-27 10:55:12 +020091 /*
92 * cfqq lookup hash
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jens Axboe22e2c502005-06-27 10:55:12 +020096 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020097 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020098
99 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200100 * idle window management
101 */
102 struct timer_list idle_slice_timer;
103 struct work_struct unplug_work;
104
105 struct cfq_queue *active_queue;
106 struct cfq_io_context *active_cic;
107 int cur_prio, cur_end_prio;
108 unsigned int dispatch_slice;
109
110 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200113 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * tunables, see top of file
117 */
118 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200119 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int cfq_back_penalty;
121 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200122 unsigned int cfq_slice[2];
123 unsigned int cfq_slice_async_rq;
124 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500125
126 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Jens Axboe22e2c502005-06-27 10:55:12 +0200129/*
130 * Per process-grouping structure
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132struct cfq_queue {
133 /* reference count */
134 atomic_t ref;
135 /* parent cfq_data */
136 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200137 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct hlist_node cfq_hash;
139 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200140 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* on either rr or empty list of cfqd */
142 struct list_head cfq_list;
143 /* sorted list of pending requests */
144 struct rb_root sort_list;
145 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200146 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* requests queued in sort_list */
148 int queued[2];
149 /* currently allocated requests */
150 int allocated[2];
151 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200152 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jens Axboe22e2c502005-06-27 10:55:12 +0200154 unsigned long slice_start;
155 unsigned long slice_end;
156 unsigned long slice_left;
157 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jens Axboe3b181522005-06-27 10:56:24 +0200159 /* number of requests that are on the dispatch list */
160 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200161
162 /* io prio of this group */
163 unsigned short ioprio, org_ioprio;
164 unsigned short ioprio_class, org_ioprio_class;
165
Jens Axboe3b181522005-06-27 10:56:24 +0200166 /* various state flags, see below */
167 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
Jens Axboe3b181522005-06-27 10:56:24 +0200170enum cfqq_state_flags {
171 CFQ_CFQQ_FLAG_on_rr = 0,
172 CFQ_CFQQ_FLAG_wait_request,
173 CFQ_CFQQ_FLAG_must_alloc,
174 CFQ_CFQQ_FLAG_must_alloc_slice,
175 CFQ_CFQQ_FLAG_must_dispatch,
176 CFQ_CFQQ_FLAG_fifo_expire,
177 CFQ_CFQQ_FLAG_idle_window,
178 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200179};
180
181#define CFQ_CFQQ_FNS(name) \
182static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
183{ \
184 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
185} \
186static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
187{ \
188 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
189} \
190static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
191{ \
192 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
193}
194
195CFQ_CFQQ_FNS(on_rr);
196CFQ_CFQQ_FNS(wait_request);
197CFQ_CFQQ_FNS(must_alloc);
198CFQ_CFQQ_FNS(must_alloc_slice);
199CFQ_CFQQ_FNS(must_dispatch);
200CFQ_CFQQ_FNS(fifo_expire);
201CFQ_CFQQ_FNS(idle_window);
202CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200203#undef CFQ_CFQQ_FNS
204
Jens Axboe3b181522005-06-27 10:56:24 +0200205static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200206static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500207static 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 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700210 * scheduler run of queue, if there are requests pending and no one in the
211 * driver that will restart queueing
212 */
213static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
214{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100215 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700216 kblockd_schedule_work(&cfqd->unplug_work);
217}
218
219static int cfq_queue_empty(request_queue_t *q)
220{
221 struct cfq_data *cfqd = q->elevator->elevator_data;
222
Jens Axboeb4878f22005-10-20 16:42:29 +0200223 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700224}
225
Jens Axboe206dc692006-03-28 13:03:44 +0200226static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
227{
Jens Axboeb31dc662006-06-13 08:26:10 +0200228 if (rw == READ || rw == WRITE_SYNC)
Jens Axboe206dc692006-03-28 13:03:44 +0200229 return task->pid;
230
231 return CFQ_KEY_ASYNC;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
Jens Axboe5e705372006-07-13 12:39:25 +0200235 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200237 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Jens Axboe5e705372006-07-13 12:39:25 +0200239static struct request *
240cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200244#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
245#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
246 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jens Axboe5e705372006-07-13 12:39:25 +0200248 if (rq1 == NULL || rq1 == rq2)
249 return rq2;
250 if (rq2 == NULL)
251 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200252
Jens Axboe5e705372006-07-13 12:39:25 +0200253 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
254 return rq1;
255 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
256 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jens Axboe5e705372006-07-13 12:39:25 +0200258 s1 = rq1->sector;
259 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 last = cfqd->last_sector;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
264 * by definition, 1KiB is 2 sectors
265 */
266 back_max = cfqd->cfq_back_max * 2;
267
268 /*
269 * Strict one way elevator _except_ in the case where we allow
270 * short backward seeks which are biased as twice the cost of a
271 * similar forward seek.
272 */
273 if (s1 >= last)
274 d1 = s1 - last;
275 else if (s1 + back_max >= last)
276 d1 = (last - s1) * cfqd->cfq_back_penalty;
277 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200278 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (s2 >= last)
281 d2 = s2 - last;
282 else if (s2 + back_max >= last)
283 d2 = (last - s2) * cfqd->cfq_back_penalty;
284 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200285 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Andreas Mohre8a99052006-03-28 08:59:49 +0200289 /*
290 * By doing switch() on the bit mask "wrap" we avoid having to
291 * check two variables for all permutations: --> faster!
292 */
293 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200294 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200295 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200296 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200297 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200298 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200299 else {
300 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200301 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200302 else
Jens Axboe5e705372006-07-13 12:39:25 +0200303 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200304 }
305
306 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200307 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200308 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200309 return rq2;
310 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200311 default:
312 /*
313 * Since both rqs are wrapped,
314 * start with the one that's further behind head
315 * (--> only *one* back seek required),
316 * since back seek takes more time than forward.
317 */
318 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200319 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 else
Jens Axboe5e705372006-07-13 12:39:25 +0200321 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323}
324
325/*
326 * would be nice to take fifo expire time into account as well
327 */
Jens Axboe5e705372006-07-13 12:39:25 +0200328static struct request *
329cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
330 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Jens Axboe21183b02006-07-13 12:33:14 +0200332 struct rb_node *rbnext = rb_next(&last->rb_node);
333 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200334 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Jens Axboe21183b02006-07-13 12:33:14 +0200336 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200339 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200342 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200343 else {
344 rbnext = rb_first(&cfqq->sort_list);
345 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200346 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jens Axboe21183b02006-07-13 12:33:14 +0200349 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Jens Axboe22e2c502005-06-27 10:55:12 +0200352static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Jens Axboe22e2c502005-06-27 10:55:12 +0200354 struct cfq_data *cfqd = cfqq->cfqd;
355 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jens Axboe3b181522005-06-27 10:56:24 +0200357 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 list_del(&cfqq->cfq_list);
360
Jens Axboe22e2c502005-06-27 10:55:12 +0200361 if (cfq_class_rt(cfqq))
362 list = &cfqd->cur_rr;
363 else if (cfq_class_idle(cfqq))
364 list = &cfqd->idle_rr;
365 else {
366 /*
367 * if cfqq has requests in flight, don't allow it to be
368 * found in cfq_set_active_queue before it has finished them.
369 * this is done to increase fairness between a process that
370 * has lots of io pending vs one that only generates one
371 * sporadically or synchronously
372 */
Jens Axboe3b181522005-06-27 10:56:24 +0200373 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200374 list = &cfqd->busy_rr;
375 else
376 list = &cfqd->rr_list[cfqq->ioprio];
377 }
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200380 * if queue was preempted, just add to front to be fair. busy_rr
Jens Axboeb52a8342006-06-01 18:53:43 +0200381 * isn't sorted, but insert at the back for fairness.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200383 if (preempted || list == &cfqd->busy_rr) {
Jens Axboeb52a8342006-06-01 18:53:43 +0200384 if (preempted)
385 list = list->prev;
386
387 list_add_tail(&cfqq->cfq_list, list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200388 return;
389 }
390
391 /*
392 * sort by when queue was last serviced
393 */
394 entry = list;
395 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
397
Jens Axboe22e2c502005-06-27 10:55:12 +0200398 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200400 if (time_before(__cfqq->service_last, cfqq->service_last))
401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 list_add(&cfqq->cfq_list, entry);
405}
406
407/*
408 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200409 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
411static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200412cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Jens Axboe3b181522005-06-27 10:56:24 +0200414 BUG_ON(cfq_cfqq_on_rr(cfqq));
415 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 cfqd->busy_queues++;
417
Jens Axboeb4878f22005-10-20 16:42:29 +0200418 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static inline void
422cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
423{
Jens Axboe3b181522005-06-27 10:56:24 +0200424 BUG_ON(!cfq_cfqq_on_rr(cfqq));
425 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200426 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 BUG_ON(!cfqd->busy_queues);
429 cfqd->busy_queues--;
430}
431
432/*
433 * rb tree support functions
434 */
Jens Axboe5e705372006-07-13 12:39:25 +0200435static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Jens Axboe5e705372006-07-13 12:39:25 +0200437 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200438 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200439 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Jens Axboeb4878f22005-10-20 16:42:29 +0200441 BUG_ON(!cfqq->queued[sync]);
442 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jens Axboe5e705372006-07-13 12:39:25 +0200444 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jens Axboedd67d052006-06-21 09:36:18 +0200446 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200447 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Jens Axboe5e705372006-07-13 12:39:25 +0200450static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jens Axboe5e705372006-07-13 12:39:25 +0200452 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200454 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jens Axboe5380a102006-07-13 12:37:56 +0200456 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /*
459 * looks a little odd, but the first insert might return an alias.
460 * if that happens, put the alias on the dispatch list
461 */
Jens Axboe21183b02006-07-13 12:33:14 +0200462 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200463 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200467cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jens Axboe5380a102006-07-13 12:37:56 +0200469 elv_rb_del(&cfqq->sort_list, rq);
470 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200471 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Jens Axboe206dc692006-03-28 13:03:44 +0200474static struct request *
475cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jens Axboe206dc692006-03-28 13:03:44 +0200477 struct task_struct *tsk = current;
478 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
479 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jens Axboe206dc692006-03-28 13:03:44 +0200481 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200482 if (cfqq) {
483 sector_t sector = bio->bi_sector + bio_sectors(bio);
484
Jens Axboe21183b02006-07-13 12:33:14 +0200485 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return NULL;
489}
490
Jens Axboeb4878f22005-10-20 16:42:29 +0200491static void cfq_activate_request(request_queue_t *q, struct request *rq)
492{
493 struct cfq_data *cfqd = q->elevator->elevator_data;
494
495 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200496
497 /*
498 * If the depth is larger 1, it really could be queueing. But lets
499 * make the mark a little higher - idling could still be good for
500 * low queueing, and a low queueing number could also just indicate
501 * a SCSI mid layer like behaviour where limit+1 is often seen.
502 */
503 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
504 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
508{
Jens Axboe22e2c502005-06-27 10:55:12 +0200509 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Jens Axboeb4878f22005-10-20 16:42:29 +0200511 WARN_ON(!cfqd->rq_in_driver);
512 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Jens Axboeb4878f22005-10-20 16:42:29 +0200515static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jens Axboe5e705372006-07-13 12:39:25 +0200517 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200518
Jens Axboe5e705372006-07-13 12:39:25 +0200519 if (cfqq->next_rq == rq)
520 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jens Axboeb4878f22005-10-20 16:42:29 +0200522 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200523 cfq_del_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526static int
527cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
528{
529 struct cfq_data *cfqd = q->elevator->elevator_data;
530 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jens Axboe206dc692006-03-28 13:03:44 +0200532 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200533 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200534 *req = __rq;
535 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Jens Axboe21183b02006-07-13 12:33:14 +0200541static void cfq_merged_request(request_queue_t *q, struct request *req,
542 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jens Axboe21183b02006-07-13 12:33:14 +0200544 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200545 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jens Axboe5e705372006-07-13 12:39:25 +0200547 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static void
552cfq_merged_requests(request_queue_t *q, struct request *rq,
553 struct request *next)
554{
Jens Axboe22e2c502005-06-27 10:55:12 +0200555 /*
556 * reposition in fifo if next is older than rq
557 */
558 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
559 time_before(next->start_time, rq->start_time))
560 list_move(&rq->queuelist, &next->queuelist);
561
Jens Axboeb4878f22005-10-20 16:42:29 +0200562 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200563}
564
565static inline void
566__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
567{
568 if (cfqq) {
569 /*
570 * stop potential idle class queues waiting service
571 */
572 del_timer(&cfqd->idle_class_timer);
573
574 cfqq->slice_start = jiffies;
575 cfqq->slice_end = 0;
576 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200577 cfq_clear_cfqq_must_alloc_slice(cfqq);
578 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200579 }
580
581 cfqd->active_queue = cfqq;
582}
583
584/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100585 * current cfqq expired its slice (or was too idle), select new one
586 */
587static void
588__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
589 int preempted)
590{
591 unsigned long now = jiffies;
592
593 if (cfq_cfqq_wait_request(cfqq))
594 del_timer(&cfqd->idle_slice_timer);
595
596 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
597 cfqq->service_last = now;
598 cfq_schedule_dispatch(cfqd);
599 }
600
601 cfq_clear_cfqq_must_dispatch(cfqq);
602 cfq_clear_cfqq_wait_request(cfqq);
603
604 /*
605 * store what was left of this slice, if the queue idled out
606 * or was preempted
607 */
608 if (time_after(cfqq->slice_end, now))
609 cfqq->slice_left = cfqq->slice_end - now;
610 else
611 cfqq->slice_left = 0;
612
613 if (cfq_cfqq_on_rr(cfqq))
614 cfq_resort_rr_list(cfqq, preempted);
615
616 if (cfqq == cfqd->active_queue)
617 cfqd->active_queue = NULL;
618
619 if (cfqd->active_cic) {
620 put_io_context(cfqd->active_cic->ioc);
621 cfqd->active_cic = NULL;
622 }
623
624 cfqd->dispatch_slice = 0;
625}
626
627static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
628{
629 struct cfq_queue *cfqq = cfqd->active_queue;
630
631 if (cfqq)
632 __cfq_slice_expired(cfqd, cfqq, preempted);
633}
634
635/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200636 * 0
637 * 0,1
638 * 0,1,2
639 * 0,1,2,3
640 * 0,1,2,3,4
641 * 0,1,2,3,4,5
642 * 0,1,2,3,4,5,6
643 * 0,1,2,3,4,5,6,7
644 */
645static int cfq_get_next_prio_level(struct cfq_data *cfqd)
646{
647 int prio, wrap;
648
649 prio = -1;
650 wrap = 0;
651 do {
652 int p;
653
654 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
655 if (!list_empty(&cfqd->rr_list[p])) {
656 prio = p;
657 break;
658 }
659 }
660
661 if (prio != -1)
662 break;
663 cfqd->cur_prio = 0;
664 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
665 cfqd->cur_end_prio = 0;
666 if (wrap)
667 break;
668 wrap = 1;
669 }
670 } while (1);
671
672 if (unlikely(prio == -1))
673 return -1;
674
675 BUG_ON(prio >= CFQ_PRIO_LISTS);
676
677 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
678
679 cfqd->cur_prio = prio + 1;
680 if (cfqd->cur_prio > cfqd->cur_end_prio) {
681 cfqd->cur_end_prio = cfqd->cur_prio;
682 cfqd->cur_prio = 0;
683 }
684 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
685 cfqd->cur_prio = 0;
686 cfqd->cur_end_prio = 0;
687 }
688
689 return prio;
690}
691
Jens Axboe3b181522005-06-27 10:56:24 +0200692static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200693{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100694 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200695
Jens Axboe89850f72006-07-22 16:48:31 +0200696 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
697 /*
698 * if current list is non-empty, grab first entry. if it is
699 * empty, get next prio level and grab first entry then if any
700 * are spliced
701 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200702 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200703 } else if (!list_empty(&cfqd->busy_rr)) {
704 /*
705 * If no new queues are available, check if the busy list has
706 * some before falling back to idle io.
707 */
Jens Axboee0de0202006-06-01 10:07:26 +0200708 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200709 } else if (!list_empty(&cfqd->idle_rr)) {
710 /*
711 * if we have idle queues and no rt or be queues had pending
712 * requests, either allow immediate service if the grace period
713 * has passed or arm the idle grace timer
714 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200715 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
716
717 if (time_after_eq(jiffies, end))
718 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
719 else
720 mod_timer(&cfqd->idle_class_timer, end);
721 }
722
723 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200724 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200725}
726
Jens Axboecaaa5f92006-06-16 11:23:00 +0200727#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
728
Jens Axboe22e2c502005-06-27 10:55:12 +0200729static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
730
731{
Jens Axboe206dc692006-03-28 13:03:44 +0200732 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100733 unsigned long sl;
734
Jens Axboedd67d052006-06-21 09:36:18 +0200735 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200736 WARN_ON(cfqq != cfqd->active_queue);
737
738 /*
739 * idle is disabled, either manually or by past process history
740 */
741 if (!cfqd->cfq_slice_idle)
742 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200743 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200744 return 0;
745 /*
746 * task has exited, don't wait
747 */
Jens Axboe206dc692006-03-28 13:03:44 +0200748 cic = cfqd->active_cic;
749 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200750 return 0;
751
Jens Axboe3b181522005-06-27 10:56:24 +0200752 cfq_mark_cfqq_must_dispatch(cfqq);
753 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200754
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100755 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200756
757 /*
758 * we don't want to idle for seeks, but we do want to allow
759 * fair distribution of slice time for a process doing back-to-back
760 * seeks. so allow a little bit of time for him to submit a new rq
761 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200762 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200763 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200764
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100765 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200766 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Jens Axboe5e705372006-07-13 12:39:25 +0200769static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200772 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200773
Jens Axboe5380a102006-07-13 12:37:56 +0200774 cfq_remove_request(rq);
775 cfqq->on_dispatch[rq_is_sync(rq)]++;
776 elv_dispatch_sort(q, rq);
Jens Axboefd61af02006-06-16 15:35:39 +0200777
778 rq = list_entry(q->queue_head.prev, struct request, queuelist);
779 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * return expired entry, or NULL to just start from scratch in rbtree
784 */
Jens Axboe5e705372006-07-13 12:39:25 +0200785static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200788 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200789 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Jens Axboe3b181522005-06-27 10:56:24 +0200791 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return NULL;
Jens Axboe89850f72006-07-22 16:48:31 +0200793 if (list_empty(&cfqq->fifo))
794 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Jens Axboe89850f72006-07-22 16:48:31 +0200796 fifo = cfq_cfqq_class_sync(cfqq);
797 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jens Axboe89850f72006-07-22 16:48:31 +0200799 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
800 cfq_mark_cfqq_fifo_expire(cfqq);
801 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 return NULL;
805}
806
807/*
Jens Axboe3b181522005-06-27 10:56:24 +0200808 * Scale schedule slice based on io priority. Use the sync time slice only
809 * if a queue is marked sync and has sync io queued. A sync queue with async
810 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200812static inline int
813cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jens Axboe22e2c502005-06-27 10:55:12 +0200815 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jens Axboe22e2c502005-06-27 10:55:12 +0200817 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jens Axboe22e2c502005-06-27 10:55:12 +0200819 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Jens Axboe22e2c502005-06-27 10:55:12 +0200822static inline void
823cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
824{
825 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
826}
827
828static inline int
829cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
830{
831 const int base_rq = cfqd->cfq_slice_async_rq;
832
833 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
834
835 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
836}
837
838/*
839 * get next queue for service
840 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100841static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200842{
843 unsigned long now = jiffies;
844 struct cfq_queue *cfqq;
845
846 cfqq = cfqd->active_queue;
847 if (!cfqq)
848 goto new_queue;
849
850 /*
851 * slice has expired
852 */
Jens Axboe3b181522005-06-27 10:56:24 +0200853 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
854 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200855
856 /*
857 * if queue has requests, dispatch one. if not, check if
858 * enough slice is left to wait for one
859 */
Jens Axboedd67d052006-06-21 09:36:18 +0200860 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200861 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200862 else if (cfq_cfqq_dispatched(cfqq)) {
863 cfqq = NULL;
864 goto keep_queue;
865 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200866 if (cfq_arm_slice_timer(cfqd, cfqq))
867 return NULL;
868 }
869
Jens Axboe3b181522005-06-27 10:56:24 +0200870expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200871 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200872new_queue:
873 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200874keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200875 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200876}
877
878static int
879__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
880 int max_dispatch)
881{
882 int dispatched = 0;
883
Jens Axboedd67d052006-06-21 09:36:18 +0200884 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200885
886 do {
Jens Axboe5e705372006-07-13 12:39:25 +0200887 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200888
889 /*
890 * follow expired path, else get first next available
891 */
Jens Axboe5e705372006-07-13 12:39:25 +0200892 if ((rq = cfq_check_fifo(cfqq)) == NULL)
893 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200894
895 /*
896 * finally, insert request into driver dispatch list
897 */
Jens Axboe5e705372006-07-13 12:39:25 +0200898 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200899
900 cfqd->dispatch_slice++;
901 dispatched++;
902
903 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +0200904 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
905 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200906 }
907
Jens Axboedd67d052006-06-21 09:36:18 +0200908 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200909 break;
910
911 } while (dispatched < max_dispatch);
912
913 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +0200914 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +0200915 */
916 if (!cfqq->slice_end)
917 cfq_set_prio_slice(cfqd, cfqq);
918
919 /*
920 * expire an async queue immediately if it has used up its slice. idle
921 * queue always expire after 1 dispatch round.
922 */
923 if ((!cfq_cfqq_sync(cfqq) &&
924 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +0200925 cfq_class_idle(cfqq) ||
926 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 cfq_slice_expired(cfqd, 0);
928
929 return dispatched;
930}
931
932static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100933cfq_forced_dispatch_cfqqs(struct list_head *list)
934{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100935 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200936 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100937
Jens Axboecaaa5f92006-06-16 11:23:00 +0200938 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100939 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +0200940 while (cfqq->next_rq) {
941 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100942 dispatched++;
943 }
944 BUG_ON(!list_empty(&cfqq->fifo));
945 }
Jens Axboecaaa5f92006-06-16 11:23:00 +0200946
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100947 return dispatched;
948}
949
950static int
951cfq_forced_dispatch(struct cfq_data *cfqd)
952{
953 int i, dispatched = 0;
954
955 for (i = 0; i < CFQ_PRIO_LISTS; i++)
956 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
957
958 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
959 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
960 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
961
962 cfq_slice_expired(cfqd, 0);
963
964 BUG_ON(cfqd->busy_queues);
965
966 return dispatched;
967}
968
969static int
Jens Axboeb4878f22005-10-20 16:42:29 +0200970cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200973 struct cfq_queue *cfqq, *prev_cfqq;
974 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jens Axboe22e2c502005-06-27 10:55:12 +0200976 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100979 if (unlikely(force))
980 return cfq_forced_dispatch(cfqd);
981
Jens Axboecaaa5f92006-06-16 11:23:00 +0200982 dispatched = 0;
983 prev_cfqq = NULL;
984 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +0200985 int max_dispatch;
986
Jens Axboecaaa5f92006-06-16 11:23:00 +0200987 /*
988 * Don't repeat dispatch from the previous queue.
989 */
990 if (prev_cfqq == cfqq)
991 break;
992
Jens Axboe3b181522005-06-27 10:56:24 +0200993 cfq_clear_cfqq_must_dispatch(cfqq);
994 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200995 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100997 max_dispatch = cfqd->cfq_quantum;
998 if (cfq_class_idle(cfqq))
999 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jens Axboecaaa5f92006-06-16 11:23:00 +02001001 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1002
1003 /*
1004 * If the dispatch cfqq has idling enabled and is still
1005 * the active queue, break out.
1006 */
1007 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1008 break;
1009
1010 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Jens Axboecaaa5f92006-06-16 11:23:00 +02001013 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
Jens Axboe5e705372006-07-13 12:39:25 +02001017 * task holds one reference to the queue, dropped when task exits. each rq
1018 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 *
1020 * queue lock must be held here.
1021 */
1022static void cfq_put_queue(struct cfq_queue *cfqq)
1023{
Jens Axboe22e2c502005-06-27 10:55:12 +02001024 struct cfq_data *cfqd = cfqq->cfqd;
1025
1026 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!atomic_dec_and_test(&cfqq->ref))
1029 return;
1030
1031 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001032 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001033 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001035 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001036 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * it's on the empty list and still hashed
1040 */
1041 list_del(&cfqq->cfq_list);
1042 hlist_del(&cfqq->cfq_hash);
1043 kmem_cache_free(cfq_pool, cfqq);
1044}
1045
1046static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001047__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1048 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001051 struct hlist_node *entry;
1052 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jens Axboe206dc692006-03-28 13:03:44 +02001054 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001055 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jens Axboe206dc692006-03-28 13:03:44 +02001057 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return __cfqq;
1059 }
1060
1061 return NULL;
1062}
1063
1064static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001065cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Jens Axboe3b181522005-06-27 10:56:24 +02001067 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Jens Axboee2d74ac2006-03-28 08:59:01 +02001070static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jens Axboe22e2c502005-06-27 10:55:12 +02001072 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001073 struct rb_node *n;
1074 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001075
Jens Axboee2d74ac2006-03-28 08:59:01 +02001076 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1077 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1078 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001079 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001080 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001081 }
1082
Al Viro334e94d2006-03-18 15:05:53 -05001083 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1084 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Al Viroe17a9482006-03-18 13:21:20 -05001087static void cfq_trim(struct io_context *ioc)
1088{
1089 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001090 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001091}
1092
Jens Axboe89850f72006-07-22 16:48:31 +02001093static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1094{
1095 if (unlikely(cfqq == cfqd->active_queue))
1096 __cfq_slice_expired(cfqd, cfqq, 0);
1097
1098 cfq_put_queue(cfqq);
1099}
1100
1101static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1102 struct cfq_io_context *cic)
1103{
1104 if (cic->cfqq[ASYNC]) {
1105 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1106 cic->cfqq[ASYNC] = NULL;
1107 }
1108
1109 if (cic->cfqq[SYNC]) {
1110 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1111 cic->cfqq[SYNC] = NULL;
1112 }
1113
1114 cic->key = NULL;
1115 list_del_init(&cic->queue_list);
1116}
1117
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001120 * Called with interrupts disabled
1121 */
1122static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1123{
Al Viro478a82b2006-03-18 13:25:24 -05001124 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001125
1126 WARN_ON(!irqs_disabled());
1127
Jens Axboe89850f72006-07-22 16:48:31 +02001128 if (cfqd) {
1129 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001130
Jens Axboe89850f72006-07-22 16:48:31 +02001131 spin_lock(q->queue_lock);
1132 __cfq_exit_single_io_context(cfqd, cic);
1133 spin_unlock(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001134 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001135}
1136
Jens Axboee2d74ac2006-03-28 08:59:01 +02001137static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jens Axboe22e2c502005-06-27 10:55:12 +02001139 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001141 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * put the reference this task is holding to the various queues
1145 */
Jens Axboe3793c652006-05-30 21:11:04 +02001146 spin_lock_irqsave(&cfq_exit_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001147
1148 n = rb_first(&ioc->cic_root);
1149 while (n != NULL) {
1150 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1151
Jens Axboe22e2c502005-06-27 10:55:12 +02001152 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001153 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Jens Axboe3793c652006-05-30 21:11:04 +02001156 spin_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Jens Axboe22e2c502005-06-27 10:55:12 +02001159static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001160cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Jens Axboe22e2c502005-06-27 10:55:12 +02001162 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001165 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001166 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001167 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001168 cic->dtor = cfq_free_io_context;
1169 cic->exit = cfq_exit_io_context;
Al Viro334e94d2006-03-18 15:05:53 -05001170 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 return cic;
1174}
1175
Jens Axboe22e2c502005-06-27 10:55:12 +02001176static void cfq_init_prio_data(struct cfq_queue *cfqq)
1177{
1178 struct task_struct *tsk = current;
1179 int ioprio_class;
1180
Jens Axboe3b181522005-06-27 10:56:24 +02001181 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001182 return;
1183
1184 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1185 switch (ioprio_class) {
1186 default:
1187 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1188 case IOPRIO_CLASS_NONE:
1189 /*
1190 * no prio set, place us in the middle of the BE classes
1191 */
1192 cfqq->ioprio = task_nice_ioprio(tsk);
1193 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1194 break;
1195 case IOPRIO_CLASS_RT:
1196 cfqq->ioprio = task_ioprio(tsk);
1197 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1198 break;
1199 case IOPRIO_CLASS_BE:
1200 cfqq->ioprio = task_ioprio(tsk);
1201 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1202 break;
1203 case IOPRIO_CLASS_IDLE:
1204 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1205 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001206 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001207 break;
1208 }
1209
1210 /*
1211 * keep track of original prio settings in case we have to temporarily
1212 * elevate the priority of this queue
1213 */
1214 cfqq->org_ioprio = cfqq->ioprio;
1215 cfqq->org_ioprio_class = cfqq->ioprio_class;
1216
Jens Axboe3b181522005-06-27 10:56:24 +02001217 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001218 cfq_resort_rr_list(cfqq, 0);
1219
Jens Axboe3b181522005-06-27 10:56:24 +02001220 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001221}
1222
Al Viro478a82b2006-03-18 13:25:24 -05001223static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001224{
Al Viro478a82b2006-03-18 13:25:24 -05001225 struct cfq_data *cfqd = cic->key;
1226 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02001227
Jens Axboecaaa5f92006-06-16 11:23:00 +02001228 if (unlikely(!cfqd))
1229 return;
1230
1231 spin_lock(cfqd->queue->queue_lock);
1232
1233 cfqq = cic->cfqq[ASYNC];
1234 if (cfqq) {
1235 struct cfq_queue *new_cfqq;
1236 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1237 GFP_ATOMIC);
1238 if (new_cfqq) {
1239 cic->cfqq[ASYNC] = new_cfqq;
1240 cfq_put_queue(cfqq);
1241 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001242 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001243
1244 cfqq = cic->cfqq[SYNC];
1245 if (cfqq)
1246 cfq_mark_cfqq_prio_changed(cfqq);
1247
1248 spin_unlock(cfqd->queue->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001252 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001254static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Al Viroa6a07632006-03-18 13:26:44 -05001256 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001257 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001258
Jens Axboe3793c652006-05-30 21:11:04 +02001259 spin_lock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001260
Jens Axboee2d74ac2006-03-28 08:59:01 +02001261 n = rb_first(&ioc->cic_root);
1262 while (n != NULL) {
1263 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001264
Al Viro478a82b2006-03-18 13:25:24 -05001265 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001266 n = rb_next(n);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jens Axboe3793c652006-05-30 21:11:04 +02001269 spin_unlock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001270
Jens Axboe22e2c502005-06-27 10:55:12 +02001271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001275cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001276 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1279 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001280 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282retry:
Al Viro6f325a12006-03-18 14:58:37 -05001283 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001284 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (!cfqq) {
1287 if (new_cfqq) {
1288 cfqq = new_cfqq;
1289 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001290 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001291 /*
1292 * Inform the allocator of the fact that we will
1293 * just repeat this allocation if it fails, to allow
1294 * the allocator to do whatever it needs to attempt to
1295 * free memory.
1296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001298 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask|__GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 spin_lock_irq(cfqd->queue->queue_lock);
1300 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001301 } else {
1302 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1303 if (!cfqq)
1304 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 memset(cfqq, 0, sizeof(*cfqq));
1308
1309 INIT_HLIST_NODE(&cfqq->cfq_hash);
1310 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001311 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 cfqq->key = key;
1314 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1315 atomic_set(&cfqq->ref, 0);
1316 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001317 cfqq->service_last = 0;
1318 /*
1319 * set ->slice_left to allow preemption for a new process
1320 */
1321 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001322 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001323 cfq_mark_cfqq_prio_changed(cfqq);
1324 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 if (new_cfqq)
1328 kmem_cache_free(cfq_pool, new_cfqq);
1329
1330 atomic_inc(&cfqq->ref);
1331out:
1332 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1333 return cfqq;
1334}
1335
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001336static void
1337cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1338{
Jens Axboe3793c652006-05-30 21:11:04 +02001339 spin_lock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001340 rb_erase(&cic->rb_node, &ioc->cic_root);
Jens Axboe3793c652006-05-30 21:11:04 +02001341 list_del_init(&cic->queue_list);
1342 spin_unlock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001343 kmem_cache_free(cfq_ioc_pool, cic);
1344 atomic_dec(&ioc_count);
1345}
1346
Jens Axboee2d74ac2006-03-28 08:59:01 +02001347static struct cfq_io_context *
1348cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1349{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001350 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001351 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001352 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001353
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001354restart:
1355 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001356 while (n) {
1357 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001358 /* ->key must be copied to avoid race with cfq_exit_queue() */
1359 k = cic->key;
1360 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001361 cfq_drop_dead_cic(ioc, cic);
1362 goto restart;
1363 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001364
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001365 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001366 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001367 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001368 n = n->rb_right;
1369 else
1370 return cic;
1371 }
1372
1373 return NULL;
1374}
1375
1376static inline void
1377cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1378 struct cfq_io_context *cic)
1379{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001380 struct rb_node **p;
1381 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001382 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001383 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001384
Jens Axboee2d74ac2006-03-28 08:59:01 +02001385 cic->ioc = ioc;
1386 cic->key = cfqd;
1387
1388 ioc->set_ioprio = cfq_ioc_set_ioprio;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001389restart:
1390 parent = NULL;
1391 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001392 while (*p) {
1393 parent = *p;
1394 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001395 /* ->key must be copied to avoid race with cfq_exit_queue() */
1396 k = __cic->key;
1397 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001398 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001399 goto restart;
1400 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001401
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001402 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001403 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001404 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001405 p = &(*p)->rb_right;
1406 else
1407 BUG();
1408 }
1409
Jens Axboe3793c652006-05-30 21:11:04 +02001410 spin_lock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001411 rb_link_node(&cic->rb_node, parent, p);
1412 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1413 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe3793c652006-05-30 21:11:04 +02001414 spin_unlock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001415}
1416
Jens Axboe22e2c502005-06-27 10:55:12 +02001417/*
1418 * Setup general io context and cfq io context. There can be several cfq
1419 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001420 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001421 */
1422static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001423cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Jens Axboe22e2c502005-06-27 10:55:12 +02001425 struct io_context *ioc = NULL;
1426 struct cfq_io_context *cic;
1427
1428 might_sleep_if(gfp_mask & __GFP_WAIT);
1429
1430 ioc = get_io_context(gfp_mask);
1431 if (!ioc)
1432 return NULL;
1433
Jens Axboee2d74ac2006-03-28 08:59:01 +02001434 cic = cfq_cic_rb_lookup(cfqd, ioc);
1435 if (cic)
1436 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001437
Jens Axboee2d74ac2006-03-28 08:59:01 +02001438 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1439 if (cic == NULL)
1440 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001441
Jens Axboee2d74ac2006-03-28 08:59:01 +02001442 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001443out:
1444 return cic;
1445err:
1446 put_io_context(ioc);
1447 return NULL;
1448}
1449
1450static void
1451cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1452{
1453 unsigned long elapsed, ttime;
1454
1455 /*
1456 * if this context already has stuff queued, thinktime is from
1457 * last queue not last end
1458 */
1459#if 0
1460 if (time_after(cic->last_end_request, cic->last_queue))
1461 elapsed = jiffies - cic->last_end_request;
1462 else
1463 elapsed = jiffies - cic->last_queue;
1464#else
1465 elapsed = jiffies - cic->last_end_request;
1466#endif
1467
1468 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1469
1470 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1471 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1472 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1473}
1474
Jens Axboe206dc692006-03-28 13:03:44 +02001475static void
1476cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
Jens Axboe5e705372006-07-13 12:39:25 +02001477 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001478{
1479 sector_t sdist;
1480 u64 total;
1481
Jens Axboe5e705372006-07-13 12:39:25 +02001482 if (cic->last_request_pos < rq->sector)
1483 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001484 else
Jens Axboe5e705372006-07-13 12:39:25 +02001485 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001486
1487 /*
1488 * Don't allow the seek distance to get too large from the
1489 * odd fragment, pagein, etc
1490 */
1491 if (cic->seek_samples <= 60) /* second&third seek */
1492 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1493 else
1494 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1495
1496 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1497 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1498 total = cic->seek_total + (cic->seek_samples/2);
1499 do_div(total, cic->seek_samples);
1500 cic->seek_mean = (sector_t)total;
1501}
Jens Axboe22e2c502005-06-27 10:55:12 +02001502
1503/*
1504 * Disable idle window if the process thinks too long or seeks so much that
1505 * it doesn't matter
1506 */
1507static void
1508cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1509 struct cfq_io_context *cic)
1510{
Jens Axboe3b181522005-06-27 10:56:24 +02001511 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001512
Jens Axboecaaa5f92006-06-16 11:23:00 +02001513 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1514 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001515 enable_idle = 0;
1516 else if (sample_valid(cic->ttime_samples)) {
1517 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1518 enable_idle = 0;
1519 else
1520 enable_idle = 1;
1521 }
1522
Jens Axboe3b181522005-06-27 10:56:24 +02001523 if (enable_idle)
1524 cfq_mark_cfqq_idle_window(cfqq);
1525 else
1526 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001527}
1528
1529
1530/*
1531 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1532 * no or if we aren't sure, a 1 will cause a preempt.
1533 */
1534static int
1535cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001536 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001537{
1538 struct cfq_queue *cfqq = cfqd->active_queue;
1539
1540 if (cfq_class_idle(new_cfqq))
1541 return 0;
1542
1543 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001544 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001545
1546 if (cfq_class_idle(cfqq))
1547 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001548 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001549 return 0;
1550 /*
1551 * if it doesn't have slice left, forget it
1552 */
1553 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1554 return 0;
Jens Axboe5e705372006-07-13 12:39:25 +02001555 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001556 return 1;
1557
1558 return 0;
1559}
1560
1561/*
1562 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1563 * let it have half of its nominal slice.
1564 */
1565static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1566{
1567 struct cfq_queue *__cfqq, *next;
1568
1569 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1570 cfq_resort_rr_list(__cfqq, 1);
1571
1572 if (!cfqq->slice_left)
1573 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1574
1575 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001576 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001577 __cfq_set_active_queue(cfqd, cfqq);
1578}
1579
1580/*
1581 * should really be a ll_rw_blk.c helper
1582 */
1583static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1584{
1585 request_queue_t *q = cfqd->queue;
1586
1587 if (!blk_queue_plugged(q))
1588 q->request_fn(q);
1589 else
1590 __generic_unplug_device(q);
1591}
1592
1593/*
Jens Axboe5e705372006-07-13 12:39:25 +02001594 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001595 * something we should do about it
1596 */
1597static void
Jens Axboe5e705372006-07-13 12:39:25 +02001598cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1599 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001600{
Jens Axboe5e705372006-07-13 12:39:25 +02001601 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001602
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001603 /*
Jens Axboe5380a102006-07-13 12:37:56 +02001604 * check if this request is a better next-serve candidate)) {
Jens Axboe21183b02006-07-13 12:33:14 +02001605 */
Jens Axboe5e705372006-07-13 12:39:25 +02001606 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
1607 BUG_ON(!cfqq->next_rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001608
1609 /*
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001610 * we never wait for an async request and we don't allow preemption
1611 * of an async request. so just return early
1612 */
Jens Axboe5e705372006-07-13 12:39:25 +02001613 if (!rq_is_sync(rq)) {
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001614 /*
1615 * sync process issued an async request, if it's waiting
1616 * then expire it and kick rq handling.
1617 */
1618 if (cic == cfqd->active_cic &&
1619 del_timer(&cfqd->idle_slice_timer)) {
1620 cfq_slice_expired(cfqd, 0);
1621 cfq_start_queueing(cfqd, cfqq);
1622 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001623 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001624 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001625
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001626 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe5e705372006-07-13 12:39:25 +02001627 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001628 cfq_update_idle_window(cfqd, cfqq, cic);
1629
1630 cic->last_queue = jiffies;
Jens Axboe5e705372006-07-13 12:39:25 +02001631 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001632
1633 if (cfqq == cfqd->active_queue) {
1634 /*
1635 * if we are waiting for a request for this queue, let it rip
1636 * immediately and flag that we must not expire this queue
1637 * just now
1638 */
Jens Axboe3b181522005-06-27 10:56:24 +02001639 if (cfq_cfqq_wait_request(cfqq)) {
1640 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001641 del_timer(&cfqd->idle_slice_timer);
1642 cfq_start_queueing(cfqd, cfqq);
1643 }
Jens Axboe5e705372006-07-13 12:39:25 +02001644 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001645 /*
1646 * not the active queue - expire current slice if it is
1647 * idle and has expired it's mean thinktime or this new queue
1648 * has some old slice time left and is of higher priority
1649 */
1650 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001651 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001652 cfq_start_queueing(cfqd, cfqq);
1653 }
1654}
1655
Jens Axboeb4878f22005-10-20 16:42:29 +02001656static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001657{
Jens Axboeb4878f22005-10-20 16:42:29 +02001658 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001659 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001660
1661 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jens Axboe5e705372006-07-13 12:39:25 +02001663 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Jens Axboe21183b02006-07-13 12:33:14 +02001665 if (!cfq_cfqq_on_rr(cfqq))
1666 cfq_add_cfqq_rr(cfqd, cfqq);
1667
Jens Axboe22e2c502005-06-27 10:55:12 +02001668 list_add_tail(&rq->queuelist, &cfqq->fifo);
1669
Jens Axboe5e705372006-07-13 12:39:25 +02001670 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static void cfq_completed_request(request_queue_t *q, struct request *rq)
1674{
Jens Axboe5e705372006-07-13 12:39:25 +02001675 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001676 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001677 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001678 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Jens Axboeb4878f22005-10-20 16:42:29 +02001680 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Jens Axboeb4878f22005-10-20 16:42:29 +02001682 WARN_ON(!cfqd->rq_in_driver);
1683 WARN_ON(!cfqq->on_dispatch[sync]);
1684 cfqd->rq_in_driver--;
1685 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Jens Axboeb4878f22005-10-20 16:42:29 +02001687 if (!cfq_class_idle(cfqq))
1688 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001689
Jens Axboeb4878f22005-10-20 16:42:29 +02001690 if (!cfq_cfqq_dispatched(cfqq)) {
1691 if (cfq_cfqq_on_rr(cfqq)) {
1692 cfqq->service_last = now;
1693 cfq_resort_rr_list(cfqq, 0);
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Jens Axboecaaa5f92006-06-16 11:23:00 +02001697 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001698 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001699
1700 /*
1701 * If this is the active queue, check if it needs to be expired,
1702 * or if we want to idle in case it has no pending requests.
1703 */
1704 if (cfqd->active_queue == cfqq) {
1705 if (time_after(now, cfqq->slice_end))
1706 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001707 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001708 if (!cfq_arm_slice_timer(cfqd, cfqq))
1709 cfq_schedule_dispatch(cfqd);
1710 }
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Jens Axboe22e2c502005-06-27 10:55:12 +02001714/*
1715 * we temporarily boost lower priority queues if they are holding fs exclusive
1716 * resources. they are boosted to normal prio (CLASS_BE/4)
1717 */
1718static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Jens Axboe22e2c502005-06-27 10:55:12 +02001720 const int ioprio_class = cfqq->ioprio_class;
1721 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Jens Axboe22e2c502005-06-27 10:55:12 +02001723 if (has_fs_excl()) {
1724 /*
1725 * boost idle prio on transactions that would lock out other
1726 * users of the filesystem
1727 */
1728 if (cfq_class_idle(cfqq))
1729 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1730 if (cfqq->ioprio > IOPRIO_NORM)
1731 cfqq->ioprio = IOPRIO_NORM;
1732 } else {
1733 /*
1734 * check if we need to unboost the queue
1735 */
1736 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1737 cfqq->ioprio_class = cfqq->org_ioprio_class;
1738 if (cfqq->ioprio != cfqq->org_ioprio)
1739 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
Jens Axboe22e2c502005-06-27 10:55:12 +02001742 /*
1743 * refile between round-robin lists if we moved the priority class
1744 */
1745 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001746 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001747 cfq_resort_rr_list(cfqq, 0);
1748}
1749
Jens Axboe89850f72006-07-22 16:48:31 +02001750static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001751{
Jens Axboe3b181522005-06-27 10:56:24 +02001752 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001753 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001754 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001755 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001756 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001757
1758 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001759}
1760
Jens Axboecb78b282006-07-28 09:32:57 +02001761static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001762{
1763 struct cfq_data *cfqd = q->elevator->elevator_data;
1764 struct task_struct *tsk = current;
1765 struct cfq_queue *cfqq;
1766
1767 /*
1768 * don't force setup of a queue from here, as a call to may_queue
1769 * does not necessarily imply that a request actually will be queued.
1770 * so just lookup a possibly existing queue, or return 'may queue'
1771 * if that fails
1772 */
Jens Axboe3b181522005-06-27 10:56:24 +02001773 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001774 if (cfqq) {
1775 cfq_init_prio_data(cfqq);
1776 cfq_prio_boost(cfqq);
1777
Jens Axboe89850f72006-07-22 16:48:31 +02001778 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001779 }
1780
1781 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784/*
1785 * queue lock held here
1786 */
1787static void cfq_put_request(request_queue_t *q, struct request *rq)
1788{
Jens Axboe5e705372006-07-13 12:39:25 +02001789 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jens Axboe5e705372006-07-13 12:39:25 +02001791 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001792 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Jens Axboe22e2c502005-06-27 10:55:12 +02001794 BUG_ON(!cfqq->allocated[rw]);
1795 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jens Axboe5e705372006-07-13 12:39:25 +02001797 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001800 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 cfq_put_queue(cfqq);
1803 }
1804}
1805
1806/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001807 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001809static int
Jens Axboecb78b282006-07-28 09:32:57 +02001810cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001813 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct cfq_io_context *cic;
1815 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001816 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001817 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001819 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 might_sleep_if(gfp_mask & __GFP_WAIT);
1822
Jens Axboee2d74ac2006-03-28 08:59:01 +02001823 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 spin_lock_irqsave(q->queue_lock, flags);
1826
Jens Axboe22e2c502005-06-27 10:55:12 +02001827 if (!cic)
1828 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Al Viro12a05732006-03-18 13:38:01 -05001830 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001831 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001832 if (!cfqq)
1833 goto queue_fail;
1834
Al Viro12a05732006-03-18 13:38:01 -05001835 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001836 } else
Al Viro12a05732006-03-18 13:38:01 -05001837 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001840 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001841 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 spin_unlock_irqrestore(q->queue_lock, flags);
1844
Jens Axboe5e705372006-07-13 12:39:25 +02001845 rq->elevator_private = cic;
1846 rq->elevator_private2 = cfqq;
1847 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001848
Jens Axboe22e2c502005-06-27 10:55:12 +02001849queue_fail:
1850 if (cic)
1851 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001852
Jens Axboe3b181522005-06-27 10:56:24 +02001853 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 spin_unlock_irqrestore(q->queue_lock, flags);
1855 return 1;
1856}
1857
Jens Axboe22e2c502005-06-27 10:55:12 +02001858static void cfq_kick_queue(void *data)
1859{
1860 request_queue_t *q = data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001861 unsigned long flags;
1862
1863 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001864 blk_remove_plug(q);
1865 q->request_fn(q);
1866 spin_unlock_irqrestore(q->queue_lock, flags);
1867}
1868
1869/*
1870 * Timer running if the active_queue is currently idling inside its time slice
1871 */
1872static void cfq_idle_slice_timer(unsigned long data)
1873{
1874 struct cfq_data *cfqd = (struct cfq_data *) data;
1875 struct cfq_queue *cfqq;
1876 unsigned long flags;
1877
1878 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1879
1880 if ((cfqq = cfqd->active_queue) != NULL) {
1881 unsigned long now = jiffies;
1882
1883 /*
1884 * expired
1885 */
1886 if (time_after(now, cfqq->slice_end))
1887 goto expire;
1888
1889 /*
1890 * only expire and reinvoke request handler, if there are
1891 * other queues with pending requests
1892 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001893 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001894 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001895
1896 /*
1897 * not expired and it has a request pending, let it dispatch
1898 */
Jens Axboedd67d052006-06-21 09:36:18 +02001899 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001900 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001901 goto out_kick;
1902 }
1903 }
1904expire:
1905 cfq_slice_expired(cfqd, 0);
1906out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001907 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001908out_cont:
1909 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1910}
1911
1912/*
1913 * Timer running if an idle class queue is waiting for service
1914 */
1915static void cfq_idle_class_timer(unsigned long data)
1916{
1917 struct cfq_data *cfqd = (struct cfq_data *) data;
1918 unsigned long flags, end;
1919
1920 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1921
1922 /*
1923 * race with a non-idle queue, reset timer
1924 */
1925 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001926 if (!time_after_eq(jiffies, end))
1927 mod_timer(&cfqd->idle_class_timer, end);
1928 else
Jens Axboe3b181522005-06-27 10:56:24 +02001929 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001930
1931 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1932}
1933
Jens Axboe3b181522005-06-27 10:56:24 +02001934static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1935{
1936 del_timer_sync(&cfqd->idle_slice_timer);
1937 del_timer_sync(&cfqd->idle_class_timer);
1938 blk_sync_queue(cfqd->queue);
1939}
Jens Axboe22e2c502005-06-27 10:55:12 +02001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static void cfq_exit_queue(elevator_t *e)
1942{
Jens Axboe22e2c502005-06-27 10:55:12 +02001943 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05001944 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001945
Jens Axboe3b181522005-06-27 10:56:24 +02001946 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001947
Jens Axboe3793c652006-05-30 21:11:04 +02001948 spin_lock(&cfq_exit_lock);
Al Virod9ff4182006-03-18 13:51:22 -05001949 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001950
Al Virod9ff4182006-03-18 13:51:22 -05001951 if (cfqd->active_queue)
1952 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001953
1954 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05001955 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1956 struct cfq_io_context,
1957 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02001958
1959 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05001960 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001961
Al Virod9ff4182006-03-18 13:51:22 -05001962 spin_unlock_irq(q->queue_lock);
Jens Axboe3793c652006-05-30 21:11:04 +02001963 spin_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05001964
1965 cfq_shutdown_timer_wq(cfqd);
1966
Al Viroa90d7422006-03-18 12:05:37 -05001967 kfree(cfqd->cfq_hash);
1968 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Jens Axboebc1c1162006-06-08 08:49:06 +02001971static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 struct cfq_data *cfqd;
1974 int i;
1975
1976 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
1977 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02001978 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02001981
1982 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1983 INIT_LIST_HEAD(&cfqd->rr_list[i]);
1984
1985 INIT_LIST_HEAD(&cfqd->busy_rr);
1986 INIT_LIST_HEAD(&cfqd->cur_rr);
1987 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05001989 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
1992 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02001993 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
1996 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Jens Axboe22e2c502005-06-27 10:55:12 +02002000 init_timer(&cfqd->idle_slice_timer);
2001 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2002 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2003
2004 init_timer(&cfqd->idle_class_timer);
2005 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2006 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2007
2008 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002011 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2012 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 cfqd->cfq_back_max = cfq_back_max;
2014 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002015 cfqd->cfq_slice[0] = cfq_slice_async;
2016 cfqd->cfq_slice[1] = cfq_slice_sync;
2017 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2018 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002019
Jens Axboebc1c1162006-06-08 08:49:06 +02002020 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002021out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002023 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026static void cfq_slab_kill(void)
2027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (cfq_pool)
2029 kmem_cache_destroy(cfq_pool);
2030 if (cfq_ioc_pool)
2031 kmem_cache_destroy(cfq_ioc_pool);
2032}
2033
2034static int __init cfq_slab_setup(void)
2035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2037 NULL, NULL);
2038 if (!cfq_pool)
2039 goto fail;
2040
2041 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2042 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2043 if (!cfq_ioc_pool)
2044 goto fail;
2045
2046 return 0;
2047fail:
2048 cfq_slab_kill();
2049 return -ENOMEM;
2050}
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052/*
2053 * sysfs parts below -->
2054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056static ssize_t
2057cfq_var_show(unsigned int var, char *page)
2058{
2059 return sprintf(page, "%d\n", var);
2060}
2061
2062static ssize_t
2063cfq_var_store(unsigned int *var, const char *page, size_t count)
2064{
2065 char *p = (char *) page;
2066
2067 *var = simple_strtoul(p, &p, 10);
2068 return count;
2069}
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002072static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002074 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 unsigned int __data = __VAR; \
2076 if (__CONV) \
2077 __data = jiffies_to_msecs(__data); \
2078 return cfq_var_show(__data, (page)); \
2079}
2080SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002081SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2082SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002083SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2084SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002085SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2086SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2087SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2088SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089#undef SHOW_FUNCTION
2090
2091#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002092static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002094 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 unsigned int __data; \
2096 int ret = cfq_var_store(&__data, (page), count); \
2097 if (__data < (MIN)) \
2098 __data = (MIN); \
2099 else if (__data > (MAX)) \
2100 __data = (MAX); \
2101 if (__CONV) \
2102 *(__PTR) = msecs_to_jiffies(__data); \
2103 else \
2104 *(__PTR) = __data; \
2105 return ret; \
2106}
2107STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002108STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2109STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002110STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2111STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002112STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2113STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2114STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2115STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#undef STORE_FUNCTION
2117
Al Viroe572ec72006-03-18 22:27:18 -05002118#define CFQ_ATTR(name) \
2119 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002120
Al Viroe572ec72006-03-18 22:27:18 -05002121static struct elv_fs_entry cfq_attrs[] = {
2122 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002123 CFQ_ATTR(fifo_expire_sync),
2124 CFQ_ATTR(fifo_expire_async),
2125 CFQ_ATTR(back_seek_max),
2126 CFQ_ATTR(back_seek_penalty),
2127 CFQ_ATTR(slice_sync),
2128 CFQ_ATTR(slice_async),
2129 CFQ_ATTR(slice_async_rq),
2130 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002131 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132};
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134static struct elevator_type iosched_cfq = {
2135 .ops = {
2136 .elevator_merge_fn = cfq_merge,
2137 .elevator_merged_fn = cfq_merged_request,
2138 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002139 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002141 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 .elevator_deactivate_req_fn = cfq_deactivate_request,
2143 .elevator_queue_empty_fn = cfq_queue_empty,
2144 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002145 .elevator_former_req_fn = elv_rb_former_request,
2146 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 .elevator_set_req_fn = cfq_set_request,
2148 .elevator_put_req_fn = cfq_put_request,
2149 .elevator_may_queue_fn = cfq_may_queue,
2150 .elevator_init_fn = cfq_init_queue,
2151 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002152 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 },
Al Viro3d1ab402006-03-18 18:35:43 -05002154 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 .elevator_name = "cfq",
2156 .elevator_owner = THIS_MODULE,
2157};
2158
2159static int __init cfq_init(void)
2160{
2161 int ret;
2162
Jens Axboe22e2c502005-06-27 10:55:12 +02002163 /*
2164 * could be 0 on HZ < 1000 setups
2165 */
2166 if (!cfq_slice_async)
2167 cfq_slice_async = 1;
2168 if (!cfq_slice_idle)
2169 cfq_slice_idle = 1;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (cfq_slab_setup())
2172 return -ENOMEM;
2173
2174 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002175 if (ret)
2176 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return ret;
2179}
2180
2181static void __exit cfq_exit(void)
2182{
Al Viro334e94d2006-03-18 15:05:53 -05002183 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002185 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002186 /* ioc_gone's update must be visible before reading ioc_count */
2187 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05002188 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002189 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002190 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002191 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
2194module_init(cfq_init);
2195module_exit(cfq_exit);
2196
2197MODULE_AUTHOR("Jens Axboe");
2198MODULE_LICENSE("GPL");
2199MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");