• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Block device elevator/IO-scheduler.
4  *
5  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6  *
7  * 30042000 Jens Axboe <axboe@kernel.dk> :
8  *
9  * Split the elevator a bit so that it is possible to choose a different
10  * one or even write a new "plug in". There are three pieces:
11  * - elevator_fn, inserts a new request in the queue list
12  * - elevator_merge_fn, decides whether a new buffer can be merged with
13  *   an existing request
14  * - elevator_dequeue_fn, called when a request is taken off the active list
15  *
16  * 20082000 Dave Jones <davej@suse.de> :
17  * Removed tests for max-bomb-segments, which was breaking elvtune
18  *  when run without -bN
19  *
20  * Jens:
21  * - Rework again to work with bio instead of buffer_heads
22  * - loose bi_dev comparisons, partition handling is right now
23  * - completely modularize elevator setup and teardown
24  *
25  */
26 #include <linux/kernel.h>
27 #include <linux/fs.h>
28 #include <linux/blkdev.h>
29 #include <linux/elevator.h>
30 #include <linux/bio.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/compiler.h>
35 #include <linux/blktrace_api.h>
36 #include <linux/hash.h>
37 #include <linux/uaccess.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/blk-cgroup.h>
40 
41 #include <trace/events/block.h>
42 
43 #include "blk.h"
44 #include "blk-mq-sched.h"
45 #include "blk-pm.h"
46 #include "blk-wbt.h"
47 
48 static DEFINE_SPINLOCK(elv_list_lock);
49 static LIST_HEAD(elv_list);
50 
51 /*
52  * Merge hash stuff.
53  */
54 #define rq_hash_key(rq)		(blk_rq_pos(rq) + blk_rq_sectors(rq))
55 
56 /*
57  * Query io scheduler to see if the current process issuing bio may be
58  * merged with rq.
59  */
elv_iosched_allow_bio_merge(struct request * rq,struct bio * bio)60 static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
61 {
62 	struct request_queue *q = rq->q;
63 	struct elevator_queue *e = q->elevator;
64 
65 	if (e->type->ops.allow_merge)
66 		return e->type->ops.allow_merge(q, rq, bio);
67 
68 	return 1;
69 }
70 
71 /*
72  * can we safely merge with this request?
73  */
elv_bio_merge_ok(struct request * rq,struct bio * bio)74 bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
75 {
76 	if (!blk_rq_merge_ok(rq, bio))
77 		return false;
78 
79 	if (!elv_iosched_allow_bio_merge(rq, bio))
80 		return false;
81 
82 	return true;
83 }
84 EXPORT_SYMBOL(elv_bio_merge_ok);
85 
elv_support_features(unsigned int elv_features,unsigned int required_features)86 static inline bool elv_support_features(unsigned int elv_features,
87 					unsigned int required_features)
88 {
89 	return (required_features & elv_features) == required_features;
90 }
91 
92 /**
93  * elevator_match - Test an elevator name and features
94  * @e: Scheduler to test
95  * @name: Elevator name to test
96  * @required_features: Features that the elevator must provide
97  *
98  * Return true if the elevator @e name matches @name and if @e provides all
99  * the features specified by @required_features.
100  */
elevator_match(const struct elevator_type * e,const char * name,unsigned int required_features)101 static bool elevator_match(const struct elevator_type *e, const char *name,
102 			   unsigned int required_features)
103 {
104 	if (!elv_support_features(e->elevator_features, required_features))
105 		return false;
106 	if (!strcmp(e->elevator_name, name))
107 		return true;
108 	if (e->elevator_alias && !strcmp(e->elevator_alias, name))
109 		return true;
110 
111 	return false;
112 }
113 
114 /**
115  * elevator_find - Find an elevator
116  * @name: Name of the elevator to find
117  * @required_features: Features that the elevator must provide
118  *
119  * Return the first registered scheduler with name @name and supporting the
120  * features @required_features and NULL otherwise.
121  */
elevator_find(const char * name,unsigned int required_features)122 static struct elevator_type *elevator_find(const char *name,
123 					   unsigned int required_features)
124 {
125 	struct elevator_type *e;
126 
127 	list_for_each_entry(e, &elv_list, list) {
128 		if (elevator_match(e, name, required_features))
129 			return e;
130 	}
131 
132 	return NULL;
133 }
134 
elevator_put(struct elevator_type * e)135 static void elevator_put(struct elevator_type *e)
136 {
137 	module_put(e->elevator_owner);
138 }
139 
elevator_get(struct request_queue * q,const char * name,bool try_loading)140 static struct elevator_type *elevator_get(struct request_queue *q,
141 					  const char *name, bool try_loading)
142 {
143 	struct elevator_type *e;
144 
145 	spin_lock(&elv_list_lock);
146 
147 	e = elevator_find(name, q->required_elevator_features);
148 	if (!e && try_loading) {
149 		spin_unlock(&elv_list_lock);
150 		request_module("%s-iosched", name);
151 		spin_lock(&elv_list_lock);
152 		e = elevator_find(name, q->required_elevator_features);
153 	}
154 
155 	if (e && !try_module_get(e->elevator_owner))
156 		e = NULL;
157 
158 	spin_unlock(&elv_list_lock);
159 	return e;
160 }
161 
162 static struct kobj_type elv_ktype;
163 
elevator_alloc(struct request_queue * q,struct elevator_type * e)164 struct elevator_queue *elevator_alloc(struct request_queue *q,
165 				  struct elevator_type *e)
166 {
167 	struct elevator_queue *eq;
168 
169 	eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
170 	if (unlikely(!eq))
171 		return NULL;
172 
173 	eq->type = e;
174 	kobject_init(&eq->kobj, &elv_ktype);
175 	mutex_init(&eq->sysfs_lock);
176 	hash_init(eq->hash);
177 
178 	return eq;
179 }
180 EXPORT_SYMBOL(elevator_alloc);
181 
elevator_release(struct kobject * kobj)182 static void elevator_release(struct kobject *kobj)
183 {
184 	struct elevator_queue *e;
185 
186 	e = container_of(kobj, struct elevator_queue, kobj);
187 	elevator_put(e->type);
188 	kfree(e);
189 }
190 
__elevator_exit(struct request_queue * q,struct elevator_queue * e)191 void __elevator_exit(struct request_queue *q, struct elevator_queue *e)
192 {
193 	mutex_lock(&e->sysfs_lock);
194 	blk_mq_exit_sched(q, e);
195 	mutex_unlock(&e->sysfs_lock);
196 
197 	kobject_put(&e->kobj);
198 }
199 
__elv_rqhash_del(struct request * rq)200 static inline void __elv_rqhash_del(struct request *rq)
201 {
202 	hash_del(&rq->hash);
203 	rq->rq_flags &= ~RQF_HASHED;
204 }
205 
elv_rqhash_del(struct request_queue * q,struct request * rq)206 void elv_rqhash_del(struct request_queue *q, struct request *rq)
207 {
208 	if (ELV_ON_HASH(rq))
209 		__elv_rqhash_del(rq);
210 }
211 EXPORT_SYMBOL_GPL(elv_rqhash_del);
212 
elv_rqhash_add(struct request_queue * q,struct request * rq)213 void elv_rqhash_add(struct request_queue *q, struct request *rq)
214 {
215 	struct elevator_queue *e = q->elevator;
216 
217 	BUG_ON(ELV_ON_HASH(rq));
218 	hash_add(e->hash, &rq->hash, rq_hash_key(rq));
219 	rq->rq_flags |= RQF_HASHED;
220 }
221 EXPORT_SYMBOL_GPL(elv_rqhash_add);
222 
elv_rqhash_reposition(struct request_queue * q,struct request * rq)223 void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
224 {
225 	__elv_rqhash_del(rq);
226 	elv_rqhash_add(q, rq);
227 }
228 
elv_rqhash_find(struct request_queue * q,sector_t offset)229 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
230 {
231 	struct elevator_queue *e = q->elevator;
232 	struct hlist_node *next;
233 	struct request *rq;
234 
235 	hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
236 		BUG_ON(!ELV_ON_HASH(rq));
237 
238 		if (unlikely(!rq_mergeable(rq))) {
239 			__elv_rqhash_del(rq);
240 			continue;
241 		}
242 
243 		if (rq_hash_key(rq) == offset)
244 			return rq;
245 	}
246 
247 	return NULL;
248 }
249 
250 /*
251  * RB-tree support functions for inserting/lookup/removal of requests
252  * in a sorted RB tree.
253  */
elv_rb_add(struct rb_root * root,struct request * rq)254 void elv_rb_add(struct rb_root *root, struct request *rq)
255 {
256 	struct rb_node **p = &root->rb_node;
257 	struct rb_node *parent = NULL;
258 	struct request *__rq;
259 
260 	while (*p) {
261 		parent = *p;
262 		__rq = rb_entry(parent, struct request, rb_node);
263 
264 		if (blk_rq_pos(rq) < blk_rq_pos(__rq))
265 			p = &(*p)->rb_left;
266 		else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
267 			p = &(*p)->rb_right;
268 	}
269 
270 	rb_link_node(&rq->rb_node, parent, p);
271 	rb_insert_color(&rq->rb_node, root);
272 }
273 EXPORT_SYMBOL(elv_rb_add);
274 
elv_rb_del(struct rb_root * root,struct request * rq)275 void elv_rb_del(struct rb_root *root, struct request *rq)
276 {
277 	BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
278 	rb_erase(&rq->rb_node, root);
279 	RB_CLEAR_NODE(&rq->rb_node);
280 }
281 EXPORT_SYMBOL(elv_rb_del);
282 
elv_rb_find(struct rb_root * root,sector_t sector)283 struct request *elv_rb_find(struct rb_root *root, sector_t sector)
284 {
285 	struct rb_node *n = root->rb_node;
286 	struct request *rq;
287 
288 	while (n) {
289 		rq = rb_entry(n, struct request, rb_node);
290 
291 		if (sector < blk_rq_pos(rq))
292 			n = n->rb_left;
293 		else if (sector > blk_rq_pos(rq))
294 			n = n->rb_right;
295 		else
296 			return rq;
297 	}
298 
299 	return NULL;
300 }
301 EXPORT_SYMBOL(elv_rb_find);
302 
elv_merge(struct request_queue * q,struct request ** req,struct bio * bio)303 enum elv_merge elv_merge(struct request_queue *q, struct request **req,
304 		struct bio *bio)
305 {
306 	struct elevator_queue *e = q->elevator;
307 	struct request *__rq;
308 
309 	/*
310 	 * Levels of merges:
311 	 * 	nomerges:  No merges at all attempted
312 	 * 	noxmerges: Only simple one-hit cache try
313 	 * 	merges:	   All merge tries attempted
314 	 */
315 	if (blk_queue_nomerges(q) || !bio_mergeable(bio))
316 		return ELEVATOR_NO_MERGE;
317 
318 	/*
319 	 * First try one-hit cache.
320 	 */
321 	if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
322 		enum elv_merge ret = blk_try_merge(q->last_merge, bio);
323 
324 		if (ret != ELEVATOR_NO_MERGE) {
325 			*req = q->last_merge;
326 			return ret;
327 		}
328 	}
329 
330 	if (blk_queue_noxmerges(q))
331 		return ELEVATOR_NO_MERGE;
332 
333 	/*
334 	 * See if our hash lookup can find a potential backmerge.
335 	 */
336 	__rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
337 	if (__rq && elv_bio_merge_ok(__rq, bio)) {
338 		*req = __rq;
339 
340 		if (blk_discard_mergable(__rq))
341 			return ELEVATOR_DISCARD_MERGE;
342 		return ELEVATOR_BACK_MERGE;
343 	}
344 
345 	if (e->type->ops.request_merge)
346 		return e->type->ops.request_merge(q, req, bio);
347 
348 	return ELEVATOR_NO_MERGE;
349 }
350 
351 /*
352  * Attempt to do an insertion back merge. Only check for the case where
353  * we can append 'rq' to an existing request, so we can throw 'rq' away
354  * afterwards.
355  *
356  * Returns true if we merged, false otherwise
357  */
elv_attempt_insert_merge(struct request_queue * q,struct request * rq)358 bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
359 {
360 	struct request *__rq;
361 	bool ret;
362 
363 	if (blk_queue_nomerges(q))
364 		return false;
365 
366 	/*
367 	 * First try one-hit cache.
368 	 */
369 	if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
370 		return true;
371 
372 	if (blk_queue_noxmerges(q))
373 		return false;
374 
375 	ret = false;
376 	/*
377 	 * See if our hash lookup can find a potential backmerge.
378 	 */
379 	while (1) {
380 		__rq = elv_rqhash_find(q, blk_rq_pos(rq));
381 		if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
382 			break;
383 
384 		/* The merged request could be merged with others, try again */
385 		ret = true;
386 		rq = __rq;
387 	}
388 
389 	return ret;
390 }
391 
elv_merged_request(struct request_queue * q,struct request * rq,enum elv_merge type)392 void elv_merged_request(struct request_queue *q, struct request *rq,
393 		enum elv_merge type)
394 {
395 	struct elevator_queue *e = q->elevator;
396 
397 	if (e->type->ops.request_merged)
398 		e->type->ops.request_merged(q, rq, type);
399 
400 	if (type == ELEVATOR_BACK_MERGE)
401 		elv_rqhash_reposition(q, rq);
402 
403 	q->last_merge = rq;
404 }
405 
elv_merge_requests(struct request_queue * q,struct request * rq,struct request * next)406 void elv_merge_requests(struct request_queue *q, struct request *rq,
407 			     struct request *next)
408 {
409 	struct elevator_queue *e = q->elevator;
410 
411 	if (e->type->ops.requests_merged)
412 		e->type->ops.requests_merged(q, rq, next);
413 
414 	elv_rqhash_reposition(q, rq);
415 	q->last_merge = rq;
416 }
417 
elv_latter_request(struct request_queue * q,struct request * rq)418 struct request *elv_latter_request(struct request_queue *q, struct request *rq)
419 {
420 	struct elevator_queue *e = q->elevator;
421 
422 	if (e->type->ops.next_request)
423 		return e->type->ops.next_request(q, rq);
424 
425 	return NULL;
426 }
427 
elv_former_request(struct request_queue * q,struct request * rq)428 struct request *elv_former_request(struct request_queue *q, struct request *rq)
429 {
430 	struct elevator_queue *e = q->elevator;
431 
432 	if (e->type->ops.former_request)
433 		return e->type->ops.former_request(q, rq);
434 
435 	return NULL;
436 }
437 
438 #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
439 
440 static ssize_t
elv_attr_show(struct kobject * kobj,struct attribute * attr,char * page)441 elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
442 {
443 	struct elv_fs_entry *entry = to_elv(attr);
444 	struct elevator_queue *e;
445 	ssize_t error;
446 
447 	if (!entry->show)
448 		return -EIO;
449 
450 	e = container_of(kobj, struct elevator_queue, kobj);
451 	mutex_lock(&e->sysfs_lock);
452 	error = e->type ? entry->show(e, page) : -ENOENT;
453 	mutex_unlock(&e->sysfs_lock);
454 	return error;
455 }
456 
457 static ssize_t
elv_attr_store(struct kobject * kobj,struct attribute * attr,const char * page,size_t length)458 elv_attr_store(struct kobject *kobj, struct attribute *attr,
459 	       const char *page, size_t length)
460 {
461 	struct elv_fs_entry *entry = to_elv(attr);
462 	struct elevator_queue *e;
463 	ssize_t error;
464 
465 	if (!entry->store)
466 		return -EIO;
467 
468 	e = container_of(kobj, struct elevator_queue, kobj);
469 	mutex_lock(&e->sysfs_lock);
470 	error = e->type ? entry->store(e, page, length) : -ENOENT;
471 	mutex_unlock(&e->sysfs_lock);
472 	return error;
473 }
474 
475 static const struct sysfs_ops elv_sysfs_ops = {
476 	.show	= elv_attr_show,
477 	.store	= elv_attr_store,
478 };
479 
480 static struct kobj_type elv_ktype = {
481 	.sysfs_ops	= &elv_sysfs_ops,
482 	.release	= elevator_release,
483 };
484 
elv_register_queue(struct request_queue * q,bool uevent)485 int elv_register_queue(struct request_queue *q, bool uevent)
486 {
487 	struct elevator_queue *e = q->elevator;
488 	int error;
489 
490 	lockdep_assert_held(&q->sysfs_lock);
491 
492 	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
493 	if (!error) {
494 		struct elv_fs_entry *attr = e->type->elevator_attrs;
495 		if (attr) {
496 			while (attr->attr.name) {
497 				if (sysfs_create_file(&e->kobj, &attr->attr))
498 					break;
499 				attr++;
500 			}
501 		}
502 		if (uevent)
503 			kobject_uevent(&e->kobj, KOBJ_ADD);
504 
505 		e->registered = 1;
506 	}
507 	return error;
508 }
509 
elv_unregister_queue(struct request_queue * q)510 void elv_unregister_queue(struct request_queue *q)
511 {
512 	lockdep_assert_held(&q->sysfs_lock);
513 
514 	if (q) {
515 		struct elevator_queue *e = q->elevator;
516 
517 		kobject_uevent(&e->kobj, KOBJ_REMOVE);
518 		kobject_del(&e->kobj);
519 
520 		e->registered = 0;
521 	}
522 }
523 
elv_register(struct elevator_type * e)524 int elv_register(struct elevator_type *e)
525 {
526 	/* create icq_cache if requested */
527 	if (e->icq_size) {
528 		if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
529 		    WARN_ON(e->icq_align < __alignof__(struct io_cq)))
530 			return -EINVAL;
531 
532 		snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
533 			 "%s_io_cq", e->elevator_name);
534 		e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
535 						 e->icq_align, 0, NULL);
536 		if (!e->icq_cache)
537 			return -ENOMEM;
538 	}
539 
540 	/* register, don't allow duplicate names */
541 	spin_lock(&elv_list_lock);
542 	if (elevator_find(e->elevator_name, 0)) {
543 		spin_unlock(&elv_list_lock);
544 		kmem_cache_destroy(e->icq_cache);
545 		return -EBUSY;
546 	}
547 	list_add_tail(&e->list, &elv_list);
548 	spin_unlock(&elv_list_lock);
549 
550 	printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name);
551 
552 	return 0;
553 }
554 EXPORT_SYMBOL_GPL(elv_register);
555 
elv_unregister(struct elevator_type * e)556 void elv_unregister(struct elevator_type *e)
557 {
558 	/* unregister */
559 	spin_lock(&elv_list_lock);
560 	list_del_init(&e->list);
561 	spin_unlock(&elv_list_lock);
562 
563 	/*
564 	 * Destroy icq_cache if it exists.  icq's are RCU managed.  Make
565 	 * sure all RCU operations are complete before proceeding.
566 	 */
567 	if (e->icq_cache) {
568 		rcu_barrier();
569 		kmem_cache_destroy(e->icq_cache);
570 		e->icq_cache = NULL;
571 	}
572 }
573 EXPORT_SYMBOL_GPL(elv_unregister);
574 
elevator_switch_mq(struct request_queue * q,struct elevator_type * new_e)575 int elevator_switch_mq(struct request_queue *q,
576 			      struct elevator_type *new_e)
577 {
578 	int ret;
579 
580 	lockdep_assert_held(&q->sysfs_lock);
581 
582 	if (q->elevator) {
583 		if (q->elevator->registered)
584 			elv_unregister_queue(q);
585 
586 		ioc_clear_queue(q);
587 		elevator_exit(q, q->elevator);
588 	}
589 
590 	ret = blk_mq_init_sched(q, new_e);
591 	if (ret)
592 		goto out;
593 
594 	if (new_e) {
595 		ret = elv_register_queue(q, true);
596 		if (ret) {
597 			elevator_exit(q, q->elevator);
598 			goto out;
599 		}
600 	}
601 
602 	if (new_e)
603 		blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
604 	else
605 		blk_add_trace_msg(q, "elv switch: none");
606 
607 out:
608 	return ret;
609 }
610 
elv_support_iosched(struct request_queue * q)611 static inline bool elv_support_iosched(struct request_queue *q)
612 {
613 	if (!queue_is_mq(q) ||
614 	    (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)))
615 		return false;
616 	return true;
617 }
618 
619 /*
620  * For single queue devices, default to using mq-deadline. If we have multiple
621  * queues or mq-deadline is not available, default to "none".
622  */
elevator_get_default(struct request_queue * q)623 static struct elevator_type *elevator_get_default(struct request_queue *q)
624 {
625 	if (q->tag_set && q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT)
626 		return NULL;
627 
628 	if (q->nr_hw_queues != 1)
629 		return NULL;
630 
631 	return elevator_get(q, "mq-deadline", false);
632 }
633 
634 /*
635  * Get the first elevator providing the features required by the request queue.
636  * Default to "none" if no matching elevator is found.
637  */
elevator_get_by_features(struct request_queue * q)638 static struct elevator_type *elevator_get_by_features(struct request_queue *q)
639 {
640 	struct elevator_type *e, *found = NULL;
641 
642 	spin_lock(&elv_list_lock);
643 
644 	list_for_each_entry(e, &elv_list, list) {
645 		if (elv_support_features(e->elevator_features,
646 					 q->required_elevator_features)) {
647 			found = e;
648 			break;
649 		}
650 	}
651 
652 	if (found && !try_module_get(found->elevator_owner))
653 		found = NULL;
654 
655 	spin_unlock(&elv_list_lock);
656 	return found;
657 }
658 
659 /*
660  * For a device queue that has no required features, use the default elevator
661  * settings. Otherwise, use the first elevator available matching the required
662  * features. If no suitable elevator is find or if the chosen elevator
663  * initialization fails, fall back to the "none" elevator (no elevator).
664  */
elevator_init_mq(struct request_queue * q)665 void elevator_init_mq(struct request_queue *q)
666 {
667 	struct elevator_type *e;
668 	int err;
669 
670 	if (!elv_support_iosched(q))
671 		return;
672 
673 	WARN_ON_ONCE(blk_queue_registered(q));
674 
675 	if (unlikely(q->elevator))
676 		return;
677 
678 	if (!q->required_elevator_features)
679 		e = elevator_get_default(q);
680 	else
681 		e = elevator_get_by_features(q);
682 	if (!e)
683 		return;
684 
685 	blk_mq_freeze_queue(q);
686 	blk_mq_quiesce_queue(q);
687 
688 	err = blk_mq_init_sched(q, e);
689 
690 	blk_mq_unquiesce_queue(q);
691 	blk_mq_unfreeze_queue(q);
692 
693 	if (err) {
694 		pr_warn("\"%s\" elevator initialization failed, "
695 			"falling back to \"none\"\n", e->elevator_name);
696 		elevator_put(e);
697 	}
698 }
699 
700 
701 /*
702  * switch to new_e io scheduler. be careful not to introduce deadlocks -
703  * we don't free the old io scheduler, before we have allocated what we
704  * need for the new one. this way we have a chance of going back to the old
705  * one, if the new one fails init for some reason.
706  */
elevator_switch(struct request_queue * q,struct elevator_type * new_e)707 static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
708 {
709 	int err;
710 
711 	lockdep_assert_held(&q->sysfs_lock);
712 
713 	blk_mq_freeze_queue(q);
714 	blk_mq_quiesce_queue(q);
715 
716 	err = elevator_switch_mq(q, new_e);
717 
718 	blk_mq_unquiesce_queue(q);
719 	blk_mq_unfreeze_queue(q);
720 
721 	return err;
722 }
723 
724 /*
725  * Switch this queue to the given IO scheduler.
726  */
__elevator_change(struct request_queue * q,const char * name)727 static int __elevator_change(struct request_queue *q, const char *name)
728 {
729 	char elevator_name[ELV_NAME_MAX];
730 	struct elevator_type *e;
731 
732 	/* Make sure queue is not in the middle of being removed */
733 	if (!blk_queue_registered(q))
734 		return -ENOENT;
735 
736 	/*
737 	 * Special case for mq, turn off scheduling
738 	 */
739 	if (!strncmp(name, "none", 4)) {
740 		if (!q->elevator)
741 			return 0;
742 		return elevator_switch(q, NULL);
743 	}
744 
745 	strlcpy(elevator_name, name, sizeof(elevator_name));
746 	e = elevator_get(q, strstrip(elevator_name), true);
747 	if (!e)
748 		return -EINVAL;
749 
750 	if (q->elevator &&
751 	    elevator_match(q->elevator->type, elevator_name, 0)) {
752 		elevator_put(e);
753 		return 0;
754 	}
755 
756 	return elevator_switch(q, e);
757 }
758 
elv_iosched_store(struct request_queue * q,const char * name,size_t count)759 ssize_t elv_iosched_store(struct request_queue *q, const char *name,
760 			  size_t count)
761 {
762 	int ret;
763 
764 	if (!elv_support_iosched(q))
765 		return count;
766 
767 	ret = __elevator_change(q, name);
768 	if (!ret)
769 		return count;
770 
771 	return ret;
772 }
773 
elv_iosched_show(struct request_queue * q,char * name)774 ssize_t elv_iosched_show(struct request_queue *q, char *name)
775 {
776 	struct elevator_queue *e = q->elevator;
777 	struct elevator_type *elv = NULL;
778 	struct elevator_type *__e;
779 	int len = 0;
780 
781 	if (!queue_is_mq(q))
782 		return sprintf(name, "none\n");
783 
784 	if (!q->elevator)
785 		len += sprintf(name+len, "[none] ");
786 	else
787 		elv = e->type;
788 
789 	spin_lock(&elv_list_lock);
790 	list_for_each_entry(__e, &elv_list, list) {
791 		if (elv && elevator_match(elv, __e->elevator_name, 0)) {
792 			len += sprintf(name+len, "[%s] ", elv->elevator_name);
793 			continue;
794 		}
795 		if (elv_support_iosched(q) &&
796 		    elevator_match(__e, __e->elevator_name,
797 				   q->required_elevator_features))
798 			len += sprintf(name+len, "%s ", __e->elevator_name);
799 	}
800 	spin_unlock(&elv_list_lock);
801 
802 	if (q->elevator)
803 		len += sprintf(name+len, "none");
804 
805 	len += sprintf(len+name, "\n");
806 	return len;
807 }
808 
elv_rb_former_request(struct request_queue * q,struct request * rq)809 struct request *elv_rb_former_request(struct request_queue *q,
810 				      struct request *rq)
811 {
812 	struct rb_node *rbprev = rb_prev(&rq->rb_node);
813 
814 	if (rbprev)
815 		return rb_entry_rq(rbprev);
816 
817 	return NULL;
818 }
819 EXPORT_SYMBOL(elv_rb_former_request);
820 
elv_rb_latter_request(struct request_queue * q,struct request * rq)821 struct request *elv_rb_latter_request(struct request_queue *q,
822 				      struct request *rq)
823 {
824 	struct rb_node *rbnext = rb_next(&rq->rb_node);
825 
826 	if (rbnext)
827 		return rb_entry_rq(rbnext);
828 
829 	return NULL;
830 }
831 EXPORT_SYMBOL(elv_rb_latter_request);
832 
elevator_setup(char * str)833 static int __init elevator_setup(char *str)
834 {
835 	pr_warn("Kernel parameter elevator= does not have any effect anymore.\n"
836 		"Please use sysfs to set IO scheduler for individual devices.\n");
837 	return 1;
838 }
839 
840 __setup("elevator=", elevator_setup);
841