Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler, |
| 4 | * for the blk-mq scheduling framework |
| 5 | * |
| 6 | * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk> |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/blk-mq.h> |
| 12 | #include <linux/elevator.h> |
| 13 | #include <linux/bio.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/compiler.h> |
| 18 | #include <linux/rbtree.h> |
| 19 | #include <linux/sbitmap.h> |
| 20 | |
Chaitanya Kulkarni | b357e4a | 2021-02-21 21:29:59 -0800 | [diff] [blame] | 21 | #include <trace/events/block.h> |
| 22 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 23 | #include "blk.h" |
| 24 | #include "blk-mq.h" |
Omar Sandoval | daaadb3 | 2017-05-04 00:31:34 -0700 | [diff] [blame] | 25 | #include "blk-mq-debugfs.h" |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 26 | #include "blk-mq-tag.h" |
| 27 | #include "blk-mq-sched.h" |
| 28 | |
| 29 | /* |
Mauro Carvalho Chehab | 898bd37 | 2019-04-18 19:45:00 -0300 | [diff] [blame] | 30 | * See Documentation/block/deadline-iosched.rst |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 31 | */ |
| 32 | static const int read_expire = HZ / 2; /* max time before a read is submitted. */ |
| 33 | static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */ |
| 34 | static const int writes_starved = 2; /* max times reads can starve a write */ |
| 35 | static const int fifo_batch = 16; /* # of sequential requests treated as one |
| 36 | by the above parameters. For throughput. */ |
| 37 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 38 | enum dd_data_dir { |
| 39 | DD_READ = READ, |
| 40 | DD_WRITE = WRITE, |
| 41 | }; |
| 42 | |
| 43 | enum { DD_DIR_COUNT = 2 }; |
| 44 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 45 | struct deadline_data { |
| 46 | /* |
| 47 | * run time data |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * requests (deadline_rq s) are present on both sort_list and fifo_list |
| 52 | */ |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 53 | struct rb_root sort_list[DD_DIR_COUNT]; |
| 54 | struct list_head fifo_list[DD_DIR_COUNT]; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * next in sort order. read, write or both are NULL |
| 58 | */ |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 59 | struct request *next_rq[DD_DIR_COUNT]; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 60 | unsigned int batching; /* number of sequential requests made */ |
| 61 | unsigned int starved; /* times reads have starved writes */ |
| 62 | |
| 63 | /* |
| 64 | * settings that change how the i/o scheduler behaves |
| 65 | */ |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 66 | int fifo_expire[DD_DIR_COUNT]; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 67 | int fifo_batch; |
| 68 | int writes_starved; |
| 69 | int front_merges; |
| 70 | |
| 71 | spinlock_t lock; |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 72 | spinlock_t zone_lock; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 73 | struct list_head dispatch; |
| 74 | }; |
| 75 | |
| 76 | static inline struct rb_root * |
| 77 | deadline_rb_root(struct deadline_data *dd, struct request *rq) |
| 78 | { |
| 79 | return &dd->sort_list[rq_data_dir(rq)]; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * get the request after `rq' in sector-sorted order |
| 84 | */ |
| 85 | static inline struct request * |
| 86 | deadline_latter_request(struct request *rq) |
| 87 | { |
| 88 | struct rb_node *node = rb_next(&rq->rb_node); |
| 89 | |
| 90 | if (node) |
| 91 | return rb_entry_rq(node); |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | deadline_add_rq_rb(struct deadline_data *dd, struct request *rq) |
| 98 | { |
| 99 | struct rb_root *root = deadline_rb_root(dd, rq); |
| 100 | |
| 101 | elv_rb_add(root, rq); |
| 102 | } |
| 103 | |
| 104 | static inline void |
| 105 | deadline_del_rq_rb(struct deadline_data *dd, struct request *rq) |
| 106 | { |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 107 | const enum dd_data_dir data_dir = rq_data_dir(rq); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 108 | |
| 109 | if (dd->next_rq[data_dir] == rq) |
| 110 | dd->next_rq[data_dir] = deadline_latter_request(rq); |
| 111 | |
| 112 | elv_rb_del(deadline_rb_root(dd, rq), rq); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * remove rq from rbtree and fifo. |
| 117 | */ |
| 118 | static void deadline_remove_request(struct request_queue *q, struct request *rq) |
| 119 | { |
| 120 | struct deadline_data *dd = q->elevator->elevator_data; |
| 121 | |
| 122 | list_del_init(&rq->queuelist); |
| 123 | |
| 124 | /* |
| 125 | * We might not be on the rbtree, if we are doing an insert merge |
| 126 | */ |
| 127 | if (!RB_EMPTY_NODE(&rq->rb_node)) |
| 128 | deadline_del_rq_rb(dd, rq); |
| 129 | |
| 130 | elv_rqhash_del(q, rq); |
| 131 | if (q->last_merge == rq) |
| 132 | q->last_merge = NULL; |
| 133 | } |
| 134 | |
| 135 | static void dd_request_merged(struct request_queue *q, struct request *req, |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 136 | enum elv_merge type) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 137 | { |
| 138 | struct deadline_data *dd = q->elevator->elevator_data; |
| 139 | |
| 140 | /* |
| 141 | * if the merge was a front merge, we need to reposition request |
| 142 | */ |
| 143 | if (type == ELEVATOR_FRONT_MERGE) { |
| 144 | elv_rb_del(deadline_rb_root(dd, req), req); |
| 145 | deadline_add_rq_rb(dd, req); |
| 146 | } |
| 147 | } |
| 148 | |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Callback function that is invoked after @next has been merged into @req. |
| 151 | */ |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 152 | static void dd_merged_requests(struct request_queue *q, struct request *req, |
| 153 | struct request *next) |
| 154 | { |
| 155 | /* |
| 156 | * if next expires before rq, assign its expire time to rq |
| 157 | * and move into next position (next will be deleted) in fifo |
| 158 | */ |
| 159 | if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) { |
| 160 | if (time_before((unsigned long)next->fifo_time, |
| 161 | (unsigned long)req->fifo_time)) { |
| 162 | list_move(&req->queuelist, &next->queuelist); |
| 163 | req->fifo_time = next->fifo_time; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * kill knowledge of next, this one is a goner |
| 169 | */ |
| 170 | deadline_remove_request(q, next); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * move an entry to dispatch queue |
| 175 | */ |
| 176 | static void |
| 177 | deadline_move_request(struct deadline_data *dd, struct request *rq) |
| 178 | { |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 179 | const enum dd_data_dir data_dir = rq_data_dir(rq); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 180 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 181 | dd->next_rq[DD_READ] = NULL; |
| 182 | dd->next_rq[DD_WRITE] = NULL; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 183 | dd->next_rq[data_dir] = deadline_latter_request(rq); |
| 184 | |
| 185 | /* |
| 186 | * take it off the sort and fifo list |
| 187 | */ |
| 188 | deadline_remove_request(rq->q, rq); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * deadline_check_fifo returns 0 if there are no expired requests on the fifo, |
| 193 | * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir]) |
| 194 | */ |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 195 | static inline int deadline_check_fifo(struct deadline_data *dd, |
| 196 | enum dd_data_dir data_dir) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 197 | { |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 198 | struct request *rq = rq_entry_fifo(dd->fifo_list[data_dir].next); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * rq is expired! |
| 202 | */ |
| 203 | if (time_after_eq(jiffies, (unsigned long)rq->fifo_time)) |
| 204 | return 1; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 210 | * For the specified data direction, return the next request to |
| 211 | * dispatch using arrival ordered lists. |
| 212 | */ |
| 213 | static struct request * |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 214 | deadline_fifo_request(struct deadline_data *dd, enum dd_data_dir data_dir) |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 215 | { |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 216 | struct request *rq; |
| 217 | unsigned long flags; |
| 218 | |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 219 | if (list_empty(&dd->fifo_list[data_dir])) |
| 220 | return NULL; |
| 221 | |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 222 | rq = rq_entry_fifo(dd->fifo_list[data_dir].next); |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 223 | if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q)) |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 224 | return rq; |
| 225 | |
| 226 | /* |
| 227 | * Look for a write request that can be dispatched, that is one with |
| 228 | * an unlocked target zone. |
| 229 | */ |
| 230 | spin_lock_irqsave(&dd->zone_lock, flags); |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 231 | list_for_each_entry(rq, &dd->fifo_list[DD_WRITE], queuelist) { |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 232 | if (blk_req_can_dispatch_to_zone(rq)) |
| 233 | goto out; |
| 234 | } |
| 235 | rq = NULL; |
| 236 | out: |
| 237 | spin_unlock_irqrestore(&dd->zone_lock, flags); |
| 238 | |
| 239 | return rq; |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * For the specified data direction, return the next request to |
| 244 | * dispatch using sector position sorted lists. |
| 245 | */ |
| 246 | static struct request * |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 247 | deadline_next_request(struct deadline_data *dd, enum dd_data_dir data_dir) |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 248 | { |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 249 | struct request *rq; |
| 250 | unsigned long flags; |
| 251 | |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 252 | rq = dd->next_rq[data_dir]; |
| 253 | if (!rq) |
| 254 | return NULL; |
| 255 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 256 | if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q)) |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 257 | return rq; |
| 258 | |
| 259 | /* |
| 260 | * Look for a write request that can be dispatched, that is one with |
| 261 | * an unlocked target zone. |
| 262 | */ |
| 263 | spin_lock_irqsave(&dd->zone_lock, flags); |
| 264 | while (rq) { |
| 265 | if (blk_req_can_dispatch_to_zone(rq)) |
| 266 | break; |
| 267 | rq = deadline_latter_request(rq); |
| 268 | } |
| 269 | spin_unlock_irqrestore(&dd->zone_lock, flags); |
| 270 | |
| 271 | return rq; |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 275 | * deadline_dispatch_requests selects the best request according to |
| 276 | * read/write expire, fifo_batch, etc |
| 277 | */ |
Jens Axboe | ca11f20 | 2018-01-06 09:23:11 -0700 | [diff] [blame] | 278 | static struct request *__dd_dispatch_request(struct deadline_data *dd) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 279 | { |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 280 | struct request *rq, *next_rq; |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 281 | enum dd_data_dir data_dir; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 282 | |
Bart Van Assche | 3bd473f | 2021-06-17 17:44:46 -0700 | [diff] [blame] | 283 | lockdep_assert_held(&dd->lock); |
| 284 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 285 | if (!list_empty(&dd->dispatch)) { |
| 286 | rq = list_first_entry(&dd->dispatch, struct request, queuelist); |
| 287 | list_del_init(&rq->queuelist); |
| 288 | goto done; |
| 289 | } |
| 290 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 291 | /* |
| 292 | * batches are currently reads XOR writes |
| 293 | */ |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 294 | rq = deadline_next_request(dd, DD_WRITE); |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 295 | if (!rq) |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 296 | rq = deadline_next_request(dd, DD_READ); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 297 | |
| 298 | if (rq && dd->batching < dd->fifo_batch) |
| 299 | /* we have a next request are still entitled to batch */ |
| 300 | goto dispatch_request; |
| 301 | |
| 302 | /* |
| 303 | * at this point we are not running a batch. select the appropriate |
| 304 | * data direction (read / write) |
| 305 | */ |
| 306 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 307 | if (!list_empty(&dd->fifo_list[DD_READ])) { |
| 308 | BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_READ])); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 309 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 310 | if (deadline_fifo_request(dd, DD_WRITE) && |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 311 | (dd->starved++ >= dd->writes_starved)) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 312 | goto dispatch_writes; |
| 313 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 314 | data_dir = DD_READ; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 315 | |
| 316 | goto dispatch_find_request; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * there are either no reads or writes have been starved |
| 321 | */ |
| 322 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 323 | if (!list_empty(&dd->fifo_list[DD_WRITE])) { |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 324 | dispatch_writes: |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 325 | BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_WRITE])); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 326 | |
| 327 | dd->starved = 0; |
| 328 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 329 | data_dir = DD_WRITE; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 330 | |
| 331 | goto dispatch_find_request; |
| 332 | } |
| 333 | |
| 334 | return NULL; |
| 335 | |
| 336 | dispatch_find_request: |
| 337 | /* |
| 338 | * we are not running a batch, find best request for selected data_dir |
| 339 | */ |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 340 | next_rq = deadline_next_request(dd, data_dir); |
| 341 | if (deadline_check_fifo(dd, data_dir) || !next_rq) { |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 342 | /* |
| 343 | * A deadline has expired, the last request was in the other |
| 344 | * direction, or we have run out of higher-sectored requests. |
| 345 | * Start again from the request with the earliest expiry time. |
| 346 | */ |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 347 | rq = deadline_fifo_request(dd, data_dir); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 348 | } else { |
| 349 | /* |
| 350 | * The last req was the same dir and we have a next request in |
| 351 | * sort order. No expired requests so continue on from here. |
| 352 | */ |
Damien Le Moal | bf09ce5 | 2017-12-21 15:43:39 +0900 | [diff] [blame] | 353 | rq = next_rq; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 356 | /* |
| 357 | * For a zoned block device, if we only have writes queued and none of |
| 358 | * them can be dispatched, rq will be NULL. |
| 359 | */ |
| 360 | if (!rq) |
| 361 | return NULL; |
| 362 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 363 | dd->batching = 0; |
| 364 | |
| 365 | dispatch_request: |
| 366 | /* |
| 367 | * rq is the selected appropriate request. |
| 368 | */ |
| 369 | dd->batching++; |
| 370 | deadline_move_request(dd, rq); |
| 371 | done: |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 372 | /* |
| 373 | * If the request needs its target zone locked, do it. |
| 374 | */ |
| 375 | blk_req_zone_write_lock(rq); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 376 | rq->rq_flags |= RQF_STARTED; |
| 377 | return rq; |
| 378 | } |
| 379 | |
Jens Axboe | ca11f20 | 2018-01-06 09:23:11 -0700 | [diff] [blame] | 380 | /* |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 381 | * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests(). |
| 382 | * |
Jens Axboe | ca11f20 | 2018-01-06 09:23:11 -0700 | [diff] [blame] | 383 | * One confusing aspect here is that we get called for a specific |
Damien Le Moal | 7211aef8 | 2018-12-17 15:14:05 +0900 | [diff] [blame] | 384 | * hardware queue, but we may return a request that is for a |
Jens Axboe | ca11f20 | 2018-01-06 09:23:11 -0700 | [diff] [blame] | 385 | * different hardware queue. This is because mq-deadline has shared |
| 386 | * state for all hardware queues, in terms of sorting, FIFOs, etc. |
| 387 | */ |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 388 | static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 389 | { |
| 390 | struct deadline_data *dd = hctx->queue->elevator->elevator_data; |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 391 | struct request *rq; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 392 | |
| 393 | spin_lock(&dd->lock); |
Jens Axboe | ca11f20 | 2018-01-06 09:23:11 -0700 | [diff] [blame] | 394 | rq = __dd_dispatch_request(dd); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 395 | spin_unlock(&dd->lock); |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 396 | |
| 397 | return rq; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Bart Van Assche | 3e9a99e | 2021-06-17 17:44:48 -0700 | [diff] [blame] | 400 | static void dd_exit_sched(struct elevator_queue *e) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 401 | { |
| 402 | struct deadline_data *dd = e->elevator_data; |
| 403 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 404 | BUG_ON(!list_empty(&dd->fifo_list[DD_READ])); |
| 405 | BUG_ON(!list_empty(&dd->fifo_list[DD_WRITE])); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 406 | |
| 407 | kfree(dd); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * initialize elevator private data (deadline_data). |
| 412 | */ |
Bart Van Assche | 3e9a99e | 2021-06-17 17:44:48 -0700 | [diff] [blame] | 413 | static int dd_init_sched(struct request_queue *q, struct elevator_type *e) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 414 | { |
| 415 | struct deadline_data *dd; |
| 416 | struct elevator_queue *eq; |
| 417 | |
| 418 | eq = elevator_alloc(q, e); |
| 419 | if (!eq) |
| 420 | return -ENOMEM; |
| 421 | |
| 422 | dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node); |
| 423 | if (!dd) { |
| 424 | kobject_put(&eq->kobj); |
| 425 | return -ENOMEM; |
| 426 | } |
| 427 | eq->elevator_data = dd; |
| 428 | |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 429 | INIT_LIST_HEAD(&dd->fifo_list[DD_READ]); |
| 430 | INIT_LIST_HEAD(&dd->fifo_list[DD_WRITE]); |
| 431 | dd->sort_list[DD_READ] = RB_ROOT; |
| 432 | dd->sort_list[DD_WRITE] = RB_ROOT; |
| 433 | dd->fifo_expire[DD_READ] = read_expire; |
| 434 | dd->fifo_expire[DD_WRITE] = write_expire; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 435 | dd->writes_starved = writes_starved; |
| 436 | dd->front_merges = 1; |
| 437 | dd->fifo_batch = fifo_batch; |
| 438 | spin_lock_init(&dd->lock); |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 439 | spin_lock_init(&dd->zone_lock); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 440 | INIT_LIST_HEAD(&dd->dispatch); |
| 441 | |
| 442 | q->elevator = eq; |
| 443 | return 0; |
| 444 | } |
| 445 | |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Try to merge @bio into an existing request. If @bio has been merged into |
| 448 | * an existing request, store the pointer to that request into *@rq. |
| 449 | */ |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 450 | static int dd_request_merge(struct request_queue *q, struct request **rq, |
| 451 | struct bio *bio) |
| 452 | { |
| 453 | struct deadline_data *dd = q->elevator->elevator_data; |
| 454 | sector_t sector = bio_end_sector(bio); |
| 455 | struct request *__rq; |
| 456 | |
| 457 | if (!dd->front_merges) |
| 458 | return ELEVATOR_NO_MERGE; |
| 459 | |
| 460 | __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector); |
| 461 | if (__rq) { |
| 462 | BUG_ON(sector != blk_rq_pos(__rq)); |
| 463 | |
| 464 | if (elv_bio_merge_ok(__rq, bio)) { |
| 465 | *rq = __rq; |
| 466 | return ELEVATOR_FRONT_MERGE; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return ELEVATOR_NO_MERGE; |
| 471 | } |
| 472 | |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 473 | /* |
| 474 | * Attempt to merge a bio into an existing request. This function is called |
| 475 | * before @bio is associated with a request. |
| 476 | */ |
Omar Sandoval | efed9a3 | 2021-05-10 17:05:35 -0700 | [diff] [blame] | 477 | static bool dd_bio_merge(struct request_queue *q, struct bio *bio, |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 478 | unsigned int nr_segs) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 479 | { |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 480 | struct deadline_data *dd = q->elevator->elevator_data; |
Jens Axboe | e4d750c | 2017-02-03 09:48:28 -0700 | [diff] [blame] | 481 | struct request *free = NULL; |
| 482 | bool ret; |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 483 | |
| 484 | spin_lock(&dd->lock); |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 485 | ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 486 | spin_unlock(&dd->lock); |
| 487 | |
Jens Axboe | e4d750c | 2017-02-03 09:48:28 -0700 | [diff] [blame] | 488 | if (free) |
| 489 | blk_mq_free_request(free); |
| 490 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * add rq to rbtree and fifo |
| 496 | */ |
| 497 | static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, |
| 498 | bool at_head) |
| 499 | { |
| 500 | struct request_queue *q = hctx->queue; |
| 501 | struct deadline_data *dd = q->elevator->elevator_data; |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 502 | const enum dd_data_dir data_dir = rq_data_dir(rq); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 503 | |
Bart Van Assche | 3bd473f | 2021-06-17 17:44:46 -0700 | [diff] [blame] | 504 | lockdep_assert_held(&dd->lock); |
| 505 | |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 506 | /* |
| 507 | * This may be a requeue of a write request that has locked its |
| 508 | * target zone. If it is the case, this releases the zone lock. |
| 509 | */ |
| 510 | blk_req_zone_write_unlock(rq); |
| 511 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 512 | if (blk_mq_sched_try_insert_merge(q, rq)) |
| 513 | return; |
| 514 | |
Chaitanya Kulkarni | b357e4a | 2021-02-21 21:29:59 -0800 | [diff] [blame] | 515 | trace_block_rq_insert(rq); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 516 | |
Lin Feng | 7687b38 | 2021-04-15 11:43:26 +0800 | [diff] [blame] | 517 | if (at_head) { |
| 518 | list_add(&rq->queuelist, &dd->dispatch); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 519 | } else { |
| 520 | deadline_add_rq_rb(dd, rq); |
| 521 | |
| 522 | if (rq_mergeable(rq)) { |
| 523 | elv_rqhash_add(q, rq); |
| 524 | if (!q->last_merge) |
| 525 | q->last_merge = rq; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * set expire time and add to fifo list |
| 530 | */ |
| 531 | rq->fifo_time = jiffies + dd->fifo_expire[data_dir]; |
| 532 | list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]); |
| 533 | } |
| 534 | } |
| 535 | |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 536 | /* |
| 537 | * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests(). |
| 538 | */ |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 539 | static void dd_insert_requests(struct blk_mq_hw_ctx *hctx, |
| 540 | struct list_head *list, bool at_head) |
| 541 | { |
| 542 | struct request_queue *q = hctx->queue; |
| 543 | struct deadline_data *dd = q->elevator->elevator_data; |
| 544 | |
| 545 | spin_lock(&dd->lock); |
| 546 | while (!list_empty(list)) { |
| 547 | struct request *rq; |
| 548 | |
| 549 | rq = list_first_entry(list, struct request, queuelist); |
| 550 | list_del_init(&rq->queuelist); |
| 551 | dd_insert_request(hctx, rq, at_head); |
| 552 | } |
| 553 | spin_unlock(&dd->lock); |
| 554 | } |
| 555 | |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 556 | /* |
Damien Le Moal | f3bc78d | 2018-02-28 09:35:29 -0800 | [diff] [blame] | 557 | * Nothing to do here. This is defined only to ensure that .finish_request |
| 558 | * method is called upon request completion. |
| 559 | */ |
Christoph Hellwig | 5d9c305 | 2020-05-29 15:53:08 +0200 | [diff] [blame] | 560 | static void dd_prepare_request(struct request *rq) |
Damien Le Moal | f3bc78d | 2018-02-28 09:35:29 -0800 | [diff] [blame] | 561 | { |
| 562 | } |
| 563 | |
| 564 | /* |
Bart Van Assche | 46eae2e | 2021-06-17 17:44:45 -0700 | [diff] [blame] | 565 | * Callback from inside blk_mq_free_request(). |
| 566 | * |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 567 | * For zoned block devices, write unlock the target zone of |
| 568 | * completed write requests. Do this while holding the zone lock |
| 569 | * spinlock so that the zone is never unlocked while deadline_fifo_request() |
Damien Le Moal | f3bc78d | 2018-02-28 09:35:29 -0800 | [diff] [blame] | 570 | * or deadline_next_request() are executing. This function is called for |
| 571 | * all requests, whether or not these requests complete successfully. |
Damien Le Moal | cb8acab | 2019-08-28 13:40:20 +0900 | [diff] [blame] | 572 | * |
| 573 | * For a zoned block device, __dd_dispatch_request() may have stopped |
| 574 | * dispatching requests if all the queued requests are write requests directed |
| 575 | * at zones that are already locked due to on-going write requests. To ensure |
| 576 | * write request dispatch progress in this case, mark the queue as needing a |
| 577 | * restart to ensure that the queue is run again after completion of the |
| 578 | * request and zones being unlocked. |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 579 | */ |
Damien Le Moal | f3bc78d | 2018-02-28 09:35:29 -0800 | [diff] [blame] | 580 | static void dd_finish_request(struct request *rq) |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 581 | { |
| 582 | struct request_queue *q = rq->q; |
| 583 | |
| 584 | if (blk_queue_is_zoned(q)) { |
| 585 | struct deadline_data *dd = q->elevator->elevator_data; |
| 586 | unsigned long flags; |
| 587 | |
| 588 | spin_lock_irqsave(&dd->zone_lock, flags); |
| 589 | blk_req_zone_write_unlock(rq); |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 590 | if (!list_empty(&dd->fifo_list[DD_WRITE])) |
Damien Le Moal | cb8acab | 2019-08-28 13:40:20 +0900 | [diff] [blame] | 591 | blk_mq_sched_mark_restart_hctx(rq->mq_hctx); |
Damien Le Moal | 5700f69 | 2017-12-21 15:43:40 +0900 | [diff] [blame] | 592 | spin_unlock_irqrestore(&dd->zone_lock, flags); |
| 593 | } |
| 594 | } |
| 595 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 596 | static bool dd_has_work(struct blk_mq_hw_ctx *hctx) |
| 597 | { |
| 598 | struct deadline_data *dd = hctx->queue->elevator->elevator_data; |
| 599 | |
| 600 | return !list_empty_careful(&dd->dispatch) || |
| 601 | !list_empty_careful(&dd->fifo_list[0]) || |
| 602 | !list_empty_careful(&dd->fifo_list[1]); |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * sysfs parts below |
| 607 | */ |
| 608 | static ssize_t |
| 609 | deadline_var_show(int var, char *page) |
| 610 | { |
| 611 | return sprintf(page, "%d\n", var); |
| 612 | } |
| 613 | |
weiping zhang | 235f8da | 2017-08-25 01:11:33 +0800 | [diff] [blame] | 614 | static void |
| 615 | deadline_var_store(int *var, const char *page) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 616 | { |
| 617 | char *p = (char *) page; |
| 618 | |
| 619 | *var = simple_strtol(p, &p, 10); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
| 623 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
| 624 | { \ |
| 625 | struct deadline_data *dd = e->elevator_data; \ |
| 626 | int __data = __VAR; \ |
| 627 | if (__CONV) \ |
| 628 | __data = jiffies_to_msecs(__data); \ |
| 629 | return deadline_var_show(__data, (page)); \ |
| 630 | } |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 631 | SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[DD_READ], 1); |
| 632 | SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[DD_WRITE], 1); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 633 | SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0); |
| 634 | SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0); |
| 635 | SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0); |
| 636 | #undef SHOW_FUNCTION |
| 637 | |
| 638 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ |
| 639 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
| 640 | { \ |
| 641 | struct deadline_data *dd = e->elevator_data; \ |
| 642 | int __data; \ |
weiping zhang | 235f8da | 2017-08-25 01:11:33 +0800 | [diff] [blame] | 643 | deadline_var_store(&__data, (page)); \ |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 644 | if (__data < (MIN)) \ |
| 645 | __data = (MIN); \ |
| 646 | else if (__data > (MAX)) \ |
| 647 | __data = (MAX); \ |
| 648 | if (__CONV) \ |
| 649 | *(__PTR) = msecs_to_jiffies(__data); \ |
| 650 | else \ |
| 651 | *(__PTR) = __data; \ |
weiping zhang | 235f8da | 2017-08-25 01:11:33 +0800 | [diff] [blame] | 652 | return count; \ |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 653 | } |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 654 | STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX, 1); |
| 655 | STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX, 1); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 656 | STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0); |
| 657 | STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0); |
| 658 | STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0); |
| 659 | #undef STORE_FUNCTION |
| 660 | |
| 661 | #define DD_ATTR(name) \ |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 662 | __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store) |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 663 | |
| 664 | static struct elv_fs_entry deadline_attrs[] = { |
| 665 | DD_ATTR(read_expire), |
| 666 | DD_ATTR(write_expire), |
| 667 | DD_ATTR(writes_starved), |
| 668 | DD_ATTR(front_merges), |
| 669 | DD_ATTR(fifo_batch), |
| 670 | __ATTR_NULL |
| 671 | }; |
| 672 | |
Omar Sandoval | daaadb3 | 2017-05-04 00:31:34 -0700 | [diff] [blame] | 673 | #ifdef CONFIG_BLK_DEBUG_FS |
| 674 | #define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \ |
| 675 | static void *deadline_##name##_fifo_start(struct seq_file *m, \ |
| 676 | loff_t *pos) \ |
| 677 | __acquires(&dd->lock) \ |
| 678 | { \ |
| 679 | struct request_queue *q = m->private; \ |
| 680 | struct deadline_data *dd = q->elevator->elevator_data; \ |
| 681 | \ |
| 682 | spin_lock(&dd->lock); \ |
| 683 | return seq_list_start(&dd->fifo_list[ddir], *pos); \ |
| 684 | } \ |
| 685 | \ |
| 686 | static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \ |
| 687 | loff_t *pos) \ |
| 688 | { \ |
| 689 | struct request_queue *q = m->private; \ |
| 690 | struct deadline_data *dd = q->elevator->elevator_data; \ |
| 691 | \ |
| 692 | return seq_list_next(v, &dd->fifo_list[ddir], pos); \ |
| 693 | } \ |
| 694 | \ |
| 695 | static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \ |
| 696 | __releases(&dd->lock) \ |
| 697 | { \ |
| 698 | struct request_queue *q = m->private; \ |
| 699 | struct deadline_data *dd = q->elevator->elevator_data; \ |
| 700 | \ |
| 701 | spin_unlock(&dd->lock); \ |
| 702 | } \ |
| 703 | \ |
| 704 | static const struct seq_operations deadline_##name##_fifo_seq_ops = { \ |
| 705 | .start = deadline_##name##_fifo_start, \ |
| 706 | .next = deadline_##name##_fifo_next, \ |
| 707 | .stop = deadline_##name##_fifo_stop, \ |
| 708 | .show = blk_mq_debugfs_rq_show, \ |
| 709 | }; \ |
| 710 | \ |
| 711 | static int deadline_##name##_next_rq_show(void *data, \ |
| 712 | struct seq_file *m) \ |
| 713 | { \ |
| 714 | struct request_queue *q = data; \ |
| 715 | struct deadline_data *dd = q->elevator->elevator_data; \ |
| 716 | struct request *rq = dd->next_rq[ddir]; \ |
| 717 | \ |
| 718 | if (rq) \ |
| 719 | __blk_mq_debugfs_rq_show(m, rq); \ |
| 720 | return 0; \ |
| 721 | } |
Bart Van Assche | 004a26b | 2021-06-17 17:44:49 -0700 | [diff] [blame^] | 722 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_READ, read) |
| 723 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_WRITE, write) |
Omar Sandoval | daaadb3 | 2017-05-04 00:31:34 -0700 | [diff] [blame] | 724 | #undef DEADLINE_DEBUGFS_DDIR_ATTRS |
| 725 | |
| 726 | static int deadline_batching_show(void *data, struct seq_file *m) |
| 727 | { |
| 728 | struct request_queue *q = data; |
| 729 | struct deadline_data *dd = q->elevator->elevator_data; |
| 730 | |
| 731 | seq_printf(m, "%u\n", dd->batching); |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static int deadline_starved_show(void *data, struct seq_file *m) |
| 736 | { |
| 737 | struct request_queue *q = data; |
| 738 | struct deadline_data *dd = q->elevator->elevator_data; |
| 739 | |
| 740 | seq_printf(m, "%u\n", dd->starved); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos) |
| 745 | __acquires(&dd->lock) |
| 746 | { |
| 747 | struct request_queue *q = m->private; |
| 748 | struct deadline_data *dd = q->elevator->elevator_data; |
| 749 | |
| 750 | spin_lock(&dd->lock); |
| 751 | return seq_list_start(&dd->dispatch, *pos); |
| 752 | } |
| 753 | |
| 754 | static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos) |
| 755 | { |
| 756 | struct request_queue *q = m->private; |
| 757 | struct deadline_data *dd = q->elevator->elevator_data; |
| 758 | |
| 759 | return seq_list_next(v, &dd->dispatch, pos); |
| 760 | } |
| 761 | |
| 762 | static void deadline_dispatch_stop(struct seq_file *m, void *v) |
| 763 | __releases(&dd->lock) |
| 764 | { |
| 765 | struct request_queue *q = m->private; |
| 766 | struct deadline_data *dd = q->elevator->elevator_data; |
| 767 | |
| 768 | spin_unlock(&dd->lock); |
| 769 | } |
| 770 | |
| 771 | static const struct seq_operations deadline_dispatch_seq_ops = { |
| 772 | .start = deadline_dispatch_start, |
| 773 | .next = deadline_dispatch_next, |
| 774 | .stop = deadline_dispatch_stop, |
| 775 | .show = blk_mq_debugfs_rq_show, |
| 776 | }; |
| 777 | |
| 778 | #define DEADLINE_QUEUE_DDIR_ATTRS(name) \ |
| 779 | {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \ |
| 780 | {#name "_next_rq", 0400, deadline_##name##_next_rq_show} |
| 781 | static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = { |
| 782 | DEADLINE_QUEUE_DDIR_ATTRS(read), |
| 783 | DEADLINE_QUEUE_DDIR_ATTRS(write), |
| 784 | {"batching", 0400, deadline_batching_show}, |
| 785 | {"starved", 0400, deadline_starved_show}, |
| 786 | {"dispatch", 0400, .seq_ops = &deadline_dispatch_seq_ops}, |
| 787 | {}, |
| 788 | }; |
| 789 | #undef DEADLINE_QUEUE_DDIR_ATTRS |
| 790 | #endif |
| 791 | |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 792 | static struct elevator_type mq_deadline = { |
Jens Axboe | f9cd4bf | 2018-11-01 16:41:41 -0600 | [diff] [blame] | 793 | .ops = { |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 794 | .insert_requests = dd_insert_requests, |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 795 | .dispatch_request = dd_dispatch_request, |
Damien Le Moal | f3bc78d | 2018-02-28 09:35:29 -0800 | [diff] [blame] | 796 | .prepare_request = dd_prepare_request, |
| 797 | .finish_request = dd_finish_request, |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 798 | .next_request = elv_rb_latter_request, |
| 799 | .former_request = elv_rb_former_request, |
| 800 | .bio_merge = dd_bio_merge, |
| 801 | .request_merge = dd_request_merge, |
| 802 | .requests_merged = dd_merged_requests, |
| 803 | .request_merged = dd_request_merged, |
| 804 | .has_work = dd_has_work, |
Bart Van Assche | 3e9a99e | 2021-06-17 17:44:48 -0700 | [diff] [blame] | 805 | .init_sched = dd_init_sched, |
| 806 | .exit_sched = dd_exit_sched, |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 807 | }, |
| 808 | |
Omar Sandoval | daaadb3 | 2017-05-04 00:31:34 -0700 | [diff] [blame] | 809 | #ifdef CONFIG_BLK_DEBUG_FS |
| 810 | .queue_debugfs_attrs = deadline_queue_debugfs_attrs, |
| 811 | #endif |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 812 | .elevator_attrs = deadline_attrs, |
| 813 | .elevator_name = "mq-deadline", |
Jens Axboe | 4d740bc | 2017-10-25 09:47:20 -0600 | [diff] [blame] | 814 | .elevator_alias = "deadline", |
Damien Le Moal | 68c43f1 | 2019-09-05 18:51:31 +0900 | [diff] [blame] | 815 | .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE, |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 816 | .elevator_owner = THIS_MODULE, |
| 817 | }; |
Ben Hutchings | 7de967e | 2017-08-13 18:03:15 +0100 | [diff] [blame] | 818 | MODULE_ALIAS("mq-deadline-iosched"); |
Jens Axboe | 945ffb6 | 2017-01-14 17:11:11 -0700 | [diff] [blame] | 819 | |
| 820 | static int __init deadline_init(void) |
| 821 | { |
| 822 | return elv_register(&mq_deadline); |
| 823 | } |
| 824 | |
| 825 | static void __exit deadline_exit(void) |
| 826 | { |
| 827 | elv_unregister(&mq_deadline); |
| 828 | } |
| 829 | |
| 830 | module_init(deadline_init); |
| 831 | module_exit(deadline_exit); |
| 832 | |
| 833 | MODULE_AUTHOR("Jens Axboe"); |
| 834 | MODULE_LICENSE("GPL"); |
| 835 | MODULE_DESCRIPTION("MQ deadline IO scheduler"); |