blob: 533a2938ffd6740f6f41f2c5bd52177e8dde8a0e [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
29#define CFQ_IDLE_GRACE (HZ / 10)
30#define CFQ_SLICE_SCALE (5)
31
32#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * for the hash of cfqq inside the cfqd
36 */
37#define CFQ_QHASH_SHIFT 6
38#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
39#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
42
Jens Axboe5e705372006-07-13 12:39:25 +020043#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
44#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *cfq_pool;
47static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jens Axboe4050cf12006-07-19 05:07:12 +020049static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050050static struct completion *ioc_gone;
51
Jens Axboe22e2c502005-06-27 10:55:12 +020052#define CFQ_PRIO_LISTS IOPRIO_BE_NR
53#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020054#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
55
Jens Axboe3b181522005-06-27 10:56:24 +020056#define ASYNC (0)
57#define SYNC (1)
58
59#define cfq_cfqq_dispatched(cfqq) \
60 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
61
62#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
63
64#define cfq_cfqq_sync(cfqq) \
65 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020066
Jens Axboe206dc692006-03-28 13:03:44 +020067#define sample_valid(samples) ((samples) > 80)
68
Jens Axboe22e2c502005-06-27 10:55:12 +020069/*
70 * Per block device queue structure
71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020073 request_queue_t *queue;
74
75 /*
76 * rr list of queues with requests and the count of them
77 */
78 struct list_head rr_list[CFQ_PRIO_LISTS];
79 struct list_head busy_rr;
80 struct list_head cur_rr;
81 struct list_head idle_rr;
82 unsigned int busy_queues;
83
84 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020085 * cfqq lookup hash
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jens Axboe22e2c502005-06-27 10:55:12 +020089 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020090 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020091
92 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020093 * idle window management
94 */
95 struct timer_list idle_slice_timer;
96 struct work_struct unplug_work;
97
98 struct cfq_queue *active_queue;
99 struct cfq_io_context *active_cic;
100 int cur_prio, cur_end_prio;
101 unsigned int dispatch_slice;
102
103 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200106 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * tunables, see top of file
110 */
111 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200112 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int cfq_back_penalty;
114 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200115 unsigned int cfq_slice[2];
116 unsigned int cfq_slice_async_rq;
117 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500118
119 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Jens Axboe22e2c502005-06-27 10:55:12 +0200122/*
123 * Per process-grouping structure
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct cfq_queue {
126 /* reference count */
127 atomic_t ref;
128 /* parent cfq_data */
129 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200130 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct hlist_node cfq_hash;
132 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200133 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200134 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct list_head cfq_list;
136 /* sorted list of pending requests */
137 struct rb_root sort_list;
138 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200139 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* requests queued in sort_list */
141 int queued[2];
142 /* currently allocated requests */
143 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200144 /* pending metadata requests */
145 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200147 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jens Axboe22e2c502005-06-27 10:55:12 +0200149 unsigned long slice_start;
150 unsigned long slice_end;
151 unsigned long slice_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jens Axboe3b181522005-06-27 10:56:24 +0200153 /* number of requests that are on the dispatch list */
154 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200155
156 /* io prio of this group */
157 unsigned short ioprio, org_ioprio;
158 unsigned short ioprio_class, org_ioprio_class;
159
Jens Axboe3b181522005-06-27 10:56:24 +0200160 /* various state flags, see below */
161 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Jens Axboe3b181522005-06-27 10:56:24 +0200164enum cfqq_state_flags {
165 CFQ_CFQQ_FLAG_on_rr = 0,
166 CFQ_CFQQ_FLAG_wait_request,
167 CFQ_CFQQ_FLAG_must_alloc,
168 CFQ_CFQQ_FLAG_must_alloc_slice,
169 CFQ_CFQQ_FLAG_must_dispatch,
170 CFQ_CFQQ_FLAG_fifo_expire,
171 CFQ_CFQQ_FLAG_idle_window,
172 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe53b03742006-07-28 09:48:51 +0200173 CFQ_CFQQ_FLAG_queue_new,
Jens Axboe3b181522005-06-27 10:56:24 +0200174};
175
176#define CFQ_CFQQ_FNS(name) \
177static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
178{ \
179 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
180} \
181static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
182{ \
183 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
184} \
185static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
186{ \
187 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
188}
189
190CFQ_CFQQ_FNS(on_rr);
191CFQ_CFQQ_FNS(wait_request);
192CFQ_CFQQ_FNS(must_alloc);
193CFQ_CFQQ_FNS(must_alloc_slice);
194CFQ_CFQQ_FNS(must_dispatch);
195CFQ_CFQQ_FNS(fifo_expire);
196CFQ_CFQQ_FNS(idle_window);
197CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200198CFQ_CFQQ_FNS(queue_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200199#undef CFQ_CFQQ_FNS
200
Jens Axboe3b181522005-06-27 10:56:24 +0200201static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200202static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500203static 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 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700206 * scheduler run of queue, if there are requests pending and no one in the
207 * driver that will restart queueing
208 */
209static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
210{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100211 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700212 kblockd_schedule_work(&cfqd->unplug_work);
213}
214
215static int cfq_queue_empty(request_queue_t *q)
216{
217 struct cfq_data *cfqd = q->elevator->elevator_data;
218
Jens Axboeb4878f22005-10-20 16:42:29 +0200219 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700220}
221
Jens Axboe7749a8d2006-12-13 13:02:26 +0100222static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200223{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100224 /*
225 * Use the per-process queue, for read requests and syncronous writes
226 */
227 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200228 return task->pid;
229
230 return CFQ_KEY_ASYNC;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
Jens Axboe5e705372006-07-13 12:39:25 +0200234 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200236 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Jens Axboe5e705372006-07-13 12:39:25 +0200238static struct request *
239cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200243#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
244#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
245 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jens Axboe5e705372006-07-13 12:39:25 +0200247 if (rq1 == NULL || rq1 == rq2)
248 return rq2;
249 if (rq2 == NULL)
250 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200251
Jens Axboe5e705372006-07-13 12:39:25 +0200252 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
253 return rq1;
254 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
255 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200256 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
257 return rq1;
258 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
259 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jens Axboe5e705372006-07-13 12:39:25 +0200261 s1 = rq1->sector;
262 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 last = cfqd->last_sector;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /*
267 * by definition, 1KiB is 2 sectors
268 */
269 back_max = cfqd->cfq_back_max * 2;
270
271 /*
272 * Strict one way elevator _except_ in the case where we allow
273 * short backward seeks which are biased as twice the cost of a
274 * similar forward seek.
275 */
276 if (s1 >= last)
277 d1 = s1 - last;
278 else if (s1 + back_max >= last)
279 d1 = (last - s1) * cfqd->cfq_back_penalty;
280 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200281 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (s2 >= last)
284 d2 = s2 - last;
285 else if (s2 + back_max >= last)
286 d2 = (last - s2) * cfqd->cfq_back_penalty;
287 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200288 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Andreas Mohre8a99052006-03-28 08:59:49 +0200292 /*
293 * By doing switch() on the bit mask "wrap" we avoid having to
294 * check two variables for all permutations: --> faster!
295 */
296 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200297 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200298 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200299 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200300 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200301 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200302 else {
303 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200304 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200305 else
Jens Axboe5e705372006-07-13 12:39:25 +0200306 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200307 }
308
309 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200310 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200311 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200312 return rq2;
313 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200314 default:
315 /*
316 * Since both rqs are wrapped,
317 * start with the one that's further behind head
318 * (--> only *one* back seek required),
319 * since back seek takes more time than forward.
320 */
321 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200322 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 else
Jens Axboe5e705372006-07-13 12:39:25 +0200324 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326}
327
328/*
329 * would be nice to take fifo expire time into account as well
330 */
Jens Axboe5e705372006-07-13 12:39:25 +0200331static struct request *
332cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
333 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Jens Axboe21183b02006-07-13 12:33:14 +0200335 struct rb_node *rbnext = rb_next(&last->rb_node);
336 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200337 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jens Axboe21183b02006-07-13 12:33:14 +0200339 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200342 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200345 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200346 else {
347 rbnext = rb_first(&cfqq->sort_list);
348 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200349 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jens Axboe21183b02006-07-13 12:33:14 +0200352 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Jens Axboe22e2c502005-06-27 10:55:12 +0200355static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Jens Axboe22e2c502005-06-27 10:55:12 +0200357 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe53b03742006-07-28 09:48:51 +0200358 struct list_head *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jens Axboe3b181522005-06-27 10:56:24 +0200360 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 list_del(&cfqq->cfq_list);
363
Jens Axboe22e2c502005-06-27 10:55:12 +0200364 if (cfq_class_rt(cfqq))
365 list = &cfqd->cur_rr;
366 else if (cfq_class_idle(cfqq))
367 list = &cfqd->idle_rr;
368 else {
369 /*
370 * if cfqq has requests in flight, don't allow it to be
371 * found in cfq_set_active_queue before it has finished them.
372 * this is done to increase fairness between a process that
373 * has lots of io pending vs one that only generates one
374 * sporadically or synchronously
375 */
Jens Axboe3b181522005-06-27 10:56:24 +0200376 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200377 list = &cfqd->busy_rr;
378 else
379 list = &cfqd->rr_list[cfqq->ioprio];
380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
Jens Axboe53b03742006-07-28 09:48:51 +0200383 * If this queue was preempted or is new (never been serviced), let
384 * it be added first for fairness but beind other new queues.
385 * Otherwise, just add to the back of the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Jens Axboe53b03742006-07-28 09:48:51 +0200387 if (preempted || cfq_cfqq_queue_new(cfqq)) {
388 struct list_head *n = list;
389 struct cfq_queue *__cfqq;
Jens Axboeb52a8342006-06-01 18:53:43 +0200390
Jens Axboe53b03742006-07-28 09:48:51 +0200391 while (n->next != list) {
392 __cfqq = list_entry_cfqq(n->next);
393 if (!cfq_cfqq_queue_new(__cfqq))
394 break;
395
396 n = n->next;
397 }
398
399 list = n;
Jens Axboe22e2c502005-06-27 10:55:12 +0200400 }
401
Jens Axboe53b03742006-07-28 09:48:51 +0200402 list_add_tail(&cfqq->cfq_list, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200407 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200410cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jens Axboe3b181522005-06-27 10:56:24 +0200412 BUG_ON(cfq_cfqq_on_rr(cfqq));
413 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 cfqd->busy_queues++;
415
Jens Axboeb4878f22005-10-20 16:42:29 +0200416 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static inline void
420cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
421{
Jens Axboe3b181522005-06-27 10:56:24 +0200422 BUG_ON(!cfq_cfqq_on_rr(cfqq));
423 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200424 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 BUG_ON(!cfqd->busy_queues);
427 cfqd->busy_queues--;
428}
429
430/*
431 * rb tree support functions
432 */
Jens Axboe5e705372006-07-13 12:39:25 +0200433static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jens Axboe5e705372006-07-13 12:39:25 +0200435 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200436 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200437 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jens Axboeb4878f22005-10-20 16:42:29 +0200439 BUG_ON(!cfqq->queued[sync]);
440 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jens Axboe5e705372006-07-13 12:39:25 +0200442 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jens Axboedd67d052006-06-21 09:36:18 +0200444 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200445 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Jens Axboe5e705372006-07-13 12:39:25 +0200448static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Jens Axboe5e705372006-07-13 12:39:25 +0200450 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200452 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Jens Axboe5380a102006-07-13 12:37:56 +0200454 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /*
457 * looks a little odd, but the first insert might return an alias.
458 * if that happens, put the alias on the dispatch list
459 */
Jens Axboe21183b02006-07-13 12:33:14 +0200460 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200461 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100462
463 if (!cfq_cfqq_on_rr(cfqq))
464 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200468cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jens Axboe5380a102006-07-13 12:37:56 +0200470 elv_rb_del(&cfqq->sort_list, rq);
471 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200472 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Jens Axboe206dc692006-03-28 13:03:44 +0200475static struct request *
476cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jens Axboe206dc692006-03-28 13:03:44 +0200478 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100479 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200480 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Jens Axboe206dc692006-03-28 13:03:44 +0200482 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200483 if (cfqq) {
484 sector_t sector = bio->bi_sector + bio_sectors(bio);
485
Jens Axboe21183b02006-07-13 12:33:14 +0200486 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return NULL;
490}
491
Jens Axboeb4878f22005-10-20 16:42:29 +0200492static void cfq_activate_request(request_queue_t *q, struct request *rq)
493{
494 struct cfq_data *cfqd = q->elevator->elevator_data;
495
496 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200497
498 /*
499 * If the depth is larger 1, it really could be queueing. But lets
500 * make the mark a little higher - idling could still be good for
501 * low queueing, and a low queueing number could also just indicate
502 * a SCSI mid layer like behaviour where limit+1 is often seen.
503 */
504 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
505 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
509{
Jens Axboe22e2c502005-06-27 10:55:12 +0200510 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Jens Axboeb4878f22005-10-20 16:42:29 +0200512 WARN_ON(!cfqd->rq_in_driver);
513 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Jens Axboeb4878f22005-10-20 16:42:29 +0200516static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Jens Axboe5e705372006-07-13 12:39:25 +0200518 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200519
Jens Axboe5e705372006-07-13 12:39:25 +0200520 if (cfqq->next_rq == rq)
521 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Jens Axboeb4878f22005-10-20 16:42:29 +0200523 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200524 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200525
526 if (rq_is_meta(rq)) {
527 WARN_ON(!cfqq->meta_pending);
528 cfqq->meta_pending--;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static int
533cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
534{
535 struct cfq_data *cfqd = q->elevator->elevator_data;
536 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jens Axboe206dc692006-03-28 13:03:44 +0200538 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200539 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200540 *req = __rq;
541 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Jens Axboe21183b02006-07-13 12:33:14 +0200547static void cfq_merged_request(request_queue_t *q, struct request *req,
548 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Jens Axboe21183b02006-07-13 12:33:14 +0200550 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200551 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jens Axboe5e705372006-07-13 12:39:25 +0200553 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static void
558cfq_merged_requests(request_queue_t *q, struct request *rq,
559 struct request *next)
560{
Jens Axboe22e2c502005-06-27 10:55:12 +0200561 /*
562 * reposition in fifo if next is older than rq
563 */
564 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
565 time_before(next->start_time, rq->start_time))
566 list_move(&rq->queuelist, &next->queuelist);
567
Jens Axboeb4878f22005-10-20 16:42:29 +0200568 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200569}
570
571static inline void
572__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
573{
574 if (cfqq) {
575 /*
576 * stop potential idle class queues waiting service
577 */
578 del_timer(&cfqd->idle_class_timer);
579
580 cfqq->slice_start = jiffies;
581 cfqq->slice_end = 0;
582 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200583 cfq_clear_cfqq_must_alloc_slice(cfqq);
584 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200585 }
586
587 cfqd->active_queue = cfqq;
588}
589
590/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100591 * current cfqq expired its slice (or was too idle), select new one
592 */
593static void
594__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
595 int preempted)
596{
597 unsigned long now = jiffies;
598
599 if (cfq_cfqq_wait_request(cfqq))
600 del_timer(&cfqd->idle_slice_timer);
601
Jens Axboe53b03742006-07-28 09:48:51 +0200602 if (!preempted && !cfq_cfqq_dispatched(cfqq))
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100603 cfq_schedule_dispatch(cfqd);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100604
605 cfq_clear_cfqq_must_dispatch(cfqq);
606 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +0200607 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100608
609 /*
610 * store what was left of this slice, if the queue idled out
611 * or was preempted
612 */
613 if (time_after(cfqq->slice_end, now))
614 cfqq->slice_left = cfqq->slice_end - now;
615 else
616 cfqq->slice_left = 0;
617
618 if (cfq_cfqq_on_rr(cfqq))
619 cfq_resort_rr_list(cfqq, preempted);
620
621 if (cfqq == cfqd->active_queue)
622 cfqd->active_queue = NULL;
623
624 if (cfqd->active_cic) {
625 put_io_context(cfqd->active_cic->ioc);
626 cfqd->active_cic = NULL;
627 }
628
629 cfqd->dispatch_slice = 0;
630}
631
632static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
633{
634 struct cfq_queue *cfqq = cfqd->active_queue;
635
636 if (cfqq)
637 __cfq_slice_expired(cfqd, cfqq, preempted);
638}
639
640/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200641 * 0
642 * 0,1
643 * 0,1,2
644 * 0,1,2,3
645 * 0,1,2,3,4
646 * 0,1,2,3,4,5
647 * 0,1,2,3,4,5,6
648 * 0,1,2,3,4,5,6,7
649 */
650static int cfq_get_next_prio_level(struct cfq_data *cfqd)
651{
652 int prio, wrap;
653
654 prio = -1;
655 wrap = 0;
656 do {
657 int p;
658
659 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
660 if (!list_empty(&cfqd->rr_list[p])) {
661 prio = p;
662 break;
663 }
664 }
665
666 if (prio != -1)
667 break;
668 cfqd->cur_prio = 0;
669 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
670 cfqd->cur_end_prio = 0;
671 if (wrap)
672 break;
673 wrap = 1;
674 }
675 } while (1);
676
677 if (unlikely(prio == -1))
678 return -1;
679
680 BUG_ON(prio >= CFQ_PRIO_LISTS);
681
682 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
683
684 cfqd->cur_prio = prio + 1;
685 if (cfqd->cur_prio > cfqd->cur_end_prio) {
686 cfqd->cur_end_prio = cfqd->cur_prio;
687 cfqd->cur_prio = 0;
688 }
689 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
690 cfqd->cur_prio = 0;
691 cfqd->cur_end_prio = 0;
692 }
693
694 return prio;
695}
696
Jens Axboe3b181522005-06-27 10:56:24 +0200697static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200698{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100699 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200700
Jens Axboe89850f72006-07-22 16:48:31 +0200701 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
702 /*
703 * if current list is non-empty, grab first entry. if it is
704 * empty, get next prio level and grab first entry then if any
705 * are spliced
706 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200707 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200708 } else if (!list_empty(&cfqd->busy_rr)) {
709 /*
710 * If no new queues are available, check if the busy list has
711 * some before falling back to idle io.
712 */
Jens Axboee0de0202006-06-01 10:07:26 +0200713 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200714 } else if (!list_empty(&cfqd->idle_rr)) {
715 /*
716 * if we have idle queues and no rt or be queues had pending
717 * requests, either allow immediate service if the grace period
718 * has passed or arm the idle grace timer
719 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200720 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
721
722 if (time_after_eq(jiffies, end))
723 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
724 else
725 mod_timer(&cfqd->idle_class_timer, end);
726 }
727
728 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200729 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200730}
731
Jens Axboecaaa5f92006-06-16 11:23:00 +0200732#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
733
Jens Axboe22e2c502005-06-27 10:55:12 +0200734static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
735
736{
Jens Axboe206dc692006-03-28 13:03:44 +0200737 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100738 unsigned long sl;
739
Jens Axboedd67d052006-06-21 09:36:18 +0200740 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200741 WARN_ON(cfqq != cfqd->active_queue);
742
743 /*
744 * idle is disabled, either manually or by past process history
745 */
746 if (!cfqd->cfq_slice_idle)
747 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200748 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200749 return 0;
750 /*
751 * task has exited, don't wait
752 */
Jens Axboe206dc692006-03-28 13:03:44 +0200753 cic = cfqd->active_cic;
754 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200755 return 0;
756
Jens Axboe3b181522005-06-27 10:56:24 +0200757 cfq_mark_cfqq_must_dispatch(cfqq);
758 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200759
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100760 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200761
762 /*
763 * we don't want to idle for seeks, but we do want to allow
764 * fair distribution of slice time for a process doing back-to-back
765 * seeks. so allow a little bit of time for him to submit a new rq
766 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200767 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200768 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200769
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100770 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200771 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Jens Axboe5e705372006-07-13 12:39:25 +0200774static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200777 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200778
Jens Axboe5380a102006-07-13 12:37:56 +0200779 cfq_remove_request(rq);
780 cfqq->on_dispatch[rq_is_sync(rq)]++;
781 elv_dispatch_sort(q, rq);
Jens Axboefd61af02006-06-16 15:35:39 +0200782
783 rq = list_entry(q->queue_head.prev, struct request, queuelist);
784 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * return expired entry, or NULL to just start from scratch in rbtree
789 */
Jens Axboe5e705372006-07-13 12:39:25 +0200790static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200793 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200794 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Jens Axboe3b181522005-06-27 10:56:24 +0200796 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return NULL;
Jens Axboe89850f72006-07-22 16:48:31 +0200798 if (list_empty(&cfqq->fifo))
799 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Jens Axboe89850f72006-07-22 16:48:31 +0200801 fifo = cfq_cfqq_class_sync(cfqq);
802 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jens Axboe89850f72006-07-22 16:48:31 +0200804 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
805 cfq_mark_cfqq_fifo_expire(cfqq);
806 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
809 return NULL;
810}
811
812/*
Jens Axboe3b181522005-06-27 10:56:24 +0200813 * Scale schedule slice based on io priority. Use the sync time slice only
814 * if a queue is marked sync and has sync io queued. A sync queue with async
815 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200817static inline int
818cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Jens Axboe22e2c502005-06-27 10:55:12 +0200820 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jens Axboe22e2c502005-06-27 10:55:12 +0200822 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jens Axboe22e2c502005-06-27 10:55:12 +0200824 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Jens Axboe22e2c502005-06-27 10:55:12 +0200827static inline void
828cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
829{
830 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
831}
832
833static inline int
834cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
835{
836 const int base_rq = cfqd->cfq_slice_async_rq;
837
838 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
839
840 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
841}
842
843/*
844 * get next queue for service
845 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100846static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200847{
848 unsigned long now = jiffies;
849 struct cfq_queue *cfqq;
850
851 cfqq = cfqd->active_queue;
852 if (!cfqq)
853 goto new_queue;
854
855 /*
856 * slice has expired
857 */
Jens Axboe3b181522005-06-27 10:56:24 +0200858 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
859 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200860
861 /*
862 * if queue has requests, dispatch one. if not, check if
863 * enough slice is left to wait for one
864 */
Jens Axboedd67d052006-06-21 09:36:18 +0200865 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200866 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200867 else if (cfq_cfqq_dispatched(cfqq)) {
868 cfqq = NULL;
869 goto keep_queue;
870 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200871 if (cfq_arm_slice_timer(cfqd, cfqq))
872 return NULL;
873 }
874
Jens Axboe3b181522005-06-27 10:56:24 +0200875expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200876 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200877new_queue:
878 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200879keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200880 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200881}
882
883static int
884__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
885 int max_dispatch)
886{
887 int dispatched = 0;
888
Jens Axboedd67d052006-06-21 09:36:18 +0200889 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200890
891 do {
Jens Axboe5e705372006-07-13 12:39:25 +0200892 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200893
894 /*
895 * follow expired path, else get first next available
896 */
Jens Axboe5e705372006-07-13 12:39:25 +0200897 if ((rq = cfq_check_fifo(cfqq)) == NULL)
898 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200899
900 /*
901 * finally, insert request into driver dispatch list
902 */
Jens Axboe5e705372006-07-13 12:39:25 +0200903 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200904
905 cfqd->dispatch_slice++;
906 dispatched++;
907
908 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +0200909 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
910 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200911 }
912
Jens Axboedd67d052006-06-21 09:36:18 +0200913 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200914 break;
915
916 } while (dispatched < max_dispatch);
917
918 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +0200919 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +0200920 */
921 if (!cfqq->slice_end)
922 cfq_set_prio_slice(cfqd, cfqq);
923
924 /*
925 * expire an async queue immediately if it has used up its slice. idle
926 * queue always expire after 1 dispatch round.
927 */
928 if ((!cfq_cfqq_sync(cfqq) &&
929 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +0200930 cfq_class_idle(cfqq) ||
931 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200932 cfq_slice_expired(cfqd, 0);
933
934 return dispatched;
935}
936
937static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100938cfq_forced_dispatch_cfqqs(struct list_head *list)
939{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100940 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200941 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100942
Jens Axboecaaa5f92006-06-16 11:23:00 +0200943 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100944 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +0200945 while (cfqq->next_rq) {
946 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100947 dispatched++;
948 }
949 BUG_ON(!list_empty(&cfqq->fifo));
950 }
Jens Axboecaaa5f92006-06-16 11:23:00 +0200951
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100952 return dispatched;
953}
954
955static int
956cfq_forced_dispatch(struct cfq_data *cfqd)
957{
958 int i, dispatched = 0;
959
960 for (i = 0; i < CFQ_PRIO_LISTS; i++)
961 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
962
963 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
964 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
965 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
966
967 cfq_slice_expired(cfqd, 0);
968
969 BUG_ON(cfqd->busy_queues);
970
971 return dispatched;
972}
973
974static int
Jens Axboeb4878f22005-10-20 16:42:29 +0200975cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200978 struct cfq_queue *cfqq, *prev_cfqq;
979 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Jens Axboe22e2c502005-06-27 10:55:12 +0200981 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return 0;
983
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100984 if (unlikely(force))
985 return cfq_forced_dispatch(cfqd);
986
Jens Axboecaaa5f92006-06-16 11:23:00 +0200987 dispatched = 0;
988 prev_cfqq = NULL;
989 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +0200990 int max_dispatch;
991
Jens Axboecaaa5f92006-06-16 11:23:00 +0200992 /*
993 * Don't repeat dispatch from the previous queue.
994 */
995 if (prev_cfqq == cfqq)
996 break;
997
Jens Axboe3b181522005-06-27 10:56:24 +0200998 cfq_clear_cfqq_must_dispatch(cfqq);
999 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001000 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001002 max_dispatch = cfqd->cfq_quantum;
1003 if (cfq_class_idle(cfqq))
1004 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jens Axboecaaa5f92006-06-16 11:23:00 +02001006 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1007
1008 /*
1009 * If the dispatch cfqq has idling enabled and is still
1010 * the active queue, break out.
1011 */
1012 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1013 break;
1014
1015 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Jens Axboecaaa5f92006-06-16 11:23:00 +02001018 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
Jens Axboe5e705372006-07-13 12:39:25 +02001022 * task holds one reference to the queue, dropped when task exits. each rq
1023 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *
1025 * queue lock must be held here.
1026 */
1027static void cfq_put_queue(struct cfq_queue *cfqq)
1028{
Jens Axboe22e2c502005-06-27 10:55:12 +02001029 struct cfq_data *cfqd = cfqq->cfqd;
1030
1031 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 if (!atomic_dec_and_test(&cfqq->ref))
1034 return;
1035
1036 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001037 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001038 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001040 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001041 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * it's on the empty list and still hashed
1045 */
1046 list_del(&cfqq->cfq_list);
1047 hlist_del(&cfqq->cfq_hash);
1048 kmem_cache_free(cfq_pool, cfqq);
1049}
1050
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001051static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001052__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1053 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001056 struct hlist_node *entry;
1057 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Jens Axboe206dc692006-03-28 13:03:44 +02001059 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001060 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Jens Axboe206dc692006-03-28 13:03:44 +02001062 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return __cfqq;
1064 }
1065
1066 return NULL;
1067}
1068
1069static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001070cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jens Axboe3b181522005-06-27 10:56:24 +02001072 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Jens Axboee2d74ac2006-03-28 08:59:01 +02001075static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Jens Axboe22e2c502005-06-27 10:55:12 +02001077 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001078 struct rb_node *n;
1079 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001080
Jens Axboee2d74ac2006-03-28 08:59:01 +02001081 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1082 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1083 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001084 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001085 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001086 }
1087
Jens Axboe4050cf12006-07-19 05:07:12 +02001088 elv_ioc_count_mod(ioc_count, -freed);
1089
1090 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001091 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Jens Axboe89850f72006-07-22 16:48:31 +02001094static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1095{
1096 if (unlikely(cfqq == cfqd->active_queue))
1097 __cfq_slice_expired(cfqd, cfqq, 0);
1098
1099 cfq_put_queue(cfqq);
1100}
1101
1102static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1103 struct cfq_io_context *cic)
1104{
Jens Axboefc463792006-08-29 09:05:44 +02001105 list_del_init(&cic->queue_list);
1106 smp_wmb();
1107 cic->key = NULL;
1108
Jens Axboe89850f72006-07-22 16:48:31 +02001109 if (cic->cfqq[ASYNC]) {
1110 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1111 cic->cfqq[ASYNC] = NULL;
1112 }
1113
1114 if (cic->cfqq[SYNC]) {
1115 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1116 cic->cfqq[SYNC] = NULL;
1117 }
Jens Axboe89850f72006-07-22 16:48:31 +02001118}
1119
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001122 * Called with interrupts disabled
1123 */
1124static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1125{
Al Viro478a82b2006-03-18 13:25:24 -05001126 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001127
Jens Axboe89850f72006-07-22 16:48:31 +02001128 if (cfqd) {
1129 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001130
Jens Axboefc463792006-08-29 09:05:44 +02001131 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001132 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001133 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001134 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001135}
1136
Jens Axboee2d74ac2006-03-28 08:59:01 +02001137static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jens Axboe22e2c502005-06-27 10:55:12 +02001139 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001140 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * put the reference this task is holding to the various queues
1144 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001145
1146 n = rb_first(&ioc->cic_root);
1147 while (n != NULL) {
1148 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1149
Jens Axboe22e2c502005-06-27 10:55:12 +02001150 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001151 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Jens Axboe22e2c502005-06-27 10:55:12 +02001155static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001156cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Jens Axboeb5deef92006-07-19 23:39:40 +02001158 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jens Axboeb5deef92006-07-19 23:39:40 +02001160 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001162 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001163 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001164 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001165 cic->dtor = cfq_free_io_context;
1166 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001167 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169
1170 return cic;
1171}
1172
Jens Axboe22e2c502005-06-27 10:55:12 +02001173static void cfq_init_prio_data(struct cfq_queue *cfqq)
1174{
1175 struct task_struct *tsk = current;
1176 int ioprio_class;
1177
Jens Axboe3b181522005-06-27 10:56:24 +02001178 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001179 return;
1180
1181 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1182 switch (ioprio_class) {
1183 default:
1184 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1185 case IOPRIO_CLASS_NONE:
1186 /*
1187 * no prio set, place us in the middle of the BE classes
1188 */
1189 cfqq->ioprio = task_nice_ioprio(tsk);
1190 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1191 break;
1192 case IOPRIO_CLASS_RT:
1193 cfqq->ioprio = task_ioprio(tsk);
1194 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1195 break;
1196 case IOPRIO_CLASS_BE:
1197 cfqq->ioprio = task_ioprio(tsk);
1198 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1199 break;
1200 case IOPRIO_CLASS_IDLE:
1201 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1202 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001203 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001204 break;
1205 }
1206
1207 /*
1208 * keep track of original prio settings in case we have to temporarily
1209 * elevate the priority of this queue
1210 */
1211 cfqq->org_ioprio = cfqq->ioprio;
1212 cfqq->org_ioprio_class = cfqq->ioprio_class;
1213
Jens Axboe3b181522005-06-27 10:56:24 +02001214 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001215 cfq_resort_rr_list(cfqq, 0);
1216
Jens Axboe3b181522005-06-27 10:56:24 +02001217 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001218}
1219
Al Viro478a82b2006-03-18 13:25:24 -05001220static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001221{
Al Viro478a82b2006-03-18 13:25:24 -05001222 struct cfq_data *cfqd = cic->key;
1223 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001224 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001225
Jens Axboecaaa5f92006-06-16 11:23:00 +02001226 if (unlikely(!cfqd))
1227 return;
1228
Jens Axboec1b707d2006-10-30 19:54:23 +01001229 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001230
1231 cfqq = cic->cfqq[ASYNC];
1232 if (cfqq) {
1233 struct cfq_queue *new_cfqq;
1234 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1235 GFP_ATOMIC);
1236 if (new_cfqq) {
1237 cic->cfqq[ASYNC] = new_cfqq;
1238 cfq_put_queue(cfqq);
1239 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001240 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001241
1242 cfqq = cic->cfqq[SYNC];
1243 if (cfqq)
1244 cfq_mark_cfqq_prio_changed(cfqq);
1245
Jens Axboec1b707d2006-10-30 19:54:23 +01001246 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001247}
1248
Jens Axboefc463792006-08-29 09:05:44 +02001249static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Al Viroa6a07632006-03-18 13:26:44 -05001251 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001252 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001253
Jens Axboefc463792006-08-29 09:05:44 +02001254 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001255
Jens Axboee2d74ac2006-03-28 08:59:01 +02001256 n = rb_first(&ioc->cic_root);
1257 while (n != NULL) {
1258 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001259
Al Viro478a82b2006-03-18 13:25:24 -05001260 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001261 n = rb_next(n);
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001266cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001267 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1270 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001271 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273retry:
Al Viro6f325a12006-03-18 14:58:37 -05001274 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001275 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (!cfqq) {
1278 if (new_cfqq) {
1279 cfqq = new_cfqq;
1280 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001281 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001282 /*
1283 * Inform the allocator of the fact that we will
1284 * just repeat this allocation if it fails, to allow
1285 * the allocator to do whatever it needs to attempt to
1286 * free memory.
1287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001289 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 spin_lock_irq(cfqd->queue->queue_lock);
1291 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001292 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001293 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001294 if (!cfqq)
1295 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 memset(cfqq, 0, sizeof(*cfqq));
1299
1300 INIT_HLIST_NODE(&cfqq->cfq_hash);
1301 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001302 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 cfqq->key = key;
1305 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1306 atomic_set(&cfqq->ref, 0);
1307 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001308 /*
1309 * set ->slice_left to allow preemption for a new process
1310 */
1311 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001312 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001313 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001314 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001315 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 if (new_cfqq)
1319 kmem_cache_free(cfq_pool, new_cfqq);
1320
1321 atomic_inc(&cfqq->ref);
1322out:
1323 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1324 return cfqq;
1325}
1326
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001327static void
1328cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1329{
Jens Axboefc463792006-08-29 09:05:44 +02001330 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001331 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001332 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001333 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001334}
1335
Jens Axboee2d74ac2006-03-28 08:59:01 +02001336static struct cfq_io_context *
1337cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1338{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001339 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001340 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001341 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001342
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001343restart:
1344 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001345 while (n) {
1346 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001347 /* ->key must be copied to avoid race with cfq_exit_queue() */
1348 k = cic->key;
1349 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001350 cfq_drop_dead_cic(ioc, cic);
1351 goto restart;
1352 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001353
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001354 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001355 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001356 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001357 n = n->rb_right;
1358 else
1359 return cic;
1360 }
1361
1362 return NULL;
1363}
1364
1365static inline void
1366cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1367 struct cfq_io_context *cic)
1368{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001369 struct rb_node **p;
1370 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001371 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001372 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001373 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001374
Jens Axboee2d74ac2006-03-28 08:59:01 +02001375 cic->ioc = ioc;
1376 cic->key = cfqd;
1377
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001378restart:
1379 parent = NULL;
1380 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001381 while (*p) {
1382 parent = *p;
1383 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001384 /* ->key must be copied to avoid race with cfq_exit_queue() */
1385 k = __cic->key;
1386 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001387 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001388 goto restart;
1389 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001390
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001391 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001392 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001393 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001394 p = &(*p)->rb_right;
1395 else
1396 BUG();
1397 }
1398
1399 rb_link_node(&cic->rb_node, parent, p);
1400 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001401
Jens Axboe0261d682006-10-30 19:07:48 +01001402 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001403 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001404 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001405}
1406
Jens Axboe22e2c502005-06-27 10:55:12 +02001407/*
1408 * Setup general io context and cfq io context. There can be several cfq
1409 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001410 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001411 */
1412static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001413cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Jens Axboe22e2c502005-06-27 10:55:12 +02001415 struct io_context *ioc = NULL;
1416 struct cfq_io_context *cic;
1417
1418 might_sleep_if(gfp_mask & __GFP_WAIT);
1419
Jens Axboeb5deef92006-07-19 23:39:40 +02001420 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001421 if (!ioc)
1422 return NULL;
1423
Jens Axboee2d74ac2006-03-28 08:59:01 +02001424 cic = cfq_cic_rb_lookup(cfqd, ioc);
1425 if (cic)
1426 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001427
Jens Axboee2d74ac2006-03-28 08:59:01 +02001428 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1429 if (cic == NULL)
1430 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001431
Jens Axboee2d74ac2006-03-28 08:59:01 +02001432 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001433out:
Jens Axboefc463792006-08-29 09:05:44 +02001434 smp_read_barrier_depends();
1435 if (unlikely(ioc->ioprio_changed))
1436 cfq_ioc_set_ioprio(ioc);
1437
Jens Axboe22e2c502005-06-27 10:55:12 +02001438 return cic;
1439err:
1440 put_io_context(ioc);
1441 return NULL;
1442}
1443
1444static void
1445cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1446{
1447 unsigned long elapsed, ttime;
1448
1449 /*
1450 * if this context already has stuff queued, thinktime is from
1451 * last queue not last end
1452 */
1453#if 0
1454 if (time_after(cic->last_end_request, cic->last_queue))
1455 elapsed = jiffies - cic->last_end_request;
1456 else
1457 elapsed = jiffies - cic->last_queue;
1458#else
1459 elapsed = jiffies - cic->last_end_request;
1460#endif
1461
1462 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1463
1464 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1465 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1466 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1467}
1468
Jens Axboe206dc692006-03-28 13:03:44 +02001469static void
Jens Axboebb37b942006-12-01 10:42:33 +01001470cfq_update_io_seektime(struct cfq_io_context *cic, struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001471{
1472 sector_t sdist;
1473 u64 total;
1474
Jens Axboe5e705372006-07-13 12:39:25 +02001475 if (cic->last_request_pos < rq->sector)
1476 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001477 else
Jens Axboe5e705372006-07-13 12:39:25 +02001478 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001479
1480 /*
1481 * Don't allow the seek distance to get too large from the
1482 * odd fragment, pagein, etc
1483 */
1484 if (cic->seek_samples <= 60) /* second&third seek */
1485 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1486 else
1487 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1488
1489 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1490 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1491 total = cic->seek_total + (cic->seek_samples/2);
1492 do_div(total, cic->seek_samples);
1493 cic->seek_mean = (sector_t)total;
1494}
Jens Axboe22e2c502005-06-27 10:55:12 +02001495
1496/*
1497 * Disable idle window if the process thinks too long or seeks so much that
1498 * it doesn't matter
1499 */
1500static void
1501cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1502 struct cfq_io_context *cic)
1503{
Jens Axboe3b181522005-06-27 10:56:24 +02001504 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001505
Jens Axboecaaa5f92006-06-16 11:23:00 +02001506 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1507 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001508 enable_idle = 0;
1509 else if (sample_valid(cic->ttime_samples)) {
1510 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1511 enable_idle = 0;
1512 else
1513 enable_idle = 1;
1514 }
1515
Jens Axboe3b181522005-06-27 10:56:24 +02001516 if (enable_idle)
1517 cfq_mark_cfqq_idle_window(cfqq);
1518 else
1519 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001520}
1521
1522
1523/*
1524 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1525 * no or if we aren't sure, a 1 will cause a preempt.
1526 */
1527static int
1528cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001529 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001530{
1531 struct cfq_queue *cfqq = cfqd->active_queue;
1532
1533 if (cfq_class_idle(new_cfqq))
1534 return 0;
1535
1536 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001537 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001538
1539 if (cfq_class_idle(cfqq))
1540 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001541 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001542 return 0;
1543 /*
1544 * if it doesn't have slice left, forget it
1545 */
1546 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1547 return 0;
Jens Axboe374f84a2006-07-23 01:42:19 +02001548 /*
1549 * if the new request is sync, but the currently running queue is
1550 * not, let the sync request have priority.
1551 */
Jens Axboe5e705372006-07-13 12:39:25 +02001552 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001553 return 1;
Jens Axboe374f84a2006-07-23 01:42:19 +02001554 /*
1555 * So both queues are sync. Let the new request get disk time if
1556 * it's a metadata request and the current queue is doing regular IO.
1557 */
1558 if (rq_is_meta(rq) && !cfqq->meta_pending)
1559 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001560
1561 return 0;
1562}
1563
1564/*
1565 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1566 * let it have half of its nominal slice.
1567 */
1568static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1569{
Jens Axboebf572252006-07-19 20:29:12 +02001570 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001571
1572 if (!cfqq->slice_left)
1573 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1574
Jens Axboebf572252006-07-19 20:29:12 +02001575 /*
1576 * Put the new queue at the front of the of the current list,
1577 * so we know that it will be selected next.
1578 */
1579 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1580 list_move(&cfqq->cfq_list, &cfqd->cur_rr);
1581
Jens Axboe22e2c502005-06-27 10:55:12 +02001582 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001583}
1584
1585/*
Jens Axboe5e705372006-07-13 12:39:25 +02001586 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001587 * something we should do about it
1588 */
1589static void
Jens Axboe5e705372006-07-13 12:39:25 +02001590cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1591 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001592{
Jens Axboe5e705372006-07-13 12:39:25 +02001593 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001594
Jens Axboe374f84a2006-07-23 01:42:19 +02001595 if (rq_is_meta(rq))
1596 cfqq->meta_pending++;
1597
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001598 /*
Jens Axboe5380a102006-07-13 12:37:56 +02001599 * check if this request is a better next-serve candidate)) {
Jens Axboe21183b02006-07-13 12:33:14 +02001600 */
Jens Axboe5e705372006-07-13 12:39:25 +02001601 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
1602 BUG_ON(!cfqq->next_rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001603
1604 /*
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001605 * we never wait for an async request and we don't allow preemption
1606 * of an async request. so just return early
1607 */
Jens Axboe5e705372006-07-13 12:39:25 +02001608 if (!rq_is_sync(rq)) {
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001609 /*
1610 * sync process issued an async request, if it's waiting
1611 * then expire it and kick rq handling.
1612 */
1613 if (cic == cfqd->active_cic &&
1614 del_timer(&cfqd->idle_slice_timer)) {
1615 cfq_slice_expired(cfqd, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +02001616 blk_start_queueing(cfqd->queue);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001617 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001618 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001619 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001620
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001621 cfq_update_io_thinktime(cfqd, cic);
Jens Axboebb37b942006-12-01 10:42:33 +01001622 cfq_update_io_seektime(cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001623 cfq_update_idle_window(cfqd, cfqq, cic);
1624
1625 cic->last_queue = jiffies;
Jens Axboe5e705372006-07-13 12:39:25 +02001626 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001627
1628 if (cfqq == cfqd->active_queue) {
1629 /*
1630 * if we are waiting for a request for this queue, let it rip
1631 * immediately and flag that we must not expire this queue
1632 * just now
1633 */
Jens Axboe3b181522005-06-27 10:56:24 +02001634 if (cfq_cfqq_wait_request(cfqq)) {
1635 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001636 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001637 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001638 }
Jens Axboe5e705372006-07-13 12:39:25 +02001639 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001640 /*
1641 * not the active queue - expire current slice if it is
1642 * idle and has expired it's mean thinktime or this new queue
1643 * has some old slice time left and is of higher priority
1644 */
1645 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001646 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001647 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001648 }
1649}
1650
Jens Axboeb4878f22005-10-20 16:42:29 +02001651static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001652{
Jens Axboeb4878f22005-10-20 16:42:29 +02001653 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001654 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001655
1656 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jens Axboe5e705372006-07-13 12:39:25 +02001658 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Jens Axboe22e2c502005-06-27 10:55:12 +02001660 list_add_tail(&rq->queuelist, &cfqq->fifo);
1661
Jens Axboe5e705372006-07-13 12:39:25 +02001662 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static void cfq_completed_request(request_queue_t *q, struct request *rq)
1666{
Jens Axboe5e705372006-07-13 12:39:25 +02001667 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001668 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001669 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001670 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Jens Axboeb4878f22005-10-20 16:42:29 +02001672 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jens Axboeb4878f22005-10-20 16:42:29 +02001674 WARN_ON(!cfqd->rq_in_driver);
1675 WARN_ON(!cfqq->on_dispatch[sync]);
1676 cfqd->rq_in_driver--;
1677 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jens Axboeb4878f22005-10-20 16:42:29 +02001679 if (!cfq_class_idle(cfqq))
1680 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001681
Jens Axboe53b03742006-07-28 09:48:51 +02001682 if (!cfq_cfqq_dispatched(cfqq) && cfq_cfqq_on_rr(cfqq))
1683 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jens Axboecaaa5f92006-06-16 11:23:00 +02001685 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001686 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001687
1688 /*
1689 * If this is the active queue, check if it needs to be expired,
1690 * or if we want to idle in case it has no pending requests.
1691 */
1692 if (cfqd->active_queue == cfqq) {
1693 if (time_after(now, cfqq->slice_end))
1694 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001695 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001696 if (!cfq_arm_slice_timer(cfqd, cfqq))
1697 cfq_schedule_dispatch(cfqd);
1698 }
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Jens Axboe22e2c502005-06-27 10:55:12 +02001702/*
1703 * we temporarily boost lower priority queues if they are holding fs exclusive
1704 * resources. they are boosted to normal prio (CLASS_BE/4)
1705 */
1706static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Jens Axboe22e2c502005-06-27 10:55:12 +02001708 const int ioprio_class = cfqq->ioprio_class;
1709 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jens Axboe22e2c502005-06-27 10:55:12 +02001711 if (has_fs_excl()) {
1712 /*
1713 * boost idle prio on transactions that would lock out other
1714 * users of the filesystem
1715 */
1716 if (cfq_class_idle(cfqq))
1717 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1718 if (cfqq->ioprio > IOPRIO_NORM)
1719 cfqq->ioprio = IOPRIO_NORM;
1720 } else {
1721 /*
1722 * check if we need to unboost the queue
1723 */
1724 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1725 cfqq->ioprio_class = cfqq->org_ioprio_class;
1726 if (cfqq->ioprio != cfqq->org_ioprio)
1727 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
Jens Axboe22e2c502005-06-27 10:55:12 +02001730 /*
1731 * refile between round-robin lists if we moved the priority class
1732 */
1733 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001734 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001735 cfq_resort_rr_list(cfqq, 0);
1736}
1737
Jens Axboe89850f72006-07-22 16:48:31 +02001738static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001739{
Jens Axboe3b181522005-06-27 10:56:24 +02001740 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001741 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001742 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001743 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001744 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001745
1746 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001747}
1748
Jens Axboecb78b282006-07-28 09:32:57 +02001749static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001750{
1751 struct cfq_data *cfqd = q->elevator->elevator_data;
1752 struct task_struct *tsk = current;
1753 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001754 unsigned int key;
1755
1756 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001757
1758 /*
1759 * don't force setup of a queue from here, as a call to may_queue
1760 * does not necessarily imply that a request actually will be queued.
1761 * so just lookup a possibly existing queue, or return 'may queue'
1762 * if that fails
1763 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001764 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001765 if (cfqq) {
1766 cfq_init_prio_data(cfqq);
1767 cfq_prio_boost(cfqq);
1768
Jens Axboe89850f72006-07-22 16:48:31 +02001769 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001770 }
1771
1772 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/*
1776 * queue lock held here
1777 */
Jens Axboebb37b942006-12-01 10:42:33 +01001778static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Jens Axboe5e705372006-07-13 12:39:25 +02001780 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jens Axboe5e705372006-07-13 12:39:25 +02001782 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001783 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jens Axboe22e2c502005-06-27 10:55:12 +02001785 BUG_ON(!cfqq->allocated[rw]);
1786 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Jens Axboe5e705372006-07-13 12:39:25 +02001788 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001791 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 cfq_put_queue(cfqq);
1794 }
1795}
1796
1797/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001798 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001800static int
Jens Axboecb78b282006-07-28 09:32:57 +02001801cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001804 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 struct cfq_io_context *cic;
1806 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001807 const int is_sync = rq_is_sync(rq);
1808 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001809 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 unsigned long flags;
1811
1812 might_sleep_if(gfp_mask & __GFP_WAIT);
1813
Jens Axboee2d74ac2006-03-28 08:59:01 +02001814 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 spin_lock_irqsave(q->queue_lock, flags);
1817
Jens Axboe22e2c502005-06-27 10:55:12 +02001818 if (!cic)
1819 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Al Viro12a05732006-03-18 13:38:01 -05001821 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001822 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001823 if (!cfqq)
1824 goto queue_fail;
1825
Al Viro12a05732006-03-18 13:38:01 -05001826 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001827 } else
Al Viro12a05732006-03-18 13:38:01 -05001828 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001831 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001832 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 spin_unlock_irqrestore(q->queue_lock, flags);
1835
Jens Axboe5e705372006-07-13 12:39:25 +02001836 rq->elevator_private = cic;
1837 rq->elevator_private2 = cfqq;
1838 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001839
Jens Axboe22e2c502005-06-27 10:55:12 +02001840queue_fail:
1841 if (cic)
1842 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001843
Jens Axboe3b181522005-06-27 10:56:24 +02001844 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 spin_unlock_irqrestore(q->queue_lock, flags);
1846 return 1;
1847}
1848
David Howells65f27f32006-11-22 14:55:48 +00001849static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001850{
David Howells65f27f32006-11-22 14:55:48 +00001851 struct cfq_data *cfqd =
1852 container_of(work, struct cfq_data, unplug_work);
1853 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001854 unsigned long flags;
1855
1856 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001857 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 spin_unlock_irqrestore(q->queue_lock, flags);
1859}
1860
1861/*
1862 * Timer running if the active_queue is currently idling inside its time slice
1863 */
1864static void cfq_idle_slice_timer(unsigned long data)
1865{
1866 struct cfq_data *cfqd = (struct cfq_data *) data;
1867 struct cfq_queue *cfqq;
1868 unsigned long flags;
1869
1870 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1871
1872 if ((cfqq = cfqd->active_queue) != NULL) {
1873 unsigned long now = jiffies;
1874
1875 /*
1876 * expired
1877 */
1878 if (time_after(now, cfqq->slice_end))
1879 goto expire;
1880
1881 /*
1882 * only expire and reinvoke request handler, if there are
1883 * other queues with pending requests
1884 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001885 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001886 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001887
1888 /*
1889 * not expired and it has a request pending, let it dispatch
1890 */
Jens Axboedd67d052006-06-21 09:36:18 +02001891 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001892 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001893 goto out_kick;
1894 }
1895 }
1896expire:
1897 cfq_slice_expired(cfqd, 0);
1898out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001899 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001900out_cont:
1901 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1902}
1903
1904/*
1905 * Timer running if an idle class queue is waiting for service
1906 */
1907static void cfq_idle_class_timer(unsigned long data)
1908{
1909 struct cfq_data *cfqd = (struct cfq_data *) data;
1910 unsigned long flags, end;
1911
1912 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1913
1914 /*
1915 * race with a non-idle queue, reset timer
1916 */
1917 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001918 if (!time_after_eq(jiffies, end))
1919 mod_timer(&cfqd->idle_class_timer, end);
1920 else
Jens Axboe3b181522005-06-27 10:56:24 +02001921 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001922
1923 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1924}
1925
Jens Axboe3b181522005-06-27 10:56:24 +02001926static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
1927{
1928 del_timer_sync(&cfqd->idle_slice_timer);
1929 del_timer_sync(&cfqd->idle_class_timer);
1930 blk_sync_queue(cfqd->queue);
1931}
Jens Axboe22e2c502005-06-27 10:55:12 +02001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933static void cfq_exit_queue(elevator_t *e)
1934{
Jens Axboe22e2c502005-06-27 10:55:12 +02001935 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05001936 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001937
Jens Axboe3b181522005-06-27 10:56:24 +02001938 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001939
Al Virod9ff4182006-03-18 13:51:22 -05001940 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001941
Al Virod9ff4182006-03-18 13:51:22 -05001942 if (cfqd->active_queue)
1943 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001944
1945 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05001946 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
1947 struct cfq_io_context,
1948 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02001949
1950 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05001951 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001952
Al Virod9ff4182006-03-18 13:51:22 -05001953 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05001954
1955 cfq_shutdown_timer_wq(cfqd);
1956
Al Viroa90d7422006-03-18 12:05:37 -05001957 kfree(cfqd->cfq_hash);
1958 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Jens Axboebb37b942006-12-01 10:42:33 +01001961static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct cfq_data *cfqd;
1964 int i;
1965
Jens Axboeb5deef92006-07-19 23:39:40 +02001966 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02001968 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02001971
1972 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1973 INIT_LIST_HEAD(&cfqd->rr_list[i]);
1974
1975 INIT_LIST_HEAD(&cfqd->busy_rr);
1976 INIT_LIST_HEAD(&cfqd->cur_rr);
1977 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05001978 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Jens Axboeb5deef92006-07-19 23:39:40 +02001980 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02001982 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
1985 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jens Axboe22e2c502005-06-27 10:55:12 +02001989 init_timer(&cfqd->idle_slice_timer);
1990 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
1991 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
1992
1993 init_timer(&cfqd->idle_class_timer);
1994 cfqd->idle_class_timer.function = cfq_idle_class_timer;
1995 cfqd->idle_class_timer.data = (unsigned long) cfqd;
1996
David Howells65f27f32006-11-22 14:55:48 +00001997 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002000 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2001 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 cfqd->cfq_back_max = cfq_back_max;
2003 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002004 cfqd->cfq_slice[0] = cfq_slice_async;
2005 cfqd->cfq_slice[1] = cfq_slice_sync;
2006 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2007 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002008
Jens Axboebc1c1162006-06-08 08:49:06 +02002009 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002010out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002012 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
2015static void cfq_slab_kill(void)
2016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (cfq_pool)
2018 kmem_cache_destroy(cfq_pool);
2019 if (cfq_ioc_pool)
2020 kmem_cache_destroy(cfq_ioc_pool);
2021}
2022
2023static int __init cfq_slab_setup(void)
2024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2026 NULL, NULL);
2027 if (!cfq_pool)
2028 goto fail;
2029
2030 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2031 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2032 if (!cfq_ioc_pool)
2033 goto fail;
2034
2035 return 0;
2036fail:
2037 cfq_slab_kill();
2038 return -ENOMEM;
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/*
2042 * sysfs parts below -->
2043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045static ssize_t
2046cfq_var_show(unsigned int var, char *page)
2047{
2048 return sprintf(page, "%d\n", var);
2049}
2050
2051static ssize_t
2052cfq_var_store(unsigned int *var, const char *page, size_t count)
2053{
2054 char *p = (char *) page;
2055
2056 *var = simple_strtoul(p, &p, 10);
2057 return count;
2058}
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002061static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002063 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 unsigned int __data = __VAR; \
2065 if (__CONV) \
2066 __data = jiffies_to_msecs(__data); \
2067 return cfq_var_show(__data, (page)); \
2068}
2069SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002070SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2071SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002072SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2073SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002074SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2075SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2076SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2077SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078#undef SHOW_FUNCTION
2079
2080#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002081static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002083 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 unsigned int __data; \
2085 int ret = cfq_var_store(&__data, (page), count); \
2086 if (__data < (MIN)) \
2087 __data = (MIN); \
2088 else if (__data > (MAX)) \
2089 __data = (MAX); \
2090 if (__CONV) \
2091 *(__PTR) = msecs_to_jiffies(__data); \
2092 else \
2093 *(__PTR) = __data; \
2094 return ret; \
2095}
2096STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002097STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2098STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002099STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2100STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002101STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2102STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2103STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2104STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105#undef STORE_FUNCTION
2106
Al Viroe572ec72006-03-18 22:27:18 -05002107#define CFQ_ATTR(name) \
2108 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002109
Al Viroe572ec72006-03-18 22:27:18 -05002110static struct elv_fs_entry cfq_attrs[] = {
2111 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002112 CFQ_ATTR(fifo_expire_sync),
2113 CFQ_ATTR(fifo_expire_async),
2114 CFQ_ATTR(back_seek_max),
2115 CFQ_ATTR(back_seek_penalty),
2116 CFQ_ATTR(slice_sync),
2117 CFQ_ATTR(slice_async),
2118 CFQ_ATTR(slice_async_rq),
2119 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002120 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121};
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static struct elevator_type iosched_cfq = {
2124 .ops = {
2125 .elevator_merge_fn = cfq_merge,
2126 .elevator_merged_fn = cfq_merged_request,
2127 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002128 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002130 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 .elevator_deactivate_req_fn = cfq_deactivate_request,
2132 .elevator_queue_empty_fn = cfq_queue_empty,
2133 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002134 .elevator_former_req_fn = elv_rb_former_request,
2135 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 .elevator_set_req_fn = cfq_set_request,
2137 .elevator_put_req_fn = cfq_put_request,
2138 .elevator_may_queue_fn = cfq_may_queue,
2139 .elevator_init_fn = cfq_init_queue,
2140 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002141 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 },
Al Viro3d1ab402006-03-18 18:35:43 -05002143 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 .elevator_name = "cfq",
2145 .elevator_owner = THIS_MODULE,
2146};
2147
2148static int __init cfq_init(void)
2149{
2150 int ret;
2151
Jens Axboe22e2c502005-06-27 10:55:12 +02002152 /*
2153 * could be 0 on HZ < 1000 setups
2154 */
2155 if (!cfq_slice_async)
2156 cfq_slice_async = 1;
2157 if (!cfq_slice_idle)
2158 cfq_slice_idle = 1;
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (cfq_slab_setup())
2161 return -ENOMEM;
2162
2163 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002164 if (ret)
2165 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return ret;
2168}
2169
2170static void __exit cfq_exit(void)
2171{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002172 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002174 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002175 /* ioc_gone's update must be visible before reading ioc_count */
2176 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002177 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002178 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002179 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002180 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183module_init(cfq_init);
2184module_exit(cfq_exit);
2185
2186MODULE_AUTHOR("Jens Axboe");
2187MODULE_LICENSE("GPL");
2188MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");