2 * Block device elevator/IO-scheduler.
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * 30042000 Jens Axboe <axboe@kernel.dk> :
8 * Split the elevator a bit so that it is possible to choose a different
9 * one or even write a new "plug in". There are three pieces:
10 * - elevator_fn, inserts a new request in the queue list
11 * - elevator_merge_fn, decides whether a new buffer can be merged with
13 * - elevator_dequeue_fn, called when a request is taken off the active list
15 * 20082000 Dave Jones <davej@suse.de> :
16 * Removed tests for max-bomb-segments, which was breaking elvtune
17 * when run without -bN
20 * - Rework again to work with bio instead of buffer_heads
21 * - loose bi_dev comparisons, partition handling is right now
22 * - completely modularize elevator setup and teardown
25 #include <linux/kernel.h>
27 #include <linux/blkdev.h>
28 #include <linux/elevator.h>
29 #include <linux/bio.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/compiler.h>
34 #include <linux/blktrace_api.h>
35 #include <linux/hash.h>
36 #include <linux/uaccess.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/blk-cgroup.h>
40 #include <trace/events/block.h>
43 #include "blk-mq-sched.h"
46 static DEFINE_SPINLOCK(elv_list_lock
);
47 static LIST_HEAD(elv_list
);
52 #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
55 * Query io scheduler to see if the current process issuing bio may be
58 static int elv_iosched_allow_bio_merge(struct request
*rq
, struct bio
*bio
)
60 struct request_queue
*q
= rq
->q
;
61 struct elevator_queue
*e
= q
->elevator
;
63 if (e
->uses_mq
&& e
->type
->ops
.mq
.allow_merge
)
64 return e
->type
->ops
.mq
.allow_merge(q
, rq
, bio
);
65 else if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_allow_bio_merge_fn
)
66 return e
->type
->ops
.sq
.elevator_allow_bio_merge_fn(q
, rq
, bio
);
72 * can we safely merge with this request?
74 bool elv_bio_merge_ok(struct request
*rq
, struct bio
*bio
)
76 if (!blk_rq_merge_ok(rq
, bio
))
79 if (!elv_iosched_allow_bio_merge(rq
, bio
))
84 EXPORT_SYMBOL(elv_bio_merge_ok
);
87 * Return scheduler with name 'name' and with matching 'mq capability
89 static struct elevator_type
*elevator_find(const char *name
, bool mq
)
91 struct elevator_type
*e
;
93 list_for_each_entry(e
, &elv_list
, list
) {
94 if (!strcmp(e
->elevator_name
, name
) && (mq
== e
->uses_mq
))
101 static void elevator_put(struct elevator_type
*e
)
103 module_put(e
->elevator_owner
);
106 static struct elevator_type
*elevator_get(struct request_queue
*q
,
107 const char *name
, bool try_loading
)
109 struct elevator_type
*e
;
111 spin_lock(&elv_list_lock
);
113 e
= elevator_find(name
, q
->mq_ops
!= NULL
);
114 if (!e
&& try_loading
) {
115 spin_unlock(&elv_list_lock
);
116 request_module("%s-iosched", name
);
117 spin_lock(&elv_list_lock
);
118 e
= elevator_find(name
, q
->mq_ops
!= NULL
);
121 if (e
&& !try_module_get(e
->elevator_owner
))
124 spin_unlock(&elv_list_lock
);
128 static char chosen_elevator
[ELV_NAME_MAX
];
130 static int __init
elevator_setup(char *str
)
133 * Be backwards-compatible with previous kernels, so users
134 * won't get the wrong elevator.
136 strncpy(chosen_elevator
, str
, sizeof(chosen_elevator
) - 1);
140 __setup("elevator=", elevator_setup
);
142 /* called during boot to load the elevator chosen by the elevator param */
143 void __init
load_default_elevator_module(void)
145 struct elevator_type
*e
;
147 if (!chosen_elevator
[0])
151 * Boot parameter is deprecated, we haven't supported that for MQ.
152 * Only look for non-mq schedulers from here.
154 spin_lock(&elv_list_lock
);
155 e
= elevator_find(chosen_elevator
, false);
156 spin_unlock(&elv_list_lock
);
159 request_module("%s-iosched", chosen_elevator
);
162 static struct kobj_type elv_ktype
;
164 struct elevator_queue
*elevator_alloc(struct request_queue
*q
,
165 struct elevator_type
*e
)
167 struct elevator_queue
*eq
;
169 eq
= kzalloc_node(sizeof(*eq
), GFP_KERNEL
, q
->node
);
174 kobject_init(&eq
->kobj
, &elv_ktype
);
175 mutex_init(&eq
->sysfs_lock
);
177 eq
->uses_mq
= e
->uses_mq
;
181 EXPORT_SYMBOL(elevator_alloc
);
183 static void elevator_release(struct kobject
*kobj
)
185 struct elevator_queue
*e
;
187 e
= container_of(kobj
, struct elevator_queue
, kobj
);
188 elevator_put(e
->type
);
192 int elevator_init(struct request_queue
*q
, char *name
)
194 struct elevator_type
*e
= NULL
;
198 * q->sysfs_lock must be held to provide mutual exclusion between
199 * elevator_switch() and here.
201 lockdep_assert_held(&q
->sysfs_lock
);
203 if (unlikely(q
->elevator
))
206 INIT_LIST_HEAD(&q
->queue_head
);
207 q
->last_merge
= NULL
;
209 q
->boundary_rq
= NULL
;
212 e
= elevator_get(q
, name
, true);
218 * Use the default elevator specified by config boot param for
219 * non-mq devices, or by config option. Don't try to load modules
220 * as we could be running off async and request_module() isn't
221 * allowed from async.
223 if (!e
&& !q
->mq_ops
&& *chosen_elevator
) {
224 e
= elevator_get(q
, chosen_elevator
, false);
226 printk(KERN_ERR
"I/O scheduler %s not found\n",
232 * For blk-mq devices, we default to using mq-deadline,
233 * if available, for single queue devices. If deadline
234 * isn't available OR we have multiple queues, default
238 if (q
->nr_hw_queues
== 1)
239 e
= elevator_get(q
, "mq-deadline", false);
243 e
= elevator_get(q
, CONFIG_DEFAULT_IOSCHED
, false);
247 "Default I/O scheduler not found. " \
249 e
= elevator_get(q
, "noop", false);
254 err
= blk_mq_init_sched(q
, e
);
256 err
= e
->ops
.sq
.elevator_init_fn(q
, e
);
261 EXPORT_SYMBOL(elevator_init
);
263 void elevator_exit(struct request_queue
*q
, struct elevator_queue
*e
)
265 mutex_lock(&e
->sysfs_lock
);
266 if (e
->uses_mq
&& e
->type
->ops
.mq
.exit_sched
)
267 blk_mq_exit_sched(q
, e
);
268 else if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_exit_fn
)
269 e
->type
->ops
.sq
.elevator_exit_fn(e
);
270 mutex_unlock(&e
->sysfs_lock
);
272 kobject_put(&e
->kobj
);
274 EXPORT_SYMBOL(elevator_exit
);
276 static inline void __elv_rqhash_del(struct request
*rq
)
279 rq
->rq_flags
&= ~RQF_HASHED
;
282 void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
)
285 __elv_rqhash_del(rq
);
287 EXPORT_SYMBOL_GPL(elv_rqhash_del
);
289 void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
)
291 struct elevator_queue
*e
= q
->elevator
;
293 BUG_ON(ELV_ON_HASH(rq
));
294 hash_add(e
->hash
, &rq
->hash
, rq_hash_key(rq
));
295 rq
->rq_flags
|= RQF_HASHED
;
297 EXPORT_SYMBOL_GPL(elv_rqhash_add
);
299 void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
)
301 __elv_rqhash_del(rq
);
302 elv_rqhash_add(q
, rq
);
305 struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
)
307 struct elevator_queue
*e
= q
->elevator
;
308 struct hlist_node
*next
;
311 hash_for_each_possible_safe(e
->hash
, rq
, next
, hash
, offset
) {
312 BUG_ON(!ELV_ON_HASH(rq
));
314 if (unlikely(!rq_mergeable(rq
))) {
315 __elv_rqhash_del(rq
);
319 if (rq_hash_key(rq
) == offset
)
327 * RB-tree support functions for inserting/lookup/removal of requests
328 * in a sorted RB tree.
330 void elv_rb_add(struct rb_root
*root
, struct request
*rq
)
332 struct rb_node
**p
= &root
->rb_node
;
333 struct rb_node
*parent
= NULL
;
334 struct request
*__rq
;
338 __rq
= rb_entry(parent
, struct request
, rb_node
);
340 if (blk_rq_pos(rq
) < blk_rq_pos(__rq
))
342 else if (blk_rq_pos(rq
) >= blk_rq_pos(__rq
))
346 rb_link_node(&rq
->rb_node
, parent
, p
);
347 rb_insert_color(&rq
->rb_node
, root
);
349 EXPORT_SYMBOL(elv_rb_add
);
351 void elv_rb_del(struct rb_root
*root
, struct request
*rq
)
353 BUG_ON(RB_EMPTY_NODE(&rq
->rb_node
));
354 rb_erase(&rq
->rb_node
, root
);
355 RB_CLEAR_NODE(&rq
->rb_node
);
357 EXPORT_SYMBOL(elv_rb_del
);
359 struct request
*elv_rb_find(struct rb_root
*root
, sector_t sector
)
361 struct rb_node
*n
= root
->rb_node
;
365 rq
= rb_entry(n
, struct request
, rb_node
);
367 if (sector
< blk_rq_pos(rq
))
369 else if (sector
> blk_rq_pos(rq
))
377 EXPORT_SYMBOL(elv_rb_find
);
380 * Insert rq into dispatch queue of q. Queue lock must be held on
381 * entry. rq is sort instead into the dispatch queue. To be used by
382 * specific elevators.
384 void elv_dispatch_sort(struct request_queue
*q
, struct request
*rq
)
387 struct list_head
*entry
;
389 if (q
->last_merge
== rq
)
390 q
->last_merge
= NULL
;
392 elv_rqhash_del(q
, rq
);
396 boundary
= q
->end_sector
;
397 list_for_each_prev(entry
, &q
->queue_head
) {
398 struct request
*pos
= list_entry_rq(entry
);
400 if (req_op(rq
) != req_op(pos
))
402 if (rq_data_dir(rq
) != rq_data_dir(pos
))
404 if (pos
->rq_flags
& (RQF_STARTED
| RQF_SOFTBARRIER
))
406 if (blk_rq_pos(rq
) >= boundary
) {
407 if (blk_rq_pos(pos
) < boundary
)
410 if (blk_rq_pos(pos
) >= boundary
)
413 if (blk_rq_pos(rq
) >= blk_rq_pos(pos
))
417 list_add(&rq
->queuelist
, entry
);
419 EXPORT_SYMBOL(elv_dispatch_sort
);
422 * Insert rq into dispatch queue of q. Queue lock must be held on
423 * entry. rq is added to the back of the dispatch queue. To be used by
424 * specific elevators.
426 void elv_dispatch_add_tail(struct request_queue
*q
, struct request
*rq
)
428 if (q
->last_merge
== rq
)
429 q
->last_merge
= NULL
;
431 elv_rqhash_del(q
, rq
);
435 q
->end_sector
= rq_end_sector(rq
);
437 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
439 EXPORT_SYMBOL(elv_dispatch_add_tail
);
441 enum elv_merge
elv_merge(struct request_queue
*q
, struct request
**req
,
444 struct elevator_queue
*e
= q
->elevator
;
445 struct request
*__rq
;
449 * nomerges: No merges at all attempted
450 * noxmerges: Only simple one-hit cache try
451 * merges: All merge tries attempted
453 if (blk_queue_nomerges(q
) || !bio_mergeable(bio
))
454 return ELEVATOR_NO_MERGE
;
457 * First try one-hit cache.
459 if (q
->last_merge
&& elv_bio_merge_ok(q
->last_merge
, bio
)) {
460 enum elv_merge ret
= blk_try_merge(q
->last_merge
, bio
);
462 if (ret
!= ELEVATOR_NO_MERGE
) {
463 *req
= q
->last_merge
;
468 if (blk_queue_noxmerges(q
))
469 return ELEVATOR_NO_MERGE
;
472 * See if our hash lookup can find a potential backmerge.
474 __rq
= elv_rqhash_find(q
, bio
->bi_iter
.bi_sector
);
475 if (__rq
&& elv_bio_merge_ok(__rq
, bio
)) {
477 return ELEVATOR_BACK_MERGE
;
480 if (e
->uses_mq
&& e
->type
->ops
.mq
.request_merge
)
481 return e
->type
->ops
.mq
.request_merge(q
, req
, bio
);
482 else if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_merge_fn
)
483 return e
->type
->ops
.sq
.elevator_merge_fn(q
, req
, bio
);
485 return ELEVATOR_NO_MERGE
;
489 * Attempt to do an insertion back merge. Only check for the case where
490 * we can append 'rq' to an existing request, so we can throw 'rq' away
493 * Returns true if we merged, false otherwise
495 bool elv_attempt_insert_merge(struct request_queue
*q
, struct request
*rq
)
497 struct request
*__rq
;
500 if (blk_queue_nomerges(q
))
504 * First try one-hit cache.
506 if (q
->last_merge
&& blk_attempt_req_merge(q
, q
->last_merge
, rq
))
509 if (blk_queue_noxmerges(q
))
514 * See if our hash lookup can find a potential backmerge.
517 __rq
= elv_rqhash_find(q
, blk_rq_pos(rq
));
518 if (!__rq
|| !blk_attempt_req_merge(q
, __rq
, rq
))
521 /* The merged request could be merged with others, try again */
529 void elv_merged_request(struct request_queue
*q
, struct request
*rq
,
532 struct elevator_queue
*e
= q
->elevator
;
534 if (e
->uses_mq
&& e
->type
->ops
.mq
.request_merged
)
535 e
->type
->ops
.mq
.request_merged(q
, rq
, type
);
536 else if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_merged_fn
)
537 e
->type
->ops
.sq
.elevator_merged_fn(q
, rq
, type
);
539 if (type
== ELEVATOR_BACK_MERGE
)
540 elv_rqhash_reposition(q
, rq
);
545 void elv_merge_requests(struct request_queue
*q
, struct request
*rq
,
546 struct request
*next
)
548 struct elevator_queue
*e
= q
->elevator
;
549 bool next_sorted
= false;
551 if (e
->uses_mq
&& e
->type
->ops
.mq
.requests_merged
)
552 e
->type
->ops
.mq
.requests_merged(q
, rq
, next
);
553 else if (e
->type
->ops
.sq
.elevator_merge_req_fn
) {
554 next_sorted
= (__force
bool)(next
->rq_flags
& RQF_SORTED
);
556 e
->type
->ops
.sq
.elevator_merge_req_fn(q
, rq
, next
);
559 elv_rqhash_reposition(q
, rq
);
562 elv_rqhash_del(q
, next
);
569 void elv_bio_merged(struct request_queue
*q
, struct request
*rq
,
572 struct elevator_queue
*e
= q
->elevator
;
574 if (WARN_ON_ONCE(e
->uses_mq
))
577 if (e
->type
->ops
.sq
.elevator_bio_merged_fn
)
578 e
->type
->ops
.sq
.elevator_bio_merged_fn(q
, rq
, bio
);
582 static void blk_pm_requeue_request(struct request
*rq
)
584 if (rq
->q
->dev
&& !(rq
->rq_flags
& RQF_PM
))
588 static void blk_pm_add_request(struct request_queue
*q
, struct request
*rq
)
590 if (q
->dev
&& !(rq
->rq_flags
& RQF_PM
) && q
->nr_pending
++ == 0 &&
591 (q
->rpm_status
== RPM_SUSPENDED
|| q
->rpm_status
== RPM_SUSPENDING
))
592 pm_request_resume(q
->dev
);
595 static inline void blk_pm_requeue_request(struct request
*rq
) {}
596 static inline void blk_pm_add_request(struct request_queue
*q
,
602 void elv_requeue_request(struct request_queue
*q
, struct request
*rq
)
605 * it already went through dequeue, we need to decrement the
606 * in_flight count again
608 if (blk_account_rq(rq
)) {
609 q
->in_flight
[rq_is_sync(rq
)]--;
610 if (rq
->rq_flags
& RQF_SORTED
)
611 elv_deactivate_rq(q
, rq
);
614 rq
->rq_flags
&= ~RQF_STARTED
;
616 blk_pm_requeue_request(rq
);
618 __elv_add_request(q
, rq
, ELEVATOR_INSERT_REQUEUE
);
621 void elv_drain_elevator(struct request_queue
*q
)
623 struct elevator_queue
*e
= q
->elevator
;
626 if (WARN_ON_ONCE(e
->uses_mq
))
629 lockdep_assert_held(q
->queue_lock
);
631 while (e
->type
->ops
.sq
.elevator_dispatch_fn(q
, 1))
633 if (q
->nr_sorted
&& printed
++ < 10) {
634 printk(KERN_ERR
"%s: forced dispatching is broken "
635 "(nr_sorted=%u), please report this\n",
636 q
->elevator
->type
->elevator_name
, q
->nr_sorted
);
640 void __elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
)
642 trace_block_rq_insert(q
, rq
);
644 blk_pm_add_request(q
, rq
);
648 if (rq
->rq_flags
& RQF_SOFTBARRIER
) {
649 /* barriers are scheduling boundary, update end_sector */
650 if (!blk_rq_is_passthrough(rq
)) {
651 q
->end_sector
= rq_end_sector(rq
);
654 } else if (!(rq
->rq_flags
& RQF_ELVPRIV
) &&
655 (where
== ELEVATOR_INSERT_SORT
||
656 where
== ELEVATOR_INSERT_SORT_MERGE
))
657 where
= ELEVATOR_INSERT_BACK
;
660 case ELEVATOR_INSERT_REQUEUE
:
661 case ELEVATOR_INSERT_FRONT
:
662 rq
->rq_flags
|= RQF_SOFTBARRIER
;
663 list_add(&rq
->queuelist
, &q
->queue_head
);
666 case ELEVATOR_INSERT_BACK
:
667 rq
->rq_flags
|= RQF_SOFTBARRIER
;
668 elv_drain_elevator(q
);
669 list_add_tail(&rq
->queuelist
, &q
->queue_head
);
671 * We kick the queue here for the following reasons.
672 * - The elevator might have returned NULL previously
673 * to delay requests and returned them now. As the
674 * queue wasn't empty before this request, ll_rw_blk
675 * won't run the queue on return, resulting in hang.
676 * - Usually, back inserted requests won't be merged
677 * with anything. There's no point in delaying queue
683 case ELEVATOR_INSERT_SORT_MERGE
:
685 * If we succeed in merging this request with one in the
686 * queue already, we are done - rq has now been freed,
687 * so no need to do anything further.
689 if (elv_attempt_insert_merge(q
, rq
))
692 case ELEVATOR_INSERT_SORT
:
693 BUG_ON(blk_rq_is_passthrough(rq
));
694 rq
->rq_flags
|= RQF_SORTED
;
696 if (rq_mergeable(rq
)) {
697 elv_rqhash_add(q
, rq
);
703 * Some ioscheds (cfq) run q->request_fn directly, so
704 * rq cannot be accessed after calling
705 * elevator_add_req_fn.
707 q
->elevator
->type
->ops
.sq
.elevator_add_req_fn(q
, rq
);
710 case ELEVATOR_INSERT_FLUSH
:
711 rq
->rq_flags
|= RQF_SOFTBARRIER
;
712 blk_insert_flush(rq
);
715 printk(KERN_ERR
"%s: bad insertion point %d\n",
720 EXPORT_SYMBOL(__elv_add_request
);
722 void elv_add_request(struct request_queue
*q
, struct request
*rq
, int where
)
726 spin_lock_irqsave(q
->queue_lock
, flags
);
727 __elv_add_request(q
, rq
, where
);
728 spin_unlock_irqrestore(q
->queue_lock
, flags
);
730 EXPORT_SYMBOL(elv_add_request
);
732 struct request
*elv_latter_request(struct request_queue
*q
, struct request
*rq
)
734 struct elevator_queue
*e
= q
->elevator
;
736 if (e
->uses_mq
&& e
->type
->ops
.mq
.next_request
)
737 return e
->type
->ops
.mq
.next_request(q
, rq
);
738 else if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_latter_req_fn
)
739 return e
->type
->ops
.sq
.elevator_latter_req_fn(q
, rq
);
744 struct request
*elv_former_request(struct request_queue
*q
, struct request
*rq
)
746 struct elevator_queue
*e
= q
->elevator
;
748 if (e
->uses_mq
&& e
->type
->ops
.mq
.former_request
)
749 return e
->type
->ops
.mq
.former_request(q
, rq
);
750 if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_former_req_fn
)
751 return e
->type
->ops
.sq
.elevator_former_req_fn(q
, rq
);
755 int elv_set_request(struct request_queue
*q
, struct request
*rq
,
756 struct bio
*bio
, gfp_t gfp_mask
)
758 struct elevator_queue
*e
= q
->elevator
;
760 if (WARN_ON_ONCE(e
->uses_mq
))
763 if (e
->type
->ops
.sq
.elevator_set_req_fn
)
764 return e
->type
->ops
.sq
.elevator_set_req_fn(q
, rq
, bio
, gfp_mask
);
768 void elv_put_request(struct request_queue
*q
, struct request
*rq
)
770 struct elevator_queue
*e
= q
->elevator
;
772 if (WARN_ON_ONCE(e
->uses_mq
))
775 if (e
->type
->ops
.sq
.elevator_put_req_fn
)
776 e
->type
->ops
.sq
.elevator_put_req_fn(rq
);
779 int elv_may_queue(struct request_queue
*q
, unsigned int op
)
781 struct elevator_queue
*e
= q
->elevator
;
783 if (WARN_ON_ONCE(e
->uses_mq
))
786 if (e
->type
->ops
.sq
.elevator_may_queue_fn
)
787 return e
->type
->ops
.sq
.elevator_may_queue_fn(q
, op
);
789 return ELV_MQUEUE_MAY
;
792 void elv_completed_request(struct request_queue
*q
, struct request
*rq
)
794 struct elevator_queue
*e
= q
->elevator
;
796 if (WARN_ON_ONCE(e
->uses_mq
))
800 * request is released from the driver, io must be done
802 if (blk_account_rq(rq
)) {
803 q
->in_flight
[rq_is_sync(rq
)]--;
804 if ((rq
->rq_flags
& RQF_SORTED
) &&
805 e
->type
->ops
.sq
.elevator_completed_req_fn
)
806 e
->type
->ops
.sq
.elevator_completed_req_fn(q
, rq
);
810 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
813 elv_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
815 struct elv_fs_entry
*entry
= to_elv(attr
);
816 struct elevator_queue
*e
;
822 e
= container_of(kobj
, struct elevator_queue
, kobj
);
823 mutex_lock(&e
->sysfs_lock
);
824 error
= e
->type
? entry
->show(e
, page
) : -ENOENT
;
825 mutex_unlock(&e
->sysfs_lock
);
830 elv_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
831 const char *page
, size_t length
)
833 struct elv_fs_entry
*entry
= to_elv(attr
);
834 struct elevator_queue
*e
;
840 e
= container_of(kobj
, struct elevator_queue
, kobj
);
841 mutex_lock(&e
->sysfs_lock
);
842 error
= e
->type
? entry
->store(e
, page
, length
) : -ENOENT
;
843 mutex_unlock(&e
->sysfs_lock
);
847 static const struct sysfs_ops elv_sysfs_ops
= {
848 .show
= elv_attr_show
,
849 .store
= elv_attr_store
,
852 static struct kobj_type elv_ktype
= {
853 .sysfs_ops
= &elv_sysfs_ops
,
854 .release
= elevator_release
,
857 int elv_register_queue(struct request_queue
*q
)
859 struct elevator_queue
*e
= q
->elevator
;
862 error
= kobject_add(&e
->kobj
, &q
->kobj
, "%s", "iosched");
864 struct elv_fs_entry
*attr
= e
->type
->elevator_attrs
;
866 while (attr
->attr
.name
) {
867 if (sysfs_create_file(&e
->kobj
, &attr
->attr
))
872 kobject_uevent(&e
->kobj
, KOBJ_ADD
);
874 if (!e
->uses_mq
&& e
->type
->ops
.sq
.elevator_registered_fn
)
875 e
->type
->ops
.sq
.elevator_registered_fn(q
);
879 EXPORT_SYMBOL(elv_register_queue
);
881 void elv_unregister_queue(struct request_queue
*q
)
884 struct elevator_queue
*e
= q
->elevator
;
886 kobject_uevent(&e
->kobj
, KOBJ_REMOVE
);
887 kobject_del(&e
->kobj
);
889 /* Re-enable throttling in case elevator disabled it */
890 wbt_enable_default(q
);
893 EXPORT_SYMBOL(elv_unregister_queue
);
895 int elv_register(struct elevator_type
*e
)
899 /* create icq_cache if requested */
901 if (WARN_ON(e
->icq_size
< sizeof(struct io_cq
)) ||
902 WARN_ON(e
->icq_align
< __alignof__(struct io_cq
)))
905 snprintf(e
->icq_cache_name
, sizeof(e
->icq_cache_name
),
906 "%s_io_cq", e
->elevator_name
);
907 e
->icq_cache
= kmem_cache_create(e
->icq_cache_name
, e
->icq_size
,
908 e
->icq_align
, 0, NULL
);
913 /* register, don't allow duplicate names */
914 spin_lock(&elv_list_lock
);
915 if (elevator_find(e
->elevator_name
, e
->uses_mq
)) {
916 spin_unlock(&elv_list_lock
);
918 kmem_cache_destroy(e
->icq_cache
);
921 list_add_tail(&e
->list
, &elv_list
);
922 spin_unlock(&elv_list_lock
);
924 /* print pretty message */
925 if (!strcmp(e
->elevator_name
, chosen_elevator
) ||
926 (!*chosen_elevator
&&
927 !strcmp(e
->elevator_name
, CONFIG_DEFAULT_IOSCHED
)))
930 printk(KERN_INFO
"io scheduler %s registered%s\n", e
->elevator_name
,
934 EXPORT_SYMBOL_GPL(elv_register
);
936 void elv_unregister(struct elevator_type
*e
)
939 spin_lock(&elv_list_lock
);
940 list_del_init(&e
->list
);
941 spin_unlock(&elv_list_lock
);
944 * Destroy icq_cache if it exists. icq's are RCU managed. Make
945 * sure all RCU operations are complete before proceeding.
949 kmem_cache_destroy(e
->icq_cache
);
953 EXPORT_SYMBOL_GPL(elv_unregister
);
955 static int elevator_switch_mq(struct request_queue
*q
,
956 struct elevator_type
*new_e
)
960 blk_mq_freeze_queue(q
);
963 if (q
->elevator
->registered
)
964 elv_unregister_queue(q
);
966 elevator_exit(q
, q
->elevator
);
969 ret
= blk_mq_init_sched(q
, new_e
);
974 ret
= elv_register_queue(q
);
976 elevator_exit(q
, q
->elevator
);
982 blk_add_trace_msg(q
, "elv switch: %s", new_e
->elevator_name
);
984 blk_add_trace_msg(q
, "elv switch: none");
987 blk_mq_unfreeze_queue(q
);
992 * switch to new_e io scheduler. be careful not to introduce deadlocks -
993 * we don't free the old io scheduler, before we have allocated what we
994 * need for the new one. this way we have a chance of going back to the old
995 * one, if the new one fails init for some reason.
997 static int elevator_switch(struct request_queue
*q
, struct elevator_type
*new_e
)
999 struct elevator_queue
*old
= q
->elevator
;
1000 bool old_registered
= false;
1004 return elevator_switch_mq(q
, new_e
);
1007 * Turn on BYPASS and drain all requests w/ elevator private data.
1008 * Block layer doesn't call into a quiesced elevator - all requests
1009 * are directly put on the dispatch list without elevator data
1010 * using INSERT_BACK. All requests have SOFTBARRIER set and no
1011 * merge happens either.
1014 old_registered
= old
->registered
;
1016 blk_queue_bypass_start(q
);
1018 /* unregister and clear all auxiliary data of the old elevator */
1020 elv_unregister_queue(q
);
1025 /* allocate, init and register new elevator */
1026 err
= new_e
->ops
.sq
.elevator_init_fn(q
, new_e
);
1030 err
= elv_register_queue(q
);
1034 /* done, kill the old one and finish */
1036 elevator_exit(q
, old
);
1037 blk_queue_bypass_end(q
);
1040 blk_add_trace_msg(q
, "elv switch: %s", new_e
->elevator_name
);
1045 elevator_exit(q
, q
->elevator
);
1047 /* switch failed, restore and re-register old elevator */
1050 elv_register_queue(q
);
1051 blk_queue_bypass_end(q
);
1058 * Switch this queue to the given IO scheduler.
1060 static int __elevator_change(struct request_queue
*q
, const char *name
)
1062 char elevator_name
[ELV_NAME_MAX
];
1063 struct elevator_type
*e
;
1065 /* Make sure queue is not in the middle of being removed */
1066 if (!test_bit(QUEUE_FLAG_REGISTERED
, &q
->queue_flags
))
1070 * Special case for mq, turn off scheduling
1072 if (q
->mq_ops
&& !strncmp(name
, "none", 4))
1073 return elevator_switch(q
, NULL
);
1075 strlcpy(elevator_name
, name
, sizeof(elevator_name
));
1076 e
= elevator_get(q
, strstrip(elevator_name
), true);
1081 !strcmp(elevator_name
, q
->elevator
->type
->elevator_name
)) {
1086 return elevator_switch(q
, e
);
1089 static inline bool elv_support_iosched(struct request_queue
*q
)
1091 if (q
->mq_ops
&& q
->tag_set
&& (q
->tag_set
->flags
&
1097 ssize_t
elv_iosched_store(struct request_queue
*q
, const char *name
,
1102 if (!(q
->mq_ops
|| q
->request_fn
) || !elv_support_iosched(q
))
1105 ret
= __elevator_change(q
, name
);
1112 ssize_t
elv_iosched_show(struct request_queue
*q
, char *name
)
1114 struct elevator_queue
*e
= q
->elevator
;
1115 struct elevator_type
*elv
= NULL
;
1116 struct elevator_type
*__e
;
1119 if (!blk_queue_stackable(q
))
1120 return sprintf(name
, "none\n");
1123 len
+= sprintf(name
+len
, "[none] ");
1127 spin_lock(&elv_list_lock
);
1128 list_for_each_entry(__e
, &elv_list
, list
) {
1129 if (elv
&& !strcmp(elv
->elevator_name
, __e
->elevator_name
)) {
1130 len
+= sprintf(name
+len
, "[%s] ", elv
->elevator_name
);
1133 if (__e
->uses_mq
&& q
->mq_ops
&& elv_support_iosched(q
))
1134 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1135 else if (!__e
->uses_mq
&& !q
->mq_ops
)
1136 len
+= sprintf(name
+len
, "%s ", __e
->elevator_name
);
1138 spin_unlock(&elv_list_lock
);
1140 if (q
->mq_ops
&& q
->elevator
)
1141 len
+= sprintf(name
+len
, "none");
1143 len
+= sprintf(len
+name
, "\n");
1147 struct request
*elv_rb_former_request(struct request_queue
*q
,
1150 struct rb_node
*rbprev
= rb_prev(&rq
->rb_node
);
1153 return rb_entry_rq(rbprev
);
1157 EXPORT_SYMBOL(elv_rb_former_request
);
1159 struct request
*elv_rb_latter_request(struct request_queue
*q
,
1162 struct rb_node
*rbnext
= rb_next(&rq
->rb_node
);
1165 return rb_entry_rq(rbnext
);
1169 EXPORT_SYMBOL(elv_rb_latter_request
);