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