blob: 28236f2cd908a72b3c51a01f69cbd689769a944a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050010#include <linux/blkdev.h>
11#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hash.h>
13#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * tunables
18 */
Arjan van de Ven64100092006-01-06 09:46:02 +010019static const int cfq_quantum = 4; /* max queue in one round of service */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven64100092006-01-06 09:46:02 +010024static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020025static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020027static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020028
29#define CFQ_IDLE_GRACE (HZ / 10)
30#define CFQ_SLICE_SCALE (5)
31
32#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * for the hash of cfqq inside the cfqd
36 */
37#define CFQ_QHASH_SHIFT 6
38#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
39#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
42
Jens Axboe5e705372006-07-13 12:39:25 +020043#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
44#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *cfq_pool;
47static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jens Axboe4050cf12006-07-19 05:07:12 +020049static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050050static struct completion *ioc_gone;
51
Jens Axboe22e2c502005-06-27 10:55:12 +020052#define CFQ_PRIO_LISTS IOPRIO_BE_NR
53#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020054#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
55
Jens Axboe3b181522005-06-27 10:56:24 +020056#define ASYNC (0)
57#define SYNC (1)
58
Jens Axboe6d048f52007-04-25 12:44:27 +020059#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
Jens Axboe22e2c502005-06-27 10:55:12 +020060
Jens Axboe206dc692006-03-28 13:03:44 +020061#define sample_valid(samples) ((samples) > 80)
62
Jens Axboe22e2c502005-06-27 10:55:12 +020063/*
64 * Per block device queue structure
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020067 request_queue_t *queue;
68
69 /*
70 * rr list of queues with requests and the count of them
71 */
72 struct list_head rr_list[CFQ_PRIO_LISTS];
73 struct list_head busy_rr;
74 struct list_head cur_rr;
75 struct list_head idle_rr;
Jens Axboe6d048f52007-04-25 12:44:27 +020076 unsigned long cur_rr_tick;
Jens Axboe22e2c502005-06-27 10:55:12 +020077 unsigned int busy_queues;
78
79 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020080 * cfqq lookup hash
81 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jens Axboe22e2c502005-06-27 10:55:12 +020084 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020085 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +020086
87 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020088 * idle window management
89 */
90 struct timer_list idle_slice_timer;
91 struct work_struct unplug_work;
92
93 struct cfq_queue *active_queue;
94 struct cfq_io_context *active_cic;
95 int cur_prio, cur_end_prio;
Jens Axboe6d048f52007-04-25 12:44:27 +020096 unsigned long prio_time;
Jens Axboe22e2c502005-06-27 10:55:12 +020097 unsigned int dispatch_slice;
98
99 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jens Axboe6d048f52007-04-25 12:44:27 +0200101 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200102 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * tunables, see top of file
106 */
107 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200108 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned int cfq_back_penalty;
110 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200111 unsigned int cfq_slice[2];
112 unsigned int cfq_slice_async_rq;
113 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500114
115 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200116
117 sector_t new_seek_mean;
118 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Jens Axboe22e2c502005-06-27 10:55:12 +0200121/*
122 * Per process-grouping structure
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct cfq_queue {
125 /* reference count */
126 atomic_t ref;
127 /* parent cfq_data */
128 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200129 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct hlist_node cfq_hash;
131 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200132 unsigned int key;
Jens Axboe981a7972006-07-19 14:56:28 +0200133 /* member of the rr/busy/cur/idle cfqd list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct list_head cfq_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200135 /* in what tick we were last serviced */
136 unsigned long rr_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* sorted list of pending requests */
138 struct rb_root sort_list;
139 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200140 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* requests queued in sort_list */
142 int queued[2];
143 /* currently allocated requests */
144 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200145 /* pending metadata requests */
146 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jens Axboe22e2c502005-06-27 10:55:12 +0200150 unsigned long slice_end;
Jens Axboe99f96282007-02-05 11:56:25 +0100151 unsigned long service_last;
Jens Axboe6d048f52007-04-25 12:44:27 +0200152 unsigned long slice_start;
Jens Axboec5b680f2007-01-19 11:56:49 +1100153 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jens Axboe6d048f52007-04-25 12:44:27 +0200155 /* number of requests that are on the dispatch list or inside driver */
156 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200157
158 /* io prio of this group */
159 unsigned short ioprio, org_ioprio;
160 unsigned short ioprio_class, org_ioprio_class;
161
Jens Axboe3b181522005-06-27 10:56:24 +0200162 /* various state flags, see below */
163 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200164
165 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
Jens Axboe3b181522005-06-27 10:56:24 +0200168enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100169 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
170 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
171 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
172 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
173 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
174 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
175 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
176 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
177 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100178 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200179};
180
181#define CFQ_CFQQ_FNS(name) \
182static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
183{ \
184 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
185} \
186static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
187{ \
188 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
189} \
190static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
191{ \
192 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
193}
194
195CFQ_CFQQ_FNS(on_rr);
196CFQ_CFQQ_FNS(wait_request);
197CFQ_CFQQ_FNS(must_alloc);
198CFQ_CFQQ_FNS(must_alloc_slice);
199CFQ_CFQQ_FNS(must_dispatch);
200CFQ_CFQQ_FNS(fifo_expire);
201CFQ_CFQQ_FNS(idle_window);
202CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200203CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100204CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200205#undef CFQ_CFQQ_FNS
206
Jens Axboe3b181522005-06-27 10:56:24 +0200207static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200208static void cfq_dispatch_insert(request_queue_t *, struct request *);
Al Viro6f325a12006-03-18 14:58:37 -0500209static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700212 * scheduler run of queue, if there are requests pending and no one in the
213 * driver that will restart queueing
214 */
215static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
216{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100217 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700218 kblockd_schedule_work(&cfqd->unplug_work);
219}
220
221static int cfq_queue_empty(request_queue_t *q)
222{
223 struct cfq_data *cfqd = q->elevator->elevator_data;
224
Jens Axboeb4878f22005-10-20 16:42:29 +0200225 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700226}
227
Jens Axboe7749a8d2006-12-13 13:02:26 +0100228static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200229{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100230 /*
231 * Use the per-process queue, for read requests and syncronous writes
232 */
233 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200234 return task->pid;
235
236 return CFQ_KEY_ASYNC;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100240 * Scale schedule slice based on io priority. Use the sync time slice only
241 * if a queue is marked sync and has sync io queued. A sync queue with async
242 * io only, should not get full sync slice length.
243 */
244static inline int
245cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
246{
247 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
248
249 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
250
251 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
252}
253
254static inline void
255cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
256{
257 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
Jens Axboec5b680f2007-01-19 11:56:49 +1100258 cfqq->slice_end += cfqq->slice_resid;
259
260 /*
261 * Don't carry over residual for more than one slice, we only want
262 * to slightly correct the fairness. Carrying over forever would
263 * easily introduce oscillations.
264 */
265 cfqq->slice_resid = 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200266
267 cfqq->slice_start = jiffies;
Jens Axboe44f7c162007-01-19 11:51:58 +1100268}
269
270/*
271 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
272 * isn't valid until the first request from the dispatch is activated
273 * and the slice time set.
274 */
275static inline int cfq_slice_used(struct cfq_queue *cfqq)
276{
277 if (cfq_cfqq_slice_new(cfqq))
278 return 0;
279 if (time_before(jiffies, cfqq->slice_end))
280 return 0;
281
282 return 1;
283}
284
285/*
Jens Axboe5e705372006-07-13 12:39:25 +0200286 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200288 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Jens Axboe5e705372006-07-13 12:39:25 +0200290static struct request *
291cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200295#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
296#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
297 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jens Axboe5e705372006-07-13 12:39:25 +0200299 if (rq1 == NULL || rq1 == rq2)
300 return rq2;
301 if (rq2 == NULL)
302 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200303
Jens Axboe5e705372006-07-13 12:39:25 +0200304 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
305 return rq1;
306 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
307 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200308 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
309 return rq1;
310 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
311 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jens Axboe5e705372006-07-13 12:39:25 +0200313 s1 = rq1->sector;
314 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Jens Axboe6d048f52007-04-25 12:44:27 +0200316 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * by definition, 1KiB is 2 sectors
320 */
321 back_max = cfqd->cfq_back_max * 2;
322
323 /*
324 * Strict one way elevator _except_ in the case where we allow
325 * short backward seeks which are biased as twice the cost of a
326 * similar forward seek.
327 */
328 if (s1 >= last)
329 d1 = s1 - last;
330 else if (s1 + back_max >= last)
331 d1 = (last - s1) * cfqd->cfq_back_penalty;
332 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200333 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (s2 >= last)
336 d2 = s2 - last;
337 else if (s2 + back_max >= last)
338 d2 = (last - s2) * cfqd->cfq_back_penalty;
339 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200340 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Andreas Mohre8a99052006-03-28 08:59:49 +0200344 /*
345 * By doing switch() on the bit mask "wrap" we avoid having to
346 * check two variables for all permutations: --> faster!
347 */
348 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200349 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200350 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200351 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200352 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200353 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200354 else {
355 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200356 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200357 else
Jens Axboe5e705372006-07-13 12:39:25 +0200358 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200359 }
360
361 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200362 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200363 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200364 return rq2;
365 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200366 default:
367 /*
368 * Since both rqs are wrapped,
369 * start with the one that's further behind head
370 * (--> only *one* back seek required),
371 * since back seek takes more time than forward.
372 */
373 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200374 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 else
Jens Axboe5e705372006-07-13 12:39:25 +0200376 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378}
379
380/*
381 * would be nice to take fifo expire time into account as well
382 */
Jens Axboe5e705372006-07-13 12:39:25 +0200383static struct request *
384cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
385 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Jens Axboe21183b02006-07-13 12:33:14 +0200387 struct rb_node *rbnext = rb_next(&last->rb_node);
388 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200389 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jens Axboe21183b02006-07-13 12:33:14 +0200391 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200394 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200397 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200398 else {
399 rbnext = rb_first(&cfqq->sort_list);
400 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200401 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jens Axboe21183b02006-07-13 12:33:14 +0200404 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Jens Axboe6d048f52007-04-25 12:44:27 +0200407/*
408 * This function finds out where to insert a BE queue in the service hierarchy
409 */
410static void cfq_resort_be_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
411 int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jens Axboe99f96282007-02-05 11:56:25 +0100413 struct list_head *list, *n;
414 struct cfq_queue *__cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +0200415 int add_tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jens Axboe98e41c72007-02-05 11:55:35 +0100417 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200418 * if cfqq has requests in flight, don't allow it to be
419 * found in cfq_set_active_queue before it has finished them.
420 * this is done to increase fairness between a process that
421 * has lots of io pending vs one that only generates one
422 * sporadically or synchronously
Jens Axboe98e41c72007-02-05 11:55:35 +0100423 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200424 if (cfqq->dispatched)
425 list = &cfqd->busy_rr;
426 else if (cfqq->ioprio == (cfqd->cur_prio + 1) &&
427 cfq_cfqq_sync(cfqq) &&
428 (time_before(cfqd->prio_time, cfqq->service_last) ||
429 cfq_cfqq_queue_new(cfqq) || preempted)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200430 list = &cfqd->cur_rr;
Jens Axboe6d048f52007-04-25 12:44:27 +0200431 add_tail = 1;
432 } else
433 list = &cfqd->rr_list[cfqq->ioprio];
Jens Axboe22e2c502005-06-27 10:55:12 +0200434
Jens Axboe6d048f52007-04-25 12:44:27 +0200435 if (!cfq_cfqq_sync(cfqq) || add_tail) {
436 /*
437 * async queue always goes to the end. this wont be overly
438 * unfair to writes, as the sort of the sync queue wont be
439 * allowed to pass the async queue again.
440 */
441 list_add_tail(&cfqq->cfq_list, list);
442 } else if (preempted || cfq_cfqq_queue_new(cfqq)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100443 /*
444 * If this queue was preempted or is new (never been serviced),
445 * let it be added first for fairness but beind other new
446 * queues.
447 */
448 n = list;
Jens Axboe53b03742006-07-28 09:48:51 +0200449 while (n->next != list) {
450 __cfqq = list_entry_cfqq(n->next);
451 if (!cfq_cfqq_queue_new(__cfqq))
452 break;
453
454 n = n->next;
455 }
Jens Axboe6d048f52007-04-25 12:44:27 +0200456 list_add(&cfqq->cfq_list, n);
Jens Axboe99f96282007-02-05 11:56:25 +0100457 } else {
458 /*
459 * sort by last service, but don't cross a new or async
460 * queue. we don't cross a new queue because it hasn't been
461 * service before, and we don't cross an async queue because
462 * it gets added to the end on expire.
463 */
464 n = list;
465 while ((n = n->prev) != list) {
Jens Axboe6d048f52007-04-25 12:44:27 +0200466 struct cfq_queue *__c = list_entry_cfqq(n);
Jens Axboe53b03742006-07-28 09:48:51 +0200467
Jens Axboe6d048f52007-04-25 12:44:27 +0200468 if (!cfq_cfqq_sync(__c) || !__c->service_last)
Jens Axboe99f96282007-02-05 11:56:25 +0100469 break;
Jens Axboe6d048f52007-04-25 12:44:27 +0200470 if (time_before(__c->service_last, cfqq->service_last))
Jens Axboe99f96282007-02-05 11:56:25 +0100471 break;
472 }
473 list_add(&cfqq->cfq_list, n);
Jens Axboe22e2c502005-06-27 10:55:12 +0200474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Jens Axboe6d048f52007-04-25 12:44:27 +0200477static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
478{
479 struct cfq_data *cfqd = cfqq->cfqd;
480 struct list_head *n;
481
482 /*
483 * Resorting requires the cfqq to be on the RR list already.
484 */
485 if (!cfq_cfqq_on_rr(cfqq))
486 return;
487
488 list_del(&cfqq->cfq_list);
489
490 if (cfq_class_rt(cfqq)) {
491 /*
492 * At to the front of the current list, but behind other
493 * RT queues.
494 */
495 n = &cfqd->cur_rr;
496 while (n->next != &cfqd->cur_rr)
497 if (!cfq_class_rt(cfqq))
498 break;
499
500 list_add(&cfqq->cfq_list, n);
501 } else if (cfq_class_idle(cfqq)) {
502 /*
503 * IDLE goes to the tail of the idle list
504 */
505 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
506 } else {
507 /*
508 * So we get here, ergo the queue is a regular best-effort queue
509 */
510 cfq_resort_be_queue(cfqd, cfqq, preempted);
511 }
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
515 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200516 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200519cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jens Axboe3b181522005-06-27 10:56:24 +0200521 BUG_ON(cfq_cfqq_on_rr(cfqq));
522 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 cfqd->busy_queues++;
524
Jens Axboeb4878f22005-10-20 16:42:29 +0200525 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528static inline void
529cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
530{
Jens Axboe3b181522005-06-27 10:56:24 +0200531 BUG_ON(!cfq_cfqq_on_rr(cfqq));
532 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe981a7972006-07-19 14:56:28 +0200533 list_del_init(&cfqq->cfq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 BUG_ON(!cfqd->busy_queues);
536 cfqd->busy_queues--;
537}
538
539/*
540 * rb tree support functions
541 */
Jens Axboe5e705372006-07-13 12:39:25 +0200542static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jens Axboe5e705372006-07-13 12:39:25 +0200544 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200545 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200546 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jens Axboeb4878f22005-10-20 16:42:29 +0200548 BUG_ON(!cfqq->queued[sync]);
549 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jens Axboe5e705372006-07-13 12:39:25 +0200551 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jens Axboedd67d052006-06-21 09:36:18 +0200553 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200554 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Jens Axboe5e705372006-07-13 12:39:25 +0200557static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jens Axboe5e705372006-07-13 12:39:25 +0200559 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200561 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jens Axboe5380a102006-07-13 12:37:56 +0200563 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /*
566 * looks a little odd, but the first insert might return an alias.
567 * if that happens, put the alias on the dispatch list
568 */
Jens Axboe21183b02006-07-13 12:33:14 +0200569 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200570 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100571
572 if (!cfq_cfqq_on_rr(cfqq))
573 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200574
575 /*
576 * check if this request is a better next-serve candidate
577 */
578 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
579 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200583cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jens Axboe5380a102006-07-13 12:37:56 +0200585 elv_rb_del(&cfqq->sort_list, rq);
586 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200587 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Jens Axboe206dc692006-03-28 13:03:44 +0200590static struct request *
591cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jens Axboe206dc692006-03-28 13:03:44 +0200593 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100594 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200595 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jens Axboe206dc692006-03-28 13:03:44 +0200597 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200598 if (cfqq) {
599 sector_t sector = bio->bi_sector + bio_sectors(bio);
600
Jens Axboe21183b02006-07-13 12:33:14 +0200601 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return NULL;
605}
606
Jens Axboeb4878f22005-10-20 16:42:29 +0200607static void cfq_activate_request(request_queue_t *q, struct request *rq)
608{
609 struct cfq_data *cfqd = q->elevator->elevator_data;
610
611 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200612
613 /*
614 * If the depth is larger 1, it really could be queueing. But lets
615 * make the mark a little higher - idling could still be good for
616 * low queueing, and a low queueing number could also just indicate
617 * a SCSI mid layer like behaviour where limit+1 is often seen.
618 */
619 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
620 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200621
622 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
626{
Jens Axboe22e2c502005-06-27 10:55:12 +0200627 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jens Axboeb4878f22005-10-20 16:42:29 +0200629 WARN_ON(!cfqd->rq_in_driver);
630 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Jens Axboeb4878f22005-10-20 16:42:29 +0200633static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jens Axboe5e705372006-07-13 12:39:25 +0200635 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200636
Jens Axboe5e705372006-07-13 12:39:25 +0200637 if (cfqq->next_rq == rq)
638 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jens Axboeb4878f22005-10-20 16:42:29 +0200640 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200641 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200642
643 if (rq_is_meta(rq)) {
644 WARN_ON(!cfqq->meta_pending);
645 cfqq->meta_pending--;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static int
650cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
651{
652 struct cfq_data *cfqd = q->elevator->elevator_data;
653 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jens Axboe206dc692006-03-28 13:03:44 +0200655 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200656 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200657 *req = __rq;
658 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Jens Axboe21183b02006-07-13 12:33:14 +0200664static void cfq_merged_request(request_queue_t *q, struct request *req,
665 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Jens Axboe21183b02006-07-13 12:33:14 +0200667 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200668 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jens Axboe5e705372006-07-13 12:39:25 +0200670 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674static void
675cfq_merged_requests(request_queue_t *q, struct request *rq,
676 struct request *next)
677{
Jens Axboe22e2c502005-06-27 10:55:12 +0200678 /*
679 * reposition in fifo if next is older than rq
680 */
681 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
682 time_before(next->start_time, rq->start_time))
683 list_move(&rq->queuelist, &next->queuelist);
684
Jens Axboeb4878f22005-10-20 16:42:29 +0200685 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200686}
687
Jens Axboeda775262006-12-20 11:04:12 +0100688static int cfq_allow_merge(request_queue_t *q, struct request *rq,
689 struct bio *bio)
690{
691 struct cfq_data *cfqd = q->elevator->elevator_data;
692 const int rw = bio_data_dir(bio);
693 struct cfq_queue *cfqq;
694 pid_t key;
695
696 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100697 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100698 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100699 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100700 return 0;
701
702 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100703 * Lookup the cfqq that this bio will be queued with. Allow
704 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100705 */
Jens Axboe719d3402006-12-22 09:38:53 +0100706 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100707 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100708
709 if (cfqq == RQ_CFQQ(rq))
710 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100711
Jens Axboeec8acb62007-01-02 18:32:11 +0100712 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100713}
714
Jens Axboe22e2c502005-06-27 10:55:12 +0200715static inline void
716__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
717{
718 if (cfqq) {
719 /*
720 * stop potential idle class queues waiting service
721 */
722 del_timer(&cfqd->idle_class_timer);
723
Jens Axboe22e2c502005-06-27 10:55:12 +0200724 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200725 cfq_clear_cfqq_must_alloc_slice(cfqq);
726 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100727 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200728 cfqq->rr_tick = cfqd->cur_rr_tick;
Jens Axboe22e2c502005-06-27 10:55:12 +0200729 }
730
731 cfqd->active_queue = cfqq;
732}
733
734/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100735 * current cfqq expired its slice (or was too idle), select new one
736 */
737static void
738__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100739 int preempted, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100740{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100741 if (cfq_cfqq_wait_request(cfqq))
742 del_timer(&cfqd->idle_slice_timer);
743
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100744 cfq_clear_cfqq_must_dispatch(cfqq);
745 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +0200746 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100747
748 /*
749 * store what was left of this slice, if the queue idled out
750 * or was preempted
751 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100752 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100753 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100754
Jens Axboe98e41c72007-02-05 11:55:35 +0100755 cfq_resort_rr_list(cfqq, preempted);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100756
757 if (cfqq == cfqd->active_queue)
758 cfqd->active_queue = NULL;
759
760 if (cfqd->active_cic) {
761 put_io_context(cfqd->active_cic->ioc);
762 cfqd->active_cic = NULL;
763 }
764
765 cfqd->dispatch_slice = 0;
766}
767
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100768static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
769 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100770{
771 struct cfq_queue *cfqq = cfqd->active_queue;
772
773 if (cfqq)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100774 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100775}
776
777/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200778 * 0
779 * 0,1
780 * 0,1,2
781 * 0,1,2,3
782 * 0,1,2,3,4
783 * 0,1,2,3,4,5
784 * 0,1,2,3,4,5,6
785 * 0,1,2,3,4,5,6,7
786 */
787static int cfq_get_next_prio_level(struct cfq_data *cfqd)
788{
789 int prio, wrap;
790
791 prio = -1;
792 wrap = 0;
793 do {
794 int p;
795
796 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
797 if (!list_empty(&cfqd->rr_list[p])) {
798 prio = p;
799 break;
800 }
801 }
802
803 if (prio != -1)
804 break;
805 cfqd->cur_prio = 0;
806 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
807 cfqd->cur_end_prio = 0;
808 if (wrap)
809 break;
810 wrap = 1;
811 }
812 } while (1);
813
814 if (unlikely(prio == -1))
815 return -1;
816
817 BUG_ON(prio >= CFQ_PRIO_LISTS);
818
819 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
820
821 cfqd->cur_prio = prio + 1;
822 if (cfqd->cur_prio > cfqd->cur_end_prio) {
823 cfqd->cur_end_prio = cfqd->cur_prio;
824 cfqd->cur_prio = 0;
825 }
826 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
827 cfqd->cur_prio = 0;
828 cfqd->cur_end_prio = 0;
829 }
830
Jens Axboe6d048f52007-04-25 12:44:27 +0200831 cfqd->cur_rr_tick++;
832 cfqd->prio_time = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +0200833 return prio;
834}
835
Jens Axboe6d048f52007-04-25 12:44:27 +0200836static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
837 struct request *rq)
838{
839 if (rq->sector >= cfqd->last_position)
840 return rq->sector - cfqd->last_position;
841 else
842 return cfqd->last_position - rq->sector;
843}
844
845static struct cfq_queue *cfq_get_best_queue(struct cfq_data *cfqd)
846{
847 struct cfq_queue *cfqq = NULL, *__cfqq;
848 sector_t best = -1, dist;
849
850 list_for_each_entry(__cfqq, &cfqd->cur_rr, cfq_list) {
851 if (!__cfqq->next_rq || !cfq_cfqq_sync(__cfqq))
852 continue;
853
854 dist = cfq_dist_from_last(cfqd, __cfqq->next_rq);
855 if (dist < best) {
856 best = dist;
857 cfqq = __cfqq;
858 }
859 }
860
861 /*
862 * Only async queue(s) available, grab first entry
863 */
864 if (!cfqq)
865 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
866
867 return cfqq;
868}
869
870static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200871{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100872 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200873
Jens Axboe89850f72006-07-22 16:48:31 +0200874 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1) {
875 /*
876 * if current list is non-empty, grab first entry. if it is
877 * empty, get next prio level and grab first entry then if any
878 * are spliced
879 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200880 cfqq = cfq_get_best_queue(cfqd);
Jens Axboe89850f72006-07-22 16:48:31 +0200881 } else if (!list_empty(&cfqd->busy_rr)) {
882 /*
883 * If no new queues are available, check if the busy list has
884 * some before falling back to idle io.
885 */
Jens Axboee0de0202006-06-01 10:07:26 +0200886 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
Jens Axboe89850f72006-07-22 16:48:31 +0200887 } else if (!list_empty(&cfqd->idle_rr)) {
888 /*
889 * if we have idle queues and no rt or be queues had pending
890 * requests, either allow immediate service if the grace period
891 * has passed or arm the idle grace timer
892 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200893 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
894
895 if (time_after_eq(jiffies, end))
896 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
897 else
898 mod_timer(&cfqd->idle_class_timer, end);
899 }
900
Jens Axboe6d048f52007-04-25 12:44:27 +0200901 return cfqq;
902}
903
904static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
905{
906 struct cfq_queue *cfqq;
907
908 do {
909 long prio;
910
911 cfqq = cfq_get_next_queue(cfqd);
912 if (!cfqq)
913 break;
914
915 prio = cfq_prio_to_slice(cfqd, cfqq);
916 if (cfqq->slice_resid > -prio)
917 break;
918
919 cfqq->slice_resid += prio;
920 list_del_init(&cfqq->cfq_list);
921 list_add_tail(&cfqq->cfq_list, &cfqd->rr_list[cfqq->ioprio]);
922 cfqq = NULL;
923 } while (1);
924
Jens Axboe22e2c502005-06-27 10:55:12 +0200925 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200926 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200927}
928
Jens Axboe6d048f52007-04-25 12:44:27 +0200929static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
930{
931 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200932
Jens Axboe6d048f52007-04-25 12:44:27 +0200933 if (!sample_valid(cic->seek_samples))
934 return 0;
935
936 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
937}
938
939static struct cfq_queue *__cfq_close_cooperator(struct cfq_data *cfqd,
940 struct cfq_queue *cur_cfqq,
941 struct list_head *list)
942{
943 struct cfq_queue *cfqq;
944
945 list_for_each_entry(cfqq, list, cfq_list) {
946 if (cfqq == cur_cfqq || !cfq_cfqq_sync(cfqq))
947 continue;
948
949 BUG_ON(!cfqq->next_rq);
950
951 if (cfq_rq_close(cfqd, cfqq->next_rq))
952 return cfqq;
953 }
954
955 return NULL;
956}
957
958static int cfq_close_cooperator(struct cfq_data *cfqd,
959 struct cfq_queue *cur_cfqq)
960{
961 struct cfq_queue *cfqq;
962
963 if (!cfqd->busy_queues)
964 return 0;
965
966 /*
967 * check cur_rr and same-prio rr_list for candidates
968 */
969 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->cur_rr);
970 if (cfqq)
971 return 1;
972
973 cfqq = __cfq_close_cooperator(cfqd, cur_cfqq, &cfqd->rr_list[cur_cfqq->ioprio]);
974 if (cfqq && (cfqq->rr_tick == cfqd->cur_rr_tick))
975 cfqq = NULL;
976
977 return cfqq != NULL;
978}
979
980#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
981
982static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200983{
Jens Axboe17926692007-01-19 11:59:30 +1100984 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200985 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100986 unsigned long sl;
987
Jens Axboedd67d052006-06-21 09:36:18 +0200988 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200989 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200990
991 /*
992 * idle is disabled, either manually or by past process history
993 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200994 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
995 return;
996
Jens Axboe22e2c502005-06-27 10:55:12 +0200997 /*
998 * task has exited, don't wait
999 */
Jens Axboe206dc692006-03-28 13:03:44 +02001000 cic = cfqd->active_cic;
1001 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +02001002 return;
1003
1004 /*
1005 * See if this prio level has a good candidate
1006 */
1007 if (cfq_close_cooperator(cfqd, cfqq))
1008 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001009
Jens Axboe3b181522005-06-27 10:56:24 +02001010 cfq_mark_cfqq_must_dispatch(cfqq);
1011 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001012
Jens Axboe206dc692006-03-28 13:03:44 +02001013 /*
1014 * we don't want to idle for seeks, but we do want to allow
1015 * fair distribution of slice time for a process doing back-to-back
1016 * seeks. so allow a little bit of time for him to submit a new rq
1017 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001018 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001019 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +02001020 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +02001021
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001022 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Jens Axboe5e705372006-07-13 12:39:25 +02001025static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Jens Axboe5e705372006-07-13 12:39:25 +02001027 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001028
Jens Axboe5380a102006-07-13 12:37:56 +02001029 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001030 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001031 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/*
1035 * return expired entry, or NULL to just start from scratch in rbtree
1036 */
Jens Axboe5e705372006-07-13 12:39:25 +02001037static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001040 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +02001041 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jens Axboe3b181522005-06-27 10:56:24 +02001043 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001045
1046 cfq_mark_cfqq_fifo_expire(cfqq);
1047
Jens Axboe89850f72006-07-22 16:48:31 +02001048 if (list_empty(&cfqq->fifo))
1049 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Jens Axboe6d048f52007-04-25 12:44:27 +02001051 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +02001052 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jens Axboe6d048f52007-04-25 12:44:27 +02001054 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
1055 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jens Axboe6d048f52007-04-25 12:44:27 +02001057 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Jens Axboe22e2c502005-06-27 10:55:12 +02001060static inline int
1061cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1062{
1063 const int base_rq = cfqd->cfq_slice_async_rq;
1064
1065 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1066
1067 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1068}
1069
1070/*
1071 * get next queue for service
1072 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001073static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001074{
Jens Axboe22e2c502005-06-27 10:55:12 +02001075 struct cfq_queue *cfqq;
1076
1077 cfqq = cfqd->active_queue;
1078 if (!cfqq)
1079 goto new_queue;
1080
1081 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001082 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02001083 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001084 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001085 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001086
1087 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001088 * The active queue has requests and isn't expired, allow it to
1089 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02001090 */
Jens Axboedd67d052006-06-21 09:36:18 +02001091 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001092 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02001093
1094 /*
1095 * No requests pending. If the active queue still has requests in
1096 * flight or is idling for a new request, allow either of these
1097 * conditions to happen (or time out) before selecting a new queue.
1098 */
1099 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001100 cfqq = NULL;
1101 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001102 }
1103
Jens Axboe3b181522005-06-27 10:56:24 +02001104expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001105 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001106new_queue:
1107 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001108keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001109 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001110}
1111
1112static int
1113__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1114 int max_dispatch)
1115{
1116 int dispatched = 0;
1117
Jens Axboedd67d052006-06-21 09:36:18 +02001118 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001119
1120 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001121 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001122
1123 /*
1124 * follow expired path, else get first next available
1125 */
Jens Axboe5e705372006-07-13 12:39:25 +02001126 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1127 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001128
1129 /*
1130 * finally, insert request into driver dispatch list
1131 */
Jens Axboe5e705372006-07-13 12:39:25 +02001132 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001133
1134 cfqd->dispatch_slice++;
1135 dispatched++;
1136
1137 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001138 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1139 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001140 }
1141
Jens Axboedd67d052006-06-21 09:36:18 +02001142 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001143 break;
1144
1145 } while (dispatched < max_dispatch);
1146
1147 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001148 * expire an async queue immediately if it has used up its slice. idle
1149 * queue always expire after 1 dispatch round.
1150 */
Jens Axboea9938002007-04-20 08:55:52 +02001151 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001152 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001153 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001154 cfqq->slice_end = jiffies + 1;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001155 cfq_slice_expired(cfqd, 0, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001156 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001157
1158 return dispatched;
1159}
1160
1161static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001162cfq_forced_dispatch_cfqqs(struct list_head *list)
1163{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001164 struct cfq_queue *cfqq, *next;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001165 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001166
Jens Axboecaaa5f92006-06-16 11:23:00 +02001167 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001168 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
Jens Axboe5e705372006-07-13 12:39:25 +02001169 while (cfqq->next_rq) {
1170 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001171 dispatched++;
1172 }
1173 BUG_ON(!list_empty(&cfqq->fifo));
1174 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001175
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001176 return dispatched;
1177}
1178
1179static int
1180cfq_forced_dispatch(struct cfq_data *cfqd)
1181{
1182 int i, dispatched = 0;
1183
1184 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1185 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1186
1187 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1188 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1189 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1190
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001191 cfq_slice_expired(cfqd, 0, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001192
1193 BUG_ON(cfqd->busy_queues);
1194
1195 return dispatched;
1196}
1197
1198static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001199cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001202 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001203 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jens Axboe22e2c502005-06-27 10:55:12 +02001205 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return 0;
1207
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001208 if (unlikely(force))
1209 return cfq_forced_dispatch(cfqd);
1210
Jens Axboecaaa5f92006-06-16 11:23:00 +02001211 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001212 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001213 int max_dispatch;
1214
Jens Axboea9938002007-04-20 08:55:52 +02001215 if (cfqd->busy_queues > 1) {
1216 /*
Jens Axboea9938002007-04-20 08:55:52 +02001217 * So we have dispatched before in this round, if the
1218 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001219 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001220 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001221 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1222 dispatched)
1223 break;
1224 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001225 break;
1226 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001227
Jens Axboe3b181522005-06-27 10:56:24 +02001228 cfq_clear_cfqq_must_dispatch(cfqq);
1229 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001230 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001232 max_dispatch = cfqd->cfq_quantum;
1233 if (cfq_class_idle(cfqq))
1234 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Jens Axboecaaa5f92006-06-16 11:23:00 +02001236 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
Jens Axboecaaa5f92006-06-16 11:23:00 +02001239 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/*
Jens Axboe5e705372006-07-13 12:39:25 +02001243 * task holds one reference to the queue, dropped when task exits. each rq
1244 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 *
1246 * queue lock must be held here.
1247 */
1248static void cfq_put_queue(struct cfq_queue *cfqq)
1249{
Jens Axboe22e2c502005-06-27 10:55:12 +02001250 struct cfq_data *cfqd = cfqq->cfqd;
1251
1252 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (!atomic_dec_and_test(&cfqq->ref))
1255 return;
1256
1257 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001258 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001259 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001261 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001262 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001263 cfq_schedule_dispatch(cfqd);
1264 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * it's on the empty list and still hashed
1268 */
1269 list_del(&cfqq->cfq_list);
1270 hlist_del(&cfqq->cfq_hash);
1271 kmem_cache_free(cfq_pool, cfqq);
1272}
1273
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001274static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001275__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1276 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001279 struct hlist_node *entry;
1280 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jens Axboe206dc692006-03-28 13:03:44 +02001282 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001283 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Jens Axboe206dc692006-03-28 13:03:44 +02001285 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return __cfqq;
1287 }
1288
1289 return NULL;
1290}
1291
1292static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001293cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Jens Axboe3b181522005-06-27 10:56:24 +02001295 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Jens Axboee2d74ac2006-03-28 08:59:01 +02001298static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Jens Axboe22e2c502005-06-27 10:55:12 +02001300 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001301 struct rb_node *n;
1302 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001303
Jens Axboee2d74ac2006-03-28 08:59:01 +02001304 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1305 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1306 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001307 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001308 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001309 }
1310
Jens Axboe4050cf12006-07-19 05:07:12 +02001311 elv_ioc_count_mod(ioc_count, -freed);
1312
1313 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001314 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Jens Axboe89850f72006-07-22 16:48:31 +02001317static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1318{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001319 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001320 __cfq_slice_expired(cfqd, cfqq, 0, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001321 cfq_schedule_dispatch(cfqd);
1322 }
Jens Axboe89850f72006-07-22 16:48:31 +02001323
1324 cfq_put_queue(cfqq);
1325}
1326
1327static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1328 struct cfq_io_context *cic)
1329{
Jens Axboefc463792006-08-29 09:05:44 +02001330 list_del_init(&cic->queue_list);
1331 smp_wmb();
1332 cic->key = NULL;
1333
Jens Axboe89850f72006-07-22 16:48:31 +02001334 if (cic->cfqq[ASYNC]) {
1335 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1336 cic->cfqq[ASYNC] = NULL;
1337 }
1338
1339 if (cic->cfqq[SYNC]) {
1340 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1341 cic->cfqq[SYNC] = NULL;
1342 }
Jens Axboe89850f72006-07-22 16:48:31 +02001343}
1344
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001347 * Called with interrupts disabled
1348 */
1349static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1350{
Al Viro478a82b2006-03-18 13:25:24 -05001351 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001352
Jens Axboe89850f72006-07-22 16:48:31 +02001353 if (cfqd) {
1354 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001355
Jens Axboefc463792006-08-29 09:05:44 +02001356 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001357 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001358 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001359 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001360}
1361
Jens Axboee2d74ac2006-03-28 08:59:01 +02001362static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Jens Axboe22e2c502005-06-27 10:55:12 +02001364 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001365 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * put the reference this task is holding to the various queues
1369 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001370
1371 n = rb_first(&ioc->cic_root);
1372 while (n != NULL) {
1373 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1374
Jens Axboe22e2c502005-06-27 10:55:12 +02001375 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001376 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Jens Axboe22e2c502005-06-27 10:55:12 +02001380static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001381cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Jens Axboeb5deef92006-07-19 23:39:40 +02001383 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Jens Axboeb5deef92006-07-19 23:39:40 +02001385 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001387 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001388 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001389 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001390 cic->dtor = cfq_free_io_context;
1391 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001392 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
1395 return cic;
1396}
1397
Jens Axboe22e2c502005-06-27 10:55:12 +02001398static void cfq_init_prio_data(struct cfq_queue *cfqq)
1399{
1400 struct task_struct *tsk = current;
1401 int ioprio_class;
1402
Jens Axboe3b181522005-06-27 10:56:24 +02001403 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001404 return;
1405
1406 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1407 switch (ioprio_class) {
1408 default:
1409 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1410 case IOPRIO_CLASS_NONE:
1411 /*
1412 * no prio set, place us in the middle of the BE classes
1413 */
1414 cfqq->ioprio = task_nice_ioprio(tsk);
1415 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1416 break;
1417 case IOPRIO_CLASS_RT:
1418 cfqq->ioprio = task_ioprio(tsk);
1419 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1420 break;
1421 case IOPRIO_CLASS_BE:
1422 cfqq->ioprio = task_ioprio(tsk);
1423 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1424 break;
1425 case IOPRIO_CLASS_IDLE:
1426 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1427 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001428 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001429 break;
1430 }
1431
1432 /*
1433 * keep track of original prio settings in case we have to temporarily
1434 * elevate the priority of this queue
1435 */
1436 cfqq->org_ioprio = cfqq->ioprio;
1437 cfqq->org_ioprio_class = cfqq->ioprio_class;
1438
Jens Axboe98e41c72007-02-05 11:55:35 +01001439 cfq_resort_rr_list(cfqq, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001440 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001441}
1442
Al Viro478a82b2006-03-18 13:25:24 -05001443static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001444{
Al Viro478a82b2006-03-18 13:25:24 -05001445 struct cfq_data *cfqd = cic->key;
1446 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001447 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001448
Jens Axboecaaa5f92006-06-16 11:23:00 +02001449 if (unlikely(!cfqd))
1450 return;
1451
Jens Axboec1b707d2006-10-30 19:54:23 +01001452 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001453
1454 cfqq = cic->cfqq[ASYNC];
1455 if (cfqq) {
1456 struct cfq_queue *new_cfqq;
1457 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1458 GFP_ATOMIC);
1459 if (new_cfqq) {
1460 cic->cfqq[ASYNC] = new_cfqq;
1461 cfq_put_queue(cfqq);
1462 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001463 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001464
1465 cfqq = cic->cfqq[SYNC];
1466 if (cfqq)
1467 cfq_mark_cfqq_prio_changed(cfqq);
1468
Jens Axboec1b707d2006-10-30 19:54:23 +01001469 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001470}
1471
Jens Axboefc463792006-08-29 09:05:44 +02001472static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Al Viroa6a07632006-03-18 13:26:44 -05001474 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001475 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001476
Jens Axboefc463792006-08-29 09:05:44 +02001477 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001478
Jens Axboee2d74ac2006-03-28 08:59:01 +02001479 n = rb_first(&ioc->cic_root);
1480 while (n != NULL) {
1481 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001482
Al Viro478a82b2006-03-18 13:25:24 -05001483 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001484 n = rb_next(n);
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001489cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001490 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1493 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001494 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496retry:
Al Viro6f325a12006-03-18 14:58:37 -05001497 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001498 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (!cfqq) {
1501 if (new_cfqq) {
1502 cfqq = new_cfqq;
1503 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001504 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001505 /*
1506 * Inform the allocator of the fact that we will
1507 * just repeat this allocation if it fails, to allow
1508 * the allocator to do whatever it needs to attempt to
1509 * free memory.
1510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001512 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 spin_lock_irq(cfqd->queue->queue_lock);
1514 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001515 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001516 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001517 if (!cfqq)
1518 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 memset(cfqq, 0, sizeof(*cfqq));
1522
1523 INIT_HLIST_NODE(&cfqq->cfq_hash);
1524 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001525 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 cfqq->key = key;
1528 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1529 atomic_set(&cfqq->ref, 0);
1530 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001531
Jens Axboea9938002007-04-20 08:55:52 +02001532 if (key != CFQ_KEY_ASYNC)
1533 cfq_mark_cfqq_idle_window(cfqq);
1534
Jens Axboe3b181522005-06-27 10:56:24 +02001535 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001536 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001537 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 if (new_cfqq)
1541 kmem_cache_free(cfq_pool, new_cfqq);
1542
1543 atomic_inc(&cfqq->ref);
1544out:
1545 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1546 return cfqq;
1547}
1548
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001549static void
1550cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1551{
Jens Axboefc463792006-08-29 09:05:44 +02001552 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001553 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001554 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001555 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001556}
1557
Jens Axboee2d74ac2006-03-28 08:59:01 +02001558static struct cfq_io_context *
1559cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1560{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001561 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001562 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001563 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001564
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001565restart:
1566 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001567 while (n) {
1568 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001569 /* ->key must be copied to avoid race with cfq_exit_queue() */
1570 k = cic->key;
1571 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001572 cfq_drop_dead_cic(ioc, cic);
1573 goto restart;
1574 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001575
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001576 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001577 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001578 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001579 n = n->rb_right;
1580 else
1581 return cic;
1582 }
1583
1584 return NULL;
1585}
1586
1587static inline void
1588cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1589 struct cfq_io_context *cic)
1590{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001591 struct rb_node **p;
1592 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001593 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001594 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001595 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001596
Jens Axboee2d74ac2006-03-28 08:59:01 +02001597 cic->ioc = ioc;
1598 cic->key = cfqd;
1599
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001600restart:
1601 parent = NULL;
1602 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001603 while (*p) {
1604 parent = *p;
1605 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001606 /* ->key must be copied to avoid race with cfq_exit_queue() */
1607 k = __cic->key;
1608 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001609 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001610 goto restart;
1611 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001612
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001613 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001614 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001615 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001616 p = &(*p)->rb_right;
1617 else
1618 BUG();
1619 }
1620
1621 rb_link_node(&cic->rb_node, parent, p);
1622 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001623
Jens Axboe0261d682006-10-30 19:07:48 +01001624 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001625 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001626 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001627}
1628
Jens Axboe22e2c502005-06-27 10:55:12 +02001629/*
1630 * Setup general io context and cfq io context. There can be several cfq
1631 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001632 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001633 */
1634static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001635cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Jens Axboe22e2c502005-06-27 10:55:12 +02001637 struct io_context *ioc = NULL;
1638 struct cfq_io_context *cic;
1639
1640 might_sleep_if(gfp_mask & __GFP_WAIT);
1641
Jens Axboeb5deef92006-07-19 23:39:40 +02001642 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001643 if (!ioc)
1644 return NULL;
1645
Jens Axboee2d74ac2006-03-28 08:59:01 +02001646 cic = cfq_cic_rb_lookup(cfqd, ioc);
1647 if (cic)
1648 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001649
Jens Axboee2d74ac2006-03-28 08:59:01 +02001650 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1651 if (cic == NULL)
1652 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001653
Jens Axboee2d74ac2006-03-28 08:59:01 +02001654 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001655out:
Jens Axboefc463792006-08-29 09:05:44 +02001656 smp_read_barrier_depends();
1657 if (unlikely(ioc->ioprio_changed))
1658 cfq_ioc_set_ioprio(ioc);
1659
Jens Axboe22e2c502005-06-27 10:55:12 +02001660 return cic;
1661err:
1662 put_io_context(ioc);
1663 return NULL;
1664}
1665
1666static void
1667cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1668{
Jens Axboeaaf12282007-01-19 11:30:16 +11001669 unsigned long elapsed = jiffies - cic->last_end_request;
1670 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001671
1672 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1673 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1674 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1675}
1676
Jens Axboe206dc692006-03-28 13:03:44 +02001677static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001678cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1679 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001680{
1681 sector_t sdist;
1682 u64 total;
1683
Jens Axboe5e705372006-07-13 12:39:25 +02001684 if (cic->last_request_pos < rq->sector)
1685 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001686 else
Jens Axboe5e705372006-07-13 12:39:25 +02001687 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001688
Jens Axboe6d048f52007-04-25 12:44:27 +02001689 if (!cic->seek_samples) {
1690 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1691 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1692 }
1693
Jens Axboe206dc692006-03-28 13:03:44 +02001694 /*
1695 * Don't allow the seek distance to get too large from the
1696 * odd fragment, pagein, etc
1697 */
1698 if (cic->seek_samples <= 60) /* second&third seek */
1699 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1700 else
1701 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1702
1703 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1704 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1705 total = cic->seek_total + (cic->seek_samples/2);
1706 do_div(total, cic->seek_samples);
1707 cic->seek_mean = (sector_t)total;
1708}
Jens Axboe22e2c502005-06-27 10:55:12 +02001709
1710/*
1711 * Disable idle window if the process thinks too long or seeks so much that
1712 * it doesn't matter
1713 */
1714static void
1715cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1716 struct cfq_io_context *cic)
1717{
Jens Axboe3b181522005-06-27 10:56:24 +02001718 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001719
Jens Axboecaaa5f92006-06-16 11:23:00 +02001720 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1721 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001722 enable_idle = 0;
1723 else if (sample_valid(cic->ttime_samples)) {
1724 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1725 enable_idle = 0;
1726 else
1727 enable_idle = 1;
1728 }
1729
Jens Axboe3b181522005-06-27 10:56:24 +02001730 if (enable_idle)
1731 cfq_mark_cfqq_idle_window(cfqq);
1732 else
1733 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001734}
1735
Jens Axboe22e2c502005-06-27 10:55:12 +02001736/*
1737 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1738 * no or if we aren't sure, a 1 will cause a preempt.
1739 */
1740static int
1741cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001742 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001743{
Jens Axboe6d048f52007-04-25 12:44:27 +02001744 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001745
Jens Axboe6d048f52007-04-25 12:44:27 +02001746 cfqq = cfqd->active_queue;
1747 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001748 return 0;
1749
Jens Axboe6d048f52007-04-25 12:44:27 +02001750 if (cfq_slice_used(cfqq))
1751 return 1;
1752
1753 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001754 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001755
1756 if (cfq_class_idle(cfqq))
1757 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001758
Jens Axboe22e2c502005-06-27 10:55:12 +02001759 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001760 * if the new request is sync, but the currently running queue is
1761 * not, let the sync request have priority.
1762 */
Jens Axboe5e705372006-07-13 12:39:25 +02001763 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001764 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001765
Jens Axboe374f84a2006-07-23 01:42:19 +02001766 /*
1767 * So both queues are sync. Let the new request get disk time if
1768 * it's a metadata request and the current queue is doing regular IO.
1769 */
1770 if (rq_is_meta(rq) && !cfqq->meta_pending)
1771 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001772
Jens Axboe1e3335d2007-02-14 19:59:49 +01001773 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1774 return 0;
1775
1776 /*
1777 * if this request is as-good as one we would expect from the
1778 * current cfqq, let it preempt
1779 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001780 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001781 return 1;
1782
Jens Axboe22e2c502005-06-27 10:55:12 +02001783 return 0;
1784}
1785
1786/*
1787 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1788 * let it have half of its nominal slice.
1789 */
1790static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1791{
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001792 cfq_slice_expired(cfqd, 1, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001793
Jens Axboebf572252006-07-19 20:29:12 +02001794 /*
1795 * Put the new queue at the front of the of the current list,
1796 * so we know that it will be selected next.
1797 */
1798 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1799 list_move(&cfqq->cfq_list, &cfqd->cur_rr);
1800
Jens Axboe44f7c162007-01-19 11:51:58 +11001801 cfqq->slice_end = 0;
1802 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001803}
1804
1805/*
Jens Axboe5e705372006-07-13 12:39:25 +02001806 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001807 * something we should do about it
1808 */
1809static void
Jens Axboe5e705372006-07-13 12:39:25 +02001810cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1811 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001812{
Jens Axboe5e705372006-07-13 12:39:25 +02001813 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001814
Jens Axboe374f84a2006-07-23 01:42:19 +02001815 if (rq_is_meta(rq))
1816 cfqq->meta_pending++;
1817
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001818 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001819 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001820 cfq_update_idle_window(cfqd, cfqq, cic);
1821
Jens Axboe5e705372006-07-13 12:39:25 +02001822 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001823 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001824
1825 if (cfqq == cfqd->active_queue) {
1826 /*
1827 * if we are waiting for a request for this queue, let it rip
1828 * immediately and flag that we must not expire this queue
1829 * just now
1830 */
Jens Axboe3b181522005-06-27 10:56:24 +02001831 if (cfq_cfqq_wait_request(cfqq)) {
1832 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001833 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001834 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001835 }
Jens Axboe5e705372006-07-13 12:39:25 +02001836 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001837 /*
1838 * not the active queue - expire current slice if it is
1839 * idle and has expired it's mean thinktime or this new queue
1840 * has some old slice time left and is of higher priority
1841 */
1842 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001843 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001844 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001845 }
1846}
1847
Jens Axboeb4878f22005-10-20 16:42:29 +02001848static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001849{
Jens Axboeb4878f22005-10-20 16:42:29 +02001850 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001851 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001852
1853 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jens Axboe5e705372006-07-13 12:39:25 +02001855 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jens Axboe22e2c502005-06-27 10:55:12 +02001857 list_add_tail(&rq->queuelist, &cfqq->fifo);
1858
Jens Axboe5e705372006-07-13 12:39:25 +02001859 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862static void cfq_completed_request(request_queue_t *q, struct request *rq)
1863{
Jens Axboe5e705372006-07-13 12:39:25 +02001864 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001865 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001866 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001867 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jens Axboeb4878f22005-10-20 16:42:29 +02001869 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jens Axboeb4878f22005-10-20 16:42:29 +02001871 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001872 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001873 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001874 cfqq->dispatched--;
Jens Axboe99f96282007-02-05 11:56:25 +01001875 cfqq->service_last = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Jens Axboeb4878f22005-10-20 16:42:29 +02001877 if (!cfq_class_idle(cfqq))
1878 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001879
Jens Axboe98e41c72007-02-05 11:55:35 +01001880 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jens Axboecaaa5f92006-06-16 11:23:00 +02001882 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001883 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001884
1885 /*
1886 * If this is the active queue, check if it needs to be expired,
1887 * or if we want to idle in case it has no pending requests.
1888 */
1889 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001890 if (cfq_cfqq_slice_new(cfqq)) {
1891 cfq_set_prio_slice(cfqd, cfqq);
1892 cfq_clear_cfqq_slice_new(cfqq);
1893 }
1894 if (cfq_slice_used(cfqq))
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001895 cfq_slice_expired(cfqd, 0, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001896 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1897 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001898 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001899
1900 if (!cfqd->rq_in_driver)
1901 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Jens Axboe22e2c502005-06-27 10:55:12 +02001904/*
1905 * we temporarily boost lower priority queues if they are holding fs exclusive
1906 * resources. they are boosted to normal prio (CLASS_BE/4)
1907 */
1908static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Jens Axboe22e2c502005-06-27 10:55:12 +02001910 const int ioprio_class = cfqq->ioprio_class;
1911 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Jens Axboe22e2c502005-06-27 10:55:12 +02001913 if (has_fs_excl()) {
1914 /*
1915 * boost idle prio on transactions that would lock out other
1916 * users of the filesystem
1917 */
1918 if (cfq_class_idle(cfqq))
1919 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1920 if (cfqq->ioprio > IOPRIO_NORM)
1921 cfqq->ioprio = IOPRIO_NORM;
1922 } else {
1923 /*
1924 * check if we need to unboost the queue
1925 */
1926 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1927 cfqq->ioprio_class = cfqq->org_ioprio_class;
1928 if (cfqq->ioprio != cfqq->org_ioprio)
1929 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931
Jens Axboe22e2c502005-06-27 10:55:12 +02001932 /*
1933 * refile between round-robin lists if we moved the priority class
1934 */
Jens Axboe98e41c72007-02-05 11:55:35 +01001935 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio))
Jens Axboe22e2c502005-06-27 10:55:12 +02001936 cfq_resort_rr_list(cfqq, 0);
1937}
1938
Jens Axboe89850f72006-07-22 16:48:31 +02001939static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001940{
Jens Axboe3b181522005-06-27 10:56:24 +02001941 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001942 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001943 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001944 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001945 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001946
1947 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001948}
1949
Jens Axboecb78b282006-07-28 09:32:57 +02001950static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001951{
1952 struct cfq_data *cfqd = q->elevator->elevator_data;
1953 struct task_struct *tsk = current;
1954 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001955 unsigned int key;
1956
1957 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001958
1959 /*
1960 * don't force setup of a queue from here, as a call to may_queue
1961 * does not necessarily imply that a request actually will be queued.
1962 * so just lookup a possibly existing queue, or return 'may queue'
1963 * if that fails
1964 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001965 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001966 if (cfqq) {
1967 cfq_init_prio_data(cfqq);
1968 cfq_prio_boost(cfqq);
1969
Jens Axboe89850f72006-07-22 16:48:31 +02001970 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001971 }
1972
1973 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976/*
1977 * queue lock held here
1978 */
Jens Axboebb37b942006-12-01 10:42:33 +01001979static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Jens Axboe5e705372006-07-13 12:39:25 +02001981 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Jens Axboe5e705372006-07-13 12:39:25 +02001983 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001984 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jens Axboe22e2c502005-06-27 10:55:12 +02001986 BUG_ON(!cfqq->allocated[rw]);
1987 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jens Axboe5e705372006-07-13 12:39:25 +02001989 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001992 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 cfq_put_queue(cfqq);
1995 }
1996}
1997
1998/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001999 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002001static int
Jens Axboecb78b282006-07-28 09:32:57 +02002002cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02002005 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct cfq_io_context *cic;
2007 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01002008 const int is_sync = rq_is_sync(rq);
2009 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02002010 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 unsigned long flags;
2012
2013 might_sleep_if(gfp_mask & __GFP_WAIT);
2014
Jens Axboee2d74ac2006-03-28 08:59:01 +02002015 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 spin_lock_irqsave(q->queue_lock, flags);
2018
Jens Axboe22e2c502005-06-27 10:55:12 +02002019 if (!cic)
2020 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Al Viro12a05732006-03-18 13:38:01 -05002022 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05002023 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002024 if (!cfqq)
2025 goto queue_fail;
2026
Al Viro12a05732006-03-18 13:38:01 -05002027 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002028 } else
Al Viro12a05732006-03-18 13:38:01 -05002029 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02002032 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002033 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 spin_unlock_irqrestore(q->queue_lock, flags);
2036
Jens Axboe5e705372006-07-13 12:39:25 +02002037 rq->elevator_private = cic;
2038 rq->elevator_private2 = cfqq;
2039 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02002040
Jens Axboe22e2c502005-06-27 10:55:12 +02002041queue_fail:
2042 if (cic)
2043 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02002044
Jens Axboe3b181522005-06-27 10:56:24 +02002045 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 spin_unlock_irqrestore(q->queue_lock, flags);
2047 return 1;
2048}
2049
David Howells65f27f32006-11-22 14:55:48 +00002050static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02002051{
David Howells65f27f32006-11-22 14:55:48 +00002052 struct cfq_data *cfqd =
2053 container_of(work, struct cfq_data, unplug_work);
2054 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002055 unsigned long flags;
2056
2057 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02002058 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02002059 spin_unlock_irqrestore(q->queue_lock, flags);
2060}
2061
2062/*
2063 * Timer running if the active_queue is currently idling inside its time slice
2064 */
2065static void cfq_idle_slice_timer(unsigned long data)
2066{
2067 struct cfq_data *cfqd = (struct cfq_data *) data;
2068 struct cfq_queue *cfqq;
2069 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002070 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02002071
2072 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2073
2074 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002075 timed_out = 0;
2076
Jens Axboe22e2c502005-06-27 10:55:12 +02002077 /*
2078 * expired
2079 */
Jens Axboe44f7c162007-01-19 11:51:58 +11002080 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002081 goto expire;
2082
2083 /*
2084 * only expire and reinvoke request handler, if there are
2085 * other queues with pending requests
2086 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002087 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002088 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002089
2090 /*
2091 * not expired and it has a request pending, let it dispatch
2092 */
Jens Axboedd67d052006-06-21 09:36:18 +02002093 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002094 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002095 goto out_kick;
2096 }
2097 }
2098expire:
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002099 cfq_slice_expired(cfqd, 0, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002100out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002101 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002102out_cont:
2103 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2104}
2105
2106/*
2107 * Timer running if an idle class queue is waiting for service
2108 */
2109static void cfq_idle_class_timer(unsigned long data)
2110{
2111 struct cfq_data *cfqd = (struct cfq_data *) data;
2112 unsigned long flags, end;
2113
2114 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2115
2116 /*
2117 * race with a non-idle queue, reset timer
2118 */
2119 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002120 if (!time_after_eq(jiffies, end))
2121 mod_timer(&cfqd->idle_class_timer, end);
2122 else
Jens Axboe3b181522005-06-27 10:56:24 +02002123 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002124
2125 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2126}
2127
Jens Axboe3b181522005-06-27 10:56:24 +02002128static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2129{
2130 del_timer_sync(&cfqd->idle_slice_timer);
2131 del_timer_sync(&cfqd->idle_class_timer);
2132 blk_sync_queue(cfqd->queue);
2133}
Jens Axboe22e2c502005-06-27 10:55:12 +02002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135static void cfq_exit_queue(elevator_t *e)
2136{
Jens Axboe22e2c502005-06-27 10:55:12 +02002137 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002138 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002139
Jens Axboe3b181522005-06-27 10:56:24 +02002140 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002141
Al Virod9ff4182006-03-18 13:51:22 -05002142 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002143
Al Virod9ff4182006-03-18 13:51:22 -05002144 if (cfqd->active_queue)
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002145 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002146
2147 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002148 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2149 struct cfq_io_context,
2150 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002151
2152 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002153 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002154
Al Virod9ff4182006-03-18 13:51:22 -05002155 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002156
2157 cfq_shutdown_timer_wq(cfqd);
2158
Al Viroa90d7422006-03-18 12:05:37 -05002159 kfree(cfqd->cfq_hash);
2160 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Jens Axboebb37b942006-12-01 10:42:33 +01002163static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 struct cfq_data *cfqd;
2166 int i;
2167
Jens Axboeb5deef92006-07-19 23:39:40 +02002168 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002170 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002173
2174 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2175 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2176
2177 INIT_LIST_HEAD(&cfqd->busy_rr);
2178 INIT_LIST_HEAD(&cfqd->cur_rr);
2179 INIT_LIST_HEAD(&cfqd->idle_rr);
Al Virod9ff4182006-03-18 13:51:22 -05002180 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Jens Axboeb5deef92006-07-19 23:39:40 +02002182 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002184 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2187 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Jens Axboe22e2c502005-06-27 10:55:12 +02002191 init_timer(&cfqd->idle_slice_timer);
2192 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2193 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2194
2195 init_timer(&cfqd->idle_class_timer);
2196 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2197 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2198
David Howells65f27f32006-11-22 14:55:48 +00002199 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002202 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2203 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 cfqd->cfq_back_max = cfq_back_max;
2205 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002206 cfqd->cfq_slice[0] = cfq_slice_async;
2207 cfqd->cfq_slice[1] = cfq_slice_sync;
2208 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2209 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002210
Jens Axboebc1c1162006-06-08 08:49:06 +02002211 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002212out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002214 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217static void cfq_slab_kill(void)
2218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (cfq_pool)
2220 kmem_cache_destroy(cfq_pool);
2221 if (cfq_ioc_pool)
2222 kmem_cache_destroy(cfq_ioc_pool);
2223}
2224
2225static int __init cfq_slab_setup(void)
2226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2228 NULL, NULL);
2229 if (!cfq_pool)
2230 goto fail;
2231
2232 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2233 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2234 if (!cfq_ioc_pool)
2235 goto fail;
2236
2237 return 0;
2238fail:
2239 cfq_slab_kill();
2240 return -ENOMEM;
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/*
2244 * sysfs parts below -->
2245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246static ssize_t
2247cfq_var_show(unsigned int var, char *page)
2248{
2249 return sprintf(page, "%d\n", var);
2250}
2251
2252static ssize_t
2253cfq_var_store(unsigned int *var, const char *page, size_t count)
2254{
2255 char *p = (char *) page;
2256
2257 *var = simple_strtoul(p, &p, 10);
2258 return count;
2259}
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002262static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002264 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 unsigned int __data = __VAR; \
2266 if (__CONV) \
2267 __data = jiffies_to_msecs(__data); \
2268 return cfq_var_show(__data, (page)); \
2269}
2270SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002271SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2272SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002273SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2274SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002275SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2276SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2277SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2278SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#undef SHOW_FUNCTION
2280
2281#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002282static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002284 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 unsigned int __data; \
2286 int ret = cfq_var_store(&__data, (page), count); \
2287 if (__data < (MIN)) \
2288 __data = (MIN); \
2289 else if (__data > (MAX)) \
2290 __data = (MAX); \
2291 if (__CONV) \
2292 *(__PTR) = msecs_to_jiffies(__data); \
2293 else \
2294 *(__PTR) = __data; \
2295 return ret; \
2296}
2297STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002298STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2299STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002300STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2301STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002302STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2303STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2304STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2305STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#undef STORE_FUNCTION
2307
Al Viroe572ec72006-03-18 22:27:18 -05002308#define CFQ_ATTR(name) \
2309 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002310
Al Viroe572ec72006-03-18 22:27:18 -05002311static struct elv_fs_entry cfq_attrs[] = {
2312 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002313 CFQ_ATTR(fifo_expire_sync),
2314 CFQ_ATTR(fifo_expire_async),
2315 CFQ_ATTR(back_seek_max),
2316 CFQ_ATTR(back_seek_penalty),
2317 CFQ_ATTR(slice_sync),
2318 CFQ_ATTR(slice_async),
2319 CFQ_ATTR(slice_async_rq),
2320 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002321 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322};
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324static struct elevator_type iosched_cfq = {
2325 .ops = {
2326 .elevator_merge_fn = cfq_merge,
2327 .elevator_merged_fn = cfq_merged_request,
2328 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002329 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002330 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002332 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 .elevator_deactivate_req_fn = cfq_deactivate_request,
2334 .elevator_queue_empty_fn = cfq_queue_empty,
2335 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002336 .elevator_former_req_fn = elv_rb_former_request,
2337 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 .elevator_set_req_fn = cfq_set_request,
2339 .elevator_put_req_fn = cfq_put_request,
2340 .elevator_may_queue_fn = cfq_may_queue,
2341 .elevator_init_fn = cfq_init_queue,
2342 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002343 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 },
Al Viro3d1ab402006-03-18 18:35:43 -05002345 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 .elevator_name = "cfq",
2347 .elevator_owner = THIS_MODULE,
2348};
2349
2350static int __init cfq_init(void)
2351{
2352 int ret;
2353
Jens Axboe22e2c502005-06-27 10:55:12 +02002354 /*
2355 * could be 0 on HZ < 1000 setups
2356 */
2357 if (!cfq_slice_async)
2358 cfq_slice_async = 1;
2359 if (!cfq_slice_idle)
2360 cfq_slice_idle = 1;
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (cfq_slab_setup())
2363 return -ENOMEM;
2364
2365 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002366 if (ret)
2367 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return ret;
2370}
2371
2372static void __exit cfq_exit(void)
2373{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002374 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002376 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002377 /* ioc_gone's update must be visible before reading ioc_count */
2378 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002379 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002380 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002381 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002382 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385module_init(cfq_init);
2386module_exit(cfq_exit);
2387
2388MODULE_AUTHOR("Jens Axboe");
2389MODULE_LICENSE("GPL");
2390MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");