1 // SPDX-License-Identifier: GPL-2.0 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/bio.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include <linux/init.h> 15 #include <linux/compiler.h> 16 #include <linux/rbtree.h> 17 #include <linux/sbitmap.h> 18 19 #include <trace/events/block.h> 20 21 #include "elevator.h" 22 #include "blk.h" 23 #include "blk-mq.h" 24 #include "blk-mq-debugfs.h" 25 #include "blk-mq-sched.h" 26 27 /* 28 * See Documentation/block/deadline-iosched.rst 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 /* 33 * Time after which to dispatch lower priority requests even if higher 34 * priority requests are pending. 35 */ 36 static const int prio_aging_expire = 10 * HZ; 37 static const int writes_starved = 2; /* max times reads can starve a write */ 38 static const int fifo_batch = 16; /* # of sequential requests treated as one 39 by the above parameters. For throughput. */ 40 41 enum dd_data_dir { 42 DD_READ = READ, 43 DD_WRITE = WRITE, 44 }; 45 46 enum { DD_DIR_COUNT = 2 }; 47 48 enum dd_prio { 49 DD_RT_PRIO = 0, 50 DD_BE_PRIO = 1, 51 DD_IDLE_PRIO = 2, 52 DD_PRIO_MAX = 2, 53 }; 54 55 enum { DD_PRIO_COUNT = 3 }; 56 57 /* 58 * I/O statistics per I/O priority. It is fine if these counters overflow. 59 * What matters is that these counters are at least as wide as 60 * log2(max_outstanding_requests). 61 */ 62 struct io_stats_per_prio { 63 uint32_t inserted; 64 uint32_t merged; 65 uint32_t dispatched; 66 atomic_t completed; 67 }; 68 69 /* 70 * Deadline scheduler data per I/O priority (enum dd_prio). Requests are 71 * present on both sort_list[] and fifo_list[]. 72 */ 73 struct dd_per_prio { 74 struct rb_root sort_list[DD_DIR_COUNT]; 75 struct list_head fifo_list[DD_DIR_COUNT]; 76 /* Position of the most recently dispatched request. */ 77 sector_t latest_pos[DD_DIR_COUNT]; 78 struct io_stats_per_prio stats; 79 }; 80 81 struct deadline_data { 82 /* 83 * run time data 84 */ 85 86 struct list_head dispatch; 87 struct dd_per_prio per_prio[DD_PRIO_COUNT]; 88 89 /* Data direction of latest dispatched request. */ 90 enum dd_data_dir last_dir; 91 unsigned int batching; /* number of sequential requests made */ 92 unsigned int starved; /* times reads have starved writes */ 93 94 /* 95 * settings that change how the i/o scheduler behaves 96 */ 97 int fifo_expire[DD_DIR_COUNT]; 98 int fifo_batch; 99 int writes_starved; 100 int front_merges; 101 int prio_aging_expire; 102 103 spinlock_t lock; 104 }; 105 106 /* Maps an I/O priority class to a deadline scheduler priority. */ 107 static const enum dd_prio ioprio_class_to_prio[] = { 108 [IOPRIO_CLASS_NONE] = DD_BE_PRIO, 109 [IOPRIO_CLASS_RT] = DD_RT_PRIO, 110 [IOPRIO_CLASS_BE] = DD_BE_PRIO, 111 [IOPRIO_CLASS_IDLE] = DD_IDLE_PRIO, 112 }; 113 114 static inline struct rb_root * 115 deadline_rb_root(struct dd_per_prio *per_prio, struct request *rq) 116 { 117 return &per_prio->sort_list[rq_data_dir(rq)]; 118 } 119 120 /* 121 * Returns the I/O priority class (IOPRIO_CLASS_*) that has been assigned to a 122 * request. 123 */ 124 static u8 dd_rq_ioclass(struct request *rq) 125 { 126 return IOPRIO_PRIO_CLASS(req_get_ioprio(rq)); 127 } 128 129 /* 130 * Return the first request for which blk_rq_pos() >= @pos. 131 */ 132 static inline struct request *deadline_from_pos(struct dd_per_prio *per_prio, 133 enum dd_data_dir data_dir, sector_t pos) 134 { 135 struct rb_node *node = per_prio->sort_list[data_dir].rb_node; 136 struct request *rq, *res = NULL; 137 138 while (node) { 139 rq = rb_entry_rq(node); 140 if (blk_rq_pos(rq) >= pos) { 141 res = rq; 142 node = node->rb_left; 143 } else { 144 node = node->rb_right; 145 } 146 } 147 return res; 148 } 149 150 static void 151 deadline_add_rq_rb(struct dd_per_prio *per_prio, struct request *rq) 152 { 153 struct rb_root *root = deadline_rb_root(per_prio, rq); 154 155 elv_rb_add(root, rq); 156 } 157 158 static inline void 159 deadline_del_rq_rb(struct dd_per_prio *per_prio, struct request *rq) 160 { 161 elv_rb_del(deadline_rb_root(per_prio, rq), rq); 162 } 163 164 /* 165 * remove rq from rbtree and fifo. 166 */ 167 static void deadline_remove_request(struct request_queue *q, 168 struct dd_per_prio *per_prio, 169 struct request *rq) 170 { 171 list_del_init(&rq->queuelist); 172 173 /* 174 * We might not be on the rbtree, if we are doing an insert merge 175 */ 176 if (!RB_EMPTY_NODE(&rq->rb_node)) 177 deadline_del_rq_rb(per_prio, rq); 178 179 elv_rqhash_del(q, rq); 180 if (q->last_merge == rq) 181 q->last_merge = NULL; 182 } 183 184 static void dd_request_merged(struct request_queue *q, struct request *req, 185 enum elv_merge type) 186 { 187 struct deadline_data *dd = q->elevator->elevator_data; 188 const u8 ioprio_class = dd_rq_ioclass(req); 189 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; 190 struct dd_per_prio *per_prio = &dd->per_prio[prio]; 191 192 /* 193 * if the merge was a front merge, we need to reposition request 194 */ 195 if (type == ELEVATOR_FRONT_MERGE) { 196 elv_rb_del(deadline_rb_root(per_prio, req), req); 197 deadline_add_rq_rb(per_prio, req); 198 } 199 } 200 201 /* 202 * Callback function that is invoked after @next has been merged into @req. 203 */ 204 static void dd_merged_requests(struct request_queue *q, struct request *req, 205 struct request *next) 206 { 207 struct deadline_data *dd = q->elevator->elevator_data; 208 const u8 ioprio_class = dd_rq_ioclass(next); 209 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; 210 211 lockdep_assert_held(&dd->lock); 212 213 dd->per_prio[prio].stats.merged++; 214 215 /* 216 * if next expires before rq, assign its expire time to rq 217 * and move into next position (next will be deleted) in fifo 218 */ 219 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) { 220 if (time_before((unsigned long)next->fifo_time, 221 (unsigned long)req->fifo_time)) { 222 list_move(&req->queuelist, &next->queuelist); 223 req->fifo_time = next->fifo_time; 224 } 225 } 226 227 /* 228 * kill knowledge of next, this one is a goner 229 */ 230 deadline_remove_request(q, &dd->per_prio[prio], next); 231 } 232 233 /* 234 * move an entry to dispatch queue 235 */ 236 static void deadline_move_request(struct dd_per_prio *per_prio, 237 struct request *rq) 238 { 239 /* 240 * take it off the sort and fifo list 241 */ 242 deadline_remove_request(rq->q, per_prio, rq); 243 } 244 245 /* Number of requests queued for a given priority level. */ 246 static u32 dd_queued(struct deadline_data *dd, enum dd_prio prio) 247 { 248 const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats; 249 250 lockdep_assert_held(&dd->lock); 251 252 return stats->inserted - atomic_read(&stats->completed); 253 } 254 255 /* 256 * deadline_check_fifo returns true if and only if there are expired requests 257 * in the FIFO list. Requires !list_empty(&dd->fifo_list[data_dir]). 258 */ 259 static inline bool deadline_check_fifo(struct dd_per_prio *per_prio, 260 enum dd_data_dir data_dir) 261 { 262 struct request *rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next); 263 264 return time_is_before_eq_jiffies((unsigned long)rq->fifo_time); 265 } 266 267 /* 268 * For the specified data direction, return the next request to 269 * dispatch using arrival ordered lists. 270 */ 271 static struct request *deadline_fifo_request(struct dd_per_prio *per_prio, 272 enum dd_data_dir data_dir) 273 { 274 if (list_empty(&per_prio->fifo_list[data_dir])) 275 return NULL; 276 277 return rq_entry_fifo(per_prio->fifo_list[data_dir].next); 278 } 279 280 /* 281 * For the specified data direction, return the next request to 282 * dispatch using sector position sorted lists. 283 */ 284 static struct request *deadline_next_request(struct dd_per_prio *per_prio, 285 enum dd_data_dir data_dir) 286 { 287 return deadline_from_pos(per_prio, data_dir, 288 per_prio->latest_pos[data_dir]); 289 } 290 291 /* 292 * Returns true if and only if @rq started after @latest_start where 293 * @latest_start is in jiffies. 294 */ 295 static bool started_after(struct deadline_data *dd, struct request *rq, 296 unsigned long latest_start) 297 { 298 unsigned long start_time = (unsigned long)rq->fifo_time; 299 300 start_time -= dd->fifo_expire[rq_data_dir(rq)]; 301 302 return time_after(start_time, latest_start); 303 } 304 305 static struct request *dd_start_request(struct deadline_data *dd, 306 enum dd_data_dir data_dir, 307 struct request *rq) 308 { 309 u8 ioprio_class = dd_rq_ioclass(rq); 310 enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; 311 312 dd->per_prio[prio].latest_pos[data_dir] = blk_rq_pos(rq); 313 dd->per_prio[prio].stats.dispatched++; 314 rq->rq_flags |= RQF_STARTED; 315 return rq; 316 } 317 318 /* 319 * deadline_dispatch_requests selects the best request according to 320 * read/write expire, fifo_batch, etc and with a start time <= @latest_start. 321 */ 322 static struct request *__dd_dispatch_request(struct deadline_data *dd, 323 struct dd_per_prio *per_prio, 324 unsigned long latest_start) 325 { 326 struct request *rq, *next_rq; 327 enum dd_data_dir data_dir; 328 329 lockdep_assert_held(&dd->lock); 330 331 /* 332 * batches are currently reads XOR writes 333 */ 334 rq = deadline_next_request(per_prio, dd->last_dir); 335 if (rq && dd->batching < dd->fifo_batch) { 336 /* we have a next request and are still entitled to batch */ 337 data_dir = rq_data_dir(rq); 338 goto dispatch_request; 339 } 340 341 /* 342 * at this point we are not running a batch. select the appropriate 343 * data direction (read / write) 344 */ 345 346 if (!list_empty(&per_prio->fifo_list[DD_READ])) { 347 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_READ])); 348 349 if (deadline_fifo_request(per_prio, DD_WRITE) && 350 (dd->starved++ >= dd->writes_starved)) 351 goto dispatch_writes; 352 353 data_dir = DD_READ; 354 355 goto dispatch_find_request; 356 } 357 358 /* 359 * there are either no reads or writes have been starved 360 */ 361 362 if (!list_empty(&per_prio->fifo_list[DD_WRITE])) { 363 dispatch_writes: 364 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_WRITE])); 365 366 dd->starved = 0; 367 368 data_dir = DD_WRITE; 369 370 goto dispatch_find_request; 371 } 372 373 return NULL; 374 375 dispatch_find_request: 376 /* 377 * we are not running a batch, find best request for selected data_dir 378 */ 379 next_rq = deadline_next_request(per_prio, data_dir); 380 if (deadline_check_fifo(per_prio, data_dir) || !next_rq) { 381 /* 382 * A deadline has expired, the last request was in the other 383 * direction, or we have run out of higher-sectored requests. 384 * Start again from the request with the earliest expiry time. 385 */ 386 rq = deadline_fifo_request(per_prio, data_dir); 387 } else { 388 /* 389 * The last req was the same dir and we have a next request in 390 * sort order. No expired requests so continue on from here. 391 */ 392 rq = next_rq; 393 } 394 395 if (!rq) 396 return NULL; 397 398 dd->last_dir = data_dir; 399 dd->batching = 0; 400 401 dispatch_request: 402 if (started_after(dd, rq, latest_start)) 403 return NULL; 404 405 /* 406 * rq is the selected appropriate request. 407 */ 408 dd->batching++; 409 deadline_move_request(per_prio, rq); 410 return dd_start_request(dd, data_dir, rq); 411 } 412 413 /* 414 * Check whether there are any requests with priority other than DD_RT_PRIO 415 * that were inserted more than prio_aging_expire jiffies ago. 416 */ 417 static struct request *dd_dispatch_prio_aged_requests(struct deadline_data *dd, 418 unsigned long now) 419 { 420 struct request *rq; 421 enum dd_prio prio; 422 int prio_cnt; 423 424 lockdep_assert_held(&dd->lock); 425 426 prio_cnt = !!dd_queued(dd, DD_RT_PRIO) + !!dd_queued(dd, DD_BE_PRIO) + 427 !!dd_queued(dd, DD_IDLE_PRIO); 428 if (prio_cnt < 2) 429 return NULL; 430 431 for (prio = DD_BE_PRIO; prio <= DD_PRIO_MAX; prio++) { 432 rq = __dd_dispatch_request(dd, &dd->per_prio[prio], 433 now - dd->prio_aging_expire); 434 if (rq) 435 return rq; 436 } 437 438 return NULL; 439 } 440 441 /* 442 * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests(). 443 * 444 * One confusing aspect here is that we get called for a specific 445 * hardware queue, but we may return a request that is for a 446 * different hardware queue. This is because mq-deadline has shared 447 * state for all hardware queues, in terms of sorting, FIFOs, etc. 448 */ 449 static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx) 450 { 451 struct deadline_data *dd = hctx->queue->elevator->elevator_data; 452 const unsigned long now = jiffies; 453 struct request *rq; 454 enum dd_prio prio; 455 456 spin_lock(&dd->lock); 457 458 if (!list_empty(&dd->dispatch)) { 459 rq = list_first_entry(&dd->dispatch, struct request, queuelist); 460 list_del_init(&rq->queuelist); 461 dd_start_request(dd, rq_data_dir(rq), rq); 462 goto unlock; 463 } 464 465 rq = dd_dispatch_prio_aged_requests(dd, now); 466 if (rq) 467 goto unlock; 468 469 /* 470 * Next, dispatch requests in priority order. Ignore lower priority 471 * requests if any higher priority requests are pending. 472 */ 473 for (prio = 0; prio <= DD_PRIO_MAX; prio++) { 474 rq = __dd_dispatch_request(dd, &dd->per_prio[prio], now); 475 if (rq || dd_queued(dd, prio)) 476 break; 477 } 478 479 unlock: 480 spin_unlock(&dd->lock); 481 482 return rq; 483 } 484 485 static void dd_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data) 486 { 487 if (!blk_mq_is_sync_read(opf)) 488 data->shallow_depth = data->q->async_depth; 489 } 490 491 /* Called by blk_mq_init_sched() and blk_mq_update_nr_requests(). */ 492 static void dd_depth_updated(struct request_queue *q) 493 { 494 blk_mq_set_min_shallow_depth(q, q->async_depth); 495 } 496 497 static void dd_exit_sched(struct elevator_queue *e) 498 { 499 struct deadline_data *dd = e->elevator_data; 500 enum dd_prio prio; 501 502 for (prio = 0; prio <= DD_PRIO_MAX; prio++) { 503 struct dd_per_prio *per_prio = &dd->per_prio[prio]; 504 const struct io_stats_per_prio *stats = &per_prio->stats; 505 uint32_t queued; 506 507 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ])); 508 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE])); 509 510 spin_lock(&dd->lock); 511 queued = dd_queued(dd, prio); 512 spin_unlock(&dd->lock); 513 514 WARN_ONCE(queued != 0, 515 "statistics for priority %d: i %u m %u d %u c %u\n", 516 prio, stats->inserted, stats->merged, 517 stats->dispatched, atomic_read(&stats->completed)); 518 } 519 520 kfree(dd); 521 } 522 523 /* 524 * initialize elevator private data (deadline_data). 525 */ 526 static int dd_init_sched(struct request_queue *q, struct elevator_queue *eq) 527 { 528 struct deadline_data *dd; 529 enum dd_prio prio; 530 531 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node); 532 if (!dd) 533 return -ENOMEM; 534 535 eq->elevator_data = dd; 536 537 INIT_LIST_HEAD(&dd->dispatch); 538 for (prio = 0; prio <= DD_PRIO_MAX; prio++) { 539 struct dd_per_prio *per_prio = &dd->per_prio[prio]; 540 541 INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]); 542 INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]); 543 per_prio->sort_list[DD_READ] = RB_ROOT; 544 per_prio->sort_list[DD_WRITE] = RB_ROOT; 545 } 546 dd->fifo_expire[DD_READ] = read_expire; 547 dd->fifo_expire[DD_WRITE] = write_expire; 548 dd->writes_starved = writes_starved; 549 dd->front_merges = 1; 550 dd->last_dir = DD_WRITE; 551 dd->fifo_batch = fifo_batch; 552 dd->prio_aging_expire = prio_aging_expire; 553 spin_lock_init(&dd->lock); 554 555 /* We dispatch from request queue wide instead of hw queue */ 556 blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q); 557 558 q->elevator = eq; 559 q->async_depth = q->nr_requests; 560 dd_depth_updated(q); 561 return 0; 562 } 563 564 /* 565 * Try to merge @bio into an existing request. If @bio has been merged into 566 * an existing request, store the pointer to that request into *@rq. 567 */ 568 static int dd_request_merge(struct request_queue *q, struct request **rq, 569 struct bio *bio) 570 { 571 struct deadline_data *dd = q->elevator->elevator_data; 572 const u8 ioprio_class = IOPRIO_PRIO_CLASS(bio->bi_ioprio); 573 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; 574 struct dd_per_prio *per_prio = &dd->per_prio[prio]; 575 sector_t sector = bio_end_sector(bio); 576 struct request *__rq; 577 578 if (!dd->front_merges) 579 return ELEVATOR_NO_MERGE; 580 581 __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector); 582 if (__rq) { 583 BUG_ON(sector != blk_rq_pos(__rq)); 584 585 if (elv_bio_merge_ok(__rq, bio)) { 586 *rq = __rq; 587 if (blk_discard_mergable(__rq)) 588 return ELEVATOR_DISCARD_MERGE; 589 return ELEVATOR_FRONT_MERGE; 590 } 591 } 592 593 return ELEVATOR_NO_MERGE; 594 } 595 596 /* 597 * Attempt to merge a bio into an existing request. This function is called 598 * before @bio is associated with a request. 599 */ 600 static bool dd_bio_merge(struct request_queue *q, struct bio *bio, 601 unsigned int nr_segs) 602 { 603 struct deadline_data *dd = q->elevator->elevator_data; 604 struct request *free = NULL; 605 bool ret; 606 607 spin_lock(&dd->lock); 608 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free); 609 spin_unlock(&dd->lock); 610 611 if (free) 612 blk_mq_free_request(free); 613 614 return ret; 615 } 616 617 /* 618 * add rq to rbtree and fifo 619 */ 620 static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, 621 blk_insert_t flags, struct list_head *free) 622 { 623 struct request_queue *q = hctx->queue; 624 struct deadline_data *dd = q->elevator->elevator_data; 625 const enum dd_data_dir data_dir = rq_data_dir(rq); 626 u16 ioprio = req_get_ioprio(rq); 627 u8 ioprio_class = IOPRIO_PRIO_CLASS(ioprio); 628 struct dd_per_prio *per_prio; 629 enum dd_prio prio; 630 631 lockdep_assert_held(&dd->lock); 632 633 prio = ioprio_class_to_prio[ioprio_class]; 634 per_prio = &dd->per_prio[prio]; 635 if (!rq->elv.priv[0]) 636 per_prio->stats.inserted++; 637 rq->elv.priv[0] = per_prio; 638 639 if (blk_mq_sched_try_insert_merge(q, rq, free)) 640 return; 641 642 trace_block_rq_insert(rq); 643 644 if (flags & BLK_MQ_INSERT_AT_HEAD) { 645 list_add(&rq->queuelist, &dd->dispatch); 646 rq->fifo_time = jiffies; 647 } else { 648 deadline_add_rq_rb(per_prio, rq); 649 650 if (rq_mergeable(rq)) { 651 elv_rqhash_add(q, rq); 652 if (!q->last_merge) 653 q->last_merge = rq; 654 } 655 656 /* 657 * set expire time and add to fifo list 658 */ 659 rq->fifo_time = jiffies + dd->fifo_expire[data_dir]; 660 list_add_tail(&rq->queuelist, &per_prio->fifo_list[data_dir]); 661 } 662 } 663 664 /* 665 * Called from blk_mq_insert_request() or blk_mq_dispatch_list(). 666 */ 667 static void dd_insert_requests(struct blk_mq_hw_ctx *hctx, 668 struct list_head *list, 669 blk_insert_t flags) 670 { 671 struct request_queue *q = hctx->queue; 672 struct deadline_data *dd = q->elevator->elevator_data; 673 LIST_HEAD(free); 674 675 spin_lock(&dd->lock); 676 while (!list_empty(list)) { 677 struct request *rq; 678 679 rq = list_first_entry(list, struct request, queuelist); 680 list_del_init(&rq->queuelist); 681 dd_insert_request(hctx, rq, flags, &free); 682 } 683 spin_unlock(&dd->lock); 684 685 blk_mq_free_requests(&free); 686 } 687 688 /* Callback from inside blk_mq_rq_ctx_init(). */ 689 static void dd_prepare_request(struct request *rq) 690 { 691 rq->elv.priv[0] = NULL; 692 } 693 694 /* 695 * Callback from inside blk_mq_free_request(). 696 */ 697 static void dd_finish_request(struct request *rq) 698 { 699 struct dd_per_prio *per_prio = rq->elv.priv[0]; 700 701 /* 702 * The block layer core may call dd_finish_request() without having 703 * called dd_insert_requests(). Skip requests that bypassed I/O 704 * scheduling. See also blk_mq_request_bypass_insert(). 705 */ 706 if (per_prio) 707 atomic_inc(&per_prio->stats.completed); 708 } 709 710 static bool dd_has_work_for_prio(struct dd_per_prio *per_prio) 711 { 712 return !list_empty_careful(&per_prio->fifo_list[DD_READ]) || 713 !list_empty_careful(&per_prio->fifo_list[DD_WRITE]); 714 } 715 716 static bool dd_has_work(struct blk_mq_hw_ctx *hctx) 717 { 718 struct deadline_data *dd = hctx->queue->elevator->elevator_data; 719 enum dd_prio prio; 720 721 if (!list_empty_careful(&dd->dispatch)) 722 return true; 723 724 for (prio = 0; prio <= DD_PRIO_MAX; prio++) 725 if (dd_has_work_for_prio(&dd->per_prio[prio])) 726 return true; 727 728 return false; 729 } 730 731 /* 732 * sysfs parts below 733 */ 734 #define SHOW_INT(__FUNC, __VAR) \ 735 static ssize_t __FUNC(struct elevator_queue *e, char *page) \ 736 { \ 737 struct deadline_data *dd = e->elevator_data; \ 738 \ 739 return sysfs_emit(page, "%d\n", __VAR); \ 740 } 741 #define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR)) 742 SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]); 743 SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]); 744 SHOW_JIFFIES(deadline_prio_aging_expire_show, dd->prio_aging_expire); 745 SHOW_INT(deadline_writes_starved_show, dd->writes_starved); 746 SHOW_INT(deadline_front_merges_show, dd->front_merges); 747 SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch); 748 #undef SHOW_INT 749 #undef SHOW_JIFFIES 750 751 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ 752 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ 753 { \ 754 struct deadline_data *dd = e->elevator_data; \ 755 int __data, __ret; \ 756 \ 757 __ret = kstrtoint(page, 0, &__data); \ 758 if (__ret < 0) \ 759 return __ret; \ 760 if (__data < (MIN)) \ 761 __data = (MIN); \ 762 else if (__data > (MAX)) \ 763 __data = (MAX); \ 764 *(__PTR) = __CONV(__data); \ 765 return count; \ 766 } 767 #define STORE_INT(__FUNC, __PTR, MIN, MAX) \ 768 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, ) 769 #define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \ 770 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies) 771 STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX); 772 STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX); 773 STORE_JIFFIES(deadline_prio_aging_expire_store, &dd->prio_aging_expire, 0, INT_MAX); 774 STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX); 775 STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1); 776 STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX); 777 #undef STORE_FUNCTION 778 #undef STORE_INT 779 #undef STORE_JIFFIES 780 781 #define DD_ATTR(name) \ 782 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store) 783 784 static const struct elv_fs_entry deadline_attrs[] = { 785 DD_ATTR(read_expire), 786 DD_ATTR(write_expire), 787 DD_ATTR(writes_starved), 788 DD_ATTR(front_merges), 789 DD_ATTR(fifo_batch), 790 DD_ATTR(prio_aging_expire), 791 __ATTR_NULL 792 }; 793 794 #define RQ_FROM_SEQ_FILE(m) ((struct request_queue *)(m)->private) 795 #define DD_DATA_FROM_RQ(rq) \ 796 ((struct deadline_data *)(rq)->elevator->elevator_data) 797 798 #ifdef CONFIG_BLK_DEBUG_FS 799 #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \ 800 static void *deadline_##name##_fifo_start(struct seq_file *m, \ 801 loff_t *pos) \ 802 __acquires(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) \ 803 { \ 804 struct request_queue *q = m->private; \ 805 struct deadline_data *dd = q->elevator->elevator_data; \ 806 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ 807 \ 808 spin_lock(&dd->lock); \ 809 return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \ 810 } \ 811 \ 812 static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \ 813 loff_t *pos) \ 814 { \ 815 struct request_queue *q = m->private; \ 816 struct deadline_data *dd = q->elevator->elevator_data; \ 817 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ 818 \ 819 return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \ 820 } \ 821 \ 822 static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \ 823 __releases(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) \ 824 { \ 825 struct request_queue *q = m->private; \ 826 struct deadline_data *dd = q->elevator->elevator_data; \ 827 \ 828 spin_unlock(&dd->lock); \ 829 } \ 830 \ 831 static const struct seq_operations deadline_##name##_fifo_seq_ops = { \ 832 .start = deadline_##name##_fifo_start, \ 833 .next = deadline_##name##_fifo_next, \ 834 .stop = deadline_##name##_fifo_stop, \ 835 .show = blk_mq_debugfs_rq_show, \ 836 }; \ 837 \ 838 static int deadline_##name##_next_rq_show(void *data, \ 839 struct seq_file *m) \ 840 { \ 841 struct request_queue *q = data; \ 842 struct deadline_data *dd = q->elevator->elevator_data; \ 843 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ 844 struct request *rq; \ 845 \ 846 rq = deadline_from_pos(per_prio, data_dir, \ 847 per_prio->latest_pos[data_dir]); \ 848 if (rq) \ 849 __blk_mq_debugfs_rq_show(m, rq); \ 850 return 0; \ 851 } 852 853 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0); 854 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0); 855 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1); 856 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1); 857 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2); 858 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2); 859 #undef DEADLINE_DEBUGFS_DDIR_ATTRS 860 861 static int deadline_batching_show(void *data, struct seq_file *m) 862 { 863 struct request_queue *q = data; 864 struct deadline_data *dd = q->elevator->elevator_data; 865 866 seq_printf(m, "%u\n", dd->batching); 867 return 0; 868 } 869 870 static int deadline_starved_show(void *data, struct seq_file *m) 871 { 872 struct request_queue *q = data; 873 struct deadline_data *dd = q->elevator->elevator_data; 874 875 seq_printf(m, "%u\n", dd->starved); 876 return 0; 877 } 878 879 static int dd_queued_show(void *data, struct seq_file *m) 880 { 881 struct request_queue *q = data; 882 struct deadline_data *dd = q->elevator->elevator_data; 883 u32 rt, be, idle; 884 885 spin_lock(&dd->lock); 886 rt = dd_queued(dd, DD_RT_PRIO); 887 be = dd_queued(dd, DD_BE_PRIO); 888 idle = dd_queued(dd, DD_IDLE_PRIO); 889 spin_unlock(&dd->lock); 890 891 seq_printf(m, "%u %u %u\n", rt, be, idle); 892 893 return 0; 894 } 895 896 /* Number of requests owned by the block driver for a given priority. */ 897 static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio) 898 { 899 const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats; 900 901 lockdep_assert_held(&dd->lock); 902 903 return stats->dispatched + stats->merged - 904 atomic_read(&stats->completed); 905 } 906 907 static int dd_owned_by_driver_show(void *data, struct seq_file *m) 908 { 909 struct request_queue *q = data; 910 struct deadline_data *dd = q->elevator->elevator_data; 911 u32 rt, be, idle; 912 913 spin_lock(&dd->lock); 914 rt = dd_owned_by_driver(dd, DD_RT_PRIO); 915 be = dd_owned_by_driver(dd, DD_BE_PRIO); 916 idle = dd_owned_by_driver(dd, DD_IDLE_PRIO); 917 spin_unlock(&dd->lock); 918 919 seq_printf(m, "%u %u %u\n", rt, be, idle); 920 921 return 0; 922 } 923 924 static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos) 925 __acquires(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) 926 { 927 struct request_queue *q = m->private; 928 struct deadline_data *dd = q->elevator->elevator_data; 929 930 spin_lock(&dd->lock); 931 return seq_list_start(&dd->dispatch, *pos); 932 } 933 934 static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos) 935 { 936 struct request_queue *q = m->private; 937 struct deadline_data *dd = q->elevator->elevator_data; 938 939 return seq_list_next(v, &dd->dispatch, pos); 940 } 941 942 static void deadline_dispatch_stop(struct seq_file *m, void *v) 943 __releases(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) 944 { 945 struct request_queue *q = m->private; 946 struct deadline_data *dd = q->elevator->elevator_data; 947 948 spin_unlock(&dd->lock); 949 } 950 951 static const struct seq_operations deadline_dispatch_seq_ops = { 952 .start = deadline_dispatch_start, 953 .next = deadline_dispatch_next, 954 .stop = deadline_dispatch_stop, 955 .show = blk_mq_debugfs_rq_show, 956 }; 957 958 #define DEADLINE_QUEUE_DDIR_ATTRS(name) \ 959 {#name "_fifo_list", 0400, \ 960 .seq_ops = &deadline_##name##_fifo_seq_ops} 961 #define DEADLINE_NEXT_RQ_ATTR(name) \ 962 {#name "_next_rq", 0400, deadline_##name##_next_rq_show} 963 static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = { 964 DEADLINE_QUEUE_DDIR_ATTRS(read0), 965 DEADLINE_QUEUE_DDIR_ATTRS(write0), 966 DEADLINE_QUEUE_DDIR_ATTRS(read1), 967 DEADLINE_QUEUE_DDIR_ATTRS(write1), 968 DEADLINE_QUEUE_DDIR_ATTRS(read2), 969 DEADLINE_QUEUE_DDIR_ATTRS(write2), 970 DEADLINE_NEXT_RQ_ATTR(read0), 971 DEADLINE_NEXT_RQ_ATTR(write0), 972 DEADLINE_NEXT_RQ_ATTR(read1), 973 DEADLINE_NEXT_RQ_ATTR(write1), 974 DEADLINE_NEXT_RQ_ATTR(read2), 975 DEADLINE_NEXT_RQ_ATTR(write2), 976 {"batching", 0400, deadline_batching_show}, 977 {"starved", 0400, deadline_starved_show}, 978 {"dispatch", 0400, .seq_ops = &deadline_dispatch_seq_ops}, 979 {"owned_by_driver", 0400, dd_owned_by_driver_show}, 980 {"queued", 0400, dd_queued_show}, 981 {}, 982 }; 983 #undef DEADLINE_QUEUE_DDIR_ATTRS 984 #endif 985 986 static struct elevator_type mq_deadline = { 987 .ops = { 988 .depth_updated = dd_depth_updated, 989 .limit_depth = dd_limit_depth, 990 .insert_requests = dd_insert_requests, 991 .dispatch_request = dd_dispatch_request, 992 .prepare_request = dd_prepare_request, 993 .finish_request = dd_finish_request, 994 .next_request = elv_rb_latter_request, 995 .former_request = elv_rb_former_request, 996 .bio_merge = dd_bio_merge, 997 .request_merge = dd_request_merge, 998 .requests_merged = dd_merged_requests, 999 .request_merged = dd_request_merged, 1000 .has_work = dd_has_work, 1001 .init_sched = dd_init_sched, 1002 .exit_sched = dd_exit_sched, 1003 }, 1004 1005 #ifdef CONFIG_BLK_DEBUG_FS 1006 .queue_debugfs_attrs = deadline_queue_debugfs_attrs, 1007 #endif 1008 .elevator_attrs = deadline_attrs, 1009 .elevator_name = "mq-deadline", 1010 .elevator_alias = "deadline", 1011 .elevator_owner = THIS_MODULE, 1012 }; 1013 MODULE_ALIAS("mq-deadline-iosched"); 1014 1015 static int __init deadline_init(void) 1016 { 1017 return elv_register(&mq_deadline); 1018 } 1019 1020 static void __exit deadline_exit(void) 1021 { 1022 elv_unregister(&mq_deadline); 1023 } 1024 1025 module_init(deadline_init); 1026 module_exit(deadline_exit); 1027 1028 MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche"); 1029 MODULE_LICENSE("GPL"); 1030 MODULE_DESCRIPTION("MQ deadline IO scheduler"); 1031