xref: /OK3568_Linux_fs/kernel/block/bfq-wf2q.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Hierarchical Budget Worst-case Fair Weighted Fair Queueing
4*4882a593Smuzhiyun  * (B-WF2Q+): hierarchical scheduling algorithm by which the BFQ I/O
5*4882a593Smuzhiyun  * scheduler schedules generic entities. The latter can represent
6*4882a593Smuzhiyun  * either single bfq queues (associated with processes) or groups of
7*4882a593Smuzhiyun  * bfq queues (associated with cgroups).
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun #include "bfq-iosched.h"
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /**
12*4882a593Smuzhiyun  * bfq_gt - compare two timestamps.
13*4882a593Smuzhiyun  * @a: first ts.
14*4882a593Smuzhiyun  * @b: second ts.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Return @a > @b, dealing with wrapping correctly.
17*4882a593Smuzhiyun  */
bfq_gt(u64 a,u64 b)18*4882a593Smuzhiyun static int bfq_gt(u64 a, u64 b)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	return (s64)(a - b) > 0;
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun 
bfq_root_active_entity(struct rb_root * tree)23*4882a593Smuzhiyun static struct bfq_entity *bfq_root_active_entity(struct rb_root *tree)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	struct rb_node *node = tree->rb_node;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	return rb_entry(node, struct bfq_entity, rb_node);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
bfq_class_idx(struct bfq_entity * entity)30*4882a593Smuzhiyun static unsigned int bfq_class_idx(struct bfq_entity *entity)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	return bfqq ? bfqq->ioprio_class - 1 :
35*4882a593Smuzhiyun 		BFQ_DEFAULT_GRP_CLASS - 1;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
bfq_tot_busy_queues(struct bfq_data * bfqd)38*4882a593Smuzhiyun unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	return bfqd->busy_queues[0] + bfqd->busy_queues[1] +
41*4882a593Smuzhiyun 		bfqd->busy_queues[2];
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
45*4882a593Smuzhiyun 						 bool expiration);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static bool bfq_update_parent_budget(struct bfq_entity *next_in_service);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /**
50*4882a593Smuzhiyun  * bfq_update_next_in_service - update sd->next_in_service
51*4882a593Smuzhiyun  * @sd: sched_data for which to perform the update.
52*4882a593Smuzhiyun  * @new_entity: if not NULL, pointer to the entity whose activation,
53*4882a593Smuzhiyun  *		requeueing or repositioning triggered the invocation of
54*4882a593Smuzhiyun  *		this function.
55*4882a593Smuzhiyun  * @expiration: id true, this function is being invoked after the
56*4882a593Smuzhiyun  *             expiration of the in-service entity
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * This function is called to update sd->next_in_service, which, in
59*4882a593Smuzhiyun  * its turn, may change as a consequence of the insertion or
60*4882a593Smuzhiyun  * extraction of an entity into/from one of the active trees of
61*4882a593Smuzhiyun  * sd. These insertions/extractions occur as a consequence of
62*4882a593Smuzhiyun  * activations/deactivations of entities, with some activations being
63*4882a593Smuzhiyun  * 'true' activations, and other activations being requeueings (i.e.,
64*4882a593Smuzhiyun  * implementing the second, requeueing phase of the mechanism used to
65*4882a593Smuzhiyun  * reposition an entity in its active tree; see comments on
66*4882a593Smuzhiyun  * __bfq_activate_entity and __bfq_requeue_entity for details). In
67*4882a593Smuzhiyun  * both the last two activation sub-cases, new_entity points to the
68*4882a593Smuzhiyun  * just activated or requeued entity.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Returns true if sd->next_in_service changes in such a way that
71*4882a593Smuzhiyun  * entity->parent may become the next_in_service for its parent
72*4882a593Smuzhiyun  * entity.
73*4882a593Smuzhiyun  */
bfq_update_next_in_service(struct bfq_sched_data * sd,struct bfq_entity * new_entity,bool expiration)74*4882a593Smuzhiyun static bool bfq_update_next_in_service(struct bfq_sched_data *sd,
75*4882a593Smuzhiyun 				       struct bfq_entity *new_entity,
76*4882a593Smuzhiyun 				       bool expiration)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct bfq_entity *next_in_service = sd->next_in_service;
79*4882a593Smuzhiyun 	bool parent_sched_may_change = false;
80*4882a593Smuzhiyun 	bool change_without_lookup = false;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/*
83*4882a593Smuzhiyun 	 * If this update is triggered by the activation, requeueing
84*4882a593Smuzhiyun 	 * or repositioning of an entity that does not coincide with
85*4882a593Smuzhiyun 	 * sd->next_in_service, then a full lookup in the active tree
86*4882a593Smuzhiyun 	 * can be avoided. In fact, it is enough to check whether the
87*4882a593Smuzhiyun 	 * just-modified entity has the same priority as
88*4882a593Smuzhiyun 	 * sd->next_in_service, is eligible and has a lower virtual
89*4882a593Smuzhiyun 	 * finish time than sd->next_in_service. If this compound
90*4882a593Smuzhiyun 	 * condition holds, then the new entity becomes the new
91*4882a593Smuzhiyun 	 * next_in_service. Otherwise no change is needed.
92*4882a593Smuzhiyun 	 */
93*4882a593Smuzhiyun 	if (new_entity && new_entity != sd->next_in_service) {
94*4882a593Smuzhiyun 		/*
95*4882a593Smuzhiyun 		 * Flag used to decide whether to replace
96*4882a593Smuzhiyun 		 * sd->next_in_service with new_entity. Tentatively
97*4882a593Smuzhiyun 		 * set to true, and left as true if
98*4882a593Smuzhiyun 		 * sd->next_in_service is NULL.
99*4882a593Smuzhiyun 		 */
100*4882a593Smuzhiyun 		change_without_lookup = true;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 		/*
103*4882a593Smuzhiyun 		 * If there is already a next_in_service candidate
104*4882a593Smuzhiyun 		 * entity, then compare timestamps to decide whether
105*4882a593Smuzhiyun 		 * to replace sd->service_tree with new_entity.
106*4882a593Smuzhiyun 		 */
107*4882a593Smuzhiyun 		if (next_in_service) {
108*4882a593Smuzhiyun 			unsigned int new_entity_class_idx =
109*4882a593Smuzhiyun 				bfq_class_idx(new_entity);
110*4882a593Smuzhiyun 			struct bfq_service_tree *st =
111*4882a593Smuzhiyun 				sd->service_tree + new_entity_class_idx;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 			change_without_lookup =
114*4882a593Smuzhiyun 				(new_entity_class_idx ==
115*4882a593Smuzhiyun 				 bfq_class_idx(next_in_service)
116*4882a593Smuzhiyun 				 &&
117*4882a593Smuzhiyun 				 !bfq_gt(new_entity->start, st->vtime)
118*4882a593Smuzhiyun 				 &&
119*4882a593Smuzhiyun 				 bfq_gt(next_in_service->finish,
120*4882a593Smuzhiyun 					new_entity->finish));
121*4882a593Smuzhiyun 		}
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 		if (change_without_lookup)
124*4882a593Smuzhiyun 			next_in_service = new_entity;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (!change_without_lookup) /* lookup needed */
128*4882a593Smuzhiyun 		next_in_service = bfq_lookup_next_entity(sd, expiration);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (next_in_service) {
131*4882a593Smuzhiyun 		bool new_budget_triggers_change =
132*4882a593Smuzhiyun 			bfq_update_parent_budget(next_in_service);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 		parent_sched_may_change = !sd->next_in_service ||
135*4882a593Smuzhiyun 			new_budget_triggers_change;
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	sd->next_in_service = next_in_service;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (!next_in_service)
141*4882a593Smuzhiyun 		return parent_sched_may_change;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return parent_sched_may_change;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
147*4882a593Smuzhiyun 
bfq_bfqq_to_bfqg(struct bfq_queue * bfqq)148*4882a593Smuzhiyun struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	struct bfq_entity *group_entity = bfqq->entity.parent;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!group_entity)
153*4882a593Smuzhiyun 		group_entity = &bfqq->bfqd->root_group->entity;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return container_of(group_entity, struct bfq_group, entity);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun  * Returns true if this budget changes may let next_in_service->parent
160*4882a593Smuzhiyun  * become the next_in_service entity for its parent entity.
161*4882a593Smuzhiyun  */
bfq_update_parent_budget(struct bfq_entity * next_in_service)162*4882a593Smuzhiyun static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	struct bfq_entity *bfqg_entity;
165*4882a593Smuzhiyun 	struct bfq_group *bfqg;
166*4882a593Smuzhiyun 	struct bfq_sched_data *group_sd;
167*4882a593Smuzhiyun 	bool ret = false;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	group_sd = next_in_service->sched_data;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	bfqg = container_of(group_sd, struct bfq_group, sched_data);
172*4882a593Smuzhiyun 	/*
173*4882a593Smuzhiyun 	 * bfq_group's my_entity field is not NULL only if the group
174*4882a593Smuzhiyun 	 * is not the root group. We must not touch the root entity
175*4882a593Smuzhiyun 	 * as it must never become an in-service entity.
176*4882a593Smuzhiyun 	 */
177*4882a593Smuzhiyun 	bfqg_entity = bfqg->my_entity;
178*4882a593Smuzhiyun 	if (bfqg_entity) {
179*4882a593Smuzhiyun 		if (bfqg_entity->budget > next_in_service->budget)
180*4882a593Smuzhiyun 			ret = true;
181*4882a593Smuzhiyun 		bfqg_entity->budget = next_in_service->budget;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	return ret;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * This function tells whether entity stops being a candidate for next
189*4882a593Smuzhiyun  * service, according to the restrictive definition of the field
190*4882a593Smuzhiyun  * next_in_service. In particular, this function is invoked for an
191*4882a593Smuzhiyun  * entity that is about to be set in service.
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * If entity is a queue, then the entity is no longer a candidate for
194*4882a593Smuzhiyun  * next service according to the that definition, because entity is
195*4882a593Smuzhiyun  * about to become the in-service queue. This function then returns
196*4882a593Smuzhiyun  * true if entity is a queue.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * In contrast, entity could still be a candidate for next service if
199*4882a593Smuzhiyun  * it is not a queue, and has more than one active child. In fact,
200*4882a593Smuzhiyun  * even if one of its children is about to be set in service, other
201*4882a593Smuzhiyun  * active children may still be the next to serve, for the parent
202*4882a593Smuzhiyun  * entity, even according to the above definition. As a consequence, a
203*4882a593Smuzhiyun  * non-queue entity is not a candidate for next-service only if it has
204*4882a593Smuzhiyun  * only one active child. And only if this condition holds, then this
205*4882a593Smuzhiyun  * function returns true for a non-queue entity.
206*4882a593Smuzhiyun  */
bfq_no_longer_next_in_service(struct bfq_entity * entity)207*4882a593Smuzhiyun static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct bfq_group *bfqg;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	if (bfq_entity_to_bfqq(entity))
212*4882a593Smuzhiyun 		return true;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	bfqg = container_of(entity, struct bfq_group, entity);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * The field active_entities does not always contain the
218*4882a593Smuzhiyun 	 * actual number of active children entities: it happens to
219*4882a593Smuzhiyun 	 * not account for the in-service entity in case the latter is
220*4882a593Smuzhiyun 	 * removed from its active tree (which may get done after
221*4882a593Smuzhiyun 	 * invoking the function bfq_no_longer_next_in_service in
222*4882a593Smuzhiyun 	 * bfq_get_next_queue). Fortunately, here, i.e., while
223*4882a593Smuzhiyun 	 * bfq_no_longer_next_in_service is not yet completed in
224*4882a593Smuzhiyun 	 * bfq_get_next_queue, bfq_active_extract has not yet been
225*4882a593Smuzhiyun 	 * invoked, and thus active_entities still coincides with the
226*4882a593Smuzhiyun 	 * actual number of active entities.
227*4882a593Smuzhiyun 	 */
228*4882a593Smuzhiyun 	if (bfqg->active_entities == 1)
229*4882a593Smuzhiyun 		return true;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return false;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun #else /* CONFIG_BFQ_GROUP_IOSCHED */
235*4882a593Smuzhiyun 
bfq_bfqq_to_bfqg(struct bfq_queue * bfqq)236*4882a593Smuzhiyun struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	return bfqq->bfqd->root_group;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
bfq_update_parent_budget(struct bfq_entity * next_in_service)241*4882a593Smuzhiyun static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	return false;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
bfq_no_longer_next_in_service(struct bfq_entity * entity)246*4882a593Smuzhiyun static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	return true;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun #endif /* CONFIG_BFQ_GROUP_IOSCHED */
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun  * Shift for timestamp calculations.  This actually limits the maximum
255*4882a593Smuzhiyun  * service allowed in one timestamp delta (small shift values increase it),
256*4882a593Smuzhiyun  * the maximum total weight that can be used for the queues in the system
257*4882a593Smuzhiyun  * (big shift values increase it), and the period of virtual time
258*4882a593Smuzhiyun  * wraparounds.
259*4882a593Smuzhiyun  */
260*4882a593Smuzhiyun #define WFQ_SERVICE_SHIFT	22
261*4882a593Smuzhiyun 
bfq_entity_to_bfqq(struct bfq_entity * entity)262*4882a593Smuzhiyun struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct bfq_queue *bfqq = NULL;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (!entity->my_sched_data)
267*4882a593Smuzhiyun 		bfqq = container_of(entity, struct bfq_queue, entity);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	return bfqq;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun  * bfq_delta - map service into the virtual time domain.
275*4882a593Smuzhiyun  * @service: amount of service.
276*4882a593Smuzhiyun  * @weight: scale factor (weight of an entity or weight sum).
277*4882a593Smuzhiyun  */
bfq_delta(unsigned long service,unsigned long weight)278*4882a593Smuzhiyun static u64 bfq_delta(unsigned long service, unsigned long weight)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	return div64_ul((u64)service << WFQ_SERVICE_SHIFT, weight);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /**
284*4882a593Smuzhiyun  * bfq_calc_finish - assign the finish time to an entity.
285*4882a593Smuzhiyun  * @entity: the entity to act upon.
286*4882a593Smuzhiyun  * @service: the service to be charged to the entity.
287*4882a593Smuzhiyun  */
bfq_calc_finish(struct bfq_entity * entity,unsigned long service)288*4882a593Smuzhiyun static void bfq_calc_finish(struct bfq_entity *entity, unsigned long service)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	entity->finish = entity->start +
293*4882a593Smuzhiyun 		bfq_delta(service, entity->weight);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (bfqq) {
296*4882a593Smuzhiyun 		bfq_log_bfqq(bfqq->bfqd, bfqq,
297*4882a593Smuzhiyun 			"calc_finish: serv %lu, w %d",
298*4882a593Smuzhiyun 			service, entity->weight);
299*4882a593Smuzhiyun 		bfq_log_bfqq(bfqq->bfqd, bfqq,
300*4882a593Smuzhiyun 			"calc_finish: start %llu, finish %llu, delta %llu",
301*4882a593Smuzhiyun 			entity->start, entity->finish,
302*4882a593Smuzhiyun 			bfq_delta(service, entity->weight));
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun /**
307*4882a593Smuzhiyun  * bfq_entity_of - get an entity from a node.
308*4882a593Smuzhiyun  * @node: the node field of the entity.
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * Convert a node pointer to the relative entity.  This is used only
311*4882a593Smuzhiyun  * to simplify the logic of some functions and not as the generic
312*4882a593Smuzhiyun  * conversion mechanism because, e.g., in the tree walking functions,
313*4882a593Smuzhiyun  * the check for a %NULL value would be redundant.
314*4882a593Smuzhiyun  */
bfq_entity_of(struct rb_node * node)315*4882a593Smuzhiyun struct bfq_entity *bfq_entity_of(struct rb_node *node)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	struct bfq_entity *entity = NULL;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (node)
320*4882a593Smuzhiyun 		entity = rb_entry(node, struct bfq_entity, rb_node);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	return entity;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /**
326*4882a593Smuzhiyun  * bfq_extract - remove an entity from a tree.
327*4882a593Smuzhiyun  * @root: the tree root.
328*4882a593Smuzhiyun  * @entity: the entity to remove.
329*4882a593Smuzhiyun  */
bfq_extract(struct rb_root * root,struct bfq_entity * entity)330*4882a593Smuzhiyun static void bfq_extract(struct rb_root *root, struct bfq_entity *entity)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	entity->tree = NULL;
333*4882a593Smuzhiyun 	rb_erase(&entity->rb_node, root);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun /**
337*4882a593Smuzhiyun  * bfq_idle_extract - extract an entity from the idle tree.
338*4882a593Smuzhiyun  * @st: the service tree of the owning @entity.
339*4882a593Smuzhiyun  * @entity: the entity being removed.
340*4882a593Smuzhiyun  */
bfq_idle_extract(struct bfq_service_tree * st,struct bfq_entity * entity)341*4882a593Smuzhiyun static void bfq_idle_extract(struct bfq_service_tree *st,
342*4882a593Smuzhiyun 			     struct bfq_entity *entity)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
345*4882a593Smuzhiyun 	struct rb_node *next;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (entity == st->first_idle) {
348*4882a593Smuzhiyun 		next = rb_next(&entity->rb_node);
349*4882a593Smuzhiyun 		st->first_idle = bfq_entity_of(next);
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (entity == st->last_idle) {
353*4882a593Smuzhiyun 		next = rb_prev(&entity->rb_node);
354*4882a593Smuzhiyun 		st->last_idle = bfq_entity_of(next);
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	bfq_extract(&st->idle, entity);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (bfqq)
360*4882a593Smuzhiyun 		list_del(&bfqq->bfqq_list);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun  * bfq_insert - generic tree insertion.
365*4882a593Smuzhiyun  * @root: tree root.
366*4882a593Smuzhiyun  * @entity: entity to insert.
367*4882a593Smuzhiyun  *
368*4882a593Smuzhiyun  * This is used for the idle and the active tree, since they are both
369*4882a593Smuzhiyun  * ordered by finish time.
370*4882a593Smuzhiyun  */
bfq_insert(struct rb_root * root,struct bfq_entity * entity)371*4882a593Smuzhiyun static void bfq_insert(struct rb_root *root, struct bfq_entity *entity)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	struct bfq_entity *entry;
374*4882a593Smuzhiyun 	struct rb_node **node = &root->rb_node;
375*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	while (*node) {
378*4882a593Smuzhiyun 		parent = *node;
379*4882a593Smuzhiyun 		entry = rb_entry(parent, struct bfq_entity, rb_node);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 		if (bfq_gt(entry->finish, entity->finish))
382*4882a593Smuzhiyun 			node = &parent->rb_left;
383*4882a593Smuzhiyun 		else
384*4882a593Smuzhiyun 			node = &parent->rb_right;
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	rb_link_node(&entity->rb_node, parent, node);
388*4882a593Smuzhiyun 	rb_insert_color(&entity->rb_node, root);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	entity->tree = root;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * bfq_update_min - update the min_start field of a entity.
395*4882a593Smuzhiyun  * @entity: the entity to update.
396*4882a593Smuzhiyun  * @node: one of its children.
397*4882a593Smuzhiyun  *
398*4882a593Smuzhiyun  * This function is called when @entity may store an invalid value for
399*4882a593Smuzhiyun  * min_start due to updates to the active tree.  The function  assumes
400*4882a593Smuzhiyun  * that the subtree rooted at @node (which may be its left or its right
401*4882a593Smuzhiyun  * child) has a valid min_start value.
402*4882a593Smuzhiyun  */
bfq_update_min(struct bfq_entity * entity,struct rb_node * node)403*4882a593Smuzhiyun static void bfq_update_min(struct bfq_entity *entity, struct rb_node *node)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	struct bfq_entity *child;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	if (node) {
408*4882a593Smuzhiyun 		child = rb_entry(node, struct bfq_entity, rb_node);
409*4882a593Smuzhiyun 		if (bfq_gt(entity->min_start, child->min_start))
410*4882a593Smuzhiyun 			entity->min_start = child->min_start;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun  * bfq_update_active_node - recalculate min_start.
416*4882a593Smuzhiyun  * @node: the node to update.
417*4882a593Smuzhiyun  *
418*4882a593Smuzhiyun  * @node may have changed position or one of its children may have moved,
419*4882a593Smuzhiyun  * this function updates its min_start value.  The left and right subtrees
420*4882a593Smuzhiyun  * are assumed to hold a correct min_start value.
421*4882a593Smuzhiyun  */
bfq_update_active_node(struct rb_node * node)422*4882a593Smuzhiyun static void bfq_update_active_node(struct rb_node *node)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	entity->min_start = entity->start;
427*4882a593Smuzhiyun 	bfq_update_min(entity, node->rb_right);
428*4882a593Smuzhiyun 	bfq_update_min(entity, node->rb_left);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun /**
432*4882a593Smuzhiyun  * bfq_update_active_tree - update min_start for the whole active tree.
433*4882a593Smuzhiyun  * @node: the starting node.
434*4882a593Smuzhiyun  *
435*4882a593Smuzhiyun  * @node must be the deepest modified node after an update.  This function
436*4882a593Smuzhiyun  * updates its min_start using the values held by its children, assuming
437*4882a593Smuzhiyun  * that they did not change, and then updates all the nodes that may have
438*4882a593Smuzhiyun  * changed in the path to the root.  The only nodes that may have changed
439*4882a593Smuzhiyun  * are the ones in the path or their siblings.
440*4882a593Smuzhiyun  */
bfq_update_active_tree(struct rb_node * node)441*4882a593Smuzhiyun static void bfq_update_active_tree(struct rb_node *node)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct rb_node *parent;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun up:
446*4882a593Smuzhiyun 	bfq_update_active_node(node);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	parent = rb_parent(node);
449*4882a593Smuzhiyun 	if (!parent)
450*4882a593Smuzhiyun 		return;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (node == parent->rb_left && parent->rb_right)
453*4882a593Smuzhiyun 		bfq_update_active_node(parent->rb_right);
454*4882a593Smuzhiyun 	else if (parent->rb_left)
455*4882a593Smuzhiyun 		bfq_update_active_node(parent->rb_left);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	node = parent;
458*4882a593Smuzhiyun 	goto up;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun  * bfq_active_insert - insert an entity in the active tree of its
463*4882a593Smuzhiyun  *                     group/device.
464*4882a593Smuzhiyun  * @st: the service tree of the entity.
465*4882a593Smuzhiyun  * @entity: the entity being inserted.
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * The active tree is ordered by finish time, but an extra key is kept
468*4882a593Smuzhiyun  * per each node, containing the minimum value for the start times of
469*4882a593Smuzhiyun  * its children (and the node itself), so it's possible to search for
470*4882a593Smuzhiyun  * the eligible node with the lowest finish time in logarithmic time.
471*4882a593Smuzhiyun  */
bfq_active_insert(struct bfq_service_tree * st,struct bfq_entity * entity)472*4882a593Smuzhiyun static void bfq_active_insert(struct bfq_service_tree *st,
473*4882a593Smuzhiyun 			      struct bfq_entity *entity)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
476*4882a593Smuzhiyun 	struct rb_node *node = &entity->rb_node;
477*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
478*4882a593Smuzhiyun 	struct bfq_sched_data *sd = NULL;
479*4882a593Smuzhiyun 	struct bfq_group *bfqg = NULL;
480*4882a593Smuzhiyun 	struct bfq_data *bfqd = NULL;
481*4882a593Smuzhiyun #endif
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	bfq_insert(&st->active, entity);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	if (node->rb_left)
486*4882a593Smuzhiyun 		node = node->rb_left;
487*4882a593Smuzhiyun 	else if (node->rb_right)
488*4882a593Smuzhiyun 		node = node->rb_right;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	bfq_update_active_tree(node);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
493*4882a593Smuzhiyun 	sd = entity->sched_data;
494*4882a593Smuzhiyun 	bfqg = container_of(sd, struct bfq_group, sched_data);
495*4882a593Smuzhiyun 	bfqd = (struct bfq_data *)bfqg->bfqd;
496*4882a593Smuzhiyun #endif
497*4882a593Smuzhiyun 	if (bfqq)
498*4882a593Smuzhiyun 		list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list);
499*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
500*4882a593Smuzhiyun 	if (bfqg != bfqd->root_group)
501*4882a593Smuzhiyun 		bfqg->active_entities++;
502*4882a593Smuzhiyun #endif
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun  * bfq_ioprio_to_weight - calc a weight from an ioprio.
507*4882a593Smuzhiyun  * @ioprio: the ioprio value to convert.
508*4882a593Smuzhiyun  */
bfq_ioprio_to_weight(int ioprio)509*4882a593Smuzhiyun unsigned short bfq_ioprio_to_weight(int ioprio)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	return (IOPRIO_BE_NR - ioprio) * BFQ_WEIGHT_CONVERSION_COEFF;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun /**
515*4882a593Smuzhiyun  * bfq_weight_to_ioprio - calc an ioprio from a weight.
516*4882a593Smuzhiyun  * @weight: the weight value to convert.
517*4882a593Smuzhiyun  *
518*4882a593Smuzhiyun  * To preserve as much as possible the old only-ioprio user interface,
519*4882a593Smuzhiyun  * 0 is used as an escape ioprio value for weights (numerically) equal or
520*4882a593Smuzhiyun  * larger than IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF.
521*4882a593Smuzhiyun  */
bfq_weight_to_ioprio(int weight)522*4882a593Smuzhiyun static unsigned short bfq_weight_to_ioprio(int weight)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun 	return max_t(int, 0,
525*4882a593Smuzhiyun 		     IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF - weight);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
bfq_get_entity(struct bfq_entity * entity)528*4882a593Smuzhiyun static void bfq_get_entity(struct bfq_entity *entity)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	if (bfqq) {
533*4882a593Smuzhiyun 		bfqq->ref++;
534*4882a593Smuzhiyun 		bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
535*4882a593Smuzhiyun 			     bfqq, bfqq->ref);
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun /**
540*4882a593Smuzhiyun  * bfq_find_deepest - find the deepest node that an extraction can modify.
541*4882a593Smuzhiyun  * @node: the node being removed.
542*4882a593Smuzhiyun  *
543*4882a593Smuzhiyun  * Do the first step of an extraction in an rb tree, looking for the
544*4882a593Smuzhiyun  * node that will replace @node, and returning the deepest node that
545*4882a593Smuzhiyun  * the following modifications to the tree can touch.  If @node is the
546*4882a593Smuzhiyun  * last node in the tree return %NULL.
547*4882a593Smuzhiyun  */
bfq_find_deepest(struct rb_node * node)548*4882a593Smuzhiyun static struct rb_node *bfq_find_deepest(struct rb_node *node)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct rb_node *deepest;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (!node->rb_right && !node->rb_left)
553*4882a593Smuzhiyun 		deepest = rb_parent(node);
554*4882a593Smuzhiyun 	else if (!node->rb_right)
555*4882a593Smuzhiyun 		deepest = node->rb_left;
556*4882a593Smuzhiyun 	else if (!node->rb_left)
557*4882a593Smuzhiyun 		deepest = node->rb_right;
558*4882a593Smuzhiyun 	else {
559*4882a593Smuzhiyun 		deepest = rb_next(node);
560*4882a593Smuzhiyun 		if (deepest->rb_right)
561*4882a593Smuzhiyun 			deepest = deepest->rb_right;
562*4882a593Smuzhiyun 		else if (rb_parent(deepest) != node)
563*4882a593Smuzhiyun 			deepest = rb_parent(deepest);
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	return deepest;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun  * bfq_active_extract - remove an entity from the active tree.
571*4882a593Smuzhiyun  * @st: the service_tree containing the tree.
572*4882a593Smuzhiyun  * @entity: the entity being removed.
573*4882a593Smuzhiyun  */
bfq_active_extract(struct bfq_service_tree * st,struct bfq_entity * entity)574*4882a593Smuzhiyun static void bfq_active_extract(struct bfq_service_tree *st,
575*4882a593Smuzhiyun 			       struct bfq_entity *entity)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
578*4882a593Smuzhiyun 	struct rb_node *node;
579*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
580*4882a593Smuzhiyun 	struct bfq_sched_data *sd = NULL;
581*4882a593Smuzhiyun 	struct bfq_group *bfqg = NULL;
582*4882a593Smuzhiyun 	struct bfq_data *bfqd = NULL;
583*4882a593Smuzhiyun #endif
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	node = bfq_find_deepest(&entity->rb_node);
586*4882a593Smuzhiyun 	bfq_extract(&st->active, entity);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (node)
589*4882a593Smuzhiyun 		bfq_update_active_tree(node);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
592*4882a593Smuzhiyun 	sd = entity->sched_data;
593*4882a593Smuzhiyun 	bfqg = container_of(sd, struct bfq_group, sched_data);
594*4882a593Smuzhiyun 	bfqd = (struct bfq_data *)bfqg->bfqd;
595*4882a593Smuzhiyun #endif
596*4882a593Smuzhiyun 	if (bfqq)
597*4882a593Smuzhiyun 		list_del(&bfqq->bfqq_list);
598*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
599*4882a593Smuzhiyun 	if (bfqg != bfqd->root_group)
600*4882a593Smuzhiyun 		bfqg->active_entities--;
601*4882a593Smuzhiyun #endif
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun  * bfq_idle_insert - insert an entity into the idle tree.
606*4882a593Smuzhiyun  * @st: the service tree containing the tree.
607*4882a593Smuzhiyun  * @entity: the entity to insert.
608*4882a593Smuzhiyun  */
bfq_idle_insert(struct bfq_service_tree * st,struct bfq_entity * entity)609*4882a593Smuzhiyun static void bfq_idle_insert(struct bfq_service_tree *st,
610*4882a593Smuzhiyun 			    struct bfq_entity *entity)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
613*4882a593Smuzhiyun 	struct bfq_entity *first_idle = st->first_idle;
614*4882a593Smuzhiyun 	struct bfq_entity *last_idle = st->last_idle;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	if (!first_idle || bfq_gt(first_idle->finish, entity->finish))
617*4882a593Smuzhiyun 		st->first_idle = entity;
618*4882a593Smuzhiyun 	if (!last_idle || bfq_gt(entity->finish, last_idle->finish))
619*4882a593Smuzhiyun 		st->last_idle = entity;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	bfq_insert(&st->idle, entity);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	if (bfqq)
624*4882a593Smuzhiyun 		list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list);
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun /**
628*4882a593Smuzhiyun  * bfq_forget_entity - do not consider entity any longer for scheduling
629*4882a593Smuzhiyun  * @st: the service tree.
630*4882a593Smuzhiyun  * @entity: the entity being removed.
631*4882a593Smuzhiyun  * @is_in_service: true if entity is currently the in-service entity.
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * Forget everything about @entity. In addition, if entity represents
634*4882a593Smuzhiyun  * a queue, and the latter is not in service, then release the service
635*4882a593Smuzhiyun  * reference to the queue (the one taken through bfq_get_entity). In
636*4882a593Smuzhiyun  * fact, in this case, there is really no more service reference to
637*4882a593Smuzhiyun  * the queue, as the latter is also outside any service tree. If,
638*4882a593Smuzhiyun  * instead, the queue is in service, then __bfq_bfqd_reset_in_service
639*4882a593Smuzhiyun  * will take care of putting the reference when the queue finally
640*4882a593Smuzhiyun  * stops being served.
641*4882a593Smuzhiyun  */
bfq_forget_entity(struct bfq_service_tree * st,struct bfq_entity * entity,bool is_in_service)642*4882a593Smuzhiyun static void bfq_forget_entity(struct bfq_service_tree *st,
643*4882a593Smuzhiyun 			      struct bfq_entity *entity,
644*4882a593Smuzhiyun 			      bool is_in_service)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	entity->on_st_or_in_serv = false;
649*4882a593Smuzhiyun 	st->wsum -= entity->weight;
650*4882a593Smuzhiyun 	if (bfqq && !is_in_service)
651*4882a593Smuzhiyun 		bfq_put_queue(bfqq);
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun /**
655*4882a593Smuzhiyun  * bfq_put_idle_entity - release the idle tree ref of an entity.
656*4882a593Smuzhiyun  * @st: service tree for the entity.
657*4882a593Smuzhiyun  * @entity: the entity being released.
658*4882a593Smuzhiyun  */
bfq_put_idle_entity(struct bfq_service_tree * st,struct bfq_entity * entity)659*4882a593Smuzhiyun void bfq_put_idle_entity(struct bfq_service_tree *st, struct bfq_entity *entity)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	bfq_idle_extract(st, entity);
662*4882a593Smuzhiyun 	bfq_forget_entity(st, entity,
663*4882a593Smuzhiyun 			  entity == entity->sched_data->in_service_entity);
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun /**
667*4882a593Smuzhiyun  * bfq_forget_idle - update the idle tree if necessary.
668*4882a593Smuzhiyun  * @st: the service tree to act upon.
669*4882a593Smuzhiyun  *
670*4882a593Smuzhiyun  * To preserve the global O(log N) complexity we only remove one entry here;
671*4882a593Smuzhiyun  * as the idle tree will not grow indefinitely this can be done safely.
672*4882a593Smuzhiyun  */
bfq_forget_idle(struct bfq_service_tree * st)673*4882a593Smuzhiyun static void bfq_forget_idle(struct bfq_service_tree *st)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct bfq_entity *first_idle = st->first_idle;
676*4882a593Smuzhiyun 	struct bfq_entity *last_idle = st->last_idle;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(&st->active) && last_idle &&
679*4882a593Smuzhiyun 	    !bfq_gt(last_idle->finish, st->vtime)) {
680*4882a593Smuzhiyun 		/*
681*4882a593Smuzhiyun 		 * Forget the whole idle tree, increasing the vtime past
682*4882a593Smuzhiyun 		 * the last finish time of idle entities.
683*4882a593Smuzhiyun 		 */
684*4882a593Smuzhiyun 		st->vtime = last_idle->finish;
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	if (first_idle && !bfq_gt(first_idle->finish, st->vtime))
688*4882a593Smuzhiyun 		bfq_put_idle_entity(st, first_idle);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
bfq_entity_service_tree(struct bfq_entity * entity)691*4882a593Smuzhiyun struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	struct bfq_sched_data *sched_data = entity->sched_data;
694*4882a593Smuzhiyun 	unsigned int idx = bfq_class_idx(entity);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	return sched_data->service_tree + idx;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun /*
700*4882a593Smuzhiyun  * Update weight and priority of entity. If update_class_too is true,
701*4882a593Smuzhiyun  * then update the ioprio_class of entity too.
702*4882a593Smuzhiyun  *
703*4882a593Smuzhiyun  * The reason why the update of ioprio_class is controlled through the
704*4882a593Smuzhiyun  * last parameter is as follows. Changing the ioprio class of an
705*4882a593Smuzhiyun  * entity implies changing the destination service trees for that
706*4882a593Smuzhiyun  * entity. If such a change occurred when the entity is already on one
707*4882a593Smuzhiyun  * of the service trees for its previous class, then the state of the
708*4882a593Smuzhiyun  * entity would become more complex: none of the new possible service
709*4882a593Smuzhiyun  * trees for the entity, according to bfq_entity_service_tree(), would
710*4882a593Smuzhiyun  * match any of the possible service trees on which the entity
711*4882a593Smuzhiyun  * is. Complex operations involving these trees, such as entity
712*4882a593Smuzhiyun  * activations and deactivations, should take into account this
713*4882a593Smuzhiyun  * additional complexity.  To avoid this issue, this function is
714*4882a593Smuzhiyun  * invoked with update_class_too unset in the points in the code where
715*4882a593Smuzhiyun  * entity may happen to be on some tree.
716*4882a593Smuzhiyun  */
717*4882a593Smuzhiyun struct bfq_service_tree *
__bfq_entity_update_weight_prio(struct bfq_service_tree * old_st,struct bfq_entity * entity,bool update_class_too)718*4882a593Smuzhiyun __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
719*4882a593Smuzhiyun 				struct bfq_entity *entity,
720*4882a593Smuzhiyun 				bool update_class_too)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct bfq_service_tree *new_st = old_st;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	if (entity->prio_changed) {
725*4882a593Smuzhiyun 		struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
726*4882a593Smuzhiyun 		unsigned int prev_weight, new_weight;
727*4882a593Smuzhiyun 		struct bfq_data *bfqd = NULL;
728*4882a593Smuzhiyun 		struct rb_root_cached *root;
729*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
730*4882a593Smuzhiyun 		struct bfq_sched_data *sd;
731*4882a593Smuzhiyun 		struct bfq_group *bfqg;
732*4882a593Smuzhiyun #endif
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 		if (bfqq)
735*4882a593Smuzhiyun 			bfqd = bfqq->bfqd;
736*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
737*4882a593Smuzhiyun 		else {
738*4882a593Smuzhiyun 			sd = entity->my_sched_data;
739*4882a593Smuzhiyun 			bfqg = container_of(sd, struct bfq_group, sched_data);
740*4882a593Smuzhiyun 			bfqd = (struct bfq_data *)bfqg->bfqd;
741*4882a593Smuzhiyun 		}
742*4882a593Smuzhiyun #endif
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 		/* Matches the smp_wmb() in bfq_group_set_weight. */
745*4882a593Smuzhiyun 		smp_rmb();
746*4882a593Smuzhiyun 		old_st->wsum -= entity->weight;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		if (entity->new_weight != entity->orig_weight) {
749*4882a593Smuzhiyun 			if (entity->new_weight < BFQ_MIN_WEIGHT ||
750*4882a593Smuzhiyun 			    entity->new_weight > BFQ_MAX_WEIGHT) {
751*4882a593Smuzhiyun 				pr_crit("update_weight_prio: new_weight %d\n",
752*4882a593Smuzhiyun 					entity->new_weight);
753*4882a593Smuzhiyun 				if (entity->new_weight < BFQ_MIN_WEIGHT)
754*4882a593Smuzhiyun 					entity->new_weight = BFQ_MIN_WEIGHT;
755*4882a593Smuzhiyun 				else
756*4882a593Smuzhiyun 					entity->new_weight = BFQ_MAX_WEIGHT;
757*4882a593Smuzhiyun 			}
758*4882a593Smuzhiyun 			entity->orig_weight = entity->new_weight;
759*4882a593Smuzhiyun 			if (bfqq)
760*4882a593Smuzhiyun 				bfqq->ioprio =
761*4882a593Smuzhiyun 				  bfq_weight_to_ioprio(entity->orig_weight);
762*4882a593Smuzhiyun 		}
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 		if (bfqq && update_class_too)
765*4882a593Smuzhiyun 			bfqq->ioprio_class = bfqq->new_ioprio_class;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 		/*
768*4882a593Smuzhiyun 		 * Reset prio_changed only if the ioprio_class change
769*4882a593Smuzhiyun 		 * is not pending any longer.
770*4882a593Smuzhiyun 		 */
771*4882a593Smuzhiyun 		if (!bfqq || bfqq->ioprio_class == bfqq->new_ioprio_class)
772*4882a593Smuzhiyun 			entity->prio_changed = 0;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		/*
775*4882a593Smuzhiyun 		 * NOTE: here we may be changing the weight too early,
776*4882a593Smuzhiyun 		 * this will cause unfairness.  The correct approach
777*4882a593Smuzhiyun 		 * would have required additional complexity to defer
778*4882a593Smuzhiyun 		 * weight changes to the proper time instants (i.e.,
779*4882a593Smuzhiyun 		 * when entity->finish <= old_st->vtime).
780*4882a593Smuzhiyun 		 */
781*4882a593Smuzhiyun 		new_st = bfq_entity_service_tree(entity);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		prev_weight = entity->weight;
784*4882a593Smuzhiyun 		new_weight = entity->orig_weight *
785*4882a593Smuzhiyun 			     (bfqq ? bfqq->wr_coeff : 1);
786*4882a593Smuzhiyun 		/*
787*4882a593Smuzhiyun 		 * If the weight of the entity changes, and the entity is a
788*4882a593Smuzhiyun 		 * queue, remove the entity from its old weight counter (if
789*4882a593Smuzhiyun 		 * there is a counter associated with the entity).
790*4882a593Smuzhiyun 		 */
791*4882a593Smuzhiyun 		if (prev_weight != new_weight && bfqq) {
792*4882a593Smuzhiyun 			root = &bfqd->queue_weights_tree;
793*4882a593Smuzhiyun 			__bfq_weights_tree_remove(bfqd, bfqq, root);
794*4882a593Smuzhiyun 		}
795*4882a593Smuzhiyun 		entity->weight = new_weight;
796*4882a593Smuzhiyun 		/*
797*4882a593Smuzhiyun 		 * Add the entity, if it is not a weight-raised queue,
798*4882a593Smuzhiyun 		 * to the counter associated with its new weight.
799*4882a593Smuzhiyun 		 */
800*4882a593Smuzhiyun 		if (prev_weight != new_weight && bfqq && bfqq->wr_coeff == 1) {
801*4882a593Smuzhiyun 			/* If we get here, root has been initialized. */
802*4882a593Smuzhiyun 			bfq_weights_tree_add(bfqd, bfqq, root);
803*4882a593Smuzhiyun 		}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 		new_st->wsum += entity->weight;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 		if (new_st != old_st)
808*4882a593Smuzhiyun 			entity->start = new_st->vtime;
809*4882a593Smuzhiyun 	}
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	return new_st;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun /**
815*4882a593Smuzhiyun  * bfq_bfqq_served - update the scheduler status after selection for
816*4882a593Smuzhiyun  *                   service.
817*4882a593Smuzhiyun  * @bfqq: the queue being served.
818*4882a593Smuzhiyun  * @served: bytes to transfer.
819*4882a593Smuzhiyun  *
820*4882a593Smuzhiyun  * NOTE: this can be optimized, as the timestamps of upper level entities
821*4882a593Smuzhiyun  * are synchronized every time a new bfqq is selected for service.  By now,
822*4882a593Smuzhiyun  * we keep it to better check consistency.
823*4882a593Smuzhiyun  */
bfq_bfqq_served(struct bfq_queue * bfqq,int served)824*4882a593Smuzhiyun void bfq_bfqq_served(struct bfq_queue *bfqq, int served)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
827*4882a593Smuzhiyun 	struct bfq_service_tree *st;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	if (!bfqq->service_from_backlogged)
830*4882a593Smuzhiyun 		bfqq->first_IO_time = jiffies;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1)
833*4882a593Smuzhiyun 		bfqq->service_from_wr += served;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	bfqq->service_from_backlogged += served;
836*4882a593Smuzhiyun 	for_each_entity(entity) {
837*4882a593Smuzhiyun 		st = bfq_entity_service_tree(entity);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 		entity->service += served;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 		st->vtime += bfq_delta(served, st->wsum);
842*4882a593Smuzhiyun 		bfq_forget_idle(st);
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs", served);
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun /**
848*4882a593Smuzhiyun  * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
849*4882a593Smuzhiyun  *			  of the time interval during which bfqq has been in
850*4882a593Smuzhiyun  *			  service.
851*4882a593Smuzhiyun  * @bfqd: the device
852*4882a593Smuzhiyun  * @bfqq: the queue that needs a service update.
853*4882a593Smuzhiyun  * @time_ms: the amount of time during which the queue has received service
854*4882a593Smuzhiyun  *
855*4882a593Smuzhiyun  * If a queue does not consume its budget fast enough, then providing
856*4882a593Smuzhiyun  * the queue with service fairness may impair throughput, more or less
857*4882a593Smuzhiyun  * severely. For this reason, queues that consume their budget slowly
858*4882a593Smuzhiyun  * are provided with time fairness instead of service fairness. This
859*4882a593Smuzhiyun  * goal is achieved through the BFQ scheduling engine, even if such an
860*4882a593Smuzhiyun  * engine works in the service, and not in the time domain. The trick
861*4882a593Smuzhiyun  * is charging these queues with an inflated amount of service, equal
862*4882a593Smuzhiyun  * to the amount of service that they would have received during their
863*4882a593Smuzhiyun  * service slot if they had been fast, i.e., if their requests had
864*4882a593Smuzhiyun  * been dispatched at a rate equal to the estimated peak rate.
865*4882a593Smuzhiyun  *
866*4882a593Smuzhiyun  * It is worth noting that time fairness can cause important
867*4882a593Smuzhiyun  * distortions in terms of bandwidth distribution, on devices with
868*4882a593Smuzhiyun  * internal queueing. The reason is that I/O requests dispatched
869*4882a593Smuzhiyun  * during the service slot of a queue may be served after that service
870*4882a593Smuzhiyun  * slot is finished, and may have a total processing time loosely
871*4882a593Smuzhiyun  * correlated with the duration of the service slot. This is
872*4882a593Smuzhiyun  * especially true for short service slots.
873*4882a593Smuzhiyun  */
bfq_bfqq_charge_time(struct bfq_data * bfqd,struct bfq_queue * bfqq,unsigned long time_ms)874*4882a593Smuzhiyun void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
875*4882a593Smuzhiyun 			  unsigned long time_ms)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
878*4882a593Smuzhiyun 	unsigned long timeout_ms = jiffies_to_msecs(bfq_timeout);
879*4882a593Smuzhiyun 	unsigned long bounded_time_ms = min(time_ms, timeout_ms);
880*4882a593Smuzhiyun 	int serv_to_charge_for_time =
881*4882a593Smuzhiyun 		(bfqd->bfq_max_budget * bounded_time_ms) / timeout_ms;
882*4882a593Smuzhiyun 	int tot_serv_to_charge = max(serv_to_charge_for_time, entity->service);
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	/* Increase budget to avoid inconsistencies */
885*4882a593Smuzhiyun 	if (tot_serv_to_charge > entity->budget)
886*4882a593Smuzhiyun 		entity->budget = tot_serv_to_charge;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	bfq_bfqq_served(bfqq,
889*4882a593Smuzhiyun 			max_t(int, 0, tot_serv_to_charge - entity->service));
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
bfq_update_fin_time_enqueue(struct bfq_entity * entity,struct bfq_service_tree * st,bool backshifted)892*4882a593Smuzhiyun static void bfq_update_fin_time_enqueue(struct bfq_entity *entity,
893*4882a593Smuzhiyun 					struct bfq_service_tree *st,
894*4882a593Smuzhiyun 					bool backshifted)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	/*
899*4882a593Smuzhiyun 	 * When this function is invoked, entity is not in any service
900*4882a593Smuzhiyun 	 * tree, then it is safe to invoke next function with the last
901*4882a593Smuzhiyun 	 * parameter set (see the comments on the function).
902*4882a593Smuzhiyun 	 */
903*4882a593Smuzhiyun 	st = __bfq_entity_update_weight_prio(st, entity, true);
904*4882a593Smuzhiyun 	bfq_calc_finish(entity, entity->budget);
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	/*
907*4882a593Smuzhiyun 	 * If some queues enjoy backshifting for a while, then their
908*4882a593Smuzhiyun 	 * (virtual) finish timestamps may happen to become lower and
909*4882a593Smuzhiyun 	 * lower than the system virtual time.	In particular, if
910*4882a593Smuzhiyun 	 * these queues often happen to be idle for short time
911*4882a593Smuzhiyun 	 * periods, and during such time periods other queues with
912*4882a593Smuzhiyun 	 * higher timestamps happen to be busy, then the backshifted
913*4882a593Smuzhiyun 	 * timestamps of the former queues can become much lower than
914*4882a593Smuzhiyun 	 * the system virtual time. In fact, to serve the queues with
915*4882a593Smuzhiyun 	 * higher timestamps while the ones with lower timestamps are
916*4882a593Smuzhiyun 	 * idle, the system virtual time may be pushed-up to much
917*4882a593Smuzhiyun 	 * higher values than the finish timestamps of the idle
918*4882a593Smuzhiyun 	 * queues. As a consequence, the finish timestamps of all new
919*4882a593Smuzhiyun 	 * or newly activated queues may end up being much larger than
920*4882a593Smuzhiyun 	 * those of lucky queues with backshifted timestamps. The
921*4882a593Smuzhiyun 	 * latter queues may then monopolize the device for a lot of
922*4882a593Smuzhiyun 	 * time. This would simply break service guarantees.
923*4882a593Smuzhiyun 	 *
924*4882a593Smuzhiyun 	 * To reduce this problem, push up a little bit the
925*4882a593Smuzhiyun 	 * backshifted timestamps of the queue associated with this
926*4882a593Smuzhiyun 	 * entity (only a queue can happen to have the backshifted
927*4882a593Smuzhiyun 	 * flag set): just enough to let the finish timestamp of the
928*4882a593Smuzhiyun 	 * queue be equal to the current value of the system virtual
929*4882a593Smuzhiyun 	 * time. This may introduce a little unfairness among queues
930*4882a593Smuzhiyun 	 * with backshifted timestamps, but it does not break
931*4882a593Smuzhiyun 	 * worst-case fairness guarantees.
932*4882a593Smuzhiyun 	 *
933*4882a593Smuzhiyun 	 * As a special case, if bfqq is weight-raised, push up
934*4882a593Smuzhiyun 	 * timestamps much less, to keep very low the probability that
935*4882a593Smuzhiyun 	 * this push up causes the backshifted finish timestamps of
936*4882a593Smuzhiyun 	 * weight-raised queues to become higher than the backshifted
937*4882a593Smuzhiyun 	 * finish timestamps of non weight-raised queues.
938*4882a593Smuzhiyun 	 */
939*4882a593Smuzhiyun 	if (backshifted && bfq_gt(st->vtime, entity->finish)) {
940*4882a593Smuzhiyun 		unsigned long delta = st->vtime - entity->finish;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 		if (bfqq)
943*4882a593Smuzhiyun 			delta /= bfqq->wr_coeff;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 		entity->start += delta;
946*4882a593Smuzhiyun 		entity->finish += delta;
947*4882a593Smuzhiyun 	}
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	bfq_active_insert(st, entity);
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun /**
953*4882a593Smuzhiyun  * __bfq_activate_entity - handle activation of entity.
954*4882a593Smuzhiyun  * @entity: the entity being activated.
955*4882a593Smuzhiyun  * @non_blocking_wait_rq: true if entity was waiting for a request
956*4882a593Smuzhiyun  *
957*4882a593Smuzhiyun  * Called for a 'true' activation, i.e., if entity is not active and
958*4882a593Smuzhiyun  * one of its children receives a new request.
959*4882a593Smuzhiyun  *
960*4882a593Smuzhiyun  * Basically, this function updates the timestamps of entity and
961*4882a593Smuzhiyun  * inserts entity into its active tree, after possibly extracting it
962*4882a593Smuzhiyun  * from its idle tree.
963*4882a593Smuzhiyun  */
__bfq_activate_entity(struct bfq_entity * entity,bool non_blocking_wait_rq)964*4882a593Smuzhiyun static void __bfq_activate_entity(struct bfq_entity *entity,
965*4882a593Smuzhiyun 				  bool non_blocking_wait_rq)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun 	struct bfq_service_tree *st = bfq_entity_service_tree(entity);
968*4882a593Smuzhiyun 	bool backshifted = false;
969*4882a593Smuzhiyun 	unsigned long long min_vstart;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	/* See comments on bfq_fqq_update_budg_for_activation */
972*4882a593Smuzhiyun 	if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) {
973*4882a593Smuzhiyun 		backshifted = true;
974*4882a593Smuzhiyun 		min_vstart = entity->finish;
975*4882a593Smuzhiyun 	} else
976*4882a593Smuzhiyun 		min_vstart = st->vtime;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	if (entity->tree == &st->idle) {
979*4882a593Smuzhiyun 		/*
980*4882a593Smuzhiyun 		 * Must be on the idle tree, bfq_idle_extract() will
981*4882a593Smuzhiyun 		 * check for that.
982*4882a593Smuzhiyun 		 */
983*4882a593Smuzhiyun 		bfq_idle_extract(st, entity);
984*4882a593Smuzhiyun 		entity->start = bfq_gt(min_vstart, entity->finish) ?
985*4882a593Smuzhiyun 			min_vstart : entity->finish;
986*4882a593Smuzhiyun 	} else {
987*4882a593Smuzhiyun 		/*
988*4882a593Smuzhiyun 		 * The finish time of the entity may be invalid, and
989*4882a593Smuzhiyun 		 * it is in the past for sure, otherwise the queue
990*4882a593Smuzhiyun 		 * would have been on the idle tree.
991*4882a593Smuzhiyun 		 */
992*4882a593Smuzhiyun 		entity->start = min_vstart;
993*4882a593Smuzhiyun 		st->wsum += entity->weight;
994*4882a593Smuzhiyun 		/*
995*4882a593Smuzhiyun 		 * entity is about to be inserted into a service tree,
996*4882a593Smuzhiyun 		 * and then set in service: get a reference to make
997*4882a593Smuzhiyun 		 * sure entity does not disappear until it is no
998*4882a593Smuzhiyun 		 * longer in service or scheduled for service.
999*4882a593Smuzhiyun 		 */
1000*4882a593Smuzhiyun 		bfq_get_entity(entity);
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 		entity->on_st_or_in_serv = true;
1003*4882a593Smuzhiyun 	}
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
1006*4882a593Smuzhiyun 	if (!bfq_entity_to_bfqq(entity)) { /* bfq_group */
1007*4882a593Smuzhiyun 		struct bfq_group *bfqg =
1008*4882a593Smuzhiyun 			container_of(entity, struct bfq_group, entity);
1009*4882a593Smuzhiyun 		struct bfq_data *bfqd = bfqg->bfqd;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 		if (!entity->in_groups_with_pending_reqs) {
1012*4882a593Smuzhiyun 			entity->in_groups_with_pending_reqs = true;
1013*4882a593Smuzhiyun 			bfqd->num_groups_with_pending_reqs++;
1014*4882a593Smuzhiyun 		}
1015*4882a593Smuzhiyun 	}
1016*4882a593Smuzhiyun #endif
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	bfq_update_fin_time_enqueue(entity, st, backshifted);
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /**
1022*4882a593Smuzhiyun  * __bfq_requeue_entity - handle requeueing or repositioning of an entity.
1023*4882a593Smuzhiyun  * @entity: the entity being requeued or repositioned.
1024*4882a593Smuzhiyun  *
1025*4882a593Smuzhiyun  * Requeueing is needed if this entity stops being served, which
1026*4882a593Smuzhiyun  * happens if a leaf descendant entity has expired. On the other hand,
1027*4882a593Smuzhiyun  * repositioning is needed if the next_inservice_entity for the child
1028*4882a593Smuzhiyun  * entity has changed. See the comments inside the function for
1029*4882a593Smuzhiyun  * details.
1030*4882a593Smuzhiyun  *
1031*4882a593Smuzhiyun  * Basically, this function: 1) removes entity from its active tree if
1032*4882a593Smuzhiyun  * present there, 2) updates the timestamps of entity and 3) inserts
1033*4882a593Smuzhiyun  * entity back into its active tree (in the new, right position for
1034*4882a593Smuzhiyun  * the new values of the timestamps).
1035*4882a593Smuzhiyun  */
__bfq_requeue_entity(struct bfq_entity * entity)1036*4882a593Smuzhiyun static void __bfq_requeue_entity(struct bfq_entity *entity)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun 	struct bfq_sched_data *sd = entity->sched_data;
1039*4882a593Smuzhiyun 	struct bfq_service_tree *st = bfq_entity_service_tree(entity);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	if (entity == sd->in_service_entity) {
1042*4882a593Smuzhiyun 		/*
1043*4882a593Smuzhiyun 		 * We are requeueing the current in-service entity,
1044*4882a593Smuzhiyun 		 * which may have to be done for one of the following
1045*4882a593Smuzhiyun 		 * reasons:
1046*4882a593Smuzhiyun 		 * - entity represents the in-service queue, and the
1047*4882a593Smuzhiyun 		 *   in-service queue is being requeued after an
1048*4882a593Smuzhiyun 		 *   expiration;
1049*4882a593Smuzhiyun 		 * - entity represents a group, and its budget has
1050*4882a593Smuzhiyun 		 *   changed because one of its child entities has
1051*4882a593Smuzhiyun 		 *   just been either activated or requeued for some
1052*4882a593Smuzhiyun 		 *   reason; the timestamps of the entity need then to
1053*4882a593Smuzhiyun 		 *   be updated, and the entity needs to be enqueued
1054*4882a593Smuzhiyun 		 *   or repositioned accordingly.
1055*4882a593Smuzhiyun 		 *
1056*4882a593Smuzhiyun 		 * In particular, before requeueing, the start time of
1057*4882a593Smuzhiyun 		 * the entity must be moved forward to account for the
1058*4882a593Smuzhiyun 		 * service that the entity has received while in
1059*4882a593Smuzhiyun 		 * service. This is done by the next instructions. The
1060*4882a593Smuzhiyun 		 * finish time will then be updated according to this
1061*4882a593Smuzhiyun 		 * new value of the start time, and to the budget of
1062*4882a593Smuzhiyun 		 * the entity.
1063*4882a593Smuzhiyun 		 */
1064*4882a593Smuzhiyun 		bfq_calc_finish(entity, entity->service);
1065*4882a593Smuzhiyun 		entity->start = entity->finish;
1066*4882a593Smuzhiyun 		/*
1067*4882a593Smuzhiyun 		 * In addition, if the entity had more than one child
1068*4882a593Smuzhiyun 		 * when set in service, then it was not extracted from
1069*4882a593Smuzhiyun 		 * the active tree. This implies that the position of
1070*4882a593Smuzhiyun 		 * the entity in the active tree may need to be
1071*4882a593Smuzhiyun 		 * changed now, because we have just updated the start
1072*4882a593Smuzhiyun 		 * time of the entity, and we will update its finish
1073*4882a593Smuzhiyun 		 * time in a moment (the requeueing is then, more
1074*4882a593Smuzhiyun 		 * precisely, a repositioning in this case). To
1075*4882a593Smuzhiyun 		 * implement this repositioning, we: 1) dequeue the
1076*4882a593Smuzhiyun 		 * entity here, 2) update the finish time and requeue
1077*4882a593Smuzhiyun 		 * the entity according to the new timestamps below.
1078*4882a593Smuzhiyun 		 */
1079*4882a593Smuzhiyun 		if (entity->tree)
1080*4882a593Smuzhiyun 			bfq_active_extract(st, entity);
1081*4882a593Smuzhiyun 	} else { /* The entity is already active, and not in service */
1082*4882a593Smuzhiyun 		/*
1083*4882a593Smuzhiyun 		 * In this case, this function gets called only if the
1084*4882a593Smuzhiyun 		 * next_in_service entity below this entity has
1085*4882a593Smuzhiyun 		 * changed, and this change has caused the budget of
1086*4882a593Smuzhiyun 		 * this entity to change, which, finally implies that
1087*4882a593Smuzhiyun 		 * the finish time of this entity must be
1088*4882a593Smuzhiyun 		 * updated. Such an update may cause the scheduling,
1089*4882a593Smuzhiyun 		 * i.e., the position in the active tree, of this
1090*4882a593Smuzhiyun 		 * entity to change. We handle this change by: 1)
1091*4882a593Smuzhiyun 		 * dequeueing the entity here, 2) updating the finish
1092*4882a593Smuzhiyun 		 * time and requeueing the entity according to the new
1093*4882a593Smuzhiyun 		 * timestamps below. This is the same approach as the
1094*4882a593Smuzhiyun 		 * non-extracted-entity sub-case above.
1095*4882a593Smuzhiyun 		 */
1096*4882a593Smuzhiyun 		bfq_active_extract(st, entity);
1097*4882a593Smuzhiyun 	}
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	bfq_update_fin_time_enqueue(entity, st, false);
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
__bfq_activate_requeue_entity(struct bfq_entity * entity,struct bfq_sched_data * sd,bool non_blocking_wait_rq)1102*4882a593Smuzhiyun static void __bfq_activate_requeue_entity(struct bfq_entity *entity,
1103*4882a593Smuzhiyun 					  struct bfq_sched_data *sd,
1104*4882a593Smuzhiyun 					  bool non_blocking_wait_rq)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	struct bfq_service_tree *st = bfq_entity_service_tree(entity);
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	if (sd->in_service_entity == entity || entity->tree == &st->active)
1109*4882a593Smuzhiyun 		 /*
1110*4882a593Smuzhiyun 		  * in service or already queued on the active tree,
1111*4882a593Smuzhiyun 		  * requeue or reposition
1112*4882a593Smuzhiyun 		  */
1113*4882a593Smuzhiyun 		__bfq_requeue_entity(entity);
1114*4882a593Smuzhiyun 	else
1115*4882a593Smuzhiyun 		/*
1116*4882a593Smuzhiyun 		 * Not in service and not queued on its active tree:
1117*4882a593Smuzhiyun 		 * the activity is idle and this is a true activation.
1118*4882a593Smuzhiyun 		 */
1119*4882a593Smuzhiyun 		__bfq_activate_entity(entity, non_blocking_wait_rq);
1120*4882a593Smuzhiyun }
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun /**
1124*4882a593Smuzhiyun  * bfq_activate_requeue_entity - activate or requeue an entity representing a
1125*4882a593Smuzhiyun  *				 bfq_queue, and activate, requeue or reposition
1126*4882a593Smuzhiyun  *				 all ancestors for which such an update becomes
1127*4882a593Smuzhiyun  *				 necessary.
1128*4882a593Smuzhiyun  * @entity: the entity to activate.
1129*4882a593Smuzhiyun  * @non_blocking_wait_rq: true if this entity was waiting for a request
1130*4882a593Smuzhiyun  * @requeue: true if this is a requeue, which implies that bfqq is
1131*4882a593Smuzhiyun  *	     being expired; thus ALL its ancestors stop being served and must
1132*4882a593Smuzhiyun  *	     therefore be requeued
1133*4882a593Smuzhiyun  * @expiration: true if this function is being invoked in the expiration path
1134*4882a593Smuzhiyun  *             of the in-service queue
1135*4882a593Smuzhiyun  */
bfq_activate_requeue_entity(struct bfq_entity * entity,bool non_blocking_wait_rq,bool requeue,bool expiration)1136*4882a593Smuzhiyun static void bfq_activate_requeue_entity(struct bfq_entity *entity,
1137*4882a593Smuzhiyun 					bool non_blocking_wait_rq,
1138*4882a593Smuzhiyun 					bool requeue, bool expiration)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun 	struct bfq_sched_data *sd;
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	for_each_entity(entity) {
1143*4882a593Smuzhiyun 		sd = entity->sched_data;
1144*4882a593Smuzhiyun 		__bfq_activate_requeue_entity(entity, sd, non_blocking_wait_rq);
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 		if (!bfq_update_next_in_service(sd, entity, expiration) &&
1147*4882a593Smuzhiyun 		    !requeue)
1148*4882a593Smuzhiyun 			break;
1149*4882a593Smuzhiyun 	}
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun /**
1153*4882a593Smuzhiyun  * __bfq_deactivate_entity - update sched_data and service trees for
1154*4882a593Smuzhiyun  * entity, so as to represent entity as inactive
1155*4882a593Smuzhiyun  * @entity: the entity being deactivated.
1156*4882a593Smuzhiyun  * @ins_into_idle_tree: if false, the entity will not be put into the
1157*4882a593Smuzhiyun  *			idle tree.
1158*4882a593Smuzhiyun  *
1159*4882a593Smuzhiyun  * If necessary and allowed, puts entity into the idle tree. NOTE:
1160*4882a593Smuzhiyun  * entity may be on no tree if in service.
1161*4882a593Smuzhiyun  */
__bfq_deactivate_entity(struct bfq_entity * entity,bool ins_into_idle_tree)1162*4882a593Smuzhiyun bool __bfq_deactivate_entity(struct bfq_entity *entity, bool ins_into_idle_tree)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun 	struct bfq_sched_data *sd = entity->sched_data;
1165*4882a593Smuzhiyun 	struct bfq_service_tree *st;
1166*4882a593Smuzhiyun 	bool is_in_service;
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	if (!entity->on_st_or_in_serv) /*
1169*4882a593Smuzhiyun 					* entity never activated, or
1170*4882a593Smuzhiyun 					* already inactive
1171*4882a593Smuzhiyun 					*/
1172*4882a593Smuzhiyun 		return false;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	/*
1175*4882a593Smuzhiyun 	 * If we get here, then entity is active, which implies that
1176*4882a593Smuzhiyun 	 * bfq_group_set_parent has already been invoked for the group
1177*4882a593Smuzhiyun 	 * represented by entity. Therefore, the field
1178*4882a593Smuzhiyun 	 * entity->sched_data has been set, and we can safely use it.
1179*4882a593Smuzhiyun 	 */
1180*4882a593Smuzhiyun 	st = bfq_entity_service_tree(entity);
1181*4882a593Smuzhiyun 	is_in_service = entity == sd->in_service_entity;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	bfq_calc_finish(entity, entity->service);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	if (is_in_service)
1186*4882a593Smuzhiyun 		sd->in_service_entity = NULL;
1187*4882a593Smuzhiyun 	else
1188*4882a593Smuzhiyun 		/*
1189*4882a593Smuzhiyun 		 * Non in-service entity: nobody will take care of
1190*4882a593Smuzhiyun 		 * resetting its service counter on expiration. Do it
1191*4882a593Smuzhiyun 		 * now.
1192*4882a593Smuzhiyun 		 */
1193*4882a593Smuzhiyun 		entity->service = 0;
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	if (entity->tree == &st->active)
1196*4882a593Smuzhiyun 		bfq_active_extract(st, entity);
1197*4882a593Smuzhiyun 	else if (!is_in_service && entity->tree == &st->idle)
1198*4882a593Smuzhiyun 		bfq_idle_extract(st, entity);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	if (!ins_into_idle_tree || !bfq_gt(entity->finish, st->vtime))
1201*4882a593Smuzhiyun 		bfq_forget_entity(st, entity, is_in_service);
1202*4882a593Smuzhiyun 	else
1203*4882a593Smuzhiyun 		bfq_idle_insert(st, entity);
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	return true;
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun /**
1209*4882a593Smuzhiyun  * bfq_deactivate_entity - deactivate an entity representing a bfq_queue.
1210*4882a593Smuzhiyun  * @entity: the entity to deactivate.
1211*4882a593Smuzhiyun  * @ins_into_idle_tree: true if the entity can be put into the idle tree
1212*4882a593Smuzhiyun  * @expiration: true if this function is being invoked in the expiration path
1213*4882a593Smuzhiyun  *             of the in-service queue
1214*4882a593Smuzhiyun  */
bfq_deactivate_entity(struct bfq_entity * entity,bool ins_into_idle_tree,bool expiration)1215*4882a593Smuzhiyun static void bfq_deactivate_entity(struct bfq_entity *entity,
1216*4882a593Smuzhiyun 				  bool ins_into_idle_tree,
1217*4882a593Smuzhiyun 				  bool expiration)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun 	struct bfq_sched_data *sd;
1220*4882a593Smuzhiyun 	struct bfq_entity *parent = NULL;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	for_each_entity_safe(entity, parent) {
1223*4882a593Smuzhiyun 		sd = entity->sched_data;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		if (!__bfq_deactivate_entity(entity, ins_into_idle_tree)) {
1226*4882a593Smuzhiyun 			/*
1227*4882a593Smuzhiyun 			 * entity is not in any tree any more, so
1228*4882a593Smuzhiyun 			 * this deactivation is a no-op, and there is
1229*4882a593Smuzhiyun 			 * nothing to change for upper-level entities
1230*4882a593Smuzhiyun 			 * (in case of expiration, this can never
1231*4882a593Smuzhiyun 			 * happen).
1232*4882a593Smuzhiyun 			 */
1233*4882a593Smuzhiyun 			return;
1234*4882a593Smuzhiyun 		}
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 		if (sd->next_in_service == entity)
1237*4882a593Smuzhiyun 			/*
1238*4882a593Smuzhiyun 			 * entity was the next_in_service entity,
1239*4882a593Smuzhiyun 			 * then, since entity has just been
1240*4882a593Smuzhiyun 			 * deactivated, a new one must be found.
1241*4882a593Smuzhiyun 			 */
1242*4882a593Smuzhiyun 			bfq_update_next_in_service(sd, NULL, expiration);
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 		if (sd->next_in_service || sd->in_service_entity) {
1245*4882a593Smuzhiyun 			/*
1246*4882a593Smuzhiyun 			 * The parent entity is still active, because
1247*4882a593Smuzhiyun 			 * either next_in_service or in_service_entity
1248*4882a593Smuzhiyun 			 * is not NULL. So, no further upwards
1249*4882a593Smuzhiyun 			 * deactivation must be performed.  Yet,
1250*4882a593Smuzhiyun 			 * next_in_service has changed.	Then the
1251*4882a593Smuzhiyun 			 * schedule does need to be updated upwards.
1252*4882a593Smuzhiyun 			 *
1253*4882a593Smuzhiyun 			 * NOTE If in_service_entity is not NULL, then
1254*4882a593Smuzhiyun 			 * next_in_service may happen to be NULL,
1255*4882a593Smuzhiyun 			 * although the parent entity is evidently
1256*4882a593Smuzhiyun 			 * active. This happens if 1) the entity
1257*4882a593Smuzhiyun 			 * pointed by in_service_entity is the only
1258*4882a593Smuzhiyun 			 * active entity in the parent entity, and 2)
1259*4882a593Smuzhiyun 			 * according to the definition of
1260*4882a593Smuzhiyun 			 * next_in_service, the in_service_entity
1261*4882a593Smuzhiyun 			 * cannot be considered as
1262*4882a593Smuzhiyun 			 * next_in_service. See the comments on the
1263*4882a593Smuzhiyun 			 * definition of next_in_service for details.
1264*4882a593Smuzhiyun 			 */
1265*4882a593Smuzhiyun 			break;
1266*4882a593Smuzhiyun 		}
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 		/*
1269*4882a593Smuzhiyun 		 * If we get here, then the parent is no more
1270*4882a593Smuzhiyun 		 * backlogged and we need to propagate the
1271*4882a593Smuzhiyun 		 * deactivation upwards. Thus let the loop go on.
1272*4882a593Smuzhiyun 		 */
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 		/*
1275*4882a593Smuzhiyun 		 * Also let parent be queued into the idle tree on
1276*4882a593Smuzhiyun 		 * deactivation, to preserve service guarantees, and
1277*4882a593Smuzhiyun 		 * assuming that who invoked this function does not
1278*4882a593Smuzhiyun 		 * need parent entities too to be removed completely.
1279*4882a593Smuzhiyun 		 */
1280*4882a593Smuzhiyun 		ins_into_idle_tree = true;
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	/*
1284*4882a593Smuzhiyun 	 * If the deactivation loop is fully executed, then there are
1285*4882a593Smuzhiyun 	 * no more entities to touch and next loop is not executed at
1286*4882a593Smuzhiyun 	 * all. Otherwise, requeue remaining entities if they are
1287*4882a593Smuzhiyun 	 * about to stop receiving service, or reposition them if this
1288*4882a593Smuzhiyun 	 * is not the case.
1289*4882a593Smuzhiyun 	 */
1290*4882a593Smuzhiyun 	entity = parent;
1291*4882a593Smuzhiyun 	for_each_entity(entity) {
1292*4882a593Smuzhiyun 		/*
1293*4882a593Smuzhiyun 		 * Invoke __bfq_requeue_entity on entity, even if
1294*4882a593Smuzhiyun 		 * already active, to requeue/reposition it in the
1295*4882a593Smuzhiyun 		 * active tree (because sd->next_in_service has
1296*4882a593Smuzhiyun 		 * changed)
1297*4882a593Smuzhiyun 		 */
1298*4882a593Smuzhiyun 		__bfq_requeue_entity(entity);
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 		sd = entity->sched_data;
1301*4882a593Smuzhiyun 		if (!bfq_update_next_in_service(sd, entity, expiration) &&
1302*4882a593Smuzhiyun 		    !expiration)
1303*4882a593Smuzhiyun 			/*
1304*4882a593Smuzhiyun 			 * next_in_service unchanged or not causing
1305*4882a593Smuzhiyun 			 * any change in entity->parent->sd, and no
1306*4882a593Smuzhiyun 			 * requeueing needed for expiration: stop
1307*4882a593Smuzhiyun 			 * here.
1308*4882a593Smuzhiyun 			 */
1309*4882a593Smuzhiyun 			break;
1310*4882a593Smuzhiyun 	}
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun /**
1314*4882a593Smuzhiyun  * bfq_calc_vtime_jump - compute the value to which the vtime should jump,
1315*4882a593Smuzhiyun  *                       if needed, to have at least one entity eligible.
1316*4882a593Smuzhiyun  * @st: the service tree to act upon.
1317*4882a593Smuzhiyun  *
1318*4882a593Smuzhiyun  * Assumes that st is not empty.
1319*4882a593Smuzhiyun  */
bfq_calc_vtime_jump(struct bfq_service_tree * st)1320*4882a593Smuzhiyun static u64 bfq_calc_vtime_jump(struct bfq_service_tree *st)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun 	struct bfq_entity *root_entity = bfq_root_active_entity(&st->active);
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	if (bfq_gt(root_entity->min_start, st->vtime))
1325*4882a593Smuzhiyun 		return root_entity->min_start;
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	return st->vtime;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun 
bfq_update_vtime(struct bfq_service_tree * st,u64 new_value)1330*4882a593Smuzhiyun static void bfq_update_vtime(struct bfq_service_tree *st, u64 new_value)
1331*4882a593Smuzhiyun {
1332*4882a593Smuzhiyun 	if (new_value > st->vtime) {
1333*4882a593Smuzhiyun 		st->vtime = new_value;
1334*4882a593Smuzhiyun 		bfq_forget_idle(st);
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun /**
1339*4882a593Smuzhiyun  * bfq_first_active_entity - find the eligible entity with
1340*4882a593Smuzhiyun  *                           the smallest finish time
1341*4882a593Smuzhiyun  * @st: the service tree to select from.
1342*4882a593Smuzhiyun  * @vtime: the system virtual to use as a reference for eligibility
1343*4882a593Smuzhiyun  *
1344*4882a593Smuzhiyun  * This function searches the first schedulable entity, starting from the
1345*4882a593Smuzhiyun  * root of the tree and going on the left every time on this side there is
1346*4882a593Smuzhiyun  * a subtree with at least one eligible (start <= vtime) entity. The path on
1347*4882a593Smuzhiyun  * the right is followed only if a) the left subtree contains no eligible
1348*4882a593Smuzhiyun  * entities and b) no eligible entity has been found yet.
1349*4882a593Smuzhiyun  */
bfq_first_active_entity(struct bfq_service_tree * st,u64 vtime)1350*4882a593Smuzhiyun static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st,
1351*4882a593Smuzhiyun 						  u64 vtime)
1352*4882a593Smuzhiyun {
1353*4882a593Smuzhiyun 	struct bfq_entity *entry, *first = NULL;
1354*4882a593Smuzhiyun 	struct rb_node *node = st->active.rb_node;
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	while (node) {
1357*4882a593Smuzhiyun 		entry = rb_entry(node, struct bfq_entity, rb_node);
1358*4882a593Smuzhiyun left:
1359*4882a593Smuzhiyun 		if (!bfq_gt(entry->start, vtime))
1360*4882a593Smuzhiyun 			first = entry;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 		if (node->rb_left) {
1363*4882a593Smuzhiyun 			entry = rb_entry(node->rb_left,
1364*4882a593Smuzhiyun 					 struct bfq_entity, rb_node);
1365*4882a593Smuzhiyun 			if (!bfq_gt(entry->min_start, vtime)) {
1366*4882a593Smuzhiyun 				node = node->rb_left;
1367*4882a593Smuzhiyun 				goto left;
1368*4882a593Smuzhiyun 			}
1369*4882a593Smuzhiyun 		}
1370*4882a593Smuzhiyun 		if (first)
1371*4882a593Smuzhiyun 			break;
1372*4882a593Smuzhiyun 		node = node->rb_right;
1373*4882a593Smuzhiyun 	}
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	return first;
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun /**
1379*4882a593Smuzhiyun  * __bfq_lookup_next_entity - return the first eligible entity in @st.
1380*4882a593Smuzhiyun  * @st: the service tree.
1381*4882a593Smuzhiyun  *
1382*4882a593Smuzhiyun  * If there is no in-service entity for the sched_data st belongs to,
1383*4882a593Smuzhiyun  * then return the entity that will be set in service if:
1384*4882a593Smuzhiyun  * 1) the parent entity this st belongs to is set in service;
1385*4882a593Smuzhiyun  * 2) no entity belonging to such parent entity undergoes a state change
1386*4882a593Smuzhiyun  * that would influence the timestamps of the entity (e.g., becomes idle,
1387*4882a593Smuzhiyun  * becomes backlogged, changes its budget, ...).
1388*4882a593Smuzhiyun  *
1389*4882a593Smuzhiyun  * In this first case, update the virtual time in @st too (see the
1390*4882a593Smuzhiyun  * comments on this update inside the function).
1391*4882a593Smuzhiyun  *
1392*4882a593Smuzhiyun  * In contrast, if there is an in-service entity, then return the
1393*4882a593Smuzhiyun  * entity that would be set in service if not only the above
1394*4882a593Smuzhiyun  * conditions, but also the next one held true: the currently
1395*4882a593Smuzhiyun  * in-service entity, on expiration,
1396*4882a593Smuzhiyun  * 1) gets a finish time equal to the current one, or
1397*4882a593Smuzhiyun  * 2) is not eligible any more, or
1398*4882a593Smuzhiyun  * 3) is idle.
1399*4882a593Smuzhiyun  */
1400*4882a593Smuzhiyun static struct bfq_entity *
__bfq_lookup_next_entity(struct bfq_service_tree * st,bool in_service)1401*4882a593Smuzhiyun __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service)
1402*4882a593Smuzhiyun {
1403*4882a593Smuzhiyun 	struct bfq_entity *entity;
1404*4882a593Smuzhiyun 	u64 new_vtime;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(&st->active))
1407*4882a593Smuzhiyun 		return NULL;
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	/*
1410*4882a593Smuzhiyun 	 * Get the value of the system virtual time for which at
1411*4882a593Smuzhiyun 	 * least one entity is eligible.
1412*4882a593Smuzhiyun 	 */
1413*4882a593Smuzhiyun 	new_vtime = bfq_calc_vtime_jump(st);
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	/*
1416*4882a593Smuzhiyun 	 * If there is no in-service entity for the sched_data this
1417*4882a593Smuzhiyun 	 * active tree belongs to, then push the system virtual time
1418*4882a593Smuzhiyun 	 * up to the value that guarantees that at least one entity is
1419*4882a593Smuzhiyun 	 * eligible. If, instead, there is an in-service entity, then
1420*4882a593Smuzhiyun 	 * do not make any such update, because there is already an
1421*4882a593Smuzhiyun 	 * eligible entity, namely the in-service one (even if the
1422*4882a593Smuzhiyun 	 * entity is not on st, because it was extracted when set in
1423*4882a593Smuzhiyun 	 * service).
1424*4882a593Smuzhiyun 	 */
1425*4882a593Smuzhiyun 	if (!in_service)
1426*4882a593Smuzhiyun 		bfq_update_vtime(st, new_vtime);
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	entity = bfq_first_active_entity(st, new_vtime);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	return entity;
1431*4882a593Smuzhiyun }
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun /**
1434*4882a593Smuzhiyun  * bfq_lookup_next_entity - return the first eligible entity in @sd.
1435*4882a593Smuzhiyun  * @sd: the sched_data.
1436*4882a593Smuzhiyun  * @expiration: true if we are on the expiration path of the in-service queue
1437*4882a593Smuzhiyun  *
1438*4882a593Smuzhiyun  * This function is invoked when there has been a change in the trees
1439*4882a593Smuzhiyun  * for sd, and we need to know what is the new next entity to serve
1440*4882a593Smuzhiyun  * after this change.
1441*4882a593Smuzhiyun  */
bfq_lookup_next_entity(struct bfq_sched_data * sd,bool expiration)1442*4882a593Smuzhiyun static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
1443*4882a593Smuzhiyun 						 bool expiration)
1444*4882a593Smuzhiyun {
1445*4882a593Smuzhiyun 	struct bfq_service_tree *st = sd->service_tree;
1446*4882a593Smuzhiyun 	struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1);
1447*4882a593Smuzhiyun 	struct bfq_entity *entity = NULL;
1448*4882a593Smuzhiyun 	int class_idx = 0;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	/*
1451*4882a593Smuzhiyun 	 * Choose from idle class, if needed to guarantee a minimum
1452*4882a593Smuzhiyun 	 * bandwidth to this class (and if there is some active entity
1453*4882a593Smuzhiyun 	 * in idle class). This should also mitigate
1454*4882a593Smuzhiyun 	 * priority-inversion problems in case a low priority task is
1455*4882a593Smuzhiyun 	 * holding file system resources.
1456*4882a593Smuzhiyun 	 */
1457*4882a593Smuzhiyun 	if (time_is_before_jiffies(sd->bfq_class_idle_last_service +
1458*4882a593Smuzhiyun 				   BFQ_CL_IDLE_TIMEOUT)) {
1459*4882a593Smuzhiyun 		if (!RB_EMPTY_ROOT(&idle_class_st->active))
1460*4882a593Smuzhiyun 			class_idx = BFQ_IOPRIO_CLASSES - 1;
1461*4882a593Smuzhiyun 		/* About to be served if backlogged, or not yet backlogged */
1462*4882a593Smuzhiyun 		sd->bfq_class_idle_last_service = jiffies;
1463*4882a593Smuzhiyun 	}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	/*
1466*4882a593Smuzhiyun 	 * Find the next entity to serve for the highest-priority
1467*4882a593Smuzhiyun 	 * class, unless the idle class needs to be served.
1468*4882a593Smuzhiyun 	 */
1469*4882a593Smuzhiyun 	for (; class_idx < BFQ_IOPRIO_CLASSES; class_idx++) {
1470*4882a593Smuzhiyun 		/*
1471*4882a593Smuzhiyun 		 * If expiration is true, then bfq_lookup_next_entity
1472*4882a593Smuzhiyun 		 * is being invoked as a part of the expiration path
1473*4882a593Smuzhiyun 		 * of the in-service queue. In this case, even if
1474*4882a593Smuzhiyun 		 * sd->in_service_entity is not NULL,
1475*4882a593Smuzhiyun 		 * sd->in_service_entity at this point is actually not
1476*4882a593Smuzhiyun 		 * in service any more, and, if needed, has already
1477*4882a593Smuzhiyun 		 * been properly queued or requeued into the right
1478*4882a593Smuzhiyun 		 * tree. The reason why sd->in_service_entity is still
1479*4882a593Smuzhiyun 		 * not NULL here, even if expiration is true, is that
1480*4882a593Smuzhiyun 		 * sd->in_service_entity is reset as a last step in the
1481*4882a593Smuzhiyun 		 * expiration path. So, if expiration is true, tell
1482*4882a593Smuzhiyun 		 * __bfq_lookup_next_entity that there is no
1483*4882a593Smuzhiyun 		 * sd->in_service_entity.
1484*4882a593Smuzhiyun 		 */
1485*4882a593Smuzhiyun 		entity = __bfq_lookup_next_entity(st + class_idx,
1486*4882a593Smuzhiyun 						  sd->in_service_entity &&
1487*4882a593Smuzhiyun 						  !expiration);
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 		if (entity)
1490*4882a593Smuzhiyun 			break;
1491*4882a593Smuzhiyun 	}
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	if (!entity)
1494*4882a593Smuzhiyun 		return NULL;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	return entity;
1497*4882a593Smuzhiyun }
1498*4882a593Smuzhiyun 
next_queue_may_preempt(struct bfq_data * bfqd)1499*4882a593Smuzhiyun bool next_queue_may_preempt(struct bfq_data *bfqd)
1500*4882a593Smuzhiyun {
1501*4882a593Smuzhiyun 	struct bfq_sched_data *sd = &bfqd->root_group->sched_data;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	return sd->next_in_service != sd->in_service_entity;
1504*4882a593Smuzhiyun }
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun /*
1507*4882a593Smuzhiyun  * Get next queue for service.
1508*4882a593Smuzhiyun  */
bfq_get_next_queue(struct bfq_data * bfqd)1509*4882a593Smuzhiyun struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
1510*4882a593Smuzhiyun {
1511*4882a593Smuzhiyun 	struct bfq_entity *entity = NULL;
1512*4882a593Smuzhiyun 	struct bfq_sched_data *sd;
1513*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun 	if (bfq_tot_busy_queues(bfqd) == 0)
1516*4882a593Smuzhiyun 		return NULL;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	/*
1519*4882a593Smuzhiyun 	 * Traverse the path from the root to the leaf entity to
1520*4882a593Smuzhiyun 	 * serve. Set in service all the entities visited along the
1521*4882a593Smuzhiyun 	 * way.
1522*4882a593Smuzhiyun 	 */
1523*4882a593Smuzhiyun 	sd = &bfqd->root_group->sched_data;
1524*4882a593Smuzhiyun 	for (; sd ; sd = entity->my_sched_data) {
1525*4882a593Smuzhiyun 		/*
1526*4882a593Smuzhiyun 		 * WARNING. We are about to set the in-service entity
1527*4882a593Smuzhiyun 		 * to sd->next_in_service, i.e., to the (cached) value
1528*4882a593Smuzhiyun 		 * returned by bfq_lookup_next_entity(sd) the last
1529*4882a593Smuzhiyun 		 * time it was invoked, i.e., the last time when the
1530*4882a593Smuzhiyun 		 * service order in sd changed as a consequence of the
1531*4882a593Smuzhiyun 		 * activation or deactivation of an entity. In this
1532*4882a593Smuzhiyun 		 * respect, if we execute bfq_lookup_next_entity(sd)
1533*4882a593Smuzhiyun 		 * in this very moment, it may, although with low
1534*4882a593Smuzhiyun 		 * probability, yield a different entity than that
1535*4882a593Smuzhiyun 		 * pointed to by sd->next_in_service. This rare event
1536*4882a593Smuzhiyun 		 * happens in case there was no CLASS_IDLE entity to
1537*4882a593Smuzhiyun 		 * serve for sd when bfq_lookup_next_entity(sd) was
1538*4882a593Smuzhiyun 		 * invoked for the last time, while there is now one
1539*4882a593Smuzhiyun 		 * such entity.
1540*4882a593Smuzhiyun 		 *
1541*4882a593Smuzhiyun 		 * If the above event happens, then the scheduling of
1542*4882a593Smuzhiyun 		 * such entity in CLASS_IDLE is postponed until the
1543*4882a593Smuzhiyun 		 * service of the sd->next_in_service entity
1544*4882a593Smuzhiyun 		 * finishes. In fact, when the latter is expired,
1545*4882a593Smuzhiyun 		 * bfq_lookup_next_entity(sd) gets called again,
1546*4882a593Smuzhiyun 		 * exactly to update sd->next_in_service.
1547*4882a593Smuzhiyun 		 */
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 		/* Make next_in_service entity become in_service_entity */
1550*4882a593Smuzhiyun 		entity = sd->next_in_service;
1551*4882a593Smuzhiyun 		sd->in_service_entity = entity;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 		/*
1554*4882a593Smuzhiyun 		 * If entity is no longer a candidate for next
1555*4882a593Smuzhiyun 		 * service, then it must be extracted from its active
1556*4882a593Smuzhiyun 		 * tree, so as to make sure that it won't be
1557*4882a593Smuzhiyun 		 * considered when computing next_in_service. See the
1558*4882a593Smuzhiyun 		 * comments on the function
1559*4882a593Smuzhiyun 		 * bfq_no_longer_next_in_service() for details.
1560*4882a593Smuzhiyun 		 */
1561*4882a593Smuzhiyun 		if (bfq_no_longer_next_in_service(entity))
1562*4882a593Smuzhiyun 			bfq_active_extract(bfq_entity_service_tree(entity),
1563*4882a593Smuzhiyun 					   entity);
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 		/*
1566*4882a593Smuzhiyun 		 * Even if entity is not to be extracted according to
1567*4882a593Smuzhiyun 		 * the above check, a descendant entity may get
1568*4882a593Smuzhiyun 		 * extracted in one of the next iterations of this
1569*4882a593Smuzhiyun 		 * loop. Such an event could cause a change in
1570*4882a593Smuzhiyun 		 * next_in_service for the level of the descendant
1571*4882a593Smuzhiyun 		 * entity, and thus possibly back to this level.
1572*4882a593Smuzhiyun 		 *
1573*4882a593Smuzhiyun 		 * However, we cannot perform the resulting needed
1574*4882a593Smuzhiyun 		 * update of next_in_service for this level before the
1575*4882a593Smuzhiyun 		 * end of the whole loop, because, to know which is
1576*4882a593Smuzhiyun 		 * the correct next-to-serve candidate entity for each
1577*4882a593Smuzhiyun 		 * level, we need first to find the leaf entity to set
1578*4882a593Smuzhiyun 		 * in service. In fact, only after we know which is
1579*4882a593Smuzhiyun 		 * the next-to-serve leaf entity, we can discover
1580*4882a593Smuzhiyun 		 * whether the parent entity of the leaf entity
1581*4882a593Smuzhiyun 		 * becomes the next-to-serve, and so on.
1582*4882a593Smuzhiyun 		 */
1583*4882a593Smuzhiyun 	}
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	bfqq = bfq_entity_to_bfqq(entity);
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	/*
1588*4882a593Smuzhiyun 	 * We can finally update all next-to-serve entities along the
1589*4882a593Smuzhiyun 	 * path from the leaf entity just set in service to the root.
1590*4882a593Smuzhiyun 	 */
1591*4882a593Smuzhiyun 	for_each_entity(entity) {
1592*4882a593Smuzhiyun 		struct bfq_sched_data *sd = entity->sched_data;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 		if (!bfq_update_next_in_service(sd, NULL, false))
1595*4882a593Smuzhiyun 			break;
1596*4882a593Smuzhiyun 	}
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun 	return bfqq;
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun /* returns true if the in-service queue gets freed */
__bfq_bfqd_reset_in_service(struct bfq_data * bfqd)1602*4882a593Smuzhiyun bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd)
1603*4882a593Smuzhiyun {
1604*4882a593Smuzhiyun 	struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue;
1605*4882a593Smuzhiyun 	struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity;
1606*4882a593Smuzhiyun 	struct bfq_entity *entity = in_serv_entity;
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun 	bfq_clear_bfqq_wait_request(in_serv_bfqq);
1609*4882a593Smuzhiyun 	hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
1610*4882a593Smuzhiyun 	bfqd->in_service_queue = NULL;
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 	/*
1613*4882a593Smuzhiyun 	 * When this function is called, all in-service entities have
1614*4882a593Smuzhiyun 	 * been properly deactivated or requeued, so we can safely
1615*4882a593Smuzhiyun 	 * execute the final step: reset in_service_entity along the
1616*4882a593Smuzhiyun 	 * path from entity to the root.
1617*4882a593Smuzhiyun 	 */
1618*4882a593Smuzhiyun 	for_each_entity(entity)
1619*4882a593Smuzhiyun 		entity->sched_data->in_service_entity = NULL;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	/*
1622*4882a593Smuzhiyun 	 * in_serv_entity is no longer in service, so, if it is in no
1623*4882a593Smuzhiyun 	 * service tree either, then release the service reference to
1624*4882a593Smuzhiyun 	 * the queue it represents (taken with bfq_get_entity).
1625*4882a593Smuzhiyun 	 */
1626*4882a593Smuzhiyun 	if (!in_serv_entity->on_st_or_in_serv) {
1627*4882a593Smuzhiyun 		/*
1628*4882a593Smuzhiyun 		 * If no process is referencing in_serv_bfqq any
1629*4882a593Smuzhiyun 		 * longer, then the service reference may be the only
1630*4882a593Smuzhiyun 		 * reference to the queue. If this is the case, then
1631*4882a593Smuzhiyun 		 * bfqq gets freed here.
1632*4882a593Smuzhiyun 		 */
1633*4882a593Smuzhiyun 		int ref = in_serv_bfqq->ref;
1634*4882a593Smuzhiyun 		bfq_put_queue(in_serv_bfqq);
1635*4882a593Smuzhiyun 		if (ref == 1)
1636*4882a593Smuzhiyun 			return true;
1637*4882a593Smuzhiyun 	}
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	return false;
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun 
bfq_deactivate_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool ins_into_idle_tree,bool expiration)1642*4882a593Smuzhiyun void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1643*4882a593Smuzhiyun 			 bool ins_into_idle_tree, bool expiration)
1644*4882a593Smuzhiyun {
1645*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 	bfq_deactivate_entity(entity, ins_into_idle_tree, expiration);
1648*4882a593Smuzhiyun }
1649*4882a593Smuzhiyun 
bfq_activate_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq)1650*4882a593Smuzhiyun void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1651*4882a593Smuzhiyun {
1652*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	bfq_activate_requeue_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq),
1655*4882a593Smuzhiyun 				    false, false);
1656*4882a593Smuzhiyun 	bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun 
bfq_requeue_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool expiration)1659*4882a593Smuzhiyun void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1660*4882a593Smuzhiyun 		      bool expiration)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	bfq_activate_requeue_entity(entity, false,
1665*4882a593Smuzhiyun 				    bfqq == bfqd->in_service_queue, expiration);
1666*4882a593Smuzhiyun }
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun /*
1669*4882a593Smuzhiyun  * Called when the bfqq no longer has requests pending, remove it from
1670*4882a593Smuzhiyun  * the service tree. As a special case, it can be invoked during an
1671*4882a593Smuzhiyun  * expiration.
1672*4882a593Smuzhiyun  */
bfq_del_bfqq_busy(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool expiration)1673*4882a593Smuzhiyun void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1674*4882a593Smuzhiyun 		       bool expiration)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "del from busy");
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	bfq_clear_bfqq_busy(bfqq);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	bfqd->busy_queues[bfqq->ioprio_class - 1]--;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1)
1683*4882a593Smuzhiyun 		bfqd->wr_busy_queues--;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	bfqg_stats_update_dequeue(bfqq_group(bfqq));
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	bfq_deactivate_bfqq(bfqd, bfqq, true, expiration);
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	if (!bfqq->dispatched)
1690*4882a593Smuzhiyun 		bfq_weights_tree_remove(bfqd, bfqq);
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun /*
1694*4882a593Smuzhiyun  * Called when an inactive queue receives a new request.
1695*4882a593Smuzhiyun  */
bfq_add_bfqq_busy(struct bfq_data * bfqd,struct bfq_queue * bfqq)1696*4882a593Smuzhiyun void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1697*4882a593Smuzhiyun {
1698*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "add to busy");
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	bfq_activate_bfqq(bfqd, bfqq);
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	bfq_mark_bfqq_busy(bfqq);
1703*4882a593Smuzhiyun 	bfqd->busy_queues[bfqq->ioprio_class - 1]++;
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 	if (!bfqq->dispatched)
1706*4882a593Smuzhiyun 		if (bfqq->wr_coeff == 1)
1707*4882a593Smuzhiyun 			bfq_weights_tree_add(bfqd, bfqq,
1708*4882a593Smuzhiyun 					     &bfqd->queue_weights_tree);
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1)
1711*4882a593Smuzhiyun 		bfqd->wr_busy_queues++;
1712*4882a593Smuzhiyun }
1713