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