blob: bc7190eed10d129012b5d4381f843ba6d7390d21 [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/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020013#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15/*
16 * tunables
17 */
Arjan van de Ven64100092006-01-06 09:46:02 +010018static const int cfq_quantum = 4; /* max queue in one round of service */
Arjan van de Ven64100092006-01-06 09:46:02 +010019static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
20static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
21static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Arjan van de Ven64100092006-01-06 09:46:02 +010023static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020024static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010025static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020026static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020027
Jens Axboed9e76202007-04-20 14:27:50 +020028/*
29 * grace period before allowing idle class to get disk access
30 */
Jens Axboe22e2c502005-06-27 10:55:12 +020031#define CFQ_IDLE_GRACE (HZ / 10)
Jens Axboed9e76202007-04-20 14:27:50 +020032
33/*
34 * below this threshold, we consider thinktime immediate
35 */
36#define CFQ_MIN_TT (2)
37
Jens Axboe22e2c502005-06-27 10:55:12 +020038#define CFQ_SLICE_SCALE (5)
39
Jens Axboe5e705372006-07-13 12:39:25 +020040#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
41#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Lametere18b8902006-12-06 20:33:20 -080043static struct kmem_cache *cfq_pool;
44static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jens Axboe4050cf12006-07-19 05:07:12 +020046static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050047static struct completion *ioc_gone;
48
Jens Axboe22e2c502005-06-27 10:55:12 +020049#define CFQ_PRIO_LISTS IOPRIO_BE_NR
50#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020051#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
52
Jens Axboe3b181522005-06-27 10:56:24 +020053#define ASYNC (0)
54#define SYNC (1)
55
Jens Axboe206dc692006-03-28 13:03:44 +020056#define sample_valid(samples) ((samples) > 80)
57
Jens Axboe22e2c502005-06-27 10:55:12 +020058/*
Jens Axboecc09e292007-04-26 12:53:50 +020059 * Most of our rbtree usage is for sorting with min extraction, so
60 * if we cache the leftmost node we don't have to walk down the tree
61 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
62 * move this into the elevator for the rq sorting as well.
63 */
64struct cfq_rb_root {
65 struct rb_root rb;
66 struct rb_node *left;
67};
68#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
69
70/*
Jens Axboe22e2c502005-06-27 10:55:12 +020071 * Per block device queue structure
72 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020074 request_queue_t *queue;
75
76 /*
77 * rr list of queues with requests and the count of them
78 */
Jens Axboecc09e292007-04-26 12:53:50 +020079 struct cfq_rb_root service_tree;
Jens Axboe22e2c502005-06-27 10:55:12 +020080 unsigned int busy_queues;
81
Jens Axboe22e2c502005-06-27 10:55:12 +020082 int rq_in_driver;
Jens Axboe3ed9a292007-04-23 08:33:33 +020083 int sync_flight;
Jens Axboe25776e32006-06-01 10:12:26 +020084 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020085
86 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020087 * idle window management
88 */
89 struct timer_list idle_slice_timer;
90 struct work_struct unplug_work;
91
92 struct cfq_queue *active_queue;
93 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +020094
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +020095 /*
96 * async queue for each priority case
97 */
98 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
99 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200100
Jens Axboe22e2c502005-06-27 10:55:12 +0200101 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jens Axboe6d048f52007-04-25 12:44:27 +0200103 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200104 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /*
107 * tunables, see top of file
108 */
109 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200110 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned int cfq_back_penalty;
112 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200113 unsigned int cfq_slice[2];
114 unsigned int cfq_slice_async_rq;
115 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500116
117 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200118
119 sector_t new_seek_mean;
120 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Jens Axboe22e2c502005-06-27 10:55:12 +0200123/*
124 * Per process-grouping structure
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct cfq_queue {
127 /* reference count */
128 atomic_t ref;
129 /* parent cfq_data */
130 struct cfq_data *cfqd;
Jens Axboed9e76202007-04-20 14:27:50 +0200131 /* service_tree member */
132 struct rb_node rb_node;
133 /* service_tree key */
134 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* sorted list of pending requests */
136 struct rb_root sort_list;
137 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200138 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* requests queued in sort_list */
140 int queued[2];
141 /* currently allocated requests */
142 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200143 /* pending metadata requests */
144 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200146 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100149 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jens Axboe6d048f52007-04-25 12:44:27 +0200151 /* number of requests that are on the dispatch list or inside driver */
152 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200153
154 /* io prio of this group */
155 unsigned short ioprio, org_ioprio;
156 unsigned short ioprio_class, org_ioprio_class;
157
Jens Axboe3b181522005-06-27 10:56:24 +0200158 /* various state flags, see below */
159 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200160
161 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Jens Axboe3b181522005-06-27 10:56:24 +0200164enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100165 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
166 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
167 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
168 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
169 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
170 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
171 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
172 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
173 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100174 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200175 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jens Axboe3b181522005-06-27 10:56:24 +0200176};
177
178#define CFQ_CFQQ_FNS(name) \
179static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
180{ \
181 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
182} \
183static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
184{ \
185 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
186} \
187static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
188{ \
189 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
190}
191
192CFQ_CFQQ_FNS(on_rr);
193CFQ_CFQQ_FNS(wait_request);
194CFQ_CFQQ_FNS(must_alloc);
195CFQ_CFQQ_FNS(must_alloc_slice);
196CFQ_CFQQ_FNS(must_dispatch);
197CFQ_CFQQ_FNS(fifo_expire);
198CFQ_CFQQ_FNS(idle_window);
199CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b037442006-07-28 09:48:51 +0200200CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100201CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200202CFQ_CFQQ_FNS(sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200203#undef CFQ_CFQQ_FNS
204
Jens Axboe5e705372006-07-13 12:39:25 +0200205static void cfq_dispatch_insert(request_queue_t *, struct request *);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200206static struct cfq_queue *cfq_get_queue(struct cfq_data *, int,
207 struct task_struct *, gfp_t);
208static struct cfq_io_context *cfq_cic_rb_lookup(struct cfq_data *,
209 struct io_context *);
210
211static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
212 int is_sync)
213{
214 return cic->cfqq[!!is_sync];
215}
216
217static inline void cic_set_cfqq(struct cfq_io_context *cic,
218 struct cfq_queue *cfqq, int is_sync)
219{
220 cic->cfqq[!!is_sync] = cfqq;
221}
222
223/*
224 * We regard a request as SYNC, if it's either a read or has the SYNC bit
225 * set (in which case it could also be direct WRITE).
226 */
227static inline int cfq_bio_sync(struct bio *bio)
228{
229 if (bio_data_dir(bio) == READ || bio_sync(bio))
230 return 1;
231
232 return 0;
233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700236 * scheduler run of queue, if there are requests pending and no one in the
237 * driver that will restart queueing
238 */
239static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
240{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100241 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700242 kblockd_schedule_work(&cfqd->unplug_work);
243}
244
245static int cfq_queue_empty(request_queue_t *q)
246{
247 struct cfq_data *cfqd = q->elevator->elevator_data;
248
Jens Axboeb4878f22005-10-20 16:42:29 +0200249 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100253 * Scale schedule slice based on io priority. Use the sync time slice only
254 * if a queue is marked sync and has sync io queued. A sync queue with async
255 * io only, should not get full sync slice length.
256 */
Jens Axboed9e76202007-04-20 14:27:50 +0200257static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
258 unsigned short prio)
259{
260 const int base_slice = cfqd->cfq_slice[sync];
261
262 WARN_ON(prio >= IOPRIO_BE_NR);
263
264 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
265}
266
Jens Axboe44f7c162007-01-19 11:51:58 +1100267static inline int
268cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
269{
Jens Axboed9e76202007-04-20 14:27:50 +0200270 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100271}
272
273static inline void
274cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
275{
276 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
277}
278
279/*
280 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
281 * isn't valid until the first request from the dispatch is activated
282 * and the slice time set.
283 */
284static inline int cfq_slice_used(struct cfq_queue *cfqq)
285{
286 if (cfq_cfqq_slice_new(cfqq))
287 return 0;
288 if (time_before(jiffies, cfqq->slice_end))
289 return 0;
290
291 return 1;
292}
293
294/*
Jens Axboe5e705372006-07-13 12:39:25 +0200295 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200297 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Jens Axboe5e705372006-07-13 12:39:25 +0200299static struct request *
300cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200304#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
305#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
306 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Jens Axboe5e705372006-07-13 12:39:25 +0200308 if (rq1 == NULL || rq1 == rq2)
309 return rq2;
310 if (rq2 == NULL)
311 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200312
Jens Axboe5e705372006-07-13 12:39:25 +0200313 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
314 return rq1;
315 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
316 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200317 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
318 return rq1;
319 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
320 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jens Axboe5e705372006-07-13 12:39:25 +0200322 s1 = rq1->sector;
323 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jens Axboe6d048f52007-04-25 12:44:27 +0200325 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /*
328 * by definition, 1KiB is 2 sectors
329 */
330 back_max = cfqd->cfq_back_max * 2;
331
332 /*
333 * Strict one way elevator _except_ in the case where we allow
334 * short backward seeks which are biased as twice the cost of a
335 * similar forward seek.
336 */
337 if (s1 >= last)
338 d1 = s1 - last;
339 else if (s1 + back_max >= last)
340 d1 = (last - s1) * cfqd->cfq_back_penalty;
341 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200342 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (s2 >= last)
345 d2 = s2 - last;
346 else if (s2 + back_max >= last)
347 d2 = (last - s2) * cfqd->cfq_back_penalty;
348 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200349 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Andreas Mohre8a99052006-03-28 08:59:49 +0200353 /*
354 * By doing switch() on the bit mask "wrap" we avoid having to
355 * check two variables for all permutations: --> faster!
356 */
357 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200358 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200359 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200360 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200361 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200362 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200363 else {
364 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200365 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200366 else
Jens Axboe5e705372006-07-13 12:39:25 +0200367 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200368 }
369
370 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200371 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200372 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200373 return rq2;
374 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200375 default:
376 /*
377 * Since both rqs are wrapped,
378 * start with the one that's further behind head
379 * (--> only *one* back seek required),
380 * since back seek takes more time than forward.
381 */
382 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200383 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 else
Jens Axboe5e705372006-07-13 12:39:25 +0200385 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387}
388
Jens Axboe498d3aa22007-04-26 12:54:48 +0200389/*
390 * The below is leftmost cache rbtree addon
391 */
Jens Axboecc09e292007-04-26 12:53:50 +0200392static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
393{
394 if (!root->left)
395 root->left = rb_first(&root->rb);
396
397 return root->left;
398}
399
400static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
401{
402 if (root->left == n)
403 root->left = NULL;
404
405 rb_erase(n, &root->rb);
406 RB_CLEAR_NODE(n);
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * would be nice to take fifo expire time into account as well
411 */
Jens Axboe5e705372006-07-13 12:39:25 +0200412static struct request *
413cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
414 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Jens Axboe21183b02006-07-13 12:33:14 +0200416 struct rb_node *rbnext = rb_next(&last->rb_node);
417 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200418 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jens Axboe21183b02006-07-13 12:33:14 +0200420 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200423 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200426 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200427 else {
428 rbnext = rb_first(&cfqq->sort_list);
429 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200430 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jens Axboe21183b02006-07-13 12:33:14 +0200433 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Jens Axboed9e76202007-04-20 14:27:50 +0200436static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
437 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jens Axboed9e76202007-04-20 14:27:50 +0200439 /*
440 * just an approximation, should be ok.
441 */
Jens Axboe67e6b492007-04-20 14:18:00 +0200442 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
443 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jens Axboe498d3aa22007-04-26 12:54:48 +0200446/*
447 * The cfqd->service_tree holds all pending cfq_queue's that have
448 * requests waiting to be processed. It is sorted in the order that
449 * we will service the queues.
450 */
Jens Axboed9e76202007-04-20 14:27:50 +0200451static void cfq_service_tree_add(struct cfq_data *cfqd,
Jens Axboeedd75ff2007-04-19 12:03:34 +0200452 struct cfq_queue *cfqq, int add_front)
Jens Axboed9e76202007-04-20 14:27:50 +0200453{
Jens Axboecc09e292007-04-26 12:53:50 +0200454 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200455 struct rb_node *parent = NULL;
Jens Axboed9e76202007-04-20 14:27:50 +0200456 unsigned long rb_key;
Jens Axboe498d3aa22007-04-26 12:54:48 +0200457 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200458
Jens Axboeedd75ff2007-04-19 12:03:34 +0200459 if (!add_front) {
460 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
461 rb_key += cfqq->slice_resid;
462 cfqq->slice_resid = 0;
463 } else
464 rb_key = 0;
Jens Axboed9e76202007-04-20 14:27:50 +0200465
466 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100467 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200468 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100469 */
Jens Axboed9e76202007-04-20 14:27:50 +0200470 if (rb_key == cfqq->rb_key)
471 return;
Jens Axboe53b037442006-07-28 09:48:51 +0200472
Jens Axboecc09e292007-04-26 12:53:50 +0200473 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200474 }
Jens Axboed9e76202007-04-20 14:27:50 +0200475
Jens Axboe498d3aa22007-04-26 12:54:48 +0200476 left = 1;
Jens Axboed9e76202007-04-20 14:27:50 +0200477 while (*p) {
Jens Axboecc09e292007-04-26 12:53:50 +0200478 struct cfq_queue *__cfqq;
Jens Axboe67060e32007-04-18 20:13:32 +0200479 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200480
Jens Axboed9e76202007-04-20 14:27:50 +0200481 parent = *p;
482 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
483
Jens Axboe0c534e02007-04-18 20:01:57 +0200484 /*
485 * sort RT queues first, we always want to give
Jens Axboe67060e32007-04-18 20:13:32 +0200486 * preference to them. IDLE queues goes to the back.
487 * after that, sort on the next service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200488 */
489 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200490 n = &(*p)->rb_left;
Jens Axboe0c534e02007-04-18 20:01:57 +0200491 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200492 n = &(*p)->rb_right;
493 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
494 n = &(*p)->rb_left;
495 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
496 n = &(*p)->rb_right;
Jens Axboe0c534e02007-04-18 20:01:57 +0200497 else if (rb_key < __cfqq->rb_key)
Jens Axboe67060e32007-04-18 20:13:32 +0200498 n = &(*p)->rb_left;
499 else
500 n = &(*p)->rb_right;
501
502 if (n == &(*p)->rb_right)
Jens Axboecc09e292007-04-26 12:53:50 +0200503 left = 0;
Jens Axboe67060e32007-04-18 20:13:32 +0200504
505 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200506 }
507
Jens Axboecc09e292007-04-26 12:53:50 +0200508 if (left)
509 cfqd->service_tree.left = &cfqq->rb_node;
510
Jens Axboed9e76202007-04-20 14:27:50 +0200511 cfqq->rb_key = rb_key;
512 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200513 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Jens Axboe498d3aa22007-04-26 12:54:48 +0200516/*
517 * Update cfqq's position in the service tree.
518 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200519static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200520{
Jens Axboe6d048f52007-04-25 12:44:27 +0200521 /*
522 * Resorting requires the cfqq to be on the RR list already.
523 */
Jens Axboe498d3aa22007-04-26 12:54:48 +0200524 if (cfq_cfqq_on_rr(cfqq))
Jens Axboeedd75ff2007-04-19 12:03:34 +0200525 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboe6d048f52007-04-25 12:44:27 +0200526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200530 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200533cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Jens Axboe3b181522005-06-27 10:56:24 +0200535 BUG_ON(cfq_cfqq_on_rr(cfqq));
536 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 cfqd->busy_queues++;
538
Jens Axboeedd75ff2007-04-19 12:03:34 +0200539 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Jens Axboe498d3aa22007-04-26 12:54:48 +0200542/*
543 * Called when the cfqq no longer has requests pending, remove it from
544 * the service tree.
545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static inline void
547cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
548{
Jens Axboe3b181522005-06-27 10:56:24 +0200549 BUG_ON(!cfq_cfqq_on_rr(cfqq));
550 cfq_clear_cfqq_on_rr(cfqq);
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;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200614 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +0200615 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Vasily Tarasov91fac312007-04-25 12:29:51 +0200617 cic = cfq_cic_rb_lookup(cfqd, tsk->io_context);
618 if (!cic)
619 return NULL;
620
621 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +0200622 if (cfqq) {
623 sector_t sector = bio->bi_sector + bio_sectors(bio);
624
Jens Axboe21183b02006-07-13 12:33:14 +0200625 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return NULL;
629}
630
Jens Axboeb4878f22005-10-20 16:42:29 +0200631static void cfq_activate_request(request_queue_t *q, struct request *rq)
632{
633 struct cfq_data *cfqd = q->elevator->elevator_data;
634
635 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200636
637 /*
638 * If the depth is larger 1, it really could be queueing. But lets
639 * make the mark a little higher - idling could still be good for
640 * low queueing, and a low queueing number could also just indicate
641 * a SCSI mid layer like behaviour where limit+1 is often seen.
642 */
643 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
644 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200645
646 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
650{
Jens Axboe22e2c502005-06-27 10:55:12 +0200651 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jens Axboeb4878f22005-10-20 16:42:29 +0200653 WARN_ON(!cfqd->rq_in_driver);
654 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Jens Axboeb4878f22005-10-20 16:42:29 +0200657static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Jens Axboe5e705372006-07-13 12:39:25 +0200659 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200660
Jens Axboe5e705372006-07-13 12:39:25 +0200661 if (cfqq->next_rq == rq)
662 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Jens Axboeb4878f22005-10-20 16:42:29 +0200664 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200665 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200666
667 if (rq_is_meta(rq)) {
668 WARN_ON(!cfqq->meta_pending);
669 cfqq->meta_pending--;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Jens Axboe498d3aa22007-04-26 12:54:48 +0200673static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct cfq_data *cfqd = q->elevator->elevator_data;
676 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jens Axboe206dc692006-03-28 13:03:44 +0200678 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200679 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200680 *req = __rq;
681 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Jens Axboe21183b02006-07-13 12:33:14 +0200687static void cfq_merged_request(request_queue_t *q, struct request *req,
688 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jens Axboe21183b02006-07-13 12:33:14 +0200690 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200691 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jens Axboe5e705372006-07-13 12:39:25 +0200693 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static void
698cfq_merged_requests(request_queue_t *q, struct request *rq,
699 struct request *next)
700{
Jens Axboe22e2c502005-06-27 10:55:12 +0200701 /*
702 * reposition in fifo if next is older than rq
703 */
704 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
705 time_before(next->start_time, rq->start_time))
706 list_move(&rq->queuelist, &next->queuelist);
707
Jens Axboeb4878f22005-10-20 16:42:29 +0200708 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200709}
710
Jens Axboeda775262006-12-20 11:04:12 +0100711static int cfq_allow_merge(request_queue_t *q, struct request *rq,
712 struct bio *bio)
713{
714 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200715 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +0100716 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +0100717
718 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100719 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100720 */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200721 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100722 return 0;
723
724 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100725 * Lookup the cfqq that this bio will be queued with. Allow
726 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100727 */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200728 cic = cfq_cic_rb_lookup(cfqd, current->io_context);
729 if (!cic)
730 return 0;
Jens Axboe719d3402006-12-22 09:38:53 +0100731
Vasily Tarasov91fac312007-04-25 12:29:51 +0200732 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe719d3402006-12-22 09:38:53 +0100733 if (cfqq == RQ_CFQQ(rq))
734 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100735
Jens Axboeec8acb62007-01-02 18:32:11 +0100736 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100737}
738
Jens Axboe22e2c502005-06-27 10:55:12 +0200739static inline void
740__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
741{
742 if (cfqq) {
743 /*
744 * stop potential idle class queues waiting service
745 */
746 del_timer(&cfqd->idle_class_timer);
747
Jens Axboe22e2c502005-06-27 10:55:12 +0200748 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200749 cfq_clear_cfqq_must_alloc_slice(cfqq);
750 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100751 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200752 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200753 }
754
755 cfqd->active_queue = cfqq;
756}
757
758/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100759 * current cfqq expired its slice (or was too idle), select new one
760 */
761static void
762__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6084cdd2007-04-23 08:25:00 +0200763 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100764{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100765 if (cfq_cfqq_wait_request(cfqq))
766 del_timer(&cfqd->idle_slice_timer);
767
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100768 cfq_clear_cfqq_must_dispatch(cfqq);
769 cfq_clear_cfqq_wait_request(cfqq);
770
771 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +0200772 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100773 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100774 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100775 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100776
Jens Axboeedd75ff2007-04-19 12:03:34 +0200777 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100778
779 if (cfqq == cfqd->active_queue)
780 cfqd->active_queue = NULL;
781
782 if (cfqd->active_cic) {
783 put_io_context(cfqd->active_cic->ioc);
784 cfqd->active_cic = NULL;
785 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100786}
787
Jens Axboe6084cdd2007-04-23 08:25:00 +0200788static inline void cfq_slice_expired(struct cfq_data *cfqd, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100789{
790 struct cfq_queue *cfqq = cfqd->active_queue;
791
792 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +0200793 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100794}
795
Jens Axboe498d3aa22007-04-26 12:54:48 +0200796/*
797 * Get next queue for service. Unless we have a queue preemption,
798 * we'll simply select the first cfqq in the service tree.
799 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200800static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200801{
Jens Axboeedd75ff2007-04-19 12:03:34 +0200802 struct cfq_queue *cfqq;
803 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +0200804
Jens Axboeedd75ff2007-04-19 12:03:34 +0200805 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
806 return NULL;
807
808 n = cfq_rb_first(&cfqd->service_tree);
809 cfqq = rb_entry(n, struct cfq_queue, rb_node);
810
811 if (cfq_class_idle(cfqq)) {
812 unsigned long end;
813
Jens Axboe89850f72006-07-22 16:48:31 +0200814 /*
Jens Axboeedd75ff2007-04-19 12:03:34 +0200815 * if we have idle queues and no rt or be queues had
816 * pending requests, either allow immediate service if
817 * the grace period has passed or arm the idle grace
818 * timer
Jens Axboe89850f72006-07-22 16:48:31 +0200819 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200820 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
821 if (time_before(jiffies, end)) {
822 mod_timer(&cfqd->idle_class_timer, end);
823 cfqq = NULL;
Jens Axboe67060e32007-04-18 20:13:32 +0200824 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200825 }
826
Jens Axboe6d048f52007-04-25 12:44:27 +0200827 return cfqq;
828}
829
Jens Axboe498d3aa22007-04-26 12:54:48 +0200830/*
831 * Get and set a new active queue for service.
832 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200833static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
834{
835 struct cfq_queue *cfqq;
836
Jens Axboed9e76202007-04-20 14:27:50 +0200837 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200838 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200839 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200840}
841
Jens Axboed9e76202007-04-20 14:27:50 +0200842static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
843 struct request *rq)
844{
845 if (rq->sector >= cfqd->last_position)
846 return rq->sector - cfqd->last_position;
847 else
848 return cfqd->last_position - rq->sector;
849}
850
Jens Axboe6d048f52007-04-25 12:44:27 +0200851static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
852{
853 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200854
Jens Axboe6d048f52007-04-25 12:44:27 +0200855 if (!sample_valid(cic->seek_samples))
856 return 0;
857
858 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
859}
860
Jens Axboed9e76202007-04-20 14:27:50 +0200861static int cfq_close_cooperator(struct cfq_data *cfq_data,
862 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200863{
Jens Axboe6d048f52007-04-25 12:44:27 +0200864 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200865 * We should notice if some of the queues are cooperating, eg
866 * working closely on the same area of the disk. In that case,
867 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200868 */
Jens Axboed9e76202007-04-20 14:27:50 +0200869 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200870}
871
872#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
873
874static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200875{
Jens Axboe17926692007-01-19 11:59:30 +1100876 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200877 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100878 unsigned long sl;
879
Jens Axboedd67d052006-06-21 09:36:18 +0200880 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200881 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200882
883 /*
884 * idle is disabled, either manually or by past process history
885 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200886 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
887 return;
888
Jens Axboe22e2c502005-06-27 10:55:12 +0200889 /*
890 * task has exited, don't wait
891 */
Jens Axboe206dc692006-03-28 13:03:44 +0200892 cic = cfqd->active_cic;
893 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200894 return;
895
896 /*
897 * See if this prio level has a good candidate
898 */
Jens Axboe1afba042007-04-17 12:47:55 +0200899 if (cfq_close_cooperator(cfqd, cfqq) &&
900 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200901 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200902
Jens Axboe3b181522005-06-27 10:56:24 +0200903 cfq_mark_cfqq_must_dispatch(cfqq);
904 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200905
Jens Axboe206dc692006-03-28 13:03:44 +0200906 /*
907 * we don't want to idle for seeks, but we do want to allow
908 * fair distribution of slice time for a process doing back-to-back
909 * seeks. so allow a little bit of time for him to submit a new rq
910 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200911 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200912 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200913 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200914
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100915 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Jens Axboe498d3aa22007-04-26 12:54:48 +0200918/*
919 * Move request from internal lists to the request queue dispatch list.
920 */
Jens Axboe5e705372006-07-13 12:39:25 +0200921static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jens Axboe3ed9a292007-04-23 08:33:33 +0200923 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200924 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200925
Jens Axboe5380a102006-07-13 12:37:56 +0200926 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200927 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200928 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +0200929
930 if (cfq_cfqq_sync(cfqq))
931 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934/*
935 * return expired entry, or NULL to just start from scratch in rbtree
936 */
Jens Axboe5e705372006-07-13 12:39:25 +0200937static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200940 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200941 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jens Axboe3b181522005-06-27 10:56:24 +0200943 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100945
946 cfq_mark_cfqq_fifo_expire(cfqq);
947
Jens Axboe89850f72006-07-22 16:48:31 +0200948 if (list_empty(&cfqq->fifo))
949 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jens Axboe6d048f52007-04-25 12:44:27 +0200951 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200952 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jens Axboe6d048f52007-04-25 12:44:27 +0200954 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
955 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jens Axboe6d048f52007-04-25 12:44:27 +0200957 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Jens Axboe22e2c502005-06-27 10:55:12 +0200960static inline int
961cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
962{
963 const int base_rq = cfqd->cfq_slice_async_rq;
964
965 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
966
967 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
968}
969
970/*
Jens Axboe498d3aa22007-04-26 12:54:48 +0200971 * Select a queue for service. If we have a current active queue,
972 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +0200973 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100974static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200975{
Jens Axboe22e2c502005-06-27 10:55:12 +0200976 struct cfq_queue *cfqq;
977
978 cfqq = cfqd->active_queue;
979 if (!cfqq)
980 goto new_queue;
981
982 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200983 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200984 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200985 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200986 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200987
988 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200989 * The active queue has requests and isn't expired, allow it to
990 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200991 */
Jens Axboedd67d052006-06-21 09:36:18 +0200992 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200993 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200994
995 /*
996 * No requests pending. If the active queue still has requests in
997 * flight or is idling for a new request, allow either of these
998 * conditions to happen (or time out) before selecting a new queue.
999 */
Jens Axboecc197472007-04-20 20:45:39 +02001000 if (timer_pending(&cfqd->idle_slice_timer) ||
1001 (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001002 cfqq = NULL;
1003 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001004 }
1005
Jens Axboe3b181522005-06-27 10:56:24 +02001006expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02001007 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001008new_queue:
1009 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001010keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001011 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001012}
1013
Jens Axboe498d3aa22007-04-26 12:54:48 +02001014/*
1015 * Dispatch some requests from cfqq, moving them to the request queue
1016 * dispatch list.
1017 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001018static int
1019__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1020 int max_dispatch)
1021{
1022 int dispatched = 0;
1023
Jens Axboedd67d052006-06-21 09:36:18 +02001024 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001025
1026 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001027 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001028
1029 /*
1030 * follow expired path, else get first next available
1031 */
Jens Axboe5e705372006-07-13 12:39:25 +02001032 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1033 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001034
1035 /*
1036 * finally, insert request into driver dispatch list
1037 */
Jens Axboe5e705372006-07-13 12:39:25 +02001038 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001039
Jens Axboe22e2c502005-06-27 10:55:12 +02001040 dispatched++;
1041
1042 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001043 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1044 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001045 }
1046
Jens Axboedd67d052006-06-21 09:36:18 +02001047 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001048 break;
1049
1050 } while (dispatched < max_dispatch);
1051
1052 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001053 * expire an async queue immediately if it has used up its slice. idle
1054 * queue always expire after 1 dispatch round.
1055 */
Jens Axboea9938002007-04-20 08:55:52 +02001056 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe20e493a2007-04-23 08:26:36 +02001057 dispatched >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001058 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001059 cfqq->slice_end = jiffies + 1;
Jens Axboe6084cdd2007-04-23 08:25:00 +02001060 cfq_slice_expired(cfqd, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001061 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001062
1063 return dispatched;
1064}
1065
Jens Axboed9e76202007-04-20 14:27:50 +02001066static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1067{
1068 int dispatched = 0;
1069
1070 while (cfqq->next_rq) {
1071 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1072 dispatched++;
1073 }
1074
1075 BUG_ON(!list_empty(&cfqq->fifo));
1076 return dispatched;
1077}
1078
Jens Axboe498d3aa22007-04-26 12:54:48 +02001079/*
1080 * Drain our current requests. Used for barriers and when switching
1081 * io schedulers on-the-fly.
1082 */
Jens Axboed9e76202007-04-20 14:27:50 +02001083static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001084{
Jens Axboed9e76202007-04-20 14:27:50 +02001085 int dispatched = 0;
1086 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001087
Jens Axboecc09e292007-04-26 12:53:50 +02001088 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
Jens Axboed9e76202007-04-20 14:27:50 +02001089 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1090
1091 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1092 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001093
Jens Axboe6084cdd2007-04-23 08:25:00 +02001094 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001095
1096 BUG_ON(cfqd->busy_queues);
1097
1098 return dispatched;
1099}
1100
Jens Axboed9e76202007-04-20 14:27:50 +02001101static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001104 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001105 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jens Axboe22e2c502005-06-27 10:55:12 +02001107 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001110 if (unlikely(force))
1111 return cfq_forced_dispatch(cfqd);
1112
Jens Axboecaaa5f92006-06-16 11:23:00 +02001113 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001114 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001115 int max_dispatch;
1116
Jens Axboe3ed9a292007-04-23 08:33:33 +02001117 max_dispatch = cfqd->cfq_quantum;
1118 if (cfq_class_idle(cfqq))
1119 max_dispatch = 1;
1120
1121 if (cfqq->dispatched >= max_dispatch) {
1122 if (cfqd->busy_queues > 1)
Jens Axboe6d048f52007-04-25 12:44:27 +02001123 break;
Jens Axboe3ed9a292007-04-23 08:33:33 +02001124 if (cfqq->dispatched >= 4 * max_dispatch)
Jens Axboea9938002007-04-20 08:55:52 +02001125 break;
1126 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001127
Jens Axboe3ed9a292007-04-23 08:33:33 +02001128 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
1129 break;
1130
Jens Axboe3b181522005-06-27 10:56:24 +02001131 cfq_clear_cfqq_must_dispatch(cfqq);
1132 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001133 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Jens Axboecaaa5f92006-06-16 11:23:00 +02001135 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Jens Axboecaaa5f92006-06-16 11:23:00 +02001138 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
Jens Axboe5e705372006-07-13 12:39:25 +02001142 * task holds one reference to the queue, dropped when task exits. each rq
1143 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 *
1145 * queue lock must be held here.
1146 */
1147static void cfq_put_queue(struct cfq_queue *cfqq)
1148{
Jens Axboe22e2c502005-06-27 10:55:12 +02001149 struct cfq_data *cfqd = cfqq->cfqd;
1150
1151 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (!atomic_dec_and_test(&cfqq->ref))
1154 return;
1155
1156 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001157 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001158 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001160 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001161 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001162 cfq_schedule_dispatch(cfqd);
1163 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 kmem_cache_free(cfq_pool, cfqq);
1166}
1167
Jens Axboee2d74ac2006-03-28 08:59:01 +02001168static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jens Axboe22e2c502005-06-27 10:55:12 +02001170 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001171 struct rb_node *n;
1172 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001173
Jens Axboe597bc482007-04-24 21:23:53 +02001174 ioc->ioc_data = NULL;
1175
Jens Axboee2d74ac2006-03-28 08:59:01 +02001176 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1177 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1178 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001179 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001180 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001181 }
1182
Jens Axboe4050cf12006-07-19 05:07:12 +02001183 elv_ioc_count_mod(ioc_count, -freed);
1184
1185 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001186 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Jens Axboe89850f72006-07-22 16:48:31 +02001189static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1190{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001191 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001192 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001193 cfq_schedule_dispatch(cfqd);
1194 }
Jens Axboe89850f72006-07-22 16:48:31 +02001195
1196 cfq_put_queue(cfqq);
1197}
1198
1199static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1200 struct cfq_io_context *cic)
1201{
Jens Axboefc463792006-08-29 09:05:44 +02001202 list_del_init(&cic->queue_list);
1203 smp_wmb();
1204 cic->key = NULL;
1205
Jens Axboe89850f72006-07-22 16:48:31 +02001206 if (cic->cfqq[ASYNC]) {
1207 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1208 cic->cfqq[ASYNC] = NULL;
1209 }
1210
1211 if (cic->cfqq[SYNC]) {
1212 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1213 cic->cfqq[SYNC] = NULL;
1214 }
Jens Axboe89850f72006-07-22 16:48:31 +02001215}
1216
Jens Axboe22e2c502005-06-27 10:55:12 +02001217static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1218{
Al Viro478a82b2006-03-18 13:25:24 -05001219 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001220
Jens Axboe89850f72006-07-22 16:48:31 +02001221 if (cfqd) {
1222 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001223
Jens Axboefc463792006-08-29 09:05:44 +02001224 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001225 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001226 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001227 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001228}
1229
Jens Axboe498d3aa22007-04-26 12:54:48 +02001230/*
1231 * The process that ioc belongs to has exited, we need to clean up
1232 * and put the internal structures we have that belongs to that process.
1233 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001234static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jens Axboe22e2c502005-06-27 10:55:12 +02001236 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001237 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001238
Jens Axboe597bc482007-04-24 21:23:53 +02001239 ioc->ioc_data = NULL;
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * put the reference this task is holding to the various queues
1243 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001244 n = rb_first(&ioc->cic_root);
1245 while (n != NULL) {
1246 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1247
Jens Axboe22e2c502005-06-27 10:55:12 +02001248 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001249 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Jens Axboe22e2c502005-06-27 10:55:12 +02001253static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001254cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Jens Axboeb5deef92006-07-19 23:39:40 +02001256 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christoph Lameter94f60302007-07-17 04:03:29 -07001258 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
1259 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001261 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001262 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001263 cic->dtor = cfq_free_io_context;
1264 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001265 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
1268 return cic;
1269}
1270
Jens Axboe22e2c502005-06-27 10:55:12 +02001271static void cfq_init_prio_data(struct cfq_queue *cfqq)
1272{
1273 struct task_struct *tsk = current;
1274 int ioprio_class;
1275
Jens Axboe3b181522005-06-27 10:56:24 +02001276 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001277 return;
1278
1279 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1280 switch (ioprio_class) {
1281 default:
1282 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1283 case IOPRIO_CLASS_NONE:
1284 /*
1285 * no prio set, place us in the middle of the BE classes
1286 */
1287 cfqq->ioprio = task_nice_ioprio(tsk);
1288 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1289 break;
1290 case IOPRIO_CLASS_RT:
1291 cfqq->ioprio = task_ioprio(tsk);
1292 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1293 break;
1294 case IOPRIO_CLASS_BE:
1295 cfqq->ioprio = task_ioprio(tsk);
1296 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1297 break;
1298 case IOPRIO_CLASS_IDLE:
1299 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1300 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001301 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001302 break;
1303 }
1304
1305 /*
1306 * keep track of original prio settings in case we have to temporarily
1307 * elevate the priority of this queue
1308 */
1309 cfqq->org_ioprio = cfqq->ioprio;
1310 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001311 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001312}
1313
Al Viro478a82b2006-03-18 13:25:24 -05001314static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001315{
Al Viro478a82b2006-03-18 13:25:24 -05001316 struct cfq_data *cfqd = cic->key;
1317 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001318 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001319
Jens Axboecaaa5f92006-06-16 11:23:00 +02001320 if (unlikely(!cfqd))
1321 return;
1322
Jens Axboec1b707d2006-10-30 19:54:23 +01001323 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001324
1325 cfqq = cic->cfqq[ASYNC];
1326 if (cfqq) {
1327 struct cfq_queue *new_cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001328 new_cfqq = cfq_get_queue(cfqd, ASYNC, cic->ioc->task,
Jens Axboecaaa5f92006-06-16 11:23:00 +02001329 GFP_ATOMIC);
1330 if (new_cfqq) {
1331 cic->cfqq[ASYNC] = new_cfqq;
1332 cfq_put_queue(cfqq);
1333 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001334 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001335
1336 cfqq = cic->cfqq[SYNC];
1337 if (cfqq)
1338 cfq_mark_cfqq_prio_changed(cfqq);
1339
Jens Axboec1b707d2006-10-30 19:54:23 +01001340 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001341}
1342
Jens Axboefc463792006-08-29 09:05:44 +02001343static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Al Viroa6a07632006-03-18 13:26:44 -05001345 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001346 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001347
Jens Axboefc463792006-08-29 09:05:44 +02001348 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001349
Jens Axboee2d74ac2006-03-28 08:59:01 +02001350 n = rb_first(&ioc->cic_root);
1351 while (n != NULL) {
1352 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001353
Al Viro478a82b2006-03-18 13:25:24 -05001354 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001355 n = rb_next(n);
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359static struct cfq_queue *
Jens Axboe15c31be2007-07-10 13:43:25 +02001360cfq_find_alloc_queue(struct cfq_data *cfqd, int is_sync,
1361 struct task_struct *tsk, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001364 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366retry:
Vasily Tarasov91fac312007-04-25 12:29:51 +02001367 cic = cfq_cic_rb_lookup(cfqd, tsk->io_context);
1368 /* cic always exists here */
1369 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (!cfqq) {
1372 if (new_cfqq) {
1373 cfqq = new_cfqq;
1374 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001375 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001376 /*
1377 * Inform the allocator of the fact that we will
1378 * just repeat this allocation if it fails, to allow
1379 * the allocator to do whatever it needs to attempt to
1380 * free memory.
1381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07001383 new_cfqq = kmem_cache_alloc_node(cfq_pool,
1384 gfp_mask | __GFP_NOFAIL | __GFP_ZERO,
1385 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_lock_irq(cfqd->queue->queue_lock);
1387 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001388 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07001389 cfqq = kmem_cache_alloc_node(cfq_pool,
1390 gfp_mask | __GFP_ZERO,
1391 cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001392 if (!cfqq)
1393 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Jens Axboed9e76202007-04-20 14:27:50 +02001396 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001397 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 atomic_set(&cfqq->ref, 0);
1400 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001401
Vasily Tarasov91fac312007-04-25 12:29:51 +02001402 if (is_sync) {
Jens Axboea9938002007-04-20 08:55:52 +02001403 cfq_mark_cfqq_idle_window(cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001404 cfq_mark_cfqq_sync(cfqq);
1405 }
Jens Axboea9938002007-04-20 08:55:52 +02001406
Jens Axboe3b181522005-06-27 10:56:24 +02001407 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b037442006-07-28 09:48:51 +02001408 cfq_mark_cfqq_queue_new(cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001409
Jens Axboe3b181522005-06-27 10:56:24 +02001410 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
1413 if (new_cfqq)
1414 kmem_cache_free(cfq_pool, new_cfqq);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416out:
1417 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1418 return cfqq;
1419}
1420
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001421static struct cfq_queue **
1422cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
1423{
1424 switch(ioprio_class) {
1425 case IOPRIO_CLASS_RT:
1426 return &cfqd->async_cfqq[0][ioprio];
1427 case IOPRIO_CLASS_BE:
1428 return &cfqd->async_cfqq[1][ioprio];
1429 case IOPRIO_CLASS_IDLE:
1430 return &cfqd->async_idle_cfqq;
1431 default:
1432 BUG();
1433 }
1434}
1435
Jens Axboe15c31be2007-07-10 13:43:25 +02001436static struct cfq_queue *
1437cfq_get_queue(struct cfq_data *cfqd, int is_sync, struct task_struct *tsk,
1438 gfp_t gfp_mask)
1439{
1440 const int ioprio = task_ioprio(tsk);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001441 const int ioprio_class = task_ioprio_class(tsk);
1442 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02001443 struct cfq_queue *cfqq = NULL;
1444
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001445 if (!is_sync) {
1446 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
1447 cfqq = *async_cfqq;
1448 }
1449
Jens Axboe15c31be2007-07-10 13:43:25 +02001450 if (!cfqq)
1451 cfqq = cfq_find_alloc_queue(cfqd, is_sync, tsk, gfp_mask);
1452
1453 /*
1454 * pin the queue now that it's allocated, scheduler exit will prune it
1455 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001456 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02001457 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001458 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02001459 }
1460
1461 atomic_inc(&cfqq->ref);
1462 return cfqq;
1463}
1464
Jens Axboe498d3aa22007-04-26 12:54:48 +02001465/*
1466 * We drop cfq io contexts lazily, so we may find a dead one.
1467 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001468static void
1469cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1470{
Jens Axboefc463792006-08-29 09:05:44 +02001471 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02001472
1473 if (ioc->ioc_data == cic)
1474 ioc->ioc_data = NULL;
1475
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001476 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001477 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001478 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001479}
1480
Jens Axboee2d74ac2006-03-28 08:59:01 +02001481static struct cfq_io_context *
1482cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1483{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001484 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001485 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001486 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001487
Vasily Tarasov91fac312007-04-25 12:29:51 +02001488 if (unlikely(!ioc))
1489 return NULL;
1490
Jens Axboe597bc482007-04-24 21:23:53 +02001491 /*
1492 * we maintain a last-hit cache, to avoid browsing over the tree
1493 */
1494 cic = ioc->ioc_data;
1495 if (cic && cic->key == cfqd)
1496 return cic;
1497
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001498restart:
1499 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001500 while (n) {
1501 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001502 /* ->key must be copied to avoid race with cfq_exit_queue() */
1503 k = cic->key;
1504 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001505 cfq_drop_dead_cic(ioc, cic);
1506 goto restart;
1507 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001508
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001509 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001510 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001511 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001512 n = n->rb_right;
Jens Axboe597bc482007-04-24 21:23:53 +02001513 else {
1514 ioc->ioc_data = cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001515 return cic;
Jens Axboe597bc482007-04-24 21:23:53 +02001516 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001517 }
1518
1519 return NULL;
1520}
1521
1522static inline void
1523cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1524 struct cfq_io_context *cic)
1525{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001526 struct rb_node **p;
1527 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001528 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001529 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001530 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001531
Jens Axboee2d74ac2006-03-28 08:59:01 +02001532 cic->ioc = ioc;
1533 cic->key = cfqd;
1534
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001535restart:
1536 parent = NULL;
1537 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001538 while (*p) {
1539 parent = *p;
1540 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001541 /* ->key must be copied to avoid race with cfq_exit_queue() */
1542 k = __cic->key;
1543 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001544 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001545 goto restart;
1546 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001547
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001548 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001549 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001550 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001551 p = &(*p)->rb_right;
1552 else
1553 BUG();
1554 }
1555
1556 rb_link_node(&cic->rb_node, parent, p);
1557 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001558
Jens Axboe0261d682006-10-30 19:07:48 +01001559 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001560 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001561 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001562}
1563
Jens Axboe22e2c502005-06-27 10:55:12 +02001564/*
1565 * Setup general io context and cfq io context. There can be several cfq
1566 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001567 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001568 */
1569static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001570cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Jens Axboe22e2c502005-06-27 10:55:12 +02001572 struct io_context *ioc = NULL;
1573 struct cfq_io_context *cic;
1574
1575 might_sleep_if(gfp_mask & __GFP_WAIT);
1576
Jens Axboeb5deef92006-07-19 23:39:40 +02001577 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001578 if (!ioc)
1579 return NULL;
1580
Jens Axboee2d74ac2006-03-28 08:59:01 +02001581 cic = cfq_cic_rb_lookup(cfqd, ioc);
1582 if (cic)
1583 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001584
Jens Axboee2d74ac2006-03-28 08:59:01 +02001585 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1586 if (cic == NULL)
1587 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001588
Jens Axboee2d74ac2006-03-28 08:59:01 +02001589 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001590out:
Jens Axboefc463792006-08-29 09:05:44 +02001591 smp_read_barrier_depends();
1592 if (unlikely(ioc->ioprio_changed))
1593 cfq_ioc_set_ioprio(ioc);
1594
Jens Axboe22e2c502005-06-27 10:55:12 +02001595 return cic;
1596err:
1597 put_io_context(ioc);
1598 return NULL;
1599}
1600
1601static void
1602cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1603{
Jens Axboeaaf12282007-01-19 11:30:16 +11001604 unsigned long elapsed = jiffies - cic->last_end_request;
1605 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001606
1607 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1608 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1609 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1610}
1611
Jens Axboe206dc692006-03-28 13:03:44 +02001612static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001613cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1614 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001615{
1616 sector_t sdist;
1617 u64 total;
1618
Jens Axboe5e705372006-07-13 12:39:25 +02001619 if (cic->last_request_pos < rq->sector)
1620 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001621 else
Jens Axboe5e705372006-07-13 12:39:25 +02001622 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001623
Jens Axboe6d048f52007-04-25 12:44:27 +02001624 if (!cic->seek_samples) {
1625 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1626 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1627 }
1628
Jens Axboe206dc692006-03-28 13:03:44 +02001629 /*
1630 * Don't allow the seek distance to get too large from the
1631 * odd fragment, pagein, etc
1632 */
1633 if (cic->seek_samples <= 60) /* second&third seek */
1634 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1635 else
1636 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1637
1638 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1639 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1640 total = cic->seek_total + (cic->seek_samples/2);
1641 do_div(total, cic->seek_samples);
1642 cic->seek_mean = (sector_t)total;
1643}
Jens Axboe22e2c502005-06-27 10:55:12 +02001644
1645/*
1646 * Disable idle window if the process thinks too long or seeks so much that
1647 * it doesn't matter
1648 */
1649static void
1650cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1651 struct cfq_io_context *cic)
1652{
Jens Axboe1be92f2f2007-04-19 14:32:26 +02001653 int enable_idle;
1654
1655 if (!cfq_cfqq_sync(cfqq))
1656 return;
1657
1658 enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001659
Jens Axboecaaa5f92006-06-16 11:23:00 +02001660 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1661 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001662 enable_idle = 0;
1663 else if (sample_valid(cic->ttime_samples)) {
1664 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1665 enable_idle = 0;
1666 else
1667 enable_idle = 1;
1668 }
1669
Jens Axboe3b181522005-06-27 10:56:24 +02001670 if (enable_idle)
1671 cfq_mark_cfqq_idle_window(cfqq);
1672 else
1673 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001674}
1675
Jens Axboe22e2c502005-06-27 10:55:12 +02001676/*
1677 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1678 * no or if we aren't sure, a 1 will cause a preempt.
1679 */
1680static int
1681cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001682 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001683{
Jens Axboe6d048f52007-04-25 12:44:27 +02001684 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001685
Jens Axboe6d048f52007-04-25 12:44:27 +02001686 cfqq = cfqd->active_queue;
1687 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001688 return 0;
1689
Jens Axboe6d048f52007-04-25 12:44:27 +02001690 if (cfq_slice_used(cfqq))
1691 return 1;
1692
1693 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001694 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001695
1696 if (cfq_class_idle(cfqq))
1697 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001698
Jens Axboe22e2c502005-06-27 10:55:12 +02001699 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001700 * if the new request is sync, but the currently running queue is
1701 * not, let the sync request have priority.
1702 */
Jens Axboe5e705372006-07-13 12:39:25 +02001703 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001704 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001705
Jens Axboe374f84a2006-07-23 01:42:19 +02001706 /*
1707 * So both queues are sync. Let the new request get disk time if
1708 * it's a metadata request and the current queue is doing regular IO.
1709 */
1710 if (rq_is_meta(rq) && !cfqq->meta_pending)
1711 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001712
Jens Axboe1e3335d2007-02-14 19:59:49 +01001713 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1714 return 0;
1715
1716 /*
1717 * if this request is as-good as one we would expect from the
1718 * current cfqq, let it preempt
1719 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001720 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001721 return 1;
1722
Jens Axboe22e2c502005-06-27 10:55:12 +02001723 return 0;
1724}
1725
1726/*
1727 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1728 * let it have half of its nominal slice.
1729 */
1730static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1731{
Jens Axboe6084cdd2007-04-23 08:25:00 +02001732 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001733
Jens Axboebf572252006-07-19 20:29:12 +02001734 /*
1735 * Put the new queue at the front of the of the current list,
1736 * so we know that it will be selected next.
1737 */
1738 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02001739
1740 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02001741
Jens Axboe44f7c162007-01-19 11:51:58 +11001742 cfqq->slice_end = 0;
1743 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001744}
1745
1746/*
Jens Axboe5e705372006-07-13 12:39:25 +02001747 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001748 * something we should do about it
1749 */
1750static void
Jens Axboe5e705372006-07-13 12:39:25 +02001751cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1752 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001753{
Jens Axboe5e705372006-07-13 12:39:25 +02001754 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001755
Jens Axboe374f84a2006-07-23 01:42:19 +02001756 if (rq_is_meta(rq))
1757 cfqq->meta_pending++;
1758
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001759 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001760 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001761 cfq_update_idle_window(cfqd, cfqq, cic);
1762
Jens Axboe5e705372006-07-13 12:39:25 +02001763 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001764 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001765
1766 if (cfqq == cfqd->active_queue) {
1767 /*
1768 * if we are waiting for a request for this queue, let it rip
1769 * immediately and flag that we must not expire this queue
1770 * just now
1771 */
Jens Axboe3b181522005-06-27 10:56:24 +02001772 if (cfq_cfqq_wait_request(cfqq)) {
1773 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001774 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001775 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001776 }
Jens Axboe5e705372006-07-13 12:39:25 +02001777 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001778 /*
1779 * not the active queue - expire current slice if it is
1780 * idle and has expired it's mean thinktime or this new queue
1781 * has some old slice time left and is of higher priority
1782 */
1783 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001784 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001785 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001786 }
1787}
1788
Jens Axboeb4878f22005-10-20 16:42:29 +02001789static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001790{
Jens Axboeb4878f22005-10-20 16:42:29 +02001791 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001792 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001793
1794 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jens Axboe5e705372006-07-13 12:39:25 +02001796 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Jens Axboe22e2c502005-06-27 10:55:12 +02001798 list_add_tail(&rq->queuelist, &cfqq->fifo);
1799
Jens Axboe5e705372006-07-13 12:39:25 +02001800 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803static void cfq_completed_request(request_queue_t *q, struct request *rq)
1804{
Jens Axboe5e705372006-07-13 12:39:25 +02001805 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001806 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001807 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001808 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jens Axboeb4878f22005-10-20 16:42:29 +02001810 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Jens Axboeb4878f22005-10-20 16:42:29 +02001812 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001813 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001814 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001815 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Jens Axboe3ed9a292007-04-23 08:33:33 +02001817 if (cfq_cfqq_sync(cfqq))
1818 cfqd->sync_flight--;
1819
Jens Axboeb4878f22005-10-20 16:42:29 +02001820 if (!cfq_class_idle(cfqq))
1821 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001822
Jens Axboecaaa5f92006-06-16 11:23:00 +02001823 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001824 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001825
1826 /*
1827 * If this is the active queue, check if it needs to be expired,
1828 * or if we want to idle in case it has no pending requests.
1829 */
1830 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001831 if (cfq_cfqq_slice_new(cfqq)) {
1832 cfq_set_prio_slice(cfqd, cfqq);
1833 cfq_clear_cfqq_slice_new(cfqq);
1834 }
1835 if (cfq_slice_used(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02001836 cfq_slice_expired(cfqd, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001837 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1838 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001839 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001840
1841 if (!cfqd->rq_in_driver)
1842 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Jens Axboe22e2c502005-06-27 10:55:12 +02001845/*
1846 * we temporarily boost lower priority queues if they are holding fs exclusive
1847 * resources. they are boosted to normal prio (CLASS_BE/4)
1848 */
1849static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Jens Axboe22e2c502005-06-27 10:55:12 +02001851 if (has_fs_excl()) {
1852 /*
1853 * boost idle prio on transactions that would lock out other
1854 * users of the filesystem
1855 */
1856 if (cfq_class_idle(cfqq))
1857 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1858 if (cfqq->ioprio > IOPRIO_NORM)
1859 cfqq->ioprio = IOPRIO_NORM;
1860 } else {
1861 /*
1862 * check if we need to unboost the queue
1863 */
1864 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1865 cfqq->ioprio_class = cfqq->org_ioprio_class;
1866 if (cfqq->ioprio != cfqq->org_ioprio)
1867 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001869}
1870
Jens Axboe89850f72006-07-22 16:48:31 +02001871static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001872{
Jens Axboe3b181522005-06-27 10:56:24 +02001873 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001874 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001875 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001876 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001877 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001878
1879 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001880}
1881
Jens Axboecb78b282006-07-28 09:32:57 +02001882static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001883{
1884 struct cfq_data *cfqd = q->elevator->elevator_data;
1885 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001886 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001887 struct cfq_queue *cfqq;
1888
1889 /*
1890 * don't force setup of a queue from here, as a call to may_queue
1891 * does not necessarily imply that a request actually will be queued.
1892 * so just lookup a possibly existing queue, or return 'may queue'
1893 * if that fails
1894 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001895 cic = cfq_cic_rb_lookup(cfqd, tsk->io_context);
1896 if (!cic)
1897 return ELV_MQUEUE_MAY;
1898
1899 cfqq = cic_to_cfqq(cic, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001900 if (cfqq) {
1901 cfq_init_prio_data(cfqq);
1902 cfq_prio_boost(cfqq);
1903
Jens Axboe89850f72006-07-22 16:48:31 +02001904 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001905 }
1906
1907 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910/*
1911 * queue lock held here
1912 */
Jens Axboebb37b942006-12-01 10:42:33 +01001913static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Jens Axboe5e705372006-07-13 12:39:25 +02001915 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Jens Axboe5e705372006-07-13 12:39:25 +02001917 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001918 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jens Axboe22e2c502005-06-27 10:55:12 +02001920 BUG_ON(!cfqq->allocated[rw]);
1921 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Jens Axboe5e705372006-07-13 12:39:25 +02001923 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001926 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 cfq_put_queue(cfqq);
1929 }
1930}
1931
1932/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001933 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001935static int
Jens Axboecb78b282006-07-28 09:32:57 +02001936cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001939 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct cfq_io_context *cic;
1941 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001942 const int is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001943 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 unsigned long flags;
1945
1946 might_sleep_if(gfp_mask & __GFP_WAIT);
1947
Jens Axboee2d74ac2006-03-28 08:59:01 +02001948 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 spin_lock_irqsave(q->queue_lock, flags);
1951
Jens Axboe22e2c502005-06-27 10:55:12 +02001952 if (!cic)
1953 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Vasily Tarasov91fac312007-04-25 12:29:51 +02001955 cfqq = cic_to_cfqq(cic, is_sync);
1956 if (!cfqq) {
1957 cfqq = cfq_get_queue(cfqd, is_sync, tsk, gfp_mask);
1958
Jens Axboe22e2c502005-06-27 10:55:12 +02001959 if (!cfqq)
1960 goto queue_fail;
1961
Vasily Tarasov91fac312007-04-25 12:29:51 +02001962 cic_set_cfqq(cic, cfqq, is_sync);
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001966 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001967 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 spin_unlock_irqrestore(q->queue_lock, flags);
1970
Jens Axboe5e705372006-07-13 12:39:25 +02001971 rq->elevator_private = cic;
1972 rq->elevator_private2 = cfqq;
1973 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001974
Jens Axboe22e2c502005-06-27 10:55:12 +02001975queue_fail:
1976 if (cic)
1977 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001978
Jens Axboe3b181522005-06-27 10:56:24 +02001979 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 spin_unlock_irqrestore(q->queue_lock, flags);
1981 return 1;
1982}
1983
David Howells65f27f32006-11-22 14:55:48 +00001984static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001985{
David Howells65f27f32006-11-22 14:55:48 +00001986 struct cfq_data *cfqd =
1987 container_of(work, struct cfq_data, unplug_work);
1988 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001989 unsigned long flags;
1990
1991 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001992 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001993 spin_unlock_irqrestore(q->queue_lock, flags);
1994}
1995
1996/*
1997 * Timer running if the active_queue is currently idling inside its time slice
1998 */
1999static void cfq_idle_slice_timer(unsigned long data)
2000{
2001 struct cfq_data *cfqd = (struct cfq_data *) data;
2002 struct cfq_queue *cfqq;
2003 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002004 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02002005
2006 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2007
2008 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002009 timed_out = 0;
2010
Jens Axboe22e2c502005-06-27 10:55:12 +02002011 /*
2012 * expired
2013 */
Jens Axboe44f7c162007-01-19 11:51:58 +11002014 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002015 goto expire;
2016
2017 /*
2018 * only expire and reinvoke request handler, if there are
2019 * other queues with pending requests
2020 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002021 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002022 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002023
2024 /*
2025 * not expired and it has a request pending, let it dispatch
2026 */
Jens Axboedd67d052006-06-21 09:36:18 +02002027 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002028 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002029 goto out_kick;
2030 }
2031 }
2032expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002033 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002034out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002035 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002036out_cont:
2037 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2038}
2039
2040/*
2041 * Timer running if an idle class queue is waiting for service
2042 */
2043static void cfq_idle_class_timer(unsigned long data)
2044{
2045 struct cfq_data *cfqd = (struct cfq_data *) data;
2046 unsigned long flags, end;
2047
2048 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2049
2050 /*
2051 * race with a non-idle queue, reset timer
2052 */
2053 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002054 if (!time_after_eq(jiffies, end))
2055 mod_timer(&cfqd->idle_class_timer, end);
2056 else
Jens Axboe3b181522005-06-27 10:56:24 +02002057 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002058
2059 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2060}
2061
Jens Axboe3b181522005-06-27 10:56:24 +02002062static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2063{
2064 del_timer_sync(&cfqd->idle_slice_timer);
2065 del_timer_sync(&cfqd->idle_class_timer);
2066 blk_sync_queue(cfqd->queue);
2067}
Jens Axboe22e2c502005-06-27 10:55:12 +02002068
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002069static void cfq_put_async_queues(struct cfq_data *cfqd)
2070{
2071 int i;
2072
2073 for (i = 0; i < IOPRIO_BE_NR; i++) {
2074 if (cfqd->async_cfqq[0][i])
2075 cfq_put_queue(cfqd->async_cfqq[0][i]);
2076 if (cfqd->async_cfqq[1][i])
2077 cfq_put_queue(cfqd->async_cfqq[1][i]);
2078 if (cfqd->async_idle_cfqq)
2079 cfq_put_queue(cfqd->async_idle_cfqq);
2080 }
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static void cfq_exit_queue(elevator_t *e)
2084{
Jens Axboe22e2c502005-06-27 10:55:12 +02002085 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002086 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002087
Jens Axboe3b181522005-06-27 10:56:24 +02002088 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002089
Al Virod9ff4182006-03-18 13:51:22 -05002090 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002091
Al Virod9ff4182006-03-18 13:51:22 -05002092 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02002093 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002094
2095 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002096 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2097 struct cfq_io_context,
2098 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002099
2100 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002101 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002102
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002103 cfq_put_async_queues(cfqd);
Jens Axboe15c31be2007-07-10 13:43:25 +02002104
Al Virod9ff4182006-03-18 13:51:22 -05002105 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002106
2107 cfq_shutdown_timer_wq(cfqd);
2108
Al Viroa90d7422006-03-18 12:05:37 -05002109 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Jens Axboebb37b942006-12-01 10:42:33 +01002112static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 struct cfq_data *cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Christoph Lameter94f60302007-07-17 04:03:29 -07002116 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jens Axboecc09e292007-04-26 12:53:50 +02002120 cfqd->service_tree = CFQ_RB_ROOT;
Al Virod9ff4182006-03-18 13:51:22 -05002121 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Jens Axboe22e2c502005-06-27 10:55:12 +02002125 init_timer(&cfqd->idle_slice_timer);
2126 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2127 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2128
2129 init_timer(&cfqd->idle_class_timer);
2130 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2131 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2132
David Howells65f27f32006-11-22 14:55:48 +00002133 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002136 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2137 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 cfqd->cfq_back_max = cfq_back_max;
2139 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002140 cfqd->cfq_slice[0] = cfq_slice_async;
2141 cfqd->cfq_slice[1] = cfq_slice_sync;
2142 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2143 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002144
Jens Axboebc1c1162006-06-08 08:49:06 +02002145 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148static void cfq_slab_kill(void)
2149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (cfq_pool)
2151 kmem_cache_destroy(cfq_pool);
2152 if (cfq_ioc_pool)
2153 kmem_cache_destroy(cfq_ioc_pool);
2154}
2155
2156static int __init cfq_slab_setup(void)
2157{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002158 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (!cfq_pool)
2160 goto fail;
2161
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002162 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (!cfq_ioc_pool)
2164 goto fail;
2165
2166 return 0;
2167fail:
2168 cfq_slab_kill();
2169 return -ENOMEM;
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172/*
2173 * sysfs parts below -->
2174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175static ssize_t
2176cfq_var_show(unsigned int var, char *page)
2177{
2178 return sprintf(page, "%d\n", var);
2179}
2180
2181static ssize_t
2182cfq_var_store(unsigned int *var, const char *page, size_t count)
2183{
2184 char *p = (char *) page;
2185
2186 *var = simple_strtoul(p, &p, 10);
2187 return count;
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002191static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002193 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 unsigned int __data = __VAR; \
2195 if (__CONV) \
2196 __data = jiffies_to_msecs(__data); \
2197 return cfq_var_show(__data, (page)); \
2198}
2199SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002200SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2201SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002202SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2203SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002204SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2205SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2206SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2207SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208#undef SHOW_FUNCTION
2209
2210#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002211static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002213 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 unsigned int __data; \
2215 int ret = cfq_var_store(&__data, (page), count); \
2216 if (__data < (MIN)) \
2217 __data = (MIN); \
2218 else if (__data > (MAX)) \
2219 __data = (MAX); \
2220 if (__CONV) \
2221 *(__PTR) = msecs_to_jiffies(__data); \
2222 else \
2223 *(__PTR) = __data; \
2224 return ret; \
2225}
2226STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002227STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2228STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002229STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2230STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002231STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2232STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2233STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2234STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#undef STORE_FUNCTION
2236
Al Viroe572ec72006-03-18 22:27:18 -05002237#define CFQ_ATTR(name) \
2238 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002239
Al Viroe572ec72006-03-18 22:27:18 -05002240static struct elv_fs_entry cfq_attrs[] = {
2241 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002242 CFQ_ATTR(fifo_expire_sync),
2243 CFQ_ATTR(fifo_expire_async),
2244 CFQ_ATTR(back_seek_max),
2245 CFQ_ATTR(back_seek_penalty),
2246 CFQ_ATTR(slice_sync),
2247 CFQ_ATTR(slice_async),
2248 CFQ_ATTR(slice_async_rq),
2249 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002250 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251};
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253static struct elevator_type iosched_cfq = {
2254 .ops = {
2255 .elevator_merge_fn = cfq_merge,
2256 .elevator_merged_fn = cfq_merged_request,
2257 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002258 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002259 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002261 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 .elevator_deactivate_req_fn = cfq_deactivate_request,
2263 .elevator_queue_empty_fn = cfq_queue_empty,
2264 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002265 .elevator_former_req_fn = elv_rb_former_request,
2266 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 .elevator_set_req_fn = cfq_set_request,
2268 .elevator_put_req_fn = cfq_put_request,
2269 .elevator_may_queue_fn = cfq_may_queue,
2270 .elevator_init_fn = cfq_init_queue,
2271 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002272 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 },
Al Viro3d1ab402006-03-18 18:35:43 -05002274 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 .elevator_name = "cfq",
2276 .elevator_owner = THIS_MODULE,
2277};
2278
2279static int __init cfq_init(void)
2280{
2281 int ret;
2282
Jens Axboe22e2c502005-06-27 10:55:12 +02002283 /*
2284 * could be 0 on HZ < 1000 setups
2285 */
2286 if (!cfq_slice_async)
2287 cfq_slice_async = 1;
2288 if (!cfq_slice_idle)
2289 cfq_slice_idle = 1;
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (cfq_slab_setup())
2292 return -ENOMEM;
2293
2294 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002295 if (ret)
2296 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return ret;
2299}
2300
2301static void __exit cfq_exit(void)
2302{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002303 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002305 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002306 /* ioc_gone's update must be visible before reading ioc_count */
2307 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002308 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002309 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002310 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002311 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314module_init(cfq_init);
2315module_exit(cfq_exit);
2316
2317MODULE_AUTHOR("Jens Axboe");
2318MODULE_LICENSE("GPL");
2319MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");