xref: /openbmc/linux/block/bfq-iosched.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1a497ee34SChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0-or-later */
2ea25da48SPaolo Valente /*
3ea25da48SPaolo Valente  * Header file for the BFQ I/O scheduler: data structures and
4ea25da48SPaolo Valente  * prototypes of interface functions among BFQ components.
5ea25da48SPaolo Valente  */
6ea25da48SPaolo Valente #ifndef _BFQ_H
7ea25da48SPaolo Valente #define _BFQ_H
8ea25da48SPaolo Valente 
9ea25da48SPaolo Valente #include <linux/blktrace_api.h>
10ea25da48SPaolo Valente #include <linux/hrtimer.h>
11ea25da48SPaolo Valente 
121d156646STejun Heo #include "blk-cgroup-rwstat.h"
131d156646STejun Heo 
14ea25da48SPaolo Valente #define BFQ_IOPRIO_CLASSES	3
15ea25da48SPaolo Valente #define BFQ_CL_IDLE_TIMEOUT	(HZ/5)
16ea25da48SPaolo Valente 
17ea25da48SPaolo Valente #define BFQ_MIN_WEIGHT			1
18ea25da48SPaolo Valente #define BFQ_MAX_WEIGHT			1000
19ea25da48SPaolo Valente #define BFQ_WEIGHT_CONVERSION_COEFF	10
20ea25da48SPaolo Valente 
21ea25da48SPaolo Valente #define BFQ_DEFAULT_QUEUE_IOPRIO	4
22ea25da48SPaolo Valente 
23ea25da48SPaolo Valente #define BFQ_DEFAULT_GRP_IOPRIO	0
24ea25da48SPaolo Valente #define BFQ_DEFAULT_GRP_CLASS	IOPRIO_CLASS_BE
25ea25da48SPaolo Valente 
26582f04e1SJan Kara #define MAX_BFQQ_NAME_LENGTH 16
271e66413cSFrancesco Pollicino 
28ea25da48SPaolo Valente /*
29ea25da48SPaolo Valente  * Soft real-time applications are extremely more latency sensitive
30ea25da48SPaolo Valente  * than interactive ones. Over-raise the weight of the former to
31ea25da48SPaolo Valente  * privilege them against the latter.
32ea25da48SPaolo Valente  */
33ea25da48SPaolo Valente #define BFQ_SOFTRT_WEIGHT_FACTOR	100
34ea25da48SPaolo Valente 
359778369aSPaolo Valente /*
369778369aSPaolo Valente  * Maximum number of actuators supported. This constant is used simply
379778369aSPaolo Valente  * to define the size of the static array that will contain
389778369aSPaolo Valente  * per-actuator data. The current value is hopefully a good upper
399778369aSPaolo Valente  * bound to the possible number of actuators of any actual drive.
409778369aSPaolo Valente  */
419778369aSPaolo Valente #define BFQ_MAX_ACTUATORS 8
429778369aSPaolo Valente 
43ea25da48SPaolo Valente struct bfq_entity;
44ea25da48SPaolo Valente 
45ea25da48SPaolo Valente /**
46ea25da48SPaolo Valente  * struct bfq_service_tree - per ioprio_class service tree.
47ea25da48SPaolo Valente  *
48ea25da48SPaolo Valente  * Each service tree represents a B-WF2Q+ scheduler on its own.  Each
49ea25da48SPaolo Valente  * ioprio_class has its own independent scheduler, and so its own
50ea25da48SPaolo Valente  * bfq_service_tree.  All the fields are protected by the queue lock
51ea25da48SPaolo Valente  * of the containing bfqd.
52ea25da48SPaolo Valente  */
53ea25da48SPaolo Valente struct bfq_service_tree {
54ea25da48SPaolo Valente 	/* tree for active entities (i.e., those backlogged) */
55ea25da48SPaolo Valente 	struct rb_root active;
5638c91407SHou Tao 	/* tree for idle entities (i.e., not backlogged, with V < F_i)*/
57ea25da48SPaolo Valente 	struct rb_root idle;
58ea25da48SPaolo Valente 
59ea25da48SPaolo Valente 	/* idle entity with minimum F_i */
60ea25da48SPaolo Valente 	struct bfq_entity *first_idle;
61ea25da48SPaolo Valente 	/* idle entity with maximum F_i */
62ea25da48SPaolo Valente 	struct bfq_entity *last_idle;
63ea25da48SPaolo Valente 
64ea25da48SPaolo Valente 	/* scheduler virtual time */
65ea25da48SPaolo Valente 	u64 vtime;
66ea25da48SPaolo Valente 	/* scheduler weight sum; active and idle entities contribute to it */
67ea25da48SPaolo Valente 	unsigned long wsum;
68ea25da48SPaolo Valente };
69ea25da48SPaolo Valente 
70ea25da48SPaolo Valente /**
71ea25da48SPaolo Valente  * struct bfq_sched_data - multi-class scheduler.
72ea25da48SPaolo Valente  *
73ea25da48SPaolo Valente  * bfq_sched_data is the basic scheduler queue.  It supports three
74ea25da48SPaolo Valente  * ioprio_classes, and can be used either as a toplevel queue or as an
7546d556e6SPaolo Valente  * intermediate queue in a hierarchical setup.
76ea25da48SPaolo Valente  *
77ea25da48SPaolo Valente  * The supported ioprio_classes are the same as in CFQ, in descending
78ea25da48SPaolo Valente  * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
79ea25da48SPaolo Valente  * Requests from higher priority queues are served before all the
80ea25da48SPaolo Valente  * requests from lower priority queues; among requests of the same
81ea25da48SPaolo Valente  * queue requests are served according to B-WF2Q+.
8246d556e6SPaolo Valente  *
8346d556e6SPaolo Valente  * The schedule is implemented by the service trees, plus the field
8446d556e6SPaolo Valente  * @next_in_service, which points to the entity on the active trees
8546d556e6SPaolo Valente  * that will be served next, if 1) no changes in the schedule occurs
8646d556e6SPaolo Valente  * before the current in-service entity is expired, 2) the in-service
8746d556e6SPaolo Valente  * queue becomes idle when it expires, and 3) if the entity pointed by
8846d556e6SPaolo Valente  * in_service_entity is not a queue, then the in-service child entity
8946d556e6SPaolo Valente  * of the entity pointed by in_service_entity becomes idle on
9046d556e6SPaolo Valente  * expiration. This peculiar definition allows for the following
9146d556e6SPaolo Valente  * optimization, not yet exploited: while a given entity is still in
9246d556e6SPaolo Valente  * service, we already know which is the best candidate for next
93636b8fe8SAngelo Ruocco  * service among the other active entities in the same parent
9446d556e6SPaolo Valente  * entity. We can then quickly compare the timestamps of the
9546d556e6SPaolo Valente  * in-service entity with those of such best candidate.
9646d556e6SPaolo Valente  *
9746d556e6SPaolo Valente  * All fields are protected by the lock of the containing bfqd.
98ea25da48SPaolo Valente  */
99ea25da48SPaolo Valente struct bfq_sched_data {
100ea25da48SPaolo Valente 	/* entity in service */
101ea25da48SPaolo Valente 	struct bfq_entity *in_service_entity;
102ea25da48SPaolo Valente 	/* head-of-line entity (see comments above) */
103ea25da48SPaolo Valente 	struct bfq_entity *next_in_service;
104ea25da48SPaolo Valente 	/* array of service trees, one per ioprio_class */
105ea25da48SPaolo Valente 	struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
106ea25da48SPaolo Valente 	/* last time CLASS_IDLE was served */
107ea25da48SPaolo Valente 	unsigned long bfq_class_idle_last_service;
108ea25da48SPaolo Valente 
109ea25da48SPaolo Valente };
110ea25da48SPaolo Valente 
111ea25da48SPaolo Valente /**
1122d29c9f8SFederico Motta  * struct bfq_weight_counter - counter of the number of all active queues
113ea25da48SPaolo Valente  *                             with a given weight.
114ea25da48SPaolo Valente  */
115ea25da48SPaolo Valente struct bfq_weight_counter {
1162d29c9f8SFederico Motta 	unsigned int weight; /* weight of the queues this counter refers to */
1172d29c9f8SFederico Motta 	unsigned int num_active; /* nr of active queues with this weight */
118ea25da48SPaolo Valente 	/*
1192d29c9f8SFederico Motta 	 * Weights tree member (see bfq_data's @queue_weights_tree)
120ea25da48SPaolo Valente 	 */
121ea25da48SPaolo Valente 	struct rb_node weights_node;
122ea25da48SPaolo Valente };
123ea25da48SPaolo Valente 
124ea25da48SPaolo Valente /**
125ea25da48SPaolo Valente  * struct bfq_entity - schedulable entity.
126ea25da48SPaolo Valente  *
127ea25da48SPaolo Valente  * A bfq_entity is used to represent either a bfq_queue (leaf node in the
128ea25da48SPaolo Valente  * cgroup hierarchy) or a bfq_group into the upper level scheduler.  Each
129ea25da48SPaolo Valente  * entity belongs to the sched_data of the parent group in the cgroup
130ea25da48SPaolo Valente  * hierarchy.  Non-leaf entities have also their own sched_data, stored
131ea25da48SPaolo Valente  * in @my_sched_data.
132ea25da48SPaolo Valente  *
133ea25da48SPaolo Valente  * Each entity stores independently its priority values; this would
134ea25da48SPaolo Valente  * allow different weights on different devices, but this
135ea25da48SPaolo Valente  * functionality is not exported to userspace by now.  Priorities and
136ea25da48SPaolo Valente  * weights are updated lazily, first storing the new values into the
137ea25da48SPaolo Valente  * new_* fields, then setting the @prio_changed flag.  As soon as
138ea25da48SPaolo Valente  * there is a transition in the entity state that allows the priority
139ea25da48SPaolo Valente  * update to take place the effective and the requested priority
140ea25da48SPaolo Valente  * values are synchronized.
141ea25da48SPaolo Valente  *
142ea25da48SPaolo Valente  * Unless cgroups are used, the weight value is calculated from the
143ea25da48SPaolo Valente  * ioprio to export the same interface as CFQ.  When dealing with
144636b8fe8SAngelo Ruocco  * "well-behaved" queues (i.e., queues that do not spend too much
145ea25da48SPaolo Valente  * time to consume their budget and have true sequential behavior, and
146ea25da48SPaolo Valente  * when there are no external factors breaking anticipation) the
147ea25da48SPaolo Valente  * relative weights at each level of the cgroups hierarchy should be
148ea25da48SPaolo Valente  * guaranteed.  All the fields are protected by the queue lock of the
149ea25da48SPaolo Valente  * containing bfqd.
150ea25da48SPaolo Valente  */
151ea25da48SPaolo Valente struct bfq_entity {
152ea25da48SPaolo Valente 	/* service_tree member */
153ea25da48SPaolo Valente 	struct rb_node rb_node;
154ea25da48SPaolo Valente 
155ea25da48SPaolo Valente 	/*
156ea25da48SPaolo Valente 	 * Flag, true if the entity is on a tree (either the active or
157ea25da48SPaolo Valente 	 * the idle one of its service_tree) or is in service.
158ea25da48SPaolo Valente 	 */
15933a16a98SPaolo Valente 	bool on_st_or_in_serv;
160ea25da48SPaolo Valente 
161ea25da48SPaolo Valente 	/* B-WF2Q+ start and finish timestamps [sectors/weight] */
162ea25da48SPaolo Valente 	u64 start, finish;
163ea25da48SPaolo Valente 
164ea25da48SPaolo Valente 	/* tree the entity is enqueued into; %NULL if not on a tree */
165ea25da48SPaolo Valente 	struct rb_root *tree;
166ea25da48SPaolo Valente 
167ea25da48SPaolo Valente 	/*
168ea25da48SPaolo Valente 	 * minimum start time of the (active) subtree rooted at this
169ea25da48SPaolo Valente 	 * entity; used for O(log N) lookups into active trees
170ea25da48SPaolo Valente 	 */
171ea25da48SPaolo Valente 	u64 min_start;
172ea25da48SPaolo Valente 
173ea25da48SPaolo Valente 	/* amount of service received during the last service slot */
174ea25da48SPaolo Valente 	int service;
175ea25da48SPaolo Valente 
176ea25da48SPaolo Valente 	/* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
177ea25da48SPaolo Valente 	int budget;
178ea25da48SPaolo Valente 
17998f04499SJan Kara 	/* Number of requests allocated in the subtree of this entity */
18098f04499SJan Kara 	int allocated;
18198f04499SJan Kara 
182795fe54cSFam Zheng 	/* device weight, if non-zero, it overrides the default weight of
183795fe54cSFam Zheng 	 * bfq_group_data */
184795fe54cSFam Zheng 	int dev_weight;
185ea25da48SPaolo Valente 	/* weight of the queue */
186ea25da48SPaolo Valente 	int weight;
187ea25da48SPaolo Valente 	/* next weight if a change is in progress */
188ea25da48SPaolo Valente 	int new_weight;
189ea25da48SPaolo Valente 
190ea25da48SPaolo Valente 	/* original weight, used to implement weight boosting */
191ea25da48SPaolo Valente 	int orig_weight;
192ea25da48SPaolo Valente 
193ea25da48SPaolo Valente 	/* parent entity, for hierarchical scheduling */
194ea25da48SPaolo Valente 	struct bfq_entity *parent;
195ea25da48SPaolo Valente 
196ea25da48SPaolo Valente 	/*
197ea25da48SPaolo Valente 	 * For non-leaf nodes in the hierarchy, the associated
198ea25da48SPaolo Valente 	 * scheduler queue, %NULL on leaf nodes.
199ea25da48SPaolo Valente 	 */
200ea25da48SPaolo Valente 	struct bfq_sched_data *my_sched_data;
201ea25da48SPaolo Valente 	/* the scheduler queue this entity belongs to */
202ea25da48SPaolo Valente 	struct bfq_sched_data *sched_data;
203ea25da48SPaolo Valente 
204ea25da48SPaolo Valente 	/* flag, set to request a weight, ioprio or ioprio_class change  */
205ea25da48SPaolo Valente 	int prio_changed;
206ba7aeae5SPaolo Valente 
2071eb20620SYuwei Guan #ifdef CONFIG_BFQ_GROUP_IOSCHED
208ba7aeae5SPaolo Valente 	/* flag, set if the entity is counted in groups_with_pending_reqs */
209ba7aeae5SPaolo Valente 	bool in_groups_with_pending_reqs;
2101eb20620SYuwei Guan #endif
211430a67f9SPaolo Valente 
212430a67f9SPaolo Valente 	/* last child queue of entity created (for non-leaf entities) */
213430a67f9SPaolo Valente 	struct bfq_queue *last_bfqq_created;
214ea25da48SPaolo Valente };
215ea25da48SPaolo Valente 
216ea25da48SPaolo Valente struct bfq_group;
217ea25da48SPaolo Valente 
218ea25da48SPaolo Valente /**
219ea25da48SPaolo Valente  * struct bfq_ttime - per process thinktime stats.
220ea25da48SPaolo Valente  */
221ea25da48SPaolo Valente struct bfq_ttime {
222ea25da48SPaolo Valente 	/* completion time of the last request */
223ea25da48SPaolo Valente 	u64 last_end_request;
224ea25da48SPaolo Valente 
225ea25da48SPaolo Valente 	/* total process thinktime */
226ea25da48SPaolo Valente 	u64 ttime_total;
227ea25da48SPaolo Valente 	/* number of thinktime samples */
228ea25da48SPaolo Valente 	unsigned long ttime_samples;
229ea25da48SPaolo Valente 	/* average process thinktime */
230ea25da48SPaolo Valente 	u64 ttime_mean;
231ea25da48SPaolo Valente };
232ea25da48SPaolo Valente 
233ea25da48SPaolo Valente /**
234ea25da48SPaolo Valente  * struct bfq_queue - leaf schedulable entity.
235ea25da48SPaolo Valente  *
236ea25da48SPaolo Valente  * A bfq_queue is a leaf request queue; it can be associated with an
237ea25da48SPaolo Valente  * io_context or more, if it is async or shared between cooperating
2389778369aSPaolo Valente  * processes. Besides, it contains I/O requests for only one actuator
2399778369aSPaolo Valente  * (an io_context is associated with a different bfq_queue for each
2409778369aSPaolo Valente  * actuator it generates I/O for). @cgroup holds a reference to the
2419778369aSPaolo Valente  * cgroup, to be sure that it does not disappear while a bfqq still
2429778369aSPaolo Valente  * references it (mostly to avoid races between request issuing and
2439778369aSPaolo Valente  * task migration followed by cgroup destruction).  All the fields are
2449778369aSPaolo Valente  * protected by the queue lock of the containing bfqd.
245ea25da48SPaolo Valente  */
246ea25da48SPaolo Valente struct bfq_queue {
247ea25da48SPaolo Valente 	/* reference counter */
248ea25da48SPaolo Valente 	int ref;
249430a67f9SPaolo Valente 	/* counter of references from other queues for delayed stable merge */
250430a67f9SPaolo Valente 	int stable_ref;
251ea25da48SPaolo Valente 	/* parent bfq_data */
252ea25da48SPaolo Valente 	struct bfq_data *bfqd;
253ea25da48SPaolo Valente 
254ea25da48SPaolo Valente 	/* current ioprio and ioprio class */
255ea25da48SPaolo Valente 	unsigned short ioprio, ioprio_class;
256ea25da48SPaolo Valente 	/* next ioprio and ioprio class if a change is in progress */
257ea25da48SPaolo Valente 	unsigned short new_ioprio, new_ioprio_class;
258ea25da48SPaolo Valente 
2592341d662SPaolo Valente 	/* last total-service-time sample, see bfq_update_inject_limit() */
2602341d662SPaolo Valente 	u64 last_serv_time_ns;
2612341d662SPaolo Valente 	/* limit for request injection */
2622341d662SPaolo Valente 	unsigned int inject_limit;
2632341d662SPaolo Valente 	/* last time the inject limit has been decreased, in jiffies */
2642341d662SPaolo Valente 	unsigned long decrease_time_jif;
2652341d662SPaolo Valente 
266ea25da48SPaolo Valente 	/*
267ea25da48SPaolo Valente 	 * Shared bfq_queue if queue is cooperating with one or more
268ea25da48SPaolo Valente 	 * other queues.
269ea25da48SPaolo Valente 	 */
270ea25da48SPaolo Valente 	struct bfq_queue *new_bfqq;
271ea25da48SPaolo Valente 	/* request-position tree member (see bfq_group's @rq_pos_tree) */
272ea25da48SPaolo Valente 	struct rb_node pos_node;
273ea25da48SPaolo Valente 	/* request-position tree root (see bfq_group's @rq_pos_tree) */
274ea25da48SPaolo Valente 	struct rb_root *pos_root;
275ea25da48SPaolo Valente 
276ea25da48SPaolo Valente 	/* sorted list of pending requests */
277ea25da48SPaolo Valente 	struct rb_root sort_list;
278ea25da48SPaolo Valente 	/* if fifo isn't expired, next request to serve */
279ea25da48SPaolo Valente 	struct request *next_rq;
280ea25da48SPaolo Valente 	/* number of sync and async requests queued */
281ea25da48SPaolo Valente 	int queued[2];
282ea25da48SPaolo Valente 	/* number of pending metadata requests */
283ea25da48SPaolo Valente 	int meta_pending;
284ea25da48SPaolo Valente 	/* fifo list of requests in sort_list */
285ea25da48SPaolo Valente 	struct list_head fifo;
286ea25da48SPaolo Valente 
287ea25da48SPaolo Valente 	/* entity representing this queue in the scheduler */
288ea25da48SPaolo Valente 	struct bfq_entity entity;
289ea25da48SPaolo Valente 
2902d29c9f8SFederico Motta 	/* pointer to the weight counter associated with this entity */
2912d29c9f8SFederico Motta 	struct bfq_weight_counter *weight_counter;
2922d29c9f8SFederico Motta 
293ea25da48SPaolo Valente 	/* maximum budget allowed from the feedback mechanism */
294ea25da48SPaolo Valente 	int max_budget;
295ea25da48SPaolo Valente 	/* budget expiration (in jiffies) */
296ea25da48SPaolo Valente 	unsigned long budget_timeout;
297ea25da48SPaolo Valente 
298ea25da48SPaolo Valente 	/* number of requests on the dispatch list or inside driver */
299ea25da48SPaolo Valente 	int dispatched;
300ea25da48SPaolo Valente 
301ea25da48SPaolo Valente 	/* status flags */
302ea25da48SPaolo Valente 	unsigned long flags;
303ea25da48SPaolo Valente 
304ea25da48SPaolo Valente 	/* node for active/idle bfqq list inside parent bfqd */
305ea25da48SPaolo Valente 	struct list_head bfqq_list;
306ea25da48SPaolo Valente 
307ea25da48SPaolo Valente 	/* associated @bfq_ttime struct */
308ea25da48SPaolo Valente 	struct bfq_ttime ttime;
309ea25da48SPaolo Valente 
310eb2fd80fSPaolo Valente 	/* when bfqq started to do I/O within the last observation window */
311eb2fd80fSPaolo Valente 	u64 io_start_time;
312eb2fd80fSPaolo Valente 	/* how long bfqq has remained empty during the last observ. window */
313eb2fd80fSPaolo Valente 	u64 tot_idle_time;
314eb2fd80fSPaolo Valente 
315ea25da48SPaolo Valente 	/* bit vector: a 1 for each seeky requests in history */
316ea25da48SPaolo Valente 	u32 seek_history;
317ea25da48SPaolo Valente 
318ea25da48SPaolo Valente 	/* node for the device's burst list */
319ea25da48SPaolo Valente 	struct hlist_node burst_list_node;
320ea25da48SPaolo Valente 
321ea25da48SPaolo Valente 	/* position of the last request enqueued */
322ea25da48SPaolo Valente 	sector_t last_request_pos;
323ea25da48SPaolo Valente 
324ea25da48SPaolo Valente 	/* Number of consecutive pairs of request completion and
325ea25da48SPaolo Valente 	 * arrival, such that the queue becomes idle after the
326ea25da48SPaolo Valente 	 * completion, but the next request arrives within an idle
327ea25da48SPaolo Valente 	 * time slice; used only if the queue's IO_bound flag has been
328ea25da48SPaolo Valente 	 * cleared.
329ea25da48SPaolo Valente 	 */
330ea25da48SPaolo Valente 	unsigned int requests_within_timer;
331ea25da48SPaolo Valente 
332ea25da48SPaolo Valente 	/* pid of the process owning the queue, used for logging purposes */
333ea25da48SPaolo Valente 	pid_t pid;
334ea25da48SPaolo Valente 
335ea25da48SPaolo Valente 	/*
336ea25da48SPaolo Valente 	 * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
337ea25da48SPaolo Valente 	 * if the queue is shared.
338ea25da48SPaolo Valente 	 */
339ea25da48SPaolo Valente 	struct bfq_io_cq *bic;
340ea25da48SPaolo Valente 
341ea25da48SPaolo Valente 	/* current maximum weight-raising time for this queue */
342ea25da48SPaolo Valente 	unsigned long wr_cur_max_time;
343ea25da48SPaolo Valente 	/*
344ea25da48SPaolo Valente 	 * Minimum time instant such that, only if a new request is
345ea25da48SPaolo Valente 	 * enqueued after this time instant in an idle @bfq_queue with
346ea25da48SPaolo Valente 	 * no outstanding requests, then the task associated with the
347ea25da48SPaolo Valente 	 * queue it is deemed as soft real-time (see the comments on
348ea25da48SPaolo Valente 	 * the function bfq_bfqq_softrt_next_start())
349ea25da48SPaolo Valente 	 */
350ea25da48SPaolo Valente 	unsigned long soft_rt_next_start;
351ea25da48SPaolo Valente 	/*
352ea25da48SPaolo Valente 	 * Start time of the current weight-raising period if
353ea25da48SPaolo Valente 	 * the @bfq-queue is being weight-raised, otherwise
354ea25da48SPaolo Valente 	 * finish time of the last weight-raising period.
355ea25da48SPaolo Valente 	 */
356ea25da48SPaolo Valente 	unsigned long last_wr_start_finish;
357ea25da48SPaolo Valente 	/* factor by which the weight of this queue is multiplied */
358ea25da48SPaolo Valente 	unsigned int wr_coeff;
359ea25da48SPaolo Valente 	/*
360ea25da48SPaolo Valente 	 * Time of the last transition of the @bfq_queue from idle to
361ea25da48SPaolo Valente 	 * backlogged.
362ea25da48SPaolo Valente 	 */
363ea25da48SPaolo Valente 	unsigned long last_idle_bklogged;
364ea25da48SPaolo Valente 	/*
365ea25da48SPaolo Valente 	 * Cumulative service received from the @bfq_queue since the
366ea25da48SPaolo Valente 	 * last transition from idle to backlogged.
367ea25da48SPaolo Valente 	 */
368ea25da48SPaolo Valente 	unsigned long service_from_backlogged;
3698a8747dcSPaolo Valente 	/*
3708a8747dcSPaolo Valente 	 * Cumulative service received from the @bfq_queue since its
3718a8747dcSPaolo Valente 	 * last transition to weight-raised state.
3728a8747dcSPaolo Valente 	 */
3738a8747dcSPaolo Valente 	unsigned long service_from_wr;
374ea25da48SPaolo Valente 
375ea25da48SPaolo Valente 	/*
376ea25da48SPaolo Valente 	 * Value of wr start time when switching to soft rt
377ea25da48SPaolo Valente 	 */
378ea25da48SPaolo Valente 	unsigned long wr_start_at_switch_to_srt;
379ea25da48SPaolo Valente 
380ea25da48SPaolo Valente 	unsigned long split_time; /* time of last split */
3817b8fa3b9SPaolo Valente 
3827b8fa3b9SPaolo Valente 	unsigned long first_IO_time; /* time of first I/O for this queue */
383430a67f9SPaolo Valente 	unsigned long creation_time; /* when this queue is created */
384430a67f9SPaolo Valente 
38513a857a4SPaolo Valente 	/*
38613a857a4SPaolo Valente 	 * Pointer to the waker queue for this queue, i.e., to the
38713a857a4SPaolo Valente 	 * queue Q such that this queue happens to get new I/O right
38813a857a4SPaolo Valente 	 * after some I/O request of Q is completed. For details, see
38913a857a4SPaolo Valente 	 * the comments on the choice of the queue for injection in
39013a857a4SPaolo Valente 	 * bfq_select_queue().
39113a857a4SPaolo Valente 	 */
39213a857a4SPaolo Valente 	struct bfq_queue *waker_bfqq;
39371217df3SPaolo Valente 	/* pointer to the curr. tentative waker queue, see bfq_check_waker() */
39471217df3SPaolo Valente 	struct bfq_queue *tentative_waker_bfqq;
39571217df3SPaolo Valente 	/* number of times the same tentative waker has been detected */
39671217df3SPaolo Valente 	unsigned int num_waker_detections;
3971f18b700SJan Kara 	/* time when we started considering this waker */
3981f18b700SJan Kara 	u64 waker_detection_started;
39971217df3SPaolo Valente 
40013a857a4SPaolo Valente 	/* node for woken_list, see below */
40113a857a4SPaolo Valente 	struct hlist_node woken_list_node;
40213a857a4SPaolo Valente 	/*
40313a857a4SPaolo Valente 	 * Head of the list of the woken queues for this queue, i.e.,
40413a857a4SPaolo Valente 	 * of the list of the queues for which this queue is a waker
40513a857a4SPaolo Valente 	 * queue. This list is used to reset the waker_bfqq pointer in
40613a857a4SPaolo Valente 	 * the woken queues when this queue exits.
40713a857a4SPaolo Valente 	 */
40813a857a4SPaolo Valente 	struct hlist_head woken_list;
4099778369aSPaolo Valente 
4109778369aSPaolo Valente 	/* index of the actuator this queue is associated with */
4119778369aSPaolo Valente 	unsigned int actuator_idx;
412ea25da48SPaolo Valente };
413ea25da48SPaolo Valente 
414ea25da48SPaolo Valente /**
415a6123047SPaolo Valente * struct bfq_data - bfqq data unique and persistent for associated bfq_io_cq
416ea25da48SPaolo Valente */
417a6123047SPaolo Valente struct bfq_iocq_bfqq_data {
418ea25da48SPaolo Valente 	/*
419d5be3fefSPaolo Valente 	 * Snapshot of the has_short_time flag before merging; taken
420fd571df0SPaolo Valente 	 * to remember its values while the queue is merged, so as to
421d5be3fefSPaolo Valente 	 * be able to restore it in case of split.
422ea25da48SPaolo Valente 	 */
423d5be3fefSPaolo Valente 	bool saved_has_short_ttime;
424ea25da48SPaolo Valente 	/*
425ea25da48SPaolo Valente 	 * Same purpose as the previous two fields for the I/O bound
426ea25da48SPaolo Valente 	 * classification of a queue.
427ea25da48SPaolo Valente 	 */
428ea25da48SPaolo Valente 	bool saved_IO_bound;
429ea25da48SPaolo Valente 
430eb2fd80fSPaolo Valente 	u64 saved_io_start_time;
431eb2fd80fSPaolo Valente 	u64 saved_tot_idle_time;
432eb2fd80fSPaolo Valente 
433ea25da48SPaolo Valente 	/*
434fd571df0SPaolo Valente 	 * Same purpose as the previous fields for the values of the
435ea25da48SPaolo Valente 	 * field keeping the queue's belonging to a large burst
436ea25da48SPaolo Valente 	 */
437ea25da48SPaolo Valente 	bool saved_in_large_burst;
438ea25da48SPaolo Valente 	/*
439ea25da48SPaolo Valente 	 * True if the queue belonged to a burst list before its merge
440ea25da48SPaolo Valente 	 * with another cooperating queue.
441ea25da48SPaolo Valente 	 */
442ea25da48SPaolo Valente 	bool was_in_burst_list;
443ea25da48SPaolo Valente 
444ea25da48SPaolo Valente 	/*
445fffca087SFrancesco Pollicino 	 * Save the weight when a merge occurs, to be able
446fffca087SFrancesco Pollicino 	 * to restore it in case of split. If the weight is not
447fffca087SFrancesco Pollicino 	 * correctly resumed when the queue is recycled,
448fffca087SFrancesco Pollicino 	 * then the weight of the recycled queue could differ
449fffca087SFrancesco Pollicino 	 * from the weight of the original queue.
450fffca087SFrancesco Pollicino 	 */
451fffca087SFrancesco Pollicino 	unsigned int saved_weight;
452fffca087SFrancesco Pollicino 
453fffca087SFrancesco Pollicino 	/*
454ea25da48SPaolo Valente 	 * Similar to previous fields: save wr information.
455ea25da48SPaolo Valente 	 */
456ea25da48SPaolo Valente 	unsigned long saved_wr_coeff;
457ea25da48SPaolo Valente 	unsigned long saved_last_wr_start_finish;
458e673914dSPaolo Valente 	unsigned long saved_service_from_wr;
459ea25da48SPaolo Valente 	unsigned long saved_wr_start_at_switch_to_srt;
460ea25da48SPaolo Valente 	unsigned int saved_wr_cur_max_time;
461ea25da48SPaolo Valente 	struct bfq_ttime saved_ttime;
4625a5436b9SPaolo Valente 
4635a5436b9SPaolo Valente 	/* Save also injection state */
4645a5436b9SPaolo Valente 	u64 saved_last_serv_time_ns;
4655a5436b9SPaolo Valente 	unsigned int saved_inject_limit;
4665a5436b9SPaolo Valente 	unsigned long saved_decrease_time_jif;
467430a67f9SPaolo Valente 
468430a67f9SPaolo Valente 	/* candidate queue for a stable merge (due to close creation time) */
469430a67f9SPaolo Valente 	struct bfq_queue *stable_merge_bfqq;
470430a67f9SPaolo Valente 
471430a67f9SPaolo Valente 	bool stably_merged;	/* non splittable if true */
472a6123047SPaolo Valente };
473a6123047SPaolo Valente 
474a6123047SPaolo Valente /**
475a6123047SPaolo Valente  * struct bfq_io_cq - per (request_queue, io_context) structure.
476a6123047SPaolo Valente  */
477a6123047SPaolo Valente struct bfq_io_cq {
478a6123047SPaolo Valente 	/* associated io_cq structure */
479a6123047SPaolo Valente 	struct io_cq icq; /* must be the first member */
480a6123047SPaolo Valente 	/*
481a6123047SPaolo Valente 	 * Matrix of associated process queues: first row for async
482a6123047SPaolo Valente 	 * queues, second row sync queues. Each row contains one
483a6123047SPaolo Valente 	 * column for each actuator. An I/O request generated by the
484a6123047SPaolo Valente 	 * process is inserted into the queue pointed by bfqq[i][j] if
485a6123047SPaolo Valente 	 * the request is to be served by the j-th actuator of the
486a6123047SPaolo Valente 	 * drive, where i==0 or i==1, depending on whether the request
487a6123047SPaolo Valente 	 * is async or sync. So there is a distinct queue for each
488a6123047SPaolo Valente 	 * actuator.
489a6123047SPaolo Valente 	 */
490a6123047SPaolo Valente 	struct bfq_queue *bfqq[2][BFQ_MAX_ACTUATORS];
491a6123047SPaolo Valente 	/* per (request_queue, blkcg) ioprio */
492a6123047SPaolo Valente 	int ioprio;
493a6123047SPaolo Valente #ifdef CONFIG_BFQ_GROUP_IOSCHED
494a6123047SPaolo Valente 	uint64_t blkcg_serial_nr; /* the current blkcg serial */
495a6123047SPaolo Valente #endif
496a6123047SPaolo Valente 
497fd571df0SPaolo Valente 	/*
498fd571df0SPaolo Valente 	 * Persistent data for associated synchronous process queues
499fd571df0SPaolo Valente 	 * (one queue per actuator, see field bfqq above). In
500fd571df0SPaolo Valente 	 * particular, each of these queues may undergo a merge.
501fd571df0SPaolo Valente 	 */
502fd571df0SPaolo Valente 	struct bfq_iocq_bfqq_data bfqq_data[BFQ_MAX_ACTUATORS];
503a6123047SPaolo Valente 
504f9506673SJan Kara 	unsigned int requests;	/* Number of requests this process has in flight */
505ea25da48SPaolo Valente };
506ea25da48SPaolo Valente 
507ea25da48SPaolo Valente /**
508ea25da48SPaolo Valente  * struct bfq_data - per-device data structure.
509ea25da48SPaolo Valente  *
510ea25da48SPaolo Valente  * All the fields are protected by @lock.
511ea25da48SPaolo Valente  */
512ea25da48SPaolo Valente struct bfq_data {
513ea25da48SPaolo Valente 	/* device request queue */
514ea25da48SPaolo Valente 	struct request_queue *queue;
515ea25da48SPaolo Valente 	/* dispatch queue */
516ea25da48SPaolo Valente 	struct list_head dispatch;
517ea25da48SPaolo Valente 
518ea25da48SPaolo Valente 	/* root bfq_group for the device */
519ea25da48SPaolo Valente 	struct bfq_group *root_group;
520ea25da48SPaolo Valente 
521ea25da48SPaolo Valente 	/*
522ea25da48SPaolo Valente 	 * rbtree of weight counters of @bfq_queues, sorted by
523ea25da48SPaolo Valente 	 * weight. Used to keep track of whether all @bfq_queues have
524ea25da48SPaolo Valente 	 * the same weight. The tree contains one counter for each
525ea25da48SPaolo Valente 	 * distinct weight associated to some active and not
526ea25da48SPaolo Valente 	 * weight-raised @bfq_queue (see the comments to the functions
527ea25da48SPaolo Valente 	 * bfq_weights_tree_[add|remove] for further details).
528ea25da48SPaolo Valente 	 */
529fb53ac6cSPaolo Valente 	struct rb_root_cached queue_weights_tree;
530ba7aeae5SPaolo Valente 
5311eb20620SYuwei Guan #ifdef CONFIG_BFQ_GROUP_IOSCHED
532ea25da48SPaolo Valente 	/*
53371f8ca77SYu Kuai 	 * Number of groups with at least one process that
534ba7aeae5SPaolo Valente 	 * has at least one request waiting for completion. Note that
535ba7aeae5SPaolo Valente 	 * this accounts for also requests already dispatched, but not
536ba7aeae5SPaolo Valente 	 * yet completed. Therefore this number of groups may differ
537ba7aeae5SPaolo Valente 	 * (be larger) than the number of active groups, as a group is
538ba7aeae5SPaolo Valente 	 * considered active only if its corresponding entity has
53971f8ca77SYu Kuai 	 * queues with at least one request queued. This
540ba7aeae5SPaolo Valente 	 * number is used to decide whether a scenario is symmetric.
541ba7aeae5SPaolo Valente 	 * For a detailed explanation see comments on the computation
542ba7aeae5SPaolo Valente 	 * of the variable asymmetric_scenario in the function
543ba7aeae5SPaolo Valente 	 * bfq_better_to_idle().
544ba7aeae5SPaolo Valente 	 *
545ba7aeae5SPaolo Valente 	 * However, it is hard to compute this number exactly, for
54671f8ca77SYu Kuai 	 * groups with multiple processes. Consider a group
54771f8ca77SYu Kuai 	 * that is inactive, i.e., that has no process with
548ba7aeae5SPaolo Valente 	 * pending I/O inside BFQ queues. Then suppose that
549ba7aeae5SPaolo Valente 	 * num_groups_with_pending_reqs is still accounting for this
55071f8ca77SYu Kuai 	 * group, because the group has processes with some
551ba7aeae5SPaolo Valente 	 * I/O request still in flight. num_groups_with_pending_reqs
552ba7aeae5SPaolo Valente 	 * should be decremented when the in-flight request of the
55371f8ca77SYu Kuai 	 * last process is finally completed (assuming that
554ba7aeae5SPaolo Valente 	 * nothing else has changed for the group in the meantime, in
555ba7aeae5SPaolo Valente 	 * terms of composition of the group and active/inactive state of child
556ba7aeae5SPaolo Valente 	 * groups and processes). To accomplish this, an additional
557ba7aeae5SPaolo Valente 	 * pending-request counter must be added to entities, and must
558ba7aeae5SPaolo Valente 	 * be updated correctly. To avoid this additional field and operations,
559ba7aeae5SPaolo Valente 	 * we resort to the following tradeoff between simplicity and
560ba7aeae5SPaolo Valente 	 * accuracy: for an inactive group that is still counted in
561ba7aeae5SPaolo Valente 	 * num_groups_with_pending_reqs, we decrement
56271f8ca77SYu Kuai 	 * num_groups_with_pending_reqs when the first
563ba7aeae5SPaolo Valente 	 * process of the group remains with no request waiting for
564ba7aeae5SPaolo Valente 	 * completion.
565ba7aeae5SPaolo Valente 	 *
566ba7aeae5SPaolo Valente 	 * Even this simpler decrement strategy requires a little
567ba7aeae5SPaolo Valente 	 * carefulness: to avoid multiple decrements, we flag a group,
568ba7aeae5SPaolo Valente 	 * more precisely an entity representing a group, as still
569ba7aeae5SPaolo Valente 	 * counted in num_groups_with_pending_reqs when it becomes
57071f8ca77SYu Kuai 	 * inactive. Then, when the first queue of the
571ba7aeae5SPaolo Valente 	 * entity remains with no request waiting for completion,
572ba7aeae5SPaolo Valente 	 * num_groups_with_pending_reqs is decremented, and this flag
573ba7aeae5SPaolo Valente 	 * is reset. After this flag is reset for the entity,
574ba7aeae5SPaolo Valente 	 * num_groups_with_pending_reqs won't be decremented any
57571f8ca77SYu Kuai 	 * longer in case a new queue of the entity remains
576ba7aeae5SPaolo Valente 	 * with no request waiting for completion.
577ea25da48SPaolo Valente 	 */
578ba7aeae5SPaolo Valente 	unsigned int num_groups_with_pending_reqs;
5791eb20620SYuwei Guan #endif
580ea25da48SPaolo Valente 
581ea25da48SPaolo Valente 	/*
58273d58118SPaolo Valente 	 * Per-class (RT, BE, IDLE) number of bfq_queues containing
58373d58118SPaolo Valente 	 * requests (including the queue in service, even if it is
58473d58118SPaolo Valente 	 * idling).
585ea25da48SPaolo Valente 	 */
58673d58118SPaolo Valente 	unsigned int busy_queues[3];
587ea25da48SPaolo Valente 	/* number of weight-raised busy @bfq_queues */
588ea25da48SPaolo Valente 	int wr_busy_queues;
589ea25da48SPaolo Valente 	/* number of queued requests */
590ea25da48SPaolo Valente 	int queued;
591ea25da48SPaolo Valente 	/* number of requests dispatched and waiting for completion */
592*2d31c684SDavide Zini 	int tot_rq_in_driver;
593*2d31c684SDavide Zini 	/*
594*2d31c684SDavide Zini 	 * number of requests dispatched and waiting for completion
595*2d31c684SDavide Zini 	 * for each actuator
596*2d31c684SDavide Zini 	 */
597*2d31c684SDavide Zini 	int rq_in_driver[BFQ_MAX_ACTUATORS];
598ea25da48SPaolo Valente 
5998cacc5abSPaolo Valente 	/* true if the device is non rotational and performs queueing */
6008cacc5abSPaolo Valente 	bool nonrot_with_queueing;
6018cacc5abSPaolo Valente 
602ea25da48SPaolo Valente 	/*
603ea25da48SPaolo Valente 	 * Maximum number of requests in driver in the last
604ea25da48SPaolo Valente 	 * @hw_tag_samples completed requests.
605ea25da48SPaolo Valente 	 */
606ea25da48SPaolo Valente 	int max_rq_in_driver;
607ea25da48SPaolo Valente 	/* number of samples used to calculate hw_tag */
608ea25da48SPaolo Valente 	int hw_tag_samples;
609ea25da48SPaolo Valente 	/* flag set to one if the driver is showing a queueing behavior */
610ea25da48SPaolo Valente 	int hw_tag;
611ea25da48SPaolo Valente 
612ea25da48SPaolo Valente 	/* number of budgets assigned */
613ea25da48SPaolo Valente 	int budgets_assigned;
614ea25da48SPaolo Valente 
615ea25da48SPaolo Valente 	/*
616ea25da48SPaolo Valente 	 * Timer set when idling (waiting) for the next request from
617ea25da48SPaolo Valente 	 * the queue in service.
618ea25da48SPaolo Valente 	 */
619ea25da48SPaolo Valente 	struct hrtimer idle_slice_timer;
620ea25da48SPaolo Valente 
621ea25da48SPaolo Valente 	/* bfq_queue in service */
622ea25da48SPaolo Valente 	struct bfq_queue *in_service_queue;
623ea25da48SPaolo Valente 
624ea25da48SPaolo Valente 	/* on-disk position of the last served request */
625ea25da48SPaolo Valente 	sector_t last_position;
626ea25da48SPaolo Valente 
627058fdeccSPaolo Valente 	/* position of the last served request for the in-service queue */
628058fdeccSPaolo Valente 	sector_t in_serv_last_pos;
629058fdeccSPaolo Valente 
630ea25da48SPaolo Valente 	/* time of last request completion (ns) */
631ea25da48SPaolo Valente 	u64 last_completion;
632ea25da48SPaolo Valente 
63313a857a4SPaolo Valente 	/* bfqq owning the last completed rq */
63413a857a4SPaolo Valente 	struct bfq_queue *last_completed_rq_bfqq;
63513a857a4SPaolo Valente 
636430a67f9SPaolo Valente 	/* last bfqq created, among those in the root group */
637430a67f9SPaolo Valente 	struct bfq_queue *last_bfqq_created;
638430a67f9SPaolo Valente 
6392341d662SPaolo Valente 	/* time of last transition from empty to non-empty (ns) */
6402341d662SPaolo Valente 	u64 last_empty_occupied_ns;
6412341d662SPaolo Valente 
6422341d662SPaolo Valente 	/*
6432341d662SPaolo Valente 	 * Flag set to activate the sampling of the total service time
6442341d662SPaolo Valente 	 * of a just-arrived first I/O request (see
6452341d662SPaolo Valente 	 * bfq_update_inject_limit()). This will cause the setting of
6462341d662SPaolo Valente 	 * waited_rq when the request is finally dispatched.
6472341d662SPaolo Valente 	 */
6482341d662SPaolo Valente 	bool wait_dispatch;
6492341d662SPaolo Valente 	/*
6502341d662SPaolo Valente 	 *  If set, then bfq_update_inject_limit() is invoked when
6512341d662SPaolo Valente 	 *  waited_rq is eventually completed.
6522341d662SPaolo Valente 	 */
6532341d662SPaolo Valente 	struct request *waited_rq;
6542341d662SPaolo Valente 	/*
6552341d662SPaolo Valente 	 * True if some request has been injected during the last service hole.
6562341d662SPaolo Valente 	 */
6572341d662SPaolo Valente 	bool rqs_injected;
6582341d662SPaolo Valente 
659ea25da48SPaolo Valente 	/* time of first rq dispatch in current observation interval (ns) */
660ea25da48SPaolo Valente 	u64 first_dispatch;
661ea25da48SPaolo Valente 	/* time of last rq dispatch in current observation interval (ns) */
662ea25da48SPaolo Valente 	u64 last_dispatch;
663ea25da48SPaolo Valente 
664ea25da48SPaolo Valente 	/* beginning of the last budget */
665ea25da48SPaolo Valente 	ktime_t last_budget_start;
666ea25da48SPaolo Valente 	/* beginning of the last idle slice */
667ea25da48SPaolo Valente 	ktime_t last_idling_start;
6682341d662SPaolo Valente 	unsigned long last_idling_start_jiffies;
669ea25da48SPaolo Valente 
670ea25da48SPaolo Valente 	/* number of samples in current observation interval */
671ea25da48SPaolo Valente 	int peak_rate_samples;
672ea25da48SPaolo Valente 	/* num of samples of seq dispatches in current observation interval */
673ea25da48SPaolo Valente 	u32 sequential_samples;
674ea25da48SPaolo Valente 	/* total num of sectors transferred in current observation interval */
675ea25da48SPaolo Valente 	u64 tot_sectors_dispatched;
676ea25da48SPaolo Valente 	/* max rq size seen during current observation interval (sectors) */
677ea25da48SPaolo Valente 	u32 last_rq_max_size;
678ea25da48SPaolo Valente 	/* time elapsed from first dispatch in current observ. interval (us) */
679ea25da48SPaolo Valente 	u64 delta_from_first;
680ea25da48SPaolo Valente 	/*
681ea25da48SPaolo Valente 	 * Current estimate of the device peak rate, measured in
682bc56e2caSPaolo Valente 	 * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
683ea25da48SPaolo Valente 	 * BFQ_RATE_SHIFT is performed to increase precision in
684ea25da48SPaolo Valente 	 * fixed-point calculations.
685ea25da48SPaolo Valente 	 */
686ea25da48SPaolo Valente 	u32 peak_rate;
687ea25da48SPaolo Valente 
688ea25da48SPaolo Valente 	/* maximum budget allotted to a bfq_queue before rescheduling */
689ea25da48SPaolo Valente 	int bfq_max_budget;
690ea25da48SPaolo Valente 
691*2d31c684SDavide Zini 	/*
692*2d31c684SDavide Zini 	 * List of all the bfq_queues active for a specific actuator
693*2d31c684SDavide Zini 	 * on the device. Keeping active queues separate on a
694*2d31c684SDavide Zini 	 * per-actuator basis helps implementing per-actuator
695*2d31c684SDavide Zini 	 * injection more efficiently.
696*2d31c684SDavide Zini 	 */
697*2d31c684SDavide Zini 	struct list_head active_list[BFQ_MAX_ACTUATORS];
698ea25da48SPaolo Valente 	/* list of all the bfq_queues idle on the device */
699ea25da48SPaolo Valente 	struct list_head idle_list;
700ea25da48SPaolo Valente 
701ea25da48SPaolo Valente 	/*
702ea25da48SPaolo Valente 	 * Timeout for async/sync requests; when it fires, requests
703ea25da48SPaolo Valente 	 * are served in fifo order.
704ea25da48SPaolo Valente 	 */
705ea25da48SPaolo Valente 	u64 bfq_fifo_expire[2];
706ea25da48SPaolo Valente 	/* weight of backward seeks wrt forward ones */
707ea25da48SPaolo Valente 	unsigned int bfq_back_penalty;
708ea25da48SPaolo Valente 	/* maximum allowed backward seek */
709ea25da48SPaolo Valente 	unsigned int bfq_back_max;
710ea25da48SPaolo Valente 	/* maximum idling time */
711ea25da48SPaolo Valente 	u32 bfq_slice_idle;
712ea25da48SPaolo Valente 
713ea25da48SPaolo Valente 	/* user-configured max budget value (0 for auto-tuning) */
714ea25da48SPaolo Valente 	int bfq_user_max_budget;
715ea25da48SPaolo Valente 	/*
716ea25da48SPaolo Valente 	 * Timeout for bfq_queues to consume their budget; used to
717ea25da48SPaolo Valente 	 * prevent seeky queues from imposing long latencies to
718ea25da48SPaolo Valente 	 * sequential or quasi-sequential ones (this also implies that
719ea25da48SPaolo Valente 	 * seeky queues cannot receive guarantees in the service
720ea25da48SPaolo Valente 	 * domain; after a timeout they are charged for the time they
721ea25da48SPaolo Valente 	 * have been in service, to preserve fairness among them, but
722ea25da48SPaolo Valente 	 * without service-domain guarantees).
723ea25da48SPaolo Valente 	 */
724ea25da48SPaolo Valente 	unsigned int bfq_timeout;
725ea25da48SPaolo Valente 
726ea25da48SPaolo Valente 	/*
727ea25da48SPaolo Valente 	 * Force device idling whenever needed to provide accurate
728ea25da48SPaolo Valente 	 * service guarantees, without caring about throughput
729ea25da48SPaolo Valente 	 * issues. CAVEAT: this may even increase latencies, in case
730ea25da48SPaolo Valente 	 * of useless idling for processes that did stop doing I/O.
731ea25da48SPaolo Valente 	 */
732ea25da48SPaolo Valente 	bool strict_guarantees;
733ea25da48SPaolo Valente 
734ea25da48SPaolo Valente 	/*
735ea25da48SPaolo Valente 	 * Last time at which a queue entered the current burst of
736ea25da48SPaolo Valente 	 * queues being activated shortly after each other; for more
737ea25da48SPaolo Valente 	 * details about this and the following parameters related to
738ea25da48SPaolo Valente 	 * a burst of activations, see the comments on the function
739ea25da48SPaolo Valente 	 * bfq_handle_burst.
740ea25da48SPaolo Valente 	 */
741ea25da48SPaolo Valente 	unsigned long last_ins_in_burst;
742ea25da48SPaolo Valente 	/*
743ea25da48SPaolo Valente 	 * Reference time interval used to decide whether a queue has
744ea25da48SPaolo Valente 	 * been activated shortly after @last_ins_in_burst.
745ea25da48SPaolo Valente 	 */
746ea25da48SPaolo Valente 	unsigned long bfq_burst_interval;
747ea25da48SPaolo Valente 	/* number of queues in the current burst of queue activations */
748ea25da48SPaolo Valente 	int burst_size;
749ea25da48SPaolo Valente 
750ea25da48SPaolo Valente 	/* common parent entity for the queues in the burst */
751ea25da48SPaolo Valente 	struct bfq_entity *burst_parent_entity;
752ea25da48SPaolo Valente 	/* Maximum burst size above which the current queue-activation
753ea25da48SPaolo Valente 	 * burst is deemed as 'large'.
754ea25da48SPaolo Valente 	 */
755ea25da48SPaolo Valente 	unsigned long bfq_large_burst_thresh;
756ea25da48SPaolo Valente 	/* true if a large queue-activation burst is in progress */
757ea25da48SPaolo Valente 	bool large_burst;
758ea25da48SPaolo Valente 	/*
759ea25da48SPaolo Valente 	 * Head of the burst list (as for the above fields, more
760ea25da48SPaolo Valente 	 * details in the comments on the function bfq_handle_burst).
761ea25da48SPaolo Valente 	 */
762ea25da48SPaolo Valente 	struct hlist_head burst_list;
763ea25da48SPaolo Valente 
764ea25da48SPaolo Valente 	/* if set to true, low-latency heuristics are enabled */
765ea25da48SPaolo Valente 	bool low_latency;
766ea25da48SPaolo Valente 	/*
767ea25da48SPaolo Valente 	 * Maximum factor by which the weight of a weight-raised queue
768ea25da48SPaolo Valente 	 * is multiplied.
769ea25da48SPaolo Valente 	 */
770ea25da48SPaolo Valente 	unsigned int bfq_wr_coeff;
771ea25da48SPaolo Valente 
772ea25da48SPaolo Valente 	/* Maximum weight-raising duration for soft real-time processes */
773ea25da48SPaolo Valente 	unsigned int bfq_wr_rt_max_time;
774ea25da48SPaolo Valente 	/*
775ea25da48SPaolo Valente 	 * Minimum idle period after which weight-raising may be
776ea25da48SPaolo Valente 	 * reactivated for a queue (in jiffies).
777ea25da48SPaolo Valente 	 */
778ea25da48SPaolo Valente 	unsigned int bfq_wr_min_idle_time;
779ea25da48SPaolo Valente 	/*
780ea25da48SPaolo Valente 	 * Minimum period between request arrivals after which
781ea25da48SPaolo Valente 	 * weight-raising may be reactivated for an already busy async
782ea25da48SPaolo Valente 	 * queue (in jiffies).
783ea25da48SPaolo Valente 	 */
784ea25da48SPaolo Valente 	unsigned long bfq_wr_min_inter_arr_async;
785ea25da48SPaolo Valente 
786ea25da48SPaolo Valente 	/* Max service-rate for a soft real-time queue, in sectors/sec */
787ea25da48SPaolo Valente 	unsigned int bfq_wr_max_softrt_rate;
788ea25da48SPaolo Valente 	/*
789e24f1c24SPaolo Valente 	 * Cached value of the product ref_rate*ref_wr_duration, used
790e24f1c24SPaolo Valente 	 * for computing the maximum duration of weight raising
791e24f1c24SPaolo Valente 	 * automatically.
792ea25da48SPaolo Valente 	 */
793e24f1c24SPaolo Valente 	u64 rate_dur_prod;
794ea25da48SPaolo Valente 
795ea25da48SPaolo Valente 	/* fallback dummy bfqq for extreme OOM conditions */
796ea25da48SPaolo Valente 	struct bfq_queue oom_bfqq;
797ea25da48SPaolo Valente 
798ea25da48SPaolo Valente 	spinlock_t lock;
799ea25da48SPaolo Valente 
800ea25da48SPaolo Valente 	/*
801ea25da48SPaolo Valente 	 * bic associated with the task issuing current bio for
802ea25da48SPaolo Valente 	 * merging. This and the next field are used as a support to
803ea25da48SPaolo Valente 	 * be able to perform the bic lookup, needed by bio-merge
804ea25da48SPaolo Valente 	 * functions, before the scheduler lock is taken, and thus
805ea25da48SPaolo Valente 	 * avoid taking the request-queue lock while the scheduler
806ea25da48SPaolo Valente 	 * lock is being held.
807ea25da48SPaolo Valente 	 */
808ea25da48SPaolo Valente 	struct bfq_io_cq *bio_bic;
809ea25da48SPaolo Valente 	/* bfqq associated with the task issuing current bio for merging */
810ea25da48SPaolo Valente 	struct bfq_queue *bio_bfqq;
811a52a69eaSPaolo Valente 
812a52a69eaSPaolo Valente 	/*
813a52a69eaSPaolo Valente 	 * Depth limits used in bfq_limit_depth (see comments on the
814a52a69eaSPaolo Valente 	 * function)
815a52a69eaSPaolo Valente 	 */
816a52a69eaSPaolo Valente 	unsigned int word_depths[2][2];
81744dfa279SJan Kara 	unsigned int full_depth_shift;
8189778369aSPaolo Valente 
8199778369aSPaolo Valente 	/*
8209778369aSPaolo Valente 	 * Number of independent actuators. This is equal to 1 in
8219778369aSPaolo Valente 	 * case of single-actuator drives.
8229778369aSPaolo Valente 	 */
8239778369aSPaolo Valente 	unsigned int num_actuators;
8244fdb3b9fSFederico Gavioli 	/*
8254fdb3b9fSFederico Gavioli 	 * Disk independent access ranges for each actuator
8264fdb3b9fSFederico Gavioli 	 * in this device.
8274fdb3b9fSFederico Gavioli 	 */
8284fdb3b9fSFederico Gavioli 	sector_t sector[BFQ_MAX_ACTUATORS];
8294fdb3b9fSFederico Gavioli 	sector_t nr_sectors[BFQ_MAX_ACTUATORS];
8304fdb3b9fSFederico Gavioli 	struct blk_independent_access_range ia_ranges[BFQ_MAX_ACTUATORS];
831*2d31c684SDavide Zini 
832*2d31c684SDavide Zini 	/*
833*2d31c684SDavide Zini 	 * If the number of I/O requests queued in the device for a
834*2d31c684SDavide Zini 	 * given actuator is below next threshold, then the actuator
835*2d31c684SDavide Zini 	 * is deemed as underutilized. If this condition is found to
836*2d31c684SDavide Zini 	 * hold for some actuator upon a dispatch, but (i) the
837*2d31c684SDavide Zini 	 * in-service queue does not contain I/O for that actuator,
838*2d31c684SDavide Zini 	 * while (ii) some other queue does contain I/O for that
839*2d31c684SDavide Zini 	 * actuator, then the head I/O request of the latter queue is
840*2d31c684SDavide Zini 	 * returned (injected), instead of the head request of the
841*2d31c684SDavide Zini 	 * currently in-service queue.
842*2d31c684SDavide Zini 	 *
843*2d31c684SDavide Zini 	 * We set the threshold, empirically, to the minimum possible
844*2d31c684SDavide Zini 	 * value for which an actuator is fully utilized, or close to
845*2d31c684SDavide Zini 	 * be fully utilized. By doing so, injected I/O 'steals' as
846*2d31c684SDavide Zini 	 * few drive-queue slots as possibile to the in-service
847*2d31c684SDavide Zini 	 * queue. This reduces as much as possible the probability
848*2d31c684SDavide Zini 	 * that the service of I/O from the in-service bfq_queue gets
849*2d31c684SDavide Zini 	 * delayed because of slot exhaustion, i.e., because all the
850*2d31c684SDavide Zini 	 * slots of the drive queue are filled with I/O injected from
851*2d31c684SDavide Zini 	 * other queues (NCQ provides for 32 slots).
852*2d31c684SDavide Zini 	 */
853*2d31c684SDavide Zini 	unsigned int actuator_load_threshold;
854ea25da48SPaolo Valente };
855ea25da48SPaolo Valente 
856ea25da48SPaolo Valente enum bfqq_state_flags {
857ea25da48SPaolo Valente 	BFQQF_just_created = 0,	/* queue just allocated */
858ea25da48SPaolo Valente 	BFQQF_busy,		/* has requests or is in service */
859ea25da48SPaolo Valente 	BFQQF_wait_request,	/* waiting for a request */
860ea25da48SPaolo Valente 	BFQQF_non_blocking_wait_rq, /*
861ea25da48SPaolo Valente 				     * waiting for a request
862ea25da48SPaolo Valente 				     * without idling the device
863ea25da48SPaolo Valente 				     */
864ea25da48SPaolo Valente 	BFQQF_fifo_expire,	/* FIFO checked in this slice */
865d5be3fefSPaolo Valente 	BFQQF_has_short_ttime,	/* queue has a short think time */
866ea25da48SPaolo Valente 	BFQQF_sync,		/* synchronous queue */
867ea25da48SPaolo Valente 	BFQQF_IO_bound,		/*
868ea25da48SPaolo Valente 				 * bfqq has timed-out at least once
869ea25da48SPaolo Valente 				 * having consumed at most 2/10 of
870ea25da48SPaolo Valente 				 * its budget
871ea25da48SPaolo Valente 				 */
872ea25da48SPaolo Valente 	BFQQF_in_large_burst,	/*
873ea25da48SPaolo Valente 				 * bfqq activated in a large burst,
874ea25da48SPaolo Valente 				 * see comments to bfq_handle_burst.
875ea25da48SPaolo Valente 				 */
876ea25da48SPaolo Valente 	BFQQF_softrt_update,	/*
877ea25da48SPaolo Valente 				 * may need softrt-next-start
878ea25da48SPaolo Valente 				 * update
879ea25da48SPaolo Valente 				 */
880ea25da48SPaolo Valente 	BFQQF_coop,		/* bfqq is shared */
88113a857a4SPaolo Valente 	BFQQF_split_coop,	/* shared bfqq will be split */
882ea25da48SPaolo Valente };
883ea25da48SPaolo Valente 
884ea25da48SPaolo Valente #define BFQ_BFQQ_FNS(name)						\
885ea25da48SPaolo Valente void bfq_mark_bfqq_##name(struct bfq_queue *bfqq);			\
886ea25da48SPaolo Valente void bfq_clear_bfqq_##name(struct bfq_queue *bfqq);			\
887ea25da48SPaolo Valente int bfq_bfqq_##name(const struct bfq_queue *bfqq);
888ea25da48SPaolo Valente 
889ea25da48SPaolo Valente BFQ_BFQQ_FNS(just_created);
890ea25da48SPaolo Valente BFQ_BFQQ_FNS(busy);
891ea25da48SPaolo Valente BFQ_BFQQ_FNS(wait_request);
892ea25da48SPaolo Valente BFQ_BFQQ_FNS(non_blocking_wait_rq);
893ea25da48SPaolo Valente BFQ_BFQQ_FNS(fifo_expire);
894d5be3fefSPaolo Valente BFQ_BFQQ_FNS(has_short_ttime);
895ea25da48SPaolo Valente BFQ_BFQQ_FNS(sync);
896ea25da48SPaolo Valente BFQ_BFQQ_FNS(IO_bound);
897ea25da48SPaolo Valente BFQ_BFQQ_FNS(in_large_burst);
898ea25da48SPaolo Valente BFQ_BFQQ_FNS(coop);
899ea25da48SPaolo Valente BFQ_BFQQ_FNS(split_coop);
900ea25da48SPaolo Valente BFQ_BFQQ_FNS(softrt_update);
901ea25da48SPaolo Valente #undef BFQ_BFQQ_FNS
902ea25da48SPaolo Valente 
903ea25da48SPaolo Valente /* Expiration reasons. */
904ea25da48SPaolo Valente enum bfqq_expiration {
905ea25da48SPaolo Valente 	BFQQE_TOO_IDLE = 0,		/*
906ea25da48SPaolo Valente 					 * queue has been idling for
907ea25da48SPaolo Valente 					 * too long
908ea25da48SPaolo Valente 					 */
909ea25da48SPaolo Valente 	BFQQE_BUDGET_TIMEOUT,	/* budget took too long to be used */
910ea25da48SPaolo Valente 	BFQQE_BUDGET_EXHAUSTED,	/* budget consumed */
911ea25da48SPaolo Valente 	BFQQE_NO_MORE_REQUESTS,	/* the queue has no more requests */
912ea25da48SPaolo Valente 	BFQQE_PREEMPTED		/* preemption in progress */
913ea25da48SPaolo Valente };
914ea25da48SPaolo Valente 
915c0ce79dcSChristoph Hellwig struct bfq_stat {
916c0ce79dcSChristoph Hellwig 	struct percpu_counter		cpu_cnt;
917c0ce79dcSChristoph Hellwig 	atomic64_t			aux_cnt;
918c0ce79dcSChristoph Hellwig };
919c0ce79dcSChristoph Hellwig 
920ea25da48SPaolo Valente struct bfqg_stats {
921fd41e603STejun Heo 	/* basic stats */
922fd41e603STejun Heo 	struct blkg_rwstat		bytes;
923fd41e603STejun Heo 	struct blkg_rwstat		ios;
9248060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
925ea25da48SPaolo Valente 	/* number of ios merged */
926ea25da48SPaolo Valente 	struct blkg_rwstat		merged;
927ea25da48SPaolo Valente 	/* total time spent on device in ns, may not be accurate w/ queueing */
928ea25da48SPaolo Valente 	struct blkg_rwstat		service_time;
929ea25da48SPaolo Valente 	/* total time spent waiting in scheduler queue in ns */
930ea25da48SPaolo Valente 	struct blkg_rwstat		wait_time;
931ea25da48SPaolo Valente 	/* number of IOs queued up */
932ea25da48SPaolo Valente 	struct blkg_rwstat		queued;
933ea25da48SPaolo Valente 	/* total disk time and nr sectors dispatched by this group */
934c0ce79dcSChristoph Hellwig 	struct bfq_stat		time;
935ea25da48SPaolo Valente 	/* sum of number of ios queued across all samples */
936c0ce79dcSChristoph Hellwig 	struct bfq_stat		avg_queue_size_sum;
937ea25da48SPaolo Valente 	/* count of samples taken for average */
938c0ce79dcSChristoph Hellwig 	struct bfq_stat		avg_queue_size_samples;
939ea25da48SPaolo Valente 	/* how many times this group has been removed from service tree */
940c0ce79dcSChristoph Hellwig 	struct bfq_stat		dequeue;
941ea25da48SPaolo Valente 	/* total time spent waiting for it to be assigned a timeslice. */
942c0ce79dcSChristoph Hellwig 	struct bfq_stat		group_wait_time;
943ea25da48SPaolo Valente 	/* time spent idling for this blkcg_gq */
944c0ce79dcSChristoph Hellwig 	struct bfq_stat		idle_time;
945ea25da48SPaolo Valente 	/* total time with empty current active q with other requests queued */
946c0ce79dcSChristoph Hellwig 	struct bfq_stat		empty_time;
947ea25da48SPaolo Valente 	/* fields after this shouldn't be cleared on stat reset */
94884c7afceSOmar Sandoval 	u64				start_group_wait_time;
94984c7afceSOmar Sandoval 	u64				start_idle_time;
95084c7afceSOmar Sandoval 	u64				start_empty_time;
951ea25da48SPaolo Valente 	uint16_t			flags;
9528060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
953ea25da48SPaolo Valente };
954ea25da48SPaolo Valente 
955ea25da48SPaolo Valente #ifdef CONFIG_BFQ_GROUP_IOSCHED
956ea25da48SPaolo Valente 
957ea25da48SPaolo Valente /*
958ea25da48SPaolo Valente  * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
959ea25da48SPaolo Valente  *
960ea25da48SPaolo Valente  * @ps: @blkcg_policy_storage that this structure inherits
961ea25da48SPaolo Valente  * @weight: weight of the bfq_group
962ea25da48SPaolo Valente  */
963ea25da48SPaolo Valente struct bfq_group_data {
964ea25da48SPaolo Valente 	/* must be the first member */
965ea25da48SPaolo Valente 	struct blkcg_policy_data pd;
966ea25da48SPaolo Valente 
967ea25da48SPaolo Valente 	unsigned int weight;
968ea25da48SPaolo Valente };
969ea25da48SPaolo Valente 
970ea25da48SPaolo Valente /**
971ea25da48SPaolo Valente  * struct bfq_group - per (device, cgroup) data structure.
972ea25da48SPaolo Valente  * @entity: schedulable entity to insert into the parent group sched_data.
973ea25da48SPaolo Valente  * @sched_data: own sched_data, to contain child entities (they may be
974ea25da48SPaolo Valente  *              both bfq_queues and bfq_groups).
975ea25da48SPaolo Valente  * @bfqd: the bfq_data for the device this group acts upon.
976ea25da48SPaolo Valente  * @async_bfqq: array of async queues for all the tasks belonging to
977ea25da48SPaolo Valente  *              the group, one queue per ioprio value per ioprio_class,
978ea25da48SPaolo Valente  *              except for the idle class that has only one queue.
979ea25da48SPaolo Valente  * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
980ea25da48SPaolo Valente  * @my_entity: pointer to @entity, %NULL for the toplevel group; used
981ea25da48SPaolo Valente  *             to avoid too many special cases during group creation/
982ea25da48SPaolo Valente  *             migration.
983ea25da48SPaolo Valente  * @stats: stats for this bfqg.
984ea25da48SPaolo Valente  * @active_entities: number of active entities belonging to the group;
985ea25da48SPaolo Valente  *                   unused for the root group. Used to know whether there
986ea25da48SPaolo Valente  *                   are groups with more than one active @bfq_entity
987ea25da48SPaolo Valente  *                   (see the comments to the function
988ea25da48SPaolo Valente  *                   bfq_bfqq_may_idle()).
989ea25da48SPaolo Valente  * @rq_pos_tree: rbtree sorted by next_request position, used when
990ea25da48SPaolo Valente  *               determining if two or more queues have interleaving
991ea25da48SPaolo Valente  *               requests (see bfq_find_close_cooperator()).
992ea25da48SPaolo Valente  *
993ea25da48SPaolo Valente  * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
994ea25da48SPaolo Valente  * there is a set of bfq_groups, each one collecting the lower-level
995ea25da48SPaolo Valente  * entities belonging to the group that are acting on the same device.
996ea25da48SPaolo Valente  *
997ea25da48SPaolo Valente  * Locking works as follows:
998ea25da48SPaolo Valente  *    o @bfqd is protected by the queue lock, RCU is used to access it
999ea25da48SPaolo Valente  *      from the readers.
1000ea25da48SPaolo Valente  *    o All the other fields are protected by the @bfqd queue lock.
1001ea25da48SPaolo Valente  */
1002ea25da48SPaolo Valente struct bfq_group {
1003ea25da48SPaolo Valente 	/* must be the first member */
1004ea25da48SPaolo Valente 	struct blkg_policy_data pd;
1005ea25da48SPaolo Valente 
10068f9bebc3SPaolo Valente 	/* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
10078f9bebc3SPaolo Valente 	char blkg_path[128];
10088f9bebc3SPaolo Valente 
10098f9bebc3SPaolo Valente 	/* reference counter (see comments in bfq_bic_update_cgroup) */
1010216f7647SYu Kuai 	refcount_t ref;
10118f9bebc3SPaolo Valente 
1012ea25da48SPaolo Valente 	struct bfq_entity entity;
1013ea25da48SPaolo Valente 	struct bfq_sched_data sched_data;
1014ea25da48SPaolo Valente 
1015aa625117SYu Kuai 	struct bfq_data *bfqd;
1016ea25da48SPaolo Valente 
10178b7fd741SDavide Zini 	struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS][BFQ_MAX_ACTUATORS];
10188b7fd741SDavide Zini 	struct bfq_queue *async_idle_bfqq[BFQ_MAX_ACTUATORS];
1019ea25da48SPaolo Valente 
1020ea25da48SPaolo Valente 	struct bfq_entity *my_entity;
1021ea25da48SPaolo Valente 
1022ea25da48SPaolo Valente 	int active_entities;
102360a6e10cSYu Kuai 	int num_queues_with_pending_reqs;
1024ea25da48SPaolo Valente 
1025ea25da48SPaolo Valente 	struct rb_root rq_pos_tree;
1026ea25da48SPaolo Valente 
1027ea25da48SPaolo Valente 	struct bfqg_stats stats;
1028ea25da48SPaolo Valente };
1029ea25da48SPaolo Valente 
1030ea25da48SPaolo Valente #else
1031ea25da48SPaolo Valente struct bfq_group {
10324d8340d0SPaolo Valente 	struct bfq_entity entity;
1033ea25da48SPaolo Valente 	struct bfq_sched_data sched_data;
1034ea25da48SPaolo Valente 
10358b7fd741SDavide Zini 	struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS][BFQ_MAX_ACTUATORS];
10368b7fd741SDavide Zini 	struct bfq_queue *async_idle_bfqq[BFQ_MAX_ACTUATORS];
1037ea25da48SPaolo Valente 
1038ea25da48SPaolo Valente 	struct rb_root rq_pos_tree;
1039ea25da48SPaolo Valente };
1040ea25da48SPaolo Valente #endif
1041ea25da48SPaolo Valente 
1042ea25da48SPaolo Valente /* --------------- main algorithm interface ----------------- */
1043ea25da48SPaolo Valente 
1044ea25da48SPaolo Valente #define BFQ_SERVICE_TREE_INIT	((struct bfq_service_tree)		\
1045ea25da48SPaolo Valente 				{ RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
1046ea25da48SPaolo Valente 
1047ea25da48SPaolo Valente extern const int bfq_timeout;
1048ea25da48SPaolo Valente 
10499778369aSPaolo Valente struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync,
10509778369aSPaolo Valente 				unsigned int actuator_idx);
10519778369aSPaolo Valente void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync,
10529778369aSPaolo Valente 				unsigned int actuator_idx);
1053ea25da48SPaolo Valente struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
1054ea25da48SPaolo Valente void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1055afdba146SYu Kuai void bfq_weights_tree_add(struct bfq_queue *bfqq);
1056afdba146SYu Kuai void bfq_weights_tree_remove(struct bfq_queue *bfqq);
1057ea25da48SPaolo Valente void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1058ea25da48SPaolo Valente 		     bool compensate, enum bfqq_expiration reason);
1059ea25da48SPaolo Valente void bfq_put_queue(struct bfq_queue *bfqq);
10603bc5e683SJan Kara void bfq_put_cooperator(struct bfq_queue *bfqq);
1061ea25da48SPaolo Valente void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
1062c8997736SPaolo Valente void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1063ea25da48SPaolo Valente void bfq_schedule_dispatch(struct bfq_data *bfqd);
1064ea25da48SPaolo Valente void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
1065ea25da48SPaolo Valente 
1066ea25da48SPaolo Valente /* ------------ end of main algorithm interface -------------- */
1067ea25da48SPaolo Valente 
1068ea25da48SPaolo Valente /* ---------------- cgroups-support interface ---------------- */
1069ea25da48SPaolo Valente 
1070fd41e603STejun Heo void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq);
1071dc469ba2SBart Van Assche void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf);
1072dc469ba2SBart Van Assche void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf);
107384c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
1074dc469ba2SBart Van Assche 				  u64 io_start_time_ns, blk_opf_t opf);
1075ea25da48SPaolo Valente void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
1076ea25da48SPaolo Valente void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
1077ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1078ea25da48SPaolo Valente 		   struct bfq_group *bfqg);
1079ea25da48SPaolo Valente 
1080c2090eabSYu Kuai #ifdef CONFIG_BFQ_CGROUP_DEBUG
1081c2090eabSYu Kuai void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
1082c2090eabSYu Kuai 			      blk_opf_t opf);
1083c2090eabSYu Kuai void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
1084c2090eabSYu Kuai void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
1085c2090eabSYu Kuai void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
1086c2090eabSYu Kuai #endif
1087c2090eabSYu Kuai 
1088ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
1089ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
1090ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd);
10914e54a249SJan Kara struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio);
1092ea25da48SPaolo Valente struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
1093ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1094ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
10958f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg);
1096ea25da48SPaolo Valente 
1097ea25da48SPaolo Valente #ifdef CONFIG_BFQ_GROUP_IOSCHED
1098659b3394SJens Axboe extern struct cftype bfq_blkcg_legacy_files[];
1099659b3394SJens Axboe extern struct cftype bfq_blkg_files[];
1100ea25da48SPaolo Valente extern struct blkcg_policy blkcg_policy_bfq;
1101ea25da48SPaolo Valente #endif
1102ea25da48SPaolo Valente 
1103ea25da48SPaolo Valente /* ------------- end of cgroups-support interface ------------- */
1104ea25da48SPaolo Valente 
1105ea25da48SPaolo Valente /* - interface of the internal hierarchical B-WF2Q+ scheduler - */
1106ea25da48SPaolo Valente 
1107ea25da48SPaolo Valente #ifdef CONFIG_BFQ_GROUP_IOSCHED
1108ea25da48SPaolo Valente /* both next loops stop at one of the child entities of the root group */
1109ea25da48SPaolo Valente #define for_each_entity(entity)	\
1110ea25da48SPaolo Valente 	for (; entity ; entity = entity->parent)
1111ea25da48SPaolo Valente 
1112ea25da48SPaolo Valente /*
1113ea25da48SPaolo Valente  * For each iteration, compute parent in advance, so as to be safe if
1114ea25da48SPaolo Valente  * entity is deallocated during the iteration. Such a deallocation may
1115ea25da48SPaolo Valente  * happen as a consequence of a bfq_put_queue that frees the bfq_queue
1116ea25da48SPaolo Valente  * containing entity.
1117ea25da48SPaolo Valente  */
1118ea25da48SPaolo Valente #define for_each_entity_safe(entity, parent) \
1119ea25da48SPaolo Valente 	for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
1120ea25da48SPaolo Valente 
1121ea25da48SPaolo Valente #else /* CONFIG_BFQ_GROUP_IOSCHED */
1122ea25da48SPaolo Valente /*
1123ea25da48SPaolo Valente  * Next two macros are fake loops when cgroups support is not
1124ea25da48SPaolo Valente  * enabled. I fact, in such a case, there is only one level to go up
1125ea25da48SPaolo Valente  * (to reach the root group).
1126ea25da48SPaolo Valente  */
1127ea25da48SPaolo Valente #define for_each_entity(entity)	\
1128ea25da48SPaolo Valente 	for (; entity ; entity = NULL)
1129ea25da48SPaolo Valente 
1130ea25da48SPaolo Valente #define for_each_entity_safe(entity, parent) \
1131ea25da48SPaolo Valente 	for (parent = NULL; entity ; entity = parent)
1132ea25da48SPaolo Valente #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1133ea25da48SPaolo Valente 
1134ea25da48SPaolo Valente struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
113573d58118SPaolo Valente unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
1136ea25da48SPaolo Valente struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
1137ea25da48SPaolo Valente struct bfq_entity *bfq_entity_of(struct rb_node *node);
1138ea25da48SPaolo Valente unsigned short bfq_ioprio_to_weight(int ioprio);
1139ea25da48SPaolo Valente void bfq_put_idle_entity(struct bfq_service_tree *st,
1140ea25da48SPaolo Valente 			 struct bfq_entity *entity);
1141ea25da48SPaolo Valente struct bfq_service_tree *
1142ea25da48SPaolo Valente __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
1143431b17f9SPaolo Valente 				struct bfq_entity *entity,
1144431b17f9SPaolo Valente 				bool update_class_too);
1145ea25da48SPaolo Valente void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
1146ea25da48SPaolo Valente void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1147ea25da48SPaolo Valente 			  unsigned long time_ms);
1148ea25da48SPaolo Valente bool __bfq_deactivate_entity(struct bfq_entity *entity,
1149ea25da48SPaolo Valente 			     bool ins_into_idle_tree);
1150ea25da48SPaolo Valente bool next_queue_may_preempt(struct bfq_data *bfqd);
1151ea25da48SPaolo Valente struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
1152eed47d19SPaolo Valente bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
1153ea25da48SPaolo Valente void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1154ea25da48SPaolo Valente 			 bool ins_into_idle_tree, bool expiration);
1155ea25da48SPaolo Valente void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
115680294c3bSPaolo Valente void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
115780294c3bSPaolo Valente 		      bool expiration);
1158d322f355SYu Kuai void bfq_del_bfqq_busy(struct bfq_queue *bfqq, bool expiration);
1159d322f355SYu Kuai void bfq_add_bfqq_busy(struct bfq_queue *bfqq);
11603d89bd12SYu Kuai void bfq_add_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq);
11613d89bd12SYu Kuai void bfq_del_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq);
1162ea25da48SPaolo Valente 
1163ea25da48SPaolo Valente /* --------------- end of interface of B-WF2Q+ ---------------- */
1164ea25da48SPaolo Valente 
1165ea25da48SPaolo Valente /* Logging facilities. */
bfq_bfqq_name(struct bfq_queue * bfqq,char * str,int len)1166582f04e1SJan Kara static inline void bfq_bfqq_name(struct bfq_queue *bfqq, char *str, int len)
11671e66413cSFrancesco Pollicino {
1168582f04e1SJan Kara 	char type = bfq_bfqq_sync(bfqq) ? 'S' : 'A';
1169582f04e1SJan Kara 
1170582f04e1SJan Kara 	if (bfqq->pid != -1)
1171582f04e1SJan Kara 		snprintf(str, len, "bfq%d%c", bfqq->pid, type);
11721e66413cSFrancesco Pollicino 	else
1173582f04e1SJan Kara 		snprintf(str, len, "bfqSHARED-%c", type);
11741e66413cSFrancesco Pollicino }
11751e66413cSFrancesco Pollicino 
1176ea25da48SPaolo Valente #ifdef CONFIG_BFQ_GROUP_IOSCHED
1177ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1178ea25da48SPaolo Valente 
1179ea25da48SPaolo Valente #define bfq_log_bfqq(bfqd, bfqq, fmt, args...)	do {			\
1180582f04e1SJan Kara 	char pid_str[MAX_BFQQ_NAME_LENGTH];				\
118140d47c15SDmitry Monakhov 	if (likely(!blk_trace_note_message_enabled((bfqd)->queue)))	\
118240d47c15SDmitry Monakhov 		break;							\
1183582f04e1SJan Kara 	bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH);		\
118435fe6d76SShaohua Li 	blk_add_cgroup_trace_msg((bfqd)->queue,				\
1185f4a6a61cSChristoph Hellwig 			&bfqg_to_blkg(bfqq_group(bfqq))->blkcg->css,	\
1186582f04e1SJan Kara 			"%s " fmt, pid_str, ##args);			\
1187ea25da48SPaolo Valente } while (0)
1188ea25da48SPaolo Valente 
118935fe6d76SShaohua Li #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)	do {			\
119035fe6d76SShaohua Li 	blk_add_cgroup_trace_msg((bfqd)->queue,				\
1191f4a6a61cSChristoph Hellwig 		&bfqg_to_blkg(bfqg)->blkcg->css, fmt, ##args);		\
119235fe6d76SShaohua Li } while (0)
1193ea25da48SPaolo Valente 
1194ea25da48SPaolo Valente #else /* CONFIG_BFQ_GROUP_IOSCHED */
1195ea25da48SPaolo Valente 
11961e66413cSFrancesco Pollicino #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do {	\
1197582f04e1SJan Kara 	char pid_str[MAX_BFQQ_NAME_LENGTH];				\
119840d47c15SDmitry Monakhov 	if (likely(!blk_trace_note_message_enabled((bfqd)->queue)))	\
119940d47c15SDmitry Monakhov 		break;							\
1200582f04e1SJan Kara 	bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH);		\
1201582f04e1SJan Kara 	blk_add_trace_msg((bfqd)->queue, "%s " fmt, pid_str, ##args);	\
12021e66413cSFrancesco Pollicino } while (0)
1203ea25da48SPaolo Valente #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)		do {} while (0)
1204ea25da48SPaolo Valente 
1205ea25da48SPaolo Valente #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1206ea25da48SPaolo Valente 
1207ea25da48SPaolo Valente #define bfq_log(bfqd, fmt, args...) \
1208ea25da48SPaolo Valente 	blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
1209ea25da48SPaolo Valente 
1210ea25da48SPaolo Valente #endif /* _BFQ_H */
1211