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