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 entities
112  *                             with a given weight.
113  */
114 struct bfq_weight_counter {
115 	unsigned int weight; /* weight of the entities this counter refers to */
116 	unsigned int num_active; /* nr of active entities with this weight */
117 	/*
118 	 * Weights tree member (see bfq_data's @queue_weights_tree and
119 	 * @group_weights_tree)
120 	 */
121 	struct rb_node weights_node;
122 };
123 
124 /**
125  * struct bfq_entity - schedulable entity.
126  *
127  * A bfq_entity is used to represent either a bfq_queue (leaf node in the
128  * cgroup hierarchy) or a bfq_group into the upper level scheduler.  Each
129  * entity belongs to the sched_data of the parent group in the cgroup
130  * hierarchy.  Non-leaf entities have also their own sched_data, stored
131  * in @my_sched_data.
132  *
133  * Each entity stores independently its priority values; this would
134  * allow different weights on different devices, but this
135  * functionality is not exported to userspace by now.  Priorities and
136  * weights are updated lazily, first storing the new values into the
137  * new_* fields, then setting the @prio_changed flag.  As soon as
138  * there is a transition in the entity state that allows the priority
139  * update to take place the effective and the requested priority
140  * values are synchronized.
141  *
142  * Unless cgroups are used, the weight value is calculated from the
143  * ioprio to export the same interface as CFQ.  When dealing with
144  * ``well-behaved'' queues (i.e., queues that do not spend too much
145  * time to consume their budget and have true sequential behavior, and
146  * when there are no external factors breaking anticipation) the
147  * relative weights at each level of the cgroups hierarchy should be
148  * guaranteed.  All the fields are protected by the queue lock of the
149  * containing bfqd.
150  */
151 struct bfq_entity {
152 	/* service_tree member */
153 	struct rb_node rb_node;
154 	/* pointer to the weight counter associated with this entity */
155 	struct bfq_weight_counter *weight_counter;
156 
157 	/*
158 	 * Flag, true if the entity is on a tree (either the active or
159 	 * the idle one of its service_tree) or is in service.
160 	 */
161 	bool on_st;
162 
163 	/* B-WF2Q+ start and finish timestamps [sectors/weight] */
164 	u64 start, finish;
165 
166 	/* tree the entity is enqueued into; %NULL if not on a tree */
167 	struct rb_root *tree;
168 
169 	/*
170 	 * minimum start time of the (active) subtree rooted at this
171 	 * entity; used for O(log N) lookups into active trees
172 	 */
173 	u64 min_start;
174 
175 	/* amount of service received during the last service slot */
176 	int service;
177 
178 	/* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
179 	int budget;
180 
181 	/* weight of the queue */
182 	int weight;
183 	/* next weight if a change is in progress */
184 	int new_weight;
185 
186 	/* original weight, used to implement weight boosting */
187 	int orig_weight;
188 
189 	/* parent entity, for hierarchical scheduling */
190 	struct bfq_entity *parent;
191 
192 	/*
193 	 * For non-leaf nodes in the hierarchy, the associated
194 	 * scheduler queue, %NULL on leaf nodes.
195 	 */
196 	struct bfq_sched_data *my_sched_data;
197 	/* the scheduler queue this entity belongs to */
198 	struct bfq_sched_data *sched_data;
199 
200 	/* flag, set to request a weight, ioprio or ioprio_class change  */
201 	int prio_changed;
202 };
203 
204 struct bfq_group;
205 
206 /**
207  * struct bfq_ttime - per process thinktime stats.
208  */
209 struct bfq_ttime {
210 	/* completion time of the last request */
211 	u64 last_end_request;
212 
213 	/* total process thinktime */
214 	u64 ttime_total;
215 	/* number of thinktime samples */
216 	unsigned long ttime_samples;
217 	/* average process thinktime */
218 	u64 ttime_mean;
219 };
220 
221 /**
222  * struct bfq_queue - leaf schedulable entity.
223  *
224  * A bfq_queue is a leaf request queue; it can be associated with an
225  * io_context or more, if it  is  async or shared  between  cooperating
226  * processes. @cgroup holds a reference to the cgroup, to be sure that it
227  * does not disappear while a bfqq still references it (mostly to avoid
228  * races between request issuing and task migration followed by cgroup
229  * destruction).
230  * All the fields are protected by the queue lock of the containing bfqd.
231  */
232 struct bfq_queue {
233 	/* reference counter */
234 	int ref;
235 	/* parent bfq_data */
236 	struct bfq_data *bfqd;
237 
238 	/* current ioprio and ioprio class */
239 	unsigned short ioprio, ioprio_class;
240 	/* next ioprio and ioprio class if a change is in progress */
241 	unsigned short new_ioprio, new_ioprio_class;
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 	/* 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 
356 /**
357  * struct bfq_io_cq - per (request_queue, io_context) structure.
358  */
359 struct bfq_io_cq {
360 	/* associated io_cq structure */
361 	struct io_cq icq; /* must be the first member */
362 	/* array of two process queues, the sync and the async */
363 	struct bfq_queue *bfqq[2];
364 	/* per (request_queue, blkcg) ioprio */
365 	int ioprio;
366 #ifdef CONFIG_BFQ_GROUP_IOSCHED
367 	uint64_t blkcg_serial_nr; /* the current blkcg serial */
368 #endif
369 	/*
370 	 * Snapshot of the has_short_time flag before merging; taken
371 	 * to remember its value while the queue is merged, so as to
372 	 * be able to restore it in case of split.
373 	 */
374 	bool saved_has_short_ttime;
375 	/*
376 	 * Same purpose as the previous two fields for the I/O bound
377 	 * classification of a queue.
378 	 */
379 	bool saved_IO_bound;
380 
381 	/*
382 	 * Same purpose as the previous fields for the value of the
383 	 * field keeping the queue's belonging to a large burst
384 	 */
385 	bool saved_in_large_burst;
386 	/*
387 	 * True if the queue belonged to a burst list before its merge
388 	 * with another cooperating queue.
389 	 */
390 	bool was_in_burst_list;
391 
392 	/*
393 	 * Similar to previous fields: save wr information.
394 	 */
395 	unsigned long saved_wr_coeff;
396 	unsigned long saved_last_wr_start_finish;
397 	unsigned long saved_wr_start_at_switch_to_srt;
398 	unsigned int saved_wr_cur_max_time;
399 	struct bfq_ttime saved_ttime;
400 };
401 
402 /**
403  * struct bfq_data - per-device data structure.
404  *
405  * All the fields are protected by @lock.
406  */
407 struct bfq_data {
408 	/* device request queue */
409 	struct request_queue *queue;
410 	/* dispatch queue */
411 	struct list_head dispatch;
412 
413 	/* root bfq_group for the device */
414 	struct bfq_group *root_group;
415 
416 	/*
417 	 * rbtree of weight counters of @bfq_queues, sorted by
418 	 * weight. Used to keep track of whether all @bfq_queues have
419 	 * the same weight. The tree contains one counter for each
420 	 * distinct weight associated to some active and not
421 	 * weight-raised @bfq_queue (see the comments to the functions
422 	 * bfq_weights_tree_[add|remove] for further details).
423 	 */
424 	struct rb_root queue_weights_tree;
425 	/*
426 	 * rbtree of non-queue @bfq_entity weight counters, sorted by
427 	 * weight. Used to keep track of whether all @bfq_groups have
428 	 * the same weight. The tree contains one counter for each
429 	 * distinct weight associated to some active @bfq_group (see
430 	 * the comments to the functions bfq_weights_tree_[add|remove]
431 	 * for further details).
432 	 */
433 	struct rb_root group_weights_tree;
434 
435 	/*
436 	 * Number of bfq_queues containing requests (including the
437 	 * queue in service, even if it is idling).
438 	 */
439 	int busy_queues;
440 	/* number of weight-raised busy @bfq_queues */
441 	int wr_busy_queues;
442 	/* number of queued requests */
443 	int queued;
444 	/* number of requests dispatched and waiting for completion */
445 	int rq_in_driver;
446 
447 	/*
448 	 * Maximum number of requests in driver in the last
449 	 * @hw_tag_samples completed requests.
450 	 */
451 	int max_rq_in_driver;
452 	/* number of samples used to calculate hw_tag */
453 	int hw_tag_samples;
454 	/* flag set to one if the driver is showing a queueing behavior */
455 	int hw_tag;
456 
457 	/* number of budgets assigned */
458 	int budgets_assigned;
459 
460 	/*
461 	 * Timer set when idling (waiting) for the next request from
462 	 * the queue in service.
463 	 */
464 	struct hrtimer idle_slice_timer;
465 
466 	/* bfq_queue in service */
467 	struct bfq_queue *in_service_queue;
468 
469 	/* on-disk position of the last served request */
470 	sector_t last_position;
471 
472 	/* time of last request completion (ns) */
473 	u64 last_completion;
474 
475 	/* time of first rq dispatch in current observation interval (ns) */
476 	u64 first_dispatch;
477 	/* time of last rq dispatch in current observation interval (ns) */
478 	u64 last_dispatch;
479 
480 	/* beginning of the last budget */
481 	ktime_t last_budget_start;
482 	/* beginning of the last idle slice */
483 	ktime_t last_idling_start;
484 
485 	/* number of samples in current observation interval */
486 	int peak_rate_samples;
487 	/* num of samples of seq dispatches in current observation interval */
488 	u32 sequential_samples;
489 	/* total num of sectors transferred in current observation interval */
490 	u64 tot_sectors_dispatched;
491 	/* max rq size seen during current observation interval (sectors) */
492 	u32 last_rq_max_size;
493 	/* time elapsed from first dispatch in current observ. interval (us) */
494 	u64 delta_from_first;
495 	/*
496 	 * Current estimate of the device peak rate, measured in
497 	 * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
498 	 * BFQ_RATE_SHIFT is performed to increase precision in
499 	 * fixed-point calculations.
500 	 */
501 	u32 peak_rate;
502 
503 	/* maximum budget allotted to a bfq_queue before rescheduling */
504 	int bfq_max_budget;
505 
506 	/* list of all the bfq_queues active on the device */
507 	struct list_head active_list;
508 	/* list of all the bfq_queues idle on the device */
509 	struct list_head idle_list;
510 
511 	/*
512 	 * Timeout for async/sync requests; when it fires, requests
513 	 * are served in fifo order.
514 	 */
515 	u64 bfq_fifo_expire[2];
516 	/* weight of backward seeks wrt forward ones */
517 	unsigned int bfq_back_penalty;
518 	/* maximum allowed backward seek */
519 	unsigned int bfq_back_max;
520 	/* maximum idling time */
521 	u32 bfq_slice_idle;
522 
523 	/* user-configured max budget value (0 for auto-tuning) */
524 	int bfq_user_max_budget;
525 	/*
526 	 * Timeout for bfq_queues to consume their budget; used to
527 	 * prevent seeky queues from imposing long latencies to
528 	 * sequential or quasi-sequential ones (this also implies that
529 	 * seeky queues cannot receive guarantees in the service
530 	 * domain; after a timeout they are charged for the time they
531 	 * have been in service, to preserve fairness among them, but
532 	 * without service-domain guarantees).
533 	 */
534 	unsigned int bfq_timeout;
535 
536 	/*
537 	 * Number of consecutive requests that must be issued within
538 	 * the idle time slice to set again idling to a queue which
539 	 * was marked as non-I/O-bound (see the definition of the
540 	 * IO_bound flag for further details).
541 	 */
542 	unsigned int bfq_requests_within_timer;
543 
544 	/*
545 	 * Force device idling whenever needed to provide accurate
546 	 * service guarantees, without caring about throughput
547 	 * issues. CAVEAT: this may even increase latencies, in case
548 	 * of useless idling for processes that did stop doing I/O.
549 	 */
550 	bool strict_guarantees;
551 
552 	/*
553 	 * Last time at which a queue entered the current burst of
554 	 * queues being activated shortly after each other; for more
555 	 * details about this and the following parameters related to
556 	 * a burst of activations, see the comments on the function
557 	 * bfq_handle_burst.
558 	 */
559 	unsigned long last_ins_in_burst;
560 	/*
561 	 * Reference time interval used to decide whether a queue has
562 	 * been activated shortly after @last_ins_in_burst.
563 	 */
564 	unsigned long bfq_burst_interval;
565 	/* number of queues in the current burst of queue activations */
566 	int burst_size;
567 
568 	/* common parent entity for the queues in the burst */
569 	struct bfq_entity *burst_parent_entity;
570 	/* Maximum burst size above which the current queue-activation
571 	 * burst is deemed as 'large'.
572 	 */
573 	unsigned long bfq_large_burst_thresh;
574 	/* true if a large queue-activation burst is in progress */
575 	bool large_burst;
576 	/*
577 	 * Head of the burst list (as for the above fields, more
578 	 * details in the comments on the function bfq_handle_burst).
579 	 */
580 	struct hlist_head burst_list;
581 
582 	/* if set to true, low-latency heuristics are enabled */
583 	bool low_latency;
584 	/*
585 	 * Maximum factor by which the weight of a weight-raised queue
586 	 * is multiplied.
587 	 */
588 	unsigned int bfq_wr_coeff;
589 	/* maximum duration of a weight-raising period (jiffies) */
590 	unsigned int bfq_wr_max_time;
591 
592 	/* Maximum weight-raising duration for soft real-time processes */
593 	unsigned int bfq_wr_rt_max_time;
594 	/*
595 	 * Minimum idle period after which weight-raising may be
596 	 * reactivated for a queue (in jiffies).
597 	 */
598 	unsigned int bfq_wr_min_idle_time;
599 	/*
600 	 * Minimum period between request arrivals after which
601 	 * weight-raising may be reactivated for an already busy async
602 	 * queue (in jiffies).
603 	 */
604 	unsigned long bfq_wr_min_inter_arr_async;
605 
606 	/* Max service-rate for a soft real-time queue, in sectors/sec */
607 	unsigned int bfq_wr_max_softrt_rate;
608 	/*
609 	 * Cached value of the product ref_rate*ref_wr_duration, used
610 	 * for computing the maximum duration of weight raising
611 	 * automatically.
612 	 */
613 	u64 rate_dur_prod;
614 
615 	/* fallback dummy bfqq for extreme OOM conditions */
616 	struct bfq_queue oom_bfqq;
617 
618 	spinlock_t lock;
619 
620 	/*
621 	 * bic associated with the task issuing current bio for
622 	 * merging. This and the next field are used as a support to
623 	 * be able to perform the bic lookup, needed by bio-merge
624 	 * functions, before the scheduler lock is taken, and thus
625 	 * avoid taking the request-queue lock while the scheduler
626 	 * lock is being held.
627 	 */
628 	struct bfq_io_cq *bio_bic;
629 	/* bfqq associated with the task issuing current bio for merging */
630 	struct bfq_queue *bio_bfqq;
631 
632 	/*
633 	 * Depth limits used in bfq_limit_depth (see comments on the
634 	 * function)
635 	 */
636 	unsigned int word_depths[2][2];
637 };
638 
639 enum bfqq_state_flags {
640 	BFQQF_just_created = 0,	/* queue just allocated */
641 	BFQQF_busy,		/* has requests or is in service */
642 	BFQQF_wait_request,	/* waiting for a request */
643 	BFQQF_non_blocking_wait_rq, /*
644 				     * waiting for a request
645 				     * without idling the device
646 				     */
647 	BFQQF_fifo_expire,	/* FIFO checked in this slice */
648 	BFQQF_has_short_ttime,	/* queue has a short think time */
649 	BFQQF_sync,		/* synchronous queue */
650 	BFQQF_IO_bound,		/*
651 				 * bfqq has timed-out at least once
652 				 * having consumed at most 2/10 of
653 				 * its budget
654 				 */
655 	BFQQF_in_large_burst,	/*
656 				 * bfqq activated in a large burst,
657 				 * see comments to bfq_handle_burst.
658 				 */
659 	BFQQF_softrt_update,	/*
660 				 * may need softrt-next-start
661 				 * update
662 				 */
663 	BFQQF_coop,		/* bfqq is shared */
664 	BFQQF_split_coop	/* shared bfqq will be split */
665 };
666 
667 #define BFQ_BFQQ_FNS(name)						\
668 void bfq_mark_bfqq_##name(struct bfq_queue *bfqq);			\
669 void bfq_clear_bfqq_##name(struct bfq_queue *bfqq);			\
670 int bfq_bfqq_##name(const struct bfq_queue *bfqq);
671 
672 BFQ_BFQQ_FNS(just_created);
673 BFQ_BFQQ_FNS(busy);
674 BFQ_BFQQ_FNS(wait_request);
675 BFQ_BFQQ_FNS(non_blocking_wait_rq);
676 BFQ_BFQQ_FNS(fifo_expire);
677 BFQ_BFQQ_FNS(has_short_ttime);
678 BFQ_BFQQ_FNS(sync);
679 BFQ_BFQQ_FNS(IO_bound);
680 BFQ_BFQQ_FNS(in_large_burst);
681 BFQ_BFQQ_FNS(coop);
682 BFQ_BFQQ_FNS(split_coop);
683 BFQ_BFQQ_FNS(softrt_update);
684 #undef BFQ_BFQQ_FNS
685 
686 /* Expiration reasons. */
687 enum bfqq_expiration {
688 	BFQQE_TOO_IDLE = 0,		/*
689 					 * queue has been idling for
690 					 * too long
691 					 */
692 	BFQQE_BUDGET_TIMEOUT,	/* budget took too long to be used */
693 	BFQQE_BUDGET_EXHAUSTED,	/* budget consumed */
694 	BFQQE_NO_MORE_REQUESTS,	/* the queue has no more requests */
695 	BFQQE_PREEMPTED		/* preemption in progress */
696 };
697 
698 struct bfqg_stats {
699 #if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
700 	/* number of ios merged */
701 	struct blkg_rwstat		merged;
702 	/* total time spent on device in ns, may not be accurate w/ queueing */
703 	struct blkg_rwstat		service_time;
704 	/* total time spent waiting in scheduler queue in ns */
705 	struct blkg_rwstat		wait_time;
706 	/* number of IOs queued up */
707 	struct blkg_rwstat		queued;
708 	/* total disk time and nr sectors dispatched by this group */
709 	struct blkg_stat		time;
710 	/* sum of number of ios queued across all samples */
711 	struct blkg_stat		avg_queue_size_sum;
712 	/* count of samples taken for average */
713 	struct blkg_stat		avg_queue_size_samples;
714 	/* how many times this group has been removed from service tree */
715 	struct blkg_stat		dequeue;
716 	/* total time spent waiting for it to be assigned a timeslice. */
717 	struct blkg_stat		group_wait_time;
718 	/* time spent idling for this blkcg_gq */
719 	struct blkg_stat		idle_time;
720 	/* total time with empty current active q with other requests queued */
721 	struct blkg_stat		empty_time;
722 	/* fields after this shouldn't be cleared on stat reset */
723 	u64				start_group_wait_time;
724 	u64				start_idle_time;
725 	u64				start_empty_time;
726 	uint16_t			flags;
727 #endif	/* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
728 };
729 
730 #ifdef CONFIG_BFQ_GROUP_IOSCHED
731 
732 /*
733  * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
734  *
735  * @ps: @blkcg_policy_storage that this structure inherits
736  * @weight: weight of the bfq_group
737  */
738 struct bfq_group_data {
739 	/* must be the first member */
740 	struct blkcg_policy_data pd;
741 
742 	unsigned int weight;
743 };
744 
745 /**
746  * struct bfq_group - per (device, cgroup) data structure.
747  * @entity: schedulable entity to insert into the parent group sched_data.
748  * @sched_data: own sched_data, to contain child entities (they may be
749  *              both bfq_queues and bfq_groups).
750  * @bfqd: the bfq_data for the device this group acts upon.
751  * @async_bfqq: array of async queues for all the tasks belonging to
752  *              the group, one queue per ioprio value per ioprio_class,
753  *              except for the idle class that has only one queue.
754  * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
755  * @my_entity: pointer to @entity, %NULL for the toplevel group; used
756  *             to avoid too many special cases during group creation/
757  *             migration.
758  * @stats: stats for this bfqg.
759  * @active_entities: number of active entities belonging to the group;
760  *                   unused for the root group. Used to know whether there
761  *                   are groups with more than one active @bfq_entity
762  *                   (see the comments to the function
763  *                   bfq_bfqq_may_idle()).
764  * @rq_pos_tree: rbtree sorted by next_request position, used when
765  *               determining if two or more queues have interleaving
766  *               requests (see bfq_find_close_cooperator()).
767  *
768  * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
769  * there is a set of bfq_groups, each one collecting the lower-level
770  * entities belonging to the group that are acting on the same device.
771  *
772  * Locking works as follows:
773  *    o @bfqd is protected by the queue lock, RCU is used to access it
774  *      from the readers.
775  *    o All the other fields are protected by the @bfqd queue lock.
776  */
777 struct bfq_group {
778 	/* must be the first member */
779 	struct blkg_policy_data pd;
780 
781 	/* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
782 	char blkg_path[128];
783 
784 	/* reference counter (see comments in bfq_bic_update_cgroup) */
785 	int ref;
786 
787 	struct bfq_entity entity;
788 	struct bfq_sched_data sched_data;
789 
790 	void *bfqd;
791 
792 	struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
793 	struct bfq_queue *async_idle_bfqq;
794 
795 	struct bfq_entity *my_entity;
796 
797 	int active_entities;
798 
799 	struct rb_root rq_pos_tree;
800 
801 	struct bfqg_stats stats;
802 };
803 
804 #else
805 struct bfq_group {
806 	struct bfq_sched_data sched_data;
807 
808 	struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
809 	struct bfq_queue *async_idle_bfqq;
810 
811 	struct rb_root rq_pos_tree;
812 };
813 #endif
814 
815 struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
816 
817 /* --------------- main algorithm interface ----------------- */
818 
819 #define BFQ_SERVICE_TREE_INIT	((struct bfq_service_tree)		\
820 				{ RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
821 
822 extern const int bfq_timeout;
823 
824 struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
825 void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
826 struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
827 void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
828 void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_entity *entity,
829 			  struct rb_root *root);
830 void __bfq_weights_tree_remove(struct bfq_data *bfqd,
831 			       struct bfq_entity *entity,
832 			       struct rb_root *root);
833 void bfq_weights_tree_remove(struct bfq_data *bfqd,
834 			     struct bfq_queue *bfqq);
835 void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
836 		     bool compensate, enum bfqq_expiration reason);
837 void bfq_put_queue(struct bfq_queue *bfqq);
838 void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
839 void bfq_schedule_dispatch(struct bfq_data *bfqd);
840 void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
841 
842 /* ------------ end of main algorithm interface -------------- */
843 
844 /* ---------------- cgroups-support interface ---------------- */
845 
846 void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
847 			      unsigned int op);
848 void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op);
849 void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op);
850 void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
851 				  u64 io_start_time_ns, unsigned int op);
852 void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
853 void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
854 void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
855 void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
856 void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
857 void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
858 		   struct bfq_group *bfqg);
859 
860 void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
861 void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
862 void bfq_end_wr_async(struct bfq_data *bfqd);
863 struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
864 				     struct blkcg *blkcg);
865 struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
866 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
867 struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
868 void bfqg_and_blkg_put(struct bfq_group *bfqg);
869 
870 #ifdef CONFIG_BFQ_GROUP_IOSCHED
871 extern struct cftype bfq_blkcg_legacy_files[];
872 extern struct cftype bfq_blkg_files[];
873 extern struct blkcg_policy blkcg_policy_bfq;
874 #endif
875 
876 /* ------------- end of cgroups-support interface ------------- */
877 
878 /* - interface of the internal hierarchical B-WF2Q+ scheduler - */
879 
880 #ifdef CONFIG_BFQ_GROUP_IOSCHED
881 /* both next loops stop at one of the child entities of the root group */
882 #define for_each_entity(entity)	\
883 	for (; entity ; entity = entity->parent)
884 
885 /*
886  * For each iteration, compute parent in advance, so as to be safe if
887  * entity is deallocated during the iteration. Such a deallocation may
888  * happen as a consequence of a bfq_put_queue that frees the bfq_queue
889  * containing entity.
890  */
891 #define for_each_entity_safe(entity, parent) \
892 	for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
893 
894 #else /* CONFIG_BFQ_GROUP_IOSCHED */
895 /*
896  * Next two macros are fake loops when cgroups support is not
897  * enabled. I fact, in such a case, there is only one level to go up
898  * (to reach the root group).
899  */
900 #define for_each_entity(entity)	\
901 	for (; entity ; entity = NULL)
902 
903 #define for_each_entity_safe(entity, parent) \
904 	for (parent = NULL; entity ; entity = parent)
905 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
906 
907 struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq);
908 struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
909 struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
910 struct bfq_entity *bfq_entity_of(struct rb_node *node);
911 unsigned short bfq_ioprio_to_weight(int ioprio);
912 void bfq_put_idle_entity(struct bfq_service_tree *st,
913 			 struct bfq_entity *entity);
914 struct bfq_service_tree *
915 __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
916 				struct bfq_entity *entity,
917 				bool update_class_too);
918 void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
919 void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
920 			  unsigned long time_ms);
921 bool __bfq_deactivate_entity(struct bfq_entity *entity,
922 			     bool ins_into_idle_tree);
923 bool next_queue_may_preempt(struct bfq_data *bfqd);
924 struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
925 void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
926 void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
927 			 bool ins_into_idle_tree, bool expiration);
928 void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
929 void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
930 		      bool expiration);
931 void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
932 		       bool expiration);
933 void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq);
934 
935 /* --------------- end of interface of B-WF2Q+ ---------------- */
936 
937 /* Logging facilities. */
938 #ifdef CONFIG_BFQ_GROUP_IOSCHED
939 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
940 
941 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...)	do {			\
942 	blk_add_cgroup_trace_msg((bfqd)->queue,				\
943 			bfqg_to_blkg(bfqq_group(bfqq))->blkcg,		\
944 			"bfq%d%c " fmt, (bfqq)->pid,			\
945 			bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args);	\
946 } while (0)
947 
948 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)	do {			\
949 	blk_add_cgroup_trace_msg((bfqd)->queue,				\
950 		bfqg_to_blkg(bfqg)->blkcg, fmt, ##args);		\
951 } while (0)
952 
953 #else /* CONFIG_BFQ_GROUP_IOSCHED */
954 
955 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...)	\
956 	blk_add_trace_msg((bfqd)->queue, "bfq%d%c " fmt, (bfqq)->pid,	\
957 			bfq_bfqq_sync((bfqq)) ? 'S' : 'A',		\
958 				##args)
959 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)		do {} while (0)
960 
961 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
962 
963 #define bfq_log(bfqd, fmt, args...) \
964 	blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
965 
966 #endif /* _BFQ_H */
967