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