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