linux/block/bfq-iosched.h
<<
>>
Prefs
   1/*
   2 * Header file for the BFQ I/O scheduler: data structures and
   3 * prototypes of interface functions among BFQ components.
   4 *
   5 *  This program is free software; you can redistribute it and/or
   6 *  modify it under the terms of the GNU General Public License as
   7 *  published by the Free Software Foundation; either version 2 of the
   8 *  License, or (at your option) any later version.
   9 *
  10 *  This program is distributed in the hope that it will be useful,
  11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 *  General Public License for more details.
  14 */
  15#ifndef _BFQ_H
  16#define _BFQ_H
  17
  18#include <linux/blktrace_api.h>
  19#include <linux/hrtimer.h>
  20#include <linux/blk-cgroup.h>
  21
  22#include "blk-cgroup-rwstat.h"
  23
  24#define BFQ_IOPRIO_CLASSES      3
  25#define BFQ_CL_IDLE_TIMEOUT     (HZ/5)
  26
  27#define BFQ_MIN_WEIGHT                  1
  28#define BFQ_MAX_WEIGHT                  1000
  29#define BFQ_WEIGHT_CONVERSION_COEFF     10
  30
  31#define BFQ_DEFAULT_QUEUE_IOPRIO        4
  32
  33#define BFQ_WEIGHT_LEGACY_DFL   100
  34#define BFQ_DEFAULT_GRP_IOPRIO  0
  35#define BFQ_DEFAULT_GRP_CLASS   IOPRIO_CLASS_BE
  36
  37#define MAX_PID_STR_LENGTH 12
  38
  39/*
  40 * Soft real-time applications are extremely more latency sensitive
  41 * than interactive ones. Over-raise the weight of the former to
  42 * privilege them against the latter.
  43 */
  44#define BFQ_SOFTRT_WEIGHT_FACTOR        100
  45
  46struct bfq_entity;
  47
  48/**
  49 * struct bfq_service_tree - per ioprio_class service tree.
  50 *
  51 * Each service tree represents a B-WF2Q+ scheduler on its own.  Each
  52 * ioprio_class has its own independent scheduler, and so its own
  53 * bfq_service_tree.  All the fields are protected by the queue lock
  54 * of the containing bfqd.
  55 */
  56struct bfq_service_tree {
  57        /* tree for active entities (i.e., those backlogged) */
  58        struct rb_root active;
  59        /* tree for idle entities (i.e., not backlogged, with V < F_i)*/
  60        struct rb_root idle;
  61
  62        /* idle entity with minimum F_i */
  63        struct bfq_entity *first_idle;
  64        /* idle entity with maximum F_i */
  65        struct bfq_entity *last_idle;
  66
  67        /* scheduler virtual time */
  68        u64 vtime;
  69        /* scheduler weight sum; active and idle entities contribute to it */
  70        unsigned long wsum;
  71};
  72
  73/**
  74 * struct bfq_sched_data - multi-class scheduler.
  75 *
  76 * bfq_sched_data is the basic scheduler queue.  It supports three
  77 * ioprio_classes, and can be used either as a toplevel queue or as an
  78 * intermediate queue in a hierarchical setup.
  79 *
  80 * The supported ioprio_classes are the same as in CFQ, in descending
  81 * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
  82 * Requests from higher priority queues are served before all the
  83 * requests from lower priority queues; among requests of the same
  84 * queue requests are served according to B-WF2Q+.
  85 *
  86 * The schedule is implemented by the service trees, plus the field
  87 * @next_in_service, which points to the entity on the active trees
  88 * that will be served next, if 1) no changes in the schedule occurs
  89 * before the current in-service entity is expired, 2) the in-service
  90 * queue becomes idle when it expires, and 3) if the entity pointed by
  91 * in_service_entity is not a queue, then the in-service child entity
  92 * of the entity pointed by in_service_entity becomes idle on
  93 * expiration. This peculiar definition allows for the following
  94 * optimization, not yet exploited: while a given entity is still in
  95 * service, we already know which is the best candidate for next
  96 * service among the other active entitities in the same parent
  97 * entity. We can then quickly compare the timestamps of the
  98 * in-service entity with those of such best candidate.
  99 *
 100 * All fields are protected by the lock of the containing bfqd.
 101 */
 102struct bfq_sched_data {
 103        /* entity in service */
 104        struct bfq_entity *in_service_entity;
 105        /* head-of-line entity (see comments above) */
 106        struct bfq_entity *next_in_service;
 107        /* array of service trees, one per ioprio_class */
 108        struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
 109        /* last time CLASS_IDLE was served */
 110        unsigned long bfq_class_idle_last_service;
 111
 112};
 113
 114/**
 115 * struct bfq_weight_counter - counter of the number of all active queues
 116 *                             with a given weight.
 117 */
 118struct bfq_weight_counter {
 119        unsigned int weight; /* weight of the queues this counter refers to */
 120        unsigned int num_active; /* nr of active queues with this weight */
 121        /*
 122         * Weights tree member (see bfq_data's @queue_weights_tree)
 123         */
 124        struct rb_node weights_node;
 125};
 126
 127/**
 128 * struct bfq_entity - schedulable entity.
 129 *
 130 * A bfq_entity is used to represent either a bfq_queue (leaf node in the
 131 * cgroup hierarchy) or a bfq_group into the upper level scheduler.  Each
 132 * entity belongs to the sched_data of the parent group in the cgroup
 133 * hierarchy.  Non-leaf entities have also their own sched_data, stored
 134 * in @my_sched_data.
 135 *
 136 * Each entity stores independently its priority values; this would
 137 * allow different weights on different devices, but this
 138 * functionality is not exported to userspace by now.  Priorities and
 139 * weights are updated lazily, first storing the new values into the
 140 * new_* fields, then setting the @prio_changed flag.  As soon as
 141 * there is a transition in the entity state that allows the priority
 142 * update to take place the effective and the requested priority
 143 * values are synchronized.
 144 *
 145 * Unless cgroups are used, the weight value is calculated from the
 146 * ioprio to export the same interface as CFQ.  When dealing with
 147 * ``well-behaved'' queues (i.e., queues that do not spend too much
 148 * time to consume their budget and have true sequential behavior, and
 149 * when there are no external factors breaking anticipation) the
 150 * relative weights at each level of the cgroups hierarchy should be
 151 * guaranteed.  All the fields are protected by the queue lock of the
 152 * containing bfqd.
 153 */
 154struct bfq_entity {
 155        /* service_tree member */
 156        struct rb_node rb_node;
 157
 158        /*
 159         * Flag, true if the entity is on a tree (either the active or
 160         * the idle one of its service_tree) or is in service.
 161         */
 162        bool on_st_or_in_serv;
 163
 164        /* B-WF2Q+ start and finish timestamps [sectors/weight] */
 165        u64 start, finish;
 166
 167        /* tree the entity is enqueued into; %NULL if not on a tree */
 168        struct rb_root *tree;
 169
 170        /*
 171         * minimum start time of the (active) subtree rooted at this
 172         * entity; used for O(log N) lookups into active trees
 173         */
 174        u64 min_start;
 175
 176        /* amount of service received during the last service slot */
 177        int service;
 178
 179        /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
 180        int budget;
 181
 182        /* device weight, if non-zero, it overrides the default weight of
 183         * bfq_group_data */
 184        int dev_weight;
 185        /* weight of the queue */
 186        int weight;
 187        /* next weight if a change is in progress */
 188        int new_weight;
 189
 190        /* original weight, used to implement weight boosting */
 191        int orig_weight;
 192
 193        /* parent entity, for hierarchical scheduling */
 194        struct bfq_entity *parent;
 195
 196        /*
 197         * For non-leaf nodes in the hierarchy, the associated
 198         * scheduler queue, %NULL on leaf nodes.
 199         */
 200        struct bfq_sched_data *my_sched_data;
 201        /* the scheduler queue this entity belongs to */
 202        struct bfq_sched_data *sched_data;
 203
 204        /* flag, set to request a weight, ioprio or ioprio_class change  */
 205        int prio_changed;
 206
 207        /* flag, set if the entity is counted in groups_with_pending_reqs */
 208        bool in_groups_with_pending_reqs;
 209};
 210
 211struct bfq_group;
 212
 213/**
 214 * struct bfq_ttime - per process thinktime stats.
 215 */
 216struct bfq_ttime {
 217        /* completion time of the last request */
 218        u64 last_end_request;
 219
 220        /* total process thinktime */
 221        u64 ttime_total;
 222        /* number of thinktime samples */
 223        unsigned long ttime_samples;
 224        /* average process thinktime */
 225        u64 ttime_mean;
 226};
 227
 228/**
 229 * struct bfq_queue - leaf schedulable entity.
 230 *
 231 * A bfq_queue is a leaf request queue; it can be associated with an
 232 * io_context or more, if it  is  async or shared  between  cooperating
 233 * processes. @cgroup holds a reference to the cgroup, to be sure that it
 234 * does not disappear while a bfqq still references it (mostly to avoid
 235 * races between request issuing and task migration followed by cgroup
 236 * destruction).
 237 * All the fields are protected by the queue lock of the containing bfqd.
 238 */
 239struct bfq_queue {
 240        /* reference counter */
 241        int ref;
 242        /* parent bfq_data */
 243        struct bfq_data *bfqd;
 244
 245        /* current ioprio and ioprio class */
 246        unsigned short ioprio, ioprio_class;
 247        /* next ioprio and ioprio class if a change is in progress */
 248        unsigned short new_ioprio, new_ioprio_class;
 249
 250        /* last total-service-time sample, see bfq_update_inject_limit() */
 251        u64 last_serv_time_ns;
 252        /* limit for request injection */
 253        unsigned int inject_limit;
 254        /* last time the inject limit has been decreased, in jiffies */
 255        unsigned long decrease_time_jif;
 256
 257        /*
 258         * Shared bfq_queue if queue is cooperating with one or more
 259         * other queues.
 260         */
 261        struct bfq_queue *new_bfqq;
 262        /* request-position tree member (see bfq_group's @rq_pos_tree) */
 263        struct rb_node pos_node;
 264        /* request-position tree root (see bfq_group's @rq_pos_tree) */
 265        struct rb_root *pos_root;
 266
 267        /* sorted list of pending requests */
 268        struct rb_root sort_list;
 269        /* if fifo isn't expired, next request to serve */
 270        struct request *next_rq;
 271        /* number of sync and async requests queued */
 272        int queued[2];
 273        /* number of requests currently allocated */
 274        int allocated;
 275        /* number of pending metadata requests */
 276        int meta_pending;
 277        /* fifo list of requests in sort_list */
 278        struct list_head fifo;
 279
 280        /* entity representing this queue in the scheduler */
 281        struct bfq_entity entity;
 282
 283        /* pointer to the weight counter associated with this entity */
 284        struct bfq_weight_counter *weight_counter;
 285
 286        /* maximum budget allowed from the feedback mechanism */
 287        int max_budget;
 288        /* budget expiration (in jiffies) */
 289        unsigned long budget_timeout;
 290
 291        /* number of requests on the dispatch list or inside driver */
 292        int dispatched;
 293
 294        /* status flags */
 295        unsigned long flags;
 296
 297        /* node for active/idle bfqq list inside parent bfqd */
 298        struct list_head bfqq_list;
 299
 300        /* associated @bfq_ttime struct */
 301        struct bfq_ttime ttime;
 302
 303        /* when bfqq started to do I/O within the last observation window */
 304        u64 io_start_time;
 305        /* how long bfqq has remained empty during the last observ. window */
 306        u64 tot_idle_time;
 307
 308        /* bit vector: a 1 for each seeky requests in history */
 309        u32 seek_history;
 310
 311        /* node for the device's burst list */
 312        struct hlist_node burst_list_node;
 313
 314        /* position of the last request enqueued */
 315        sector_t last_request_pos;
 316
 317        /* Number of consecutive pairs of request completion and
 318         * arrival, such that the queue becomes idle after the
 319         * completion, but the next request arrives within an idle
 320         * time slice; used only if the queue's IO_bound flag has been
 321         * cleared.
 322         */
 323        unsigned int requests_within_timer;
 324
 325        /* pid of the process owning the queue, used for logging purposes */
 326        pid_t pid;
 327
 328        /*
 329         * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
 330         * if the queue is shared.
 331         */
 332        struct bfq_io_cq *bic;
 333
 334        /* current maximum weight-raising time for this queue */
 335        unsigned long wr_cur_max_time;
 336        /*
 337         * Minimum time instant such that, only if a new request is
 338         * enqueued after this time instant in an idle @bfq_queue with
 339         * no outstanding requests, then the task associated with the
 340         * queue it is deemed as soft real-time (see the comments on
 341         * the function bfq_bfqq_softrt_next_start())
 342         */
 343        unsigned long soft_rt_next_start;
 344        /*
 345         * Start time of the current weight-raising period if
 346         * the @bfq-queue is being weight-raised, otherwise
 347         * finish time of the last weight-raising period.
 348         */
 349        unsigned long last_wr_start_finish;
 350        /* factor by which the weight of this queue is multiplied */
 351        unsigned int wr_coeff;
 352        /*
 353         * Time of the last transition of the @bfq_queue from idle to
 354         * backlogged.
 355         */
 356        unsigned long last_idle_bklogged;
 357        /*
 358         * Cumulative service received from the @bfq_queue since the
 359         * last transition from idle to backlogged.
 360         */
 361        unsigned long service_from_backlogged;
 362        /*
 363         * Cumulative service received from the @bfq_queue since its
 364         * last transition to weight-raised state.
 365         */
 366        unsigned long service_from_wr;
 367
 368        /*
 369         * Value of wr start time when switching to soft rt
 370         */
 371        unsigned long wr_start_at_switch_to_srt;
 372
 373        unsigned long split_time; /* time of last split */
 374
 375        unsigned long first_IO_time; /* time of first I/O for this queue */
 376
 377        /* max service rate measured so far */
 378        u32 max_service_rate;
 379
 380        /*
 381         * Pointer to the waker queue for this queue, i.e., to the
 382         * queue Q such that this queue happens to get new I/O right
 383         * after some I/O request of Q is completed. For details, see
 384         * the comments on the choice of the queue for injection in
 385         * bfq_select_queue().
 386         */
 387        struct bfq_queue *waker_bfqq;
 388        /* pointer to the curr. tentative waker queue, see bfq_check_waker() */
 389        struct bfq_queue *tentative_waker_bfqq;
 390        /* number of times the same tentative waker has been detected */
 391        unsigned int num_waker_detections;
 392
 393        /* node for woken_list, see below */
 394        struct hlist_node woken_list_node;
 395        /*
 396         * Head of the list of the woken queues for this queue, i.e.,
 397         * of the list of the queues for which this queue is a waker
 398         * queue. This list is used to reset the waker_bfqq pointer in
 399         * the woken queues when this queue exits.
 400         */
 401        struct hlist_head woken_list;
 402};
 403
 404/**
 405 * struct bfq_io_cq - per (request_queue, io_context) structure.
 406 */
 407struct bfq_io_cq {
 408        /* associated io_cq structure */
 409        struct io_cq icq; /* must be the first member */
 410        /* array of two process queues, the sync and the async */
 411        struct bfq_queue *bfqq[2];
 412        /* per (request_queue, blkcg) ioprio */
 413        int ioprio;
 414#ifdef CONFIG_BFQ_GROUP_IOSCHED
 415        uint64_t blkcg_serial_nr; /* the current blkcg serial */
 416#endif
 417        /*
 418         * Snapshot of the has_short_time flag before merging; taken
 419         * to remember its value while the queue is merged, so as to
 420         * be able to restore it in case of split.
 421         */
 422        bool saved_has_short_ttime;
 423        /*
 424         * Same purpose as the previous two fields for the I/O bound
 425         * classification of a queue.
 426         */
 427        bool saved_IO_bound;
 428
 429        u64 saved_io_start_time;
 430        u64 saved_tot_idle_time;
 431
 432        /*
 433         * Same purpose as the previous fields for the value of the
 434         * field keeping the queue's belonging to a large burst
 435         */
 436        bool saved_in_large_burst;
 437        /*
 438         * True if the queue belonged to a burst list before its merge
 439         * with another cooperating queue.
 440         */
 441        bool was_in_burst_list;
 442
 443        /*
 444         * Save the weight when a merge occurs, to be able
 445         * to restore it in case of split. If the weight is not
 446         * correctly resumed when the queue is recycled,
 447         * then the weight of the recycled queue could differ
 448         * from the weight of the original queue.
 449         */
 450        unsigned int saved_weight;
 451
 452        /*
 453         * Similar to previous fields: save wr information.
 454         */
 455        unsigned long saved_wr_coeff;
 456        unsigned long saved_last_wr_start_finish;
 457        unsigned long saved_service_from_wr;
 458        unsigned long saved_wr_start_at_switch_to_srt;
 459        unsigned int saved_wr_cur_max_time;
 460        struct bfq_ttime saved_ttime;
 461
 462        /* Save also injection state */
 463        u64 saved_last_serv_time_ns;
 464        unsigned int saved_inject_limit;
 465        unsigned long saved_decrease_time_jif;
 466};
 467
 468/**
 469 * struct bfq_data - per-device data structure.
 470 *
 471 * All the fields are protected by @lock.
 472 */
 473struct bfq_data {
 474        /* device request queue */
 475        struct request_queue *queue;
 476        /* dispatch queue */
 477        struct list_head dispatch;
 478
 479        /* root bfq_group for the device */
 480        struct bfq_group *root_group;
 481
 482        /*
 483         * rbtree of weight counters of @bfq_queues, sorted by
 484         * weight. Used to keep track of whether all @bfq_queues have
 485         * the same weight. The tree contains one counter for each
 486         * distinct weight associated to some active and not
 487         * weight-raised @bfq_queue (see the comments to the functions
 488         * bfq_weights_tree_[add|remove] for further details).
 489         */
 490        struct rb_root_cached queue_weights_tree;
 491
 492        /*
 493         * Number of groups with at least one descendant process that
 494         * has at least one request waiting for completion. Note that
 495         * this accounts for also requests already dispatched, but not
 496         * yet completed. Therefore this number of groups may differ
 497         * (be larger) than the number of active groups, as a group is
 498         * considered active only if its corresponding entity has
 499         * descendant queues with at least one request queued. This
 500         * number is used to decide whether a scenario is symmetric.
 501         * For a detailed explanation see comments on the computation
 502         * of the variable asymmetric_scenario in the function
 503         * bfq_better_to_idle().
 504         *
 505         * However, it is hard to compute this number exactly, for
 506         * groups with multiple descendant processes. Consider a group
 507         * that is inactive, i.e., that has no descendant process with
 508         * pending I/O inside BFQ queues. Then suppose that
 509         * num_groups_with_pending_reqs is still accounting for this
 510         * group, because the group has descendant processes with some
 511         * I/O request still in flight. num_groups_with_pending_reqs
 512         * should be decremented when the in-flight request of the
 513         * last descendant process is finally completed (assuming that
 514         * nothing else has changed for the group in the meantime, in
 515         * terms of composition of the group and active/inactive state of child
 516         * groups and processes). To accomplish this, an additional
 517         * pending-request counter must be added to entities, and must
 518         * be updated correctly. To avoid this additional field and operations,
 519         * we resort to the following tradeoff between simplicity and
 520         * accuracy: for an inactive group that is still counted in
 521         * num_groups_with_pending_reqs, we decrement
 522         * num_groups_with_pending_reqs when the first descendant
 523         * process of the group remains with no request waiting for
 524         * completion.
 525         *
 526         * Even this simpler decrement strategy requires a little
 527         * carefulness: to avoid multiple decrements, we flag a group,
 528         * more precisely an entity representing a group, as still
 529         * counted in num_groups_with_pending_reqs when it becomes
 530         * inactive. Then, when the first descendant queue of the
 531         * entity remains with no request waiting for completion,
 532         * num_groups_with_pending_reqs is decremented, and this flag
 533         * is reset. After this flag is reset for the entity,
 534         * num_groups_with_pending_reqs won't be decremented any
 535         * longer in case a new descendant queue of the entity remains
 536         * with no request waiting for completion.
 537         */
 538        unsigned int num_groups_with_pending_reqs;
 539
 540        /*
 541         * Per-class (RT, BE, IDLE) number of bfq_queues containing
 542         * requests (including the queue in service, even if it is
 543         * idling).
 544         */
 545        unsigned int busy_queues[3];
 546        /* number of weight-raised busy @bfq_queues */
 547        int wr_busy_queues;
 548        /* number of queued requests */
 549        int queued;
 550        /* number of requests dispatched and waiting for completion */
 551        int rq_in_driver;
 552
 553        /* true if the device is non rotational and performs queueing */
 554        bool nonrot_with_queueing;
 555
 556        /*
 557         * Maximum number of requests in driver in the last
 558         * @hw_tag_samples completed requests.
 559         */
 560        int max_rq_in_driver;
 561        /* number of samples used to calculate hw_tag */
 562        int hw_tag_samples;
 563        /* flag set to one if the driver is showing a queueing behavior */
 564        int hw_tag;
 565
 566        /* number of budgets assigned */
 567        int budgets_assigned;
 568
 569        /*
 570         * Timer set when idling (waiting) for the next request from
 571         * the queue in service.
 572         */
 573        struct hrtimer idle_slice_timer;
 574
 575        /* bfq_queue in service */
 576        struct bfq_queue *in_service_queue;
 577
 578        /* on-disk position of the last served request */
 579        sector_t last_position;
 580
 581        /* position of the last served request for the in-service queue */
 582        sector_t in_serv_last_pos;
 583
 584        /* time of last request completion (ns) */
 585        u64 last_completion;
 586
 587        /* bfqq owning the last completed rq */
 588        struct bfq_queue *last_completed_rq_bfqq;
 589
 590        /* time of last transition from empty to non-empty (ns) */
 591        u64 last_empty_occupied_ns;
 592
 593        /*
 594         * Flag set to activate the sampling of the total service time
 595         * of a just-arrived first I/O request (see
 596         * bfq_update_inject_limit()). This will cause the setting of
 597         * waited_rq when the request is finally dispatched.
 598         */
 599        bool wait_dispatch;
 600        /*
 601         *  If set, then bfq_update_inject_limit() is invoked when
 602         *  waited_rq is eventually completed.
 603         */
 604        struct request *waited_rq;
 605        /*
 606         * True if some request has been injected during the last service hole.
 607         */
 608        bool rqs_injected;
 609
 610        /* time of first rq dispatch in current observation interval (ns) */
 611        u64 first_dispatch;
 612        /* time of last rq dispatch in current observation interval (ns) */
 613        u64 last_dispatch;
 614
 615        /* beginning of the last budget */
 616        ktime_t last_budget_start;
 617        /* beginning of the last idle slice */
 618        ktime_t last_idling_start;
 619        unsigned long last_idling_start_jiffies;
 620
 621        /* number of samples in current observation interval */
 622        int peak_rate_samples;
 623        /* num of samples of seq dispatches in current observation interval */
 624        u32 sequential_samples;
 625        /* total num of sectors transferred in current observation interval */
 626        u64 tot_sectors_dispatched;
 627        /* max rq size seen during current observation interval (sectors) */
 628        u32 last_rq_max_size;
 629        /* time elapsed from first dispatch in current observ. interval (us) */
 630        u64 delta_from_first;
 631        /*
 632         * Current estimate of the device peak rate, measured in
 633         * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
 634         * BFQ_RATE_SHIFT is performed to increase precision in
 635         * fixed-point calculations.
 636         */
 637        u32 peak_rate;
 638
 639        /* maximum budget allotted to a bfq_queue before rescheduling */
 640        int bfq_max_budget;
 641
 642        /* list of all the bfq_queues active on the device */
 643        struct list_head active_list;
 644        /* list of all the bfq_queues idle on the device */
 645        struct list_head idle_list;
 646
 647        /*
 648         * Timeout for async/sync requests; when it fires, requests
 649         * are served in fifo order.
 650         */
 651        u64 bfq_fifo_expire[2];
 652        /* weight of backward seeks wrt forward ones */
 653        unsigned int bfq_back_penalty;
 654        /* maximum allowed backward seek */
 655        unsigned int bfq_back_max;
 656        /* maximum idling time */
 657        u32 bfq_slice_idle;
 658
 659        /* user-configured max budget value (0 for auto-tuning) */
 660        int bfq_user_max_budget;
 661        /*
 662         * Timeout for bfq_queues to consume their budget; used to
 663         * prevent seeky queues from imposing long latencies to
 664         * sequential or quasi-sequential ones (this also implies that
 665         * seeky queues cannot receive guarantees in the service
 666         * domain; after a timeout they are charged for the time they
 667         * have been in service, to preserve fairness among them, but
 668         * without service-domain guarantees).
 669         */
 670        unsigned int bfq_timeout;
 671
 672        /*
 673         * Force device idling whenever needed to provide accurate
 674         * service guarantees, without caring about throughput
 675         * issues. CAVEAT: this may even increase latencies, in case
 676         * of useless idling for processes that did stop doing I/O.
 677         */
 678        bool strict_guarantees;
 679
 680        /*
 681         * Last time at which a queue entered the current burst of
 682         * queues being activated shortly after each other; for more
 683         * details about this and the following parameters related to
 684         * a burst of activations, see the comments on the function
 685         * bfq_handle_burst.
 686         */
 687        unsigned long last_ins_in_burst;
 688        /*
 689         * Reference time interval used to decide whether a queue has
 690         * been activated shortly after @last_ins_in_burst.
 691         */
 692        unsigned long bfq_burst_interval;
 693        /* number of queues in the current burst of queue activations */
 694        int burst_size;
 695
 696        /* common parent entity for the queues in the burst */
 697        struct bfq_entity *burst_parent_entity;
 698        /* Maximum burst size above which the current queue-activation
 699         * burst is deemed as 'large'.
 700         */
 701        unsigned long bfq_large_burst_thresh;
 702        /* true if a large queue-activation burst is in progress */
 703        bool large_burst;
 704        /*
 705         * Head of the burst list (as for the above fields, more
 706         * details in the comments on the function bfq_handle_burst).
 707         */
 708        struct hlist_head burst_list;
 709
 710        /* if set to true, low-latency heuristics are enabled */
 711        bool low_latency;
 712        /*
 713         * Maximum factor by which the weight of a weight-raised queue
 714         * is multiplied.
 715         */
 716        unsigned int bfq_wr_coeff;
 717        /* maximum duration of a weight-raising period (jiffies) */
 718        unsigned int bfq_wr_max_time;
 719
 720        /* Maximum weight-raising duration for soft real-time processes */
 721        unsigned int bfq_wr_rt_max_time;
 722        /*
 723         * Minimum idle period after which weight-raising may be
 724         * reactivated for a queue (in jiffies).
 725         */
 726        unsigned int bfq_wr_min_idle_time;
 727        /*
 728         * Minimum period between request arrivals after which
 729         * weight-raising may be reactivated for an already busy async
 730         * queue (in jiffies).
 731         */
 732        unsigned long bfq_wr_min_inter_arr_async;
 733
 734        /* Max service-rate for a soft real-time queue, in sectors/sec */
 735        unsigned int bfq_wr_max_softrt_rate;
 736        /*
 737         * Cached value of the product ref_rate*ref_wr_duration, used
 738         * for computing the maximum duration of weight raising
 739         * automatically.
 740         */
 741        u64 rate_dur_prod;
 742
 743        /* fallback dummy bfqq for extreme OOM conditions */
 744        struct bfq_queue oom_bfqq;
 745
 746        spinlock_t lock;
 747
 748        /*
 749         * bic associated with the task issuing current bio for
 750         * merging. This and the next field are used as a support to
 751         * be able to perform the bic lookup, needed by bio-merge
 752         * functions, before the scheduler lock is taken, and thus
 753         * avoid taking the request-queue lock while the scheduler
 754         * lock is being held.
 755         */
 756        struct bfq_io_cq *bio_bic;
 757        /* bfqq associated with the task issuing current bio for merging */
 758        struct bfq_queue *bio_bfqq;
 759
 760        /*
 761         * Depth limits used in bfq_limit_depth (see comments on the
 762         * function)
 763         */
 764        unsigned int word_depths[2][2];
 765};
 766
 767enum bfqq_state_flags {
 768        BFQQF_just_created = 0, /* queue just allocated */
 769        BFQQF_busy,             /* has requests or is in service */
 770        BFQQF_wait_request,     /* waiting for a request */
 771        BFQQF_non_blocking_wait_rq, /*
 772                                     * waiting for a request
 773                                     * without idling the device
 774                                     */
 775        BFQQF_fifo_expire,      /* FIFO checked in this slice */
 776        BFQQF_has_short_ttime,  /* queue has a short think time */
 777        BFQQF_sync,             /* synchronous queue */
 778        BFQQF_IO_bound,         /*
 779                                 * bfqq has timed-out at least once
 780                                 * having consumed at most 2/10 of
 781                                 * its budget
 782                                 */
 783        BFQQF_in_large_burst,   /*
 784                                 * bfqq activated in a large burst,
 785                                 * see comments to bfq_handle_burst.
 786                                 */
 787        BFQQF_softrt_update,    /*
 788                                 * may need softrt-next-start
 789                                 * update
 790                                 */
 791        BFQQF_coop,             /* bfqq is shared */
 792        BFQQF_split_coop,       /* shared bfqq will be split */
 793};
 794
 795#define BFQ_BFQQ_FNS(name)                                              \
 796void bfq_mark_bfqq_##name(struct bfq_queue *bfqq);                      \
 797void bfq_clear_bfqq_##name(struct bfq_queue *bfqq);                     \
 798int bfq_bfqq_##name(const struct bfq_queue *bfqq);
 799
 800BFQ_BFQQ_FNS(just_created);
 801BFQ_BFQQ_FNS(busy);
 802BFQ_BFQQ_FNS(wait_request);
 803BFQ_BFQQ_FNS(non_blocking_wait_rq);
 804BFQ_BFQQ_FNS(fifo_expire);
 805BFQ_BFQQ_FNS(has_short_ttime);
 806BFQ_BFQQ_FNS(sync);
 807BFQ_BFQQ_FNS(IO_bound);
 808BFQ_BFQQ_FNS(in_large_burst);
 809BFQ_BFQQ_FNS(coop);
 810BFQ_BFQQ_FNS(split_coop);
 811BFQ_BFQQ_FNS(softrt_update);
 812#undef BFQ_BFQQ_FNS
 813
 814/* Expiration reasons. */
 815enum bfqq_expiration {
 816        BFQQE_TOO_IDLE = 0,             /*
 817                                         * queue has been idling for
 818                                         * too long
 819                                         */
 820        BFQQE_BUDGET_TIMEOUT,   /* budget took too long to be used */
 821        BFQQE_BUDGET_EXHAUSTED, /* budget consumed */
 822        BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */
 823        BFQQE_PREEMPTED         /* preemption in progress */
 824};
 825
 826struct bfq_stat {
 827        struct percpu_counter           cpu_cnt;
 828        atomic64_t                      aux_cnt;
 829};
 830
 831struct bfqg_stats {
 832        /* basic stats */
 833        struct blkg_rwstat              bytes;
 834        struct blkg_rwstat              ios;
 835#ifdef CONFIG_BFQ_CGROUP_DEBUG
 836        /* number of ios merged */
 837        struct blkg_rwstat              merged;
 838        /* total time spent on device in ns, may not be accurate w/ queueing */
 839        struct blkg_rwstat              service_time;
 840        /* total time spent waiting in scheduler queue in ns */
 841        struct blkg_rwstat              wait_time;
 842        /* number of IOs queued up */
 843        struct blkg_rwstat              queued;
 844        /* total disk time and nr sectors dispatched by this group */
 845        struct bfq_stat         time;
 846        /* sum of number of ios queued across all samples */
 847        struct bfq_stat         avg_queue_size_sum;
 848        /* count of samples taken for average */
 849        struct bfq_stat         avg_queue_size_samples;
 850        /* how many times this group has been removed from service tree */
 851        struct bfq_stat         dequeue;
 852        /* total time spent waiting for it to be assigned a timeslice. */
 853        struct bfq_stat         group_wait_time;
 854        /* time spent idling for this blkcg_gq */
 855        struct bfq_stat         idle_time;
 856        /* total time with empty current active q with other requests queued */
 857        struct bfq_stat         empty_time;
 858        /* fields after this shouldn't be cleared on stat reset */
 859        u64                             start_group_wait_time;
 860        u64                             start_idle_time;
 861        u64                             start_empty_time;
 862        uint16_t                        flags;
 863#endif /* CONFIG_BFQ_CGROUP_DEBUG */
 864};
 865
 866#ifdef CONFIG_BFQ_GROUP_IOSCHED
 867
 868/*
 869 * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
 870 *
 871 * @ps: @blkcg_policy_storage that this structure inherits
 872 * @weight: weight of the bfq_group
 873 */
 874struct bfq_group_data {
 875        /* must be the first member */
 876        struct blkcg_policy_data pd;
 877
 878        unsigned int weight;
 879};
 880
 881/**
 882 * struct bfq_group - per (device, cgroup) data structure.
 883 * @entity: schedulable entity to insert into the parent group sched_data.
 884 * @sched_data: own sched_data, to contain child entities (they may be
 885 *              both bfq_queues and bfq_groups).
 886 * @bfqd: the bfq_data for the device this group acts upon.
 887 * @async_bfqq: array of async queues for all the tasks belonging to
 888 *              the group, one queue per ioprio value per ioprio_class,
 889 *              except for the idle class that has only one queue.
 890 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
 891 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
 892 *             to avoid too many special cases during group creation/
 893 *             migration.
 894 * @stats: stats for this bfqg.
 895 * @active_entities: number of active entities belonging to the group;
 896 *                   unused for the root group. Used to know whether there
 897 *                   are groups with more than one active @bfq_entity
 898 *                   (see the comments to the function
 899 *                   bfq_bfqq_may_idle()).
 900 * @rq_pos_tree: rbtree sorted by next_request position, used when
 901 *               determining if two or more queues have interleaving
 902 *               requests (see bfq_find_close_cooperator()).
 903 *
 904 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
 905 * there is a set of bfq_groups, each one collecting the lower-level
 906 * entities belonging to the group that are acting on the same device.
 907 *
 908 * Locking works as follows:
 909 *    o @bfqd is protected by the queue lock, RCU is used to access it
 910 *      from the readers.
 911 *    o All the other fields are protected by the @bfqd queue lock.
 912 */
 913struct bfq_group {
 914        /* must be the first member */
 915        struct blkg_policy_data pd;
 916
 917        /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
 918        char blkg_path[128];
 919
 920        /* reference counter (see comments in bfq_bic_update_cgroup) */
 921        int ref;
 922
 923        struct bfq_entity entity;
 924        struct bfq_sched_data sched_data;
 925
 926        void *bfqd;
 927
 928        struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
 929        struct bfq_queue *async_idle_bfqq;
 930
 931        struct bfq_entity *my_entity;
 932
 933        int active_entities;
 934
 935        struct rb_root rq_pos_tree;
 936
 937        struct bfqg_stats stats;
 938};
 939
 940#else
 941struct bfq_group {
 942        struct bfq_entity entity;
 943        struct bfq_sched_data sched_data;
 944
 945        struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
 946        struct bfq_queue *async_idle_bfqq;
 947
 948        struct rb_root rq_pos_tree;
 949};
 950#endif
 951
 952struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
 953
 954/* --------------- main algorithm interface ----------------- */
 955
 956#define BFQ_SERVICE_TREE_INIT   ((struct bfq_service_tree)              \
 957                                { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
 958
 959extern const int bfq_timeout;
 960
 961struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
 962void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
 963struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
 964void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
 965void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
 966                          struct rb_root_cached *root);
 967void __bfq_weights_tree_remove(struct bfq_data *bfqd,
 968                               struct bfq_queue *bfqq,
 969                               struct rb_root_cached *root);
 970void bfq_weights_tree_remove(struct bfq_data *bfqd,
 971                             struct bfq_queue *bfqq);
 972void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
 973                     bool compensate, enum bfqq_expiration reason);
 974void bfq_put_queue(struct bfq_queue *bfqq);
 975void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
 976void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq);
 977void bfq_schedule_dispatch(struct bfq_data *bfqd);
 978void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
 979
 980/* ------------ end of main algorithm interface -------------- */
 981
 982/* ---------------- cgroups-support interface ---------------- */
 983
 984void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq);
 985void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
 986                              unsigned int op);
 987void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op);
 988void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op);
 989void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
 990                                  u64 io_start_time_ns, unsigned int op);
 991void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
 992void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
 993void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
 994void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
 995void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
 996void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
 997                   struct bfq_group *bfqg);
 998
 999void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
