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