blob: 93ba54605eda98c9c62657e3e461526c34a31a61 [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/config.h>
10#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hash.h>
14#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * tunables
19 */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_quantum = 4; /* max queue in one round of service */
21static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
22static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
23static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
24static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020027static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_slice_async_rq = 2;
Jens Axboe206dc692006-03-28 13:03:44 +020029static int cfq_slice_idle = HZ / 70;
Jens Axboe22e2c502005-06-27 10:55:12 +020030
31#define CFQ_IDLE_GRACE (HZ / 10)
32#define CFQ_SLICE_SCALE (5)
33
34#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020035
Jens Axboe3793c652006-05-30 21:11:04 +020036static DEFINE_SPINLOCK(cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -050037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * for the hash of cfqq inside the cfqd
40 */
41#define CFQ_QHASH_SHIFT 6
42#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
43#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
44
45/*
46 * for the hash of crq inside the cfqq
47 */
48#define CFQ_MHASH_SHIFT 6
49#define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
50#define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
51#define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
52#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
53#define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
54
55#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
Jens Axboe22e2c502005-06-27 10:55:12 +020056#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define RQ_DATA(rq) (rq)->elevator_private
59
60/*
61 * rb-tree defines
62 */
63#define RB_NONE (2)
64#define RB_EMPTY(node) ((node)->rb_node == NULL)
65#define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
66#define RB_CLEAR(node) do { \
67 (node)->rb_parent = NULL; \
68 RB_CLEAR_COLOR((node)); \
69 (node)->rb_right = NULL; \
70 (node)->rb_left = NULL; \
71} while (0)
72#define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
74#define rq_rb_key(rq) (rq)->sector
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static kmem_cache_t *crq_pool;
77static kmem_cache_t *cfq_pool;
78static kmem_cache_t *cfq_ioc_pool;
79
Al Viro334e94d2006-03-18 15:05:53 -050080static atomic_t ioc_count = ATOMIC_INIT(0);
81static struct completion *ioc_gone;
82
Jens Axboe22e2c502005-06-27 10:55:12 +020083#define CFQ_PRIO_LISTS IOPRIO_BE_NR
84#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
85#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
86#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
87
Jens Axboe3b181522005-06-27 10:56:24 +020088#define ASYNC (0)
89#define SYNC (1)
90
91#define cfq_cfqq_dispatched(cfqq) \
92 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
93
94#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
95
96#define cfq_cfqq_sync(cfqq) \
97 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +020098
Jens Axboe206dc692006-03-28 13:03:44 +020099#define sample_valid(samples) ((samples) > 80)
100
Jens Axboe22e2c502005-06-27 10:55:12 +0200101/*
102 * Per block device queue structure
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +0200105 request_queue_t *queue;
106
107 /*
108 * rr list of queues with requests and the count of them
109 */
110 struct list_head rr_list[CFQ_PRIO_LISTS];
111 struct list_head busy_rr;
112 struct list_head cur_rr;
113 struct list_head idle_rr;
114 unsigned int busy_queues;
115
116 /*
117 * non-ordered list of empty cfqq's
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct list_head empty_list;
120
Jens Axboe22e2c502005-06-27 10:55:12 +0200121 /*
122 * cfqq lookup hash
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jens Axboe22e2c502005-06-27 10:55:12 +0200126 /*
127 * global crq hash for all queues
128 */
129 struct hlist_head *crq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 unsigned int max_queued;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 mempool_t *crq_pool;
134
Jens Axboe22e2c502005-06-27 10:55:12 +0200135 int rq_in_driver;
136
137 /*
138 * schedule slice state info
139 */
140 /*
141 * idle window management
142 */
143 struct timer_list idle_slice_timer;
144 struct work_struct unplug_work;
145
146 struct cfq_queue *active_queue;
147 struct cfq_io_context *active_cic;
148 int cur_prio, cur_end_prio;
149 unsigned int dispatch_slice;
150
151 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200154 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jens Axboe22e2c502005-06-27 10:55:12 +0200156 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /*
159 * tunables, see top of file
160 */
161 unsigned int cfq_quantum;
162 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200163 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int cfq_back_penalty;
165 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 unsigned int cfq_slice[2];
167 unsigned int cfq_slice_async_rq;
168 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500169
170 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Jens Axboe22e2c502005-06-27 10:55:12 +0200173/*
174 * Per process-grouping structure
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176struct cfq_queue {
177 /* reference count */
178 atomic_t ref;
179 /* parent cfq_data */
180 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200181 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct hlist_node cfq_hash;
183 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200184 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* on either rr or empty list of cfqd */
186 struct list_head cfq_list;
187 /* sorted list of pending requests */
188 struct rb_root sort_list;
189 /* if fifo isn't expired, next request to serve */
190 struct cfq_rq *next_crq;
191 /* requests queued in sort_list */
192 int queued[2];
193 /* currently allocated requests */
194 int allocated[2];
195 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200196 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jens Axboe22e2c502005-06-27 10:55:12 +0200198 unsigned long slice_start;
199 unsigned long slice_end;
200 unsigned long slice_left;
201 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jens Axboe3b181522005-06-27 10:56:24 +0200203 /* number of requests that are on the dispatch list */
204 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200205
206 /* io prio of this group */
207 unsigned short ioprio, org_ioprio;
208 unsigned short ioprio_class, org_ioprio_class;
209
Jens Axboe3b181522005-06-27 10:56:24 +0200210 /* various state flags, see below */
211 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214struct cfq_rq {
215 struct rb_node rb_node;
216 sector_t rb_key;
217 struct request *request;
218 struct hlist_node hash;
219
220 struct cfq_queue *cfq_queue;
221 struct cfq_io_context *io_context;
222
Jens Axboe3b181522005-06-27 10:56:24 +0200223 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Jens Axboe3b181522005-06-27 10:56:24 +0200226enum cfqq_state_flags {
227 CFQ_CFQQ_FLAG_on_rr = 0,
228 CFQ_CFQQ_FLAG_wait_request,
229 CFQ_CFQQ_FLAG_must_alloc,
230 CFQ_CFQQ_FLAG_must_alloc_slice,
231 CFQ_CFQQ_FLAG_must_dispatch,
232 CFQ_CFQQ_FLAG_fifo_expire,
233 CFQ_CFQQ_FLAG_idle_window,
234 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200235};
236
237#define CFQ_CFQQ_FNS(name) \
238static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
239{ \
240 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
241} \
242static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
243{ \
244 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
245} \
246static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
247{ \
248 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
249}
250
251CFQ_CFQQ_FNS(on_rr);
252CFQ_CFQQ_FNS(wait_request);
253CFQ_CFQQ_FNS(must_alloc);
254CFQ_CFQQ_FNS(must_alloc_slice);
255CFQ_CFQQ_FNS(must_dispatch);
256CFQ_CFQQ_FNS(fifo_expire);
257CFQ_CFQQ_FNS(idle_window);
258CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200259#undef CFQ_CFQQ_FNS
260
261enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200262 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200263};
264
265#define CFQ_CRQ_FNS(name) \
266static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
267{ \
268 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
269} \
270static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
271{ \
272 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
273} \
274static inline int cfq_crq_##name(const struct cfq_rq *crq) \
275{ \
276 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
277}
278
Jens Axboe3b181522005-06-27 10:56:24 +0200279CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200280#undef CFQ_CRQ_FNS
281
282static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200283static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Al Viro6f325a12006-03-18 14:58:37 -0500284static 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 -0700285
Jens Axboe22e2c502005-06-27 10:55:12 +0200286#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*
289 * lots of deadline iosched dupes, can be abstracted later...
290 */
291static inline void cfq_del_crq_hash(struct cfq_rq *crq)
292{
293 hlist_del_init(&crq->hash);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
297{
298 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
301}
302
303static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
304{
305 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
306 struct hlist_node *entry, *next;
307
308 hlist_for_each_safe(entry, next, hash_list) {
309 struct cfq_rq *crq = list_entry_hash(entry);
310 struct request *__rq = crq->request;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!rq_mergeable(__rq)) {
313 cfq_del_crq_hash(crq);
314 continue;
315 }
316
317 if (rq_hash_key(__rq) == offset)
318 return __rq;
319 }
320
321 return NULL;
322}
323
Andrew Morton99f95e52005-06-27 20:14:05 -0700324/*
325 * scheduler run of queue, if there are requests pending and no one in the
326 * driver that will restart queueing
327 */
328static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
329{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100330 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700331 kblockd_schedule_work(&cfqd->unplug_work);
332}
333
334static int cfq_queue_empty(request_queue_t *q)
335{
336 struct cfq_data *cfqd = q->elevator->elevator_data;
337
Jens Axboeb4878f22005-10-20 16:42:29 +0200338 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700339}
340
Jens Axboe206dc692006-03-28 13:03:44 +0200341static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
342{
343 if (rw == READ || process_sync(task))
344 return task->pid;
345
346 return CFQ_KEY_ASYNC;
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/*
350 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
351 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200352 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354static struct cfq_rq *
355cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
356{
357 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200359#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
360#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
361 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (crq1 == NULL || crq1 == crq2)
364 return crq2;
365 if (crq2 == NULL)
366 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200367
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200368 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
369 return crq1;
370 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200371 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 s1 = crq1->request->sector;
374 s2 = crq2->request->sector;
375
376 last = cfqd->last_sector;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * by definition, 1KiB is 2 sectors
380 */
381 back_max = cfqd->cfq_back_max * 2;
382
383 /*
384 * Strict one way elevator _except_ in the case where we allow
385 * short backward seeks which are biased as twice the cost of a
386 * similar forward seek.
387 */
388 if (s1 >= last)
389 d1 = s1 - last;
390 else if (s1 + back_max >= last)
391 d1 = (last - s1) * cfqd->cfq_back_penalty;
392 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200393 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (s2 >= last)
396 d2 = s2 - last;
397 else if (s2 + back_max >= last)
398 d2 = (last - s2) * cfqd->cfq_back_penalty;
399 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200400 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Andreas Mohre8a99052006-03-28 08:59:49 +0200404 /*
405 * By doing switch() on the bit mask "wrap" we avoid having to
406 * check two variables for all permutations: --> faster!
407 */
408 switch (wrap) {
409 case 0: /* common case for CFQ: crq1 and crq2 not wrapped */
410 if (d1 < d2)
411 return crq1;
412 else if (d2 < d1)
413 return crq2;
414 else {
415 if (s1 >= s2)
416 return crq1;
417 else
418 return crq2;
419 }
420
421 case CFQ_RQ2_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return crq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200423 case CFQ_RQ1_WRAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return crq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200425 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both crqs wrapped */
426 default:
427 /*
428 * Since both rqs are wrapped,
429 * start with the one that's further behind head
430 * (--> only *one* back seek required),
431 * since back seek takes more time than forward.
432 */
433 if (s1 <= s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return crq1;
435 else
436 return crq2;
437 }
438}
439
440/*
441 * would be nice to take fifo expire time into account as well
442 */
443static struct cfq_rq *
444cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
445 struct cfq_rq *last)
446{
447 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
448 struct rb_node *rbnext, *rbprev;
449
Jens Axboeb4878f22005-10-20 16:42:29 +0200450 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200452 if (rbnext == &last->rb_node)
453 rbnext = NULL;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 rbprev = rb_prev(&last->rb_node);
457
458 if (rbprev)
459 crq_prev = rb_entry_crq(rbprev);
460 if (rbnext)
461 crq_next = rb_entry_crq(rbnext);
462
463 return cfq_choose_req(cfqd, crq_next, crq_prev);
464}
465
466static void cfq_update_next_crq(struct cfq_rq *crq)
467{
468 struct cfq_queue *cfqq = crq->cfq_queue;
469
470 if (cfqq->next_crq == crq)
471 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
472}
473
Jens Axboe22e2c502005-06-27 10:55:12 +0200474static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jens Axboe22e2c502005-06-27 10:55:12 +0200476 struct cfq_data *cfqd = cfqq->cfqd;
477 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jens Axboe3b181522005-06-27 10:56:24 +0200479 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 list_del(&cfqq->cfq_list);
482
Jens Axboe22e2c502005-06-27 10:55:12 +0200483 if (cfq_class_rt(cfqq))
484 list = &cfqd->cur_rr;
485 else if (cfq_class_idle(cfqq))
486 list = &cfqd->idle_rr;
487 else {
488 /*
489 * if cfqq has requests in flight, don't allow it to be
490 * found in cfq_set_active_queue before it has finished them.
491 * this is done to increase fairness between a process that
492 * has lots of io pending vs one that only generates one
493 * sporadically or synchronously
494 */
Jens Axboe3b181522005-06-27 10:56:24 +0200495 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200496 list = &cfqd->busy_rr;
497 else
498 list = &cfqd->rr_list[cfqq->ioprio];
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200502 * if queue was preempted, just add to front to be fair. busy_rr
503 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200505 if (preempted || list == &cfqd->busy_rr) {
506 list_add(&cfqq->cfq_list, list);
507 return;
508 }
509
510 /*
511 * sort by when queue was last serviced
512 */
513 entry = list;
514 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
516
Jens Axboe22e2c502005-06-27 10:55:12 +0200517 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200519 if (time_before(__cfqq->service_last, cfqq->service_last))
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 list_add(&cfqq->cfq_list, entry);
524}
525
526/*
527 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200528 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
530static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200531cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jens Axboe3b181522005-06-27 10:56:24 +0200533 BUG_ON(cfq_cfqq_on_rr(cfqq));
534 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 cfqd->busy_queues++;
536
Jens Axboeb4878f22005-10-20 16:42:29 +0200537 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540static inline void
541cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
542{
Jens Axboe3b181522005-06-27 10:56:24 +0200543 BUG_ON(!cfq_cfqq_on_rr(cfqq));
544 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200545 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 BUG_ON(!cfqd->busy_queues);
548 cfqd->busy_queues--;
549}
550
551/*
552 * rb tree support functions
553 */
554static inline void cfq_del_crq_rb(struct cfq_rq *crq)
555{
556 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200557 struct cfq_data *cfqd = cfqq->cfqd;
558 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jens Axboeb4878f22005-10-20 16:42:29 +0200560 BUG_ON(!cfqq->queued[sync]);
561 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jens Axboeb4878f22005-10-20 16:42:29 +0200563 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jens Axboeb4878f22005-10-20 16:42:29 +0200565 rb_erase(&crq->rb_node, &cfqq->sort_list);
566 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jens Axboeb4878f22005-10-20 16:42:29 +0200568 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
569 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static struct cfq_rq *
573__cfq_add_crq_rb(struct cfq_rq *crq)
574{
575 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
576 struct rb_node *parent = NULL;
577 struct cfq_rq *__crq;
578
579 while (*p) {
580 parent = *p;
581 __crq = rb_entry_crq(parent);
582
583 if (crq->rb_key < __crq->rb_key)
584 p = &(*p)->rb_left;
585 else if (crq->rb_key > __crq->rb_key)
586 p = &(*p)->rb_right;
587 else
588 return __crq;
589 }
590
591 rb_link_node(&crq->rb_node, parent, p);
592 return NULL;
593}
594
595static void cfq_add_crq_rb(struct cfq_rq *crq)
596{
597 struct cfq_queue *cfqq = crq->cfq_queue;
598 struct cfq_data *cfqd = cfqq->cfqd;
599 struct request *rq = crq->request;
600 struct cfq_rq *__alias;
601
602 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200603 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * looks a little odd, but the first insert might return an alias.
607 * if that happens, put the alias on the dispatch list
608 */
609 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200610 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
613
Jens Axboe3b181522005-06-27 10:56:24 +0200614 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200615 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * check if this request is a better next-serve candidate
619 */
620 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
621}
622
623static inline void
624cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
625{
Jens Axboeb4878f22005-10-20 16:42:29 +0200626 rb_erase(&crq->rb_node, &cfqq->sort_list);
627 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 cfq_add_crq_rb(crq);
630}
631
Jens Axboe206dc692006-03-28 13:03:44 +0200632static struct request *
633cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jens Axboe206dc692006-03-28 13:03:44 +0200635 struct task_struct *tsk = current;
636 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio));
637 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct rb_node *n;
Jens Axboe206dc692006-03-28 13:03:44 +0200639 sector_t sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Jens Axboe206dc692006-03-28 13:03:44 +0200641 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!cfqq)
643 goto out;
644
Jens Axboe206dc692006-03-28 13:03:44 +0200645 sector = bio->bi_sector + bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 n = cfqq->sort_list.rb_node;
647 while (n) {
648 struct cfq_rq *crq = rb_entry_crq(n);
649
650 if (sector < crq->rb_key)
651 n = n->rb_left;
652 else if (sector > crq->rb_key)
653 n = n->rb_right;
654 else
655 return crq->request;
656 }
657
658out:
659 return NULL;
660}
661
Jens Axboeb4878f22005-10-20 16:42:29 +0200662static void cfq_activate_request(request_queue_t *q, struct request *rq)
663{
664 struct cfq_data *cfqd = q->elevator->elevator_data;
665
666 cfqd->rq_in_driver++;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
670{
Jens Axboe22e2c502005-06-27 10:55:12 +0200671 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Jens Axboeb4878f22005-10-20 16:42:29 +0200673 WARN_ON(!cfqd->rq_in_driver);
674 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Jens Axboeb4878f22005-10-20 16:42:29 +0200677static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct cfq_rq *crq = RQ_DATA(rq);
680
Jens Axboeb4878f22005-10-20 16:42:29 +0200681 list_del_init(&rq->queuelist);
682 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200683 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686static int
687cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
688{
689 struct cfq_data *cfqd = q->elevator->elevator_data;
690 struct request *__rq;
691 int ret;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200694 if (__rq && elv_rq_merge_ok(__rq, bio)) {
695 ret = ELEVATOR_BACK_MERGE;
696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Jens Axboe206dc692006-03-28 13:03:44 +0200699 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200700 if (__rq && elv_rq_merge_ok(__rq, bio)) {
701 ret = ELEVATOR_FRONT_MERGE;
702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 return ELEVATOR_NO_MERGE;
706out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *req = __rq;
708 return ret;
709}
710
711static void cfq_merged_request(request_queue_t *q, struct request *req)
712{
713 struct cfq_data *cfqd = q->elevator->elevator_data;
714 struct cfq_rq *crq = RQ_DATA(req);
715
716 cfq_del_crq_hash(crq);
717 cfq_add_crq_hash(cfqd, crq);
718
Jens Axboeb4878f22005-10-20 16:42:29 +0200719 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct cfq_queue *cfqq = crq->cfq_queue;
721
722 cfq_update_next_crq(crq);
723 cfq_reposition_crq_rb(cfqq, crq);
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static void
728cfq_merged_requests(request_queue_t *q, struct request *rq,
729 struct request *next)
730{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 cfq_merged_request(q, rq);
732
Jens Axboe22e2c502005-06-27 10:55:12 +0200733 /*
734 * reposition in fifo if next is older than rq
735 */
736 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
737 time_before(next->start_time, rq->start_time))
738 list_move(&rq->queuelist, &next->queuelist);
739
Jens Axboeb4878f22005-10-20 16:42:29 +0200740 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200741}
742
743static inline void
744__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
745{
746 if (cfqq) {
747 /*
748 * stop potential idle class queues waiting service
749 */
750 del_timer(&cfqd->idle_class_timer);
751
752 cfqq->slice_start = jiffies;
753 cfqq->slice_end = 0;
754 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200755 cfq_clear_cfqq_must_alloc_slice(cfqq);
756 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200757 }
758
759 cfqd->active_queue = cfqq;
760}
761
762/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100763 * current cfqq expired its slice (or was too idle), select new one
764 */
765static void
766__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
767 int preempted)
768{
769 unsigned long now = jiffies;
770
771 if (cfq_cfqq_wait_request(cfqq))
772 del_timer(&cfqd->idle_slice_timer);
773
774 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
775 cfqq->service_last = now;
776 cfq_schedule_dispatch(cfqd);
777 }
778
779 cfq_clear_cfqq_must_dispatch(cfqq);
780 cfq_clear_cfqq_wait_request(cfqq);
781
782 /*
783 * store what was left of this slice, if the queue idled out
784 * or was preempted
785 */
786 if (time_after(cfqq->slice_end, now))
787 cfqq->slice_left = cfqq->slice_end - now;
788 else
789 cfqq->slice_left = 0;
790
791 if (cfq_cfqq_on_rr(cfqq))
792 cfq_resort_rr_list(cfqq, preempted);
793
794 if (cfqq == cfqd->active_queue)
795 cfqd->active_queue = NULL;
796
797 if (cfqd->active_cic) {
798 put_io_context(cfqd->active_cic->ioc);
799 cfqd->active_cic = NULL;
800 }
801
802 cfqd->dispatch_slice = 0;
803}
804
805static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
806{
807 struct cfq_queue *cfqq = cfqd->active_queue;
808
809 if (cfqq)
810 __cfq_slice_expired(cfqd, cfqq, preempted);
811}
812
813/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200814 * 0
815 * 0,1
816 * 0,1,2
817 * 0,1,2,3
818 * 0,1,2,3,4
819 * 0,1,2,3,4,5
820 * 0,1,2,3,4,5,6
821 * 0,1,2,3,4,5,6,7
822 */
823static int cfq_get_next_prio_level(struct cfq_data *cfqd)
824{
825 int prio, wrap;
826
827 prio = -1;
828 wrap = 0;
829 do {
830 int p;
831
832 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
833 if (!list_empty(&cfqd->rr_list[p])) {
834 prio = p;
835 break;
836 }
837 }
838
839 if (prio != -1)
840 break;
841 cfqd->cur_prio = 0;
842 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
843 cfqd->cur_end_prio = 0;
844 if (wrap)
845 break;
846 wrap = 1;
847 }
848 } while (1);
849
850 if (unlikely(prio == -1))
851 return -1;
852
853 BUG_ON(prio >= CFQ_PRIO_LISTS);
854
855 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
856
857 cfqd->cur_prio = prio + 1;
858 if (cfqd->cur_prio > cfqd->cur_end_prio) {
859 cfqd->cur_end_prio = cfqd->cur_prio;
860 cfqd->cur_prio = 0;
861 }
862 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
863 cfqd->cur_prio = 0;
864 cfqd->cur_end_prio = 0;
865 }
866
867 return prio;
868}
869
Jens Axboe3b181522005-06-27 10:56:24 +0200870static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200871{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100872 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200873
874 /*
875 * if current list is non-empty, grab first entry. if it is empty,
876 * get next prio level and grab first entry then if any are spliced
877 */
878 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
879 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
880
881 /*
Jens Axboee0de0202006-06-01 10:07:26 +0200882 * If no new queues are available, check if the busy list has some
883 * before falling back to idle io.
884 */
885 if (!cfqq && !list_empty(&cfqd->busy_rr))
886 cfqq = list_entry_cfqq(cfqd->busy_rr.next);
887
888 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200889 * if we have idle queues and no rt or be queues had pending
890 * requests, either allow immediate service if the grace period
891 * has passed or arm the idle grace timer
892 */
893 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
894 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
895
896 if (time_after_eq(jiffies, end))
897 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
898 else
899 mod_timer(&cfqd->idle_class_timer, end);
900 }
901
902 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200903 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200904}
905
Jens Axboe22e2c502005-06-27 10:55:12 +0200906static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
907
908{
Jens Axboe206dc692006-03-28 13:03:44 +0200909 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100910 unsigned long sl;
911
Jens Axboe22e2c502005-06-27 10:55:12 +0200912 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
913 WARN_ON(cfqq != cfqd->active_queue);
914
915 /*
916 * idle is disabled, either manually or by past process history
917 */
918 if (!cfqd->cfq_slice_idle)
919 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200920 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200921 return 0;
922 /*
923 * task has exited, don't wait
924 */
Jens Axboe206dc692006-03-28 13:03:44 +0200925 cic = cfqd->active_cic;
926 if (!cic || !cic->ioc->task)
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 return 0;
928
Jens Axboe3b181522005-06-27 10:56:24 +0200929 cfq_mark_cfqq_must_dispatch(cfqq);
930 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200931
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100932 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
Jens Axboe206dc692006-03-28 13:03:44 +0200933
934 /*
935 * we don't want to idle for seeks, but we do want to allow
936 * fair distribution of slice time for a process doing back-to-back
937 * seeks. so allow a little bit of time for him to submit a new rq
938 */
939 if (sample_valid(cic->seek_samples) && cic->seek_mean > 131072)
940 sl = 2;
941
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100942 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Jens Axboeb4878f22005-10-20 16:42:29 +0200946static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 struct cfq_data *cfqd = q->elevator->elevator_data;
949 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200950
951 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200952 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200953 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200954 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * return expired entry, or NULL to just start from scratch in rbtree
959 */
960static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
961{
962 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200963 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct cfq_rq *crq;
965
Jens Axboe3b181522005-06-27 10:56:24 +0200966 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return NULL;
968
Jens Axboe22e2c502005-06-27 10:55:12 +0200969 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200970 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Jens Axboe22e2c502005-06-27 10:55:12 +0200972 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
973 rq = crq->request;
974 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200975 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200976 return crq;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 return NULL;
981}
982
983/*
Jens Axboe3b181522005-06-27 10:56:24 +0200984 * Scale schedule slice based on io priority. Use the sync time slice only
985 * if a queue is marked sync and has sync io queued. A sync queue with async
986 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200988static inline int
989cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Jens Axboe22e2c502005-06-27 10:55:12 +0200991 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Jens Axboe22e2c502005-06-27 10:55:12 +0200993 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Jens Axboe22e2c502005-06-27 10:55:12 +0200995 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Jens Axboe22e2c502005-06-27 10:55:12 +0200998static inline void
999cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1000{
1001 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
1002}
1003
1004static inline int
1005cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1006{
1007 const int base_rq = cfqd->cfq_slice_async_rq;
1008
1009 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1010
1011 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1012}
1013
1014/*
1015 * get next queue for service
1016 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001017static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001018{
1019 unsigned long now = jiffies;
1020 struct cfq_queue *cfqq;
1021
1022 cfqq = cfqd->active_queue;
1023 if (!cfqq)
1024 goto new_queue;
1025
1026 /*
1027 * slice has expired
1028 */
Jens Axboe3b181522005-06-27 10:56:24 +02001029 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1030 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001031
1032 /*
1033 * if queue has requests, dispatch one. if not, check if
1034 * enough slice is left to wait for one
1035 */
1036 if (!RB_EMPTY(&cfqq->sort_list))
1037 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001038 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001039 time_before(now, cfqq->slice_end)) {
1040 if (cfq_arm_slice_timer(cfqd, cfqq))
1041 return NULL;
1042 }
1043
Jens Axboe3b181522005-06-27 10:56:24 +02001044expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001045 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001046new_queue:
1047 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001048keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001049 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001050}
1051
1052static int
1053__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1054 int max_dispatch)
1055{
1056 int dispatched = 0;
1057
1058 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1059
1060 do {
1061 struct cfq_rq *crq;
1062
1063 /*
1064 * follow expired path, else get first next available
1065 */
1066 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1067 crq = cfqq->next_crq;
1068
1069 /*
1070 * finally, insert request into driver dispatch list
1071 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001072 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001073
1074 cfqd->dispatch_slice++;
1075 dispatched++;
1076
1077 if (!cfqd->active_cic) {
1078 atomic_inc(&crq->io_context->ioc->refcount);
1079 cfqd->active_cic = crq->io_context;
1080 }
1081
1082 if (RB_EMPTY(&cfqq->sort_list))
1083 break;
1084
1085 } while (dispatched < max_dispatch);
1086
1087 /*
1088 * if slice end isn't set yet, set it. if at least one request was
1089 * sync, use the sync time slice value
1090 */
1091 if (!cfqq->slice_end)
1092 cfq_set_prio_slice(cfqd, cfqq);
1093
1094 /*
1095 * expire an async queue immediately if it has used up its slice. idle
1096 * queue always expire after 1 dispatch round.
1097 */
1098 if ((!cfq_cfqq_sync(cfqq) &&
1099 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1100 cfq_class_idle(cfqq))
1101 cfq_slice_expired(cfqd, 0);
1102
1103 return dispatched;
1104}
1105
1106static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001107cfq_forced_dispatch_cfqqs(struct list_head *list)
1108{
1109 int dispatched = 0;
1110 struct cfq_queue *cfqq, *next;
1111 struct cfq_rq *crq;
1112
1113 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1114 while ((crq = cfqq->next_crq)) {
1115 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1116 dispatched++;
1117 }
1118 BUG_ON(!list_empty(&cfqq->fifo));
1119 }
1120 return dispatched;
1121}
1122
1123static int
1124cfq_forced_dispatch(struct cfq_data *cfqd)
1125{
1126 int i, dispatched = 0;
1127
1128 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1129 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1130
1131 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1132 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1133 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1134
1135 cfq_slice_expired(cfqd, 0);
1136
1137 BUG_ON(cfqd->busy_queues);
1138
1139 return dispatched;
1140}
1141
1142static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001143cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 struct cfq_data *cfqd = q->elevator->elevator_data;
1146 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe22e2c502005-06-27 10:55:12 +02001148 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 0;
1150
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001151 if (unlikely(force))
1152 return cfq_forced_dispatch(cfqd);
1153
1154 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001155 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001156 int max_dispatch;
1157
Jens Axboe3b181522005-06-27 10:56:24 +02001158 cfq_clear_cfqq_must_dispatch(cfqq);
1159 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001160 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001162 max_dispatch = cfqd->cfq_quantum;
1163 if (cfq_class_idle(cfqq))
1164 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jens Axboe22e2c502005-06-27 10:55:12 +02001166 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
Jens Axboe22e2c502005-06-27 10:55:12 +02001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/*
1173 * task holds one reference to the queue, dropped when task exits. each crq
1174 * in-flight on this queue also holds a reference, dropped when crq is freed.
1175 *
1176 * queue lock must be held here.
1177 */
1178static void cfq_put_queue(struct cfq_queue *cfqq)
1179{
Jens Axboe22e2c502005-06-27 10:55:12 +02001180 struct cfq_data *cfqd = cfqq->cfqd;
1181
1182 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (!atomic_dec_and_test(&cfqq->ref))
1185 return;
1186
1187 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001188 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001189 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001191 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001192 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * it's on the empty list and still hashed
1196 */
1197 list_del(&cfqq->cfq_list);
1198 hlist_del(&cfqq->cfq_hash);
1199 kmem_cache_free(cfq_pool, cfqq);
1200}
1201
1202static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001203__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1204 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001207 struct hlist_node *entry;
1208 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Jens Axboe206dc692006-03-28 13:03:44 +02001210 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001211 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Jens Axboe206dc692006-03-28 13:03:44 +02001213 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return __cfqq;
1215 }
1216
1217 return NULL;
1218}
1219
1220static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001221cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jens Axboe3b181522005-06-27 10:56:24 +02001223 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Jens Axboee2d74ac2006-03-28 08:59:01 +02001226static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Jens Axboe22e2c502005-06-27 10:55:12 +02001228 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001229 struct rb_node *n;
1230 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001231
Jens Axboee2d74ac2006-03-28 08:59:01 +02001232 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1233 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1234 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001235 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001236 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001237 }
1238
Al Viro334e94d2006-03-18 15:05:53 -05001239 if (atomic_sub_and_test(freed, &ioc_count) && ioc_gone)
1240 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Al Viroe17a9482006-03-18 13:21:20 -05001243static void cfq_trim(struct io_context *ioc)
1244{
1245 ioc->set_ioprio = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001246 cfq_free_io_context(ioc);
Al Viroe17a9482006-03-18 13:21:20 -05001247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001250 * Called with interrupts disabled
1251 */
1252static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1253{
Al Viro478a82b2006-03-18 13:25:24 -05001254 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001255 request_queue_t *q;
1256
1257 if (!cfqd)
1258 return;
1259
1260 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001261
1262 WARN_ON(!irqs_disabled());
1263
1264 spin_lock(q->queue_lock);
1265
Al Viro12a05732006-03-18 13:38:01 -05001266 if (cic->cfqq[ASYNC]) {
1267 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1268 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1269 cfq_put_queue(cic->cfqq[ASYNC]);
1270 cic->cfqq[ASYNC] = NULL;
1271 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001272
Al Viro12a05732006-03-18 13:38:01 -05001273 if (cic->cfqq[SYNC]) {
1274 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1275 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1276 cfq_put_queue(cic->cfqq[SYNC]);
1277 cic->cfqq[SYNC] = NULL;
1278 }
1279
Al Viro478a82b2006-03-18 13:25:24 -05001280 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001281 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001282 spin_unlock(q->queue_lock);
1283}
1284
Jens Axboee2d74ac2006-03-28 08:59:01 +02001285static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Jens Axboe22e2c502005-06-27 10:55:12 +02001287 struct cfq_io_context *__cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001289 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * put the reference this task is holding to the various queues
1293 */
Jens Axboe3793c652006-05-30 21:11:04 +02001294 spin_lock_irqsave(&cfq_exit_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001295
1296 n = rb_first(&ioc->cic_root);
1297 while (n != NULL) {
1298 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1299
Jens Axboe22e2c502005-06-27 10:55:12 +02001300 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001301 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Jens Axboe3793c652006-05-30 21:11:04 +02001304 spin_unlock_irqrestore(&cfq_exit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Jens Axboe22e2c502005-06-27 10:55:12 +02001307static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001308cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Jens Axboe22e2c502005-06-27 10:55:12 +02001310 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (cic) {
Jens Axboee2d74ac2006-03-28 08:59:01 +02001313 RB_CLEAR(&cic->rb_node);
1314 cic->key = NULL;
Al Viro12a05732006-03-18 13:38:01 -05001315 cic->cfqq[ASYNC] = NULL;
1316 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001317 cic->last_end_request = jiffies;
1318 cic->ttime_total = 0;
1319 cic->ttime_samples = 0;
1320 cic->ttime_mean = 0;
1321 cic->dtor = cfq_free_io_context;
1322 cic->exit = cfq_exit_io_context;
Al Virod9ff4182006-03-18 13:51:22 -05001323 INIT_LIST_HEAD(&cic->queue_list);
Al Viro334e94d2006-03-18 15:05:53 -05001324 atomic_inc(&ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 return cic;
1328}
1329
Jens Axboe22e2c502005-06-27 10:55:12 +02001330static void cfq_init_prio_data(struct cfq_queue *cfqq)
1331{
1332 struct task_struct *tsk = current;
1333 int ioprio_class;
1334
Jens Axboe3b181522005-06-27 10:56:24 +02001335 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001336 return;
1337
1338 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1339 switch (ioprio_class) {
1340 default:
1341 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1342 case IOPRIO_CLASS_NONE:
1343 /*
1344 * no prio set, place us in the middle of the BE classes
1345 */
1346 cfqq->ioprio = task_nice_ioprio(tsk);
1347 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1348 break;
1349 case IOPRIO_CLASS_RT:
1350 cfqq->ioprio = task_ioprio(tsk);
1351 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1352 break;
1353 case IOPRIO_CLASS_BE:
1354 cfqq->ioprio = task_ioprio(tsk);
1355 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1356 break;
1357 case IOPRIO_CLASS_IDLE:
1358 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1359 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001360 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001361 break;
1362 }
1363
1364 /*
1365 * keep track of original prio settings in case we have to temporarily
1366 * elevate the priority of this queue
1367 */
1368 cfqq->org_ioprio = cfqq->ioprio;
1369 cfqq->org_ioprio_class = cfqq->ioprio_class;
1370
Jens Axboe3b181522005-06-27 10:56:24 +02001371 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001372 cfq_resort_rr_list(cfqq, 0);
1373
Jens Axboe3b181522005-06-27 10:56:24 +02001374 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001375}
1376
Al Viro478a82b2006-03-18 13:25:24 -05001377static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001378{
Al Viro478a82b2006-03-18 13:25:24 -05001379 struct cfq_data *cfqd = cic->key;
1380 struct cfq_queue *cfqq;
1381 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001382 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001383 cfqq = cic->cfqq[ASYNC];
1384 if (cfqq) {
Al Viro6f325a12006-03-18 14:58:37 -05001385 struct cfq_queue *new_cfqq;
1386 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC,
1387 cic->ioc->task, GFP_ATOMIC);
1388 if (new_cfqq) {
1389 cic->cfqq[ASYNC] = new_cfqq;
1390 cfq_put_queue(cfqq);
1391 }
Al Viro12a05732006-03-18 13:38:01 -05001392 }
1393 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001394 if (cfqq) {
1395 cfq_mark_cfqq_prio_changed(cfqq);
1396 cfq_init_prio_data(cfqq);
1397 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001398 spin_unlock(cfqd->queue->queue_lock);
1399 }
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001403 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001405static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Al Viroa6a07632006-03-18 13:26:44 -05001407 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001408 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001409
Jens Axboe3793c652006-05-30 21:11:04 +02001410 spin_lock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001411
Jens Axboee2d74ac2006-03-28 08:59:01 +02001412 n = rb_first(&ioc->cic_root);
1413 while (n != NULL) {
1414 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001415
Al Viro478a82b2006-03-18 13:25:24 -05001416 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001417 n = rb_next(n);
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jens Axboe3793c652006-05-30 21:11:04 +02001420 spin_unlock(&cfq_exit_lock);
Al Viroa6a07632006-03-18 13:26:44 -05001421
Jens Axboe22e2c502005-06-27 10:55:12 +02001422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001426cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001427 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1430 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001431 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433retry:
Al Viro6f325a12006-03-18 14:58:37 -05001434 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001435 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (!cfqq) {
1438 if (new_cfqq) {
1439 cfqq = new_cfqq;
1440 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001441 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 spin_unlock_irq(cfqd->queue->queue_lock);
1443 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1444 spin_lock_irq(cfqd->queue->queue_lock);
1445 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001446 } else {
1447 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1448 if (!cfqq)
1449 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 memset(cfqq, 0, sizeof(*cfqq));
1453
1454 INIT_HLIST_NODE(&cfqq->cfq_hash);
1455 INIT_LIST_HEAD(&cfqq->cfq_list);
1456 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001457 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 cfqq->key = key;
1460 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1461 atomic_set(&cfqq->ref, 0);
1462 cfqq->cfqd = cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +02001463 cfqq->service_last = 0;
1464 /*
1465 * set ->slice_left to allow preemption for a new process
1466 */
1467 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001468 cfq_mark_cfqq_idle_window(cfqq);
1469 cfq_mark_cfqq_prio_changed(cfqq);
1470 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
1473 if (new_cfqq)
1474 kmem_cache_free(cfq_pool, new_cfqq);
1475
1476 atomic_inc(&cfqq->ref);
1477out:
1478 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1479 return cfqq;
1480}
1481
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001482static void
1483cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1484{
Jens Axboe3793c652006-05-30 21:11:04 +02001485 spin_lock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001486 rb_erase(&cic->rb_node, &ioc->cic_root);
Jens Axboe3793c652006-05-30 21:11:04 +02001487 list_del_init(&cic->queue_list);
1488 spin_unlock(&cfq_exit_lock);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001489 kmem_cache_free(cfq_ioc_pool, cic);
1490 atomic_dec(&ioc_count);
1491}
1492
Jens Axboee2d74ac2006-03-28 08:59:01 +02001493static struct cfq_io_context *
1494cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1495{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001496 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001497 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001498 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001499
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001500restart:
1501 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001502 while (n) {
1503 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001504 /* ->key must be copied to avoid race with cfq_exit_queue() */
1505 k = cic->key;
1506 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001507 cfq_drop_dead_cic(ioc, cic);
1508 goto restart;
1509 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001510
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001511 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001512 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001513 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001514 n = n->rb_right;
1515 else
1516 return cic;
1517 }
1518
1519 return NULL;
1520}
1521
1522static inline void
1523cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1524 struct cfq_io_context *cic)
1525{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001526 struct rb_node **p;
1527 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001528 struct cfq_io_context *__cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001529 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001530
Jens Axboee2d74ac2006-03-28 08:59:01 +02001531 cic->ioc = ioc;
1532 cic->key = cfqd;
1533
1534 ioc->set_ioprio = cfq_ioc_set_ioprio;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001535restart:
1536 parent = NULL;
1537 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001538 while (*p) {
1539 parent = *p;
1540 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001541 /* ->key must be copied to avoid race with cfq_exit_queue() */
1542 k = __cic->key;
1543 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001544 cfq_drop_dead_cic(ioc, cic);
1545 goto restart;
1546 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001547
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001548 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001549 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001550 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001551 p = &(*p)->rb_right;
1552 else
1553 BUG();
1554 }
1555
Jens Axboe3793c652006-05-30 21:11:04 +02001556 spin_lock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001557 rb_link_node(&cic->rb_node, parent, p);
1558 rb_insert_color(&cic->rb_node, &ioc->cic_root);
1559 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe3793c652006-05-30 21:11:04 +02001560 spin_unlock(&cfq_exit_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001561}
1562
Jens Axboe22e2c502005-06-27 10:55:12 +02001563/*
1564 * Setup general io context and cfq io context. There can be several cfq
1565 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001566 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001567 */
1568static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001569cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Jens Axboe22e2c502005-06-27 10:55:12 +02001571 struct io_context *ioc = NULL;
1572 struct cfq_io_context *cic;
1573
1574 might_sleep_if(gfp_mask & __GFP_WAIT);
1575
1576 ioc = get_io_context(gfp_mask);
1577 if (!ioc)
1578 return NULL;
1579
Jens Axboee2d74ac2006-03-28 08:59:01 +02001580 cic = cfq_cic_rb_lookup(cfqd, ioc);
1581 if (cic)
1582 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001583
Jens Axboee2d74ac2006-03-28 08:59:01 +02001584 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1585 if (cic == NULL)
1586 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001587
Jens Axboee2d74ac2006-03-28 08:59:01 +02001588 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001589out:
1590 return cic;
1591err:
1592 put_io_context(ioc);
1593 return NULL;
1594}
1595
1596static void
1597cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1598{
1599 unsigned long elapsed, ttime;
1600
1601 /*
1602 * if this context already has stuff queued, thinktime is from
1603 * last queue not last end
1604 */
1605#if 0
1606 if (time_after(cic->last_end_request, cic->last_queue))
1607 elapsed = jiffies - cic->last_end_request;
1608 else
1609 elapsed = jiffies - cic->last_queue;
1610#else
1611 elapsed = jiffies - cic->last_end_request;
1612#endif
1613
1614 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1615
1616 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1617 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1618 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1619}
1620
Jens Axboe206dc692006-03-28 13:03:44 +02001621static void
1622cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1623 struct cfq_rq *crq)
1624{
1625 sector_t sdist;
1626 u64 total;
1627
1628 if (cic->last_request_pos < crq->request->sector)
1629 sdist = crq->request->sector - cic->last_request_pos;
1630 else
1631 sdist = cic->last_request_pos - crq->request->sector;
1632
1633 /*
1634 * Don't allow the seek distance to get too large from the
1635 * odd fragment, pagein, etc
1636 */
1637 if (cic->seek_samples <= 60) /* second&third seek */
1638 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1639 else
1640 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1641
1642 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1643 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1644 total = cic->seek_total + (cic->seek_samples/2);
1645 do_div(total, cic->seek_samples);
1646 cic->seek_mean = (sector_t)total;
1647}
Jens Axboe22e2c502005-06-27 10:55:12 +02001648
1649/*
1650 * Disable idle window if the process thinks too long or seeks so much that
1651 * it doesn't matter
1652 */
1653static void
1654cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1655 struct cfq_io_context *cic)
1656{
Jens Axboe3b181522005-06-27 10:56:24 +02001657 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001658
1659 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1660 enable_idle = 0;
1661 else if (sample_valid(cic->ttime_samples)) {
1662 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1663 enable_idle = 0;
1664 else
1665 enable_idle = 1;
1666 }
1667
Jens Axboe3b181522005-06-27 10:56:24 +02001668 if (enable_idle)
1669 cfq_mark_cfqq_idle_window(cfqq);
1670 else
1671 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001672}
1673
1674
1675/*
1676 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1677 * no or if we aren't sure, a 1 will cause a preempt.
1678 */
1679static int
1680cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1681 struct cfq_rq *crq)
1682{
1683 struct cfq_queue *cfqq = cfqd->active_queue;
1684
1685 if (cfq_class_idle(new_cfqq))
1686 return 0;
1687
1688 if (!cfqq)
1689 return 1;
1690
1691 if (cfq_class_idle(cfqq))
1692 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001693 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001694 return 0;
1695 /*
1696 * if it doesn't have slice left, forget it
1697 */
1698 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1699 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001700 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001701 return 1;
1702
1703 return 0;
1704}
1705
1706/*
1707 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1708 * let it have half of its nominal slice.
1709 */
1710static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1711{
1712 struct cfq_queue *__cfqq, *next;
1713
1714 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1715 cfq_resort_rr_list(__cfqq, 1);
1716
1717 if (!cfqq->slice_left)
1718 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1719
1720 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001721 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001722 __cfq_set_active_queue(cfqd, cfqq);
1723}
1724
1725/*
1726 * should really be a ll_rw_blk.c helper
1727 */
1728static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1729{
1730 request_queue_t *q = cfqd->queue;
1731
1732 if (!blk_queue_plugged(q))
1733 q->request_fn(q);
1734 else
1735 __generic_unplug_device(q);
1736}
1737
1738/*
1739 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1740 * something we should do about it
1741 */
1742static void
1743cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1744 struct cfq_rq *crq)
1745{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001746 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001747
1748 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1749
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001750 cic = crq->io_context;
1751
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001752 /*
1753 * we never wait for an async request and we don't allow preemption
1754 * of an async request. so just return early
1755 */
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001756 if (!cfq_crq_is_sync(crq)) {
1757 /*
1758 * sync process issued an async request, if it's waiting
1759 * then expire it and kick rq handling.
1760 */
1761 if (cic == cfqd->active_cic &&
1762 del_timer(&cfqd->idle_slice_timer)) {
1763 cfq_slice_expired(cfqd, 0);
1764 cfq_start_queueing(cfqd, cfqq);
1765 }
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001766 return;
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001767 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001768
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001769 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe206dc692006-03-28 13:03:44 +02001770 cfq_update_io_seektime(cfqd, cic, crq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001771 cfq_update_idle_window(cfqd, cfqq, cic);
1772
1773 cic->last_queue = jiffies;
Jens Axboe206dc692006-03-28 13:03:44 +02001774 cic->last_request_pos = crq->request->sector + crq->request->nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001775
1776 if (cfqq == cfqd->active_queue) {
1777 /*
1778 * if we are waiting for a request for this queue, let it rip
1779 * immediately and flag that we must not expire this queue
1780 * just now
1781 */
Jens Axboe3b181522005-06-27 10:56:24 +02001782 if (cfq_cfqq_wait_request(cfqq)) {
1783 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001784 del_timer(&cfqd->idle_slice_timer);
1785 cfq_start_queueing(cfqd, cfqq);
1786 }
1787 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1788 /*
1789 * not the active queue - expire current slice if it is
1790 * idle and has expired it's mean thinktime or this new queue
1791 * has some old slice time left and is of higher priority
1792 */
1793 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001794 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001795 cfq_start_queueing(cfqd, cfqq);
1796 }
1797}
1798
Jens Axboeb4878f22005-10-20 16:42:29 +02001799static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001800{
Jens Axboeb4878f22005-10-20 16:42:29 +02001801 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001802 struct cfq_rq *crq = RQ_DATA(rq);
1803 struct cfq_queue *cfqq = crq->cfq_queue;
1804
1805 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Jens Axboe22e2c502005-06-27 10:55:12 +02001809 list_add_tail(&rq->queuelist, &cfqq->fifo);
1810
Tejun Heo98b11472005-10-20 16:46:54 +02001811 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001812 cfq_add_crq_hash(cfqd, crq);
1813
Jens Axboe22e2c502005-06-27 10:55:12 +02001814 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static void cfq_completed_request(request_queue_t *q, struct request *rq)
1818{
1819 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001820 struct cfq_queue *cfqq = crq->cfq_queue;
1821 struct cfq_data *cfqd = cfqq->cfqd;
1822 const int sync = cfq_crq_is_sync(crq);
1823 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jens Axboeb4878f22005-10-20 16:42:29 +02001825 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jens Axboeb4878f22005-10-20 16:42:29 +02001827 WARN_ON(!cfqd->rq_in_driver);
1828 WARN_ON(!cfqq->on_dispatch[sync]);
1829 cfqd->rq_in_driver--;
1830 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jens Axboeb4878f22005-10-20 16:42:29 +02001832 if (!cfq_class_idle(cfqq))
1833 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001834
Jens Axboeb4878f22005-10-20 16:42:29 +02001835 if (!cfq_cfqq_dispatched(cfqq)) {
1836 if (cfq_cfqq_on_rr(cfqq)) {
1837 cfqq->service_last = now;
1838 cfq_resort_rr_list(cfqq, 0);
1839 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001840 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
Jens Axboeb4878f22005-10-20 16:42:29 +02001843 if (cfq_crq_is_sync(crq))
1844 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847static struct request *
1848cfq_former_request(request_queue_t *q, struct request *rq)
1849{
1850 struct cfq_rq *crq = RQ_DATA(rq);
1851 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1852
1853 if (rbprev)
1854 return rb_entry_crq(rbprev)->request;
1855
1856 return NULL;
1857}
1858
1859static struct request *
1860cfq_latter_request(request_queue_t *q, struct request *rq)
1861{
1862 struct cfq_rq *crq = RQ_DATA(rq);
1863 struct rb_node *rbnext = rb_next(&crq->rb_node);
1864
1865 if (rbnext)
1866 return rb_entry_crq(rbnext)->request;
1867
1868 return NULL;
1869}
1870
Jens Axboe22e2c502005-06-27 10:55:12 +02001871/*
1872 * we temporarily boost lower priority queues if they are holding fs exclusive
1873 * resources. they are boosted to normal prio (CLASS_BE/4)
1874 */
1875static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Jens Axboe22e2c502005-06-27 10:55:12 +02001877 const int ioprio_class = cfqq->ioprio_class;
1878 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Jens Axboe22e2c502005-06-27 10:55:12 +02001880 if (has_fs_excl()) {
1881 /*
1882 * boost idle prio on transactions that would lock out other
1883 * users of the filesystem
1884 */
1885 if (cfq_class_idle(cfqq))
1886 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1887 if (cfqq->ioprio > IOPRIO_NORM)
1888 cfqq->ioprio = IOPRIO_NORM;
1889 } else {
1890 /*
1891 * check if we need to unboost the queue
1892 */
1893 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1894 cfqq->ioprio_class = cfqq->org_ioprio_class;
1895 if (cfqq->ioprio != cfqq->org_ioprio)
1896 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
Jens Axboe22e2c502005-06-27 10:55:12 +02001899 /*
1900 * refile between round-robin lists if we moved the priority class
1901 */
1902 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001903 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001904 cfq_resort_rr_list(cfqq, 0);
1905}
1906
Jens Axboe22e2c502005-06-27 10:55:12 +02001907static inline int
1908__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1909 struct task_struct *task, int rw)
1910{
Jens Axboe3b181522005-06-27 10:56:24 +02001911#if 1
1912 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001913 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001914 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001915 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001916 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001917
1918 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001919#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001920 if (!cfqq || task->flags & PF_MEMALLOC)
1921 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001922 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1923 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001924 return ELV_MQUEUE_MUST;
1925
1926 /*
1927 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1928 * can quickly flood the queue with writes from a single task
1929 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001930 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001931 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001932 return ELV_MQUEUE_MUST;
1933 }
1934
1935 return ELV_MQUEUE_MAY;
1936 }
1937 if (cfq_class_idle(cfqq))
1938 return ELV_MQUEUE_NO;
1939 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1940 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1941 int ret = ELV_MQUEUE_NO;
1942
1943 if (ioc && ioc->nr_batch_requests)
1944 ret = ELV_MQUEUE_MAY;
1945
1946 put_io_context(ioc);
1947 return ret;
1948 }
1949
1950 return ELV_MQUEUE_MAY;
1951#endif
1952}
1953
1954static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1955{
1956 struct cfq_data *cfqd = q->elevator->elevator_data;
1957 struct task_struct *tsk = current;
1958 struct cfq_queue *cfqq;
1959
1960 /*
1961 * don't force setup of a queue from here, as a call to may_queue
1962 * does not necessarily imply that a request actually will be queued.
1963 * so just lookup a possibly existing queue, or return 'may queue'
1964 * if that fails
1965 */
Jens Axboe3b181522005-06-27 10:56:24 +02001966 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001967 if (cfqq) {
1968 cfq_init_prio_data(cfqq);
1969 cfq_prio_boost(cfqq);
1970
1971 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1972 }
1973
1974 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1978{
Jens Axboe22e2c502005-06-27 10:55:12 +02001979 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Jens Axboe22e2c502005-06-27 10:55:12 +02001982 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1983 smp_mb();
1984 if (waitqueue_active(&rl->wait[READ]))
1985 wake_up(&rl->wait[READ]);
1986 }
1987
1988 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1989 smp_mb();
1990 if (waitqueue_active(&rl->wait[WRITE]))
1991 wake_up(&rl->wait[WRITE]);
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995/*
1996 * queue lock held here
1997 */
1998static void cfq_put_request(request_queue_t *q, struct request *rq)
1999{
2000 struct cfq_data *cfqd = q->elevator->elevator_data;
2001 struct cfq_rq *crq = RQ_DATA(rq);
2002
2003 if (crq) {
2004 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002005 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Jens Axboe22e2c502005-06-27 10:55:12 +02002007 BUG_ON(!cfqq->allocated[rw]);
2008 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Jens Axboe22e2c502005-06-27 10:55:12 +02002010 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 mempool_free(crq, cfqd->crq_pool);
2013 rq->elevator_private = NULL;
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 cfq_check_waiters(q, cfqq);
2016 cfq_put_queue(cfqq);
2017 }
2018}
2019
2020/*
Jens Axboe22e2c502005-06-27 10:55:12 +02002021 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002023static int
2024cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04002025 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02002028 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct cfq_io_context *cic;
2030 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02002031 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02002032 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct cfq_rq *crq;
2034 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05002035 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 might_sleep_if(gfp_mask & __GFP_WAIT);
2038
Jens Axboee2d74ac2006-03-28 08:59:01 +02002039 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 spin_lock_irqsave(q->queue_lock, flags);
2042
Jens Axboe22e2c502005-06-27 10:55:12 +02002043 if (!cic)
2044 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Al Viro12a05732006-03-18 13:38:01 -05002046 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05002047 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002048 if (!cfqq)
2049 goto queue_fail;
2050
Al Viro12a05732006-03-18 13:38:01 -05002051 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002052 } else
Al Viro12a05732006-03-18 13:38:01 -05002053 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02002056 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002057 cfqd->rq_starved = 0;
2058 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 spin_unlock_irqrestore(q->queue_lock, flags);
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
2062 if (crq) {
2063 RB_CLEAR(&crq->rb_node);
2064 crq->rb_key = 0;
2065 crq->request = rq;
2066 INIT_HLIST_NODE(&crq->hash);
2067 crq->cfq_queue = cfqq;
2068 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02002069
Al Viro12a05732006-03-18 13:38:01 -05002070 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02002071 cfq_mark_crq_is_sync(crq);
2072 else
2073 cfq_clear_crq_is_sync(crq);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 spin_lock_irqsave(q->queue_lock, flags);
2080 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02002081 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02002082 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002084queue_fail:
2085 if (cic)
2086 put_io_context(cic->ioc);
2087 /*
2088 * mark us rq allocation starved. we need to kickstart the process
2089 * ourselves if there are no pending requests that can do it for us.
2090 * that would be an extremely rare OOM situation
2091 */
2092 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002093 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 spin_unlock_irqrestore(q->queue_lock, flags);
2095 return 1;
2096}
2097
Jens Axboe22e2c502005-06-27 10:55:12 +02002098static void cfq_kick_queue(void *data)
2099{
2100 request_queue_t *q = data;
2101 struct cfq_data *cfqd = q->elevator->elevator_data;
2102 unsigned long flags;
2103
2104 spin_lock_irqsave(q->queue_lock, flags);
2105
2106 if (cfqd->rq_starved) {
2107 struct request_list *rl = &q->rq;
2108
2109 /*
2110 * we aren't guaranteed to get a request after this, but we
2111 * have to be opportunistic
2112 */
2113 smp_mb();
2114 if (waitqueue_active(&rl->wait[READ]))
2115 wake_up(&rl->wait[READ]);
2116 if (waitqueue_active(&rl->wait[WRITE]))
2117 wake_up(&rl->wait[WRITE]);
2118 }
2119
2120 blk_remove_plug(q);
2121 q->request_fn(q);
2122 spin_unlock_irqrestore(q->queue_lock, flags);
2123}
2124
2125/*
2126 * Timer running if the active_queue is currently idling inside its time slice
2127 */
2128static void cfq_idle_slice_timer(unsigned long data)
2129{
2130 struct cfq_data *cfqd = (struct cfq_data *) data;
2131 struct cfq_queue *cfqq;
2132 unsigned long flags;
2133
2134 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2135
2136 if ((cfqq = cfqd->active_queue) != NULL) {
2137 unsigned long now = jiffies;
2138
2139 /*
2140 * expired
2141 */
2142 if (time_after(now, cfqq->slice_end))
2143 goto expire;
2144
2145 /*
2146 * only expire and reinvoke request handler, if there are
2147 * other queues with pending requests
2148 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002149 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002150 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2151 add_timer(&cfqd->idle_slice_timer);
2152 goto out_cont;
2153 }
2154
2155 /*
2156 * not expired and it has a request pending, let it dispatch
2157 */
2158 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002159 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002160 goto out_kick;
2161 }
2162 }
2163expire:
2164 cfq_slice_expired(cfqd, 0);
2165out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002166 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002167out_cont:
2168 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2169}
2170
2171/*
2172 * Timer running if an idle class queue is waiting for service
2173 */
2174static void cfq_idle_class_timer(unsigned long data)
2175{
2176 struct cfq_data *cfqd = (struct cfq_data *) data;
2177 unsigned long flags, end;
2178
2179 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2180
2181 /*
2182 * race with a non-idle queue, reset timer
2183 */
2184 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2185 if (!time_after_eq(jiffies, end)) {
2186 cfqd->idle_class_timer.expires = end;
2187 add_timer(&cfqd->idle_class_timer);
2188 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002189 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002190
2191 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2192}
2193
Jens Axboe3b181522005-06-27 10:56:24 +02002194static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2195{
2196 del_timer_sync(&cfqd->idle_slice_timer);
2197 del_timer_sync(&cfqd->idle_class_timer);
2198 blk_sync_queue(cfqd->queue);
2199}
Jens Axboe22e2c502005-06-27 10:55:12 +02002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static void cfq_exit_queue(elevator_t *e)
2202{
Jens Axboe22e2c502005-06-27 10:55:12 +02002203 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002204 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002205
Jens Axboe3b181522005-06-27 10:56:24 +02002206 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002207
Jens Axboe3793c652006-05-30 21:11:04 +02002208 spin_lock(&cfq_exit_lock);
Al Virod9ff4182006-03-18 13:51:22 -05002209 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002210
Al Virod9ff4182006-03-18 13:51:22 -05002211 if (cfqd->active_queue)
2212 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002213
2214 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002215 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2216 struct cfq_io_context,
2217 queue_list);
2218 if (cic->cfqq[ASYNC]) {
2219 cfq_put_queue(cic->cfqq[ASYNC]);
2220 cic->cfqq[ASYNC] = NULL;
2221 }
2222 if (cic->cfqq[SYNC]) {
2223 cfq_put_queue(cic->cfqq[SYNC]);
2224 cic->cfqq[SYNC] = NULL;
2225 }
2226 cic->key = NULL;
2227 list_del_init(&cic->queue_list);
2228 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002229
Al Virod9ff4182006-03-18 13:51:22 -05002230 spin_unlock_irq(q->queue_lock);
Jens Axboe3793c652006-05-30 21:11:04 +02002231 spin_unlock(&cfq_exit_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002232
2233 cfq_shutdown_timer_wq(cfqd);
2234
2235 mempool_destroy(cfqd->crq_pool);
2236 kfree(cfqd->crq_hash);
2237 kfree(cfqd->cfq_hash);
2238 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
2241static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2242{
2243 struct cfq_data *cfqd;
2244 int i;
2245
2246 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2247 if (!cfqd)
2248 return -ENOMEM;
2249
2250 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002251
2252 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2253 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2254
2255 INIT_LIST_HEAD(&cfqd->busy_rr);
2256 INIT_LIST_HEAD(&cfqd->cur_rr);
2257 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002259 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2262 if (!cfqd->crq_hash)
2263 goto out_crqhash;
2264
2265 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2266 if (!cfqd->cfq_hash)
2267 goto out_cfqhash;
2268
Matthew Dobson93d23412006-03-26 01:37:50 -08002269 cfqd->crq_pool = mempool_create_slab_pool(BLKDEV_MIN_RQ, crq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (!cfqd->crq_pool)
2271 goto out_crqpool;
2272
2273 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2274 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2275 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2276 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2277
2278 e->elevator_data = cfqd;
2279
2280 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jens Axboe22e2c502005-06-27 10:55:12 +02002282 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002284
2285 init_timer(&cfqd->idle_slice_timer);
2286 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2287 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2288
2289 init_timer(&cfqd->idle_class_timer);
2290 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2291 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2292
2293 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 cfqd->cfq_queued = cfq_queued;
2296 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002297 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2298 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 cfqd->cfq_back_max = cfq_back_max;
2300 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002301 cfqd->cfq_slice[0] = cfq_slice_async;
2302 cfqd->cfq_slice[1] = cfq_slice_sync;
2303 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2304 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307out_crqpool:
2308 kfree(cfqd->cfq_hash);
2309out_cfqhash:
2310 kfree(cfqd->crq_hash);
2311out_crqhash:
2312 kfree(cfqd);
2313 return -ENOMEM;
2314}
2315
2316static void cfq_slab_kill(void)
2317{
2318 if (crq_pool)
2319 kmem_cache_destroy(crq_pool);
2320 if (cfq_pool)
2321 kmem_cache_destroy(cfq_pool);
2322 if (cfq_ioc_pool)
2323 kmem_cache_destroy(cfq_ioc_pool);
2324}
2325
2326static int __init cfq_slab_setup(void)
2327{
2328 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2329 NULL, NULL);
2330 if (!crq_pool)
2331 goto fail;
2332
2333 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2334 NULL, NULL);
2335 if (!cfq_pool)
2336 goto fail;
2337
2338 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2339 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2340 if (!cfq_ioc_pool)
2341 goto fail;
2342
2343 return 0;
2344fail:
2345 cfq_slab_kill();
2346 return -ENOMEM;
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/*
2350 * sysfs parts below -->
2351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353static ssize_t
2354cfq_var_show(unsigned int var, char *page)
2355{
2356 return sprintf(page, "%d\n", var);
2357}
2358
2359static ssize_t
2360cfq_var_store(unsigned int *var, const char *page, size_t count)
2361{
2362 char *p = (char *) page;
2363
2364 *var = simple_strtoul(p, &p, 10);
2365 return count;
2366}
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002369static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002371 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 unsigned int __data = __VAR; \
2373 if (__CONV) \
2374 __data = jiffies_to_msecs(__data); \
2375 return cfq_var_show(__data, (page)); \
2376}
2377SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2378SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002379SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2380SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002381SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2382SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002383SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2384SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2385SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2386SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387#undef SHOW_FUNCTION
2388
2389#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002390static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002392 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 unsigned int __data; \
2394 int ret = cfq_var_store(&__data, (page), count); \
2395 if (__data < (MIN)) \
2396 __data = (MIN); \
2397 else if (__data > (MAX)) \
2398 __data = (MAX); \
2399 if (__CONV) \
2400 *(__PTR) = msecs_to_jiffies(__data); \
2401 else \
2402 *(__PTR) = __data; \
2403 return ret; \
2404}
2405STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2406STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002407STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2408STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002409STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2410STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002411STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2412STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2413STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2414STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415#undef STORE_FUNCTION
2416
Al Viroe572ec72006-03-18 22:27:18 -05002417#define CFQ_ATTR(name) \
2418 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002419
Al Viroe572ec72006-03-18 22:27:18 -05002420static struct elv_fs_entry cfq_attrs[] = {
2421 CFQ_ATTR(quantum),
2422 CFQ_ATTR(queued),
2423 CFQ_ATTR(fifo_expire_sync),
2424 CFQ_ATTR(fifo_expire_async),
2425 CFQ_ATTR(back_seek_max),
2426 CFQ_ATTR(back_seek_penalty),
2427 CFQ_ATTR(slice_sync),
2428 CFQ_ATTR(slice_async),
2429 CFQ_ATTR(slice_async_rq),
2430 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002431 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432};
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434static struct elevator_type iosched_cfq = {
2435 .ops = {
2436 .elevator_merge_fn = cfq_merge,
2437 .elevator_merged_fn = cfq_merged_request,
2438 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002439 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002441 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 .elevator_deactivate_req_fn = cfq_deactivate_request,
2443 .elevator_queue_empty_fn = cfq_queue_empty,
2444 .elevator_completed_req_fn = cfq_completed_request,
2445 .elevator_former_req_fn = cfq_former_request,
2446 .elevator_latter_req_fn = cfq_latter_request,
2447 .elevator_set_req_fn = cfq_set_request,
2448 .elevator_put_req_fn = cfq_put_request,
2449 .elevator_may_queue_fn = cfq_may_queue,
2450 .elevator_init_fn = cfq_init_queue,
2451 .elevator_exit_fn = cfq_exit_queue,
Al Viroe17a9482006-03-18 13:21:20 -05002452 .trim = cfq_trim,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 },
Al Viro3d1ab402006-03-18 18:35:43 -05002454 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .elevator_name = "cfq",
2456 .elevator_owner = THIS_MODULE,
2457};
2458
2459static int __init cfq_init(void)
2460{
2461 int ret;
2462
Jens Axboe22e2c502005-06-27 10:55:12 +02002463 /*
2464 * could be 0 on HZ < 1000 setups
2465 */
2466 if (!cfq_slice_async)
2467 cfq_slice_async = 1;
2468 if (!cfq_slice_idle)
2469 cfq_slice_idle = 1;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (cfq_slab_setup())
2472 return -ENOMEM;
2473
2474 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002475 if (ret)
2476 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return ret;
2479}
2480
2481static void __exit cfq_exit(void)
2482{
Al Viro334e94d2006-03-18 15:05:53 -05002483 DECLARE_COMPLETION(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002485 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002486 /* ioc_gone's update must be visible before reading ioc_count */
2487 smp_wmb();
Al Viro334e94d2006-03-18 15:05:53 -05002488 if (atomic_read(&ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002489 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002490 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002491 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494module_init(cfq_init);
2495module_exit(cfq_exit);
2496
2497MODULE_AUTHOR("Jens Axboe");
2498MODULE_LICENSE("GPL");
2499MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");