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 */
bfq_gt(u64 a,u64 b)18ea25da48SPaolo Valente static int bfq_gt(u64 a, u64 b)
19ea25da48SPaolo Valente {
20ea25da48SPaolo Valente return (s64)(a - b) > 0;
21ea25da48SPaolo Valente }
22ea25da48SPaolo Valente
bfq_root_active_entity(struct rb_root * tree)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
bfq_class_idx(struct bfq_entity * entity)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
bfq_tot_busy_queues(struct bfq_data * bfqd)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 */
bfq_update_next_in_service(struct bfq_sched_data * sd,struct bfq_entity * new_entity,bool expiration)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 /*
146ea25da48SPaolo Valente * Returns true if this budget changes may let next_in_service->parent
147ea25da48SPaolo Valente * become the next_in_service entity for its parent entity.
148ea25da48SPaolo Valente */
bfq_update_parent_budget(struct bfq_entity * next_in_service)149ea25da48SPaolo Valente static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
150ea25da48SPaolo Valente {
151ea25da48SPaolo Valente struct bfq_entity *bfqg_entity;
152ea25da48SPaolo Valente struct bfq_group *bfqg;
153ea25da48SPaolo Valente struct bfq_sched_data *group_sd;
154ea25da48SPaolo Valente bool ret = false;
155ea25da48SPaolo Valente
156ea25da48SPaolo Valente group_sd = next_in_service->sched_data;
157ea25da48SPaolo Valente
158ea25da48SPaolo Valente bfqg = container_of(group_sd, struct bfq_group, sched_data);
159ea25da48SPaolo Valente /*
160ea25da48SPaolo Valente * bfq_group's my_entity field is not NULL only if the group
161ea25da48SPaolo Valente * is not the root group. We must not touch the root entity
162ea25da48SPaolo Valente * as it must never become an in-service entity.
163ea25da48SPaolo Valente */
164ea25da48SPaolo Valente bfqg_entity = bfqg->my_entity;
165ea25da48SPaolo Valente if (bfqg_entity) {
166ea25da48SPaolo Valente if (bfqg_entity->budget > next_in_service->budget)
167ea25da48SPaolo Valente ret = true;
168ea25da48SPaolo Valente bfqg_entity->budget = next_in_service->budget;
169ea25da48SPaolo Valente }
170ea25da48SPaolo Valente
171ea25da48SPaolo Valente return ret;
172ea25da48SPaolo Valente }
173ea25da48SPaolo Valente
174ea25da48SPaolo Valente /*
175ea25da48SPaolo Valente * This function tells whether entity stops being a candidate for next
17646d556e6SPaolo Valente * service, according to the restrictive definition of the field
17746d556e6SPaolo Valente * next_in_service. In particular, this function is invoked for an
17846d556e6SPaolo Valente * entity that is about to be set in service.
179ea25da48SPaolo Valente *
18046d556e6SPaolo Valente * If entity is a queue, then the entity is no longer a candidate for
18146d556e6SPaolo Valente * next service according to the that definition, because entity is
18246d556e6SPaolo Valente * about to become the in-service queue. This function then returns
18346d556e6SPaolo Valente * true if entity is a queue.
184ea25da48SPaolo Valente *
18546d556e6SPaolo Valente * In contrast, entity could still be a candidate for next service if
18646d556e6SPaolo Valente * it is not a queue, and has more than one active child. In fact,
18746d556e6SPaolo Valente * even if one of its children is about to be set in service, other
18846d556e6SPaolo Valente * active children may still be the next to serve, for the parent
18946d556e6SPaolo Valente * entity, even according to the above definition. As a consequence, a
19046d556e6SPaolo Valente * non-queue entity is not a candidate for next-service only if it has
19146d556e6SPaolo Valente * only one active child. And only if this condition holds, then this
19246d556e6SPaolo Valente * function returns true for a non-queue entity.
193ea25da48SPaolo Valente */
bfq_no_longer_next_in_service(struct bfq_entity * entity)194ea25da48SPaolo Valente static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
195ea25da48SPaolo Valente {
196ea25da48SPaolo Valente struct bfq_group *bfqg;
197ea25da48SPaolo Valente
198ea25da48SPaolo Valente if (bfq_entity_to_bfqq(entity))
199ea25da48SPaolo Valente return true;
200ea25da48SPaolo Valente
201ea25da48SPaolo Valente bfqg = container_of(entity, struct bfq_group, entity);
202ea25da48SPaolo Valente
20346d556e6SPaolo Valente /*
20446d556e6SPaolo Valente * The field active_entities does not always contain the
20546d556e6SPaolo Valente * actual number of active children entities: it happens to
20646d556e6SPaolo Valente * not account for the in-service entity in case the latter is
20746d556e6SPaolo Valente * removed from its active tree (which may get done after
20846d556e6SPaolo Valente * invoking the function bfq_no_longer_next_in_service in
20946d556e6SPaolo Valente * bfq_get_next_queue). Fortunately, here, i.e., while
21046d556e6SPaolo Valente * bfq_no_longer_next_in_service is not yet completed in
21146d556e6SPaolo Valente * bfq_get_next_queue, bfq_active_extract has not yet been
21246d556e6SPaolo Valente * invoked, and thus active_entities still coincides with the
21346d556e6SPaolo Valente * actual number of active entities.
21446d556e6SPaolo Valente */
215ea25da48SPaolo Valente if (bfqg->active_entities == 1)
216ea25da48SPaolo Valente return true;
217ea25da48SPaolo Valente
218ea25da48SPaolo Valente return false;
219ea25da48SPaolo Valente }
220ea25da48SPaolo Valente
bfq_inc_active_entities(struct bfq_entity * entity)221e5c63eb4SYu Kuai static void bfq_inc_active_entities(struct bfq_entity *entity)
222e5c63eb4SYu Kuai {
223e5c63eb4SYu Kuai struct bfq_sched_data *sd = entity->sched_data;
224e5c63eb4SYu Kuai struct bfq_group *bfqg = container_of(sd, struct bfq_group, sched_data);
225e5c63eb4SYu Kuai
226aa625117SYu Kuai if (bfqg != bfqg->bfqd->root_group)
227e5c63eb4SYu Kuai bfqg->active_entities++;
228e5c63eb4SYu Kuai }
229e5c63eb4SYu Kuai
bfq_dec_active_entities(struct bfq_entity * entity)230e5c63eb4SYu Kuai static void bfq_dec_active_entities(struct bfq_entity *entity)
231e5c63eb4SYu Kuai {
232e5c63eb4SYu Kuai struct bfq_sched_data *sd = entity->sched_data;
233e5c63eb4SYu Kuai struct bfq_group *bfqg = container_of(sd, struct bfq_group, sched_data);
234e5c63eb4SYu Kuai
235aa625117SYu Kuai if (bfqg != bfqg->bfqd->root_group)
236e5c63eb4SYu Kuai bfqg->active_entities--;
237e5c63eb4SYu Kuai }
238e5c63eb4SYu Kuai
239ea25da48SPaolo Valente #else /* CONFIG_BFQ_GROUP_IOSCHED */
240ea25da48SPaolo Valente
bfq_update_parent_budget(struct bfq_entity * next_in_service)241ea25da48SPaolo Valente static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
242ea25da48SPaolo Valente {
243ea25da48SPaolo Valente return false;
244ea25da48SPaolo Valente }
245ea25da48SPaolo Valente
bfq_no_longer_next_in_service(struct bfq_entity * entity)246ea25da48SPaolo Valente static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
247ea25da48SPaolo Valente {
248ea25da48SPaolo Valente return true;
249ea25da48SPaolo Valente }
250ea25da48SPaolo Valente
bfq_inc_active_entities(struct bfq_entity * entity)251e5c63eb4SYu Kuai static void bfq_inc_active_entities(struct bfq_entity *entity)
252e5c63eb4SYu Kuai {
253e5c63eb4SYu Kuai }
254e5c63eb4SYu Kuai
bfq_dec_active_entities(struct bfq_entity * entity)255e5c63eb4SYu Kuai static void bfq_dec_active_entities(struct bfq_entity *entity)
256e5c63eb4SYu Kuai {
257e5c63eb4SYu Kuai }
258e5c63eb4SYu Kuai
259ea25da48SPaolo Valente #endif /* CONFIG_BFQ_GROUP_IOSCHED */
260ea25da48SPaolo Valente
261ea25da48SPaolo Valente /*
262ea25da48SPaolo Valente * Shift for timestamp calculations. This actually limits the maximum
263ea25da48SPaolo Valente * service allowed in one timestamp delta (small shift values increase it),
264ea25da48SPaolo Valente * the maximum total weight that can be used for the queues in the system
265ea25da48SPaolo Valente * (big shift values increase it), and the period of virtual time
266ea25da48SPaolo Valente * wraparounds.
267ea25da48SPaolo Valente */
268ea25da48SPaolo Valente #define WFQ_SERVICE_SHIFT 22
269ea25da48SPaolo Valente
bfq_entity_to_bfqq(struct bfq_entity * entity)270ea25da48SPaolo Valente struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity)
271ea25da48SPaolo Valente {
272ea25da48SPaolo Valente struct bfq_queue *bfqq = NULL;
273ea25da48SPaolo Valente
274ea25da48SPaolo Valente if (!entity->my_sched_data)
275ea25da48SPaolo Valente bfqq = container_of(entity, struct bfq_queue, entity);
276ea25da48SPaolo Valente
277ea25da48SPaolo Valente return bfqq;
278ea25da48SPaolo Valente }
279ea25da48SPaolo Valente
280ea25da48SPaolo Valente
281ea25da48SPaolo Valente /**
282ea25da48SPaolo Valente * bfq_delta - map service into the virtual time domain.
283ea25da48SPaolo Valente * @service: amount of service.
284ea25da48SPaolo Valente * @weight: scale factor (weight of an entity or weight sum).
285ea25da48SPaolo Valente */
bfq_delta(unsigned long service,unsigned long weight)286ea25da48SPaolo Valente static u64 bfq_delta(unsigned long service, unsigned long weight)
287ea25da48SPaolo Valente {
288554d21efSWen Yang return div64_ul((u64)service << WFQ_SERVICE_SHIFT, weight);
289ea25da48SPaolo Valente }
290ea25da48SPaolo Valente
291ea25da48SPaolo Valente /**
292ea25da48SPaolo Valente * bfq_calc_finish - assign the finish time to an entity.
293ea25da48SPaolo Valente * @entity: the entity to act upon.
294ea25da48SPaolo Valente * @service: the service to be charged to the entity.
295ea25da48SPaolo Valente */
bfq_calc_finish(struct bfq_entity * entity,unsigned long service)296ea25da48SPaolo Valente static void bfq_calc_finish(struct bfq_entity *entity, unsigned long service)
297ea25da48SPaolo Valente {
298ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
299ea25da48SPaolo Valente
300ea25da48SPaolo Valente entity->finish = entity->start +
301ea25da48SPaolo Valente bfq_delta(service, entity->weight);
302ea25da48SPaolo Valente
303ea25da48SPaolo Valente if (bfqq) {
304ea25da48SPaolo Valente bfq_log_bfqq(bfqq->bfqd, bfqq,
305ea25da48SPaolo Valente "calc_finish: serv %lu, w %d",
306ea25da48SPaolo Valente service, entity->weight);
307ea25da48SPaolo Valente bfq_log_bfqq(bfqq->bfqd, bfqq,
308ea25da48SPaolo Valente "calc_finish: start %llu, finish %llu, delta %llu",
309ea25da48SPaolo Valente entity->start, entity->finish,
310ea25da48SPaolo Valente bfq_delta(service, entity->weight));
311ea25da48SPaolo Valente }
312ea25da48SPaolo Valente }
313ea25da48SPaolo Valente
314ea25da48SPaolo Valente /**
315ea25da48SPaolo Valente * bfq_entity_of - get an entity from a node.
316ea25da48SPaolo Valente * @node: the node field of the entity.
317ea25da48SPaolo Valente *
318ea25da48SPaolo Valente * Convert a node pointer to the relative entity. This is used only
319ea25da48SPaolo Valente * to simplify the logic of some functions and not as the generic
320ea25da48SPaolo Valente * conversion mechanism because, e.g., in the tree walking functions,
321ea25da48SPaolo Valente * the check for a %NULL value would be redundant.
322ea25da48SPaolo Valente */
bfq_entity_of(struct rb_node * node)323ea25da48SPaolo Valente struct bfq_entity *bfq_entity_of(struct rb_node *node)
324ea25da48SPaolo Valente {
325ea25da48SPaolo Valente struct bfq_entity *entity = NULL;
326ea25da48SPaolo Valente
327ea25da48SPaolo Valente if (node)
328ea25da48SPaolo Valente entity = rb_entry(node, struct bfq_entity, rb_node);
329ea25da48SPaolo Valente
330ea25da48SPaolo Valente return entity;
331ea25da48SPaolo Valente }
332ea25da48SPaolo Valente
333ea25da48SPaolo Valente /**
334ea25da48SPaolo Valente * bfq_extract - remove an entity from a tree.
335ea25da48SPaolo Valente * @root: the tree root.
336ea25da48SPaolo Valente * @entity: the entity to remove.
337ea25da48SPaolo Valente */
bfq_extract(struct rb_root * root,struct bfq_entity * entity)338ea25da48SPaolo Valente static void bfq_extract(struct rb_root *root, struct bfq_entity *entity)
339ea25da48SPaolo Valente {
340ea25da48SPaolo Valente entity->tree = NULL;
341ea25da48SPaolo Valente rb_erase(&entity->rb_node, root);
342ea25da48SPaolo Valente }
343ea25da48SPaolo Valente
344ea25da48SPaolo Valente /**
345ea25da48SPaolo Valente * bfq_idle_extract - extract an entity from the idle tree.
346ea25da48SPaolo Valente * @st: the service tree of the owning @entity.
347ea25da48SPaolo Valente * @entity: the entity being removed.
348ea25da48SPaolo Valente */
bfq_idle_extract(struct bfq_service_tree * st,struct bfq_entity * entity)349ea25da48SPaolo Valente static void bfq_idle_extract(struct bfq_service_tree *st,
350ea25da48SPaolo Valente struct bfq_entity *entity)
351ea25da48SPaolo Valente {
352ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
353ea25da48SPaolo Valente struct rb_node *next;
354ea25da48SPaolo Valente
355ea25da48SPaolo Valente if (entity == st->first_idle) {
356ea25da48SPaolo Valente next = rb_next(&entity->rb_node);
357ea25da48SPaolo Valente st->first_idle = bfq_entity_of(next);
358ea25da48SPaolo Valente }
359ea25da48SPaolo Valente
360ea25da48SPaolo Valente if (entity == st->last_idle) {
361ea25da48SPaolo Valente next = rb_prev(&entity->rb_node);
362ea25da48SPaolo Valente st->last_idle = bfq_entity_of(next);
363ea25da48SPaolo Valente }
364ea25da48SPaolo Valente
365ea25da48SPaolo Valente bfq_extract(&st->idle, entity);
366ea25da48SPaolo Valente
367ea25da48SPaolo Valente if (bfqq)
368ea25da48SPaolo Valente list_del(&bfqq->bfqq_list);
369ea25da48SPaolo Valente }
370ea25da48SPaolo Valente
371ea25da48SPaolo Valente /**
372ea25da48SPaolo Valente * bfq_insert - generic tree insertion.
373ea25da48SPaolo Valente * @root: tree root.
374ea25da48SPaolo Valente * @entity: entity to insert.
375ea25da48SPaolo Valente *
376ea25da48SPaolo Valente * This is used for the idle and the active tree, since they are both
377ea25da48SPaolo Valente * ordered by finish time.
378ea25da48SPaolo Valente */
bfq_insert(struct rb_root * root,struct bfq_entity * entity)379ea25da48SPaolo Valente static void bfq_insert(struct rb_root *root, struct bfq_entity *entity)
380ea25da48SPaolo Valente {
381ea25da48SPaolo Valente struct bfq_entity *entry;
382ea25da48SPaolo Valente struct rb_node **node = &root->rb_node;
383ea25da48SPaolo Valente struct rb_node *parent = NULL;
384ea25da48SPaolo Valente
385ea25da48SPaolo Valente while (*node) {
386ea25da48SPaolo Valente parent = *node;
387ea25da48SPaolo Valente entry = rb_entry(parent, struct bfq_entity, rb_node);
388ea25da48SPaolo Valente
389ea25da48SPaolo Valente if (bfq_gt(entry->finish, entity->finish))
390ea25da48SPaolo Valente node = &parent->rb_left;
391ea25da48SPaolo Valente else
392ea25da48SPaolo Valente node = &parent->rb_right;
393ea25da48SPaolo Valente }
394ea25da48SPaolo Valente
395ea25da48SPaolo Valente rb_link_node(&entity->rb_node, parent, node);
396ea25da48SPaolo Valente rb_insert_color(&entity->rb_node, root);
397ea25da48SPaolo Valente
398ea25da48SPaolo Valente entity->tree = root;
399ea25da48SPaolo Valente }
400ea25da48SPaolo Valente
401ea25da48SPaolo Valente /**
402ea25da48SPaolo Valente * bfq_update_min - update the min_start field of a entity.
403ea25da48SPaolo Valente * @entity: the entity to update.
404ea25da48SPaolo Valente * @node: one of its children.
405ea25da48SPaolo Valente *
406ea25da48SPaolo Valente * This function is called when @entity may store an invalid value for
407ea25da48SPaolo Valente * min_start due to updates to the active tree. The function assumes
408ea25da48SPaolo Valente * that the subtree rooted at @node (which may be its left or its right
409ea25da48SPaolo Valente * child) has a valid min_start value.
410ea25da48SPaolo Valente */
bfq_update_min(struct bfq_entity * entity,struct rb_node * node)411ea25da48SPaolo Valente static void bfq_update_min(struct bfq_entity *entity, struct rb_node *node)
412ea25da48SPaolo Valente {
413ea25da48SPaolo Valente struct bfq_entity *child;
414ea25da48SPaolo Valente
415ea25da48SPaolo Valente if (node) {
416ea25da48SPaolo Valente child = rb_entry(node, struct bfq_entity, rb_node);
417ea25da48SPaolo Valente if (bfq_gt(entity->min_start, child->min_start))
418ea25da48SPaolo Valente entity->min_start = child->min_start;
419ea25da48SPaolo Valente }
420ea25da48SPaolo Valente }
421ea25da48SPaolo Valente
422ea25da48SPaolo Valente /**
423ea25da48SPaolo Valente * bfq_update_active_node - recalculate min_start.
424ea25da48SPaolo Valente * @node: the node to update.
425ea25da48SPaolo Valente *
426ea25da48SPaolo Valente * @node may have changed position or one of its children may have moved,
427ea25da48SPaolo Valente * this function updates its min_start value. The left and right subtrees
428ea25da48SPaolo Valente * are assumed to hold a correct min_start value.
429ea25da48SPaolo Valente */
bfq_update_active_node(struct rb_node * node)430ea25da48SPaolo Valente static void bfq_update_active_node(struct rb_node *node)
431ea25da48SPaolo Valente {
432ea25da48SPaolo Valente struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node);
433ea25da48SPaolo Valente
434ea25da48SPaolo Valente entity->min_start = entity->start;
435ea25da48SPaolo Valente bfq_update_min(entity, node->rb_right);
436ea25da48SPaolo Valente bfq_update_min(entity, node->rb_left);
437ea25da48SPaolo Valente }
438ea25da48SPaolo Valente
439ea25da48SPaolo Valente /**
440ea25da48SPaolo Valente * bfq_update_active_tree - update min_start for the whole active tree.
441ea25da48SPaolo Valente * @node: the starting node.
442ea25da48SPaolo Valente *
443ea25da48SPaolo Valente * @node must be the deepest modified node after an update. This function
444ea25da48SPaolo Valente * updates its min_start using the values held by its children, assuming
445ea25da48SPaolo Valente * that they did not change, and then updates all the nodes that may have
446ea25da48SPaolo Valente * changed in the path to the root. The only nodes that may have changed
447ea25da48SPaolo Valente * are the ones in the path or their siblings.
448ea25da48SPaolo Valente */
bfq_update_active_tree(struct rb_node * node)449ea25da48SPaolo Valente static void bfq_update_active_tree(struct rb_node *node)
450ea25da48SPaolo Valente {
451ea25da48SPaolo Valente struct rb_node *parent;
452ea25da48SPaolo Valente
453ea25da48SPaolo Valente up:
454ea25da48SPaolo Valente bfq_update_active_node(node);
455ea25da48SPaolo Valente
456ea25da48SPaolo Valente parent = rb_parent(node);
457ea25da48SPaolo Valente if (!parent)
458ea25da48SPaolo Valente return;
459ea25da48SPaolo Valente
460ea25da48SPaolo Valente if (node == parent->rb_left && parent->rb_right)
461ea25da48SPaolo Valente bfq_update_active_node(parent->rb_right);
462ea25da48SPaolo Valente else if (parent->rb_left)
463ea25da48SPaolo Valente bfq_update_active_node(parent->rb_left);
464ea25da48SPaolo Valente
465ea25da48SPaolo Valente node = parent;
466ea25da48SPaolo Valente goto up;
467ea25da48SPaolo Valente }
468ea25da48SPaolo Valente
469ea25da48SPaolo Valente /**
470ea25da48SPaolo Valente * bfq_active_insert - insert an entity in the active tree of its
471ea25da48SPaolo Valente * group/device.
472ea25da48SPaolo Valente * @st: the service tree of the entity.
473ea25da48SPaolo Valente * @entity: the entity being inserted.
474ea25da48SPaolo Valente *
475ea25da48SPaolo Valente * The active tree is ordered by finish time, but an extra key is kept
476ea25da48SPaolo Valente * per each node, containing the minimum value for the start times of
477ea25da48SPaolo Valente * its children (and the node itself), so it's possible to search for
478ea25da48SPaolo Valente * the eligible node with the lowest finish time in logarithmic time.
479ea25da48SPaolo Valente */
bfq_active_insert(struct bfq_service_tree * st,struct bfq_entity * entity)480ea25da48SPaolo Valente static void bfq_active_insert(struct bfq_service_tree *st,
481ea25da48SPaolo Valente struct bfq_entity *entity)
482ea25da48SPaolo Valente {
483ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
484ea25da48SPaolo Valente struct rb_node *node = &entity->rb_node;
485ea25da48SPaolo Valente
486ea25da48SPaolo Valente bfq_insert(&st->active, entity);
487ea25da48SPaolo Valente
488ea25da48SPaolo Valente if (node->rb_left)
489ea25da48SPaolo Valente node = node->rb_left;
490ea25da48SPaolo Valente else if (node->rb_right)
491ea25da48SPaolo Valente node = node->rb_right;
492ea25da48SPaolo Valente
493ea25da48SPaolo Valente bfq_update_active_tree(node);
494ea25da48SPaolo Valente
495ea25da48SPaolo Valente if (bfqq)
496*2d31c684SDavide Zini list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list[bfqq->actuator_idx]);
497e5c63eb4SYu Kuai
498e5c63eb4SYu Kuai bfq_inc_active_entities(entity);
499ea25da48SPaolo Valente }
500ea25da48SPaolo Valente
501ea25da48SPaolo Valente /**
502ea25da48SPaolo Valente * bfq_ioprio_to_weight - calc a weight from an ioprio.
503ea25da48SPaolo Valente * @ioprio: the ioprio value to convert.
504ea25da48SPaolo Valente */
bfq_ioprio_to_weight(int ioprio)505ea25da48SPaolo Valente unsigned short bfq_ioprio_to_weight(int ioprio)
506ea25da48SPaolo Valente {
507202bc942SDamien Le Moal return (IOPRIO_NR_LEVELS - ioprio) * BFQ_WEIGHT_CONVERSION_COEFF;
508ea25da48SPaolo Valente }
509ea25da48SPaolo Valente
510ea25da48SPaolo Valente /**
511ea25da48SPaolo Valente * bfq_weight_to_ioprio - calc an ioprio from a weight.
512ea25da48SPaolo Valente * @weight: the weight value to convert.
513ea25da48SPaolo Valente *
514ea25da48SPaolo Valente * To preserve as much as possible the old only-ioprio user interface,
515ea25da48SPaolo Valente * 0 is used as an escape ioprio value for weights (numerically) equal or
516202bc942SDamien Le Moal * larger than IOPRIO_NR_LEVELS * BFQ_WEIGHT_CONVERSION_COEFF.
517ea25da48SPaolo Valente */
bfq_weight_to_ioprio(int weight)518ea25da48SPaolo Valente static unsigned short bfq_weight_to_ioprio(int weight)
519ea25da48SPaolo Valente {
520ea25da48SPaolo Valente return max_t(int, 0,
521bcd2be76SYahu Gao IOPRIO_NR_LEVELS - weight / BFQ_WEIGHT_CONVERSION_COEFF);
522ea25da48SPaolo Valente }
523ea25da48SPaolo Valente
bfq_get_entity(struct bfq_entity * entity)524ea25da48SPaolo Valente static void bfq_get_entity(struct bfq_entity *entity)
525ea25da48SPaolo Valente {
526ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
527ea25da48SPaolo Valente
528ea25da48SPaolo Valente if (bfqq) {
529ea25da48SPaolo Valente bfqq->ref++;
530ea25da48SPaolo Valente bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
531ea25da48SPaolo Valente bfqq, bfqq->ref);
5322de791abSDmitry Monakhov }
533ea25da48SPaolo Valente }
534ea25da48SPaolo Valente
535ea25da48SPaolo Valente /**
536ea25da48SPaolo Valente * bfq_find_deepest - find the deepest node that an extraction can modify.
537ea25da48SPaolo Valente * @node: the node being removed.
538ea25da48SPaolo Valente *
539ea25da48SPaolo Valente * Do the first step of an extraction in an rb tree, looking for the
540ea25da48SPaolo Valente * node that will replace @node, and returning the deepest node that
541ea25da48SPaolo Valente * the following modifications to the tree can touch. If @node is the
542ea25da48SPaolo Valente * last node in the tree return %NULL.
543ea25da48SPaolo Valente */
bfq_find_deepest(struct rb_node * node)544ea25da48SPaolo Valente static struct rb_node *bfq_find_deepest(struct rb_node *node)
545ea25da48SPaolo Valente {
546ea25da48SPaolo Valente struct rb_node *deepest;
547ea25da48SPaolo Valente
548ea25da48SPaolo Valente if (!node->rb_right && !node->rb_left)
549ea25da48SPaolo Valente deepest = rb_parent(node);
550ea25da48SPaolo Valente else if (!node->rb_right)
551ea25da48SPaolo Valente deepest = node->rb_left;
552ea25da48SPaolo Valente else if (!node->rb_left)
553ea25da48SPaolo Valente deepest = node->rb_right;
554ea25da48SPaolo Valente else {
555ea25da48SPaolo Valente deepest = rb_next(node);
556ea25da48SPaolo Valente if (deepest->rb_right)
557ea25da48SPaolo Valente deepest = deepest->rb_right;
558ea25da48SPaolo Valente else if (rb_parent(deepest) != node)
559ea25da48SPaolo Valente deepest = rb_parent(deepest);
560ea25da48SPaolo Valente }
561ea25da48SPaolo Valente
562ea25da48SPaolo Valente return deepest;
563ea25da48SPaolo Valente }
564ea25da48SPaolo Valente
565ea25da48SPaolo Valente /**
566ea25da48SPaolo Valente * bfq_active_extract - remove an entity from the active tree.
567ea25da48SPaolo Valente * @st: the service_tree containing the tree.
568ea25da48SPaolo Valente * @entity: the entity being removed.
569ea25da48SPaolo Valente */
bfq_active_extract(struct bfq_service_tree * st,struct bfq_entity * entity)570ea25da48SPaolo Valente static void bfq_active_extract(struct bfq_service_tree *st,
571ea25da48SPaolo Valente struct bfq_entity *entity)
572ea25da48SPaolo Valente {
573ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
574ea25da48SPaolo Valente struct rb_node *node;
575ea25da48SPaolo Valente
576ea25da48SPaolo Valente node = bfq_find_deepest(&entity->rb_node);
577ea25da48SPaolo Valente bfq_extract(&st->active, entity);
578ea25da48SPaolo Valente
579ea25da48SPaolo Valente if (node)
580ea25da48SPaolo Valente bfq_update_active_tree(node);
581ea25da48SPaolo Valente if (bfqq)
582ea25da48SPaolo Valente list_del(&bfqq->bfqq_list);
583e5c63eb4SYu Kuai
584e5c63eb4SYu Kuai bfq_dec_active_entities(entity);
585ea25da48SPaolo Valente }
586ea25da48SPaolo Valente
587ea25da48SPaolo Valente /**
588ea25da48SPaolo Valente * bfq_idle_insert - insert an entity into the idle tree.
589ea25da48SPaolo Valente * @st: the service tree containing the tree.
590ea25da48SPaolo Valente * @entity: the entity to insert.
591ea25da48SPaolo Valente */
bfq_idle_insert(struct bfq_service_tree * st,struct bfq_entity * entity)592ea25da48SPaolo Valente static void bfq_idle_insert(struct bfq_service_tree *st,
593ea25da48SPaolo Valente struct bfq_entity *entity)
594ea25da48SPaolo Valente {
595ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
596ea25da48SPaolo Valente struct bfq_entity *first_idle = st->first_idle;
597ea25da48SPaolo Valente struct bfq_entity *last_idle = st->last_idle;
598ea25da48SPaolo Valente
599ea25da48SPaolo Valente if (!first_idle || bfq_gt(first_idle->finish, entity->finish))
600ea25da48SPaolo Valente st->first_idle = entity;
601ea25da48SPaolo Valente if (!last_idle || bfq_gt(entity->finish, last_idle->finish))
602ea25da48SPaolo Valente st->last_idle = entity;
603ea25da48SPaolo Valente
604ea25da48SPaolo Valente bfq_insert(&st->idle, entity);
605ea25da48SPaolo Valente
606ea25da48SPaolo Valente if (bfqq)
607ea25da48SPaolo Valente list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list);
608ea25da48SPaolo Valente }
609ea25da48SPaolo Valente
610ea25da48SPaolo Valente /**
611ea25da48SPaolo Valente * bfq_forget_entity - do not consider entity any longer for scheduling
612ea25da48SPaolo Valente * @st: the service tree.
613ea25da48SPaolo Valente * @entity: the entity being removed.
614ea25da48SPaolo Valente * @is_in_service: true if entity is currently the in-service entity.
615ea25da48SPaolo Valente *
616ea25da48SPaolo Valente * Forget everything about @entity. In addition, if entity represents
617ea25da48SPaolo Valente * a queue, and the latter is not in service, then release the service
618ea25da48SPaolo Valente * reference to the queue (the one taken through bfq_get_entity). In
619ea25da48SPaolo Valente * fact, in this case, there is really no more service reference to
620ea25da48SPaolo Valente * the queue, as the latter is also outside any service tree. If,
621ea25da48SPaolo Valente * instead, the queue is in service, then __bfq_bfqd_reset_in_service
622ea25da48SPaolo Valente * will take care of putting the reference when the queue finally
623ea25da48SPaolo Valente * stops being served.
624ea25da48SPaolo Valente */
bfq_forget_entity(struct bfq_service_tree * st,struct bfq_entity * entity,bool is_in_service)625ea25da48SPaolo Valente static void bfq_forget_entity(struct bfq_service_tree *st,
626ea25da48SPaolo Valente struct bfq_entity *entity,
627ea25da48SPaolo Valente bool is_in_service)
628ea25da48SPaolo Valente {
629ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
630ea25da48SPaolo Valente
63133a16a98SPaolo Valente entity->on_st_or_in_serv = false;
632ea25da48SPaolo Valente st->wsum -= entity->weight;
6332de791abSDmitry Monakhov if (bfqq && !is_in_service)
634ea25da48SPaolo Valente bfq_put_queue(bfqq);
635ea25da48SPaolo Valente }
636ea25da48SPaolo Valente
637ea25da48SPaolo Valente /**
638ea25da48SPaolo Valente * bfq_put_idle_entity - release the idle tree ref of an entity.
639ea25da48SPaolo Valente * @st: service tree for the entity.
640ea25da48SPaolo Valente * @entity: the entity being released.
641ea25da48SPaolo Valente */
bfq_put_idle_entity(struct bfq_service_tree * st,struct bfq_entity * entity)642ea25da48SPaolo Valente void bfq_put_idle_entity(struct bfq_service_tree *st, struct bfq_entity *entity)
643ea25da48SPaolo Valente {
644ea25da48SPaolo Valente bfq_idle_extract(st, entity);
645ea25da48SPaolo Valente bfq_forget_entity(st, entity,
646ea25da48SPaolo Valente entity == entity->sched_data->in_service_entity);
647ea25da48SPaolo Valente }
648ea25da48SPaolo Valente
649ea25da48SPaolo Valente /**
650ea25da48SPaolo Valente * bfq_forget_idle - update the idle tree if necessary.
651ea25da48SPaolo Valente * @st: the service tree to act upon.
652ea25da48SPaolo Valente *
653ea25da48SPaolo Valente * To preserve the global O(log N) complexity we only remove one entry here;
654ea25da48SPaolo Valente * as the idle tree will not grow indefinitely this can be done safely.
655ea25da48SPaolo Valente */
bfq_forget_idle(struct bfq_service_tree * st)656ea25da48SPaolo Valente static void bfq_forget_idle(struct bfq_service_tree *st)
657ea25da48SPaolo Valente {
658ea25da48SPaolo Valente struct bfq_entity *first_idle = st->first_idle;
659ea25da48SPaolo Valente struct bfq_entity *last_idle = st->last_idle;
660ea25da48SPaolo Valente
661ea25da48SPaolo Valente if (RB_EMPTY_ROOT(&st->active) && last_idle &&
662ea25da48SPaolo Valente !bfq_gt(last_idle->finish, st->vtime)) {
663ea25da48SPaolo Valente /*
664ea25da48SPaolo Valente * Forget the whole idle tree, increasing the vtime past
665ea25da48SPaolo Valente * the last finish time of idle entities.
666ea25da48SPaolo Valente */
667ea25da48SPaolo Valente st->vtime = last_idle->finish;
668ea25da48SPaolo Valente }
669ea25da48SPaolo Valente
670ea25da48SPaolo Valente if (first_idle && !bfq_gt(first_idle->finish, st->vtime))
671ea25da48SPaolo Valente bfq_put_idle_entity(st, first_idle);
672ea25da48SPaolo Valente }
673ea25da48SPaolo Valente
bfq_entity_service_tree(struct bfq_entity * entity)674ea25da48SPaolo Valente struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity)
675ea25da48SPaolo Valente {
676ea25da48SPaolo Valente struct bfq_sched_data *sched_data = entity->sched_data;
677ea25da48SPaolo Valente unsigned int idx = bfq_class_idx(entity);
678ea25da48SPaolo Valente
679ea25da48SPaolo Valente return sched_data->service_tree + idx;
680ea25da48SPaolo Valente }
681ea25da48SPaolo Valente
682431b17f9SPaolo Valente /*
683431b17f9SPaolo Valente * Update weight and priority of entity. If update_class_too is true,
684431b17f9SPaolo Valente * then update the ioprio_class of entity too.
685431b17f9SPaolo Valente *
686431b17f9SPaolo Valente * The reason why the update of ioprio_class is controlled through the
687431b17f9SPaolo Valente * last parameter is as follows. Changing the ioprio class of an
688431b17f9SPaolo Valente * entity implies changing the destination service trees for that
689431b17f9SPaolo Valente * entity. If such a change occurred when the entity is already on one
690431b17f9SPaolo Valente * of the service trees for its previous class, then the state of the
691431b17f9SPaolo Valente * entity would become more complex: none of the new possible service
692431b17f9SPaolo Valente * trees for the entity, according to bfq_entity_service_tree(), would
693431b17f9SPaolo Valente * match any of the possible service trees on which the entity
694431b17f9SPaolo Valente * is. Complex operations involving these trees, such as entity
695431b17f9SPaolo Valente * activations and deactivations, should take into account this
696431b17f9SPaolo Valente * additional complexity. To avoid this issue, this function is
697431b17f9SPaolo Valente * invoked with update_class_too unset in the points in the code where
698431b17f9SPaolo Valente * entity may happen to be on some tree.
699431b17f9SPaolo Valente */
700ea25da48SPaolo Valente struct bfq_service_tree *
__bfq_entity_update_weight_prio(struct bfq_service_tree * old_st,struct bfq_entity * entity,bool update_class_too)701ea25da48SPaolo Valente __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
702431b17f9SPaolo Valente struct bfq_entity *entity,
703431b17f9SPaolo Valente bool update_class_too)
704ea25da48SPaolo Valente {
705ea25da48SPaolo Valente struct bfq_service_tree *new_st = old_st;
706ea25da48SPaolo Valente
707ea25da48SPaolo Valente if (entity->prio_changed) {
708ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
709ea25da48SPaolo Valente unsigned int prev_weight, new_weight;
710ea25da48SPaolo Valente
711e9d3c866SFam Zheng /* Matches the smp_wmb() in bfq_group_set_weight. */
712e9d3c866SFam Zheng smp_rmb();
713ea25da48SPaolo Valente old_st->wsum -= entity->weight;
714ea25da48SPaolo Valente
715ea25da48SPaolo Valente if (entity->new_weight != entity->orig_weight) {
716ea25da48SPaolo Valente if (entity->new_weight < BFQ_MIN_WEIGHT ||
717ea25da48SPaolo Valente entity->new_weight > BFQ_MAX_WEIGHT) {
718ea25da48SPaolo Valente pr_crit("update_weight_prio: new_weight %d\n",
719ea25da48SPaolo Valente entity->new_weight);
720ea25da48SPaolo Valente if (entity->new_weight < BFQ_MIN_WEIGHT)
721ea25da48SPaolo Valente entity->new_weight = BFQ_MIN_WEIGHT;
722ea25da48SPaolo Valente else
723ea25da48SPaolo Valente entity->new_weight = BFQ_MAX_WEIGHT;
724ea25da48SPaolo Valente }
725ea25da48SPaolo Valente entity->orig_weight = entity->new_weight;
726ea25da48SPaolo Valente if (bfqq)
727ea25da48SPaolo Valente bfqq->ioprio =
728ea25da48SPaolo Valente bfq_weight_to_ioprio(entity->orig_weight);
729ea25da48SPaolo Valente }
730ea25da48SPaolo Valente
731431b17f9SPaolo Valente if (bfqq && update_class_too)
732ea25da48SPaolo Valente bfqq->ioprio_class = bfqq->new_ioprio_class;
733431b17f9SPaolo Valente
734431b17f9SPaolo Valente /*
735431b17f9SPaolo Valente * Reset prio_changed only if the ioprio_class change
736431b17f9SPaolo Valente * is not pending any longer.
737431b17f9SPaolo Valente */
738431b17f9SPaolo Valente if (!bfqq || bfqq->ioprio_class == bfqq->new_ioprio_class)
739ea25da48SPaolo Valente entity->prio_changed = 0;
740ea25da48SPaolo Valente
741ea25da48SPaolo Valente /*
742ea25da48SPaolo Valente * NOTE: here we may be changing the weight too early,
743ea25da48SPaolo Valente * this will cause unfairness. The correct approach
744ea25da48SPaolo Valente * would have required additional complexity to defer
745ea25da48SPaolo Valente * weight changes to the proper time instants (i.e.,
746ea25da48SPaolo Valente * when entity->finish <= old_st->vtime).
747ea25da48SPaolo Valente */
748ea25da48SPaolo Valente new_st = bfq_entity_service_tree(entity);
749ea25da48SPaolo Valente
750ea25da48SPaolo Valente prev_weight = entity->weight;
751ea25da48SPaolo Valente new_weight = entity->orig_weight *
752ea25da48SPaolo Valente (bfqq ? bfqq->wr_coeff : 1);
753ea25da48SPaolo Valente /*
7542d29c9f8SFederico Motta * If the weight of the entity changes, and the entity is a
7552d29c9f8SFederico Motta * queue, remove the entity from its old weight counter (if
7562d29c9f8SFederico Motta * there is a counter associated with the entity).
757ea25da48SPaolo Valente */
758afdba146SYu Kuai if (prev_weight != new_weight && bfqq)
759eb5bca73SYu Kuai bfq_weights_tree_remove(bfqq);
760ea25da48SPaolo Valente entity->weight = new_weight;
761ea25da48SPaolo Valente /*
7622d29c9f8SFederico Motta * Add the entity, if it is not a weight-raised queue,
7632d29c9f8SFederico Motta * to the counter associated with its new weight.
764ea25da48SPaolo Valente */
765afdba146SYu Kuai if (prev_weight != new_weight && bfqq && bfqq->wr_coeff == 1)
766afdba146SYu Kuai bfq_weights_tree_add(bfqq);
767ea25da48SPaolo Valente
768ea25da48SPaolo Valente new_st->wsum += entity->weight;
769ea25da48SPaolo Valente
770ea25da48SPaolo Valente if (new_st != old_st)
771ea25da48SPaolo Valente entity->start = new_st->vtime;
772ea25da48SPaolo Valente }
773ea25da48SPaolo Valente
774ea25da48SPaolo Valente return new_st;
775ea25da48SPaolo Valente }
776ea25da48SPaolo Valente
777ea25da48SPaolo Valente /**
778ea25da48SPaolo Valente * bfq_bfqq_served - update the scheduler status after selection for
779ea25da48SPaolo Valente * service.
780ea25da48SPaolo Valente * @bfqq: the queue being served.
781ea25da48SPaolo Valente * @served: bytes to transfer.
782ea25da48SPaolo Valente *
783ea25da48SPaolo Valente * NOTE: this can be optimized, as the timestamps of upper level entities
784ea25da48SPaolo Valente * are synchronized every time a new bfqq is selected for service. By now,
785ea25da48SPaolo Valente * we keep it to better check consistency.
786ea25da48SPaolo Valente */
bfq_bfqq_served(struct bfq_queue * bfqq,int served)787ea25da48SPaolo Valente void bfq_bfqq_served(struct bfq_queue *bfqq, int served)
788ea25da48SPaolo Valente {
789ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity;
790ea25da48SPaolo Valente struct bfq_service_tree *st;
791ea25da48SPaolo Valente
7927b8fa3b9SPaolo Valente if (!bfqq->service_from_backlogged)
7937b8fa3b9SPaolo Valente bfqq->first_IO_time = jiffies;
7947b8fa3b9SPaolo Valente
7958a8747dcSPaolo Valente if (bfqq->wr_coeff > 1)
7968a8747dcSPaolo Valente bfqq->service_from_wr += served;
7978a8747dcSPaolo Valente
7987b8fa3b9SPaolo Valente bfqq->service_from_backlogged += served;
799ea25da48SPaolo Valente for_each_entity(entity) {
800ea25da48SPaolo Valente st = bfq_entity_service_tree(entity);
801ea25da48SPaolo Valente
802ea25da48SPaolo Valente entity->service += served;
803ea25da48SPaolo Valente
804ea25da48SPaolo Valente st->vtime += bfq_delta(served, st->wsum);
805ea25da48SPaolo Valente bfq_forget_idle(st);
806ea25da48SPaolo Valente }
807ea25da48SPaolo Valente bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs", served);
808ea25da48SPaolo Valente }
809ea25da48SPaolo Valente
810ea25da48SPaolo Valente /**
811ea25da48SPaolo Valente * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
812ea25da48SPaolo Valente * of the time interval during which bfqq has been in
813ea25da48SPaolo Valente * service.
814ea25da48SPaolo Valente * @bfqd: the device
815ea25da48SPaolo Valente * @bfqq: the queue that needs a service update.
816ea25da48SPaolo Valente * @time_ms: the amount of time during which the queue has received service
817ea25da48SPaolo Valente *
818ea25da48SPaolo Valente * If a queue does not consume its budget fast enough, then providing
819ea25da48SPaolo Valente * the queue with service fairness may impair throughput, more or less
820ea25da48SPaolo Valente * severely. For this reason, queues that consume their budget slowly
821ea25da48SPaolo Valente * are provided with time fairness instead of service fairness. This
822ea25da48SPaolo Valente * goal is achieved through the BFQ scheduling engine, even if such an
823ea25da48SPaolo Valente * engine works in the service, and not in the time domain. The trick
824ea25da48SPaolo Valente * is charging these queues with an inflated amount of service, equal
825ea25da48SPaolo Valente * to the amount of service that they would have received during their
826ea25da48SPaolo Valente * service slot if they had been fast, i.e., if their requests had
827ea25da48SPaolo Valente * been dispatched at a rate equal to the estimated peak rate.
828ea25da48SPaolo Valente *
829ea25da48SPaolo Valente * It is worth noting that time fairness can cause important
830ea25da48SPaolo Valente * distortions in terms of bandwidth distribution, on devices with
831ea25da48SPaolo Valente * internal queueing. The reason is that I/O requests dispatched
832ea25da48SPaolo Valente * during the service slot of a queue may be served after that service
833ea25da48SPaolo Valente * slot is finished, and may have a total processing time loosely
834ea25da48SPaolo Valente * correlated with the duration of the service slot. This is
835ea25da48SPaolo Valente * especially true for short service slots.
836ea25da48SPaolo Valente */
bfq_bfqq_charge_time(struct bfq_data * bfqd,struct bfq_queue * bfqq,unsigned long time_ms)837ea25da48SPaolo Valente void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
838ea25da48SPaolo Valente unsigned long time_ms)
839ea25da48SPaolo Valente {
840ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity;
841f8121648SPaolo Valente unsigned long timeout_ms = jiffies_to_msecs(bfq_timeout);
842f8121648SPaolo Valente unsigned long bounded_time_ms = min(time_ms, timeout_ms);
843f8121648SPaolo Valente int serv_to_charge_for_time =
844f8121648SPaolo Valente (bfqd->bfq_max_budget * bounded_time_ms) / timeout_ms;
845f8121648SPaolo Valente int tot_serv_to_charge = max(serv_to_charge_for_time, entity->service);
846ea25da48SPaolo Valente
847ea25da48SPaolo Valente /* Increase budget to avoid inconsistencies */
848ea25da48SPaolo Valente if (tot_serv_to_charge > entity->budget)
849ea25da48SPaolo Valente entity->budget = tot_serv_to_charge;
850ea25da48SPaolo Valente
851ea25da48SPaolo Valente bfq_bfqq_served(bfqq,
852ea25da48SPaolo Valente max_t(int, 0, tot_serv_to_charge - entity->service));
853ea25da48SPaolo Valente }
854ea25da48SPaolo Valente
bfq_update_fin_time_enqueue(struct bfq_entity * entity,struct bfq_service_tree * st,bool backshifted)855ea25da48SPaolo Valente static void bfq_update_fin_time_enqueue(struct bfq_entity *entity,
856ea25da48SPaolo Valente struct bfq_service_tree *st,
857ea25da48SPaolo Valente bool backshifted)
858ea25da48SPaolo Valente {
859ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
860ea25da48SPaolo Valente
861431b17f9SPaolo Valente /*
862431b17f9SPaolo Valente * When this function is invoked, entity is not in any service
863431b17f9SPaolo Valente * tree, then it is safe to invoke next function with the last
864431b17f9SPaolo Valente * parameter set (see the comments on the function).
865431b17f9SPaolo Valente */
866431b17f9SPaolo Valente st = __bfq_entity_update_weight_prio(st, entity, true);
867ea25da48SPaolo Valente bfq_calc_finish(entity, entity->budget);
868ea25da48SPaolo Valente
869ea25da48SPaolo Valente /*
870ea25da48SPaolo Valente * If some queues enjoy backshifting for a while, then their
871ea25da48SPaolo Valente * (virtual) finish timestamps may happen to become lower and
872ea25da48SPaolo Valente * lower than the system virtual time. In particular, if
873ea25da48SPaolo Valente * these queues often happen to be idle for short time
874ea25da48SPaolo Valente * periods, and during such time periods other queues with
875ea25da48SPaolo Valente * higher timestamps happen to be busy, then the backshifted
876ea25da48SPaolo Valente * timestamps of the former queues can become much lower than
877ea25da48SPaolo Valente * the system virtual time. In fact, to serve the queues with
878ea25da48SPaolo Valente * higher timestamps while the ones with lower timestamps are
879ea25da48SPaolo Valente * idle, the system virtual time may be pushed-up to much
880ea25da48SPaolo Valente * higher values than the finish timestamps of the idle
881ea25da48SPaolo Valente * queues. As a consequence, the finish timestamps of all new
882ea25da48SPaolo Valente * or newly activated queues may end up being much larger than
883ea25da48SPaolo Valente * those of lucky queues with backshifted timestamps. The
884ea25da48SPaolo Valente * latter queues may then monopolize the device for a lot of
885ea25da48SPaolo Valente * time. This would simply break service guarantees.
886ea25da48SPaolo Valente *
887ea25da48SPaolo Valente * To reduce this problem, push up a little bit the
888ea25da48SPaolo Valente * backshifted timestamps of the queue associated with this
889ea25da48SPaolo Valente * entity (only a queue can happen to have the backshifted
890ea25da48SPaolo Valente * flag set): just enough to let the finish timestamp of the
891ea25da48SPaolo Valente * queue be equal to the current value of the system virtual
892ea25da48SPaolo Valente * time. This may introduce a little unfairness among queues
893ea25da48SPaolo Valente * with backshifted timestamps, but it does not break
894ea25da48SPaolo Valente * worst-case fairness guarantees.
895ea25da48SPaolo Valente *
896ea25da48SPaolo Valente * As a special case, if bfqq is weight-raised, push up
897ea25da48SPaolo Valente * timestamps much less, to keep very low the probability that
898ea25da48SPaolo Valente * this push up causes the backshifted finish timestamps of
899ea25da48SPaolo Valente * weight-raised queues to become higher than the backshifted
900ea25da48SPaolo Valente * finish timestamps of non weight-raised queues.
901ea25da48SPaolo Valente */
902ea25da48SPaolo Valente if (backshifted && bfq_gt(st->vtime, entity->finish)) {
903ea25da48SPaolo Valente unsigned long delta = st->vtime - entity->finish;
904ea25da48SPaolo Valente
905ea25da48SPaolo Valente if (bfqq)
906ea25da48SPaolo Valente delta /= bfqq->wr_coeff;
907ea25da48SPaolo Valente
908ea25da48SPaolo Valente entity->start += delta;
909ea25da48SPaolo Valente entity->finish += delta;
910ea25da48SPaolo Valente }
911ea25da48SPaolo Valente
912ea25da48SPaolo Valente bfq_active_insert(st, entity);
913ea25da48SPaolo Valente }
914ea25da48SPaolo Valente
915ea25da48SPaolo Valente /**
916ea25da48SPaolo Valente * __bfq_activate_entity - handle activation of entity.
917ea25da48SPaolo Valente * @entity: the entity being activated.
918ea25da48SPaolo Valente * @non_blocking_wait_rq: true if entity was waiting for a request
919ea25da48SPaolo Valente *
920ea25da48SPaolo Valente * Called for a 'true' activation, i.e., if entity is not active and
921ea25da48SPaolo Valente * one of its children receives a new request.
922ea25da48SPaolo Valente *
923ea25da48SPaolo Valente * Basically, this function updates the timestamps of entity and
9240471559cSPaolo Valente * inserts entity into its active tree, after possibly extracting it
925ea25da48SPaolo Valente * from its idle tree.
926ea25da48SPaolo Valente */
__bfq_activate_entity(struct bfq_entity * entity,bool non_blocking_wait_rq)927ea25da48SPaolo Valente static void __bfq_activate_entity(struct bfq_entity *entity,
928ea25da48SPaolo Valente bool non_blocking_wait_rq)
929ea25da48SPaolo Valente {
930ea25da48SPaolo Valente struct bfq_service_tree *st = bfq_entity_service_tree(entity);
931ea25da48SPaolo Valente bool backshifted = false;
932ea25da48SPaolo Valente unsigned long long min_vstart;
933ea25da48SPaolo Valente
934ea25da48SPaolo Valente /* See comments on bfq_fqq_update_budg_for_activation */
935ea25da48SPaolo Valente if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) {
936ea25da48SPaolo Valente backshifted = true;
937ea25da48SPaolo Valente min_vstart = entity->finish;
938ea25da48SPaolo Valente } else
939ea25da48SPaolo Valente min_vstart = st->vtime;
940ea25da48SPaolo Valente
941ea25da48SPaolo Valente if (entity->tree == &st->idle) {
942ea25da48SPaolo Valente /*
943ea25da48SPaolo Valente * Must be on the idle tree, bfq_idle_extract() will
944ea25da48SPaolo Valente * check for that.
945ea25da48SPaolo Valente */
946ea25da48SPaolo Valente bfq_idle_extract(st, entity);
947ea25da48SPaolo Valente entity->start = bfq_gt(min_vstart, entity->finish) ?
948ea25da48SPaolo Valente min_vstart : entity->finish;
949ea25da48SPaolo Valente } else {
950ea25da48SPaolo Valente /*
951ea25da48SPaolo Valente * The finish time of the entity may be invalid, and
952ea25da48SPaolo Valente * it is in the past for sure, otherwise the queue
953ea25da48SPaolo Valente * would have been on the idle tree.
954ea25da48SPaolo Valente */
955ea25da48SPaolo Valente entity->start = min_vstart;
956ea25da48SPaolo Valente st->wsum += entity->weight;
957ea25da48SPaolo Valente /*
958ea25da48SPaolo Valente * entity is about to be inserted into a service tree,
959ea25da48SPaolo Valente * and then set in service: get a reference to make
960ea25da48SPaolo Valente * sure entity does not disappear until it is no
961ea25da48SPaolo Valente * longer in service or scheduled for service.
962ea25da48SPaolo Valente */
963ea25da48SPaolo Valente bfq_get_entity(entity);
964ea25da48SPaolo Valente
96533a16a98SPaolo Valente entity->on_st_or_in_serv = true;
966ea25da48SPaolo Valente }
967ea25da48SPaolo Valente
968ea25da48SPaolo Valente bfq_update_fin_time_enqueue(entity, st, backshifted);
969ea25da48SPaolo Valente }
970ea25da48SPaolo Valente
971ea25da48SPaolo Valente /**
972ea25da48SPaolo Valente * __bfq_requeue_entity - handle requeueing or repositioning of an entity.
973ea25da48SPaolo Valente * @entity: the entity being requeued or repositioned.
974ea25da48SPaolo Valente *
975ea25da48SPaolo Valente * Requeueing is needed if this entity stops being served, which
976ea25da48SPaolo Valente * happens if a leaf descendant entity has expired. On the other hand,
977ea25da48SPaolo Valente * repositioning is needed if the next_inservice_entity for the child
978ea25da48SPaolo Valente * entity has changed. See the comments inside the function for
979ea25da48SPaolo Valente * details.
980ea25da48SPaolo Valente *
981ea25da48SPaolo Valente * Basically, this function: 1) removes entity from its active tree if
982ea25da48SPaolo Valente * present there, 2) updates the timestamps of entity and 3) inserts
983ea25da48SPaolo Valente * entity back into its active tree (in the new, right position for
984ea25da48SPaolo Valente * the new values of the timestamps).
985ea25da48SPaolo Valente */
__bfq_requeue_entity(struct bfq_entity * entity)986ea25da48SPaolo Valente static void __bfq_requeue_entity(struct bfq_entity *entity)
987ea25da48SPaolo Valente {
988ea25da48SPaolo Valente struct bfq_sched_data *sd = entity->sched_data;
989ea25da48SPaolo Valente struct bfq_service_tree *st = bfq_entity_service_tree(entity);
990ea25da48SPaolo Valente
991ea25da48SPaolo Valente if (entity == sd->in_service_entity) {
992ea25da48SPaolo Valente /*
993ea25da48SPaolo Valente * We are requeueing the current in-service entity,
994ea25da48SPaolo Valente * which may have to be done for one of the following
995ea25da48SPaolo Valente * reasons:
996ea25da48SPaolo Valente * - entity represents the in-service queue, and the
997ea25da48SPaolo Valente * in-service queue is being requeued after an
998ea25da48SPaolo Valente * expiration;
999ea25da48SPaolo Valente * - entity represents a group, and its budget has
1000ea25da48SPaolo Valente * changed because one of its child entities has
1001ea25da48SPaolo Valente * just been either activated or requeued for some
1002ea25da48SPaolo Valente * reason; the timestamps of the entity need then to
1003ea25da48SPaolo Valente * be updated, and the entity needs to be enqueued
1004ea25da48SPaolo Valente * or repositioned accordingly.
1005ea25da48SPaolo Valente *
1006ea25da48SPaolo Valente * In particular, before requeueing, the start time of
1007ea25da48SPaolo Valente * the entity must be moved forward to account for the
1008ea25da48SPaolo Valente * service that the entity has received while in
1009ea25da48SPaolo Valente * service. This is done by the next instructions. The
1010ea25da48SPaolo Valente * finish time will then be updated according to this
1011ea25da48SPaolo Valente * new value of the start time, and to the budget of
1012ea25da48SPaolo Valente * the entity.
1013ea25da48SPaolo Valente */
1014ea25da48SPaolo Valente bfq_calc_finish(entity, entity->service);
1015ea25da48SPaolo Valente entity->start = entity->finish;
1016ea25da48SPaolo Valente /*
1017ea25da48SPaolo Valente * In addition, if the entity had more than one child
101846d556e6SPaolo Valente * when set in service, then it was not extracted from
1019ea25da48SPaolo Valente * the active tree. This implies that the position of
1020ea25da48SPaolo Valente * the entity in the active tree may need to be
1021ea25da48SPaolo Valente * changed now, because we have just updated the start
1022ea25da48SPaolo Valente * time of the entity, and we will update its finish
1023ea25da48SPaolo Valente * time in a moment (the requeueing is then, more
1024ea25da48SPaolo Valente * precisely, a repositioning in this case). To
1025ea25da48SPaolo Valente * implement this repositioning, we: 1) dequeue the
102646d556e6SPaolo Valente * entity here, 2) update the finish time and requeue
102746d556e6SPaolo Valente * the entity according to the new timestamps below.
1028ea25da48SPaolo Valente */
1029ea25da48SPaolo Valente if (entity->tree)
1030ea25da48SPaolo Valente bfq_active_extract(st, entity);
1031ea25da48SPaolo Valente } else { /* The entity is already active, and not in service */
1032ea25da48SPaolo Valente /*
1033ea25da48SPaolo Valente * In this case, this function gets called only if the
1034ea25da48SPaolo Valente * next_in_service entity below this entity has
1035ea25da48SPaolo Valente * changed, and this change has caused the budget of
1036ea25da48SPaolo Valente * this entity to change, which, finally implies that
1037ea25da48SPaolo Valente * the finish time of this entity must be
1038ea25da48SPaolo Valente * updated. Such an update may cause the scheduling,
1039ea25da48SPaolo Valente * i.e., the position in the active tree, of this
1040ea25da48SPaolo Valente * entity to change. We handle this change by: 1)
1041ea25da48SPaolo Valente * dequeueing the entity here, 2) updating the finish
1042ea25da48SPaolo Valente * time and requeueing the entity according to the new
1043ea25da48SPaolo Valente * timestamps below. This is the same approach as the
1044ea25da48SPaolo Valente * non-extracted-entity sub-case above.
1045ea25da48SPaolo Valente */
1046ea25da48SPaolo Valente bfq_active_extract(st, entity);
1047ea25da48SPaolo Valente }
1048ea25da48SPaolo Valente
1049ea25da48SPaolo Valente bfq_update_fin_time_enqueue(entity, st, false);
1050ea25da48SPaolo Valente }
1051ea25da48SPaolo Valente
__bfq_activate_requeue_entity(struct bfq_entity * entity,bool non_blocking_wait_rq)1052ea25da48SPaolo Valente static void __bfq_activate_requeue_entity(struct bfq_entity *entity,
1053ea25da48SPaolo Valente bool non_blocking_wait_rq)
1054ea25da48SPaolo Valente {
1055ea25da48SPaolo Valente struct bfq_service_tree *st = bfq_entity_service_tree(entity);
1056ea25da48SPaolo Valente
1057f6fd119bSYu Kuai if (entity->sched_data->in_service_entity == entity ||
1058f6fd119bSYu Kuai entity->tree == &st->active)
1059ea25da48SPaolo Valente /*
1060ea25da48SPaolo Valente * in service or already queued on the active tree,
1061ea25da48SPaolo Valente * requeue or reposition
1062ea25da48SPaolo Valente */
1063ea25da48SPaolo Valente __bfq_requeue_entity(entity);
1064ea25da48SPaolo Valente else
1065ea25da48SPaolo Valente /*
1066ea25da48SPaolo Valente * Not in service and not queued on its active tree:
1067ea25da48SPaolo Valente * the activity is idle and this is a true activation.
1068ea25da48SPaolo Valente */
1069ea25da48SPaolo Valente __bfq_activate_entity(entity, non_blocking_wait_rq);
1070ea25da48SPaolo Valente }
1071ea25da48SPaolo Valente
1072ea25da48SPaolo Valente
1073ea25da48SPaolo Valente /**
107446d556e6SPaolo Valente * bfq_activate_requeue_entity - activate or requeue an entity representing a
107546d556e6SPaolo Valente * bfq_queue, and activate, requeue or reposition
107646d556e6SPaolo Valente * all ancestors for which such an update becomes
107746d556e6SPaolo Valente * necessary.
1078ea25da48SPaolo Valente * @entity: the entity to activate.
1079ea25da48SPaolo Valente * @non_blocking_wait_rq: true if this entity was waiting for a request
1080ea25da48SPaolo Valente * @requeue: true if this is a requeue, which implies that bfqq is
1081ea25da48SPaolo Valente * being expired; thus ALL its ancestors stop being served and must
1082ea25da48SPaolo Valente * therefore be requeued
108380294c3bSPaolo Valente * @expiration: true if this function is being invoked in the expiration path
108480294c3bSPaolo Valente * of the in-service queue
1085ea25da48SPaolo Valente */
bfq_activate_requeue_entity(struct bfq_entity * entity,bool non_blocking_wait_rq,bool requeue,bool expiration)1086ea25da48SPaolo Valente static void bfq_activate_requeue_entity(struct bfq_entity *entity,
1087ea25da48SPaolo Valente bool non_blocking_wait_rq,
108880294c3bSPaolo Valente bool requeue, bool expiration)
1089ea25da48SPaolo Valente {
1090ea25da48SPaolo Valente for_each_entity(entity) {
1091f6fd119bSYu Kuai __bfq_activate_requeue_entity(entity, non_blocking_wait_rq);
1092f6fd119bSYu Kuai if (!bfq_update_next_in_service(entity->sched_data, entity,
1093f6fd119bSYu Kuai expiration) && !requeue)
1094ea25da48SPaolo Valente break;
1095ea25da48SPaolo Valente }
1096ea25da48SPaolo Valente }
1097ea25da48SPaolo Valente
1098ea25da48SPaolo Valente /**
10995bf85908SPaolo Valente * __bfq_deactivate_entity - update sched_data and service trees for
11005bf85908SPaolo Valente * entity, so as to represent entity as inactive
11015bf85908SPaolo Valente * @entity: the entity being deactivated.
1102ea25da48SPaolo Valente * @ins_into_idle_tree: if false, the entity will not be put into the
1103ea25da48SPaolo Valente * idle tree.
1104ea25da48SPaolo Valente *
11055bf85908SPaolo Valente * If necessary and allowed, puts entity into the idle tree. NOTE:
11065bf85908SPaolo Valente * entity may be on no tree if in service.
1107ea25da48SPaolo Valente */
__bfq_deactivate_entity(struct bfq_entity * entity,bool ins_into_idle_tree)1108ea25da48SPaolo Valente bool __bfq_deactivate_entity(struct bfq_entity *entity, bool ins_into_idle_tree)
1109ea25da48SPaolo Valente {
1110ea25da48SPaolo Valente struct bfq_sched_data *sd = entity->sched_data;
1111a66c38a1SPaolo Valente struct bfq_service_tree *st;
1112a66c38a1SPaolo Valente bool is_in_service;
1113ea25da48SPaolo Valente
111433a16a98SPaolo Valente if (!entity->on_st_or_in_serv) /*
111533a16a98SPaolo Valente * entity never activated, or
111633a16a98SPaolo Valente * already inactive
111733a16a98SPaolo Valente */
1118ea25da48SPaolo Valente return false;
1119ea25da48SPaolo Valente
1120a66c38a1SPaolo Valente /*
1121a66c38a1SPaolo Valente * If we get here, then entity is active, which implies that
1122a66c38a1SPaolo Valente * bfq_group_set_parent has already been invoked for the group
1123a66c38a1SPaolo Valente * represented by entity. Therefore, the field
1124a66c38a1SPaolo Valente * entity->sched_data has been set, and we can safely use it.
1125a66c38a1SPaolo Valente */
1126a66c38a1SPaolo Valente st = bfq_entity_service_tree(entity);
1127a66c38a1SPaolo Valente is_in_service = entity == sd->in_service_entity;
1128a66c38a1SPaolo Valente
1129ea25da48SPaolo Valente bfq_calc_finish(entity, entity->service);
1130cbeb869aSPaolo Valente
1131cbeb869aSPaolo Valente if (is_in_service)
11326ab1d8daSPaolo Valente sd->in_service_entity = NULL;
1133cbeb869aSPaolo Valente else
1134cbeb869aSPaolo Valente /*
1135cbeb869aSPaolo Valente * Non in-service entity: nobody will take care of
1136cbeb869aSPaolo Valente * resetting its service counter on expiration. Do it
1137cbeb869aSPaolo Valente * now.
1138cbeb869aSPaolo Valente */
1139cbeb869aSPaolo Valente entity->service = 0;
1140ea25da48SPaolo Valente
1141ea25da48SPaolo Valente if (entity->tree == &st->active)
1142ea25da48SPaolo Valente bfq_active_extract(st, entity);
1143ea25da48SPaolo Valente else if (!is_in_service && entity->tree == &st->idle)
1144ea25da48SPaolo Valente bfq_idle_extract(st, entity);
1145ea25da48SPaolo Valente
1146ea25da48SPaolo Valente if (!ins_into_idle_tree || !bfq_gt(entity->finish, st->vtime))
1147ea25da48SPaolo Valente bfq_forget_entity(st, entity, is_in_service);
1148ea25da48SPaolo Valente else
1149ea25da48SPaolo Valente bfq_idle_insert(st, entity);
1150ea25da48SPaolo Valente
1151ea25da48SPaolo Valente return true;
1152ea25da48SPaolo Valente }
1153ea25da48SPaolo Valente
1154ea25da48SPaolo Valente /**
1155ea25da48SPaolo Valente * bfq_deactivate_entity - deactivate an entity representing a bfq_queue.
1156ea25da48SPaolo Valente * @entity: the entity to deactivate.
115746d556e6SPaolo Valente * @ins_into_idle_tree: true if the entity can be put into the idle tree
115880294c3bSPaolo Valente * @expiration: true if this function is being invoked in the expiration path
115980294c3bSPaolo Valente * of the in-service queue
1160ea25da48SPaolo Valente */
bfq_deactivate_entity(struct bfq_entity * entity,bool ins_into_idle_tree,bool expiration)1161ea25da48SPaolo Valente static void bfq_deactivate_entity(struct bfq_entity *entity,
1162ea25da48SPaolo Valente bool ins_into_idle_tree,
1163ea25da48SPaolo Valente bool expiration)
1164ea25da48SPaolo Valente {
1165ea25da48SPaolo Valente struct bfq_sched_data *sd;
1166ea25da48SPaolo Valente struct bfq_entity *parent = NULL;
1167ea25da48SPaolo Valente
1168ea25da48SPaolo Valente for_each_entity_safe(entity, parent) {
1169ea25da48SPaolo Valente sd = entity->sched_data;
1170ea25da48SPaolo Valente
1171ea25da48SPaolo Valente if (!__bfq_deactivate_entity(entity, ins_into_idle_tree)) {
1172ea25da48SPaolo Valente /*
1173ea25da48SPaolo Valente * entity is not in any tree any more, so
1174ea25da48SPaolo Valente * this deactivation is a no-op, and there is
1175ea25da48SPaolo Valente * nothing to change for upper-level entities
1176ea25da48SPaolo Valente * (in case of expiration, this can never
1177ea25da48SPaolo Valente * happen).
1178ea25da48SPaolo Valente */
1179ea25da48SPaolo Valente return;
1180ea25da48SPaolo Valente }
1181ea25da48SPaolo Valente
1182ea25da48SPaolo Valente if (sd->next_in_service == entity)
1183ea25da48SPaolo Valente /*
1184ea25da48SPaolo Valente * entity was the next_in_service entity,
1185ea25da48SPaolo Valente * then, since entity has just been
1186ea25da48SPaolo Valente * deactivated, a new one must be found.
1187ea25da48SPaolo Valente */
118880294c3bSPaolo Valente bfq_update_next_in_service(sd, NULL, expiration);
1189ea25da48SPaolo Valente
119046d556e6SPaolo Valente if (sd->next_in_service || sd->in_service_entity) {
1191ea25da48SPaolo Valente /*
119246d556e6SPaolo Valente * The parent entity is still active, because
119346d556e6SPaolo Valente * either next_in_service or in_service_entity
119446d556e6SPaolo Valente * is not NULL. So, no further upwards
119546d556e6SPaolo Valente * deactivation must be performed. Yet,
119646d556e6SPaolo Valente * next_in_service has changed. Then the
119746d556e6SPaolo Valente * schedule does need to be updated upwards.
119846d556e6SPaolo Valente *
119946d556e6SPaolo Valente * NOTE If in_service_entity is not NULL, then
120046d556e6SPaolo Valente * next_in_service may happen to be NULL,
120146d556e6SPaolo Valente * although the parent entity is evidently
120246d556e6SPaolo Valente * active. This happens if 1) the entity
120346d556e6SPaolo Valente * pointed by in_service_entity is the only
120446d556e6SPaolo Valente * active entity in the parent entity, and 2)
120546d556e6SPaolo Valente * according to the definition of
120646d556e6SPaolo Valente * next_in_service, the in_service_entity
120746d556e6SPaolo Valente * cannot be considered as
120846d556e6SPaolo Valente * next_in_service. See the comments on the
120946d556e6SPaolo Valente * definition of next_in_service for details.
1210ea25da48SPaolo Valente */
1211ea25da48SPaolo Valente break;
121246d556e6SPaolo Valente }
1213ea25da48SPaolo Valente
1214ea25da48SPaolo Valente /*
1215ea25da48SPaolo Valente * If we get here, then the parent is no more
1216ea25da48SPaolo Valente * backlogged and we need to propagate the
1217ea25da48SPaolo Valente * deactivation upwards. Thus let the loop go on.
1218ea25da48SPaolo Valente */
1219ea25da48SPaolo Valente
1220ea25da48SPaolo Valente /*
1221ea25da48SPaolo Valente * Also let parent be queued into the idle tree on
1222ea25da48SPaolo Valente * deactivation, to preserve service guarantees, and
1223ea25da48SPaolo Valente * assuming that who invoked this function does not
1224ea25da48SPaolo Valente * need parent entities too to be removed completely.
1225ea25da48SPaolo Valente */
1226ea25da48SPaolo Valente ins_into_idle_tree = true;
1227ea25da48SPaolo Valente }
1228ea25da48SPaolo Valente
1229ea25da48SPaolo Valente /*
1230ea25da48SPaolo Valente * If the deactivation loop is fully executed, then there are
1231ea25da48SPaolo Valente * no more entities to touch and next loop is not executed at
1232ea25da48SPaolo Valente * all. Otherwise, requeue remaining entities if they are
1233ea25da48SPaolo Valente * about to stop receiving service, or reposition them if this
1234ea25da48SPaolo Valente * is not the case.
1235ea25da48SPaolo Valente */
1236ea25da48SPaolo Valente entity = parent;
1237ea25da48SPaolo Valente for_each_entity(entity) {
1238ea25da48SPaolo Valente /*
1239ea25da48SPaolo Valente * Invoke __bfq_requeue_entity on entity, even if
1240ea25da48SPaolo Valente * already active, to requeue/reposition it in the
1241ea25da48SPaolo Valente * active tree (because sd->next_in_service has
1242ea25da48SPaolo Valente * changed)
1243ea25da48SPaolo Valente */
1244ea25da48SPaolo Valente __bfq_requeue_entity(entity);
1245ea25da48SPaolo Valente
1246ea25da48SPaolo Valente sd = entity->sched_data;
124780294c3bSPaolo Valente if (!bfq_update_next_in_service(sd, entity, expiration) &&
1248ea25da48SPaolo Valente !expiration)
1249ea25da48SPaolo Valente /*
1250ea25da48SPaolo Valente * next_in_service unchanged or not causing
1251ea25da48SPaolo Valente * any change in entity->parent->sd, and no
1252ea25da48SPaolo Valente * requeueing needed for expiration: stop
1253ea25da48SPaolo Valente * here.
1254ea25da48SPaolo Valente */
1255ea25da48SPaolo Valente break;
1256ea25da48SPaolo Valente }
1257ea25da48SPaolo Valente }
1258ea25da48SPaolo Valente
1259ea25da48SPaolo Valente /**
1260ea25da48SPaolo Valente * bfq_calc_vtime_jump - compute the value to which the vtime should jump,
1261ea25da48SPaolo Valente * if needed, to have at least one entity eligible.
1262ea25da48SPaolo Valente * @st: the service tree to act upon.
1263ea25da48SPaolo Valente *
1264ea25da48SPaolo Valente * Assumes that st is not empty.
1265ea25da48SPaolo Valente */
bfq_calc_vtime_jump(struct bfq_service_tree * st)1266ea25da48SPaolo Valente static u64 bfq_calc_vtime_jump(struct bfq_service_tree *st)
1267ea25da48SPaolo Valente {
1268ea25da48SPaolo Valente struct bfq_entity *root_entity = bfq_root_active_entity(&st->active);
1269ea25da48SPaolo Valente
1270ea25da48SPaolo Valente if (bfq_gt(root_entity->min_start, st->vtime))
1271ea25da48SPaolo Valente return root_entity->min_start;
1272ea25da48SPaolo Valente
1273ea25da48SPaolo Valente return st->vtime;
1274ea25da48SPaolo Valente }
1275ea25da48SPaolo Valente
bfq_update_vtime(struct bfq_service_tree * st,u64 new_value)1276ea25da48SPaolo Valente static void bfq_update_vtime(struct bfq_service_tree *st, u64 new_value)
1277ea25da48SPaolo Valente {
1278ea25da48SPaolo Valente if (new_value > st->vtime) {
1279ea25da48SPaolo Valente st->vtime = new_value;
1280ea25da48SPaolo Valente bfq_forget_idle(st);
1281ea25da48SPaolo Valente }
1282ea25da48SPaolo Valente }
1283ea25da48SPaolo Valente
1284ea25da48SPaolo Valente /**
1285ea25da48SPaolo Valente * bfq_first_active_entity - find the eligible entity with
1286ea25da48SPaolo Valente * the smallest finish time
1287ea25da48SPaolo Valente * @st: the service tree to select from.
1288ea25da48SPaolo Valente * @vtime: the system virtual to use as a reference for eligibility
1289ea25da48SPaolo Valente *
1290ea25da48SPaolo Valente * This function searches the first schedulable entity, starting from the
1291ea25da48SPaolo Valente * root of the tree and going on the left every time on this side there is
129238c91407SHou Tao * a subtree with at least one eligible (start <= vtime) entity. The path on
1293ea25da48SPaolo Valente * the right is followed only if a) the left subtree contains no eligible
1294ea25da48SPaolo Valente * entities and b) no eligible entity has been found yet.
1295ea25da48SPaolo Valente */
bfq_first_active_entity(struct bfq_service_tree * st,u64 vtime)1296ea25da48SPaolo Valente static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st,
1297ea25da48SPaolo Valente u64 vtime)
1298ea25da48SPaolo Valente {
1299ea25da48SPaolo Valente struct bfq_entity *entry, *first = NULL;
1300ea25da48SPaolo Valente struct rb_node *node = st->active.rb_node;
1301ea25da48SPaolo Valente
1302ea25da48SPaolo Valente while (node) {
1303ea25da48SPaolo Valente entry = rb_entry(node, struct bfq_entity, rb_node);
1304ea25da48SPaolo Valente left:
1305ea25da48SPaolo Valente if (!bfq_gt(entry->start, vtime))
1306ea25da48SPaolo Valente first = entry;
1307ea25da48SPaolo Valente
1308ea25da48SPaolo Valente if (node->rb_left) {
1309ea25da48SPaolo Valente entry = rb_entry(node->rb_left,
1310ea25da48SPaolo Valente struct bfq_entity, rb_node);
1311ea25da48SPaolo Valente if (!bfq_gt(entry->min_start, vtime)) {
1312ea25da48SPaolo Valente node = node->rb_left;
1313ea25da48SPaolo Valente goto left;
1314ea25da48SPaolo Valente }
1315ea25da48SPaolo Valente }
1316ea25da48SPaolo Valente if (first)
1317ea25da48SPaolo Valente break;
1318ea25da48SPaolo Valente node = node->rb_right;
1319ea25da48SPaolo Valente }
1320ea25da48SPaolo Valente
1321ea25da48SPaolo Valente return first;
1322ea25da48SPaolo Valente }
1323ea25da48SPaolo Valente
1324ea25da48SPaolo Valente /**
1325ea25da48SPaolo Valente * __bfq_lookup_next_entity - return the first eligible entity in @st.
1326ea25da48SPaolo Valente * @st: the service tree.
13271d87be82SBart Van Assche * @in_service: whether or not there is an in-service entity for the sched_data
13281d87be82SBart Van Assche * this active tree belongs to.
1329ea25da48SPaolo Valente *
1330ea25da48SPaolo Valente * If there is no in-service entity for the sched_data st belongs to,
1331ea25da48SPaolo Valente * then return the entity that will be set in service if:
1332ea25da48SPaolo Valente * 1) the parent entity this st belongs to is set in service;
1333ea25da48SPaolo Valente * 2) no entity belonging to such parent entity undergoes a state change
1334ea25da48SPaolo Valente * that would influence the timestamps of the entity (e.g., becomes idle,
1335ea25da48SPaolo Valente * becomes backlogged, changes its budget, ...).
1336ea25da48SPaolo Valente *
1337ea25da48SPaolo Valente * In this first case, update the virtual time in @st too (see the
1338ea25da48SPaolo Valente * comments on this update inside the function).
1339ea25da48SPaolo Valente *
1340636b8fe8SAngelo Ruocco * In contrast, if there is an in-service entity, then return the
1341ea25da48SPaolo Valente * entity that would be set in service if not only the above
1342ea25da48SPaolo Valente * conditions, but also the next one held true: the currently
1343ea25da48SPaolo Valente * in-service entity, on expiration,
1344ea25da48SPaolo Valente * 1) gets a finish time equal to the current one, or
1345ea25da48SPaolo Valente * 2) is not eligible any more, or
1346ea25da48SPaolo Valente * 3) is idle.
1347ea25da48SPaolo Valente */
1348ea25da48SPaolo Valente static struct bfq_entity *
__bfq_lookup_next_entity(struct bfq_service_tree * st,bool in_service)1349ea25da48SPaolo Valente __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service)
1350ea25da48SPaolo Valente {
1351ea25da48SPaolo Valente struct bfq_entity *entity;
1352ea25da48SPaolo Valente u64 new_vtime;
1353ea25da48SPaolo Valente
1354ea25da48SPaolo Valente if (RB_EMPTY_ROOT(&st->active))
1355ea25da48SPaolo Valente return NULL;
1356ea25da48SPaolo Valente
1357ea25da48SPaolo Valente /*
1358ea25da48SPaolo Valente * Get the value of the system virtual time for which at
1359ea25da48SPaolo Valente * least one entity is eligible.
1360ea25da48SPaolo Valente */
1361ea25da48SPaolo Valente new_vtime = bfq_calc_vtime_jump(st);
1362ea25da48SPaolo Valente
1363ea25da48SPaolo Valente /*
1364ea25da48SPaolo Valente * If there is no in-service entity for the sched_data this
1365ea25da48SPaolo Valente * active tree belongs to, then push the system virtual time
1366ea25da48SPaolo Valente * up to the value that guarantees that at least one entity is
1367ea25da48SPaolo Valente * eligible. If, instead, there is an in-service entity, then
1368ea25da48SPaolo Valente * do not make any such update, because there is already an
1369ea25da48SPaolo Valente * eligible entity, namely the in-service one (even if the
1370ea25da48SPaolo Valente * entity is not on st, because it was extracted when set in
1371ea25da48SPaolo Valente * service).
1372ea25da48SPaolo Valente */
1373ea25da48SPaolo Valente if (!in_service)
1374ea25da48SPaolo Valente bfq_update_vtime(st, new_vtime);
1375ea25da48SPaolo Valente
1376ea25da48SPaolo Valente entity = bfq_first_active_entity(st, new_vtime);
1377ea25da48SPaolo Valente
1378ea25da48SPaolo Valente return entity;
1379ea25da48SPaolo Valente }
1380ea25da48SPaolo Valente
1381ea25da48SPaolo Valente /**
1382ea25da48SPaolo Valente * bfq_lookup_next_entity - return the first eligible entity in @sd.
1383ea25da48SPaolo Valente * @sd: the sched_data.
138480294c3bSPaolo Valente * @expiration: true if we are on the expiration path of the in-service queue
1385ea25da48SPaolo Valente *
1386ea25da48SPaolo Valente * This function is invoked when there has been a change in the trees
138780294c3bSPaolo Valente * for sd, and we need to know what is the new next entity to serve
138880294c3bSPaolo Valente * after this change.
1389ea25da48SPaolo Valente */
bfq_lookup_next_entity(struct bfq_sched_data * sd,bool expiration)139080294c3bSPaolo Valente static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
139180294c3bSPaolo Valente bool expiration)
1392ea25da48SPaolo Valente {
1393ea25da48SPaolo Valente struct bfq_service_tree *st = sd->service_tree;
1394ea25da48SPaolo Valente struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1);
1395ea25da48SPaolo Valente struct bfq_entity *entity = NULL;
1396ea25da48SPaolo Valente int class_idx = 0;
1397ea25da48SPaolo Valente
1398ea25da48SPaolo Valente /*
1399ea25da48SPaolo Valente * Choose from idle class, if needed to guarantee a minimum
1400ea25da48SPaolo Valente * bandwidth to this class (and if there is some active entity
1401ea25da48SPaolo Valente * in idle class). This should also mitigate
1402ea25da48SPaolo Valente * priority-inversion problems in case a low priority task is
1403ea25da48SPaolo Valente * holding file system resources.
1404ea25da48SPaolo Valente */
1405ea25da48SPaolo Valente if (time_is_before_jiffies(sd->bfq_class_idle_last_service +
1406ea25da48SPaolo Valente BFQ_CL_IDLE_TIMEOUT)) {
1407ea25da48SPaolo Valente if (!RB_EMPTY_ROOT(&idle_class_st->active))
1408ea25da48SPaolo Valente class_idx = BFQ_IOPRIO_CLASSES - 1;
1409ea25da48SPaolo Valente /* About to be served if backlogged, or not yet backlogged */
1410ea25da48SPaolo Valente sd->bfq_class_idle_last_service = jiffies;
1411ea25da48SPaolo Valente }
1412ea25da48SPaolo Valente
1413ea25da48SPaolo Valente /*
1414ea25da48SPaolo Valente * Find the next entity to serve for the highest-priority
1415ea25da48SPaolo Valente * class, unless the idle class needs to be served.
1416ea25da48SPaolo Valente */
1417ea25da48SPaolo Valente for (; class_idx < BFQ_IOPRIO_CLASSES; class_idx++) {
141880294c3bSPaolo Valente /*
141980294c3bSPaolo Valente * If expiration is true, then bfq_lookup_next_entity
142080294c3bSPaolo Valente * is being invoked as a part of the expiration path
142180294c3bSPaolo Valente * of the in-service queue. In this case, even if
142280294c3bSPaolo Valente * sd->in_service_entity is not NULL,
1423636b8fe8SAngelo Ruocco * sd->in_service_entity at this point is actually not
142480294c3bSPaolo Valente * in service any more, and, if needed, has already
142580294c3bSPaolo Valente * been properly queued or requeued into the right
142680294c3bSPaolo Valente * tree. The reason why sd->in_service_entity is still
142780294c3bSPaolo Valente * not NULL here, even if expiration is true, is that
1428636b8fe8SAngelo Ruocco * sd->in_service_entity is reset as a last step in the
142980294c3bSPaolo Valente * expiration path. So, if expiration is true, tell
143080294c3bSPaolo Valente * __bfq_lookup_next_entity that there is no
143180294c3bSPaolo Valente * sd->in_service_entity.
143280294c3bSPaolo Valente */
1433ea25da48SPaolo Valente entity = __bfq_lookup_next_entity(st + class_idx,
143480294c3bSPaolo Valente sd->in_service_entity &&
143580294c3bSPaolo Valente !expiration);
1436ea25da48SPaolo Valente
1437ea25da48SPaolo Valente if (entity)
1438ea25da48SPaolo Valente break;
1439ea25da48SPaolo Valente }
1440ea25da48SPaolo Valente
1441ea25da48SPaolo Valente return entity;
1442ea25da48SPaolo Valente }
1443ea25da48SPaolo Valente
next_queue_may_preempt(struct bfq_data * bfqd)1444ea25da48SPaolo Valente bool next_queue_may_preempt(struct bfq_data *bfqd)
1445ea25da48SPaolo Valente {
1446ea25da48SPaolo Valente struct bfq_sched_data *sd = &bfqd->root_group->sched_data;
1447ea25da48SPaolo Valente
1448ea25da48SPaolo Valente return sd->next_in_service != sd->in_service_entity;
1449ea25da48SPaolo Valente }
1450ea25da48SPaolo Valente
1451ea25da48SPaolo Valente /*
1452ea25da48SPaolo Valente * Get next queue for service.
1453ea25da48SPaolo Valente */
bfq_get_next_queue(struct bfq_data * bfqd)1454ea25da48SPaolo Valente struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
1455ea25da48SPaolo Valente {
1456ea25da48SPaolo Valente struct bfq_entity *entity = NULL;
1457ea25da48SPaolo Valente struct bfq_sched_data *sd;
1458ea25da48SPaolo Valente struct bfq_queue *bfqq;
1459ea25da48SPaolo Valente
146073d58118SPaolo Valente if (bfq_tot_busy_queues(bfqd) == 0)
1461ea25da48SPaolo Valente return NULL;
1462ea25da48SPaolo Valente
1463ea25da48SPaolo Valente /*
1464ea25da48SPaolo Valente * Traverse the path from the root to the leaf entity to
1465ea25da48SPaolo Valente * serve. Set in service all the entities visited along the
1466ea25da48SPaolo Valente * way.
1467ea25da48SPaolo Valente */
1468ea25da48SPaolo Valente sd = &bfqd->root_group->sched_data;
1469ea25da48SPaolo Valente for (; sd ; sd = entity->my_sched_data) {
1470ea25da48SPaolo Valente /*
1471ea25da48SPaolo Valente * WARNING. We are about to set the in-service entity
1472ea25da48SPaolo Valente * to sd->next_in_service, i.e., to the (cached) value
1473ea25da48SPaolo Valente * returned by bfq_lookup_next_entity(sd) the last
1474ea25da48SPaolo Valente * time it was invoked, i.e., the last time when the
1475ea25da48SPaolo Valente * service order in sd changed as a consequence of the
1476ea25da48SPaolo Valente * activation or deactivation of an entity. In this
1477ea25da48SPaolo Valente * respect, if we execute bfq_lookup_next_entity(sd)
1478ea25da48SPaolo Valente * in this very moment, it may, although with low
1479ea25da48SPaolo Valente * probability, yield a different entity than that
1480ea25da48SPaolo Valente * pointed to by sd->next_in_service. This rare event
1481ea25da48SPaolo Valente * happens in case there was no CLASS_IDLE entity to
1482ea25da48SPaolo Valente * serve for sd when bfq_lookup_next_entity(sd) was
1483ea25da48SPaolo Valente * invoked for the last time, while there is now one
1484ea25da48SPaolo Valente * such entity.
1485ea25da48SPaolo Valente *
1486ea25da48SPaolo Valente * If the above event happens, then the scheduling of
1487ea25da48SPaolo Valente * such entity in CLASS_IDLE is postponed until the
1488ea25da48SPaolo Valente * service of the sd->next_in_service entity
1489ea25da48SPaolo Valente * finishes. In fact, when the latter is expired,
1490ea25da48SPaolo Valente * bfq_lookup_next_entity(sd) gets called again,
1491ea25da48SPaolo Valente * exactly to update sd->next_in_service.
1492ea25da48SPaolo Valente */
1493ea25da48SPaolo Valente
1494ea25da48SPaolo Valente /* Make next_in_service entity become in_service_entity */
1495ea25da48SPaolo Valente entity = sd->next_in_service;
1496ea25da48SPaolo Valente sd->in_service_entity = entity;
1497ea25da48SPaolo Valente
1498ea25da48SPaolo Valente /*
1499ea25da48SPaolo Valente * If entity is no longer a candidate for next
150046d556e6SPaolo Valente * service, then it must be extracted from its active
150146d556e6SPaolo Valente * tree, so as to make sure that it won't be
150246d556e6SPaolo Valente * considered when computing next_in_service. See the
150346d556e6SPaolo Valente * comments on the function
150446d556e6SPaolo Valente * bfq_no_longer_next_in_service() for details.
1505ea25da48SPaolo Valente */
1506ea25da48SPaolo Valente if (bfq_no_longer_next_in_service(entity))
1507ea25da48SPaolo Valente bfq_active_extract(bfq_entity_service_tree(entity),
1508ea25da48SPaolo Valente entity);
1509ea25da48SPaolo Valente
1510ea25da48SPaolo Valente /*
151146d556e6SPaolo Valente * Even if entity is not to be extracted according to
151246d556e6SPaolo Valente * the above check, a descendant entity may get
151346d556e6SPaolo Valente * extracted in one of the next iterations of this
151446d556e6SPaolo Valente * loop. Such an event could cause a change in
151546d556e6SPaolo Valente * next_in_service for the level of the descendant
151646d556e6SPaolo Valente * entity, and thus possibly back to this level.
1517ea25da48SPaolo Valente *
151846d556e6SPaolo Valente * However, we cannot perform the resulting needed
151946d556e6SPaolo Valente * update of next_in_service for this level before the
152046d556e6SPaolo Valente * end of the whole loop, because, to know which is
152146d556e6SPaolo Valente * the correct next-to-serve candidate entity for each
152246d556e6SPaolo Valente * level, we need first to find the leaf entity to set
152346d556e6SPaolo Valente * in service. In fact, only after we know which is
152446d556e6SPaolo Valente * the next-to-serve leaf entity, we can discover
152546d556e6SPaolo Valente * whether the parent entity of the leaf entity
152646d556e6SPaolo Valente * becomes the next-to-serve, and so on.
1527ea25da48SPaolo Valente */
1528ea25da48SPaolo Valente }
1529ea25da48SPaolo Valente
1530ea25da48SPaolo Valente bfqq = bfq_entity_to_bfqq(entity);
1531ea25da48SPaolo Valente
1532ea25da48SPaolo Valente /*
1533ea25da48SPaolo Valente * We can finally update all next-to-serve entities along the
1534ea25da48SPaolo Valente * path from the leaf entity just set in service to the root.
1535ea25da48SPaolo Valente */
1536ea25da48SPaolo Valente for_each_entity(entity) {
1537ea25da48SPaolo Valente struct bfq_sched_data *sd = entity->sched_data;
1538ea25da48SPaolo Valente
153980294c3bSPaolo Valente if (!bfq_update_next_in_service(sd, NULL, false))
1540ea25da48SPaolo Valente break;
1541ea25da48SPaolo Valente }
1542ea25da48SPaolo Valente
1543ea25da48SPaolo Valente return bfqq;
1544ea25da48SPaolo Valente }
1545ea25da48SPaolo Valente
1546eed47d19SPaolo Valente /* returns true if the in-service queue gets freed */
__bfq_bfqd_reset_in_service(struct bfq_data * bfqd)1547eed47d19SPaolo Valente bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd)
1548ea25da48SPaolo Valente {
1549ea25da48SPaolo Valente struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue;
1550ea25da48SPaolo Valente struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity;
1551ea25da48SPaolo Valente struct bfq_entity *entity = in_serv_entity;
1552ea25da48SPaolo Valente
1553ea25da48SPaolo Valente bfq_clear_bfqq_wait_request(in_serv_bfqq);
1554ea25da48SPaolo Valente hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
1555ea25da48SPaolo Valente bfqd->in_service_queue = NULL;
1556ea25da48SPaolo Valente
1557ea25da48SPaolo Valente /*
1558ea25da48SPaolo Valente * When this function is called, all in-service entities have
1559ea25da48SPaolo Valente * been properly deactivated or requeued, so we can safely
1560ea25da48SPaolo Valente * execute the final step: reset in_service_entity along the
1561ea25da48SPaolo Valente * path from entity to the root.
1562ea25da48SPaolo Valente */
1563ea25da48SPaolo Valente for_each_entity(entity)
1564ea25da48SPaolo Valente entity->sched_data->in_service_entity = NULL;
1565ea25da48SPaolo Valente
1566ea25da48SPaolo Valente /*
1567ea25da48SPaolo Valente * in_serv_entity is no longer in service, so, if it is in no
1568ea25da48SPaolo Valente * service tree either, then release the service reference to
1569ea25da48SPaolo Valente * the queue it represents (taken with bfq_get_entity).
1570ea25da48SPaolo Valente */
157133a16a98SPaolo Valente if (!in_serv_entity->on_st_or_in_serv) {
1572eed47d19SPaolo Valente /*
1573eed47d19SPaolo Valente * If no process is referencing in_serv_bfqq any
1574eed47d19SPaolo Valente * longer, then the service reference may be the only
1575eed47d19SPaolo Valente * reference to the queue. If this is the case, then
1576eed47d19SPaolo Valente * bfqq gets freed here.
1577eed47d19SPaolo Valente */
1578eed47d19SPaolo Valente int ref = in_serv_bfqq->ref;
1579ea25da48SPaolo Valente bfq_put_queue(in_serv_bfqq);
1580eed47d19SPaolo Valente if (ref == 1)
1581eed47d19SPaolo Valente return true;
1582eed47d19SPaolo Valente }
1583eed47d19SPaolo Valente
1584eed47d19SPaolo Valente return false;
1585ea25da48SPaolo Valente }
1586ea25da48SPaolo Valente
bfq_deactivate_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool ins_into_idle_tree,bool expiration)1587ea25da48SPaolo Valente void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1588ea25da48SPaolo Valente bool ins_into_idle_tree, bool expiration)
1589ea25da48SPaolo Valente {
1590ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity;
1591ea25da48SPaolo Valente
1592ea25da48SPaolo Valente bfq_deactivate_entity(entity, ins_into_idle_tree, expiration);
1593ea25da48SPaolo Valente }
1594ea25da48SPaolo Valente
bfq_activate_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq)1595ea25da48SPaolo Valente void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1596ea25da48SPaolo Valente {
1597ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity;
1598ea25da48SPaolo Valente
1599ea25da48SPaolo Valente bfq_activate_requeue_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq),
160080294c3bSPaolo Valente false, false);
1601ea25da48SPaolo Valente bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
1602ea25da48SPaolo Valente }
1603ea25da48SPaolo Valente
bfq_requeue_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool expiration)160480294c3bSPaolo Valente void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
160580294c3bSPaolo Valente bool expiration)
1606ea25da48SPaolo Valente {
1607ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity;
1608ea25da48SPaolo Valente
1609ea25da48SPaolo Valente bfq_activate_requeue_entity(entity, false,
161080294c3bSPaolo Valente bfqq == bfqd->in_service_queue, expiration);
1611ea25da48SPaolo Valente }
1612ea25da48SPaolo Valente
bfq_add_bfqq_in_groups_with_pending_reqs(struct bfq_queue * bfqq)16133d89bd12SYu Kuai void bfq_add_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq)
16143d89bd12SYu Kuai {
16151eb20620SYuwei Guan #ifdef CONFIG_BFQ_GROUP_IOSCHED
16163d89bd12SYu Kuai struct bfq_entity *entity = &bfqq->entity;
16173d89bd12SYu Kuai
161860a6e10cSYu Kuai if (!entity->in_groups_with_pending_reqs) {
16193d89bd12SYu Kuai entity->in_groups_with_pending_reqs = true;
162071f8ca77SYu Kuai if (!(bfqq_group(bfqq)->num_queues_with_pending_reqs++))
162171f8ca77SYu Kuai bfqq->bfqd->num_groups_with_pending_reqs++;
162260a6e10cSYu Kuai }
16231eb20620SYuwei Guan #endif
16243d89bd12SYu Kuai }
16253d89bd12SYu Kuai
bfq_del_bfqq_in_groups_with_pending_reqs(struct bfq_queue * bfqq)16263d89bd12SYu Kuai void bfq_del_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq)
16273d89bd12SYu Kuai {
16281eb20620SYuwei Guan #ifdef CONFIG_BFQ_GROUP_IOSCHED
16293d89bd12SYu Kuai struct bfq_entity *entity = &bfqq->entity;
16303d89bd12SYu Kuai
163160a6e10cSYu Kuai if (entity->in_groups_with_pending_reqs) {
16323d89bd12SYu Kuai entity->in_groups_with_pending_reqs = false;
163371f8ca77SYu Kuai if (!(--bfqq_group(bfqq)->num_queues_with_pending_reqs))
163471f8ca77SYu Kuai bfqq->bfqd->num_groups_with_pending_reqs--;
163560a6e10cSYu Kuai }
16361eb20620SYuwei Guan #endif
16373d89bd12SYu Kuai }
16383d89bd12SYu Kuai
1639ea25da48SPaolo Valente /*
1640ea25da48SPaolo Valente * Called when the bfqq no longer has requests pending, remove it from
1641ea25da48SPaolo Valente * the service tree. As a special case, it can be invoked during an
1642ea25da48SPaolo Valente * expiration.
1643ea25da48SPaolo Valente */
bfq_del_bfqq_busy(struct bfq_queue * bfqq,bool expiration)1644d322f355SYu Kuai void bfq_del_bfqq_busy(struct bfq_queue *bfqq, bool expiration)
1645ea25da48SPaolo Valente {
1646d322f355SYu Kuai struct bfq_data *bfqd = bfqq->bfqd;
1647d322f355SYu Kuai
1648ea25da48SPaolo Valente bfq_log_bfqq(bfqd, bfqq, "del from busy");
1649ea25da48SPaolo Valente
1650ea25da48SPaolo Valente bfq_clear_bfqq_busy(bfqq);
1651ea25da48SPaolo Valente
165273d58118SPaolo Valente bfqd->busy_queues[bfqq->ioprio_class - 1]--;
1653ea25da48SPaolo Valente
1654ea25da48SPaolo Valente if (bfqq->wr_coeff > 1)
1655ea25da48SPaolo Valente bfqd->wr_busy_queues--;
1656ea25da48SPaolo Valente
1657ea25da48SPaolo Valente bfqg_stats_update_dequeue(bfqq_group(bfqq));
1658ea25da48SPaolo Valente
1659ea25da48SPaolo Valente bfq_deactivate_bfqq(bfqd, bfqq, true, expiration);
16609dee8b3bSPaolo Valente
16613d89bd12SYu Kuai if (!bfqq->dispatched) {
16623d89bd12SYu Kuai bfq_del_bfqq_in_groups_with_pending_reqs(bfqq);
166371f8ca77SYu Kuai /*
166471f8ca77SYu Kuai * Next function is invoked last, because it causes bfqq to be
166571f8ca77SYu Kuai * freed. DO NOT use bfqq after the next function invocation.
166671f8ca77SYu Kuai */
1667afdba146SYu Kuai bfq_weights_tree_remove(bfqq);
1668ea25da48SPaolo Valente }
16693d89bd12SYu Kuai }
1670ea25da48SPaolo Valente
1671ea25da48SPaolo Valente /*
1672ea25da48SPaolo Valente * Called when an inactive queue receives a new request.
1673ea25da48SPaolo Valente */
bfq_add_bfqq_busy(struct bfq_queue * bfqq)1674d322f355SYu Kuai void bfq_add_bfqq_busy(struct bfq_queue *bfqq)
1675ea25da48SPaolo Valente {
1676d322f355SYu Kuai struct bfq_data *bfqd = bfqq->bfqd;
1677d322f355SYu Kuai
1678ea25da48SPaolo Valente bfq_log_bfqq(bfqd, bfqq, "add to busy");
1679ea25da48SPaolo Valente
1680ea25da48SPaolo Valente bfq_activate_bfqq(bfqd, bfqq);
1681ea25da48SPaolo Valente
1682ea25da48SPaolo Valente bfq_mark_bfqq_busy(bfqq);
168373d58118SPaolo Valente bfqd->busy_queues[bfqq->ioprio_class - 1]++;
1684ea25da48SPaolo Valente
16853d89bd12SYu Kuai if (!bfqq->dispatched) {
16863d89bd12SYu Kuai bfq_add_bfqq_in_groups_with_pending_reqs(bfqq);
1687ea25da48SPaolo Valente if (bfqq->wr_coeff == 1)
1688afdba146SYu Kuai bfq_weights_tree_add(bfqq);
16893d89bd12SYu Kuai }
1690ea25da48SPaolo Valente
1691ea25da48SPaolo Valente if (bfqq->wr_coeff > 1)
1692ea25da48SPaolo Valente bfqd->wr_busy_queues++;
16932ec5a5c4SPaolo Valente
16942ec5a5c4SPaolo Valente /* Move bfqq to the head of the woken list of its waker */
16952ec5a5c4SPaolo Valente if (!hlist_unhashed(&bfqq->woken_list_node) &&
16962ec5a5c4SPaolo Valente &bfqq->woken_list_node != bfqq->waker_bfqq->woken_list.first) {
16972ec5a5c4SPaolo Valente hlist_del_init(&bfqq->woken_list_node);
16982ec5a5c4SPaolo Valente hlist_add_head(&bfqq->woken_list_node,
16992ec5a5c4SPaolo Valente &bfqq->waker_bfqq->woken_list);
17002ec5a5c4SPaolo Valente }
1701ea25da48SPaolo Valente }
1702