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