blob: 4838c2b16f2c4759fb0aa0f7375b2b0d1b07074e [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 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
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
Jens Axboed9e76202007-04-20 14:27:50 +020029/*
30 * grace period before allowing idle class to get disk access
31 */
Jens Axboe22e2c502005-06-27 10:55:12 +020032#define CFQ_IDLE_GRACE (HZ / 10)
Jens Axboed9e76202007-04-20 14:27:50 +020033
34/*
35 * below this threshold, we consider thinktime immediate
36 */
37#define CFQ_MIN_TT (2)
38
Jens Axboe22e2c502005-06-27 10:55:12 +020039#define CFQ_SLICE_SCALE (5)
40
41#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * for the hash of cfqq inside the cfqd
45 */
46#define CFQ_QHASH_SHIFT 6
47#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
48#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
51
Jens Axboe5e705372006-07-13 12:39:25 +020052#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
53#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Lametere18b8902006-12-06 20:33:20 -080055static struct kmem_cache *cfq_pool;
56static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jens Axboe4050cf12006-07-19 05:07:12 +020058static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050059static struct completion *ioc_gone;
60
Jens Axboe22e2c502005-06-27 10:55:12 +020061#define CFQ_PRIO_LISTS IOPRIO_BE_NR
62#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020063#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
64
Jens Axboe3b181522005-06-27 10:56:24 +020065#define ASYNC (0)
66#define SYNC (1)
67
Jens Axboe6d048f52007-04-25 12:44:27 +020068#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
Jens Axboe22e2c502005-06-27 10:55:12 +020069
Jens Axboe206dc692006-03-28 13:03:44 +020070#define sample_valid(samples) ((samples) > 80)
71
Jens Axboe22e2c502005-06-27 10:55:12 +020072/*
73 * Per block device queue structure
74 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020076 request_queue_t *queue;
77
78 /*
79 * rr list of queues with requests and the count of them
80 */
Jens Axboed9e76202007-04-20 14:27:50 +020081 struct rb_root service_tree;
Jens Axboe22e2c502005-06-27 10:55:12 +020082 struct list_head cur_rr;
83 struct list_head idle_rr;
84 unsigned int busy_queues;
85
86 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020087 * cfqq lookup hash
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jens Axboe22e2c502005-06-27 10:55:12 +020091 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020092 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020093
94 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020095 * idle window management
96 */
97 struct timer_list idle_slice_timer;
98 struct work_struct unplug_work;
99
100 struct cfq_queue *active_queue;
101 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200102 unsigned int dispatch_slice;
103
104 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jens Axboe6d048f52007-04-25 12:44:27 +0200106 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200107 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * tunables, see top of file
111 */
112 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200113 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int cfq_back_penalty;
115 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200116 unsigned int cfq_slice[2];
117 unsigned int cfq_slice_async_rq;
118 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500119
120 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200121
122 sector_t new_seek_mean;
123 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Jens Axboe22e2c502005-06-27 10:55:12 +0200126/*
127 * Per process-grouping structure
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129struct cfq_queue {
130 /* reference count */
131 atomic_t ref;
132 /* parent cfq_data */
133 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200134 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct hlist_node cfq_hash;
136 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200137 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200138 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct list_head cfq_list;
Jens Axboed9e76202007-04-20 14:27:50 +0200140 /* service_tree member */
141 struct rb_node rb_node;
142 /* service_tree key */
143 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* sorted list of pending requests */
145 struct rb_root sort_list;
146 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200147 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* requests queued in sort_list */
149 int queued[2];
150 /* currently allocated requests */
151 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200152 /* pending metadata requests */
153 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200155 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jens Axboe22e2c502005-06-27 10:55:12 +0200157 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100158 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jens Axboe6d048f52007-04-25 12:44:27 +0200160 /* number of requests that are on the dispatch list or inside driver */
161 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200162
163 /* io prio of this group */
164 unsigned short ioprio, org_ioprio;
165 unsigned short ioprio_class, org_ioprio_class;
166
Jens Axboe3b181522005-06-27 10:56:24 +0200167 /* various state flags, see below */
168 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200169
170 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Jens Axboe3b181522005-06-27 10:56:24 +0200173enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100174 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
175 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
176 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
177 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
178 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
179 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
180 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
181 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
182 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100183 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200184};
185
186#define CFQ_CFQQ_FNS(name) \
187static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
188{ \
189 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
190} \
191static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
192{ \
193 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
194} \
195static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
196{ \
197 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
198}
199
200CFQ_CFQQ_FNS(on_rr);
201CFQ_CFQQ_FNS(wait_request);
202CFQ_CFQQ_FNS(must_alloc);
203CFQ_CFQQ_FNS(must_alloc_slice);
204CFQ_CFQQ_FNS(must_dispatch);
205CFQ_CFQQ_FNS(fifo_expire);
206CFQ_CFQQ_FNS(idle_window);
207CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200208CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100209CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200210#undef CFQ_CFQQ_FNS
211
Jens Axboe3b181522005-06-27 10:56:24 +0200212static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200213static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500214static 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 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700217 * scheduler run of queue, if there are requests pending and no one in the
218 * driver that will restart queueing
219 */
220static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
221{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100222 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700223 kblockd_schedule_work(&cfqd->unplug_work);
224}
225
226static int cfq_queue_empty(request_queue_t *q)
227{
228 struct cfq_data *cfqd = q->elevator->elevator_data;
229
Jens Axboeb4878f22005-10-20 16:42:29 +0200230 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700231}
232
Jens Axboe7749a8d2006-12-13 13:02:26 +0100233static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200234{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100235 /*
236 * Use the per-process queue, for read requests and syncronous writes
237 */
238 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200239 return task->pid;
240
241 return CFQ_KEY_ASYNC;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100245 * Scale schedule slice based on io priority. Use the sync time slice only
246 * if a queue is marked sync and has sync io queued. A sync queue with async
247 * io only, should not get full sync slice length.
248 */
Jens Axboed9e76202007-04-20 14:27:50 +0200249static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
250 unsigned short prio)
251{
252 const int base_slice = cfqd->cfq_slice[sync];
253
254 WARN_ON(prio >= IOPRIO_BE_NR);
255
256 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
257}
258
Jens Axboe44f7c162007-01-19 11:51:58 +1100259static inline int
260cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
261{
Jens Axboed9e76202007-04-20 14:27:50 +0200262 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100263}
264
265static inline void
266cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
267{
268 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
269}
270
271/*
272 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
273 * isn't valid until the first request from the dispatch is activated
274 * and the slice time set.
275 */
276static inline int cfq_slice_used(struct cfq_queue *cfqq)
277{
278 if (cfq_cfqq_slice_new(cfqq))
279 return 0;
280 if (time_before(jiffies, cfqq->slice_end))
281 return 0;
282
283 return 1;
284}
285
286/*
Jens Axboe5e705372006-07-13 12:39:25 +0200287 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200289 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Jens Axboe5e705372006-07-13 12:39:25 +0200291static struct request *
292cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200296#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
297#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
298 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Jens Axboe5e705372006-07-13 12:39:25 +0200300 if (rq1 == NULL || rq1 == rq2)
301 return rq2;
302 if (rq2 == NULL)
303 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200304
Jens Axboe5e705372006-07-13 12:39:25 +0200305 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
306 return rq1;
307 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
308 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200309 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
310 return rq1;
311 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
312 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Jens Axboe5e705372006-07-13 12:39:25 +0200314 s1 = rq1->sector;
315 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jens Axboe6d048f52007-04-25 12:44:27 +0200317 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * by definition, 1KiB is 2 sectors
321 */
322 back_max = cfqd->cfq_back_max * 2;
323
324 /*
325 * Strict one way elevator _except_ in the case where we allow
326 * short backward seeks which are biased as twice the cost of a
327 * similar forward seek.
328 */
329 if (s1 >= last)
330 d1 = s1 - last;
331 else if (s1 + back_max >= last)
332 d1 = (last - s1) * cfqd->cfq_back_penalty;
333 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200334 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (s2 >= last)
337 d2 = s2 - last;
338 else if (s2 + back_max >= last)
339 d2 = (last - s2) * cfqd->cfq_back_penalty;
340 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200341 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Andreas Mohre8a99052006-03-28 08:59:49 +0200345 /*
346 * By doing switch() on the bit mask "wrap" we avoid having to
347 * check two variables for all permutations: --> faster!
348 */
349 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200350 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200351 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200352 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200353 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200354 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200355 else {
356 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200357 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200358 else
Jens Axboe5e705372006-07-13 12:39:25 +0200359 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200360 }
361
362 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200363 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200364 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200365 return rq2;
366 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200367 default:
368 /*
369 * Since both rqs are wrapped,
370 * start with the one that's further behind head
371 * (--> only *one* back seek required),
372 * since back seek takes more time than forward.
373 */
374 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200375 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 else
Jens Axboe5e705372006-07-13 12:39:25 +0200377 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379}
380
381/*
382 * would be nice to take fifo expire time into account as well
383 */
Jens Axboe5e705372006-07-13 12:39:25 +0200384static struct request *
385cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
386 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jens Axboe21183b02006-07-13 12:33:14 +0200388 struct rb_node *rbnext = rb_next(&last->rb_node);
389 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200390 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Jens Axboe21183b02006-07-13 12:33:14 +0200392 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200395 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200398 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200399 else {
400 rbnext = rb_first(&cfqq->sort_list);
401 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200402 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Jens Axboe21183b02006-07-13 12:33:14 +0200405 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Jens Axboed9e76202007-04-20 14:27:50 +0200408static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
409 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jens Axboed9e76202007-04-20 14:27:50 +0200411 /*
412 * just an approximation, should be ok.
413 */
414 return ((cfqd->busy_queues - 1) * cfq_prio_slice(cfqd, 1, 0));
415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jens Axboed9e76202007-04-20 14:27:50 +0200417static void cfq_service_tree_add(struct cfq_data *cfqd,
418 struct cfq_queue *cfqq)
419{
420 struct rb_node **p = &cfqd->service_tree.rb_node;
421 struct rb_node *parent = NULL;
422 struct cfq_queue *__cfqq;
423 unsigned long rb_key;
424
425 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
426 rb_key += cfqq->slice_resid;
427 cfqq->slice_resid = 0;
428
429 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100430 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200431 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100432 */
Jens Axboed9e76202007-04-20 14:27:50 +0200433 if (rb_key == cfqq->rb_key)
434 return;
Jens Axboe53b03742006-07-28 09:48:51 +0200435
Jens Axboed9e76202007-04-20 14:27:50 +0200436 rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200437 }
Jens Axboed9e76202007-04-20 14:27:50 +0200438
439 while (*p) {
440 parent = *p;
441 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
442
443 if (rb_key < __cfqq->rb_key)
444 p = &(*p)->rb_left;
445 else
446 p = &(*p)->rb_right;
447 }
448
449 cfqq->rb_key = rb_key;
450 rb_link_node(&cfqq->rb_node, parent, p);
451 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Jens Axboe6d048f52007-04-25 12:44:27 +0200454static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
455{
456 struct cfq_data *cfqd = cfqq->cfqd;
457 struct list_head *n;
458
459 /*
460 * Resorting requires the cfqq to be on the RR list already.
461 */
462 if (!cfq_cfqq_on_rr(cfqq))
463 return;
464
Jens Axboed9e76202007-04-20 14:27:50 +0200465 list_del_init(&cfqq->cfq_list);
Jens Axboe6d048f52007-04-25 12:44:27 +0200466
467 if (cfq_class_rt(cfqq)) {
468 /*
469 * At to the front of the current list, but behind other
470 * RT queues.
471 */
472 n = &cfqd->cur_rr;
473 while (n->next != &cfqd->cur_rr)
474 if (!cfq_class_rt(cfqq))
475 break;
476
477 list_add(&cfqq->cfq_list, n);
478 } else if (cfq_class_idle(cfqq)) {
479 /*
480 * IDLE goes to the tail of the idle list
481 */
482 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
483 } else {
484 /*
485 * So we get here, ergo the queue is a regular best-effort queue
486 */
Jens Axboed9e76202007-04-20 14:27:50 +0200487 cfq_service_tree_add(cfqd, cfqq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200488 }
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200493 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
495static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200496cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jens Axboe3b181522005-06-27 10:56:24 +0200498 BUG_ON(cfq_cfqq_on_rr(cfqq));
499 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 cfqd->busy_queues++;
501
Jens Axboeb4878f22005-10-20 16:42:29 +0200502 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static inline void
506cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
507{
Jens Axboe3b181522005-06-27 10:56:24 +0200508 BUG_ON(!cfq_cfqq_on_rr(cfqq));
509 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200510 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Jens Axboed9e76202007-04-20 14:27:50 +0200512 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
513 rb_erase(&cfqq->rb_node, &cfqd->service_tree);
514 RB_CLEAR_NODE(&cfqq->rb_node);
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 BUG_ON(!cfqd->busy_queues);
518 cfqd->busy_queues--;
519}
520
521/*
522 * rb tree support functions
523 */
Jens Axboe5e705372006-07-13 12:39:25 +0200524static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Jens Axboe5e705372006-07-13 12:39:25 +0200526 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200527 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200528 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jens Axboeb4878f22005-10-20 16:42:29 +0200530 BUG_ON(!cfqq->queued[sync]);
531 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jens Axboe5e705372006-07-13 12:39:25 +0200533 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jens Axboedd67d052006-06-21 09:36:18 +0200535 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200536 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Jens Axboe5e705372006-07-13 12:39:25 +0200539static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jens Axboe5e705372006-07-13 12:39:25 +0200541 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200543 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jens Axboe5380a102006-07-13 12:37:56 +0200545 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
548 * looks a little odd, but the first insert might return an alias.
549 * if that happens, put the alias on the dispatch list
550 */
Jens Axboe21183b02006-07-13 12:33:14 +0200551 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200552 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100553
554 if (!cfq_cfqq_on_rr(cfqq))
555 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200556
557 /*
558 * check if this request is a better next-serve candidate
559 */
560 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
561 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200565cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Jens Axboe5380a102006-07-13 12:37:56 +0200567 elv_rb_del(&cfqq->sort_list, rq);
568 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200569 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Jens Axboe206dc692006-03-28 13:03:44 +0200572static struct request *
573cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jens Axboe206dc692006-03-28 13:03:44 +0200575 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100576 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200577 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jens Axboe206dc692006-03-28 13:03:44 +0200579 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200580 if (cfqq) {
581 sector_t sector = bio->bi_sector + bio_sectors(bio);
582
Jens Axboe21183b02006-07-13 12:33:14 +0200583 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return NULL;
587}
588
Jens Axboeb4878f22005-10-20 16:42:29 +0200589static void cfq_activate_request(request_queue_t *q, struct request *rq)
590{
591 struct cfq_data *cfqd = q->elevator->elevator_data;
592
593 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200594
595 /*
596 * If the depth is larger 1, it really could be queueing. But lets
597 * make the mark a little higher - idling could still be good for
598 * low queueing, and a low queueing number could also just indicate
599 * a SCSI mid layer like behaviour where limit+1 is often seen.
600 */
601 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
602 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200603
604 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
608{
Jens Axboe22e2c502005-06-27 10:55:12 +0200609 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jens Axboeb4878f22005-10-20 16:42:29 +0200611 WARN_ON(!cfqd->rq_in_driver);
612 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Jens Axboeb4878f22005-10-20 16:42:29 +0200615static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jens Axboe5e705372006-07-13 12:39:25 +0200617 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200618
Jens Axboe5e705372006-07-13 12:39:25 +0200619 if (cfqq->next_rq == rq)
620 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jens Axboeb4878f22005-10-20 16:42:29 +0200622 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200623 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200624
625 if (rq_is_meta(rq)) {
626 WARN_ON(!cfqq->meta_pending);
627 cfqq->meta_pending--;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static int
632cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
633{
634 struct cfq_data *cfqd = q->elevator->elevator_data;
635 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jens Axboe206dc692006-03-28 13:03:44 +0200637 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200638 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200639 *req = __rq;
640 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Jens Axboe21183b02006-07-13 12:33:14 +0200646static void cfq_merged_request(request_queue_t *q, struct request *req,
647 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Jens Axboe21183b02006-07-13 12:33:14 +0200649 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200650 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jens Axboe5e705372006-07-13 12:39:25 +0200652 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void
657cfq_merged_requests(request_queue_t *q, struct request *rq,
658 struct request *next)
659{
Jens Axboe22e2c502005-06-27 10:55:12 +0200660 /*
661 * reposition in fifo if next is older than rq
662 */
663 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
664 time_before(next->start_time, rq->start_time))
665 list_move(&rq->queuelist, &next->queuelist);
666
Jens Axboeb4878f22005-10-20 16:42:29 +0200667 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200668}
669
Jens Axboeda775262006-12-20 11:04:12 +0100670static int cfq_allow_merge(request_queue_t *q, struct request *rq,
671 struct bio *bio)
672{
673 struct cfq_data *cfqd = q->elevator->elevator_data;
674 const int rw = bio_data_dir(bio);
675 struct cfq_queue *cfqq;
676 pid_t key;
677
678 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100679 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100680 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100681 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100682 return 0;
683
684 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100685 * Lookup the cfqq that this bio will be queued with. Allow
686 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100687 */
Jens Axboe719d3402006-12-22 09:38:53 +0100688 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100689 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100690
691 if (cfqq == RQ_CFQQ(rq))
692 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100693
Jens Axboeec8acb62007-01-02 18:32:11 +0100694 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100695}
696
Jens Axboe22e2c502005-06-27 10:55:12 +0200697static inline void
698__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
699{
700 if (cfqq) {
701 /*
702 * stop potential idle class queues waiting service
703 */
704 del_timer(&cfqd->idle_class_timer);
705
Jens Axboe22e2c502005-06-27 10:55:12 +0200706 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200707 cfq_clear_cfqq_must_alloc_slice(cfqq);
708 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100709 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200710 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200711 }
712
713 cfqd->active_queue = cfqq;
714}
715
716/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100717 * current cfqq expired its slice (or was too idle), select new one
718 */
719static void
720__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100721 int preempted, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100722{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100723 if (cfq_cfqq_wait_request(cfqq))
724 del_timer(&cfqd->idle_slice_timer);
725
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100726 cfq_clear_cfqq_must_dispatch(cfqq);
727 cfq_clear_cfqq_wait_request(cfqq);
728
729 /*
730 * store what was left of this slice, if the queue idled out
731 * or was preempted
732 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100733 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100734 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100735
Jens Axboe98e41c72007-02-05 11:55:35 +0100736 cfq_resort_rr_list(cfqq, preempted);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100737
738 if (cfqq == cfqd->active_queue)
739 cfqd->active_queue = NULL;
740
741 if (cfqd->active_cic) {
742 put_io_context(cfqd->active_cic->ioc);
743 cfqd->active_cic = NULL;
744 }
745
746 cfqd->dispatch_slice = 0;
747}
748
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100749static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
750 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100751{
752 struct cfq_queue *cfqq = cfqd->active_queue;
753
754 if (cfqq)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100755 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100756}
757
Jens Axboe6d048f52007-04-25 12:44:27 +0200758static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200759{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100760 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200761
Jens Axboed9e76202007-04-20 14:27:50 +0200762 if (!list_empty(&cfqd->cur_rr)) {
Jens Axboe89850f72006-07-22 16:48:31 +0200763 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200764 * if current list is non-empty, grab first entry.
Jens Axboe89850f72006-07-22 16:48:31 +0200765 */
Jens Axboed9e76202007-04-20 14:27:50 +0200766 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
767 } else if (!RB_EMPTY_ROOT(&cfqd->service_tree)) {
768 struct rb_node *n = rb_first(&cfqd->service_tree);
769
770 cfqq = rb_entry(n, struct cfq_queue, rb_node);
Jens Axboe89850f72006-07-22 16:48:31 +0200771 } else if (!list_empty(&cfqd->idle_rr)) {
772 /*
773 * if we have idle queues and no rt or be queues had pending
774 * requests, either allow immediate service if the grace period
775 * has passed or arm the idle grace timer
776 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200777 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
778
779 if (time_after_eq(jiffies, end))
780 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
781 else
782 mod_timer(&cfqd->idle_class_timer, end);
783 }
784
Jens Axboe6d048f52007-04-25 12:44:27 +0200785 return cfqq;
786}
787
788static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
789{
790 struct cfq_queue *cfqq;
791
Jens Axboed9e76202007-04-20 14:27:50 +0200792 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200793 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200794 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200795}
796
Jens Axboed9e76202007-04-20 14:27:50 +0200797static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
798 struct request *rq)
799{
800 if (rq->sector >= cfqd->last_position)
801 return rq->sector - cfqd->last_position;
802 else
803 return cfqd->last_position - rq->sector;
804}
805
Jens Axboe6d048f52007-04-25 12:44:27 +0200806static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
807{
808 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200809
Jens Axboe6d048f52007-04-25 12:44:27 +0200810 if (!sample_valid(cic->seek_samples))
811 return 0;
812
813 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
814}
815
Jens Axboed9e76202007-04-20 14:27:50 +0200816static int cfq_close_cooperator(struct cfq_data *cfq_data,
817 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200818{
Jens Axboe6d048f52007-04-25 12:44:27 +0200819 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200820 * We should notice if some of the queues are cooperating, eg
821 * working closely on the same area of the disk. In that case,
822 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200823 */
Jens Axboed9e76202007-04-20 14:27:50 +0200824 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200825}
826
827#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
828
829static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200830{
Jens Axboe17926692007-01-19 11:59:30 +1100831 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200832 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100833 unsigned long sl;
834
Jens Axboedd67d052006-06-21 09:36:18 +0200835 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200836 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200837
838 /*
839 * idle is disabled, either manually or by past process history
840 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200841 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
842 return;
843
Jens Axboe22e2c502005-06-27 10:55:12 +0200844 /*
845 * task has exited, don't wait
846 */
Jens Axboe206dc692006-03-28 13:03:44 +0200847 cic = cfqd->active_cic;
848 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200849 return;
850
851 /*
852 * See if this prio level has a good candidate
853 */
Jens Axboe1afba042007-04-17 12:47:55 +0200854 if (cfq_close_cooperator(cfqd, cfqq) &&
855 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200856 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200857
Jens Axboe3b181522005-06-27 10:56:24 +0200858 cfq_mark_cfqq_must_dispatch(cfqq);
859 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200860
Jens Axboe206dc692006-03-28 13:03:44 +0200861 /*
862 * we don't want to idle for seeks, but we do want to allow
863 * fair distribution of slice time for a process doing back-to-back
864 * seeks. so allow a little bit of time for him to submit a new rq
865 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200866 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200867 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200868 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200869
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100870 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Jens Axboe5e705372006-07-13 12:39:25 +0200873static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jens Axboe5e705372006-07-13 12:39:25 +0200875 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200876
Jens Axboe5380a102006-07-13 12:37:56 +0200877 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200878 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200879 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882/*
883 * return expired entry, or NULL to just start from scratch in rbtree
884 */
Jens Axboe5e705372006-07-13 12:39:25 +0200885static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200888 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200889 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Jens Axboe3b181522005-06-27 10:56:24 +0200891 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100893
894 cfq_mark_cfqq_fifo_expire(cfqq);
895
Jens Axboe89850f72006-07-22 16:48:31 +0200896 if (list_empty(&cfqq->fifo))
897 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jens Axboe6d048f52007-04-25 12:44:27 +0200899 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200900 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Jens Axboe6d048f52007-04-25 12:44:27 +0200902 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
903 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Jens Axboe6d048f52007-04-25 12:44:27 +0200905 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Jens Axboe22e2c502005-06-27 10:55:12 +0200908static inline int
909cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
910{
911 const int base_rq = cfqd->cfq_slice_async_rq;
912
913 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
914
915 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
916}
917
918/*
919 * get next queue for service
920 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100921static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200922{
Jens Axboe22e2c502005-06-27 10:55:12 +0200923 struct cfq_queue *cfqq;
924
925 cfqq = cfqd->active_queue;
926 if (!cfqq)
927 goto new_queue;
928
929 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200930 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200931 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200932 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200933 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200934
935 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200936 * The active queue has requests and isn't expired, allow it to
937 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200938 */
Jens Axboedd67d052006-06-21 09:36:18 +0200939 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200940 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200941
942 /*
943 * No requests pending. If the active queue still has requests in
944 * flight or is idling for a new request, allow either of these
945 * conditions to happen (or time out) before selecting a new queue.
946 */
947 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +0200948 cfqq = NULL;
949 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200950 }
951
Jens Axboe3b181522005-06-27 10:56:24 +0200952expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100953 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200954new_queue:
955 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200956keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200957 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200958}
959
960static int
961__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
962 int max_dispatch)
963{
964 int dispatched = 0;
965
Jens Axboedd67d052006-06-21 09:36:18 +0200966 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200967
968 do {
Jens Axboe5e705372006-07-13 12:39:25 +0200969 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200970
971 /*
972 * follow expired path, else get first next available
973 */
Jens Axboe5e705372006-07-13 12:39:25 +0200974 if ((rq = cfq_check_fifo(cfqq)) == NULL)
975 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200976
977 /*
978 * finally, insert request into driver dispatch list
979 */
Jens Axboe5e705372006-07-13 12:39:25 +0200980 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200981
982 cfqd->dispatch_slice++;
983 dispatched++;
984
985 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +0200986 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
987 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200988 }
989
Jens Axboedd67d052006-06-21 09:36:18 +0200990 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200991 break;
992
993 } while (dispatched < max_dispatch);
994
995 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200996 * expire an async queue immediately if it has used up its slice. idle
997 * queue always expire after 1 dispatch round.
998 */
Jens Axboea9938002007-04-20 08:55:52 +0200999 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001000 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001001 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001002 cfqq->slice_end = jiffies + 1;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001003 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001004 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001005
1006 return dispatched;
1007}
1008
Jens Axboed9e76202007-04-20 14:27:50 +02001009static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1010{
1011 int dispatched = 0;
1012
1013 while (cfqq->next_rq) {
1014 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1015 dispatched++;
1016 }
1017
1018 BUG_ON(!list_empty(&cfqq->fifo));
1019 return dispatched;
1020}
1021
1022static int cfq_forced_dispatch_cfqqs(struct list_head *list)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001023{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001024 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001025 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001026
Jens Axboecaaa5f92006-06-16 11:23:00 +02001027 dispatched = 0;
Jens Axboed9e76202007-04-20 14:27:50 +02001028 list_for_each_entry_safe(cfqq, next, list, cfq_list)
1029 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001030
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001031 return dispatched;
1032}
1033
Jens Axboed9e76202007-04-20 14:27:50 +02001034static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001035{
Jens Axboed9e76202007-04-20 14:27:50 +02001036 int dispatched = 0;
1037 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001038
Jens Axboed9e76202007-04-20 14:27:50 +02001039 while ((n = rb_first(&cfqd->service_tree)) != NULL) {
1040 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1041
1042 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1043 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001044
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001045 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1046 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1047
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001048 cfq_slice_expired(cfqd, 0, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001049
1050 BUG_ON(cfqd->busy_queues);
1051
1052 return dispatched;
1053}
1054
Jens Axboed9e76202007-04-20 14:27:50 +02001055static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001058 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001059 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jens Axboe22e2c502005-06-27 10:55:12 +02001061 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001064 if (unlikely(force))
1065 return cfq_forced_dispatch(cfqd);
1066
Jens Axboecaaa5f92006-06-16 11:23:00 +02001067 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001068 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001069 int max_dispatch;
1070
Jens Axboea9938002007-04-20 08:55:52 +02001071 if (cfqd->busy_queues > 1) {
1072 /*
Jens Axboea9938002007-04-20 08:55:52 +02001073 * So we have dispatched before in this round, if the
1074 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001075 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001076 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001077 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1078 dispatched)
1079 break;
1080 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001081 break;
1082 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001083
Jens Axboe3b181522005-06-27 10:56:24 +02001084 cfq_clear_cfqq_must_dispatch(cfqq);
1085 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001086 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001088 max_dispatch = cfqd->cfq_quantum;
1089 if (cfq_class_idle(cfqq))
1090 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Jens Axboecaaa5f92006-06-16 11:23:00 +02001092 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Jens Axboecaaa5f92006-06-16 11:23:00 +02001095 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098/*
Jens Axboe5e705372006-07-13 12:39:25 +02001099 * task holds one reference to the queue, dropped when task exits. each rq
1100 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *
1102 * queue lock must be held here.
1103 */
1104static void cfq_put_queue(struct cfq_queue *cfqq)
1105{
Jens Axboe22e2c502005-06-27 10:55:12 +02001106 struct cfq_data *cfqd = cfqq->cfqd;
1107
1108 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (!atomic_dec_and_test(&cfqq->ref))
1111 return;
1112
1113 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001114 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001115 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001117 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001118 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001119 cfq_schedule_dispatch(cfqd);
1120 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * it's on the empty list and still hashed
1124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 hlist_del(&cfqq->cfq_hash);
1126 kmem_cache_free(cfq_pool, cfqq);
1127}
1128
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001129static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001130__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1131 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001134 struct hlist_node *entry;
1135 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jens Axboe206dc692006-03-28 13:03:44 +02001137 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001138 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jens Axboe206dc692006-03-28 13:03:44 +02001140 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return __cfqq;
1142 }
1143
1144 return NULL;
1145}
1146
1147static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001148cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Jens Axboe3b181522005-06-27 10:56:24 +02001150 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Jens Axboee2d74ac2006-03-28 08:59:01 +02001153static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Jens Axboe22e2c502005-06-27 10:55:12 +02001155 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001156 struct rb_node *n;
1157 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001158
Jens Axboee2d74ac2006-03-28 08:59:01 +02001159 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1160 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1161 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001162 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001163 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001164 }
1165
Jens Axboe4050cf12006-07-19 05:07:12 +02001166 elv_ioc_count_mod(ioc_count, -freed);
1167
1168 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001169 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Jens Axboe89850f72006-07-22 16:48:31 +02001172static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1173{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001174 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001175 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001176 cfq_schedule_dispatch(cfqd);
1177 }
Jens Axboe89850f72006-07-22 16:48:31 +02001178
1179 cfq_put_queue(cfqq);
1180}
1181
1182static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1183 struct cfq_io_context *cic)
1184{
Jens Axboefc463792006-08-29 09:05:44 +02001185 list_del_init(&cic->queue_list);
1186 smp_wmb();
1187 cic->key = NULL;
1188
Jens Axboe89850f72006-07-22 16:48:31 +02001189 if (cic->cfqq[ASYNC]) {
1190 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1191 cic->cfqq[ASYNC] = NULL;
1192 }
1193
1194 if (cic->cfqq[SYNC]) {
1195 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1196 cic->cfqq[SYNC] = NULL;
1197 }
Jens Axboe89850f72006-07-22 16:48:31 +02001198}
1199
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001202 * Called with interrupts disabled
1203 */
1204static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1205{
Al Viro478a82b2006-03-18 13:25:24 -05001206 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001207
Jens Axboe89850f72006-07-22 16:48:31 +02001208 if (cfqd) {
1209 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001210
Jens Axboefc463792006-08-29 09:05:44 +02001211 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001212 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001213 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001214 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001215}
1216
Jens Axboee2d74ac2006-03-28 08:59:01 +02001217static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Jens Axboe22e2c502005-06-27 10:55:12 +02001219 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001220 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * put the reference this task is holding to the various queues
1224 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001225
1226 n = rb_first(&ioc->cic_root);
1227 while (n != NULL) {
1228 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1229
Jens Axboe22e2c502005-06-27 10:55:12 +02001230 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001231 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Jens Axboe22e2c502005-06-27 10:55:12 +02001235static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001236cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Jens Axboeb5deef92006-07-19 23:39:40 +02001238 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jens Axboeb5deef92006-07-19 23:39:40 +02001240 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001242 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001243 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001244 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001245 cic->dtor = cfq_free_io_context;
1246 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001247 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
1250 return cic;
1251}
1252
Jens Axboe22e2c502005-06-27 10:55:12 +02001253static void cfq_init_prio_data(struct cfq_queue *cfqq)
1254{
1255 struct task_struct *tsk = current;
1256 int ioprio_class;
1257
Jens Axboe3b181522005-06-27 10:56:24 +02001258 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001259 return;
1260
1261 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1262 switch (ioprio_class) {
1263 default:
1264 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1265 case IOPRIO_CLASS_NONE:
1266 /*
1267 * no prio set, place us in the middle of the BE classes
1268 */
1269 cfqq->ioprio = task_nice_ioprio(tsk);
1270 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1271 break;
1272 case IOPRIO_CLASS_RT:
1273 cfqq->ioprio = task_ioprio(tsk);
1274 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1275 break;
1276 case IOPRIO_CLASS_BE:
1277 cfqq->ioprio = task_ioprio(tsk);
1278 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1279 break;
1280 case IOPRIO_CLASS_IDLE:
1281 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1282 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001283 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001284 break;
1285 }
1286
1287 /*
1288 * keep track of original prio settings in case we have to temporarily
1289 * elevate the priority of this queue
1290 */
1291 cfqq->org_ioprio = cfqq->ioprio;
1292 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001293 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001294}
1295
Al Viro478a82b2006-03-18 13:25:24 -05001296static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001297{
Al Viro478a82b2006-03-18 13:25:24 -05001298 struct cfq_data *cfqd = cic->key;
1299 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001300 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001301
Jens Axboecaaa5f92006-06-16 11:23:00 +02001302 if (unlikely(!cfqd))
1303 return;
1304
Jens Axboec1b707d2006-10-30 19:54:23 +01001305 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001306
1307 cfqq = cic->cfqq[ASYNC];
1308 if (cfqq) {
1309 struct cfq_queue *new_cfqq;
1310 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1311 GFP_ATOMIC);
1312 if (new_cfqq) {
1313 cic->cfqq[ASYNC] = new_cfqq;
1314 cfq_put_queue(cfqq);
1315 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001316 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001317
1318 cfqq = cic->cfqq[SYNC];
1319 if (cfqq)
1320 cfq_mark_cfqq_prio_changed(cfqq);
1321
Jens Axboec1b707d2006-10-30 19:54:23 +01001322 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001323}
1324
Jens Axboefc463792006-08-29 09:05:44 +02001325static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Al Viroa6a07632006-03-18 13:26:44 -05001327 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001328 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001329
Jens Axboefc463792006-08-29 09:05:44 +02001330 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001331
Jens Axboee2d74ac2006-03-28 08:59:01 +02001332 n = rb_first(&ioc->cic_root);
1333 while (n != NULL) {
1334 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001335
Al Viro478a82b2006-03-18 13:25:24 -05001336 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001337 n = rb_next(n);
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001342cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001343 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1346 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001347 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349retry:
Al Viro6f325a12006-03-18 14:58:37 -05001350 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001351 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (!cfqq) {
1354 if (new_cfqq) {
1355 cfqq = new_cfqq;
1356 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001357 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001358 /*
1359 * Inform the allocator of the fact that we will
1360 * just repeat this allocation if it fails, to allow
1361 * the allocator to do whatever it needs to attempt to
1362 * free memory.
1363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001365 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_lock_irq(cfqd->queue->queue_lock);
1367 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001368 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001369 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001370 if (!cfqq)
1371 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 memset(cfqq, 0, sizeof(*cfqq));
1375
1376 INIT_HLIST_NODE(&cfqq->cfq_hash);
1377 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboed9e76202007-04-20 14:27:50 +02001378 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001379 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 cfqq->key = key;
1382 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1383 atomic_set(&cfqq->ref, 0);
1384 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001385
Jens Axboea9938002007-04-20 08:55:52 +02001386 if (key != CFQ_KEY_ASYNC)
1387 cfq_mark_cfqq_idle_window(cfqq);
1388
Jens Axboe3b181522005-06-27 10:56:24 +02001389 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001390 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001391 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
1394 if (new_cfqq)
1395 kmem_cache_free(cfq_pool, new_cfqq);
1396
1397 atomic_inc(&cfqq->ref);
1398out:
1399 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1400 return cfqq;
1401}
1402
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001403static void
1404cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1405{
Jens Axboefc463792006-08-29 09:05:44 +02001406 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001407 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001408 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001409 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001410}
1411
Jens Axboee2d74ac2006-03-28 08:59:01 +02001412static struct cfq_io_context *
1413cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1414{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001415 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001416 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001417 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001418
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001419restart:
1420 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001421 while (n) {
1422 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001423 /* ->key must be copied to avoid race with cfq_exit_queue() */
1424 k = cic->key;
1425 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001426 cfq_drop_dead_cic(ioc, cic);
1427 goto restart;
1428 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001429
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001430 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001431 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001432 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001433 n = n->rb_right;
1434 else
1435 return cic;
1436 }
1437
1438 return NULL;
1439}
1440
1441static inline void
1442cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1443 struct cfq_io_context *cic)
1444{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001445 struct rb_node **p;
1446 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001447 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001448 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001449 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001450
Jens Axboee2d74ac2006-03-28 08:59:01 +02001451 cic->ioc = ioc;
1452 cic->key = cfqd;
1453
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001454restart:
1455 parent = NULL;
1456 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001457 while (*p) {
1458 parent = *p;
1459 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001460 /* ->key must be copied to avoid race with cfq_exit_queue() */
1461 k = __cic->key;
1462 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001463 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001464 goto restart;
1465 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001466
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001467 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001468 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001469 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001470 p = &(*p)->rb_right;
1471 else
1472 BUG();
1473 }
1474
1475 rb_link_node(&cic->rb_node, parent, p);
1476 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001477
Jens Axboe0261d682006-10-30 19:07:48 +01001478 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001479 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001480 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001481}
1482
Jens Axboe22e2c502005-06-27 10:55:12 +02001483/*
1484 * Setup general io context and cfq io context. There can be several cfq
1485 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001486 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001487 */
1488static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001489cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Jens Axboe22e2c502005-06-27 10:55:12 +02001491 struct io_context *ioc = NULL;
1492 struct cfq_io_context *cic;
1493
1494 might_sleep_if(gfp_mask & __GFP_WAIT);
1495
Jens Axboeb5deef92006-07-19 23:39:40 +02001496 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001497 if (!ioc)
1498 return NULL;
1499
Jens Axboee2d74ac2006-03-28 08:59:01 +02001500 cic = cfq_cic_rb_lookup(cfqd, ioc);
1501 if (cic)
1502 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001503
Jens Axboee2d74ac2006-03-28 08:59:01 +02001504 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1505 if (cic == NULL)
1506 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001507
Jens Axboee2d74ac2006-03-28 08:59:01 +02001508 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001509out:
Jens Axboefc463792006-08-29 09:05:44 +02001510 smp_read_barrier_depends();
1511 if (unlikely(ioc->ioprio_changed))
1512 cfq_ioc_set_ioprio(ioc);
1513
Jens Axboe22e2c502005-06-27 10:55:12 +02001514 return cic;
1515err:
1516 put_io_context(ioc);
1517 return NULL;
1518}
1519
1520static void
1521cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1522{
Jens Axboeaaf12282007-01-19 11:30:16 +11001523 unsigned long elapsed = jiffies - cic->last_end_request;
1524 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001525
1526 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1527 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1528 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1529}
1530
Jens Axboe206dc692006-03-28 13:03:44 +02001531static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001532cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1533 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001534{
1535 sector_t sdist;
1536 u64 total;
1537
Jens Axboe5e705372006-07-13 12:39:25 +02001538 if (cic->last_request_pos < rq->sector)
1539 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001540 else
Jens Axboe5e705372006-07-13 12:39:25 +02001541 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001542
Jens Axboe6d048f52007-04-25 12:44:27 +02001543 if (!cic->seek_samples) {
1544 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1545 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1546 }
1547
Jens Axboe206dc692006-03-28 13:03:44 +02001548 /*
1549 * Don't allow the seek distance to get too large from the
1550 * odd fragment, pagein, etc
1551 */
1552 if (cic->seek_samples <= 60) /* second&third seek */
1553 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1554 else
1555 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1556
1557 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1558 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1559 total = cic->seek_total + (cic->seek_samples/2);
1560 do_div(total, cic->seek_samples);
1561 cic->seek_mean = (sector_t)total;
1562}
Jens Axboe22e2c502005-06-27 10:55:12 +02001563
1564/*
1565 * Disable idle window if the process thinks too long or seeks so much that
1566 * it doesn't matter
1567 */
1568static void
1569cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1570 struct cfq_io_context *cic)
1571{
Jens Axboe3b181522005-06-27 10:56:24 +02001572 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001573
Jens Axboecaaa5f92006-06-16 11:23:00 +02001574 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1575 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001576 enable_idle = 0;
1577 else if (sample_valid(cic->ttime_samples)) {
1578 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1579 enable_idle = 0;
1580 else
1581 enable_idle = 1;
1582 }
1583
Jens Axboe3b181522005-06-27 10:56:24 +02001584 if (enable_idle)
1585 cfq_mark_cfqq_idle_window(cfqq);
1586 else
1587 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001588}
1589
Jens Axboe22e2c502005-06-27 10:55:12 +02001590/*
1591 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1592 * no or if we aren't sure, a 1 will cause a preempt.
1593 */
1594static int
1595cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001596 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001597{
Jens Axboe6d048f52007-04-25 12:44:27 +02001598 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001599
Jens Axboe6d048f52007-04-25 12:44:27 +02001600 cfqq = cfqd->active_queue;
1601 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001602 return 0;
1603
Jens Axboe6d048f52007-04-25 12:44:27 +02001604 if (cfq_slice_used(cfqq))
1605 return 1;
1606
1607 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001608 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001609
1610 if (cfq_class_idle(cfqq))
1611 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001612
Jens Axboe22e2c502005-06-27 10:55:12 +02001613 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001614 * if the new request is sync, but the currently running queue is
1615 * not, let the sync request have priority.
1616 */
Jens Axboe5e705372006-07-13 12:39:25 +02001617 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001618 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001619
Jens Axboe374f84a2006-07-23 01:42:19 +02001620 /*
1621 * So both queues are sync. Let the new request get disk time if
1622 * it's a metadata request and the current queue is doing regular IO.
1623 */
1624 if (rq_is_meta(rq) && !cfqq->meta_pending)
1625 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001626
Jens Axboe1e3335d2007-02-14 19:59:49 +01001627 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1628 return 0;
1629
1630 /*
1631 * if this request is as-good as one we would expect from the
1632 * current cfqq, let it preempt
1633 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001634 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001635 return 1;
1636
Jens Axboe22e2c502005-06-27 10:55:12 +02001637 return 0;
1638}
1639
1640/*
1641 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1642 * let it have half of its nominal slice.
1643 */
1644static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1645{
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001646 cfq_slice_expired(cfqd, 1, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001647
Jens Axboebf572252006-07-19 20:29:12 +02001648 /*
1649 * Put the new queue at the front of the of the current list,
1650 * so we know that it will be selected next.
1651 */
1652 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboed9e76202007-04-20 14:27:50 +02001653 list_del_init(&cfqq->cfq_list);
1654 list_add(&cfqq->cfq_list, &cfqd->cur_rr);
Jens Axboebf572252006-07-19 20:29:12 +02001655
Jens Axboe44f7c162007-01-19 11:51:58 +11001656 cfqq->slice_end = 0;
1657 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001658}
1659
1660/*
Jens Axboe5e705372006-07-13 12:39:25 +02001661 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001662 * something we should do about it
1663 */
1664static void
Jens Axboe5e705372006-07-13 12:39:25 +02001665cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1666 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001667{
Jens Axboe5e705372006-07-13 12:39:25 +02001668 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001669
Jens Axboe374f84a2006-07-23 01:42:19 +02001670 if (rq_is_meta(rq))
1671 cfqq->meta_pending++;
1672
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001673 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001674 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001675 cfq_update_idle_window(cfqd, cfqq, cic);
1676
Jens Axboe5e705372006-07-13 12:39:25 +02001677 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001678 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001679
1680 if (cfqq == cfqd->active_queue) {
1681 /*
1682 * if we are waiting for a request for this queue, let it rip
1683 * immediately and flag that we must not expire this queue
1684 * just now
1685 */
Jens Axboe3b181522005-06-27 10:56:24 +02001686 if (cfq_cfqq_wait_request(cfqq)) {
1687 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001688 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001689 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001690 }
Jens Axboe5e705372006-07-13 12:39:25 +02001691 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001692 /*
1693 * not the active queue - expire current slice if it is
1694 * idle and has expired it's mean thinktime or this new queue
1695 * has some old slice time left and is of higher priority
1696 */
1697 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001698 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001699 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001700 }
1701}
1702
Jens Axboeb4878f22005-10-20 16:42:29 +02001703static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001704{
Jens Axboeb4878f22005-10-20 16:42:29 +02001705 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001706 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001707
1708 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Jens Axboe5e705372006-07-13 12:39:25 +02001710 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Jens Axboe22e2c502005-06-27 10:55:12 +02001712 list_add_tail(&rq->queuelist, &cfqq->fifo);
1713
Jens Axboe5e705372006-07-13 12:39:25 +02001714 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static void cfq_completed_request(request_queue_t *q, struct request *rq)
1718{
Jens Axboe5e705372006-07-13 12:39:25 +02001719 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001720 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001721 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001722 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Jens Axboeb4878f22005-10-20 16:42:29 +02001724 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jens Axboeb4878f22005-10-20 16:42:29 +02001726 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001727 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001728 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001729 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Jens Axboeb4878f22005-10-20 16:42:29 +02001731 if (!cfq_class_idle(cfqq))
1732 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001733
Jens Axboecaaa5f92006-06-16 11:23:00 +02001734 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001735 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001736
1737 /*
1738 * If this is the active queue, check if it needs to be expired,
1739 * or if we want to idle in case it has no pending requests.
1740 */
1741 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001742 if (cfq_cfqq_slice_new(cfqq)) {
1743 cfq_set_prio_slice(cfqd, cfqq);
1744 cfq_clear_cfqq_slice_new(cfqq);
1745 }
1746 if (cfq_slice_used(cfqq))
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001747 cfq_slice_expired(cfqd, 0, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001748 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1749 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001750 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001751
1752 if (!cfqd->rq_in_driver)
1753 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Jens Axboe22e2c502005-06-27 10:55:12 +02001756/*
1757 * we temporarily boost lower priority queues if they are holding fs exclusive
1758 * resources. they are boosted to normal prio (CLASS_BE/4)
1759 */
1760static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Jens Axboe22e2c502005-06-27 10:55:12 +02001762 if (has_fs_excl()) {
1763 /*
1764 * boost idle prio on transactions that would lock out other
1765 * users of the filesystem
1766 */
1767 if (cfq_class_idle(cfqq))
1768 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1769 if (cfqq->ioprio > IOPRIO_NORM)
1770 cfqq->ioprio = IOPRIO_NORM;
1771 } else {
1772 /*
1773 * check if we need to unboost the queue
1774 */
1775 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1776 cfqq->ioprio_class = cfqq->org_ioprio_class;
1777 if (cfqq->ioprio != cfqq->org_ioprio)
1778 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001780}
1781
Jens Axboe89850f72006-07-22 16:48:31 +02001782static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001783{
Jens Axboe3b181522005-06-27 10:56:24 +02001784 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001785 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001786 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001787 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001788 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001789
1790 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001791}
1792
Jens Axboecb78b282006-07-28 09:32:57 +02001793static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001794{
1795 struct cfq_data *cfqd = q->elevator->elevator_data;
1796 struct task_struct *tsk = current;
1797 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001798 unsigned int key;
1799
1800 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001801
1802 /*
1803 * don't force setup of a queue from here, as a call to may_queue
1804 * does not necessarily imply that a request actually will be queued.
1805 * so just lookup a possibly existing queue, or return 'may queue'
1806 * if that fails
1807 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001808 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001809 if (cfqq) {
1810 cfq_init_prio_data(cfqq);
1811 cfq_prio_boost(cfqq);
1812
Jens Axboe89850f72006-07-22 16:48:31 +02001813 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001814 }
1815
1816 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/*
1820 * queue lock held here
1821 */
Jens Axboebb37b942006-12-01 10:42:33 +01001822static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jens Axboe5e705372006-07-13 12:39:25 +02001824 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Jens Axboe5e705372006-07-13 12:39:25 +02001826 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001827 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jens Axboe22e2c502005-06-27 10:55:12 +02001829 BUG_ON(!cfqq->allocated[rw]);
1830 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jens Axboe5e705372006-07-13 12:39:25 +02001832 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001835 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 cfq_put_queue(cfqq);
1838 }
1839}
1840
1841/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001842 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001844static int
Jens Axboecb78b282006-07-28 09:32:57 +02001845cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001848 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct cfq_io_context *cic;
1850 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001851 const int is_sync = rq_is_sync(rq);
1852 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001853 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 unsigned long flags;
1855
1856 might_sleep_if(gfp_mask & __GFP_WAIT);
1857
Jens Axboee2d74ac2006-03-28 08:59:01 +02001858 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 spin_lock_irqsave(q->queue_lock, flags);
1861
Jens Axboe22e2c502005-06-27 10:55:12 +02001862 if (!cic)
1863 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Al Viro12a05732006-03-18 13:38:01 -05001865 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001866 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001867 if (!cfqq)
1868 goto queue_fail;
1869
Al Viro12a05732006-03-18 13:38:01 -05001870 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001871 } else
Al Viro12a05732006-03-18 13:38:01 -05001872 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001875 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001876 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_unlock_irqrestore(q->queue_lock, flags);
1879
Jens Axboe5e705372006-07-13 12:39:25 +02001880 rq->elevator_private = cic;
1881 rq->elevator_private2 = cfqq;
1882 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001883
Jens Axboe22e2c502005-06-27 10:55:12 +02001884queue_fail:
1885 if (cic)
1886 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001887
Jens Axboe3b181522005-06-27 10:56:24 +02001888 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 spin_unlock_irqrestore(q->queue_lock, flags);
1890 return 1;
1891}
1892
David Howells65f27f32006-11-22 14:55:48 +00001893static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001894{
David Howells65f27f32006-11-22 14:55:48 +00001895 struct cfq_data *cfqd =
1896 container_of(work, struct cfq_data, unplug_work);
1897 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001898 unsigned long flags;
1899
1900 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001901 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001902 spin_unlock_irqrestore(q->queue_lock, flags);
1903}
1904
1905/*
1906 * Timer running if the active_queue is currently idling inside its time slice
1907 */
1908static void cfq_idle_slice_timer(unsigned long data)
1909{
1910 struct cfq_data *cfqd = (struct cfq_data *) data;
1911 struct cfq_queue *cfqq;
1912 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001913 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001914
1915 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1916
1917 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001918 timed_out = 0;
1919
Jens Axboe22e2c502005-06-27 10:55:12 +02001920 /*
1921 * expired
1922 */
Jens Axboe44f7c162007-01-19 11:51:58 +11001923 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001924 goto expire;
1925
1926 /*
1927 * only expire and reinvoke request handler, if there are
1928 * other queues with pending requests
1929 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001930 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001931 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001932
1933 /*
1934 * not expired and it has a request pending, let it dispatch
1935 */
Jens Axboedd67d052006-06-21 09:36:18 +02001936 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001937 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001938 goto out_kick;
1939 }
1940 }
1941expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001942 cfq_slice_expired(cfqd, 0, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02001943out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001944 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001945out_cont:
1946 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1947}
1948
1949/*
1950 * Timer running if an idle class queue is waiting for service
1951 */
1952static void cfq_idle_class_timer(unsigned long data)
1953{
1954 struct cfq_data *cfqd = (struct cfq_data *) data;
1955 unsigned long flags, end;
1956
1957 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1958
1959 /*
1960 * race with a non-idle queue, reset timer
1961 */
1962 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001963 if (!time_after_eq(jiffies, end))
1964 mod_timer(&cfqd->idle_class_timer, end);
1965 else
Jens Axboe3b181522005-06-27 10:56:24 +02001966 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001967
1968 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1969}
1970
Jens Axboe3b181522005-06-27 10:56:24 +02001971static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1972{
1973 del_timer_sync(&cfqd->idle_slice_timer);
1974 del_timer_sync(&cfqd->idle_class_timer);
1975 blk_sync_queue(cfqd->queue);
1976}
Jens Axboe22e2c502005-06-27 10:55:12 +02001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978static void cfq_exit_queue(elevator_t *e)
1979{
Jens Axboe22e2c502005-06-27 10:55:12 +02001980 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05001981 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001982
Jens Axboe3b181522005-06-27 10:56:24 +02001983 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001984
Al Virod9ff4182006-03-18 13:51:22 -05001985 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001986
Al Virod9ff4182006-03-18 13:51:22 -05001987 if (cfqd->active_queue)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001988 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001989
1990 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05001991 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1992 struct cfq_io_context,
1993 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02001994
1995 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05001996 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001997
Al Virod9ff4182006-03-18 13:51:22 -05001998 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05001999
2000 cfq_shutdown_timer_wq(cfqd);
2001
Al Viroa90d7422006-03-18 12:05:37 -05002002 kfree(cfqd->cfq_hash);
2003 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Jens Axboebb37b942006-12-01 10:42:33 +01002006static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 struct cfq_data *cfqd;
2009 int i;
2010
Jens Axboeb5deef92006-07-19 23:39:40 +02002011 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002013 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002016
Jens Axboed9e76202007-04-20 14:27:50 +02002017 cfqd->service_tree = RB_ROOT;
Jens Axboe22e2c502005-06-27 10:55:12 +02002018 INIT_LIST_HEAD(&cfqd->cur_rr);
2019 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05002020 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jens Axboeb5deef92006-07-19 23:39:40 +02002022 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002024 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2027 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Jens Axboe22e2c502005-06-27 10:55:12 +02002031 init_timer(&cfqd->idle_slice_timer);
2032 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2033 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2034
2035 init_timer(&cfqd->idle_class_timer);
2036 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2037 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2038
David Howells65f27f32006-11-22 14:55:48 +00002039 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002042 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2043 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 cfqd->cfq_back_max = cfq_back_max;
2045 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002046 cfqd->cfq_slice[0] = cfq_slice_async;
2047 cfqd->cfq_slice[1] = cfq_slice_sync;
2048 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2049 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002050
Jens Axboebc1c1162006-06-08 08:49:06 +02002051 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002052out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002054 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057static void cfq_slab_kill(void)
2058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (cfq_pool)
2060 kmem_cache_destroy(cfq_pool);
2061 if (cfq_ioc_pool)
2062 kmem_cache_destroy(cfq_ioc_pool);
2063}
2064
2065static int __init cfq_slab_setup(void)
2066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2068 NULL, NULL);
2069 if (!cfq_pool)
2070 goto fail;
2071
2072 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2073 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2074 if (!cfq_ioc_pool)
2075 goto fail;
2076
2077 return 0;
2078fail:
2079 cfq_slab_kill();
2080 return -ENOMEM;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * sysfs parts below -->
2085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086static ssize_t
2087cfq_var_show(unsigned int var, char *page)
2088{
2089 return sprintf(page, "%d\n", var);
2090}
2091
2092static ssize_t
2093cfq_var_store(unsigned int *var, const char *page, size_t count)
2094{
2095 char *p = (char *) page;
2096
2097 *var = simple_strtoul(p, &p, 10);
2098 return count;
2099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002102static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002104 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 unsigned int __data = __VAR; \
2106 if (__CONV) \
2107 __data = jiffies_to_msecs(__data); \
2108 return cfq_var_show(__data, (page)); \
2109}
2110SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002111SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2112SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002113SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2114SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002115SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2116SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2117SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2118SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#undef SHOW_FUNCTION
2120
2121#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002122static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002124 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 unsigned int __data; \
2126 int ret = cfq_var_store(&__data, (page), count); \
2127 if (__data < (MIN)) \
2128 __data = (MIN); \
2129 else if (__data > (MAX)) \
2130 __data = (MAX); \
2131 if (__CONV) \
2132 *(__PTR) = msecs_to_jiffies(__data); \
2133 else \
2134 *(__PTR) = __data; \
2135 return ret; \
2136}
2137STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002138STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2139STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002140STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2141STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002142STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2143STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2144STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2145STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146#undef STORE_FUNCTION
2147
Al Viroe572ec72006-03-18 22:27:18 -05002148#define CFQ_ATTR(name) \
2149 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002150
Al Viroe572ec72006-03-18 22:27:18 -05002151static struct elv_fs_entry cfq_attrs[] = {
2152 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002153 CFQ_ATTR(fifo_expire_sync),
2154 CFQ_ATTR(fifo_expire_async),
2155 CFQ_ATTR(back_seek_max),
2156 CFQ_ATTR(back_seek_penalty),
2157 CFQ_ATTR(slice_sync),
2158 CFQ_ATTR(slice_async),
2159 CFQ_ATTR(slice_async_rq),
2160 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002161 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162};
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164static struct elevator_type iosched_cfq = {
2165 .ops = {
2166 .elevator_merge_fn = cfq_merge,
2167 .elevator_merged_fn = cfq_merged_request,
2168 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002169 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002170 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002172 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .elevator_deactivate_req_fn = cfq_deactivate_request,
2174 .elevator_queue_empty_fn = cfq_queue_empty,
2175 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002176 .elevator_former_req_fn = elv_rb_former_request,
2177 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 .elevator_set_req_fn = cfq_set_request,
2179 .elevator_put_req_fn = cfq_put_request,
2180 .elevator_may_queue_fn = cfq_may_queue,
2181 .elevator_init_fn = cfq_init_queue,
2182 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002183 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 },
Al Viro3d1ab402006-03-18 18:35:43 -05002185 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 .elevator_name = "cfq",
2187 .elevator_owner = THIS_MODULE,
2188};
2189
2190static int __init cfq_init(void)
2191{
2192 int ret;
2193
Jens Axboe22e2c502005-06-27 10:55:12 +02002194 /*
2195 * could be 0 on HZ < 1000 setups
2196 */
2197 if (!cfq_slice_async)
2198 cfq_slice_async = 1;
2199 if (!cfq_slice_idle)
2200 cfq_slice_idle = 1;
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (cfq_slab_setup())
2203 return -ENOMEM;
2204
2205 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002206 if (ret)
2207 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return ret;
2210}
2211
2212static void __exit cfq_exit(void)
2213{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002214 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002216 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002217 /* ioc_gone's update must be visible before reading ioc_count */
2218 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002219 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002220 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002221 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002222 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
2225module_init(cfq_init);
2226module_exit(cfq_exit);
2227
2228MODULE_AUTHOR("Jens Axboe");
2229MODULE_LICENSE("GPL");
2230MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");