]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/cfq-iosched.c
cfq-iosched: sort RT queues into the rbtree
[mirror_ubuntu-artful-kernel.git] / block / cfq-iosched.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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 *
0fe23479 7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
1da177e4 8 */
1da177e4 9#include <linux/module.h>
1cc9be68
AV
10#include <linux/blkdev.h>
11#include <linux/elevator.h>
1da177e4
LT
12#include <linux/hash.h>
13#include <linux/rbtree.h>
22e2c507 14#include <linux/ioprio.h>
1da177e4
LT
15
16/*
17 * tunables
18 */
64100099 19static const int cfq_quantum = 4; /* max queue in one round of service */
64100099
AV
20static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
1da177e4 23
64100099 24static const int cfq_slice_sync = HZ / 10;
3b18152c 25static int cfq_slice_async = HZ / 25;
64100099 26static const int cfq_slice_async_rq = 2;
caaa5f9f 27static int cfq_slice_idle = HZ / 125;
22e2c507 28
d9e7620e
JA
29/*
30 * grace period before allowing idle class to get disk access
31 */
22e2c507 32#define CFQ_IDLE_GRACE (HZ / 10)
d9e7620e
JA
33
34/*
35 * below this threshold, we consider thinktime immediate
36 */
37#define CFQ_MIN_TT (2)
38
22e2c507
JA
39#define CFQ_SLICE_SCALE (5)
40
41#define CFQ_KEY_ASYNC (0)
22e2c507 42
1da177e4
LT
43/*
44 * for the hash of cfqq inside the cfqd
45 */
46#define CFQ_QHASH_SHIFT 6
47#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
48#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
49
1da177e4
LT
50#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
51
5e705374
JA
52#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
53#define RQ_CFQQ(rq) ((rq)->elevator_private2)
1da177e4 54
e18b890b
CL
55static struct kmem_cache *cfq_pool;
56static struct kmem_cache *cfq_ioc_pool;
1da177e4 57
4050cf16 58static DEFINE_PER_CPU(unsigned long, ioc_count);
334e94de
AV
59static struct completion *ioc_gone;
60
22e2c507
JA
61#define CFQ_PRIO_LISTS IOPRIO_BE_NR
62#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
22e2c507
JA
63#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
64
3b18152c
JA
65#define ASYNC (0)
66#define SYNC (1)
67
6d048f53 68#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
22e2c507 69
206dc69b
JA
70#define sample_valid(samples) ((samples) > 80)
71
cc09e299
JA
72/*
73 * Most of our rbtree usage is for sorting with min extraction, so
74 * if we cache the leftmost node we don't have to walk down the tree
75 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
76 * move this into the elevator for the rq sorting as well.
77 */
78struct cfq_rb_root {
79 struct rb_root rb;
80 struct rb_node *left;
81};
82#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
83
22e2c507
JA
84/*
85 * Per block device queue structure
86 */
1da177e4 87struct cfq_data {
22e2c507
JA
88 request_queue_t *queue;
89
90 /*
91 * rr list of queues with requests and the count of them
92 */
cc09e299 93 struct cfq_rb_root service_tree;
22e2c507
JA
94 struct list_head cur_rr;
95 struct list_head idle_rr;
96 unsigned int busy_queues;
97
22e2c507
JA
98 /*
99 * cfqq lookup hash
100 */
1da177e4 101 struct hlist_head *cfq_hash;
1da177e4 102
22e2c507 103 int rq_in_driver;
25776e35 104 int hw_tag;
1da177e4 105
22e2c507
JA
106 /*
107 * idle window management
108 */
109 struct timer_list idle_slice_timer;
110 struct work_struct unplug_work;
1da177e4 111
22e2c507
JA
112 struct cfq_queue *active_queue;
113 struct cfq_io_context *active_cic;
22e2c507
JA
114 unsigned int dispatch_slice;
115
116 struct timer_list idle_class_timer;
1da177e4 117
6d048f53 118 sector_t last_position;
22e2c507 119 unsigned long last_end_request;
1da177e4 120
1da177e4
LT
121 /*
122 * tunables, see top of file
123 */
124 unsigned int cfq_quantum;
22e2c507 125 unsigned int cfq_fifo_expire[2];
1da177e4
LT
126 unsigned int cfq_back_penalty;
127 unsigned int cfq_back_max;
22e2c507
JA
128 unsigned int cfq_slice[2];
129 unsigned int cfq_slice_async_rq;
130 unsigned int cfq_slice_idle;
d9ff4187
AV
131
132 struct list_head cic_list;
6d048f53
JA
133
134 sector_t new_seek_mean;
135 u64 new_seek_total;
1da177e4
LT
136};
137
22e2c507
JA
138/*
139 * Per process-grouping structure
140 */
1da177e4
LT
141struct cfq_queue {
142 /* reference count */
143 atomic_t ref;
144 /* parent cfq_data */
145 struct cfq_data *cfqd;
22e2c507 146 /* cfqq lookup hash */
1da177e4
LT
147 struct hlist_node cfq_hash;
148 /* hash key */
22e2c507 149 unsigned int key;
981a7973 150 /* member of the rr/busy/cur/idle cfqd list */
1da177e4 151 struct list_head cfq_list;
d9e7620e
JA
152 /* service_tree member */
153 struct rb_node rb_node;
154 /* service_tree key */
155 unsigned long rb_key;
1da177e4
LT
156 /* sorted list of pending requests */
157 struct rb_root sort_list;
158 /* if fifo isn't expired, next request to serve */
5e705374 159 struct request *next_rq;
1da177e4
LT
160 /* requests queued in sort_list */
161 int queued[2];
162 /* currently allocated requests */
163 int allocated[2];
374f84ac
JA
164 /* pending metadata requests */
165 int meta_pending;
1da177e4 166 /* fifo list of requests in sort_list */
22e2c507 167 struct list_head fifo;
1da177e4 168
22e2c507 169 unsigned long slice_end;
c5b680f3 170 long slice_resid;
1da177e4 171
6d048f53
JA
172 /* number of requests that are on the dispatch list or inside driver */
173 int dispatched;
22e2c507
JA
174
175 /* io prio of this group */
176 unsigned short ioprio, org_ioprio;
177 unsigned short ioprio_class, org_ioprio_class;
178
3b18152c
JA
179 /* various state flags, see below */
180 unsigned int flags;
6d048f53
JA
181
182 sector_t last_request_pos;
1da177e4
LT
183};
184
3b18152c 185enum cfqq_state_flags {
b0b8d749
JA
186 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
187 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
188 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
189 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
190 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
191 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
192 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
193 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
194 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
44f7c160 195 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
3b18152c
JA
196};
197
198#define CFQ_CFQQ_FNS(name) \
199static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
200{ \
201 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
202} \
203static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
204{ \
205 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
206} \
207static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
208{ \
209 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
210}
211
212CFQ_CFQQ_FNS(on_rr);
213CFQ_CFQQ_FNS(wait_request);
214CFQ_CFQQ_FNS(must_alloc);
215CFQ_CFQQ_FNS(must_alloc_slice);
216CFQ_CFQQ_FNS(must_dispatch);
217CFQ_CFQQ_FNS(fifo_expire);
218CFQ_CFQQ_FNS(idle_window);
219CFQ_CFQQ_FNS(prio_changed);
53b03744 220CFQ_CFQQ_FNS(queue_new);
44f7c160 221CFQ_CFQQ_FNS(slice_new);
3b18152c
JA
222#undef CFQ_CFQQ_FNS
223
3b18152c 224static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
5e705374 225static void cfq_dispatch_insert(request_queue_t *, struct request *);
6f325a13 226static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, gfp_t gfp_mask);
1da177e4 227
99f95e52
AM
228/*
229 * scheduler run of queue, if there are requests pending and no one in the
230 * driver that will restart queueing
231 */
232static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
233{
7b14e3b5 234 if (cfqd->busy_queues)
99f95e52
AM
235 kblockd_schedule_work(&cfqd->unplug_work);
236}
237
238static int cfq_queue_empty(request_queue_t *q)
239{
240 struct cfq_data *cfqd = q->elevator->elevator_data;
241
b4878f24 242 return !cfqd->busy_queues;
99f95e52
AM
243}
244
7749a8d4 245static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
206dc69b 246{
7749a8d4
JA
247 /*
248 * Use the per-process queue, for read requests and syncronous writes
249 */
250 if (!(rw & REQ_RW) || is_sync)
206dc69b
JA
251 return task->pid;
252
253 return CFQ_KEY_ASYNC;
254}
255
44f7c160
JA
256/*
257 * Scale schedule slice based on io priority. Use the sync time slice only
258 * if a queue is marked sync and has sync io queued. A sync queue with async
259 * io only, should not get full sync slice length.
260 */
d9e7620e
JA
261static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
262 unsigned short prio)
44f7c160 263{
d9e7620e 264 const int base_slice = cfqd->cfq_slice[sync];
44f7c160 265
d9e7620e
JA
266 WARN_ON(prio >= IOPRIO_BE_NR);
267
268 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
269}
44f7c160 270
d9e7620e
JA
271static inline int
272cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
273{
274 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
44f7c160
JA
275}
276
277static inline void
278cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
279{
280 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
281}
282
283/*
284 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
285 * isn't valid until the first request from the dispatch is activated
286 * and the slice time set.
287 */
288static inline int cfq_slice_used(struct cfq_queue *cfqq)
289{
290 if (cfq_cfqq_slice_new(cfqq))
291 return 0;
292 if (time_before(jiffies, cfqq->slice_end))
293 return 0;
294
295 return 1;
296}
297
1da177e4 298/*
5e705374 299 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4 300 * We choose the request that is closest to the head right now. Distance
e8a99053 301 * behind the head is penalized and only allowed to a certain extent.
1da177e4 302 */
5e705374
JA
303static struct request *
304cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
1da177e4
LT
305{
306 sector_t last, s1, s2, d1 = 0, d2 = 0;
1da177e4 307 unsigned long back_max;
e8a99053
AM
308#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
309#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
310 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
1da177e4 311
5e705374
JA
312 if (rq1 == NULL || rq1 == rq2)
313 return rq2;
314 if (rq2 == NULL)
315 return rq1;
9c2c38a1 316
5e705374
JA
317 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
318 return rq1;
319 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
320 return rq2;
374f84ac
JA
321 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
322 return rq1;
323 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
324 return rq2;
1da177e4 325
5e705374
JA
326 s1 = rq1->sector;
327 s2 = rq2->sector;
1da177e4 328
6d048f53 329 last = cfqd->last_position;
1da177e4 330
1da177e4
LT
331 /*
332 * by definition, 1KiB is 2 sectors
333 */
334 back_max = cfqd->cfq_back_max * 2;
335
336 /*
337 * Strict one way elevator _except_ in the case where we allow
338 * short backward seeks which are biased as twice the cost of a
339 * similar forward seek.
340 */
341 if (s1 >= last)
342 d1 = s1 - last;
343 else if (s1 + back_max >= last)
344 d1 = (last - s1) * cfqd->cfq_back_penalty;
345 else
e8a99053 346 wrap |= CFQ_RQ1_WRAP;
1da177e4
LT
347
348 if (s2 >= last)
349 d2 = s2 - last;
350 else if (s2 + back_max >= last)
351 d2 = (last - s2) * cfqd->cfq_back_penalty;
352 else
e8a99053 353 wrap |= CFQ_RQ2_WRAP;
1da177e4
LT
354
355 /* Found required data */
e8a99053
AM
356
357 /*
358 * By doing switch() on the bit mask "wrap" we avoid having to
359 * check two variables for all permutations: --> faster!
360 */
361 switch (wrap) {
5e705374 362 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053 363 if (d1 < d2)
5e705374 364 return rq1;
e8a99053 365 else if (d2 < d1)
5e705374 366 return rq2;
e8a99053
AM
367 else {
368 if (s1 >= s2)
5e705374 369 return rq1;
e8a99053 370 else
5e705374 371 return rq2;
e8a99053 372 }
1da177e4 373
e8a99053 374 case CFQ_RQ2_WRAP:
5e705374 375 return rq1;
e8a99053 376 case CFQ_RQ1_WRAP:
5e705374
JA
377 return rq2;
378 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053
AM
379 default:
380 /*
381 * Since both rqs are wrapped,
382 * start with the one that's further behind head
383 * (--> only *one* back seek required),
384 * since back seek takes more time than forward.
385 */
386 if (s1 <= s2)
5e705374 387 return rq1;
1da177e4 388 else
5e705374 389 return rq2;
1da177e4
LT
390 }
391}
392
cc09e299
JA
393static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
394{
395 if (!root->left)
396 root->left = rb_first(&root->rb);
397
398 return root->left;
399}
400
401static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
402{
403 if (root->left == n)
404 root->left = NULL;
405
406 rb_erase(n, &root->rb);
407 RB_CLEAR_NODE(n);
408}
409
1da177e4
LT
410/*
411 * would be nice to take fifo expire time into account as well
412 */
5e705374
JA
413static struct request *
414cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
415 struct request *last)
1da177e4 416{
21183b07
JA
417 struct rb_node *rbnext = rb_next(&last->rb_node);
418 struct rb_node *rbprev = rb_prev(&last->rb_node);
5e705374 419 struct request *next = NULL, *prev = NULL;
1da177e4 420
21183b07 421 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4
LT
422
423 if (rbprev)
5e705374 424 prev = rb_entry_rq(rbprev);
1da177e4 425
21183b07 426 if (rbnext)
5e705374 427 next = rb_entry_rq(rbnext);
21183b07
JA
428 else {
429 rbnext = rb_first(&cfqq->sort_list);
430 if (rbnext && rbnext != &last->rb_node)
5e705374 431 next = rb_entry_rq(rbnext);
21183b07 432 }
1da177e4 433
21183b07 434 return cfq_choose_req(cfqd, next, prev);
1da177e4
LT
435}
436
d9e7620e
JA
437static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
438 struct cfq_queue *cfqq)
1da177e4 439{
d9e7620e
JA
440 /*
441 * just an approximation, should be ok.
442 */
443 return ((cfqd->busy_queues - 1) * cfq_prio_slice(cfqd, 1, 0));
444}
445
446static void cfq_service_tree_add(struct cfq_data *cfqd,
447 struct cfq_queue *cfqq)
448{
cc09e299 449 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
d9e7620e 450 struct rb_node *parent = NULL;
d9e7620e 451 unsigned long rb_key;
cc09e299 452 int left = 1;
d9e7620e
JA
453
454 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
455 rb_key += cfqq->slice_resid;
456 cfqq->slice_resid = 0;
1da177e4 457
d9e7620e 458 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
99f9628a 459 /*
d9e7620e 460 * same position, nothing more to do
99f9628a 461 */
d9e7620e
JA
462 if (rb_key == cfqq->rb_key)
463 return;
1da177e4 464
cc09e299 465 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
1da177e4 466 }
d9e7620e
JA
467
468 while (*p) {
cc09e299
JA
469 struct cfq_queue *__cfqq;
470
d9e7620e
JA
471 parent = *p;
472 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
473
0c534e0a
JA
474 /*
475 * sort RT queues first, we always want to give
476 * preference to them. after that, sort on the next
477 * service time.
478 */
479 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
480 p = &(*p)->rb_left;
481 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
482 p = &(*p)->rb_right;
483 else if (rb_key < __cfqq->rb_key)
d9e7620e 484 p = &(*p)->rb_left;
cc09e299 485 else {
d9e7620e 486 p = &(*p)->rb_right;
cc09e299
JA
487 left = 0;
488 }
d9e7620e
JA
489 }
490
cc09e299
JA
491 if (left)
492 cfqd->service_tree.left = &cfqq->rb_node;
493
d9e7620e
JA
494 cfqq->rb_key = rb_key;
495 rb_link_node(&cfqq->rb_node, parent, p);
cc09e299 496 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
1da177e4
LT
497}
498
6d048f53
JA
499static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
500{
501 struct cfq_data *cfqd = cfqq->cfqd;
6d048f53
JA
502
503 /*
504 * Resorting requires the cfqq to be on the RR list already.
505 */
506 if (!cfq_cfqq_on_rr(cfqq))
507 return;
508
d9e7620e 509 list_del_init(&cfqq->cfq_list);
6d048f53 510
0c534e0a 511 if (cfq_class_idle(cfqq)) {
6d048f53
JA
512 /*
513 * IDLE goes to the tail of the idle list
514 */
515 list_add_tail(&cfqq->cfq_list, &cfqd->idle_rr);
516 } else {
517 /*
0c534e0a 518 * RT and BE queues, sort into the rbtree
6d048f53 519 */
d9e7620e 520 cfq_service_tree_add(cfqd, cfqq);
6d048f53
JA
521 }
522}
523
1da177e4
LT
524/*
525 * add to busy list of queues for service, trying to be fair in ordering
22e2c507 526 * the pending list according to last request service
1da177e4
LT
527 */
528static inline void
b4878f24 529cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 530{
3b18152c
JA
531 BUG_ON(cfq_cfqq_on_rr(cfqq));
532 cfq_mark_cfqq_on_rr(cfqq);
1da177e4
LT
533 cfqd->busy_queues++;
534
b4878f24 535 cfq_resort_rr_list(cfqq, 0);
1da177e4
LT
536}
537
538static inline void
539cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
540{
3b18152c
JA
541 BUG_ON(!cfq_cfqq_on_rr(cfqq));
542 cfq_clear_cfqq_on_rr(cfqq);
981a7973 543 list_del_init(&cfqq->cfq_list);
1da177e4 544
cc09e299
JA
545 if (!RB_EMPTY_NODE(&cfqq->rb_node))
546 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
d9e7620e 547
1da177e4
LT
548 BUG_ON(!cfqd->busy_queues);
549 cfqd->busy_queues--;
550}
551
552/*
553 * rb tree support functions
554 */
5e705374 555static inline void cfq_del_rq_rb(struct request *rq)
1da177e4 556{
5e705374 557 struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f24 558 struct cfq_data *cfqd = cfqq->cfqd;
5e705374 559 const int sync = rq_is_sync(rq);
1da177e4 560
b4878f24
JA
561 BUG_ON(!cfqq->queued[sync]);
562 cfqq->queued[sync]--;
1da177e4 563
5e705374 564 elv_rb_del(&cfqq->sort_list, rq);
1da177e4 565
dd67d051 566 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
b4878f24 567 cfq_del_cfqq_rr(cfqd, cfqq);
1da177e4
LT
568}
569
5e705374 570static void cfq_add_rq_rb(struct request *rq)
1da177e4 571{
5e705374 572 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 573 struct cfq_data *cfqd = cfqq->cfqd;
21183b07 574 struct request *__alias;
1da177e4 575
5380a101 576 cfqq->queued[rq_is_sync(rq)]++;
1da177e4
LT
577
578 /*
579 * looks a little odd, but the first insert might return an alias.
580 * if that happens, put the alias on the dispatch list
581 */
21183b07 582 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
5e705374 583 cfq_dispatch_insert(cfqd->queue, __alias);
5fccbf61
JA
584
585 if (!cfq_cfqq_on_rr(cfqq))
586 cfq_add_cfqq_rr(cfqd, cfqq);
5044eed4
JA
587
588 /*
589 * check if this request is a better next-serve candidate
590 */
591 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
592 BUG_ON(!cfqq->next_rq);
1da177e4
LT
593}
594
595static inline void
5e705374 596cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4 597{
5380a101
JA
598 elv_rb_del(&cfqq->sort_list, rq);
599 cfqq->queued[rq_is_sync(rq)]--;
5e705374 600 cfq_add_rq_rb(rq);
1da177e4
LT
601}
602
206dc69b
JA
603static struct request *
604cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4 605{
206dc69b 606 struct task_struct *tsk = current;
7749a8d4 607 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
206dc69b 608 struct cfq_queue *cfqq;
1da177e4 609
206dc69b 610 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
89850f7e
JA
611 if (cfqq) {
612 sector_t sector = bio->bi_sector + bio_sectors(bio);
613
21183b07 614 return elv_rb_find(&cfqq->sort_list, sector);
89850f7e 615 }
1da177e4 616
1da177e4
LT
617 return NULL;
618}
619
b4878f24 620static void cfq_activate_request(request_queue_t *q, struct request *rq)
1da177e4 621{
22e2c507 622 struct cfq_data *cfqd = q->elevator->elevator_data;
3b18152c 623
b4878f24 624 cfqd->rq_in_driver++;
25776e35
JA
625
626 /*
627 * If the depth is larger 1, it really could be queueing. But lets
628 * make the mark a little higher - idling could still be good for
629 * low queueing, and a low queueing number could also just indicate
630 * a SCSI mid layer like behaviour where limit+1 is often seen.
631 */
632 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
633 cfqd->hw_tag = 1;
6d048f53
JA
634
635 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
1da177e4
LT
636}
637
b4878f24 638static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
1da177e4 639{
b4878f24
JA
640 struct cfq_data *cfqd = q->elevator->elevator_data;
641
642 WARN_ON(!cfqd->rq_in_driver);
643 cfqd->rq_in_driver--;
1da177e4
LT
644}
645
b4878f24 646static void cfq_remove_request(struct request *rq)
1da177e4 647{
5e705374 648 struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07 649
5e705374
JA
650 if (cfqq->next_rq == rq)
651 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1da177e4 652
b4878f24 653 list_del_init(&rq->queuelist);
5e705374 654 cfq_del_rq_rb(rq);
374f84ac
JA
655
656 if (rq_is_meta(rq)) {
657 WARN_ON(!cfqq->meta_pending);
658 cfqq->meta_pending--;
659 }
1da177e4
LT
660}
661
662static int
663cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
664{
665 struct cfq_data *cfqd = q->elevator->elevator_data;
666 struct request *__rq;
1da177e4 667
206dc69b 668 __rq = cfq_find_rq_fmerge(cfqd, bio);
22e2c507 669 if (__rq && elv_rq_merge_ok(__rq, bio)) {
9817064b
JA
670 *req = __rq;
671 return ELEVATOR_FRONT_MERGE;
1da177e4
LT
672 }
673
674 return ELEVATOR_NO_MERGE;
1da177e4
LT
675}
676
21183b07
JA
677static void cfq_merged_request(request_queue_t *q, struct request *req,
678 int type)
1da177e4 679{
21183b07 680 if (type == ELEVATOR_FRONT_MERGE) {
5e705374 681 struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4 682
5e705374 683 cfq_reposition_rq_rb(cfqq, req);
1da177e4 684 }
1da177e4
LT
685}
686
687static void
688cfq_merged_requests(request_queue_t *q, struct request *rq,
689 struct request *next)
690{
22e2c507
JA
691 /*
692 * reposition in fifo if next is older than rq
693 */
694 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
695 time_before(next->start_time, rq->start_time))
696 list_move(&rq->queuelist, &next->queuelist);
697
b4878f24 698 cfq_remove_request(next);
22e2c507
JA
699}
700
da775265
JA
701static int cfq_allow_merge(request_queue_t *q, struct request *rq,
702 struct bio *bio)
703{
704 struct cfq_data *cfqd = q->elevator->elevator_data;
705 const int rw = bio_data_dir(bio);
706 struct cfq_queue *cfqq;
707 pid_t key;
708
709 /*
ec8acb69 710 * Disallow merge of a sync bio into an async request.
da775265 711 */
ec8acb69 712 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
da775265
JA
713 return 0;
714
715 /*
719d3402
JA
716 * Lookup the cfqq that this bio will be queued with. Allow
717 * merge only if rq is queued there.
da775265 718 */
719d3402 719 key = cfq_queue_pid(current, rw, bio_sync(bio));
da775265 720 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
719d3402
JA
721
722 if (cfqq == RQ_CFQQ(rq))
723 return 1;
da775265 724
ec8acb69 725 return 0;
da775265
JA
726}
727
22e2c507
JA
728static inline void
729__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
730{
731 if (cfqq) {
732 /*
733 * stop potential idle class queues waiting service
734 */
735 del_timer(&cfqd->idle_class_timer);
736
22e2c507 737 cfqq->slice_end = 0;
3b18152c
JA
738 cfq_clear_cfqq_must_alloc_slice(cfqq);
739 cfq_clear_cfqq_fifo_expire(cfqq);
44f7c160 740 cfq_mark_cfqq_slice_new(cfqq);
1afba045 741 cfq_clear_cfqq_queue_new(cfqq);
22e2c507
JA
742 }
743
744 cfqd->active_queue = cfqq;
745}
746
7b14e3b5
JA
747/*
748 * current cfqq expired its slice (or was too idle), select new one
749 */
750static void
751__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3c6bd2f8 752 int preempted, int timed_out)
7b14e3b5 753{
7b14e3b5
JA
754 if (cfq_cfqq_wait_request(cfqq))
755 del_timer(&cfqd->idle_slice_timer);
756
7b14e3b5
JA
757 cfq_clear_cfqq_must_dispatch(cfqq);
758 cfq_clear_cfqq_wait_request(cfqq);
759
760 /*
761 * store what was left of this slice, if the queue idled out
762 * or was preempted
763 */
3c6bd2f8 764 if (timed_out && !cfq_cfqq_slice_new(cfqq))
c5b680f3 765 cfqq->slice_resid = cfqq->slice_end - jiffies;
7b14e3b5 766
98e41c7d 767 cfq_resort_rr_list(cfqq, preempted);
7b14e3b5
JA
768
769 if (cfqq == cfqd->active_queue)
770 cfqd->active_queue = NULL;
771
772 if (cfqd->active_cic) {
773 put_io_context(cfqd->active_cic->ioc);
774 cfqd->active_cic = NULL;
775 }
776
777 cfqd->dispatch_slice = 0;
778}
779
3c6bd2f8
JA
780static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted,
781 int timed_out)
7b14e3b5
JA
782{
783 struct cfq_queue *cfqq = cfqd->active_queue;
784
785 if (cfqq)
3c6bd2f8 786 __cfq_slice_expired(cfqd, cfqq, preempted, timed_out);
7b14e3b5
JA
787}
788
6d048f53 789static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507 790{
7b14e3b5 791 struct cfq_queue *cfqq = NULL;
22e2c507 792
d9e7620e 793 if (!list_empty(&cfqd->cur_rr)) {
89850f7e 794 /*
d9e7620e 795 * if current list is non-empty, grab first entry.
89850f7e 796 */
d9e7620e 797 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
cc09e299
JA
798 } else if (!RB_EMPTY_ROOT(&cfqd->service_tree.rb)) {
799 struct rb_node *n = cfq_rb_first(&cfqd->service_tree);
d9e7620e
JA
800
801 cfqq = rb_entry(n, struct cfq_queue, rb_node);
89850f7e
JA
802 } else if (!list_empty(&cfqd->idle_rr)) {
803 /*
804 * if we have idle queues and no rt or be queues had pending
805 * requests, either allow immediate service if the grace period
806 * has passed or arm the idle grace timer
807 */
22e2c507
JA
808 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
809
810 if (time_after_eq(jiffies, end))
811 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
812 else
813 mod_timer(&cfqd->idle_class_timer, end);
814 }
815
6d048f53
JA
816 return cfqq;
817}
818
819static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
820{
821 struct cfq_queue *cfqq;
822
d9e7620e 823 cfqq = cfq_get_next_queue(cfqd);
22e2c507 824 __cfq_set_active_queue(cfqd, cfqq);
3b18152c 825 return cfqq;
22e2c507
JA
826}
827
d9e7620e
JA
828static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
829 struct request *rq)
830{
831 if (rq->sector >= cfqd->last_position)
832 return rq->sector - cfqd->last_position;
833 else
834 return cfqd->last_position - rq->sector;
835}
836
6d048f53
JA
837static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
838{
839 struct cfq_io_context *cic = cfqd->active_cic;
840
841 if (!sample_valid(cic->seek_samples))
842 return 0;
843
844 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
845}
846
d9e7620e
JA
847static int cfq_close_cooperator(struct cfq_data *cfq_data,
848 struct cfq_queue *cfqq)
6d048f53 849{
6d048f53 850 /*
d9e7620e
JA
851 * We should notice if some of the queues are cooperating, eg
852 * working closely on the same area of the disk. In that case,
853 * we can group them together and don't waste time idling.
6d048f53 854 */
d9e7620e 855 return 0;
6d048f53
JA
856}
857
858#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
caaa5f9f 859
6d048f53 860static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507 861{
1792669c 862 struct cfq_queue *cfqq = cfqd->active_queue;
206dc69b 863 struct cfq_io_context *cic;
7b14e3b5
JA
864 unsigned long sl;
865
dd67d051 866 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f53 867 WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507
JA
868
869 /*
870 * idle is disabled, either manually or by past process history
871 */
6d048f53
JA
872 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
873 return;
874
22e2c507
JA
875 /*
876 * task has exited, don't wait
877 */
206dc69b
JA
878 cic = cfqd->active_cic;
879 if (!cic || !cic->ioc->task)
6d048f53
JA
880 return;
881
882 /*
883 * See if this prio level has a good candidate
884 */
1afba045
JA
885 if (cfq_close_cooperator(cfqd, cfqq) &&
886 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
6d048f53 887 return;
22e2c507 888
3b18152c
JA
889 cfq_mark_cfqq_must_dispatch(cfqq);
890 cfq_mark_cfqq_wait_request(cfqq);
22e2c507 891
206dc69b
JA
892 /*
893 * we don't want to idle for seeks, but we do want to allow
894 * fair distribution of slice time for a process doing back-to-back
895 * seeks. so allow a little bit of time for him to submit a new rq
896 */
6d048f53 897 sl = cfqd->cfq_slice_idle;
caaa5f9f 898 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
d9e7620e 899 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
206dc69b 900
7b14e3b5 901 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
1da177e4
LT
902}
903
5e705374 904static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
1da177e4 905{
5e705374 906 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 907
5380a101 908 cfq_remove_request(rq);
6d048f53 909 cfqq->dispatched++;
5380a101 910 elv_dispatch_sort(q, rq);
1da177e4
LT
911}
912
913/*
914 * return expired entry, or NULL to just start from scratch in rbtree
915 */
5e705374 916static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4
LT
917{
918 struct cfq_data *cfqd = cfqq->cfqd;
22e2c507 919 struct request *rq;
89850f7e 920 int fifo;
1da177e4 921
3b18152c 922 if (cfq_cfqq_fifo_expire(cfqq))
1da177e4 923 return NULL;
cb887411
JA
924
925 cfq_mark_cfqq_fifo_expire(cfqq);
926
89850f7e
JA
927 if (list_empty(&cfqq->fifo))
928 return NULL;
1da177e4 929
6d048f53 930 fifo = cfq_cfqq_sync(cfqq);
89850f7e 931 rq = rq_entry_fifo(cfqq->fifo.next);
1da177e4 932
6d048f53
JA
933 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
934 return NULL;
1da177e4 935
6d048f53 936 return rq;
1da177e4
LT
937}
938
22e2c507
JA
939static inline int
940cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
941{
942 const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4 943
22e2c507 944 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4 945
22e2c507 946 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1da177e4
LT
947}
948
22e2c507
JA
949/*
950 * get next queue for service
951 */
1b5ed5e1 952static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4 953{
1da177e4 954 struct cfq_queue *cfqq;
1da177e4 955
22e2c507
JA
956 cfqq = cfqd->active_queue;
957 if (!cfqq)
958 goto new_queue;
1da177e4 959
22e2c507 960 /*
6d048f53 961 * The active queue has run out of time, expire it and select new.
22e2c507 962 */
6d048f53 963 if (cfq_slice_used(cfqq))
3b18152c 964 goto expire;
1da177e4 965
22e2c507 966 /*
6d048f53
JA
967 * The active queue has requests and isn't expired, allow it to
968 * dispatch.
22e2c507 969 */
dd67d051 970 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 971 goto keep_queue;
6d048f53
JA
972
973 /*
974 * No requests pending. If the active queue still has requests in
975 * flight or is idling for a new request, allow either of these
976 * conditions to happen (or time out) before selecting a new queue.
977 */
978 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
caaa5f9f
JA
979 cfqq = NULL;
980 goto keep_queue;
22e2c507
JA
981 }
982
3b18152c 983expire:
3c6bd2f8 984 cfq_slice_expired(cfqd, 0, 0);
3b18152c
JA
985new_queue:
986 cfqq = cfq_set_active_queue(cfqd);
22e2c507 987keep_queue:
3b18152c 988 return cfqq;
22e2c507
JA
989}
990
991static int
992__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
993 int max_dispatch)
994{
995 int dispatched = 0;
996
dd67d051 997 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
22e2c507
JA
998
999 do {
5e705374 1000 struct request *rq;
1da177e4
LT
1001
1002 /*
22e2c507 1003 * follow expired path, else get first next available
1da177e4 1004 */
5e705374
JA
1005 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1006 rq = cfqq->next_rq;
22e2c507
JA
1007
1008 /*
1009 * finally, insert request into driver dispatch list
1010 */
5e705374 1011 cfq_dispatch_insert(cfqd->queue, rq);
1da177e4 1012
22e2c507
JA
1013 cfqd->dispatch_slice++;
1014 dispatched++;
1da177e4 1015
22e2c507 1016 if (!cfqd->active_cic) {
5e705374
JA
1017 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1018 cfqd->active_cic = RQ_CIC(rq);
22e2c507 1019 }
1da177e4 1020
dd67d051 1021 if (RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507
JA
1022 break;
1023
1024 } while (dispatched < max_dispatch);
1025
22e2c507
JA
1026 /*
1027 * expire an async queue immediately if it has used up its slice. idle
1028 * queue always expire after 1 dispatch round.
1029 */
a9938006 1030 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
22e2c507 1031 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
a9938006 1032 cfq_class_idle(cfqq))) {
44f7c160 1033 cfqq->slice_end = jiffies + 1;
3c6bd2f8 1034 cfq_slice_expired(cfqd, 0, 0);
44f7c160 1035 }
22e2c507
JA
1036
1037 return dispatched;
1038}
1039
d9e7620e
JA
1040static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1041{
1042 int dispatched = 0;
1043
1044 while (cfqq->next_rq) {
1045 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1046 dispatched++;
1047 }
1048
1049 BUG_ON(!list_empty(&cfqq->fifo));
1050 return dispatched;
1051}
1052
1053static int cfq_forced_dispatch_cfqqs(struct list_head *list)
1b5ed5e1 1054{
1b5ed5e1 1055 struct cfq_queue *cfqq, *next;
caaa5f9f 1056 int dispatched;
1b5ed5e1 1057
caaa5f9f 1058 dispatched = 0;
d9e7620e
JA
1059 list_for_each_entry_safe(cfqq, next, list, cfq_list)
1060 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
caaa5f9f 1061
1b5ed5e1
TH
1062 return dispatched;
1063}
1064
d9e7620e 1065static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1 1066{
d9e7620e
JA
1067 int dispatched = 0;
1068 struct rb_node *n;
1069
cc09e299 1070 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
d9e7620e 1071 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1b5ed5e1 1072
d9e7620e
JA
1073 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1074 }
1b5ed5e1 1075
1b5ed5e1
TH
1076 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1077 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1078
3c6bd2f8 1079 cfq_slice_expired(cfqd, 0, 0);
1b5ed5e1
TH
1080
1081 BUG_ON(cfqd->busy_queues);
1082
1083 return dispatched;
1084}
1085
d9e7620e 1086static int cfq_dispatch_requests(request_queue_t *q, int force)
22e2c507
JA
1087{
1088 struct cfq_data *cfqd = q->elevator->elevator_data;
6d048f53 1089 struct cfq_queue *cfqq;
caaa5f9f 1090 int dispatched;
22e2c507
JA
1091
1092 if (!cfqd->busy_queues)
1093 return 0;
1094
1b5ed5e1
TH
1095 if (unlikely(force))
1096 return cfq_forced_dispatch(cfqd);
1097
caaa5f9f 1098 dispatched = 0;
caaa5f9f 1099 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
b4878f24
JA
1100 int max_dispatch;
1101
a9938006 1102 if (cfqd->busy_queues > 1) {
a9938006
JA
1103 /*
1104 * So we have dispatched before in this round, if the
1105 * next queue has idling enabled (must be sync), don't
6d048f53 1106 * allow it service until the previous have completed.
a9938006 1107 */
6d048f53
JA
1108 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1109 dispatched)
1110 break;
1111 if (cfqq->dispatched >= cfqd->cfq_quantum)
a9938006
JA
1112 break;
1113 }
9ede209e 1114
3b18152c
JA
1115 cfq_clear_cfqq_must_dispatch(cfqq);
1116 cfq_clear_cfqq_wait_request(cfqq);
22e2c507
JA
1117 del_timer(&cfqd->idle_slice_timer);
1118
1b5ed5e1
TH
1119 max_dispatch = cfqd->cfq_quantum;
1120 if (cfq_class_idle(cfqq))
1121 max_dispatch = 1;
1da177e4 1122
caaa5f9f 1123 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
1da177e4
LT
1124 }
1125
caaa5f9f 1126 return dispatched;
1da177e4
LT
1127}
1128
1da177e4 1129/*
5e705374
JA
1130 * task holds one reference to the queue, dropped when task exits. each rq
1131 * in-flight on this queue also holds a reference, dropped when rq is freed.
1da177e4
LT
1132 *
1133 * queue lock must be held here.
1134 */
1135static void cfq_put_queue(struct cfq_queue *cfqq)
1136{
22e2c507
JA
1137 struct cfq_data *cfqd = cfqq->cfqd;
1138
1139 BUG_ON(atomic_read(&cfqq->ref) <= 0);
1da177e4
LT
1140
1141 if (!atomic_dec_and_test(&cfqq->ref))
1142 return;
1143
1144 BUG_ON(rb_first(&cfqq->sort_list));
22e2c507 1145 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
3b18152c 1146 BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4 1147
28f95cbc 1148 if (unlikely(cfqd->active_queue == cfqq)) {
3c6bd2f8 1149 __cfq_slice_expired(cfqd, cfqq, 0, 0);
28f95cbc
JA
1150 cfq_schedule_dispatch(cfqd);
1151 }
22e2c507 1152
1da177e4
LT
1153 /*
1154 * it's on the empty list and still hashed
1155 */
1da177e4
LT
1156 hlist_del(&cfqq->cfq_hash);
1157 kmem_cache_free(cfq_pool, cfqq);
1158}
1159
1ea25ecb 1160static struct cfq_queue *
3b18152c
JA
1161__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1162 const int hashval)
1da177e4
LT
1163{
1164 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
206dc69b
JA
1165 struct hlist_node *entry;
1166 struct cfq_queue *__cfqq;
1da177e4 1167
206dc69b 1168 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
b0a6916b 1169 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
1da177e4 1170
206dc69b 1171 if (__cfqq->key == key && (__p == prio || !prio))
1da177e4
LT
1172 return __cfqq;
1173 }
1174
1175 return NULL;
1176}
1177
1178static struct cfq_queue *
3b18152c 1179cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1da177e4 1180{
3b18152c 1181 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1da177e4
LT
1182}
1183
e2d74ac0 1184static void cfq_free_io_context(struct io_context *ioc)
1da177e4 1185{
22e2c507 1186 struct cfq_io_context *__cic;
e2d74ac0
JA
1187 struct rb_node *n;
1188 int freed = 0;
1da177e4 1189
e2d74ac0
JA
1190 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1191 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1192 rb_erase(&__cic->rb_node, &ioc->cic_root);
22e2c507 1193 kmem_cache_free(cfq_ioc_pool, __cic);
334e94de 1194 freed++;
1da177e4
LT
1195 }
1196
4050cf16
JA
1197 elv_ioc_count_mod(ioc_count, -freed);
1198
1199 if (ioc_gone && !elv_ioc_count_read(ioc_count))
334e94de 1200 complete(ioc_gone);
1da177e4
LT
1201}
1202
89850f7e 1203static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 1204{
28f95cbc 1205 if (unlikely(cfqq == cfqd->active_queue)) {
3c6bd2f8 1206 __cfq_slice_expired(cfqd, cfqq, 0, 0);
28f95cbc
JA
1207 cfq_schedule_dispatch(cfqd);
1208 }
22e2c507 1209
89850f7e
JA
1210 cfq_put_queue(cfqq);
1211}
22e2c507 1212
89850f7e
JA
1213static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1214 struct cfq_io_context *cic)
1215{
fc46379d
JA
1216 list_del_init(&cic->queue_list);
1217 smp_wmb();
1218 cic->key = NULL;
1219
12a05732 1220 if (cic->cfqq[ASYNC]) {
89850f7e 1221 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
12a05732
AV
1222 cic->cfqq[ASYNC] = NULL;
1223 }
1224
1225 if (cic->cfqq[SYNC]) {
89850f7e 1226 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
12a05732
AV
1227 cic->cfqq[SYNC] = NULL;
1228 }
89850f7e
JA
1229}
1230
1231
1232/*
1233 * Called with interrupts disabled
1234 */
1235static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1236{
1237 struct cfq_data *cfqd = cic->key;
1238
89850f7e
JA
1239 if (cfqd) {
1240 request_queue_t *q = cfqd->queue;
1241
fc46379d 1242 spin_lock_irq(q->queue_lock);
89850f7e 1243 __cfq_exit_single_io_context(cfqd, cic);
fc46379d 1244 spin_unlock_irq(q->queue_lock);
89850f7e 1245 }
1da177e4
LT
1246}
1247
e2d74ac0 1248static void cfq_exit_io_context(struct io_context *ioc)
1da177e4 1249{
22e2c507 1250 struct cfq_io_context *__cic;
e2d74ac0 1251 struct rb_node *n;
22e2c507 1252
1da177e4
LT
1253 /*
1254 * put the reference this task is holding to the various queues
1255 */
e2d74ac0
JA
1256
1257 n = rb_first(&ioc->cic_root);
1258 while (n != NULL) {
1259 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1260
22e2c507 1261 cfq_exit_single_io_context(__cic);
e2d74ac0 1262 n = rb_next(n);
1da177e4 1263 }
1da177e4
LT
1264}
1265
22e2c507 1266static struct cfq_io_context *
8267e268 1267cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1da177e4 1268{
b5deef90 1269 struct cfq_io_context *cic;
1da177e4 1270
b5deef90 1271 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
1da177e4 1272 if (cic) {
553698f9 1273 memset(cic, 0, sizeof(*cic));
22e2c507 1274 cic->last_end_request = jiffies;
553698f9 1275 INIT_LIST_HEAD(&cic->queue_list);
22e2c507
JA
1276 cic->dtor = cfq_free_io_context;
1277 cic->exit = cfq_exit_io_context;
4050cf16 1278 elv_ioc_count_inc(ioc_count);
1da177e4
LT
1279 }
1280
1281 return cic;
1282}
1283
22e2c507
JA
1284static void cfq_init_prio_data(struct cfq_queue *cfqq)
1285{
1286 struct task_struct *tsk = current;
1287 int ioprio_class;
1288
3b18152c 1289 if (!cfq_cfqq_prio_changed(cfqq))
22e2c507
JA
1290 return;
1291
1292 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1293 switch (ioprio_class) {
1294 default:
1295 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1296 case IOPRIO_CLASS_NONE:
1297 /*
1298 * no prio set, place us in the middle of the BE classes
1299 */
1300 cfqq->ioprio = task_nice_ioprio(tsk);
1301 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1302 break;
1303 case IOPRIO_CLASS_RT:
1304 cfqq->ioprio = task_ioprio(tsk);
1305 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1306 break;
1307 case IOPRIO_CLASS_BE:
1308 cfqq->ioprio = task_ioprio(tsk);
1309 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1310 break;
1311 case IOPRIO_CLASS_IDLE:
1312 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1313 cfqq->ioprio = 7;
3b18152c 1314 cfq_clear_cfqq_idle_window(cfqq);
22e2c507
JA
1315 break;
1316 }
1317
1318 /*
1319 * keep track of original prio settings in case we have to temporarily
1320 * elevate the priority of this queue
1321 */
1322 cfqq->org_ioprio = cfqq->ioprio;
1323 cfqq->org_ioprio_class = cfqq->ioprio_class;
3b18152c 1324 cfq_clear_cfqq_prio_changed(cfqq);
22e2c507
JA
1325}
1326
478a82b0 1327static inline void changed_ioprio(struct cfq_io_context *cic)
22e2c507 1328{
478a82b0
AV
1329 struct cfq_data *cfqd = cic->key;
1330 struct cfq_queue *cfqq;
c1b707d2 1331 unsigned long flags;
35e6077c 1332
caaa5f9f
JA
1333 if (unlikely(!cfqd))
1334 return;
1335
c1b707d2 1336 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
caaa5f9f
JA
1337
1338 cfqq = cic->cfqq[ASYNC];
1339 if (cfqq) {
1340 struct cfq_queue *new_cfqq;
1341 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1342 GFP_ATOMIC);
1343 if (new_cfqq) {
1344 cic->cfqq[ASYNC] = new_cfqq;
1345 cfq_put_queue(cfqq);
1346 }
22e2c507 1347 }
caaa5f9f
JA
1348
1349 cfqq = cic->cfqq[SYNC];
1350 if (cfqq)
1351 cfq_mark_cfqq_prio_changed(cfqq);
1352
c1b707d2 1353 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
22e2c507
JA
1354}
1355
fc46379d 1356static void cfq_ioc_set_ioprio(struct io_context *ioc)
22e2c507 1357{
a6a0763a 1358 struct cfq_io_context *cic;
e2d74ac0 1359 struct rb_node *n;
a6a0763a 1360
fc46379d 1361 ioc->ioprio_changed = 0;
a6a0763a 1362
e2d74ac0
JA
1363 n = rb_first(&ioc->cic_root);
1364 while (n != NULL) {
1365 cic = rb_entry(n, struct cfq_io_context, rb_node);
3793c65c 1366
478a82b0 1367 changed_ioprio(cic);
e2d74ac0
JA
1368 n = rb_next(n);
1369 }
22e2c507
JA
1370}
1371
1372static struct cfq_queue *
6f325a13 1373cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
8267e268 1374 gfp_t gfp_mask)
22e2c507
JA
1375{
1376 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1377 struct cfq_queue *cfqq, *new_cfqq = NULL;
6f325a13 1378 unsigned short ioprio;
22e2c507
JA
1379
1380retry:
6f325a13 1381 ioprio = tsk->ioprio;
3b18152c 1382 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
22e2c507
JA
1383
1384 if (!cfqq) {
1385 if (new_cfqq) {
1386 cfqq = new_cfqq;
1387 new_cfqq = NULL;
1388 } else if (gfp_mask & __GFP_WAIT) {
89850f7e
JA
1389 /*
1390 * Inform the allocator of the fact that we will
1391 * just repeat this allocation if it fails, to allow
1392 * the allocator to do whatever it needs to attempt to
1393 * free memory.
1394 */
22e2c507 1395 spin_unlock_irq(cfqd->queue->queue_lock);
b5deef90 1396 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
22e2c507
JA
1397 spin_lock_irq(cfqd->queue->queue_lock);
1398 goto retry;
1399 } else {
b5deef90 1400 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
22e2c507
JA
1401 if (!cfqq)
1402 goto out;
1403 }
1404
1405 memset(cfqq, 0, sizeof(*cfqq));
1406
1407 INIT_HLIST_NODE(&cfqq->cfq_hash);
1408 INIT_LIST_HEAD(&cfqq->cfq_list);
d9e7620e 1409 RB_CLEAR_NODE(&cfqq->rb_node);
22e2c507
JA
1410 INIT_LIST_HEAD(&cfqq->fifo);
1411
1412 cfqq->key = key;
1413 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1414 atomic_set(&cfqq->ref, 0);
1415 cfqq->cfqd = cfqd;
c5b680f3 1416
a9938006
JA
1417 if (key != CFQ_KEY_ASYNC)
1418 cfq_mark_cfqq_idle_window(cfqq);
1419
3b18152c 1420 cfq_mark_cfqq_prio_changed(cfqq);
53b03744 1421 cfq_mark_cfqq_queue_new(cfqq);
3b18152c 1422 cfq_init_prio_data(cfqq);
22e2c507
JA
1423 }
1424
1425 if (new_cfqq)
1426 kmem_cache_free(cfq_pool, new_cfqq);
1427
1428 atomic_inc(&cfqq->ref);
1429out:
1430 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1431 return cfqq;
1432}
1433
dbecf3ab
OH
1434static void
1435cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1436{
fc46379d 1437 WARN_ON(!list_empty(&cic->queue_list));
dbecf3ab 1438 rb_erase(&cic->rb_node, &ioc->cic_root);
dbecf3ab 1439 kmem_cache_free(cfq_ioc_pool, cic);
4050cf16 1440 elv_ioc_count_dec(ioc_count);
dbecf3ab
OH
1441}
1442
e2d74ac0
JA
1443static struct cfq_io_context *
1444cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1445{
dbecf3ab 1446 struct rb_node *n;
e2d74ac0 1447 struct cfq_io_context *cic;
be3b0753 1448 void *k, *key = cfqd;
e2d74ac0 1449
dbecf3ab
OH
1450restart:
1451 n = ioc->cic_root.rb_node;
e2d74ac0
JA
1452 while (n) {
1453 cic = rb_entry(n, struct cfq_io_context, rb_node);
be3b0753
OH
1454 /* ->key must be copied to avoid race with cfq_exit_queue() */
1455 k = cic->key;
1456 if (unlikely(!k)) {
dbecf3ab
OH
1457 cfq_drop_dead_cic(ioc, cic);
1458 goto restart;
1459 }
e2d74ac0 1460
be3b0753 1461 if (key < k)
e2d74ac0 1462 n = n->rb_left;
be3b0753 1463 else if (key > k)
e2d74ac0
JA
1464 n = n->rb_right;
1465 else
1466 return cic;
1467 }
1468
1469 return NULL;
1470}
1471
1472static inline void
1473cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1474 struct cfq_io_context *cic)
1475{
dbecf3ab
OH
1476 struct rb_node **p;
1477 struct rb_node *parent;
e2d74ac0 1478 struct cfq_io_context *__cic;
0261d688 1479 unsigned long flags;
be3b0753 1480 void *k;
e2d74ac0 1481
e2d74ac0
JA
1482 cic->ioc = ioc;
1483 cic->key = cfqd;
1484
dbecf3ab
OH
1485restart:
1486 parent = NULL;
1487 p = &ioc->cic_root.rb_node;
e2d74ac0
JA
1488 while (*p) {
1489 parent = *p;
1490 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
be3b0753
OH
1491 /* ->key must be copied to avoid race with cfq_exit_queue() */
1492 k = __cic->key;
1493 if (unlikely(!k)) {
be33c3a6 1494 cfq_drop_dead_cic(ioc, __cic);
dbecf3ab
OH
1495 goto restart;
1496 }
e2d74ac0 1497
be3b0753 1498 if (cic->key < k)
e2d74ac0 1499 p = &(*p)->rb_left;
be3b0753 1500 else if (cic->key > k)
e2d74ac0
JA
1501 p = &(*p)->rb_right;
1502 else
1503 BUG();
1504 }
1505
1506 rb_link_node(&cic->rb_node, parent, p);
1507 rb_insert_color(&cic->rb_node, &ioc->cic_root);
fc46379d 1508
0261d688 1509 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
e2d74ac0 1510 list_add(&cic->queue_list, &cfqd->cic_list);
0261d688 1511 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
e2d74ac0
JA
1512}
1513
1da177e4
LT
1514/*
1515 * Setup general io context and cfq io context. There can be several cfq
1516 * io contexts per general io context, if this process is doing io to more
e2d74ac0 1517 * than one device managed by cfq.
1da177e4
LT
1518 */
1519static struct cfq_io_context *
e2d74ac0 1520cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
1da177e4 1521{
22e2c507 1522 struct io_context *ioc = NULL;
1da177e4 1523 struct cfq_io_context *cic;
1da177e4 1524
22e2c507 1525 might_sleep_if(gfp_mask & __GFP_WAIT);
1da177e4 1526
b5deef90 1527 ioc = get_io_context(gfp_mask, cfqd->queue->node);
1da177e4
LT
1528 if (!ioc)
1529 return NULL;
1530
e2d74ac0
JA
1531 cic = cfq_cic_rb_lookup(cfqd, ioc);
1532 if (cic)
1533 goto out;
1da177e4 1534
e2d74ac0
JA
1535 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1536 if (cic == NULL)
1537 goto err;
1da177e4 1538
e2d74ac0 1539 cfq_cic_link(cfqd, ioc, cic);
1da177e4 1540out:
fc46379d
JA
1541 smp_read_barrier_depends();
1542 if (unlikely(ioc->ioprio_changed))
1543 cfq_ioc_set_ioprio(ioc);
1544
1da177e4
LT
1545 return cic;
1546err:
1547 put_io_context(ioc);
1548 return NULL;
1549}
1550
22e2c507
JA
1551static void
1552cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1da177e4 1553{
aaf1228d
JA
1554 unsigned long elapsed = jiffies - cic->last_end_request;
1555 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
db3b5848 1556
22e2c507
JA
1557 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1558 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1559 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1560}
1da177e4 1561
206dc69b 1562static void
6d048f53
JA
1563cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1564 struct request *rq)
206dc69b
JA
1565{
1566 sector_t sdist;
1567 u64 total;
1568
5e705374
JA
1569 if (cic->last_request_pos < rq->sector)
1570 sdist = rq->sector - cic->last_request_pos;
206dc69b 1571 else
5e705374 1572 sdist = cic->last_request_pos - rq->sector;
206dc69b 1573
6d048f53
JA
1574 if (!cic->seek_samples) {
1575 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1576 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1577 }
1578
206dc69b
JA
1579 /*
1580 * Don't allow the seek distance to get too large from the
1581 * odd fragment, pagein, etc
1582 */
1583 if (cic->seek_samples <= 60) /* second&third seek */
1584 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1585 else
1586 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1587
1588 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1589 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1590 total = cic->seek_total + (cic->seek_samples/2);
1591 do_div(total, cic->seek_samples);
1592 cic->seek_mean = (sector_t)total;
1593}
1da177e4 1594
22e2c507
JA
1595/*
1596 * Disable idle window if the process thinks too long or seeks so much that
1597 * it doesn't matter
1598 */
1599static void
1600cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1601 struct cfq_io_context *cic)
1602{
3b18152c 1603 int enable_idle = cfq_cfqq_idle_window(cfqq);
1da177e4 1604
caaa5f9f
JA
1605 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1606 (cfqd->hw_tag && CIC_SEEKY(cic)))
22e2c507
JA
1607 enable_idle = 0;
1608 else if (sample_valid(cic->ttime_samples)) {
1609 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1610 enable_idle = 0;
1611 else
1612 enable_idle = 1;
1da177e4
LT
1613 }
1614
3b18152c
JA
1615 if (enable_idle)
1616 cfq_mark_cfqq_idle_window(cfqq);
1617 else
1618 cfq_clear_cfqq_idle_window(cfqq);
22e2c507 1619}
1da177e4 1620
22e2c507
JA
1621/*
1622 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1623 * no or if we aren't sure, a 1 will cause a preempt.
1624 */
1625static int
1626cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e705374 1627 struct request *rq)
22e2c507 1628{
6d048f53 1629 struct cfq_queue *cfqq;
22e2c507 1630
6d048f53
JA
1631 cfqq = cfqd->active_queue;
1632 if (!cfqq)
22e2c507
JA
1633 return 0;
1634
6d048f53
JA
1635 if (cfq_slice_used(cfqq))
1636 return 1;
1637
1638 if (cfq_class_idle(new_cfqq))
caaa5f9f 1639 return 0;
22e2c507
JA
1640
1641 if (cfq_class_idle(cfqq))
1642 return 1;
1e3335de 1643
374f84ac
JA
1644 /*
1645 * if the new request is sync, but the currently running queue is
1646 * not, let the sync request have priority.
1647 */
5e705374 1648 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
22e2c507 1649 return 1;
1e3335de 1650
374f84ac
JA
1651 /*
1652 * So both queues are sync. Let the new request get disk time if
1653 * it's a metadata request and the current queue is doing regular IO.
1654 */
1655 if (rq_is_meta(rq) && !cfqq->meta_pending)
1656 return 1;
22e2c507 1657
1e3335de
JA
1658 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1659 return 0;
1660
1661 /*
1662 * if this request is as-good as one we would expect from the
1663 * current cfqq, let it preempt
1664 */
6d048f53 1665 if (cfq_rq_close(cfqd, rq))
1e3335de
JA
1666 return 1;
1667
22e2c507
JA
1668 return 0;
1669}
1670
1671/*
1672 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1673 * let it have half of its nominal slice.
1674 */
1675static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1676{
3c6bd2f8 1677 cfq_slice_expired(cfqd, 1, 1);
22e2c507 1678
bf572256
JA
1679 /*
1680 * Put the new queue at the front of the of the current list,
1681 * so we know that it will be selected next.
1682 */
1683 BUG_ON(!cfq_cfqq_on_rr(cfqq));
d9e7620e
JA
1684 list_del_init(&cfqq->cfq_list);
1685 list_add(&cfqq->cfq_list, &cfqd->cur_rr);
bf572256 1686
44f7c160
JA
1687 cfqq->slice_end = 0;
1688 cfq_mark_cfqq_slice_new(cfqq);
22e2c507
JA
1689}
1690
22e2c507 1691/*
5e705374 1692 * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507
JA
1693 * something we should do about it
1694 */
1695static void
5e705374
JA
1696cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1697 struct request *rq)
22e2c507 1698{
5e705374 1699 struct cfq_io_context *cic = RQ_CIC(rq);
12e9fddd 1700
374f84ac
JA
1701 if (rq_is_meta(rq))
1702 cfqq->meta_pending++;
1703
9c2c38a1 1704 cfq_update_io_thinktime(cfqd, cic);
6d048f53 1705 cfq_update_io_seektime(cfqd, cic, rq);
9c2c38a1
JA
1706 cfq_update_idle_window(cfqd, cfqq, cic);
1707
5e705374 1708 cic->last_request_pos = rq->sector + rq->nr_sectors;
6d048f53 1709 cfqq->last_request_pos = cic->last_request_pos;
22e2c507
JA
1710
1711 if (cfqq == cfqd->active_queue) {
1712 /*
1713 * if we are waiting for a request for this queue, let it rip
1714 * immediately and flag that we must not expire this queue
1715 * just now
1716 */
3b18152c
JA
1717 if (cfq_cfqq_wait_request(cfqq)) {
1718 cfq_mark_cfqq_must_dispatch(cfqq);
22e2c507 1719 del_timer(&cfqd->idle_slice_timer);
dc72ef4a 1720 blk_start_queueing(cfqd->queue);
22e2c507 1721 }
5e705374 1722 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507
JA
1723 /*
1724 * not the active queue - expire current slice if it is
1725 * idle and has expired it's mean thinktime or this new queue
1726 * has some old slice time left and is of higher priority
1727 */
1728 cfq_preempt_queue(cfqd, cfqq);
3b18152c 1729 cfq_mark_cfqq_must_dispatch(cfqq);
dc72ef4a 1730 blk_start_queueing(cfqd->queue);
22e2c507 1731 }
1da177e4
LT
1732}
1733
b4878f24 1734static void cfq_insert_request(request_queue_t *q, struct request *rq)
1da177e4 1735{
b4878f24 1736 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 1737 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507
JA
1738
1739 cfq_init_prio_data(cfqq);
1da177e4 1740
5e705374 1741 cfq_add_rq_rb(rq);
1da177e4 1742
22e2c507
JA
1743 list_add_tail(&rq->queuelist, &cfqq->fifo);
1744
5e705374 1745 cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4
LT
1746}
1747
1da177e4
LT
1748static void cfq_completed_request(request_queue_t *q, struct request *rq)
1749{
5e705374 1750 struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f24 1751 struct cfq_data *cfqd = cfqq->cfqd;
5380a101 1752 const int sync = rq_is_sync(rq);
b4878f24 1753 unsigned long now;
1da177e4 1754
b4878f24 1755 now = jiffies;
1da177e4 1756
b4878f24 1757 WARN_ON(!cfqd->rq_in_driver);
6d048f53 1758 WARN_ON(!cfqq->dispatched);
b4878f24 1759 cfqd->rq_in_driver--;
6d048f53 1760 cfqq->dispatched--;
1da177e4 1761
b4878f24
JA
1762 if (!cfq_class_idle(cfqq))
1763 cfqd->last_end_request = now;
3b18152c 1764
caaa5f9f 1765 if (sync)
5e705374 1766 RQ_CIC(rq)->last_end_request = now;
caaa5f9f
JA
1767
1768 /*
1769 * If this is the active queue, check if it needs to be expired,
1770 * or if we want to idle in case it has no pending requests.
1771 */
1772 if (cfqd->active_queue == cfqq) {
44f7c160
JA
1773 if (cfq_cfqq_slice_new(cfqq)) {
1774 cfq_set_prio_slice(cfqd, cfqq);
1775 cfq_clear_cfqq_slice_new(cfqq);
1776 }
1777 if (cfq_slice_used(cfqq))
3c6bd2f8 1778 cfq_slice_expired(cfqd, 0, 1);
6d048f53
JA
1779 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1780 cfq_arm_slice_timer(cfqd);
caaa5f9f 1781 }
6d048f53
JA
1782
1783 if (!cfqd->rq_in_driver)
1784 cfq_schedule_dispatch(cfqd);
1da177e4
LT
1785}
1786
22e2c507
JA
1787/*
1788 * we temporarily boost lower priority queues if they are holding fs exclusive
1789 * resources. they are boosted to normal prio (CLASS_BE/4)
1790 */
1791static void cfq_prio_boost(struct cfq_queue *cfqq)
1da177e4 1792{
22e2c507
JA
1793 if (has_fs_excl()) {
1794 /*
1795 * boost idle prio on transactions that would lock out other
1796 * users of the filesystem
1797 */
1798 if (cfq_class_idle(cfqq))
1799 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1800 if (cfqq->ioprio > IOPRIO_NORM)
1801 cfqq->ioprio = IOPRIO_NORM;
1802 } else {
1803 /*
1804 * check if we need to unboost the queue
1805 */
1806 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1807 cfqq->ioprio_class = cfqq->org_ioprio_class;
1808 if (cfqq->ioprio != cfqq->org_ioprio)
1809 cfqq->ioprio = cfqq->org_ioprio;
1810 }
22e2c507 1811}
1da177e4 1812
89850f7e 1813static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507 1814{
3b18152c 1815 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
99f95e52 1816 !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c 1817 cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507 1818 return ELV_MQUEUE_MUST;
3b18152c 1819 }
1da177e4 1820
22e2c507 1821 return ELV_MQUEUE_MAY;
22e2c507
JA
1822}
1823
cb78b285 1824static int cfq_may_queue(request_queue_t *q, int rw)
22e2c507
JA
1825{
1826 struct cfq_data *cfqd = q->elevator->elevator_data;
1827 struct task_struct *tsk = current;
1828 struct cfq_queue *cfqq;
7749a8d4
JA
1829 unsigned int key;
1830
1831 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
22e2c507
JA
1832
1833 /*
1834 * don't force setup of a queue from here, as a call to may_queue
1835 * does not necessarily imply that a request actually will be queued.
1836 * so just lookup a possibly existing queue, or return 'may queue'
1837 * if that fails
1838 */
7749a8d4 1839 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
22e2c507
JA
1840 if (cfqq) {
1841 cfq_init_prio_data(cfqq);
1842 cfq_prio_boost(cfqq);
1843
89850f7e 1844 return __cfq_may_queue(cfqq);
22e2c507
JA
1845 }
1846
1847 return ELV_MQUEUE_MAY;
1da177e4
LT
1848}
1849
1da177e4
LT
1850/*
1851 * queue lock held here
1852 */
bb37b94c 1853static void cfq_put_request(struct request *rq)
1da177e4 1854{
5e705374 1855 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 1856
5e705374 1857 if (cfqq) {
22e2c507 1858 const int rw = rq_data_dir(rq);
1da177e4 1859
22e2c507
JA
1860 BUG_ON(!cfqq->allocated[rw]);
1861 cfqq->allocated[rw]--;
1da177e4 1862
5e705374 1863 put_io_context(RQ_CIC(rq)->ioc);
1da177e4 1864
1da177e4 1865 rq->elevator_private = NULL;
5e705374 1866 rq->elevator_private2 = NULL;
1da177e4 1867
1da177e4
LT
1868 cfq_put_queue(cfqq);
1869 }
1870}
1871
1872/*
22e2c507 1873 * Allocate cfq data structures associated with this request.
1da177e4 1874 */
22e2c507 1875static int
cb78b285 1876cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
1da177e4
LT
1877{
1878 struct cfq_data *cfqd = q->elevator->elevator_data;
3b18152c 1879 struct task_struct *tsk = current;
1da177e4
LT
1880 struct cfq_io_context *cic;
1881 const int rw = rq_data_dir(rq);
7749a8d4
JA
1882 const int is_sync = rq_is_sync(rq);
1883 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
22e2c507 1884 struct cfq_queue *cfqq;
1da177e4
LT
1885 unsigned long flags;
1886
1887 might_sleep_if(gfp_mask & __GFP_WAIT);
1888
e2d74ac0 1889 cic = cfq_get_io_context(cfqd, gfp_mask);
22e2c507 1890
1da177e4
LT
1891 spin_lock_irqsave(q->queue_lock, flags);
1892
22e2c507
JA
1893 if (!cic)
1894 goto queue_fail;
1895
12a05732 1896 if (!cic->cfqq[is_sync]) {
6f325a13 1897 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
22e2c507
JA
1898 if (!cfqq)
1899 goto queue_fail;
1da177e4 1900
12a05732 1901 cic->cfqq[is_sync] = cfqq;
22e2c507 1902 } else
12a05732 1903 cfqq = cic->cfqq[is_sync];
1da177e4
LT
1904
1905 cfqq->allocated[rw]++;
3b18152c 1906 cfq_clear_cfqq_must_alloc(cfqq);
22e2c507 1907 atomic_inc(&cfqq->ref);
1da177e4 1908
5e705374 1909 spin_unlock_irqrestore(q->queue_lock, flags);
3b18152c 1910
5e705374
JA
1911 rq->elevator_private = cic;
1912 rq->elevator_private2 = cfqq;
1913 return 0;
1da177e4 1914
22e2c507
JA
1915queue_fail:
1916 if (cic)
1917 put_io_context(cic->ioc);
89850f7e 1918
3b18152c 1919 cfq_schedule_dispatch(cfqd);
1da177e4
LT
1920 spin_unlock_irqrestore(q->queue_lock, flags);
1921 return 1;
1922}
1923
65f27f38 1924static void cfq_kick_queue(struct work_struct *work)
22e2c507 1925{
65f27f38
DH
1926 struct cfq_data *cfqd =
1927 container_of(work, struct cfq_data, unplug_work);
1928 request_queue_t *q = cfqd->queue;
22e2c507
JA
1929 unsigned long flags;
1930
1931 spin_lock_irqsave(q->queue_lock, flags);
dc72ef4a 1932 blk_start_queueing(q);
22e2c507
JA
1933 spin_unlock_irqrestore(q->queue_lock, flags);
1934}
1935
1936/*
1937 * Timer running if the active_queue is currently idling inside its time slice
1938 */
1939static void cfq_idle_slice_timer(unsigned long data)
1940{
1941 struct cfq_data *cfqd = (struct cfq_data *) data;
1942 struct cfq_queue *cfqq;
1943 unsigned long flags;
3c6bd2f8 1944 int timed_out = 1;
22e2c507
JA
1945
1946 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1947
1948 if ((cfqq = cfqd->active_queue) != NULL) {
3c6bd2f8
JA
1949 timed_out = 0;
1950
22e2c507
JA
1951 /*
1952 * expired
1953 */
44f7c160 1954 if (cfq_slice_used(cfqq))
22e2c507
JA
1955 goto expire;
1956
1957 /*
1958 * only expire and reinvoke request handler, if there are
1959 * other queues with pending requests
1960 */
caaa5f9f 1961 if (!cfqd->busy_queues)
22e2c507 1962 goto out_cont;
22e2c507
JA
1963
1964 /*
1965 * not expired and it has a request pending, let it dispatch
1966 */
dd67d051 1967 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
3b18152c 1968 cfq_mark_cfqq_must_dispatch(cfqq);
22e2c507
JA
1969 goto out_kick;
1970 }
1971 }
1972expire:
3c6bd2f8 1973 cfq_slice_expired(cfqd, 0, timed_out);
22e2c507 1974out_kick:
3b18152c 1975 cfq_schedule_dispatch(cfqd);
22e2c507
JA
1976out_cont:
1977 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1978}
1979
1980/*
1981 * Timer running if an idle class queue is waiting for service
1982 */
1983static void cfq_idle_class_timer(unsigned long data)
1984{
1985 struct cfq_data *cfqd = (struct cfq_data *) data;
1986 unsigned long flags, end;
1987
1988 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1989
1990 /*
1991 * race with a non-idle queue, reset timer
1992 */
1993 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
ae818a38
JA
1994 if (!time_after_eq(jiffies, end))
1995 mod_timer(&cfqd->idle_class_timer, end);
1996 else
3b18152c 1997 cfq_schedule_dispatch(cfqd);
22e2c507
JA
1998
1999 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2000}
2001
3b18152c
JA
2002static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2003{
2004 del_timer_sync(&cfqd->idle_slice_timer);
2005 del_timer_sync(&cfqd->idle_class_timer);
2006 blk_sync_queue(cfqd->queue);
2007}
22e2c507 2008
1da177e4
LT
2009static void cfq_exit_queue(elevator_t *e)
2010{
22e2c507 2011 struct cfq_data *cfqd = e->elevator_data;
d9ff4187 2012 request_queue_t *q = cfqd->queue;
22e2c507 2013
3b18152c 2014 cfq_shutdown_timer_wq(cfqd);
e2d74ac0 2015
d9ff4187 2016 spin_lock_irq(q->queue_lock);
e2d74ac0 2017
d9ff4187 2018 if (cfqd->active_queue)
3c6bd2f8 2019 __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0);
e2d74ac0
JA
2020
2021 while (!list_empty(&cfqd->cic_list)) {
d9ff4187
AV
2022 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2023 struct cfq_io_context,
2024 queue_list);
89850f7e
JA
2025
2026 __cfq_exit_single_io_context(cfqd, cic);
d9ff4187 2027 }
e2d74ac0 2028
d9ff4187 2029 spin_unlock_irq(q->queue_lock);
a90d742e
AV
2030
2031 cfq_shutdown_timer_wq(cfqd);
2032
a90d742e
AV
2033 kfree(cfqd->cfq_hash);
2034 kfree(cfqd);
1da177e4
LT
2035}
2036
bb37b94c 2037static void *cfq_init_queue(request_queue_t *q)
1da177e4
LT
2038{
2039 struct cfq_data *cfqd;
2040 int i;
2041
b5deef90 2042 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
1da177e4 2043 if (!cfqd)
bc1c1169 2044 return NULL;
1da177e4
LT
2045
2046 memset(cfqd, 0, sizeof(*cfqd));
22e2c507 2047
cc09e299 2048 cfqd->service_tree = CFQ_RB_ROOT;
22e2c507
JA
2049 INIT_LIST_HEAD(&cfqd->cur_rr);
2050 INIT_LIST_HEAD(&cfqd->idle_rr);
d9ff4187 2051 INIT_LIST_HEAD(&cfqd->cic_list);
1da177e4 2052
b5deef90 2053 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
1da177e4 2054 if (!cfqd->cfq_hash)
5e705374 2055 goto out_free;
1da177e4 2056
1da177e4
LT
2057 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2058 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2059
1da177e4 2060 cfqd->queue = q;
1da177e4 2061
22e2c507
JA
2062 init_timer(&cfqd->idle_slice_timer);
2063 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2064 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2065
2066 init_timer(&cfqd->idle_class_timer);
2067 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2068 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2069
65f27f38 2070 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507 2071
1da177e4 2072 cfqd->cfq_quantum = cfq_quantum;
22e2c507
JA
2073 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2074 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4
LT
2075 cfqd->cfq_back_max = cfq_back_max;
2076 cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507
JA
2077 cfqd->cfq_slice[0] = cfq_slice_async;
2078 cfqd->cfq_slice[1] = cfq_slice_sync;
2079 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2080 cfqd->cfq_slice_idle = cfq_slice_idle;
3b18152c 2081
bc1c1169 2082 return cfqd;
5e705374 2083out_free:
1da177e4 2084 kfree(cfqd);
bc1c1169 2085 return NULL;
1da177e4
LT
2086}
2087
2088static void cfq_slab_kill(void)
2089{
1da177e4
LT
2090 if (cfq_pool)
2091 kmem_cache_destroy(cfq_pool);
2092 if (cfq_ioc_pool)
2093 kmem_cache_destroy(cfq_ioc_pool);
2094}
2095
2096static int __init cfq_slab_setup(void)
2097{
1da177e4
LT
2098 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2099 NULL, NULL);
2100 if (!cfq_pool)
2101 goto fail;
2102
2103 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2104 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2105 if (!cfq_ioc_pool)
2106 goto fail;
2107
2108 return 0;
2109fail:
2110 cfq_slab_kill();
2111 return -ENOMEM;
2112}
2113
1da177e4
LT
2114/*
2115 * sysfs parts below -->
2116 */
1da177e4
LT
2117static ssize_t
2118cfq_var_show(unsigned int var, char *page)
2119{
2120 return sprintf(page, "%d\n", var);
2121}
2122
2123static ssize_t
2124cfq_var_store(unsigned int *var, const char *page, size_t count)
2125{
2126 char *p = (char *) page;
2127
2128 *var = simple_strtoul(p, &p, 10);
2129 return count;
2130}
2131
1da177e4 2132#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
3d1ab40f 2133static ssize_t __FUNC(elevator_t *e, char *page) \
1da177e4 2134{ \
3d1ab40f 2135 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
2136 unsigned int __data = __VAR; \
2137 if (__CONV) \
2138 __data = jiffies_to_msecs(__data); \
2139 return cfq_var_show(__data, (page)); \
2140}
2141SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
22e2c507
JA
2142SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2143SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
e572ec7e
AV
2144SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2145SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
22e2c507
JA
2146SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2147SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2148SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2149SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
1da177e4
LT
2150#undef SHOW_FUNCTION
2151
2152#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
3d1ab40f 2153static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1da177e4 2154{ \
3d1ab40f 2155 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
2156 unsigned int __data; \
2157 int ret = cfq_var_store(&__data, (page), count); \
2158 if (__data < (MIN)) \
2159 __data = (MIN); \
2160 else if (__data > (MAX)) \
2161 __data = (MAX); \
2162 if (__CONV) \
2163 *(__PTR) = msecs_to_jiffies(__data); \
2164 else \
2165 *(__PTR) = __data; \
2166 return ret; \
2167}
2168STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
22e2c507
JA
2169STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2170STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
e572ec7e
AV
2171STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2172STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
22e2c507
JA
2173STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2174STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2175STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2176STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
1da177e4
LT
2177#undef STORE_FUNCTION
2178
e572ec7e
AV
2179#define CFQ_ATTR(name) \
2180 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
2181
2182static struct elv_fs_entry cfq_attrs[] = {
2183 CFQ_ATTR(quantum),
e572ec7e
AV
2184 CFQ_ATTR(fifo_expire_sync),
2185 CFQ_ATTR(fifo_expire_async),
2186 CFQ_ATTR(back_seek_max),
2187 CFQ_ATTR(back_seek_penalty),
2188 CFQ_ATTR(slice_sync),
2189 CFQ_ATTR(slice_async),
2190 CFQ_ATTR(slice_async_rq),
2191 CFQ_ATTR(slice_idle),
e572ec7e 2192 __ATTR_NULL
1da177e4
LT
2193};
2194
1da177e4
LT
2195static struct elevator_type iosched_cfq = {
2196 .ops = {
2197 .elevator_merge_fn = cfq_merge,
2198 .elevator_merged_fn = cfq_merged_request,
2199 .elevator_merge_req_fn = cfq_merged_requests,
da775265 2200 .elevator_allow_merge_fn = cfq_allow_merge,
b4878f24 2201 .elevator_dispatch_fn = cfq_dispatch_requests,
1da177e4 2202 .elevator_add_req_fn = cfq_insert_request,
b4878f24 2203 .elevator_activate_req_fn = cfq_activate_request,
1da177e4
LT
2204 .elevator_deactivate_req_fn = cfq_deactivate_request,
2205 .elevator_queue_empty_fn = cfq_queue_empty,
2206 .elevator_completed_req_fn = cfq_completed_request,
21183b07
JA
2207 .elevator_former_req_fn = elv_rb_former_request,
2208 .elevator_latter_req_fn = elv_rb_latter_request,
1da177e4
LT
2209 .elevator_set_req_fn = cfq_set_request,
2210 .elevator_put_req_fn = cfq_put_request,
2211 .elevator_may_queue_fn = cfq_may_queue,
2212 .elevator_init_fn = cfq_init_queue,
2213 .elevator_exit_fn = cfq_exit_queue,
fc46379d 2214 .trim = cfq_free_io_context,
1da177e4 2215 },
3d1ab40f 2216 .elevator_attrs = cfq_attrs,
1da177e4
LT
2217 .elevator_name = "cfq",
2218 .elevator_owner = THIS_MODULE,
2219};
2220
2221static int __init cfq_init(void)
2222{
2223 int ret;
2224
22e2c507
JA
2225 /*
2226 * could be 0 on HZ < 1000 setups
2227 */
2228 if (!cfq_slice_async)
2229 cfq_slice_async = 1;
2230 if (!cfq_slice_idle)
2231 cfq_slice_idle = 1;
2232
1da177e4
LT
2233 if (cfq_slab_setup())
2234 return -ENOMEM;
2235
2236 ret = elv_register(&iosched_cfq);
22e2c507
JA
2237 if (ret)
2238 cfq_slab_kill();
1da177e4 2239
1da177e4
LT
2240 return ret;
2241}
2242
2243static void __exit cfq_exit(void)
2244{
6e9a4738 2245 DECLARE_COMPLETION_ONSTACK(all_gone);
1da177e4 2246 elv_unregister(&iosched_cfq);
334e94de 2247 ioc_gone = &all_gone;
fba82272
OH
2248 /* ioc_gone's update must be visible before reading ioc_count */
2249 smp_wmb();
4050cf16 2250 if (elv_ioc_count_read(ioc_count))
fba82272 2251 wait_for_completion(ioc_gone);
334e94de 2252 synchronize_rcu();
83521d3e 2253 cfq_slab_kill();
1da177e4
LT
2254}
2255
2256module_init(cfq_init);
2257module_exit(cfq_exit);
2258
2259MODULE_AUTHOR("Jens Axboe");
2260MODULE_LICENSE("GPL");
2261MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");