blob: 81c057eadfccd5f5c9480e0fa752a49d566bbcb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050010#include <linux/blkdev.h>
11#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hash.h>
13#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * tunables
18 */
Arjan van de Ven64100092006-01-06 09:46:02 +010019static const int cfq_quantum = 4; /* max queue in one round of service */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven64100092006-01-06 09:46:02 +010024static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020025static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020027static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020028
Jens Axboed9e76202007-04-20 14:27:50 +020029/*
30 * grace period before allowing idle class to get disk access
31 */
Jens Axboe22e2c502005-06-27 10:55:12 +020032#define CFQ_IDLE_GRACE (HZ / 10)
Jens Axboed9e76202007-04-20 14:27:50 +020033
34/*
35 * below this threshold, we consider thinktime immediate
36 */
37#define CFQ_MIN_TT (2)
38
Jens Axboe22e2c502005-06-27 10:55:12 +020039#define CFQ_SLICE_SCALE (5)
40
41#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * for the hash of cfqq inside the cfqd
45 */
46#define CFQ_QHASH_SHIFT 6
47#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
48#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
51
Jens Axboe5e705372006-07-13 12:39:25 +020052#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
53#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Lametere18b8902006-12-06 20:33:20 -080055static struct kmem_cache *cfq_pool;
56static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jens Axboe4050cf12006-07-19 05:07:12 +020058static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050059static struct completion *ioc_gone;
60
Jens Axboe22e2c502005-06-27 10:55:12 +020061#define CFQ_PRIO_LISTS IOPRIO_BE_NR
62#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020063#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
64
Jens Axboe3b181522005-06-27 10:56:24 +020065#define ASYNC (0)
66#define SYNC (1)
67
Jens Axboe6d048f52007-04-25 12:44:27 +020068#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
Jens Axboe22e2c502005-06-27 10:55:12 +020069
Jens Axboe206dc692006-03-28 13:03:44 +020070#define sample_valid(samples) ((samples) > 80)
71
Jens Axboe22e2c502005-06-27 10:55:12 +020072/*
Jens Axboecc09e292007-04-26 12:53:50 +020073 * Most of our rbtree usage is for sorting with min extraction, so
74 * if we cache the leftmost node we don't have to walk down the tree
75 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
76 * move this into the elevator for the rq sorting as well.
77 */
78struct cfq_rb_root {
79 struct rb_root rb;
80 struct rb_node *left;
81};
82#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
83
84/*
Jens Axboe22e2c502005-06-27 10:55:12 +020085 * Per block device queue structure
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020088 request_queue_t *queue;
89
90 /*
91 * rr list of queues with requests and the count of them
92 */
Jens Axboecc09e292007-04-26 12:53:50 +020093 struct cfq_rb_root service_tree;
Jens Axboe22e2c502005-06-27 10:55:12 +020094 struct list_head cur_rr;
95 struct list_head idle_rr;
96 unsigned int busy_queues;
97
98 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020099 * cfqq lookup hash
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jens Axboe22e2c502005-06-27 10:55:12 +0200103 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +0200104 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200105
106 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200107 * idle window management
108 */
109 struct timer_list idle_slice_timer;
110 struct work_struct unplug_work;
111
112 struct cfq_queue *active_queue;
113 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200114 unsigned int dispatch_slice;
115
116 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jens Axboe6d048f52007-04-25 12:44:27 +0200118 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200119 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
122 * tunables, see top of file
123 */
124 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200125 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int cfq_back_penalty;
127 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200128 unsigned int cfq_slice[2];
129 unsigned int cfq_slice_async_rq;
130 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500131
132 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200133
134 sector_t new_seek_mean;
135 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Jens Axboe22e2c502005-06-27 10:55:12 +0200138/*
139 * Per process-grouping structure
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct cfq_queue {
142 /* reference count */
143 atomic_t ref;
144 /* parent cfq_data */
145 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200146 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct hlist_node cfq_hash;
148 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200149 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200150 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct list_head cfq_list;
Jens Axboed9e76202007-04-20 14:27:50 +0200152 /* service_tree member */
153 struct rb_node rb_node;
154 /* service_tree key */
155 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* sorted list of pending requests */
157 struct rb_root sort_list;
158 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200159 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* requests queued in sort_list */
161 int queued[2];
162 /* currently allocated requests */
163 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200164 /* pending metadata requests */
165 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200167 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jens Axboe22e2c502005-06-27 10:55:12 +0200169 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100170 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jens Axboe6d048f52007-04-25 12:44:27 +0200172 /* number of requests that are on the dispatch list or inside driver */
173 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200174
175 /* io prio of this group */
176 unsigned short ioprio, org_ioprio;
177 unsigned short ioprio_class, org_ioprio_class;
178
Jens Axboe3b181522005-06-27 10:56:24 +0200179 /* various state flags, see below */
180 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200181
182 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Jens Axboe3b181522005-06-27 10:56:24 +0200185enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100186 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
187 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
188 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
189 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
190 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
191 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
192 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
193 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
194 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100195 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200196};
197
198#define CFQ_CFQQ_FNS(name) \
199static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
200{ \
201 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
202} \
203static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
204{ \
205 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
206} \
207static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
208{ \
209 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
210}
211
212CFQ_CFQQ_FNS(on_rr);
213CFQ_CFQQ_FNS(wait_request);
214CFQ_CFQQ_FNS(must_alloc);
215CFQ_CFQQ_FNS(must_alloc_slice);
216CFQ_CFQQ_FNS(must_dispatch);
217CFQ_CFQQ_FNS(fifo_expire);
218CFQ_CFQQ_FNS(idle_window);
219CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200220CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100221CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200222#undef CFQ_CFQQ_FNS
223
Jens Axboe3b181522005-06-27 10:56:24 +0200224static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200225static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500226static 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 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700229 * scheduler run of queue, if there are requests pending and no one in the
230 * driver that will restart queueing
231 */
232static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
233{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100234 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700235 kblockd_schedule_work(&cfqd->unplug_work);
236}
237
238static int cfq_queue_empty(request_queue_t *q)
239{
240 struct cfq_data *cfqd = q->elevator->elevator_data;
241
Jens Axboeb4878f22005-10-20 16:42:29 +0200242 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700243}
244
Jens Axboe7749a8d2006-12-13 13:02:26 +0100245static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200246{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100247 /*
248 * Use the per-process queue, for read requests and syncronous writes
249 */
250 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200251 return task->pid;
252
253 return CFQ_KEY_ASYNC;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100257 * Scale schedule slice based on io priority. Use the sync time slice only
258 * if a queue is marked sync and has sync io queued. A sync queue with async
259 * io only, should not get full sync slice length.
260 */
Jens Axboed9e76202007-04-20 14:27:50 +0200261static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
262 unsigned short prio)
263{
264 const int base_slice = cfqd->cfq_slice[sync];
265
266 WARN_ON(prio >= IOPRIO_BE_NR);
267
268 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
269}
270
Jens Axboe44f7c162007-01-19 11:51:58 +1100271static inline int
272cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
273{
Jens Axboed9e76202007-04-20 14:27:50 +0200274 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100275}
276
277static inline void
278cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
279{
280 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
281}
282
283/*
284 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
285 * isn't valid until the first request from the dispatch is activated
286 * and the slice time set.
287 */
288static inline int cfq_slice_used(struct cfq_queue *cfqq)
289{
290 if (cfq_cfqq_slice_new(cfqq))
291 return 0;
292 if (time_before(jiffies, cfqq->slice_end))
293 return 0;
294
295 return 1;
296}
297
298/*
Jens Axboe5e705372006-07-13 12:39:25 +0200299 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200301 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
Jens Axboe5e705372006-07-13 12:39:25 +0200303static struct request *
304cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200308#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
309#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
310 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jens Axboe5e705372006-07-13 12:39:25 +0200312 if (rq1 == NULL || rq1 == rq2)
313 return rq2;
314 if (rq2 == NULL)
315 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200316
Jens Axboe5e705372006-07-13 12:39:25 +0200317 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
318 return rq1;
319 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
320 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200321 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
322 return rq1;
323 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
324 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jens Axboe5e705372006-07-13 12:39:25 +0200326 s1 = rq1->sector;
327 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jens Axboe6d048f52007-04-25 12:44:27 +0200329 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * by definition, 1KiB is 2 sectors
333 */
334 back_max = cfqd->cfq_back_max * 2;
335
336 /*
337 * Strict one way elevator _except_ in the case where we allow
338 * short backward seeks which are biased as twice the cost of a
339 * similar forward seek.
340 */
341 if (s1 >= last)
342 d1 = s1 - last;
343 else if (s1 + back_max >= last)
344 d1 = (last - s1) * cfqd->cfq_back_penalty;
345 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200346 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (s2 >= last)
349 d2 = s2 - last;
350 else if (s2 + back_max >= last)
351 d2 = (last - s2) * cfqd->cfq_back_penalty;
352 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200353 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Andreas Mohre8a99052006-03-28 08:59:49 +0200357 /*
358 * By doing switch() on the bit mask "wrap" we avoid having to
359 * check two variables for all permutations: --> faster!
360 */
361 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200362 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200363 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200364 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200365 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200366 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200367 else {
368 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200369 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200370 else
Jens Axboe5e705372006-07-13 12:39:25 +0200371 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200372 }
373
374 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200375 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200376 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200377 return rq2;
378 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200379 default:
380 /*
381 * Since both rqs are wrapped,
382 * start with the one that's further behind head
383 * (--> only *one* back seek required),
384 * since back seek takes more time than forward.
385 */
386 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200387 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 else
Jens Axboe5e705372006-07-13 12:39:25 +0200389 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391}
392
Jens Axboecc09e292007-04-26 12:53:50 +0200393static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
394{
395 if (!root->left)
396 root->left = rb_first(&root->rb);
397
398 return root->left;
399}
400
401static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
402{
403 if (root->left == n)
404 root->left = NULL;
405
406 rb_erase(n, &root->rb);
407 RB_CLEAR_NODE(n);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
411 * would be nice to take fifo expire time into account as well
412 */
Jens Axboe5e705372006-07-13 12:39:25 +0200413static struct request *
414cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
415 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jens Axboe21183b02006-07-13 12:33:14 +0200417 struct rb_node *rbnext = rb_next(&last->rb_node);
418 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200419 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jens Axboe21183b02006-07-13 12:33:14 +0200421 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200424 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200427 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200428 else {
429 rbnext = rb_first(&cfqq->sort_list);
430 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200431 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jens Axboe21183b02006-07-13 12:33:14 +0200434 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Jens Axboed9e76202007-04-20 14:27:50 +0200437static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
438 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jens Axboed9e76202007-04-20 14:27:50 +0200440 /*
441 * just an approximation, should be ok.
442 */
443 return ((cfqd->busy_queues - 1) * cfq_prio_slice(cfqd, 1, 0));
444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jens Axboed9e76202007-04-20 14:27:50 +0200446static void cfq_service_tree_add(struct cfq_data *cfqd,
447 struct cfq_queue *cfqq)
448{
Jens Axboecc09e292007-04-26 12:53:50 +0200449 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200450 struct rb_node *parent = NULL;
Jens Axboed9e76202007-04-20 14:27:50 +0200451 unsigned long rb_key;
Jens Axboecc09e292007-04-26 12:53:50 +0200452 int left = 1;
Jens Axboed9e76202007-04-20 14:27:50 +0200453
454 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
455 rb_key += cfqq->slice_resid;
456 cfqq->slice_resid = 0;
457
458 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100459 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200460 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100461 */
Jens Axboed9e76202007-04-20 14:27:50 +0200462 if (rb_key == cfqq->rb_key)
463 return;
Jens Axboe53b03742006-07-28 09:48:51 +0200464
Jens Axboecc09e292007-04-26 12:53:50 +0200465 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200466 }
Jens Axboed9e76202007-04-20 14:27:50 +0200467
468 while (*p) {
Jens Axboecc09e292007-04-26 12:53:50 +0200469 struct cfq_queue *__cfqq;
470
Jens Axboed9e76202007-04-20 14:27:50 +0200471 parent = *p;
472 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
473
Jens Axboe0c534e02007-04-18 20:01:57 +0200474 /*
475 * sort RT queues first, we always want to give
476 * preference to them. after that, sort on the next
477 * service time.
478 */
479 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
480 p = &(*p)->rb_left;
481 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
482 p = &(*p)->rb_right;
483 else if (rb_key < __cfqq->rb_key)
Jens Axboed9e76202007-04-20 14:27:50 +0200484 p = &(*p)->rb_left;
Jens Axboecc09e292007-04-26 12:53:50 +0200485 else {
Jens Axboed9e76202007-04-20 14:27:50 +0200486 p = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +0200487 left = 0;
488 }
Jens Axboed9e76202007-04-20 14:27:50 +0200489 }
490
Jens Axboecc09e292007-04-26 12:53:50 +0200491 if (left)
492 cfqd->service_tree.left = &cfqq->rb_node;
493
Jens Axboed9e76202007-04-20 14:27:50 +0200494 cfqq->rb_key = rb_key;
495 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200496 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Jens Axboe6d048f52007-04-25 12:44:27 +0200499static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
500{
501 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe6d048f52007-04-25 12:44:27 +0200502
503 /*
504 * Resorting requires the cfqq to be on the RR list already.
505 */
506 if (!cfq_cfqq_on_rr(cfqq))
507 return;
508
Jens Axboed9e76202007-04-20 14:27:50 +0200509 list_del_init(&cfqq->cfq_list);
Jens Axboe6d048f52007-04-25 12:44:27 +0200510
Jens Axboe0c534e02007-04-18 20:01:57 +0200511 if (cfq_class_idle(cfqq)) {
Jens Axboe6d048f52007-04-25 12:44:27 +0200512 /*
513 * IDLE goes to the tail of the idle list
514 */
515 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
516 } else {
517 /*
Jens Axboe0c534e02007-04-18 20:01:57 +0200518 * RT and BE queues, sort into the rbtree
Jens Axboe6d048f52007-04-25 12:44:27 +0200519 */
Jens Axboed9e76202007-04-20 14:27:50 +0200520 cfq_service_tree_add(cfqd, cfqq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200521 }
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200526 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200529cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Jens Axboe3b181522005-06-27 10:56:24 +0200531 BUG_ON(cfq_cfqq_on_rr(cfqq));
532 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cfqd->busy_queues++;
534
Jens Axboeb4878f22005-10-20 16:42:29 +0200535 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538static inline void
539cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
540{
Jens Axboe3b181522005-06-27 10:56:24 +0200541 BUG_ON(!cfq_cfqq_on_rr(cfqq));
542 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200543 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jens Axboecc09e292007-04-26 12:53:50 +0200545 if (!RB_EMPTY_NODE(&cfqq->rb_node))
546 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 BUG_ON(!cfqd->busy_queues);
549 cfqd->busy_queues--;
550}
551
552/*
553 * rb tree support functions
554 */
Jens Axboe5e705372006-07-13 12:39:25 +0200555static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Jens Axboe5e705372006-07-13 12:39:25 +0200557 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200558 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200559 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jens Axboeb4878f22005-10-20 16:42:29 +0200561 BUG_ON(!cfqq->queued[sync]);
562 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jens Axboe5e705372006-07-13 12:39:25 +0200564 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jens Axboedd67d052006-06-21 09:36:18 +0200566 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200567 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Jens Axboe5e705372006-07-13 12:39:25 +0200570static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Jens Axboe5e705372006-07-13 12:39:25 +0200572 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200574 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jens Axboe5380a102006-07-13 12:37:56 +0200576 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /*
579 * looks a little odd, but the first insert might return an alias.
580 * if that happens, put the alias on the dispatch list
581 */
Jens Axboe21183b02006-07-13 12:33:14 +0200582 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200583 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100584
585 if (!cfq_cfqq_on_rr(cfqq))
586 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200587
588 /*
589 * check if this request is a better next-serve candidate
590 */
591 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
592 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200596cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jens Axboe5380a102006-07-13 12:37:56 +0200598 elv_rb_del(&cfqq->sort_list, rq);
599 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200600 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Jens Axboe206dc692006-03-28 13:03:44 +0200603static struct request *
604cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jens Axboe206dc692006-03-28 13:03:44 +0200606 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100607 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200608 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jens Axboe206dc692006-03-28 13:03:44 +0200610 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200611 if (cfqq) {
612 sector_t sector = bio->bi_sector + bio_sectors(bio);
613
Jens Axboe21183b02006-07-13 12:33:14 +0200614 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return NULL;
618}
619
Jens Axboeb4878f22005-10-20 16:42:29 +0200620static void cfq_activate_request(request_queue_t *q, struct request *rq)
621{
622 struct cfq_data *cfqd = q->elevator->elevator_data;
623
624 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200625
626 /*
627 * If the depth is larger 1, it really could be queueing. But lets
628 * make the mark a little higher - idling could still be good for
629 * low queueing, and a low queueing number could also just indicate
630 * a SCSI mid layer like behaviour where limit+1 is often seen.
631 */
632 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
633 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200634
635 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
639{
Jens Axboe22e2c502005-06-27 10:55:12 +0200640 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jens Axboeb4878f22005-10-20 16:42:29 +0200642 WARN_ON(!cfqd->rq_in_driver);
643 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Jens Axboeb4878f22005-10-20 16:42:29 +0200646static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jens Axboe5e705372006-07-13 12:39:25 +0200648 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200649
Jens Axboe5e705372006-07-13 12:39:25 +0200650 if (cfqq->next_rq == rq)
651 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jens Axboeb4878f22005-10-20 16:42:29 +0200653 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200654 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200655
656 if (rq_is_meta(rq)) {
657 WARN_ON(!cfqq->meta_pending);
658 cfqq->meta_pending--;
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662static int
663cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
664{
665 struct cfq_data *cfqd = q->elevator->elevator_data;
666 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jens Axboe206dc692006-03-28 13:03:44 +0200668 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200669 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200670 *req = __rq;
671 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
674 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Jens Axboe21183b02006-07-13 12:33:14 +0200677static void cfq_merged_request(request_queue_t *q, struct request *req,
678 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jens Axboe21183b02006-07-13 12:33:14 +0200680 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200681 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Jens Axboe5e705372006-07-13 12:39:25 +0200683 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void
688cfq_merged_requests(request_queue_t *q, struct request *rq,
689 struct request *next)
690{
Jens Axboe22e2c502005-06-27 10:55:12 +0200691 /*
692 * reposition in fifo if next is older than rq
693 */
694 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
695 time_before(next->start_time, rq->start_time))
696 list_move(&rq->queuelist, &next->queuelist);
697
Jens Axboeb4878f22005-10-20 16:42:29 +0200698 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200699}
700
Jens Axboeda775262006-12-20 11:04:12 +0100701static int cfq_allow_merge(request_queue_t *q, struct request *rq,
702 struct bio *bio)
703{
704 struct cfq_data *cfqd = q->elevator->elevator_data;
705 const int rw = bio_data_dir(bio);
706 struct cfq_queue *cfqq;
707 pid_t key;
708
709 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100710 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100711 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100712 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100713 return 0;
714
715 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100716 * Lookup the cfqq that this bio will be queued with. Allow
717 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100718 */
Jens Axboe719d3402006-12-22 09:38:53 +0100719 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100720 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100721
722 if (cfqq == RQ_CFQQ(rq))
723 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100724
Jens Axboeec8acb62007-01-02 18:32:11 +0100725 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100726}
727
Jens Axboe22e2c502005-06-27 10:55:12 +0200728static inline void
729__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
730{
731 if (cfqq) {
732 /*
733 * stop potential idle class queues waiting service
734 */
735 del_timer(&cfqd->idle_class_timer);
736
Jens Axboe22e2c502005-06-27 10:55:12 +0200737 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200738 cfq_clear_cfqq_must_alloc_slice(cfqq);
739 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100740 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200741 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200742 }
743
744 cfqd->active_queue = cfqq;
745}
746
747/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100748 * current cfqq expired its slice (or was too idle), select new one
749 */
750static void
751__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100752 int preempted, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100753{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100754 if (cfq_cfqq_wait_request(cfqq))
755 del_timer(&cfqd->idle_slice_timer);
756
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100757 cfq_clear_cfqq_must_dispatch(cfqq);
758 cfq_clear_cfqq_wait_request(cfqq);
759
760 /*
761 * store what was left of this slice, if the queue idled out
762 * or was preempted
763 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100764 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100765 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100766
Jens Axboe98e41c72007-02-05 11:55:35 +0100767 cfq_resort_rr_list(cfqq, preempted);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100768
769 if (cfqq == cfqd->active_queue)
770 cfqd->active_queue = NULL;
771
772 if (cfqd->active_cic) {
773 put_io_context(cfqd->active_cic->ioc);
774 cfqd->active_cic = NULL;
775 }
776
777 cfqd->dispatch_slice = 0;
778}
779
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100780static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
781 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100782{
783 struct cfq_queue *cfqq = cfqd->active_queue;
784
785 if (cfqq)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100786 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100787}
788
Jens Axboe6d048f52007-04-25 12:44:27 +0200789static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200790{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100791 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200792
Jens Axboed9e76202007-04-20 14:27:50 +0200793 if (!list_empty(&cfqd->cur_rr)) {
Jens Axboe89850f72006-07-22 16:48:31 +0200794 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200795 * if current list is non-empty, grab first entry.
Jens Axboe89850f72006-07-22 16:48:31 +0200796 */
Jens Axboed9e76202007-04-20 14:27:50 +0200797 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
Jens Axboecc09e292007-04-26 12:53:50 +0200798 } else if (!RB_EMPTY_ROOT(&cfqd->service_tree.rb)) {
799 struct rb_node *n = cfq_rb_first(&cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200800
801 cfqq = rb_entry(n, struct cfq_queue, rb_node);
Jens Axboe89850f72006-07-22 16:48:31 +0200802 } else if (!list_empty(&cfqd->idle_rr)) {
803 /*
804 * if we have idle queues and no rt or be queues had pending
805 * requests, either allow immediate service if the grace period
806 * has passed or arm the idle grace timer
807 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200808 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
809
810 if (time_after_eq(jiffies, end))
811 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
812 else
813 mod_timer(&cfqd->idle_class_timer, end);
814 }
815
Jens Axboe6d048f52007-04-25 12:44:27 +0200816 return cfqq;
817}
818
819static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
820{
821 struct cfq_queue *cfqq;
822
Jens Axboed9e76202007-04-20 14:27:50 +0200823 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200824 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200825 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200826}
827
Jens Axboed9e76202007-04-20 14:27:50 +0200828static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
829 struct request *rq)
830{
831 if (rq->sector >= cfqd->last_position)
832 return rq->sector - cfqd->last_position;
833 else
834 return cfqd->last_position - rq->sector;
835}
836
Jens Axboe6d048f52007-04-25 12:44:27 +0200837static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
838{
839 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200840
Jens Axboe6d048f52007-04-25 12:44:27 +0200841 if (!sample_valid(cic->seek_samples))
842 return 0;
843
844 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
845}
846
Jens Axboed9e76202007-04-20 14:27:50 +0200847static int cfq_close_cooperator(struct cfq_data *cfq_data,
848 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200849{
Jens Axboe6d048f52007-04-25 12:44:27 +0200850 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200851 * We should notice if some of the queues are cooperating, eg
852 * working closely on the same area of the disk. In that case,
853 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200854 */
Jens Axboed9e76202007-04-20 14:27:50 +0200855 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200856}
857
858#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
859
860static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200861{
Jens Axboe17926692007-01-19 11:59:30 +1100862 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200863 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100864 unsigned long sl;
865
Jens Axboedd67d052006-06-21 09:36:18 +0200866 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200867 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200868
869 /*
870 * idle is disabled, either manually or by past process history
871 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200872 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
873 return;
874
Jens Axboe22e2c502005-06-27 10:55:12 +0200875 /*
876 * task has exited, don't wait
877 */
Jens Axboe206dc692006-03-28 13:03:44 +0200878 cic = cfqd->active_cic;
879 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200880 return;
881
882 /*
883 * See if this prio level has a good candidate
884 */
Jens Axboe1afba042007-04-17 12:47:55 +0200885 if (cfq_close_cooperator(cfqd, cfqq) &&
886 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200887 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200888
Jens Axboe3b181522005-06-27 10:56:24 +0200889 cfq_mark_cfqq_must_dispatch(cfqq);
890 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200891
Jens Axboe206dc692006-03-28 13:03:44 +0200892 /*
893 * we don't want to idle for seeks, but we do want to allow
894 * fair distribution of slice time for a process doing back-to-back
895 * seeks. so allow a little bit of time for him to submit a new rq
896 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200897 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200898 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200899 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200900
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100901 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Jens Axboe5e705372006-07-13 12:39:25 +0200904static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jens Axboe5e705372006-07-13 12:39:25 +0200906 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200907
Jens Axboe5380a102006-07-13 12:37:56 +0200908 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200909 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200910 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * return expired entry, or NULL to just start from scratch in rbtree
915 */
Jens Axboe5e705372006-07-13 12:39:25 +0200916static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200919 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200920 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Jens Axboe3b181522005-06-27 10:56:24 +0200922 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100924
925 cfq_mark_cfqq_fifo_expire(cfqq);
926
Jens Axboe89850f72006-07-22 16:48:31 +0200927 if (list_empty(&cfqq->fifo))
928 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jens Axboe6d048f52007-04-25 12:44:27 +0200930 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200931 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Jens Axboe6d048f52007-04-25 12:44:27 +0200933 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
934 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Jens Axboe6d048f52007-04-25 12:44:27 +0200936 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Jens Axboe22e2c502005-06-27 10:55:12 +0200939static inline int
940cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
941{
942 const int base_rq = cfqd->cfq_slice_async_rq;
943
944 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
945
946 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
947}
948
949/*
950 * get next queue for service
951 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100952static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200953{
Jens Axboe22e2c502005-06-27 10:55:12 +0200954 struct cfq_queue *cfqq;
955
956 cfqq = cfqd->active_queue;
957 if (!cfqq)
958 goto new_queue;
959
960 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200961 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200962 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200963 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200964 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200965
966 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200967 * The active queue has requests and isn't expired, allow it to
968 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200969 */
Jens Axboedd67d052006-06-21 09:36:18 +0200970 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200971 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200972
973 /*
974 * No requests pending. If the active queue still has requests in
975 * flight or is idling for a new request, allow either of these
976 * conditions to happen (or time out) before selecting a new queue.
977 */
978 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +0200979 cfqq = NULL;
980 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200981 }
982
Jens Axboe3b181522005-06-27 10:56:24 +0200983expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100984 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200985new_queue:
986 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200987keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200988 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200989}
990
991static int
992__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
993 int max_dispatch)
994{
995 int dispatched = 0;
996
Jens Axboedd67d052006-06-21 09:36:18 +0200997 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200998
999 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001000 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001001
1002 /*
1003 * follow expired path, else get first next available
1004 */
Jens Axboe5e705372006-07-13 12:39:25 +02001005 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1006 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001007
1008 /*
1009 * finally, insert request into driver dispatch list
1010 */
Jens Axboe5e705372006-07-13 12:39:25 +02001011 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001012
1013 cfqd->dispatch_slice++;
1014 dispatched++;
1015
1016 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001017 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1018 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001019 }
1020
Jens Axboedd67d052006-06-21 09:36:18 +02001021 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001022 break;
1023
1024 } while (dispatched < max_dispatch);
1025
1026 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001027 * expire an async queue immediately if it has used up its slice. idle
1028 * queue always expire after 1 dispatch round.
1029 */
Jens Axboea9938002007-04-20 08:55:52 +02001030 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001031 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001032 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001033 cfqq->slice_end = jiffies + 1;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001034 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001035 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001036
1037 return dispatched;
1038}
1039
Jens Axboed9e76202007-04-20 14:27:50 +02001040static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1041{
1042 int dispatched = 0;
1043
1044 while (cfqq->next_rq) {
1045 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1046 dispatched++;
1047 }
1048
1049 BUG_ON(!list_empty(&cfqq->fifo));
1050 return dispatched;
1051}
1052
1053static int cfq_forced_dispatch_cfqqs(struct list_head *list)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001054{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001055 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001056 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001057
Jens Axboecaaa5f92006-06-16 11:23:00 +02001058 dispatched = 0;
Jens Axboed9e76202007-04-20 14:27:50 +02001059 list_for_each_entry_safe(cfqq, next, list, cfq_list)
1060 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001061
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001062 return dispatched;
1063}
1064
Jens Axboed9e76202007-04-20 14:27:50 +02001065static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001066{
Jens Axboed9e76202007-04-20 14:27:50 +02001067 int dispatched = 0;
1068 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001069
Jens Axboecc09e292007-04-26 12:53:50 +02001070 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
Jens Axboed9e76202007-04-20 14:27:50 +02001071 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1072
1073 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1074 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001075
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001076 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1077 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1078
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001079 cfq_slice_expired(cfqd, 0, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001080
1081 BUG_ON(cfqd->busy_queues);
1082
1083 return dispatched;
1084}
1085
Jens Axboed9e76202007-04-20 14:27:50 +02001086static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001089 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001090 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Jens Axboe22e2c502005-06-27 10:55:12 +02001092 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 0;
1094
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001095 if (unlikely(force))
1096 return cfq_forced_dispatch(cfqd);
1097
Jens Axboecaaa5f92006-06-16 11:23:00 +02001098 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001099 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001100 int max_dispatch;
1101
Jens Axboea9938002007-04-20 08:55:52 +02001102 if (cfqd->busy_queues > 1) {
1103 /*
Jens Axboea9938002007-04-20 08:55:52 +02001104 * So we have dispatched before in this round, if the
1105 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001106 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001107 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001108 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1109 dispatched)
1110 break;
1111 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001112 break;
1113 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001114
Jens Axboe3b181522005-06-27 10:56:24 +02001115 cfq_clear_cfqq_must_dispatch(cfqq);
1116 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001117 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001119 max_dispatch = cfqd->cfq_quantum;
1120 if (cfq_class_idle(cfqq))
1121 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Jens Axboecaaa5f92006-06-16 11:23:00 +02001123 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Jens Axboecaaa5f92006-06-16 11:23:00 +02001126 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/*
Jens Axboe5e705372006-07-13 12:39:25 +02001130 * task holds one reference to the queue, dropped when task exits. each rq
1131 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *
1133 * queue lock must be held here.
1134 */
1135static void cfq_put_queue(struct cfq_queue *cfqq)
1136{
Jens Axboe22e2c502005-06-27 10:55:12 +02001137 struct cfq_data *cfqd = cfqq->cfqd;
1138
1139 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!atomic_dec_and_test(&cfqq->ref))
1142 return;
1143
1144 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001145 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001146 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001148 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001149 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001150 cfq_schedule_dispatch(cfqd);
1151 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * it's on the empty list and still hashed
1155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 hlist_del(&cfqq->cfq_hash);
1157 kmem_cache_free(cfq_pool, cfqq);
1158}
1159
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001160static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001161__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1162 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001165 struct hlist_node *entry;
1166 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jens Axboe206dc692006-03-28 13:03:44 +02001168 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001169 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jens Axboe206dc692006-03-28 13:03:44 +02001171 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return __cfqq;
1173 }
1174
1175 return NULL;
1176}
1177
1178static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001179cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Jens Axboe3b181522005-06-27 10:56:24 +02001181 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Jens Axboee2d74ac2006-03-28 08:59:01 +02001184static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jens Axboe22e2c502005-06-27 10:55:12 +02001186 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001187 struct rb_node *n;
1188 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001189
Jens Axboee2d74ac2006-03-28 08:59:01 +02001190 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1191 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1192 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001193 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001194 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001195 }
1196
Jens Axboe4050cf12006-07-19 05:07:12 +02001197 elv_ioc_count_mod(ioc_count, -freed);
1198
1199 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001200 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Jens Axboe89850f72006-07-22 16:48:31 +02001203static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1204{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001205 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001206 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001207 cfq_schedule_dispatch(cfqd);
1208 }
Jens Axboe89850f72006-07-22 16:48:31 +02001209
1210 cfq_put_queue(cfqq);
1211}
1212
1213static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1214 struct cfq_io_context *cic)
1215{
Jens Axboefc463792006-08-29 09:05:44 +02001216 list_del_init(&cic->queue_list);
1217 smp_wmb();
1218 cic->key = NULL;
1219
Jens Axboe89850f72006-07-22 16:48:31 +02001220 if (cic->cfqq[ASYNC]) {
1221 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1222 cic->cfqq[ASYNC] = NULL;
1223 }
1224
1225 if (cic->cfqq[SYNC]) {
1226 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1227 cic->cfqq[SYNC] = NULL;
1228 }
Jens Axboe89850f72006-07-22 16:48:31 +02001229}
1230
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001233 * Called with interrupts disabled
1234 */
1235static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1236{
Al Viro478a82b2006-03-18 13:25:24 -05001237 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001238
Jens Axboe89850f72006-07-22 16:48:31 +02001239 if (cfqd) {
1240 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001241
Jens Axboefc463792006-08-29 09:05:44 +02001242 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001243 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001244 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001245 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001246}
1247
Jens Axboee2d74ac2006-03-28 08:59:01 +02001248static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Jens Axboe22e2c502005-06-27 10:55:12 +02001250 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001251 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * put the reference this task is holding to the various queues
1255 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001256
1257 n = rb_first(&ioc->cic_root);
1258 while (n != NULL) {
1259 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1260
Jens Axboe22e2c502005-06-27 10:55:12 +02001261 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001262 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Jens Axboe22e2c502005-06-27 10:55:12 +02001266static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001267cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Jens Axboeb5deef92006-07-19 23:39:40 +02001269 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jens Axboeb5deef92006-07-19 23:39:40 +02001271 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001273 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001274 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001275 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001276 cic->dtor = cfq_free_io_context;
1277 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001278 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 return cic;
1282}
1283
Jens Axboe22e2c502005-06-27 10:55:12 +02001284static void cfq_init_prio_data(struct cfq_queue *cfqq)
1285{
1286 struct task_struct *tsk = current;
1287 int ioprio_class;
1288
Jens Axboe3b181522005-06-27 10:56:24 +02001289 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001290 return;
1291
1292 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1293 switch (ioprio_class) {
1294 default:
1295 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1296 case IOPRIO_CLASS_NONE:
1297 /*
1298 * no prio set, place us in the middle of the BE classes
1299 */
1300 cfqq->ioprio = task_nice_ioprio(tsk);
1301 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1302 break;
1303 case IOPRIO_CLASS_RT:
1304 cfqq->ioprio = task_ioprio(tsk);
1305 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1306 break;
1307 case IOPRIO_CLASS_BE:
1308 cfqq->ioprio = task_ioprio(tsk);
1309 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1310 break;
1311 case IOPRIO_CLASS_IDLE:
1312 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1313 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001314 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001315 break;
1316 }
1317
1318 /*
1319 * keep track of original prio settings in case we have to temporarily
1320 * elevate the priority of this queue
1321 */
1322 cfqq->org_ioprio = cfqq->ioprio;
1323 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001324 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001325}
1326
Al Viro478a82b2006-03-18 13:25:24 -05001327static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001328{
Al Viro478a82b2006-03-18 13:25:24 -05001329 struct cfq_data *cfqd = cic->key;
1330 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001331 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001332
Jens Axboecaaa5f92006-06-16 11:23:00 +02001333 if (unlikely(!cfqd))
1334 return;
1335
Jens Axboec1b707d2006-10-30 19:54:23 +01001336 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001337
1338 cfqq = cic->cfqq[ASYNC];
1339 if (cfqq) {
1340 struct cfq_queue *new_cfqq;
1341 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1342 GFP_ATOMIC);
1343 if (new_cfqq) {
1344 cic->cfqq[ASYNC] = new_cfqq;
1345 cfq_put_queue(cfqq);
1346 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001347 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001348
1349 cfqq = cic->cfqq[SYNC];
1350 if (cfqq)
1351 cfq_mark_cfqq_prio_changed(cfqq);
1352
Jens Axboec1b707d2006-10-30 19:54:23 +01001353 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001354}
1355
Jens Axboefc463792006-08-29 09:05:44 +02001356static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Al Viroa6a07632006-03-18 13:26:44 -05001358 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001359 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001360
Jens Axboefc463792006-08-29 09:05:44 +02001361 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001362
Jens Axboee2d74ac2006-03-28 08:59:01 +02001363 n = rb_first(&ioc->cic_root);
1364 while (n != NULL) {
1365 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001366
Al Viro478a82b2006-03-18 13:25:24 -05001367 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001368 n = rb_next(n);
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001373cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001374 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1377 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001378 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380retry:
Al Viro6f325a12006-03-18 14:58:37 -05001381 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001382 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (!cfqq) {
1385 if (new_cfqq) {
1386 cfqq = new_cfqq;
1387 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001388 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001389 /*
1390 * Inform the allocator of the fact that we will
1391 * just repeat this allocation if it fails, to allow
1392 * the allocator to do whatever it needs to attempt to
1393 * free memory.
1394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001396 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 spin_lock_irq(cfqd->queue->queue_lock);
1398 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001399 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001400 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001401 if (!cfqq)
1402 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 memset(cfqq, 0, sizeof(*cfqq));
1406
1407 INIT_HLIST_NODE(&cfqq->cfq_hash);
1408 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboed9e76202007-04-20 14:27:50 +02001409 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001410 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 cfqq->key = key;
1413 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1414 atomic_set(&cfqq->ref, 0);
1415 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001416
Jens Axboea9938002007-04-20 08:55:52 +02001417 if (key != CFQ_KEY_ASYNC)
1418 cfq_mark_cfqq_idle_window(cfqq);
1419
Jens Axboe3b181522005-06-27 10:56:24 +02001420 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001421 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001422 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 if (new_cfqq)
1426 kmem_cache_free(cfq_pool, new_cfqq);
1427
1428 atomic_inc(&cfqq->ref);
1429out:
1430 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1431 return cfqq;
1432}
1433
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001434static void
1435cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1436{
Jens Axboefc463792006-08-29 09:05:44 +02001437 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001438 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001439 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001440 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001441}
1442
Jens Axboee2d74ac2006-03-28 08:59:01 +02001443static struct cfq_io_context *
1444cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1445{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001446 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001447 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001448 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001449
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001450restart:
1451 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001452 while (n) {
1453 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001454 /* ->key must be copied to avoid race with cfq_exit_queue() */
1455 k = cic->key;
1456 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001457 cfq_drop_dead_cic(ioc, cic);
1458 goto restart;
1459 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001460
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001461 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001462 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001463 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001464 n = n->rb_right;
1465 else
1466 return cic;
1467 }
1468
1469 return NULL;
1470}
1471
1472static inline void
1473cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1474 struct cfq_io_context *cic)
1475{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001476 struct rb_node **p;
1477 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001478 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001479 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001480 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001481
Jens Axboee2d74ac2006-03-28 08:59:01 +02001482 cic->ioc = ioc;
1483 cic->key = cfqd;
1484
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001485restart:
1486 parent = NULL;
1487 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001488 while (*p) {
1489 parent = *p;
1490 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001491 /* ->key must be copied to avoid race with cfq_exit_queue() */
1492 k = __cic->key;
1493 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001494 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001495 goto restart;
1496 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001497
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001498 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001499 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001500 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001501 p = &(*p)->rb_right;
1502 else
1503 BUG();
1504 }
1505
1506 rb_link_node(&cic->rb_node, parent, p);
1507 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001508
Jens Axboe0261d682006-10-30 19:07:48 +01001509 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001510 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001511 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001512}
1513
Jens Axboe22e2c502005-06-27 10:55:12 +02001514/*
1515 * Setup general io context and cfq io context. There can be several cfq
1516 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001517 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001518 */
1519static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001520cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Jens Axboe22e2c502005-06-27 10:55:12 +02001522 struct io_context *ioc = NULL;
1523 struct cfq_io_context *cic;
1524
1525 might_sleep_if(gfp_mask & __GFP_WAIT);
1526
Jens Axboeb5deef92006-07-19 23:39:40 +02001527 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001528 if (!ioc)
1529 return NULL;
1530
Jens Axboee2d74ac2006-03-28 08:59:01 +02001531 cic = cfq_cic_rb_lookup(cfqd, ioc);
1532 if (cic)
1533 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001534
Jens Axboee2d74ac2006-03-28 08:59:01 +02001535 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1536 if (cic == NULL)
1537 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001538
Jens Axboee2d74ac2006-03-28 08:59:01 +02001539 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001540out:
Jens Axboefc463792006-08-29 09:05:44 +02001541 smp_read_barrier_depends();
1542 if (unlikely(ioc->ioprio_changed))
1543 cfq_ioc_set_ioprio(ioc);
1544
Jens Axboe22e2c502005-06-27 10:55:12 +02001545 return cic;
1546err:
1547 put_io_context(ioc);
1548 return NULL;
1549}
1550
1551static void
1552cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1553{
Jens Axboeaaf12282007-01-19 11:30:16 +11001554 unsigned long elapsed = jiffies - cic->last_end_request;
1555 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001556
1557 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1558 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1559 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1560}
1561
Jens Axboe206dc692006-03-28 13:03:44 +02001562static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001563cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1564 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001565{
1566 sector_t sdist;
1567 u64 total;
1568
Jens Axboe5e705372006-07-13 12:39:25 +02001569 if (cic->last_request_pos < rq->sector)
1570 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001571 else
Jens Axboe5e705372006-07-13 12:39:25 +02001572 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001573
Jens Axboe6d048f52007-04-25 12:44:27 +02001574 if (!cic->seek_samples) {
1575 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1576 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1577 }
1578
Jens Axboe206dc692006-03-28 13:03:44 +02001579 /*
1580 * Don't allow the seek distance to get too large from the
1581 * odd fragment, pagein, etc
1582 */
1583 if (cic->seek_samples <= 60) /* second&third seek */
1584 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1585 else
1586 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1587
1588 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1589 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1590 total = cic->seek_total + (cic->seek_samples/2);
1591 do_div(total, cic->seek_samples);
1592 cic->seek_mean = (sector_t)total;
1593}
Jens Axboe22e2c502005-06-27 10:55:12 +02001594
1595/*
1596 * Disable idle window if the process thinks too long or seeks so much that
1597 * it doesn't matter
1598 */
1599static void
1600cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1601 struct cfq_io_context *cic)
1602{
Jens Axboe3b181522005-06-27 10:56:24 +02001603 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001604
Jens Axboecaaa5f92006-06-16 11:23:00 +02001605 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1606 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001607 enable_idle = 0;
1608 else if (sample_valid(cic->ttime_samples)) {
1609 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1610 enable_idle = 0;
1611 else
1612 enable_idle = 1;
1613 }
1614
Jens Axboe3b181522005-06-27 10:56:24 +02001615 if (enable_idle)
1616 cfq_mark_cfqq_idle_window(cfqq);
1617 else
1618 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001619}
1620
Jens Axboe22e2c502005-06-27 10:55:12 +02001621/*
1622 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1623 * no or if we aren't sure, a 1 will cause a preempt.
1624 */
1625static int
1626cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001627 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001628{
Jens Axboe6d048f52007-04-25 12:44:27 +02001629 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001630
Jens Axboe6d048f52007-04-25 12:44:27 +02001631 cfqq = cfqd->active_queue;
1632 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001633 return 0;
1634
Jens Axboe6d048f52007-04-25 12:44:27 +02001635 if (cfq_slice_used(cfqq))
1636 return 1;
1637
1638 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001639 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001640
1641 if (cfq_class_idle(cfqq))
1642 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001643
Jens Axboe22e2c502005-06-27 10:55:12 +02001644 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001645 * if the new request is sync, but the currently running queue is
1646 * not, let the sync request have priority.
1647 */
Jens Axboe5e705372006-07-13 12:39:25 +02001648 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001649 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001650
Jens Axboe374f84a2006-07-23 01:42:19 +02001651 /*
1652 * So both queues are sync. Let the new request get disk time if
1653 * it's a metadata request and the current queue is doing regular IO.
1654 */
1655 if (rq_is_meta(rq) && !cfqq->meta_pending)
1656 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001657
Jens Axboe1e3335d2007-02-14 19:59:49 +01001658 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1659 return 0;
1660
1661 /*
1662 * if this request is as-good as one we would expect from the
1663 * current cfqq, let it preempt
1664 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001665 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001666 return 1;
1667
Jens Axboe22e2c502005-06-27 10:55:12 +02001668 return 0;
1669}
1670
1671/*
1672 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1673 * let it have half of its nominal slice.
1674 */
1675static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1676{
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001677 cfq_slice_expired(cfqd, 1, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001678
Jens Axboebf572252006-07-19 20:29:12 +02001679 /*
1680 * Put the new queue at the front of the of the current list,
1681 * so we know that it will be selected next.
1682 */
1683 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboed9e76202007-04-20 14:27:50 +02001684 list_del_init(&cfqq->cfq_list);
1685 list_add(&cfqq->cfq_list, &cfqd->cur_rr);
Jens Axboebf572252006-07-19 20:29:12 +02001686
Jens Axboe44f7c162007-01-19 11:51:58 +11001687 cfqq->slice_end = 0;
1688 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001689}
1690
1691/*
Jens Axboe5e705372006-07-13 12:39:25 +02001692 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001693 * something we should do about it
1694 */
1695static void
Jens Axboe5e705372006-07-13 12:39:25 +02001696cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1697 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001698{
Jens Axboe5e705372006-07-13 12:39:25 +02001699 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001700
Jens Axboe374f84a2006-07-23 01:42:19 +02001701 if (rq_is_meta(rq))
1702 cfqq->meta_pending++;
1703
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001704 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001705 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001706 cfq_update_idle_window(cfqd, cfqq, cic);
1707
Jens Axboe5e705372006-07-13 12:39:25 +02001708 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001709 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001710
1711 if (cfqq == cfqd->active_queue) {
1712 /*
1713 * if we are waiting for a request for this queue, let it rip
1714 * immediately and flag that we must not expire this queue
1715 * just now
1716 */
Jens Axboe3b181522005-06-27 10:56:24 +02001717 if (cfq_cfqq_wait_request(cfqq)) {
1718 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001719 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001720 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001721 }
Jens Axboe5e705372006-07-13 12:39:25 +02001722 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001723 /*
1724 * not the active queue - expire current slice if it is
1725 * idle and has expired it's mean thinktime or this new queue
1726 * has some old slice time left and is of higher priority
1727 */
1728 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001729 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001730 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001731 }
1732}
1733
Jens Axboeb4878f22005-10-20 16:42:29 +02001734static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001735{
Jens Axboeb4878f22005-10-20 16:42:29 +02001736 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001737 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001738
1739 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Jens Axboe5e705372006-07-13 12:39:25 +02001741 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jens Axboe22e2c502005-06-27 10:55:12 +02001743 list_add_tail(&rq->queuelist, &cfqq->fifo);
1744
Jens Axboe5e705372006-07-13 12:39:25 +02001745 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static void cfq_completed_request(request_queue_t *q, struct request *rq)
1749{
Jens Axboe5e705372006-07-13 12:39:25 +02001750 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001751 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001752 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001753 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jens Axboeb4878f22005-10-20 16:42:29 +02001755 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jens Axboeb4878f22005-10-20 16:42:29 +02001757 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001758 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001759 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001760 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jens Axboeb4878f22005-10-20 16:42:29 +02001762 if (!cfq_class_idle(cfqq))
1763 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001764
Jens Axboecaaa5f92006-06-16 11:23:00 +02001765 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001766 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001767
1768 /*
1769 * If this is the active queue, check if it needs to be expired,
1770 * or if we want to idle in case it has no pending requests.
1771 */
1772 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001773 if (cfq_cfqq_slice_new(cfqq)) {
1774 cfq_set_prio_slice(cfqd, cfqq);
1775 cfq_clear_cfqq_slice_new(cfqq);
1776 }
1777 if (cfq_slice_used(cfqq))
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001778 cfq_slice_expired(cfqd, 0, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001779 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1780 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001781 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001782
1783 if (!cfqd->rq_in_driver)
1784 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Jens Axboe22e2c502005-06-27 10:55:12 +02001787/*
1788 * we temporarily boost lower priority queues if they are holding fs exclusive
1789 * resources. they are boosted to normal prio (CLASS_BE/4)
1790 */
1791static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Jens Axboe22e2c502005-06-27 10:55:12 +02001793 if (has_fs_excl()) {
1794 /*
1795 * boost idle prio on transactions that would lock out other
1796 * users of the filesystem
1797 */
1798 if (cfq_class_idle(cfqq))
1799 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1800 if (cfqq->ioprio > IOPRIO_NORM)
1801 cfqq->ioprio = IOPRIO_NORM;
1802 } else {
1803 /*
1804 * check if we need to unboost the queue
1805 */
1806 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1807 cfqq->ioprio_class = cfqq->org_ioprio_class;
1808 if (cfqq->ioprio != cfqq->org_ioprio)
1809 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001811}
1812
Jens Axboe89850f72006-07-22 16:48:31 +02001813static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001814{
Jens Axboe3b181522005-06-27 10:56:24 +02001815 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001816 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001817 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001818 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001819 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001820
1821 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001822}
1823
Jens Axboecb78b282006-07-28 09:32:57 +02001824static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001825{
1826 struct cfq_data *cfqd = q->elevator->elevator_data;
1827 struct task_struct *tsk = current;
1828 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001829 unsigned int key;
1830
1831 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001832
1833 /*
1834 * don't force setup of a queue from here, as a call to may_queue
1835 * does not necessarily imply that a request actually will be queued.
1836 * so just lookup a possibly existing queue, or return 'may queue'
1837 * if that fails
1838 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001839 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001840 if (cfqq) {
1841 cfq_init_prio_data(cfqq);
1842 cfq_prio_boost(cfqq);
1843
Jens Axboe89850f72006-07-22 16:48:31 +02001844 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001845 }
1846
1847 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/*
1851 * queue lock held here
1852 */
Jens Axboebb37b942006-12-01 10:42:33 +01001853static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Jens Axboe5e705372006-07-13 12:39:25 +02001855 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jens Axboe5e705372006-07-13 12:39:25 +02001857 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jens Axboe22e2c502005-06-27 10:55:12 +02001860 BUG_ON(!cfqq->allocated[rw]);
1861 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jens Axboe5e705372006-07-13 12:39:25 +02001863 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001866 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 cfq_put_queue(cfqq);
1869 }
1870}
1871
1872/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001873 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001875static int
Jens Axboecb78b282006-07-28 09:32:57 +02001876cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001879 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct cfq_io_context *cic;
1881 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001882 const int is_sync = rq_is_sync(rq);
1883 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001884 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned long flags;
1886
1887 might_sleep_if(gfp_mask & __GFP_WAIT);
1888
Jens Axboee2d74ac2006-03-28 08:59:01 +02001889 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 spin_lock_irqsave(q->queue_lock, flags);
1892
Jens Axboe22e2c502005-06-27 10:55:12 +02001893 if (!cic)
1894 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Al Viro12a05732006-03-18 13:38:01 -05001896 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001897 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001898 if (!cfqq)
1899 goto queue_fail;
1900
Al Viro12a05732006-03-18 13:38:01 -05001901 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001902 } else
Al Viro12a05732006-03-18 13:38:01 -05001903 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001906 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001907 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 spin_unlock_irqrestore(q->queue_lock, flags);
1910
Jens Axboe5e705372006-07-13 12:39:25 +02001911 rq->elevator_private = cic;
1912 rq->elevator_private2 = cfqq;
1913 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001914
Jens Axboe22e2c502005-06-27 10:55:12 +02001915queue_fail:
1916 if (cic)
1917 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001918
Jens Axboe3b181522005-06-27 10:56:24 +02001919 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 spin_unlock_irqrestore(q->queue_lock, flags);
1921 return 1;
1922}
1923
David Howells65f27f32006-11-22 14:55:48 +00001924static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001925{
David Howells65f27f32006-11-22 14:55:48 +00001926 struct cfq_data *cfqd =
1927 container_of(work, struct cfq_data, unplug_work);
1928 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001929 unsigned long flags;
1930
1931 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001932 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001933 spin_unlock_irqrestore(q->queue_lock, flags);
1934}
1935
1936/*
1937 * Timer running if the active_queue is currently idling inside its time slice
1938 */
1939static void cfq_idle_slice_timer(unsigned long data)
1940{
1941 struct cfq_data *cfqd = (struct cfq_data *) data;
1942 struct cfq_queue *cfqq;
1943 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001944 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001945
1946 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1947
1948 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001949 timed_out = 0;
1950
Jens Axboe22e2c502005-06-27 10:55:12 +02001951 /*
1952 * expired
1953 */
Jens Axboe44f7c162007-01-19 11:51:58 +11001954 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001955 goto expire;
1956
1957 /*
1958 * only expire and reinvoke request handler, if there are
1959 * other queues with pending requests
1960 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001961 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001962 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001963
1964 /*
1965 * not expired and it has a request pending, let it dispatch
1966 */
Jens Axboedd67d052006-06-21 09:36:18 +02001967 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001968 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001969 goto out_kick;
1970 }
1971 }
1972expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001973 cfq_slice_expired(cfqd, 0, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02001974out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001975 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001976out_cont:
1977 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1978}
1979
1980/*
1981 * Timer running if an idle class queue is waiting for service
1982 */
1983static void cfq_idle_class_timer(unsigned long data)
1984{
1985 struct cfq_data *cfqd = (struct cfq_data *) data;
1986 unsigned long flags, end;
1987
1988 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1989
1990 /*
1991 * race with a non-idle queue, reset timer
1992 */
1993 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02001994 if (!time_after_eq(jiffies, end))
1995 mod_timer(&cfqd->idle_class_timer, end);
1996 else
Jens Axboe3b181522005-06-27 10:56:24 +02001997 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001998
1999 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2000}
2001
Jens Axboe3b181522005-06-27 10:56:24 +02002002static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2003{
2004 del_timer_sync(&cfqd->idle_slice_timer);
2005 del_timer_sync(&cfqd->idle_class_timer);
2006 blk_sync_queue(cfqd->queue);
2007}
Jens Axboe22e2c502005-06-27 10:55:12 +02002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static void cfq_exit_queue(elevator_t *e)
2010{
Jens Axboe22e2c502005-06-27 10:55:12 +02002011 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002012 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002013
Jens Axboe3b181522005-06-27 10:56:24 +02002014 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002015
Al Virod9ff4182006-03-18 13:51:22 -05002016 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002017
Al Virod9ff4182006-03-18 13:51:22 -05002018 if (cfqd->active_queue)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002019 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002020
2021 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002022 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2023 struct cfq_io_context,
2024 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002025
2026 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002027 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002028
Al Virod9ff4182006-03-18 13:51:22 -05002029 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002030
2031 cfq_shutdown_timer_wq(cfqd);
2032
Al Viroa90d7422006-03-18 12:05:37 -05002033 kfree(cfqd->cfq_hash);
2034 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Jens Axboebb37b942006-12-01 10:42:33 +01002037static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 struct cfq_data *cfqd;
2040 int i;
2041
Jens Axboeb5deef92006-07-19 23:39:40 +02002042 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002044 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002047
Jens Axboecc09e292007-04-26 12:53:50 +02002048 cfqd->service_tree = CFQ_RB_ROOT;
Jens Axboe22e2c502005-06-27 10:55:12 +02002049 INIT_LIST_HEAD(&cfqd->cur_rr);
2050 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05002051 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Jens Axboeb5deef92006-07-19 23:39:40 +02002053 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002055 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2058 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Jens Axboe22e2c502005-06-27 10:55:12 +02002062 init_timer(&cfqd->idle_slice_timer);
2063 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2064 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2065
2066 init_timer(&cfqd->idle_class_timer);
2067 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2068 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2069
David Howells65f27f32006-11-22 14:55:48 +00002070 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002073 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2074 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 cfqd->cfq_back_max = cfq_back_max;
2076 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002077 cfqd->cfq_slice[0] = cfq_slice_async;
2078 cfqd->cfq_slice[1] = cfq_slice_sync;
2079 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2080 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002081
Jens Axboebc1c1162006-06-08 08:49:06 +02002082 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002083out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002085 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088static void cfq_slab_kill(void)
2089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (cfq_pool)
2091 kmem_cache_destroy(cfq_pool);
2092 if (cfq_ioc_pool)
2093 kmem_cache_destroy(cfq_ioc_pool);
2094}
2095
2096static int __init cfq_slab_setup(void)
2097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2099 NULL, NULL);
2100 if (!cfq_pool)
2101 goto fail;
2102
2103 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2104 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2105 if (!cfq_ioc_pool)
2106 goto fail;
2107
2108 return 0;
2109fail:
2110 cfq_slab_kill();
2111 return -ENOMEM;
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/*
2115 * sysfs parts below -->
2116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117static ssize_t
2118cfq_var_show(unsigned int var, char *page)
2119{
2120 return sprintf(page, "%d\n", var);
2121}
2122
2123static ssize_t
2124cfq_var_store(unsigned int *var, const char *page, size_t count)
2125{
2126 char *p = (char *) page;
2127
2128 *var = simple_strtoul(p, &p, 10);
2129 return count;
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002133static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002135 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 unsigned int __data = __VAR; \
2137 if (__CONV) \
2138 __data = jiffies_to_msecs(__data); \
2139 return cfq_var_show(__data, (page)); \
2140}
2141SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002142SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2143SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002144SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2145SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002146SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2147SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2148SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2149SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#undef SHOW_FUNCTION
2151
2152#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002153static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002155 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unsigned int __data; \
2157 int ret = cfq_var_store(&__data, (page), count); \
2158 if (__data < (MIN)) \
2159 __data = (MIN); \
2160 else if (__data > (MAX)) \
2161 __data = (MAX); \
2162 if (__CONV) \
2163 *(__PTR) = msecs_to_jiffies(__data); \
2164 else \
2165 *(__PTR) = __data; \
2166 return ret; \
2167}
2168STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002169STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2170STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002171STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2172STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002173STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2174STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2175STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2176STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#undef STORE_FUNCTION
2178
Al Viroe572ec72006-03-18 22:27:18 -05002179#define CFQ_ATTR(name) \
2180 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002181
Al Viroe572ec72006-03-18 22:27:18 -05002182static struct elv_fs_entry cfq_attrs[] = {
2183 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002184 CFQ_ATTR(fifo_expire_sync),
2185 CFQ_ATTR(fifo_expire_async),
2186 CFQ_ATTR(back_seek_max),
2187 CFQ_ATTR(back_seek_penalty),
2188 CFQ_ATTR(slice_sync),
2189 CFQ_ATTR(slice_async),
2190 CFQ_ATTR(slice_async_rq),
2191 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002192 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193};
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195static struct elevator_type iosched_cfq = {
2196 .ops = {
2197 .elevator_merge_fn = cfq_merge,
2198 .elevator_merged_fn = cfq_merged_request,
2199 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002200 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002201 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002203 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .elevator_deactivate_req_fn = cfq_deactivate_request,
2205 .elevator_queue_empty_fn = cfq_queue_empty,
2206 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002207 .elevator_former_req_fn = elv_rb_former_request,
2208 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 .elevator_set_req_fn = cfq_set_request,
2210 .elevator_put_req_fn = cfq_put_request,
2211 .elevator_may_queue_fn = cfq_may_queue,
2212 .elevator_init_fn = cfq_init_queue,
2213 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002214 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 },
Al Viro3d1ab402006-03-18 18:35:43 -05002216 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 .elevator_name = "cfq",
2218 .elevator_owner = THIS_MODULE,
2219};
2220
2221static int __init cfq_init(void)
2222{
2223 int ret;
2224
Jens Axboe22e2c502005-06-27 10:55:12 +02002225 /*
2226 * could be 0 on HZ < 1000 setups
2227 */
2228 if (!cfq_slice_async)
2229 cfq_slice_async = 1;
2230 if (!cfq_slice_idle)
2231 cfq_slice_idle = 1;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (cfq_slab_setup())
2234 return -ENOMEM;
2235
2236 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002237 if (ret)
2238 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return ret;
2241}
2242
2243static void __exit cfq_exit(void)
2244{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002245 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002247 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002248 /* ioc_gone's update must be visible before reading ioc_count */
2249 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002250 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002251 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002252 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002253 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256module_init(cfq_init);
2257module_exit(cfq_exit);
2258
2259MODULE_AUTHOR("Jens Axboe");
2260MODULE_LICENSE("GPL");
2261MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");