aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/cfq-iosched.c
diff options
context:
space:
mode:
authorJens Axboe <axboe@suse.de>2005-06-27 04:56:24 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-06-27 17:33:30 -0400
commit3b18152c327707ae6a2eeba4cfb66457143753bc (patch)
tree5cdf299af4119762c036cc98a09e23458e0b37fa /drivers/block/cfq-iosched.c
parent3d25f3566bb606720a67caef77b16190df10dd98 (diff)
[PATCH] CFQ io scheduler updates
- Adjust slice values - Instead of one async queue, one is defined per priority level. This prevents kernel threads (such as reiserfs/x and others) that run at higher io priority from conflicting with others. Previously, it was a coin toss what io prio the async queue got, it was defined by who first set up the queue. - Let a time slice only begin, when the previous slice is completely done. Previously we could be somewhat unfair to a new sync slice, if the previous slice was async and had several ios queued. This might need a little tweaking if throughput suffers a little due to this, allowing perhaps an overlap of a single request or so. - Optimize the calling of kblockd_schedule_work() by doing it only when it is strictly necessary (no requests in driver and work left to do). - Correct sync vs async logic. A 'normal' process can be purely async as well, and a flusher can be purely sync as well. Sync or async is now a property of the class defined and requests pending. Previously writers could be considered sync, when they were really async. - Get rid of the bit fields in cfqq and crq, use flags instead. - Various other cleanups and fixes Signed-off-by: Jens Axboe <axboe@suse.de> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/block/cfq-iosched.c')
-rw-r--r--drivers/block/cfq-iosched.c460
1 files changed, 299 insertions, 161 deletions
diff --git a/drivers/block/cfq-iosched.c b/drivers/block/cfq-iosched.c
index baa3e268250a..1ecb179b8604 100644
--- a/drivers/block/cfq-iosched.c
+++ b/drivers/block/cfq-iosched.c
@@ -34,14 +34,15 @@ static int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
34static int cfq_back_penalty = 2; /* penalty of a backwards seek */ 34static int cfq_back_penalty = 2; /* penalty of a backwards seek */
35 35
36static int cfq_slice_sync = HZ / 10; 36static int cfq_slice_sync = HZ / 10;
37static int cfq_slice_async = HZ / 50; 37static int cfq_slice_async = HZ / 25;
38static int cfq_slice_async_rq = 2; 38static int cfq_slice_async_rq = 2;
39static int cfq_slice_idle = HZ / 50; 39static int cfq_slice_idle = HZ / 100;
40 40
41#define CFQ_IDLE_GRACE (HZ / 10) 41#define CFQ_IDLE_GRACE (HZ / 10)
42#define CFQ_SLICE_SCALE (5) 42#define CFQ_SLICE_SCALE (5)
43 43
44#define CFQ_KEY_ASYNC (0) 44#define CFQ_KEY_ASYNC (0)
45#define CFQ_KEY_ANY (0xffff)
45 46
46/* 47/*
47 * disable queueing at the driver/hardware level 48 * disable queueing at the driver/hardware level
@@ -96,7 +97,16 @@ static kmem_cache_t *cfq_ioc_pool;
96#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE) 97#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) 98#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
98 99
99#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC) 100#define ASYNC (0)
101#define SYNC (1)
102
103#define cfq_cfqq_dispatched(cfqq) \
104 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
105
106#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
107
108#define cfq_cfqq_sync(cfqq) \
109 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
100 110
101/* 111/*
102 * Per block device queue structure 112 * Per block device queue structure
@@ -200,28 +210,15 @@ struct cfq_queue {
200 unsigned long slice_left; 210 unsigned long slice_left;
201 unsigned long service_last; 211 unsigned long service_last;
202 212
203 /* number of requests that have been handed to the driver */ 213 /* number of requests that are on the dispatch list */
204 int in_flight; 214 int on_dispatch[2];
205 215
206 /* io prio of this group */ 216 /* io prio of this group */
207 unsigned short ioprio, org_ioprio; 217 unsigned short ioprio, org_ioprio;
208 unsigned short ioprio_class, org_ioprio_class; 218 unsigned short ioprio_class, org_ioprio_class;
209 219
210 /* whether queue is on rr (or empty) list */ 220 /* various state flags, see below */
211 unsigned on_rr : 1; 221 unsigned int flags;
212 /* idle slice, waiting for new request submission */
213 unsigned wait_request : 1;
214 /* set when wait_request gets set, reset on first rq alloc */
215 unsigned must_alloc : 1;
216 /* only gets one must_alloc per slice */
217 unsigned must_alloc_slice : 1;
218 /* idle slice, request added, now waiting to dispatch it */
219 unsigned must_dispatch : 1;
220 /* fifo expire per-slice */
221 unsigned fifo_expire : 1;
222
223 unsigned idle_window : 1;
224 unsigned prio_changed : 1;
225}; 222};
226 223
227struct cfq_rq { 224struct cfq_rq {
@@ -233,15 +230,77 @@ struct cfq_rq {
233 struct cfq_queue *cfq_queue; 230 struct cfq_queue *cfq_queue;
234 struct cfq_io_context *io_context; 231 struct cfq_io_context *io_context;
235 232
236 unsigned in_flight : 1; 233 unsigned int crq_flags;
237 unsigned accounted : 1;
238 unsigned is_sync : 1;
239 unsigned requeued : 1;
240}; 234};
241 235
242static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int); 236enum cfqq_state_flags {
237 CFQ_CFQQ_FLAG_on_rr = 0,
238 CFQ_CFQQ_FLAG_wait_request,
239 CFQ_CFQQ_FLAG_must_alloc,
240 CFQ_CFQQ_FLAG_must_alloc_slice,
241 CFQ_CFQQ_FLAG_must_dispatch,
242 CFQ_CFQQ_FLAG_fifo_expire,
243 CFQ_CFQQ_FLAG_idle_window,
244 CFQ_CFQQ_FLAG_prio_changed,
245 CFQ_CFQQ_FLAG_expired,
246};
247
248#define CFQ_CFQQ_FNS(name) \
249static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
250{ \
251 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
252} \
253static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
254{ \
255 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
256} \
257static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
258{ \
259 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
260}
261
262CFQ_CFQQ_FNS(on_rr);
263CFQ_CFQQ_FNS(wait_request);
264CFQ_CFQQ_FNS(must_alloc);
265CFQ_CFQQ_FNS(must_alloc_slice);
266CFQ_CFQQ_FNS(must_dispatch);
267CFQ_CFQQ_FNS(fifo_expire);
268CFQ_CFQQ_FNS(idle_window);
269CFQ_CFQQ_FNS(prio_changed);
270CFQ_CFQQ_FNS(expired);
271#undef CFQ_CFQQ_FNS
272
273enum cfq_rq_state_flags {
274 CFQ_CRQ_FLAG_in_flight = 0,
275 CFQ_CRQ_FLAG_in_driver,
276 CFQ_CRQ_FLAG_is_sync,
277 CFQ_CRQ_FLAG_requeued,
278};
279
280#define CFQ_CRQ_FNS(name) \
281static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
282{ \
283 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
284} \
285static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
286{ \
287 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
288} \
289static inline int cfq_crq_##name(const struct cfq_rq *crq) \
290{ \
291 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
292}
293
294CFQ_CRQ_FNS(in_flight);
295CFQ_CRQ_FNS(in_driver);
296CFQ_CRQ_FNS(is_sync);
297CFQ_CRQ_FNS(requeued);
298#undef CFQ_CRQ_FNS
299
300static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
243static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *); 301static void cfq_dispatch_sort(request_queue_t *, struct cfq_rq *);
244static void cfq_put_cfqd(struct cfq_data *cfqd); 302static void cfq_put_cfqd(struct cfq_data *cfqd);
303static inline int cfq_pending_requests(struct cfq_data *cfqd);
245 304
246#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE) 305#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
247 306
@@ -305,9 +364,9 @@ cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
305 return crq2; 364 return crq2;
306 if (crq2 == NULL) 365 if (crq2 == NULL)
307 return crq1; 366 return crq1;
308 if (crq1->requeued) 367 if (cfq_crq_requeued(crq1))
309 return crq1; 368 return crq1;
310 if (crq2->requeued) 369 if (cfq_crq_requeued(crq2))
311 return crq2; 370 return crq2;
312 371
313 s1 = crq1->request->sector; 372 s1 = crq1->request->sector;
@@ -407,7 +466,7 @@ static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
407 struct cfq_data *cfqd = cfqq->cfqd; 466 struct cfq_data *cfqd = cfqq->cfqd;
408 struct list_head *list, *entry; 467 struct list_head *list, *entry;
409 468
410 BUG_ON(!cfqq->on_rr); 469 BUG_ON(!cfq_cfqq_on_rr(cfqq));
411 470
412 list_del(&cfqq->cfq_list); 471 list_del(&cfqq->cfq_list);
413 472
@@ -423,7 +482,7 @@ static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
423 * has lots of io pending vs one that only generates one 482 * has lots of io pending vs one that only generates one
424 * sporadically or synchronously 483 * sporadically or synchronously
425 */ 484 */
426 if (cfqq->in_flight) 485 if (cfq_cfqq_dispatched(cfqq))
427 list = &cfqd->busy_rr; 486 list = &cfqd->busy_rr;
428 else 487 else
429 list = &cfqd->rr_list[cfqq->ioprio]; 488 list = &cfqd->rr_list[cfqq->ioprio];
@@ -461,8 +520,8 @@ static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
461static inline void 520static inline void
462cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue) 521cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
463{ 522{
464 BUG_ON(cfqq->on_rr); 523 BUG_ON(cfq_cfqq_on_rr(cfqq));
465 cfqq->on_rr = 1; 524 cfq_mark_cfqq_on_rr(cfqq);
466 cfqd->busy_queues++; 525 cfqd->busy_queues++;
467 526
468 cfq_resort_rr_list(cfqq, requeue); 527 cfq_resort_rr_list(cfqq, requeue);
@@ -471,8 +530,8 @@ cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq, int requeue)
471static inline void 530static inline void
472cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) 531cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
473{ 532{
474 BUG_ON(!cfqq->on_rr); 533 BUG_ON(!cfq_cfqq_on_rr(cfqq));
475 cfqq->on_rr = 0; 534 cfq_clear_cfqq_on_rr(cfqq);
476 list_move(&cfqq->cfq_list, &cfqd->empty_list); 535 list_move(&cfqq->cfq_list, &cfqd->empty_list);
477 536
478 BUG_ON(!cfqd->busy_queues); 537 BUG_ON(!cfqd->busy_queues);
@@ -488,7 +547,7 @@ static inline void cfq_del_crq_rb(struct cfq_rq *crq)
488 547
489 if (ON_RB(&crq->rb_node)) { 548 if (ON_RB(&crq->rb_node)) {
490 struct cfq_data *cfqd = cfqq->cfqd; 549 struct cfq_data *cfqd = cfqq->cfqd;
491 const int sync = crq->is_sync; 550 const int sync = cfq_crq_is_sync(crq);
492 551
493 BUG_ON(!cfqq->queued[sync]); 552 BUG_ON(!cfqq->queued[sync]);
494 cfqq->queued[sync]--; 553 cfqq->queued[sync]--;
@@ -498,7 +557,7 @@ static inline void cfq_del_crq_rb(struct cfq_rq *crq)
498 rb_erase(&crq->rb_node, &cfqq->sort_list); 557 rb_erase(&crq->rb_node, &cfqq->sort_list);
499 RB_CLEAR_COLOR(&crq->rb_node); 558 RB_CLEAR_COLOR(&crq->rb_node);
500 559
501 if (cfqq->on_rr && RB_EMPTY(&cfqq->sort_list)) 560 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
502 cfq_del_cfqq_rr(cfqd, cfqq); 561 cfq_del_cfqq_rr(cfqd, cfqq);
503 } 562 }
504} 563}
@@ -534,7 +593,7 @@ static void cfq_add_crq_rb(struct cfq_rq *crq)
534 struct cfq_rq *__alias; 593 struct cfq_rq *__alias;
535 594
536 crq->rb_key = rq_rb_key(rq); 595 crq->rb_key = rq_rb_key(rq);
537 cfqq->queued[crq->is_sync]++; 596 cfqq->queued[cfq_crq_is_sync(crq)]++;
538 597
539 /* 598 /*
540 * looks a little odd, but the first insert might return an alias. 599 * looks a little odd, but the first insert might return an alias.
@@ -545,8 +604,8 @@ static void cfq_add_crq_rb(struct cfq_rq *crq)
545 604
546 rb_insert_color(&crq->rb_node, &cfqq->sort_list); 605 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
547 606
548 if (!cfqq->on_rr) 607 if (!cfq_cfqq_on_rr(cfqq))
549 cfq_add_cfqq_rr(cfqd, cfqq, crq->requeued); 608 cfq_add_cfqq_rr(cfqd, cfqq, cfq_crq_requeued(crq));
550 609
551 /* 610 /*
552 * check if this request is a better next-serve candidate 611 * check if this request is a better next-serve candidate
@@ -559,7 +618,7 @@ cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
559{ 618{
560 if (ON_RB(&crq->rb_node)) { 619 if (ON_RB(&crq->rb_node)) {
561 rb_erase(&crq->rb_node, &cfqq->sort_list); 620 rb_erase(&crq->rb_node, &cfqq->sort_list);
562 cfqq->queued[crq->is_sync]--; 621 cfqq->queued[cfq_crq_is_sync(crq)]--;
563 } 622 }
564 623
565 cfq_add_crq_rb(crq); 624 cfq_add_crq_rb(crq);
@@ -568,7 +627,7 @@ cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
568static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector) 627static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
569 628
570{ 629{
571 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid); 630 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
572 struct rb_node *n; 631 struct rb_node *n;
573 632
574 if (!cfqq) 633 if (!cfqq)
@@ -598,17 +657,19 @@ static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
598 if (crq) { 657 if (crq) {
599 struct cfq_queue *cfqq = crq->cfq_queue; 658 struct cfq_queue *cfqq = crq->cfq_queue;
600 659
601 if (crq->accounted) { 660 if (cfq_crq_in_driver(crq)) {
602 crq->accounted = 0; 661 cfq_clear_crq_in_driver(crq);
603 WARN_ON(!cfqd->rq_in_driver); 662 WARN_ON(!cfqd->rq_in_driver);
604 cfqd->rq_in_driver--; 663 cfqd->rq_in_driver--;
605 } 664 }
606 if (crq->in_flight) { 665 if (cfq_crq_in_flight(crq)) {
607 crq->in_flight = 0; 666 const int sync = cfq_crq_is_sync(crq);
608 WARN_ON(!cfqq->in_flight); 667
609 cfqq->in_flight--; 668 cfq_clear_crq_in_flight(crq);
669 WARN_ON(!cfqq->on_dispatch[sync]);
670 cfqq->on_dispatch[sync]--;
610 } 671 }
611 crq->requeued = 1; 672 cfq_mark_crq_requeued(crq);
612 } 673 }
613} 674}
614 675
@@ -712,8 +773,9 @@ __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
712 cfqq->slice_start = jiffies; 773 cfqq->slice_start = jiffies;
713 cfqq->slice_end = 0; 774 cfqq->slice_end = 0;
714 cfqq->slice_left = 0; 775 cfqq->slice_left = 0;
715 cfqq->must_alloc_slice = 0; 776 cfq_clear_cfqq_must_alloc_slice(cfqq);
716 cfqq->fifo_expire = 0; 777 cfq_clear_cfqq_fifo_expire(cfqq);
778 cfq_clear_cfqq_expired(cfqq);
717 } 779 }
718 780
719 cfqd->active_queue = cfqq; 781 cfqd->active_queue = cfqq;
@@ -776,9 +838,18 @@ static int cfq_get_next_prio_level(struct cfq_data *cfqd)
776 return prio; 838 return prio;
777} 839}
778 840
779static void cfq_set_active_queue(struct cfq_data *cfqd) 841static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
780{ 842{
781 struct cfq_queue *cfqq = NULL; 843 struct cfq_queue *cfqq;
844
845 /*
846 * if current queue is expired but not done with its requests yet,
847 * wait for that to happen
848 */
849 if ((cfqq = cfqd->active_queue) != NULL) {
850 if (cfq_cfqq_expired(cfqq) && cfq_cfqq_dispatched(cfqq))
851 return NULL;
852 }
782 853
783 /* 854 /*
784 * if current list is non-empty, grab first entry. if it is empty, 855 * if current list is non-empty, grab first entry. if it is empty,
@@ -802,50 +873,66 @@ static void cfq_set_active_queue(struct cfq_data *cfqd)
802 } 873 }
803 874
804 __cfq_set_active_queue(cfqd, cfqq); 875 __cfq_set_active_queue(cfqd, cfqq);
876 return cfqq;
805} 877}
806 878
807/* 879/*
808 * current cfqq expired its slice (or was too idle), select new one 880 * current cfqq expired its slice (or was too idle), select new one
809 */ 881 */
810static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted) 882static void
883__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
884 int preempted)
811{ 885{
812 struct cfq_queue *cfqq = cfqd->active_queue; 886 unsigned long now = jiffies;
813
814 if (cfqq) {
815 unsigned long now = jiffies;
816 887
817 if (cfqq->wait_request) 888 if (cfq_cfqq_wait_request(cfqq))
818 del_timer(&cfqd->idle_slice_timer); 889 del_timer(&cfqd->idle_slice_timer);
819 890
820 if (!preempted && !cfqq->in_flight) 891 if (!preempted && !cfq_cfqq_dispatched(cfqq))
821 cfqq->service_last = now; 892 cfqq->service_last = now;
822 893
823 cfqq->must_dispatch = 0; 894 cfq_clear_cfqq_must_dispatch(cfqq);
824 cfqq->wait_request = 0; 895 cfq_clear_cfqq_wait_request(cfqq);
825 896
826 /* 897 /*
827 * store what was left of this slice, if the queue idled out 898 * store what was left of this slice, if the queue idled out
828 * or was preempted 899 * or was preempted
829 */ 900 */
830 if (time_after(now, cfqq->slice_end)) 901 if (time_after(now, cfqq->slice_end))
831 cfqq->slice_left = now - cfqq->slice_end; 902 cfqq->slice_left = now - cfqq->slice_end;
832 else 903 else
833 cfqq->slice_left = 0; 904 cfqq->slice_left = 0;
834 905
835 if (cfqq->on_rr) 906 if (cfq_cfqq_on_rr(cfqq))
836 cfq_resort_rr_list(cfqq, preempted); 907 cfq_resort_rr_list(cfqq, preempted);
837 908
909 if (cfqq == cfqd->active_queue)
838 cfqd->active_queue = NULL; 910 cfqd->active_queue = NULL;
839 911
840 if (cfqd->active_cic) { 912 if (cfqd->active_cic) {
841 put_io_context(cfqd->active_cic->ioc); 913 put_io_context(cfqd->active_cic->ioc);
842 cfqd->active_cic = NULL; 914 cfqd->active_cic = NULL;
843 }
844 } 915 }
845 916
846 cfqd->dispatch_slice = 0; 917 cfqd->dispatch_slice = 0;
847} 918}
848 919
920static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
921{
922 struct cfq_queue *cfqq = cfqd->active_queue;
923
924 if (cfqq) {
925 /*
926 * use deferred expiry, if there are requests in progress as
927 * not to disturb the slice of the next queue
928 */
929 if (cfq_cfqq_dispatched(cfqq))
930 cfq_mark_cfqq_expired(cfqq);
931 else
932 __cfq_slice_expired(cfqd, cfqq, preempted);
933 }
934}
935
849static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) 936static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
850 937
851{ 938{
@@ -857,7 +944,7 @@ static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
857 */ 944 */
858 if (!cfqd->cfq_slice_idle) 945 if (!cfqd->cfq_slice_idle)
859 return 0; 946 return 0;
860 if (!cfqq->idle_window) 947 if (!cfq_cfqq_idle_window(cfqq))
861 return 0; 948 return 0;
862 /* 949 /*
863 * task has exited, don't wait 950 * task has exited, don't wait
@@ -865,13 +952,13 @@ static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
865 if (cfqd->active_cic && !cfqd->active_cic->ioc->task) 952 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
866 return 0; 953 return 0;
867 954
868 cfqq->wait_request = 1; 955 cfq_mark_cfqq_must_dispatch(cfqq);
869 cfqq->must_alloc = 1; 956 cfq_mark_cfqq_wait_request(cfqq);
870 957
871 if (!timer_pending(&cfqd->idle_slice_timer)) { 958 if (!timer_pending(&cfqd->idle_slice_timer)) {
872 unsigned long slice_left = cfqq->slice_end - 1; 959 unsigned long slice_left = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
873 960
874 cfqd->idle_slice_timer.expires = min(jiffies + cfqd->cfq_slice_idle, slice_left); 961 cfqd->idle_slice_timer.expires = jiffies + slice_left;
875 add_timer(&cfqd->idle_slice_timer); 962 add_timer(&cfqd->idle_slice_timer);
876 } 963 }
877 964
@@ -901,7 +988,7 @@ static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
901 break; 988 break;
902 if (!blk_fs_request(__rq)) 989 if (!blk_fs_request(__rq))
903 break; 990 break;
904 if (__crq->requeued) 991 if (cfq_crq_requeued(__crq))
905 break; 992 break;
906 993
907 if (__rq->sector <= crq->request->sector) 994 if (__rq->sector <= crq->request->sector)
@@ -920,9 +1007,10 @@ static void cfq_dispatch_sort(request_queue_t *q, struct cfq_rq *crq)
920 cfq_del_crq_rb(crq); 1007 cfq_del_crq_rb(crq);
921 cfq_remove_merge_hints(q, crq); 1008 cfq_remove_merge_hints(q, crq);
922 1009
923 crq->in_flight = 1; 1010 cfq_mark_crq_in_flight(crq);
924 crq->requeued = 0; 1011 cfq_clear_crq_requeued(crq);
925 cfqq->in_flight++; 1012
1013 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
926 list_add_tail(&crq->request->queuelist, entry); 1014 list_add_tail(&crq->request->queuelist, entry);
927} 1015}
928 1016
@@ -935,16 +1023,16 @@ static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
935 struct request *rq; 1023 struct request *rq;
936 struct cfq_rq *crq; 1024 struct cfq_rq *crq;
937 1025
938 if (cfqq->fifo_expire) 1026 if (cfq_cfqq_fifo_expire(cfqq))
939 return NULL; 1027 return NULL;
940 1028
941 if (!list_empty(&cfqq->fifo)) { 1029 if (!list_empty(&cfqq->fifo)) {
942 int fifo = cfq_cfqq_sync(cfqq); 1030 int fifo = cfq_cfqq_class_sync(cfqq);
943 1031
944 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next)); 1032 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
945 rq = crq->request; 1033 rq = crq->request;
946 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) { 1034 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
947 cfqq->fifo_expire = 1; 1035 cfq_mark_cfqq_fifo_expire(cfqq);
948 return crq; 1036 return crq;
949 } 1037 }
950 } 1038 }
@@ -953,7 +1041,9 @@ static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
953} 1041}
954 1042
955/* 1043/*
956 * Scale schedule slice based on io priority 1044 * Scale schedule slice based on io priority. Use the sync time slice only
1045 * if a queue is marked sync and has sync io queued. A sync queue with async
1046 * io only, should not get full sync slice length.
957 */ 1047 */
958static inline int 1048static inline int
959cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) 1049cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
@@ -982,6 +1072,16 @@ cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
982} 1072}
983 1073
984/* 1074/*
1075 * scheduler run of queue, if there are requests pending and no one in the
1076 * driver that will restart queueing
1077 */
1078static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
1079{
1080 if (!cfqd->rq_in_driver && cfq_pending_requests(cfqd))
1081 kblockd_schedule_work(&cfqd->unplug_work);
1082}
1083
1084/*
985 * get next queue for service 1085 * get next queue for service
986 */ 1086 */
987static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force) 1087static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
@@ -993,11 +1093,14 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
993 if (!cfqq) 1093 if (!cfqq)
994 goto new_queue; 1094 goto new_queue;
995 1095
1096 if (cfq_cfqq_expired(cfqq))
1097 goto new_queue;
1098
996 /* 1099 /*
997 * slice has expired 1100 * slice has expired
998 */ 1101 */
999 if (!cfqq->must_dispatch && time_after(jiffies, cfqq->slice_end)) 1102 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
1000 goto new_queue; 1103 goto expire;
1001 1104
1002 /* 1105 /*
1003 * if queue has requests, dispatch one. if not, check if 1106 * if queue has requests, dispatch one. if not, check if
@@ -1005,17 +1108,18 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd, int force)
1005 */ 1108 */
1006 if (!RB_EMPTY(&cfqq->sort_list)) 1109 if (!RB_EMPTY(&cfqq->sort_list))
1007 goto keep_queue; 1110 goto keep_queue;
1008 else if (!force && cfq_cfqq_sync(cfqq) && 1111 else if (!force && cfq_cfqq_class_sync(cfqq) &&
1009 time_before(now, cfqq->slice_end)) { 1112 time_before(now, cfqq->slice_end)) {
1010 if (cfq_arm_slice_timer(cfqd, cfqq)) 1113 if (cfq_arm_slice_timer(cfqd, cfqq))
1011 return NULL; 1114 return NULL;
1012 } 1115 }
1013 1116
1014new_queue: 1117expire:
1015 cfq_slice_expired(cfqd, 0); 1118 cfq_slice_expired(cfqd, 0);
1016 cfq_set_active_queue(cfqd); 1119new_queue:
1120 cfqq = cfq_set_active_queue(cfqd);
1017keep_queue: 1121keep_queue:
1018 return cfqd->active_queue; 1122 return cfqq;
1019} 1123}
1020 1124
1021static int 1125static int
@@ -1083,8 +1187,8 @@ cfq_dispatch_requests(request_queue_t *q, int max_dispatch, int force)
1083 1187
1084 cfqq = cfq_select_queue(cfqd, force); 1188 cfqq = cfq_select_queue(cfqd, force);
1085 if (cfqq) { 1189 if (cfqq) {
1086 cfqq->wait_request = 0; 1190 cfq_clear_cfqq_must_dispatch(cfqq);
1087 cfqq->must_dispatch = 0; 1191 cfq_clear_cfqq_wait_request(cfqq);
1088 del_timer(&cfqd->idle_slice_timer); 1192 del_timer(&cfqd->idle_slice_timer);
1089 1193
1090 if (cfq_class_idle(cfqq)) 1194 if (cfq_class_idle(cfqq))
@@ -1108,10 +1212,10 @@ static inline void cfq_account_dispatch(struct cfq_rq *crq)
1108 * accounted bit is necessary since some drivers will call 1212 * accounted bit is necessary since some drivers will call
1109 * elv_next_request() many times for the same request (eg ide) 1213 * elv_next_request() many times for the same request (eg ide)
1110 */ 1214 */
1111 if (crq->accounted) 1215 if (cfq_crq_in_driver(crq))
1112 return; 1216 return;
1113 1217
1114 crq->accounted = 1; 1218 cfq_mark_crq_in_driver(crq);
1115 cfqd->rq_in_driver++; 1219 cfqd->rq_in_driver++;
1116} 1220}
1117 1221
@@ -1121,7 +1225,7 @@ cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1121 struct cfq_data *cfqd = cfqq->cfqd; 1225 struct cfq_data *cfqd = cfqq->cfqd;
1122 unsigned long now; 1226 unsigned long now;
1123 1227
1124 if (!crq->accounted) 1228 if (!cfq_crq_in_driver(crq))
1125 return; 1229 return;
1126 1230
1127 now = jiffies; 1231 now = jiffies;
@@ -1132,12 +1236,18 @@ cfq_account_completion(struct cfq_queue *cfqq, struct cfq_rq *crq)
1132 if (!cfq_class_idle(cfqq)) 1236 if (!cfq_class_idle(cfqq))
1133 cfqd->last_end_request = now; 1237 cfqd->last_end_request = now;
1134 1238
1135 if (!cfqq->in_flight && cfqq->on_rr) { 1239 if (!cfq_cfqq_dispatched(cfqq)) {
1136 cfqq->service_last = now; 1240 if (cfq_cfqq_on_rr(cfqq)) {
1137 cfq_resort_rr_list(cfqq, 0); 1241 cfqq->service_last = now;
1242 cfq_resort_rr_list(cfqq, 0);
1243 }
1244 if (cfq_cfqq_expired(cfqq)) {
1245 __cfq_slice_expired(cfqd, cfqq, 0);
1246 cfq_schedule_dispatch(cfqd);
1247 }
1138 } 1248 }
1139 1249
1140 if (crq->is_sync) 1250 if (cfq_crq_is_sync(crq))
1141 crq->io_context->last_end_request = now; 1251 crq->io_context->last_end_request = now;
1142} 1252}
1143 1253
@@ -1153,10 +1263,13 @@ dispatch:
1153 1263
1154 crq = RQ_DATA(rq); 1264 crq = RQ_DATA(rq);
1155 if (crq) { 1265 if (crq) {
1266 struct cfq_queue *cfqq = crq->cfq_queue;
1267
1156 /* 1268 /*
1157 * if idle window is disabled, allow queue buildup 1269 * if idle window is disabled, allow queue buildup
1158 */ 1270 */
1159 if (!crq->in_flight && !crq->cfq_queue->idle_window && 1271 if (!cfq_crq_in_driver(crq) &&
1272 !cfq_cfqq_idle_window(cfqq) &&
1160 cfqd->rq_in_driver >= cfqd->cfq_max_depth) 1273 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1161 return NULL; 1274 return NULL;
1162 1275
@@ -1190,11 +1303,11 @@ static void cfq_put_queue(struct cfq_queue *cfqq)
1190 1303
1191 BUG_ON(rb_first(&cfqq->sort_list)); 1304 BUG_ON(rb_first(&cfqq->sort_list));
1192 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); 1305 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
1193 BUG_ON(cfqq->on_rr); 1306 BUG_ON(cfq_cfqq_on_rr(cfqq));
1194 1307
1195 if (unlikely(cfqd->active_queue == cfqq)) { 1308 if (unlikely(cfqd->active_queue == cfqq)) {
1196 cfq_slice_expired(cfqd, 0); 1309 __cfq_slice_expired(cfqd, cfqq, 0);
1197 kblockd_schedule_work(&cfqd->unplug_work); 1310 cfq_schedule_dispatch(cfqd);
1198 } 1311 }
1199 1312
1200 cfq_put_cfqd(cfqq->cfqd); 1313 cfq_put_cfqd(cfqq->cfqd);
@@ -1208,15 +1321,17 @@ static void cfq_put_queue(struct cfq_queue *cfqq)
1208} 1321}
1209 1322
1210static inline struct cfq_queue * 1323static inline struct cfq_queue *
1211__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, const int hashval) 1324__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1325 const int hashval)
1212{ 1326{
1213 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval]; 1327 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1214 struct hlist_node *entry, *next; 1328 struct hlist_node *entry, *next;
1215 1329
1216 hlist_for_each_safe(entry, next, hash_list) { 1330 hlist_for_each_safe(entry, next, hash_list) {
1217 struct cfq_queue *__cfqq = list_entry_qhash(entry); 1331 struct cfq_queue *__cfqq = list_entry_qhash(entry);
1332 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->ioprio_class, __cfqq->ioprio);
1218 1333
1219 if (__cfqq->key == key) 1334 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
1220 return __cfqq; 1335 return __cfqq;
1221 } 1336 }
1222 1337
@@ -1224,9 +1339,9 @@ __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, const int hashval)
1224} 1339}
1225 1340
1226static struct cfq_queue * 1341static struct cfq_queue *
1227cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key) 1342cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
1228{ 1343{
1229 return __cfq_find_cfq_hash(cfqd, key, hash_long(key, CFQ_QHASH_SHIFT)); 1344 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
1230} 1345}
1231 1346
1232static void cfq_free_io_context(struct cfq_io_context *cic) 1347static void cfq_free_io_context(struct cfq_io_context *cic)
@@ -1255,8 +1370,8 @@ static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1255 spin_lock(q->queue_lock); 1370 spin_lock(q->queue_lock);
1256 1371
1257 if (unlikely(cic->cfqq == cfqd->active_queue)) { 1372 if (unlikely(cic->cfqq == cfqd->active_queue)) {
1258 cfq_slice_expired(cfqd, 0); 1373 __cfq_slice_expired(cfqd, cic->cfqq, 0);
1259 kblockd_schedule_work(&cfqd->unplug_work); 1374 cfq_schedule_dispatch(cfqd);
1260 } 1375 }
1261 1376
1262 cfq_put_queue(cic->cfqq); 1377 cfq_put_queue(cic->cfqq);
@@ -1313,7 +1428,7 @@ static void cfq_init_prio_data(struct cfq_queue *cfqq)
1313 struct task_struct *tsk = current; 1428 struct task_struct *tsk = current;
1314 int ioprio_class; 1429 int ioprio_class;
1315 1430
1316 if (!cfqq->prio_changed) 1431 if (!cfq_cfqq_prio_changed(cfqq))
1317 return; 1432 return;
1318 1433
1319 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio); 1434 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
@@ -1338,7 +1453,7 @@ static void cfq_init_prio_data(struct cfq_queue *cfqq)
1338 case IOPRIO_CLASS_IDLE: 1453 case IOPRIO_CLASS_IDLE:
1339 cfqq->ioprio_class = IOPRIO_CLASS_IDLE; 1454 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1340 cfqq->ioprio = 7; 1455 cfqq->ioprio = 7;
1341 cfqq->idle_window = 0; 1456 cfq_clear_cfqq_idle_window(cfqq);
1342 break; 1457 break;
1343 } 1458 }
1344 1459
@@ -1349,10 +1464,10 @@ static void cfq_init_prio_data(struct cfq_queue *cfqq)
1349 cfqq->org_ioprio = cfqq->ioprio; 1464 cfqq->org_ioprio = cfqq->ioprio;
1350 cfqq->org_ioprio_class = cfqq->ioprio_class; 1465 cfqq->org_ioprio_class = cfqq->ioprio_class;
1351 1466
1352 if (cfqq->on_rr) 1467 if (cfq_cfqq_on_rr(cfqq))
1353 cfq_resort_rr_list(cfqq, 0); 1468 cfq_resort_rr_list(cfqq, 0);
1354 1469
1355 cfqq->prio_changed = 0; 1470 cfq_clear_cfqq_prio_changed(cfqq);
1356} 1471}
1357 1472
1358static inline void changed_ioprio(struct cfq_queue *cfqq) 1473static inline void changed_ioprio(struct cfq_queue *cfqq)
@@ -1361,7 +1476,7 @@ static inline void changed_ioprio(struct cfq_queue *cfqq)
1361 struct cfq_data *cfqd = cfqq->cfqd; 1476 struct cfq_data *cfqd = cfqq->cfqd;
1362 1477
1363 spin_lock(cfqd->queue->queue_lock); 1478 spin_lock(cfqd->queue->queue_lock);
1364 cfqq->prio_changed = 1; 1479 cfq_mark_cfqq_prio_changed(cfqq);
1365 cfq_init_prio_data(cfqq); 1480 cfq_init_prio_data(cfqq);
1366 spin_unlock(cfqd->queue->queue_lock); 1481 spin_unlock(cfqd->queue->queue_lock);
1367 } 1482 }
@@ -1383,13 +1498,14 @@ static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
1383} 1498}
1384 1499
1385static struct cfq_queue * 1500static struct cfq_queue *
1386cfq_get_queue(struct cfq_data *cfqd, unsigned int key, int gfp_mask) 1501cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
1502 int gfp_mask)
1387{ 1503{
1388 const int hashval = hash_long(key, CFQ_QHASH_SHIFT); 1504 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1389 struct cfq_queue *cfqq, *new_cfqq = NULL; 1505 struct cfq_queue *cfqq, *new_cfqq = NULL;
1390 1506
1391retry: 1507retry:
1392 cfqq = __cfq_find_cfq_hash(cfqd, key, hashval); 1508 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
1393 1509
1394 if (!cfqq) { 1510 if (!cfqq) {
1395 if (new_cfqq) { 1511 if (new_cfqq) {
@@ -1423,10 +1539,9 @@ retry:
1423 * set ->slice_left to allow preemption for a new process 1539 * set ->slice_left to allow preemption for a new process
1424 */ 1540 */
1425 cfqq->slice_left = 2 * cfqd->cfq_slice_idle; 1541 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
1426 cfqq->idle_window = 1; 1542 cfq_mark_cfqq_idle_window(cfqq);
1427 cfqq->ioprio = -1; 1543 cfq_mark_cfqq_prio_changed(cfqq);
1428 cfqq->ioprio_class = -1; 1544 cfq_init_prio_data(cfqq);
1429 cfqq->prio_changed = 1;
1430 } 1545 }
1431 1546
1432 if (new_cfqq) 1547 if (new_cfqq)
@@ -1553,7 +1668,7 @@ static void
1553cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, 1668cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1554 struct cfq_io_context *cic) 1669 struct cfq_io_context *cic)
1555{ 1670{
1556 int enable_idle = cfqq->idle_window; 1671 int enable_idle = cfq_cfqq_idle_window(cfqq);
1557 1672
1558 if (!cic->ioc->task || !cfqd->cfq_slice_idle) 1673 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1559 enable_idle = 0; 1674 enable_idle = 0;
@@ -1564,7 +1679,10 @@ cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1564 enable_idle = 1; 1679 enable_idle = 1;
1565 } 1680 }
1566 1681
1567 cfqq->idle_window = enable_idle; 1682 if (enable_idle)
1683 cfq_mark_cfqq_idle_window(cfqq);
1684 else
1685 cfq_clear_cfqq_idle_window(cfqq);
1568} 1686}
1569 1687
1570 1688
@@ -1586,14 +1704,14 @@ cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1586 1704
1587 if (cfq_class_idle(cfqq)) 1705 if (cfq_class_idle(cfqq))
1588 return 1; 1706 return 1;
1589 if (!new_cfqq->wait_request) 1707 if (!cfq_cfqq_wait_request(new_cfqq))
1590 return 0; 1708 return 0;
1591 /* 1709 /*
1592 * if it doesn't have slice left, forget it 1710 * if it doesn't have slice left, forget it
1593 */ 1711 */
1594 if (new_cfqq->slice_left < cfqd->cfq_slice_idle) 1712 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1595 return 0; 1713 return 0;
1596 if (crq->is_sync && !cfq_cfqq_sync(cfqq)) 1714 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
1597 return 1; 1715 return 1;
1598 1716
1599 return 0; 1717 return 0;
@@ -1614,7 +1732,7 @@ static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1614 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2; 1732 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1615 1733
1616 cfqq->slice_end = cfqq->slice_left + jiffies; 1734 cfqq->slice_end = cfqq->slice_left + jiffies;
1617 cfq_slice_expired(cfqd, 1); 1735 __cfq_slice_expired(cfqd, cfqq, 1);
1618 __cfq_set_active_queue(cfqd, cfqq); 1736 __cfq_set_active_queue(cfqd, cfqq);
1619} 1737}
1620 1738
@@ -1639,7 +1757,7 @@ static void
1639cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, 1757cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1640 struct cfq_rq *crq) 1758 struct cfq_rq *crq)
1641{ 1759{
1642 const int sync = crq->is_sync; 1760 const int sync = cfq_crq_is_sync(crq);
1643 1761
1644 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq); 1762 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1645 1763
@@ -1658,8 +1776,8 @@ cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1658 * immediately and flag that we must not expire this queue 1776 * immediately and flag that we must not expire this queue
1659 * just now 1777 * just now
1660 */ 1778 */
1661 if (cfqq->wait_request) { 1779 if (cfq_cfqq_wait_request(cfqq)) {
1662 cfqq->must_dispatch = 1; 1780 cfq_mark_cfqq_must_dispatch(cfqq);
1663 del_timer(&cfqd->idle_slice_timer); 1781 del_timer(&cfqd->idle_slice_timer);
1664 cfq_start_queueing(cfqd, cfqq); 1782 cfq_start_queueing(cfqd, cfqq);
1665 } 1783 }
@@ -1670,7 +1788,7 @@ cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1670 * has some old slice time left and is of higher priority 1788 * has some old slice time left and is of higher priority
1671 */ 1789 */
1672 cfq_preempt_queue(cfqd, cfqq); 1790 cfq_preempt_queue(cfqd, cfqq);
1673 cfqq->must_dispatch = 1; 1791 cfq_mark_cfqq_must_dispatch(cfqq);
1674 cfq_start_queueing(cfqd, cfqq); 1792 cfq_start_queueing(cfqd, cfqq);
1675 } 1793 }
1676} 1794}
@@ -1713,7 +1831,7 @@ cfq_insert_request(request_queue_t *q, struct request *rq, int where)
1713 * be kicked by __make_request() afterward. 1831 * be kicked by __make_request() afterward.
1714 * Kick it here. 1832 * Kick it here.
1715 */ 1833 */
1716 kblockd_schedule_work(&cfqd->unplug_work); 1834 cfq_schedule_dispatch(cfqd);
1717 break; 1835 break;
1718 case ELEVATOR_INSERT_FRONT: 1836 case ELEVATOR_INSERT_FRONT:
1719 list_add(&rq->queuelist, &q->queue_head); 1837 list_add(&rq->queuelist, &q->queue_head);
@@ -1750,9 +1868,11 @@ static void cfq_completed_request(request_queue_t *q, struct request *rq)
1750 1868
1751 cfqq = crq->cfq_queue; 1869 cfqq = crq->cfq_queue;
1752 1870
1753 if (crq->in_flight) { 1871 if (cfq_crq_in_flight(crq)) {
1754 WARN_ON(!cfqq->in_flight); 1872 const int sync = cfq_crq_is_sync(crq);
1755 cfqq->in_flight--; 1873
1874 WARN_ON(!cfqq->on_dispatch[sync]);
1875 cfqq->on_dispatch[sync]--;
1756 } 1876 }
1757 1877
1758 cfq_account_completion(cfqq, crq); 1878 cfq_account_completion(cfqq, crq);
@@ -1814,7 +1934,7 @@ static void cfq_prio_boost(struct cfq_queue *cfqq)
1814 * refile between round-robin lists if we moved the priority class 1934 * refile between round-robin lists if we moved the priority class
1815 */ 1935 */
1816 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) && 1936 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
1817 cfqq->on_rr) 1937 cfq_cfqq_on_rr(cfqq))
1818 cfq_resort_rr_list(cfqq, 0); 1938 cfq_resort_rr_list(cfqq, 0);
1819} 1939}
1820 1940
@@ -1830,23 +1950,27 @@ static inline int
1830__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq, 1950__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1831 struct task_struct *task, int rw) 1951 struct task_struct *task, int rw)
1832{ 1952{
1833 if (cfqq->wait_request && cfqq->must_alloc) 1953#if 1
1954 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
1955 !cfq_cfqq_must_alloc_slice) {
1956 cfq_mark_cfqq_must_alloc_slice(cfqq);
1834 return ELV_MQUEUE_MUST; 1957 return ELV_MQUEUE_MUST;
1958 }
1835 1959
1836 return ELV_MQUEUE_MAY; 1960 return ELV_MQUEUE_MAY;
1837#if 0 1961#else
1838 if (!cfqq || task->flags & PF_MEMALLOC) 1962 if (!cfqq || task->flags & PF_MEMALLOC)
1839 return ELV_MQUEUE_MAY; 1963 return ELV_MQUEUE_MAY;
1840 if (!cfqq->allocated[rw] || cfqq->must_alloc) { 1964 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1841 if (cfqq->wait_request) 1965 if (cfq_cfqq_wait_request(cfqq))
1842 return ELV_MQUEUE_MUST; 1966 return ELV_MQUEUE_MUST;
1843 1967
1844 /* 1968 /*
1845 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we 1969 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1846 * can quickly flood the queue with writes from a single task 1970 * can quickly flood the queue with writes from a single task
1847 */ 1971 */
1848 if (rw == READ || !cfqq->must_alloc_slice) { 1972 if (rw == READ || !cfq_cfqq_must_alloc_slice) {
1849 cfqq->must_alloc_slice = 1; 1973 cfq_mark_cfqq_must_alloc_slice(cfqq);
1850 return ELV_MQUEUE_MUST; 1974 return ELV_MQUEUE_MUST;
1851 } 1975 }
1852 1976
@@ -1881,7 +2005,7 @@ static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1881 * so just lookup a possibly existing queue, or return 'may queue' 2005 * so just lookup a possibly existing queue, or return 'may queue'
1882 * if that fails 2006 * if that fails
1883 */ 2007 */
1884 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw)); 2008 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
1885 if (cfqq) { 2009 if (cfqq) {
1886 cfq_init_prio_data(cfqq); 2010 cfq_init_prio_data(cfqq);
1887 cfq_prio_boost(cfqq); 2011 cfq_prio_boost(cfqq);
@@ -1943,15 +2067,17 @@ cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1943 int gfp_mask) 2067 int gfp_mask)
1944{ 2068{
1945 struct cfq_data *cfqd = q->elevator->elevator_data; 2069 struct cfq_data *cfqd = q->elevator->elevator_data;
2070 struct task_struct *tsk = current;
1946 struct cfq_io_context *cic; 2071 struct cfq_io_context *cic;
1947 const int rw = rq_data_dir(rq); 2072 const int rw = rq_data_dir(rq);
2073 pid_t key = cfq_queue_pid(tsk, rw);
1948 struct cfq_queue *cfqq; 2074 struct cfq_queue *cfqq;
1949 struct cfq_rq *crq; 2075 struct cfq_rq *crq;
1950 unsigned long flags; 2076 unsigned long flags;
1951 2077
1952 might_sleep_if(gfp_mask & __GFP_WAIT); 2078 might_sleep_if(gfp_mask & __GFP_WAIT);
1953 2079
1954 cic = cfq_get_io_context(cfqd, cfq_queue_pid(current, rw), gfp_mask); 2080 cic = cfq_get_io_context(cfqd, key, gfp_mask);
1955 2081
1956 spin_lock_irqsave(q->queue_lock, flags); 2082 spin_lock_irqsave(q->queue_lock, flags);
1957 2083
@@ -1959,7 +2085,7 @@ cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1959 goto queue_fail; 2085 goto queue_fail;
1960 2086
1961 if (!cic->cfqq) { 2087 if (!cic->cfqq) {
1962 cfqq = cfq_get_queue(cfqd, current->pid, gfp_mask); 2088 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
1963 if (!cfqq) 2089 if (!cfqq)
1964 goto queue_fail; 2090 goto queue_fail;
1965 2091
@@ -1968,7 +2094,7 @@ cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1968 cfqq = cic->cfqq; 2094 cfqq = cic->cfqq;
1969 2095
1970 cfqq->allocated[rw]++; 2096 cfqq->allocated[rw]++;
1971 cfqq->must_alloc = 0; 2097 cfq_clear_cfqq_must_alloc(cfqq);
1972 cfqd->rq_starved = 0; 2098 cfqd->rq_starved = 0;
1973 atomic_inc(&cfqq->ref); 2099 atomic_inc(&cfqq->ref);
1974 spin_unlock_irqrestore(q->queue_lock, flags); 2100 spin_unlock_irqrestore(q->queue_lock, flags);
@@ -1981,9 +2107,15 @@ cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1981 INIT_HLIST_NODE(&crq->hash); 2107 INIT_HLIST_NODE(&crq->hash);
1982 crq->cfq_queue = cfqq; 2108 crq->cfq_queue = cfqq;
1983 crq->io_context = cic; 2109 crq->io_context = cic;
1984 crq->in_flight = crq->accounted = 0; 2110 cfq_clear_crq_in_flight(crq);
1985 crq->is_sync = (rw == READ || process_sync(current)); 2111 cfq_clear_crq_in_driver(crq);
1986 crq->requeued = 0; 2112 cfq_clear_crq_requeued(crq);
2113
2114 if (rw == READ || process_sync(tsk))
2115 cfq_mark_crq_is_sync(crq);
2116 else
2117 cfq_clear_crq_is_sync(crq);
2118
1987 rq->elevator_private = crq; 2119 rq->elevator_private = crq;
1988 return 0; 2120 return 0;
1989 } 2121 }
@@ -1991,7 +2123,7 @@ cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
1991 spin_lock_irqsave(q->queue_lock, flags); 2123 spin_lock_irqsave(q->queue_lock, flags);
1992 cfqq->allocated[rw]--; 2124 cfqq->allocated[rw]--;
1993 if (!(cfqq->allocated[0] + cfqq->allocated[1])) 2125 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
1994 cfqq->must_alloc = 1; 2126 cfq_mark_cfqq_must_alloc(cfqq);
1995 cfq_put_queue(cfqq); 2127 cfq_put_queue(cfqq);
1996queue_fail: 2128queue_fail:
1997 if (cic) 2129 if (cic)
@@ -2002,7 +2134,7 @@ queue_fail:
2002 * that would be an extremely rare OOM situation 2134 * that would be an extremely rare OOM situation
2003 */ 2135 */
2004 cfqd->rq_starved = 1; 2136 cfqd->rq_starved = 1;
2005 kblockd_schedule_work(&cfqd->unplug_work); 2137 cfq_schedule_dispatch(cfqd);
2006 spin_unlock_irqrestore(q->queue_lock, flags); 2138 spin_unlock_irqrestore(q->queue_lock, flags);
2007 return 1; 2139 return 1;
2008} 2140}
@@ -2068,15 +2200,14 @@ static void cfq_idle_slice_timer(unsigned long data)
2068 * not expired and it has a request pending, let it dispatch 2200 * not expired and it has a request pending, let it dispatch
2069 */ 2201 */
2070 if (!RB_EMPTY(&cfqq->sort_list)) { 2202 if (!RB_EMPTY(&cfqq->sort_list)) {
2071 cfqq->must_dispatch = 1; 2203 cfq_mark_cfqq_must_dispatch(cfqq);
2072 goto out_kick; 2204 goto out_kick;
2073 } 2205 }
2074 } 2206 }
2075expire: 2207expire:
2076 cfq_slice_expired(cfqd, 0); 2208 cfq_slice_expired(cfqd, 0);
2077out_kick: 2209out_kick:
2078 if (cfq_pending_requests(cfqd)) 2210 cfq_schedule_dispatch(cfqd);
2079 kblockd_schedule_work(&cfqd->unplug_work);
2080out_cont: 2211out_cont:
2081 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); 2212 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2082} 2213}
@@ -2099,11 +2230,17 @@ static void cfq_idle_class_timer(unsigned long data)
2099 cfqd->idle_class_timer.expires = end; 2230 cfqd->idle_class_timer.expires = end;
2100 add_timer(&cfqd->idle_class_timer); 2231 add_timer(&cfqd->idle_class_timer);
2101 } else 2232 } else
2102 kblockd_schedule_work(&cfqd->unplug_work); 2233 cfq_schedule_dispatch(cfqd);
2103 2234
2104 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); 2235 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2105} 2236}
2106 2237
2238static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2239{
2240 del_timer_sync(&cfqd->idle_slice_timer);
2241 del_timer_sync(&cfqd->idle_class_timer);
2242 blk_sync_queue(cfqd->queue);
2243}
2107 2244
2108static void cfq_put_cfqd(struct cfq_data *cfqd) 2245static void cfq_put_cfqd(struct cfq_data *cfqd)
2109{ 2246{
@@ -2112,7 +2249,7 @@ static void cfq_put_cfqd(struct cfq_data *cfqd)
2112 if (!atomic_dec_and_test(&cfqd->ref)) 2249 if (!atomic_dec_and_test(&cfqd->ref))
2113 return; 2250 return;
2114 2251
2115 blk_sync_queue(q); 2252 cfq_shutdown_timer_wq(cfqd);
2116 2253
2117 blk_put_queue(q); 2254 blk_put_queue(q);
2118 2255
@@ -2126,8 +2263,7 @@ static void cfq_exit_queue(elevator_t *e)
2126{ 2263{
2127 struct cfq_data *cfqd = e->elevator_data; 2264 struct cfq_data *cfqd = e->elevator_data;
2128 2265
2129 del_timer_sync(&cfqd->idle_slice_timer); 2266 cfq_shutdown_timer_wq(cfqd);
2130 del_timer_sync(&cfqd->idle_class_timer);
2131 cfq_put_cfqd(cfqd); 2267 cfq_put_cfqd(cfqd);
2132} 2268}
2133 2269
@@ -2198,6 +2334,7 @@ static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2198 cfqd->cfq_slice_async_rq = cfq_slice_async_rq; 2334 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2199 cfqd->cfq_slice_idle = cfq_slice_idle; 2335 cfqd->cfq_slice_idle = cfq_slice_idle;
2200 cfqd->cfq_max_depth = cfq_max_depth; 2336 cfqd->cfq_max_depth = cfq_max_depth;
2337
2201 return 0; 2338 return 0;
2202out_crqpool: 2339out_crqpool:
2203 kfree(cfqd->cfq_hash); 2340 kfree(cfqd->cfq_hash);
@@ -2369,6 +2506,7 @@ static struct cfq_fs_entry cfq_max_depth_entry = {
2369 .show = cfq_max_depth_show, 2506 .show = cfq_max_depth_show,
2370 .store = cfq_max_depth_store, 2507 .store = cfq_max_depth_store,
2371}; 2508};
2509
2372static struct attribute *default_attrs[] = { 2510static struct attribute *default_attrs[] = {
2373 &cfq_quantum_entry.attr, 2511 &cfq_quantum_entry.attr,
2374 &cfq_queued_entry.attr, 2512 &cfq_queued_entry.attr,