1 // SPDX-License-Identifier: GPL-2.0
3 * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler,
4 * for the blk-mq scheduling framework
6 * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk>
8 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/blk-mq.h>
12 #include <linux/elevator.h>
13 #include <linux/bio.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/compiler.h>
18 #include <linux/rbtree.h>
19 #include <linux/sbitmap.h>
23 #include "blk-mq-debugfs.h"
24 #include "blk-mq-tag.h"
25 #include "blk-mq-sched.h"
28 * See Documentation/block/deadline-iosched.rst
30 static const int read_expire
= HZ
/ 2; /* max time before a read is submitted. */
31 static const int write_expire
= 5 * HZ
; /* ditto for writes, these limits are SOFT! */
32 static const int writes_starved
= 2; /* max times reads can starve a write */
33 static const int fifo_batch
= 16; /* # of sequential requests treated as one
34 by the above parameters. For throughput. */
36 struct deadline_data
{
42 * requests (deadline_rq s) are present on both sort_list and fifo_list
44 struct rb_root sort_list
[2];
45 struct list_head fifo_list
[2];
48 * next in sort order. read, write or both are NULL
50 struct request
*next_rq
[2];
51 unsigned int batching
; /* number of sequential requests made */
52 unsigned int starved
; /* times reads have starved writes */
55 * settings that change how the i/o scheduler behaves
64 struct list_head dispatch
;
67 static inline struct rb_root
*
68 deadline_rb_root(struct deadline_data
*dd
, struct request
*rq
)
70 return &dd
->sort_list
[rq_data_dir(rq
)];
74 * get the request after `rq' in sector-sorted order
76 static inline struct request
*
77 deadline_latter_request(struct request
*rq
)
79 struct rb_node
*node
= rb_next(&rq
->rb_node
);
82 return rb_entry_rq(node
);
88 deadline_add_rq_rb(struct deadline_data
*dd
, struct request
*rq
)
90 struct rb_root
*root
= deadline_rb_root(dd
, rq
);
96 deadline_del_rq_rb(struct deadline_data
*dd
, struct request
*rq
)
98 const int data_dir
= rq_data_dir(rq
);
100 if (dd
->next_rq
[data_dir
] == rq
)
101 dd
->next_rq
[data_dir
] = deadline_latter_request(rq
);
103 elv_rb_del(deadline_rb_root(dd
, rq
), rq
);
107 * remove rq from rbtree and fifo.
109 static void deadline_remove_request(struct request_queue
*q
, struct request
*rq
)
111 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
113 list_del_init(&rq
->queuelist
);
116 * We might not be on the rbtree, if we are doing an insert merge
118 if (!RB_EMPTY_NODE(&rq
->rb_node
))
119 deadline_del_rq_rb(dd
, rq
);
121 elv_rqhash_del(q
, rq
);
122 if (q
->last_merge
== rq
)
123 q
->last_merge
= NULL
;
126 static void dd_request_merged(struct request_queue
*q
, struct request
*req
,
129 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
132 * if the merge was a front merge, we need to reposition request
134 if (type
== ELEVATOR_FRONT_MERGE
) {
135 elv_rb_del(deadline_rb_root(dd
, req
), req
);
136 deadline_add_rq_rb(dd
, req
);
140 static void dd_merged_requests(struct request_queue
*q
, struct request
*req
,
141 struct request
*next
)
144 * if next expires before rq, assign its expire time to rq
145 * and move into next position (next will be deleted) in fifo
147 if (!list_empty(&req
->queuelist
) && !list_empty(&next
->queuelist
)) {
148 if (time_before((unsigned long)next
->fifo_time
,
149 (unsigned long)req
->fifo_time
)) {
150 list_move(&req
->queuelist
, &next
->queuelist
);
151 req
->fifo_time
= next
->fifo_time
;
156 * kill knowledge of next, this one is a goner
158 deadline_remove_request(q
, next
);
162 * move an entry to dispatch queue
165 deadline_move_request(struct deadline_data
*dd
, struct request
*rq
)
167 const int data_dir
= rq_data_dir(rq
);
169 dd
->next_rq
[READ
] = NULL
;
170 dd
->next_rq
[WRITE
] = NULL
;
171 dd
->next_rq
[data_dir
] = deadline_latter_request(rq
);
174 * take it off the sort and fifo list
176 deadline_remove_request(rq
->q
, rq
);
180 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
181 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
183 static inline int deadline_check_fifo(struct deadline_data
*dd
, int ddir
)
185 struct request
*rq
= rq_entry_fifo(dd
->fifo_list
[ddir
].next
);
190 if (time_after_eq(jiffies
, (unsigned long)rq
->fifo_time
))
197 * For the specified data direction, return the next request to
198 * dispatch using arrival ordered lists.
200 static struct request
*
201 deadline_fifo_request(struct deadline_data
*dd
, int data_dir
)
206 if (WARN_ON_ONCE(data_dir
!= READ
&& data_dir
!= WRITE
))
209 if (list_empty(&dd
->fifo_list
[data_dir
]))
212 rq
= rq_entry_fifo(dd
->fifo_list
[data_dir
].next
);
213 if (data_dir
== READ
|| !blk_queue_is_zoned(rq
->q
))
217 * Look for a write request that can be dispatched, that is one with
218 * an unlocked target zone.
220 spin_lock_irqsave(&dd
->zone_lock
, flags
);
221 list_for_each_entry(rq
, &dd
->fifo_list
[WRITE
], queuelist
) {
222 if (blk_req_can_dispatch_to_zone(rq
))
227 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
233 * For the specified data direction, return the next request to
234 * dispatch using sector position sorted lists.
236 static struct request
*
237 deadline_next_request(struct deadline_data
*dd
, int data_dir
)
242 if (WARN_ON_ONCE(data_dir
!= READ
&& data_dir
!= WRITE
))
245 rq
= dd
->next_rq
[data_dir
];
249 if (data_dir
== READ
|| !blk_queue_is_zoned(rq
->q
))
253 * Look for a write request that can be dispatched, that is one with
254 * an unlocked target zone.
256 spin_lock_irqsave(&dd
->zone_lock
, flags
);
258 if (blk_req_can_dispatch_to_zone(rq
))
260 rq
= deadline_latter_request(rq
);
262 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
268 * deadline_dispatch_requests selects the best request according to
269 * read/write expire, fifo_batch, etc
271 static struct request
*__dd_dispatch_request(struct deadline_data
*dd
)
273 struct request
*rq
, *next_rq
;
277 if (!list_empty(&dd
->dispatch
)) {
278 rq
= list_first_entry(&dd
->dispatch
, struct request
, queuelist
);
279 list_del_init(&rq
->queuelist
);
283 reads
= !list_empty(&dd
->fifo_list
[READ
]);
284 writes
= !list_empty(&dd
->fifo_list
[WRITE
]);
287 * batches are currently reads XOR writes
289 rq
= deadline_next_request(dd
, WRITE
);
291 rq
= deadline_next_request(dd
, READ
);
293 if (rq
&& dd
->batching
< dd
->fifo_batch
)
294 /* we have a next request are still entitled to batch */
295 goto dispatch_request
;
298 * at this point we are not running a batch. select the appropriate
299 * data direction (read / write)
303 BUG_ON(RB_EMPTY_ROOT(&dd
->sort_list
[READ
]));
305 if (deadline_fifo_request(dd
, WRITE
) &&
306 (dd
->starved
++ >= dd
->writes_starved
))
307 goto dispatch_writes
;
311 goto dispatch_find_request
;
315 * there are either no reads or writes have been starved
320 BUG_ON(RB_EMPTY_ROOT(&dd
->sort_list
[WRITE
]));
326 goto dispatch_find_request
;
331 dispatch_find_request
:
333 * we are not running a batch, find best request for selected data_dir
335 next_rq
= deadline_next_request(dd
, data_dir
);
336 if (deadline_check_fifo(dd
, data_dir
) || !next_rq
) {
338 * A deadline has expired, the last request was in the other
339 * direction, or we have run out of higher-sectored requests.
340 * Start again from the request with the earliest expiry time.
342 rq
= deadline_fifo_request(dd
, data_dir
);
345 * The last req was the same dir and we have a next request in
346 * sort order. No expired requests so continue on from here.
352 * For a zoned block device, if we only have writes queued and none of
353 * them can be dispatched, rq will be NULL.
362 * rq is the selected appropriate request.
365 deadline_move_request(dd
, rq
);
368 * If the request needs its target zone locked, do it.
370 blk_req_zone_write_lock(rq
);
371 rq
->rq_flags
|= RQF_STARTED
;
376 * One confusing aspect here is that we get called for a specific
377 * hardware queue, but we may return a request that is for a
378 * different hardware queue. This is because mq-deadline has shared
379 * state for all hardware queues, in terms of sorting, FIFOs, etc.
381 * For a zoned block device, __dd_dispatch_request() may return NULL
382 * if all the queued write requests are directed at zones that are already
383 * locked due to on-going write requests. In this case, make sure to mark
384 * the queue as needing a restart to ensure that the queue is run again
385 * and the pending writes dispatched once the target zones for the ongoing
386 * write requests are unlocked in dd_finish_request().
388 static struct request
*dd_dispatch_request(struct blk_mq_hw_ctx
*hctx
)
390 struct deadline_data
*dd
= hctx
->queue
->elevator
->elevator_data
;
393 spin_lock(&dd
->lock
);
394 rq
= __dd_dispatch_request(dd
);
395 if (!rq
&& blk_queue_is_zoned(hctx
->queue
) &&
396 !list_empty(&dd
->fifo_list
[WRITE
]))
397 blk_mq_sched_mark_restart_hctx(hctx
);
398 spin_unlock(&dd
->lock
);
403 static void dd_exit_queue(struct elevator_queue
*e
)
405 struct deadline_data
*dd
= e
->elevator_data
;
407 BUG_ON(!list_empty(&dd
->fifo_list
[READ
]));
408 BUG_ON(!list_empty(&dd
->fifo_list
[WRITE
]));
414 * initialize elevator private data (deadline_data).
416 static int dd_init_queue(struct request_queue
*q
, struct elevator_type
*e
)
418 struct deadline_data
*dd
;
419 struct elevator_queue
*eq
;
421 eq
= elevator_alloc(q
, e
);
425 dd
= kzalloc_node(sizeof(*dd
), GFP_KERNEL
, q
->node
);
427 kobject_put(&eq
->kobj
);
430 eq
->elevator_data
= dd
;
432 INIT_LIST_HEAD(&dd
->fifo_list
[READ
]);
433 INIT_LIST_HEAD(&dd
->fifo_list
[WRITE
]);
434 dd
->sort_list
[READ
] = RB_ROOT
;
435 dd
->sort_list
[WRITE
] = RB_ROOT
;
436 dd
->fifo_expire
[READ
] = read_expire
;
437 dd
->fifo_expire
[WRITE
] = write_expire
;
438 dd
->writes_starved
= writes_starved
;
439 dd
->front_merges
= 1;
440 dd
->fifo_batch
= fifo_batch
;
441 spin_lock_init(&dd
->lock
);
442 spin_lock_init(&dd
->zone_lock
);
443 INIT_LIST_HEAD(&dd
->dispatch
);
449 static int dd_request_merge(struct request_queue
*q
, struct request
**rq
,
452 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
453 sector_t sector
= bio_end_sector(bio
);
454 struct request
*__rq
;
456 if (!dd
->front_merges
)
457 return ELEVATOR_NO_MERGE
;
459 __rq
= elv_rb_find(&dd
->sort_list
[bio_data_dir(bio
)], sector
);
461 BUG_ON(sector
!= blk_rq_pos(__rq
));
463 if (elv_bio_merge_ok(__rq
, bio
)) {
465 return ELEVATOR_FRONT_MERGE
;
469 return ELEVATOR_NO_MERGE
;
472 static bool dd_bio_merge(struct blk_mq_hw_ctx
*hctx
, struct bio
*bio
,
473 unsigned int nr_segs
)
475 struct request_queue
*q
= hctx
->queue
;
476 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
477 struct request
*free
= NULL
;
480 spin_lock(&dd
->lock
);
481 ret
= blk_mq_sched_try_merge(q
, bio
, nr_segs
, &free
);
482 spin_unlock(&dd
->lock
);
485 blk_mq_free_request(free
);
491 * add rq to rbtree and fifo
493 static void dd_insert_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
,
496 struct request_queue
*q
= hctx
->queue
;
497 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
498 const int data_dir
= rq_data_dir(rq
);
501 * This may be a requeue of a write request that has locked its
502 * target zone. If it is the case, this releases the zone lock.
504 blk_req_zone_write_unlock(rq
);
506 if (blk_mq_sched_try_insert_merge(q
, rq
))
509 blk_mq_sched_request_inserted(rq
);
511 if (at_head
|| blk_rq_is_passthrough(rq
)) {
513 list_add(&rq
->queuelist
, &dd
->dispatch
);
515 list_add_tail(&rq
->queuelist
, &dd
->dispatch
);
517 deadline_add_rq_rb(dd
, rq
);
519 if (rq_mergeable(rq
)) {
520 elv_rqhash_add(q
, rq
);
526 * set expire time and add to fifo list
528 rq
->fifo_time
= jiffies
+ dd
->fifo_expire
[data_dir
];
529 list_add_tail(&rq
->queuelist
, &dd
->fifo_list
[data_dir
]);
533 static void dd_insert_requests(struct blk_mq_hw_ctx
*hctx
,
534 struct list_head
*list
, bool at_head
)
536 struct request_queue
*q
= hctx
->queue
;
537 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
539 spin_lock(&dd
->lock
);
540 while (!list_empty(list
)) {
543 rq
= list_first_entry(list
, struct request
, queuelist
);
544 list_del_init(&rq
->queuelist
);
545 dd_insert_request(hctx
, rq
, at_head
);
547 spin_unlock(&dd
->lock
);
551 * Nothing to do here. This is defined only to ensure that .finish_request
552 * method is called upon request completion.
554 static void dd_prepare_request(struct request
*rq
, struct bio
*bio
)
559 * For zoned block devices, write unlock the target zone of
560 * completed write requests. Do this while holding the zone lock
561 * spinlock so that the zone is never unlocked while deadline_fifo_request()
562 * or deadline_next_request() are executing. This function is called for
563 * all requests, whether or not these requests complete successfully.
565 static void dd_finish_request(struct request
*rq
)
567 struct request_queue
*q
= rq
->q
;
569 if (blk_queue_is_zoned(q
)) {
570 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
573 spin_lock_irqsave(&dd
->zone_lock
, flags
);
574 blk_req_zone_write_unlock(rq
);
575 spin_unlock_irqrestore(&dd
->zone_lock
, flags
);
579 static bool dd_has_work(struct blk_mq_hw_ctx
*hctx
)
581 struct deadline_data
*dd
= hctx
->queue
->elevator
->elevator_data
;
583 return !list_empty_careful(&dd
->dispatch
) ||
584 !list_empty_careful(&dd
->fifo_list
[0]) ||
585 !list_empty_careful(&dd
->fifo_list
[1]);
592 deadline_var_show(int var
, char *page
)
594 return sprintf(page
, "%d\n", var
);
598 deadline_var_store(int *var
, const char *page
)
600 char *p
= (char *) page
;
602 *var
= simple_strtol(p
, &p
, 10);
605 #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
606 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
608 struct deadline_data *dd = e->elevator_data; \
609 int __data = __VAR; \
611 __data = jiffies_to_msecs(__data); \
612 return deadline_var_show(__data, (page)); \
614 SHOW_FUNCTION(deadline_read_expire_show
, dd
->fifo_expire
[READ
], 1);
615 SHOW_FUNCTION(deadline_write_expire_show
, dd
->fifo_expire
[WRITE
], 1);
616 SHOW_FUNCTION(deadline_writes_starved_show
, dd
->writes_starved
, 0);
617 SHOW_FUNCTION(deadline_front_merges_show
, dd
->front_merges
, 0);
618 SHOW_FUNCTION(deadline_fifo_batch_show
, dd
->fifo_batch
, 0);
621 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
622 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
624 struct deadline_data *dd = e->elevator_data; \
626 deadline_var_store(&__data, (page)); \
627 if (__data < (MIN)) \
629 else if (__data > (MAX)) \
632 *(__PTR) = msecs_to_jiffies(__data); \
637 STORE_FUNCTION(deadline_read_expire_store
, &dd
->fifo_expire
[READ
], 0, INT_MAX
, 1);
638 STORE_FUNCTION(deadline_write_expire_store
, &dd
->fifo_expire
[WRITE
], 0, INT_MAX
, 1);
639 STORE_FUNCTION(deadline_writes_starved_store
, &dd
->writes_starved
, INT_MIN
, INT_MAX
, 0);
640 STORE_FUNCTION(deadline_front_merges_store
, &dd
->front_merges
, 0, 1, 0);
641 STORE_FUNCTION(deadline_fifo_batch_store
, &dd
->fifo_batch
, 0, INT_MAX
, 0);
642 #undef STORE_FUNCTION
644 #define DD_ATTR(name) \
645 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
647 static struct elv_fs_entry deadline_attrs
[] = {
648 DD_ATTR(read_expire
),
649 DD_ATTR(write_expire
),
650 DD_ATTR(writes_starved
),
651 DD_ATTR(front_merges
),
656 #ifdef CONFIG_BLK_DEBUG_FS
657 #define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \
658 static void *deadline_##name##_fifo_start(struct seq_file *m, \
660 __acquires(&dd->lock) \
662 struct request_queue *q = m->private; \
663 struct deadline_data *dd = q->elevator->elevator_data; \
665 spin_lock(&dd->lock); \
666 return seq_list_start(&dd->fifo_list[ddir], *pos); \
669 static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
672 struct request_queue *q = m->private; \
673 struct deadline_data *dd = q->elevator->elevator_data; \
675 return seq_list_next(v, &dd->fifo_list[ddir], pos); \
678 static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
679 __releases(&dd->lock) \
681 struct request_queue *q = m->private; \
682 struct deadline_data *dd = q->elevator->elevator_data; \
684 spin_unlock(&dd->lock); \
687 static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
688 .start = deadline_##name##_fifo_start, \
689 .next = deadline_##name##_fifo_next, \
690 .stop = deadline_##name##_fifo_stop, \
691 .show = blk_mq_debugfs_rq_show, \
694 static int deadline_##name##_next_rq_show(void *data, \
695 struct seq_file *m) \
697 struct request_queue *q = data; \
698 struct deadline_data *dd = q->elevator->elevator_data; \
699 struct request *rq = dd->next_rq[ddir]; \
702 __blk_mq_debugfs_rq_show(m, rq); \
705 DEADLINE_DEBUGFS_DDIR_ATTRS(READ
, read
)
706 DEADLINE_DEBUGFS_DDIR_ATTRS(WRITE
, write
)
707 #undef DEADLINE_DEBUGFS_DDIR_ATTRS
709 static int deadline_batching_show(void *data
, struct seq_file
*m
)
711 struct request_queue
*q
= data
;
712 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
714 seq_printf(m
, "%u\n", dd
->batching
);
718 static int deadline_starved_show(void *data
, struct seq_file
*m
)
720 struct request_queue
*q
= data
;
721 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
723 seq_printf(m
, "%u\n", dd
->starved
);
727 static void *deadline_dispatch_start(struct seq_file
*m
, loff_t
*pos
)
728 __acquires(&dd
->lock
)
730 struct request_queue
*q
= m
->private;
731 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
733 spin_lock(&dd
->lock
);
734 return seq_list_start(&dd
->dispatch
, *pos
);
737 static void *deadline_dispatch_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
739 struct request_queue
*q
= m
->private;
740 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
742 return seq_list_next(v
, &dd
->dispatch
, pos
);
745 static void deadline_dispatch_stop(struct seq_file
*m
, void *v
)
746 __releases(&dd
->lock
)
748 struct request_queue
*q
= m
->private;
749 struct deadline_data
*dd
= q
->elevator
->elevator_data
;
751 spin_unlock(&dd
->lock
);
754 static const struct seq_operations deadline_dispatch_seq_ops
= {
755 .start
= deadline_dispatch_start
,
756 .next
= deadline_dispatch_next
,
757 .stop
= deadline_dispatch_stop
,
758 .show
= blk_mq_debugfs_rq_show
,
761 #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
762 {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \
763 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
764 static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs
[] = {
765 DEADLINE_QUEUE_DDIR_ATTRS(read
),
766 DEADLINE_QUEUE_DDIR_ATTRS(write
),
767 {"batching", 0400, deadline_batching_show
},
768 {"starved", 0400, deadline_starved_show
},
769 {"dispatch", 0400, .seq_ops
= &deadline_dispatch_seq_ops
},
772 #undef DEADLINE_QUEUE_DDIR_ATTRS
775 static struct elevator_type mq_deadline
= {
777 .insert_requests
= dd_insert_requests
,
778 .dispatch_request
= dd_dispatch_request
,
779 .prepare_request
= dd_prepare_request
,
780 .finish_request
= dd_finish_request
,
781 .next_request
= elv_rb_latter_request
,
782 .former_request
= elv_rb_former_request
,
783 .bio_merge
= dd_bio_merge
,
784 .request_merge
= dd_request_merge
,
785 .requests_merged
= dd_merged_requests
,
786 .request_merged
= dd_request_merged
,
787 .has_work
= dd_has_work
,
788 .init_sched
= dd_init_queue
,
789 .exit_sched
= dd_exit_queue
,
792 #ifdef CONFIG_BLK_DEBUG_FS
793 .queue_debugfs_attrs
= deadline_queue_debugfs_attrs
,
795 .elevator_attrs
= deadline_attrs
,
796 .elevator_name
= "mq-deadline",
797 .elevator_alias
= "deadline",
798 .elevator_owner
= THIS_MODULE
,
800 MODULE_ALIAS("mq-deadline-iosched");
802 static int __init
deadline_init(void)
804 return elv_register(&mq_deadline
);
807 static void __exit
deadline_exit(void)
809 elv_unregister(&mq_deadline
);
812 module_init(deadline_init
);
813 module_exit(deadline_exit
);
815 MODULE_AUTHOR("Jens Axboe");
816 MODULE_LICENSE("GPL");
817 MODULE_DESCRIPTION("MQ deadline IO scheduler");