Linux Audio

Check our new training course

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