blob: 9d6f04103f01ae965959bb8b791373ffb633b020 [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
Jens Axboe6d048f52007-04-25 12:44:27 +020059#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
Jens Axboe22e2c502005-06-27 10:55:12 +020060
Jens Axboe206dc692006-03-28 13:03:44 +020061#define sample_valid(samples) ((samples) > 80)
62
Jens Axboe22e2c502005-06-27 10:55:12 +020063/*
64 * Per block device queue structure
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020067 request_queue_t *queue;
68
69 /*
70 * rr list of queues with requests and the count of them
71 */
72 struct list_head rr_list[CFQ_PRIO_LISTS];
Jens Axboe22e2c502005-06-27 10:55:12 +020073 struct list_head cur_rr;
74 struct list_head idle_rr;
Jens Axboe6d048f52007-04-25 12:44:27 +020075 unsigned long cur_rr_tick;
Jens Axboe22e2c502005-06-27 10:55:12 +020076 unsigned int busy_queues;
77
78 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020079 * cfqq lookup hash
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jens Axboe22e2c502005-06-27 10:55:12 +020083 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020084 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020085
86 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020087 * idle window management
88 */
89 struct timer_list idle_slice_timer;
90 struct work_struct unplug_work;
91
92 struct cfq_queue *active_queue;
93 struct cfq_io_context *active_cic;
94 int cur_prio, cur_end_prio;
Jens Axboe6d048f52007-04-25 12:44:27 +020095 unsigned long prio_time;
Jens Axboe22e2c502005-06-27 10:55:12 +020096 unsigned int dispatch_slice;
97
98 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe6d048f52007-04-25 12:44:27 +0200100 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200101 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /*
104 * tunables, see top of file
105 */
106 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200107 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int cfq_back_penalty;
109 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200110 unsigned int cfq_slice[2];
111 unsigned int cfq_slice_async_rq;
112 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500113
114 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200115
116 sector_t new_seek_mean;
117 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Jens Axboe22e2c502005-06-27 10:55:12 +0200120/*
121 * Per process-grouping structure
122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct cfq_queue {
124 /* reference count */
125 atomic_t ref;
126 /* parent cfq_data */
127 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200128 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct hlist_node cfq_hash;
130 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200131 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200132 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct list_head cfq_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200134 /* in what tick we were last serviced */
135 unsigned long rr_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* 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_end;
Jens Axboe99f96282007-02-05 11:56:25 +0100150 unsigned long service_last;
Jens Axboe6d048f52007-04-25 12:44:27 +0200151 unsigned long slice_start;
Jens Axboec5b680f2007-01-19 11:56:49 +1100152 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jens Axboe6d048f52007-04-25 12:44:27 +0200154 /* number of requests that are on the dispatch list or inside driver */
155 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200156
157 /* io prio of this group */
158 unsigned short ioprio, org_ioprio;
159 unsigned short ioprio_class, org_ioprio_class;
160
Jens Axboe3b181522005-06-27 10:56:24 +0200161 /* various state flags, see below */
162 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200163
164 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Jens Axboe3b181522005-06-27 10:56:24 +0200167enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100168 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
169 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
170 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
171 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
172 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
173 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
174 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
175 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
176 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100177 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200178};
179
180#define CFQ_CFQQ_FNS(name) \
181static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
182{ \
183 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
184} \
185static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
186{ \
187 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
188} \
189static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
190{ \
191 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
192}
193
194CFQ_CFQQ_FNS(on_rr);
195CFQ_CFQQ_FNS(wait_request);
196CFQ_CFQQ_FNS(must_alloc);
197CFQ_CFQQ_FNS(must_alloc_slice);
198CFQ_CFQQ_FNS(must_dispatch);
199CFQ_CFQQ_FNS(fifo_expire);
200CFQ_CFQQ_FNS(idle_window);
201CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200202CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100203CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200204#undef CFQ_CFQQ_FNS
205
Jens Axboe3b181522005-06-27 10:56:24 +0200206static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200207static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500208static 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 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700211 * scheduler run of queue, if there are requests pending and no one in the
212 * driver that will restart queueing
213 */
214static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
215{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100216 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700217 kblockd_schedule_work(&cfqd->unplug_work);
218}
219
220static int cfq_queue_empty(request_queue_t *q)
221{
222 struct cfq_data *cfqd = q->elevator->elevator_data;
223
Jens Axboeb4878f22005-10-20 16:42:29 +0200224 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700225}
226
Jens Axboe7749a8d2006-12-13 13:02:26 +0100227static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200228{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100229 /*
230 * Use the per-process queue, for read requests and syncronous writes
231 */
232 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200233 return task->pid;
234
235 return CFQ_KEY_ASYNC;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100239 * Scale schedule slice based on io priority. Use the sync time slice only
240 * if a queue is marked sync and has sync io queued. A sync queue with async
241 * io only, should not get full sync slice length.
242 */
243static inline int
244cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
245{
246 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
247
248 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
249
250 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
251}
252
253static inline void
254cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
255{
256 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
Jens Axboec5b680f2007-01-19 11:56:49 +1100257 cfqq->slice_end += cfqq->slice_resid;
258
259 /*
260 * Don't carry over residual for more than one slice, we only want
261 * to slightly correct the fairness. Carrying over forever would
262 * easily introduce oscillations.
263 */
264 cfqq->slice_resid = 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200265
266 cfqq->slice_start = jiffies;
Jens Axboe44f7c162007-01-19 11:51:58 +1100267}
268
269/*
270 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
271 * isn't valid until the first request from the dispatch is activated
272 * and the slice time set.
273 */
274static inline int cfq_slice_used(struct cfq_queue *cfqq)
275{
276 if (cfq_cfqq_slice_new(cfqq))
277 return 0;
278 if (time_before(jiffies, cfqq->slice_end))
279 return 0;
280
281 return 1;
282}
283
284/*
Jens Axboe5e705372006-07-13 12:39:25 +0200285 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200287 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Jens Axboe5e705372006-07-13 12:39:25 +0200289static struct request *
290cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200294#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
295#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
296 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Jens Axboe5e705372006-07-13 12:39:25 +0200298 if (rq1 == NULL || rq1 == rq2)
299 return rq2;
300 if (rq2 == NULL)
301 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200302
Jens Axboe5e705372006-07-13 12:39:25 +0200303 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
304 return rq1;
305 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
306 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200307 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
308 return rq1;
309 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
310 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jens Axboe5e705372006-07-13 12:39:25 +0200312 s1 = rq1->sector;
313 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Jens Axboe6d048f52007-04-25 12:44:27 +0200315 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * by definition, 1KiB is 2 sectors
319 */
320 back_max = cfqd->cfq_back_max * 2;
321
322 /*
323 * Strict one way elevator _except_ in the case where we allow
324 * short backward seeks which are biased as twice the cost of a
325 * similar forward seek.
326 */
327 if (s1 >= last)
328 d1 = s1 - last;
329 else if (s1 + back_max >= last)
330 d1 = (last - s1) * cfqd->cfq_back_penalty;
331 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200332 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (s2 >= last)
335 d2 = s2 - last;
336 else if (s2 + back_max >= last)
337 d2 = (last - s2) * cfqd->cfq_back_penalty;
338 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200339 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andreas Mohre8a99052006-03-28 08:59:49 +0200343 /*
344 * By doing switch() on the bit mask "wrap" we avoid having to
345 * check two variables for all permutations: --> faster!
346 */
347 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200348 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200349 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200350 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200351 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200352 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200353 else {
354 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200355 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200356 else
Jens Axboe5e705372006-07-13 12:39:25 +0200357 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200358 }
359
360 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200361 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200362 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200363 return rq2;
364 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200365 default:
366 /*
367 * Since both rqs are wrapped,
368 * start with the one that's further behind head
369 * (--> only *one* back seek required),
370 * since back seek takes more time than forward.
371 */
372 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200373 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 else
Jens Axboe5e705372006-07-13 12:39:25 +0200375 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377}
378
379/*
380 * would be nice to take fifo expire time into account as well
381 */
Jens Axboe5e705372006-07-13 12:39:25 +0200382static struct request *
383cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
384 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jens Axboe21183b02006-07-13 12:33:14 +0200386 struct rb_node *rbnext = rb_next(&last->rb_node);
387 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200388 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jens Axboe21183b02006-07-13 12:33:14 +0200390 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200393 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200396 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200397 else {
398 rbnext = rb_first(&cfqq->sort_list);
399 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200400 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jens Axboe21183b02006-07-13 12:33:14 +0200403 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Jens Axboe6d048f52007-04-25 12:44:27 +0200406/*
407 * This function finds out where to insert a BE queue in the service hierarchy
408 */
409static void cfq_resort_be_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
410 int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jens Axboe1afba042007-04-17 12:47:55 +0200412 if (!cfq_cfqq_sync(cfqq))
413 list_add_tail(&cfqq->cfq_list, &cfqd->rr_list[cfqq->ioprio]);
414 else {
415 struct list_head *n = &cfqd->rr_list[cfqq->ioprio];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jens Axboe99f96282007-02-05 11:56:25 +0100417 /*
418 * sort by last service, but don't cross a new or async
Jens Axboe1afba042007-04-17 12:47:55 +0200419 * queue. we don't cross a new queue because it hasn't
420 * been service before, and we don't cross an async
421 * queue because it gets added to the end on expire.
Jens Axboe99f96282007-02-05 11:56:25 +0100422 */
Jens Axboe1afba042007-04-17 12:47:55 +0200423 while ((n = n->prev) != &cfqd->rr_list[cfqq->ioprio]) {
Jens Axboe6d048f52007-04-25 12:44:27 +0200424 struct cfq_queue *__c = list_entry_cfqq(n);
Jens Axboe53b03742006-07-28 09:48:51 +0200425
Jens Axboe6d048f52007-04-25 12:44:27 +0200426 if (!cfq_cfqq_sync(__c) || !__c->service_last)
Jens Axboe99f96282007-02-05 11:56:25 +0100427 break;
Jens Axboe6d048f52007-04-25 12:44:27 +0200428 if (time_before(__c->service_last, cfqq->service_last))
Jens Axboe99f96282007-02-05 11:56:25 +0100429 break;
430 }
431 list_add(&cfqq->cfq_list, n);
Jens Axboe22e2c502005-06-27 10:55:12 +0200432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Jens Axboe6d048f52007-04-25 12:44:27 +0200435static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
436{
437 struct cfq_data *cfqd = cfqq->cfqd;
438 struct list_head *n;
439
440 /*
441 * Resorting requires the cfqq to be on the RR list already.
442 */
443 if (!cfq_cfqq_on_rr(cfqq))
444 return;
445
446 list_del(&cfqq->cfq_list);
447
448 if (cfq_class_rt(cfqq)) {
449 /*
450 * At to the front of the current list, but behind other
451 * RT queues.
452 */
453 n = &cfqd->cur_rr;
454 while (n->next != &cfqd->cur_rr)
455 if (!cfq_class_rt(cfqq))
456 break;
457
458 list_add(&cfqq->cfq_list, n);
459 } else if (cfq_class_idle(cfqq)) {
460 /*
461 * IDLE goes to the tail of the idle list
462 */
463 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
464 } else {
465 /*
466 * So we get here, ergo the queue is a regular best-effort queue
467 */
468 cfq_resort_be_queue(cfqd, cfqq, preempted);
469 }
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
473 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200474 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200477cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jens Axboe3b181522005-06-27 10:56:24 +0200479 BUG_ON(cfq_cfqq_on_rr(cfqq));
480 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 cfqd->busy_queues++;
482
Jens Axboeb4878f22005-10-20 16:42:29 +0200483 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static inline void
487cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
488{
Jens Axboe3b181522005-06-27 10:56:24 +0200489 BUG_ON(!cfq_cfqq_on_rr(cfqq));
490 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200491 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 BUG_ON(!cfqd->busy_queues);
494 cfqd->busy_queues--;
495}
496
497/*
498 * rb tree support functions
499 */
Jens Axboe5e705372006-07-13 12:39:25 +0200500static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jens Axboe5e705372006-07-13 12:39:25 +0200502 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200503 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200504 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jens Axboeb4878f22005-10-20 16:42:29 +0200506 BUG_ON(!cfqq->queued[sync]);
507 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jens Axboe5e705372006-07-13 12:39:25 +0200509 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Jens Axboedd67d052006-06-21 09:36:18 +0200511 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200512 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Jens Axboe5e705372006-07-13 12:39:25 +0200515static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jens Axboe5e705372006-07-13 12:39:25 +0200517 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200519 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jens Axboe5380a102006-07-13 12:37:56 +0200521 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * looks a little odd, but the first insert might return an alias.
525 * if that happens, put the alias on the dispatch list
526 */
Jens Axboe21183b02006-07-13 12:33:14 +0200527 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200528 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100529
530 if (!cfq_cfqq_on_rr(cfqq))
531 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200532
533 /*
534 * check if this request is a better next-serve candidate
535 */
536 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
537 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200541cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jens Axboe5380a102006-07-13 12:37:56 +0200543 elv_rb_del(&cfqq->sort_list, rq);
544 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200545 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Jens Axboe206dc692006-03-28 13:03:44 +0200548static struct request *
549cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jens Axboe206dc692006-03-28 13:03:44 +0200551 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100552 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200553 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jens Axboe206dc692006-03-28 13:03:44 +0200555 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200556 if (cfqq) {
557 sector_t sector = bio->bi_sector + bio_sectors(bio);
558
Jens Axboe21183b02006-07-13 12:33:14 +0200559 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return NULL;
563}
564
Jens Axboeb4878f22005-10-20 16:42:29 +0200565static void cfq_activate_request(request_queue_t *q, struct request *rq)
566{
567 struct cfq_data *cfqd = q->elevator->elevator_data;
568
569 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200570
571 /*
572 * If the depth is larger 1, it really could be queueing. But lets
573 * make the mark a little higher - idling could still be good for
574 * low queueing, and a low queueing number could also just indicate
575 * a SCSI mid layer like behaviour where limit+1 is often seen.
576 */
577 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
578 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200579
580 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
584{
Jens Axboe22e2c502005-06-27 10:55:12 +0200585 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jens Axboeb4878f22005-10-20 16:42:29 +0200587 WARN_ON(!cfqd->rq_in_driver);
588 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Jens Axboeb4878f22005-10-20 16:42:29 +0200591static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jens Axboe5e705372006-07-13 12:39:25 +0200593 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200594
Jens Axboe5e705372006-07-13 12:39:25 +0200595 if (cfqq->next_rq == rq)
596 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jens Axboeb4878f22005-10-20 16:42:29 +0200598 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200599 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200600
601 if (rq_is_meta(rq)) {
602 WARN_ON(!cfqq->meta_pending);
603 cfqq->meta_pending--;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int
608cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
609{
610 struct cfq_data *cfqd = q->elevator->elevator_data;
611 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Jens Axboe206dc692006-03-28 13:03:44 +0200613 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200614 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200615 *req = __rq;
616 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Jens Axboe21183b02006-07-13 12:33:14 +0200622static void cfq_merged_request(request_queue_t *q, struct request *req,
623 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jens Axboe21183b02006-07-13 12:33:14 +0200625 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200626 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jens Axboe5e705372006-07-13 12:39:25 +0200628 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632static void
633cfq_merged_requests(request_queue_t *q, struct request *rq,
634 struct request *next)
635{
Jens Axboe22e2c502005-06-27 10:55:12 +0200636 /*
637 * reposition in fifo if next is older than rq
638 */
639 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
640 time_before(next->start_time, rq->start_time))
641 list_move(&rq->queuelist, &next->queuelist);
642
Jens Axboeb4878f22005-10-20 16:42:29 +0200643 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200644}
645
Jens Axboeda775262006-12-20 11:04:12 +0100646static int cfq_allow_merge(request_queue_t *q, struct request *rq,
647 struct bio *bio)
648{
649 struct cfq_data *cfqd = q->elevator->elevator_data;
650 const int rw = bio_data_dir(bio);
651 struct cfq_queue *cfqq;
652 pid_t key;
653
654 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100655 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100656 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100657 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100658 return 0;
659
660 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100661 * Lookup the cfqq that this bio will be queued with. Allow
662 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100663 */
Jens Axboe719d3402006-12-22 09:38:53 +0100664 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100665 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100666
667 if (cfqq == RQ_CFQQ(rq))
668 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100669
Jens Axboeec8acb62007-01-02 18:32:11 +0100670 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100671}
672
Jens Axboe22e2c502005-06-27 10:55:12 +0200673static inline void
674__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
675{
676 if (cfqq) {
677 /*
678 * stop potential idle class queues waiting service
679 */
680 del_timer(&cfqd->idle_class_timer);
681
Jens Axboe22e2c502005-06-27 10:55:12 +0200682 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200683 cfq_clear_cfqq_must_alloc_slice(cfqq);
684 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100685 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200686 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200687 cfqq->rr_tick = cfqd->cur_rr_tick;
Jens Axboe22e2c502005-06-27 10:55:12 +0200688 }
689
690 cfqd->active_queue = cfqq;
691}
692
693/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100694 * current cfqq expired its slice (or was too idle), select new one
695 */
696static void
697__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100698 int preempted, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100699{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100700 if (cfq_cfqq_wait_request(cfqq))
701 del_timer(&cfqd->idle_slice_timer);
702
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100703 cfq_clear_cfqq_must_dispatch(cfqq);
704 cfq_clear_cfqq_wait_request(cfqq);
705
706 /*
707 * store what was left of this slice, if the queue idled out
708 * or was preempted
709 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100710 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100711 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100712
Jens Axboe98e41c72007-02-05 11:55:35 +0100713 cfq_resort_rr_list(cfqq, preempted);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100714
715 if (cfqq == cfqd->active_queue)
716 cfqd->active_queue = NULL;
717
718 if (cfqd->active_cic) {
719 put_io_context(cfqd->active_cic->ioc);
720 cfqd->active_cic = NULL;
721 }
722
723 cfqd->dispatch_slice = 0;
724}
725
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100726static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
727 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100728{
729 struct cfq_queue *cfqq = cfqd->active_queue;
730
731 if (cfqq)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100732 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100733}
734
735/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200736 * 0
737 * 0,1
738 * 0,1,2
739 * 0,1,2,3
740 * 0,1,2,3,4
741 * 0,1,2,3,4,5
742 * 0,1,2,3,4,5,6
743 * 0,1,2,3,4,5,6,7
744 */
745static int cfq_get_next_prio_level(struct cfq_data *cfqd)
746{
747 int prio, wrap;
748
749 prio = -1;
750 wrap = 0;
751 do {
752 int p;
753
754 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
755 if (!list_empty(&cfqd->rr_list[p])) {
756 prio = p;
757 break;
758 }
759 }
760
761 if (prio != -1)
762 break;
763 cfqd->cur_prio = 0;
764 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
765 cfqd->cur_end_prio = 0;
766 if (wrap)
767 break;
768 wrap = 1;
769 }
770 } while (1);
771
772 if (unlikely(prio == -1))
773 return -1;
774
775 BUG_ON(prio >= CFQ_PRIO_LISTS);
776
777 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
778
779 cfqd->cur_prio = prio + 1;
780 if (cfqd->cur_prio > cfqd->cur_end_prio) {
781 cfqd->cur_end_prio = cfqd->cur_prio;
782 cfqd->cur_prio = 0;
783 }
784 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
785 cfqd->cur_prio = 0;
786 cfqd->cur_end_prio = 0;
787 }
788
Jens Axboe6d048f52007-04-25 12:44:27 +0200789 cfqd->cur_rr_tick++;
790 cfqd->prio_time = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +0200791 return prio;
792}
793
Jens Axboe6d048f52007-04-25 12:44:27 +0200794static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
795 struct request *rq)
796{
797 if (rq->sector >= cfqd->last_position)
798 return rq->sector - cfqd->last_position;
799 else
800 return cfqd->last_position - rq->sector;
801}
802
803static struct cfq_queue *cfq_get_best_queue(struct cfq_data *cfqd)
804{
805 struct cfq_queue *cfqq = NULL, *__cfqq;
Jens Axboe1afba042007-04-17 12:47:55 +0200806 sector_t best = -1, first = -1, dist;
Jens Axboe6d048f52007-04-25 12:44:27 +0200807
808 list_for_each_entry(__cfqq, &cfqd->cur_rr, cfq_list) {
809 if (!__cfqq->next_rq || !cfq_cfqq_sync(__cfqq))
810 continue;
811
812 dist = cfq_dist_from_last(cfqd, __cfqq->next_rq);
Jens Axboe1afba042007-04-17 12:47:55 +0200813 if (first == -1)
814 first = dist;
Jens Axboe6d048f52007-04-25 12:44:27 +0200815 if (dist < best) {
816 best = dist;
817 cfqq = __cfqq;
818 }
819 }
820
821 /*
Jens Axboe1afba042007-04-17 12:47:55 +0200822 * Only async queue(s) available, grab first entry. Do the same
823 * if the difference between the first and best isn't more than
824 * twice, to obey fairness.
Jens Axboe6d048f52007-04-25 12:44:27 +0200825 */
Jens Axboe1afba042007-04-17 12:47:55 +0200826 if (!cfqq || (best && first != best && ((first / best) < 4)))
Jens Axboe6d048f52007-04-25 12:44:27 +0200827 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
828
829 return cfqq;
830}
831
832static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200833{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100834 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200835
Jens Axboe89850f72006-07-22 16:48:31 +0200836 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
837 /*
838 * if current list is non-empty, grab first entry. if it is
839 * empty, get next prio level and grab first entry then if any
840 * are spliced
841 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200842 cfqq = cfq_get_best_queue(cfqd);
Jens Axboe89850f72006-07-22 16:48:31 +0200843 } else if (!list_empty(&cfqd->idle_rr)) {
844 /*
845 * if we have idle queues and no rt or be queues had pending
846 * requests, either allow immediate service if the grace period
847 * has passed or arm the idle grace timer
848 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200849 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
850
851 if (time_after_eq(jiffies, end))
852 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
853 else
854 mod_timer(&cfqd->idle_class_timer, end);
855 }
856
Jens Axboe6d048f52007-04-25 12:44:27 +0200857 return cfqq;
858}
859
860static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
861{
862 struct cfq_queue *cfqq;
863
864 do {
865 long prio;
866
867 cfqq = cfq_get_next_queue(cfqd);
868 if (!cfqq)
869 break;
870
871 prio = cfq_prio_to_slice(cfqd, cfqq);
872 if (cfqq->slice_resid > -prio)
873 break;
874
875 cfqq->slice_resid += prio;
876 list_del_init(&cfqq->cfq_list);
877 list_add_tail(&cfqq->cfq_list, &cfqd->rr_list[cfqq->ioprio]);
878 cfqq = NULL;
879 } while (1);
880
Jens Axboe22e2c502005-06-27 10:55:12 +0200881 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200882 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200883}
884
Jens Axboe6d048f52007-04-25 12:44:27 +0200885static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
886{
887 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200888
Jens Axboe6d048f52007-04-25 12:44:27 +0200889 if (!sample_valid(cic->seek_samples))
890 return 0;
891
892 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
893}
894
895static struct cfq_queue *__cfq_close_cooperator(struct cfq_data *cfqd,
896 struct cfq_queue *cur_cfqq,
897 struct list_head *list)
898{
899 struct cfq_queue *cfqq;
900
901 list_for_each_entry(cfqq, list, cfq_list) {
902 if (cfqq == cur_cfqq || !cfq_cfqq_sync(cfqq))
903 continue;
904
905 BUG_ON(!cfqq->next_rq);
906
907 if (cfq_rq_close(cfqd, cfqq->next_rq))
908 return cfqq;
909 }
910
911 return NULL;
912}
913
914static int cfq_close_cooperator(struct cfq_data *cfqd,
915 struct cfq_queue *cur_cfqq)
916{
917 struct cfq_queue *cfqq;
918
919 if (!cfqd->busy_queues)
920 return 0;
921
922 /*
923 * check cur_rr and same-prio rr_list for candidates
924 */
925 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->cur_rr);
926 if (cfqq)
927 return 1;
928
929 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->rr_list[cur_cfqq->ioprio]);
930 if (cfqq && (cfqq->rr_tick == cfqd->cur_rr_tick))
931 cfqq = NULL;
932
933 return cfqq != NULL;
934}
935
936#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
937
938static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200939{
Jens Axboe17926692007-01-19 11:59:30 +1100940 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200941 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100942 unsigned long sl;
943
Jens Axboedd67d052006-06-21 09:36:18 +0200944 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200945 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200946
947 /*
948 * idle is disabled, either manually or by past process history
949 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200950 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
951 return;
952
Jens Axboe22e2c502005-06-27 10:55:12 +0200953 /*
954 * task has exited, don't wait
955 */
Jens Axboe206dc692006-03-28 13:03:44 +0200956 cic = cfqd->active_cic;
957 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200958 return;
959
960 /*
961 * See if this prio level has a good candidate
962 */
Jens Axboe1afba042007-04-17 12:47:55 +0200963 if (cfq_close_cooperator(cfqd, cfqq) &&
964 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200965 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200966
Jens Axboe3b181522005-06-27 10:56:24 +0200967 cfq_mark_cfqq_must_dispatch(cfqq);
968 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200969
Jens Axboe206dc692006-03-28 13:03:44 +0200970 /*
971 * we don't want to idle for seeks, but we do want to allow
972 * fair distribution of slice time for a process doing back-to-back
973 * seeks. so allow a little bit of time for him to submit a new rq
974 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200975 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200976 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200977 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200978
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100979 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Jens Axboe5e705372006-07-13 12:39:25 +0200982static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jens Axboe5e705372006-07-13 12:39:25 +0200984 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200985
Jens Axboe5380a102006-07-13 12:37:56 +0200986 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200987 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200988 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991/*
992 * return expired entry, or NULL to just start from scratch in rbtree
993 */
Jens Axboe5e705372006-07-13 12:39:25 +0200994static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200997 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200998 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jens Axboe3b181522005-06-27 10:56:24 +02001000 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001002
1003 cfq_mark_cfqq_fifo_expire(cfqq);
1004
Jens Axboe89850f72006-07-22 16:48:31 +02001005 if (list_empty(&cfqq->fifo))
1006 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Jens Axboe6d048f52007-04-25 12:44:27 +02001008 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +02001009 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jens Axboe6d048f52007-04-25 12:44:27 +02001011 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
1012 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jens Axboe6d048f52007-04-25 12:44:27 +02001014 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Jens Axboe22e2c502005-06-27 10:55:12 +02001017static inline int
1018cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1019{
1020 const int base_rq = cfqd->cfq_slice_async_rq;
1021
1022 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1023
1024 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1025}
1026
1027/*
1028 * get next queue for service
1029 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001030static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001031{
Jens Axboe22e2c502005-06-27 10:55:12 +02001032 struct cfq_queue *cfqq;
1033
1034 cfqq = cfqd->active_queue;
1035 if (!cfqq)
1036 goto new_queue;
1037
1038 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001039 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02001040 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001041 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001042 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001043
1044 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001045 * The active queue has requests and isn't expired, allow it to
1046 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02001047 */
Jens Axboedd67d052006-06-21 09:36:18 +02001048 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001049 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02001050
1051 /*
1052 * No requests pending. If the active queue still has requests in
1053 * flight or is idling for a new request, allow either of these
1054 * conditions to happen (or time out) before selecting a new queue.
1055 */
1056 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001057 cfqq = NULL;
1058 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001059 }
1060
Jens Axboe3b181522005-06-27 10:56:24 +02001061expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001062 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001063new_queue:
1064 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001065keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001066 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001067}
1068
1069static int
1070__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1071 int max_dispatch)
1072{
1073 int dispatched = 0;
1074
Jens Axboedd67d052006-06-21 09:36:18 +02001075 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001076
1077 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001078 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001079
1080 /*
1081 * follow expired path, else get first next available
1082 */
Jens Axboe5e705372006-07-13 12:39:25 +02001083 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1084 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001085
1086 /*
1087 * finally, insert request into driver dispatch list
1088 */
Jens Axboe5e705372006-07-13 12:39:25 +02001089 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001090
1091 cfqd->dispatch_slice++;
1092 dispatched++;
1093
1094 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001095 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1096 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001097 }
1098
Jens Axboedd67d052006-06-21 09:36:18 +02001099 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001100 break;
1101
1102 } while (dispatched < max_dispatch);
1103
1104 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001105 * expire an async queue immediately if it has used up its slice. idle
1106 * queue always expire after 1 dispatch round.
1107 */
Jens Axboea9938002007-04-20 08:55:52 +02001108 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001109 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001110 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001111 cfqq->slice_end = jiffies + 1;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001112 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001113 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001114
1115 return dispatched;
1116}
1117
1118static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001119cfq_forced_dispatch_cfqqs(struct list_head *list)
1120{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001121 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001122 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001123
Jens Axboecaaa5f92006-06-16 11:23:00 +02001124 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001125 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +02001126 while (cfqq->next_rq) {
1127 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001128 dispatched++;
1129 }
1130 BUG_ON(!list_empty(&cfqq->fifo));
1131 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001132
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001133 return dispatched;
1134}
1135
1136static int
1137cfq_forced_dispatch(struct cfq_data *cfqd)
1138{
1139 int i, dispatched = 0;
1140
1141 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1142 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1143
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001144 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1145 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1146
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001147 cfq_slice_expired(cfqd, 0, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001148
1149 BUG_ON(cfqd->busy_queues);
1150
1151 return dispatched;
1152}
1153
1154static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001155cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001158 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001159 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jens Axboe22e2c502005-06-27 10:55:12 +02001161 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
1163
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001164 if (unlikely(force))
1165 return cfq_forced_dispatch(cfqd);
1166
Jens Axboecaaa5f92006-06-16 11:23:00 +02001167 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001168 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001169 int max_dispatch;
1170
Jens Axboea9938002007-04-20 08:55:52 +02001171 if (cfqd->busy_queues > 1) {
1172 /*
Jens Axboea9938002007-04-20 08:55:52 +02001173 * So we have dispatched before in this round, if the
1174 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001175 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001176 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001177 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1178 dispatched)
1179 break;
1180 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001181 break;
1182 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001183
Jens Axboe3b181522005-06-27 10:56:24 +02001184 cfq_clear_cfqq_must_dispatch(cfqq);
1185 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001186 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001188 max_dispatch = cfqd->cfq_quantum;
1189 if (cfq_class_idle(cfqq))
1190 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jens Axboecaaa5f92006-06-16 11:23:00 +02001192 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
Jens Axboecaaa5f92006-06-16 11:23:00 +02001195 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/*
Jens Axboe5e705372006-07-13 12:39:25 +02001199 * task holds one reference to the queue, dropped when task exits. each rq
1200 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
1202 * queue lock must be held here.
1203 */
1204static void cfq_put_queue(struct cfq_queue *cfqq)
1205{
Jens Axboe22e2c502005-06-27 10:55:12 +02001206 struct cfq_data *cfqd = cfqq->cfqd;
1207
1208 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (!atomic_dec_and_test(&cfqq->ref))
1211 return;
1212
1213 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001214 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001215 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001217 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001218 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001219 cfq_schedule_dispatch(cfqd);
1220 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * it's on the empty list and still hashed
1224 */
1225 list_del(&cfqq->cfq_list);
1226 hlist_del(&cfqq->cfq_hash);
1227 kmem_cache_free(cfq_pool, cfqq);
1228}
1229
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001230static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001231__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1232 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001235 struct hlist_node *entry;
1236 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jens Axboe206dc692006-03-28 13:03:44 +02001238 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001239 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jens Axboe206dc692006-03-28 13:03:44 +02001241 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return __cfqq;
1243 }
1244
1245 return NULL;
1246}
1247
1248static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001249cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jens Axboe3b181522005-06-27 10:56:24 +02001251 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Jens Axboee2d74ac2006-03-28 08:59:01 +02001254static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Jens Axboe22e2c502005-06-27 10:55:12 +02001256 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001257 struct rb_node *n;
1258 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001259
Jens Axboee2d74ac2006-03-28 08:59:01 +02001260 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1261 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1262 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001263 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001264 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001265 }
1266
Jens Axboe4050cf12006-07-19 05:07:12 +02001267 elv_ioc_count_mod(ioc_count, -freed);
1268
1269 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001270 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Jens Axboe89850f72006-07-22 16:48:31 +02001273static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1274{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001275 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001276 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001277 cfq_schedule_dispatch(cfqd);
1278 }
Jens Axboe89850f72006-07-22 16:48:31 +02001279
1280 cfq_put_queue(cfqq);
1281}
1282
1283static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1284 struct cfq_io_context *cic)
1285{
Jens Axboefc463792006-08-29 09:05:44 +02001286 list_del_init(&cic->queue_list);
1287 smp_wmb();
1288 cic->key = NULL;
1289
Jens Axboe89850f72006-07-22 16:48:31 +02001290 if (cic->cfqq[ASYNC]) {
1291 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1292 cic->cfqq[ASYNC] = NULL;
1293 }
1294
1295 if (cic->cfqq[SYNC]) {
1296 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1297 cic->cfqq[SYNC] = NULL;
1298 }
Jens Axboe89850f72006-07-22 16:48:31 +02001299}
1300
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001303 * Called with interrupts disabled
1304 */
1305static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1306{
Al Viro478a82b2006-03-18 13:25:24 -05001307 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001308
Jens Axboe89850f72006-07-22 16:48:31 +02001309 if (cfqd) {
1310 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001311
Jens Axboefc463792006-08-29 09:05:44 +02001312 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001313 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001314 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001315 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001316}
1317
Jens Axboee2d74ac2006-03-28 08:59:01 +02001318static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Jens Axboe22e2c502005-06-27 10:55:12 +02001320 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001321 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * put the reference this task is holding to the various queues
1325 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001326
1327 n = rb_first(&ioc->cic_root);
1328 while (n != NULL) {
1329 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1330
Jens Axboe22e2c502005-06-27 10:55:12 +02001331 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001332 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Jens Axboe22e2c502005-06-27 10:55:12 +02001336static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001337cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Jens Axboeb5deef92006-07-19 23:39:40 +02001339 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Jens Axboeb5deef92006-07-19 23:39:40 +02001341 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001343 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001344 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001345 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001346 cic->dtor = cfq_free_io_context;
1347 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001348 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 return cic;
1352}
1353
Jens Axboe22e2c502005-06-27 10:55:12 +02001354static void cfq_init_prio_data(struct cfq_queue *cfqq)
1355{
1356 struct task_struct *tsk = current;
1357 int ioprio_class;
1358
Jens Axboe3b181522005-06-27 10:56:24 +02001359 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001360 return;
1361
1362 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1363 switch (ioprio_class) {
1364 default:
1365 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1366 case IOPRIO_CLASS_NONE:
1367 /*
1368 * no prio set, place us in the middle of the BE classes
1369 */
1370 cfqq->ioprio = task_nice_ioprio(tsk);
1371 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1372 break;
1373 case IOPRIO_CLASS_RT:
1374 cfqq->ioprio = task_ioprio(tsk);
1375 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1376 break;
1377 case IOPRIO_CLASS_BE:
1378 cfqq->ioprio = task_ioprio(tsk);
1379 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1380 break;
1381 case IOPRIO_CLASS_IDLE:
1382 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1383 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001384 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001385 break;
1386 }
1387
1388 /*
1389 * keep track of original prio settings in case we have to temporarily
1390 * elevate the priority of this queue
1391 */
1392 cfqq->org_ioprio = cfqq->ioprio;
1393 cfqq->org_ioprio_class = cfqq->ioprio_class;
1394
Jens Axboe98e41c72007-02-05 11:55:35 +01001395 cfq_resort_rr_list(cfqq, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001396 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001397}
1398
Al Viro478a82b2006-03-18 13:25:24 -05001399static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001400{
Al Viro478a82b2006-03-18 13:25:24 -05001401 struct cfq_data *cfqd = cic->key;
1402 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001403 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001404
Jens Axboecaaa5f92006-06-16 11:23:00 +02001405 if (unlikely(!cfqd))
1406 return;
1407
Jens Axboec1b707d2006-10-30 19:54:23 +01001408 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001409
1410 cfqq = cic->cfqq[ASYNC];
1411 if (cfqq) {
1412 struct cfq_queue *new_cfqq;
1413 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1414 GFP_ATOMIC);
1415 if (new_cfqq) {
1416 cic->cfqq[ASYNC] = new_cfqq;
1417 cfq_put_queue(cfqq);
1418 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001419 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001420
1421 cfqq = cic->cfqq[SYNC];
1422 if (cfqq)
1423 cfq_mark_cfqq_prio_changed(cfqq);
1424
Jens Axboec1b707d2006-10-30 19:54:23 +01001425 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001426}
1427
Jens Axboefc463792006-08-29 09:05:44 +02001428static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Al Viroa6a07632006-03-18 13:26:44 -05001430 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001431 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001432
Jens Axboefc463792006-08-29 09:05:44 +02001433 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001434
Jens Axboee2d74ac2006-03-28 08:59:01 +02001435 n = rb_first(&ioc->cic_root);
1436 while (n != NULL) {
1437 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001438
Al Viro478a82b2006-03-18 13:25:24 -05001439 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001440 n = rb_next(n);
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001445cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001446 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1449 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001450 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452retry:
Al Viro6f325a12006-03-18 14:58:37 -05001453 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001454 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (!cfqq) {
1457 if (new_cfqq) {
1458 cfqq = new_cfqq;
1459 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001460 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001461 /*
1462 * Inform the allocator of the fact that we will
1463 * just repeat this allocation if it fails, to allow
1464 * the allocator to do whatever it needs to attempt to
1465 * free memory.
1466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001468 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_lock_irq(cfqd->queue->queue_lock);
1470 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001471 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001472 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001473 if (!cfqq)
1474 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 memset(cfqq, 0, sizeof(*cfqq));
1478
1479 INIT_HLIST_NODE(&cfqq->cfq_hash);
1480 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001481 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 cfqq->key = key;
1484 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1485 atomic_set(&cfqq->ref, 0);
1486 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001487
Jens Axboea9938002007-04-20 08:55:52 +02001488 if (key != CFQ_KEY_ASYNC)
1489 cfq_mark_cfqq_idle_window(cfqq);
1490
Jens Axboe3b181522005-06-27 10:56:24 +02001491 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001492 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001493 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 if (new_cfqq)
1497 kmem_cache_free(cfq_pool, new_cfqq);
1498
1499 atomic_inc(&cfqq->ref);
1500out:
1501 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1502 return cfqq;
1503}
1504
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001505static void
1506cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1507{
Jens Axboefc463792006-08-29 09:05:44 +02001508 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001509 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001510 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001511 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001512}
1513
Jens Axboee2d74ac2006-03-28 08:59:01 +02001514static struct cfq_io_context *
1515cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1516{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001517 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001518 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001519 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001520
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001521restart:
1522 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001523 while (n) {
1524 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001525 /* ->key must be copied to avoid race with cfq_exit_queue() */
1526 k = cic->key;
1527 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001528 cfq_drop_dead_cic(ioc, cic);
1529 goto restart;
1530 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001531
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001532 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001533 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001534 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001535 n = n->rb_right;
1536 else
1537 return cic;
1538 }
1539
1540 return NULL;
1541}
1542
1543static inline void
1544cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1545 struct cfq_io_context *cic)
1546{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001547 struct rb_node **p;
1548 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001549 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001550 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001551 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001552
Jens Axboee2d74ac2006-03-28 08:59:01 +02001553 cic->ioc = ioc;
1554 cic->key = cfqd;
1555
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001556restart:
1557 parent = NULL;
1558 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001559 while (*p) {
1560 parent = *p;
1561 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001562 /* ->key must be copied to avoid race with cfq_exit_queue() */
1563 k = __cic->key;
1564 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001565 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001566 goto restart;
1567 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001568
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001569 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001570 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001571 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001572 p = &(*p)->rb_right;
1573 else
1574 BUG();
1575 }
1576
1577 rb_link_node(&cic->rb_node, parent, p);
1578 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001579
Jens Axboe0261d682006-10-30 19:07:48 +01001580 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001581 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001582 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001583}
1584
Jens Axboe22e2c502005-06-27 10:55:12 +02001585/*
1586 * Setup general io context and cfq io context. There can be several cfq
1587 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001588 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001589 */
1590static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001591cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Jens Axboe22e2c502005-06-27 10:55:12 +02001593 struct io_context *ioc = NULL;
1594 struct cfq_io_context *cic;
1595
1596 might_sleep_if(gfp_mask & __GFP_WAIT);
1597
Jens Axboeb5deef92006-07-19 23:39:40 +02001598 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001599 if (!ioc)
1600 return NULL;
1601
Jens Axboee2d74ac2006-03-28 08:59:01 +02001602 cic = cfq_cic_rb_lookup(cfqd, ioc);
1603 if (cic)
1604 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001605
Jens Axboee2d74ac2006-03-28 08:59:01 +02001606 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1607 if (cic == NULL)
1608 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001609
Jens Axboee2d74ac2006-03-28 08:59:01 +02001610 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001611out:
Jens Axboefc463792006-08-29 09:05:44 +02001612 smp_read_barrier_depends();
1613 if (unlikely(ioc->ioprio_changed))
1614 cfq_ioc_set_ioprio(ioc);
1615
Jens Axboe22e2c502005-06-27 10:55:12 +02001616 return cic;
1617err:
1618 put_io_context(ioc);
1619 return NULL;
1620}
1621
1622static void
1623cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1624{
Jens Axboeaaf12282007-01-19 11:30:16 +11001625 unsigned long elapsed = jiffies - cic->last_end_request;
1626 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001627
1628 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1629 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1630 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1631}
1632
Jens Axboe206dc692006-03-28 13:03:44 +02001633static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001634cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1635 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001636{
1637 sector_t sdist;
1638 u64 total;
1639
Jens Axboe5e705372006-07-13 12:39:25 +02001640 if (cic->last_request_pos < rq->sector)
1641 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001642 else
Jens Axboe5e705372006-07-13 12:39:25 +02001643 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001644
Jens Axboe6d048f52007-04-25 12:44:27 +02001645 if (!cic->seek_samples) {
1646 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1647 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1648 }
1649
Jens Axboe206dc692006-03-28 13:03:44 +02001650 /*
1651 * Don't allow the seek distance to get too large from the
1652 * odd fragment, pagein, etc
1653 */
1654 if (cic->seek_samples <= 60) /* second&third seek */
1655 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1656 else
1657 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1658
1659 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1660 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1661 total = cic->seek_total + (cic->seek_samples/2);
1662 do_div(total, cic->seek_samples);
1663 cic->seek_mean = (sector_t)total;
1664}
Jens Axboe22e2c502005-06-27 10:55:12 +02001665
1666/*
1667 * Disable idle window if the process thinks too long or seeks so much that
1668 * it doesn't matter
1669 */
1670static void
1671cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1672 struct cfq_io_context *cic)
1673{
Jens Axboe3b181522005-06-27 10:56:24 +02001674 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001675
Jens Axboecaaa5f92006-06-16 11:23:00 +02001676 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1677 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001678 enable_idle = 0;
1679 else if (sample_valid(cic->ttime_samples)) {
1680 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1681 enable_idle = 0;
1682 else
1683 enable_idle = 1;
1684 }
1685
Jens Axboe3b181522005-06-27 10:56:24 +02001686 if (enable_idle)
1687 cfq_mark_cfqq_idle_window(cfqq);
1688 else
1689 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001690}
1691
Jens Axboe22e2c502005-06-27 10:55:12 +02001692/*
1693 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1694 * no or if we aren't sure, a 1 will cause a preempt.
1695 */
1696static int
1697cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001698 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001699{
Jens Axboe6d048f52007-04-25 12:44:27 +02001700 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001701
Jens Axboe6d048f52007-04-25 12:44:27 +02001702 cfqq = cfqd->active_queue;
1703 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001704 return 0;
1705
Jens Axboe6d048f52007-04-25 12:44:27 +02001706 if (cfq_slice_used(cfqq))
1707 return 1;
1708
1709 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001710 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001711
1712 if (cfq_class_idle(cfqq))
1713 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001714
Jens Axboe22e2c502005-06-27 10:55:12 +02001715 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001716 * if the new request is sync, but the currently running queue is
1717 * not, let the sync request have priority.
1718 */
Jens Axboe5e705372006-07-13 12:39:25 +02001719 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001720 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001721
Jens Axboe374f84a2006-07-23 01:42:19 +02001722 /*
1723 * So both queues are sync. Let the new request get disk time if
1724 * it's a metadata request and the current queue is doing regular IO.
1725 */
1726 if (rq_is_meta(rq) && !cfqq->meta_pending)
1727 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001728
Jens Axboe1e3335d2007-02-14 19:59:49 +01001729 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1730 return 0;
1731
1732 /*
1733 * if this request is as-good as one we would expect from the
1734 * current cfqq, let it preempt
1735 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001736 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001737 return 1;
1738
Jens Axboe22e2c502005-06-27 10:55:12 +02001739 return 0;
1740}
1741
1742/*
1743 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1744 * let it have half of its nominal slice.
1745 */
1746static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1747{
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001748 cfq_slice_expired(cfqd, 1, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001749
Jens Axboebf572252006-07-19 20:29:12 +02001750 /*
1751 * Put the new queue at the front of the of the current list,
1752 * so we know that it will be selected next.
1753 */
1754 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1755 list_move(&cfqq->cfq_list, &cfqd->cur_rr);
1756
Jens Axboe44f7c162007-01-19 11:51:58 +11001757 cfqq->slice_end = 0;
1758 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001759}
1760
1761/*
Jens Axboe5e705372006-07-13 12:39:25 +02001762 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001763 * something we should do about it
1764 */
1765static void
Jens Axboe5e705372006-07-13 12:39:25 +02001766cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1767 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001768{
Jens Axboe5e705372006-07-13 12:39:25 +02001769 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001770
Jens Axboe374f84a2006-07-23 01:42:19 +02001771 if (rq_is_meta(rq))
1772 cfqq->meta_pending++;
1773
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001774 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001775 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001776 cfq_update_idle_window(cfqd, cfqq, cic);
1777
Jens Axboe5e705372006-07-13 12:39:25 +02001778 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001779 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001780
1781 if (cfqq == cfqd->active_queue) {
1782 /*
1783 * if we are waiting for a request for this queue, let it rip
1784 * immediately and flag that we must not expire this queue
1785 * just now
1786 */
Jens Axboe3b181522005-06-27 10:56:24 +02001787 if (cfq_cfqq_wait_request(cfqq)) {
1788 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001789 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001790 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001791 }
Jens Axboe5e705372006-07-13 12:39:25 +02001792 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001793 /*
1794 * not the active queue - expire current slice if it is
1795 * idle and has expired it's mean thinktime or this new queue
1796 * has some old slice time left and is of higher priority
1797 */
1798 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001799 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001800 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001801 }
1802}
1803
Jens Axboeb4878f22005-10-20 16:42:29 +02001804static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001805{
Jens Axboeb4878f22005-10-20 16:42:29 +02001806 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001807 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001808
1809 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jens Axboe5e705372006-07-13 12:39:25 +02001811 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jens Axboe22e2c502005-06-27 10:55:12 +02001813 list_add_tail(&rq->queuelist, &cfqq->fifo);
1814
Jens Axboe5e705372006-07-13 12:39:25 +02001815 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818static void cfq_completed_request(request_queue_t *q, struct request *rq)
1819{
Jens Axboe5e705372006-07-13 12:39:25 +02001820 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001821 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001822 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001823 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jens Axboeb4878f22005-10-20 16:42:29 +02001825 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jens Axboeb4878f22005-10-20 16:42:29 +02001827 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001828 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001829 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001830 cfqq->dispatched--;
Jens Axboe99f96282007-02-05 11:56:25 +01001831 cfqq->service_last = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jens Axboeb4878f22005-10-20 16:42:29 +02001833 if (!cfq_class_idle(cfqq))
1834 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001835
Jens Axboe98e41c72007-02-05 11:55:35 +01001836 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Jens Axboecaaa5f92006-06-16 11:23:00 +02001838 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001839 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001840
1841 /*
1842 * If this is the active queue, check if it needs to be expired,
1843 * or if we want to idle in case it has no pending requests.
1844 */
1845 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001846 if (cfq_cfqq_slice_new(cfqq)) {
1847 cfq_set_prio_slice(cfqd, cfqq);
1848 cfq_clear_cfqq_slice_new(cfqq);
1849 }
1850 if (cfq_slice_used(cfqq))
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001851 cfq_slice_expired(cfqd, 0, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001852 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1853 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001854 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001855
1856 if (!cfqd->rq_in_driver)
1857 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Jens Axboe22e2c502005-06-27 10:55:12 +02001860/*
1861 * we temporarily boost lower priority queues if they are holding fs exclusive
1862 * resources. they are boosted to normal prio (CLASS_BE/4)
1863 */
1864static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Jens Axboe22e2c502005-06-27 10:55:12 +02001866 const int ioprio_class = cfqq->ioprio_class;
1867 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jens Axboe22e2c502005-06-27 10:55:12 +02001869 if (has_fs_excl()) {
1870 /*
1871 * boost idle prio on transactions that would lock out other
1872 * users of the filesystem
1873 */
1874 if (cfq_class_idle(cfqq))
1875 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1876 if (cfqq->ioprio > IOPRIO_NORM)
1877 cfqq->ioprio = IOPRIO_NORM;
1878 } else {
1879 /*
1880 * check if we need to unboost the queue
1881 */
1882 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1883 cfqq->ioprio_class = cfqq->org_ioprio_class;
1884 if (cfqq->ioprio != cfqq->org_ioprio)
1885 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Jens Axboe22e2c502005-06-27 10:55:12 +02001888 /*
1889 * refile between round-robin lists if we moved the priority class
1890 */
Jens Axboe98e41c72007-02-05 11:55:35 +01001891 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio))
Jens Axboe22e2c502005-06-27 10:55:12 +02001892 cfq_resort_rr_list(cfqq, 0);
1893}
1894
Jens Axboe89850f72006-07-22 16:48:31 +02001895static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001896{
Jens Axboe3b181522005-06-27 10:56:24 +02001897 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001898 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001899 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001900 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001901 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001902
1903 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001904}
1905
Jens Axboecb78b282006-07-28 09:32:57 +02001906static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001907{
1908 struct cfq_data *cfqd = q->elevator->elevator_data;
1909 struct task_struct *tsk = current;
1910 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001911 unsigned int key;
1912
1913 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001914
1915 /*
1916 * don't force setup of a queue from here, as a call to may_queue
1917 * does not necessarily imply that a request actually will be queued.
1918 * so just lookup a possibly existing queue, or return 'may queue'
1919 * if that fails
1920 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001921 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001922 if (cfqq) {
1923 cfq_init_prio_data(cfqq);
1924 cfq_prio_boost(cfqq);
1925
Jens Axboe89850f72006-07-22 16:48:31 +02001926 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001927 }
1928
1929 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * queue lock held here
1934 */
Jens Axboebb37b942006-12-01 10:42:33 +01001935static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Jens Axboe5e705372006-07-13 12:39:25 +02001937 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jens Axboe5e705372006-07-13 12:39:25 +02001939 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001940 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jens Axboe22e2c502005-06-27 10:55:12 +02001942 BUG_ON(!cfqq->allocated[rw]);
1943 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Jens Axboe5e705372006-07-13 12:39:25 +02001945 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001948 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 cfq_put_queue(cfqq);
1951 }
1952}
1953
1954/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001955 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001957static int
Jens Axboecb78b282006-07-28 09:32:57 +02001958cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001961 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct cfq_io_context *cic;
1963 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001964 const int is_sync = rq_is_sync(rq);
1965 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001966 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 unsigned long flags;
1968
1969 might_sleep_if(gfp_mask & __GFP_WAIT);
1970
Jens Axboee2d74ac2006-03-28 08:59:01 +02001971 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 spin_lock_irqsave(q->queue_lock, flags);
1974
Jens Axboe22e2c502005-06-27 10:55:12 +02001975 if (!cic)
1976 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Al Viro12a05732006-03-18 13:38:01 -05001978 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001979 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001980 if (!cfqq)
1981 goto queue_fail;
1982
Al Viro12a05732006-03-18 13:38:01 -05001983 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001984 } else
Al Viro12a05732006-03-18 13:38:01 -05001985 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001988 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001989 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 spin_unlock_irqrestore(q->queue_lock, flags);
1992
Jens Axboe5e705372006-07-13 12:39:25 +02001993 rq->elevator_private = cic;
1994 rq->elevator_private2 = cfqq;
1995 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001996
Jens Axboe22e2c502005-06-27 10:55:12 +02001997queue_fail:
1998 if (cic)
1999 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02002000
Jens Axboe3b181522005-06-27 10:56:24 +02002001 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 spin_unlock_irqrestore(q->queue_lock, flags);
2003 return 1;
2004}
2005
David Howells65f27f32006-11-22 14:55:48 +00002006static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02002007{
David Howells65f27f32006-11-22 14:55:48 +00002008 struct cfq_data *cfqd =
2009 container_of(work, struct cfq_data, unplug_work);
2010 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002011 unsigned long flags;
2012
2013 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02002014 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02002015 spin_unlock_irqrestore(q->queue_lock, flags);
2016}
2017
2018/*
2019 * Timer running if the active_queue is currently idling inside its time slice
2020 */
2021static void cfq_idle_slice_timer(unsigned long data)
2022{
2023 struct cfq_data *cfqd = (struct cfq_data *) data;
2024 struct cfq_queue *cfqq;
2025 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002026 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02002027
2028 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2029
2030 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002031 timed_out = 0;
2032
Jens Axboe22e2c502005-06-27 10:55:12 +02002033 /*
2034 * expired
2035 */
Jens Axboe44f7c162007-01-19 11:51:58 +11002036 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002037 goto expire;
2038
2039 /*
2040 * only expire and reinvoke request handler, if there are
2041 * other queues with pending requests
2042 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002043 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002044 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002045
2046 /*
2047 * not expired and it has a request pending, let it dispatch
2048 */
Jens Axboedd67d052006-06-21 09:36:18 +02002049 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002050 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002051 goto out_kick;
2052 }
2053 }
2054expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002055 cfq_slice_expired(cfqd, 0, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002056out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002057 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002058out_cont:
2059 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2060}
2061
2062/*
2063 * Timer running if an idle class queue is waiting for service
2064 */
2065static void cfq_idle_class_timer(unsigned long data)
2066{
2067 struct cfq_data *cfqd = (struct cfq_data *) data;
2068 unsigned long flags, end;
2069
2070 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2071
2072 /*
2073 * race with a non-idle queue, reset timer
2074 */
2075 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002076 if (!time_after_eq(jiffies, end))
2077 mod_timer(&cfqd->idle_class_timer, end);
2078 else
Jens Axboe3b181522005-06-27 10:56:24 +02002079 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002080
2081 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2082}
2083
Jens Axboe3b181522005-06-27 10:56:24 +02002084static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2085{
2086 del_timer_sync(&cfqd->idle_slice_timer);
2087 del_timer_sync(&cfqd->idle_class_timer);
2088 blk_sync_queue(cfqd->queue);
2089}
Jens Axboe22e2c502005-06-27 10:55:12 +02002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091static void cfq_exit_queue(elevator_t *e)
2092{
Jens Axboe22e2c502005-06-27 10:55:12 +02002093 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002094 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002095
Jens Axboe3b181522005-06-27 10:56:24 +02002096 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002097
Al Virod9ff4182006-03-18 13:51:22 -05002098 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002099
Al Virod9ff4182006-03-18 13:51:22 -05002100 if (cfqd->active_queue)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002101 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002102
2103 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002104 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2105 struct cfq_io_context,
2106 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002107
2108 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002109 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002110
Al Virod9ff4182006-03-18 13:51:22 -05002111 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002112
2113 cfq_shutdown_timer_wq(cfqd);
2114
Al Viroa90d7422006-03-18 12:05:37 -05002115 kfree(cfqd->cfq_hash);
2116 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Jens Axboebb37b942006-12-01 10:42:33 +01002119static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct cfq_data *cfqd;
2122 int i;
2123
Jens Axboeb5deef92006-07-19 23:39:40 +02002124 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002126 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002129
2130 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2131 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2132
Jens Axboe22e2c502005-06-27 10:55:12 +02002133 INIT_LIST_HEAD(&cfqd->cur_rr);
2134 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05002135 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Jens Axboeb5deef92006-07-19 23:39:40 +02002137 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002139 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2142 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Jens Axboe22e2c502005-06-27 10:55:12 +02002146 init_timer(&cfqd->idle_slice_timer);
2147 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2148 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2149
2150 init_timer(&cfqd->idle_class_timer);
2151 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2152 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2153
David Howells65f27f32006-11-22 14:55:48 +00002154 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002157 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2158 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 cfqd->cfq_back_max = cfq_back_max;
2160 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002161 cfqd->cfq_slice[0] = cfq_slice_async;
2162 cfqd->cfq_slice[1] = cfq_slice_sync;
2163 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2164 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002165
Jens Axboebc1c1162006-06-08 08:49:06 +02002166 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002167out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172static void cfq_slab_kill(void)
2173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (cfq_pool)
2175 kmem_cache_destroy(cfq_pool);
2176 if (cfq_ioc_pool)
2177 kmem_cache_destroy(cfq_ioc_pool);
2178}
2179
2180static int __init cfq_slab_setup(void)
2181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2183 NULL, NULL);
2184 if (!cfq_pool)
2185 goto fail;
2186
2187 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2188 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2189 if (!cfq_ioc_pool)
2190 goto fail;
2191
2192 return 0;
2193fail:
2194 cfq_slab_kill();
2195 return -ENOMEM;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198/*
2199 * sysfs parts below -->
2200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static ssize_t
2202cfq_var_show(unsigned int var, char *page)
2203{
2204 return sprintf(page, "%d\n", var);
2205}
2206
2207static ssize_t
2208cfq_var_store(unsigned int *var, const char *page, size_t count)
2209{
2210 char *p = (char *) page;
2211
2212 *var = simple_strtoul(p, &p, 10);
2213 return count;
2214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002217static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002219 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 unsigned int __data = __VAR; \
2221 if (__CONV) \
2222 __data = jiffies_to_msecs(__data); \
2223 return cfq_var_show(__data, (page)); \
2224}
2225SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002226SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2227SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002228SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2229SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002230SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2231SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2232SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2233SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234#undef SHOW_FUNCTION
2235
2236#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002237static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002239 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 unsigned int __data; \
2241 int ret = cfq_var_store(&__data, (page), count); \
2242 if (__data < (MIN)) \
2243 __data = (MIN); \
2244 else if (__data > (MAX)) \
2245 __data = (MAX); \
2246 if (__CONV) \
2247 *(__PTR) = msecs_to_jiffies(__data); \
2248 else \
2249 *(__PTR) = __data; \
2250 return ret; \
2251}
2252STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002253STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2254STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002255STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2256STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002257STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2258STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2259STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2260STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261#undef STORE_FUNCTION
2262
Al Viroe572ec72006-03-18 22:27:18 -05002263#define CFQ_ATTR(name) \
2264 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002265
Al Viroe572ec72006-03-18 22:27:18 -05002266static struct elv_fs_entry cfq_attrs[] = {
2267 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002268 CFQ_ATTR(fifo_expire_sync),
2269 CFQ_ATTR(fifo_expire_async),
2270 CFQ_ATTR(back_seek_max),
2271 CFQ_ATTR(back_seek_penalty),
2272 CFQ_ATTR(slice_sync),
2273 CFQ_ATTR(slice_async),
2274 CFQ_ATTR(slice_async_rq),
2275 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002276 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277};
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279static struct elevator_type iosched_cfq = {
2280 .ops = {
2281 .elevator_merge_fn = cfq_merge,
2282 .elevator_merged_fn = cfq_merged_request,
2283 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002284 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002285 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002287 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 .elevator_deactivate_req_fn = cfq_deactivate_request,
2289 .elevator_queue_empty_fn = cfq_queue_empty,
2290 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002291 .elevator_former_req_fn = elv_rb_former_request,
2292 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .elevator_set_req_fn = cfq_set_request,
2294 .elevator_put_req_fn = cfq_put_request,
2295 .elevator_may_queue_fn = cfq_may_queue,
2296 .elevator_init_fn = cfq_init_queue,
2297 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002298 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 },
Al Viro3d1ab402006-03-18 18:35:43 -05002300 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .elevator_name = "cfq",
2302 .elevator_owner = THIS_MODULE,
2303};
2304
2305static int __init cfq_init(void)
2306{
2307 int ret;
2308
Jens Axboe22e2c502005-06-27 10:55:12 +02002309 /*
2310 * could be 0 on HZ < 1000 setups
2311 */
2312 if (!cfq_slice_async)
2313 cfq_slice_async = 1;
2314 if (!cfq_slice_idle)
2315 cfq_slice_idle = 1;
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (cfq_slab_setup())
2318 return -ENOMEM;
2319
2320 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002321 if (ret)
2322 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return ret;
2325}
2326
2327static void __exit cfq_exit(void)
2328{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002329 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002331 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002332 /* ioc_gone's update must be visible before reading ioc_count */
2333 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002334 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002335 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002336 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002337 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
2340module_init(cfq_init);
2341module_exit(cfq_exit);
2342
2343MODULE_AUTHOR("Jens Axboe");
2344MODULE_LICENSE("GPL");
2345MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");