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