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