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