1
2
3
4
5
6
7
8#include <linux/kernel.h>
9#include <linux/fs.h>
10#include <linux/blkdev.h>
11#include <linux/elevator.h>
12#include <linux/bio.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/compiler.h>
17#include <linux/rbtree.h>
18#include <linux/interrupt.h>
19
20#define REQ_SYNC 1
21#define REQ_ASYNC 0
22
23
24
25
26
27
28
29
30#define default_read_expire (HZ / 8)
31
32
33
34
35
36#define default_write_expire (HZ / 4)
37
38
39
40
41
42#define default_read_batch_expire (HZ / 2)
43
44
45
46
47
48
49
50#define default_write_batch_expire (HZ / 8)
51
52
53
54
55#define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
56
57
58
59
60
61
62
63
64#define MAX_THINKTIME (HZ/50UL)
65
66
67enum as_io_states {
68 AS_TASK_RUNNING=0,
69 AS_TASK_IOSTARTED,
70 AS_TASK_IORUNNING,
71};
72
73enum anticipation_status {
74 ANTIC_OFF=0,
75 ANTIC_WAIT_REQ,
76 ANTIC_WAIT_NEXT,
77
78 ANTIC_FINISHED,
79
80};
81
82struct as_data {
83
84
85
86
87 struct request_queue *q;
88
89
90
91
92 struct rb_root sort_list[2];
93 struct list_head fifo_list[2];
94
95 struct request *next_rq[2];
96 sector_t last_sector[2];
97
98 unsigned long exit_prob;
99
100 unsigned long exit_no_coop;
101
102
103 unsigned long new_ttime_total;
104 unsigned long new_ttime_mean;
105 u64 new_seek_total;
106 sector_t new_seek_mean;
107
108 unsigned long current_batch_expires;
109 unsigned long last_check_fifo[2];
110 int changed_batch;
111 int new_batch;
112 int batch_data_dir;
113 int write_batch_count;
114 int current_write_count;
115 int write_batch_idled;
116
117 enum anticipation_status antic_status;
118 unsigned long antic_start;
119 struct timer_list antic_timer;
120 struct work_struct antic_work;
121 struct io_context *io_context;
122 int ioc_finished;
123 int nr_dispatched;
124
125
126
127
128 unsigned long fifo_expire[2];
129 unsigned long batch_expire[2];
130 unsigned long antic_expire;
131};
132
133
134
135
136enum arq_state {
137 AS_RQ_NEW=0,
138 AS_RQ_QUEUED,
139
140 AS_RQ_DISPATCHED,
141
142 AS_RQ_PRESCHED,
143 AS_RQ_REMOVED,
144 AS_RQ_MERGED,
145 AS_RQ_POSTSCHED,
146};
147
148#define RQ_IOC(rq) ((struct io_context *) (rq)->elevator_private)
149#define RQ_STATE(rq) ((enum arq_state)(rq)->elevator_private2)
150#define RQ_SET_STATE(rq, state) ((rq)->elevator_private2 = (void *) state)
151
152static DEFINE_PER_CPU(unsigned long, ioc_count);
153static struct completion *ioc_gone;
154static DEFINE_SPINLOCK(ioc_gone_lock);
155
156static void as_move_to_dispatch(struct as_data *ad, struct request *rq);
157static void as_antic_stop(struct as_data *ad);
158
159
160
161
162
163
164static void free_as_io_context(struct as_io_context *aic)
165{
166 kfree(aic);
167 elv_ioc_count_dec(ioc_count);
168 if (ioc_gone) {
169
170
171
172
173
174 spin_lock(&ioc_gone_lock);
175 if (ioc_gone && !elv_ioc_count_read(ioc_count)) {
176 complete(ioc_gone);
177 ioc_gone = NULL;
178 }
179 spin_unlock(&ioc_gone_lock);
180 }
181}
182
183static void as_trim(struct io_context *ioc)
184{
185 spin_lock_irq(&ioc->lock);
186 if (ioc->aic)
187 free_as_io_context(ioc->aic);
188 ioc->aic = NULL;
189 spin_unlock_irq(&ioc->lock);
190}
191
192
193static void exit_as_io_context(struct as_io_context *aic)
194{
195 WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
196 clear_bit(AS_TASK_RUNNING, &aic->state);
197}
198
199static struct as_io_context *alloc_as_io_context(void)
200{
201 struct as_io_context *ret;
202
203 ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
204 if (ret) {
205 ret->dtor = free_as_io_context;
206 ret->exit = exit_as_io_context;
207 ret->state = 1 << AS_TASK_RUNNING;
208 atomic_set(&ret->nr_queued, 0);
209 atomic_set(&ret->nr_dispatched, 0);
210 spin_lock_init(&ret->lock);
211 ret->ttime_total = 0;
212 ret->ttime_samples = 0;
213 ret->ttime_mean = 0;
214 ret->seek_total = 0;
215 ret->seek_samples = 0;
216 ret->seek_mean = 0;
217 elv_ioc_count_inc(ioc_count);
218 }
219
220 return ret;
221}
222
223
224
225
226
227static struct io_context *as_get_io_context(int node)
228{
229 struct io_context *ioc = get_io_context(GFP_ATOMIC, node);
230 if (ioc && !ioc->aic) {
231 ioc->aic = alloc_as_io_context();
232 if (!ioc->aic) {
233 put_io_context(ioc);
234 ioc = NULL;
235 }
236 }
237 return ioc;
238}
239
240static void as_put_io_context(struct request *rq)
241{
242 struct as_io_context *aic;
243
244 if (unlikely(!RQ_IOC(rq)))
245 return;
246
247 aic = RQ_IOC(rq)->aic;
248
249 if (rq_is_sync(rq) && aic) {
250 unsigned long flags;
251
252 spin_lock_irqsave(&aic->lock, flags);
253 set_bit(AS_TASK_IORUNNING, &aic->state);
254 aic->last_end_request = jiffies;
255 spin_unlock_irqrestore(&aic->lock, flags);
256 }
257
258 put_io_context(RQ_IOC(rq));
259}
260
261
262
263
264#define RQ_RB_ROOT(ad, rq) (&(ad)->sort_list[rq_is_sync((rq))])
265
266static void as_add_rq_rb(struct as_data *ad, struct request *rq)
267{
268 struct request *alias;
269
270 while ((unlikely(alias = elv_rb_add(RQ_RB_ROOT(ad, rq), rq)))) {
271 as_move_to_dispatch(ad, alias);
272 as_antic_stop(ad);
273 }
274}
275
276static inline void as_del_rq_rb(struct as_data *ad, struct request *rq)
277{
278 elv_rb_del(RQ_RB_ROOT(ad, rq), rq);
279}
280
281
282
283
284
285#define MAXBACK (1024 * 1024)
286
287
288
289
290#define BACK_PENALTY 2
291
292
293
294
295
296static struct request *
297as_choose_req(struct as_data *ad, struct request *rq1, struct request *rq2)
298{
299 int data_dir;
300 sector_t last, s1, s2, d1, d2;
301 int r1_wrap=0, r2_wrap=0;
302 const sector_t maxback = MAXBACK;
303
304 if (rq1 == NULL || rq1 == rq2)
305 return rq2;
306 if (rq2 == NULL)
307 return rq1;
308
309 data_dir = rq_is_sync(rq1);
310
311 last = ad->last_sector[data_dir];
312 s1 = rq1->sector;
313 s2 = rq2->sector;
314
315 BUG_ON(data_dir != rq_is_sync(rq2));
316
317
318
319
320
321
322 if (s1 >= last)
323 d1 = s1 - last;
324 else if (s1+maxback >= last)
325 d1 = (last - s1)*BACK_PENALTY;
326 else {
327 r1_wrap = 1;
328 d1 = 0;
329 }
330
331 if (s2 >= last)
332 d2 = s2 - last;
333 else if (s2+maxback >= last)
334 d2 = (last - s2)*BACK_PENALTY;
335 else {
336 r2_wrap = 1;
337 d2 = 0;
338 }
339
340
341 if (!r1_wrap && r2_wrap)
342 return rq1;
343 else if (!r2_wrap && r1_wrap)
344 return rq2;
345 else if (r1_wrap && r2_wrap) {
346
347 if (s1 <= s2)
348 return rq1;
349 else
350 return rq2;
351 }
352
353
354 if (d1 < d2)
355 return rq1;
356 else if (d2 < d1)
357 return rq2;
358 else {
359 if (s1 >= s2)
360 return rq1;
361 else
362 return rq2;
363 }
364}
365
366
367
368
369
370
371static struct request *
372as_find_next_rq(struct as_data *ad, struct request *last)
373{
374 struct rb_node *rbnext = rb_next(&last->rb_node);
375 struct rb_node *rbprev = rb_prev(&last->rb_node);
376 struct request *next = NULL, *prev = NULL;
377
378 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
379
380 if (rbprev)
381 prev = rb_entry_rq(rbprev);
382
383 if (rbnext)
384 next = rb_entry_rq(rbnext);
385 else {
386 const int data_dir = rq_is_sync(last);
387
388 rbnext = rb_first(&ad->sort_list[data_dir]);
389 if (rbnext && rbnext != &last->rb_node)
390 next = rb_entry_rq(rbnext);
391 }
392
393 return as_choose_req(ad, next, prev);
394}
395
396
397
398
399
400
401
402
403
404
405static int as_antic_expired(struct as_data *ad)
406{
407 long delta_jif;
408
409 delta_jif = jiffies - ad->antic_start;
410 if (unlikely(delta_jif < 0))
411 delta_jif = -delta_jif;
412 if (delta_jif < ad->antic_expire)
413 return 0;
414
415 return 1;
416}
417
418
419
420
421
422static void as_antic_waitnext(struct as_data *ad)
423{
424 unsigned long timeout;
425
426 BUG_ON(ad->antic_status != ANTIC_OFF
427 && ad->antic_status != ANTIC_WAIT_REQ);
428
429 timeout = ad->antic_start + ad->antic_expire;
430
431 mod_timer(&ad->antic_timer, timeout);
432
433 ad->antic_status = ANTIC_WAIT_NEXT;
434}
435
436
437
438
439
440
441static void as_antic_waitreq(struct as_data *ad)
442{
443 BUG_ON(ad->antic_status == ANTIC_FINISHED);
444 if (ad->antic_status == ANTIC_OFF) {
445 if (!ad->io_context || ad->ioc_finished)
446 as_antic_waitnext(ad);
447 else
448 ad->antic_status = ANTIC_WAIT_REQ;
449 }
450}
451
452
453
454
455
456static void as_antic_stop(struct as_data *ad)
457{
458 int status = ad->antic_status;
459
460 if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
461 if (status == ANTIC_WAIT_NEXT)
462 del_timer(&ad->antic_timer);
463 ad->antic_status = ANTIC_FINISHED;
464
465 kblockd_schedule_work(&ad->antic_work);
466 }
467}
468
469
470
471
472static void as_antic_timeout(unsigned long data)
473{
474 struct request_queue *q = (struct request_queue *)data;
475 struct as_data *ad = q->elevator->elevator_data;
476 unsigned long flags;
477
478 spin_lock_irqsave(q->queue_lock, flags);
479 if (ad->antic_status == ANTIC_WAIT_REQ
480 || ad->antic_status == ANTIC_WAIT_NEXT) {
481 struct as_io_context *aic;
482 spin_lock(&ad->io_context->lock);
483 aic = ad->io_context->aic;
484
485 ad->antic_status = ANTIC_FINISHED;
486 kblockd_schedule_work(&ad->antic_work);
487
488 if (aic->ttime_samples == 0) {
489
490 ad->exit_prob = (7*ad->exit_prob + 256)/8;
491 }
492 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
493
494 ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8;
495 }
496 spin_unlock(&ad->io_context->lock);
497 }
498 spin_unlock_irqrestore(q->queue_lock, flags);
499}
500
501static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic,
502 unsigned long ttime)
503{
504
505 if (aic->ttime_samples == 0) {
506 ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
507 ad->new_ttime_mean = ad->new_ttime_total / 256;
508
509 ad->exit_prob = (7*ad->exit_prob)/8;
510 }
511 aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
512 aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
513 aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
514}
515
516static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic,
517 sector_t sdist)
518{
519 u64 total;
520
521 if (aic->seek_samples == 0) {
522 ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
523 ad->new_seek_mean = ad->new_seek_total / 256;
524 }
525
526
527
528
529
530 if (aic->seek_samples <= 60)
531 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
532 else
533 sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
534
535 aic->seek_samples = (7*aic->seek_samples + 256) / 8;
536 aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
537 total = aic->seek_total + (aic->seek_samples/2);
538 do_div(total, aic->seek_samples);
539 aic->seek_mean = (sector_t)total;
540}
541
542
543
544
545
546
547static void as_update_iohist(struct as_data *ad, struct as_io_context *aic,
548 struct request *rq)
549{
550 int data_dir = rq_is_sync(rq);
551 unsigned long thinktime = 0;
552 sector_t seek_dist;
553
554 if (aic == NULL)
555 return;
556
557 if (data_dir == REQ_SYNC) {
558 unsigned long in_flight = atomic_read(&aic->nr_queued)
559 + atomic_read(&aic->nr_dispatched);
560 spin_lock(&aic->lock);
561 if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
562 test_bit(AS_TASK_IOSTARTED, &aic->state)) {
563
564 if (test_bit(AS_TASK_IORUNNING, &aic->state)
565 && in_flight == 0) {
566 thinktime = jiffies - aic->last_end_request;
567 thinktime = min(thinktime, MAX_THINKTIME-1);
568 }
569 as_update_thinktime(ad, aic, thinktime);
570
571
572 if (aic->last_request_pos < rq->sector)
573 seek_dist = rq->sector - aic->last_request_pos;
574 else
575 seek_dist = aic->last_request_pos - rq->sector;
576 as_update_seekdist(ad, aic, seek_dist);
577 }
578 aic->last_request_pos = rq->sector + rq->nr_sectors;
579 set_bit(AS_TASK_IOSTARTED, &aic->state);
580 spin_unlock(&aic->lock);
581 }
582}
583
584
585
586
587
588static int as_close_req(struct as_data *ad, struct as_io_context *aic,
589 struct request *rq)
590{
591 unsigned long delay;
592 sector_t last = ad->last_sector[ad->batch_data_dir];
593 sector_t next = rq->sector;
594 sector_t delta;
595 sector_t s;
596
597 if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
598 delay = 0;
599 else
600 delay = jiffies - ad->antic_start;
601
602 if (delay == 0)
603 delta = 8192;
604 else if (delay <= (20 * HZ / 1000) && delay <= ad->antic_expire)
605 delta = 8192 << delay;
606 else
607 return 1;
608
609 if ((last <= next + (delta>>1)) && (next <= last + delta))
610 return 1;
611
612 if (last < next)
613 s = next - last;
614 else
615 s = last - next;
616
617 if (aic->seek_samples == 0) {
618
619
620
621
622 if (ad->new_seek_mean > s) {
623
624 return 1;
625 }
626
627 } else {
628 if (aic->seek_mean > s) {
629
630 return 1;
631 }
632 }
633
634 return 0;
635}
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650static int as_can_break_anticipation(struct as_data *ad, struct request *rq)
651{
652 struct io_context *ioc;
653 struct as_io_context *aic;
654
655 ioc = ad->io_context;
656 BUG_ON(!ioc);
657 spin_lock(&ioc->lock);
658
659 if (rq && ioc == RQ_IOC(rq)) {
660
661 spin_unlock(&ioc->lock);
662 return 1;
663 }
664
665 if (ad->ioc_finished && as_antic_expired(ad)) {
666
667
668
669
670 spin_unlock(&ioc->lock);
671 return 1;
672 }
673
674 aic = ioc->aic;
675 if (!aic) {
676 spin_unlock(&ioc->lock);
677 return 0;
678 }
679
680 if (atomic_read(&aic->nr_queued) > 0) {
681
682 spin_unlock(&ioc->lock);
683 return 1;
684 }
685
686 if (atomic_read(&aic->nr_dispatched) > 0) {
687
688 spin_unlock(&ioc->lock);
689 return 1;
690 }
691
692 if (rq && rq_is_sync(rq) && as_close_req(ad, aic, rq)) {
693
694
695
696
697
698
699
700
701 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
702 if (aic->ttime_samples == 0)
703 ad->exit_prob = (7*ad->exit_prob + 256)/8;
704
705 ad->exit_no_coop = (7*ad->exit_no_coop)/8;
706 }
707
708 as_update_iohist(ad, aic, rq);
709 spin_unlock(&ioc->lock);
710 return 1;
711 }
712
713 if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
714
715 if (aic->ttime_samples == 0)
716 ad->exit_prob = (7*ad->exit_prob + 256)/8;
717
718 if (ad->exit_no_coop > 128) {
719 spin_unlock(&ioc->lock);
720 return 1;
721 }
722 }
723
724 if (aic->ttime_samples == 0) {
725 if (ad->new_ttime_mean > ad->antic_expire) {
726 spin_unlock(&ioc->lock);
727 return 1;
728 }
729 if (ad->exit_prob * ad->exit_no_coop > 128*256) {
730 spin_unlock(&ioc->lock);
731 return 1;
732 }
733 } else if (aic->ttime_mean > ad->antic_expire) {
734
735 spin_unlock(&ioc->lock);
736 return 1;
737 }
738 spin_unlock(&ioc->lock);
739 return 0;
740}
741
742
743
744
745
746static int as_can_anticipate(struct as_data *ad, struct request *rq)
747{
748 if (!ad->io_context)
749
750
751
752 return 0;
753
754 if (ad->antic_status == ANTIC_FINISHED)
755
756
757
758 return 0;
759
760 if (as_can_break_anticipation(ad, rq))
761
762
763
764
765 return 0;
766
767
768
769
770
771
772
773
774 return 1;
775}
776
777
778
779
780
781
782static void as_update_rq(struct as_data *ad, struct request *rq)
783{
784 const int data_dir = rq_is_sync(rq);
785
786
787 ad->next_rq[data_dir] = as_choose_req(ad, rq, ad->next_rq[data_dir]);
788
789
790
791
792
793
794 if (ad->antic_status == ANTIC_WAIT_REQ
795 || ad->antic_status == ANTIC_WAIT_NEXT) {
796 if (as_can_break_anticipation(ad, rq))
797 as_antic_stop(ad);
798 }
799}
800
801
802
803
804static void update_write_batch(struct as_data *ad)
805{
806 unsigned long batch = ad->batch_expire[REQ_ASYNC];
807 long write_time;
808
809 write_time = (jiffies - ad->current_batch_expires) + batch;
810 if (write_time < 0)
811 write_time = 0;
812
813 if (write_time > batch && !ad->write_batch_idled) {
814 if (write_time > batch * 3)
815 ad->write_batch_count /= 2;
816 else
817 ad->write_batch_count--;
818 } else if (write_time < batch && ad->current_write_count == 0) {
819 if (batch > write_time * 3)
820 ad->write_batch_count *= 2;
821 else
822 ad->write_batch_count++;
823 }
824
825 if (ad->write_batch_count < 1)
826 ad->write_batch_count = 1;
827}
828
829
830
831
832
833static void as_completed_request(struct request_queue *q, struct request *rq)
834{
835 struct as_data *ad = q->elevator->elevator_data;
836
837 WARN_ON(!list_empty(&rq->queuelist));
838
839 if (RQ_STATE(rq) != AS_RQ_REMOVED) {
840 WARN(1, "rq->state %d\n", RQ_STATE(rq));
841 goto out;
842 }
843
844 if (ad->changed_batch && ad->nr_dispatched == 1) {
845 ad->current_batch_expires = jiffies +
846 ad->batch_expire[ad->batch_data_dir];
847 kblockd_schedule_work(&ad->antic_work);
848 ad->changed_batch = 0;
849
850 if (ad->batch_data_dir == REQ_SYNC)
851 ad->new_batch = 1;
852 }
853 WARN_ON(ad->nr_dispatched == 0);
854 ad->nr_dispatched--;
855
856
857
858
859
860
861 if (ad->new_batch && ad->batch_data_dir == rq_is_sync(rq)) {
862 update_write_batch(ad);
863 ad->current_batch_expires = jiffies +
864 ad->batch_expire[REQ_SYNC];
865 ad->new_batch = 0;
866 }
867
868 if (ad->io_context == RQ_IOC(rq) && ad->io_context) {
869 ad->antic_start = jiffies;
870 ad->ioc_finished = 1;
871 if (ad->antic_status == ANTIC_WAIT_REQ) {
872
873
874
875
876 as_antic_waitnext(ad);
877 }
878 }
879
880 as_put_io_context(rq);
881out:
882 RQ_SET_STATE(rq, AS_RQ_POSTSCHED);
883}
884
885
886
887
888
889
890
891static void as_remove_queued_request(struct request_queue *q,
892 struct request *rq)
893{
894 const int data_dir = rq_is_sync(rq);
895 struct as_data *ad = q->elevator->elevator_data;
896 struct io_context *ioc;
897
898 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
899
900 ioc = RQ_IOC(rq);
901 if (ioc && ioc->aic) {
902 BUG_ON(!atomic_read(&ioc->aic->nr_queued));
903 atomic_dec(&ioc->aic->nr_queued);
904 }
905
906
907
908
909
910 if (ad->next_rq[data_dir] == rq)
911 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
912
913 rq_fifo_clear(rq);
914 as_del_rq_rb(ad, rq);
915}
916
917
918
919
920
921
922
923
924
925static int as_fifo_expired(struct as_data *ad, int adir)
926{
927 struct request *rq;
928 long delta_jif;
929
930 delta_jif = jiffies - ad->last_check_fifo[adir];
931 if (unlikely(delta_jif < 0))
932 delta_jif = -delta_jif;
933 if (delta_jif < ad->fifo_expire[adir])
934 return 0;
935
936 ad->last_check_fifo[adir] = jiffies;
937
938 if (list_empty(&ad->fifo_list[adir]))
939 return 0;
940
941 rq = rq_entry_fifo(ad->fifo_list[adir].next);
942
943 return time_after(jiffies, rq_fifo_time(rq));
944}
945
946
947
948
949
950static inline int as_batch_expired(struct as_data *ad)
951{
952 if (ad->changed_batch || ad->new_batch)
953 return 0;
954
955 if (ad->batch_data_dir == REQ_SYNC)
956
957 return time_after(jiffies, ad->current_batch_expires);
958
959 return time_after(jiffies, ad->current_batch_expires)
960 || ad->current_write_count == 0;
961}
962
963
964
965
966static void as_move_to_dispatch(struct as_data *ad, struct request *rq)
967{
968 const int data_dir = rq_is_sync(rq);
969
970 BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
971
972 as_antic_stop(ad);
973 ad->antic_status = ANTIC_OFF;
974
975
976
977
978
979 ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
980
981 if (data_dir == REQ_SYNC) {
982 struct io_context *ioc = RQ_IOC(rq);
983
984 copy_io_context(&ad->io_context, &ioc);
985 } else {
986 if (ad->io_context) {
987 put_io_context(ad->io_context);
988 ad->io_context = NULL;
989 }
990
991 if (ad->current_write_count != 0)
992 ad->current_write_count--;
993 }
994 ad->ioc_finished = 0;
995
996 ad->next_rq[data_dir] = as_find_next_rq(ad, rq);
997
998
999
1000
1001 as_remove_queued_request(ad->q, rq);
1002 WARN_ON(RQ_STATE(rq) != AS_RQ_QUEUED);
1003
1004 elv_dispatch_sort(ad->q, rq);
1005
1006 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1007 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1008 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
1009 ad->nr_dispatched++;
1010}
1011
1012
1013
1014
1015
1016
1017static int as_dispatch_request(struct request_queue *q, int force)
1018{
1019 struct as_data *ad = q->elevator->elevator_data;
1020 const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
1021 const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
1022 struct request *rq;
1023
1024 if (unlikely(force)) {
1025
1026
1027
1028
1029
1030
1031
1032 int dispatched = 0;
1033
1034 ad->batch_data_dir = REQ_SYNC;
1035 ad->changed_batch = 0;
1036 ad->new_batch = 0;
1037
1038 while (ad->next_rq[REQ_SYNC]) {
1039 as_move_to_dispatch(ad, ad->next_rq[REQ_SYNC]);
1040 dispatched++;
1041 }
1042 ad->last_check_fifo[REQ_SYNC] = jiffies;
1043
1044 while (ad->next_rq[REQ_ASYNC]) {
1045 as_move_to_dispatch(ad, ad->next_rq[REQ_ASYNC]);
1046 dispatched++;
1047 }
1048 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1049
1050 return dispatched;
1051 }
1052
1053
1054 if (ad->batch_data_dir == REQ_ASYNC && !reads) {
1055 if (ad->current_write_count == 0 || !writes)
1056 ad->write_batch_idled = 1;
1057 }
1058
1059 if (!(reads || writes)
1060 || ad->antic_status == ANTIC_WAIT_REQ
1061 || ad->antic_status == ANTIC_WAIT_NEXT
1062 || ad->changed_batch)
1063 return 0;
1064
1065 if (!(reads && writes && as_batch_expired(ad))) {
1066
1067
1068
1069 rq = ad->next_rq[ad->batch_data_dir];
1070
1071 if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
1072 if (as_fifo_expired(ad, REQ_SYNC))
1073 goto fifo_expired;
1074
1075 if (as_can_anticipate(ad, rq)) {
1076 as_antic_waitreq(ad);
1077 return 0;
1078 }
1079 }
1080
1081 if (rq) {
1082
1083 if (reads && !writes)
1084 ad->current_batch_expires =
1085 jiffies + ad->batch_expire[REQ_SYNC];
1086 goto dispatch_request;
1087 }
1088 }
1089
1090
1091
1092
1093
1094
1095 if (reads) {
1096 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_SYNC]));
1097
1098 if (writes && ad->batch_data_dir == REQ_SYNC)
1099
1100
1101
1102 goto dispatch_writes;
1103
1104 if (ad->batch_data_dir == REQ_ASYNC) {
1105 WARN_ON(ad->new_batch);
1106 ad->changed_batch = 1;
1107 }
1108 ad->batch_data_dir = REQ_SYNC;
1109 rq = rq_entry_fifo(ad->fifo_list[REQ_SYNC].next);
1110 ad->last_check_fifo[ad->batch_data_dir] = jiffies;
1111 goto dispatch_request;
1112 }
1113
1114
1115
1116
1117
1118 if (writes) {
1119dispatch_writes:
1120 BUG_ON(RB_EMPTY_ROOT(&ad->sort_list[REQ_ASYNC]));
1121
1122 if (ad->batch_data_dir == REQ_SYNC) {
1123 ad->changed_batch = 1;
1124
1125
1126
1127
1128
1129
1130 ad->new_batch = 0;
1131 }
1132 ad->batch_data_dir = REQ_ASYNC;
1133 ad->current_write_count = ad->write_batch_count;
1134 ad->write_batch_idled = 0;
1135 rq = rq_entry_fifo(ad->fifo_list[REQ_ASYNC].next);
1136 ad->last_check_fifo[REQ_ASYNC] = jiffies;
1137 goto dispatch_request;
1138 }
1139
1140 BUG();
1141 return 0;
1142
1143dispatch_request:
1144
1145
1146
1147
1148 if (as_fifo_expired(ad, ad->batch_data_dir)) {
1149fifo_expired:
1150 rq = rq_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
1151 }
1152
1153 if (ad->changed_batch) {
1154 WARN_ON(ad->new_batch);
1155
1156 if (ad->nr_dispatched)
1157 return 0;
1158
1159 if (ad->batch_data_dir == REQ_ASYNC)
1160 ad->current_batch_expires = jiffies +
1161 ad->batch_expire[REQ_ASYNC];
1162 else
1163 ad->new_batch = 1;
1164
1165 ad->changed_batch = 0;
1166 }
1167
1168
1169
1170
1171 as_move_to_dispatch(ad, rq);
1172
1173 return 1;
1174}
1175
1176
1177
1178
1179static void as_add_request(struct request_queue *q, struct request *rq)
1180{
1181 struct as_data *ad = q->elevator->elevator_data;
1182 int data_dir;
1183
1184 RQ_SET_STATE(rq, AS_RQ_NEW);
1185
1186 data_dir = rq_is_sync(rq);
1187
1188 rq->elevator_private = as_get_io_context(q->node);
1189
1190 if (RQ_IOC(rq)) {
1191 as_update_iohist(ad, RQ_IOC(rq)->aic, rq);
1192 atomic_inc(&RQ_IOC(rq)->aic->nr_queued);
1193 }
1194
1195 as_add_rq_rb(ad, rq);
1196
1197
1198
1199
1200 rq_set_fifo_time(rq, jiffies + ad->fifo_expire[data_dir]);
1201 list_add_tail(&rq->queuelist, &ad->fifo_list[data_dir]);
1202
1203 as_update_rq(ad, rq);
1204 RQ_SET_STATE(rq, AS_RQ_QUEUED);
1205}
1206
1207static void as_activate_request(struct request_queue *q, struct request *rq)
1208{
1209 WARN_ON(RQ_STATE(rq) != AS_RQ_DISPATCHED);
1210 RQ_SET_STATE(rq, AS_RQ_REMOVED);
1211 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1212 atomic_dec(&RQ_IOC(rq)->aic->nr_dispatched);
1213}
1214
1215static void as_deactivate_request(struct request_queue *q, struct request *rq)
1216{
1217 WARN_ON(RQ_STATE(rq) != AS_RQ_REMOVED);
1218 RQ_SET_STATE(rq, AS_RQ_DISPATCHED);
1219 if (RQ_IOC(rq) && RQ_IOC(rq)->aic)
1220 atomic_inc(&RQ_IOC(rq)->aic->nr_dispatched);
1221}
1222
1223
1224
1225
1226
1227
1228
1229static int as_queue_empty(struct request_queue *q)
1230{
1231 struct as_data *ad = q->elevator->elevator_data;
1232
1233 return list_empty(&ad->fifo_list[REQ_ASYNC])
1234 && list_empty(&ad->fifo_list[REQ_SYNC]);
1235}
1236
1237static int
1238as_merge(struct request_queue *q, struct request **req, struct bio *bio)
1239{
1240 struct as_data *ad = q->elevator->elevator_data;
1241 sector_t rb_key = bio->bi_sector + bio_sectors(bio);
1242 struct request *__rq;
1243
1244
1245
1246
1247 __rq = elv_rb_find(&ad->sort_list[bio_data_dir(bio)], rb_key);
1248 if (__rq && elv_rq_merge_ok(__rq, bio)) {
1249 *req = __rq;
1250 return ELEVATOR_FRONT_MERGE;
1251 }
1252
1253 return ELEVATOR_NO_MERGE;
1254}
1255
1256static void as_merged_request(struct request_queue *q, struct request *req,
1257 int type)
1258{
1259 struct as_data *ad = q->elevator->elevator_data;
1260
1261
1262
1263
1264 if (type == ELEVATOR_FRONT_MERGE) {
1265 as_del_rq_rb(ad, req);
1266 as_add_rq_rb(ad, req);
1267
1268
1269
1270
1271
1272 }
1273}
1274
1275static void as_merged_requests(struct request_queue *q, struct request *req,
1276 struct request *next)
1277{
1278
1279
1280
1281
1282 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
1283 if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
1284 list_move(&req->queuelist, &next->queuelist);
1285 rq_set_fifo_time(req, rq_fifo_time(next));
1286 }
1287 }
1288
1289
1290
1291
1292 as_remove_queued_request(q, next);
1293 as_put_io_context(next);
1294
1295 RQ_SET_STATE(next, AS_RQ_MERGED);
1296}
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307static void as_work_handler(struct work_struct *work)
1308{
1309 struct as_data *ad = container_of(work, struct as_data, antic_work);
1310 struct request_queue *q = ad->q;
1311 unsigned long flags;
1312
1313 spin_lock_irqsave(q->queue_lock, flags);
1314 blk_start_queueing(q);
1315 spin_unlock_irqrestore(q->queue_lock, flags);
1316}
1317
1318static int as_may_queue(struct request_queue *q, int rw)
1319{
1320 int ret = ELV_MQUEUE_MAY;
1321 struct as_data *ad = q->elevator->elevator_data;
1322 struct io_context *ioc;
1323 if (ad->antic_status == ANTIC_WAIT_REQ ||
1324 ad->antic_status == ANTIC_WAIT_NEXT) {
1325 ioc = as_get_io_context(q->node);
1326 if (ad->io_context == ioc)
1327 ret = ELV_MQUEUE_MUST;
1328 put_io_context(ioc);
1329 }
1330
1331 return ret;
1332}
1333
1334static void as_exit_queue(elevator_t *e)
1335{
1336 struct as_data *ad = e->elevator_data;
1337
1338 del_timer_sync(&ad->antic_timer);
1339 kblockd_flush_work(&ad->antic_work);
1340
1341 BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
1342 BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
1343
1344 put_io_context(ad->io_context);
1345 kfree(ad);
1346}
1347
1348
1349
1350
1351static void *as_init_queue(struct request_queue *q)
1352{
1353 struct as_data *ad;
1354
1355 ad = kmalloc_node(sizeof(*ad), GFP_KERNEL | __GFP_ZERO, q->node);
1356 if (!ad)
1357 return NULL;
1358
1359 ad->q = q;
1360
1361
1362 ad->antic_timer.function = as_antic_timeout;
1363 ad->antic_timer.data = (unsigned long)q;
1364 init_timer(&ad->antic_timer);
1365 INIT_WORK(&ad->antic_work, as_work_handler);
1366
1367 INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
1368 INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
1369 ad->sort_list[REQ_SYNC] = RB_ROOT;
1370 ad->sort_list[REQ_ASYNC] = RB_ROOT;
1371 ad->fifo_expire[REQ_SYNC] = default_read_expire;
1372 ad->fifo_expire[REQ_ASYNC] = default_write_expire;
1373 ad->antic_expire = default_antic_expire;
1374 ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
1375 ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
1376
1377 ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
1378 ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
1379 if (ad->write_batch_count < 2)
1380 ad->write_batch_count = 2;
1381
1382 return ad;
1383}
1384
1385
1386
1387
1388
1389static ssize_t
1390as_var_show(unsigned int var, char *page)
1391{
1392 return sprintf(page, "%d\n", var);
1393}
1394
1395static ssize_t
1396as_var_store(unsigned long *var, const char *page, size_t count)
1397{
1398 char *p = (char *) page;
1399
1400 *var = simple_strtoul(p, &p, 10);
1401 return count;
1402}
1403
1404static ssize_t est_time_show(elevator_t *e, char *page)
1405{
1406 struct as_data *ad = e->elevator_data;
1407 int pos = 0;
1408
1409 pos += sprintf(page+pos, "%lu %% exit probability\n",
1410 100*ad->exit_prob/256);
1411 pos += sprintf(page+pos, "%lu %% probability of exiting without a "
1412 "cooperating process submitting IO\n",
1413 100*ad->exit_no_coop/256);
1414 pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
1415 pos += sprintf(page+pos, "%llu sectors new seek distance\n",
1416 (unsigned long long)ad->new_seek_mean);
1417
1418 return pos;
1419}
1420
1421#define SHOW_FUNCTION(__FUNC, __VAR) \
1422static ssize_t __FUNC(elevator_t *e, char *page) \
1423{ \
1424 struct as_data *ad = e->elevator_data; \
1425 return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
1426}
1427SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
1428SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
1429SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
1430SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
1431SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
1432#undef SHOW_FUNCTION
1433
1434#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
1435static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
1436{ \
1437 struct as_data *ad = e->elevator_data; \
1438 int ret = as_var_store(__PTR, (page), count); \
1439 if (*(__PTR) < (MIN)) \
1440 *(__PTR) = (MIN); \
1441 else if (*(__PTR) > (MAX)) \
1442 *(__PTR) = (MAX); \
1443 *(__PTR) = msecs_to_jiffies(*(__PTR)); \
1444 return ret; \
1445}
1446STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
1447STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
1448STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
1449STORE_FUNCTION(as_read_batch_expire_store,
1450 &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
1451STORE_FUNCTION(as_write_batch_expire_store,
1452 &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
1453#undef STORE_FUNCTION
1454
1455#define AS_ATTR(name) \
1456 __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
1457
1458static struct elv_fs_entry as_attrs[] = {
1459 __ATTR_RO(est_time),
1460 AS_ATTR(read_expire),
1461 AS_ATTR(write_expire),
1462 AS_ATTR(antic_expire),
1463 AS_ATTR(read_batch_expire),
1464 AS_ATTR(write_batch_expire),
1465 __ATTR_NULL
1466};
1467
1468static struct elevator_type iosched_as = {
1469 .ops = {
1470 .elevator_merge_fn = as_merge,
1471 .elevator_merged_fn = as_merged_request,
1472 .elevator_merge_req_fn = as_merged_requests,
1473 .elevator_dispatch_fn = as_dispatch_request,
1474 .elevator_add_req_fn = as_add_request,
1475 .elevator_activate_req_fn = as_activate_request,
1476 .elevator_deactivate_req_fn = as_deactivate_request,
1477 .elevator_queue_empty_fn = as_queue_empty,
1478 .elevator_completed_req_fn = as_completed_request,
1479 .elevator_former_req_fn = elv_rb_former_request,
1480 .elevator_latter_req_fn = elv_rb_latter_request,
1481 .elevator_may_queue_fn = as_may_queue,
1482 .elevator_init_fn = as_init_queue,
1483 .elevator_exit_fn = as_exit_queue,
1484 .trim = as_trim,
1485 },
1486
1487 .elevator_attrs = as_attrs,
1488 .elevator_name = "anticipatory",
1489 .elevator_owner = THIS_MODULE,
1490};
1491
1492static int __init as_init(void)
1493{
1494 elv_register(&iosched_as);
1495
1496 return 0;
1497}
1498
1499static void __exit as_exit(void)
1500{
1501 DECLARE_COMPLETION_ONSTACK(all_gone);
1502 elv_unregister(&iosched_as);
1503 ioc_gone = &all_gone;
1504
1505 smp_wmb();
1506 if (elv_ioc_count_read(ioc_count))
1507 wait_for_completion(&all_gone);
1508 synchronize_rcu();
1509}
1510
1511module_init(as_init);
1512module_exit(as_exit);
1513
1514MODULE_AUTHOR("Nick Piggin");
1515MODULE_LICENSE("GPL");
1516MODULE_DESCRIPTION("anticipatory IO scheduler");
1517