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