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