Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Header file for the BFQ I/O scheduler: data structures and
0004  * prototypes of interface functions among BFQ components.
0005  */
0006 #ifndef _BFQ_H
0007 #define _BFQ_H
0008 
0009 #include <linux/blktrace_api.h>
0010 #include <linux/hrtimer.h>
0011 
0012 #include "blk-cgroup-rwstat.h"
0013 
0014 #define BFQ_IOPRIO_CLASSES  3
0015 #define BFQ_CL_IDLE_TIMEOUT (HZ/5)
0016 
0017 #define BFQ_MIN_WEIGHT          1
0018 #define BFQ_MAX_WEIGHT          1000
0019 #define BFQ_WEIGHT_CONVERSION_COEFF 10
0020 
0021 #define BFQ_DEFAULT_QUEUE_IOPRIO    4
0022 
0023 #define BFQ_WEIGHT_LEGACY_DFL   100
0024 #define BFQ_DEFAULT_GRP_IOPRIO  0
0025 #define BFQ_DEFAULT_GRP_CLASS   IOPRIO_CLASS_BE
0026 
0027 #define MAX_BFQQ_NAME_LENGTH 16
0028 
0029 /*
0030  * Soft real-time applications are extremely more latency sensitive
0031  * than interactive ones. Over-raise the weight of the former to
0032  * privilege them against the latter.
0033  */
0034 #define BFQ_SOFTRT_WEIGHT_FACTOR    100
0035 
0036 struct bfq_entity;
0037 
0038 /**
0039  * struct bfq_service_tree - per ioprio_class service tree.
0040  *
0041  * Each service tree represents a B-WF2Q+ scheduler on its own.  Each
0042  * ioprio_class has its own independent scheduler, and so its own
0043  * bfq_service_tree.  All the fields are protected by the queue lock
0044  * of the containing bfqd.
0045  */
0046 struct bfq_service_tree {
0047     /* tree for active entities (i.e., those backlogged) */
0048     struct rb_root active;
0049     /* tree for idle entities (i.e., not backlogged, with V < F_i)*/
0050     struct rb_root idle;
0051 
0052     /* idle entity with minimum F_i */
0053     struct bfq_entity *first_idle;
0054     /* idle entity with maximum F_i */
0055     struct bfq_entity *last_idle;
0056 
0057     /* scheduler virtual time */
0058     u64 vtime;
0059     /* scheduler weight sum; active and idle entities contribute to it */
0060     unsigned long wsum;
0061 };
0062 
0063 /**
0064  * struct bfq_sched_data - multi-class scheduler.
0065  *
0066  * bfq_sched_data is the basic scheduler queue.  It supports three
0067  * ioprio_classes, and can be used either as a toplevel queue or as an
0068  * intermediate queue in a hierarchical setup.
0069  *
0070  * The supported ioprio_classes are the same as in CFQ, in descending
0071  * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
0072  * Requests from higher priority queues are served before all the
0073  * requests from lower priority queues; among requests of the same
0074  * queue requests are served according to B-WF2Q+.
0075  *
0076  * The schedule is implemented by the service trees, plus the field
0077  * @next_in_service, which points to the entity on the active trees
0078  * that will be served next, if 1) no changes in the schedule occurs
0079  * before the current in-service entity is expired, 2) the in-service
0080  * queue becomes idle when it expires, and 3) if the entity pointed by
0081  * in_service_entity is not a queue, then the in-service child entity
0082  * of the entity pointed by in_service_entity becomes idle on
0083  * expiration. This peculiar definition allows for the following
0084  * optimization, not yet exploited: while a given entity is still in
0085  * service, we already know which is the best candidate for next
0086  * service among the other active entities in the same parent
0087  * entity. We can then quickly compare the timestamps of the
0088  * in-service entity with those of such best candidate.
0089  *
0090  * All fields are protected by the lock of the containing bfqd.
0091  */
0092 struct bfq_sched_data {
0093     /* entity in service */
0094     struct bfq_entity *in_service_entity;
0095     /* head-of-line entity (see comments above) */
0096     struct bfq_entity *next_in_service;
0097     /* array of service trees, one per ioprio_class */
0098     struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
0099     /* last time CLASS_IDLE was served */
0100     unsigned long bfq_class_idle_last_service;
0101 
0102 };
0103 
0104 /**
0105  * struct bfq_weight_counter - counter of the number of all active queues
0106  *                             with a given weight.
0107  */
0108 struct bfq_weight_counter {
0109     unsigned int weight; /* weight of the queues this counter refers to */
0110     unsigned int num_active; /* nr of active queues with this weight */
0111     /*
0112      * Weights tree member (see bfq_data's @queue_weights_tree)
0113      */
0114     struct rb_node weights_node;
0115 };
0116 
0117 /**
0118  * struct bfq_entity - schedulable entity.
0119  *
0120  * A bfq_entity is used to represent either a bfq_queue (leaf node in the
0121  * cgroup hierarchy) or a bfq_group into the upper level scheduler.  Each
0122  * entity belongs to the sched_data of the parent group in the cgroup
0123  * hierarchy.  Non-leaf entities have also their own sched_data, stored
0124  * in @my_sched_data.
0125  *
0126  * Each entity stores independently its priority values; this would
0127  * allow different weights on different devices, but this
0128  * functionality is not exported to userspace by now.  Priorities and
0129  * weights are updated lazily, first storing the new values into the
0130  * new_* fields, then setting the @prio_changed flag.  As soon as
0131  * there is a transition in the entity state that allows the priority
0132  * update to take place the effective and the requested priority
0133  * values are synchronized.
0134  *
0135  * Unless cgroups are used, the weight value is calculated from the
0136  * ioprio to export the same interface as CFQ.  When dealing with
0137  * "well-behaved" queues (i.e., queues that do not spend too much
0138  * time to consume their budget and have true sequential behavior, and
0139  * when there are no external factors breaking anticipation) the
0140  * relative weights at each level of the cgroups hierarchy should be
0141  * guaranteed.  All the fields are protected by the queue lock of the
0142  * containing bfqd.
0143  */
0144 struct bfq_entity {
0145     /* service_tree member */
0146     struct rb_node rb_node;
0147 
0148     /*
0149      * Flag, true if the entity is on a tree (either the active or
0150      * the idle one of its service_tree) or is in service.
0151      */
0152     bool on_st_or_in_serv;
0153 
0154     /* B-WF2Q+ start and finish timestamps [sectors/weight] */
0155     u64 start, finish;
0156 
0157     /* tree the entity is enqueued into; %NULL if not on a tree */
0158     struct rb_root *tree;
0159 
0160     /*
0161      * minimum start time of the (active) subtree rooted at this
0162      * entity; used for O(log N) lookups into active trees
0163      */
0164     u64 min_start;
0165 
0166     /* amount of service received during the last service slot */
0167     int service;
0168 
0169     /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
0170     int budget;
0171 
0172     /* Number of requests allocated in the subtree of this entity */
0173     int allocated;
0174 
0175     /* device weight, if non-zero, it overrides the default weight of
0176      * bfq_group_data */
0177     int dev_weight;
0178     /* weight of the queue */
0179     int weight;
0180     /* next weight if a change is in progress */
0181     int new_weight;
0182 
0183     /* original weight, used to implement weight boosting */
0184     int orig_weight;
0185 
0186     /* parent entity, for hierarchical scheduling */
0187     struct bfq_entity *parent;
0188 
0189     /*
0190      * For non-leaf nodes in the hierarchy, the associated
0191      * scheduler queue, %NULL on leaf nodes.
0192      */
0193     struct bfq_sched_data *my_sched_data;
0194     /* the scheduler queue this entity belongs to */
0195     struct bfq_sched_data *sched_data;
0196 
0197     /* flag, set to request a weight, ioprio or ioprio_class change  */
0198     int prio_changed;
0199 
0200     /* flag, set if the entity is counted in groups_with_pending_reqs */
0201     bool in_groups_with_pending_reqs;
0202 
0203     /* last child queue of entity created (for non-leaf entities) */
0204     struct bfq_queue *last_bfqq_created;
0205 };
0206 
0207 struct bfq_group;
0208 
0209 /**
0210  * struct bfq_ttime - per process thinktime stats.
0211  */
0212 struct bfq_ttime {
0213     /* completion time of the last request */
0214     u64 last_end_request;
0215 
0216     /* total process thinktime */
0217     u64 ttime_total;
0218     /* number of thinktime samples */
0219     unsigned long ttime_samples;
0220     /* average process thinktime */
0221     u64 ttime_mean;
0222 };
0223 
0224 /**
0225  * struct bfq_queue - leaf schedulable entity.
0226  *
0227  * A bfq_queue is a leaf request queue; it can be associated with an
0228  * io_context or more, if it  is  async or shared  between  cooperating
0229  * processes. @cgroup holds a reference to the cgroup, to be sure that it
0230  * does not disappear while a bfqq still references it (mostly to avoid
0231  * races between request issuing and task migration followed by cgroup
0232  * destruction).
0233  * All the fields are protected by the queue lock of the containing bfqd.
0234  */
0235 struct bfq_queue {
0236     /* reference counter */
0237     int ref;
0238     /* counter of references from other queues for delayed stable merge */
0239     int stable_ref;
0240     /* parent bfq_data */
0241     struct bfq_data *bfqd;
0242 
0243     /* current ioprio and ioprio class */
0244     unsigned short ioprio, ioprio_class;
0245     /* next ioprio and ioprio class if a change is in progress */
0246     unsigned short new_ioprio, new_ioprio_class;
0247 
0248     /* last total-service-time sample, see bfq_update_inject_limit() */
0249     u64 last_serv_time_ns;
0250     /* limit for request injection */
0251     unsigned int inject_limit;
0252     /* last time the inject limit has been decreased, in jiffies */
0253     unsigned long decrease_time_jif;
0254 
0255     /*
0256      * Shared bfq_queue if queue is cooperating with one or more
0257      * other queues.
0258      */
0259     struct bfq_queue *new_bfqq;
0260     /* request-position tree member (see bfq_group's @rq_pos_tree) */
0261     struct rb_node pos_node;
0262     /* request-position tree root (see bfq_group's @rq_pos_tree) */
0263     struct rb_root *pos_root;
0264 
0265     /* sorted list of pending requests */
0266     struct rb_root sort_list;
0267     /* if fifo isn't expired, next request to serve */
0268     struct request *next_rq;
0269     /* number of sync and async requests queued */
0270     int queued[2];
0271     /* number of pending metadata requests */
0272     int meta_pending;
0273     /* fifo list of requests in sort_list */
0274     struct list_head fifo;
0275 
0276     /* entity representing this queue in the scheduler */
0277     struct bfq_entity entity;
0278 
0279     /* pointer to the weight counter associated with this entity */
0280     struct bfq_weight_counter *weight_counter;
0281 
0282     /* maximum budget allowed from the feedback mechanism */
0283     int max_budget;
0284     /* budget expiration (in jiffies) */
0285     unsigned long budget_timeout;
0286 
0287     /* number of requests on the dispatch list or inside driver */
0288     int dispatched;
0289 
0290     /* status flags */
0291     unsigned long flags;
0292 
0293     /* node for active/idle bfqq list inside parent bfqd */
0294     struct list_head bfqq_list;
0295 
0296     /* associated @bfq_ttime struct */
0297     struct bfq_ttime ttime;
0298 
0299     /* when bfqq started to do I/O within the last observation window */
0300     u64 io_start_time;
0301     /* how long bfqq has remained empty during the last observ. window */
0302     u64 tot_idle_time;
0303 
0304     /* bit vector: a 1 for each seeky requests in history */
0305     u32 seek_history;
0306 
0307     /* node for the device's burst list */
0308     struct hlist_node burst_list_node;
0309 
0310     /* position of the last request enqueued */
0311     sector_t last_request_pos;
0312 
0313     /* Number of consecutive pairs of request completion and
0314      * arrival, such that the queue becomes idle after the
0315      * completion, but the next request arrives within an idle
0316      * time slice; used only if the queue's IO_bound flag has been
0317      * cleared.
0318      */
0319     unsigned int requests_within_timer;
0320 
0321     /* pid of the process owning the queue, used for logging purposes */
0322     pid_t pid;
0323 
0324     /*
0325      * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
0326      * if the queue is shared.
0327      */
0328     struct bfq_io_cq *bic;
0329 
0330     /* current maximum weight-raising time for this queue */
0331     unsigned long wr_cur_max_time;
0332     /*
0333      * Minimum time instant such that, only if a new request is
0334      * enqueued after this time instant in an idle @bfq_queue with
0335      * no outstanding requests, then the task associated with the
0336      * queue it is deemed as soft real-time (see the comments on
0337      * the function bfq_bfqq_softrt_next_start())
0338      */
0339     unsigned long soft_rt_next_start;
0340     /*
0341      * Start time of the current weight-raising period if
0342      * the @bfq-queue is being weight-raised, otherwise
0343      * finish time of the last weight-raising period.
0344      */
0345     unsigned long last_wr_start_finish;
0346     /* factor by which the weight of this queue is multiplied */
0347     unsigned int wr_coeff;
0348     /*
0349      * Time of the last transition of the @bfq_queue from idle to
0350      * backlogged.
0351      */
0352     unsigned long last_idle_bklogged;
0353     /*
0354      * Cumulative service received from the @bfq_queue since the
0355      * last transition from idle to backlogged.
0356      */
0357     unsigned long service_from_backlogged;
0358     /*
0359      * Cumulative service received from the @bfq_queue since its
0360      * last transition to weight-raised state.
0361      */
0362     unsigned long service_from_wr;
0363 
0364     /*
0365      * Value of wr start time when switching to soft rt
0366      */
0367     unsigned long wr_start_at_switch_to_srt;
0368 
0369     unsigned long split_time; /* time of last split */
0370 
0371     unsigned long first_IO_time; /* time of first I/O for this queue */
0372 
0373     unsigned long creation_time; /* when this queue is created */
0374 
0375     /* max service rate measured so far */
0376     u32 max_service_rate;
0377 
0378     /*
0379      * Pointer to the waker queue for this queue, i.e., to the
0380      * queue Q such that this queue happens to get new I/O right
0381      * after some I/O request of Q is completed. For details, see
0382      * the comments on the choice of the queue for injection in
0383      * bfq_select_queue().
0384      */
0385     struct bfq_queue *waker_bfqq;
0386     /* pointer to the curr. tentative waker queue, see bfq_check_waker() */
0387     struct bfq_queue *tentative_waker_bfqq;
0388     /* number of times the same tentative waker has been detected */
0389     unsigned int num_waker_detections;
0390     /* time when we started considering this waker */
0391     u64 waker_detection_started;
0392 
0393     /* node for woken_list, see below */
0394     struct hlist_node woken_list_node;
0395     /*
0396      * Head of the list of the woken queues for this queue, i.e.,
0397      * of the list of the queues for which this queue is a waker
0398      * queue. This list is used to reset the waker_bfqq pointer in
0399      * the woken queues when this queue exits.
0400      */
0401     struct hlist_head woken_list;
0402 };
0403 
0404 /**
0405  * struct bfq_io_cq - per (request_queue, io_context) structure.
0406  */
0407 struct bfq_io_cq {
0408     /* associated io_cq structure */
0409     struct io_cq icq; /* must be the first member */
0410     /* array of two process queues, the sync and the async */
0411     struct bfq_queue *bfqq[2];
0412     /* per (request_queue, blkcg) ioprio */
0413     int ioprio;
0414 #ifdef CONFIG_BFQ_GROUP_IOSCHED
0415     uint64_t blkcg_serial_nr; /* the current blkcg serial */
0416 #endif
0417     /*
0418      * Snapshot of the has_short_time flag before merging; taken
0419      * to remember its value while the queue is merged, so as to
0420      * be able to restore it in case of split.
0421      */
0422     bool saved_has_short_ttime;
0423     /*
0424      * Same purpose as the previous two fields for the I/O bound
0425      * classification of a queue.
0426      */
0427     bool saved_IO_bound;
0428 
0429     u64 saved_io_start_time;
0430     u64 saved_tot_idle_time;
0431 
0432     /*
0433      * Same purpose as the previous fields for the value of the
0434      * field keeping the queue's belonging to a large burst
0435      */
0436     bool saved_in_large_burst;
0437     /*
0438      * True if the queue belonged to a burst list before its merge
0439      * with another cooperating queue.
0440      */
0441     bool was_in_burst_list;
0442 
0443     /*
0444      * Save the weight when a merge occurs, to be able
0445      * to restore it in case of split. If the weight is not
0446      * correctly resumed when the queue is recycled,
0447      * then the weight of the recycled queue could differ
0448      * from the weight of the original queue.
0449      */
0450     unsigned int saved_weight;
0451 
0452     /*
0453      * Similar to previous fields: save wr information.
0454      */
0455     unsigned long saved_wr_coeff;
0456     unsigned long saved_last_wr_start_finish;
0457     unsigned long saved_service_from_wr;
0458     unsigned long saved_wr_start_at_switch_to_srt;
0459     unsigned int saved_wr_cur_max_time;
0460     struct bfq_ttime saved_ttime;
0461 
0462     /* Save also injection state */
0463     u64 saved_last_serv_time_ns;
0464     unsigned int saved_inject_limit;
0465     unsigned long saved_decrease_time_jif;
0466 
0467     /* candidate queue for a stable merge (due to close creation time) */
0468     struct bfq_queue *stable_merge_bfqq;
0469 
0470     bool stably_merged; /* non splittable if true */
0471     unsigned int requests;  /* Number of requests this process has in flight */
0472 };
0473 
0474 /**
0475  * struct bfq_data - per-device data structure.
0476  *
0477  * All the fields are protected by @lock.
0478  */
0479 struct bfq_data {
0480     /* device request queue */
0481     struct request_queue *queue;
0482     /* dispatch queue */
0483     struct list_head dispatch;
0484 
0485     /* root bfq_group for the device */
0486     struct bfq_group *root_group;
0487 
0488     /*
0489      * rbtree of weight counters of @bfq_queues, sorted by
0490      * weight. Used to keep track of whether all @bfq_queues have
0491      * the same weight. The tree contains one counter for each
0492      * distinct weight associated to some active and not
0493      * weight-raised @bfq_queue (see the comments to the functions
0494      * bfq_weights_tree_[add|remove] for further details).
0495      */
0496     struct rb_root_cached queue_weights_tree;
0497 
0498     /*
0499      * Number of groups with at least one descendant process that
0500      * has at least one request waiting for completion. Note that
0501      * this accounts for also requests already dispatched, but not
0502      * yet completed. Therefore this number of groups may differ
0503      * (be larger) than the number of active groups, as a group is
0504      * considered active only if its corresponding entity has
0505      * descendant queues with at least one request queued. This
0506      * number is used to decide whether a scenario is symmetric.
0507      * For a detailed explanation see comments on the computation
0508      * of the variable asymmetric_scenario in the function
0509      * bfq_better_to_idle().
0510      *
0511      * However, it is hard to compute this number exactly, for
0512      * groups with multiple descendant processes. Consider a group
0513      * that is inactive, i.e., that has no descendant process with
0514      * pending I/O inside BFQ queues. Then suppose that
0515      * num_groups_with_pending_reqs is still accounting for this
0516      * group, because the group has descendant processes with some
0517      * I/O request still in flight. num_groups_with_pending_reqs
0518      * should be decremented when the in-flight request of the
0519      * last descendant process is finally completed (assuming that
0520      * nothing else has changed for the group in the meantime, in
0521      * terms of composition of the group and active/inactive state of child
0522      * groups and processes). To accomplish this, an additional
0523      * pending-request counter must be added to entities, and must
0524      * be updated correctly. To avoid this additional field and operations,
0525      * we resort to the following tradeoff between simplicity and
0526      * accuracy: for an inactive group that is still counted in
0527      * num_groups_with_pending_reqs, we decrement
0528      * num_groups_with_pending_reqs when the first descendant
0529      * process of the group remains with no request waiting for
0530      * completion.
0531      *
0532      * Even this simpler decrement strategy requires a little
0533      * carefulness: to avoid multiple decrements, we flag a group,
0534      * more precisely an entity representing a group, as still
0535      * counted in num_groups_with_pending_reqs when it becomes
0536      * inactive. Then, when the first descendant queue of the
0537      * entity remains with no request waiting for completion,
0538      * num_groups_with_pending_reqs is decremented, and this flag
0539      * is reset. After this flag is reset for the entity,
0540      * num_groups_with_pending_reqs won't be decremented any
0541      * longer in case a new descendant queue of the entity remains
0542      * with no request waiting for completion.
0543      */
0544     unsigned int num_groups_with_pending_reqs;
0545 
0546     /*
0547      * Per-class (RT, BE, IDLE) number of bfq_queues containing
0548      * requests (including the queue in service, even if it is
0549      * idling).
0550      */
0551     unsigned int busy_queues[3];
0552     /* number of weight-raised busy @bfq_queues */
0553     int wr_busy_queues;
0554     /* number of queued requests */
0555     int queued;
0556     /* number of requests dispatched and waiting for completion */
0557     int rq_in_driver;
0558 
0559     /* true if the device is non rotational and performs queueing */
0560     bool nonrot_with_queueing;
0561 
0562     /*
0563      * Maximum number of requests in driver in the last
0564      * @hw_tag_samples completed requests.
0565      */
0566     int max_rq_in_driver;
0567     /* number of samples used to calculate hw_tag */
0568     int hw_tag_samples;
0569     /* flag set to one if the driver is showing a queueing behavior */
0570     int hw_tag;
0571 
0572     /* number of budgets assigned */
0573     int budgets_assigned;
0574 
0575     /*
0576      * Timer set when idling (waiting) for the next request from
0577      * the queue in service.
0578      */
0579     struct hrtimer idle_slice_timer;
0580 
0581     /* bfq_queue in service */
0582     struct bfq_queue *in_service_queue;
0583 
0584     /* on-disk position of the last served request */
0585     sector_t last_position;
0586 
0587     /* position of the last served request for the in-service queue */
0588     sector_t in_serv_last_pos;
0589 
0590     /* time of last request completion (ns) */
0591     u64 last_completion;
0592 
0593     /* bfqq owning the last completed rq */
0594     struct bfq_queue *last_completed_rq_bfqq;
0595 
0596     /* last bfqq created, among those in the root group */
0597     struct bfq_queue *last_bfqq_created;
0598 
0599     /* time of last transition from empty to non-empty (ns) */
0600     u64 last_empty_occupied_ns;
0601 
0602     /*
0603      * Flag set to activate the sampling of the total service time
0604      * of a just-arrived first I/O request (see
0605      * bfq_update_inject_limit()). This will cause the setting of
0606      * waited_rq when the request is finally dispatched.
0607      */
0608     bool wait_dispatch;
0609     /*
0610      *  If set, then bfq_update_inject_limit() is invoked when
0611      *  waited_rq is eventually completed.
0612      */
0613     struct request *waited_rq;
0614     /*
0615      * True if some request has been injected during the last service hole.
0616      */
0617     bool rqs_injected;
0618 
0619     /* time of first rq dispatch in current observation interval (ns) */
0620     u64 first_dispatch;
0621     /* time of last rq dispatch in current observation interval (ns) */
0622     u64 last_dispatch;
0623 
0624     /* beginning of the last budget */
0625     ktime_t last_budget_start;
0626     /* beginning of the last idle slice */
0627     ktime_t last_idling_start;
0628     unsigned long last_idling_start_jiffies;
0629 
0630     /* number of samples in current observation interval */
0631     int peak_rate_samples;
0632     /* num of samples of seq dispatches in current observation interval */
0633     u32 sequential_samples;
0634     /* total num of sectors transferred in current observation interval */
0635     u64 tot_sectors_dispatched;
0636     /* max rq size seen during current observation interval (sectors) */
0637     u32 last_rq_max_size;
0638     /* time elapsed from first dispatch in current observ. interval (us) */
0639     u64 delta_from_first;
0640     /*
0641      * Current estimate of the device peak rate, measured in
0642      * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
0643      * BFQ_RATE_SHIFT is performed to increase precision in
0644      * fixed-point calculations.
0645      */
0646     u32 peak_rate;
0647 
0648     /* maximum budget allotted to a bfq_queue before rescheduling */
0649     int bfq_max_budget;
0650 
0651     /* list of all the bfq_queues active on the device */
0652     struct list_head active_list;
0653     /* list of all the bfq_queues idle on the device */
0654     struct list_head idle_list;
0655 
0656     /*
0657      * Timeout for async/sync requests; when it fires, requests
0658      * are served in fifo order.
0659      */
0660     u64 bfq_fifo_expire[2];
0661     /* weight of backward seeks wrt forward ones */
0662     unsigned int bfq_back_penalty;
0663     /* maximum allowed backward seek */
0664     unsigned int bfq_back_max;
0665     /* maximum idling time */
0666     u32 bfq_slice_idle;
0667 
0668     /* user-configured max budget value (0 for auto-tuning) */
0669     int bfq_user_max_budget;
0670     /*
0671      * Timeout for bfq_queues to consume their budget; used to
0672      * prevent seeky queues from imposing long latencies to
0673      * sequential or quasi-sequential ones (this also implies that
0674      * seeky queues cannot receive guarantees in the service
0675      * domain; after a timeout they are charged for the time they
0676      * have been in service, to preserve fairness among them, but
0677      * without service-domain guarantees).
0678      */
0679     unsigned int bfq_timeout;
0680 
0681     /*
0682      * Force device idling whenever needed to provide accurate
0683      * service guarantees, without caring about throughput
0684      * issues. CAVEAT: this may even increase latencies, in case
0685      * of useless idling for processes that did stop doing I/O.
0686      */
0687     bool strict_guarantees;
0688 
0689     /*
0690      * Last time at which a queue entered the current burst of
0691      * queues being activated shortly after each other; for more
0692      * details about this and the following parameters related to
0693      * a burst of activations, see the comments on the function
0694      * bfq_handle_burst.
0695      */
0696     unsigned long last_ins_in_burst;
0697     /*
0698      * Reference time interval used to decide whether a queue has
0699      * been activated shortly after @last_ins_in_burst.
0700      */
0701     unsigned long bfq_burst_interval;
0702     /* number of queues in the current burst of queue activations */
0703     int burst_size;
0704 
0705     /* common parent entity for the queues in the burst */
0706     struct bfq_entity *burst_parent_entity;
0707     /* Maximum burst size above which the current queue-activation
0708      * burst is deemed as 'large'.
0709      */
0710     unsigned long bfq_large_burst_thresh;
0711     /* true if a large queue-activation burst is in progress */
0712     bool large_burst;
0713     /*
0714      * Head of the burst list (as for the above fields, more
0715      * details in the comments on the function bfq_handle_burst).
0716      */
0717     struct hlist_head burst_list;
0718 
0719     /* if set to true, low-latency heuristics are enabled */
0720     bool low_latency;
0721     /*
0722      * Maximum factor by which the weight of a weight-raised queue
0723      * is multiplied.
0724      */
0725     unsigned int bfq_wr_coeff;
0726     /* maximum duration of a weight-raising period (jiffies) */
0727     unsigned int bfq_wr_max_time;
0728 
0729     /* Maximum weight-raising duration for soft real-time processes */
0730     unsigned int bfq_wr_rt_max_time;
0731     /*
0732      * Minimum idle period after which weight-raising may be
0733      * reactivated for a queue (in jiffies).
0734      */
0735     unsigned int bfq_wr_min_idle_time;
0736     /*
0737      * Minimum period between request arrivals after which
0738      * weight-raising may be reactivated for an already busy async
0739      * queue (in jiffies).
0740      */
0741     unsigned long bfq_wr_min_inter_arr_async;
0742 
0743     /* Max service-rate for a soft real-time queue, in sectors/sec */
0744     unsigned int bfq_wr_max_softrt_rate;
0745     /*
0746      * Cached value of the product ref_rate*ref_wr_duration, used
0747      * for computing the maximum duration of weight raising
0748      * automatically.
0749      */
0750     u64 rate_dur_prod;
0751 
0752     /* fallback dummy bfqq for extreme OOM conditions */
0753     struct bfq_queue oom_bfqq;
0754 
0755     spinlock_t lock;
0756 
0757     /*
0758      * bic associated with the task issuing current bio for
0759      * merging. This and the next field are used as a support to
0760      * be able to perform the bic lookup, needed by bio-merge
0761      * functions, before the scheduler lock is taken, and thus
0762      * avoid taking the request-queue lock while the scheduler
0763      * lock is being held.
0764      */
0765     struct bfq_io_cq *bio_bic;
0766     /* bfqq associated with the task issuing current bio for merging */
0767     struct bfq_queue *bio_bfqq;
0768 
0769     /*
0770      * Depth limits used in bfq_limit_depth (see comments on the
0771      * function)
0772      */
0773     unsigned int word_depths[2][2];
0774     unsigned int full_depth_shift;
0775 };
0776 
0777 enum bfqq_state_flags {
0778     BFQQF_just_created = 0, /* queue just allocated */
0779     BFQQF_busy,     /* has requests or is in service */
0780     BFQQF_wait_request, /* waiting for a request */
0781     BFQQF_non_blocking_wait_rq, /*
0782                      * waiting for a request
0783                      * without idling the device
0784                      */
0785     BFQQF_fifo_expire,  /* FIFO checked in this slice */
0786     BFQQF_has_short_ttime,  /* queue has a short think time */
0787     BFQQF_sync,     /* synchronous queue */
0788     BFQQF_IO_bound,     /*
0789                  * bfqq has timed-out at least once
0790                  * having consumed at most 2/10 of
0791                  * its budget
0792                  */
0793     BFQQF_in_large_burst,   /*
0794                  * bfqq activated in a large burst,
0795                  * see comments to bfq_handle_burst.
0796                  */
0797     BFQQF_softrt_update,    /*
0798                  * may need softrt-next-start
0799                  * update
0800                  */
0801     BFQQF_coop,     /* bfqq is shared */
0802     BFQQF_split_coop,   /* shared bfqq will be split */
0803 };
0804 
0805 #define BFQ_BFQQ_FNS(name)                      \
0806 void bfq_mark_bfqq_##name(struct bfq_queue *bfqq);          \
0807 void bfq_clear_bfqq_##name(struct bfq_queue *bfqq);         \
0808 int bfq_bfqq_##name(const struct bfq_queue *bfqq);
0809 
0810 BFQ_BFQQ_FNS(just_created);
0811 BFQ_BFQQ_FNS(busy);
0812 BFQ_BFQQ_FNS(wait_request);
0813 BFQ_BFQQ_FNS(non_blocking_wait_rq);
0814 BFQ_BFQQ_FNS(fifo_expire);
0815 BFQ_BFQQ_FNS(has_short_ttime);
0816 BFQ_BFQQ_FNS(sync);
0817 BFQ_BFQQ_FNS(IO_bound);
0818 BFQ_BFQQ_FNS(in_large_burst);
0819 BFQ_BFQQ_FNS(coop);
0820 BFQ_BFQQ_FNS(split_coop);
0821 BFQ_BFQQ_FNS(softrt_update);
0822 #undef BFQ_BFQQ_FNS
0823 
0824 /* Expiration reasons. */
0825 enum bfqq_expiration {
0826     BFQQE_TOO_IDLE = 0,     /*
0827                      * queue has been idling for
0828                      * too long
0829                      */
0830     BFQQE_BUDGET_TIMEOUT,   /* budget took too long to be used */
0831     BFQQE_BUDGET_EXHAUSTED, /* budget consumed */
0832     BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */
0833     BFQQE_PREEMPTED     /* preemption in progress */
0834 };
0835 
0836 struct bfq_stat {
0837     struct percpu_counter       cpu_cnt;
0838     atomic64_t          aux_cnt;
0839 };
0840 
0841 struct bfqg_stats {
0842     /* basic stats */
0843     struct blkg_rwstat      bytes;
0844     struct blkg_rwstat      ios;
0845 #ifdef CONFIG_BFQ_CGROUP_DEBUG
0846     /* number of ios merged */
0847     struct blkg_rwstat      merged;
0848     /* total time spent on device in ns, may not be accurate w/ queueing */
0849     struct blkg_rwstat      service_time;
0850     /* total time spent waiting in scheduler queue in ns */
0851     struct blkg_rwstat      wait_time;
0852     /* number of IOs queued up */
0853     struct blkg_rwstat      queued;
0854     /* total disk time and nr sectors dispatched by this group */
0855     struct bfq_stat     time;
0856     /* sum of number of ios queued across all samples */
0857     struct bfq_stat     avg_queue_size_sum;
0858     /* count of samples taken for average */
0859     struct bfq_stat     avg_queue_size_samples;
0860     /* how many times this group has been removed from service tree */
0861     struct bfq_stat     dequeue;
0862     /* total time spent waiting for it to be assigned a timeslice. */
0863     struct bfq_stat     group_wait_time;
0864     /* time spent idling for this blkcg_gq */
0865     struct bfq_stat     idle_time;
0866     /* total time with empty current active q with other requests queued */
0867     struct bfq_stat     empty_time;
0868     /* fields after this shouldn't be cleared on stat reset */
0869     u64             start_group_wait_time;
0870     u64             start_idle_time;
0871     u64             start_empty_time;
0872     uint16_t            flags;
0873 #endif /* CONFIG_BFQ_CGROUP_DEBUG */
0874 };
0875 
0876 #ifdef CONFIG_BFQ_GROUP_IOSCHED
0877 
0878 /*
0879  * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
0880  *
0881  * @ps: @blkcg_policy_storage that this structure inherits
0882  * @weight: weight of the bfq_group
0883  */
0884 struct bfq_group_data {
0885     /* must be the first member */
0886     struct blkcg_policy_data pd;
0887 
0888     unsigned int weight;
0889 };
0890 
0891 /**
0892  * struct bfq_group - per (device, cgroup) data structure.
0893  * @entity: schedulable entity to insert into the parent group sched_data.
0894  * @sched_data: own sched_data, to contain child entities (they may be
0895  *              both bfq_queues and bfq_groups).
0896  * @bfqd: the bfq_data for the device this group acts upon.
0897  * @async_bfqq: array of async queues for all the tasks belonging to
0898  *              the group, one queue per ioprio value per ioprio_class,
0899  *              except for the idle class that has only one queue.
0900  * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
0901  * @my_entity: pointer to @entity, %NULL for the toplevel group; used
0902  *             to avoid too many special cases during group creation/
0903  *             migration.
0904  * @stats: stats for this bfqg.
0905  * @active_entities: number of active entities belonging to the group;
0906  *                   unused for the root group. Used to know whether there
0907  *                   are groups with more than one active @bfq_entity
0908  *                   (see the comments to the function
0909  *                   bfq_bfqq_may_idle()).
0910  * @rq_pos_tree: rbtree sorted by next_request position, used when
0911  *               determining if two or more queues have interleaving
0912  *               requests (see bfq_find_close_cooperator()).
0913  *
0914  * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
0915  * there is a set of bfq_groups, each one collecting the lower-level
0916  * entities belonging to the group that are acting on the same device.
0917  *
0918  * Locking works as follows:
0919  *    o @bfqd is protected by the queue lock, RCU is used to access it
0920  *      from the readers.
0921  *    o All the other fields are protected by the @bfqd queue lock.
0922  */
0923 struct bfq_group {
0924     /* must be the first member */
0925     struct blkg_policy_data pd;
0926 
0927     /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
0928     char blkg_path[128];
0929 
0930     /* reference counter (see comments in bfq_bic_update_cgroup) */
0931     int ref;
0932     /* Is bfq_group still online? */
0933     bool online;
0934 
0935     struct bfq_entity entity;
0936     struct bfq_sched_data sched_data;
0937 
0938     void *bfqd;
0939 
0940     struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS];
0941     struct bfq_queue *async_idle_bfqq;
0942 
0943     struct bfq_entity *my_entity;
0944 
0945     int active_entities;
0946 
0947     struct rb_root rq_pos_tree;
0948 
0949     struct bfqg_stats stats;
0950 };
0951 
0952 #else
0953 struct bfq_group {
0954     struct bfq_entity entity;
0955     struct bfq_sched_data sched_data;
0956 
0957     struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS];
0958     struct bfq_queue *async_idle_bfqq;
0959 
0960     struct rb_root rq_pos_tree;
0961 };
0962 #endif
0963 
0964 /* --------------- main algorithm interface ----------------- */
0965 
0966 #define BFQ_SERVICE_TREE_INIT   ((struct bfq_service_tree)      \
0967                 { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
0968 
0969 extern const int bfq_timeout;
0970 
0971 struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
0972 void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
0973 struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
0974 void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
0975 void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
0976               struct rb_root_cached *root);
0977 void __bfq_weights_tree_remove(struct bfq_data *bfqd,
0978                    struct bfq_queue *bfqq,
0979                    struct rb_root_cached *root);
0980 void bfq_weights_tree_remove(struct bfq_data *bfqd,
0981                  struct bfq_queue *bfqq);
0982 void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
0983              bool compensate, enum bfqq_expiration reason);
0984 void bfq_put_queue(struct bfq_queue *bfqq);
0985 void bfq_put_cooperator(struct bfq_queue *bfqq);
0986 void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
0987 void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq);
0988 void bfq_schedule_dispatch(struct bfq_data *bfqd);
0989 void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
0990 
0991 /* ------------ end of main algorithm interface -------------- */
0992 
0993 /* ---------------- cgroups-support interface ---------------- */
0994 
0995 void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq);
0996 void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
0997                   blk_opf_t opf);
0998 void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf);
0999 void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf);
1000 void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
1001                   u64 io_start_time_ns, blk_opf_t opf);
1002 void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
1003 void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
1004 void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
1005 void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
1006 void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
1007 void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1008            struct bfq_group *bfqg);
1009 
1010 void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
1011 void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
1012 void bfq_end_wr_async(struct bfq_data *bfqd);
1013 struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio);
1014 struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
1015 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1016 struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
1017 void bfqg_and_blkg_put(struct bfq_group *bfqg);
1018 
1019 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1020 extern struct cftype bfq_blkcg_legacy_files[];
1021 extern struct cftype bfq_blkg_files[];
1022 extern struct blkcg_policy blkcg_policy_bfq;
1023 #endif
1024 
1025 /* ------------- end of cgroups-support interface ------------- */
1026 
1027 /* - interface of the internal hierarchical B-WF2Q+ scheduler - */
1028 
1029 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1030 /* both next loops stop at one of the child entities of the root group */
1031 #define for_each_entity(entity) \
1032     for (; entity ; entity = entity->parent)
1033 
1034 /*
1035  * For each iteration, compute parent in advance, so as to be safe if
1036  * entity is deallocated during the iteration. Such a deallocation may
1037  * happen as a consequence of a bfq_put_queue that frees the bfq_queue
1038  * containing entity.
1039  */
1040 #define for_each_entity_safe(entity, parent) \
1041     for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
1042 
1043 #else /* CONFIG_BFQ_GROUP_IOSCHED */
1044 /*
1045  * Next two macros are fake loops when cgroups support is not
1046  * enabled. I fact, in such a case, there is only one level to go up
1047  * (to reach the root group).
1048  */
1049 #define for_each_entity(entity) \
1050     for (; entity ; entity = NULL)
1051 
1052 #define for_each_entity_safe(entity, parent) \
1053     for (parent = NULL; entity ; entity = parent)
1054 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1055 
1056 struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
1057 unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
1058 struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
1059 struct bfq_entity *bfq_entity_of(struct rb_node *node);
1060 unsigned short bfq_ioprio_to_weight(int ioprio);
1061 void bfq_put_idle_entity(struct bfq_service_tree *st,
1062              struct bfq_entity *entity);
1063 struct bfq_service_tree *
1064 __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
1065                 struct bfq_entity *entity,
1066                 bool update_class_too);
1067 void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
1068 void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1069               unsigned long time_ms);
1070 bool __bfq_deactivate_entity(struct bfq_entity *entity,
1071                  bool ins_into_idle_tree);
1072 bool next_queue_may_preempt(struct bfq_data *bfqd);
1073 struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
1074 bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
1075 void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1076              bool ins_into_idle_tree, bool expiration);
1077 void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1078 void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1079               bool expiration);
1080 void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1081                bool expiration);
1082 void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1083 
1084 /* --------------- end of interface of B-WF2Q+ ---------------- */
1085 
1086 /* Logging facilities. */
1087 static inline void bfq_bfqq_name(struct bfq_queue *bfqq, char *str, int len)
1088 {
1089     char type = bfq_bfqq_sync(bfqq) ? 'S' : 'A';
1090 
1091     if (bfqq->pid != -1)
1092         snprintf(str, len, "bfq%d%c", bfqq->pid, type);
1093     else
1094         snprintf(str, len, "bfqSHARED-%c", type);
1095 }
1096 
1097 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1098 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1099 
1100 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...)  do {            \
1101     char pid_str[MAX_BFQQ_NAME_LENGTH];             \
1102     if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
1103         break;                          \
1104     bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH);       \
1105     blk_add_cgroup_trace_msg((bfqd)->queue,             \
1106             &bfqg_to_blkg(bfqq_group(bfqq))->blkcg->css,    \
1107             "%s " fmt, pid_str, ##args);            \
1108 } while (0)
1109 
1110 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)  do {            \
1111     blk_add_cgroup_trace_msg((bfqd)->queue,             \
1112         &bfqg_to_blkg(bfqg)->blkcg->css, fmt, ##args);      \
1113 } while (0)
1114 
1115 #else /* CONFIG_BFQ_GROUP_IOSCHED */
1116 
1117 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
1118     char pid_str[MAX_BFQQ_NAME_LENGTH];             \
1119     if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
1120         break;                          \
1121     bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH);       \
1122     blk_add_trace_msg((bfqd)->queue, "%s " fmt, pid_str, ##args);   \
1123 } while (0)
1124 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)      do {} while (0)
1125 
1126 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1127 
1128 #define bfq_log(bfqd, fmt, args...) \
1129     blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
1130 
1131 #endif /* _BFQ_H */