Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Block device elevator/IO-scheduler. |
| 4 | * |
| 5 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE |
| 6 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 7 | * 30042000 Jens Axboe <axboe@kernel.dk> : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/compiler.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 35 | #include <linux/blktrace_api.h> |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 36 | #include <linux/hash.h> |
Jens Axboe | 0835da6 | 2008-08-26 09:15:47 +0200 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
Lin Ming | c815881 | 2013-03-23 11:42:27 +0800 | [diff] [blame] | 38 | #include <linux/pm_runtime.h> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 39 | #include <linux/blk-cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 41 | #include <trace/events/block.h> |
| 42 | |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 43 | #include "blk.h" |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 44 | #include "blk-mq-sched.h" |
Bart Van Assche | bca6b06 | 2018-09-26 14:01:03 -0700 | [diff] [blame] | 45 | #include "blk-pm.h" |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 46 | #include "blk-wbt.h" |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(elv_list_lock); |
| 49 | static LIST_HEAD(elv_list); |
| 50 | |
| 51 | /* |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 52 | * Merge hash stuff. |
| 53 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 54 | #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq)) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 55 | |
| 56 | /* |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 57 | * Query io scheduler to see if the current process issuing bio may be |
| 58 | * merged with rq. |
| 59 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 60 | static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio) |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 61 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 62 | struct request_queue *q = rq->q; |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 63 | struct elevator_queue *e = q->elevator; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 64 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 65 | if (e->type->ops.allow_merge) |
| 66 | return e->type->ops.allow_merge(q, rq, bio); |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 67 | |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * can we safely merge with this request? |
| 73 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 74 | bool elv_bio_merge_ok(struct request *rq, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 76 | if (!blk_rq_merge_ok(rq, bio)) |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 77 | return false; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 78 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 79 | if (!elv_iosched_allow_bio_merge(rq, bio)) |
| 80 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 81 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 82 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 84 | EXPORT_SYMBOL(elv_bio_merge_ok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Jens Axboe | 8ac0d9a | 2017-10-25 12:35:02 -0600 | [diff] [blame] | 86 | static bool elevator_match(const struct elevator_type *e, const char *name) |
| 87 | { |
| 88 | if (!strcmp(e->elevator_name, name)) |
| 89 | return true; |
| 90 | if (e->elevator_alias && !strcmp(e->elevator_alias, name)) |
| 91 | return true; |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 96 | /* |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 97 | * Return scheduler with name 'name' |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 98 | */ |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 99 | static struct elevator_type *elevator_find(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 101 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 103 | list_for_each_entry(e, &elv_list, list) { |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 104 | if (elevator_match(e, name)) |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 105 | return e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Vasily Tarasov | a22b169 | 2006-10-11 09:24:27 +0200 | [diff] [blame] | 108 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void elevator_put(struct elevator_type *e) |
| 112 | { |
| 113 | module_put(e->elevator_owner); |
| 114 | } |
| 115 | |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 116 | static struct elevator_type *elevator_get(struct request_queue *q, |
| 117 | const char *name, bool try_loading) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 119 | struct elevator_type *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 121 | spin_lock(&elv_list_lock); |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 122 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 123 | e = elevator_find(name); |
Tejun Heo | 21c3c5d | 2013-01-22 16:48:03 -0800 | [diff] [blame] | 124 | if (!e && try_loading) { |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 125 | spin_unlock(&elv_list_lock); |
Kees Cook | 490b94b | 2011-05-05 18:02:12 -0600 | [diff] [blame] | 126 | request_module("%s-iosched", name); |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 127 | spin_lock(&elv_list_lock); |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 128 | e = elevator_find(name); |
Jens Axboe | e164094 | 2008-02-19 10:20:37 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Tejun Heo | 2824bc93 | 2005-10-20 10:56:41 +0200 | [diff] [blame] | 131 | if (e && !try_module_get(e->elevator_owner)) |
| 132 | e = NULL; |
| 133 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 134 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return e; |
| 136 | } |
| 137 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 138 | static struct kobj_type elv_ktype; |
| 139 | |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 140 | struct elevator_queue *elevator_alloc(struct request_queue *q, |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 141 | struct elevator_type *e) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 142 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 143 | struct elevator_queue *eq; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 144 | |
Joe Perches | c1b511e | 2013-08-29 15:21:42 -0700 | [diff] [blame] | 145 | eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 146 | if (unlikely(!eq)) |
Chao Yu | 8406a4d | 2015-04-23 10:47:44 -0600 | [diff] [blame] | 147 | return NULL; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 148 | |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 149 | eq->type = e; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 150 | kobject_init(&eq->kobj, &elv_ktype); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 151 | mutex_init(&eq->sysfs_lock); |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 152 | hash_init(eq->hash); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 153 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 154 | return eq; |
| 155 | } |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 156 | EXPORT_SYMBOL(elevator_alloc); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 157 | |
| 158 | static void elevator_release(struct kobject *kobj) |
| 159 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 160 | struct elevator_queue *e; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 161 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 162 | e = container_of(kobj, struct elevator_queue, kobj); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 163 | elevator_put(e->type); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 164 | kfree(e); |
| 165 | } |
| 166 | |
Ming Lei | c3e2219 | 2019-06-04 21:08:02 +0800 | [diff] [blame] | 167 | void __elevator_exit(struct request_queue *q, struct elevator_queue *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 169 | mutex_lock(&e->sysfs_lock); |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 170 | if (e->type->ops.exit_sched) |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 171 | blk_mq_exit_sched(q, e); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 172 | mutex_unlock(&e->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 174 | kobject_put(&e->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 176 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 177 | static inline void __elv_rqhash_del(struct request *rq) |
| 178 | { |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 179 | hash_del(&rq->hash); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 180 | rq->rq_flags &= ~RQF_HASHED; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 183 | void elv_rqhash_del(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 184 | { |
| 185 | if (ELV_ON_HASH(rq)) |
| 186 | __elv_rqhash_del(rq); |
| 187 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 188 | EXPORT_SYMBOL_GPL(elv_rqhash_del); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 189 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 190 | void elv_rqhash_add(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 191 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 192 | struct elevator_queue *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 193 | |
| 194 | BUG_ON(ELV_ON_HASH(rq)); |
Sasha Levin | 242d98f | 2012-12-17 10:01:27 -0500 | [diff] [blame] | 195 | hash_add(e->hash, &rq->hash, rq_hash_key(rq)); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 196 | rq->rq_flags |= RQF_HASHED; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 197 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 198 | EXPORT_SYMBOL_GPL(elv_rqhash_add); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 199 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 200 | void elv_rqhash_reposition(struct request_queue *q, struct request *rq) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 201 | { |
| 202 | __elv_rqhash_del(rq); |
| 203 | elv_rqhash_add(q, rq); |
| 204 | } |
| 205 | |
Jens Axboe | 70b3ea0 | 2016-12-07 08:43:31 -0700 | [diff] [blame] | 206 | struct request *elv_rqhash_find(struct request_queue *q, sector_t offset) |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 207 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 208 | struct elevator_queue *e = q->elevator; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 209 | struct hlist_node *next; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 210 | struct request *rq; |
| 211 | |
Linus Torvalds | ee89f81 | 2013-02-28 12:52:24 -0800 | [diff] [blame] | 212 | hash_for_each_possible_safe(e->hash, rq, next, hash, offset) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 213 | BUG_ON(!ELV_ON_HASH(rq)); |
| 214 | |
| 215 | if (unlikely(!rq_mergeable(rq))) { |
| 216 | __elv_rqhash_del(rq); |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | if (rq_hash_key(rq) == offset) |
| 221 | return rq; |
| 222 | } |
| 223 | |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Tejun Heo | 8922e16 | 2005-10-20 16:23:44 +0200 | [diff] [blame] | 227 | /* |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 228 | * RB-tree support functions for inserting/lookup/removal of requests |
| 229 | * in a sorted RB tree. |
| 230 | */ |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 231 | void elv_rb_add(struct rb_root *root, struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 232 | { |
| 233 | struct rb_node **p = &root->rb_node; |
| 234 | struct rb_node *parent = NULL; |
| 235 | struct request *__rq; |
| 236 | |
| 237 | while (*p) { |
| 238 | parent = *p; |
| 239 | __rq = rb_entry(parent, struct request, rb_node); |
| 240 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 241 | if (blk_rq_pos(rq) < blk_rq_pos(__rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 242 | p = &(*p)->rb_left; |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 243 | else if (blk_rq_pos(rq) >= blk_rq_pos(__rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 244 | p = &(*p)->rb_right; |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | rb_link_node(&rq->rb_node, parent, p); |
| 248 | rb_insert_color(&rq->rb_node, root); |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 249 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 250 | EXPORT_SYMBOL(elv_rb_add); |
| 251 | |
| 252 | void elv_rb_del(struct rb_root *root, struct request *rq) |
| 253 | { |
| 254 | BUG_ON(RB_EMPTY_NODE(&rq->rb_node)); |
| 255 | rb_erase(&rq->rb_node, root); |
| 256 | RB_CLEAR_NODE(&rq->rb_node); |
| 257 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 258 | EXPORT_SYMBOL(elv_rb_del); |
| 259 | |
| 260 | struct request *elv_rb_find(struct rb_root *root, sector_t sector) |
| 261 | { |
| 262 | struct rb_node *n = root->rb_node; |
| 263 | struct request *rq; |
| 264 | |
| 265 | while (n) { |
| 266 | rq = rb_entry(n, struct request, rb_node); |
| 267 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 268 | if (sector < blk_rq_pos(rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 269 | n = n->rb_left; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 270 | else if (sector > blk_rq_pos(rq)) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 271 | n = n->rb_right; |
| 272 | else |
| 273 | return rq; |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 278 | EXPORT_SYMBOL(elv_rb_find); |
| 279 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 280 | enum elv_merge elv_merge(struct request_queue *q, struct request **req, |
| 281 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 283 | struct elevator_queue *e = q->elevator; |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 284 | struct request *__rq; |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 285 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 286 | /* |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 287 | * Levels of merges: |
| 288 | * nomerges: No merges at all attempted |
| 289 | * noxmerges: Only simple one-hit cache try |
| 290 | * merges: All merge tries attempted |
| 291 | */ |
Ming Lei | 7460d38 | 2015-10-20 23:13:55 +0800 | [diff] [blame] | 292 | if (blk_queue_nomerges(q) || !bio_mergeable(bio)) |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 293 | return ELEVATOR_NO_MERGE; |
| 294 | |
| 295 | /* |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 296 | * First try one-hit cache. |
| 297 | */ |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 298 | if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) { |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 299 | enum elv_merge ret = blk_try_merge(q->last_merge, bio); |
| 300 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 301 | if (ret != ELEVATOR_NO_MERGE) { |
| 302 | *req = q->last_merge; |
| 303 | return ret; |
| 304 | } |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 307 | if (blk_queue_noxmerges(q)) |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 308 | return ELEVATOR_NO_MERGE; |
| 309 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 310 | /* |
| 311 | * See if our hash lookup can find a potential backmerge. |
| 312 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 313 | __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector); |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 314 | if (__rq && elv_bio_merge_ok(__rq, bio)) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 315 | *req = __rq; |
| 316 | return ELEVATOR_BACK_MERGE; |
| 317 | } |
| 318 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 319 | if (e->type->ops.request_merge) |
| 320 | return e->type->ops.request_merge(q, req, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | return ELEVATOR_NO_MERGE; |
| 323 | } |
| 324 | |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 325 | /* |
| 326 | * Attempt to do an insertion back merge. Only check for the case where |
| 327 | * we can append 'rq' to an existing request, so we can throw 'rq' away |
| 328 | * afterwards. |
| 329 | * |
| 330 | * Returns true if we merged, false otherwise |
| 331 | */ |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 332 | bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq) |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 333 | { |
| 334 | struct request *__rq; |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 335 | bool ret; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 336 | |
| 337 | if (blk_queue_nomerges(q)) |
| 338 | return false; |
| 339 | |
| 340 | /* |
| 341 | * First try one-hit cache. |
| 342 | */ |
| 343 | if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq)) |
| 344 | return true; |
| 345 | |
| 346 | if (blk_queue_noxmerges(q)) |
| 347 | return false; |
| 348 | |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 349 | ret = false; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 350 | /* |
| 351 | * See if our hash lookup can find a potential backmerge. |
| 352 | */ |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 353 | while (1) { |
| 354 | __rq = elv_rqhash_find(q, blk_rq_pos(rq)); |
| 355 | if (!__rq || !blk_attempt_req_merge(q, __rq, rq)) |
| 356 | break; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 357 | |
Shaohua Li | bee0393 | 2012-11-09 08:44:27 +0100 | [diff] [blame] | 358 | /* The merged request could be merged with others, try again */ |
| 359 | ret = true; |
| 360 | rq = __rq; |
| 361 | } |
| 362 | |
| 363 | return ret; |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 366 | void elv_merged_request(struct request_queue *q, struct request *rq, |
| 367 | enum elv_merge type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 369 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 371 | if (e->type->ops.request_merged) |
| 372 | e->type->ops.request_merged(q, rq, type); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 373 | |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 374 | if (type == ELEVATOR_BACK_MERGE) |
| 375 | elv_rqhash_reposition(q, rq); |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 376 | |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 377 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 380 | void elv_merge_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | struct request *next) |
| 382 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 383 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 385 | if (e->type->ops.requests_merged) |
| 386 | e->type->ops.requests_merged(q, rq, next); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 387 | |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 388 | elv_rqhash_reposition(q, rq); |
Tejun Heo | 06b8624 | 2005-10-20 16:46:23 +0200 | [diff] [blame] | 389 | q->last_merge = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 392 | struct request *elv_latter_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 394 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 396 | if (e->type->ops.next_request) |
| 397 | return e->type->ops.next_request(q, rq); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return NULL; |
| 400 | } |
| 401 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 402 | struct request *elv_former_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 404 | struct elevator_queue *e = q->elevator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 406 | if (e->type->ops.former_request) |
| 407 | return e->type->ops.former_request(q, rq); |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
| 411 | |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 412 | #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr) |
| 413 | |
| 414 | static ssize_t |
| 415 | elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 416 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 417 | struct elv_fs_entry *entry = to_elv(attr); |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 418 | struct elevator_queue *e; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 419 | ssize_t error; |
| 420 | |
| 421 | if (!entry->show) |
| 422 | return -EIO; |
| 423 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 424 | e = container_of(kobj, struct elevator_queue, kobj); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 425 | mutex_lock(&e->sysfs_lock); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 426 | error = e->type ? entry->show(e, page) : -ENOENT; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 427 | mutex_unlock(&e->sysfs_lock); |
| 428 | return error; |
| 429 | } |
| 430 | |
| 431 | static ssize_t |
| 432 | elv_attr_store(struct kobject *kobj, struct attribute *attr, |
| 433 | const char *page, size_t length) |
| 434 | { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 435 | struct elv_fs_entry *entry = to_elv(attr); |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 436 | struct elevator_queue *e; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 437 | ssize_t error; |
| 438 | |
| 439 | if (!entry->store) |
| 440 | return -EIO; |
| 441 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 442 | e = container_of(kobj, struct elevator_queue, kobj); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 443 | mutex_lock(&e->sysfs_lock); |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 444 | error = e->type ? entry->store(e, page, length) : -ENOENT; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 445 | mutex_unlock(&e->sysfs_lock); |
| 446 | return error; |
| 447 | } |
| 448 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 449 | static const struct sysfs_ops elv_sysfs_ops = { |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 450 | .show = elv_attr_show, |
| 451 | .store = elv_attr_store, |
| 452 | }; |
| 453 | |
| 454 | static struct kobj_type elv_ktype = { |
| 455 | .sysfs_ops = &elv_sysfs_ops, |
| 456 | .release = elevator_release, |
| 457 | }; |
| 458 | |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 459 | /* |
| 460 | * elv_register_queue is called from either blk_register_queue or |
| 461 | * elevator_switch, elevator switch is prevented from being happen |
| 462 | * in the two paths, so it is safe to not hold q->sysfs_lock. |
| 463 | */ |
| 464 | int elv_register_queue(struct request_queue *q, bool uevent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Tejun Heo | 5a5bafd | 2012-03-05 13:14:56 -0800 | [diff] [blame] | 466 | struct elevator_queue *e = q->elevator; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 467 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 469 | error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched"); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 470 | if (!error) { |
Tejun Heo | 22f746e | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 471 | struct elv_fs_entry *attr = e->type->elevator_attrs; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 472 | if (attr) { |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 473 | while (attr->attr.name) { |
| 474 | if (sysfs_create_file(&e->kobj, &attr->attr)) |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 475 | break; |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 476 | attr++; |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 477 | } |
| 478 | } |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 479 | if (uevent) |
| 480 | kobject_uevent(&e->kobj, KOBJ_ADD); |
| 481 | |
| 482 | mutex_lock(&q->sysfs_lock); |
Jens Axboe | 430c62f | 2010-10-07 09:35:16 +0200 | [diff] [blame] | 483 | e->registered = 1; |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 484 | mutex_unlock(&q->sysfs_lock); |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 485 | } |
| 486 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
Jens Axboe | bc1c116 | 2006-06-08 08:49:06 +0200 | [diff] [blame] | 488 | |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 489 | /* |
| 490 | * elv_unregister_queue is called from either blk_unregister_queue or |
| 491 | * elevator_switch, elevator switch is prevented from being happen |
| 492 | * in the two paths, so it is safe to not hold q->sysfs_lock. |
| 493 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | void elv_unregister_queue(struct request_queue *q) |
| 495 | { |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 496 | if (q) { |
| 497 | struct elevator_queue *e = q->elevator; |
| 498 | |
| 499 | kobject_uevent(&e->kobj, KOBJ_REMOVE); |
| 500 | kobject_del(&e->kobj); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 501 | |
| 502 | mutex_lock(&q->sysfs_lock); |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 503 | e->registered = 0; |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 504 | /* Re-enable throttling in case elevator disabled it */ |
| 505 | wbt_enable_default(q); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 506 | mutex_unlock(&q->sysfs_lock); |
Tejun Heo | f8fc877 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Jens Axboe | e567bf7 | 2014-06-22 16:32:48 -0600 | [diff] [blame] | 510 | int elv_register(struct elevator_type *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 512 | /* create icq_cache if requested */ |
| 513 | if (e->icq_size) { |
| 514 | if (WARN_ON(e->icq_size < sizeof(struct io_cq)) || |
| 515 | WARN_ON(e->icq_align < __alignof__(struct io_cq))) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | snprintf(e->icq_cache_name, sizeof(e->icq_cache_name), |
| 519 | "%s_io_cq", e->elevator_name); |
| 520 | e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size, |
| 521 | e->icq_align, 0, NULL); |
| 522 | if (!e->icq_cache) |
| 523 | return -ENOMEM; |
| 524 | } |
| 525 | |
| 526 | /* register, don't allow duplicate names */ |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 527 | spin_lock(&elv_list_lock); |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 528 | if (elevator_find(e->elevator_name)) { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 529 | spin_unlock(&elv_list_lock); |
Chengguang Xu | 62d2a19 | 2018-08-28 07:31:11 +0800 | [diff] [blame] | 530 | kmem_cache_destroy(e->icq_cache); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 531 | return -EBUSY; |
| 532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | list_add_tail(&e->list, &elv_list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 534 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Hisao Tanabe | d0b0a81 | 2019-04-08 00:27:42 +0900 | [diff] [blame] | 536 | printk(KERN_INFO "io scheduler %s registered\n", e->elevator_name); |
| 537 | |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 538 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | EXPORT_SYMBOL_GPL(elv_register); |
| 541 | |
| 542 | void elv_unregister(struct elevator_type *e) |
| 543 | { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 544 | /* unregister */ |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 545 | spin_lock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | list_del_init(&e->list); |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 547 | spin_unlock(&elv_list_lock); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Destroy icq_cache if it exists. icq's are RCU managed. Make |
| 551 | * sure all RCU operations are complete before proceeding. |
| 552 | */ |
| 553 | if (e->icq_cache) { |
| 554 | rcu_barrier(); |
| 555 | kmem_cache_destroy(e->icq_cache); |
| 556 | e->icq_cache = NULL; |
| 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | EXPORT_SYMBOL_GPL(elv_unregister); |
| 560 | |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 561 | int elevator_switch_mq(struct request_queue *q, |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 562 | struct elevator_type *new_e) |
| 563 | { |
| 564 | int ret; |
| 565 | |
Bart Van Assche | 14a2349 | 2018-01-17 11:48:09 -0800 | [diff] [blame] | 566 | lockdep_assert_held(&q->sysfs_lock); |
| 567 | |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 568 | if (q->elevator) { |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 569 | if (q->elevator->registered) { |
| 570 | mutex_unlock(&q->sysfs_lock); |
| 571 | |
| 572 | /* |
| 573 | * Concurrent elevator switch can't happen becasue |
| 574 | * sysfs write is always exclusively on same file. |
| 575 | * |
| 576 | * Also the elevator queue won't be freed after |
| 577 | * sysfs_lock is released becasue kobject_del() in |
| 578 | * blk_unregister_queue() waits for completion of |
| 579 | * .store & .show on its attributes. |
| 580 | */ |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 581 | elv_unregister_queue(q); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 582 | |
| 583 | mutex_lock(&q->sysfs_lock); |
| 584 | } |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 585 | ioc_clear_queue(q); |
| 586 | elevator_exit(q, q->elevator); |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * sysfs_lock may be dropped, so re-check if queue is |
| 590 | * unregistered. If yes, don't switch to new elevator |
| 591 | * any more |
| 592 | */ |
| 593 | if (!blk_queue_registered(q)) |
| 594 | return 0; |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | ret = blk_mq_init_sched(q, new_e); |
| 598 | if (ret) |
| 599 | goto out; |
| 600 | |
| 601 | if (new_e) { |
Ming Lei | cecf5d8 | 2019-08-27 19:01:48 +0800 | [diff] [blame] | 602 | mutex_unlock(&q->sysfs_lock); |
| 603 | |
| 604 | ret = elv_register_queue(q, true); |
| 605 | |
| 606 | mutex_lock(&q->sysfs_lock); |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 607 | if (ret) { |
| 608 | elevator_exit(q, q->elevator); |
| 609 | goto out; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (new_e) |
| 614 | blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name); |
| 615 | else |
| 616 | blk_add_trace_msg(q, "elv switch: none"); |
| 617 | |
| 618 | out: |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 619 | return ret; |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 620 | } |
| 621 | |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 622 | static inline bool elv_support_iosched(struct request_queue *q) |
| 623 | { |
| 624 | if (q->tag_set && (q->tag_set->flags & BLK_MQ_F_NO_SCHED)) |
| 625 | return false; |
| 626 | return true; |
| 627 | } |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 630 | * For blk-mq devices supporting IO scheduling, we default to using mq-deadline, |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 631 | * if available, for single queue devices. If deadline isn't available OR |
| 632 | * deadline initialization fails OR we have multiple queues, default to "none". |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 633 | */ |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 634 | void elevator_init_mq(struct request_queue *q) |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 635 | { |
| 636 | struct elevator_type *e; |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 637 | int err; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 638 | |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 639 | if (!elv_support_iosched(q)) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 640 | return; |
Damien Le Moal | 61db437 | 2019-09-05 18:51:29 +0900 | [diff] [blame] | 641 | |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 642 | if (q->nr_hw_queues != 1) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 643 | return; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 644 | |
Ming Lei | c48dac1 | 2019-08-27 19:01:45 +0800 | [diff] [blame] | 645 | WARN_ON_ONCE(test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags)); |
| 646 | |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 647 | if (unlikely(q->elevator)) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 648 | return; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 649 | |
| 650 | e = elevator_get(q, "mq-deadline", false); |
| 651 | if (!e) |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 652 | return; |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 653 | |
| 654 | err = blk_mq_init_sched(q, e); |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 655 | if (err) { |
| 656 | pr_warn("\"%s\" elevator initialization failed, " |
| 657 | "falling back to \"none\"\n", e->elevator_name); |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 658 | elevator_put(e); |
Damien Le Moal | 954b4a5 | 2019-09-05 18:51:30 +0900 | [diff] [blame^] | 659 | } |
Christoph Hellwig | 131d08e | 2018-05-31 19:11:40 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
| 663 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | * switch to new_e io scheduler. be careful not to introduce deadlocks - |
| 665 | * we don't free the old io scheduler, before we have allocated what we |
| 666 | * need for the new one. this way we have a chance of going back to the old |
Tejun Heo | cb98fc8 | 2005-10-28 08:29:39 +0200 | [diff] [blame] | 667 | * one, if the new one fails init for some reason. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 669 | static int elevator_switch(struct request_queue *q, struct elevator_type *new_e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 671 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Bart Van Assche | 14a2349 | 2018-01-17 11:48:09 -0800 | [diff] [blame] | 673 | lockdep_assert_held(&q->sysfs_lock); |
| 674 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 675 | blk_mq_freeze_queue(q); |
| 676 | blk_mq_quiesce_queue(q); |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 677 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 678 | err = elevator_switch_mq(q, new_e); |
Jianchao Wang | d48ece2 | 2018-08-21 15:15:03 +0800 | [diff] [blame] | 679 | |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 680 | blk_mq_unquiesce_queue(q); |
| 681 | blk_mq_unfreeze_queue(q); |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 682 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 683 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 686 | /* |
| 687 | * Switch this queue to the given IO scheduler. |
| 688 | */ |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 689 | static int __elevator_change(struct request_queue *q, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
| 691 | char elevator_name[ELV_NAME_MAX]; |
| 692 | struct elevator_type *e; |
| 693 | |
David Jeffery | e9a823f | 2017-08-28 10:52:44 -0600 | [diff] [blame] | 694 | /* Make sure queue is not in the middle of being removed */ |
Ming Lei | 58c898b | 2019-08-27 19:01:47 +0800 | [diff] [blame] | 695 | if (!blk_queue_registered(q)) |
David Jeffery | e9a823f | 2017-08-28 10:52:44 -0600 | [diff] [blame] | 696 | return -ENOENT; |
| 697 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 698 | /* |
| 699 | * Special case for mq, turn off scheduling |
| 700 | */ |
Aleksei Zakharov | fbd7212 | 2019-02-11 13:50:37 +0300 | [diff] [blame] | 701 | if (!strncmp(name, "none", 4)) { |
| 702 | if (!q->elevator) |
| 703 | return 0; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 704 | return elevator_switch(q, NULL); |
Aleksei Zakharov | fbd7212 | 2019-02-11 13:50:37 +0300 | [diff] [blame] | 705 | } |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 706 | |
Li Zefan | ee2e992 | 2008-10-14 08:49:56 +0200 | [diff] [blame] | 707 | strlcpy(elevator_name, name, sizeof(elevator_name)); |
Jens Axboe | 2527d99 | 2017-10-25 12:33:42 -0600 | [diff] [blame] | 708 | e = elevator_get(q, strstrip(elevator_name), true); |
Jens Axboe | 340ff32 | 2017-05-10 07:40:04 -0600 | [diff] [blame] | 709 | if (!e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Jens Axboe | 8ac0d9a | 2017-10-25 12:35:02 -0600 | [diff] [blame] | 712 | if (q->elevator && elevator_match(q->elevator->type, elevator_name)) { |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 713 | elevator_put(e); |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 714 | return 0; |
Nate Diller | 2ca7d93 | 2005-10-30 15:02:24 -0800 | [diff] [blame] | 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 717 | return elevator_switch(q, e); |
| 718 | } |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 719 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 720 | ssize_t elv_iosched_store(struct request_queue *q, const char *name, |
| 721 | size_t count) |
| 722 | { |
| 723 | int ret; |
| 724 | |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 725 | if (!queue_is_mq(q) || !elv_support_iosched(q)) |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 726 | return count; |
| 727 | |
Tomoki Sekiyama | 7c8a367 | 2013-10-15 16:42:19 -0600 | [diff] [blame] | 728 | ret = __elevator_change(q, name); |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 729 | if (!ret) |
| 730 | return count; |
| 731 | |
Jens Axboe | 5dd531a | 2010-08-23 13:52:19 +0200 | [diff] [blame] | 732 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 735 | ssize_t elv_iosched_show(struct request_queue *q, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 737 | struct elevator_queue *e = q->elevator; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 738 | struct elevator_type *elv = NULL; |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 739 | struct elevator_type *__e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | int len = 0; |
| 741 | |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 742 | if (!queue_is_mq(q)) |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 743 | return sprintf(name, "none\n"); |
| 744 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 745 | if (!q->elevator) |
| 746 | len += sprintf(name+len, "[none] "); |
| 747 | else |
| 748 | elv = e->type; |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 749 | |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 750 | spin_lock(&elv_list_lock); |
Matthias Kaehlcke | 70cee26 | 2007-07-10 12:26:24 +0200 | [diff] [blame] | 751 | list_for_each_entry(__e, &elv_list, list) { |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 752 | if (elv && elevator_match(elv, __e->elevator_name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | len += sprintf(name+len, "[%s] ", elv->elevator_name); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 754 | continue; |
| 755 | } |
Jens Axboe | a1ce35f | 2018-10-29 10:23:51 -0600 | [diff] [blame] | 756 | if (elv_support_iosched(q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | len += sprintf(name+len, "%s ", __e->elevator_name); |
| 758 | } |
Jens Axboe | 2a12dcd | 2007-04-26 14:41:53 +0200 | [diff] [blame] | 759 | spin_unlock(&elv_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 761 | if (q->elevator) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 762 | len += sprintf(name+len, "none"); |
| 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | len += sprintf(len+name, "\n"); |
| 765 | return len; |
| 766 | } |
| 767 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 768 | struct request *elv_rb_former_request(struct request_queue *q, |
| 769 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 770 | { |
| 771 | struct rb_node *rbprev = rb_prev(&rq->rb_node); |
| 772 | |
| 773 | if (rbprev) |
| 774 | return rb_entry_rq(rbprev); |
| 775 | |
| 776 | return NULL; |
| 777 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 778 | EXPORT_SYMBOL(elv_rb_former_request); |
| 779 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 780 | struct request *elv_rb_latter_request(struct request_queue *q, |
| 781 | struct request *rq) |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 782 | { |
| 783 | struct rb_node *rbnext = rb_next(&rq->rb_node); |
| 784 | |
| 785 | if (rbnext) |
| 786 | return rb_entry_rq(rbnext); |
| 787 | |
| 788 | return NULL; |
| 789 | } |
Jens Axboe | 2e662b6 | 2006-07-13 11:55:04 +0200 | [diff] [blame] | 790 | EXPORT_SYMBOL(elv_rb_latter_request); |