Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.11-rc2 4940 lines 130 kB view raw
1/* 2 * CFQ, or complete fairness queueing, disk scheduler. 3 * 4 * Based on ideas from a previously unfinished io 5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli. 6 * 7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> 8 */ 9#include <linux/module.h> 10#include <linux/slab.h> 11#include <linux/sched/clock.h> 12#include <linux/blkdev.h> 13#include <linux/elevator.h> 14#include <linux/ktime.h> 15#include <linux/rbtree.h> 16#include <linux/ioprio.h> 17#include <linux/blktrace_api.h> 18#include <linux/blk-cgroup.h> 19#include "blk.h" 20#include "blk-wbt.h" 21 22/* 23 * tunables 24 */ 25/* max queue in one round of service */ 26static const int cfq_quantum = 8; 27static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 }; 28/* maximum backwards seek, in KiB */ 29static const int cfq_back_max = 16 * 1024; 30/* penalty of a backwards seek */ 31static const int cfq_back_penalty = 2; 32static const u64 cfq_slice_sync = NSEC_PER_SEC / 10; 33static u64 cfq_slice_async = NSEC_PER_SEC / 25; 34static const int cfq_slice_async_rq = 2; 35static u64 cfq_slice_idle = NSEC_PER_SEC / 125; 36static u64 cfq_group_idle = NSEC_PER_SEC / 125; 37static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */ 38static const int cfq_hist_divisor = 4; 39 40/* 41 * offset from end of service tree 42 */ 43#define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5) 44 45/* 46 * below this threshold, we consider thinktime immediate 47 */ 48#define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ) 49 50#define CFQ_SLICE_SCALE (5) 51#define CFQ_HW_QUEUE_MIN (5) 52#define CFQ_SERVICE_SHIFT 12 53 54#define CFQQ_SEEK_THR (sector_t)(8 * 100) 55#define CFQQ_CLOSE_THR (sector_t)(8 * 1024) 56#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32) 57#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8) 58 59#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq) 60#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0]) 61#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1]) 62 63static struct kmem_cache *cfq_pool; 64 65#define CFQ_PRIO_LISTS IOPRIO_BE_NR 66#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) 67#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) 68 69#define sample_valid(samples) ((samples) > 80) 70#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node) 71 72/* blkio-related constants */ 73#define CFQ_WEIGHT_LEGACY_MIN 10 74#define CFQ_WEIGHT_LEGACY_DFL 500 75#define CFQ_WEIGHT_LEGACY_MAX 1000 76 77struct cfq_ttime { 78 u64 last_end_request; 79 80 u64 ttime_total; 81 u64 ttime_mean; 82 unsigned long ttime_samples; 83}; 84 85/* 86 * Most of our rbtree usage is for sorting with min extraction, so 87 * if we cache the leftmost node we don't have to walk down the tree 88 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should 89 * move this into the elevator for the rq sorting as well. 90 */ 91struct cfq_rb_root { 92 struct rb_root rb; 93 struct rb_node *left; 94 unsigned count; 95 u64 min_vdisktime; 96 struct cfq_ttime ttime; 97}; 98#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \ 99 .ttime = {.last_end_request = ktime_get_ns(),},} 100 101/* 102 * Per process-grouping structure 103 */ 104struct cfq_queue { 105 /* reference count */ 106 int ref; 107 /* various state flags, see below */ 108 unsigned int flags; 109 /* parent cfq_data */ 110 struct cfq_data *cfqd; 111 /* service_tree member */ 112 struct rb_node rb_node; 113 /* service_tree key */ 114 u64 rb_key; 115 /* prio tree member */ 116 struct rb_node p_node; 117 /* prio tree root we belong to, if any */ 118 struct rb_root *p_root; 119 /* sorted list of pending requests */ 120 struct rb_root sort_list; 121 /* if fifo isn't expired, next request to serve */ 122 struct request *next_rq; 123 /* requests queued in sort_list */ 124 int queued[2]; 125 /* currently allocated requests */ 126 int allocated[2]; 127 /* fifo list of requests in sort_list */ 128 struct list_head fifo; 129 130 /* time when queue got scheduled in to dispatch first request. */ 131 u64 dispatch_start; 132 u64 allocated_slice; 133 u64 slice_dispatch; 134 /* time when first request from queue completed and slice started. */ 135 u64 slice_start; 136 u64 slice_end; 137 s64 slice_resid; 138 139 /* pending priority requests */ 140 int prio_pending; 141 /* number of requests that are on the dispatch list or inside driver */ 142 int dispatched; 143 144 /* io prio of this group */ 145 unsigned short ioprio, org_ioprio; 146 unsigned short ioprio_class, org_ioprio_class; 147 148 pid_t pid; 149 150 u32 seek_history; 151 sector_t last_request_pos; 152 153 struct cfq_rb_root *service_tree; 154 struct cfq_queue *new_cfqq; 155 struct cfq_group *cfqg; 156 /* Number of sectors dispatched from queue in single dispatch round */ 157 unsigned long nr_sectors; 158}; 159 160/* 161 * First index in the service_trees. 162 * IDLE is handled separately, so it has negative index 163 */ 164enum wl_class_t { 165 BE_WORKLOAD = 0, 166 RT_WORKLOAD = 1, 167 IDLE_WORKLOAD = 2, 168 CFQ_PRIO_NR, 169}; 170 171/* 172 * Second index in the service_trees. 173 */ 174enum wl_type_t { 175 ASYNC_WORKLOAD = 0, 176 SYNC_NOIDLE_WORKLOAD = 1, 177 SYNC_WORKLOAD = 2 178}; 179 180struct cfqg_stats { 181#ifdef CONFIG_CFQ_GROUP_IOSCHED 182 /* number of ios merged */ 183 struct blkg_rwstat merged; 184 /* total time spent on device in ns, may not be accurate w/ queueing */ 185 struct blkg_rwstat service_time; 186 /* total time spent waiting in scheduler queue in ns */ 187 struct blkg_rwstat wait_time; 188 /* number of IOs queued up */ 189 struct blkg_rwstat queued; 190 /* total disk time and nr sectors dispatched by this group */ 191 struct blkg_stat time; 192#ifdef CONFIG_DEBUG_BLK_CGROUP 193 /* time not charged to this cgroup */ 194 struct blkg_stat unaccounted_time; 195 /* sum of number of ios queued across all samples */ 196 struct blkg_stat avg_queue_size_sum; 197 /* count of samples taken for average */ 198 struct blkg_stat avg_queue_size_samples; 199 /* how many times this group has been removed from service tree */ 200 struct blkg_stat dequeue; 201 /* total time spent waiting for it to be assigned a timeslice. */ 202 struct blkg_stat group_wait_time; 203 /* time spent idling for this blkcg_gq */ 204 struct blkg_stat idle_time; 205 /* total time with empty current active q with other requests queued */ 206 struct blkg_stat empty_time; 207 /* fields after this shouldn't be cleared on stat reset */ 208 uint64_t start_group_wait_time; 209 uint64_t start_idle_time; 210 uint64_t start_empty_time; 211 uint16_t flags; 212#endif /* CONFIG_DEBUG_BLK_CGROUP */ 213#endif /* CONFIG_CFQ_GROUP_IOSCHED */ 214}; 215 216/* Per-cgroup data */ 217struct cfq_group_data { 218 /* must be the first member */ 219 struct blkcg_policy_data cpd; 220 221 unsigned int weight; 222 unsigned int leaf_weight; 223}; 224 225/* This is per cgroup per device grouping structure */ 226struct cfq_group { 227 /* must be the first member */ 228 struct blkg_policy_data pd; 229 230 /* group service_tree member */ 231 struct rb_node rb_node; 232 233 /* group service_tree key */ 234 u64 vdisktime; 235 236 /* 237 * The number of active cfqgs and sum of their weights under this 238 * cfqg. This covers this cfqg's leaf_weight and all children's 239 * weights, but does not cover weights of further descendants. 240 * 241 * If a cfqg is on the service tree, it's active. An active cfqg 242 * also activates its parent and contributes to the children_weight 243 * of the parent. 244 */ 245 int nr_active; 246 unsigned int children_weight; 247 248 /* 249 * vfraction is the fraction of vdisktime that the tasks in this 250 * cfqg are entitled to. This is determined by compounding the 251 * ratios walking up from this cfqg to the root. 252 * 253 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all 254 * vfractions on a service tree is approximately 1. The sum may 255 * deviate a bit due to rounding errors and fluctuations caused by 256 * cfqgs entering and leaving the service tree. 257 */ 258 unsigned int vfraction; 259 260 /* 261 * There are two weights - (internal) weight is the weight of this 262 * cfqg against the sibling cfqgs. leaf_weight is the wight of 263 * this cfqg against the child cfqgs. For the root cfqg, both 264 * weights are kept in sync for backward compatibility. 265 */ 266 unsigned int weight; 267 unsigned int new_weight; 268 unsigned int dev_weight; 269 270 unsigned int leaf_weight; 271 unsigned int new_leaf_weight; 272 unsigned int dev_leaf_weight; 273 274 /* number of cfqq currently on this group */ 275 int nr_cfqq; 276 277 /* 278 * Per group busy queues average. Useful for workload slice calc. We 279 * create the array for each prio class but at run time it is used 280 * only for RT and BE class and slot for IDLE class remains unused. 281 * This is primarily done to avoid confusion and a gcc warning. 282 */ 283 unsigned int busy_queues_avg[CFQ_PRIO_NR]; 284 /* 285 * rr lists of queues with requests. We maintain service trees for 286 * RT and BE classes. These trees are subdivided in subclasses 287 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE 288 * class there is no subclassification and all the cfq queues go on 289 * a single tree service_tree_idle. 290 * Counts are embedded in the cfq_rb_root 291 */ 292 struct cfq_rb_root service_trees[2][3]; 293 struct cfq_rb_root service_tree_idle; 294 295 u64 saved_wl_slice; 296 enum wl_type_t saved_wl_type; 297 enum wl_class_t saved_wl_class; 298 299 /* number of requests that are on the dispatch list or inside driver */ 300 int dispatched; 301 struct cfq_ttime ttime; 302 struct cfqg_stats stats; /* stats for this cfqg */ 303 304 /* async queue for each priority case */ 305 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR]; 306 struct cfq_queue *async_idle_cfqq; 307 308}; 309 310struct cfq_io_cq { 311 struct io_cq icq; /* must be the first member */ 312 struct cfq_queue *cfqq[2]; 313 struct cfq_ttime ttime; 314 int ioprio; /* the current ioprio */ 315#ifdef CONFIG_CFQ_GROUP_IOSCHED 316 uint64_t blkcg_serial_nr; /* the current blkcg serial */ 317#endif 318}; 319 320/* 321 * Per block device queue structure 322 */ 323struct cfq_data { 324 struct request_queue *queue; 325 /* Root service tree for cfq_groups */ 326 struct cfq_rb_root grp_service_tree; 327 struct cfq_group *root_group; 328 329 /* 330 * The priority currently being served 331 */ 332 enum wl_class_t serving_wl_class; 333 enum wl_type_t serving_wl_type; 334 u64 workload_expires; 335 struct cfq_group *serving_group; 336 337 /* 338 * Each priority tree is sorted by next_request position. These 339 * trees are used when determining if two or more queues are 340 * interleaving requests (see cfq_close_cooperator). 341 */ 342 struct rb_root prio_trees[CFQ_PRIO_LISTS]; 343 344 unsigned int busy_queues; 345 unsigned int busy_sync_queues; 346 347 int rq_in_driver; 348 int rq_in_flight[2]; 349 350 /* 351 * queue-depth detection 352 */ 353 int rq_queued; 354 int hw_tag; 355 /* 356 * hw_tag can be 357 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection) 358 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth) 359 * 0 => no NCQ 360 */ 361 int hw_tag_est_depth; 362 unsigned int hw_tag_samples; 363 364 /* 365 * idle window management 366 */ 367 struct hrtimer idle_slice_timer; 368 struct work_struct unplug_work; 369 370 struct cfq_queue *active_queue; 371 struct cfq_io_cq *active_cic; 372 373 sector_t last_position; 374 375 /* 376 * tunables, see top of file 377 */ 378 unsigned int cfq_quantum; 379 unsigned int cfq_back_penalty; 380 unsigned int cfq_back_max; 381 unsigned int cfq_slice_async_rq; 382 unsigned int cfq_latency; 383 u64 cfq_fifo_expire[2]; 384 u64 cfq_slice[2]; 385 u64 cfq_slice_idle; 386 u64 cfq_group_idle; 387 u64 cfq_target_latency; 388 389 /* 390 * Fallback dummy cfqq for extreme OOM conditions 391 */ 392 struct cfq_queue oom_cfqq; 393 394 u64 last_delayed_sync; 395}; 396 397static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd); 398static void cfq_put_queue(struct cfq_queue *cfqq); 399 400static struct cfq_rb_root *st_for(struct cfq_group *cfqg, 401 enum wl_class_t class, 402 enum wl_type_t type) 403{ 404 if (!cfqg) 405 return NULL; 406 407 if (class == IDLE_WORKLOAD) 408 return &cfqg->service_tree_idle; 409 410 return &cfqg->service_trees[class][type]; 411} 412 413enum cfqq_state_flags { 414 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ 415 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ 416 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */ 417 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ 418 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ 419 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ 420 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ 421 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ 422 CFQ_CFQQ_FLAG_sync, /* synchronous queue */ 423 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */ 424 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */ 425 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */ 426 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */ 427}; 428 429#define CFQ_CFQQ_FNS(name) \ 430static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ 431{ \ 432 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ 433} \ 434static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ 435{ \ 436 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ 437} \ 438static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ 439{ \ 440 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ 441} 442 443CFQ_CFQQ_FNS(on_rr); 444CFQ_CFQQ_FNS(wait_request); 445CFQ_CFQQ_FNS(must_dispatch); 446CFQ_CFQQ_FNS(must_alloc_slice); 447CFQ_CFQQ_FNS(fifo_expire); 448CFQ_CFQQ_FNS(idle_window); 449CFQ_CFQQ_FNS(prio_changed); 450CFQ_CFQQ_FNS(slice_new); 451CFQ_CFQQ_FNS(sync); 452CFQ_CFQQ_FNS(coop); 453CFQ_CFQQ_FNS(split_coop); 454CFQ_CFQQ_FNS(deep); 455CFQ_CFQQ_FNS(wait_busy); 456#undef CFQ_CFQQ_FNS 457 458#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP) 459 460/* cfqg stats flags */ 461enum cfqg_stats_flags { 462 CFQG_stats_waiting = 0, 463 CFQG_stats_idling, 464 CFQG_stats_empty, 465}; 466 467#define CFQG_FLAG_FNS(name) \ 468static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \ 469{ \ 470 stats->flags |= (1 << CFQG_stats_##name); \ 471} \ 472static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \ 473{ \ 474 stats->flags &= ~(1 << CFQG_stats_##name); \ 475} \ 476static inline int cfqg_stats_##name(struct cfqg_stats *stats) \ 477{ \ 478 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \ 479} \ 480 481CFQG_FLAG_FNS(waiting) 482CFQG_FLAG_FNS(idling) 483CFQG_FLAG_FNS(empty) 484#undef CFQG_FLAG_FNS 485 486/* This should be called with the queue_lock held. */ 487static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats) 488{ 489 unsigned long long now; 490 491 if (!cfqg_stats_waiting(stats)) 492 return; 493 494 now = sched_clock(); 495 if (time_after64(now, stats->start_group_wait_time)) 496 blkg_stat_add(&stats->group_wait_time, 497 now - stats->start_group_wait_time); 498 cfqg_stats_clear_waiting(stats); 499} 500 501/* This should be called with the queue_lock held. */ 502static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, 503 struct cfq_group *curr_cfqg) 504{ 505 struct cfqg_stats *stats = &cfqg->stats; 506 507 if (cfqg_stats_waiting(stats)) 508 return; 509 if (cfqg == curr_cfqg) 510 return; 511 stats->start_group_wait_time = sched_clock(); 512 cfqg_stats_mark_waiting(stats); 513} 514 515/* This should be called with the queue_lock held. */ 516static void cfqg_stats_end_empty_time(struct cfqg_stats *stats) 517{ 518 unsigned long long now; 519 520 if (!cfqg_stats_empty(stats)) 521 return; 522 523 now = sched_clock(); 524 if (time_after64(now, stats->start_empty_time)) 525 blkg_stat_add(&stats->empty_time, 526 now - stats->start_empty_time); 527 cfqg_stats_clear_empty(stats); 528} 529 530static void cfqg_stats_update_dequeue(struct cfq_group *cfqg) 531{ 532 blkg_stat_add(&cfqg->stats.dequeue, 1); 533} 534 535static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) 536{ 537 struct cfqg_stats *stats = &cfqg->stats; 538 539 if (blkg_rwstat_total(&stats->queued)) 540 return; 541 542 /* 543 * group is already marked empty. This can happen if cfqq got new 544 * request in parent group and moved to this group while being added 545 * to service tree. Just ignore the event and move on. 546 */ 547 if (cfqg_stats_empty(stats)) 548 return; 549 550 stats->start_empty_time = sched_clock(); 551 cfqg_stats_mark_empty(stats); 552} 553 554static void cfqg_stats_update_idle_time(struct cfq_group *cfqg) 555{ 556 struct cfqg_stats *stats = &cfqg->stats; 557 558 if (cfqg_stats_idling(stats)) { 559 unsigned long long now = sched_clock(); 560 561 if (time_after64(now, stats->start_idle_time)) 562 blkg_stat_add(&stats->idle_time, 563 now - stats->start_idle_time); 564 cfqg_stats_clear_idling(stats); 565 } 566} 567 568static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) 569{ 570 struct cfqg_stats *stats = &cfqg->stats; 571 572 BUG_ON(cfqg_stats_idling(stats)); 573 574 stats->start_idle_time = sched_clock(); 575 cfqg_stats_mark_idling(stats); 576} 577 578static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) 579{ 580 struct cfqg_stats *stats = &cfqg->stats; 581 582 blkg_stat_add(&stats->avg_queue_size_sum, 583 blkg_rwstat_total(&stats->queued)); 584 blkg_stat_add(&stats->avg_queue_size_samples, 1); 585 cfqg_stats_update_group_wait_time(stats); 586} 587 588#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ 589 590static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { } 591static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { } 592static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { } 593static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { } 594static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { } 595static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { } 596static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { } 597 598#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ 599 600#ifdef CONFIG_CFQ_GROUP_IOSCHED 601 602static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd) 603{ 604 return pd ? container_of(pd, struct cfq_group, pd) : NULL; 605} 606 607static struct cfq_group_data 608*cpd_to_cfqgd(struct blkcg_policy_data *cpd) 609{ 610 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL; 611} 612 613static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg) 614{ 615 return pd_to_blkg(&cfqg->pd); 616} 617 618static struct blkcg_policy blkcg_policy_cfq; 619 620static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg) 621{ 622 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq)); 623} 624 625static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg) 626{ 627 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq)); 628} 629 630static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) 631{ 632 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent; 633 634 return pblkg ? blkg_to_cfqg(pblkg) : NULL; 635} 636 637static inline bool cfqg_is_descendant(struct cfq_group *cfqg, 638 struct cfq_group *ancestor) 639{ 640 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup, 641 cfqg_to_blkg(ancestor)->blkcg->css.cgroup); 642} 643 644static inline void cfqg_get(struct cfq_group *cfqg) 645{ 646 return blkg_get(cfqg_to_blkg(cfqg)); 647} 648 649static inline void cfqg_put(struct cfq_group *cfqg) 650{ 651 return blkg_put(cfqg_to_blkg(cfqg)); 652} 653 654#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \ 655 char __pbuf[128]; \ 656 \ 657 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \ 658 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \ 659 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ 660 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ 661 __pbuf, ##args); \ 662} while (0) 663 664#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \ 665 char __pbuf[128]; \ 666 \ 667 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \ 668 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \ 669} while (0) 670 671static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, 672 struct cfq_group *curr_cfqg, 673 unsigned int op) 674{ 675 blkg_rwstat_add(&cfqg->stats.queued, op, 1); 676 cfqg_stats_end_empty_time(&cfqg->stats); 677 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg); 678} 679 680static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, 681 uint64_t time, unsigned long unaccounted_time) 682{ 683 blkg_stat_add(&cfqg->stats.time, time); 684#ifdef CONFIG_DEBUG_BLK_CGROUP 685 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time); 686#endif 687} 688 689static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, 690 unsigned int op) 691{ 692 blkg_rwstat_add(&cfqg->stats.queued, op, -1); 693} 694 695static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, 696 unsigned int op) 697{ 698 blkg_rwstat_add(&cfqg->stats.merged, op, 1); 699} 700 701static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, 702 uint64_t start_time, uint64_t io_start_time, 703 unsigned int op) 704{ 705 struct cfqg_stats *stats = &cfqg->stats; 706 unsigned long long now = sched_clock(); 707 708 if (time_after64(now, io_start_time)) 709 blkg_rwstat_add(&stats->service_time, op, now - io_start_time); 710 if (time_after64(io_start_time, start_time)) 711 blkg_rwstat_add(&stats->wait_time, op, 712 io_start_time - start_time); 713} 714 715/* @stats = 0 */ 716static void cfqg_stats_reset(struct cfqg_stats *stats) 717{ 718 /* queued stats shouldn't be cleared */ 719 blkg_rwstat_reset(&stats->merged); 720 blkg_rwstat_reset(&stats->service_time); 721 blkg_rwstat_reset(&stats->wait_time); 722 blkg_stat_reset(&stats->time); 723#ifdef CONFIG_DEBUG_BLK_CGROUP 724 blkg_stat_reset(&stats->unaccounted_time); 725 blkg_stat_reset(&stats->avg_queue_size_sum); 726 blkg_stat_reset(&stats->avg_queue_size_samples); 727 blkg_stat_reset(&stats->dequeue); 728 blkg_stat_reset(&stats->group_wait_time); 729 blkg_stat_reset(&stats->idle_time); 730 blkg_stat_reset(&stats->empty_time); 731#endif 732} 733 734/* @to += @from */ 735static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from) 736{ 737 /* queued stats shouldn't be cleared */ 738 blkg_rwstat_add_aux(&to->merged, &from->merged); 739 blkg_rwstat_add_aux(&to->service_time, &from->service_time); 740 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time); 741 blkg_stat_add_aux(&from->time, &from->time); 742#ifdef CONFIG_DEBUG_BLK_CGROUP 743 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time); 744 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum); 745 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples); 746 blkg_stat_add_aux(&to->dequeue, &from->dequeue); 747 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time); 748 blkg_stat_add_aux(&to->idle_time, &from->idle_time); 749 blkg_stat_add_aux(&to->empty_time, &from->empty_time); 750#endif 751} 752 753/* 754 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors' 755 * recursive stats can still account for the amount used by this cfqg after 756 * it's gone. 757 */ 758static void cfqg_stats_xfer_dead(struct cfq_group *cfqg) 759{ 760 struct cfq_group *parent = cfqg_parent(cfqg); 761 762 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock); 763 764 if (unlikely(!parent)) 765 return; 766 767 cfqg_stats_add_aux(&parent->stats, &cfqg->stats); 768 cfqg_stats_reset(&cfqg->stats); 769} 770 771#else /* CONFIG_CFQ_GROUP_IOSCHED */ 772 773static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; } 774static inline bool cfqg_is_descendant(struct cfq_group *cfqg, 775 struct cfq_group *ancestor) 776{ 777 return true; 778} 779static inline void cfqg_get(struct cfq_group *cfqg) { } 780static inline void cfqg_put(struct cfq_group *cfqg) { } 781 782#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ 783 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \ 784 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ 785 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ 786 ##args) 787#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0) 788 789static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, 790 struct cfq_group *curr_cfqg, unsigned int op) { } 791static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, 792 uint64_t time, unsigned long unaccounted_time) { } 793static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, 794 unsigned int op) { } 795static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, 796 unsigned int op) { } 797static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, 798 uint64_t start_time, uint64_t io_start_time, 799 unsigned int op) { } 800 801#endif /* CONFIG_CFQ_GROUP_IOSCHED */ 802 803#define cfq_log(cfqd, fmt, args...) \ 804 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) 805 806/* Traverses through cfq group service trees */ 807#define for_each_cfqg_st(cfqg, i, j, st) \ 808 for (i = 0; i <= IDLE_WORKLOAD; i++) \ 809 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\ 810 : &cfqg->service_tree_idle; \ 811 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \ 812 (i == IDLE_WORKLOAD && j == 0); \ 813 j++, st = i < IDLE_WORKLOAD ? \ 814 &cfqg->service_trees[i][j]: NULL) \ 815 816static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd, 817 struct cfq_ttime *ttime, bool group_idle) 818{ 819 u64 slice; 820 if (!sample_valid(ttime->ttime_samples)) 821 return false; 822 if (group_idle) 823 slice = cfqd->cfq_group_idle; 824 else 825 slice = cfqd->cfq_slice_idle; 826 return ttime->ttime_mean > slice; 827} 828 829static inline bool iops_mode(struct cfq_data *cfqd) 830{ 831 /* 832 * If we are not idling on queues and it is a NCQ drive, parallel 833 * execution of requests is on and measuring time is not possible 834 * in most of the cases until and unless we drive shallower queue 835 * depths and that becomes a performance bottleneck. In such cases 836 * switch to start providing fairness in terms of number of IOs. 837 */ 838 if (!cfqd->cfq_slice_idle && cfqd->hw_tag) 839 return true; 840 else 841 return false; 842} 843 844static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq) 845{ 846 if (cfq_class_idle(cfqq)) 847 return IDLE_WORKLOAD; 848 if (cfq_class_rt(cfqq)) 849 return RT_WORKLOAD; 850 return BE_WORKLOAD; 851} 852 853 854static enum wl_type_t cfqq_type(struct cfq_queue *cfqq) 855{ 856 if (!cfq_cfqq_sync(cfqq)) 857 return ASYNC_WORKLOAD; 858 if (!cfq_cfqq_idle_window(cfqq)) 859 return SYNC_NOIDLE_WORKLOAD; 860 return SYNC_WORKLOAD; 861} 862 863static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class, 864 struct cfq_data *cfqd, 865 struct cfq_group *cfqg) 866{ 867 if (wl_class == IDLE_WORKLOAD) 868 return cfqg->service_tree_idle.count; 869 870 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count + 871 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count + 872 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count; 873} 874 875static inline int cfqg_busy_async_queues(struct cfq_data *cfqd, 876 struct cfq_group *cfqg) 877{ 878 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count + 879 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count; 880} 881 882static void cfq_dispatch_insert(struct request_queue *, struct request *); 883static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync, 884 struct cfq_io_cq *cic, struct bio *bio); 885 886static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq) 887{ 888 /* cic->icq is the first member, %NULL will convert to %NULL */ 889 return container_of(icq, struct cfq_io_cq, icq); 890} 891 892static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd, 893 struct io_context *ioc) 894{ 895 if (ioc) 896 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue)); 897 return NULL; 898} 899 900static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync) 901{ 902 return cic->cfqq[is_sync]; 903} 904 905static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq, 906 bool is_sync) 907{ 908 cic->cfqq[is_sync] = cfqq; 909} 910 911static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic) 912{ 913 return cic->icq.q->elevator->elevator_data; 914} 915 916/* 917 * scheduler run of queue, if there are requests pending and no one in the 918 * driver that will restart queueing 919 */ 920static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) 921{ 922 if (cfqd->busy_queues) { 923 cfq_log(cfqd, "schedule dispatch"); 924 kblockd_schedule_work(&cfqd->unplug_work); 925 } 926} 927 928/* 929 * Scale schedule slice based on io priority. Use the sync time slice only 930 * if a queue is marked sync and has sync io queued. A sync queue with async 931 * io only, should not get full sync slice length. 932 */ 933static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync, 934 unsigned short prio) 935{ 936 u64 base_slice = cfqd->cfq_slice[sync]; 937 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE); 938 939 WARN_ON(prio >= IOPRIO_BE_NR); 940 941 return base_slice + (slice * (4 - prio)); 942} 943 944static inline u64 945cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) 946{ 947 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); 948} 949 950/** 951 * cfqg_scale_charge - scale disk time charge according to cfqg weight 952 * @charge: disk time being charged 953 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT 954 * 955 * Scale @charge according to @vfraction, which is in range (0, 1]. The 956 * scaling is inversely proportional. 957 * 958 * scaled = charge / vfraction 959 * 960 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT. 961 */ 962static inline u64 cfqg_scale_charge(u64 charge, 963 unsigned int vfraction) 964{ 965 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */ 966 967 /* charge / vfraction */ 968 c <<= CFQ_SERVICE_SHIFT; 969 return div_u64(c, vfraction); 970} 971 972static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime) 973{ 974 s64 delta = (s64)(vdisktime - min_vdisktime); 975 if (delta > 0) 976 min_vdisktime = vdisktime; 977 978 return min_vdisktime; 979} 980 981static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime) 982{ 983 s64 delta = (s64)(vdisktime - min_vdisktime); 984 if (delta < 0) 985 min_vdisktime = vdisktime; 986 987 return min_vdisktime; 988} 989 990static void update_min_vdisktime(struct cfq_rb_root *st) 991{ 992 struct cfq_group *cfqg; 993 994 if (st->left) { 995 cfqg = rb_entry_cfqg(st->left); 996 st->min_vdisktime = max_vdisktime(st->min_vdisktime, 997 cfqg->vdisktime); 998 } 999} 1000 1001/* 1002 * get averaged number of queues of RT/BE priority. 1003 * average is updated, with a formula that gives more weight to higher numbers, 1004 * to quickly follows sudden increases and decrease slowly 1005 */ 1006 1007static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd, 1008 struct cfq_group *cfqg, bool rt) 1009{ 1010 unsigned min_q, max_q; 1011 unsigned mult = cfq_hist_divisor - 1; 1012 unsigned round = cfq_hist_divisor / 2; 1013 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg); 1014 1015 min_q = min(cfqg->busy_queues_avg[rt], busy); 1016 max_q = max(cfqg->busy_queues_avg[rt], busy); 1017 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) / 1018 cfq_hist_divisor; 1019 return cfqg->busy_queues_avg[rt]; 1020} 1021 1022static inline u64 1023cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg) 1024{ 1025 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT; 1026} 1027 1028static inline u64 1029cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) 1030{ 1031 u64 slice = cfq_prio_to_slice(cfqd, cfqq); 1032 if (cfqd->cfq_latency) { 1033 /* 1034 * interested queues (we consider only the ones with the same 1035 * priority class in the cfq group) 1036 */ 1037 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg, 1038 cfq_class_rt(cfqq)); 1039 u64 sync_slice = cfqd->cfq_slice[1]; 1040 u64 expect_latency = sync_slice * iq; 1041 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg); 1042 1043 if (expect_latency > group_slice) { 1044 u64 base_low_slice = 2 * cfqd->cfq_slice_idle; 1045 u64 low_slice; 1046 1047 /* scale low_slice according to IO priority 1048 * and sync vs async */ 1049 low_slice = div64_u64(base_low_slice*slice, sync_slice); 1050 low_slice = min(slice, low_slice); 1051 /* the adapted slice value is scaled to fit all iqs 1052 * into the target latency */ 1053 slice = div64_u64(slice*group_slice, expect_latency); 1054 slice = max(slice, low_slice); 1055 } 1056 } 1057 return slice; 1058} 1059 1060static inline void 1061cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) 1062{ 1063 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq); 1064 u64 now = ktime_get_ns(); 1065 1066 cfqq->slice_start = now; 1067 cfqq->slice_end = now + slice; 1068 cfqq->allocated_slice = slice; 1069 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now); 1070} 1071 1072/* 1073 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end 1074 * isn't valid until the first request from the dispatch is activated 1075 * and the slice time set. 1076 */ 1077static inline bool cfq_slice_used(struct cfq_queue *cfqq) 1078{ 1079 if (cfq_cfqq_slice_new(cfqq)) 1080 return false; 1081 if (ktime_get_ns() < cfqq->slice_end) 1082 return false; 1083 1084 return true; 1085} 1086 1087/* 1088 * Lifted from AS - choose which of rq1 and rq2 that is best served now. 1089 * We choose the request that is closest to the head right now. Distance 1090 * behind the head is penalized and only allowed to a certain extent. 1091 */ 1092static struct request * 1093cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last) 1094{ 1095 sector_t s1, s2, d1 = 0, d2 = 0; 1096 unsigned long back_max; 1097#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ 1098#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ 1099 unsigned wrap = 0; /* bit mask: requests behind the disk head? */ 1100 1101 if (rq1 == NULL || rq1 == rq2) 1102 return rq2; 1103 if (rq2 == NULL) 1104 return rq1; 1105 1106 if (rq_is_sync(rq1) != rq_is_sync(rq2)) 1107 return rq_is_sync(rq1) ? rq1 : rq2; 1108 1109 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO) 1110 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2; 1111 1112 s1 = blk_rq_pos(rq1); 1113 s2 = blk_rq_pos(rq2); 1114 1115 /* 1116 * by definition, 1KiB is 2 sectors 1117 */ 1118 back_max = cfqd->cfq_back_max * 2; 1119 1120 /* 1121 * Strict one way elevator _except_ in the case where we allow 1122 * short backward seeks which are biased as twice the cost of a 1123 * similar forward seek. 1124 */ 1125 if (s1 >= last) 1126 d1 = s1 - last; 1127 else if (s1 + back_max >= last) 1128 d1 = (last - s1) * cfqd->cfq_back_penalty; 1129 else 1130 wrap |= CFQ_RQ1_WRAP; 1131 1132 if (s2 >= last) 1133 d2 = s2 - last; 1134 else if (s2 + back_max >= last) 1135 d2 = (last - s2) * cfqd->cfq_back_penalty; 1136 else 1137 wrap |= CFQ_RQ2_WRAP; 1138 1139 /* Found required data */ 1140 1141 /* 1142 * By doing switch() on the bit mask "wrap" we avoid having to 1143 * check two variables for all permutations: --> faster! 1144 */ 1145 switch (wrap) { 1146 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ 1147 if (d1 < d2) 1148 return rq1; 1149 else if (d2 < d1) 1150 return rq2; 1151 else { 1152 if (s1 >= s2) 1153 return rq1; 1154 else 1155 return rq2; 1156 } 1157 1158 case CFQ_RQ2_WRAP: 1159 return rq1; 1160 case CFQ_RQ1_WRAP: 1161 return rq2; 1162 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ 1163 default: 1164 /* 1165 * Since both rqs are wrapped, 1166 * start with the one that's further behind head 1167 * (--> only *one* back seek required), 1168 * since back seek takes more time than forward. 1169 */ 1170 if (s1 <= s2) 1171 return rq1; 1172 else 1173 return rq2; 1174 } 1175} 1176 1177/* 1178 * The below is leftmost cache rbtree addon 1179 */ 1180static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) 1181{ 1182 /* Service tree is empty */ 1183 if (!root->count) 1184 return NULL; 1185 1186 if (!root->left) 1187 root->left = rb_first(&root->rb); 1188 1189 if (root->left) 1190 return rb_entry(root->left, struct cfq_queue, rb_node); 1191 1192 return NULL; 1193} 1194 1195static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root) 1196{ 1197 if (!root->left) 1198 root->left = rb_first(&root->rb); 1199 1200 if (root->left) 1201 return rb_entry_cfqg(root->left); 1202 1203 return NULL; 1204} 1205 1206static void rb_erase_init(struct rb_node *n, struct rb_root *root) 1207{ 1208 rb_erase(n, root); 1209 RB_CLEAR_NODE(n); 1210} 1211 1212static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) 1213{ 1214 if (root->left == n) 1215 root->left = NULL; 1216 rb_erase_init(n, &root->rb); 1217 --root->count; 1218} 1219 1220/* 1221 * would be nice to take fifo expire time into account as well 1222 */ 1223static struct request * 1224cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, 1225 struct request *last) 1226{ 1227 struct rb_node *rbnext = rb_next(&last->rb_node); 1228 struct rb_node *rbprev = rb_prev(&last->rb_node); 1229 struct request *next = NULL, *prev = NULL; 1230 1231 BUG_ON(RB_EMPTY_NODE(&last->rb_node)); 1232 1233 if (rbprev) 1234 prev = rb_entry_rq(rbprev); 1235 1236 if (rbnext) 1237 next = rb_entry_rq(rbnext); 1238 else { 1239 rbnext = rb_first(&cfqq->sort_list); 1240 if (rbnext && rbnext != &last->rb_node) 1241 next = rb_entry_rq(rbnext); 1242 } 1243 1244 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last)); 1245} 1246 1247static u64 cfq_slice_offset(struct cfq_data *cfqd, 1248 struct cfq_queue *cfqq) 1249{ 1250 /* 1251 * just an approximation, should be ok. 1252 */ 1253 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) - 1254 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); 1255} 1256 1257static inline s64 1258cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg) 1259{ 1260 return cfqg->vdisktime - st->min_vdisktime; 1261} 1262 1263static void 1264__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) 1265{ 1266 struct rb_node **node = &st->rb.rb_node; 1267 struct rb_node *parent = NULL; 1268 struct cfq_group *__cfqg; 1269 s64 key = cfqg_key(st, cfqg); 1270 int left = 1; 1271 1272 while (*node != NULL) { 1273 parent = *node; 1274 __cfqg = rb_entry_cfqg(parent); 1275 1276 if (key < cfqg_key(st, __cfqg)) 1277 node = &parent->rb_left; 1278 else { 1279 node = &parent->rb_right; 1280 left = 0; 1281 } 1282 } 1283 1284 if (left) 1285 st->left = &cfqg->rb_node; 1286 1287 rb_link_node(&cfqg->rb_node, parent, node); 1288 rb_insert_color(&cfqg->rb_node, &st->rb); 1289} 1290 1291/* 1292 * This has to be called only on activation of cfqg 1293 */ 1294static void 1295cfq_update_group_weight(struct cfq_group *cfqg) 1296{ 1297 if (cfqg->new_weight) { 1298 cfqg->weight = cfqg->new_weight; 1299 cfqg->new_weight = 0; 1300 } 1301} 1302 1303static void 1304cfq_update_group_leaf_weight(struct cfq_group *cfqg) 1305{ 1306 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); 1307 1308 if (cfqg->new_leaf_weight) { 1309 cfqg->leaf_weight = cfqg->new_leaf_weight; 1310 cfqg->new_leaf_weight = 0; 1311 } 1312} 1313 1314static void 1315cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) 1316{ 1317 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */ 1318 struct cfq_group *pos = cfqg; 1319 struct cfq_group *parent; 1320 bool propagate; 1321 1322 /* add to the service tree */ 1323 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); 1324 1325 /* 1326 * Update leaf_weight. We cannot update weight at this point 1327 * because cfqg might already have been activated and is 1328 * contributing its current weight to the parent's child_weight. 1329 */ 1330 cfq_update_group_leaf_weight(cfqg); 1331 __cfq_group_service_tree_add(st, cfqg); 1332 1333 /* 1334 * Activate @cfqg and calculate the portion of vfraction @cfqg is 1335 * entitled to. vfraction is calculated by walking the tree 1336 * towards the root calculating the fraction it has at each level. 1337 * The compounded ratio is how much vfraction @cfqg owns. 1338 * 1339 * Start with the proportion tasks in this cfqg has against active 1340 * children cfqgs - its leaf_weight against children_weight. 1341 */ 1342 propagate = !pos->nr_active++; 1343 pos->children_weight += pos->leaf_weight; 1344 vfr = vfr * pos->leaf_weight / pos->children_weight; 1345 1346 /* 1347 * Compound ->weight walking up the tree. Both activation and 1348 * vfraction calculation are done in the same loop. Propagation 1349 * stops once an already activated node is met. vfraction 1350 * calculation should always continue to the root. 1351 */ 1352 while ((parent = cfqg_parent(pos))) { 1353 if (propagate) { 1354 cfq_update_group_weight(pos); 1355 propagate = !parent->nr_active++; 1356 parent->children_weight += pos->weight; 1357 } 1358 vfr = vfr * pos->weight / parent->children_weight; 1359 pos = parent; 1360 } 1361 1362 cfqg->vfraction = max_t(unsigned, vfr, 1); 1363} 1364 1365static void 1366cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg) 1367{ 1368 struct cfq_rb_root *st = &cfqd->grp_service_tree; 1369 struct cfq_group *__cfqg; 1370 struct rb_node *n; 1371 1372 cfqg->nr_cfqq++; 1373 if (!RB_EMPTY_NODE(&cfqg->rb_node)) 1374 return; 1375 1376 /* 1377 * Currently put the group at the end. Later implement something 1378 * so that groups get lesser vtime based on their weights, so that 1379 * if group does not loose all if it was not continuously backlogged. 1380 */ 1381 n = rb_last(&st->rb); 1382 if (n) { 1383 __cfqg = rb_entry_cfqg(n); 1384 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY; 1385 } else 1386 cfqg->vdisktime = st->min_vdisktime; 1387 cfq_group_service_tree_add(st, cfqg); 1388} 1389 1390static void 1391cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg) 1392{ 1393 struct cfq_group *pos = cfqg; 1394 bool propagate; 1395 1396 /* 1397 * Undo activation from cfq_group_service_tree_add(). Deactivate 1398 * @cfqg and propagate deactivation upwards. 1399 */ 1400 propagate = !--pos->nr_active; 1401 pos->children_weight -= pos->leaf_weight; 1402 1403 while (propagate) { 1404 struct cfq_group *parent = cfqg_parent(pos); 1405 1406 /* @pos has 0 nr_active at this point */ 1407 WARN_ON_ONCE(pos->children_weight); 1408 pos->vfraction = 0; 1409 1410 if (!parent) 1411 break; 1412 1413 propagate = !--parent->nr_active; 1414 parent->children_weight -= pos->weight; 1415 pos = parent; 1416 } 1417 1418 /* remove from the service tree */ 1419 if (!RB_EMPTY_NODE(&cfqg->rb_node)) 1420 cfq_rb_erase(&cfqg->rb_node, st); 1421} 1422 1423static void 1424cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg) 1425{ 1426 struct cfq_rb_root *st = &cfqd->grp_service_tree; 1427 1428 BUG_ON(cfqg->nr_cfqq < 1); 1429 cfqg->nr_cfqq--; 1430 1431 /* If there are other cfq queues under this group, don't delete it */ 1432 if (cfqg->nr_cfqq) 1433 return; 1434 1435 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group"); 1436 cfq_group_service_tree_del(st, cfqg); 1437 cfqg->saved_wl_slice = 0; 1438 cfqg_stats_update_dequeue(cfqg); 1439} 1440 1441static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq, 1442 u64 *unaccounted_time) 1443{ 1444 u64 slice_used; 1445 u64 now = ktime_get_ns(); 1446 1447 /* 1448 * Queue got expired before even a single request completed or 1449 * got expired immediately after first request completion. 1450 */ 1451 if (!cfqq->slice_start || cfqq->slice_start == now) { 1452 /* 1453 * Also charge the seek time incurred to the group, otherwise 1454 * if there are mutiple queues in the group, each can dispatch 1455 * a single request on seeky media and cause lots of seek time 1456 * and group will never know it. 1457 */ 1458 slice_used = max_t(u64, (now - cfqq->dispatch_start), 1459 jiffies_to_nsecs(1)); 1460 } else { 1461 slice_used = now - cfqq->slice_start; 1462 if (slice_used > cfqq->allocated_slice) { 1463 *unaccounted_time = slice_used - cfqq->allocated_slice; 1464 slice_used = cfqq->allocated_slice; 1465 } 1466 if (cfqq->slice_start > cfqq->dispatch_start) 1467 *unaccounted_time += cfqq->slice_start - 1468 cfqq->dispatch_start; 1469 } 1470 1471 return slice_used; 1472} 1473 1474static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg, 1475 struct cfq_queue *cfqq) 1476{ 1477 struct cfq_rb_root *st = &cfqd->grp_service_tree; 1478 u64 used_sl, charge, unaccounted_sl = 0; 1479 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg) 1480 - cfqg->service_tree_idle.count; 1481 unsigned int vfr; 1482 u64 now = ktime_get_ns(); 1483 1484 BUG_ON(nr_sync < 0); 1485 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl); 1486 1487 if (iops_mode(cfqd)) 1488 charge = cfqq->slice_dispatch; 1489 else if (!cfq_cfqq_sync(cfqq) && !nr_sync) 1490 charge = cfqq->allocated_slice; 1491 1492 /* 1493 * Can't update vdisktime while on service tree and cfqg->vfraction 1494 * is valid only while on it. Cache vfr, leave the service tree, 1495 * update vdisktime and go back on. The re-addition to the tree 1496 * will also update the weights as necessary. 1497 */ 1498 vfr = cfqg->vfraction; 1499 cfq_group_service_tree_del(st, cfqg); 1500 cfqg->vdisktime += cfqg_scale_charge(charge, vfr); 1501 cfq_group_service_tree_add(st, cfqg); 1502 1503 /* This group is being expired. Save the context */ 1504 if (cfqd->workload_expires > now) { 1505 cfqg->saved_wl_slice = cfqd->workload_expires - now; 1506 cfqg->saved_wl_type = cfqd->serving_wl_type; 1507 cfqg->saved_wl_class = cfqd->serving_wl_class; 1508 } else 1509 cfqg->saved_wl_slice = 0; 1510 1511 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime, 1512 st->min_vdisktime); 1513 cfq_log_cfqq(cfqq->cfqd, cfqq, 1514 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu", 1515 used_sl, cfqq->slice_dispatch, charge, 1516 iops_mode(cfqd), cfqq->nr_sectors); 1517 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl); 1518 cfqg_stats_set_start_empty_time(cfqg); 1519} 1520 1521/** 1522 * cfq_init_cfqg_base - initialize base part of a cfq_group 1523 * @cfqg: cfq_group to initialize 1524 * 1525 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED 1526 * is enabled or not. 1527 */ 1528static void cfq_init_cfqg_base(struct cfq_group *cfqg) 1529{ 1530 struct cfq_rb_root *st; 1531 int i, j; 1532 1533 for_each_cfqg_st(cfqg, i, j, st) 1534 *st = CFQ_RB_ROOT; 1535 RB_CLEAR_NODE(&cfqg->rb_node); 1536 1537 cfqg->ttime.last_end_request = ktime_get_ns(); 1538} 1539 1540#ifdef CONFIG_CFQ_GROUP_IOSCHED 1541static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val, 1542 bool on_dfl, bool reset_dev, bool is_leaf_weight); 1543 1544static void cfqg_stats_exit(struct cfqg_stats *stats) 1545{ 1546 blkg_rwstat_exit(&stats->merged); 1547 blkg_rwstat_exit(&stats->service_time); 1548 blkg_rwstat_exit(&stats->wait_time); 1549 blkg_rwstat_exit(&stats->queued); 1550 blkg_stat_exit(&stats->time); 1551#ifdef CONFIG_DEBUG_BLK_CGROUP 1552 blkg_stat_exit(&stats->unaccounted_time); 1553 blkg_stat_exit(&stats->avg_queue_size_sum); 1554 blkg_stat_exit(&stats->avg_queue_size_samples); 1555 blkg_stat_exit(&stats->dequeue); 1556 blkg_stat_exit(&stats->group_wait_time); 1557 blkg_stat_exit(&stats->idle_time); 1558 blkg_stat_exit(&stats->empty_time); 1559#endif 1560} 1561 1562static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp) 1563{ 1564 if (blkg_rwstat_init(&stats->merged, gfp) || 1565 blkg_rwstat_init(&stats->service_time, gfp) || 1566 blkg_rwstat_init(&stats->wait_time, gfp) || 1567 blkg_rwstat_init(&stats->queued, gfp) || 1568 blkg_stat_init(&stats->time, gfp)) 1569 goto err; 1570 1571#ifdef CONFIG_DEBUG_BLK_CGROUP 1572 if (blkg_stat_init(&stats->unaccounted_time, gfp) || 1573 blkg_stat_init(&stats->avg_queue_size_sum, gfp) || 1574 blkg_stat_init(&stats->avg_queue_size_samples, gfp) || 1575 blkg_stat_init(&stats->dequeue, gfp) || 1576 blkg_stat_init(&stats->group_wait_time, gfp) || 1577 blkg_stat_init(&stats->idle_time, gfp) || 1578 blkg_stat_init(&stats->empty_time, gfp)) 1579 goto err; 1580#endif 1581 return 0; 1582err: 1583 cfqg_stats_exit(stats); 1584 return -ENOMEM; 1585} 1586 1587static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp) 1588{ 1589 struct cfq_group_data *cgd; 1590 1591 cgd = kzalloc(sizeof(*cgd), gfp); 1592 if (!cgd) 1593 return NULL; 1594 return &cgd->cpd; 1595} 1596 1597static void cfq_cpd_init(struct blkcg_policy_data *cpd) 1598{ 1599 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd); 1600 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ? 1601 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL; 1602 1603 if (cpd_to_blkcg(cpd) == &blkcg_root) 1604 weight *= 2; 1605 1606 cgd->weight = weight; 1607 cgd->leaf_weight = weight; 1608} 1609 1610static void cfq_cpd_free(struct blkcg_policy_data *cpd) 1611{ 1612 kfree(cpd_to_cfqgd(cpd)); 1613} 1614 1615static void cfq_cpd_bind(struct blkcg_policy_data *cpd) 1616{ 1617 struct blkcg *blkcg = cpd_to_blkcg(cpd); 1618 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys); 1619 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL; 1620 1621 if (blkcg == &blkcg_root) 1622 weight *= 2; 1623 1624 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false)); 1625 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true)); 1626} 1627 1628static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node) 1629{ 1630 struct cfq_group *cfqg; 1631 1632 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node); 1633 if (!cfqg) 1634 return NULL; 1635 1636 cfq_init_cfqg_base(cfqg); 1637 if (cfqg_stats_init(&cfqg->stats, gfp)) { 1638 kfree(cfqg); 1639 return NULL; 1640 } 1641 1642 return &cfqg->pd; 1643} 1644 1645static void cfq_pd_init(struct blkg_policy_data *pd) 1646{ 1647 struct cfq_group *cfqg = pd_to_cfqg(pd); 1648 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg); 1649 1650 cfqg->weight = cgd->weight; 1651 cfqg->leaf_weight = cgd->leaf_weight; 1652} 1653 1654static void cfq_pd_offline(struct blkg_policy_data *pd) 1655{ 1656 struct cfq_group *cfqg = pd_to_cfqg(pd); 1657 int i; 1658 1659 for (i = 0; i < IOPRIO_BE_NR; i++) { 1660 if (cfqg->async_cfqq[0][i]) 1661 cfq_put_queue(cfqg->async_cfqq[0][i]); 1662 if (cfqg->async_cfqq[1][i]) 1663 cfq_put_queue(cfqg->async_cfqq[1][i]); 1664 } 1665 1666 if (cfqg->async_idle_cfqq) 1667 cfq_put_queue(cfqg->async_idle_cfqq); 1668 1669 /* 1670 * @blkg is going offline and will be ignored by 1671 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so 1672 * that they don't get lost. If IOs complete after this point, the 1673 * stats for them will be lost. Oh well... 1674 */ 1675 cfqg_stats_xfer_dead(cfqg); 1676} 1677 1678static void cfq_pd_free(struct blkg_policy_data *pd) 1679{ 1680 struct cfq_group *cfqg = pd_to_cfqg(pd); 1681 1682 cfqg_stats_exit(&cfqg->stats); 1683 return kfree(cfqg); 1684} 1685 1686static void cfq_pd_reset_stats(struct blkg_policy_data *pd) 1687{ 1688 struct cfq_group *cfqg = pd_to_cfqg(pd); 1689 1690 cfqg_stats_reset(&cfqg->stats); 1691} 1692 1693static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd, 1694 struct blkcg *blkcg) 1695{ 1696 struct blkcg_gq *blkg; 1697 1698 blkg = blkg_lookup(blkcg, cfqd->queue); 1699 if (likely(blkg)) 1700 return blkg_to_cfqg(blkg); 1701 return NULL; 1702} 1703 1704static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) 1705{ 1706 cfqq->cfqg = cfqg; 1707 /* cfqq reference on cfqg */ 1708 cfqg_get(cfqg); 1709} 1710 1711static u64 cfqg_prfill_weight_device(struct seq_file *sf, 1712 struct blkg_policy_data *pd, int off) 1713{ 1714 struct cfq_group *cfqg = pd_to_cfqg(pd); 1715 1716 if (!cfqg->dev_weight) 1717 return 0; 1718 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight); 1719} 1720 1721static int cfqg_print_weight_device(struct seq_file *sf, void *v) 1722{ 1723 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1724 cfqg_prfill_weight_device, &blkcg_policy_cfq, 1725 0, false); 1726 return 0; 1727} 1728 1729static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf, 1730 struct blkg_policy_data *pd, int off) 1731{ 1732 struct cfq_group *cfqg = pd_to_cfqg(pd); 1733 1734 if (!cfqg->dev_leaf_weight) 1735 return 0; 1736 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight); 1737} 1738 1739static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v) 1740{ 1741 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1742 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 1743 0, false); 1744 return 0; 1745} 1746 1747static int cfq_print_weight(struct seq_file *sf, void *v) 1748{ 1749 struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); 1750 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); 1751 unsigned int val = 0; 1752 1753 if (cgd) 1754 val = cgd->weight; 1755 1756 seq_printf(sf, "%u\n", val); 1757 return 0; 1758} 1759 1760static int cfq_print_leaf_weight(struct seq_file *sf, void *v) 1761{ 1762 struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); 1763 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); 1764 unsigned int val = 0; 1765 1766 if (cgd) 1767 val = cgd->leaf_weight; 1768 1769 seq_printf(sf, "%u\n", val); 1770 return 0; 1771} 1772 1773static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of, 1774 char *buf, size_t nbytes, loff_t off, 1775 bool on_dfl, bool is_leaf_weight) 1776{ 1777 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN; 1778 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX; 1779 struct blkcg *blkcg = css_to_blkcg(of_css(of)); 1780 struct blkg_conf_ctx ctx; 1781 struct cfq_group *cfqg; 1782 struct cfq_group_data *cfqgd; 1783 int ret; 1784 u64 v; 1785 1786 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx); 1787 if (ret) 1788 return ret; 1789 1790 if (sscanf(ctx.body, "%llu", &v) == 1) { 1791 /* require "default" on dfl */ 1792 ret = -ERANGE; 1793 if (!v && on_dfl) 1794 goto out_finish; 1795 } else if (!strcmp(strim(ctx.body), "default")) { 1796 v = 0; 1797 } else { 1798 ret = -EINVAL; 1799 goto out_finish; 1800 } 1801 1802 cfqg = blkg_to_cfqg(ctx.blkg); 1803 cfqgd = blkcg_to_cfqgd(blkcg); 1804 1805 ret = -ERANGE; 1806 if (!v || (v >= min && v <= max)) { 1807 if (!is_leaf_weight) { 1808 cfqg->dev_weight = v; 1809 cfqg->new_weight = v ?: cfqgd->weight; 1810 } else { 1811 cfqg->dev_leaf_weight = v; 1812 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight; 1813 } 1814 ret = 0; 1815 } 1816out_finish: 1817 blkg_conf_finish(&ctx); 1818 return ret ?: nbytes; 1819} 1820 1821static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of, 1822 char *buf, size_t nbytes, loff_t off) 1823{ 1824 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false); 1825} 1826 1827static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of, 1828 char *buf, size_t nbytes, loff_t off) 1829{ 1830 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true); 1831} 1832 1833static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val, 1834 bool on_dfl, bool reset_dev, bool is_leaf_weight) 1835{ 1836 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN; 1837 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX; 1838 struct blkcg *blkcg = css_to_blkcg(css); 1839 struct blkcg_gq *blkg; 1840 struct cfq_group_data *cfqgd; 1841 int ret = 0; 1842 1843 if (val < min || val > max) 1844 return -ERANGE; 1845 1846 spin_lock_irq(&blkcg->lock); 1847 cfqgd = blkcg_to_cfqgd(blkcg); 1848 if (!cfqgd) { 1849 ret = -EINVAL; 1850 goto out; 1851 } 1852 1853 if (!is_leaf_weight) 1854 cfqgd->weight = val; 1855 else 1856 cfqgd->leaf_weight = val; 1857 1858 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { 1859 struct cfq_group *cfqg = blkg_to_cfqg(blkg); 1860 1861 if (!cfqg) 1862 continue; 1863 1864 if (!is_leaf_weight) { 1865 if (reset_dev) 1866 cfqg->dev_weight = 0; 1867 if (!cfqg->dev_weight) 1868 cfqg->new_weight = cfqgd->weight; 1869 } else { 1870 if (reset_dev) 1871 cfqg->dev_leaf_weight = 0; 1872 if (!cfqg->dev_leaf_weight) 1873 cfqg->new_leaf_weight = cfqgd->leaf_weight; 1874 } 1875 } 1876 1877out: 1878 spin_unlock_irq(&blkcg->lock); 1879 return ret; 1880} 1881 1882static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft, 1883 u64 val) 1884{ 1885 return __cfq_set_weight(css, val, false, false, false); 1886} 1887 1888static int cfq_set_leaf_weight(struct cgroup_subsys_state *css, 1889 struct cftype *cft, u64 val) 1890{ 1891 return __cfq_set_weight(css, val, false, false, true); 1892} 1893 1894static int cfqg_print_stat(struct seq_file *sf, void *v) 1895{ 1896 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat, 1897 &blkcg_policy_cfq, seq_cft(sf)->private, false); 1898 return 0; 1899} 1900 1901static int cfqg_print_rwstat(struct seq_file *sf, void *v) 1902{ 1903 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat, 1904 &blkcg_policy_cfq, seq_cft(sf)->private, true); 1905 return 0; 1906} 1907 1908static u64 cfqg_prfill_stat_recursive(struct seq_file *sf, 1909 struct blkg_policy_data *pd, int off) 1910{ 1911 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd), 1912 &blkcg_policy_cfq, off); 1913 return __blkg_prfill_u64(sf, pd, sum); 1914} 1915 1916static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf, 1917 struct blkg_policy_data *pd, int off) 1918{ 1919 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd), 1920 &blkcg_policy_cfq, off); 1921 return __blkg_prfill_rwstat(sf, pd, &sum); 1922} 1923 1924static int cfqg_print_stat_recursive(struct seq_file *sf, void *v) 1925{ 1926 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1927 cfqg_prfill_stat_recursive, &blkcg_policy_cfq, 1928 seq_cft(sf)->private, false); 1929 return 0; 1930} 1931 1932static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v) 1933{ 1934 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1935 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq, 1936 seq_cft(sf)->private, true); 1937 return 0; 1938} 1939 1940static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd, 1941 int off) 1942{ 1943 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes); 1944 1945 return __blkg_prfill_u64(sf, pd, sum >> 9); 1946} 1947 1948static int cfqg_print_stat_sectors(struct seq_file *sf, void *v) 1949{ 1950 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1951 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false); 1952 return 0; 1953} 1954 1955static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf, 1956 struct blkg_policy_data *pd, int off) 1957{ 1958 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL, 1959 offsetof(struct blkcg_gq, stat_bytes)); 1960 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + 1961 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); 1962 1963 return __blkg_prfill_u64(sf, pd, sum >> 9); 1964} 1965 1966static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v) 1967{ 1968 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1969 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0, 1970 false); 1971 return 0; 1972} 1973 1974#ifdef CONFIG_DEBUG_BLK_CGROUP 1975static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf, 1976 struct blkg_policy_data *pd, int off) 1977{ 1978 struct cfq_group *cfqg = pd_to_cfqg(pd); 1979 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples); 1980 u64 v = 0; 1981 1982 if (samples) { 1983 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum); 1984 v = div64_u64(v, samples); 1985 } 1986 __blkg_prfill_u64(sf, pd, v); 1987 return 0; 1988} 1989 1990/* print avg_queue_size */ 1991static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v) 1992{ 1993 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1994 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq, 1995 0, false); 1996 return 0; 1997} 1998#endif /* CONFIG_DEBUG_BLK_CGROUP */ 1999 2000static struct cftype cfq_blkcg_legacy_files[] = { 2001 /* on root, weight is mapped to leaf_weight */ 2002 { 2003 .name = "weight_device", 2004 .flags = CFTYPE_ONLY_ON_ROOT, 2005 .seq_show = cfqg_print_leaf_weight_device, 2006 .write = cfqg_set_leaf_weight_device, 2007 }, 2008 { 2009 .name = "weight", 2010 .flags = CFTYPE_ONLY_ON_ROOT, 2011 .seq_show = cfq_print_leaf_weight, 2012 .write_u64 = cfq_set_leaf_weight, 2013 }, 2014 2015 /* no such mapping necessary for !roots */ 2016 { 2017 .name = "weight_device", 2018 .flags = CFTYPE_NOT_ON_ROOT, 2019 .seq_show = cfqg_print_weight_device, 2020 .write = cfqg_set_weight_device, 2021 }, 2022 { 2023 .name = "weight", 2024 .flags = CFTYPE_NOT_ON_ROOT, 2025 .seq_show = cfq_print_weight, 2026 .write_u64 = cfq_set_weight, 2027 }, 2028 2029 { 2030 .name = "leaf_weight_device", 2031 .seq_show = cfqg_print_leaf_weight_device, 2032 .write = cfqg_set_leaf_weight_device, 2033 }, 2034 { 2035 .name = "leaf_weight", 2036 .seq_show = cfq_print_leaf_weight, 2037 .write_u64 = cfq_set_leaf_weight, 2038 }, 2039 2040 /* statistics, covers only the tasks in the cfqg */ 2041 { 2042 .name = "time", 2043 .private = offsetof(struct cfq_group, stats.time), 2044 .seq_show = cfqg_print_stat, 2045 }, 2046 { 2047 .name = "sectors", 2048 .seq_show = cfqg_print_stat_sectors, 2049 }, 2050 { 2051 .name = "io_service_bytes", 2052 .private = (unsigned long)&blkcg_policy_cfq, 2053 .seq_show = blkg_print_stat_bytes, 2054 }, 2055 { 2056 .name = "io_serviced", 2057 .private = (unsigned long)&blkcg_policy_cfq, 2058 .seq_show = blkg_print_stat_ios, 2059 }, 2060 { 2061 .name = "io_service_time", 2062 .private = offsetof(struct cfq_group, stats.service_time), 2063 .seq_show = cfqg_print_rwstat, 2064 }, 2065 { 2066 .name = "io_wait_time", 2067 .private = offsetof(struct cfq_group, stats.wait_time), 2068 .seq_show = cfqg_print_rwstat, 2069 }, 2070 { 2071 .name = "io_merged", 2072 .private = offsetof(struct cfq_group, stats.merged), 2073 .seq_show = cfqg_print_rwstat, 2074 }, 2075 { 2076 .name = "io_queued", 2077 .private = offsetof(struct cfq_group, stats.queued), 2078 .seq_show = cfqg_print_rwstat, 2079 }, 2080 2081 /* the same statictics which cover the cfqg and its descendants */ 2082 { 2083 .name = "time_recursive", 2084 .private = offsetof(struct cfq_group, stats.time), 2085 .seq_show = cfqg_print_stat_recursive, 2086 }, 2087 { 2088 .name = "sectors_recursive", 2089 .seq_show = cfqg_print_stat_sectors_recursive, 2090 }, 2091 { 2092 .name = "io_service_bytes_recursive", 2093 .private = (unsigned long)&blkcg_policy_cfq, 2094 .seq_show = blkg_print_stat_bytes_recursive, 2095 }, 2096 { 2097 .name = "io_serviced_recursive", 2098 .private = (unsigned long)&blkcg_policy_cfq, 2099 .seq_show = blkg_print_stat_ios_recursive, 2100 }, 2101 { 2102 .name = "io_service_time_recursive", 2103 .private = offsetof(struct cfq_group, stats.service_time), 2104 .seq_show = cfqg_print_rwstat_recursive, 2105 }, 2106 { 2107 .name = "io_wait_time_recursive", 2108 .private = offsetof(struct cfq_group, stats.wait_time), 2109 .seq_show = cfqg_print_rwstat_recursive, 2110 }, 2111 { 2112 .name = "io_merged_recursive", 2113 .private = offsetof(struct cfq_group, stats.merged), 2114 .seq_show = cfqg_print_rwstat_recursive, 2115 }, 2116 { 2117 .name = "io_queued_recursive", 2118 .private = offsetof(struct cfq_group, stats.queued), 2119 .seq_show = cfqg_print_rwstat_recursive, 2120 }, 2121#ifdef CONFIG_DEBUG_BLK_CGROUP 2122 { 2123 .name = "avg_queue_size", 2124 .seq_show = cfqg_print_avg_queue_size, 2125 }, 2126 { 2127 .name = "group_wait_time", 2128 .private = offsetof(struct cfq_group, stats.group_wait_time), 2129 .seq_show = cfqg_print_stat, 2130 }, 2131 { 2132 .name = "idle_time", 2133 .private = offsetof(struct cfq_group, stats.idle_time), 2134 .seq_show = cfqg_print_stat, 2135 }, 2136 { 2137 .name = "empty_time", 2138 .private = offsetof(struct cfq_group, stats.empty_time), 2139 .seq_show = cfqg_print_stat, 2140 }, 2141 { 2142 .name = "dequeue", 2143 .private = offsetof(struct cfq_group, stats.dequeue), 2144 .seq_show = cfqg_print_stat, 2145 }, 2146 { 2147 .name = "unaccounted_time", 2148 .private = offsetof(struct cfq_group, stats.unaccounted_time), 2149 .seq_show = cfqg_print_stat, 2150 }, 2151#endif /* CONFIG_DEBUG_BLK_CGROUP */ 2152 { } /* terminate */ 2153}; 2154 2155static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v) 2156{ 2157 struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); 2158 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); 2159 2160 seq_printf(sf, "default %u\n", cgd->weight); 2161 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device, 2162 &blkcg_policy_cfq, 0, false); 2163 return 0; 2164} 2165 2166static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of, 2167 char *buf, size_t nbytes, loff_t off) 2168{ 2169 char *endp; 2170 int ret; 2171 u64 v; 2172 2173 buf = strim(buf); 2174 2175 /* "WEIGHT" or "default WEIGHT" sets the default weight */ 2176 v = simple_strtoull(buf, &endp, 0); 2177 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) { 2178 ret = __cfq_set_weight(of_css(of), v, true, false, false); 2179 return ret ?: nbytes; 2180 } 2181 2182 /* "MAJ:MIN WEIGHT" */ 2183 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false); 2184} 2185 2186static struct cftype cfq_blkcg_files[] = { 2187 { 2188 .name = "weight", 2189 .flags = CFTYPE_NOT_ON_ROOT, 2190 .seq_show = cfq_print_weight_on_dfl, 2191 .write = cfq_set_weight_on_dfl, 2192 }, 2193 { } /* terminate */ 2194}; 2195 2196#else /* GROUP_IOSCHED */ 2197static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd, 2198 struct blkcg *blkcg) 2199{ 2200 return cfqd->root_group; 2201} 2202 2203static inline void 2204cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) { 2205 cfqq->cfqg = cfqg; 2206} 2207 2208#endif /* GROUP_IOSCHED */ 2209 2210/* 2211 * The cfqd->service_trees holds all pending cfq_queue's that have 2212 * requests waiting to be processed. It is sorted in the order that 2213 * we will service the queues. 2214 */ 2215static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq, 2216 bool add_front) 2217{ 2218 struct rb_node **p, *parent; 2219 struct cfq_queue *__cfqq; 2220 u64 rb_key; 2221 struct cfq_rb_root *st; 2222 int left; 2223 int new_cfqq = 1; 2224 u64 now = ktime_get_ns(); 2225 2226 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq)); 2227 if (cfq_class_idle(cfqq)) { 2228 rb_key = CFQ_IDLE_DELAY; 2229 parent = rb_last(&st->rb); 2230 if (parent && parent != &cfqq->rb_node) { 2231 __cfqq = rb_entry(parent, struct cfq_queue, rb_node); 2232 rb_key += __cfqq->rb_key; 2233 } else 2234 rb_key += now; 2235 } else if (!add_front) { 2236 /* 2237 * Get our rb key offset. Subtract any residual slice 2238 * value carried from last service. A negative resid 2239 * count indicates slice overrun, and this should position 2240 * the next service time further away in the tree. 2241 */ 2242 rb_key = cfq_slice_offset(cfqd, cfqq) + now; 2243 rb_key -= cfqq->slice_resid; 2244 cfqq->slice_resid = 0; 2245 } else { 2246 rb_key = -NSEC_PER_SEC; 2247 __cfqq = cfq_rb_first(st); 2248 rb_key += __cfqq ? __cfqq->rb_key : now; 2249 } 2250 2251 if (!RB_EMPTY_NODE(&cfqq->rb_node)) { 2252 new_cfqq = 0; 2253 /* 2254 * same position, nothing more to do 2255 */ 2256 if (rb_key == cfqq->rb_key && cfqq->service_tree == st) 2257 return; 2258 2259 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); 2260 cfqq->service_tree = NULL; 2261 } 2262 2263 left = 1; 2264 parent = NULL; 2265 cfqq->service_tree = st; 2266 p = &st->rb.rb_node; 2267 while (*p) { 2268 parent = *p; 2269 __cfqq = rb_entry(parent, struct cfq_queue, rb_node); 2270 2271 /* 2272 * sort by key, that represents service time. 2273 */ 2274 if (rb_key < __cfqq->rb_key) 2275 p = &parent->rb_left; 2276 else { 2277 p = &parent->rb_right; 2278 left = 0; 2279 } 2280 } 2281 2282 if (left) 2283 st->left = &cfqq->rb_node; 2284 2285 cfqq->rb_key = rb_key; 2286 rb_link_node(&cfqq->rb_node, parent, p); 2287 rb_insert_color(&cfqq->rb_node, &st->rb); 2288 st->count++; 2289 if (add_front || !new_cfqq) 2290 return; 2291 cfq_group_notify_queue_add(cfqd, cfqq->cfqg); 2292} 2293 2294static struct cfq_queue * 2295cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root, 2296 sector_t sector, struct rb_node **ret_parent, 2297 struct rb_node ***rb_link) 2298{ 2299 struct rb_node **p, *parent; 2300 struct cfq_queue *cfqq = NULL; 2301 2302 parent = NULL; 2303 p = &root->rb_node; 2304 while (*p) { 2305 struct rb_node **n; 2306 2307 parent = *p; 2308 cfqq = rb_entry(parent, struct cfq_queue, p_node); 2309 2310 /* 2311 * Sort strictly based on sector. Smallest to the left, 2312 * largest to the right. 2313 */ 2314 if (sector > blk_rq_pos(cfqq->next_rq)) 2315 n = &(*p)->rb_right; 2316 else if (sector < blk_rq_pos(cfqq->next_rq)) 2317 n = &(*p)->rb_left; 2318 else 2319 break; 2320 p = n; 2321 cfqq = NULL; 2322 } 2323 2324 *ret_parent = parent; 2325 if (rb_link) 2326 *rb_link = p; 2327 return cfqq; 2328} 2329 2330static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2331{ 2332 struct rb_node **p, *parent; 2333 struct cfq_queue *__cfqq; 2334 2335 if (cfqq->p_root) { 2336 rb_erase(&cfqq->p_node, cfqq->p_root); 2337 cfqq->p_root = NULL; 2338 } 2339 2340 if (cfq_class_idle(cfqq)) 2341 return; 2342 if (!cfqq->next_rq) 2343 return; 2344 2345 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio]; 2346 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root, 2347 blk_rq_pos(cfqq->next_rq), &parent, &p); 2348 if (!__cfqq) { 2349 rb_link_node(&cfqq->p_node, parent, p); 2350 rb_insert_color(&cfqq->p_node, cfqq->p_root); 2351 } else 2352 cfqq->p_root = NULL; 2353} 2354 2355/* 2356 * Update cfqq's position in the service tree. 2357 */ 2358static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2359{ 2360 /* 2361 * Resorting requires the cfqq to be on the RR list already. 2362 */ 2363 if (cfq_cfqq_on_rr(cfqq)) { 2364 cfq_service_tree_add(cfqd, cfqq, 0); 2365 cfq_prio_tree_add(cfqd, cfqq); 2366 } 2367} 2368 2369/* 2370 * add to busy list of queues for service, trying to be fair in ordering 2371 * the pending list according to last request service 2372 */ 2373static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2374{ 2375 cfq_log_cfqq(cfqd, cfqq, "add_to_rr"); 2376 BUG_ON(cfq_cfqq_on_rr(cfqq)); 2377 cfq_mark_cfqq_on_rr(cfqq); 2378 cfqd->busy_queues++; 2379 if (cfq_cfqq_sync(cfqq)) 2380 cfqd->busy_sync_queues++; 2381 2382 cfq_resort_rr_list(cfqd, cfqq); 2383} 2384 2385/* 2386 * Called when the cfqq no longer has requests pending, remove it from 2387 * the service tree. 2388 */ 2389static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2390{ 2391 cfq_log_cfqq(cfqd, cfqq, "del_from_rr"); 2392 BUG_ON(!cfq_cfqq_on_rr(cfqq)); 2393 cfq_clear_cfqq_on_rr(cfqq); 2394 2395 if (!RB_EMPTY_NODE(&cfqq->rb_node)) { 2396 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); 2397 cfqq->service_tree = NULL; 2398 } 2399 if (cfqq->p_root) { 2400 rb_erase(&cfqq->p_node, cfqq->p_root); 2401 cfqq->p_root = NULL; 2402 } 2403 2404 cfq_group_notify_queue_del(cfqd, cfqq->cfqg); 2405 BUG_ON(!cfqd->busy_queues); 2406 cfqd->busy_queues--; 2407 if (cfq_cfqq_sync(cfqq)) 2408 cfqd->busy_sync_queues--; 2409} 2410 2411/* 2412 * rb tree support functions 2413 */ 2414static void cfq_del_rq_rb(struct request *rq) 2415{ 2416 struct cfq_queue *cfqq = RQ_CFQQ(rq); 2417 const int sync = rq_is_sync(rq); 2418 2419 BUG_ON(!cfqq->queued[sync]); 2420 cfqq->queued[sync]--; 2421 2422 elv_rb_del(&cfqq->sort_list, rq); 2423 2424 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) { 2425 /* 2426 * Queue will be deleted from service tree when we actually 2427 * expire it later. Right now just remove it from prio tree 2428 * as it is empty. 2429 */ 2430 if (cfqq->p_root) { 2431 rb_erase(&cfqq->p_node, cfqq->p_root); 2432 cfqq->p_root = NULL; 2433 } 2434 } 2435} 2436 2437static void cfq_add_rq_rb(struct request *rq) 2438{ 2439 struct cfq_queue *cfqq = RQ_CFQQ(rq); 2440 struct cfq_data *cfqd = cfqq->cfqd; 2441 struct request *prev; 2442 2443 cfqq->queued[rq_is_sync(rq)]++; 2444 2445 elv_rb_add(&cfqq->sort_list, rq); 2446 2447 if (!cfq_cfqq_on_rr(cfqq)) 2448 cfq_add_cfqq_rr(cfqd, cfqq); 2449 2450 /* 2451 * check if this request is a better next-serve candidate 2452 */ 2453 prev = cfqq->next_rq; 2454 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position); 2455 2456 /* 2457 * adjust priority tree position, if ->next_rq changes 2458 */ 2459 if (prev != cfqq->next_rq) 2460 cfq_prio_tree_add(cfqd, cfqq); 2461 2462 BUG_ON(!cfqq->next_rq); 2463} 2464 2465static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) 2466{ 2467 elv_rb_del(&cfqq->sort_list, rq); 2468 cfqq->queued[rq_is_sync(rq)]--; 2469 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); 2470 cfq_add_rq_rb(rq); 2471 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group, 2472 rq->cmd_flags); 2473} 2474 2475static struct request * 2476cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) 2477{ 2478 struct task_struct *tsk = current; 2479 struct cfq_io_cq *cic; 2480 struct cfq_queue *cfqq; 2481 2482 cic = cfq_cic_lookup(cfqd, tsk->io_context); 2483 if (!cic) 2484 return NULL; 2485 2486 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf)); 2487 if (cfqq) 2488 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio)); 2489 2490 return NULL; 2491} 2492 2493static void cfq_activate_request(struct request_queue *q, struct request *rq) 2494{ 2495 struct cfq_data *cfqd = q->elevator->elevator_data; 2496 2497 cfqd->rq_in_driver++; 2498 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", 2499 cfqd->rq_in_driver); 2500 2501 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); 2502} 2503 2504static void cfq_deactivate_request(struct request_queue *q, struct request *rq) 2505{ 2506 struct cfq_data *cfqd = q->elevator->elevator_data; 2507 2508 WARN_ON(!cfqd->rq_in_driver); 2509 cfqd->rq_in_driver--; 2510 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", 2511 cfqd->rq_in_driver); 2512} 2513 2514static void cfq_remove_request(struct request *rq) 2515{ 2516 struct cfq_queue *cfqq = RQ_CFQQ(rq); 2517 2518 if (cfqq->next_rq == rq) 2519 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); 2520 2521 list_del_init(&rq->queuelist); 2522 cfq_del_rq_rb(rq); 2523 2524 cfqq->cfqd->rq_queued--; 2525 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); 2526 if (rq->cmd_flags & REQ_PRIO) { 2527 WARN_ON(!cfqq->prio_pending); 2528 cfqq->prio_pending--; 2529 } 2530} 2531 2532static enum elv_merge cfq_merge(struct request_queue *q, struct request **req, 2533 struct bio *bio) 2534{ 2535 struct cfq_data *cfqd = q->elevator->elevator_data; 2536 struct request *__rq; 2537 2538 __rq = cfq_find_rq_fmerge(cfqd, bio); 2539 if (__rq && elv_bio_merge_ok(__rq, bio)) { 2540 *req = __rq; 2541 return ELEVATOR_FRONT_MERGE; 2542 } 2543 2544 return ELEVATOR_NO_MERGE; 2545} 2546 2547static void cfq_merged_request(struct request_queue *q, struct request *req, 2548 enum elv_merge type) 2549{ 2550 if (type == ELEVATOR_FRONT_MERGE) { 2551 struct cfq_queue *cfqq = RQ_CFQQ(req); 2552 2553 cfq_reposition_rq_rb(cfqq, req); 2554 } 2555} 2556 2557static void cfq_bio_merged(struct request_queue *q, struct request *req, 2558 struct bio *bio) 2559{ 2560 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf); 2561} 2562 2563static void 2564cfq_merged_requests(struct request_queue *q, struct request *rq, 2565 struct request *next) 2566{ 2567 struct cfq_queue *cfqq = RQ_CFQQ(rq); 2568 struct cfq_data *cfqd = q->elevator->elevator_data; 2569 2570 /* 2571 * reposition in fifo if next is older than rq 2572 */ 2573 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && 2574 next->fifo_time < rq->fifo_time && 2575 cfqq == RQ_CFQQ(next)) { 2576 list_move(&rq->queuelist, &next->queuelist); 2577 rq->fifo_time = next->fifo_time; 2578 } 2579 2580 if (cfqq->next_rq == next) 2581 cfqq->next_rq = rq; 2582 cfq_remove_request(next); 2583 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags); 2584 2585 cfqq = RQ_CFQQ(next); 2586 /* 2587 * all requests of this queue are merged to other queues, delete it 2588 * from the service tree. If it's the active_queue, 2589 * cfq_dispatch_requests() will choose to expire it or do idle 2590 */ 2591 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) && 2592 cfqq != cfqd->active_queue) 2593 cfq_del_cfqq_rr(cfqd, cfqq); 2594} 2595 2596static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq, 2597 struct bio *bio) 2598{ 2599 struct cfq_data *cfqd = q->elevator->elevator_data; 2600 bool is_sync = op_is_sync(bio->bi_opf); 2601 struct cfq_io_cq *cic; 2602 struct cfq_queue *cfqq; 2603 2604 /* 2605 * Disallow merge of a sync bio into an async request. 2606 */ 2607 if (is_sync && !rq_is_sync(rq)) 2608 return false; 2609 2610 /* 2611 * Lookup the cfqq that this bio will be queued with and allow 2612 * merge only if rq is queued there. 2613 */ 2614 cic = cfq_cic_lookup(cfqd, current->io_context); 2615 if (!cic) 2616 return false; 2617 2618 cfqq = cic_to_cfqq(cic, is_sync); 2619 return cfqq == RQ_CFQQ(rq); 2620} 2621 2622static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq, 2623 struct request *next) 2624{ 2625 return RQ_CFQQ(rq) == RQ_CFQQ(next); 2626} 2627 2628static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2629{ 2630 hrtimer_try_to_cancel(&cfqd->idle_slice_timer); 2631 cfqg_stats_update_idle_time(cfqq->cfqg); 2632} 2633 2634static void __cfq_set_active_queue(struct cfq_data *cfqd, 2635 struct cfq_queue *cfqq) 2636{ 2637 if (cfqq) { 2638 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d", 2639 cfqd->serving_wl_class, cfqd->serving_wl_type); 2640 cfqg_stats_update_avg_queue_size(cfqq->cfqg); 2641 cfqq->slice_start = 0; 2642 cfqq->dispatch_start = ktime_get_ns(); 2643 cfqq->allocated_slice = 0; 2644 cfqq->slice_end = 0; 2645 cfqq->slice_dispatch = 0; 2646 cfqq->nr_sectors = 0; 2647 2648 cfq_clear_cfqq_wait_request(cfqq); 2649 cfq_clear_cfqq_must_dispatch(cfqq); 2650 cfq_clear_cfqq_must_alloc_slice(cfqq); 2651 cfq_clear_cfqq_fifo_expire(cfqq); 2652 cfq_mark_cfqq_slice_new(cfqq); 2653 2654 cfq_del_timer(cfqd, cfqq); 2655 } 2656 2657 cfqd->active_queue = cfqq; 2658} 2659 2660/* 2661 * current cfqq expired its slice (or was too idle), select new one 2662 */ 2663static void 2664__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, 2665 bool timed_out) 2666{ 2667 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out); 2668 2669 if (cfq_cfqq_wait_request(cfqq)) 2670 cfq_del_timer(cfqd, cfqq); 2671 2672 cfq_clear_cfqq_wait_request(cfqq); 2673 cfq_clear_cfqq_wait_busy(cfqq); 2674 2675 /* 2676 * If this cfqq is shared between multiple processes, check to 2677 * make sure that those processes are still issuing I/Os within 2678 * the mean seek distance. If not, it may be time to break the 2679 * queues apart again. 2680 */ 2681 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq)) 2682 cfq_mark_cfqq_split_coop(cfqq); 2683 2684 /* 2685 * store what was left of this slice, if the queue idled/timed out 2686 */ 2687 if (timed_out) { 2688 if (cfq_cfqq_slice_new(cfqq)) 2689 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq); 2690 else 2691 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns(); 2692 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid); 2693 } 2694 2695 cfq_group_served(cfqd, cfqq->cfqg, cfqq); 2696 2697 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) 2698 cfq_del_cfqq_rr(cfqd, cfqq); 2699 2700 cfq_resort_rr_list(cfqd, cfqq); 2701 2702 if (cfqq == cfqd->active_queue) 2703 cfqd->active_queue = NULL; 2704 2705 if (cfqd->active_cic) { 2706 put_io_context(cfqd->active_cic->icq.ioc); 2707 cfqd->active_cic = NULL; 2708 } 2709} 2710 2711static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out) 2712{ 2713 struct cfq_queue *cfqq = cfqd->active_queue; 2714 2715 if (cfqq) 2716 __cfq_slice_expired(cfqd, cfqq, timed_out); 2717} 2718 2719/* 2720 * Get next queue for service. Unless we have a queue preemption, 2721 * we'll simply select the first cfqq in the service tree. 2722 */ 2723static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) 2724{ 2725 struct cfq_rb_root *st = st_for(cfqd->serving_group, 2726 cfqd->serving_wl_class, cfqd->serving_wl_type); 2727 2728 if (!cfqd->rq_queued) 2729 return NULL; 2730 2731 /* There is nothing to dispatch */ 2732 if (!st) 2733 return NULL; 2734 if (RB_EMPTY_ROOT(&st->rb)) 2735 return NULL; 2736 return cfq_rb_first(st); 2737} 2738 2739static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd) 2740{ 2741 struct cfq_group *cfqg; 2742 struct cfq_queue *cfqq; 2743 int i, j; 2744 struct cfq_rb_root *st; 2745 2746 if (!cfqd->rq_queued) 2747 return NULL; 2748 2749 cfqg = cfq_get_next_cfqg(cfqd); 2750 if (!cfqg) 2751 return NULL; 2752 2753 for_each_cfqg_st(cfqg, i, j, st) { 2754 cfqq = cfq_rb_first(st); 2755 if (cfqq) 2756 return cfqq; 2757 } 2758 return NULL; 2759} 2760 2761/* 2762 * Get and set a new active queue for service. 2763 */ 2764static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, 2765 struct cfq_queue *cfqq) 2766{ 2767 if (!cfqq) 2768 cfqq = cfq_get_next_queue(cfqd); 2769 2770 __cfq_set_active_queue(cfqd, cfqq); 2771 return cfqq; 2772} 2773 2774static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, 2775 struct request *rq) 2776{ 2777 if (blk_rq_pos(rq) >= cfqd->last_position) 2778 return blk_rq_pos(rq) - cfqd->last_position; 2779 else 2780 return cfqd->last_position - blk_rq_pos(rq); 2781} 2782 2783static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq, 2784 struct request *rq) 2785{ 2786 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR; 2787} 2788 2789static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, 2790 struct cfq_queue *cur_cfqq) 2791{ 2792 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio]; 2793 struct rb_node *parent, *node; 2794 struct cfq_queue *__cfqq; 2795 sector_t sector = cfqd->last_position; 2796 2797 if (RB_EMPTY_ROOT(root)) 2798 return NULL; 2799 2800 /* 2801 * First, if we find a request starting at the end of the last 2802 * request, choose it. 2803 */ 2804 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL); 2805 if (__cfqq) 2806 return __cfqq; 2807 2808 /* 2809 * If the exact sector wasn't found, the parent of the NULL leaf 2810 * will contain the closest sector. 2811 */ 2812 __cfqq = rb_entry(parent, struct cfq_queue, p_node); 2813 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) 2814 return __cfqq; 2815 2816 if (blk_rq_pos(__cfqq->next_rq) < sector) 2817 node = rb_next(&__cfqq->p_node); 2818 else 2819 node = rb_prev(&__cfqq->p_node); 2820 if (!node) 2821 return NULL; 2822 2823 __cfqq = rb_entry(node, struct cfq_queue, p_node); 2824 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) 2825 return __cfqq; 2826 2827 return NULL; 2828} 2829 2830/* 2831 * cfqd - obvious 2832 * cur_cfqq - passed in so that we don't decide that the current queue is 2833 * closely cooperating with itself. 2834 * 2835 * So, basically we're assuming that that cur_cfqq has dispatched at least 2836 * one request, and that cfqd->last_position reflects a position on the disk 2837 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid 2838 * assumption. 2839 */ 2840static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, 2841 struct cfq_queue *cur_cfqq) 2842{ 2843 struct cfq_queue *cfqq; 2844 2845 if (cfq_class_idle(cur_cfqq)) 2846 return NULL; 2847 if (!cfq_cfqq_sync(cur_cfqq)) 2848 return NULL; 2849 if (CFQQ_SEEKY(cur_cfqq)) 2850 return NULL; 2851 2852 /* 2853 * Don't search priority tree if it's the only queue in the group. 2854 */ 2855 if (cur_cfqq->cfqg->nr_cfqq == 1) 2856 return NULL; 2857 2858 /* 2859 * We should notice if some of the queues are cooperating, eg 2860 * working closely on the same area of the disk. In that case, 2861 * we can group them together and don't waste time idling. 2862 */ 2863 cfqq = cfqq_close(cfqd, cur_cfqq); 2864 if (!cfqq) 2865 return NULL; 2866 2867 /* If new queue belongs to different cfq_group, don't choose it */ 2868 if (cur_cfqq->cfqg != cfqq->cfqg) 2869 return NULL; 2870 2871 /* 2872 * It only makes sense to merge sync queues. 2873 */ 2874 if (!cfq_cfqq_sync(cfqq)) 2875 return NULL; 2876 if (CFQQ_SEEKY(cfqq)) 2877 return NULL; 2878 2879 /* 2880 * Do not merge queues of different priority classes 2881 */ 2882 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq)) 2883 return NULL; 2884 2885 return cfqq; 2886} 2887 2888/* 2889 * Determine whether we should enforce idle window for this queue. 2890 */ 2891 2892static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq) 2893{ 2894 enum wl_class_t wl_class = cfqq_class(cfqq); 2895 struct cfq_rb_root *st = cfqq->service_tree; 2896 2897 BUG_ON(!st); 2898 BUG_ON(!st->count); 2899 2900 if (!cfqd->cfq_slice_idle) 2901 return false; 2902 2903 /* We never do for idle class queues. */ 2904 if (wl_class == IDLE_WORKLOAD) 2905 return false; 2906 2907 /* We do for queues that were marked with idle window flag. */ 2908 if (cfq_cfqq_idle_window(cfqq) && 2909 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)) 2910 return true; 2911 2912 /* 2913 * Otherwise, we do only if they are the last ones 2914 * in their service tree. 2915 */ 2916 if (st->count == 1 && cfq_cfqq_sync(cfqq) && 2917 !cfq_io_thinktime_big(cfqd, &st->ttime, false)) 2918 return true; 2919 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count); 2920 return false; 2921} 2922 2923static void cfq_arm_slice_timer(struct cfq_data *cfqd) 2924{ 2925 struct cfq_queue *cfqq = cfqd->active_queue; 2926 struct cfq_rb_root *st = cfqq->service_tree; 2927 struct cfq_io_cq *cic; 2928 u64 sl, group_idle = 0; 2929 u64 now = ktime_get_ns(); 2930 2931 /* 2932 * SSD device without seek penalty, disable idling. But only do so 2933 * for devices that support queuing, otherwise we still have a problem 2934 * with sync vs async workloads. 2935 */ 2936 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag) 2937 return; 2938 2939 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); 2940 WARN_ON(cfq_cfqq_slice_new(cfqq)); 2941 2942 /* 2943 * idle is disabled, either manually or by past process history 2944 */ 2945 if (!cfq_should_idle(cfqd, cfqq)) { 2946 /* no queue idling. Check for group idling */ 2947 if (cfqd->cfq_group_idle) 2948 group_idle = cfqd->cfq_group_idle; 2949 else 2950 return; 2951 } 2952 2953 /* 2954 * still active requests from this queue, don't idle 2955 */ 2956 if (cfqq->dispatched) 2957 return; 2958 2959 /* 2960 * task has exited, don't wait 2961 */ 2962 cic = cfqd->active_cic; 2963 if (!cic || !atomic_read(&cic->icq.ioc->active_ref)) 2964 return; 2965 2966 /* 2967 * If our average think time is larger than the remaining time 2968 * slice, then don't idle. This avoids overrunning the allotted 2969 * time slice. 2970 */ 2971 if (sample_valid(cic->ttime.ttime_samples) && 2972 (cfqq->slice_end - now < cic->ttime.ttime_mean)) { 2973 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu", 2974 cic->ttime.ttime_mean); 2975 return; 2976 } 2977 2978 /* 2979 * There are other queues in the group or this is the only group and 2980 * it has too big thinktime, don't do group idle. 2981 */ 2982 if (group_idle && 2983 (cfqq->cfqg->nr_cfqq > 1 || 2984 cfq_io_thinktime_big(cfqd, &st->ttime, true))) 2985 return; 2986 2987 cfq_mark_cfqq_wait_request(cfqq); 2988 2989 if (group_idle) 2990 sl = cfqd->cfq_group_idle; 2991 else 2992 sl = cfqd->cfq_slice_idle; 2993 2994 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl), 2995 HRTIMER_MODE_REL); 2996 cfqg_stats_set_start_idle_time(cfqq->cfqg); 2997 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl, 2998 group_idle ? 1 : 0); 2999} 3000 3001/* 3002 * Move request from internal lists to the request queue dispatch list. 3003 */ 3004static void cfq_dispatch_insert(struct request_queue *q, struct request *rq) 3005{ 3006 struct cfq_data *cfqd = q->elevator->elevator_data; 3007 struct cfq_queue *cfqq = RQ_CFQQ(rq); 3008 3009 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert"); 3010 3011 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq); 3012 cfq_remove_request(rq); 3013 cfqq->dispatched++; 3014 (RQ_CFQG(rq))->dispatched++; 3015 elv_dispatch_sort(q, rq); 3016 3017 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++; 3018 cfqq->nr_sectors += blk_rq_sectors(rq); 3019} 3020 3021/* 3022 * return expired entry, or NULL to just start from scratch in rbtree 3023 */ 3024static struct request *cfq_check_fifo(struct cfq_queue *cfqq) 3025{ 3026 struct request *rq = NULL; 3027 3028 if (cfq_cfqq_fifo_expire(cfqq)) 3029 return NULL; 3030 3031 cfq_mark_cfqq_fifo_expire(cfqq); 3032 3033 if (list_empty(&cfqq->fifo)) 3034 return NULL; 3035 3036 rq = rq_entry_fifo(cfqq->fifo.next); 3037 if (ktime_get_ns() < rq->fifo_time) 3038 rq = NULL; 3039 3040 return rq; 3041} 3042 3043static inline int 3044cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) 3045{ 3046 const int base_rq = cfqd->cfq_slice_async_rq; 3047 3048 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); 3049 3050 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio); 3051} 3052 3053/* 3054 * Must be called with the queue_lock held. 3055 */ 3056static int cfqq_process_refs(struct cfq_queue *cfqq) 3057{ 3058 int process_refs, io_refs; 3059 3060 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE]; 3061 process_refs = cfqq->ref - io_refs; 3062 BUG_ON(process_refs < 0); 3063 return process_refs; 3064} 3065 3066static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq) 3067{ 3068 int process_refs, new_process_refs; 3069 struct cfq_queue *__cfqq; 3070 3071 /* 3072 * If there are no process references on the new_cfqq, then it is 3073 * unsafe to follow the ->new_cfqq chain as other cfqq's in the 3074 * chain may have dropped their last reference (not just their 3075 * last process reference). 3076 */ 3077 if (!cfqq_process_refs(new_cfqq)) 3078 return; 3079 3080 /* Avoid a circular list and skip interim queue merges */ 3081 while ((__cfqq = new_cfqq->new_cfqq)) { 3082 if (__cfqq == cfqq) 3083 return; 3084 new_cfqq = __cfqq; 3085 } 3086 3087 process_refs = cfqq_process_refs(cfqq); 3088 new_process_refs = cfqq_process_refs(new_cfqq); 3089 /* 3090 * If the process for the cfqq has gone away, there is no 3091 * sense in merging the queues. 3092 */ 3093 if (process_refs == 0 || new_process_refs == 0) 3094 return; 3095 3096 /* 3097 * Merge in the direction of the lesser amount of work. 3098 */ 3099 if (new_process_refs >= process_refs) { 3100 cfqq->new_cfqq = new_cfqq; 3101 new_cfqq->ref += process_refs; 3102 } else { 3103 new_cfqq->new_cfqq = cfqq; 3104 cfqq->ref += new_process_refs; 3105 } 3106} 3107 3108static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd, 3109 struct cfq_group *cfqg, enum wl_class_t wl_class) 3110{ 3111 struct cfq_queue *queue; 3112 int i; 3113 bool key_valid = false; 3114 u64 lowest_key = 0; 3115 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD; 3116 3117 for (i = 0; i <= SYNC_WORKLOAD; ++i) { 3118 /* select the one with lowest rb_key */ 3119 queue = cfq_rb_first(st_for(cfqg, wl_class, i)); 3120 if (queue && 3121 (!key_valid || queue->rb_key < lowest_key)) { 3122 lowest_key = queue->rb_key; 3123 cur_best = i; 3124 key_valid = true; 3125 } 3126 } 3127 3128 return cur_best; 3129} 3130 3131static void 3132choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg) 3133{ 3134 u64 slice; 3135 unsigned count; 3136 struct cfq_rb_root *st; 3137 u64 group_slice; 3138 enum wl_class_t original_class = cfqd->serving_wl_class; 3139 u64 now = ktime_get_ns(); 3140 3141 /* Choose next priority. RT > BE > IDLE */ 3142 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg)) 3143 cfqd->serving_wl_class = RT_WORKLOAD; 3144 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg)) 3145 cfqd->serving_wl_class = BE_WORKLOAD; 3146 else { 3147 cfqd->serving_wl_class = IDLE_WORKLOAD; 3148 cfqd->workload_expires = now + jiffies_to_nsecs(1); 3149 return; 3150 } 3151 3152 if (original_class != cfqd->serving_wl_class) 3153 goto new_workload; 3154 3155 /* 3156 * For RT and BE, we have to choose also the type 3157 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload 3158 * expiration time 3159 */ 3160 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); 3161 count = st->count; 3162 3163 /* 3164 * check workload expiration, and that we still have other queues ready 3165 */ 3166 if (count && !(now > cfqd->workload_expires)) 3167 return; 3168 3169new_workload: 3170 /* otherwise select new workload type */ 3171 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg, 3172 cfqd->serving_wl_class); 3173 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); 3174 count = st->count; 3175 3176 /* 3177 * the workload slice is computed as a fraction of target latency 3178 * proportional to the number of queues in that workload, over 3179 * all the queues in the same priority class 3180 */ 3181 group_slice = cfq_group_slice(cfqd, cfqg); 3182 3183 slice = div_u64(group_slice * count, 3184 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class], 3185 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd, 3186 cfqg))); 3187 3188 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) { 3189 u64 tmp; 3190 3191 /* 3192 * Async queues are currently system wide. Just taking 3193 * proportion of queues with-in same group will lead to higher 3194 * async ratio system wide as generally root group is going 3195 * to have higher weight. A more accurate thing would be to 3196 * calculate system wide asnc/sync ratio. 3197 */ 3198 tmp = cfqd->cfq_target_latency * 3199 cfqg_busy_async_queues(cfqd, cfqg); 3200 tmp = div_u64(tmp, cfqd->busy_queues); 3201 slice = min_t(u64, slice, tmp); 3202 3203 /* async workload slice is scaled down according to 3204 * the sync/async slice ratio. */ 3205 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]); 3206 } else 3207 /* sync workload slice is at least 2 * cfq_slice_idle */ 3208 slice = max(slice, 2 * cfqd->cfq_slice_idle); 3209 3210 slice = max_t(u64, slice, CFQ_MIN_TT); 3211 cfq_log(cfqd, "workload slice:%llu", slice); 3212 cfqd->workload_expires = now + slice; 3213} 3214 3215static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd) 3216{ 3217 struct cfq_rb_root *st = &cfqd->grp_service_tree; 3218 struct cfq_group *cfqg; 3219 3220 if (RB_EMPTY_ROOT(&st->rb)) 3221 return NULL; 3222 cfqg = cfq_rb_first_group(st); 3223 update_min_vdisktime(st); 3224 return cfqg; 3225} 3226 3227static void cfq_choose_cfqg(struct cfq_data *cfqd) 3228{ 3229 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd); 3230 u64 now = ktime_get_ns(); 3231 3232 cfqd->serving_group = cfqg; 3233 3234 /* Restore the workload type data */ 3235 if (cfqg->saved_wl_slice) { 3236 cfqd->workload_expires = now + cfqg->saved_wl_slice; 3237 cfqd->serving_wl_type = cfqg->saved_wl_type; 3238 cfqd->serving_wl_class = cfqg->saved_wl_class; 3239 } else 3240 cfqd->workload_expires = now - 1; 3241 3242 choose_wl_class_and_type(cfqd, cfqg); 3243} 3244 3245/* 3246 * Select a queue for service. If we have a current active queue, 3247 * check whether to continue servicing it, or retrieve and set a new one. 3248 */ 3249static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) 3250{ 3251 struct cfq_queue *cfqq, *new_cfqq = NULL; 3252 u64 now = ktime_get_ns(); 3253 3254 cfqq = cfqd->active_queue; 3255 if (!cfqq) 3256 goto new_queue; 3257 3258 if (!cfqd->rq_queued) 3259 return NULL; 3260 3261 /* 3262 * We were waiting for group to get backlogged. Expire the queue 3263 */ 3264 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list)) 3265 goto expire; 3266 3267 /* 3268 * The active queue has run out of time, expire it and select new. 3269 */ 3270 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) { 3271 /* 3272 * If slice had not expired at the completion of last request 3273 * we might not have turned on wait_busy flag. Don't expire 3274 * the queue yet. Allow the group to get backlogged. 3275 * 3276 * The very fact that we have used the slice, that means we 3277 * have been idling all along on this queue and it should be 3278 * ok to wait for this request to complete. 3279 */ 3280 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list) 3281 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { 3282 cfqq = NULL; 3283 goto keep_queue; 3284 } else 3285 goto check_group_idle; 3286 } 3287 3288 /* 3289 * The active queue has requests and isn't expired, allow it to 3290 * dispatch. 3291 */ 3292 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) 3293 goto keep_queue; 3294 3295 /* 3296 * If another queue has a request waiting within our mean seek 3297 * distance, let it run. The expire code will check for close 3298 * cooperators and put the close queue at the front of the service 3299 * tree. If possible, merge the expiring queue with the new cfqq. 3300 */ 3301 new_cfqq = cfq_close_cooperator(cfqd, cfqq); 3302 if (new_cfqq) { 3303 if (!cfqq->new_cfqq) 3304 cfq_setup_merge(cfqq, new_cfqq); 3305 goto expire; 3306 } 3307 3308 /* 3309 * No requests pending. If the active queue still has requests in 3310 * flight or is idling for a new request, allow either of these 3311 * conditions to happen (or time out) before selecting a new queue. 3312 */ 3313 if (hrtimer_active(&cfqd->idle_slice_timer)) { 3314 cfqq = NULL; 3315 goto keep_queue; 3316 } 3317 3318 /* 3319 * This is a deep seek queue, but the device is much faster than 3320 * the queue can deliver, don't idle 3321 **/ 3322 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) && 3323 (cfq_cfqq_slice_new(cfqq) || 3324 (cfqq->slice_end - now > now - cfqq->slice_start))) { 3325 cfq_clear_cfqq_deep(cfqq); 3326 cfq_clear_cfqq_idle_window(cfqq); 3327 } 3328 3329 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { 3330 cfqq = NULL; 3331 goto keep_queue; 3332 } 3333 3334 /* 3335 * If group idle is enabled and there are requests dispatched from 3336 * this group, wait for requests to complete. 3337 */ 3338check_group_idle: 3339 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 && 3340 cfqq->cfqg->dispatched && 3341 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) { 3342 cfqq = NULL; 3343 goto keep_queue; 3344 } 3345 3346expire: 3347 cfq_slice_expired(cfqd, 0); 3348new_queue: 3349 /* 3350 * Current queue expired. Check if we have to switch to a new 3351 * service tree 3352 */ 3353 if (!new_cfqq) 3354 cfq_choose_cfqg(cfqd); 3355 3356 cfqq = cfq_set_active_queue(cfqd, new_cfqq); 3357keep_queue: 3358 return cfqq; 3359} 3360 3361static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) 3362{ 3363 int dispatched = 0; 3364 3365 while (cfqq->next_rq) { 3366 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); 3367 dispatched++; 3368 } 3369 3370 BUG_ON(!list_empty(&cfqq->fifo)); 3371 3372 /* By default cfqq is not expired if it is empty. Do it explicitly */ 3373 __cfq_slice_expired(cfqq->cfqd, cfqq, 0); 3374 return dispatched; 3375} 3376 3377/* 3378 * Drain our current requests. Used for barriers and when switching 3379 * io schedulers on-the-fly. 3380 */ 3381static int cfq_forced_dispatch(struct cfq_data *cfqd) 3382{ 3383 struct cfq_queue *cfqq; 3384 int dispatched = 0; 3385 3386 /* Expire the timeslice of the current active queue first */ 3387 cfq_slice_expired(cfqd, 0); 3388 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) { 3389 __cfq_set_active_queue(cfqd, cfqq); 3390 dispatched += __cfq_forced_dispatch_cfqq(cfqq); 3391 } 3392 3393 BUG_ON(cfqd->busy_queues); 3394 3395 cfq_log(cfqd, "forced_dispatch=%d", dispatched); 3396 return dispatched; 3397} 3398 3399static inline bool cfq_slice_used_soon(struct cfq_data *cfqd, 3400 struct cfq_queue *cfqq) 3401{ 3402 u64 now = ktime_get_ns(); 3403 3404 /* the queue hasn't finished any request, can't estimate */ 3405 if (cfq_cfqq_slice_new(cfqq)) 3406 return true; 3407 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end) 3408 return true; 3409 3410 return false; 3411} 3412 3413static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) 3414{ 3415 unsigned int max_dispatch; 3416 3417 if (cfq_cfqq_must_dispatch(cfqq)) 3418 return true; 3419 3420 /* 3421 * Drain async requests before we start sync IO 3422 */ 3423 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC]) 3424 return false; 3425 3426 /* 3427 * If this is an async queue and we have sync IO in flight, let it wait 3428 */ 3429 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq)) 3430 return false; 3431 3432 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1); 3433 if (cfq_class_idle(cfqq)) 3434 max_dispatch = 1; 3435 3436 /* 3437 * Does this cfqq already have too much IO in flight? 3438 */ 3439 if (cfqq->dispatched >= max_dispatch) { 3440 bool promote_sync = false; 3441 /* 3442 * idle queue must always only have a single IO in flight 3443 */ 3444 if (cfq_class_idle(cfqq)) 3445 return false; 3446 3447 /* 3448 * If there is only one sync queue 3449 * we can ignore async queue here and give the sync 3450 * queue no dispatch limit. The reason is a sync queue can 3451 * preempt async queue, limiting the sync queue doesn't make 3452 * sense. This is useful for aiostress test. 3453 */ 3454 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1) 3455 promote_sync = true; 3456 3457 /* 3458 * We have other queues, don't allow more IO from this one 3459 */ 3460 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) && 3461 !promote_sync) 3462 return false; 3463 3464 /* 3465 * Sole queue user, no limit 3466 */ 3467 if (cfqd->busy_queues == 1 || promote_sync) 3468 max_dispatch = -1; 3469 else 3470 /* 3471 * Normally we start throttling cfqq when cfq_quantum/2 3472 * requests have been dispatched. But we can drive 3473 * deeper queue depths at the beginning of slice 3474 * subjected to upper limit of cfq_quantum. 3475 * */ 3476 max_dispatch = cfqd->cfq_quantum; 3477 } 3478 3479 /* 3480 * Async queues must wait a bit before being allowed dispatch. 3481 * We also ramp up the dispatch depth gradually for async IO, 3482 * based on the last sync IO we serviced 3483 */ 3484 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { 3485 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync; 3486 unsigned int depth; 3487 3488 depth = div64_u64(last_sync, cfqd->cfq_slice[1]); 3489 if (!depth && !cfqq->dispatched) 3490 depth = 1; 3491 if (depth < max_dispatch) 3492 max_dispatch = depth; 3493 } 3494 3495 /* 3496 * If we're below the current max, allow a dispatch 3497 */ 3498 return cfqq->dispatched < max_dispatch; 3499} 3500 3501/* 3502 * Dispatch a request from cfqq, moving them to the request queue 3503 * dispatch list. 3504 */ 3505static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq) 3506{ 3507 struct request *rq; 3508 3509 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); 3510 3511 rq = cfq_check_fifo(cfqq); 3512 if (rq) 3513 cfq_mark_cfqq_must_dispatch(cfqq); 3514 3515 if (!cfq_may_dispatch(cfqd, cfqq)) 3516 return false; 3517 3518 /* 3519 * follow expired path, else get first next available 3520 */ 3521 if (!rq) 3522 rq = cfqq->next_rq; 3523 else 3524 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq); 3525 3526 /* 3527 * insert request into driver dispatch list 3528 */ 3529 cfq_dispatch_insert(cfqd->queue, rq); 3530 3531 if (!cfqd->active_cic) { 3532 struct cfq_io_cq *cic = RQ_CIC(rq); 3533 3534 atomic_long_inc(&cic->icq.ioc->refcount); 3535 cfqd->active_cic = cic; 3536 } 3537 3538 return true; 3539} 3540 3541/* 3542 * Find the cfqq that we need to service and move a request from that to the 3543 * dispatch list 3544 */ 3545static int cfq_dispatch_requests(struct request_queue *q, int force) 3546{ 3547 struct cfq_data *cfqd = q->elevator->elevator_data; 3548 struct cfq_queue *cfqq; 3549 3550 if (!cfqd->busy_queues) 3551 return 0; 3552 3553 if (unlikely(force)) 3554 return cfq_forced_dispatch(cfqd); 3555 3556 cfqq = cfq_select_queue(cfqd); 3557 if (!cfqq) 3558 return 0; 3559 3560 /* 3561 * Dispatch a request from this cfqq, if it is allowed 3562 */ 3563 if (!cfq_dispatch_request(cfqd, cfqq)) 3564 return 0; 3565 3566 cfqq->slice_dispatch++; 3567 cfq_clear_cfqq_must_dispatch(cfqq); 3568 3569 /* 3570 * expire an async queue immediately if it has used up its slice. idle 3571 * queue always expire after 1 dispatch round. 3572 */ 3573 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && 3574 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) || 3575 cfq_class_idle(cfqq))) { 3576 cfqq->slice_end = ktime_get_ns() + 1; 3577 cfq_slice_expired(cfqd, 0); 3578 } 3579 3580 cfq_log_cfqq(cfqd, cfqq, "dispatched a request"); 3581 return 1; 3582} 3583 3584/* 3585 * task holds one reference to the queue, dropped when task exits. each rq 3586 * in-flight on this queue also holds a reference, dropped when rq is freed. 3587 * 3588 * Each cfq queue took a reference on the parent group. Drop it now. 3589 * queue lock must be held here. 3590 */ 3591static void cfq_put_queue(struct cfq_queue *cfqq) 3592{ 3593 struct cfq_data *cfqd = cfqq->cfqd; 3594 struct cfq_group *cfqg; 3595 3596 BUG_ON(cfqq->ref <= 0); 3597 3598 cfqq->ref--; 3599 if (cfqq->ref) 3600 return; 3601 3602 cfq_log_cfqq(cfqd, cfqq, "put_queue"); 3603 BUG_ON(rb_first(&cfqq->sort_list)); 3604 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); 3605 cfqg = cfqq->cfqg; 3606 3607 if (unlikely(cfqd->active_queue == cfqq)) { 3608 __cfq_slice_expired(cfqd, cfqq, 0); 3609 cfq_schedule_dispatch(cfqd); 3610 } 3611 3612 BUG_ON(cfq_cfqq_on_rr(cfqq)); 3613 kmem_cache_free(cfq_pool, cfqq); 3614 cfqg_put(cfqg); 3615} 3616 3617static void cfq_put_cooperator(struct cfq_queue *cfqq) 3618{ 3619 struct cfq_queue *__cfqq, *next; 3620 3621 /* 3622 * If this queue was scheduled to merge with another queue, be 3623 * sure to drop the reference taken on that queue (and others in 3624 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs. 3625 */ 3626 __cfqq = cfqq->new_cfqq; 3627 while (__cfqq) { 3628 if (__cfqq == cfqq) { 3629 WARN(1, "cfqq->new_cfqq loop detected\n"); 3630 break; 3631 } 3632 next = __cfqq->new_cfqq; 3633 cfq_put_queue(__cfqq); 3634 __cfqq = next; 3635 } 3636} 3637 3638static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) 3639{ 3640 if (unlikely(cfqq == cfqd->active_queue)) { 3641 __cfq_slice_expired(cfqd, cfqq, 0); 3642 cfq_schedule_dispatch(cfqd); 3643 } 3644 3645 cfq_put_cooperator(cfqq); 3646 3647 cfq_put_queue(cfqq); 3648} 3649 3650static void cfq_init_icq(struct io_cq *icq) 3651{ 3652 struct cfq_io_cq *cic = icq_to_cic(icq); 3653 3654 cic->ttime.last_end_request = ktime_get_ns(); 3655} 3656 3657static void cfq_exit_icq(struct io_cq *icq) 3658{ 3659 struct cfq_io_cq *cic = icq_to_cic(icq); 3660 struct cfq_data *cfqd = cic_to_cfqd(cic); 3661 3662 if (cic_to_cfqq(cic, false)) { 3663 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false)); 3664 cic_set_cfqq(cic, NULL, false); 3665 } 3666 3667 if (cic_to_cfqq(cic, true)) { 3668 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true)); 3669 cic_set_cfqq(cic, NULL, true); 3670 } 3671} 3672 3673static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic) 3674{ 3675 struct task_struct *tsk = current; 3676 int ioprio_class; 3677 3678 if (!cfq_cfqq_prio_changed(cfqq)) 3679 return; 3680 3681 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); 3682 switch (ioprio_class) { 3683 default: 3684 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); 3685 case IOPRIO_CLASS_NONE: 3686 /* 3687 * no prio set, inherit CPU scheduling settings 3688 */ 3689 cfqq->ioprio = task_nice_ioprio(tsk); 3690 cfqq->ioprio_class = task_nice_ioclass(tsk); 3691 break; 3692 case IOPRIO_CLASS_RT: 3693 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); 3694 cfqq->ioprio_class = IOPRIO_CLASS_RT; 3695 break; 3696 case IOPRIO_CLASS_BE: 3697 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); 3698 cfqq->ioprio_class = IOPRIO_CLASS_BE; 3699 break; 3700 case IOPRIO_CLASS_IDLE: 3701 cfqq->ioprio_class = IOPRIO_CLASS_IDLE; 3702 cfqq->ioprio = 7; 3703 cfq_clear_cfqq_idle_window(cfqq); 3704 break; 3705 } 3706 3707 /* 3708 * keep track of original prio settings in case we have to temporarily 3709 * elevate the priority of this queue 3710 */ 3711 cfqq->org_ioprio = cfqq->ioprio; 3712 cfqq->org_ioprio_class = cfqq->ioprio_class; 3713 cfq_clear_cfqq_prio_changed(cfqq); 3714} 3715 3716static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio) 3717{ 3718 int ioprio = cic->icq.ioc->ioprio; 3719 struct cfq_data *cfqd = cic_to_cfqd(cic); 3720 struct cfq_queue *cfqq; 3721 3722 /* 3723 * Check whether ioprio has changed. The condition may trigger 3724 * spuriously on a newly created cic but there's no harm. 3725 */ 3726 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio)) 3727 return; 3728 3729 cfqq = cic_to_cfqq(cic, false); 3730 if (cfqq) { 3731 cfq_put_queue(cfqq); 3732 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio); 3733 cic_set_cfqq(cic, cfqq, false); 3734 } 3735 3736 cfqq = cic_to_cfqq(cic, true); 3737 if (cfqq) 3738 cfq_mark_cfqq_prio_changed(cfqq); 3739 3740 cic->ioprio = ioprio; 3741} 3742 3743static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq, 3744 pid_t pid, bool is_sync) 3745{ 3746 RB_CLEAR_NODE(&cfqq->rb_node); 3747 RB_CLEAR_NODE(&cfqq->p_node); 3748 INIT_LIST_HEAD(&cfqq->fifo); 3749 3750 cfqq->ref = 0; 3751 cfqq->cfqd = cfqd; 3752 3753 cfq_mark_cfqq_prio_changed(cfqq); 3754 3755 if (is_sync) { 3756 if (!cfq_class_idle(cfqq)) 3757 cfq_mark_cfqq_idle_window(cfqq); 3758 cfq_mark_cfqq_sync(cfqq); 3759 } 3760 cfqq->pid = pid; 3761} 3762 3763#ifdef CONFIG_CFQ_GROUP_IOSCHED 3764static bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) 3765{ 3766 struct cfq_data *cfqd = cic_to_cfqd(cic); 3767 struct cfq_queue *cfqq; 3768 uint64_t serial_nr; 3769 bool nonroot_cg; 3770 3771 rcu_read_lock(); 3772 serial_nr = bio_blkcg(bio)->css.serial_nr; 3773 nonroot_cg = bio_blkcg(bio) != &blkcg_root; 3774 rcu_read_unlock(); 3775 3776 /* 3777 * Check whether blkcg has changed. The condition may trigger 3778 * spuriously on a newly created cic but there's no harm. 3779 */ 3780 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr)) 3781 return nonroot_cg; 3782 3783 /* 3784 * Drop reference to queues. New queues will be assigned in new 3785 * group upon arrival of fresh requests. 3786 */ 3787 cfqq = cic_to_cfqq(cic, false); 3788 if (cfqq) { 3789 cfq_log_cfqq(cfqd, cfqq, "changed cgroup"); 3790 cic_set_cfqq(cic, NULL, false); 3791 cfq_put_queue(cfqq); 3792 } 3793 3794 cfqq = cic_to_cfqq(cic, true); 3795 if (cfqq) { 3796 cfq_log_cfqq(cfqd, cfqq, "changed cgroup"); 3797 cic_set_cfqq(cic, NULL, true); 3798 cfq_put_queue(cfqq); 3799 } 3800 3801 cic->blkcg_serial_nr = serial_nr; 3802 return nonroot_cg; 3803} 3804#else 3805static inline bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) 3806{ 3807 return false; 3808} 3809#endif /* CONFIG_CFQ_GROUP_IOSCHED */ 3810 3811static struct cfq_queue ** 3812cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio) 3813{ 3814 switch (ioprio_class) { 3815 case IOPRIO_CLASS_RT: 3816 return &cfqg->async_cfqq[0][ioprio]; 3817 case IOPRIO_CLASS_NONE: 3818 ioprio = IOPRIO_NORM; 3819 /* fall through */ 3820 case IOPRIO_CLASS_BE: 3821 return &cfqg->async_cfqq[1][ioprio]; 3822 case IOPRIO_CLASS_IDLE: 3823 return &cfqg->async_idle_cfqq; 3824 default: 3825 BUG(); 3826 } 3827} 3828 3829static struct cfq_queue * 3830cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic, 3831 struct bio *bio) 3832{ 3833 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); 3834 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio); 3835 struct cfq_queue **async_cfqq = NULL; 3836 struct cfq_queue *cfqq; 3837 struct cfq_group *cfqg; 3838 3839 rcu_read_lock(); 3840 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio)); 3841 if (!cfqg) { 3842 cfqq = &cfqd->oom_cfqq; 3843 goto out; 3844 } 3845 3846 if (!is_sync) { 3847 if (!ioprio_valid(cic->ioprio)) { 3848 struct task_struct *tsk = current; 3849 ioprio = task_nice_ioprio(tsk); 3850 ioprio_class = task_nice_ioclass(tsk); 3851 } 3852 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio); 3853 cfqq = *async_cfqq; 3854 if (cfqq) 3855 goto out; 3856 } 3857 3858 cfqq = kmem_cache_alloc_node(cfq_pool, 3859 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN, 3860 cfqd->queue->node); 3861 if (!cfqq) { 3862 cfqq = &cfqd->oom_cfqq; 3863 goto out; 3864 } 3865 3866 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */ 3867 cfqq->ioprio_class = IOPRIO_CLASS_NONE; 3868 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); 3869 cfq_init_prio_data(cfqq, cic); 3870 cfq_link_cfqq_cfqg(cfqq, cfqg); 3871 cfq_log_cfqq(cfqd, cfqq, "alloced"); 3872 3873 if (async_cfqq) { 3874 /* a new async queue is created, pin and remember */ 3875 cfqq->ref++; 3876 *async_cfqq = cfqq; 3877 } 3878out: 3879 cfqq->ref++; 3880 rcu_read_unlock(); 3881 return cfqq; 3882} 3883 3884static void 3885__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle) 3886{ 3887 u64 elapsed = ktime_get_ns() - ttime->last_end_request; 3888 elapsed = min(elapsed, 2UL * slice_idle); 3889 3890 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; 3891 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8); 3892 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128, 3893 ttime->ttime_samples); 3894} 3895 3896static void 3897cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq, 3898 struct cfq_io_cq *cic) 3899{ 3900 if (cfq_cfqq_sync(cfqq)) { 3901 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle); 3902 __cfq_update_io_thinktime(&cfqq->service_tree->ttime, 3903 cfqd->cfq_slice_idle); 3904 } 3905#ifdef CONFIG_CFQ_GROUP_IOSCHED 3906 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle); 3907#endif 3908} 3909 3910static void 3911cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq, 3912 struct request *rq) 3913{ 3914 sector_t sdist = 0; 3915 sector_t n_sec = blk_rq_sectors(rq); 3916 if (cfqq->last_request_pos) { 3917 if (cfqq->last_request_pos < blk_rq_pos(rq)) 3918 sdist = blk_rq_pos(rq) - cfqq->last_request_pos; 3919 else 3920 sdist = cfqq->last_request_pos - blk_rq_pos(rq); 3921 } 3922 3923 cfqq->seek_history <<= 1; 3924 if (blk_queue_nonrot(cfqd->queue)) 3925 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT); 3926 else 3927 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR); 3928} 3929 3930static inline bool req_noidle(struct request *req) 3931{ 3932 return req_op(req) == REQ_OP_WRITE && 3933 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC; 3934} 3935 3936/* 3937 * Disable idle window if the process thinks too long or seeks so much that 3938 * it doesn't matter 3939 */ 3940static void 3941cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, 3942 struct cfq_io_cq *cic) 3943{ 3944 int old_idle, enable_idle; 3945 3946 /* 3947 * Don't idle for async or idle io prio class 3948 */ 3949 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq)) 3950 return; 3951 3952 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); 3953 3954 if (cfqq->queued[0] + cfqq->queued[1] >= 4) 3955 cfq_mark_cfqq_deep(cfqq); 3956 3957 if (cfqq->next_rq && req_noidle(cfqq->next_rq)) 3958 enable_idle = 0; 3959 else if (!atomic_read(&cic->icq.ioc->active_ref) || 3960 !cfqd->cfq_slice_idle || 3961 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq))) 3962 enable_idle = 0; 3963 else if (sample_valid(cic->ttime.ttime_samples)) { 3964 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle) 3965 enable_idle = 0; 3966 else 3967 enable_idle = 1; 3968 } 3969 3970 if (old_idle != enable_idle) { 3971 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle); 3972 if (enable_idle) 3973 cfq_mark_cfqq_idle_window(cfqq); 3974 else 3975 cfq_clear_cfqq_idle_window(cfqq); 3976 } 3977} 3978 3979/* 3980 * Check if new_cfqq should preempt the currently active queue. Return 0 for 3981 * no or if we aren't sure, a 1 will cause a preempt. 3982 */ 3983static bool 3984cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, 3985 struct request *rq) 3986{ 3987 struct cfq_queue *cfqq; 3988 3989 cfqq = cfqd->active_queue; 3990 if (!cfqq) 3991 return false; 3992 3993 if (cfq_class_idle(new_cfqq)) 3994 return false; 3995 3996 if (cfq_class_idle(cfqq)) 3997 return true; 3998 3999 /* 4000 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice. 4001 */ 4002 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq)) 4003 return false; 4004 4005 /* 4006 * if the new request is sync, but the currently running queue is 4007 * not, let the sync request have priority. 4008 */ 4009 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) 4010 return true; 4011 4012 /* 4013 * Treat ancestors of current cgroup the same way as current cgroup. 4014 * For anybody else we disallow preemption to guarantee service 4015 * fairness among cgroups. 4016 */ 4017 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg)) 4018 return false; 4019 4020 if (cfq_slice_used(cfqq)) 4021 return true; 4022 4023 /* 4024 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice. 4025 */ 4026 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq)) 4027 return true; 4028 4029 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class); 4030 /* Allow preemption only if we are idling on sync-noidle tree */ 4031 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD && 4032 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD && 4033 RB_EMPTY_ROOT(&cfqq->sort_list)) 4034 return true; 4035 4036 /* 4037 * So both queues are sync. Let the new request get disk time if 4038 * it's a metadata request and the current queue is doing regular IO. 4039 */ 4040 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending) 4041 return true; 4042 4043 /* An idle queue should not be idle now for some reason */ 4044 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq)) 4045 return true; 4046 4047 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) 4048 return false; 4049 4050 /* 4051 * if this request is as-good as one we would expect from the 4052 * current cfqq, let it preempt 4053 */ 4054 if (cfq_rq_close(cfqd, cfqq, rq)) 4055 return true; 4056 4057 return false; 4058} 4059 4060/* 4061 * cfqq preempts the active queue. if we allowed preempt with no slice left, 4062 * let it have half of its nominal slice. 4063 */ 4064static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) 4065{ 4066 enum wl_type_t old_type = cfqq_type(cfqd->active_queue); 4067 4068 cfq_log_cfqq(cfqd, cfqq, "preempt"); 4069 cfq_slice_expired(cfqd, 1); 4070 4071 /* 4072 * workload type is changed, don't save slice, otherwise preempt 4073 * doesn't happen 4074 */ 4075 if (old_type != cfqq_type(cfqq)) 4076 cfqq->cfqg->saved_wl_slice = 0; 4077 4078 /* 4079 * Put the new queue at the front of the of the current list, 4080 * so we know that it will be selected next. 4081 */ 4082 BUG_ON(!cfq_cfqq_on_rr(cfqq)); 4083 4084 cfq_service_tree_add(cfqd, cfqq, 1); 4085 4086 cfqq->slice_end = 0; 4087 cfq_mark_cfqq_slice_new(cfqq); 4088} 4089 4090/* 4091 * Called when a new fs request (rq) is added (to cfqq). Check if there's 4092 * something we should do about it 4093 */ 4094static void 4095cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, 4096 struct request *rq) 4097{ 4098 struct cfq_io_cq *cic = RQ_CIC(rq); 4099 4100 cfqd->rq_queued++; 4101 if (rq->cmd_flags & REQ_PRIO) 4102 cfqq->prio_pending++; 4103 4104 cfq_update_io_thinktime(cfqd, cfqq, cic); 4105 cfq_update_io_seektime(cfqd, cfqq, rq); 4106 cfq_update_idle_window(cfqd, cfqq, cic); 4107 4108 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); 4109 4110 if (cfqq == cfqd->active_queue) { 4111 /* 4112 * Remember that we saw a request from this process, but 4113 * don't start queuing just yet. Otherwise we risk seeing lots 4114 * of tiny requests, because we disrupt the normal plugging 4115 * and merging. If the request is already larger than a single 4116 * page, let it rip immediately. For that case we assume that 4117 * merging is already done. Ditto for a busy system that 4118 * has other work pending, don't risk delaying until the 4119 * idle timer unplug to continue working. 4120 */ 4121 if (cfq_cfqq_wait_request(cfqq)) { 4122 if (blk_rq_bytes(rq) > PAGE_SIZE || 4123 cfqd->busy_queues > 1) { 4124 cfq_del_timer(cfqd, cfqq); 4125 cfq_clear_cfqq_wait_request(cfqq); 4126 __blk_run_queue(cfqd->queue); 4127 } else { 4128 cfqg_stats_update_idle_time(cfqq->cfqg); 4129 cfq_mark_cfqq_must_dispatch(cfqq); 4130 } 4131 } 4132 } else if (cfq_should_preempt(cfqd, cfqq, rq)) { 4133 /* 4134 * not the active queue - expire current slice if it is 4135 * idle and has expired it's mean thinktime or this new queue 4136 * has some old slice time left and is of higher priority or 4137 * this new queue is RT and the current one is BE 4138 */ 4139 cfq_preempt_queue(cfqd, cfqq); 4140 __blk_run_queue(cfqd->queue); 4141 } 4142} 4143 4144static void cfq_insert_request(struct request_queue *q, struct request *rq) 4145{ 4146 struct cfq_data *cfqd = q->elevator->elevator_data; 4147 struct cfq_queue *cfqq = RQ_CFQQ(rq); 4148 4149 cfq_log_cfqq(cfqd, cfqq, "insert_request"); 4150 cfq_init_prio_data(cfqq, RQ_CIC(rq)); 4151 4152 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)]; 4153 list_add_tail(&rq->queuelist, &cfqq->fifo); 4154 cfq_add_rq_rb(rq); 4155 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group, 4156 rq->cmd_flags); 4157 cfq_rq_enqueued(cfqd, cfqq, rq); 4158} 4159 4160/* 4161 * Update hw_tag based on peak queue depth over 50 samples under 4162 * sufficient load. 4163 */ 4164static void cfq_update_hw_tag(struct cfq_data *cfqd) 4165{ 4166 struct cfq_queue *cfqq = cfqd->active_queue; 4167 4168 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth) 4169 cfqd->hw_tag_est_depth = cfqd->rq_in_driver; 4170 4171 if (cfqd->hw_tag == 1) 4172 return; 4173 4174 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && 4175 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN) 4176 return; 4177 4178 /* 4179 * If active queue hasn't enough requests and can idle, cfq might not 4180 * dispatch sufficient requests to hardware. Don't zero hw_tag in this 4181 * case 4182 */ 4183 if (cfqq && cfq_cfqq_idle_window(cfqq) && 4184 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] < 4185 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN) 4186 return; 4187 4188 if (cfqd->hw_tag_samples++ < 50) 4189 return; 4190 4191 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN) 4192 cfqd->hw_tag = 1; 4193 else 4194 cfqd->hw_tag = 0; 4195} 4196 4197static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq) 4198{ 4199 struct cfq_io_cq *cic = cfqd->active_cic; 4200 u64 now = ktime_get_ns(); 4201 4202 /* If the queue already has requests, don't wait */ 4203 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) 4204 return false; 4205 4206 /* If there are other queues in the group, don't wait */ 4207 if (cfqq->cfqg->nr_cfqq > 1) 4208 return false; 4209 4210 /* the only queue in the group, but think time is big */ 4211 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) 4212 return false; 4213 4214 if (cfq_slice_used(cfqq)) 4215 return true; 4216 4217 /* if slice left is less than think time, wait busy */ 4218 if (cic && sample_valid(cic->ttime.ttime_samples) 4219 && (cfqq->slice_end - now < cic->ttime.ttime_mean)) 4220 return true; 4221 4222 /* 4223 * If think times is less than a jiffy than ttime_mean=0 and above 4224 * will not be true. It might happen that slice has not expired yet 4225 * but will expire soon (4-5 ns) during select_queue(). To cover the 4226 * case where think time is less than a jiffy, mark the queue wait 4227 * busy if only 1 jiffy is left in the slice. 4228 */ 4229 if (cfqq->slice_end - now <= jiffies_to_nsecs(1)) 4230 return true; 4231 4232 return false; 4233} 4234 4235static void cfq_completed_request(struct request_queue *q, struct request *rq) 4236{ 4237 struct cfq_queue *cfqq = RQ_CFQQ(rq); 4238 struct cfq_data *cfqd = cfqq->cfqd; 4239 const int sync = rq_is_sync(rq); 4240 u64 now = ktime_get_ns(); 4241 4242 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq)); 4243 4244 cfq_update_hw_tag(cfqd); 4245 4246 WARN_ON(!cfqd->rq_in_driver); 4247 WARN_ON(!cfqq->dispatched); 4248 cfqd->rq_in_driver--; 4249 cfqq->dispatched--; 4250 (RQ_CFQG(rq))->dispatched--; 4251 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq), 4252 rq_io_start_time_ns(rq), rq->cmd_flags); 4253 4254 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--; 4255 4256 if (sync) { 4257 struct cfq_rb_root *st; 4258 4259 RQ_CIC(rq)->ttime.last_end_request = now; 4260 4261 if (cfq_cfqq_on_rr(cfqq)) 4262 st = cfqq->service_tree; 4263 else 4264 st = st_for(cfqq->cfqg, cfqq_class(cfqq), 4265 cfqq_type(cfqq)); 4266 4267 st->ttime.last_end_request = now; 4268 /* 4269 * We have to do this check in jiffies since start_time is in 4270 * jiffies and it is not trivial to convert to ns. If 4271 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test 4272 * will become problematic but so far we are fine (the default 4273 * is 128 ms). 4274 */ 4275 if (!time_after(rq->start_time + 4276 nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]), 4277 jiffies)) 4278 cfqd->last_delayed_sync = now; 4279 } 4280 4281#ifdef CONFIG_CFQ_GROUP_IOSCHED 4282 cfqq->cfqg->ttime.last_end_request = now; 4283#endif 4284 4285 /* 4286 * If this is the active queue, check if it needs to be expired, 4287 * or if we want to idle in case it has no pending requests. 4288 */ 4289 if (cfqd->active_queue == cfqq) { 4290 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list); 4291 4292 if (cfq_cfqq_slice_new(cfqq)) { 4293 cfq_set_prio_slice(cfqd, cfqq); 4294 cfq_clear_cfqq_slice_new(cfqq); 4295 } 4296 4297 /* 4298 * Should we wait for next request to come in before we expire 4299 * the queue. 4300 */ 4301 if (cfq_should_wait_busy(cfqd, cfqq)) { 4302 u64 extend_sl = cfqd->cfq_slice_idle; 4303 if (!cfqd->cfq_slice_idle) 4304 extend_sl = cfqd->cfq_group_idle; 4305 cfqq->slice_end = now + extend_sl; 4306 cfq_mark_cfqq_wait_busy(cfqq); 4307 cfq_log_cfqq(cfqd, cfqq, "will busy wait"); 4308 } 4309 4310 /* 4311 * Idling is not enabled on: 4312 * - expired queues 4313 * - idle-priority queues 4314 * - async queues 4315 * - queues with still some requests queued 4316 * - when there is a close cooperator 4317 */ 4318 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) 4319 cfq_slice_expired(cfqd, 1); 4320 else if (sync && cfqq_empty && 4321 !cfq_close_cooperator(cfqd, cfqq)) { 4322 cfq_arm_slice_timer(cfqd); 4323 } 4324 } 4325 4326 if (!cfqd->rq_in_driver) 4327 cfq_schedule_dispatch(cfqd); 4328} 4329 4330static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op) 4331{ 4332 /* 4333 * If REQ_PRIO is set, boost class and prio level, if it's below 4334 * BE/NORM. If prio is not set, restore the potentially boosted 4335 * class/prio level. 4336 */ 4337 if (!(op & REQ_PRIO)) { 4338 cfqq->ioprio_class = cfqq->org_ioprio_class; 4339 cfqq->ioprio = cfqq->org_ioprio; 4340 } else { 4341 if (cfq_class_idle(cfqq)) 4342 cfqq->ioprio_class = IOPRIO_CLASS_BE; 4343 if (cfqq->ioprio > IOPRIO_NORM) 4344 cfqq->ioprio = IOPRIO_NORM; 4345 } 4346} 4347 4348static inline int __cfq_may_queue(struct cfq_queue *cfqq) 4349{ 4350 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) { 4351 cfq_mark_cfqq_must_alloc_slice(cfqq); 4352 return ELV_MQUEUE_MUST; 4353 } 4354 4355 return ELV_MQUEUE_MAY; 4356} 4357 4358static int cfq_may_queue(struct request_queue *q, unsigned int op) 4359{ 4360 struct cfq_data *cfqd = q->elevator->elevator_data; 4361 struct task_struct *tsk = current; 4362 struct cfq_io_cq *cic; 4363 struct cfq_queue *cfqq; 4364 4365 /* 4366 * don't force setup of a queue from here, as a call to may_queue 4367 * does not necessarily imply that a request actually will be queued. 4368 * so just lookup a possibly existing queue, or return 'may queue' 4369 * if that fails 4370 */ 4371 cic = cfq_cic_lookup(cfqd, tsk->io_context); 4372 if (!cic) 4373 return ELV_MQUEUE_MAY; 4374 4375 cfqq = cic_to_cfqq(cic, op_is_sync(op)); 4376 if (cfqq) { 4377 cfq_init_prio_data(cfqq, cic); 4378 cfqq_boost_on_prio(cfqq, op); 4379 4380 return __cfq_may_queue(cfqq); 4381 } 4382 4383 return ELV_MQUEUE_MAY; 4384} 4385 4386/* 4387 * queue lock held here 4388 */ 4389static void cfq_put_request(struct request *rq) 4390{ 4391 struct cfq_queue *cfqq = RQ_CFQQ(rq); 4392 4393 if (cfqq) { 4394 const int rw = rq_data_dir(rq); 4395 4396 BUG_ON(!cfqq->allocated[rw]); 4397 cfqq->allocated[rw]--; 4398 4399 /* Put down rq reference on cfqg */ 4400 cfqg_put(RQ_CFQG(rq)); 4401 rq->elv.priv[0] = NULL; 4402 rq->elv.priv[1] = NULL; 4403 4404 cfq_put_queue(cfqq); 4405 } 4406} 4407 4408static struct cfq_queue * 4409cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic, 4410 struct cfq_queue *cfqq) 4411{ 4412 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq); 4413 cic_set_cfqq(cic, cfqq->new_cfqq, 1); 4414 cfq_mark_cfqq_coop(cfqq->new_cfqq); 4415 cfq_put_queue(cfqq); 4416 return cic_to_cfqq(cic, 1); 4417} 4418 4419/* 4420 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this 4421 * was the last process referring to said cfqq. 4422 */ 4423static struct cfq_queue * 4424split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq) 4425{ 4426 if (cfqq_process_refs(cfqq) == 1) { 4427 cfqq->pid = current->pid; 4428 cfq_clear_cfqq_coop(cfqq); 4429 cfq_clear_cfqq_split_coop(cfqq); 4430 return cfqq; 4431 } 4432 4433 cic_set_cfqq(cic, NULL, 1); 4434 4435 cfq_put_cooperator(cfqq); 4436 4437 cfq_put_queue(cfqq); 4438 return NULL; 4439} 4440/* 4441 * Allocate cfq data structures associated with this request. 4442 */ 4443static int 4444cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio, 4445 gfp_t gfp_mask) 4446{ 4447 struct cfq_data *cfqd = q->elevator->elevator_data; 4448 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq); 4449 const int rw = rq_data_dir(rq); 4450 const bool is_sync = rq_is_sync(rq); 4451 struct cfq_queue *cfqq; 4452 bool disable_wbt; 4453 4454 spin_lock_irq(q->queue_lock); 4455 4456 check_ioprio_changed(cic, bio); 4457 disable_wbt = check_blkcg_changed(cic, bio); 4458new_queue: 4459 cfqq = cic_to_cfqq(cic, is_sync); 4460 if (!cfqq || cfqq == &cfqd->oom_cfqq) { 4461 if (cfqq) 4462 cfq_put_queue(cfqq); 4463 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio); 4464 cic_set_cfqq(cic, cfqq, is_sync); 4465 } else { 4466 /* 4467 * If the queue was seeky for too long, break it apart. 4468 */ 4469 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) { 4470 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq"); 4471 cfqq = split_cfqq(cic, cfqq); 4472 if (!cfqq) 4473 goto new_queue; 4474 } 4475 4476 /* 4477 * Check to see if this queue is scheduled to merge with 4478 * another, closely cooperating queue. The merging of 4479 * queues happens here as it must be done in process context. 4480 * The reference on new_cfqq was taken in merge_cfqqs. 4481 */ 4482 if (cfqq->new_cfqq) 4483 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq); 4484 } 4485 4486 cfqq->allocated[rw]++; 4487 4488 cfqq->ref++; 4489 cfqg_get(cfqq->cfqg); 4490 rq->elv.priv[0] = cfqq; 4491 rq->elv.priv[1] = cfqq->cfqg; 4492 spin_unlock_irq(q->queue_lock); 4493 4494 if (disable_wbt) 4495 wbt_disable_default(q); 4496 4497 return 0; 4498} 4499 4500static void cfq_kick_queue(struct work_struct *work) 4501{ 4502 struct cfq_data *cfqd = 4503 container_of(work, struct cfq_data, unplug_work); 4504 struct request_queue *q = cfqd->queue; 4505 4506 spin_lock_irq(q->queue_lock); 4507 __blk_run_queue(cfqd->queue); 4508 spin_unlock_irq(q->queue_lock); 4509} 4510 4511/* 4512 * Timer running if the active_queue is currently idling inside its time slice 4513 */ 4514static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer) 4515{ 4516 struct cfq_data *cfqd = container_of(timer, struct cfq_data, 4517 idle_slice_timer); 4518 struct cfq_queue *cfqq; 4519 unsigned long flags; 4520 int timed_out = 1; 4521 4522 cfq_log(cfqd, "idle timer fired"); 4523 4524 spin_lock_irqsave(cfqd->queue->queue_lock, flags); 4525 4526 cfqq = cfqd->active_queue; 4527 if (cfqq) { 4528 timed_out = 0; 4529 4530 /* 4531 * We saw a request before the queue expired, let it through 4532 */ 4533 if (cfq_cfqq_must_dispatch(cfqq)) 4534 goto out_kick; 4535 4536 /* 4537 * expired 4538 */ 4539 if (cfq_slice_used(cfqq)) 4540 goto expire; 4541 4542 /* 4543 * only expire and reinvoke request handler, if there are 4544 * other queues with pending requests 4545 */ 4546 if (!cfqd->busy_queues) 4547 goto out_cont; 4548 4549 /* 4550 * not expired and it has a request pending, let it dispatch 4551 */ 4552 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) 4553 goto out_kick; 4554 4555 /* 4556 * Queue depth flag is reset only when the idle didn't succeed 4557 */ 4558 cfq_clear_cfqq_deep(cfqq); 4559 } 4560expire: 4561 cfq_slice_expired(cfqd, timed_out); 4562out_kick: 4563 cfq_schedule_dispatch(cfqd); 4564out_cont: 4565 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); 4566 return HRTIMER_NORESTART; 4567} 4568 4569static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) 4570{ 4571 hrtimer_cancel(&cfqd->idle_slice_timer); 4572 cancel_work_sync(&cfqd->unplug_work); 4573} 4574 4575static void cfq_exit_queue(struct elevator_queue *e) 4576{ 4577 struct cfq_data *cfqd = e->elevator_data; 4578 struct request_queue *q = cfqd->queue; 4579 4580 cfq_shutdown_timer_wq(cfqd); 4581 4582 spin_lock_irq(q->queue_lock); 4583 4584 if (cfqd->active_queue) 4585 __cfq_slice_expired(cfqd, cfqd->active_queue, 0); 4586 4587 spin_unlock_irq(q->queue_lock); 4588 4589 cfq_shutdown_timer_wq(cfqd); 4590 4591#ifdef CONFIG_CFQ_GROUP_IOSCHED 4592 blkcg_deactivate_policy(q, &blkcg_policy_cfq); 4593#else 4594 kfree(cfqd->root_group); 4595#endif 4596 kfree(cfqd); 4597} 4598 4599static int cfq_init_queue(struct request_queue *q, struct elevator_type *e) 4600{ 4601 struct cfq_data *cfqd; 4602 struct blkcg_gq *blkg __maybe_unused; 4603 int i, ret; 4604 struct elevator_queue *eq; 4605 4606 eq = elevator_alloc(q, e); 4607 if (!eq) 4608 return -ENOMEM; 4609 4610 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node); 4611 if (!cfqd) { 4612 kobject_put(&eq->kobj); 4613 return -ENOMEM; 4614 } 4615 eq->elevator_data = cfqd; 4616 4617 cfqd->queue = q; 4618 spin_lock_irq(q->queue_lock); 4619 q->elevator = eq; 4620 spin_unlock_irq(q->queue_lock); 4621 4622 /* Init root service tree */ 4623 cfqd->grp_service_tree = CFQ_RB_ROOT; 4624 4625 /* Init root group and prefer root group over other groups by default */ 4626#ifdef CONFIG_CFQ_GROUP_IOSCHED 4627 ret = blkcg_activate_policy(q, &blkcg_policy_cfq); 4628 if (ret) 4629 goto out_free; 4630 4631 cfqd->root_group = blkg_to_cfqg(q->root_blkg); 4632#else 4633 ret = -ENOMEM; 4634 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group), 4635 GFP_KERNEL, cfqd->queue->node); 4636 if (!cfqd->root_group) 4637 goto out_free; 4638 4639 cfq_init_cfqg_base(cfqd->root_group); 4640 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL; 4641 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL; 4642#endif 4643 4644 /* 4645 * Not strictly needed (since RB_ROOT just clears the node and we 4646 * zeroed cfqd on alloc), but better be safe in case someone decides 4647 * to add magic to the rb code 4648 */ 4649 for (i = 0; i < CFQ_PRIO_LISTS; i++) 4650 cfqd->prio_trees[i] = RB_ROOT; 4651 4652 /* 4653 * Our fallback cfqq if cfq_get_queue() runs into OOM issues. 4654 * Grab a permanent reference to it, so that the normal code flow 4655 * will not attempt to free it. oom_cfqq is linked to root_group 4656 * but shouldn't hold a reference as it'll never be unlinked. Lose 4657 * the reference from linking right away. 4658 */ 4659 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); 4660 cfqd->oom_cfqq.ref++; 4661 4662 spin_lock_irq(q->queue_lock); 4663 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group); 4664 cfqg_put(cfqd->root_group); 4665 spin_unlock_irq(q->queue_lock); 4666 4667 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC, 4668 HRTIMER_MODE_REL); 4669 cfqd->idle_slice_timer.function = cfq_idle_slice_timer; 4670 4671 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); 4672 4673 cfqd->cfq_quantum = cfq_quantum; 4674 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; 4675 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; 4676 cfqd->cfq_back_max = cfq_back_max; 4677 cfqd->cfq_back_penalty = cfq_back_penalty; 4678 cfqd->cfq_slice[0] = cfq_slice_async; 4679 cfqd->cfq_slice[1] = cfq_slice_sync; 4680 cfqd->cfq_target_latency = cfq_target_latency; 4681 cfqd->cfq_slice_async_rq = cfq_slice_async_rq; 4682 cfqd->cfq_slice_idle = cfq_slice_idle; 4683 cfqd->cfq_group_idle = cfq_group_idle; 4684 cfqd->cfq_latency = 1; 4685 cfqd->hw_tag = -1; 4686 /* 4687 * we optimistically start assuming sync ops weren't delayed in last 4688 * second, in order to have larger depth for async operations. 4689 */ 4690 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC; 4691 return 0; 4692 4693out_free: 4694 kfree(cfqd); 4695 kobject_put(&eq->kobj); 4696 return ret; 4697} 4698 4699static void cfq_registered_queue(struct request_queue *q) 4700{ 4701 struct elevator_queue *e = q->elevator; 4702 struct cfq_data *cfqd = e->elevator_data; 4703 4704 /* 4705 * Default to IOPS mode with no idling for SSDs 4706 */ 4707 if (blk_queue_nonrot(q)) 4708 cfqd->cfq_slice_idle = 0; 4709} 4710 4711/* 4712 * sysfs parts below --> 4713 */ 4714static ssize_t 4715cfq_var_show(unsigned int var, char *page) 4716{ 4717 return sprintf(page, "%u\n", var); 4718} 4719 4720static ssize_t 4721cfq_var_store(unsigned int *var, const char *page, size_t count) 4722{ 4723 char *p = (char *) page; 4724 4725 *var = simple_strtoul(p, &p, 10); 4726 return count; 4727} 4728 4729#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ 4730static ssize_t __FUNC(struct elevator_queue *e, char *page) \ 4731{ \ 4732 struct cfq_data *cfqd = e->elevator_data; \ 4733 u64 __data = __VAR; \ 4734 if (__CONV) \ 4735 __data = div_u64(__data, NSEC_PER_MSEC); \ 4736 return cfq_var_show(__data, (page)); \ 4737} 4738SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); 4739SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); 4740SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); 4741SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); 4742SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); 4743SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); 4744SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1); 4745SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); 4746SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); 4747SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); 4748SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); 4749SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1); 4750#undef SHOW_FUNCTION 4751 4752#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \ 4753static ssize_t __FUNC(struct elevator_queue *e, char *page) \ 4754{ \ 4755 struct cfq_data *cfqd = e->elevator_data; \ 4756 u64 __data = __VAR; \ 4757 __data = div_u64(__data, NSEC_PER_USEC); \ 4758 return cfq_var_show(__data, (page)); \ 4759} 4760USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle); 4761USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle); 4762USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]); 4763USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]); 4764USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency); 4765#undef USEC_SHOW_FUNCTION 4766 4767#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ 4768static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ 4769{ \ 4770 struct cfq_data *cfqd = e->elevator_data; \ 4771 unsigned int __data; \ 4772 int ret = cfq_var_store(&__data, (page), count); \ 4773 if (__data < (MIN)) \ 4774 __data = (MIN); \ 4775 else if (__data > (MAX)) \ 4776 __data = (MAX); \ 4777 if (__CONV) \ 4778 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \ 4779 else \ 4780 *(__PTR) = __data; \ 4781 return ret; \ 4782} 4783STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); 4784STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, 4785 UINT_MAX, 1); 4786STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, 4787 UINT_MAX, 1); 4788STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); 4789STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, 4790 UINT_MAX, 0); 4791STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); 4792STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1); 4793STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); 4794STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); 4795STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, 4796 UINT_MAX, 0); 4797STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); 4798STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1); 4799#undef STORE_FUNCTION 4800 4801#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ 4802static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ 4803{ \ 4804 struct cfq_data *cfqd = e->elevator_data; \ 4805 unsigned int __data; \ 4806 int ret = cfq_var_store(&__data, (page), count); \ 4807 if (__data < (MIN)) \ 4808 __data = (MIN); \ 4809 else if (__data > (MAX)) \ 4810 __data = (MAX); \ 4811 *(__PTR) = (u64)__data * NSEC_PER_USEC; \ 4812 return ret; \ 4813} 4814USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX); 4815USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX); 4816USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX); 4817USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX); 4818USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX); 4819#undef USEC_STORE_FUNCTION 4820 4821#define CFQ_ATTR(name) \ 4822 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) 4823 4824static struct elv_fs_entry cfq_attrs[] = { 4825 CFQ_ATTR(quantum), 4826 CFQ_ATTR(fifo_expire_sync), 4827 CFQ_ATTR(fifo_expire_async), 4828 CFQ_ATTR(back_seek_max), 4829 CFQ_ATTR(back_seek_penalty), 4830 CFQ_ATTR(slice_sync), 4831 CFQ_ATTR(slice_sync_us), 4832 CFQ_ATTR(slice_async), 4833 CFQ_ATTR(slice_async_us), 4834 CFQ_ATTR(slice_async_rq), 4835 CFQ_ATTR(slice_idle), 4836 CFQ_ATTR(slice_idle_us), 4837 CFQ_ATTR(group_idle), 4838 CFQ_ATTR(group_idle_us), 4839 CFQ_ATTR(low_latency), 4840 CFQ_ATTR(target_latency), 4841 CFQ_ATTR(target_latency_us), 4842 __ATTR_NULL 4843}; 4844 4845static struct elevator_type iosched_cfq = { 4846 .ops.sq = { 4847 .elevator_merge_fn = cfq_merge, 4848 .elevator_merged_fn = cfq_merged_request, 4849 .elevator_merge_req_fn = cfq_merged_requests, 4850 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge, 4851 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge, 4852 .elevator_bio_merged_fn = cfq_bio_merged, 4853 .elevator_dispatch_fn = cfq_dispatch_requests, 4854 .elevator_add_req_fn = cfq_insert_request, 4855 .elevator_activate_req_fn = cfq_activate_request, 4856 .elevator_deactivate_req_fn = cfq_deactivate_request, 4857 .elevator_completed_req_fn = cfq_completed_request, 4858 .elevator_former_req_fn = elv_rb_former_request, 4859 .elevator_latter_req_fn = elv_rb_latter_request, 4860 .elevator_init_icq_fn = cfq_init_icq, 4861 .elevator_exit_icq_fn = cfq_exit_icq, 4862 .elevator_set_req_fn = cfq_set_request, 4863 .elevator_put_req_fn = cfq_put_request, 4864 .elevator_may_queue_fn = cfq_may_queue, 4865 .elevator_init_fn = cfq_init_queue, 4866 .elevator_exit_fn = cfq_exit_queue, 4867 .elevator_registered_fn = cfq_registered_queue, 4868 }, 4869 .icq_size = sizeof(struct cfq_io_cq), 4870 .icq_align = __alignof__(struct cfq_io_cq), 4871 .elevator_attrs = cfq_attrs, 4872 .elevator_name = "cfq", 4873 .elevator_owner = THIS_MODULE, 4874}; 4875 4876#ifdef CONFIG_CFQ_GROUP_IOSCHED 4877static struct blkcg_policy blkcg_policy_cfq = { 4878 .dfl_cftypes = cfq_blkcg_files, 4879 .legacy_cftypes = cfq_blkcg_legacy_files, 4880 4881 .cpd_alloc_fn = cfq_cpd_alloc, 4882 .cpd_init_fn = cfq_cpd_init, 4883 .cpd_free_fn = cfq_cpd_free, 4884 .cpd_bind_fn = cfq_cpd_bind, 4885 4886 .pd_alloc_fn = cfq_pd_alloc, 4887 .pd_init_fn = cfq_pd_init, 4888 .pd_offline_fn = cfq_pd_offline, 4889 .pd_free_fn = cfq_pd_free, 4890 .pd_reset_stats_fn = cfq_pd_reset_stats, 4891}; 4892#endif 4893 4894static int __init cfq_init(void) 4895{ 4896 int ret; 4897 4898#ifdef CONFIG_CFQ_GROUP_IOSCHED 4899 ret = blkcg_policy_register(&blkcg_policy_cfq); 4900 if (ret) 4901 return ret; 4902#else 4903 cfq_group_idle = 0; 4904#endif 4905 4906 ret = -ENOMEM; 4907 cfq_pool = KMEM_CACHE(cfq_queue, 0); 4908 if (!cfq_pool) 4909 goto err_pol_unreg; 4910 4911 ret = elv_register(&iosched_cfq); 4912 if (ret) 4913 goto err_free_pool; 4914 4915 return 0; 4916 4917err_free_pool: 4918 kmem_cache_destroy(cfq_pool); 4919err_pol_unreg: 4920#ifdef CONFIG_CFQ_GROUP_IOSCHED 4921 blkcg_policy_unregister(&blkcg_policy_cfq); 4922#endif 4923 return ret; 4924} 4925 4926static void __exit cfq_exit(void) 4927{ 4928#ifdef CONFIG_CFQ_GROUP_IOSCHED 4929 blkcg_policy_unregister(&blkcg_policy_cfq); 4930#endif 4931 elv_unregister(&iosched_cfq); 4932 kmem_cache_destroy(cfq_pool); 4933} 4934 4935module_init(cfq_init); 4936module_exit(cfq_exit); 4937 4938MODULE_AUTHOR("Jens Axboe"); 4939MODULE_LICENSE("GPL"); 4940MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");