2 * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler,
3 * for the blk-mq scheduling framework
5 * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk>
7 #include <linux/kernel.h>
9 #include <linux/blkdev.h>
10 #include <linux/blk-mq.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/sbitmap.h>
22 #include "blk-mq-debugfs.h"
23 #include "blk-mq-tag.h"
24 #include "blk-mq-sched.h"
27 * See Documentation/block/deadline-iosched.txt
29 static const int read_expire
= HZ
/ 2; /* max time before a read is submitted. */
30 static const int write_expire
= 5 * HZ
; /* ditto for writes, these limits are SOFT! */
31 static const int writes_starved
= 2; /* max times reads can starve a write */
32 static const int fifo_batch
= 16; /* # of sequential requests treated as one
33 by the above parameters. For throughput. */
35 struct deadline_data
{
41 * requests (deadline_rq s) are present on both sort_list and fifo_list
43 struct rb_root sort_list
[2];
44 struct list_head fifo_list
[2];
47 * next in sort order. read, write or both are NULL
49 struct request
*next_rq
[2];
50 unsigned int batching
; /* number of sequential requests made */
51 unsigned int starved
; /* times reads have starved writes */
54 * settings that change how the i/o scheduler behaves
63 struct list_head dispatch
;
66 static inline struct rb_root
*
67 deadline_rb_root(struct deadline_data
*dd
, struct request
*rq
)
69 return &dd
->sort_list
[rq_data_dir(rq
)];
73 * get the request after `rq' in sector-sorted order
75 static inline struct request
*
76 deadline_latter_request(struct request
*rq
)
78 struct rb_node
*node
= rb_next(&rq
->rb_node
);
81 return rb_entry_rq(node
);
87 deadline_add_rq_rb(struct deadline_data
*dd
, struct request
*rq
)
89 struct rb_root
*root
= deadline_rb_root(dd
, rq
);
95 deadline_del_rq_rb(struct deadline_data
*dd
, struct request
*rq
)
97 const int data_dir
= rq_data_dir(rq
);
99 if (dd
->next_rq
[data_dir
] == rq
)
100 dd
->next_rq
[data_dir
] = deadline_latter_request(rq
);
102 elv_rb_del(deadline_rb_root(dd
, rq
), rq
);
106 * remove rq from rbtree and fifo.
108 static void deadline_remove_request(struct request_queue
*q
, struct request
*rq
)
110 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
112 list_del_init(&rq
->queuelist
);
115 * We might not be on the rbtree, if we are doing an insert merge
117 if (!RB_EMPTY_NODE(&rq
->rb_node
))
118 deadline_del_rq_rb(dd
, rq
);
120 elv_rqhash_del(q
, rq
);
121 if (q
->last_merge
== rq
)
122 q
->last_merge
= NULL
;
125 static void dd_request_merged(struct request_queue
*q
, struct request
*req
,
128 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
131 * if the merge was a front merge, we need to reposition request
133 if (type
== ELEVATOR_FRONT_MERGE
) {
134 elv_rb_del(deadline_rb_root(dd
, req
), req
);
135 deadline_add_rq_rb(dd
, req
);
139 static void dd_merged_requests(struct request_queue
*q
, struct request
*req
,
140 struct request
*next
)
143 * if next expires before rq, assign its expire time to rq
144 * and move into next position (next will be deleted) in fifo
146 if (!list_empty(&req
->queuelist
) && !list_empty(&next
->queuelist
)) {
147 if (time_before((unsigned long)next
->fifo_time
,
148 (unsigned long)req
->fifo_time
)) {
149 list_move(&req
->queuelist
, &next
->queuelist
);
150 req
->fifo_time
= next
->fifo_time
;
155 * kill knowledge of next, this one is a goner
157 deadline_remove_request(q
, next
);
161 * move an entry to dispatch queue
164 deadline_move_request(struct deadline_data
*dd
, struct request
*rq
)
166 const int data_dir
= rq_data_dir(rq
);
168 dd
->next_rq
[READ
] = NULL
;
169 dd
->next_rq
[WRITE
] = NULL
;
170 dd
->next_rq
[data_dir
] = deadline_latter_request(rq
);
173 * take it off the sort and fifo list
175 deadline_remove_request(rq
->q
, rq
);
179 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
180 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
182 static inline int deadline_check_fifo(struct deadline_data
*dd
, int ddir
)
184 struct request
*rq
= rq_entry_fifo(dd
->fifo_list
[ddir
].next
);
189 if (time_after_eq(jiffies
, (unsigned long)rq
->fifo_time
))
196 * For the specified data direction, return the next request to
197 * dispatch using arrival ordered lists.
199 static struct request
*
200 deadline_fifo_request(struct deadline_data
*dd
, int data_dir
)
205 if (WARN_ON_ONCE(data_dir
!= READ
&& data_dir
!= WRITE
))
208 if (list_empty(&dd
->fifo_list
[data_dir
]))
211 rq
= rq_entry_fifo(dd
->fifo_list
[data_dir
].next
);
212 if (data_dir
== READ
|| !blk_queue_is_zoned(rq
->q
))
216 * Look for a write request that can be dispatched, that is one with
217 * an unlocked target zone.
219 spin_lock_irqsave(&dd
->zone_lock
, flags
);
220 list_for_each_entry(rq
, &dd
->fifo_list
[WRITE
], queuelist
) {
221 if (blk_req_can_dispatch_to_zone(rq
))
226 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
232 * For the specified data direction, return the next request to
233 * dispatch using sector position sorted lists.
235 static struct request
*
236 deadline_next_request(struct deadline_data
*dd
, int data_dir
)
241 if (WARN_ON_ONCE(data_dir
!= READ
&& data_dir
!= WRITE
))
244 rq
= dd
->next_rq
[data_dir
];
248 if (data_dir
== READ
|| !blk_queue_is_zoned(rq
->q
))
252 * Look for a write request that can be dispatched, that is one with
253 * an unlocked target zone.
255 spin_lock_irqsave(&dd
->zone_lock
, flags
);
257 if (blk_req_can_dispatch_to_zone(rq
))
259 rq
= deadline_latter_request(rq
);
261 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
267 * deadline_dispatch_requests selects the best request according to
268 * read/write expire, fifo_batch, etc
270 static struct request
*__dd_dispatch_request(struct deadline_data
*dd
)
272 struct request
*rq
, *next_rq
;
276 if (!list_empty(&dd
->dispatch
)) {
277 rq
= list_first_entry(&dd
->dispatch
, struct request
, queuelist
);
278 list_del_init(&rq
->queuelist
);
282 reads
= !list_empty(&dd
->fifo_list
[READ
]);
283 writes
= !list_empty(&dd
->fifo_list
[WRITE
]);
286 * batches are currently reads XOR writes
288 rq
= deadline_next_request(dd
, WRITE
);
290 rq
= deadline_next_request(dd
, READ
);
292 if (rq
&& dd
->batching
< dd
->fifo_batch
)
293 /* we have a next request are still entitled to batch */
294 goto dispatch_request
;
297 * at this point we are not running a batch. select the appropriate
298 * data direction (read / write)
302 BUG_ON(RB_EMPTY_ROOT(&dd
->sort_list
[READ
]));
304 if (deadline_fifo_request(dd
, WRITE
) &&
305 (dd
->starved
++ >= dd
->writes_starved
))
306 goto dispatch_writes
;
310 goto dispatch_find_request
;
314 * there are either no reads or writes have been starved
319 BUG_ON(RB_EMPTY_ROOT(&dd
->sort_list
[WRITE
]));
325 goto dispatch_find_request
;
330 dispatch_find_request
:
332 * we are not running a batch, find best request for selected data_dir
334 next_rq
= deadline_next_request(dd
, data_dir
);
335 if (deadline_check_fifo(dd
, data_dir
) || !next_rq
) {
337 * A deadline has expired, the last request was in the other
338 * direction, or we have run out of higher-sectored requests.
339 * Start again from the request with the earliest expiry time.
341 rq
= deadline_fifo_request(dd
, data_dir
);
344 * The last req was the same dir and we have a next request in
345 * sort order. No expired requests so continue on from here.
351 * For a zoned block device, if we only have writes queued and none of
352 * them can be dispatched, rq will be NULL.
361 * rq is the selected appropriate request.
364 deadline_move_request(dd
, rq
);
367 * If the request needs its target zone locked, do it.
369 blk_req_zone_write_lock(rq
);
370 rq
->rq_flags
|= RQF_STARTED
;
375 * One confusing aspect here is that we get called for a specific
376 * hardware queue, but we may return a request that is for a
377 * different hardware queue. This is because mq-deadline has shared
378 * state for all hardware queues, in terms of sorting, FIFOs, etc.
380 static struct request
*dd_dispatch_request(struct blk_mq_hw_ctx
*hctx
)
382 struct deadline_data
*dd
= hctx
->queue
->elevator
->elevator_data
;
385 spin_lock(&dd
->lock
);
386 rq
= __dd_dispatch_request(dd
);
387 spin_unlock(&dd
->lock
);
392 static void dd_exit_queue(struct elevator_queue
*e
)
394 struct deadline_data
*dd
= e
->elevator_data
;
396 BUG_ON(!list_empty(&dd
->fifo_list
[READ
]));
397 BUG_ON(!list_empty(&dd
->fifo_list
[WRITE
]));
403 * initialize elevator private data (deadline_data).
405 static int dd_init_queue(struct request_queue
*q
, struct elevator_type
*e
)
407 struct deadline_data
*dd
;
408 struct elevator_queue
*eq
;
410 eq
= elevator_alloc(q
, e
);
414 dd
= kzalloc_node(sizeof(*dd
), GFP_KERNEL
, q
->node
);
416 kobject_put(&eq
->kobj
);
419 eq
->elevator_data
= dd
;
421 INIT_LIST_HEAD(&dd
->fifo_list
[READ
]);
422 INIT_LIST_HEAD(&dd
->fifo_list
[WRITE
]);
423 dd
->sort_list
[READ
] = RB_ROOT
;
424 dd
->sort_list
[WRITE
] = RB_ROOT
;
425 dd
->fifo_expire
[READ
] = read_expire
;
426 dd
->fifo_expire
[WRITE
] = write_expire
;
427 dd
->writes_starved
= writes_starved
;
428 dd
->front_merges
= 1;
429 dd
->fifo_batch
= fifo_batch
;
430 spin_lock_init(&dd
->lock
);
431 spin_lock_init(&dd
->zone_lock
);
432 INIT_LIST_HEAD(&dd
->dispatch
);
438 static int dd_request_merge(struct request_queue
*q
, struct request
**rq
,
441 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
442 sector_t sector
= bio_end_sector(bio
);
443 struct request
*__rq
;
445 if (!dd
->front_merges
)
446 return ELEVATOR_NO_MERGE
;
448 __rq
= elv_rb_find(&dd
->sort_list
[bio_data_dir(bio
)], sector
);
450 BUG_ON(sector
!= blk_rq_pos(__rq
));
452 if (elv_bio_merge_ok(__rq
, bio
)) {
454 return ELEVATOR_FRONT_MERGE
;
458 return ELEVATOR_NO_MERGE
;
461 static bool dd_bio_merge(struct blk_mq_hw_ctx
*hctx
, struct bio
*bio
)
463 struct request_queue
*q
= hctx
->queue
;
464 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
465 struct request
*free
= NULL
;
468 spin_lock(&dd
->lock
);
469 ret
= blk_mq_sched_try_merge(q
, bio
, &free
);
470 spin_unlock(&dd
->lock
);
473 blk_mq_free_request(free
);
479 * add rq to rbtree and fifo
481 static void dd_insert_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
,
484 struct request_queue
*q
= hctx
->queue
;
485 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
486 const int data_dir
= rq_data_dir(rq
);
489 * This may be a requeue of a write request that has locked its
490 * target zone. If it is the case, this releases the zone lock.
492 blk_req_zone_write_unlock(rq
);
494 if (blk_mq_sched_try_insert_merge(q
, rq
))
497 blk_mq_sched_request_inserted(rq
);
499 if (at_head
|| blk_rq_is_passthrough(rq
)) {
501 list_add(&rq
->queuelist
, &dd
->dispatch
);
503 list_add_tail(&rq
->queuelist
, &dd
->dispatch
);
505 deadline_add_rq_rb(dd
, rq
);
507 if (rq_mergeable(rq
)) {
508 elv_rqhash_add(q
, rq
);
514 * set expire time and add to fifo list
516 rq
->fifo_time
= jiffies
+ dd
->fifo_expire
[data_dir
];
517 list_add_tail(&rq
->queuelist
, &dd
->fifo_list
[data_dir
]);
521 static void dd_insert_requests(struct blk_mq_hw_ctx
*hctx
,
522 struct list_head
*list
, bool at_head
)
524 struct request_queue
*q
= hctx
->queue
;
525 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
527 spin_lock(&dd
->lock
);
528 while (!list_empty(list
)) {
531 rq
= list_first_entry(list
, struct request
, queuelist
);
532 list_del_init(&rq
->queuelist
);
533 dd_insert_request(hctx
, rq
, at_head
);
535 spin_unlock(&dd
->lock
);
539 * Nothing to do here. This is defined only to ensure that .finish_request
540 * method is called upon request completion.
542 static void dd_prepare_request(struct request
*rq
, struct bio
*bio
)
547 * For zoned block devices, write unlock the target zone of
548 * completed write requests. Do this while holding the zone lock
549 * spinlock so that the zone is never unlocked while deadline_fifo_request()
550 * or deadline_next_request() are executing. This function is called for
551 * all requests, whether or not these requests complete successfully.
553 * For a zoned block device, __dd_dispatch_request() may have stopped
554 * dispatching requests if all the queued requests are write requests directed
555 * at zones that are already locked due to on-going write requests. To ensure
556 * write request dispatch progress in this case, mark the queue as needing a
557 * restart to ensure that the queue is run again after completion of the
558 * request and zones being unlocked.
560 static void dd_finish_request(struct request
*rq
)
562 struct request_queue
*q
= rq
->q
;
564 if (blk_queue_is_zoned(q
)) {
565 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
568 spin_lock_irqsave(&dd
->zone_lock
, flags
);
569 blk_req_zone_write_unlock(rq
);
570 if (!list_empty(&dd
->fifo_list
[WRITE
])) {
571 struct blk_mq_hw_ctx
*hctx
;
573 hctx
= blk_mq_map_queue(q
, rq
->mq_ctx
->cpu
);
574 blk_mq_sched_mark_restart_hctx(hctx
);
576 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
580 static bool dd_has_work(struct blk_mq_hw_ctx
*hctx
)
582 struct deadline_data
*dd
= hctx
->queue
->elevator
->elevator_data
;
584 return !list_empty_careful(&dd
->dispatch
) ||
585 !list_empty_careful(&dd
->fifo_list
[0]) ||
586 !list_empty_careful(&dd
->fifo_list
[1]);
593 deadline_var_show(int var
, char *page
)
595 return sprintf(page
, "%d\n", var
);
599 deadline_var_store(int *var
, const char *page
)
601 char *p
= (char *) page
;
603 *var
= simple_strtol(p
, &p
, 10);
606 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
607 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
609 struct deadline_data *dd = e->elevator_data; \
610 int __data = __VAR; \
612 __data = jiffies_to_msecs(__data); \
613 return deadline_var_show(__data, (page)); \
615 SHOW_FUNCTION(deadline_read_expire_show
, dd
->fifo_expire
[READ
], 1);
616 SHOW_FUNCTION(deadline_write_expire_show
, dd
->fifo_expire
[WRITE
], 1);
617 SHOW_FUNCTION(deadline_writes_starved_show
, dd
->writes_starved
, 0);
618 SHOW_FUNCTION(deadline_front_merges_show
, dd
->front_merges
, 0);
619 SHOW_FUNCTION(deadline_fifo_batch_show
, dd
->fifo_batch
, 0);
622 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
623 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
625 struct deadline_data *dd = e->elevator_data; \
627 deadline_var_store(&__data, (page)); \
628 if (__data < (MIN)) \
630 else if (__data > (MAX)) \
633 *(__PTR) = msecs_to_jiffies(__data); \
638 STORE_FUNCTION(deadline_read_expire_store
, &dd
->fifo_expire
[READ
], 0, INT_MAX
, 1);
639 STORE_FUNCTION(deadline_write_expire_store
, &dd
->fifo_expire
[WRITE
], 0, INT_MAX
, 1);
640 STORE_FUNCTION(deadline_writes_starved_store
, &dd
->writes_starved
, INT_MIN
, INT_MAX
, 0);
641 STORE_FUNCTION(deadline_front_merges_store
, &dd
->front_merges
, 0, 1, 0);
642 STORE_FUNCTION(deadline_fifo_batch_store
, &dd
->fifo_batch
, 0, INT_MAX
, 0);
643 #undef STORE_FUNCTION
645 #define DD_ATTR(name) \
646 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
648 static struct elv_fs_entry deadline_attrs
[] = {
649 DD_ATTR(read_expire
),
650 DD_ATTR(write_expire
),
651 DD_ATTR(writes_starved
),
652 DD_ATTR(front_merges
),
657 #ifdef CONFIG_BLK_DEBUG_FS
658 #define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \
659 static void *deadline_##name##_fifo_start(struct seq_file *m, \
661 __acquires(&dd->lock) \
663 struct request_queue *q = m->private; \
664 struct deadline_data *dd = q->elevator->elevator_data; \
666 spin_lock(&dd->lock); \
667 return seq_list_start(&dd->fifo_list[ddir], *pos); \
670 static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
673 struct request_queue *q = m->private; \
674 struct deadline_data *dd = q->elevator->elevator_data; \
676 return seq_list_next(v, &dd->fifo_list[ddir], pos); \
679 static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
680 __releases(&dd->lock) \
682 struct request_queue *q = m->private; \
683 struct deadline_data *dd = q->elevator->elevator_data; \
685 spin_unlock(&dd->lock); \
688 static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
689 .start = deadline_##name##_fifo_start, \
690 .next = deadline_##name##_fifo_next, \
691 .stop = deadline_##name##_fifo_stop, \
692 .show = blk_mq_debugfs_rq_show, \
695 static int deadline_##name##_next_rq_show(void *data, \
696 struct seq_file *m) \
698 struct request_queue *q = data; \
699 struct deadline_data *dd = q->elevator->elevator_data; \
700 struct request *rq = dd->next_rq[ddir]; \
703 __blk_mq_debugfs_rq_show(m, rq); \
706 DEADLINE_DEBUGFS_DDIR_ATTRS(READ
, read
)
707 DEADLINE_DEBUGFS_DDIR_ATTRS(WRITE
, write
)
708 #undef DEADLINE_DEBUGFS_DDIR_ATTRS
710 static int deadline_batching_show(void *data
, struct seq_file
*m
)
712 struct request_queue
*q
= data
;
713 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
715 seq_printf(m
, "%u\n", dd
->batching
);
719 static int deadline_starved_show(void *data
, struct seq_file
*m
)
721 struct request_queue
*q
= data
;
722 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
724 seq_printf(m
, "%u\n", dd
->starved
);
728 static void *deadline_dispatch_start(struct seq_file
*m
, loff_t
*pos
)
729 __acquires(&dd
->lock
)
731 struct request_queue
*q
= m
->private;
732 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
734 spin_lock(&dd
->lock
);
735 return seq_list_start(&dd
->dispatch
, *pos
);
738 static void *deadline_dispatch_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
740 struct request_queue
*q
= m
->private;
741 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
743 return seq_list_next(v
, &dd
->dispatch
, pos
);
746 static void deadline_dispatch_stop(struct seq_file
*m
, void *v
)
747 __releases(&dd
->lock
)
749 struct request_queue
*q
= m
->private;
750 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
752 spin_unlock(&dd
->lock
);
755 static const struct seq_operations deadline_dispatch_seq_ops
= {
756 .start
= deadline_dispatch_start
,
757 .next
= deadline_dispatch_next
,
758 .stop
= deadline_dispatch_stop
,
759 .show
= blk_mq_debugfs_rq_show
,
762 #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
763 {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \
764 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
765 static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs
[] = {
766 DEADLINE_QUEUE_DDIR_ATTRS(read
),
767 DEADLINE_QUEUE_DDIR_ATTRS(write
),
768 {"batching", 0400, deadline_batching_show
},
769 {"starved", 0400, deadline_starved_show
},
770 {"dispatch", 0400, .seq_ops
= &deadline_dispatch_seq_ops
},
773 #undef DEADLINE_QUEUE_DDIR_ATTRS
776 static struct elevator_type mq_deadline
= {
778 .insert_requests
= dd_insert_requests
,
779 .dispatch_request
= dd_dispatch_request
,
780 .prepare_request
= dd_prepare_request
,
781 .finish_request
= dd_finish_request
,
782 .next_request
= elv_rb_latter_request
,
783 .former_request
= elv_rb_former_request
,
784 .bio_merge
= dd_bio_merge
,
785 .request_merge
= dd_request_merge
,
786 .requests_merged
= dd_merged_requests
,
787 .request_merged
= dd_request_merged
,
788 .has_work
= dd_has_work
,
789 .init_sched
= dd_init_queue
,
790 .exit_sched
= dd_exit_queue
,
794 #ifdef CONFIG_BLK_DEBUG_FS
795 .queue_debugfs_attrs
= deadline_queue_debugfs_attrs
,
797 .elevator_attrs
= deadline_attrs
,
798 .elevator_name
= "mq-deadline",
799 .elevator_alias
= "deadline",
800 .elevator_owner
= THIS_MODULE
,
802 MODULE_ALIAS("mq-deadline-iosched");
804 static int __init
deadline_init(void)
806 return elv_register(&mq_deadline
);
809 static void __exit
deadline_exit(void)
811 elv_unregister(&mq_deadline
);
814 module_init(deadline_init
);
815 module_exit(deadline_exit
);
817 MODULE_AUTHOR("Jens Axboe");
818 MODULE_LICENSE("GPL");
819 MODULE_DESCRIPTION("MQ deadline IO scheduler");