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