blob: 29284fa06e6b2adc7097d6f951495be8cdf5dd09 [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;
Jens Axboe22e2c502005-06-27 10:55:12 +020095 unsigned int busy_queues;
96
97 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020098 * cfqq lookup hash
99 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jens Axboe22e2c502005-06-27 10:55:12 +0200102 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +0200103 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200104
105 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200106 * idle window management
107 */
108 struct timer_list idle_slice_timer;
109 struct work_struct unplug_work;
110
111 struct cfq_queue *active_queue;
112 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200113 unsigned int dispatch_slice;
114
115 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jens Axboe6d048f52007-04-25 12:44:27 +0200117 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200118 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
121 * tunables, see top of file
122 */
123 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200124 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned int cfq_back_penalty;
126 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200127 unsigned int cfq_slice[2];
128 unsigned int cfq_slice_async_rq;
129 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500130
131 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200132
133 sector_t new_seek_mean;
134 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Jens Axboe22e2c502005-06-27 10:55:12 +0200137/*
138 * Per process-grouping structure
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct cfq_queue {
141 /* reference count */
142 atomic_t ref;
143 /* parent cfq_data */
144 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct hlist_node cfq_hash;
147 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200149 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct list_head cfq_list;
Jens Axboed9e76202007-04-20 14:27:50 +0200151 /* service_tree member */
152 struct rb_node rb_node;
153 /* service_tree key */
154 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* sorted list of pending requests */
156 struct rb_root sort_list;
157 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200158 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* requests queued in sort_list */
160 int queued[2];
161 /* currently allocated requests */
162 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200163 /* pending metadata requests */
164 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jens Axboe22e2c502005-06-27 10:55:12 +0200168 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100169 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jens Axboe6d048f52007-04-25 12:44:27 +0200171 /* number of requests that are on the dispatch list or inside driver */
172 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200173
174 /* io prio of this group */
175 unsigned short ioprio, org_ioprio;
176 unsigned short ioprio_class, org_ioprio_class;
177
Jens Axboe3b181522005-06-27 10:56:24 +0200178 /* various state flags, see below */
179 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200180
181 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Jens Axboe3b181522005-06-27 10:56:24 +0200184enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100185 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
186 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
187 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
188 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
189 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
190 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
191 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
192 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
193 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100194 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200195};
196
197#define CFQ_CFQQ_FNS(name) \
198static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
199{ \
200 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
201} \
202static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
203{ \
204 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
205} \
206static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
207{ \
208 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
209}
210
211CFQ_CFQQ_FNS(on_rr);
212CFQ_CFQQ_FNS(wait_request);
213CFQ_CFQQ_FNS(must_alloc);
214CFQ_CFQQ_FNS(must_alloc_slice);
215CFQ_CFQQ_FNS(must_dispatch);
216CFQ_CFQQ_FNS(fifo_expire);
217CFQ_CFQQ_FNS(idle_window);
218CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b037442006-07-28 09:48:51 +0200219CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100220CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200221#undef CFQ_CFQQ_FNS
222
Jens Axboe3b181522005-06-27 10:56:24 +0200223static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200224static void cfq_dispatch_insert(request_queue_t *, struct request *);
Jens Axboe498d3aa22007-04-26 12:54:48 +0200225static struct cfq_queue *cfq_get_queue(struct cfq_data *, unsigned int, struct task_struct *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700228 * scheduler run of queue, if there are requests pending and no one in the
229 * driver that will restart queueing
230 */
231static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
232{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100233 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700234 kblockd_schedule_work(&cfqd->unplug_work);
235}
236
237static int cfq_queue_empty(request_queue_t *q)
238{
239 struct cfq_data *cfqd = q->elevator->elevator_data;
240
Jens Axboeb4878f22005-10-20 16:42:29 +0200241 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700242}
243
Jens Axboe7749a8d2006-12-13 13:02:26 +0100244static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200245{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100246 /*
247 * Use the per-process queue, for read requests and syncronous writes
248 */
249 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200250 return task->pid;
251
252 return CFQ_KEY_ASYNC;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100256 * Scale schedule slice based on io priority. Use the sync time slice only
257 * if a queue is marked sync and has sync io queued. A sync queue with async
258 * io only, should not get full sync slice length.
259 */
Jens Axboed9e76202007-04-20 14:27:50 +0200260static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
261 unsigned short prio)
262{
263 const int base_slice = cfqd->cfq_slice[sync];
264
265 WARN_ON(prio >= IOPRIO_BE_NR);
266
267 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
268}
269
Jens Axboe44f7c162007-01-19 11:51:58 +1100270static inline int
271cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
272{
Jens Axboed9e76202007-04-20 14:27:50 +0200273 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100274}
275
276static inline void
277cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
278{
279 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
280}
281
282/*
283 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
284 * isn't valid until the first request from the dispatch is activated
285 * and the slice time set.
286 */
287static inline int cfq_slice_used(struct cfq_queue *cfqq)
288{
289 if (cfq_cfqq_slice_new(cfqq))
290 return 0;
291 if (time_before(jiffies, cfqq->slice_end))
292 return 0;
293
294 return 1;
295}
296
297/*
Jens Axboe5e705372006-07-13 12:39:25 +0200298 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200300 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Jens Axboe5e705372006-07-13 12:39:25 +0200302static struct request *
303cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200307#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
308#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
309 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Jens Axboe5e705372006-07-13 12:39:25 +0200311 if (rq1 == NULL || rq1 == rq2)
312 return rq2;
313 if (rq2 == NULL)
314 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200315
Jens Axboe5e705372006-07-13 12:39:25 +0200316 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
317 return rq1;
318 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
319 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200320 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
321 return rq1;
322 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
323 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jens Axboe5e705372006-07-13 12:39:25 +0200325 s1 = rq1->sector;
326 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jens Axboe6d048f52007-04-25 12:44:27 +0200328 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * by definition, 1KiB is 2 sectors
332 */
333 back_max = cfqd->cfq_back_max * 2;
334
335 /*
336 * Strict one way elevator _except_ in the case where we allow
337 * short backward seeks which are biased as twice the cost of a
338 * similar forward seek.
339 */
340 if (s1 >= last)
341 d1 = s1 - last;
342 else if (s1 + back_max >= last)
343 d1 = (last - s1) * cfqd->cfq_back_penalty;
344 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200345 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (s2 >= last)
348 d2 = s2 - last;
349 else if (s2 + back_max >= last)
350 d2 = (last - s2) * cfqd->cfq_back_penalty;
351 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200352 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Andreas Mohre8a99052006-03-28 08:59:49 +0200356 /*
357 * By doing switch() on the bit mask "wrap" we avoid having to
358 * check two variables for all permutations: --> faster!
359 */
360 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200361 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200362 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200363 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200364 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200365 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200366 else {
367 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200368 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200369 else
Jens Axboe5e705372006-07-13 12:39:25 +0200370 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200371 }
372
373 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200374 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200375 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200376 return rq2;
377 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200378 default:
379 /*
380 * Since both rqs are wrapped,
381 * start with the one that's further behind head
382 * (--> only *one* back seek required),
383 * since back seek takes more time than forward.
384 */
385 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200386 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 else
Jens Axboe5e705372006-07-13 12:39:25 +0200388 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390}
391
Jens Axboe498d3aa22007-04-26 12:54:48 +0200392/*
393 * The below is leftmost cache rbtree addon
394 */
Jens Axboecc09e292007-04-26 12:53:50 +0200395static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
396{
397 if (!root->left)
398 root->left = rb_first(&root->rb);
399
400 return root->left;
401}
402
403static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
404{
405 if (root->left == n)
406 root->left = NULL;
407
408 rb_erase(n, &root->rb);
409 RB_CLEAR_NODE(n);
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * would be nice to take fifo expire time into account as well
414 */
Jens Axboe5e705372006-07-13 12:39:25 +0200415static struct request *
416cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
417 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Jens Axboe21183b02006-07-13 12:33:14 +0200419 struct rb_node *rbnext = rb_next(&last->rb_node);
420 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200421 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jens Axboe21183b02006-07-13 12:33:14 +0200423 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200426 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200429 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200430 else {
431 rbnext = rb_first(&cfqq->sort_list);
432 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200433 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jens Axboe21183b02006-07-13 12:33:14 +0200436 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Jens Axboed9e76202007-04-20 14:27:50 +0200439static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
440 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Jens Axboed9e76202007-04-20 14:27:50 +0200442 /*
443 * just an approximation, should be ok.
444 */
445 return ((cfqd->busy_queues - 1) * cfq_prio_slice(cfqd, 1, 0));
446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jens Axboe498d3aa22007-04-26 12:54:48 +0200448/*
449 * The cfqd->service_tree holds all pending cfq_queue's that have
450 * requests waiting to be processed. It is sorted in the order that
451 * we will service the queues.
452 */
Jens Axboed9e76202007-04-20 14:27:50 +0200453static void cfq_service_tree_add(struct cfq_data *cfqd,
454 struct cfq_queue *cfqq)
455{
Jens Axboecc09e292007-04-26 12:53:50 +0200456 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200457 struct rb_node *parent = NULL;
Jens Axboed9e76202007-04-20 14:27:50 +0200458 unsigned long rb_key;
Jens Axboe498d3aa22007-04-26 12:54:48 +0200459 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200460
461 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
462 rb_key += cfqq->slice_resid;
463 cfqq->slice_resid = 0;
464
465 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100466 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200467 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100468 */
Jens Axboed9e76202007-04-20 14:27:50 +0200469 if (rb_key == cfqq->rb_key)
470 return;
Jens Axboe53b037442006-07-28 09:48:51 +0200471
Jens Axboecc09e292007-04-26 12:53:50 +0200472 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200473 }
Jens Axboed9e76202007-04-20 14:27:50 +0200474
Jens Axboe498d3aa22007-04-26 12:54:48 +0200475 left = 1;
Jens Axboed9e76202007-04-20 14:27:50 +0200476 while (*p) {
Jens Axboecc09e292007-04-26 12:53:50 +0200477 struct cfq_queue *__cfqq;
Jens Axboe67060e32007-04-18 20:13:32 +0200478 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200479
Jens Axboed9e76202007-04-20 14:27:50 +0200480 parent = *p;
481 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
482
Jens Axboe0c534e02007-04-18 20:01:57 +0200483 /*
484 * sort RT queues first, we always want to give
Jens Axboe67060e32007-04-18 20:13:32 +0200485 * preference to them. IDLE queues goes to the back.
486 * after that, sort on the next service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200487 */
488 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200489 n = &(*p)->rb_left;
Jens Axboe0c534e02007-04-18 20:01:57 +0200490 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200491 n = &(*p)->rb_right;
492 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
493 n = &(*p)->rb_left;
494 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
495 n = &(*p)->rb_right;
Jens Axboe0c534e02007-04-18 20:01:57 +0200496 else if (rb_key < __cfqq->rb_key)
Jens Axboe67060e32007-04-18 20:13:32 +0200497 n = &(*p)->rb_left;
498 else
499 n = &(*p)->rb_right;
500
501 if (n == &(*p)->rb_right)
Jens Axboecc09e292007-04-26 12:53:50 +0200502 left = 0;
Jens Axboe67060e32007-04-18 20:13:32 +0200503
504 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200505 }
506
Jens Axboecc09e292007-04-26 12:53:50 +0200507 if (left)
508 cfqd->service_tree.left = &cfqq->rb_node;
509
Jens Axboed9e76202007-04-20 14:27:50 +0200510 cfqq->rb_key = rb_key;
511 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200512 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Jens Axboe498d3aa22007-04-26 12:54:48 +0200515/*
516 * Update cfqq's position in the service tree.
517 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200518static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
519{
Jens Axboe6d048f52007-04-25 12:44:27 +0200520 /*
521 * Resorting requires the cfqq to be on the RR list already.
522 */
Jens Axboe498d3aa22007-04-26 12:54:48 +0200523 if (cfq_cfqq_on_rr(cfqq))
524 cfq_service_tree_add(cfqq->cfqd, cfqq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200529 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
531static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200532cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Jens Axboe3b181522005-06-27 10:56:24 +0200534 BUG_ON(cfq_cfqq_on_rr(cfqq));
535 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cfqd->busy_queues++;
537
Jens Axboeb4878f22005-10-20 16:42:29 +0200538 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Jens Axboe498d3aa22007-04-26 12:54:48 +0200541/*
542 * Called when the cfqq no longer has requests pending, remove it from
543 * the service tree.
544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static inline void
546cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
547{
Jens Axboe3b181522005-06-27 10:56:24 +0200548 BUG_ON(!cfq_cfqq_on_rr(cfqq));
549 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200550 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jens Axboecc09e292007-04-26 12:53:50 +0200552 if (!RB_EMPTY_NODE(&cfqq->rb_node))
553 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 BUG_ON(!cfqd->busy_queues);
556 cfqd->busy_queues--;
557}
558
559/*
560 * rb tree support functions
561 */
Jens Axboe5e705372006-07-13 12:39:25 +0200562static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jens Axboe5e705372006-07-13 12:39:25 +0200564 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200565 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200566 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jens Axboeb4878f22005-10-20 16:42:29 +0200568 BUG_ON(!cfqq->queued[sync]);
569 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jens Axboe5e705372006-07-13 12:39:25 +0200571 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jens Axboedd67d052006-06-21 09:36:18 +0200573 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200574 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Jens Axboe5e705372006-07-13 12:39:25 +0200577static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jens Axboe5e705372006-07-13 12:39:25 +0200579 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200581 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Jens Axboe5380a102006-07-13 12:37:56 +0200583 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /*
586 * looks a little odd, but the first insert might return an alias.
587 * if that happens, put the alias on the dispatch list
588 */
Jens Axboe21183b02006-07-13 12:33:14 +0200589 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200590 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100591
592 if (!cfq_cfqq_on_rr(cfqq))
593 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200594
595 /*
596 * check if this request is a better next-serve candidate
597 */
598 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
599 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200603cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jens Axboe5380a102006-07-13 12:37:56 +0200605 elv_rb_del(&cfqq->sort_list, rq);
606 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200607 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Jens Axboe206dc692006-03-28 13:03:44 +0200610static struct request *
611cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jens Axboe206dc692006-03-28 13:03:44 +0200613 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100614 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200615 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jens Axboe206dc692006-03-28 13:03:44 +0200617 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200618 if (cfqq) {
619 sector_t sector = bio->bi_sector + bio_sectors(bio);
620
Jens Axboe21183b02006-07-13 12:33:14 +0200621 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return NULL;
625}
626
Jens Axboeb4878f22005-10-20 16:42:29 +0200627static void cfq_activate_request(request_queue_t *q, struct request *rq)
628{
629 struct cfq_data *cfqd = q->elevator->elevator_data;
630
631 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200632
633 /*
634 * If the depth is larger 1, it really could be queueing. But lets
635 * make the mark a little higher - idling could still be good for
636 * low queueing, and a low queueing number could also just indicate
637 * a SCSI mid layer like behaviour where limit+1 is often seen.
638 */
639 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
640 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200641
642 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
646{
Jens Axboe22e2c502005-06-27 10:55:12 +0200647 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jens Axboeb4878f22005-10-20 16:42:29 +0200649 WARN_ON(!cfqd->rq_in_driver);
650 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Jens Axboeb4878f22005-10-20 16:42:29 +0200653static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Jens Axboe5e705372006-07-13 12:39:25 +0200655 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200656
Jens Axboe5e705372006-07-13 12:39:25 +0200657 if (cfqq->next_rq == rq)
658 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Jens Axboeb4878f22005-10-20 16:42:29 +0200660 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200661 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200662
663 if (rq_is_meta(rq)) {
664 WARN_ON(!cfqq->meta_pending);
665 cfqq->meta_pending--;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Jens Axboe498d3aa22007-04-26 12:54:48 +0200669static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct cfq_data *cfqd = q->elevator->elevator_data;
672 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jens Axboe206dc692006-03-28 13:03:44 +0200674 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200675 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200676 *req = __rq;
677 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Jens Axboe21183b02006-07-13 12:33:14 +0200683static void cfq_merged_request(request_queue_t *q, struct request *req,
684 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Jens Axboe21183b02006-07-13 12:33:14 +0200686 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200687 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jens Axboe5e705372006-07-13 12:39:25 +0200689 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static void
694cfq_merged_requests(request_queue_t *q, struct request *rq,
695 struct request *next)
696{
Jens Axboe22e2c502005-06-27 10:55:12 +0200697 /*
698 * reposition in fifo if next is older than rq
699 */
700 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
701 time_before(next->start_time, rq->start_time))
702 list_move(&rq->queuelist, &next->queuelist);
703
Jens Axboeb4878f22005-10-20 16:42:29 +0200704 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200705}
706
Jens Axboeda775262006-12-20 11:04:12 +0100707static int cfq_allow_merge(request_queue_t *q, struct request *rq,
708 struct bio *bio)
709{
710 struct cfq_data *cfqd = q->elevator->elevator_data;
711 const int rw = bio_data_dir(bio);
712 struct cfq_queue *cfqq;
713 pid_t key;
714
715 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100716 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100717 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100718 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100719 return 0;
720
721 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100722 * Lookup the cfqq that this bio will be queued with. Allow
723 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100724 */
Jens Axboe719d3402006-12-22 09:38:53 +0100725 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100726 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100727
728 if (cfqq == RQ_CFQQ(rq))
729 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100730
Jens Axboeec8acb62007-01-02 18:32:11 +0100731 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100732}
733
Jens Axboe22e2c502005-06-27 10:55:12 +0200734static inline void
735__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
736{
737 if (cfqq) {
738 /*
739 * stop potential idle class queues waiting service
740 */
741 del_timer(&cfqd->idle_class_timer);
742
Jens Axboe22e2c502005-06-27 10:55:12 +0200743 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200744 cfq_clear_cfqq_must_alloc_slice(cfqq);
745 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100746 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200747 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200748 }
749
750 cfqd->active_queue = cfqq;
751}
752
753/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100754 * current cfqq expired its slice (or was too idle), select new one
755 */
756static void
757__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100758 int preempted, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100759{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100760 if (cfq_cfqq_wait_request(cfqq))
761 del_timer(&cfqd->idle_slice_timer);
762
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100763 cfq_clear_cfqq_must_dispatch(cfqq);
764 cfq_clear_cfqq_wait_request(cfqq);
765
766 /*
767 * store what was left of this slice, if the queue idled out
768 * or was preempted
769 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100770 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100771 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100772
Jens Axboe98e41c72007-02-05 11:55:35 +0100773 cfq_resort_rr_list(cfqq, preempted);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100774
775 if (cfqq == cfqd->active_queue)
776 cfqd->active_queue = NULL;
777
778 if (cfqd->active_cic) {
779 put_io_context(cfqd->active_cic->ioc);
780 cfqd->active_cic = NULL;
781 }
782
783 cfqd->dispatch_slice = 0;
784}
785
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100786static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
787 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100788{
789 struct cfq_queue *cfqq = cfqd->active_queue;
790
791 if (cfqq)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100792 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100793}
794
Jens Axboe498d3aa22007-04-26 12:54:48 +0200795/*
796 * Get next queue for service. Unless we have a queue preemption,
797 * we'll simply select the first cfqq in the service tree.
798 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200799static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200800{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100801 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200802
Jens Axboed9e76202007-04-20 14:27:50 +0200803 if (!list_empty(&cfqd->cur_rr)) {
Jens Axboe89850f72006-07-22 16:48:31 +0200804 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200805 * if current list is non-empty, grab first entry.
Jens Axboe89850f72006-07-22 16:48:31 +0200806 */
Jens Axboed9e76202007-04-20 14:27:50 +0200807 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
Jens Axboecc09e292007-04-26 12:53:50 +0200808 } else if (!RB_EMPTY_ROOT(&cfqd->service_tree.rb)) {
809 struct rb_node *n = cfq_rb_first(&cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200810
811 cfqq = rb_entry(n, struct cfq_queue, rb_node);
Jens Axboe67060e32007-04-18 20:13:32 +0200812 if (cfq_class_idle(cfqq)) {
Jens Axboe498d3aa22007-04-26 12:54:48 +0200813 unsigned long end;
814
Jens Axboe67060e32007-04-18 20:13:32 +0200815 /*
816 * if we have idle queues and no rt or be queues had
817 * pending requests, either allow immediate service if
818 * the grace period has passed or arm the idle grace
819 * timer
820 */
821 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
822 if (time_before(jiffies, end)) {
823 mod_timer(&cfqd->idle_class_timer, end);
824 cfqq = NULL;
825 }
826 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200827 }
828
Jens Axboe6d048f52007-04-25 12:44:27 +0200829 return cfqq;
830}
831
Jens Axboe498d3aa22007-04-26 12:54:48 +0200832/*
833 * Get and set a new active queue for service.
834 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200835static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
836{
837 struct cfq_queue *cfqq;
838
Jens Axboed9e76202007-04-20 14:27:50 +0200839 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200840 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200841 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200842}
843
Jens Axboed9e76202007-04-20 14:27:50 +0200844static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
845 struct request *rq)
846{
847 if (rq->sector >= cfqd->last_position)
848 return rq->sector - cfqd->last_position;
849 else
850 return cfqd->last_position - rq->sector;
851}
852
Jens Axboe6d048f52007-04-25 12:44:27 +0200853static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
854{
855 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200856
Jens Axboe6d048f52007-04-25 12:44:27 +0200857 if (!sample_valid(cic->seek_samples))
858 return 0;
859
860 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
861}
862
Jens Axboed9e76202007-04-20 14:27:50 +0200863static int cfq_close_cooperator(struct cfq_data *cfq_data,
864 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200865{
Jens Axboe6d048f52007-04-25 12:44:27 +0200866 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200867 * We should notice if some of the queues are cooperating, eg
868 * working closely on the same area of the disk. In that case,
869 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200870 */
Jens Axboed9e76202007-04-20 14:27:50 +0200871 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200872}
873
874#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
875
876static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200877{
Jens Axboe17926692007-01-19 11:59:30 +1100878 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200879 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100880 unsigned long sl;
881
Jens Axboedd67d052006-06-21 09:36:18 +0200882 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200883 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200884
885 /*
886 * idle is disabled, either manually or by past process history
887 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200888 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
889 return;
890
Jens Axboe22e2c502005-06-27 10:55:12 +0200891 /*
892 * task has exited, don't wait
893 */
Jens Axboe206dc692006-03-28 13:03:44 +0200894 cic = cfqd->active_cic;
895 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200896 return;
897
898 /*
899 * See if this prio level has a good candidate
900 */
Jens Axboe1afba042007-04-17 12:47:55 +0200901 if (cfq_close_cooperator(cfqd, cfqq) &&
902 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200903 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200904
Jens Axboe3b181522005-06-27 10:56:24 +0200905 cfq_mark_cfqq_must_dispatch(cfqq);
906 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200907
Jens Axboe206dc692006-03-28 13:03:44 +0200908 /*
909 * we don't want to idle for seeks, but we do want to allow
910 * fair distribution of slice time for a process doing back-to-back
911 * seeks. so allow a little bit of time for him to submit a new rq
912 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200913 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200914 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200915 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200916
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100917 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Jens Axboe498d3aa22007-04-26 12:54:48 +0200920/*
921 * Move request from internal lists to the request queue dispatch list.
922 */
Jens Axboe5e705372006-07-13 12:39:25 +0200923static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jens Axboe5e705372006-07-13 12:39:25 +0200925 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200926
Jens Axboe5380a102006-07-13 12:37:56 +0200927 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200928 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200929 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/*
933 * return expired entry, or NULL to just start from scratch in rbtree
934 */
Jens Axboe5e705372006-07-13 12:39:25 +0200935static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200938 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200939 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Jens Axboe3b181522005-06-27 10:56:24 +0200941 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100943
944 cfq_mark_cfqq_fifo_expire(cfqq);
945
Jens Axboe89850f72006-07-22 16:48:31 +0200946 if (list_empty(&cfqq->fifo))
947 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jens Axboe6d048f52007-04-25 12:44:27 +0200949 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200950 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jens Axboe6d048f52007-04-25 12:44:27 +0200952 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
953 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Jens Axboe6d048f52007-04-25 12:44:27 +0200955 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Jens Axboe22e2c502005-06-27 10:55:12 +0200958static inline int
959cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
960{
961 const int base_rq = cfqd->cfq_slice_async_rq;
962
963 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
964
965 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
966}
967
968/*
Jens Axboe498d3aa22007-04-26 12:54:48 +0200969 * Select a queue for service. If we have a current active queue,
970 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +0200971 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100972static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200973{
Jens Axboe22e2c502005-06-27 10:55:12 +0200974 struct cfq_queue *cfqq;
975
976 cfqq = cfqd->active_queue;
977 if (!cfqq)
978 goto new_queue;
979
980 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200981 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200982 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200983 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200984 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200985
986 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200987 * The active queue has requests and isn't expired, allow it to
988 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200989 */
Jens Axboedd67d052006-06-21 09:36:18 +0200990 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200991 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200992
993 /*
994 * No requests pending. If the active queue still has requests in
995 * flight or is idling for a new request, allow either of these
996 * conditions to happen (or time out) before selecting a new queue.
997 */
998 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +0200999 cfqq = NULL;
1000 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001001 }
1002
Jens Axboe3b181522005-06-27 10:56:24 +02001003expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001004 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001005new_queue:
1006 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001007keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001008 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001009}
1010
Jens Axboe498d3aa22007-04-26 12:54:48 +02001011/*
1012 * Dispatch some requests from cfqq, moving them to the request queue
1013 * dispatch list.
1014 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001015static int
1016__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1017 int max_dispatch)
1018{
1019 int dispatched = 0;
1020
Jens Axboedd67d052006-06-21 09:36:18 +02001021 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001022
1023 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001024 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001025
1026 /*
1027 * follow expired path, else get first next available
1028 */
Jens Axboe5e705372006-07-13 12:39:25 +02001029 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1030 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001031
1032 /*
1033 * finally, insert request into driver dispatch list
1034 */
Jens Axboe5e705372006-07-13 12:39:25 +02001035 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001036
1037 cfqd->dispatch_slice++;
1038 dispatched++;
1039
1040 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001041 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1042 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001043 }
1044
Jens Axboedd67d052006-06-21 09:36:18 +02001045 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001046 break;
1047
1048 } while (dispatched < max_dispatch);
1049
1050 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001051 * expire an async queue immediately if it has used up its slice. idle
1052 * queue always expire after 1 dispatch round.
1053 */
Jens Axboea9938002007-04-20 08:55:52 +02001054 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001055 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001056 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001057 cfqq->slice_end = jiffies + 1;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001058 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001059 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001060
1061 return dispatched;
1062}
1063
Jens Axboed9e76202007-04-20 14:27:50 +02001064static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1065{
1066 int dispatched = 0;
1067
1068 while (cfqq->next_rq) {
1069 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1070 dispatched++;
1071 }
1072
1073 BUG_ON(!list_empty(&cfqq->fifo));
1074 return dispatched;
1075}
1076
1077static int cfq_forced_dispatch_cfqqs(struct list_head *list)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001078{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001079 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001080 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001081
Jens Axboecaaa5f92006-06-16 11:23:00 +02001082 dispatched = 0;
Jens Axboed9e76202007-04-20 14:27:50 +02001083 list_for_each_entry_safe(cfqq, next, list, cfq_list)
1084 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001085
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001086 return dispatched;
1087}
1088
Jens Axboe498d3aa22007-04-26 12:54:48 +02001089/*
1090 * Drain our current requests. Used for barriers and when switching
1091 * io schedulers on-the-fly.
1092 */
Jens Axboed9e76202007-04-20 14:27:50 +02001093static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001094{
Jens Axboed9e76202007-04-20 14:27:50 +02001095 int dispatched = 0;
1096 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001097
Jens Axboecc09e292007-04-26 12:53:50 +02001098 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
Jens Axboed9e76202007-04-20 14:27:50 +02001099 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1100
1101 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1102 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001103
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001104 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001105
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001106 cfq_slice_expired(cfqd, 0, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001107
1108 BUG_ON(cfqd->busy_queues);
1109
1110 return dispatched;
1111}
1112
Jens Axboed9e76202007-04-20 14:27:50 +02001113static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001116 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001117 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Jens Axboe22e2c502005-06-27 10:55:12 +02001119 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return 0;
1121
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001122 if (unlikely(force))
1123 return cfq_forced_dispatch(cfqd);
1124
Jens Axboecaaa5f92006-06-16 11:23:00 +02001125 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001126 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001127 int max_dispatch;
1128
Jens Axboea9938002007-04-20 08:55:52 +02001129 if (cfqd->busy_queues > 1) {
1130 /*
Jens Axboea9938002007-04-20 08:55:52 +02001131 * So we have dispatched before in this round, if the
1132 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001133 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001134 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001135 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1136 dispatched)
1137 break;
1138 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001139 break;
1140 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001141
Jens Axboe3b181522005-06-27 10:56:24 +02001142 cfq_clear_cfqq_must_dispatch(cfqq);
1143 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001144 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001146 max_dispatch = cfqd->cfq_quantum;
1147 if (cfq_class_idle(cfqq))
1148 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jens Axboecaaa5f92006-06-16 11:23:00 +02001150 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
Jens Axboecaaa5f92006-06-16 11:23:00 +02001153 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
Jens Axboe5e705372006-07-13 12:39:25 +02001157 * task holds one reference to the queue, dropped when task exits. each rq
1158 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *
1160 * queue lock must be held here.
1161 */
1162static void cfq_put_queue(struct cfq_queue *cfqq)
1163{
Jens Axboe22e2c502005-06-27 10:55:12 +02001164 struct cfq_data *cfqd = cfqq->cfqd;
1165
1166 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!atomic_dec_and_test(&cfqq->ref))
1169 return;
1170
1171 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001172 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001173 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001175 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001176 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001177 cfq_schedule_dispatch(cfqd);
1178 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /*
1181 * it's on the empty list and still hashed
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 hlist_del(&cfqq->cfq_hash);
1184 kmem_cache_free(cfq_pool, cfqq);
1185}
1186
Jens Axboe1ea25ecb2006-07-18 22:24:11 +02001187static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001188__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1189 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001192 struct hlist_node *entry;
1193 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jens Axboe206dc692006-03-28 13:03:44 +02001195 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001196 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jens Axboe206dc692006-03-28 13:03:44 +02001198 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return __cfqq;
1200 }
1201
1202 return NULL;
1203}
1204
1205static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001206cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Jens Axboe3b181522005-06-27 10:56:24 +02001208 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Jens Axboee2d74ac2006-03-28 08:59:01 +02001211static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Jens Axboe22e2c502005-06-27 10:55:12 +02001213 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001214 struct rb_node *n;
1215 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001216
Jens Axboee2d74ac2006-03-28 08:59:01 +02001217 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1218 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1219 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001220 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001221 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001222 }
1223
Jens Axboe4050cf12006-07-19 05:07:12 +02001224 elv_ioc_count_mod(ioc_count, -freed);
1225
1226 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001227 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Jens Axboe89850f72006-07-22 16:48:31 +02001230static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1231{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001232 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001233 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001234 cfq_schedule_dispatch(cfqd);
1235 }
Jens Axboe89850f72006-07-22 16:48:31 +02001236
1237 cfq_put_queue(cfqq);
1238}
1239
1240static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1241 struct cfq_io_context *cic)
1242{
Jens Axboefc463792006-08-29 09:05:44 +02001243 list_del_init(&cic->queue_list);
1244 smp_wmb();
1245 cic->key = NULL;
1246
Jens Axboe89850f72006-07-22 16:48:31 +02001247 if (cic->cfqq[ASYNC]) {
1248 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1249 cic->cfqq[ASYNC] = NULL;
1250 }
1251
1252 if (cic->cfqq[SYNC]) {
1253 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1254 cic->cfqq[SYNC] = NULL;
1255 }
Jens Axboe89850f72006-07-22 16:48:31 +02001256}
1257
Jens Axboe22e2c502005-06-27 10:55:12 +02001258static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1259{
Al Viro478a82b2006-03-18 13:25:24 -05001260 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001261
Jens Axboe89850f72006-07-22 16:48:31 +02001262 if (cfqd) {
1263 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001264
Jens Axboefc463792006-08-29 09:05:44 +02001265 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001266 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001267 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001268 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001269}
1270
Jens Axboe498d3aa22007-04-26 12:54:48 +02001271/*
1272 * The process that ioc belongs to has exited, we need to clean up
1273 * and put the internal structures we have that belongs to that process.
1274 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001275static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Jens Axboe22e2c502005-06-27 10:55:12 +02001277 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001278 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * put the reference this task is holding to the various queues
1282 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001283
1284 n = rb_first(&ioc->cic_root);
1285 while (n != NULL) {
1286 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1287
Jens Axboe22e2c502005-06-27 10:55:12 +02001288 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001289 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Jens Axboe22e2c502005-06-27 10:55:12 +02001293static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001294cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Jens Axboeb5deef92006-07-19 23:39:40 +02001296 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jens Axboeb5deef92006-07-19 23:39:40 +02001298 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001300 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001301 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001302 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001303 cic->dtor = cfq_free_io_context;
1304 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001305 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 return cic;
1309}
1310
Jens Axboe22e2c502005-06-27 10:55:12 +02001311static void cfq_init_prio_data(struct cfq_queue *cfqq)
1312{
1313 struct task_struct *tsk = current;
1314 int ioprio_class;
1315
Jens Axboe3b181522005-06-27 10:56:24 +02001316 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001317 return;
1318
1319 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1320 switch (ioprio_class) {
1321 default:
1322 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1323 case IOPRIO_CLASS_NONE:
1324 /*
1325 * no prio set, place us in the middle of the BE classes
1326 */
1327 cfqq->ioprio = task_nice_ioprio(tsk);
1328 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1329 break;
1330 case IOPRIO_CLASS_RT:
1331 cfqq->ioprio = task_ioprio(tsk);
1332 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1333 break;
1334 case IOPRIO_CLASS_BE:
1335 cfqq->ioprio = task_ioprio(tsk);
1336 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1337 break;
1338 case IOPRIO_CLASS_IDLE:
1339 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1340 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001341 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001342 break;
1343 }
1344
1345 /*
1346 * keep track of original prio settings in case we have to temporarily
1347 * elevate the priority of this queue
1348 */
1349 cfqq->org_ioprio = cfqq->ioprio;
1350 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001351 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001352}
1353
Al Viro478a82b2006-03-18 13:25:24 -05001354static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001355{
Al Viro478a82b2006-03-18 13:25:24 -05001356 struct cfq_data *cfqd = cic->key;
1357 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001358 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001359
Jens Axboecaaa5f92006-06-16 11:23:00 +02001360 if (unlikely(!cfqd))
1361 return;
1362
Jens Axboec1b707d2006-10-30 19:54:23 +01001363 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001364
1365 cfqq = cic->cfqq[ASYNC];
1366 if (cfqq) {
1367 struct cfq_queue *new_cfqq;
1368 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1369 GFP_ATOMIC);
1370 if (new_cfqq) {
1371 cic->cfqq[ASYNC] = new_cfqq;
1372 cfq_put_queue(cfqq);
1373 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001374 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001375
1376 cfqq = cic->cfqq[SYNC];
1377 if (cfqq)
1378 cfq_mark_cfqq_prio_changed(cfqq);
1379
Jens Axboec1b707d2006-10-30 19:54:23 +01001380 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001381}
1382
Jens Axboefc463792006-08-29 09:05:44 +02001383static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Al Viroa6a07632006-03-18 13:26:44 -05001385 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001386 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001387
Jens Axboefc463792006-08-29 09:05:44 +02001388 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001389
Jens Axboee2d74ac2006-03-28 08:59:01 +02001390 n = rb_first(&ioc->cic_root);
1391 while (n != NULL) {
1392 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001393
Al Viro478a82b2006-03-18 13:25:24 -05001394 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001395 n = rb_next(n);
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001400cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001401 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1404 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001405 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407retry:
Al Viro6f325a12006-03-18 14:58:37 -05001408 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001409 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (!cfqq) {
1412 if (new_cfqq) {
1413 cfqq = new_cfqq;
1414 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001415 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001416 /*
1417 * Inform the allocator of the fact that we will
1418 * just repeat this allocation if it fails, to allow
1419 * the allocator to do whatever it needs to attempt to
1420 * free memory.
1421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001423 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_lock_irq(cfqd->queue->queue_lock);
1425 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001426 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001427 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001428 if (!cfqq)
1429 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 memset(cfqq, 0, sizeof(*cfqq));
1433
1434 INIT_HLIST_NODE(&cfqq->cfq_hash);
1435 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboed9e76202007-04-20 14:27:50 +02001436 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001437 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 cfqq->key = key;
1440 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1441 atomic_set(&cfqq->ref, 0);
1442 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001443
Jens Axboea9938002007-04-20 08:55:52 +02001444 if (key != CFQ_KEY_ASYNC)
1445 cfq_mark_cfqq_idle_window(cfqq);
1446
Jens Axboe3b181522005-06-27 10:56:24 +02001447 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b037442006-07-28 09:48:51 +02001448 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001449 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
1452 if (new_cfqq)
1453 kmem_cache_free(cfq_pool, new_cfqq);
1454
1455 atomic_inc(&cfqq->ref);
1456out:
1457 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1458 return cfqq;
1459}
1460
Jens Axboe498d3aa22007-04-26 12:54:48 +02001461/*
1462 * We drop cfq io contexts lazily, so we may find a dead one.
1463 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001464static void
1465cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1466{
Jens Axboefc463792006-08-29 09:05:44 +02001467 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001468 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001469 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001470 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001471}
1472
Jens Axboee2d74ac2006-03-28 08:59:01 +02001473static struct cfq_io_context *
1474cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1475{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001476 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001477 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001478 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001479
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001480restart:
1481 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001482 while (n) {
1483 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001484 /* ->key must be copied to avoid race with cfq_exit_queue() */
1485 k = cic->key;
1486 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001487 cfq_drop_dead_cic(ioc, cic);
1488 goto restart;
1489 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001490
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001491 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001492 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001493 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001494 n = n->rb_right;
1495 else
1496 return cic;
1497 }
1498
1499 return NULL;
1500}
1501
1502static inline void
1503cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1504 struct cfq_io_context *cic)
1505{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001506 struct rb_node **p;
1507 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001508 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001509 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001510 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001511
Jens Axboee2d74ac2006-03-28 08:59:01 +02001512 cic->ioc = ioc;
1513 cic->key = cfqd;
1514
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001515restart:
1516 parent = NULL;
1517 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001518 while (*p) {
1519 parent = *p;
1520 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001521 /* ->key must be copied to avoid race with cfq_exit_queue() */
1522 k = __cic->key;
1523 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001524 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001525 goto restart;
1526 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001527
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001528 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001529 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001530 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001531 p = &(*p)->rb_right;
1532 else
1533 BUG();
1534 }
1535
1536 rb_link_node(&cic->rb_node, parent, p);
1537 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001538
Jens Axboe0261d682006-10-30 19:07:48 +01001539 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001540 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001541 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001542}
1543
Jens Axboe22e2c502005-06-27 10:55:12 +02001544/*
1545 * Setup general io context and cfq io context. There can be several cfq
1546 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001547 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001548 */
1549static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001550cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Jens Axboe22e2c502005-06-27 10:55:12 +02001552 struct io_context *ioc = NULL;
1553 struct cfq_io_context *cic;
1554
1555 might_sleep_if(gfp_mask & __GFP_WAIT);
1556
Jens Axboeb5deef92006-07-19 23:39:40 +02001557 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001558 if (!ioc)
1559 return NULL;
1560
Jens Axboee2d74ac2006-03-28 08:59:01 +02001561 cic = cfq_cic_rb_lookup(cfqd, ioc);
1562 if (cic)
1563 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001564
Jens Axboee2d74ac2006-03-28 08:59:01 +02001565 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1566 if (cic == NULL)
1567 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001568
Jens Axboee2d74ac2006-03-28 08:59:01 +02001569 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001570out:
Jens Axboefc463792006-08-29 09:05:44 +02001571 smp_read_barrier_depends();
1572 if (unlikely(ioc->ioprio_changed))
1573 cfq_ioc_set_ioprio(ioc);
1574
Jens Axboe22e2c502005-06-27 10:55:12 +02001575 return cic;
1576err:
1577 put_io_context(ioc);
1578 return NULL;
1579}
1580
1581static void
1582cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1583{
Jens Axboeaaf12282007-01-19 11:30:16 +11001584 unsigned long elapsed = jiffies - cic->last_end_request;
1585 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001586
1587 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1588 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1589 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1590}
1591
Jens Axboe206dc692006-03-28 13:03:44 +02001592static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001593cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1594 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001595{
1596 sector_t sdist;
1597 u64 total;
1598
Jens Axboe5e705372006-07-13 12:39:25 +02001599 if (cic->last_request_pos < rq->sector)
1600 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001601 else
Jens Axboe5e705372006-07-13 12:39:25 +02001602 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001603
Jens Axboe6d048f52007-04-25 12:44:27 +02001604 if (!cic->seek_samples) {
1605 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1606 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1607 }
1608
Jens Axboe206dc692006-03-28 13:03:44 +02001609 /*
1610 * Don't allow the seek distance to get too large from the
1611 * odd fragment, pagein, etc
1612 */
1613 if (cic->seek_samples <= 60) /* second&third seek */
1614 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1615 else
1616 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1617
1618 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1619 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1620 total = cic->seek_total + (cic->seek_samples/2);
1621 do_div(total, cic->seek_samples);
1622 cic->seek_mean = (sector_t)total;
1623}
Jens Axboe22e2c502005-06-27 10:55:12 +02001624
1625/*
1626 * Disable idle window if the process thinks too long or seeks so much that
1627 * it doesn't matter
1628 */
1629static void
1630cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1631 struct cfq_io_context *cic)
1632{
Jens Axboe3b181522005-06-27 10:56:24 +02001633 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001634
Jens Axboecaaa5f92006-06-16 11:23:00 +02001635 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1636 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001637 enable_idle = 0;
1638 else if (sample_valid(cic->ttime_samples)) {
1639 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1640 enable_idle = 0;
1641 else
1642 enable_idle = 1;
1643 }
1644
Jens Axboe3b181522005-06-27 10:56:24 +02001645 if (enable_idle)
1646 cfq_mark_cfqq_idle_window(cfqq);
1647 else
1648 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001649}
1650
Jens Axboe22e2c502005-06-27 10:55:12 +02001651/*
1652 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1653 * no or if we aren't sure, a 1 will cause a preempt.
1654 */
1655static int
1656cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001657 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001658{
Jens Axboe6d048f52007-04-25 12:44:27 +02001659 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001660
Jens Axboe6d048f52007-04-25 12:44:27 +02001661 cfqq = cfqd->active_queue;
1662 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001663 return 0;
1664
Jens Axboe6d048f52007-04-25 12:44:27 +02001665 if (cfq_slice_used(cfqq))
1666 return 1;
1667
1668 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001669 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001670
1671 if (cfq_class_idle(cfqq))
1672 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001673
Jens Axboe22e2c502005-06-27 10:55:12 +02001674 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001675 * if the new request is sync, but the currently running queue is
1676 * not, let the sync request have priority.
1677 */
Jens Axboe5e705372006-07-13 12:39:25 +02001678 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001679 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001680
Jens Axboe374f84a2006-07-23 01:42:19 +02001681 /*
1682 * So both queues are sync. Let the new request get disk time if
1683 * it's a metadata request and the current queue is doing regular IO.
1684 */
1685 if (rq_is_meta(rq) && !cfqq->meta_pending)
1686 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001687
Jens Axboe1e3335d2007-02-14 19:59:49 +01001688 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1689 return 0;
1690
1691 /*
1692 * if this request is as-good as one we would expect from the
1693 * current cfqq, let it preempt
1694 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001695 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001696 return 1;
1697
Jens Axboe22e2c502005-06-27 10:55:12 +02001698 return 0;
1699}
1700
1701/*
1702 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1703 * let it have half of its nominal slice.
1704 */
1705static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1706{
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001707 cfq_slice_expired(cfqd, 1, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001708
Jens Axboebf572252006-07-19 20:29:12 +02001709 /*
1710 * Put the new queue at the front of the of the current list,
1711 * so we know that it will be selected next.
1712 */
1713 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboed9e76202007-04-20 14:27:50 +02001714 list_del_init(&cfqq->cfq_list);
1715 list_add(&cfqq->cfq_list, &cfqd->cur_rr);
Jens Axboebf572252006-07-19 20:29:12 +02001716
Jens Axboe44f7c162007-01-19 11:51:58 +11001717 cfqq->slice_end = 0;
1718 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001719}
1720
1721/*
Jens Axboe5e705372006-07-13 12:39:25 +02001722 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001723 * something we should do about it
1724 */
1725static void
Jens Axboe5e705372006-07-13 12:39:25 +02001726cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1727 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001728{
Jens Axboe5e705372006-07-13 12:39:25 +02001729 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001730
Jens Axboe374f84a2006-07-23 01:42:19 +02001731 if (rq_is_meta(rq))
1732 cfqq->meta_pending++;
1733
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001734 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001735 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001736 cfq_update_idle_window(cfqd, cfqq, cic);
1737
Jens Axboe5e705372006-07-13 12:39:25 +02001738 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001739 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001740
1741 if (cfqq == cfqd->active_queue) {
1742 /*
1743 * if we are waiting for a request for this queue, let it rip
1744 * immediately and flag that we must not expire this queue
1745 * just now
1746 */
Jens Axboe3b181522005-06-27 10:56:24 +02001747 if (cfq_cfqq_wait_request(cfqq)) {
1748 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001749 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001750 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001751 }
Jens Axboe5e705372006-07-13 12:39:25 +02001752 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001753 /*
1754 * not the active queue - expire current slice if it is
1755 * idle and has expired it's mean thinktime or this new queue
1756 * has some old slice time left and is of higher priority
1757 */
1758 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001759 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001760 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001761 }
1762}
1763
Jens Axboeb4878f22005-10-20 16:42:29 +02001764static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001765{
Jens Axboeb4878f22005-10-20 16:42:29 +02001766 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001767 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001768
1769 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jens Axboe5e705372006-07-13 12:39:25 +02001771 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jens Axboe22e2c502005-06-27 10:55:12 +02001773 list_add_tail(&rq->queuelist, &cfqq->fifo);
1774
Jens Axboe5e705372006-07-13 12:39:25 +02001775 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778static void cfq_completed_request(request_queue_t *q, struct request *rq)
1779{
Jens Axboe5e705372006-07-13 12:39:25 +02001780 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001781 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001782 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001783 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jens Axboeb4878f22005-10-20 16:42:29 +02001785 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Jens Axboeb4878f22005-10-20 16:42:29 +02001787 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001788 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001789 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001790 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jens Axboeb4878f22005-10-20 16:42:29 +02001792 if (!cfq_class_idle(cfqq))
1793 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001794
Jens Axboecaaa5f92006-06-16 11:23:00 +02001795 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001796 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001797
1798 /*
1799 * If this is the active queue, check if it needs to be expired,
1800 * or if we want to idle in case it has no pending requests.
1801 */
1802 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001803 if (cfq_cfqq_slice_new(cfqq)) {
1804 cfq_set_prio_slice(cfqd, cfqq);
1805 cfq_clear_cfqq_slice_new(cfqq);
1806 }
1807 if (cfq_slice_used(cfqq))
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001808 cfq_slice_expired(cfqd, 0, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001809 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1810 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001811 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001812
1813 if (!cfqd->rq_in_driver)
1814 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Jens Axboe22e2c502005-06-27 10:55:12 +02001817/*
1818 * we temporarily boost lower priority queues if they are holding fs exclusive
1819 * resources. they are boosted to normal prio (CLASS_BE/4)
1820 */
1821static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Jens Axboe22e2c502005-06-27 10:55:12 +02001823 if (has_fs_excl()) {
1824 /*
1825 * boost idle prio on transactions that would lock out other
1826 * users of the filesystem
1827 */
1828 if (cfq_class_idle(cfqq))
1829 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1830 if (cfqq->ioprio > IOPRIO_NORM)
1831 cfqq->ioprio = IOPRIO_NORM;
1832 } else {
1833 /*
1834 * check if we need to unboost the queue
1835 */
1836 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1837 cfqq->ioprio_class = cfqq->org_ioprio_class;
1838 if (cfqq->ioprio != cfqq->org_ioprio)
1839 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001841}
1842
Jens Axboe89850f72006-07-22 16:48:31 +02001843static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001844{
Jens Axboe3b181522005-06-27 10:56:24 +02001845 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001846 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001847 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001848 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001849 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001850
1851 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001852}
1853
Jens Axboecb78b282006-07-28 09:32:57 +02001854static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001855{
1856 struct cfq_data *cfqd = q->elevator->elevator_data;
1857 struct task_struct *tsk = current;
1858 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001859 unsigned int key;
1860
1861 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001862
1863 /*
1864 * don't force setup of a queue from here, as a call to may_queue
1865 * does not necessarily imply that a request actually will be queued.
1866 * so just lookup a possibly existing queue, or return 'may queue'
1867 * if that fails
1868 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001869 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001870 if (cfqq) {
1871 cfq_init_prio_data(cfqq);
1872 cfq_prio_boost(cfqq);
1873
Jens Axboe89850f72006-07-22 16:48:31 +02001874 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001875 }
1876
1877 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/*
1881 * queue lock held here
1882 */
Jens Axboebb37b942006-12-01 10:42:33 +01001883static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Jens Axboe5e705372006-07-13 12:39:25 +02001885 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jens Axboe5e705372006-07-13 12:39:25 +02001887 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001888 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jens Axboe22e2c502005-06-27 10:55:12 +02001890 BUG_ON(!cfqq->allocated[rw]);
1891 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Jens Axboe5e705372006-07-13 12:39:25 +02001893 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001896 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 cfq_put_queue(cfqq);
1899 }
1900}
1901
1902/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001903 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001905static int
Jens Axboecb78b282006-07-28 09:32:57 +02001906cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001909 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct cfq_io_context *cic;
1911 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001912 const int is_sync = rq_is_sync(rq);
1913 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001914 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 unsigned long flags;
1916
1917 might_sleep_if(gfp_mask & __GFP_WAIT);
1918
Jens Axboee2d74ac2006-03-28 08:59:01 +02001919 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 spin_lock_irqsave(q->queue_lock, flags);
1922
Jens Axboe22e2c502005-06-27 10:55:12 +02001923 if (!cic)
1924 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Al Viro12a05732006-03-18 13:38:01 -05001926 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001927 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001928 if (!cfqq)
1929 goto queue_fail;
1930
Al Viro12a05732006-03-18 13:38:01 -05001931 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001932 } else
Al Viro12a05732006-03-18 13:38:01 -05001933 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001936 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001937 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 spin_unlock_irqrestore(q->queue_lock, flags);
1940
Jens Axboe5e705372006-07-13 12:39:25 +02001941 rq->elevator_private = cic;
1942 rq->elevator_private2 = cfqq;
1943 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001944
Jens Axboe22e2c502005-06-27 10:55:12 +02001945queue_fail:
1946 if (cic)
1947 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001948
Jens Axboe3b181522005-06-27 10:56:24 +02001949 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 spin_unlock_irqrestore(q->queue_lock, flags);
1951 return 1;
1952}
1953
David Howells65f27f32006-11-22 14:55:48 +00001954static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001955{
David Howells65f27f32006-11-22 14:55:48 +00001956 struct cfq_data *cfqd =
1957 container_of(work, struct cfq_data, unplug_work);
1958 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001959 unsigned long flags;
1960
1961 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001962 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001963 spin_unlock_irqrestore(q->queue_lock, flags);
1964}
1965
1966/*
1967 * Timer running if the active_queue is currently idling inside its time slice
1968 */
1969static void cfq_idle_slice_timer(unsigned long data)
1970{
1971 struct cfq_data *cfqd = (struct cfq_data *) data;
1972 struct cfq_queue *cfqq;
1973 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001974 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001975
1976 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1977
1978 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001979 timed_out = 0;
1980
Jens Axboe22e2c502005-06-27 10:55:12 +02001981 /*
1982 * expired
1983 */
Jens Axboe44f7c162007-01-19 11:51:58 +11001984 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001985 goto expire;
1986
1987 /*
1988 * only expire and reinvoke request handler, if there are
1989 * other queues with pending requests
1990 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001991 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001992 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001993
1994 /*
1995 * not expired and it has a request pending, let it dispatch
1996 */
Jens Axboedd67d052006-06-21 09:36:18 +02001997 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001998 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001999 goto out_kick;
2000 }
2001 }
2002expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002003 cfq_slice_expired(cfqd, 0, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002004out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002005 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002006out_cont:
2007 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2008}
2009
2010/*
2011 * Timer running if an idle class queue is waiting for service
2012 */
2013static void cfq_idle_class_timer(unsigned long data)
2014{
2015 struct cfq_data *cfqd = (struct cfq_data *) data;
2016 unsigned long flags, end;
2017
2018 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2019
2020 /*
2021 * race with a non-idle queue, reset timer
2022 */
2023 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002024 if (!time_after_eq(jiffies, end))
2025 mod_timer(&cfqd->idle_class_timer, end);
2026 else
Jens Axboe3b181522005-06-27 10:56:24 +02002027 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002028
2029 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2030}
2031
Jens Axboe3b181522005-06-27 10:56:24 +02002032static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2033{
2034 del_timer_sync(&cfqd->idle_slice_timer);
2035 del_timer_sync(&cfqd->idle_class_timer);
2036 blk_sync_queue(cfqd->queue);
2037}
Jens Axboe22e2c502005-06-27 10:55:12 +02002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039static void cfq_exit_queue(elevator_t *e)
2040{
Jens Axboe22e2c502005-06-27 10:55:12 +02002041 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002042 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002043
Jens Axboe3b181522005-06-27 10:56:24 +02002044 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002045
Al Virod9ff4182006-03-18 13:51:22 -05002046 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002047
Al Virod9ff4182006-03-18 13:51:22 -05002048 if (cfqd->active_queue)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002049 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002050
2051 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002052 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2053 struct cfq_io_context,
2054 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002055
2056 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002057 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002058
Al Virod9ff4182006-03-18 13:51:22 -05002059 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002060
2061 cfq_shutdown_timer_wq(cfqd);
2062
Al Viroa90d7422006-03-18 12:05:37 -05002063 kfree(cfqd->cfq_hash);
2064 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Jens Axboebb37b942006-12-01 10:42:33 +01002067static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct cfq_data *cfqd;
2070 int i;
2071
Jens Axboeb5deef92006-07-19 23:39:40 +02002072 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002074 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002077
Jens Axboecc09e292007-04-26 12:53:50 +02002078 cfqd->service_tree = CFQ_RB_ROOT;
Jens Axboe22e2c502005-06-27 10:55:12 +02002079 INIT_LIST_HEAD(&cfqd->cur_rr);
Al Virod9ff4182006-03-18 13:51:22 -05002080 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Jens Axboeb5deef92006-07-19 23:39:40 +02002082 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002084 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2087 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Jens Axboe22e2c502005-06-27 10:55:12 +02002091 init_timer(&cfqd->idle_slice_timer);
2092 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2093 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2094
2095 init_timer(&cfqd->idle_class_timer);
2096 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2097 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2098
David Howells65f27f32006-11-22 14:55:48 +00002099 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002102 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2103 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cfqd->cfq_back_max = cfq_back_max;
2105 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002106 cfqd->cfq_slice[0] = cfq_slice_async;
2107 cfqd->cfq_slice[1] = cfq_slice_sync;
2108 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2109 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002110
Jens Axboebc1c1162006-06-08 08:49:06 +02002111 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002112out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002114 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
2117static void cfq_slab_kill(void)
2118{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (cfq_pool)
2120 kmem_cache_destroy(cfq_pool);
2121 if (cfq_ioc_pool)
2122 kmem_cache_destroy(cfq_ioc_pool);
2123}
2124
2125static int __init cfq_slab_setup(void)
2126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2128 NULL, NULL);
2129 if (!cfq_pool)
2130 goto fail;
2131
2132 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2133 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2134 if (!cfq_ioc_pool)
2135 goto fail;
2136
2137 return 0;
2138fail:
2139 cfq_slab_kill();
2140 return -ENOMEM;
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/*
2144 * sysfs parts below -->
2145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146static ssize_t
2147cfq_var_show(unsigned int var, char *page)
2148{
2149 return sprintf(page, "%d\n", var);
2150}
2151
2152static ssize_t
2153cfq_var_store(unsigned int *var, const char *page, size_t count)
2154{
2155 char *p = (char *) page;
2156
2157 *var = simple_strtoul(p, &p, 10);
2158 return count;
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002162static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002164 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 unsigned int __data = __VAR; \
2166 if (__CONV) \
2167 __data = jiffies_to_msecs(__data); \
2168 return cfq_var_show(__data, (page)); \
2169}
2170SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002171SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2172SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002173SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2174SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002175SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2176SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2177SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2178SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179#undef SHOW_FUNCTION
2180
2181#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002182static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002184 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 unsigned int __data; \
2186 int ret = cfq_var_store(&__data, (page), count); \
2187 if (__data < (MIN)) \
2188 __data = (MIN); \
2189 else if (__data > (MAX)) \
2190 __data = (MAX); \
2191 if (__CONV) \
2192 *(__PTR) = msecs_to_jiffies(__data); \
2193 else \
2194 *(__PTR) = __data; \
2195 return ret; \
2196}
2197STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002198STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2199STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002200STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2201STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002202STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2203STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2204STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2205STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206#undef STORE_FUNCTION
2207
Al Viroe572ec72006-03-18 22:27:18 -05002208#define CFQ_ATTR(name) \
2209 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002210
Al Viroe572ec72006-03-18 22:27:18 -05002211static struct elv_fs_entry cfq_attrs[] = {
2212 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002213 CFQ_ATTR(fifo_expire_sync),
2214 CFQ_ATTR(fifo_expire_async),
2215 CFQ_ATTR(back_seek_max),
2216 CFQ_ATTR(back_seek_penalty),
2217 CFQ_ATTR(slice_sync),
2218 CFQ_ATTR(slice_async),
2219 CFQ_ATTR(slice_async_rq),
2220 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002221 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222};
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224static struct elevator_type iosched_cfq = {
2225 .ops = {
2226 .elevator_merge_fn = cfq_merge,
2227 .elevator_merged_fn = cfq_merged_request,
2228 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002229 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002230 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002232 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 .elevator_deactivate_req_fn = cfq_deactivate_request,
2234 .elevator_queue_empty_fn = cfq_queue_empty,
2235 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002236 .elevator_former_req_fn = elv_rb_former_request,
2237 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 .elevator_set_req_fn = cfq_set_request,
2239 .elevator_put_req_fn = cfq_put_request,
2240 .elevator_may_queue_fn = cfq_may_queue,
2241 .elevator_init_fn = cfq_init_queue,
2242 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002243 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 },
Al Viro3d1ab402006-03-18 18:35:43 -05002245 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 .elevator_name = "cfq",
2247 .elevator_owner = THIS_MODULE,
2248};
2249
2250static int __init cfq_init(void)
2251{
2252 int ret;
2253
Jens Axboe22e2c502005-06-27 10:55:12 +02002254 /*
2255 * could be 0 on HZ < 1000 setups
2256 */
2257 if (!cfq_slice_async)
2258 cfq_slice_async = 1;
2259 if (!cfq_slice_idle)
2260 cfq_slice_idle = 1;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (cfq_slab_setup())
2263 return -ENOMEM;
2264
2265 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002266 if (ret)
2267 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return ret;
2270}
2271
2272static void __exit cfq_exit(void)
2273{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002274 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002276 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002277 /* ioc_gone's update must be visible before reading ioc_count */
2278 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002279 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002280 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002281 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002282 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285module_init(cfq_init);
2286module_exit(cfq_exit);
2287
2288MODULE_AUTHOR("Jens Axboe");
2289MODULE_LICENSE("GPL");
2290MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");