1000void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
1001void bfq_end_wr_async(struct bfq_data *bfqd);
1002struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
1003                                     struct blkcg *blkcg);
1004struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
1005struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1006struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
1007void bfqg_and_blkg_get(struct bfq_group *bfqg);
1008void bfqg_and_blkg_put(struct bfq_group *bfqg);
1009
1010#ifdef CONFIG_BFQ_GROUP_IOSCHED
1011extern struct cftype bfq_blkcg_legacy_files[];
1012extern struct cftype bfq_blkg_files[];
1013extern struct blkcg_policy blkcg_policy_bfq;
1014#endif
1015
1016/* ------------- end of cgroups-support interface ------------- */
1017
1018/* - interface of the internal hierarchical B-WF2Q+ scheduler - */
1019
1020#ifdef CONFIG_BFQ_GROUP_IOSCHED
1021/* both next loops stop at one of the child entities of the root group */
1022#define for_each_entity(entity) \
1023        for (; entity ; entity = entity->parent)
1024
1025/*
1026 * For each iteration, compute parent in advance, so as to be safe if
1027 * entity is deallocated during the iteration. Such a deallocation may
1028 * happen as a consequence of a bfq_put_queue that frees the bfq_queue
1029 * containing entity.
1030 */
1031#define for_each_entity_safe(entity, parent) \
1032        for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
1033
1034#else /* CONFIG_BFQ_GROUP_IOSCHED */
1035/*
1036 * Next two macros are fake loops when cgroups support is not
1037 * enabled. I fact, in such a case, there is only one level to go up
1038 * (to reach the root group).
1039 */
1040#define for_each_entity(entity) \
1041        for (; entity ; entity = NULL)
1042
1043#define for_each_entity_safe(entity, parent) \
1044        for (parent = NULL; entity ; entity = parent)
1045#endif /* CONFIG_BFQ_GROUP_IOSCHED */
1046
1047struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq);
1048struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
1049unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
1050struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
1051struct bfq_entity *bfq_entity_of(struct rb_node *node);
1052unsigned short bfq_ioprio_to_weight(int ioprio);
1053void bfq_put_idle_entity(struct bfq_service_tree *st,
1054                         struct bfq_entity *entity);
1055struct bfq_service_tree *
1056__bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
1057                                struct bfq_entity *entity,
1058                                bool update_class_too);
1059void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
1060void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1061                          unsigned long time_ms);
1062bool __bfq_deactivate_entity(struct bfq_entity *entity,
1063                             bool ins_into_idle_tree);
1064bool next_queue_may_preempt(struct bfq_data *bfqd);
1065struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
1066bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
1067void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1068                         bool ins_into_idle_tree, bool expiration);
1069void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1070void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1071                      bool expiration);
1072void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1073                       bool expiration);
1074void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1075
1076/* --------------- end of interface of B-WF2Q+ ---------------- */
1077
1078/* Logging facilities. */
1079static inline void bfq_pid_to_str(int pid, char *str, int len)
1080{
1081        if (pid != -1)
1082                snprintf(str, len, "%d", pid);
1083        else
1084                snprintf(str, len, "SHARED-");
1085}
1086
1087#ifdef CONFIG_BFQ_GROUP_IOSCHED
1088struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1089
1090#define bfq_log_bfqq(bfqd, bfqq, fmt, args...)  do {                    \
1091        char pid_str[MAX_PID_STR_LENGTH];       \
1092        if (likely(!blk_trace_note_message_enabled((bfqd)->queue)))     \
1093                break;                                                  \
1094        bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH);       \
1095        blk_add_cgroup_trace_msg((bfqd)->queue,                         \
1096                        bfqg_to_blkg(bfqq_group(bfqq))->blkcg,          \
1097                        "bfq%s%c " fmt, pid_str,                        \
1098                        bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args);     \
1099} while (0)
1100
1101#define bfq_log_bfqg(bfqd, bfqg, fmt, args...)  do {                    \
1102        blk_add_cgroup_trace_msg((bfqd)->queue,                         \
1103                bfqg_to_blkg(bfqg)->blkcg, fmt, ##args);                \
1104} while (0)
1105
1106#else /* CONFIG_BFQ_GROUP_IOSCHED */
1107
1108#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do {     \
1109        char pid_str[MAX_PID_STR_LENGTH];       \
1110        if (likely(!blk_trace_note_message_enabled((bfqd)->queue)))     \
1111                break;                                                  \
1112        bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH);       \
1113        blk_add_trace_msg((bfqd)->queue, "bfq%s%c " fmt, pid_str,       \
1114                        bfq_bfqq_sync((bfqq)) ? 'S' : 'A',              \
1115                                ##args);        \
1116} while (0)
1117#define bfq_log_bfqg(bfqd, bfqg, fmt, args...)          do {} while (0)
1118
1119#endif /* CONFIG_BFQ_GROUP_IOSCHED */
1120
1121#define bfq_log(bfqd, fmt, args...) \
1122        blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
1123
1124#endif /* _BFQ_H */
1125