blob: 1b803c0c90f170bd7ac22ec6747cd699a14f2dab [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 *
7 * Copyright (C) 2003 Jens Axboe <axboe@suse.de>
8 */
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 */
20static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
21static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
22static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
23static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Arjan van de Ven64100092006-01-06 09:46:02 +010025static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020026static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010027static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020028static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020029
30#define CFQ_IDLE_GRACE (HZ / 10)
31#define CFQ_SLICE_SCALE (5)
32
33#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020034
Jens Axboe3793c652006-05-30 21:11:04 +020035static DEFINE_SPINLOCK(cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -050036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * for the hash of cfqq inside the cfqd
39 */
40#define CFQ_QHASH_SHIFT 6
41#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
42#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
Jens Axboe22e2c502005-06-27 10:55:12 +020045#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define RQ_DATA(rq) (rq)->elevator_private
48
49/*
50 * rb-tree defines
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
53#define rq_rb_key(rq) (rq)->sector
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static kmem_cache_t *crq_pool;
56static kmem_cache_t *cfq_pool;
57static kmem_cache_t *cfq_ioc_pool;
58
Al Viro334e94d2006-03-18 15:05:53 -050059static atomic_t ioc_count = ATOMIC_INIT(0);
60static struct completion *ioc_gone;
61
Jens Axboe22e2c502005-06-27 10:55:12 +020062#define CFQ_PRIO_LISTS IOPRIO_BE_NR
63#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
64#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
65#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
66
Jens Axboe3b181522005-06-27 10:56:24 +020067#define ASYNC (0)
68#define SYNC (1)
69
70#define cfq_cfqq_dispatched(cfqq) \
71 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
72
73#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
74
75#define cfq_cfqq_sync(cfqq) \
76 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020077
Jens Axboe206dc692006-03-28 13:03:44 +020078#define sample_valid(samples) ((samples) > 80)
79
Jens Axboe22e2c502005-06-27 10:55:12 +020080/*
81 * Per block device queue structure
82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020084 request_queue_t *queue;
85
86 /*
87 * rr list of queues with requests and the count of them
88 */
89 struct list_head rr_list[CFQ_PRIO_LISTS];
90 struct list_head busy_rr;
91 struct list_head cur_rr;
92 struct list_head idle_rr;
93 unsigned int busy_queues;
94
95 /*
96 * non-ordered list of empty cfqq's
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct list_head empty_list;
99
Jens Axboe22e2c502005-06-27 10:55:12 +0200100 /*
101 * cfqq lookup hash
102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 mempool_t *crq_pool;
106
Jens Axboe22e2c502005-06-27 10:55:12 +0200107 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +0200108 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200109
110 /*
111 * schedule slice state info
112 */
113 /*
114 * idle window management
115 */
116 struct timer_list idle_slice_timer;
117 struct work_struct unplug_work;
118
119 struct cfq_queue *active_queue;
120 struct cfq_io_context *active_cic;
121 int cur_prio, cur_end_prio;
122 unsigned int dispatch_slice;
123
124 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200127 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jens Axboe22e2c502005-06-27 10:55:12 +0200129 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /*
132 * tunables, see top of file
133 */
134 unsigned int cfq_quantum;
135 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200136 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 unsigned int cfq_back_penalty;
138 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200139 unsigned int cfq_slice[2];
140 unsigned int cfq_slice_async_rq;
141 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500142
143 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Jens Axboe22e2c502005-06-27 10:55:12 +0200146/*
147 * Per process-grouping structure
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct cfq_queue {
150 /* reference count */
151 atomic_t ref;
152 /* parent cfq_data */
153 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200154 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct hlist_node cfq_hash;
156 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200157 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* on either rr or empty list of cfqd */
159 struct list_head cfq_list;
160 /* sorted list of pending requests */
161 struct rb_root sort_list;
162 /* if fifo isn't expired, next request to serve */
163 struct cfq_rq *next_crq;
164 /* requests queued in sort_list */
165 int queued[2];
166 /* currently allocated requests */
167 int allocated[2];
168 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200169 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jens Axboe22e2c502005-06-27 10:55:12 +0200171 unsigned long slice_start;
172 unsigned long slice_end;
173 unsigned long slice_left;
174 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jens Axboe3b181522005-06-27 10:56:24 +0200176 /* number of requests that are on the dispatch list */
177 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200178
179 /* io prio of this group */
180 unsigned short ioprio, org_ioprio;
181 unsigned short ioprio_class, org_ioprio_class;
182
Jens Axboe3b181522005-06-27 10:56:24 +0200183 /* various state flags, see below */
184 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187struct cfq_rq {
188 struct rb_node rb_node;
189 sector_t rb_key;
190 struct request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 struct cfq_queue *cfq_queue;
193 struct cfq_io_context *io_context;
194
Jens Axboe3b181522005-06-27 10:56:24 +0200195 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Jens Axboe3b181522005-06-27 10:56:24 +0200198enum cfqq_state_flags {
199 CFQ_CFQQ_FLAG_on_rr = 0,
200 CFQ_CFQQ_FLAG_wait_request,
201 CFQ_CFQQ_FLAG_must_alloc,
202 CFQ_CFQQ_FLAG_must_alloc_slice,
203 CFQ_CFQQ_FLAG_must_dispatch,
204 CFQ_CFQQ_FLAG_fifo_expire,
205 CFQ_CFQQ_FLAG_idle_window,
206 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200207};
208
209#define CFQ_CFQQ_FNS(name) \
210static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
211{ \
212 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
213} \
214static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
215{ \
216 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
217} \
218static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
219{ \
220 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
221}
222
223CFQ_CFQQ_FNS(on_rr);
224CFQ_CFQQ_FNS(wait_request);
225CFQ_CFQQ_FNS(must_alloc);
226CFQ_CFQQ_FNS(must_alloc_slice);
227CFQ_CFQQ_FNS(must_dispatch);
228CFQ_CFQQ_FNS(fifo_expire);
229CFQ_CFQQ_FNS(idle_window);
230CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200231#undef CFQ_CFQQ_FNS
232
233enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200234 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200235};
236
237#define CFQ_CRQ_FNS(name) \
238static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
239{ \
240 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
241} \
242static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
243{ \
244 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
245} \
246static inline int cfq_crq_##name(const struct cfq_rq *crq) \
247{ \
248 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
249}
250
Jens Axboe3b181522005-06-27 10:56:24 +0200251CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200252#undef CFQ_CRQ_FNS
253
254static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200255static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Al Viro6f325a12006-03-18 14:58:37 -0500256static 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 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700259 * scheduler run of queue, if there are requests pending and no one in the
260 * driver that will restart queueing
261 */
262static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
263{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100264 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700265 kblockd_schedule_work(&cfqd->unplug_work);
266}
267
268static int cfq_queue_empty(request_queue_t *q)
269{
270 struct cfq_data *cfqd = q->elevator->elevator_data;
271
Jens Axboeb4878f22005-10-20 16:42:29 +0200272 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700273}
274
Jens Axboe206dc692006-03-28 13:03:44 +0200275static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
276{
Jens Axboeb31dc662006-06-13 08:26:10 +0200277 if (rw == READ || rw == WRITE_SYNC)
Jens Axboe206dc692006-03-28 13:03:44 +0200278 return task->pid;
279
280 return CFQ_KEY_ASYNC;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
285 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200286 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288static struct cfq_rq *
289cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
290{
291 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200293#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
294#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
295 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (crq1 == NULL || crq1 == crq2)
298 return crq2;
299 if (crq2 == NULL)
300 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200301
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200302 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
303 return crq1;
304 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200305 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 s1 = crq1->request->sector;
308 s2 = crq2->request->sector;
309
310 last = cfqd->last_sector;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * by definition, 1KiB is 2 sectors
314 */
315 back_max = cfqd->cfq_back_max * 2;
316
317 /*
318 * Strict one way elevator _except_ in the case where we allow
319 * short backward seeks which are biased as twice the cost of a
320 * similar forward seek.
321 */
322 if (s1 >= last)
323 d1 = s1 - last;
324 else if (s1 + back_max >= last)
325 d1 = (last - s1) * cfqd->cfq_back_penalty;
326 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200327 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (s2 >= last)
330 d2 = s2 - last;
331 else if (s2 + back_max >= last)
332 d2 = (last - s2) * cfqd->cfq_back_penalty;
333 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200334 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Andreas Mohre8a99052006-03-28 08:59:49 +0200338 /*
339 * By doing switch() on the bit mask "wrap" we avoid having to
340 * check two variables for all permutations: --> faster!
341 */
342 switch (wrap) {
343 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
344 if (d1 < d2)
345 return crq1;
346 else if (d2 < d1)
347 return crq2;
348 else {
349 if (s1 >= s2)
350 return crq1;
351 else
352 return crq2;
353 }
354
355 case CFQ_RQ2_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return crq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200357 case CFQ_RQ1_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return crq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200359 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
360 default:
361 /*
362 * Since both rqs are wrapped,
363 * start with the one that's further behind head
364 * (--> only *one* back seek required),
365 * since back seek takes more time than forward.
366 */
367 if (s1 <= s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return crq1;
369 else
370 return crq2;
371 }
372}
373
374/*
375 * would be nice to take fifo expire time into account as well
376 */
377static struct cfq_rq *
378cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
379 struct cfq_rq *last)
380{
381 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
382 struct rb_node *rbnext, *rbprev;
383
Jens Axboeb4878f22005-10-20 16:42:29 +0200384 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200386 if (rbnext == &last->rb_node)
387 rbnext = NULL;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 rbprev = rb_prev(&last->rb_node);
391
392 if (rbprev)
393 crq_prev = rb_entry_crq(rbprev);
394 if (rbnext)
395 crq_next = rb_entry_crq(rbnext);
396
397 return cfq_choose_req(cfqd, crq_next, crq_prev);
398}
399
400static void cfq_update_next_crq(struct cfq_rq *crq)
401{
402 struct cfq_queue *cfqq = crq->cfq_queue;
403
404 if (cfqq->next_crq == crq)
405 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
406}
407
Jens Axboe22e2c502005-06-27 10:55:12 +0200408static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jens Axboe22e2c502005-06-27 10:55:12 +0200410 struct cfq_data *cfqd = cfqq->cfqd;
411 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jens Axboe3b181522005-06-27 10:56:24 +0200413 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 list_del(&cfqq->cfq_list);
416
Jens Axboe22e2c502005-06-27 10:55:12 +0200417 if (cfq_class_rt(cfqq))
418 list = &cfqd->cur_rr;
419 else if (cfq_class_idle(cfqq))
420 list = &cfqd->idle_rr;
421 else {
422 /*
423 * if cfqq has requests in flight, don't allow it to be
424 * found in cfq_set_active_queue before it has finished them.
425 * this is done to increase fairness between a process that
426 * has lots of io pending vs one that only generates one
427 * sporadically or synchronously
428 */
Jens Axboe3b181522005-06-27 10:56:24 +0200429 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200430 list = &cfqd->busy_rr;
431 else
432 list = &cfqd->rr_list[cfqq->ioprio];
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200436 * if queue was preempted, just add to front to be fair. busy_rr
Jens Axboeb52a8342006-06-01 18:53:43 +0200437 * isn't sorted, but insert at the back for fairness.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200439 if (preempted || list == &cfqd->busy_rr) {
Jens Axboeb52a8342006-06-01 18:53:43 +0200440 if (preempted)
441 list = list->prev;
442
443 list_add_tail(&cfqq->cfq_list, list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200444 return;
445 }
446
447 /*
448 * sort by when queue was last serviced
449 */
450 entry = list;
451 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
453
Jens Axboe22e2c502005-06-27 10:55:12 +0200454 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200456 if (time_before(__cfqq->service_last, cfqq->service_last))
457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 list_add(&cfqq->cfq_list, entry);
461}
462
463/*
464 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200465 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200468cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jens Axboe3b181522005-06-27 10:56:24 +0200470 BUG_ON(cfq_cfqq_on_rr(cfqq));
471 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 cfqd->busy_queues++;
473
Jens Axboeb4878f22005-10-20 16:42:29 +0200474 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static inline void
478cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
479{
Jens Axboe3b181522005-06-27 10:56:24 +0200480 BUG_ON(!cfq_cfqq_on_rr(cfqq));
481 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200482 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 BUG_ON(!cfqd->busy_queues);
485 cfqd->busy_queues--;
486}
487
488/*
489 * rb tree support functions
490 */
491static inline void cfq_del_crq_rb(struct cfq_rq *crq)
492{
493 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200494 struct cfq_data *cfqd = cfqq->cfqd;
495 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jens Axboeb4878f22005-10-20 16:42:29 +0200497 BUG_ON(!cfqq->queued[sync]);
498 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Jens Axboeb4878f22005-10-20 16:42:29 +0200500 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jens Axboeb4878f22005-10-20 16:42:29 +0200502 rb_erase(&crq->rb_node, &cfqq->sort_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jens Axboedd67d052006-06-21 09:36:18 +0200504 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200505 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508static struct cfq_rq *
509__cfq_add_crq_rb(struct cfq_rq *crq)
510{
511 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
512 struct rb_node *parent = NULL;
513 struct cfq_rq *__crq;
514
515 while (*p) {
516 parent = *p;
517 __crq = rb_entry_crq(parent);
518
519 if (crq->rb_key < __crq->rb_key)
520 p = &(*p)->rb_left;
521 else if (crq->rb_key > __crq->rb_key)
522 p = &(*p)->rb_right;
523 else
524 return __crq;
525 }
526
527 rb_link_node(&crq->rb_node, parent, p);
528 return NULL;
529}
530
531static void cfq_add_crq_rb(struct cfq_rq *crq)
532{
533 struct cfq_queue *cfqq = crq->cfq_queue;
534 struct cfq_data *cfqd = cfqq->cfqd;
535 struct request *rq = crq->request;
536 struct cfq_rq *__alias;
537
538 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200539 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * looks a little odd, but the first insert might return an alias.
543 * if that happens, put the alias on the dispatch list
544 */
545 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200546 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
549
Jens Axboe3b181522005-06-27 10:56:24 +0200550 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200551 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /*
554 * check if this request is a better next-serve candidate
555 */
556 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
557}
558
559static inline void
560cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
561{
Jens Axboeb4878f22005-10-20 16:42:29 +0200562 rb_erase(&crq->rb_node, &cfqq->sort_list);
563 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 cfq_add_crq_rb(crq);
566}
567
Jens Axboe206dc692006-03-28 13:03:44 +0200568static struct request *
569cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jens Axboe206dc692006-03-28 13:03:44 +0200571 struct task_struct *tsk = current;
572 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
573 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct rb_node *n;
Jens Axboe206dc692006-03-28 13:03:44 +0200575 sector_t sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jens Axboe206dc692006-03-28 13:03:44 +0200577 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!cfqq)
579 goto out;
580
Jens Axboe206dc692006-03-28 13:03:44 +0200581 sector = bio->bi_sector + bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 n = cfqq->sort_list.rb_node;
583 while (n) {
584 struct cfq_rq *crq = rb_entry_crq(n);
585
586 if (sector < crq->rb_key)
587 n = n->rb_left;
588 else if (sector > crq->rb_key)
589 n = n->rb_right;
590 else
591 return crq->request;
592 }
593
594out:
595 return NULL;
596}
597
Jens Axboeb4878f22005-10-20 16:42:29 +0200598static void cfq_activate_request(request_queue_t *q, struct request *rq)
599{
600 struct cfq_data *cfqd = q->elevator->elevator_data;
601
602 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200603
604 /*
605 * If the depth is larger 1, it really could be queueing. But lets
606 * make the mark a little higher - idling could still be good for
607 * low queueing, and a low queueing number could also just indicate
608 * a SCSI mid layer like behaviour where limit+1 is often seen.
609 */
610 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
611 cfqd->hw_tag = 1;
Jens Axboeb4878f22005-10-20 16:42:29 +0200612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
615{
Jens Axboe22e2c502005-06-27 10:55:12 +0200616 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jens Axboeb4878f22005-10-20 16:42:29 +0200618 WARN_ON(!cfqd->rq_in_driver);
619 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Jens Axboeb4878f22005-10-20 16:42:29 +0200622static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct cfq_rq *crq = RQ_DATA(rq);
625
Jens Axboeb4878f22005-10-20 16:42:29 +0200626 list_del_init(&rq->queuelist);
627 cfq_del_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630static int
631cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
632{
633 struct cfq_data *cfqd = q->elevator->elevator_data;
634 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jens Axboe206dc692006-03-28 13:03:44 +0200636 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200637 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200638 *req = __rq;
639 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645static void cfq_merged_request(request_queue_t *q, struct request *req)
646{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct cfq_rq *crq = RQ_DATA(req);
648
Jens Axboeb4878f22005-10-20 16:42:29 +0200649 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct cfq_queue *cfqq = crq->cfq_queue;
651
652 cfq_update_next_crq(crq);
653 cfq_reposition_crq_rb(cfqq, crq);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static void
658cfq_merged_requests(request_queue_t *q, struct request *rq,
659 struct request *next)
660{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 cfq_merged_request(q, rq);
662
Jens Axboe22e2c502005-06-27 10:55:12 +0200663 /*
664 * reposition in fifo if next is older than rq
665 */
666 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
667 time_before(next->start_time, rq->start_time))
668 list_move(&rq->queuelist, &next->queuelist);
669
Jens Axboeb4878f22005-10-20 16:42:29 +0200670 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200671}
672
673static inline void
674__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
675{
676 if (cfqq) {
677 /*
678 * stop potential idle class queues waiting service
679 */
680 del_timer(&cfqd->idle_class_timer);
681
682 cfqq->slice_start = jiffies;
683 cfqq->slice_end = 0;
684 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200685 cfq_clear_cfqq_must_alloc_slice(cfqq);
686 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200687 }
688
689 cfqd->active_queue = cfqq;
690}
691
692/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100693 * current cfqq expired its slice (or was too idle), select new one
694 */
695static void
696__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
697 int preempted)
698{
699 unsigned long now = jiffies;
700
701 if (cfq_cfqq_wait_request(cfqq))
702 del_timer(&cfqd->idle_slice_timer);
703
704 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
705 cfqq->service_last = now;
706 cfq_schedule_dispatch(cfqd);
707 }
708
709 cfq_clear_cfqq_must_dispatch(cfqq);
710 cfq_clear_cfqq_wait_request(cfqq);
711
712 /*
713 * store what was left of this slice, if the queue idled out
714 * or was preempted
715 */
716 if (time_after(cfqq->slice_end, now))
717 cfqq->slice_left = cfqq->slice_end - now;
718 else
719 cfqq->slice_left = 0;
720
721 if (cfq_cfqq_on_rr(cfqq))
722 cfq_resort_rr_list(cfqq, preempted);
723
724 if (cfqq == cfqd->active_queue)
725 cfqd->active_queue = NULL;
726
727 if (cfqd->active_cic) {
728 put_io_context(cfqd->active_cic->ioc);
729 cfqd->active_cic = NULL;
730 }
731
732 cfqd->dispatch_slice = 0;
733}
734
735static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
736{
737 struct cfq_queue *cfqq = cfqd->active_queue;
738
739 if (cfqq)
740 __cfq_slice_expired(cfqd, cfqq, preempted);
741}
742
743/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200744 * 0
745 * 0,1
746 * 0,1,2
747 * 0,1,2,3
748 * 0,1,2,3,4
749 * 0,1,2,3,4,5
750 * 0,1,2,3,4,5,6
751 * 0,1,2,3,4,5,6,7
752 */
753static int cfq_get_next_prio_level(struct cfq_data *cfqd)
754{
755 int prio, wrap;
756
757 prio = -1;
758 wrap = 0;
759 do {
760 int p;
761
762 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
763 if (!list_empty(&cfqd->rr_list[p])) {
764 prio = p;
765 break;
766 }
767 }
768
769 if (prio != -1)
770 break;
771 cfqd->cur_prio = 0;
772 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
773 cfqd->cur_end_prio = 0;
774 if (wrap)
775 break;
776 wrap = 1;
777 }
778 } while (1);
779
780 if (unlikely(prio == -1))
781 return -1;
782
783 BUG_ON(prio >= CFQ_PRIO_LISTS);
784
785 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
786
787 cfqd->cur_prio = prio + 1;
788 if (cfqd->cur_prio > cfqd->cur_end_prio) {
789 cfqd->cur_end_prio = cfqd->cur_prio;
790 cfqd->cur_prio = 0;
791 }
792 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
793 cfqd->cur_prio = 0;
794 cfqd->cur_end_prio = 0;
795 }
796
797 return prio;
798}
799
Jens Axboe3b181522005-06-27 10:56:24 +0200800static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200801{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100802 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200803
804 /*
805 * if current list is non-empty, grab first entry. if it is empty,
806 * get next prio level and grab first entry then if any are spliced
807 */
808 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
809 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
810
811 /*
Jens Axboee0de0202006-06-01 10:07:26 +0200812 * If no new queues are available, check if the busy list has some
813 * before falling back to idle io.
814 */
815 if (!cfqq && !list_empty(&cfqd->busy_rr))
816 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
817
818 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200819 * if we have idle queues and no rt or be queues had pending
820 * requests, either allow immediate service if the grace period
821 * has passed or arm the idle grace timer
822 */
823 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
824 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
825
826 if (time_after_eq(jiffies, end))
827 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
828 else
829 mod_timer(&cfqd->idle_class_timer, end);
830 }
831
832 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200833 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200834}
835
Jens Axboecaaa5f92006-06-16 11:23:00 +0200836#define CIC_SEEKY(cic) ((cic)->seek_mean > (128 * 1024))
837
Jens Axboe22e2c502005-06-27 10:55:12 +0200838static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
839
840{
Jens Axboe206dc692006-03-28 13:03:44 +0200841 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100842 unsigned long sl;
843
Jens Axboedd67d052006-06-21 09:36:18 +0200844 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200845 WARN_ON(cfqq != cfqd->active_queue);
846
847 /*
848 * idle is disabled, either manually or by past process history
849 */
850 if (!cfqd->cfq_slice_idle)
851 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200852 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200853 return 0;
854 /*
855 * task has exited, don't wait
856 */
Jens Axboe206dc692006-03-28 13:03:44 +0200857 cic = cfqd->active_cic;
858 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200859 return 0;
860
Jens Axboe3b181522005-06-27 10:56:24 +0200861 cfq_mark_cfqq_must_dispatch(cfqq);
862 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200863
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100864 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200865
866 /*
867 * we don't want to idle for seeks, but we do want to allow
868 * fair distribution of slice time for a process doing back-to-back
869 * seeks. so allow a little bit of time for him to submit a new rq
870 */
Jens Axboecaaa5f92006-06-16 11:23:00 +0200871 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboe44eb1232006-07-25 15:05:21 +0200872 sl = min(sl, msecs_to_jiffies(2));
Jens Axboe206dc692006-03-28 13:03:44 +0200873
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100874 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200875 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Jens Axboeb4878f22005-10-20 16:42:29 +0200878static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct cfq_data *cfqd = q->elevator->elevator_data;
881 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboefd61af02006-06-16 15:35:39 +0200882 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200883
884 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200885 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200886 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200887 elv_dispatch_sort(q, crq->request);
Jens Axboefd61af02006-06-16 15:35:39 +0200888
889 rq = list_entry(q->queue_head.prev, struct request, queuelist);
890 cfqd->last_sector = rq->sector + rq->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * return expired entry, or NULL to just start from scratch in rbtree
895 */
896static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
897{
898 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200899 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct cfq_rq *crq;
901
Jens Axboe3b181522005-06-27 10:56:24 +0200902 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return NULL;
904
Jens Axboe22e2c502005-06-27 10:55:12 +0200905 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200906 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Jens Axboe22e2c502005-06-27 10:55:12 +0200908 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
909 rq = crq->request;
910 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200911 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200912 return crq;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 return NULL;
917}
918
919/*
Jens Axboe3b181522005-06-27 10:56:24 +0200920 * Scale schedule slice based on io priority. Use the sync time slice only
921 * if a queue is marked sync and has sync io queued. A sync queue with async
922 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200924static inline int
925cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Jens Axboe22e2c502005-06-27 10:55:12 +0200929 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jens Axboe22e2c502005-06-27 10:55:12 +0200931 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Jens Axboe22e2c502005-06-27 10:55:12 +0200934static inline void
935cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
936{
937 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
938}
939
940static inline int
941cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
942{
943 const int base_rq = cfqd->cfq_slice_async_rq;
944
945 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
946
947 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
948}
949
950/*
951 * get next queue for service
952 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100953static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200954{
955 unsigned long now = jiffies;
956 struct cfq_queue *cfqq;
957
958 cfqq = cfqd->active_queue;
959 if (!cfqq)
960 goto new_queue;
961
962 /*
963 * slice has expired
964 */
Jens Axboe3b181522005-06-27 10:56:24 +0200965 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
966 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200967
968 /*
969 * if queue has requests, dispatch one. if not, check if
970 * enough slice is left to wait for one
971 */
Jens Axboedd67d052006-06-21 09:36:18 +0200972 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200973 goto keep_queue;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200974 else if (cfq_cfqq_dispatched(cfqq)) {
975 cfqq = NULL;
976 goto keep_queue;
977 } else if (cfq_cfqq_class_sync(cfqq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200978 if (cfq_arm_slice_timer(cfqd, cfqq))
979 return NULL;
980 }
981
Jens Axboe3b181522005-06-27 10:56:24 +0200982expire:
Jens Axboe22e2c502005-06-27 10:55:12 +0200983 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200984new_queue:
985 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200986keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200987 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200988}
989
990static int
991__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
992 int max_dispatch)
993{
994 int dispatched = 0;
995
Jens Axboedd67d052006-06-21 09:36:18 +0200996 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +0200997
998 do {
999 struct cfq_rq *crq;
1000
1001 /*
1002 * follow expired path, else get first next available
1003 */
1004 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1005 crq = cfqq->next_crq;
1006
1007 /*
1008 * finally, insert request into driver dispatch list
1009 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001010 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001011
1012 cfqd->dispatch_slice++;
1013 dispatched++;
1014
1015 if (!cfqd->active_cic) {
1016 atomic_inc(&crq->io_context->ioc->refcount);
1017 cfqd->active_cic = crq->io_context;
1018 }
1019
Jens Axboedd67d052006-06-21 09:36:18 +02001020 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001021 break;
1022
1023 } while (dispatched < max_dispatch);
1024
1025 /*
Jens Axboecaaa5f92006-06-16 11:23:00 +02001026 * if slice end isn't set yet, set it.
Jens Axboe22e2c502005-06-27 10:55:12 +02001027 */
1028 if (!cfqq->slice_end)
1029 cfq_set_prio_slice(cfqd, cfqq);
1030
1031 /*
1032 * expire an async queue immediately if it has used up its slice. idle
1033 * queue always expire after 1 dispatch round.
1034 */
1035 if ((!cfq_cfqq_sync(cfqq) &&
1036 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboecaaa5f92006-06-16 11:23:00 +02001037 cfq_class_idle(cfqq) ||
1038 !cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001039 cfq_slice_expired(cfqd, 0);
1040
1041 return dispatched;
1042}
1043
1044static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001045cfq_forced_dispatch_cfqqs(struct list_head *list)
1046{
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001047 struct cfq_queue *cfqq, *next;
1048 struct cfq_rq *crq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001049 int dispatched;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001050
Jens Axboecaaa5f92006-06-16 11:23:00 +02001051 dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001052 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1053 while ((crq = cfqq->next_crq)) {
1054 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1055 dispatched++;
1056 }
1057 BUG_ON(!list_empty(&cfqq->fifo));
1058 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001059
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001060 return dispatched;
1061}
1062
1063static int
1064cfq_forced_dispatch(struct cfq_data *cfqd)
1065{
1066 int i, dispatched = 0;
1067
1068 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1069 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1070
1071 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1072 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1073 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1074
1075 cfq_slice_expired(cfqd, 0);
1076
1077 BUG_ON(cfqd->busy_queues);
1078
1079 return dispatched;
1080}
1081
1082static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001083cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001086 struct cfq_queue *cfqq, *prev_cfqq;
1087 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Jens Axboe22e2c502005-06-27 10:55:12 +02001089 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0;
1091
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001092 if (unlikely(force))
1093 return cfq_forced_dispatch(cfqd);
1094
Jens Axboecaaa5f92006-06-16 11:23:00 +02001095 dispatched = 0;
1096 prev_cfqq = NULL;
1097 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001098 int max_dispatch;
1099
Jens Axboecaaa5f92006-06-16 11:23:00 +02001100 /*
1101 * Don't repeat dispatch from the previous queue.
1102 */
1103 if (prev_cfqq == cfqq)
1104 break;
1105
Jens Axboe3b181522005-06-27 10:56:24 +02001106 cfq_clear_cfqq_must_dispatch(cfqq);
1107 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001108 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001110 max_dispatch = cfqd->cfq_quantum;
1111 if (cfq_class_idle(cfqq))
1112 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jens Axboecaaa5f92006-06-16 11:23:00 +02001114 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1115
1116 /*
1117 * If the dispatch cfqq has idling enabled and is still
1118 * the active queue, break out.
1119 */
1120 if (cfq_cfqq_idle_window(cfqq) && cfqd->active_queue)
1121 break;
1122
1123 prev_cfqq = cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Jens Axboecaaa5f92006-06-16 11:23:00 +02001126 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/*
1130 * task holds one reference to the queue, dropped when task exits. each crq
1131 * in-flight on this queue also holds a reference, dropped when crq is freed.
1132 *
1133 * queue lock must be held here.
1134 */
1135static void cfq_put_queue(struct cfq_queue *cfqq)
1136{
Jens Axboe22e2c502005-06-27 10:55:12 +02001137 struct cfq_data *cfqd = cfqq->cfqd;
1138
1139 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!atomic_dec_and_test(&cfqq->ref))
1142 return;
1143
1144 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001145 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001146 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001148 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001149 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * it's on the empty list and still hashed
1153 */
1154 list_del(&cfqq->cfq_list);
1155 hlist_del(&cfqq->cfq_hash);
1156 kmem_cache_free(cfq_pool, cfqq);
1157}
1158
1159static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001160__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1161 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001164 struct hlist_node *entry;
1165 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jens Axboe206dc692006-03-28 13:03:44 +02001167 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001168 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jens Axboe206dc692006-03-28 13:03:44 +02001170 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return __cfqq;
1172 }
1173
1174 return NULL;
1175}
1176
1177static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001178cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jens Axboe3b181522005-06-27 10:56:24 +02001180 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Jens Axboee2d74ac2006-03-28 08:59:01 +02001183static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Jens Axboe22e2c502005-06-27 10:55:12 +02001185 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001186 struct rb_node *n;
1187 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001188
Jens Axboee2d74ac2006-03-28 08:59:01 +02001189 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1190 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1191 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001192 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001193 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001194 }
1195
Al Viro334e94d2006-03-18 15:05:53 -05001196 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1197 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Al Viroe17a9482006-03-18 13:21:20 -05001200static void cfq_trim(struct io_context *ioc)
1201{
1202 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001203 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001207 * Called with interrupts disabled
1208 */
1209static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1210{
Al Viro478a82b2006-03-18 13:25:24 -05001211 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001212 request_queue_t *q;
1213
1214 if (!cfqd)
1215 return;
1216
1217 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001218
1219 WARN_ON(!irqs_disabled());
1220
1221 spin_lock(q->queue_lock);
1222
Al Viro12a05732006-03-18 13:38:01 -05001223 if (cic->cfqq[ASYNC]) {
1224 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1225 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1226 cfq_put_queue(cic->cfqq[ASYNC]);
1227 cic->cfqq[ASYNC] = NULL;
1228 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001229
Al Viro12a05732006-03-18 13:38:01 -05001230 if (cic->cfqq[SYNC]) {
1231 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1232 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1233 cfq_put_queue(cic->cfqq[SYNC]);
1234 cic->cfqq[SYNC] = NULL;
1235 }
1236
Al Viro478a82b2006-03-18 13:25:24 -05001237 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001238 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001239 spin_unlock(q->queue_lock);
1240}
1241
Jens Axboee2d74ac2006-03-28 08:59:01 +02001242static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Jens Axboe22e2c502005-06-27 10:55:12 +02001244 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001246 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * put the reference this task is holding to the various queues
1250 */
Jens Axboe3793c652006-05-30 21:11:04 +02001251 spin_lock_irqsave(&cfq_exit_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001252
1253 n = rb_first(&ioc->cic_root);
1254 while (n != NULL) {
1255 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1256
Jens Axboe22e2c502005-06-27 10:55:12 +02001257 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001258 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Jens Axboe3793c652006-05-30 21:11:04 +02001261 spin_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Jens Axboe22e2c502005-06-27 10:55:12 +02001264static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001265cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Jens Axboe22e2c502005-06-27 10:55:12 +02001267 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001270 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001271 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001272 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001273 cic->dtor = cfq_free_io_context;
1274 cic->exit = cfq_exit_io_context;
Al Viro334e94d2006-03-18 15:05:53 -05001275 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 return cic;
1279}
1280
Jens Axboe22e2c502005-06-27 10:55:12 +02001281static void cfq_init_prio_data(struct cfq_queue *cfqq)
1282{
1283 struct task_struct *tsk = current;
1284 int ioprio_class;
1285
Jens Axboe3b181522005-06-27 10:56:24 +02001286 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001287 return;
1288
1289 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1290 switch (ioprio_class) {
1291 default:
1292 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1293 case IOPRIO_CLASS_NONE:
1294 /*
1295 * no prio set, place us in the middle of the BE classes
1296 */
1297 cfqq->ioprio = task_nice_ioprio(tsk);
1298 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1299 break;
1300 case IOPRIO_CLASS_RT:
1301 cfqq->ioprio = task_ioprio(tsk);
1302 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1303 break;
1304 case IOPRIO_CLASS_BE:
1305 cfqq->ioprio = task_ioprio(tsk);
1306 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1307 break;
1308 case IOPRIO_CLASS_IDLE:
1309 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1310 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001311 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001312 break;
1313 }
1314
1315 /*
1316 * keep track of original prio settings in case we have to temporarily
1317 * elevate the priority of this queue
1318 */
1319 cfqq->org_ioprio = cfqq->ioprio;
1320 cfqq->org_ioprio_class = cfqq->ioprio_class;
1321
Jens Axboe3b181522005-06-27 10:56:24 +02001322 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001323 cfq_resort_rr_list(cfqq, 0);
1324
Jens Axboe3b181522005-06-27 10:56:24 +02001325 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001326}
1327
Al Viro478a82b2006-03-18 13:25:24 -05001328static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001329{
Al Viro478a82b2006-03-18 13:25:24 -05001330 struct cfq_data *cfqd = cic->key;
1331 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02001332
Jens Axboecaaa5f92006-06-16 11:23:00 +02001333 if (unlikely(!cfqd))
1334 return;
1335
1336 spin_lock(cfqd->queue->queue_lock);
1337
1338 cfqq = cic->cfqq[ASYNC];
1339 if (cfqq) {
1340 struct cfq_queue *new_cfqq;
1341 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1342 GFP_ATOMIC);
1343 if (new_cfqq) {
1344 cic->cfqq[ASYNC] = new_cfqq;
1345 cfq_put_queue(cfqq);
1346 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001347 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001348
1349 cfqq = cic->cfqq[SYNC];
1350 if (cfqq)
1351 cfq_mark_cfqq_prio_changed(cfqq);
1352
1353 spin_unlock(cfqd->queue->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001357 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001359static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Al Viroa6a07632006-03-18 13:26:44 -05001361 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001362 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001363
Jens Axboe3793c652006-05-30 21:11:04 +02001364 spin_lock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001365
Jens Axboee2d74ac2006-03-28 08:59:01 +02001366 n = rb_first(&ioc->cic_root);
1367 while (n != NULL) {
1368 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001369
Al Viro478a82b2006-03-18 13:25:24 -05001370 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001371 n = rb_next(n);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Jens Axboe3793c652006-05-30 21:11:04 +02001374 spin_unlock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001375
Jens Axboe22e2c502005-06-27 10:55:12 +02001376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001380cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001381 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1384 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001385 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387retry:
Al Viro6f325a12006-03-18 14:58:37 -05001388 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001389 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 if (!cfqq) {
1392 if (new_cfqq) {
1393 cfqq = new_cfqq;
1394 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001395 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_unlock_irq(cfqd->queue->queue_lock);
1397 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1398 spin_lock_irq(cfqd->queue->queue_lock);
1399 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001400 } else {
1401 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1402 if (!cfqq)
1403 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 memset(cfqq, 0, sizeof(*cfqq));
1407
1408 INIT_HLIST_NODE(&cfqq->cfq_hash);
1409 INIT_LIST_HEAD(&cfqq->cfq_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001410 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 cfqq->key = key;
1413 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1414 atomic_set(&cfqq->ref, 0);
1415 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001416 cfqq->service_last = 0;
1417 /*
1418 * set ->slice_left to allow preemption for a new process
1419 */
1420 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001421 cfq_mark_cfqq_idle_window(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001422 cfq_mark_cfqq_prio_changed(cfqq);
1423 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
1426 if (new_cfqq)
1427 kmem_cache_free(cfq_pool, new_cfqq);
1428
1429 atomic_inc(&cfqq->ref);
1430out:
1431 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1432 return cfqq;
1433}
1434
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001435static void
1436cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1437{
Jens Axboe3793c652006-05-30 21:11:04 +02001438 spin_lock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001439 rb_erase(&cic->rb_node, &ioc->cic_root);
Jens Axboe3793c652006-05-30 21:11:04 +02001440 list_del_init(&cic->queue_list);
1441 spin_unlock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001442 kmem_cache_free(cfq_ioc_pool, cic);
1443 atomic_dec(&ioc_count);
1444}
1445
Jens Axboee2d74ac2006-03-28 08:59:01 +02001446static struct cfq_io_context *
1447cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1448{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001449 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001450 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001451 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001452
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001453restart:
1454 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001455 while (n) {
1456 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001457 /* ->key must be copied to avoid race with cfq_exit_queue() */
1458 k = cic->key;
1459 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001460 cfq_drop_dead_cic(ioc, cic);
1461 goto restart;
1462 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001463
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001464 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001465 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001466 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001467 n = n->rb_right;
1468 else
1469 return cic;
1470 }
1471
1472 return NULL;
1473}
1474
1475static inline void
1476cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1477 struct cfq_io_context *cic)
1478{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001479 struct rb_node **p;
1480 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001481 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001482 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001483
Jens Axboee2d74ac2006-03-28 08:59:01 +02001484 cic->ioc = ioc;
1485 cic->key = cfqd;
1486
1487 ioc->set_ioprio = cfq_ioc_set_ioprio;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001488restart:
1489 parent = NULL;
1490 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001491 while (*p) {
1492 parent = *p;
1493 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001494 /* ->key must be copied to avoid race with cfq_exit_queue() */
1495 k = __cic->key;
1496 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001497 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001498 goto restart;
1499 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001500
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001501 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001502 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001503 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001504 p = &(*p)->rb_right;
1505 else
1506 BUG();
1507 }
1508
Jens Axboe3793c652006-05-30 21:11:04 +02001509 spin_lock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001510 rb_link_node(&cic->rb_node, parent, p);
1511 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1512 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe3793c652006-05-30 21:11:04 +02001513 spin_unlock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001514}
1515
Jens Axboe22e2c502005-06-27 10:55:12 +02001516/*
1517 * Setup general io context and cfq io context. There can be several cfq
1518 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001519 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001520 */
1521static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001522cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Jens Axboe22e2c502005-06-27 10:55:12 +02001524 struct io_context *ioc = NULL;
1525 struct cfq_io_context *cic;
1526
1527 might_sleep_if(gfp_mask & __GFP_WAIT);
1528
1529 ioc = get_io_context(gfp_mask);
1530 if (!ioc)
1531 return NULL;
1532
Jens Axboee2d74ac2006-03-28 08:59:01 +02001533 cic = cfq_cic_rb_lookup(cfqd, ioc);
1534 if (cic)
1535 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001536
Jens Axboee2d74ac2006-03-28 08:59:01 +02001537 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1538 if (cic == NULL)
1539 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001540
Jens Axboee2d74ac2006-03-28 08:59:01 +02001541 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001542out:
1543 return cic;
1544err:
1545 put_io_context(ioc);
1546 return NULL;
1547}
1548
1549static void
1550cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1551{
1552 unsigned long elapsed, ttime;
1553
1554 /*
1555 * if this context already has stuff queued, thinktime is from
1556 * last queue not last end
1557 */
1558#if 0
1559 if (time_after(cic->last_end_request, cic->last_queue))
1560 elapsed = jiffies - cic->last_end_request;
1561 else
1562 elapsed = jiffies - cic->last_queue;
1563#else
1564 elapsed = jiffies - cic->last_end_request;
1565#endif
1566
1567 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1568
1569 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1570 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1571 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1572}
1573
Jens Axboe206dc692006-03-28 13:03:44 +02001574static void
1575cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1576 struct cfq_rq *crq)
1577{
1578 sector_t sdist;
1579 u64 total;
1580
1581 if (cic->last_request_pos < crq->request->sector)
1582 sdist = crq->request->sector - cic->last_request_pos;
1583 else
1584 sdist = cic->last_request_pos - crq->request->sector;
1585
1586 /*
1587 * Don't allow the seek distance to get too large from the
1588 * odd fragment, pagein, etc
1589 */
1590 if (cic->seek_samples <= 60) /* second&third seek */
1591 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1592 else
1593 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1594
1595 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1596 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1597 total = cic->seek_total + (cic->seek_samples/2);
1598 do_div(total, cic->seek_samples);
1599 cic->seek_mean = (sector_t)total;
1600}
Jens Axboe22e2c502005-06-27 10:55:12 +02001601
1602/*
1603 * Disable idle window if the process thinks too long or seeks so much that
1604 * it doesn't matter
1605 */
1606static void
1607cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1608 struct cfq_io_context *cic)
1609{
Jens Axboe3b181522005-06-27 10:56:24 +02001610 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001611
Jens Axboecaaa5f92006-06-16 11:23:00 +02001612 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1613 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001614 enable_idle = 0;
1615 else if (sample_valid(cic->ttime_samples)) {
1616 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1617 enable_idle = 0;
1618 else
1619 enable_idle = 1;
1620 }
1621
Jens Axboe3b181522005-06-27 10:56:24 +02001622 if (enable_idle)
1623 cfq_mark_cfqq_idle_window(cfqq);
1624 else
1625 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001626}
1627
1628
1629/*
1630 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1631 * no or if we aren't sure, a 1 will cause a preempt.
1632 */
1633static int
1634cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1635 struct cfq_rq *crq)
1636{
1637 struct cfq_queue *cfqq = cfqd->active_queue;
1638
1639 if (cfq_class_idle(new_cfqq))
1640 return 0;
1641
1642 if (!cfqq)
Jens Axboecaaa5f92006-06-16 11:23:00 +02001643 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001644
1645 if (cfq_class_idle(cfqq))
1646 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001647 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001648 return 0;
1649 /*
1650 * if it doesn't have slice left, forget it
1651 */
1652 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1653 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001654 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001655 return 1;
1656
1657 return 0;
1658}
1659
1660/*
1661 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1662 * let it have half of its nominal slice.
1663 */
1664static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1665{
1666 struct cfq_queue *__cfqq, *next;
1667
1668 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1669 cfq_resort_rr_list(__cfqq, 1);
1670
1671 if (!cfqq->slice_left)
1672 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1673
1674 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001675 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001676 __cfq_set_active_queue(cfqd, cfqq);
1677}
1678
1679/*
1680 * should really be a ll_rw_blk.c helper
1681 */
1682static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1683{
1684 request_queue_t *q = cfqd->queue;
1685
1686 if (!blk_queue_plugged(q))
1687 q->request_fn(q);
1688 else
1689 __generic_unplug_device(q);
1690}
1691
1692/*
1693 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1694 * something we should do about it
1695 */
1696static void
1697cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1698 struct cfq_rq *crq)
1699{
Jens Axboefd61af02006-06-16 15:35:39 +02001700 struct cfq_io_context *cic = crq->io_context;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001701
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001702 /*
1703 * we never wait for an async request and we don't allow preemption
1704 * of an async request. so just return early
1705 */
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001706 if (!cfq_crq_is_sync(crq)) {
1707 /*
1708 * sync process issued an async request, if it's waiting
1709 * then expire it and kick rq handling.
1710 */
1711 if (cic == cfqd->active_cic &&
1712 del_timer(&cfqd->idle_slice_timer)) {
1713 cfq_slice_expired(cfqd, 0);
1714 cfq_start_queueing(cfqd, cfqq);
1715 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001716 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001717 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001718
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001719 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe206dc692006-03-28 13:03:44 +02001720 cfq_update_io_seektime(cfqd, cic, crq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001721 cfq_update_idle_window(cfqd, cfqq, cic);
1722
1723 cic->last_queue = jiffies;
Jens Axboe206dc692006-03-28 13:03:44 +02001724 cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001725
1726 if (cfqq == cfqd->active_queue) {
1727 /*
1728 * if we are waiting for a request for this queue, let it rip
1729 * immediately and flag that we must not expire this queue
1730 * just now
1731 */
Jens Axboe3b181522005-06-27 10:56:24 +02001732 if (cfq_cfqq_wait_request(cfqq)) {
1733 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001734 del_timer(&cfqd->idle_slice_timer);
1735 cfq_start_queueing(cfqd, cfqq);
1736 }
1737 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1738 /*
1739 * not the active queue - expire current slice if it is
1740 * idle and has expired it's mean thinktime or this new queue
1741 * has some old slice time left and is of higher priority
1742 */
1743 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001744 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001745 cfq_start_queueing(cfqd, cfqq);
1746 }
1747}
1748
Jens Axboeb4878f22005-10-20 16:42:29 +02001749static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001750{
Jens Axboeb4878f22005-10-20 16:42:29 +02001751 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001752 struct cfq_rq *crq = RQ_DATA(rq);
1753 struct cfq_queue *cfqq = crq->cfq_queue;
1754
1755 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Jens Axboe22e2c502005-06-27 10:55:12 +02001759 list_add_tail(&rq->queuelist, &cfqq->fifo);
1760
Jens Axboe22e2c502005-06-27 10:55:12 +02001761 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764static void cfq_completed_request(request_queue_t *q, struct request *rq)
1765{
1766 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001767 struct cfq_queue *cfqq = crq->cfq_queue;
1768 struct cfq_data *cfqd = cfqq->cfqd;
1769 const int sync = cfq_crq_is_sync(crq);
1770 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Jens Axboeb4878f22005-10-20 16:42:29 +02001772 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Jens Axboeb4878f22005-10-20 16:42:29 +02001774 WARN_ON(!cfqd->rq_in_driver);
1775 WARN_ON(!cfqq->on_dispatch[sync]);
1776 cfqd->rq_in_driver--;
1777 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Jens Axboeb4878f22005-10-20 16:42:29 +02001779 if (!cfq_class_idle(cfqq))
1780 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001781
Jens Axboeb4878f22005-10-20 16:42:29 +02001782 if (!cfq_cfqq_dispatched(cfqq)) {
1783 if (cfq_cfqq_on_rr(cfqq)) {
1784 cfqq->service_last = now;
1785 cfq_resort_rr_list(cfqq, 0);
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
Jens Axboecaaa5f92006-06-16 11:23:00 +02001789 if (sync)
Jens Axboeb4878f22005-10-20 16:42:29 +02001790 crq->io_context->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001791
1792 /*
1793 * If this is the active queue, check if it needs to be expired,
1794 * or if we want to idle in case it has no pending requests.
1795 */
1796 if (cfqd->active_queue == cfqq) {
1797 if (time_after(now, cfqq->slice_end))
1798 cfq_slice_expired(cfqd, 0);
Jens Axboedd67d052006-06-21 09:36:18 +02001799 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001800 if (!cfq_arm_slice_timer(cfqd, cfqq))
1801 cfq_schedule_dispatch(cfqd);
1802 }
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806static struct request *
1807cfq_former_request(request_queue_t *q, struct request *rq)
1808{
1809 struct cfq_rq *crq = RQ_DATA(rq);
1810 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1811
1812 if (rbprev)
1813 return rb_entry_crq(rbprev)->request;
1814
1815 return NULL;
1816}
1817
1818static struct request *
1819cfq_latter_request(request_queue_t *q, struct request *rq)
1820{
1821 struct cfq_rq *crq = RQ_DATA(rq);
1822 struct rb_node *rbnext = rb_next(&crq->rb_node);
1823
1824 if (rbnext)
1825 return rb_entry_crq(rbnext)->request;
1826
1827 return NULL;
1828}
1829
Jens Axboe22e2c502005-06-27 10:55:12 +02001830/*
1831 * we temporarily boost lower priority queues if they are holding fs exclusive
1832 * resources. they are boosted to normal prio (CLASS_BE/4)
1833 */
1834static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Jens Axboe22e2c502005-06-27 10:55:12 +02001836 const int ioprio_class = cfqq->ioprio_class;
1837 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Jens Axboe22e2c502005-06-27 10:55:12 +02001839 if (has_fs_excl()) {
1840 /*
1841 * boost idle prio on transactions that would lock out other
1842 * users of the filesystem
1843 */
1844 if (cfq_class_idle(cfqq))
1845 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1846 if (cfqq->ioprio > IOPRIO_NORM)
1847 cfqq->ioprio = IOPRIO_NORM;
1848 } else {
1849 /*
1850 * check if we need to unboost the queue
1851 */
1852 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1853 cfqq->ioprio_class = cfqq->org_ioprio_class;
1854 if (cfqq->ioprio != cfqq->org_ioprio)
1855 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 /*
1859 * refile between round-robin lists if we moved the priority class
1860 */
1861 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001862 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001863 cfq_resort_rr_list(cfqq, 0);
1864}
1865
Jens Axboe22e2c502005-06-27 10:55:12 +02001866static inline int
1867__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1868 struct task_struct *task, int rw)
1869{
Jens Axboe3b181522005-06-27 10:56:24 +02001870 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001871 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001872 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001873 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001874 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001875
1876 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001877}
1878
1879static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1880{
1881 struct cfq_data *cfqd = q->elevator->elevator_data;
1882 struct task_struct *tsk = current;
1883 struct cfq_queue *cfqq;
1884
1885 /*
1886 * don't force setup of a queue from here, as a call to may_queue
1887 * does not necessarily imply that a request actually will be queued.
1888 * so just lookup a possibly existing queue, or return 'may queue'
1889 * if that fails
1890 */
Jens Axboe3b181522005-06-27 10:56:24 +02001891 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001892 if (cfqq) {
1893 cfq_init_prio_data(cfqq);
1894 cfq_prio_boost(cfqq);
1895
1896 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1897 }
1898
1899 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1903{
Jens Axboe22e2c502005-06-27 10:55:12 +02001904 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jens Axboe271f18f2006-06-13 08:08:38 +02001906 if (unlikely(cfqd->rq_starved)) {
1907 struct request_list *rl = &q->rq;
1908
Jens Axboe22e2c502005-06-27 10:55:12 +02001909 smp_mb();
1910 if (waitqueue_active(&rl->wait[READ]))
1911 wake_up(&rl->wait[READ]);
Jens Axboe22e2c502005-06-27 10:55:12 +02001912 if (waitqueue_active(&rl->wait[WRITE]))
1913 wake_up(&rl->wait[WRITE]);
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917/*
1918 * queue lock held here
1919 */
1920static void cfq_put_request(request_queue_t *q, struct request *rq)
1921{
1922 struct cfq_data *cfqd = q->elevator->elevator_data;
1923 struct cfq_rq *crq = RQ_DATA(rq);
1924
1925 if (crq) {
1926 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001927 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Jens Axboe22e2c502005-06-27 10:55:12 +02001929 BUG_ON(!cfqq->allocated[rw]);
1930 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Jens Axboe22e2c502005-06-27 10:55:12 +02001932 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 mempool_free(crq, cfqd->crq_pool);
1935 rq->elevator_private = NULL;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 cfq_check_waiters(q, cfqq);
1938 cfq_put_queue(cfqq);
1939 }
1940}
1941
1942/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001943 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001945static int
1946cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001947 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001950 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct cfq_io_context *cic;
1952 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001953 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001954 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct cfq_rq *crq;
1956 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001957 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 might_sleep_if(gfp_mask & __GFP_WAIT);
1960
Jens Axboee2d74ac2006-03-28 08:59:01 +02001961 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 spin_lock_irqsave(q->queue_lock, flags);
1964
Jens Axboe22e2c502005-06-27 10:55:12 +02001965 if (!cic)
1966 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Al Viro12a05732006-03-18 13:38:01 -05001968 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001969 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001970 if (!cfqq)
1971 goto queue_fail;
1972
Al Viro12a05732006-03-18 13:38:01 -05001973 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001974 } else
Al Viro12a05732006-03-18 13:38:01 -05001975 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001978 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001979 cfqd->rq_starved = 0;
1980 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 spin_unlock_irqrestore(q->queue_lock, flags);
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1984 if (crq) {
Jens Axboedd67d052006-06-21 09:36:18 +02001985 RB_CLEAR_NODE(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 crq->rb_key = 0;
1987 crq->request = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 crq->cfq_queue = cfqq;
1989 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001990
Al Viro12a05732006-03-18 13:38:01 -05001991 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02001992 cfq_mark_crq_is_sync(crq);
1993 else
1994 cfq_clear_crq_is_sync(crq);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return 0;
1998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 spin_lock_irqsave(q->queue_lock, flags);
2001 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02002002 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02002003 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002005queue_fail:
2006 if (cic)
2007 put_io_context(cic->ioc);
2008 /*
2009 * mark us rq allocation starved. we need to kickstart the process
2010 * ourselves if there are no pending requests that can do it for us.
2011 * that would be an extremely rare OOM situation
2012 */
2013 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002014 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 spin_unlock_irqrestore(q->queue_lock, flags);
2016 return 1;
2017}
2018
Jens Axboe22e2c502005-06-27 10:55:12 +02002019static void cfq_kick_queue(void *data)
2020{
2021 request_queue_t *q = data;
2022 struct cfq_data *cfqd = q->elevator->elevator_data;
2023 unsigned long flags;
2024
2025 spin_lock_irqsave(q->queue_lock, flags);
2026
2027 if (cfqd->rq_starved) {
2028 struct request_list *rl = &q->rq;
2029
2030 /*
2031 * we aren't guaranteed to get a request after this, but we
2032 * have to be opportunistic
2033 */
2034 smp_mb();
2035 if (waitqueue_active(&rl->wait[READ]))
2036 wake_up(&rl->wait[READ]);
2037 if (waitqueue_active(&rl->wait[WRITE]))
2038 wake_up(&rl->wait[WRITE]);
2039 }
2040
2041 blk_remove_plug(q);
2042 q->request_fn(q);
2043 spin_unlock_irqrestore(q->queue_lock, flags);
2044}
2045
2046/*
2047 * Timer running if the active_queue is currently idling inside its time slice
2048 */
2049static void cfq_idle_slice_timer(unsigned long data)
2050{
2051 struct cfq_data *cfqd = (struct cfq_data *) data;
2052 struct cfq_queue *cfqq;
2053 unsigned long flags;
2054
2055 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2056
2057 if ((cfqq = cfqd->active_queue) != NULL) {
2058 unsigned long now = jiffies;
2059
2060 /*
2061 * expired
2062 */
2063 if (time_after(now, cfqq->slice_end))
2064 goto expire;
2065
2066 /*
2067 * only expire and reinvoke request handler, if there are
2068 * other queues with pending requests
2069 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002070 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002071 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002072
2073 /*
2074 * not expired and it has a request pending, let it dispatch
2075 */
Jens Axboedd67d052006-06-21 09:36:18 +02002076 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002077 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002078 goto out_kick;
2079 }
2080 }
2081expire:
2082 cfq_slice_expired(cfqd, 0);
2083out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002084 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002085out_cont:
2086 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2087}
2088
2089/*
2090 * Timer running if an idle class queue is waiting for service
2091 */
2092static void cfq_idle_class_timer(unsigned long data)
2093{
2094 struct cfq_data *cfqd = (struct cfq_data *) data;
2095 unsigned long flags, end;
2096
2097 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2098
2099 /*
2100 * race with a non-idle queue, reset timer
2101 */
2102 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002103 if (!time_after_eq(jiffies, end))
2104 mod_timer(&cfqd->idle_class_timer, end);
2105 else
Jens Axboe3b181522005-06-27 10:56:24 +02002106 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002107
2108 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2109}
2110
Jens Axboe3b181522005-06-27 10:56:24 +02002111static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2112{
2113 del_timer_sync(&cfqd->idle_slice_timer);
2114 del_timer_sync(&cfqd->idle_class_timer);
2115 blk_sync_queue(cfqd->queue);
2116}
Jens Axboe22e2c502005-06-27 10:55:12 +02002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118static void cfq_exit_queue(elevator_t *e)
2119{
Jens Axboe22e2c502005-06-27 10:55:12 +02002120 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002121 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002122
Jens Axboe3b181522005-06-27 10:56:24 +02002123 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002124
Jens Axboe3793c652006-05-30 21:11:04 +02002125 spin_lock(&cfq_exit_lock);
Al Virod9ff4182006-03-18 13:51:22 -05002126 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002127
Al Virod9ff4182006-03-18 13:51:22 -05002128 if (cfqd->active_queue)
2129 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002130
2131 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002132 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2133 struct cfq_io_context,
2134 queue_list);
2135 if (cic->cfqq[ASYNC]) {
2136 cfq_put_queue(cic->cfqq[ASYNC]);
2137 cic->cfqq[ASYNC] = NULL;
2138 }
2139 if (cic->cfqq[SYNC]) {
2140 cfq_put_queue(cic->cfqq[SYNC]);
2141 cic->cfqq[SYNC] = NULL;
2142 }
2143 cic->key = NULL;
2144 list_del_init(&cic->queue_list);
2145 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002146
Al Virod9ff4182006-03-18 13:51:22 -05002147 spin_unlock_irq(q->queue_lock);
Jens Axboe3793c652006-05-30 21:11:04 +02002148 spin_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002149
2150 cfq_shutdown_timer_wq(cfqd);
2151
2152 mempool_destroy(cfqd->crq_pool);
Al Viroa90d7422006-03-18 12:05:37 -05002153 kfree(cfqd->cfq_hash);
2154 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Jens Axboebc1c1162006-06-08 08:49:06 +02002157static void *cfq_init_queue(request_queue_t *q, elevator_t *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct cfq_data *cfqd;
2160 int i;
2161
2162 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2163 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002167
2168 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2169 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2170
2171 INIT_LIST_HEAD(&cfqd->busy_rr);
2172 INIT_LIST_HEAD(&cfqd->cur_rr);
2173 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002175 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2178 if (!cfqd->cfq_hash)
Jens Axboe98170642006-07-28 09:23:08 +02002179 goto out_crqhash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Matthew Dobson93d23412006-03-26 01:37:50 -08002181 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (!cfqd->crq_pool)
2183 goto out_crqpool;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2186 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jens Axboe22e2c502005-06-27 10:55:12 +02002190 init_timer(&cfqd->idle_slice_timer);
2191 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2192 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2193
2194 init_timer(&cfqd->idle_class_timer);
2195 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2196 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2197
2198 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 cfqd->cfq_queued = cfq_queued;
2201 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212out_crqpool:
2213 kfree(cfqd->cfq_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214out_crqhash:
2215 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002216 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219static void cfq_slab_kill(void)
2220{
2221 if (crq_pool)
2222 kmem_cache_destroy(crq_pool);
2223 if (cfq_pool)
2224 kmem_cache_destroy(cfq_pool);
2225 if (cfq_ioc_pool)
2226 kmem_cache_destroy(cfq_ioc_pool);
2227}
2228
2229static int __init cfq_slab_setup(void)
2230{
2231 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2232 NULL, NULL);
2233 if (!crq_pool)
2234 goto fail;
2235
2236 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2237 NULL, NULL);
2238 if (!cfq_pool)
2239 goto fail;
2240
2241 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2242 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2243 if (!cfq_ioc_pool)
2244 goto fail;
2245
2246 return 0;
2247fail:
2248 cfq_slab_kill();
2249 return -ENOMEM;
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/*
2253 * sysfs parts below -->
2254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256static ssize_t
2257cfq_var_show(unsigned int var, char *page)
2258{
2259 return sprintf(page, "%d\n", var);
2260}
2261
2262static ssize_t
2263cfq_var_store(unsigned int *var, const char *page, size_t count)
2264{
2265 char *p = (char *) page;
2266
2267 *var = simple_strtoul(p, &p, 10);
2268 return count;
2269}
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002272static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002274 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 unsigned int __data = __VAR; \
2276 if (__CONV) \
2277 __data = jiffies_to_msecs(__data); \
2278 return cfq_var_show(__data, (page)); \
2279}
2280SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2281SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002282SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2283SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002284SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2285SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002286SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2287SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2288SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2289SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290#undef SHOW_FUNCTION
2291
2292#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002293static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002295 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 unsigned int __data; \
2297 int ret = cfq_var_store(&__data, (page), count); \
2298 if (__data < (MIN)) \
2299 __data = (MIN); \
2300 else if (__data > (MAX)) \
2301 __data = (MAX); \
2302 if (__CONV) \
2303 *(__PTR) = msecs_to_jiffies(__data); \
2304 else \
2305 *(__PTR) = __data; \
2306 return ret; \
2307}
2308STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2309STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002310STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2311STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002312STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2313STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002314STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2315STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2316STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2317STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318#undef STORE_FUNCTION
2319
Al Viroe572ec72006-03-18 22:27:18 -05002320#define CFQ_ATTR(name) \
2321 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002322
Al Viroe572ec72006-03-18 22:27:18 -05002323static struct elv_fs_entry cfq_attrs[] = {
2324 CFQ_ATTR(quantum),
2325 CFQ_ATTR(queued),
2326 CFQ_ATTR(fifo_expire_sync),
2327 CFQ_ATTR(fifo_expire_async),
2328 CFQ_ATTR(back_seek_max),
2329 CFQ_ATTR(back_seek_penalty),
2330 CFQ_ATTR(slice_sync),
2331 CFQ_ATTR(slice_async),
2332 CFQ_ATTR(slice_async_rq),
2333 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002334 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337static struct elevator_type iosched_cfq = {
2338 .ops = {
2339 .elevator_merge_fn = cfq_merge,
2340 .elevator_merged_fn = cfq_merged_request,
2341 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002342 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002344 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 .elevator_deactivate_req_fn = cfq_deactivate_request,
2346 .elevator_queue_empty_fn = cfq_queue_empty,
2347 .elevator_completed_req_fn = cfq_completed_request,
2348 .elevator_former_req_fn = cfq_former_request,
2349 .elevator_latter_req_fn = cfq_latter_request,
2350 .elevator_set_req_fn = cfq_set_request,
2351 .elevator_put_req_fn = cfq_put_request,
2352 .elevator_may_queue_fn = cfq_may_queue,
2353 .elevator_init_fn = cfq_init_queue,
2354 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002355 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 },
Al Viro3d1ab402006-03-18 18:35:43 -05002357 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 .elevator_name = "cfq",
2359 .elevator_owner = THIS_MODULE,
2360};
2361
2362static int __init cfq_init(void)
2363{
2364 int ret;
2365
Jens Axboe22e2c502005-06-27 10:55:12 +02002366 /*
2367 * could be 0 on HZ < 1000 setups
2368 */
2369 if (!cfq_slice_async)
2370 cfq_slice_async = 1;
2371 if (!cfq_slice_idle)
2372 cfq_slice_idle = 1;
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (cfq_slab_setup())
2375 return -ENOMEM;
2376
2377 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002378 if (ret)
2379 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return ret;
2382}
2383
2384static void __exit cfq_exit(void)
2385{
Al Viro334e94d2006-03-18 15:05:53 -05002386 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002388 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002389 /* ioc_gone's update must be visible before reading ioc_count */
2390 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05002391 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002392 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002393 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002394 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397module_init(cfq_init);
2398module_exit(cfq_exit);
2399
2400MODULE_AUTHOR("Jens Axboe");
2401MODULE_LICENSE("GPL");
2402MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");