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